static int decode_cell(Indeo3DecodeContext *ctx, AVCodecContext *avctx,

                       Plane *plane, Cell *cell, const uint8_t *data_ptr,

                       const uint8_t *last_ptr)

{

    int           x, mv_x, mv_y, mode, vq_index, prim_indx, second_indx;

    int           zoom_fac;

    int           offset, error = 0, swap_quads[2];

    uint8_t       code, *block, *ref_block = 0;

    const vqEntry *delta[2];

    const uint8_t *data_start = data_ptr;



    /* get coding mode and VQ table index from the VQ descriptor byte */

    code     = *data_ptr++;

    mode     = code >> 4;

    vq_index = code & 0xF;



    /* setup output and reference pointers */

    offset = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);

    block  =  plane->pixels[ctx->buf_sel] + offset;

    if (!cell->mv_ptr) {

        /* use previous line as reference for INTRA cells */

        ref_block = block - plane->pitch;

    } else if (mode >= 10) {

        /* for mode 10 and 11 INTER first copy the predicted cell into the current one */

        /* so we don't need to do data copying for each RLE code later */

        copy_cell(ctx, plane, cell);

    } else {

        /* set the pointer to the reference pixels for modes 0-4 INTER */

        mv_y      = cell->mv_ptr[0];

        mv_x      = cell->mv_ptr[1];

        if (   mv_x + 4*cell->xpos < 0

            || mv_y + 4*cell->ypos < 0

            || mv_x + 4*cell->xpos + 4*cell->width  > plane->width

            || mv_y + 4*cell->ypos + 4*cell->height > plane->height) {

            av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", mv_x + 4*cell->xpos, mv_y + 4*cell->ypos);

            return AVERROR_INVALIDDATA;

        }

        offset   += mv_y * plane->pitch + mv_x;

        ref_block = plane->pixels[ctx->buf_sel ^ 1] + offset;

    }



    /* select VQ tables as follows: */

    /* modes 0 and 3 use only the primary table for all lines in a block */

    /* while modes 1 and 4 switch between primary and secondary tables on alternate lines */

    if (mode == 1 || mode == 4) {

        code        = ctx->alt_quant[vq_index];

        prim_indx   = (code >> 4)  + ctx->cb_offset;

        second_indx = (code & 0xF) + ctx->cb_offset;

    } else {

        vq_index += ctx->cb_offset;

        prim_indx = second_indx = vq_index;

    }



    if (prim_indx >= 24 || second_indx >= 24) {

        av_log(avctx, AV_LOG_ERROR, "Invalid VQ table indexes! Primary: %d, secondary: %d!\n",

               prim_indx, second_indx);

        return AVERROR_INVALIDDATA;

    }



    delta[0] = &vq_tab[second_indx];

    delta[1] = &vq_tab[prim_indx];

    swap_quads[0] = second_indx >= 16;

    swap_quads[1] = prim_indx   >= 16;



    /* requantize the prediction if VQ index of this cell differs from VQ index */

    /* of the predicted cell in order to avoid overflows. */

    if (vq_index >= 8 && ref_block) {

        for (x = 0; x < cell->width << 2; x++)

            ref_block[x] = requant_tab[vq_index & 7][ref_block[x] & 127];

    }



    error = IV3_NOERR;



    switch (mode) {

    case 0: /*------------------ MODES 0 & 1 (4x4 block processing) --------------------*/

    case 1:

    case 3: /*------------------ MODES 3 & 4 (4x8 block processing) --------------------*/

    case 4:

        if (mode >= 3 && cell->mv_ptr) {

            av_log(avctx, AV_LOG_ERROR, "Attempt to apply Mode 3/4 to an INTER cell!\n");

            return AVERROR_INVALIDDATA;

        }



        zoom_fac = mode >= 3;

        error = decode_cell_data(cell, block, ref_block, plane->pitch, 0, zoom_fac,

                                 mode, delta, swap_quads, &data_ptr, last_ptr);

        break;

    case 10: /*-------------------- MODE 10 (8x8 block processing) ---------------------*/

    case 11: /*----------------- MODE 11 (4x8 INTER block processing) ------------------*/

        if (mode == 10 && !cell->mv_ptr) { /* MODE 10 INTRA processing */

            error = decode_cell_data(cell, block, ref_block, plane->pitch, 1, 1,

                                     mode, delta, swap_quads, &data_ptr, last_ptr);

        } else { /* mode 10 and 11 INTER processing */

            if (mode == 11 && !cell->mv_ptr) {

               av_log(avctx, AV_LOG_ERROR, "Attempt to use Mode 11 for an INTRA cell!\n");

               return AVERROR_INVALIDDATA;

            }



            zoom_fac = mode == 10;

            error = decode_cell_data(cell, block, ref_block, plane->pitch,

                                     zoom_fac, 1, mode, delta, swap_quads,

                                     &data_ptr, last_ptr);

        }

        break;

    default:

        av_log(avctx, AV_LOG_ERROR, "Unsupported coding mode: %d\n", mode);

        return AVERROR_INVALIDDATA;

    }//switch mode



    switch (error) {

    case IV3_BAD_RLE:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE code %X is not allowed at the current line\n",

               mode, data_ptr[-1]);

        return AVERROR_INVALIDDATA;

    case IV3_BAD_DATA:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: invalid VQ data\n", mode);

        return AVERROR_INVALIDDATA;

    case IV3_BAD_COUNTER:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE-FB invalid counter: %d\n", mode, code);

        return AVERROR_INVALIDDATA;

    case IV3_UNSUPPORTED:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);

        return AVERROR_INVALIDDATA;

    case IV3_OUT_OF_DATA:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: attempt to read past end of buffer\n", mode);

        return AVERROR_INVALIDDATA;

    }



    return data_ptr - data_start; /* report number of bytes consumed from the input buffer */

}

static int decode_cell(Indeo3DecodeContext *ctx, AVCodecContext *avctx,

                       Plane *plane, Cell *cell, const uint8_t *data_ptr,

                       const uint8_t *last_ptr)

{

    int           x, mv_x, mv_y, mode, vq_index, prim_indx, second_indx;

    int           zoom_fac;

    int           offset, error = 0, swap_quads[2];

    uint8_t       code, *block, *ref_block = 0;

    const vqEntry *delta[2];

    const uint8_t *data_start = data_ptr;



    

    code     = *data_ptr++;

    mode     = code >> 4;

    vq_index = code & 0xF;



    

    offset = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);

    block  =  plane->pixels[ctx->buf_sel] + offset;

    if (!cell->mv_ptr) {

        

        ref_block = block - plane->pitch;

    } else if (mode >= 10) {

        

        

        copy_cell(ctx, plane, cell);

    } else {

        

        mv_y      = cell->mv_ptr[0];

        mv_x      = cell->mv_ptr[1];

        if (   mv_x + 4*cell->xpos < 0

            || mv_y + 4*cell->ypos < 0

            || mv_x + 4*cell->xpos + 4*cell->width  > plane->width

            || mv_y + 4*cell->ypos + 4*cell->height > plane->height) {

            av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", mv_x + 4*cell->xpos, mv_y + 4*cell->ypos);

            return AVERROR_INVALIDDATA;

        }

        offset   += mv_y * plane->pitch + mv_x;

        ref_block = plane->pixels[ctx->buf_sel ^ 1] + offset;

    }



    

    

    

    if (mode == 1 || mode == 4) {

        code        = ctx->alt_quant[vq_index];

        prim_indx   = (code >> 4)  + ctx->cb_offset;

        second_indx = (code & 0xF) + ctx->cb_offset;

    } else {

        vq_index += ctx->cb_offset;

        prim_indx = second_indx = vq_index;

    }



    if (prim_indx >= 24 || second_indx >= 24) {

        av_log(avctx, AV_LOG_ERROR, "Invalid VQ table indexes! Primary: %d, secondary: %d!\n",

               prim_indx, second_indx);

        return AVERROR_INVALIDDATA;

    }



    delta[0] = &vq_tab[second_indx];

    delta[1] = &vq_tab[prim_indx];

    swap_quads[0] = second_indx >= 16;

    swap_quads[1] = prim_indx   >= 16;



    

    

    if (vq_index >= 8 && ref_block) {

        for (x = 0; x < cell->width << 2; x++)

            ref_block[x] = requant_tab[vq_index & 7][ref_block[x] & 127];

    }



    error = IV3_NOERR;



    switch (mode) {

    case 0: 

    case 1:

    case 3: 

    case 4:

        if (mode >= 3 && cell->mv_ptr) {

            av_log(avctx, AV_LOG_ERROR, "Attempt to apply Mode 3/4 to an INTER cell!\n");

            return AVERROR_INVALIDDATA;

        }



        zoom_fac = mode >= 3;

        error = decode_cell_data(cell, block, ref_block, plane->pitch, 0, zoom_fac,

                                 mode, delta, swap_quads, &data_ptr, last_ptr);

        break;

    case 10: 

    case 11: 

        if (mode == 10 && !cell->mv_ptr) { 

            error = decode_cell_data(cell, block, ref_block, plane->pitch, 1, 1,

                                     mode, delta, swap_quads, &data_ptr, last_ptr);

        } else { 

            if (mode == 11 && !cell->mv_ptr) {

               av_log(avctx, AV_LOG_ERROR, "Attempt to use Mode 11 for an INTRA cell!\n");

               return AVERROR_INVALIDDATA;

            }



            zoom_fac = mode == 10;

            error = decode_cell_data(cell, block, ref_block, plane->pitch,

                                     zoom_fac, 1, mode, delta, swap_quads,

                                     &data_ptr, last_ptr);

        }

        break;

    default:

        av_log(avctx, AV_LOG_ERROR, "Unsupported coding mode: %d\n", mode);

        return AVERROR_INVALIDDATA;

    }



    switch (error) {

    case IV3_BAD_RLE:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE code %X is not allowed at the current line\n",

               mode, data_ptr[-1]);

        return AVERROR_INVALIDDATA;

    case IV3_BAD_DATA:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: invalid VQ data\n", mode);

        return AVERROR_INVALIDDATA;

    case IV3_BAD_COUNTER:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: RLE-FB invalid counter: %d\n", mode, code);

        return AVERROR_INVALIDDATA;

    case IV3_UNSUPPORTED:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: unsupported RLE code: %X\n", mode, data_ptr[-1]);

        return AVERROR_INVALIDDATA;

    case IV3_OUT_OF_DATA:

        av_log(avctx, AV_LOG_ERROR, "Mode %d: attempt to read past end of buffer\n", mode);

        return AVERROR_INVALIDDATA;

    }



    return data_ptr - data_start; 

}

static int FUNC_0(Indeo3DecodeContext *VAR_0, AVCodecContext *VAR_1,

                       Plane *VAR_2, Cell *VAR_3, const uint8_t *VAR_4,

                       const uint8_t *VAR_5)

{

    int           VAR_6, VAR_7, VAR_8, VAR_9, VAR_10, VAR_11, VAR_12;

    int           VAR_13;

    int           VAR_14, VAR_15 = 0, VAR_16[2];

    uint8_t       code, *block, *ref_block = 0;

    const vqEntry *VAR_17[2];

    const uint8_t *VAR_18 = VAR_4;



    

    code     = *VAR_4++;

    VAR_9     = code >> 4;

    VAR_10 = code & 0xF;



    

    VAR_14 = (VAR_3->ypos << 2) * VAR_2->pitch + (VAR_3->xpos << 2);

    block  =  VAR_2->pixels[VAR_0->buf_sel] + VAR_14;

    if (!VAR_3->mv_ptr) {

        

        ref_block = block - VAR_2->pitch;

    } else if (VAR_9 >= 10) {

        

        

        copy_cell(VAR_0, VAR_2, VAR_3);

    } else {

        

        VAR_8      = VAR_3->mv_ptr[0];

        VAR_7      = VAR_3->mv_ptr[1];

        if (   VAR_7 + 4*VAR_3->xpos < 0

            || VAR_8 + 4*VAR_3->ypos < 0

            || VAR_7 + 4*VAR_3->xpos + 4*VAR_3->width  > VAR_2->width

            || VAR_8 + 4*VAR_3->ypos + 4*VAR_3->height > VAR_2->height) {

            av_log(VAR_1, AV_LOG_ERROR, "motion vector %d %d outside reference\n", VAR_7 + 4*VAR_3->xpos, VAR_8 + 4*VAR_3->ypos);

            return AVERROR_INVALIDDATA;

        }

        VAR_14   += VAR_8 * VAR_2->pitch + VAR_7;

        ref_block = VAR_2->pixels[VAR_0->buf_sel ^ 1] + VAR_14;

    }



    

    

    

    if (VAR_9 == 1 || VAR_9 == 4) {

        code        = VAR_0->alt_quant[VAR_10];

        VAR_11   = (code >> 4)  + VAR_0->cb_offset;

        VAR_12 = (code & 0xF) + VAR_0->cb_offset;

    } else {

        VAR_10 += VAR_0->cb_offset;

        VAR_11 = VAR_12 = VAR_10;

    }



    if (VAR_11 >= 24 || VAR_12 >= 24) {

        av_log(VAR_1, AV_LOG_ERROR, "Invalid VQ table indexes! Primary: %d, secondary: %d!\n",

               VAR_11, VAR_12);

        return AVERROR_INVALIDDATA;

    }



    VAR_17[0] = &vq_tab[VAR_12];

    VAR_17[1] = &vq_tab[VAR_11];

    VAR_16[0] = VAR_12 >= 16;

    VAR_16[1] = VAR_11   >= 16;



    

    

    if (VAR_10 >= 8 && ref_block) {

        for (VAR_6 = 0; VAR_6 < VAR_3->width << 2; VAR_6++)

            ref_block[VAR_6] = requant_tab[VAR_10 & 7][ref_block[VAR_6] & 127];

    }



    VAR_15 = IV3_NOERR;



    switch (VAR_9) {

    case 0: 

    case 1:

    case 3: 

    case 4:

        if (VAR_9 >= 3 && VAR_3->mv_ptr) {

            av_log(VAR_1, AV_LOG_ERROR, "Attempt to apply Mode 3/4 to an INTER VAR_3!\n");

            return AVERROR_INVALIDDATA;

        }



        VAR_13 = VAR_9 >= 3;

        VAR_15 = decode_cell_data(VAR_3, block, ref_block, VAR_2->pitch, 0, VAR_13,

                                 VAR_9, VAR_17, VAR_16, &VAR_4, VAR_5);

        break;

    case 10: 

    case 11: 

        if (VAR_9 == 10 && !VAR_3->mv_ptr) { 

            VAR_15 = decode_cell_data(VAR_3, block, ref_block, VAR_2->pitch, 1, 1,

                                     VAR_9, VAR_17, VAR_16, &VAR_4, VAR_5);

        } else { 

            if (VAR_9 == 11 && !VAR_3->mv_ptr) {

               av_log(VAR_1, AV_LOG_ERROR, "Attempt to use Mode 11 for an INTRA VAR_3!\n");

               return AVERROR_INVALIDDATA;

            }



            VAR_13 = VAR_9 == 10;

            VAR_15 = decode_cell_data(VAR_3, block, ref_block, VAR_2->pitch,

                                     VAR_13, 1, VAR_9, VAR_17, VAR_16,

                                     &VAR_4, VAR_5);

        }

        break;

    default:

        av_log(VAR_1, AV_LOG_ERROR, "Unsupported coding VAR_9: %d\n", VAR_9);

        return AVERROR_INVALIDDATA;

    }



    switch (VAR_15) {

    case IV3_BAD_RLE:

        av_log(VAR_1, AV_LOG_ERROR, "Mode %d: RLE code %X is not allowed at the current line\n",

               VAR_9, VAR_4[-1]);

        return AVERROR_INVALIDDATA;

    case IV3_BAD_DATA:

        av_log(VAR_1, AV_LOG_ERROR, "Mode %d: invalid VQ data\n", VAR_9);

        return AVERROR_INVALIDDATA;

    case IV3_BAD_COUNTER:

        av_log(VAR_1, AV_LOG_ERROR, "Mode %d: RLE-FB invalid counter: %d\n", VAR_9, code);

        return AVERROR_INVALIDDATA;

    case IV3_UNSUPPORTED:

        av_log(VAR_1, AV_LOG_ERROR, "Mode %d: unsupported RLE code: %X\n", VAR_9, VAR_4[-1]);

        return AVERROR_INVALIDDATA;

    case IV3_OUT_OF_DATA:

        av_log(VAR_1, AV_LOG_ERROR, "Mode %d: attempt to read past end of buffer\n", VAR_9);

        return AVERROR_INVALIDDATA;

    }



    return VAR_4 - VAR_18; 

}

static int FUNC_0(Indeo3DecodeContext *VAR_0, AVCodecContext *VAR_1,
Plane *VAR_2, Cell *VAR_3, const uint8_t *VAR_4,
const uint8_t *VAR_5)
{ int           VAR_6, VAR_7, VAR_8, VAR_9, VAR_10, VAR_11, VAR_12; int           VAR_13; int           VAR_14, VAR_15 = 0, VAR_16[2]; uint8_t       code, *block, *ref_block = 0; const vqEntry *VAR_17[2]; const uint8_t *VAR_18 = VAR_4; code     = *VAR_4++; VAR_9     = code >> 4; VAR_10 = code & 0xF; VAR_14 = (VAR_3->ypos << 2) * VAR_2->pitch + (VAR_3->xpos << 2); block  =  VAR_2->pixels[VAR_0->buf_sel] + VAR_14; if (!VAR_3->mv_ptr) { ref_block = block - VAR_2->pitch; } else if (VAR_9 >= 10) { copy_cell(VAR_0, VAR_2, VAR_3); } else { VAR_8      = VAR_3->mv_ptr[0]; VAR_7      = VAR_3->mv_ptr[1]; if (   VAR_7 + 4*VAR_3->xpos < 0
|| VAR_8 + 4*VAR_3->ypos < 0
|| VAR_7 + 4*VAR_3->xpos + 4*VAR_3->width  > VAR_2->width
|| VAR_8 + 4*VAR_3->ypos + 4*VAR_3->height > VAR_2->height) { av_log(VAR_1, AV_LOG_ERROR, "motion vector %d %d outside reference\n", VAR_7 + 4*VAR_3->xpos, VAR_8 + 4*VAR_3->ypos); return AVERROR_INVALIDDATA; } VAR_14   += VAR_8 * VAR_2->pitch + VAR_7; ref_block = VAR_2->pixels[VAR_0->buf_sel ^ 1] + VAR_14; } if (VAR_9 == 1 || VAR_9 == 4) { code        = VAR_0->alt_quant[VAR_10]; VAR_11   = (code >> 4)  + VAR_0->cb_offset; VAR_12 = (code & 0xF) + VAR_0->cb_offset; } else { VAR_10 += VAR_0->cb_offset; VAR_11 = VAR_12 = VAR_10; } if (VAR_11 >= 24 || VAR_12 >= 24) { av_log(VAR_1, AV_LOG_ERROR, "Invalid VQ table indexes! Primary: %d, secondary: %d!\n",
VAR_11, VAR_12); return AVERROR_INVALIDDATA; } VAR_17[0] = &vq_tab[VAR_12]; VAR_17[1] = &vq_tab[VAR_11]; VAR_16[0] = VAR_12 >= 16; VAR_16[1] = VAR_11   >= 16; if (VAR_10 >= 8 && ref_block) { for (VAR_6 = 0; VAR_6 < VAR_3->width << 2; VAR_6++) ref_block[VAR_6] = requant_tab[VAR_10 & 7][ref_block[VAR_6] & 127]; } VAR_15 = IV3_NOERR; switch (VAR_9) { case 0:
case 1:
case 3:
case 4:
if (VAR_9 >= 3 && VAR_3->mv_ptr) { av_log(VAR_1, AV_LOG_ERROR, "Attempt to apply Mode 3/4 to an INTER VAR_3!\n"); return AVERROR_INVALIDDATA; } VAR_13 = VAR_9 >= 3; VAR_15 = decode_cell_data(VAR_3, block, ref_block, VAR_2->pitch, 0, VAR_13,
VAR_9, VAR_17, VAR_16, &VAR_4, VAR_5); break; case 10:
case 11:
if (VAR_9 == 10 && !VAR_3->mv_ptr) { VAR_15 = decode_cell_data(VAR_3, block, ref_block, VAR_2->pitch, 1, 1,
VAR_9, VAR_17, VAR_16, &VAR_4, VAR_5); } else { if (VAR_9 == 11 && !VAR_3->mv_ptr) { av_log(VAR_1, AV_LOG_ERROR, "Attempt to use Mode 11 for an INTRA VAR_3!\n"); return AVERROR_INVALIDDATA; } VAR_13 = VAR_9 == 10; VAR_15 = decode_cell_data(VAR_3, block, ref_block, VAR_2->pitch,
VAR_13, 1, VAR_9, VAR_17, VAR_16,
&VAR_4, VAR_5); } break; default:
av_log(VAR_1, AV_LOG_ERROR, "Unsupported coding VAR_9: %d\n", VAR_9); return AVERROR_INVALIDDATA; } switch (VAR_15) { case IV3_BAD_RLE:
av_log(VAR_1, AV_LOG_ERROR, "Mode %d: RLE code %X is not allowed at the current line\n",
VAR_9, VAR_4[-1]); return AVERROR_INVALIDDATA; case IV3_BAD_DATA:
av_log(VAR_1, AV_LOG_ERROR, "Mode %d: invalid VQ data\n", VAR_9); return AVERROR_INVALIDDATA; case IV3_BAD_COUNTER:
av_log(VAR_1, AV_LOG_ERROR, "Mode %d: RLE-FB invalid counter: %d\n", VAR_9, code); return AVERROR_INVALIDDATA; case IV3_UNSUPPORTED:
av_log(VAR_1, AV_LOG_ERROR, "Mode %d: unsupported RLE code: %X\n", VAR_9, VAR_4[-1]); return AVERROR_INVALIDDATA; case IV3_OUT_OF_DATA:
av_log(VAR_1, AV_LOG_ERROR, "Mode %d: attempt to read past end of buffer\n", VAR_9); return AVERROR_INVALIDDATA; } return VAR_4 - VAR_18; }