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