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