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 (a_avail && c_avail) {
427 if (abs(a - b) <= abs(b - c)) {
429 *dir_ptr = 1; // left
434 } else if (a_avail) {
437 } else if (c_avail) {
439 *dir_ptr = 1; // left
442 *dir_ptr = 1; // left
445 /* update predictor */
446 *dc_val_ptr = &dc_val[0];
450 /** @} */ // Block group
453 * @name VC1 Macroblock-level functions in Simple/Main Profiles
454 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
458 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
459 uint8_t **coded_block_ptr)
461 int xy, wrap, pred, a, b, c;
463 xy = s->block_index[n];
469 a = s->coded_block[xy - 1 ];
470 b = s->coded_block[xy - 1 - wrap];
471 c = s->coded_block[xy - wrap];
480 *coded_block_ptr = &s->coded_block[xy];
486 * Decode one AC coefficient
487 * @param v The VC1 context
488 * @param last Last coefficient
489 * @param skip How much zero coefficients to skip
490 * @param value Decoded AC coefficient value
491 * @param codingset set of VLC to decode data
494 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
495 int *value, int codingset)
497 GetBitContext *gb = &v->s.gb;
498 int index, escape, run = 0, level = 0, lst = 0;
500 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
501 if (index != ff_vc1_ac_sizes[codingset] - 1) {
502 run = vc1_index_decode_table[codingset][index][0];
503 level = vc1_index_decode_table[codingset][index][1];
504 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
508 escape = decode210(gb);
510 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
511 run = vc1_index_decode_table[codingset][index][0];
512 level = vc1_index_decode_table[codingset][index][1];
513 lst = index >= vc1_last_decode_table[codingset];
516 level += vc1_last_delta_level_table[codingset][run];
518 level += vc1_delta_level_table[codingset][run];
521 run += vc1_last_delta_run_table[codingset][level] + 1;
523 run += vc1_delta_run_table[codingset][level] + 1;
530 if (v->s.esc3_level_length == 0) {
531 if (v->pq < 8 || v->dquantfrm) { // table 59
532 v->s.esc3_level_length = get_bits(gb, 3);
533 if (!v->s.esc3_level_length)
534 v->s.esc3_level_length = get_bits(gb, 2) + 8;
536 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
538 v->s.esc3_run_length = 3 + get_bits(gb, 2);
540 run = get_bits(gb, v->s.esc3_run_length);
541 sign = get_bits1(gb);
542 level = get_bits(gb, v->s.esc3_level_length);
553 /** Decode intra block in intra frames - should be faster than decode_intra_block
554 * @param v VC1Context
555 * @param block block to decode
556 * @param[in] n subblock index
557 * @param coded are AC coeffs present or not
558 * @param codingset set of VLC to decode data
560 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
561 int coded, int codingset)
563 GetBitContext *gb = &v->s.gb;
564 MpegEncContext *s = &v->s;
565 int dc_pred_dir = 0; /* Direction of the DC prediction used */
568 int16_t *ac_val, *ac_val2;
571 /* Get DC differential */
573 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
575 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
578 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
582 if (dcdiff == 119 /* ESC index value */) {
584 if (v->pq == 1) dcdiff = get_bits(gb, 10);
585 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
586 else dcdiff = get_bits(gb, 8);
589 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
591 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
598 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
601 /* Store the quantized DC coeff, used for prediction */
603 block[0] = dcdiff * s->y_dc_scale;
605 block[0] = dcdiff * s->c_dc_scale;
616 int last = 0, skip, value;
617 const uint8_t *zz_table;
621 scale = v->pq * 2 + v->halfpq;
625 zz_table = v->zz_8x8[2];
627 zz_table = v->zz_8x8[3];
629 zz_table = v->zz_8x8[1];
631 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
633 if (dc_pred_dir) // left
636 ac_val -= 16 * s->block_wrap[n];
639 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
643 block[zz_table[i++]] = value;
646 /* apply AC prediction if needed */
648 if (dc_pred_dir) { // left
649 for (k = 1; k < 8; k++)
650 block[k << v->left_blk_sh] += ac_val[k];
652 for (k = 1; k < 8; k++)
653 block[k << v->top_blk_sh] += ac_val[k + 8];
656 /* save AC coeffs for further prediction */
657 for (k = 1; k < 8; k++) {
658 ac_val2[k] = block[k << v->left_blk_sh];
659 ac_val2[k + 8] = block[k << v->top_blk_sh];
662 /* scale AC coeffs */
663 for (k = 1; k < 64; k++)
667 block[k] += (block[k] < 0) ? -v->pq : v->pq;
670 if (s->ac_pred) i = 63;
676 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
680 scale = v->pq * 2 + v->halfpq;
681 memset(ac_val2, 0, 16 * 2);
682 if (dc_pred_dir) { // left
685 memcpy(ac_val2, ac_val, 8 * 2);
687 ac_val -= 16 * s->block_wrap[n];
689 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
692 /* apply AC prediction if needed */
694 if (dc_pred_dir) { //left
695 for (k = 1; k < 8; k++) {
696 block[k << v->left_blk_sh] = ac_val[k] * scale;
697 if (!v->pquantizer && block[k << v->left_blk_sh])
698 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
701 for (k = 1; k < 8; k++) {
702 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
703 if (!v->pquantizer && block[k << v->top_blk_sh])
704 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
710 s->block_last_index[n] = i;
715 /** Decode intra block in intra frames - should be faster than decode_intra_block
716 * @param v VC1Context
717 * @param block block to decode
718 * @param[in] n subblock number
719 * @param coded are AC coeffs present or not
720 * @param codingset set of VLC to decode data
721 * @param mquant quantizer value for this macroblock
723 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
724 int coded, int codingset, int mquant)
726 GetBitContext *gb = &v->s.gb;
727 MpegEncContext *s = &v->s;
728 int dc_pred_dir = 0; /* Direction of the DC prediction used */
730 int16_t *dc_val = NULL;
731 int16_t *ac_val, *ac_val2;
733 int a_avail = v->a_avail, c_avail = v->c_avail;
734 int use_pred = s->ac_pred;
737 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
739 /* Get DC differential */
741 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
743 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
746 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
750 if (dcdiff == 119 /* ESC index value */) {
752 if (mquant == 1) dcdiff = get_bits(gb, 10);
753 else if (mquant == 2) dcdiff = get_bits(gb, 9);
754 else dcdiff = get_bits(gb, 8);
757 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
758 else if (mquant == 2)
759 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
766 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
769 /* Store the quantized DC coeff, used for prediction */
771 block[0] = dcdiff * s->y_dc_scale;
773 block[0] = dcdiff * s->c_dc_scale;
779 /* check if AC is needed at all */
780 if (!a_avail && !c_avail)
782 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
785 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
787 if (dc_pred_dir) // left
790 ac_val -= 16 * s->block_wrap[n];
792 q1 = s->current_picture.qscale_table[mb_pos];
793 if ( dc_pred_dir && c_avail && mb_pos)
794 q2 = s->current_picture.qscale_table[mb_pos - 1];
795 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
796 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
797 if ( dc_pred_dir && n == 1)
799 if (!dc_pred_dir && n == 2)
805 int last = 0, skip, value;
806 const uint8_t *zz_table;
810 if (!use_pred && v->fcm == ILACE_FRAME) {
811 zz_table = v->zzi_8x8;
813 if (!dc_pred_dir) // top
814 zz_table = v->zz_8x8[2];
816 zz_table = v->zz_8x8[3];
819 if (v->fcm != ILACE_FRAME)
820 zz_table = v->zz_8x8[1];
822 zz_table = v->zzi_8x8;
826 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
830 block[zz_table[i++]] = value;
833 /* apply AC prediction if needed */
835 /* scale predictors if needed*/
836 if (q2 && q1 != q2) {
837 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
838 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
841 return AVERROR_INVALIDDATA;
842 if (dc_pred_dir) { // left
843 for (k = 1; k < 8; k++)
844 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
846 for (k = 1; k < 8; k++)
847 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
850 if (dc_pred_dir) { //left
851 for (k = 1; k < 8; k++)
852 block[k << v->left_blk_sh] += ac_val[k];
854 for (k = 1; k < 8; k++)
855 block[k << v->top_blk_sh] += ac_val[k + 8];
859 /* save AC coeffs for further prediction */
860 for (k = 1; k < 8; k++) {
861 ac_val2[k ] = block[k << v->left_blk_sh];
862 ac_val2[k + 8] = block[k << v->top_blk_sh];
865 /* scale AC coeffs */
866 for (k = 1; k < 64; k++)
870 block[k] += (block[k] < 0) ? -mquant : mquant;
873 if (use_pred) i = 63;
874 } else { // no AC coeffs
877 memset(ac_val2, 0, 16 * 2);
878 if (dc_pred_dir) { // left
880 memcpy(ac_val2, ac_val, 8 * 2);
881 if (q2 && q1 != q2) {
882 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
883 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
885 return AVERROR_INVALIDDATA;
886 for (k = 1; k < 8; k++)
887 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
892 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
893 if (q2 && q1 != q2) {
894 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
895 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
897 return AVERROR_INVALIDDATA;
898 for (k = 1; k < 8; k++)
899 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
904 /* apply AC prediction if needed */
906 if (dc_pred_dir) { // left
907 for (k = 1; k < 8; k++) {
908 block[k << v->left_blk_sh] = ac_val2[k] * scale;
909 if (!v->pquantizer && block[k << v->left_blk_sh])
910 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
913 for (k = 1; k < 8; k++) {
914 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
915 if (!v->pquantizer && block[k << v->top_blk_sh])
916 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
922 s->block_last_index[n] = i;
927 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
928 * @param v VC1Context
929 * @param block block to decode
930 * @param[in] n subblock index
931 * @param coded are AC coeffs present or not
932 * @param mquant block quantizer
933 * @param codingset set of VLC to decode data
935 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
936 int coded, int mquant, int codingset)
938 GetBitContext *gb = &v->s.gb;
939 MpegEncContext *s = &v->s;
940 int dc_pred_dir = 0; /* Direction of the DC prediction used */
942 int16_t *dc_val = NULL;
943 int16_t *ac_val, *ac_val2;
945 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
946 int a_avail = v->a_avail, c_avail = v->c_avail;
947 int use_pred = s->ac_pred;
951 s->bdsp.clear_block(block);
953 /* XXX: Guard against dumb values of mquant */
954 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
956 /* Set DC scale - y and c use the same */
957 s->y_dc_scale = s->y_dc_scale_table[mquant];
958 s->c_dc_scale = s->c_dc_scale_table[mquant];
960 /* Get DC differential */
962 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
964 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
967 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
971 if (dcdiff == 119 /* ESC index value */) {
973 if (mquant == 1) dcdiff = get_bits(gb, 10);
974 else if (mquant == 2) dcdiff = get_bits(gb, 9);
975 else dcdiff = get_bits(gb, 8);
978 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
979 else if (mquant == 2)
980 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
987 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
990 /* Store the quantized DC coeff, used for prediction */
993 block[0] = dcdiff * s->y_dc_scale;
995 block[0] = dcdiff * s->c_dc_scale;
1001 /* check if AC is needed at all and adjust direction if needed */
1002 if (!a_avail) dc_pred_dir = 1;
1003 if (!c_avail) dc_pred_dir = 0;
1004 if (!a_avail && !c_avail) use_pred = 0;
1005 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1008 scale = mquant * 2 + v->halfpq;
1010 if (dc_pred_dir) //left
1013 ac_val -= 16 * s->block_wrap[n];
1015 q1 = s->current_picture.qscale_table[mb_pos];
1016 if (dc_pred_dir && c_avail && mb_pos)
1017 q2 = s->current_picture.qscale_table[mb_pos - 1];
1018 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
1019 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1020 if ( dc_pred_dir && n == 1)
1022 if (!dc_pred_dir && n == 2)
1024 if (n == 3) q2 = q1;
1027 int last = 0, skip, value;
1031 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1035 if (v->fcm == PROGRESSIVE)
1036 block[v->zz_8x8[0][i++]] = value;
1038 if (use_pred && (v->fcm == ILACE_FRAME)) {
1039 if (!dc_pred_dir) // top
1040 block[v->zz_8x8[2][i++]] = value;
1042 block[v->zz_8x8[3][i++]] = value;
1044 block[v->zzi_8x8[i++]] = value;
1049 /* apply AC prediction if needed */
1051 /* scale predictors if needed*/
1052 if (q2 && q1 != q2) {
1053 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1054 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1057 return AVERROR_INVALIDDATA;
1058 if (dc_pred_dir) { // left
1059 for (k = 1; k < 8; k++)
1060 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1062 for (k = 1; k < 8; k++)
1063 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1066 if (dc_pred_dir) { // left
1067 for (k = 1; k < 8; k++)
1068 block[k << v->left_blk_sh] += ac_val[k];
1070 for (k = 1; k < 8; k++)
1071 block[k << v->top_blk_sh] += ac_val[k + 8];
1075 /* save AC coeffs for further prediction */
1076 for (k = 1; k < 8; k++) {
1077 ac_val2[k ] = block[k << v->left_blk_sh];
1078 ac_val2[k + 8] = block[k << v->top_blk_sh];
1081 /* scale AC coeffs */
1082 for (k = 1; k < 64; k++)
1086 block[k] += (block[k] < 0) ? -mquant : mquant;
1089 if (use_pred) i = 63;
1090 } else { // no AC coeffs
1093 memset(ac_val2, 0, 16 * 2);
1094 if (dc_pred_dir) { // left
1096 memcpy(ac_val2, ac_val, 8 * 2);
1097 if (q2 && q1 != q2) {
1098 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1099 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1101 return AVERROR_INVALIDDATA;
1102 for (k = 1; k < 8; k++)
1103 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1108 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1109 if (q2 && q1 != q2) {
1110 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1111 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1113 return AVERROR_INVALIDDATA;
1114 for (k = 1; k < 8; k++)
1115 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1120 /* apply AC prediction if needed */
1122 if (dc_pred_dir) { // left
1123 for (k = 1; k < 8; k++) {
1124 block[k << v->left_blk_sh] = ac_val2[k] * scale;
1125 if (!v->pquantizer && block[k << v->left_blk_sh])
1126 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1129 for (k = 1; k < 8; k++) {
1130 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1131 if (!v->pquantizer && block[k << v->top_blk_sh])
1132 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1138 s->block_last_index[n] = i;
1145 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1146 int mquant, int ttmb, int first_block,
1147 uint8_t *dst, int linesize, int skip_block,
1150 MpegEncContext *s = &v->s;
1151 GetBitContext *gb = &s->gb;
1154 int scale, off, idx, last, skip, value;
1155 int ttblk = ttmb & 7;
1158 s->bdsp.clear_block(block);
1161 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)];
1163 if (ttblk == TT_4X4) {
1164 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1166 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1167 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1168 || (!v->res_rtm_flag && !first_block))) {
1169 subblkpat = decode012(gb);
1171 subblkpat ^= 3; // swap decoded pattern bits
1172 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1174 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1177 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1179 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1180 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1181 subblkpat = 2 - (ttblk == TT_8X4_TOP);
1184 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1185 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1194 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1199 idx = v->zz_8x8[0][i++];
1201 idx = v->zzi_8x8[i++];
1202 block[idx] = value * scale;
1204 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1208 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1210 v->vc1dsp.vc1_inv_trans_8x8(block);
1211 s->idsp.add_pixels_clamped(block, dst, linesize);
1216 pat = ~subblkpat & 0xF;
1217 for (j = 0; j < 4; j++) {
1218 last = subblkpat & (1 << (3 - j));
1220 off = (j & 1) * 4 + (j & 2) * 16;
1222 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1227 idx = ff_vc1_simple_progressive_4x4_zz[i++];
1229 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1230 block[idx + off] = value * scale;
1232 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1234 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1236 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1238 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1243 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1244 for (j = 0; j < 2; j++) {
1245 last = subblkpat & (1 << (1 - j));
1249 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1254 idx = v->zz_8x4[i++] + off;
1256 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1257 block[idx] = value * scale;
1259 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1261 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1263 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1265 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1270 pat = ~(subblkpat * 5) & 0xF;
1271 for (j = 0; j < 2; j++) {
1272 last = subblkpat & (1 << (1 - j));
1276 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1281 idx = v->zz_4x8[i++] + off;
1283 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1284 block[idx] = value * scale;
1286 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1288 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1290 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1292 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1298 *ttmb_out |= ttblk << (n * 4);
1302 /** @} */ // Macroblock group
1304 static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 };
1306 /** Decode one P-frame MB
1308 static int vc1_decode_p_mb(VC1Context *v)
1310 MpegEncContext *s = &v->s;
1311 GetBitContext *gb = &s->gb;
1313 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1314 int cbp; /* cbp decoding stuff */
1315 int mqdiff, mquant; /* MB quantization */
1316 int ttmb = v->ttfrm; /* MB Transform type */
1318 int mb_has_coeffs = 1; /* last_flag */
1319 int dmv_x, dmv_y; /* Differential MV components */
1320 int index, index1; /* LUT indexes */
1321 int val, sign; /* temp values */
1322 int first_block = 1;
1324 int skipped, fourmv;
1325 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1327 mquant = v->pq; /* lossy initialization */
1329 if (v->mv_type_is_raw)
1330 fourmv = get_bits1(gb);
1332 fourmv = v->mv_type_mb_plane[mb_pos];
1334 skipped = get_bits1(gb);
1336 skipped = v->s.mbskip_table[mb_pos];
1338 if (!fourmv) { /* 1MV mode */
1340 GET_MVDATA(dmv_x, dmv_y);
1343 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1344 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1346 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1347 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1349 /* FIXME Set DC val for inter block ? */
1350 if (s->mb_intra && !mb_has_coeffs) {
1352 s->ac_pred = get_bits1(gb);
1354 } else if (mb_has_coeffs) {
1356 s->ac_pred = get_bits1(gb);
1357 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1363 s->current_picture.qscale_table[mb_pos] = mquant;
1365 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1366 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1367 VC1_TTMB_VLC_BITS, 2);
1368 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1370 for (i = 0; i < 6; i++) {
1371 s->dc_val[0][s->block_index[i]] = 0;
1373 val = ((cbp >> (5 - i)) & 1);
1374 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1375 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1377 /* check if prediction blocks A and C are available */
1378 v->a_avail = v->c_avail = 0;
1379 if (i == 2 || i == 3 || !s->first_slice_line)
1380 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1381 if (i == 1 || i == 3 || s->mb_x)
1382 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1384 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1385 (i & 4) ? v->codingset2 : v->codingset);
1386 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1388 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1390 for (j = 0; j < 64; j++)
1391 s->block[i][j] <<= 1;
1392 s->idsp.put_signed_pixels_clamped(s->block[i],
1393 s->dest[dst_idx] + off,
1394 i & 4 ? s->uvlinesize
1396 if (v->pq >= 9 && v->overlap) {
1398 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1400 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1402 block_cbp |= 0xF << (i << 2);
1403 block_intra |= 1 << i;
1405 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
1406 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1407 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1408 block_cbp |= pat << (i << 2);
1409 if (!v->ttmbf && ttmb < 8)
1416 for (i = 0; i < 6; i++) {
1417 v->mb_type[0][s->block_index[i]] = 0;
1418 s->dc_val[0][s->block_index[i]] = 0;
1420 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1421 s->current_picture.qscale_table[mb_pos] = 0;
1422 ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1423 ff_vc1_mc_1mv(v, 0);
1425 } else { // 4MV mode
1426 if (!skipped /* unskipped MB */) {
1427 int intra_count = 0, coded_inter = 0;
1428 int is_intra[6], is_coded[6];
1430 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1431 for (i = 0; i < 6; i++) {
1432 val = ((cbp >> (5 - i)) & 1);
1433 s->dc_val[0][s->block_index[i]] = 0;
1440 GET_MVDATA(dmv_x, dmv_y);
1442 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1444 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1445 intra_count += s->mb_intra;
1446 is_intra[i] = s->mb_intra;
1447 is_coded[i] = mb_has_coeffs;
1450 is_intra[i] = (intra_count >= 3);
1454 ff_vc1_mc_4mv_chroma(v, 0);
1455 v->mb_type[0][s->block_index[i]] = is_intra[i];
1457 coded_inter = !is_intra[i] & is_coded[i];
1459 // if there are no coded blocks then don't do anything more
1461 if (!intra_count && !coded_inter)
1464 s->current_picture.qscale_table[mb_pos] = mquant;
1465 /* test if block is intra and has pred */
1468 for (i = 0; i < 6; i++)
1470 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1471 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1477 s->ac_pred = get_bits1(gb);
1481 if (!v->ttmbf && coded_inter)
1482 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1483 for (i = 0; i < 6; i++) {
1485 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1486 s->mb_intra = is_intra[i];
1488 /* check if prediction blocks A and C are available */
1489 v->a_avail = v->c_avail = 0;
1490 if (i == 2 || i == 3 || !s->first_slice_line)
1491 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1492 if (i == 1 || i == 3 || s->mb_x)
1493 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1495 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
1496 (i & 4) ? v->codingset2 : v->codingset);
1497 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1499 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1501 for (j = 0; j < 64; j++)
1502 s->block[i][j] <<= 1;
1503 s->idsp.put_signed_pixels_clamped(s->block[i],
1504 s->dest[dst_idx] + off,
1505 (i & 4) ? s->uvlinesize
1507 if (v->pq >= 9 && v->overlap) {
1509 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1511 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1513 block_cbp |= 0xF << (i << 2);
1514 block_intra |= 1 << i;
1515 } else if (is_coded[i]) {
1516 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1517 first_block, s->dest[dst_idx] + off,
1518 (i & 4) ? s->uvlinesize : s->linesize,
1519 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1521 block_cbp |= pat << (i << 2);
1522 if (!v->ttmbf && ttmb < 8)
1527 } else { // skipped MB
1529 s->current_picture.qscale_table[mb_pos] = 0;
1530 for (i = 0; i < 6; i++) {
1531 v->mb_type[0][s->block_index[i]] = 0;
1532 s->dc_val[0][s->block_index[i]] = 0;
1534 for (i = 0; i < 4; i++) {
1535 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1536 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1538 ff_vc1_mc_4mv_chroma(v, 0);
1539 s->current_picture.qscale_table[mb_pos] = 0;
1543 v->cbp[s->mb_x] = block_cbp;
1544 v->ttblk[s->mb_x] = block_tt;
1545 v->is_intra[s->mb_x] = block_intra;
1550 /* Decode one macroblock in an interlaced frame p picture */
1552 static int vc1_decode_p_mb_intfr(VC1Context *v)
1554 MpegEncContext *s = &v->s;
1555 GetBitContext *gb = &s->gb;
1557 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1558 int cbp = 0; /* cbp decoding stuff */
1559 int mqdiff, mquant; /* MB quantization */
1560 int ttmb = v->ttfrm; /* MB Transform type */
1562 int mb_has_coeffs = 1; /* last_flag */
1563 int dmv_x, dmv_y; /* Differential MV components */
1564 int val; /* temp value */
1565 int first_block = 1;
1567 int skipped, fourmv = 0, twomv = 0;
1568 int block_cbp = 0, pat, block_tt = 0;
1569 int idx_mbmode = 0, mvbp;
1570 int stride_y, fieldtx;
1572 mquant = v->pq; /* Lossy initialization */
1575 skipped = get_bits1(gb);
1577 skipped = v->s.mbskip_table[mb_pos];
1579 if (v->fourmvswitch)
1580 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1582 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1583 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1584 /* store the motion vector type in a flag (useful later) */
1585 case MV_PMODE_INTFR_4MV:
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;
1592 case MV_PMODE_INTFR_4MV_FIELD:
1594 v->blk_mv_type[s->block_index[0]] = 1;
1595 v->blk_mv_type[s->block_index[1]] = 1;
1596 v->blk_mv_type[s->block_index[2]] = 1;
1597 v->blk_mv_type[s->block_index[3]] = 1;
1599 case MV_PMODE_INTFR_2MV_FIELD:
1601 v->blk_mv_type[s->block_index[0]] = 1;
1602 v->blk_mv_type[s->block_index[1]] = 1;
1603 v->blk_mv_type[s->block_index[2]] = 1;
1604 v->blk_mv_type[s->block_index[3]] = 1;
1606 case MV_PMODE_INTFR_1MV:
1607 v->blk_mv_type[s->block_index[0]] = 0;
1608 v->blk_mv_type[s->block_index[1]] = 0;
1609 v->blk_mv_type[s->block_index[2]] = 0;
1610 v->blk_mv_type[s->block_index[3]] = 0;
1613 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1614 for (i = 0; i < 4; i++) {
1615 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1616 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1618 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1620 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1621 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1622 mb_has_coeffs = get_bits1(gb);
1624 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1625 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1627 s->current_picture.qscale_table[mb_pos] = mquant;
1628 /* Set DC scale - y and c use the same (not sure if necessary here) */
1629 s->y_dc_scale = s->y_dc_scale_table[mquant];
1630 s->c_dc_scale = s->c_dc_scale_table[mquant];
1632 for (i = 0; i < 6; i++) {
1633 v->a_avail = v->c_avail = 0;
1634 v->mb_type[0][s->block_index[i]] = 1;
1635 s->dc_val[0][s->block_index[i]] = 0;
1637 val = ((cbp >> (5 - i)) & 1);
1638 if (i == 2 || i == 3 || !s->first_slice_line)
1639 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1640 if (i == 1 || i == 3 || s->mb_x)
1641 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1643 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1644 (i & 4) ? v->codingset2 : v->codingset);
1645 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
1646 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1648 stride_y = s->linesize << fieldtx;
1649 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1651 stride_y = s->uvlinesize;
1654 s->idsp.put_signed_pixels_clamped(s->block[i],
1655 s->dest[dst_idx] + off,
1660 } else { // inter MB
1661 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1663 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1664 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1665 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1667 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1668 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1669 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1672 s->mb_intra = v->is_intra[s->mb_x] = 0;
1673 for (i = 0; i < 6; i++)
1674 v->mb_type[0][s->block_index[i]] = 0;
1675 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1676 /* for all motion vector read MVDATA and motion compensate each block */
1680 for (i = 0; i < 4; i++) {
1682 if (mvbp & (8 >> i))
1683 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1684 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1685 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1687 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1692 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1694 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1695 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1696 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1699 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1701 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1702 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1703 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1704 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1706 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1709 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1711 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1712 ff_vc1_mc_1mv(v, 0);
1715 GET_MQUANT(); // p. 227
1716 s->current_picture.qscale_table[mb_pos] = mquant;
1717 if (!v->ttmbf && cbp)
1718 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1719 for (i = 0; i < 6; i++) {
1720 s->dc_val[0][s->block_index[i]] = 0;
1722 val = ((cbp >> (5 - i)) & 1);
1724 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1726 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1728 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1729 first_block, s->dest[dst_idx] + off,
1730 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1731 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1732 block_cbp |= pat << (i << 2);
1733 if (!v->ttmbf && ttmb < 8)
1740 s->mb_intra = v->is_intra[s->mb_x] = 0;
1741 for (i = 0; i < 6; i++) {
1742 v->mb_type[0][s->block_index[i]] = 0;
1743 s->dc_val[0][s->block_index[i]] = 0;
1745 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1746 s->current_picture.qscale_table[mb_pos] = 0;
1747 v->blk_mv_type[s->block_index[0]] = 0;
1748 v->blk_mv_type[s->block_index[1]] = 0;
1749 v->blk_mv_type[s->block_index[2]] = 0;
1750 v->blk_mv_type[s->block_index[3]] = 0;
1751 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1752 ff_vc1_mc_1mv(v, 0);
1754 if (s->mb_x == s->mb_width - 1)
1755 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
1759 static int vc1_decode_p_mb_intfi(VC1Context *v)
1761 MpegEncContext *s = &v->s;
1762 GetBitContext *gb = &s->gb;
1764 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1765 int cbp = 0; /* cbp decoding stuff */
1766 int mqdiff, mquant; /* MB quantization */
1767 int ttmb = v->ttfrm; /* MB Transform type */
1769 int mb_has_coeffs = 1; /* last_flag */
1770 int dmv_x, dmv_y; /* Differential MV components */
1771 int val; /* temp values */
1772 int first_block = 1;
1775 int block_cbp = 0, pat, block_tt = 0;
1778 mquant = v->pq; /* Lossy initialization */
1780 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1781 if (idx_mbmode <= 1) { // intra MB
1782 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1784 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1785 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1786 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1788 s->current_picture.qscale_table[mb_pos] = mquant;
1789 /* Set DC scale - y and c use the same (not sure if necessary here) */
1790 s->y_dc_scale = s->y_dc_scale_table[mquant];
1791 s->c_dc_scale = s->c_dc_scale_table[mquant];
1792 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1793 mb_has_coeffs = idx_mbmode & 1;
1795 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1797 for (i = 0; i < 6; i++) {
1798 v->a_avail = v->c_avail = 0;
1799 v->mb_type[0][s->block_index[i]] = 1;
1800 s->dc_val[0][s->block_index[i]] = 0;
1802 val = ((cbp >> (5 - i)) & 1);
1803 if (i == 2 || i == 3 || !s->first_slice_line)
1804 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1805 if (i == 1 || i == 3 || s->mb_x)
1806 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1808 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1809 (i & 4) ? v->codingset2 : v->codingset);
1810 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1812 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1813 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1814 s->idsp.put_signed_pixels_clamped(s->block[i],
1815 s->dest[dst_idx] + off,
1816 (i & 4) ? s->uvlinesize
1818 // TODO: loop filter
1821 s->mb_intra = v->is_intra[s->mb_x] = 0;
1822 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1823 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
1824 if (idx_mbmode <= 5) { // 1-MV
1825 dmv_x = dmv_y = pred_flag = 0;
1826 if (idx_mbmode & 1) {
1827 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1829 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1830 ff_vc1_mc_1mv(v, 0);
1831 mb_has_coeffs = !(idx_mbmode & 2);
1833 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1834 for (i = 0; i < 4; i++) {
1835 dmv_x = dmv_y = pred_flag = 0;
1836 if (v->fourmvbp & (8 >> i))
1837 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1838 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1839 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1841 ff_vc1_mc_4mv_chroma(v, 0);
1842 mb_has_coeffs = idx_mbmode & 1;
1845 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1849 s->current_picture.qscale_table[mb_pos] = mquant;
1850 if (!v->ttmbf && cbp) {
1851 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1854 for (i = 0; i < 6; i++) {
1855 s->dc_val[0][s->block_index[i]] = 0;
1857 val = ((cbp >> (5 - i)) & 1);
1858 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1860 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1861 first_block, s->dest[dst_idx] + off,
1862 (i & 4) ? s->uvlinesize : s->linesize,
1863 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1865 block_cbp |= pat << (i << 2);
1866 if (!v->ttmbf && ttmb < 8) ttmb = -1;
1871 if (s->mb_x == s->mb_width - 1)
1872 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1876 /** Decode one B-frame MB (in Main profile)
1878 static void vc1_decode_b_mb(VC1Context *v)
1880 MpegEncContext *s = &v->s;
1881 GetBitContext *gb = &s->gb;
1883 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1884 int cbp = 0; /* cbp decoding stuff */
1885 int mqdiff, mquant; /* MB quantization */
1886 int ttmb = v->ttfrm; /* MB Transform type */
1887 int mb_has_coeffs = 0; /* last_flag */
1888 int index, index1; /* LUT indexes */
1889 int val, sign; /* temp values */
1890 int first_block = 1;
1892 int skipped, direct;
1893 int dmv_x[2], dmv_y[2];
1894 int bmvtype = BMV_TYPE_BACKWARD;
1896 mquant = v->pq; /* lossy initialization */
1900 direct = get_bits1(gb);
1902 direct = v->direct_mb_plane[mb_pos];
1904 skipped = get_bits1(gb);
1906 skipped = v->s.mbskip_table[mb_pos];
1908 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1909 for (i = 0; i < 6; i++) {
1910 v->mb_type[0][s->block_index[i]] = 0;
1911 s->dc_val[0][s->block_index[i]] = 0;
1913 s->current_picture.qscale_table[mb_pos] = 0;
1917 GET_MVDATA(dmv_x[0], dmv_y[0]);
1918 dmv_x[1] = dmv_x[0];
1919 dmv_y[1] = dmv_y[0];
1921 if (skipped || !s->mb_intra) {
1922 bmvtype = decode012(gb);
1925 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1928 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1931 bmvtype = BMV_TYPE_INTERPOLATED;
1932 dmv_x[0] = dmv_y[0] = 0;
1936 for (i = 0; i < 6; i++)
1937 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1941 bmvtype = BMV_TYPE_INTERPOLATED;
1942 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1943 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1947 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1950 s->current_picture.qscale_table[mb_pos] = mquant;
1952 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1953 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1954 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1955 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1957 if (!mb_has_coeffs && !s->mb_intra) {
1958 /* no coded blocks - effectively skipped */
1959 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1960 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1963 if (s->mb_intra && !mb_has_coeffs) {
1965 s->current_picture.qscale_table[mb_pos] = mquant;
1966 s->ac_pred = get_bits1(gb);
1968 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1970 if (bmvtype == BMV_TYPE_INTERPOLATED) {
1971 GET_MVDATA(dmv_x[0], dmv_y[0]);
1972 if (!mb_has_coeffs) {
1973 /* interpolated skipped block */
1974 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1975 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1979 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1981 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1984 s->ac_pred = get_bits1(gb);
1985 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1987 s->current_picture.qscale_table[mb_pos] = mquant;
1988 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1989 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1993 for (i = 0; i < 6; i++) {
1994 s->dc_val[0][s->block_index[i]] = 0;
1996 val = ((cbp >> (5 - i)) & 1);
1997 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1998 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2000 /* check if prediction blocks A and C are available */
2001 v->a_avail = v->c_avail = 0;
2002 if (i == 2 || i == 3 || !s->first_slice_line)
2003 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2004 if (i == 1 || i == 3 || s->mb_x)
2005 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2007 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2008 (i & 4) ? v->codingset2 : v->codingset);
2009 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2011 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2013 for (j = 0; j < 64; j++)
2014 s->block[i][j] <<= 1;
2015 s->idsp.put_signed_pixels_clamped(s->block[i],
2016 s->dest[dst_idx] + off,
2017 i & 4 ? s->uvlinesize
2020 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2021 first_block, s->dest[dst_idx] + off,
2022 (i & 4) ? s->uvlinesize : s->linesize,
2023 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2024 if (!v->ttmbf && ttmb < 8)
2031 /** Decode one B-frame MB (in interlaced field B picture)
2033 static void vc1_decode_b_mb_intfi(VC1Context *v)
2035 MpegEncContext *s = &v->s;
2036 GetBitContext *gb = &s->gb;
2038 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2039 int cbp = 0; /* cbp decoding stuff */
2040 int mqdiff, mquant; /* MB quantization */
2041 int ttmb = v->ttfrm; /* MB Transform type */
2042 int mb_has_coeffs = 0; /* last_flag */
2043 int val; /* temp value */
2044 int first_block = 1;
2047 int dmv_x[2], dmv_y[2], pred_flag[2];
2048 int bmvtype = BMV_TYPE_BACKWARD;
2051 mquant = v->pq; /* Lossy initialization */
2054 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2055 if (idx_mbmode <= 1) { // intra MB
2056 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2058 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2059 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2060 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2062 s->current_picture.qscale_table[mb_pos] = mquant;
2063 /* Set DC scale - y and c use the same (not sure if necessary here) */
2064 s->y_dc_scale = s->y_dc_scale_table[mquant];
2065 s->c_dc_scale = s->c_dc_scale_table[mquant];
2066 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2067 mb_has_coeffs = idx_mbmode & 1;
2069 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2071 for (i = 0; i < 6; i++) {
2072 v->a_avail = v->c_avail = 0;
2073 v->mb_type[0][s->block_index[i]] = 1;
2074 s->dc_val[0][s->block_index[i]] = 0;
2076 val = ((cbp >> (5 - i)) & 1);
2077 if (i == 2 || i == 3 || !s->first_slice_line)
2078 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2079 if (i == 1 || i == 3 || s->mb_x)
2080 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2082 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2083 (i & 4) ? v->codingset2 : v->codingset);
2084 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2086 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2088 for (j = 0; j < 64; j++)
2089 s->block[i][j] <<= 1;
2090 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2091 s->idsp.put_signed_pixels_clamped(s->block[i],
2092 s->dest[dst_idx] + off,
2093 (i & 4) ? s->uvlinesize
2095 // TODO: yet to perform loop filter
2098 s->mb_intra = v->is_intra[s->mb_x] = 0;
2099 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2100 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2102 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2104 fwd = v->forward_mb_plane[mb_pos];
2105 if (idx_mbmode <= 5) { // 1-MV
2107 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2108 pred_flag[0] = pred_flag[1] = 0;
2110 bmvtype = BMV_TYPE_FORWARD;
2112 bmvtype = decode012(gb);
2115 bmvtype = BMV_TYPE_BACKWARD;
2118 bmvtype = BMV_TYPE_DIRECT;
2121 bmvtype = BMV_TYPE_INTERPOLATED;
2122 interpmvp = get_bits1(gb);
2125 v->bmvtype = bmvtype;
2126 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2127 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2130 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2132 if (bmvtype == BMV_TYPE_DIRECT) {
2133 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2134 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2135 if (!s->next_picture_ptr->field_picture) {
2136 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2140 ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2141 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2142 mb_has_coeffs = !(idx_mbmode & 2);
2145 bmvtype = BMV_TYPE_FORWARD;
2146 v->bmvtype = bmvtype;
2147 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2148 for (i = 0; i < 4; i++) {
2149 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2150 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2151 if (v->fourmvbp & (8 >> i)) {
2152 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2153 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2154 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2156 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2157 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2159 ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2160 mb_has_coeffs = idx_mbmode & 1;
2163 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2167 s->current_picture.qscale_table[mb_pos] = mquant;
2168 if (!v->ttmbf && cbp) {
2169 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2172 for (i = 0; i < 6; i++) {
2173 s->dc_val[0][s->block_index[i]] = 0;
2175 val = ((cbp >> (5 - i)) & 1);
2176 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2178 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2179 first_block, s->dest[dst_idx] + off,
2180 (i & 4) ? s->uvlinesize : s->linesize,
2181 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2182 if (!v->ttmbf && ttmb < 8)
2190 /** Decode one B-frame MB (in interlaced frame B picture)
2192 static int vc1_decode_b_mb_intfr(VC1Context *v)
2194 MpegEncContext *s = &v->s;
2195 GetBitContext *gb = &s->gb;
2197 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2198 int cbp = 0; /* cbp decoding stuff */
2199 int mqdiff, mquant; /* MB quantization */
2200 int ttmb = v->ttfrm; /* MB Transform type */
2201 int mvsw = 0; /* motion vector switch */
2202 int mb_has_coeffs = 1; /* last_flag */
2203 int dmv_x, dmv_y; /* Differential MV components */
2204 int val; /* temp value */
2205 int first_block = 1;
2207 int skipped, direct, twomv = 0;
2208 int block_cbp = 0, pat, block_tt = 0;
2209 int idx_mbmode = 0, mvbp;
2210 int stride_y, fieldtx;
2211 int bmvtype = BMV_TYPE_BACKWARD;
2214 mquant = v->pq; /* Lossy initialization */
2217 skipped = get_bits1(gb);
2219 skipped = v->s.mbskip_table[mb_pos];
2222 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2223 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2225 v->blk_mv_type[s->block_index[0]] = 1;
2226 v->blk_mv_type[s->block_index[1]] = 1;
2227 v->blk_mv_type[s->block_index[2]] = 1;
2228 v->blk_mv_type[s->block_index[3]] = 1;
2230 v->blk_mv_type[s->block_index[0]] = 0;
2231 v->blk_mv_type[s->block_index[1]] = 0;
2232 v->blk_mv_type[s->block_index[2]] = 0;
2233 v->blk_mv_type[s->block_index[3]] = 0;
2238 direct = get_bits1(gb);
2240 direct = v->direct_mb_plane[mb_pos];
2243 if (s->next_picture_ptr->field_picture)
2244 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2245 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);
2246 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);
2247 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);
2248 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);
2251 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);
2252 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);
2253 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);
2254 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);
2256 for (i = 1; i < 4; i += 2) {
2257 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2258 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2259 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2260 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2263 for (i = 1; i < 4; i++) {
2264 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2265 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2266 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2267 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2272 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2273 for (i = 0; i < 4; i++) {
2274 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2275 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2276 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2277 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2279 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2281 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2282 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2283 mb_has_coeffs = get_bits1(gb);
2285 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2286 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2288 s->current_picture.qscale_table[mb_pos] = mquant;
2289 /* Set DC scale - y and c use the same (not sure if necessary here) */
2290 s->y_dc_scale = s->y_dc_scale_table[mquant];
2291 s->c_dc_scale = s->c_dc_scale_table[mquant];
2293 for (i = 0; i < 6; i++) {
2294 v->a_avail = v->c_avail = 0;
2295 v->mb_type[0][s->block_index[i]] = 1;
2296 s->dc_val[0][s->block_index[i]] = 0;
2298 val = ((cbp >> (5 - i)) & 1);
2299 if (i == 2 || i == 3 || !s->first_slice_line)
2300 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2301 if (i == 1 || i == 3 || s->mb_x)
2302 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2304 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2305 (i & 4) ? v->codingset2 : v->codingset);
2306 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
2308 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2310 stride_y = s->linesize << fieldtx;
2311 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2313 stride_y = s->uvlinesize;
2316 s->idsp.put_signed_pixels_clamped(s->block[i],
2317 s->dest[dst_idx] + off,
2321 s->mb_intra = v->is_intra[s->mb_x] = 0;
2323 if (skipped || !s->mb_intra) {
2324 bmvtype = decode012(gb);
2327 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2330 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2333 bmvtype = BMV_TYPE_INTERPOLATED;
2337 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2338 mvsw = get_bits1(gb);
2341 if (!skipped) { // inter MB
2342 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2344 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2346 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2347 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2348 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2349 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2353 for (i = 0; i < 6; i++)
2354 v->mb_type[0][s->block_index[i]] = 0;
2355 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2356 /* for all motion vector read MVDATA and motion compensate each block */
2360 for (i = 0; i < 4; i++) {
2361 ff_vc1_mc_4mv_luma(v, i, 0, 0);
2362 ff_vc1_mc_4mv_luma(v, i, 1, 1);
2364 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2365 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2367 ff_vc1_mc_1mv(v, 0);
2368 ff_vc1_interp_mc(v);
2370 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2372 for (i = 0; i < 4; i++) {
2375 val = ((mvbp >> (3 - i)) & 1);
2377 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2379 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2380 ff_vc1_mc_4mv_luma(v, j, dir, dir);
2381 ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2384 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2385 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2386 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2390 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2392 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2393 ff_vc1_mc_1mv(v, 0);
2397 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2399 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2400 ff_vc1_interp_mc(v);
2402 dir = bmvtype == BMV_TYPE_BACKWARD;
2409 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2410 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2414 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2415 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2418 for (i = 0; i < 2; i++) {
2419 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];
2420 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];
2421 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];
2422 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];
2425 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2426 ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2429 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2430 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2431 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2432 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2433 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2435 dir = bmvtype == BMV_TYPE_BACKWARD;
2437 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2440 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2442 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2443 v->blk_mv_type[s->block_index[0]] = 1;
2444 v->blk_mv_type[s->block_index[1]] = 1;
2445 v->blk_mv_type[s->block_index[2]] = 1;
2446 v->blk_mv_type[s->block_index[3]] = 1;
2447 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2448 for (i = 0; i < 2; i++) {
2449 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];
2450 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];
2452 ff_vc1_mc_1mv(v, dir);
2456 GET_MQUANT(); // p. 227
2457 s->current_picture.qscale_table[mb_pos] = mquant;
2458 if (!v->ttmbf && cbp)
2459 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2460 for (i = 0; i < 6; i++) {
2461 s->dc_val[0][s->block_index[i]] = 0;
2463 val = ((cbp >> (5 - i)) & 1);
2465 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2467 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2469 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2470 first_block, s->dest[dst_idx] + off,
2471 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2472 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
2473 block_cbp |= pat << (i << 2);
2474 if (!v->ttmbf && ttmb < 8)
2482 for (i = 0; i < 6; i++) {
2483 v->mb_type[0][s->block_index[i]] = 0;
2484 s->dc_val[0][s->block_index[i]] = 0;
2486 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2487 s->current_picture.qscale_table[mb_pos] = 0;
2488 v->blk_mv_type[s->block_index[0]] = 0;
2489 v->blk_mv_type[s->block_index[1]] = 0;
2490 v->blk_mv_type[s->block_index[2]] = 0;
2491 v->blk_mv_type[s->block_index[3]] = 0;
2494 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2495 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2496 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2498 dir = bmvtype == BMV_TYPE_BACKWARD;
2499 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2504 for (i = 0; i < 2; i++) {
2505 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];
2506 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];
2507 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];
2508 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];
2511 v->blk_mv_type[s->block_index[0]] = 1;
2512 v->blk_mv_type[s->block_index[1]] = 1;
2513 v->blk_mv_type[s->block_index[2]] = 1;
2514 v->blk_mv_type[s->block_index[3]] = 1;
2515 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2516 for (i = 0; i < 2; i++) {
2517 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];
2518 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];
2524 ff_vc1_mc_1mv(v, dir);
2525 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2526 ff_vc1_interp_mc(v);
2530 if (s->mb_x == s->mb_width - 1)
2531 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2532 v->cbp[s->mb_x] = block_cbp;
2533 v->ttblk[s->mb_x] = block_tt;
2537 /** Decode blocks of I-frame
2539 static void vc1_decode_i_blocks(VC1Context *v)
2542 MpegEncContext *s = &v->s;
2547 /* select codingmode used for VLC tables selection */
2548 switch (v->y_ac_table_index) {
2550 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2553 v->codingset = CS_HIGH_MOT_INTRA;
2556 v->codingset = CS_MID_RATE_INTRA;
2560 switch (v->c_ac_table_index) {
2562 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2565 v->codingset2 = CS_HIGH_MOT_INTER;
2568 v->codingset2 = CS_MID_RATE_INTER;
2572 /* Set DC scale - y and c use the same */
2573 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2574 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2577 s->mb_x = s->mb_y = 0;
2579 s->first_slice_line = 1;
2580 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2582 init_block_index(v);
2583 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2585 ff_update_block_index(s);
2586 dst[0] = s->dest[0];
2587 dst[1] = dst[0] + 8;
2588 dst[2] = s->dest[0] + s->linesize * 8;
2589 dst[3] = dst[2] + 8;
2590 dst[4] = s->dest[1];
2591 dst[5] = s->dest[2];
2592 s->bdsp.clear_blocks(s->block[0]);
2593 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2594 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2595 s->current_picture.qscale_table[mb_pos] = v->pq;
2596 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2597 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2599 // do actual MB decoding and displaying
2600 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2601 v->s.ac_pred = get_bits1(&v->s.gb);
2603 for (k = 0; k < 6; k++) {
2604 val = ((cbp >> (5 - k)) & 1);
2607 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2611 cbp |= val << (5 - k);
2613 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2615 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2617 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2618 if (v->pq >= 9 && v->overlap) {
2620 for (j = 0; j < 64; j++)
2621 s->block[k][j] <<= 1;
2622 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2623 k & 4 ? s->uvlinesize
2627 for (j = 0; j < 64; j++)
2628 s->block[k][j] = (s->block[k][j] - 64) << 1;
2629 s->idsp.put_pixels_clamped(s->block[k], dst[k],
2630 k & 4 ? s->uvlinesize
2635 if (v->pq >= 9 && v->overlap) {
2637 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2638 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2639 if (!(s->flags & CODEC_FLAG_GRAY)) {
2640 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2641 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2644 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2645 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2646 if (!s->first_slice_line) {
2647 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2648 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2649 if (!(s->flags & CODEC_FLAG_GRAY)) {
2650 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2651 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2654 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2655 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2657 if (v->s.loop_filter)
2658 ff_vc1_loop_filter_iblk(v, v->pq);
2660 if (get_bits_count(&s->gb) > v->bits) {
2661 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2662 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2663 get_bits_count(&s->gb), v->bits);
2667 if (!v->s.loop_filter)
2668 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2670 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2672 s->first_slice_line = 0;
2674 if (v->s.loop_filter)
2675 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2677 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2678 * profile, these only differ are when decoding MSS2 rectangles. */
2679 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2682 /** Decode blocks of I-frame for advanced profile
2684 static void vc1_decode_i_blocks_adv(VC1Context *v)
2687 MpegEncContext *s = &v->s;
2693 GetBitContext *gb = &s->gb;
2695 /* select codingmode used for VLC tables selection */
2696 switch (v->y_ac_table_index) {
2698 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2701 v->codingset = CS_HIGH_MOT_INTRA;
2704 v->codingset = CS_MID_RATE_INTRA;
2708 switch (v->c_ac_table_index) {
2710 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2713 v->codingset2 = CS_HIGH_MOT_INTER;
2716 v->codingset2 = CS_MID_RATE_INTER;
2721 s->mb_x = s->mb_y = 0;
2723 s->first_slice_line = 1;
2724 s->mb_y = s->start_mb_y;
2725 if (s->start_mb_y) {
2727 init_block_index(v);
2728 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2729 (1 + s->b8_stride) * sizeof(*s->coded_block));
2731 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2733 init_block_index(v);
2734 for (;s->mb_x < s->mb_width; s->mb_x++) {
2735 int16_t (*block)[64] = v->block[v->cur_blk_idx];
2736 ff_update_block_index(s);
2737 s->bdsp.clear_blocks(block[0]);
2738 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2739 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2740 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2741 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2743 // do actual MB decoding and displaying
2744 if (v->fieldtx_is_raw)
2745 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2746 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2747 if ( v->acpred_is_raw)
2748 v->s.ac_pred = get_bits1(&v->s.gb);
2750 v->s.ac_pred = v->acpred_plane[mb_pos];
2752 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2753 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2757 s->current_picture.qscale_table[mb_pos] = mquant;
2758 /* Set DC scale - y and c use the same */
2759 s->y_dc_scale = s->y_dc_scale_table[mquant];
2760 s->c_dc_scale = s->c_dc_scale_table[mquant];
2762 for (k = 0; k < 6; k++) {
2763 val = ((cbp >> (5 - k)) & 1);
2766 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2770 cbp |= val << (5 - k);
2772 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2773 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2775 vc1_decode_i_block_adv(v, block[k], k, val,
2776 (k < 4) ? v->codingset : v->codingset2, mquant);
2778 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2780 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2783 ff_vc1_smooth_overlap_filter_iblk(v);
2784 vc1_put_signed_blocks_clamped(v);
2785 if (v->s.loop_filter)
2786 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2788 if (get_bits_count(&s->gb) > v->bits) {
2789 // TODO: may need modification to handle slice coding
2790 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2791 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2792 get_bits_count(&s->gb), v->bits);
2796 if (!v->s.loop_filter)
2797 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2799 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2800 s->first_slice_line = 0;
2803 /* raw bottom MB row */
2805 init_block_index(v);
2807 for (;s->mb_x < s->mb_width; s->mb_x++) {
2808 ff_update_block_index(s);
2809 vc1_put_signed_blocks_clamped(v);
2810 if (v->s.loop_filter)
2811 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2813 if (v->s.loop_filter)
2814 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
2815 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2816 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2819 static void vc1_decode_p_blocks(VC1Context *v)
2821 MpegEncContext *s = &v->s;
2822 int apply_loop_filter;
2824 /* select codingmode used for VLC tables selection */
2825 switch (v->c_ac_table_index) {
2827 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2830 v->codingset = CS_HIGH_MOT_INTRA;
2833 v->codingset = CS_MID_RATE_INTRA;
2837 switch (v->c_ac_table_index) {
2839 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2842 v->codingset2 = CS_HIGH_MOT_INTER;
2845 v->codingset2 = CS_MID_RATE_INTER;
2849 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2850 v->fcm == PROGRESSIVE;
2851 s->first_slice_line = 1;
2852 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2853 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2855 init_block_index(v);
2856 for (; s->mb_x < s->mb_width; s->mb_x++) {
2857 ff_update_block_index(s);
2859 if (v->fcm == ILACE_FIELD)
2860 vc1_decode_p_mb_intfi(v);
2861 else if (v->fcm == ILACE_FRAME)
2862 vc1_decode_p_mb_intfr(v);
2863 else vc1_decode_p_mb(v);
2864 if (s->mb_y != s->start_mb_y && apply_loop_filter)
2865 ff_vc1_apply_p_loop_filter(v);
2866 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2867 // TODO: may need modification to handle slice coding
2868 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2869 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2870 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2874 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
2875 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
2876 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2877 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
2878 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2879 s->first_slice_line = 0;
2881 if (apply_loop_filter) {
2883 init_block_index(v);
2884 for (; s->mb_x < s->mb_width; s->mb_x++) {
2885 ff_update_block_index(s);
2886 ff_vc1_apply_p_loop_filter(v);
2889 if (s->end_mb_y >= s->start_mb_y)
2890 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2891 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2892 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2895 static void vc1_decode_b_blocks(VC1Context *v)
2897 MpegEncContext *s = &v->s;
2899 /* select codingmode used for VLC tables selection */
2900 switch (v->c_ac_table_index) {
2902 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2905 v->codingset = CS_HIGH_MOT_INTRA;
2908 v->codingset = CS_MID_RATE_INTRA;
2912 switch (v->c_ac_table_index) {
2914 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2917 v->codingset2 = CS_HIGH_MOT_INTER;
2920 v->codingset2 = CS_MID_RATE_INTER;
2924 s->first_slice_line = 1;
2925 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2927 init_block_index(v);
2928 for (; s->mb_x < s->mb_width; s->mb_x++) {
2929 ff_update_block_index(s);
2931 if (v->fcm == ILACE_FIELD)
2932 vc1_decode_b_mb_intfi(v);
2933 else if (v->fcm == ILACE_FRAME)
2934 vc1_decode_b_mb_intfr(v);
2937 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2938 // TODO: may need modification to handle slice coding
2939 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2940 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2941 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2944 if (v->s.loop_filter)
2945 ff_vc1_loop_filter_iblk(v, v->pq);
2947 if (!v->s.loop_filter)
2948 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2950 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2951 s->first_slice_line = 0;
2953 if (v->s.loop_filter)
2954 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2955 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2956 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2959 static void vc1_decode_skip_blocks(VC1Context *v)
2961 MpegEncContext *s = &v->s;
2963 if (!v->s.last_picture.f->data[0])
2966 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2967 s->first_slice_line = 1;
2968 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2970 init_block_index(v);
2971 ff_update_block_index(s);
2972 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2973 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2974 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2975 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2976 s->first_slice_line = 0;
2978 s->pict_type = AV_PICTURE_TYPE_P;
2981 void ff_vc1_decode_blocks(VC1Context *v)
2984 v->s.esc3_level_length = 0;
2986 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
2989 v->left_blk_idx = -1;
2990 v->topleft_blk_idx = 1;
2992 switch (v->s.pict_type) {
2993 case AV_PICTURE_TYPE_I:
2994 if (v->profile == PROFILE_ADVANCED)
2995 vc1_decode_i_blocks_adv(v);
2997 vc1_decode_i_blocks(v);
2999 case AV_PICTURE_TYPE_P:
3000 if (v->p_frame_skipped)
3001 vc1_decode_skip_blocks(v);
3003 vc1_decode_p_blocks(v);
3005 case AV_PICTURE_TYPE_B:
3007 if (v->profile == PROFILE_ADVANCED)
3008 vc1_decode_i_blocks_adv(v);
3010 vc1_decode_i_blocks(v);
3012 vc1_decode_b_blocks(v);