static int ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile,
                             AVCodecContext *avctx)
{
    int         mbn, blk, num_blocks, num_coeffs, blk_size, scan_pos, run, val,
                pos, is_intra, mc_type = 0, mv_x, mv_y, col_mask;
    uint8_t     col_flags[8];
    int32_t     prev_dc, trvec[64];
    uint32_t    cbp, sym, lo, hi, quant, buf_offs, q;
    IVIMbInfo   *mb;
    RVMapDesc   *rvmap = band->rv_map;
    void (*mc_with_delta_func)(int16_t *buf, const int16_t *ref_buf, uint32_t pitch, int mc_type);
    void (*mc_no_delta_func)  (int16_t *buf, const int16_t *ref_buf, uint32_t pitch, int mc_type);
    const uint16_t  *base_tab;
    const uint8_t   *scale_tab;
    prev_dc = 0; /* init intra prediction for the DC coefficient */
    blk_size   = band->blk_size;
    col_mask   = blk_size - 1; /* column mask for tracking non-zero coeffs */
    num_blocks = (band->mb_size != blk_size) ? 4 : 1; /* number of blocks per mb */
    num_coeffs = blk_size * blk_size;
    if (blk_size == 8) {
        mc_with_delta_func = ff_ivi_mc_8x8_delta;
        mc_no_delta_func   = ff_ivi_mc_8x8_no_delta;
    } else {
        mc_with_delta_func = ff_ivi_mc_4x4_delta;
        mc_no_delta_func   = ff_ivi_mc_4x4_no_delta;
    for (mbn = 0, mb = tile->mbs; mbn < tile->num_MBs; mb++, mbn++) {
        is_intra = !mb->type;
        cbp      = mb->cbp;
        buf_offs = mb->buf_offs;
        quant = av_clip(band->glob_quant + mb->q_delta, 0, 23);
        base_tab  = is_intra ? band->intra_base  : band->inter_base;
        scale_tab = is_intra ? band->intra_scale : band->inter_scale;
        if (scale_tab)
            quant = scale_tab[quant];
        if (!is_intra) {
            mv_x = mb->mv_x;
            mv_y = mb->mv_y;
            if (band->is_halfpel) {
                mc_type = ((mv_y & 1) << 1) | (mv_x & 1);
                mv_x >>= 1;
                mv_y >>= 1; /* convert halfpel vectors into fullpel ones */
            if (mb->type) {
                int dmv_x, dmv_y, cx, cy;
                dmv_x = mb->mv_x >> band->is_halfpel;
                dmv_y = mb->mv_y >> band->is_halfpel;
                cx    = mb->mv_x &  band->is_halfpel;
                cy    = mb->mv_y &  band->is_halfpel;
                if (   mb->xpos + dmv_x < 0
                    || mb->xpos + dmv_x + band->mb_size + cx > band->pitch
                    || mb->ypos + dmv_y < 0
                    || mb->ypos + dmv_y + band->mb_size + cy > band->aheight) {
        for (blk = 0; blk < num_blocks; blk++) {
            /* adjust block position in the buffer according to its number */
            if (blk & 1) {
                buf_offs += blk_size;
            } else if (blk == 2) {
                buf_offs -= blk_size;
                buf_offs += blk_size * band->pitch;
            if (cbp & 1) { /* block coded ? */
                scan_pos = -1;
                memset(trvec, 0, num_coeffs*sizeof(trvec[0])); /* zero transform vector */
                memset(col_flags, 0, sizeof(col_flags));      /* zero column flags */
                while (scan_pos <= num_coeffs) {
                    sym = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                    if (sym == rvmap->eob_sym)
                        break; /* End of block */
                    if (sym == rvmap->esc_sym) { /* Escape - run/val explicitly coded using 3 vlc codes */
                        run = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1) + 1;
                        lo  = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                        hi  = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                        val = IVI_TOSIGNED((hi << 6) | lo); /* merge them and convert into signed val */
                    } else {
                        if (sym >= 256U) {
                            av_log(avctx, AV_LOG_ERROR, "Invalid sym encountered: %d.\n", sym);
                            return -1;
                        run = rvmap->runtab[sym];
                        val = rvmap->valtab[sym];
                    /* de-zigzag and dequantize */
                    scan_pos += run;
                    if (scan_pos >= num_coeffs)
                        break;
                    pos = band->scan[scan_pos];
                    if (!val)
                        av_dlog(avctx, "Val = 0 encountered!\n");
                    q = (base_tab[pos] * quant) >> 9;
                    if (q > 1)
                        val = val * q + FFSIGN(val) * (((q ^ 1) - 1) >> 1);
                    trvec[pos] = val;
                    col_flags[pos & col_mask] |= !!val; /* track columns containing non-zero coeffs */
                }// while
                if (scan_pos >= num_coeffs && sym != rvmap->eob_sym)
                    return -1; /* corrupt block data */
                /* undoing DC coeff prediction for intra-blocks */
                if (is_intra && band->is_2d_trans) {
                    prev_dc      += trvec[0];
                    trvec[0]      = prev_dc;
                    col_flags[0] |= !!prev_dc;
                /* apply inverse transform */
                band->inv_transform(trvec, band->buf + buf_offs,
                                    band->pitch, col_flags);
                /* apply motion compensation */
                if (!is_intra)
                    mc_with_delta_func(band->buf + buf_offs,
                                       band->ref_buf + buf_offs + mv_y * band->pitch + mv_x,
                                       band->pitch, mc_type);
            } else {
                /* block not coded */
                /* for intra blocks apply the dc slant transform */
                /* for inter - perform the motion compensation without delta */
                if (is_intra && band->dc_transform) {
                    band->dc_transform(&prev_dc, band->buf + buf_offs,
                                       band->pitch, blk_size);
                } else
                    mc_no_delta_func(band->buf + buf_offs,
                                     band->ref_buf + buf_offs + mv_y * band->pitch + mv_x,
                                     band->pitch, mc_type);
            cbp >>= 1;
        }// for blk
    }// for mbn
    align_get_bits(gb);
    return 0;
static int ivi_decode_blocks(GetBitContext *gb, IVIBandDesc *band, IVITile *tile,
                             AVCodecContext *avctx)
{
    int         mbn, blk, num_blocks, num_coeffs, blk_size, scan_pos, run, val,
                pos, is_intra, mc_type = 0, mv_x, mv_y, col_mask;
    uint8_t     col_flags[8];
    int32_t     prev_dc, trvec[64];
    uint32_t    cbp, sym, lo, hi, quant, buf_offs, q;
    IVIMbInfo   *mb;
    RVMapDesc   *rvmap = band->rv_map;
    void (*mc_with_delta_func)(int16_t *buf, const int16_t *ref_buf, uint32_t pitch, int mc_type);
    void (*mc_no_delta_func)  (int16_t *buf, const int16_t *ref_buf, uint32_t pitch, int mc_type);
    const uint16_t  *base_tab;
    const uint8_t   *scale_tab;
    prev_dc = 0; 
    blk_size   = band->blk_size;
    col_mask   = blk_size - 1; 
    num_blocks = (band->mb_size != blk_size) ? 4 : 1; 
    num_coeffs = blk_size * blk_size;
    if (blk_size == 8) {
        mc_with_delta_func = ff_ivi_mc_8x8_delta;
        mc_no_delta_func   = ff_ivi_mc_8x8_no_delta;
    } else {
        mc_with_delta_func = ff_ivi_mc_4x4_delta;
        mc_no_delta_func   = ff_ivi_mc_4x4_no_delta;
    for (mbn = 0, mb = tile->mbs; mbn < tile->num_MBs; mb++, mbn++) {
        is_intra = !mb->type;
        cbp      = mb->cbp;
        buf_offs = mb->buf_offs;
        quant = av_clip(band->glob_quant + mb->q_delta, 0, 23);
        base_tab  = is_intra ? band->intra_base  : band->inter_base;
        scale_tab = is_intra ? band->intra_scale : band->inter_scale;
        if (scale_tab)
            quant = scale_tab[quant];
        if (!is_intra) {
            mv_x = mb->mv_x;
            mv_y = mb->mv_y;
            if (band->is_halfpel) {
                mc_type = ((mv_y & 1) << 1) | (mv_x & 1);
                mv_x >>= 1;
                mv_y >>= 1; 
            if (mb->type) {
                int dmv_x, dmv_y, cx, cy;
                dmv_x = mb->mv_x >> band->is_halfpel;
                dmv_y = mb->mv_y >> band->is_halfpel;
                cx    = mb->mv_x &  band->is_halfpel;
                cy    = mb->mv_y &  band->is_halfpel;
                if (   mb->xpos + dmv_x < 0
                    || mb->xpos + dmv_x + band->mb_size + cx > band->pitch
                    || mb->ypos + dmv_y < 0
                    || mb->ypos + dmv_y + band->mb_size + cy > band->aheight) {
        for (blk = 0; blk < num_blocks; blk++) {
            
            if (blk & 1) {
                buf_offs += blk_size;
            } else if (blk == 2) {
                buf_offs -= blk_size;
                buf_offs += blk_size * band->pitch;
            if (cbp & 1) { 
                scan_pos = -1;
                memset(trvec, 0, num_coeffs*sizeof(trvec[0])); 
                memset(col_flags, 0, sizeof(col_flags));      
                while (scan_pos <= num_coeffs) {
                    sym = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                    if (sym == rvmap->eob_sym)
                        break; 
                    if (sym == rvmap->esc_sym) { 
                        run = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1) + 1;
                        lo  = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                        hi  = get_vlc2(gb, band->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                        val = IVI_TOSIGNED((hi << 6) | lo); 
                    } else {
                        if (sym >= 256U) {
                            av_log(avctx, AV_LOG_ERROR, "Invalid sym encountered: %d.\n", sym);
                            return -1;
                        run = rvmap->runtab[sym];
                        val = rvmap->valtab[sym];
                    
                    scan_pos += run;
                    if (scan_pos >= num_coeffs)
                        break;
                    pos = band->scan[scan_pos];
                    if (!val)
                        av_dlog(avctx, "Val = 0 encountered!\n");
                    q = (base_tab[pos] * quant) >> 9;
                    if (q > 1)
                        val = val * q + FFSIGN(val) * (((q ^ 1) - 1) >> 1);
                    trvec[pos] = val;
                    col_flags[pos & col_mask] |= !!val; 
                }
                if (scan_pos >= num_coeffs && sym != rvmap->eob_sym)
                    return -1; 
                
                if (is_intra && band->is_2d_trans) {
                    prev_dc      += trvec[0];
                    trvec[0]      = prev_dc;
                    col_flags[0] |= !!prev_dc;
                
                band->inv_transform(trvec, band->buf + buf_offs,
                                    band->pitch, col_flags);
                
                if (!is_intra)
                    mc_with_delta_func(band->buf + buf_offs,
                                       band->ref_buf + buf_offs + mv_y * band->pitch + mv_x,
                                       band->pitch, mc_type);
            } else {
                
                
                
                if (is_intra && band->dc_transform) {
                    band->dc_transform(&prev_dc, band->buf + buf_offs,
                                       band->pitch, blk_size);
                } else
                    mc_no_delta_func(band->buf + buf_offs,
                                     band->ref_buf + buf_offs + mv_y * band->pitch + mv_x,
                                     band->pitch, mc_type);
            cbp >>= 1;
        }
    }
    align_get_bits(gb);
    return 0;
static int FUNC_0(GetBitContext *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_23 = 0, VAR_15, VAR_16, VAR_17;
    uint8_t     col_flags[8];
    int32_t     prev_dc, trvec[64];
    uint32_t    cbp, sym, lo, hi, quant, buf_offs, q;
    IVIMbInfo   *mb;
    RVMapDesc   *rvmap = VAR_1->rv_map;
    void (*VAR_18)(int16_t *VAR_23, const int16_t *VAR_23, uint32_t VAR_23, int VAR_23);
    void (*VAR_22)  (int16_t *VAR_23, const int16_t *VAR_23, uint32_t VAR_23, int VAR_23);
    const uint16_t  *VAR_23;
    const uint8_t   *VAR_24;
    prev_dc = 0; 
    VAR_8   = VAR_1->VAR_8;
    VAR_17   = VAR_8 - 1; 
    VAR_6 = (VAR_1->mb_size != VAR_8) ? 4 : 1; 
    VAR_7 = VAR_8 * VAR_8;
    if (VAR_8 == 8) {
        VAR_18 = ff_ivi_mc_8x8_delta;
        VAR_22   = ff_ivi_mc_8x8_no_delta;
    } else {
        VAR_18 = ff_ivi_mc_4x4_delta;
        VAR_22   = ff_ivi_mc_4x4_no_delta;
    for (VAR_4 = 0, mb = VAR_2->mbs; VAR_4 < VAR_2->num_MBs; mb++, VAR_4++) {
        VAR_13 = !mb->type;
        cbp      = mb->cbp;
        buf_offs = mb->buf_offs;
        quant = av_clip(VAR_1->glob_quant + mb->q_delta, 0, 23);
        VAR_23  = VAR_13 ? VAR_1->intra_base  : VAR_1->inter_base;
        VAR_24 = VAR_13 ? VAR_1->intra_scale : VAR_1->inter_scale;
        if (VAR_24)
            quant = VAR_24[quant];
        if (!VAR_13) {
            VAR_15 = mb->VAR_15;
            VAR_16 = mb->VAR_16;
            if (VAR_1->is_halfpel) {
                VAR_23 = ((VAR_16 & 1) << 1) | (VAR_15 & 1);
                VAR_15 >>= 1;
                VAR_16 >>= 1; 
            if (mb->type) {
                int dmv_x, dmv_y, cx, cy;
                dmv_x = mb->VAR_15 >> VAR_1->is_halfpel;
                dmv_y = mb->VAR_16 >> VAR_1->is_halfpel;
                cx    = mb->VAR_15 &  VAR_1->is_halfpel;
                cy    = mb->VAR_16 &  VAR_1->is_halfpel;
                if (   mb->xpos + dmv_x < 0
                    || mb->xpos + dmv_x + VAR_1->mb_size + cx > VAR_1->VAR_23
                    || mb->ypos + dmv_y < 0
                    || mb->ypos + dmv_y + VAR_1->mb_size + cy > VAR_1->aheight) {
        for (VAR_5 = 0; VAR_5 < VAR_6; VAR_5++) {
            
            if (VAR_5 & 1) {
                buf_offs += VAR_8;
            } else if (VAR_5 == 2) {
                buf_offs -= VAR_8;
                buf_offs += VAR_8 * VAR_1->VAR_23;
            if (cbp & 1) { 
                VAR_9 = -1;
                memset(trvec, 0, VAR_7*sizeof(trvec[0])); 
                memset(col_flags, 0, sizeof(col_flags));      
                while (VAR_9 <= VAR_7) {
                    sym = get_vlc2(VAR_0, VAR_1->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                    if (sym == rvmap->eob_sym)
                        break; 
                    if (sym == rvmap->esc_sym) { 
                        VAR_10 = get_vlc2(VAR_0, VAR_1->blk_vlc.tab->table, IVI_VLC_BITS, 1) + 1;
                        lo  = get_vlc2(VAR_0, VAR_1->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                        hi  = get_vlc2(VAR_0, VAR_1->blk_vlc.tab->table, IVI_VLC_BITS, 1);
                        VAR_11 = IVI_TOSIGNED((hi << 6) | lo); 
                    } else {
                        if (sym >= 256U) {
                            av_log(VAR_3, AV_LOG_ERROR, "Invalid sym encountered: %d.\n", sym);
                            return -1;
                        VAR_10 = rvmap->runtab[sym];
                        VAR_11 = rvmap->valtab[sym];
                    
                    VAR_9 += VAR_10;
                    if (VAR_9 >= VAR_7)
                        break;
                    VAR_12 = VAR_1->scan[VAR_9];
                    if (!VAR_11)
                        av_dlog(VAR_3, "Val = 0 encountered!\n");
                    q = (VAR_23[VAR_12] * quant) >> 9;
                    if (q > 1)
                        VAR_11 = VAR_11 * q + FFSIGN(VAR_11) * (((q ^ 1) - 1) >> 1);
                    trvec[VAR_12] = VAR_11;
                    col_flags[VAR_12 & VAR_17] |= !!VAR_11; 
                }
                if (VAR_9 >= VAR_7 && sym != rvmap->eob_sym)
                    return -1; 
                
                if (VAR_13 && VAR_1->is_2d_trans) {
                    prev_dc      += trvec[0];
                    trvec[0]      = prev_dc;
                    col_flags[0] |= !!prev_dc;
                
                VAR_1->inv_transform(trvec, VAR_1->VAR_23 + buf_offs,
                                    VAR_1->VAR_23, col_flags);
                
                if (!VAR_13)
                    VAR_18(VAR_1->VAR_23 + buf_offs,
                                       VAR_1->VAR_23 + buf_offs + VAR_16 * VAR_1->VAR_23 + VAR_15,
                                       VAR_1->VAR_23, VAR_23);
            } else {
                
                
                
                if (VAR_13 && VAR_1->dc_transform) {
                    VAR_1->dc_transform(&prev_dc, VAR_1->VAR_23 + buf_offs,
                                       VAR_1->VAR_23, VAR_8);
                } else
                    VAR_22(VAR_1->VAR_23 + buf_offs,
                                     VAR_1->VAR_23 + buf_offs + VAR_16 * VAR_1->VAR_23 + VAR_15,
                                     VAR_1->VAR_23, VAR_23);
            cbp >>= 1;
        }
    }
    align_get_bits(VAR_0);
    return 0;
static int FUNC_0(GetBitContext *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_23 = 0, VAR_15, VAR_16, VAR_17; uint8_t     col_flags[8]; int32_t     prev_dc, trvec[64]; uint32_t    cbp, sym, lo, hi, quant, buf_offs, q; IVIMbInfo   *mb; RVMapDesc   *rvmap = VAR_1->rv_map; void (*VAR_18)(int16_t *VAR_23, const int16_t *VAR_23, uint32_t VAR_23, int VAR_23); void (*VAR_22)  (int16_t *VAR_23, const int16_t *VAR_23, uint32_t VAR_23, int VAR_23); const uint16_t  *VAR_23; const uint8_t   *VAR_24; prev_dc = 0; VAR_8   = VAR_1->VAR_8; VAR_17   = VAR_8 - 1; VAR_6 = (VAR_1->mb_size != VAR_8) ? 4 : 1; VAR_7 = VAR_8 * VAR_8; if (VAR_8 == 8) { VAR_18 = ff_ivi_mc_8x8_delta; VAR_22   = ff_ivi_mc_8x8_no_delta; } else { VAR_18 = ff_ivi_mc_4x4_delta; VAR_22   = ff_ivi_mc_4x4_no_delta; for (VAR_4 = 0, mb = VAR_2->mbs; VAR_4 < VAR_2->num_MBs; mb++, VAR_4++) { VAR_13 = !mb->type; cbp      = mb->cbp; buf_offs = mb->buf_offs; quant = av_clip(VAR_1->glob_quant + mb->q_delta, 0, 23); VAR_23  = VAR_13 ? VAR_1->intra_base  : VAR_1->inter_base; VAR_24 = VAR_13 ? VAR_1->intra_scale : VAR_1->inter_scale; if (VAR_24)
quant = VAR_24[quant]; if (!VAR_13) { VAR_15 = mb->VAR_15; VAR_16 = mb->VAR_16; if (VAR_1->is_halfpel) { VAR_23 = ((VAR_16 & 1) << 1) | (VAR_15 & 1); VAR_15 >>= 1; VAR_16 >>= 1; if (mb->type) { int dmv_x, dmv_y, cx, cy; dmv_x = mb->VAR_15 >> VAR_1->is_halfpel; dmv_y = mb->VAR_16 >> VAR_1->is_halfpel; cx    = mb->VAR_15 &  VAR_1->is_halfpel; cy    = mb->VAR_16 &  VAR_1->is_halfpel; if (   mb->xpos + dmv_x < 0
|| mb->xpos + dmv_x + VAR_1->mb_size + cx > VAR_1->VAR_23
|| mb->ypos + dmv_y < 0
|| mb->ypos + dmv_y + VAR_1->mb_size + cy > VAR_1->aheight) { for (VAR_5 = 0; VAR_5 < VAR_6; VAR_5++) { if (VAR_5 & 1) { buf_offs += VAR_8; } else if (VAR_5 == 2) { buf_offs -= VAR_8; buf_offs += VAR_8 * VAR_1->VAR_23; if (cbp & 1) { VAR_9 = -1; memset(trvec, 0, VAR_7*sizeof(trvec[0])); memset(col_flags, 0, sizeof(col_flags)); while (VAR_9 <= VAR_7) { sym = get_vlc2(VAR_0, VAR_1->blk_vlc.tab->table, IVI_VLC_BITS, 1); if (sym == rvmap->eob_sym)
break; if (sym == rvmap->esc_sym) { VAR_10 = get_vlc2(VAR_0, VAR_1->blk_vlc.tab->table, IVI_VLC_BITS, 1) + 1; lo  = get_vlc2(VAR_0, VAR_1->blk_vlc.tab->table, IVI_VLC_BITS, 1); hi  = get_vlc2(VAR_0, VAR_1->blk_vlc.tab->table, IVI_VLC_BITS, 1); VAR_11 = IVI_TOSIGNED((hi << 6) | lo); } else { if (sym >= 256U) { av_log(VAR_3, AV_LOG_ERROR, "Invalid sym encountered: %d.\n", sym); return -1; VAR_10 = rvmap->runtab[sym]; VAR_11 = rvmap->valtab[sym]; VAR_9 += VAR_10; if (VAR_9 >= VAR_7)
break; VAR_12 = VAR_1->scan[VAR_9]; if (!VAR_11)
av_dlog(VAR_3, "Val = 0 encountered!\n"); q = (VAR_23[VAR_12] * quant) >> 9; if (q > 1)
VAR_11 = VAR_11 * q + FFSIGN(VAR_11) * (((q ^ 1) - 1) >> 1); trvec[VAR_12] = VAR_11; col_flags[VAR_12 & VAR_17] |= !!VAR_11; } if (VAR_9 >= VAR_7 && sym != rvmap->eob_sym)
return -1; if (VAR_13 && VAR_1->is_2d_trans) { prev_dc      += trvec[0]; trvec[0]      = prev_dc; col_flags[0] |= !!prev_dc; VAR_1->inv_transform(trvec, VAR_1->VAR_23 + buf_offs,
VAR_1->VAR_23, col_flags); if (!VAR_13)
VAR_18(VAR_1->VAR_23 + buf_offs,
VAR_1->VAR_23 + buf_offs + VAR_16 * VAR_1->VAR_23 + VAR_15,
VAR_1->VAR_23, VAR_23); } else { if (VAR_13 && VAR_1->dc_transform) { VAR_1->dc_transform(&prev_dc, VAR_1->VAR_23 + buf_offs,
VAR_1->VAR_23, VAR_8); } else VAR_22(VAR_1->VAR_23 + buf_offs,
VAR_1->VAR_23 + buf_offs + VAR_16 * VAR_1->VAR_23 + VAR_15,
VAR_1->VAR_23, VAR_23); cbp >>= 1; } } align_get_bits(VAR_0); return 0;