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,
141 #define inc_blk_idx(idx) do { \
143 if (idx >= v->n_allocated_blks) \
147 /***********************************************************************/
149 * @name VC-1 Block-level functions
150 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
156 * @brief Get macroblock-level quantizer scale
158 #define GET_MQUANT() \
159 if (v->dquantfrm) { \
161 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
162 if (v->dqbilevel) { \
163 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
165 mqdiff = get_bits(gb, 3); \
167 mquant = v->pq + mqdiff; \
169 mquant = get_bits(gb, 5); \
172 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
173 edges = 1 << v->dqsbedge; \
174 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
175 edges = (3 << v->dqsbedge) % 15; \
176 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
178 if ((edges&1) && !s->mb_x) \
180 if ((edges&2) && s->first_slice_line) \
182 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
184 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
186 if (!mquant || mquant > 31) { \
187 av_log(v->s.avctx, AV_LOG_ERROR, \
188 "Overriding invalid mquant %d\n", mquant); \
194 * @def GET_MVDATA(_dmv_x, _dmv_y)
195 * @brief Get MV differentials
196 * @see MVDATA decoding from 8.3.5.2, p(1)20
197 * @param _dmv_x Horizontal differential for decoded MV
198 * @param _dmv_y Vertical differential for decoded MV
200 #define GET_MVDATA(_dmv_x, _dmv_y) \
201 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
202 VC1_MV_DIFF_VLC_BITS, 2); \
210 _dmv_x = _dmv_y = 0; \
211 } else if (index == 35) { \
212 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
213 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
214 } else if (index == 36) { \
219 index1 = index % 6; \
220 _dmv_x = offset_table[1][index1]; \
221 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
223 val = get_bits(gb, val); \
224 sign = 0 - (val & 1); \
225 _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign; \
228 index1 = index / 6; \
229 _dmv_y = offset_table[1][index1]; \
230 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
232 val = get_bits(gb, val); \
233 sign = 0 - (val & 1); \
234 _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign; \
238 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
239 int *dmv_y, int *pred_flag)
242 int extend_x, extend_y;
243 GetBitContext *gb = &v->s.gb;
248 bits = VC1_2REF_MVDATA_VLC_BITS;
251 bits = VC1_1REF_MVDATA_VLC_BITS;
254 extend_x = v->dmvrange & 1;
255 extend_y = (v->dmvrange >> 1) & 1;
256 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
258 *dmv_x = get_bits(gb, v->k_x);
259 *dmv_y = get_bits(gb, v->k_y);
262 *pred_flag = *dmv_y & 1;
263 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
267 av_assert0(index < esc);
268 index1 = (index + 1) % 9;
270 val = get_bits(gb, index1 + extend_x);
271 sign = 0 - (val & 1);
272 *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
275 index1 = (index + 1) / 9;
276 if (index1 > v->numref) {
277 val = get_bits(gb, (index1 >> v->numref) + extend_y);
278 sign = 0 - (val & 1);
279 *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
282 if (v->numref && pred_flag)
283 *pred_flag = index1 & 1;
287 /** Reconstruct motion vector for B-frame and do motion compensation
289 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
290 int direct, int mode)
297 if (mode == BMV_TYPE_INTERPOLATED) {
303 ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
306 /** Get predicted DC value for I-frames only
307 * prediction dir: left=0, top=1
308 * @param s MpegEncContext
309 * @param overlap flag indicating that overlap filtering is used
310 * @param pq integer part of picture quantizer
311 * @param[in] n block index in the current MB
312 * @param dc_val_ptr Pointer to DC predictor
313 * @param dir_ptr Prediction direction for use in AC prediction
315 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
316 int16_t **dc_val_ptr, int *dir_ptr)
318 int a, b, c, wrap, pred, scale;
320 static const uint16_t dcpred[32] = {
321 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
322 114, 102, 93, 85, 79, 73, 68, 64,
323 60, 57, 54, 51, 49, 47, 45, 43,
324 41, 39, 38, 37, 35, 34, 33
327 /* find prediction - wmv3_dc_scale always used here in fact */
328 if (n < 4) scale = s->y_dc_scale;
329 else scale = s->c_dc_scale;
331 wrap = s->block_wrap[n];
332 dc_val = s->dc_val[0] + s->block_index[n];
338 b = dc_val[ - 1 - wrap];
341 if (pq < 9 || !overlap) {
342 /* Set outer values */
343 if (s->first_slice_line && (n != 2 && n != 3))
344 b = a = dcpred[scale];
345 if (s->mb_x == 0 && (n != 1 && n != 3))
346 b = c = dcpred[scale];
348 /* Set outer values */
349 if (s->first_slice_line && (n != 2 && n != 3))
351 if (s->mb_x == 0 && (n != 1 && n != 3))
355 if (abs(a - b) <= abs(b - c)) {
357 *dir_ptr = 1; // left
363 /* update predictor */
364 *dc_val_ptr = &dc_val[0];
369 /** Get predicted DC value
370 * prediction dir: left=0, top=1
371 * @param s MpegEncContext
372 * @param overlap flag indicating that overlap filtering is used
373 * @param pq integer part of picture quantizer
374 * @param[in] n block index in the current MB
375 * @param a_avail flag indicating top block availability
376 * @param c_avail flag indicating left block availability
377 * @param dc_val_ptr Pointer to DC predictor
378 * @param dir_ptr Prediction direction for use in AC prediction
380 static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
381 int a_avail, int c_avail,
382 int16_t **dc_val_ptr, int *dir_ptr)
384 int a, b, c, wrap, pred;
386 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
390 /* scale predictors if needed */
391 q1 = s->current_picture.qscale_table[mb_pos];
392 dqscale_index = s->y_dc_scale_table[q1] - 1;
393 if (dqscale_index < 0)
396 wrap = s->block_wrap[n];
397 dc_val = s->dc_val[0] + s->block_index[n];
403 b = dc_val[ - 1 - wrap];
406 if (c_avail && (n != 1 && n != 3)) {
407 q2 = s->current_picture.qscale_table[mb_pos - 1];
409 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
411 if (a_avail && (n != 2 && n != 3)) {
412 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
414 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
416 if (a_avail && c_avail && (n != 3)) {
422 q2 = s->current_picture.qscale_table[off];
424 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
427 if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
429 *dir_ptr = 1; // left
430 } else if (a_avail) {
435 *dir_ptr = 1; // left
438 /* update predictor */
439 *dc_val_ptr = &dc_val[0];
443 /** @} */ // Block group
446 * @name VC1 Macroblock-level functions in Simple/Main Profiles
447 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
451 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
452 uint8_t **coded_block_ptr)
454 int xy, wrap, pred, a, b, c;
456 xy = s->block_index[n];
462 a = s->coded_block[xy - 1 ];
463 b = s->coded_block[xy - 1 - wrap];
464 c = s->coded_block[xy - wrap];
473 *coded_block_ptr = &s->coded_block[xy];
479 * Decode one AC coefficient
480 * @param v The VC1 context
481 * @param last Last coefficient
482 * @param skip How much zero coefficients to skip
483 * @param value Decoded AC coefficient value
484 * @param codingset set of VLC to decode data
487 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
488 int *value, int codingset)
490 GetBitContext *gb = &v->s.gb;
491 int index, run, level, lst, sign;
493 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
494 if (index != ff_vc1_ac_sizes[codingset] - 1) {
495 run = vc1_index_decode_table[codingset][index][0];
496 level = vc1_index_decode_table[codingset][index][1];
497 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
498 sign = get_bits1(gb);
500 int escape = decode210(gb);
502 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
503 run = vc1_index_decode_table[codingset][index][0];
504 level = vc1_index_decode_table[codingset][index][1];
505 lst = index >= vc1_last_decode_table[codingset];
508 level += vc1_last_delta_level_table[codingset][run];
510 level += vc1_delta_level_table[codingset][run];
513 run += vc1_last_delta_run_table[codingset][level] + 1;
515 run += vc1_delta_run_table[codingset][level] + 1;
517 sign = get_bits1(gb);
520 if (v->s.esc3_level_length == 0) {
521 if (v->pq < 8 || v->dquantfrm) { // table 59
522 v->s.esc3_level_length = get_bits(gb, 3);
523 if (!v->s.esc3_level_length)
524 v->s.esc3_level_length = get_bits(gb, 2) + 8;
526 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
528 v->s.esc3_run_length = 3 + get_bits(gb, 2);
530 run = get_bits(gb, v->s.esc3_run_length);
531 sign = get_bits1(gb);
532 level = get_bits(gb, v->s.esc3_level_length);
538 *value = (level ^ -sign) + sign;
541 /** Decode intra block in intra frames - should be faster than decode_intra_block
542 * @param v VC1Context
543 * @param block block to decode
544 * @param[in] n subblock index
545 * @param coded are AC coeffs present or not
546 * @param codingset set of VLC to decode data
548 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
549 int coded, int codingset)
551 GetBitContext *gb = &v->s.gb;
552 MpegEncContext *s = &v->s;
553 int dc_pred_dir = 0; /* Direction of the DC prediction used */
556 int16_t *ac_val, *ac_val2;
559 /* Get DC differential */
561 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
563 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
566 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
570 const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
571 if (dcdiff == 119 /* ESC index value */) {
572 dcdiff = get_bits(gb, 8 + m);
575 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
582 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
585 /* Store the quantized DC coeff, used for prediction */
587 scale = s->y_dc_scale;
589 scale = s->c_dc_scale;
590 block[0] = dcdiff * scale;
592 ac_val = s->ac_val[0][s->block_index[n]];
594 if (dc_pred_dir) // left
597 ac_val -= 16 * s->block_wrap[n];
599 scale = v->pq * 2 + v->halfpq;
605 int last = 0, skip, value;
606 const uint8_t *zz_table;
611 zz_table = v->zz_8x8[2];
613 zz_table = v->zz_8x8[3];
615 zz_table = v->zz_8x8[1];
618 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
622 block[zz_table[i++]] = value;
625 /* apply AC prediction if needed */
628 if (dc_pred_dir) { // left
634 for (k = 1; k < 8; k++)
635 block[k << sh] += ac_val[k];
637 /* save AC coeffs for further prediction */
638 for (k = 1; k < 8; k++) {
639 ac_val2[k] = block[k << v->left_blk_sh];
640 ac_val2[k + 8] = block[k << v->top_blk_sh];
643 /* scale AC coeffs */
644 for (k = 1; k < 64; k++)
648 block[k] += (block[k] < 0) ? -v->pq : v->pq;
654 memset(ac_val2, 0, 16 * 2);
656 /* apply AC prediction if needed */
659 if (dc_pred_dir) { //left
666 memcpy(ac_val2, ac_val, 8 * 2);
667 for (k = 1; k < 8; k++) {
668 block[k << sh] = ac_val[k] * scale;
669 if (!v->pquantizer && block[k << sh])
670 block[k << sh] += (block[k << sh] < 0) ? -v->pq : v->pq;
674 if (s->ac_pred) i = 63;
675 s->block_last_index[n] = i;
680 /** Decode intra block in intra frames - should be faster than decode_intra_block
681 * @param v VC1Context
682 * @param block block to decode
683 * @param[in] n subblock number
684 * @param coded are AC coeffs present or not
685 * @param codingset set of VLC to decode data
686 * @param mquant quantizer value for this macroblock
688 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
689 int coded, int codingset, int mquant)
691 GetBitContext *gb = &v->s.gb;
692 MpegEncContext *s = &v->s;
693 int dc_pred_dir = 0; /* Direction of the DC prediction used */
695 int16_t *dc_val = NULL;
696 int16_t *ac_val, *ac_val2;
698 int a_avail = v->a_avail, c_avail = v->c_avail;
699 int use_pred = s->ac_pred;
702 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
704 /* Get DC differential */
706 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
708 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
711 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
715 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
716 if (dcdiff == 119 /* ESC index value */) {
717 dcdiff = get_bits(gb, 8 + m);
720 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
727 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
730 /* Store the quantized DC coeff, used for prediction */
732 scale = s->y_dc_scale;
734 scale = s->c_dc_scale;
735 block[0] = dcdiff * scale;
737 /* check if AC is needed at all */
738 if (!a_avail && !c_avail)
741 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
743 ac_val = s->ac_val[0][s->block_index[n]];
745 if (dc_pred_dir) // left
748 ac_val -= 16 * s->block_wrap[n];
750 q1 = s->current_picture.qscale_table[mb_pos];
753 else if (dc_pred_dir) {
756 else if (c_avail && mb_pos)
757 q2 = s->current_picture.qscale_table[mb_pos - 1];
761 else if (a_avail && mb_pos >= s->mb_stride)
762 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
769 int last = 0, skip, value;
770 const uint8_t *zz_table;
774 if (!use_pred && v->fcm == ILACE_FRAME) {
775 zz_table = v->zzi_8x8;
777 if (!dc_pred_dir) // top
778 zz_table = v->zz_8x8[2];
780 zz_table = v->zz_8x8[3];
783 if (v->fcm != ILACE_FRAME)
784 zz_table = v->zz_8x8[1];
786 zz_table = v->zzi_8x8;
790 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
794 block[zz_table[i++]] = value;
797 /* apply AC prediction if needed */
800 if (dc_pred_dir) { // left
806 /* scale predictors if needed*/
807 if (q2 && q1 != q2) {
808 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
810 return AVERROR_INVALIDDATA;
811 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
812 for (k = 1; k < 8; k++)
813 block[k << sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
815 for (k = 1; k < 8; k++)
816 block[k << sh] += ac_val[k];
819 /* save AC coeffs for further prediction */
820 for (k = 1; k < 8; k++) {
821 ac_val2[k ] = block[k << v->left_blk_sh];
822 ac_val2[k + 8] = block[k << v->top_blk_sh];
825 /* scale AC coeffs */
826 for (k = 1; k < 64; k++)
830 block[k] += (block[k] < 0) ? -mquant : mquant;
833 } else { // no AC coeffs
836 memset(ac_val2, 0, 16 * 2);
838 /* apply AC prediction if needed */
841 if (dc_pred_dir) { // left
848 memcpy(ac_val2, ac_val, 8 * 2);
849 if (q2 && q1 != q2) {
850 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
851 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
853 return AVERROR_INVALIDDATA;
854 for (k = 1; k < 8; k++)
855 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
857 for (k = 1; k < 8; k++) {
858 block[k << sh] = ac_val2[k] * scale;
859 if (!v->pquantizer && block[k << sh])
860 block[k << sh] += (block[k << sh] < 0) ? -mquant : mquant;
864 if (use_pred) i = 63;
865 s->block_last_index[n] = i;
870 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
871 * @param v VC1Context
872 * @param block block to decode
873 * @param[in] n subblock index
874 * @param coded are AC coeffs present or not
875 * @param mquant block quantizer
876 * @param codingset set of VLC to decode data
878 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
879 int coded, int mquant, int codingset)
881 GetBitContext *gb = &v->s.gb;
882 MpegEncContext *s = &v->s;
883 int dc_pred_dir = 0; /* Direction of the DC prediction used */
885 int16_t *dc_val = NULL;
886 int16_t *ac_val, *ac_val2;
888 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
889 int a_avail = v->a_avail, c_avail = v->c_avail;
890 int use_pred = s->ac_pred;
894 s->bdsp.clear_block(block);
896 /* XXX: Guard against dumb values of mquant */
897 mquant = av_clip_uintp2(mquant, 5);
899 /* Set DC scale - y and c use the same */
900 s->y_dc_scale = s->y_dc_scale_table[mquant];
901 s->c_dc_scale = s->c_dc_scale_table[mquant];
903 /* Get DC differential */
905 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
907 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
910 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
914 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
915 if (dcdiff == 119 /* ESC index value */) {
916 dcdiff = get_bits(gb, 8 + m);
919 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
926 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
929 /* Store the quantized DC coeff, used for prediction */
932 block[0] = dcdiff * s->y_dc_scale;
934 block[0] = dcdiff * s->c_dc_scale;
940 /* check if AC is needed at all and adjust direction if needed */
941 if (!a_avail) dc_pred_dir = 1;
942 if (!c_avail) dc_pred_dir = 0;
943 if (!a_avail && !c_avail) use_pred = 0;
944 ac_val = s->ac_val[0][s->block_index[n]];
947 scale = mquant * 2 + v->halfpq;
949 if (dc_pred_dir) //left
952 ac_val -= 16 * s->block_wrap[n];
954 q1 = s->current_picture.qscale_table[mb_pos];
955 if (dc_pred_dir && c_avail && mb_pos)
956 q2 = s->current_picture.qscale_table[mb_pos - 1];
957 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
958 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
959 if (dc_pred_dir && n == 1)
961 if (!dc_pred_dir && n == 2)
966 int last = 0, skip, value;
970 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
974 if (v->fcm == PROGRESSIVE)
975 block[v->zz_8x8[0][i++]] = value;
977 if (use_pred && (v->fcm == ILACE_FRAME)) {
978 if (!dc_pred_dir) // top
979 block[v->zz_8x8[2][i++]] = value;
981 block[v->zz_8x8[3][i++]] = value;
983 block[v->zzi_8x8[i++]] = value;
988 /* apply AC prediction if needed */
990 /* scale predictors if needed*/
991 if (q2 && q1 != q2) {
992 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
993 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
996 return AVERROR_INVALIDDATA;
997 if (dc_pred_dir) { // left
998 for (k = 1; k < 8; k++)
999 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1001 for (k = 1; k < 8; k++)
1002 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1005 if (dc_pred_dir) { // left
1006 for (k = 1; k < 8; k++)
1007 block[k << v->left_blk_sh] += ac_val[k];
1009 for (k = 1; k < 8; k++)
1010 block[k << v->top_blk_sh] += ac_val[k + 8];
1014 /* save AC coeffs for further prediction */
1015 for (k = 1; k < 8; k++) {
1016 ac_val2[k ] = block[k << v->left_blk_sh];
1017 ac_val2[k + 8] = block[k << v->top_blk_sh];
1020 /* scale AC coeffs */
1021 for (k = 1; k < 64; k++)
1025 block[k] += (block[k] < 0) ? -mquant : mquant;
1028 if (use_pred) i = 63;
1029 } else { // no AC coeffs
1032 memset(ac_val2, 0, 16 * 2);
1033 if (dc_pred_dir) { // left
1035 memcpy(ac_val2, ac_val, 8 * 2);
1036 if (q2 && q1 != q2) {
1037 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1038 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1040 return AVERROR_INVALIDDATA;
1041 for (k = 1; k < 8; k++)
1042 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1047 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1048 if (q2 && q1 != q2) {
1049 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1050 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1052 return AVERROR_INVALIDDATA;
1053 for (k = 1; k < 8; k++)
1054 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1059 /* apply AC prediction if needed */
1061 if (dc_pred_dir) { // left
1062 for (k = 1; k < 8; k++) {
1063 block[k << v->left_blk_sh] = ac_val2[k] * scale;
1064 if (!v->pquantizer && block[k << v->left_blk_sh])
1065 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1068 for (k = 1; k < 8; k++) {
1069 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1070 if (!v->pquantizer && block[k << v->top_blk_sh])
1071 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1077 s->block_last_index[n] = i;
1084 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1085 int mquant, int ttmb, int first_block,
1086 uint8_t *dst, int linesize, int skip_block,
1089 MpegEncContext *s = &v->s;
1090 GetBitContext *gb = &s->gb;
1093 int scale, off, idx, last, skip, value;
1094 int ttblk = ttmb & 7;
1097 s->bdsp.clear_block(block);
1100 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)];
1102 if (ttblk == TT_4X4) {
1103 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1105 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1106 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1107 || (!v->res_rtm_flag && !first_block))) {
1108 subblkpat = decode012(gb);
1110 subblkpat ^= 3; // swap decoded pattern bits
1111 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1113 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1116 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1118 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1119 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1120 subblkpat = 2 - (ttblk == TT_8X4_TOP);
1123 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1124 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1133 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1138 idx = v->zz_8x8[0][i++];
1140 idx = v->zzi_8x8[i++];
1141 block[idx] = value * scale;
1143 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1147 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1149 v->vc1dsp.vc1_inv_trans_8x8(block);
1150 s->idsp.add_pixels_clamped(block, dst, linesize);
1155 pat = ~subblkpat & 0xF;
1156 for (j = 0; j < 4; j++) {
1157 last = subblkpat & (1 << (3 - j));
1159 off = (j & 1) * 4 + (j & 2) * 16;
1161 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1166 idx = ff_vc1_simple_progressive_4x4_zz[i++];
1168 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1169 block[idx + off] = value * scale;
1171 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1173 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1175 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1177 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1182 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1183 for (j = 0; j < 2; j++) {
1184 last = subblkpat & (1 << (1 - j));
1188 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1193 idx = v->zz_8x4[i++] + off;
1195 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1196 block[idx] = value * scale;
1198 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1200 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1202 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1204 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1209 pat = ~(subblkpat * 5) & 0xF;
1210 for (j = 0; j < 2; j++) {
1211 last = subblkpat & (1 << (1 - j));
1215 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1220 idx = v->zz_4x8[i++] + off;
1222 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1223 block[idx] = value * scale;
1225 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1227 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1229 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1231 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1237 *ttmb_out |= ttblk << (n * 4);
1241 /** @} */ // Macroblock group
1243 static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 };
1245 /** Decode one P-frame MB
1247 static int vc1_decode_p_mb(VC1Context *v)
1249 MpegEncContext *s = &v->s;
1250 GetBitContext *gb = &s->gb;
1252 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1253 int cbp; /* cbp decoding stuff */
1254 int mqdiff, mquant; /* MB quantization */
1255 int ttmb = v->ttfrm; /* MB Transform type */
1257 int mb_has_coeffs = 1; /* last_flag */
1258 int dmv_x, dmv_y; /* Differential MV components */
1259 int index, index1; /* LUT indexes */
1260 int val, sign; /* temp values */
1261 int first_block = 1;
1263 int skipped, fourmv;
1264 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1266 mquant = v->pq; /* lossy initialization */
1268 if (v->mv_type_is_raw)
1269 fourmv = get_bits1(gb);
1271 fourmv = v->mv_type_mb_plane[mb_pos];
1273 skipped = get_bits1(gb);
1275 skipped = v->s.mbskip_table[mb_pos];
1277 if (!fourmv) { /* 1MV mode */
1279 GET_MVDATA(dmv_x, dmv_y);
1282 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1283 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1285 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1286 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1288 /* FIXME Set DC val for inter block ? */
1289 if (s->mb_intra && !mb_has_coeffs) {
1291 s->ac_pred = get_bits1(gb);
1293 } else if (mb_has_coeffs) {
1295 s->ac_pred = get_bits1(gb);
1296 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1302 s->current_picture.qscale_table[mb_pos] = mquant;
1304 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1305 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1306 VC1_TTMB_VLC_BITS, 2);
1307 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1309 for (i = 0; i < 6; i++) {
1310 s->dc_val[0][s->block_index[i]] = 0;
1312 val = ((cbp >> (5 - i)) & 1);
1313 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1314 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1316 /* check if prediction blocks A and C are available */
1317 v->a_avail = v->c_avail = 0;
1318 if (i == 2 || i == 3 || !s->first_slice_line)
1319 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1320 if (i == 1 || i == 3 || s->mb_x)
1321 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1323 vc1_decode_intra_block(v, v->block[v->cur_blk_idx][i], i, val, mquant,
1324 (i & 4) ? v->codingset2 : v->codingset);
1325 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1327 v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][i]);
1329 for (j = 0; j < 64; j++)
1330 v->block[v->cur_blk_idx][i][j] <<= 1;
1331 s->idsp.put_signed_pixels_clamped(v->block[v->cur_blk_idx][i],
1332 s->dest[dst_idx] + off,
1333 i & 4 ? s->uvlinesize
1335 if (v->pq >= 9 && v->overlap) {
1337 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1339 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1341 block_cbp |= 0xF << (i << 2);
1342 block_intra |= 1 << i;
1344 pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][i], i, mquant, ttmb, first_block,
1345 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1346 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1347 block_cbp |= pat << (i << 2);
1348 if (!v->ttmbf && ttmb < 8)
1355 for (i = 0; i < 6; i++) {
1356 v->mb_type[0][s->block_index[i]] = 0;
1357 s->dc_val[0][s->block_index[i]] = 0;
1359 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1360 s->current_picture.qscale_table[mb_pos] = 0;
1361 ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1362 ff_vc1_mc_1mv(v, 0);
1364 } else { // 4MV mode
1365 if (!skipped /* unskipped MB */) {
1366 int intra_count = 0, coded_inter = 0;
1367 int is_intra[6], is_coded[6];
1369 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1370 for (i = 0; i < 6; i++) {
1371 val = ((cbp >> (5 - i)) & 1);
1372 s->dc_val[0][s->block_index[i]] = 0;
1379 GET_MVDATA(dmv_x, dmv_y);
1381 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1383 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1384 intra_count += s->mb_intra;
1385 is_intra[i] = s->mb_intra;
1386 is_coded[i] = mb_has_coeffs;
1389 is_intra[i] = (intra_count >= 3);
1393 ff_vc1_mc_4mv_chroma(v, 0);
1394 v->mb_type[0][s->block_index[i]] = is_intra[i];
1396 coded_inter = !is_intra[i] & is_coded[i];
1398 // if there are no coded blocks then don't do anything more
1400 if (!intra_count && !coded_inter)
1403 s->current_picture.qscale_table[mb_pos] = mquant;
1404 /* test if block is intra and has pred */
1407 for (i = 0; i < 6; i++)
1409 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1410 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1416 s->ac_pred = get_bits1(gb);
1420 if (!v->ttmbf && coded_inter)
1421 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1422 for (i = 0; i < 6; i++) {
1424 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1425 s->mb_intra = is_intra[i];
1427 /* check if prediction blocks A and C are available */
1428 v->a_avail = v->c_avail = 0;
1429 if (i == 2 || i == 3 || !s->first_slice_line)
1430 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1431 if (i == 1 || i == 3 || s->mb_x)
1432 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1434 vc1_decode_intra_block(v, v->block[v->cur_blk_idx][i], i, is_coded[i], mquant,
1435 (i & 4) ? v->codingset2 : v->codingset);
1436 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1438 v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][i]);
1440 for (j = 0; j < 64; j++)
1441 v->block[v->cur_blk_idx][i][j] <<= 1;
1442 s->idsp.put_signed_pixels_clamped(v->block[v->cur_blk_idx][i],
1443 s->dest[dst_idx] + off,
1444 (i & 4) ? s->uvlinesize
1446 if (v->pq >= 9 && v->overlap) {
1448 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1450 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1452 block_cbp |= 0xF << (i << 2);
1453 block_intra |= 1 << i;
1454 } else if (is_coded[i]) {
1455 pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][i], i, mquant, ttmb,
1456 first_block, s->dest[dst_idx] + off,
1457 (i & 4) ? s->uvlinesize : s->linesize,
1458 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1460 block_cbp |= pat << (i << 2);
1461 if (!v->ttmbf && ttmb < 8)
1466 } else { // skipped MB
1468 s->current_picture.qscale_table[mb_pos] = 0;
1469 for (i = 0; i < 6; i++) {
1470 v->mb_type[0][s->block_index[i]] = 0;
1471 s->dc_val[0][s->block_index[i]] = 0;
1473 for (i = 0; i < 4; i++) {
1474 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1475 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1477 ff_vc1_mc_4mv_chroma(v, 0);
1478 s->current_picture.qscale_table[mb_pos] = 0;
1482 v->cbp[s->mb_x] = block_cbp;
1483 v->ttblk[s->mb_x] = block_tt;
1484 v->is_intra[s->mb_x] = block_intra;
1489 /* Decode one macroblock in an interlaced frame p picture */
1491 static int vc1_decode_p_mb_intfr(VC1Context *v)
1493 MpegEncContext *s = &v->s;
1494 GetBitContext *gb = &s->gb;
1496 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1497 int cbp = 0; /* cbp decoding stuff */
1498 int mqdiff, mquant; /* MB quantization */
1499 int ttmb = v->ttfrm; /* MB Transform type */
1501 int mb_has_coeffs = 1; /* last_flag */
1502 int dmv_x, dmv_y; /* Differential MV components */
1503 int val; /* temp value */
1504 int first_block = 1;
1506 int skipped, fourmv = 0, twomv = 0;
1507 int block_cbp = 0, pat, block_tt = 0;
1508 int idx_mbmode = 0, mvbp;
1509 int stride_y, fieldtx;
1511 mquant = v->pq; /* Lossy initialization */
1514 skipped = get_bits1(gb);
1516 skipped = v->s.mbskip_table[mb_pos];
1518 if (v->fourmvswitch)
1519 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1521 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1522 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1523 /* store the motion vector type in a flag (useful later) */
1524 case MV_PMODE_INTFR_4MV:
1526 v->blk_mv_type[s->block_index[0]] = 0;
1527 v->blk_mv_type[s->block_index[1]] = 0;
1528 v->blk_mv_type[s->block_index[2]] = 0;
1529 v->blk_mv_type[s->block_index[3]] = 0;
1531 case MV_PMODE_INTFR_4MV_FIELD:
1533 v->blk_mv_type[s->block_index[0]] = 1;
1534 v->blk_mv_type[s->block_index[1]] = 1;
1535 v->blk_mv_type[s->block_index[2]] = 1;
1536 v->blk_mv_type[s->block_index[3]] = 1;
1538 case MV_PMODE_INTFR_2MV_FIELD:
1540 v->blk_mv_type[s->block_index[0]] = 1;
1541 v->blk_mv_type[s->block_index[1]] = 1;
1542 v->blk_mv_type[s->block_index[2]] = 1;
1543 v->blk_mv_type[s->block_index[3]] = 1;
1545 case MV_PMODE_INTFR_1MV:
1546 v->blk_mv_type[s->block_index[0]] = 0;
1547 v->blk_mv_type[s->block_index[1]] = 0;
1548 v->blk_mv_type[s->block_index[2]] = 0;
1549 v->blk_mv_type[s->block_index[3]] = 0;
1552 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1553 for (i = 0; i < 4; i++) {
1554 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1555 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1557 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1559 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1560 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1561 mb_has_coeffs = get_bits1(gb);
1563 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1564 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1566 s->current_picture.qscale_table[mb_pos] = mquant;
1567 /* Set DC scale - y and c use the same (not sure if necessary here) */
1568 s->y_dc_scale = s->y_dc_scale_table[mquant];
1569 s->c_dc_scale = s->c_dc_scale_table[mquant];
1571 for (i = 0; i < 6; i++) {
1572 v->a_avail = v->c_avail = 0;
1573 v->mb_type[0][s->block_index[i]] = 1;
1574 s->dc_val[0][s->block_index[i]] = 0;
1576 val = ((cbp >> (5 - i)) & 1);
1577 if (i == 2 || i == 3 || !s->first_slice_line)
1578 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1579 if (i == 1 || i == 3 || s->mb_x)
1580 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1582 vc1_decode_intra_block(v, v->block[v->cur_blk_idx][i], i, val, mquant,
1583 (i & 4) ? v->codingset2 : v->codingset);
1584 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1586 v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][i]);
1588 stride_y = s->linesize << fieldtx;
1589 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1591 stride_y = s->uvlinesize;
1594 s->idsp.put_signed_pixels_clamped(v->block[v->cur_blk_idx][i],
1595 s->dest[dst_idx] + off,
1598 block_cbp |= 0xf << (i << 2);
1601 } else { // inter MB
1602 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1604 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1605 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1606 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1608 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1609 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1610 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1613 s->mb_intra = v->is_intra[s->mb_x] = 0;
1614 for (i = 0; i < 6; i++)
1615 v->mb_type[0][s->block_index[i]] = 0;
1616 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1617 /* for all motion vector read MVDATA and motion compensate each block */
1621 for (i = 0; i < 4; i++) {
1623 if (mvbp & (8 >> i))
1624 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1625 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1626 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1628 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1633 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1635 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1636 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1637 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1640 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1642 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1643 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1644 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1645 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1647 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1650 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1652 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1653 ff_vc1_mc_1mv(v, 0);
1656 GET_MQUANT(); // p. 227
1657 s->current_picture.qscale_table[mb_pos] = mquant;
1658 if (!v->ttmbf && cbp)
1659 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1660 for (i = 0; i < 6; i++) {
1661 s->dc_val[0][s->block_index[i]] = 0;
1663 val = ((cbp >> (5 - i)) & 1);
1665 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1667 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1669 pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][i], i, mquant, ttmb,
1670 first_block, s->dest[dst_idx] + off,
1671 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1672 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1673 block_cbp |= pat << (i << 2);
1674 if (!v->ttmbf && ttmb < 8)
1681 s->mb_intra = v->is_intra[s->mb_x] = 0;
1682 for (i = 0; i < 6; i++) {
1683 v->mb_type[0][s->block_index[i]] = 0;
1684 s->dc_val[0][s->block_index[i]] = 0;
1686 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1687 s->current_picture.qscale_table[mb_pos] = 0;
1688 v->blk_mv_type[s->block_index[0]] = 0;
1689 v->blk_mv_type[s->block_index[1]] = 0;
1690 v->blk_mv_type[s->block_index[2]] = 0;
1691 v->blk_mv_type[s->block_index[3]] = 0;
1692 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1693 ff_vc1_mc_1mv(v, 0);
1694 v->fieldtx_plane[mb_pos] = 0;
1696 v->cbp[s->mb_x] = block_cbp;
1697 v->ttblk[s->mb_x] = block_tt;
1702 static int vc1_decode_p_mb_intfi(VC1Context *v)
1704 MpegEncContext *s = &v->s;
1705 GetBitContext *gb = &s->gb;
1707 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1708 int cbp = 0; /* cbp decoding stuff */
1709 int mqdiff, mquant; /* MB quantization */
1710 int ttmb = v->ttfrm; /* MB Transform type */
1712 int mb_has_coeffs = 1; /* last_flag */
1713 int dmv_x, dmv_y; /* Differential MV components */
1714 int val; /* temp values */
1715 int first_block = 1;
1718 int block_cbp = 0, pat, block_tt = 0;
1721 mquant = v->pq; /* Lossy initialization */
1723 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1724 if (idx_mbmode <= 1) { // intra MB
1725 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1727 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1728 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1729 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1731 s->current_picture.qscale_table[mb_pos] = mquant;
1732 /* Set DC scale - y and c use the same (not sure if necessary here) */
1733 s->y_dc_scale = s->y_dc_scale_table[mquant];
1734 s->c_dc_scale = s->c_dc_scale_table[mquant];
1735 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1736 mb_has_coeffs = idx_mbmode & 1;
1738 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1740 for (i = 0; i < 6; i++) {
1741 v->a_avail = v->c_avail = 0;
1742 v->mb_type[0][s->block_index[i]] = 1;
1743 s->dc_val[0][s->block_index[i]] = 0;
1745 val = ((cbp >> (5 - i)) & 1);
1746 if (i == 2 || i == 3 || !s->first_slice_line)
1747 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1748 if (i == 1 || i == 3 || s->mb_x)
1749 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1751 vc1_decode_intra_block(v, v->block[v->cur_blk_idx][i], i, val, mquant,
1752 (i & 4) ? v->codingset2 : v->codingset);
1753 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1755 v->vc1dsp.vc1_inv_trans_8x8(v->block[v->cur_blk_idx][i]);
1756 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1757 s->idsp.put_signed_pixels_clamped(v->block[v->cur_blk_idx][i],
1758 s->dest[dst_idx] + off,
1759 (i & 4) ? s->uvlinesize
1761 // TODO: loop filter
1762 block_cbp |= 0xf << (i << 2);
1765 s->mb_intra = v->is_intra[s->mb_x] = 0;
1766 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1767 for (i = 0; i < 6; i++)
1768 v->mb_type[0][s->block_index[i]] = 0;
1769 if (idx_mbmode <= 5) { // 1-MV
1770 dmv_x = dmv_y = pred_flag = 0;
1771 if (idx_mbmode & 1) {
1772 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1774 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1775 ff_vc1_mc_1mv(v, 0);
1776 mb_has_coeffs = !(idx_mbmode & 2);
1778 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1779 for (i = 0; i < 4; i++) {
1780 dmv_x = dmv_y = pred_flag = 0;
1781 if (v->fourmvbp & (8 >> i))
1782 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1783 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1784 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1786 ff_vc1_mc_4mv_chroma(v, 0);
1787 mb_has_coeffs = idx_mbmode & 1;
1790 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1794 s->current_picture.qscale_table[mb_pos] = mquant;
1795 if (!v->ttmbf && cbp) {
1796 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1799 for (i = 0; i < 6; i++) {
1800 s->dc_val[0][s->block_index[i]] = 0;
1802 val = ((cbp >> (5 - i)) & 1);
1803 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1805 pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][i], i, mquant, ttmb,
1806 first_block, s->dest[dst_idx] + off,
1807 (i & 4) ? s->uvlinesize : s->linesize,
1808 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1810 block_cbp |= pat << (i << 2);
1811 if (!v->ttmbf && ttmb < 8)
1817 v->cbp[s->mb_x] = block_cbp;
1818 v->ttblk[s->mb_x] = block_tt;
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;
1996 int block_cbp = 0, pat, block_tt = 0;
1999 mquant = v->pq; /* Lossy initialization */
2002 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2003 if (idx_mbmode <= 1) { // intra MB
2004 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2006 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2007 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2008 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2010 s->current_picture.qscale_table[mb_pos] = mquant;
2011 /* Set DC scale - y and c use the same (not sure if necessary here) */
2012 s->y_dc_scale = s->y_dc_scale_table[mquant];
2013 s->c_dc_scale = s->c_dc_scale_table[mquant];
2014 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2015 mb_has_coeffs = idx_mbmode & 1;
2017 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2019 for (i = 0; i < 6; i++) {
2020 v->a_avail = v->c_avail = 0;
2021 v->mb_type[0][s->block_index[i]] = 1;
2022 s->dc_val[0][s->block_index[i]] = 0;
2024 val = ((cbp >> (5 - i)) & 1);
2025 if (i == 2 || i == 3 || !s->first_slice_line)
2026 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2027 if (i == 1 || i == 3 || s->mb_x)
2028 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2030 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2031 (i & 4) ? v->codingset2 : v->codingset);
2032 if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2034 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2036 for (j = 0; j < 64; j++)
2037 s->block[i][j] <<= 1;
2038 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2039 s->idsp.put_signed_pixels_clamped(s->block[i],
2040 s->dest[dst_idx] + off,
2041 (i & 4) ? s->uvlinesize
2043 // TODO: yet to perform loop filter
2046 s->mb_intra = v->is_intra[s->mb_x] = 0;
2047 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2048 for (i = 0; i < 6; i++)
2049 v->mb_type[0][s->block_index[i]] = 0;
2051 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2053 fwd = v->forward_mb_plane[mb_pos];
2054 if (idx_mbmode <= 5) { // 1-MV
2056 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2057 pred_flag[0] = pred_flag[1] = 0;
2059 bmvtype = BMV_TYPE_FORWARD;
2061 bmvtype = decode012(gb);
2064 bmvtype = BMV_TYPE_BACKWARD;
2067 bmvtype = BMV_TYPE_DIRECT;
2070 bmvtype = BMV_TYPE_INTERPOLATED;
2071 interpmvp = get_bits1(gb);
2074 v->bmvtype = bmvtype;
2075 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2076 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2079 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2081 if (bmvtype == BMV_TYPE_DIRECT) {
2082 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2083 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2084 if (!s->next_picture_ptr->field_picture) {
2085 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2089 ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2090 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2091 mb_has_coeffs = !(idx_mbmode & 2);
2094 bmvtype = BMV_TYPE_FORWARD;
2095 v->bmvtype = bmvtype;
2096 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2097 for (i = 0; i < 4; i++) {
2098 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2099 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2100 if (v->fourmvbp & (8 >> i)) {
2101 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2102 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2103 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2105 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2106 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2108 ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2109 mb_has_coeffs = idx_mbmode & 1;
2112 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2116 s->current_picture.qscale_table[mb_pos] = mquant;
2117 if (!v->ttmbf && cbp) {
2118 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2121 for (i = 0; i < 6; i++) {
2122 s->dc_val[0][s->block_index[i]] = 0;
2124 val = ((cbp >> (5 - i)) & 1);
2125 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2127 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2128 first_block, s->dest[dst_idx] + off,
2129 (i & 4) ? s->uvlinesize : s->linesize,
2130 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2131 block_cbp |= pat << (i << 2);
2132 if (!v->ttmbf && ttmb < 8)
2138 v->cbp[s->mb_x] = block_cbp;
2139 v->ttblk[s->mb_x] = block_tt;
2142 /** Decode one B-frame MB (in interlaced frame B picture)
2144 static int vc1_decode_b_mb_intfr(VC1Context *v)
2146 MpegEncContext *s = &v->s;
2147 GetBitContext *gb = &s->gb;
2149 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2150 int cbp = 0; /* cbp decoding stuff */
2151 int mqdiff, mquant; /* MB quantization */
2152 int ttmb = v->ttfrm; /* MB Transform type */
2153 int mvsw = 0; /* motion vector switch */
2154 int mb_has_coeffs = 1; /* last_flag */
2155 int dmv_x, dmv_y; /* Differential MV components */
2156 int val; /* temp value */
2157 int first_block = 1;
2159 int skipped, direct, twomv = 0;
2160 int block_cbp = 0, pat, block_tt = 0;
2161 int idx_mbmode = 0, mvbp;
2162 int stride_y, fieldtx;
2163 int bmvtype = BMV_TYPE_BACKWARD;
2166 mquant = v->pq; /* Lossy initialization */
2169 skipped = get_bits1(gb);
2171 skipped = v->s.mbskip_table[mb_pos];
2174 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2175 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2177 v->blk_mv_type[s->block_index[0]] = 1;
2178 v->blk_mv_type[s->block_index[1]] = 1;
2179 v->blk_mv_type[s->block_index[2]] = 1;
2180 v->blk_mv_type[s->block_index[3]] = 1;
2182 v->blk_mv_type[s->block_index[0]] = 0;
2183 v->blk_mv_type[s->block_index[1]] = 0;
2184 v->blk_mv_type[s->block_index[2]] = 0;
2185 v->blk_mv_type[s->block_index[3]] = 0;
2190 direct = get_bits1(gb);
2192 direct = v->direct_mb_plane[mb_pos];
2195 if (s->next_picture_ptr->field_picture)
2196 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2197 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);
2198 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);
2199 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);
2200 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);
2203 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);
2204 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);
2205 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);
2206 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);
2208 for (i = 1; i < 4; i += 2) {
2209 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2210 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2211 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2212 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2215 for (i = 1; i < 4; i++) {
2216 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2217 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2218 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2219 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2224 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2225 for (i = 0; i < 4; i++) {
2226 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2227 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2228 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2229 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2231 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2233 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2234 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2235 mb_has_coeffs = get_bits1(gb);
2237 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2238 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2240 s->current_picture.qscale_table[mb_pos] = mquant;
2241 /* Set DC scale - y and c use the same (not sure if necessary here) */
2242 s->y_dc_scale = s->y_dc_scale_table[mquant];
2243 s->c_dc_scale = s->c_dc_scale_table[mquant];
2245 for (i = 0; i < 6; i++) {
2246 v->a_avail = v->c_avail = 0;
2247 v->mb_type[0][s->block_index[i]] = 1;
2248 s->dc_val[0][s->block_index[i]] = 0;
2250 val = ((cbp >> (5 - i)) & 1);
2251 if (i == 2 || i == 3 || !s->first_slice_line)
2252 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2253 if (i == 1 || i == 3 || s->mb_x)
2254 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2256 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2257 (i & 4) ? v->codingset2 : v->codingset);
2258 if (CONFIG_GRAY && i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2260 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2262 stride_y = s->linesize << fieldtx;
2263 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2265 stride_y = s->uvlinesize;
2268 s->idsp.put_signed_pixels_clamped(s->block[i],
2269 s->dest[dst_idx] + off,
2273 s->mb_intra = v->is_intra[s->mb_x] = 0;
2275 if (skipped || !s->mb_intra) {
2276 bmvtype = decode012(gb);
2279 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2282 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2285 bmvtype = BMV_TYPE_INTERPOLATED;
2289 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2290 mvsw = get_bits1(gb);
2293 if (!skipped) { // inter MB
2294 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2296 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2298 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2299 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2300 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2301 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2305 for (i = 0; i < 6; i++)
2306 v->mb_type[0][s->block_index[i]] = 0;
2307 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2308 /* for all motion vector read MVDATA and motion compensate each block */
2312 for (i = 0; i < 4; i++) {
2313 ff_vc1_mc_4mv_luma(v, i, 0, 0);
2314 ff_vc1_mc_4mv_luma(v, i, 1, 1);
2316 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2317 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2319 ff_vc1_mc_1mv(v, 0);
2320 ff_vc1_interp_mc(v);
2322 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2324 for (i = 0; i < 4; i++) {
2327 val = ((mvbp >> (3 - i)) & 1);
2329 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2331 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2332 ff_vc1_mc_4mv_luma(v, j, dir, dir);
2333 ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2336 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2337 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2338 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2342 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2344 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2345 ff_vc1_mc_1mv(v, 0);
2349 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2351 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2352 ff_vc1_interp_mc(v);
2354 dir = bmvtype == BMV_TYPE_BACKWARD;
2361 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2362 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2366 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2367 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2370 for (i = 0; i < 2; i++) {
2371 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];
2372 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];
2373 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];
2374 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];
2377 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2378 ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2381 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2382 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2383 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2384 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2385 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2387 dir = bmvtype == BMV_TYPE_BACKWARD;
2389 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2392 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2394 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2395 v->blk_mv_type[s->block_index[0]] = 1;
2396 v->blk_mv_type[s->block_index[1]] = 1;
2397 v->blk_mv_type[s->block_index[2]] = 1;
2398 v->blk_mv_type[s->block_index[3]] = 1;
2399 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2400 for (i = 0; i < 2; i++) {
2401 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];
2402 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];
2404 ff_vc1_mc_1mv(v, dir);
2408 GET_MQUANT(); // p. 227
2409 s->current_picture.qscale_table[mb_pos] = mquant;
2410 if (!v->ttmbf && cbp)
2411 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2412 for (i = 0; i < 6; i++) {
2413 s->dc_val[0][s->block_index[i]] = 0;
2415 val = ((cbp >> (5 - i)) & 1);
2417 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2419 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2421 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2422 first_block, s->dest[dst_idx] + off,
2423 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2424 CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2425 block_cbp |= pat << (i << 2);
2426 if (!v->ttmbf && ttmb < 8)
2434 for (i = 0; i < 6; i++) {
2435 v->mb_type[0][s->block_index[i]] = 0;
2436 s->dc_val[0][s->block_index[i]] = 0;
2438 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2439 s->current_picture.qscale_table[mb_pos] = 0;
2440 v->blk_mv_type[s->block_index[0]] = 0;
2441 v->blk_mv_type[s->block_index[1]] = 0;
2442 v->blk_mv_type[s->block_index[2]] = 0;
2443 v->blk_mv_type[s->block_index[3]] = 0;
2446 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2447 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2448 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2450 dir = bmvtype == BMV_TYPE_BACKWARD;
2451 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2456 for (i = 0; i < 2; i++) {
2457 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];
2458 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];
2459 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];
2460 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];
2463 v->blk_mv_type[s->block_index[0]] = 1;
2464 v->blk_mv_type[s->block_index[1]] = 1;
2465 v->blk_mv_type[s->block_index[2]] = 1;
2466 v->blk_mv_type[s->block_index[3]] = 1;
2467 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2468 for (i = 0; i < 2; i++) {
2469 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];
2470 s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
2476 ff_vc1_mc_1mv(v, dir);
2477 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2478 ff_vc1_interp_mc(v);
2480 v->fieldtx_plane[mb_pos] = 0;
2483 v->cbp[s->mb_x] = block_cbp;
2484 v->ttblk[s->mb_x] = block_tt;
2489 /** Decode blocks of I-frame
2491 static void vc1_decode_i_blocks(VC1Context *v)
2494 MpegEncContext *s = &v->s;
2499 /* select coding mode used for VLC tables selection */
2500 switch (v->y_ac_table_index) {
2502 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2505 v->codingset = CS_HIGH_MOT_INTRA;
2508 v->codingset = CS_MID_RATE_INTRA;
2512 switch (v->c_ac_table_index) {
2514 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2517 v->codingset2 = CS_HIGH_MOT_INTER;
2520 v->codingset2 = CS_MID_RATE_INTER;
2524 /* Set DC scale - y and c use the same */
2525 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2526 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2529 s->mb_x = s->mb_y = 0;
2531 s->first_slice_line = 1;
2532 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2534 init_block_index(v);
2535 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2537 ff_update_block_index(s);
2538 dst[0] = s->dest[0];
2539 dst[1] = dst[0] + 8;
2540 dst[2] = s->dest[0] + s->linesize * 8;
2541 dst[3] = dst[2] + 8;
2542 dst[4] = s->dest[1];
2543 dst[5] = s->dest[2];
2544 s->bdsp.clear_blocks(s->block[0]);
2545 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2546 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2547 s->current_picture.qscale_table[mb_pos] = v->pq;
2548 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2549 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2551 // do actual MB decoding and displaying
2552 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2553 v->s.ac_pred = get_bits1(&v->s.gb);
2555 for (k = 0; k < 6; k++) {
2556 val = ((cbp >> (5 - k)) & 1);
2559 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2563 cbp |= val << (5 - k);
2565 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2567 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2569 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2570 if (v->pq >= 9 && v->overlap) {
2572 for (j = 0; j < 64; j++)
2573 s->block[k][j] <<= 1;
2574 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2575 k & 4 ? s->uvlinesize
2579 for (j = 0; j < 64; j++)
2580 s->block[k][j] = (s->block[k][j] - 64) << 1;
2581 s->idsp.put_pixels_clamped(s->block[k], dst[k],
2582 k & 4 ? s->uvlinesize
2587 if (v->pq >= 9 && v->overlap) {
2589 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2590 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2591 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2592 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2593 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2596 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2597 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2598 if (!s->first_slice_line) {
2599 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2600 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2601 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
2602 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2603 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2606 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2607 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2609 if (v->s.loop_filter)
2610 ff_vc1_loop_filter_iblk(v, v->pq);
2612 if (get_bits_count(&s->gb) > v->bits) {
2613 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2614 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2615 get_bits_count(&s->gb), v->bits);
2619 if (!v->s.loop_filter)
2620 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2622 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2624 s->first_slice_line = 0;
2626 if (v->s.loop_filter)
2627 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2629 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2630 * profile, these only differ are when decoding MSS2 rectangles. */
2631 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2634 /** Decode blocks of I-frame for advanced profile
2636 static void vc1_decode_i_blocks_adv(VC1Context *v)
2639 MpegEncContext *s = &v->s;
2645 GetBitContext *gb = &s->gb;
2647 /* select coding mode used for VLC tables selection */
2648 switch (v->y_ac_table_index) {
2650 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2653 v->codingset = CS_HIGH_MOT_INTRA;
2656 v->codingset = CS_MID_RATE_INTRA;
2660 switch (v->c_ac_table_index) {
2662 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2665 v->codingset2 = CS_HIGH_MOT_INTER;
2668 v->codingset2 = CS_MID_RATE_INTER;
2673 s->mb_x = s->mb_y = 0;
2675 s->first_slice_line = 1;
2676 s->mb_y = s->start_mb_y;
2677 if (s->start_mb_y) {
2679 init_block_index(v);
2680 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2681 (1 + s->b8_stride) * sizeof(*s->coded_block));
2683 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2685 init_block_index(v);
2686 for (;s->mb_x < s->mb_width; s->mb_x++) {
2687 int16_t (*block)[64] = v->block[v->cur_blk_idx];
2688 ff_update_block_index(s);
2689 s->bdsp.clear_blocks(block[0]);
2690 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2691 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2692 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2693 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2695 // do actual MB decoding and displaying
2696 if (v->fieldtx_is_raw)
2697 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2698 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2699 if (v->acpred_is_raw)
2700 v->s.ac_pred = get_bits1(&v->s.gb);
2702 v->s.ac_pred = v->acpred_plane[mb_pos];
2704 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2705 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2709 s->current_picture.qscale_table[mb_pos] = mquant;
2710 /* Set DC scale - y and c use the same */
2711 s->y_dc_scale = s->y_dc_scale_table[mquant];
2712 s->c_dc_scale = s->c_dc_scale_table[mquant];
2714 for (k = 0; k < 6; k++) {
2715 v->mb_type[0][s->block_index[k]] = 1;
2717 val = ((cbp >> (5 - k)) & 1);
2720 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2724 cbp |= val << (5 - k);
2726 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2727 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2729 vc1_decode_i_block_adv(v, block[k], k, val,
2730 (k < 4) ? v->codingset : v->codingset2, mquant);
2732 if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2734 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2737 ff_vc1_smooth_overlap_filter_iblk(v);
2738 vc1_put_signed_blocks_clamped(v);
2739 if (v->s.loop_filter)
2740 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2742 if (get_bits_count(&s->gb) > v->bits) {
2743 // TODO: may need modification to handle slice coding
2744 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2745 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2746 get_bits_count(&s->gb), v->bits);
2749 inc_blk_idx(v->topleft_blk_idx);
2750 inc_blk_idx(v->top_blk_idx);
2751 inc_blk_idx(v->left_blk_idx);
2752 inc_blk_idx(v->cur_blk_idx);
2754 if (!v->s.loop_filter)
2755 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2757 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2758 s->first_slice_line = 0;
2761 /* raw bottom MB row */
2763 init_block_index(v);
2764 for (; s->mb_x < s->mb_width; s->mb_x++) {
2765 ff_update_block_index(s);
2766 vc1_put_signed_blocks_clamped(v);
2767 if (v->s.loop_filter)
2768 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2769 inc_blk_idx(v->topleft_blk_idx);
2770 inc_blk_idx(v->top_blk_idx);
2771 inc_blk_idx(v->left_blk_idx);
2772 inc_blk_idx(v->cur_blk_idx);
2774 if (v->s.loop_filter)
2775 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2776 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2777 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2780 static void vc1_decode_p_blocks(VC1Context *v)
2782 MpegEncContext *s = &v->s;
2783 int apply_loop_filter;
2785 /* select coding mode used for VLC tables selection */
2786 switch (v->c_ac_table_index) {
2788 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2791 v->codingset = CS_HIGH_MOT_INTRA;
2794 v->codingset = CS_MID_RATE_INTRA;
2798 switch (v->c_ac_table_index) {
2800 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2803 v->codingset2 = CS_HIGH_MOT_INTER;
2806 v->codingset2 = CS_MID_RATE_INTER;
2810 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2811 v->fcm == PROGRESSIVE;
2812 s->first_slice_line = 1;
2813 memset(v->cbp_base, 0, sizeof(v->cbp_base[0]) * 3 * s->mb_stride);
2814 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2816 init_block_index(v);
2817 for (; s->mb_x < s->mb_width; s->mb_x++) {
2818 ff_update_block_index(s);
2820 if (v->fcm == ILACE_FIELD)
2821 vc1_decode_p_mb_intfi(v);
2822 else if (v->fcm == ILACE_FRAME)
2823 vc1_decode_p_mb_intfr(v);
2824 else vc1_decode_p_mb(v);
2825 if (s->mb_y != s->start_mb_y && apply_loop_filter)
2826 ff_vc1_apply_p_loop_filter(v);
2827 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2828 // TODO: may need modification to handle slice coding
2829 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2830 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2831 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2834 inc_blk_idx(v->topleft_blk_idx);
2835 inc_blk_idx(v->top_blk_idx);
2836 inc_blk_idx(v->left_blk_idx);
2837 inc_blk_idx(v->cur_blk_idx);
2839 memmove(v->cbp_base,
2840 v->cbp - s->mb_stride,
2841 sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
2842 memmove(v->ttblk_base,
2843 v->ttblk - s->mb_stride,
2844 sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
2845 memmove(v->is_intra_base,
2846 v->is_intra - s->mb_stride,
2847 sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
2848 memmove(v->luma_mv_base,
2849 v->luma_mv - s->mb_stride,
2850 sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
2851 if (s->mb_y != s->start_mb_y)
2852 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2853 s->first_slice_line = 0;
2855 if (apply_loop_filter) {
2857 init_block_index(v);
2858 for (; s->mb_x < s->mb_width; s->mb_x++) {
2859 ff_update_block_index(s);
2860 ff_vc1_apply_p_loop_filter(v);
2863 if (s->end_mb_y >= s->start_mb_y)
2864 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2865 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2866 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2869 static void vc1_decode_b_blocks(VC1Context *v)
2871 MpegEncContext *s = &v->s;
2873 /* select coding mode used for VLC tables selection */
2874 switch (v->c_ac_table_index) {
2876 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2879 v->codingset = CS_HIGH_MOT_INTRA;
2882 v->codingset = CS_MID_RATE_INTRA;
2886 switch (v->c_ac_table_index) {
2888 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2891 v->codingset2 = CS_HIGH_MOT_INTER;
2894 v->codingset2 = CS_MID_RATE_INTER;
2898 s->first_slice_line = 1;
2899 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2901 init_block_index(v);
2902 for (; s->mb_x < s->mb_width; s->mb_x++) {
2903 ff_update_block_index(s);
2905 if (v->fcm == ILACE_FIELD)
2906 vc1_decode_b_mb_intfi(v);
2907 else if (v->fcm == ILACE_FRAME)
2908 vc1_decode_b_mb_intfr(v);
2911 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2912 // TODO: may need modification to handle slice coding
2913 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2914 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2915 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2918 if (v->s.loop_filter)
2919 ff_vc1_loop_filter_iblk(v, v->pq);
2921 memmove(v->cbp_base,
2922 v->cbp - s->mb_stride,
2923 sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
2924 memmove(v->ttblk_base,
2925 v->ttblk - s->mb_stride,
2926 sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
2927 memmove(v->is_intra_base,
2928 v->is_intra - s->mb_stride,
2929 sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
2930 if (!v->s.loop_filter)
2931 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2933 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2934 s->first_slice_line = 0;
2936 if (v->s.loop_filter)
2937 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2938 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2939 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2942 static void vc1_decode_skip_blocks(VC1Context *v)
2944 MpegEncContext *s = &v->s;
2946 if (!v->s.last_picture.f->data[0])
2949 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2950 s->first_slice_line = 1;
2951 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2953 init_block_index(v);
2954 ff_update_block_index(s);
2955 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2956 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2957 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2958 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2959 s->first_slice_line = 0;
2961 s->pict_type = AV_PICTURE_TYPE_P;
2964 void ff_vc1_decode_blocks(VC1Context *v)
2967 v->s.esc3_level_length = 0;
2969 ff_intrax8_decode_picture(&v->x8, &v->s.current_picture,
2970 &v->s.gb, &v->s.mb_x, &v->s.mb_y,
2971 2 * v->pq + v->halfpq, v->pq * !v->pquantizer,
2972 v->s.loop_filter, v->s.low_delay);
2974 ff_er_add_slice(&v->s.er, 0, 0,
2975 (v->s.mb_x >> 1) - 1, (v->s.mb_y >> 1) - 1,
2979 v->left_blk_idx = -1;
2980 v->topleft_blk_idx = 1;
2982 switch (v->s.pict_type) {
2983 case AV_PICTURE_TYPE_I:
2984 if (v->profile == PROFILE_ADVANCED)
2985 vc1_decode_i_blocks_adv(v);
2987 vc1_decode_i_blocks(v);
2989 case AV_PICTURE_TYPE_P:
2990 if (v->p_frame_skipped)
2991 vc1_decode_skip_blocks(v);
2993 vc1_decode_p_blocks(v);
2995 case AV_PICTURE_TYPE_B:
2997 if (v->profile == PROFILE_ADVANCED)
2998 vc1_decode_i_blocks_adv(v);
3000 vc1_decode_i_blocks(v);
3002 vc1_decode_b_blocks(v);