static void exponents_from_scale_factors(MPADecodeContext *s,
                                         GranuleDef *g,
                                         int16_t *exponents)
{
    const uint8_t *bstab, *pretab;
    int len, i, j, k, l, v0, shift, gain, gains[3];
    int16_t *exp_ptr;
    exp_ptr = exponents;
    gain = g->global_gain - 210;
    shift = g->scalefac_scale + 1;
    bstab = band_size_long[s->sample_rate_index];
    pretab = mpa_pretab[g->preflag];
    for(i=0;i<g->long_end;i++) {
        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;
        len = bstab[i];
        for(j=len;j>0;j--)
            *exp_ptr++ = v0;
    }
    if (g->short_start < 13) {
        bstab = band_size_short[s->sample_rate_index];
        gains[0] = gain - (g->subblock_gain[0] << 3);
        gains[1] = gain - (g->subblock_gain[1] << 3);
        gains[2] = gain - (g->subblock_gain[2] << 3);
        k = g->long_end;
        for(i=g->short_start;i<13;i++) {
            len = bstab[i];
            for(l=0;l<3;l++) {
                v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;
                for(j=len;j>0;j--)
                *exp_ptr++ = v0;
            }
        }
    }
}
['static int decode_frame_adu(AVCodecContext * avctx,\n                        void *data, int *data_size,\n                        const uint8_t * buf, int buf_size)\n{\n    MPADecodeContext *s = avctx->priv_data;\n    uint32_t header;\n    int len, out_size;\n    OUT_INT *out_samples = data;\n    len = buf_size;\n    if (buf_size < HEADER_SIZE) {\n        *data_size = 0;\n        return buf_size;\n    }\n    if (len > MPA_MAX_CODED_FRAME_SIZE)\n        len = MPA_MAX_CODED_FRAME_SIZE;\n    header = AV_RB32(buf) | 0xffe00000;\n    if (ff_mpa_check_header(header) < 0) {\n        *data_size = 0;\n        return buf_size;\n    }\n    ff_mpegaudio_decode_header(s, header);\n    avctx->sample_rate = s->sample_rate;\n    avctx->channels = s->nb_channels;\n    avctx->bit_rate = s->bit_rate;\n    avctx->sub_id = s->layer;\n    s->frame_size = len;\n    if (avctx->parse_only) {\n        out_size = buf_size;\n    } else {\n        out_size = mp_decode_frame(s, out_samples, buf, buf_size);\n    }\n    *data_size = out_size;\n    return buf_size;\n}', 'int ff_mpegaudio_decode_header(MPADecodeContext *s, uint32_t header)\n{\n    int sample_rate, frame_size, mpeg25, padding;\n    int sample_rate_index, bitrate_index;\n    if (header & (1<<20)) {\n        s->lsf = (header & (1<<19)) ? 0 : 1;\n        mpeg25 = 0;\n    } else {\n        s->lsf = 1;\n        mpeg25 = 1;\n    }\n    s->layer = 4 - ((header >> 17) & 3);\n    sample_rate_index = (header >> 10) & 3;\n    sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);\n    sample_rate_index += 3 * (s->lsf + mpeg25);\n    s->sample_rate_index = sample_rate_index;\n    s->error_protection = ((header >> 16) & 1) ^ 1;\n    s->sample_rate = sample_rate;\n    bitrate_index = (header >> 12) & 0xf;\n    padding = (header >> 9) & 1;\n    s->mode = (header >> 6) & 3;\n    s->mode_ext = (header >> 4) & 3;\n    if (s->mode == MPA_MONO)\n        s->nb_channels = 1;\n    else\n        s->nb_channels = 2;\n    if (bitrate_index != 0) {\n        frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];\n        s->bit_rate = frame_size * 1000;\n        switch(s->layer) {\n        case 1:\n            frame_size = (frame_size * 12000) / sample_rate;\n            frame_size = (frame_size + padding) * 4;\n            break;\n        case 2:\n            frame_size = (frame_size * 144000) / sample_rate;\n            frame_size += padding;\n            break;\n        default:\n        case 3:\n            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);\n            frame_size += padding;\n            break;\n        }\n        s->frame_size = frame_size;\n    } else {\n        return 1;\n    }\n#if defined(DEBUG)\n    dprintf(s->avctx, "layer%d, %d Hz, %d kbits/s, ",\n           s->layer, s->sample_rate, s->bit_rate);\n    if (s->nb_channels == 2) {\n        if (s->layer == 3) {\n            if (s->mode_ext & MODE_EXT_MS_STEREO)\n                dprintf(s->avctx, "ms-");\n            if (s->mode_ext & MODE_EXT_I_STEREO)\n                dprintf(s->avctx, "i-");\n        }\n        dprintf(s->avctx, "stereo");\n    } else {\n        dprintf(s->avctx, "mono");\n    }\n    dprintf(s->avctx, "\\n");\n#endif\n    return 0;\n}', 'static int mp_decode_frame(MPADecodeContext *s,\n                           OUT_INT *samples, const uint8_t *buf, int buf_size)\n{\n    int i, nb_frames, ch;\n    OUT_INT *samples_ptr;\n    init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);\n    if (s->error_protection)\n        skip_bits(&s->gb, 16);\n    dprintf(s->avctx, "frame %d:\\n", s->frame_count);\n    switch(s->layer) {\n    case 1:\n        s->avctx->frame_size = 384;\n        nb_frames = mp_decode_layer1(s);\n        break;\n    case 2:\n        s->avctx->frame_size = 1152;\n        nb_frames = mp_decode_layer2(s);\n        break;\n    case 3:\n        s->avctx->frame_size = s->lsf ? 576 : 1152;\n    default:\n        nb_frames = mp_decode_layer3(s);\n        s->last_buf_size=0;\n        if(s->in_gb.buffer){\n            align_get_bits(&s->gb);\n            i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;\n            if(i >= 0 && i <= BACKSTEP_SIZE){\n                memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);\n                s->last_buf_size=i;\n            }else\n                av_log(NULL, AV_LOG_ERROR, "invalid old backstep %d\\n", i);\n            s->gb= s->in_gb;\n            s->in_gb.buffer= NULL;\n        }\n        align_get_bits(&s->gb);\n        assert((get_bits_count(&s->gb) & 7) == 0);\n        i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;\n        if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){\n            av_log(NULL, AV_LOG_ERROR, "invalid new backstep %d\\n", i);\n            i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);\n        }\n        assert(i <= buf_size - HEADER_SIZE && i>= 0);\n        memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);\n        s->last_buf_size += i;\n        break;\n    }\n#if defined(DEBUG)\n    for(i=0;i<nb_frames;i++) {\n        for(ch=0;ch<s->nb_channels;ch++) {\n            int j;\n            dprintf(s->avctx, "%d-%d:", i, ch);\n            for(j=0;j<SBLIMIT;j++)\n                dprintf(s->avctx, " %0.6f", (double)s->sb_samples[ch][i][j] / FRAC_ONE);\n            dprintf(s->avctx, "\\n");\n        }\n    }\n#endif\n    for(ch=0;ch<s->nb_channels;ch++) {\n        samples_ptr = samples + ch;\n        for(i=0;i<nb_frames;i++) {\n            ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),\n                         window, &s->dither_state,\n                         samples_ptr, s->nb_channels,\n                         s->sb_samples[ch][i]);\n            samples_ptr += 32 * s->nb_channels;\n        }\n    }\n#ifdef DEBUG\n    s->frame_count++;\n#endif\n    return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;\n}', 'static int mp_decode_layer3(MPADecodeContext *s)\n{\n    int nb_granules, main_data_begin, private_bits;\n    int gr, ch, blocksplit_flag, i, j, k, n, bits_pos;\n    GranuleDef granules[2][2], *g;\n    int16_t exponents[576];\n    if (s->lsf) {\n        main_data_begin = get_bits(&s->gb, 8);\n        private_bits = get_bits(&s->gb, s->nb_channels);\n        nb_granules = 1;\n    } else {\n        main_data_begin = get_bits(&s->gb, 9);\n        if (s->nb_channels == 2)\n            private_bits = get_bits(&s->gb, 3);\n        else\n            private_bits = get_bits(&s->gb, 5);\n        nb_granules = 2;\n        for(ch=0;ch<s->nb_channels;ch++) {\n            granules[ch][0].scfsi = 0;\n            granules[ch][1].scfsi = get_bits(&s->gb, 4);\n        }\n    }\n    for(gr=0;gr<nb_granules;gr++) {\n        for(ch=0;ch<s->nb_channels;ch++) {\n            dprintf(s->avctx, "gr=%d ch=%d: side_info\\n", gr, ch);\n            g = &granules[ch][gr];\n            g->part2_3_length = get_bits(&s->gb, 12);\n            g->big_values = get_bits(&s->gb, 9);\n            if(g->big_values > 288){\n                av_log(s->avctx, AV_LOG_ERROR, "big_values too big\\n");\n                return -1;\n            }\n            g->global_gain = get_bits(&s->gb, 8);\n            if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) ==\n                MODE_EXT_MS_STEREO)\n                g->global_gain -= 2;\n            if (s->lsf)\n                g->scalefac_compress = get_bits(&s->gb, 9);\n            else\n                g->scalefac_compress = get_bits(&s->gb, 4);\n            blocksplit_flag = get_bits1(&s->gb);\n            if (blocksplit_flag) {\n                g->block_type = get_bits(&s->gb, 2);\n                if (g->block_type == 0){\n                    av_log(NULL, AV_LOG_ERROR, "invalid block type\\n");\n                    return -1;\n                }\n                g->switch_point = get_bits1(&s->gb);\n                for(i=0;i<2;i++)\n                    g->table_select[i] = get_bits(&s->gb, 5);\n                for(i=0;i<3;i++)\n                    g->subblock_gain[i] = get_bits(&s->gb, 3);\n                ff_init_short_region(s, g);\n            } else {\n                int region_address1, region_address2;\n                g->block_type = 0;\n                g->switch_point = 0;\n                for(i=0;i<3;i++)\n                    g->table_select[i] = get_bits(&s->gb, 5);\n                region_address1 = get_bits(&s->gb, 4);\n                region_address2 = get_bits(&s->gb, 3);\n                dprintf(s->avctx, "region1=%d region2=%d\\n",\n                        region_address1, region_address2);\n                ff_init_long_region(s, g, region_address1, region_address2);\n            }\n            ff_region_offset2size(g);\n            ff_compute_band_indexes(s, g);\n            g->preflag = 0;\n            if (!s->lsf)\n                g->preflag = get_bits1(&s->gb);\n            g->scalefac_scale = get_bits1(&s->gb);\n            g->count1table_select = get_bits1(&s->gb);\n            dprintf(s->avctx, "block_type=%d switch_point=%d\\n",\n                    g->block_type, g->switch_point);\n        }\n    }\n  if (!s->adu_mode) {\n    const uint8_t *ptr = s->gb.buffer + (get_bits_count(&s->gb)>>3);\n    assert((get_bits_count(&s->gb) & 7) == 0);\n    dprintf(s->avctx, "seekback: %d\\n", main_data_begin);\n    memcpy(s->last_buf + s->last_buf_size, ptr, EXTRABYTES);\n    s->in_gb= s->gb;\n        init_get_bits(&s->gb, s->last_buf, s->last_buf_size*8);\n        skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin));\n  }\n    for(gr=0;gr<nb_granules;gr++) {\n        for(ch=0;ch<s->nb_channels;ch++) {\n            g = &granules[ch][gr];\n            if(get_bits_count(&s->gb)<0){\n                av_log(NULL, AV_LOG_ERROR, "mdb:%d, lastbuf:%d skipping granule %d\\n",\n                                            main_data_begin, s->last_buf_size, gr);\n                skip_bits_long(&s->gb, g->part2_3_length);\n                memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid));\n                if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->in_gb.buffer){\n                    skip_bits_long(&s->in_gb, get_bits_count(&s->gb) - s->gb.size_in_bits);\n                    s->gb= s->in_gb;\n                    s->in_gb.buffer=NULL;\n                }\n                continue;\n            }\n            bits_pos = get_bits_count(&s->gb);\n            if (!s->lsf) {\n                uint8_t *sc;\n                int slen, slen1, slen2;\n                slen1 = slen_table[0][g->scalefac_compress];\n                slen2 = slen_table[1][g->scalefac_compress];\n                dprintf(s->avctx, "slen1=%d slen2=%d\\n", slen1, slen2);\n                if (g->block_type == 2) {\n                    n = g->switch_point ? 17 : 18;\n                    j = 0;\n                    if(slen1){\n                        for(i=0;i<n;i++)\n                            g->scale_factors[j++] = get_bits(&s->gb, slen1);\n                    }else{\n                        for(i=0;i<n;i++)\n                            g->scale_factors[j++] = 0;\n                    }\n                    if(slen2){\n                        for(i=0;i<18;i++)\n                            g->scale_factors[j++] = get_bits(&s->gb, slen2);\n                        for(i=0;i<3;i++)\n                            g->scale_factors[j++] = 0;\n                    }else{\n                        for(i=0;i<21;i++)\n                            g->scale_factors[j++] = 0;\n                    }\n                } else {\n                    sc = granules[ch][0].scale_factors;\n                    j = 0;\n                    for(k=0;k<4;k++) {\n                        n = (k == 0 ? 6 : 5);\n                        if ((g->scfsi & (0x8 >> k)) == 0) {\n                            slen = (k < 2) ? slen1 : slen2;\n                            if(slen){\n                                for(i=0;i<n;i++)\n                                    g->scale_factors[j++] = get_bits(&s->gb, slen);\n                            }else{\n                                for(i=0;i<n;i++)\n                                    g->scale_factors[j++] = 0;\n                            }\n                        } else {\n                            for(i=0;i<n;i++) {\n                                g->scale_factors[j] = sc[j];\n                                j++;\n                            }\n                        }\n                    }\n                    g->scale_factors[j++] = 0;\n                }\n#if defined(DEBUG)\n                {\n                    dprintf(s->avctx, "scfsi=%x gr=%d ch=%d scale_factors:\\n",\n                           g->scfsi, gr, ch);\n                    for(i=0;i<j;i++)\n                        dprintf(s->avctx, " %d", g->scale_factors[i]);\n                    dprintf(s->avctx, "\\n");\n                }\n#endif\n            } else {\n                int tindex, tindex2, slen[4], sl, sf;\n                if (g->block_type == 2) {\n                    tindex = g->switch_point ? 2 : 1;\n                } else {\n                    tindex = 0;\n                }\n                sf = g->scalefac_compress;\n                if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) {\n                    sf >>= 1;\n                    if (sf < 180) {\n                        lsf_sf_expand(slen, sf, 6, 6, 0);\n                        tindex2 = 3;\n                    } else if (sf < 244) {\n                        lsf_sf_expand(slen, sf - 180, 4, 4, 0);\n                        tindex2 = 4;\n                    } else {\n                        lsf_sf_expand(slen, sf - 244, 3, 0, 0);\n                        tindex2 = 5;\n                    }\n                } else {\n                    if (sf < 400) {\n                        lsf_sf_expand(slen, sf, 5, 4, 4);\n                        tindex2 = 0;\n                    } else if (sf < 500) {\n                        lsf_sf_expand(slen, sf - 400, 5, 4, 0);\n                        tindex2 = 1;\n                    } else {\n                        lsf_sf_expand(slen, sf - 500, 3, 0, 0);\n                        tindex2 = 2;\n                        g->preflag = 1;\n                    }\n                }\n                j = 0;\n                for(k=0;k<4;k++) {\n                    n = lsf_nsf_table[tindex2][tindex][k];\n                    sl = slen[k];\n                    if(sl){\n                        for(i=0;i<n;i++)\n                            g->scale_factors[j++] = get_bits(&s->gb, sl);\n                    }else{\n                        for(i=0;i<n;i++)\n                            g->scale_factors[j++] = 0;\n                    }\n                }\n                for(;j<40;j++)\n                    g->scale_factors[j] = 0;\n#if defined(DEBUG)\n                {\n                    dprintf(s->avctx, "gr=%d ch=%d scale_factors:\\n",\n                           gr, ch);\n                    for(i=0;i<40;i++)\n                        dprintf(s->avctx, " %d", g->scale_factors[i]);\n                    dprintf(s->avctx, "\\n");\n                }\n#endif\n            }\n            exponents_from_scale_factors(s, g, exponents);\n            huffman_decode(s, g, exponents, bits_pos + g->part2_3_length);\n#if defined(DEBUG)\n            sample_dump(0, g->sb_hybrid, 576);\n#endif\n        }\n        if (s->nb_channels == 2)\n            compute_stereo(s, &granules[0][gr], &granules[1][gr]);\n        for(ch=0;ch<s->nb_channels;ch++) {\n            g = &granules[ch][gr];\n            reorder_block(s, g);\n#if defined(DEBUG)\n            sample_dump(0, g->sb_hybrid, 576);\n#endif\n            s->compute_antialias(s, g);\n#if defined(DEBUG)\n            sample_dump(1, g->sb_hybrid, 576);\n#endif\n            compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]);\n#if defined(DEBUG)\n            sample_dump(2, &s->sb_samples[ch][18 * gr][0], 576);\n#endif\n        }\n    }\n    if(get_bits_count(&s->gb)<0)\n        skip_bits_long(&s->gb, -get_bits_count(&s->gb));\n    return nb_granules * 18;\n}', 'static void exponents_from_scale_factors(MPADecodeContext *s,\n                                         GranuleDef *g,\n                                         int16_t *exponents)\n{\n    const uint8_t *bstab, *pretab;\n    int len, i, j, k, l, v0, shift, gain, gains[3];\n    int16_t *exp_ptr;\n    exp_ptr = exponents;\n    gain = g->global_gain - 210;\n    shift = g->scalefac_scale + 1;\n    bstab = band_size_long[s->sample_rate_index];\n    pretab = mpa_pretab[g->preflag];\n    for(i=0;i<g->long_end;i++) {\n        v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400;\n        len = bstab[i];\n        for(j=len;j>0;j--)\n            *exp_ptr++ = v0;\n    }\n    if (g->short_start < 13) {\n        bstab = band_size_short[s->sample_rate_index];\n        gains[0] = gain - (g->subblock_gain[0] << 3);\n        gains[1] = gain - (g->subblock_gain[1] << 3);\n        gains[2] = gain - (g->subblock_gain[2] << 3);\n        k = g->long_end;\n        for(i=g->short_start;i<13;i++) {\n            len = bstab[i];\n            for(l=0;l<3;l++) {\n                v0 = gains[l] - (g->scale_factors[k++] << shift) + 400;\n                for(j=len;j>0;j--)\n                *exp_ptr++ = v0;\n            }\n        }\n    }\n}']