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 tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,\n                     const uint8_t **poutbuf, int *poutbuf_size,\n                     const uint8_t *buf, int buf_size)\n{\n    TAKParseContext *t = s->priv_data;\n    ParseContext *pc   = &t->pc;\n    int next           = END_NOT_FOUND;\n    BitstreamContext bc;\n    int consumed = 0;\n    int needed   = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;\n    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {\n        TAKStreamInfo ti;\n        bitstream_init(&bc, buf, buf_size);\n        if (!ff_tak_decode_frame_header(avctx, &bc, &ti, 127))\n            s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples\n                                                   : t->ti.frame_samples;\n        *poutbuf      = buf;\n        *poutbuf_size = buf_size;\n        return buf_size;\n    }\n    while (buf_size || t->index + needed <= pc->index) {\n        if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {\n            int tmp_buf_size       = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES,\n                                           buf_size);\n            const uint8_t *tmp_buf = buf;\n            ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size);\n            consumed += tmp_buf_size;\n            buf      += tmp_buf_size;\n            buf_size -= tmp_buf_size;\n        }\n        for (; t->index + needed <= pc->index; t->index++)\n            if (pc->buffer[t->index]     == 0xFF &&\n                pc->buffer[t->index + 1] == 0xA0) {\n                TAKStreamInfo ti;\n                bitstream_init8(&bc, pc->buffer + t->index,\n                                pc->index - t->index);\n                if (!ff_tak_decode_frame_header(avctx, &bc,\n                                                pc->frame_start_found ? &ti\n                                                                      : &t->ti,\n                                                127) &&\n                    !ff_tak_check_crc(pc->buffer + t->index,\n                                      bitstream_tell(&bc) / 8)) {\n                    if (!pc->frame_start_found) {\n                        pc->frame_start_found = 1;\n                        s->duration           = t->ti.last_frame_samples ?\n                                                t->ti.last_frame_samples :\n                                                t->ti.frame_samples;\n                    } else {\n                        pc->frame_start_found = 0;\n                        next                  = t->index - pc->index;\n                        t->index              = 0;\n                        goto found;\n                    }\n                }\n            }\n    }\nfound:\n    if (consumed && !buf_size && next == END_NOT_FOUND ||\n        ff_combine_frame(pc, next, &buf, &buf_size) < 0) {\n        *poutbuf      = NULL;\n        *poutbuf_size = 0;\n        return buf_size + consumed;\n    }\n    if (next != END_NOT_FOUND) {\n        next        += consumed;\n        pc->overread = FFMAX(0, -next);\n    }\n    *poutbuf      = buf;\n    *poutbuf_size = buf_size;\n    return next;\n}', 'static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,\n                                 unsigned bit_size)\n{\n    unsigned buffer_size;\n    if (bit_size > INT_MAX - 7 || !buffer) {\n        buffer        =\n        bc->buffer    =\n        bc->ptr       = NULL;\n        bc->bits_left = 0;\n        return AVERROR_INVALIDDATA;\n    }\n    buffer_size = (bit_size + 7) >> 3;\n    bc->buffer       = buffer;\n    bc->buffer_end   = buffer + buffer_size;\n    bc->ptr          = bc->buffer;\n    bc->size_in_bits = bit_size;\n    bc->bits_left    = 0;\n    bc->bits         = 0;\n    refill_64(bc);\n    return 0;\n}', 'int ff_tak_decode_frame_header(AVCodecContext *avctx, BitstreamContext *bc,\n                               TAKStreamInfo *ti, int log_level_offset)\n{\n    if (bitstream_read(bc, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {\n        av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    ti->flags     = bitstream_read(bc, TAK_FRAME_HEADER_FLAGS_BITS);\n    ti->frame_num = bitstream_read(bc, TAK_FRAME_HEADER_NO_BITS);\n    if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {\n        ti->last_frame_samples = bitstream_read(bc, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;\n        bitstream_skip(bc, 2);\n    } else {\n        ti->last_frame_samples = 0;\n    }\n    if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {\n        avpriv_tak_parse_streaminfo(bc, ti);\n        if (bitstream_read(bc, 6))\n            bitstream_skip(bc, 25);\n        bitstream_align(bc);\n    }\n    bitstream_skip(bc, 24);\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 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}']