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