]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1_block.c
avcodec/videotoolbox: remove unnecessary if statement
[ffmpeg] / 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->avctx->flags & AV_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->avctx->flags & AV_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 (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_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                                              CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_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 (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_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                                              CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_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 (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1590                     continue;
1591                 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1592                 if (i < 4) {
1593                     stride_y = s->linesize << fieldtx;
1594                     off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1595                 } else {
1596                     stride_y = s->uvlinesize;
1597                     off = 0;
1598                 }
1599                 s->idsp.put_signed_pixels_clamped(s->block[i],
1600                                                   s->dest[dst_idx] + off,
1601                                                   stride_y);
1602                 //TODO: loop filter
1603             }
1604
1605         } else { // inter MB
1606             mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1607             if (mb_has_coeffs)
1608                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1609             if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1610                 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1611             } else {
1612                 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1613                     || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1614                     v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1615                 }
1616             }
1617             s->mb_intra = v->is_intra[s->mb_x] = 0;
1618             for (i = 0; i < 6; i++)
1619                 v->mb_type[0][s->block_index[i]] = 0;
1620             fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1621             /* for all motion vector read MVDATA and motion compensate each block */
1622             dst_idx = 0;
1623             if (fourmv) {
1624                 mvbp = v->fourmvbp;
1625                 for (i = 0; i < 4; i++) {
1626                     dmv_x = dmv_y = 0;
1627                     if (mvbp & (8 >> i))
1628                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1629                     ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1630                     ff_vc1_mc_4mv_luma(v, i, 0, 0);
1631                 }
1632                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1633             } else if (twomv) {
1634                 mvbp  = v->twomvbp;
1635                 dmv_x = dmv_y = 0;
1636                 if (mvbp & 2) {
1637                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1638                 }
1639                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1640                 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1641                 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1642                 dmv_x = dmv_y = 0;
1643                 if (mvbp & 1) {
1644                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1645                 }
1646                 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1647                 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1648                 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1649                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1650             } else {
1651                 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1652                 dmv_x = dmv_y = 0;
1653                 if (mvbp) {
1654                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1655                 }
1656                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1657                 ff_vc1_mc_1mv(v, 0);
1658             }
1659             if (cbp)
1660                 GET_MQUANT();  // p. 227
1661             s->current_picture.qscale_table[mb_pos] = mquant;
1662             if (!v->ttmbf && cbp)
1663                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1664             for (i = 0; i < 6; i++) {
1665                 s->dc_val[0][s->block_index[i]] = 0;
1666                 dst_idx += i >> 2;
1667                 val = ((cbp >> (5 - i)) & 1);
1668                 if (!fieldtx)
1669                     off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1670                 else
1671                     off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1672                 if (val) {
1673                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1674                                              first_block, s->dest[dst_idx] + off,
1675                                              (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1676                                              CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1677                     block_cbp |= pat << (i << 2);
1678                     if (!v->ttmbf && ttmb < 8)
1679                         ttmb = -1;
1680                     first_block = 0;
1681                 }
1682             }
1683         }
1684     } else { // skipped
1685         s->mb_intra = v->is_intra[s->mb_x] = 0;
1686         for (i = 0; i < 6; i++) {
1687             v->mb_type[0][s->block_index[i]] = 0;
1688             s->dc_val[0][s->block_index[i]] = 0;
1689         }
1690         s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
1691         s->current_picture.qscale_table[mb_pos] = 0;
1692         v->blk_mv_type[s->block_index[0]] = 0;
1693         v->blk_mv_type[s->block_index[1]] = 0;
1694         v->blk_mv_type[s->block_index[2]] = 0;
1695         v->blk_mv_type[s->block_index[3]] = 0;
1696         ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1697         ff_vc1_mc_1mv(v, 0);
1698     }
1699     if (s->mb_x == s->mb_width - 1)
1700         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
1701     return 0;
1702 }
1703
1704 static int vc1_decode_p_mb_intfi(VC1Context *v)
1705 {
1706     MpegEncContext *s = &v->s;
1707     GetBitContext *gb = &s->gb;
1708     int i;
1709     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1710     int cbp = 0; /* cbp decoding stuff */
1711     int mqdiff, mquant; /* MB quantization */
1712     int ttmb = v->ttfrm; /* MB Transform type */
1713
1714     int mb_has_coeffs = 1; /* last_flag */
1715     int dmv_x, dmv_y; /* Differential MV components */
1716     int val; /* temp values */
1717     int first_block = 1;
1718     int dst_idx, off;
1719     int pred_flag = 0;
1720     int block_cbp = 0, pat, block_tt = 0;
1721     int idx_mbmode = 0;
1722
1723     mquant = v->pq; /* Lossy initialization */
1724
1725     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1726     if (idx_mbmode <= 1) { // intra MB
1727         v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1728         s->mb_intra          = 1;
1729         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1730         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1731         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1732         GET_MQUANT();
1733         s->current_picture.qscale_table[mb_pos] = mquant;
1734         /* Set DC scale - y and c use the same (not sure if necessary here) */
1735         s->y_dc_scale = s->y_dc_scale_table[mquant];
1736         s->c_dc_scale = s->c_dc_scale_table[mquant];
1737         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
1738         mb_has_coeffs = idx_mbmode & 1;
1739         if (mb_has_coeffs)
1740             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1741         dst_idx = 0;
1742         for (i = 0; i < 6; i++) {
1743             v->a_avail = v->c_avail          = 0;
1744             v->mb_type[0][s->block_index[i]] = 1;
1745             s->dc_val[0][s->block_index[i]]  = 0;
1746             dst_idx += i >> 2;
1747             val = ((cbp >> (5 - i)) & 1);
1748             if (i == 2 || i == 3 || !s->first_slice_line)
1749                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1750             if (i == 1 || i == 3 || s->mb_x)
1751                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1752
1753             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1754                                    (i & 4) ? v->codingset2 : v->codingset);
1755             if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1756                 continue;
1757             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1758             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1759             s->idsp.put_signed_pixels_clamped(s->block[i],
1760                                               s->dest[dst_idx] + off,
1761                                               (i & 4) ? s->uvlinesize
1762                                                       : s->linesize);
1763             // TODO: loop filter
1764         }
1765     } else {
1766         s->mb_intra = v->is_intra[s->mb_x] = 0;
1767         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1768         for (i = 0; i < 6; i++)
1769             v->mb_type[0][s->block_index[i]] = 0;
1770         if (idx_mbmode <= 5) { // 1-MV
1771             dmv_x = dmv_y = pred_flag = 0;
1772             if (idx_mbmode & 1) {
1773                 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1774             }
1775             ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1776             ff_vc1_mc_1mv(v, 0);
1777             mb_has_coeffs = !(idx_mbmode & 2);
1778         } else { // 4-MV
1779             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1780             for (i = 0; i < 4; i++) {
1781                 dmv_x = dmv_y = pred_flag = 0;
1782                 if (v->fourmvbp & (8 >> i))
1783                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1784                 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1785                 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1786             }
1787             ff_vc1_mc_4mv_chroma(v, 0);
1788             mb_has_coeffs = idx_mbmode & 1;
1789         }
1790         if (mb_has_coeffs)
1791             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1792         if (cbp) {
1793             GET_MQUANT();
1794         }
1795         s->current_picture.qscale_table[mb_pos] = mquant;
1796         if (!v->ttmbf && cbp) {
1797             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1798         }
1799         dst_idx = 0;
1800         for (i = 0; i < 6; i++) {
1801             s->dc_val[0][s->block_index[i]] = 0;
1802             dst_idx += i >> 2;
1803             val = ((cbp >> (5 - i)) & 1);
1804             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1805             if (val) {
1806                 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1807                                          first_block, s->dest[dst_idx] + off,
1808                                          (i & 4) ? s->uvlinesize : s->linesize,
1809                                          CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1810                                          &block_tt);
1811                 block_cbp |= pat << (i << 2);
1812                 if (!v->ttmbf && ttmb < 8)
1813                     ttmb = -1;
1814                 first_block = 0;
1815             }
1816         }
1817     }
1818     if (s->mb_x == s->mb_width - 1)
1819         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1820     return 0;
1821 }
1822
1823 /** Decode one B-frame MB (in Main profile)
1824  */
1825 static void vc1_decode_b_mb(VC1Context *v)
1826 {
1827     MpegEncContext *s = &v->s;
1828     GetBitContext *gb = &s->gb;
1829     int i, j;
1830     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1831     int cbp = 0; /* cbp decoding stuff */
1832     int mqdiff, mquant; /* MB quantization */
1833     int ttmb = v->ttfrm; /* MB Transform type */
1834     int mb_has_coeffs = 0; /* last_flag */
1835     int index, index1; /* LUT indexes */
1836     int val, sign; /* temp values */
1837     int first_block = 1;
1838     int dst_idx, off;
1839     int skipped, direct;
1840     int dmv_x[2], dmv_y[2];
1841     int bmvtype = BMV_TYPE_BACKWARD;
1842
1843     mquant      = v->pq; /* lossy initialization */
1844     s->mb_intra = 0;
1845
1846     if (v->dmb_is_raw)
1847         direct = get_bits1(gb);
1848     else
1849         direct = v->direct_mb_plane[mb_pos];
1850     if (v->skip_is_raw)
1851         skipped = get_bits1(gb);
1852     else
1853         skipped = v->s.mbskip_table[mb_pos];
1854
1855     dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1856     for (i = 0; i < 6; i++) {
1857         v->mb_type[0][s->block_index[i]] = 0;
1858         s->dc_val[0][s->block_index[i]]  = 0;
1859     }
1860     s->current_picture.qscale_table[mb_pos] = 0;
1861
1862     if (!direct) {
1863         if (!skipped) {
1864             GET_MVDATA(dmv_x[0], dmv_y[0]);
1865             dmv_x[1] = dmv_x[0];
1866             dmv_y[1] = dmv_y[0];
1867         }
1868         if (skipped || !s->mb_intra) {
1869             bmvtype = decode012(gb);
1870             switch (bmvtype) {
1871             case 0:
1872                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1873                 break;
1874             case 1:
1875                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1876                 break;
1877             case 2:
1878                 bmvtype  = BMV_TYPE_INTERPOLATED;
1879                 dmv_x[0] = dmv_y[0] = 0;
1880             }
1881         }
1882     }
1883     for (i = 0; i < 6; i++)
1884         v->mb_type[0][s->block_index[i]] = s->mb_intra;
1885
1886     if (skipped) {
1887         if (direct)
1888             bmvtype = BMV_TYPE_INTERPOLATED;
1889         ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1890         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1891         return;
1892     }
1893     if (direct) {
1894         cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1895         GET_MQUANT();
1896         s->mb_intra = 0;
1897         s->current_picture.qscale_table[mb_pos] = mquant;
1898         if (!v->ttmbf)
1899             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1900         dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1901         ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1902         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1903     } else {
1904         if (!mb_has_coeffs && !s->mb_intra) {
1905             /* no coded blocks - effectively skipped */
1906             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1907             vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1908             return;
1909         }
1910         if (s->mb_intra && !mb_has_coeffs) {
1911             GET_MQUANT();
1912             s->current_picture.qscale_table[mb_pos] = mquant;
1913             s->ac_pred = get_bits1(gb);
1914             cbp = 0;
1915             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1916         } else {
1917             if (bmvtype == BMV_TYPE_INTERPOLATED) {
1918                 GET_MVDATA(dmv_x[0], dmv_y[0]);
1919                 if (!mb_has_coeffs) {
1920                     /* interpolated skipped block */
1921                     ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1922                     vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1923                     return;
1924                 }
1925             }
1926             ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1927             if (!s->mb_intra) {
1928                 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1929             }
1930             if (s->mb_intra)
1931                 s->ac_pred = get_bits1(gb);
1932             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1933             GET_MQUANT();
1934             s->current_picture.qscale_table[mb_pos] = mquant;
1935             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1936                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1937         }
1938     }
1939     dst_idx = 0;
1940     for (i = 0; i < 6; i++) {
1941         s->dc_val[0][s->block_index[i]] = 0;
1942         dst_idx += i >> 2;
1943         val = ((cbp >> (5 - i)) & 1);
1944         off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1945         v->mb_type[0][s->block_index[i]] = s->mb_intra;
1946         if (s->mb_intra) {
1947             /* check if prediction blocks A and C are available */
1948             v->a_avail = v->c_avail = 0;
1949             if (i == 2 || i == 3 || !s->first_slice_line)
1950                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1951             if (i == 1 || i == 3 || s->mb_x)
1952                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1953
1954             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1955                                    (i & 4) ? v->codingset2 : v->codingset);
1956             if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1957                 continue;
1958             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1959             if (v->rangeredfrm)
1960                 for (j = 0; j < 64; j++)
1961                     s->block[i][j] <<= 1;
1962             s->idsp.put_signed_pixels_clamped(s->block[i],
1963                                               s->dest[dst_idx] + off,
1964                                               i & 4 ? s->uvlinesize
1965                                                     : s->linesize);
1966         } else if (val) {
1967             vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1968                                first_block, s->dest[dst_idx] + off,
1969                                (i & 4) ? s->uvlinesize : s->linesize,
1970                                CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
1971             if (!v->ttmbf && ttmb < 8)
1972                 ttmb = -1;
1973             first_block = 0;
1974         }
1975     }
1976 }
1977
1978 /** Decode one B-frame MB (in interlaced field B picture)
1979  */
1980 static void vc1_decode_b_mb_intfi(VC1Context *v)
1981 {
1982     MpegEncContext *s = &v->s;
1983     GetBitContext *gb = &s->gb;
1984     int i, j;
1985     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1986     int cbp = 0; /* cbp decoding stuff */
1987     int mqdiff, mquant; /* MB quantization */
1988     int ttmb = v->ttfrm; /* MB Transform type */
1989     int mb_has_coeffs = 0; /* last_flag */
1990     int val; /* temp value */
1991     int first_block = 1;
1992     int dst_idx, off;
1993     int fwd;
1994     int dmv_x[2], dmv_y[2], pred_flag[2];
1995     int bmvtype = BMV_TYPE_BACKWARD;
1996     int idx_mbmode;
1997
1998     mquant      = v->pq; /* Lossy initialization */
1999     s->mb_intra = 0;
2000
2001     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2002     if (idx_mbmode <= 1) { // intra MB
2003         v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2004         s->mb_intra          = 1;
2005         s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2006         s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2007         s->current_picture.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
2008         GET_MQUANT();
2009         s->current_picture.qscale_table[mb_pos] = mquant;
2010         /* Set DC scale - y and c use the same (not sure if necessary here) */
2011         s->y_dc_scale = s->y_dc_scale_table[mquant];
2012         s->c_dc_scale = s->c_dc_scale_table[mquant];
2013         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
2014         mb_has_coeffs = idx_mbmode & 1;
2015         if (mb_has_coeffs)
2016             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2017         dst_idx = 0;
2018         for (i = 0; i < 6; i++) {
2019             v->a_avail = v->c_avail          = 0;
2020             v->mb_type[0][s->block_index[i]] = 1;
2021             s->dc_val[0][s->block_index[i]]  = 0;
2022             dst_idx += i >> 2;
2023             val = ((cbp >> (5 - i)) & 1);
2024             if (i == 2 || i == 3 || !s->first_slice_line)
2025                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2026             if (i == 1 || i == 3 || s->mb_x)
2027                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2028
2029             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2030                                    (i & 4) ? v->codingset2 : v->codingset);
2031             if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2032                 continue;
2033             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2034             if (v->rangeredfrm)
2035                 for (j = 0; j < 64; j++)
2036                     s->block[i][j] <<= 1;
2037             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2038             s->idsp.put_signed_pixels_clamped(s->block[i],
2039                                               s->dest[dst_idx] + off,
2040                                               (i & 4) ? s->uvlinesize
2041                                                       : s->linesize);
2042             // TODO: yet to perform loop filter
2043         }
2044     } else {
2045         s->mb_intra = v->is_intra[s->mb_x] = 0;
2046         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2047         for (i = 0; i < 6; i++)
2048             v->mb_type[0][s->block_index[i]] = 0;
2049         if (v->fmb_is_raw)
2050             fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2051         else
2052             fwd = v->forward_mb_plane[mb_pos];
2053         if (idx_mbmode <= 5) { // 1-MV
2054             int interpmvp = 0;
2055             dmv_x[0]     = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2056             pred_flag[0] = pred_flag[1] = 0;
2057             if (fwd)
2058                 bmvtype = BMV_TYPE_FORWARD;
2059             else {
2060                 bmvtype = decode012(gb);
2061                 switch (bmvtype) {
2062                 case 0:
2063                     bmvtype = BMV_TYPE_BACKWARD;
2064                     break;
2065                 case 1:
2066                     bmvtype = BMV_TYPE_DIRECT;
2067                     break;
2068                 case 2:
2069                     bmvtype   = BMV_TYPE_INTERPOLATED;
2070                     interpmvp = get_bits1(gb);
2071                 }
2072             }
2073             v->bmvtype = bmvtype;
2074             if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2075                 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2076             }
2077             if (interpmvp) {
2078                 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2079             }
2080             if (bmvtype == BMV_TYPE_DIRECT) {
2081                 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2082                 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2083                 if (!s->next_picture_ptr->field_picture) {
2084                     av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2085                     return;
2086                 }
2087             }
2088             ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2089             vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2090             mb_has_coeffs = !(idx_mbmode & 2);
2091         } else { // 4-MV
2092             if (fwd)
2093                 bmvtype = BMV_TYPE_FORWARD;
2094             v->bmvtype  = bmvtype;
2095             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2096             for (i = 0; i < 4; i++) {
2097                 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2098                 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2099                 if (v->fourmvbp & (8 >> i)) {
2100                     get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2101                                              &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2102                                          &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2103                 }
2104                 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2105                 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2106             }
2107             ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2108             mb_has_coeffs = idx_mbmode & 1;
2109         }
2110         if (mb_has_coeffs)
2111             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2112         if (cbp) {
2113             GET_MQUANT();
2114         }
2115         s->current_picture.qscale_table[mb_pos] = mquant;
2116         if (!v->ttmbf && cbp) {
2117             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2118         }
2119         dst_idx = 0;
2120         for (i = 0; i < 6; i++) {
2121             s->dc_val[0][s->block_index[i]] = 0;
2122             dst_idx += i >> 2;
2123             val = ((cbp >> (5 - i)) & 1);
2124             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2125             if (val) {
2126                 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2127                                    first_block, s->dest[dst_idx] + off,
2128                                    (i & 4) ? s->uvlinesize : s->linesize,
2129                                    CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
2130                 if (!v->ttmbf && ttmb < 8)
2131                     ttmb = -1;
2132                 first_block = 0;
2133             }
2134         }
2135     }
2136 }
2137
2138 /** Decode one B-frame MB (in interlaced frame B picture)
2139  */
2140 static int vc1_decode_b_mb_intfr(VC1Context *v)
2141 {
2142     MpegEncContext *s = &v->s;
2143     GetBitContext *gb = &s->gb;
2144     int i, j;
2145     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2146     int cbp = 0; /* cbp decoding stuff */
2147     int mqdiff, mquant; /* MB quantization */
2148     int ttmb = v->ttfrm; /* MB Transform type */
2149     int mvsw = 0; /* motion vector switch */
2150     int mb_has_coeffs = 1; /* last_flag */
2151     int dmv_x, dmv_y; /* Differential MV components */
2152     int val; /* temp value */
2153     int first_block = 1;
2154     int dst_idx, off;
2155     int skipped, direct, twomv = 0;
2156     int block_cbp = 0, pat, block_tt = 0;
2157     int idx_mbmode = 0, mvbp;
2158     int stride_y, fieldtx;
2159     int bmvtype = BMV_TYPE_BACKWARD;
2160     int dir, dir2;
2161
2162     mquant = v->pq; /* Lossy initialization */
2163     s->mb_intra = 0;
2164     if (v->skip_is_raw)
2165         skipped = get_bits1(gb);
2166     else
2167         skipped = v->s.mbskip_table[mb_pos];
2168
2169     if (!skipped) {
2170         idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2171         if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2172             twomv = 1;
2173             v->blk_mv_type[s->block_index[0]] = 1;
2174             v->blk_mv_type[s->block_index[1]] = 1;
2175             v->blk_mv_type[s->block_index[2]] = 1;
2176             v->blk_mv_type[s->block_index[3]] = 1;
2177         } else {
2178             v->blk_mv_type[s->block_index[0]] = 0;
2179             v->blk_mv_type[s->block_index[1]] = 0;
2180             v->blk_mv_type[s->block_index[2]] = 0;
2181             v->blk_mv_type[s->block_index[3]] = 0;
2182         }
2183     }
2184
2185     if (v->dmb_is_raw)
2186         direct = get_bits1(gb);
2187     else
2188         direct = v->direct_mb_plane[mb_pos];
2189
2190     if (direct) {
2191         if (s->next_picture_ptr->field_picture)
2192             av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2193         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);
2194         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);
2195         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);
2196         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);
2197
2198         if (twomv) {
2199             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);
2200             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);
2201             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);
2202             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);
2203
2204             for (i = 1; i < 4; i += 2) {
2205                 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2206                 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2207                 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2208                 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2209             }
2210         } else {
2211             for (i = 1; i < 4; i++) {
2212                 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2213                 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2214                 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2215                 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2216             }
2217         }
2218     }
2219
2220     if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2221         for (i = 0; i < 4; i++) {
2222             s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2223             s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2224             s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2225             s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2226         }
2227         v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2228         s->mb_intra          = 1;
2229         s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2230         fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2231         mb_has_coeffs = get_bits1(gb);
2232         if (mb_has_coeffs)
2233             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2234         v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2235         GET_MQUANT();
2236         s->current_picture.qscale_table[mb_pos] = mquant;
2237         /* Set DC scale - y and c use the same (not sure if necessary here) */
2238         s->y_dc_scale = s->y_dc_scale_table[mquant];
2239         s->c_dc_scale = s->c_dc_scale_table[mquant];
2240         dst_idx = 0;
2241         for (i = 0; i < 6; i++) {
2242             v->a_avail = v->c_avail          = 0;
2243             v->mb_type[0][s->block_index[i]] = 1;
2244             s->dc_val[0][s->block_index[i]]  = 0;
2245             dst_idx += i >> 2;
2246             val = ((cbp >> (5 - i)) & 1);
2247             if (i == 2 || i == 3 || !s->first_slice_line)
2248                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2249             if (i == 1 || i == 3 || s->mb_x)
2250                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2251
2252             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2253                                    (i & 4) ? v->codingset2 : v->codingset);
2254             if (CONFIG_GRAY && i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2255                 continue;
2256             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2257             if (i < 4) {
2258                 stride_y = s->linesize << fieldtx;
2259                 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2260             } else {
2261                 stride_y = s->uvlinesize;
2262                 off = 0;
2263             }
2264             s->idsp.put_signed_pixels_clamped(s->block[i],
2265                                               s->dest[dst_idx] + off,
2266                                               stride_y);
2267         }
2268     } else {
2269         s->mb_intra = v->is_intra[s->mb_x] = 0;
2270         if (!direct) {
2271             if (skipped || !s->mb_intra) {
2272                 bmvtype = decode012(gb);
2273                 switch (bmvtype) {
2274                 case 0:
2275                     bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2276                     break;
2277                 case 1:
2278                     bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2279                     break;
2280                 case 2:
2281                     bmvtype  = BMV_TYPE_INTERPOLATED;
2282                 }
2283             }
2284
2285             if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2286                 mvsw = get_bits1(gb);
2287         }
2288
2289         if (!skipped) { // inter MB
2290             mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2291             if (mb_has_coeffs)
2292                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2293             if (!direct) {
2294                 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2295                     v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2296                 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2297                     v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2298                 }
2299             }
2300
2301             for (i = 0; i < 6; i++)
2302                 v->mb_type[0][s->block_index[i]] = 0;
2303             fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2304             /* for all motion vector read MVDATA and motion compensate each block */
2305             dst_idx = 0;
2306             if (direct) {
2307                 if (twomv) {
2308                     for (i = 0; i < 4; i++) {
2309                         ff_vc1_mc_4mv_luma(v, i, 0, 0);
2310                         ff_vc1_mc_4mv_luma(v, i, 1, 1);
2311                     }
2312                     ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2313                     ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2314                 } else {
2315                     ff_vc1_mc_1mv(v, 0);
2316                     ff_vc1_interp_mc(v);
2317                 }
2318             } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2319                 mvbp = v->fourmvbp;
2320                 for (i = 0; i < 4; i++) {
2321                     dir = i==1 || i==3;
2322                     dmv_x = dmv_y = 0;
2323                     val = ((mvbp >> (3 - i)) & 1);
2324                     if (val)
2325                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2326                     j = i > 1 ? 2 : 0;
2327                     ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2328                     ff_vc1_mc_4mv_luma(v, j, dir, dir);
2329                     ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2330                 }
2331
2332                 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2333                 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2334             } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2335                 mvbp = v->twomvbp;
2336                 dmv_x = dmv_y = 0;
2337                 if (mvbp & 2)
2338                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2339
2340                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2341                 ff_vc1_mc_1mv(v, 0);
2342
2343                 dmv_x = dmv_y = 0;
2344                 if (mvbp & 1)
2345                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2346
2347                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2348                 ff_vc1_interp_mc(v);
2349             } else if (twomv) {
2350                 dir = bmvtype == BMV_TYPE_BACKWARD;
2351                 dir2 = dir;
2352                 if (mvsw)
2353                     dir2 = !dir;
2354                 mvbp = v->twomvbp;
2355                 dmv_x = dmv_y = 0;
2356                 if (mvbp & 2)
2357                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2358                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2359
2360                 dmv_x = dmv_y = 0;
2361                 if (mvbp & 1)
2362                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2363                 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2364
2365                 if (mvsw) {
2366                     for (i = 0; i < 2; i++) {
2367                         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];
2368                         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];
2369                         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];
2370                         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];
2371                     }
2372                 } else {
2373                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2374                     ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2375                 }
2376
2377                 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2378                 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2379                 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2380                 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2381                 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2382             } else {
2383                 dir = bmvtype == BMV_TYPE_BACKWARD;
2384
2385                 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2386                 dmv_x = dmv_y = 0;
2387                 if (mvbp)
2388                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2389
2390                 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2391                 v->blk_mv_type[s->block_index[0]] = 1;
2392                 v->blk_mv_type[s->block_index[1]] = 1;
2393                 v->blk_mv_type[s->block_index[2]] = 1;
2394                 v->blk_mv_type[s->block_index[3]] = 1;
2395                 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2396                 for (i = 0; i < 2; i++) {
2397                     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];
2398                     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];
2399                 }
2400                 ff_vc1_mc_1mv(v, dir);
2401             }
2402
2403             if (cbp)
2404                 GET_MQUANT();  // p. 227
2405             s->current_picture.qscale_table[mb_pos] = mquant;
2406             if (!v->ttmbf && cbp)
2407                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2408             for (i = 0; i < 6; i++) {
2409                 s->dc_val[0][s->block_index[i]] = 0;
2410                 dst_idx += i >> 2;
2411                 val = ((cbp >> (5 - i)) & 1);
2412                 if (!fieldtx)
2413                     off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2414                 else
2415                     off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2416                 if (val) {
2417                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2418                                              first_block, s->dest[dst_idx] + off,
2419                                              (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2420                                              CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2421                     block_cbp |= pat << (i << 2);
2422                     if (!v->ttmbf && ttmb < 8)
2423                         ttmb = -1;
2424                     first_block = 0;
2425                 }
2426             }
2427
2428         } else { // skipped
2429             dir = 0;
2430             for (i = 0; i < 6; i++) {
2431                 v->mb_type[0][s->block_index[i]] = 0;
2432                 s->dc_val[0][s->block_index[i]] = 0;
2433             }
2434             s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
2435             s->current_picture.qscale_table[mb_pos] = 0;
2436             v->blk_mv_type[s->block_index[0]] = 0;
2437             v->blk_mv_type[s->block_index[1]] = 0;
2438             v->blk_mv_type[s->block_index[2]] = 0;
2439             v->blk_mv_type[s->block_index[3]] = 0;
2440
2441             if (!direct) {
2442                 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2443                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2444                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2445                 } else {
2446                     dir = bmvtype == BMV_TYPE_BACKWARD;
2447                     ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2448                     if (mvsw) {
2449                         int dir2 = dir;
2450                         if (mvsw)
2451                             dir2 = !dir;
2452                         for (i = 0; i < 2; i++) {
2453                             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];
2454                             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];
2455                             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];
2456                             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];
2457                         }
2458                     } else {
2459                         v->blk_mv_type[s->block_index[0]] = 1;
2460                         v->blk_mv_type[s->block_index[1]] = 1;
2461                         v->blk_mv_type[s->block_index[2]] = 1;
2462                         v->blk_mv_type[s->block_index[3]] = 1;
2463                         ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2464                         for (i = 0; i < 2; i++) {
2465                             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];
2466                             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];
2467                         }
2468                     }
2469                 }
2470             }
2471
2472             ff_vc1_mc_1mv(v, dir);
2473             if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2474                 ff_vc1_interp_mc(v);
2475             }
2476         }
2477     }
2478     if (s->mb_x == s->mb_width - 1)
2479         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2480     v->cbp[s->mb_x]      = block_cbp;
2481     v->ttblk[s->mb_x]    = block_tt;
2482     return 0;
2483 }
2484
2485 /** Decode blocks of I-frame
2486  */
2487 static void vc1_decode_i_blocks(VC1Context *v)
2488 {
2489     int k, j;
2490     MpegEncContext *s = &v->s;
2491     int cbp, val;
2492     uint8_t *coded_val;
2493     int mb_pos;
2494
2495     /* select coding mode used for VLC tables selection */
2496     switch (v->y_ac_table_index) {
2497     case 0:
2498         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2499         break;
2500     case 1:
2501         v->codingset = CS_HIGH_MOT_INTRA;
2502         break;
2503     case 2:
2504         v->codingset = CS_MID_RATE_INTRA;
2505         break;
2506     }
2507
2508     switch (v->c_ac_table_index) {
2509     case 0:
2510         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2511         break;
2512     case 1:
2513         v->codingset2 = CS_HIGH_MOT_INTER;
2514         break;
2515     case 2:
2516         v->codingset2 = CS_MID_RATE_INTER;
2517         break;
2518     }
2519
2520     /* Set DC scale - y and c use the same */
2521     s->y_dc_scale = s->y_dc_scale_table[v->pq];
2522     s->c_dc_scale = s->c_dc_scale_table[v->pq];
2523
2524     //do frame decode
2525     s->mb_x = s->mb_y = 0;
2526     s->mb_intra         = 1;
2527     s->first_slice_line = 1;
2528     for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2529         s->mb_x = 0;
2530         init_block_index(v);
2531         for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2532             uint8_t *dst[6];
2533             ff_update_block_index(s);
2534             dst[0] = s->dest[0];
2535             dst[1] = dst[0] + 8;
2536             dst[2] = s->dest[0] + s->linesize * 8;
2537             dst[3] = dst[2] + 8;
2538             dst[4] = s->dest[1];
2539             dst[5] = s->dest[2];
2540             s->bdsp.clear_blocks(s->block[0]);
2541             mb_pos = s->mb_x + s->mb_y * s->mb_width;
2542             s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
2543             s->current_picture.qscale_table[mb_pos]                = v->pq;
2544             s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2545             s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2546
2547             // do actual MB decoding and displaying
2548             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2549             v->s.ac_pred = get_bits1(&v->s.gb);
2550
2551             for (k = 0; k < 6; k++) {
2552                 val = ((cbp >> (5 - k)) & 1);
2553
2554                 if (k < 4) {
2555                     int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
2556                     val        = val ^ pred;
2557                     *coded_val = val;
2558                 }
2559                 cbp |= val << (5 - k);
2560
2561                 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2562
2563                 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2564                     continue;
2565                 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2566                 if (v->pq >= 9 && v->overlap) {
2567                     if (v->rangeredfrm)
2568                         for (j = 0; j < 64; j++)
2569                             s->block[k][j] <<= 1;
2570                     s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2571                                                       k & 4 ? s->uvlinesize
2572                                                             : s->linesize);
2573                 } else {
2574                     if (v->rangeredfrm)
2575                         for (j = 0; j < 64; j++)
2576                             s->block[k][j] = (s->block[k][j] - 64) << 1;
2577                     s->idsp.put_pixels_clamped(s->block[k], dst[k],
2578                                                k & 4 ? s->uvlinesize
2579                                                      : s->linesize);
2580                 }
2581             }
2582
2583             if (v->pq >= 9 && v->overlap) {
2584                 if (s->mb_x) {
2585                     v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2586                     v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2587                     if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2588                         v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2589                         v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2590                     }
2591                 }
2592                 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2593                 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2594                 if (!s->first_slice_line) {
2595                     v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2596                     v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2597                     if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2598                         v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2599                         v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2600                     }
2601                 }
2602                 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2603                 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2604             }
2605             if (v->s.loop_filter)
2606                 ff_vc1_loop_filter_iblk(v, v->pq);
2607
2608             if (get_bits_count(&s->gb) > v->bits) {
2609                 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2610                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2611                        get_bits_count(&s->gb), v->bits);
2612                 return;
2613             }
2614         }
2615         if (!v->s.loop_filter)
2616             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2617         else if (s->mb_y)
2618             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2619
2620         s->first_slice_line = 0;
2621     }
2622     if (v->s.loop_filter)
2623         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2624
2625     /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2626      * profile, these only differ are when decoding MSS2 rectangles. */
2627     ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2628 }
2629
2630 /** Decode blocks of I-frame for advanced profile
2631  */
2632 static void vc1_decode_i_blocks_adv(VC1Context *v)
2633 {
2634     int k;
2635     MpegEncContext *s = &v->s;
2636     int cbp, val;
2637     uint8_t *coded_val;
2638     int mb_pos;
2639     int mquant = v->pq;
2640     int mqdiff;
2641     GetBitContext *gb = &s->gb;
2642
2643     /* select coding mode used for VLC tables selection */
2644     switch (v->y_ac_table_index) {
2645     case 0:
2646         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2647         break;
2648     case 1:
2649         v->codingset = CS_HIGH_MOT_INTRA;
2650         break;
2651     case 2:
2652         v->codingset = CS_MID_RATE_INTRA;
2653         break;
2654     }
2655
2656     switch (v->c_ac_table_index) {
2657     case 0:
2658         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2659         break;
2660     case 1:
2661         v->codingset2 = CS_HIGH_MOT_INTER;
2662         break;
2663     case 2:
2664         v->codingset2 = CS_MID_RATE_INTER;
2665         break;
2666     }
2667
2668     // do frame decode
2669     s->mb_x             = s->mb_y = 0;
2670     s->mb_intra         = 1;
2671     s->first_slice_line = 1;
2672     s->mb_y             = s->start_mb_y;
2673     if (s->start_mb_y) {
2674         s->mb_x = 0;
2675         init_block_index(v);
2676         memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2677                (1 + s->b8_stride) * sizeof(*s->coded_block));
2678     }
2679     for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2680         s->mb_x = 0;
2681         init_block_index(v);
2682         for (;s->mb_x < s->mb_width; s->mb_x++) {
2683             int16_t (*block)[64] = v->block[v->cur_blk_idx];
2684             ff_update_block_index(s);
2685             s->bdsp.clear_blocks(block[0]);
2686             mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2687             s->current_picture.mb_type[mb_pos + v->mb_off]                         = MB_TYPE_INTRA;
2688             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2689             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2690
2691             // do actual MB decoding and displaying
2692             if (v->fieldtx_is_raw)
2693                 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2694             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2695             if (v->acpred_is_raw)
2696                 v->s.ac_pred = get_bits1(&v->s.gb);
2697             else
2698                 v->s.ac_pred = v->acpred_plane[mb_pos];
2699
2700             if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2701                 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2702
2703             GET_MQUANT();
2704
2705             s->current_picture.qscale_table[mb_pos] = mquant;
2706             /* Set DC scale - y and c use the same */
2707             s->y_dc_scale = s->y_dc_scale_table[mquant];
2708             s->c_dc_scale = s->c_dc_scale_table[mquant];
2709
2710             for (k = 0; k < 6; k++) {
2711                 val = ((cbp >> (5 - k)) & 1);
2712
2713                 if (k < 4) {
2714                     int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
2715                     val        = val ^ pred;
2716                     *coded_val = val;
2717                 }
2718                 cbp |= val << (5 - k);
2719
2720                 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2721                 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2722
2723                 vc1_decode_i_block_adv(v, block[k], k, val,
2724                                        (k < 4) ? v->codingset : v->codingset2, mquant);
2725
2726                 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2727                     continue;
2728                 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2729             }
2730
2731             ff_vc1_smooth_overlap_filter_iblk(v);
2732             vc1_put_signed_blocks_clamped(v);
2733             if (v->s.loop_filter)
2734                 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2735
2736             if (get_bits_count(&s->gb) > v->bits) {
2737                 // TODO: may need modification to handle slice coding
2738                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2739                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2740                        get_bits_count(&s->gb), v->bits);
2741                 return;
2742             }
2743         }
2744         if (!v->s.loop_filter)
2745             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2746         else if (s->mb_y)
2747             ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2748         s->first_slice_line = 0;
2749     }
2750
2751     /* raw bottom MB row */
2752     s->mb_x = 0;
2753     init_block_index(v);
2754     for (; s->mb_x < s->mb_width; s->mb_x++) {
2755         ff_update_block_index(s);
2756         vc1_put_signed_blocks_clamped(v);
2757         if (v->s.loop_filter)
2758             ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2759     }
2760     if (v->s.loop_filter)
2761         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2762     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2763                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2764 }
2765
2766 static void vc1_decode_p_blocks(VC1Context *v)
2767 {
2768     MpegEncContext *s = &v->s;
2769     int apply_loop_filter;
2770
2771     /* select coding mode used for VLC tables selection */
2772     switch (v->c_ac_table_index) {
2773     case 0:
2774         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2775         break;
2776     case 1:
2777         v->codingset = CS_HIGH_MOT_INTRA;
2778         break;
2779     case 2:
2780         v->codingset = CS_MID_RATE_INTRA;
2781         break;
2782     }
2783
2784     switch (v->c_ac_table_index) {
2785     case 0:
2786         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2787         break;
2788     case 1:
2789         v->codingset2 = CS_HIGH_MOT_INTER;
2790         break;
2791     case 2:
2792         v->codingset2 = CS_MID_RATE_INTER;
2793         break;
2794     }
2795
2796     apply_loop_filter   = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2797                           v->fcm == PROGRESSIVE;
2798     s->first_slice_line = 1;
2799     memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2800     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2801         s->mb_x = 0;
2802         init_block_index(v);
2803         for (; s->mb_x < s->mb_width; s->mb_x++) {
2804             ff_update_block_index(s);
2805
2806             if (v->fcm == ILACE_FIELD)
2807                 vc1_decode_p_mb_intfi(v);
2808             else if (v->fcm == ILACE_FRAME)
2809                 vc1_decode_p_mb_intfr(v);
2810             else vc1_decode_p_mb(v);
2811             if (s->mb_y != s->start_mb_y && apply_loop_filter)
2812                 ff_vc1_apply_p_loop_filter(v);
2813             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2814                 // TODO: may need modification to handle slice coding
2815                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2816                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2817                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2818                 return;
2819             }
2820         }
2821         memmove(v->cbp_base,      v->cbp,      sizeof(v->cbp_base[0])      * s->mb_stride);
2822         memmove(v->ttblk_base,    v->ttblk,    sizeof(v->ttblk_base[0])    * s->mb_stride);
2823         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2824         memmove(v->luma_mv_base,  v->luma_mv,  sizeof(v->luma_mv_base[0])  * s->mb_stride);
2825         if (s->mb_y != s->start_mb_y)
2826             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2827         s->first_slice_line = 0;
2828     }
2829     if (apply_loop_filter) {
2830         s->mb_x = 0;
2831         init_block_index(v);
2832         for (; s->mb_x < s->mb_width; s->mb_x++) {
2833             ff_update_block_index(s);
2834             ff_vc1_apply_p_loop_filter(v);
2835         }
2836     }
2837     if (s->end_mb_y >= s->start_mb_y)
2838         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2839     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2840                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2841 }
2842
2843 static void vc1_decode_b_blocks(VC1Context *v)
2844 {
2845     MpegEncContext *s = &v->s;
2846
2847     /* select coding mode used for VLC tables selection */
2848     switch (v->c_ac_table_index) {
2849     case 0:
2850         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2851         break;
2852     case 1:
2853         v->codingset = CS_HIGH_MOT_INTRA;
2854         break;
2855     case 2:
2856         v->codingset = CS_MID_RATE_INTRA;
2857         break;
2858     }
2859
2860     switch (v->c_ac_table_index) {
2861     case 0:
2862         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2863         break;
2864     case 1:
2865         v->codingset2 = CS_HIGH_MOT_INTER;
2866         break;
2867     case 2:
2868         v->codingset2 = CS_MID_RATE_INTER;
2869         break;
2870     }
2871
2872     s->first_slice_line = 1;
2873     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2874         s->mb_x = 0;
2875         init_block_index(v);
2876         for (; s->mb_x < s->mb_width; s->mb_x++) {
2877             ff_update_block_index(s);
2878
2879             if (v->fcm == ILACE_FIELD)
2880                 vc1_decode_b_mb_intfi(v);
2881             else if (v->fcm == ILACE_FRAME)
2882                 vc1_decode_b_mb_intfr(v);
2883             else
2884                 vc1_decode_b_mb(v);
2885             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2886                 // TODO: may need modification to handle slice coding
2887                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2888                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2889                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2890                 return;
2891             }
2892             if (v->s.loop_filter)
2893                 ff_vc1_loop_filter_iblk(v, v->pq);
2894         }
2895         if (!v->s.loop_filter)
2896             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2897         else if (s->mb_y)
2898             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2899         s->first_slice_line = 0;
2900     }
2901     if (v->s.loop_filter)
2902         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2903     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2904                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2905 }
2906
2907 static void vc1_decode_skip_blocks(VC1Context *v)
2908 {
2909     MpegEncContext *s = &v->s;
2910
2911     if (!v->s.last_picture.f->data[0])
2912         return;
2913
2914     ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2915     s->first_slice_line = 1;
2916     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2917         s->mb_x = 0;
2918         init_block_index(v);
2919         ff_update_block_index(s);
2920         memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize,   s->linesize   * 16);
2921         memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
2922         memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
2923         ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2924         s->first_slice_line = 0;
2925     }
2926     s->pict_type = AV_PICTURE_TYPE_P;
2927 }
2928
2929 void ff_vc1_decode_blocks(VC1Context *v)
2930 {
2931
2932     v->s.esc3_level_length = 0;
2933     if (v->x8_type) {
2934         ff_intrax8_decode_picture(&v->x8, &v->s.current_picture,
2935                                   &v->s.gb, &v->s.mb_x, &v->s.mb_y,
2936                                   2 * v->pq + v->halfpq, v->pq * !v->pquantizer,
2937                                   v->s.loop_filter, v->s.low_delay);
2938
2939         ff_er_add_slice(&v->s.er, 0, 0,
2940                         (v->s.mb_x >> 1) - 1, (v->s.mb_y >> 1) - 1,
2941                         ER_MB_END);
2942     } else {
2943         v->cur_blk_idx     =  0;
2944         v->left_blk_idx    = -1;
2945         v->topleft_blk_idx =  1;
2946         v->top_blk_idx     =  2;
2947         switch (v->s.pict_type) {
2948         case AV_PICTURE_TYPE_I:
2949             if (v->profile == PROFILE_ADVANCED)
2950                 vc1_decode_i_blocks_adv(v);
2951             else
2952                 vc1_decode_i_blocks(v);
2953             break;
2954         case AV_PICTURE_TYPE_P:
2955             if (v->p_frame_skipped)
2956                 vc1_decode_skip_blocks(v);
2957             else
2958                 vc1_decode_p_blocks(v);
2959             break;
2960         case AV_PICTURE_TYPE_B:
2961             if (v->bi_type) {
2962                 if (v->profile == PROFILE_ADVANCED)
2963                     vc1_decode_i_blocks_adv(v);
2964                 else
2965                     vc1_decode_i_blocks(v);
2966             } else
2967                 vc1_decode_b_blocks(v);
2968             break;
2969         }
2970     }
2971 }