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

                          IVITile *tile, AVCodecContext *avctx)

{

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

                mv_scale, blks_per_mb;

    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;



    if (!ref_mb &&

        ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))

        return AVERROR_INVALIDDATA;



    /* scale factor for motion vectors */

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

    mv_x = mv_y = 0;



    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 -1;

                }

                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->frame_flags & 8)) {

                    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 && ref_mb){

                    /* 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_bits1(&ctx->gb);

                }



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

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



                mb->q_delta = 0;

                if (band->qdelta_present) {

                    if (band->inherit_qdelta) {

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

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

                                           (ctx->frame_flags & 8))) {

                        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 && ref_mb){

                        /* 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;

                    }

                }

            }



            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(IVI5DecContext *ctx, IVIBandDesc *band,

                          IVITile *tile, AVCodecContext *avctx)

{

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

                mv_scale, blks_per_mb;

    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;



    if (!ref_mb &&

        ((band->qdelta_present && band->inherit_qdelta) || band->inherit_mv))

        return AVERROR_INVALIDDATA;



    

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

    mv_x = mv_y = 0;



    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 -1;

                }

                mb->type = 1; 

                mb->cbp  = 0; 



                mb->q_delta = 0;

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

                    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 && ref_mb){

                    

                    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_bits1(&ctx->gb);

                }



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

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



                mb->q_delta = 0;

                if (band->qdelta_present) {

                    if (band->inherit_qdelta) {

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

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

                                           (ctx->frame_flags & 8))) {

                        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 && ref_mb){

                        

                        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;

                    }

                }

            }



            mb++;

            if (ref_mb)

                ref_mb++;

            mb_offset += band->mb_size;

        }



        offs += row_offset;

    }



    align_get_bits(&ctx->gb);



    return 0;

}

static int FUNC_0(IVI5DecContext *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;

    IVIMbInfo   *mb, *ref_mb;

    int         VAR_13 = 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;



    if (!ref_mb &&

        ((VAR_1->qdelta_present && VAR_1->inherit_qdelta) || VAR_1->inherit_mv))

        return AVERROR_INVALIDDATA;



    

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

    VAR_6 = VAR_7 = 0;



    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 -1;

                }

                mb->type = 1; 

                mb->cbp  = 0; 



                mb->q_delta = 0;

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

                    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 && ref_mb){

                    

                    if (VAR_11) {

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

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

                    } 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_bits1(&VAR_0->gb);

                }



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

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



                mb->q_delta = 0;

                if (VAR_1->qdelta_present) {

                    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->frame_flags & 8))) {

                        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 && ref_mb){

                        

                        if (VAR_11) {

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

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

                        } 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;

                    }

                }

            }



            mb++;

            if (ref_mb)

                ref_mb++;

            VAR_10 += VAR_1->mb_size;

        }



        VAR_9 += VAR_13;

    }



    align_get_bits(&VAR_0->gb);



    return 0;

}

static int FUNC_0(IVI5DecContext *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; IVIMbInfo   *mb, *ref_mb; int         VAR_13 = 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; if (!ref_mb &&
((VAR_1->qdelta_present && VAR_1->inherit_qdelta) || VAR_1->inherit_mv))
return AVERROR_INVALIDDATA; VAR_11 = (VAR_0->planes[0].bands[0].mb_size >> 3) - (VAR_1->mb_size >> 3); VAR_6 = VAR_7 = 0; 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 -1; } mb->type = 1; mb->cbp  = 0; mb->q_delta = 0; if (!VAR_1->plane && !VAR_1->band_num && (VAR_0->frame_flags & 8)) { 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 && ref_mb){ if (VAR_11) { mb->VAR_6 = ivi_scale_mv(ref_mb->VAR_6, VAR_11); mb->VAR_7 = ivi_scale_mv(ref_mb->VAR_7, VAR_11); } 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_bits1(&VAR_0->gb); } VAR_12 = VAR_1->mb_size != VAR_1->blk_size ? 4 : 1; mb->cbp = get_bits(&VAR_0->gb, VAR_12); mb->q_delta = 0; if (VAR_1->qdelta_present) { 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->frame_flags & 8))) { 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 && ref_mb){ if (VAR_11) { mb->VAR_6 = ivi_scale_mv(ref_mb->VAR_6, VAR_11); mb->VAR_7 = ivi_scale_mv(ref_mb->VAR_7, VAR_11); } 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; } } } mb++; if (ref_mb)
ref_mb++; VAR_10 += VAR_1->mb_size; } VAR_9 += VAR_13; } align_get_bits(&VAR_0->gb); return 0; }