CAVS decoder by (Stefan Gehrer stefan.gehrer gmx.de)
[libav.git] / libavcodec / vc1.c
CommitLineData
21aa398f 1/*
10b9c374 2 * VC-1 and WMV3 decoder
be3492ec
KS
3 * Copyright (c) 2006 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
21aa398f
AB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
5509bffa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21aa398f
AB
19 *
20 */
21
22/**
10b9c374
KS
23 * @file vc1.c
24 * VC-1 and WMV3 decoder
21aa398f 25 *
21aa398f
AB
26 */
27#include "common.h"
28#include "dsputil.h"
29#include "avcodec.h"
30#include "mpegvideo.h"
10b9c374 31#include "vc1data.h"
be3492ec 32#include "vc1acdata.h"
0d33db8a 33
9da235c8
MN
34#undef NDEBUG
35#include <assert.h>
36
0c040aac 37extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
38extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
0d33db8a 39extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
40#define MB_INTRA_VLC_BITS 9
41extern VLC ff_msmp4_mb_i_vlc;
be3492ec 42extern const uint16_t ff_msmp4_mb_i_table[64][2];
0d33db8a 43#define DC_VLC_BITS 9
be3492ec 44#define AC_VLC_BITS 9
0d33db8a 45static const uint16_t table_mb_intra[64][2];
21aa398f 46
21aa398f 47
2ce151f8 48/** Available Profiles */
49//@{
be3492ec
KS
50enum Profile {
51 PROFILE_SIMPLE,
52 PROFILE_MAIN,
53 PROFILE_COMPLEX, ///< TODO: WMV9 specific
54 PROFILE_ADVANCED
55};
2ce151f8 56//@}
21aa398f 57
2ce151f8 58/** Sequence quantizer mode */
59//@{
be3492ec
KS
60enum QuantMode {
61 QUANT_FRAME_IMPLICIT, ///< Implicitly specified at frame level
62 QUANT_FRAME_EXPLICIT, ///< Explicitly specified at frame level
63 QUANT_NON_UNIFORM, ///< Non-uniform quant used for all frames
64 QUANT_UNIFORM ///< Uniform quant used for all frames
65};
2ce151f8 66//@}
21aa398f 67
2ce151f8 68/** Where quant can be changed */
69//@{
be3492ec
KS
70enum DQProfile {
71 DQPROFILE_FOUR_EDGES,
72 DQPROFILE_DOUBLE_EDGES,
73 DQPROFILE_SINGLE_EDGE,
74 DQPROFILE_ALL_MBS
75};
2ce151f8 76//@}
21aa398f 77
2ce151f8 78/** @name Where quant can be changed
79 */
80//@{
be3492ec
KS
81enum DQSingleEdge {
82 DQSINGLE_BEDGE_LEFT,
83 DQSINGLE_BEDGE_TOP,
84 DQSINGLE_BEDGE_RIGHT,
85 DQSINGLE_BEDGE_BOTTOM
86};
2ce151f8 87//@}
21aa398f 88
2ce151f8 89/** Which pair of edges is quantized with ALTPQUANT */
90//@{
be3492ec
KS
91enum DQDoubleEdge {
92 DQDOUBLE_BEDGE_TOPLEFT,
93 DQDOUBLE_BEDGE_TOPRIGHT,
94 DQDOUBLE_BEDGE_BOTTOMRIGHT,
95 DQDOUBLE_BEDGE_BOTTOMLEFT
96};
2ce151f8 97//@}
21aa398f 98
2ce151f8 99/** MV modes for P frames */
100//@{
be3492ec
KS
101enum MVModes {
102 MV_PMODE_1MV_HPEL_BILIN,
103 MV_PMODE_1MV,
104 MV_PMODE_1MV_HPEL,
105 MV_PMODE_MIXED_MV,
106 MV_PMODE_INTENSITY_COMP
107};
2ce151f8 108//@}
21aa398f 109
2ce151f8 110/** @name MV types for B frames */
111//@{
be3492ec
KS
112enum BMVTypes {
113 BMV_TYPE_BACKWARD,
114 BMV_TYPE_FORWARD,
115 BMV_TYPE_INTERPOLATED = 3 //XXX: ??
116};
117//@}
118
119/** @name Block types for P/B frames */
120//@{
121enum TransformTypes {
122 TT_8X8,
123 TT_8X4_BOTTOM,
124 TT_8X4_TOP,
125 TT_8X4, //Both halves
126 TT_4X8_RIGHT,
127 TT_4X8_LEFT,
128 TT_4X8, //Both halves
129 TT_4X4
130};
2ce151f8 131//@}
e5540b3f 132
be3492ec
KS
133/** Table for conversion between TTBLK and TTMB */
134static const int ttblk_to_tt[3][8] = {
135 { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
136 { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
137 { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
138};
139
2ce151f8 140/** MV P mode - the 5th element is only used for mode 1 */
21aa398f 141static const uint8_t mv_pmode_table[2][5] = {
be3492ec
KS
142 { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
143 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
21aa398f
AB
144};
145
2ce151f8 146/** One more frame type */
21aa398f
AB
147#define BI_TYPE 7
148
21aa398f
AB
149static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
150 fps_dr[2] = { 1000, 1001 };
151static const uint8_t pquant_table[3][32] = {
152 { /* Implicit quantizer */
153 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
154 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
155 },
156 { /* Explicit quantizer, pquantizer uniform */
157 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
158 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
159 },
160 { /* Explicit quantizer, pquantizer non-uniform */
161 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
162 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
163 }
164};
165
10b9c374 166/** @name VC-1 VLC tables and defines
2ce151f8 167 * @todo TODO move this into the context
168 */
169//@{
10b9c374
KS
170#define VC1_BFRACTION_VLC_BITS 7
171static VLC vc1_bfraction_vlc;
172#define VC1_IMODE_VLC_BITS 4
173static VLC vc1_imode_vlc;
174#define VC1_NORM2_VLC_BITS 3
175static VLC vc1_norm2_vlc;
176#define VC1_NORM6_VLC_BITS 9
177static VLC vc1_norm6_vlc;
21aa398f 178/* Could be optimized, one table only needs 8 bits */
10b9c374
KS
179#define VC1_TTMB_VLC_BITS 9 //12
180static VLC vc1_ttmb_vlc[3];
181#define VC1_MV_DIFF_VLC_BITS 9 //15
182static VLC vc1_mv_diff_vlc[4];
183#define VC1_CBPCY_P_VLC_BITS 9 //14
184static VLC vc1_cbpcy_p_vlc[4];
185#define VC1_4MV_BLOCK_PATTERN_VLC_BITS 6
186static VLC vc1_4mv_block_pattern_vlc[4];
187#define VC1_TTBLK_VLC_BITS 5
188static VLC vc1_ttblk_vlc[3];
189#define VC1_SUBBLKPAT_VLC_BITS 6
190static VLC vc1_subblkpat_vlc[3];
be3492ec
KS
191
192static VLC vc1_ac_coeff_table[8];
2ce151f8 193//@}
e5540b3f 194
be3492ec
KS
195enum CodingSet {
196 CS_HIGH_MOT_INTRA = 0,
197 CS_HIGH_MOT_INTER,
198 CS_LOW_MOT_INTRA,
199 CS_LOW_MOT_INTER,
200 CS_MID_RATE_INTRA,
201 CS_MID_RATE_INTER,
202 CS_HIGH_RATE_INTRA,
203 CS_HIGH_RATE_INTER
204};
205
10b9c374 206/** The VC1 Context
8da75fb2 207 * @fixme Change size wherever another size is more efficient
208 * Many members are only used for Advanced Profile
209 */
10b9c374 210typedef struct VC1Context{
2ce151f8 211 MpegEncContext s;
212
be3492ec
KS
213 int bits;
214
2ce151f8 215 /** Simple/Main Profile sequence header */
216 //@{
217 int res_sm; ///< reserved, 2b
218 int res_x8; ///< reserved
219 int multires; ///< frame-level RESPIC syntax element present
220 int res_fasttx; ///< reserved, always 1
221 int res_transtab; ///< reserved, always 0
222 int rangered; ///< RANGEREDFRM (range reduction) syntax element present
223 ///< at frame level
224 int res_rtm_flag; ///< reserved, set to 1
225 int reserved; ///< reserved
226 //@}
21aa398f 227
2ce151f8 228 /** Advanced Profile */
229 //@{
230 int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
231 int chromaformat; ///< 2bits, 2=4:2:0, only defined
232 int postprocflag; ///< Per-frame processing suggestion flag present
233 int broadcast; ///< TFF/RFF present
234 int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
235 int tfcntrflag; ///< TFCNTR present
236 int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
237 int extended_dmv; ///< Additional extended dmv range at P/B frame-level
238 int color_prim; ///< 8bits, chroma coordinates of the color primaries
239 int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
240 int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
241 int hrd_param_flag; ///< Presence of Hypothetical Reference
242 ///< Decoder parameters
243 //@}
21aa398f 244
2ce151f8 245 /** Sequence header data for all Profiles
246 * TODO: choose between ints, uint8_ts and monobit flags
247 */
248 //@{
249 int profile; ///< 2bits, Profile
115329f1 250 int frmrtq_postproc; ///< 3bits,
2ce151f8 251 int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
252 int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
253 int extended_mv; ///< Ext MV in P/B (not in Simple)
254 int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
255 int vstransform; ///< variable-size [48]x[48] transform type + info
256 int overlap; ///< overlapped transforms in use
257 int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
258 int finterpflag; ///< INTERPFRM present
259 //@}
260
261 /** Frame decoding info for all profiles */
262 //@{
263 uint8_t mv_mode; ///< MV coding monde
264 uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
7cc84d24 265 int k_x; ///< Number of bits for MVs (depends on MV range)
266 int k_y; ///< Number of bits for MVs (depends on MV range)
be3492ec 267 int range_x, range_y; ///< MV range
2ce151f8 268 uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
269 /** pquant parameters */
270 //@{
271 uint8_t dquantfrm;
272 uint8_t dqprofile;
273 uint8_t dqsbedge;
274 uint8_t dqbilevel;
275 //@}
8da75fb2 276 /** AC coding set indexes
277 * @see 8.1.1.10, p(1)10
278 */
279 //@{
280 int c_ac_table_index; ///< Chroma index from ACFRM element
281 int y_ac_table_index; ///< Luma index from AC2FRM element
282 //@}
2ce151f8 283 int ttfrm; ///< Transform type info present at frame level
7cc84d24 284 uint8_t ttmbf; ///< Transform type flag
285 int ttmb; ///< Transform type
286 uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
be3492ec
KS
287 int codingset; ///< index of current table set from 11.8 to use for luma block decoding
288 int codingset2; ///< index of current table set from 11.8 to use for chroma block decoding
289 int pqindex; ///< raw pqindex used in coding set selection
f26c2ef5 290 int a_avail, c_avail;
be3492ec
KS
291
292
2ce151f8 293 /** Luma compensation parameters */
294 //@{
295 uint8_t lumscale;
296 uint8_t lumshift;
297 //@}
298 int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
299 uint8_t halfpq; ///< Uniform quant over image and qp+.5
300 uint8_t respic; ///< Frame-level flag for resized images
301 int buffer_fullness; ///< HRD info
302 /** Ranges:
303 * -# 0 -> [-64n 63.f] x [-32, 31.f]
304 * -# 1 -> [-128, 127.f] x [-64, 63.f]
305 * -# 2 -> [-512, 511.f] x [-128, 127.f]
306 * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
307 */
308 uint8_t mvrange;
bf2bc926 309 uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
7cc84d24 310 VLC *cbpcy_vlc; ///< CBPCY VLC table
311 int tt_index; ///< Index for Transform Type tables
87dfe848 312 uint8_t* mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
87dfe848
KS
313// BitPlane direct_mb_plane; ///< bitplane for "direct" MBs
314 int mv_type_is_raw; ///< mv type mb plane is not coded
315 int skip_is_raw; ///< skip mb plane is not coded
2ce151f8 316
317 /** Frame decoding info for S/M profiles only */
318 //@{
319 uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
320 uint8_t interpfrm;
321 //@}
21aa398f 322
2ce151f8 323 /** Frame decoding info for Advanced profile */
324 //@{
325 uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
326 uint8_t numpanscanwin;
327 uint8_t tfcntr;
328 uint8_t rptfrm, tff, rff;
bf2bc926 329 uint16_t topleftx;
330 uint16_t toplefty;
331 uint16_t bottomrightx;
332 uint16_t bottomrighty;
2ce151f8 333 uint8_t uvsamp;
334 uint8_t postproc;
335 int hrd_num_leaky_buckets;
336 uint8_t bit_rate_exponent;
337 uint8_t buffer_size_exponent;
87dfe848
KS
338// BitPlane ac_pred_plane; ///< AC prediction flags bitplane
339// BitPlane over_flags_plane; ///< Overflags bitplane
2ce151f8 340 uint8_t condover;
341 uint16_t *hrd_rate, *hrd_buffer;
1cf9f514
IK
342 uint8_t *hrd_fullness;
343 uint8_t range_mapy_flag;
344 uint8_t range_mapuv_flag;
345 uint8_t range_mapy;
346 uint8_t range_mapuv;
2ce151f8 347 //@}
10b9c374 348} VC1Context;
21aa398f 349
2ce151f8 350/**
351 * Get unary code of limited length
352 * @fixme FIXME Slow and ugly
353 * @param gb GetBitContext
354 * @param[in] stop The bitstop value (unary code of 1's or 0's)
355 * @param[in] len Maximum length
356 * @return Unary length/index
357 */
21aa398f
AB
358static int get_prefix(GetBitContext *gb, int stop, int len)
359{
e5540b3f 360#if 1
be3492ec
KS
361 int i;
362
363 for(i = 0; i < len && get_bits1(gb) != stop; i++);
364 return i;
365/* int i = 0, tmp = !stop;
21aa398f
AB
366
367 while (i != len && tmp != stop)
368 {
369 tmp = get_bits(gb, 1);
370 i++;
371 }
bf2bc926 372 if (i == len && tmp != stop) return len+1;
be3492ec 373 return i;*/
e5540b3f 374#else
375 unsigned int buf;
376 int log;
377
378 OPEN_READER(re, gb);
379 UPDATE_CACHE(re, gb);
380 buf=GET_CACHE(re, gb); //Still not sure
381 if (stop) buf = ~buf;
42cc17f9 382
e5540b3f 383 log= av_log2(-buf); //FIXME: -?
384 if (log < limit){
385 LAST_SKIP_BITS(re, gb, log+1);
386 CLOSE_READER(re, gb);
387 return log;
388 }
42cc17f9 389
e5540b3f 390 LAST_SKIP_BITS(re, gb, limit);
391 CLOSE_READER(re, gb);
392 return limit;
393#endif
394}
395
be3492ec
KS
396static inline int decode210(GetBitContext *gb){
397 int n;
398 n = get_bits1(gb);
399 if (n == 1)
400 return 0;
401 else
402 return 2 - get_bits1(gb);
403}
404
2ce151f8 405/**
10b9c374
KS
406 * Init VC-1 specific tables and VC1Context members
407 * @param v The VC1Context to initialize
2ce151f8 408 * @return Status
409 */
10b9c374 410static int vc1_init_common(VC1Context *v)
21aa398f
AB
411{
412 static int done = 0;
bf2bc926 413 int i = 0;
21aa398f 414
21aa398f 415 v->hrd_rate = v->hrd_buffer = NULL;
e5540b3f 416
417 /* VLC tables */
21aa398f
AB
418 if(!done)
419 {
420 done = 1;
be3492ec 421 init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
10b9c374
KS
422 vc1_bfraction_bits, 1, 1,
423 vc1_bfraction_codes, 1, 1, 1);
be3492ec 424 init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
10b9c374
KS
425 vc1_norm2_bits, 1, 1,
426 vc1_norm2_codes, 1, 1, 1);
be3492ec 427 init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
10b9c374
KS
428 vc1_norm6_bits, 1, 1,
429 vc1_norm6_codes, 2, 2, 1);
be3492ec 430 init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
10b9c374
KS
431 vc1_imode_bits, 1, 1,
432 vc1_imode_codes, 1, 1, 1);
e5540b3f 433 for (i=0; i<3; i++)
434 {
be3492ec 435 init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
10b9c374
KS
436 vc1_ttmb_bits[i], 1, 1,
437 vc1_ttmb_codes[i], 2, 2, 1);
be3492ec 438 init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
10b9c374
KS
439 vc1_ttblk_bits[i], 1, 1,
440 vc1_ttblk_codes[i], 1, 1, 1);
be3492ec 441 init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
10b9c374
KS
442 vc1_subblkpat_bits[i], 1, 1,
443 vc1_subblkpat_codes[i], 1, 1, 1);
e5540b3f 444 }
445 for(i=0; i<4; i++)
21aa398f 446 {
be3492ec 447 init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
10b9c374
KS
448 vc1_4mv_block_pattern_bits[i], 1, 1,
449 vc1_4mv_block_pattern_codes[i], 1, 1, 1);
be3492ec 450 init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
10b9c374
KS
451 vc1_cbpcy_p_bits[i], 1, 1,
452 vc1_cbpcy_p_codes[i], 2, 2, 1);
be3492ec 453 init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
10b9c374
KS
454 vc1_mv_diff_bits[i], 1, 1,
455 vc1_mv_diff_codes[i], 2, 2, 1);
21aa398f 456 }
be3492ec
KS
457 for(i=0; i<8; i++)
458 init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
459 &vc1_ac_tables[i][0][1], 8, 4,
460 &vc1_ac_tables[i][0][0], 8, 4, 1);
461 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
462 &ff_msmp4_mb_i_table[0][1], 4, 2,
463 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
21aa398f
AB
464 }
465
e5540b3f 466 /* Other defaults */
467 v->pq = -1;
468 v->mvrange = 0; /* 7.1.1.18, p80 */
469
21aa398f
AB
470 return 0;
471}
472
be3492ec 473/***********************************************************************/
2ce151f8 474/**
be3492ec
KS
475 * @defgroup bitplane VC9 Bitplane decoding
476 * @see 8.7, p56
477 * @{
478 */
479
480/** @addtogroup bitplane
481 * Imode types
482 * @{
483 */
484enum Imode {
485 IMODE_RAW,
486 IMODE_NORM2,
487 IMODE_DIFF2,
488 IMODE_NORM6,
489 IMODE_DIFF6,
490 IMODE_ROWSKIP,
491 IMODE_COLSKIP
492};
493/** @} */ //imode defines
494
be3492ec
KS
495/** Decode rows by checking if they are skipped
496 * @param plane Buffer to store decoded bits
497 * @param[in] width Width of this buffer
498 * @param[in] height Height of this buffer
499 * @param[in] stride of this buffer
500 */
501static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
502 int x, y;
21aa398f 503
be3492ec
KS
504 for (y=0; y<height; y++){
505 if (!get_bits(gb, 1)) //rowskip
506 memset(plane, 0, width);
507 else
508 for (x=0; x<width; x++)
509 plane[x] = get_bits(gb, 1);
510 plane += stride;
1cf9f514 511 }
be3492ec 512}
21aa398f 513
be3492ec
KS
514/** Decode columns by checking if they are skipped
515 * @param plane Buffer to store decoded bits
516 * @param[in] width Width of this buffer
517 * @param[in] height Height of this buffer
518 * @param[in] stride of this buffer
519 * @fixme FIXME: Optimize
520 */
521static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
522 int x, y;
21aa398f 523
be3492ec
KS
524 for (x=0; x<width; x++){
525 if (!get_bits(gb, 1)) //colskip
526 for (y=0; y<height; y++)
527 plane[y*stride] = 0;
528 else
529 for (y=0; y<height; y++)
530 plane[y*stride] = get_bits(gb, 1);
531 plane ++;
21aa398f 532 }
21aa398f
AB
533}
534
be3492ec
KS
535/** Decode a bitplane's bits
536 * @param bp Bitplane where to store the decode bits
537 * @param v VC-1 context for bit reading and logging
2ce151f8 538 * @return Status
be3492ec
KS
539 * @fixme FIXME: Optimize
540 * @todo TODO: Decide if a struct is needed
2ce151f8 541 */
87dfe848 542static int bitplane_decoding(uint8_t* data, int *raw_flag, VC1Context *v)
21aa398f 543{
be3492ec 544 GetBitContext *gb = &v->s.gb;
21aa398f 545
be3492ec 546 int imode, x, y, code, offset;
87dfe848
KS
547 uint8_t invert, *planep = data;
548 int width, height, stride;
21aa398f 549
87dfe848
KS
550 width = v->s.mb_width;
551 height = v->s.mb_height;
552 stride = v->s.mb_stride;
be3492ec
KS
553 invert = get_bits(gb, 1);
554 imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
21aa398f 555
87dfe848 556 *raw_flag = 0;
be3492ec 557 switch (imode)
21aa398f 558 {
be3492ec
KS
559 case IMODE_RAW:
560 //Data is actually read in the MB layer (same for all tests == "raw")
87dfe848 561 *raw_flag = 1; //invert ignored
be3492ec
KS
562 return invert;
563 case IMODE_DIFF2:
564 case IMODE_NORM2:
87dfe848 565 if ((height * width) & 1)
21aa398f 566 {
be3492ec
KS
567 *planep++ = get_bits(gb, 1);
568 offset = 1;
21aa398f 569 }
be3492ec
KS
570 else offset = 0;
571 // decode bitplane as one long line
87dfe848 572 for (y = offset; y < height * width; y += 2) {
be3492ec
KS
573 code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
574 *planep++ = code & 1;
575 offset++;
87dfe848 576 if(offset == width) {
be3492ec 577 offset = 0;
87dfe848 578 planep += stride - width;
21aa398f 579 }
be3492ec
KS
580 *planep++ = code >> 1;
581 offset++;
87dfe848 582 if(offset == width) {
be3492ec 583 offset = 0;
87dfe848 584 planep += stride - width;
21aa398f 585 }
be3492ec
KS
586 }
587 break;
588 case IMODE_DIFF6:
589 case IMODE_NORM6:
87dfe848
KS
590 if(!(height % 3) && (width % 3)) { // use 2x3 decoding
591 for(y = 0; y < height; y+= 3) {
592 for(x = width & 1; x < width; x += 2) {
be3492ec
KS
593 code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
594 if(code < 0){
595 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
596 return -1;
597 }
598 planep[x + 0] = (code >> 0) & 1;
599 planep[x + 1] = (code >> 1) & 1;
87dfe848
KS
600 planep[x + 0 + stride] = (code >> 2) & 1;
601 planep[x + 1 + stride] = (code >> 3) & 1;
602 planep[x + 0 + stride * 2] = (code >> 4) & 1;
603 planep[x + 1 + stride * 2] = (code >> 5) & 1;
be3492ec 604 }
87dfe848 605 planep += stride * 3;
be3492ec 606 }
87dfe848 607 if(width & 1) decode_colskip(data, 1, height, stride, &v->s.gb);
be3492ec 608 } else { // 3x2
87dfe848
KS
609 for(y = height & 1; y < height; y += 2) {
610 for(x = width % 3; x < width; x += 3) {
be3492ec
KS
611 code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
612 if(code < 0){
613 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
614 return -1;
615 }
616 planep[x + 0] = (code >> 0) & 1;
617 planep[x + 1] = (code >> 1) & 1;
618 planep[x + 2] = (code >> 2) & 1;
87dfe848
KS
619 planep[x + 0 + stride] = (code >> 3) & 1;
620 planep[x + 1 + stride] = (code >> 4) & 1;
621 planep[x + 2 + stride] = (code >> 5) & 1;
be3492ec 622 }
87dfe848 623 planep += stride * 2;
be3492ec 624 }
87dfe848
KS
625 x = width % 3;
626 if(x) decode_colskip(data , x, height , stride, &v->s.gb);
627 if(height & 1) decode_rowskip(data+x, width - x, 1, stride, &v->s.gb);
be3492ec
KS
628 }
629 break;
630 case IMODE_ROWSKIP:
87dfe848 631 decode_rowskip(data, width, height, stride, &v->s.gb);
be3492ec
KS
632 break;
633 case IMODE_COLSKIP:
87dfe848 634 decode_colskip(data, width, height, stride, &v->s.gb);
be3492ec
KS
635 break;
636 default: break;
637 }
638
639 /* Applying diff operator */
640 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
641 {
87dfe848 642 planep = data;
be3492ec 643 planep[0] ^= invert;
87dfe848 644 for (x=1; x<width; x++)
be3492ec 645 planep[x] ^= planep[x-1];
87dfe848 646 for (y=1; y<height; y++)
be3492ec 647 {
87dfe848
KS
648 planep += stride;
649 planep[0] ^= planep[-stride];
650 for (x=1; x<width; x++)
21aa398f 651 {
87dfe848
KS
652 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert;
653 else planep[x] ^= planep[x-1];
21aa398f
AB
654 }
655 }
656 }
be3492ec 657 else if (invert)
21aa398f 658 {
87dfe848
KS
659 planep = data;
660 for (x=0; x<stride*height; x++) planep[x] = !planep[x]; //FIXME stride
21aa398f 661 }
be3492ec
KS
662 return (imode<<1) + invert;
663}
87dfe848 664
be3492ec 665/** @} */ //Bitplane group
21aa398f 666
be3492ec
KS
667/***********************************************************************/
668/** VOP Dquant decoding
669 * @param v VC-1 Context
670 */
671static int vop_dquant_decoding(VC1Context *v)
672{
673 GetBitContext *gb = &v->s.gb;
674 int pqdiff;
675
676 //variable size
677 if (v->dquant == 2)
678 {
679 pqdiff = get_bits(gb, 3);
680 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
681 else v->altpq = v->pq + pqdiff + 1;
682 }
683 else
21aa398f 684 {
be3492ec
KS
685 v->dquantfrm = get_bits(gb, 1);
686 if ( v->dquantfrm )
21aa398f 687 {
be3492ec
KS
688 v->dqprofile = get_bits(gb, 2);
689 switch (v->dqprofile)
21aa398f 690 {
be3492ec
KS
691 case DQPROFILE_SINGLE_EDGE:
692 case DQPROFILE_DOUBLE_EDGES:
693 v->dqsbedge = get_bits(gb, 2);
694 break;
695 case DQPROFILE_ALL_MBS:
696 v->dqbilevel = get_bits(gb, 1);
697 default: break; //Forbidden ?
42cc17f9 698 }
be3492ec 699 if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
21aa398f 700 {
be3492ec
KS
701 pqdiff = get_bits(gb, 3);
702 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
703 else v->altpq = v->pq + pqdiff + 1;
21aa398f 704 }
21aa398f
AB
705 }
706 }
be3492ec
KS
707 return 0;
708}
21aa398f 709
21aa398f 710
be3492ec
KS
711/** Do inverse transform
712 */
713static void vc1_inv_trans(DCTELEM block[64], int M, int N)
714{
715 int i;
716 register int t1,t2,t3,t4,t5,t6,t7,t8;
717 DCTELEM *src, *dst;
718
719 src = block;
720 dst = block;
721 if(M==4){
722 for(i = 0; i < N; i++){
723 t1 = 17 * (src[0] + src[2]);
724 t2 = 17 * (src[0] - src[2]);
725 t3 = 22 * src[1];
726 t4 = 22 * src[3];
727 t5 = 10 * src[1];
728 t6 = 10 * src[3];
729
730 dst[0] = (t1 + t3 + t6 + 4) >> 3;
731 dst[1] = (t2 - t4 + t5 + 4) >> 3;
732 dst[2] = (t2 + t4 - t5 + 4) >> 3;
733 dst[3] = (t1 - t3 - t6 + 4) >> 3;
734
735 src += 8;
736 dst += 8;
25a0a0a5 737 }
be3492ec
KS
738 }else{
739 for(i = 0; i < N; i++){
740 t1 = 12 * (src[0] + src[4]);
741 t2 = 12 * (src[0] - src[4]);
742 t3 = 16 * src[2] + 6 * src[6];
743 t4 = 6 * src[2] - 16 * src[6];
744
745 t5 = t1 + t3;
746 t6 = t2 + t4;
747 t7 = t2 - t4;
748 t8 = t1 - t3;
749
750 t1 = 16 * src[1] + 15 * src[3] + 9 * src[5] + 4 * src[7];
751 t2 = 15 * src[1] - 4 * src[3] - 16 * src[5] - 9 * src[7];
752 t3 = 9 * src[1] - 16 * src[3] + 4 * src[5] + 15 * src[7];
753 t4 = 4 * src[1] - 9 * src[3] + 15 * src[5] - 16 * src[7];
754
755 dst[0] = (t5 + t1 + 4) >> 3;
756 dst[1] = (t6 + t2 + 4) >> 3;
757 dst[2] = (t7 + t3 + 4) >> 3;
758 dst[3] = (t8 + t4 + 4) >> 3;
759 dst[4] = (t8 - t4 + 4) >> 3;
760 dst[5] = (t7 - t3 + 4) >> 3;
761 dst[6] = (t6 - t2 + 4) >> 3;
762 dst[7] = (t5 - t1 + 4) >> 3;
763
764 src += 8;
765 dst += 8;
21aa398f 766 }
be3492ec 767 }
21aa398f 768
be3492ec
KS
769 src = block;
770 dst = block;
771 if(N==4){
772 for(i = 0; i < M; i++){
773 t1 = 17 * (src[ 0] + src[16]);
774 t2 = 17 * (src[ 0] - src[16]);
775 t3 = 22 * src[ 8];
776 t4 = 22 * src[24];
777 t5 = 10 * src[ 8];
778 t6 = 10 * src[24];
779
780 dst[ 0] = (t1 + t3 + t6 + 64) >> 7;
781 dst[ 8] = (t2 - t4 + t5 + 64) >> 7;
782 dst[16] = (t2 + t4 - t5 + 64) >> 7;
783 dst[24] = (t1 - t3 - t6 + 64) >> 7;
784
785 src ++;
786 dst ++;
25a0a0a5 787 }
be3492ec
KS
788 }else{
789 for(i = 0; i < M; i++){
790 t1 = 12 * (src[ 0] + src[32]);
791 t2 = 12 * (src[ 0] - src[32]);
792 t3 = 16 * src[16] + 6 * src[48];
793 t4 = 6 * src[16] - 16 * src[48];
794
795 t5 = t1 + t3;
796 t6 = t2 + t4;
797 t7 = t2 - t4;
798 t8 = t1 - t3;
799
800 t1 = 16 * src[ 8] + 15 * src[24] + 9 * src[40] + 4 * src[56];
801 t2 = 15 * src[ 8] - 4 * src[24] - 16 * src[40] - 9 * src[56];
802 t3 = 9 * src[ 8] - 16 * src[24] + 4 * src[40] + 15 * src[56];
803 t4 = 4 * src[ 8] - 9 * src[24] + 15 * src[40] - 16 * src[56];
804
805 dst[ 0] = (t5 + t1 + 64) >> 7;
806 dst[ 8] = (t6 + t2 + 64) >> 7;
807 dst[16] = (t7 + t3 + 64) >> 7;
808 dst[24] = (t8 + t4 + 64) >> 7;
809 dst[32] = (t8 - t4 + 64 + 1) >> 7;
810 dst[40] = (t7 - t3 + 64 + 1) >> 7;
811 dst[48] = (t6 - t2 + 64 + 1) >> 7;
812 dst[56] = (t5 - t1 + 64 + 1) >> 7;
813
814 src++;
815 dst++;
21aa398f
AB
816 }
817 }
be3492ec 818}
21aa398f 819
be3492ec
KS
820/** Apply overlap transform
821 * @todo optimize
822 * @todo move to DSPContext
823 */
824static void vc1_overlap_block(MpegEncContext *s, DCTELEM block[64], int n, int do_hor, int do_vert)
825{
826 int i;
827
828 if(do_hor) { //TODO
829 }
830 if(do_vert) { //TODO
831 }
832
833 for(i = 0; i < 64; i++)
834 block[i] += 128;
835}
836
837
87dfe848
KS
838static void vc1_v_overlap(uint8_t* src, int stride)
839{
840 int i;
841 int a, b, c, d;
842 for(i = 0; i < 8; i++) {
843 a = src[-2*stride];
844 b = src[-stride];
845 c = src[0];
846 d = src[stride];
847
848 src[-2*stride] = (7*a + d) >> 3;
849 src[-stride] = (-a + 7*b + c + d) >> 3;
850 src[0] = (a + b + 7*c - d) >> 3;
851 src[stride] = (a + 7*d) >> 3;
852 src++;
853 }
854}
855
856static void vc1_h_overlap(uint8_t* src, int stride)
857{
858 int i;
859 int a, b, c, d;
860 for(i = 0; i < 8; i++) {
861 a = src[-2];
862 b = src[-1];
863 c = src[0];
864 d = src[1];
865
866 src[-2] = (7*a + d) >> 3;
867 src[-1] = (-a + 7*b + c + d) >> 3;
868 src[0] = (a + b + 7*c - d) >> 3;
869 src[1] = (a + 7*d) >> 3;
870 src += stride;
871 }
872}
873
be3492ec
KS
874/** Put block onto picture
875 * @todo move to DSPContext
876 */
877static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
878{
879 uint8_t *Y;
880 int ys, us, vs;
881 DSPContext *dsp = &v->s.dsp;
882
883 ys = v->s.current_picture.linesize[0];
884 us = v->s.current_picture.linesize[1];
885 vs = v->s.current_picture.linesize[2];
886 Y = v->s.dest[0];
887
888 dsp->put_pixels_clamped(block[0], Y, ys);
889 dsp->put_pixels_clamped(block[1], Y + 8, ys);
890 Y += ys * 8;
891 dsp->put_pixels_clamped(block[2], Y, ys);
892 dsp->put_pixels_clamped(block[3], Y + 8, ys);
893
894 dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
895 dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
896}
897
87dfe848
KS
898/* clip motion vector as specified in 8.3.6.5 */
899#define CLIP_RANGE(mv, src, lim, bs) \
900 if(mv < -bs) mv = -bs - src * bs; \
901 if(mv > lim) mv = lim - src * bs;
902
be3492ec
KS
903/** Do motion compensation over 1 macroblock
904 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
905 */
906static void vc1_mc_1mv(VC1Context *v)
907{
908 MpegEncContext *s = &v->s;
909 DSPContext *dsp = &v->s.dsp;
910 uint8_t *srcY, *srcU, *srcV;
87dfe848 911 int dxy, uvdxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
be3492ec
KS
912
913 if(!v->s.last_picture.data[0])return;
914
87dfe848
KS
915 mx = s->mv[0][0][0];
916 my = s->mv[0][0][1];
917 uvmx = (mx + ((mx & 3) == 3)) >> 1;
918 uvmy = (my + ((my & 3) == 3)) >> 1;
be3492ec
KS
919 srcY = s->last_picture.data[0];
920 srcU = s->last_picture.data[1];
921 srcV = s->last_picture.data[2];
922
87dfe848 923 if(v->fastuvmc) { // XXX: 8.3.5.4.5 specifies something different
3f6d6af6
KS
924 uvmx = (uvmx + 1) & ~1;
925 uvmy = (uvmy + 1) & ~1;
87dfe848
KS
926 }
927
928 src_x = s->mb_x * 16 + (mx >> 2);
929 src_y = s->mb_y * 16 + (my >> 2);
930 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
931 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
932
933 CLIP_RANGE( src_x, s->mb_x, s->mb_width * 16, 16);
934 CLIP_RANGE( src_y, s->mb_y, s->mb_height * 16, 16);
935 CLIP_RANGE(uvsrc_x, s->mb_x, s->mb_width * 8, 8);
936 CLIP_RANGE(uvsrc_y, s->mb_y, s->mb_height * 8, 8);
937
938 srcY += src_y * s->linesize + src_x;
939 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
940 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
941
942 if((unsigned)src_x > s->h_edge_pos - (mx&3) - 16
943 || (unsigned)src_y > s->v_edge_pos - (my&3) - 16){
944 uint8_t *uvbuf= s->edge_emu_buffer + 18 * s->linesize;
945
946 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 16+1, 16+1,
947 src_x, src_y, s->h_edge_pos, s->v_edge_pos);
948 srcY = s->edge_emu_buffer;
949 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
950 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
951 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
952 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
953 srcU = uvbuf;
954 srcV = uvbuf + 16;
955 }
956
957 if(!s->quarter_sample) { // hpel mc
958 mx >>= 1;
959 my >>= 1;
be3492ec 960 dxy = ((my & 1) << 1) | (mx & 1);
87dfe848
KS
961 uvdxy = 0;
962
be3492ec 963 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
be3492ec 964 } else {
87dfe848
KS
965 dxy = ((my & 3) << 2) | (mx & 3);
966 uvdxy = ((uvmy & 1) << 1) | (uvmx & 1);
be3492ec
KS
967
968 dsp->put_no_rnd_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
be3492ec 969 }
3f6d6af6
KS
970 dsp->put_no_rnd_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize, 8);
971 dsp->put_no_rnd_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize, 8);
972// dsp->put_mspel_pixels_tab[uvdxy](s->dest[1], srcU, s->uvlinesize);
973// dsp->put_mspel_pixels_tab[uvdxy](s->dest[2], srcV, s->uvlinesize);
21aa398f 974}
21aa398f 975
115329f1 976/**
2ce151f8 977 * Decode Simple/Main Profiles sequence header
978 * @see Figure 7-8, p16-17
979 * @param avctx Codec context
980 * @param gb GetBit context initialized from Codec context extra_data
981 * @return Status
982 */
21aa398f
AB
983static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
984{
10b9c374 985 VC1Context *v = avctx->priv_data;
21aa398f 986
be3492ec 987 av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
21aa398f 988 v->profile = get_bits(gb, 2);
7cc84d24 989 if (v->profile == 2)
25a0a0a5 990 {
be3492ec 991 av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
25a0a0a5
IK
992 return -1;
993 }
21aa398f 994
7cc84d24 995 if (v->profile == PROFILE_ADVANCED)
21aa398f
AB
996 {
997 v->level = get_bits(gb, 3);
25a0a0a5
IK
998 if(v->level >= 5)
999 {
1000 av_log(avctx, AV_LOG_ERROR, "Reserved LEVEL %i\n",v->level);
1001 }
21aa398f
AB
1002 v->chromaformat = get_bits(gb, 2);
1003 if (v->chromaformat != 1)
1004 {
1005 av_log(avctx, AV_LOG_ERROR,
1006 "Only 4:2:0 chroma format supported\n");
1007 return -1;
1008 }
1009 }
1010 else
21aa398f
AB
1011 {
1012 v->res_sm = get_bits(gb, 2); //reserved
1013 if (v->res_sm)
1014 {
1015 av_log(avctx, AV_LOG_ERROR,
1016 "Reserved RES_SM=%i is forbidden\n", v->res_sm);
7cc84d24 1017 return -1;
21aa398f
AB
1018 }
1019 }
1020
1021 // (fps-2)/4 (->30)
1022 v->frmrtq_postproc = get_bits(gb, 3); //common
1023 // (bitrate-32kbps)/64kbps
1024 v->bitrtq_postproc = get_bits(gb, 5); //common
5e424311 1025 v->s.loop_filter = get_bits(gb, 1); //common
25a0a0a5
IK
1026 if(v->s.loop_filter == 1 && v->profile == PROFILE_SIMPLE)
1027 {
1028 av_log(avctx, AV_LOG_ERROR,
1029 "LOOPFILTER shell not be enabled in simple profile\n");
1030 }
21aa398f 1031
7cc84d24 1032 if (v->profile < PROFILE_ADVANCED)
21aa398f
AB
1033 {
1034 v->res_x8 = get_bits(gb, 1); //reserved
1035 if (v->res_x8)
1036 {
1037 av_log(avctx, AV_LOG_ERROR,
1038 "1 for reserved RES_X8 is forbidden\n");
5e424311 1039 //return -1;
21aa398f
AB
1040 }
1041 v->multires = get_bits(gb, 1);
1042 v->res_fasttx = get_bits(gb, 1);
1043 if (!v->res_fasttx)
1044 {
1045 av_log(avctx, AV_LOG_ERROR,
1046 "0 for reserved RES_FASTTX is forbidden\n");
1047 //return -1;
1048 }
1049 }
1050
1051 v->fastuvmc = get_bits(gb, 1); //common
1052 if (!v->profile && !v->fastuvmc)
1053 {
1054 av_log(avctx, AV_LOG_ERROR,
1055 "FASTUVMC unavailable in Simple Profile\n");
1056 return -1;
1057 }
1058 v->extended_mv = get_bits(gb, 1); //common
1059 if (!v->profile && v->extended_mv)
1060 {
1061 av_log(avctx, AV_LOG_ERROR,
1062 "Extended MVs unavailable in Simple Profile\n");
1063 return -1;
1064 }
1065 v->dquant = get_bits(gb, 2); //common
1066 v->vstransform = get_bits(gb, 1); //common
42cc17f9 1067
7cc84d24 1068 if (v->profile < PROFILE_ADVANCED)
21aa398f
AB
1069 {
1070 v->res_transtab = get_bits(gb, 1);
1071 if (v->res_transtab)
1072 {
1073 av_log(avctx, AV_LOG_ERROR,
1074 "1 for reserved RES_TRANSTAB is forbidden\n");
1075 return -1;
1076 }
1077 }
1078
1079 v->overlap = get_bits(gb, 1); //common
1080
7cc84d24 1081 if (v->profile < PROFILE_ADVANCED)
21aa398f 1082 {
0d33db8a 1083 v->s.resync_marker = get_bits(gb, 1);
21aa398f 1084 v->rangered = get_bits(gb, 1);
25a0a0a5
IK
1085 if (v->rangered && v->profile == PROFILE_SIMPLE)
1086 {
be3492ec 1087 av_log(avctx, AV_LOG_INFO,
42cc17f9 1088 "RANGERED should be set to 0 in simple profile\n");
25a0a0a5 1089 }
21aa398f
AB
1090 }
1091
0d33db8a 1092 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
21aa398f
AB
1093 v->quantizer_mode = get_bits(gb, 2); //common
1094
7cc84d24 1095 if (v->profile < PROFILE_ADVANCED)
21aa398f
AB
1096 {
1097 v->finterpflag = get_bits(gb, 1); //common
1098 v->res_rtm_flag = get_bits(gb, 1); //reserved
1099 if (!v->res_rtm_flag)
1100 {
1101 av_log(avctx, AV_LOG_ERROR,
1102 "0 for reserved RES_RTM_FLAG is forbidden\n");
1103 //return -1;
1104 }
be3492ec 1105 av_log(avctx, AV_LOG_DEBUG,
21aa398f
AB
1106 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1107 "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
1108 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
1109 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
1110 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
2ce151f8 1111 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
0d33db8a 1112 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
21aa398f
AB
1113 v->dquant, v->quantizer_mode, avctx->max_b_frames
1114 );
e5540b3f 1115 return 0;
21aa398f 1116 }
be3492ec 1117 return -1;
21aa398f
AB
1118}
1119
1120
be3492ec 1121static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
21aa398f 1122{
be3492ec
KS
1123 int pqindex, lowquant, status;
1124
1125 if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1126 skip_bits(gb, 2); //framecnt unused
1127 v->rangeredfrm = 0;
1128 if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1129 v->s.pict_type = get_bits(gb, 1);
1130 if (v->s.avctx->max_b_frames) {
1131 if (!v->s.pict_type) {
1132 if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1133 else v->s.pict_type = B_TYPE;
1134 } else v->s.pict_type = P_TYPE;
1135 } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1136
1137 if(v->s.pict_type == I_TYPE)
1138 get_bits(gb, 7); // skip buffer fullness
1139
1140 /* Quantizer stuff */
1141 pqindex = get_bits(gb, 5);
1142 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1143 v->pq = pquant_table[0][pqindex];
1144 else
1145 v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1146
1147 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1148 v->pquantizer = pqindex < 9;
1149 if (v->quantizer_mode == QUANT_UNIFORM || v->quantizer_mode == QUANT_NON_UNIFORM)
1150 v->pquantizer = v->quantizer_mode == QUANT_UNIFORM;
1151 v->pqindex = pqindex;
1152 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1153 else v->halfpq = 0;
1154 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1155 v->pquantizer = get_bits(gb, 1);
1156 v->dquantfrm = 0;
1157
1158//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1159// (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1160
1161 //TODO: complete parsing for P/B/BI frames
1162 switch(v->s.pict_type) {
1163 case P_TYPE:
1164 if (v->pq < 5) v->tt_index = 0;
1165 else if(v->pq < 13) v->tt_index = 1;
1166 else v->tt_index = 2;
1167
1168 if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1169 v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1170 v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1171 v->range_x = 1 << (v->k_x - 1);
1172 v->range_y = 1 << (v->k_y - 1);
1173 if (v->profile == PROFILE_ADVANCED)
1174 {
1175 if (v->postprocflag) v->postproc = get_bits(gb, 1);
1176 }
1177 else
1178 if (v->multires) v->respic = get_bits(gb, 2);
1179 lowquant = (v->pq > 12) ? 0 : 1;
1180 v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1181 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1182 {
1183 v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(gb, 1, 3)];
1184 v->lumscale = get_bits(gb, 6);
1185 v->lumshift = get_bits(gb, 6);
1186 }
1187 if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
87dfe848 1188 v->s.quarter_sample = 0;
be3492ec 1189 else
87dfe848 1190 v->s.quarter_sample = 1;
be3492ec
KS
1191
1192if(v->mv_mode != MV_PMODE_1MV && v->mv_mode != MV_PMODE_1MV_HPEL && v->mv_mode != MV_PMODE_1MV_HPEL_BILIN) {
1193 av_log(v->s.avctx, AV_LOG_ERROR, "Only 1MV P-frames are supported by now\n");
1194 return -1;
1195}
1196 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1197 v->mv_mode2 == MV_PMODE_MIXED_MV)
1198 || v->mv_mode == MV_PMODE_MIXED_MV)
1199 {
87dfe848 1200 status = bitplane_decoding(v->mv_type_mb_plane, &v->mv_type_is_raw, v);
be3492ec
KS
1201 if (status < 0) return -1;
1202 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1203 "Imode: %i, Invert: %i\n", status>>1, status&1);
87dfe848
KS
1204 } else {
1205 v->mv_type_is_raw = 0;
1206 memset(v->mv_type_mb_plane, 0, v->s.mb_stride * v->s.mb_height);
be3492ec 1207 }
0f7344aa 1208 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
be3492ec
KS
1209 if (status < 0) return -1;
1210 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1211 "Imode: %i, Invert: %i\n", status>>1, status&1);
1212
1213 /* Hopefully this is correct for P frames */
1214 v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1215 v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1216
1217 if (v->dquant)
1218 {
1219 av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1220 vop_dquant_decoding(v);
1221 }
1222
1223 v->ttfrm = 0; //FIXME Is that so ?
1224 if (v->vstransform)
1225 {
1226 v->ttmbf = get_bits(gb, 1);
1227 if (v->ttmbf)
1228 {
1229 v->ttfrm = get_bits(gb, 2);
1230 }
1231 }
1232 break;
1233 case B_TYPE:
1234 break;
21aa398f 1235 }
be3492ec
KS
1236
1237 /* AC Syntax */
1238 v->c_ac_table_index = decode012(gb);
1239 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
21aa398f 1240 {
be3492ec 1241 v->y_ac_table_index = decode012(gb);
21aa398f 1242 }
be3492ec
KS
1243 /* DC Syntax */
1244 v->s.dc_table_index = get_bits(gb, 1);
1245
21aa398f
AB
1246 return 0;
1247}
21aa398f 1248
2ce151f8 1249/***********************************************************************/
1250/**
be3492ec
KS
1251 * @defgroup block VC-1 Block-level functions
1252 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1253 * @todo TODO: Integrate to MpegEncContext facilities
2ce151f8 1254 * @{
1255 */
1256
be3492ec
KS
1257/**
1258 * @def GET_MQUANT
1259 * @brief Get macroblock-level quantizer scale
1260 * @warning XXX: qdiff to the frame quant, not previous quant ?
1261 * @fixme XXX: Don't know how to initialize mquant otherwise in last case
2ce151f8 1262 */
be3492ec
KS
1263#define GET_MQUANT() \
1264 if (v->dquantfrm) \
1265 { \
1266 if (v->dqprofile == DQPROFILE_ALL_MBS) \
1267 { \
1268 if (v->dqbilevel) \
1269 { \
1270 mquant = (get_bits(gb, 1)) ? v->pq : v->altpq; \
1271 } \
1272 else \
1273 { \
1274 mqdiff = get_bits(gb, 3); \
1275 if (mqdiff != 7) mquant = v->pq + mqdiff; \
1276 else mquant = get_bits(gb, 5); \
1277 } \
1278 } \
1279 else mquant = v->pq; \
1280 }
2ce151f8 1281
be3492ec
KS
1282/**
1283 * @def GET_MVDATA(_dmv_x, _dmv_y)
1284 * @brief Get MV differentials
1285 * @see MVDATA decoding from 8.3.5.2, p(1)20
1286 * @param _dmv_x Horizontal differential for decoded MV
1287 * @param _dmv_y Vertical differential for decoded MV
1288 * @todo TODO: Use MpegEncContext arrays to store them
2ce151f8 1289 */
be3492ec
KS
1290#define GET_MVDATA(_dmv_x, _dmv_y) \
1291 index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1292 VC1_MV_DIFF_VLC_BITS, 2); \
1293 if (index > 36) \
1294 { \
1295 mb_has_coeffs = 1; \
1296 index -= 37; \
1297 } \
1298 else mb_has_coeffs = 0; \
1299 s->mb_intra = 0; \
1300 if (!index) { _dmv_x = _dmv_y = 0; } \
1301 else if (index == 35) \
1302 { \
87dfe848
KS
1303 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1304 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
be3492ec
KS
1305 } \
1306 else if (index == 36) \
1307 { \
1308 _dmv_x = 0; \
1309 _dmv_y = 0; \
1310 s->mb_intra = 1; \
1311 } \
1312 else \
1313 { \
1314 index1 = index%6; \
87dfe848
KS
1315 if (!s->quarter_sample && index1 == 5) val = 1; \
1316 else val = 0; \
e8ba1cea
KS
1317 if(size_table[index1] - val > 0) \
1318 val = get_bits(gb, size_table[index1] - val); \
1319 else val = 0; \
be3492ec
KS
1320 sign = 0 - (val&1); \
1321 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1322 \
1323 index1 = index/6; \
87dfe848
KS
1324 if (!s->quarter_sample && index1 == 5) val = 1; \
1325 else val = 0; \
e8ba1cea
KS
1326 if(size_table[index1] - val > 0) \
1327 val = get_bits(gb, size_table[index1] - val); \
1328 else val = 0; \
be3492ec
KS
1329 sign = 0 - (val&1); \
1330 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1331 }
21aa398f 1332
be3492ec 1333/** Predict and set motion vector
2ce151f8 1334 */
be3492ec 1335static inline void vc1_pred_mv(MpegEncContext *s, int dmv_x, int dmv_y, int mv1, int r_x, int r_y)
0d33db8a 1336{
be3492ec
KS
1337 int xy, wrap, off;
1338 int16_t *A, *B, *C;
1339 int px, py;
1340 int sum;
1341 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
0d33db8a 1342
be3492ec 1343 /* scale MV difference to be quad-pel */
87dfe848
KS
1344 dmv_x <<= 1 - s->quarter_sample;
1345 dmv_y <<= 1 - s->quarter_sample;
d29f0cd9 1346
be3492ec
KS
1347 wrap = s->b8_stride;
1348 xy = s->block_index[0];
d29f0cd9 1349
be3492ec
KS
1350 C = s->current_picture.motion_val[0][xy - (1 << mv1)];
1351 A = s->current_picture.motion_val[0][xy - (wrap << mv1)];
1352 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1353 B = s->current_picture.motion_val[0][xy + ((off - wrap) << mv1)];
d29f0cd9 1354
be3492ec
KS
1355 if(!s->first_slice_line) { // predictor A is not out of bounds
1356 if(s->mb_width == 1) {
1357 px = A[0];
1358 py = A[1];
1359 } else {
1360 px = mid_pred(A[0], B[0], C[0]);
1361 py = mid_pred(A[1], B[1], C[1]);
1362 }
1363 } else if(s->mb_x) { // predictor C is not out of bounds
1364 px = C[0];
1365 py = C[1];
1366 } else {
1367 px = py = 0;
1368 }
1369 if(s->mb_intra) px = py = 0;
1370
1371 /* Pullback MV as specified in 8.3.5.3.4 */
1372 {
1373 int qx, qy, X, Y;
1374 qx = s->mb_x << 6; //FIXME: add real block coords for 4MV mode
1375 qy = s->mb_y << 6;
1376 X = (s->mb_width << 6) - 4;
1377 Y = (s->mb_height << 6) - 4;
1378 if(mv1) {
1379 if(qx + px < -60) px = -60 - qx;
1380 if(qy + py < -60) py = -60 - qy;
1381 } else {
1382 if(qx + px < -28) px = -28 - qx;
1383 if(qy + py < -28) py = -28 - qy;
1384 }
1385 if(qx + px > X) px = X - qx;
1386 if(qy + py > Y) py = Y - qy;
1387 }
1388 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1389 if(!s->mb_intra && !s->first_slice_line && s->mb_x) {
1390 if(IS_INTRA(s->current_picture.mb_type[mb_pos - s->mb_stride]))
1391 sum = ABS(px) + ABS(py);
d29f0cd9 1392 else
be3492ec
KS
1393 sum = ABS(px - A[0]) + ABS(py - A[1]);
1394 if(sum > 32) {
1395 if(get_bits1(&s->gb)) {
1396 px = A[0];
1397 py = A[1];
1398 } else {
1399 px = C[0];
1400 py = C[1];
1401 }
1402 } else {
1403 if(IS_INTRA(s->current_picture.mb_type[mb_pos - 1]))
1404 sum = ABS(px) + ABS(py);
1405 else
1406 sum = ABS(px - C[0]) + ABS(py - C[1]);
1407 if(sum > 32) {
1408 if(get_bits1(&s->gb)) {
1409 px = A[0];
1410 py = A[1];
1411 } else {
1412 px = C[0];
1413 py = C[1];
1414 }
1415 }
1416 }
d29f0cd9 1417 }
be3492ec
KS
1418 /* store MV using signed modulus of MV range defined in 4.11 */
1419 s->mv[0][0][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1420 s->mv[0][0][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
d29f0cd9
MN
1421}
1422
be3492ec
KS
1423/** Get predicted DC value for I-frames only
1424 * prediction dir: left=0, top=1
1425 * @param s MpegEncContext
1426 * @param[in] n block index in the current MB
1427 * @param dc_val_ptr Pointer to DC predictor
1428 * @param dir_ptr Prediction direction for use in AC prediction
2ce151f8 1429 */
be3492ec
KS
1430static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1431 int16_t **dc_val_ptr, int *dir_ptr)
21aa398f 1432{
be3492ec
KS
1433 int a, b, c, wrap, pred, scale;
1434 int16_t *dc_val;
1435 static const uint16_t dcpred[32] = {
1436 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
1437 114, 102, 93, 85, 79, 73, 68, 64,
1438 60, 57, 54, 51, 49, 47, 45, 43,
1439 41, 39, 38, 37, 35, 34, 33
1440 };
0d33db8a 1441
be3492ec
KS
1442 /* find prediction - wmv3_dc_scale always used here in fact */
1443 if (n < 4) scale = s->y_dc_scale;
1444 else scale = s->c_dc_scale;
21aa398f 1445
be3492ec
KS
1446 wrap = s->block_wrap[n];
1447 dc_val= s->dc_val[0] + s->block_index[n];
21aa398f 1448
be3492ec
KS
1449 /* B A
1450 * C X
1451 */
1452 c = dc_val[ - 1];
1453 b = dc_val[ - 1 - wrap];
1454 a = dc_val[ - wrap];
21aa398f 1455
be3492ec 1456 if (pq < 9 || !overlap)
21aa398f 1457 {
be3492ec
KS
1458 /* Set outer values */
1459 if (!s->mb_y && (n!=2 && n!=3)) b=a=dcpred[scale];
1460 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
bf2bc926 1461 }
1462 else
1463 {
be3492ec
KS
1464 /* Set outer values */
1465 if (!s->mb_y && (n!=2 && n!=3)) b=a=0;
1466 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
21aa398f 1467 }
21aa398f 1468
be3492ec
KS
1469 if (abs(a - b) <= abs(b - c)) {
1470 pred = c;
1471 *dir_ptr = 1;//left
1472 } else {
1473 pred = a;
1474 *dir_ptr = 0;//top
21aa398f
AB
1475 }
1476
be3492ec
KS
1477 /* update predictor */
1478 *dc_val_ptr = &dc_val[0];
1479 return pred;
bf2bc926 1480}
1481
21aa398f 1482
be3492ec
KS
1483/** Get predicted DC value
1484 * prediction dir: left=0, top=1
1485 * @param s MpegEncContext
1486 * @param[in] n block index in the current MB
1487 * @param dc_val_ptr Pointer to DC predictor
1488 * @param dir_ptr Prediction direction for use in AC prediction
2ce151f8 1489 */
be3492ec
KS
1490static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1491 int a_avail, int c_avail,
1492 int16_t **dc_val_ptr, int *dir_ptr)
21aa398f 1493{
be3492ec
KS
1494 int a, b, c, wrap, pred, scale;
1495 int16_t *dc_val;
1496 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1497 int mb_pos2, q1, q2;
21aa398f 1498
be3492ec
KS
1499 /* find prediction - wmv3_dc_scale always used here in fact */
1500 if (n < 4) scale = s->y_dc_scale;
1501 else scale = s->c_dc_scale;
21aa398f 1502
be3492ec
KS
1503 wrap = s->block_wrap[n];
1504 dc_val= s->dc_val[0] + s->block_index[n];
21aa398f 1505
be3492ec
KS
1506 /* B A
1507 * C X
1508 */
1509 c = dc_val[ - 1];
1510 b = dc_val[ - 1 - wrap];
1511 a = dc_val[ - wrap];
21aa398f 1512
be3492ec
KS
1513 if(a_avail && c_avail) {
1514 if(abs(a - b) <= abs(b - c)) {
1515 pred = c;
1516 *dir_ptr = 1;//left
1517 } else {
1518 pred = a;
1519 *dir_ptr = 0;//top
1520 }
1521 } else if(a_avail) {
1522 pred = a;
1523 *dir_ptr = 0;//top
1524 } else if(c_avail) {
1525 pred = c;
1526 *dir_ptr = 1;//left
1527 } else {
1528 pred = 0;
1529 *dir_ptr = 1;//left
21aa398f
AB
1530 }
1531
be3492ec
KS
1532 /* scale coeffs if needed */
1533 mb_pos2 = mb_pos - *dir_ptr - (1 - *dir_ptr) * s->mb_stride;
1534 q1 = s->current_picture.qscale_table[mb_pos];
1535 q2 = s->current_picture.qscale_table[mb_pos2];
1536 if(0 && q1 && q2 && q1 != q2) {
1537 q1 = s->y_dc_scale_table[q1];
1538 q2 = s->y_dc_scale_table[q2];
1539 pred = (pred * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
21aa398f 1540 }
be3492ec
KS
1541
1542 /* update predictor */
1543 *dc_val_ptr = &dc_val[0];
1544 return pred;
21aa398f 1545}
2ce151f8 1546
21aa398f 1547
115329f1 1548/**
be3492ec
KS
1549 * @defgroup std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1550 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1551 * @todo TODO: Integrate to MpegEncContext facilities
2ce151f8 1552 * @{
1553 */
21aa398f 1554
be3492ec 1555static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
21aa398f 1556{
be3492ec 1557 int xy, wrap, pred, a, b, c;
21aa398f 1558
be3492ec
KS
1559 xy = s->block_index[n];
1560 wrap = s->b8_stride;
21aa398f 1561
be3492ec
KS
1562 /* B C
1563 * A X
1564 */
1565 a = s->coded_block[xy - 1 ];
1566 b = s->coded_block[xy - 1 - wrap];
1567 c = s->coded_block[xy - wrap];
21aa398f 1568
be3492ec
KS
1569 if (b == c) {
1570 pred = a;
1571 } else {
1572 pred = c;
21aa398f 1573 }
be3492ec
KS
1574
1575 /* store value */
1576 *coded_block_ptr = &s->coded_block[xy];
1577
1578 return pred;
0d33db8a 1579}
1580
be3492ec
KS
1581/**
1582 * Decode one AC coefficient
1583 * @param v The VC1 context
1584 * @param last Last coefficient
1585 * @param skip How much zero coefficients to skip
1586 * @param value Decoded AC coefficient value
1587 * @see 8.1.3.4
2ce151f8 1588 */
be3492ec 1589static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
0d33db8a 1590{
1591 GetBitContext *gb = &v->s.gb;
be3492ec
KS
1592 int index, escape, run = 0, level = 0, lst = 0;
1593
1594 index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1595 if (index != vc1_ac_sizes[codingset] - 1) {
1596 run = vc1_index_decode_table[codingset][index][0];
1597 level = vc1_index_decode_table[codingset][index][1];
1598 lst = index >= vc1_last_decode_table[codingset];
1599 if(get_bits(gb, 1))
1600 level = -level;
1601 } else {
1602 escape = decode210(gb);
87dfe848 1603 if (escape != 2) {
be3492ec
KS
1604 index = get_vlc2(gb, vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1605 run = vc1_index_decode_table[codingset][index][0];
1606 level = vc1_index_decode_table[codingset][index][1];
1607 lst = index >= vc1_last_decode_table[codingset];
87dfe848
KS
1608 if(escape == 0) {
1609 if(lst)
1610 level += vc1_last_delta_level_table[codingset][run];
1611 else
1612 level += vc1_delta_level_table[codingset][run];
1613 } else {
1614 if(lst)
1615 run += vc1_last_delta_run_table[codingset][level] + 1;
1616 else
1617 run += vc1_delta_run_table[codingset][level] + 1;
1618 }
be3492ec
KS
1619 if(get_bits(gb, 1))
1620 level = -level;
1621 } else {
1622 int sign;
1623 lst = get_bits(gb, 1);
1624 if(v->s.esc3_level_length == 0) {
1625 if(v->pq < 8 || v->dquantfrm) { // table 59
1626 v->s.esc3_level_length = get_bits(gb, 3);
1627 if(!v->s.esc3_level_length)
1628 v->s.esc3_level_length = get_bits(gb, 2) + 8;
1629 } else { //table 60
1630 v->s.esc3_level_length = get_prefix(gb, 1, 6) + 2;
1631 }
1632 v->s.esc3_run_length = 3 + get_bits(gb, 2);
1633 }
1634 run = get_bits(gb, v->s.esc3_run_length);
1635 sign = get_bits(gb, 1);
1636 level = get_bits(gb, v->s.esc3_level_length);
1637 if(sign)
1638 level = -level;
1639 }
21aa398f 1640 }
7cc84d24 1641
be3492ec
KS
1642 *last = lst;
1643 *skip = run;
1644 *value = level;
21aa398f 1645}
21aa398f 1646
be3492ec
KS
1647/** Decode intra block in intra frames - should be faster than decode_intra_block
1648 * @param v VC1Context
1649 * @param block block to decode
1650 * @param coded are AC coeffs present or not
1651 * @param codingset set of VLC to decode data
2ce151f8 1652 */
be3492ec 1653static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
21aa398f 1654{
0d33db8a 1655 GetBitContext *gb = &v->s.gb;
be3492ec
KS
1656 MpegEncContext *s = &v->s;
1657 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1658 int run_diff, i;
1659 int16_t *dc_val;
1660 int16_t *ac_val, *ac_val2;
1661 int dcdiff;
21aa398f 1662
be3492ec
KS
1663 /* Get DC differential */
1664 if (n < 4) {
1665 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1666 } else {
1667 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
21aa398f 1668 }
be3492ec
KS
1669 if (dcdiff < 0){
1670 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1671 return -1;
1672 }
1673 if (dcdiff)
21aa398f 1674 {
be3492ec 1675 if (dcdiff == 119 /* ESC index value */)
21aa398f 1676 {
be3492ec
KS
1677 /* TODO: Optimize */
1678 if (v->pq == 1) dcdiff = get_bits(gb, 10);
1679 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1680 else dcdiff = get_bits(gb, 8);
21aa398f 1681 }
be3492ec 1682 else
21aa398f 1683 {
be3492ec
KS
1684 if (v->pq == 1)
1685 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1686 else if (v->pq == 2)
1687 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
21aa398f 1688 }
be3492ec
KS
1689 if (get_bits(gb, 1))
1690 dcdiff = -dcdiff;
21aa398f 1691 }
21aa398f 1692
be3492ec
KS
1693 /* Prediction */
1694 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1695 *dc_val = dcdiff;
0d33db8a 1696
be3492ec 1697 /* Store the quantized DC coeff, used for prediction */
0d33db8a 1698
be3492ec
KS
1699 if (n < 4) {
1700 block[0] = dcdiff * s->y_dc_scale;
1701 } else {
1702 block[0] = dcdiff * s->c_dc_scale;
21aa398f 1703 }
be3492ec
KS
1704 /* Skip ? */
1705 run_diff = 0;
1706 i = 0;
1707 if (!coded) {
1708 goto not_coded;
21aa398f 1709 }
7cc84d24 1710
be3492ec
KS
1711 //AC Decoding
1712 i = 1;
7cc84d24 1713
be3492ec
KS
1714 {
1715 int last = 0, skip, value;
1716 const int8_t *zz_table;
1717 int scale;
1718 int k;
8da75fb2 1719
be3492ec 1720 scale = v->pq * 2 + v->halfpq;
42cc17f9 1721
be3492ec
KS
1722 if(v->s.ac_pred) {
1723 if(!dc_pred_dir)
1724 zz_table = vc1_horizontal_zz;
1725 else
1726 zz_table = vc1_vertical_zz;
1727 } else
1728 zz_table = vc1_normal_zz;
1729
1730 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1731 ac_val2 = ac_val;
1732 if(dc_pred_dir) //left
1733 ac_val -= 16;
1734 else //top
1735 ac_val -= 16 * s->block_wrap[n];
1736
1737 while (!last) {
1738 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1739 i += skip;
1740 if(i > 63)
1741 break;
1742 block[zz_table[i++]] = value;
1743 }
7cc84d24 1744
be3492ec
KS
1745 /* apply AC prediction if needed */
1746 if(s->ac_pred) {
1747 if(dc_pred_dir) { //left
1748 for(k = 1; k < 8; k++)
1749 block[k << 3] += ac_val[k];
1750 } else { //top
1751 for(k = 1; k < 8; k++)
1752 block[k] += ac_val[k + 8];
1753 }
1754 }
1755 /* save AC coeffs for further prediction */
1756 for(k = 1; k < 8; k++) {
1757 ac_val2[k] = block[k << 3];
1758 ac_val2[k + 8] = block[k];
1759 }
42cc17f9 1760
be3492ec
KS
1761 /* scale AC coeffs */
1762 for(k = 1; k < 64; k++)
1763 if(block[k]) {
1764 block[k] *= scale;
1765 if(!v->pquantizer)
1766 block[k] += (block[k] < 0) ? -v->pq : v->pq;
1767 }
8da75fb2 1768
be3492ec
KS
1769 if(s->ac_pred) i = 63;
1770 }
1771
1772not_coded:
1773 if(!coded) {
1774 int k, scale;
1775 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1776 ac_val2 = ac_val;
1777
1778 scale = v->pq * 2 + v->halfpq;
1779 memset(ac_val2, 0, 16 * 2);
1780 if(dc_pred_dir) {//left
1781 ac_val -= 16;
1782 if(s->ac_pred)
1783 memcpy(ac_val2, ac_val, 8 * 2);
1784 } else {//top
1785 ac_val -= 16 * s->block_wrap[n];
1786 if(s->ac_pred)
1787 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
8da75fb2 1788 }
7cc84d24 1789
be3492ec
KS
1790 /* apply AC prediction if needed */
1791 if(s->ac_pred) {
1792 if(dc_pred_dir) { //left
1793 for(k = 1; k < 8; k++) {
1794 block[k << 3] = ac_val[k] * scale;
1795 if(!v->pquantizer)
1796 block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
1797 }
1798 } else { //top
1799 for(k = 1; k < 8; k++) {
1800 block[k] = ac_val[k + 8] * scale;
1801 if(!v->pquantizer)
1802 block[k] += (block[k] < 0) ? -v->pq : v->pq;
1803 }
1804 }
1805 i = 63;
1806 }
7cc84d24 1807 }
be3492ec 1808 s->block_last_index[n] = i;
7cc84d24 1809
be3492ec 1810 return 0;
7cc84d24 1811}
1812
be3492ec
KS
1813/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
1814 * @param v VC1Context
1815 * @param block block to decode
1816 * @param coded are AC coeffs present or not
1817 * @param mquant block quantizer
1818 * @param codingset set of VLC to decode data
7cc84d24 1819 */
be3492ec 1820static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
e5540b3f 1821{
0d33db8a 1822 GetBitContext *gb = &v->s.gb;
7cc84d24 1823 MpegEncContext *s = &v->s;
be3492ec 1824 int dc_pred_dir = 0; /* Direction of the DC prediction used */
7cc84d24 1825 int run_diff, i;
be3492ec
KS
1826 int16_t *dc_val;
1827 int16_t *ac_val, *ac_val2;
1828 int dcdiff;
1829 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
f26c2ef5 1830 int a_avail = v->a_avail, c_avail = v->c_avail;
e5540b3f 1831
8da75fb2 1832 /* XXX: Guard against dumb values of mquant */
1833 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1834
be3492ec
KS
1835 /* Set DC scale - y and c use the same */
1836 s->y_dc_scale = s->y_dc_scale_table[mquant];
1837 s->c_dc_scale = s->c_dc_scale_table[mquant];
1838
be3492ec
KS
1839 /* Get DC differential */
1840 if (n < 4) {
1841 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1842 } else {
1843 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1844 }
1845 if (dcdiff < 0){
1846 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1847 return -1;
1848 }
1849 if (dcdiff)
1850 {
1851 if (dcdiff == 119 /* ESC index value */)
1852 {
1853 /* TODO: Optimize */
1854 if (mquant == 1) dcdiff = get_bits(gb, 10);
1855 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1856 else dcdiff = get_bits(gb, 8);
1857 }
1858 else
1859 {
1860 if (mquant == 1)
1861 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1862 else if (mquant == 2)
1863 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
1864 }
1865 if (get_bits(gb, 1))
1866 dcdiff = -dcdiff;
1867 }
1868
1869 /* Prediction */
1870 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1871 *dc_val = dcdiff;
1872
1873 /* Store the quantized DC coeff, used for prediction */
1874
1875 if (n < 4) {
1876 block[0] = dcdiff * s->y_dc_scale;
1877 } else {
1878 block[0] = dcdiff * s->c_dc_scale;
1879 }
1880 /* Skip ? */
1881 run_diff = 0;
1882 i = 0;
1883 if (!coded) {
1884 goto not_coded;
1885 }
1886
1887 //AC Decoding
1888 i = 1;
8da75fb2 1889
e5540b3f 1890 {
be3492ec
KS
1891 int last = 0, skip, value;
1892 const int8_t *zz_table;
1893 int scale;
1894 int k;
8f8d0e48 1895 int use_pred = s->ac_pred;
7cc84d24 1896
f26c2ef5 1897 scale = mquant * 2;
be3492ec 1898
8985c915 1899 zz_table = vc1_simple_progressive_8x8_zz;
be3492ec
KS
1900
1901 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1902 ac_val2 = ac_val;
8f8d0e48
KS
1903 if(!a_avail) dc_pred_dir = 1;
1904 if(!c_avail) dc_pred_dir = 0;
1905 if(!a_avail && !c_avail) use_pred = 0;
1906
be3492ec
KS
1907 if(dc_pred_dir) //left
1908 ac_val -= 16;
1909 else //top
1910 ac_val -= 16 * s->block_wrap[n];
1911
1912 while (!last) {
1913 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1914 i += skip;
1915 if(i > 63)
1916 break;
1917 block[zz_table[i++]] = value;
e5540b3f 1918 }
7cc84d24 1919
be3492ec 1920 /* apply AC prediction if needed */
8f8d0e48 1921 if(use_pred) {
be3492ec
KS
1922 /* scale predictors if needed*/
1923 int mb_pos2, q1, q2;
42cc17f9 1924
be3492ec
KS
1925 mb_pos2 = mb_pos - dc_pred_dir - (1 - dc_pred_dir) * s->mb_stride;
1926 q1 = s->current_picture.qscale_table[mb_pos];
1927 q2 = s->current_picture.qscale_table[mb_pos2];
1928
87dfe848 1929 if(q2 && q1 != q2) {
be3492ec
KS
1930 q1 = q1 * 2 - 1;
1931 q2 = q2 * 2 - 1;
1932
1933 if(dc_pred_dir) { //left
1934 for(k = 1; k < 8; k++)
1935 block[k << 3] += (ac_val[k] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1936 } else { //top
1937 for(k = 1; k < 8; k++)
1938 block[k] += (ac_val[k + 8] * q2 * vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1939 }
1940 } else {
1941 if(dc_pred_dir) { //left
1942 for(k = 1; k < 8; k++)
1943 block[k << 3] += ac_val[k];
1944 } else { //top
1945 for(k = 1; k < 8; k++)
1946 block[k] += ac_val[k + 8];
1947 }
1948 }
7cc84d24 1949 }
be3492ec
KS
1950 /* save AC coeffs for further prediction */
1951 for(k = 1; k < 8; k++) {
1952 ac_val2[k] = block[k << 3];
1953 ac_val2[k + 8] = block[k];
e5540b3f 1954 }
7cc84d24 1955
be3492ec
KS
1956 /* scale AC coeffs */
1957 for(k = 1; k < 64; k++)
1958 if(block[k]) {
1959 block[k] *= scale;
1960 if(!v->pquantizer)
1961 block[k] += (block[k] < 0) ? -mquant : mquant;
1962 }
7cc84d24 1963
8f8d0e48 1964 if(use_pred) i = 63;
7cc84d24 1965 }
1966
be3492ec
KS
1967not_coded:
1968 if(!coded) {
1969 int k, scale;
8f8d0e48
KS
1970 int use_pred = s->ac_pred;
1971
be3492ec
KS
1972 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1973 ac_val2 = ac_val;
1974
1975 if(!c_avail) {
1976 memset(ac_val, 0, 8 * sizeof(ac_val[0]));
1977 dc_pred_dir = 0;
1978 }
1979 if(!a_avail) {
1980 memset(ac_val + 8, 0, 8 * sizeof(ac_val[0]));
1981 dc_pred_dir = 1;
1982 }
e5540b3f 1983
8f8d0e48
KS
1984 if(!a_avail && !c_avail) use_pred = 0;
1985
f26c2ef5 1986 scale = mquant * 2;
be3492ec
KS
1987 memset(ac_val2, 0, 16 * 2);
1988 if(dc_pred_dir) {//left
1989 ac_val -= 16;
8f8d0e48 1990 if(use_pred)
be3492ec
KS
1991 memcpy(ac_val2, ac_val, 8 * 2);
1992 } else {//top
1993 ac_val -= 16 * s->block_wrap[n];
8f8d0e48 1994 if(use_pred)
be3492ec
KS
1995 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1996 }
7cc84d24 1997
be3492ec 1998 /* apply AC prediction if needed */
8f8d0e48 1999 if(use_pred) {
be3492ec
KS
2000 if(dc_pred_dir) { //left
2001 for(k = 1; k < 8; k++) {
2002 block[k << 3] = ac_val[k] * scale;
2003 if(!v->pquantizer)
2004 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
2005 }
2006 } else { //top
2007 for(k = 1; k < 8; k++) {
2008 block[k] = ac_val[k + 8] * scale;
2009 if(!v->pquantizer)
2010 block[k] += (block[k] < 0) ? -mquant : mquant;
2011 }
2012 }
2013 i = 63;
7cc84d24 2014 }
2015 }
7cc84d24 2016 s->block_last_index[n] = i;
be3492ec 2017
e5540b3f 2018 return 0;
2019}
7cc84d24 2020
be3492ec 2021/** Decode P block
2ce151f8 2022 */
be3492ec 2023static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block)
7cc84d24 2024{
be3492ec
KS
2025 MpegEncContext *s = &v->s;
2026 GetBitContext *gb = &s->gb;
2027 int i, j;
2028 int subblkpat = 0;
2029 int scale, off, idx, last, skip, value;
2030 int ttblk = ttmb & 7;
42cc17f9 2031
be3492ec
KS
2032 if(ttmb == -1) {
2033 ttblk = ttblk_to_tt[v->tt_index][get_vlc2(gb, vc1_ttblk_vlc[v->tt_index].table, VC1_TTBLK_VLC_BITS, 1)];
7cc84d24 2034 }
be3492ec
KS
2035 if(ttblk == TT_4X4) {
2036 subblkpat = ~(get_vlc2(gb, vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2037 }
2038 if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2039 subblkpat = decode012(gb);
f26c2ef5 2040 if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
be3492ec
KS
2041 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2042 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2043 }
2044 scale = 2 * mquant;
42cc17f9 2045
be3492ec
KS
2046 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2047 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
be3492ec 2048 subblkpat = 2 - (ttblk == TT_8X4_TOP);
e9f2396e 2049 ttblk = TT_8X4;
be3492ec
KS
2050 }
2051 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
be3492ec 2052 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
e9f2396e 2053 ttblk = TT_4X8;
be3492ec 2054 }
be3492ec
KS
2055 switch(ttblk) {
2056 case TT_8X8:
2057 i = 0;
2058 last = 0;
2059 while (!last) {
2060 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2061 i += skip;
2062 if(i > 63)
2063 break;
2064 idx = vc1_simple_progressive_8x8_zz[i++];
2065 block[idx] = value * scale;
7cc84d24 2066 }
be3492ec
KS
2067 vc1_inv_trans(block, 8, 8);
2068 break;
2069 case TT_4X4:
2070 for(j = 0; j < 4; j++) {
2071 last = subblkpat & (1 << (3 - j));
2072 i = 0;
87dfe848 2073 off = (j & 1) * 4 + (j & 2) * 16;
be3492ec
KS
2074 while (!last) {
2075 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2076 i += skip;
2077 if(i > 15)
2078 break;
2079 idx = vc1_simple_progressive_4x4_zz[i++];
2080 block[idx + off] = value * scale;
2081 }
87dfe848
KS
2082 if(!(subblkpat & (1 << (3 - j))))
2083 vc1_inv_trans(block + off, 4, 4);
be3492ec
KS
2084 }
2085 break;
2086 case TT_8X4:
2087 for(j = 0; j < 2; j++) {
2088 last = subblkpat & (1 << (1 - j));
2089 i = 0;
2090 off = j * 32;
2091 while (!last) {
2092 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2093 i += skip;
2094 if(i > 31)
2095 break;
2096 idx = vc1_simple_progressive_8x4_zz[i++];
2097 block[idx + off] = value * scale;
2098 }
87dfe848
KS
2099 if(!(subblkpat & (1 << (1 - j))))
2100 vc1_inv_trans(block + off, 8, 4);
be3492ec
KS
2101 }
2102 break;
2103 case TT_4X8:
2104 for(j = 0; j < 2; j++) {
2105 last = subblkpat & (1 << (1 - j));
2106 i = 0;
2107 off = j * 4;
2108 while (!last) {
2109 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2110 i += skip;
2111 if(i > 31)
2112 break;
c436ff41 2113 idx = vc1_simple_progressive_4x8_zz[i++];
be3492ec
KS
2114 block[idx + off] = value * scale;
2115 }
87dfe848
KS
2116 if(!(subblkpat & (1 << (1 - j))))
2117 vc1_inv_trans(block + off, 4, 8);
8da75fb2 2118 }
be3492ec 2119 break;
7cc84d24 2120 }
2121 return 0;
2122}
2123
be3492ec 2124
8da75fb2 2125/** Decode one P-frame MB (in Simple/Main profile)
2126 * @todo TODO: Extend to AP
2127 * @fixme FIXME: DC value for inter blocks not set
2128 */
10b9c374 2129static int vc1_decode_p_mb(VC1Context *v, DCTELEM block[6][64])
7cc84d24 2130{
2131 MpegEncContext *s = &v->s;
2132 GetBitContext *gb = &s->gb;
87dfe848 2133 int i, j;
be3492ec 2134 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
7cc84d24 2135 int cbp; /* cbp decoding stuff */
2136 int hybrid_pred; /* Prediction types */
7cc84d24 2137 int mqdiff, mquant; /* MB quantization */
be3492ec 2138 int ttmb = v->ttmb; /* MB Transform type */
7cc84d24 2139 int status;
7cc84d24 2140
2141 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
2142 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2143 int mb_has_coeffs = 1; /* last_flag */
2144 int dmv_x, dmv_y; /* Differential MV components */
2145 int index, index1; /* LUT indices */
2146 int val, sign; /* temp values */
be3492ec
KS
2147 int first_block = 1;
2148 int dst_idx, off;
87dfe848 2149 int skipped, fourmv;
7cc84d24 2150
8da75fb2 2151 mquant = v->pq; /* Loosy initialization */
2152
87dfe848
KS
2153 if (v->mv_type_is_raw)
2154 fourmv = get_bits1(gb);
2155 else
2156 fourmv = v->mv_type_mb_plane[mb_pos];
2157 if (v->skip_is_raw)
2158 skipped = get_bits1(gb);
2159 else
0f7344aa 2160 skipped = v->s.mbskip_table[mb_pos];
87dfe848
KS
2161
2162 if (!fourmv) /* 1MV mode */
7cc84d24 2163 {
87dfe848 2164 if (!skipped)
7cc84d24 2165 {
2166 GET_MVDATA(dmv_x, dmv_y);
42cc17f9 2167
be3492ec
KS
2168 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2169 vc1_pred_mv(s, dmv_x, dmv_y, 1, v->range_x, v->range_y);
2170
8da75fb2 2171 /* FIXME Set DC val for inter block ? */
7cc84d24 2172 if (s->mb_intra && !mb_has_coeffs)
2173 {
2174 GET_MQUANT();
2175 s->ac_pred = get_bits(gb, 1);
8da75fb2 2176 cbp = 0;
7cc84d24 2177 }
2178 else if (mb_has_coeffs)
2179 {
2180 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
10b9c374 2181 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
7cc84d24 2182 GET_MQUANT();
2183 }
2184 else
2185 {
2186 mquant = v->pq;
8da75fb2 2187 cbp = 0;
7cc84d24 2188 }
be3492ec 2189 s->current_picture.qscale_table[mb_pos] = mquant;
7cc84d24 2190
be3492ec 2191 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
10b9c374 2192 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
be3492ec
KS
2193 VC1_TTMB_VLC_BITS, 2);
2194 s->dsp.clear_blocks(block[0]);
2195 vc1_mc_1mv(v);
2196 dst_idx = 0;
7cc84d24 2197 for (i=0; i<6; i++)
2198 {
be3492ec
KS
2199 s->dc_val[0][s->block_index[i]] = 0;
2200 dst_idx += i >> 2;
7cc84d24 2201 val = ((cbp >> (5 - i)) & 1);
be3492ec
KS
2202 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2203 if(s->mb_intra) {
f26c2ef5
KS
2204 /* check if prediction blocks A and C are available */
2205 v->a_avail = v->c_avail = 0;
2206 if((i == 2 || i == 3) || (s->mb_y && IS_INTRA(s->current_picture.mb_type[mb_pos - s->mb_stride])))
2207 v->a_avail = 1;
2208 if((i == 1 || i == 3) || (s->mb_x && IS_INTRA(s->current_picture.mb_type[mb_pos - 1])))
2209 v->c_avail = 1;
2210
be3492ec 2211 vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
87dfe848
KS
2212 vc1_inv_trans(block[i], 8, 8);
2213 for(j = 0; j < 64; j++) block[i][j] += 128;
2214 s->dsp.put_pixels_clamped(block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
f26c2ef5 2215 /* TODO: proper loop filtering */
87dfe848
KS
2216 if(v->pq >= 9 && v->overlap) {
2217 if(v->a_avail)
2218 s->dsp.h263_v_loop_filter(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), s->y_dc_scale);
2219 if(v->c_avail)
2220 s->dsp.h263_h_loop_filter(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2), s->y_dc_scale);
2221 }
be3492ec
KS
2222 } else if(val) {
2223 vc1_decode_p_block(v, block[i], i, mquant, ttmb, first_block);
2224 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2225 first_block = 0;
87dfe848 2226 s->dsp.add_pixels_clamped(block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
7cc84d24 2227 }
7cc84d24 2228 }
2229 }
2230 else //Skipped
2231 {
be3492ec
KS
2232 s->mb_intra = 0;
2233 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2234 vc1_pred_mv(s, 0, 0, 1, v->range_x, v->range_y);
2235 vc1_mc_1mv(v);
7cc84d24 2236 return 0;
2237 }
2238 } //1MV mode
2239 else //4MV mode
be3492ec 2240 {//FIXME: looks not conforming to standard and is not even theoretically complete
87dfe848 2241 if (!skipped /* unskipped MB */)
7cc84d24 2242 {
be3492ec 2243 int blk_intra[4], blk_coded[4];
7cc84d24 2244 /* Get CBPCY */
10b9c374 2245 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
be3492ec 2246 for (i=0; i<4; i++)
7cc84d24 2247 {
2248 val = ((cbp >> (5 - i)) & 1);
be3492ec
KS
2249 blk_intra[i] = 0;
2250 blk_coded[i] = val;
2251 if(val) {
7cc84d24 2252 GET_MVDATA(dmv_x, dmv_y);
be3492ec 2253 blk_intra[i] = s->mb_intra;
7cc84d24 2254 }
2255 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
2256 hybrid_pred = get_bits(gb, 1);
be3492ec
KS
2257 }
2258 if((blk_intra[0] | blk_intra[1] | blk_intra[2] | blk_intra[3]) ||
2259 (blk_coded[0] | blk_coded[1] | blk_coded[2] | blk_coded[3])) {
7cc84d24 2260 GET_MQUANT();
8da75fb2 2261
be3492ec
KS
2262 if (s->mb_intra /* One of the 4 blocks is intra */
2263 /* non-zero pred for that block */)
7cc84d24 2264 s->ac_pred = get_bits(gb, 1);
2265 if (!v->ttmbf)
10b9c374
KS
2266 ttmb = get_vlc2(gb, vc1_ttmb_vlc[v->tt_index].table,
2267 VC1_TTMB_VLC_BITS, 12);
be3492ec
KS
2268 for(i = 0; i < 6; i++) {
2269 val = ((cbp >> (5 - i)) & 1);
2270 if(i & 4 || blk_intra[i] || val) {
2271 if(i < 4 && blk_intra[i])
2272 status = vc1_decode_intra_block(v, block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2273 else
2274 status = vc1_decode_p_block(v, block[i], i, mquant, ttmb, 0);
2275 }
2276 }
7cc84d24 2277 }
2278 return status;
2279 }
2280 else //Skipped MB
2281 {
8da75fb2 2282 /* XXX: Skipped => cbp=0 and mquant doesn't matter ? */
7cc84d24 2283 for (i=0; i<4; i++)
2284 {
2285 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
2286 hybrid_pred = get_bits(gb, 1);
7cc84d24 2287 }
2288 /* TODO: blah */
2289 return 0;
2290 }
2291 }
42cc17f9 2292
7cc84d24 2293 /* Should never happen */
2294 return -1;
2295}
2296
be3492ec 2297/** Decode blocks of I-frame
8da75fb2 2298 */
be3492ec 2299static void vc1_decode_i_blocks(VC1Context *v)
7cc84d24 2300{
be3492ec 2301 int k;
8da75fb2 2302 MpegEncContext *s = &v->s;
be3492ec
KS
2303 int cbp, val;
2304 uint8_t *coded_val;
2305 int mb_pos;
42cc17f9 2306
be3492ec
KS
2307 /* select codingmode used for VLC tables selection */
2308 switch(v->y_ac_table_index){
2309 case 0:
2310 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2311 break;
2312 case 1:
2313 v->codingset = CS_HIGH_MOT_INTRA;
2314 break;
2315 case 2:
2316 v->codingset = CS_MID_RATE_INTRA;
2317 break;
2318 }
42cc17f9 2319
be3492ec
KS
2320 switch(v->c_ac_table_index){
2321 case 0:
2322 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2323 break;
2324 case 1:
2325 v->codingset2 = CS_HIGH_MOT_INTER;
2326 break;
2327 case 2:
2328 v->codingset2 = CS_MID_RATE_INTER;
2329 break;
2330 }
8da75fb2 2331
be3492ec
KS
2332 /* Set DC scale - y and c use the same */
2333 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2334 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2335
2336 //do frame decode
2337 s->mb_x = s->mb_y = 0;
2338 s->mb_intra = 1;
2339 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2340 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2341 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
2342 ff_init_block_index(s);
2343 ff_update_block_index(s);
2344 s->dsp.clear_blocks(s->block[0]);
2345 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2346 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2347 s->current_picture.qscale_table[mb_pos] = v->pq;
2348
2349 // do actual MB decoding and displaying
2350 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2351 v->s.ac_pred = get_bits(&v->s.gb, 1);
2352
2353 for(k = 0; k < 6; k++) {
2354 val = ((cbp >> (5 - k)) & 1);
f26c2ef5 2355
be3492ec
KS
2356 if (k < 4) {
2357 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2358 val = val ^ pred;
2359 *coded_val = val;
2360 }
2361 cbp |= val << (5 - k);
42cc17f9 2362
be3492ec
KS
2363 vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
2364
2365 vc1_inv_trans(s->block[k], 8, 8);
2366 if(v->pq >= 9 && v->overlap) {
2367 vc1_overlap_block(s, s->block[k], k, (s->mb_y || k>1), (s->mb_x || (k != 0 && k != 2)));
2368 }
8da75fb2 2369 }
8da75fb2 2370
be3492ec 2371 vc1_put_block(v, s->block);
f26c2ef5
KS
2372 if(v->pq >= 9 && v->overlap) { /* XXX: do proper overlapping insted of loop filter */
2373 if(s->mb_y) {
2374 s->dsp.h263_v_loop_filter(s->dest[0], s->linesize, s->y_dc_scale);
2375 s->dsp.h263_v_loop_filter(s->dest[0] + 8, s->linesize, s->y_dc_scale);
2376 s->dsp.h263_v_loop_filter(s->dest[1], s->uvlinesize, s->y_dc_scale);
2377 s->dsp.h263_v_loop_filter(s->dest[2], s->uvlinesize, s->y_dc_scale);
2378 }
2379 s->dsp.h263_v_loop_filter(s->dest[0] + 8 * s->linesize, s->linesize, s->y_dc_scale);
2380 s->dsp.h263_v_loop_filter(s->dest[0] + 8 * s->linesize + 8, s->linesize, s->y_dc_scale);
2381 if(s->mb_x) {
2382 s->dsp.h263_h_loop_filter(s->dest[0], s->linesize, s->y_dc_scale);
2383 s->dsp.h263_h_loop_filter(s->dest[0] + 8 * s->linesize, s->linesize, s->y_dc_scale);
2384 s->dsp.h263_h_loop_filter(s->dest[1], s->uvlinesize, s->y_dc_scale);
2385 s->dsp.h263_h_loop_filter(s->dest[2], s->uvlinesize, s->y_dc_scale);
2386 }
2387 s->dsp.h263_h_loop_filter(s->dest[0] + 8, s->linesize, s->y_dc_scale);
2388 s->dsp.h263_h_loop_filter(s->dest[0] + 8 * s->linesize + 8, s->linesize, s->y_dc_scale);
2389 }
8da75fb2 2390
be3492ec
KS
2391 if(get_bits_count(&s->gb) > v->bits) {
2392 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2393 return;
2394 }
2395 }
2396 ff_draw_horiz_band(s, s->mb_y * 16, 16);
7cc84d24 2397 }
7cc84d24 2398}
2399
be3492ec 2400static void vc1_decode_p_blocks(VC1Context *v)
7cc84d24 2401{
7cc84d24 2402 MpegEncContext *s = &v->s;
42cc17f9 2403
be3492ec
KS
2404 /* select codingmode used for VLC tables selection */
2405 switch(v->c_ac_table_index){
2406 case 0:
2407 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2408 break;
2409 case 1:
2410 v->codingset = CS_HIGH_MOT_INTRA;
2411 break;
2412 case 2:
2413 v->codingset = CS_MID_RATE_INTRA;
2414 break;
7cc84d24 2415 }
42cc17f9 2416
be3492ec
KS
2417 switch(v->c_ac_table_index){
2418 case 0:
2419 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2420 break;
2421 case 1:
2422 v->codingset2 = CS_HIGH_MOT_INTER;
2423 break;
2424 case 2:
2425 v->codingset2 = CS_MID_RATE_INTER;
2426 break;
7cc84d24 2427 }
2428
be3492ec
KS
2429 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2430 s->first_slice_line = 1;
2431 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2432 for(s->mb_x = 0; s->mb_x < s->mb_width; s->mb_x++) {
2433 ff_init_block_index(s);
2434 ff_update_block_index(s);
2435 s->dsp.clear_blocks(s->block[0]);
2436
2437 vc1_decode_p_mb(v, s->block);
2438 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2439 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);
2440 return;
7cc84d24 2441 }
2442 }
be3492ec
KS
2443 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2444 s->first_slice_line = 0;
7cc84d24 2445 }
7cc84d24 2446}
21aa398f 2447
be3492ec 2448static void vc1_decode_blocks(VC1Context *v)
21aa398f 2449{
21aa398f 2450
be3492ec 2451 v->s.esc3_level_length = 0;
e5540b3f 2452
be3492ec
KS
2453 switch(v->s.pict_type) {
2454 case I_TYPE:
2455 vc1_decode_i_blocks(v);
2456 break;
2457 case P_TYPE:
2458 vc1_decode_p_blocks(v);
2459 break;
21aa398f 2460 }
21aa398f 2461}
be3492ec 2462
21aa398f 2463
10b9c374
KS
2464/** Initialize a VC1/WMV3 decoder
2465 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2ce151f8 2466 * @todo TODO: Decypher remaining bits in extra_data
2467 */
10b9c374 2468static int vc1_decode_init(AVCodecContext *avctx)
21aa398f 2469{
10b9c374 2470 VC1Context *v = avctx->priv_data;
0d33db8a 2471 MpegEncContext *s = &v->s;
21aa398f
AB
2472 GetBitContext gb;
2473
2474 if (!avctx->extradata_size || !avctx->extradata) return -1;
2475 avctx->pix_fmt = PIX_FMT_YUV420P;
0d33db8a 2476 v->s.avctx = avctx;
87dfe848
KS
2477 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2478 v->s.flags |= CODEC_FLAG_EMU_EDGE;
21aa398f 2479
0d33db8a 2480 if(ff_h263_decode_init(avctx) < 0)
2481 return -1;
10b9c374 2482 if (vc1_init_common(v) < 0) return -1;
21aa398f 2483
0ede2c55 2484 av_log(avctx, AV_LOG_INFO, "This decoder is not supposed to produce picture. Dont report this as a bug!\n");
be3492ec 2485 av_log(avctx, AV_LOG_INFO, "If you see a picture, don't believe your eyes.\n");
0ede2c55 2486
e5540b3f 2487 avctx->coded_width = avctx->width;
2488 avctx->coded_height = avctx->height;
21aa398f
AB
2489 if (avctx->codec_id == CODEC_ID_WMV3)
2490 {
2491 int count = 0;
2492
0d33db8a 2493 // looks like WMV3 has a sequence header stored in the extradata
2494 // advanced sequence header may be before the first frame
2495 // the last byte of the extradata is a version number, 1 for the
2496 // samples we can decode
21aa398f 2497
7cc84d24 2498 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
42cc17f9 2499
7cc84d24 2500 if (decode_sequence_header(avctx, &gb) < 0)
2501 return -1;
21aa398f 2502
0d33db8a 2503 count = avctx->extradata_size*8 - get_bits_count(&gb);
2504 if (count>0)
2505 {
2506 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
2507 count, get_bits(&gb, count));
2508 }
7cc84d24 2509 else if (count < 0)
0d33db8a 2510 {
2511 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
2512 }
21aa398f 2513 }
0d33db8a 2514 avctx->has_b_frames= !!(avctx->max_b_frames);
21aa398f 2515
0d33db8a 2516 s->mb_width = (avctx->coded_width+15)>>4;
2517 s->mb_height = (avctx->coded_height+15)>>4;
21aa398f
AB
2518
2519 /* Allocate mb bitplanes */
87dfe848 2520 v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
e5540b3f 2521
be3492ec 2522 /* Init coded blocks info */
7cc84d24 2523 if (v->profile == PROFILE_ADVANCED)
21aa398f 2524 {
87dfe848
KS
2525// if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
2526// return -1;
2527// if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
2528// return -1;
21aa398f 2529 }
21aa398f
AB
2530
2531 return 0;
be3492ec
KS
2532}
2533
21aa398f 2534
10b9c374
KS
2535/** Decode a VC1/WMV3 frame
2536 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2ce151f8 2537 * @warning Initial try at using MpegEncContext stuff
2538 */
10b9c374 2539static int vc1_decode_frame(AVCodecContext *avctx,
21aa398f
AB
2540 void *data, int *data_size,
2541 uint8_t *buf, int buf_size)
2542{
10b9c374 2543 VC1Context *v = avctx->priv_data;
0d33db8a 2544 MpegEncContext *s = &v->s;
21aa398f 2545 AVFrame *pict = data;
0d33db8a 2546
2547 /* no supplementary picture */
2548 if (buf_size == 0) {
2549 /* special case for last picture */
2550 if (s->low_delay==0 && s->next_picture_ptr) {
2551 *pict= *(AVFrame*)s->next_picture_ptr;
2552 s->next_picture_ptr= NULL;
2553
2554 *data_size = sizeof(AVFrame);
2555 }
2556
2557 return 0;
2558 }
2559
0d33db8a 2560 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
2561 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
be3492ec
KS
2562 int i= ff_find_unused_picture(s, 0);
2563 s->current_picture_ptr= &s->picture[i];
0d33db8a 2564 }
2565
be3492ec 2566 avctx->has_b_frames= !s->low_delay;
0d33db8a 2567
be3492ec
KS
2568 init_get_bits(&s->gb, buf, buf_size*8);
2569 // do parse frame header
2570 if(vc1_parse_frame_header(v, &s->gb) == -1)
2571 return -1;
2572
2573 if(s->pict_type != I_TYPE && s->pict_type != P_TYPE)return -1;
0d33db8a 2574
2575 // for hurry_up==5
2576 s->current_picture.pict_type= s->pict_type;
2577 s->current_picture.key_frame= s->pict_type == I_TYPE;
2578
be3492ec
KS
2579 /* skip B-frames if we don't have reference frames */
2580 if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return -1;//buf_size;
0d33db8a 2581 /* skip b frames if we are in a hurry */
be3492ec
KS
2582 if(avctx->hurry_up && s->pict_type==B_TYPE) return -1;//buf_size;
2583 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
2584 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
2585 || avctx->skip_frame >= AVDISCARD_ALL)
2586 return buf_size;
0d33db8a 2587 /* skip everything if we are in a hurry>=5 */
be3492ec 2588 if(avctx->hurry_up>=5) return -1;//buf_size;
42cc17f9 2589
0d33db8a 2590 if(s->next_p_frame_damaged){
2591 if(s->pict_type==B_TYPE)
be3492ec 2592 return buf_size;
0d33db8a 2593 else
2594 s->next_p_frame_damaged=0;
2595 }
2596
2597 if(MPV_frame_start(s, avctx) < 0)
2598 return -1;
2599
2600 ff_er_frame_start(s);
2601
be3492ec
KS
2602 v->bits = buf_size * 8;
2603 vc1_decode_blocks(v);
2604//av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
2605// if(get_bits_count(&s->gb) > buf_size * 8)
2606// return -1;
0d33db8a 2607 ff_er_frame_end(s);
2608
2609 MPV_frame_end(s);
2610
be3492ec
KS
2611assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
2612assert(s->current_picture.pict_type == s->pict_type);
5430b070
MN
2613 if (s->pict_type == B_TYPE || s->low_delay) {
2614 *pict= *(AVFrame*)s->current_picture_ptr;
2615 } else if (s->last_picture_ptr != NULL) {
2616 *pict= *(AVFrame*)s->last_picture_ptr;
2617 }
2618
2619 if(s->last_picture_ptr || s->low_delay){
2620 *data_size = sizeof(AVFrame);
0d33db8a 2621 ff_print_debug_info(s, pict);
0d33db8a 2622 }
2623
2624 /* Return the Picture timestamp as the frame number */
2625 /* we substract 1 because it is added on utils.c */
2626 avctx->frame_number = s->picture_number - 1;
2627
be3492ec 2628 return buf_size;
21aa398f
AB
2629}
2630
be3492ec 2631
10b9c374 2632/** Close a VC1/WMV3 decoder
2ce151f8 2633 * @warning Initial try at using MpegEncContext stuff
2634 */
10b9c374 2635static int vc1_decode_end(AVCodecContext *avctx)
21aa398f 2636{
10b9c374 2637 VC1Context *v = avctx->priv_data;
21aa398f 2638
e9025600
MN
2639 av_freep(&v->hrd_rate);
2640 av_freep(&v->hrd_buffer);
0d33db8a 2641 MPV_common_end(&v->s);
87dfe848 2642 av_freep(&v->mv_type_mb_plane);
21aa398f
AB
2643 return 0;
2644}
2645
be3492ec 2646
10b9c374
KS
2647AVCodec vc1_decoder = {
2648 "vc1",
21aa398f 2649 CODEC_TYPE_VIDEO,
10b9c374
KS
2650 CODEC_ID_VC1,
2651 sizeof(VC1Context),
2652 vc1_decode_init,
21aa398f 2653 NULL,
10b9c374
KS
2654 vc1_decode_end,
2655 vc1_decode_frame,
934982c4 2656 CODEC_CAP_DELAY,
21aa398f
AB
2657 NULL
2658};
2659
2660AVCodec wmv3_decoder = {
2661 "wmv3",
2662 CODEC_TYPE_VIDEO,
2663 CODEC_ID_WMV3,
10b9c374
KS
2664 sizeof(VC1Context),
2665 vc1_decode_init,
21aa398f 2666 NULL,
10b9c374
KS
2667 vc1_decode_end,
2668 vc1_decode_frame,
934982c4 2669 CODEC_CAP_DELAY,
21aa398f
AB
2670 NULL
2671};