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