dump: split audio and video probing on multiple lines
[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->flags & 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->flags & 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->flags & 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->flags & 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             s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
1652             s->mb_intra = v->is_intra[s->mb_x] = 1;
1653             for (i = 0; i < 6; i++)
1654                 v->mb_type[0][s->block_index[i]] = 1;
1655             fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1656             mb_has_coeffs = get_bits1(gb);
1657             if (mb_has_coeffs)
1658                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1659             v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1660             GET_MQUANT();
1661             s->current_picture.qscale_table[mb_pos] = mquant;
1662             /* Set DC scale - y and c use the same (not sure if necessary here) */
1663             s->y_dc_scale = s->y_dc_scale_table[mquant];
1664             s->c_dc_scale = s->c_dc_scale_table[mquant];
1665             dst_idx = 0;
1666             for (i = 0; i < 6; i++) {
1667                 s->dc_val[0][s->block_index[i]] = 0;
1668                 dst_idx += i >> 2;
1669                 val = ((cbp >> (5 - i)) & 1);
1670                 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1671                 v->a_avail = v->c_avail = 0;
1672                 if (i == 2 || i == 3 || !s->first_slice_line)
1673                     v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1674                 if (i == 1 || i == 3 || s->mb_x)
1675                     v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1676
1677                 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1678                                        (i & 4) ? v->codingset2 : v->codingset);
1679                 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) 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->flags & 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         s->mb_intra = v->is_intra[s->mb_x] = 1;
1822         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1823         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1824         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1825         GET_MQUANT();
1826         s->current_picture.qscale_table[mb_pos] = mquant;
1827         /* Set DC scale - y and c use the same (not sure if necessary here) */
1828         s->y_dc_scale = s->y_dc_scale_table[mquant];
1829         s->c_dc_scale = s->c_dc_scale_table[mquant];
1830         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
1831         mb_has_coeffs = idx_mbmode & 1;
1832         if (mb_has_coeffs)
1833             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1834         dst_idx = 0;
1835         for (i = 0; i < 6; i++) {
1836             s->dc_val[0][s->block_index[i]]  = 0;
1837             v->mb_type[0][s->block_index[i]] = 1;
1838             dst_idx += i >> 2;
1839             val = ((cbp >> (5 - i)) & 1);
1840             v->a_avail = v->c_avail = 0;
1841             if (i == 2 || i == 3 || !s->first_slice_line)
1842                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1843             if (i == 1 || i == 3 || s->mb_x)
1844                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1845
1846             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1847                                    (i & 4) ? v->codingset2 : v->codingset);
1848             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1849                 continue;
1850             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1851             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1852             s->idsp.put_signed_pixels_clamped(s->block[i],
1853                                               s->dest[dst_idx] + off,
1854                                               (i & 4) ? s->uvlinesize
1855                                                       : s->linesize);
1856             // TODO: loop filter
1857         }
1858     } else {
1859         s->mb_intra = v->is_intra[s->mb_x] = 0;
1860         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1861         for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
1862         if (idx_mbmode <= 5) { // 1-MV
1863             dmv_x = dmv_y = pred_flag = 0;
1864             if (idx_mbmode & 1) {
1865                 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1866             }
1867             ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1868             ff_vc1_mc_1mv(v, 0);
1869             mb_has_coeffs = !(idx_mbmode & 2);
1870         } else { // 4-MV
1871             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1872             for (i = 0; i < 6; i++) {
1873                 if (i < 4) {
1874                     dmv_x = dmv_y = pred_flag = 0;
1875                     val   = ((v->fourmvbp >> (3 - i)) & 1);
1876                     if (val) {
1877                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1878                     }
1879                     ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1880                     ff_vc1_mc_4mv_luma(v, i, 0, 0);
1881                 } else if (i == 4)
1882                     ff_vc1_mc_4mv_chroma(v, 0);
1883             }
1884             mb_has_coeffs = idx_mbmode & 1;
1885         }
1886         if (mb_has_coeffs)
1887             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1888         if (cbp) {
1889             GET_MQUANT();
1890         }
1891         s->current_picture.qscale_table[mb_pos] = mquant;
1892         if (!v->ttmbf && cbp) {
1893             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1894         }
1895         dst_idx = 0;
1896         for (i = 0; i < 6; i++) {
1897             s->dc_val[0][s->block_index[i]] = 0;
1898             dst_idx += i >> 2;
1899             val = ((cbp >> (5 - i)) & 1);
1900             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1901             if (val) {
1902                 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1903                                          first_block, s->dest[dst_idx] + off,
1904                                          (i & 4) ? s->uvlinesize : s->linesize,
1905                                          (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1906                                          &block_tt);
1907                 block_cbp |= pat << (i << 2);
1908                 if (!v->ttmbf && ttmb < 8) ttmb = -1;
1909                 first_block = 0;
1910             }
1911         }
1912     }
1913     if (s->mb_x == s->mb_width - 1)
1914         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1915     return 0;
1916 }
1917
1918 /** Decode one B-frame MB (in Main profile)
1919  */
1920 static void vc1_decode_b_mb(VC1Context *v)
1921 {
1922     MpegEncContext *s = &v->s;
1923     GetBitContext *gb = &s->gb;
1924     int i, j;
1925     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1926     int cbp = 0; /* cbp decoding stuff */
1927     int mqdiff, mquant; /* MB quantization */
1928     int ttmb = v->ttfrm; /* MB Transform type */
1929     int mb_has_coeffs = 0; /* last_flag */
1930     int index, index1; /* LUT indexes */
1931     int val, sign; /* temp values */
1932     int first_block = 1;
1933     int dst_idx, off;
1934     int skipped, direct;
1935     int dmv_x[2], dmv_y[2];
1936     int bmvtype = BMV_TYPE_BACKWARD;
1937
1938     mquant      = v->pq; /* lossy initialization */
1939     s->mb_intra = 0;
1940
1941     if (v->dmb_is_raw)
1942         direct = get_bits1(gb);
1943     else
1944         direct = v->direct_mb_plane[mb_pos];
1945     if (v->skip_is_raw)
1946         skipped = get_bits1(gb);
1947     else
1948         skipped = v->s.mbskip_table[mb_pos];
1949
1950     dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1951     for (i = 0; i < 6; i++) {
1952         v->mb_type[0][s->block_index[i]] = 0;
1953         s->dc_val[0][s->block_index[i]]  = 0;
1954     }
1955     s->current_picture.qscale_table[mb_pos] = 0;
1956
1957     if (!direct) {
1958         if (!skipped) {
1959             GET_MVDATA(dmv_x[0], dmv_y[0]);
1960             dmv_x[1] = dmv_x[0];
1961             dmv_y[1] = dmv_y[0];
1962         }
1963         if (skipped || !s->mb_intra) {
1964             bmvtype = decode012(gb);
1965             switch (bmvtype) {
1966             case 0:
1967                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1968                 break;
1969             case 1:
1970                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1971                 break;
1972             case 2:
1973                 bmvtype  = BMV_TYPE_INTERPOLATED;
1974                 dmv_x[0] = dmv_y[0] = 0;
1975             }
1976         }
1977     }
1978     for (i = 0; i < 6; i++)
1979         v->mb_type[0][s->block_index[i]] = s->mb_intra;
1980
1981     if (skipped) {
1982         if (direct)
1983             bmvtype = BMV_TYPE_INTERPOLATED;
1984         ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1985         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1986         return;
1987     }
1988     if (direct) {
1989         cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1990         GET_MQUANT();
1991         s->mb_intra = 0;
1992         s->current_picture.qscale_table[mb_pos] = mquant;
1993         if (!v->ttmbf)
1994             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1995         dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1996         ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1997         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1998     } else {
1999         if (!mb_has_coeffs && !s->mb_intra) {
2000             /* no coded blocks - effectively skipped */
2001             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2002             vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2003             return;
2004         }
2005         if (s->mb_intra && !mb_has_coeffs) {
2006             GET_MQUANT();
2007             s->current_picture.qscale_table[mb_pos] = mquant;
2008             s->ac_pred = get_bits1(gb);
2009             cbp = 0;
2010             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2011         } else {
2012             if (bmvtype == BMV_TYPE_INTERPOLATED) {
2013                 GET_MVDATA(dmv_x[0], dmv_y[0]);
2014                 if (!mb_has_coeffs) {
2015                     /* interpolated skipped block */
2016                     ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2017                     vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2018                     return;
2019                 }
2020             }
2021             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2022             if (!s->mb_intra) {
2023                 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2024             }
2025             if (s->mb_intra)
2026                 s->ac_pred = get_bits1(gb);
2027             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2028             GET_MQUANT();
2029             s->current_picture.qscale_table[mb_pos] = mquant;
2030             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2031                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2032         }
2033     }
2034     dst_idx = 0;
2035     for (i = 0; i < 6; i++) {
2036         s->dc_val[0][s->block_index[i]] = 0;
2037         dst_idx += i >> 2;
2038         val = ((cbp >> (5 - i)) & 1);
2039         off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2040         v->mb_type[0][s->block_index[i]] = s->mb_intra;
2041         if (s->mb_intra) {
2042             /* check if prediction blocks A and C are available */
2043             v->a_avail = v->c_avail = 0;
2044             if (i == 2 || i == 3 || !s->first_slice_line)
2045                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2046             if (i == 1 || i == 3 || s->mb_x)
2047                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2048
2049             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2050                                    (i & 4) ? v->codingset2 : v->codingset);
2051             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2052                 continue;
2053             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2054             if (v->rangeredfrm)
2055                 for (j = 0; j < 64; j++)
2056                     s->block[i][j] <<= 1;
2057             s->idsp.put_signed_pixels_clamped(s->block[i],
2058                                               s->dest[dst_idx] + off,
2059                                               i & 4 ? s->uvlinesize
2060                                                     : s->linesize);
2061         } else if (val) {
2062             vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2063                                first_block, s->dest[dst_idx] + off,
2064                                (i & 4) ? s->uvlinesize : s->linesize,
2065                                (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2066             if (!v->ttmbf && ttmb < 8)
2067                 ttmb = -1;
2068             first_block = 0;
2069         }
2070     }
2071 }
2072
2073 /** Decode one B-frame MB (in interlaced field B picture)
2074  */
2075 static void vc1_decode_b_mb_intfi(VC1Context *v)
2076 {
2077     MpegEncContext *s = &v->s;
2078     GetBitContext *gb = &s->gb;
2079     int i, j;
2080     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2081     int cbp = 0; /* cbp decoding stuff */
2082     int mqdiff, mquant; /* MB quantization */
2083     int ttmb = v->ttfrm; /* MB Transform type */
2084     int mb_has_coeffs = 0; /* last_flag */
2085     int val; /* temp value */
2086     int first_block = 1;
2087     int dst_idx, off;
2088     int fwd;
2089     int dmv_x[2], dmv_y[2], pred_flag[2];
2090     int bmvtype = BMV_TYPE_BACKWARD;
2091     int idx_mbmode, interpmvp;
2092
2093     mquant      = v->pq; /* Loosy initialization */
2094     s->mb_intra = 0;
2095
2096     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2097     if (idx_mbmode <= 1) { // intra MB
2098         s->mb_intra = v->is_intra[s->mb_x] = 1;
2099         s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2100         s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2101         s->current_picture.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
2102         GET_MQUANT();
2103         s->current_picture.qscale_table[mb_pos] = mquant;
2104         /* Set DC scale - y and c use the same (not sure if necessary here) */
2105         s->y_dc_scale = s->y_dc_scale_table[mquant];
2106         s->c_dc_scale = s->c_dc_scale_table[mquant];
2107         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
2108         mb_has_coeffs = idx_mbmode & 1;
2109         if (mb_has_coeffs)
2110             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2111         dst_idx = 0;
2112         for (i = 0; i < 6; i++) {
2113             s->dc_val[0][s->block_index[i]] = 0;
2114             dst_idx += i >> 2;
2115             val = ((cbp >> (5 - i)) & 1);
2116             v->mb_type[0][s->block_index[i]] = s->mb_intra;
2117             v->a_avail                       = v->c_avail = 0;
2118             if (i == 2 || i == 3 || !s->first_slice_line)
2119                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2120             if (i == 1 || i == 3 || s->mb_x)
2121                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2122
2123             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2124                                    (i & 4) ? v->codingset2 : v->codingset);
2125             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2126                 continue;
2127             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2128             if (v->rangeredfrm)
2129                 for (j = 0; j < 64; j++)
2130                     s->block[i][j] <<= 1;
2131             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2132             s->idsp.put_signed_pixels_clamped(s->block[i],
2133                                               s->dest[dst_idx] + off,
2134                                               (i & 4) ? s->uvlinesize
2135                                                       : s->linesize);
2136             // TODO: yet to perform loop filter
2137         }
2138     } else {
2139         s->mb_intra = v->is_intra[s->mb_x] = 0;
2140         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2141         for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2142         if (v->fmb_is_raw)
2143             fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2144         else
2145             fwd = v->forward_mb_plane[mb_pos];
2146         if (idx_mbmode <= 5) { // 1-MV
2147             dmv_x[0]     = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2148             pred_flag[0] = pred_flag[1] = 0;
2149             if (fwd)
2150                 bmvtype = BMV_TYPE_FORWARD;
2151             else {
2152                 bmvtype = decode012(gb);
2153                 switch (bmvtype) {
2154                 case 0:
2155                     bmvtype = BMV_TYPE_BACKWARD;
2156                     break;
2157                 case 1:
2158                     bmvtype = BMV_TYPE_DIRECT;
2159                     break;
2160                 case 2:
2161                     bmvtype   = BMV_TYPE_INTERPOLATED;
2162                     interpmvp = get_bits1(gb);
2163                 }
2164             }
2165             v->bmvtype = bmvtype;
2166             if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2167                 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2168             }
2169             if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
2170                 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2171             }
2172             if (bmvtype == BMV_TYPE_DIRECT) {
2173                 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2174                 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2175             }
2176             ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2177             vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2178             mb_has_coeffs = !(idx_mbmode & 2);
2179         } else { // 4-MV
2180             if (fwd)
2181                 bmvtype = BMV_TYPE_FORWARD;
2182             v->bmvtype  = bmvtype;
2183             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2184             for (i = 0; i < 6; i++) {
2185                 if (i < 4) {
2186                     dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2187                     dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2188                     val = ((v->fourmvbp >> (3 - i)) & 1);
2189                     if (val) {
2190                         get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2191                                                  &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2192                                              &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2193                     }
2194                     ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2195                     ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2196                 } else if (i == 4)
2197                     ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2198             }
2199             mb_has_coeffs = idx_mbmode & 1;
2200         }
2201         if (mb_has_coeffs)
2202             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2203         if (cbp) {
2204             GET_MQUANT();
2205         }
2206         s->current_picture.qscale_table[mb_pos] = mquant;
2207         if (!v->ttmbf && cbp) {
2208             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2209         }
2210         dst_idx = 0;
2211         for (i = 0; i < 6; i++) {
2212             s->dc_val[0][s->block_index[i]] = 0;
2213             dst_idx += i >> 2;
2214             val = ((cbp >> (5 - i)) & 1);
2215             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2216             if (val) {
2217                 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2218                                    first_block, s->dest[dst_idx] + off,
2219                                    (i & 4) ? s->uvlinesize : s->linesize,
2220                                    (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2221                 if (!v->ttmbf && ttmb < 8)
2222                     ttmb = -1;
2223                 first_block = 0;
2224             }
2225         }
2226     }
2227 }
2228
2229 /** Decode one B-frame MB (in interlaced frame B picture)
2230  */
2231 static int vc1_decode_b_mb_intfr(VC1Context *v)
2232 {
2233     MpegEncContext *s = &v->s;
2234     GetBitContext *gb = &s->gb;
2235     int i, j;
2236     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2237     int cbp = 0; /* cbp decoding stuff */
2238     int mqdiff, mquant; /* MB quantization */
2239     int ttmb = v->ttfrm; /* MB Transform type */
2240     int mvsw = 0; /* motion vector switch */
2241     int mb_has_coeffs = 1; /* last_flag */
2242     int dmv_x, dmv_y; /* Differential MV components */
2243     int val; /* temp value */
2244     int first_block = 1;
2245     int dst_idx, off;
2246     int skipped, direct, twomv = 0;
2247     int block_cbp = 0, pat, block_tt = 0;
2248     int idx_mbmode = 0, mvbp;
2249     int stride_y, fieldtx;
2250     int bmvtype = BMV_TYPE_BACKWARD;
2251     int dir, dir2;
2252
2253     mquant = v->pq; /* Lossy initialization */
2254     s->mb_intra = 0;
2255     if (v->skip_is_raw)
2256         skipped = get_bits1(gb);
2257     else
2258         skipped = v->s.mbskip_table[mb_pos];
2259
2260     if (!skipped) {
2261         idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2262         if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2263             twomv = 1;
2264             v->blk_mv_type[s->block_index[0]] = 1;
2265             v->blk_mv_type[s->block_index[1]] = 1;
2266             v->blk_mv_type[s->block_index[2]] = 1;
2267             v->blk_mv_type[s->block_index[3]] = 1;
2268         } else {
2269             v->blk_mv_type[s->block_index[0]] = 0;
2270             v->blk_mv_type[s->block_index[1]] = 0;
2271             v->blk_mv_type[s->block_index[2]] = 0;
2272             v->blk_mv_type[s->block_index[3]] = 0;
2273         }
2274     }
2275
2276     if (v->dmb_is_raw)
2277         direct = get_bits1(gb);
2278     else
2279         direct = v->direct_mb_plane[mb_pos];
2280
2281     if (direct) {
2282         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);
2283         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);
2284         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);
2285         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);
2286
2287         if (twomv) {
2288             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);
2289             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);
2290             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);
2291             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);
2292
2293             for (i = 1; i < 4; i += 2) {
2294                 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2295                 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2296                 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2297                 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2298             }
2299         } else {
2300             for (i = 1; i < 4; i++) {
2301                 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2302                 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2303                 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2304                 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2305             }
2306         }
2307     }
2308
2309     if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2310         for (i = 0; i < 4; i++) {
2311             s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2312             s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2313             s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2314             s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2315         }
2316         s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2317         s->mb_intra = v->is_intra[s->mb_x] = 1;
2318         for (i = 0; i < 6; i++)
2319             v->mb_type[0][s->block_index[i]] = 1;
2320         fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2321         mb_has_coeffs = get_bits1(gb);
2322         if (mb_has_coeffs)
2323             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2324         v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2325         GET_MQUANT();
2326         s->current_picture.qscale_table[mb_pos] = mquant;
2327         /* Set DC scale - y and c use the same (not sure if necessary here) */
2328         s->y_dc_scale = s->y_dc_scale_table[mquant];
2329         s->c_dc_scale = s->c_dc_scale_table[mquant];
2330         dst_idx = 0;
2331         for (i = 0; i < 6; i++) {
2332             s->dc_val[0][s->block_index[i]] = 0;
2333             dst_idx += i >> 2;
2334             val = ((cbp >> (5 - i)) & 1);
2335             v->mb_type[0][s->block_index[i]] = s->mb_intra;
2336             v->a_avail = v->c_avail = 0;
2337             if (i == 2 || i == 3 || !s->first_slice_line)
2338                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2339             if (i == 1 || i == 3 || s->mb_x)
2340                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2341
2342             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2343                                    (i & 4) ? v->codingset2 : v->codingset);
2344             if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
2345                 continue;
2346             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2347             if (i < 4) {
2348                 stride_y = s->linesize << fieldtx;
2349                 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2350             } else {
2351                 stride_y = s->uvlinesize;
2352                 off = 0;
2353             }
2354             s->idsp.put_signed_pixels_clamped(s->block[i],
2355                                               s->dest[dst_idx] + off,
2356                                               stride_y);
2357         }
2358     } else {
2359         s->mb_intra = v->is_intra[s->mb_x] = 0;
2360         if (!direct) {
2361             if (skipped || !s->mb_intra) {
2362                 bmvtype = decode012(gb);
2363                 switch (bmvtype) {
2364                 case 0:
2365                     bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2366                     break;
2367                 case 1:
2368                     bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2369                     break;
2370                 case 2:
2371                     bmvtype  = BMV_TYPE_INTERPOLATED;
2372                 }
2373             }
2374
2375             if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2376                 mvsw = get_bits1(gb);
2377         }
2378
2379         if (!skipped) { // inter MB
2380             mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2381             if (mb_has_coeffs)
2382                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2383             if (!direct) {
2384                 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2385                     v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2386                 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2387                     v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2388                 }
2389             }
2390
2391             for (i = 0; i < 6; i++)
2392                 v->mb_type[0][s->block_index[i]] = 0;
2393             fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2394             /* for all motion vector read MVDATA and motion compensate each block */
2395             dst_idx = 0;
2396             if (direct) {
2397                 if (twomv) {
2398                     for (i = 0; i < 4; i++) {
2399                         ff_vc1_mc_4mv_luma(v, i, 0, 0);
2400                         ff_vc1_mc_4mv_luma(v, i, 1, 1);
2401                     }
2402                     ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2403                     ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2404                 } else {
2405                     ff_vc1_mc_1mv(v, 0);
2406                     ff_vc1_interp_mc(v);
2407                 }
2408             } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2409                 mvbp = v->fourmvbp;
2410                 for (i = 0; i < 4; i++) {
2411                     dir = i==1 || i==3;
2412                     dmv_x = dmv_y = 0;
2413                     val = ((mvbp >> (3 - i)) & 1);
2414                     if (val)
2415                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2416                     j = i > 1 ? 2 : 0;
2417                     ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2418                     ff_vc1_mc_4mv_luma(v, j, dir, dir);
2419                     ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2420                 }
2421
2422                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2423                 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2424             } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2425                 mvbp = v->twomvbp;
2426                 dmv_x = dmv_y = 0;
2427                 if (mvbp & 2)
2428                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2429
2430                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2431                 ff_vc1_mc_1mv(v, 0);
2432
2433                 dmv_x = dmv_y = 0;
2434                 if (mvbp & 1)
2435                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2436
2437                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2438                 ff_vc1_interp_mc(v);
2439             } else if (twomv) {
2440                 dir = bmvtype == BMV_TYPE_BACKWARD;
2441                 dir2 = dir;
2442                 if (mvsw)
2443                     dir2 = !dir;
2444                 mvbp = v->twomvbp;
2445                 dmv_x = dmv_y = 0;
2446                 if (mvbp & 2)
2447                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2448                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2449
2450                 dmv_x = dmv_y = 0;
2451                 if (mvbp & 1)
2452                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2453                 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2454
2455                 if (mvsw) {
2456                     for (i = 0; i < 2; i++) {
2457                         s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
2458                         s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
2459                         s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
2460                         s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
2461                     }
2462                 } else {
2463                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2464                     ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2465                 }
2466
2467                 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2468                 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2469                 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2470                 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2471                 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2472             } else {
2473                 dir = bmvtype == BMV_TYPE_BACKWARD;
2474
2475                 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2476                 dmv_x = dmv_y = 0;
2477                 if (mvbp)
2478                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2479
2480                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2481                 v->blk_mv_type[s->block_index[0]] = 1;
2482                 v->blk_mv_type[s->block_index[1]] = 1;
2483                 v->blk_mv_type[s->block_index[2]] = 1;
2484                 v->blk_mv_type[s->block_index[3]] = 1;
2485                 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2486                 for (i = 0; i < 2; i++) {
2487                     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];
2488                     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];
2489                 }
2490                 ff_vc1_mc_1mv(v, dir);
2491             }
2492
2493             if (cbp)
2494                 GET_MQUANT();  // p. 227
2495             s->current_picture.qscale_table[mb_pos] = mquant;
2496             if (!v->ttmbf && cbp)
2497                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2498             for (i = 0; i < 6; i++) {
2499                 s->dc_val[0][s->block_index[i]] = 0;
2500                 dst_idx += i >> 2;
2501                 val = ((cbp >> (5 - i)) & 1);
2502                 if (!fieldtx)
2503                     off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2504                 else
2505                     off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2506                 if (val) {
2507                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2508                                              first_block, s->dest[dst_idx] + off,
2509                                              (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2510                                              (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
2511                     block_cbp |= pat << (i << 2);
2512                     if (!v->ttmbf && ttmb < 8)
2513                         ttmb = -1;
2514                     first_block = 0;
2515                 }
2516             }
2517
2518         } else { // skipped
2519             dir = 0;
2520             for (i = 0; i < 6; i++) {
2521                 v->mb_type[0][s->block_index[i]] = 0;
2522                 s->dc_val[0][s->block_index[i]] = 0;
2523             }
2524             s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
2525             s->current_picture.qscale_table[mb_pos] = 0;
2526             v->blk_mv_type[s->block_index[0]] = 0;
2527             v->blk_mv_type[s->block_index[1]] = 0;
2528             v->blk_mv_type[s->block_index[2]] = 0;
2529             v->blk_mv_type[s->block_index[3]] = 0;
2530
2531             if (!direct) {
2532                 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2533                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2534                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2535                 } else {
2536                     dir = bmvtype == BMV_TYPE_BACKWARD;
2537                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2538                     if (mvsw) {
2539                         int dir2 = dir;
2540                         if (mvsw)
2541                             dir2 = !dir;
2542                         for (i = 0; i < 2; i++) {
2543                             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];
2544                             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];
2545                             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];
2546                             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];
2547                         }
2548                     } else {
2549                         v->blk_mv_type[s->block_index[0]] = 1;
2550                         v->blk_mv_type[s->block_index[1]] = 1;
2551                         v->blk_mv_type[s->block_index[2]] = 1;
2552                         v->blk_mv_type[s->block_index[3]] = 1;
2553                         ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2554                         for (i = 0; i < 2; i++) {
2555                             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];
2556                             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];
2557                         }
2558                     }
2559                 }
2560             }
2561
2562             ff_vc1_mc_1mv(v, dir);
2563             if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2564                 ff_vc1_interp_mc(v);
2565             }
2566         }
2567     }
2568     if (s->mb_x == s->mb_width - 1)
2569         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2570     v->cbp[s->mb_x]      = block_cbp;
2571     v->ttblk[s->mb_x]    = block_tt;
2572     return 0;
2573 }
2574
2575 /** Decode blocks of I-frame
2576  */
2577 static void vc1_decode_i_blocks(VC1Context *v)
2578 {
2579     int k, j;
2580     MpegEncContext *s = &v->s;
2581     int cbp, val;
2582     uint8_t *coded_val;
2583     int mb_pos;
2584
2585     /* select codingmode used for VLC tables selection */
2586     switch (v->y_ac_table_index) {
2587     case 0:
2588         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2589         break;
2590     case 1:
2591         v->codingset = CS_HIGH_MOT_INTRA;
2592         break;
2593     case 2:
2594         v->codingset = CS_MID_RATE_INTRA;
2595         break;
2596     }
2597
2598     switch (v->c_ac_table_index) {
2599     case 0:
2600         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2601         break;
2602     case 1:
2603         v->codingset2 = CS_HIGH_MOT_INTER;
2604         break;
2605     case 2:
2606         v->codingset2 = CS_MID_RATE_INTER;
2607         break;
2608     }
2609
2610     /* Set DC scale - y and c use the same */
2611     s->y_dc_scale = s->y_dc_scale_table[v->pq];
2612     s->c_dc_scale = s->c_dc_scale_table[v->pq];
2613
2614     //do frame decode
2615     s->mb_x = s->mb_y = 0;
2616     s->mb_intra         = 1;
2617     s->first_slice_line = 1;
2618     for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2619         s->mb_x = 0;
2620         init_block_index(v);
2621         for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2622             uint8_t *dst[6];
2623             ff_update_block_index(s);
2624             dst[0] = s->dest[0];
2625             dst[1] = dst[0] + 8;
2626             dst[2] = s->dest[0] + s->linesize * 8;
2627             dst[3] = dst[2] + 8;
2628             dst[4] = s->dest[1];
2629             dst[5] = s->dest[2];
2630             s->bdsp.clear_blocks(s->block[0]);
2631             mb_pos = s->mb_x + s->mb_y * s->mb_width;
2632             s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
2633             s->current_picture.qscale_table[mb_pos]                = v->pq;
2634             s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2635             s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2636
2637             // do actual MB decoding and displaying
2638             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2639             v->s.ac_pred = get_bits1(&v->s.gb);
2640
2641             for (k = 0; k < 6; k++) {
2642                 val = ((cbp >> (5 - k)) & 1);
2643
2644                 if (k < 4) {
2645                     int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
2646                     val        = val ^ pred;
2647                     *coded_val = val;
2648                 }
2649                 cbp |= val << (5 - k);
2650
2651                 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2652
2653                 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2654                     continue;
2655                 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2656                 if (v->pq >= 9 && v->overlap) {
2657                     if (v->rangeredfrm)
2658                         for (j = 0; j < 64; j++)
2659                             s->block[k][j] <<= 1;
2660                     s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2661                                                       k & 4 ? s->uvlinesize
2662                                                             : s->linesize);
2663                 } else {
2664                     if (v->rangeredfrm)
2665                         for (j = 0; j < 64; j++)
2666                             s->block[k][j] = (s->block[k][j] - 64) << 1;
2667                     s->idsp.put_pixels_clamped(s->block[k], dst[k],
2668                                                k & 4 ? s->uvlinesize
2669                                                      : s->linesize);
2670                 }
2671             }
2672
2673             if (v->pq >= 9 && v->overlap) {
2674                 if (s->mb_x) {
2675                     v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2676                     v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2677                     if (!(s->flags & CODEC_FLAG_GRAY)) {
2678                         v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2679                         v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2680                     }
2681                 }
2682                 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2683                 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2684                 if (!s->first_slice_line) {
2685                     v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2686                     v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2687                     if (!(s->flags & CODEC_FLAG_GRAY)) {
2688                         v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2689                         v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2690                     }
2691                 }
2692                 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2693                 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2694             }
2695             if (v->s.loop_filter)
2696                 ff_vc1_loop_filter_iblk(v, v->pq);
2697
2698             if (get_bits_count(&s->gb) > v->bits) {
2699                 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2700                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2701                        get_bits_count(&s->gb), v->bits);
2702                 return;
2703             }
2704         }
2705         if (!v->s.loop_filter)
2706             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2707         else if (s->mb_y)
2708             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2709
2710         s->first_slice_line = 0;
2711     }
2712     if (v->s.loop_filter)
2713         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2714
2715     /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2716      * profile, these only differ are when decoding MSS2 rectangles. */
2717     ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2718 }
2719
2720 /** Decode blocks of I-frame for advanced profile
2721  */
2722 static void vc1_decode_i_blocks_adv(VC1Context *v)
2723 {
2724     int k;
2725     MpegEncContext *s = &v->s;
2726     int cbp, val;
2727     uint8_t *coded_val;
2728     int mb_pos;
2729     int mquant = v->pq;
2730     int mqdiff;
2731     GetBitContext *gb = &s->gb;
2732
2733     /* select codingmode used for VLC tables selection */
2734     switch (v->y_ac_table_index) {
2735     case 0:
2736         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2737         break;
2738     case 1:
2739         v->codingset = CS_HIGH_MOT_INTRA;
2740         break;
2741     case 2:
2742         v->codingset = CS_MID_RATE_INTRA;
2743         break;
2744     }
2745
2746     switch (v->c_ac_table_index) {
2747     case 0:
2748         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2749         break;
2750     case 1:
2751         v->codingset2 = CS_HIGH_MOT_INTER;
2752         break;
2753     case 2:
2754         v->codingset2 = CS_MID_RATE_INTER;
2755         break;
2756     }
2757
2758     // do frame decode
2759     s->mb_x             = s->mb_y = 0;
2760     s->mb_intra         = 1;
2761     s->first_slice_line = 1;
2762     s->mb_y             = s->start_mb_y;
2763     if (s->start_mb_y) {
2764         s->mb_x = 0;
2765         init_block_index(v);
2766         memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2767                (1 + s->b8_stride) * sizeof(*s->coded_block));
2768     }
2769     for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2770         s->mb_x = 0;
2771         init_block_index(v);
2772         for (;s->mb_x < s->mb_width; s->mb_x++) {
2773             int16_t (*block)[64] = v->block[v->cur_blk_idx];
2774             ff_update_block_index(s);
2775             s->bdsp.clear_blocks(block[0]);
2776             mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2777             s->current_picture.mb_type[mb_pos + v->mb_off]                         = MB_TYPE_INTRA;
2778             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2779             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2780
2781             // do actual MB decoding and displaying
2782             if (v->fieldtx_is_raw)
2783                 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2784             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2785             if ( v->acpred_is_raw)
2786                 v->s.ac_pred = get_bits1(&v->s.gb);
2787             else
2788                 v->s.ac_pred = v->acpred_plane[mb_pos];
2789
2790             if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2791                 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2792
2793             GET_MQUANT();
2794
2795             s->current_picture.qscale_table[mb_pos] = mquant;
2796             /* Set DC scale - y and c use the same */
2797             s->y_dc_scale = s->y_dc_scale_table[mquant];
2798             s->c_dc_scale = s->c_dc_scale_table[mquant];
2799
2800             for (k = 0; k < 6; k++) {
2801                 val = ((cbp >> (5 - k)) & 1);
2802
2803                 if (k < 4) {
2804                     int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
2805                     val        = val ^ pred;
2806                     *coded_val = val;
2807                 }
2808                 cbp |= val << (5 - k);
2809
2810                 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2811                 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2812
2813                 vc1_decode_i_block_adv(v, block[k], k, val,
2814                                        (k < 4) ? v->codingset : v->codingset2, mquant);
2815
2816                 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2817                     continue;
2818                 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2819             }
2820
2821             ff_vc1_smooth_overlap_filter_iblk(v);
2822             vc1_put_signed_blocks_clamped(v);
2823             if (v->s.loop_filter)
2824                 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2825
2826             if (get_bits_count(&s->gb) > v->bits) {
2827                 // TODO: may need modification to handle slice coding
2828                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2829                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2830                        get_bits_count(&s->gb), v->bits);
2831                 return;
2832             }
2833         }
2834         if (!v->s.loop_filter)
2835             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2836         else if (s->mb_y)
2837             ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2838         s->first_slice_line = 0;
2839     }
2840
2841     /* raw bottom MB row */
2842     s->mb_x = 0;
2843     init_block_index(v);
2844
2845     for (;s->mb_x < s->mb_width; s->mb_x++) {
2846         ff_update_block_index(s);
2847         vc1_put_signed_blocks_clamped(v);
2848         if (v->s.loop_filter)
2849             ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2850     }
2851     if (v->s.loop_filter)
2852         ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
2853     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2854                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2855 }
2856
2857 static void vc1_decode_p_blocks(VC1Context *v)
2858 {
2859     MpegEncContext *s = &v->s;
2860     int apply_loop_filter;
2861
2862     /* select codingmode used for VLC tables selection */
2863     switch (v->c_ac_table_index) {
2864     case 0:
2865         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2866         break;
2867     case 1:
2868         v->codingset = CS_HIGH_MOT_INTRA;
2869         break;
2870     case 2:
2871         v->codingset = CS_MID_RATE_INTRA;
2872         break;
2873     }
2874
2875     switch (v->c_ac_table_index) {
2876     case 0:
2877         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2878         break;
2879     case 1:
2880         v->codingset2 = CS_HIGH_MOT_INTER;
2881         break;
2882     case 2:
2883         v->codingset2 = CS_MID_RATE_INTER;
2884         break;
2885     }
2886
2887     apply_loop_filter   = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2888                           v->fcm == PROGRESSIVE;
2889     s->first_slice_line = 1;
2890     memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2891     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2892         s->mb_x = 0;
2893         init_block_index(v);
2894         for (; s->mb_x < s->mb_width; s->mb_x++) {
2895             ff_update_block_index(s);
2896
2897             if (v->fcm == ILACE_FIELD)
2898                 vc1_decode_p_mb_intfi(v);
2899             else if (v->fcm == ILACE_FRAME)
2900                 vc1_decode_p_mb_intfr(v);
2901             else vc1_decode_p_mb(v);
2902             if (s->mb_y != s->start_mb_y && apply_loop_filter)
2903                 ff_vc1_apply_p_loop_filter(v);
2904             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2905                 // TODO: may need modification to handle slice coding
2906                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2907                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2908                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2909                 return;
2910             }
2911         }
2912         memmove(v->cbp_base,      v->cbp,      sizeof(v->cbp_base[0])      * s->mb_stride);
2913         memmove(v->ttblk_base,    v->ttblk,    sizeof(v->ttblk_base[0])    * s->mb_stride);
2914         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2915         memmove(v->luma_mv_base,  v->luma_mv,  sizeof(v->luma_mv_base[0])  * s->mb_stride);
2916         if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2917         s->first_slice_line = 0;
2918     }
2919     if (apply_loop_filter) {
2920         s->mb_x = 0;
2921         init_block_index(v);
2922         for (; s->mb_x < s->mb_width; s->mb_x++) {
2923             ff_update_block_index(s);
2924             ff_vc1_apply_p_loop_filter(v);
2925         }
2926     }
2927     if (s->end_mb_y >= s->start_mb_y)
2928         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2929     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2930                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2931 }
2932
2933 static void vc1_decode_b_blocks(VC1Context *v)
2934 {
2935     MpegEncContext *s = &v->s;
2936
2937     /* select codingmode used for VLC tables selection */
2938     switch (v->c_ac_table_index) {
2939     case 0:
2940         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2941         break;
2942     case 1:
2943         v->codingset = CS_HIGH_MOT_INTRA;
2944         break;
2945     case 2:
2946         v->codingset = CS_MID_RATE_INTRA;
2947         break;
2948     }
2949
2950     switch (v->c_ac_table_index) {
2951     case 0:
2952         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2953         break;
2954     case 1:
2955         v->codingset2 = CS_HIGH_MOT_INTER;
2956         break;
2957     case 2:
2958         v->codingset2 = CS_MID_RATE_INTER;
2959         break;
2960     }
2961
2962     s->first_slice_line = 1;
2963     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2964         s->mb_x = 0;
2965         init_block_index(v);
2966         for (; s->mb_x < s->mb_width; s->mb_x++) {
2967             ff_update_block_index(s);
2968
2969             if (v->fcm == ILACE_FIELD)
2970                 vc1_decode_b_mb_intfi(v);
2971             else if (v->fcm == ILACE_FRAME)
2972                 vc1_decode_b_mb_intfr(v);
2973             else
2974                 vc1_decode_b_mb(v);
2975             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2976                 // TODO: may need modification to handle slice coding
2977                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2978                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2979                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2980                 return;
2981             }
2982             if (v->s.loop_filter)
2983                 ff_vc1_loop_filter_iblk(v, v->pq);
2984         }
2985         if (!v->s.loop_filter)
2986             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2987         else if (s->mb_y)
2988             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2989         s->first_slice_line = 0;
2990     }
2991     if (v->s.loop_filter)
2992         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2993     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2994                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2995 }
2996
2997 static void vc1_decode_skip_blocks(VC1Context *v)
2998 {
2999     MpegEncContext *s = &v->s;
3000
3001     if (!v->s.last_picture.f->data[0])
3002         return;
3003
3004     ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
3005     s->first_slice_line = 1;
3006     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
3007         s->mb_x = 0;
3008         init_block_index(v);
3009         ff_update_block_index(s);
3010         memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize,   s->linesize   * 16);
3011         memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
3012         memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
3013         ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
3014         s->first_slice_line = 0;
3015     }
3016     s->pict_type = AV_PICTURE_TYPE_P;
3017 }
3018
3019 void ff_vc1_decode_blocks(VC1Context *v)
3020 {
3021
3022     v->s.esc3_level_length = 0;
3023     if (v->x8_type) {
3024         ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
3025     } else {
3026         v->cur_blk_idx     =  0;
3027         v->left_blk_idx    = -1;
3028         v->topleft_blk_idx =  1;
3029         v->top_blk_idx     =  2;
3030         switch (v->s.pict_type) {
3031         case AV_PICTURE_TYPE_I:
3032             if (v->profile == PROFILE_ADVANCED)
3033                 vc1_decode_i_blocks_adv(v);
3034             else
3035                 vc1_decode_i_blocks(v);
3036             break;
3037         case AV_PICTURE_TYPE_P:
3038             if (v->p_frame_skipped)
3039                 vc1_decode_skip_blocks(v);
3040             else
3041                 vc1_decode_p_blocks(v);
3042             break;
3043         case AV_PICTURE_TYPE_B:
3044             if (v->bi_type) {
3045                 if (v->profile == PROFILE_ADVANCED)
3046                     vc1_decode_i_blocks_adv(v);
3047                 else
3048                     vc1_decode_i_blocks(v);
3049             } else
3050                 vc1_decode_b_blocks(v);
3051             break;
3052         }
3053     }
3054 }