2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * VC-1 and WMV3 decoder
30 #include "mpegvideo.h"
32 #include "vc1acdata.h"
37 extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
38 extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
39 extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
40 #define MB_INTRA_VLC_BITS 9
41 extern VLC ff_msmp4_mb_i_vlc;
42 extern const uint16_t ff_msmp4_mb_i_table[64][2];
45 static const uint16_t table_mb_intra[64][2];
48 /** Available Profiles */
53 PROFILE_COMPLEX, ///< TODO: WMV9 specific
58 /** Sequence quantizer mode */
61 QUANT_FRAME_IMPLICIT, ///< Implicitly specified at frame level
62 QUANT_FRAME_EXPLICIT, ///< Explicitly specified at frame level
63 QUANT_NON_UNIFORM, ///< Non-uniform quant used for all frames
64 QUANT_UNIFORM ///< Uniform quant used for all frames
68 /** Where quant can be changed */
72 DQPROFILE_DOUBLE_EDGES,
73 DQPROFILE_SINGLE_EDGE,
78 /** @name Where quant can be changed
89 /** Which pair of edges is quantized with ALTPQUANT */
92 DQDOUBLE_BEDGE_TOPLEFT,
93 DQDOUBLE_BEDGE_TOPRIGHT,
94 DQDOUBLE_BEDGE_BOTTOMRIGHT,
95 DQDOUBLE_BEDGE_BOTTOMLEFT
99 /** MV modes for P frames */
102 MV_PMODE_1MV_HPEL_BILIN,
106 MV_PMODE_INTENSITY_COMP
110 /** @name MV types for B frames */
115 BMV_TYPE_INTERPOLATED = 3 //XXX: ??
119 /** @name Block types for P/B frames */
121 enum TransformTypes {
125 TT_8X4, //Both halves
128 TT_4X8, //Both halves
133 /** Table for conversion between TTBLK and TTMB */
134 static const int ttblk_to_tt[3][8] = {
135 { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
136 { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
137 { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
140 static const int ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 };
142 /** MV P mode - the 5th element is only used for mode 1 */
143 static const uint8_t mv_pmode_table[2][5] = {
144 { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
145 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
147 static const uint8_t mv_pmode_table2[2][4] = {
148 { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV },
149 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN }
152 /** One more frame type */
155 static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
156 fps_dr[2] = { 1000, 1001 };
157 static const uint8_t pquant_table[3][32] = {
158 { /* Implicit quantizer */
159 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
160 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
162 { /* Explicit quantizer, pquantizer uniform */
163 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
164 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
166 { /* Explicit quantizer, pquantizer non-uniform */
167 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
168 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
172 /** @name VC-1 VLC tables and defines
173 * @todo TODO move this into the context
176 #define VC1_BFRACTION_VLC_BITS 7
177 static VLC vc1_bfraction_vlc;
178 #define VC1_IMODE_VLC_BITS 4
179 static VLC vc1_imode_vlc;
180 #define VC1_NORM2_VLC_BITS 3
181 static VLC vc1_norm2_vlc;
182 #define VC1_NORM6_VLC_BITS 9
183 static VLC vc1_norm6_vlc;
184 /* Could be optimized, one table only needs 8 bits */
185 #define VC1_TTMB_VLC_BITS 9 //12
186 static VLC vc1_ttmb_vlc[3];
187 #define VC1_MV_DIFF_VLC_BITS 9 //15
188 static VLC vc1_mv_diff_vlc[4];
189 #define VC1_CBPCY_P_VLC_BITS 9 //14
190 static VLC vc1_cbpcy_p_vlc[4];
191 #define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
192 static VLC vc1_4mv_block_pattern_vlc[4];
193 #define VC1_TTBLK_VLC_BITS 5
194 static VLC vc1_ttblk_vlc[3];
195 #define VC1_SUBBLKPAT_VLC_BITS 6
196 static VLC vc1_subblkpat_vlc[3];
198 static VLC vc1_ac_coeff_table[8];
202 CS_HIGH_MOT_INTRA = 0,
213 * @fixme Change size wherever another size is more efficient
214 * Many members are only used for Advanced Profile
216 typedef struct VC1Context{
221 /** Simple/Main Profile sequence header */
223 int res_sm; ///< reserved, 2b
224 int res_x8; ///< reserved
225 int multires; ///< frame-level RESPIC syntax element present
226 int res_fasttx; ///< reserved, always 1
227 int res_transtab; ///< reserved, always 0
228 int rangered; ///< RANGEREDFRM (range reduction) syntax element present
230 int res_rtm_flag; ///< reserved, set to 1
231 int reserved; ///< reserved
234 /** Advanced Profile */
236 int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
237 int chromaformat; ///< 2bits, 2=4:2:0, only defined
238 int postprocflag; ///< Per-frame processing suggestion flag present
239 int broadcast; ///< TFF/RFF present
240 int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
241 int tfcntrflag; ///< TFCNTR present
242 int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
243 int extended_dmv; ///< Additional extended dmv range at P/B frame-level
244 int color_prim; ///< 8bits, chroma coordinates of the color primaries
245 int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
246 int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
247 int hrd_param_flag; ///< Presence of Hypothetical Reference
248 ///< Decoder parameters
251 /** Sequence header data for all Profiles
252 * TODO: choose between ints, uint8_ts and monobit flags
255 int profile; ///< 2bits, Profile
256 int frmrtq_postproc; ///< 3bits,
257 int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
258 int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
259 int extended_mv; ///< Ext MV in P/B (not in Simple)
260 int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
261 int vstransform; ///< variable-size [48]x[48] transform type + info
262 int overlap; ///< overlapped transforms in use
263 int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
264 int finterpflag; ///< INTERPFRM present
267 /** Frame decoding info for all profiles */
269 uint8_t mv_mode; ///< MV coding monde
270 uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
271 int k_x; ///< Number of bits for MVs (depends on MV range)
272 int k_y; ///< Number of bits for MVs (depends on MV range)
273 int range_x, range_y; ///< MV range
274 uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
275 /** pquant parameters */
282 /** AC coding set indexes
283 * @see 8.1.1.10, p(1)10
286 int c_ac_table_index; ///< Chroma index from ACFRM element
287 int y_ac_table_index; ///< Luma index from AC2FRM element
289 int ttfrm; ///< Transform type info present at frame level
290 uint8_t ttmbf; ///< Transform type flag
291 uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
292 int codingset; ///< index of current table set from 11.8 to use for luma block decoding
293 int codingset2; ///< index of current table set from 11.8 to use for chroma block decoding
294 int pqindex; ///< raw pqindex used in coding set selection
295 int a_avail, c_avail;
296 uint8_t *mb_type_base, *mb_type[3];
299 /** Luma compensation parameters */
304 int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
305 uint8_t halfpq; ///< Uniform quant over image and qp+.5
306 uint8_t respic; ///< Frame-level flag for resized images
307 int buffer_fullness; ///< HRD info
309 * -# 0 -> [-64n 63.f] x [-32, 31.f]
310 * -# 1 -> [-128, 127.f] x [-64, 63.f]
311 * -# 2 -> [-512, 511.f] x [-128, 127.f]
312 * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
315 uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
316 VLC *cbpcy_vlc; ///< CBPCY VLC table
317 int tt_index; ///< Index for Transform Type tables
318 uint8_t* mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
319 uint8_t* direct_mb_plane; ///< bitplane for "direct" MBs
320 int mv_type_is_raw; ///< mv type mb plane is not coded
321 int dmb_is_raw; ///< direct mb plane is raw
322 int skip_is_raw; ///< skip mb plane is not coded
323 uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
324 int rnd; ///< rounding control
326 /** Frame decoding info for S/M profiles only */
328 uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
332 /** Frame decoding info for Advanced profile */
334 uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
335 uint8_t numpanscanwin;
337 uint8_t rptfrm, tff, rff;
340 uint16_t bottomrightx;
341 uint16_t bottomrighty;
344 int hrd_num_leaky_buckets;
345 uint8_t bit_rate_exponent;
346 uint8_t buffer_size_exponent;
347 // BitPlane ac_pred_plane; ///< AC prediction flags bitplane
348 // BitPlane over_flags_plane; ///< Overflags bitplane
350 uint16_t *hrd_rate, *hrd_buffer;
351 uint8_t *hrd_fullness;
352 uint8_t range_mapy_flag;
353 uint8_t range_mapuv_flag;
360 * Get unary code of limited length
361 * @fixme FIXME Slow and ugly
362 * @param gb GetBitContext
363 * @param[in] stop The bitstop value (unary code of 1's or 0's)
364 * @param[in] len Maximum length
365 * @return Unary length/index
367 static int get_prefix(GetBitContext *gb, int stop, int len)
372 for(i = 0; i < len && get_bits1(gb) != stop; i++);
374 /* int i = 0, tmp = !stop;
376 while (i != len && tmp != stop)
378 tmp = get_bits(gb, 1);
381 if (i == len && tmp != stop) return len+1;
388 UPDATE_CACHE(re, gb);
389 buf=GET_CACHE(re, gb); //Still not sure
390 if (stop) buf = ~buf;
392 log= av_log2(-buf); //FIXME: -?
394 LAST_SKIP_BITS(re, gb, log+1);
395 CLOSE_READER(re, gb);
399 LAST_SKIP_BITS(re, gb, limit);
400 CLOSE_READER(re, gb);
405 static inline int decode210(GetBitContext *gb){
411 return 2 - get_bits1(gb);
415 * Init VC-1 specific tables and VC1Context members
416 * @param v The VC1Context to initialize
419 static int vc1_init_common(VC1Context *v)
424 v->hrd_rate = v->hrd_buffer = NULL;
430 init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
431 vc1_bfraction_bits, 1, 1,
432 vc1_bfraction_codes, 1, 1, 1);
433 init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
434 vc1_norm2_bits, 1, 1,
435 vc1_norm2_codes, 1, 1, 1);
436 init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
437 vc1_norm6_bits, 1, 1,
438 vc1_norm6_codes, 2, 2, 1);
439 init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
440 vc1_imode_bits, 1, 1,
441 vc1_imode_codes, 1, 1, 1);
444 init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
445 vc1_ttmb_bits[i], 1, 1,
446 vc1_ttmb_codes[i], 2, 2, 1);
447 init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
448 vc1_ttblk_bits[i], 1, 1,
449 vc1_ttblk_codes[i], 1, 1, 1);
450 init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
451 vc1_subblkpat_bits[i], 1, 1,
452 vc1_subblkpat_codes[i], 1, 1, 1);
456 init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
457 vc1_4mv_block_pattern_bits[i], 1, 1,
458 vc1_4mv_block_pattern_codes[i], 1, 1, 1);
459 init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
460 vc1_cbpcy_p_bits[i], 1, 1,
461 vc1_cbpcy_p_codes[i], 2, 2, 1);
462 init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
463 vc1_mv_diff_bits[i], 1, 1,
464 vc1_mv_diff_codes[i], 2, 2, 1);
467 init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
468 &vc1_ac_tables[i][0][1], 8, 4,
469 &vc1_ac_tables[i][0][0], 8, 4, 1);
470 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
471 &ff_msmp4_mb_i_table[0][1], 4, 2,
472 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
477 v->mvrange = 0; /* 7.1.1.18, p80 */
482 /***********************************************************************/
484 * @defgroup bitplane VC9 Bitplane decoding
489 /** @addtogroup bitplane
502 /** @} */ //imode defines
504 /** Decode rows by checking if they are skipped
505 * @param plane Buffer to store decoded bits
506 * @param[in] width Width of this buffer
507 * @param[in] height Height of this buffer
508 * @param[in] stride of this buffer
510 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
513 for (y=0; y<height; y++){
514 if (!get_bits(gb, 1)) //rowskip
515 memset(plane, 0, width);
517 for (x=0; x<width; x++)
518 plane[x] = get_bits(gb, 1);
523 /** Decode columns by checking if they are skipped
524 * @param plane Buffer to store decoded bits
525 * @param[in] width Width of this buffer
526 * @param[in] height Height of this buffer
527 * @param[in] stride of this buffer
528 * @fixme FIXME: Optimize
530 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
533 for (x=0; x<width; x++){
534 if (!get_bits(gb, 1)) //colskip
535 for (y=0; y<height; y++)
538 for (y=0; y<height; y++)
539 plane[y*stride] = get_bits(gb, 1);
544 /** Decode a bitplane's bits
545 * @param bp Bitplane where to store the decode bits
546 * @param v VC-1 context for bit reading and logging
548 * @fixme FIXME: Optimize
549 * @todo TODO: Decide if a struct is needed
551 static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
553 GetBitContext *gb = &v->s.gb;
555 int imode, x, y, code, offset;
556 uint8_t invert, *planep = data;
557 int width, height, stride;
559 width = v->s.mb_width;
560 height = v->s.mb_height;
561 stride = v->s.mb_stride;
562 invert = get_bits(gb, 1);
563 imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
569 //Data is actually read in the MB layer (same for all tests == "raw")
570 *raw_flag = 1; //invert ignored
574 if ((height * width) & 1)
576 *planep++ = get_bits(gb, 1);
580 // decode bitplane as one long line
581 for (y = offset; y < height * width; y += 2) {
582 code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
583 *planep++ = code & 1;
585 if(offset == width) {
587 planep += stride - width;
589 *planep++ = code >> 1;
591 if(offset == width) {
593 planep += stride - width;
599 if(!(height % 3) && (width % 3)) { // use 2x3 decoding
600 for(y = 0; y < height; y+= 3) {
601 for(x = width & 1; x < width; x += 2) {
602 code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
604 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
607 planep[x + 0] = (code >> 0) & 1;
608 planep[x + 1] = (code >> 1) & 1;
609 planep[x + 0 + stride] = (code >> 2) & 1;
610 planep[x + 1 + stride] = (code >> 3) & 1;
611 planep[x + 0 + stride * 2] = (code >> 4) & 1;
612 planep[x + 1 + stride * 2] = (code >> 5) & 1;
614 planep += stride * 3;
616 if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
618 planep += (height & 1) * stride;
619 for(y = height & 1; y < height; y += 2) {
620 for(x = width % 3; x < width; x += 3) {
621 code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
623 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
626 planep[x + 0] = (code >> 0) & 1;
627 planep[x + 1] = (code >> 1) & 1;
628 planep[x + 2] = (code >> 2) & 1;
629 planep[x + 0 + stride] = (code >> 3) & 1;
630 planep[x + 1 + stride] = (code >> 4) & 1;
631 planep[x + 2 + stride] = (code >> 5) & 1;
633 planep += stride * 2;
636 if(x) decode_colskip(data , x, height , stride, &v->s.gb);
637 if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
641 decode_rowskip(data, width, height, stride, &v->s.gb);
644 decode_colskip(data, width, height, stride, &v->s.gb);
649 /* Applying diff operator */
650 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
654 for (x=1; x<width; x++)
655 planep[x] ^= planep[x-1];
656 for (y=1; y<height; y++)
659 planep[0] ^= planep[-stride];
660 for (x=1; x<width; x++)
662 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
663 else planep[x] ^= planep[x-1];
670 for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
672 return (imode<<1) + invert;
675 /** @} */ //Bitplane group
677 /***********************************************************************/
678 /** VOP Dquant decoding
679 * @param v VC-1 Context
681 static int vop_dquant_decoding(VC1Context *v)
683 GetBitContext *gb = &v->s.gb;
689 pqdiff = get_bits(gb, 3);
690 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
691 else v->altpq = v->pq + pqdiff + 1;
695 v->dquantfrm = get_bits(gb, 1);
698 v->dqprofile = get_bits(gb, 2);
699 switch (v->dqprofile)
701 case DQPROFILE_SINGLE_EDGE:
702 case DQPROFILE_DOUBLE_EDGES:
703 v->dqsbedge = get_bits(gb, 2);
705 case DQPROFILE_ALL_MBS:
706 v->dqbilevel = get_bits(gb, 1);
707 default: break; //Forbidden ?
709 if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
711 pqdiff = get_bits(gb, 3);
712 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
713 else v->altpq = v->pq + pqdiff + 1;
720 /** Put block onto picture
721 * @todo move to DSPContext
723 static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
727 DSPContext *dsp = &v->s.dsp;
731 for(k = 0; k < 6; k++)
732 for(j = 0; j < 8; j++)
733 for(i = 0; i < 8; i++)
734 block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
737 ys = v->s.current_picture.linesize[0];
738 us = v->s.current_picture.linesize[1];
739 vs = v->s.current_picture.linesize[2];
742 dsp->put_pixels_clamped(block[0], Y, ys);
743 dsp->put_pixels_clamped(block[1], Y + 8, ys);
745 dsp->put_pixels_clamped(block[2], Y, ys);
746 dsp->put_pixels_clamped(block[3], Y + 8, ys);
748 if(!(v->s.flags & CODEC_FLAG_GRAY)) {
749 dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
750 dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
754 /** Do motion compensation over 1 macroblock
755 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
757 static void vc1_mc_1mv(VC1Context *v, int dir)
759 MpegEncContext *s = &v->s;
760 DSPContext *dsp = &v->s.dsp;
761 uint8_t *srcY, *srcU, *srcV;
762 int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
764 if(!v->s.last_picture.data[0])return;
768 uvmx = (mx + ((mx & 3) == 3)) >> 1;
769 uvmy = (my + ((my & 3) == 3)) >> 1;
771 srcY = s->last_picture.data[0];
772 srcU = s->last_picture.data[1];
773 srcV = s->last_picture.data[2];
775 srcY = s->next_picture.data[0];
776 srcU = s->next_picture.data[1];
777 srcV = s->next_picture.data[2];
780 src_x = s->mb_x * 16 + (mx >> 2);
781 src_y = s->mb_y * 16 + (my >> 2);
782 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
783 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
785 src_x = clip( src_x, -16, s->mb_width * 16);
786 src_y = clip( src_y, -16, s->mb_height * 16);
787 uvsrc_x = clip(uvsrc_x, -8, s->mb_width * 8);
788 uvsrc_y = clip(uvsrc_y, -8, s->mb_height * 8);
790 srcY += src_y * s->linesize + src_x;
791 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
792 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
794 /* for grayscale we should not try to read from unknown area */
795 if(s->flags & CODEC_FLAG_GRAY) {
796 srcU = s->edge_emu_buffer + 18 * s->linesize;
797 srcV = s->edge_emu_buffer + 18 * s->linesize;
800 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
801 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
802 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
803 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
805 srcY -= s->mspel * (1 + s->linesize);
806 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
807 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
808 srcY = s->edge_emu_buffer;
809 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
810 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
811 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
812 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
815 /* if we deal with range reduction we need to scale source blocks */
821 for(j = 0; j < 17 + s->mspel*2; j++) {
822 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
825 src = srcU; src2 = srcV;
826 for(j = 0; j < 9; j++) {
827 for(i = 0; i < 9; i++) {
828 src[i] = ((src[i] - 128) >> 1) + 128;
829 src2[i] = ((src2[i] - 128) >> 1) + 128;
831 src += s->uvlinesize;
832 src2 += s->uvlinesize;
835 /* if we deal with intensity compensation we need to scale source blocks */
836 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
841 for(j = 0; j < 17 + s->mspel*2; j++) {
842 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
845 src = srcU; src2 = srcV;
846 for(j = 0; j < 9; j++) {
847 for(i = 0; i < 9; i++) {
848 src[i] = v->lutuv[src[i]];
849 src2[i] = v->lutuv[src2[i]];
851 src += s->uvlinesize;
852 src2 += s->uvlinesize;
855 srcY += s->mspel * (1 + s->linesize);
859 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
860 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
864 dxy = ((my & 3) << 2) | (mx & 3);
865 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
866 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
867 srcY += s->linesize * 8;
868 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
869 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
870 } else if(!s->quarter_sample) { // hpel mc
873 dxy = ((my & 1) << 1) | (mx & 1);
876 dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
878 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
880 dxy = ((my & 3) << 2) | (mx & 3);
883 dsp->put_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
885 dsp->put_no_rnd_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
888 if(s->flags & CODEC_FLAG_GRAY) return;
889 /* Chroma MC always uses qpel blilinear */
890 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
892 dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
893 dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
895 dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
896 dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
900 /** Do motion compensation for 4-MV macroblock - luminance block
902 static void vc1_mc_4mv_luma(VC1Context *v, int n)
904 MpegEncContext *s = &v->s;
905 DSPContext *dsp = &v->s.dsp;
907 int dxy, mx, my, src_x, src_y;
910 if(!v->s.last_picture.data[0])return;
913 srcY = s->last_picture.data[0];
915 off = s->linesize * 4 * (n&2) + (n&1) * 8;
917 src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
918 src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
920 src_x = clip( src_x, -16, s->mb_width * 16);
921 src_y = clip( src_y, -16, s->mb_height * 16);
923 srcY += src_y * s->linesize + src_x;
925 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
926 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
927 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
928 srcY -= s->mspel * (1 + s->linesize);
929 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
930 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
931 srcY = s->edge_emu_buffer;
932 /* if we deal with range reduction we need to scale source blocks */
938 for(j = 0; j < 9 + s->mspel*2; j++) {
939 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
943 /* if we deal with intensity compensation we need to scale source blocks */
944 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
949 for(j = 0; j < 9 + s->mspel*2; j++) {
950 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
954 srcY += s->mspel * (1 + s->linesize);
958 dxy = ((my & 3) << 2) | (mx & 3);
959 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
960 } else if(!s->quarter_sample) { // hpel mc
963 dxy = ((my & 1) << 1) | (mx & 1);
966 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
968 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
970 dxy = ((my & 3) << 2) | (mx & 3);
973 dsp->put_qpel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize);
975 dsp->put_no_rnd_qpel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize);
979 static inline int median4(int a, int b, int c, int d)
982 if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
983 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
985 if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
986 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
991 /** Do motion compensation for 4-MV macroblock - both chroma blocks
993 static void vc1_mc_4mv_chroma(VC1Context *v)
995 MpegEncContext *s = &v->s;
996 DSPContext *dsp = &v->s.dsp;
997 uint8_t *srcU, *srcV;
998 int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
999 int i, idx, tx = 0, ty = 0;
1000 int mvx[4], mvy[4], intra[4];
1001 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
1003 if(!v->s.last_picture.data[0])return;
1004 if(s->flags & CODEC_FLAG_GRAY) return;
1006 for(i = 0; i < 4; i++) {
1007 mvx[i] = s->mv[0][i][0];
1008 mvy[i] = s->mv[0][i][1];
1009 intra[i] = v->mb_type[0][s->block_index[i]];
1012 /* calculate chroma MV vector from four luma MVs */
1013 idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
1014 if(!idx) { // all blocks are inter
1015 tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
1016 ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
1017 } else if(count[idx] == 1) { // 3 inter blocks
1020 tx = mid_pred(mvx[1], mvx[2], mvx[3]);
1021 ty = mid_pred(mvy[1], mvy[2], mvy[3]);
1024 tx = mid_pred(mvx[0], mvx[2], mvx[3]);
1025 ty = mid_pred(mvy[0], mvy[2], mvy[3]);
1028 tx = mid_pred(mvx[0], mvx[1], mvx[3]);
1029 ty = mid_pred(mvy[0], mvy[1], mvy[3]);
1032 tx = mid_pred(mvx[0], mvx[1], mvx[2]);
1033 ty = mid_pred(mvy[0], mvy[1], mvy[2]);
1036 } else if(count[idx] == 2) {
1038 for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
1039 for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
1040 tx = (mvx[t1] + mvx[t2]) / 2;
1041 ty = (mvy[t1] + mvy[t2]) / 2;
1043 return; //no need to do MC for inter blocks
1045 uvmx = (tx + ((tx&3) == 3)) >> 1;
1046 uvmy = (ty + ((ty&3) == 3)) >> 1;
1048 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1049 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1051 uvsrc_x = clip(uvsrc_x, -8, s->mb_width * 8);
1052 uvsrc_y = clip(uvsrc_y, -8, s->mb_height * 8);
1053 srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1054 srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1055 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1056 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
1057 || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
1058 ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1,
1059 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1060 ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
1061 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1062 srcU = s->edge_emu_buffer;
1063 srcV = s->edge_emu_buffer + 16;
1065 /* if we deal with range reduction we need to scale source blocks */
1066 if(v->rangeredfrm) {
1068 uint8_t *src, *src2;
1070 src = srcU; src2 = srcV;
1071 for(j = 0; j < 9; j++) {
1072 for(i = 0; i < 9; i++) {
1073 src[i] = ((src[i] - 128) >> 1) + 128;
1074 src2[i] = ((src2[i] - 128) >> 1) + 128;
1076 src += s->uvlinesize;
1077 src2 += s->uvlinesize;
1080 /* if we deal with intensity compensation we need to scale source blocks */
1081 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1083 uint8_t *src, *src2;
1085 src = srcU; src2 = srcV;
1086 for(j = 0; j < 9; j++) {
1087 for(i = 0; i < 9; i++) {
1088 src[i] = v->lutuv[src[i]];
1089 src2[i] = v->lutuv[src2[i]];
1091 src += s->uvlinesize;
1092 src2 += s->uvlinesize;
1098 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
1099 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
1102 /* Chroma MC always uses qpel blilinear */
1103 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1105 dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
1106 dsp->put_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
1108 dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize);
1109 dsp->put_no_rnd_qpel_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize);
1114 * Decode Simple/Main Profiles sequence header
1115 * @see Figure 7-8, p16-17
1116 * @param avctx Codec context
1117 * @param gb GetBit context initialized from Codec context extra_data
1120 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
1122 VC1Context *v = avctx->priv_data;
1124 av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
1125 v->profile = get_bits(gb, 2);
1126 if (v->profile == 2)
1128 av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
1132 if (v->profile == PROFILE_ADVANCED)
1134 v->level = get_bits(gb, 3);
1137 av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
1139 v->chromaformat = get_bits(gb, 2);
1140 if (v->chromaformat != 1)
1142 av_log(avctx, AV_LOG_ERROR,
1143 "Only 4:2:0 chroma format supported\n");
1149 v->res_sm = get_bits(gb, 2); //reserved
1152 av_log(avctx, AV_LOG_ERROR,
1153 "Reserved RES_SM=%i is forbidden\n", v->res_sm);
1159 v->frmrtq_postproc = get_bits(gb, 3); //common
1160 // (bitrate-32kbps)/64kbps
1161 v->bitrtq_postproc = get_bits(gb, 5); //common
1162 v->s.loop_filter = get_bits(gb, 1); //common
1163 if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
1165 av_log(avctx, AV_LOG_ERROR,
1166 "LOOPFILTER shell not be enabled in simple profile\n");
1169 if (v->profile < PROFILE_ADVANCED)
1171 v->res_x8 = get_bits(gb, 1); //reserved
1174 av_log(avctx, AV_LOG_ERROR,
1175 "1 for reserved RES_X8 is forbidden\n");
1178 v->multires = get_bits(gb, 1);
1179 v->res_fasttx = get_bits(gb, 1);
1182 av_log(avctx, AV_LOG_ERROR,
1183 "0 for reserved RES_FASTTX is forbidden\n");
1188 v->fastuvmc = get_bits(gb, 1); //common
1189 if (!v->profile && !v->fastuvmc)
1191 av_log(avctx, AV_LOG_ERROR,
1192 "FASTUVMC unavailable in Simple Profile\n");
1195 v->extended_mv = get_bits(gb, 1); //common
1196 if (!v->profile && v->extended_mv)
1198 av_log(avctx, AV_LOG_ERROR,
1199 "Extended MVs unavailable in Simple Profile\n");
1202 v->dquant = get_bits(gb, 2); //common
1203 v->vstransform = get_bits(gb, 1); //common
1205 if (v->profile < PROFILE_ADVANCED)
1207 v->res_transtab = get_bits(gb, 1);
1208 if (v->res_transtab)
1210 av_log(avctx, AV_LOG_ERROR,
1211 "1 for reserved RES_TRANSTAB is forbidden\n");
1216 v->overlap = get_bits(gb, 1); //common
1218 if (v->profile < PROFILE_ADVANCED)
1220 v->s.resync_marker = get_bits(gb, 1);
1221 v->rangered = get_bits(gb, 1);
1222 if (v->rangered && v->profile == PROFILE_SIMPLE)
1224 av_log(avctx, AV_LOG_INFO,
1225 "RANGERED should be set to 0 in simple profile\n");
1229 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
1230 v->quantizer_mode = get_bits(gb, 2); //common
1232 if (v->profile < PROFILE_ADVANCED)
1234 v->finterpflag = get_bits(gb, 1); //common
1235 v->res_rtm_flag = get_bits(gb, 1); //reserved
1236 if (!v->res_rtm_flag)
1238 // av_log(avctx, AV_LOG_ERROR,
1239 // "0 for reserved RES_RTM_FLAG is forbidden\n");
1240 av_log(avctx, AV_LOG_ERROR,
1241 "Old WMV3 version detected, only I-frames will be decoded\n");
1244 av_log(avctx, AV_LOG_DEBUG,
1245 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1246 "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
1247 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
1248 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
1249 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
1250 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
1251 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
1252 v->dquant, v->quantizer_mode, avctx->max_b_frames
1260 static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1262 int pqindex, lowquant, status;
1264 if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1265 skip_bits(gb, 2); //framecnt unused
1267 if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1268 v->s.pict_type = get_bits(gb, 1);
1269 if (v->s.avctx->max_b_frames) {
1270 if (!v->s.pict_type) {
1271 if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1272 else v->s.pict_type = B_TYPE;
1273 } else v->s.pict_type = P_TYPE;
1274 } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1276 if(v->s.pict_type == I_TYPE)
1277 get_bits(gb, 7); // skip buffer fullness
1278 if(v->s.pict_type == B_TYPE) {
1279 v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1280 v->bfraction = vc1_bfraction_lut[v->bfraction];
1281 if(v->bfraction == -1) {
1282 v->s.pict_type = BI_TYPE;
1287 if(v->s.pict_type == I_TYPE)
1289 if(v->s.pict_type == P_TYPE)
1292 /* Quantizer stuff */
1293 pqindex = get_bits(gb, 5);
1294 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1295 v->pq = pquant_table[0][pqindex];
1297 v->pq = pquant_table[1][pqindex];
1300 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1301 v->pquantizer = pqindex < 9;
1302 if (v->quantizer_mode == QUANT_NON_UNIFORM)
1304 v->pqindex = pqindex;
1305 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1307 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1308 v->pquantizer = get_bits(gb, 1);
1310 if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1311 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1312 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1313 v->range_x = 1 << (v->k_x - 1);
1314 v->range_y = 1 << (v->k_y - 1);
1315 if (v->profile == PROFILE_ADVANCED)
1317 if (v->postprocflag) v->postproc = get_bits(gb, 1);
1320 if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
1322 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1323 // (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1325 //TODO: complete parsing for P/B/BI frames
1326 switch(v->s.pict_type) {
1328 if (v->pq < 5) v->tt_index = 0;
1329 else if(v->pq < 13) v->tt_index = 1;
1330 else v->tt_index = 2;
1332 lowquant = (v->pq > 12) ? 0 : 1;
1333 v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1334 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1336 int scale, shift, i;
1337 v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1338 v->lumscale = get_bits(gb, 6);
1339 v->lumshift = get_bits(gb, 6);
1340 /* fill lookup tables for intensity compensation */
1343 shift = (255 - v->lumshift * 2) << 6;
1344 if(v->lumshift > 31)
1347 scale = v->lumscale + 32;
1348 if(v->lumshift > 31)
1349 shift = (v->lumshift - 64) << 6;
1351 shift = v->lumshift << 6;
1353 for(i = 0; i < 256; i++) {
1354 v->luty[i] = clip_uint8((scale * i + shift + 32) >> 6);
1355 v->lutuv[i] = clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1358 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1359 v->s.quarter_sample = 0;
1360 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1361 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1362 v->s.quarter_sample = 0;
1364 v->s.quarter_sample = 1;
1366 v->s.quarter_sample = 1;
1367 v->s.mspel = !(v->mv_mode == MV_PMODE_1MV_HPEL_BILIN || (v->mv_mode == MV_PMODE_INTENSITY_COMP && v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN));
1369 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1370 v->mv_mode2 == MV_PMODE_MIXED_MV)
1371 || v->mv_mode == MV_PMODE_MIXED_MV)
1373 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1374 if (status < 0) return -1;
1375 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1376 "Imode: %i, Invert: %i\n", status>>1, status&1);
1378 v->mv_type_is_raw = 0;
1379 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1381 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1382 if (status < 0) return -1;
1383 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1384 "Imode: %i, Invert: %i\n", status>>1, status&1);
1386 /* Hopefully this is correct for P frames */
1387 v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1388 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1392 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1393 vop_dquant_decoding(v);
1396 v->ttfrm = 0; //FIXME Is that so ?
1399 v->ttmbf = get_bits(gb, 1);
1402 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1410 if (v->pq < 5) v->tt_index = 0;
1411 else if(v->pq < 13) v->tt_index = 1;
1412 else v->tt_index = 2;
1414 lowquant = (v->pq > 12) ? 0 : 1;
1415 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1416 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1417 v->s.mspel = v->s.quarter_sample;
1419 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1420 if (status < 0) return -1;
1421 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1422 "Imode: %i, Invert: %i\n", status>>1, status&1);
1423 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1424 if (status < 0) return -1;
1425 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1426 "Imode: %i, Invert: %i\n", status>>1, status&1);
1428 v->s.mv_table_index = get_bits(gb, 2);
1429 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1433 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1434 vop_dquant_decoding(v);
1440 v->ttmbf = get_bits(gb, 1);
1443 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1453 v->c_ac_table_index = decode012(gb);
1454 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1456 v->y_ac_table_index = decode012(gb);
1459 v->s.dc_table_index = get_bits(gb, 1);
1464 /***********************************************************************/
1466 * @defgroup block VC-1 Block-level functions
1467 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1468 * @todo TODO: Integrate to MpegEncContext facilities
1474 * @brief Get macroblock-level quantizer scale
1475 * @warning XXX: qdiff to the frame quant, not previous quant ?
1476 * @fixme XXX: Don't know how to initialize mquant otherwise in last case
1478 #define GET_MQUANT() \
1482 if (v->dqprofile == DQPROFILE_ALL_MBS) \
1486 mquant = (get_bits(gb, 1)) ? v->altpq : v->pq; \
1490 mqdiff = get_bits(gb, 3); \
1491 if (mqdiff != 7) mquant = v->pq + mqdiff; \
1492 else mquant = get_bits(gb, 5); \
1495 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1496 edges = 1 << v->dqsbedge; \
1497 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1498 edges = (3 << v->dqsbedge) % 15; \
1499 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
1501 if((edges&1) && !s->mb_x) \
1502 mquant = v->altpq; \
1503 if((edges&2) && s->first_slice_line) \
1504 mquant = v->altpq; \
1505 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
1506 mquant = v->altpq; \
1507 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
1508 mquant = v->altpq; \
1512 * @def GET_MVDATA(_dmv_x, _dmv_y)
1513 * @brief Get MV differentials
1514 * @see MVDATA decoding from 8.3.5.2, p(1)20
1515 * @param _dmv_x Horizontal differential for decoded MV
1516 * @param _dmv_y Vertical differential for decoded MV
1517 * @todo TODO: Use MpegEncContext arrays to store them
1519 #define GET_MVDATA(_dmv_x, _dmv_y) \
1520 index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1521 VC1_MV_DIFF_VLC_BITS, 2); \
1524 mb_has_coeffs = 1; \
1527 else mb_has_coeffs = 0; \
1529 if (!index) { _dmv_x = _dmv_y = 0; } \
1530 else if (index == 35) \
1532 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1533 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1535 else if (index == 36) \
1544 if (!s->quarter_sample && index1 == 5) val = 1; \
1546 if(size_table[index1] - val > 0) \
1547 val = get_bits(gb, size_table[index1] - val); \
1549 sign = 0 - (val&1); \
1550 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1553 if (!s->quarter_sample && index1 == 5) val = 1; \
1555 if(size_table[index1] - val > 0) \
1556 val = get_bits(gb, size_table[index1] - val); \
1558 sign = 0 - (val&1); \
1559 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1562 /** Predict and set motion vector
1564 static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra)
1566 int xy, wrap, off = 0;
1571 /* scale MV difference to be quad-pel */
1572 dmv_x <<= 1 - s->quarter_sample;
1573 dmv_y <<= 1 - s->quarter_sample;
1575 wrap = s->b8_stride;
1576 xy = s->block_index[n];
1579 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1580 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1581 if(mv1) { /* duplicate motion data for 1-MV block */
1582 s->current_picture.motion_val[0][xy + 1][0] = 0;
1583 s->current_picture.motion_val[0][xy + 1][1] = 0;
1584 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1585 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1586 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1587 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1592 C = s->current_picture.motion_val[0][xy - 1];
1593 A = s->current_picture.motion_val[0][xy - wrap];
1595 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1597 //in 4-MV mode different blocks have different B predictor position
1600 off = (s->mb_x > 0) ? -1 : 1;
1603 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1612 B = s->current_picture.motion_val[0][xy - wrap + off];
1614 if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
1615 if(s->mb_width == 1) {
1619 px = mid_pred(A[0], B[0], C[0]);
1620 py = mid_pred(A[1], B[1], C[1]);
1622 } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
1628 /* Pullback MV as specified in 8.3.5.3.4 */
1631 qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
1632 qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
1633 X = (s->mb_width << 6) - 4;
1634 Y = (s->mb_height << 6) - 4;
1636 if(qx + px < -60) px = -60 - qx;
1637 if(qy + py < -60) py = -60 - qy;
1639 if(qx + px < -28) px = -28 - qx;
1640 if(qy + py < -28) py = -28 - qy;
1642 if(qx + px > X) px = X - qx;
1643 if(qy + py > Y) py = Y - qy;
1645 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1646 if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
1647 if(is_intra[xy - wrap])
1648 sum = ABS(px) + ABS(py);
1650 sum = ABS(px - A[0]) + ABS(py - A[1]);
1652 if(get_bits1(&s->gb)) {
1660 if(is_intra[xy - 1])
1661 sum = ABS(px) + ABS(py);
1663 sum = ABS(px - C[0]) + ABS(py - C[1]);
1665 if(get_bits1(&s->gb)) {
1675 /* store MV using signed modulus of MV range defined in 4.11 */
1676 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1677 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1678 if(mv1) { /* duplicate motion data for 1-MV block */
1679 s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
1680 s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
1681 s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
1682 s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
1683 s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
1684 s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
1688 /** Reconstruct motion vector for B-frame and do motion compensation
1690 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
1692 MpegEncContext *s = &v->s;
1693 int mx[4], my[4], mv_x, mv_y;
1696 /* scale MV difference to be quad-pel */
1697 dmv_x[0] <<= 1 - s->quarter_sample;
1698 dmv_y[0] <<= 1 - s->quarter_sample;
1699 dmv_x[1] <<= 1 - s->quarter_sample;
1700 dmv_y[1] <<= 1 - s->quarter_sample;
1702 if(direct || mode == BMV_TYPE_INTERPOLATED) {
1707 if(mode == BMV_TYPE_BACKWARD) {
1708 for(i = 0; i < 4; i++) {
1709 mx[i] = s->last_picture.motion_val[0][s->block_index[i]][0];
1710 my[i] = s->last_picture.motion_val[0][s->block_index[i]][1];
1713 for(i = 0; i < 4; i++) {
1714 mx[i] = s->next_picture.motion_val[0][s->block_index[i]][0];
1715 my[i] = s->next_picture.motion_val[0][s->block_index[i]][1];
1719 /* XXX: not right but how to determine 4-MV intra/inter in another frame? */
1720 mv_x = median4(mx[0], mx[1], mx[2], mx[3]);
1721 mv_y = median4(my[0], my[1], my[2], my[3]);
1722 s->mv[0][0][0] = mv_x;
1723 s->mv[0][0][1] = mv_y;
1725 vc1_mc_1mv(v, (mode == BMV_TYPE_FORWARD));
1728 /** Get predicted DC value for I-frames only
1729 * prediction dir: left=0, top=1
1730 * @param s MpegEncContext
1731 * @param[in] n block index in the current MB
1732 * @param dc_val_ptr Pointer to DC predictor
1733 * @param dir_ptr Prediction direction for use in AC prediction
1735 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1736 int16_t **dc_val_ptr, int *dir_ptr)
1738 int a, b, c, wrap, pred, scale;
1740 static const uint16_t dcpred[32] = {
1741 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
1742 114, 102, 93, 85, 79, 73, 68, 64,
1743 60, 57, 54, 51, 49, 47, 45, 43,
1744 41, 39, 38, 37, 35, 34, 33
1747 /* find prediction - wmv3_dc_scale always used here in fact */
1748 if (n < 4) scale = s->y_dc_scale;
1749 else scale = s->c_dc_scale;
1751 wrap = s->block_wrap[n];
1752 dc_val= s->dc_val[0] + s->block_index[n];
1758 b = dc_val[ - 1 - wrap];
1759 a = dc_val[ - wrap];
1761 if (pq < 9 || !overlap)
1763 /* Set outer values */
1764 if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1765 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1769 /* Set outer values */
1770 if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1771 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1774 if (abs(a - b) <= abs(b - c)) {
1782 /* update predictor */
1783 *dc_val_ptr = &dc_val[0];
1788 /** Get predicted DC value
1789 * prediction dir: left=0, top=1
1790 * @param s MpegEncContext
1791 * @param[in] n block index in the current MB
1792 * @param dc_val_ptr Pointer to DC predictor
1793 * @param dir_ptr Prediction direction for use in AC prediction
1795 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1796 int a_avail, int c_avail,
1797 int16_t **dc_val_ptr, int *dir_ptr)
1799 int a, b, c, wrap, pred, scale;
1801 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1804 /* find prediction - wmv3_dc_scale always used here in fact */
1805 if (n < 4) scale = s->y_dc_scale;
1806 else scale = s->c_dc_scale;
1808 wrap = s->block_wrap[n];
1809 dc_val= s->dc_val[0] + s->block_index[n];
1815 b = dc_val[ - 1 - wrap];
1816 a = dc_val[ - wrap];
1817 /* scale predictors if needed */
1818 q1 = s->current_picture.qscale_table[mb_pos];
1819 if(c_avail && (n!= 1 && n!=3)) {
1820 q2 = s->current_picture.qscale_table[mb_pos - 1];
1822 c = (c * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1824 if(a_avail && (n!= 2 && n!=3)) {
1825 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1827 a = (a * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1829 if(a_avail && c_avail && (n!=3)) {
1832 if(n != 2) off -= s->mb_stride;
1833 q2 = s->current_picture.qscale_table[off];
1835 b = (b * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1838 if(a_avail && c_avail) {
1839 if(abs(a - b) <= abs(b - c)) {
1846 } else if(a_avail) {
1849 } else if(c_avail) {
1857 /* update predictor */
1858 *dc_val_ptr = &dc_val[0];
1864 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1865 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1866 * @todo TODO: Integrate to MpegEncContext facilities
1870 static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1872 int xy, wrap, pred, a, b, c;
1874 xy = s->block_index[n];
1875 wrap = s->b8_stride;
1880 a = s->coded_block[xy - 1 ];
1881 b = s->coded_block[xy - 1 - wrap];
1882 c = s->coded_block[xy - wrap];
1891 *coded_block_ptr = &s->coded_block[xy];
1897 * Decode one AC coefficient
1898 * @param v The VC1 context
1899 * @param last Last coefficient
1900 * @param skip How much zero coefficients to skip
1901 * @param value Decoded AC coefficient value
1904 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1906 GetBitContext *gb = &v->s.gb;
1907 int index, escape, run = 0, level = 0, lst = 0;
1909 index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1910 if (index != vc1_ac_sizes[codingset] - 1) {
1911 run = vc1_index_decode_table[codingset][index][0];
1912 level = vc1_index_decode_table[codingset][index][1];
1913 lst = index >= vc1_last_decode_table[codingset];
1917 escape = decode210(gb);
1919 index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1920 run = vc1_index_decode_table[codingset][index][0];
1921 level = vc1_index_decode_table[codingset][index][1];
1922 lst = index >= vc1_last_decode_table[codingset];
1925 level += vc1_last_delta_level_table[codingset][run];
1927 level += vc1_delta_level_table[codingset][run];
1930 run += vc1_last_delta_run_table[codingset][level] + 1;
1932 run += vc1_delta_run_table[codingset][level] + 1;
1938 lst = get_bits(gb, 1);
1939 if(v->s.esc3_level_length == 0) {
1940 if(v->pq < 8 || v->dquantfrm) { // table 59
1941 v->s.esc3_level_length = get_bits(gb, 3);
1942 if(!v->s.esc3_level_length)
1943 v->s.esc3_level_length = get_bits(gb, 2) + 8;
1945 v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
1947 v->s.esc3_run_length = 3 + get_bits(gb, 2);
1949 run = get_bits(gb, v->s.esc3_run_length);
1950 sign = get_bits(gb, 1);
1951 level = get_bits(gb, v->s.esc3_level_length);
1962 /** Decode intra block in intra frames - should be faster than decode_intra_block
1963 * @param v VC1Context
1964 * @param block block to decode
1965 * @param coded are AC coeffs present or not
1966 * @param codingset set of VLC to decode data
1968 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1970 GetBitContext *gb = &v->s.gb;
1971 MpegEncContext *s = &v->s;
1972 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1975 int16_t *ac_val, *ac_val2;
1978 /* Get DC differential */
1980 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1982 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1985 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1990 if (dcdiff == 119 /* ESC index value */)
1992 /* TODO: Optimize */
1993 if (v->pq == 1) dcdiff = get_bits(gb, 10);
1994 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1995 else dcdiff = get_bits(gb, 8);
2000 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2001 else if (v->pq == 2)
2002 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2004 if (get_bits(gb, 1))
2009 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2012 /* Store the quantized DC coeff, used for prediction */
2014 block[0] = dcdiff * s->y_dc_scale;
2016 block[0] = dcdiff * s->c_dc_scale;
2029 int last = 0, skip, value;
2030 const int8_t *zz_table;
2034 scale = v->pq * 2 + v->halfpq;
2038 zz_table = vc1_horizontal_zz;
2040 zz_table = vc1_vertical_zz;
2042 zz_table = vc1_normal_zz;
2044 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2046 if(dc_pred_dir) //left
2049 ac_val -= 16 * s->block_wrap[n];
2052 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2056 block[zz_table[i++]] = value;
2059 /* apply AC prediction if needed */
2061 if(dc_pred_dir) { //left
2062 for(k = 1; k < 8; k++)
2063 block[k << 3] += ac_val[k];
2065 for(k = 1; k < 8; k++)
2066 block[k] += ac_val[k + 8];
2069 /* save AC coeffs for further prediction */
2070 for(k = 1; k < 8; k++) {
2071 ac_val2[k] = block[k << 3];
2072 ac_val2[k + 8] = block[k];
2075 /* scale AC coeffs */
2076 for(k = 1; k < 64; k++)
2080 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2083 if(s->ac_pred) i = 63;
2089 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2092 scale = v->pq * 2 + v->halfpq;
2093 memset(ac_val2, 0, 16 * 2);
2094 if(dc_pred_dir) {//left
2097 memcpy(ac_val2, ac_val, 8 * 2);
2099 ac_val -= 16 * s->block_wrap[n];
2101 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2104 /* apply AC prediction if needed */
2106 if(dc_pred_dir) { //left
2107 for(k = 1; k < 8; k++) {
2108 block[k << 3] = ac_val[k] * scale;
2109 if(!v->pquantizer && block[k << 3])
2110 block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2113 for(k = 1; k < 8; k++) {
2114 block[k] = ac_val[k + 8] * scale;
2115 if(!v->pquantizer && block[k])
2116 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2122 s->block_last_index[n] = i;
2127 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2128 * @param v VC1Context
2129 * @param block block to decode
2130 * @param coded are AC coeffs present or not
2131 * @param mquant block quantizer
2132 * @param codingset set of VLC to decode data
2134 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
2136 GetBitContext *gb = &v->s.gb;
2137 MpegEncContext *s = &v->s;
2138 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2141 int16_t *ac_val, *ac_val2;
2143 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2144 int a_avail = v->a_avail, c_avail = v->c_avail;
2145 int use_pred = s->ac_pred;
2149 /* XXX: Guard against dumb values of mquant */
2150 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
2152 /* Set DC scale - y and c use the same */
2153 s->y_dc_scale = s->y_dc_scale_table[mquant];
2154 s->c_dc_scale = s->c_dc_scale_table[mquant];
2156 /* Get DC differential */
2158 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2160 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2163 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2168 if (dcdiff == 119 /* ESC index value */)
2170 /* TODO: Optimize */
2171 if (mquant == 1) dcdiff = get_bits(gb, 10);
2172 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2173 else dcdiff = get_bits(gb, 8);
2178 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2179 else if (mquant == 2)
2180 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2182 if (get_bits(gb, 1))
2187 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2190 /* Store the quantized DC coeff, used for prediction */
2193 block[0] = dcdiff * s->y_dc_scale;
2195 block[0] = dcdiff * s->c_dc_scale;
2204 /* check if AC is needed at all and adjust direction if needed */
2205 if(!a_avail) dc_pred_dir = 1;
2206 if(!c_avail) dc_pred_dir = 0;
2207 if(!a_avail && !c_avail) use_pred = 0;
2208 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2211 scale = mquant * 2 + v->halfpq;
2213 if(dc_pred_dir) //left
2216 ac_val -= 16 * s->block_wrap[n];
2218 q1 = s->current_picture.qscale_table[mb_pos];
2219 if(dc_pred_dir && c_avail) q2 = s->current_picture.qscale_table[mb_pos - 1];
2220 if(!dc_pred_dir && a_avail) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2221 if(n && n<4) q2 = q1;
2224 int last = 0, skip, value;
2225 const int8_t *zz_table;
2228 zz_table = vc1_simple_progressive_8x8_zz;
2231 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2235 block[zz_table[i++]] = value;
2238 /* apply AC prediction if needed */
2240 /* scale predictors if needed*/
2245 if(dc_pred_dir) { //left
2246 for(k = 1; k < 8; k++)
2247 block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2249 for(k = 1; k < 8; k++)
2250 block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2253 if(dc_pred_dir) { //left
2254 for(k = 1; k < 8; k++)
2255 block[k << 3] += ac_val[k];
2257 for(k = 1; k < 8; k++)
2258 block[k] += ac_val[k + 8];
2262 /* save AC coeffs for further prediction */
2263 for(k = 1; k < 8; k++) {
2264 ac_val2[k] = block[k << 3];
2265 ac_val2[k + 8] = block[k];
2268 /* scale AC coeffs */
2269 for(k = 1; k < 64; k++)
2273 block[k] += (block[k] < 0) ? -mquant : mquant;
2276 if(use_pred) i = 63;
2277 } else { // no AC coeffs
2280 memset(ac_val2, 0, 16 * 2);
2281 if(dc_pred_dir) {//left
2283 memcpy(ac_val2, ac_val, 8 * 2);
2287 for(k = 1; k < 8; k++)
2288 ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2293 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2297 for(k = 1; k < 8; k++)
2298 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2303 /* apply AC prediction if needed */
2305 if(dc_pred_dir) { //left
2306 for(k = 1; k < 8; k++) {
2307 block[k << 3] = ac_val2[k] * scale;
2308 if(!v->pquantizer && block[k << 3])
2309 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2312 for(k = 1; k < 8; k++) {
2313 block[k] = ac_val2[k + 8] * scale;
2314 if(!v->pquantizer && block[k])
2315 block[k] += (block[k] < 0) ? -mquant : mquant;
2321 s->block_last_index[n] = i;
2328 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
2330 MpegEncContext *s = &v->s;
2331 GetBitContext *gb = &s->gb;
2334 int scale, off, idx, last, skip, value;
2335 int ttblk = ttmb & 7;
2338 ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
2340 if(ttblk == TT_4X4) {
2341 subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2343 if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2344 subblkpat = decode012(gb);
2345 if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2346 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2347 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2349 scale = 2 * mquant + v->halfpq;
2351 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2352 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2353 subblkpat = 2 - (ttblk == TT_8X4_TOP);
2356 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2357 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2365 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2369 idx = vc1_simple_progressive_8x8_zz[i++];
2370 block[idx] = value * scale;
2372 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2374 s->dsp.vc1_inv_trans_8x8(block);
2377 for(j = 0; j < 4; j++) {
2378 last = subblkpat & (1 << (3 - j));
2380 off = (j & 1) * 4 + (j & 2) * 16;
2382 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2386 idx = vc1_simple_progressive_4x4_zz[i++];
2387 block[idx + off] = value * scale;
2389 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2391 if(!(subblkpat & (1 << (3 - j))))
2392 s->dsp.vc1_inv_trans_4x4(block, j);
2396 for(j = 0; j < 2; j++) {
2397 last = subblkpat & (1 << (1 - j));
2401 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2405 idx = vc1_simple_progressive_8x4_zz[i++];
2406 block[idx + off] = value * scale;
2408 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2410 if(!(subblkpat & (1 << (1 - j))))
2411 s->dsp.vc1_inv_trans_8x4(block, j);
2415 for(j = 0; j < 2; j++) {
2416 last = subblkpat & (1 << (1 - j));
2420 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2424 idx = vc1_simple_progressive_4x8_zz[i++];
2425 block[idx + off] = value * scale;
2427 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2429 if(!(subblkpat & (1 << (1 - j))))
2430 s->dsp.vc1_inv_trans_4x8(block, j);
2438 /** Decode one P-frame MB (in Simple/Main profile)
2439 * @todo TODO: Extend to AP
2440 * @fixme FIXME: DC value for inter blocks not set
2442 static int vc1_decode_p_mb(VC1Context *v)
2444 MpegEncContext *s = &v->s;
2445 GetBitContext *gb = &s->gb;
2447 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2448 int cbp; /* cbp decoding stuff */
2449 int mqdiff, mquant; /* MB quantization */
2450 int ttmb = v->ttfrm; /* MB Transform type */
2453 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
2454 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2455 int mb_has_coeffs = 1; /* last_flag */
2456 int dmv_x, dmv_y; /* Differential MV components */
2457 int index, index1; /* LUT indices */
2458 int val, sign; /* temp values */
2459 int first_block = 1;
2461 int skipped, fourmv;
2463 mquant = v->pq; /* Loosy initialization */
2465 if (v->mv_type_is_raw)
2466 fourmv = get_bits1(gb);
2468 fourmv = v->mv_type_mb_plane[mb_pos];
2470 skipped = get_bits1(gb);
2472 skipped = v->s.mbskip_table[mb_pos];
2474 s->dsp.clear_blocks(s->block[0]);
2476 if (!fourmv) /* 1MV mode */
2480 GET_MVDATA(dmv_x, dmv_y);
2482 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2483 vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2485 /* FIXME Set DC val for inter block ? */
2486 if (s->mb_intra && !mb_has_coeffs)
2489 s->ac_pred = get_bits(gb, 1);
2492 else if (mb_has_coeffs)
2494 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
2495 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2503 s->current_picture.qscale_table[mb_pos] = mquant;
2505 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2506 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
2507 VC1_TTMB_VLC_BITS, 2);
2508 if(!s->mb_intra) vc1_mc_1mv(v, 0);
2512 s->dc_val[0][s->block_index[i]] = 0;
2514 val = ((cbp >> (5 - i)) & 1);
2515 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2516 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2518 /* check if prediction blocks A and C are available */
2519 v->a_avail = v->c_avail = 0;
2520 if(i == 2 || i == 3 || !s->first_slice_line)
2521 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2522 if(i == 1 || i == 3 || s->mb_x)
2523 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2525 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2526 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2527 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2528 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2529 for(j = 0; j < 64; j++) s->block[i][j] += 128;
2530 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2531 /* TODO: proper loop filtering */
2532 if(v->pq >= 9 && v->overlap) {
2534 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1));
2536 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1));
2539 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
2540 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2542 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
2543 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2550 for(i = 0; i < 6; i++) {
2551 v->mb_type[0][s->block_index[i]] = 0;
2552 s->dc_val[0][s->block_index[i]] = 0;
2554 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2555 s->current_picture.qscale_table[mb_pos] = 0;
2556 vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2563 if (!skipped /* unskipped MB */)
2565 int intra_count = 0, coded_inter = 0;
2566 int is_intra[6], is_coded[6];
2568 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2571 val = ((cbp >> (5 - i)) & 1);
2572 s->dc_val[0][s->block_index[i]] = 0;
2579 GET_MVDATA(dmv_x, dmv_y);
2581 vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2582 if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2583 intra_count += s->mb_intra;
2584 is_intra[i] = s->mb_intra;
2585 is_coded[i] = mb_has_coeffs;
2588 is_intra[i] = (intra_count >= 3);
2591 if(i == 4) vc1_mc_4mv_chroma(v);
2592 v->mb_type[0][s->block_index[i]] = is_intra[i];
2593 if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2595 // if there are no coded blocks then don't do anything more
2596 if(!intra_count && !coded_inter) return 0;
2599 s->current_picture.qscale_table[mb_pos] = mquant;
2600 /* test if block is intra and has pred */
2605 if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
2606 || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
2611 if(intrapred)s->ac_pred = get_bits(gb, 1);
2612 else s->ac_pred = 0;
2614 if (!v->ttmbf && coded_inter)
2615 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2619 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2620 s->mb_intra = is_intra[i];
2622 /* check if prediction blocks A and C are available */
2623 v->a_avail = v->c_avail = 0;
2624 if(i == 2 || i == 3 || !s->first_slice_line)
2625 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2626 if(i == 1 || i == 3 || s->mb_x)
2627 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2629 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2630 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2631 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2632 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2633 for(j = 0; j < 64; j++) s->block[i][j] += 128;
2634 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2635 /* TODO: proper loop filtering */
2636 if(v->pq >= 9 && v->overlap) {
2638 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1));
2640 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1));
2642 } else if(is_coded[i]) {
2643 status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
2644 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2646 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
2647 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2655 s->current_picture.qscale_table[mb_pos] = 0;
2656 for (i=0; i<6; i++) {
2657 v->mb_type[0][s->block_index[i]] = 0;
2658 s->dc_val[0][s->block_index[i]] = 0;
2662 vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2663 vc1_mc_4mv_luma(v, i);
2665 vc1_mc_4mv_chroma(v);
2666 s->current_picture.qscale_table[mb_pos] = 0;
2671 /* Should never happen */
2675 /** Decode one B-frame MB (in Main profile)
2677 static void vc1_decode_b_mb(VC1Context *v)
2679 MpegEncContext *s = &v->s;
2680 GetBitContext *gb = &s->gb;
2682 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2683 int cbp; /* cbp decoding stuff */
2684 int mqdiff, mquant; /* MB quantization */
2685 int ttmb = v->ttfrm; /* MB Transform type */
2687 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
2688 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2689 int mb_has_coeffs = 0; /* last_flag */
2690 int index, index1; /* LUT indices */
2691 int val, sign; /* temp values */
2692 int first_block = 1;
2694 int skipped, direct;
2695 int dmv_x[2], dmv_y[2];
2696 int bmvtype = BMV_TYPE_BACKWARD; /* XXX: is it so? */
2698 mquant = v->pq; /* Loosy initialization */
2702 direct = get_bits1(gb);
2704 direct = v->direct_mb_plane[mb_pos];
2706 skipped = get_bits1(gb);
2708 skipped = v->s.mbskip_table[mb_pos];
2710 s->dsp.clear_blocks(s->block[0]);
2711 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2712 for(i = 0; i < 6; i++) {
2713 v->mb_type[0][s->block_index[i]] = 0;
2714 s->dc_val[0][s->block_index[i]] = 0;
2716 s->current_picture.qscale_table[mb_pos] = 0;
2720 GET_MVDATA(dmv_x[0], dmv_y[0]);
2722 if(skipped || !s->mb_intra) {
2723 bmvtype = decode012(gb);
2726 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2729 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2732 bmvtype = BMV_TYPE_INTERPOLATED;
2738 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2742 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2744 s->current_picture.qscale_table[mb_pos] = mquant;
2745 if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2746 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2747 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2749 if(!mb_has_coeffs && !s->mb_intra) {
2750 /* no coded blocks - effectively skipped */
2751 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2754 if(s->mb_intra && !mb_has_coeffs) {
2756 s->current_picture.qscale_table[mb_pos] = mquant;
2757 s->ac_pred = get_bits1(gb);
2760 if(bmvtype == BMV_TYPE_INTERPOLATED) {
2761 GET_MVDATA(dmv_x[1], dmv_y[1]);
2762 if(!mb_has_coeffs) {
2763 /* interpolated skipped block */
2764 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2769 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2771 s->ac_pred = get_bits1(gb);
2772 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2774 s->current_picture.qscale_table[mb_pos] = mquant;
2775 if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2776 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2782 s->dc_val[0][s->block_index[i]] = 0;
2784 val = ((cbp >> (5 - i)) & 1);
2785 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2786 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2788 /* check if prediction blocks A and C are available */
2789 v->a_avail = v->c_avail = 0;
2790 if(i == 2 || i == 3 || !s->first_slice_line)
2791 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2792 if(i == 1 || i == 3 || s->mb_x)
2793 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2795 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2796 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2797 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2798 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2799 for(j = 0; j < 64; j++) s->block[i][j] += 128;
2800 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2801 /* TODO: proper loop filtering */
2802 if(v->pq >= 9 && v->overlap) {
2804 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? ((i&1)>>1) : (s->mb_y&1));
2806 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), (i<4) ? (i&1) : (s->mb_x&1));
2809 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
2810 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2812 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
2813 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2818 /** Decode blocks of I-frame
2820 static void vc1_decode_i_blocks(VC1Context *v)
2823 MpegEncContext *s = &v->s;
2828 /* select codingmode used for VLC tables selection */
2829 switch(v->y_ac_table_index){
2831 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2834 v->codingset = CS_HIGH_MOT_INTRA;
2837 v->codingset = CS_MID_RATE_INTRA;
2841 switch(v->c_ac_table_index){
2843 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2846 v->codingset2 = CS_HIGH_MOT_INTER;
2849 v->codingset2 = CS_MID_RATE_INTER;
2853 /* Set DC scale - y and c use the same */
2854 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2855 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2858 s->mb_x = s->mb_y = 0;
2860 s->first_slice_line = 1;
2861 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2862 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2863 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
2864 ff_init_block_index(s);
2865 ff_update_block_index(s);
2866 s->dsp.clear_blocks(s->block[0]);
2867 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2868 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2869 s->current_picture.qscale_table[mb_pos] = v->pq;
2871 // do actual MB decoding and displaying
2872 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2873 v->s.ac_pred = get_bits(&v->s.gb, 1);
2875 for(k = 0; k < 6; k++) {
2876 val = ((cbp >> (5 - k)) & 1);
2879 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2883 cbp |= val << (5 - k);
2885 vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
2887 s->dsp.vc1_inv_trans_8x8(s->block[k]);
2888 if(v->pq >= 9 && v->overlap) {
2889 for(j = 0; j < 64; j++) s->block[k][j] += 128;
2893 vc1_put_block(v, s->block);
2894 if(v->pq >= 9 && v->overlap) { /* XXX: do proper overlapping insted of loop filter */
2895 if(!s->first_slice_line) {
2896 s->dsp.vc1_v_overlap(s->dest[0], s->linesize, 0);
2897 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize, 0);
2898 if(!(s->flags & CODEC_FLAG_GRAY)) {
2899 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize, s->mb_y&1);
2900 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize, s->mb_y&1);
2903 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 1);
2904 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1);
2906 s->dsp.vc1_h_overlap(s->dest[0], s->linesize, 0);
2907 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize, 0);
2908 if(!(s->flags & CODEC_FLAG_GRAY)) {
2909 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize, s->mb_x&1);
2910 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize, s->mb_x&1);
2913 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize, 1);
2914 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize, 1);
2917 if(get_bits_count(&s->gb) > v->bits) {
2918 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2922 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2923 s->first_slice_line = 0;
2927 static void vc1_decode_p_blocks(VC1Context *v)
2929 MpegEncContext *s = &v->s;
2931 /* select codingmode used for VLC tables selection */
2932 switch(v->c_ac_table_index){
2934 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2937 v->codingset = CS_HIGH_MOT_INTRA;
2940 v->codingset = CS_MID_RATE_INTRA;
2944 switch(v->c_ac_table_index){
2946 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2949 v->codingset2 = CS_HIGH_MOT_INTER;
2952 v->codingset2 = CS_MID_RATE_INTER;
2956 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2957 s->first_slice_line = 1;
2958 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2959 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
2960 ff_init_block_index(s);
2961 ff_update_block_index(s);
2962 s->dsp.clear_blocks(s->block[0]);
2965 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2966 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
2970 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2971 s->first_slice_line = 0;
2975 static void vc1_decode_b_blocks(VC1Context *v)
2977 MpegEncContext *s = &v->s;
2979 /* select codingmode used for VLC tables selection */
2980 switch(v->c_ac_table_index){
2982 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2985 v->codingset = CS_HIGH_MOT_INTRA;
2988 v->codingset = CS_MID_RATE_INTRA;
2992 switch(v->c_ac_table_index){
2994 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2997 v->codingset2 = CS_HIGH_MOT_INTER;
3000 v->codingset2 = CS_MID_RATE_INTER;
3004 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3005 s->first_slice_line = 1;
3006 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3007 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3008 ff_init_block_index(s);
3009 ff_update_block_index(s);
3010 s->dsp.clear_blocks(s->block[0]);
3013 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
3014 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
3018 ff_draw_horiz_band(s, s->mb_y * 16, 16);
3019 s->first_slice_line = 0;
3023 static void vc1_decode_blocks(VC1Context *v)
3026 v->s.esc3_level_length = 0;
3028 switch(v->s.pict_type) {
3030 vc1_decode_i_blocks(v);
3033 vc1_decode_p_blocks(v);
3036 vc1_decode_b_blocks(v);
3042 /** Initialize a VC1/WMV3 decoder
3043 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3044 * @todo TODO: Decypher remaining bits in extra_data
3046 static int vc1_decode_init(AVCodecContext *avctx)
3048 VC1Context *v = avctx->priv_data;
3049 MpegEncContext *s = &v->s;
3052 if (!avctx->extradata_size || !avctx->extradata) return -1;
3053 if (!(avctx->flags & CODEC_FLAG_GRAY))
3054 avctx->pix_fmt = PIX_FMT_YUV420P;
3056 avctx->pix_fmt = PIX_FMT_GRAY8;
3058 avctx->flags |= CODEC_FLAG_EMU_EDGE;
3059 v->s.flags |= CODEC_FLAG_EMU_EDGE;
3061 if(ff_h263_decode_init(avctx) < 0)
3063 if (vc1_init_common(v) < 0) return -1;
3065 avctx->coded_width = avctx->width;
3066 avctx->coded_height = avctx->height;
3067 if (avctx->codec_id == CODEC_ID_WMV3)
3071 // looks like WMV3 has a sequence header stored in the extradata
3072 // advanced sequence header may be before the first frame
3073 // the last byte of the extradata is a version number, 1 for the
3074 // samples we can decode
3076 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3078 if (decode_sequence_header(avctx, &gb) < 0)
3081 count = avctx->extradata_size*8 - get_bits_count(&gb);
3084 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3085 count, get_bits(&gb, count));
3089 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3092 avctx->has_b_frames= !!(avctx->max_b_frames);
3094 s->mb_width = (avctx->coded_width+15)>>4;
3095 s->mb_height = (avctx->coded_height+15)>>4;
3097 /* Allocate mb bitplanes */
3098 v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3099 v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3101 /* allocate block type info in that way so it could be used with s->block_index[] */
3102 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3103 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3104 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3105 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3107 /* Init coded blocks info */
3108 if (v->profile == PROFILE_ADVANCED)
3110 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3112 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3120 /** Decode a VC1/WMV3 frame
3121 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3122 * @warning Initial try at using MpegEncContext stuff
3124 static int vc1_decode_frame(AVCodecContext *avctx,
3125 void *data, int *data_size,
3126 uint8_t *buf, int buf_size)
3128 VC1Context *v = avctx->priv_data;
3129 MpegEncContext *s = &v->s;
3130 AVFrame *pict = data;
3132 /* no supplementary picture */
3133 if (buf_size == 0) {
3134 /* special case for last picture */
3135 if (s->low_delay==0 && s->next_picture_ptr) {
3136 *pict= *(AVFrame*)s->next_picture_ptr;
3137 s->next_picture_ptr= NULL;
3139 *data_size = sizeof(AVFrame);
3145 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
3146 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3147 int i= ff_find_unused_picture(s, 0);
3148 s->current_picture_ptr= &s->picture[i];
3151 avctx->has_b_frames= !s->low_delay;
3153 init_get_bits(&s->gb, buf, buf_size*8);
3154 // do parse frame header
3155 if(vc1_parse_frame_header(v, &s->gb) == -1)
3158 if(s->pict_type != I_TYPE && !v->res_rtm_flag)return -1;
3161 s->current_picture.pict_type= s->pict_type;
3162 s->current_picture.key_frame= s->pict_type == I_TYPE;
3164 /* skip B-frames if we don't have reference frames */
3165 if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return -1;//buf_size;
3166 /* skip b frames if we are in a hurry */
3167 if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
3168 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
3169 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
3170 || avctx->skip_frame >= AVDISCARD_ALL)
3172 /* skip everything if we are in a hurry>=5 */
3173 if(avctx->hurry_up>=5) return -1;//buf_size;
3175 if(s->next_p_frame_damaged){
3176 if(s->pict_type==B_TYPE)
3179 s->next_p_frame_damaged=0;
3182 if(MPV_frame_start(s, avctx) < 0)
3185 ff_er_frame_start(s);
3187 v->bits = buf_size * 8;
3188 vc1_decode_blocks(v);
3189 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
3190 // if(get_bits_count(&s->gb) > buf_size * 8)
3196 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
3197 assert(s->current_picture.pict_type == s->pict_type);
3198 if (s->pict_type == B_TYPE || s->low_delay) {
3199 *pict= *(AVFrame*)s->current_picture_ptr;
3200 } else if (s->last_picture_ptr != NULL) {
3201 *pict= *(AVFrame*)s->last_picture_ptr;
3204 if(s->last_picture_ptr || s->low_delay){
3205 *data_size = sizeof(AVFrame);
3206 ff_print_debug_info(s, pict);
3209 /* Return the Picture timestamp as the frame number */
3210 /* we substract 1 because it is added on utils.c */
3211 avctx->frame_number = s->picture_number - 1;
3217 /** Close a VC1/WMV3 decoder
3218 * @warning Initial try at using MpegEncContext stuff
3220 static int vc1_decode_end(AVCodecContext *avctx)
3222 VC1Context *v = avctx->priv_data;
3224 av_freep(&v->hrd_rate);
3225 av_freep(&v->hrd_buffer);
3226 MPV_common_end(&v->s);
3227 av_freep(&v->mv_type_mb_plane);
3228 av_freep(&v->direct_mb_plane);
3229 av_freep(&v->mb_type_base);
3234 AVCodec vc1_decoder = {
3247 AVCodec wmv3_decoder = {