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