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