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