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;

}
