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