static inline int get_context(PlaneContext *p, int16_t *src,
                              int16_t *last, int16_t *last2)
{
    const int LT = last[-1];
    const int T  = last[0];
    const int RT = last[1];
    const int L  = src[-1];
    if (p->quant_table[3][127]) {
        const int TT = last2[0];
        const int LL = src[-2];
        return p->quant_table[0][(L - LT) & 0xFF] +
               p->quant_table[1][(LT - T) & 0xFF] +
               p->quant_table[2][(T - RT) & 0xFF] +
               p->quant_table[3][(LL - L) & 0xFF] +
               p->quant_table[4][(TT - T) & 0xFF];
    } else
        return p->quant_table[0][(L - LT) & 0xFF] +
               p->quant_table[1][(LT - T) & 0xFF] +
               p->quant_table[2][(T - RT) & 0xFF];
}
['static int encode_slice(AVCodecContext *c, void *arg)\n{\n    FFV1Context *fs  = *(void **)arg;\n    FFV1Context *f   = fs->avctx->priv_data;\n    int width        = fs->slice_width;\n    int height       = fs->slice_height;\n    int x            = fs->slice_x;\n    int y            = fs->slice_y;\n    const AVFrame *const p = f->frame;\n    const int ps     = (av_pix_fmt_desc_get(c->pix_fmt)->flags & AV_PIX_FMT_FLAG_PLANAR)\n                       ? (f->bits_per_raw_sample > 8) + 1\n                       : 4;\n    if (c->coded_frame->key_frame)\n        ffv1_clear_slice_state(f, fs);\n    if (f->version > 2) {\n        encode_slice_header(f, fs);\n    }\n    if (!fs->ac) {\n        if (f->version > 2)\n            put_rac(&fs->c, (uint8_t[]) { 129 }, 0);\n        fs->ac_byte_count = f->version > 2 || (!x && !y) ? ff_rac_terminate( &fs->c) : 0;\n        init_put_bits(&fs->pb, fs->c.bytestream_start + fs->ac_byte_count,\n                      fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count);\n    }\n    if (f->colorspace == 0) {\n        const int chroma_width  = -((-width) >> f->chroma_h_shift);\n        const int chroma_height = -((-height) >> f->chroma_v_shift);\n        const int cx            = x >> f->chroma_h_shift;\n        const int cy            = y >> f->chroma_v_shift;\n        encode_plane(fs, p->data[0] + ps * x + y * p->linesize[0],\n                     width, height, p->linesize[0], 0);\n        if (f->chroma_planes) {\n            encode_plane(fs, p->data[1] + ps * cx + cy * p->linesize[1],\n                         chroma_width, chroma_height, p->linesize[1], 1);\n            encode_plane(fs, p->data[2] + ps * cx + cy * p->linesize[2],\n                         chroma_width, chroma_height, p->linesize[2], 1);\n        }\n        if (fs->transparency)\n            encode_plane(fs, p->data[3] + ps * x + y * p->linesize[3], width,\n                         height, p->linesize[3], 2);\n    } else {\n        const uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],\n                                     p->data[1] + ps * x + y * p->linesize[1],\n                                     p->data[2] + ps * x + y * p->linesize[2] };\n        encode_rgb_frame(fs, planes, width, height, p->linesize);\n    }\n    emms_c();\n    return 0;\n}', 'static void encode_rgb_frame(FFV1Context *s, const uint8_t *src[3],\n                             int w, int h, const int stride[3])\n{\n    int x, y, p, i;\n    const int ring_size = s->avctx->context_model ? 3 : 2;\n    int16_t *sample[MAX_PLANES][3];\n    int lbd  = s->avctx->bits_per_raw_sample <= 8;\n    int bits = s->avctx->bits_per_raw_sample > 0\n               ? s->avctx->bits_per_raw_sample\n               : 8;\n    int offset = 1 << bits;\n    s->run_index = 0;\n    memset(s->sample_buffer, 0, ring_size * MAX_PLANES *\n                                (w + 6) * sizeof(*s->sample_buffer));\n    for (y = 0; y < h; y++) {\n        for (i = 0; i < ring_size; i++)\n            for (p = 0; p < MAX_PLANES; p++)\n                sample[p][i] = s->sample_buffer + p * ring_size *\n                               (w + 6) +\n                               ((h + i - y) % ring_size) * (w + 6) + 3;\n        for (x = 0; x < w; x++) {\n            int b, g, r, av_uninit(a);\n            if (lbd) {\n                unsigned v = *((const uint32_t *)(src[0] + x * 4 + stride[0] * y));\n                b = v & 0xFF;\n                g = (v >> 8) & 0xFF;\n                r = (v >> 16) & 0xFF;\n                a = v >> 24;\n            } else {\n                b = *((const uint16_t *)(src[0] + x * 2 + stride[0] * y));\n                g = *((const uint16_t *)(src[1] + x * 2 + stride[1] * y));\n                r = *((const uint16_t *)(src[2] + x * 2 + stride[2] * y));\n            }\n            b -= g;\n            r -= g;\n            g += (b + r) >> 2;\n            b += offset;\n            r += offset;\n            sample[0][0][x] = g;\n            sample[1][0][x] = b;\n            sample[2][0][x] = r;\n            sample[3][0][x] = a;\n        }\n        for (p = 0; p < 3 + s->transparency; p++) {\n            sample[p][0][-1] = sample[p][1][0];\n            sample[p][1][w]  = sample[p][1][w - 1];\n            if (lbd)\n                encode_line(s, w, sample[p], (p + 1) / 2, 9);\n            else\n                encode_line(s, w, sample[p], (p + 1) / 2, bits + 1);\n        }\n    }\n}', 'static av_always_inline int encode_line(FFV1Context *s, int w,\n                                        int16_t *sample[3],\n                                        int plane_index, int bits)\n{\n    PlaneContext *const p = &s->plane[plane_index];\n    RangeCoder *const c   = &s->c;\n    int x;\n    int run_index = s->run_index;\n    int run_count = 0;\n    int run_mode  = 0;\n    if (s->ac) {\n        if (c->bytestream_end - c->bytestream < w * 20) {\n            av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\\n");\n            return AVERROR_INVALIDDATA;\n        }\n    } else {\n        if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < w * 4) {\n            av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\\n");\n            return AVERROR_INVALIDDATA;\n        }\n    }\n    for (x = 0; x < w; x++) {\n        int diff, context;\n        context = get_context(p, sample[0] + x, sample[1] + x, sample[2] + x);\n        diff    = sample[0][x] - predict(sample[0] + x, sample[1] + x);\n        if (context < 0) {\n            context = -context;\n            diff    = -diff;\n        }\n        diff = fold(diff, bits);\n        if (s->ac) {\n            if (s->flags & CODEC_FLAG_PASS1) {\n                put_symbol_inline(c, p->state[context], diff, 1, s->rc_stat,\n                                  s->rc_stat2[p->quant_table_index][context]);\n            } else {\n                put_symbol_inline(c, p->state[context], diff, 1, NULL, NULL);\n            }\n        } else {\n            if (context == 0)\n                run_mode = 1;\n            if (run_mode) {\n                if (diff) {\n                    while (run_count >= 1 << ff_log2_run[run_index]) {\n                        run_count -= 1 << ff_log2_run[run_index];\n                        run_index++;\n                        put_bits(&s->pb, 1, 1);\n                    }\n                    put_bits(&s->pb, 1 + ff_log2_run[run_index], run_count);\n                    if (run_index)\n                        run_index--;\n                    run_count = 0;\n                    run_mode  = 0;\n                    if (diff > 0)\n                        diff--;\n                } else {\n                    run_count++;\n                }\n            }\n            av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\\n",\n                    run_count, run_index, run_mode, x,\n                    (int)put_bits_count(&s->pb));\n            if (run_mode == 0)\n                put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);\n        }\n    }\n    if (run_mode) {\n        while (run_count >= 1 << ff_log2_run[run_index]) {\n            run_count -= 1 << ff_log2_run[run_index];\n            run_index++;\n            put_bits(&s->pb, 1, 1);\n        }\n        if (run_count)\n            put_bits(&s->pb, 1, 1);\n    }\n    s->run_index = run_index;\n    return 0;\n}', 'static inline int get_context(PlaneContext *p, int16_t *src,\n                              int16_t *last, int16_t *last2)\n{\n    const int LT = last[-1];\n    const int T  = last[0];\n    const int RT = last[1];\n    const int L  = src[-1];\n    if (p->quant_table[3][127]) {\n        const int TT = last2[0];\n        const int LL = src[-2];\n        return p->quant_table[0][(L - LT) & 0xFF] +\n               p->quant_table[1][(LT - T) & 0xFF] +\n               p->quant_table[2][(T - RT) & 0xFF] +\n               p->quant_table[3][(LL - L) & 0xFF] +\n               p->quant_table[4][(TT - T) & 0xFF];\n    } else\n        return p->quant_table[0][(L - LT) & 0xFF] +\n               p->quant_table[1][(LT - T) & 0xFF] +\n               p->quant_table[2][(T - RT) & 0xFF];\n}']