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