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