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