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