2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2006-2007 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * VC-1 and WMV3 decoder
32 #include "mpegvideo.h"
34 #include "vc1acdata.h"
40 extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
41 extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
42 extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
43 #define MB_INTRA_VLC_BITS 9
44 extern VLC ff_msmp4_mb_i_vlc;
45 extern const uint16_t ff_msmp4_mb_i_table[64][2];
48 static const uint16_t table_mb_intra[64][2];
50 /** Available Profiles */
55 PROFILE_COMPLEX, ///< TODO: WMV9 specific
60 /** Sequence quantizer mode */
63 QUANT_FRAME_IMPLICIT, ///< Implicitly specified at frame level
64 QUANT_FRAME_EXPLICIT, ///< Explicitly specified at frame level
65 QUANT_NON_UNIFORM, ///< Non-uniform quant used for all frames
66 QUANT_UNIFORM ///< Uniform quant used for all frames
70 /** Where quant can be changed */
74 DQPROFILE_DOUBLE_EDGES,
75 DQPROFILE_SINGLE_EDGE,
80 /** @name Where quant can be changed
91 /** Which pair of edges is quantized with ALTPQUANT */
94 DQDOUBLE_BEDGE_TOPLEFT,
95 DQDOUBLE_BEDGE_TOPRIGHT,
96 DQDOUBLE_BEDGE_BOTTOMRIGHT,
97 DQDOUBLE_BEDGE_BOTTOMLEFT
101 /** MV modes for P frames */
104 MV_PMODE_1MV_HPEL_BILIN,
108 MV_PMODE_INTENSITY_COMP
112 /** @name MV types for B frames */
117 BMV_TYPE_INTERPOLATED
121 /** @name Block types for P/B frames */
123 enum TransformTypes {
127 TT_8X4, //Both halves
130 TT_4X8, //Both halves
135 /** Table for conversion between TTBLK and TTMB */
136 static const int ttblk_to_tt[3][8] = {
137 { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
138 { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
139 { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
142 static const int ttfrm_to_tt[4] = { TT_8X8, TT_8X4, TT_4X8, TT_4X4 };
144 /** MV P mode - the 5th element is only used for mode 1 */
145 static const uint8_t mv_pmode_table[2][5] = {
146 { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
147 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
149 static const uint8_t mv_pmode_table2[2][4] = {
150 { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV },
151 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN }
154 /** One more frame type */
157 static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
158 fps_dr[2] = { 1000, 1001 };
159 static const uint8_t pquant_table[3][32] = {
160 { /* Implicit quantizer */
161 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
162 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
164 { /* Explicit quantizer, pquantizer uniform */
165 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
166 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
168 { /* Explicit quantizer, pquantizer non-uniform */
169 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
170 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
174 /** @name VC-1 VLC tables and defines
175 * @todo TODO move this into the context
178 #define VC1_BFRACTION_VLC_BITS 7
179 static VLC vc1_bfraction_vlc;
180 #define VC1_IMODE_VLC_BITS 4
181 static VLC vc1_imode_vlc;
182 #define VC1_NORM2_VLC_BITS 3
183 static VLC vc1_norm2_vlc;
184 #define VC1_NORM6_VLC_BITS 9
185 static VLC vc1_norm6_vlc;
186 /* Could be optimized, one table only needs 8 bits */
187 #define VC1_TTMB_VLC_BITS 9 //12
188 static VLC vc1_ttmb_vlc[3];
189 #define VC1_MV_DIFF_VLC_BITS 9 //15
190 static VLC vc1_mv_diff_vlc[4];
191 #define VC1_CBPCY_P_VLC_BITS 9 //14
192 static VLC vc1_cbpcy_p_vlc[4];
193 #define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
194 static VLC vc1_4mv_block_pattern_vlc[4];
195 #define VC1_TTBLK_VLC_BITS 5
196 static VLC vc1_ttblk_vlc[3];
197 #define VC1_SUBBLKPAT_VLC_BITS 6
198 static VLC vc1_subblkpat_vlc[3];
200 static VLC vc1_ac_coeff_table[8];
204 CS_HIGH_MOT_INTRA = 0,
214 /** @name Overlap conditions for Advanced Profile */
225 * @fixme Change size wherever another size is more efficient
226 * Many members are only used for Advanced Profile
228 typedef struct VC1Context{
233 /** Simple/Main Profile sequence header */
235 int res_sm; ///< reserved, 2b
236 int res_x8; ///< reserved
237 int multires; ///< frame-level RESPIC syntax element present
238 int res_fasttx; ///< reserved, always 1
239 int res_transtab; ///< reserved, always 0
240 int rangered; ///< RANGEREDFRM (range reduction) syntax element present
242 int res_rtm_flag; ///< reserved, set to 1
243 int reserved; ///< reserved
246 /** Advanced Profile */
248 int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
249 int chromaformat; ///< 2bits, 2=4:2:0, only defined
250 int postprocflag; ///< Per-frame processing suggestion flag present
251 int broadcast; ///< TFF/RFF present
252 int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
253 int tfcntrflag; ///< TFCNTR present
254 int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
255 int extended_dmv; ///< Additional extended dmv range at P/B frame-level
256 int color_prim; ///< 8bits, chroma coordinates of the color primaries
257 int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
258 int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
259 int hrd_param_flag; ///< Presence of Hypothetical Reference
260 ///< Decoder parameters
261 int psf; ///< Progressive Segmented Frame
264 /** Sequence header data for all Profiles
265 * TODO: choose between ints, uint8_ts and monobit flags
268 int profile; ///< 2bits, Profile
269 int frmrtq_postproc; ///< 3bits,
270 int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
271 int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
272 int extended_mv; ///< Ext MV in P/B (not in Simple)
273 int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
274 int vstransform; ///< variable-size [48]x[48] transform type + info
275 int overlap; ///< overlapped transforms in use
276 int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
277 int finterpflag; ///< INTERPFRM present
280 /** Frame decoding info for all profiles */
282 uint8_t mv_mode; ///< MV coding monde
283 uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
284 int k_x; ///< Number of bits for MVs (depends on MV range)
285 int k_y; ///< Number of bits for MVs (depends on MV range)
286 int range_x, range_y; ///< MV range
287 uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
288 /** pquant parameters */
295 /** AC coding set indexes
296 * @see 8.1.1.10, p(1)10
299 int c_ac_table_index; ///< Chroma index from ACFRM element
300 int y_ac_table_index; ///< Luma index from AC2FRM element
302 int ttfrm; ///< Transform type info present at frame level
303 uint8_t ttmbf; ///< Transform type flag
304 uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
305 int codingset; ///< index of current table set from 11.8 to use for luma block decoding
306 int codingset2; ///< index of current table set from 11.8 to use for chroma block decoding
307 int pqindex; ///< raw pqindex used in coding set selection
308 int a_avail, c_avail;
309 uint8_t *mb_type_base, *mb_type[3];
312 /** Luma compensation parameters */
317 int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
318 uint8_t halfpq; ///< Uniform quant over image and qp+.5
319 uint8_t respic; ///< Frame-level flag for resized images
320 int buffer_fullness; ///< HRD info
322 * -# 0 -> [-64n 63.f] x [-32, 31.f]
323 * -# 1 -> [-128, 127.f] x [-64, 63.f]
324 * -# 2 -> [-512, 511.f] x [-128, 127.f]
325 * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
328 uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
329 VLC *cbpcy_vlc; ///< CBPCY VLC table
330 int tt_index; ///< Index for Transform Type tables
331 uint8_t* mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
332 uint8_t* direct_mb_plane; ///< bitplane for "direct" MBs
333 int mv_type_is_raw; ///< mv type mb plane is not coded
334 int dmb_is_raw; ///< direct mb plane is raw
335 int skip_is_raw; ///< skip mb plane is not coded
336 uint8_t luty[256], lutuv[256]; // lookup tables used for intensity compensation
337 int use_ic; ///< use intensity compensation in B-frames
338 int rnd; ///< rounding control
340 /** Frame decoding info for S/M profiles only */
342 uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
346 /** Frame decoding info for Advanced profile */
348 uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
349 uint8_t numpanscanwin;
351 uint8_t rptfrm, tff, rff;
354 uint16_t bottomrightx;
355 uint16_t bottomrighty;
358 int hrd_num_leaky_buckets;
359 uint8_t bit_rate_exponent;
360 uint8_t buffer_size_exponent;
361 uint8_t* acpred_plane; ///< AC prediction flags bitplane
363 uint8_t* over_flags_plane; ///< Overflags bitplane
366 uint16_t *hrd_rate, *hrd_buffer;
367 uint8_t *hrd_fullness;
368 uint8_t range_mapy_flag;
369 uint8_t range_mapuv_flag;
379 * Get unary code of limited length
380 * @fixme FIXME Slow and ugly
381 * @param gb GetBitContext
382 * @param[in] stop The bitstop value (unary code of 1's or 0's)
383 * @param[in] len Maximum length
384 * @return Unary length/index
386 static int get_prefix(GetBitContext *gb, int stop, int len)
391 for(i = 0; i < len && get_bits1(gb) != stop; i++);
393 /* int i = 0, tmp = !stop;
395 while (i != len && tmp != stop)
397 tmp = get_bits(gb, 1);
400 if (i == len && tmp != stop) return len+1;
407 UPDATE_CACHE(re, gb);
408 buf=GET_CACHE(re, gb); //Still not sure
409 if (stop) buf = ~buf;
411 log= av_log2(-buf); //FIXME: -?
413 LAST_SKIP_BITS(re, gb, log+1);
414 CLOSE_READER(re, gb);
418 LAST_SKIP_BITS(re, gb, limit);
419 CLOSE_READER(re, gb);
424 static inline int decode210(GetBitContext *gb){
430 return 2 - get_bits1(gb);
434 * Init VC-1 specific tables and VC1Context members
435 * @param v The VC1Context to initialize
438 static int vc1_init_common(VC1Context *v)
443 v->hrd_rate = v->hrd_buffer = NULL;
449 init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
450 vc1_bfraction_bits, 1, 1,
451 vc1_bfraction_codes, 1, 1, 1);
452 init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
453 vc1_norm2_bits, 1, 1,
454 vc1_norm2_codes, 1, 1, 1);
455 init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
456 vc1_norm6_bits, 1, 1,
457 vc1_norm6_codes, 2, 2, 1);
458 init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
459 vc1_imode_bits, 1, 1,
460 vc1_imode_codes, 1, 1, 1);
463 init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
464 vc1_ttmb_bits[i], 1, 1,
465 vc1_ttmb_codes[i], 2, 2, 1);
466 init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
467 vc1_ttblk_bits[i], 1, 1,
468 vc1_ttblk_codes[i], 1, 1, 1);
469 init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
470 vc1_subblkpat_bits[i], 1, 1,
471 vc1_subblkpat_codes[i], 1, 1, 1);
475 init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
476 vc1_4mv_block_pattern_bits[i], 1, 1,
477 vc1_4mv_block_pattern_codes[i], 1, 1, 1);
478 init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
479 vc1_cbpcy_p_bits[i], 1, 1,
480 vc1_cbpcy_p_codes[i], 2, 2, 1);
481 init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
482 vc1_mv_diff_bits[i], 1, 1,
483 vc1_mv_diff_codes[i], 2, 2, 1);
486 init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
487 &vc1_ac_tables[i][0][1], 8, 4,
488 &vc1_ac_tables[i][0][0], 8, 4, 1);
489 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
490 &ff_msmp4_mb_i_table[0][1], 4, 2,
491 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
496 v->mvrange = 0; /* 7.1.1.18, p80 */
501 /***********************************************************************/
503 * @defgroup bitplane VC9 Bitplane decoding
508 /** @addtogroup bitplane
521 /** @} */ //imode defines
523 /** Decode rows 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
529 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
532 for (y=0; y<height; y++){
533 if (!get_bits(gb, 1)) //rowskip
534 memset(plane, 0, width);
536 for (x=0; x<width; x++)
537 plane[x] = get_bits(gb, 1);
542 /** Decode columns by checking if they are skipped
543 * @param plane Buffer to store decoded bits
544 * @param[in] width Width of this buffer
545 * @param[in] height Height of this buffer
546 * @param[in] stride of this buffer
547 * @fixme FIXME: Optimize
549 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
552 for (x=0; x<width; x++){
553 if (!get_bits(gb, 1)) //colskip
554 for (y=0; y<height; y++)
557 for (y=0; y<height; y++)
558 plane[y*stride] = get_bits(gb, 1);
563 /** Decode a bitplane's bits
564 * @param bp Bitplane where to store the decode bits
565 * @param v VC-1 context for bit reading and logging
567 * @fixme FIXME: Optimize
569 static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
571 GetBitContext *gb = &v->s.gb;
573 int imode, x, y, code, offset;
574 uint8_t invert, *planep = data;
575 int width, height, stride;
577 width = v->s.mb_width;
578 height = v->s.mb_height;
579 stride = v->s.mb_stride;
580 invert = get_bits(gb, 1);
581 imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
587 //Data is actually read in the MB layer (same for all tests == "raw")
588 *raw_flag = 1; //invert ignored
592 if ((height * width) & 1)
594 *planep++ = get_bits(gb, 1);
598 // decode bitplane as one long line
599 for (y = offset; y < height * width; y += 2) {
600 code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
601 *planep++ = code & 1;
603 if(offset == width) {
605 planep += stride - width;
607 *planep++ = code >> 1;
609 if(offset == width) {
611 planep += stride - width;
617 if(!(height % 3) && (width % 3)) { // use 2x3 decoding
618 for(y = 0; y < height; y+= 3) {
619 for(x = width & 1; x < width; x += 2) {
620 code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
622 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
625 planep[x + 0] = (code >> 0) & 1;
626 planep[x + 1] = (code >> 1) & 1;
627 planep[x + 0 + stride] = (code >> 2) & 1;
628 planep[x + 1 + stride] = (code >> 3) & 1;
629 planep[x + 0 + stride * 2] = (code >> 4) & 1;
630 planep[x + 1 + stride * 2] = (code >> 5) & 1;
632 planep += stride * 3;
634 if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
636 planep += (height & 1) * stride;
637 for(y = height & 1; y < height; y += 2) {
638 for(x = width % 3; x < width; x += 3) {
639 code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
641 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
644 planep[x + 0] = (code >> 0) & 1;
645 planep[x + 1] = (code >> 1) & 1;
646 planep[x + 2] = (code >> 2) & 1;
647 planep[x + 0 + stride] = (code >> 3) & 1;
648 planep[x + 1 + stride] = (code >> 4) & 1;
649 planep[x + 2 + stride] = (code >> 5) & 1;
651 planep += stride * 2;
654 if(x) decode_colskip(data , x, height , stride, &v->s.gb);
655 if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
659 decode_rowskip(data, width, height, stride, &v->s.gb);
662 decode_colskip(data, width, height, stride, &v->s.gb);
667 /* Applying diff operator */
668 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
672 for (x=1; x<width; x++)
673 planep[x] ^= planep[x-1];
674 for (y=1; y<height; y++)
677 planep[0] ^= planep[-stride];
678 for (x=1; x<width; x++)
680 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
681 else planep[x] ^= planep[x-1];
688 for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
690 return (imode<<1) + invert;
693 /** @} */ //Bitplane group
695 /***********************************************************************/
696 /** VOP Dquant decoding
697 * @param v VC-1 Context
699 static int vop_dquant_decoding(VC1Context *v)
701 GetBitContext *gb = &v->s.gb;
707 pqdiff = get_bits(gb, 3);
708 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
709 else v->altpq = v->pq + pqdiff + 1;
713 v->dquantfrm = get_bits(gb, 1);
716 v->dqprofile = get_bits(gb, 2);
717 switch (v->dqprofile)
719 case DQPROFILE_SINGLE_EDGE:
720 case DQPROFILE_DOUBLE_EDGES:
721 v->dqsbedge = get_bits(gb, 2);
723 case DQPROFILE_ALL_MBS:
724 v->dqbilevel = get_bits(gb, 1);
725 default: break; //Forbidden ?
727 if (v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
729 pqdiff = get_bits(gb, 3);
730 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
731 else v->altpq = v->pq + pqdiff + 1;
738 /** Put block onto picture
740 static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
744 DSPContext *dsp = &v->s.dsp;
748 for(k = 0; k < 6; k++)
749 for(j = 0; j < 8; j++)
750 for(i = 0; i < 8; i++)
751 block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
754 ys = v->s.current_picture.linesize[0];
755 us = v->s.current_picture.linesize[1];
756 vs = v->s.current_picture.linesize[2];
759 dsp->put_pixels_clamped(block[0], Y, ys);
760 dsp->put_pixels_clamped(block[1], Y + 8, ys);
762 dsp->put_pixels_clamped(block[2], Y, ys);
763 dsp->put_pixels_clamped(block[3], Y + 8, ys);
765 if(!(v->s.flags & CODEC_FLAG_GRAY)) {
766 dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
767 dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
771 /** Do motion compensation over 1 macroblock
772 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
774 static void vc1_mc_1mv(VC1Context *v, int dir)
776 MpegEncContext *s = &v->s;
777 DSPContext *dsp = &v->s.dsp;
778 uint8_t *srcY, *srcU, *srcV;
779 int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
781 if(!v->s.last_picture.data[0])return;
783 mx = s->mv[dir][0][0];
784 my = s->mv[dir][0][1];
786 // store motion vectors for further use in B frames
787 if(s->pict_type == P_TYPE) {
788 s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
789 s->current_picture.motion_val[1][s->block_index[0]][1] = my;
791 uvmx = (mx + ((mx & 3) == 3)) >> 1;
792 uvmy = (my + ((my & 3) == 3)) >> 1;
794 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
795 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
798 srcY = s->last_picture.data[0];
799 srcU = s->last_picture.data[1];
800 srcV = s->last_picture.data[2];
802 srcY = s->next_picture.data[0];
803 srcU = s->next_picture.data[1];
804 srcV = s->next_picture.data[2];
807 src_x = s->mb_x * 16 + (mx >> 2);
808 src_y = s->mb_y * 16 + (my >> 2);
809 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
810 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
812 if(v->profile != PROFILE_ADVANCED){
813 src_x = av_clip( src_x, -16, s->mb_width * 16);
814 src_y = av_clip( src_y, -16, s->mb_height * 16);
815 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
816 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
818 src_x = av_clip( src_x, -17, s->avctx->coded_width);
819 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
820 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
821 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
824 srcY += src_y * s->linesize + src_x;
825 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
826 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
828 /* for grayscale we should not try to read from unknown area */
829 if(s->flags & CODEC_FLAG_GRAY) {
830 srcU = s->edge_emu_buffer + 18 * s->linesize;
831 srcV = s->edge_emu_buffer + 18 * s->linesize;
834 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
835 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
836 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
837 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
839 srcY -= s->mspel * (1 + s->linesize);
840 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
841 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
842 srcY = s->edge_emu_buffer;
843 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
844 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
845 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
846 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
849 /* if we deal with range reduction we need to scale source blocks */
855 for(j = 0; j < 17 + s->mspel*2; j++) {
856 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
859 src = srcU; src2 = srcV;
860 for(j = 0; j < 9; j++) {
861 for(i = 0; i < 9; i++) {
862 src[i] = ((src[i] - 128) >> 1) + 128;
863 src2[i] = ((src2[i] - 128) >> 1) + 128;
865 src += s->uvlinesize;
866 src2 += s->uvlinesize;
869 /* if we deal with intensity compensation we need to scale source blocks */
870 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
875 for(j = 0; j < 17 + s->mspel*2; j++) {
876 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
879 src = srcU; src2 = srcV;
880 for(j = 0; j < 9; j++) {
881 for(i = 0; i < 9; i++) {
882 src[i] = v->lutuv[src[i]];
883 src2[i] = v->lutuv[src2[i]];
885 src += s->uvlinesize;
886 src2 += s->uvlinesize;
889 srcY += s->mspel * (1 + s->linesize);
893 dxy = ((my & 3) << 2) | (mx & 3);
894 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
895 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
896 srcY += s->linesize * 8;
897 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
898 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
899 } else { // hpel mc - always used for luma
900 dxy = (my & 2) | ((mx & 2) >> 1);
903 dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
905 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
908 if(s->flags & CODEC_FLAG_GRAY) return;
909 /* Chroma MC always uses qpel bilinear */
910 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
914 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
915 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
917 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
918 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
922 /** Do motion compensation for 4-MV macroblock - luminance block
924 static void vc1_mc_4mv_luma(VC1Context *v, int n)
926 MpegEncContext *s = &v->s;
927 DSPContext *dsp = &v->s.dsp;
929 int dxy, mx, my, src_x, src_y;
932 if(!v->s.last_picture.data[0])return;
935 srcY = s->last_picture.data[0];
937 off = s->linesize * 4 * (n&2) + (n&1) * 8;
939 src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
940 src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
942 if(v->profile != PROFILE_ADVANCED){
943 src_x = av_clip( src_x, -16, s->mb_width * 16);
944 src_y = av_clip( src_y, -16, s->mb_height * 16);
946 src_x = av_clip( src_x, -17, s->avctx->coded_width);
947 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
950 srcY += src_y * s->linesize + src_x;
952 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
953 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
954 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
955 srcY -= s->mspel * (1 + s->linesize);
956 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
957 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
958 srcY = s->edge_emu_buffer;
959 /* if we deal with range reduction we need to scale source blocks */
965 for(j = 0; j < 9 + s->mspel*2; j++) {
966 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
970 /* if we deal with intensity compensation we need to scale source blocks */
971 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
976 for(j = 0; j < 9 + s->mspel*2; j++) {
977 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
981 srcY += s->mspel * (1 + s->linesize);
985 dxy = ((my & 3) << 2) | (mx & 3);
986 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
987 } else { // hpel mc - always used for luma
988 dxy = (my & 2) | ((mx & 2) >> 1);
990 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
992 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
996 static inline int median4(int a, int b, int c, int d)
999 if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
1000 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
1002 if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
1003 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
1008 /** Do motion compensation for 4-MV macroblock - both chroma blocks
1010 static void vc1_mc_4mv_chroma(VC1Context *v)
1012 MpegEncContext *s = &v->s;
1013 DSPContext *dsp = &v->s.dsp;
1014 uint8_t *srcU, *srcV;
1015 int uvdxy, uvmx, uvmy, uvsrc_x, uvsrc_y;
1016 int i, idx, tx = 0, ty = 0;
1017 int mvx[4], mvy[4], intra[4];
1018 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
1020 if(!v->s.last_picture.data[0])return;
1021 if(s->flags & CODEC_FLAG_GRAY) return;
1023 for(i = 0; i < 4; i++) {
1024 mvx[i] = s->mv[0][i][0];
1025 mvy[i] = s->mv[0][i][1];
1026 intra[i] = v->mb_type[0][s->block_index[i]];
1029 /* calculate chroma MV vector from four luma MVs */
1030 idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
1031 if(!idx) { // all blocks are inter
1032 tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
1033 ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
1034 } else if(count[idx] == 1) { // 3 inter blocks
1037 tx = mid_pred(mvx[1], mvx[2], mvx[3]);
1038 ty = mid_pred(mvy[1], mvy[2], mvy[3]);
1041 tx = mid_pred(mvx[0], mvx[2], mvx[3]);
1042 ty = mid_pred(mvy[0], mvy[2], mvy[3]);
1045 tx = mid_pred(mvx[0], mvx[1], mvx[3]);
1046 ty = mid_pred(mvy[0], mvy[1], mvy[3]);
1049 tx = mid_pred(mvx[0], mvx[1], mvx[2]);
1050 ty = mid_pred(mvy[0], mvy[1], mvy[2]);
1053 } else if(count[idx] == 2) {
1055 for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
1056 for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
1057 tx = (mvx[t1] + mvx[t2]) / 2;
1058 ty = (mvy[t1] + mvy[t2]) / 2;
1060 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
1061 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
1062 return; //no need to do MC for inter blocks
1065 s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
1066 s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
1067 uvmx = (tx + ((tx&3) == 3)) >> 1;
1068 uvmy = (ty + ((ty&3) == 3)) >> 1;
1070 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
1071 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
1074 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1075 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1077 if(v->profile != PROFILE_ADVANCED){
1078 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1079 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1081 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1082 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1085 srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1086 srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1087 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1088 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
1089 || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
1090 ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1,
1091 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1092 ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
1093 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
1094 srcU = s->edge_emu_buffer;
1095 srcV = s->edge_emu_buffer + 16;
1097 /* if we deal with range reduction we need to scale source blocks */
1098 if(v->rangeredfrm) {
1100 uint8_t *src, *src2;
1102 src = srcU; src2 = srcV;
1103 for(j = 0; j < 9; j++) {
1104 for(i = 0; i < 9; i++) {
1105 src[i] = ((src[i] - 128) >> 1) + 128;
1106 src2[i] = ((src2[i] - 128) >> 1) + 128;
1108 src += s->uvlinesize;
1109 src2 += s->uvlinesize;
1112 /* if we deal with intensity compensation we need to scale source blocks */
1113 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1115 uint8_t *src, *src2;
1117 src = srcU; src2 = srcV;
1118 for(j = 0; j < 9; j++) {
1119 for(i = 0; i < 9; i++) {
1120 src[i] = v->lutuv[src[i]];
1121 src2[i] = v->lutuv[src2[i]];
1123 src += s->uvlinesize;
1124 src2 += s->uvlinesize;
1129 /* Chroma MC always uses qpel bilinear */
1130 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
1134 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1135 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1137 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
1138 dsp->put_no_rnd_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
1142 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb);
1145 * Decode Simple/Main Profiles sequence header
1146 * @see Figure 7-8, p16-17
1147 * @param avctx Codec context
1148 * @param gb GetBit context initialized from Codec context extra_data
1151 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
1153 VC1Context *v = avctx->priv_data;
1155 av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
1156 v->profile = get_bits(gb, 2);
1157 if (v->profile == PROFILE_COMPLEX)
1159 av_log(avctx, AV_LOG_ERROR, "WMV3 Complex Profile is not fully supported\n");
1162 if (v->profile == PROFILE_ADVANCED)
1164 return decode_sequence_header_adv(v, gb);
1168 v->res_sm = get_bits(gb, 2); //reserved
1171 av_log(avctx, AV_LOG_ERROR,
1172 "Reserved RES_SM=%i is forbidden\n", v->res_sm);
1178 v->frmrtq_postproc = get_bits(gb, 3); //common
1179 // (bitrate-32kbps)/64kbps
1180 v->bitrtq_postproc = get_bits(gb, 5); //common
1181 v->s.loop_filter = get_bits(gb, 1); //common
1182 if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
1184 av_log(avctx, AV_LOG_ERROR,
1185 "LOOPFILTER shell not be enabled in simple profile\n");
1188 v->res_x8 = get_bits(gb, 1); //reserved
1191 av_log(avctx, AV_LOG_ERROR,
1192 "1 for reserved RES_X8 is forbidden\n");
1195 v->multires = get_bits(gb, 1);
1196 v->res_fasttx = get_bits(gb, 1);
1199 av_log(avctx, AV_LOG_ERROR,
1200 "0 for reserved RES_FASTTX is forbidden\n");
1204 v->fastuvmc = get_bits(gb, 1); //common
1205 if (!v->profile && !v->fastuvmc)
1207 av_log(avctx, AV_LOG_ERROR,
1208 "FASTUVMC unavailable in Simple Profile\n");
1211 v->extended_mv = get_bits(gb, 1); //common
1212 if (!v->profile && v->extended_mv)
1214 av_log(avctx, AV_LOG_ERROR,
1215 "Extended MVs unavailable in Simple Profile\n");
1218 v->dquant = get_bits(gb, 2); //common
1219 v->vstransform = get_bits(gb, 1); //common
1221 v->res_transtab = get_bits(gb, 1);
1222 if (v->res_transtab)
1224 av_log(avctx, AV_LOG_ERROR,
1225 "1 for reserved RES_TRANSTAB is forbidden\n");
1229 v->overlap = get_bits(gb, 1); //common
1231 v->s.resync_marker = get_bits(gb, 1);
1232 v->rangered = get_bits(gb, 1);
1233 if (v->rangered && v->profile == PROFILE_SIMPLE)
1235 av_log(avctx, AV_LOG_INFO,
1236 "RANGERED should be set to 0 in simple profile\n");
1239 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
1240 v->quantizer_mode = get_bits(gb, 2); //common
1242 v->finterpflag = get_bits(gb, 1); //common
1243 v->res_rtm_flag = get_bits(gb, 1); //reserved
1244 if (!v->res_rtm_flag)
1246 // av_log(avctx, AV_LOG_ERROR,
1247 // "0 for reserved RES_RTM_FLAG is forbidden\n");
1248 av_log(avctx, AV_LOG_ERROR,
1249 "Old WMV3 version detected, only I-frames will be decoded\n");
1252 //TODO: figure out what they mean (always 0x402F)
1253 if(!v->res_fasttx) skip_bits(gb, 16);
1254 av_log(avctx, AV_LOG_DEBUG,
1255 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1256 "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
1257 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
1258 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
1259 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
1260 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
1261 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
1262 v->dquant, v->quantizer_mode, avctx->max_b_frames
1267 static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb)
1269 v->res_rtm_flag = 1;
1270 v->level = get_bits(gb, 3);
1273 av_log(v->s.avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
1275 v->chromaformat = get_bits(gb, 2);
1276 if (v->chromaformat != 1)
1278 av_log(v->s.avctx, AV_LOG_ERROR,
1279 "Only 4:2:0 chroma format supported\n");
1284 v->frmrtq_postproc = get_bits(gb, 3); //common
1285 // (bitrate-32kbps)/64kbps
1286 v->bitrtq_postproc = get_bits(gb, 5); //common
1287 v->postprocflag = get_bits(gb, 1); //common
1289 v->s.avctx->coded_width = (get_bits(gb, 12) + 1) << 1;
1290 v->s.avctx->coded_height = (get_bits(gb, 12) + 1) << 1;
1291 v->s.avctx->width = v->s.avctx->coded_width;
1292 v->s.avctx->height = v->s.avctx->coded_height;
1293 v->broadcast = get_bits1(gb);
1294 v->interlace = get_bits1(gb);
1295 v->tfcntrflag = get_bits1(gb);
1296 v->finterpflag = get_bits1(gb);
1297 get_bits1(gb); // reserved
1299 v->s.h_edge_pos = v->s.avctx->coded_width;
1300 v->s.v_edge_pos = v->s.avctx->coded_height;
1302 av_log(v->s.avctx, AV_LOG_DEBUG,
1303 "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1304 "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
1305 "TFCTRflag=%i, FINTERPflag=%i\n",
1306 v->level, v->frmrtq_postproc, v->bitrtq_postproc,
1307 v->s.loop_filter, v->chromaformat, v->broadcast, v->interlace,
1308 v->tfcntrflag, v->finterpflag
1311 v->psf = get_bits1(gb);
1312 if(v->psf) { //PsF, 6.1.13
1313 av_log(v->s.avctx, AV_LOG_ERROR, "Progressive Segmented Frame mode: not supported (yet)\n");
1316 v->s.max_b_frames = v->s.avctx->max_b_frames = 7;
1317 if(get_bits1(gb)) { //Display Info - decoding is not affected by it
1319 av_log(v->s.avctx, AV_LOG_DEBUG, "Display extended info:\n");
1320 v->s.avctx->width = v->s.width = w = get_bits(gb, 14) + 1;
1321 v->s.avctx->height = v->s.height = h = get_bits(gb, 14) + 1;
1322 av_log(v->s.avctx, AV_LOG_DEBUG, "Display dimensions: %ix%i\n", w, h);
1324 ar = get_bits(gb, 4);
1326 v->s.avctx->sample_aspect_ratio = vc1_pixel_aspect[ar];
1328 w = get_bits(gb, 8);
1329 h = get_bits(gb, 8);
1330 v->s.avctx->sample_aspect_ratio = (AVRational){w, h};
1333 if(get_bits1(gb)){ //framerate stuff
1335 v->s.avctx->time_base.num = 32;
1336 v->s.avctx->time_base.den = get_bits(gb, 16) + 1;
1339 nr = get_bits(gb, 8);
1340 dr = get_bits(gb, 4);
1341 if(nr && nr < 8 && dr && dr < 3){
1342 v->s.avctx->time_base.num = fps_dr[dr - 1];
1343 v->s.avctx->time_base.den = fps_nr[nr - 1] * 1000;
1349 v->color_prim = get_bits(gb, 8);
1350 v->transfer_char = get_bits(gb, 8);
1351 v->matrix_coef = get_bits(gb, 8);
1355 v->hrd_param_flag = get_bits1(gb);
1356 if(v->hrd_param_flag) {
1358 v->hrd_num_leaky_buckets = get_bits(gb, 5);
1359 get_bits(gb, 4); //bitrate exponent
1360 get_bits(gb, 4); //buffer size exponent
1361 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1362 get_bits(gb, 16); //hrd_rate[n]
1363 get_bits(gb, 16); //hrd_buffer[n]
1369 static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb)
1371 VC1Context *v = avctx->priv_data;
1372 int i, blink, clentry, refdist;
1374 av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32));
1375 blink = get_bits1(gb); // broken link
1376 clentry = get_bits1(gb); // closed entry
1377 v->panscanflag = get_bits1(gb);
1378 refdist = get_bits1(gb); // refdist flag
1379 v->s.loop_filter = get_bits1(gb);
1380 v->fastuvmc = get_bits1(gb);
1381 v->extended_mv = get_bits1(gb);
1382 v->dquant = get_bits(gb, 2);
1383 v->vstransform = get_bits1(gb);
1384 v->overlap = get_bits1(gb);
1385 v->quantizer_mode = get_bits(gb, 2);
1387 if(v->hrd_param_flag){
1388 for(i = 0; i < v->hrd_num_leaky_buckets; i++) {
1389 get_bits(gb, 8); //hrd_full[n]
1394 avctx->coded_width = (get_bits(gb, 12)+1)<<1;
1395 avctx->coded_height = (get_bits(gb, 12)+1)<<1;
1398 v->extended_dmv = get_bits1(gb);
1400 av_log(avctx, AV_LOG_ERROR, "Luma scaling is not supported, expect wrong picture\n");
1401 skip_bits(gb, 3); // Y range, ignored for now
1404 av_log(avctx, AV_LOG_ERROR, "Chroma scaling is not supported, expect wrong picture\n");
1405 skip_bits(gb, 3); // UV range, ignored for now
1408 av_log(avctx, AV_LOG_DEBUG, "Entry point info:\n"
1409 "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
1410 "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
1411 "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
1412 blink, clentry, v->panscanflag, refdist, v->s.loop_filter,
1413 v->fastuvmc, v->extended_mv, v->dquant, v->vstransform, v->overlap, v->quantizer_mode);
1418 static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
1420 int pqindex, lowquant, status;
1422 if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1423 skip_bits(gb, 2); //framecnt unused
1425 if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1426 v->s.pict_type = get_bits(gb, 1);
1427 if (v->s.avctx->max_b_frames) {
1428 if (!v->s.pict_type) {
1429 if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1430 else v->s.pict_type = B_TYPE;
1431 } else v->s.pict_type = P_TYPE;
1432 } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1435 if(v->s.pict_type == B_TYPE) {
1436 v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1437 v->bfraction = vc1_bfraction_lut[v->bfraction];
1438 if(v->bfraction == 0) {
1439 v->s.pict_type = BI_TYPE;
1442 if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1443 get_bits(gb, 7); // skip buffer fullness
1446 if(v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1448 if(v->s.pict_type == P_TYPE)
1451 /* Quantizer stuff */
1452 pqindex = get_bits(gb, 5);
1453 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1454 v->pq = pquant_table[0][pqindex];
1456 v->pq = pquant_table[1][pqindex];
1459 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1460 v->pquantizer = pqindex < 9;
1461 if (v->quantizer_mode == QUANT_NON_UNIFORM)
1463 v->pqindex = pqindex;
1464 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1466 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1467 v->pquantizer = get_bits(gb, 1);
1469 if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1470 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1471 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1472 v->range_x = 1 << (v->k_x - 1);
1473 v->range_y = 1 << (v->k_y - 1);
1474 if (v->profile == PROFILE_ADVANCED)
1476 if (v->postprocflag) v->postproc = get_bits(gb, 1);
1479 if (v->multires && v->s.pict_type != B_TYPE) v->respic = get_bits(gb, 2);
1481 if(v->res_x8 && (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)){
1482 if(get_bits1(gb))return -1;
1484 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1485 // (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1487 if(v->s.pict_type == I_TYPE || v->s.pict_type == P_TYPE) v->use_ic = 0;
1489 switch(v->s.pict_type) {
1491 if (v->pq < 5) v->tt_index = 0;
1492 else if(v->pq < 13) v->tt_index = 1;
1493 else v->tt_index = 2;
1495 lowquant = (v->pq > 12) ? 0 : 1;
1496 v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1497 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1499 int scale, shift, i;
1500 v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1501 v->lumscale = get_bits(gb, 6);
1502 v->lumshift = get_bits(gb, 6);
1504 /* fill lookup tables for intensity compensation */
1507 shift = (255 - v->lumshift * 2) << 6;
1508 if(v->lumshift > 31)
1511 scale = v->lumscale + 32;
1512 if(v->lumshift > 31)
1513 shift = (v->lumshift - 64) << 6;
1515 shift = v->lumshift << 6;
1517 for(i = 0; i < 256; i++) {
1518 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1519 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1522 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1523 v->s.quarter_sample = 0;
1524 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1525 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1526 v->s.quarter_sample = 0;
1528 v->s.quarter_sample = 1;
1530 v->s.quarter_sample = 1;
1531 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));
1533 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1534 v->mv_mode2 == MV_PMODE_MIXED_MV)
1535 || v->mv_mode == MV_PMODE_MIXED_MV)
1537 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1538 if (status < 0) return -1;
1539 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1540 "Imode: %i, Invert: %i\n", status>>1, status&1);
1542 v->mv_type_is_raw = 0;
1543 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1545 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1546 if (status < 0) return -1;
1547 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1548 "Imode: %i, Invert: %i\n", status>>1, status&1);
1550 /* Hopefully this is correct for P frames */
1551 v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1552 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1556 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1557 vop_dquant_decoding(v);
1560 v->ttfrm = 0; //FIXME Is that so ?
1563 v->ttmbf = get_bits(gb, 1);
1566 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1574 if (v->pq < 5) v->tt_index = 0;
1575 else if(v->pq < 13) v->tt_index = 1;
1576 else v->tt_index = 2;
1578 lowquant = (v->pq > 12) ? 0 : 1;
1579 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1580 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1581 v->s.mspel = v->s.quarter_sample;
1583 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1584 if (status < 0) return -1;
1585 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1586 "Imode: %i, Invert: %i\n", status>>1, status&1);
1587 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1588 if (status < 0) return -1;
1589 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1590 "Imode: %i, Invert: %i\n", status>>1, status&1);
1592 v->s.mv_table_index = get_bits(gb, 2);
1593 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1597 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1598 vop_dquant_decoding(v);
1604 v->ttmbf = get_bits(gb, 1);
1607 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1617 v->c_ac_table_index = decode012(gb);
1618 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1620 v->y_ac_table_index = decode012(gb);
1623 v->s.dc_table_index = get_bits(gb, 1);
1625 if(v->s.pict_type == BI_TYPE) {
1626 v->s.pict_type = B_TYPE;
1632 static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
1634 int pqindex, lowquant;
1637 v->p_frame_skipped = 0;
1640 v->fcm = decode012(gb);
1641 if(v->fcm) return -1; // interlaced frames/fields are not implemented
1643 switch(get_prefix(gb, 0, 4)) {
1645 v->s.pict_type = P_TYPE;
1648 v->s.pict_type = B_TYPE;
1651 v->s.pict_type = I_TYPE;
1654 v->s.pict_type = BI_TYPE;
1657 v->s.pict_type = P_TYPE; // skipped pic
1658 v->p_frame_skipped = 1;
1664 if(!v->interlace || v->psf) {
1665 v->rptfrm = get_bits(gb, 2);
1667 v->tff = get_bits1(gb);
1668 v->rptfrm = get_bits1(gb);
1671 if(v->panscanflag) {
1674 v->rnd = get_bits1(gb);
1676 v->uvsamp = get_bits1(gb);
1677 if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1678 if(v->s.pict_type == B_TYPE) {
1679 v->bfraction = get_vlc2(gb, vc1_bfraction_vlc.table, VC1_BFRACTION_VLC_BITS, 1);
1680 v->bfraction = vc1_bfraction_lut[v->bfraction];
1681 if(v->bfraction == 0) {
1682 v->s.pict_type = BI_TYPE; /* XXX: should not happen here */
1685 pqindex = get_bits(gb, 5);
1686 v->pqindex = pqindex;
1687 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1688 v->pq = pquant_table[0][pqindex];
1690 v->pq = pquant_table[1][pqindex];
1693 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1694 v->pquantizer = pqindex < 9;
1695 if (v->quantizer_mode == QUANT_NON_UNIFORM)
1697 v->pqindex = pqindex;
1698 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1700 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1701 v->pquantizer = get_bits(gb, 1);
1703 if(v->s.pict_type == I_TYPE || v->s.pict_type == P_TYPE) v->use_ic = 0;
1705 switch(v->s.pict_type) {
1708 status = bitplane_decoding(v->acpred_plane, &v->acpred_is_raw, v);
1709 if (status < 0) return -1;
1710 av_log(v->s.avctx, AV_LOG_DEBUG, "ACPRED plane encoding: "
1711 "Imode: %i, Invert: %i\n", status>>1, status&1);
1712 v->condover = CONDOVER_NONE;
1713 if(v->overlap && v->pq <= 8) {
1714 v->condover = decode012(gb);
1715 if(v->condover == CONDOVER_SELECT) {
1716 status = bitplane_decoding(v->over_flags_plane, &v->overflg_is_raw, v);
1717 if (status < 0) return -1;
1718 av_log(v->s.avctx, AV_LOG_DEBUG, "CONDOVER plane encoding: "
1719 "Imode: %i, Invert: %i\n", status>>1, status&1);
1725 v->postproc = get_bits1(gb);
1726 if (v->extended_mv) v->mvrange = get_prefix(gb, 0, 3);
1727 else v->mvrange = 0;
1728 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1729 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1730 v->range_x = 1 << (v->k_x - 1);
1731 v->range_y = 1 << (v->k_y - 1);
1733 if (v->pq < 5) v->tt_index = 0;
1734 else if(v->pq < 13) v->tt_index = 1;
1735 else v->tt_index = 2;
1737 lowquant = (v->pq > 12) ? 0 : 1;
1738 v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1739 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1741 int scale, shift, i;
1742 v->mv_mode2 = mv_pmode_table2[lowquant][get_prefix(gb, 1, 3)];
1743 v->lumscale = get_bits(gb, 6);
1744 v->lumshift = get_bits(gb, 6);
1745 /* fill lookup tables for intensity compensation */
1748 shift = (255 - v->lumshift * 2) << 6;
1749 if(v->lumshift > 31)
1752 scale = v->lumscale + 32;
1753 if(v->lumshift > 31)
1754 shift = (v->lumshift - 64) << 6;
1756 shift = v->lumshift << 6;
1758 for(i = 0; i < 256; i++) {
1759 v->luty[i] = av_clip_uint8((scale * i + shift + 32) >> 6);
1760 v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
1764 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1765 v->s.quarter_sample = 0;
1766 else if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1767 if(v->mv_mode2 == MV_PMODE_1MV_HPEL || v->mv_mode2 == MV_PMODE_1MV_HPEL_BILIN)
1768 v->s.quarter_sample = 0;
1770 v->s.quarter_sample = 1;
1772 v->s.quarter_sample = 1;
1773 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));
1775 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1776 v->mv_mode2 == MV_PMODE_MIXED_MV)
1777 || v->mv_mode == MV_PMODE_MIXED_MV)
1779 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
1780 if (status < 0) return -1;
1781 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1782 "Imode: %i, Invert: %i\n", status>>1, status&1);
1784 v->mv_type_is_raw = 0;
1785 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
1787 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1788 if (status < 0) return -1;
1789 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1790 "Imode: %i, Invert: %i\n", status>>1, status&1);
1792 /* Hopefully this is correct for P frames */
1793 v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1794 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1797 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1798 vop_dquant_decoding(v);
1801 v->ttfrm = 0; //FIXME Is that so ?
1804 v->ttmbf = get_bits(gb, 1);
1807 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1816 v->postproc = get_bits1(gb);
1817 if (v->extended_mv) v->mvrange = get_prefix(gb, 0, 3);
1818 else v->mvrange = 0;
1819 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1820 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1821 v->range_x = 1 << (v->k_x - 1);
1822 v->range_y = 1 << (v->k_y - 1);
1824 if (v->pq < 5) v->tt_index = 0;
1825 else if(v->pq < 13) v->tt_index = 1;
1826 else v->tt_index = 2;
1828 lowquant = (v->pq > 12) ? 0 : 1;
1829 v->mv_mode = get_bits1(gb) ? MV_PMODE_1MV : MV_PMODE_1MV_HPEL_BILIN;
1830 v->s.quarter_sample = (v->mv_mode == MV_PMODE_1MV);
1831 v->s.mspel = v->s.quarter_sample;
1833 status = bitplane_decoding(v->direct_mb_plane, &v->dmb_is_raw, v);
1834 if (status < 0) return -1;
1835 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct Type plane encoding: "
1836 "Imode: %i, Invert: %i\n", status>>1, status&1);
1837 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
1838 if (status < 0) return -1;
1839 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1840 "Imode: %i, Invert: %i\n", status>>1, status&1);
1842 v->s.mv_table_index = get_bits(gb, 2);
1843 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1847 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1848 vop_dquant_decoding(v);
1854 v->ttmbf = get_bits(gb, 1);
1857 v->ttfrm = ttfrm_to_tt[get_bits(gb, 2)];
1867 v->c_ac_table_index = decode012(gb);
1868 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1870 v->y_ac_table_index = decode012(gb);
1873 v->s.dc_table_index = get_bits(gb, 1);
1874 if ((v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) && v->dquant) {
1875 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1876 vop_dquant_decoding(v);
1880 if(v->s.pict_type == BI_TYPE) {
1881 v->s.pict_type = B_TYPE;
1887 /***********************************************************************/
1889 * @defgroup block VC-1 Block-level functions
1890 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1896 * @brief Get macroblock-level quantizer scale
1898 #define GET_MQUANT() \
1902 if (v->dqprofile == DQPROFILE_ALL_MBS) \
1906 mquant = (get_bits(gb, 1)) ? v->altpq : v->pq; \
1910 mqdiff = get_bits(gb, 3); \
1911 if (mqdiff != 7) mquant = v->pq + mqdiff; \
1912 else mquant = get_bits(gb, 5); \
1915 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1916 edges = 1 << v->dqsbedge; \
1917 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1918 edges = (3 << v->dqsbedge) % 15; \
1919 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
1921 if((edges&1) && !s->mb_x) \
1922 mquant = v->altpq; \
1923 if((edges&2) && s->first_slice_line) \
1924 mquant = v->altpq; \
1925 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
1926 mquant = v->altpq; \
1927 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
1928 mquant = v->altpq; \
1932 * @def GET_MVDATA(_dmv_x, _dmv_y)
1933 * @brief Get MV differentials
1934 * @see MVDATA decoding from 8.3.5.2, p(1)20
1935 * @param _dmv_x Horizontal differential for decoded MV
1936 * @param _dmv_y Vertical differential for decoded MV
1938 #define GET_MVDATA(_dmv_x, _dmv_y) \
1939 index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1940 VC1_MV_DIFF_VLC_BITS, 2); \
1943 mb_has_coeffs = 1; \
1946 else mb_has_coeffs = 0; \
1948 if (!index) { _dmv_x = _dmv_y = 0; } \
1949 else if (index == 35) \
1951 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1952 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1954 else if (index == 36) \
1963 if (!s->quarter_sample && index1 == 5) val = 1; \
1965 if(size_table[index1] - val > 0) \
1966 val = get_bits(gb, size_table[index1] - val); \
1968 sign = 0 - (val&1); \
1969 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1972 if (!s->quarter_sample && index1 == 5) val = 1; \
1974 if(size_table[index1] - val > 0) \
1975 val = get_bits(gb, size_table[index1] - val); \
1977 sign = 0 - (val&1); \
1978 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1981 /** Predict and set motion vector
1983 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)
1985 int xy, wrap, off = 0;
1990 /* scale MV difference to be quad-pel */
1991 dmv_x <<= 1 - s->quarter_sample;
1992 dmv_y <<= 1 - s->quarter_sample;
1994 wrap = s->b8_stride;
1995 xy = s->block_index[n];
1998 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1999 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
2000 s->current_picture.motion_val[1][xy][0] = 0;
2001 s->current_picture.motion_val[1][xy][1] = 0;
2002 if(mv1) { /* duplicate motion data for 1-MV block */
2003 s->current_picture.motion_val[0][xy + 1][0] = 0;
2004 s->current_picture.motion_val[0][xy + 1][1] = 0;
2005 s->current_picture.motion_val[0][xy + wrap][0] = 0;
2006 s->current_picture.motion_val[0][xy + wrap][1] = 0;
2007 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
2008 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
2009 s->current_picture.motion_val[1][xy + 1][0] = 0;
2010 s->current_picture.motion_val[1][xy + 1][1] = 0;
2011 s->current_picture.motion_val[1][xy + wrap][0] = 0;
2012 s->current_picture.motion_val[1][xy + wrap][1] = 0;
2013 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
2014 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
2019 C = s->current_picture.motion_val[0][xy - 1];
2020 A = s->current_picture.motion_val[0][xy - wrap];
2022 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
2024 //in 4-MV mode different blocks have different B predictor position
2027 off = (s->mb_x > 0) ? -1 : 1;
2030 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
2039 B = s->current_picture.motion_val[0][xy - wrap + off];
2041 if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
2042 if(s->mb_width == 1) {
2046 px = mid_pred(A[0], B[0], C[0]);
2047 py = mid_pred(A[1], B[1], C[1]);
2049 } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
2055 /* Pullback MV as specified in 8.3.5.3.4 */
2058 qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
2059 qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
2060 X = (s->mb_width << 6) - 4;
2061 Y = (s->mb_height << 6) - 4;
2063 if(qx + px < -60) px = -60 - qx;
2064 if(qy + py < -60) py = -60 - qy;
2066 if(qx + px < -28) px = -28 - qx;
2067 if(qy + py < -28) py = -28 - qy;
2069 if(qx + px > X) px = X - qx;
2070 if(qy + py > Y) py = Y - qy;
2072 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2073 if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
2074 if(is_intra[xy - wrap])
2075 sum = FFABS(px) + FFABS(py);
2077 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2079 if(get_bits1(&s->gb)) {
2087 if(is_intra[xy - 1])
2088 sum = FFABS(px) + FFABS(py);
2090 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2092 if(get_bits1(&s->gb)) {
2102 /* store MV using signed modulus of MV range defined in 4.11 */
2103 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
2104 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
2105 if(mv1) { /* duplicate motion data for 1-MV block */
2106 s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
2107 s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
2108 s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
2109 s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
2110 s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
2111 s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
2115 /** Motion compensation for direct or interpolated blocks in B-frames
2117 static void vc1_interp_mc(VC1Context *v)
2119 MpegEncContext *s = &v->s;
2120 DSPContext *dsp = &v->s.dsp;
2121 uint8_t *srcY, *srcU, *srcV;
2122 int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
2124 if(!v->s.next_picture.data[0])return;
2126 mx = s->mv[1][0][0];
2127 my = s->mv[1][0][1];
2128 uvmx = (mx + ((mx & 3) == 3)) >> 1;
2129 uvmy = (my + ((my & 3) == 3)) >> 1;
2131 uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
2132 uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
2134 srcY = s->next_picture.data[0];
2135 srcU = s->next_picture.data[1];
2136 srcV = s->next_picture.data[2];
2138 src_x = s->mb_x * 16 + (mx >> 2);
2139 src_y = s->mb_y * 16 + (my >> 2);
2140 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
2141 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
2143 if(v->profile != PROFILE_ADVANCED){
2144 src_x = av_clip( src_x, -16, s->mb_width * 16);
2145 src_y = av_clip( src_y, -16, s->mb_height * 16);
2146 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
2147 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
2149 src_x = av_clip( src_x, -17, s->avctx->coded_width);
2150 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
2151 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
2152 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
2155 srcY += src_y * s->linesize + src_x;
2156 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
2157 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
2159 /* for grayscale we should not try to read from unknown area */
2160 if(s->flags & CODEC_FLAG_GRAY) {
2161 srcU = s->edge_emu_buffer + 18 * s->linesize;
2162 srcV = s->edge_emu_buffer + 18 * s->linesize;
2166 || (unsigned)src_x > s->h_edge_pos - (mx&3) - 16
2167 || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
2168 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
2170 srcY -= s->mspel * (1 + s->linesize);
2171 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
2172 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
2173 srcY = s->edge_emu_buffer;
2174 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
2175 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
2176 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
2177 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
2180 /* if we deal with range reduction we need to scale source blocks */
2181 if(v->rangeredfrm) {
2183 uint8_t *src, *src2;
2186 for(j = 0; j < 17 + s->mspel*2; j++) {
2187 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
2190 src = srcU; src2 = srcV;
2191 for(j = 0; j < 9; j++) {
2192 for(i = 0; i < 9; i++) {
2193 src[i] = ((src[i] - 128) >> 1) + 128;
2194 src2[i] = ((src2[i] - 128) >> 1) + 128;
2196 src += s->uvlinesize;
2197 src2 += s->uvlinesize;
2200 srcY += s->mspel * (1 + s->linesize);
2205 dxy = ((my & 1) << 1) | (mx & 1);
2207 dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
2209 if(s->flags & CODEC_FLAG_GRAY) return;
2210 /* Chroma MC always uses qpel blilinear */
2211 uvdxy = ((uvmy & 3) << 2) | (uvmx & 3);
2214 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
2215 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
2218 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2222 #if B_FRACTION_DEN==256
2226 return 2 * ((value * n + 255) >> 9);
2227 return (value * n + 128) >> 8;
2230 n -= B_FRACTION_DEN;
2232 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2233 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2237 /** Reconstruct motion vector for B-frame and do motion compensation
2239 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
2242 v->mv_mode2 = v->mv_mode;
2243 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2248 if(v->use_ic) v->mv_mode = v->mv_mode2;
2251 if(mode == BMV_TYPE_INTERPOLATED) {
2254 if(v->use_ic) v->mv_mode = v->mv_mode2;
2258 if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
2259 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2260 if(v->use_ic) v->mv_mode = v->mv_mode2;
2263 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
2265 MpegEncContext *s = &v->s;
2266 int xy, wrap, off = 0;
2271 const uint8_t *is_intra = v->mb_type[0];
2275 /* scale MV difference to be quad-pel */
2276 dmv_x[0] <<= 1 - s->quarter_sample;
2277 dmv_y[0] <<= 1 - s->quarter_sample;
2278 dmv_x[1] <<= 1 - s->quarter_sample;
2279 dmv_y[1] <<= 1 - s->quarter_sample;
2281 wrap = s->b8_stride;
2282 xy = s->block_index[0];
2285 s->current_picture.motion_val[0][xy][0] =
2286 s->current_picture.motion_val[0][xy][1] =
2287 s->current_picture.motion_val[1][xy][0] =
2288 s->current_picture.motion_val[1][xy][1] = 0;
2291 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2292 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2293 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2294 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2296 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2297 s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
2298 s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2299 s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
2300 s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
2302 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2303 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2304 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2305 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2309 if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2310 C = s->current_picture.motion_val[0][xy - 2];
2311 A = s->current_picture.motion_val[0][xy - wrap*2];
2312 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2313 B = s->current_picture.motion_val[0][xy - wrap*2 + off];
2315 if(!s->mb_x) C[0] = C[1] = 0;
2316 if(!s->first_slice_line) { // predictor A is not out of bounds
2317 if(s->mb_width == 1) {
2321 px = mid_pred(A[0], B[0], C[0]);
2322 py = mid_pred(A[1], B[1], C[1]);
2324 } else if(s->mb_x) { // predictor C is not out of bounds
2330 /* Pullback MV as specified in 8.3.5.3.4 */
2333 if(v->profile < PROFILE_ADVANCED) {
2334 qx = (s->mb_x << 5);
2335 qy = (s->mb_y << 5);
2336 X = (s->mb_width << 5) - 4;
2337 Y = (s->mb_height << 5) - 4;
2338 if(qx + px < -28) px = -28 - qx;
2339 if(qy + py < -28) py = -28 - qy;
2340 if(qx + px > X) px = X - qx;
2341 if(qy + py > Y) py = Y - qy;
2343 qx = (s->mb_x << 6);
2344 qy = (s->mb_y << 6);
2345 X = (s->mb_width << 6) - 4;
2346 Y = (s->mb_height << 6) - 4;
2347 if(qx + px < -60) px = -60 - qx;
2348 if(qy + py < -60) py = -60 - qy;
2349 if(qx + px > X) px = X - qx;
2350 if(qy + py > Y) py = Y - qy;
2353 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2354 if(0 && !s->first_slice_line && s->mb_x) {
2355 if(is_intra[xy - wrap])
2356 sum = FFABS(px) + FFABS(py);
2358 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2360 if(get_bits1(&s->gb)) {
2368 if(is_intra[xy - 2])
2369 sum = FFABS(px) + FFABS(py);
2371 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2373 if(get_bits1(&s->gb)) {
2383 /* store MV using signed modulus of MV range defined in 4.11 */
2384 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2385 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2387 if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2388 C = s->current_picture.motion_val[1][xy - 2];
2389 A = s->current_picture.motion_val[1][xy - wrap*2];
2390 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2391 B = s->current_picture.motion_val[1][xy - wrap*2 + off];
2393 if(!s->mb_x) C[0] = C[1] = 0;
2394 if(!s->first_slice_line) { // predictor A is not out of bounds
2395 if(s->mb_width == 1) {
2399 px = mid_pred(A[0], B[0], C[0]);
2400 py = mid_pred(A[1], B[1], C[1]);
2402 } else if(s->mb_x) { // predictor C is not out of bounds
2408 /* Pullback MV as specified in 8.3.5.3.4 */
2411 if(v->profile < PROFILE_ADVANCED) {
2412 qx = (s->mb_x << 5);
2413 qy = (s->mb_y << 5);
2414 X = (s->mb_width << 5) - 4;
2415 Y = (s->mb_height << 5) - 4;
2416 if(qx + px < -28) px = -28 - qx;
2417 if(qy + py < -28) py = -28 - qy;
2418 if(qx + px > X) px = X - qx;
2419 if(qy + py > Y) py = Y - qy;
2421 qx = (s->mb_x << 6);
2422 qy = (s->mb_y << 6);
2423 X = (s->mb_width << 6) - 4;
2424 Y = (s->mb_height << 6) - 4;
2425 if(qx + px < -60) px = -60 - qx;
2426 if(qy + py < -60) py = -60 - qy;
2427 if(qx + px > X) px = X - qx;
2428 if(qy + py > Y) py = Y - qy;
2431 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2432 if(0 && !s->first_slice_line && s->mb_x) {
2433 if(is_intra[xy - wrap])
2434 sum = FFABS(px) + FFABS(py);
2436 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2438 if(get_bits1(&s->gb)) {
2446 if(is_intra[xy - 2])
2447 sum = FFABS(px) + FFABS(py);
2449 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2451 if(get_bits1(&s->gb)) {
2461 /* store MV using signed modulus of MV range defined in 4.11 */
2463 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2464 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2466 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2467 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2468 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2469 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2472 /** Get predicted DC value for I-frames only
2473 * prediction dir: left=0, top=1
2474 * @param s MpegEncContext
2475 * @param[in] n block index in the current MB
2476 * @param dc_val_ptr Pointer to DC predictor
2477 * @param dir_ptr Prediction direction for use in AC prediction
2479 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2480 int16_t **dc_val_ptr, int *dir_ptr)
2482 int a, b, c, wrap, pred, scale;
2484 static const uint16_t dcpred[32] = {
2485 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2486 114, 102, 93, 85, 79, 73, 68, 64,
2487 60, 57, 54, 51, 49, 47, 45, 43,
2488 41, 39, 38, 37, 35, 34, 33
2491 /* find prediction - wmv3_dc_scale always used here in fact */
2492 if (n < 4) scale = s->y_dc_scale;
2493 else scale = s->c_dc_scale;
2495 wrap = s->block_wrap[n];
2496 dc_val= s->dc_val[0] + s->block_index[n];
2502 b = dc_val[ - 1 - wrap];
2503 a = dc_val[ - wrap];
2505 if (pq < 9 || !overlap)
2507 /* Set outer values */
2508 if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
2509 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
2513 /* Set outer values */
2514 if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
2515 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
2518 if (abs(a - b) <= abs(b - c)) {
2526 /* update predictor */
2527 *dc_val_ptr = &dc_val[0];
2532 /** Get predicted DC value
2533 * prediction dir: left=0, top=1
2534 * @param s MpegEncContext
2535 * @param[in] n block index in the current MB
2536 * @param dc_val_ptr Pointer to DC predictor
2537 * @param dir_ptr Prediction direction for use in AC prediction
2539 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2540 int a_avail, int c_avail,
2541 int16_t **dc_val_ptr, int *dir_ptr)
2543 int a, b, c, wrap, pred, scale;
2545 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2548 /* find prediction - wmv3_dc_scale always used here in fact */
2549 if (n < 4) scale = s->y_dc_scale;
2550 else scale = s->c_dc_scale;
2552 wrap = s->block_wrap[n];
2553 dc_val= s->dc_val[0] + s->block_index[n];
2559 b = dc_val[ - 1 - wrap];
2560 a = dc_val[ - wrap];
2561 /* scale predictors if needed */
2562 q1 = s->current_picture.qscale_table[mb_pos];
2563 if(c_avail && (n!= 1 && n!=3)) {
2564 q2 = s->current_picture.qscale_table[mb_pos - 1];
2566 c = (c * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2568 if(a_avail && (n!= 2 && n!=3)) {
2569 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2571 a = (a * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2573 if(a_avail && c_avail && (n!=3)) {
2576 if(n != 2) off -= s->mb_stride;
2577 q2 = s->current_picture.qscale_table[off];
2579 b = (b * s->y_dc_scale_table[q2] * vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2582 if(a_avail && c_avail) {
2583 if(abs(a - b) <= abs(b - c)) {
2590 } else if(a_avail) {
2593 } else if(c_avail) {
2601 /* update predictor */
2602 *dc_val_ptr = &dc_val[0];
2608 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2609 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2613 static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
2615 int xy, wrap, pred, a, b, c;
2617 xy = s->block_index[n];
2618 wrap = s->b8_stride;
2623 a = s->coded_block[xy - 1 ];
2624 b = s->coded_block[xy - 1 - wrap];
2625 c = s->coded_block[xy - wrap];
2634 *coded_block_ptr = &s->coded_block[xy];
2640 * Decode one AC coefficient
2641 * @param v The VC1 context
2642 * @param last Last coefficient
2643 * @param skip How much zero coefficients to skip
2644 * @param value Decoded AC coefficient value
2647 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
2649 GetBitContext *gb = &v->s.gb;
2650 int index, escape, run = 0, level = 0, lst = 0;
2652 index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2653 if (index != vc1_ac_sizes[codingset] - 1) {
2654 run = vc1_index_decode_table[codingset][index][0];
2655 level = vc1_index_decode_table[codingset][index][1];
2656 lst = index >= vc1_last_decode_table[codingset];
2660 escape = decode210(gb);
2662 index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2663 run = vc1_index_decode_table[codingset][index][0];
2664 level = vc1_index_decode_table[codingset][index][1];
2665 lst = index >= vc1_last_decode_table[codingset];
2668 level += vc1_last_delta_level_table[codingset][run];
2670 level += vc1_delta_level_table[codingset][run];
2673 run += vc1_last_delta_run_table[codingset][level] + 1;
2675 run += vc1_delta_run_table[codingset][level] + 1;
2681 lst = get_bits(gb, 1);
2682 if(v->s.esc3_level_length == 0) {
2683 if(v->pq < 8 || v->dquantfrm) { // table 59
2684 v->s.esc3_level_length = get_bits(gb, 3);
2685 if(!v->s.esc3_level_length)
2686 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2688 v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
2690 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2692 run = get_bits(gb, v->s.esc3_run_length);
2693 sign = get_bits(gb, 1);
2694 level = get_bits(gb, v->s.esc3_level_length);
2705 /** Decode intra block in intra frames - should be faster than decode_intra_block
2706 * @param v VC1Context
2707 * @param block block to decode
2708 * @param coded are AC coeffs present or not
2709 * @param codingset set of VLC to decode data
2711 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
2713 GetBitContext *gb = &v->s.gb;
2714 MpegEncContext *s = &v->s;
2715 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2718 int16_t *ac_val, *ac_val2;
2721 /* Get DC differential */
2723 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2725 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2728 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2733 if (dcdiff == 119 /* ESC index value */)
2735 /* TODO: Optimize */
2736 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2737 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2738 else dcdiff = get_bits(gb, 8);
2743 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2744 else if (v->pq == 2)
2745 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2747 if (get_bits(gb, 1))
2752 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2755 /* Store the quantized DC coeff, used for prediction */
2757 block[0] = dcdiff * s->y_dc_scale;
2759 block[0] = dcdiff * s->c_dc_scale;
2772 int last = 0, skip, value;
2773 const int8_t *zz_table;
2777 scale = v->pq * 2 + v->halfpq;
2781 zz_table = vc1_horizontal_zz;
2783 zz_table = vc1_vertical_zz;
2785 zz_table = vc1_normal_zz;
2787 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2789 if(dc_pred_dir) //left
2792 ac_val -= 16 * s->block_wrap[n];
2795 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2799 block[zz_table[i++]] = value;
2802 /* apply AC prediction if needed */
2804 if(dc_pred_dir) { //left
2805 for(k = 1; k < 8; k++)
2806 block[k << 3] += ac_val[k];
2808 for(k = 1; k < 8; k++)
2809 block[k] += ac_val[k + 8];
2812 /* save AC coeffs for further prediction */
2813 for(k = 1; k < 8; k++) {
2814 ac_val2[k] = block[k << 3];
2815 ac_val2[k + 8] = block[k];
2818 /* scale AC coeffs */
2819 for(k = 1; k < 64; k++)
2823 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2826 if(s->ac_pred) i = 63;
2832 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2835 scale = v->pq * 2 + v->halfpq;
2836 memset(ac_val2, 0, 16 * 2);
2837 if(dc_pred_dir) {//left
2840 memcpy(ac_val2, ac_val, 8 * 2);
2842 ac_val -= 16 * s->block_wrap[n];
2844 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2847 /* apply AC prediction if needed */
2849 if(dc_pred_dir) { //left
2850 for(k = 1; k < 8; k++) {
2851 block[k << 3] = ac_val[k] * scale;
2852 if(!v->pquantizer && block[k << 3])
2853 block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
2856 for(k = 1; k < 8; k++) {
2857 block[k] = ac_val[k + 8] * scale;
2858 if(!v->pquantizer && block[k])
2859 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2865 s->block_last_index[n] = i;
2870 /** Decode intra block in intra frames - should be faster than decode_intra_block
2871 * @param v VC1Context
2872 * @param block block to decode
2873 * @param coded are AC coeffs present or not
2874 * @param codingset set of VLC to decode data
2876 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
2878 GetBitContext *gb = &v->s.gb;
2879 MpegEncContext *s = &v->s;
2880 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2883 int16_t *ac_val, *ac_val2;
2885 int a_avail = v->a_avail, c_avail = v->c_avail;
2886 int use_pred = s->ac_pred;
2889 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2891 /* Get DC differential */
2893 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2895 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2898 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2903 if (dcdiff == 119 /* ESC index value */)
2905 /* TODO: Optimize */
2906 if (mquant == 1) dcdiff = get_bits(gb, 10);
2907 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2908 else dcdiff = get_bits(gb, 8);
2913 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
2914 else if (mquant == 2)
2915 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
2917 if (get_bits(gb, 1))
2922 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2925 /* Store the quantized DC coeff, used for prediction */
2927 block[0] = dcdiff * s->y_dc_scale;
2929 block[0] = dcdiff * s->c_dc_scale;
2938 /* check if AC is needed at all and adjust direction if needed */
2939 if(!a_avail) dc_pred_dir = 1;
2940 if(!c_avail) dc_pred_dir = 0;
2941 if(!a_avail && !c_avail) use_pred = 0;
2942 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2945 scale = mquant * 2 + v->halfpq;
2947 if(dc_pred_dir) //left
2950 ac_val -= 16 * s->block_wrap[n];
2952 q1 = s->current_picture.qscale_table[mb_pos];
2953 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
2954 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2955 if(dc_pred_dir && n==1) q2 = q1;
2956 if(!dc_pred_dir && n==2) q2 = q1;
2960 int last = 0, skip, value;
2961 const int8_t *zz_table;
2966 zz_table = vc1_horizontal_zz;
2968 zz_table = vc1_vertical_zz;
2970 zz_table = vc1_normal_zz;
2973 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2977 block[zz_table[i++]] = value;
2980 /* apply AC prediction if needed */
2982 /* scale predictors if needed*/
2984 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2985 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2987 if(dc_pred_dir) { //left
2988 for(k = 1; k < 8; k++)
2989 block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2991 for(k = 1; k < 8; k++)
2992 block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2995 if(dc_pred_dir) { //left
2996 for(k = 1; k < 8; k++)
2997 block[k << 3] += ac_val[k];
2999 for(k = 1; k < 8; k++)
3000 block[k] += ac_val[k + 8];
3004 /* save AC coeffs for further prediction */
3005 for(k = 1; k < 8; k++) {
3006 ac_val2[k] = block[k << 3];
3007 ac_val2[k + 8] = block[k];
3010 /* scale AC coeffs */
3011 for(k = 1; k < 64; k++)
3015 block[k] += (block[k] < 0) ? -mquant : mquant;
3018 if(use_pred) i = 63;
3019 } else { // no AC coeffs
3022 memset(ac_val2, 0, 16 * 2);
3023 if(dc_pred_dir) {//left
3025 memcpy(ac_val2, ac_val, 8 * 2);
3027 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3028 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3029 for(k = 1; k < 8; k++)
3030 ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3035 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3037 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3038 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3039 for(k = 1; k < 8; k++)
3040 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3045 /* apply AC prediction if needed */
3047 if(dc_pred_dir) { //left
3048 for(k = 1; k < 8; k++) {
3049 block[k << 3] = ac_val2[k] * scale;
3050 if(!v->pquantizer && block[k << 3])
3051 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
3054 for(k = 1; k < 8; k++) {
3055 block[k] = ac_val2[k + 8] * scale;
3056 if(!v->pquantizer && block[k])
3057 block[k] += (block[k] < 0) ? -mquant : mquant;
3063 s->block_last_index[n] = i;
3068 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
3069 * @param v VC1Context
3070 * @param block block to decode
3071 * @param coded are AC coeffs present or not
3072 * @param mquant block quantizer
3073 * @param codingset set of VLC to decode data
3075 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
3077 GetBitContext *gb = &v->s.gb;
3078 MpegEncContext *s = &v->s;
3079 int dc_pred_dir = 0; /* Direction of the DC prediction used */
3082 int16_t *ac_val, *ac_val2;
3084 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3085 int a_avail = v->a_avail, c_avail = v->c_avail;
3086 int use_pred = s->ac_pred;
3090 /* XXX: Guard against dumb values of mquant */
3091 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
3093 /* Set DC scale - y and c use the same */
3094 s->y_dc_scale = s->y_dc_scale_table[mquant];
3095 s->c_dc_scale = s->c_dc_scale_table[mquant];
3097 /* Get DC differential */
3099 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3101 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3104 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3109 if (dcdiff == 119 /* ESC index value */)
3111 /* TODO: Optimize */
3112 if (mquant == 1) dcdiff = get_bits(gb, 10);
3113 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3114 else dcdiff = get_bits(gb, 8);
3119 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
3120 else if (mquant == 2)
3121 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
3123 if (get_bits(gb, 1))
3128 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3131 /* Store the quantized DC coeff, used for prediction */
3134 block[0] = dcdiff * s->y_dc_scale;
3136 block[0] = dcdiff * s->c_dc_scale;
3145 /* check if AC is needed at all and adjust direction if needed */
3146 if(!a_avail) dc_pred_dir = 1;
3147 if(!c_avail) dc_pred_dir = 0;
3148 if(!a_avail && !c_avail) use_pred = 0;
3149 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3152 scale = mquant * 2 + v->halfpq;
3154 if(dc_pred_dir) //left
3157 ac_val -= 16 * s->block_wrap[n];
3159 q1 = s->current_picture.qscale_table[mb_pos];
3160 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
3161 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3162 if(dc_pred_dir && n==1) q2 = q1;
3163 if(!dc_pred_dir && n==2) q2 = q1;
3167 int last = 0, skip, value;
3168 const int8_t *zz_table;
3171 zz_table = vc1_simple_progressive_8x8_zz;
3174 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3178 block[zz_table[i++]] = value;
3181 /* apply AC prediction if needed */
3183 /* scale predictors if needed*/
3185 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3186 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3188 if(dc_pred_dir) { //left
3189 for(k = 1; k < 8; k++)
3190 block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3192 for(k = 1; k < 8; k++)
3193 block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3196 if(dc_pred_dir) { //left
3197 for(k = 1; k < 8; k++)
3198 block[k << 3] += ac_val[k];
3200 for(k = 1; k < 8; k++)
3201 block[k] += ac_val[k + 8];
3205 /* save AC coeffs for further prediction */
3206 for(k = 1; k < 8; k++) {
3207 ac_val2[k] = block[k << 3];
3208 ac_val2[k + 8] = block[k];
3211 /* scale AC coeffs */
3212 for(k = 1; k < 64; k++)
3216 block[k] += (block[k] < 0) ? -mquant : mquant;
3219 if(use_pred) i = 63;
3220 } else { // no AC coeffs
3223 memset(ac_val2, 0, 16 * 2);
3224 if(dc_pred_dir) {//left
3226 memcpy(ac_val2, ac_val, 8 * 2);
3228 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3229 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3230 for(k = 1; k < 8; k++)
3231 ac_val2[k] = (ac_val2[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3236 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3238 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3239 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3240 for(k = 1; k < 8; k++)
3241 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3246 /* apply AC prediction if needed */
3248 if(dc_pred_dir) { //left
3249 for(k = 1; k < 8; k++) {
3250 block[k << 3] = ac_val2[k] * scale;
3251 if(!v->pquantizer && block[k << 3])
3252 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
3255 for(k = 1; k < 8; k++) {
3256 block[k] = ac_val2[k + 8] * scale;
3257 if(!v->pquantizer && block[k])
3258 block[k] += (block[k] < 0) ? -mquant : mquant;
3264 s->block_last_index[n] = i;
3271 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
3273 MpegEncContext *s = &v->s;
3274 GetBitContext *gb = &s->gb;
3277 int scale, off, idx, last, skip, value;
3278 int ttblk = ttmb & 7;
3281 ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
3283 if(ttblk == TT_4X4) {
3284 subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3286 if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
3287 subblkpat = decode012(gb);
3288 if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
3289 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
3290 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
3292 scale = 2 * mquant + v->halfpq;
3294 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3295 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3296 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3299 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3300 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3308 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3312 idx = vc1_simple_progressive_8x8_zz[i++];
3313 block[idx] = value * scale;
3315 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3317 s->dsp.vc1_inv_trans_8x8(block);
3320 for(j = 0; j < 4; j++) {
3321 last = subblkpat & (1 << (3 - j));
3323 off = (j & 1) * 4 + (j & 2) * 16;
3325 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3329 idx = vc1_simple_progressive_4x4_zz[i++];
3330 block[idx + off] = value * scale;
3332 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3334 if(!(subblkpat & (1 << (3 - j))))
3335 s->dsp.vc1_inv_trans_4x4(block, j);
3339 for(j = 0; j < 2; j++) {
3340 last = subblkpat & (1 << (1 - j));
3344 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3348 if(v->profile < PROFILE_ADVANCED)
3349 idx = vc1_simple_progressive_8x4_zz[i++];
3351 idx = vc1_adv_progressive_8x4_zz[i++];
3352 block[idx + off] = value * scale;
3354 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3356 if(!(subblkpat & (1 << (1 - j))))
3357 s->dsp.vc1_inv_trans_8x4(block, j);
3361 for(j = 0; j < 2; j++) {
3362 last = subblkpat & (1 << (1 - j));
3366 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3370 if(v->profile < PROFILE_ADVANCED)
3371 idx = vc1_simple_progressive_4x8_zz[i++];
3373 idx = vc1_adv_progressive_4x8_zz[i++];
3374 block[idx + off] = value * scale;
3376 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3378 if(!(subblkpat & (1 << (1 - j))))
3379 s->dsp.vc1_inv_trans_4x8(block, j);
3387 /** Decode one P-frame MB (in Simple/Main profile)
3389 static int vc1_decode_p_mb(VC1Context *v)
3391 MpegEncContext *s = &v->s;
3392 GetBitContext *gb = &s->gb;
3394 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3395 int cbp; /* cbp decoding stuff */
3396 int mqdiff, mquant; /* MB quantization */
3397 int ttmb = v->ttfrm; /* MB Transform type */
3400 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3401 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3402 int mb_has_coeffs = 1; /* last_flag */
3403 int dmv_x, dmv_y; /* Differential MV components */
3404 int index, index1; /* LUT indices */
3405 int val, sign; /* temp values */
3406 int first_block = 1;
3408 int skipped, fourmv;
3410 mquant = v->pq; /* Loosy initialization */
3412 if (v->mv_type_is_raw)
3413 fourmv = get_bits1(gb);
3415 fourmv = v->mv_type_mb_plane[mb_pos];
3417 skipped = get_bits1(gb);
3419 skipped = v->s.mbskip_table[mb_pos];
3421 s->dsp.clear_blocks(s->block[0]);
3423 if (!fourmv) /* 1MV mode */
3427 GET_MVDATA(dmv_x, dmv_y);
3430 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3431 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3433 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3434 vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3436 /* FIXME Set DC val for inter block ? */
3437 if (s->mb_intra && !mb_has_coeffs)
3440 s->ac_pred = get_bits(gb, 1);
3443 else if (mb_has_coeffs)
3445 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
3446 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3454 s->current_picture.qscale_table[mb_pos] = mquant;
3456 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3457 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
3458 VC1_TTMB_VLC_BITS, 2);
3459 if(!s->mb_intra) vc1_mc_1mv(v, 0);
3463 s->dc_val[0][s->block_index[i]] = 0;
3465 val = ((cbp >> (5 - i)) & 1);
3466 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3467 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3469 /* check if prediction blocks A and C are available */
3470 v->a_avail = v->c_avail = 0;
3471 if(i == 2 || i == 3 || !s->first_slice_line)
3472 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3473 if(i == 1 || i == 3 || s->mb_x)
3474 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3476 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3477 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3478 s->dsp.vc1_inv_trans_8x8(s->block[i]);
3479 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3480 for(j = 0; j < 64; j++) s->block[i][j] += 128;
3481 if(!v->res_fasttx && v->res_x8) for(j = 0; j < 64; j++) s->block[i][j] += 16;
3482 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3483 if(v->pq >= 9 && v->overlap) {
3485 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3487 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3490 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3491 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3493 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3494 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3501 for(i = 0; i < 6; i++) {
3502 v->mb_type[0][s->block_index[i]] = 0;
3503 s->dc_val[0][s->block_index[i]] = 0;
3505 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3506 s->current_picture.qscale_table[mb_pos] = 0;
3507 vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3514 if (!skipped /* unskipped MB */)
3516 int intra_count = 0, coded_inter = 0;
3517 int is_intra[6], is_coded[6];
3519 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3522 val = ((cbp >> (5 - i)) & 1);
3523 s->dc_val[0][s->block_index[i]] = 0;
3530 GET_MVDATA(dmv_x, dmv_y);
3532 vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3533 if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
3534 intra_count += s->mb_intra;
3535 is_intra[i] = s->mb_intra;
3536 is_coded[i] = mb_has_coeffs;
3539 is_intra[i] = (intra_count >= 3);
3542 if(i == 4) vc1_mc_4mv_chroma(v);
3543 v->mb_type[0][s->block_index[i]] = is_intra[i];
3544 if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
3546 // if there are no coded blocks then don't do anything more
3547 if(!intra_count && !coded_inter) return 0;
3550 s->current_picture.qscale_table[mb_pos] = mquant;
3551 /* test if block is intra and has pred */
3556 if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3557 || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
3562 if(intrapred)s->ac_pred = get_bits(gb, 1);
3563 else s->ac_pred = 0;
3565 if (!v->ttmbf && coded_inter)
3566 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3570 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3571 s->mb_intra = is_intra[i];
3573 /* check if prediction blocks A and C are available */
3574 v->a_avail = v->c_avail = 0;
3575 if(i == 2 || i == 3 || !s->first_slice_line)
3576 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3577 if(i == 1 || i == 3 || s->mb_x)
3578 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3580 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
3581 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3582 s->dsp.vc1_inv_trans_8x8(s->block[i]);
3583 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3584 for(j = 0; j < 64; j++) s->block[i][j] += 128;
3585 if(!v->res_fasttx && v->res_x8) for(j = 0; j < 64; j++) s->block[i][j] += 16;
3586 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3587 if(v->pq >= 9 && v->overlap) {
3589 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3591 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3593 } else if(is_coded[i]) {
3594 status = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3595 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3597 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3598 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3606 s->current_picture.qscale_table[mb_pos] = 0;
3607 for (i=0; i<6; i++) {
3608 v->mb_type[0][s->block_index[i]] = 0;
3609 s->dc_val[0][s->block_index[i]] = 0;
3613 vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
3614 vc1_mc_4mv_luma(v, i);
3616 vc1_mc_4mv_chroma(v);
3617 s->current_picture.qscale_table[mb_pos] = 0;
3622 /* Should never happen */
3626 /** Decode one B-frame MB (in Main profile)
3628 static void vc1_decode_b_mb(VC1Context *v)
3630 MpegEncContext *s = &v->s;
3631 GetBitContext *gb = &s->gb;
3633 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3634 int cbp = 0; /* cbp decoding stuff */
3635 int mqdiff, mquant; /* MB quantization */
3636 int ttmb = v->ttfrm; /* MB Transform type */
3638 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
3639 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3640 int mb_has_coeffs = 0; /* last_flag */
3641 int index, index1; /* LUT indices */
3642 int val, sign; /* temp values */
3643 int first_block = 1;
3645 int skipped, direct;
3646 int dmv_x[2], dmv_y[2];
3647 int bmvtype = BMV_TYPE_BACKWARD;
3649 mquant = v->pq; /* Loosy initialization */
3653 direct = get_bits1(gb);
3655 direct = v->direct_mb_plane[mb_pos];
3657 skipped = get_bits1(gb);
3659 skipped = v->s.mbskip_table[mb_pos];
3661 s->dsp.clear_blocks(s->block[0]);
3662 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
3663 for(i = 0; i < 6; i++) {
3664 v->mb_type[0][s->block_index[i]] = 0;
3665 s->dc_val[0][s->block_index[i]] = 0;
3667 s->current_picture.qscale_table[mb_pos] = 0;
3671 GET_MVDATA(dmv_x[0], dmv_y[0]);
3672 dmv_x[1] = dmv_x[0];
3673 dmv_y[1] = dmv_y[0];
3675 if(skipped || !s->mb_intra) {
3676 bmvtype = decode012(gb);
3679 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
3682 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
3685 bmvtype = BMV_TYPE_INTERPOLATED;
3686 dmv_x[0] = dmv_y[0] = 0;
3690 for(i = 0; i < 6; i++)
3691 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3694 if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
3695 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3696 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3700 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3704 s->current_picture.qscale_table[mb_pos] = mquant;
3706 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3707 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
3708 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3709 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3711 if(!mb_has_coeffs && !s->mb_intra) {
3712 /* no coded blocks - effectively skipped */
3713 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3714 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3717 if(s->mb_intra && !mb_has_coeffs) {
3719 s->current_picture.qscale_table[mb_pos] = mquant;
3720 s->ac_pred = get_bits1(gb);
3722 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3724 if(bmvtype == BMV_TYPE_INTERPOLATED) {
3725 GET_MVDATA(dmv_x[0], dmv_y[0]);
3726 if(!mb_has_coeffs) {
3727 /* interpolated skipped block */
3728 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3729 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3733 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
3735 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
3738 s->ac_pred = get_bits1(gb);
3739 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3741 s->current_picture.qscale_table[mb_pos] = mquant;
3742 if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3743 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3749 s->dc_val[0][s->block_index[i]] = 0;
3751 val = ((cbp >> (5 - i)) & 1);
3752 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3753 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3755 /* check if prediction blocks A and C are available */
3756 v->a_avail = v->c_avail = 0;
3757 if(i == 2 || i == 3 || !s->first_slice_line)
3758 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3759 if(i == 1 || i == 3 || s->mb_x)
3760 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3762 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
3763 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3764 s->dsp.vc1_inv_trans_8x8(s->block[i]);
3765 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
3766 for(j = 0; j < 64; j++) s->block[i][j] += 128;
3767 s->dsp.put_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
3769 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block);
3770 if(!v->ttmbf && ttmb < 8) ttmb = -1;
3772 if((i<4) || !(s->flags & CODEC_FLAG_GRAY))
3773 s->dsp.add_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
3778 /** Decode blocks of I-frame
3780 static void vc1_decode_i_blocks(VC1Context *v)
3783 MpegEncContext *s = &v->s;
3788 /* select codingmode used for VLC tables selection */
3789 switch(v->y_ac_table_index){
3791 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3794 v->codingset = CS_HIGH_MOT_INTRA;
3797 v->codingset = CS_MID_RATE_INTRA;
3801 switch(v->c_ac_table_index){
3803 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3806 v->codingset2 = CS_HIGH_MOT_INTER;
3809 v->codingset2 = CS_MID_RATE_INTER;
3813 /* Set DC scale - y and c use the same */
3814 s->y_dc_scale = s->y_dc_scale_table[v->pq];
3815 s->c_dc_scale = s->c_dc_scale_table[v->pq];
3818 s->mb_x = s->mb_y = 0;
3820 s->first_slice_line = 1;
3821 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3822 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3823 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3824 ff_init_block_index(s);
3825 ff_update_block_index(s);
3826 s->dsp.clear_blocks(s->block[0]);
3827 mb_pos = s->mb_x + s->mb_y * s->mb_width;
3828 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3829 s->current_picture.qscale_table[mb_pos] = v->pq;
3830 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3831 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3833 // do actual MB decoding and displaying
3834 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3835 v->s.ac_pred = get_bits(&v->s.gb, 1);
3837 for(k = 0; k < 6; k++) {
3838 val = ((cbp >> (5 - k)) & 1);
3841 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3845 cbp |= val << (5 - k);
3847 vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
3849 s->dsp.vc1_inv_trans_8x8(s->block[k]);
3850 if(!v->res_fasttx && !v->res_x8) for(j = 0; j < 64; j++) s->block[k][j] -= 16;
3851 if(v->pq >= 9 && v->overlap) {
3852 for(j = 0; j < 64; j++) s->block[k][j] += 128;
3856 vc1_put_block(v, s->block);
3857 if(v->pq >= 9 && v->overlap) {
3859 s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3860 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3861 if(!(s->flags & CODEC_FLAG_GRAY)) {
3862 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3863 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3866 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3867 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3868 if(!s->first_slice_line) {
3869 s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3870 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
3871 if(!(s->flags & CODEC_FLAG_GRAY)) {
3872 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
3873 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
3876 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3877 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3880 if(get_bits_count(&s->gb) > v->bits) {
3881 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
3885 ff_draw_horiz_band(s, s->mb_y * 16, 16);
3886 s->first_slice_line = 0;
3890 /** Decode blocks of I-frame for advanced profile
3892 static void vc1_decode_i_blocks_adv(VC1Context *v)
3895 MpegEncContext *s = &v->s;
3902 GetBitContext *gb = &s->gb;
3904 /* select codingmode used for VLC tables selection */
3905 switch(v->y_ac_table_index){
3907 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
3910 v->codingset = CS_HIGH_MOT_INTRA;
3913 v->codingset = CS_MID_RATE_INTRA;
3917 switch(v->c_ac_table_index){
3919 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
3922 v->codingset2 = CS_HIGH_MOT_INTER;
3925 v->codingset2 = CS_MID_RATE_INTER;
3930 s->mb_x = s->mb_y = 0;
3932 s->first_slice_line = 1;
3933 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
3934 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
3935 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
3936 ff_init_block_index(s);
3937 ff_update_block_index(s);
3938 s->dsp.clear_blocks(s->block[0]);
3939 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3940 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3941 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3942 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3944 // do actual MB decoding and displaying
3945 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
3946 if(v->acpred_is_raw)
3947 v->s.ac_pred = get_bits(&v->s.gb, 1);
3949 v->s.ac_pred = v->acpred_plane[mb_pos];
3951 if(v->condover == CONDOVER_SELECT) {
3952 if(v->overflg_is_raw)
3953 overlap = get_bits(&v->s.gb, 1);
3955 overlap = v->over_flags_plane[mb_pos];
3957 overlap = (v->condover == CONDOVER_ALL);
3961 s->current_picture.qscale_table[mb_pos] = mquant;
3962 /* Set DC scale - y and c use the same */
3963 s->y_dc_scale = s->y_dc_scale_table[mquant];
3964 s->c_dc_scale = s->c_dc_scale_table[mquant];
3966 for(k = 0; k < 6; k++) {
3967 val = ((cbp >> (5 - k)) & 1);
3970 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
3974 cbp |= val << (5 - k);
3976 v->a_avail = !s->first_slice_line || (k==2 || k==3);
3977 v->c_avail = !!s->mb_x || (k==1 || k==3);
3979 vc1_decode_i_block_adv(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2, mquant);
3981 s->dsp.vc1_inv_trans_8x8(s->block[k]);
3982 for(j = 0; j < 64; j++) s->block[k][j] += 128;
3985 vc1_put_block(v, s->block);
3988 s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
3989 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
3990 if(!(s->flags & CODEC_FLAG_GRAY)) {
3991 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
3992 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
3995 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
3996 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
3997 if(!s->first_slice_line) {
3998 s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
3999 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4000 if(!(s->flags & CODEC_FLAG_GRAY)) {
4001 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4002 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4005 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4006 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4009 if(get_bits_count(&s->gb) > v->bits) {
4010 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
4014 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4015 s->first_slice_line = 0;
4019 static void vc1_decode_p_blocks(VC1Context *v)
4021 MpegEncContext *s = &v->s;
4023 /* select codingmode used for VLC tables selection */
4024 switch(v->c_ac_table_index){
4026 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4029 v->codingset = CS_HIGH_MOT_INTRA;
4032 v->codingset = CS_MID_RATE_INTRA;
4036 switch(v->c_ac_table_index){
4038 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4041 v->codingset2 = CS_HIGH_MOT_INTER;
4044 v->codingset2 = CS_MID_RATE_INTER;
4048 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
4049 s->first_slice_line = 1;
4050 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
4051 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
4052 ff_init_block_index(s);
4053 ff_update_block_index(s);
4054 s->dsp.clear_blocks(s->block[0]);
4057 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4058 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);
4062 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4063 s->first_slice_line = 0;
4067 static void vc1_decode_b_blocks(VC1Context *v)
4069 MpegEncContext *s = &v->s;
4071 /* select codingmode used for VLC tables selection */
4072 switch(v->c_ac_table_index){
4074 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4077 v->codingset = CS_HIGH_MOT_INTRA;
4080 v->codingset = CS_MID_RATE_INTRA;
4084 switch(v->c_ac_table_index){
4086 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4089 v->codingset2 = CS_HIGH_MOT_INTER;
4092 v->codingset2 = CS_MID_RATE_INTER;
4096 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
4097 s->first_slice_line = 1;
4098 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
4099 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
4100 ff_init_block_index(s);
4101 ff_update_block_index(s);
4102 s->dsp.clear_blocks(s->block[0]);
4105 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4106 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);
4110 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4111 s->first_slice_line = 0;
4115 static void vc1_decode_skip_blocks(VC1Context *v)
4117 MpegEncContext *s = &v->s;
4119 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
4120 s->first_slice_line = 1;
4121 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
4123 ff_init_block_index(s);
4124 ff_update_block_index(s);
4125 memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4126 memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4127 memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4128 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4129 s->first_slice_line = 0;
4131 s->pict_type = P_TYPE;
4134 static void vc1_decode_blocks(VC1Context *v)
4137 v->s.esc3_level_length = 0;
4139 switch(v->s.pict_type) {
4141 if(v->profile == PROFILE_ADVANCED)
4142 vc1_decode_i_blocks_adv(v);
4144 vc1_decode_i_blocks(v);
4147 if(v->p_frame_skipped)
4148 vc1_decode_skip_blocks(v);
4150 vc1_decode_p_blocks(v);
4154 if(v->profile == PROFILE_ADVANCED)
4155 vc1_decode_i_blocks_adv(v);
4157 vc1_decode_i_blocks(v);
4159 vc1_decode_b_blocks(v);
4164 /** Find VC-1 marker in buffer
4165 * @return position where next marker starts or end of buffer if no marker found
4167 static av_always_inline uint8_t* find_next_marker(uint8_t *src, uint8_t *end)
4169 uint32_t mrk = 0xFFFFFFFF;
4171 if(end-src < 4) return end;
4173 mrk = (mrk << 8) | *src++;
4180 static av_always_inline int vc1_unescape_buffer(uint8_t *src, int size, uint8_t *dst)
4185 for(dsize = 0; dsize < size; dsize++) *dst++ = *src++;
4188 for(i = 0; i < size; i++, src++) {
4189 if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) {
4190 dst[dsize++] = src[1];
4194 dst[dsize++] = *src;
4199 /** Initialize a VC1/WMV3 decoder
4200 * @todo TODO: Handle VC-1 IDUs (Transport level?)
4201 * @todo TODO: Decypher remaining bits in extra_data
4203 static int vc1_decode_init(AVCodecContext *avctx)
4205 VC1Context *v = avctx->priv_data;
4206 MpegEncContext *s = &v->s;
4209 if (!avctx->extradata_size || !avctx->extradata) return -1;
4210 if (!(avctx->flags & CODEC_FLAG_GRAY))
4211 avctx->pix_fmt = PIX_FMT_YUV420P;
4213 avctx->pix_fmt = PIX_FMT_GRAY8;
4215 avctx->flags |= CODEC_FLAG_EMU_EDGE;
4216 v->s.flags |= CODEC_FLAG_EMU_EDGE;
4218 if(ff_h263_decode_init(avctx) < 0)
4220 if (vc1_init_common(v) < 0) return -1;
4222 avctx->coded_width = avctx->width;
4223 avctx->coded_height = avctx->height;
4224 if (avctx->codec_id == CODEC_ID_WMV3)
4228 // looks like WMV3 has a sequence header stored in the extradata
4229 // advanced sequence header may be before the first frame
4230 // the last byte of the extradata is a version number, 1 for the
4231 // samples we can decode
4233 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
4235 if (decode_sequence_header(avctx, &gb) < 0)
4238 count = avctx->extradata_size*8 - get_bits_count(&gb);
4241 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
4242 count, get_bits(&gb, count));
4246 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
4248 } else { // VC1/WVC1
4249 uint8_t *start = avctx->extradata, *end = avctx->extradata + avctx->extradata_size;
4250 uint8_t *next; int size, buf2_size;
4251 uint8_t *buf2 = NULL;
4252 int seq_inited = 0, ep_inited = 0;
4254 if(avctx->extradata_size < 16) {
4255 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
4259 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
4260 if(start[0]) start++; // in WVC1 extradata first byte is its size
4262 for(; next < end; start = next){
4263 next = find_next_marker(start + 4, end);
4264 size = next - start - 4;
4265 if(size <= 0) continue;
4266 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
4267 init_get_bits(&gb, buf2, buf2_size * 8);
4268 switch(AV_RB32(start)){
4269 case VC1_CODE_SEQHDR:
4270 if(decode_sequence_header(avctx, &gb) < 0){
4276 case VC1_CODE_ENTRYPOINT:
4277 if(decode_entry_point(avctx, &gb) < 0){
4286 if(!seq_inited || !ep_inited){
4287 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
4291 avctx->has_b_frames= !!(avctx->max_b_frames);
4292 s->low_delay = !avctx->has_b_frames;
4294 s->mb_width = (avctx->coded_width+15)>>4;
4295 s->mb_height = (avctx->coded_height+15)>>4;
4297 /* Allocate mb bitplanes */
4298 v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
4299 v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
4300 v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
4301 v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
4303 /* allocate block type info in that way so it could be used with s->block_index[] */
4304 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
4305 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
4306 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
4307 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
4309 /* Init coded blocks info */
4310 if (v->profile == PROFILE_ADVANCED)
4312 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
4314 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
4322 /** Decode a VC1/WMV3 frame
4323 * @todo TODO: Handle VC-1 IDUs (Transport level?)
4325 static int vc1_decode_frame(AVCodecContext *avctx,
4326 void *data, int *data_size,
4327 uint8_t *buf, int buf_size)
4329 VC1Context *v = avctx->priv_data;
4330 MpegEncContext *s = &v->s;
4331 AVFrame *pict = data;
4332 uint8_t *buf2 = NULL;
4334 /* no supplementary picture */
4335 if (buf_size == 0) {
4336 /* special case for last picture */
4337 if (s->low_delay==0 && s->next_picture_ptr) {
4338 *pict= *(AVFrame*)s->next_picture_ptr;
4339 s->next_picture_ptr= NULL;
4341 *data_size = sizeof(AVFrame);
4347 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
4348 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
4349 int i= ff_find_unused_picture(s, 0);
4350 s->current_picture_ptr= &s->picture[i];
4353 //for advanced profile we may need to parse and unescape data
4354 if (avctx->codec_id == CODEC_ID_VC1) {
4356 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
4358 if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
4359 uint8_t *start, *end, *next;
4363 for(start = buf, end = buf + buf_size; next < end; start = next){
4364 next = find_next_marker(start + 4, end);
4365 size = next - start - 4;
4366 if(size <= 0) continue;
4367 switch(AV_RB32(start)){
4368 case VC1_CODE_FRAME:
4369 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
4371 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
4372 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
4373 init_get_bits(&s->gb, buf2, buf_size2*8);
4374 decode_entry_point(avctx, &s->gb);
4376 case VC1_CODE_SLICE:
4377 av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
4382 }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
4385 divider = find_next_marker(buf, buf + buf_size);
4386 if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
4387 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
4391 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
4393 av_free(buf2);return -1;
4395 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
4397 init_get_bits(&s->gb, buf2, buf_size2*8);
4399 init_get_bits(&s->gb, buf, buf_size*8);
4400 // do parse frame header
4401 if(v->profile < PROFILE_ADVANCED) {
4402 if(vc1_parse_frame_header(v, &s->gb) == -1) {
4407 if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
4413 if(s->pict_type != I_TYPE && !v->res_rtm_flag){
4419 s->current_picture.pict_type= s->pict_type;
4420 s->current_picture.key_frame= s->pict_type == I_TYPE;
4422 /* skip B-frames if we don't have reference frames */
4423 if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)){
4425 return -1;//buf_size;
4427 /* skip b frames if we are in a hurry */
4428 if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
4429 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
4430 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
4431 || avctx->skip_frame >= AVDISCARD_ALL) {
4435 /* skip everything if we are in a hurry>=5 */
4436 if(avctx->hurry_up>=5) {
4438 return -1;//buf_size;
4441 if(s->next_p_frame_damaged){
4442 if(s->pict_type==B_TYPE)
4445 s->next_p_frame_damaged=0;
4448 if(MPV_frame_start(s, avctx) < 0) {
4453 ff_er_frame_start(s);
4455 v->bits = buf_size * 8;
4456 vc1_decode_blocks(v);
4457 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4458 // if(get_bits_count(&s->gb) > buf_size * 8)
4464 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
4465 assert(s->current_picture.pict_type == s->pict_type);
4466 if (s->pict_type == B_TYPE || s->low_delay) {
4467 *pict= *(AVFrame*)s->current_picture_ptr;
4468 } else if (s->last_picture_ptr != NULL) {
4469 *pict= *(AVFrame*)s->last_picture_ptr;
4472 if(s->last_picture_ptr || s->low_delay){
4473 *data_size = sizeof(AVFrame);
4474 ff_print_debug_info(s, pict);
4477 /* Return the Picture timestamp as the frame number */
4478 /* we substract 1 because it is added on utils.c */
4479 avctx->frame_number = s->picture_number - 1;
4486 /** Close a VC1/WMV3 decoder
4487 * @warning Initial try at using MpegEncContext stuff
4489 static int vc1_decode_end(AVCodecContext *avctx)
4491 VC1Context *v = avctx->priv_data;
4493 av_freep(&v->hrd_rate);
4494 av_freep(&v->hrd_buffer);
4495 MPV_common_end(&v->s);
4496 av_freep(&v->mv_type_mb_plane);
4497 av_freep(&v->direct_mb_plane);
4498 av_freep(&v->acpred_plane);
4499 av_freep(&v->over_flags_plane);
4500 av_freep(&v->mb_type_base);
4505 AVCodec vc1_decoder = {
4518 AVCodec wmv3_decoder = {