LCOV - code coverage report
Current view: top level - libavcodec - vc1_block.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1781 1896 93.9 %
Date: 2017-12-14 01:15:32 Functions: 24 24 100.0 %

          Line data    Source code
       1             : /*
       2             :  * VC-1 and WMV3 decoder
       3             :  * Copyright (c) 2011 Mashiat Sarker Shakkhar
       4             :  * Copyright (c) 2006-2007 Konstantin Shishkov
       5             :  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
       6             :  *
       7             :  * This file is part of FFmpeg.
       8             :  *
       9             :  * FFmpeg is free software; you can redistribute it and/or
      10             :  * modify it under the terms of the GNU Lesser General Public
      11             :  * License as published by the Free Software Foundation; either
      12             :  * version 2.1 of the License, or (at your option) any later version.
      13             :  *
      14             :  * FFmpeg is distributed in the hope that it will be useful,
      15             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :  * Lesser General Public License for more details.
      18             :  *
      19             :  * You should have received a copy of the GNU Lesser General Public
      20             :  * License along with FFmpeg; if not, write to the Free Software
      21             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      22             :  */
      23             : 
      24             : /**
      25             :  * @file
      26             :  * VC-1 and WMV3 block decoding routines
      27             :  */
      28             : 
      29             : #include "avcodec.h"
      30             : #include "mpegutils.h"
      31             : #include "mpegvideo.h"
      32             : #include "msmpeg4data.h"
      33             : #include "unary.h"
      34             : #include "vc1.h"
      35             : #include "vc1_pred.h"
      36             : #include "vc1acdata.h"
      37             : #include "vc1data.h"
      38             : 
      39             : #define MB_INTRA_VLC_BITS 9
      40             : #define DC_VLC_BITS 9
      41             : 
      42             : // offset tables for interlaced picture MVDATA decoding
      43             : static const uint8_t offset_table[2][9] = {
      44             :     {  0,  1,  2,  4,  8, 16, 32,  64, 128 },
      45             :     {  0,  1,  3,  7, 15, 31, 63, 127, 255 },
      46             : };
      47             : 
      48             : /***********************************************************************/
      49             : /**
      50             :  * @name VC-1 Bitplane decoding
      51             :  * @see 8.7, p56
      52             :  * @{
      53             :  */
      54             : 
      55             : 
      56        8990 : static inline void init_block_index(VC1Context *v)
      57             : {
      58        8990 :     MpegEncContext *s = &v->s;
      59        8990 :     ff_init_block_index(s);
      60        8990 :     if (v->field_mode && !(v->second_field ^ v->tff)) {
      61         654 :         s->dest[0] += s->current_picture_ptr->f->linesize[0];
      62         654 :         s->dest[1] += s->current_picture_ptr->f->linesize[1];
      63         654 :         s->dest[2] += s->current_picture_ptr->f->linesize[2];
      64             :     }
      65        8990 : }
      66             : 
      67             : /** @} */ //Bitplane group
      68             : 
      69       12696 : static void vc1_put_signed_blocks_clamped(VC1Context *v)
      70             : {
      71       12696 :     MpegEncContext *s = &v->s;
      72             :     int topleft_mb_pos, top_mb_pos;
      73       12696 :     int stride_y, fieldtx = 0;
      74             :     int v_dist;
      75             : 
      76             :     /* The put pixels loop is always one MB row behind the decoding loop,
      77             :      * because we can only put pixels when overlap filtering is done, and
      78             :      * for filtering of the bottom edge of a MB, we need the next MB row
      79             :      * present as well.
      80             :      * Within the row, the put pixels loop is also one MB col behind the
      81             :      * decoding loop. The reason for this is again, because for filtering
      82             :      * of the right MB edge, we need the next MB present. */
      83       12696 :     if (!s->first_slice_line) {
      84       12114 :         if (s->mb_x) {
      85       11933 :             topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
      86       11933 :             if (v->fcm == ILACE_FRAME)
      87        8092 :                 fieldtx = v->fieldtx_plane[topleft_mb_pos];
      88       11933 :             stride_y       = s->linesize << fieldtx;
      89       11933 :             v_dist         = (16 - fieldtx) >> (fieldtx == 0);
      90       23866 :             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
      91       11933 :                                               s->dest[0] - 16 * s->linesize - 16,
      92             :                                               stride_y);
      93       23866 :             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
      94       11933 :                                               s->dest[0] - 16 * s->linesize - 8,
      95             :                                               stride_y);
      96       23866 :             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
      97       11933 :                                               s->dest[0] - v_dist * s->linesize - 16,
      98             :                                               stride_y);
      99       23866 :             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
     100       11933 :                                               s->dest[0] - v_dist * s->linesize - 8,
     101             :                                               stride_y);
     102             :             if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
     103       23866 :             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
     104       11933 :                                               s->dest[1] - 8 * s->uvlinesize - 8,
     105             :                                               s->uvlinesize);
     106       23866 :             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
     107       11933 :                                               s->dest[2] - 8 * s->uvlinesize - 8,
     108             :                                               s->uvlinesize);
     109             :             }
     110             :         }
     111       12114 :         if (s->mb_x == s->mb_width - 1) {
     112         181 :             top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
     113         181 :             if (v->fcm == ILACE_FRAME)
     114          68 :                 fieldtx = v->fieldtx_plane[top_mb_pos];
     115         181 :             stride_y   = s->linesize << fieldtx;
     116         181 :             v_dist     = fieldtx ? 15 : 8;
     117         362 :             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
     118         181 :                                               s->dest[0] - 16 * s->linesize,
     119             :                                               stride_y);
     120         362 :             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
     121         181 :                                               s->dest[0] - 16 * s->linesize + 8,
     122             :                                               stride_y);
     123         362 :             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
     124         181 :                                               s->dest[0] - v_dist * s->linesize,
     125             :                                               stride_y);
     126         362 :             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
     127         181 :                                               s->dest[0] - v_dist * s->linesize + 8,
     128             :                                               stride_y);
     129             :             if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
     130         362 :             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
     131         181 :                                               s->dest[1] - 8 * s->uvlinesize,
     132             :                                               s->uvlinesize);
     133         362 :             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
     134         181 :                                               s->dest[2] - 8 * s->uvlinesize,
     135             :                                               s->uvlinesize);
     136             :             }
     137             :         }
     138             :     }
     139             : 
     140             : #define inc_blk_idx(idx) do { \
     141             :         idx++; \
     142             :         if (idx >= v->n_allocated_blks) \
     143             :             idx = 0; \
     144             :     } while (0)
     145             : 
     146       12696 :     inc_blk_idx(v->topleft_blk_idx);
     147       12696 :     inc_blk_idx(v->top_blk_idx);
     148       12696 :     inc_blk_idx(v->left_blk_idx);
     149       12696 :     inc_blk_idx(v->cur_blk_idx);
     150       12696 : }
     151             : 
     152             : /***********************************************************************/
     153             : /**
     154             :  * @name VC-1 Block-level functions
     155             :  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
     156             :  * @{
     157             :  */
     158             : 
     159             : /**
     160             :  * @def GET_MQUANT
     161             :  * @brief Get macroblock-level quantizer scale
     162             :  */
     163             : #define GET_MQUANT()                                           \
     164             :     if (v->dquantfrm) {                                        \
     165             :         int edges = 0;                                         \
     166             :         if (v->dqprofile == DQPROFILE_ALL_MBS) {               \
     167             :             if (v->dqbilevel) {                                \
     168             :                 mquant = (get_bits1(gb)) ? v->altpq : v->pq;   \
     169             :             } else {                                           \
     170             :                 mqdiff = get_bits(gb, 3);                      \
     171             :                 if (mqdiff != 7)                               \
     172             :                     mquant = v->pq + mqdiff;                   \
     173             :                 else                                           \
     174             :                     mquant = get_bits(gb, 5);                  \
     175             :             }                                                  \
     176             :         }                                                      \
     177             :         if (v->dqprofile == DQPROFILE_SINGLE_EDGE)             \
     178             :             edges = 1 << v->dqsbedge;                          \
     179             :         else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES)       \
     180             :             edges = (3 << v->dqsbedge) % 15;                   \
     181             :         else if (v->dqprofile == DQPROFILE_FOUR_EDGES)         \
     182             :             edges = 15;                                        \
     183             :         if ((edges&1) && !s->mb_x)                             \
     184             :             mquant = v->altpq;                                 \
     185             :         if ((edges&2) && s->first_slice_line)                  \
     186             :             mquant = v->altpq;                                 \
     187             :         if ((edges&4) && s->mb_x == (s->mb_width - 1))         \
     188             :             mquant = v->altpq;                                 \
     189             :         if ((edges&8) && s->mb_y == (s->mb_height - 1))        \
     190             :             mquant = v->altpq;                                 \
     191             :         if (!mquant || mquant > 31) {                          \
     192             :             av_log(v->s.avctx, AV_LOG_ERROR,                   \
     193             :                    "Overriding invalid mquant %d\n", mquant);  \
     194             :             mquant = 1;                                        \
     195             :         }                                                      \
     196             :     }
     197             : 
     198             : /**
     199             :  * @def GET_MVDATA(_dmv_x, _dmv_y)
     200             :  * @brief Get MV differentials
     201             :  * @see MVDATA decoding from 8.3.5.2, p(1)20
     202             :  * @param _dmv_x Horizontal differential for decoded MV
     203             :  * @param _dmv_y Vertical differential for decoded MV
     204             :  */
     205             : #define GET_MVDATA(_dmv_x, _dmv_y)                                      \
     206             :     index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
     207             :                          VC1_MV_DIFF_VLC_BITS, 2);                      \
     208             :     if (index > 36) {                                                   \
     209             :         mb_has_coeffs = 1;                                              \
     210             :         index -= 37;                                                    \
     211             :     } else                                                              \
     212             :         mb_has_coeffs = 0;                                              \
     213             :     s->mb_intra = 0;                                                    \
     214             :     if (!index) {                                                       \
     215             :         _dmv_x = _dmv_y = 0;                                            \
     216             :     } else if (index == 35) {                                           \
     217             :         _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
     218             :         _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
     219             :     } else if (index == 36) {                                           \
     220             :         _dmv_x = 0;                                                     \
     221             :         _dmv_y = 0;                                                     \
     222             :         s->mb_intra = 1;                                                \
     223             :     } else {                                                            \
     224             :         index1 = index % 6;                                             \
     225             :         _dmv_x = offset_table[1][index1];                               \
     226             :         val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
     227             :         if (val > 0) {                                                  \
     228             :             val = get_bits(gb, val);                                    \
     229             :             sign = 0 - (val & 1);                                       \
     230             :             _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign;             \
     231             :         }                                                               \
     232             :                                                                         \
     233             :         index1 = index / 6;                                             \
     234             :         _dmv_y = offset_table[1][index1];                               \
     235             :         val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
     236             :         if (val > 0) {                                                  \
     237             :             val = get_bits(gb, val);                                    \
     238             :             sign = 0 - (val & 1);                                       \
     239             :             _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign;             \
     240             :         }                                                               \
     241             :     }
     242             : 
     243      118892 : static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
     244             :                                                    int *dmv_y, int *pred_flag)
     245             : {
     246             :     int index, index1;
     247             :     int extend_x, extend_y;
     248      118892 :     GetBitContext *gb = &v->s.gb;
     249             :     int bits, esc;
     250             :     int val, sign;
     251             : 
     252      118892 :     if (v->numref) {
     253       59109 :         bits = VC1_2REF_MVDATA_VLC_BITS;
     254       59109 :         esc  = 125;
     255             :     } else {
     256       59783 :         bits = VC1_1REF_MVDATA_VLC_BITS;
     257       59783 :         esc  = 71;
     258             :     }
     259      118892 :     extend_x = v->dmvrange & 1;
     260      118892 :     extend_y = (v->dmvrange >> 1) & 1;
     261      118892 :     index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
     262      118892 :     if (index == esc) {
     263        8896 :         *dmv_x = get_bits(gb, v->k_x);
     264        8896 :         *dmv_y = get_bits(gb, v->k_y);
     265        8896 :         if (v->numref) {
     266         414 :             if (pred_flag)
     267         414 :                 *pred_flag = *dmv_y & 1;
     268         414 :             *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
     269             :         }
     270             :     }
     271             :     else {
     272      109996 :         av_assert0(index < esc);
     273      109996 :         index1 = (index + 1) % 9;
     274      109996 :         if (index1 != 0) {
     275       84682 :             val    = get_bits(gb, index1 + extend_x);
     276       84682 :             sign   = 0 - (val & 1);
     277       84682 :             *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
     278             :         } else
     279       25314 :             *dmv_x = 0;
     280      109996 :         index1 = (index + 1) / 9;
     281      109996 :         if (index1 > v->numref) {
     282       76703 :             val    = get_bits(gb, (index1 >> v->numref) + extend_y);
     283       76703 :             sign   = 0 - (val & 1);
     284       76703 :             *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
     285             :         } else
     286       33293 :             *dmv_y = 0;
     287      109996 :         if (v->numref && pred_flag)
     288       58695 :             *pred_flag = index1 & 1;
     289             :     }
     290      118892 : }
     291             : 
     292             : /** Reconstruct motion vector for B-frame and do motion compensation
     293             :  */
     294       48166 : static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
     295             :                             int direct, int mode)
     296             : {
     297       48166 :     if (direct) {
     298        8367 :         ff_vc1_mc_1mv(v, 0);
     299        8367 :         ff_vc1_interp_mc(v);
     300        8367 :         return;
     301             :     }
     302       39799 :     if (mode == BMV_TYPE_INTERPOLATED) {
     303        7849 :         ff_vc1_mc_1mv(v, 0);
     304        7849 :         ff_vc1_interp_mc(v);
     305        7849 :         return;
     306             :     }
     307             : 
     308       31950 :     ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
     309             : }
     310             : 
     311             : /** Get predicted DC value for I-frames only
     312             :  * prediction dir: left=0, top=1
     313             :  * @param s MpegEncContext
     314             :  * @param overlap flag indicating that overlap filtering is used
     315             :  * @param pq integer part of picture quantizer
     316             :  * @param[in] n block index in the current MB
     317             :  * @param dc_val_ptr Pointer to DC predictor
     318             :  * @param dir_ptr Prediction direction for use in AC prediction
     319             :  */
     320       74880 : static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
     321             :                                 int16_t **dc_val_ptr, int *dir_ptr)
     322             : {
     323             :     int a, b, c, wrap, pred, scale;
     324             :     int16_t *dc_val;
     325             :     static const uint16_t dcpred[32] = {
     326             :         -1, 1024,  512,  341,  256,  205,  171,  146,  128,
     327             :              114,  102,   93,   85,   79,   73,   68,   64,
     328             :               60,   57,   54,   51,   49,   47,   45,   43,
     329             :               41,   39,   38,   37,   35,   34,   33
     330             :     };
     331             : 
     332             :     /* find prediction - wmv3_dc_scale always used here in fact */
     333       74880 :     if (n < 4) scale = s->y_dc_scale;
     334       24960 :     else       scale = s->c_dc_scale;
     335             : 
     336       74880 :     wrap   = s->block_wrap[n];
     337       74880 :     dc_val = s->dc_val[0] + s->block_index[n];
     338             : 
     339             :     /* B A
     340             :      * C X
     341             :      */
     342       74880 :     c = dc_val[ - 1];
     343       74880 :     b = dc_val[ - 1 - wrap];
     344       74880 :     a = dc_val[ - wrap];
     345             : 
     346       74880 :     if (pq < 9 || !overlap) {
     347             :         /* Set outer values */
     348       74880 :         if (s->first_slice_line && (n != 2 && n != 3))
     349        4360 :             b = a = dcpred[scale];
     350      149760 :         if (s->mb_x == 0 && (n != 1 && n != 3))
     351        3344 :             b = c = dcpred[scale];
     352             :     } else {
     353             :         /* Set outer values */
     354           0 :         if (s->first_slice_line && (n != 2 && n != 3))
     355           0 :             b = a = 0;
     356           0 :         if (s->mb_x == 0 && (n != 1 && n != 3))
     357           0 :             b = c = 0;
     358             :     }
     359             : 
     360       74880 :     if (abs(a - b) <= abs(b - c)) {
     361       51469 :         pred     = c;
     362       51469 :         *dir_ptr = 1; // left
     363             :     } else {
     364       23411 :         pred     = a;
     365       23411 :         *dir_ptr = 0; // top
     366             :     }
     367             : 
     368             :     /* update predictor */
     369       74880 :     *dc_val_ptr = &dc_val[0];
     370       74880 :     return pred;
     371             : }
     372             : 
     373             : 
     374             : /** Get predicted DC value
     375             :  * prediction dir: left=0, top=1
     376             :  * @param s MpegEncContext
     377             :  * @param overlap flag indicating that overlap filtering is used
     378             :  * @param pq integer part of picture quantizer
     379             :  * @param[in] n block index in the current MB
     380             :  * @param a_avail flag indicating top block availability
     381             :  * @param c_avail flag indicating left block availability
     382             :  * @param dc_val_ptr Pointer to DC predictor
     383             :  * @param dir_ptr Prediction direction for use in AC prediction
     384             :  */
     385      291030 : static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
     386             :                               int a_avail, int c_avail,
     387             :                               int16_t **dc_val_ptr, int *dir_ptr)
     388             : {
     389             :     int a, b, c, wrap, pred;
     390             :     int16_t *dc_val;
     391      291030 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
     392      291030 :     int q1, q2 = 0;
     393             :     int dqscale_index;
     394             : 
     395             :     /* scale predictors if needed */
     396      291030 :     q1 = s->current_picture.qscale_table[mb_pos];
     397      291030 :     dqscale_index = s->y_dc_scale_table[q1] - 1;
     398      291030 :     if (dqscale_index < 0)
     399           0 :         return 0;
     400             : 
     401      291030 :     wrap = s->block_wrap[n];
     402      291030 :     dc_val = s->dc_val[0] + s->block_index[n];
     403             : 
     404             :     /* B A
     405             :      * C X
     406             :      */
     407      291030 :     c = dc_val[ - 1];
     408      291030 :     b = dc_val[ - 1 - wrap];
     409      291030 :     a = dc_val[ - wrap];
     410             : 
     411      291030 :     if (c_avail && (n != 1 && n != 3)) {
     412      107084 :         q2 = s->current_picture.qscale_table[mb_pos - 1];
     413      107084 :         if (q2 && q2 != q1)
     414        5816 :             c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
     415             :     }
     416      291030 :     if (a_avail && (n != 2 && n != 3)) {
     417       88553 :         q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
     418       88553 :         if (q2 && q2 != q1)
     419        5480 :             a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
     420             :     }
     421      291030 :     if (a_avail && c_avail && (n != 3)) {
     422       99007 :         int off = mb_pos;
     423       99007 :         if (n != 1)
     424       76892 :             off--;
     425       99007 :         if (n != 2)
     426       72300 :             off -= s->mb_stride;
     427       99007 :         q2 = s->current_picture.qscale_table[off];
     428       99007 :         if (q2 && q2 != q1)
     429        7525 :             b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
     430             :     }
     431             : 
     432      291030 :     if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
     433      153658 :         pred     = c;
     434      153658 :         *dir_ptr = 1; // left
     435      137372 :     } else if (a_avail) {
     436       86929 :         pred     = a;
     437       86929 :         *dir_ptr = 0; // top
     438             :     } else {
     439       50443 :         pred     = 0;
     440       50443 :         *dir_ptr = 1; // left
     441             :     }
     442             : 
     443             :     /* update predictor */
     444      291030 :     *dc_val_ptr = &dc_val[0];
     445      291030 :     return pred;
     446             : }
     447             : 
     448             : /** @} */ // Block group
     449             : 
     450             : /**
     451             :  * @name VC1 Macroblock-level functions in Simple/Main Profiles
     452             :  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
     453             :  * @{
     454             :  */
     455             : 
     456       98376 : static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
     457             :                                        uint8_t **coded_block_ptr)
     458             : {
     459             :     int xy, wrap, pred, a, b, c;
     460             : 
     461       98376 :     xy   = s->block_index[n];
     462       98376 :     wrap = s->b8_stride;
     463             : 
     464             :     /* B C
     465             :      * A X
     466             :      */
     467       98376 :     a = s->coded_block[xy - 1       ];
     468       98376 :     b = s->coded_block[xy - 1 - wrap];
     469       98376 :     c = s->coded_block[xy     - wrap];
     470             : 
     471       98376 :     if (b == c) {
     472       85935 :         pred = a;
     473             :     } else {
     474       12441 :         pred = c;
     475             :     }
     476             : 
     477             :     /* store value */
     478       98376 :     *coded_block_ptr = &s->coded_block[xy];
     479             : 
     480       98376 :     return pred;
     481             : }
     482             : 
     483             : /**
     484             :  * Decode one AC coefficient
     485             :  * @param v The VC1 context
     486             :  * @param last Last coefficient
     487             :  * @param skip How much zero coefficients to skip
     488             :  * @param value Decoded AC coefficient value
     489             :  * @param codingset set of VLC to decode data
     490             :  * @see 8.1.3.4
     491             :  */
     492     5431621 : static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
     493             :                                 int *value, int codingset)
     494             : {
     495     5431621 :     GetBitContext *gb = &v->s.gb;
     496             :     int index, run, level, lst, sign;
     497             : 
     498     5431621 :     index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
     499     5431621 :     if (index != ff_vc1_ac_sizes[codingset] - 1) {
     500     5380728 :         run   = vc1_index_decode_table[codingset][index][0];
     501     5380728 :         level = vc1_index_decode_table[codingset][index][1];
     502     5380728 :         lst   = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
     503     5380728 :         sign  = get_bits1(gb);
     504             :     } else {
     505       50893 :         int escape = decode210(gb);
     506       50893 :         if (escape != 2) {
     507       44521 :             index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
     508       44521 :             run   = vc1_index_decode_table[codingset][index][0];
     509       44521 :             level = vc1_index_decode_table[codingset][index][1];
     510       44521 :             lst   = index >= vc1_last_decode_table[codingset];
     511       44521 :             if (escape == 0) {
     512       21772 :                 if (lst)
     513        9860 :                     level += vc1_last_delta_level_table[codingset][run];
     514             :                 else
     515       11912 :                     level += vc1_delta_level_table[codingset][run];
     516             :             } else {
     517       22749 :                 if (lst)
     518        7352 :                     run += vc1_last_delta_run_table[codingset][level] + 1;
     519             :                 else
     520       15397 :                     run += vc1_delta_run_table[codingset][level] + 1;
     521             :             }
     522       44521 :             sign = get_bits1(gb);
     523             :         } else {
     524        6372 :             lst = get_bits1(gb);
     525        6372 :             if (v->s.esc3_level_length == 0) {
     526         326 :                 if (v->pq < 8 || v->dquantfrm) { // table 59
     527         287 :                     v->s.esc3_level_length = get_bits(gb, 3);
     528         574 :                     if (!v->s.esc3_level_length)
     529          21 :                         v->s.esc3_level_length = get_bits(gb, 2) + 8;
     530             :                 } else { // table 60
     531          39 :                     v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
     532             :                 }
     533         326 :                 v->s.esc3_run_length = 3 + get_bits(gb, 2);
     534             :             }
     535        6372 :             run   = get_bits(gb, v->s.esc3_run_length);
     536        6372 :             sign  = get_bits1(gb);
     537        6372 :             level = get_bits(gb, v->s.esc3_level_length);
     538             :         }
     539             :     }
     540             : 
     541     5431621 :     *last  = lst;
     542     5431621 :     *skip  = run;
     543     5431621 :     *value = (level ^ -sign) + sign;
     544     5431621 : }
     545             : 
     546             : /** Decode intra block in intra frames - should be faster than decode_intra_block
     547             :  * @param v VC1Context
     548             :  * @param block block to decode
     549             :  * @param[in] n subblock index
     550             :  * @param coded are AC coeffs present or not
     551             :  * @param codingset set of VLC to decode data
     552             :  */
     553       74880 : static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
     554             :                               int coded, int codingset)
     555             : {
     556       74880 :     GetBitContext *gb = &v->s.gb;
     557       74880 :     MpegEncContext *s = &v->s;
     558       74880 :     int dc_pred_dir = 0; /* Direction of the DC prediction used */
     559             :     int i;
     560             :     int16_t *dc_val;
     561             :     int16_t *ac_val, *ac_val2;
     562             :     int dcdiff, scale;
     563             : 
     564             :     /* Get DC differential */
     565       74880 :     if (n < 4) {
     566       49920 :         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
     567             :     } else {
     568       24960 :         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
     569             :     }
     570       74880 :     if (dcdiff < 0) {
     571           0 :         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
     572           0 :         return -1;
     573             :     }
     574       74880 :     if (dcdiff) {
     575       52876 :         const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
     576       52876 :         if (dcdiff == 119 /* ESC index value */) {
     577           0 :             dcdiff = get_bits(gb, 8 + m);
     578             :         } else {
     579       52876 :             if (m)
     580           0 :                 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
     581             :         }
     582       52876 :         if (get_bits1(gb))
     583       27549 :             dcdiff = -dcdiff;
     584             :     }
     585             : 
     586             :     /* Prediction */
     587       74880 :     dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
     588       74880 :     *dc_val = dcdiff;
     589             : 
     590             :     /* Store the quantized DC coeff, used for prediction */
     591       74880 :     if (n < 4)
     592       49920 :         scale = s->y_dc_scale;
     593             :     else
     594       24960 :         scale = s->c_dc_scale;
     595       74880 :     block[0] = dcdiff * scale;
     596             : 
     597       74880 :     ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
     598       74880 :     ac_val2 = ac_val;
     599       74880 :     if (dc_pred_dir) // left
     600       51469 :         ac_val -= 16;
     601             :     else // top
     602       23411 :         ac_val -= 16 * s->block_wrap[n];
     603             : 
     604       74880 :     scale = v->pq * 2 + v->halfpq;
     605             : 
     606             :     //AC Decoding
     607       74880 :     i = !!coded;
     608             : 
     609       74880 :     if (coded) {
     610       26460 :         int last = 0, skip, value;
     611             :         const uint8_t *zz_table;
     612             :         int k;
     613             : 
     614       26460 :         if (v->s.ac_pred) {
     615        4460 :             if (!dc_pred_dir)
     616        1292 :                 zz_table = v->zz_8x8[2];
     617             :             else
     618        3168 :                 zz_table = v->zz_8x8[3];
     619             :         } else
     620       22000 :             zz_table = v->zz_8x8[1];
     621             : 
     622      234641 :         while (!last) {
     623      181721 :             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
     624      181721 :             i += skip;
     625      181721 :             if (i > 63)
     626           0 :                 break;
     627      181721 :             block[zz_table[i++]] = value;
     628             :         }
     629             : 
     630             :         /* apply AC prediction if needed */
     631       26460 :         if (s->ac_pred) {
     632             :             int sh;
     633        4460 :             if (dc_pred_dir) { // left
     634        3168 :                 sh = v->left_blk_sh;
     635             :             } else { // top
     636        1292 :                 sh = v->top_blk_sh;
     637        1292 :                 ac_val += 8;
     638             :             }
     639       35680 :             for (k = 1; k < 8; k++)
     640       31220 :                 block[k << sh] += ac_val[k];
     641             :         }
     642             :         /* save AC coeffs for further prediction */
     643      211680 :         for (k = 1; k < 8; k++) {
     644      185220 :             ac_val2[k]     = block[k << v->left_blk_sh];
     645      185220 :             ac_val2[k + 8] = block[k << v->top_blk_sh];
     646             :         }
     647             : 
     648             :         /* scale AC coeffs */
     649     1693440 :         for (k = 1; k < 64; k++)
     650     1666980 :             if (block[k]) {
     651      182350 :                 block[k] *= scale;
     652      182350 :                 if (!v->pquantizer)
     653       66373 :                     block[k] += (block[k] < 0) ? -v->pq : v->pq;
     654             :             }
     655             : 
     656             :     } else {
     657             :         int k;
     658             : 
     659       48420 :         memset(ac_val2, 0, 16 * 2);
     660             : 
     661             :         /* apply AC prediction if needed */
     662       48420 :         if (s->ac_pred) {
     663             :             int sh;
     664        3490 :             if (dc_pred_dir) { //left
     665        2672 :                 sh = v->left_blk_sh;
     666             :             } else { // top
     667         818 :                 sh = v->top_blk_sh;
     668         818 :                 ac_val  += 8;
     669         818 :                 ac_val2 += 8;
     670             :             }
     671        3490 :             memcpy(ac_val2, ac_val, 8 * 2);
     672       27920 :             for (k = 1; k < 8; k++) {
     673       24430 :                 block[k << sh] = ac_val[k] * scale;
     674       24430 :                 if (!v->pquantizer && block[k << sh])
     675         583 :                     block[k << sh] += (block[k << sh] < 0) ? -v->pq : v->pq;
     676             :             }
     677             :         }
     678             :     }
     679       74880 :     if (s->ac_pred) i = 63;
     680       74880 :     s->block_last_index[n] = i;
     681             : 
     682       74880 :     return 0;
     683             : }
     684             : 
     685             : /** Decode intra block in intra frames - should be faster than decode_intra_block
     686             :  * @param v VC1Context
     687             :  * @param block block to decode
     688             :  * @param[in] n subblock number
     689             :  * @param coded are AC coeffs present or not
     690             :  * @param codingset set of VLC to decode data
     691             :  * @param mquant quantizer value for this macroblock
     692             :  */
     693       72684 : static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
     694             :                                   int coded, int codingset, int mquant)
     695             : {
     696       72684 :     GetBitContext *gb = &v->s.gb;
     697       72684 :     MpegEncContext *s = &v->s;
     698       72684 :     int dc_pred_dir = 0; /* Direction of the DC prediction used */
     699             :     int i;
     700       72684 :     int16_t *dc_val = NULL;
     701             :     int16_t *ac_val, *ac_val2;
     702             :     int dcdiff;
     703       72684 :     int a_avail = v->a_avail, c_avail = v->c_avail;
     704       72684 :     int use_pred = s->ac_pred;
     705             :     int scale;
     706       72684 :     int q1, q2 = 0;
     707       72684 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
     708             : 
     709             :     /* Get DC differential */
     710       72684 :     if (n < 4) {
     711       48456 :         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
     712             :     } else {
     713       24228 :         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
     714             :     }
     715       72684 :     if (dcdiff < 0) {
     716           0 :         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
     717           0 :         return -1;
     718             :     }
     719       72684 :     if (dcdiff) {
     720       50133 :         const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
     721       50133 :         if (dcdiff == 119 /* ESC index value */) {
     722           1 :             dcdiff = get_bits(gb, 8 + m);
     723             :         } else {
     724       50132 :             if (m)
     725        6545 :                 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
     726             :         }
     727       50133 :         if (get_bits1(gb))
     728       25831 :             dcdiff = -dcdiff;
     729             :     }
     730             : 
     731             :     /* Prediction */
     732       72684 :     dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
     733       72684 :     *dc_val = dcdiff;
     734             : 
     735             :     /* Store the quantized DC coeff, used for prediction */
     736       72684 :     if (n < 4)
     737       48456 :         scale = s->y_dc_scale;
     738             :     else
     739       24228 :         scale = s->c_dc_scale;
     740       72684 :     block[0] = dcdiff * scale;
     741             : 
     742             :     /* check if AC is needed at all */
     743       72684 :     if (!a_avail && !c_avail)
     744          42 :         use_pred = 0;
     745             : 
     746       72684 :     scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
     747             : 
     748       72684 :     ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
     749       72684 :     ac_val2 = ac_val;
     750       72684 :     if (dc_pred_dir) // left
     751       48137 :         ac_val -= 16;
     752             :     else // top
     753       24547 :         ac_val -= 16 * s->block_wrap[n];
     754             : 
     755       72684 :     q1 = s->current_picture.qscale_table[mb_pos];
     756       72684 :     if (n == 3)
     757       12114 :         q2 = q1;
     758       60570 :     else if (dc_pred_dir) {
     759       40639 :         if (n == 1)
     760        8215 :             q2 = q1;
     761       32424 :         else if (c_avail && mb_pos)
     762       32382 :             q2 = s->current_picture.qscale_table[mb_pos - 1];
     763             :     } else {
     764       19931 :         if (n == 2)
     765        4905 :             q2 = q1;
     766       15026 :         else if (a_avail && mb_pos >= s->mb_stride)
     767       15026 :             q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
     768             :     }
     769             : 
     770             :     //AC Decoding
     771       72684 :     i = 1;
     772             : 
     773       72684 :     if (coded) {
     774       65520 :         int last = 0, skip, value;
     775             :         const uint8_t *zz_table;
     776             :         int k;
     777             : 
     778       65520 :         if (v->s.ac_pred) {
     779       16184 :             if (!use_pred && v->fcm == ILACE_FRAME) {
     780           3 :                 zz_table = v->zzi_8x8;
     781             :             } else {
     782       16181 :                 if (!dc_pred_dir) // top
     783        5968 :                     zz_table = v->zz_8x8[2];
     784             :                 else // left
     785       10213 :                     zz_table = v->zz_8x8[3];
     786             :             }
     787             :         } else {
     788       49336 :             if (v->fcm != ILACE_FRAME)
     789       12456 :                 zz_table = v->zz_8x8[1];
     790             :             else
     791       36880 :                 zz_table = v->zzi_8x8;
     792             :         }
     793             : 
     794      911445 :         while (!last) {
     795      780405 :             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
     796      780405 :             i += skip;
     797      780405 :             if (i > 63)
     798           0 :                 break;
     799      780405 :             block[zz_table[i++]] = value;
     800             :         }
     801             : 
     802             :         /* apply AC prediction if needed */
     803       65520 :         if (use_pred) {
     804             :             int sh;
     805       16172 :             if (dc_pred_dir) { // left
     806       10204 :                 sh = v->left_blk_sh;
     807             :             } else { // top
     808        5968 :                 sh = v->top_blk_sh;
     809        5968 :                 ac_val += 8;
     810             :             }
     811             :             /* scale predictors if needed*/
     812       17189 :             if (q2 && q1 != q2) {
     813        1017 :                 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
     814        1017 :                 if (q1 < 1)
     815           0 :                     return AVERROR_INVALIDDATA;
     816        1017 :                 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
     817        8136 :                 for (k = 1; k < 8; k++)
     818        7119 :                     block[k << sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
     819             :             } else {
     820      121240 :                 for (k = 1; k < 8; k++)
     821      106085 :                     block[k << sh] += ac_val[k];
     822             :             }
     823             :         }
     824             :         /* save AC coeffs for further prediction */
     825      524160 :         for (k = 1; k < 8; k++) {
     826      458640 :             ac_val2[k    ] = block[k << v->left_blk_sh];
     827      458640 :             ac_val2[k + 8] = block[k << v->top_blk_sh];
     828             :         }
     829             : 
     830             :         /* scale AC coeffs */
     831     4193280 :         for (k = 1; k < 64; k++)
     832     4127760 :             if (block[k]) {
     833      779370 :                 block[k] *= scale;
     834      779370 :                 if (!v->pquantizer)
     835           0 :                     block[k] += (block[k] < 0) ? -mquant : mquant;
     836             :             }
     837             : 
     838             :     } else { // no AC coeffs
     839             :         int k;
     840             : 
     841        7164 :         memset(ac_val2, 0, 16 * 2);
     842             : 
     843             :         /* apply AC prediction if needed */
     844        7164 :         if (use_pred) {
     845             :             int sh;
     846        1708 :             if (dc_pred_dir) { // left
     847        1468 :                 sh = v->left_blk_sh;
     848             :             } else { // top
     849         240 :                 sh = v->top_blk_sh;
     850         240 :                 ac_val  += 8;
     851         240 :                 ac_val2 += 8;
     852             :             }
     853        1708 :             memcpy(ac_val2, ac_val, 8 * 2);
     854        1708 :             if (q2 && q1 != q2) {
     855         105 :                 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
     856         105 :                 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
     857         105 :                 if (q1 < 1)
     858           0 :                     return AVERROR_INVALIDDATA;
     859         840 :                 for (k = 1; k < 8; k++)
     860         735 :                     ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
     861             :             }
     862       13664 :             for (k = 1; k < 8; k++) {
     863       11956 :                 block[k << sh] = ac_val2[k] * scale;
     864       11956 :                 if (!v->pquantizer && block[k << sh])
     865           0 :                     block[k << sh] += (block[k << sh] < 0) ? -mquant : mquant;
     866             :             }
     867             :         }
     868             :     }
     869       72684 :     if (use_pred) i = 63;
     870       72684 :     s->block_last_index[n] = i;
     871             : 
     872       72684 :     return 0;
     873             : }
     874             : 
     875             : /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
     876             :  * @param v VC1Context
     877             :  * @param block block to decode
     878             :  * @param[in] n subblock index
     879             :  * @param coded are AC coeffs present or not
     880             :  * @param mquant block quantizer
     881             :  * @param codingset set of VLC to decode data
     882             :  */
     883      218346 : static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
     884             :                                   int coded, int mquant, int codingset)
     885             : {
     886      218346 :     GetBitContext *gb = &v->s.gb;
     887      218346 :     MpegEncContext *s = &v->s;
     888      218346 :     int dc_pred_dir = 0; /* Direction of the DC prediction used */
     889             :     int i;
     890      218346 :     int16_t *dc_val = NULL;
     891             :     int16_t *ac_val, *ac_val2;
     892             :     int dcdiff;
     893      218346 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
     894      218346 :     int a_avail = v->a_avail, c_avail = v->c_avail;
     895      218346 :     int use_pred = s->ac_pred;
     896             :     int scale;
     897      218346 :     int q1, q2 = 0;
     898             : 
     899      218346 :     s->bdsp.clear_block(block);
     900             : 
     901             :     /* XXX: Guard against dumb values of mquant */
     902      218346 :     mquant = av_clip_uintp2(mquant, 5);
     903             : 
     904             :     /* Set DC scale - y and c use the same */
     905      218346 :     s->y_dc_scale = s->y_dc_scale_table[mquant];
     906      218346 :     s->c_dc_scale = s->c_dc_scale_table[mquant];
     907             : 
     908             :     /* Get DC differential */
     909      218346 :     if (n < 4) {
     910      146782 :         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
     911             :     } else {
     912       71564 :         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
     913             :     }
     914      218346 :     if (dcdiff < 0) {
     915           0 :         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
     916           0 :         return -1;
     917             :     }
     918      218346 :     if (dcdiff) {
     919      175194 :         const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
     920      175194 :         if (dcdiff == 119 /* ESC index value */) {
     921           3 :             dcdiff = get_bits(gb, 8 + m);
     922             :         } else {
     923      175191 :             if (m)
     924       13134 :                 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
     925             :         }
     926      175194 :         if (get_bits1(gb))
     927       97897 :             dcdiff = -dcdiff;
     928             :     }
     929             : 
     930             :     /* Prediction */
     931      218346 :     dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
     932      218346 :     *dc_val = dcdiff;
     933             : 
     934             :     /* Store the quantized DC coeff, used for prediction */
     935             : 
     936      218346 :     if (n < 4) {
     937      146782 :         block[0] = dcdiff * s->y_dc_scale;
     938             :     } else {
     939       71564 :         block[0] = dcdiff * s->c_dc_scale;
     940             :     }
     941             : 
     942             :     //AC Decoding
     943      218346 :     i = 1;
     944             : 
     945             :     /* check if AC is needed at all and adjust direction if needed */
     946      218346 :     if (!a_avail) dc_pred_dir = 1;
     947      218346 :     if (!c_avail) dc_pred_dir = 0;
     948      218346 :     if (!a_avail && !c_avail) use_pred = 0;
     949      218346 :     ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
     950      218346 :     ac_val2 = ac_val;
     951             : 
     952      218346 :     scale = mquant * 2 + v->halfpq;
     953             : 
     954      218346 :     if (dc_pred_dir) //left
     955      105563 :         ac_val -= 16;
     956             :     else //top
     957      112783 :         ac_val -= 16 * s->block_wrap[n];
     958             : 
     959      218346 :     q1 = s->current_picture.qscale_table[mb_pos];
     960      218346 :     if (dc_pred_dir && c_avail && mb_pos)
     961      105484 :         q2 = s->current_picture.qscale_table[mb_pos - 1];
     962      218346 :     if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
     963       60883 :         q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
     964      218346 :     if (dc_pred_dir && n == 1)
     965       32225 :         q2 = q1;
     966      218346 :     if (!dc_pred_dir && n == 2)
     967       26380 :         q2 = q1;
     968      218346 :     if (n == 3) q2 = q1;
     969             : 
     970      218346 :     if (coded) {
     971      151727 :         int last = 0, skip, value;
     972             :         int k;
     973             : 
     974     1728052 :         while (!last) {
     975     1424598 :             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
     976     1424598 :             i += skip;
     977     1424598 :             if (i > 63)
     978           0 :                 break;
     979     1424598 :             if (v->fcm == PROGRESSIVE)
     980      830592 :                 block[v->zz_8x8[0][i++]] = value;
     981             :             else {
     982      594006 :                 if (use_pred && (v->fcm == ILACE_FRAME)) {
     983       38376 :                     if (!dc_pred_dir) // top
     984        8623 :                         block[v->zz_8x8[2][i++]] = value;
     985             :                     else // left
     986       10565 :                         block[v->zz_8x8[3][i++]] = value;
     987             :                 } else {
     988      574818 :                     block[v->zzi_8x8[i++]] = value;
     989             :                 }
     990             :             }
     991             :         }
     992             : 
     993             :         /* apply AC prediction if needed */
     994      151727 :         if (use_pred) {
     995             :             /* scale predictors if needed*/
     996       39179 :             if (q2 && q1 != q2) {
     997         354 :                 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
     998         354 :                 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
     999             : 
    1000         354 :                 if (q1 < 1)
    1001           0 :                     return AVERROR_INVALIDDATA;
    1002         708 :                 if (dc_pred_dir) { // left
    1003        1880 :                     for (k = 1; k < 8; k++)
    1004        1645 :                         block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
    1005             :                 } else { //top
    1006         952 :                     for (k = 1; k < 8; k++)
    1007         833 :                         block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
    1008             :                 }
    1009             :             } else {
    1010       38825 :                 if (dc_pred_dir) { // left
    1011      189552 :                     for (k = 1; k < 8; k++)
    1012      165858 :                         block[k << v->left_blk_sh] += ac_val[k];
    1013             :                 } else { // top
    1014      121048 :                     for (k = 1; k < 8; k++)
    1015      105917 :                         block[k << v->top_blk_sh] += ac_val[k + 8];
    1016             :                 }
    1017             :             }
    1018             :         }
    1019             :         /* save AC coeffs for further prediction */
    1020     1213816 :         for (k = 1; k < 8; k++) {
    1021     1062089 :             ac_val2[k    ] = block[k << v->left_blk_sh];
    1022     1062089 :             ac_val2[k + 8] = block[k << v->top_blk_sh];
    1023             :         }
    1024             : 
    1025             :         /* scale AC coeffs */
    1026     9710528 :         for (k = 1; k < 64; k++)
    1027     9558801 :             if (block[k]) {
    1028     1427682 :                 block[k] *= scale;
    1029     1427682 :                 if (!v->pquantizer)
    1030       15146 :                     block[k] += (block[k] < 0) ? -mquant : mquant;
    1031             :             }
    1032             : 
    1033      151727 :         if (use_pred) i = 63;
    1034             :     } else { // no AC coeffs
    1035             :         int k;
    1036             : 
    1037       66619 :         memset(ac_val2, 0, 16 * 2);
    1038       66619 :         if (dc_pred_dir) { // left
    1039       30949 :             if (use_pred) {
    1040        5969 :                 memcpy(ac_val2, ac_val, 8 * 2);
    1041        5969 :                 if (q2 && q1 != q2) {
    1042          32 :                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
    1043          32 :                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
    1044          32 :                     if (q1 < 1)
    1045           0 :                         return AVERROR_INVALIDDATA;
    1046         256 :                     for (k = 1; k < 8; k++)
    1047         224 :                         ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
    1048             :                 }
    1049             :             }
    1050             :         } else { // top
    1051       35670 :             if (use_pred) {
    1052        2151 :                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
    1053        2151 :                 if (q2 && q1 != q2) {
    1054          13 :                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
    1055          13 :                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
    1056          13 :                     if (q1 < 1)
    1057           0 :                         return AVERROR_INVALIDDATA;
    1058         104 :                     for (k = 1; k < 8; k++)
    1059          91 :                         ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
    1060             :                 }
    1061             :             }
    1062             :         }
    1063             : 
    1064             :         /* apply AC prediction if needed */
    1065       66619 :         if (use_pred) {
    1066        8120 :             if (dc_pred_dir) { // left
    1067       47752 :                 for (k = 1; k < 8; k++) {
    1068       41783 :                     block[k << v->left_blk_sh] = ac_val2[k] * scale;
    1069       41783 :                     if (!v->pquantizer && block[k << v->left_blk_sh])
    1070          65 :                         block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
    1071             :                 }
    1072             :             } else { // top
    1073       17208 :                 for (k = 1; k < 8; k++) {
    1074       15057 :                     block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
    1075       15057 :                     if (!v->pquantizer && block[k << v->top_blk_sh])
    1076           8 :                         block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
    1077             :                 }
    1078             :             }
    1079        8120 :             i = 63;
    1080             :         }
    1081             :     }
    1082      218346 :     s->block_last_index[n] = i;
    1083             : 
    1084      218346 :     return 0;
    1085             : }
    1086             : 
    1087             : /** Decode P block
    1088             :  */
    1089      511729 : static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
    1090             :                               int mquant, int ttmb, int first_block,
    1091             :                               uint8_t *dst, int linesize, int skip_block,
    1092             :                               int *ttmb_out)
    1093             : {
    1094      511729 :     MpegEncContext *s = &v->s;
    1095      511729 :     GetBitContext *gb = &s->gb;
    1096             :     int i, j;
    1097      511729 :     int subblkpat = 0;
    1098             :     int scale, off, idx, last, skip, value;
    1099      511729 :     int ttblk = ttmb & 7;
    1100      511729 :     int pat = 0;
    1101             : 
    1102      511729 :     s->bdsp.clear_block(block);
    1103             : 
    1104      511729 :     if (ttmb == -1) {
    1105      249651 :         ttblk = ff_vc1_ttblk_to_tt[v->tt_index][get_vlc2(gb, ff_vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
    1106             :     }
    1107      511729 :     if (ttblk == TT_4X4) {
    1108       64186 :         subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
    1109             :     }
    1110      511729 :     if ((ttblk != TT_8X8 && ttblk != TT_4X4)
    1111      217097 :         && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
    1112      187071 :             || (!v->res_rtm_flag && !first_block))) {
    1113       30026 :         subblkpat = decode012(gb);
    1114       30026 :         if (subblkpat)
    1115       10830 :             subblkpat ^= 3; // swap decoded pattern bits
    1116       30026 :         if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
    1117        5372 :             ttblk = TT_8X4;
    1118       30026 :         if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
    1119        3974 :             ttblk = TT_4X8;
    1120             :     }
    1121      511729 :     scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
    1122             : 
    1123             :     // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
    1124      511729 :     if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
    1125       31925 :         subblkpat = 2 - (ttblk == TT_8X4_TOP);
    1126       31925 :         ttblk     = TT_8X4;
    1127             :     }
    1128      511729 :     if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
    1129       38818 :         subblkpat = 2 - (ttblk == TT_4X8_LEFT);
    1130       38818 :         ttblk     = TT_4X8;
    1131             :     }
    1132      511729 :     switch (ttblk) {
    1133      230446 :     case TT_8X8:
    1134      230446 :         pat  = 0xF;
    1135      230446 :         i    = 0;
    1136      230446 :         last = 0;
    1137     1892399 :         while (!last) {
    1138     1431507 :             vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
    1139     1431507 :             i += skip;
    1140     1431507 :             if (i > 63)
    1141           0 :                 break;
    1142     1431507 :             if (!v->fcm)
    1143      291944 :                 idx = v->zz_8x8[0][i++];
    1144             :             else
    1145     1139563 :                 idx = v->zzi_8x8[i++];
    1146     1431507 :             block[idx] = value * scale;
    1147     1431507 :             if (!v->pquantizer)
    1148       14711 :                 block[idx] += (block[idx] < 0) ? -mquant : mquant;
    1149             :         }
    1150      230446 :         if (!skip_block) {
    1151      230446 :             if (i == 1)
    1152       33296 :                 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
    1153             :             else {
    1154      197150 :                 v->vc1dsp.vc1_inv_trans_8x8(block);
    1155      197150 :                 s->idsp.add_pixels_clamped(block, dst, linesize);
    1156             :             }
    1157             :         }
    1158      230446 :         break;
    1159       64186 :     case TT_4X4:
    1160       64186 :         pat = ~subblkpat & 0xF;
    1161      320930 :         for (j = 0; j < 4; j++) {
    1162      256744 :             last = subblkpat & (1 << (3 - j));
    1163      256744 :             i    = 0;
    1164      256744 :             off  = (j & 1) * 4 + (j & 2) * 16;
    1165      890550 :             while (!last) {
    1166      377062 :                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
    1167      377062 :                 i += skip;
    1168      377062 :                 if (i > 15)
    1169           0 :                     break;
    1170      377062 :                 if (!v->fcm)
    1171      153878 :                     idx = ff_vc1_simple_progressive_4x4_zz[i++];
    1172             :                 else
    1173      223184 :                     idx = ff_vc1_adv_interlaced_4x4_zz[i++];
    1174      377062 :                 block[idx + off] = value * scale;
    1175      377062 :                 if (!v->pquantizer)
    1176        9718 :                     block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
    1177             :             }
    1178      256744 :             if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
    1179      153664 :                 if (i == 1)
    1180       18929 :                     v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
    1181             :                 else
    1182      134735 :                     v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) *  2 * linesize, linesize, block + off);
    1183             :             }
    1184             :         }
    1185       64186 :         break;
    1186      105024 :     case TT_8X4:
    1187      105024 :         pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
    1188      315072 :         for (j = 0; j < 2; j++) {
    1189      210048 :             last = subblkpat & (1 << (1 - j));
    1190      210048 :             i    = 0;
    1191      210048 :             off  = j * 32;
    1192     1022200 :             while (!last) {
    1193      602104 :                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
    1194      602104 :                 i += skip;
    1195      602104 :                 if (i > 31)
    1196           0 :                     break;
    1197      602104 :                 if (!v->fcm)
    1198      176086 :                     idx = v->zz_8x4[i++] + off;
    1199             :                 else
    1200      426018 :                     idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
    1201      602104 :                 block[idx] = value * scale;
    1202      602104 :                 if (!v->pquantizer)
    1203        9538 :                     block[idx] += (block[idx] < 0) ? -mquant : mquant;
    1204             :             }
    1205      210048 :             if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
    1206      171732 :                 if (i == 1)
    1207       14081 :                     v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
    1208             :                 else
    1209      157651 :                     v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
    1210             :             }
    1211             :         }
    1212      105024 :         break;
    1213      112073 :     case TT_4X8:
    1214      112073 :         pat = ~(subblkpat * 5) & 0xF;
    1215      336219 :         for (j = 0; j < 2; j++) {
    1216      224146 :             last = subblkpat & (1 << (1 - j));
    1217      224146 :             i    = 0;
    1218      224146 :             off  = j * 4;
    1219     1082516 :             while (!last) {
    1220      634224 :                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
    1221      634224 :                 i += skip;
    1222      634224 :                 if (i > 31)
    1223           0 :                     break;
    1224      634224 :                 if (!v->fcm)
    1225      189544 :                     idx = v->zz_4x8[i++] + off;
    1226             :                 else
    1227      444680 :                     idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
    1228      634224 :                 block[idx] = value * scale;
    1229      634224 :                 if (!v->pquantizer)
    1230       10845 :                     block[idx] += (block[idx] < 0) ? -mquant : mquant;
    1231             :             }
    1232      224146 :             if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
    1233      180889 :                 if (i == 1)
    1234       16034 :                     v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
    1235             :                 else
    1236      164855 :                     v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
    1237             :             }
    1238             :         }
    1239      112073 :         break;
    1240             :     }
    1241      511729 :     if (ttmb_out)
    1242      414304 :         *ttmb_out |= ttblk << (n * 4);
    1243      511729 :     return pat;
    1244             : }
    1245             : 
    1246             : /** @} */ // Macroblock group
    1247             : 
    1248             : static const uint8_t size_table[6] = { 0, 2, 3, 4,  5,  8 };
    1249             : 
    1250             : /** Decode one P-frame MB
    1251             :  */
    1252       97504 : static int vc1_decode_p_mb(VC1Context *v)
    1253             : {
    1254       97504 :     MpegEncContext *s = &v->s;
    1255       97504 :     GetBitContext *gb = &s->gb;
    1256             :     int i, j;
    1257       97504 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    1258             :     int cbp; /* cbp decoding stuff */
    1259             :     int mqdiff, mquant; /* MB quantization */
    1260       97504 :     int ttmb = v->ttfrm; /* MB Transform type */
    1261             : 
    1262       97504 :     int mb_has_coeffs = 1; /* last_flag */
    1263             :     int dmv_x, dmv_y; /* Differential MV components */
    1264             :     int index, index1; /* LUT indexes */
    1265             :     int val, sign; /* temp values */
    1266       97504 :     int first_block = 1;
    1267             :     int dst_idx, off;
    1268             :     int skipped, fourmv;
    1269       97504 :     int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
    1270             : 
    1271       97504 :     mquant = v->pq; /* lossy initialization */
    1272             : 
    1273       97504 :     if (v->mv_type_is_raw)
    1274       58770 :         fourmv = get_bits1(gb);
    1275             :     else
    1276       38734 :         fourmv = v->mv_type_mb_plane[mb_pos];
    1277       97504 :     if (v->skip_is_raw)
    1278       58470 :         skipped = get_bits1(gb);
    1279             :     else
    1280       39034 :         skipped = v->s.mbskip_table[mb_pos];
    1281             : 
    1282       97504 :     if (!fourmv) { /* 1MV mode */
    1283       76537 :         if (!skipped) {
    1284       68850 :             GET_MVDATA(dmv_x, dmv_y);
    1285             : 
    1286       68850 :             if (s->mb_intra) {
    1287       22934 :                 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
    1288       22934 :                 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
    1289             :             }
    1290       68850 :             s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
    1291       68850 :             ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
    1292             : 
    1293             :             /* FIXME Set DC val for inter block ? */
    1294       68850 :             if (s->mb_intra && !mb_has_coeffs) {
    1295        2600 :                 GET_MQUANT();
    1296        2600 :                 s->ac_pred = get_bits1(gb);
    1297        2600 :                 cbp        = 0;
    1298       66250 :             } else if (mb_has_coeffs) {
    1299       54606 :                 if (s->mb_intra)
    1300       20334 :                     s->ac_pred = get_bits1(gb);
    1301       54606 :                 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    1302       54606 :                 GET_MQUANT();
    1303             :             } else {
    1304       11644 :                 mquant = v->pq;
    1305       11644 :                 cbp    = 0;
    1306             :             }
    1307       68850 :             s->current_picture.qscale_table[mb_pos] = mquant;
    1308             : 
    1309       68850 :             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
    1310       32859 :                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
    1311             :                                 VC1_TTMB_VLC_BITS, 2);
    1312       68850 :             if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
    1313       68850 :             dst_idx = 0;
    1314      481950 :             for (i = 0; i < 6; i++) {
    1315      413100 :                 s->dc_val[0][s->block_index[i]] = 0;
    1316      413100 :                 dst_idx += i >> 2;
    1317      413100 :                 val = ((cbp >> (5 - i)) & 1);
    1318      413100 :                 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
    1319      413100 :                 v->mb_type[0][s->block_index[i]] = s->mb_intra;
    1320      413100 :                 if (s->mb_intra) {
    1321             :                     /* check if prediction blocks A and C are available */
    1322      137604 :                     v->a_avail = v->c_avail = 0;
    1323      137604 :                     if (i == 2 || i == 3 || !s->first_slice_line)
    1324       98268 :                         v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
    1325      137604 :                     if (i == 1 || i == 3 || s->mb_x)
    1326      135012 :                         v->c_avail = v->mb_type[0][s->block_index[i] - 1];
    1327             : 
    1328      137604 :                     vc1_decode_intra_block(v, s->block[i], i, val, mquant,
    1329      137604 :                                            (i & 4) ? v->codingset2 : v->codingset);
    1330             :                     if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    1331             :                         continue;
    1332      137604 :                     v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
    1333      137604 :                     if (v->rangeredfrm)
    1334           0 :                         for (j = 0; j < 64; j++)
    1335           0 :                             s->block[i][j] <<= 1;
    1336      412812 :                     s->idsp.put_signed_pixels_clamped(s->block[i],
    1337      137604 :                                                       s->dest[dst_idx] + off,
    1338      137604 :                                                       i & 4 ? s->uvlinesize
    1339             :                                                             : s->linesize);
    1340      137604 :                     if (v->pq >= 9 && v->overlap) {
    1341           0 :                         if (v->c_avail)
    1342           0 :                             v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
    1343           0 :                         if (v->a_avail)
    1344           0 :                             v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
    1345             :                     }
    1346      137604 :                     block_cbp   |= 0xF << (i << 2);
    1347      137604 :                     block_intra |= 1 << i;
    1348      275496 :                 } else if (val) {
    1349      314442 :                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
    1350      314442 :                                              s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
    1351             :                                              CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
    1352      104814 :                     block_cbp |= pat << (i << 2);
    1353      104814 :                     if (!v->ttmbf && ttmb < 8)
    1354       57498 :                         ttmb = -1;
    1355      104814 :                     first_block = 0;
    1356             :                 }
    1357             :             }
    1358             :         } else { // skipped
    1359        7687 :             s->mb_intra = 0;
    1360       53809 :             for (i = 0; i < 6; i++) {
    1361       46122 :                 v->mb_type[0][s->block_index[i]] = 0;
    1362       46122 :                 s->dc_val[0][s->block_index[i]]  = 0;
    1363             :             }
    1364        7687 :             s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
    1365        7687 :             s->current_picture.qscale_table[mb_pos] = 0;
    1366        7687 :             ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
    1367        7687 :             ff_vc1_mc_1mv(v, 0);
    1368             :         }
    1369             :     } else { // 4MV mode
    1370       20967 :         if (!skipped /* unskipped MB */) {
    1371       17310 :             int intra_count = 0, coded_inter = 0;
    1372             :             int is_intra[6], is_coded[6];
    1373             :             /* Get CBPCY */
    1374       17310 :             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    1375      121170 :             for (i = 0; i < 6; i++) {
    1376      103860 :                 val = ((cbp >> (5 - i)) & 1);
    1377      103860 :                 s->dc_val[0][s->block_index[i]] = 0;
    1378      103860 :                 s->mb_intra                     = 0;
    1379      103860 :                 if (i < 4) {
    1380       69240 :                     dmv_x = dmv_y = 0;
    1381       69240 :                     s->mb_intra   = 0;
    1382       69240 :                     mb_has_coeffs = 0;
    1383       69240 :                     if (val) {
    1384       51084 :                         GET_MVDATA(dmv_x, dmv_y);
    1385             :                     }
    1386       69240 :                     ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
    1387       69240 :                     if (!s->mb_intra)
    1388       61486 :                         ff_vc1_mc_4mv_luma(v, i, 0, 0);
    1389       69240 :                     intra_count += s->mb_intra;
    1390       69240 :                     is_intra[i]  = s->mb_intra;
    1391       69240 :                     is_coded[i]  = mb_has_coeffs;
    1392             :                 }
    1393      103860 :                 if (i & 4) {
    1394       34620 :                     is_intra[i] = (intra_count >= 3);
    1395       34620 :                     is_coded[i] = val;
    1396             :                 }
    1397      103860 :                 if (i == 4)
    1398       17310 :                     ff_vc1_mc_4mv_chroma(v, 0);
    1399      103860 :                 v->mb_type[0][s->block_index[i]] = is_intra[i];
    1400      103860 :                 if (!coded_inter)
    1401       45169 :                     coded_inter = !is_intra[i] & is_coded[i];
    1402             :             }
    1403             :             // if there are no coded blocks then don't do anything more
    1404       17310 :             dst_idx = 0;
    1405       17310 :             if (!intra_count && !coded_inter)
    1406        2694 :                 goto end;
    1407       14616 :             GET_MQUANT();
    1408       14616 :             s->current_picture.qscale_table[mb_pos] = mquant;
    1409             :             /* test if block is intra and has pred */
    1410             :             {
    1411       14616 :                 int intrapred = 0;
    1412       88476 :                 for (i = 0; i < 6; i++)
    1413       76603 :                     if (is_intra[i]) {
    1414        4992 :                         if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
    1415        3796 :                             || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
    1416        2743 :                             intrapred = 1;
    1417        2743 :                             break;
    1418             :                         }
    1419             :                     }
    1420       14616 :                 if (intrapred)
    1421        2743 :                     s->ac_pred = get_bits1(gb);
    1422             :                 else
    1423       11873 :                     s->ac_pred = 0;
    1424             :             }
    1425       14616 :             if (!v->ttmbf && coded_inter)
    1426       12699 :                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
    1427      102312 :             for (i = 0; i < 6; i++) {
    1428       87696 :                 dst_idx    += i >> 2;
    1429       87696 :                 off         = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
    1430       87696 :                 s->mb_intra = is_intra[i];
    1431       87696 :                 if (is_intra[i]) {
    1432             :                     /* check if prediction blocks A and C are available */
    1433        9804 :                     v->a_avail = v->c_avail = 0;
    1434        9804 :                     if (i == 2 || i == 3 || !s->first_slice_line)
    1435        6920 :                         v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
    1436        9804 :                     if (i == 1 || i == 3 || s->mb_x)
    1437        9676 :                         v->c_avail = v->mb_type[0][s->block_index[i] - 1];
    1438             : 
    1439        9804 :                     vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
    1440        9804 :                                            (i & 4) ? v->codingset2 : v->codingset);
    1441             :                     if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    1442             :                         continue;
    1443        9804 :                     v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
    1444        9804 :                     if (v->rangeredfrm)
    1445           0 :                         for (j = 0; j < 64; j++)
    1446           0 :                             s->block[i][j] <<= 1;
    1447       29412 :                     s->idsp.put_signed_pixels_clamped(s->block[i],
    1448        9804 :                                                       s->dest[dst_idx] + off,
    1449        9804 :                                                       (i & 4) ? s->uvlinesize
    1450             :                                                               : s->linesize);
    1451        9804 :                     if (v->pq >= 9 && v->overlap) {
    1452           0 :                         if (v->c_avail)
    1453           0 :                             v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
    1454           0 :                         if (v->a_avail)
    1455           0 :                             v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
    1456             :                     }
    1457        9804 :                     block_cbp   |= 0xF << (i << 2);
    1458        9804 :                     block_intra |= 1 << i;
    1459       77892 :                 } else if (is_coded[i]) {
    1460      122322 :                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
    1461       40774 :                                              first_block, s->dest[dst_idx] + off,
    1462       81548 :                                              (i & 4) ? s->uvlinesize : s->linesize,
    1463             :                                              CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
    1464             :                                              &block_tt);
    1465       40774 :                     block_cbp |= pat << (i << 2);
    1466       40774 :                     if (!v->ttmbf && ttmb < 8)
    1467       18836 :                         ttmb = -1;
    1468       40774 :                     first_block = 0;
    1469             :                 }
    1470             :             }
    1471             :         } else { // skipped MB
    1472        3657 :             s->mb_intra                               = 0;
    1473        3657 :             s->current_picture.qscale_table[mb_pos] = 0;
    1474       25599 :             for (i = 0; i < 6; i++) {
    1475       21942 :                 v->mb_type[0][s->block_index[i]] = 0;
    1476       21942 :                 s->dc_val[0][s->block_index[i]]  = 0;
    1477             :             }
    1478       18285 :             for (i = 0; i < 4; i++) {
    1479       14628 :                 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
    1480       14628 :                 ff_vc1_mc_4mv_luma(v, i, 0, 0);
    1481             :             }
    1482        3657 :             ff_vc1_mc_4mv_chroma(v, 0);
    1483        3657 :             s->current_picture.qscale_table[mb_pos] = 0;
    1484             :         }
    1485             :     }
    1486       97504 : end:
    1487       97504 :     v->cbp[s->mb_x]      = block_cbp;
    1488       97504 :     v->ttblk[s->mb_x]    = block_tt;
    1489       97504 :     v->is_intra[s->mb_x] = block_intra;
    1490             : 
    1491       97504 :     return 0;
    1492             : }
    1493             : 
    1494             : /* Decode one macroblock in an interlaced frame p picture */
    1495             : 
    1496       16320 : static int vc1_decode_p_mb_intfr(VC1Context *v)
    1497             : {
    1498       16320 :     MpegEncContext *s = &v->s;
    1499       16320 :     GetBitContext *gb = &s->gb;
    1500             :     int i;
    1501       16320 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    1502       16320 :     int cbp = 0; /* cbp decoding stuff */
    1503             :     int mqdiff, mquant; /* MB quantization */
    1504       16320 :     int ttmb = v->ttfrm; /* MB Transform type */
    1505             : 
    1506       16320 :     int mb_has_coeffs = 1; /* last_flag */
    1507             :     int dmv_x, dmv_y; /* Differential MV components */
    1508             :     int val; /* temp value */
    1509       16320 :     int first_block = 1;
    1510             :     int dst_idx, off;
    1511       16320 :     int skipped, fourmv = 0, twomv = 0;
    1512       16320 :     int block_cbp = 0, pat, block_tt = 0;
    1513       16320 :     int idx_mbmode = 0, mvbp;
    1514             :     int stride_y, fieldtx;
    1515             : 
    1516       16320 :     mquant = v->pq; /* Lossy initialization */
    1517             : 
    1518       16320 :     if (v->skip_is_raw)
    1519           0 :         skipped = get_bits1(gb);
    1520             :     else
    1521       16320 :         skipped = v->s.mbskip_table[mb_pos];
    1522       16320 :     if (!skipped) {
    1523       16307 :         if (v->fourmvswitch)
    1524       16307 :             idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
    1525             :         else
    1526           0 :             idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
    1527       16307 :         switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
    1528             :         /* store the motion vector type in a flag (useful later) */
    1529         704 :         case MV_PMODE_INTFR_4MV:
    1530         704 :             fourmv = 1;
    1531         704 :             v->blk_mv_type[s->block_index[0]] = 0;
    1532         704 :             v->blk_mv_type[s->block_index[1]] = 0;
    1533         704 :             v->blk_mv_type[s->block_index[2]] = 0;
    1534         704 :             v->blk_mv_type[s->block_index[3]] = 0;
    1535         704 :             break;
    1536        1084 :         case MV_PMODE_INTFR_4MV_FIELD:
    1537        1084 :             fourmv = 1;
    1538        1084 :             v->blk_mv_type[s->block_index[0]] = 1;
    1539        1084 :             v->blk_mv_type[s->block_index[1]] = 1;
    1540        1084 :             v->blk_mv_type[s->block_index[2]] = 1;
    1541        1084 :             v->blk_mv_type[s->block_index[3]] = 1;
    1542        1084 :             break;
    1543        4797 :         case MV_PMODE_INTFR_2MV_FIELD:
    1544        4797 :             twomv = 1;
    1545        4797 :             v->blk_mv_type[s->block_index[0]] = 1;
    1546        4797 :             v->blk_mv_type[s->block_index[1]] = 1;
    1547        4797 :             v->blk_mv_type[s->block_index[2]] = 1;
    1548        4797 :             v->blk_mv_type[s->block_index[3]] = 1;
    1549        4797 :             break;
    1550        8046 :         case MV_PMODE_INTFR_1MV:
    1551        8046 :             v->blk_mv_type[s->block_index[0]] = 0;
    1552        8046 :             v->blk_mv_type[s->block_index[1]] = 0;
    1553        8046 :             v->blk_mv_type[s->block_index[2]] = 0;
    1554        8046 :             v->blk_mv_type[s->block_index[3]] = 0;
    1555        8046 :             break;
    1556             :         }
    1557       16307 :         if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
    1558        8380 :             for (i = 0; i < 4; i++) {
    1559        6704 :                 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
    1560        6704 :                 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
    1561             :             }
    1562        1676 :             v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
    1563        1676 :             s->mb_intra          = 1;
    1564        1676 :             s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
    1565        1676 :             fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
    1566        1676 :             mb_has_coeffs = get_bits1(gb);
    1567        1676 :             if (mb_has_coeffs)
    1568        1615 :                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    1569        1676 :             v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
    1570        1676 :             GET_MQUANT();
    1571        1676 :             s->current_picture.qscale_table[mb_pos] = mquant;
    1572             :             /* Set DC scale - y and c use the same (not sure if necessary here) */
    1573        1676 :             s->y_dc_scale = s->y_dc_scale_table[mquant];
    1574        1676 :             s->c_dc_scale = s->c_dc_scale_table[mquant];
    1575        1676 :             dst_idx = 0;
    1576       11732 :             for (i = 0; i < 6; i++) {
    1577       10056 :                 v->a_avail = v->c_avail          = 0;
    1578       10056 :                 v->mb_type[0][s->block_index[i]] = 1;
    1579       10056 :                 s->dc_val[0][s->block_index[i]]  = 0;
    1580       10056 :                 dst_idx += i >> 2;
    1581       10056 :                 val = ((cbp >> (5 - i)) & 1);
    1582       10056 :                 if (i == 2 || i == 3 || !s->first_slice_line)
    1583        9840 :                     v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
    1584       10056 :                 if (i == 1 || i == 3 || s->mb_x)
    1585       10000 :                     v->c_avail = v->mb_type[0][s->block_index[i] - 1];
    1586             : 
    1587       10056 :                 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
    1588       10056 :                                        (i & 4) ? v->codingset2 : v->codingset);
    1589             :                 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    1590             :                     continue;
    1591       10056 :                 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
    1592       10056 :                 if (i < 4) {
    1593        6704 :                     stride_y = s->linesize << fieldtx;
    1594        6704 :                     off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
    1595             :                 } else {
    1596        3352 :                     stride_y = s->uvlinesize;
    1597        3352 :                     off = 0;
    1598             :                 }
    1599       20112 :                 s->idsp.put_signed_pixels_clamped(s->block[i],
    1600       10056 :                                                   s->dest[dst_idx] + off,
    1601             :                                                   stride_y);
    1602             :                 //TODO: loop filter
    1603             :             }
    1604             : 
    1605             :         } else { // inter MB
    1606       14631 :             mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
    1607       14631 :             if (mb_has_coeffs)
    1608       13330 :                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    1609       14631 :             if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
    1610        4797 :                 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
    1611             :             } else {
    1612        9834 :                 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
    1613        9130 :                     || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
    1614        1788 :                     v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
    1615             :                 }
    1616             :             }
    1617       14631 :             s->mb_intra = v->is_intra[s->mb_x] = 0;
    1618      102417 :             for (i = 0; i < 6; i++)
    1619       87786 :                 v->mb_type[0][s->block_index[i]] = 0;
    1620       14631 :             fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
    1621             :             /* for all motion vector read MVDATA and motion compensate each block */
    1622       14631 :             dst_idx = 0;
    1623       14631 :             if (fourmv) {
    1624        1788 :                 mvbp = v->fourmvbp;
    1625        8940 :                 for (i = 0; i < 4; i++) {
    1626        7152 :                     dmv_x = dmv_y = 0;
    1627        7152 :                     if (mvbp & (8 >> i))
    1628        5798 :                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    1629        7152 :                     ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
    1630        7152 :                     ff_vc1_mc_4mv_luma(v, i, 0, 0);
    1631             :                 }
    1632        1788 :                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
    1633       12843 :             } else if (twomv) {
    1634        4797 :                 mvbp  = v->twomvbp;
    1635        4797 :                 dmv_x = dmv_y = 0;
    1636        4797 :                 if (mvbp & 2) {
    1637        4513 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    1638             :                 }
    1639        4797 :                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
    1640        4797 :                 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
    1641        4797 :                 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
    1642        4797 :                 dmv_x = dmv_y = 0;
    1643        4797 :                 if (mvbp & 1) {
    1644        4517 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    1645             :                 }
    1646        4797 :                 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
    1647        4797 :                 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
    1648        4797 :                 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
    1649        4797 :                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
    1650             :             } else {
    1651        8046 :                 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
    1652        8046 :                 dmv_x = dmv_y = 0;
    1653        8046 :                 if (mvbp) {
    1654        7892 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    1655             :                 }
    1656        8046 :                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
    1657        8046 :                 ff_vc1_mc_1mv(v, 0);
    1658             :             }
    1659       14631 :             if (cbp)
    1660       13330 :                 GET_MQUANT();  // p. 227
    1661       14631 :             s->current_picture.qscale_table[mb_pos] = mquant;
    1662       14631 :             if (!v->ttmbf && cbp)
    1663       13330 :                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
    1664      102417 :             for (i = 0; i < 6; i++) {
    1665       87786 :                 s->dc_val[0][s->block_index[i]] = 0;
    1666       87786 :                 dst_idx += i >> 2;
    1667       87786 :                 val = ((cbp >> (5 - i)) & 1);
    1668       87786 :                 if (!fieldtx)
    1669       72450 :                     off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
    1670             :                 else
    1671       15336 :                     off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
    1672       87786 :                 if (val) {
    1673      190881 :                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
    1674       63627 :                                              first_block, s->dest[dst_idx] + off,
    1675      127254 :                                              (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
    1676             :                                              CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
    1677       63627 :                     block_cbp |= pat << (i << 2);
    1678       63627 :                     if (!v->ttmbf && ttmb < 8)
    1679       44047 :                         ttmb = -1;
    1680       63627 :                     first_block = 0;
    1681             :                 }
    1682             :             }
    1683             :         }
    1684             :     } else { // skipped
    1685          13 :         s->mb_intra = v->is_intra[s->mb_x] = 0;
    1686          91 :         for (i = 0; i < 6; i++) {
    1687          78 :             v->mb_type[0][s->block_index[i]] = 0;
    1688          78 :             s->dc_val[0][s->block_index[i]] = 0;
    1689             :         }
    1690          13 :         s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
    1691          13 :         s->current_picture.qscale_table[mb_pos] = 0;
    1692          13 :         v->blk_mv_type[s->block_index[0]] = 0;
    1693          13 :         v->blk_mv_type[s->block_index[1]] = 0;
    1694          13 :         v->blk_mv_type[s->block_index[2]] = 0;
    1695          13 :         v->blk_mv_type[s->block_index[3]] = 0;
    1696          13 :         ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
    1697          13 :         ff_vc1_mc_1mv(v, 0);
    1698             :     }
    1699       16320 :     if (s->mb_x == s->mb_width - 1)
    1700         136 :         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
    1701       16320 :     return 0;
    1702             : }
    1703             : 
    1704       37245 : static int vc1_decode_p_mb_intfi(VC1Context *v)
    1705             : {
    1706       37245 :     MpegEncContext *s = &v->s;
    1707       37245 :     GetBitContext *gb = &s->gb;
    1708             :     int i;
    1709       37245 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    1710       37245 :     int cbp = 0; /* cbp decoding stuff */
    1711             :     int mqdiff, mquant; /* MB quantization */
    1712       37245 :     int ttmb = v->ttfrm; /* MB Transform type */
    1713             : 
    1714       37245 :     int mb_has_coeffs = 1; /* last_flag */
    1715             :     int dmv_x, dmv_y; /* Differential MV components */
    1716             :     int val; /* temp values */
    1717       37245 :     int first_block = 1;
    1718             :     int dst_idx, off;
    1719       37245 :     int pred_flag = 0;
    1720       37245 :     int block_cbp = 0, pat, block_tt = 0;
    1721       37245 :     int idx_mbmode = 0;
    1722             : 
    1723       37245 :     mquant = v->pq; /* Lossy initialization */
    1724             : 
    1725       37245 :     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
    1726       37245 :     if (idx_mbmode <= 1) { // intra MB
    1727        9990 :         v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
    1728        9990 :         s->mb_intra          = 1;
    1729        9990 :         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
    1730        9990 :         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
    1731        9990 :         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
    1732        9990 :         GET_MQUANT();
    1733        9990 :         s->current_picture.qscale_table[mb_pos] = mquant;
    1734             :         /* Set DC scale - y and c use the same (not sure if necessary here) */
    1735        9990 :         s->y_dc_scale = s->y_dc_scale_table[mquant];
    1736        9990 :         s->c_dc_scale = s->c_dc_scale_table[mquant];
    1737        9990 :         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
    1738        9990 :         mb_has_coeffs = idx_mbmode & 1;
    1739        9990 :         if (mb_has_coeffs)
    1740        9243 :             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
    1741        9990 :         dst_idx = 0;
    1742       69930 :         for (i = 0; i < 6; i++) {
    1743       59940 :             v->a_avail = v->c_avail          = 0;
    1744       59940 :             v->mb_type[0][s->block_index[i]] = 1;
    1745       59940 :             s->dc_val[0][s->block_index[i]]  = 0;
    1746       59940 :             dst_idx += i >> 2;
    1747       59940 :             val = ((cbp >> (5 - i)) & 1);
    1748       59940 :             if (i == 2 || i == 3 || !s->first_slice_line)
    1749       57452 :                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
    1750       59940 :             if (i == 1 || i == 3 || s->mb_x)
    1751       59200 :                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
    1752             : 
    1753       59940 :             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
    1754       59940 :                                    (i & 4) ? v->codingset2 : v->codingset);
    1755             :             if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    1756             :                 continue;
    1757       59940 :             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
    1758       59940 :             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
    1759      179820 :             s->idsp.put_signed_pixels_clamped(s->block[i],
    1760       59940 :                                               s->dest[dst_idx] + off,
    1761       59940 :                                               (i & 4) ? s->uvlinesize
    1762             :                                                       : s->linesize);
    1763             :             // TODO: loop filter
    1764             :         }
    1765             :     } else {
    1766       27255 :         s->mb_intra = v->is_intra[s->mb_x] = 0;
    1767       27255 :         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
    1768      190785 :         for (i = 0; i < 6; i++)
    1769      163530 :             v->mb_type[0][s->block_index[i]] = 0;
    1770       27255 :         if (idx_mbmode <= 5) { // 1-MV
    1771       20405 :             dmv_x = dmv_y = pred_flag = 0;
    1772       20405 :             if (idx_mbmode & 1) {
    1773       15790 :                 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
    1774             :             }
    1775       20405 :             ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
    1776       20405 :             ff_vc1_mc_1mv(v, 0);
    1777       20405 :             mb_has_coeffs = !(idx_mbmode & 2);
    1778             :         } else { // 4-MV
    1779        6850 :             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
    1780       34250 :             for (i = 0; i < 4; i++) {
    1781       27400 :                 dmv_x = dmv_y = pred_flag = 0;
    1782       27400 :                 if (v->fourmvbp & (8 >> i))
    1783        8342 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
    1784       27400 :                 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
    1785       27400 :                 ff_vc1_mc_4mv_luma(v, i, 0, 0);
    1786             :             }
    1787        6850 :             ff_vc1_mc_4mv_chroma(v, 0);
    1788        6850 :             mb_has_coeffs = idx_mbmode & 1;
    1789             :         }
    1790       27255 :         if (mb_has_coeffs)
    1791       20418 :             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    1792       27255 :         if (cbp) {
    1793       20418 :             GET_MQUANT();
    1794             :         }
    1795       27255 :         s->current_picture.qscale_table[mb_pos] = mquant;
    1796       27255 :         if (!v->ttmbf && cbp) {
    1797       20418 :             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
    1798             :         }
    1799       27255 :         dst_idx = 0;
    1800      190785 :         for (i = 0; i < 6; i++) {
    1801      163530 :             s->dc_val[0][s->block_index[i]] = 0;
    1802      163530 :             dst_idx += i >> 2;
    1803      163530 :             val = ((cbp >> (5 - i)) & 1);
    1804      163530 :             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
    1805      163530 :             if (val) {
    1806      268404 :                 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
    1807       89468 :                                          first_block, s->dest[dst_idx] + off,
    1808      178936 :                                          (i & 4) ? s->uvlinesize : s->linesize,
    1809             :                                          CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
    1810             :                                          &block_tt);
    1811       89468 :                 block_cbp |= pat << (i << 2);
    1812       89468 :                 if (!v->ttmbf && ttmb < 8)
    1813       58173 :                     ttmb = -1;
    1814       89468 :                 first_block = 0;
    1815             :             }
    1816             :         }
    1817             :     }
    1818       37245 :     if (s->mb_x == s->mb_width - 1)
    1819         601 :         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
    1820       37245 :     return 0;
    1821             : }
    1822             : 
    1823             : /** Decode one B-frame MB (in Main profile)
    1824             :  */
    1825        6090 : static void vc1_decode_b_mb(VC1Context *v)
    1826             : {
    1827        6090 :     MpegEncContext *s = &v->s;
    1828        6090 :     GetBitContext *gb = &s->gb;
    1829             :     int i, j;
    1830        6090 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    1831        6090 :     int cbp = 0; /* cbp decoding stuff */
    1832             :     int mqdiff, mquant; /* MB quantization */
    1833        6090 :     int ttmb = v->ttfrm; /* MB Transform type */
    1834        6090 :     int mb_has_coeffs = 0; /* last_flag */
    1835             :     int index, index1; /* LUT indexes */
    1836             :     int val, sign; /* temp values */
    1837        6090 :     int first_block = 1;
    1838             :     int dst_idx, off;
    1839             :     int skipped, direct;
    1840             :     int dmv_x[2], dmv_y[2];
    1841        6090 :     int bmvtype = BMV_TYPE_BACKWARD;
    1842             : 
    1843        6090 :     mquant      = v->pq; /* lossy initialization */
    1844        6090 :     s->mb_intra = 0;
    1845             : 
    1846        6090 :     if (v->dmb_is_raw)
    1847           0 :         direct = get_bits1(gb);
    1848             :     else
    1849        6090 :         direct = v->direct_mb_plane[mb_pos];
    1850        6090 :     if (v->skip_is_raw)
    1851           0 :         skipped = get_bits1(gb);
    1852             :     else
    1853        6090 :         skipped = v->s.mbskip_table[mb_pos];
    1854             : 
    1855        6090 :     dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
    1856       42630 :     for (i = 0; i < 6; i++) {
    1857       36540 :         v->mb_type[0][s->block_index[i]] = 0;
    1858       36540 :         s->dc_val[0][s->block_index[i]]  = 0;
    1859             :     }
    1860        6090 :     s->current_picture.qscale_table[mb_pos] = 0;
    1861             : 
    1862        6090 :     if (!direct) {
    1863        4493 :         if (!skipped) {
    1864        2996 :             GET_MVDATA(dmv_x[0], dmv_y[0]);
    1865        2996 :             dmv_x[1] = dmv_x[0];
    1866        2996 :             dmv_y[1] = dmv_y[0];
    1867             :         }
    1868        4493 :         if (skipped || !s->mb_intra) {
    1869        4485 :             bmvtype = decode012(gb);
    1870        4485 :             switch (bmvtype) {
    1871        1818 :             case 0:
    1872        1818 :                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
    1873        1818 :                 break;
    1874         704 :             case 1:
    1875         704 :                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
    1876         704 :                 break;
    1877        1963 :             case 2:
    1878        1963 :                 bmvtype  = BMV_TYPE_INTERPOLATED;
    1879        1963 :                 dmv_x[0] = dmv_y[0] = 0;
    1880             :             }
    1881             :         }
    1882             :     }
    1883       42630 :     for (i = 0; i < 6; i++)
    1884       36540 :         v->mb_type[0][s->block_index[i]] = s->mb_intra;
    1885             : 
    1886        6090 :     if (skipped) {
    1887        2459 :         if (direct)
    1888         962 :             bmvtype = BMV_TYPE_INTERPOLATED;
    1889        2459 :         ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
    1890        2459 :         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
    1891        2459 :         return;
    1892             :     }
    1893        3631 :     if (direct) {
    1894         635 :         cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    1895         635 :         GET_MQUANT();
    1896         635 :         s->mb_intra = 0;
    1897         635 :         s->current_picture.qscale_table[mb_pos] = mquant;
    1898         635 :         if (!v->ttmbf)
    1899         635 :             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
    1900         635 :         dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
    1901         635 :         ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
    1902         635 :         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
    1903             :     } else {
    1904        2996 :         if (!mb_has_coeffs && !s->mb_intra) {
    1905             :             /* no coded blocks - effectively skipped */
    1906         789 :             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
    1907         789 :             vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
    1908         789 :             return;
    1909             :         }
    1910        2207 :         if (s->mb_intra && !mb_has_coeffs) {
    1911           0 :             GET_MQUANT();
    1912           0 :             s->current_picture.qscale_table[mb_pos] = mquant;
    1913           0 :             s->ac_pred = get_bits1(gb);
    1914           0 :             cbp = 0;
    1915           0 :             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
    1916             :         } else {
    1917        2207 :             if (bmvtype == BMV_TYPE_INTERPOLATED) {
    1918        1350 :                 GET_MVDATA(dmv_x[0], dmv_y[0]);
    1919        1350 :                 if (!mb_has_coeffs) {
    1920             :                     /* interpolated skipped block */
    1921         440 :                     ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
    1922         440 :                     vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
    1923         440 :                     return;
    1924             :                 }
    1925             :             }
    1926        1767 :             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
    1927        1767 :             if (!s->mb_intra) {
    1928        1759 :                 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
    1929             :             }
    1930        1767 :             if (s->mb_intra)
    1931           8 :                 s->ac_pred = get_bits1(gb);
    1932        1767 :             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    1933        1767 :             GET_MQUANT();
    1934        1767 :             s->current_picture.qscale_table[mb_pos] = mquant;
    1935        1767 :             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
    1936        1759 :                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
    1937             :         }
    1938             :     }
    1939        2402 :     dst_idx = 0;
    1940       16814 :     for (i = 0; i < 6; i++) {
    1941       14412 :         s->dc_val[0][s->block_index[i]] = 0;
    1942       14412 :         dst_idx += i >> 2;
    1943       14412 :         val = ((cbp >> (5 - i)) & 1);
    1944       14412 :         off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
    1945       14412 :         v->mb_type[0][s->block_index[i]] = s->mb_intra;
    1946       14412 :         if (s->mb_intra) {
    1947             :             /* check if prediction blocks A and C are available */
    1948          48 :             v->a_avail = v->c_avail = 0;
    1949          48 :             if (i == 2 || i == 3 || !s->first_slice_line)
    1950          48 :                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
    1951          48 :             if (i == 1 || i == 3 || s->mb_x)
    1952          44 :                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
    1953             : 
    1954          48 :             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
    1955          48 :                                    (i & 4) ? v->codingset2 : v->codingset);
    1956             :             if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    1957             :                 continue;
    1958          48 :             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
    1959          48 :             if (v->rangeredfrm)
    1960           0 :                 for (j = 0; j < 64; j++)
    1961           0 :                     s->block[i][j] <<= 1;
    1962         144 :             s->idsp.put_signed_pixels_clamped(s->block[i],
    1963          48 :                                               s->dest[dst_idx] + off,
    1964          48 :                                               i & 4 ? s->uvlinesize
    1965             :                                                     : s->linesize);
    1966       14364 :         } else if (val) {
    1967       17865 :             vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
    1968        5955 :                                first_block, s->dest[dst_idx] + off,
    1969       11910 :                                (i & 4) ? s->uvlinesize : s->linesize,
    1970             :                                CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
    1971        5955 :             if (!v->ttmbf && ttmb < 8)
    1972        4525 :                 ttmb = -1;
    1973        5955 :             first_block = 0;
    1974             :         }
    1975             :     }
    1976             : }
    1977             : 
    1978             : /** Decode one B-frame MB (in interlaced field B picture)
    1979             :  */
    1980       51540 : static void vc1_decode_b_mb_intfi(VC1Context *v)
    1981             : {
    1982       51540 :     MpegEncContext *s = &v->s;
    1983       51540 :     GetBitContext *gb = &s->gb;
    1984             :     int i, j;
    1985       51540 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    1986       51540 :     int cbp = 0; /* cbp decoding stuff */
    1987             :     int mqdiff, mquant; /* MB quantization */
    1988       51540 :     int ttmb = v->ttfrm; /* MB Transform type */
    1989       51540 :     int mb_has_coeffs = 0; /* last_flag */
    1990             :     int val; /* temp value */
    1991       51540 :     int first_block = 1;
    1992             :     int dst_idx, off;
    1993             :     int fwd;
    1994             :     int dmv_x[2], dmv_y[2], pred_flag[2];
    1995       51540 :     int bmvtype = BMV_TYPE_BACKWARD;
    1996             :     int idx_mbmode;
    1997             : 
    1998       51540 :     mquant      = v->pq; /* Lossy initialization */
    1999       51540 :     s->mb_intra = 0;
    2000             : 
    2001       51540 :     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
    2002       51540 :     if (idx_mbmode <= 1) { // intra MB
    2003          48 :         v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
    2004          48 :         s->mb_intra          = 1;
    2005          48 :         s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
    2006          48 :         s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
    2007          48 :         s->current_picture.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
    2008          48 :         GET_MQUANT();
    2009          48 :         s->current_picture.qscale_table[mb_pos] = mquant;
    2010             :         /* Set DC scale - y and c use the same (not sure if necessary here) */
    2011          48 :         s->y_dc_scale = s->y_dc_scale_table[mquant];
    2012          48 :         s->c_dc_scale = s->c_dc_scale_table[mquant];
    2013          48 :         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
    2014          48 :         mb_has_coeffs = idx_mbmode & 1;
    2015          48 :         if (mb_has_coeffs)
    2016          33 :             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
    2017          48 :         dst_idx = 0;
    2018         336 :         for (i = 0; i < 6; i++) {
    2019         288 :             v->a_avail = v->c_avail          = 0;
    2020         288 :             v->mb_type[0][s->block_index[i]] = 1;
    2021         288 :             s->dc_val[0][s->block_index[i]]  = 0;
    2022         288 :             dst_idx += i >> 2;
    2023         288 :             val = ((cbp >> (5 - i)) & 1);
    2024         288 :             if (i == 2 || i == 3 || !s->first_slice_line)
    2025         288 :                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
    2026         288 :             if (i == 1 || i == 3 || s->mb_x)
    2027         284 :                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
    2028             : 
    2029         288 :             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
    2030         288 :                                    (i & 4) ? v->codingset2 : v->codingset);
    2031             :             if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    2032             :                 continue;
    2033         288 :             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
    2034         288 :             if (v->rangeredfrm)
    2035           0 :                 for (j = 0; j < 64; j++)
    2036           0 :                     s->block[i][j] <<= 1;
    2037         288 :             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
    2038         864 :             s->idsp.put_signed_pixels_clamped(s->block[i],
    2039         288 :                                               s->dest[dst_idx] + off,
    2040         288 :                                               (i & 4) ? s->uvlinesize
    2041             :                                                       : s->linesize);
    2042             :             // TODO: yet to perform loop filter
    2043             :         }
    2044             :     } else {
    2045       51492 :         s->mb_intra = v->is_intra[s->mb_x] = 0;
    2046       51492 :         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
    2047      360444 :         for (i = 0; i < 6; i++)
    2048      308952 :             v->mb_type[0][s->block_index[i]] = 0;
    2049       51492 :         if (v->fmb_is_raw)
    2050         675 :             fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
    2051             :         else
    2052       50817 :             fwd = v->forward_mb_plane[mb_pos];
    2053       51492 :         if (idx_mbmode <= 5) { // 1-MV
    2054       42084 :             int interpmvp = 0;
    2055       42084 :             dmv_x[0]     = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
    2056       42084 :             pred_flag[0] = pred_flag[1] = 0;
    2057       42084 :             if (fwd)
    2058       15503 :                 bmvtype = BMV_TYPE_FORWARD;
    2059             :             else {
    2060       26581 :                 bmvtype = decode012(gb);
    2061       26581 :                 switch (bmvtype) {
    2062       13925 :                 case 0:
    2063       13925 :                     bmvtype = BMV_TYPE_BACKWARD;
    2064       13925 :                     break;
    2065        6770 :                 case 1:
    2066        6770 :                     bmvtype = BMV_TYPE_DIRECT;
    2067        6770 :                     break;
    2068        5886 :                 case 2:
    2069        5886 :                     bmvtype   = BMV_TYPE_INTERPOLATED;
    2070        5886 :                     interpmvp = get_bits1(gb);
    2071             :                 }
    2072             :             }
    2073       42084 :             v->bmvtype = bmvtype;
    2074       42084 :             if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
    2075       20242 :                 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
    2076             :             }
    2077       42084 :             if (interpmvp) {
    2078        4685 :                 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
    2079             :             }
    2080       42084 :             if (bmvtype == BMV_TYPE_DIRECT) {
    2081        6770 :                 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
    2082        6770 :                 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
    2083        6770 :                 if (!s->next_picture_ptr->field_picture) {
    2084           0 :                     av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
    2085           0 :                     return;
    2086             :                 }
    2087             :             }
    2088       42084 :             ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
    2089       42084 :             vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
    2090       42084 :             mb_has_coeffs = !(idx_mbmode & 2);
    2091             :         } else { // 4-MV
    2092        9408 :             if (fwd)
    2093        4892 :                 bmvtype = BMV_TYPE_FORWARD;
    2094        9408 :             v->bmvtype  = bmvtype;
    2095        9408 :             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
    2096       47040 :             for (i = 0; i < 4; i++) {
    2097       37632 :                 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
    2098       37632 :                 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
    2099       37632 :                 if (v->fourmvbp & (8 >> i)) {
    2100       20100 :                     get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
    2101       10050 :                                              &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
    2102       10050 :                                          &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
    2103             :                 }
    2104       37632 :                 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
    2105       37632 :                 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
    2106             :             }
    2107        9408 :             ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
    2108        9408 :             mb_has_coeffs = idx_mbmode & 1;
    2109             :         }
    2110       51492 :         if (mb_has_coeffs)
    2111       30972 :             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    2112       51492 :         if (cbp) {
    2113       30972 :             GET_MQUANT();
    2114             :         }
    2115       51492 :         s->current_picture.qscale_table[mb_pos] = mquant;
    2116       51492 :         if (!v->ttmbf && cbp) {
    2117       30972 :             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
    2118             :         }
    2119       51492 :         dst_idx = 0;
    2120      360444 :         for (i = 0; i < 6; i++) {
    2121      308952 :             s->dc_val[0][s->block_index[i]] = 0;
    2122      308952 :             dst_idx += i >> 2;
    2123      308952 :             val = ((cbp >> (5 - i)) & 1);
    2124      308952 :             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
    2125      308952 :             if (val) {
    2126      274410 :                 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
    2127       91470 :                                    first_block, s->dest[dst_idx] + off,
    2128      182940 :                                    (i & 4) ? s->uvlinesize : s->linesize,
    2129             :                                    CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
    2130       91470 :                 if (!v->ttmbf && ttmb < 8)
    2131       69969 :                     ttmb = -1;
    2132       91470 :                 first_block = 0;
    2133             :             }
    2134             :         }
    2135             :     }
    2136             : }
    2137             : 
    2138             : /** Decode one B-frame MB (in interlaced frame B picture)
    2139             :  */
    2140       32640 : static int vc1_decode_b_mb_intfr(VC1Context *v)
    2141             : {
    2142       32640 :     MpegEncContext *s = &v->s;
    2143       32640 :     GetBitContext *gb = &s->gb;
    2144             :     int i, j;
    2145       32640 :     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    2146       32640 :     int cbp = 0; /* cbp decoding stuff */
    2147             :     int mqdiff, mquant; /* MB quantization */
    2148       32640 :     int ttmb = v->ttfrm; /* MB Transform type */
    2149       32640 :     int mvsw = 0; /* motion vector switch */
    2150       32640 :     int mb_has_coeffs = 1; /* last_flag */
    2151             :     int dmv_x, dmv_y; /* Differential MV components */
    2152             :     int val; /* temp value */
    2153       32640 :     int first_block = 1;
    2154             :     int dst_idx, off;
    2155       32640 :     int skipped, direct, twomv = 0;
    2156       32640 :     int block_cbp = 0, pat, block_tt = 0;
    2157       32640 :     int idx_mbmode = 0, mvbp;
    2158             :     int stride_y, fieldtx;
    2159       32640 :     int bmvtype = BMV_TYPE_BACKWARD;
    2160             :     int dir, dir2;
    2161             : 
    2162       32640 :     mquant = v->pq; /* Lossy initialization */
    2163       32640 :     s->mb_intra = 0;
    2164       32640 :     if (v->skip_is_raw)
    2165           0 :         skipped = get_bits1(gb);
    2166             :     else
    2167       32640 :         skipped = v->s.mbskip_table[mb_pos];
    2168             : 
    2169       32640 :     if (!skipped) {
    2170       31098 :         idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
    2171       31098 :         if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
    2172       17446 :             twomv = 1;
    2173       17446 :             v->blk_mv_type[s->block_index[0]] = 1;
    2174       17446 :             v->blk_mv_type[s->block_index[1]] = 1;
    2175       17446 :             v->blk_mv_type[s->block_index[2]] = 1;
    2176       17446 :             v->blk_mv_type[s->block_index[3]] = 1;
    2177             :         } else {
    2178       13652 :             v->blk_mv_type[s->block_index[0]] = 0;
    2179       13652 :             v->blk_mv_type[s->block_index[1]] = 0;
    2180       13652 :             v->blk_mv_type[s->block_index[2]] = 0;
    2181       13652 :             v->blk_mv_type[s->block_index[3]] = 0;
    2182             :         }
    2183             :     }
    2184             : 
    2185       32640 :     if (v->dmb_is_raw)
    2186           0 :         direct = get_bits1(gb);
    2187             :     else
    2188       32640 :         direct = v->direct_mb_plane[mb_pos];
    2189             : 
    2190       32640 :     if (direct) {
    2191        8474 :         if (s->next_picture_ptr->field_picture)
    2192           0 :             av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
    2193        8474 :         s->mv[0][0][0] = s->current_picture.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
    2194        8474 :         s->mv[0][0][1] = s->current_picture.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
    2195        8474 :         s->mv[1][0][0] = s->current_picture.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
    2196        8474 :         s->mv[1][0][1] = s->current_picture.motion_val[1][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 1, s->quarter_sample);
    2197             : 
    2198        8474 :         if (twomv) {
    2199        4360 :             s->mv[0][2][0] = s->current_picture.motion_val[0][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 0, s->quarter_sample);
    2200        4360 :             s->mv[0][2][1] = s->current_picture.motion_val[0][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 0, s->quarter_sample);
    2201        4360 :             s->mv[1][2][0] = s->current_picture.motion_val[1][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 1, s->quarter_sample);
    2202        4360 :             s->mv[1][2][1] = s->current_picture.motion_val[1][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 1, s->quarter_sample);
    2203             : 
    2204       13080 :             for (i = 1; i < 4; i += 2) {
    2205        8720 :                 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
    2206        8720 :                 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
    2207        8720 :                 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
    2208        8720 :                 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
    2209             :             }
    2210             :         } else {
    2211       16456 :             for (i = 1; i < 4; i++) {
    2212       12342 :                 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
    2213       12342 :                 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
    2214       12342 :                 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
    2215       12342 :                 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
    2216             :             }
    2217             :         }
    2218             :     }
    2219             : 
    2220       32640 :     if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
    2221         505 :         for (i = 0; i < 4; i++) {
    2222         404 :             s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
    2223         404 :             s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
    2224         404 :             s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
    2225         404 :             s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
    2226             :         }
    2227         101 :         v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
    2228         101 :         s->mb_intra          = 1;
    2229         101 :         s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
    2230         101 :         fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
    2231         101 :         mb_has_coeffs = get_bits1(gb);
    2232         101 :         if (mb_has_coeffs)
    2233         101 :             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    2234         101 :         v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
    2235         101 :         GET_MQUANT();
    2236         101 :         s->current_picture.qscale_table[mb_pos] = mquant;
    2237             :         /* Set DC scale - y and c use the same (not sure if necessary here) */
    2238         101 :         s->y_dc_scale = s->y_dc_scale_table[mquant];
    2239         101 :         s->c_dc_scale = s->c_dc_scale_table[mquant];
    2240         101 :         dst_idx = 0;
    2241         707 :         for (i = 0; i < 6; i++) {
    2242         606 :             v->a_avail = v->c_avail          = 0;
    2243         606 :             v->mb_type[0][s->block_index[i]] = 1;
    2244         606 :             s->dc_val[0][s->block_index[i]]  = 0;
    2245         606 :             dst_idx += i >> 2;
    2246         606 :             val = ((cbp >> (5 - i)) & 1);
    2247         606 :             if (i == 2 || i == 3 || !s->first_slice_line)
    2248         574 :                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
    2249         606 :             if (i == 1 || i == 3 || s->mb_x)
    2250         594 :                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
    2251             : 
    2252         606 :             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
    2253         606 :                                    (i & 4) ? v->codingset2 : v->codingset);
    2254             :             if (CONFIG_GRAY && i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    2255             :                 continue;
    2256         606 :             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
    2257         606 :             if (i < 4) {
    2258         404 :                 stride_y = s->linesize << fieldtx;
    2259         404 :                 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
    2260             :             } else {
    2261         202 :                 stride_y = s->uvlinesize;
    2262         202 :                 off = 0;
    2263             :             }
    2264        1212 :             s->idsp.put_signed_pixels_clamped(s->block[i],
    2265         606 :                                               s->dest[dst_idx] + off,
    2266             :                                               stride_y);
    2267             :         }
    2268             :     } else {
    2269       32539 :         s->mb_intra = v->is_intra[s->mb_x] = 0;
    2270       32539 :         if (!direct) {
    2271       24065 :             if (skipped || !s->mb_intra) {
    2272       24065 :                 bmvtype = decode012(gb);
    2273       24065 :                 switch (bmvtype) {
    2274       14341 :                 case 0:
    2275       14341 :                     bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
    2276       14341 :                     break;
    2277        6232 :                 case 1:
    2278        6232 :                     bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
    2279        6232 :                     break;
    2280        3492 :                 case 2:
    2281        3492 :                     bmvtype  = BMV_TYPE_INTERPOLATED;
    2282             :                 }
    2283             :             }
    2284             : 
    2285       24065 :             if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
    2286       12364 :                 mvsw = get_bits1(gb);
    2287             :         }
    2288             : 
    2289       32539 :         if (!skipped) { // inter MB
    2290       30997 :             mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
    2291       30997 :             if (mb_has_coeffs)
    2292       26859 :                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
    2293       30997 :             if (!direct) {
    2294       23850 :                 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
    2295         722 :                     v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
    2296       23128 :                 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
    2297       15125 :                     v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
    2298             :                 }
    2299             :             }
    2300             : 
    2301      216979 :             for (i = 0; i < 6; i++)
    2302      185982 :                 v->mb_type[0][s->block_index[i]] = 0;
    2303       30997 :             fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
    2304             :             /* for all motion vector read MVDATA and motion compensate each block */
    2305       30997 :             dst_idx = 0;
    2306       30997 :             if (direct) {
    2307        7147 :                 if (twomv) {
    2308       21800 :                     for (i = 0; i < 4; i++) {
    2309       17440 :                         ff_vc1_mc_4mv_luma(v, i, 0, 0);
    2310       17440 :                         ff_vc1_mc_4mv_luma(v, i, 1, 1);
    2311             :                     }
    2312        4360 :                     ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
    2313        4360 :                     ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
    2314             :                 } else {
    2315        2787 :                     ff_vc1_mc_1mv(v, 0);
    2316        2787 :                     ff_vc1_interp_mc(v);
    2317             :                 }
    2318       23850 :             } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
    2319         722 :                 mvbp = v->fourmvbp;
    2320        3610 :                 for (i = 0; i < 4; i++) {
    2321        2888 :                     dir = i==1 || i==3;
    2322        2888 :                     dmv_x = dmv_y = 0;
    2323        2888 :                     val = ((mvbp >> (3 - i)) & 1);
    2324        2888 :                     if (val)
    2325        2477 :                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    2326        2888 :                     j = i > 1 ? 2 : 0;
    2327        2888 :                     ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
    2328        2888 :                     ff_vc1_mc_4mv_luma(v, j, dir, dir);
    2329        2888 :                     ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
    2330             :                 }
    2331             : 
    2332         722 :                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
    2333         722 :                 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
    2334       23128 :             } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
    2335        2761 :                 mvbp = v->twomvbp;
    2336        2761 :                 dmv_x = dmv_y = 0;
    2337        2761 :                 if (mvbp & 2)
    2338        2691 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    2339             : 
    2340        2761 :                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
    2341        2761 :                 ff_vc1_mc_1mv(v, 0);
    2342             : 
    2343        2761 :                 dmv_x = dmv_y = 0;
    2344        2761 :                 if (mvbp & 1)
    2345        2648 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    2346             : 
    2347        2761 :                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
    2348        2761 :                 ff_vc1_interp_mc(v);
    2349       20367 :             } else if (twomv) {
    2350       12364 :                 dir = bmvtype == BMV_TYPE_BACKWARD;
    2351       12364 :                 dir2 = dir;
    2352       12364 :                 if (mvsw)
    2353        5853 :                     dir2 = !dir;
    2354       12364 :                 mvbp = v->twomvbp;
    2355       12364 :                 dmv_x = dmv_y = 0;
    2356       12364 :                 if (mvbp & 2)
    2357       10627 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    2358       12364 :                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
    2359             : 
    2360       12364 :                 dmv_x = dmv_y = 0;
    2361       12364 :                 if (mvbp & 1)
    2362       10929 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    2363       12364 :                 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
    2364             : 
    2365       12364 :                 if (mvsw) {
    2366       17559 :                     for (i = 0; i < 2; i++) {
    2367       11706 :                         s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
    2368       11706 :                         s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
    2369       11706 :                         s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
    2370       11706 :                         s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
    2371             :                     }
    2372             :                 } else {
    2373        6511 :                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
    2374        6511 :                     ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
    2375             :                 }
    2376             : 
    2377       12364 :                 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
    2378       12364 :                 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
    2379       12364 :                 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
    2380       12364 :                 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
    2381       12364 :                 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
    2382             :             } else {
    2383        8003 :                 dir = bmvtype == BMV_TYPE_BACKWARD;
    2384             : 
    2385        8003 :                 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
    2386        8003 :                 dmv_x = dmv_y = 0;
    2387        8003 :                 if (mvbp)
    2388        7691 :                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
    2389             : 
    2390        8003 :                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
    2391        8003 :                 v->blk_mv_type[s->block_index[0]] = 1;
    2392        8003 :                 v->blk_mv_type[s->block_index[1]] = 1;
    2393        8003 :                 v->blk_mv_type[s->block_index[2]] = 1;
    2394        8003 :                 v->blk_mv_type[s->block_index[3]] = 1;
    2395        8003 :                 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
    2396       24009 :                 for (i = 0; i < 2; i++) {
    2397       16006 :                     s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
    2398       16006 :                     s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
    2399             :                 }
    2400        8003 :                 ff_vc1_mc_1mv(v, dir);
    2401             :             }
    2402             : 
    2403       30997 :             if (cbp)
    2404       26859 :                 GET_MQUANT();  // p. 227
    2405       30997 :             s->current_picture.qscale_table[mb_pos] = mquant;
    2406       30997 :             if (!v->ttmbf && cbp)
    2407       26859 :                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
    2408      216979 :             for (i = 0; i < 6; i++) {
    2409      185982 :                 s->dc_val[0][s->block_index[i]] = 0;
    2410      185982 :                 dst_idx += i >> 2;
    2411      185982 :                 val = ((cbp >> (5 - i)) & 1);
    2412      185982 :                 if (!fieldtx)
    2413       99456 :                     off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
    2414             :                 else
    2415       86526 :                     off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
    2416      185982 :                 if (val) {
    2417      346863 :                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
    2418      115621 :                                              first_block, s->dest[dst_idx] + off,
    2419      231242 :                                              (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
    2420             :                                              CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
    2421      115621 :                     block_cbp |= pat << (i << 2);
    2422      115621 :                     if (!v->ttmbf && ttmb < 8)
    2423       92324 :                         ttmb = -1;
    2424      115621 :                     first_block = 0;
    2425             :                 }
    2426             :             }
    2427             : 
    2428             :         } else { // skipped
    2429        1542 :             dir = 0;
    2430       10794 :             for (i = 0; i < 6; i++) {
    2431        9252 :                 v->mb_type[0][s->block_index[i]] = 0;
    2432        9252 :                 s->dc_val[0][s->block_index[i]] = 0;
    2433             :             }
    2434        1542 :             s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
    2435        1542 :             s->current_picture.qscale_table[mb_pos] = 0;
    2436        1542 :             v->blk_mv_type[s->block_index[0]] = 0;
    2437        1542 :             v->blk_mv_type[s->block_index[1]] = 0;
    2438        1542 :             v->blk_mv_type[s->block_index[2]] = 0;
    2439        1542 :             v->blk_mv_type[s->block_index[3]] = 0;
    2440             : 
    2441        1542 :             if (!direct) {
    2442         215 :                 if (bmvtype == BMV_TYPE_INTERPOLATED) {
    2443           9 :                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
    2444           9 :                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
    2445             :                 } else {
    2446         206 :                     dir = bmvtype == BMV_TYPE_BACKWARD;
    2447         206 :                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
    2448         206 :                     if (mvsw) {
    2449           0 :                         int dir2 = dir;
    2450           0 :                         if (mvsw)
    2451           0 :                             dir2 = !dir;
    2452           0 :                         for (i = 0; i < 2; i++) {
    2453           0 :                             s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
    2454           0 :                             s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
    2455           0 :                             s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
    2456           0 :                             s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
    2457             :                         }
    2458             :                     } else {
    2459         206 :                         v->blk_mv_type[s->block_index[0]] = 1;
    2460         206 :                         v->blk_mv_type[s->block_index[1]] = 1;
    2461         206 :                         v->blk_mv_type[s->block_index[2]] = 1;
    2462         206 :                         v->blk_mv_type[s->block_index[3]] = 1;
    2463         206 :                         ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
    2464         618 :                         for (i = 0; i < 2; i++) {
    2465         412 :                             s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
    2466         412 :                             s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
    2467             :                         }
    2468             :                     }
    2469             :                 }
    2470             :             }
    2471             : 
    2472        1542 :             ff_vc1_mc_1mv(v, dir);
    2473        1542 :             if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
    2474        1336 :                 ff_vc1_interp_mc(v);
    2475             :             }
    2476             :         }
    2477             :     }
    2478       32640 :     if (s->mb_x == s->mb_width - 1)
    2479         272 :         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
    2480       32640 :     v->cbp[s->mb_x]      = block_cbp;
    2481       32640 :     v->ttblk[s->mb_x]    = block_tt;
    2482       32640 :     return 0;
    2483             : }
    2484             : 
    2485             : /** Decode blocks of I-frame
    2486             :  */
    2487          81 : static void vc1_decode_i_blocks(VC1Context *v)
    2488             : {
    2489             :     int k, j;
    2490          81 :     MpegEncContext *s = &v->s;
    2491             :     int cbp, val;
    2492             :     uint8_t *coded_val;
    2493             :     int mb_pos;
    2494             : 
    2495             :     /* select coding mode used for VLC tables selection */
    2496          81 :     switch (v->y_ac_table_index) {
    2497           3 :     case 0:
    2498           3 :         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
    2499           3 :         break;
    2500          70 :     case 1:
    2501          70 :         v->codingset = CS_HIGH_MOT_INTRA;
    2502          70 :         break;
    2503           8 :     case 2:
    2504           8 :         v->codingset = CS_MID_RATE_INTRA;
    2505           8 :         break;
    2506             :     }
    2507             : 
    2508          81 :     switch (v->c_ac_table_index) {
    2509          75 :     case 0:
    2510          75 :         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
    2511          75 :         break;
    2512           5 :     case 1:
    2513           5 :         v->codingset2 = CS_HIGH_MOT_INTER;
    2514           5 :         break;
    2515           1 :     case 2:
    2516           1 :         v->codingset2 = CS_MID_RATE_INTER;
    2517           1 :         break;
    2518             :     }
    2519             : 
    2520             :     /* Set DC scale - y and c use the same */
    2521          81 :     s->y_dc_scale = s->y_dc_scale_table[v->pq];
    2522          81 :     s->c_dc_scale = s->c_dc_scale_table[v->pq];
    2523             : 
    2524             :     //do frame decode
    2525          81 :     s->mb_x = s->mb_y = 0;
    2526          81 :     s->mb_intra         = 1;
    2527          81 :     s->first_slice_line = 1;
    2528         917 :     for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
    2529         836 :         s->mb_x = 0;
    2530         836 :         init_block_index(v);
    2531       13316 :         for (; s->mb_x < v->end_mb_x; s->mb_x++) {
    2532             :             uint8_t *dst[6];
    2533       12480 :             ff_update_block_index(s);
    2534       12480 :             dst[0] = s->dest[0];
    2535       12480 :             dst[1] = dst[0] + 8;
    2536       12480 :             dst[2] = s->dest[0] + s->linesize * 8;
    2537       12480 :             dst[3] = dst[2] + 8;
    2538       12480 :             dst[4] = s->dest[1];
    2539       12480 :             dst[5] = s->dest[2];
    2540       12480 :             s->bdsp.clear_blocks(s->block[0]);
    2541       12480 :             mb_pos = s->mb_x + s->mb_y * s->mb_width;
    2542       12480 :             s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
    2543       12480 :             s->current_picture.qscale_table[mb_pos]                = v->pq;
    2544       12480 :             s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
    2545       12480 :             s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
    2546             : 
    2547             :             // do actual MB decoding and displaying
    2548       12480 :             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
    2549       12480 :             v->s.ac_pred = get_bits1(&v->s.gb);
    2550             : 
    2551       87360 :             for (k = 0; k < 6; k++) {
    2552       74880 :                 val = ((cbp >> (5 - k)) & 1);
    2553             : 
    2554       74880 :                 if (k < 4) {
    2555       49920 :                     int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
    2556       49920 :                     val        = val ^ pred;
    2557       49920 :                     *coded_val = val;
    2558             :                 }
    2559       74880 :                 cbp |= val << (5 - k);
    2560             : 
    2561       74880 :                 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
    2562             : 
    2563             :                 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    2564             :                     continue;
    2565       74880 :                 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
    2566       74880 :                 if (v->pq >= 9 && v->overlap) {
    2567           0 :                     if (v->rangeredfrm)
    2568           0 :                         for (j = 0; j < 64; j++)
    2569           0 :                             s->block[k][j] <<= 1;
    2570           0 :                     s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
    2571           0 :                                                       k & 4 ? s->uvlinesize
    2572             :                                                             : s->linesize);
    2573             :                 } else {
    2574       74880 :                     if (v->rangeredfrm)
    2575     1653600 :                         for (j = 0; j < 64; j++)
    2576     1628160 :                             s->block[k][j] = (s->block[k][j] - 64) << 1;
    2577      149760 :                     s->idsp.put_pixels_clamped(s->block[k], dst[k],
    2578       74880 :                                                k & 4 ? s->uvlinesize
    2579             :                                                      : s->linesize);
    2580             :                 }
    2581             :             }
    2582             : 
    2583       12480 :             if (v->pq >= 9 && v->overlap) {
    2584           0 :                 if (s->mb_x) {
    2585           0 :                     v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
    2586           0 :                     v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
    2587             :                     if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
    2588           0 :                         v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
    2589           0 :                         v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
    2590             :                     }
    2591             :                 }
    2592           0 :                 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
    2593           0 :                 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
    2594           0 :                 if (!s->first_slice_line) {
    2595           0 :                     v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
    2596           0 :                     v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
    2597             :                     if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
    2598           0 :                         v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
    2599           0 :                         v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
    2600             :                     }
    2601             :                 }
    2602           0 :                 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
    2603           0 :                 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
    2604             :             }
    2605       12480 :             if (v->s.loop_filter)
    2606       12480 :                 ff_vc1_loop_filter_iblk(v, v->pq);
    2607             : 
    2608       12480 :             if (get_bits_count(&s->gb) > v->bits) {
    2609           0 :                 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
    2610           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
    2611           0 :                        get_bits_count(&s->gb), v->bits);
    2612           0 :                 return;
    2613             :             }
    2614             :         }
    2615         836 :         if (!v->s.loop_filter)
    2616           0 :             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
    2617         836 :         else if (s->mb_y)
    2618         755 :             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
    2619             : 
    2620         836 :         s->first_slice_line = 0;
    2621             :     }
    2622          81 :     if (v->s.loop_filter)
    2623          81 :         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
    2624             : 
    2625             :     /* This is intentionally mb_height and not end_mb_y - unlike in advanced
    2626             :      * profile, these only differ are when decoding MSS2 rectangles. */
    2627          81 :     ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
    2628             : }
    2629             : 
    2630             : /** Decode blocks of I-frame for advanced profile
    2631             :  */
    2632          14 : static void vc1_decode_i_blocks_adv(VC1Context *v)
    2633             : {
    2634             :     int k;
    2635          14 :     MpegEncContext *s = &v->s;
    2636             :     int cbp, val;
    2637             :     uint8_t *coded_val;
    2638             :     int mb_pos;
    2639          14 :     int mquant = v->pq;
    2640             :     int mqdiff;
    2641          14 :     GetBitContext *gb = &s->gb;
    2642             : 
    2643             :     /* select coding mode used for VLC tables selection */
    2644          14 :     switch (v->y_ac_table_index) {
    2645           1 :     case 0:
    2646           1 :         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
    2647           1 :         break;
    2648           2 :     case 1:
    2649           2 :         v->codingset = CS_HIGH_MOT_INTRA;
    2650           2 :         break;
    2651          11 :     case 2:
    2652          11 :         v->codingset = CS_MID_RATE_INTRA;
    2653          11 :         break;
    2654             :     }
    2655             : 
    2656          14 :     switch (v->c_ac_table_index) {
    2657          12 :     case 0:
    2658          12 :         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
    2659          12 :         break;
    2660           0 :     case 1:
    2661           0 :         v->codingset2 = CS_HIGH_MOT_INTER;
    2662           0 :         break;
    2663           2 :     case 2:
    2664           2 :         v->codingset2 = CS_MID_RATE_INTER;
    2665           2 :         break;
    2666             :     }
    2667             : 
    2668             :     // do frame decode
    2669          14 :     s->mb_x             = s->mb_y = 0;
    2670          14 :     s->mb_intra         = 1;
    2671          14 :     s->first_slice_line = 1;
    2672          14 :     s->mb_y             = s->start_mb_y;
    2673          14 :     if (s->start_mb_y) {
    2674           6 :         s->mb_x = 0;
    2675           6 :         init_block_index(v);
    2676           6 :         memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
    2677           6 :                (1 + s->b8_stride) * sizeof(*s->coded_block));
    2678             :     }
    2679         195 :     for (; s->mb_y < s->end_mb_y; s->mb_y++) {
    2680         181 :         s->mb_x = 0;
    2681         181 :         init_block_index(v);
    2682       12295 :         for (;s->mb_x < s->mb_width; s->mb_x++) {
    2683       12114 :             int16_t (*block)[64] = v->block[v->cur_blk_idx];
    2684       12114 :             ff_update_block_index(s);
    2685       12114 :             s->bdsp.clear_blocks(block[0]);
    2686       12114 :             mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    2687       12114 :             s->current_picture.mb_type[mb_pos + v->mb_off]                         = MB_TYPE_INTRA;
    2688       12114 :             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
    2689       12114 :             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
    2690             : 
    2691             :             // do actual MB decoding and displaying
    2692       12114 :             if (v->fieldtx_is_raw)
    2693           0 :                 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
    2694       12114 :             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
    2695       12114 :             if (v->acpred_is_raw)
    2696        2670 :                 v->s.ac_pred = get_bits1(&v->s.gb);
    2697             :             else
    2698        9444 :                 v->s.ac_pred = v->acpred_plane[mb_pos];
    2699             : 
    2700       12114 :             if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
    2701           0 :                 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
    2702             : 
    2703       12114 :             GET_MQUANT();
    2704             : 
    2705       12114 :             s->current_picture.qscale_table[mb_pos] = mquant;
    2706             :             /* Set DC scale - y and c use the same */
    2707       12114 :             s->y_dc_scale = s->y_dc_scale_table[mquant];
    2708       12114 :             s->c_dc_scale = s->c_dc_scale_table[mquant];
    2709             : 
    2710       84798 :             for (k = 0; k < 6; k++) {
    2711       72684 :                 val = ((cbp >> (5 - k)) & 1);
    2712             : 
    2713       72684 :                 if (k < 4) {
    2714       48456 :                     int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
    2715       48456 :                     val        = val ^ pred;
    2716       48456 :                     *coded_val = val;
    2717             :                 }
    2718       72684 :                 cbp |= val << (5 - k);
    2719             : 
    2720       72684 :                 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
    2721       72684 :                 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
    2722             : 
    2723       72684 :                 vc1_decode_i_block_adv(v, block[k], k, val,
    2724             :                                        (k < 4) ? v->codingset : v->codingset2, mquant);
    2725             : 
    2726             :                 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
    2727             :                     continue;
    2728       72684 :                 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
    2729             :             }
    2730             : 
    2731       12114 :             ff_vc1_smooth_overlap_filter_iblk(v);
    2732       12114 :             vc1_put_signed_blocks_clamped(v);
    2733       12114 :             if (v->s.loop_filter)
    2734       11814 :                 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
    2735             : 
    2736       12114 :             if (get_bits_count(&s->gb) > v->bits) {
    2737             :                 // TODO: may need modification to handle slice coding
    2738           0 :                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
    2739           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
    2740           0 :                        get_bits_count(&s->gb), v->bits);
    2741           0 :                 return;
    2742             :             }
    2743             :         }
    2744         181 :         if (!v->s.loop_filter)
    2745          15 :             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
    2746         166 :         else if (s->mb_y)
    2747         159 :             ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
    2748         181 :         s->first_slice_line = 0;
    2749             :     }
    2750             : 
    2751             :     /* raw bottom MB row */
    2752          14 :     s->mb_x = 0;
    2753          14 :     init_block_index(v);
    2754         596 :     for (; s->mb_x < s->mb_width; s->mb_x++) {
    2755         582 :         ff_update_block_index(s);
    2756         582 :         vc1_put_signed_blocks_clamped(v);
    2757         582 :         if (v->s.loop_filter)
    2758         562 :             ff_vc1_loop_filter_iblk_delayed(v, v->pq);
    2759             :     }
    2760          14 :     if (v->s.loop_filter)
    2761          13 :         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
    2762          14 :     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
    2763          14 :                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
    2764             : }
    2765             : 
    2766         879 : static void vc1_decode_p_blocks(VC1Context *v)
    2767             : {
    2768         879 :     MpegEncContext *s = &v->s;
    2769             :     int apply_loop_filter;
    2770             : 
    2771             :     /* select coding mode used for VLC tables selection */
    2772         879 :     switch (v->c_ac_table_index) {
    2773         232 :     case 0:
    2774         232 :         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
    2775         232 :         break;
    2776         391 :     case 1:
    2777         391 :         v->codingset = CS_HIGH_MOT_INTRA;
    2778         391 :         break;
    2779         256 :     case 2:
    2780         256 :         v->codingset = CS_MID_RATE_INTRA;
    2781         256 :         break;
    2782             :     }
    2783             : 
    2784         879 :     switch (v->c_ac_table_index) {
    2785         232 :     case 0:
    2786         232 :         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
    2787         232 :         break;
    2788         391 :     case 1:
    2789         391 :         v->codingset2 = CS_HIGH_MOT_INTER;
    2790         391 :         break;
    2791         256 :     case 2:
    2792         256 :         v->codingset2 = CS_MID_RATE_INTER;
    2793         256 :         break;
    2794             :     }
    2795             : 
    2796        1744 :     apply_loop_filter   = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
    2797         865 :                           v->fcm == PROGRESSIVE;
    2798         879 :     s->first_slice_line = 1;
    2799         879 :     memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
    2800        5017 :     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
    2801        4138 :         s->mb_x = 0;
    2802        4138 :         init_block_index(v);
    2803      155207 :         for (; s->mb_x < s->mb_width; s->mb_x++) {
    2804      151069 :             ff_update_block_index(s);
    2805             : 
    2806      151069 :             if (v->fcm == ILACE_FIELD)
    2807       37245 :                 vc1_decode_p_mb_intfi(v);
    2808      113824 :             else if (v->fcm == ILACE_FRAME)
    2809       16320 :                 vc1_decode_p_mb_intfr(v);
    2810       97504 :             else vc1_decode_p_mb(v);
    2811      151069 :             if (s->mb_y != s->start_mb_y && apply_loop_filter)
    2812       61688 :                 ff_vc1_apply_p_loop_filter(v);
    2813      151069 :             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
    2814             :                 // TODO: may need modification to handle slice coding
    2815           0 :                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
    2816           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
    2817           0 :                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
    2818           0 :                 return;
    2819             :             }
    2820             :         }
    2821        4138 :         memmove(v->cbp_base,      v->cbp,      sizeof(v->cbp_base[0])      * s->mb_stride);
    2822        4138 :         memmove(v->ttblk_base,    v->ttblk,    sizeof(v->ttblk_base[0])    * s->mb_stride);
    2823        4138 :         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
    2824        4138 :         memmove(v->luma_mv_base,  v->luma_mv,  sizeof(v->luma_mv_base[0])  * s->mb_stride);
    2825        4138 :         if (s->mb_y != s->start_mb_y)
    2826        3259 :             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
    2827        4138 :         s->first_slice_line = 0;
    2828             :     }
    2829         879 :     if (apply_loop_filter) {
    2830         828 :         s->mb_x = 0;
    2831         828 :         init_block_index(v);
    2832       32444 :         for (; s->mb_x < s->mb_width; s->mb_x++) {
    2833       31616 :             ff_update_block_index(s);
    2834       31616 :             ff_vc1_apply_p_loop_filter(v);
    2835             :         }
    2836             :     }
    2837         879 :     if (s->end_mb_y >= s->start_mb_y)
    2838         879 :         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
    2839         879 :     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
    2840         879 :                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
    2841             : }
    2842             : 
    2843          98 : static void vc1_decode_b_blocks(VC1Context *v)
    2844             : {
    2845          98 :     MpegEncContext *s = &v->s;
    2846             : 
    2847             :     /* select coding mode used for VLC tables selection */
    2848          98 :     switch (v->c_ac_table_index) {
    2849           4 :     case 0:
    2850           4 :         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
    2851           4 :         break;
    2852          16 :     case 1:
    2853          16 :         v->codingset = CS_HIGH_MOT_INTRA;
    2854          16 :         break;
    2855          78 :     case 2:
    2856          78 :         v->codingset = CS_MID_RATE_INTRA;
    2857          78 :         break;
    2858             :     }
    2859             : 
    2860          98 :     switch (v->c_ac_table_index) {
    2861           4 :     case 0:
    2862           4 :         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
    2863           4 :         break;
    2864          16 :     case 1:
    2865          16 :         v->codingset2 = CS_HIGH_MOT_INTER;
    2866          16 :         break;
    2867          78 :     case 2:
    2868          78 :         v->codingset2 = CS_MID_RATE_INTER;
    2869          78 :         break;
    2870             :     }
    2871             : 
    2872          98 :     s->first_slice_line = 1;
    2873        1468 :     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
    2874        1370 :         s->mb_x = 0;
    2875        1370 :         init_block_index(v);
    2876       91640 :         for (; s->mb_x < s->mb_width; s->mb_x++) {
    2877       90270 :             ff_update_block_index(s);
    2878             : 
    2879       90270 :             if (v->fcm == ILACE_FIELD)
    2880       51540 :                 vc1_decode_b_mb_intfi(v);
    2881       38730 :             else if (v->fcm == ILACE_FRAME)
    2882       32640 :                 vc1_decode_b_mb_intfr(v);
    2883             :             else
    2884        6090 :                 vc1_decode_b_mb(v);
    2885       90270 :             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
    2886             :                 // TODO: may need modification to handle slice coding
    2887           0 :                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
    2888           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
    2889           0 :                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
    2890           0 :                 return;
    2891             :             }
    2892       90270 :             if (v->s.loop_filter)
    2893       90270 :                 ff_vc1_loop_filter_iblk(v, v->pq);
    2894             :         }
    2895        1370 :         if (!v->s.loop_filter)
    2896           0 :             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
    2897        1370 :         else if (s->mb_y)
    2898        1272 :             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
    2899        1370 :         s->first_slice_line = 0;
    2900             :     }
    2901          98 :     if (v->s.loop_filter)
    2902          98 :         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
    2903          98 :     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
    2904          98 :                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
    2905             : }
    2906             : 
    2907          67 : static void vc1_decode_skip_blocks(VC1Context *v)
    2908             : {
    2909          67 :     MpegEncContext *s = &v->s;
    2910             : 
    2911          67 :     if (!v->s.last_picture.f->data[0])
    2912           0 :         return;
    2913             : 
    2914          67 :     ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
    2915          67 :     s->first_slice_line = 1;
    2916        1684 :     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
    2917        1617 :         s->mb_x = 0;
    2918        1617 :         init_block_index(v);
    2919        1617 :         ff_update_block_index(s);
    2920        1617 :         memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize,   s->linesize   * 16);
    2921        1617 :         memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
    2922        1617 :         memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
    2923        1617 :         ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
    2924        1617 :         s->first_slice_line = 0;
    2925             :     }
    2926          67 :     s->pict_type = AV_PICTURE_TYPE_P;
    2927             : }
    2928             : 
    2929        1139 : void ff_vc1_decode_blocks(VC1Context *v)
    2930             : {
    2931             : 
    2932        1139 :     v->s.esc3_level_length = 0;
    2933        1139 :     if (v->x8_type) {
    2934           0 :         ff_intrax8_decode_picture(&v->x8, &v->s.current_picture,
    2935             :                                   &v->s.gb, &v->s.mb_x, &v->s.mb_y,
    2936           0 :                                   2 * v->pq + v->halfpq, v->pq * !v->pquantizer,
    2937             :                                   v->s.loop_filter, v->s.low_delay);
    2938             : 
    2939           0 :         ff_er_add_slice(&v->s.er, 0, 0,
    2940           0 :                         (v->s.mb_x >> 1) - 1, (v->s.mb_y >> 1) - 1,
    2941             :                         ER_MB_END);
    2942             :     } else {
    2943        1139 :         v->cur_blk_idx     =  0;
    2944        1139 :         v->left_blk_idx    = -1;
    2945        1139 :         v->topleft_blk_idx =  1;
    2946        1139 :         v->top_blk_idx     =  2;
    2947        1139 :         switch (v->s.pict_type) {
    2948          95 :         case AV_PICTURE_TYPE_I:
    2949          95 :             if (v->profile == PROFILE_ADVANCED)
    2950          14 :                 vc1_decode_i_blocks_adv(v);
    2951             :             else
    2952          81 :                 vc1_decode_i_blocks(v);
    2953          95 :             break;
    2954         946 :         case AV_PICTURE_TYPE_P:
    2955         946 :             if (v->p_frame_skipped)
    2956          67 :                 vc1_decode_skip_blocks(v);
    2957             :             else
    2958         879 :                 vc1_decode_p_blocks(v);
    2959         946 :             break;
    2960          98 :         case AV_PICTURE_TYPE_B:
    2961          98 :             if (v->bi_type) {
    2962           0 :                 if (v->profile == PROFILE_ADVANCED)
    2963           0 :                     vc1_decode_i_blocks_adv(v);
    2964             :                 else
    2965           0 :                     vc1_decode_i_blocks(v);
    2966             :             } else
    2967          98 :                 vc1_decode_b_blocks(v);
    2968          98 :             break;
    2969             :         }
    2970             :     }
    2971        1139 : }

Generated by: LCOV version 1.13