avcodec/vc1_block: cosmetics
[ffmpeg.git] / libavcodec / vc1_block.c
1 /*
2  * VC-1 and WMV3 decoder
3  * Copyright (c) 2011 Mashiat Sarker Shakkhar
4  * Copyright (c) 2006-2007 Konstantin Shishkov
5  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 /**
25  * @file
26  * VC-1 and WMV3 block decoding routines
27  */
28
29 #include "avcodec.h"
30 #include "mpegutils.h"
31 #include "mpegvideo.h"
32 #include "msmpeg4data.h"
33 #include "unary.h"
34 #include "vc1.h"
35 #include "vc1_pred.h"
36 #include "vc1acdata.h"
37 #include "vc1data.h"
38
39 #define MB_INTRA_VLC_BITS 9
40 #define DC_VLC_BITS 9
41
42 // offset tables for interlaced picture MVDATA decoding
43 static const uint8_t offset_table[2][9] = {
44     {  0,  1,  2,  4,  8, 16, 32,  64, 128 },
45     {  0,  1,  3,  7, 15, 31, 63, 127, 255 },
46 };
47
48 /***********************************************************************/
49 /**
50  * @name VC-1 Bitplane decoding
51  * @see 8.7, p56
52  * @{
53  */
54
55
56 static inline void init_block_index(VC1Context *v)
57 {
58     MpegEncContext *s = &v->s;
59     ff_init_block_index(s);
60     if (v->field_mode && !(v->second_field ^ v->tff)) {
61         s->dest[0] += s->current_picture_ptr->f->linesize[0];
62         s->dest[1] += s->current_picture_ptr->f->linesize[1];
63         s->dest[2] += s->current_picture_ptr->f->linesize[2];
64     }
65 }
66
67 /** @} */ //Bitplane group
68
69 static void vc1_put_signed_blocks_clamped(VC1Context *v)
70 {
71     MpegEncContext *s = &v->s;
72     int topleft_mb_pos, top_mb_pos;
73     int stride_y, fieldtx = 0;
74     int v_dist;
75
76     /* The put pixels loop is always one MB row behind the decoding loop,
77      * because we can only put pixels when overlap filtering is done, and
78      * for filtering of the bottom edge of a MB, we need the next MB row
79      * present as well.
80      * Within the row, the put pixels loop is also one MB col behind the
81      * decoding loop. The reason for this is again, because for filtering
82      * of the right MB edge, we need the next MB present. */
83     if (!s->first_slice_line) {
84         if (s->mb_x) {
85             topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
86             if (v->fcm == ILACE_FRAME)
87                 fieldtx = v->fieldtx_plane[topleft_mb_pos];
88             stride_y       = s->linesize << fieldtx;
89             v_dist         = (16 - fieldtx) >> (fieldtx == 0);
90             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
91                                               s->dest[0] - 16 * s->linesize - 16,
92                                               stride_y);
93             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
94                                               s->dest[0] - 16 * s->linesize - 8,
95                                               stride_y);
96             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
97                                               s->dest[0] - v_dist * s->linesize - 16,
98                                               stride_y);
99             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
100                                               s->dest[0] - v_dist * s->linesize - 8,
101                                               stride_y);
102             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
103                                               s->dest[1] - 8 * s->uvlinesize - 8,
104                                               s->uvlinesize);
105             s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
106                                               s->dest[2] - 8 * s->uvlinesize - 8,
107                                               s->uvlinesize);
108         }
109         if (s->mb_x == s->mb_width - 1) {
110             top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
111             if (v->fcm == ILACE_FRAME)
112                 fieldtx = v->fieldtx_plane[top_mb_pos];
113             stride_y   = s->linesize << fieldtx;
114             v_dist     = fieldtx ? 15 : 8;
115             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
116                                               s->dest[0] - 16 * s->linesize,
117                                               stride_y);
118             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
119                                               s->dest[0] - 16 * s->linesize + 8,
120                                               stride_y);
121             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
122                                               s->dest[0] - v_dist * s->linesize,
123                                               stride_y);
124             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
125                                               s->dest[0] - v_dist * s->linesize + 8,
126                                               stride_y);
127             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
128                                               s->dest[1] - 8 * s->uvlinesize,
129                                               s->uvlinesize);
130             s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
131                                               s->dest[2] - 8 * s->uvlinesize,
132                                               s->uvlinesize);
133         }
134     }
135
136 #define inc_blk_idx(idx) do { \
137         idx++; \
138         if (idx >= v->n_allocated_blks) \
139             idx = 0; \
140     } while (0)
141
142     inc_blk_idx(v->topleft_blk_idx);
143     inc_blk_idx(v->top_blk_idx);
144     inc_blk_idx(v->left_blk_idx);
145     inc_blk_idx(v->cur_blk_idx);
146 }
147
148 /***********************************************************************/
149 /**
150  * @name VC-1 Block-level functions
151  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
152  * @{
153  */
154
155 /**
156  * @def GET_MQUANT
157  * @brief Get macroblock-level quantizer scale
158  */
159 #define GET_MQUANT()                                           \
160     if (v->dquantfrm) {                                        \
161         int edges = 0;                                         \
162         if (v->dqprofile == DQPROFILE_ALL_MBS) {               \
163             if (v->dqbilevel) {                                \
164                 mquant = (get_bits1(gb)) ? v->altpq : v->pq;   \
165             } else {                                           \
166                 mqdiff = get_bits(gb, 3);                      \
167                 if (mqdiff != 7)                               \
168                     mquant = v->pq + mqdiff;                   \
169                 else                                           \
170                     mquant = get_bits(gb, 5);                  \
171             }                                                  \
172         }                                                      \
173         if (v->dqprofile == DQPROFILE_SINGLE_EDGE)             \
174             edges = 1 << v->dqsbedge;                          \
175         else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES)       \
176             edges = (3 << v->dqsbedge) % 15;                   \
177         else if (v->dqprofile == DQPROFILE_FOUR_EDGES)         \
178             edges = 15;                                        \
179         if ((edges&1) && !s->mb_x)                             \
180             mquant = v->altpq;                                 \
181         if ((edges&2) && s->first_slice_line)                  \
182             mquant = v->altpq;                                 \
183         if ((edges&4) && s->mb_x == (s->mb_width - 1))         \
184             mquant = v->altpq;                                 \
185         if ((edges&8) && s->mb_y == (s->mb_height - 1))        \
186             mquant = v->altpq;                                 \
187         if (!mquant || mquant > 31) {                          \
188             av_log(v->s.avctx, AV_LOG_ERROR,                   \
189                    "Overriding invalid mquant %d\n", mquant);  \
190             mquant = 1;                                        \
191         }                                                      \
192     }
193
194 /**
195  * @def GET_MVDATA(_dmv_x, _dmv_y)
196  * @brief Get MV differentials
197  * @see MVDATA decoding from 8.3.5.2, p(1)20
198  * @param _dmv_x Horizontal differential for decoded MV
199  * @param _dmv_y Vertical differential for decoded MV
200  */
201 #define GET_MVDATA(_dmv_x, _dmv_y)                                      \
202     index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
203                          VC1_MV_DIFF_VLC_BITS, 2);                      \
204     if (index > 36) {                                                   \
205         mb_has_coeffs = 1;                                              \
206         index -= 37;                                                    \
207     } else                                                              \
208         mb_has_coeffs = 0;                                              \
209     s->mb_intra = 0;                                                    \
210     if (!index) {                                                       \
211         _dmv_x = _dmv_y = 0;                                            \
212     } else if (index == 35) {                                           \
213         _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
214         _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
215     } else if (index == 36) {                                           \
216         _dmv_x = 0;                                                     \
217         _dmv_y = 0;                                                     \
218         s->mb_intra = 1;                                                \
219     } else {                                                            \
220         index1 = index % 6;                                             \
221         _dmv_x = offset_table[1][index1];                               \
222         val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
223         if (val > 0) {                                                  \
224             val = get_bits(gb, val);                                    \
225             sign = 0 - (val & 1);                                       \
226             _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign;             \
227         }                                                               \
228                                                                         \
229         index1 = index / 6;                                             \
230         _dmv_y = offset_table[1][index1];                               \
231         val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
232         if (val > 0) {                                                  \
233             val = get_bits(gb, val);                                    \
234             sign = 0 - (val & 1);                                       \
235             _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign;             \
236         }                                                               \
237     }
238
239 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
240                                                    int *dmv_y, int *pred_flag)
241 {
242     int index, index1;
243     int extend_x, extend_y;
244     GetBitContext *gb = &v->s.gb;
245     int bits, esc;
246     int val, sign;
247
248     if (v->numref) {
249         bits = VC1_2REF_MVDATA_VLC_BITS;
250         esc  = 125;
251     } else {
252         bits = VC1_1REF_MVDATA_VLC_BITS;
253         esc  = 71;
254     }
255     extend_x = v->dmvrange & 1;
256     extend_y = (v->dmvrange >> 1) & 1;
257     index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
258     if (index == esc) {
259         *dmv_x = get_bits(gb, v->k_x);
260         *dmv_y = get_bits(gb, v->k_y);
261         if (v->numref) {
262             if (pred_flag)
263                 *pred_flag = *dmv_y & 1;
264             *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
265         }
266     }
267     else {
268         av_assert0(index < esc);
269         index1 = (index + 1) % 9;
270         if (index1 != 0) {
271             val    = get_bits(gb, index1 + extend_x);
272             sign   = 0 - (val & 1);
273             *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
274         } else
275             *dmv_x = 0;
276         index1 = (index + 1) / 9;
277         if (index1 > v->numref) {
278             val    = get_bits(gb, (index1 >> v->numref) + extend_y);
279             sign   = 0 - (val & 1);
280             *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
281         } else
282             *dmv_y = 0;
283         if (v->numref && pred_flag)
284             *pred_flag = index1 & 1;
285     }
286 }
287
288 /** Reconstruct motion vector for B-frame and do motion compensation
289  */
290 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
291                             int direct, int mode)
292 {
293     if (direct) {
294         ff_vc1_mc_1mv(v, 0);
295         ff_vc1_interp_mc(v);
296         return;
297     }
298     if (mode == BMV_TYPE_INTERPOLATED) {
299         ff_vc1_mc_1mv(v, 0);
300         ff_vc1_interp_mc(v);
301         return;
302     }
303
304     ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
305 }
306
307 /** Get predicted DC value for I-frames only
308  * prediction dir: left=0, top=1
309  * @param s MpegEncContext
310  * @param overlap flag indicating that overlap filtering is used
311  * @param pq integer part of picture quantizer
312  * @param[in] n block index in the current MB
313  * @param dc_val_ptr Pointer to DC predictor
314  * @param dir_ptr Prediction direction for use in AC prediction
315  */
316 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
317                                 int16_t **dc_val_ptr, int *dir_ptr)
318 {
319     int a, b, c, wrap, pred, scale;
320     int16_t *dc_val;
321     static const uint16_t dcpred[32] = {
322         -1, 1024,  512,  341,  256,  205,  171,  146,  128,
323              114,  102,   93,   85,   79,   73,   68,   64,
324               60,   57,   54,   51,   49,   47,   45,   43,
325               41,   39,   38,   37,   35,   34,   33
326     };
327
328     /* find prediction - wmv3_dc_scale always used here in fact */
329     if (n < 4) scale = s->y_dc_scale;
330     else       scale = s->c_dc_scale;
331
332     wrap   = s->block_wrap[n];
333     dc_val = s->dc_val[0] + s->block_index[n];
334
335     /* B A
336      * C X
337      */
338     c = dc_val[ - 1];
339     b = dc_val[ - 1 - wrap];
340     a = dc_val[ - wrap];
341
342     if (pq < 9 || !overlap) {
343         /* Set outer values */
344         if (s->first_slice_line && (n != 2 && n != 3))
345             b = a = dcpred[scale];
346         if (s->mb_x == 0 && (n != 1 && n != 3))
347             b = c = dcpred[scale];
348     } else {
349         /* Set outer values */
350         if (s->first_slice_line && (n != 2 && n != 3))
351             b = a = 0;
352         if (s->mb_x == 0 && (n != 1 && n != 3))
353             b = c = 0;
354     }
355
356     if (abs(a - b) <= abs(b - c)) {
357         pred     = c;
358         *dir_ptr = 1; // left
359     } else {
360         pred     = a;
361         *dir_ptr = 0; // top
362     }
363
364     /* update predictor */
365     *dc_val_ptr = &dc_val[0];
366     return pred;
367 }
368
369
370 /** Get predicted DC value
371  * prediction dir: left=0, top=1
372  * @param s MpegEncContext
373  * @param overlap flag indicating that overlap filtering is used
374  * @param pq integer part of picture quantizer
375  * @param[in] n block index in the current MB
376  * @param a_avail flag indicating top block availability
377  * @param c_avail flag indicating left block availability
378  * @param dc_val_ptr Pointer to DC predictor
379  * @param dir_ptr Prediction direction for use in AC prediction
380  */
381 static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
382                               int a_avail, int c_avail,
383                               int16_t **dc_val_ptr, int *dir_ptr)
384 {
385     int a, b, c, wrap, pred;
386     int16_t *dc_val;
387     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
388     int q1, q2 = 0;
389     int dqscale_index;
390
391     /* scale predictors if needed */
392     q1 = s->current_picture.qscale_table[mb_pos];
393     dqscale_index = s->y_dc_scale_table[q1] - 1;
394     if (dqscale_index < 0)
395         return 0;
396
397     wrap = s->block_wrap[n];
398     dc_val = s->dc_val[0] + s->block_index[n];
399
400     /* B A
401      * C X
402      */
403     c = dc_val[ - 1];
404     b = dc_val[ - 1 - wrap];
405     a = dc_val[ - wrap];
406
407     if (c_avail && (n != 1 && n != 3)) {
408         q2 = s->current_picture.qscale_table[mb_pos - 1];
409         if (q2 && q2 != q1)
410             c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
411     }
412     if (a_avail && (n != 2 && n != 3)) {
413         q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
414         if (q2 && q2 != q1)
415             a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
416     }
417     if (a_avail && c_avail && (n != 3)) {
418         int off = mb_pos;
419         if (n != 1)
420             off--;
421         if (n != 2)
422             off -= s->mb_stride;
423         q2 = s->current_picture.qscale_table[off];
424         if (q2 && q2 != q1)
425             b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
426     }
427
428     if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
429         pred     = c;
430         *dir_ptr = 1; // left
431     } else if (a_avail) {
432         pred     = a;
433         *dir_ptr = 0; // top
434     } else {
435         pred     = 0;
436         *dir_ptr = 1; // left
437     }
438
439     /* update predictor */
440     *dc_val_ptr = &dc_val[0];
441     return pred;
442 }
443
444 /** @} */ // Block group
445
446 /**
447  * @name VC1 Macroblock-level functions in Simple/Main Profiles
448  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
449  * @{
450  */
451
452 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
453                                        uint8_t **coded_block_ptr)
454 {
455     int xy, wrap, pred, a, b, c;
456
457     xy   = s->block_index[n];
458     wrap = s->b8_stride;
459
460     /* B C
461      * A X
462      */
463     a = s->coded_block[xy - 1       ];
464     b = s->coded_block[xy - 1 - wrap];
465     c = s->coded_block[xy     - wrap];
466
467     if (b == c) {
468         pred = a;
469     } else {
470         pred = c;
471     }
472
473     /* store value */
474     *coded_block_ptr = &s->coded_block[xy];
475
476     return pred;
477 }
478
479 /**
480  * Decode one AC coefficient
481  * @param v The VC1 context
482  * @param last Last coefficient
483  * @param skip How much zero coefficients to skip
484  * @param value Decoded AC coefficient value
485  * @param codingset set of VLC to decode data
486  * @see 8.1.3.4
487  */
488 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
489                                 int *value, int codingset)
490 {
491     GetBitContext *gb = &v->s.gb;
492     int index, run, level, lst, sign;
493
494     index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
495     if (index != ff_vc1_ac_sizes[codingset] - 1) {
496         run   = vc1_index_decode_table[codingset][index][0];
497         level = vc1_index_decode_table[codingset][index][1];
498         lst   = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
499         sign  = get_bits1(gb);
500     } else {
501         int escape = decode210(gb);
502         if (escape != 2) {
503             index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
504             run   = vc1_index_decode_table[codingset][index][0];
505             level = vc1_index_decode_table[codingset][index][1];
506             lst   = index >= vc1_last_decode_table[codingset];
507             if (escape == 0) {
508                 if (lst)
509                     level += vc1_last_delta_level_table[codingset][run];
510                 else
511                     level += vc1_delta_level_table[codingset][run];
512             } else {
513                 if (lst)
514                     run += vc1_last_delta_run_table[codingset][level] + 1;
515                 else
516                     run += vc1_delta_run_table[codingset][level] + 1;
517             }
518             sign = get_bits1(gb);
519         } else {
520             lst = get_bits1(gb);
521             if (v->s.esc3_level_length == 0) {
522                 if (v->pq < 8 || v->dquantfrm) { // table 59
523                     v->s.esc3_level_length = get_bits(gb, 3);
524                     if (!v->s.esc3_level_length)
525                         v->s.esc3_level_length = get_bits(gb, 2) + 8;
526                 } else { // table 60
527                     v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
528                 }
529                 v->s.esc3_run_length = 3 + get_bits(gb, 2);
530             }
531             run   = get_bits(gb, v->s.esc3_run_length);
532             sign  = get_bits1(gb);
533             level = get_bits(gb, v->s.esc3_level_length);
534         }
535     }
536
537     *last  = lst;
538     *skip  = run;
539     *value = (level ^ -sign) + sign;
540 }
541
542 /** Decode intra block in intra frames - should be faster than decode_intra_block
543  * @param v VC1Context
544  * @param block block to decode
545  * @param[in] n subblock index
546  * @param coded are AC coeffs present or not
547  * @param codingset set of VLC to decode data
548  */
549 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
550                               int coded, int codingset)
551 {
552     GetBitContext *gb = &v->s.gb;
553     MpegEncContext *s = &v->s;
554     int dc_pred_dir = 0; /* Direction of the DC prediction used */
555     int i;
556     int16_t *dc_val;
557     int16_t *ac_val, *ac_val2;
558     int dcdiff;
559
560     /* Get DC differential */
561     if (n < 4) {
562         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
563     } else {
564         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
565     }
566     if (dcdiff < 0) {
567         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
568         return -1;
569     }
570     if (dcdiff) {
571         const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
572         if (dcdiff == 119 /* ESC index value */) {
573             dcdiff = get_bits(gb, 8 + m);
574         } else {
575             if (m)
576                 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
577         }
578         if (get_bits1(gb))
579             dcdiff = -dcdiff;
580     }
581
582     /* Prediction */
583     dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
584     *dc_val = dcdiff;
585
586     /* Store the quantized DC coeff, used for prediction */
587     if (n < 4) {
588         block[0] = dcdiff * s->y_dc_scale;
589     } else {
590         block[0] = dcdiff * s->c_dc_scale;
591     }
592     /* Skip ? */
593     if (!coded) {
594         goto not_coded;
595     }
596
597     // AC Decoding
598     i = 1;
599
600     {
601         int last = 0, skip, value;
602         const uint8_t *zz_table;
603         int scale;
604         int k;
605
606         scale = v->pq * 2 + v->halfpq;
607
608         if (v->s.ac_pred) {
609             if (!dc_pred_dir)
610                 zz_table = v->zz_8x8[2];
611             else
612                 zz_table = v->zz_8x8[3];
613         } else
614             zz_table = v->zz_8x8[1];
615
616         ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
617         ac_val2 = ac_val;
618         if (dc_pred_dir) // left
619             ac_val -= 16;
620         else // top
621             ac_val -= 16 * s->block_wrap[n];
622
623         while (!last) {
624             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
625             i += skip;
626             if (i > 63)
627                 break;
628             block[zz_table[i++]] = value;
629         }
630
631         /* apply AC prediction if needed */
632         if (s->ac_pred) {
633             if (dc_pred_dir) { // left
634                 for (k = 1; k < 8; k++)
635                     block[k << v->left_blk_sh] += ac_val[k];
636             } else { // top
637                 for (k = 1; k < 8; k++)
638                     block[k << v->top_blk_sh] += ac_val[k + 8];
639             }
640         }
641         /* save AC coeffs for further prediction */
642         for (k = 1; k < 8; k++) {
643             ac_val2[k]     = block[k << v->left_blk_sh];
644             ac_val2[k + 8] = block[k << v->top_blk_sh];
645         }
646
647         /* scale AC coeffs */
648         for (k = 1; k < 64; k++)
649             if (block[k]) {
650                 block[k] *= scale;
651                 if (!v->pquantizer)
652                     block[k] += (block[k] < 0) ? -v->pq : v->pq;
653             }
654
655         if (s->ac_pred) i = 63;
656     }
657
658 not_coded:
659     if (!coded) {
660         int k, scale;
661         ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
662         ac_val2 = ac_val;
663
664         i = 0;
665         scale = v->pq * 2 + v->halfpq;
666         memset(ac_val2, 0, 16 * 2);
667         if (dc_pred_dir) { // left
668             ac_val -= 16;
669             if (s->ac_pred)
670                 memcpy(ac_val2, ac_val, 8 * 2);
671         } else { // top
672             ac_val -= 16 * s->block_wrap[n];
673             if (s->ac_pred)
674                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
675         }
676
677         /* apply AC prediction if needed */
678         if (s->ac_pred) {
679             if (dc_pred_dir) { //left
680                 for (k = 1; k < 8; k++) {
681                     block[k << v->left_blk_sh] = ac_val[k] * scale;
682                     if (!v->pquantizer && block[k << v->left_blk_sh])
683                         block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
684                 }
685             } else { // top
686                 for (k = 1; k < 8; k++) {
687                     block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
688                     if (!v->pquantizer && block[k << v->top_blk_sh])
689                         block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
690                 }
691             }
692             i = 63;
693         }
694     }
695     s->block_last_index[n] = i;
696
697     return 0;
698 }
699
700 /** Decode intra block in intra frames - should be faster than decode_intra_block
701  * @param v VC1Context
702  * @param block block to decode
703  * @param[in] n subblock number
704  * @param coded are AC coeffs present or not
705  * @param codingset set of VLC to decode data
706  * @param mquant quantizer value for this macroblock
707  */
708 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
709                                   int coded, int codingset, int mquant)
710 {
711     GetBitContext *gb = &v->s.gb;
712     MpegEncContext *s = &v->s;
713     int dc_pred_dir = 0; /* Direction of the DC prediction used */
714     int i;
715     int16_t *dc_val = NULL;
716     int16_t *ac_val, *ac_val2;
717     int dcdiff;
718     int a_avail = v->a_avail, c_avail = v->c_avail;
719     int use_pred = s->ac_pred;
720     int scale;
721     int q1, q2 = 0;
722     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
723
724     /* Get DC differential */
725     if (n < 4) {
726         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
727     } else {
728         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
729     }
730     if (dcdiff < 0) {
731         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
732         return -1;
733     }
734     if (dcdiff) {
735         const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
736         if (dcdiff == 119 /* ESC index value */) {
737             dcdiff = get_bits(gb, 8 + m);
738         } else {
739             if (m)
740                 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
741         }
742         if (get_bits1(gb))
743             dcdiff = -dcdiff;
744     }
745
746     /* Prediction */
747     dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
748     *dc_val = dcdiff;
749
750     /* Store the quantized DC coeff, used for prediction */
751     if (n < 4) {
752         block[0] = dcdiff * s->y_dc_scale;
753     } else {
754         block[0] = dcdiff * s->c_dc_scale;
755     }
756
757     //AC Decoding
758     i = 1;
759
760     /* check if AC is needed at all */
761     if (!a_avail && !c_avail)
762         use_pred = 0;
763     ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
764     ac_val2 = ac_val;
765
766     scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
767
768     if (dc_pred_dir) // left
769         ac_val -= 16;
770     else // top
771         ac_val -= 16 * s->block_wrap[n];
772
773     q1 = s->current_picture.qscale_table[mb_pos];
774     if ( dc_pred_dir && c_avail && mb_pos)
775         q2 = s->current_picture.qscale_table[mb_pos - 1];
776     if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
777         q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
778     if ( dc_pred_dir && n == 1)
779         q2 = q1;
780     if (!dc_pred_dir && n == 2)
781         q2 = q1;
782     if (n == 3)
783         q2 = q1;
784
785     if (coded) {
786         int last = 0, skip, value;
787         const uint8_t *zz_table;
788         int k;
789
790         if (v->s.ac_pred) {
791             if (!use_pred && v->fcm == ILACE_FRAME) {
792                 zz_table = v->zzi_8x8;
793             } else {
794                 if (!dc_pred_dir) // top
795                     zz_table = v->zz_8x8[2];
796                 else // left
797                     zz_table = v->zz_8x8[3];
798             }
799         } else {
800             if (v->fcm != ILACE_FRAME)
801                 zz_table = v->zz_8x8[1];
802             else
803                 zz_table = v->zzi_8x8;
804         }
805
806         while (!last) {
807             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
808             i += skip;
809             if (i > 63)
810                 break;
811             block[zz_table[i++]] = value;
812         }
813
814         /* apply AC prediction if needed */
815         if (use_pred) {
816             /* scale predictors if needed*/
817             if (q2 && q1 != q2) {
818                 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
819                 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
820
821                 if (q1 < 1)
822                     return AVERROR_INVALIDDATA;
823                 if (dc_pred_dir) { // left
824                     for (k = 1; k < 8; k++)
825                         block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
826                 } else { // top
827                     for (k = 1; k < 8; k++)
828                         block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
829                 }
830             } else {
831                 if (dc_pred_dir) { //left
832                     for (k = 1; k < 8; k++)
833                         block[k << v->left_blk_sh] += ac_val[k];
834                 } else { //top
835                     for (k = 1; k < 8; k++)
836                         block[k << v->top_blk_sh] += ac_val[k + 8];
837                 }
838             }
839         }
840         /* save AC coeffs for further prediction */
841         for (k = 1; k < 8; k++) {
842             ac_val2[k    ] = block[k << v->left_blk_sh];
843             ac_val2[k + 8] = block[k << v->top_blk_sh];
844         }
845
846         /* scale AC coeffs */
847         for (k = 1; k < 64; k++)
848             if (block[k]) {
849                 block[k] *= scale;
850                 if (!v->pquantizer)
851                     block[k] += (block[k] < 0) ? -mquant : mquant;
852             }
853
854         if (use_pred) i = 63;
855     } else { // no AC coeffs
856         int k;
857
858         memset(ac_val2, 0, 16 * 2);
859         if (dc_pred_dir) { // left
860             if (use_pred) {
861                 memcpy(ac_val2, ac_val, 8 * 2);
862                 if (q2 && q1 != q2) {
863                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
864                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
865                     if (q1 < 1)
866                         return AVERROR_INVALIDDATA;
867                     for (k = 1; k < 8; k++)
868                         ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
869                 }
870             }
871         } else { // top
872             if (use_pred) {
873                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
874                 if (q2 && q1 != q2) {
875                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
876                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
877                     if (q1 < 1)
878                         return AVERROR_INVALIDDATA;
879                     for (k = 1; k < 8; k++)
880                         ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
881                 }
882             }
883         }
884
885         /* apply AC prediction if needed */
886         if (use_pred) {
887             if (dc_pred_dir) { // left
888                 for (k = 1; k < 8; k++) {
889                     block[k << v->left_blk_sh] = ac_val2[k] * scale;
890                     if (!v->pquantizer && block[k << v->left_blk_sh])
891                         block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
892                 }
893             } else { // top
894                 for (k = 1; k < 8; k++) {
895                     block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
896                     if (!v->pquantizer && block[k << v->top_blk_sh])
897                         block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
898                 }
899             }
900             i = 63;
901         }
902     }
903     s->block_last_index[n] = i;
904
905     return 0;
906 }
907
908 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
909  * @param v VC1Context
910  * @param block block to decode
911  * @param[in] n subblock index
912  * @param coded are AC coeffs present or not
913  * @param mquant block quantizer
914  * @param codingset set of VLC to decode data
915  */
916 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
917                                   int coded, int mquant, int codingset)
918 {
919     GetBitContext *gb = &v->s.gb;
920     MpegEncContext *s = &v->s;
921     int dc_pred_dir = 0; /* Direction of the DC prediction used */
922     int i;
923     int16_t *dc_val = NULL;
924     int16_t *ac_val, *ac_val2;
925     int dcdiff;
926     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
927     int a_avail = v->a_avail, c_avail = v->c_avail;
928     int use_pred = s->ac_pred;
929     int scale;
930     int q1, q2 = 0;
931
932     s->bdsp.clear_block(block);
933
934     /* XXX: Guard against dumb values of mquant */
935     mquant = av_clip_uintp2(mquant, 5);
936
937     /* Set DC scale - y and c use the same */
938     s->y_dc_scale = s->y_dc_scale_table[mquant];
939     s->c_dc_scale = s->c_dc_scale_table[mquant];
940
941     /* Get DC differential */
942     if (n < 4) {
943         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
944     } else {
945         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
946     }
947     if (dcdiff < 0) {
948         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
949         return -1;
950     }
951     if (dcdiff) {
952         const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
953         if (dcdiff == 119 /* ESC index value */) {
954             dcdiff = get_bits(gb, 8 + m);
955         } else {
956             if (m)
957                 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
958         }
959         if (get_bits1(gb))
960             dcdiff = -dcdiff;
961     }
962
963     /* Prediction */
964     dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
965     *dc_val = dcdiff;
966
967     /* Store the quantized DC coeff, used for prediction */
968
969     if (n < 4) {
970         block[0] = dcdiff * s->y_dc_scale;
971     } else {
972         block[0] = dcdiff * s->c_dc_scale;
973     }
974
975     //AC Decoding
976     i = 1;
977
978     /* check if AC is needed at all and adjust direction if needed */
979     if (!a_avail) dc_pred_dir = 1;
980     if (!c_avail) dc_pred_dir = 0;
981     if (!a_avail && !c_avail) use_pred = 0;
982     ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
983     ac_val2 = ac_val;
984
985     scale = mquant * 2 + v->halfpq;
986
987     if (dc_pred_dir) //left
988         ac_val -= 16;
989     else //top
990         ac_val -= 16 * s->block_wrap[n];
991
992     q1 = s->current_picture.qscale_table[mb_pos];
993     if (dc_pred_dir && c_avail && mb_pos)
994         q2 = s->current_picture.qscale_table[mb_pos - 1];
995     if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
996         q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
997     if ( dc_pred_dir && n == 1)
998         q2 = q1;
999     if (!dc_pred_dir && n == 2)
1000         q2 = q1;
1001     if (n == 3) q2 = q1;
1002
1003     if (coded) {
1004         int last = 0, skip, value;
1005         int k;
1006
1007         while (!last) {
1008             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1009             i += skip;
1010             if (i > 63)
1011                 break;
1012             if (v->fcm == PROGRESSIVE)
1013                 block[v->zz_8x8[0][i++]] = value;
1014             else {
1015                 if (use_pred && (v->fcm == ILACE_FRAME)) {
1016                     if (!dc_pred_dir) // top
1017                         block[v->zz_8x8[2][i++]] = value;
1018                     else // left
1019                         block[v->zz_8x8[3][i++]] = value;
1020                 } else {
1021                     block[v->zzi_8x8[i++]] = value;
1022                 }
1023             }
1024         }
1025
1026         /* apply AC prediction if needed */
1027         if (use_pred) {
1028             /* scale predictors if needed*/
1029             if (q2 && q1 != q2) {
1030                 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1031                 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1032
1033                 if (q1 < 1)
1034                     return AVERROR_INVALIDDATA;
1035                 if (dc_pred_dir) { // left
1036                     for (k = 1; k < 8; k++)
1037                         block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1038                 } else { //top
1039                     for (k = 1; k < 8; k++)
1040                         block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1041                 }
1042             } else {
1043                 if (dc_pred_dir) { // left
1044                     for (k = 1; k < 8; k++)
1045                         block[k << v->left_blk_sh] += ac_val[k];
1046                 } else { // top
1047                     for (k = 1; k < 8; k++)
1048                         block[k << v->top_blk_sh] += ac_val[k + 8];
1049                 }
1050             }
1051         }
1052         /* save AC coeffs for further prediction */
1053         for (k = 1; k < 8; k++) {
1054             ac_val2[k    ] = block[k << v->left_blk_sh];
1055             ac_val2[k + 8] = block[k << v->top_blk_sh];
1056         }
1057
1058         /* scale AC coeffs */
1059         for (k = 1; k < 64; k++)
1060             if (block[k]) {
1061                 block[k] *= scale;
1062                 if (!v->pquantizer)
1063                     block[k] += (block[k] < 0) ? -mquant : mquant;
1064             }
1065
1066         if (use_pred) i = 63;
1067     } else { // no AC coeffs
1068         int k;
1069
1070         memset(ac_val2, 0, 16 * 2);
1071         if (dc_pred_dir) { // left
1072             if (use_pred) {
1073                 memcpy(ac_val2, ac_val, 8 * 2);
1074                 if (q2 && q1 != q2) {
1075                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1076                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1077                     if (q1 < 1)
1078                         return AVERROR_INVALIDDATA;
1079                     for (k = 1; k < 8; k++)
1080                         ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1081                 }
1082             }
1083         } else { // top
1084             if (use_pred) {
1085                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1086                 if (q2 && q1 != q2) {
1087                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1088                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1089                     if (q1 < 1)
1090                         return AVERROR_INVALIDDATA;
1091                     for (k = 1; k < 8; k++)
1092                         ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1093                 }
1094             }
1095         }
1096
1097         /* apply AC prediction if needed */
1098         if (use_pred) {
1099             if (dc_pred_dir) { // left
1100                 for (k = 1; k < 8; k++) {
1101                     block[k << v->left_blk_sh] = ac_val2[k] * scale;
1102                     if (!v->pquantizer && block[k << v->left_blk_sh])
1103                         block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1104                 }
1105             } else { // top
1106                 for (k = 1; k < 8; k++) {
1107                     block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1108                     if (!v->pquantizer && block[k << v->top_blk_sh])
1109                         block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1110                 }
1111             }
1112             i = 63;
1113         }
1114     }
1115     s->block_last_index[n] = i;
1116
1117     return 0;
1118 }
1119
1120 /** Decode P block
1121  */
1122 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1123                               int mquant, int ttmb, int first_block,
1124                               uint8_t *dst, int linesize, int skip_block,
1125                               int *ttmb_out)
1126 {
1127     MpegEncContext *s = &v->s;
1128     GetBitContext *gb = &s->gb;
1129     int i, j;
1130     int subblkpat = 0;
1131     int scale, off, idx, last, skip, value;
1132     int ttblk = ttmb & 7;
1133     int pat = 0;
1134
1135     s->bdsp.clear_block(block);
1136
1137     if (ttmb == -1) {
1138         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)];
1139     }
1140     if (ttblk == TT_4X4) {
1141         subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1142     }
1143     if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1144         && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1145             || (!v->res_rtm_flag && !first_block))) {
1146         subblkpat = decode012(gb);
1147         if (subblkpat)
1148             subblkpat ^= 3; // swap decoded pattern bits
1149         if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1150             ttblk = TT_8X4;
1151         if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1152             ttblk = TT_4X8;
1153     }
1154     scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1155
1156     // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1157     if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1158         subblkpat = 2 - (ttblk == TT_8X4_TOP);
1159         ttblk     = TT_8X4;
1160     }
1161     if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1162         subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1163         ttblk     = TT_4X8;
1164     }
1165     switch (ttblk) {
1166     case TT_8X8:
1167         pat  = 0xF;
1168         i    = 0;
1169         last = 0;
1170         while (!last) {
1171             vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1172             i += skip;
1173             if (i > 63)
1174                 break;
1175             if (!v->fcm)
1176                 idx = v->zz_8x8[0][i++];
1177             else
1178                 idx = v->zzi_8x8[i++];
1179             block[idx] = value * scale;
1180             if (!v->pquantizer)
1181                 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1182         }
1183         if (!skip_block) {
1184             if (i == 1)
1185                 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1186             else {
1187                 v->vc1dsp.vc1_inv_trans_8x8(block);
1188                 s->idsp.add_pixels_clamped(block, dst, linesize);
1189             }
1190         }
1191         break;
1192     case TT_4X4:
1193         pat = ~subblkpat & 0xF;
1194         for (j = 0; j < 4; j++) {
1195             last = subblkpat & (1 << (3 - j));
1196             i    = 0;
1197             off  = (j & 1) * 4 + (j & 2) * 16;
1198             while (!last) {
1199                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1200                 i += skip;
1201                 if (i > 15)
1202                     break;
1203                 if (!v->fcm)
1204                     idx = ff_vc1_simple_progressive_4x4_zz[i++];
1205                 else
1206                     idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1207                 block[idx + off] = value * scale;
1208                 if (!v->pquantizer)
1209                     block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1210             }
1211             if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1212                 if (i == 1)
1213                     v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1214                 else
1215                     v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) *  2 * linesize, linesize, block + off);
1216             }
1217         }
1218         break;
1219     case TT_8X4:
1220         pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1221         for (j = 0; j < 2; j++) {
1222             last = subblkpat & (1 << (1 - j));
1223             i    = 0;
1224             off  = j * 32;
1225             while (!last) {
1226                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1227                 i += skip;
1228                 if (i > 31)
1229                     break;
1230                 if (!v->fcm)
1231                     idx = v->zz_8x4[i++] + off;
1232                 else
1233                     idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1234                 block[idx] = value * scale;
1235                 if (!v->pquantizer)
1236                     block[idx] += (block[idx] < 0) ? -mquant : mquant;
1237             }
1238             if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1239                 if (i == 1)
1240                     v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1241                 else
1242                     v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1243             }
1244         }
1245         break;
1246     case TT_4X8:
1247         pat = ~(subblkpat * 5) & 0xF;
1248         for (j = 0; j < 2; j++) {
1249             last = subblkpat & (1 << (1 - j));
1250             i    = 0;
1251             off  = j * 4;
1252             while (!last) {
1253                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1254                 i += skip;
1255                 if (i > 31)
1256                     break;
1257                 if (!v->fcm)
1258                     idx = v->zz_4x8[i++] + off;
1259                 else
1260                     idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1261                 block[idx] = value * scale;
1262                 if (!v->pquantizer)
1263                     block[idx] += (block[idx] < 0) ? -mquant : mquant;
1264             }
1265             if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1266                 if (i == 1)
1267                     v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1268                 else
1269                     v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1270             }
1271         }
1272         break;
1273     }
1274     if (ttmb_out)
1275         *ttmb_out |= ttblk << (n * 4);
1276     return pat;
1277 }
1278
1279 /** @} */ // Macroblock group
1280
1281 static const uint8_t size_table[6] = { 0, 2, 3, 4,  5,  8 };
1282
1283 /** Decode one P-frame MB
1284  */
1285 static int vc1_decode_p_mb(VC1Context *v)
1286 {
1287     MpegEncContext *s = &v->s;
1288     GetBitContext *gb = &s->gb;
1289     int i, j;
1290     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1291     int cbp; /* cbp decoding stuff */
1292     int mqdiff, mquant; /* MB quantization */
1293     int ttmb = v->ttfrm; /* MB Transform type */
1294
1295     int mb_has_coeffs = 1; /* last_flag */
1296     int dmv_x, dmv_y; /* Differential MV components */
1297     int index, index1; /* LUT indexes */
1298     int val, sign; /* temp values */
1299     int first_block = 1;
1300     int dst_idx, off;
1301     int skipped, fourmv;
1302     int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1303
1304     mquant = v->pq; /* lossy initialization */
1305
1306     if (v->mv_type_is_raw)
1307         fourmv = get_bits1(gb);
1308     else
1309         fourmv = v->mv_type_mb_plane[mb_pos];
1310     if (v->skip_is_raw)
1311         skipped = get_bits1(gb);
1312     else
1313         skipped = v->s.mbskip_table[mb_pos];
1314
1315     if (!fourmv) { /* 1MV mode */
1316         if (!skipped) {
1317             GET_MVDATA(dmv_x, dmv_y);
1318
1319             if (s->mb_intra) {
1320                 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1321                 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1322             }
1323             s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1324             ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1325
1326             /* FIXME Set DC val for inter block ? */
1327             if (s->mb_intra && !mb_has_coeffs) {
1328                 GET_MQUANT();
1329                 s->ac_pred = get_bits1(gb);
1330                 cbp        = 0;
1331             } else if (mb_has_coeffs) {
1332                 if (s->mb_intra)
1333                     s->ac_pred = get_bits1(gb);
1334                 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1335                 GET_MQUANT();
1336             } else {
1337                 mquant = v->pq;
1338                 cbp    = 0;
1339             }
1340             s->current_picture.qscale_table[mb_pos] = mquant;
1341
1342             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1343                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1344                                 VC1_TTMB_VLC_BITS, 2);
1345             if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1346             dst_idx = 0;
1347             for (i = 0; i < 6; i++) {
1348                 s->dc_val[0][s->block_index[i]] = 0;
1349                 dst_idx += i >> 2;
1350                 val = ((cbp >> (5 - i)) & 1);
1351                 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1352                 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1353                 if (s->mb_intra) {
1354                     /* check if prediction blocks A and C are available */
1355                     v->a_avail = v->c_avail = 0;
1356                     if (i == 2 || i == 3 || !s->first_slice_line)
1357                         v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1358                     if (i == 1 || i == 3 || s->mb_x)
1359                         v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1360
1361                     vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1362                                            (i & 4) ? v->codingset2 : v->codingset);
1363                     if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1364                         continue;
1365                     v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1366                     if (v->rangeredfrm)
1367                         for (j = 0; j < 64; j++)
1368                             s->block[i][j] <<= 1;
1369                     s->idsp.put_signed_pixels_clamped(s->block[i],
1370                                                       s->dest[dst_idx] + off,
1371                                                       i & 4 ? s->uvlinesize
1372                                                             : s->linesize);
1373                     if (v->pq >= 9 && v->overlap) {
1374                         if (v->c_avail)
1375                             v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1376                         if (v->a_avail)
1377                             v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1378                     }
1379                     block_cbp   |= 0xF << (i << 2);
1380                     block_intra |= 1 << i;
1381                 } else if (val) {
1382                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
1383                                              s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1384                                              (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1385                     block_cbp |= pat << (i << 2);
1386                     if (!v->ttmbf && ttmb < 8)
1387                         ttmb = -1;
1388                     first_block = 0;
1389                 }
1390             }
1391         } else { // skipped
1392             s->mb_intra = 0;
1393             for (i = 0; i < 6; i++) {
1394                 v->mb_type[0][s->block_index[i]] = 0;
1395                 s->dc_val[0][s->block_index[i]]  = 0;
1396             }
1397             s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
1398             s->current_picture.qscale_table[mb_pos] = 0;
1399             ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1400             ff_vc1_mc_1mv(v, 0);
1401         }
1402     } else { // 4MV mode
1403         if (!skipped /* unskipped MB */) {
1404             int intra_count = 0, coded_inter = 0;
1405             int is_intra[6], is_coded[6];
1406             /* Get CBPCY */
1407             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1408             for (i = 0; i < 6; i++) {
1409                 val = ((cbp >> (5 - i)) & 1);
1410                 s->dc_val[0][s->block_index[i]] = 0;
1411                 s->mb_intra                     = 0;
1412                 if (i < 4) {
1413                     dmv_x = dmv_y = 0;
1414                     s->mb_intra   = 0;
1415                     mb_has_coeffs = 0;
1416                     if (val) {
1417                         GET_MVDATA(dmv_x, dmv_y);
1418                     }
1419                     ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1420                     if (!s->mb_intra)
1421                         ff_vc1_mc_4mv_luma(v, i, 0, 0);
1422                     intra_count += s->mb_intra;
1423                     is_intra[i]  = s->mb_intra;
1424                     is_coded[i]  = mb_has_coeffs;
1425                 }
1426                 if (i & 4) {
1427                     is_intra[i] = (intra_count >= 3);
1428                     is_coded[i] = val;
1429                 }
1430                 if (i == 4)
1431                     ff_vc1_mc_4mv_chroma(v, 0);
1432                 v->mb_type[0][s->block_index[i]] = is_intra[i];
1433                 if (!coded_inter)
1434                     coded_inter = !is_intra[i] & is_coded[i];
1435             }
1436             // if there are no coded blocks then don't do anything more
1437             dst_idx = 0;
1438             if (!intra_count && !coded_inter)
1439                 goto end;
1440             GET_MQUANT();
1441             s->current_picture.qscale_table[mb_pos] = mquant;
1442             /* test if block is intra and has pred */
1443             {
1444                 int intrapred = 0;
1445                 for (i = 0; i < 6; i++)
1446                     if (is_intra[i]) {
1447                         if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1448                             || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1449                             intrapred = 1;
1450                             break;
1451                         }
1452                     }
1453                 if (intrapred)
1454                     s->ac_pred = get_bits1(gb);
1455                 else
1456                     s->ac_pred = 0;
1457             }
1458             if (!v->ttmbf && coded_inter)
1459                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1460             for (i = 0; i < 6; i++) {
1461                 dst_idx    += i >> 2;
1462                 off         = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1463                 s->mb_intra = is_intra[i];
1464                 if (is_intra[i]) {
1465                     /* check if prediction blocks A and C are available */
1466                     v->a_avail = v->c_avail = 0;
1467                     if (i == 2 || i == 3 || !s->first_slice_line)
1468                         v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1469                     if (i == 1 || i == 3 || s->mb_x)
1470                         v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1471
1472                     vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
1473                                            (i & 4) ? v->codingset2 : v->codingset);
1474                     if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1475                         continue;
1476                     v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1477                     if (v->rangeredfrm)
1478                         for (j = 0; j < 64; j++)
1479                             s->block[i][j] <<= 1;
1480                     s->idsp.put_signed_pixels_clamped(s->block[i],
1481                                                       s->dest[dst_idx] + off,
1482                                                       (i & 4) ? s->uvlinesize
1483                                                               : s->linesize);
1484                     if (v->pq >= 9 && v->overlap) {
1485                         if (v->c_avail)
1486                             v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1487                         if (v->a_avail)
1488                             v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1489                     }
1490                     block_cbp   |= 0xF << (i << 2);
1491                     block_intra |= 1 << i;
1492                 } else if (is_coded[i]) {
1493                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1494                                              first_block, s->dest[dst_idx] + off,
1495                                              (i & 4) ? s->uvlinesize : s->linesize,
1496                                              (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1497                                              &block_tt);
1498                     block_cbp |= pat << (i << 2);
1499                     if (!v->ttmbf && ttmb < 8)
1500                         ttmb = -1;
1501                     first_block = 0;
1502                 }
1503             }
1504         } else { // skipped MB
1505             s->mb_intra                               = 0;
1506             s->current_picture.qscale_table[mb_pos] = 0;
1507             for (i = 0; i < 6; i++) {
1508                 v->mb_type[0][s->block_index[i]] = 0;
1509                 s->dc_val[0][s->block_index[i]]  = 0;
1510             }
1511             for (i = 0; i < 4; i++) {
1512                 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1513                 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1514             }
1515             ff_vc1_mc_4mv_chroma(v, 0);
1516             s->current_picture.qscale_table[mb_pos] = 0;
1517         }
1518     }
1519 end:
1520     v->cbp[s->mb_x]      = block_cbp;
1521     v->ttblk[s->mb_x]    = block_tt;
1522     v->is_intra[s->mb_x] = block_intra;
1523
1524     return 0;
1525 }
1526
1527 /* Decode one macroblock in an interlaced frame p picture */
1528
1529 static int vc1_decode_p_mb_intfr(VC1Context *v)
1530 {
1531     MpegEncContext *s = &v->s;
1532     GetBitContext *gb = &s->gb;
1533     int i;
1534     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1535     int cbp = 0; /* cbp decoding stuff */
1536     int mqdiff, mquant; /* MB quantization */
1537     int ttmb = v->ttfrm; /* MB Transform type */
1538
1539     int mb_has_coeffs = 1; /* last_flag */
1540     int dmv_x, dmv_y; /* Differential MV components */
1541     int val; /* temp value */
1542     int first_block = 1;
1543     int dst_idx, off;
1544     int skipped, fourmv = 0, twomv = 0;
1545     int block_cbp = 0, pat, block_tt = 0;
1546     int idx_mbmode = 0, mvbp;
1547     int stride_y, fieldtx;
1548
1549     mquant = v->pq; /* Lossy initialization */
1550
1551     if (v->skip_is_raw)
1552         skipped = get_bits1(gb);
1553     else
1554         skipped = v->s.mbskip_table[mb_pos];
1555     if (!skipped) {
1556         if (v->fourmvswitch)
1557             idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1558         else
1559             idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1560         switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1561         /* store the motion vector type in a flag (useful later) */
1562         case MV_PMODE_INTFR_4MV:
1563             fourmv = 1;
1564             v->blk_mv_type[s->block_index[0]] = 0;
1565             v->blk_mv_type[s->block_index[1]] = 0;
1566             v->blk_mv_type[s->block_index[2]] = 0;
1567             v->blk_mv_type[s->block_index[3]] = 0;
1568             break;
1569         case MV_PMODE_INTFR_4MV_FIELD:
1570             fourmv = 1;
1571             v->blk_mv_type[s->block_index[0]] = 1;
1572             v->blk_mv_type[s->block_index[1]] = 1;
1573             v->blk_mv_type[s->block_index[2]] = 1;
1574             v->blk_mv_type[s->block_index[3]] = 1;
1575             break;
1576         case MV_PMODE_INTFR_2MV_FIELD:
1577             twomv = 1;
1578             v->blk_mv_type[s->block_index[0]] = 1;
1579             v->blk_mv_type[s->block_index[1]] = 1;
1580             v->blk_mv_type[s->block_index[2]] = 1;
1581             v->blk_mv_type[s->block_index[3]] = 1;
1582             break;
1583         case MV_PMODE_INTFR_1MV:
1584             v->blk_mv_type[s->block_index[0]] = 0;
1585             v->blk_mv_type[s->block_index[1]] = 0;
1586             v->blk_mv_type[s->block_index[2]] = 0;
1587             v->blk_mv_type[s->block_index[3]] = 0;
1588             break;
1589         }
1590         if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1591             for (i = 0; i < 4; i++) {
1592                 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1593                 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1594             }
1595             v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1596             s->mb_intra          = 1;
1597             s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1598             fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1599             mb_has_coeffs = get_bits1(gb);
1600             if (mb_has_coeffs)
1601                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1602             v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1603             GET_MQUANT();
1604             s->current_picture.qscale_table[mb_pos] = mquant;
1605             /* Set DC scale - y and c use the same (not sure if necessary here) */
1606             s->y_dc_scale = s->y_dc_scale_table[mquant];
1607             s->c_dc_scale = s->c_dc_scale_table[mquant];
1608             dst_idx = 0;
1609             for (i = 0; i < 6; i++) {
1610                 v->a_avail = v->c_avail          = 0;
1611                 v->mb_type[0][s->block_index[i]] = 1;
1612                 s->dc_val[0][s->block_index[i]]  = 0;
1613                 dst_idx += i >> 2;
1614                 val = ((cbp >> (5 - i)) & 1);
1615                 if (i == 2 || i == 3 || !s->first_slice_line)
1616                     v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1617                 if (i == 1 || i == 3 || s->mb_x)
1618                     v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1619
1620                 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1621                                        (i & 4) ? v->codingset2 : v->codingset);
1622                 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
1623                 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1624                 if (i < 4) {
1625                     stride_y = s->linesize << fieldtx;
1626                     off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1627                 } else {
1628                     stride_y = s->uvlinesize;
1629                     off = 0;
1630                 }
1631                 s->idsp.put_signed_pixels_clamped(s->block[i],
1632                                                   s->dest[dst_idx] + off,
1633                                                   stride_y);
1634                 //TODO: loop filter
1635             }
1636
1637         } else { // inter MB
1638             mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1639             if (mb_has_coeffs)
1640                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1641             if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1642                 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1643             } else {
1644                 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1645                     || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1646                     v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1647                 }
1648             }
1649             s->mb_intra = v->is_intra[s->mb_x] = 0;
1650             for (i = 0; i < 6; i++)
1651                 v->mb_type[0][s->block_index[i]] = 0;
1652             fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1653             /* for all motion vector read MVDATA and motion compensate each block */
1654             dst_idx = 0;
1655             if (fourmv) {
1656                 mvbp = v->fourmvbp;
1657                 for (i = 0; i < 4; i++) {
1658                     dmv_x = dmv_y = 0;
1659                     if (mvbp & (8 >> i))
1660                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1661                     ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1662                     ff_vc1_mc_4mv_luma(v, i, 0, 0);
1663                 }
1664                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1665             } else if (twomv) {
1666                 mvbp  = v->twomvbp;
1667                 dmv_x = dmv_y = 0;
1668                 if (mvbp & 2) {
1669                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1670                 }
1671                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1672                 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1673                 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1674                 dmv_x = dmv_y = 0;
1675                 if (mvbp & 1) {
1676                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1677                 }
1678                 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1679                 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1680                 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1681                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1682             } else {
1683                 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1684                 dmv_x = dmv_y = 0;
1685                 if (mvbp) {
1686                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1687                 }
1688                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1689                 ff_vc1_mc_1mv(v, 0);
1690             }
1691             if (cbp)
1692                 GET_MQUANT();  // p. 227
1693             s->current_picture.qscale_table[mb_pos] = mquant;
1694             if (!v->ttmbf && cbp)
1695                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1696             for (i = 0; i < 6; i++) {
1697                 s->dc_val[0][s->block_index[i]] = 0;
1698                 dst_idx += i >> 2;
1699                 val = ((cbp >> (5 - i)) & 1);
1700                 if (!fieldtx)
1701                     off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1702                 else
1703                     off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1704                 if (val) {
1705                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1706                                              first_block, s->dest[dst_idx] + off,
1707                                              (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1708                                              (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1709                     block_cbp |= pat << (i << 2);
1710                     if (!v->ttmbf && ttmb < 8)
1711                         ttmb = -1;
1712                     first_block = 0;
1713                 }
1714             }
1715         }
1716     } else { // skipped
1717         s->mb_intra = v->is_intra[s->mb_x] = 0;
1718         for (i = 0; i < 6; i++) {
1719             v->mb_type[0][s->block_index[i]] = 0;
1720             s->dc_val[0][s->block_index[i]] = 0;
1721         }
1722         s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
1723         s->current_picture.qscale_table[mb_pos] = 0;
1724         v->blk_mv_type[s->block_index[0]] = 0;
1725         v->blk_mv_type[s->block_index[1]] = 0;
1726         v->blk_mv_type[s->block_index[2]] = 0;
1727         v->blk_mv_type[s->block_index[3]] = 0;
1728         ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1729         ff_vc1_mc_1mv(v, 0);
1730     }
1731     if (s->mb_x == s->mb_width - 1)
1732         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
1733     return 0;
1734 }
1735
1736 static int vc1_decode_p_mb_intfi(VC1Context *v)
1737 {
1738     MpegEncContext *s = &v->s;
1739     GetBitContext *gb = &s->gb;
1740     int i;
1741     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1742     int cbp = 0; /* cbp decoding stuff */
1743     int mqdiff, mquant; /* MB quantization */
1744     int ttmb = v->ttfrm; /* MB Transform type */
1745
1746     int mb_has_coeffs = 1; /* last_flag */
1747     int dmv_x, dmv_y; /* Differential MV components */
1748     int val; /* temp values */
1749     int first_block = 1;
1750     int dst_idx, off;
1751     int pred_flag = 0;
1752     int block_cbp = 0, pat, block_tt = 0;
1753     int idx_mbmode = 0;
1754
1755     mquant = v->pq; /* Lossy initialization */
1756
1757     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1758     if (idx_mbmode <= 1) { // intra MB
1759         v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1760         s->mb_intra          = 1;
1761         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1762         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1763         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1764         GET_MQUANT();
1765         s->current_picture.qscale_table[mb_pos] = mquant;
1766         /* Set DC scale - y and c use the same (not sure if necessary here) */
1767         s->y_dc_scale = s->y_dc_scale_table[mquant];
1768         s->c_dc_scale = s->c_dc_scale_table[mquant];
1769         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
1770         mb_has_coeffs = idx_mbmode & 1;
1771         if (mb_has_coeffs)
1772             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1773         dst_idx = 0;
1774         for (i = 0; i < 6; i++) {
1775             v->a_avail = v->c_avail          = 0;
1776             v->mb_type[0][s->block_index[i]] = 1;
1777             s->dc_val[0][s->block_index[i]]  = 0;
1778             dst_idx += i >> 2;
1779             val = ((cbp >> (5 - i)) & 1);
1780             if (i == 2 || i == 3 || !s->first_slice_line)
1781                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1782             if (i == 1 || i == 3 || s->mb_x)
1783                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1784
1785             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1786                                    (i & 4) ? v->codingset2 : v->codingset);
1787             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1788                 continue;
1789             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1790             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1791             s->idsp.put_signed_pixels_clamped(s->block[i],
1792                                               s->dest[dst_idx] + off,
1793                                               (i & 4) ? s->uvlinesize
1794                                                       : s->linesize);
1795             // TODO: loop filter
1796         }
1797     } else {
1798         s->mb_intra = v->is_intra[s->mb_x] = 0;
1799         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1800         for (i = 0; i < 6; i++)
1801             v->mb_type[0][s->block_index[i]] = 0;
1802         if (idx_mbmode <= 5) { // 1-MV
1803             dmv_x = dmv_y = pred_flag = 0;
1804             if (idx_mbmode & 1) {
1805                 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1806             }
1807             ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1808             ff_vc1_mc_1mv(v, 0);
1809             mb_has_coeffs = !(idx_mbmode & 2);
1810         } else { // 4-MV
1811             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1812             for (i = 0; i < 4; i++) {
1813                 dmv_x = dmv_y = pred_flag = 0;
1814                 if (v->fourmvbp & (8 >> i))
1815                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1816                 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1817                 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1818             }
1819             ff_vc1_mc_4mv_chroma(v, 0);
1820             mb_has_coeffs = idx_mbmode & 1;
1821         }
1822         if (mb_has_coeffs)
1823             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1824         if (cbp) {
1825             GET_MQUANT();
1826         }
1827         s->current_picture.qscale_table[mb_pos] = mquant;
1828         if (!v->ttmbf && cbp) {
1829             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1830         }
1831         dst_idx = 0;
1832         for (i = 0; i < 6; i++) {
1833             s->dc_val[0][s->block_index[i]] = 0;
1834             dst_idx += i >> 2;
1835             val = ((cbp >> (5 - i)) & 1);
1836             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1837             if (val) {
1838                 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1839                                          first_block, s->dest[dst_idx] + off,
1840                                          (i & 4) ? s->uvlinesize : s->linesize,
1841                                          (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1842                                          &block_tt);
1843                 block_cbp |= pat << (i << 2);
1844                 if (!v->ttmbf && ttmb < 8)
1845                     ttmb = -1;
1846                 first_block = 0;
1847             }
1848         }
1849     }
1850     if (s->mb_x == s->mb_width - 1)
1851         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1852     return 0;
1853 }
1854
1855 /** Decode one B-frame MB (in Main profile)
1856  */
1857 static void vc1_decode_b_mb(VC1Context *v)
1858 {
1859     MpegEncContext *s = &v->s;
1860     GetBitContext *gb = &s->gb;
1861     int i, j;
1862     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1863     int cbp = 0; /* cbp decoding stuff */
1864     int mqdiff, mquant; /* MB quantization */
1865     int ttmb = v->ttfrm; /* MB Transform type */
1866     int mb_has_coeffs = 0; /* last_flag */
1867     int index, index1; /* LUT indexes */
1868     int val, sign; /* temp values */
1869     int first_block = 1;
1870     int dst_idx, off;
1871     int skipped, direct;
1872     int dmv_x[2], dmv_y[2];
1873     int bmvtype = BMV_TYPE_BACKWARD;
1874
1875     mquant      = v->pq; /* lossy initialization */
1876     s->mb_intra = 0;
1877
1878     if (v->dmb_is_raw)
1879         direct = get_bits1(gb);
1880     else
1881         direct = v->direct_mb_plane[mb_pos];
1882     if (v->skip_is_raw)
1883         skipped = get_bits1(gb);
1884     else
1885         skipped = v->s.mbskip_table[mb_pos];
1886
1887     dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1888     for (i = 0; i < 6; i++) {
1889         v->mb_type[0][s->block_index[i]] = 0;
1890         s->dc_val[0][s->block_index[i]]  = 0;
1891     }
1892     s->current_picture.qscale_table[mb_pos] = 0;
1893
1894     if (!direct) {
1895         if (!skipped) {
1896             GET_MVDATA(dmv_x[0], dmv_y[0]);
1897             dmv_x[1] = dmv_x[0];
1898             dmv_y[1] = dmv_y[0];
1899         }
1900         if (skipped || !s->mb_intra) {
1901             bmvtype = decode012(gb);
1902             switch (bmvtype) {
1903             case 0:
1904                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1905                 break;
1906             case 1:
1907                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1908                 break;
1909             case 2:
1910                 bmvtype  = BMV_TYPE_INTERPOLATED;
1911                 dmv_x[0] = dmv_y[0] = 0;
1912             }
1913         }
1914     }
1915     for (i = 0; i < 6; i++)
1916         v->mb_type[0][s->block_index[i]] = s->mb_intra;
1917
1918     if (skipped) {
1919         if (direct)
1920             bmvtype = BMV_TYPE_INTERPOLATED;
1921         ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1922         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1923         return;
1924     }
1925     if (direct) {
1926         cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1927         GET_MQUANT();
1928         s->mb_intra = 0;
1929         s->current_picture.qscale_table[mb_pos] = mquant;
1930         if (!v->ttmbf)
1931             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1932         dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1933         ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1934         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1935     } else {
1936         if (!mb_has_coeffs && !s->mb_intra) {
1937             /* no coded blocks - effectively skipped */
1938             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1939             vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1940             return;
1941         }
1942         if (s->mb_intra && !mb_has_coeffs) {
1943             GET_MQUANT();
1944             s->current_picture.qscale_table[mb_pos] = mquant;
1945             s->ac_pred = get_bits1(gb);
1946             cbp = 0;
1947             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1948         } else {
1949             if (bmvtype == BMV_TYPE_INTERPOLATED) {
1950                 GET_MVDATA(dmv_x[0], dmv_y[0]);
1951                 if (!mb_has_coeffs) {
1952                     /* interpolated skipped block */
1953                     ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1954                     vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1955                     return;
1956                 }
1957             }
1958             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1959             if (!s->mb_intra) {
1960                 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1961             }
1962             if (s->mb_intra)
1963                 s->ac_pred = get_bits1(gb);
1964             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1965             GET_MQUANT();
1966             s->current_picture.qscale_table[mb_pos] = mquant;
1967             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1968                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1969         }
1970     }
1971     dst_idx = 0;
1972     for (i = 0; i < 6; i++) {
1973         s->dc_val[0][s->block_index[i]] = 0;
1974         dst_idx += i >> 2;
1975         val = ((cbp >> (5 - i)) & 1);
1976         off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1977         v->mb_type[0][s->block_index[i]] = s->mb_intra;
1978         if (s->mb_intra) {
1979             /* check if prediction blocks A and C are available */
1980             v->a_avail = v->c_avail = 0;
1981             if (i == 2 || i == 3 || !s->first_slice_line)
1982                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1983             if (i == 1 || i == 3 || s->mb_x)
1984                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1985
1986             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1987                                    (i & 4) ? v->codingset2 : v->codingset);
1988             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1989                 continue;
1990             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1991             if (v->rangeredfrm)
1992                 for (j = 0; j < 64; j++)
1993                     s->block[i][j] <<= 1;
1994             s->idsp.put_signed_pixels_clamped(s->block[i],
1995                                               s->dest[dst_idx] + off,
1996                                               i & 4 ? s->uvlinesize
1997                                                     : s->linesize);
1998         } else if (val) {
1999             vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2000                                first_block, s->dest[dst_idx] + off,
2001                                (i & 4) ? s->uvlinesize : s->linesize,
2002                                (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2003             if (!v->ttmbf && ttmb < 8)
2004                 ttmb = -1;
2005             first_block = 0;
2006         }
2007     }
2008 }
2009
2010 /** Decode one B-frame MB (in interlaced field B picture)
2011  */
2012 static void vc1_decode_b_mb_intfi(VC1Context *v)
2013 {
2014     MpegEncContext *s = &v->s;
2015     GetBitContext *gb = &s->gb;
2016     int i, j;
2017     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2018     int cbp = 0; /* cbp decoding stuff */
2019     int mqdiff, mquant; /* MB quantization */
2020     int ttmb = v->ttfrm; /* MB Transform type */
2021     int mb_has_coeffs = 0; /* last_flag */
2022     int val; /* temp value */
2023     int first_block = 1;
2024     int dst_idx, off;
2025     int fwd;
2026     int dmv_x[2], dmv_y[2], pred_flag[2];
2027     int bmvtype = BMV_TYPE_BACKWARD;
2028     int idx_mbmode;
2029
2030     mquant      = v->pq; /* Lossy initialization */
2031     s->mb_intra = 0;
2032
2033     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2034     if (idx_mbmode <= 1) { // intra MB
2035         v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2036         s->mb_intra          = 1;
2037         s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2038         s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2039         s->current_picture.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
2040         GET_MQUANT();
2041         s->current_picture.qscale_table[mb_pos] = mquant;
2042         /* Set DC scale - y and c use the same (not sure if necessary here) */
2043         s->y_dc_scale = s->y_dc_scale_table[mquant];
2044         s->c_dc_scale = s->c_dc_scale_table[mquant];
2045         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
2046         mb_has_coeffs = idx_mbmode & 1;
2047         if (mb_has_coeffs)
2048             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2049         dst_idx = 0;
2050         for (i = 0; i < 6; i++) {
2051             v->a_avail = v->c_avail          = 0;
2052             v->mb_type[0][s->block_index[i]] = 1;
2053             s->dc_val[0][s->block_index[i]]  = 0;
2054             dst_idx += i >> 2;
2055             val = ((cbp >> (5 - i)) & 1);
2056             if (i == 2 || i == 3 || !s->first_slice_line)
2057                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2058             if (i == 1 || i == 3 || s->mb_x)
2059                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2060
2061             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2062                                    (i & 4) ? v->codingset2 : v->codingset);
2063             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2064                 continue;
2065             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2066             if (v->rangeredfrm)
2067                 for (j = 0; j < 64; j++)
2068                     s->block[i][j] <<= 1;
2069             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2070             s->idsp.put_signed_pixels_clamped(s->block[i],
2071                                               s->dest[dst_idx] + off,
2072                                               (i & 4) ? s->uvlinesize
2073                                                       : s->linesize);
2074             // TODO: yet to perform loop filter
2075         }
2076     } else {
2077         s->mb_intra = v->is_intra[s->mb_x] = 0;
2078         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2079         for (i = 0; i < 6; i++)
2080             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     for (; s->mb_x < s->mb_width; s->mb_x++) {
2787         ff_update_block_index(s);
2788         vc1_put_signed_blocks_clamped(v);
2789         if (v->s.loop_filter)
2790             ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2791     }
2792     if (v->s.loop_filter)
2793         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2794     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2795                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2796 }
2797
2798 static void vc1_decode_p_blocks(VC1Context *v)
2799 {
2800     MpegEncContext *s = &v->s;
2801     int apply_loop_filter;
2802
2803     /* select codingmode used for VLC tables selection */
2804     switch (v->c_ac_table_index) {
2805     case 0:
2806         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2807         break;
2808     case 1:
2809         v->codingset = CS_HIGH_MOT_INTRA;
2810         break;
2811     case 2:
2812         v->codingset = CS_MID_RATE_INTRA;
2813         break;
2814     }
2815
2816     switch (v->c_ac_table_index) {
2817     case 0:
2818         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2819         break;
2820     case 1:
2821         v->codingset2 = CS_HIGH_MOT_INTER;
2822         break;
2823     case 2:
2824         v->codingset2 = CS_MID_RATE_INTER;
2825         break;
2826     }
2827
2828     apply_loop_filter   = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2829                           v->fcm == PROGRESSIVE;
2830     s->first_slice_line = 1;
2831     memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2832     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2833         s->mb_x = 0;
2834         init_block_index(v);
2835         for (; s->mb_x < s->mb_width; s->mb_x++) {
2836             ff_update_block_index(s);
2837
2838             if (v->fcm == ILACE_FIELD)
2839                 vc1_decode_p_mb_intfi(v);
2840             else if (v->fcm == ILACE_FRAME)
2841                 vc1_decode_p_mb_intfr(v);
2842             else vc1_decode_p_mb(v);
2843             if (s->mb_y != s->start_mb_y && apply_loop_filter)
2844                 ff_vc1_apply_p_loop_filter(v);
2845             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2846                 // TODO: may need modification to handle slice coding
2847                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2848                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2849                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2850                 return;
2851             }
2852         }
2853         memmove(v->cbp_base,      v->cbp,      sizeof(v->cbp_base[0])      * s->mb_stride);
2854         memmove(v->ttblk_base,    v->ttblk,    sizeof(v->ttblk_base[0])    * s->mb_stride);
2855         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2856         memmove(v->luma_mv_base,  v->luma_mv,  sizeof(v->luma_mv_base[0])  * s->mb_stride);
2857         if (s->mb_y != s->start_mb_y)
2858             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 }