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
7 * This file is part of FFmpeg.
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.
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.
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
26 * VC-1 and WMV3 block decoding routines
30 #include "mpegutils.h"
31 #include "mpegvideo.h"
32 #include "mpegvideodec.h"
33 #include "msmpeg4data.h"
37 #include "vc1acdata.h"
40 #define MB_INTRA_VLC_BITS 9
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 },
49 // mapping table for internal block representation
50 static const int block_map
[6] = {0, 2, 1, 3, 4, 5};
52 /***********************************************************************/
54 * @name VC-1 Bitplane decoding
60 static inline void init_block_index(VC1Context
*v
)
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];
71 /** @} */ //Bitplane group
73 static void vc1_put_blocks_clamped(VC1Context
*v
, int put_signed
)
75 MpegEncContext
*s
= &v
->s
;
77 int block_count
= CONFIG_GRAY
&& (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
) ? 4 : 6;
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
) {
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;
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
);
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
);
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;
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
);
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
);
120 if (s
->mb_y
== s
->end_mb_y
- 1 || v
->fcm
== ILACE_FRAME
) {
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]) {
128 dest
= s
->dest
[0] + ((i
& 2) >> 1) * s
->linesize
+ ((i
& 1) - 2) * 8;
130 dest
= s
->dest
[0] + (i
& 2) * 4 * s
->linesize
+ ((i
& 1) - 2) * 8;
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
);
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
);
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
]]) {
148 dest
= s
->dest
[0] + ((i
& 2) >> 1) * s
->linesize
+ (i
& 1) * 8;
150 dest
= s
->dest
[0] + (i
& 2) * 4 * s
->linesize
+ (i
& 1) * 8;
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
);
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
);
165 #define inc_blk_idx(idx) do { \
167 if (idx >= v->n_allocated_blks) \
171 /***********************************************************************/
173 * @name VC-1 Block-level functions
174 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
180 * @brief Get macroblock-level quantizer scale
182 #define GET_MQUANT() \
183 if (v->dquantfrm) { \
185 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
186 if (v->dqbilevel) { \
187 mquant = (get_bits1(gb)) ? -v->altpq : v->pq; \
189 mqdiff = get_bits(gb, 3); \
191 mquant = -v->pq - mqdiff; \
193 mquant = -get_bits(gb, 5); \
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) \
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; \
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); \
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
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); \
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) { \
244 index1 = index % 6; \
245 _dmv_x = offset_table[1][index1]; \
246 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
248 val = get_bits(gb, val); \
249 sign = 0 - (val & 1); \
250 _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign; \
253 index1 = index / 6; \
254 _dmv_y = offset_table[1][index1]; \
255 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
257 val = get_bits(gb, val); \
258 sign = 0 - (val & 1); \
259 _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign; \
263 static av_always_inline
void get_mvdata_interlaced(VC1Context
*v
, int *dmv_x
,
264 int *dmv_y
, int *pred_flag
)
267 int extend_x
, extend_y
;
268 GetBitContext
*gb
= &v
->s
.gb
;
273 bits
= VC1_2REF_MVDATA_VLC_BITS
;
276 bits
= VC1_1REF_MVDATA_VLC_BITS
;
279 extend_x
= v
->dmvrange
& 1;
280 extend_y
= (v
->dmvrange
>> 1) & 1;
281 index
= get_vlc2(gb
, v
->imv_vlc
->table
, bits
, 3);
283 *dmv_x
= get_bits(gb
, v
->k_x
);
284 *dmv_y
= get_bits(gb
, v
->k_y
);
287 *pred_flag
= *dmv_y
& 1;
288 *dmv_y
= (*dmv_y
+ (*dmv_y
& 1)) >> 1;
292 av_assert0(index
< esc
);
293 index1
= (index
+ 1) % 9;
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
;
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
;
307 if (v
->numref
&& pred_flag
)
308 *pred_flag
= index1
& 1;
312 /** Reconstruct motion vector for B-frame and do motion compensation
314 static inline void vc1_b_mc(VC1Context
*v
, int dmv_x
[2], int dmv_y
[2],
315 int direct
, int mode
)
322 if (mode
== BMV_TYPE_INTERPOLATED
) {
328 ff_vc1_mc_1mv(v
, (mode
== BMV_TYPE_BACKWARD
));
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
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
)
343 int a
, b
, c
, wrap
, pred
, scale
;
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
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
;
356 wrap
= s
->block_wrap
[n
];
357 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
363 b
= dc_val
[ - 1 - wrap
];
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
];
373 /* Set outer values */
374 if (s
->first_slice_line
&& (n
!= 2 && n
!= 3))
376 if (s
->mb_x
== 0 && (n
!= 1 && n
!= 3))
380 if (abs(a
- b
) <= abs(b
- c
)) {
382 *dir_ptr
= 1; // left
388 /* update predictor */
389 *dc_val_ptr
= &dc_val
[0];
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
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
)
409 int a
, b
, c
, wrap
, pred
;
411 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
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)
421 wrap
= s
->block_wrap
[n
];
422 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
428 b
= dc_val
[ - 1 - wrap
];
431 if (c_avail
&& (n
!= 1 && n
!= 3)) {
432 q2
= FFABS(s
->current_picture
.qscale_table
[mb_pos
- 1]);
434 c
= (int)((unsigned)c
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[dqscale_index
] + 0x20000) >> 18;
436 if (a_avail
&& (n
!= 2 && n
!= 3)) {
437 q2
= FFABS(s
->current_picture
.qscale_table
[mb_pos
- s
->mb_stride
]);
439 a
= (int)((unsigned)a
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[dqscale_index
] + 0x20000) >> 18;
441 if (a_avail
&& c_avail
&& (n
!= 3)) {
447 q2
= FFABS(s
->current_picture
.qscale_table
[off
]);
449 b
= (int)((unsigned)b
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[dqscale_index
] + 0x20000) >> 18;
452 if (c_avail
&& (!a_avail
|| abs(a
- b
) <= abs(b
- c
))) {
454 *dir_ptr
= 1; // left
455 } else if (a_avail
) {
460 *dir_ptr
= 1; // left
463 /* update predictor */
464 *dc_val_ptr
= &dc_val
[0];
468 /** @} */ // Block group
471 * @name VC1 Macroblock-level functions in Simple/Main Profiles
472 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
476 static inline int vc1_coded_block_pred(MpegEncContext
* s
, int n
,
477 uint8_t **coded_block_ptr
)
479 int xy
, wrap
, pred
, a
, b
, c
;
481 xy
= s
->block_index
[n
];
487 a
= s
->coded_block
[xy
- 1 ];
488 b
= s
->coded_block
[xy
- 1 - wrap
];
489 c
= s
->coded_block
[xy
- wrap
];
498 *coded_block_ptr
= &s
->coded_block
[xy
];
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
512 static int vc1_decode_ac_coeff(VC1Context
*v
, int *last
, int *skip
,
513 int *value
, int codingset
)
515 GetBitContext
*gb
= &v
->s
.gb
;
516 int index
, run
, level
, lst
, sign
;
518 index
= get_vlc2(gb
, ff_vc1_ac_coeff_table
[codingset
].table
, AC_VLC_BITS
, 3);
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
);
527 int escape
= decode210(gb
);
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
];
537 level
+= vc1_last_delta_level_table
[codingset
][run
];
539 level
+= vc1_delta_level_table
[codingset
][run
];
542 run
+= vc1_last_delta_run_table
[codingset
][level
] + 1;
544 run
+= vc1_delta_run_table
[codingset
][level
] + 1;
546 sign
= 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;
555 v
->s
.esc3_level_length
= get_unary(gb
, 1, 6) + 2;
557 v
->s
.esc3_run_length
= 3 + get_bits(gb
, 2);
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
);
567 *value
= (level
^ -sign
) + sign
;
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
579 static int vc1_decode_i_block(VC1Context
*v
, int16_t block
[64], int n
,
580 int coded
, int codingset
)
582 GetBitContext
*gb
= &v
->s
.gb
;
583 MpegEncContext
*s
= &v
->s
;
584 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
587 int16_t *ac_val
, *ac_val2
;
590 /* Get DC differential */
592 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
594 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
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
);
602 dcdiff
= (dcdiff
<< m
) + get_bits(gb
, m
) - ((1 << m
) - 1);
609 dcdiff
+= vc1_i_pred_dc(&v
->s
, v
->overlap
, v
->pq
, n
, &dc_val
, &dc_pred_dir
);
612 /* Store the quantized DC coeff, used for prediction */
614 scale
= s
->y_dc_scale
;
616 scale
= s
->c_dc_scale
;
617 block
[0] = dcdiff
* scale
;
619 ac_val
= s
->ac_val
[0][s
->block_index
[n
]];
621 if (dc_pred_dir
) // left
624 ac_val
-= 16 * s
->block_wrap
[n
];
626 scale
= v
->pq
* 2 + v
->halfpq
;
632 int last
= 0, skip
, value
;
633 const uint8_t *zz_table
;
638 zz_table
= v
->zz_8x8
[2];
640 zz_table
= v
->zz_8x8
[3];
642 zz_table
= v
->zz_8x8
[1];
645 int ret
= vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
651 block
[zz_table
[i
++]] = value
;
654 /* apply AC prediction if needed */
657 if (dc_pred_dir
) { // left
663 for (k
= 1; k
< 8; k
++)
664 block
[k
<< sh
] += ac_val
[k
];
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
];
672 /* scale AC coeffs */
673 for (k
= 1; k
< 64; k
++)
677 block
[k
] += (block
[k
] < 0) ? -v
->pq
: v
->pq
;
683 memset(ac_val2
, 0, 16 * 2);
685 /* apply AC prediction if needed */
688 if (dc_pred_dir
) { //left
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
;
703 if (s
->ac_pred
) i
= 63;
704 s
->block_last_index
[n
] = i
;
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
717 static int vc1_decode_i_block_adv(VC1Context
*v
, int16_t block
[64], int n
,
718 int coded
, int codingset
, int mquant
)
720 GetBitContext
*gb
= &v
->s
.gb
;
721 MpegEncContext
*s
= &v
->s
;
722 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
724 int16_t *dc_val
= NULL
;
725 int16_t *ac_val
, *ac_val2
;
727 int a_avail
= v
->a_avail
, c_avail
= v
->c_avail
;
728 int use_pred
= s
->ac_pred
;
731 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
732 int quant
= FFABS(mquant
);
734 /* Get DC differential */
736 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
738 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
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
);
746 dcdiff
= (dcdiff
<< m
) + get_bits(gb
, m
) - ((1 << m
) - 1);
753 dcdiff
+= ff_vc1_pred_dc(&v
->s
, v
->overlap
, quant
, n
, v
->a_avail
, v
->c_avail
, &dc_val
, &dc_pred_dir
);
756 /* Store the quantized DC coeff, used for prediction */
758 scale
= s
->y_dc_scale
;
760 scale
= s
->c_dc_scale
;
761 block
[0] = dcdiff
* scale
;
763 /* check if AC is needed at all */
764 if (!a_avail
&& !c_avail
)
767 scale
= quant
* 2 + ((mquant
< 0) ? 0 : v
->halfpq
);
769 ac_val
= s
->ac_val
[0][s
->block_index
[n
]];
771 if (dc_pred_dir
) // left
774 ac_val
-= 16 * s
->block_wrap
[n
];
776 q1
= s
->current_picture
.qscale_table
[mb_pos
];
779 else if (dc_pred_dir
) {
782 else if (c_avail
&& mb_pos
)
783 q2
= s
->current_picture
.qscale_table
[mb_pos
- 1];
787 else if (a_avail
&& mb_pos
>= s
->mb_stride
)
788 q2
= s
->current_picture
.qscale_table
[mb_pos
- s
->mb_stride
];
795 int last
= 0, skip
, value
;
796 const uint8_t *zz_table
;
800 if (!use_pred
&& v
->fcm
== ILACE_FRAME
) {
801 zz_table
= v
->zzi_8x8
;
803 if (!dc_pred_dir
) // top
804 zz_table
= v
->zz_8x8
[2];
806 zz_table
= v
->zz_8x8
[3];
809 if (v
->fcm
!= ILACE_FRAME
)
810 zz_table
= v
->zz_8x8
[1];
812 zz_table
= v
->zzi_8x8
;
816 int ret
= vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
822 block
[zz_table
[i
++]] = value
;
825 /* apply AC prediction if needed */
828 if (dc_pred_dir
) { // left
834 /* scale predictors if needed*/
835 q1
= FFABS(q1
) * 2 + ((q1
< 0) ? 0 : v
->halfpq
) - 1;
837 return AVERROR_INVALIDDATA
;
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;
844 for (k
= 1; k
< 8; k
++)
845 block
[k
<< sh
] += ac_val
[k
];
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
];
854 /* scale AC coeffs */
855 for (k
= 1; k
< 64; k
++)
859 block
[k
] += (block
[k
] < 0) ? -quant
: quant
;
862 } else { // no AC coeffs
865 memset(ac_val2
, 0, 16 * 2);
867 /* apply AC prediction if needed */
870 if (dc_pred_dir
) { // left
877 memcpy(ac_val2
, ac_val
, 8 * 2);
878 q1
= FFABS(q1
) * 2 + ((q1
< 0) ? 0 : v
->halfpq
) - 1;
880 return AVERROR_INVALIDDATA
;
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;
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
;
894 if (use_pred
) i
= 63;
895 s
->block_last_index
[n
] = i
;
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
908 static int vc1_decode_intra_block(VC1Context
*v
, int16_t block
[64], int n
,
909 int coded
, int mquant
, int codingset
)
911 GetBitContext
*gb
= &v
->s
.gb
;
912 MpegEncContext
*s
= &v
->s
;
913 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
915 int16_t *dc_val
= NULL
;
916 int16_t *ac_val
, *ac_val2
;
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
;
923 int quant
= FFABS(mquant
);
925 s
->bdsp
.clear_block(block
);
927 /* XXX: Guard against dumb values of mquant */
928 quant
= av_clip_uintp2(quant
, 5);
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
];
934 /* Get DC differential */
936 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
938 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
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
);
946 dcdiff
= (dcdiff
<< m
) + get_bits(gb
, m
) - ((1 << m
) - 1);
953 dcdiff
+= ff_vc1_pred_dc(&v
->s
, v
->overlap
, quant
, n
, a_avail
, c_avail
, &dc_val
, &dc_pred_dir
);
956 /* Store the quantized DC coeff, used for prediction */
959 block
[0] = dcdiff
* s
->y_dc_scale
;
961 block
[0] = dcdiff
* s
->c_dc_scale
;
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
]];
974 scale
= quant
* 2 + ((mquant
< 0) ? 0 : v
->halfpq
);
976 if (dc_pred_dir
) //left
979 ac_val
-= 16 * s
->block_wrap
[n
];
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)
988 if (!dc_pred_dir
&& n
== 2)
993 int last
= 0, skip
, value
;
997 int ret
= vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
1003 if (v
->fcm
== PROGRESSIVE
)
1004 block
[v
->zz_8x8
[0][i
++]] = value
;
1006 if (use_pred
&& (v
->fcm
== ILACE_FRAME
)) {
1007 if (!dc_pred_dir
) // top
1008 block
[v
->zz_8x8
[2][i
++]] = value
;
1010 block
[v
->zz_8x8
[3][i
++]] = value
;
1012 block
[v
->zzi_8x8
[i
++]] = value
;
1017 /* apply AC prediction if needed */
1019 /* scale predictors if needed*/
1020 q1
= FFABS(q1
) * 2 + ((q1
< 0) ? 0 : v
->halfpq
) - 1;
1022 return AVERROR_INVALIDDATA
;
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;
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;
1034 if (dc_pred_dir
) { // left
1035 for (k
= 1; k
< 8; k
++)
1036 block
[k
<< v
->left_blk_sh
] += ac_val
[k
];
1038 for (k
= 1; k
< 8; k
++)
1039 block
[k
<< v
->top_blk_sh
] += ac_val
[k
+ 8];
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
];
1049 /* scale AC coeffs */
1050 for (k
= 1; k
< 64; k
++)
1054 block
[k
] += (block
[k
] < 0) ? -quant
: quant
;
1057 if (use_pred
) i
= 63;
1058 } else { // no AC coeffs
1061 memset(ac_val2
, 0, 16 * 2);
1062 if (dc_pred_dir
) { // left
1064 memcpy(ac_val2
, ac_val
, 8 * 2);
1065 q1
= FFABS(q1
) * 2 + ((q1
< 0) ? 0 : v
->halfpq
) - 1;
1067 return AVERROR_INVALIDDATA
;
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;
1077 memcpy(ac_val2
+ 8, ac_val
+ 8, 8 * 2);
1078 q1
= FFABS(q1
) * 2 + ((q1
< 0) ? 0 : v
->halfpq
) - 1;
1080 return AVERROR_INVALIDDATA
;
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;
1090 /* apply AC prediction if needed */
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
;
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
;
1108 s
->block_last_index
[n
] = i
;
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
,
1120 MpegEncContext
*s
= &v
->s
;
1121 GetBitContext
*gb
= &s
->gb
;
1124 int scale
, off
, idx
, last
, skip
, value
;
1125 int ttblk
= ttmb
& 7;
1127 int quant
= FFABS(mquant
);
1129 s
->bdsp
.clear_block(block
);
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)];
1134 if (ttblk
== TT_4X4
) {
1135 subblkpat
= ~(get_vlc2(gb
, ff_vc1_subblkpat_vlc
[v
->tt_index
].table
, VC1_SUBBLKPAT_VLC_BITS
, 1) + 1);
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
);
1142 subblkpat
^= 3; // swap decoded pattern bits
1143 if (ttblk
== TT_8X4_TOP
|| ttblk
== TT_8X4_BOTTOM
)
1145 if (ttblk
== TT_4X8_RIGHT
|| ttblk
== TT_4X8_LEFT
)
1148 scale
= quant
* 2 + ((mquant
< 0) ? 0 : v
->halfpq
);
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
);
1155 if (ttblk
== TT_4X8_RIGHT
|| ttblk
== TT_4X8_LEFT
) {
1156 subblkpat
= 2 - (ttblk
== TT_4X8_LEFT
);
1165 int ret
= vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
1172 idx
= v
->zz_8x8
[0][i
++];
1174 idx
= v
->zzi_8x8
[i
++];
1175 block
[idx
] = value
* scale
;
1177 block
[idx
] += (block
[idx
] < 0) ? -quant
: quant
;
1181 v
->vc1dsp
.vc1_inv_trans_8x8_dc(dst
, linesize
, block
);
1183 v
->vc1dsp
.vc1_inv_trans_8x8(block
);
1184 s
->idsp
.add_pixels_clamped(block
, dst
, linesize
);
1189 pat
= ~subblkpat
& 0xF;
1190 for (j
= 0; j
< 4; j
++) {
1191 last
= subblkpat
& (1 << (3 - j
));
1193 off
= (j
& 1) * 4 + (j
& 2) * 16;
1195 int ret
= vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
1202 idx
= ff_vc1_simple_progressive_4x4_zz
[i
++];
1204 idx
= ff_vc1_adv_interlaced_4x4_zz
[i
++];
1205 block
[idx
+ off
] = value
* scale
;
1207 block
[idx
+ off
] += (block
[idx
+ off
] < 0) ? -quant
: quant
;
1209 if (!(subblkpat
& (1 << (3 - j
))) && !skip_block
) {
1211 v
->vc1dsp
.vc1_inv_trans_4x4_dc(dst
+ (j
& 1) * 4 + (j
& 2) * 2 * linesize
, linesize
, block
+ off
);
1213 v
->vc1dsp
.vc1_inv_trans_4x4(dst
+ (j
& 1) * 4 + (j
& 2) * 2 * linesize
, linesize
, block
+ off
);
1218 pat
= ~((subblkpat
& 2) * 6 + (subblkpat
& 1) * 3) & 0xF;
1219 for (j
= 0; j
< 2; j
++) {
1220 last
= subblkpat
& (1 << (1 - j
));
1224 int ret
= vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
1231 idx
= v
->zz_8x4
[i
++] + off
;
1233 idx
= ff_vc1_adv_interlaced_8x4_zz
[i
++] + off
;
1234 block
[idx
] = value
* scale
;
1236 block
[idx
] += (block
[idx
] < 0) ? -quant
: quant
;
1238 if (!(subblkpat
& (1 << (1 - j
))) && !skip_block
) {
1240 v
->vc1dsp
.vc1_inv_trans_8x4_dc(dst
+ j
* 4 * linesize
, linesize
, block
+ off
);
1242 v
->vc1dsp
.vc1_inv_trans_8x4(dst
+ j
* 4 * linesize
, linesize
, block
+ off
);
1247 pat
= ~(subblkpat
* 5) & 0xF;
1248 for (j
= 0; j
< 2; j
++) {
1249 last
= subblkpat
& (1 << (1 - j
));
1253 int ret
= vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
1260 idx
= v
->zz_4x8
[i
++] + off
;
1262 idx
= ff_vc1_adv_interlaced_4x8_zz
[i
++] + off
;
1263 block
[idx
] = value
* scale
;
1265 block
[idx
] += (block
[idx
] < 0) ? -quant
: quant
;
1267 if (!(subblkpat
& (1 << (1 - j
))) && !skip_block
) {
1269 v
->vc1dsp
.vc1_inv_trans_4x8_dc(dst
+ j
* 4, linesize
, block
+ off
);
1271 v
->vc1dsp
.vc1_inv_trans_4x8(dst
+ j
*4, linesize
, block
+ off
);
1277 *ttmb_out
|= ttblk
<< (n
* 4);
1281 /** @} */ // Macroblock group
1283 static const uint8_t size_table
[6] = { 0, 2, 3, 4, 5, 8 };
1285 /** Decode one P-frame MB
1287 static int vc1_decode_p_mb(VC1Context
*v
)
1289 MpegEncContext
*s
= &v
->s
;
1290 GetBitContext
*gb
= &s
->gb
;
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 */
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;
1303 int skipped
, fourmv
;
1304 int block_cbp
= 0, pat
, block_tt
= 0, block_intra
= 0;
1307 mquant
= v
->pq
; /* lossy initialization */
1309 if (v
->mv_type_is_raw
)
1310 fourmv
= get_bits1(gb
);
1312 fourmv
= v
->mv_type_mb_plane
[mb_pos
];
1314 skipped
= get_bits1(gb
);
1316 skipped
= v
->s
.mbskip_table
[mb_pos
];
1318 if (!fourmv
) { /* 1MV mode */
1320 GET_MVDATA(dmv_x
, dmv_y
);
1323 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
1324 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
1326 s
->current_picture
.mb_type
[mb_pos
] = s
->mb_intra
? MB_TYPE_INTRA
: MB_TYPE_16x16
;
1327 ff_vc1_pred_mv(v
, 0, dmv_x
, dmv_y
, 1, v
->range_x
, v
->range_y
, v
->mb_type
[0], 0, 0);
1329 /* FIXME Set DC val for inter block ? */
1330 if (s
->mb_intra
&& !mb_has_coeffs
) {
1332 s
->ac_pred
= get_bits1(gb
);
1334 } else if (mb_has_coeffs
) {
1336 s
->ac_pred
= get_bits1(gb
);
1337 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
1343 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1345 if (!v
->ttmbf
&& !s
->mb_intra
&& mb_has_coeffs
)
1346 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
,
1347 VC1_TTMB_VLC_BITS
, 2);
1348 if (!s
->mb_intra
) ff_vc1_mc_1mv(v
, 0);
1350 for (i
= 0; i
< 6; i
++) {
1351 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1353 val
= ((cbp
>> (5 - i
)) & 1);
1354 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
1355 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
1357 /* check if prediction blocks A and C are available */
1358 v
->a_avail
= v
->c_avail
= 0;
1359 if (i
== 2 || i
== 3 || !s
->first_slice_line
)
1360 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
1361 if (i
== 1 || i
== 3 || s
->mb_x
)
1362 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
1364 ret
= vc1_decode_intra_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[i
]], i
, val
, mquant
,
1365 (i
& 4) ? v
->codingset2
: v
->codingset
);
1368 if (CONFIG_GRAY
&& (i
> 3) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
1370 v
->vc1dsp
.vc1_inv_trans_8x8(v
->block
[v
->cur_blk_idx
][block_map
[i
]]);
1372 for (j
= 0; j
< 64; j
++)
1373 v
->block
[v
->cur_blk_idx
][block_map
[i
]][j
] *= 2;
1374 block_cbp
|= 0xF << (i
<< 2);
1375 block_intra
|= 1 << i
;
1377 pat
= vc1_decode_p_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[i
]], i
, mquant
, ttmb
, first_block
,
1378 s
->dest
[dst_idx
] + off
, (i
& 4) ? s
->uvlinesize
: s
->linesize
,
1379 CONFIG_GRAY
&& (i
& 4) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
), &block_tt
);
1382 block_cbp
|= pat
<< (i
<< 2);
1383 if (!v
->ttmbf
&& ttmb
< 8)
1390 for (i
= 0; i
< 6; i
++) {
1391 v
->mb_type
[0][s
->block_index
[i
]] = 0;
1392 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1394 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_SKIP
;
1395 s
->current_picture
.qscale_table
[mb_pos
] = 0;
1396 ff_vc1_pred_mv(v
, 0, 0, 0, 1, v
->range_x
, v
->range_y
, v
->mb_type
[0], 0, 0);
1397 ff_vc1_mc_1mv(v
, 0);
1399 } else { // 4MV mode
1400 if (!skipped
/* unskipped MB */) {
1401 int intra_count
= 0, coded_inter
= 0;
1402 int is_intra
[6], is_coded
[6];
1404 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
1405 for (i
= 0; i
< 6; i
++) {
1406 val
= ((cbp
>> (5 - i
)) & 1);
1407 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1414 GET_MVDATA(dmv_x
, dmv_y
);
1416 ff_vc1_pred_mv(v
, i
, dmv_x
, dmv_y
, 0, v
->range_x
, v
->range_y
, v
->mb_type
[0], 0, 0);
1418 ff_vc1_mc_4mv_luma(v
, i
, 0, 0);
1419 intra_count
+= s
->mb_intra
;
1420 is_intra
[i
] = s
->mb_intra
;
1421 is_coded
[i
] = mb_has_coeffs
;
1424 is_intra
[i
] = (intra_count
>= 3);
1428 ff_vc1_mc_4mv_chroma(v
, 0);
1429 v
->mb_type
[0][s
->block_index
[i
]] = is_intra
[i
];
1431 coded_inter
= !is_intra
[i
] & is_coded
[i
];
1433 // if there are no coded blocks then don't do anything more
1435 if (!intra_count
&& !coded_inter
)
1438 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1439 /* test if block is intra and has pred */
1442 for (i
= 0; i
< 6; i
++)
1444 if (((!s
->first_slice_line
|| (i
== 2 || i
== 3)) && v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]])
1445 || ((s
->mb_x
|| (i
== 1 || i
== 3)) && v
->mb_type
[0][s
->block_index
[i
] - 1])) {
1451 s
->ac_pred
= get_bits1(gb
);
1455 if (!v
->ttmbf
&& coded_inter
)
1456 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
1457 for (i
= 0; i
< 6; i
++) {
1459 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
1460 s
->mb_intra
= is_intra
[i
];
1462 /* check if prediction blocks A and C are available */
1463 v
->a_avail
= v
->c_avail
= 0;
1464 if (i
== 2 || i
== 3 || !s
->first_slice_line
)
1465 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
1466 if (i
== 1 || i
== 3 || s
->mb_x
)
1467 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
1469 ret
= vc1_decode_intra_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[i
]], i
, is_coded
[i
], mquant
,
1470 (i
& 4) ? v
->codingset2
: v
->codingset
);
1473 if (CONFIG_GRAY
&& (i
> 3) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
1475 v
->vc1dsp
.vc1_inv_trans_8x8(v
->block
[v
->cur_blk_idx
][block_map
[i
]]);
1477 for (j
= 0; j
< 64; j
++)
1478 v
->block
[v
->cur_blk_idx
][block_map
[i
]][j
] *= 2;
1479 block_cbp
|= 0xF << (i
<< 2);
1480 block_intra
|= 1 << i
;
1481 } else if (is_coded
[i
]) {
1482 pat
= vc1_decode_p_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[i
]], i
, mquant
, ttmb
,
1483 first_block
, s
->dest
[dst_idx
] + off
,
1484 (i
& 4) ? s
->uvlinesize
: s
->linesize
,
1485 CONFIG_GRAY
&& (i
& 4) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
),
1489 block_cbp
|= pat
<< (i
<< 2);
1490 if (!v
->ttmbf
&& ttmb
< 8)
1495 } else { // skipped MB
1497 s
->current_picture
.qscale_table
[mb_pos
] = 0;
1498 for (i
= 0; i
< 6; i
++) {
1499 v
->mb_type
[0][s
->block_index
[i
]] = 0;
1500 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1502 for (i
= 0; i
< 4; i
++) {
1503 ff_vc1_pred_mv(v
, i
, 0, 0, 0, v
->range_x
, v
->range_y
, v
->mb_type
[0], 0, 0);
1504 ff_vc1_mc_4mv_luma(v
, i
, 0, 0);
1506 ff_vc1_mc_4mv_chroma(v
, 0);
1507 s
->current_picture
.qscale_table
[mb_pos
] = 0;
1511 if (v
->overlap
&& v
->pq
>= 9)
1512 ff_vc1_p_overlap_filter(v
);
1513 vc1_put_blocks_clamped(v
, 1);
1515 v
->cbp
[s
->mb_x
] = block_cbp
;
1516 v
->ttblk
[s
->mb_x
] = block_tt
;
1517 v
->is_intra
[s
->mb_x
] = block_intra
;
1522 /* Decode one macroblock in an interlaced frame p picture */
1524 static int vc1_decode_p_mb_intfr(VC1Context
*v
)
1526 MpegEncContext
*s
= &v
->s
;
1527 GetBitContext
*gb
= &s
->gb
;
1529 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
1530 int cbp
= 0; /* cbp decoding stuff */
1531 int mqdiff
, mquant
; /* MB quantization */
1532 int ttmb
= v
->ttfrm
; /* MB Transform type */
1534 int mb_has_coeffs
= 1; /* last_flag */
1535 int dmv_x
, dmv_y
; /* Differential MV components */
1536 int val
; /* temp value */
1537 int first_block
= 1;
1539 int skipped
, fourmv
= 0, twomv
= 0;
1540 int block_cbp
= 0, pat
, block_tt
= 0;
1541 int idx_mbmode
= 0, mvbp
;
1545 mquant
= v
->pq
; /* Lossy initialization */
1548 skipped
= get_bits1(gb
);
1550 skipped
= v
->s
.mbskip_table
[mb_pos
];
1552 if (v
->fourmvswitch
)
1553 idx_mbmode
= get_vlc2(gb
, v
->mbmode_vlc
->table
, VC1_INTFR_4MV_MBMODE_VLC_BITS
, 2); // try getting this done
1555 idx_mbmode
= get_vlc2(gb
, v
->mbmode_vlc
->table
, VC1_INTFR_NON4MV_MBMODE_VLC_BITS
, 2); // in a single line
1556 switch (ff_vc1_mbmode_intfrp
[v
->fourmvswitch
][idx_mbmode
][0]) {
1557 /* store the motion vector type in a flag (useful later) */
1558 case MV_PMODE_INTFR_4MV
:
1560 v
->blk_mv_type
[s
->block_index
[0]] = 0;
1561 v
->blk_mv_type
[s
->block_index
[1]] = 0;
1562 v
->blk_mv_type
[s
->block_index
[2]] = 0;
1563 v
->blk_mv_type
[s
->block_index
[3]] = 0;
1565 case MV_PMODE_INTFR_4MV_FIELD
:
1567 v
->blk_mv_type
[s
->block_index
[0]] = 1;
1568 v
->blk_mv_type
[s
->block_index
[1]] = 1;
1569 v
->blk_mv_type
[s
->block_index
[2]] = 1;
1570 v
->blk_mv_type
[s
->block_index
[3]] = 1;
1572 case MV_PMODE_INTFR_2MV_FIELD
:
1574 v
->blk_mv_type
[s
->block_index
[0]] = 1;
1575 v
->blk_mv_type
[s
->block_index
[1]] = 1;
1576 v
->blk_mv_type
[s
->block_index
[2]] = 1;
1577 v
->blk_mv_type
[s
->block_index
[3]] = 1;
1579 case MV_PMODE_INTFR_1MV
:
1580 v
->blk_mv_type
[s
->block_index
[0]] = 0;
1581 v
->blk_mv_type
[s
->block_index
[1]] = 0;
1582 v
->blk_mv_type
[s
->block_index
[2]] = 0;
1583 v
->blk_mv_type
[s
->block_index
[3]] = 0;
1586 if (ff_vc1_mbmode_intfrp
[v
->fourmvswitch
][idx_mbmode
][0] == MV_PMODE_INTFR_INTRA
) { // intra MB
1587 for (i
= 0; i
< 4; i
++) {
1588 s
->current_picture
.motion_val
[1][s
->block_index
[i
]][0] = 0;
1589 s
->current_picture
.motion_val
[1][s
->block_index
[i
]][1] = 0;
1591 v
->is_intra
[s
->mb_x
] = 0x3f; // Set the bitfield to all 1.
1593 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_INTRA
;
1594 fieldtx
= v
->fieldtx_plane
[mb_pos
] = get_bits1(gb
);
1595 mb_has_coeffs
= get_bits1(gb
);
1597 cbp
= 1 + get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
1598 v
->s
.ac_pred
= v
->acpred_plane
[mb_pos
] = get_bits1(gb
);
1600 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1601 /* Set DC scale - y and c use the same (not sure if necessary here) */
1602 s
->y_dc_scale
= s
->y_dc_scale_table
[FFABS(mquant
)];
1603 s
->c_dc_scale
= s
->c_dc_scale_table
[FFABS(mquant
)];
1605 for (i
= 0; i
< 6; i
++) {
1606 v
->a_avail
= v
->c_avail
= 0;
1607 v
->mb_type
[0][s
->block_index
[i
]] = 1;
1608 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1610 val
= ((cbp
>> (5 - i
)) & 1);
1611 if (i
== 2 || i
== 3 || !s
->first_slice_line
)
1612 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
1613 if (i
== 1 || i
== 3 || s
->mb_x
)
1614 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
1616 ret
= vc1_decode_intra_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[i
]], i
, val
, mquant
,
1617 (i
& 4) ? v
->codingset2
: v
->codingset
);
1620 if (CONFIG_GRAY
&& (i
> 3) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
1622 v
->vc1dsp
.vc1_inv_trans_8x8(v
->block
[v
->cur_blk_idx
][block_map
[i
]]);
1624 off
= (fieldtx
) ? ((i
& 1) * 8) + ((i
& 2) >> 1) * s
->linesize
: (i
& 1) * 8 + 4 * (i
& 2) * s
->linesize
;
1627 block_cbp
|= 0xf << (i
<< 2);
1630 } else { // inter MB
1631 mb_has_coeffs
= ff_vc1_mbmode_intfrp
[v
->fourmvswitch
][idx_mbmode
][3];
1633 cbp
= 1 + get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
1634 if (ff_vc1_mbmode_intfrp
[v
->fourmvswitch
][idx_mbmode
][0] == MV_PMODE_INTFR_2MV_FIELD
) {
1635 v
->twomvbp
= get_vlc2(gb
, v
->twomvbp_vlc
->table
, VC1_2MV_BLOCK_PATTERN_VLC_BITS
, 1);
1637 if ((ff_vc1_mbmode_intfrp
[v
->fourmvswitch
][idx_mbmode
][0] == MV_PMODE_INTFR_4MV
)
1638 || (ff_vc1_mbmode_intfrp
[v
->fourmvswitch
][idx_mbmode
][0] == MV_PMODE_INTFR_4MV_FIELD
)) {
1639 v
->fourmvbp
= get_vlc2(gb
, v
->fourmvbp_vlc
->table
, VC1_4MV_BLOCK_PATTERN_VLC_BITS
, 1);
1642 s
->mb_intra
= v
->is_intra
[s
->mb_x
] = 0;
1643 for (i
= 0; i
< 6; i
++)
1644 v
->mb_type
[0][s
->block_index
[i
]] = 0;
1645 fieldtx
= v
->fieldtx_plane
[mb_pos
] = ff_vc1_mbmode_intfrp
[v
->fourmvswitch
][idx_mbmode
][1];
1646 /* for all motion vector read MVDATA and motion compensate each block */
1650 for (i
= 0; i
< 4; i
++) {
1652 if (mvbp
& (8 >> i
))
1653 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
1654 ff_vc1_pred_mv_intfr(v
, i
, dmv_x
, dmv_y
, 0, v
->range_x
, v
->range_y
, 0);
1655 ff_vc1_mc_4mv_luma(v
, i
, 0, 0);
1657 ff_vc1_mc_4mv_chroma4(v
, 0, 0, 0);
1662 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
1664 ff_vc1_pred_mv_intfr(v
, 0, dmv_x
, dmv_y
, 2, v
->range_x
, v
->range_y
, 0);
1665 ff_vc1_mc_4mv_luma(v
, 0, 0, 0);
1666 ff_vc1_mc_4mv_luma(v
, 1, 0, 0);
1669 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
1671 ff_vc1_pred_mv_intfr(v
, 2, dmv_x
, dmv_y
, 2, v
->range_x
, v
->range_y
, 0);
1672 ff_vc1_mc_4mv_luma(v
, 2, 0, 0);
1673 ff_vc1_mc_4mv_luma(v
, 3, 0, 0);
1674 ff_vc1_mc_4mv_chroma4(v
, 0, 0, 0);
1676 mvbp
= ff_vc1_mbmode_intfrp
[v
->fourmvswitch
][idx_mbmode
][2];
1679 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
1681 ff_vc1_pred_mv_intfr(v
, 0, dmv_x
, dmv_y
, 1, v
->range_x
, v
->range_y
, 0);
1682 ff_vc1_mc_1mv(v
, 0);
1685 GET_MQUANT(); // p. 227
1686 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1687 if (!v
->ttmbf
&& cbp
)
1688 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
1689 for (i
= 0; i
< 6; i
++) {
1690 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1692 val
= ((cbp
>> (5 - i
)) & 1);
1694 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
1696 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + ((i
> 1) * s
->linesize
));
1698 pat
= vc1_decode_p_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[i
]], i
, mquant
, ttmb
,
1699 first_block
, s
->dest
[dst_idx
] + off
,
1700 (i
& 4) ? s
->uvlinesize
: (s
->linesize
<< fieldtx
),
1701 CONFIG_GRAY
&& (i
& 4) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
), &block_tt
);
1704 block_cbp
|= pat
<< (i
<< 2);
1705 if (!v
->ttmbf
&& ttmb
< 8)
1712 s
->mb_intra
= v
->is_intra
[s
->mb_x
] = 0;
1713 for (i
= 0; i
< 6; i
++) {
1714 v
->mb_type
[0][s
->block_index
[i
]] = 0;
1715 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1717 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_SKIP
;
1718 s
->current_picture
.qscale_table
[mb_pos
] = 0;
1719 v
->blk_mv_type
[s
->block_index
[0]] = 0;
1720 v
->blk_mv_type
[s
->block_index
[1]] = 0;
1721 v
->blk_mv_type
[s
->block_index
[2]] = 0;
1722 v
->blk_mv_type
[s
->block_index
[3]] = 0;
1723 ff_vc1_pred_mv_intfr(v
, 0, 0, 0, 1, v
->range_x
, v
->range_y
, 0);
1724 ff_vc1_mc_1mv(v
, 0);
1725 v
->fieldtx_plane
[mb_pos
] = 0;
1727 if (v
->overlap
&& v
->pq
>= 9)
1728 ff_vc1_p_overlap_filter(v
);
1729 vc1_put_blocks_clamped(v
, 1);
1731 v
->cbp
[s
->mb_x
] = block_cbp
;
1732 v
->ttblk
[s
->mb_x
] = block_tt
;
1737 static int vc1_decode_p_mb_intfi(VC1Context
*v
)
1739 MpegEncContext
*s
= &v
->s
;
1740 GetBitContext
*gb
= &s
->gb
;
1742 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
1743 int cbp
= 0; /* cbp decoding stuff */
1744 int mqdiff
, mquant
; /* MB quantization */
1745 int ttmb
= v
->ttfrm
; /* MB Transform type */
1747 int mb_has_coeffs
= 1; /* last_flag */
1748 int dmv_x
, dmv_y
; /* Differential MV components */
1749 int val
; /* temp values */
1750 int first_block
= 1;
1753 int block_cbp
= 0, pat
, block_tt
= 0;
1757 mquant
= v
->pq
; /* Lossy initialization */
1759 idx_mbmode
= get_vlc2(gb
, v
->mbmode_vlc
->table
, VC1_IF_MBMODE_VLC_BITS
, 2);
1760 if (idx_mbmode
<= 1) { // intra MB
1761 v
->is_intra
[s
->mb_x
] = 0x3f; // Set the bitfield to all 1.
1763 s
->current_picture
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][0] = 0;
1764 s
->current_picture
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][1] = 0;
1765 s
->current_picture
.mb_type
[mb_pos
+ v
->mb_off
] = MB_TYPE_INTRA
;
1767 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1768 /* Set DC scale - y and c use the same (not sure if necessary here) */
1769 s
->y_dc_scale
= s
->y_dc_scale_table
[FFABS(mquant
)];
1770 s
->c_dc_scale
= s
->c_dc_scale_table
[FFABS(mquant
)];
1771 v
->s
.ac_pred
= v
->acpred_plane
[mb_pos
] = get_bits1(gb
);
1772 mb_has_coeffs
= idx_mbmode
& 1;
1774 cbp
= 1 + get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_ICBPCY_VLC_BITS
, 2);
1776 for (i
= 0; i
< 6; i
++) {
1777 v
->a_avail
= v
->c_avail
= 0;
1778 v
->mb_type
[0][s
->block_index
[i
]] = 1;
1779 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1781 val
= ((cbp
>> (5 - i
)) & 1);
1782 if (i
== 2 || i
== 3 || !s
->first_slice_line
)
1783 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
1784 if (i
== 1 || i
== 3 || s
->mb_x
)
1785 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
1787 ret
= vc1_decode_intra_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[i
]], i
, val
, mquant
,
1788 (i
& 4) ? v
->codingset2
: v
->codingset
);
1791 if (CONFIG_GRAY
&& (i
> 3) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
1793 v
->vc1dsp
.vc1_inv_trans_8x8(v
->block
[v
->cur_blk_idx
][block_map
[i
]]);
1794 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
1795 block_cbp
|= 0xf << (i
<< 2);
1798 s
->mb_intra
= v
->is_intra
[s
->mb_x
] = 0;
1799 s
->current_picture
.mb_type
[mb_pos
+ v
->mb_off
] = MB_TYPE_16x16
;
1800 for (i
= 0; i
< 6; i
++)
1801 v
->mb_type
[0][s
->block_index
[i
]] = 0;
1802 if (idx_mbmode
<= 5) { // 1-MV
1803 dmv_x
= dmv_y
= pred_flag
= 0;
1804 if (idx_mbmode
& 1) {
1805 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, &pred_flag
);
1807 ff_vc1_pred_mv(v
, 0, dmv_x
, dmv_y
, 1, v
->range_x
, v
->range_y
, v
->mb_type
[0], pred_flag
, 0);
1808 ff_vc1_mc_1mv(v
, 0);
1809 mb_has_coeffs
= !(idx_mbmode
& 2);
1811 v
->fourmvbp
= get_vlc2(gb
, v
->fourmvbp_vlc
->table
, VC1_4MV_BLOCK_PATTERN_VLC_BITS
, 1);
1812 for (i
= 0; i
< 4; i
++) {
1813 dmv_x
= dmv_y
= pred_flag
= 0;
1814 if (v
->fourmvbp
& (8 >> i
))
1815 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, &pred_flag
);
1816 ff_vc1_pred_mv(v
, i
, dmv_x
, dmv_y
, 0, v
->range_x
, v
->range_y
, v
->mb_type
[0], pred_flag
, 0);
1817 ff_vc1_mc_4mv_luma(v
, i
, 0, 0);
1819 ff_vc1_mc_4mv_chroma(v
, 0);
1820 mb_has_coeffs
= idx_mbmode
& 1;
1823 cbp
= 1 + get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
1827 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1828 if (!v
->ttmbf
&& cbp
) {
1829 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
1832 for (i
= 0; i
< 6; i
++) {
1833 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1835 val
= ((cbp
>> (5 - i
)) & 1);
1836 off
= (i
& 4) ? 0 : (i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
;
1838 pat
= vc1_decode_p_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[i
]], i
, mquant
, ttmb
,
1839 first_block
, s
->dest
[dst_idx
] + off
,
1840 (i
& 4) ? s
->uvlinesize
: s
->linesize
,
1841 CONFIG_GRAY
&& (i
& 4) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
),
1845 block_cbp
|= pat
<< (i
<< 2);
1846 if (!v
->ttmbf
&& ttmb
< 8)
1852 if (v
->overlap
&& v
->pq
>= 9)
1853 ff_vc1_p_overlap_filter(v
);
1854 vc1_put_blocks_clamped(v
, 1);
1856 v
->cbp
[s
->mb_x
] = block_cbp
;
1857 v
->ttblk
[s
->mb_x
] = block_tt
;
1862 /** Decode one B-frame MB (in Main profile)
1864 static int vc1_decode_b_mb(VC1Context
*v
)
1866 MpegEncContext
*s
= &v
->s
;
1867 GetBitContext
*gb
= &s
->gb
;
1869 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
1870 int cbp
= 0; /* cbp decoding stuff */
1871 int mqdiff
, mquant
; /* MB quantization */
1872 int ttmb
= v
->ttfrm
; /* MB Transform type */
1873 int mb_has_coeffs
= 0; /* last_flag */
1874 int index
, index1
; /* LUT indexes */
1875 int val
, sign
; /* temp values */
1876 int first_block
= 1;
1878 int skipped
, direct
;
1879 int dmv_x
[2], dmv_y
[2];
1880 int bmvtype
= BMV_TYPE_BACKWARD
;
1883 mquant
= v
->pq
; /* lossy initialization */
1887 direct
= get_bits1(gb
);
1889 direct
= v
->direct_mb_plane
[mb_pos
];
1891 skipped
= get_bits1(gb
);
1893 skipped
= v
->s
.mbskip_table
[mb_pos
];
1895 dmv_x
[0] = dmv_x
[1] = dmv_y
[0] = dmv_y
[1] = 0;
1896 for (i
= 0; i
< 6; i
++) {
1897 v
->mb_type
[0][s
->block_index
[i
]] = 0;
1898 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1900 s
->current_picture
.qscale_table
[mb_pos
] = 0;
1904 GET_MVDATA(dmv_x
[0], dmv_y
[0]);
1905 dmv_x
[1] = dmv_x
[0];
1906 dmv_y
[1] = dmv_y
[0];
1908 if (skipped
|| !s
->mb_intra
) {
1909 bmvtype
= decode012(gb
);
1912 bmvtype
= (v
->bfraction
>= (B_FRACTION_DEN
/2)) ? BMV_TYPE_BACKWARD
: BMV_TYPE_FORWARD
;
1915 bmvtype
= (v
->bfraction
>= (B_FRACTION_DEN
/2)) ? BMV_TYPE_FORWARD
: BMV_TYPE_BACKWARD
;
1918 bmvtype
= BMV_TYPE_INTERPOLATED
;
1919 dmv_x
[0] = dmv_y
[0] = 0;
1923 for (i
= 0; i
< 6; i
++)
1924 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
1928 bmvtype
= BMV_TYPE_INTERPOLATED
;
1929 ff_vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1930 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1934 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
1937 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1939 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
1940 dmv_x
[0] = dmv_y
[0] = dmv_x
[1] = dmv_y
[1] = 0;
1941 ff_vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1942 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1944 if (!mb_has_coeffs
&& !s
->mb_intra
) {
1945 /* no coded blocks - effectively skipped */
1946 ff_vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1947 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1950 if (s
->mb_intra
&& !mb_has_coeffs
) {
1952 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1953 s
->ac_pred
= get_bits1(gb
);
1955 ff_vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1957 if (bmvtype
== BMV_TYPE_INTERPOLATED
) {
1958 GET_MVDATA(dmv_x
[0], dmv_y
[0]);
1959 if (!mb_has_coeffs
) {
1960 /* interpolated skipped block */
1961 ff_vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1962 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1966 ff_vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1968 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
1971 s
->ac_pred
= get_bits1(gb
);
1972 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
1974 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
1975 if (!v
->ttmbf
&& !s
->mb_intra
&& mb_has_coeffs
)
1976 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
1980 for (i
= 0; i
< 6; i
++) {
1981 s
->dc_val
[0][s
->block_index
[i
]] = 0;
1983 val
= ((cbp
>> (5 - i
)) & 1);
1984 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
1985 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
1987 /* check if prediction blocks A and C are available */
1988 v
->a_avail
= v
->c_avail
= 0;
1989 if (i
== 2 || i
== 3 || !s
->first_slice_line
)
1990 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
1991 if (i
== 1 || i
== 3 || s
->mb_x
)
1992 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
1994 ret
= vc1_decode_intra_block(v
, s
->block
[i
], i
, val
, mquant
,
1995 (i
& 4) ? v
->codingset2
: v
->codingset
);
1998 if (CONFIG_GRAY
&& (i
> 3) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
2000 v
->vc1dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
2002 for (j
= 0; j
< 64; j
++)
2003 s
->block
[i
][j
] *= 2;
2004 s
->idsp
.put_signed_pixels_clamped(s
->block
[i
],
2005 s
->dest
[dst_idx
] + off
,
2006 i
& 4 ? s
->uvlinesize
2009 int pat
= vc1_decode_p_block(v
, s
->block
[i
], i
, mquant
, ttmb
,
2010 first_block
, s
->dest
[dst_idx
] + off
,
2011 (i
& 4) ? s
->uvlinesize
: s
->linesize
,
2012 CONFIG_GRAY
&& (i
& 4) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
), NULL
);
2015 if (!v
->ttmbf
&& ttmb
< 8)
2023 /** Decode one B-frame MB (in interlaced field B picture)
2025 static int vc1_decode_b_mb_intfi(VC1Context
*v
)
2027 MpegEncContext
*s
= &v
->s
;
2028 GetBitContext
*gb
= &s
->gb
;
2030 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2031 int cbp
= 0; /* cbp decoding stuff */
2032 int mqdiff
, mquant
; /* MB quantization */
2033 int ttmb
= v
->ttfrm
; /* MB Transform type */
2034 int mb_has_coeffs
= 0; /* last_flag */
2035 int val
; /* temp value */
2036 int first_block
= 1;
2039 int dmv_x
[2], dmv_y
[2], pred_flag
[2];
2040 int bmvtype
= BMV_TYPE_BACKWARD
;
2041 int block_cbp
= 0, pat
, block_tt
= 0;
2045 mquant
= v
->pq
; /* Lossy initialization */
2048 idx_mbmode
= get_vlc2(gb
, v
->mbmode_vlc
->table
, VC1_IF_MBMODE_VLC_BITS
, 2);
2049 if (idx_mbmode
<= 1) { // intra MB
2050 v
->is_intra
[s
->mb_x
] = 0x3f; // Set the bitfield to all 1.
2052 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
2053 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
2054 s
->current_picture
.mb_type
[mb_pos
+ v
->mb_off
] = MB_TYPE_INTRA
;
2056 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2057 /* Set DC scale - y and c use the same (not sure if necessary here) */
2058 s
->y_dc_scale
= s
->y_dc_scale_table
[FFABS(mquant
)];
2059 s
->c_dc_scale
= s
->c_dc_scale_table
[FFABS(mquant
)];
2060 v
->s
.ac_pred
= v
->acpred_plane
[mb_pos
] = get_bits1(gb
);
2061 mb_has_coeffs
= idx_mbmode
& 1;
2063 cbp
= 1 + get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_ICBPCY_VLC_BITS
, 2);
2065 for (i
= 0; i
< 6; i
++) {
2066 v
->a_avail
= v
->c_avail
= 0;
2067 v
->mb_type
[0][s
->block_index
[i
]] = 1;
2068 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2070 val
= ((cbp
>> (5 - i
)) & 1);
2071 if (i
== 2 || i
== 3 || !s
->first_slice_line
)
2072 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
2073 if (i
== 1 || i
== 3 || s
->mb_x
)
2074 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
2076 ret
= vc1_decode_intra_block(v
, s
->block
[i
], i
, val
, mquant
,
2077 (i
& 4) ? v
->codingset2
: v
->codingset
);
2080 if (CONFIG_GRAY
&& (i
> 3) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
2082 v
->vc1dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
2084 for (j
= 0; j
< 64; j
++)
2085 s
->block
[i
][j
] <<= 1;
2086 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
2087 s
->idsp
.put_signed_pixels_clamped(s
->block
[i
],
2088 s
->dest
[dst_idx
] + off
,
2089 (i
& 4) ? s
->uvlinesize
2093 s
->mb_intra
= v
->is_intra
[s
->mb_x
] = 0;
2094 s
->current_picture
.mb_type
[mb_pos
+ v
->mb_off
] = MB_TYPE_16x16
;
2095 for (i
= 0; i
< 6; i
++)
2096 v
->mb_type
[0][s
->block_index
[i
]] = 0;
2098 fwd
= v
->forward_mb_plane
[mb_pos
] = get_bits1(gb
);
2100 fwd
= v
->forward_mb_plane
[mb_pos
];
2101 if (idx_mbmode
<= 5) { // 1-MV
2103 dmv_x
[0] = dmv_x
[1] = dmv_y
[0] = dmv_y
[1] = 0;
2104 pred_flag
[0] = pred_flag
[1] = 0;
2106 bmvtype
= BMV_TYPE_FORWARD
;
2108 bmvtype
= decode012(gb
);
2111 bmvtype
= BMV_TYPE_BACKWARD
;
2114 bmvtype
= BMV_TYPE_DIRECT
;
2117 bmvtype
= BMV_TYPE_INTERPOLATED
;
2118 interpmvp
= get_bits1(gb
);
2121 v
->bmvtype
= bmvtype
;
2122 if (bmvtype
!= BMV_TYPE_DIRECT
&& idx_mbmode
& 1) {
2123 get_mvdata_interlaced(v
, &dmv_x
[bmvtype
== BMV_TYPE_BACKWARD
], &dmv_y
[bmvtype
== BMV_TYPE_BACKWARD
], &pred_flag
[bmvtype
== BMV_TYPE_BACKWARD
]);
2126 get_mvdata_interlaced(v
, &dmv_x
[1], &dmv_y
[1], &pred_flag
[1]);
2128 if (bmvtype
== BMV_TYPE_DIRECT
) {
2129 dmv_x
[0] = dmv_y
[0] = pred_flag
[0] = 0;
2130 dmv_x
[1] = dmv_y
[1] = pred_flag
[0] = 0;
2131 if (!s
->next_picture_ptr
->field_picture
) {
2132 av_log(s
->avctx
, AV_LOG_ERROR
, "Mixed field/frame direct mode not supported\n");
2133 return AVERROR_INVALIDDATA
;
2136 ff_vc1_pred_b_mv_intfi(v
, 0, dmv_x
, dmv_y
, 1, pred_flag
);
2137 vc1_b_mc(v
, dmv_x
, dmv_y
, (bmvtype
== BMV_TYPE_DIRECT
), bmvtype
);
2138 mb_has_coeffs
= !(idx_mbmode
& 2);
2141 bmvtype
= BMV_TYPE_FORWARD
;
2142 v
->bmvtype
= bmvtype
;
2143 v
->fourmvbp
= get_vlc2(gb
, v
->fourmvbp_vlc
->table
, VC1_4MV_BLOCK_PATTERN_VLC_BITS
, 1);
2144 for (i
= 0; i
< 4; i
++) {
2145 dmv_x
[0] = dmv_y
[0] = pred_flag
[0] = 0;
2146 dmv_x
[1] = dmv_y
[1] = pred_flag
[1] = 0;
2147 if (v
->fourmvbp
& (8 >> i
)) {
2148 get_mvdata_interlaced(v
, &dmv_x
[bmvtype
== BMV_TYPE_BACKWARD
],
2149 &dmv_y
[bmvtype
== BMV_TYPE_BACKWARD
],
2150 &pred_flag
[bmvtype
== BMV_TYPE_BACKWARD
]);
2152 ff_vc1_pred_b_mv_intfi(v
, i
, dmv_x
, dmv_y
, 0, pred_flag
);
2153 ff_vc1_mc_4mv_luma(v
, i
, bmvtype
== BMV_TYPE_BACKWARD
, 0);
2155 ff_vc1_mc_4mv_chroma(v
, bmvtype
== BMV_TYPE_BACKWARD
);
2156 mb_has_coeffs
= idx_mbmode
& 1;
2159 cbp
= 1 + get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
2163 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2164 if (!v
->ttmbf
&& cbp
) {
2165 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
2168 for (i
= 0; i
< 6; i
++) {
2169 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2171 val
= ((cbp
>> (5 - i
)) & 1);
2172 off
= (i
& 4) ? 0 : (i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
;
2174 pat
= vc1_decode_p_block(v
, s
->block
[i
], i
, mquant
, ttmb
,
2175 first_block
, s
->dest
[dst_idx
] + off
,
2176 (i
& 4) ? s
->uvlinesize
: s
->linesize
,
2177 CONFIG_GRAY
&& (i
& 4) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
), &block_tt
);
2180 block_cbp
|= pat
<< (i
<< 2);
2181 if (!v
->ttmbf
&& ttmb
< 8)
2187 v
->cbp
[s
->mb_x
] = block_cbp
;
2188 v
->ttblk
[s
->mb_x
] = block_tt
;
2193 /** Decode one B-frame MB (in interlaced frame B picture)
2195 static int vc1_decode_b_mb_intfr(VC1Context
*v
)
2197 MpegEncContext
*s
= &v
->s
;
2198 GetBitContext
*gb
= &s
->gb
;
2200 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2201 int cbp
= 0; /* cbp decoding stuff */
2202 int mqdiff
, mquant
; /* MB quantization */
2203 int ttmb
= v
->ttfrm
; /* MB Transform type */
2204 int mvsw
= 0; /* motion vector switch */
2205 int mb_has_coeffs
= 1; /* last_flag */
2206 int dmv_x
, dmv_y
; /* Differential MV components */
2207 int val
; /* temp value */
2208 int first_block
= 1;
2210 int skipped
, direct
, twomv
= 0;
2211 int block_cbp
= 0, pat
, block_tt
= 0;
2212 int idx_mbmode
= 0, mvbp
;
2213 int stride_y
, fieldtx
;
2214 int bmvtype
= BMV_TYPE_BACKWARD
;
2218 mquant
= v
->pq
; /* Lossy initialization */
2221 skipped
= get_bits1(gb
);
2223 skipped
= v
->s
.mbskip_table
[mb_pos
];
2226 idx_mbmode
= get_vlc2(gb
, v
->mbmode_vlc
->table
, VC1_INTFR_NON4MV_MBMODE_VLC_BITS
, 2);
2227 if (ff_vc1_mbmode_intfrp
[0][idx_mbmode
][0] == MV_PMODE_INTFR_2MV_FIELD
) {
2229 v
->blk_mv_type
[s
->block_index
[0]] = 1;
2230 v
->blk_mv_type
[s
->block_index
[1]] = 1;
2231 v
->blk_mv_type
[s
->block_index
[2]] = 1;
2232 v
->blk_mv_type
[s
->block_index
[3]] = 1;
2234 v
->blk_mv_type
[s
->block_index
[0]] = 0;
2235 v
->blk_mv_type
[s
->block_index
[1]] = 0;
2236 v
->blk_mv_type
[s
->block_index
[2]] = 0;
2237 v
->blk_mv_type
[s
->block_index
[3]] = 0;
2241 if (ff_vc1_mbmode_intfrp
[0][idx_mbmode
][0] == MV_PMODE_INTFR_INTRA
) { // intra MB
2242 for (i
= 0; i
< 4; i
++) {
2243 s
->mv
[0][i
][0] = s
->current_picture
.motion_val
[0][s
->block_index
[i
]][0] = 0;
2244 s
->mv
[0][i
][1] = s
->current_picture
.motion_val
[0][s
->block_index
[i
]][1] = 0;
2245 s
->mv
[1][i
][0] = s
->current_picture
.motion_val
[1][s
->block_index
[i
]][0] = 0;
2246 s
->mv
[1][i
][1] = s
->current_picture
.motion_val
[1][s
->block_index
[i
]][1] = 0;
2248 v
->is_intra
[s
->mb_x
] = 0x3f; // Set the bitfield to all 1.
2250 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_INTRA
;
2251 fieldtx
= v
->fieldtx_plane
[mb_pos
] = get_bits1(gb
);
2252 mb_has_coeffs
= get_bits1(gb
);
2254 cbp
= 1 + get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
2255 v
->s
.ac_pred
= v
->acpred_plane
[mb_pos
] = get_bits1(gb
);
2257 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2258 /* Set DC scale - y and c use the same (not sure if necessary here) */
2259 s
->y_dc_scale
= s
->y_dc_scale_table
[FFABS(mquant
)];
2260 s
->c_dc_scale
= s
->c_dc_scale_table
[FFABS(mquant
)];
2262 for (i
= 0; i
< 6; i
++) {
2263 v
->a_avail
= v
->c_avail
= 0;
2264 v
->mb_type
[0][s
->block_index
[i
]] = 1;
2265 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2267 val
= ((cbp
>> (5 - i
)) & 1);
2268 if (i
== 2 || i
== 3 || !s
->first_slice_line
)
2269 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
2270 if (i
== 1 || i
== 3 || s
->mb_x
)
2271 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
2273 ret
= vc1_decode_intra_block(v
, s
->block
[i
], i
, val
, mquant
,
2274 (i
& 4) ? v
->codingset2
: v
->codingset
);
2277 if (CONFIG_GRAY
&& i
> 3 && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
2279 v
->vc1dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
2281 stride_y
= s
->linesize
<< fieldtx
;
2282 off
= (fieldtx
) ? ((i
& 1) * 8) + ((i
& 2) >> 1) * s
->linesize
: (i
& 1) * 8 + 4 * (i
& 2) * s
->linesize
;
2284 stride_y
= s
->uvlinesize
;
2287 s
->idsp
.put_signed_pixels_clamped(s
->block
[i
],
2288 s
->dest
[dst_idx
] + off
,
2292 s
->mb_intra
= v
->is_intra
[s
->mb_x
] = 0;
2295 direct
= get_bits1(gb
);
2297 direct
= v
->direct_mb_plane
[mb_pos
];
2300 if (s
->next_picture_ptr
->field_picture
)
2301 av_log(s
->avctx
, AV_LOG_WARNING
, "Mixed frame/field direct mode not supported\n");
2302 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
);
2303 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
);
2304 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
);
2305 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
);
2308 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
);
2309 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
);
2310 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
);
2311 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
);
2313 for (i
= 1; i
< 4; i
+= 2) {
2314 s
->mv
[0][i
][0] = s
->current_picture
.motion_val
[0][s
->block_index
[i
]][0] = s
->mv
[0][i
-1][0];
2315 s
->mv
[0][i
][1] = s
->current_picture
.motion_val
[0][s
->block_index
[i
]][1] = s
->mv
[0][i
-1][1];
2316 s
->mv
[1][i
][0] = s
->current_picture
.motion_val
[1][s
->block_index
[i
]][0] = s
->mv
[1][i
-1][0];
2317 s
->mv
[1][i
][1] = s
->current_picture
.motion_val
[1][s
->block_index
[i
]][1] = s
->mv
[1][i
-1][1];
2320 for (i
= 1; i
< 4; i
++) {
2321 s
->mv
[0][i
][0] = s
->current_picture
.motion_val
[0][s
->block_index
[i
]][0] = s
->mv
[0][0][0];
2322 s
->mv
[0][i
][1] = s
->current_picture
.motion_val
[0][s
->block_index
[i
]][1] = s
->mv
[0][0][1];
2323 s
->mv
[1][i
][0] = s
->current_picture
.motion_val
[1][s
->block_index
[i
]][0] = s
->mv
[1][0][0];
2324 s
->mv
[1][i
][1] = s
->current_picture
.motion_val
[1][s
->block_index
[i
]][1] = s
->mv
[1][0][1];
2330 if (skipped
|| !s
->mb_intra
) {
2331 bmvtype
= decode012(gb
);
2334 bmvtype
= (v
->bfraction
>= (B_FRACTION_DEN
/2)) ? BMV_TYPE_BACKWARD
: BMV_TYPE_FORWARD
;
2337 bmvtype
= (v
->bfraction
>= (B_FRACTION_DEN
/2)) ? BMV_TYPE_FORWARD
: BMV_TYPE_BACKWARD
;
2340 bmvtype
= BMV_TYPE_INTERPOLATED
;
2344 if (twomv
&& bmvtype
!= BMV_TYPE_INTERPOLATED
)
2345 mvsw
= get_bits1(gb
);
2348 if (!skipped
) { // inter MB
2349 mb_has_coeffs
= ff_vc1_mbmode_intfrp
[0][idx_mbmode
][3];
2351 cbp
= 1 + get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
2353 if (bmvtype
== BMV_TYPE_INTERPOLATED
&& twomv
) {
2354 v
->fourmvbp
= get_vlc2(gb
, v
->fourmvbp_vlc
->table
, VC1_4MV_BLOCK_PATTERN_VLC_BITS
, 1);
2355 } else if (bmvtype
== BMV_TYPE_INTERPOLATED
|| twomv
) {
2356 v
->twomvbp
= get_vlc2(gb
, v
->twomvbp_vlc
->table
, VC1_2MV_BLOCK_PATTERN_VLC_BITS
, 1);
2360 for (i
= 0; i
< 6; i
++)
2361 v
->mb_type
[0][s
->block_index
[i
]] = 0;
2362 fieldtx
= v
->fieldtx_plane
[mb_pos
] = ff_vc1_mbmode_intfrp
[0][idx_mbmode
][1];
2363 /* for all motion vector read MVDATA and motion compensate each block */
2367 for (i
= 0; i
< 4; i
++) {
2368 ff_vc1_mc_4mv_luma(v
, i
, 0, 0);
2369 ff_vc1_mc_4mv_luma(v
, i
, 1, 1);
2371 ff_vc1_mc_4mv_chroma4(v
, 0, 0, 0);
2372 ff_vc1_mc_4mv_chroma4(v
, 1, 1, 1);
2374 ff_vc1_mc_1mv(v
, 0);
2375 ff_vc1_interp_mc(v
);
2377 } else if (twomv
&& bmvtype
== BMV_TYPE_INTERPOLATED
) {
2379 for (i
= 0; i
< 4; i
++) {
2382 val
= ((mvbp
>> (3 - i
)) & 1);
2384 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
2386 ff_vc1_pred_mv_intfr(v
, j
, dmv_x
, dmv_y
, 2, v
->range_x
, v
->range_y
, dir
);
2387 ff_vc1_mc_4mv_luma(v
, j
, dir
, dir
);
2388 ff_vc1_mc_4mv_luma(v
, j
+1, dir
, dir
);
2391 ff_vc1_mc_4mv_chroma4(v
, 0, 0, 0);
2392 ff_vc1_mc_4mv_chroma4(v
, 1, 1, 1);
2393 } else if (bmvtype
== BMV_TYPE_INTERPOLATED
) {
2397 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
2399 ff_vc1_pred_mv_intfr(v
, 0, dmv_x
, dmv_y
, 1, v
->range_x
, v
->range_y
, 0);
2400 ff_vc1_mc_1mv(v
, 0);
2404 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
2406 ff_vc1_pred_mv_intfr(v
, 0, dmv_x
, dmv_y
, 1, v
->range_x
, v
->range_y
, 1);
2407 ff_vc1_interp_mc(v
);
2409 dir
= bmvtype
== BMV_TYPE_BACKWARD
;
2416 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
2417 ff_vc1_pred_mv_intfr(v
, 0, dmv_x
, dmv_y
, 2, v
->range_x
, v
->range_y
, dir
);
2421 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
2422 ff_vc1_pred_mv_intfr(v
, 2, dmv_x
, dmv_y
, 2, v
->range_x
, v
->range_y
, dir2
);
2425 for (i
= 0; i
< 2; i
++) {
2426 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];
2427 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];
2428 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];
2429 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];
2432 ff_vc1_pred_mv_intfr(v
, 0, 0, 0, 2, v
->range_x
, v
->range_y
, !dir
);
2433 ff_vc1_pred_mv_intfr(v
, 2, 0, 0, 2, v
->range_x
, v
->range_y
, !dir
);
2436 ff_vc1_mc_4mv_luma(v
, 0, dir
, 0);
2437 ff_vc1_mc_4mv_luma(v
, 1, dir
, 0);
2438 ff_vc1_mc_4mv_luma(v
, 2, dir2
, 0);
2439 ff_vc1_mc_4mv_luma(v
, 3, dir2
, 0);
2440 ff_vc1_mc_4mv_chroma4(v
, dir
, dir2
, 0);
2442 dir
= bmvtype
== BMV_TYPE_BACKWARD
;
2444 mvbp
= ff_vc1_mbmode_intfrp
[0][idx_mbmode
][2];
2447 get_mvdata_interlaced(v
, &dmv_x
, &dmv_y
, 0);
2449 ff_vc1_pred_mv_intfr(v
, 0, dmv_x
, dmv_y
, 1, v
->range_x
, v
->range_y
, dir
);
2450 v
->blk_mv_type
[s
->block_index
[0]] = 1;
2451 v
->blk_mv_type
[s
->block_index
[1]] = 1;
2452 v
->blk_mv_type
[s
->block_index
[2]] = 1;
2453 v
->blk_mv_type
[s
->block_index
[3]] = 1;
2454 ff_vc1_pred_mv_intfr(v
, 0, 0, 0, 2, v
->range_x
, v
->range_y
, !dir
);
2455 for (i
= 0; i
< 2; i
++) {
2456 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];
2457 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];
2459 ff_vc1_mc_1mv(v
, dir
);
2463 GET_MQUANT(); // p. 227
2464 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2465 if (!v
->ttmbf
&& cbp
)
2466 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
2467 for (i
= 0; i
< 6; i
++) {
2468 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2470 val
= ((cbp
>> (5 - i
)) & 1);
2472 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
2474 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + ((i
> 1) * s
->linesize
));
2476 pat
= vc1_decode_p_block(v
, s
->block
[i
], i
, mquant
, ttmb
,
2477 first_block
, s
->dest
[dst_idx
] + off
,
2478 (i
& 4) ? s
->uvlinesize
: (s
->linesize
<< fieldtx
),
2479 CONFIG_GRAY
&& (i
& 4) && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
), &block_tt
);
2482 block_cbp
|= pat
<< (i
<< 2);
2483 if (!v
->ttmbf
&& ttmb
< 8)
2491 for (i
= 0; i
< 6; i
++) {
2492 v
->mb_type
[0][s
->block_index
[i
]] = 0;
2493 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2495 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_SKIP
;
2496 s
->current_picture
.qscale_table
[mb_pos
] = 0;
2497 v
->blk_mv_type
[s
->block_index
[0]] = 0;
2498 v
->blk_mv_type
[s
->block_index
[1]] = 0;
2499 v
->blk_mv_type
[s
->block_index
[2]] = 0;
2500 v
->blk_mv_type
[s
->block_index
[3]] = 0;
2503 if (bmvtype
== BMV_TYPE_INTERPOLATED
) {
2504 ff_vc1_pred_mv_intfr(v
, 0, 0, 0, 1, v
->range_x
, v
->range_y
, 0);
2505 ff_vc1_pred_mv_intfr(v
, 0, 0, 0, 1, v
->range_x
, v
->range_y
, 1);
2507 dir
= bmvtype
== BMV_TYPE_BACKWARD
;
2508 ff_vc1_pred_mv_intfr(v
, 0, 0, 0, 1, v
->range_x
, v
->range_y
, dir
);
2513 for (i
= 0; i
< 2; i
++) {
2514 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];
2515 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];
2516 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];
2517 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];
2520 v
->blk_mv_type
[s
->block_index
[0]] = 1;
2521 v
->blk_mv_type
[s
->block_index
[1]] = 1;
2522 v
->blk_mv_type
[s
->block_index
[2]] = 1;
2523 v
->blk_mv_type
[s
->block_index
[3]] = 1;
2524 ff_vc1_pred_mv_intfr(v
, 0, 0, 0, 2, v
->range_x
, v
->range_y
, !dir
);
2525 for (i
= 0; i
< 2; i
++) {
2526 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];
2527 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];
2533 ff_vc1_mc_1mv(v
, dir
);
2534 if (direct
|| bmvtype
== BMV_TYPE_INTERPOLATED
) {
2535 ff_vc1_interp_mc(v
);
2537 v
->fieldtx_plane
[mb_pos
] = 0;
2540 v
->cbp
[s
->mb_x
] = block_cbp
;
2541 v
->ttblk
[s
->mb_x
] = block_tt
;
2546 /** Decode blocks of I-frame
2548 static void vc1_decode_i_blocks(VC1Context
*v
)
2551 MpegEncContext
*s
= &v
->s
;
2556 /* select coding mode used for VLC tables selection */
2557 switch (v
->y_ac_table_index
) {
2559 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
2562 v
->codingset
= CS_HIGH_MOT_INTRA
;
2565 v
->codingset
= CS_MID_RATE_INTRA
;
2569 switch (v
->c_ac_table_index
) {
2571 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
2574 v
->codingset2
= CS_HIGH_MOT_INTER
;
2577 v
->codingset2
= CS_MID_RATE_INTER
;
2581 /* Set DC scale - y and c use the same */
2582 s
->y_dc_scale
= s
->y_dc_scale_table
[v
->pq
];
2583 s
->c_dc_scale
= s
->c_dc_scale_table
[v
->pq
];
2586 s
->mb_x
= s
->mb_y
= 0;
2588 s
->first_slice_line
= 1;
2589 for (s
->mb_y
= s
->start_mb_y
; s
->mb_y
< s
->end_mb_y
; s
->mb_y
++) {
2591 init_block_index(v
);
2592 for (; s
->mb_x
< v
->end_mb_x
; s
->mb_x
++) {
2593 ff_update_block_index(s
);
2594 s
->bdsp
.clear_blocks(v
->block
[v
->cur_blk_idx
][0]);
2595 mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_width
;
2596 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_INTRA
;
2597 s
->current_picture
.qscale_table
[mb_pos
] = v
->pq
;
2598 for (int i
= 0; i
< 4; i
++) {
2599 s
->current_picture
.motion_val
[1][s
->block_index
[i
]][0] = 0;
2600 s
->current_picture
.motion_val
[1][s
->block_index
[i
]][1] = 0;
2603 // do actual MB decoding and displaying
2604 cbp
= get_vlc2(&v
->s
.gb
, ff_msmp4_mb_i_vlc
.table
, MB_INTRA_VLC_BITS
, 2);
2605 v
->s
.ac_pred
= get_bits1(&v
->s
.gb
);
2607 for (k
= 0; k
< 6; k
++) {
2608 v
->mb_type
[0][s
->block_index
[k
]] = 1;
2610 val
= ((cbp
>> (5 - k
)) & 1);
2613 int pred
= vc1_coded_block_pred(&v
->s
, k
, &coded_val
);
2617 cbp
|= val
<< (5 - k
);
2619 vc1_decode_i_block(v
, v
->block
[v
->cur_blk_idx
][block_map
[k
]], k
, val
, (k
< 4) ? v
->codingset
: v
->codingset2
);
2621 if (CONFIG_GRAY
&& k
> 3 && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
2623 v
->vc1dsp
.vc1_inv_trans_8x8(v
->block
[v
->cur_blk_idx
][block_map
[k
]]);
2626 if (v
->overlap
&& v
->pq
>= 9) {
2627 ff_vc1_i_overlap_filter(v
);
2629 for (k
= 0; k
< 6; k
++)
2630 for (j
= 0; j
< 64; j
++)
2631 v
->block
[v
->cur_blk_idx
][block_map
[k
]][j
] *= 2;
2632 vc1_put_blocks_clamped(v
, 1);
2635 for (k
= 0; k
< 6; k
++)
2636 for (j
= 0; j
< 64; j
++)
2637 v
->block
[v
->cur_blk_idx
][block_map
[k
]][j
] = (v
->block
[v
->cur_blk_idx
][block_map
[k
]][j
] - 64) * 2;
2638 vc1_put_blocks_clamped(v
, 0);
2641 if (v
->s
.loop_filter
)
2642 ff_vc1_i_loop_filter(v
);
2644 if (get_bits_left(&s
->gb
) < 0) {
2645 ff_er_add_slice(&s
->er
, 0, 0, s
->mb_x
, s
->mb_y
, ER_MB_ERROR
);
2646 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i\n",
2647 get_bits_count(&s
->gb
), s
->gb
.size_in_bits
);
2651 v
->topleft_blk_idx
= (v
->topleft_blk_idx
+ 1) % (v
->end_mb_x
+ 2);
2652 v
->top_blk_idx
= (v
->top_blk_idx
+ 1) % (v
->end_mb_x
+ 2);
2653 v
->left_blk_idx
= (v
->left_blk_idx
+ 1) % (v
->end_mb_x
+ 2);
2654 v
->cur_blk_idx
= (v
->cur_blk_idx
+ 1) % (v
->end_mb_x
+ 2);
2656 if (!v
->s
.loop_filter
)
2657 ff_mpeg_draw_horiz_band(s
, s
->mb_y
* 16, 16);
2659 ff_mpeg_draw_horiz_band(s
, (s
->mb_y
- 1) * 16, 16);
2661 s
->first_slice_line
= 0;
2663 if (v
->s
.loop_filter
)
2664 ff_mpeg_draw_horiz_band(s
, (s
->end_mb_y
- 1) * 16, 16);
2666 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2667 * profile, these only differ are when decoding MSS2 rectangles. */
2668 ff_er_add_slice(&s
->er
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, ER_MB_END
);
2671 /** Decode blocks of I-frame for advanced profile
2673 static int vc1_decode_i_blocks_adv(VC1Context
*v
)
2676 MpegEncContext
*s
= &v
->s
;
2682 GetBitContext
*gb
= &s
->gb
;
2684 if (get_bits_left(gb
) <= 1)
2685 return AVERROR_INVALIDDATA
;
2687 /* select coding mode used for VLC tables selection */
2688 switch (v
->y_ac_table_index
) {
2690 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
2693 v
->codingset
= CS_HIGH_MOT_INTRA
;
2696 v
->codingset
= CS_MID_RATE_INTRA
;
2700 switch (v
->c_ac_table_index
) {
2702 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
2705 v
->codingset2
= CS_HIGH_MOT_INTER
;
2708 v
->codingset2
= CS_MID_RATE_INTER
;
2713 s
->mb_x
= s
->mb_y
= 0;
2715 s
->first_slice_line
= 1;
2716 s
->mb_y
= s
->start_mb_y
;
2717 if (s
->start_mb_y
) {
2719 init_block_index(v
);
2720 memset(&s
->coded_block
[s
->block_index
[0] - s
->b8_stride
], 0,
2721 (1 + s
->b8_stride
) * sizeof(*s
->coded_block
));
2723 for (; s
->mb_y
< s
->end_mb_y
; s
->mb_y
++) {
2725 init_block_index(v
);
2726 for (;s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
2728 ff_update_block_index(s
);
2729 s
->bdsp
.clear_blocks(v
->block
[v
->cur_blk_idx
][0]);
2730 mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2731 s
->current_picture
.mb_type
[mb_pos
+ v
->mb_off
] = MB_TYPE_INTRA
;
2732 for (int i
= 0; i
< 4; i
++) {
2733 s
->current_picture
.motion_val
[1][s
->block_index
[i
] + v
->blocks_off
][0] = 0;
2734 s
->current_picture
.motion_val
[1][s
->block_index
[i
] + v
->blocks_off
][1] = 0;
2737 // do actual MB decoding and displaying
2738 if (v
->fieldtx_is_raw
)
2739 v
->fieldtx_plane
[mb_pos
] = get_bits1(&v
->s
.gb
);
2740 if (get_bits_left(&v
->s
.gb
) <= 1) {
2741 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
, s
->mb_x
, s
->mb_y
, ER_MB_ERROR
);
2745 cbp
= get_vlc2(&v
->s
.gb
, ff_msmp4_mb_i_vlc
.table
, MB_INTRA_VLC_BITS
, 2);
2746 if (v
->acpred_is_raw
)
2747 v
->s
.ac_pred
= get_bits1(&v
->s
.gb
);
2749 v
->s
.ac_pred
= v
->acpred_plane
[mb_pos
];
2751 if (v
->condover
== CONDOVER_SELECT
&& v
->overflg_is_raw
)
2752 v
->over_flags_plane
[mb_pos
] = get_bits1(&v
->s
.gb
);
2756 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2757 /* Set DC scale - y and c use the same */
2758 s
->y_dc_scale
= s
->y_dc_scale_table
[FFABS(mquant
)];
2759 s
->c_dc_scale
= s
->c_dc_scale_table
[FFABS(mquant
)];
2761 for (k
= 0; k
< 6; k
++) {
2762 v
->mb_type
[0][s
->block_index
[k
]] = 1;
2764 val
= ((cbp
>> (5 - k
)) & 1);
2767 int pred
= vc1_coded_block_pred(&v
->s
, k
, &coded_val
);
2771 cbp
|= val
<< (5 - k
);
2773 v
->a_avail
= !s
->first_slice_line
|| (k
== 2 || k
== 3);
2774 v
->c_avail
= !!s
->mb_x
|| (k
== 1 || k
== 3);
2776 vc1_decode_i_block_adv(v
, v
->block
[v
->cur_blk_idx
][block_map
[k
]], k
, val
,
2777 (k
< 4) ? v
->codingset
: v
->codingset2
, mquant
);
2779 if (CONFIG_GRAY
&& k
> 3 && (s
->avctx
->flags
& AV_CODEC_FLAG_GRAY
))
2781 v
->vc1dsp
.vc1_inv_trans_8x8(v
->block
[v
->cur_blk_idx
][block_map
[k
]]);
2784 if (v
->overlap
&& (v
->pq
>= 9 || v
->condover
!= CONDOVER_NONE
))
2785 ff_vc1_i_overlap_filter(v
);
2786 vc1_put_blocks_clamped(v
, 1);
2787 if (v
->s
.loop_filter
)
2788 ff_vc1_i_loop_filter(v
);
2790 if (get_bits_left(&s
->gb
) < 0) {
2791 // TODO: may need modification to handle slice coding
2792 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
, s
->mb_x
, s
->mb_y
, ER_MB_ERROR
);
2793 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i\n",
2794 get_bits_count(&s
->gb
), s
->gb
.size_in_bits
);
2797 inc_blk_idx(v
->topleft_blk_idx
);
2798 inc_blk_idx(v
->top_blk_idx
);
2799 inc_blk_idx(v
->left_blk_idx
);
2800 inc_blk_idx(v
->cur_blk_idx
);
2802 if (!v
->s
.loop_filter
)
2803 ff_mpeg_draw_horiz_band(s
, s
->mb_y
* 16, 16);
2805 ff_mpeg_draw_horiz_band(s
, (s
->mb_y
-1) * 16, 16);
2806 s
->first_slice_line
= 0;
2809 if (v
->s
.loop_filter
)
2810 ff_mpeg_draw_horiz_band(s
, (s
->end_mb_y
- 1) * 16, 16);
2811 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
<< v
->field_mode
, s
->mb_width
- 1,
2812 (s
->end_mb_y
<< v
->field_mode
) - 1, ER_MB_END
);
2816 static void vc1_decode_p_blocks(VC1Context
*v
)
2818 MpegEncContext
*s
= &v
->s
;
2819 int apply_loop_filter
;
2822 /* select coding mode used for VLC tables selection */
2823 switch (v
->c_ac_table_index
) {
2825 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
2828 v
->codingset
= CS_HIGH_MOT_INTRA
;
2831 v
->codingset
= CS_MID_RATE_INTRA
;
2835 switch (v
->c_ac_table_index
) {
2837 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
2840 v
->codingset2
= CS_HIGH_MOT_INTER
;
2843 v
->codingset2
= CS_MID_RATE_INTER
;
2847 apply_loop_filter
= s
->loop_filter
&& !(s
->avctx
->skip_loop_filter
>= AVDISCARD_NONKEY
);
2848 s
->first_slice_line
= 1;
2849 memset(v
->cbp_base
, 0, sizeof(v
->cbp_base
[0]) * 3 * s
->mb_stride
);
2850 for (s
->mb_y
= s
->start_mb_y
; s
->mb_y
< s
->end_mb_y
; s
->mb_y
++) {
2852 init_block_index(v
);
2853 for (; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
2854 ff_update_block_index(s
);
2856 if (v
->fcm
== ILACE_FIELD
|| (v
->fcm
== PROGRESSIVE
&& v
->mv_type_is_raw
) || v
->skip_is_raw
)
2857 if (get_bits_left(&v
->s
.gb
) <= 1) {
2858 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
, s
->mb_x
, s
->mb_y
, ER_MB_ERROR
);
2862 if (v
->fcm
== ILACE_FIELD
) {
2863 ret
= vc1_decode_p_mb_intfi(v
);
2864 if (apply_loop_filter
)
2865 ff_vc1_p_loop_filter(v
);
2866 } else if (v
->fcm
== ILACE_FRAME
) {
2867 ret
= vc1_decode_p_mb_intfr(v
);
2868 if (apply_loop_filter
)
2869 ff_vc1_p_intfr_loop_filter(v
);
2871 ret
= vc1_decode_p_mb(v
);
2872 if (apply_loop_filter
)
2873 ff_vc1_p_loop_filter(v
);
2875 if (ret
< 0 || get_bits_left(&s
->gb
) < 0 || get_bits_count(&s
->gb
) < 0) {
2876 // TODO: may need modification to handle slice coding
2877 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
, s
->mb_x
, s
->mb_y
, ER_MB_ERROR
);
2878 av_log(s
->avctx
, AV_LOG_ERROR
, "Error or Bits overconsumption: %i > %i at %ix%i\n",
2879 get_bits_count(&s
->gb
), s
->gb
.size_in_bits
, s
->mb_x
, s
->mb_y
);
2882 inc_blk_idx(v
->topleft_blk_idx
);
2883 inc_blk_idx(v
->top_blk_idx
);
2884 inc_blk_idx(v
->left_blk_idx
);
2885 inc_blk_idx(v
->cur_blk_idx
);
2887 memmove(v
->cbp_base
,
2888 v
->cbp
- s
->mb_stride
,
2889 sizeof(v
->cbp_base
[0]) * 2 * s
->mb_stride
);
2890 memmove(v
->ttblk_base
,
2891 v
->ttblk
- s
->mb_stride
,
2892 sizeof(v
->ttblk_base
[0]) * 2 * s
->mb_stride
);
2893 memmove(v
->is_intra_base
,
2894 v
->is_intra
- s
->mb_stride
,
2895 sizeof(v
->is_intra_base
[0]) * 2 * s
->mb_stride
);
2896 memmove(v
->luma_mv_base
,
2897 v
->luma_mv
- s
->mb_stride
,
2898 sizeof(v
->luma_mv_base
[0]) * 2 * s
->mb_stride
);
2899 if (s
->mb_y
!= s
->start_mb_y
)
2900 ff_mpeg_draw_horiz_band(s
, (s
->mb_y
- 1) * 16, 16);
2901 s
->first_slice_line
= 0;
2903 if (s
->end_mb_y
>= s
->start_mb_y
)
2904 ff_mpeg_draw_horiz_band(s
, (s
->end_mb_y
- 1) * 16, 16);
2905 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
<< v
->field_mode
, s
->mb_width
- 1,
2906 (s
->end_mb_y
<< v
->field_mode
) - 1, ER_MB_END
);
2909 static void vc1_decode_b_blocks(VC1Context
*v
)
2911 MpegEncContext
*s
= &v
->s
;
2913 /* select coding mode used for VLC tables selection */
2914 switch (v
->c_ac_table_index
) {
2916 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
2919 v
->codingset
= CS_HIGH_MOT_INTRA
;
2922 v
->codingset
= CS_MID_RATE_INTRA
;
2926 switch (v
->c_ac_table_index
) {
2928 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
2931 v
->codingset2
= CS_HIGH_MOT_INTER
;
2934 v
->codingset2
= CS_MID_RATE_INTER
;
2938 s
->first_slice_line
= 1;
2939 for (s
->mb_y
= s
->start_mb_y
; s
->mb_y
< s
->end_mb_y
; s
->mb_y
++) {
2941 init_block_index(v
);
2942 for (; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
2943 ff_update_block_index(s
);
2945 if (v
->fcm
== ILACE_FIELD
|| v
->skip_is_raw
|| v
->dmb_is_raw
)
2946 if (get_bits_left(&v
->s
.gb
) <= 1) {
2947 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
, s
->mb_x
, s
->mb_y
, ER_MB_ERROR
);
2951 if (v
->fcm
== ILACE_FIELD
) {
2952 vc1_decode_b_mb_intfi(v
);
2953 if (v
->s
.loop_filter
)
2954 ff_vc1_b_intfi_loop_filter(v
);
2955 } else if (v
->fcm
== ILACE_FRAME
) {
2956 vc1_decode_b_mb_intfr(v
);
2957 if (v
->s
.loop_filter
)
2958 ff_vc1_p_intfr_loop_filter(v
);
2961 if (v
->s
.loop_filter
)
2962 ff_vc1_i_loop_filter(v
);
2964 if (get_bits_left(&s
->gb
) < 0 || get_bits_count(&s
->gb
) < 0) {
2965 // TODO: may need modification to handle slice coding
2966 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
, s
->mb_x
, s
->mb_y
, ER_MB_ERROR
);
2967 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i at %ix%i\n",
2968 get_bits_count(&s
->gb
), s
->gb
.size_in_bits
, s
->mb_x
, s
->mb_y
);
2972 memmove(v
->cbp_base
,
2973 v
->cbp
- s
->mb_stride
,
2974 sizeof(v
->cbp_base
[0]) * 2 * s
->mb_stride
);
2975 memmove(v
->ttblk_base
,
2976 v
->ttblk
- s
->mb_stride
,
2977 sizeof(v
->ttblk_base
[0]) * 2 * s
->mb_stride
);
2978 memmove(v
->is_intra_base
,
2979 v
->is_intra
- s
->mb_stride
,
2980 sizeof(v
->is_intra_base
[0]) * 2 * s
->mb_stride
);
2981 if (!v
->s
.loop_filter
)
2982 ff_mpeg_draw_horiz_band(s
, s
->mb_y
* 16, 16);
2984 ff_mpeg_draw_horiz_band(s
, (s
->mb_y
- 1) * 16, 16);
2985 s
->first_slice_line
= 0;
2987 if (v
->s
.loop_filter
)
2988 ff_mpeg_draw_horiz_band(s
, (s
->end_mb_y
- 1) * 16, 16);
2989 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
<< v
->field_mode
, s
->mb_width
- 1,
2990 (s
->end_mb_y
<< v
->field_mode
) - 1, ER_MB_END
);
2993 static void vc1_decode_skip_blocks(VC1Context
*v
)
2995 MpegEncContext
*s
= &v
->s
;
2997 if (!v
->s
.last_picture
.f
->data
[0])
3000 ff_er_add_slice(&s
->er
, 0, s
->start_mb_y
, s
->mb_width
- 1, s
->end_mb_y
- 1, ER_MB_END
);
3001 s
->first_slice_line
= 1;
3002 for (s
->mb_y
= s
->start_mb_y
; s
->mb_y
< s
->end_mb_y
; s
->mb_y
++) {
3004 init_block_index(v
);
3005 ff_update_block_index(s
);
3006 memcpy(s
->dest
[0], s
->last_picture
.f
->data
[0] + s
->mb_y
* 16 * s
->linesize
, s
->linesize
* 16);
3007 memcpy(s
->dest
[1], s
->last_picture
.f
->data
[1] + s
->mb_y
* 8 * s
->uvlinesize
, s
->uvlinesize
* 8);
3008 memcpy(s
->dest
[2], s
->last_picture
.f
->data
[2] + s
->mb_y
* 8 * s
->uvlinesize
, s
->uvlinesize
* 8);
3009 ff_mpeg_draw_horiz_band(s
, s
->mb_y
* 16, 16);
3010 s
->first_slice_line
= 0;
3012 s
->pict_type
= AV_PICTURE_TYPE_P
;
3015 void ff_vc1_decode_blocks(VC1Context
*v
)
3018 v
->s
.esc3_level_length
= 0;
3020 ff_intrax8_decode_picture(&v
->x8
, &v
->s
.current_picture
,
3021 &v
->s
.gb
, &v
->s
.mb_x
, &v
->s
.mb_y
,
3022 2 * v
->pq
+ v
->halfpq
, v
->pq
* !v
->pquantizer
,
3023 v
->s
.loop_filter
, v
->s
.low_delay
);
3025 ff_er_add_slice(&v
->s
.er
, 0, 0,
3026 (v
->s
.mb_x
>> 1) - 1, (v
->s
.mb_y
>> 1) - 1,
3030 v
->left_blk_idx
= -1;
3031 v
->topleft_blk_idx
= 1;
3033 switch (v
->s
.pict_type
) {
3034 case AV_PICTURE_TYPE_I
:
3035 if (v
->profile
== PROFILE_ADVANCED
)
3036 vc1_decode_i_blocks_adv(v
);
3038 vc1_decode_i_blocks(v
);
3040 case AV_PICTURE_TYPE_P
:
3041 if (v
->p_frame_skipped
)
3042 vc1_decode_skip_blocks(v
);
3044 vc1_decode_p_blocks(v
);
3046 case AV_PICTURE_TYPE_B
:
3048 if (v
->profile
== PROFILE_ADVANCED
)
3049 vc1_decode_i_blocks_adv(v
);
3051 vc1_decode_i_blocks(v
);
3053 vc1_decode_b_blocks(v
);