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