static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,

                                  int width, int height, int stride,

                                  const uint8_t *src, int src_size)

{

    int i = 0;

    int read = 0;

    uint32_t length;

    uint32_t offset = 1;

    int esc_count;

    GetBitContext gb;

    lag_rac rac;

    const uint8_t *src_end = src + src_size;



    rac.avctx = l->avctx;

    l->zeros = 0;



    if(src_size < 2)

        return AVERROR_INVALIDDATA;



    esc_count = src[0];

    if (esc_count < 4) {

        length = width * height;

        if(src_size < 5)

            return AVERROR_INVALIDDATA;

        if (esc_count && AV_RL32(src + 1) < length) {

            length = AV_RL32(src + 1);

            offset += 4;

        }



        init_get_bits8(&gb, src + offset, src_size - offset);



        if (lag_read_prob_header(&rac, &gb) < 0)

            return -1;



        ff_lag_rac_init(&rac, &gb, length - stride);



        for (i = 0; i < height; i++)

            read += lag_decode_line(l, &rac, dst + (i * stride), width,

                                    stride, esc_count);



        if (read > length)

            av_log(l->avctx, AV_LOG_WARNING,

                   "Output more bytes than length (%d of %d)\n", read,

                   length);

    } else if (esc_count < 8) {

        esc_count -= 4;

        src ++;

        src_size --;

        if (esc_count > 0) {

            /* Zero run coding only, no range coding. */

            for (i = 0; i < height; i++) {

                int res = lag_decode_zero_run_line(l, dst + (i * stride), src,

                                                   src_end, width, esc_count);

                if (res < 0)

                    return res;

                src += res;

            }

        } else {

            if (src_size < width * height)

                return AVERROR_INVALIDDATA; // buffer not big enough

            /* Plane is stored uncompressed */

            for (i = 0; i < height; i++) {

                memcpy(dst + (i * stride), src, width);

                src += width;

            }

        }

    } else if (esc_count == 0xff) {

        /* Plane is a solid run of given value */

        for (i = 0; i < height; i++)

            memset(dst + i * stride, src[1], width);

        /* Do not apply prediction.

           Note: memset to 0 above, setting first value to src[1]

           and applying prediction gives the same result. */

        return 0;

    } else {

        av_log(l->avctx, AV_LOG_ERROR,

               "Invalid zero run escape code! (%#x)\n", esc_count);

        return -1;

    }



    if (l->avctx->pix_fmt != AV_PIX_FMT_YUV422P) {

        for (i = 0; i < height; i++) {

            lag_pred_line(l, dst, width, stride, i);

            dst += stride;

        }

    } else {

        for (i = 0; i < height; i++) {

            lag_pred_line_yuy2(l, dst, width, stride, i,

                               width == l->avctx->width);

            dst += stride;

        }

    }



    return 0;

}

static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,

                                  int width, int height, int stride,

                                  const uint8_t *src, int src_size)

{

    int i = 0;

    int read = 0;

    uint32_t length;

    uint32_t offset = 1;

    int esc_count;

    GetBitContext gb;

    lag_rac rac;

    const uint8_t *src_end = src + src_size;



    rac.avctx = l->avctx;

    l->zeros = 0;



    if(src_size < 2)

        return AVERROR_INVALIDDATA;



    esc_count = src[0];

    if (esc_count < 4) {

        length = width * height;

        if(src_size < 5)

            return AVERROR_INVALIDDATA;

        if (esc_count && AV_RL32(src + 1) < length) {

            length = AV_RL32(src + 1);

            offset += 4;

        }



        init_get_bits8(&gb, src + offset, src_size - offset);



        if (lag_read_prob_header(&rac, &gb) < 0)

            return -1;



        ff_lag_rac_init(&rac, &gb, length - stride);



        for (i = 0; i < height; i++)

            read += lag_decode_line(l, &rac, dst + (i * stride), width,

                                    stride, esc_count);



        if (read > length)

            av_log(l->avctx, AV_LOG_WARNING,

                   "Output more bytes than length (%d of %d)\n", read,

                   length);

    } else if (esc_count < 8) {

        esc_count -= 4;

        src ++;

        src_size --;

        if (esc_count > 0) {

            

            for (i = 0; i < height; i++) {

                int res = lag_decode_zero_run_line(l, dst + (i * stride), src,

                                                   src_end, width, esc_count);

                if (res < 0)

                    return res;

                src += res;

            }

        } else {

            if (src_size < width * height)

                return AVERROR_INVALIDDATA; 

            

            for (i = 0; i < height; i++) {

                memcpy(dst + (i * stride), src, width);

                src += width;

            }

        }

    } else if (esc_count == 0xff) {

        

        for (i = 0; i < height; i++)

            memset(dst + i * stride, src[1], width);

        





        return 0;

    } else {

        av_log(l->avctx, AV_LOG_ERROR,

               "Invalid zero run escape code! (%#x)\n", esc_count);

        return -1;

    }



    if (l->avctx->pix_fmt != AV_PIX_FMT_YUV422P) {

        for (i = 0; i < height; i++) {

            lag_pred_line(l, dst, width, stride, i);

            dst += stride;

        }

    } else {

        for (i = 0; i < height; i++) {

            lag_pred_line_yuy2(l, dst, width, stride, i,

                               width == l->avctx->width);

            dst += stride;

        }

    }



    return 0;

}

static int FUNC_0(LagarithContext *VAR_0, uint8_t *VAR_1,

                                  int VAR_2, int VAR_3, int VAR_4,

                                  const uint8_t *VAR_5, int VAR_6)

{

    int VAR_7 = 0;

    int VAR_8 = 0;

    uint32_t length;

    uint32_t offset = 1;

    int VAR_9;

    GetBitContext gb;

    lag_rac rac;

    const uint8_t *VAR_10 = VAR_5 + VAR_6;



    rac.avctx = VAR_0->avctx;

    VAR_0->zeros = 0;



    if(VAR_6 < 2)

        return AVERROR_INVALIDDATA;



    VAR_9 = VAR_5[0];

    if (VAR_9 < 4) {

        length = VAR_2 * VAR_3;

        if(VAR_6 < 5)

            return AVERROR_INVALIDDATA;

        if (VAR_9 && AV_RL32(VAR_5 + 1) < length) {

            length = AV_RL32(VAR_5 + 1);

            offset += 4;

        }



        init_get_bits8(&gb, VAR_5 + offset, VAR_6 - offset);



        if (lag_read_prob_header(&rac, &gb) < 0)

            return -1;



        ff_lag_rac_init(&rac, &gb, length - VAR_4);



        for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++)

            VAR_8 += lag_decode_line(VAR_0, &rac, VAR_1 + (VAR_7 * VAR_4), VAR_2,

                                    VAR_4, VAR_9);



        if (VAR_8 > length)

            av_log(VAR_0->avctx, AV_LOG_WARNING,

                   "Output more bytes than length (%d of %d)\n", VAR_8,

                   length);

    } else if (VAR_9 < 8) {

        VAR_9 -= 4;

        VAR_5 ++;

        VAR_6 --;

        if (VAR_9 > 0) {

            

            for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) {

                int VAR_11 = lag_decode_zero_run_line(VAR_0, VAR_1 + (VAR_7 * VAR_4), VAR_5,

                                                   VAR_10, VAR_2, VAR_9);

                if (VAR_11 < 0)

                    return VAR_11;

                VAR_5 += VAR_11;

            }

        } else {

            if (VAR_6 < VAR_2 * VAR_3)

                return AVERROR_INVALIDDATA; 

            

            for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) {

                memcpy(VAR_1 + (VAR_7 * VAR_4), VAR_5, VAR_2);

                VAR_5 += VAR_2;

            }

        }

    } else if (VAR_9 == 0xff) {

        

        for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++)

            memset(VAR_1 + VAR_7 * VAR_4, VAR_5[1], VAR_2);

        





        return 0;

    } else {

        av_log(VAR_0->avctx, AV_LOG_ERROR,

               "Invalid zero run escape code! (%#x)\n", VAR_9);

        return -1;

    }



    if (VAR_0->avctx->pix_fmt != AV_PIX_FMT_YUV422P) {

        for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) {

            lag_pred_line(VAR_0, VAR_1, VAR_2, VAR_4, VAR_7);

            VAR_1 += VAR_4;

        }

    } else {

        for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) {

            lag_pred_line_yuy2(VAR_0, VAR_1, VAR_2, VAR_4, VAR_7,

                               VAR_2 == VAR_0->avctx->VAR_2);

            VAR_1 += VAR_4;

        }

    }



    return 0;

}

static int FUNC_0(LagarithContext *VAR_0, uint8_t *VAR_1,
int VAR_2, int VAR_3, int VAR_4,
const uint8_t *VAR_5, int VAR_6)
{ int VAR_7 = 0; int VAR_8 = 0; uint32_t length; uint32_t offset = 1; int VAR_9; GetBitContext gb; lag_rac rac; const uint8_t *VAR_10 = VAR_5 + VAR_6; rac.avctx = VAR_0->avctx; VAR_0->zeros = 0; if(VAR_6 < 2)
return AVERROR_INVALIDDATA; VAR_9 = VAR_5[0]; if (VAR_9 < 4) { length = VAR_2 * VAR_3; if(VAR_6 < 5)
return AVERROR_INVALIDDATA; if (VAR_9 && AV_RL32(VAR_5 + 1) < length) { length = AV_RL32(VAR_5 + 1); offset += 4; } init_get_bits8(&gb, VAR_5 + offset, VAR_6 - offset); if (lag_read_prob_header(&rac, &gb) < 0)
return -1; ff_lag_rac_init(&rac, &gb, length - VAR_4); for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) VAR_8 += lag_decode_line(VAR_0, &rac, VAR_1 + (VAR_7 * VAR_4), VAR_2,
VAR_4, VAR_9); if (VAR_8 > length)
av_log(VAR_0->avctx, AV_LOG_WARNING,
"Output more bytes than length (%d of %d)\n", VAR_8,
length); } else if (VAR_9 < 8) { VAR_9 -= 4; VAR_5 ++; VAR_6 --; if (VAR_9 > 0) { for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) { int VAR_11 = lag_decode_zero_run_line(VAR_0, VAR_1 + (VAR_7 * VAR_4), VAR_5,
VAR_10, VAR_2, VAR_9); if (VAR_11 < 0)
return VAR_11; VAR_5 += VAR_11; } } else { if (VAR_6 < VAR_2 * VAR_3)
return AVERROR_INVALIDDATA; for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) { memcpy(VAR_1 + (VAR_7 * VAR_4), VAR_5, VAR_2); VAR_5 += VAR_2; } } } else if (VAR_9 == 0xff) { for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) memset(VAR_1 + VAR_7 * VAR_4, VAR_5[1], VAR_2); return 0; } else { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Invalid zero run escape code! (%#x)\n", VAR_9); return -1; } if (VAR_0->avctx->pix_fmt != AV_PIX_FMT_YUV422P) { for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) { lag_pred_line(VAR_0, VAR_1, VAR_2, VAR_4, VAR_7); VAR_1 += VAR_4; } } else { for (VAR_7 = 0; VAR_7 < VAR_3; VAR_7++) { lag_pred_line_yuy2(VAR_0, VAR_1, VAR_2, VAR_4, VAR_7,
VAR_2 == VAR_0->avctx->VAR_2); VAR_1 += VAR_4; } } return 0; }