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;
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, extend_y;
244 GetBitContext *gb = &v->s.gb;
249 bits = VC1_2REF_MVDATA_VLC_BITS;
252 bits = VC1_1REF_MVDATA_VLC_BITS;
255 extend_x = v->dmvrange & 1;
256 extend_y = (v->dmvrange >> 1) & 1;
257 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
259 *dmv_x = get_bits(gb, v->k_x);
260 *dmv_y = get_bits(gb, v->k_y);
263 *pred_flag = *dmv_y & 1;
264 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
268 av_assert0(index < esc);
269 index1 = (index + 1) % 9;
271 val = get_bits(gb, index1 + extend_x);
272 sign = 0 - (val & 1);
273 *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
276 index1 = (index + 1) / 9;
277 if (index1 > v->numref) {
278 val = get_bits(gb, (index1 >> v->numref) + extend_y);
279 sign = 0 - (val & 1);
280 *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
283 if (v->numref && pred_flag)
284 *pred_flag = index1 & 1;
288 /** Reconstruct motion vector for B-frame and do motion compensation
290 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
291 int direct, int mode)
298 if (mode == BMV_TYPE_INTERPOLATED) {
304 ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
307 /** Get predicted DC value for I-frames only
308 * prediction dir: left=0, top=1
309 * @param s MpegEncContext
310 * @param overlap flag indicating that overlap filtering is used
311 * @param pq integer part of picture quantizer
312 * @param[in] n block index in the current MB
313 * @param dc_val_ptr Pointer to DC predictor
314 * @param dir_ptr Prediction direction for use in AC prediction
316 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
317 int16_t **dc_val_ptr, int *dir_ptr)
319 int a, b, c, wrap, pred, scale;
321 static const uint16_t dcpred[32] = {
322 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
323 114, 102, 93, 85, 79, 73, 68, 64,
324 60, 57, 54, 51, 49, 47, 45, 43,
325 41, 39, 38, 37, 35, 34, 33
328 /* find prediction - wmv3_dc_scale always used here in fact */
329 if (n < 4) scale = s->y_dc_scale;
330 else scale = s->c_dc_scale;
332 wrap = s->block_wrap[n];
333 dc_val = s->dc_val[0] + s->block_index[n];
339 b = dc_val[ - 1 - wrap];
342 if (pq < 9 || !overlap) {
343 /* Set outer values */
344 if (s->first_slice_line && (n != 2 && n != 3))
345 b = a = dcpred[scale];
346 if (s->mb_x == 0 && (n != 1 && n != 3))
347 b = c = dcpred[scale];
349 /* Set outer values */
350 if (s->first_slice_line && (n != 2 && n != 3))
352 if (s->mb_x == 0 && (n != 1 && n != 3))
356 if (abs(a - b) <= abs(b - c)) {
358 *dir_ptr = 1; // left
364 /* update predictor */
365 *dc_val_ptr = &dc_val[0];
370 /** Get predicted DC value
371 * prediction dir: left=0, top=1
372 * @param s MpegEncContext
373 * @param overlap flag indicating that overlap filtering is used
374 * @param pq integer part of picture quantizer
375 * @param[in] n block index in the current MB
376 * @param a_avail flag indicating top block availability
377 * @param c_avail flag indicating left block availability
378 * @param dc_val_ptr Pointer to DC predictor
379 * @param dir_ptr Prediction direction for use in AC prediction
381 static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
382 int a_avail, int c_avail,
383 int16_t **dc_val_ptr, int *dir_ptr)
385 int a, b, c, wrap, pred;
387 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
391 wrap = s->block_wrap[n];
392 dc_val = s->dc_val[0] + s->block_index[n];
398 b = dc_val[ - 1 - wrap];
400 /* scale predictors if needed */
401 q1 = s->current_picture.qscale_table[mb_pos];
402 dqscale_index = s->y_dc_scale_table[q1] - 1;
403 if (dqscale_index < 0)
405 if (c_avail && (n != 1 && n != 3)) {
406 q2 = s->current_picture.qscale_table[mb_pos - 1];
408 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
410 if (a_avail && (n != 2 && n != 3)) {
411 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
413 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
415 if (a_avail && c_avail && (n != 3)) {
421 q2 = s->current_picture.qscale_table[off];
423 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
426 if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
428 *dir_ptr = 1; // left
429 } else if (a_avail) {
434 *dir_ptr = 1; // left
437 /* update predictor */
438 *dc_val_ptr = &dc_val[0];
442 /** @} */ // Block group
445 * @name VC1 Macroblock-level functions in Simple/Main Profiles
446 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
450 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
451 uint8_t **coded_block_ptr)
453 int xy, wrap, pred, a, b, c;
455 xy = s->block_index[n];
461 a = s->coded_block[xy - 1 ];
462 b = s->coded_block[xy - 1 - wrap];
463 c = s->coded_block[xy - wrap];
472 *coded_block_ptr = &s->coded_block[xy];
478 * Decode one AC coefficient
479 * @param v The VC1 context
480 * @param last Last coefficient
481 * @param skip How much zero coefficients to skip
482 * @param value Decoded AC coefficient value
483 * @param codingset set of VLC to decode data
486 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
487 int *value, int codingset)
489 GetBitContext *gb = &v->s.gb;
490 int index, escape, run = 0, level = 0, lst = 0;
492 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
493 if (index != ff_vc1_ac_sizes[codingset] - 1) {
494 run = vc1_index_decode_table[codingset][index][0];
495 level = vc1_index_decode_table[codingset][index][1];
496 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
500 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;
522 if (v->s.esc3_level_length == 0) {
523 if (v->pq < 8 || v->dquantfrm) { // table 59
524 v->s.esc3_level_length = get_bits(gb, 3);
525 if (!v->s.esc3_level_length)
526 v->s.esc3_level_length = get_bits(gb, 2) + 8;
528 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
530 v->s.esc3_run_length = 3 + get_bits(gb, 2);
532 run = get_bits(gb, v->s.esc3_run_length);
533 sign = get_bits1(gb);
534 level = get_bits(gb, v->s.esc3_level_length);
545 /** Decode intra block in intra frames - should be faster than decode_intra_block
546 * @param v VC1Context
547 * @param block block to decode
548 * @param[in] n subblock index
549 * @param coded are AC coeffs present or not
550 * @param codingset set of VLC to decode data
552 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
553 int coded, int codingset)
555 GetBitContext *gb = &v->s.gb;
556 MpegEncContext *s = &v->s;
557 int dc_pred_dir = 0; /* Direction of the DC prediction used */
560 int16_t *ac_val, *ac_val2;
563 /* Get DC differential */
565 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
567 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
570 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
574 const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
575 if (dcdiff == 119 /* ESC index value */) {
576 dcdiff = get_bits(gb, 8 + m);
579 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
586 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
589 /* Store the quantized DC coeff, used for prediction */
591 block[0] = dcdiff * s->y_dc_scale;
593 block[0] = dcdiff * s->c_dc_scale;
604 int last = 0, skip, value;
605 const uint8_t *zz_table;
609 scale = v->pq * 2 + v->halfpq;
613 zz_table = v->zz_8x8[2];
615 zz_table = v->zz_8x8[3];
617 zz_table = v->zz_8x8[1];
619 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
621 if (dc_pred_dir) // left
624 ac_val -= 16 * s->block_wrap[n];
627 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
631 block[zz_table[i++]] = value;
634 /* apply AC prediction if needed */
636 if (dc_pred_dir) { // left
637 for (k = 1; k < 8; k++)
638 block[k << v->left_blk_sh] += ac_val[k];
640 for (k = 1; k < 8; k++)
641 block[k << v->top_blk_sh] += ac_val[k + 8];
644 /* save AC coeffs for further prediction */
645 for (k = 1; k < 8; k++) {
646 ac_val2[k] = block[k << v->left_blk_sh];
647 ac_val2[k + 8] = block[k << v->top_blk_sh];
650 /* scale AC coeffs */
651 for (k = 1; k < 64; k++)
655 block[k] += (block[k] < 0) ? -v->pq : v->pq;
658 if (s->ac_pred) i = 63;
664 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
668 scale = v->pq * 2 + v->halfpq;
669 memset(ac_val2, 0, 16 * 2);
670 if (dc_pred_dir) { // left
673 memcpy(ac_val2, ac_val, 8 * 2);
675 ac_val -= 16 * s->block_wrap[n];
677 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
680 /* apply AC prediction if needed */
682 if (dc_pred_dir) { //left
683 for (k = 1; k < 8; k++) {
684 block[k << v->left_blk_sh] = ac_val[k] * scale;
685 if (!v->pquantizer && block[k << v->left_blk_sh])
686 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
689 for (k = 1; k < 8; k++) {
690 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
691 if (!v->pquantizer && block[k << v->top_blk_sh])
692 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
698 s->block_last_index[n] = i;
703 /** Decode intra block in intra frames - should be faster than decode_intra_block
704 * @param v VC1Context
705 * @param block block to decode
706 * @param[in] n subblock number
707 * @param coded are AC coeffs present or not
708 * @param codingset set of VLC to decode data
709 * @param mquant quantizer value for this macroblock
711 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
712 int coded, int codingset, int mquant)
714 GetBitContext *gb = &v->s.gb;
715 MpegEncContext *s = &v->s;
716 int dc_pred_dir = 0; /* Direction of the DC prediction used */
718 int16_t *dc_val = NULL;
719 int16_t *ac_val, *ac_val2;
721 int a_avail = v->a_avail, c_avail = v->c_avail;
722 int use_pred = s->ac_pred;
725 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
727 /* Get DC differential */
729 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
731 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
734 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
738 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
739 if (dcdiff == 119 /* ESC index value */) {
740 dcdiff = get_bits(gb, 8 + m);
743 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
750 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
753 /* Store the quantized DC coeff, used for prediction */
755 block[0] = dcdiff * s->y_dc_scale;
757 block[0] = dcdiff * s->c_dc_scale;
763 /* check if AC is needed at all */
764 if (!a_avail && !c_avail)
766 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
769 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
771 if (dc_pred_dir) // left
774 ac_val -= 16 * s->block_wrap[n];
776 q1 = s->current_picture.qscale_table[mb_pos];
777 if ( dc_pred_dir && c_avail && mb_pos)
778 q2 = s->current_picture.qscale_table[mb_pos - 1];
779 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
780 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
781 if ( dc_pred_dir && n == 1)
783 if (!dc_pred_dir && n == 2)
789 int last = 0, skip, value;
790 const uint8_t *zz_table;
794 if (!use_pred && v->fcm == ILACE_FRAME) {
795 zz_table = v->zzi_8x8;
797 if (!dc_pred_dir) // top
798 zz_table = v->zz_8x8[2];
800 zz_table = v->zz_8x8[3];
803 if (v->fcm != ILACE_FRAME)
804 zz_table = v->zz_8x8[1];
806 zz_table = v->zzi_8x8;
810 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
814 block[zz_table[i++]] = value;
817 /* apply AC prediction if needed */
819 /* scale predictors if needed*/
820 if (q2 && q1 != q2) {
821 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
822 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
825 return AVERROR_INVALIDDATA;
826 if (dc_pred_dir) { // left
827 for (k = 1; k < 8; k++)
828 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
830 for (k = 1; k < 8; k++)
831 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
834 if (dc_pred_dir) { //left
835 for (k = 1; k < 8; k++)
836 block[k << v->left_blk_sh] += ac_val[k];
838 for (k = 1; k < 8; k++)
839 block[k << v->top_blk_sh] += ac_val[k + 8];
843 /* save AC coeffs for further prediction */
844 for (k = 1; k < 8; k++) {
845 ac_val2[k ] = block[k << v->left_blk_sh];
846 ac_val2[k + 8] = block[k << v->top_blk_sh];
849 /* scale AC coeffs */
850 for (k = 1; k < 64; k++)
854 block[k] += (block[k] < 0) ? -mquant : mquant;
857 if (use_pred) i = 63;
858 } else { // no AC coeffs
861 memset(ac_val2, 0, 16 * 2);
862 if (dc_pred_dir) { // left
864 memcpy(ac_val2, ac_val, 8 * 2);
865 if (q2 && q1 != q2) {
866 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
867 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
869 return AVERROR_INVALIDDATA;
870 for (k = 1; k < 8; k++)
871 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
876 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
877 if (q2 && q1 != q2) {
878 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
879 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
881 return AVERROR_INVALIDDATA;
882 for (k = 1; k < 8; k++)
883 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
888 /* apply AC prediction if needed */
890 if (dc_pred_dir) { // left
891 for (k = 1; k < 8; k++) {
892 block[k << v->left_blk_sh] = ac_val2[k] * scale;
893 if (!v->pquantizer && block[k << v->left_blk_sh])
894 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
897 for (k = 1; k < 8; k++) {
898 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
899 if (!v->pquantizer && block[k << v->top_blk_sh])
900 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
906 s->block_last_index[n] = i;
911 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
912 * @param v VC1Context
913 * @param block block to decode
914 * @param[in] n subblock index
915 * @param coded are AC coeffs present or not
916 * @param mquant block quantizer
917 * @param codingset set of VLC to decode data
919 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
920 int coded, int mquant, int codingset)
922 GetBitContext *gb = &v->s.gb;
923 MpegEncContext *s = &v->s;
924 int dc_pred_dir = 0; /* Direction of the DC prediction used */
926 int16_t *dc_val = NULL;
927 int16_t *ac_val, *ac_val2;
929 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
930 int a_avail = v->a_avail, c_avail = v->c_avail;
931 int use_pred = s->ac_pred;
935 s->bdsp.clear_block(block);
937 /* XXX: Guard against dumb values of mquant */
938 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
940 /* Set DC scale - y and c use the same */
941 s->y_dc_scale = s->y_dc_scale_table[mquant];
942 s->c_dc_scale = s->c_dc_scale_table[mquant];
944 /* Get DC differential */
946 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
948 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
951 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
955 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
956 if (dcdiff == 119 /* ESC index value */) {
957 dcdiff = get_bits(gb, 8 + m);
960 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
967 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
970 /* Store the quantized DC coeff, used for prediction */
973 block[0] = dcdiff * s->y_dc_scale;
975 block[0] = dcdiff * s->c_dc_scale;
981 /* check if AC is needed at all and adjust direction if needed */
982 if (!a_avail) dc_pred_dir = 1;
983 if (!c_avail) dc_pred_dir = 0;
984 if (!a_avail && !c_avail) use_pred = 0;
985 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
988 scale = mquant * 2 + v->halfpq;
990 if (dc_pred_dir) //left
993 ac_val -= 16 * s->block_wrap[n];
995 q1 = s->current_picture.qscale_table[mb_pos];
996 if (dc_pred_dir && c_avail && mb_pos)
997 q2 = s->current_picture.qscale_table[mb_pos - 1];
998 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
999 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1000 if ( dc_pred_dir && n == 1)
1002 if (!dc_pred_dir && n == 2)
1004 if (n == 3) q2 = q1;
1007 int last = 0, skip, value;
1011 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1015 if (v->fcm == PROGRESSIVE)
1016 block[v->zz_8x8[0][i++]] = value;
1018 if (use_pred && (v->fcm == ILACE_FRAME)) {
1019 if (!dc_pred_dir) // top
1020 block[v->zz_8x8[2][i++]] = value;
1022 block[v->zz_8x8[3][i++]] = value;
1024 block[v->zzi_8x8[i++]] = value;
1029 /* apply AC prediction if needed */
1031 /* scale predictors if needed*/
1032 if (q2 && q1 != q2) {
1033 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1034 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1037 return AVERROR_INVALIDDATA;
1038 if (dc_pred_dir) { // left
1039 for (k = 1; k < 8; k++)
1040 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1042 for (k = 1; k < 8; k++)
1043 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1046 if (dc_pred_dir) { // left
1047 for (k = 1; k < 8; k++)
1048 block[k << v->left_blk_sh] += ac_val[k];
1050 for (k = 1; k < 8; k++)
1051 block[k << v->top_blk_sh] += ac_val[k + 8];
1055 /* save AC coeffs for further prediction */
1056 for (k = 1; k < 8; k++) {
1057 ac_val2[k ] = block[k << v->left_blk_sh];
1058 ac_val2[k + 8] = block[k << v->top_blk_sh];
1061 /* scale AC coeffs */
1062 for (k = 1; k < 64; k++)
1066 block[k] += (block[k] < 0) ? -mquant : mquant;
1069 if (use_pred) i = 63;
1070 } else { // no AC coeffs
1073 memset(ac_val2, 0, 16 * 2);
1074 if (dc_pred_dir) { // left
1076 memcpy(ac_val2, ac_val, 8 * 2);
1077 if (q2 && q1 != q2) {
1078 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1079 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1081 return AVERROR_INVALIDDATA;
1082 for (k = 1; k < 8; k++)
1083 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1088 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1089 if (q2 && q1 != q2) {
1090 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1091 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1093 return AVERROR_INVALIDDATA;
1094 for (k = 1; k < 8; k++)
1095 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1100 /* apply AC prediction if needed */
1102 if (dc_pred_dir) { // left
1103 for (k = 1; k < 8; k++) {
1104 block[k << v->left_blk_sh] = ac_val2[k] * scale;
1105 if (!v->pquantizer && block[k << v->left_blk_sh])
1106 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1109 for (k = 1; k < 8; k++) {
1110 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1111 if (!v->pquantizer && block[k << v->top_blk_sh])
1112 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1118 s->block_last_index[n] = i;
1125 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1126 int mquant, int ttmb, int first_block,
1127 uint8_t *dst, int linesize, int skip_block,
1130 MpegEncContext *s = &v->s;
1131 GetBitContext *gb = &s->gb;
1134 int scale, off, idx, last, skip, value;
1135 int ttblk = ttmb & 7;
1138 s->bdsp.clear_block(block);
1141 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)];
1143 if (ttblk == TT_4X4) {
1144 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1146 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1147 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1148 || (!v->res_rtm_flag && !first_block))) {
1149 subblkpat = decode012(gb);
1151 subblkpat ^= 3; // swap decoded pattern bits
1152 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1154 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1157 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1159 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1160 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1161 subblkpat = 2 - (ttblk == TT_8X4_TOP);
1164 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1165 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1174 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1179 idx = v->zz_8x8[0][i++];
1181 idx = v->zzi_8x8[i++];
1182 block[idx] = value * scale;
1184 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1188 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1190 v->vc1dsp.vc1_inv_trans_8x8(block);
1191 s->idsp.add_pixels_clamped(block, dst, linesize);
1196 pat = ~subblkpat & 0xF;
1197 for (j = 0; j < 4; j++) {
1198 last = subblkpat & (1 << (3 - j));
1200 off = (j & 1) * 4 + (j & 2) * 16;
1202 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1207 idx = ff_vc1_simple_progressive_4x4_zz[i++];
1209 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1210 block[idx + off] = value * scale;
1212 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1214 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1216 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1218 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1223 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1224 for (j = 0; j < 2; j++) {
1225 last = subblkpat & (1 << (1 - j));
1229 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1234 idx = v->zz_8x4[i++] + off;
1236 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1237 block[idx] = value * scale;
1239 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1241 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1243 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1245 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1250 pat = ~(subblkpat * 5) & 0xF;
1251 for (j = 0; j < 2; j++) {
1252 last = subblkpat & (1 << (1 - j));
1256 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1261 idx = v->zz_4x8[i++] + off;
1263 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1264 block[idx] = value * scale;
1266 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1268 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1270 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1272 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1278 *ttmb_out |= ttblk << (n * 4);
1282 /** @} */ // Macroblock group
1284 static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 };
1286 /** Decode one P-frame MB
1288 static int vc1_decode_p_mb(VC1Context *v)
1290 MpegEncContext *s = &v->s;
1291 GetBitContext *gb = &s->gb;
1293 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1294 int cbp; /* cbp decoding stuff */
1295 int mqdiff, mquant; /* MB quantization */
1296 int ttmb = v->ttfrm; /* MB Transform type */
1298 int mb_has_coeffs = 1; /* last_flag */
1299 int dmv_x, dmv_y; /* Differential MV components */
1300 int index, index1; /* LUT indexes */
1301 int val, sign; /* temp values */
1302 int first_block = 1;
1304 int skipped, fourmv;
1305 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1307 mquant = v->pq; /* lossy initialization */
1309 if (v->mv_type_is_raw)
1310 fourmv = get_bits1(gb);
1312 fourmv = v->mv_type_mb_plane[mb_pos];
1314 skipped = get_bits1(gb);
1316 skipped = v->s.mbskip_table[mb_pos];
1318 if (!fourmv) { /* 1MV mode */
1320 GET_MVDATA(dmv_x, dmv_y);
1323 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1324 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1326 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1327 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1329 /* FIXME Set DC val for inter block ? */
1330 if (s->mb_intra && !mb_has_coeffs) {
1332 s->ac_pred = get_bits1(gb);
1334 } else if (mb_has_coeffs) {
1336 s->ac_pred = get_bits1(gb);
1337 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1343 s->current_picture.qscale_table[mb_pos] = mquant;
1345 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1346 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1347 VC1_TTMB_VLC_BITS, 2);
1348 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1350 for (i = 0; i < 6; i++) {
1351 s->dc_val[0][s->block_index[i]] = 0;
1353 val = ((cbp >> (5 - i)) & 1);
1354 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1355 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1357 /* check if prediction blocks A and C are available */
1358 v->a_avail = v->c_avail = 0;
1359 if (i == 2 || i == 3 || !s->first_slice_line)
1360 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1361 if (i == 1 || i == 3 || s->mb_x)
1362 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1364 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1365 (i & 4) ? v->codingset2 : v->codingset);
1366 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1368 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1370 for (j = 0; j < 64; j++)
1371 s->block[i][j] <<= 1;
1372 s->idsp.put_signed_pixels_clamped(s->block[i],
1373 s->dest[dst_idx] + off,
1374 i & 4 ? s->uvlinesize
1376 if (v->pq >= 9 && v->overlap) {
1378 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1380 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1382 block_cbp |= 0xF << (i << 2);
1383 block_intra |= 1 << i;
1385 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
1386 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1387 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1388 block_cbp |= pat << (i << 2);
1389 if (!v->ttmbf && ttmb < 8)
1396 for (i = 0; i < 6; i++) {
1397 v->mb_type[0][s->block_index[i]] = 0;
1398 s->dc_val[0][s->block_index[i]] = 0;
1400 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1401 s->current_picture.qscale_table[mb_pos] = 0;
1402 ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1403 ff_vc1_mc_1mv(v, 0);
1405 } else { // 4MV mode
1406 if (!skipped /* unskipped MB */) {
1407 int intra_count = 0, coded_inter = 0;
1408 int is_intra[6], is_coded[6];
1410 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1411 for (i = 0; i < 6; i++) {
1412 val = ((cbp >> (5 - i)) & 1);
1413 s->dc_val[0][s->block_index[i]] = 0;
1420 GET_MVDATA(dmv_x, dmv_y);
1422 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1424 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1425 intra_count += s->mb_intra;
1426 is_intra[i] = s->mb_intra;
1427 is_coded[i] = mb_has_coeffs;
1430 is_intra[i] = (intra_count >= 3);
1434 ff_vc1_mc_4mv_chroma(v, 0);
1435 v->mb_type[0][s->block_index[i]] = is_intra[i];
1437 coded_inter = !is_intra[i] & is_coded[i];
1439 // if there are no coded blocks then don't do anything more
1441 if (!intra_count && !coded_inter)
1444 s->current_picture.qscale_table[mb_pos] = mquant;
1445 /* test if block is intra and has pred */
1448 for (i = 0; i < 6; i++)
1450 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1451 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1457 s->ac_pred = get_bits1(gb);
1461 if (!v->ttmbf && coded_inter)
1462 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1463 for (i = 0; i < 6; i++) {
1465 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1466 s->mb_intra = is_intra[i];
1468 /* check if prediction blocks A and C are available */
1469 v->a_avail = v->c_avail = 0;
1470 if (i == 2 || i == 3 || !s->first_slice_line)
1471 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1472 if (i == 1 || i == 3 || s->mb_x)
1473 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1475 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
1476 (i & 4) ? v->codingset2 : v->codingset);
1477 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1479 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1481 for (j = 0; j < 64; j++)
1482 s->block[i][j] <<= 1;
1483 s->idsp.put_signed_pixels_clamped(s->block[i],
1484 s->dest[dst_idx] + off,
1485 (i & 4) ? s->uvlinesize
1487 if (v->pq >= 9 && v->overlap) {
1489 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1491 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1493 block_cbp |= 0xF << (i << 2);
1494 block_intra |= 1 << i;
1495 } else if (is_coded[i]) {
1496 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1497 first_block, s->dest[dst_idx] + off,
1498 (i & 4) ? s->uvlinesize : s->linesize,
1499 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1501 block_cbp |= pat << (i << 2);
1502 if (!v->ttmbf && ttmb < 8)
1507 } else { // skipped MB
1509 s->current_picture.qscale_table[mb_pos] = 0;
1510 for (i = 0; i < 6; i++) {
1511 v->mb_type[0][s->block_index[i]] = 0;
1512 s->dc_val[0][s->block_index[i]] = 0;
1514 for (i = 0; i < 4; i++) {
1515 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1516 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1518 ff_vc1_mc_4mv_chroma(v, 0);
1519 s->current_picture.qscale_table[mb_pos] = 0;
1523 v->cbp[s->mb_x] = block_cbp;
1524 v->ttblk[s->mb_x] = block_tt;
1525 v->is_intra[s->mb_x] = block_intra;
1530 /* Decode one macroblock in an interlaced frame p picture */
1532 static int vc1_decode_p_mb_intfr(VC1Context *v)
1534 MpegEncContext *s = &v->s;
1535 GetBitContext *gb = &s->gb;
1537 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1538 int cbp = 0; /* cbp decoding stuff */
1539 int mqdiff, mquant; /* MB quantization */
1540 int ttmb = v->ttfrm; /* MB Transform type */
1542 int mb_has_coeffs = 1; /* last_flag */
1543 int dmv_x, dmv_y; /* Differential MV components */
1544 int val; /* temp value */
1545 int first_block = 1;
1547 int skipped, fourmv = 0, twomv = 0;
1548 int block_cbp = 0, pat, block_tt = 0;
1549 int idx_mbmode = 0, mvbp;
1550 int stride_y, fieldtx;
1552 mquant = v->pq; /* Lossy initialization */
1555 skipped = get_bits1(gb);
1557 skipped = v->s.mbskip_table[mb_pos];
1559 if (v->fourmvswitch)
1560 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1562 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1563 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1564 /* store the motion vector type in a flag (useful later) */
1565 case MV_PMODE_INTFR_4MV:
1567 v->blk_mv_type[s->block_index[0]] = 0;
1568 v->blk_mv_type[s->block_index[1]] = 0;
1569 v->blk_mv_type[s->block_index[2]] = 0;
1570 v->blk_mv_type[s->block_index[3]] = 0;
1572 case MV_PMODE_INTFR_4MV_FIELD:
1574 v->blk_mv_type[s->block_index[0]] = 1;
1575 v->blk_mv_type[s->block_index[1]] = 1;
1576 v->blk_mv_type[s->block_index[2]] = 1;
1577 v->blk_mv_type[s->block_index[3]] = 1;
1579 case MV_PMODE_INTFR_2MV_FIELD:
1581 v->blk_mv_type[s->block_index[0]] = 1;
1582 v->blk_mv_type[s->block_index[1]] = 1;
1583 v->blk_mv_type[s->block_index[2]] = 1;
1584 v->blk_mv_type[s->block_index[3]] = 1;
1586 case MV_PMODE_INTFR_1MV:
1587 v->blk_mv_type[s->block_index[0]] = 0;
1588 v->blk_mv_type[s->block_index[1]] = 0;
1589 v->blk_mv_type[s->block_index[2]] = 0;
1590 v->blk_mv_type[s->block_index[3]] = 0;
1593 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1594 for (i = 0; i < 4; i++) {
1595 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1596 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1598 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1600 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1601 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1602 mb_has_coeffs = get_bits1(gb);
1604 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1605 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1607 s->current_picture.qscale_table[mb_pos] = mquant;
1608 /* Set DC scale - y and c use the same (not sure if necessary here) */
1609 s->y_dc_scale = s->y_dc_scale_table[mquant];
1610 s->c_dc_scale = s->c_dc_scale_table[mquant];
1612 for (i = 0; i < 6; i++) {
1613 v->a_avail = v->c_avail = 0;
1614 v->mb_type[0][s->block_index[i]] = 1;
1615 s->dc_val[0][s->block_index[i]] = 0;
1617 val = ((cbp >> (5 - i)) & 1);
1618 if (i == 2 || i == 3 || !s->first_slice_line)
1619 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1620 if (i == 1 || i == 3 || s->mb_x)
1621 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1623 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1624 (i & 4) ? v->codingset2 : v->codingset);
1625 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
1626 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1628 stride_y = s->linesize << fieldtx;
1629 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1631 stride_y = s->uvlinesize;
1634 s->idsp.put_signed_pixels_clamped(s->block[i],
1635 s->dest[dst_idx] + off,
1640 } else { // inter MB
1641 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1643 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1644 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1645 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1647 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1648 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1649 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1652 s->mb_intra = v->is_intra[s->mb_x] = 0;
1653 for (i = 0; i < 6; i++)
1654 v->mb_type[0][s->block_index[i]] = 0;
1655 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1656 /* for all motion vector read MVDATA and motion compensate each block */
1660 for (i = 0; i < 4; i++) {
1662 if (mvbp & (8 >> i))
1663 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1664 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1665 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1667 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1672 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1674 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1675 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1676 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1679 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1681 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1682 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1683 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1684 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1686 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1689 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1691 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1692 ff_vc1_mc_1mv(v, 0);
1695 GET_MQUANT(); // p. 227
1696 s->current_picture.qscale_table[mb_pos] = mquant;
1697 if (!v->ttmbf && cbp)
1698 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1699 for (i = 0; i < 6; i++) {
1700 s->dc_val[0][s->block_index[i]] = 0;
1702 val = ((cbp >> (5 - i)) & 1);
1704 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1706 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1708 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1709 first_block, s->dest[dst_idx] + off,
1710 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1711 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1712 block_cbp |= pat << (i << 2);
1713 if (!v->ttmbf && ttmb < 8)
1720 s->mb_intra = v->is_intra[s->mb_x] = 0;
1721 for (i = 0; i < 6; i++) {
1722 v->mb_type[0][s->block_index[i]] = 0;
1723 s->dc_val[0][s->block_index[i]] = 0;
1725 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1726 s->current_picture.qscale_table[mb_pos] = 0;
1727 v->blk_mv_type[s->block_index[0]] = 0;
1728 v->blk_mv_type[s->block_index[1]] = 0;
1729 v->blk_mv_type[s->block_index[2]] = 0;
1730 v->blk_mv_type[s->block_index[3]] = 0;
1731 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1732 ff_vc1_mc_1mv(v, 0);
1734 if (s->mb_x == s->mb_width - 1)
1735 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
1739 static int vc1_decode_p_mb_intfi(VC1Context *v)
1741 MpegEncContext *s = &v->s;
1742 GetBitContext *gb = &s->gb;
1744 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1745 int cbp = 0; /* cbp decoding stuff */
1746 int mqdiff, mquant; /* MB quantization */
1747 int ttmb = v->ttfrm; /* MB Transform type */
1749 int mb_has_coeffs = 1; /* last_flag */
1750 int dmv_x, dmv_y; /* Differential MV components */
1751 int val; /* temp values */
1752 int first_block = 1;
1755 int block_cbp = 0, pat, block_tt = 0;
1758 mquant = v->pq; /* Lossy initialization */
1760 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1761 if (idx_mbmode <= 1) { // intra MB
1762 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1764 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1765 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1766 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1768 s->current_picture.qscale_table[mb_pos] = mquant;
1769 /* Set DC scale - y and c use the same (not sure if necessary here) */
1770 s->y_dc_scale = s->y_dc_scale_table[mquant];
1771 s->c_dc_scale = s->c_dc_scale_table[mquant];
1772 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1773 mb_has_coeffs = idx_mbmode & 1;
1775 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1777 for (i = 0; i < 6; i++) {
1778 v->a_avail = v->c_avail = 0;
1779 v->mb_type[0][s->block_index[i]] = 1;
1780 s->dc_val[0][s->block_index[i]] = 0;
1782 val = ((cbp >> (5 - i)) & 1);
1783 if (i == 2 || i == 3 || !s->first_slice_line)
1784 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1785 if (i == 1 || i == 3 || s->mb_x)
1786 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1788 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1789 (i & 4) ? v->codingset2 : v->codingset);
1790 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1792 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1793 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1794 s->idsp.put_signed_pixels_clamped(s->block[i],
1795 s->dest[dst_idx] + off,
1796 (i & 4) ? s->uvlinesize
1798 // TODO: loop filter
1801 s->mb_intra = v->is_intra[s->mb_x] = 0;
1802 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1803 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
1804 if (idx_mbmode <= 5) { // 1-MV
1805 dmv_x = dmv_y = pred_flag = 0;
1806 if (idx_mbmode & 1) {
1807 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1809 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1810 ff_vc1_mc_1mv(v, 0);
1811 mb_has_coeffs = !(idx_mbmode & 2);
1813 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1814 for (i = 0; i < 4; i++) {
1815 dmv_x = dmv_y = pred_flag = 0;
1816 if (v->fourmvbp & (8 >> i))
1817 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1818 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1819 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1821 ff_vc1_mc_4mv_chroma(v, 0);
1822 mb_has_coeffs = idx_mbmode & 1;
1825 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1829 s->current_picture.qscale_table[mb_pos] = mquant;
1830 if (!v->ttmbf && cbp) {
1831 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1834 for (i = 0; i < 6; i++) {
1835 s->dc_val[0][s->block_index[i]] = 0;
1837 val = ((cbp >> (5 - i)) & 1);
1838 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1840 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1841 first_block, s->dest[dst_idx] + off,
1842 (i & 4) ? s->uvlinesize : s->linesize,
1843 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1845 block_cbp |= pat << (i << 2);
1846 if (!v->ttmbf && ttmb < 8) ttmb = -1;
1851 if (s->mb_x == s->mb_width - 1)
1852 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1856 /** Decode one B-frame MB (in Main profile)
1858 static void vc1_decode_b_mb(VC1Context *v)
1860 MpegEncContext *s = &v->s;
1861 GetBitContext *gb = &s->gb;
1863 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1864 int cbp = 0; /* cbp decoding stuff */
1865 int mqdiff, mquant; /* MB quantization */
1866 int ttmb = v->ttfrm; /* MB Transform type */
1867 int mb_has_coeffs = 0; /* last_flag */
1868 int index, index1; /* LUT indexes */
1869 int val, sign; /* temp values */
1870 int first_block = 1;
1872 int skipped, direct;
1873 int dmv_x[2], dmv_y[2];
1874 int bmvtype = BMV_TYPE_BACKWARD;
1876 mquant = v->pq; /* lossy initialization */
1880 direct = get_bits1(gb);
1882 direct = v->direct_mb_plane[mb_pos];
1884 skipped = get_bits1(gb);
1886 skipped = v->s.mbskip_table[mb_pos];
1888 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1889 for (i = 0; i < 6; i++) {
1890 v->mb_type[0][s->block_index[i]] = 0;
1891 s->dc_val[0][s->block_index[i]] = 0;
1893 s->current_picture.qscale_table[mb_pos] = 0;
1897 GET_MVDATA(dmv_x[0], dmv_y[0]);
1898 dmv_x[1] = dmv_x[0];
1899 dmv_y[1] = dmv_y[0];
1901 if (skipped || !s->mb_intra) {
1902 bmvtype = decode012(gb);
1905 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1908 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1911 bmvtype = BMV_TYPE_INTERPOLATED;
1912 dmv_x[0] = dmv_y[0] = 0;
1916 for (i = 0; i < 6; i++)
1917 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1921 bmvtype = BMV_TYPE_INTERPOLATED;
1922 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1923 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1927 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1930 s->current_picture.qscale_table[mb_pos] = mquant;
1932 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1933 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1934 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1935 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1937 if (!mb_has_coeffs && !s->mb_intra) {
1938 /* no coded blocks - effectively skipped */
1939 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1940 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1943 if (s->mb_intra && !mb_has_coeffs) {
1945 s->current_picture.qscale_table[mb_pos] = mquant;
1946 s->ac_pred = get_bits1(gb);
1948 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1950 if (bmvtype == BMV_TYPE_INTERPOLATED) {
1951 GET_MVDATA(dmv_x[0], dmv_y[0]);
1952 if (!mb_has_coeffs) {
1953 /* interpolated skipped block */
1954 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1955 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1959 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1961 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1964 s->ac_pred = get_bits1(gb);
1965 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1967 s->current_picture.qscale_table[mb_pos] = mquant;
1968 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1969 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1973 for (i = 0; i < 6; i++) {
1974 s->dc_val[0][s->block_index[i]] = 0;
1976 val = ((cbp >> (5 - i)) & 1);
1977 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1978 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1980 /* check if prediction blocks A and C are available */
1981 v->a_avail = v->c_avail = 0;
1982 if (i == 2 || i == 3 || !s->first_slice_line)
1983 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1984 if (i == 1 || i == 3 || s->mb_x)
1985 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1987 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1988 (i & 4) ? v->codingset2 : v->codingset);
1989 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1991 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1993 for (j = 0; j < 64; j++)
1994 s->block[i][j] <<= 1;
1995 s->idsp.put_signed_pixels_clamped(s->block[i],
1996 s->dest[dst_idx] + off,
1997 i & 4 ? s->uvlinesize
2000 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2001 first_block, s->dest[dst_idx] + off,
2002 (i & 4) ? s->uvlinesize : s->linesize,
2003 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2004 if (!v->ttmbf && ttmb < 8)
2011 /** Decode one B-frame MB (in interlaced field B picture)
2013 static void vc1_decode_b_mb_intfi(VC1Context *v)
2015 MpegEncContext *s = &v->s;
2016 GetBitContext *gb = &s->gb;
2018 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2019 int cbp = 0; /* cbp decoding stuff */
2020 int mqdiff, mquant; /* MB quantization */
2021 int ttmb = v->ttfrm; /* MB Transform type */
2022 int mb_has_coeffs = 0; /* last_flag */
2023 int val; /* temp value */
2024 int first_block = 1;
2027 int dmv_x[2], dmv_y[2], pred_flag[2];
2028 int bmvtype = BMV_TYPE_BACKWARD;
2031 mquant = v->pq; /* Lossy initialization */
2034 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2035 if (idx_mbmode <= 1) { // intra MB
2036 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2038 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2039 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2040 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2042 s->current_picture.qscale_table[mb_pos] = mquant;
2043 /* Set DC scale - y and c use the same (not sure if necessary here) */
2044 s->y_dc_scale = s->y_dc_scale_table[mquant];
2045 s->c_dc_scale = s->c_dc_scale_table[mquant];
2046 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2047 mb_has_coeffs = idx_mbmode & 1;
2049 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2051 for (i = 0; i < 6; i++) {
2052 v->a_avail = v->c_avail = 0;
2053 v->mb_type[0][s->block_index[i]] = 1;
2054 s->dc_val[0][s->block_index[i]] = 0;
2056 val = ((cbp >> (5 - i)) & 1);
2057 if (i == 2 || i == 3 || !s->first_slice_line)
2058 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2059 if (i == 1 || i == 3 || s->mb_x)
2060 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2062 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2063 (i & 4) ? v->codingset2 : v->codingset);
2064 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2066 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2068 for (j = 0; j < 64; j++)
2069 s->block[i][j] <<= 1;
2070 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2071 s->idsp.put_signed_pixels_clamped(s->block[i],
2072 s->dest[dst_idx] + off,
2073 (i & 4) ? s->uvlinesize
2075 // TODO: yet to perform loop filter
2078 s->mb_intra = v->is_intra[s->mb_x] = 0;
2079 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2080 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2082 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2084 fwd = v->forward_mb_plane[mb_pos];
2085 if (idx_mbmode <= 5) { // 1-MV
2087 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2088 pred_flag[0] = pred_flag[1] = 0;
2090 bmvtype = BMV_TYPE_FORWARD;
2092 bmvtype = decode012(gb);
2095 bmvtype = BMV_TYPE_BACKWARD;
2098 bmvtype = BMV_TYPE_DIRECT;
2101 bmvtype = BMV_TYPE_INTERPOLATED;
2102 interpmvp = get_bits1(gb);
2105 v->bmvtype = bmvtype;
2106 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2107 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2110 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2112 if (bmvtype == BMV_TYPE_DIRECT) {
2113 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2114 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2115 if (!s->next_picture_ptr->field_picture) {
2116 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2120 ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2121 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2122 mb_has_coeffs = !(idx_mbmode & 2);
2125 bmvtype = BMV_TYPE_FORWARD;
2126 v->bmvtype = bmvtype;
2127 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2128 for (i = 0; i < 4; i++) {
2129 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2130 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2131 if (v->fourmvbp & (8 >> i)) {
2132 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2133 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2134 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2136 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2137 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2139 ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2140 mb_has_coeffs = idx_mbmode & 1;
2143 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2147 s->current_picture.qscale_table[mb_pos] = mquant;
2148 if (!v->ttmbf && cbp) {
2149 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2152 for (i = 0; i < 6; i++) {
2153 s->dc_val[0][s->block_index[i]] = 0;
2155 val = ((cbp >> (5 - i)) & 1);
2156 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2158 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2159 first_block, s->dest[dst_idx] + off,
2160 (i & 4) ? s->uvlinesize : s->linesize,
2161 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2162 if (!v->ttmbf && ttmb < 8)
2170 /** Decode one B-frame MB (in interlaced frame B picture)
2172 static int vc1_decode_b_mb_intfr(VC1Context *v)
2174 MpegEncContext *s = &v->s;
2175 GetBitContext *gb = &s->gb;
2177 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2178 int cbp = 0; /* cbp decoding stuff */
2179 int mqdiff, mquant; /* MB quantization */
2180 int ttmb = v->ttfrm; /* MB Transform type */
2181 int mvsw = 0; /* motion vector switch */
2182 int mb_has_coeffs = 1; /* last_flag */
2183 int dmv_x, dmv_y; /* Differential MV components */
2184 int val; /* temp value */
2185 int first_block = 1;
2187 int skipped, direct, twomv = 0;
2188 int block_cbp = 0, pat, block_tt = 0;
2189 int idx_mbmode = 0, mvbp;
2190 int stride_y, fieldtx;
2191 int bmvtype = BMV_TYPE_BACKWARD;
2194 mquant = v->pq; /* Lossy initialization */
2197 skipped = get_bits1(gb);
2199 skipped = v->s.mbskip_table[mb_pos];
2202 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2203 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2205 v->blk_mv_type[s->block_index[0]] = 1;
2206 v->blk_mv_type[s->block_index[1]] = 1;
2207 v->blk_mv_type[s->block_index[2]] = 1;
2208 v->blk_mv_type[s->block_index[3]] = 1;
2210 v->blk_mv_type[s->block_index[0]] = 0;
2211 v->blk_mv_type[s->block_index[1]] = 0;
2212 v->blk_mv_type[s->block_index[2]] = 0;
2213 v->blk_mv_type[s->block_index[3]] = 0;
2218 direct = get_bits1(gb);
2220 direct = v->direct_mb_plane[mb_pos];
2223 if (s->next_picture_ptr->field_picture)
2224 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2225 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);
2226 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);
2227 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);
2228 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);
2231 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);
2232 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);
2233 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);
2234 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);
2236 for (i = 1; i < 4; i += 2) {
2237 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2238 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2239 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2240 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2243 for (i = 1; i < 4; i++) {
2244 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2245 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2246 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2247 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2252 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2253 for (i = 0; i < 4; i++) {
2254 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2255 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2256 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2257 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2259 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2261 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2262 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2263 mb_has_coeffs = get_bits1(gb);
2265 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2266 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2268 s->current_picture.qscale_table[mb_pos] = mquant;
2269 /* Set DC scale - y and c use the same (not sure if necessary here) */
2270 s->y_dc_scale = s->y_dc_scale_table[mquant];
2271 s->c_dc_scale = s->c_dc_scale_table[mquant];
2273 for (i = 0; i < 6; i++) {
2274 v->a_avail = v->c_avail = 0;
2275 v->mb_type[0][s->block_index[i]] = 1;
2276 s->dc_val[0][s->block_index[i]] = 0;
2278 val = ((cbp >> (5 - i)) & 1);
2279 if (i == 2 || i == 3 || !s->first_slice_line)
2280 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2281 if (i == 1 || i == 3 || s->mb_x)
2282 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2284 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2285 (i & 4) ? v->codingset2 : v->codingset);
2286 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
2288 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2290 stride_y = s->linesize << fieldtx;
2291 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2293 stride_y = s->uvlinesize;
2296 s->idsp.put_signed_pixels_clamped(s->block[i],
2297 s->dest[dst_idx] + off,
2301 s->mb_intra = v->is_intra[s->mb_x] = 0;
2303 if (skipped || !s->mb_intra) {
2304 bmvtype = decode012(gb);
2307 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2310 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2313 bmvtype = BMV_TYPE_INTERPOLATED;
2317 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2318 mvsw = get_bits1(gb);
2321 if (!skipped) { // inter MB
2322 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2324 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2326 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2327 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2328 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2329 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2333 for (i = 0; i < 6; i++)
2334 v->mb_type[0][s->block_index[i]] = 0;
2335 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2336 /* for all motion vector read MVDATA and motion compensate each block */
2340 for (i = 0; i < 4; i++) {
2341 ff_vc1_mc_4mv_luma(v, i, 0, 0);
2342 ff_vc1_mc_4mv_luma(v, i, 1, 1);
2344 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2345 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2347 ff_vc1_mc_1mv(v, 0);
2348 ff_vc1_interp_mc(v);
2350 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2352 for (i = 0; i < 4; i++) {
2355 val = ((mvbp >> (3 - i)) & 1);
2357 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2359 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2360 ff_vc1_mc_4mv_luma(v, j, dir, dir);
2361 ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2364 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2365 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2366 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2370 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2372 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2373 ff_vc1_mc_1mv(v, 0);
2377 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2379 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2380 ff_vc1_interp_mc(v);
2382 dir = bmvtype == BMV_TYPE_BACKWARD;
2389 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2390 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2394 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2395 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2398 for (i = 0; i < 2; i++) {
2399 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];
2400 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];
2401 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];
2402 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];
2405 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2406 ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2409 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2410 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2411 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2412 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2413 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2415 dir = bmvtype == BMV_TYPE_BACKWARD;
2417 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2420 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2422 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2423 v->blk_mv_type[s->block_index[0]] = 1;
2424 v->blk_mv_type[s->block_index[1]] = 1;
2425 v->blk_mv_type[s->block_index[2]] = 1;
2426 v->blk_mv_type[s->block_index[3]] = 1;
2427 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2428 for (i = 0; i < 2; i++) {
2429 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];
2430 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];
2432 ff_vc1_mc_1mv(v, dir);
2436 GET_MQUANT(); // p. 227
2437 s->current_picture.qscale_table[mb_pos] = mquant;
2438 if (!v->ttmbf && cbp)
2439 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2440 for (i = 0; i < 6; i++) {
2441 s->dc_val[0][s->block_index[i]] = 0;
2443 val = ((cbp >> (5 - i)) & 1);
2445 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2447 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2449 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2450 first_block, s->dest[dst_idx] + off,
2451 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2452 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
2453 block_cbp |= pat << (i << 2);
2454 if (!v->ttmbf && ttmb < 8)
2462 for (i = 0; i < 6; i++) {
2463 v->mb_type[0][s->block_index[i]] = 0;
2464 s->dc_val[0][s->block_index[i]] = 0;
2466 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2467 s->current_picture.qscale_table[mb_pos] = 0;
2468 v->blk_mv_type[s->block_index[0]] = 0;
2469 v->blk_mv_type[s->block_index[1]] = 0;
2470 v->blk_mv_type[s->block_index[2]] = 0;
2471 v->blk_mv_type[s->block_index[3]] = 0;
2474 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2475 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2476 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2478 dir = bmvtype == BMV_TYPE_BACKWARD;
2479 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2484 for (i = 0; i < 2; i++) {
2485 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];
2486 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];
2487 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];
2488 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];
2491 v->blk_mv_type[s->block_index[0]] = 1;
2492 v->blk_mv_type[s->block_index[1]] = 1;
2493 v->blk_mv_type[s->block_index[2]] = 1;
2494 v->blk_mv_type[s->block_index[3]] = 1;
2495 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2496 for (i = 0; i < 2; i++) {
2497 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];
2498 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];
2504 ff_vc1_mc_1mv(v, dir);
2505 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2506 ff_vc1_interp_mc(v);
2510 if (s->mb_x == s->mb_width - 1)
2511 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2512 v->cbp[s->mb_x] = block_cbp;
2513 v->ttblk[s->mb_x] = block_tt;
2517 /** Decode blocks of I-frame
2519 static void vc1_decode_i_blocks(VC1Context *v)
2522 MpegEncContext *s = &v->s;
2527 /* select codingmode used for VLC tables selection */
2528 switch (v->y_ac_table_index) {
2530 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2533 v->codingset = CS_HIGH_MOT_INTRA;
2536 v->codingset = CS_MID_RATE_INTRA;
2540 switch (v->c_ac_table_index) {
2542 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2545 v->codingset2 = CS_HIGH_MOT_INTER;
2548 v->codingset2 = CS_MID_RATE_INTER;
2552 /* Set DC scale - y and c use the same */
2553 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2554 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2557 s->mb_x = s->mb_y = 0;
2559 s->first_slice_line = 1;
2560 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2562 init_block_index(v);
2563 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2565 ff_update_block_index(s);
2566 dst[0] = s->dest[0];
2567 dst[1] = dst[0] + 8;
2568 dst[2] = s->dest[0] + s->linesize * 8;
2569 dst[3] = dst[2] + 8;
2570 dst[4] = s->dest[1];
2571 dst[5] = s->dest[2];
2572 s->bdsp.clear_blocks(s->block[0]);
2573 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2574 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2575 s->current_picture.qscale_table[mb_pos] = v->pq;
2576 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2577 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2579 // do actual MB decoding and displaying
2580 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2581 v->s.ac_pred = get_bits1(&v->s.gb);
2583 for (k = 0; k < 6; k++) {
2584 val = ((cbp >> (5 - k)) & 1);
2587 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2591 cbp |= val << (5 - k);
2593 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2595 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2597 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2598 if (v->pq >= 9 && v->overlap) {
2600 for (j = 0; j < 64; j++)
2601 s->block[k][j] <<= 1;
2602 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2603 k & 4 ? s->uvlinesize
2607 for (j = 0; j < 64; j++)
2608 s->block[k][j] = (s->block[k][j] - 64) << 1;
2609 s->idsp.put_pixels_clamped(s->block[k], dst[k],
2610 k & 4 ? s->uvlinesize
2615 if (v->pq >= 9 && v->overlap) {
2617 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2618 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2619 if (!(s->flags & CODEC_FLAG_GRAY)) {
2620 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2621 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2624 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2625 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2626 if (!s->first_slice_line) {
2627 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2628 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2629 if (!(s->flags & CODEC_FLAG_GRAY)) {
2630 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2631 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2634 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2635 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2637 if (v->s.loop_filter)
2638 ff_vc1_loop_filter_iblk(v, v->pq);
2640 if (get_bits_count(&s->gb) > v->bits) {
2641 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2642 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2643 get_bits_count(&s->gb), v->bits);
2647 if (!v->s.loop_filter)
2648 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2650 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2652 s->first_slice_line = 0;
2654 if (v->s.loop_filter)
2655 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2657 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2658 * profile, these only differ are when decoding MSS2 rectangles. */
2659 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2662 /** Decode blocks of I-frame for advanced profile
2664 static void vc1_decode_i_blocks_adv(VC1Context *v)
2667 MpegEncContext *s = &v->s;
2673 GetBitContext *gb = &s->gb;
2675 /* select codingmode used for VLC tables selection */
2676 switch (v->y_ac_table_index) {
2678 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2681 v->codingset = CS_HIGH_MOT_INTRA;
2684 v->codingset = CS_MID_RATE_INTRA;
2688 switch (v->c_ac_table_index) {
2690 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2693 v->codingset2 = CS_HIGH_MOT_INTER;
2696 v->codingset2 = CS_MID_RATE_INTER;
2701 s->mb_x = s->mb_y = 0;
2703 s->first_slice_line = 1;
2704 s->mb_y = s->start_mb_y;
2705 if (s->start_mb_y) {
2707 init_block_index(v);
2708 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2709 (1 + s->b8_stride) * sizeof(*s->coded_block));
2711 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2713 init_block_index(v);
2714 for (;s->mb_x < s->mb_width; s->mb_x++) {
2715 int16_t (*block)[64] = v->block[v->cur_blk_idx];
2716 ff_update_block_index(s);
2717 s->bdsp.clear_blocks(block[0]);
2718 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2719 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2720 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2721 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2723 // do actual MB decoding and displaying
2724 if (v->fieldtx_is_raw)
2725 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2726 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2727 if ( v->acpred_is_raw)
2728 v->s.ac_pred = get_bits1(&v->s.gb);
2730 v->s.ac_pred = v->acpred_plane[mb_pos];
2732 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2733 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2737 s->current_picture.qscale_table[mb_pos] = mquant;
2738 /* Set DC scale - y and c use the same */
2739 s->y_dc_scale = s->y_dc_scale_table[mquant];
2740 s->c_dc_scale = s->c_dc_scale_table[mquant];
2742 for (k = 0; k < 6; k++) {
2743 val = ((cbp >> (5 - k)) & 1);
2746 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2750 cbp |= val << (5 - k);
2752 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2753 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2755 vc1_decode_i_block_adv(v, block[k], k, val,
2756 (k < 4) ? v->codingset : v->codingset2, mquant);
2758 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2760 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2763 ff_vc1_smooth_overlap_filter_iblk(v);
2764 vc1_put_signed_blocks_clamped(v);
2765 if (v->s.loop_filter)
2766 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2768 if (get_bits_count(&s->gb) > v->bits) {
2769 // TODO: may need modification to handle slice coding
2770 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2771 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2772 get_bits_count(&s->gb), v->bits);
2776 if (!v->s.loop_filter)
2777 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2779 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2780 s->first_slice_line = 0;
2783 /* raw bottom MB row */
2785 init_block_index(v);
2787 for (;s->mb_x < s->mb_width; s->mb_x++) {
2788 ff_update_block_index(s);
2789 vc1_put_signed_blocks_clamped(v);
2790 if (v->s.loop_filter)
2791 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2793 if (v->s.loop_filter)
2794 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
2795 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2796 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2799 static void vc1_decode_p_blocks(VC1Context *v)
2801 MpegEncContext *s = &v->s;
2802 int apply_loop_filter;
2804 /* select codingmode used for VLC tables selection */
2805 switch (v->c_ac_table_index) {
2807 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2810 v->codingset = CS_HIGH_MOT_INTRA;
2813 v->codingset = CS_MID_RATE_INTRA;
2817 switch (v->c_ac_table_index) {
2819 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2822 v->codingset2 = CS_HIGH_MOT_INTER;
2825 v->codingset2 = CS_MID_RATE_INTER;
2829 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2830 v->fcm == PROGRESSIVE;
2831 s->first_slice_line = 1;
2832 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2833 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2835 init_block_index(v);
2836 for (; s->mb_x < s->mb_width; s->mb_x++) {
2837 ff_update_block_index(s);
2839 if (v->fcm == ILACE_FIELD)
2840 vc1_decode_p_mb_intfi(v);
2841 else if (v->fcm == ILACE_FRAME)
2842 vc1_decode_p_mb_intfr(v);
2843 else vc1_decode_p_mb(v);
2844 if (s->mb_y != s->start_mb_y && apply_loop_filter)
2845 ff_vc1_apply_p_loop_filter(v);
2846 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2847 // TODO: may need modification to handle slice coding
2848 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2849 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2850 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2854 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
2855 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
2856 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2857 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
2858 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2859 s->first_slice_line = 0;
2861 if (apply_loop_filter) {
2863 init_block_index(v);
2864 for (; s->mb_x < s->mb_width; s->mb_x++) {
2865 ff_update_block_index(s);
2866 ff_vc1_apply_p_loop_filter(v);
2869 if (s->end_mb_y >= s->start_mb_y)
2870 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2871 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2872 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2875 static void vc1_decode_b_blocks(VC1Context *v)
2877 MpegEncContext *s = &v->s;
2879 /* select codingmode used for VLC tables selection */
2880 switch (v->c_ac_table_index) {
2882 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2885 v->codingset = CS_HIGH_MOT_INTRA;
2888 v->codingset = CS_MID_RATE_INTRA;
2892 switch (v->c_ac_table_index) {
2894 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2897 v->codingset2 = CS_HIGH_MOT_INTER;
2900 v->codingset2 = CS_MID_RATE_INTER;
2904 s->first_slice_line = 1;
2905 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2907 init_block_index(v);
2908 for (; s->mb_x < s->mb_width; s->mb_x++) {
2909 ff_update_block_index(s);
2911 if (v->fcm == ILACE_FIELD)
2912 vc1_decode_b_mb_intfi(v);
2913 else if (v->fcm == ILACE_FRAME)
2914 vc1_decode_b_mb_intfr(v);
2917 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2918 // TODO: may need modification to handle slice coding
2919 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2920 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2921 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2924 if (v->s.loop_filter)
2925 ff_vc1_loop_filter_iblk(v, v->pq);
2927 if (!v->s.loop_filter)
2928 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2930 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2931 s->first_slice_line = 0;
2933 if (v->s.loop_filter)
2934 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2935 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2936 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2939 static void vc1_decode_skip_blocks(VC1Context *v)
2941 MpegEncContext *s = &v->s;
2943 if (!v->s.last_picture.f->data[0])
2946 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2947 s->first_slice_line = 1;
2948 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2950 init_block_index(v);
2951 ff_update_block_index(s);
2952 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2953 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2954 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2955 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2956 s->first_slice_line = 0;
2958 s->pict_type = AV_PICTURE_TYPE_P;
2961 void ff_vc1_decode_blocks(VC1Context *v)
2964 v->s.esc3_level_length = 0;
2966 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
2969 v->left_blk_idx = -1;
2970 v->topleft_blk_idx = 1;
2972 switch (v->s.pict_type) {
2973 case AV_PICTURE_TYPE_I:
2974 if (v->profile == PROFILE_ADVANCED)
2975 vc1_decode_i_blocks_adv(v);
2977 vc1_decode_i_blocks(v);
2979 case AV_PICTURE_TYPE_P:
2980 if (v->p_frame_skipped)
2981 vc1_decode_skip_blocks(v);
2983 vc1_decode_p_blocks(v);
2985 case AV_PICTURE_TYPE_B:
2987 if (v->profile == PROFILE_ADVANCED)
2988 vc1_decode_i_blocks_adv(v);
2990 vc1_decode_i_blocks(v);
2992 vc1_decode_b_blocks(v);