2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * VC-1 and WMV3 block decoding routines
30 #include "mpegutils.h"
31 #include "mpegvideo.h"
32 #include "msmpeg4data.h"
36 #include "vc1acdata.h"
39 #define MB_INTRA_VLC_BITS 9
42 // offset tables for interlaced picture MVDATA decoding
43 static const uint8_t offset_table[2][9] = {
44 { 0, 1, 2, 4, 8, 16, 32, 64, 128 },
45 { 0, 1, 3, 7, 15, 31, 63, 127, 255 },
48 /***********************************************************************/
50 * @name VC-1 Bitplane decoding
56 static inline void init_block_index(VC1Context *v)
58 MpegEncContext *s = &v->s;
59 ff_init_block_index(s);
60 if (v->field_mode && !(v->second_field ^ v->tff)) {
61 s->dest[0] += s->current_picture_ptr->f->linesize[0];
62 s->dest[1] += s->current_picture_ptr->f->linesize[1];
63 s->dest[2] += s->current_picture_ptr->f->linesize[2];
67 /** @} */ //Bitplane group
69 static void vc1_put_signed_blocks_clamped(VC1Context *v)
71 MpegEncContext *s = &v->s;
72 int topleft_mb_pos, top_mb_pos;
73 int stride_y, fieldtx = 0;
76 /* The put pixels loop is always one MB row behind the decoding loop,
77 * because we can only put pixels when overlap filtering is done, and
78 * for filtering of the bottom edge of a MB, we need the next MB row
80 * Within the row, the put pixels loop is also one MB col behind the
81 * decoding loop. The reason for this is again, because for filtering
82 * of the right MB edge, we need the next MB present. */
83 if (!s->first_slice_line) {
85 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
86 if (v->fcm == ILACE_FRAME)
87 fieldtx = v->fieldtx_plane[topleft_mb_pos];
88 stride_y = s->linesize << fieldtx;
89 v_dist = (16 - fieldtx) >> (fieldtx == 0);
90 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
91 s->dest[0] - 16 * s->linesize - 16,
93 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
94 s->dest[0] - 16 * s->linesize - 8,
96 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
97 s->dest[0] - v_dist * s->linesize - 16,
99 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
100 s->dest[0] - v_dist * s->linesize - 8,
102 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
103 s->dest[1] - 8 * s->uvlinesize - 8,
105 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
106 s->dest[2] - 8 * s->uvlinesize - 8,
109 if (s->mb_x == s->mb_width - 1) {
110 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
111 if (v->fcm == ILACE_FRAME)
112 fieldtx = v->fieldtx_plane[top_mb_pos];
113 stride_y = s->linesize << fieldtx;
114 v_dist = fieldtx ? 15 : 8;
115 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
116 s->dest[0] - 16 * s->linesize,
118 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
119 s->dest[0] - 16 * s->linesize + 8,
121 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
122 s->dest[0] - v_dist * s->linesize,
124 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
125 s->dest[0] - v_dist * s->linesize + 8,
127 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
128 s->dest[1] - 8 * s->uvlinesize,
130 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
131 s->dest[2] - 8 * s->uvlinesize,
136 #define inc_blk_idx(idx) do { \
138 if (idx >= v->n_allocated_blks) \
142 inc_blk_idx(v->topleft_blk_idx);
143 inc_blk_idx(v->top_blk_idx);
144 inc_blk_idx(v->left_blk_idx);
145 inc_blk_idx(v->cur_blk_idx);
148 /***********************************************************************/
150 * @name VC-1 Block-level functions
151 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
157 * @brief Get macroblock-level quantizer scale
159 #define GET_MQUANT() \
160 if (v->dquantfrm) { \
162 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
163 if (v->dqbilevel) { \
164 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
166 mqdiff = get_bits(gb, 3); \
168 mquant = v->pq + mqdiff; \
170 mquant = get_bits(gb, 5); \
173 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
174 edges = 1 << v->dqsbedge; \
175 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
176 edges = (3 << v->dqsbedge) % 15; \
177 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
179 if ((edges&1) && !s->mb_x) \
181 if ((edges&2) && s->first_slice_line) \
183 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
185 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
187 if (!mquant || mquant > 31) { \
188 av_log(v->s.avctx, AV_LOG_ERROR, \
189 "Overriding invalid mquant %d\n", mquant); \
195 * @def GET_MVDATA(_dmv_x, _dmv_y)
196 * @brief Get MV differentials
197 * @see MVDATA decoding from 8.3.5.2, p(1)20
198 * @param _dmv_x Horizontal differential for decoded MV
199 * @param _dmv_y Vertical differential for decoded MV
201 #define GET_MVDATA(_dmv_x, _dmv_y) \
202 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
203 VC1_MV_DIFF_VLC_BITS, 2); \
211 _dmv_x = _dmv_y = 0; \
212 } else if (index == 35) { \
213 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
214 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
215 } else if (index == 36) { \
220 index1 = index % 6; \
221 _dmv_x = offset_table[1][index1]; \
222 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
224 val = get_bits(gb, val); \
225 sign = 0 - (val & 1); \
226 _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign; \
229 index1 = index / 6; \
230 _dmv_y = offset_table[1][index1]; \
231 val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
233 val = get_bits(gb, val); \
234 sign = 0 - (val & 1); \
235 _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign; \
239 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
240 int *dmv_y, int *pred_flag)
243 int extend_x, extend_y;
244 GetBitContext *gb = &v->s.gb;
249 bits = VC1_2REF_MVDATA_VLC_BITS;
252 bits = VC1_1REF_MVDATA_VLC_BITS;
255 extend_x = v->dmvrange & 1;
256 extend_y = (v->dmvrange >> 1) & 1;
257 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
259 *dmv_x = get_bits(gb, v->k_x);
260 *dmv_y = get_bits(gb, v->k_y);
263 *pred_flag = *dmv_y & 1;
264 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
268 av_assert0(index < esc);
269 index1 = (index + 1) % 9;
271 val = get_bits(gb, index1 + extend_x);
272 sign = 0 - (val & 1);
273 *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
276 index1 = (index + 1) / 9;
277 if (index1 > v->numref) {
278 val = get_bits(gb, (index1 >> v->numref) + extend_y);
279 sign = 0 - (val & 1);
280 *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
283 if (v->numref && pred_flag)
284 *pred_flag = index1 & 1;
288 /** Reconstruct motion vector for B-frame and do motion compensation
290 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
291 int direct, int mode)
298 if (mode == BMV_TYPE_INTERPOLATED) {
304 ff_vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
307 /** Get predicted DC value for I-frames only
308 * prediction dir: left=0, top=1
309 * @param s MpegEncContext
310 * @param overlap flag indicating that overlap filtering is used
311 * @param pq integer part of picture quantizer
312 * @param[in] n block index in the current MB
313 * @param dc_val_ptr Pointer to DC predictor
314 * @param dir_ptr Prediction direction for use in AC prediction
316 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
317 int16_t **dc_val_ptr, int *dir_ptr)
319 int a, b, c, wrap, pred, scale;
321 static const uint16_t dcpred[32] = {
322 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
323 114, 102, 93, 85, 79, 73, 68, 64,
324 60, 57, 54, 51, 49, 47, 45, 43,
325 41, 39, 38, 37, 35, 34, 33
328 /* find prediction - wmv3_dc_scale always used here in fact */
329 if (n < 4) scale = s->y_dc_scale;
330 else scale = s->c_dc_scale;
332 wrap = s->block_wrap[n];
333 dc_val = s->dc_val[0] + s->block_index[n];
339 b = dc_val[ - 1 - wrap];
342 if (pq < 9 || !overlap) {
343 /* Set outer values */
344 if (s->first_slice_line && (n != 2 && n != 3))
345 b = a = dcpred[scale];
346 if (s->mb_x == 0 && (n != 1 && n != 3))
347 b = c = dcpred[scale];
349 /* Set outer values */
350 if (s->first_slice_line && (n != 2 && n != 3))
352 if (s->mb_x == 0 && (n != 1 && n != 3))
356 if (abs(a - b) <= abs(b - c)) {
358 *dir_ptr = 1; // left
364 /* update predictor */
365 *dc_val_ptr = &dc_val[0];
370 /** Get predicted DC value
371 * prediction dir: left=0, top=1
372 * @param s MpegEncContext
373 * @param overlap flag indicating that overlap filtering is used
374 * @param pq integer part of picture quantizer
375 * @param[in] n block index in the current MB
376 * @param a_avail flag indicating top block availability
377 * @param c_avail flag indicating left block availability
378 * @param dc_val_ptr Pointer to DC predictor
379 * @param dir_ptr Prediction direction for use in AC prediction
381 static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
382 int a_avail, int c_avail,
383 int16_t **dc_val_ptr, int *dir_ptr)
385 int a, b, c, wrap, pred;
387 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
391 wrap = s->block_wrap[n];
392 dc_val = s->dc_val[0] + s->block_index[n];
398 b = dc_val[ - 1 - wrap];
400 /* scale predictors if needed */
401 q1 = s->current_picture.qscale_table[mb_pos];
402 dqscale_index = s->y_dc_scale_table[q1] - 1;
403 if (dqscale_index < 0)
405 if (c_avail && (n != 1 && n != 3)) {
406 q2 = s->current_picture.qscale_table[mb_pos - 1];
408 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
410 if (a_avail && (n != 2 && n != 3)) {
411 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
413 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
415 if (a_avail && c_avail && (n != 3)) {
421 q2 = s->current_picture.qscale_table[off];
423 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
426 if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
428 *dir_ptr = 1; // left
429 } else if (a_avail) {
434 *dir_ptr = 1; // left
437 /* update predictor */
438 *dc_val_ptr = &dc_val[0];
442 /** @} */ // Block group
445 * @name VC1 Macroblock-level functions in Simple/Main Profiles
446 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
450 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
451 uint8_t **coded_block_ptr)
453 int xy, wrap, pred, a, b, c;
455 xy = s->block_index[n];
461 a = s->coded_block[xy - 1 ];
462 b = s->coded_block[xy - 1 - wrap];
463 c = s->coded_block[xy - wrap];
472 *coded_block_ptr = &s->coded_block[xy];
478 * Decode one AC coefficient
479 * @param v The VC1 context
480 * @param last Last coefficient
481 * @param skip How much zero coefficients to skip
482 * @param value Decoded AC coefficient value
483 * @param codingset set of VLC to decode data
486 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
487 int *value, int codingset)
489 GetBitContext *gb = &v->s.gb;
490 int index, run, level, lst, sign;
492 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
493 if (index != ff_vc1_ac_sizes[codingset] - 1) {
494 run = vc1_index_decode_table[codingset][index][0];
495 level = vc1_index_decode_table[codingset][index][1];
496 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
497 sign = get_bits1(gb);
499 int escape = decode210(gb);
501 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
502 run = vc1_index_decode_table[codingset][index][0];
503 level = vc1_index_decode_table[codingset][index][1];
504 lst = index >= vc1_last_decode_table[codingset];
507 level += vc1_last_delta_level_table[codingset][run];
509 level += vc1_delta_level_table[codingset][run];
512 run += vc1_last_delta_run_table[codingset][level] + 1;
514 run += vc1_delta_run_table[codingset][level] + 1;
516 sign = get_bits1(gb);
519 if (v->s.esc3_level_length == 0) {
520 if (v->pq < 8 || v->dquantfrm) { // table 59
521 v->s.esc3_level_length = get_bits(gb, 3);
522 if (!v->s.esc3_level_length)
523 v->s.esc3_level_length = get_bits(gb, 2) + 8;
525 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
527 v->s.esc3_run_length = 3 + get_bits(gb, 2);
529 run = get_bits(gb, v->s.esc3_run_length);
530 sign = get_bits1(gb);
531 level = get_bits(gb, v->s.esc3_level_length);
537 *value = (level ^ -sign) + sign;
540 /** Decode intra block in intra frames - should be faster than decode_intra_block
541 * @param v VC1Context
542 * @param block block to decode
543 * @param[in] n subblock index
544 * @param coded are AC coeffs present or not
545 * @param codingset set of VLC to decode data
547 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
548 int coded, int codingset)
550 GetBitContext *gb = &v->s.gb;
551 MpegEncContext *s = &v->s;
552 int dc_pred_dir = 0; /* Direction of the DC prediction used */
555 int16_t *ac_val, *ac_val2;
558 /* Get DC differential */
560 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
562 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
565 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
569 const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
570 if (dcdiff == 119 /* ESC index value */) {
571 dcdiff = get_bits(gb, 8 + m);
574 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
581 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
584 /* Store the quantized DC coeff, used for prediction */
586 block[0] = dcdiff * s->y_dc_scale;
588 block[0] = dcdiff * s->c_dc_scale;
599 int last = 0, skip, value;
600 const uint8_t *zz_table;
604 scale = v->pq * 2 + v->halfpq;
608 zz_table = v->zz_8x8[2];
610 zz_table = v->zz_8x8[3];
612 zz_table = v->zz_8x8[1];
614 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
616 if (dc_pred_dir) // left
619 ac_val -= 16 * s->block_wrap[n];
622 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
626 block[zz_table[i++]] = value;
629 /* apply AC prediction if needed */
631 if (dc_pred_dir) { // left
632 for (k = 1; k < 8; k++)
633 block[k << v->left_blk_sh] += ac_val[k];
635 for (k = 1; k < 8; k++)
636 block[k << v->top_blk_sh] += ac_val[k + 8];
639 /* save AC coeffs for further prediction */
640 for (k = 1; k < 8; k++) {
641 ac_val2[k] = block[k << v->left_blk_sh];
642 ac_val2[k + 8] = block[k << v->top_blk_sh];
645 /* scale AC coeffs */
646 for (k = 1; k < 64; k++)
650 block[k] += (block[k] < 0) ? -v->pq : v->pq;
653 if (s->ac_pred) i = 63;
659 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
663 scale = v->pq * 2 + v->halfpq;
664 memset(ac_val2, 0, 16 * 2);
665 if (dc_pred_dir) { // left
668 memcpy(ac_val2, ac_val, 8 * 2);
670 ac_val -= 16 * s->block_wrap[n];
672 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
675 /* apply AC prediction if needed */
677 if (dc_pred_dir) { //left
678 for (k = 1; k < 8; k++) {
679 block[k << v->left_blk_sh] = ac_val[k] * scale;
680 if (!v->pquantizer && block[k << v->left_blk_sh])
681 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
684 for (k = 1; k < 8; k++) {
685 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
686 if (!v->pquantizer && block[k << v->top_blk_sh])
687 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
693 s->block_last_index[n] = i;
698 /** Decode intra block in intra frames - should be faster than decode_intra_block
699 * @param v VC1Context
700 * @param block block to decode
701 * @param[in] n subblock number
702 * @param coded are AC coeffs present or not
703 * @param codingset set of VLC to decode data
704 * @param mquant quantizer value for this macroblock
706 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
707 int coded, int codingset, int mquant)
709 GetBitContext *gb = &v->s.gb;
710 MpegEncContext *s = &v->s;
711 int dc_pred_dir = 0; /* Direction of the DC prediction used */
713 int16_t *dc_val = NULL;
714 int16_t *ac_val, *ac_val2;
716 int a_avail = v->a_avail, c_avail = v->c_avail;
717 int use_pred = s->ac_pred;
720 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
722 /* Get DC differential */
724 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
726 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
729 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
733 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
734 if (dcdiff == 119 /* ESC index value */) {
735 dcdiff = get_bits(gb, 8 + m);
738 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
745 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
748 /* Store the quantized DC coeff, used for prediction */
750 block[0] = dcdiff * s->y_dc_scale;
752 block[0] = dcdiff * s->c_dc_scale;
758 /* check if AC is needed at all */
759 if (!a_avail && !c_avail)
761 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
764 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
766 if (dc_pred_dir) // left
769 ac_val -= 16 * s->block_wrap[n];
771 q1 = s->current_picture.qscale_table[mb_pos];
772 if ( dc_pred_dir && c_avail && mb_pos)
773 q2 = s->current_picture.qscale_table[mb_pos - 1];
774 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
775 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
776 if ( dc_pred_dir && n == 1)
778 if (!dc_pred_dir && n == 2)
784 int last = 0, skip, value;
785 const uint8_t *zz_table;
789 if (!use_pred && v->fcm == ILACE_FRAME) {
790 zz_table = v->zzi_8x8;
792 if (!dc_pred_dir) // top
793 zz_table = v->zz_8x8[2];
795 zz_table = v->zz_8x8[3];
798 if (v->fcm != ILACE_FRAME)
799 zz_table = v->zz_8x8[1];
801 zz_table = v->zzi_8x8;
805 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
809 block[zz_table[i++]] = value;
812 /* apply AC prediction if needed */
814 /* scale predictors if needed*/
815 if (q2 && q1 != q2) {
816 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
817 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
820 return AVERROR_INVALIDDATA;
821 if (dc_pred_dir) { // left
822 for (k = 1; k < 8; k++)
823 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
825 for (k = 1; k < 8; k++)
826 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
829 if (dc_pred_dir) { //left
830 for (k = 1; k < 8; k++)
831 block[k << v->left_blk_sh] += ac_val[k];
833 for (k = 1; k < 8; k++)
834 block[k << v->top_blk_sh] += ac_val[k + 8];
838 /* save AC coeffs for further prediction */
839 for (k = 1; k < 8; k++) {
840 ac_val2[k ] = block[k << v->left_blk_sh];
841 ac_val2[k + 8] = block[k << v->top_blk_sh];
844 /* scale AC coeffs */
845 for (k = 1; k < 64; k++)
849 block[k] += (block[k] < 0) ? -mquant : mquant;
852 if (use_pred) i = 63;
853 } else { // no AC coeffs
856 memset(ac_val2, 0, 16 * 2);
857 if (dc_pred_dir) { // left
859 memcpy(ac_val2, ac_val, 8 * 2);
860 if (q2 && q1 != q2) {
861 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
862 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
864 return AVERROR_INVALIDDATA;
865 for (k = 1; k < 8; k++)
866 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
871 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
872 if (q2 && q1 != q2) {
873 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
874 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
876 return AVERROR_INVALIDDATA;
877 for (k = 1; k < 8; k++)
878 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
883 /* apply AC prediction if needed */
885 if (dc_pred_dir) { // left
886 for (k = 1; k < 8; k++) {
887 block[k << v->left_blk_sh] = ac_val2[k] * scale;
888 if (!v->pquantizer && block[k << v->left_blk_sh])
889 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
892 for (k = 1; k < 8; k++) {
893 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
894 if (!v->pquantizer && block[k << v->top_blk_sh])
895 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
901 s->block_last_index[n] = i;
906 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
907 * @param v VC1Context
908 * @param block block to decode
909 * @param[in] n subblock index
910 * @param coded are AC coeffs present or not
911 * @param mquant block quantizer
912 * @param codingset set of VLC to decode data
914 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
915 int coded, int mquant, int codingset)
917 GetBitContext *gb = &v->s.gb;
918 MpegEncContext *s = &v->s;
919 int dc_pred_dir = 0; /* Direction of the DC prediction used */
921 int16_t *dc_val = NULL;
922 int16_t *ac_val, *ac_val2;
924 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
925 int a_avail = v->a_avail, c_avail = v->c_avail;
926 int use_pred = s->ac_pred;
930 s->bdsp.clear_block(block);
932 /* XXX: Guard against dumb values of mquant */
933 mquant = av_clip_uintp2(mquant, 5);
935 /* Set DC scale - y and c use the same */
936 s->y_dc_scale = s->y_dc_scale_table[mquant];
937 s->c_dc_scale = s->c_dc_scale_table[mquant];
939 /* Get DC differential */
941 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
943 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
946 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
950 const int m = (mquant == 1 || mquant == 2) ? 3 - mquant : 0;
951 if (dcdiff == 119 /* ESC index value */) {
952 dcdiff = get_bits(gb, 8 + m);
955 dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
962 dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
965 /* Store the quantized DC coeff, used for prediction */
968 block[0] = dcdiff * s->y_dc_scale;
970 block[0] = dcdiff * s->c_dc_scale;
976 /* check if AC is needed at all and adjust direction if needed */
977 if (!a_avail) dc_pred_dir = 1;
978 if (!c_avail) dc_pred_dir = 0;
979 if (!a_avail && !c_avail) use_pred = 0;
980 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
983 scale = mquant * 2 + v->halfpq;
985 if (dc_pred_dir) //left
988 ac_val -= 16 * s->block_wrap[n];
990 q1 = s->current_picture.qscale_table[mb_pos];
991 if (dc_pred_dir && c_avail && mb_pos)
992 q2 = s->current_picture.qscale_table[mb_pos - 1];
993 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
994 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
995 if ( dc_pred_dir && n == 1)
997 if (!dc_pred_dir && n == 2)
1002 int last = 0, skip, value;
1006 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1010 if (v->fcm == PROGRESSIVE)
1011 block[v->zz_8x8[0][i++]] = value;
1013 if (use_pred && (v->fcm == ILACE_FRAME)) {
1014 if (!dc_pred_dir) // top
1015 block[v->zz_8x8[2][i++]] = value;
1017 block[v->zz_8x8[3][i++]] = value;
1019 block[v->zzi_8x8[i++]] = value;
1024 /* apply AC prediction if needed */
1026 /* scale predictors if needed*/
1027 if (q2 && q1 != q2) {
1028 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1029 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1032 return AVERROR_INVALIDDATA;
1033 if (dc_pred_dir) { // left
1034 for (k = 1; k < 8; k++)
1035 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1037 for (k = 1; k < 8; k++)
1038 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1041 if (dc_pred_dir) { // left
1042 for (k = 1; k < 8; k++)
1043 block[k << v->left_blk_sh] += ac_val[k];
1045 for (k = 1; k < 8; k++)
1046 block[k << v->top_blk_sh] += ac_val[k + 8];
1050 /* save AC coeffs for further prediction */
1051 for (k = 1; k < 8; k++) {
1052 ac_val2[k ] = block[k << v->left_blk_sh];
1053 ac_val2[k + 8] = block[k << v->top_blk_sh];
1056 /* scale AC coeffs */
1057 for (k = 1; k < 64; k++)
1061 block[k] += (block[k] < 0) ? -mquant : mquant;
1064 if (use_pred) i = 63;
1065 } else { // no AC coeffs
1068 memset(ac_val2, 0, 16 * 2);
1069 if (dc_pred_dir) { // left
1071 memcpy(ac_val2, ac_val, 8 * 2);
1072 if (q2 && q1 != q2) {
1073 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1074 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1076 return AVERROR_INVALIDDATA;
1077 for (k = 1; k < 8; k++)
1078 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1083 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1084 if (q2 && q1 != q2) {
1085 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1086 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1088 return AVERROR_INVALIDDATA;
1089 for (k = 1; k < 8; k++)
1090 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1095 /* apply AC prediction if needed */
1097 if (dc_pred_dir) { // left
1098 for (k = 1; k < 8; k++) {
1099 block[k << v->left_blk_sh] = ac_val2[k] * scale;
1100 if (!v->pquantizer && block[k << v->left_blk_sh])
1101 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
1104 for (k = 1; k < 8; k++) {
1105 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1106 if (!v->pquantizer && block[k << v->top_blk_sh])
1107 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
1113 s->block_last_index[n] = i;
1120 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1121 int mquant, int ttmb, int first_block,
1122 uint8_t *dst, int linesize, int skip_block,
1125 MpegEncContext *s = &v->s;
1126 GetBitContext *gb = &s->gb;
1129 int scale, off, idx, last, skip, value;
1130 int ttblk = ttmb & 7;
1133 s->bdsp.clear_block(block);
1136 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)];
1138 if (ttblk == TT_4X4) {
1139 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1141 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1142 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1143 || (!v->res_rtm_flag && !first_block))) {
1144 subblkpat = decode012(gb);
1146 subblkpat ^= 3; // swap decoded pattern bits
1147 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1149 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1152 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
1154 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1155 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1156 subblkpat = 2 - (ttblk == TT_8X4_TOP);
1159 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1160 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1169 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1174 idx = v->zz_8x8[0][i++];
1176 idx = v->zzi_8x8[i++];
1177 block[idx] = value * scale;
1179 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1183 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1185 v->vc1dsp.vc1_inv_trans_8x8(block);
1186 s->idsp.add_pixels_clamped(block, dst, linesize);
1191 pat = ~subblkpat & 0xF;
1192 for (j = 0; j < 4; j++) {
1193 last = subblkpat & (1 << (3 - j));
1195 off = (j & 1) * 4 + (j & 2) * 16;
1197 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1202 idx = ff_vc1_simple_progressive_4x4_zz[i++];
1204 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
1205 block[idx + off] = value * scale;
1207 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
1209 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1211 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1213 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1218 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1219 for (j = 0; j < 2; j++) {
1220 last = subblkpat & (1 << (1 - j));
1224 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1229 idx = v->zz_8x4[i++] + off;
1231 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1232 block[idx] = value * scale;
1234 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1236 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1238 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1240 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1245 pat = ~(subblkpat * 5) & 0xF;
1246 for (j = 0; j < 2; j++) {
1247 last = subblkpat & (1 << (1 - j));
1251 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1256 idx = v->zz_4x8[i++] + off;
1258 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1259 block[idx] = value * scale;
1261 block[idx] += (block[idx] < 0) ? -mquant : mquant;
1263 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1265 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1267 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1273 *ttmb_out |= ttblk << (n * 4);
1277 /** @} */ // Macroblock group
1279 static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 };
1281 /** Decode one P-frame MB
1283 static int vc1_decode_p_mb(VC1Context *v)
1285 MpegEncContext *s = &v->s;
1286 GetBitContext *gb = &s->gb;
1288 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1289 int cbp; /* cbp decoding stuff */
1290 int mqdiff, mquant; /* MB quantization */
1291 int ttmb = v->ttfrm; /* MB Transform type */
1293 int mb_has_coeffs = 1; /* last_flag */
1294 int dmv_x, dmv_y; /* Differential MV components */
1295 int index, index1; /* LUT indexes */
1296 int val, sign; /* temp values */
1297 int first_block = 1;
1299 int skipped, fourmv;
1300 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1302 mquant = v->pq; /* lossy initialization */
1304 if (v->mv_type_is_raw)
1305 fourmv = get_bits1(gb);
1307 fourmv = v->mv_type_mb_plane[mb_pos];
1309 skipped = get_bits1(gb);
1311 skipped = v->s.mbskip_table[mb_pos];
1313 if (!fourmv) { /* 1MV mode */
1315 GET_MVDATA(dmv_x, dmv_y);
1318 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1319 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1321 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1322 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1324 /* FIXME Set DC val for inter block ? */
1325 if (s->mb_intra && !mb_has_coeffs) {
1327 s->ac_pred = get_bits1(gb);
1329 } else if (mb_has_coeffs) {
1331 s->ac_pred = get_bits1(gb);
1332 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1338 s->current_picture.qscale_table[mb_pos] = mquant;
1340 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1341 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
1342 VC1_TTMB_VLC_BITS, 2);
1343 if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1345 for (i = 0; i < 6; i++) {
1346 s->dc_val[0][s->block_index[i]] = 0;
1348 val = ((cbp >> (5 - i)) & 1);
1349 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1350 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1352 /* check if prediction blocks A and C are available */
1353 v->a_avail = v->c_avail = 0;
1354 if (i == 2 || i == 3 || !s->first_slice_line)
1355 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1356 if (i == 1 || i == 3 || s->mb_x)
1357 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1359 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1360 (i & 4) ? v->codingset2 : v->codingset);
1361 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1363 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1365 for (j = 0; j < 64; j++)
1366 s->block[i][j] <<= 1;
1367 s->idsp.put_signed_pixels_clamped(s->block[i],
1368 s->dest[dst_idx] + off,
1369 i & 4 ? s->uvlinesize
1371 if (v->pq >= 9 && v->overlap) {
1373 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1375 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1377 block_cbp |= 0xF << (i << 2);
1378 block_intra |= 1 << i;
1380 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
1381 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1382 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1383 block_cbp |= pat << (i << 2);
1384 if (!v->ttmbf && ttmb < 8)
1391 for (i = 0; i < 6; i++) {
1392 v->mb_type[0][s->block_index[i]] = 0;
1393 s->dc_val[0][s->block_index[i]] = 0;
1395 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1396 s->current_picture.qscale_table[mb_pos] = 0;
1397 ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1398 ff_vc1_mc_1mv(v, 0);
1400 } else { // 4MV mode
1401 if (!skipped /* unskipped MB */) {
1402 int intra_count = 0, coded_inter = 0;
1403 int is_intra[6], is_coded[6];
1405 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1406 for (i = 0; i < 6; i++) {
1407 val = ((cbp >> (5 - i)) & 1);
1408 s->dc_val[0][s->block_index[i]] = 0;
1415 GET_MVDATA(dmv_x, dmv_y);
1417 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1419 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1420 intra_count += s->mb_intra;
1421 is_intra[i] = s->mb_intra;
1422 is_coded[i] = mb_has_coeffs;
1425 is_intra[i] = (intra_count >= 3);
1429 ff_vc1_mc_4mv_chroma(v, 0);
1430 v->mb_type[0][s->block_index[i]] = is_intra[i];
1432 coded_inter = !is_intra[i] & is_coded[i];
1434 // if there are no coded blocks then don't do anything more
1436 if (!intra_count && !coded_inter)
1439 s->current_picture.qscale_table[mb_pos] = mquant;
1440 /* test if block is intra and has pred */
1443 for (i = 0; i < 6; i++)
1445 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
1446 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
1452 s->ac_pred = get_bits1(gb);
1456 if (!v->ttmbf && coded_inter)
1457 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1458 for (i = 0; i < 6; i++) {
1460 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1461 s->mb_intra = is_intra[i];
1463 /* check if prediction blocks A and C are available */
1464 v->a_avail = v->c_avail = 0;
1465 if (i == 2 || i == 3 || !s->first_slice_line)
1466 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1467 if (i == 1 || i == 3 || s->mb_x)
1468 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1470 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
1471 (i & 4) ? v->codingset2 : v->codingset);
1472 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1474 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1476 for (j = 0; j < 64; j++)
1477 s->block[i][j] <<= 1;
1478 s->idsp.put_signed_pixels_clamped(s->block[i],
1479 s->dest[dst_idx] + off,
1480 (i & 4) ? s->uvlinesize
1482 if (v->pq >= 9 && v->overlap) {
1484 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1486 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
1488 block_cbp |= 0xF << (i << 2);
1489 block_intra |= 1 << i;
1490 } else if (is_coded[i]) {
1491 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1492 first_block, s->dest[dst_idx] + off,
1493 (i & 4) ? s->uvlinesize : s->linesize,
1494 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1496 block_cbp |= pat << (i << 2);
1497 if (!v->ttmbf && ttmb < 8)
1502 } else { // skipped MB
1504 s->current_picture.qscale_table[mb_pos] = 0;
1505 for (i = 0; i < 6; i++) {
1506 v->mb_type[0][s->block_index[i]] = 0;
1507 s->dc_val[0][s->block_index[i]] = 0;
1509 for (i = 0; i < 4; i++) {
1510 ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
1511 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1513 ff_vc1_mc_4mv_chroma(v, 0);
1514 s->current_picture.qscale_table[mb_pos] = 0;
1518 v->cbp[s->mb_x] = block_cbp;
1519 v->ttblk[s->mb_x] = block_tt;
1520 v->is_intra[s->mb_x] = block_intra;
1525 /* Decode one macroblock in an interlaced frame p picture */
1527 static int vc1_decode_p_mb_intfr(VC1Context *v)
1529 MpegEncContext *s = &v->s;
1530 GetBitContext *gb = &s->gb;
1532 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1533 int cbp = 0; /* cbp decoding stuff */
1534 int mqdiff, mquant; /* MB quantization */
1535 int ttmb = v->ttfrm; /* MB Transform type */
1537 int mb_has_coeffs = 1; /* last_flag */
1538 int dmv_x, dmv_y; /* Differential MV components */
1539 int val; /* temp value */
1540 int first_block = 1;
1542 int skipped, fourmv = 0, twomv = 0;
1543 int block_cbp = 0, pat, block_tt = 0;
1544 int idx_mbmode = 0, mvbp;
1545 int stride_y, fieldtx;
1547 mquant = v->pq; /* Lossy initialization */
1550 skipped = get_bits1(gb);
1552 skipped = v->s.mbskip_table[mb_pos];
1554 if (v->fourmvswitch)
1555 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1557 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1558 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1559 /* store the motion vector type in a flag (useful later) */
1560 case MV_PMODE_INTFR_4MV:
1562 v->blk_mv_type[s->block_index[0]] = 0;
1563 v->blk_mv_type[s->block_index[1]] = 0;
1564 v->blk_mv_type[s->block_index[2]] = 0;
1565 v->blk_mv_type[s->block_index[3]] = 0;
1567 case MV_PMODE_INTFR_4MV_FIELD:
1569 v->blk_mv_type[s->block_index[0]] = 1;
1570 v->blk_mv_type[s->block_index[1]] = 1;
1571 v->blk_mv_type[s->block_index[2]] = 1;
1572 v->blk_mv_type[s->block_index[3]] = 1;
1574 case MV_PMODE_INTFR_2MV_FIELD:
1576 v->blk_mv_type[s->block_index[0]] = 1;
1577 v->blk_mv_type[s->block_index[1]] = 1;
1578 v->blk_mv_type[s->block_index[2]] = 1;
1579 v->blk_mv_type[s->block_index[3]] = 1;
1581 case MV_PMODE_INTFR_1MV:
1582 v->blk_mv_type[s->block_index[0]] = 0;
1583 v->blk_mv_type[s->block_index[1]] = 0;
1584 v->blk_mv_type[s->block_index[2]] = 0;
1585 v->blk_mv_type[s->block_index[3]] = 0;
1588 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1589 for (i = 0; i < 4; i++) {
1590 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
1591 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
1593 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1595 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
1596 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1597 mb_has_coeffs = get_bits1(gb);
1599 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1600 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1602 s->current_picture.qscale_table[mb_pos] = mquant;
1603 /* Set DC scale - y and c use the same (not sure if necessary here) */
1604 s->y_dc_scale = s->y_dc_scale_table[mquant];
1605 s->c_dc_scale = s->c_dc_scale_table[mquant];
1607 for (i = 0; i < 6; i++) {
1608 v->a_avail = v->c_avail = 0;
1609 v->mb_type[0][s->block_index[i]] = 1;
1610 s->dc_val[0][s->block_index[i]] = 0;
1612 val = ((cbp >> (5 - i)) & 1);
1613 if (i == 2 || i == 3 || !s->first_slice_line)
1614 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1615 if (i == 1 || i == 3 || s->mb_x)
1616 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1618 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1619 (i & 4) ? v->codingset2 : v->codingset);
1620 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
1621 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1623 stride_y = s->linesize << fieldtx;
1624 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
1626 stride_y = s->uvlinesize;
1629 s->idsp.put_signed_pixels_clamped(s->block[i],
1630 s->dest[dst_idx] + off,
1635 } else { // inter MB
1636 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1638 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1639 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1640 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
1642 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1643 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1644 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1647 s->mb_intra = v->is_intra[s->mb_x] = 0;
1648 for (i = 0; i < 6; i++)
1649 v->mb_type[0][s->block_index[i]] = 0;
1650 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1651 /* for all motion vector read MVDATA and motion compensate each block */
1655 for (i = 0; i < 4; i++) {
1657 if (mvbp & (8 >> i))
1658 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1659 ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
1660 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1662 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1667 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1669 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1670 ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1671 ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1674 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1676 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
1677 ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1678 ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1679 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1681 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1684 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1686 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1687 ff_vc1_mc_1mv(v, 0);
1690 GET_MQUANT(); // p. 227
1691 s->current_picture.qscale_table[mb_pos] = mquant;
1692 if (!v->ttmbf && cbp)
1693 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1694 for (i = 0; i < 6; i++) {
1695 s->dc_val[0][s->block_index[i]] = 0;
1697 val = ((cbp >> (5 - i)) & 1);
1699 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1701 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1703 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1704 first_block, s->dest[dst_idx] + off,
1705 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1706 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
1707 block_cbp |= pat << (i << 2);
1708 if (!v->ttmbf && ttmb < 8)
1715 s->mb_intra = v->is_intra[s->mb_x] = 0;
1716 for (i = 0; i < 6; i++) {
1717 v->mb_type[0][s->block_index[i]] = 0;
1718 s->dc_val[0][s->block_index[i]] = 0;
1720 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
1721 s->current_picture.qscale_table[mb_pos] = 0;
1722 v->blk_mv_type[s->block_index[0]] = 0;
1723 v->blk_mv_type[s->block_index[1]] = 0;
1724 v->blk_mv_type[s->block_index[2]] = 0;
1725 v->blk_mv_type[s->block_index[3]] = 0;
1726 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
1727 ff_vc1_mc_1mv(v, 0);
1729 if (s->mb_x == s->mb_width - 1)
1730 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
1734 static int vc1_decode_p_mb_intfi(VC1Context *v)
1736 MpegEncContext *s = &v->s;
1737 GetBitContext *gb = &s->gb;
1739 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1740 int cbp = 0; /* cbp decoding stuff */
1741 int mqdiff, mquant; /* MB quantization */
1742 int ttmb = v->ttfrm; /* MB Transform type */
1744 int mb_has_coeffs = 1; /* last_flag */
1745 int dmv_x, dmv_y; /* Differential MV components */
1746 int val; /* temp values */
1747 int first_block = 1;
1750 int block_cbp = 0, pat, block_tt = 0;
1753 mquant = v->pq; /* Lossy initialization */
1755 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
1756 if (idx_mbmode <= 1) { // intra MB
1757 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1759 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1760 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1761 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1763 s->current_picture.qscale_table[mb_pos] = mquant;
1764 /* Set DC scale - y and c use the same (not sure if necessary here) */
1765 s->y_dc_scale = s->y_dc_scale_table[mquant];
1766 s->c_dc_scale = s->c_dc_scale_table[mquant];
1767 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1768 mb_has_coeffs = idx_mbmode & 1;
1770 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
1772 for (i = 0; i < 6; i++) {
1773 v->a_avail = v->c_avail = 0;
1774 v->mb_type[0][s->block_index[i]] = 1;
1775 s->dc_val[0][s->block_index[i]] = 0;
1777 val = ((cbp >> (5 - i)) & 1);
1778 if (i == 2 || i == 3 || !s->first_slice_line)
1779 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1780 if (i == 1 || i == 3 || s->mb_x)
1781 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1783 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1784 (i & 4) ? v->codingset2 : v->codingset);
1785 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1787 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1788 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1789 s->idsp.put_signed_pixels_clamped(s->block[i],
1790 s->dest[dst_idx] + off,
1791 (i & 4) ? s->uvlinesize
1793 // TODO: loop filter
1796 s->mb_intra = v->is_intra[s->mb_x] = 0;
1797 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1798 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
1799 if (idx_mbmode <= 5) { // 1-MV
1800 dmv_x = dmv_y = pred_flag = 0;
1801 if (idx_mbmode & 1) {
1802 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1804 ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1805 ff_vc1_mc_1mv(v, 0);
1806 mb_has_coeffs = !(idx_mbmode & 2);
1808 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
1809 for (i = 0; i < 4; i++) {
1810 dmv_x = dmv_y = pred_flag = 0;
1811 if (v->fourmvbp & (8 >> i))
1812 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1813 ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
1814 ff_vc1_mc_4mv_luma(v, i, 0, 0);
1816 ff_vc1_mc_4mv_chroma(v, 0);
1817 mb_has_coeffs = idx_mbmode & 1;
1820 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1824 s->current_picture.qscale_table[mb_pos] = mquant;
1825 if (!v->ttmbf && cbp) {
1826 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1829 for (i = 0; i < 6; i++) {
1830 s->dc_val[0][s->block_index[i]] = 0;
1832 val = ((cbp >> (5 - i)) & 1);
1833 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1835 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1836 first_block, s->dest[dst_idx] + off,
1837 (i & 4) ? s->uvlinesize : s->linesize,
1838 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
1840 block_cbp |= pat << (i << 2);
1841 if (!v->ttmbf && ttmb < 8) ttmb = -1;
1846 if (s->mb_x == s->mb_width - 1)
1847 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
1851 /** Decode one B-frame MB (in Main profile)
1853 static void vc1_decode_b_mb(VC1Context *v)
1855 MpegEncContext *s = &v->s;
1856 GetBitContext *gb = &s->gb;
1858 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1859 int cbp = 0; /* cbp decoding stuff */
1860 int mqdiff, mquant; /* MB quantization */
1861 int ttmb = v->ttfrm; /* MB Transform type */
1862 int mb_has_coeffs = 0; /* last_flag */
1863 int index, index1; /* LUT indexes */
1864 int val, sign; /* temp values */
1865 int first_block = 1;
1867 int skipped, direct;
1868 int dmv_x[2], dmv_y[2];
1869 int bmvtype = BMV_TYPE_BACKWARD;
1871 mquant = v->pq; /* lossy initialization */
1875 direct = get_bits1(gb);
1877 direct = v->direct_mb_plane[mb_pos];
1879 skipped = get_bits1(gb);
1881 skipped = v->s.mbskip_table[mb_pos];
1883 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1884 for (i = 0; i < 6; i++) {
1885 v->mb_type[0][s->block_index[i]] = 0;
1886 s->dc_val[0][s->block_index[i]] = 0;
1888 s->current_picture.qscale_table[mb_pos] = 0;
1892 GET_MVDATA(dmv_x[0], dmv_y[0]);
1893 dmv_x[1] = dmv_x[0];
1894 dmv_y[1] = dmv_y[0];
1896 if (skipped || !s->mb_intra) {
1897 bmvtype = decode012(gb);
1900 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1903 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1906 bmvtype = BMV_TYPE_INTERPOLATED;
1907 dmv_x[0] = dmv_y[0] = 0;
1911 for (i = 0; i < 6; i++)
1912 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1916 bmvtype = BMV_TYPE_INTERPOLATED;
1917 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1918 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1922 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1925 s->current_picture.qscale_table[mb_pos] = mquant;
1927 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1928 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1929 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1930 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1932 if (!mb_has_coeffs && !s->mb_intra) {
1933 /* no coded blocks - effectively skipped */
1934 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1935 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1938 if (s->mb_intra && !mb_has_coeffs) {
1940 s->current_picture.qscale_table[mb_pos] = mquant;
1941 s->ac_pred = get_bits1(gb);
1943 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1945 if (bmvtype == BMV_TYPE_INTERPOLATED) {
1946 GET_MVDATA(dmv_x[0], dmv_y[0]);
1947 if (!mb_has_coeffs) {
1948 /* interpolated skipped block */
1949 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1950 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1954 ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1956 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1959 s->ac_pred = get_bits1(gb);
1960 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
1962 s->current_picture.qscale_table[mb_pos] = mquant;
1963 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1964 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
1968 for (i = 0; i < 6; i++) {
1969 s->dc_val[0][s->block_index[i]] = 0;
1971 val = ((cbp >> (5 - i)) & 1);
1972 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1973 v->mb_type[0][s->block_index[i]] = s->mb_intra;
1975 /* check if prediction blocks A and C are available */
1976 v->a_avail = v->c_avail = 0;
1977 if (i == 2 || i == 3 || !s->first_slice_line)
1978 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
1979 if (i == 1 || i == 3 || s->mb_x)
1980 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
1982 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
1983 (i & 4) ? v->codingset2 : v->codingset);
1984 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
1986 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
1988 for (j = 0; j < 64; j++)
1989 s->block[i][j] <<= 1;
1990 s->idsp.put_signed_pixels_clamped(s->block[i],
1991 s->dest[dst_idx] + off,
1992 i & 4 ? s->uvlinesize
1995 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
1996 first_block, s->dest[dst_idx] + off,
1997 (i & 4) ? s->uvlinesize : s->linesize,
1998 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
1999 if (!v->ttmbf && ttmb < 8)
2006 /** Decode one B-frame MB (in interlaced field B picture)
2008 static void vc1_decode_b_mb_intfi(VC1Context *v)
2010 MpegEncContext *s = &v->s;
2011 GetBitContext *gb = &s->gb;
2013 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2014 int cbp = 0; /* cbp decoding stuff */
2015 int mqdiff, mquant; /* MB quantization */
2016 int ttmb = v->ttfrm; /* MB Transform type */
2017 int mb_has_coeffs = 0; /* last_flag */
2018 int val; /* temp value */
2019 int first_block = 1;
2022 int dmv_x[2], dmv_y[2], pred_flag[2];
2023 int bmvtype = BMV_TYPE_BACKWARD;
2026 mquant = v->pq; /* Lossy initialization */
2029 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
2030 if (idx_mbmode <= 1) { // intra MB
2031 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2033 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2034 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2035 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2037 s->current_picture.qscale_table[mb_pos] = mquant;
2038 /* Set DC scale - y and c use the same (not sure if necessary here) */
2039 s->y_dc_scale = s->y_dc_scale_table[mquant];
2040 s->c_dc_scale = s->c_dc_scale_table[mquant];
2041 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2042 mb_has_coeffs = idx_mbmode & 1;
2044 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
2046 for (i = 0; i < 6; i++) {
2047 v->a_avail = v->c_avail = 0;
2048 v->mb_type[0][s->block_index[i]] = 1;
2049 s->dc_val[0][s->block_index[i]] = 0;
2051 val = ((cbp >> (5 - i)) & 1);
2052 if (i == 2 || i == 3 || !s->first_slice_line)
2053 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2054 if (i == 1 || i == 3 || s->mb_x)
2055 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2057 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2058 (i & 4) ? v->codingset2 : v->codingset);
2059 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
2061 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2063 for (j = 0; j < 64; j++)
2064 s->block[i][j] <<= 1;
2065 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2066 s->idsp.put_signed_pixels_clamped(s->block[i],
2067 s->dest[dst_idx] + off,
2068 (i & 4) ? s->uvlinesize
2070 // TODO: yet to perform loop filter
2073 s->mb_intra = v->is_intra[s->mb_x] = 0;
2074 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2075 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
2077 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2079 fwd = v->forward_mb_plane[mb_pos];
2080 if (idx_mbmode <= 5) { // 1-MV
2082 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2083 pred_flag[0] = pred_flag[1] = 0;
2085 bmvtype = BMV_TYPE_FORWARD;
2087 bmvtype = decode012(gb);
2090 bmvtype = BMV_TYPE_BACKWARD;
2093 bmvtype = BMV_TYPE_DIRECT;
2096 bmvtype = BMV_TYPE_INTERPOLATED;
2097 interpmvp = get_bits1(gb);
2100 v->bmvtype = bmvtype;
2101 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2102 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2105 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2107 if (bmvtype == BMV_TYPE_DIRECT) {
2108 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2109 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2110 if (!s->next_picture_ptr->field_picture) {
2111 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2115 ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2116 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2117 mb_has_coeffs = !(idx_mbmode & 2);
2120 bmvtype = BMV_TYPE_FORWARD;
2121 v->bmvtype = bmvtype;
2122 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2123 for (i = 0; i < 4; i++) {
2124 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2125 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2126 if (v->fourmvbp & (8 >> i)) {
2127 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2128 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2129 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2131 ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2132 ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2134 ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2135 mb_has_coeffs = idx_mbmode & 1;
2138 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2142 s->current_picture.qscale_table[mb_pos] = mquant;
2143 if (!v->ttmbf && cbp) {
2144 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2147 for (i = 0; i < 6; i++) {
2148 s->dc_val[0][s->block_index[i]] = 0;
2150 val = ((cbp >> (5 - i)) & 1);
2151 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2153 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2154 first_block, s->dest[dst_idx] + off,
2155 (i & 4) ? s->uvlinesize : s->linesize,
2156 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
2157 if (!v->ttmbf && ttmb < 8)
2165 /** Decode one B-frame MB (in interlaced frame B picture)
2167 static int vc1_decode_b_mb_intfr(VC1Context *v)
2169 MpegEncContext *s = &v->s;
2170 GetBitContext *gb = &s->gb;
2172 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2173 int cbp = 0; /* cbp decoding stuff */
2174 int mqdiff, mquant; /* MB quantization */
2175 int ttmb = v->ttfrm; /* MB Transform type */
2176 int mvsw = 0; /* motion vector switch */
2177 int mb_has_coeffs = 1; /* last_flag */
2178 int dmv_x, dmv_y; /* Differential MV components */
2179 int val; /* temp value */
2180 int first_block = 1;
2182 int skipped, direct, twomv = 0;
2183 int block_cbp = 0, pat, block_tt = 0;
2184 int idx_mbmode = 0, mvbp;
2185 int stride_y, fieldtx;
2186 int bmvtype = BMV_TYPE_BACKWARD;
2189 mquant = v->pq; /* Lossy initialization */
2192 skipped = get_bits1(gb);
2194 skipped = v->s.mbskip_table[mb_pos];
2197 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2198 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2200 v->blk_mv_type[s->block_index[0]] = 1;
2201 v->blk_mv_type[s->block_index[1]] = 1;
2202 v->blk_mv_type[s->block_index[2]] = 1;
2203 v->blk_mv_type[s->block_index[3]] = 1;
2205 v->blk_mv_type[s->block_index[0]] = 0;
2206 v->blk_mv_type[s->block_index[1]] = 0;
2207 v->blk_mv_type[s->block_index[2]] = 0;
2208 v->blk_mv_type[s->block_index[3]] = 0;
2213 direct = get_bits1(gb);
2215 direct = v->direct_mb_plane[mb_pos];
2218 if (s->next_picture_ptr->field_picture)
2219 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2220 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);
2221 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);
2222 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);
2223 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);
2226 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);
2227 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);
2228 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);
2229 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);
2231 for (i = 1; i < 4; i += 2) {
2232 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2233 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2234 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2235 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2238 for (i = 1; i < 4; i++) {
2239 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2240 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2241 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2242 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2247 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2248 for (i = 0; i < 4; i++) {
2249 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
2250 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
2251 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
2252 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
2254 v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2256 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2257 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2258 mb_has_coeffs = get_bits1(gb);
2260 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2261 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2263 s->current_picture.qscale_table[mb_pos] = mquant;
2264 /* Set DC scale - y and c use the same (not sure if necessary here) */
2265 s->y_dc_scale = s->y_dc_scale_table[mquant];
2266 s->c_dc_scale = s->c_dc_scale_table[mquant];
2268 for (i = 0; i < 6; i++) {
2269 v->a_avail = v->c_avail = 0;
2270 v->mb_type[0][s->block_index[i]] = 1;
2271 s->dc_val[0][s->block_index[i]] = 0;
2273 val = ((cbp >> (5 - i)) & 1);
2274 if (i == 2 || i == 3 || !s->first_slice_line)
2275 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2276 if (i == 1 || i == 3 || s->mb_x)
2277 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2279 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
2280 (i & 4) ? v->codingset2 : v->codingset);
2281 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
2283 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
2285 stride_y = s->linesize << fieldtx;
2286 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2288 stride_y = s->uvlinesize;
2291 s->idsp.put_signed_pixels_clamped(s->block[i],
2292 s->dest[dst_idx] + off,
2296 s->mb_intra = v->is_intra[s->mb_x] = 0;
2298 if (skipped || !s->mb_intra) {
2299 bmvtype = decode012(gb);
2302 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2305 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2308 bmvtype = BMV_TYPE_INTERPOLATED;
2312 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2313 mvsw = get_bits1(gb);
2316 if (!skipped) { // inter MB
2317 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2319 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2321 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2322 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
2323 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2324 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
2328 for (i = 0; i < 6; i++)
2329 v->mb_type[0][s->block_index[i]] = 0;
2330 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2331 /* for all motion vector read MVDATA and motion compensate each block */
2335 for (i = 0; i < 4; i++) {
2336 ff_vc1_mc_4mv_luma(v, i, 0, 0);
2337 ff_vc1_mc_4mv_luma(v, i, 1, 1);
2339 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2340 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2342 ff_vc1_mc_1mv(v, 0);
2343 ff_vc1_interp_mc(v);
2345 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2347 for (i = 0; i < 4; i++) {
2350 val = ((mvbp >> (3 - i)) & 1);
2352 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2354 ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2355 ff_vc1_mc_4mv_luma(v, j, dir, dir);
2356 ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2359 ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2360 ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2361 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2365 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2367 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2368 ff_vc1_mc_1mv(v, 0);
2372 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2374 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2375 ff_vc1_interp_mc(v);
2377 dir = bmvtype == BMV_TYPE_BACKWARD;
2384 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2385 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
2389 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2390 ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
2393 for (i = 0; i < 2; i++) {
2394 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];
2395 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];
2396 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];
2397 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];
2400 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2401 ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
2404 ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2405 ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2406 ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2407 ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2408 ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2410 dir = bmvtype == BMV_TYPE_BACKWARD;
2412 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2415 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2417 ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2418 v->blk_mv_type[s->block_index[0]] = 1;
2419 v->blk_mv_type[s->block_index[1]] = 1;
2420 v->blk_mv_type[s->block_index[2]] = 1;
2421 v->blk_mv_type[s->block_index[3]] = 1;
2422 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2423 for (i = 0; i < 2; i++) {
2424 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];
2425 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];
2427 ff_vc1_mc_1mv(v, dir);
2431 GET_MQUANT(); // p. 227
2432 s->current_picture.qscale_table[mb_pos] = mquant;
2433 if (!v->ttmbf && cbp)
2434 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2435 for (i = 0; i < 6; i++) {
2436 s->dc_val[0][s->block_index[i]] = 0;
2438 val = ((cbp >> (5 - i)) & 1);
2440 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2442 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2444 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
2445 first_block, s->dest[dst_idx] + off,
2446 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2447 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
2448 block_cbp |= pat << (i << 2);
2449 if (!v->ttmbf && ttmb < 8)
2457 for (i = 0; i < 6; i++) {
2458 v->mb_type[0][s->block_index[i]] = 0;
2459 s->dc_val[0][s->block_index[i]] = 0;
2461 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2462 s->current_picture.qscale_table[mb_pos] = 0;
2463 v->blk_mv_type[s->block_index[0]] = 0;
2464 v->blk_mv_type[s->block_index[1]] = 0;
2465 v->blk_mv_type[s->block_index[2]] = 0;
2466 v->blk_mv_type[s->block_index[3]] = 0;
2469 if (bmvtype == BMV_TYPE_INTERPOLATED) {
2470 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
2471 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
2473 dir = bmvtype == BMV_TYPE_BACKWARD;
2474 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
2479 for (i = 0; i < 2; i++) {
2480 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];
2481 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];
2482 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];
2483 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];
2486 v->blk_mv_type[s->block_index[0]] = 1;
2487 v->blk_mv_type[s->block_index[1]] = 1;
2488 v->blk_mv_type[s->block_index[2]] = 1;
2489 v->blk_mv_type[s->block_index[3]] = 1;
2490 ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
2491 for (i = 0; i < 2; i++) {
2492 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];
2493 s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
2499 ff_vc1_mc_1mv(v, dir);
2500 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2501 ff_vc1_interp_mc(v);
2505 if (s->mb_x == s->mb_width - 1)
2506 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2507 v->cbp[s->mb_x] = block_cbp;
2508 v->ttblk[s->mb_x] = block_tt;
2512 /** Decode blocks of I-frame
2514 static void vc1_decode_i_blocks(VC1Context *v)
2517 MpegEncContext *s = &v->s;
2522 /* select codingmode used for VLC tables selection */
2523 switch (v->y_ac_table_index) {
2525 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2528 v->codingset = CS_HIGH_MOT_INTRA;
2531 v->codingset = CS_MID_RATE_INTRA;
2535 switch (v->c_ac_table_index) {
2537 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2540 v->codingset2 = CS_HIGH_MOT_INTER;
2543 v->codingset2 = CS_MID_RATE_INTER;
2547 /* Set DC scale - y and c use the same */
2548 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2549 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2552 s->mb_x = s->mb_y = 0;
2554 s->first_slice_line = 1;
2555 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
2557 init_block_index(v);
2558 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2560 ff_update_block_index(s);
2561 dst[0] = s->dest[0];
2562 dst[1] = dst[0] + 8;
2563 dst[2] = s->dest[0] + s->linesize * 8;
2564 dst[3] = dst[2] + 8;
2565 dst[4] = s->dest[1];
2566 dst[5] = s->dest[2];
2567 s->bdsp.clear_blocks(s->block[0]);
2568 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2569 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2570 s->current_picture.qscale_table[mb_pos] = v->pq;
2571 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2572 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2574 // do actual MB decoding and displaying
2575 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2576 v->s.ac_pred = get_bits1(&v->s.gb);
2578 for (k = 0; k < 6; k++) {
2579 val = ((cbp >> (5 - k)) & 1);
2582 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2586 cbp |= val << (5 - k);
2588 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
2590 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2592 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
2593 if (v->pq >= 9 && v->overlap) {
2595 for (j = 0; j < 64; j++)
2596 s->block[k][j] <<= 1;
2597 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
2598 k & 4 ? s->uvlinesize
2602 for (j = 0; j < 64; j++)
2603 s->block[k][j] = (s->block[k][j] - 64) << 1;
2604 s->idsp.put_pixels_clamped(s->block[k], dst[k],
2605 k & 4 ? s->uvlinesize
2610 if (v->pq >= 9 && v->overlap) {
2612 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
2613 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2614 if (!(s->flags & CODEC_FLAG_GRAY)) {
2615 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2616 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2619 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2620 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2621 if (!s->first_slice_line) {
2622 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
2623 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2624 if (!(s->flags & CODEC_FLAG_GRAY)) {
2625 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2626 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2629 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2630 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2632 if (v->s.loop_filter)
2633 ff_vc1_loop_filter_iblk(v, v->pq);
2635 if (get_bits_count(&s->gb) > v->bits) {
2636 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2637 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2638 get_bits_count(&s->gb), v->bits);
2642 if (!v->s.loop_filter)
2643 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2645 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2647 s->first_slice_line = 0;
2649 if (v->s.loop_filter)
2650 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2652 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2653 * profile, these only differ are when decoding MSS2 rectangles. */
2654 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2657 /** Decode blocks of I-frame for advanced profile
2659 static void vc1_decode_i_blocks_adv(VC1Context *v)
2662 MpegEncContext *s = &v->s;
2668 GetBitContext *gb = &s->gb;
2670 /* select codingmode used for VLC tables selection */
2671 switch (v->y_ac_table_index) {
2673 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2676 v->codingset = CS_HIGH_MOT_INTRA;
2679 v->codingset = CS_MID_RATE_INTRA;
2683 switch (v->c_ac_table_index) {
2685 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2688 v->codingset2 = CS_HIGH_MOT_INTER;
2691 v->codingset2 = CS_MID_RATE_INTER;
2696 s->mb_x = s->mb_y = 0;
2698 s->first_slice_line = 1;
2699 s->mb_y = s->start_mb_y;
2700 if (s->start_mb_y) {
2702 init_block_index(v);
2703 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
2704 (1 + s->b8_stride) * sizeof(*s->coded_block));
2706 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2708 init_block_index(v);
2709 for (;s->mb_x < s->mb_width; s->mb_x++) {
2710 int16_t (*block)[64] = v->block[v->cur_blk_idx];
2711 ff_update_block_index(s);
2712 s->bdsp.clear_blocks(block[0]);
2713 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2714 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2715 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
2716 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
2718 // do actual MB decoding and displaying
2719 if (v->fieldtx_is_raw)
2720 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
2721 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2722 if ( v->acpred_is_raw)
2723 v->s.ac_pred = get_bits1(&v->s.gb);
2725 v->s.ac_pred = v->acpred_plane[mb_pos];
2727 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2728 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
2732 s->current_picture.qscale_table[mb_pos] = mquant;
2733 /* Set DC scale - y and c use the same */
2734 s->y_dc_scale = s->y_dc_scale_table[mquant];
2735 s->c_dc_scale = s->c_dc_scale_table[mquant];
2737 for (k = 0; k < 6; k++) {
2738 val = ((cbp >> (5 - k)) & 1);
2741 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2745 cbp |= val << (5 - k);
2747 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2748 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2750 vc1_decode_i_block_adv(v, block[k], k, val,
2751 (k < 4) ? v->codingset : v->codingset2, mquant);
2753 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
2755 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
2758 ff_vc1_smooth_overlap_filter_iblk(v);
2759 vc1_put_signed_blocks_clamped(v);
2760 if (v->s.loop_filter)
2761 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2763 if (get_bits_count(&s->gb) > v->bits) {
2764 // TODO: may need modification to handle slice coding
2765 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2766 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2767 get_bits_count(&s->gb), v->bits);
2771 if (!v->s.loop_filter)
2772 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2774 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
2775 s->first_slice_line = 0;
2778 /* raw bottom MB row */
2780 init_block_index(v);
2782 for (;s->mb_x < s->mb_width; s->mb_x++) {
2783 ff_update_block_index(s);
2784 vc1_put_signed_blocks_clamped(v);
2785 if (v->s.loop_filter)
2786 ff_vc1_loop_filter_iblk_delayed(v, v->pq);
2788 if (v->s.loop_filter)
2789 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
2790 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2791 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2794 static void vc1_decode_p_blocks(VC1Context *v)
2796 MpegEncContext *s = &v->s;
2797 int apply_loop_filter;
2799 /* select codingmode used for VLC tables selection */
2800 switch (v->c_ac_table_index) {
2802 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2805 v->codingset = CS_HIGH_MOT_INTRA;
2808 v->codingset = CS_MID_RATE_INTRA;
2812 switch (v->c_ac_table_index) {
2814 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2817 v->codingset2 = CS_HIGH_MOT_INTER;
2820 v->codingset2 = CS_MID_RATE_INTER;
2824 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
2825 v->fcm == PROGRESSIVE;
2826 s->first_slice_line = 1;
2827 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2828 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2830 init_block_index(v);
2831 for (; s->mb_x < s->mb_width; s->mb_x++) {
2832 ff_update_block_index(s);
2834 if (v->fcm == ILACE_FIELD)
2835 vc1_decode_p_mb_intfi(v);
2836 else if (v->fcm == ILACE_FRAME)
2837 vc1_decode_p_mb_intfr(v);
2838 else vc1_decode_p_mb(v);
2839 if (s->mb_y != s->start_mb_y && apply_loop_filter)
2840 ff_vc1_apply_p_loop_filter(v);
2841 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2842 // TODO: may need modification to handle slice coding
2843 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2844 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2845 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2849 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
2850 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
2851 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
2852 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
2853 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2854 s->first_slice_line = 0;
2856 if (apply_loop_filter) {
2858 init_block_index(v);
2859 for (; s->mb_x < s->mb_width; s->mb_x++) {
2860 ff_update_block_index(s);
2861 ff_vc1_apply_p_loop_filter(v);
2864 if (s->end_mb_y >= s->start_mb_y)
2865 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2866 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2867 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2870 static void vc1_decode_b_blocks(VC1Context *v)
2872 MpegEncContext *s = &v->s;
2874 /* select codingmode used for VLC tables selection */
2875 switch (v->c_ac_table_index) {
2877 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2880 v->codingset = CS_HIGH_MOT_INTRA;
2883 v->codingset = CS_MID_RATE_INTRA;
2887 switch (v->c_ac_table_index) {
2889 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2892 v->codingset2 = CS_HIGH_MOT_INTER;
2895 v->codingset2 = CS_MID_RATE_INTER;
2899 s->first_slice_line = 1;
2900 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2902 init_block_index(v);
2903 for (; s->mb_x < s->mb_width; s->mb_x++) {
2904 ff_update_block_index(s);
2906 if (v->fcm == ILACE_FIELD)
2907 vc1_decode_b_mb_intfi(v);
2908 else if (v->fcm == ILACE_FRAME)
2909 vc1_decode_b_mb_intfr(v);
2912 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2913 // TODO: may need modification to handle slice coding
2914 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2915 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2916 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
2919 if (v->s.loop_filter)
2920 ff_vc1_loop_filter_iblk(v, v->pq);
2922 if (!v->s.loop_filter)
2923 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2925 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
2926 s->first_slice_line = 0;
2928 if (v->s.loop_filter)
2929 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
2930 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2931 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2934 static void vc1_decode_skip_blocks(VC1Context *v)
2936 MpegEncContext *s = &v->s;
2938 if (!v->s.last_picture.f->data[0])
2941 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2942 s->first_slice_line = 1;
2943 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2945 init_block_index(v);
2946 ff_update_block_index(s);
2947 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2948 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2949 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2950 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
2951 s->first_slice_line = 0;
2953 s->pict_type = AV_PICTURE_TYPE_P;
2956 void ff_vc1_decode_blocks(VC1Context *v)
2959 v->s.esc3_level_length = 0;
2961 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
2964 v->left_blk_idx = -1;
2965 v->topleft_blk_idx = 1;
2967 switch (v->s.pict_type) {
2968 case AV_PICTURE_TYPE_I:
2969 if (v->profile == PROFILE_ADVANCED)
2970 vc1_decode_i_blocks_adv(v);
2972 vc1_decode_i_blocks(v);
2974 case AV_PICTURE_TYPE_P:
2975 if (v->p_frame_skipped)
2976 vc1_decode_skip_blocks(v);
2978 vc1_decode_p_blocks(v);
2980 case AV_PICTURE_TYPE_B:
2982 if (v->profile == PROFILE_ADVANCED)
2983 vc1_decode_i_blocks_adv(v);
2985 vc1_decode_i_blocks(v);
2987 vc1_decode_b_blocks(v);