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