static inline uint64_t get_val(BitstreamContext *bc, unsigned n)
{
#ifdef BITSTREAM_READER_LE
    uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);
    bc->bits >>= n;
#else
    uint64_t ret = bc->bits >> (64 - n);
    bc->bits <<= n;
#endif
    bc->bits_left -= n;
    return ret;
}
['static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)\n{\n    ALSSpecificConfig *sconf = &ctx->sconf;\n    AVCodecContext *avctx    = ctx->avctx;\n    BitstreamContext *bc     = &ctx->bc;\n    unsigned int div_blocks[32];\n    unsigned int c;\n    unsigned int js_blocks[2];\n    uint32_t bs_info = 0;\n    int ret;\n    if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)\n        bitstream_skip(bc, 32);\n    if (sconf->mc_coding && sconf->joint_stereo) {\n        ctx->js_switch = bitstream_read_bit(bc);\n        bitstream_align(bc);\n    }\n    if (!sconf->mc_coding || ctx->js_switch) {\n        int independent_bs = !sconf->joint_stereo;\n        for (c = 0; c < avctx->channels; c++) {\n            js_blocks[0] = 0;\n            js_blocks[1] = 0;\n            get_block_sizes(ctx, div_blocks, &bs_info);\n            if (sconf->joint_stereo && sconf->block_switching)\n                if (bs_info >> 31)\n                    independent_bs = 2;\n            if (c == avctx->channels - 1)\n                independent_bs = 1;\n            if (independent_bs) {\n                ret = decode_blocks_ind(ctx, ra_frame, c,\n                                        div_blocks, js_blocks);\n                if (ret < 0)\n                    return ret;\n                independent_bs--;\n            } else {\n                ret = decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks);\n                if (ret < 0)\n                    return ret;\n                c++;\n            }\n            memmove(ctx->raw_samples[c] - sconf->max_order,\n                    ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,\n                    sizeof(*ctx->raw_samples[c]) * sconf->max_order);\n        }\n    } else {\n        ALSBlockData   bd = { 0 };\n        int            b, ret;\n        int            *reverted_channels = ctx->reverted_channels;\n        unsigned int   offset             = 0;\n        for (c = 0; c < avctx->channels; c++)\n            if (ctx->chan_data[c] < ctx->chan_data_buffer) {\n                av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\\n");\n                return AVERROR_INVALIDDATA;\n            }\n        memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);\n        bd.ra_block         = ra_frame;\n        bd.prev_raw_samples = ctx->prev_raw_samples;\n        get_block_sizes(ctx, div_blocks, &bs_info);\n        for (b = 0; b < ctx->num_blocks; b++) {\n            bd.block_length = div_blocks[b];\n            if (bd.block_length <= 0) {\n                av_log(ctx->avctx, AV_LOG_WARNING,\n                       "Invalid block length %u in channel data!\\n",\n                       bd.block_length);\n                continue;\n            }\n            for (c = 0; c < avctx->channels; c++) {\n                bd.const_block = ctx->const_block + c;\n                bd.shift_lsbs  = ctx->shift_lsbs + c;\n                bd.opt_order   = ctx->opt_order + c;\n                bd.store_prev_samples = ctx->store_prev_samples + c;\n                bd.use_ltp     = ctx->use_ltp + c;\n                bd.ltp_lag     = ctx->ltp_lag + c;\n                bd.ltp_gain    = ctx->ltp_gain[c];\n                bd.lpc_cof     = ctx->lpc_cof[c];\n                bd.quant_cof   = ctx->quant_cof[c];\n                bd.raw_samples = ctx->raw_samples[c] + offset;\n                bd.raw_other   = NULL;\n                if ((ret = read_block(ctx, &bd)) < 0)\n                    return ret;\n                if ((ret = read_channel_data(ctx, ctx->chan_data[c], c)) < 0)\n                    return ret;\n            }\n            for (c = 0; c < avctx->channels; c++) {\n                ret = revert_channel_correlation(ctx, &bd, ctx->chan_data,\n                                                 reverted_channels, offset, c);\n                if (ret < 0)\n                    return ret;\n            }\n            for (c = 0; c < avctx->channels; c++) {\n                bd.const_block = ctx->const_block + c;\n                bd.shift_lsbs  = ctx->shift_lsbs + c;\n                bd.opt_order   = ctx->opt_order + c;\n                bd.store_prev_samples = ctx->store_prev_samples + c;\n                bd.use_ltp     = ctx->use_ltp + c;\n                bd.ltp_lag     = ctx->ltp_lag + c;\n                bd.ltp_gain    = ctx->ltp_gain[c];\n                bd.lpc_cof     = ctx->lpc_cof[c];\n                bd.quant_cof   = ctx->quant_cof[c];\n                bd.raw_samples = ctx->raw_samples[c] + offset;\n                if ((ret = decode_block(ctx, &bd)) < 0)\n                    return ret;\n            }\n            memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));\n            offset      += div_blocks[b];\n            bd.ra_block  = 0;\n        }\n        for (c = 0; c < avctx->channels; c++)\n            memmove(ctx->raw_samples[c] - sconf->max_order,\n                    ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,\n                    sizeof(*ctx->raw_samples[c]) * sconf->max_order);\n    }\n    return 0;\n}', 'static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,\n                            uint32_t *bs_info)\n{\n    ALSSpecificConfig *sconf     = &ctx->sconf;\n    BitstreamContext *bc         = &ctx->bc;\n    unsigned int *ptr_div_blocks = div_blocks;\n    unsigned int b;\n    if (sconf->block_switching) {\n        unsigned int bs_info_len = 1 << (sconf->block_switching + 2);\n        *bs_info = bitstream_read(bc, bs_info_len);\n        *bs_info <<= (32 - bs_info_len);\n    }\n    ctx->num_blocks = 0;\n    parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);\n    for (b = 0; b < ctx->num_blocks; b++)\n        div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];\n    if (ctx->cur_frame_length != ctx->sconf.frame_length) {\n        unsigned int remaining = ctx->cur_frame_length;\n        for (b = 0; b < ctx->num_blocks; b++) {\n            if (remaining <= div_blocks[b]) {\n                div_blocks[b] = remaining;\n                ctx->num_blocks = b + 1;\n                break;\n            }\n            remaining -= div_blocks[b];\n        }\n    }\n}', 'static int read_block(ALSDecContext *ctx, ALSBlockData *bd)\n{\n    int ret = 0;\n    BitstreamContext *bc = &ctx->bc;\n    *bd->shift_lsbs = 0;\n    if (bitstream_read_bit(bc)) {\n        ret = read_var_block_data(ctx, bd);\n    } else {\n        read_const_block_data(ctx, bd);\n    }\n    return ret;\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n    if (!bc->bits_left)\n        refill_64(bc);\n    return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n    uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n    bc->bits >>= n;\n#else\n    uint64_t ret = bc->bits >> (64 - n);\n    bc->bits <<= n;\n#endif\n    bc->bits_left -= n;\n    return ret;\n}', 'static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)\n{\n    ALSSpecificConfig *sconf = &ctx->sconf;\n    AVCodecContext *avctx    = ctx->avctx;\n    BitstreamContext *bc     = &ctx->bc;\n    unsigned int k;\n    unsigned int s[8];\n    unsigned int sx[8];\n    unsigned int sub_blocks, log2_sub_blocks, sb_length;\n    unsigned int start      = 0;\n    unsigned int opt_order;\n    int          sb;\n    int32_t      *quant_cof = bd->quant_cof;\n    int32_t      *current_res;\n    *bd->const_block = 0;\n    *bd->opt_order  = 1;\n    bd->js_blocks   = bitstream_read_bit(bc);\n    opt_order       = *bd->opt_order;\n    if (!sconf->bgmc && !sconf->sb_part) {\n        log2_sub_blocks = 0;\n    } else {\n        if (sconf->bgmc && sconf->sb_part)\n            log2_sub_blocks = bitstream_read(bc, 2);\n        else\n            log2_sub_blocks = 2 * bitstream_read_bit(bc);\n    }\n    sub_blocks = 1 << log2_sub_blocks;\n    if (bd->block_length & (sub_blocks - 1)) {\n        av_log(avctx, AV_LOG_WARNING,\n               "Block length is not evenly divisible by the number of subblocks.\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    sb_length = bd->block_length >> log2_sub_blocks;\n    if (sconf->bgmc) {\n        s[0] = bitstream_read(bc, 8 + (sconf->resolution > 1));\n        for (k = 1; k < sub_blocks; k++)\n            s[k] = s[k - 1] + decode_rice(bc, 2);\n        for (k = 0; k < sub_blocks; k++) {\n            sx[k]   = s[k] & 0x0F;\n            s [k] >>= 4;\n        }\n    } else {\n        s[0] = bitstream_read(bc, 4 + (sconf->resolution > 1));\n        for (k = 1; k < sub_blocks; k++)\n            s[k] = s[k - 1] + decode_rice(bc, 0);\n    }\n    for (k = 1; k < sub_blocks; k++)\n        if (s[k] > 32) {\n            av_log(avctx, AV_LOG_ERROR, "k invalid for rice code.\\n");\n            return AVERROR_INVALIDDATA;\n        }\n    if (bitstream_read_bit(bc))\n        *bd->shift_lsbs = bitstream_read(bc, 4) + 1;\n    *bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || *bd->shift_lsbs;\n    if (!sconf->rlslms) {\n        if (sconf->adapt_order && sconf->max_order) {\n            int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1,\n                                                2, sconf->max_order + 1));\n            *bd->opt_order       = bitstream_read(bc, opt_order_length);\n            if (*bd->opt_order > sconf->max_order) {\n                *bd->opt_order = sconf->max_order;\n                av_log(avctx, AV_LOG_ERROR, "Predictor order too large!\\n");\n                return AVERROR_INVALIDDATA;\n            }\n        } else {\n            *bd->opt_order = sconf->max_order;\n        }\n        opt_order = *bd->opt_order;\n        if (opt_order) {\n            int add_base;\n            if (sconf->coef_table == 3) {\n                add_base = 0x7F;\n                quant_cof[0] = 32 * parcor_scaled_values[bitstream_read(bc, 7)];\n                if (opt_order > 1)\n                    quant_cof[1] = -32 * parcor_scaled_values[bitstream_read(bc, 7)];\n                for (k = 2; k < opt_order; k++)\n                    quant_cof[k] = bitstream_read(bc, 7);\n            } else {\n                int k_max;\n                add_base = 1;\n                k_max = FFMIN(opt_order, 20);\n                for (k = 0; k < k_max; k++) {\n                    int rice_param = parcor_rice_table[sconf->coef_table][k][1];\n                    int offset     = parcor_rice_table[sconf->coef_table][k][0];\n                    quant_cof[k] = decode_rice(bc, rice_param) + offset;\n                    if (quant_cof[k] < -64 || quant_cof[k] > 63) {\n                        av_log(avctx, AV_LOG_ERROR,\n                               "quant_cof %"PRIu32" is out of range\\n",\n                               quant_cof[k]);\n                        return AVERROR_INVALIDDATA;\n                    }\n                }\n                k_max = FFMIN(opt_order, 127);\n                for (; k < k_max; k++)\n                    quant_cof[k] = decode_rice(bc, 2) + (k & 1);\n                for (; k < opt_order; k++)\n                    quant_cof[k] = decode_rice(bc, 1);\n                quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];\n                if (opt_order > 1)\n                    quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];\n            }\n            for (k = 2; k < opt_order; k++)\n                quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);\n        }\n    }\n    if (sconf->long_term_prediction) {\n        *bd->use_ltp = bitstream_read_bit(bc);\n        if (*bd->use_ltp) {\n            int r, c;\n            bd->ltp_gain[0]   = decode_rice(bc, 1) << 3;\n            bd->ltp_gain[1]   = decode_rice(bc, 2) << 3;\n            r                 = get_unary(bc, 0, 3);\n            c                 = bitstream_read(bc, 2);\n            bd->ltp_gain[2]   = ltp_gain_values[r][c];\n            bd->ltp_gain[3]   = decode_rice(bc, 2) << 3;\n            bd->ltp_gain[4]   = decode_rice(bc, 1) << 3;\n            *bd->ltp_lag      = bitstream_read(bc, ctx->ltp_lag_length);\n            *bd->ltp_lag     += FFMAX(4, opt_order + 1);\n        }\n    }\n    if (bd->ra_block) {\n        if (opt_order)\n            bd->raw_samples[0] = decode_rice(bc, avctx->bits_per_raw_sample - 4);\n        if (opt_order > 1)\n            bd->raw_samples[1] = decode_rice(bc, FFMIN(s[0] + 3, ctx->s_max));\n        if (opt_order > 2)\n            bd->raw_samples[2] = decode_rice(bc, FFMIN(s[0] + 1, ctx->s_max));\n        start = FFMIN(opt_order, 3);\n    }\n    if (sconf->bgmc) {\n        int          delta[8];\n        unsigned int k    [8];\n        unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5);\n        unsigned int high;\n        unsigned int low;\n        unsigned int value;\n        ff_bgmc_decode_init(bc, &high, &low, &value);\n        current_res = bd->raw_samples + start;\n        for (sb = 0; sb < sub_blocks; sb++) {\n            unsigned int sb_len  = sb_length - (sb ? 0 : start);\n            k    [sb] = s[sb] > b ? s[sb] - b : 0;\n            delta[sb] = 5 - s[sb] + k[sb];\n            ff_bgmc_decode(bc, sb_len, current_res, delta[sb], sx[sb], &high,\n                           &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status);\n            current_res += sb_len;\n        }\n        ff_bgmc_decode_end(bc);\n        current_res = bd->raw_samples + start;\n        for (sb = 0; sb < sub_blocks; sb++, start = 0) {\n            unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]];\n            unsigned int cur_k         = k[sb];\n            unsigned int cur_s         = s[sb];\n            for (; start < sb_length; start++) {\n                int32_t res = *current_res;\n                if (res == cur_tail_code) {\n                    unsigned int max_msb =   (2 + (sx[sb] > 2) + (sx[sb] > 10))\n                                          << (5 - delta[sb]);\n                    res = decode_rice(bc, cur_s);\n                    if (res >= 0) {\n                        res += (max_msb    ) << cur_k;\n                    } else {\n                        res -= (max_msb - 1) << cur_k;\n                    }\n                } else {\n                    if (res > cur_tail_code)\n                        res--;\n                    if (res & 1)\n                        res = -res;\n                    res >>= 1;\n                    if (cur_k) {\n                        res <<= cur_k;\n                        res  |= bitstream_read(bc, cur_k);\n                    }\n                }\n                *current_res++ = res;\n            }\n        }\n    } else {\n        current_res = bd->raw_samples + start;\n        for (sb = 0; sb < sub_blocks; sb++, start = 0)\n            for (; start < sb_length; start++)\n                *current_res++ = decode_rice(bc, s[sb]);\n     }\n    if (!sconf->mc_coding || ctx->js_switch)\n        bitstream_align(bc);\n    return 0;\n}']