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 if (dcdiff == 119 /* ESC index value */) {
576 if (v->pq == 1) dcdiff = get_bits(gb, 10);
577 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
578 else dcdiff = get_bits(gb, 8);
581 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
583 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
590 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
593 /* Store the quantized DC coeff, used for prediction */
595 block[0] = dcdiff * s->y_dc_scale;
597 block[0] = dcdiff * s->c_dc_scale;
608 int last = 0, skip, value;
609 const uint8_t *zz_table;
613 scale = v->pq * 2 + v->halfpq;
617 zz_table = v->zz_8x8[2];
619 zz_table = v->zz_8x8[3];
621 zz_table = v->zz_8x8[1];
623 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
625 if (dc_pred_dir) // left
628 ac_val -= 16 * s->block_wrap[n];
631 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
635 block[zz_table[i++]] = value;
638 /* apply AC prediction if needed */
640 if (dc_pred_dir) { // left
641 for (k = 1; k < 8; k++)
642 block[k << v->left_blk_sh] += ac_val[k];
644 for (k = 1; k < 8; k++)
645 block[k << v->top_blk_sh] += ac_val[k + 8];
648 /* save AC coeffs for further prediction */
649 for (k = 1; k < 8; k++) {
650 ac_val2[k] = block[k << v->left_blk_sh];
651 ac_val2[k + 8] = block[k << v->top_blk_sh];
654 /* scale AC coeffs */
655 for (k = 1; k < 64; k++)
659 block[k] += (block[k] < 0) ? -v->pq : v->pq;
662 if (s->ac_pred) i = 63;
668 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
672 scale = v->pq * 2 + v->halfpq;
673 memset(ac_val2, 0, 16 * 2);
674 if (dc_pred_dir) { // left
677 memcpy(ac_val2, ac_val, 8 * 2);
679 ac_val -= 16 * s->block_wrap[n];
681 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
684 /* apply AC prediction if needed */
686 if (dc_pred_dir) { //left
687 for (k = 1; k < 8; k++) {
688 block[k << v->left_blk_sh] = ac_val[k] * scale;
689 if (!v->pquantizer && block[k << v->left_blk_sh])
690 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
693 for (k = 1; k < 8; k++) {
694 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
695 if (!v->pquantizer && block[k << v->top_blk_sh])
696 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
702 s->block_last_index[n] = i;
707 /** Decode intra block in intra frames - should be faster than decode_intra_block
708 * @param v VC1Context
709 * @param block block to decode
710 * @param[in] n subblock number
711 * @param coded are AC coeffs present or not
712 * @param codingset set of VLC to decode data
713 * @param mquant quantizer value for this macroblock
715 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
716 int coded, int codingset, int mquant)
718 GetBitContext *gb = &v->s.gb;
719 MpegEncContext *s = &v->s;
720 int dc_pred_dir = 0; /* Direction of the DC prediction used */
722 int16_t *dc_val = NULL;
723 int16_t *ac_val, *ac_val2;
725 int a_avail = v->a_avail, c_avail = v->c_avail;
726 int use_pred = s->ac_pred;
729 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
731 /* Get DC differential */
733 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
735 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
738 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
742 if (dcdiff == 119 /* ESC index value */) {
744 if (mquant == 1) dcdiff = get_bits(gb, 10);
745 else if (mquant == 2) dcdiff = get_bits(gb, 9);
746 else dcdiff = get_bits(gb, 8);
749 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
750 else if (mquant == 2)
751 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
758 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
761 /* Store the quantized DC coeff, used for prediction */
763 block[0] = dcdiff * s->y_dc_scale;
765 block[0] = dcdiff * s->c_dc_scale;
771 /* check if AC is needed at all */
772 if (!a_avail && !c_avail)
774 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
777 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
779 if (dc_pred_dir) // left
782 ac_val -= 16 * s->block_wrap[n];
784 q1 = s->current_picture.qscale_table[mb_pos];
785 if ( dc_pred_dir && c_avail && mb_pos)
786 q2 = s->current_picture.qscale_table[mb_pos - 1];
787 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
788 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
789 if ( dc_pred_dir && n == 1)
791 if (!dc_pred_dir && n == 2)
797 int last = 0, skip, value;
798 const uint8_t *zz_table;
802 if (!use_pred && v->fcm == ILACE_FRAME) {
803 zz_table = v->zzi_8x8;
805 if (!dc_pred_dir) // top
806 zz_table = v->zz_8x8[2];
808 zz_table = v->zz_8x8[3];
811 if (v->fcm != ILACE_FRAME)
812 zz_table = v->zz_8x8[1];
814 zz_table = v->zzi_8x8;
818 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
822 block[zz_table[i++]] = value;
825 /* apply AC prediction if needed */
827 /* scale predictors if needed*/
828 if (q2 && q1 != q2) {
829 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
830 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
833 return AVERROR_INVALIDDATA;
834 if (dc_pred_dir) { // left
835 for (k = 1; k < 8; k++)
836 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
838 for (k = 1; k < 8; k++)
839 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
842 if (dc_pred_dir) { //left
843 for (k = 1; k < 8; k++)
844 block[k << v->left_blk_sh] += ac_val[k];
846 for (k = 1; k < 8; k++)
847 block[k << v->top_blk_sh] += ac_val[k + 8];
851 /* save AC coeffs for further prediction */
852 for (k = 1; k < 8; k++) {
853 ac_val2[k ] = block[k << v->left_blk_sh];
854 ac_val2[k + 8] = block[k << v->top_blk_sh];
857 /* scale AC coeffs */
858 for (k = 1; k < 64; k++)
862 block[k] += (block[k] < 0) ? -mquant : mquant;
865 if (use_pred) i = 63;
866 } else { // no AC coeffs
869 memset(ac_val2, 0, 16 * 2);
870 if (dc_pred_dir) { // left
872 memcpy(ac_val2, ac_val, 8 * 2);
873 if (q2 && q1 != q2) {
874 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
875 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
877 return AVERROR_INVALIDDATA;
878 for (k = 1; k < 8; k++)
879 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
884 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
885 if (q2 && q1 != q2) {
886 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
887 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
889 return AVERROR_INVALIDDATA;
890 for (k = 1; k < 8; k++)
891 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
896 /* apply AC prediction if needed */
898 if (dc_pred_dir) { // left
899 for (k = 1; k < 8; k++) {
900 block[k << v->left_blk_sh] = ac_val2[k] * scale;
901 if (!v->pquantizer && block[k << v->left_blk_sh])
902 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
905 for (k = 1; k < 8; k++) {
906 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
907 if (!v->pquantizer && block[k << v->top_blk_sh])
908 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
914 s->block_last_index[n] = i;
919 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
920 * @param v VC1Context
921 * @param block block to decode
922 * @param[in] n subblock index
923 * @param coded are AC coeffs present or not
924 * @param mquant block quantizer
925 * @param codingset set of VLC to decode data
927 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
928 int coded, int mquant, int codingset)
930 GetBitContext *gb = &v->s.gb;
931 MpegEncContext *s = &v->s;
932 int dc_pred_dir = 0; /* Direction of the DC prediction used */
934 int16_t *dc_val = NULL;
935 int16_t *ac_val, *ac_val2;
937 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
938 int a_avail = v->a_avail, c_avail = v->c_avail;
939 int use_pred = s->ac_pred;
943 s->bdsp.clear_block(block);
945 /* XXX: Guard against dumb values of mquant */
946 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
948 /* Set DC scale - y and c use the same */
949 s->y_dc_scale = s->y_dc_scale_table[mquant];
950 s->c_dc_scale = s->c_dc_scale_table[mquant];
952 /* Get DC differential */
954 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
956 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
959 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
963 if (dcdiff == 119 /* ESC index value */) {
965 if (mquant == 1) dcdiff = get_bits(gb, 10);
966 else if (mquant == 2) dcdiff = get_bits(gb, 9);
967 else dcdiff = get_bits(gb, 8);
970 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
971 else if (mquant == 2)
972 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
979 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
982 /* Store the quantized DC coeff, used for prediction */
985 block[0] = dcdiff * s->y_dc_scale;
987 block[0] = dcdiff * s->c_dc_scale;
993 /* check if AC is needed at all and adjust direction if needed */
994 if (!a_avail) dc_pred_dir = 1;
995 if (!c_avail) dc_pred_dir = 0;
996 if (!a_avail && !c_avail) use_pred = 0;
997 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1000 scale = mquant * 2 + v->halfpq;
1002 if (dc_pred_dir) //left
1005 ac_val -= 16 * s->block_wrap[n];
1007 q1 = s->current_picture.qscale_table[mb_pos];
1008 if (dc_pred_dir && c_avail && mb_pos)
1009 q2 = s->current_picture.qscale_table[mb_pos - 1];
1010 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
1011 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1012 if ( dc_pred_dir && n == 1)
1014 if (!dc_pred_dir && n == 2)
1016 if (n == 3) q2 = q1;
1019 int last = 0, skip, value;
1023 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1027 if (v->fcm == PROGRESSIVE)
1028 block[v->zz_8x8[0][i++]] = value;
1030 if (use_pred && (v->fcm == ILACE_FRAME)) {
1031 if (!dc_pred_dir) // top
1032 block[v->zz_8x8[2][i++]] = value;
1034 block[v->zz_8x8[3][i++]] = value;
1036 block[v->zzi_8x8[i++]] = value;
1041 /* apply AC prediction if needed */
1043 /* scale predictors if needed*/
1044 if (q2 && q1 != q2) {
1045 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1046 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1049 return AVERROR_INVALIDDATA;
1050 if (dc_pred_dir) { // left
1051 for (k = 1; k < 8; k++)
1052 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1054 for (k = 1; k < 8; k++)
1055 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1058 if (dc_pred_dir) { // left
1059 for (k = 1; k < 8; k++)
1060 block[k << v->left_blk_sh] += ac_val[k];
1062 for (k = 1; k < 8; k++)
1063 block[k << v->top_blk_sh] += ac_val[k + 8];
1067 /* save AC coeffs for further prediction */
1068 for (k = 1; k < 8; k++) {
1069 ac_val2[k ] = block[k << v->left_blk_sh];
1070 ac_val2[k + 8] = block[k << v->top_blk_sh];
1073 /* scale AC coeffs */
1074 for (k = 1; k < 64; k++)
1078 block[k] += (block[k] < 0) ? -mquant : mquant;
1081 if (use_pred) i = 63;
1082 } else { // no AC coeffs
1085 memset(ac_val2, 0, 16 * 2);
1086 if (dc_pred_dir) { // left
1088 memcpy(ac_val2, ac_val, 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] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1100 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1101 if (q2 && q1 != q2) {
1102 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1103 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1105 return AVERROR_INVALIDDATA;
1106 for (k = 1; k < 8; k++)
1107 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1112 /* apply AC prediction if needed */
1114 if (dc_pred_dir) { // left
1115 for (k = 1; k < 8; k++) {
1116 block[k << v->left_blk_sh] = ac_val2[k] * scale;
1117 if (!v->pquantizer && block[k << v->left_blk_sh])
1118 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1121 for (k = 1; k < 8; k++) {
1122 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1123 if (!v->pquantizer && block[k << v->top_blk_sh])
1124 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1130 s->block_last_index[n] = i;
1137 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1138 int mquant, int ttmb, int first_block,
1139 uint8_t *dst, int linesize, int skip_block,
1142 MpegEncContext *s = &v->s;
1143 GetBitContext *gb = &s->gb;
1146 int scale, off, idx, last, skip, value;
1147 int ttblk = ttmb & 7;
1150 s->bdsp.clear_block(block);
1153 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)];
1155 if (ttblk == TT_4X4) {
1156 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1158 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1159 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1160 || (!v->res_rtm_flag && !first_block))) {
1161 subblkpat = decode012(gb);
1163 subblkpat ^= 3; // swap decoded pattern bits
1164 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1166 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1169 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1171 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1172 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1173 subblkpat = 2 - (ttblk == TT_8X4_TOP);
1176 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1177 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1186 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1191 idx = v->zz_8x8[0][i++];
1193 idx = v->zzi_8x8[i++];
1194 block[idx] = value * scale;
1196 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1200 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1202 v->vc1dsp.vc1_inv_trans_8x8(block);
1203 s->idsp.add_pixels_clamped(block, dst, linesize);
1208 pat = ~subblkpat & 0xF;
1209 for (j = 0; j < 4; j++) {
1210 last = subblkpat & (1 << (3 - j));
1212 off = (j & 1) * 4 + (j & 2) * 16;
1214 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1219 idx = ff_vc1_simple_progressive_4x4_zz[i++];
1221 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1222 block[idx + off] = value * scale;
1224 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1226 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1228 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1230 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1235 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1236 for (j = 0; j < 2; j++) {
1237 last = subblkpat & (1 << (1 - j));
1241 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1246 idx = v->zz_8x4[i++] + off;
1248 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1249 block[idx] = value * scale;
1251 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1253 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1255 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1257 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1262 pat = ~(subblkpat * 5) & 0xF;
1263 for (j = 0; j < 2; j++) {
1264 last = subblkpat & (1 << (1 - j));
1268 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1273 idx = v->zz_4x8[i++] + off;
1275 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1276 block[idx] = value * scale;
1278 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1280 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1282 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1284 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1290 *ttmb_out |= ttblk << (n * 4);
1294 /** @} */ // Macroblock group
1296 static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 };
1298 /** Decode one P-frame MB
1300 static int vc1_decode_p_mb(VC1Context *v)
1302 MpegEncContext *s = &v->s;
1303 GetBitContext *gb = &s->gb;
1305 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1306 int cbp; /* cbp decoding stuff */
1307 int mqdiff, mquant; /* MB quantization */
1308 int ttmb = v->ttfrm; /* MB Transform type */
1310 int mb_has_coeffs = 1; /* last_flag */
1311 int dmv_x, dmv_y; /* Differential MV components */
1312 int index, index1; /* LUT indexes */
1313 int val, sign; /* temp values */
1314 int first_block = 1;
1316 int skipped, fourmv;
1317 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1319 mquant = v->pq; /* lossy initialization */
1321 if (v->mv_type_is_raw)
1322 fourmv = get_bits1(gb);
1324 fourmv = v->mv_type_mb_plane[mb_pos];
1326 skipped = get_bits1(gb);
1328 skipped = v->s.mbskip_table[mb_pos];
1330 if (!fourmv) { /* 1MV mode */
1332 GET_MVDATA(dmv_x, dmv_y);
1335 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1336 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1338 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1339 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1341 /* FIXME Set DC val for inter block ? */
1342 if (s->mb_intra && !mb_has_coeffs) {
1344 s->ac_pred = get_bits1(gb);
1346 } else if (mb_has_coeffs) {
1348 s->ac_pred = get_bits1(gb);
1349 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1355 s->current_picture.qscale_table[mb_pos] = mquant;
1357 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1358 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1359 VC1_TTMB_VLC_BITS, 2);
1360 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1362 for (i = 0; i < 6; i++) {
1363 s->dc_val[0][s->block_index[i]] = 0;
1365 val = ((cbp >> (5 - i)) & 1);
1366 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1367 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1369 /* check if prediction blocks A and C are available */
1370 v->a_avail = v->c_avail = 0;
1371 if (i == 2 || i == 3 || !s->first_slice_line)
1372 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1373 if (i == 1 || i == 3 || s->mb_x)
1374 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1376 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1377 (i & 4) ? v->codingset2 : v->codingset);
1378 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1380 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1382 for (j = 0; j < 64; j++)
1383 s->block[i][j] <<= 1;
1384 s->idsp.put_signed_pixels_clamped(s->block[i],
1385 s->dest[dst_idx] + off,
1386 i & 4 ? s->uvlinesize
1388 if (v->pq >= 9 && v->overlap) {
1390 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1392 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1394 block_cbp |= 0xF << (i << 2);
1395 block_intra |= 1 << i;
1397 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
1398 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1399 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1400 block_cbp |= pat << (i << 2);
1401 if (!v->ttmbf && ttmb < 8)
1408 for (i = 0; i < 6; i++) {
1409 v->mb_type[0][s->block_index[i]] = 0;
1410 s->dc_val[0][s->block_index[i]] = 0;
1412 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1413 s->current_picture.qscale_table[mb_pos] = 0;
1414 ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1415 ff_vc1_mc_1mv(v, 0);
1417 } else { // 4MV mode
1418 if (!skipped /* unskipped MB */) {
1419 int intra_count = 0, coded_inter = 0;
1420 int is_intra[6], is_coded[6];
1422 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1423 for (i = 0; i < 6; i++) {
1424 val = ((cbp >> (5 - i)) & 1);
1425 s->dc_val[0][s->block_index[i]] = 0;
1432 GET_MVDATA(dmv_x, dmv_y);
1434 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1436 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1437 intra_count += s->mb_intra;
1438 is_intra[i] = s->mb_intra;
1439 is_coded[i] = mb_has_coeffs;
1442 is_intra[i] = (intra_count >= 3);
1446 ff_vc1_mc_4mv_chroma(v, 0);
1447 v->mb_type[0][s->block_index[i]] = is_intra[i];
1449 coded_inter = !is_intra[i] & is_coded[i];
1451 // if there are no coded blocks then don't do anything more
1453 if (!intra_count && !coded_inter)
1456 s->current_picture.qscale_table[mb_pos] = mquant;
1457 /* test if block is intra and has pred */
1460 for (i = 0; i < 6; i++)
1462 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1463 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1469 s->ac_pred = get_bits1(gb);
1473 if (!v->ttmbf && coded_inter)
1474 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1475 for (i = 0; i < 6; i++) {
1477 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1478 s->mb_intra = is_intra[i];
1480 /* check if prediction blocks A and C are available */
1481 v->a_avail = v->c_avail = 0;
1482 if (i == 2 || i == 3 || !s->first_slice_line)
1483 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1484 if (i == 1 || i == 3 || s->mb_x)
1485 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1487 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
1488 (i & 4) ? v->codingset2 : v->codingset);
1489 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1491 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1493 for (j = 0; j < 64; j++)
1494 s->block[i][j] <<= 1;
1495 s->idsp.put_signed_pixels_clamped(s->block[i],
1496 s->dest[dst_idx] + off,
1497 (i & 4) ? s->uvlinesize
1499 if (v->pq >= 9 && v->overlap) {
1501 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1503 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1505 block_cbp |= 0xF << (i << 2);
1506 block_intra |= 1 << i;
1507 } else if (is_coded[i]) {
1508 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1509 first_block, s->dest[dst_idx] + off,
1510 (i & 4) ? s->uvlinesize : s->linesize,
1511 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1513 block_cbp |= pat << (i << 2);
1514 if (!v->ttmbf && ttmb < 8)
1519 } else { // skipped MB
1521 s->current_picture.qscale_table[mb_pos] = 0;
1522 for (i = 0; i < 6; i++) {
1523 v->mb_type[0][s->block_index[i]] = 0;
1524 s->dc_val[0][s->block_index[i]] = 0;
1526 for (i = 0; i < 4; i++) {
1527 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1528 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1530 ff_vc1_mc_4mv_chroma(v, 0);
1531 s->current_picture.qscale_table[mb_pos] = 0;
1535 v->cbp[s->mb_x] = block_cbp;
1536 v->ttblk[s->mb_x] = block_tt;
1537 v->is_intra[s->mb_x] = block_intra;
1542 /* Decode one macroblock in an interlaced frame p picture */
1544 static int vc1_decode_p_mb_intfr(VC1Context *v)
1546 MpegEncContext *s = &v->s;
1547 GetBitContext *gb = &s->gb;
1549 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1550 int cbp = 0; /* cbp decoding stuff */
1551 int mqdiff, mquant; /* MB quantization */
1552 int ttmb = v->ttfrm; /* MB Transform type */
1554 int mb_has_coeffs = 1; /* last_flag */
1555 int dmv_x, dmv_y; /* Differential MV components */
1556 int val; /* temp value */
1557 int first_block = 1;
1559 int skipped, fourmv = 0, twomv = 0;
1560 int block_cbp = 0, pat, block_tt = 0;
1561 int idx_mbmode = 0, mvbp;
1562 int stride_y, fieldtx;
1564 mquant = v->pq; /* Lossy initialization */
1567 skipped = get_bits1(gb);
1569 skipped = v->s.mbskip_table[mb_pos];
1571 if (v->fourmvswitch)
1572 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1574 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1575 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1576 /* store the motion vector type in a flag (useful later) */
1577 case MV_PMODE_INTFR_4MV:
1579 v->blk_mv_type[s->block_index[0]] = 0;
1580 v->blk_mv_type[s->block_index[1]] = 0;
1581 v->blk_mv_type[s->block_index[2]] = 0;
1582 v->blk_mv_type[s->block_index[3]] = 0;
1584 case MV_PMODE_INTFR_4MV_FIELD:
1586 v->blk_mv_type[s->block_index[0]] = 1;
1587 v->blk_mv_type[s->block_index[1]] = 1;
1588 v->blk_mv_type[s->block_index[2]] = 1;
1589 v->blk_mv_type[s->block_index[3]] = 1;
1591 case MV_PMODE_INTFR_2MV_FIELD:
1593 v->blk_mv_type[s->block_index[0]] = 1;
1594 v->blk_mv_type[s->block_index[1]] = 1;
1595 v->blk_mv_type[s->block_index[2]] = 1;
1596 v->blk_mv_type[s->block_index[3]] = 1;
1598 case MV_PMODE_INTFR_1MV:
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;
1605 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1606 for (i = 0; i < 4; i++) {
1607 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1608 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1610 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1612 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1613 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1614 mb_has_coeffs = get_bits1(gb);
1616 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1617 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1619 s->current_picture.qscale_table[mb_pos] = mquant;
1620 /* Set DC scale - y and c use the same (not sure if necessary here) */
1621 s->y_dc_scale = s->y_dc_scale_table[mquant];
1622 s->c_dc_scale = s->c_dc_scale_table[mquant];
1624 for (i = 0; i < 6; i++) {
1625 v->a_avail = v->c_avail = 0;
1626 v->mb_type[0][s->block_index[i]] = 1;
1627 s->dc_val[0][s->block_index[i]] = 0;
1629 val = ((cbp >> (5 - i)) & 1);
1630 if (i == 2 || i == 3 || !s->first_slice_line)
1631 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1632 if (i == 1 || i == 3 || s->mb_x)
1633 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1635 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1636 (i & 4) ? v->codingset2 : v->codingset);
1637 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
1638 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1640 stride_y = s->linesize << fieldtx;
1641 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1643 stride_y = s->uvlinesize;
1646 s->idsp.put_signed_pixels_clamped(s->block[i],
1647 s->dest[dst_idx] + off,
1652 } else { // inter MB
1653 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1655 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1656 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1657 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1659 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1660 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1661 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1664 s->mb_intra = v->is_intra[s->mb_x] = 0;
1665 for (i = 0; i < 6; i++)
1666 v->mb_type[0][s->block_index[i]] = 0;
1667 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1668 /* for all motion vector read MVDATA and motion compensate each block */
1672 for (i = 0; i < 4; i++) {
1674 if (mvbp & (8 >> i))
1675 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1676 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1677 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1679 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1684 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1686 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1687 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1688 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1691 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1693 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1694 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1695 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1696 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1698 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1701 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1703 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1704 ff_vc1_mc_1mv(v, 0);
1707 GET_MQUANT(); // p. 227
1708 s->current_picture.qscale_table[mb_pos] = mquant;
1709 if (!v->ttmbf && cbp)
1710 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1711 for (i = 0; i < 6; i++) {
1712 s->dc_val[0][s->block_index[i]] = 0;
1714 val = ((cbp >> (5 - i)) & 1);
1716 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1718 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1720 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1721 first_block, s->dest[dst_idx] + off,
1722 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1723 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1724 block_cbp |= pat << (i << 2);
1725 if (!v->ttmbf && ttmb < 8)
1732 s->mb_intra = v->is_intra[s->mb_x] = 0;
1733 for (i = 0; i < 6; i++) {
1734 v->mb_type[0][s->block_index[i]] = 0;
1735 s->dc_val[0][s->block_index[i]] = 0;
1737 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1738 s->current_picture.qscale_table[mb_pos] = 0;
1739 v->blk_mv_type[s->block_index[0]] = 0;
1740 v->blk_mv_type[s->block_index[1]] = 0;
1741 v->blk_mv_type[s->block_index[2]] = 0;
1742 v->blk_mv_type[s->block_index[3]] = 0;
1743 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1744 ff_vc1_mc_1mv(v, 0);
1746 if (s->mb_x == s->mb_width - 1)
1747 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
1751 static int vc1_decode_p_mb_intfi(VC1Context *v)
1753 MpegEncContext *s = &v->s;
1754 GetBitContext *gb = &s->gb;
1756 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1757 int cbp = 0; /* cbp decoding stuff */
1758 int mqdiff, mquant; /* MB quantization */
1759 int ttmb = v->ttfrm; /* MB Transform type */
1761 int mb_has_coeffs = 1; /* last_flag */
1762 int dmv_x, dmv_y; /* Differential MV components */
1763 int val; /* temp values */
1764 int first_block = 1;
1767 int block_cbp = 0, pat, block_tt = 0;
1770 mquant = v->pq; /* Lossy initialization */
1772 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1773 if (idx_mbmode <= 1) { // intra MB
1774 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1776 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1777 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1778 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1780 s->current_picture.qscale_table[mb_pos] = mquant;
1781 /* Set DC scale - y and c use the same (not sure if necessary here) */
1782 s->y_dc_scale = s->y_dc_scale_table[mquant];
1783 s->c_dc_scale = s->c_dc_scale_table[mquant];
1784 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1785 mb_has_coeffs = idx_mbmode & 1;
1787 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1789 for (i = 0; i < 6; i++) {
1790 v->a_avail = v->c_avail = 0;
1791 v->mb_type[0][s->block_index[i]] = 1;
1792 s->dc_val[0][s->block_index[i]] = 0;
1794 val = ((cbp >> (5 - i)) & 1);
1795 if (i == 2 || i == 3 || !s->first_slice_line)
1796 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1797 if (i == 1 || i == 3 || s->mb_x)
1798 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1800 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1801 (i & 4) ? v->codingset2 : v->codingset);
1802 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1804 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1805 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1806 s->idsp.put_signed_pixels_clamped(s->block[i],
1807 s->dest[dst_idx] + off,
1808 (i & 4) ? s->uvlinesize
1810 // TODO: loop filter
1813 s->mb_intra = v->is_intra[s->mb_x] = 0;
1814 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1815 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
1816 if (idx_mbmode <= 5) { // 1-MV
1817 dmv_x = dmv_y = pred_flag = 0;
1818 if (idx_mbmode & 1) {
1819 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1821 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1822 ff_vc1_mc_1mv(v, 0);
1823 mb_has_coeffs = !(idx_mbmode & 2);
1825 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1826 for (i = 0; i < 4; i++) {
1827 dmv_x = dmv_y = pred_flag = 0;
1828 if (v->fourmvbp & (8 >> i))
1829 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1830 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1831 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1833 ff_vc1_mc_4mv_chroma(v, 0);
1834 mb_has_coeffs = idx_mbmode & 1;
1837 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1841 s->current_picture.qscale_table[mb_pos] = mquant;
1842 if (!v->ttmbf && cbp) {
1843 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1846 for (i = 0; i < 6; i++) {
1847 s->dc_val[0][s->block_index[i]] = 0;
1849 val = ((cbp >> (5 - i)) & 1);
1850 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1852 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1853 first_block, s->dest[dst_idx] + off,
1854 (i & 4) ? s->uvlinesize : s->linesize,
1855 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1857 block_cbp |= pat << (i << 2);
1858 if (!v->ttmbf && ttmb < 8) ttmb = -1;
1863 if (s->mb_x == s->mb_width - 1)
1864 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1868 /** Decode one B-frame MB (in Main profile)
1870 static void vc1_decode_b_mb(VC1Context *v)
1872 MpegEncContext *s = &v->s;
1873 GetBitContext *gb = &s->gb;
1875 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1876 int cbp = 0; /* cbp decoding stuff */
1877 int mqdiff, mquant; /* MB quantization */
1878 int ttmb = v->ttfrm; /* MB Transform type */
1879 int mb_has_coeffs = 0; /* last_flag */
1880 int index, index1; /* LUT indexes */
1881 int val, sign; /* temp values */
1882 int first_block = 1;
1884 int skipped, direct;
1885 int dmv_x[2], dmv_y[2];
1886 int bmvtype = BMV_TYPE_BACKWARD;
1888 mquant = v->pq; /* lossy initialization */
1892 direct = get_bits1(gb);
1894 direct = v->direct_mb_plane[mb_pos];
1896 skipped = get_bits1(gb);
1898 skipped = v->s.mbskip_table[mb_pos];
1900 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1901 for (i = 0; i < 6; i++) {
1902 v->mb_type[0][s->block_index[i]] = 0;
1903 s->dc_val[0][s->block_index[i]] = 0;
1905 s->current_picture.qscale_table[mb_pos] = 0;
1909 GET_MVDATA(dmv_x[0], dmv_y[0]);
1910 dmv_x[1] = dmv_x[0];
1911 dmv_y[1] = dmv_y[0];
1913 if (skipped || !s->mb_intra) {
1914 bmvtype = decode012(gb);
1917 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1920 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1923 bmvtype = BMV_TYPE_INTERPOLATED;
1924 dmv_x[0] = dmv_y[0] = 0;
1928 for (i = 0; i < 6; i++)
1929 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1933 bmvtype = BMV_TYPE_INTERPOLATED;
1934 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1935 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1939 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1942 s->current_picture.qscale_table[mb_pos] = mquant;
1944 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1945 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1946 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1947 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1949 if (!mb_has_coeffs && !s->mb_intra) {
1950 /* no coded blocks - effectively skipped */
1951 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1952 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1955 if (s->mb_intra && !mb_has_coeffs) {
1957 s->current_picture.qscale_table[mb_pos] = mquant;
1958 s->ac_pred = get_bits1(gb);
1960 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1962 if (bmvtype == BMV_TYPE_INTERPOLATED) {
1963 GET_MVDATA(dmv_x[0], dmv_y[0]);
1964 if (!mb_has_coeffs) {
1965 /* interpolated skipped block */
1966 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1967 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1971 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1973 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1976 s->ac_pred = get_bits1(gb);
1977 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1979 s->current_picture.qscale_table[mb_pos] = mquant;
1980 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1981 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1985 for (i = 0; i < 6; i++) {
1986 s->dc_val[0][s->block_index[i]] = 0;
1988 val = ((cbp >> (5 - i)) & 1);
1989 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1990 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1992 /* check if prediction blocks A and C are available */
1993 v->a_avail = v->c_avail = 0;
1994 if (i == 2 || i == 3 || !s->first_slice_line)
1995 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1996 if (i == 1 || i == 3 || s->mb_x)
1997 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1999 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2000 (i & 4) ? v->codingset2 : v->codingset);
2001 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2003 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2005 for (j = 0; j < 64; j++)
2006 s->block[i][j] <<= 1;
2007 s->idsp.put_signed_pixels_clamped(s->block[i],
2008 s->dest[dst_idx] + off,
2009 i & 4 ? s->uvlinesize
2012 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2013 first_block, s->dest[dst_idx] + off,
2014 (i & 4) ? s->uvlinesize : s->linesize,
2015 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2016 if (!v->ttmbf && ttmb < 8)
2023 /** Decode one B-frame MB (in interlaced field B picture)
2025 static void vc1_decode_b_mb_intfi(VC1Context *v)
2027 MpegEncContext *s = &v->s;
2028 GetBitContext *gb = &s->gb;
2030 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2031 int cbp = 0; /* cbp decoding stuff */
2032 int mqdiff, mquant; /* MB quantization */
2033 int ttmb = v->ttfrm; /* MB Transform type */
2034 int mb_has_coeffs = 0; /* last_flag */
2035 int val; /* temp value */
2036 int first_block = 1;
2039 int dmv_x[2], dmv_y[2], pred_flag[2];
2040 int bmvtype = BMV_TYPE_BACKWARD;
2043 mquant = v->pq; /* Lossy initialization */
2046 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2047 if (idx_mbmode <= 1) { // intra MB
2048 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2050 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2051 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2052 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2054 s->current_picture.qscale_table[mb_pos] = mquant;
2055 /* Set DC scale - y and c use the same (not sure if necessary here) */
2056 s->y_dc_scale = s->y_dc_scale_table[mquant];
2057 s->c_dc_scale = s->c_dc_scale_table[mquant];
2058 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2059 mb_has_coeffs = idx_mbmode & 1;
2061 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2063 for (i = 0; i < 6; i++) {
2064 v->a_avail = v->c_avail = 0;
2065 v->mb_type[0][s->block_index[i]] = 1;
2066 s->dc_val[0][s->block_index[i]] = 0;
2068 val = ((cbp >> (5 - i)) & 1);
2069 if (i == 2 || i == 3 || !s->first_slice_line)
2070 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2071 if (i == 1 || i == 3 || s->mb_x)
2072 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2074 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2075 (i & 4) ? v->codingset2 : v->codingset);
2076 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2078 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2080 for (j = 0; j < 64; j++)
2081 s->block[i][j] <<= 1;
2082 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2083 s->idsp.put_signed_pixels_clamped(s->block[i],
2084 s->dest[dst_idx] + off,
2085 (i & 4) ? s->uvlinesize
2087 // TODO: yet to perform loop filter
2090 s->mb_intra = v->is_intra[s->mb_x] = 0;
2091 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2092 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2094 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2096 fwd = v->forward_mb_plane[mb_pos];
2097 if (idx_mbmode <= 5) { // 1-MV
2099 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2100 pred_flag[0] = pred_flag[1] = 0;
2102 bmvtype = BMV_TYPE_FORWARD;
2104 bmvtype = decode012(gb);
2107 bmvtype = BMV_TYPE_BACKWARD;
2110 bmvtype = BMV_TYPE_DIRECT;
2113 bmvtype = BMV_TYPE_INTERPOLATED;
2114 interpmvp = get_bits1(gb);
2117 v->bmvtype = bmvtype;
2118 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2119 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2122 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2124 if (bmvtype == BMV_TYPE_DIRECT) {
2125 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2126 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2127 if (!s->next_picture_ptr->field_picture) {
2128 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2132 ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2133 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2134 mb_has_coeffs = !(idx_mbmode & 2);
2137 bmvtype = BMV_TYPE_FORWARD;
2138 v->bmvtype = bmvtype;
2139 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2140 for (i = 0; i < 4; i++) {
2141 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2142 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2143 if (v->fourmvbp & (8 >> i)) {
2144 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2145 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2146 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2148 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2149 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2151 ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2152 mb_has_coeffs = idx_mbmode & 1;
2155 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2159 s->current_picture.qscale_table[mb_pos] = mquant;
2160 if (!v->ttmbf && cbp) {
2161 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2164 for (i = 0; i < 6; i++) {
2165 s->dc_val[0][s->block_index[i]] = 0;
2167 val = ((cbp >> (5 - i)) & 1);
2168 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2170 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2171 first_block, s->dest[dst_idx] + off,
2172 (i & 4) ? s->uvlinesize : s->linesize,
2173 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2174 if (!v->ttmbf && ttmb < 8)
2182 /** Decode one B-frame MB (in interlaced frame B picture)
2184 static int vc1_decode_b_mb_intfr(VC1Context *v)
2186 MpegEncContext *s = &v->s;
2187 GetBitContext *gb = &s->gb;
2189 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2190 int cbp = 0; /* cbp decoding stuff */
2191 int mqdiff, mquant; /* MB quantization */
2192 int ttmb = v->ttfrm; /* MB Transform type */
2193 int mvsw = 0; /* motion vector switch */
2194 int mb_has_coeffs = 1; /* last_flag */
2195 int dmv_x, dmv_y; /* Differential MV components */
2196 int val; /* temp value */
2197 int first_block = 1;
2199 int skipped, direct, twomv = 0;
2200 int block_cbp = 0, pat, block_tt = 0;
2201 int idx_mbmode = 0, mvbp;
2202 int stride_y, fieldtx;
2203 int bmvtype = BMV_TYPE_BACKWARD;
2206 mquant = v->pq; /* Lossy initialization */
2209 skipped = get_bits1(gb);
2211 skipped = v->s.mbskip_table[mb_pos];
2214 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2215 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2217 v->blk_mv_type[s->block_index[0]] = 1;
2218 v->blk_mv_type[s->block_index[1]] = 1;
2219 v->blk_mv_type[s->block_index[2]] = 1;
2220 v->blk_mv_type[s->block_index[3]] = 1;
2222 v->blk_mv_type[s->block_index[0]] = 0;
2223 v->blk_mv_type[s->block_index[1]] = 0;
2224 v->blk_mv_type[s->block_index[2]] = 0;
2225 v->blk_mv_type[s->block_index[3]] = 0;
2230 direct = get_bits1(gb);
2232 direct = v->direct_mb_plane[mb_pos];
2235 if (s->next_picture_ptr->field_picture)
2236 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2237 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);
2238 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);
2239 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);
2240 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);
2243 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);
2244 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);
2245 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);
2246 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);
2248 for (i = 1; i < 4; i += 2) {
2249 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2250 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2251 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2252 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2255 for (i = 1; i < 4; i++) {
2256 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2257 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2258 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2259 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2264 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2265 for (i = 0; i < 4; i++) {
2266 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2267 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2268 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2269 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2271 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2273 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2274 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2275 mb_has_coeffs = get_bits1(gb);
2277 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2278 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2280 s->current_picture.qscale_table[mb_pos] = mquant;
2281 /* Set DC scale - y and c use the same (not sure if necessary here) */
2282 s->y_dc_scale = s->y_dc_scale_table[mquant];
2283 s->c_dc_scale = s->c_dc_scale_table[mquant];
2285 for (i = 0; i < 6; i++) {
2286 v->a_avail = v->c_avail = 0;
2287 v->mb_type[0][s->block_index[i]] = 1;
2288 s->dc_val[0][s->block_index[i]] = 0;
2290 val = ((cbp >> (5 - i)) & 1);
2291 if (i == 2 || i == 3 || !s->first_slice_line)
2292 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2293 if (i == 1 || i == 3 || s->mb_x)
2294 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2296 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2297 (i & 4) ? v->codingset2 : v->codingset);
2298 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
2300 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2302 stride_y = s->linesize << fieldtx;
2303 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2305 stride_y = s->uvlinesize;
2308 s->idsp.put_signed_pixels_clamped(s->block[i],
2309 s->dest[dst_idx] + off,
2313 s->mb_intra = v->is_intra[s->mb_x] = 0;
2315 if (skipped || !s->mb_intra) {
2316 bmvtype = decode012(gb);
2319 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2322 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2325 bmvtype = BMV_TYPE_INTERPOLATED;
2329 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2330 mvsw = get_bits1(gb);
2333 if (!skipped) { // inter MB
2334 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2336 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2338 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2339 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2340 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2341 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2345 for (i = 0; i < 6; i++)
2346 v->mb_type[0][s->block_index[i]] = 0;
2347 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2348 /* for all motion vector read MVDATA and motion compensate each block */
2352 for (i = 0; i < 4; i++) {
2353 ff_vc1_mc_4mv_luma(v, i, 0, 0);
2354 ff_vc1_mc_4mv_luma(v, i, 1, 1);
2356 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2357 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2359 ff_vc1_mc_1mv(v, 0);
2360 ff_vc1_interp_mc(v);
2362 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2364 for (i = 0; i < 4; i++) {
2367 val = ((mvbp >> (3 - i)) & 1);
2369 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2371 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2372 ff_vc1_mc_4mv_luma(v, j, dir, dir);
2373 ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2376 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2377 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2378 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2382 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2384 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2385 ff_vc1_mc_1mv(v, 0);
2389 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2391 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2392 ff_vc1_interp_mc(v);
2394 dir = bmvtype == BMV_TYPE_BACKWARD;
2401 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2402 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2406 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2407 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2410 for (i = 0; i < 2; i++) {
2411 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];
2412 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];
2413 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];
2414 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];
2417 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2418 ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2421 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2422 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2423 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2424 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2425 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2427 dir = bmvtype == BMV_TYPE_BACKWARD;
2429 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2432 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2434 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2435 v->blk_mv_type[s->block_index[0]] = 1;
2436 v->blk_mv_type[s->block_index[1]] = 1;
2437 v->blk_mv_type[s->block_index[2]] = 1;
2438 v->blk_mv_type[s->block_index[3]] = 1;
2439 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2440 for (i = 0; i < 2; i++) {
2441 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];
2442 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];
2444 ff_vc1_mc_1mv(v, dir);
2448 GET_MQUANT(); // p. 227
2449 s->current_picture.qscale_table[mb_pos] = mquant;
2450 if (!v->ttmbf && cbp)
2451 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2452 for (i = 0; i < 6; i++) {
2453 s->dc_val[0][s->block_index[i]] = 0;
2455 val = ((cbp >> (5 - i)) & 1);
2457 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2459 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2461 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2462 first_block, s->dest[dst_idx] + off,
2463 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2464 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
2465 block_cbp |= pat << (i << 2);
2466 if (!v->ttmbf && ttmb < 8)
2474 for (i = 0; i < 6; i++) {
2475 v->mb_type[0][s->block_index[i]] = 0;
2476 s->dc_val[0][s->block_index[i]] = 0;
2478 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2479 s->current_picture.qscale_table[mb_pos] = 0;
2480 v->blk_mv_type[s->block_index[0]] = 0;
2481 v->blk_mv_type[s->block_index[1]] = 0;
2482 v->blk_mv_type[s->block_index[2]] = 0;
2483 v->blk_mv_type[s->block_index[3]] = 0;
2486 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2487 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2488 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2490 dir = bmvtype == BMV_TYPE_BACKWARD;
2491 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[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];
2499 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];
2500 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];
2503 v->blk_mv_type[s->block_index[0]] = 1;
2504 v->blk_mv_type[s->block_index[1]] = 1;
2505 v->blk_mv_type[s->block_index[2]] = 1;
2506 v->blk_mv_type[s->block_index[3]] = 1;
2507 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2508 for (i = 0; i < 2; i++) {
2509 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];
2510 s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
2516 ff_vc1_mc_1mv(v, dir);
2517 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2518 ff_vc1_interp_mc(v);
2522 if (s->mb_x == s->mb_width - 1)
2523 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2524 v->cbp[s->mb_x] = block_cbp;
2525 v->ttblk[s->mb_x] = block_tt;
2529 /** Decode blocks of I-frame
2531 static void vc1_decode_i_blocks(VC1Context *v)
2534 MpegEncContext *s = &v->s;
2539 /* select codingmode used for VLC tables selection */
2540 switch (v->y_ac_table_index) {
2542 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2545 v->codingset = CS_HIGH_MOT_INTRA;
2548 v->codingset = CS_MID_RATE_INTRA;
2552 switch (v->c_ac_table_index) {
2554 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2557 v->codingset2 = CS_HIGH_MOT_INTER;
2560 v->codingset2 = CS_MID_RATE_INTER;
2564 /* Set DC scale - y and c use the same */
2565 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2566 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2569 s->mb_x = s->mb_y = 0;
2571 s->first_slice_line = 1;
2572 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2574 init_block_index(v);
2575 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2577 ff_update_block_index(s);
2578 dst[0] = s->dest[0];
2579 dst[1] = dst[0] + 8;
2580 dst[2] = s->dest[0] + s->linesize * 8;
2581 dst[3] = dst[2] + 8;
2582 dst[4] = s->dest[1];
2583 dst[5] = s->dest[2];
2584 s->bdsp.clear_blocks(s->block[0]);
2585 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2586 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2587 s->current_picture.qscale_table[mb_pos] = v->pq;
2588 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2589 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2591 // do actual MB decoding and displaying
2592 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2593 v->s.ac_pred = get_bits1(&v->s.gb);
2595 for (k = 0; k < 6; k++) {
2596 val = ((cbp >> (5 - k)) & 1);
2599 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2603 cbp |= val << (5 - k);
2605 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2607 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2609 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2610 if (v->pq >= 9 && v->overlap) {
2612 for (j = 0; j < 64; j++)
2613 s->block[k][j] <<= 1;
2614 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2615 k & 4 ? s->uvlinesize
2619 for (j = 0; j < 64; j++)
2620 s->block[k][j] = (s->block[k][j] - 64) << 1;
2621 s->idsp.put_pixels_clamped(s->block[k], dst[k],
2622 k & 4 ? s->uvlinesize
2627 if (v->pq >= 9 && v->overlap) {
2629 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2630 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2631 if (!(s->flags & CODEC_FLAG_GRAY)) {
2632 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2633 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2636 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2637 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2638 if (!s->first_slice_line) {
2639 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2640 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2641 if (!(s->flags & CODEC_FLAG_GRAY)) {
2642 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2643 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2646 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2647 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2649 if (v->s.loop_filter)
2650 ff_vc1_loop_filter_iblk(v, v->pq);
2652 if (get_bits_count(&s->gb) > v->bits) {
2653 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2654 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2655 get_bits_count(&s->gb), v->bits);
2659 if (!v->s.loop_filter)
2660 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2662 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2664 s->first_slice_line = 0;
2666 if (v->s.loop_filter)
2667 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2669 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2670 * profile, these only differ are when decoding MSS2 rectangles. */
2671 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2674 /** Decode blocks of I-frame for advanced profile
2676 static void vc1_decode_i_blocks_adv(VC1Context *v)
2679 MpegEncContext *s = &v->s;
2685 GetBitContext *gb = &s->gb;
2687 /* select codingmode used for VLC tables selection */
2688 switch (v->y_ac_table_index) {
2690 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2693 v->codingset = CS_HIGH_MOT_INTRA;
2696 v->codingset = CS_MID_RATE_INTRA;
2700 switch (v->c_ac_table_index) {
2702 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2705 v->codingset2 = CS_HIGH_MOT_INTER;
2708 v->codingset2 = CS_MID_RATE_INTER;
2713 s->mb_x = s->mb_y = 0;
2715 s->first_slice_line = 1;
2716 s->mb_y = s->start_mb_y;
2717 if (s->start_mb_y) {
2719 init_block_index(v);
2720 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2721 (1 + s->b8_stride) * sizeof(*s->coded_block));
2723 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2725 init_block_index(v);
2726 for (;s->mb_x < s->mb_width; s->mb_x++) {
2727 int16_t (*block)[64] = v->block[v->cur_blk_idx];
2728 ff_update_block_index(s);
2729 s->bdsp.clear_blocks(block[0]);
2730 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2731 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2732 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2733 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2735 // do actual MB decoding and displaying
2736 if (v->fieldtx_is_raw)
2737 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2738 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2739 if ( v->acpred_is_raw)
2740 v->s.ac_pred = get_bits1(&v->s.gb);
2742 v->s.ac_pred = v->acpred_plane[mb_pos];
2744 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2745 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2749 s->current_picture.qscale_table[mb_pos] = mquant;
2750 /* Set DC scale - y and c use the same */
2751 s->y_dc_scale = s->y_dc_scale_table[mquant];
2752 s->c_dc_scale = s->c_dc_scale_table[mquant];
2754 for (k = 0; k < 6; k++) {
2755 val = ((cbp >> (5 - k)) & 1);
2758 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2762 cbp |= val << (5 - k);
2764 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2765 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2767 vc1_decode_i_block_adv(v, block[k], k, val,
2768 (k < 4) ? v->codingset : v->codingset2, mquant);
2770 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2772 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2775 ff_vc1_smooth_overlap_filter_iblk(v);
2776 vc1_put_signed_blocks_clamped(v);
2777 if (v->s.loop_filter)
2778 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2780 if (get_bits_count(&s->gb) > v->bits) {
2781 // TODO: may need modification to handle slice coding
2782 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2783 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2784 get_bits_count(&s->gb), v->bits);
2788 if (!v->s.loop_filter)
2789 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2791 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2792 s->first_slice_line = 0;
2795 /* raw bottom MB row */
2797 init_block_index(v);
2799 for (;s->mb_x < s->mb_width; s->mb_x++) {
2800 ff_update_block_index(s);
2801 vc1_put_signed_blocks_clamped(v);
2802 if (v->s.loop_filter)
2803 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2805 if (v->s.loop_filter)
2806 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
2807 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2808 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2811 static void vc1_decode_p_blocks(VC1Context *v)
2813 MpegEncContext *s = &v->s;
2814 int apply_loop_filter;
2816 /* select codingmode used for VLC tables selection */
2817 switch (v->c_ac_table_index) {
2819 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2822 v->codingset = CS_HIGH_MOT_INTRA;
2825 v->codingset = CS_MID_RATE_INTRA;
2829 switch (v->c_ac_table_index) {
2831 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2834 v->codingset2 = CS_HIGH_MOT_INTER;
2837 v->codingset2 = CS_MID_RATE_INTER;
2841 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2842 v->fcm == PROGRESSIVE;
2843 s->first_slice_line = 1;
2844 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2845 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2847 init_block_index(v);
2848 for (; s->mb_x < s->mb_width; s->mb_x++) {
2849 ff_update_block_index(s);
2851 if (v->fcm == ILACE_FIELD)
2852 vc1_decode_p_mb_intfi(v);
2853 else if (v->fcm == ILACE_FRAME)
2854 vc1_decode_p_mb_intfr(v);
2855 else vc1_decode_p_mb(v);
2856 if (s->mb_y != s->start_mb_y && apply_loop_filter)
2857 ff_vc1_apply_p_loop_filter(v);
2858 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2859 // TODO: may need modification to handle slice coding
2860 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2861 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2862 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2866 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
2867 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
2868 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2869 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
2870 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2871 s->first_slice_line = 0;
2873 if (apply_loop_filter) {
2875 init_block_index(v);
2876 for (; s->mb_x < s->mb_width; s->mb_x++) {
2877 ff_update_block_index(s);
2878 ff_vc1_apply_p_loop_filter(v);
2881 if (s->end_mb_y >= s->start_mb_y)
2882 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2883 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2884 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2887 static void vc1_decode_b_blocks(VC1Context *v)
2889 MpegEncContext *s = &v->s;
2891 /* select codingmode used for VLC tables selection */
2892 switch (v->c_ac_table_index) {
2894 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2897 v->codingset = CS_HIGH_MOT_INTRA;
2900 v->codingset = CS_MID_RATE_INTRA;
2904 switch (v->c_ac_table_index) {
2906 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2909 v->codingset2 = CS_HIGH_MOT_INTER;
2912 v->codingset2 = CS_MID_RATE_INTER;
2916 s->first_slice_line = 1;
2917 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2919 init_block_index(v);
2920 for (; s->mb_x < s->mb_width; s->mb_x++) {
2921 ff_update_block_index(s);
2923 if (v->fcm == ILACE_FIELD)
2924 vc1_decode_b_mb_intfi(v);
2925 else if (v->fcm == ILACE_FRAME)
2926 vc1_decode_b_mb_intfr(v);
2929 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2930 // TODO: may need modification to handle slice coding
2931 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2932 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2933 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2936 if (v->s.loop_filter)
2937 ff_vc1_loop_filter_iblk(v, v->pq);
2939 if (!v->s.loop_filter)
2940 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2942 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2943 s->first_slice_line = 0;
2945 if (v->s.loop_filter)
2946 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2947 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2948 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2951 static void vc1_decode_skip_blocks(VC1Context *v)
2953 MpegEncContext *s = &v->s;
2955 if (!v->s.last_picture.f->data[0])
2958 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2959 s->first_slice_line = 1;
2960 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2962 init_block_index(v);
2963 ff_update_block_index(s);
2964 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2965 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2966 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2967 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2968 s->first_slice_line = 0;
2970 s->pict_type = AV_PICTURE_TYPE_P;
2973 void ff_vc1_decode_blocks(VC1Context *v)
2976 v->s.esc3_level_length = 0;
2978 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
2981 v->left_blk_idx = -1;
2982 v->topleft_blk_idx = 1;
2984 switch (v->s.pict_type) {
2985 case AV_PICTURE_TYPE_I:
2986 if (v->profile == PROFILE_ADVANCED)
2987 vc1_decode_i_blocks_adv(v);
2989 vc1_decode_i_blocks(v);
2991 case AV_PICTURE_TYPE_P:
2992 if (v->p_frame_skipped)
2993 vc1_decode_skip_blocks(v);
2995 vc1_decode_p_blocks(v);
2997 case AV_PICTURE_TYPE_B:
2999 if (v->profile == PROFILE_ADVANCED)
3000 vc1_decode_i_blocks_adv(v);
3002 vc1_decode_i_blocks(v);
3004 vc1_decode_b_blocks(v);