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