static inline void refill_32(BitstreamContext *bc)
{
    if (bc->ptr >= bc->buffer_end)
        return;
#ifdef BITSTREAM_READER_LE
    bc->bits       = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;
#else
    bc->bits       = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);
#endif
    bc->ptr       += 4;
    bc->bits_left += 32;
}
['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}', '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 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 inline void refill_32(BitstreamContext *bc)\n{\n    if (bc->ptr >= bc->buffer_end)\n        return;\n#ifdef BITSTREAM_READER_LE\n    bc->bits       = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;\n#else\n    bc->bits       = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);\n#endif\n    bc->ptr       += 4;\n    bc->bits_left += 32;\n}']