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 decode_channel(TAKDecContext *s, int chan)\n{\n    AVCodecContext *avctx = s->avctx;\n    BitstreamContext *bc  = &s->bc;\n    int32_t *decoded      = s->decoded[chan];\n    int left              = s->nb_samples - 1;\n    int i, prev, ret, nb_subframes;\n    int subframe_len[MAX_SUBFRAMES];\n    s->sample_shift[chan] = bits_esc4(bc);\n    if (s->sample_shift[chan] >= avctx->bits_per_coded_sample)\n        return AVERROR_INVALIDDATA;\n    *decoded++        = bitstream_read_signed(bc,\n                                              avctx->bits_per_coded_sample -\n                                              s->sample_shift[chan]);\n    s->lpc_mode[chan] = bitstream_read(bc, 2);\n    nb_subframes      = bitstream_read(bc, 3) + 1;\n    i = 0;\n    if (nb_subframes > 1) {\n        if (bitstream_bits_left(bc) < (nb_subframes - 1) * 6)\n            return AVERROR_INVALIDDATA;\n        prev = 0;\n        for (; i < nb_subframes - 1; i++) {\n            int subframe_end = bitstream_read(bc, 6) * s->subframe_scale;\n            if (subframe_end <= prev)\n                return AVERROR_INVALIDDATA;\n            subframe_len[i] = subframe_end - prev;\n            left           -= subframe_len[i];\n            prev            = subframe_end;\n        }\n        if (left <= 0)\n            return AVERROR_INVALIDDATA;\n    }\n    subframe_len[i] = left;\n    prev = 0;\n    for (i = 0; i < nb_subframes; i++) {\n        if ((ret = decode_subframe(s, decoded, subframe_len[i], prev)) < 0)\n            return ret;\n        decoded += subframe_len[i];\n        prev     = subframe_len[i];\n    }\n    return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n    if (!n)\n        return 0;\n    if (n > bc->bits_left) {\n        refill_32(bc);\n        if (bc->bits_left < 32)\n            bc->bits_left = n;\n    }\n    return get_val(bc, n);\n}', 'static int decode_subframe(TAKDecContext *s, int32_t *decoded,\n                           int subframe_size, int prev_subframe_size)\n{\n    LOCAL_ALIGNED_16(int16_t, filter, [MAX_PREDICTORS]);\n    BitstreamContext *bc = &s->bc;\n    int i, ret;\n    int dshift, size, filter_quant, filter_order;\n    memset(filter, 0, MAX_PREDICTORS * sizeof(*filter));\n    if (!bitstream_read_bit(bc))\n        return decode_residues(s, decoded, subframe_size);\n    filter_order = predictor_sizes[bitstream_read(bc, 4)];\n    if (prev_subframe_size > 0 && bitstream_read_bit(bc)) {\n        if (filter_order > prev_subframe_size)\n            return AVERROR_INVALIDDATA;\n        decoded       -= filter_order;\n        subframe_size += filter_order;\n        if (filter_order > subframe_size)\n            return AVERROR_INVALIDDATA;\n    } else {\n        int lpc_mode;\n        if (filter_order > subframe_size)\n            return AVERROR_INVALIDDATA;\n        lpc_mode = bitstream_read(bc, 2);\n        if (lpc_mode > 2)\n            return AVERROR_INVALIDDATA;\n        if ((ret = decode_residues(s, decoded, filter_order)) < 0)\n            return ret;\n        if (lpc_mode)\n            decode_lpc(decoded, lpc_mode, filter_order);\n    }\n    dshift = bits_esc4(bc);\n    size   = bitstream_read_bit(bc) + 6;\n    filter_quant = 10;\n    if (bitstream_read_bit(bc)) {\n        filter_quant -= bitstream_read(bc, 3) + 1;\n        if (filter_quant < 3)\n            return AVERROR_INVALIDDATA;\n    }\n    decode_filter_coeffs(s, filter_order, size, filter_quant, filter);\n    if ((ret = decode_residues(s, &decoded[filter_order],\n                               subframe_size - filter_order)) < 0)\n        return ret;\n    av_fast_malloc(&s->residues, &s->residues_buf_size,\n                   FFALIGN(subframe_size + 16, 16) * sizeof(*s->residues));\n    if (!s->residues)\n        return AVERROR(ENOMEM);\n    memset(s->residues, 0, s->residues_buf_size);\n    for (i = 0; i < filter_order; i++)\n        s->residues[i] = *decoded++ >> dshift;\n    for (i = 0; i < subframe_size - filter_order; i++) {\n        int v = 1 << (filter_quant - 1);\n        v += s->adsp.scalarproduct_int16(&s->residues[i], filter,\n                                         FFALIGN(filter_order, 16));\n        v = (av_clip_intp2(v >> filter_quant, 13) << dshift) - *decoded;\n        *decoded++ = v;\n        s->residues[filter_order + i] = v >> dshift;\n    }\n    emms_c();\n    return 0;\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 decode_residues(TAKDecContext *s, int32_t *decoded, int length)\n{\n    BitstreamContext *bc = &s->bc;\n    int i, mode, ret;\n    if (length > s->nb_samples)\n        return AVERROR_INVALIDDATA;\n    if (bitstream_read_bit(bc)) {\n        int wlength, rval;\n        int coding_mode[128];\n        wlength = length / s->uval;\n        rval = length - (wlength * s->uval);\n        if (rval < s->uval / 2)\n            rval += s->uval;\n        else\n            wlength++;\n        if (wlength <= 1 || wlength > 128)\n            return AVERROR_INVALIDDATA;\n        coding_mode[0] =\n        mode           = bitstream_read(bc, 6);\n        for (i = 1; i < wlength; i++) {\n            int c = get_unary(bc, 1, 6);\n            switch (c) {\n            case 6:\n                mode = bitstream_read(bc, 6);\n                break;\n            case 5:\n            case 4:\n            case 3: {\n                int sign = bitstream_read_bit(bc);\n                mode    += (-sign ^ (c - 1)) + sign;\n                break;\n            }\n            case 2:\n                mode++;\n                break;\n            case 1:\n                mode--;\n                break;\n            }\n            coding_mode[i] = mode;\n        }\n        i = 0;\n        while (i < wlength) {\n            int len = 0;\n            mode = coding_mode[i];\n            do {\n                if (i >= wlength - 1)\n                    len += rval;\n                else\n                    len += s->uval;\n                i++;\n                if (i == wlength)\n                    break;\n            } while (coding_mode[i] == mode);\n            if ((ret = decode_segment(bc, mode, decoded, len)) < 0)\n                return ret;\n            decoded += len;\n        }\n    } else {\n        mode = bitstream_read(bc, 6);\n        if ((ret = decode_segment(bc, mode, decoded, length)) < 0)\n            return ret;\n    }\n    return 0;\n}']