2 * VC-9 and WMV3 decoder
3 * Copyright (c) 2005 Anonymous
4 * Copyright (c) 2005 Alex Beregszaszi
5 * Copyright (c) 2005 Michael Niedermayer
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * VC-9 and WMV3 decoder
27 * TODO: most AP stuff, optimize, most of MB layer, transform, filtering and motion compensation, etc
33 #include "mpegvideo.h"
39 extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2];
40 extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2];
41 extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
42 #define MB_INTRA_VLC_BITS 9
43 extern VLC ff_msmp4_mb_i_vlc;
45 static const uint16_t table_mb_intra[64][2];
47 /* Some inhibiting stuff */
48 #define HAS_ADVANCED_PROFILE 0
52 # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
53 codes, codes_wrap, codes_size, use_static) \
54 if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
55 codes, codes_wrap, codes_size, use_static) < 0) \
57 av_log(v->s.avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i); \
61 # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
62 codes, codes_wrap, codes_size, use_static) \
63 init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
64 codes, codes_wrap, codes_size, use_static)
67 /** Available Profiles */
69 #define PROFILE_SIMPLE 0
70 #define PROFILE_MAIN 1
71 #define PROFILE_COMPLEX 2 ///< TODO: WMV9 specific
72 #define PROFILE_ADVANCED 3
75 /** Sequence quantizer mode */
77 #define QUANT_FRAME_IMPLICIT 0 ///< Implicitly specified at frame level
78 #define QUANT_FRAME_EXPLICIT 1 ///< Explicitly specified at frame level
79 #define QUANT_NON_UNIFORM 2 ///< Non-uniform quant used for all frames
80 #define QUANT_UNIFORM 3 ///< Uniform quant used for all frames
83 /** Where quant can be changed */
85 #define DQPROFILE_FOUR_EDGES 0
86 #define DQPROFILE_DOUBLE_EDGES 1
87 #define DQPROFILE_SINGLE_EDGE 2
88 #define DQPROFILE_ALL_MBS 3
91 /** @name Where quant can be changed
94 #define DQPROFILE_FOUR_EDGES 0
95 #define DQSINGLE_BEDGE_LEFT 0
96 #define DQSINGLE_BEDGE_TOP 1
97 #define DQSINGLE_BEDGE_RIGHT 2
98 #define DQSINGLE_BEDGE_BOTTOM 3
101 /** Which pair of edges is quantized with ALTPQUANT */
103 #define DQDOUBLE_BEDGE_TOPLEFT 0
104 #define DQDOUBLE_BEDGE_TOPRIGHT 1
105 #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
106 #define DQDOUBLE_BEDGE_BOTTOMLEFT 3
109 /** MV modes for P frames */
111 #define MV_PMODE_1MV_HPEL_BILIN 0
112 #define MV_PMODE_1MV 1
113 #define MV_PMODE_1MV_HPEL 2
114 #define MV_PMODE_MIXED_MV 3
115 #define MV_PMODE_INTENSITY_COMP 4
118 /** @name MV types for B frames */
120 #define BMV_TYPE_BACKWARD 0
121 #define BMV_TYPE_BACKWARD 0
122 #define BMV_TYPE_FORWARD 1
123 #define BMV_TYPE_INTERPOLATED 3
126 /** MV P mode - the 5th element is only used for mode 1 */
127 static const uint8_t mv_pmode_table[2][5] = {
128 { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV, MV_PMODE_INTENSITY_COMP },
129 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
132 /** One more frame type */
135 static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
136 fps_dr[2] = { 1000, 1001 };
137 static const uint8_t pquant_table[3][32] = {
138 { /* Implicit quantizer */
139 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
140 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 31
142 { /* Explicit quantizer, pquantizer uniform */
143 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
144 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
146 { /* Explicit quantizer, pquantizer non-uniform */
147 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
148 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
152 /** @name VC-9 VLC tables and defines
153 * @todo TODO move this into the context
156 #define VC9_BFRACTION_VLC_BITS 7
157 static VLC vc9_bfraction_vlc;
158 #define VC9_IMODE_VLC_BITS 4
159 static VLC vc9_imode_vlc;
160 #define VC9_NORM2_VLC_BITS 3
161 static VLC vc9_norm2_vlc;
162 #define VC9_NORM6_VLC_BITS 9
163 static VLC vc9_norm6_vlc;
164 /* Could be optimized, one table only needs 8 bits */
165 #define VC9_TTMB_VLC_BITS 9 //12
166 static VLC vc9_ttmb_vlc[3];
167 #define VC9_MV_DIFF_VLC_BITS 9 //15
168 static VLC vc9_mv_diff_vlc[4];
169 #define VC9_CBPCY_P_VLC_BITS 9 //14
170 static VLC vc9_cbpcy_p_vlc[4];
171 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6
172 static VLC vc9_4mv_block_pattern_vlc[4];
173 #define VC9_TTBLK_VLC_BITS 5
174 static VLC vc9_ttblk_vlc[3];
175 #define VC9_SUBBLKPAT_VLC_BITS 6
176 static VLC vc9_subblkpat_vlc[3];
180 * We mainly need data and is_raw, so this struct could be avoided
181 * to save a level of indirection; feel free to modify
182 * @fixme For now, stride=width
183 * @warning Data are bits, either 1 or 0
185 typedef struct BitPlane {
186 uint8_t *data; ///< Data buffer
187 int width; ///< Width of the buffer
188 int stride; ///< Stride of the buffer
189 int height; ///< Plane height
190 uint8_t is_raw; ///< Bit values must be read at MB level
194 * @fixme Change size wherever another size is more efficient
195 * Many members are only used for Advanced Profile
197 typedef struct VC9Context{
200 /** Simple/Main Profile sequence header */
202 int res_sm; ///< reserved, 2b
203 int res_x8; ///< reserved
204 int multires; ///< frame-level RESPIC syntax element present
205 int res_fasttx; ///< reserved, always 1
206 int res_transtab; ///< reserved, always 0
207 int rangered; ///< RANGEREDFRM (range reduction) syntax element present
209 int res_rtm_flag; ///< reserved, set to 1
210 int reserved; ///< reserved
213 #if HAS_ADVANCED_PROFILE
214 /** Advanced Profile */
216 int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
217 int chromaformat; ///< 2bits, 2=4:2:0, only defined
218 int postprocflag; ///< Per-frame processing suggestion flag present
219 int broadcast; ///< TFF/RFF present
220 int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
221 int tfcntrflag; ///< TFCNTR present
222 int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
223 int extended_dmv; ///< Additional extended dmv range at P/B frame-level
224 int color_prim; ///< 8bits, chroma coordinates of the color primaries
225 int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
226 int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
227 int hrd_param_flag; ///< Presence of Hypothetical Reference
228 ///< Decoder parameters
233 /** Sequence header data for all Profiles
234 * TODO: choose between ints, uint8_ts and monobit flags
237 int profile; ///< 2bits, Profile
238 int frmrtq_postproc; ///< 3bits,
239 int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
240 int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
241 int extended_mv; ///< Ext MV in P/B (not in Simple)
242 int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
243 int vstransform; ///< variable-size [48]x[48] transform type + info
244 int overlap; ///< overlapped transforms in use
245 int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
246 int finterpflag; ///< INTERPFRM present
249 /** Frame decoding info for all profiles */
251 uint8_t mv_mode; ///< MV coding monde
252 uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
253 int k_x; ///< Number of bits for MVs (depends on MV range)
254 int k_y; ///< Number of bits for MVs (depends on MV range)
255 uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
256 /** pquant parameters */
263 /** AC coding set indexes
264 * @see 8.1.1.10, p(1)10
267 int c_ac_table_index; ///< Chroma index from ACFRM element
268 int y_ac_table_index; ///< Luma index from AC2FRM element
270 int ttfrm; ///< Transform type info present at frame level
271 uint8_t ttmbf; ///< Transform type flag
272 int ttmb; ///< Transform type
273 uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
274 /** Luma compensation parameters */
279 int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
280 uint8_t halfpq; ///< Uniform quant over image and qp+.5
281 uint8_t respic; ///< Frame-level flag for resized images
282 int buffer_fullness; ///< HRD info
284 * -# 0 -> [-64n 63.f] x [-32, 31.f]
285 * -# 1 -> [-128, 127.f] x [-64, 63.f]
286 * -# 2 -> [-512, 511.f] x [-128, 127.f]
287 * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
290 uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
291 uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY
292 VLC *cbpcy_vlc; ///< CBPCY VLC table
293 int tt_index; ///< Index for Transform Type tables
294 BitPlane mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
295 BitPlane skip_mb_plane; ///< bitplane for skipped MBs
296 BitPlane direct_mb_plane; ///< bitplane for "direct" MBs
298 /** Frame decoding info for S/M profiles only */
300 uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
304 #if HAS_ADVANCED_PROFILE
305 /** Frame decoding info for Advanced profile */
307 uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
308 uint8_t numpanscanwin;
310 uint8_t rptfrm, tff, rff;
313 uint16_t bottomrightx;
314 uint16_t bottomrighty;
317 int hrd_num_leaky_buckets;
318 uint8_t bit_rate_exponent;
319 uint8_t buffer_size_exponent;
320 BitPlane ac_pred_plane; ///< AC prediction flags bitplane
321 BitPlane over_flags_plane; ///< Overflags bitplane
323 uint16_t *hrd_rate, *hrd_buffer;
329 * Get unary code of limited length
330 * @fixme FIXME Slow and ugly
331 * @param gb GetBitContext
332 * @param[in] stop The bitstop value (unary code of 1's or 0's)
333 * @param[in] len Maximum length
334 * @return Unary length/index
336 static int get_prefix(GetBitContext *gb, int stop, int len)
339 int i = 0, tmp = !stop;
341 while (i != len && tmp != stop)
343 tmp = get_bits(gb, 1);
346 if (i == len && tmp != stop) return len+1;
353 UPDATE_CACHE(re, gb);
354 buf=GET_CACHE(re, gb); //Still not sure
355 if (stop) buf = ~buf;
357 log= av_log2(-buf); //FIXME: -?
359 LAST_SKIP_BITS(re, gb, log+1);
360 CLOSE_READER(re, gb);
364 LAST_SKIP_BITS(re, gb, limit);
365 CLOSE_READER(re, gb);
371 * Init VC-9 specific tables and VC9Context members
372 * @param v The VC9Context to initialize
375 static int vc9_init_common(VC9Context *v)
380 /* Set the bit planes */
381 v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
382 v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
383 v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
384 #if HAS_ADVANCED_PROFILE
385 v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
386 v->hrd_rate = v->hrd_buffer = NULL;
390 #if 0 // spec -> actual tables converter
392 int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3];
393 av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
394 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
397 int code= vc9_norm6_spec[i][2] + vc9_norm6_spec[i][4];
398 av_log(NULL, AV_LOG_DEBUG, "%2d, ", code);
399 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
405 INIT_VLC(&vc9_bfraction_vlc, VC9_BFRACTION_VLC_BITS, 23,
406 vc9_bfraction_bits, 1, 1,
407 vc9_bfraction_codes, 1, 1, 1);
408 INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4,
409 vc9_norm2_bits, 1, 1,
410 vc9_norm2_codes, 1, 1, 1);
411 INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64,
412 vc9_norm6_bits, 1, 1,
413 vc9_norm6_codes, 2, 2, 1);
414 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7,
415 vc9_imode_bits, 1, 1,
416 vc9_imode_codes, 1, 1, 1);
419 INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16,
420 vc9_ttmb_bits[i], 1, 1,
421 vc9_ttmb_codes[i], 2, 2, 1);
422 INIT_VLC(&vc9_ttblk_vlc[i], VC9_TTBLK_VLC_BITS, 8,
423 vc9_ttblk_bits[i], 1, 1,
424 vc9_ttblk_codes[i], 1, 1, 1);
425 INIT_VLC(&vc9_subblkpat_vlc[i], VC9_SUBBLKPAT_VLC_BITS, 15,
426 vc9_subblkpat_bits[i], 1, 1,
427 vc9_subblkpat_codes[i], 1, 1, 1);
431 INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16,
432 vc9_4mv_block_pattern_bits[i], 1, 1,
433 vc9_4mv_block_pattern_codes[i], 1, 1, 1);
434 INIT_VLC(&vc9_cbpcy_p_vlc[i], VC9_CBPCY_P_VLC_BITS, 64,
435 vc9_cbpcy_p_bits[i], 1, 1,
436 vc9_cbpcy_p_codes[i], 2, 2, 1);
437 INIT_VLC(&vc9_mv_diff_vlc[i], VC9_MV_DIFF_VLC_BITS, 73,
438 vc9_mv_diff_bits[i], 1, 1,
439 vc9_mv_diff_codes[i], 2, 2, 1);
445 v->mvrange = 0; /* 7.1.1.18, p80 */
450 #if HAS_ADVANCED_PROFILE
452 * Decode sequence header's Hypothetic Reference Decoder data
454 * @param v The VC9Context to initialize
455 * @param gb A GetBitContext initialized from AVCodecContext extra_data
458 static int decode_hrd(VC9Context *v, GetBitContext *gb)
462 num = get_bits(gb, 5);
464 if (v->hrd_rate || num != v->hrd_num_leaky_buckets)
466 av_freep(&v->hrd_rate);
468 if (!v->hrd_rate) v->hrd_rate = av_malloc(num*sizeof(uint16_t));
469 if (!v->hrd_rate) return -1;
471 if (v->hrd_buffer || num != v->hrd_num_leaky_buckets)
473 av_freep(&v->hrd_buffer);
475 if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num*sizeof(uint16_t));
476 if (!v->hrd_buffer) return -1;
478 v->hrd_num_leaky_buckets = num;
480 //exponent in base-2 for rate
481 v->bit_rate_exponent = get_bits(gb, 4);
482 //exponent in base-2 for buffer_size
483 v->buffer_size_exponent = get_bits(gb, 4);
485 for (i=0; i<num; i++)
487 //mantissae, ordered (if not, use a function ?
488 v->hrd_rate[i] = get_bits(gb, 16);
489 if (i && v->hrd_rate[i-1]>=v->hrd_rate[i])
491 av_log(v->s.avctx, AV_LOG_ERROR, "HDR Rates aren't strictly increasing:"
492 "%i vs %i\n", v->hrd_rate[i-1], v->hrd_rate[i]);
495 v->hrd_buffer[i] = get_bits(gb, 16);
496 if (i && v->hrd_buffer[i-1]<v->hrd_buffer[i])
498 av_log(v->s.avctx, AV_LOG_ERROR, "HDR Buffers aren't decreasing:"
499 "%i vs %i\n", v->hrd_buffer[i-1], v->hrd_buffer[i]);
507 * Decode sequence header for Advanced Profile
509 * @see 6.1.7, pp21-27
510 * @param v The VC9Context to initialize
511 * @param gb A GetBitContext initialized from AVCodecContext extra_data
514 static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
516 VC9Context *v = avctx->priv_data;
517 int nr, dr, aspect_ratio;
519 v->postprocflag = get_bits(gb, 1);
520 v->broadcast = get_bits(gb, 1);
521 v->interlace = get_bits(gb, 1);
523 v->tfcntrflag = get_bits(gb, 1);
524 v->finterpflag = get_bits(gb, 1); //common
525 v->panscanflag = get_bits(gb, 1);
526 v->reserved = get_bits(gb, 1);
529 av_log(avctx, AV_LOG_ERROR, "RESERVED should be 0 (is %i)\n",
534 v->extended_dmv = get_bits(gb, 1);
537 if (get_bits(gb, 1) /* pic_size_flag */)
539 avctx->coded_width = get_bits(gb, 12);
540 avctx->coded_height = get_bits(gb, 12);
541 if ( get_bits(gb, 1) /* disp_size_flag */)
543 avctx->width = get_bits(gb, 14);
544 avctx->height = get_bits(gb, 14);
548 if ( get_bits(gb, 1) /* aspect_ratio_flag */)
550 aspect_ratio = get_bits(gb, 4); //SAR
551 if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED
553 avctx->sample_aspect_ratio.num = get_bits(gb, 8);
554 avctx->sample_aspect_ratio.den = get_bits(gb, 8);
556 else if (aspect_ratio == 0x0E)
558 av_log(avctx, AV_LOG_DEBUG, "Reserved AR found\n");
562 avctx->sample_aspect_ratio = vc9_pixel_aspect[aspect_ratio];
568 avctx->coded_width = avctx->width;
569 avctx->coded_height = avctx->height;
573 if ( !get_bits(gb, 1) /* framerateflag */)
575 if ( get_bits(gb, 1) /* framerateind */)
577 nr = get_bits(gb, 8);
578 dr = get_bits(gb, 4);
581 av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATENR\n");
586 av_log(avctx, AV_LOG_ERROR,
587 "Reserved FRAMERATENR %i not handled\n", nr);
591 av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATEDR\n");
595 av_log(avctx, AV_LOG_ERROR,
596 "Reserved FRAMERATEDR %i not handled\n", dr);
598 avctx->frame_rate_base = fps_nr[dr];
599 avctx->frame_rate = fps_nr[nr];
603 nr = get_bits(gb, 16);
604 // 0.03125->2048Hz / 0.03125Hz
605 avctx->frame_rate = 1000000;
606 avctx->frame_rate_base = 31250*(1+nr);
611 if ( get_bits(gb, 1) /* color_format_flag */)
613 //Chromacity coordinates of color primaries
614 //like ITU-R BT.709-2, BT.470-2, ...
615 v->color_prim = get_bits(gb, 8);
618 av_log(avctx, AV_LOG_ERROR, "0 for COLOR_PRIM is reserved\n");
621 if (v->color_prim == 3 || v->color_prim>6)
623 av_log(avctx, AV_LOG_DEBUG, "Reserved COLOR_PRIM %i found\n",
628 //Opto-electronic transfer characteristics
629 v->transfer_char = get_bits(gb, 8);
630 if (v->transfer_char == 3 || v->transfer_char>8)
632 av_log(avctx, AV_LOG_DEBUG, "Reserved TRANSFERT_CHAR %i found\n",
637 //Matrix coefficient for primariev->YCbCr
638 v->matrix_coef = get_bits(gb, 8);
639 if (v->matrix_coef < 1) return -1; //forbidden
640 if ((v->matrix_coef>3 && v->matrix_coef<6) || v->matrix_coef>7)
642 av_log(avctx, AV_LOG_DEBUG, "Reserved MATRIX_COEF %i found\n",
648 //Hypothetical reference decoder indicator flag
649 v->hrd_param_flag = get_bits(gb, 1);
650 if (v->hrd_param_flag)
652 if (decode_hrd(v, gb) < 0) return -1;
655 av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
661 * Decode Simple/Main Profiles sequence header
662 * @see Figure 7-8, p16-17
663 * @param avctx Codec context
664 * @param gb GetBit context initialized from Codec context extra_data
667 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
669 VC9Context *v = avctx->priv_data;
671 av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
672 v->profile = get_bits(gb, 2);
674 av_log(avctx, AV_LOG_ERROR, "Profile 2 is reserved\n");
676 #if HAS_ADVANCED_PROFILE
677 if (v->profile == PROFILE_ADVANCED)
679 v->level = get_bits(gb, 3);
680 v->chromaformat = get_bits(gb, 2);
681 if (v->chromaformat != 1)
683 av_log(avctx, AV_LOG_ERROR,
684 "Only 4:2:0 chroma format supported\n");
691 v->res_sm = get_bits(gb, 2); //reserved
694 av_log(avctx, AV_LOG_ERROR,
695 "Reserved RES_SM=%i is forbidden\n", v->res_sm);
701 v->frmrtq_postproc = get_bits(gb, 3); //common
702 // (bitrate-32kbps)/64kbps
703 v->bitrtq_postproc = get_bits(gb, 5); //common
704 v->s.loop_filter = get_bits(gb, 1); //common
706 #if HAS_ADVANCED_PROFILE
707 if (v->profile < PROFILE_ADVANCED)
710 v->res_x8 = get_bits(gb, 1); //reserved
713 av_log(avctx, AV_LOG_ERROR,
714 "1 for reserved RES_X8 is forbidden\n");
717 v->multires = get_bits(gb, 1);
718 v->res_fasttx = get_bits(gb, 1);
721 av_log(avctx, AV_LOG_ERROR,
722 "0 for reserved RES_FASTTX is forbidden\n");
727 v->fastuvmc = get_bits(gb, 1); //common
728 if (!v->profile && !v->fastuvmc)
730 av_log(avctx, AV_LOG_ERROR,
731 "FASTUVMC unavailable in Simple Profile\n");
734 v->extended_mv = get_bits(gb, 1); //common
735 if (!v->profile && v->extended_mv)
737 av_log(avctx, AV_LOG_ERROR,
738 "Extended MVs unavailable in Simple Profile\n");
741 v->dquant = get_bits(gb, 2); //common
742 v->vstransform = get_bits(gb, 1); //common
744 #if HAS_ADVANCED_PROFILE
745 if (v->profile < PROFILE_ADVANCED)
748 v->res_transtab = get_bits(gb, 1);
751 av_log(avctx, AV_LOG_ERROR,
752 "1 for reserved RES_TRANSTAB is forbidden\n");
757 v->overlap = get_bits(gb, 1); //common
759 #if HAS_ADVANCED_PROFILE
760 if (v->profile < PROFILE_ADVANCED)
763 v->s.resync_marker = get_bits(gb, 1);
764 v->rangered = get_bits(gb, 1);
767 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
768 v->quantizer_mode = get_bits(gb, 2); //common
770 #if HAS_ADVANCED_PROFILE
771 if (v->profile < PROFILE_ADVANCED)
774 v->finterpflag = get_bits(gb, 1); //common
775 v->res_rtm_flag = get_bits(gb, 1); //reserved
776 if (!v->res_rtm_flag)
778 av_log(avctx, AV_LOG_ERROR,
779 "0 for reserved RES_RTM_FLAG is forbidden\n");
783 av_log(avctx, AV_LOG_INFO,
784 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
785 "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
786 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
787 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
788 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
789 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
790 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
791 v->dquant, v->quantizer_mode, avctx->max_b_frames
796 #if HAS_ADVANCED_PROFILE
797 else return decode_advanced_sequence_header(avctx, gb);
802 #if HAS_ADVANCED_PROFILE
803 /** Entry point decoding (Advanced Profile)
804 * @param avctx Codec context
805 * @param gb GetBit context initialized from avctx->extra_data
808 static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb)
810 VC9Context *v = avctx->priv_data;
811 int range_mapy_flag, range_mapuv_flag, i;
812 if (v->profile != PROFILE_ADVANCED)
814 av_log(avctx, AV_LOG_ERROR,
815 "Entry point are only defined in Advanced Profile!\n");
816 return -1; //Only for advanced profile!
818 if (v->hrd_param_flag)
820 //Update buffer fullness
821 av_log(avctx, AV_LOG_DEBUG, "Buffer fullness update\n");
822 for (i=0; i<v->hrd_num_leaky_buckets; i++)
825 if ((range_mapy_flag = get_bits(gb, 1)))
828 av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPY\n");
831 if ((range_mapuv_flag = get_bits(gb, 1)))
834 av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPUV\n");
840 v->numpanscanwin = get_bits(gb, 3);
841 av_log(avctx, AV_LOG_DEBUG, "NUMPANSCANWIN: %u\n", v->numpanscanwin);
847 /***********************************************************************/
849 * @defgroup bitplane VC9 Bitplane decoding
854 /** @addtogroup bitplane
859 #define IMODE_NORM2 1
860 #define IMODE_DIFF2 2
861 #define IMODE_NORM6 3
862 #define IMODE_DIFF6 4
863 #define IMODE_ROWSKIP 5
864 #define IMODE_COLSKIP 6
865 /** @} */ //imode defines
867 /** Allocate the buffer from a bitplane, given its dimensions
868 * @param bp Bitplane which buffer is to allocate
869 * @param[in] width Width of the buffer
870 * @param[in] height Height of the buffer
872 * @todo TODO: Take into account stride
873 * @todo TODO: Allow use of external buffers ?
875 int alloc_bitplane(BitPlane *bp, int width, int height)
877 if (!bp || bp->width<0 || bp->height<0) return -1;
878 bp->data = (uint8_t*)av_malloc(width*height);
879 if (!bp->data) return -1;
880 bp->width = bp->stride = width;
885 /** Free the bitplane's buffer
886 * @param bp Bitplane which buffer is to free
888 void free_bitplane(BitPlane *bp)
890 bp->width = bp->stride = bp->height = 0;
891 if (bp->data) av_freep(&bp->data);
894 /** Decode rows by checking if they are skiped
895 * @param plane Buffer to store decoded bits
896 * @param[in] width Width of this buffer
897 * @param[in] height Height of this buffer
898 * @param[in] stride of this buffer
900 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
903 for (y=0; y<height; y++){
904 if (!get_bits(gb, 1)) //rowskip
905 memset(plane, 0, width);
907 for (x=0; x<width; x++)
908 plane[x] = get_bits(gb, 1);
913 /** Decode columns by checking if they are skiped
914 * @param plane Buffer to store decoded bits
915 * @param[in] width Width of this buffer
916 * @param[in] height Height of this buffer
917 * @param[in] stride of this buffer
918 * @fixme FIXME: Optimize
920 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
923 for (x=0; x<width; x++){
924 if (!get_bits(gb, 1)) //colskip
925 for (y=0; y<height; y++)
928 for (y=0; y<height; y++)
929 plane[y*stride] = get_bits(gb, 1);
934 /** Decode a bitplane's bits
935 * @param bp Bitplane where to store the decode bits
936 * @param v VC9 context for bit reading and logging
938 * @fixme FIXME: Optimize
939 * @todo TODO: Decide if a struct is needed
941 static int bitplane_decoding(BitPlane *bp, VC9Context *v)
943 GetBitContext *gb = &v->s.gb;
945 int imode, x, y, code, use_vertical_tile, tile_w, tile_h, offset;
946 uint8_t invert, *planep = bp->data;
948 invert = get_bits(gb, 1);
949 imode = get_vlc2(gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2);
955 //Data is actually read in the MB layer (same for all tests == "raw")
956 bp->is_raw = 1; //invert ignored
960 if ((bp->height*bp->width) & 1)
962 *(++planep) = get_bits(gb, 1);
967 for (y=0; y<bp->height; y++)
969 for(; x<bp->width; x+=2)
971 code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
972 *(++planep) = code&1; //lsb => left
973 *(++planep) = (code>>1)&1; //msb => right
975 planep += bp->stride-bp->width;
976 if ((bp->width-offset)&1) //Odd number previously processed
978 code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
980 planep += bp->stride-bp->width;
981 *planep = (code>>1)&1; //msb => right
987 planep += bp->stride-bp->width;
993 use_vertical_tile= bp->height%3==0 && bp->width%3!=0;
994 tile_w= use_vertical_tile ? 2 : 3;
995 tile_h= use_vertical_tile ? 3 : 2;
997 for(y= bp->height%tile_h; y< bp->height; y+=tile_h){
998 for(x= bp->width%tile_w; x< bp->width; x+=tile_w){
999 code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2);
1001 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
1004 //FIXME following is a pure guess and probably wrong
1005 //FIXME A bitplane (0 | !0), so could the shifts be avoided ?
1006 planep[x + 0*bp->stride]= (code>>0)&1;
1007 planep[x + 1 + 0*bp->stride]= (code>>1)&1;
1008 //FIXME Does branch prediction help here?
1009 if(use_vertical_tile){
1010 planep[x + 0 + 1*bp->stride]= (code>>2)&1;
1011 planep[x + 1 + 1*bp->stride]= (code>>3)&1;
1012 planep[x + 0 + 2*bp->stride]= (code>>4)&1;
1013 planep[x + 1 + 2*bp->stride]= (code>>5)&1;
1015 planep[x + 2 + 0*bp->stride]= (code>>2)&1;
1016 planep[x + 0 + 1*bp->stride]= (code>>3)&1;
1017 planep[x + 1 + 1*bp->stride]= (code>>4)&1;
1018 planep[x + 2 + 1*bp->stride]= (code>>5)&1;
1023 x= bp->width % tile_w;
1024 decode_colskip(bp->data , x, bp->height , bp->stride, &v->s.gb);
1025 decode_rowskip(bp->data+x, bp->width - x, bp->height % tile_h, bp->stride, &v->s.gb);
1029 decode_rowskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
1032 decode_colskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
1037 /* Applying diff operator */
1038 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
1041 planep[0] ^= invert;
1042 for (x=1; x<bp->width; x++)
1043 planep[x] ^= planep[x-1];
1044 for (y=1; y<bp->height; y++)
1046 planep += bp->stride;
1047 planep[0] ^= planep[-bp->stride];
1048 for (x=1; x<bp->width; x++)
1050 if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
1051 else planep[x] ^= planep[x-1];
1058 for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
1060 return (imode<<1) + invert;
1062 /** @} */ //Bitplane group
1064 /***********************************************************************/
1065 /** VOP Dquant decoding
1066 * @param v VC9 Context
1068 static int vop_dquant_decoding(VC9Context *v)
1070 GetBitContext *gb = &v->s.gb;
1076 pqdiff = get_bits(gb, 3);
1077 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
1078 else v->altpq = v->pq + pqdiff + 1;
1082 v->dquantfrm = get_bits(gb, 1);
1085 v->dqprofile = get_bits(gb, 2);
1086 switch (v->dqprofile)
1088 case DQPROFILE_SINGLE_EDGE:
1089 case DQPROFILE_DOUBLE_EDGES:
1090 v->dqsbedge = get_bits(gb, 2);
1092 case DQPROFILE_ALL_MBS:
1093 v->dqbilevel = get_bits(gb, 1);
1094 default: break; //Forbidden ?
1096 if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
1098 pqdiff = get_bits(gb, 3);
1099 if (pqdiff == 7) v->altpq = get_bits(gb, 5);
1100 else v->altpq = v->pq + pqdiff + 1;
1107 /***********************************************************************/
1109 * @defgroup all_frame_hdr All VC9 profiles frame header
1110 * @brief Part of the frame header decoding from all profiles
1111 * @warning Only pro/epilog differs between Simple/Main and Advanced => check caller
1114 /** B and BI frame header decoding, primary part
1115 * @see Tables 11+12, p62-65
1116 * @param v VC9 context
1118 * @warning Also handles BI frames
1120 static int decode_b_picture_primary_header(VC9Context *v)
1122 GetBitContext *gb = &v->s.gb;
1125 /* Prolog common to all frametypes should be done in caller */
1126 if (v->profile == PROFILE_SIMPLE)
1128 av_log(v->s.avctx, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n");
1129 return FRAME_SKIPED;
1131 v->bfraction = vc9_bfraction_lut[get_vlc2(gb, vc9_bfraction_vlc.table,
1132 VC9_BFRACTION_VLC_BITS, 2)];
1133 if (v->bfraction < -1)
1135 av_log(v->s.avctx, AV_LOG_ERROR, "Invalid BFRaction\n");
1136 return FRAME_SKIPED;
1138 else if (!v->bfraction)
1140 /* We actually have a BI frame */
1141 v->s.pict_type = BI_TYPE;
1142 v->buffer_fullness = get_bits(gb, 7);
1145 /* Read the quantization stuff */
1146 pqindex = get_bits(gb, 5);
1147 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1148 v->pq = pquant_table[0][pqindex];
1151 v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1153 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1154 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1155 v->pquantizer = get_bits(gb, 1);
1156 #if HAS_ADVANCED_PROFILE
1157 if (v->profile == PROFILE_ADVANCED)
1159 if (v->postprocflag) v->postproc = get_bits(gb, 2);
1160 if (v->extended_mv == 1 && v->s.pict_type != BI_TYPE)
1161 v->mvrange = get_prefix(gb, 0, 3);
1166 if (v->extended_mv == 1)
1167 v->mvrange = get_prefix(gb, 0, 3);
1169 /* Read the MV mode */
1170 if (v->s.pict_type != BI_TYPE)
1172 v->mv_mode = get_bits(gb, 1);
1177 v->mv_mode = get_bits(gb, 2);
1179 av_log(v->s.avctx, AV_LOG_ERROR,
1180 "mv_mode for lowquant B frame was %i\n", v->mv_mode);
1187 if (get_bits(gb, 1))
1188 av_log(v->s.avctx, AV_LOG_ERROR,
1189 "mv_mode for highquant B frame was %i\n", v->mv_mode);
1191 v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping
1198 /** B and BI frame header decoding, secondary part
1199 * @see Tables 11+12, p62-65
1200 * @param v VC9 context
1202 * @warning Also handles BI frames
1203 * @warning To call once all MB arrays are allocated
1204 * @todo Support Advanced Profile headers
1206 static int decode_b_picture_secondary_header(VC9Context *v)
1208 GetBitContext *gb = &v->s.gb;
1211 status = bitplane_decoding(&v->skip_mb_plane, v);
1212 if (status < 0) return -1;
1214 if (v->mv_mode == MV_PMODE_MIXED_MV)
1216 status = bitplane_decoding(&v->mv_type_mb_plane, v);
1220 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1221 "Imode: %i, Invert: %i\n", status>>1, status&1);
1226 status = bitplane_decoding(&v->direct_mb_plane, v);
1227 if (status < 0) return -1;
1229 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct plane encoding: "
1230 "Imode: %i, Invert: %i\n", status>>1, status&1);
1233 av_log(v->s.avctx, AV_LOG_DEBUG, "Skip MB plane encoding: "
1234 "Imode: %i, Invert: %i\n", status>>1, status&1);
1237 /* FIXME: what is actually chosen for B frames ? */
1238 v->s.mv_table_index = get_bits(gb, 2); //but using vc9_ tables
1239 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(gb, 2)];
1243 vop_dquant_decoding(v);
1248 v->ttmbf = get_bits(gb, 1);
1251 v->ttfrm = get_bits(gb, 2);
1252 av_log(v->s.avctx, AV_LOG_INFO, "Transform used: %ix%i\n",
1253 (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
1256 /* Epilog (AC/DC syntax) should be done in caller */
1260 /** I frame header decoding, primary part
1261 * @see Tables 5+7, p53-54 and 55-57
1262 * @param v VC9 context
1264 * @todo Support Advanced Profile headers
1266 static int decode_i_picture_primary_header(VC9Context *v)
1268 GetBitContext *gb = &v->s.gb;
1271 /* Prolog common to all frametypes should be done in caller */
1272 //BF = Buffer Fullness
1273 if (v->profile < PROFILE_ADVANCED && get_bits(gb, 7))
1275 av_log(v->s.avctx, AV_LOG_DEBUG, "I BufferFullness not 0\n");
1278 /* Quantizer stuff */
1279 pqindex = get_bits(gb, 5);
1280 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1281 v->pq = pquant_table[0][pqindex];
1284 v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1286 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1287 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1288 v->pquantizer = get_bits(gb, 1);
1289 av_log(v->s.avctx, AV_LOG_DEBUG, "I frame: QP=%i (+%i/2)\n",
1294 /** I frame header decoding, secondary part
1295 * @param v VC9 context
1297 * @warning Not called in A/S/C profiles, it seems
1298 * @todo Support Advanced Profile headers
1300 static int decode_i_picture_secondary_header(VC9Context *v)
1302 #if HAS_ADVANCED_PROFILE
1304 if (v->profile == PROFILE_ADVANCED)
1306 v->s.ac_pred = get_bits(&v->s.gb, 1);
1307 if (v->postprocflag) v->postproc = get_bits(&v->s.gb, 1);
1308 /* 7.1.1.34 + 8.5.2 */
1309 if (v->overlap && v->pq<9)
1311 v->condover = get_bits(&v->s.gb, 1);
1314 v->condover = 2+get_bits(&v->s.gb, 1);
1315 if (v->condover == 3)
1317 status = bitplane_decoding(&v->over_flags_plane, v);
1318 if (status < 0) return -1;
1320 av_log(v->s.avctx, AV_LOG_DEBUG, "Overflags plane encoding: "
1321 "Imode: %i, Invert: %i\n", status>>1, status&1);
1329 /* Epilog (AC/DC syntax) should be done in caller */
1333 /** P frame header decoding, primary part
1334 * @see Tables 5+7, p53-54 and 55-57
1335 * @param v VC9 context
1336 * @todo Support Advanced Profile headers
1339 static int decode_p_picture_primary_header(VC9Context *v)
1341 /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */
1342 GetBitContext *gb = &v->s.gb;
1343 int lowquant, pqindex;
1345 pqindex = get_bits(gb, 5);
1346 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1347 v->pq = pquant_table[0][pqindex];
1350 v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1352 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1353 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1354 v->pquantizer = get_bits(gb, 1);
1355 av_log(v->s.avctx, AV_LOG_DEBUG, "P Frame: QP=%i (+%i/2)\n",
1357 if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1358 #if HAS_ADVANCED_PROFILE
1359 if (v->profile == PROFILE_ADVANCED)
1361 if (v->postprocflag) v->postproc = get_bits(gb, 1);
1365 if (v->multires) v->respic = get_bits(gb, 2);
1366 lowquant = (v->pquantizer>12) ? 0 : 1;
1367 v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1368 if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1370 v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(gb, 1, 3)];
1371 v->lumscale = get_bits(gb, 6);
1372 v->lumshift = get_bits(gb, 6);
1377 /** P frame header decoding, secondary part
1378 * @see Tables 5+7, p53-54 and 55-57
1379 * @param v VC9 context
1380 * @warning To call once all MB arrays are allocated
1383 static int decode_p_picture_secondary_header(VC9Context *v)
1385 GetBitContext *gb = &v->s.gb;
1387 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1388 v->mv_mode2 == MV_PMODE_MIXED_MV)
1389 || v->mv_mode == MV_PMODE_MIXED_MV)
1391 status = bitplane_decoding(&v->mv_type_mb_plane, v);
1392 if (status < 0) return -1;
1394 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1395 "Imode: %i, Invert: %i\n", status>>1, status&1);
1399 status = bitplane_decoding(&v->skip_mb_plane, v);
1400 if (status < 0) return -1;
1402 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1403 "Imode: %i, Invert: %i\n", status>>1, status&1);
1406 /* Hopefully this is correct for P frames */
1407 v->s.mv_table_index =get_bits(gb, 2); //but using vc9_ tables
1408 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(gb, 2)];
1412 av_log(v->s.avctx, AV_LOG_INFO, "VOP DQuant info\n");
1413 vop_dquant_decoding(v);
1416 v->ttfrm = 0; //FIXME Is that so ?
1419 v->ttmbf = get_bits(gb, 1);
1422 v->ttfrm = get_bits(gb, 2);
1423 av_log(v->s.avctx, AV_LOG_INFO, "Transform used: %ix%i\n",
1424 (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8);
1427 /* Epilog (AC/DC syntax) should be done in caller */
1430 /** @} */ //End of group all_frm_hdr
1433 /***********************************************************************/
1435 * @defgroup std_frame_hdr VC9 Simple/Main Profiles header decoding
1436 * @brief Part of the frame header decoding belonging to Simple/Main Profiles
1437 * @warning Only pro/epilog differs between Simple/Main and Advanced =>
1442 /** Frame header decoding, first part, in Simple and Main profiles
1443 * @see Tables 5+7, p53-54 and 55-57
1444 * @param v VC9 context
1445 * @todo FIXME: RANGEREDFRM element not read if BI frame from Table6, P54
1446 * However, 7.1.1.8 says "all frame types, for main profiles"
1449 static int standard_decode_picture_primary_header(VC9Context *v)
1451 GetBitContext *gb = &v->s.gb;
1454 if (v->finterpflag) v->interpfrm = get_bits(gb, 1);
1455 skip_bits(gb, 2); //framecnt unused
1456 if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1457 v->s.pict_type = get_bits(gb, 1);
1458 if (v->s.avctx->max_b_frames)
1460 if (!v->s.pict_type)
1462 if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1463 else v->s.pict_type = B_TYPE;
1465 else v->s.pict_type = P_TYPE;
1467 else v->s.pict_type++;
1469 switch (v->s.pict_type)
1471 case I_TYPE: status = decode_i_picture_primary_header(v); break;
1472 case P_TYPE: status = decode_p_picture_primary_header(v); break;
1473 case BI_TYPE: //Same as B
1474 case B_TYPE: status = decode_b_picture_primary_header(v); break;
1477 if (status == FRAME_SKIPED)
1479 av_log(v->s.avctx, AV_LOG_INFO, "Skipping frame...\n");
1485 /** Frame header decoding, secondary part
1486 * @param v VC9 context
1487 * @warning To call once all MB arrays are allocated
1490 static int standard_decode_picture_secondary_header(VC9Context *v)
1492 GetBitContext *gb = &v->s.gb;
1495 switch (v->s.pict_type)
1497 case P_TYPE: status = decode_p_picture_secondary_header(v); break;
1498 case B_TYPE: status = decode_b_picture_secondary_header(v); break;
1500 case I_TYPE: break; //Nothing needed as it's done in the epilog
1502 if (status < 0) return FRAME_SKIPED;
1505 v->c_ac_table_index = decode012(gb);
1506 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1508 v->y_ac_table_index = decode012(gb);
1511 v->s.dc_table_index = decode012(gb);
1515 /** @} */ //End for group std_frame_hdr
1517 #if HAS_ADVANCED_PROFILE
1518 /***********************************************************************/
1520 * @defgroup adv_frame_hdr VC9 Advanced Profile header decoding
1521 * @brief Part of the frame header decoding belonging to Advanced Profiles
1522 * @warning Only pro/epilog differs between Simple/Main and Advanced =>
1526 /** Frame header decoding, primary part
1527 * @param v VC9 context
1530 static int advanced_decode_picture_primary_header(VC9Context *v)
1532 GetBitContext *gb = &v->s.gb;
1533 static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE };
1538 v->fcm = get_bits(gb, 1);
1539 if (v->fcm) v->fcm = 2+get_bits(gb, 1);
1542 type = get_prefix(gb, 0, 4);
1543 if (type > 4 || type < 0) return FRAME_SKIPED;
1544 v->s.pict_type = type_table[type];
1545 av_log(v->s.avctx, AV_LOG_INFO, "AP Frame Type: %i\n", v->s.pict_type);
1547 if (v->tfcntrflag) v->tfcntr = get_bits(gb, 8);
1550 if (!v->interlace) v->rptfrm = get_bits(gb, 2);
1553 v->tff = get_bits(gb, 1);
1554 v->rff = get_bits(gb, 1);
1561 for (i=0; i<v->numpanscanwin; i++)
1563 v->topleftx[i] = get_bits(gb, 16);
1564 v->toplefty[i] = get_bits(gb, 16);
1565 v->bottomrightx[i] = get_bits(gb, 16);
1566 v->bottomrighty[i] = get_bits(gb, 16);
1569 skip_bits(gb, 16*4*v->numpanscanwin);
1572 v->s.no_rounding = !get_bits(gb, 1);
1573 v->uvsamp = get_bits(gb, 1);
1574 if (v->finterpflag == 1) v->interpfrm = get_bits(gb, 1);
1576 switch(v->s.pict_type)
1578 case I_TYPE: if (decode_i_picture_primary_header(v) < 0) return -1;
1579 case P_TYPE: if (decode_p_picture_primary_header(v) < 0) return -1;
1581 case B_TYPE: if (decode_b_picture_primary_header(v) < 0) return FRAME_SKIPED;
1586 /** Frame header decoding, secondary part
1587 * @param v VC9 context
1590 static int advanced_decode_picture_secondary_header(VC9Context *v)
1592 GetBitContext *gb = &v->s.gb;
1595 switch(v->s.pict_type)
1597 case P_TYPE: status = decode_p_picture_secondary_header(v); break;
1598 case B_TYPE: status = decode_b_picture_secondary_header(v); break;
1600 case I_TYPE: status = decode_i_picture_secondary_header(v); break;
1602 if (status<0) return FRAME_SKIPED;
1605 v->c_ac_table_index = decode012(gb);
1606 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1608 v->y_ac_table_index = decode012(gb);
1611 v->s.dc_table_index = decode012(gb);
1616 /** @} */ //End for adv_frame_hdr
1618 /***********************************************************************/
1620 * @defgroup block VC9 Block-level functions
1621 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1622 * @todo TODO: Integrate to MpegEncContext facilities
1628 * @brief Get macroblock-level quantizer scale
1629 * @warning XXX: qdiff to the frame quant, not previous quant ?
1630 * @fixme XXX: Don't know how to initialize mquant otherwise in last case
1632 #define GET_MQUANT() \
1635 if (v->dqprofile == DQPROFILE_ALL_MBS) \
1639 mquant = (get_bits(gb, 1)) ? v->pq : v->altpq; \
1643 mqdiff = get_bits(gb, 3); \
1644 if (mqdiff != 7) mquant = v->pq + mqdiff; \
1645 else mquant = get_bits(gb, 5); \
1648 else mquant = v->pq; \
1652 * @def GET_MVDATA(_dmv_x, _dmv_y)
1653 * @brief Get MV differentials
1654 * @see MVDATA decoding from 8.3.5.2, p(1)20
1655 * @param _dmv_x Horizontal differential for decoded MV
1656 * @param _dmv_y Vertical differential for decoded MV
1657 * @todo TODO: Use MpegEncContext arrays to store them
1659 #define GET_MVDATA(_dmv_x, _dmv_y) \
1660 index = 1 + get_vlc2(gb, vc9_mv_diff_vlc[s->mv_table_index].table,\
1661 VC9_MV_DIFF_VLC_BITS, 2); \
1664 mb_has_coeffs = 1; \
1667 else mb_has_coeffs = 0; \
1669 if (!index) { _dmv_x = _dmv_y = 0; } \
1670 else if (index == 35) \
1672 _dmv_x = get_bits(gb, v->k_x); \
1673 _dmv_y = get_bits(gb, v->k_y); \
1679 if (s->mspel && index1 == 5) val = 1; \
1681 val = get_bits(gb, size_table[index1] - val); \
1682 sign = 0 - (val&1); \
1683 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1686 if (s->mspel && index1 == 5) val = 1; \
1688 val = get_bits(gb, size_table[index1] - val); \
1689 sign = 0 - (val&1); \
1690 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1693 /** Get predicted DC value
1694 * prediction dir: left=0, top=1
1695 * @param s MpegEncContext
1696 * @param[in] n block index in the current MB
1697 * @param dc_val_ptr Pointer to DC predictor
1698 * @param dir_ptr Prediction direction for use in AC prediction
1699 * @todo TODO: Actually do it the VC9 way
1700 * @todo TODO: Handle properly edges
1702 static inline int vc9_pred_dc(MpegEncContext *s, int n,
1703 uint16_t **dc_val_ptr, int *dir_ptr)
1705 int a, b, c, wrap, pred, scale;
1707 static const uint16_t dcpred[31] = {
1708 1024, 512, 341, 256, 205, 171, 146, 128,
1709 114, 102, 93, 85, 79, 73, 68, 64,
1710 60, 57, 54, 51, 49, 47, 45, 43,
1711 41, 39, 38, 37, 35, 34, 33
1714 /* find prediction - wmv3_dc_scale always used here in fact */
1715 if (n < 4) scale = s->y_dc_scale;
1716 else scale = s->c_dc_scale;
1718 wrap = s->block_wrap[n];
1719 dc_val= s->dc_val[0] + s->block_index[n];
1725 b = dc_val[ - 1 - wrap];
1726 c = dc_val[ - wrap];
1728 /* XXX: Rule B is used only for I and BI frames in S/M/C profile
1729 * with overlap filtering off
1731 if ((s->pict_type == I_TYPE || s->pict_type == BI_TYPE) &&
1732 1 /* XXX: overlap filtering off */)
1734 /* Set outer values */
1735 if (s->first_slice_line && n!=2) b=c=dcpred[scale];
1736 if (s->mb_x == 0) b=a=dcpred[scale];
1740 /* Set outer values */
1741 if (s->first_slice_line && n!=2) b=c=0;
1742 if (s->mb_x == 0) b=a=0;
1744 /* XXX: Rule A needs to know if blocks are inter or intra :/ */
1747 /* update predictor */
1748 *dc_val_ptr = &dc_val[0];
1754 if (abs(a - b) <= abs(b - c)) {
1762 /* update predictor */
1763 *dc_val_ptr = &dc_val[0];
1767 /** Decode one block, inter or intra
1768 * @param v The VC9 context
1769 * @param block 8x8 DCT block
1770 * @param n Block index in the current MB (<4=>luma)
1771 * @param coded If the block is coded
1772 * @param mquant Quantizer step for the current block
1773 * @see Inter TT: Table 21, p73 + p91-85
1774 * @see Intra TT: Table 20, p72 + p(1)05-(1)07
1775 * @todo TODO: Process the blocks
1776 * @todo TODO: Use M$ MPEG-4 cbp prediction
1778 int vc9_decode_block(VC9Context *v, DCTELEM block[64], int n, int coded, int mquant)
1780 GetBitContext *gb = &v->s.gb;
1781 MpegEncContext *s = &v->s;
1782 int ttblk; /* Transform Type per Block */
1783 int subblkpat; /* Sub-block Transform Type Pattern */
1784 int dc_pred_dir; /* Direction of the DC prediction used */
1787 /* XXX: Guard against dumb values of mquant */
1788 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1790 /* Set DC scale - y and c use the same */
1791 s->y_dc_scale = s->y_dc_scale_table[mquant];
1792 s->c_dc_scale = s->c_dc_scale_table[mquant];
1799 /* Get DC differential */
1801 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1803 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1806 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1811 if (dcdiff == 119 /* ESC index value */)
1813 /* TODO: Optimize */
1814 if (mquant == 1) dcdiff = get_bits(gb, 10);
1815 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1816 else dcdiff = get_bits(gb, 8);
1821 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1822 else if (mquant == 2)
1823 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
1825 if (get_bits(gb, 1))
1830 dcdiff += vc9_pred_dc(s, n, &dc_val, &dc_pred_dir);
1832 /* Store the quantized DC coeff, used for prediction */
1835 block[0] = dcdiff * s->y_dc_scale;
1837 block[0] = dcdiff * s->c_dc_scale;
1841 //av_log(s->avctx, AV_LOG_ERROR, "DC=%i<0\n", dcdiff);
1857 if (v->ttmb < 8) /* per block */
1858 ttblk = get_vlc2(gb, vc9_ttblk_vlc[v->tt_index].table, VC9_TTBLK_VLC_BITS, 2);
1859 else /* Per frame */
1860 ttblk = 0; //FIXME, depends on ttfrm
1863 if (ttblk == v->ttblk4x4) /* 4x4 transform for that qp value */
1864 subblkpat = 1+get_vlc2(gb, vc9_subblkpat_vlc[v->tt_index].table,
1865 VC9_SUBBLKPAT_VLC_BITS, 2);
1866 else /* All others: 8x8, 4x8, 8x4 */
1867 subblkpat = decode012(gb);
1871 i = 63; //XXX: nothing done yet
1876 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1878 i = 63; /* XXX: not optimal */
1881 if(i>0) i=63; //FIXME/XXX optimize
1882 s->block_last_index[n] = i;
1886 /** @} */ //End for group block
1888 /***********************************************************************/
1890 * @defgroup std_mb VC9 Macroblock-level functions in Simple/Main Profiles
1891 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1892 * @todo TODO: Integrate to MpegEncContext facilities
1896 static inline int vc9_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1898 int xy, wrap, pred, a, b, c;
1900 xy = s->block_index[n];
1901 wrap = s->b8_stride;
1906 a = s->coded_block[xy - 1 ];
1907 b = s->coded_block[xy - 1 - wrap];
1908 c = s->coded_block[xy - wrap];
1917 *coded_block_ptr = &s->coded_block[xy];
1922 /** Decode one I-frame MB (in Simple/Main profile)
1923 * @todo TODO: Extend to AP
1925 int vc9_decode_i_mb(VC9Context *v, DCTELEM block[6][64])
1929 uint32_t * const mb_type_ptr= &v->s.current_picture.mb_type[ v->s.mb_x + v->s.mb_y*v->s.mb_stride ];
1932 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
1933 if (cbp < 0) return -1;
1934 v->s.ac_pred = get_bits(&v->s.gb, 1);
1938 val = ((cbp >> (5 - i)) & 1);
1940 int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
1944 cbp |= val << (5 - i);
1945 if (vc9_decode_block(v, block[i], i, val, v->pq) < 0) //FIXME Should be mquant
1947 av_log(v->s.avctx, AV_LOG_ERROR,
1948 "\nerror while decoding block: %d x %d (%d)\n", v->s.mb_x, v->s.mb_y, i);
1955 /** Decode one P-frame MB (in Simple/Main profile)
1956 * @todo TODO: Extend to AP
1957 * @fixme FIXME: DC value for inter blocks not set
1959 int vc9_decode_p_mb(VC9Context *v, DCTELEM block[6][64])
1961 MpegEncContext *s = &v->s;
1962 GetBitContext *gb = &s->gb;
1963 int i, mb_offset = s->mb_x + s->mb_y*s->mb_width; /* XXX: mb_stride */
1964 int cbp; /* cbp decoding stuff */
1965 int hybrid_pred; /* Prediction types */
1966 int mv_mode_bit = 0;
1967 int mqdiff, mquant; /* MB quantization */
1968 int ttmb; /* MB Transform type */
1972 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
1973 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
1974 int mb_has_coeffs = 1; /* last_flag */
1975 int dmv_x, dmv_y; /* Differential MV components */
1976 int index, index1; /* LUT indices */
1977 int val, sign; /* temp values */
1979 mquant = v->pq; /* Loosy initialization */
1981 if (v->mv_type_mb_plane.is_raw)
1982 v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
1983 if (v->skip_mb_plane.is_raw)
1984 v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
1985 if (!mv_mode_bit) /* 1MV mode */
1987 if (!v->skip_mb_plane.data[mb_offset])
1989 GET_MVDATA(dmv_x, dmv_y);
1991 /* hybrid mv pred, 8.3.5.3.4 */
1992 if (v->mv_mode == MV_PMODE_1MV ||
1993 v->mv_mode == MV_PMODE_MIXED_MV)
1994 hybrid_pred = get_bits(gb, 1);
1995 /* FIXME Set DC val for inter block ? */
1996 if (s->mb_intra && !mb_has_coeffs)
1999 s->ac_pred = get_bits(gb, 1);
2000 /* XXX: how to handle cbp ? */
2004 s->coded_block[s->block_index[i]] = 0;
2005 vc9_decode_block(v, block[i], i, 0, mquant);
2009 else if (mb_has_coeffs)
2011 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
2012 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
2018 /* XXX: how to handle cbp ? */
2019 /* XXX: how to set values for following predictions ? */
2024 ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
2025 VC9_TTMB_VLC_BITS, 12);
2029 val = ((cbp >> (5 - i)) & 1);
2031 int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
2035 vc9_decode_block(v, block[i], i, val, mquant); //FIXME
2040 /* hybrid mv pred, 8.3.5.3.4 */
2041 if (v->mv_mode == MV_PMODE_1MV ||
2042 v->mv_mode == MV_PMODE_MIXED_MV)
2043 hybrid_pred = get_bits(gb, 1);
2051 if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
2054 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
2057 val = ((cbp >> (5 - i)) & 1);
2059 int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
2065 GET_MVDATA(dmv_x, dmv_y);
2067 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
2068 hybrid_pred = get_bits(gb, 1);
2071 if (s->mb_intra /* One of the 4 blocks is intra */ &&
2072 index /* non-zero pred for that block */)
2073 s->ac_pred = get_bits(gb, 1);
2075 ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
2076 VC9_TTMB_VLC_BITS, 12);
2077 status = vc9_decode_block(v, block[i], i, val, mquant);
2083 /* XXX: Skipped => cbp=0 and mquant doesn't matter ? */
2086 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
2087 hybrid_pred = get_bits(gb, 1);
2088 vc9_decode_block(v, block[i], i, 0, v->pq); //FIXME
2090 vc9_decode_block(v, block[4], 4, 0, v->pq); //FIXME
2091 vc9_decode_block(v, block[5], 5, 0, v->pq); //FIXME
2097 /* Should never happen */
2101 /** Decode one B-frame MB (in Simple/Main profile)
2102 * @todo TODO: Extend to AP
2103 * @warning XXX: Used for decoding BI MBs
2104 * @fixme FIXME: DC value for inter blocks not set
2106 int vc9_decode_b_mb(VC9Context *v, DCTELEM block[6][64])
2108 MpegEncContext *s = &v->s;
2109 GetBitContext *gb = &v->s.gb;
2110 int mb_offset, i /* MB / B postion information */;
2111 int b_mv_type = BMV_TYPE_BACKWARD;
2112 int mquant, mqdiff; /* MB quant stuff */
2113 int ttmb; /* MacroBlock transform type */
2115 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
2116 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2117 int mb_has_coeffs = 1; /* last_flag */
2118 int dmv1_x, dmv1_y, dmv2_x, dmv2_y; /* Differential MV components */
2119 int index, index1; /* LUT indices */
2120 int val, sign; /* MVDATA temp values */
2122 mb_offset = s->mb_width*s->mb_y + s->mb_x; //FIXME: arrays aren't using stride
2124 if (v->direct_mb_plane.is_raw)
2125 v->direct_mb_plane.data[mb_offset] = get_bits(gb, 1);
2126 if (v->skip_mb_plane.is_raw)
2127 v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
2129 if (!v->direct_mb_plane.data[mb_offset])
2131 if (v->skip_mb_plane.data[mb_offset])
2133 b_mv_type = decode012(gb);
2134 if (v->bfraction > 420 /*1/2*/ &&
2135 b_mv_type < 3) b_mv_type = 1-b_mv_type;
2139 GET_MVDATA(dmv1_x, dmv1_y);
2140 if (!s->mb_intra /* b_mv1 tells not intra */)
2142 b_mv_type = decode012(gb);
2143 if (v->bfraction > 420 /*1/2*/ &&
2144 b_mv_type < 3) b_mv_type = 1-b_mv_type;
2148 if (!v->skip_mb_plane.data[mb_offset])
2150 if (mb_has_coeffs /* BMV1 == "last" */)
2153 if (s->mb_intra /* intra mb */)
2154 s->ac_pred = get_bits(gb, 1);
2158 /* if bmv1 tells MVs are interpolated */
2159 if (b_mv_type == BMV_TYPE_INTERPOLATED)
2161 GET_MVDATA(dmv2_x, dmv2_y);
2162 mquant = v->pq; //FIXME: initialization not necessary ?
2164 /* GET_MVDATA has reset some stuff */
2165 if (mb_has_coeffs /* b_mv2 == "last" */)
2167 if (s->mb_intra /* intra_mb */)
2168 s->ac_pred = get_bits(gb, 1);
2176 ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
2177 VC9_TTMB_VLC_BITS, 12);
2182 vc9_decode_block(v, block[i], i, 0 /*cbp[i]*/, mquant); //FIXME
2187 /** Decode all MBs for an I frame in Simple/Main profile
2188 * @todo TODO: Move out of the loop the picture type case?
2189 (branch prediction should help there though)
2191 static int standard_decode_mbs(VC9Context *v)
2193 MpegEncContext *s = &v->s;
2195 /* Set transform type info depending on pq */
2201 else if (v->pq < 13)
2212 if (s->pict_type != I_TYPE)
2214 /* Select proper long MV range */
2217 case 1: v->k_x = 10; v->k_y = 9; break;
2218 case 2: v->k_x = 12; v->k_y = 10; break;
2219 case 3: v->k_x = 13; v->k_y = 11; break;
2220 default: /*case 0 too */ v->k_x = 9; v->k_y = 8; break;
2223 s->mspel = v->mv_mode & 1; //MV_PMODE is HPEL
2228 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
2230 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
2232 //FIXME Get proper MB DCTELEM
2233 //TODO Move out of the loop
2234 switch (s->pict_type)
2236 case I_TYPE: vc9_decode_i_mb(v, s->block); break;
2237 case P_TYPE: vc9_decode_p_mb(v, s->block); break;
2239 case B_TYPE: vc9_decode_b_mb(v, s->block); break;
2242 //Add a check for overconsumption ?
2246 /** @} */ //End for group std_mb
2248 #if HAS_ADVANCED_PROFILE
2249 /***********************************************************************/
2251 * @defgroup adv_mb VC9 Macroblock-level functions in Advanced Profile
2252 * @todo TODO: Integrate to MpegEncContext facilities
2253 * @todo TODO: Code P, B and BI
2256 static int advanced_decode_i_mbs(VC9Context *v)
2258 MpegEncContext *s = &v->s;
2259 GetBitContext *gb = &v->s.gb;
2260 int mqdiff, mquant, mb_offset = 0, over_flags_mb = 0;
2262 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
2264 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
2266 if (v->ac_pred_plane.is_raw)
2267 s->ac_pred = get_bits(gb, 1);
2269 s->ac_pred = v->ac_pred_plane.data[mb_offset];
2270 if (v->condover == 3 && v->over_flags_plane.is_raw)
2271 over_flags_mb = get_bits(gb, 1);
2280 /** @} */ //End for group adv_mb
2283 /** Initialize a VC9/WMV3 decoder
2284 * @todo TODO: Handle VC-9 IDUs (Transport level?)
2285 * @todo TODO: Decypher remaining bits in extra_data
2287 static int vc9_decode_init(AVCodecContext *avctx)
2289 VC9Context *v = avctx->priv_data;
2290 MpegEncContext *s = &v->s;
2293 if (!avctx->extradata_size || !avctx->extradata) return -1;
2294 avctx->pix_fmt = PIX_FMT_YUV420P;
2297 if(ff_h263_decode_init(avctx) < 0)
2299 if (vc9_init_common(v) < 0) return -1;
2301 avctx->coded_width = avctx->width;
2302 avctx->coded_height = avctx->height;
2303 if (avctx->codec_id == CODEC_ID_WMV3)
2307 // looks like WMV3 has a sequence header stored in the extradata
2308 // advanced sequence header may be before the first frame
2309 // the last byte of the extradata is a version number, 1 for the
2310 // samples we can decode
2312 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
2314 if (decode_sequence_header(avctx, &gb) < 0)
2317 count = avctx->extradata_size*8 - get_bits_count(&gb);
2320 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
2321 count, get_bits(&gb, count));
2325 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
2328 avctx->has_b_frames= !!(avctx->max_b_frames);
2330 s->mb_width = (avctx->coded_width+15)>>4;
2331 s->mb_height = (avctx->coded_height+15)>>4;
2333 /* Allocate mb bitplanes */
2334 if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
2336 if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0)
2338 if (alloc_bitplane(&v->skip_mb_plane, s->mb_width, s->mb_height) < 0)
2340 if (alloc_bitplane(&v->direct_mb_plane, s->mb_width, s->mb_height) < 0)
2343 /* For predictors */
2344 v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4);
2345 if (!v->previous_line_cbpcy) return -1;
2347 #if HAS_ADVANCED_PROFILE
2348 if (v->profile == PROFILE_ADVANCED)
2350 if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
2352 if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
2360 /** Decode a VC9/WMV3 frame
2361 * @todo TODO: Handle VC-9 IDUs (Transport level?)
2362 * @warning Initial try at using MpegEncContext stuff
2364 static int vc9_decode_frame(AVCodecContext *avctx,
2365 void *data, int *data_size,
2366 uint8_t *buf, int buf_size)
2368 VC9Context *v = avctx->priv_data;
2369 MpegEncContext *s = &v->s;
2370 int ret = FRAME_SKIPED, len;
2371 AVFrame *pict = data;
2375 //buf_size = 0 -> last frame
2376 if (!buf_size) return 0;
2378 len = avpicture_get_size(avctx->pix_fmt, avctx->width,
2380 tmp_buf = (uint8_t *)av_mallocz(len);
2381 avpicture_fill((AVPicture *)pict, tmp_buf, avctx->pix_fmt,
2382 avctx->width, avctx->height);
2384 if (avctx->codec_id == CODEC_ID_VC9)
2392 while (i < buf_size)
2394 for (; i < buf_size && scp != 0x000001; i++)
2395 scp = ((scp<<8)|buf[i])&0xffffff;
2397 if (scp != 0x000001)
2402 init_get_bits(gb, buf+i, (buf_size-i)*8);
2406 case 0x0A: //Sequence End Code
2408 case 0x0B: //Slice Start Code
2409 av_log(avctx, AV_LOG_ERROR, "Slice coding not supported\n");
2411 case 0x0C: //Field start code
2412 av_log(avctx, AV_LOG_ERROR, "Interlaced coding not supported\n");
2414 case 0x0D: //Frame start code
2416 case 0x0E: //Entry point Start Code
2417 if (v->profile < PROFILE_ADVANCED)
2418 av_log(avctx, AV_LOG_ERROR,
2419 "Found an entry point in profile %i\n", v->profile);
2420 advanced_entry_point_process(avctx, gb);
2422 case 0x0F: //Sequence header Start Code
2423 decode_sequence_header(avctx, gb);
2426 av_log(avctx, AV_LOG_ERROR,
2427 "Unsupported IDU suffix %lX\n", scs);
2430 i += get_bits_count(gb)*8;
2437 init_get_bits(&v->s.gb, buf, buf_size*8);
2439 s->flags= avctx->flags;
2440 s->flags2= avctx->flags2;
2442 /* no supplementary picture */
2443 if (buf_size == 0) {
2444 /* special case for last picture */
2445 if (s->low_delay==0 && s->next_picture_ptr) {
2446 *pict= *(AVFrame*)s->next_picture_ptr;
2447 s->next_picture_ptr= NULL;
2449 *data_size = sizeof(AVFrame);
2455 //No IDU - we mimic ff_h263_decode_frame
2456 s->bitstream_buffer_size=0;
2458 if (!s->context_initialized) {
2459 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
2463 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
2464 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
2465 s->current_picture_ptr= &s->picture[ff_find_unused_picture(s, 0)];
2467 #if HAS_ADVANCED_PROFILE
2468 if (v->profile == PROFILE_ADVANCED)
2469 ret= advanced_decode_picture_primary_header(v);
2472 ret= standard_decode_picture_primary_header(v);
2473 if (ret == FRAME_SKIPED) return buf_size;
2474 /* skip if the header was thrashed */
2476 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
2480 //No bug workaround yet, no DCT conformance
2482 //WMV9 does have resized images
2483 if (v->profile < PROFILE_ADVANCED && v->multires){
2484 //Parse context stuff in here, don't know how appliable it is
2486 //Not sure about context initialization
2489 s->current_picture.pict_type= s->pict_type;
2490 s->current_picture.key_frame= s->pict_type == I_TYPE;
2492 /* skip b frames if we dont have reference frames */
2493 if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable))
2494 return buf_size; //FIXME simulating all buffer consumed
2495 /* skip b frames if we are in a hurry */
2496 if(avctx->hurry_up && s->pict_type==B_TYPE)
2497 return buf_size; //FIXME simulating all buffer consumed
2498 /* skip everything if we are in a hurry>=5 */
2499 if(avctx->hurry_up>=5)
2500 return buf_size; //FIXME simulating all buffer consumed
2502 if(s->next_p_frame_damaged){
2503 if(s->pict_type==B_TYPE)
2504 return buf_size; //FIXME simulating all buffer consumed
2506 s->next_p_frame_damaged=0;
2509 if(MPV_frame_start(s, avctx) < 0)
2512 ff_er_frame_start(s);
2514 //wmv9 may or may not have skip bits
2515 #if HAS_ADVANCED_PROFILE
2516 if (v->profile == PROFILE_ADVANCED)
2517 ret= advanced_decode_picture_secondary_header(v);
2520 ret = standard_decode_picture_secondary_header(v);
2521 if (ret<0) return FRAME_SKIPED; //FIXME Non fatal for now
2523 //We consider the image coded in only one slice
2524 #if HAS_ADVANCED_PROFILE
2525 if (v->profile == PROFILE_ADVANCED)
2527 switch(s->pict_type)
2529 case I_TYPE: ret = advanced_decode_i_mbs(v); break;
2530 case P_TYPE: ret = decode_p_mbs(v); break;
2532 case BI_TYPE: ret = decode_b_mbs(v); break;
2533 default: ret = FRAME_SKIPED;
2535 if (ret == FRAME_SKIPED) return buf_size; //We ignore for now failures
2540 ret = standard_decode_mbs(v);
2541 if (ret == FRAME_SKIPED) return buf_size;
2548 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
2549 assert(s->current_picture.pict_type == s->pict_type);
2550 if(s->pict_type==B_TYPE || s->low_delay){
2551 *pict= *(AVFrame*)&s->current_picture;
2552 ff_print_debug_info(s, pict);
2554 *pict= *(AVFrame*)&s->last_picture;
2556 ff_print_debug_info(s, pict);
2559 /* Return the Picture timestamp as the frame number */
2560 /* we substract 1 because it is added on utils.c */
2561 avctx->frame_number = s->picture_number - 1;
2563 /* dont output the last pic after seeking */
2564 if(s->last_picture_ptr || s->low_delay)
2565 *data_size = sizeof(AVFrame);
2567 av_log(avctx, AV_LOG_DEBUG, "Consumed %i/%i bits\n",
2568 get_bits_count(&s->gb), buf_size*8);
2570 /* Fake consumption of all data */
2572 return buf_size; //Number of bytes consumed
2575 /** Close a VC9/WMV3 decoder
2576 * @warning Initial try at using MpegEncContext stuff
2578 static int vc9_decode_end(AVCodecContext *avctx)
2580 VC9Context *v = avctx->priv_data;
2582 #if HAS_ADVANCED_PROFILE
2583 av_freep(&v->hrd_rate);
2584 av_freep(&v->hrd_buffer);
2586 MPV_common_end(&v->s);
2587 free_bitplane(&v->mv_type_mb_plane);
2588 free_bitplane(&v->skip_mb_plane);
2589 free_bitplane(&v->direct_mb_plane);
2593 AVCodec vc9_decoder = {
2606 AVCodec wmv3_decoder = {