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