comparison vc9.c @ 2445:96da66323faa libavcodec

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