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