static int decode_frame(AVCodecContext *avctx, void *data,
                        int *got_frame, AVPacket *avpkt)
{
    AVFrame *frame = data;
    const uint8_t *buf = avpkt->data;
    int buf_size       = avpkt->size;
    EightBpsContext * const c = avctx->priv_data;
    const unsigned char *encoded = buf;
    unsigned char *pixptr, *pixptr_end;
    unsigned int height = avctx->height;
    unsigned int dlen, p, row;
    const unsigned char *lp, *dp, *ep;
    unsigned char count;
    unsigned int px_inc;
    unsigned int planes     = c->planes;
    unsigned char *planemap = c->planemap;
    int ret;
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return ret;
    }
    ep = encoded + buf_size;
    dp = encoded + planes * (height << 1);
    if (planes == 4)
        planes--;
    px_inc = planes + (avctx->pix_fmt == AV_PIX_FMT_RGB32);
    for (p = 0; p < planes; p++) {
        lp = encoded + p * (height << 1);
        for (row = 0; row < height; row++) {
            pixptr = frame->data[0] + row * frame->linesize[0] + planemap[p];
            pixptr_end = pixptr + frame->linesize[0];
            if (ep - lp < row * 2 + 2)
                return AVERROR_INVALIDDATA;
            dlen = av_be2ne16(*(const unsigned short *)(lp + row * 2));
            while (dlen > 0) {
                if (ep - dp <= 1)
                    return AVERROR_INVALIDDATA;
                if ((count = *dp++) <= 127) {
                    count++;
                    dlen -= count + 1;
                    if (pixptr_end - pixptr < count * px_inc)
                        break;
                    if (ep - dp < count)
                        return AVERROR_INVALIDDATA;
                    while (count--) {
                        *pixptr = *dp++;
                        pixptr += px_inc;
                    }
                } else {
                    count = 257 - count;
                    if (pixptr_end - pixptr < count * px_inc)
                        break;
                    while (count--) {
                        *pixptr = *dp;
                        pixptr += px_inc;
                    }
                    dp++;
                    dlen -= 2;
                }
            }
        }
    }
    if (avctx->bits_per_coded_sample <= 8) {
        const uint8_t *pal = av_packet_get_side_data(avpkt,
                                                     AV_PKT_DATA_PALETTE,
                                                     NULL);
        if (pal) {
            frame->palette_has_changed = 1;
            memcpy(c->pal, pal, AVPALETTE_SIZE);
        }
        memcpy (frame->data[1], c->pal, AVPALETTE_SIZE);
    }
    *got_frame = 1;
    return buf_size;
}
['static int decode_frame(AVCodecContext *avctx, void *data,\n                        int *got_frame, AVPacket *avpkt)\n{\n    AVFrame *frame = data;\n    const uint8_t *buf = avpkt->data;\n    int buf_size       = avpkt->size;\n    EightBpsContext * const c = avctx->priv_data;\n    const unsigned char *encoded = buf;\n    unsigned char *pixptr, *pixptr_end;\n    unsigned int height = avctx->height;\n    unsigned int dlen, p, row;\n    const unsigned char *lp, *dp, *ep;\n    unsigned char count;\n    unsigned int px_inc;\n    unsigned int planes     = c->planes;\n    unsigned char *planemap = c->planemap;\n    int ret;\n    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {\n        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n        return ret;\n    }\n    ep = encoded + buf_size;\n    dp = encoded + planes * (height << 1);\n    if (planes == 4)\n        planes--;\n    px_inc = planes + (avctx->pix_fmt == AV_PIX_FMT_RGB32);\n    for (p = 0; p < planes; p++) {\n        lp = encoded + p * (height << 1);\n        for (row = 0; row < height; row++) {\n            pixptr = frame->data[0] + row * frame->linesize[0] + planemap[p];\n            pixptr_end = pixptr + frame->linesize[0];\n            if (ep - lp < row * 2 + 2)\n                return AVERROR_INVALIDDATA;\n            dlen = av_be2ne16(*(const unsigned short *)(lp + row * 2));\n            while (dlen > 0) {\n                if (ep - dp <= 1)\n                    return AVERROR_INVALIDDATA;\n                if ((count = *dp++) <= 127) {\n                    count++;\n                    dlen -= count + 1;\n                    if (pixptr_end - pixptr < count * px_inc)\n                        break;\n                    if (ep - dp < count)\n                        return AVERROR_INVALIDDATA;\n                    while (count--) {\n                        *pixptr = *dp++;\n                        pixptr += px_inc;\n                    }\n                } else {\n                    count = 257 - count;\n                    if (pixptr_end - pixptr < count * px_inc)\n                        break;\n                    while (count--) {\n                        *pixptr = *dp;\n                        pixptr += px_inc;\n                    }\n                    dp++;\n                    dlen -= 2;\n                }\n            }\n        }\n    }\n    if (avctx->bits_per_coded_sample <= 8) {\n        const uint8_t *pal = av_packet_get_side_data(avpkt,\n                                                     AV_PKT_DATA_PALETTE,\n                                                     NULL);\n        if (pal) {\n            frame->palette_has_changed = 1;\n            memcpy(c->pal, pal, AVPALETTE_SIZE);\n        }\n        memcpy (frame->data[1], c->pal, AVPALETTE_SIZE);\n    }\n    *got_frame = 1;\n    return buf_size;\n}']