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

                           AVCodecContext *avctx)

{

    int plane, band_num, indx, transform_id, scan_indx;

    int i;

    int quant_mat;



    plane    = get_bits(&ctx->gb, 2);

    band_num = get_bits(&ctx->gb, 4);

    if (band->plane != plane || band->band_num != band_num) {

        av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");

        return AVERROR_INVALIDDATA;

    }



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

    if (!band->is_empty) {

        /* skip header size

         * If header size is not given, header size is 4 bytes. */

        if (get_bits1(&ctx->gb))

            skip_bits(&ctx->gb, 16);



        band->is_halfpel = get_bits(&ctx->gb, 2);

        if (band->is_halfpel >= 2) {

            av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",

                   band->is_halfpel);

            return AVERROR_INVALIDDATA;

        }

#if IVI4_STREAM_ANALYSER

        if (!band->is_halfpel)

            ctx->uses_fullpel = 1;

#endif



        band->checksum_present = get_bits1(&ctx->gb);

        if (band->checksum_present)

            band->checksum = get_bits(&ctx->gb, 16);



        indx = get_bits(&ctx->gb, 2);

        if (indx == 3) {

            av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");

            return AVERROR_INVALIDDATA;

        }

        band->mb_size  = 16 >> indx;

        band->blk_size = 8 >> (indx >> 1);



        band->inherit_mv     = get_bits1(&ctx->gb);

        band->inherit_qdelta = get_bits1(&ctx->gb);



        band->glob_quant = get_bits(&ctx->gb, 5);



        if (!get_bits1(&ctx->gb) || ctx->frame_type == FRAMETYPE_INTRA) {

            transform_id = get_bits(&ctx->gb, 5);

            if (transform_id >= FF_ARRAY_ELEMS(transforms) ||

                !transforms[transform_id].inv_trans) {

                av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id);

                return AVERROR_PATCHWELCOME;

            }

            if ((transform_id >= 7 && transform_id <= 9) ||

                 transform_id == 17) {

                av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n");

                return AVERROR_PATCHWELCOME;

            }



            if (transform_id < 10 && band->blk_size < 8) {

                av_log(avctx, AV_LOG_ERROR, "wrong transform size!\n");

                return AVERROR_INVALIDDATA;

            }

#if IVI4_STREAM_ANALYSER

            if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)

                ctx->uses_haar = 1;

#endif



            band->inv_transform = transforms[transform_id].inv_trans;

            band->dc_transform  = transforms[transform_id].dc_trans;

            band->is_2d_trans   = transforms[transform_id].is_2d_trans;

            band->transform_size= (transform_id < 10) ? 8 : 4;



            scan_indx = get_bits(&ctx->gb, 4);

            if ((scan_indx>4 && scan_indx<10) != (band->blk_size==4)) {

                av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");

                return AVERROR_INVALIDDATA;

            }

            if (scan_indx == 15) {

                av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            band->scan = scan_index_to_tab[scan_indx];



            quant_mat = get_bits(&ctx->gb, 5);

            if (quant_mat == 31) {

                av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            if (quant_mat > 21) {

                av_log(avctx, AV_LOG_ERROR, "Invalid quant matrix encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            band->quant_mat = quant_mat;

        }



        /* decode block huffman codebook */

        if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF,

                                 &band->blk_vlc, avctx))

            return AVERROR_INVALIDDATA;



        /* select appropriate rvmap table for this band */

        band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;



        /* decode rvmap probability corrections if any */

        band->num_corr = 0; /* there is no corrections */

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

            band->num_corr = get_bits(&ctx->gb, 8); /* get number of correction pairs */

            if (band->num_corr > 61) {

                av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",

                       band->num_corr);

                return AVERROR_INVALIDDATA;

            }



            /* read correction pairs */

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

                band->corr[i] = get_bits(&ctx->gb, 8);

        }

    }



    if (band->blk_size == 8) {

        band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];

        band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];

    } else {

        band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];

        band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];

    }



    /* Indeo 4 doesn't use scale tables */

    band->intra_scale = NULL;

    band->inter_scale = NULL;



    align_get_bits(&ctx->gb);



    if (!band->scan) {

        av_log(avctx, AV_LOG_ERROR, "band->scan not set\n");

        return AVERROR_INVALIDDATA;

    }



    return 0;

}

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

                           AVCodecContext *avctx)

{

    int plane, band_num, indx, transform_id, scan_indx;

    int i;

    int quant_mat;



    plane    = get_bits(&ctx->gb, 2);

    band_num = get_bits(&ctx->gb, 4);

    if (band->plane != plane || band->band_num != band_num) {

        av_log(avctx, AV_LOG_ERROR, "Invalid band header sequence!\n");

        return AVERROR_INVALIDDATA;

    }



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

    if (!band->is_empty) {

        



        if (get_bits1(&ctx->gb))

            skip_bits(&ctx->gb, 16);



        band->is_halfpel = get_bits(&ctx->gb, 2);

        if (band->is_halfpel >= 2) {

            av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",

                   band->is_halfpel);

            return AVERROR_INVALIDDATA;

        }

#if IVI4_STREAM_ANALYSER

        if (!band->is_halfpel)

            ctx->uses_fullpel = 1;

#endif



        band->checksum_present = get_bits1(&ctx->gb);

        if (band->checksum_present)

            band->checksum = get_bits(&ctx->gb, 16);



        indx = get_bits(&ctx->gb, 2);

        if (indx == 3) {

            av_log(avctx, AV_LOG_ERROR, "Invalid block size!\n");

            return AVERROR_INVALIDDATA;

        }

        band->mb_size  = 16 >> indx;

        band->blk_size = 8 >> (indx >> 1);



        band->inherit_mv     = get_bits1(&ctx->gb);

        band->inherit_qdelta = get_bits1(&ctx->gb);



        band->glob_quant = get_bits(&ctx->gb, 5);



        if (!get_bits1(&ctx->gb) || ctx->frame_type == FRAMETYPE_INTRA) {

            transform_id = get_bits(&ctx->gb, 5);

            if (transform_id >= FF_ARRAY_ELEMS(transforms) ||

                !transforms[transform_id].inv_trans) {

                av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id);

                return AVERROR_PATCHWELCOME;

            }

            if ((transform_id >= 7 && transform_id <= 9) ||

                 transform_id == 17) {

                av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n");

                return AVERROR_PATCHWELCOME;

            }



            if (transform_id < 10 && band->blk_size < 8) {

                av_log(avctx, AV_LOG_ERROR, "wrong transform size!\n");

                return AVERROR_INVALIDDATA;

            }

#if IVI4_STREAM_ANALYSER

            if ((transform_id >= 0 && transform_id <= 2) || transform_id == 10)

                ctx->uses_haar = 1;

#endif



            band->inv_transform = transforms[transform_id].inv_trans;

            band->dc_transform  = transforms[transform_id].dc_trans;

            band->is_2d_trans   = transforms[transform_id].is_2d_trans;

            band->transform_size= (transform_id < 10) ? 8 : 4;



            scan_indx = get_bits(&ctx->gb, 4);

            if ((scan_indx>4 && scan_indx<10) != (band->blk_size==4)) {

                av_log(avctx, AV_LOG_ERROR, "mismatching scan table!\n");

                return AVERROR_INVALIDDATA;

            }

            if (scan_indx == 15) {

                av_log(avctx, AV_LOG_ERROR, "Custom scan pattern encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            band->scan = scan_index_to_tab[scan_indx];



            quant_mat = get_bits(&ctx->gb, 5);

            if (quant_mat == 31) {

                av_log(avctx, AV_LOG_ERROR, "Custom quant matrix encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            if (quant_mat > 21) {

                av_log(avctx, AV_LOG_ERROR, "Invalid quant matrix encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            band->quant_mat = quant_mat;

        }



        

        if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF,

                                 &band->blk_vlc, avctx))

            return AVERROR_INVALIDDATA;



        

        band->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;



        

        band->num_corr = 0; 

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

            band->num_corr = get_bits(&ctx->gb, 8); 

            if (band->num_corr > 61) {

                av_log(avctx, AV_LOG_ERROR, "Too many corrections: %d\n",

                       band->num_corr);

                return AVERROR_INVALIDDATA;

            }



            

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

                band->corr[i] = get_bits(&ctx->gb, 8);

        }

    }



    if (band->blk_size == 8) {

        band->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[band->quant_mat]][0];

        band->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[band->quant_mat]][0];

    } else {

        band->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[band->quant_mat]][0];

        band->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[band->quant_mat]][0];

    }



    

    band->intra_scale = NULL;

    band->inter_scale = NULL;



    align_get_bits(&ctx->gb);



    if (!band->scan) {

        av_log(avctx, AV_LOG_ERROR, "band->scan not set\n");

        return AVERROR_INVALIDDATA;

    }



    return 0;

}

static int FUNC_0(IVI4DecContext *VAR_0, IVIBandDesc *VAR_1,

                           AVCodecContext *VAR_2)

{

    int VAR_3, VAR_4, VAR_5, VAR_6, VAR_7;

    int VAR_8;

    int VAR_9;



    VAR_3    = get_bits(&VAR_0->gb, 2);

    VAR_4 = get_bits(&VAR_0->gb, 4);

    if (VAR_1->VAR_3 != VAR_3 || VAR_1->VAR_4 != VAR_4) {

        av_log(VAR_2, AV_LOG_ERROR, "Invalid VAR_1 header sequence!\n");

        return AVERROR_INVALIDDATA;

    }



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

    if (!VAR_1->is_empty) {

        



        if (get_bits1(&VAR_0->gb))

            skip_bits(&VAR_0->gb, 16);



        VAR_1->is_halfpel = get_bits(&VAR_0->gb, 2);

        if (VAR_1->is_halfpel >= 2) {

            av_log(VAR_2, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",

                   VAR_1->is_halfpel);

            return AVERROR_INVALIDDATA;

        }

#if IVI4_STREAM_ANALYSER

        if (!VAR_1->is_halfpel)

            VAR_0->uses_fullpel = 1;

#endif



        VAR_1->checksum_present = get_bits1(&VAR_0->gb);

        if (VAR_1->checksum_present)

            VAR_1->checksum = get_bits(&VAR_0->gb, 16);



        VAR_5 = get_bits(&VAR_0->gb, 2);

        if (VAR_5 == 3) {

            av_log(VAR_2, AV_LOG_ERROR, "Invalid block size!\n");

            return AVERROR_INVALIDDATA;

        }

        VAR_1->mb_size  = 16 >> VAR_5;

        VAR_1->blk_size = 8 >> (VAR_5 >> 1);



        VAR_1->inherit_mv     = get_bits1(&VAR_0->gb);

        VAR_1->inherit_qdelta = get_bits1(&VAR_0->gb);



        VAR_1->glob_quant = get_bits(&VAR_0->gb, 5);



        if (!get_bits1(&VAR_0->gb) || VAR_0->frame_type == FRAMETYPE_INTRA) {

            VAR_6 = get_bits(&VAR_0->gb, 5);

            if (VAR_6 >= FF_ARRAY_ELEMS(transforms) ||

                !transforms[VAR_6].inv_trans) {

                av_log_ask_for_sample(VAR_2, "Unimplemented transform: %d!\n", VAR_6);

                return AVERROR_PATCHWELCOME;

            }

            if ((VAR_6 >= 7 && VAR_6 <= 9) ||

                 VAR_6 == 17) {

                av_log_ask_for_sample(VAR_2, "DCT transform not supported yet!\n");

                return AVERROR_PATCHWELCOME;

            }



            if (VAR_6 < 10 && VAR_1->blk_size < 8) {

                av_log(VAR_2, AV_LOG_ERROR, "wrong transform size!\n");

                return AVERROR_INVALIDDATA;

            }

#if IVI4_STREAM_ANALYSER

            if ((VAR_6 >= 0 && VAR_6 <= 2) || VAR_6 == 10)

                VAR_0->uses_haar = 1;

#endif



            VAR_1->inv_transform = transforms[VAR_6].inv_trans;

            VAR_1->dc_transform  = transforms[VAR_6].dc_trans;

            VAR_1->is_2d_trans   = transforms[VAR_6].is_2d_trans;

            VAR_1->transform_size= (VAR_6 < 10) ? 8 : 4;



            VAR_7 = get_bits(&VAR_0->gb, 4);

            if ((VAR_7>4 && VAR_7<10) != (VAR_1->blk_size==4)) {

                av_log(VAR_2, AV_LOG_ERROR, "mismatching scan table!\n");

                return AVERROR_INVALIDDATA;

            }

            if (VAR_7 == 15) {

                av_log(VAR_2, AV_LOG_ERROR, "Custom scan pattern encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            VAR_1->scan = scan_index_to_tab[VAR_7];



            VAR_9 = get_bits(&VAR_0->gb, 5);

            if (VAR_9 == 31) {

                av_log(VAR_2, AV_LOG_ERROR, "Custom quant matrix encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            if (VAR_9 > 21) {

                av_log(VAR_2, AV_LOG_ERROR, "Invalid quant matrix encountered!\n");

                return AVERROR_INVALIDDATA;

            }

            VAR_1->VAR_9 = VAR_9;

        }



        

        if (ff_ivi_dec_huff_desc(&VAR_0->gb, get_bits1(&VAR_0->gb), IVI_BLK_HUFF,

                                 &VAR_1->blk_vlc, VAR_2))

            return AVERROR_INVALIDDATA;



        

        VAR_1->rvmap_sel = get_bits1(&VAR_0->gb) ? get_bits(&VAR_0->gb, 3) : 8;



        

        VAR_1->num_corr = 0; 

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

            VAR_1->num_corr = get_bits(&VAR_0->gb, 8); 

            if (VAR_1->num_corr > 61) {

                av_log(VAR_2, AV_LOG_ERROR, "Too many corrections: %d\n",

                       VAR_1->num_corr);

                return AVERROR_INVALIDDATA;

            }



            

            for (VAR_8 = 0; VAR_8 < VAR_1->num_corr * 2; VAR_8++)

                VAR_1->corr[VAR_8] = get_bits(&VAR_0->gb, 8);

        }

    }



    if (VAR_1->blk_size == 8) {

        VAR_1->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[VAR_1->VAR_9]][0];

        VAR_1->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[VAR_1->VAR_9]][0];

    } else {

        VAR_1->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[VAR_1->VAR_9]][0];

        VAR_1->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[VAR_1->VAR_9]][0];

    }



    

    VAR_1->intra_scale = NULL;

    VAR_1->inter_scale = NULL;



    align_get_bits(&VAR_0->gb);



    if (!VAR_1->scan) {

        av_log(VAR_2, AV_LOG_ERROR, "VAR_1->scan not set\n");

        return AVERROR_INVALIDDATA;

    }



    return 0;

}

static int FUNC_0(IVI4DecContext *VAR_0, IVIBandDesc *VAR_1,
AVCodecContext *VAR_2)
{ int VAR_3, VAR_4, VAR_5, VAR_6, VAR_7; int VAR_8; int VAR_9; VAR_3    = get_bits(&VAR_0->gb, 2); VAR_4 = get_bits(&VAR_0->gb, 4); if (VAR_1->VAR_3 != VAR_3 || VAR_1->VAR_4 != VAR_4) { av_log(VAR_2, AV_LOG_ERROR, "Invalid VAR_1 header sequence!\n"); return AVERROR_INVALIDDATA; } VAR_1->is_empty = get_bits1(&VAR_0->gb); if (!VAR_1->is_empty) { if (get_bits1(&VAR_0->gb))
skip_bits(&VAR_0->gb, 16); VAR_1->is_halfpel = get_bits(&VAR_0->gb, 2); if (VAR_1->is_halfpel >= 2) { av_log(VAR_2, AV_LOG_ERROR, "Invalid/unsupported mv resolution: %d!\n",
VAR_1->is_halfpel); return AVERROR_INVALIDDATA; } #if IVI4_STREAM_ANALYSER
if (!VAR_1->is_halfpel)
VAR_0->uses_fullpel = 1; #endif
VAR_1->checksum_present = get_bits1(&VAR_0->gb); if (VAR_1->checksum_present)
VAR_1->checksum = get_bits(&VAR_0->gb, 16); VAR_5 = get_bits(&VAR_0->gb, 2); if (VAR_5 == 3) { av_log(VAR_2, AV_LOG_ERROR, "Invalid block size!\n"); return AVERROR_INVALIDDATA; } VAR_1->mb_size  = 16 >> VAR_5; VAR_1->blk_size = 8 >> (VAR_5 >> 1); VAR_1->inherit_mv     = get_bits1(&VAR_0->gb); VAR_1->inherit_qdelta = get_bits1(&VAR_0->gb); VAR_1->glob_quant = get_bits(&VAR_0->gb, 5); if (!get_bits1(&VAR_0->gb) || VAR_0->frame_type == FRAMETYPE_INTRA) { VAR_6 = get_bits(&VAR_0->gb, 5); if (VAR_6 >= FF_ARRAY_ELEMS(transforms) ||
!transforms[VAR_6].inv_trans) { av_log_ask_for_sample(VAR_2, "Unimplemented transform: %d!\n", VAR_6); return AVERROR_PATCHWELCOME; } if ((VAR_6 >= 7 && VAR_6 <= 9) ||
VAR_6 == 17) { av_log_ask_for_sample(VAR_2, "DCT transform not supported yet!\n"); return AVERROR_PATCHWELCOME; } if (VAR_6 < 10 && VAR_1->blk_size < 8) { av_log(VAR_2, AV_LOG_ERROR, "wrong transform size!\n"); return AVERROR_INVALIDDATA; } #if IVI4_STREAM_ANALYSER
if ((VAR_6 >= 0 && VAR_6 <= 2) || VAR_6 == 10)
VAR_0->uses_haar = 1; #endif
VAR_1->inv_transform = transforms[VAR_6].inv_trans; VAR_1->dc_transform  = transforms[VAR_6].dc_trans; VAR_1->is_2d_trans   = transforms[VAR_6].is_2d_trans; VAR_1->transform_size= (VAR_6 < 10) ? 8 : 4; VAR_7 = get_bits(&VAR_0->gb, 4); if ((VAR_7>4 && VAR_7<10) != (VAR_1->blk_size==4)) { av_log(VAR_2, AV_LOG_ERROR, "mismatching scan table!\n"); return AVERROR_INVALIDDATA; } if (VAR_7 == 15) { av_log(VAR_2, AV_LOG_ERROR, "Custom scan pattern encountered!\n"); return AVERROR_INVALIDDATA; } VAR_1->scan = scan_index_to_tab[VAR_7]; VAR_9 = get_bits(&VAR_0->gb, 5); if (VAR_9 == 31) { av_log(VAR_2, AV_LOG_ERROR, "Custom quant matrix encountered!\n"); return AVERROR_INVALIDDATA; } if (VAR_9 > 21) { av_log(VAR_2, AV_LOG_ERROR, "Invalid quant matrix encountered!\n"); return AVERROR_INVALIDDATA; } VAR_1->VAR_9 = VAR_9; } if (ff_ivi_dec_huff_desc(&VAR_0->gb, get_bits1(&VAR_0->gb), IVI_BLK_HUFF,
&VAR_1->blk_vlc, VAR_2))
return AVERROR_INVALIDDATA; VAR_1->rvmap_sel = get_bits1(&VAR_0->gb) ? get_bits(&VAR_0->gb, 3) : 8; VAR_1->num_corr = 0; if (get_bits1(&VAR_0->gb)) { VAR_1->num_corr = get_bits(&VAR_0->gb, 8); if (VAR_1->num_corr > 61) { av_log(VAR_2, AV_LOG_ERROR, "Too many corrections: %d\n",
VAR_1->num_corr); return AVERROR_INVALIDDATA; } for (VAR_8 = 0; VAR_8 < VAR_1->num_corr * 2; VAR_8++) VAR_1->corr[VAR_8] = get_bits(&VAR_0->gb, 8); } } if (VAR_1->blk_size == 8) { VAR_1->intra_base = &ivi4_quant_8x8_intra[quant_index_to_tab[VAR_1->VAR_9]][0]; VAR_1->inter_base = &ivi4_quant_8x8_inter[quant_index_to_tab[VAR_1->VAR_9]][0]; } else { VAR_1->intra_base = &ivi4_quant_4x4_intra[quant_index_to_tab[VAR_1->VAR_9]][0]; VAR_1->inter_base = &ivi4_quant_4x4_inter[quant_index_to_tab[VAR_1->VAR_9]][0]; } VAR_1->intra_scale = NULL; VAR_1->inter_scale = NULL; align_get_bits(&VAR_0->gb); if (!VAR_1->scan) { av_log(VAR_2, AV_LOG_ERROR, "VAR_1->scan not set\n"); return AVERROR_INVALIDDATA; } return 0; }