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