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