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