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 "msmpeg4data.h"
36 #include "vc1acdata.h"
39 #define MB_INTRA_VLC_BITS 9
42 // offset tables for interlaced picture MVDATA decoding
43 static const uint8_t offset_table[2][9] = {
44 { 0, 1, 2, 4, 8, 16, 32, 64, 128 },
45 { 0, 1, 3, 7, 15, 31, 63, 127, 255 },
48 /***********************************************************************/
50 * @name VC-1 Bitplane decoding
56 static inline void init_block_index(VC1Context *v)
58 MpegEncContext *s = &v->s;
59 ff_init_block_index(s);
60 if (v->field_mode && !(v->second_field ^ v->tff)) {
61 s->dest[0] += s->current_picture_ptr->f->linesize[0];
62 s->dest[1] += s->current_picture_ptr->f->linesize[1];
63 s->dest[2] += s->current_picture_ptr->f->linesize[2];
67 /** @} */ //Bitplane group
69 static void vc1_put_signed_blocks_clamped(VC1Context *v)
71 MpegEncContext *s = &v->s;
73 int block_count = CONFIG_GRAY && (s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 4 : 6;
77 /* The put pixels loop is one MB row and one MB column behind the decoding
78 * loop because we can only put pixels when overlap filtering is done. For
79 * interlaced frame pictures, however, the put pixels loop is only one
80 * column behind the decoding loop as interlaced frame pictures only need
81 * horizontal overlap filtering. */
82 if (!s->first_slice_line && v->fcm != ILACE_FRAME) {
84 for (i = 0; i < block_count; i++) {
85 if (i > 3 ? v->mb_type[0][s->block_index[i] - s->block_wrap[i] - 1] :
86 v->mb_type[0][s->block_index[i] - 2 * s->block_wrap[i] - 2]) {
87 dest = s->dest[0] + ((i & 2) - 4) * 4 * s->linesize + ((i & 1) - 2) * 8;
88 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][i],
89 i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize - 8 : dest,
90 i > 3 ? s->uvlinesize : s->linesize);
94 if (s->mb_x == s->mb_width - 1) {
95 for (i = 0; i < block_count; i++) {
96 if (i > 3 ? v->mb_type[0][s->block_index[i] - s->block_wrap[i]] :
97 v->mb_type[0][s->block_index[i] - 2 * s->block_wrap[i]]) {
98 dest = s->dest[0] + ((i & 2) - 4) * 4 * s->linesize + (i & 1) * 8;
99 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][i],
100 i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize : dest,
101 i > 3 ? s->uvlinesize : s->linesize);
106 if (s->mb_y == s->end_mb_y - 1 || v->fcm == ILACE_FRAME) {
108 if (v->fcm == ILACE_FRAME)
109 fieldtx = v->fieldtx_plane[s->mb_y * s->mb_stride + s->mb_x - 1];
110 for (i = 0; i < block_count; i++) {
111 if (i > 3 ? v->mb_type[0][s->block_index[i] - 1] :
112 v->mb_type[0][s->block_index[i] - 2]) {
114 dest = s->dest[0] + ((i & 2) >> 1) * s->linesize + ((i & 1) - 2) * 8;
116 dest = s->dest[0] + (i & 2) * 4 * s->linesize + ((i & 1) - 2) * 8;
117 s->idsp.put_signed_pixels_clamped(v->block[v->left_blk_idx][i],
118 i > 3 ? s->dest[i - 3] - 8 : dest,
119 i > 3 ? s->uvlinesize : s->linesize << fieldtx);
123 if (s->mb_x == s->mb_width - 1) {
124 if (v->fcm == ILACE_FRAME)
125 fieldtx = v->fieldtx_plane[s->mb_y * s->mb_stride + s->mb_x];
126 for (i = 0; i < block_count; i++) {
127 if (v->mb_type[0][s->block_index[i]]) {
129 dest = s->dest[0] + ((i & 2) >> 1) * s->linesize + (i & 1) * 8;
131 dest = s->dest[0] + (i & 2) * 4 * s->linesize + (i & 1) * 8;
132 s->idsp.put_signed_pixels_clamped(v->block[v->cur_blk_idx][i],
133 i > 3 ? s->dest[i - 3] : dest,
134 i > 3 ? s->uvlinesize : s->linesize << fieldtx);
141 #define inc_blk_idx(idx) do { \
143 if (idx >= v->n_allocated_blks) \
147 /***********************************************************************/
149 * @name VC-1 Block-level functions
150 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
156 * @brief Get macroblock-level quantizer scale
158 #define GET_MQUANT() \
159 if (v->dquantfrm) { \
161 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
162 if (v->dqbilevel) { \
163 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
165 mqdiff = get_bits(gb, 3); \
167 mquant = v->pq + mqdiff; \
169 mquant = get_bits(gb, 5); \
172 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
173 edges = 1 << v->dqsbedge; \
174 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
175 edges = (3 << v->dqsbedge) % 15; \
176 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
178 if ((edges&1) && !s->mb_x) \
180 if ((edges&2) && s->first_slice_line) \
182 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
184 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
186 if (!mquant || mquant > 31) { \
187 av_log(v->s.avctx, AV_LOG_ERROR, \
188 "Overriding invalid mquant %d\n", mquant); \
194 * @def GET_MVDATA(_dmv_x, _dmv_y)
195 * @brief Get MV differentials
196 * @see MVDATA decoding from 8.3.5.2, p(1)20
197 * @param _dmv_x Horizontal differential for decoded MV
198 * @param _dmv_y Vertical differential for decoded MV
200 #define GET_MVDATA(_dmv_x, _dmv_y) \
201 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
202 VC1_MV_DIFF_VLC_BITS, 2); \
210 _dmv_x = _dmv_y = 0; \
211 } else if (index == 35) { \
212 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
213 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
214 } else if (index == 36) { \
219 index1 = index % 6; \
220 _dmv_x = offset_table[1][index1]; \
221 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
223 val = get_bits(gb, val); \
224 sign = 0 - (val & 1); \
225 _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign; \
228 index1 = index / 6; \
229 _dmv_y = offset_table[1][index1]; \
230 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
232 val = get_bits(gb, val); \
233 sign = 0 - (val & 1); \
234 _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign; \
238 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
239 int *dmv_y, int *pred_flag)
242 int extend_x, extend_y;
243 GetBitContext *gb = &v->s.gb;
248 bits = VC1_2REF_MVDATA_VLC_BITS;
251 bits = VC1_1REF_MVDATA_VLC_BITS;
254 extend_x = v->dmvrange & 1;
255 extend_y = (v->dmvrange >> 1) & 1;
256 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
258 *dmv_x = get_bits(gb, v->k_x);
259 *dmv_y = get_bits(gb, v->k_y);
262 *pred_flag = *dmv_y & 1;
263 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
267 av_assert0(index < esc);
268 index1 = (index + 1) % 9;
270 val = get_bits(gb, index1 + extend_x);
271 sign = 0 - (val & 1);
272 *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
275 index1 = (index + 1) / 9;
276 if (index1 > v->numref) {
277 val = get_bits(gb, (index1 >> v->numref) + extend_y);
278 sign = 0 - (val & 1);
279 *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
282 if (v->numref && pred_flag)
283 *pred_flag = index1 & 1;
287 /** Reconstruct motion vector for B-frame and do motion compensation
289 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
290 int direct, int mode)
297 if (mode == BMV_TYPE_INTERPOLATED) {
303 ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
306 /** Get predicted DC value for I-frames only
307 * prediction dir: left=0, top=1
308 * @param s MpegEncContext
309 * @param overlap flag indicating that overlap filtering is used
310 * @param pq integer part of picture quantizer
311 * @param[in] n block index in the current MB
312 * @param dc_val_ptr Pointer to DC predictor
313 * @param dir_ptr Prediction direction for use in AC prediction
315 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
316 int16_t **dc_val_ptr, int *dir_ptr)
318 int a, b, c, wrap, pred, scale;
320 static const uint16_t dcpred[32] = {
321 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
322 114, 102, 93, 85, 79, 73, 68, 64,
323 60, 57, 54, 51, 49, 47, 45, 43,
324 41, 39, 38, 37, 35, 34, 33
327 /* find prediction - wmv3_dc_scale always used here in fact */
328 if (n < 4) scale = s->y_dc_scale;
329 else scale = s->c_dc_scale;
331 wrap = s->block_wrap[n];
332 dc_val = s->dc_val[0] + s->block_index[n];
338 b = dc_val[ - 1 - wrap];
341 if (pq < 9 || !overlap) {
342 /* Set outer values */
343 if (s->first_slice_line && (n != 2 && n != 3))
344 b = a = dcpred[scale];
345 if (s->mb_x == 0 && (n != 1 && n != 3))
346 b = c = dcpred[scale];
348 /* Set outer values */
349 if (s->first_slice_line && (n != 2 && n != 3))
351 if (s->mb_x == 0 && (n != 1 && n != 3))
355 if (abs(a - b) <= abs(b - c)) {
357 *dir_ptr = 1; // left
363 /* update predictor */
364 *dc_val_ptr = &dc_val[0];
369 /** Get predicted DC value
370 * prediction dir: left=0, top=1
371 * @param s MpegEncContext
372 * @param overlap flag indicating that overlap filtering is used
373 * @param pq integer part of picture quantizer
374 * @param[in] n block index in the current MB
375 * @param a_avail flag indicating top block availability
376 * @param c_avail flag indicating left block availability
377 * @param dc_val_ptr Pointer to DC predictor
378 * @param dir_ptr Prediction direction for use in AC prediction
380 static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
381 int a_avail, int c_avail,
382 int16_t **dc_val_ptr, int *dir_ptr)
384 int a, b, c, wrap, pred;
386 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
390 /* scale predictors if needed */
391 q1 = s->current_picture.qscale_table[mb_pos];
392 dqscale_index = s->y_dc_scale_table[q1] - 1;
393 if (dqscale_index < 0)
396 wrap = s->block_wrap[n];
397 dc_val = s->dc_val[0] + s->block_index[n];
403 b = dc_val[ - 1 - wrap];
406 if (c_avail && (n != 1 && n != 3)) {
407 q2 = s->current_picture.qscale_table[mb_pos - 1];
409 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
411 if (a_avail && (n != 2 && n != 3)) {
412 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
414 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
416 if (a_avail && c_avail && (n != 3)) {
422 q2 = s->current_picture.qscale_table[off];
424 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
427 if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
429 *dir_ptr = 1; // left
430 } else if (a_avail) {
435 *dir_ptr = 1; // left
438 /* update predictor */
439 *dc_val_ptr = &dc_val[0];
443 /** @} */ // Block group
446 * @name VC1 Macroblock-level functions in Simple/Main Profiles
447 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
451 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
452 uint8_t **coded_block_ptr)
454 int xy, wrap, pred, a, b, c;
456 xy = s->block_index[n];
462 a = s->coded_block[xy - 1 ];
463 b = s->coded_block[xy - 1 - wrap];
464 c = s->coded_block[xy - wrap];
473 *coded_block_ptr = &s->coded_block[xy];
479 * Decode one AC coefficient
480 * @param v The VC1 context
481 * @param last Last coefficient
482 * @param skip How much zero coefficients to skip
483 * @param value Decoded AC coefficient value
484 * @param codingset set of VLC to decode data
487 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
488 int *value, int codingset)
490 GetBitContext *gb = &v->s.gb;
491 int index, run, level, lst, sign;
493 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
494 if (index != ff_vc1_ac_sizes[codingset] - 1) {
495 run = vc1_index_decode_table[codingset][index][0];
496 level = vc1_index_decode_table[codingset][index][1];
497 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
498 sign = get_bits1(gb);
500 int escape = decode210(gb);
502 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
503 run = vc1_index_decode_table[codingset][index][0];
504 level = vc1_index_decode_table[codingset][index][1];
505 lst = index >= vc1_last_decode_table[codingset];
508 level += vc1_last_delta_level_table[codingset][run];
510 level += vc1_delta_level_table[codingset][run];
513 run += vc1_last_delta_run_table[codingset][level] + 1;
515 run += vc1_delta_run_table[codingset][level] + 1;
517 sign = get_bits1(gb);
520 if (v->s.esc3_level_length == 0) {
521 if (v->pq < 8 || v->dquantfrm) { // table 59
522 v->s.esc3_level_length = get_bits(gb, 3);
523 if (!v->s.esc3_level_length)
524 v->s.esc3_level_length = get_bits(gb, 2) + 8;
526 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
528 v->s.esc3_run_length = 3 + get_bits(gb, 2);
530 run = get_bits(gb, v->s.esc3_run_length);
531 sign = get_bits1(gb);
532 level = get_bits(gb, v->s.esc3_level_length);
538 *value = (level ^ -sign) + sign;
541 /** Decode intra block in intra frames - should be faster than decode_intra_block
542 * @param v VC1Context
543 * @param block block to decode
544 * @param[in] n subblock index
545 * @param coded are AC coeffs present or not
546 * @param codingset set of VLC to decode data
548 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
549 int coded, int codingset)
551 GetBitContext *gb = &v->s.gb;
552 MpegEncContext *s = &v->s;
553 int dc_pred_dir = 0; /* Direction of the DC prediction used */
556 int16_t *ac_val, *ac_val2;
559 /* Get DC differential */
561 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
563 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
566 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
570 const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
571 if (dcdiff == 119 /* ESC index value */) {
572 dcdiff = get_bits(gb, 8 + m);
575 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
582 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
585 /* Store the quantized DC coeff, used for prediction */
587 scale = s->y_dc_scale;
589 scale = s->c_dc_scale;
590 block[0] = dcdiff * scale;
592 ac_val = s->ac_val[0][s->block_index[n]];
594 if (dc_pred_dir) // left
597 ac_val -= 16 * s->block_wrap[n];
599 scale = v->pq * 2 + v->halfpq;
605 int last = 0, skip, value;
606 const uint8_t *zz_table;
611 zz_table = v->zz_8x8[2];
613 zz_table = v->zz_8x8[3];
615 zz_table = v->zz_8x8[1];
618 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
622 block[zz_table[i++]] = value;
625 /* apply AC prediction if needed */
628 if (dc_pred_dir) { // left
634 for (k = 1; k < 8; k++)
635 block[k << sh] += ac_val[k];
637 /* save AC coeffs for further prediction */
638 for (k = 1; k < 8; k++) {
639 ac_val2[k] = block[k << v->left_blk_sh];
640 ac_val2[k + 8] = block[k << v->top_blk_sh];
643 /* scale AC coeffs */
644 for (k = 1; k < 64; k++)
648 block[k] += (block[k] < 0) ? -v->pq : v->pq;
654 memset(ac_val2, 0, 16 * 2);
656 /* apply AC prediction if needed */
659 if (dc_pred_dir) { //left
666 memcpy(ac_val2, ac_val, 8 * 2);
667 for (k = 1; k < 8; k++) {
668 block[k << sh] = ac_val[k] * scale;
669 if (!v->pquantizer && block[k << sh])
670 block[k << sh] += (block[k << sh] < 0) ? -v->pq : v->pq;
674 if (s->ac_pred) i = 63;
675 s->block_last_index[n] = i;
680 /** Decode intra block in intra frames - should be faster than decode_intra_block
681 * @param v VC1Context
682 * @param block block to decode
683 * @param[in] n subblock number
684 * @param coded are AC coeffs present or not
685 * @param codingset set of VLC to decode data
686 * @param mquant quantizer value for this macroblock
688 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
689 int coded, int codingset, int mquant)
691 GetBitContext *gb = &v->s.gb;
692 MpegEncContext *s = &v->s;
693 int dc_pred_dir = 0; /* Direction of the DC prediction used */
695 int16_t *dc_val = NULL;
696 int16_t *ac_val, *ac_val2;
698 int a_avail = v->a_avail, c_avail = v->c_avail;
699 int use_pred = s->ac_pred;
702 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
704 /* Get DC differential */
706 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
708 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
711 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
715 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
716 if (dcdiff == 119 /* ESC index value */) {
717 dcdiff = get_bits(gb, 8 + m);
720 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
727 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
730 /* Store the quantized DC coeff, used for prediction */
732 scale = s->y_dc_scale;
734 scale = s->c_dc_scale;
735 block[0] = dcdiff * scale;
737 /* check if AC is needed at all */
738 if (!a_avail && !c_avail)
741 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
743 ac_val = s->ac_val[0][s->block_index[n]];
745 if (dc_pred_dir) // left
748 ac_val -= 16 * s->block_wrap[n];
750 q1 = s->current_picture.qscale_table[mb_pos];
753 else if (dc_pred_dir) {
756 else if (c_avail && mb_pos)
757 q2 = s->current_picture.qscale_table[mb_pos - 1];
761 else if (a_avail && mb_pos >= s->mb_stride)
762 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
769 int last = 0, skip, value;
770 const uint8_t *zz_table;
774 if (!use_pred && v->fcm == ILACE_FRAME) {
775 zz_table = v->zzi_8x8;
777 if (!dc_pred_dir) // top
778 zz_table = v->zz_8x8[2];
780 zz_table = v->zz_8x8[3];
783 if (v->fcm != ILACE_FRAME)
784 zz_table = v->zz_8x8[1];
786 zz_table = v->zzi_8x8;
790 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
794 block[zz_table[i++]] = value;
797 /* apply AC prediction if needed */
800 if (dc_pred_dir) { // left
806 /* scale predictors if needed*/
807 if (q2 && q1 != q2) {
808 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
810 return AVERROR_INVALIDDATA;
811 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
812 for (k = 1; k < 8; k++)
813 block[k << sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
815 for (k = 1; k < 8; k++)
816 block[k << sh] += ac_val[k];
819 /* save AC coeffs for further prediction */
820 for (k = 1; k < 8; k++) {
821 ac_val2[k ] = block[k << v->left_blk_sh];
822 ac_val2[k + 8] = block[k << v->top_blk_sh];
825 /* scale AC coeffs */
826 for (k = 1; k < 64; k++)
830 block[k] += (block[k] < 0) ? -mquant : mquant;
833 } else { // no AC coeffs
836 memset(ac_val2, 0, 16 * 2);
838 /* apply AC prediction if needed */
841 if (dc_pred_dir) { // left
848 memcpy(ac_val2, ac_val, 8 * 2);
849 if (q2 && q1 != q2) {
850 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
851 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
853 return AVERROR_INVALIDDATA;
854 for (k = 1; k < 8; k++)
855 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
857 for (k = 1; k < 8; k++) {
858 block[k << sh] = ac_val2[k] * scale;
859 if (!v->pquantizer && block[k << sh])
860 block[k << sh] += (block[k << sh] < 0) ? -mquant : mquant;
864 if (use_pred) i = 63;
865 s->block_last_index[n] = i;
870 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
871 * @param v VC1Context
872 * @param block block to decode
873 * @param[in] n subblock index
874 * @param coded are AC coeffs present or not
875 * @param mquant block quantizer
876 * @param codingset set of VLC to decode data
878 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
879 int coded, int mquant, int codingset)
881 GetBitContext *gb = &v->s.gb;
882 MpegEncContext *s = &v->s;
883 int dc_pred_dir = 0; /* Direction of the DC prediction used */
885 int16_t *dc_val = NULL;
886 int16_t *ac_val, *ac_val2;
888 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
889 int a_avail = v->a_avail, c_avail = v->c_avail;
890 int use_pred = s->ac_pred;
894 s->bdsp.clear_block(block);
896 /* XXX: Guard against dumb values of mquant */
897 mquant = av_clip_uintp2(mquant, 5);
899 /* Set DC scale - y and c use the same */
900 s->y_dc_scale = s->y_dc_scale_table[mquant];
901 s->c_dc_scale = s->c_dc_scale_table[mquant];
903 /* Get DC differential */
905 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
907 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
910 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
914 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
915 if (dcdiff == 119 /* ESC index value */) {
916 dcdiff = get_bits(gb, 8 + m);
919 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
926 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
929 /* Store the quantized DC coeff, used for prediction */
932 block[0] = dcdiff * s->y_dc_scale;
934 block[0] = dcdiff * s->c_dc_scale;
940 /* check if AC is needed at all and adjust direction if needed */
941 if (!a_avail) dc_pred_dir = 1;
942 if (!c_avail) dc_pred_dir = 0;
943 if (!a_avail && !c_avail) use_pred = 0;
944 ac_val = s->ac_val[0][s->block_index[n]];
947 scale = mquant * 2 + v->halfpq;
949 if (dc_pred_dir) //left
952 ac_val -= 16 * s->block_wrap[n];
954 q1 = s->current_picture.qscale_table[mb_pos];
955 if (dc_pred_dir && c_avail && mb_pos)
956 q2 = s->current_picture.qscale_table[mb_pos - 1];
957 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
958 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
959 if (dc_pred_dir && n == 1)
961 if (!dc_pred_dir && n == 2)
966 int last = 0, skip, value;
970 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
974 if (v->fcm == PROGRESSIVE)
975 block[v->zz_8x8[0][i++]] = value;
977 if (use_pred && (v->fcm == ILACE_FRAME)) {
978 if (!dc_pred_dir) // top
979 block[v->zz_8x8[2][i++]] = value;
981 block[v->zz_8x8[3][i++]] = value;
983 block[v->zzi_8x8[i++]] = value;
988 /* apply AC prediction if needed */
990 /* scale predictors if needed*/
991 if (q2 && q1 != q2) {
992 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
993 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
996 return AVERROR_INVALIDDATA;
997 if (dc_pred_dir) { // left
998 for (k = 1; k < 8; k++)
999 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1001 for (k = 1; k < 8; k++)
1002 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1005 if (dc_pred_dir) { // left
1006 for (k = 1; k < 8; k++)
1007 block[k << v->left_blk_sh] += ac_val[k];
1009 for (k = 1; k < 8; k++)
1010 block[k << v->top_blk_sh] += ac_val[k + 8];
1014 /* save AC coeffs for further prediction */
1015 for (k = 1; k < 8; k++) {
1016 ac_val2[k ] = block[k << v->left_blk_sh];
1017 ac_val2[k + 8] = block[k << v->top_blk_sh];
1020 /* scale AC coeffs */
1021 for (k = 1; k < 64; k++)
1025 block[k] += (block[k] < 0) ? -mquant : mquant;
1028 if (use_pred) i = 63;
1029 } else { // no AC coeffs
1032 memset(ac_val2, 0, 16 * 2);
1033 if (dc_pred_dir) { // left
1035 memcpy(ac_val2, ac_val, 8 * 2);
1036 if (q2 && q1 != q2) {
1037 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1038 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1040 return AVERROR_INVALIDDATA;
1041 for (k = 1; k < 8; k++)
1042 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1047 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1048 if (q2 && q1 != q2) {
1049 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1050 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1052 return AVERROR_INVALIDDATA;
1053 for (k = 1; k < 8; k++)
1054 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1059 /* apply AC prediction if needed */
1061 if (dc_pred_dir) { // left
1062 for (k = 1; k < 8; k++) {
1063 block[k << v->left_blk_sh] = ac_val2[k] * scale;
1064 if (!v->pquantizer && block[k << v->left_blk_sh])
1065 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1068 for (k = 1; k < 8; k++) {
1069 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1070 if (!v->pquantizer && block[k << v->top_blk_sh])
1071 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1077 s->block_last_index[n] = i;
1084 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1085 int mquant, int ttmb, int first_block,
1086 uint8_t *dst, int linesize, int skip_block,
1089 MpegEncContext *s = &v->s;
1090 GetBitContext *gb = &s->gb;
1093 int scale, off, idx, last, skip, value;
1094 int ttblk = ttmb & 7;
1097 s->bdsp.clear_block(block);
1100 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)];
1102 if (ttblk == TT_4X4) {
1103 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1105 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1106 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1107 || (!v->res_rtm_flag && !first_block))) {
1108 subblkpat = decode012(gb);
1110 subblkpat ^= 3; // swap decoded pattern bits
1111 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1113 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1116 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1118 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1119 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1120 subblkpat = 2 - (ttblk == TT_8X4_TOP);
1123 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1124 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1133 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1138 idx = v->zz_8x8[0][i++];
1140 idx = v->zzi_8x8[i++];
1141 block[idx] = value * scale;
1143 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1147 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1149 v->vc1dsp.vc1_inv_trans_8x8(block);
1150 s->idsp.add_pixels_clamped(block, dst, linesize);
1155 pat = ~subblkpat & 0xF;
1156 for (j = 0; j < 4; j++) {
1157 last = subblkpat & (1 << (3 - j));
1159 off = (j & 1) * 4 + (j & 2) * 16;
1161 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1166 idx = ff_vc1_simple_progressive_4x4_zz[i++];
1168 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1169 block[idx + off] = value * scale;
1171 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1173 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1175 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1177 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1182 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1183 for (j = 0; j < 2; j++) {
1184 last = subblkpat & (1 << (1 - j));
1188 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1193 idx = v->zz_8x4[i++] + off;
1195 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1196 block[idx] = value * scale;
1198 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1200 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1202 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1204 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1209 pat = ~(subblkpat * 5) & 0xF;
1210 for (j = 0; j < 2; j++) {
1211 last = subblkpat & (1 << (1 - j));
1215 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1220 idx = v->zz_4x8[i++] + off;
1222 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1223 block[idx] = value * scale;
1225 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1227 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1229 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1231 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1237 *ttmb_out |= ttblk << (n * 4);
1241 /** @} */ // Macroblock group
1243 static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 };
1245 /** Decode one P-frame MB
1247 static int vc1_decode_p_mb(VC1Context *v)
1249 MpegEncContext *s = &v->s;
1250 GetBitContext *gb = &s->gb;
1252 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1253 int cbp; /* cbp decoding stuff */
1254 int mqdiff, mquant; /* MB quantization */
1255 int ttmb = v->ttfrm; /* MB Transform type */
1257 int mb_has_coeffs = 1; /* last_flag */
1258 int dmv_x, dmv_y; /* Differential MV components */
1259 int index, index1; /* LUT indexes */
1260 int val, sign; /* temp values */
1261 int first_block = 1;
1263 int skipped, fourmv;
1264 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1266 mquant = v->pq; /* lossy initialization */
1268 if (v->mv_type_is_raw)
1269 fourmv = get_bits1(gb);
1271 fourmv = v->mv_type_mb_plane[mb_pos];
1273 skipped = get_bits1(gb);
1275 skipped = v->s.mbskip_table[mb_pos];
1277 if (!fourmv) { /* 1MV mode */
1279 GET_MVDATA(dmv_x, dmv_y);
1282 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1283 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1285 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1286 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1288 /* FIXME Set DC val for inter block ? */
1289 if (s->mb_intra && !mb_has_coeffs) {
1291 s->ac_pred = get_bits1(gb);
1293 } else if (mb_has_coeffs) {
1295 s->ac_pred = get_bits1(gb);
1296 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1302 s->current_picture.qscale_table[mb_pos] = mquant;
1304 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1305 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1306 VC1_TTMB_VLC_BITS, 2);
1307 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1309 for (i = 0; i < 6; i++) {
1310 s->dc_val[0][s->block_index[i]] = 0;
1312 val = ((cbp >> (5 - i)) & 1);
1313 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1314 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1316 /* check if prediction blocks A and C are available */
1317 v->a_avail = v->c_avail = 0;
1318 if (i == 2 || i == 3 || !s->first_slice_line)
1319 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1320 if (i == 1 || i == 3 || s->mb_x)
1321 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1323 vc1_decode_intra_block(v, v->block[v->cur_blk_idx][i], i, val, mquant,
1324 (i & 4) ? v->codingset2 : v->codingset);
1325 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1327 v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][i]);
1329 for (j = 0; j < 64; j++)
1330 v->block[v->cur_blk_idx][i][j] <<= 1;
1331 block_cbp |= 0xF << (i << 2);
1332 block_intra |= 1 << i;
1334 pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][i], i, mquant, ttmb, first_block,
1335 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1336 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1337 block_cbp |= pat << (i << 2);
1338 if (!v->ttmbf && ttmb < 8)
1345 for (i = 0; i < 6; i++) {
1346 v->mb_type[0][s->block_index[i]] = 0;
1347 s->dc_val[0][s->block_index[i]] = 0;
1349 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1350 s->current_picture.qscale_table[mb_pos] = 0;
1351 ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1352 ff_vc1_mc_1mv(v, 0);
1354 } else { // 4MV mode
1355 if (!skipped /* unskipped MB */) {
1356 int intra_count = 0, coded_inter = 0;
1357 int is_intra[6], is_coded[6];
1359 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1360 for (i = 0; i < 6; i++) {
1361 val = ((cbp >> (5 - i)) & 1);
1362 s->dc_val[0][s->block_index[i]] = 0;
1369 GET_MVDATA(dmv_x, dmv_y);
1371 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1373 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1374 intra_count += s->mb_intra;
1375 is_intra[i] = s->mb_intra;
1376 is_coded[i] = mb_has_coeffs;
1379 is_intra[i] = (intra_count >= 3);
1383 ff_vc1_mc_4mv_chroma(v, 0);
1384 v->mb_type[0][s->block_index[i]] = is_intra[i];
1386 coded_inter = !is_intra[i] & is_coded[i];
1388 // if there are no coded blocks then don't do anything more
1390 if (!intra_count && !coded_inter)
1393 s->current_picture.qscale_table[mb_pos] = mquant;
1394 /* test if block is intra and has pred */
1397 for (i = 0; i < 6; i++)
1399 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1400 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1406 s->ac_pred = get_bits1(gb);
1410 if (!v->ttmbf && coded_inter)
1411 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1412 for (i = 0; i < 6; i++) {
1414 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1415 s->mb_intra = is_intra[i];
1417 /* check if prediction blocks A and C are available */
1418 v->a_avail = v->c_avail = 0;
1419 if (i == 2 || i == 3 || !s->first_slice_line)
1420 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1421 if (i == 1 || i == 3 || s->mb_x)
1422 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1424 vc1_decode_intra_block(v, v->block[v->cur_blk_idx][i], i, is_coded[i], mquant,
1425 (i & 4) ? v->codingset2 : v->codingset);
1426 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1428 v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][i]);
1430 for (j = 0; j < 64; j++)
1431 v->block[v->cur_blk_idx][i][j] <<= 1;
1432 block_cbp |= 0xF << (i << 2);
1433 block_intra |= 1 << i;
1434 } else if (is_coded[i]) {
1435 pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][i], i, mquant, ttmb,
1436 first_block, s->dest[dst_idx] + off,
1437 (i & 4) ? s->uvlinesize : s->linesize,
1438 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1440 block_cbp |= pat << (i << 2);
1441 if (!v->ttmbf && ttmb < 8)
1446 } else { // skipped MB
1448 s->current_picture.qscale_table[mb_pos] = 0;
1449 for (i = 0; i < 6; i++) {
1450 v->mb_type[0][s->block_index[i]] = 0;
1451 s->dc_val[0][s->block_index[i]] = 0;
1453 for (i = 0; i < 4; i++) {
1454 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1455 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1457 ff_vc1_mc_4mv_chroma(v, 0);
1458 s->current_picture.qscale_table[mb_pos] = 0;
1462 if (v->overlap && v->pq >= 9)
1463 ff_vc1_p_overlap_filter(v);
1464 vc1_put_signed_blocks_clamped(v);
1466 v->cbp[s->mb_x] = block_cbp;
1467 v->ttblk[s->mb_x] = block_tt;
1468 v->is_intra[s->mb_x] = block_intra;
1473 /* Decode one macroblock in an interlaced frame p picture */
1475 static int vc1_decode_p_mb_intfr(VC1Context *v)
1477 MpegEncContext *s = &v->s;
1478 GetBitContext *gb = &s->gb;
1480 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1481 int cbp = 0; /* cbp decoding stuff */
1482 int mqdiff, mquant; /* MB quantization */
1483 int ttmb = v->ttfrm; /* MB Transform type */
1485 int mb_has_coeffs = 1; /* last_flag */
1486 int dmv_x, dmv_y; /* Differential MV components */
1487 int val; /* temp value */
1488 int first_block = 1;
1490 int skipped, fourmv = 0, twomv = 0;
1491 int block_cbp = 0, pat, block_tt = 0;
1492 int idx_mbmode = 0, mvbp;
1495 mquant = v->pq; /* Lossy initialization */
1498 skipped = get_bits1(gb);
1500 skipped = v->s.mbskip_table[mb_pos];
1502 if (v->fourmvswitch)
1503 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1505 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1506 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1507 /* store the motion vector type in a flag (useful later) */
1508 case MV_PMODE_INTFR_4MV:
1510 v->blk_mv_type[s->block_index[0]] = 0;
1511 v->blk_mv_type[s->block_index[1]] = 0;
1512 v->blk_mv_type[s->block_index[2]] = 0;
1513 v->blk_mv_type[s->block_index[3]] = 0;
1515 case MV_PMODE_INTFR_4MV_FIELD:
1517 v->blk_mv_type[s->block_index[0]] = 1;
1518 v->blk_mv_type[s->block_index[1]] = 1;
1519 v->blk_mv_type[s->block_index[2]] = 1;
1520 v->blk_mv_type[s->block_index[3]] = 1;
1522 case MV_PMODE_INTFR_2MV_FIELD:
1524 v->blk_mv_type[s->block_index[0]] = 1;
1525 v->blk_mv_type[s->block_index[1]] = 1;
1526 v->blk_mv_type[s->block_index[2]] = 1;
1527 v->blk_mv_type[s->block_index[3]] = 1;
1529 case MV_PMODE_INTFR_1MV:
1530 v->blk_mv_type[s->block_index[0]] = 0;
1531 v->blk_mv_type[s->block_index[1]] = 0;
1532 v->blk_mv_type[s->block_index[2]] = 0;
1533 v->blk_mv_type[s->block_index[3]] = 0;
1536 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1537 for (i = 0; i < 4; i++) {
1538 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1539 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1541 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1543 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1544 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1545 mb_has_coeffs = get_bits1(gb);
1547 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1548 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1550 s->current_picture.qscale_table[mb_pos] = mquant;
1551 /* Set DC scale - y and c use the same (not sure if necessary here) */
1552 s->y_dc_scale = s->y_dc_scale_table[mquant];
1553 s->c_dc_scale = s->c_dc_scale_table[mquant];
1555 for (i = 0; i < 6; i++) {
1556 v->a_avail = v->c_avail = 0;
1557 v->mb_type[0][s->block_index[i]] = 1;
1558 s->dc_val[0][s->block_index[i]] = 0;
1560 val = ((cbp >> (5 - i)) & 1);
1561 if (i == 2 || i == 3 || !s->first_slice_line)
1562 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1563 if (i == 1 || i == 3 || s->mb_x)
1564 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1566 vc1_decode_intra_block(v, v->block[v->cur_blk_idx][i], i, val, mquant,
1567 (i & 4) ? v->codingset2 : v->codingset);
1568 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1570 v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][i]);
1572 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1575 block_cbp |= 0xf << (i << 2);
1578 } else { // inter MB
1579 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1581 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1582 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1583 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1585 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1586 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1587 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1590 s->mb_intra = v->is_intra[s->mb_x] = 0;
1591 for (i = 0; i < 6; i++)
1592 v->mb_type[0][s->block_index[i]] = 0;
1593 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1594 /* for all motion vector read MVDATA and motion compensate each block */
1598 for (i = 0; i < 4; i++) {
1600 if (mvbp & (8 >> i))
1601 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1602 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1603 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1605 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1610 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1612 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1613 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1614 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1617 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1619 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1620 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1621 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1622 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1624 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1627 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1629 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1630 ff_vc1_mc_1mv(v, 0);
1633 GET_MQUANT(); // p. 227
1634 s->current_picture.qscale_table[mb_pos] = mquant;
1635 if (!v->ttmbf && cbp)
1636 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1637 for (i = 0; i < 6; i++) {
1638 s->dc_val[0][s->block_index[i]] = 0;
1640 val = ((cbp >> (5 - i)) & 1);
1642 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1644 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1646 pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][i], i, mquant, ttmb,
1647 first_block, s->dest[dst_idx] + off,
1648 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1649 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1650 block_cbp |= pat << (i << 2);
1651 if (!v->ttmbf && ttmb < 8)
1658 s->mb_intra = v->is_intra[s->mb_x] = 0;
1659 for (i = 0; i < 6; i++) {
1660 v->mb_type[0][s->block_index[i]] = 0;
1661 s->dc_val[0][s->block_index[i]] = 0;
1663 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1664 s->current_picture.qscale_table[mb_pos] = 0;
1665 v->blk_mv_type[s->block_index[0]] = 0;
1666 v->blk_mv_type[s->block_index[1]] = 0;
1667 v->blk_mv_type[s->block_index[2]] = 0;
1668 v->blk_mv_type[s->block_index[3]] = 0;
1669 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1670 ff_vc1_mc_1mv(v, 0);
1671 v->fieldtx_plane[mb_pos] = 0;
1673 if (v->overlap && v->pq >= 9)
1674 ff_vc1_p_overlap_filter(v);
1675 vc1_put_signed_blocks_clamped(v);
1677 v->cbp[s->mb_x] = block_cbp;
1678 v->ttblk[s->mb_x] = block_tt;
1683 static int vc1_decode_p_mb_intfi(VC1Context *v)
1685 MpegEncContext *s = &v->s;
1686 GetBitContext *gb = &s->gb;
1688 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1689 int cbp = 0; /* cbp decoding stuff */
1690 int mqdiff, mquant; /* MB quantization */
1691 int ttmb = v->ttfrm; /* MB Transform type */
1693 int mb_has_coeffs = 1; /* last_flag */
1694 int dmv_x, dmv_y; /* Differential MV components */
1695 int val; /* temp values */
1696 int first_block = 1;
1699 int block_cbp = 0, pat, block_tt = 0;
1702 mquant = v->pq; /* Lossy initialization */
1704 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1705 if (idx_mbmode <= 1) { // intra MB
1706 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1708 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1709 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1710 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1712 s->current_picture.qscale_table[mb_pos] = mquant;
1713 /* Set DC scale - y and c use the same (not sure if necessary here) */
1714 s->y_dc_scale = s->y_dc_scale_table[mquant];
1715 s->c_dc_scale = s->c_dc_scale_table[mquant];
1716 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1717 mb_has_coeffs = idx_mbmode & 1;
1719 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1721 for (i = 0; i < 6; i++) {
1722 v->a_avail = v->c_avail = 0;
1723 v->mb_type[0][s->block_index[i]] = 1;
1724 s->dc_val[0][s->block_index[i]] = 0;
1726 val = ((cbp >> (5 - i)) & 1);
1727 if (i == 2 || i == 3 || !s->first_slice_line)
1728 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1729 if (i == 1 || i == 3 || s->mb_x)
1730 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1732 vc1_decode_intra_block(v, v->block[v->cur_blk_idx][i], i, val, mquant,
1733 (i & 4) ? v->codingset2 : v->codingset);
1734 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1736 v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][i]);
1737 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1738 block_cbp |= 0xf << (i << 2);
1741 s->mb_intra = v->is_intra[s->mb_x] = 0;
1742 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1743 for (i = 0; i < 6; i++)
1744 v->mb_type[0][s->block_index[i]] = 0;
1745 if (idx_mbmode <= 5) { // 1-MV
1746 dmv_x = dmv_y = pred_flag = 0;
1747 if (idx_mbmode & 1) {
1748 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1750 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1751 ff_vc1_mc_1mv(v, 0);
1752 mb_has_coeffs = !(idx_mbmode & 2);
1754 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1755 for (i = 0; i < 4; i++) {
1756 dmv_x = dmv_y = pred_flag = 0;
1757 if (v->fourmvbp & (8 >> i))
1758 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1759 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1760 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1762 ff_vc1_mc_4mv_chroma(v, 0);
1763 mb_has_coeffs = idx_mbmode & 1;
1766 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1770 s->current_picture.qscale_table[mb_pos] = mquant;
1771 if (!v->ttmbf && cbp) {
1772 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1775 for (i = 0; i < 6; i++) {
1776 s->dc_val[0][s->block_index[i]] = 0;
1778 val = ((cbp >> (5 - i)) & 1);
1779 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1781 pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][i], i, mquant, ttmb,
1782 first_block, s->dest[dst_idx] + off,
1783 (i & 4) ? s->uvlinesize : s->linesize,
1784 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1786 block_cbp |= pat << (i << 2);
1787 if (!v->ttmbf && ttmb < 8)
1793 if (v->overlap && v->pq >= 9)
1794 ff_vc1_p_overlap_filter(v);
1795 vc1_put_signed_blocks_clamped(v);
1797 v->cbp[s->mb_x] = block_cbp;
1798 v->ttblk[s->mb_x] = block_tt;
1803 /** Decode one B-frame MB (in Main profile)
1805 static void vc1_decode_b_mb(VC1Context *v)
1807 MpegEncContext *s = &v->s;
1808 GetBitContext *gb = &s->gb;
1810 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1811 int cbp = 0; /* cbp decoding stuff */
1812 int mqdiff, mquant; /* MB quantization */
1813 int ttmb = v->ttfrm; /* MB Transform type */
1814 int mb_has_coeffs = 0; /* last_flag */
1815 int index, index1; /* LUT indexes */
1816 int val, sign; /* temp values */
1817 int first_block = 1;
1819 int skipped, direct;
1820 int dmv_x[2], dmv_y[2];
1821 int bmvtype = BMV_TYPE_BACKWARD;
1823 mquant = v->pq; /* lossy initialization */
1827 direct = get_bits1(gb);
1829 direct = v->direct_mb_plane[mb_pos];
1831 skipped = get_bits1(gb);
1833 skipped = v->s.mbskip_table[mb_pos];
1835 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1836 for (i = 0; i < 6; i++) {
1837 v->mb_type[0][s->block_index[i]] = 0;
1838 s->dc_val[0][s->block_index[i]] = 0;
1840 s->current_picture.qscale_table[mb_pos] = 0;
1844 GET_MVDATA(dmv_x[0], dmv_y[0]);
1845 dmv_x[1] = dmv_x[0];
1846 dmv_y[1] = dmv_y[0];
1848 if (skipped || !s->mb_intra) {
1849 bmvtype = decode012(gb);
1852 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1855 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1858 bmvtype = BMV_TYPE_INTERPOLATED;
1859 dmv_x[0] = dmv_y[0] = 0;
1863 for (i = 0; i < 6; i++)
1864 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1868 bmvtype = BMV_TYPE_INTERPOLATED;
1869 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1870 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1874 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1877 s->current_picture.qscale_table[mb_pos] = mquant;
1879 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1880 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1881 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1882 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1884 if (!mb_has_coeffs && !s->mb_intra) {
1885 /* no coded blocks - effectively skipped */
1886 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1887 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1890 if (s->mb_intra && !mb_has_coeffs) {
1892 s->current_picture.qscale_table[mb_pos] = mquant;
1893 s->ac_pred = get_bits1(gb);
1895 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1897 if (bmvtype == BMV_TYPE_INTERPOLATED) {
1898 GET_MVDATA(dmv_x[0], dmv_y[0]);
1899 if (!mb_has_coeffs) {
1900 /* interpolated skipped block */
1901 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1902 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1906 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1908 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1911 s->ac_pred = get_bits1(gb);
1912 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1914 s->current_picture.qscale_table[mb_pos] = mquant;
1915 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1916 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1920 for (i = 0; i < 6; i++) {
1921 s->dc_val[0][s->block_index[i]] = 0;
1923 val = ((cbp >> (5 - i)) & 1);
1924 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1925 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1927 /* check if prediction blocks A and C are available */
1928 v->a_avail = v->c_avail = 0;
1929 if (i == 2 || i == 3 || !s->first_slice_line)
1930 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1931 if (i == 1 || i == 3 || s->mb_x)
1932 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1934 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1935 (i & 4) ? v->codingset2 : v->codingset);
1936 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1938 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1940 for (j = 0; j < 64; j++)
1941 s->block[i][j] <<= 1;
1942 s->idsp.put_signed_pixels_clamped(s->block[i],
1943 s->dest[dst_idx] + off,
1944 i & 4 ? s->uvlinesize
1947 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1948 first_block, s->dest[dst_idx] + off,
1949 (i & 4) ? s->uvlinesize : s->linesize,
1950 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
1951 if (!v->ttmbf && ttmb < 8)
1958 /** Decode one B-frame MB (in interlaced field B picture)
1960 static void vc1_decode_b_mb_intfi(VC1Context *v)
1962 MpegEncContext *s = &v->s;
1963 GetBitContext *gb = &s->gb;
1965 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1966 int cbp = 0; /* cbp decoding stuff */
1967 int mqdiff, mquant; /* MB quantization */
1968 int ttmb = v->ttfrm; /* MB Transform type */
1969 int mb_has_coeffs = 0; /* last_flag */
1970 int val; /* temp value */
1971 int first_block = 1;
1974 int dmv_x[2], dmv_y[2], pred_flag[2];
1975 int bmvtype = BMV_TYPE_BACKWARD;
1976 int block_cbp = 0, pat, block_tt = 0;
1979 mquant = v->pq; /* Lossy initialization */
1982 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1983 if (idx_mbmode <= 1) { // intra MB
1984 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1986 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1987 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1988 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1990 s->current_picture.qscale_table[mb_pos] = mquant;
1991 /* Set DC scale - y and c use the same (not sure if necessary here) */
1992 s->y_dc_scale = s->y_dc_scale_table[mquant];
1993 s->c_dc_scale = s->c_dc_scale_table[mquant];
1994 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1995 mb_has_coeffs = idx_mbmode & 1;
1997 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1999 for (i = 0; i < 6; i++) {
2000 v->a_avail = v->c_avail = 0;
2001 v->mb_type[0][s->block_index[i]] = 1;
2002 s->dc_val[0][s->block_index[i]] = 0;
2004 val = ((cbp >> (5 - i)) & 1);
2005 if (i == 2 || i == 3 || !s->first_slice_line)
2006 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2007 if (i == 1 || i == 3 || s->mb_x)
2008 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2010 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2011 (i & 4) ? v->codingset2 : v->codingset);
2012 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2014 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2016 for (j = 0; j < 64; j++)
2017 s->block[i][j] <<= 1;
2018 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2019 s->idsp.put_signed_pixels_clamped(s->block[i],
2020 s->dest[dst_idx] + off,
2021 (i & 4) ? s->uvlinesize
2025 s->mb_intra = v->is_intra[s->mb_x] = 0;
2026 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2027 for (i = 0; i < 6; i++)
2028 v->mb_type[0][s->block_index[i]] = 0;
2030 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2032 fwd = v->forward_mb_plane[mb_pos];
2033 if (idx_mbmode <= 5) { // 1-MV
2035 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2036 pred_flag[0] = pred_flag[1] = 0;
2038 bmvtype = BMV_TYPE_FORWARD;
2040 bmvtype = decode012(gb);
2043 bmvtype = BMV_TYPE_BACKWARD;
2046 bmvtype = BMV_TYPE_DIRECT;
2049 bmvtype = BMV_TYPE_INTERPOLATED;
2050 interpmvp = get_bits1(gb);
2053 v->bmvtype = bmvtype;
2054 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2055 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2058 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2060 if (bmvtype == BMV_TYPE_DIRECT) {
2061 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2062 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2063 if (!s->next_picture_ptr->field_picture) {
2064 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2068 ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2069 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2070 mb_has_coeffs = !(idx_mbmode & 2);
2073 bmvtype = BMV_TYPE_FORWARD;
2074 v->bmvtype = bmvtype;
2075 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2076 for (i = 0; i < 4; i++) {
2077 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2078 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2079 if (v->fourmvbp & (8 >> i)) {
2080 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2081 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2082 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2084 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2085 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2087 ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2088 mb_has_coeffs = idx_mbmode & 1;
2091 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2095 s->current_picture.qscale_table[mb_pos] = mquant;
2096 if (!v->ttmbf && cbp) {
2097 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2100 for (i = 0; i < 6; i++) {
2101 s->dc_val[0][s->block_index[i]] = 0;
2103 val = ((cbp >> (5 - i)) & 1);
2104 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2106 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2107 first_block, s->dest[dst_idx] + off,
2108 (i & 4) ? s->uvlinesize : s->linesize,
2109 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2110 block_cbp |= pat << (i << 2);
2111 if (!v->ttmbf && ttmb < 8)
2117 v->cbp[s->mb_x] = block_cbp;
2118 v->ttblk[s->mb_x] = block_tt;
2121 /** Decode one B-frame MB (in interlaced frame B picture)
2123 static int vc1_decode_b_mb_intfr(VC1Context *v)
2125 MpegEncContext *s = &v->s;
2126 GetBitContext *gb = &s->gb;
2128 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2129 int cbp = 0; /* cbp decoding stuff */
2130 int mqdiff, mquant; /* MB quantization */
2131 int ttmb = v->ttfrm; /* MB Transform type */
2132 int mvsw = 0; /* motion vector switch */
2133 int mb_has_coeffs = 1; /* last_flag */
2134 int dmv_x, dmv_y; /* Differential MV components */
2135 int val; /* temp value */
2136 int first_block = 1;
2138 int skipped, direct, twomv = 0;
2139 int block_cbp = 0, pat, block_tt = 0;
2140 int idx_mbmode = 0, mvbp;
2141 int stride_y, fieldtx;
2142 int bmvtype = BMV_TYPE_BACKWARD;
2145 mquant = v->pq; /* Lossy initialization */
2148 skipped = get_bits1(gb);
2150 skipped = v->s.mbskip_table[mb_pos];
2153 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2154 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2156 v->blk_mv_type[s->block_index[0]] = 1;
2157 v->blk_mv_type[s->block_index[1]] = 1;
2158 v->blk_mv_type[s->block_index[2]] = 1;
2159 v->blk_mv_type[s->block_index[3]] = 1;
2161 v->blk_mv_type[s->block_index[0]] = 0;
2162 v->blk_mv_type[s->block_index[1]] = 0;
2163 v->blk_mv_type[s->block_index[2]] = 0;
2164 v->blk_mv_type[s->block_index[3]] = 0;
2169 direct = get_bits1(gb);
2171 direct = v->direct_mb_plane[mb_pos];
2174 if (s->next_picture_ptr->field_picture)
2175 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2176 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);
2177 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);
2178 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);
2179 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);
2182 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);
2183 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);
2184 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);
2185 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);
2187 for (i = 1; i < 4; i += 2) {
2188 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2189 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2190 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2191 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2194 for (i = 1; i < 4; i++) {
2195 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2196 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2197 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2198 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2203 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2204 for (i = 0; i < 4; i++) {
2205 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2206 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2207 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2208 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2210 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2212 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2213 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2214 mb_has_coeffs = get_bits1(gb);
2216 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2217 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2219 s->current_picture.qscale_table[mb_pos] = mquant;
2220 /* Set DC scale - y and c use the same (not sure if necessary here) */
2221 s->y_dc_scale = s->y_dc_scale_table[mquant];
2222 s->c_dc_scale = s->c_dc_scale_table[mquant];
2224 for (i = 0; i < 6; i++) {
2225 v->a_avail = v->c_avail = 0;
2226 v->mb_type[0][s->block_index[i]] = 1;
2227 s->dc_val[0][s->block_index[i]] = 0;
2229 val = ((cbp >> (5 - i)) & 1);
2230 if (i == 2 || i == 3 || !s->first_slice_line)
2231 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2232 if (i == 1 || i == 3 || s->mb_x)
2233 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2235 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2236 (i & 4) ? v->codingset2 : v->codingset);
2237 if (CONFIG_GRAY && i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2239 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2241 stride_y = s->linesize << fieldtx;
2242 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2244 stride_y = s->uvlinesize;
2247 s->idsp.put_signed_pixels_clamped(s->block[i],
2248 s->dest[dst_idx] + off,
2252 s->mb_intra = v->is_intra[s->mb_x] = 0;
2254 if (skipped || !s->mb_intra) {
2255 bmvtype = decode012(gb);
2258 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2261 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2264 bmvtype = BMV_TYPE_INTERPOLATED;
2268 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2269 mvsw = get_bits1(gb);
2272 if (!skipped) { // inter MB
2273 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2275 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2277 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2278 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2279 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2280 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2284 for (i = 0; i < 6; i++)
2285 v->mb_type[0][s->block_index[i]] = 0;
2286 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2287 /* for all motion vector read MVDATA and motion compensate each block */
2291 for (i = 0; i < 4; i++) {
2292 ff_vc1_mc_4mv_luma(v, i, 0, 0);
2293 ff_vc1_mc_4mv_luma(v, i, 1, 1);
2295 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2296 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2298 ff_vc1_mc_1mv(v, 0);
2299 ff_vc1_interp_mc(v);
2301 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2303 for (i = 0; i < 4; i++) {
2306 val = ((mvbp >> (3 - i)) & 1);
2308 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2310 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2311 ff_vc1_mc_4mv_luma(v, j, dir, dir);
2312 ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2315 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2316 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2317 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2321 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2323 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2324 ff_vc1_mc_1mv(v, 0);
2328 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2330 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2331 ff_vc1_interp_mc(v);
2333 dir = bmvtype == BMV_TYPE_BACKWARD;
2340 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2341 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2345 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2346 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2349 for (i = 0; i < 2; i++) {
2350 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];
2351 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];
2352 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];
2353 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];
2356 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2357 ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2360 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2361 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2362 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2363 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2364 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2366 dir = bmvtype == BMV_TYPE_BACKWARD;
2368 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2371 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2373 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2374 v->blk_mv_type[s->block_index[0]] = 1;
2375 v->blk_mv_type[s->block_index[1]] = 1;
2376 v->blk_mv_type[s->block_index[2]] = 1;
2377 v->blk_mv_type[s->block_index[3]] = 1;
2378 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2379 for (i = 0; i < 2; i++) {
2380 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];
2381 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];
2383 ff_vc1_mc_1mv(v, dir);
2387 GET_MQUANT(); // p. 227
2388 s->current_picture.qscale_table[mb_pos] = mquant;
2389 if (!v->ttmbf && cbp)
2390 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2391 for (i = 0; i < 6; i++) {
2392 s->dc_val[0][s->block_index[i]] = 0;
2394 val = ((cbp >> (5 - i)) & 1);
2396 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2398 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2400 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2401 first_block, s->dest[dst_idx] + off,
2402 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2403 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2404 block_cbp |= pat << (i << 2);
2405 if (!v->ttmbf && ttmb < 8)
2413 for (i = 0; i < 6; i++) {
2414 v->mb_type[0][s->block_index[i]] = 0;
2415 s->dc_val[0][s->block_index[i]] = 0;
2417 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2418 s->current_picture.qscale_table[mb_pos] = 0;
2419 v->blk_mv_type[s->block_index[0]] = 0;
2420 v->blk_mv_type[s->block_index[1]] = 0;
2421 v->blk_mv_type[s->block_index[2]] = 0;
2422 v->blk_mv_type[s->block_index[3]] = 0;
2425 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2426 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2427 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2429 dir = bmvtype == BMV_TYPE_BACKWARD;
2430 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 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 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];
2439 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];
2442 v->blk_mv_type[s->block_index[0]] = 1;
2443 v->blk_mv_type[s->block_index[1]] = 1;
2444 v->blk_mv_type[s->block_index[2]] = 1;
2445 v->blk_mv_type[s->block_index[3]] = 1;
2446 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2447 for (i = 0; i < 2; i++) {
2448 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];
2449 s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
2455 ff_vc1_mc_1mv(v, dir);
2456 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2457 ff_vc1_interp_mc(v);
2459 v->fieldtx_plane[mb_pos] = 0;
2462 v->cbp[s->mb_x] = block_cbp;
2463 v->ttblk[s->mb_x] = block_tt;
2468 /** Decode blocks of I-frame
2470 static void vc1_decode_i_blocks(VC1Context *v)
2473 MpegEncContext *s = &v->s;
2478 /* select coding mode used for VLC tables selection */
2479 switch (v->y_ac_table_index) {
2481 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2484 v->codingset = CS_HIGH_MOT_INTRA;
2487 v->codingset = CS_MID_RATE_INTRA;
2491 switch (v->c_ac_table_index) {
2493 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2496 v->codingset2 = CS_HIGH_MOT_INTER;
2499 v->codingset2 = CS_MID_RATE_INTER;
2503 /* Set DC scale - y and c use the same */
2504 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2505 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2508 s->mb_x = s->mb_y = 0;
2510 s->first_slice_line = 1;
2511 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2513 init_block_index(v);
2514 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2516 ff_update_block_index(s);
2517 dst[0] = s->dest[0];
2518 dst[1] = dst[0] + 8;
2519 dst[2] = s->dest[0] + s->linesize * 8;
2520 dst[3] = dst[2] + 8;
2521 dst[4] = s->dest[1];
2522 dst[5] = s->dest[2];
2523 s->bdsp.clear_blocks(s->block[0]);
2524 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2525 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2526 s->current_picture.qscale_table[mb_pos] = v->pq;
2527 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2528 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2530 // do actual MB decoding and displaying
2531 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2532 v->s.ac_pred = get_bits1(&v->s.gb);
2534 for (k = 0; k < 6; k++) {
2535 val = ((cbp >> (5 - k)) & 1);
2538 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2542 cbp |= val << (5 - k);
2544 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2546 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2548 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2549 if (v->pq >= 9 && v->overlap) {
2551 for (j = 0; j < 64; j++)
2552 s->block[k][j] <<= 1;
2553 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2554 k & 4 ? s->uvlinesize
2558 for (j = 0; j < 64; j++)
2559 s->block[k][j] = (s->block[k][j] - 64) << 1;
2560 s->idsp.put_pixels_clamped(s->block[k], dst[k],
2561 k & 4 ? s->uvlinesize
2566 if (v->pq >= 9 && v->overlap) {
2568 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2569 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2570 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2571 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2572 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2575 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2576 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2577 if (!s->first_slice_line) {
2578 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2579 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2580 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2581 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2582 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2585 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2586 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2588 if (v->s.loop_filter)
2589 ff_vc1_loop_filter_iblk(v, v->pq);
2591 if (get_bits_count(&s->gb) > v->bits) {
2592 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2593 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2594 get_bits_count(&s->gb), v->bits);
2598 if (!v->s.loop_filter)
2599 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2601 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2603 s->first_slice_line = 0;
2605 if (v->s.loop_filter)
2606 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2608 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2609 * profile, these only differ are when decoding MSS2 rectangles. */
2610 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2613 /** Decode blocks of I-frame for advanced profile
2615 static void vc1_decode_i_blocks_adv(VC1Context *v)
2618 MpegEncContext *s = &v->s;
2624 GetBitContext *gb = &s->gb;
2626 /* select coding mode used for VLC tables selection */
2627 switch (v->y_ac_table_index) {
2629 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2632 v->codingset = CS_HIGH_MOT_INTRA;
2635 v->codingset = CS_MID_RATE_INTRA;
2639 switch (v->c_ac_table_index) {
2641 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2644 v->codingset2 = CS_HIGH_MOT_INTER;
2647 v->codingset2 = CS_MID_RATE_INTER;
2652 s->mb_x = s->mb_y = 0;
2654 s->first_slice_line = 1;
2655 s->mb_y = s->start_mb_y;
2656 if (s->start_mb_y) {
2658 init_block_index(v);
2659 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2660 (1 + s->b8_stride) * sizeof(*s->coded_block));
2662 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2664 init_block_index(v);
2665 for (;s->mb_x < s->mb_width; s->mb_x++) {
2666 int16_t (*block)[64] = v->block[v->cur_blk_idx];
2667 ff_update_block_index(s);
2668 s->bdsp.clear_blocks(block[0]);
2669 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2670 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2671 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2672 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2674 // do actual MB decoding and displaying
2675 if (v->fieldtx_is_raw)
2676 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2677 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2678 if (v->acpred_is_raw)
2679 v->s.ac_pred = get_bits1(&v->s.gb);
2681 v->s.ac_pred = v->acpred_plane[mb_pos];
2683 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2684 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2688 s->current_picture.qscale_table[mb_pos] = mquant;
2689 /* Set DC scale - y and c use the same */
2690 s->y_dc_scale = s->y_dc_scale_table[mquant];
2691 s->c_dc_scale = s->c_dc_scale_table[mquant];
2693 for (k = 0; k < 6; k++) {
2694 v->mb_type[0][s->block_index[k]] = 1;
2696 val = ((cbp >> (5 - k)) & 1);
2699 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2703 cbp |= val << (5 - k);
2705 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2706 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2708 vc1_decode_i_block_adv(v, block[k], k, val,
2709 (k < 4) ? v->codingset : v->codingset2, mquant);
2711 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2713 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2716 if (v->overlap && v->condover != CONDOVER_NONE)
2717 ff_vc1_i_overlap_filter(v);
2718 vc1_put_signed_blocks_clamped(v);
2719 if (v->s.loop_filter)
2720 ff_vc1_i_loop_filter(v);
2722 if (get_bits_count(&s->gb) > v->bits) {
2723 // TODO: may need modification to handle slice coding
2724 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2725 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2726 get_bits_count(&s->gb), v->bits);
2729 inc_blk_idx(v->topleft_blk_idx);
2730 inc_blk_idx(v->top_blk_idx);
2731 inc_blk_idx(v->left_blk_idx);
2732 inc_blk_idx(v->cur_blk_idx);
2734 if (!v->s.loop_filter)
2735 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2737 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2738 s->first_slice_line = 0;
2741 if (v->s.loop_filter)
2742 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2743 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2744 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2747 static void vc1_decode_p_blocks(VC1Context *v)
2749 MpegEncContext *s = &v->s;
2750 int apply_loop_filter;
2752 /* select coding mode used for VLC tables selection */
2753 switch (v->c_ac_table_index) {
2755 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2758 v->codingset = CS_HIGH_MOT_INTRA;
2761 v->codingset = CS_MID_RATE_INTRA;
2765 switch (v->c_ac_table_index) {
2767 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2770 v->codingset2 = CS_HIGH_MOT_INTER;
2773 v->codingset2 = CS_MID_RATE_INTER;
2777 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2778 s->first_slice_line = 1;
2779 memset(v->cbp_base, 0, sizeof(v->cbp_base[0]) * 3 * s->mb_stride);
2780 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2782 init_block_index(v);
2783 for (; s->mb_x < s->mb_width; s->mb_x++) {
2784 ff_update_block_index(s);
2786 if (v->fcm == ILACE_FIELD) {
2787 vc1_decode_p_mb_intfi(v);
2788 if (apply_loop_filter)
2789 ff_vc1_p_loop_filter(v);
2790 } else if (v->fcm == ILACE_FRAME) {
2791 vc1_decode_p_mb_intfr(v);
2792 if (apply_loop_filter)
2793 ff_vc1_p_intfr_loop_filter(v);
2796 if (apply_loop_filter)
2797 ff_vc1_p_loop_filter(v);
2799 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2800 // TODO: may need modification to handle slice coding
2801 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2802 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2803 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2806 inc_blk_idx(v->topleft_blk_idx);
2807 inc_blk_idx(v->top_blk_idx);
2808 inc_blk_idx(v->left_blk_idx);
2809 inc_blk_idx(v->cur_blk_idx);
2811 memmove(v->cbp_base,
2812 v->cbp - s->mb_stride,
2813 sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
2814 memmove(v->ttblk_base,
2815 v->ttblk - s->mb_stride,
2816 sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
2817 memmove(v->is_intra_base,
2818 v->is_intra - s->mb_stride,
2819 sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
2820 memmove(v->luma_mv_base,
2821 v->luma_mv - s->mb_stride,
2822 sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
2823 if (s->mb_y != s->start_mb_y)
2824 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2825 s->first_slice_line = 0;
2827 if (s->end_mb_y >= s->start_mb_y)
2828 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2829 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2830 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2833 static void vc1_decode_b_blocks(VC1Context *v)
2835 MpegEncContext *s = &v->s;
2837 /* select coding mode used for VLC tables selection */
2838 switch (v->c_ac_table_index) {
2840 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2843 v->codingset = CS_HIGH_MOT_INTRA;
2846 v->codingset = CS_MID_RATE_INTRA;
2850 switch (v->c_ac_table_index) {
2852 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2855 v->codingset2 = CS_HIGH_MOT_INTER;
2858 v->codingset2 = CS_MID_RATE_INTER;
2862 s->first_slice_line = 1;
2863 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2865 init_block_index(v);
2866 for (; s->mb_x < s->mb_width; s->mb_x++) {
2867 ff_update_block_index(s);
2869 if (v->fcm == ILACE_FIELD) {
2870 vc1_decode_b_mb_intfi(v);
2871 if (v->s.loop_filter)
2872 ff_vc1_b_intfi_loop_filter(v);
2873 } else if (v->fcm == ILACE_FRAME) {
2874 vc1_decode_b_mb_intfr(v);
2875 if (v->s.loop_filter)
2876 ff_vc1_p_intfr_loop_filter(v);
2879 if (v->s.loop_filter)
2880 ff_vc1_i_loop_filter(v);
2882 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2883 // TODO: may need modification to handle slice coding
2884 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2885 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2886 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2890 memmove(v->cbp_base,
2891 v->cbp - s->mb_stride,
2892 sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
2893 memmove(v->ttblk_base,
2894 v->ttblk - s->mb_stride,
2895 sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
2896 memmove(v->is_intra_base,
2897 v->is_intra - s->mb_stride,
2898 sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
2899 if (!v->s.loop_filter)
2900 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2902 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2903 s->first_slice_line = 0;
2905 if (v->s.loop_filter)
2906 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2907 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2908 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2911 static void vc1_decode_skip_blocks(VC1Context *v)
2913 MpegEncContext *s = &v->s;
2915 if (!v->s.last_picture.f->data[0])
2918 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2919 s->first_slice_line = 1;
2920 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2922 init_block_index(v);
2923 ff_update_block_index(s);
2924 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2925 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2926 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2927 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2928 s->first_slice_line = 0;
2930 s->pict_type = AV_PICTURE_TYPE_P;
2933 void ff_vc1_decode_blocks(VC1Context *v)
2936 v->s.esc3_level_length = 0;
2938 ff_intrax8_decode_picture(&v->x8, &v->s.current_picture,
2939 &v->s.gb, &v->s.mb_x, &v->s.mb_y,
2940 2 * v->pq + v->halfpq, v->pq * !v->pquantizer,
2941 v->s.loop_filter, v->s.low_delay);
2943 ff_er_add_slice(&v->s.er, 0, 0,
2944 (v->s.mb_x >> 1) - 1, (v->s.mb_y >> 1) - 1,
2948 v->left_blk_idx = -1;
2949 v->topleft_blk_idx = 1;
2951 switch (v->s.pict_type) {
2952 case AV_PICTURE_TYPE_I:
2953 if (v->profile == PROFILE_ADVANCED)
2954 vc1_decode_i_blocks_adv(v);
2956 vc1_decode_i_blocks(v);
2958 case AV_PICTURE_TYPE_P:
2959 if (v->p_frame_skipped)
2960 vc1_decode_skip_blocks(v);
2962 vc1_decode_p_blocks(v);
2964 case AV_PICTURE_TYPE_B:
2966 if (v->profile == PROFILE_ADVANCED)
2967 vc1_decode_i_blocks_adv(v);
2969 vc1_decode_i_blocks(v);
2971 vc1_decode_b_blocks(v);