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