static void encode_frame(MpegAudioContext *s,
                         unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],
                         int padding)
{
    int i, j, k, l, bit_alloc_bits, b, ch;
    unsigned char *sf;
    int q[3];
    PutBitContext *p = &s->pb;
    put_bits(p, 12, 0xfff);
    put_bits(p, 1, 1 - s->lsf);
    put_bits(p, 2, 4-2);
    put_bits(p, 1, 1);
    put_bits(p, 4, s->bitrate_index);
    put_bits(p, 2, s->freq_index);
    put_bits(p, 1, s->do_padding);
    put_bits(p, 1, 0);
    put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);
    put_bits(p, 2, 0);
    put_bits(p, 1, 0);
    put_bits(p, 1, 1);
    put_bits(p, 2, 0);
    j = 0;
    for(i=0;i<s->sblimit;i++) {
        bit_alloc_bits = s->alloc_table[j];
        for(ch=0;ch<s->nb_channels;ch++) {
            put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);
        }
        j += 1 << bit_alloc_bits;
    }
    for(i=0;i<s->sblimit;i++) {
        for(ch=0;ch<s->nb_channels;ch++) {
            if (bit_alloc[ch][i])
                put_bits(p, 2, s->scale_code[ch][i]);
        }
    }
    for(i=0;i<s->sblimit;i++) {
        for(ch=0;ch<s->nb_channels;ch++) {
            if (bit_alloc[ch][i]) {
                sf = &s->scale_factors[ch][i][0];
                switch(s->scale_code[ch][i]) {
                case 0:
                    put_bits(p, 6, sf[0]);
                    put_bits(p, 6, sf[1]);
                    put_bits(p, 6, sf[2]);
                    break;
                case 3:
                case 1:
                    put_bits(p, 6, sf[0]);
                    put_bits(p, 6, sf[2]);
                    break;
                case 2:
                    put_bits(p, 6, sf[0]);
                    break;
                }
            }
        }
    }
    for(k=0;k<3;k++) {
        for(l=0;l<12;l+=3) {
            j = 0;
            for(i=0;i<s->sblimit;i++) {
                bit_alloc_bits = s->alloc_table[j];
                for(ch=0;ch<s->nb_channels;ch++) {
                    b = bit_alloc[ch][i];
                    if (b) {
                        int qindex, steps, m, sample, bits;
                        qindex = s->alloc_table[j+b];
                        steps = ff_mpa_quant_steps[qindex];
                        for(m=0;m<3;m++) {
                            sample = s->sb_samples[ch][k][l + m][i];
#ifdef USE_FLOATS
                            {
                                float a;
                                a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];
                                q[m] = (int)((a + 1.0) * steps * 0.5);
                            }
#else
                            {
                                int q1, e, shift, mult;
                                e = s->scale_factors[ch][i][k];
                                shift = scale_factor_shift[e];
                                mult = scale_factor_mult[e];
                                if (shift < 0)
                                    q1 = sample << (-shift);
                                else
                                    q1 = sample >> shift;
                                q1 = (q1 * mult) >> P;
                                q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);
                            }
#endif
                            if (q[m] >= steps)
                                q[m] = steps - 1;
                            assert(q[m] >= 0 && q[m] < steps);
                        }
                        bits = ff_mpa_quant_bits[qindex];
                        if (bits < 0) {
                            put_bits(p, -bits,
                                     q[0] + steps * (q[1] + steps * q[2]));
#if 0
                            printf("%d: gr1 %d\n",
                                   i, q[0] + steps * (q[1] + steps * q[2]));
#endif
                        } else {
#if 0
                            printf("%d: gr3 %d %d %d\n",
                                   i, q[0], q[1], q[2]);
#endif
                            put_bits(p, bits, q[0]);
                            put_bits(p, bits, q[1]);
                            put_bits(p, bits, q[2]);
                        }
                    }
                }
                j += 1 << bit_alloc_bits;
            }
        }
    }
    for(i=0;i<padding;i++)
        put_bits(p, 1, 0);
    flush_put_bits(p);
}
['static void encode_frame(MpegAudioContext *s,\n                         unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],\n                         int padding)\n{\n    int i, j, k, l, bit_alloc_bits, b, ch;\n    unsigned char *sf;\n    int q[3];\n    PutBitContext *p = &s->pb;\n    put_bits(p, 12, 0xfff);\n    put_bits(p, 1, 1 - s->lsf);\n    put_bits(p, 2, 4-2);\n    put_bits(p, 1, 1);\n    put_bits(p, 4, s->bitrate_index);\n    put_bits(p, 2, s->freq_index);\n    put_bits(p, 1, s->do_padding);\n    put_bits(p, 1, 0);\n    put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);\n    put_bits(p, 2, 0);\n    put_bits(p, 1, 0);\n    put_bits(p, 1, 1);\n    put_bits(p, 2, 0);\n    j = 0;\n    for(i=0;i<s->sblimit;i++) {\n        bit_alloc_bits = s->alloc_table[j];\n        for(ch=0;ch<s->nb_channels;ch++) {\n            put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);\n        }\n        j += 1 << bit_alloc_bits;\n    }\n    for(i=0;i<s->sblimit;i++) {\n        for(ch=0;ch<s->nb_channels;ch++) {\n            if (bit_alloc[ch][i])\n                put_bits(p, 2, s->scale_code[ch][i]);\n        }\n    }\n    for(i=0;i<s->sblimit;i++) {\n        for(ch=0;ch<s->nb_channels;ch++) {\n            if (bit_alloc[ch][i]) {\n                sf = &s->scale_factors[ch][i][0];\n                switch(s->scale_code[ch][i]) {\n                case 0:\n                    put_bits(p, 6, sf[0]);\n                    put_bits(p, 6, sf[1]);\n                    put_bits(p, 6, sf[2]);\n                    break;\n                case 3:\n                case 1:\n                    put_bits(p, 6, sf[0]);\n                    put_bits(p, 6, sf[2]);\n                    break;\n                case 2:\n                    put_bits(p, 6, sf[0]);\n                    break;\n                }\n            }\n        }\n    }\n    for(k=0;k<3;k++) {\n        for(l=0;l<12;l+=3) {\n            j = 0;\n            for(i=0;i<s->sblimit;i++) {\n                bit_alloc_bits = s->alloc_table[j];\n                for(ch=0;ch<s->nb_channels;ch++) {\n                    b = bit_alloc[ch][i];\n                    if (b) {\n                        int qindex, steps, m, sample, bits;\n                        qindex = s->alloc_table[j+b];\n                        steps = ff_mpa_quant_steps[qindex];\n                        for(m=0;m<3;m++) {\n                            sample = s->sb_samples[ch][k][l + m][i];\n#ifdef USE_FLOATS\n                            {\n                                float a;\n                                a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];\n                                q[m] = (int)((a + 1.0) * steps * 0.5);\n                            }\n#else\n                            {\n                                int q1, e, shift, mult;\n                                e = s->scale_factors[ch][i][k];\n                                shift = scale_factor_shift[e];\n                                mult = scale_factor_mult[e];\n                                if (shift < 0)\n                                    q1 = sample << (-shift);\n                                else\n                                    q1 = sample >> shift;\n                                q1 = (q1 * mult) >> P;\n                                q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);\n                            }\n#endif\n                            if (q[m] >= steps)\n                                q[m] = steps - 1;\n                            assert(q[m] >= 0 && q[m] < steps);\n                        }\n                        bits = ff_mpa_quant_bits[qindex];\n                        if (bits < 0) {\n                            put_bits(p, -bits,\n                                     q[0] + steps * (q[1] + steps * q[2]));\n#if 0\n                            printf("%d: gr1 %d\\n",\n                                   i, q[0] + steps * (q[1] + steps * q[2]));\n#endif\n                        } else {\n#if 0\n                            printf("%d: gr3 %d %d %d\\n",\n                                   i, q[0], q[1], q[2]);\n#endif\n                            put_bits(p, bits, q[0]);\n                            put_bits(p, bits, q[1]);\n                            put_bits(p, bits, q[2]);\n                        }\n                    }\n                }\n                j += 1 << bit_alloc_bits;\n            }\n        }\n    }\n    for(i=0;i<padding;i++)\n        put_bits(p, 1, 0);\n    flush_put_bits(p);\n}']