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_slice(AVCodecContext *avctx, void *tdata,\n                             int j, int threadnr)\n{\n    MagicYUVContext *s = avctx->priv_data;\n    int interlaced = s->interlaced;\n    AVFrame *p = s->p;\n    int i, k, x;\n    BitstreamContext bc;\n    uint8_t *dst;\n    for (i = 0; i < s->planes; i++) {\n        int left, lefttop, top;\n        int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->height - j * s->slice_height), s->vshift[i]);\n        int width = AV_CEIL_RSHIFT(avctx->width, s->hshift[i]);\n        int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);\n        ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);\n        ptrdiff_t stride = p->linesize[i];\n        int flags, pred;\n        int ret = bitstream_init8(&bc, s->buf + s->slices[i][j].start,\n                                  s->slices[i][j].size);\n        if (ret < 0)\n            return ret;\n        flags = bitstream_read(&bc, 8);\n        pred  = bitstream_read(&bc, 8);\n        dst = p->data[i] + j * sheight * stride;\n        if (flags & 1) {\n            for (k = 0; k < height; k++) {\n                for (x = 0; x < width; x++)\n                    dst[x] = bitstream_read(&bc, 8);\n                dst += stride;\n            }\n        } else {\n            for (k = 0; k < height; k++) {\n                for (x = 0; x < width; x++) {\n                    int pix;\n                    if (bitstream_bits_left(&bc) <= 0)\n                        return AVERROR_INVALIDDATA;\n                    pix = bitstream_read_vlc(&bc, s->vlc[i].table, s->vlc[i].bits, 3);\n                    if (pix < 0)\n                        return AVERROR_INVALIDDATA;\n                    dst[x] = 255 - pix;\n                }\n                dst += stride;\n            }\n        }\n        switch (pred) {\n        case LEFT:\n            dst = p->data[i] + j * sheight * stride;\n            s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);\n            dst += stride;\n            if (interlaced) {\n                s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);\n                dst += stride;\n            }\n            for (k = 1 + interlaced; k < height; k++) {\n                s->hdsp.add_hfyu_left_pred(dst, dst, width, dst[-fake_stride]);\n                dst += stride;\n            }\n            break;\n        case GRADIENT:\n            dst = p->data[i] + j * sheight * stride;\n            s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);\n            left = lefttop = 0;\n            dst += stride;\n            if (interlaced) {\n                s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);\n                left = lefttop = 0;\n                dst += stride;\n            }\n            for (k = 1 + interlaced; k < height; k++) {\n                top = dst[-fake_stride];\n                left = top + dst[0];\n                dst[0] = left;\n                for (x = 1; x < width; x++) {\n                    top = dst[x - fake_stride];\n                    lefttop = dst[x - (fake_stride + 1)];\n                    left += top - lefttop + dst[x];\n                    dst[x] = left;\n                }\n                dst += stride;\n            }\n            break;\n        case MEDIAN:\n            dst = p->data[i] + j * sheight * stride;\n            lefttop = left = dst[0];\n            s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);\n            dst += stride;\n            if (interlaced) {\n                lefttop = left = dst[0];\n                s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);\n                dst += stride;\n            }\n            for (k = 1 + interlaced; k < height; k++) {\n                s->hdsp.add_hfyu_median_pred(dst, dst - fake_stride,\n                                             dst, width, &left, &lefttop);\n                lefttop = left = dst[0];\n                dst += stride;\n            }\n            break;\n        default:\n            avpriv_request_sample(avctx, "Unknown prediction: %d", pred);\n        }\n    }\n    if (s->decorrelate) {\n        int height = FFMIN(s->slice_height, avctx->height - j * s->slice_height);\n        int width = avctx->width;\n        uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];\n        uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];\n        uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];\n        for (i = 0; i < height; i++) {\n            s->hdsp.add_bytes(b, g, width);\n            s->hdsp.add_bytes(r, g, width);\n            b += p->linesize[0];\n            g += p->linesize[1];\n            r += p->linesize[2];\n        }\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 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}']