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