static int decode_band(IVI5DecContext *ctx, int plane_num,

                       IVIBandDesc *band, AVCodecContext *avctx)

{

    int         result, i, t, idx1, idx2;

    IVITile     *tile;



    band->buf     = band->bufs[ctx->dst_buf];

    band->ref_buf = band->bufs[ctx->ref_buf];

    band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);



    result = decode_band_hdr(ctx, band, avctx);

    if (result) {

        av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",

               result);

        return -1;

    }



    if (band->is_empty) {

        av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");

        return -1;

    }



    band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];



    /* apply corrections to the selected rvmap table if present */

    for (i = 0; i < band->num_corr; i++) {

        idx1 = band->corr[i*2];

        idx2 = band->corr[i*2+1];

        FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);

        FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);

    }



    for (t = 0; t < band->num_tiles; t++) {

        tile = &band->tiles[t];



        tile->is_empty = get_bits1(&ctx->gb);

        if (tile->is_empty) {

            ff_ivi_process_empty_tile(avctx, band, tile,

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

            align_get_bits(&ctx->gb);

        } else {

            tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);



            result = decode_mb_info(ctx, band, tile, avctx);

            if (result < 0)

                break;



            if (band->blk_size == 8) {

                band->intra_base  = &ivi5_base_quant_8x8_intra[band->quant_mat][0];

                band->inter_base  = &ivi5_base_quant_8x8_inter[band->quant_mat][0];

                band->intra_scale = &ivi5_scale_quant_8x8_intra[band->quant_mat][0];

                band->inter_scale = &ivi5_scale_quant_8x8_inter[band->quant_mat][0];

            } else {

                band->intra_base  = ivi5_base_quant_4x4_intra;

                band->inter_base  = ivi5_base_quant_4x4_inter;

                band->intra_scale = ivi5_scale_quant_4x4_intra;

                band->inter_scale = ivi5_scale_quant_4x4_inter;

            }



            result = ff_ivi_decode_blocks(&ctx->gb, band, tile);

            if (result < 0) {

                av_log(avctx, AV_LOG_ERROR, "Corrupted blocks data encountered!\n");

                break;

            }

        }

    }



    /* restore the selected rvmap table by applying its corrections in reverse order */

    for (i = band->num_corr-1; i >= 0; i--) {

        idx1 = band->corr[i*2];

        idx2 = band->corr[i*2+1];

        FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);

        FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);

    }



#if IVI_DEBUG

    if (band->checksum_present) {

        uint16_t chksum = ivi_calc_band_checksum(band);

        if (chksum != band->checksum) {

            av_log(avctx, AV_LOG_ERROR,

                   "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",

                   band->plane, band->band_num, band->checksum, chksum);

        }

    }

#endif



    return result;

}

static int decode_band(IVI5DecContext *ctx, int plane_num,

                       IVIBandDesc *band, AVCodecContext *avctx)

{

    int         result, i, t, idx1, idx2;

    IVITile     *tile;



    band->buf     = band->bufs[ctx->dst_buf];

    band->ref_buf = band->bufs[ctx->ref_buf];

    band->data_ptr = ctx->frame_data + (get_bits_count(&ctx->gb) >> 3);



    result = decode_band_hdr(ctx, band, avctx);

    if (result) {

        av_log(avctx, AV_LOG_ERROR, "Error while decoding band header: %d\n",

               result);

        return -1;

    }



    if (band->is_empty) {

        av_log(avctx, AV_LOG_ERROR, "Empty band encountered!\n");

        return -1;

    }



    band->rv_map = &ctx->rvmap_tabs[band->rvmap_sel];



    

    for (i = 0; i < band->num_corr; i++) {

        idx1 = band->corr[i*2];

        idx2 = band->corr[i*2+1];

        FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);

        FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);

    }



    for (t = 0; t < band->num_tiles; t++) {

        tile = &band->tiles[t];



        tile->is_empty = get_bits1(&ctx->gb);

        if (tile->is_empty) {

            ff_ivi_process_empty_tile(avctx, band, tile,

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

            align_get_bits(&ctx->gb);

        } else {

            tile->data_size = ff_ivi_dec_tile_data_size(&ctx->gb);



            result = decode_mb_info(ctx, band, tile, avctx);

            if (result < 0)

                break;



            if (band->blk_size == 8) {

                band->intra_base  = &ivi5_base_quant_8x8_intra[band->quant_mat][0];

                band->inter_base  = &ivi5_base_quant_8x8_inter[band->quant_mat][0];

                band->intra_scale = &ivi5_scale_quant_8x8_intra[band->quant_mat][0];

                band->inter_scale = &ivi5_scale_quant_8x8_inter[band->quant_mat][0];

            } else {

                band->intra_base  = ivi5_base_quant_4x4_intra;

                band->inter_base  = ivi5_base_quant_4x4_inter;

                band->intra_scale = ivi5_scale_quant_4x4_intra;

                band->inter_scale = ivi5_scale_quant_4x4_inter;

            }



            result = ff_ivi_decode_blocks(&ctx->gb, band, tile);

            if (result < 0) {

                av_log(avctx, AV_LOG_ERROR, "Corrupted blocks data encountered!\n");

                break;

            }

        }

    }



    

    for (i = band->num_corr-1; i >= 0; i--) {

        idx1 = band->corr[i*2];

        idx2 = band->corr[i*2+1];

        FFSWAP(uint8_t, band->rv_map->runtab[idx1], band->rv_map->runtab[idx2]);

        FFSWAP(int16_t, band->rv_map->valtab[idx1], band->rv_map->valtab[idx2]);

    }



#if IVI_DEBUG

    if (band->checksum_present) {

        uint16_t chksum = ivi_calc_band_checksum(band);

        if (chksum != band->checksum) {

            av_log(avctx, AV_LOG_ERROR,

                   "Band checksum mismatch! Plane %d, band %d, received: %x, calculated: %x\n",

                   band->plane, band->band_num, band->checksum, chksum);

        }

    }

#endif



    return result;

}

static int FUNC_0(IVI5DecContext *VAR_0, int VAR_1,

                       IVIBandDesc *VAR_2, AVCodecContext *VAR_3)

{

    int         VAR_4, VAR_5, VAR_6, VAR_7, VAR_8;

    IVITile     *tile;



    VAR_2->buf     = VAR_2->bufs[VAR_0->dst_buf];

    VAR_2->ref_buf = VAR_2->bufs[VAR_0->ref_buf];

    VAR_2->data_ptr = VAR_0->frame_data + (get_bits_count(&VAR_0->gb) >> 3);



    VAR_4 = decode_band_hdr(VAR_0, VAR_2, VAR_3);

    if (VAR_4) {

        av_log(VAR_3, AV_LOG_ERROR, "Error while decoding VAR_2 header: %d\n",

               VAR_4);

        return -1;

    }



    if (VAR_2->is_empty) {

        av_log(VAR_3, AV_LOG_ERROR, "Empty VAR_2 encountered!\n");

        return -1;

    }



    VAR_2->rv_map = &VAR_0->rvmap_tabs[VAR_2->rvmap_sel];



    

    for (VAR_5 = 0; VAR_5 < VAR_2->num_corr; VAR_5++) {

        VAR_7 = VAR_2->corr[VAR_5*2];

        VAR_8 = VAR_2->corr[VAR_5*2+1];

        FFSWAP(uint8_t, VAR_2->rv_map->runtab[VAR_7], VAR_2->rv_map->runtab[VAR_8]);

        FFSWAP(int16_t, VAR_2->rv_map->valtab[VAR_7], VAR_2->rv_map->valtab[VAR_8]);

    }



    for (VAR_6 = 0; VAR_6 < VAR_2->num_tiles; VAR_6++) {

        tile = &VAR_2->tiles[VAR_6];



        tile->is_empty = get_bits1(&VAR_0->gb);

        if (tile->is_empty) {

            ff_ivi_process_empty_tile(VAR_3, VAR_2, tile,

                                      (VAR_0->planes[0].bands[0].mb_size >> 3) - (VAR_2->mb_size >> 3));

            align_get_bits(&VAR_0->gb);

        } else {

            tile->data_size = ff_ivi_dec_tile_data_size(&VAR_0->gb);



            VAR_4 = decode_mb_info(VAR_0, VAR_2, tile, VAR_3);

            if (VAR_4 < 0)

                break;



            if (VAR_2->blk_size == 8) {

                VAR_2->intra_base  = &ivi5_base_quant_8x8_intra[VAR_2->quant_mat][0];

                VAR_2->inter_base  = &ivi5_base_quant_8x8_inter[VAR_2->quant_mat][0];

                VAR_2->intra_scale = &ivi5_scale_quant_8x8_intra[VAR_2->quant_mat][0];

                VAR_2->inter_scale = &ivi5_scale_quant_8x8_inter[VAR_2->quant_mat][0];

            } else {

                VAR_2->intra_base  = ivi5_base_quant_4x4_intra;

                VAR_2->inter_base  = ivi5_base_quant_4x4_inter;

                VAR_2->intra_scale = ivi5_scale_quant_4x4_intra;

                VAR_2->inter_scale = ivi5_scale_quant_4x4_inter;

            }



            VAR_4 = ff_ivi_decode_blocks(&VAR_0->gb, VAR_2, tile);

            if (VAR_4 < 0) {

                av_log(VAR_3, AV_LOG_ERROR, "Corrupted blocks data encountered!\n");

                break;

            }

        }

    }



    

    for (VAR_5 = VAR_2->num_corr-1; VAR_5 >= 0; VAR_5--) {

        VAR_7 = VAR_2->corr[VAR_5*2];

        VAR_8 = VAR_2->corr[VAR_5*2+1];

        FFSWAP(uint8_t, VAR_2->rv_map->runtab[VAR_7], VAR_2->rv_map->runtab[VAR_8]);

        FFSWAP(int16_t, VAR_2->rv_map->valtab[VAR_7], VAR_2->rv_map->valtab[VAR_8]);

    }



#if IVI_DEBUG

    if (VAR_2->checksum_present) {

        uint16_t chksum = ivi_calc_band_checksum(VAR_2);

        if (chksum != VAR_2->checksum) {

            av_log(VAR_3, AV_LOG_ERROR,

                   "Band checksum mismatch! Plane %d, VAR_2 %d, received: %x, calculated: %x\n",

                   VAR_2->plane, VAR_2->band_num, VAR_2->checksum, chksum);

        }

    }

#endif



    return VAR_4;

}

static int FUNC_0(IVI5DecContext *VAR_0, int VAR_1,
IVIBandDesc *VAR_2, AVCodecContext *VAR_3)
{ int         VAR_4, VAR_5, VAR_6, VAR_7, VAR_8; IVITile     *tile; VAR_2->buf     = VAR_2->bufs[VAR_0->dst_buf]; VAR_2->ref_buf = VAR_2->bufs[VAR_0->ref_buf]; VAR_2->data_ptr = VAR_0->frame_data + (get_bits_count(&VAR_0->gb) >> 3); VAR_4 = decode_band_hdr(VAR_0, VAR_2, VAR_3); if (VAR_4) { av_log(VAR_3, AV_LOG_ERROR, "Error while decoding VAR_2 header: %d\n",
VAR_4); return -1; } if (VAR_2->is_empty) { av_log(VAR_3, AV_LOG_ERROR, "Empty VAR_2 encountered!\n"); return -1; } VAR_2->rv_map = &VAR_0->rvmap_tabs[VAR_2->rvmap_sel]; for (VAR_5 = 0; VAR_5 < VAR_2->num_corr; VAR_5++) { VAR_7 = VAR_2->corr[VAR_5*2]; VAR_8 = VAR_2->corr[VAR_5*2+1]; FFSWAP(uint8_t, VAR_2->rv_map->runtab[VAR_7], VAR_2->rv_map->runtab[VAR_8]); FFSWAP(int16_t, VAR_2->rv_map->valtab[VAR_7], VAR_2->rv_map->valtab[VAR_8]); } for (VAR_6 = 0; VAR_6 < VAR_2->num_tiles; VAR_6++) { tile = &VAR_2->tiles[VAR_6]; tile->is_empty = get_bits1(&VAR_0->gb); if (tile->is_empty) { ff_ivi_process_empty_tile(VAR_3, VAR_2, tile,
(VAR_0->planes[0].bands[0].mb_size >> 3) - (VAR_2->mb_size >> 3)); align_get_bits(&VAR_0->gb); } else { tile->data_size = ff_ivi_dec_tile_data_size(&VAR_0->gb); VAR_4 = decode_mb_info(VAR_0, VAR_2, tile, VAR_3); if (VAR_4 < 0)
break; if (VAR_2->blk_size == 8) { VAR_2->intra_base  = &ivi5_base_quant_8x8_intra[VAR_2->quant_mat][0]; VAR_2->inter_base  = &ivi5_base_quant_8x8_inter[VAR_2->quant_mat][0]; VAR_2->intra_scale = &ivi5_scale_quant_8x8_intra[VAR_2->quant_mat][0]; VAR_2->inter_scale = &ivi5_scale_quant_8x8_inter[VAR_2->quant_mat][0]; } else { VAR_2->intra_base  = ivi5_base_quant_4x4_intra; VAR_2->inter_base  = ivi5_base_quant_4x4_inter; VAR_2->intra_scale = ivi5_scale_quant_4x4_intra; VAR_2->inter_scale = ivi5_scale_quant_4x4_inter; } VAR_4 = ff_ivi_decode_blocks(&VAR_0->gb, VAR_2, tile); if (VAR_4 < 0) { av_log(VAR_3, AV_LOG_ERROR, "Corrupted blocks data encountered!\n"); break; } } } for (VAR_5 = VAR_2->num_corr-1; VAR_5 >= 0; VAR_5--) { VAR_7 = VAR_2->corr[VAR_5*2]; VAR_8 = VAR_2->corr[VAR_5*2+1]; FFSWAP(uint8_t, VAR_2->rv_map->runtab[VAR_7], VAR_2->rv_map->runtab[VAR_8]); FFSWAP(int16_t, VAR_2->rv_map->valtab[VAR_7], VAR_2->rv_map->valtab[VAR_8]); } #if IVI_DEBUG
if (VAR_2->checksum_present) { uint16_t chksum = ivi_calc_band_checksum(VAR_2); if (chksum != VAR_2->checksum) { av_log(VAR_3, AV_LOG_ERROR,
"Band checksum mismatch! Plane %d, VAR_2 %d, received: %x, calculated: %x\n",
VAR_2->plane, VAR_2->band_num, VAR_2->checksum, chksum); } } #endif
return VAR_4; }