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 magy_decode_frame(AVCodecContext *avctx, void *data,\n                             int *got_frame, AVPacket *avpkt)\n{\n    MagicYUVContext *s = avctx->priv_data;\n    ThreadFrame frame = { .f = data };\n    AVFrame *p = data;\n    GetByteContext gbyte;\n    BitstreamContext bc;\n    uint32_t first_offset, offset, next_offset, header_size, slice_width;\n    int width, height, format, version, table_size;\n    int ret, i, j, k;\n    bytestream2_init(&gbyte, avpkt->data, avpkt->size);\n    if (bytestream2_get_le32(&gbyte) != MKTAG(\'M\', \'A\', \'G\', \'Y\'))\n        return AVERROR_INVALIDDATA;\n    header_size = bytestream2_get_le32(&gbyte);\n    if (header_size < 32 || header_size >= avpkt->size) {\n        av_log(avctx, AV_LOG_ERROR,\n               "header or packet too small %"PRIu32"\\n", header_size);\n        return AVERROR_INVALIDDATA;\n    }\n    version = bytestream2_get_byte(&gbyte);\n    if (version != 7) {\n        avpriv_request_sample(avctx, "Version %d", version);\n        return AVERROR_PATCHWELCOME;\n    }\n    s->hshift[1] =\n    s->vshift[1] =\n    s->hshift[2] =\n    s->vshift[2] = 0;\n    s->decorrelate = 0;\n    format = bytestream2_get_byte(&gbyte);\n    switch (format) {\n    case 0x65:\n        avctx->pix_fmt = AV_PIX_FMT_GBRP;\n        s->decorrelate = 1;\n        break;\n    case 0x66:\n        avctx->pix_fmt = AV_PIX_FMT_GBRAP;\n        s->decorrelate = 1;\n        break;\n    case 0x67:\n        avctx->pix_fmt = AV_PIX_FMT_YUV444P;\n        break;\n    case 0x68:\n        avctx->pix_fmt = AV_PIX_FMT_YUV422P;\n        s->hshift[1] =\n        s->hshift[2] = 1;\n        break;\n    case 0x69:\n        avctx->pix_fmt = AV_PIX_FMT_YUV420P;\n        s->hshift[1] =\n        s->vshift[1] =\n        s->hshift[2] =\n        s->vshift[2] = 1;\n        break;\n    case 0x6a:\n        avctx->pix_fmt = AV_PIX_FMT_YUVA444P;\n        break;\n    case 0x6b:\n        avctx->pix_fmt = AV_PIX_FMT_GRAY8;\n        break;\n    default:\n        avpriv_request_sample(avctx, "Format 0x%X", format);\n        return AVERROR_PATCHWELCOME;\n    }\n    s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);\n    bytestream2_skip(&gbyte, 2);\n    s->interlaced = !!(bytestream2_get_byte(&gbyte) & 2);\n    bytestream2_skip(&gbyte, 3);\n    width  = bytestream2_get_le32(&gbyte);\n    height = bytestream2_get_le32(&gbyte);\n    ret = ff_set_dimensions(avctx, width, height);\n    if (ret < 0)\n        return ret;\n    slice_width = bytestream2_get_le32(&gbyte);\n    if (slice_width != width) {\n        avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);\n        return AVERROR_PATCHWELCOME;\n    }\n    s->slice_height = bytestream2_get_le32(&gbyte);\n    if (s->slice_height <= 0 || s->slice_height > INT_MAX - height) {\n        av_log(avctx, AV_LOG_ERROR,\n               "invalid slice height: %d\\n", s->slice_height);\n        return AVERROR_INVALIDDATA;\n    }\n    bytestream2_skip(&gbyte, 4);\n    s->nb_slices = (height + s->slice_height - 1) / s->slice_height;\n    if (s->nb_slices > INT_MAX / sizeof(Slice)) {\n        av_log(avctx, AV_LOG_ERROR,\n               "invalid number of slices: %d\\n", s->nb_slices);\n        return AVERROR_INVALIDDATA;\n    }\n    for (i = 0; i < s->planes; i++) {\n        av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));\n        if (!s->slices[i])\n            return AVERROR(ENOMEM);\n        offset = bytestream2_get_le32(&gbyte);\n        if (offset >= avpkt->size - header_size)\n            return AVERROR_INVALIDDATA;\n        if (i == 0)\n            first_offset = offset;\n        for (j = 0; j < s->nb_slices - 1; j++) {\n            s->slices[i][j].start = offset + header_size;\n            next_offset = bytestream2_get_le32(&gbyte);\n            if (next_offset <= offset || next_offset >= avpkt->size - header_size)\n                return AVERROR_INVALIDDATA;\n            s->slices[i][j].size = next_offset - offset;\n            offset = next_offset;\n        }\n        s->slices[i][j].start = offset + header_size;\n        s->slices[i][j].size  = avpkt->size - s->slices[i][j].start;\n    }\n    if (bytestream2_get_byte(&gbyte) != s->planes)\n        return AVERROR_INVALIDDATA;\n    bytestream2_skip(&gbyte, s->nb_slices * s->planes);\n    table_size = header_size + first_offset - bytestream2_tell(&gbyte);\n    if (table_size < 2)\n        return AVERROR_INVALIDDATA;\n    ret = bitstream_init8(&bc, avpkt->data + bytestream2_tell(&gbyte), table_size);\n    if (ret < 0)\n        return ret;\n    memset(s->len, 0, sizeof(s->len));\n    j = i = 0;\n    while (bitstream_bits_left(&bc) >= 8) {\n        int b = bitstream_read(&bc, 4);\n        int x = bitstream_read(&bc, 4);\n        int l = bitstream_read(&bc, b) + 1;\n        for (k = 0; k < l; k++)\n            if (j + k < 256)\n                s->len[i][j + k] = x;\n        j += l;\n        if (j == 256) {\n            j = 0;\n            if (huff_build(&s->vlc[i], s->len[i])) {\n                av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\\n");\n                return AVERROR_INVALIDDATA;\n            }\n            i++;\n            if (i == s->planes) {\n                break;\n            }\n        } else if (j > 256) {\n            return AVERROR_INVALIDDATA;\n        }\n    }\n    if (i != s->planes) {\n        av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    p->pict_type = AV_PICTURE_TYPE_I;\n    p->key_frame = 1;\n    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)\n        return ret;\n    s->buf = avpkt->data;\n    s->p = p;\n    avctx->execute2(avctx, magy_decode_slice, NULL, NULL, s->nb_slices);\n    if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||\n        avctx->pix_fmt == AV_PIX_FMT_GBRAP) {\n        FFSWAP(uint8_t*, p->data[0], p->data[1]);\n        FFSWAP(int, p->linesize[0], p->linesize[1]);\n    }\n    *got_frame = 1;\n    return avpkt->size;\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 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}']