static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){
    PlaneContext * const p= &s->plane[plane_index];
    RangeCoder * const c= &s->c;
    int x;
    int run_index= s->run_index;
    int run_count=0;
    int run_mode=0;
    if(s->ac){
        if(c->bytestream_end - c->bytestream < w*20){
            av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
            return -1;
        }
    }else{
        if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < w*4){
            av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
            return -1;
        }
    }
    for(x=0; x<w; x++){
        int diff, context;
        context= get_context(s, sample[0]+x, sample[1]+x, sample[2]+x);
        diff= sample[0][x] - predict(sample[0]+x, sample[1]+x);
        if(context < 0){
            context = -context;
            diff= -diff;
        }
        diff= fold(diff, bits);
        if(s->ac){
            put_symbol(c, p->state[context], diff, 1);
        }else{
            if(context == 0) run_mode=1;
            if(run_mode){
                if(diff){
                    while(run_count >= 1<<log2_run[run_index]){
                        run_count -= 1<<log2_run[run_index];
                        run_index++;
                        put_bits(&s->pb, 1, 1);
                    }
                    put_bits(&s->pb, 1 + log2_run[run_index], run_count);
                    if(run_index) run_index--;
                    run_count=0;
                    run_mode=0;
                    if(diff>0) diff--;
                }else{
                    run_count++;
                }
            }
            if(run_mode == 0)
                put_vlc_symbol(&s->pb, &p->vlc_state[context], diff, bits);
        }
    }
    if(run_mode){
        while(run_count >= 1<<log2_run[run_index]){
            run_count -= 1<<log2_run[run_index];
            run_index++;
            put_bits(&s->pb, 1, 1);
        }
        if(run_count)
            put_bits(&s->pb, 1, 1);
    }
    s->run_index= run_index;
    return 0;
}
['static void encode_rgb_frame(FFV1Context *s, uint32_t *src, int w, int h, int stride){\n    int x, y, p, i;\n    const int ring_size= s->avctx->context_model ? 3 : 2;\n    int_fast16_t sample_buffer[3][ring_size][w+6], *sample[3][ring_size];\n    s->run_index=0;\n    memset(sample_buffer, 0, sizeof(sample_buffer));\n    for(y=0; y<h; y++){\n        for(i=0; i<ring_size; i++)\n            for(p=0; p<3; p++)\n                sample[p][i]= sample_buffer[p][(h+i-y)%ring_size]+3;\n        for(x=0; x<w; x++){\n            int v= src[x + stride*y];\n            int b= v&0xFF;\n            int g= (v>>8)&0xFF;\n            int r= (v>>16)&0xFF;\n            b -= g;\n            r -= g;\n            g += (b + r)>>2;\n            b += 0x100;\n            r += 0x100;\n            sample[0][0][x]= g;\n            sample[1][0][x]= b;\n            sample[2][0][x]= r;\n        }\n        for(p=0; p<3; p++){\n            sample[p][0][-1]= sample[p][1][0  ];\n            sample[p][1][ w]= sample[p][1][w-1];\n            encode_line(s, w, sample[p], FFMIN(p, 1), 9);\n        }\n    }\n}', 'static inline int encode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){\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 -1;\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 -1;\n        }\n    }\n    for(x=0; x<w; x++){\n        int diff, context;\n        context= get_context(s, 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            put_symbol(c, p->state[context], diff, 1);\n        }else{\n            if(context == 0) run_mode=1;\n            if(run_mode){\n                if(diff){\n                    while(run_count >= 1<<log2_run[run_index]){\n                        run_count -= 1<<log2_run[run_index];\n                        run_index++;\n                        put_bits(&s->pb, 1, 1);\n                    }\n                    put_bits(&s->pb, 1 + log2_run[run_index], run_count);\n                    if(run_index) run_index--;\n                    run_count=0;\n                    run_mode=0;\n                    if(diff>0) diff--;\n                }else{\n                    run_count++;\n                }\n            }\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<<log2_run[run_index]){\n            run_count -= 1<<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}']