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

Generated by: LCOV version 1.13