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 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
103 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
104 s->dest[1] - 8 * s->uvlinesize - 8,
106 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
107 s->dest[2] - 8 * s->uvlinesize - 8,
111 if (s->mb_x == s->mb_width - 1) {
112 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
113 if (v->fcm == ILACE_FRAME)
114 fieldtx = v->fieldtx_plane[top_mb_pos];
115 stride_y = s->linesize << fieldtx;
116 v_dist = fieldtx ? 15 : 8;
117 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
118 s->dest[0] - 16 * s->linesize,
120 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
121 s->dest[0] - 16 * s->linesize + 8,
123 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
124 s->dest[0] - v_dist * s->linesize,
126 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
127 s->dest[0] - v_dist * s->linesize + 8,
129 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
130 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
131 s->dest[1] - 8 * s->uvlinesize,
133 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
134 s->dest[2] - 8 * s->uvlinesize,
140 #define inc_blk_idx(idx) do { \
142 if (idx >= v->n_allocated_blks) \
146 inc_blk_idx(v->topleft_blk_idx);
147 inc_blk_idx(v->top_blk_idx);
148 inc_blk_idx(v->left_blk_idx);
149 inc_blk_idx(v->cur_blk_idx);
152 /***********************************************************************/
154 * @name VC-1 Block-level functions
155 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
161 * @brief Get macroblock-level quantizer scale
163 #define GET_MQUANT() \
164 if (v->dquantfrm) { \
166 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
167 if (v->dqbilevel) { \
168 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
170 mqdiff = get_bits(gb, 3); \
172 mquant = v->pq + mqdiff; \
174 mquant = get_bits(gb, 5); \
177 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
178 edges = 1 << v->dqsbedge; \
179 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
180 edges = (3 << v->dqsbedge) % 15; \
181 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
183 if ((edges&1) && !s->mb_x) \
185 if ((edges&2) && s->first_slice_line) \
187 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
189 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
191 if (!mquant || mquant > 31) { \
192 av_log(v->s.avctx, AV_LOG_ERROR, \
193 "Overriding invalid mquant %d\n", mquant); \
199 * @def GET_MVDATA(_dmv_x, _dmv_y)
200 * @brief Get MV differentials
201 * @see MVDATA decoding from 8.3.5.2, p(1)20
202 * @param _dmv_x Horizontal differential for decoded MV
203 * @param _dmv_y Vertical differential for decoded MV
205 #define GET_MVDATA(_dmv_x, _dmv_y) \
206 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
207 VC1_MV_DIFF_VLC_BITS, 2); \
215 _dmv_x = _dmv_y = 0; \
216 } else if (index == 35) { \
217 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
218 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
219 } else if (index == 36) { \
224 index1 = index % 6; \
225 _dmv_x = offset_table[1][index1]; \
226 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
228 val = get_bits(gb, val); \
229 sign = 0 - (val & 1); \
230 _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign; \
233 index1 = index / 6; \
234 _dmv_y = offset_table[1][index1]; \
235 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
237 val = get_bits(gb, val); \
238 sign = 0 - (val & 1); \
239 _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign; \
243 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
244 int *dmv_y, int *pred_flag)
247 int extend_x, extend_y;
248 GetBitContext *gb = &v->s.gb;
253 bits = VC1_2REF_MVDATA_VLC_BITS;
256 bits = VC1_1REF_MVDATA_VLC_BITS;
259 extend_x = v->dmvrange & 1;
260 extend_y = (v->dmvrange >> 1) & 1;
261 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
263 *dmv_x = get_bits(gb, v->k_x);
264 *dmv_y = get_bits(gb, v->k_y);
267 *pred_flag = *dmv_y & 1;
268 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
272 av_assert0(index < esc);
273 index1 = (index + 1) % 9;
275 val = get_bits(gb, index1 + extend_x);
276 sign = 0 - (val & 1);
277 *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
280 index1 = (index + 1) / 9;
281 if (index1 > v->numref) {
282 val = get_bits(gb, (index1 >> v->numref) + extend_y);
283 sign = 0 - (val & 1);
284 *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
287 if (v->numref && pred_flag)
288 *pred_flag = index1 & 1;
292 /** Reconstruct motion vector for B-frame and do motion compensation
294 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
295 int direct, int mode)
302 if (mode == BMV_TYPE_INTERPOLATED) {
308 ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
311 /** Get predicted DC value for I-frames only
312 * prediction dir: left=0, top=1
313 * @param s MpegEncContext
314 * @param overlap flag indicating that overlap filtering is used
315 * @param pq integer part of picture quantizer
316 * @param[in] n block index in the current MB
317 * @param dc_val_ptr Pointer to DC predictor
318 * @param dir_ptr Prediction direction for use in AC prediction
320 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
321 int16_t **dc_val_ptr, int *dir_ptr)
323 int a, b, c, wrap, pred, scale;
325 static const uint16_t dcpred[32] = {
326 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
327 114, 102, 93, 85, 79, 73, 68, 64,
328 60, 57, 54, 51, 49, 47, 45, 43,
329 41, 39, 38, 37, 35, 34, 33
332 /* find prediction - wmv3_dc_scale always used here in fact */
333 if (n < 4) scale = s->y_dc_scale;
334 else scale = s->c_dc_scale;
336 wrap = s->block_wrap[n];
337 dc_val = s->dc_val[0] + s->block_index[n];
343 b = dc_val[ - 1 - wrap];
346 if (pq < 9 || !overlap) {
347 /* Set outer values */
348 if (s->first_slice_line && (n != 2 && n != 3))
349 b = a = dcpred[scale];
350 if (s->mb_x == 0 && (n != 1 && n != 3))
351 b = c = dcpred[scale];
353 /* Set outer values */
354 if (s->first_slice_line && (n != 2 && n != 3))
356 if (s->mb_x == 0 && (n != 1 && n != 3))
360 if (abs(a - b) <= abs(b - c)) {
362 *dir_ptr = 1; // left
368 /* update predictor */
369 *dc_val_ptr = &dc_val[0];
374 /** Get predicted DC value
375 * prediction dir: left=0, top=1
376 * @param s MpegEncContext
377 * @param overlap flag indicating that overlap filtering is used
378 * @param pq integer part of picture quantizer
379 * @param[in] n block index in the current MB
380 * @param a_avail flag indicating top block availability
381 * @param c_avail flag indicating left block availability
382 * @param dc_val_ptr Pointer to DC predictor
383 * @param dir_ptr Prediction direction for use in AC prediction
385 static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
386 int a_avail, int c_avail,
387 int16_t **dc_val_ptr, int *dir_ptr)
389 int a, b, c, wrap, pred;
391 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
395 /* scale predictors if needed */
396 q1 = s->current_picture.qscale_table[mb_pos];
397 dqscale_index = s->y_dc_scale_table[q1] - 1;
398 if (dqscale_index < 0)
401 wrap = s->block_wrap[n];
402 dc_val = s->dc_val[0] + s->block_index[n];
408 b = dc_val[ - 1 - wrap];
411 if (c_avail && (n != 1 && n != 3)) {
412 q2 = s->current_picture.qscale_table[mb_pos - 1];
414 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
416 if (a_avail && (n != 2 && n != 3)) {
417 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
419 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
421 if (a_avail && c_avail && (n != 3)) {
427 q2 = s->current_picture.qscale_table[off];
429 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
432 if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
434 *dir_ptr = 1; // left
435 } else if (a_avail) {
440 *dir_ptr = 1; // left
443 /* update predictor */
444 *dc_val_ptr = &dc_val[0];
448 /** @} */ // Block group
451 * @name VC1 Macroblock-level functions in Simple/Main Profiles
452 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
456 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
457 uint8_t **coded_block_ptr)
459 int xy, wrap, pred, a, b, c;
461 xy = s->block_index[n];
467 a = s->coded_block[xy - 1 ];
468 b = s->coded_block[xy - 1 - wrap];
469 c = s->coded_block[xy - wrap];
478 *coded_block_ptr = &s->coded_block[xy];
484 * Decode one AC coefficient
485 * @param v The VC1 context
486 * @param last Last coefficient
487 * @param skip How much zero coefficients to skip
488 * @param value Decoded AC coefficient value
489 * @param codingset set of VLC to decode data
492 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
493 int *value, int codingset)
495 GetBitContext *gb = &v->s.gb;
496 int index, run, level, lst, sign;
498 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
499 if (index != ff_vc1_ac_sizes[codingset] - 1) {
500 run = vc1_index_decode_table[codingset][index][0];
501 level = vc1_index_decode_table[codingset][index][1];
502 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
503 sign = get_bits1(gb);
505 int escape = decode210(gb);
507 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
508 run = vc1_index_decode_table[codingset][index][0];
509 level = vc1_index_decode_table[codingset][index][1];
510 lst = index >= vc1_last_decode_table[codingset];
513 level += vc1_last_delta_level_table[codingset][run];
515 level += vc1_delta_level_table[codingset][run];
518 run += vc1_last_delta_run_table[codingset][level] + 1;
520 run += vc1_delta_run_table[codingset][level] + 1;
522 sign = get_bits1(gb);
525 if (v->s.esc3_level_length == 0) {
526 if (v->pq < 8 || v->dquantfrm) { // table 59
527 v->s.esc3_level_length = get_bits(gb, 3);
528 if (!v->s.esc3_level_length)
529 v->s.esc3_level_length = get_bits(gb, 2) + 8;
531 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
533 v->s.esc3_run_length = 3 + get_bits(gb, 2);
535 run = get_bits(gb, v->s.esc3_run_length);
536 sign = get_bits1(gb);
537 level = get_bits(gb, v->s.esc3_level_length);
543 *value = (level ^ -sign) + sign;
546 /** Decode intra block in intra frames - should be faster than decode_intra_block
547 * @param v VC1Context
548 * @param block block to decode
549 * @param[in] n subblock index
550 * @param coded are AC coeffs present or not
551 * @param codingset set of VLC to decode data
553 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
554 int coded, int codingset)
556 GetBitContext *gb = &v->s.gb;
557 MpegEncContext *s = &v->s;
558 int dc_pred_dir = 0; /* Direction of the DC prediction used */
561 int16_t *ac_val, *ac_val2;
564 /* Get DC differential */
566 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
568 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
571 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
575 const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
576 if (dcdiff == 119 /* ESC index value */) {
577 dcdiff = get_bits(gb, 8 + m);
580 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
587 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
590 /* Store the quantized DC coeff, used for prediction */
592 scale = s->y_dc_scale;
594 scale = s->c_dc_scale;
595 block[0] = dcdiff * scale;
597 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
599 if (dc_pred_dir) // left
602 ac_val -= 16 * s->block_wrap[n];
604 scale = v->pq * 2 + v->halfpq;
610 int last = 0, skip, value;
611 const uint8_t *zz_table;
616 zz_table = v->zz_8x8[2];
618 zz_table = v->zz_8x8[3];
620 zz_table = v->zz_8x8[1];
623 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
627 block[zz_table[i++]] = value;
630 /* apply AC prediction if needed */
633 if (dc_pred_dir) { // left
639 for (k = 1; k < 8; k++)
640 block[k << sh] += ac_val[k];
642 /* save AC coeffs for further prediction */
643 for (k = 1; k < 8; k++) {
644 ac_val2[k] = block[k << v->left_blk_sh];
645 ac_val2[k + 8] = block[k << v->top_blk_sh];
648 /* scale AC coeffs */
649 for (k = 1; k < 64; k++)
653 block[k] += (block[k] < 0) ? -v->pq : v->pq;
659 memset(ac_val2, 0, 16 * 2);
661 /* apply AC prediction if needed */
664 if (dc_pred_dir) { //left
671 memcpy(ac_val2, ac_val, 8 * 2);
672 for (k = 1; k < 8; k++) {
673 block[k << sh] = ac_val[k] * scale;
674 if (!v->pquantizer && block[k << sh])
675 block[k << sh] += (block[k << sh] < 0) ? -v->pq : v->pq;
679 if (s->ac_pred) i = 63;
680 s->block_last_index[n] = i;
685 /** Decode intra block in intra frames - should be faster than decode_intra_block
686 * @param v VC1Context
687 * @param block block to decode
688 * @param[in] n subblock number
689 * @param coded are AC coeffs present or not
690 * @param codingset set of VLC to decode data
691 * @param mquant quantizer value for this macroblock
693 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
694 int coded, int codingset, int mquant)
696 GetBitContext *gb = &v->s.gb;
697 MpegEncContext *s = &v->s;
698 int dc_pred_dir = 0; /* Direction of the DC prediction used */
700 int16_t *dc_val = NULL;
701 int16_t *ac_val, *ac_val2;
703 int a_avail = v->a_avail, c_avail = v->c_avail;
704 int use_pred = s->ac_pred;
707 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
709 /* Get DC differential */
711 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
713 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
716 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
720 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
721 if (dcdiff == 119 /* ESC index value */) {
722 dcdiff = get_bits(gb, 8 + m);
725 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
732 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
735 /* Store the quantized DC coeff, used for prediction */
737 scale = s->y_dc_scale;
739 scale = s->c_dc_scale;
740 block[0] = dcdiff * scale;
742 /* check if AC is needed at all */
743 if (!a_avail && !c_avail)
746 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
748 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
750 if (dc_pred_dir) // left
753 ac_val -= 16 * s->block_wrap[n];
755 q1 = s->current_picture.qscale_table[mb_pos];
758 else if (dc_pred_dir) {
761 else if (c_avail && mb_pos)
762 q2 = s->current_picture.qscale_table[mb_pos - 1];
766 else if (a_avail && mb_pos >= s->mb_stride)
767 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
774 int last = 0, skip, value;
775 const uint8_t *zz_table;
779 if (!use_pred && v->fcm == ILACE_FRAME) {
780 zz_table = v->zzi_8x8;
782 if (!dc_pred_dir) // top
783 zz_table = v->zz_8x8[2];
785 zz_table = v->zz_8x8[3];
788 if (v->fcm != ILACE_FRAME)
789 zz_table = v->zz_8x8[1];
791 zz_table = v->zzi_8x8;
795 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
799 block[zz_table[i++]] = value;
802 /* apply AC prediction if needed */
805 if (dc_pred_dir) { // left
811 /* scale predictors if needed*/
812 if (q2 && q1 != q2) {
813 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
815 return AVERROR_INVALIDDATA;
816 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
817 for (k = 1; k < 8; k++)
818 block[k << sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
820 for (k = 1; k < 8; k++)
821 block[k << sh] += ac_val[k];
824 /* save AC coeffs for further prediction */
825 for (k = 1; k < 8; k++) {
826 ac_val2[k ] = block[k << v->left_blk_sh];
827 ac_val2[k + 8] = block[k << v->top_blk_sh];
830 /* scale AC coeffs */
831 for (k = 1; k < 64; k++)
835 block[k] += (block[k] < 0) ? -mquant : mquant;
838 } else { // no AC coeffs
841 memset(ac_val2, 0, 16 * 2);
843 /* apply AC prediction if needed */
846 if (dc_pred_dir) { // left
853 memcpy(ac_val2, ac_val, 8 * 2);
854 if (q2 && q1 != q2) {
855 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
856 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
858 return AVERROR_INVALIDDATA;
859 for (k = 1; k < 8; k++)
860 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
862 for (k = 1; k < 8; k++) {
863 block[k << sh] = ac_val2[k] * scale;
864 if (!v->pquantizer && block[k << sh])
865 block[k << sh] += (block[k << sh] < 0) ? -mquant : mquant;
869 if (use_pred) i = 63;
870 s->block_last_index[n] = i;
875 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
876 * @param v VC1Context
877 * @param block block to decode
878 * @param[in] n subblock index
879 * @param coded are AC coeffs present or not
880 * @param mquant block quantizer
881 * @param codingset set of VLC to decode data
883 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
884 int coded, int mquant, int codingset)
886 GetBitContext *gb = &v->s.gb;
887 MpegEncContext *s = &v->s;
888 int dc_pred_dir = 0; /* Direction of the DC prediction used */
890 int16_t *dc_val = NULL;
891 int16_t *ac_val, *ac_val2;
893 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
894 int a_avail = v->a_avail, c_avail = v->c_avail;
895 int use_pred = s->ac_pred;
899 s->bdsp.clear_block(block);
901 /* XXX: Guard against dumb values of mquant */
902 mquant = av_clip_uintp2(mquant, 5);
904 /* Set DC scale - y and c use the same */
905 s->y_dc_scale = s->y_dc_scale_table[mquant];
906 s->c_dc_scale = s->c_dc_scale_table[mquant];
908 /* Get DC differential */
910 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
912 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
915 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
919 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
920 if (dcdiff == 119 /* ESC index value */) {
921 dcdiff = get_bits(gb, 8 + m);
924 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
931 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
934 /* Store the quantized DC coeff, used for prediction */
937 block[0] = dcdiff * s->y_dc_scale;
939 block[0] = dcdiff * s->c_dc_scale;
945 /* check if AC is needed at all and adjust direction if needed */
946 if (!a_avail) dc_pred_dir = 1;
947 if (!c_avail) dc_pred_dir = 0;
948 if (!a_avail && !c_avail) use_pred = 0;
949 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
952 scale = mquant * 2 + v->halfpq;
954 if (dc_pred_dir) //left
957 ac_val -= 16 * s->block_wrap[n];
959 q1 = s->current_picture.qscale_table[mb_pos];
960 if (dc_pred_dir && c_avail && mb_pos)
961 q2 = s->current_picture.qscale_table[mb_pos - 1];
962 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
963 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
964 if (dc_pred_dir && n == 1)
966 if (!dc_pred_dir && n == 2)
971 int last = 0, skip, value;
975 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
979 if (v->fcm == PROGRESSIVE)
980 block[v->zz_8x8[0][i++]] = value;
982 if (use_pred && (v->fcm == ILACE_FRAME)) {
983 if (!dc_pred_dir) // top
984 block[v->zz_8x8[2][i++]] = value;
986 block[v->zz_8x8[3][i++]] = value;
988 block[v->zzi_8x8[i++]] = value;
993 /* apply AC prediction if needed */
995 /* scale predictors if needed*/
996 if (q2 && q1 != q2) {
997 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
998 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1001 return AVERROR_INVALIDDATA;
1002 if (dc_pred_dir) { // left
1003 for (k = 1; k < 8; k++)
1004 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1006 for (k = 1; k < 8; k++)
1007 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1010 if (dc_pred_dir) { // left
1011 for (k = 1; k < 8; k++)
1012 block[k << v->left_blk_sh] += ac_val[k];
1014 for (k = 1; k < 8; k++)
1015 block[k << v->top_blk_sh] += ac_val[k + 8];
1019 /* save AC coeffs for further prediction */
1020 for (k = 1; k < 8; k++) {
1021 ac_val2[k ] = block[k << v->left_blk_sh];
1022 ac_val2[k + 8] = block[k << v->top_blk_sh];
1025 /* scale AC coeffs */
1026 for (k = 1; k < 64; k++)
1030 block[k] += (block[k] < 0) ? -mquant : mquant;
1033 if (use_pred) i = 63;
1034 } else { // no AC coeffs
1037 memset(ac_val2, 0, 16 * 2);
1038 if (dc_pred_dir) { // left
1040 memcpy(ac_val2, ac_val, 8 * 2);
1041 if (q2 && q1 != q2) {
1042 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1043 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1045 return AVERROR_INVALIDDATA;
1046 for (k = 1; k < 8; k++)
1047 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1052 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1053 if (q2 && q1 != q2) {
1054 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1055 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1057 return AVERROR_INVALIDDATA;
1058 for (k = 1; k < 8; k++)
1059 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1064 /* apply AC prediction if needed */
1066 if (dc_pred_dir) { // left
1067 for (k = 1; k < 8; k++) {
1068 block[k << v->left_blk_sh] = ac_val2[k] * scale;
1069 if (!v->pquantizer && block[k << v->left_blk_sh])
1070 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1073 for (k = 1; k < 8; k++) {
1074 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1075 if (!v->pquantizer && block[k << v->top_blk_sh])
1076 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1082 s->block_last_index[n] = i;
1089 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1090 int mquant, int ttmb, int first_block,
1091 uint8_t *dst, int linesize, int skip_block,
1094 MpegEncContext *s = &v->s;
1095 GetBitContext *gb = &s->gb;
1098 int scale, off, idx, last, skip, value;
1099 int ttblk = ttmb & 7;
1102 s->bdsp.clear_block(block);
1105 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)];
1107 if (ttblk == TT_4X4) {
1108 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1110 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1111 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1112 || (!v->res_rtm_flag && !first_block))) {
1113 subblkpat = decode012(gb);
1115 subblkpat ^= 3; // swap decoded pattern bits
1116 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1118 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1121 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1123 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1124 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1125 subblkpat = 2 - (ttblk == TT_8X4_TOP);
1128 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1129 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1138 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1143 idx = v->zz_8x8[0][i++];
1145 idx = v->zzi_8x8[i++];
1146 block[idx] = value * scale;
1148 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1152 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1154 v->vc1dsp.vc1_inv_trans_8x8(block);
1155 s->idsp.add_pixels_clamped(block, dst, linesize);
1160 pat = ~subblkpat & 0xF;
1161 for (j = 0; j < 4; j++) {
1162 last = subblkpat & (1 << (3 - j));
1164 off = (j & 1) * 4 + (j & 2) * 16;
1166 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1171 idx = ff_vc1_simple_progressive_4x4_zz[i++];
1173 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1174 block[idx + off] = value * scale;
1176 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1178 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1180 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1182 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1187 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1188 for (j = 0; j < 2; j++) {
1189 last = subblkpat & (1 << (1 - j));
1193 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1198 idx = v->zz_8x4[i++] + off;
1200 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1201 block[idx] = value * scale;
1203 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1205 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1207 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1209 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1214 pat = ~(subblkpat * 5) & 0xF;
1215 for (j = 0; j < 2; j++) {
1216 last = subblkpat & (1 << (1 - j));
1220 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1225 idx = v->zz_4x8[i++] + off;
1227 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1228 block[idx] = value * scale;
1230 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1232 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1234 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1236 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1242 *ttmb_out |= ttblk << (n * 4);
1246 /** @} */ // Macroblock group
1248 static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 };
1250 /** Decode one P-frame MB
1252 static int vc1_decode_p_mb(VC1Context *v)
1254 MpegEncContext *s = &v->s;
1255 GetBitContext *gb = &s->gb;
1257 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1258 int cbp; /* cbp decoding stuff */
1259 int mqdiff, mquant; /* MB quantization */
1260 int ttmb = v->ttfrm; /* MB Transform type */
1262 int mb_has_coeffs = 1; /* last_flag */
1263 int dmv_x, dmv_y; /* Differential MV components */
1264 int index, index1; /* LUT indexes */
1265 int val, sign; /* temp values */
1266 int first_block = 1;
1268 int skipped, fourmv;
1269 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1271 mquant = v->pq; /* lossy initialization */
1273 if (v->mv_type_is_raw)
1274 fourmv = get_bits1(gb);
1276 fourmv = v->mv_type_mb_plane[mb_pos];
1278 skipped = get_bits1(gb);
1280 skipped = v->s.mbskip_table[mb_pos];
1282 if (!fourmv) { /* 1MV mode */
1284 GET_MVDATA(dmv_x, dmv_y);
1287 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1288 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1290 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1291 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1293 /* FIXME Set DC val for inter block ? */
1294 if (s->mb_intra && !mb_has_coeffs) {
1296 s->ac_pred = get_bits1(gb);
1298 } else if (mb_has_coeffs) {
1300 s->ac_pred = get_bits1(gb);
1301 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1307 s->current_picture.qscale_table[mb_pos] = mquant;
1309 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1310 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1311 VC1_TTMB_VLC_BITS, 2);
1312 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1314 for (i = 0; i < 6; i++) {
1315 s->dc_val[0][s->block_index[i]] = 0;
1317 val = ((cbp >> (5 - i)) & 1);
1318 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1319 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1321 /* check if prediction blocks A and C are available */
1322 v->a_avail = v->c_avail = 0;
1323 if (i == 2 || i == 3 || !s->first_slice_line)
1324 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1325 if (i == 1 || i == 3 || s->mb_x)
1326 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1328 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1329 (i & 4) ? v->codingset2 : v->codingset);
1330 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1332 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1334 for (j = 0; j < 64; j++)
1335 s->block[i][j] <<= 1;
1336 s->idsp.put_signed_pixels_clamped(s->block[i],
1337 s->dest[dst_idx] + off,
1338 i & 4 ? s->uvlinesize
1340 if (v->pq >= 9 && v->overlap) {
1342 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1344 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1346 block_cbp |= 0xF << (i << 2);
1347 block_intra |= 1 << i;
1349 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
1350 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1351 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1352 block_cbp |= pat << (i << 2);
1353 if (!v->ttmbf && ttmb < 8)
1360 for (i = 0; i < 6; i++) {
1361 v->mb_type[0][s->block_index[i]] = 0;
1362 s->dc_val[0][s->block_index[i]] = 0;
1364 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1365 s->current_picture.qscale_table[mb_pos] = 0;
1366 ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1367 ff_vc1_mc_1mv(v, 0);
1369 } else { // 4MV mode
1370 if (!skipped /* unskipped MB */) {
1371 int intra_count = 0, coded_inter = 0;
1372 int is_intra[6], is_coded[6];
1374 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1375 for (i = 0; i < 6; i++) {
1376 val = ((cbp >> (5 - i)) & 1);
1377 s->dc_val[0][s->block_index[i]] = 0;
1384 GET_MVDATA(dmv_x, dmv_y);
1386 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1388 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1389 intra_count += s->mb_intra;
1390 is_intra[i] = s->mb_intra;
1391 is_coded[i] = mb_has_coeffs;
1394 is_intra[i] = (intra_count >= 3);
1398 ff_vc1_mc_4mv_chroma(v, 0);
1399 v->mb_type[0][s->block_index[i]] = is_intra[i];
1401 coded_inter = !is_intra[i] & is_coded[i];
1403 // if there are no coded blocks then don't do anything more
1405 if (!intra_count && !coded_inter)
1408 s->current_picture.qscale_table[mb_pos] = mquant;
1409 /* test if block is intra and has pred */
1412 for (i = 0; i < 6; i++)
1414 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1415 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1421 s->ac_pred = get_bits1(gb);
1425 if (!v->ttmbf && coded_inter)
1426 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1427 for (i = 0; i < 6; i++) {
1429 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1430 s->mb_intra = is_intra[i];
1432 /* check if prediction blocks A and C are available */
1433 v->a_avail = v->c_avail = 0;
1434 if (i == 2 || i == 3 || !s->first_slice_line)
1435 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1436 if (i == 1 || i == 3 || s->mb_x)
1437 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1439 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
1440 (i & 4) ? v->codingset2 : v->codingset);
1441 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1443 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1445 for (j = 0; j < 64; j++)
1446 s->block[i][j] <<= 1;
1447 s->idsp.put_signed_pixels_clamped(s->block[i],
1448 s->dest[dst_idx] + off,
1449 (i & 4) ? s->uvlinesize
1451 if (v->pq >= 9 && v->overlap) {
1453 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1455 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1457 block_cbp |= 0xF << (i << 2);
1458 block_intra |= 1 << i;
1459 } else if (is_coded[i]) {
1460 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1461 first_block, s->dest[dst_idx] + off,
1462 (i & 4) ? s->uvlinesize : s->linesize,
1463 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1465 block_cbp |= pat << (i << 2);
1466 if (!v->ttmbf && ttmb < 8)
1471 } else { // skipped MB
1473 s->current_picture.qscale_table[mb_pos] = 0;
1474 for (i = 0; i < 6; i++) {
1475 v->mb_type[0][s->block_index[i]] = 0;
1476 s->dc_val[0][s->block_index[i]] = 0;
1478 for (i = 0; i < 4; i++) {
1479 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1480 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1482 ff_vc1_mc_4mv_chroma(v, 0);
1483 s->current_picture.qscale_table[mb_pos] = 0;
1487 v->cbp[s->mb_x] = block_cbp;
1488 v->ttblk[s->mb_x] = block_tt;
1489 v->is_intra[s->mb_x] = block_intra;
1494 /* Decode one macroblock in an interlaced frame p picture */
1496 static int vc1_decode_p_mb_intfr(VC1Context *v)
1498 MpegEncContext *s = &v->s;
1499 GetBitContext *gb = &s->gb;
1501 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1502 int cbp = 0; /* cbp decoding stuff */
1503 int mqdiff, mquant; /* MB quantization */
1504 int ttmb = v->ttfrm; /* MB Transform type */
1506 int mb_has_coeffs = 1; /* last_flag */
1507 int dmv_x, dmv_y; /* Differential MV components */
1508 int val; /* temp value */
1509 int first_block = 1;
1511 int skipped, fourmv = 0, twomv = 0;
1512 int block_cbp = 0, pat, block_tt = 0;
1513 int idx_mbmode = 0, mvbp;
1514 int stride_y, fieldtx;
1516 mquant = v->pq; /* Lossy initialization */
1519 skipped = get_bits1(gb);
1521 skipped = v->s.mbskip_table[mb_pos];
1523 if (v->fourmvswitch)
1524 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1526 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1527 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1528 /* store the motion vector type in a flag (useful later) */
1529 case MV_PMODE_INTFR_4MV:
1531 v->blk_mv_type[s->block_index[0]] = 0;
1532 v->blk_mv_type[s->block_index[1]] = 0;
1533 v->blk_mv_type[s->block_index[2]] = 0;
1534 v->blk_mv_type[s->block_index[3]] = 0;
1536 case MV_PMODE_INTFR_4MV_FIELD:
1538 v->blk_mv_type[s->block_index[0]] = 1;
1539 v->blk_mv_type[s->block_index[1]] = 1;
1540 v->blk_mv_type[s->block_index[2]] = 1;
1541 v->blk_mv_type[s->block_index[3]] = 1;
1543 case MV_PMODE_INTFR_2MV_FIELD:
1545 v->blk_mv_type[s->block_index[0]] = 1;
1546 v->blk_mv_type[s->block_index[1]] = 1;
1547 v->blk_mv_type[s->block_index[2]] = 1;
1548 v->blk_mv_type[s->block_index[3]] = 1;
1550 case MV_PMODE_INTFR_1MV:
1551 v->blk_mv_type[s->block_index[0]] = 0;
1552 v->blk_mv_type[s->block_index[1]] = 0;
1553 v->blk_mv_type[s->block_index[2]] = 0;
1554 v->blk_mv_type[s->block_index[3]] = 0;
1557 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1558 for (i = 0; i < 4; i++) {
1559 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1560 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1562 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1564 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1565 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1566 mb_has_coeffs = get_bits1(gb);
1568 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1569 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1571 s->current_picture.qscale_table[mb_pos] = mquant;
1572 /* Set DC scale - y and c use the same (not sure if necessary here) */
1573 s->y_dc_scale = s->y_dc_scale_table[mquant];
1574 s->c_dc_scale = s->c_dc_scale_table[mquant];
1576 for (i = 0; i < 6; i++) {
1577 v->a_avail = v->c_avail = 0;
1578 v->mb_type[0][s->block_index[i]] = 1;
1579 s->dc_val[0][s->block_index[i]] = 0;
1581 val = ((cbp >> (5 - i)) & 1);
1582 if (i == 2 || i == 3 || !s->first_slice_line)
1583 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1584 if (i == 1 || i == 3 || s->mb_x)
1585 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1587 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1588 (i & 4) ? v->codingset2 : v->codingset);
1589 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1591 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1593 stride_y = s->linesize << fieldtx;
1594 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1596 stride_y = s->uvlinesize;
1599 s->idsp.put_signed_pixels_clamped(s->block[i],
1600 s->dest[dst_idx] + off,
1605 } else { // inter MB
1606 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1608 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1609 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1610 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1612 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1613 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1614 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1617 s->mb_intra = v->is_intra[s->mb_x] = 0;
1618 for (i = 0; i < 6; i++)
1619 v->mb_type[0][s->block_index[i]] = 0;
1620 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1621 /* for all motion vector read MVDATA and motion compensate each block */
1625 for (i = 0; i < 4; i++) {
1627 if (mvbp & (8 >> i))
1628 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1629 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1630 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1632 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1637 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1639 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1640 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1641 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1644 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1646 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1647 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1648 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1649 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1651 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1654 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1656 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1657 ff_vc1_mc_1mv(v, 0);
1660 GET_MQUANT(); // p. 227
1661 s->current_picture.qscale_table[mb_pos] = mquant;
1662 if (!v->ttmbf && cbp)
1663 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1664 for (i = 0; i < 6; i++) {
1665 s->dc_val[0][s->block_index[i]] = 0;
1667 val = ((cbp >> (5 - i)) & 1);
1669 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1671 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1673 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1674 first_block, s->dest[dst_idx] + off,
1675 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1676 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1677 block_cbp |= pat << (i << 2);
1678 if (!v->ttmbf && ttmb < 8)
1685 s->mb_intra = v->is_intra[s->mb_x] = 0;
1686 for (i = 0; i < 6; i++) {
1687 v->mb_type[0][s->block_index[i]] = 0;
1688 s->dc_val[0][s->block_index[i]] = 0;
1690 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1691 s->current_picture.qscale_table[mb_pos] = 0;
1692 v->blk_mv_type[s->block_index[0]] = 0;
1693 v->blk_mv_type[s->block_index[1]] = 0;
1694 v->blk_mv_type[s->block_index[2]] = 0;
1695 v->blk_mv_type[s->block_index[3]] = 0;
1696 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1697 ff_vc1_mc_1mv(v, 0);
1699 if (s->mb_x == s->mb_width - 1)
1700 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
1704 static int vc1_decode_p_mb_intfi(VC1Context *v)
1706 MpegEncContext *s = &v->s;
1707 GetBitContext *gb = &s->gb;
1709 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1710 int cbp = 0; /* cbp decoding stuff */
1711 int mqdiff, mquant; /* MB quantization */
1712 int ttmb = v->ttfrm; /* MB Transform type */
1714 int mb_has_coeffs = 1; /* last_flag */
1715 int dmv_x, dmv_y; /* Differential MV components */
1716 int val; /* temp values */
1717 int first_block = 1;
1720 int block_cbp = 0, pat, block_tt = 0;
1723 mquant = v->pq; /* Lossy initialization */
1725 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1726 if (idx_mbmode <= 1) { // intra MB
1727 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1729 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1730 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1731 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1733 s->current_picture.qscale_table[mb_pos] = mquant;
1734 /* Set DC scale - y and c use the same (not sure if necessary here) */
1735 s->y_dc_scale = s->y_dc_scale_table[mquant];
1736 s->c_dc_scale = s->c_dc_scale_table[mquant];
1737 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1738 mb_has_coeffs = idx_mbmode & 1;
1740 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1742 for (i = 0; i < 6; i++) {
1743 v->a_avail = v->c_avail = 0;
1744 v->mb_type[0][s->block_index[i]] = 1;
1745 s->dc_val[0][s->block_index[i]] = 0;
1747 val = ((cbp >> (5 - i)) & 1);
1748 if (i == 2 || i == 3 || !s->first_slice_line)
1749 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1750 if (i == 1 || i == 3 || s->mb_x)
1751 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1753 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1754 (i & 4) ? v->codingset2 : v->codingset);
1755 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1757 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1758 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1759 s->idsp.put_signed_pixels_clamped(s->block[i],
1760 s->dest[dst_idx] + off,
1761 (i & 4) ? s->uvlinesize
1763 // TODO: loop filter
1766 s->mb_intra = v->is_intra[s->mb_x] = 0;
1767 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1768 for (i = 0; i < 6; i++)
1769 v->mb_type[0][s->block_index[i]] = 0;
1770 if (idx_mbmode <= 5) { // 1-MV
1771 dmv_x = dmv_y = pred_flag = 0;
1772 if (idx_mbmode & 1) {
1773 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1775 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1776 ff_vc1_mc_1mv(v, 0);
1777 mb_has_coeffs = !(idx_mbmode & 2);
1779 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1780 for (i = 0; i < 4; i++) {
1781 dmv_x = dmv_y = pred_flag = 0;
1782 if (v->fourmvbp & (8 >> i))
1783 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1784 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1785 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1787 ff_vc1_mc_4mv_chroma(v, 0);
1788 mb_has_coeffs = idx_mbmode & 1;
1791 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1795 s->current_picture.qscale_table[mb_pos] = mquant;
1796 if (!v->ttmbf && cbp) {
1797 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1800 for (i = 0; i < 6; i++) {
1801 s->dc_val[0][s->block_index[i]] = 0;
1803 val = ((cbp >> (5 - i)) & 1);
1804 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1806 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1807 first_block, s->dest[dst_idx] + off,
1808 (i & 4) ? s->uvlinesize : s->linesize,
1809 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1811 block_cbp |= pat << (i << 2);
1812 if (!v->ttmbf && ttmb < 8)
1818 if (s->mb_x == s->mb_width - 1)
1819 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1823 /** Decode one B-frame MB (in Main profile)
1825 static void vc1_decode_b_mb(VC1Context *v)
1827 MpegEncContext *s = &v->s;
1828 GetBitContext *gb = &s->gb;
1830 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1831 int cbp = 0; /* cbp decoding stuff */
1832 int mqdiff, mquant; /* MB quantization */
1833 int ttmb = v->ttfrm; /* MB Transform type */
1834 int mb_has_coeffs = 0; /* last_flag */
1835 int index, index1; /* LUT indexes */
1836 int val, sign; /* temp values */
1837 int first_block = 1;
1839 int skipped, direct;
1840 int dmv_x[2], dmv_y[2];
1841 int bmvtype = BMV_TYPE_BACKWARD;
1843 mquant = v->pq; /* lossy initialization */
1847 direct = get_bits1(gb);
1849 direct = v->direct_mb_plane[mb_pos];
1851 skipped = get_bits1(gb);
1853 skipped = v->s.mbskip_table[mb_pos];
1855 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1856 for (i = 0; i < 6; i++) {
1857 v->mb_type[0][s->block_index[i]] = 0;
1858 s->dc_val[0][s->block_index[i]] = 0;
1860 s->current_picture.qscale_table[mb_pos] = 0;
1864 GET_MVDATA(dmv_x[0], dmv_y[0]);
1865 dmv_x[1] = dmv_x[0];
1866 dmv_y[1] = dmv_y[0];
1868 if (skipped || !s->mb_intra) {
1869 bmvtype = decode012(gb);
1872 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1875 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1878 bmvtype = BMV_TYPE_INTERPOLATED;
1879 dmv_x[0] = dmv_y[0] = 0;
1883 for (i = 0; i < 6; i++)
1884 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1888 bmvtype = BMV_TYPE_INTERPOLATED;
1889 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1890 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1894 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1897 s->current_picture.qscale_table[mb_pos] = mquant;
1899 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1900 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1901 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1902 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1904 if (!mb_has_coeffs && !s->mb_intra) {
1905 /* no coded blocks - effectively skipped */
1906 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1907 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1910 if (s->mb_intra && !mb_has_coeffs) {
1912 s->current_picture.qscale_table[mb_pos] = mquant;
1913 s->ac_pred = get_bits1(gb);
1915 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1917 if (bmvtype == BMV_TYPE_INTERPOLATED) {
1918 GET_MVDATA(dmv_x[0], dmv_y[0]);
1919 if (!mb_has_coeffs) {
1920 /* interpolated skipped block */
1921 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1922 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1926 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1928 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1931 s->ac_pred = get_bits1(gb);
1932 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1934 s->current_picture.qscale_table[mb_pos] = mquant;
1935 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1936 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1940 for (i = 0; i < 6; i++) {
1941 s->dc_val[0][s->block_index[i]] = 0;
1943 val = ((cbp >> (5 - i)) & 1);
1944 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1945 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1947 /* check if prediction blocks A and C are available */
1948 v->a_avail = v->c_avail = 0;
1949 if (i == 2 || i == 3 || !s->first_slice_line)
1950 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1951 if (i == 1 || i == 3 || s->mb_x)
1952 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1954 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1955 (i & 4) ? v->codingset2 : v->codingset);
1956 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1958 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1960 for (j = 0; j < 64; j++)
1961 s->block[i][j] <<= 1;
1962 s->idsp.put_signed_pixels_clamped(s->block[i],
1963 s->dest[dst_idx] + off,
1964 i & 4 ? s->uvlinesize
1967 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1968 first_block, s->dest[dst_idx] + off,
1969 (i & 4) ? s->uvlinesize : s->linesize,
1970 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
1971 if (!v->ttmbf && ttmb < 8)
1978 /** Decode one B-frame MB (in interlaced field B picture)
1980 static void vc1_decode_b_mb_intfi(VC1Context *v)
1982 MpegEncContext *s = &v->s;
1983 GetBitContext *gb = &s->gb;
1985 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1986 int cbp = 0; /* cbp decoding stuff */
1987 int mqdiff, mquant; /* MB quantization */
1988 int ttmb = v->ttfrm; /* MB Transform type */
1989 int mb_has_coeffs = 0; /* last_flag */
1990 int val; /* temp value */
1991 int first_block = 1;
1994 int dmv_x[2], dmv_y[2], pred_flag[2];
1995 int bmvtype = BMV_TYPE_BACKWARD;
1998 mquant = v->pq; /* Lossy initialization */
2001 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2002 if (idx_mbmode <= 1) { // intra MB
2003 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2005 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2006 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2007 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2009 s->current_picture.qscale_table[mb_pos] = mquant;
2010 /* Set DC scale - y and c use the same (not sure if necessary here) */
2011 s->y_dc_scale = s->y_dc_scale_table[mquant];
2012 s->c_dc_scale = s->c_dc_scale_table[mquant];
2013 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2014 mb_has_coeffs = idx_mbmode & 1;
2016 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2018 for (i = 0; i < 6; i++) {
2019 v->a_avail = v->c_avail = 0;
2020 v->mb_type[0][s->block_index[i]] = 1;
2021 s->dc_val[0][s->block_index[i]] = 0;
2023 val = ((cbp >> (5 - i)) & 1);
2024 if (i == 2 || i == 3 || !s->first_slice_line)
2025 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2026 if (i == 1 || i == 3 || s->mb_x)
2027 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2029 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2030 (i & 4) ? v->codingset2 : v->codingset);
2031 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2033 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2035 for (j = 0; j < 64; j++)
2036 s->block[i][j] <<= 1;
2037 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2038 s->idsp.put_signed_pixels_clamped(s->block[i],
2039 s->dest[dst_idx] + off,
2040 (i & 4) ? s->uvlinesize
2042 // TODO: yet to perform loop filter
2045 s->mb_intra = v->is_intra[s->mb_x] = 0;
2046 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2047 for (i = 0; i < 6; i++)
2048 v->mb_type[0][s->block_index[i]] = 0;
2050 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2052 fwd = v->forward_mb_plane[mb_pos];
2053 if (idx_mbmode <= 5) { // 1-MV
2055 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2056 pred_flag[0] = pred_flag[1] = 0;
2058 bmvtype = BMV_TYPE_FORWARD;
2060 bmvtype = decode012(gb);
2063 bmvtype = BMV_TYPE_BACKWARD;
2066 bmvtype = BMV_TYPE_DIRECT;
2069 bmvtype = BMV_TYPE_INTERPOLATED;
2070 interpmvp = get_bits1(gb);
2073 v->bmvtype = bmvtype;
2074 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2075 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2078 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2080 if (bmvtype == BMV_TYPE_DIRECT) {
2081 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2082 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2083 if (!s->next_picture_ptr->field_picture) {
2084 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2088 ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2089 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2090 mb_has_coeffs = !(idx_mbmode & 2);
2093 bmvtype = BMV_TYPE_FORWARD;
2094 v->bmvtype = bmvtype;
2095 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2096 for (i = 0; i < 4; i++) {
2097 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2098 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2099 if (v->fourmvbp & (8 >> i)) {
2100 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2101 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2102 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2104 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2105 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2107 ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2108 mb_has_coeffs = idx_mbmode & 1;
2111 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2115 s->current_picture.qscale_table[mb_pos] = mquant;
2116 if (!v->ttmbf && cbp) {
2117 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2120 for (i = 0; i < 6; i++) {
2121 s->dc_val[0][s->block_index[i]] = 0;
2123 val = ((cbp >> (5 - i)) & 1);
2124 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2126 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2127 first_block, s->dest[dst_idx] + off,
2128 (i & 4) ? s->uvlinesize : s->linesize,
2129 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
2130 if (!v->ttmbf && ttmb < 8)
2138 /** Decode one B-frame MB (in interlaced frame B picture)
2140 static int vc1_decode_b_mb_intfr(VC1Context *v)
2142 MpegEncContext *s = &v->s;
2143 GetBitContext *gb = &s->gb;
2145 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2146 int cbp = 0; /* cbp decoding stuff */
2147 int mqdiff, mquant; /* MB quantization */
2148 int ttmb = v->ttfrm; /* MB Transform type */
2149 int mvsw = 0; /* motion vector switch */
2150 int mb_has_coeffs = 1; /* last_flag */
2151 int dmv_x, dmv_y; /* Differential MV components */
2152 int val; /* temp value */
2153 int first_block = 1;
2155 int skipped, direct, twomv = 0;
2156 int block_cbp = 0, pat, block_tt = 0;
2157 int idx_mbmode = 0, mvbp;
2158 int stride_y, fieldtx;
2159 int bmvtype = BMV_TYPE_BACKWARD;
2162 mquant = v->pq; /* Lossy initialization */
2165 skipped = get_bits1(gb);
2167 skipped = v->s.mbskip_table[mb_pos];
2170 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2171 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2173 v->blk_mv_type[s->block_index[0]] = 1;
2174 v->blk_mv_type[s->block_index[1]] = 1;
2175 v->blk_mv_type[s->block_index[2]] = 1;
2176 v->blk_mv_type[s->block_index[3]] = 1;
2178 v->blk_mv_type[s->block_index[0]] = 0;
2179 v->blk_mv_type[s->block_index[1]] = 0;
2180 v->blk_mv_type[s->block_index[2]] = 0;
2181 v->blk_mv_type[s->block_index[3]] = 0;
2186 direct = get_bits1(gb);
2188 direct = v->direct_mb_plane[mb_pos];
2191 if (s->next_picture_ptr->field_picture)
2192 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2193 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);
2194 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);
2195 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);
2196 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);
2199 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);
2200 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);
2201 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);
2202 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);
2204 for (i = 1; i < 4; i += 2) {
2205 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2206 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2207 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2208 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2211 for (i = 1; i < 4; i++) {
2212 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2213 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2214 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2215 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2220 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2221 for (i = 0; i < 4; i++) {
2222 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2223 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2224 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2225 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2227 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2229 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2230 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2231 mb_has_coeffs = get_bits1(gb);
2233 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2234 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2236 s->current_picture.qscale_table[mb_pos] = mquant;
2237 /* Set DC scale - y and c use the same (not sure if necessary here) */
2238 s->y_dc_scale = s->y_dc_scale_table[mquant];
2239 s->c_dc_scale = s->c_dc_scale_table[mquant];
2241 for (i = 0; i < 6; i++) {
2242 v->a_avail = v->c_avail = 0;
2243 v->mb_type[0][s->block_index[i]] = 1;
2244 s->dc_val[0][s->block_index[i]] = 0;
2246 val = ((cbp >> (5 - i)) & 1);
2247 if (i == 2 || i == 3 || !s->first_slice_line)
2248 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2249 if (i == 1 || i == 3 || s->mb_x)
2250 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2252 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2253 (i & 4) ? v->codingset2 : v->codingset);
2254 if (CONFIG_GRAY && i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2256 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2258 stride_y = s->linesize << fieldtx;
2259 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2261 stride_y = s->uvlinesize;
2264 s->idsp.put_signed_pixels_clamped(s->block[i],
2265 s->dest[dst_idx] + off,
2269 s->mb_intra = v->is_intra[s->mb_x] = 0;
2271 if (skipped || !s->mb_intra) {
2272 bmvtype = decode012(gb);
2275 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2278 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2281 bmvtype = BMV_TYPE_INTERPOLATED;
2285 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2286 mvsw = get_bits1(gb);
2289 if (!skipped) { // inter MB
2290 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2292 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2294 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2295 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2296 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2297 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2301 for (i = 0; i < 6; i++)
2302 v->mb_type[0][s->block_index[i]] = 0;
2303 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2304 /* for all motion vector read MVDATA and motion compensate each block */
2308 for (i = 0; i < 4; i++) {
2309 ff_vc1_mc_4mv_luma(v, i, 0, 0);
2310 ff_vc1_mc_4mv_luma(v, i, 1, 1);
2312 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2313 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2315 ff_vc1_mc_1mv(v, 0);
2316 ff_vc1_interp_mc(v);
2318 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2320 for (i = 0; i < 4; i++) {
2323 val = ((mvbp >> (3 - i)) & 1);
2325 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2327 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2328 ff_vc1_mc_4mv_luma(v, j, dir, dir);
2329 ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2332 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2333 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2334 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2338 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2340 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2341 ff_vc1_mc_1mv(v, 0);
2345 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2347 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2348 ff_vc1_interp_mc(v);
2350 dir = bmvtype == BMV_TYPE_BACKWARD;
2357 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2358 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2362 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2363 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2366 for (i = 0; i < 2; i++) {
2367 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];
2368 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];
2369 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];
2370 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];
2373 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2374 ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2377 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2378 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2379 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2380 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2381 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2383 dir = bmvtype == BMV_TYPE_BACKWARD;
2385 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2388 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2390 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2391 v->blk_mv_type[s->block_index[0]] = 1;
2392 v->blk_mv_type[s->block_index[1]] = 1;
2393 v->blk_mv_type[s->block_index[2]] = 1;
2394 v->blk_mv_type[s->block_index[3]] = 1;
2395 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2396 for (i = 0; i < 2; i++) {
2397 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];
2398 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];
2400 ff_vc1_mc_1mv(v, dir);
2404 GET_MQUANT(); // p. 227
2405 s->current_picture.qscale_table[mb_pos] = mquant;
2406 if (!v->ttmbf && cbp)
2407 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2408 for (i = 0; i < 6; i++) {
2409 s->dc_val[0][s->block_index[i]] = 0;
2411 val = ((cbp >> (5 - i)) & 1);
2413 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2415 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2417 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2418 first_block, s->dest[dst_idx] + off,
2419 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2420 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2421 block_cbp |= pat << (i << 2);
2422 if (!v->ttmbf && ttmb < 8)
2430 for (i = 0; i < 6; i++) {
2431 v->mb_type[0][s->block_index[i]] = 0;
2432 s->dc_val[0][s->block_index[i]] = 0;
2434 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2435 s->current_picture.qscale_table[mb_pos] = 0;
2436 v->blk_mv_type[s->block_index[0]] = 0;
2437 v->blk_mv_type[s->block_index[1]] = 0;
2438 v->blk_mv_type[s->block_index[2]] = 0;
2439 v->blk_mv_type[s->block_index[3]] = 0;
2442 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2443 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2444 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2446 dir = bmvtype == BMV_TYPE_BACKWARD;
2447 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2452 for (i = 0; i < 2; i++) {
2453 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];
2454 s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
2455 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];
2456 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];
2459 v->blk_mv_type[s->block_index[0]] = 1;
2460 v->blk_mv_type[s->block_index[1]] = 1;
2461 v->blk_mv_type[s->block_index[2]] = 1;
2462 v->blk_mv_type[s->block_index[3]] = 1;
2463 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2464 for (i = 0; i < 2; i++) {
2465 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];
2466 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];
2472 ff_vc1_mc_1mv(v, dir);
2473 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2474 ff_vc1_interp_mc(v);
2478 if (s->mb_x == s->mb_width - 1)
2479 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2480 v->cbp[s->mb_x] = block_cbp;
2481 v->ttblk[s->mb_x] = block_tt;
2485 /** Decode blocks of I-frame
2487 static void vc1_decode_i_blocks(VC1Context *v)
2490 MpegEncContext *s = &v->s;
2495 /* select coding mode used for VLC tables selection */
2496 switch (v->y_ac_table_index) {
2498 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2501 v->codingset = CS_HIGH_MOT_INTRA;
2504 v->codingset = CS_MID_RATE_INTRA;
2508 switch (v->c_ac_table_index) {
2510 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2513 v->codingset2 = CS_HIGH_MOT_INTER;
2516 v->codingset2 = CS_MID_RATE_INTER;
2520 /* Set DC scale - y and c use the same */
2521 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2522 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2525 s->mb_x = s->mb_y = 0;
2527 s->first_slice_line = 1;
2528 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2530 init_block_index(v);
2531 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2533 ff_update_block_index(s);
2534 dst[0] = s->dest[0];
2535 dst[1] = dst[0] + 8;
2536 dst[2] = s->dest[0] + s->linesize * 8;
2537 dst[3] = dst[2] + 8;
2538 dst[4] = s->dest[1];
2539 dst[5] = s->dest[2];
2540 s->bdsp.clear_blocks(s->block[0]);
2541 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2542 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2543 s->current_picture.qscale_table[mb_pos] = v->pq;
2544 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2545 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2547 // do actual MB decoding and displaying
2548 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2549 v->s.ac_pred = get_bits1(&v->s.gb);
2551 for (k = 0; k < 6; k++) {
2552 val = ((cbp >> (5 - k)) & 1);
2555 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2559 cbp |= val << (5 - k);
2561 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2563 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2565 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2566 if (v->pq >= 9 && v->overlap) {
2568 for (j = 0; j < 64; j++)
2569 s->block[k][j] <<= 1;
2570 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2571 k & 4 ? s->uvlinesize
2575 for (j = 0; j < 64; j++)
2576 s->block[k][j] = (s->block[k][j] - 64) << 1;
2577 s->idsp.put_pixels_clamped(s->block[k], dst[k],
2578 k & 4 ? s->uvlinesize
2583 if (v->pq >= 9 && v->overlap) {
2585 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2586 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2587 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2588 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2589 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2592 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2593 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2594 if (!s->first_slice_line) {
2595 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2596 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2597 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2598 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2599 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2602 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2603 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2605 if (v->s.loop_filter)
2606 ff_vc1_loop_filter_iblk(v, v->pq);
2608 if (get_bits_count(&s->gb) > v->bits) {
2609 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2610 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2611 get_bits_count(&s->gb), v->bits);
2615 if (!v->s.loop_filter)
2616 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2618 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2620 s->first_slice_line = 0;
2622 if (v->s.loop_filter)
2623 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2625 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2626 * profile, these only differ are when decoding MSS2 rectangles. */
2627 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2630 /** Decode blocks of I-frame for advanced profile
2632 static void vc1_decode_i_blocks_adv(VC1Context *v)
2635 MpegEncContext *s = &v->s;
2641 GetBitContext *gb = &s->gb;
2643 /* select coding mode used for VLC tables selection */
2644 switch (v->y_ac_table_index) {
2646 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2649 v->codingset = CS_HIGH_MOT_INTRA;
2652 v->codingset = CS_MID_RATE_INTRA;
2656 switch (v->c_ac_table_index) {
2658 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2661 v->codingset2 = CS_HIGH_MOT_INTER;
2664 v->codingset2 = CS_MID_RATE_INTER;
2669 s->mb_x = s->mb_y = 0;
2671 s->first_slice_line = 1;
2672 s->mb_y = s->start_mb_y;
2673 if (s->start_mb_y) {
2675 init_block_index(v);
2676 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2677 (1 + s->b8_stride) * sizeof(*s->coded_block));
2679 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2681 init_block_index(v);
2682 for (;s->mb_x < s->mb_width; s->mb_x++) {
2683 int16_t (*block)[64] = v->block[v->cur_blk_idx];
2684 ff_update_block_index(s);
2685 s->bdsp.clear_blocks(block[0]);
2686 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2687 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2688 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2689 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2691 // do actual MB decoding and displaying
2692 if (v->fieldtx_is_raw)
2693 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2694 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2695 if (v->acpred_is_raw)
2696 v->s.ac_pred = get_bits1(&v->s.gb);
2698 v->s.ac_pred = v->acpred_plane[mb_pos];
2700 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2701 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2705 s->current_picture.qscale_table[mb_pos] = mquant;
2706 /* Set DC scale - y and c use the same */
2707 s->y_dc_scale = s->y_dc_scale_table[mquant];
2708 s->c_dc_scale = s->c_dc_scale_table[mquant];
2710 for (k = 0; k < 6; k++) {
2711 val = ((cbp >> (5 - k)) & 1);
2714 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2718 cbp |= val << (5 - k);
2720 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2721 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2723 vc1_decode_i_block_adv(v, block[k], k, val,
2724 (k < 4) ? v->codingset : v->codingset2, mquant);
2726 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2728 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2731 ff_vc1_smooth_overlap_filter_iblk(v);
2732 vc1_put_signed_blocks_clamped(v);
2733 if (v->s.loop_filter)
2734 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2736 if (get_bits_count(&s->gb) > v->bits) {
2737 // TODO: may need modification to handle slice coding
2738 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2739 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2740 get_bits_count(&s->gb), v->bits);
2744 if (!v->s.loop_filter)
2745 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2747 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2748 s->first_slice_line = 0;
2751 /* raw bottom MB row */
2753 init_block_index(v);
2754 for (; s->mb_x < s->mb_width; s->mb_x++) {
2755 ff_update_block_index(s);
2756 vc1_put_signed_blocks_clamped(v);
2757 if (v->s.loop_filter)
2758 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2760 if (v->s.loop_filter)
2761 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2762 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2763 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2766 static void vc1_decode_p_blocks(VC1Context *v)
2768 MpegEncContext *s = &v->s;
2769 int apply_loop_filter;
2771 /* select coding mode used for VLC tables selection */
2772 switch (v->c_ac_table_index) {
2774 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2777 v->codingset = CS_HIGH_MOT_INTRA;
2780 v->codingset = CS_MID_RATE_INTRA;
2784 switch (v->c_ac_table_index) {
2786 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2789 v->codingset2 = CS_HIGH_MOT_INTER;
2792 v->codingset2 = CS_MID_RATE_INTER;
2796 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2797 v->fcm == PROGRESSIVE;
2798 s->first_slice_line = 1;
2799 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2800 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2802 init_block_index(v);
2803 for (; s->mb_x < s->mb_width; s->mb_x++) {
2804 ff_update_block_index(s);
2806 if (v->fcm == ILACE_FIELD)
2807 vc1_decode_p_mb_intfi(v);
2808 else if (v->fcm == ILACE_FRAME)
2809 vc1_decode_p_mb_intfr(v);
2810 else vc1_decode_p_mb(v);
2811 if (s->mb_y != s->start_mb_y && apply_loop_filter)
2812 ff_vc1_apply_p_loop_filter(v);
2813 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2814 // TODO: may need modification to handle slice coding
2815 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2816 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2817 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2821 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
2822 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
2823 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2824 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
2825 if (s->mb_y != s->start_mb_y)
2826 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2827 s->first_slice_line = 0;
2829 if (apply_loop_filter) {
2831 init_block_index(v);
2832 for (; s->mb_x < s->mb_width; s->mb_x++) {
2833 ff_update_block_index(s);
2834 ff_vc1_apply_p_loop_filter(v);
2837 if (s->end_mb_y >= s->start_mb_y)
2838 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2839 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2840 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2843 static void vc1_decode_b_blocks(VC1Context *v)
2845 MpegEncContext *s = &v->s;
2847 /* select coding mode used for VLC tables selection */
2848 switch (v->c_ac_table_index) {
2850 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2853 v->codingset = CS_HIGH_MOT_INTRA;
2856 v->codingset = CS_MID_RATE_INTRA;
2860 switch (v->c_ac_table_index) {
2862 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2865 v->codingset2 = CS_HIGH_MOT_INTER;
2868 v->codingset2 = CS_MID_RATE_INTER;
2872 s->first_slice_line = 1;
2873 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2875 init_block_index(v);
2876 for (; s->mb_x < s->mb_width; s->mb_x++) {
2877 ff_update_block_index(s);
2879 if (v->fcm == ILACE_FIELD)
2880 vc1_decode_b_mb_intfi(v);
2881 else if (v->fcm == ILACE_FRAME)
2882 vc1_decode_b_mb_intfr(v);
2885 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2886 // TODO: may need modification to handle slice coding
2887 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2888 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2889 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2892 if (v->s.loop_filter)
2893 ff_vc1_loop_filter_iblk(v, v->pq);
2895 if (!v->s.loop_filter)
2896 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2898 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2899 s->first_slice_line = 0;
2901 if (v->s.loop_filter)
2902 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2903 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2904 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2907 static void vc1_decode_skip_blocks(VC1Context *v)
2909 MpegEncContext *s = &v->s;
2911 if (!v->s.last_picture.f->data[0])
2914 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2915 s->first_slice_line = 1;
2916 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2918 init_block_index(v);
2919 ff_update_block_index(s);
2920 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2921 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2922 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2923 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2924 s->first_slice_line = 0;
2926 s->pict_type = AV_PICTURE_TYPE_P;
2929 void ff_vc1_decode_blocks(VC1Context *v)
2932 v->s.esc3_level_length = 0;
2934 ff_intrax8_decode_picture(&v->x8, &v->s.current_picture,
2935 &v->s.gb, &v->s.mb_x, &v->s.mb_y,
2936 2 * v->pq + v->halfpq, v->pq * !v->pquantizer,
2937 v->s.loop_filter, v->s.low_delay);
2939 ff_er_add_slice(&v->s.er, 0, 0,
2940 (v->s.mb_x >> 1) - 1, (v->s.mb_y >> 1) - 1,
2944 v->left_blk_idx = -1;
2945 v->topleft_blk_idx = 1;
2947 switch (v->s.pict_type) {
2948 case AV_PICTURE_TYPE_I:
2949 if (v->profile == PROFILE_ADVANCED)
2950 vc1_decode_i_blocks_adv(v);
2952 vc1_decode_i_blocks(v);
2954 case AV_PICTURE_TYPE_P:
2955 if (v->p_frame_skipped)
2956 vc1_decode_skip_blocks(v);
2958 vc1_decode_p_blocks(v);
2960 case AV_PICTURE_TYPE_B:
2962 if (v->profile == PROFILE_ADVANCED)
2963 vc1_decode_i_blocks_adv(v);
2965 vc1_decode_i_blocks(v);
2967 vc1_decode_b_blocks(v);