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 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;
72 int topleft_mb_pos, top_mb_pos;
73 int stride_y, fieldtx = 0;
76 /* The put pixels loop is always one MB row behind the decoding loop,
77 * because we can only put pixels when overlap filtering is done, and
78 * for filtering of the bottom edge of a MB, we need the next MB row
80 * Within the row, the put pixels loop is also one MB col behind the
81 * decoding loop. The reason for this is again, because for filtering
82 * of the right MB edge, we need the next MB present. */
83 if (!s->first_slice_line) {
85 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
86 if (v->fcm == ILACE_FRAME)
87 fieldtx = v->fieldtx_plane[topleft_mb_pos];
88 stride_y = s->linesize << fieldtx;
89 v_dist = (16 - fieldtx) >> (fieldtx == 0);
90 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
91 s->dest[0] - 16 * s->linesize - 16,
93 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
94 s->dest[0] - 16 * s->linesize - 8,
96 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
97 s->dest[0] - v_dist * s->linesize - 16,
99 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
100 s->dest[0] - v_dist * s->linesize - 8,
102 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
103 s->dest[1] - 8 * s->uvlinesize - 8,
105 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
106 s->dest[2] - 8 * s->uvlinesize - 8,
109 if (s->mb_x == s->mb_width - 1) {
110 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
111 if (v->fcm == ILACE_FRAME)
112 fieldtx = v->fieldtx_plane[top_mb_pos];
113 stride_y = s->linesize << fieldtx;
114 v_dist = fieldtx ? 15 : 8;
115 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
116 s->dest[0] - 16 * s->linesize,
118 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
119 s->dest[0] - 16 * s->linesize + 8,
121 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
122 s->dest[0] - v_dist * s->linesize,
124 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
125 s->dest[0] - v_dist * s->linesize + 8,
127 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
128 s->dest[1] - 8 * s->uvlinesize,
130 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
131 s->dest[2] - 8 * s->uvlinesize,
136 #define inc_blk_idx(idx) do { \
138 if (idx >= v->n_allocated_blks) \
142 inc_blk_idx(v->topleft_blk_idx);
143 inc_blk_idx(v->top_blk_idx);
144 inc_blk_idx(v->left_blk_idx);
145 inc_blk_idx(v->cur_blk_idx);
148 /***********************************************************************/
150 * @name VC-1 Block-level functions
151 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
157 * @brief Get macroblock-level quantizer scale
159 #define GET_MQUANT() \
160 if (v->dquantfrm) { \
162 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
163 if (v->dqbilevel) { \
164 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
166 mqdiff = get_bits(gb, 3); \
168 mquant = v->pq + mqdiff; \
170 mquant = get_bits(gb, 5); \
173 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
174 edges = 1 << v->dqsbedge; \
175 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
176 edges = (3 << v->dqsbedge) % 15; \
177 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
179 if ((edges&1) && !s->mb_x) \
181 if ((edges&2) && s->first_slice_line) \
183 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
185 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
187 if (!mquant || mquant > 31) { \
188 av_log(v->s.avctx, AV_LOG_ERROR, \
189 "Overriding invalid mquant %d\n", mquant); \
195 * @def GET_MVDATA(_dmv_x, _dmv_y)
196 * @brief Get MV differentials
197 * @see MVDATA decoding from 8.3.5.2, p(1)20
198 * @param _dmv_x Horizontal differential for decoded MV
199 * @param _dmv_y Vertical differential for decoded MV
201 #define GET_MVDATA(_dmv_x, _dmv_y) \
202 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
203 VC1_MV_DIFF_VLC_BITS, 2); \
211 _dmv_x = _dmv_y = 0; \
212 } else if (index == 35) { \
213 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
214 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
215 } else if (index == 36) { \
220 index1 = index % 6; \
221 _dmv_x = offset_table[1][index1]; \
222 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
224 val = get_bits(gb, val); \
225 sign = 0 - (val & 1); \
226 _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign; \
229 index1 = index / 6; \
230 _dmv_y = offset_table[1][index1]; \
231 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
233 val = get_bits(gb, val); \
234 sign = 0 - (val & 1); \
235 _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign; \
239 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
240 int *dmv_y, int *pred_flag)
243 int extend_x = 0, extend_y = 0;
244 GetBitContext *gb = &v->s.gb;
249 bits = VC1_2REF_MVDATA_VLC_BITS;
252 bits = VC1_1REF_MVDATA_VLC_BITS;
255 switch (v->dmvrange) {
263 extend_x = extend_y = 1;
266 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
268 *dmv_x = get_bits(gb, v->k_x);
269 *dmv_y = get_bits(gb, v->k_y);
272 *pred_flag = *dmv_y & 1;
273 *dmv_y = (*dmv_y + *pred_flag) >> 1;
275 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
280 av_assert0(index < esc);
281 index1 = (index + 1) % 9;
283 val = get_bits(gb, index1 + extend_x);
284 sign = 0 - (val & 1);
285 *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
288 index1 = (index + 1) / 9;
289 if (index1 > v->numref) {
290 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
291 sign = 0 - (val & 1);
292 *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
295 if (v->numref && pred_flag)
296 *pred_flag = index1 & 1;
300 /** Reconstruct motion vector for B-frame and do motion compensation
302 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
303 int direct, int mode)
310 if (mode == BMV_TYPE_INTERPOLATED) {
316 ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
319 /** Get predicted DC value for I-frames only
320 * prediction dir: left=0, top=1
321 * @param s MpegEncContext
322 * @param overlap flag indicating that overlap filtering is used
323 * @param pq integer part of picture quantizer
324 * @param[in] n block index in the current MB
325 * @param dc_val_ptr Pointer to DC predictor
326 * @param dir_ptr Prediction direction for use in AC prediction
328 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
329 int16_t **dc_val_ptr, int *dir_ptr)
331 int a, b, c, wrap, pred, scale;
333 static const uint16_t dcpred[32] = {
334 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
335 114, 102, 93, 85, 79, 73, 68, 64,
336 60, 57, 54, 51, 49, 47, 45, 43,
337 41, 39, 38, 37, 35, 34, 33
340 /* find prediction - wmv3_dc_scale always used here in fact */
341 if (n < 4) scale = s->y_dc_scale;
342 else scale = s->c_dc_scale;
344 wrap = s->block_wrap[n];
345 dc_val = s->dc_val[0] + s->block_index[n];
351 b = dc_val[ - 1 - wrap];
354 if (pq < 9 || !overlap) {
355 /* Set outer values */
356 if (s->first_slice_line && (n != 2 && n != 3))
357 b = a = dcpred[scale];
358 if (s->mb_x == 0 && (n != 1 && n != 3))
359 b = c = dcpred[scale];
361 /* Set outer values */
362 if (s->first_slice_line && (n != 2 && n != 3))
364 if (s->mb_x == 0 && (n != 1 && n != 3))
368 if (abs(a - b) <= abs(b - c)) {
370 *dir_ptr = 1; // left
376 /* update predictor */
377 *dc_val_ptr = &dc_val[0];
382 /** Get predicted DC value
383 * prediction dir: left=0, top=1
384 * @param s MpegEncContext
385 * @param overlap flag indicating that overlap filtering is used
386 * @param pq integer part of picture quantizer
387 * @param[in] n block index in the current MB
388 * @param a_avail flag indicating top block availability
389 * @param c_avail flag indicating left block availability
390 * @param dc_val_ptr Pointer to DC predictor
391 * @param dir_ptr Prediction direction for use in AC prediction
393 static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
394 int a_avail, int c_avail,
395 int16_t **dc_val_ptr, int *dir_ptr)
397 int a, b, c, wrap, pred;
399 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
403 wrap = s->block_wrap[n];
404 dc_val = s->dc_val[0] + s->block_index[n];
410 b = dc_val[ - 1 - wrap];
412 /* scale predictors if needed */
413 q1 = s->current_picture.qscale_table[mb_pos];
414 dqscale_index = s->y_dc_scale_table[q1] - 1;
415 if (dqscale_index < 0)
417 if (c_avail && (n != 1 && n != 3)) {
418 q2 = s->current_picture.qscale_table[mb_pos - 1];
420 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
422 if (a_avail && (n != 2 && n != 3)) {
423 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
425 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
427 if (a_avail && c_avail && (n != 3)) {
433 q2 = s->current_picture.qscale_table[off];
435 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
438 if (a_avail && c_avail) {
439 if (abs(a - b) <= abs(b - c)) {
441 *dir_ptr = 1; // left
446 } else if (a_avail) {
449 } else if (c_avail) {
451 *dir_ptr = 1; // left
454 *dir_ptr = 1; // left
457 /* update predictor */
458 *dc_val_ptr = &dc_val[0];
462 /** @} */ // Block group
465 * @name VC1 Macroblock-level functions in Simple/Main Profiles
466 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
470 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
471 uint8_t **coded_block_ptr)
473 int xy, wrap, pred, a, b, c;
475 xy = s->block_index[n];
481 a = s->coded_block[xy - 1 ];
482 b = s->coded_block[xy - 1 - wrap];
483 c = s->coded_block[xy - wrap];
492 *coded_block_ptr = &s->coded_block[xy];
498 * Decode one AC coefficient
499 * @param v The VC1 context
500 * @param last Last coefficient
501 * @param skip How much zero coefficients to skip
502 * @param value Decoded AC coefficient value
503 * @param codingset set of VLC to decode data
506 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
507 int *value, int codingset)
509 GetBitContext *gb = &v->s.gb;
510 int index, escape, run = 0, level = 0, lst = 0;
512 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
513 if (index != ff_vc1_ac_sizes[codingset] - 1) {
514 run = vc1_index_decode_table[codingset][index][0];
515 level = vc1_index_decode_table[codingset][index][1];
516 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
520 escape = decode210(gb);
522 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
523 run = vc1_index_decode_table[codingset][index][0];
524 level = vc1_index_decode_table[codingset][index][1];
525 lst = index >= vc1_last_decode_table[codingset];
528 level += vc1_last_delta_level_table[codingset][run];
530 level += vc1_delta_level_table[codingset][run];
533 run += vc1_last_delta_run_table[codingset][level] + 1;
535 run += vc1_delta_run_table[codingset][level] + 1;
542 if (v->s.esc3_level_length == 0) {
543 if (v->pq < 8 || v->dquantfrm) { // table 59
544 v->s.esc3_level_length = get_bits(gb, 3);
545 if (!v->s.esc3_level_length)
546 v->s.esc3_level_length = get_bits(gb, 2) + 8;
548 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
550 v->s.esc3_run_length = 3 + get_bits(gb, 2);
552 run = get_bits(gb, v->s.esc3_run_length);
553 sign = get_bits1(gb);
554 level = get_bits(gb, v->s.esc3_level_length);
565 /** Decode intra block in intra frames - should be faster than decode_intra_block
566 * @param v VC1Context
567 * @param block block to decode
568 * @param[in] n subblock index
569 * @param coded are AC coeffs present or not
570 * @param codingset set of VLC to decode data
572 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
573 int coded, int codingset)
575 GetBitContext *gb = &v->s.gb;
576 MpegEncContext *s = &v->s;
577 int dc_pred_dir = 0; /* Direction of the DC prediction used */
580 int16_t *ac_val, *ac_val2;
583 /* Get DC differential */
585 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
587 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
590 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
594 if (dcdiff == 119 /* ESC index value */) {
596 if (v->pq == 1) dcdiff = get_bits(gb, 10);
597 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
598 else dcdiff = get_bits(gb, 8);
601 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
603 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
610 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
613 /* Store the quantized DC coeff, used for prediction */
615 block[0] = dcdiff * s->y_dc_scale;
617 block[0] = dcdiff * s->c_dc_scale;
628 int last = 0, skip, value;
629 const uint8_t *zz_table;
633 scale = v->pq * 2 + v->halfpq;
637 zz_table = v->zz_8x8[2];
639 zz_table = v->zz_8x8[3];
641 zz_table = v->zz_8x8[1];
643 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
645 if (dc_pred_dir) // left
648 ac_val -= 16 * s->block_wrap[n];
651 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
655 block[zz_table[i++]] = value;
658 /* apply AC prediction if needed */
660 if (dc_pred_dir) { // left
661 for (k = 1; k < 8; k++)
662 block[k << v->left_blk_sh] += ac_val[k];
664 for (k = 1; k < 8; k++)
665 block[k << v->top_blk_sh] += ac_val[k + 8];
668 /* save AC coeffs for further prediction */
669 for (k = 1; k < 8; k++) {
670 ac_val2[k] = block[k << v->left_blk_sh];
671 ac_val2[k + 8] = block[k << v->top_blk_sh];
674 /* scale AC coeffs */
675 for (k = 1; k < 64; k++)
679 block[k] += (block[k] < 0) ? -v->pq : v->pq;
682 if (s->ac_pred) i = 63;
688 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
692 scale = v->pq * 2 + v->halfpq;
693 memset(ac_val2, 0, 16 * 2);
694 if (dc_pred_dir) { // left
697 memcpy(ac_val2, ac_val, 8 * 2);
699 ac_val -= 16 * s->block_wrap[n];
701 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
704 /* apply AC prediction if needed */
706 if (dc_pred_dir) { //left
707 for (k = 1; k < 8; k++) {
708 block[k << v->left_blk_sh] = ac_val[k] * scale;
709 if (!v->pquantizer && block[k << v->left_blk_sh])
710 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
713 for (k = 1; k < 8; k++) {
714 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
715 if (!v->pquantizer && block[k << v->top_blk_sh])
716 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
722 s->block_last_index[n] = i;
727 /** Decode intra block in intra frames - should be faster than decode_intra_block
728 * @param v VC1Context
729 * @param block block to decode
730 * @param[in] n subblock number
731 * @param coded are AC coeffs present or not
732 * @param codingset set of VLC to decode data
733 * @param mquant quantizer value for this macroblock
735 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
736 int coded, int codingset, int mquant)
738 GetBitContext *gb = &v->s.gb;
739 MpegEncContext *s = &v->s;
740 int dc_pred_dir = 0; /* Direction of the DC prediction used */
742 int16_t *dc_val = NULL;
743 int16_t *ac_val, *ac_val2;
745 int a_avail = v->a_avail, c_avail = v->c_avail;
746 int use_pred = s->ac_pred;
749 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
751 /* Get DC differential */
753 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
755 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
758 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
762 if (dcdiff == 119 /* ESC index value */) {
764 if (mquant == 1) dcdiff = get_bits(gb, 10);
765 else if (mquant == 2) dcdiff = get_bits(gb, 9);
766 else dcdiff = get_bits(gb, 8);
769 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
770 else if (mquant == 2)
771 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
778 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
781 /* Store the quantized DC coeff, used for prediction */
783 block[0] = dcdiff * s->y_dc_scale;
785 block[0] = dcdiff * s->c_dc_scale;
791 /* check if AC is needed at all */
792 if (!a_avail && !c_avail)
794 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
797 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
799 if (dc_pred_dir) // left
802 ac_val -= 16 * s->block_wrap[n];
804 q1 = s->current_picture.qscale_table[mb_pos];
805 if ( dc_pred_dir && c_avail && mb_pos)
806 q2 = s->current_picture.qscale_table[mb_pos - 1];
807 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
808 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
809 if ( dc_pred_dir && n == 1)
811 if (!dc_pred_dir && n == 2)
817 int last = 0, skip, value;
818 const uint8_t *zz_table;
822 if (!use_pred && v->fcm == ILACE_FRAME) {
823 zz_table = v->zzi_8x8;
825 if (!dc_pred_dir) // top
826 zz_table = v->zz_8x8[2];
828 zz_table = v->zz_8x8[3];
831 if (v->fcm != ILACE_FRAME)
832 zz_table = v->zz_8x8[1];
834 zz_table = v->zzi_8x8;
838 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
842 block[zz_table[i++]] = value;
845 /* apply AC prediction if needed */
847 /* scale predictors if needed*/
848 if (q2 && q1 != q2) {
849 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
850 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
853 return AVERROR_INVALIDDATA;
854 if (dc_pred_dir) { // left
855 for (k = 1; k < 8; k++)
856 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
858 for (k = 1; k < 8; k++)
859 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
862 if (dc_pred_dir) { //left
863 for (k = 1; k < 8; k++)
864 block[k << v->left_blk_sh] += ac_val[k];
866 for (k = 1; k < 8; k++)
867 block[k << v->top_blk_sh] += ac_val[k + 8];
871 /* save AC coeffs for further prediction */
872 for (k = 1; k < 8; k++) {
873 ac_val2[k ] = block[k << v->left_blk_sh];
874 ac_val2[k + 8] = block[k << v->top_blk_sh];
877 /* scale AC coeffs */
878 for (k = 1; k < 64; k++)
882 block[k] += (block[k] < 0) ? -mquant : mquant;
885 if (use_pred) i = 63;
886 } else { // no AC coeffs
889 memset(ac_val2, 0, 16 * 2);
890 if (dc_pred_dir) { // left
892 memcpy(ac_val2, ac_val, 8 * 2);
893 if (q2 && q1 != q2) {
894 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
895 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
897 return AVERROR_INVALIDDATA;
898 for (k = 1; k < 8; k++)
899 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
904 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
905 if (q2 && q1 != q2) {
906 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
907 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
909 return AVERROR_INVALIDDATA;
910 for (k = 1; k < 8; k++)
911 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
916 /* apply AC prediction if needed */
918 if (dc_pred_dir) { // left
919 for (k = 1; k < 8; k++) {
920 block[k << v->left_blk_sh] = ac_val2[k] * scale;
921 if (!v->pquantizer && block[k << v->left_blk_sh])
922 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
925 for (k = 1; k < 8; k++) {
926 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
927 if (!v->pquantizer && block[k << v->top_blk_sh])
928 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
934 s->block_last_index[n] = i;
939 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
940 * @param v VC1Context
941 * @param block block to decode
942 * @param[in] n subblock index
943 * @param coded are AC coeffs present or not
944 * @param mquant block quantizer
945 * @param codingset set of VLC to decode data
947 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
948 int coded, int mquant, int codingset)
950 GetBitContext *gb = &v->s.gb;
951 MpegEncContext *s = &v->s;
952 int dc_pred_dir = 0; /* Direction of the DC prediction used */
954 int16_t *dc_val = NULL;
955 int16_t *ac_val, *ac_val2;
957 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
958 int a_avail = v->a_avail, c_avail = v->c_avail;
959 int use_pred = s->ac_pred;
963 s->bdsp.clear_block(block);
965 /* XXX: Guard against dumb values of mquant */
966 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
968 /* Set DC scale - y and c use the same */
969 s->y_dc_scale = s->y_dc_scale_table[mquant];
970 s->c_dc_scale = s->c_dc_scale_table[mquant];
972 /* Get DC differential */
974 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
976 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
979 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
983 if (dcdiff == 119 /* ESC index value */) {
985 if (mquant == 1) dcdiff = get_bits(gb, 10);
986 else if (mquant == 2) dcdiff = get_bits(gb, 9);
987 else dcdiff = get_bits(gb, 8);
990 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
991 else if (mquant == 2)
992 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
999 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1002 /* Store the quantized DC coeff, used for prediction */
1005 block[0] = dcdiff * s->y_dc_scale;
1007 block[0] = dcdiff * s->c_dc_scale;
1013 /* check if AC is needed at all and adjust direction if needed */
1014 if (!a_avail) dc_pred_dir = 1;
1015 if (!c_avail) dc_pred_dir = 0;
1016 if (!a_avail && !c_avail) use_pred = 0;
1017 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1020 scale = mquant * 2 + v->halfpq;
1022 if (dc_pred_dir) //left
1025 ac_val -= 16 * s->block_wrap[n];
1027 q1 = s->current_picture.qscale_table[mb_pos];
1028 if (dc_pred_dir && c_avail && mb_pos)
1029 q2 = s->current_picture.qscale_table[mb_pos - 1];
1030 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
1031 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1032 if ( dc_pred_dir && n == 1)
1034 if (!dc_pred_dir && n == 2)
1036 if (n == 3) q2 = q1;
1039 int last = 0, skip, value;
1043 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1047 if (v->fcm == PROGRESSIVE)
1048 block[v->zz_8x8[0][i++]] = value;
1050 if (use_pred && (v->fcm == ILACE_FRAME)) {
1051 if (!dc_pred_dir) // top
1052 block[v->zz_8x8[2][i++]] = value;
1054 block[v->zz_8x8[3][i++]] = value;
1056 block[v->zzi_8x8[i++]] = value;
1061 /* apply AC prediction if needed */
1063 /* scale predictors if needed*/
1064 if (q2 && q1 != q2) {
1065 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1066 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1069 return AVERROR_INVALIDDATA;
1070 if (dc_pred_dir) { // left
1071 for (k = 1; k < 8; k++)
1072 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1074 for (k = 1; k < 8; k++)
1075 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1078 if (dc_pred_dir) { // left
1079 for (k = 1; k < 8; k++)
1080 block[k << v->left_blk_sh] += ac_val[k];
1082 for (k = 1; k < 8; k++)
1083 block[k << v->top_blk_sh] += ac_val[k + 8];
1087 /* save AC coeffs for further prediction */
1088 for (k = 1; k < 8; k++) {
1089 ac_val2[k ] = block[k << v->left_blk_sh];
1090 ac_val2[k + 8] = block[k << v->top_blk_sh];
1093 /* scale AC coeffs */
1094 for (k = 1; k < 64; k++)
1098 block[k] += (block[k] < 0) ? -mquant : mquant;
1101 if (use_pred) i = 63;
1102 } else { // no AC coeffs
1105 memset(ac_val2, 0, 16 * 2);
1106 if (dc_pred_dir) { // left
1108 memcpy(ac_val2, ac_val, 8 * 2);
1109 if (q2 && q1 != q2) {
1110 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1111 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1113 return AVERROR_INVALIDDATA;
1114 for (k = 1; k < 8; k++)
1115 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1120 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1121 if (q2 && q1 != q2) {
1122 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1123 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1125 return AVERROR_INVALIDDATA;
1126 for (k = 1; k < 8; k++)
1127 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1132 /* apply AC prediction if needed */
1134 if (dc_pred_dir) { // left
1135 for (k = 1; k < 8; k++) {
1136 block[k << v->left_blk_sh] = ac_val2[k] * scale;
1137 if (!v->pquantizer && block[k << v->left_blk_sh])
1138 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1141 for (k = 1; k < 8; k++) {
1142 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1143 if (!v->pquantizer && block[k << v->top_blk_sh])
1144 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1150 s->block_last_index[n] = i;
1157 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1158 int mquant, int ttmb, int first_block,
1159 uint8_t *dst, int linesize, int skip_block,
1162 MpegEncContext *s = &v->s;
1163 GetBitContext *gb = &s->gb;
1166 int scale, off, idx, last, skip, value;
1167 int ttblk = ttmb & 7;
1170 s->bdsp.clear_block(block);
1173 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)];
1175 if (ttblk == TT_4X4) {
1176 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1178 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1179 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1180 || (!v->res_rtm_flag && !first_block))) {
1181 subblkpat = decode012(gb);
1183 subblkpat ^= 3; // swap decoded pattern bits
1184 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1186 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1189 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1191 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1192 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1193 subblkpat = 2 - (ttblk == TT_8X4_TOP);
1196 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1197 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1206 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1211 idx = v->zz_8x8[0][i++];
1213 idx = v->zzi_8x8[i++];
1214 block[idx] = value * scale;
1216 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1220 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1222 v->vc1dsp.vc1_inv_trans_8x8(block);
1223 s->idsp.add_pixels_clamped(block, dst, linesize);
1228 pat = ~subblkpat & 0xF;
1229 for (j = 0; j < 4; j++) {
1230 last = subblkpat & (1 << (3 - j));
1232 off = (j & 1) * 4 + (j & 2) * 16;
1234 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1239 idx = ff_vc1_simple_progressive_4x4_zz[i++];
1241 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1242 block[idx + off] = value * scale;
1244 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1246 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1248 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1250 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1255 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1256 for (j = 0; j < 2; j++) {
1257 last = subblkpat & (1 << (1 - j));
1261 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1266 idx = v->zz_8x4[i++] + off;
1268 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1269 block[idx] = value * scale;
1271 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1273 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1275 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1277 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1282 pat = ~(subblkpat * 5) & 0xF;
1283 for (j = 0; j < 2; j++) {
1284 last = subblkpat & (1 << (1 - j));
1288 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1293 idx = v->zz_4x8[i++] + off;
1295 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1296 block[idx] = value * scale;
1298 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1300 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1302 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1304 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1310 *ttmb_out |= ttblk << (n * 4);
1314 /** @} */ // Macroblock group
1316 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
1318 /** Decode one P-frame MB
1320 static int vc1_decode_p_mb(VC1Context *v)
1322 MpegEncContext *s = &v->s;
1323 GetBitContext *gb = &s->gb;
1325 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1326 int cbp; /* cbp decoding stuff */
1327 int mqdiff, mquant; /* MB quantization */
1328 int ttmb = v->ttfrm; /* MB Transform type */
1330 int mb_has_coeffs = 1; /* last_flag */
1331 int dmv_x, dmv_y; /* Differential MV components */
1332 int index, index1; /* LUT indexes */
1333 int val, sign; /* temp values */
1334 int first_block = 1;
1336 int skipped, fourmv;
1337 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1339 mquant = v->pq; /* lossy initialization */
1341 if (v->mv_type_is_raw)
1342 fourmv = get_bits1(gb);
1344 fourmv = v->mv_type_mb_plane[mb_pos];
1346 skipped = get_bits1(gb);
1348 skipped = v->s.mbskip_table[mb_pos];
1350 if (!fourmv) { /* 1MV mode */
1352 GET_MVDATA(dmv_x, dmv_y);
1355 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1356 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1358 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1359 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1361 /* FIXME Set DC val for inter block ? */
1362 if (s->mb_intra && !mb_has_coeffs) {
1364 s->ac_pred = get_bits1(gb);
1366 } else if (mb_has_coeffs) {
1368 s->ac_pred = get_bits1(gb);
1369 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1375 s->current_picture.qscale_table[mb_pos] = mquant;
1377 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1378 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1379 VC1_TTMB_VLC_BITS, 2);
1380 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1382 for (i = 0; i < 6; i++) {
1383 s->dc_val[0][s->block_index[i]] = 0;
1385 val = ((cbp >> (5 - i)) & 1);
1386 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1387 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1389 /* check if prediction blocks A and C are available */
1390 v->a_avail = v->c_avail = 0;
1391 if (i == 2 || i == 3 || !s->first_slice_line)
1392 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1393 if (i == 1 || i == 3 || s->mb_x)
1394 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1396 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1397 (i & 4) ? v->codingset2 : v->codingset);
1398 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1400 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1402 for (j = 0; j < 64; j++)
1403 s->block[i][j] <<= 1;
1404 s->idsp.put_signed_pixels_clamped(s->block[i],
1405 s->dest[dst_idx] + off,
1406 i & 4 ? s->uvlinesize
1408 if (v->pq >= 9 && v->overlap) {
1410 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1412 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1414 block_cbp |= 0xF << (i << 2);
1415 block_intra |= 1 << i;
1417 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
1418 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1419 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1420 block_cbp |= pat << (i << 2);
1421 if (!v->ttmbf && ttmb < 8)
1428 for (i = 0; i < 6; i++) {
1429 v->mb_type[0][s->block_index[i]] = 0;
1430 s->dc_val[0][s->block_index[i]] = 0;
1432 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1433 s->current_picture.qscale_table[mb_pos] = 0;
1434 ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1435 ff_vc1_mc_1mv(v, 0);
1437 } else { // 4MV mode
1438 if (!skipped /* unskipped MB */) {
1439 int intra_count = 0, coded_inter = 0;
1440 int is_intra[6], is_coded[6];
1442 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1443 for (i = 0; i < 6; i++) {
1444 val = ((cbp >> (5 - i)) & 1);
1445 s->dc_val[0][s->block_index[i]] = 0;
1452 GET_MVDATA(dmv_x, dmv_y);
1454 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1456 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1457 intra_count += s->mb_intra;
1458 is_intra[i] = s->mb_intra;
1459 is_coded[i] = mb_has_coeffs;
1462 is_intra[i] = (intra_count >= 3);
1466 ff_vc1_mc_4mv_chroma(v, 0);
1467 v->mb_type[0][s->block_index[i]] = is_intra[i];
1469 coded_inter = !is_intra[i] & is_coded[i];
1471 // if there are no coded blocks then don't do anything more
1473 if (!intra_count && !coded_inter)
1476 s->current_picture.qscale_table[mb_pos] = mquant;
1477 /* test if block is intra and has pred */
1480 for (i = 0; i < 6; i++)
1482 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1483 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1489 s->ac_pred = get_bits1(gb);
1493 if (!v->ttmbf && coded_inter)
1494 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1495 for (i = 0; i < 6; i++) {
1497 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1498 s->mb_intra = is_intra[i];
1500 /* check if prediction blocks A and C are available */
1501 v->a_avail = v->c_avail = 0;
1502 if (i == 2 || i == 3 || !s->first_slice_line)
1503 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1504 if (i == 1 || i == 3 || s->mb_x)
1505 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1507 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
1508 (i & 4) ? v->codingset2 : v->codingset);
1509 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1511 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1513 for (j = 0; j < 64; j++)
1514 s->block[i][j] <<= 1;
1515 s->idsp.put_signed_pixels_clamped(s->block[i],
1516 s->dest[dst_idx] + off,
1517 (i & 4) ? s->uvlinesize
1519 if (v->pq >= 9 && v->overlap) {
1521 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1523 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1525 block_cbp |= 0xF << (i << 2);
1526 block_intra |= 1 << i;
1527 } else if (is_coded[i]) {
1528 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1529 first_block, s->dest[dst_idx] + off,
1530 (i & 4) ? s->uvlinesize : s->linesize,
1531 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1533 block_cbp |= pat << (i << 2);
1534 if (!v->ttmbf && ttmb < 8)
1539 } else { // skipped MB
1541 s->current_picture.qscale_table[mb_pos] = 0;
1542 for (i = 0; i < 6; i++) {
1543 v->mb_type[0][s->block_index[i]] = 0;
1544 s->dc_val[0][s->block_index[i]] = 0;
1546 for (i = 0; i < 4; i++) {
1547 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1548 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1550 ff_vc1_mc_4mv_chroma(v, 0);
1551 s->current_picture.qscale_table[mb_pos] = 0;
1555 v->cbp[s->mb_x] = block_cbp;
1556 v->ttblk[s->mb_x] = block_tt;
1557 v->is_intra[s->mb_x] = block_intra;
1562 /* Decode one macroblock in an interlaced frame p picture */
1564 static int vc1_decode_p_mb_intfr(VC1Context *v)
1566 MpegEncContext *s = &v->s;
1567 GetBitContext *gb = &s->gb;
1569 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1570 int cbp = 0; /* cbp decoding stuff */
1571 int mqdiff, mquant; /* MB quantization */
1572 int ttmb = v->ttfrm; /* MB Transform type */
1574 int mb_has_coeffs = 1; /* last_flag */
1575 int dmv_x, dmv_y; /* Differential MV components */
1576 int val; /* temp value */
1577 int first_block = 1;
1579 int skipped, fourmv = 0, twomv = 0;
1580 int block_cbp = 0, pat, block_tt = 0;
1581 int idx_mbmode = 0, mvbp;
1582 int stride_y, fieldtx;
1584 mquant = v->pq; /* Lossy initialization */
1587 skipped = get_bits1(gb);
1589 skipped = v->s.mbskip_table[mb_pos];
1591 if (v->fourmvswitch)
1592 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1594 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1595 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1596 /* store the motion vector type in a flag (useful later) */
1597 case MV_PMODE_INTFR_4MV:
1599 v->blk_mv_type[s->block_index[0]] = 0;
1600 v->blk_mv_type[s->block_index[1]] = 0;
1601 v->blk_mv_type[s->block_index[2]] = 0;
1602 v->blk_mv_type[s->block_index[3]] = 0;
1604 case MV_PMODE_INTFR_4MV_FIELD:
1606 v->blk_mv_type[s->block_index[0]] = 1;
1607 v->blk_mv_type[s->block_index[1]] = 1;
1608 v->blk_mv_type[s->block_index[2]] = 1;
1609 v->blk_mv_type[s->block_index[3]] = 1;
1611 case MV_PMODE_INTFR_2MV_FIELD:
1613 v->blk_mv_type[s->block_index[0]] = 1;
1614 v->blk_mv_type[s->block_index[1]] = 1;
1615 v->blk_mv_type[s->block_index[2]] = 1;
1616 v->blk_mv_type[s->block_index[3]] = 1;
1618 case MV_PMODE_INTFR_1MV:
1619 v->blk_mv_type[s->block_index[0]] = 0;
1620 v->blk_mv_type[s->block_index[1]] = 0;
1621 v->blk_mv_type[s->block_index[2]] = 0;
1622 v->blk_mv_type[s->block_index[3]] = 0;
1625 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1626 for (i = 0; i < 4; i++) {
1627 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1628 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1630 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1632 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1633 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1634 mb_has_coeffs = get_bits1(gb);
1636 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1637 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1639 s->current_picture.qscale_table[mb_pos] = mquant;
1640 /* Set DC scale - y and c use the same (not sure if necessary here) */
1641 s->y_dc_scale = s->y_dc_scale_table[mquant];
1642 s->c_dc_scale = s->c_dc_scale_table[mquant];
1644 for (i = 0; i < 6; i++) {
1645 v->a_avail = v->c_avail = 0;
1646 v->mb_type[0][s->block_index[i]] = 1;
1647 s->dc_val[0][s->block_index[i]] = 0;
1649 val = ((cbp >> (5 - i)) & 1);
1650 if (i == 2 || i == 3 || !s->first_slice_line)
1651 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1652 if (i == 1 || i == 3 || s->mb_x)
1653 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1655 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1656 (i & 4) ? v->codingset2 : v->codingset);
1657 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
1658 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1660 stride_y = s->linesize << fieldtx;
1661 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1663 stride_y = s->uvlinesize;
1666 s->idsp.put_signed_pixels_clamped(s->block[i],
1667 s->dest[dst_idx] + off,
1672 } else { // inter MB
1673 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1675 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1676 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1677 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1679 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1680 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1681 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1684 s->mb_intra = v->is_intra[s->mb_x] = 0;
1685 for (i = 0; i < 6; i++)
1686 v->mb_type[0][s->block_index[i]] = 0;
1687 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1688 /* for all motion vector read MVDATA and motion compensate each block */
1692 for (i = 0; i < 6; i++) {
1695 val = ((mvbp >> (3 - i)) & 1);
1697 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1699 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1700 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1701 } else if (i == 4) {
1702 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1709 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1711 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1712 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1713 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1716 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1718 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1719 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1720 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1721 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1723 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1726 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1728 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1729 ff_vc1_mc_1mv(v, 0);
1732 GET_MQUANT(); // p. 227
1733 s->current_picture.qscale_table[mb_pos] = mquant;
1734 if (!v->ttmbf && cbp)
1735 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1736 for (i = 0; i < 6; i++) {
1737 s->dc_val[0][s->block_index[i]] = 0;
1739 val = ((cbp >> (5 - i)) & 1);
1741 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1743 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1745 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1746 first_block, s->dest[dst_idx] + off,
1747 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1748 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1749 block_cbp |= pat << (i << 2);
1750 if (!v->ttmbf && ttmb < 8)
1757 s->mb_intra = v->is_intra[s->mb_x] = 0;
1758 for (i = 0; i < 6; i++) {
1759 v->mb_type[0][s->block_index[i]] = 0;
1760 s->dc_val[0][s->block_index[i]] = 0;
1762 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1763 s->current_picture.qscale_table[mb_pos] = 0;
1764 v->blk_mv_type[s->block_index[0]] = 0;
1765 v->blk_mv_type[s->block_index[1]] = 0;
1766 v->blk_mv_type[s->block_index[2]] = 0;
1767 v->blk_mv_type[s->block_index[3]] = 0;
1768 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1769 ff_vc1_mc_1mv(v, 0);
1771 if (s->mb_x == s->mb_width - 1)
1772 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
1776 static int vc1_decode_p_mb_intfi(VC1Context *v)
1778 MpegEncContext *s = &v->s;
1779 GetBitContext *gb = &s->gb;
1781 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1782 int cbp = 0; /* cbp decoding stuff */
1783 int mqdiff, mquant; /* MB quantization */
1784 int ttmb = v->ttfrm; /* MB Transform type */
1786 int mb_has_coeffs = 1; /* last_flag */
1787 int dmv_x, dmv_y; /* Differential MV components */
1788 int val; /* temp values */
1789 int first_block = 1;
1792 int block_cbp = 0, pat, block_tt = 0;
1795 mquant = v->pq; /* Lossy initialization */
1797 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1798 if (idx_mbmode <= 1) { // intra MB
1799 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1801 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1802 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1803 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1805 s->current_picture.qscale_table[mb_pos] = mquant;
1806 /* Set DC scale - y and c use the same (not sure if necessary here) */
1807 s->y_dc_scale = s->y_dc_scale_table[mquant];
1808 s->c_dc_scale = s->c_dc_scale_table[mquant];
1809 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1810 mb_has_coeffs = idx_mbmode & 1;
1812 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1814 for (i = 0; i < 6; i++) {
1815 v->a_avail = v->c_avail = 0;
1816 v->mb_type[0][s->block_index[i]] = 1;
1817 s->dc_val[0][s->block_index[i]] = 0;
1819 val = ((cbp >> (5 - i)) & 1);
1820 if (i == 2 || i == 3 || !s->first_slice_line)
1821 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1822 if (i == 1 || i == 3 || s->mb_x)
1823 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1825 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1826 (i & 4) ? v->codingset2 : v->codingset);
1827 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1829 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1830 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1831 s->idsp.put_signed_pixels_clamped(s->block[i],
1832 s->dest[dst_idx] + off,
1833 (i & 4) ? s->uvlinesize
1835 // TODO: loop filter
1838 s->mb_intra = v->is_intra[s->mb_x] = 0;
1839 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1840 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
1841 if (idx_mbmode <= 5) { // 1-MV
1842 dmv_x = dmv_y = pred_flag = 0;
1843 if (idx_mbmode & 1) {
1844 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1846 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1847 ff_vc1_mc_1mv(v, 0);
1848 mb_has_coeffs = !(idx_mbmode & 2);
1850 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1851 for (i = 0; i < 6; i++) {
1853 dmv_x = dmv_y = pred_flag = 0;
1854 val = ((v->fourmvbp >> (3 - i)) & 1);
1856 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1858 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1859 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1861 ff_vc1_mc_4mv_chroma(v, 0);
1863 mb_has_coeffs = idx_mbmode & 1;
1866 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1870 s->current_picture.qscale_table[mb_pos] = mquant;
1871 if (!v->ttmbf && cbp) {
1872 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1875 for (i = 0; i < 6; i++) {
1876 s->dc_val[0][s->block_index[i]] = 0;
1878 val = ((cbp >> (5 - i)) & 1);
1879 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1881 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1882 first_block, s->dest[dst_idx] + off,
1883 (i & 4) ? s->uvlinesize : s->linesize,
1884 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1886 block_cbp |= pat << (i << 2);
1887 if (!v->ttmbf && ttmb < 8) ttmb = -1;
1892 if (s->mb_x == s->mb_width - 1)
1893 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1897 /** Decode one B-frame MB (in Main profile)
1899 static void vc1_decode_b_mb(VC1Context *v)
1901 MpegEncContext *s = &v->s;
1902 GetBitContext *gb = &s->gb;
1904 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1905 int cbp = 0; /* cbp decoding stuff */
1906 int mqdiff, mquant; /* MB quantization */
1907 int ttmb = v->ttfrm; /* MB Transform type */
1908 int mb_has_coeffs = 0; /* last_flag */
1909 int index, index1; /* LUT indexes */
1910 int val, sign; /* temp values */
1911 int first_block = 1;
1913 int skipped, direct;
1914 int dmv_x[2], dmv_y[2];
1915 int bmvtype = BMV_TYPE_BACKWARD;
1917 mquant = v->pq; /* lossy initialization */
1921 direct = get_bits1(gb);
1923 direct = v->direct_mb_plane[mb_pos];
1925 skipped = get_bits1(gb);
1927 skipped = v->s.mbskip_table[mb_pos];
1929 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1930 for (i = 0; i < 6; i++) {
1931 v->mb_type[0][s->block_index[i]] = 0;
1932 s->dc_val[0][s->block_index[i]] = 0;
1934 s->current_picture.qscale_table[mb_pos] = 0;
1938 GET_MVDATA(dmv_x[0], dmv_y[0]);
1939 dmv_x[1] = dmv_x[0];
1940 dmv_y[1] = dmv_y[0];
1942 if (skipped || !s->mb_intra) {
1943 bmvtype = decode012(gb);
1946 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1949 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1952 bmvtype = BMV_TYPE_INTERPOLATED;
1953 dmv_x[0] = dmv_y[0] = 0;
1957 for (i = 0; i < 6; i++)
1958 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1962 bmvtype = BMV_TYPE_INTERPOLATED;
1963 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1964 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1968 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1971 s->current_picture.qscale_table[mb_pos] = mquant;
1973 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1974 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1975 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1976 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1978 if (!mb_has_coeffs && !s->mb_intra) {
1979 /* no coded blocks - effectively skipped */
1980 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1981 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1984 if (s->mb_intra && !mb_has_coeffs) {
1986 s->current_picture.qscale_table[mb_pos] = mquant;
1987 s->ac_pred = get_bits1(gb);
1989 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1991 if (bmvtype == BMV_TYPE_INTERPOLATED) {
1992 GET_MVDATA(dmv_x[0], dmv_y[0]);
1993 if (!mb_has_coeffs) {
1994 /* interpolated skipped block */
1995 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1996 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2000 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2002 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2005 s->ac_pred = get_bits1(gb);
2006 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2008 s->current_picture.qscale_table[mb_pos] = mquant;
2009 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2010 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2014 for (i = 0; i < 6; i++) {
2015 s->dc_val[0][s->block_index[i]] = 0;
2017 val = ((cbp >> (5 - i)) & 1);
2018 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2019 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2021 /* check if prediction blocks A and C are available */
2022 v->a_avail = v->c_avail = 0;
2023 if (i == 2 || i == 3 || !s->first_slice_line)
2024 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2025 if (i == 1 || i == 3 || s->mb_x)
2026 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2028 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2029 (i & 4) ? v->codingset2 : v->codingset);
2030 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2032 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2034 for (j = 0; j < 64; j++)
2035 s->block[i][j] <<= 1;
2036 s->idsp.put_signed_pixels_clamped(s->block[i],
2037 s->dest[dst_idx] + off,
2038 i & 4 ? s->uvlinesize
2041 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2042 first_block, s->dest[dst_idx] + off,
2043 (i & 4) ? s->uvlinesize : s->linesize,
2044 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2045 if (!v->ttmbf && ttmb < 8)
2052 /** Decode one B-frame MB (in interlaced field B picture)
2054 static void vc1_decode_b_mb_intfi(VC1Context *v)
2056 MpegEncContext *s = &v->s;
2057 GetBitContext *gb = &s->gb;
2059 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2060 int cbp = 0; /* cbp decoding stuff */
2061 int mqdiff, mquant; /* MB quantization */
2062 int ttmb = v->ttfrm; /* MB Transform type */
2063 int mb_has_coeffs = 0; /* last_flag */
2064 int val; /* temp value */
2065 int first_block = 1;
2068 int dmv_x[2], dmv_y[2], pred_flag[2];
2069 int bmvtype = BMV_TYPE_BACKWARD;
2072 mquant = v->pq; /* Lossy initialization */
2075 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2076 if (idx_mbmode <= 1) { // intra MB
2077 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2079 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2080 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2081 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2083 s->current_picture.qscale_table[mb_pos] = mquant;
2084 /* Set DC scale - y and c use the same (not sure if necessary here) */
2085 s->y_dc_scale = s->y_dc_scale_table[mquant];
2086 s->c_dc_scale = s->c_dc_scale_table[mquant];
2087 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2088 mb_has_coeffs = idx_mbmode & 1;
2090 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2092 for (i = 0; i < 6; i++) {
2093 v->a_avail = v->c_avail = 0;
2094 v->mb_type[0][s->block_index[i]] = 1;
2095 s->dc_val[0][s->block_index[i]] = 0;
2097 val = ((cbp >> (5 - i)) & 1);
2098 if (i == 2 || i == 3 || !s->first_slice_line)
2099 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2100 if (i == 1 || i == 3 || s->mb_x)
2101 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2103 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2104 (i & 4) ? v->codingset2 : v->codingset);
2105 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2107 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2109 for (j = 0; j < 64; j++)
2110 s->block[i][j] <<= 1;
2111 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2112 s->idsp.put_signed_pixels_clamped(s->block[i],
2113 s->dest[dst_idx] + off,
2114 (i & 4) ? s->uvlinesize
2116 // TODO: yet to perform loop filter
2119 s->mb_intra = v->is_intra[s->mb_x] = 0;
2120 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2121 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2123 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2125 fwd = v->forward_mb_plane[mb_pos];
2126 if (idx_mbmode <= 5) { // 1-MV
2128 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2129 pred_flag[0] = pred_flag[1] = 0;
2131 bmvtype = BMV_TYPE_FORWARD;
2133 bmvtype = decode012(gb);
2136 bmvtype = BMV_TYPE_BACKWARD;
2139 bmvtype = BMV_TYPE_DIRECT;
2142 bmvtype = BMV_TYPE_INTERPOLATED;
2143 interpmvp = get_bits1(gb);
2146 v->bmvtype = bmvtype;
2147 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2148 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2151 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2153 if (bmvtype == BMV_TYPE_DIRECT) {
2154 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2155 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2156 if (!s->next_picture_ptr->field_picture) {
2157 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2161 ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2162 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2163 mb_has_coeffs = !(idx_mbmode & 2);
2166 bmvtype = BMV_TYPE_FORWARD;
2167 v->bmvtype = bmvtype;
2168 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2169 for (i = 0; i < 6; i++) {
2171 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2172 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2173 val = ((v->fourmvbp >> (3 - i)) & 1);
2175 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2176 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2177 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2179 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2180 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2182 ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2184 mb_has_coeffs = idx_mbmode & 1;
2187 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2191 s->current_picture.qscale_table[mb_pos] = mquant;
2192 if (!v->ttmbf && cbp) {
2193 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2196 for (i = 0; i < 6; i++) {
2197 s->dc_val[0][s->block_index[i]] = 0;
2199 val = ((cbp >> (5 - i)) & 1);
2200 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2202 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2203 first_block, s->dest[dst_idx] + off,
2204 (i & 4) ? s->uvlinesize : s->linesize,
2205 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2206 if (!v->ttmbf && ttmb < 8)
2214 /** Decode one B-frame MB (in interlaced frame B picture)
2216 static int vc1_decode_b_mb_intfr(VC1Context *v)
2218 MpegEncContext *s = &v->s;
2219 GetBitContext *gb = &s->gb;
2221 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2222 int cbp = 0; /* cbp decoding stuff */
2223 int mqdiff, mquant; /* MB quantization */
2224 int ttmb = v->ttfrm; /* MB Transform type */
2225 int mvsw = 0; /* motion vector switch */
2226 int mb_has_coeffs = 1; /* last_flag */
2227 int dmv_x, dmv_y; /* Differential MV components */
2228 int val; /* temp value */
2229 int first_block = 1;
2231 int skipped, direct, twomv = 0;
2232 int block_cbp = 0, pat, block_tt = 0;
2233 int idx_mbmode = 0, mvbp;
2234 int stride_y, fieldtx;
2235 int bmvtype = BMV_TYPE_BACKWARD;
2238 mquant = v->pq; /* Lossy initialization */
2241 skipped = get_bits1(gb);
2243 skipped = v->s.mbskip_table[mb_pos];
2246 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2247 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2249 v->blk_mv_type[s->block_index[0]] = 1;
2250 v->blk_mv_type[s->block_index[1]] = 1;
2251 v->blk_mv_type[s->block_index[2]] = 1;
2252 v->blk_mv_type[s->block_index[3]] = 1;
2254 v->blk_mv_type[s->block_index[0]] = 0;
2255 v->blk_mv_type[s->block_index[1]] = 0;
2256 v->blk_mv_type[s->block_index[2]] = 0;
2257 v->blk_mv_type[s->block_index[3]] = 0;
2262 direct = get_bits1(gb);
2264 direct = v->direct_mb_plane[mb_pos];
2267 if (s->next_picture_ptr->field_picture)
2268 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2269 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);
2270 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);
2271 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);
2272 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);
2275 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);
2276 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);
2277 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);
2278 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);
2280 for (i = 1; i < 4; i += 2) {
2281 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2282 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2283 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2284 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2287 for (i = 1; i < 4; i++) {
2288 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2289 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2290 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2291 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2296 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2297 for (i = 0; i < 4; i++) {
2298 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2299 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2300 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2301 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2303 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2305 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2306 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2307 mb_has_coeffs = get_bits1(gb);
2309 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2310 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2312 s->current_picture.qscale_table[mb_pos] = mquant;
2313 /* Set DC scale - y and c use the same (not sure if necessary here) */
2314 s->y_dc_scale = s->y_dc_scale_table[mquant];
2315 s->c_dc_scale = s->c_dc_scale_table[mquant];
2317 for (i = 0; i < 6; i++) {
2318 v->a_avail = v->c_avail = 0;
2319 v->mb_type[0][s->block_index[i]] = 1;
2320 s->dc_val[0][s->block_index[i]] = 0;
2322 val = ((cbp >> (5 - i)) & 1);
2323 if (i == 2 || i == 3 || !s->first_slice_line)
2324 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2325 if (i == 1 || i == 3 || s->mb_x)
2326 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2328 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2329 (i & 4) ? v->codingset2 : v->codingset);
2330 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
2332 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2334 stride_y = s->linesize << fieldtx;
2335 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2337 stride_y = s->uvlinesize;
2340 s->idsp.put_signed_pixels_clamped(s->block[i],
2341 s->dest[dst_idx] + off,
2345 s->mb_intra = v->is_intra[s->mb_x] = 0;
2347 if (skipped || !s->mb_intra) {
2348 bmvtype = decode012(gb);
2351 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2354 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2357 bmvtype = BMV_TYPE_INTERPOLATED;
2361 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2362 mvsw = get_bits1(gb);
2365 if (!skipped) { // inter MB
2366 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2368 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2370 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2371 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2372 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2373 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2377 for (i = 0; i < 6; i++)
2378 v->mb_type[0][s->block_index[i]] = 0;
2379 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2380 /* for all motion vector read MVDATA and motion compensate each block */
2384 for (i = 0; i < 4; i++) {
2385 ff_vc1_mc_4mv_luma(v, i, 0, 0);
2386 ff_vc1_mc_4mv_luma(v, i, 1, 1);
2388 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2389 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2391 ff_vc1_mc_1mv(v, 0);
2392 ff_vc1_interp_mc(v);
2394 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2396 for (i = 0; i < 4; i++) {
2399 val = ((mvbp >> (3 - i)) & 1);
2401 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2403 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2404 ff_vc1_mc_4mv_luma(v, j, dir, dir);
2405 ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2408 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2409 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2410 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2414 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2416 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2417 ff_vc1_mc_1mv(v, 0);
2421 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2423 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2424 ff_vc1_interp_mc(v);
2426 dir = bmvtype == BMV_TYPE_BACKWARD;
2433 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2434 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2438 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2439 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2442 for (i = 0; i < 2; i++) {
2443 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];
2444 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];
2445 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];
2446 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];
2449 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2450 ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2453 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2454 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2455 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2456 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2457 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2459 dir = bmvtype == BMV_TYPE_BACKWARD;
2461 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2464 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2466 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2467 v->blk_mv_type[s->block_index[0]] = 1;
2468 v->blk_mv_type[s->block_index[1]] = 1;
2469 v->blk_mv_type[s->block_index[2]] = 1;
2470 v->blk_mv_type[s->block_index[3]] = 1;
2471 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2472 for (i = 0; i < 2; i++) {
2473 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];
2474 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];
2476 ff_vc1_mc_1mv(v, dir);
2480 GET_MQUANT(); // p. 227
2481 s->current_picture.qscale_table[mb_pos] = mquant;
2482 if (!v->ttmbf && cbp)
2483 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2484 for (i = 0; i < 6; i++) {
2485 s->dc_val[0][s->block_index[i]] = 0;
2487 val = ((cbp >> (5 - i)) & 1);
2489 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2491 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2493 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2494 first_block, s->dest[dst_idx] + off,
2495 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2496 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
2497 block_cbp |= pat << (i << 2);
2498 if (!v->ttmbf && ttmb < 8)
2506 for (i = 0; i < 6; i++) {
2507 v->mb_type[0][s->block_index[i]] = 0;
2508 s->dc_val[0][s->block_index[i]] = 0;
2510 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2511 s->current_picture.qscale_table[mb_pos] = 0;
2512 v->blk_mv_type[s->block_index[0]] = 0;
2513 v->blk_mv_type[s->block_index[1]] = 0;
2514 v->blk_mv_type[s->block_index[2]] = 0;
2515 v->blk_mv_type[s->block_index[3]] = 0;
2518 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2519 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2520 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2522 dir = bmvtype == BMV_TYPE_BACKWARD;
2523 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2528 for (i = 0; i < 2; i++) {
2529 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];
2530 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];
2531 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];
2532 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];
2535 v->blk_mv_type[s->block_index[0]] = 1;
2536 v->blk_mv_type[s->block_index[1]] = 1;
2537 v->blk_mv_type[s->block_index[2]] = 1;
2538 v->blk_mv_type[s->block_index[3]] = 1;
2539 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2540 for (i = 0; i < 2; i++) {
2541 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];
2542 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];
2548 ff_vc1_mc_1mv(v, dir);
2549 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2550 ff_vc1_interp_mc(v);
2554 if (s->mb_x == s->mb_width - 1)
2555 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2556 v->cbp[s->mb_x] = block_cbp;
2557 v->ttblk[s->mb_x] = block_tt;
2561 /** Decode blocks of I-frame
2563 static void vc1_decode_i_blocks(VC1Context *v)
2566 MpegEncContext *s = &v->s;
2571 /* select codingmode used for VLC tables selection */
2572 switch (v->y_ac_table_index) {
2574 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2577 v->codingset = CS_HIGH_MOT_INTRA;
2580 v->codingset = CS_MID_RATE_INTRA;
2584 switch (v->c_ac_table_index) {
2586 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2589 v->codingset2 = CS_HIGH_MOT_INTER;
2592 v->codingset2 = CS_MID_RATE_INTER;
2596 /* Set DC scale - y and c use the same */
2597 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2598 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2601 s->mb_x = s->mb_y = 0;
2603 s->first_slice_line = 1;
2604 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2606 init_block_index(v);
2607 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2609 ff_update_block_index(s);
2610 dst[0] = s->dest[0];
2611 dst[1] = dst[0] + 8;
2612 dst[2] = s->dest[0] + s->linesize * 8;
2613 dst[3] = dst[2] + 8;
2614 dst[4] = s->dest[1];
2615 dst[5] = s->dest[2];
2616 s->bdsp.clear_blocks(s->block[0]);
2617 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2618 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2619 s->current_picture.qscale_table[mb_pos] = v->pq;
2620 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2621 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2623 // do actual MB decoding and displaying
2624 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2625 v->s.ac_pred = get_bits1(&v->s.gb);
2627 for (k = 0; k < 6; k++) {
2628 val = ((cbp >> (5 - k)) & 1);
2631 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2635 cbp |= val << (5 - k);
2637 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2639 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2641 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2642 if (v->pq >= 9 && v->overlap) {
2644 for (j = 0; j < 64; j++)
2645 s->block[k][j] <<= 1;
2646 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2647 k & 4 ? s->uvlinesize
2651 for (j = 0; j < 64; j++)
2652 s->block[k][j] = (s->block[k][j] - 64) << 1;
2653 s->idsp.put_pixels_clamped(s->block[k], dst[k],
2654 k & 4 ? s->uvlinesize
2659 if (v->pq >= 9 && v->overlap) {
2661 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2662 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2663 if (!(s->flags & CODEC_FLAG_GRAY)) {
2664 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2665 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2668 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2669 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2670 if (!s->first_slice_line) {
2671 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2672 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2673 if (!(s->flags & CODEC_FLAG_GRAY)) {
2674 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2675 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2678 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2679 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2681 if (v->s.loop_filter)
2682 ff_vc1_loop_filter_iblk(v, v->pq);
2684 if (get_bits_count(&s->gb) > v->bits) {
2685 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2686 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2687 get_bits_count(&s->gb), v->bits);
2691 if (!v->s.loop_filter)
2692 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2694 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2696 s->first_slice_line = 0;
2698 if (v->s.loop_filter)
2699 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2701 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2702 * profile, these only differ are when decoding MSS2 rectangles. */
2703 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2706 /** Decode blocks of I-frame for advanced profile
2708 static void vc1_decode_i_blocks_adv(VC1Context *v)
2711 MpegEncContext *s = &v->s;
2717 GetBitContext *gb = &s->gb;
2719 /* select codingmode used for VLC tables selection */
2720 switch (v->y_ac_table_index) {
2722 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2725 v->codingset = CS_HIGH_MOT_INTRA;
2728 v->codingset = CS_MID_RATE_INTRA;
2732 switch (v->c_ac_table_index) {
2734 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2737 v->codingset2 = CS_HIGH_MOT_INTER;
2740 v->codingset2 = CS_MID_RATE_INTER;
2745 s->mb_x = s->mb_y = 0;
2747 s->first_slice_line = 1;
2748 s->mb_y = s->start_mb_y;
2749 if (s->start_mb_y) {
2751 init_block_index(v);
2752 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2753 (1 + s->b8_stride) * sizeof(*s->coded_block));
2755 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2757 init_block_index(v);
2758 for (;s->mb_x < s->mb_width; s->mb_x++) {
2759 int16_t (*block)[64] = v->block[v->cur_blk_idx];
2760 ff_update_block_index(s);
2761 s->bdsp.clear_blocks(block[0]);
2762 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2763 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2764 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2765 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2767 // do actual MB decoding and displaying
2768 if (v->fieldtx_is_raw)
2769 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2770 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2771 if ( v->acpred_is_raw)
2772 v->s.ac_pred = get_bits1(&v->s.gb);
2774 v->s.ac_pred = v->acpred_plane[mb_pos];
2776 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2777 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2781 s->current_picture.qscale_table[mb_pos] = mquant;
2782 /* Set DC scale - y and c use the same */
2783 s->y_dc_scale = s->y_dc_scale_table[mquant];
2784 s->c_dc_scale = s->c_dc_scale_table[mquant];
2786 for (k = 0; k < 6; k++) {
2787 val = ((cbp >> (5 - k)) & 1);
2790 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2794 cbp |= val << (5 - k);
2796 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2797 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2799 vc1_decode_i_block_adv(v, block[k], k, val,
2800 (k < 4) ? v->codingset : v->codingset2, mquant);
2802 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2804 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2807 ff_vc1_smooth_overlap_filter_iblk(v);
2808 vc1_put_signed_blocks_clamped(v);
2809 if (v->s.loop_filter)
2810 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2812 if (get_bits_count(&s->gb) > v->bits) {
2813 // TODO: may need modification to handle slice coding
2814 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2815 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2816 get_bits_count(&s->gb), v->bits);
2820 if (!v->s.loop_filter)
2821 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2823 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2824 s->first_slice_line = 0;
2827 /* raw bottom MB row */
2829 init_block_index(v);
2831 for (;s->mb_x < s->mb_width; s->mb_x++) {
2832 ff_update_block_index(s);
2833 vc1_put_signed_blocks_clamped(v);
2834 if (v->s.loop_filter)
2835 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2837 if (v->s.loop_filter)
2838 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
2839 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2840 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2843 static void vc1_decode_p_blocks(VC1Context *v)
2845 MpegEncContext *s = &v->s;
2846 int apply_loop_filter;
2848 /* select codingmode used for VLC tables selection */
2849 switch (v->c_ac_table_index) {
2851 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2854 v->codingset = CS_HIGH_MOT_INTRA;
2857 v->codingset = CS_MID_RATE_INTRA;
2861 switch (v->c_ac_table_index) {
2863 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2866 v->codingset2 = CS_HIGH_MOT_INTER;
2869 v->codingset2 = CS_MID_RATE_INTER;
2873 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2874 v->fcm == PROGRESSIVE;
2875 s->first_slice_line = 1;
2876 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2877 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2879 init_block_index(v);
2880 for (; s->mb_x < s->mb_width; s->mb_x++) {
2881 ff_update_block_index(s);
2883 if (v->fcm == ILACE_FIELD)
2884 vc1_decode_p_mb_intfi(v);
2885 else if (v->fcm == ILACE_FRAME)
2886 vc1_decode_p_mb_intfr(v);
2887 else vc1_decode_p_mb(v);
2888 if (s->mb_y != s->start_mb_y && apply_loop_filter)
2889 ff_vc1_apply_p_loop_filter(v);
2890 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2891 // TODO: may need modification to handle slice coding
2892 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2893 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2894 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2898 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
2899 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
2900 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2901 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
2902 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2903 s->first_slice_line = 0;
2905 if (apply_loop_filter) {
2907 init_block_index(v);
2908 for (; s->mb_x < s->mb_width; s->mb_x++) {
2909 ff_update_block_index(s);
2910 ff_vc1_apply_p_loop_filter(v);
2913 if (s->end_mb_y >= s->start_mb_y)
2914 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2915 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2916 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2919 static void vc1_decode_b_blocks(VC1Context *v)
2921 MpegEncContext *s = &v->s;
2923 /* select codingmode used for VLC tables selection */
2924 switch (v->c_ac_table_index) {
2926 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2929 v->codingset = CS_HIGH_MOT_INTRA;
2932 v->codingset = CS_MID_RATE_INTRA;
2936 switch (v->c_ac_table_index) {
2938 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2941 v->codingset2 = CS_HIGH_MOT_INTER;
2944 v->codingset2 = CS_MID_RATE_INTER;
2948 s->first_slice_line = 1;
2949 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2951 init_block_index(v);
2952 for (; s->mb_x < s->mb_width; s->mb_x++) {
2953 ff_update_block_index(s);
2955 if (v->fcm == ILACE_FIELD)
2956 vc1_decode_b_mb_intfi(v);
2957 else if (v->fcm == ILACE_FRAME)
2958 vc1_decode_b_mb_intfr(v);
2961 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2962 // TODO: may need modification to handle slice coding
2963 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2964 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2965 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2968 if (v->s.loop_filter)
2969 ff_vc1_loop_filter_iblk(v, v->pq);
2971 if (!v->s.loop_filter)
2972 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2974 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2975 s->first_slice_line = 0;
2977 if (v->s.loop_filter)
2978 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2979 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2980 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2983 static void vc1_decode_skip_blocks(VC1Context *v)
2985 MpegEncContext *s = &v->s;
2987 if (!v->s.last_picture.f->data[0])
2990 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2991 s->first_slice_line = 1;
2992 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2994 init_block_index(v);
2995 ff_update_block_index(s);
2996 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2997 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2998 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2999 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
3000 s->first_slice_line = 0;
3002 s->pict_type = AV_PICTURE_TYPE_P;
3005 void ff_vc1_decode_blocks(VC1Context *v)
3008 v->s.esc3_level_length = 0;
3010 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
3013 v->left_blk_idx = -1;
3014 v->topleft_blk_idx = 1;
3016 switch (v->s.pict_type) {
3017 case AV_PICTURE_TYPE_I:
3018 if (v->profile == PROFILE_ADVANCED)
3019 vc1_decode_i_blocks_adv(v);
3021 vc1_decode_i_blocks(v);
3023 case AV_PICTURE_TYPE_P:
3024 if (v->p_frame_skipped)
3025 vc1_decode_skip_blocks(v);
3027 vc1_decode_p_blocks(v);
3029 case AV_PICTURE_TYPE_B:
3031 if (v->profile == PROFILE_ADVANCED)
3032 vc1_decode_i_blocks_adv(v);
3034 vc1_decode_i_blocks(v);
3036 vc1_decode_b_blocks(v);