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