static int escape130_decode_frame(AVCodecContext *avctx, void *data,
                                  int *got_frame, AVPacket *avpkt)
{
    const uint8_t *buf  = avpkt->data;
    int buf_size        = avpkt->size;
    Escape130Context *s = avctx->priv_data;
    AVFrame *pic        = data;
    GetBitContext gb;
    int ret;
    uint8_t *old_y, *old_cb, *old_cr,
            *new_y, *new_cb, *new_cr;
    uint8_t *dstY, *dstU, *dstV;
    unsigned old_y_stride, old_cb_stride, old_cr_stride,
             new_y_stride, new_cb_stride, new_cr_stride;
    unsigned total_blocks = avctx->width * avctx->height / 4,
             block_index, block_x = 0;
    unsigned y[4] = { 0 }, cb = 0x10, cr = 0x10;
    int skip = -1, y_avg = 0, i, j;
    uint8_t *ya = s->old_y_avg;
    if (buf_size <= 16) {
        av_log(avctx, AV_LOG_ERROR, "Insufficient frame data\n");
        return AVERROR_INVALIDDATA;
    }
    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
        return ret;
    init_get_bits(&gb, buf + 16, (buf_size - 16) * 8);
    new_y  = s->new_y;
    new_cb = s->new_u;
    new_cr = s->new_v;
    new_y_stride  = s->linesize[0];
    new_cb_stride = s->linesize[1];
    new_cr_stride = s->linesize[2];
    old_y  = s->old_y;
    old_cb = s->old_u;
    old_cr = s->old_v;
    old_y_stride  = s->linesize[0];
    old_cb_stride = s->linesize[1];
    old_cr_stride = s->linesize[2];
    for (block_index = 0; block_index < total_blocks; block_index++) {
        if (skip == -1)
            skip = decode_skip_count(&gb);
        if (skip == -1) {
            av_log(avctx, AV_LOG_ERROR, "Error decoding skip value\n");
            return AVERROR_INVALIDDATA;
        }
        if (skip) {
            y[0] = old_y[0];
            y[1] = old_y[1];
            y[2] = old_y[old_y_stride];
            y[3] = old_y[old_y_stride + 1];
            y_avg = ya[0];
            cb = old_cb[0];
            cr = old_cr[0];
        } else {
            if (get_bits1(&gb)) {
                unsigned sign_selector       = get_bits(&gb, 6);
                unsigned difference_selector = get_bits(&gb, 2);
                y_avg = 2 * get_bits(&gb, 5);
                for (i = 0; i < 4; i++) {
                    y[i] = av_clip(y_avg + offset_table[difference_selector] *
                                   sign_table[sign_selector][i], 0, 63);
                }
            } else if (get_bits1(&gb)) {
                if (get_bits1(&gb)) {
                    y_avg = get_bits(&gb, 6);
                } else {
                    unsigned adjust_index = get_bits(&gb, 3);
                    y_avg = (y_avg + luma_adjust[adjust_index]) & 63;
                }
                for (i = 0; i < 4; i++)
                    y[i] = y_avg;
            }
            if (get_bits1(&gb)) {
                if (get_bits1(&gb)) {
                    cb = get_bits(&gb, 5);
                    cr = get_bits(&gb, 5);
                } else {
                    unsigned adjust_index = get_bits(&gb, 3);
                    cb = (cb + chroma_adjust[0][adjust_index]) & 31;
                    cr = (cr + chroma_adjust[1][adjust_index]) & 31;
                }
            }
        }
        *ya++ = y_avg;
        new_y[0]                = y[0];
        new_y[1]                = y[1];
        new_y[new_y_stride]     = y[2];
        new_y[new_y_stride + 1] = y[3];
        *new_cb = cb;
        *new_cr = cr;
        old_y += 2;
        old_cb++;
        old_cr++;
        new_y += 2;
        new_cb++;
        new_cr++;
        block_x++;
        if (block_x * 2 == avctx->width) {
            block_x = 0;
            old_y  += old_y_stride * 2  - avctx->width;
            old_cb += old_cb_stride     - avctx->width / 2;
            old_cr += old_cr_stride     - avctx->width / 2;
            new_y  += new_y_stride * 2  - avctx->width;
            new_cb += new_cb_stride     - avctx->width / 2;
            new_cr += new_cr_stride     - avctx->width / 2;
        }
        skip--;
    }
    new_y  = s->new_y;
    new_cb = s->new_u;
    new_cr = s->new_v;
    dstY   = pic->data[0];
    dstU   = pic->data[1];
    dstV   = pic->data[2];
    for (j = 0; j < avctx->height; j++) {
        for (i = 0; i < avctx->width; i++)
            dstY[i] = new_y[i] << 2;
        dstY  += pic->linesize[0];
        new_y += new_y_stride;
    }
    for (j = 0; j < avctx->height / 2; j++) {
        for (i = 0; i < avctx->width / 2; i++) {
            dstU[i] = chroma_vals[new_cb[i]];
            dstV[i] = chroma_vals[new_cr[i]];
        }
        dstU   += pic->linesize[1];
        dstV   += pic->linesize[2];
        new_cb += new_cb_stride;
        new_cr += new_cr_stride;
    }
    av_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\n",
            buf_size, get_bits_count(&gb) >> 3);
    FFSWAP(uint8_t*, s->old_y, s->new_y);
    FFSWAP(uint8_t*, s->old_u, s->new_u);
    FFSWAP(uint8_t*, s->old_v, s->new_v);
    *got_frame = 1;
    return buf_size;
}
['static int escape130_decode_frame(AVCodecContext *avctx, void *data,\n                                  int *got_frame, AVPacket *avpkt)\n{\n    const uint8_t *buf  = avpkt->data;\n    int buf_size        = avpkt->size;\n    Escape130Context *s = avctx->priv_data;\n    AVFrame *pic        = data;\n    GetBitContext gb;\n    int ret;\n    uint8_t *old_y, *old_cb, *old_cr,\n            *new_y, *new_cb, *new_cr;\n    uint8_t *dstY, *dstU, *dstV;\n    unsigned old_y_stride, old_cb_stride, old_cr_stride,\n             new_y_stride, new_cb_stride, new_cr_stride;\n    unsigned total_blocks = avctx->width * avctx->height / 4,\n             block_index, block_x = 0;\n    unsigned y[4] = { 0 }, cb = 0x10, cr = 0x10;\n    int skip = -1, y_avg = 0, i, j;\n    uint8_t *ya = s->old_y_avg;\n    if (buf_size <= 16) {\n        av_log(avctx, AV_LOG_ERROR, "Insufficient frame data\\n");\n        return AVERROR_INVALIDDATA;\n    }\n    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)\n        return ret;\n    init_get_bits(&gb, buf + 16, (buf_size - 16) * 8);\n    new_y  = s->new_y;\n    new_cb = s->new_u;\n    new_cr = s->new_v;\n    new_y_stride  = s->linesize[0];\n    new_cb_stride = s->linesize[1];\n    new_cr_stride = s->linesize[2];\n    old_y  = s->old_y;\n    old_cb = s->old_u;\n    old_cr = s->old_v;\n    old_y_stride  = s->linesize[0];\n    old_cb_stride = s->linesize[1];\n    old_cr_stride = s->linesize[2];\n    for (block_index = 0; block_index < total_blocks; block_index++) {\n        if (skip == -1)\n            skip = decode_skip_count(&gb);\n        if (skip == -1) {\n            av_log(avctx, AV_LOG_ERROR, "Error decoding skip value\\n");\n            return AVERROR_INVALIDDATA;\n        }\n        if (skip) {\n            y[0] = old_y[0];\n            y[1] = old_y[1];\n            y[2] = old_y[old_y_stride];\n            y[3] = old_y[old_y_stride + 1];\n            y_avg = ya[0];\n            cb = old_cb[0];\n            cr = old_cr[0];\n        } else {\n            if (get_bits1(&gb)) {\n                unsigned sign_selector       = get_bits(&gb, 6);\n                unsigned difference_selector = get_bits(&gb, 2);\n                y_avg = 2 * get_bits(&gb, 5);\n                for (i = 0; i < 4; i++) {\n                    y[i] = av_clip(y_avg + offset_table[difference_selector] *\n                                   sign_table[sign_selector][i], 0, 63);\n                }\n            } else if (get_bits1(&gb)) {\n                if (get_bits1(&gb)) {\n                    y_avg = get_bits(&gb, 6);\n                } else {\n                    unsigned adjust_index = get_bits(&gb, 3);\n                    y_avg = (y_avg + luma_adjust[adjust_index]) & 63;\n                }\n                for (i = 0; i < 4; i++)\n                    y[i] = y_avg;\n            }\n            if (get_bits1(&gb)) {\n                if (get_bits1(&gb)) {\n                    cb = get_bits(&gb, 5);\n                    cr = get_bits(&gb, 5);\n                } else {\n                    unsigned adjust_index = get_bits(&gb, 3);\n                    cb = (cb + chroma_adjust[0][adjust_index]) & 31;\n                    cr = (cr + chroma_adjust[1][adjust_index]) & 31;\n                }\n            }\n        }\n        *ya++ = y_avg;\n        new_y[0]                = y[0];\n        new_y[1]                = y[1];\n        new_y[new_y_stride]     = y[2];\n        new_y[new_y_stride + 1] = y[3];\n        *new_cb = cb;\n        *new_cr = cr;\n        old_y += 2;\n        old_cb++;\n        old_cr++;\n        new_y += 2;\n        new_cb++;\n        new_cr++;\n        block_x++;\n        if (block_x * 2 == avctx->width) {\n            block_x = 0;\n            old_y  += old_y_stride * 2  - avctx->width;\n            old_cb += old_cb_stride     - avctx->width / 2;\n            old_cr += old_cr_stride     - avctx->width / 2;\n            new_y  += new_y_stride * 2  - avctx->width;\n            new_cb += new_cb_stride     - avctx->width / 2;\n            new_cr += new_cr_stride     - avctx->width / 2;\n        }\n        skip--;\n    }\n    new_y  = s->new_y;\n    new_cb = s->new_u;\n    new_cr = s->new_v;\n    dstY   = pic->data[0];\n    dstU   = pic->data[1];\n    dstV   = pic->data[2];\n    for (j = 0; j < avctx->height; j++) {\n        for (i = 0; i < avctx->width; i++)\n            dstY[i] = new_y[i] << 2;\n        dstY  += pic->linesize[0];\n        new_y += new_y_stride;\n    }\n    for (j = 0; j < avctx->height / 2; j++) {\n        for (i = 0; i < avctx->width / 2; i++) {\n            dstU[i] = chroma_vals[new_cb[i]];\n            dstV[i] = chroma_vals[new_cr[i]];\n        }\n        dstU   += pic->linesize[1];\n        dstV   += pic->linesize[2];\n        new_cb += new_cb_stride;\n        new_cr += new_cr_stride;\n    }\n    av_dlog(avctx, "Frame data: provided %d bytes, used %d bytes\\n",\n            buf_size, get_bits_count(&gb) >> 3);\n    FFSWAP(uint8_t*, s->old_y, s->new_y);\n    FFSWAP(uint8_t*, s->old_u, s->new_u);\n    FFSWAP(uint8_t*, s->old_v, s->new_v);\n    *got_frame = 1;\n    return buf_size;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n                                int bit_size)\n{\n    int buffer_size;\n    int ret = 0;\n    if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n        bit_size    = 0;\n        buffer      = NULL;\n        ret         = AVERROR_INVALIDDATA;\n    }\n    buffer_size = (bit_size + 7) >> 3;\n    s->buffer             = buffer;\n    s->size_in_bits       = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n    s->size_in_bits_plus8 = bit_size + 8;\n#endif\n    s->buffer_end         = buffer + buffer_size;\n    s->index              = 0;\n    return ret;\n}', 'static int decode_skip_count(GetBitContext* gb)\n{\n    int value;\n    value = get_bits1(gb);\n    if (value)\n        return 0;\n    value = get_bits(gb, 3);\n    if (value)\n        return value;\n    value = get_bits(gb, 8);\n    if (value)\n        return value + 7;\n    value = get_bits(gb, 15);\n    if (value)\n        return value + 262;\n    return -1;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n    unsigned int index = s->index;\n    uint8_t result     = s->buffer[index >> 3];\n#ifdef BITSTREAM_READER_LE\n    result >>= index & 7;\n    result  &= 1;\n#else\n    result <<= index & 7;\n    result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n    if (s->index < s->size_in_bits_plus8)\n#endif\n        index++;\n    s->index = index;\n    return result;\n}']