static int decode_mb_info(IVI4DecContext *ctx, IVIBandDesc *band,

                          IVITile *tile, AVCodecContext *avctx)

{

    int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,

                mv_scale, mb_type_bits, s;

    IVIMbInfo   *mb, *ref_mb;

    int         row_offset = band->mb_size * band->pitch;



    mb     = tile->mbs;

    ref_mb = tile->ref_mbs;

    offs   = tile->ypos * band->pitch + tile->xpos;



    blks_per_mb  = band->mb_size   != band->blk_size  ? 4 : 1;

    mb_type_bits = ctx->frame_type == FRAMETYPE_BIDIR ? 2 : 1;



    /* scale factor for motion vectors */

    mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);

    mv_x = mv_y = 0;



    if (((tile->width + band->mb_size-1)/band->mb_size) * ((tile->height + band->mb_size-1)/band->mb_size) != tile->num_MBs) {

        av_log(avctx, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", tile->width, tile->height, band->mb_size, tile->num_MBs);

        return -1;

    }



    for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {

        mb_offset = offs;



        for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {

            mb->xpos     = x;

            mb->ypos     = y;

            mb->buf_offs = mb_offset;



            if (get_bits1(&ctx->gb)) {

                if (ctx->frame_type == FRAMETYPE_INTRA) {

                    av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");

                    return AVERROR_INVALIDDATA;

                }

                mb->type = 1; /* empty macroblocks are always INTER */

                mb->cbp  = 0; /* all blocks are empty */



                mb->q_delta = 0;

                if (!band->plane && !band->band_num && ctx->in_q) {

                    mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,

                                           IVI_VLC_BITS, 1);

                    mb->q_delta = IVI_TOSIGNED(mb->q_delta);

                }



                mb->mv_x = mb->mv_y = 0; /* no motion vector coded */

                if (band->inherit_mv) {

                    /* motion vector inheritance */

                    if (mv_scale) {

                        mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);

                        mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);

                    } else {

                        mb->mv_x = ref_mb->mv_x;

                        mb->mv_y = ref_mb->mv_y;

                    }

                }

            } else {

                if (band->inherit_mv && ref_mb) {

                    mb->type = ref_mb->type; /* copy mb_type from corresponding reference mb */

                } else if (ctx->frame_type == FRAMETYPE_INTRA) {

                    mb->type = 0; /* mb_type is always INTRA for intra-frames */

                } else {

                    mb->type = get_bits(&ctx->gb, mb_type_bits);

                }



                mb->cbp = get_bits(&ctx->gb, blks_per_mb);



                mb->q_delta = 0;

                if (band->inherit_qdelta) {

                    if (ref_mb) mb->q_delta = ref_mb->q_delta;

                } else if (mb->cbp || (!band->plane && !band->band_num &&

                           ctx->in_q)) {

                    mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,

                                           IVI_VLC_BITS, 1);

                    mb->q_delta = IVI_TOSIGNED(mb->q_delta);

                }



                if (!mb->type) {

                    mb->mv_x = mb->mv_y = 0; /* there is no motion vector in intra-macroblocks */

                } else {

                    if (band->inherit_mv) {

                        /* motion vector inheritance */

                        if (mv_scale) {

                            mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);

                            mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);

                        } else {

                            mb->mv_x = ref_mb->mv_x;

                            mb->mv_y = ref_mb->mv_y;

                        }

                    } else {

                        /* decode motion vector deltas */

                        mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,

                                            IVI_VLC_BITS, 1);

                        mv_y += IVI_TOSIGNED(mv_delta);

                        mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,

                                            IVI_VLC_BITS, 1);

                        mv_x += IVI_TOSIGNED(mv_delta);

                        mb->mv_x = mv_x;

                        mb->mv_y = mv_y;

                    }

                }

            }



            s= band->is_halfpel;

            if (mb->type)

            if ( x +  (mb->mv_x   >>s) +                 (y+               (mb->mv_y   >>s))*band->pitch < 0 ||

                 x + ((mb->mv_x+s)>>s) + band->mb_size - 1

                   + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize -1) {

                av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);

                return AVERROR_INVALIDDATA;

            }



            mb++;

            if (ref_mb)

                ref_mb++;

            mb_offset += band->mb_size;

        }



        offs += row_offset;

    }



    align_get_bits(&ctx->gb);



    return 0;

}

static int decode_mb_info(IVI4DecContext *ctx, IVIBandDesc *band,

                          IVITile *tile, AVCodecContext *avctx)

{

    int         x, y, mv_x, mv_y, mv_delta, offs, mb_offset, blks_per_mb,

                mv_scale, mb_type_bits, s;

    IVIMbInfo   *mb, *ref_mb;

    int         row_offset = band->mb_size * band->pitch;



    mb     = tile->mbs;

    ref_mb = tile->ref_mbs;

    offs   = tile->ypos * band->pitch + tile->xpos;



    blks_per_mb  = band->mb_size   != band->blk_size  ? 4 : 1;

    mb_type_bits = ctx->frame_type == FRAMETYPE_BIDIR ? 2 : 1;



    

    mv_scale = (ctx->planes[0].bands[0].mb_size >> 3) - (band->mb_size >> 3);

    mv_x = mv_y = 0;



    if (((tile->width + band->mb_size-1)/band->mb_size) * ((tile->height + band->mb_size-1)/band->mb_size) != tile->num_MBs) {

        av_log(avctx, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", tile->width, tile->height, band->mb_size, tile->num_MBs);

        return -1;

    }



    for (y = tile->ypos; y < tile->ypos + tile->height; y += band->mb_size) {

        mb_offset = offs;



        for (x = tile->xpos; x < tile->xpos + tile->width; x += band->mb_size) {

            mb->xpos     = x;

            mb->ypos     = y;

            mb->buf_offs = mb_offset;



            if (get_bits1(&ctx->gb)) {

                if (ctx->frame_type == FRAMETYPE_INTRA) {

                    av_log(avctx, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");

                    return AVERROR_INVALIDDATA;

                }

                mb->type = 1; 

                mb->cbp  = 0; 



                mb->q_delta = 0;

                if (!band->plane && !band->band_num && ctx->in_q) {

                    mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,

                                           IVI_VLC_BITS, 1);

                    mb->q_delta = IVI_TOSIGNED(mb->q_delta);

                }



                mb->mv_x = mb->mv_y = 0; 

                if (band->inherit_mv) {

                    

                    if (mv_scale) {

                        mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);

                        mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);

                    } else {

                        mb->mv_x = ref_mb->mv_x;

                        mb->mv_y = ref_mb->mv_y;

                    }

                }

            } else {

                if (band->inherit_mv && ref_mb) {

                    mb->type = ref_mb->type; 

                } else if (ctx->frame_type == FRAMETYPE_INTRA) {

                    mb->type = 0; 

                } else {

                    mb->type = get_bits(&ctx->gb, mb_type_bits);

                }



                mb->cbp = get_bits(&ctx->gb, blks_per_mb);



                mb->q_delta = 0;

                if (band->inherit_qdelta) {

                    if (ref_mb) mb->q_delta = ref_mb->q_delta;

                } else if (mb->cbp || (!band->plane && !band->band_num &&

                           ctx->in_q)) {

                    mb->q_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,

                                           IVI_VLC_BITS, 1);

                    mb->q_delta = IVI_TOSIGNED(mb->q_delta);

                }



                if (!mb->type) {

                    mb->mv_x = mb->mv_y = 0; 

                } else {

                    if (band->inherit_mv) {

                        

                        if (mv_scale) {

                            mb->mv_x = ivi_scale_mv(ref_mb->mv_x, mv_scale);

                            mb->mv_y = ivi_scale_mv(ref_mb->mv_y, mv_scale);

                        } else {

                            mb->mv_x = ref_mb->mv_x;

                            mb->mv_y = ref_mb->mv_y;

                        }

                    } else {

                        

                        mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,

                                            IVI_VLC_BITS, 1);

                        mv_y += IVI_TOSIGNED(mv_delta);

                        mv_delta = get_vlc2(&ctx->gb, ctx->mb_vlc.tab->table,

                                            IVI_VLC_BITS, 1);

                        mv_x += IVI_TOSIGNED(mv_delta);

                        mb->mv_x = mv_x;

                        mb->mv_y = mv_y;

                    }

                }

            }



            s= band->is_halfpel;

            if (mb->type)

            if ( x +  (mb->mv_x   >>s) +                 (y+               (mb->mv_y   >>s))*band->pitch < 0 ||

                 x + ((mb->mv_x+s)>>s) + band->mb_size - 1

                   + (y+band->mb_size - 1 +((mb->mv_y+s)>>s))*band->pitch > band->bufsize -1) {

                av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", x*s + mb->mv_x, y*s + mb->mv_y);

                return AVERROR_INVALIDDATA;

            }



            mb++;

            if (ref_mb)

                ref_mb++;

            mb_offset += band->mb_size;

        }



        offs += row_offset;

    }



    align_get_bits(&ctx->gb);



    return 0;

}

                if (band->inherit_mv) {                     if (band->inherit_mv) {
static int FUNC_0(IVI4DecContext *VAR_0, IVIBandDesc *VAR_1,

                          IVITile *VAR_2, AVCodecContext *VAR_3)

{

    int         VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10, VAR_11,

                VAR_12, VAR_13, VAR_14;

    IVIMbInfo   *mb, *ref_mb;

    int         VAR_15 = VAR_1->mb_size * VAR_1->pitch;



    mb     = VAR_2->mbs;

    ref_mb = VAR_2->ref_mbs;

    VAR_9   = VAR_2->ypos * VAR_1->pitch + VAR_2->xpos;



    VAR_11  = VAR_1->mb_size   != VAR_1->blk_size  ? 4 : 1;

    VAR_13 = VAR_0->frame_type == FRAMETYPE_BIDIR ? 2 : 1;



    

    VAR_12 = (VAR_0->planes[0].bands[0].mb_size >> 3) - (VAR_1->mb_size >> 3);

    VAR_6 = VAR_7 = 0;



    if (((VAR_2->width + VAR_1->mb_size-1)/VAR_1->mb_size) * ((VAR_2->height + VAR_1->mb_size-1)/VAR_1->mb_size) != VAR_2->num_MBs) {

        av_log(VAR_3, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", VAR_2->width, VAR_2->height, VAR_1->mb_size, VAR_2->num_MBs);

        return -1;

    }



    for (VAR_5 = VAR_2->ypos; VAR_5 < VAR_2->ypos + VAR_2->height; VAR_5 += VAR_1->mb_size) {

        VAR_10 = VAR_9;



        for (VAR_4 = VAR_2->xpos; VAR_4 < VAR_2->xpos + VAR_2->width; VAR_4 += VAR_1->mb_size) {

            mb->xpos     = VAR_4;

            mb->ypos     = VAR_5;

            mb->buf_offs = VAR_10;



            if (get_bits1(&VAR_0->gb)) {

                if (VAR_0->frame_type == FRAMETYPE_INTRA) {

                    av_log(VAR_3, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n");

                    return AVERROR_INVALIDDATA;

                }

                mb->type = 1; 

                mb->cbp  = 0; 



                mb->q_delta = 0;

                if (!VAR_1->plane && !VAR_1->band_num && VAR_0->in_q) {

                    mb->q_delta = get_vlc2(&VAR_0->gb, VAR_0->mb_vlc.tab->table,

                                           IVI_VLC_BITS, 1);

                    mb->q_delta = IVI_TOSIGNED(mb->q_delta);

                }



                mb->VAR_6 = mb->VAR_7 = 0; 

                if (VAR_1->inherit_mv) {

                    

                    if (VAR_12) {

                        mb->VAR_6 = ivi_scale_mv(ref_mb->VAR_6, VAR_12);

                        mb->VAR_7 = ivi_scale_mv(ref_mb->VAR_7, VAR_12);

                    } else {

                        mb->VAR_6 = ref_mb->VAR_6;

                        mb->VAR_7 = ref_mb->VAR_7;

                    }

                }

            } else {

                if (VAR_1->inherit_mv && ref_mb) {

                    mb->type = ref_mb->type; 

                } else if (VAR_0->frame_type == FRAMETYPE_INTRA) {

                    mb->type = 0; 

                } else {

                    mb->type = get_bits(&VAR_0->gb, VAR_13);

                }



                mb->cbp = get_bits(&VAR_0->gb, VAR_11);



                mb->q_delta = 0;

                if (VAR_1->inherit_qdelta) {

                    if (ref_mb) mb->q_delta = ref_mb->q_delta;

                } else if (mb->cbp || (!VAR_1->plane && !VAR_1->band_num &&

                           VAR_0->in_q)) {

                    mb->q_delta = get_vlc2(&VAR_0->gb, VAR_0->mb_vlc.tab->table,

                                           IVI_VLC_BITS, 1);

                    mb->q_delta = IVI_TOSIGNED(mb->q_delta);

                }



                if (!mb->type) {

                    mb->VAR_6 = mb->VAR_7 = 0; 

                } else {

                    if (VAR_1->inherit_mv) {

                        

                        if (VAR_12) {

                            mb->VAR_6 = ivi_scale_mv(ref_mb->VAR_6, VAR_12);

                            mb->VAR_7 = ivi_scale_mv(ref_mb->VAR_7, VAR_12);

                        } else {

                            mb->VAR_6 = ref_mb->VAR_6;

                            mb->VAR_7 = ref_mb->VAR_7;

                        }

                    } else {

                        

                        VAR_8 = get_vlc2(&VAR_0->gb, VAR_0->mb_vlc.tab->table,

                                            IVI_VLC_BITS, 1);

                        VAR_7 += IVI_TOSIGNED(VAR_8);

                        VAR_8 = get_vlc2(&VAR_0->gb, VAR_0->mb_vlc.tab->table,

                                            IVI_VLC_BITS, 1);

                        VAR_6 += IVI_TOSIGNED(VAR_8);

                        mb->VAR_6 = VAR_6;

                        mb->VAR_7 = VAR_7;

                    }

                }

            }



            VAR_14= VAR_1->is_halfpel;

            if (mb->type)

            if ( VAR_4 +  (mb->VAR_6   >>VAR_14) +                 (VAR_5+               (mb->VAR_7   >>VAR_14))*VAR_1->pitch < 0 ||

                 VAR_4 + ((mb->VAR_6+VAR_14)>>VAR_14) + VAR_1->mb_size - 1

                   + (VAR_5+VAR_1->mb_size - 1 +((mb->VAR_7+VAR_14)>>VAR_14))*VAR_1->pitch > VAR_1->bufsize -1) {

                av_log(VAR_3, AV_LOG_ERROR, "motion vector %d %d outside reference\n", VAR_4*VAR_14 + mb->VAR_6, VAR_5*VAR_14 + mb->VAR_7);

                return AVERROR_INVALIDDATA;

            }



            mb++;

            if (ref_mb)

                ref_mb++;

            VAR_10 += VAR_1->mb_size;

        }



        VAR_9 += VAR_15;

    }



    align_get_bits(&VAR_0->gb);



    return 0;

}

static int FUNC_0(IVI4DecContext *VAR_0, IVIBandDesc *VAR_1,
IVITile *VAR_2, AVCodecContext *VAR_3)
{ int         VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10, VAR_11,
VAR_12, VAR_13, VAR_14; IVIMbInfo   *mb, *ref_mb; int         VAR_15 = VAR_1->mb_size * VAR_1->pitch; mb     = VAR_2->mbs; ref_mb = VAR_2->ref_mbs; VAR_9   = VAR_2->ypos * VAR_1->pitch + VAR_2->xpos; VAR_11  = VAR_1->mb_size   != VAR_1->blk_size  ? 4 : 1; VAR_13 = VAR_0->frame_type == FRAMETYPE_BIDIR ? 2 : 1; VAR_12 = (VAR_0->planes[0].bands[0].mb_size >> 3) - (VAR_1->mb_size >> 3); VAR_6 = VAR_7 = 0; if (((VAR_2->width + VAR_1->mb_size-1)/VAR_1->mb_size) * ((VAR_2->height + VAR_1->mb_size-1)/VAR_1->mb_size) != VAR_2->num_MBs) { av_log(VAR_3, AV_LOG_ERROR, "num_MBs mismatch %d %d %d %d\n", VAR_2->width, VAR_2->height, VAR_1->mb_size, VAR_2->num_MBs); return -1; } for (VAR_5 = VAR_2->ypos; VAR_5 < VAR_2->ypos + VAR_2->height; VAR_5 += VAR_1->mb_size) { VAR_10 = VAR_9; for (VAR_4 = VAR_2->xpos; VAR_4 < VAR_2->xpos + VAR_2->width; VAR_4 += VAR_1->mb_size) { mb->xpos     = VAR_4; mb->ypos     = VAR_5; mb->buf_offs = VAR_10; if (get_bits1(&VAR_0->gb)) { if (VAR_0->frame_type == FRAMETYPE_INTRA) { av_log(VAR_3, AV_LOG_ERROR, "Empty macroblock in an INTRA picture!\n"); return AVERROR_INVALIDDATA; } mb->type = 1; mb->cbp  = 0; mb->q_delta = 0; if (!VAR_1->plane && !VAR_1->band_num && VAR_0->in_q) { mb->q_delta = get_vlc2(&VAR_0->gb, VAR_0->mb_vlc.tab->table,
IVI_VLC_BITS, 1); mb->q_delta = IVI_TOSIGNED(mb->q_delta); } mb->VAR_6 = mb->VAR_7 = 0; if (VAR_1->inherit_mv) { if (VAR_12) { mb->VAR_6 = ivi_scale_mv(ref_mb->VAR_6, VAR_12); mb->VAR_7 = ivi_scale_mv(ref_mb->VAR_7, VAR_12); } else { mb->VAR_6 = ref_mb->VAR_6; mb->VAR_7 = ref_mb->VAR_7; } } } else { if (VAR_1->inherit_mv && ref_mb) { mb->type = ref_mb->type; } else if (VAR_0->frame_type == FRAMETYPE_INTRA) { mb->type = 0; } else { mb->type = get_bits(&VAR_0->gb, VAR_13); } mb->cbp = get_bits(&VAR_0->gb, VAR_11); mb->q_delta = 0; if (VAR_1->inherit_qdelta) { if (ref_mb) mb->q_delta = ref_mb->q_delta; } else if (mb->cbp || (!VAR_1->plane && !VAR_1->band_num && VAR_0->in_q)) { mb->q_delta = get_vlc2(&VAR_0->gb, VAR_0->mb_vlc.tab->table,
IVI_VLC_BITS, 1); mb->q_delta = IVI_TOSIGNED(mb->q_delta); } if (!mb->type) { mb->VAR_6 = mb->VAR_7 = 0; } else { if (VAR_1->inherit_mv) { if (VAR_12) { mb->VAR_6 = ivi_scale_mv(ref_mb->VAR_6, VAR_12); mb->VAR_7 = ivi_scale_mv(ref_mb->VAR_7, VAR_12); } else { mb->VAR_6 = ref_mb->VAR_6; mb->VAR_7 = ref_mb->VAR_7; } } else { VAR_8 = get_vlc2(&VAR_0->gb, VAR_0->mb_vlc.tab->table,
IVI_VLC_BITS, 1); VAR_7 += IVI_TOSIGNED(VAR_8); VAR_8 = get_vlc2(&VAR_0->gb, VAR_0->mb_vlc.tab->table,
IVI_VLC_BITS, 1); VAR_6 += IVI_TOSIGNED(VAR_8); mb->VAR_6 = VAR_6; mb->VAR_7 = VAR_7; } } } VAR_14= VAR_1->is_halfpel; if (mb->type)
if ( VAR_4 +  (mb->VAR_6   >>VAR_14) +                 (VAR_5+               (mb->VAR_7   >>VAR_14))*VAR_1->pitch < 0 ||
VAR_4 + ((mb->VAR_6+VAR_14)>>VAR_14) + VAR_1->mb_size - 1
+ (VAR_5+VAR_1->mb_size - 1 +((mb->VAR_7+VAR_14)>>VAR_14))*VAR_1->pitch > VAR_1->bufsize -1) { av_log(VAR_3, AV_LOG_ERROR, "motion vector %d %d outside reference\n", VAR_4*VAR_14 + mb->VAR_6, VAR_5*VAR_14 + mb->VAR_7); return AVERROR_INVALIDDATA; } mb++; if (ref_mb)
ref_mb++; VAR_10 += VAR_1->mb_size; } VAR_9 += VAR_15; } align_get_bits(&VAR_0->gb); return 0; }