comparison vc9.c @ 2482:5d28ea40fd98 libavcodec

Documentation patch by anonymous
author michael
date Tue, 01 Feb 2005 09:51:49 +0000
parents 38dbec9a148d
children 4eb9d61cad4e
comparison
equal deleted inserted replaced
2481:0803adcb3ec3 2482:5d28ea40fd98
62 codes, codes_wrap, codes_size, use_static) \ 62 codes, codes_wrap, codes_size, use_static) \
63 init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \ 63 init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
64 codes, codes_wrap, codes_size, use_static) 64 codes, codes_wrap, codes_size, use_static)
65 #endif 65 #endif
66 66
67 /** Available Profiles */
68 //@{
67 #define PROFILE_SIMPLE 0 69 #define PROFILE_SIMPLE 0
68 #define PROFILE_MAIN 1 70 #define PROFILE_MAIN 1
69 #define PROFILE_ADVANCED 3 71 #define PROFILE_ADVANCED 3
70 72 //@}
73
74 /** Sequence quantizer mode */
75 //@{
71 #define QUANT_FRAME_IMPLICIT 0 76 #define QUANT_FRAME_IMPLICIT 0
72 #define QUANT_FRAME_EXPLICIT 1 77 #define QUANT_FRAME_EXPLICIT 1
73 #define QUANT_NON_UNIFORM 2 78 #define QUANT_NON_UNIFORM 2
74 #define QUANT_UNIFORM 3 79 #define QUANT_UNIFORM 3
75 80 //@}
76 /* Where quant can be changed */ 81
82 /** Where quant can be changed */
83 //@{
77 #define DQPROFILE_FOUR_EDGES 0 84 #define DQPROFILE_FOUR_EDGES 0
78 #define DQPROFILE_DOUBLE_EDGES 1 85 #define DQPROFILE_DOUBLE_EDGES 1
79 #define DQPROFILE_SINGLE_EDGE 2 86 #define DQPROFILE_SINGLE_EDGE 2
80 #define DQPROFILE_ALL_MBS 3 87 #define DQPROFILE_ALL_MBS 3
81 88 //@}
82 /* Which edge is quantized with ALTPQUANT */ 89
90 /** @name Where quant can be changed
91 */
92 //@{
93 #define DQPROFILE_FOUR_EDGES 0
83 #define DQSINGLE_BEDGE_LEFT 0 94 #define DQSINGLE_BEDGE_LEFT 0
84 #define DQSINGLE_BEDGE_TOP 1 95 #define DQSINGLE_BEDGE_TOP 1
85 #define DQSINGLE_BEDGE_RIGHT 2 96 #define DQSINGLE_BEDGE_RIGHT 2
86 #define DQSINGLE_BEDGE_BOTTOM 3 97 #define DQSINGLE_BEDGE_BOTTOM 3
87 98 //@}
88 /* Which pair of edges is quantized with ALTPQUANT */ 99
100 /** Which pair of edges is quantized with ALTPQUANT */
101 //@{
89 #define DQDOUBLE_BEDGE_TOPLEFT 0 102 #define DQDOUBLE_BEDGE_TOPLEFT 0
90 #define DQDOUBLE_BEDGE_TOPRIGHT 1 103 #define DQDOUBLE_BEDGE_TOPRIGHT 1
91 #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2 104 #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
92 #define DQDOUBLE_BEDGE_BOTTOMLEFT 3 105 #define DQDOUBLE_BEDGE_BOTTOMLEFT 3
93 106 //@}
94 /* MV P modes */ 107
108 /** MV modes for P frames */
109 //@{
95 #define MV_PMODE_1MV_HPEL_BILIN 0 110 #define MV_PMODE_1MV_HPEL_BILIN 0
96 #define MV_PMODE_1MV 1 111 #define MV_PMODE_1MV 1
97 #define MV_PMODE_1MV_HPEL 2 112 #define MV_PMODE_1MV_HPEL 2
98 #define MV_PMODE_MIXED_MV 3 113 #define MV_PMODE_MIXED_MV 3
99 #define MV_PMODE_INTENSITY_COMP 4 114 #define MV_PMODE_INTENSITY_COMP 4
100 115 //@}
116
117 /** @name MV types for B frames */
118 //@{
119 #define BMV_TYPE_BACKWARD 0
101 #define BMV_TYPE_BACKWARD 0 120 #define BMV_TYPE_BACKWARD 0
102 #define BMV_TYPE_FORWARD 1 121 #define BMV_TYPE_FORWARD 1
103 #define BMV_TYPE_INTERPOLATED 3 122 #define BMV_TYPE_INTERPOLATED 3
104 123 //@}
105 /* MV P mode - the 5th element is only used for mode 1 */ 124
125 /** MV P mode - the 5th element is only used for mode 1 */
106 static const uint8_t mv_pmode_table[2][5] = { 126 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 }, 127 { 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 } 128 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
109 }; 129 };
110 130
111 /* One more frame type */ 131 /** One more frame type */
112 #define BI_TYPE 7 132 #define BI_TYPE 7
113 133
114 /* FIXME Worse than ugly */
115 static const int fps_nr[5] = { 24, 25, 30, 50, 60 }, 134 static const int fps_nr[5] = { 24, 25, 30, 50, 60 },
116 fps_dr[2] = { 1000, 1001 }; 135 fps_dr[2] = { 1000, 1001 };
117 static const uint8_t pquant_table[3][32] = { 136 static const uint8_t pquant_table[3][32] = {
118 { /* Implicit quantizer */ 137 { /* Implicit quantizer */
119 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12, 138 0, 1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8, 9, 10, 11, 12,
127 0, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 146 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 147 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 31
129 } 148 }
130 }; 149 };
131 150
132 // FIXME move this into the context 151 /** @name VC-9 VLC tables and defines
152 * @todo TODO move this into the context
153 */
154 //@{
133 #define VC9_BFRACTION_VLC_BITS 7 155 #define VC9_BFRACTION_VLC_BITS 7
134 static VLC vc9_bfraction_vlc; 156 static VLC vc9_bfraction_vlc;
135 #define VC9_IMODE_VLC_BITS 4 157 #define VC9_IMODE_VLC_BITS 4
136 static VLC vc9_imode_vlc; 158 static VLC vc9_imode_vlc;
137 #define VC9_NORM2_VLC_BITS 3 159 #define VC9_NORM2_VLC_BITS 3
138 static VLC vc9_norm2_vlc; 160 static VLC vc9_norm2_vlc;
139 #if TILE_VLC_METHOD == 1 161 #if VLC_NORM6_METH0D == 1
140 #define VC9_NORM6_VLC_BITS 9 162 #define VC9_NORM6_VLC_BITS 9
141 static VLC vc9_norm6_vlc; 163 static VLC vc9_norm6_vlc;
142 #endif 164 #endif
143 #if TILE_VLC_METHOD == 2 165 #if VLC_NORM6_METH0D == 2
144 #define VC9_NORM6_FIRST_BITS 8 166 #define VC9_NORM6_FIRST_BITS 8
145 #define VC9_NORM6_SECOND_BITS 8 167 #define VC9_NORM6_SECOND_BITS 8
146 static VLC vc9_norm6_first, vc9_norm6_second; 168 static VLC vc9_norm6_first_vlc, vc9_norm6_second_vlc;
147 #endif 169 #endif
148 /* Could be optimized, one table only needs 8 bits */ 170 /* Could be optimized, one table only needs 8 bits */
149 #define VC9_TTMB_VLC_BITS 9 //12 171 #define VC9_TTMB_VLC_BITS 9 //12
150 static VLC vc9_ttmb_vlc[3]; 172 static VLC vc9_ttmb_vlc[3];
151 #define VC9_MV_DIFF_VLC_BITS 9 //15 173 #define VC9_MV_DIFF_VLC_BITS 9 //15
152 static VLC vc9_mv_diff_vlc[4]; 174 static VLC vc9_mv_diff_vlc[4];
153 #define VC9_CBPCY_P_VLC_BITS 9 //14 175 #define VC9_CBPCY_P_VLC_BITS 9 //14
154 static VLC vc9_cbpcy_p_vlc[4]; 176 static VLC vc9_cbpcy_p_vlc[4];
155 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6 177 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6
156 static VLC vc9_4mv_block_pattern_vlc[4]; 178 static VLC vc9_4mv_block_pattern_vlc[4];
157 179 //@}
158 //We mainly need data and is_raw, so this struct could be avoided 180
159 //to save a level of indirection; feel free to modify 181 /** Bitplane struct
182 * We mainly need data and is_raw, so this struct could be avoided
183 * to save a level of indirection; feel free to modify
184 * @fixme For now, stride=width
185 * @warning Data are bits, either 1 or 0
186 */
160 typedef struct BitPlane { 187 typedef struct BitPlane {
161 uint8_t *data; 188 uint8_t *data; ///< Data buffer
162 int width, stride; 189 int width; ///< Width of the buffer
163 int height; 190 int stride; ///< Stride of the buffer
164 uint8_t is_raw; 191 int height; ///< Plane height
192 uint8_t is_raw; ///< Bit values must be read at MB level
165 } BitPlane; 193 } BitPlane;
166 194
195 /** The VC9 Context */
167 typedef struct VC9Context{ 196 typedef struct VC9Context{
168 /* No MpegEnc context, might be good to use it */ 197 MpegEncContext s;
169 MpegEncContext s; 198
170 199 /** Simple/Main Profile sequence header */
171 /***************************/ 200 //@{
172 /* Sequence Header */ 201 int res_sm; ///< reserved, 2b
173 /***************************/ 202 int res_x8; ///< reserved
174 /* Simple/Main Profile */ 203 int multires; ///< frame-level RESPIC syntax element present
175 int res_sm; //reserved, 2b 204 int res_fasttx; ///< reserved, always 1
176 int res_x8; //reserved 205 int res_transtab; ///< reserved, always 0
177 int multires; //frame-level RESPIC syntax element present 206 int rangered; ///< RANGEREDFRM (range reduction) syntax element present
178 int res_fasttx; //always 1 207 ///< at frame level
179 int res_transtab; //always 0 208 int res_rtm_flag; ///< reserved, set to 1
180 int rangered; //RANGEREDFRM (range reduction) syntax element present 209 int reserved; ///< reserved
181 int res_rtm_flag; //reserved, set to 1 210 //@}
182 int reserved; //duh
183 211
184 #if HAS_ADVANCED_PROFILE 212 #if HAS_ADVANCED_PROFILE
185 /* Advanced Profile */ 213 /** Advanced Profile */
186 int level; //3 214 //@{
187 int chromaformat; //2 215 int level; ///< 3bits, for Advanced/Simple Profile, provided by TS layer
188 int postprocflag; //frame-based processing use 216 int chromaformat; ///< 2bits, 2=4:2:0, only defined
189 int broadcast; //TFF/RFF present 217 int postprocflag; ///< Per-frame processing suggestion flag present
190 int interlace; //Progressive/interlaced (RPTFTM syntax element) 218 int broadcast; ///< TFF/RFF present
191 int tfcntrflag; //TFCNTR present 219 int interlace; ///< Progressive/interlaced (RPTFTM syntax element)
192 int panscanflag; //NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} presents 220 int tfcntrflag; ///< TFCNTR present
193 int extended_dmv; 221 int panscanflag; ///< NUMPANSCANWIN, TOPLEFT{X,Y}, BOTRIGHT{X,Y} present
194 int color_prim; //8 222 int extended_dmv; ///< Additional extended dmv range at P/B frame-level
195 int transfer_char; //8 223 int color_prim; ///< 8bits, chroma coordinates of the color primaries
196 int matrix_coef; //8 224 int transfer_char; ///< 8bits, Opto-electronic transfer characteristics
197 int hrd_param_flag; 225 int matrix_coef; ///< 8bits, Color primaries->YCbCr transform matrix
198 #endif 226 int hrd_param_flag; ///< Presence of Hypothetical Reference
199 227 ///< Decoder parameters
200 /* All Profiles */ 228 //@}
201 /* TODO: move all int to flags */ 229 #endif
202 int profile; //2 230
203 int frmrtq_postproc; //3 231
204 int bitrtq_postproc; //5 232 /** Sequence header data for all Profiles
205 int loopfilter; 233 * TODO: choose between ints, uint8_ts and monobit flags
206 int fastuvmc; //Rounding of qpel vector to hpel ? (not in Simple) 234 */
207 int extended_mv; //Ext MV in P/B (not in Simple) 235 //@{
208 int dquant; //How qscale varies with MBs, 2bits (not in Simple) 236 int profile; ///< 2bits, Profile
209 int vstransform; //variable-size transform46 237 int frmrtq_postproc; ///< 3bits,
210 238 int bitrtq_postproc; ///< 5bits, quantized framerate-based postprocessing strength
211 239 int fastuvmc; ///< Rounding of qpel vector to hpel ? (not in Simple)
212 int overlap; //overlapped transforms in use 240 int extended_mv; ///< Ext MV in P/B (not in Simple)
213 int quantizer_mode; //2, quantizer mode used for sequence, see QUANT_* 241 int dquant; ///< How qscale varies with MBs, 2bits (not in Simple)
214 int finterpflag; //INTERPFRM present 242 int vstransform; ///< variable-size [48]x[48] transform type + info
215 243 int overlap; ///< overlapped transforms in use
216 244 int quantizer_mode; ///< 2bits, quantizer mode used for sequence, see QUANT_*
217 /*****************************/ 245 int finterpflag; ///< INTERPFRM present
218 /* Frame decoding */ 246 //@}
219 /*****************************/ 247
220 /* All profiles */ 248 /** Frame decoding info for all profiles */
221 uint8_t mv_mode, mv_mode2; /* MV coding mode */ 249 //@{
222 uint8_t pq, altpq; /* Quantizers */ 250 uint8_t mv_mode; ///< MV coding monde
223 uint8_t dquantfrm, dqprofile, dqsbedge, dqbilevel; /* pquant parameters */ 251 uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
224 int tile; /* 3x2 if (width_mb%3) else 2x3 */ 252 uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
225 int ac_table_level; 253 /** pquant parameters */
226 VLC *luma_dc_vlc, *chroma_dc_vlc; /* transac/dcfrm bits are indexes */ 254 //@{
227 uint8_t ttmbf, ttfrm; /* Transform type */ 255 uint8_t dquantfrm;
228 uint8_t lumscale, lumshift; /* Luma compensation parameters */ 256 uint8_t dqprofile;
229 int16_t bfraction; /* Relative position % anchors=> how to scale MVs */ 257 uint8_t dqsbedge;
230 uint8_t halfpq; /* Uniform quant over image and qp+.5 */ 258 uint8_t dqbilevel;
231 uint8_t respic; 259 //@}
232 int buffer_fullness; /* For HRD ? */ 260 int ac_table_level; ///< Index for AC tables from ACFRM element
233 /* Ranges: 261 VLC *luma_dc_vlc; ///< Pointer to current luma DC VLC table
234 * 0 -> [-64n 63.f] x [-32, 31.f] 262 VLC *chroma_dc_vlc; ///< Pointer to current luma AC VLC table
235 * 1 -> [-128, 127.f] x [-64, 63.f] 263 int ttfrm; ///< Transform type info present at frame level
236 * 2 -> [-512, 511.f] x [-128, 127.f] 264 uint8_t ttmbf; ///< Transform type
237 * 3 -> [-1024, 1023.f] x [-256, 255.f] 265 /** Luma compensation parameters */
238 */ 266 //@{
239 uint8_t mvrange; 267 uint8_t lumscale;
240 uint8_t pquantizer; 268 uint8_t lumshift;
241 uint8_t *previous_line_cbpcy; /* To use for predicted CBPCY */ 269 //@}
242 VLC *cbpcy_vlc /* Current CBPCY VLC table */, 270 int16_t bfraction; ///< Relative position % anchors=> how to scale MVs
243 *ttmb_vlc /* Current MB Transform Type VLC table */; 271 uint8_t halfpq; ///< Uniform quant over image and qp+.5
244 BitPlane mv_type_mb_plane; /* bitplane for mv_type == (4MV) */ 272 uint8_t respic; ///< Frame-level flag for resized images
245 BitPlane skip_mb_plane, /* bitplane for skipped MBs */ 273 int buffer_fullness; ///< HRD info
246 direct_mb_plane; /* bitplane for "direct" MBs */ 274 /** Ranges:
247 275 * -# 0 -> [-64n 63.f] x [-32, 31.f]
248 /* S/M only ? */ 276 * -# 1 -> [-128, 127.f] x [-64, 63.f]
249 uint8_t rangeredfrm; /* out_sample = CLIP((in_sample-128)*2+128) */ 277 * -# 2 -> [-512, 511.f] x [-128, 127.f]
250 uint8_t interpfrm; 278 * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
279 */
280 uint8_t mvrange;
281 uint8_t pquantizer;
282 uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY
283 VLC *cbpcy_vlc; ///< Current CBPCY VLC table
284 VLC *ttmb_vlc; ///< Current MB Transform Type VLC table
285 BitPlane mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
286 BitPlane skip_mb_plane; ///< bitplane for skipped MBs
287 BitPlane direct_mb_plane; ///< bitplane for "direct" MBs
288
289 /** Frame decoding info for S/M profiles only */
290 //@{
291 uint8_t rangeredfrm; ///< out_sample = CLIP((in_sample-128)*2+128)
292 uint8_t interpfrm;
293 //@}
251 294
252 #if HAS_ADVANCED_PROFILE 295 #if HAS_ADVANCED_PROFILE
253 /* Advanced */ 296 /** Frame decoding info for Advanced profile */
254 uint8_t fcm; //0->Progressive, 2->Frame-Interlace, 3->Field-Interlace 297 //@{
255 uint8_t numpanscanwin; 298 uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
256 uint8_t tfcntr; 299 uint8_t numpanscanwin;
257 uint8_t rptfrm, tff, rff; 300 uint8_t tfcntr;
258 uint8_t topleftx; 301 uint8_t rptfrm, tff, rff;
259 uint8_t toplefty; 302 uint8_t topleftx;
260 uint8_t bottomrightx; 303 uint8_t toplefty;
261 uint8_t bottomrighty; 304 uint8_t bottomrightx;
262 uint8_t uvsamp; 305 uint8_t bottomrighty;
263 uint8_t postproc; 306 uint8_t uvsamp;
264 int hrd_num_leaky_buckets; 307 uint8_t postproc;
265 uint8_t bit_rate_exponent; 308 int hrd_num_leaky_buckets;
266 uint8_t buffer_size_exponent; 309 uint8_t bit_rate_exponent;
267 BitPlane ac_pred_plane; //AC prediction flags bitplane 310 uint8_t buffer_size_exponent;
268 BitPlane over_flags_plane; //Overflags bitplane 311 BitPlane ac_pred_plane; ///< AC prediction flags bitplane
269 uint8_t condover; 312 BitPlane over_flags_plane; ///< Overflags bitplane
270 uint16_t *hrd_rate, *hrd_buffer; 313 uint8_t condover;
271 VLC *luma_ac2_vlc, *chroma_ac2_vlc; 314 uint16_t *hrd_rate, *hrd_buffer;
315 int ac2_table_level; ///< Index for AC2 tables from AC2FRM element
316 //@}
272 #endif 317 #endif
273 } VC9Context; 318 } VC9Context;
274 319
275 /* FIXME Slow and ugly */ 320 /**
321 * Get unary code of limited length
322 * @fixme FIXME Slow and ugly
323 * @param gb GetBitContext
324 * @param[in] stop The bitstop value (unary code of 1's or 0's)
325 * @param[in] len Maximum length
326 * @return Unary length/index
327 */
276 static int get_prefix(GetBitContext *gb, int stop, int len) 328 static int get_prefix(GetBitContext *gb, int stop, int len)
277 { 329 {
278 #if 1 330 #if 1
279 int i = 0, tmp = !stop; 331 int i = 0, tmp = !stop;
280 332
304 CLOSE_READER(re, gb); 356 CLOSE_READER(re, gb);
305 return limit; 357 return limit;
306 #endif 358 #endif
307 } 359 }
308 360
361 /**
362 * Init VC-9 specific tables and VC9Context members
363 * @param v The VC9Context to initialize
364 * @return Status
365 */
309 static int vc9_init_common(VC9Context *v) 366 static int vc9_init_common(VC9Context *v)
310 { 367 {
311 static int done = 0; 368 static int done = 0;
312 int i; 369 int i;
313 370
314 /* Set the bit planes */ 371 /* Set the bit planes */
315 /* FIXME memset better ? (16bytes) */
316 v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 }; 372 v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
317 v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 }; 373 v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
318 v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 }; 374 v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
319 #if HAS_ADVANCED_PROFILE 375 #if HAS_ADVANCED_PROFILE
320 v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 }; 376 v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
321 v->hrd_rate = v->hrd_buffer = NULL; 377 v->hrd_rate = v->hrd_buffer = NULL;
322 #endif 378 #endif
323 379
324 /* VLC tables */ 380 /* VLC tables */
325 #if TILE_VLC_METHOD == 1 381 #if VLC_NORM6_METH0D == 1
326 # if 0 // spec -> actual tables converter 382 # if 0 // spec -> actual tables converter
327 for(i=0; i<64; i++){ 383 for(i=0; i<64; i++){
328 int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3]; 384 int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3];
329 av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code); 385 av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
330 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n"); 386 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
343 vc9_bfraction_bits, 1, 1, 399 vc9_bfraction_bits, 1, 1,
344 vc9_bfraction_codes, 1, 1, 1); 400 vc9_bfraction_codes, 1, 1, 1);
345 INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4, 401 INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4,
346 vc9_norm2_bits, 1, 1, 402 vc9_norm2_bits, 1, 1,
347 vc9_norm2_codes, 1, 1, 1); 403 vc9_norm2_codes, 1, 1, 1);
348 #if TILE_VLC_METHOD == 1 404 #if VLC_NORM6_METH0D == 1
349 INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64, 405 INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64,
350 vc9_norm6_bits, 1, 1, 406 vc9_norm6_bits, 1, 1,
351 vc9_norm6_codes, 2, 2, 1); 407 vc9_norm6_codes, 2, 2, 1);
352 #endif 408 #endif
353 #if TILE_VLC_METHOD == 2 409 #if VLC_NORM6_METH0D == 2
354 INIT_VLC(&vc9_norm6_first, VC9_NORM6_FIRST_BITS, 64, 410 INIT_VLC(&vc9_norm6_first_vlc, VC9_NORM6_FIRST_BITS, 24,
355 &vc9_norm6_first[0][1], 1, 1, 411 &vc9_norm6_first[0][1], 1, 1,
356 &vc9_norm6_first[0][0], 1, 1, 1); 412 &vc9_norm6_first[0][0], 1, 1, 1);
357 INIT_VLC(&vc9_norm6_second, VC9_NORM6_SECOND_BITS, 64, 413 INIT_VLC(&vc9_norm6_second_vlc, VC9_NORM6_SECOND_BITS, 22,
358 vc9_norm6_second[0][1], 1, 1, 414 &vc9_norm6_second[0][1], 1, 1,
359 vc9_norm6_second[0][1], 1, 1, 1); 415 &vc9_norm6_second[0][0], 1, 1, 1);
360 #endif 416 #endif
361 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7, 417 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7,
362 vc9_imode_bits, 1, 1, 418 vc9_imode_bits, 1, 1,
363 vc9_imode_codes, 1, 1, 1); 419 vc9_imode_codes, 1, 1, 1);
364 for (i=0; i<3; i++) 420 for (i=0; i<3; i++)
387 443
388 return 0; 444 return 0;
389 } 445 }
390 446
391 #if HAS_ADVANCED_PROFILE 447 #if HAS_ADVANCED_PROFILE
392 /* 6.2.1, p32 */ 448 /**
449 * Decode sequence header's Hypothetic Reference Decoder data
450 * @see 6.2.1, p32
451 * @param v The VC9Context to initialize
452 * @param gb A GetBitContext initialized from AVCodecContext extra_data
453 * @return Status
454 */
393 static int decode_hrd(VC9Context *v, GetBitContext *gb) 455 static int decode_hrd(VC9Context *v, GetBitContext *gb)
394 { 456 {
395 int i, num; 457 int i, num;
396 458
397 num = get_bits(gb, 5); 459 num = get_bits(gb, 5);
436 } 498 }
437 } 499 }
438 return 0; 500 return 0;
439 } 501 }
440 502
441 /* Table 2, p18 */ 503 /**
504 * Decode sequence header for Advanced Profile
505 * @see Table 2, p18
506 * @see 6.1.7, pp21-27
507 * @param v The VC9Context to initialize
508 * @param gb A GetBitContext initialized from AVCodecContext extra_data
509 * @return Status
510 */
442 static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb) 511 static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
443 { 512 {
444 VC9Context *v = avctx->priv_data; 513 VC9Context *v = avctx->priv_data;
445 int nr, dr, aspect_ratio; 514 int nr, dr, aspect_ratio;
446 515
583 av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n"); 652 av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
584 return -1; 653 return -1;
585 } 654 }
586 #endif 655 #endif
587 656
588 /* Figure 7-8, p16-17 */ 657 /**
658 * Decode Simple/Main Profiles sequence header
659 * @see Figure 7-8, p16-17
660 * @param avctx Codec context
661 * @param gb GetBit context initialized from Codec context extra_data
662 * @return Status
663 */
589 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb) 664 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
590 { 665 {
591 VC9Context *v = avctx->priv_data; 666 VC9Context *v = avctx->priv_data;
592 667
593 v->profile = get_bits(gb, 2); 668 v->profile = get_bits(gb, 2);
704 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n" 779 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
705 "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n" 780 "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
706 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n" 781 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
707 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n", 782 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
708 v->profile, v->frmrtq_postproc, v->bitrtq_postproc, 783 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
709 v->loopfilter, v->multires, v->fastuvmc, v->extended_mv, 784 v->s.loop_filter, v->multires, v->fastuvmc, v->extended_mv,
710 v->rangered, v->vstransform, v->overlap, v->s.resync_marker, 785 v->rangered, v->vstransform, v->overlap, v->s.resync_marker,
711 v->dquant, v->quantizer_mode, avctx->max_b_frames 786 v->dquant, v->quantizer_mode, avctx->max_b_frames
712 ); 787 );
713 return 0; 788 return 0;
714 #endif 789 #endif
718 #endif 793 #endif
719 } 794 }
720 795
721 796
722 #if HAS_ADVANCED_PROFILE 797 #if HAS_ADVANCED_PROFILE
723 /*****************************************************************************/ 798 /** Entry point decoding (Advanced Profile)
724 /* Entry point decoding (Advanced Profile) */ 799 * @param avctx Codec context
725 /*****************************************************************************/ 800 * @param gb GetBit context initialized from avctx->extra_data
801 * @return Status
802 */
726 static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb) 803 static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb)
727 { 804 {
728 VC9Context *v = avctx->priv_data; 805 VC9Context *v = avctx->priv_data;
729 int range_mapy_flag, range_mapuv_flag, i; 806 int range_mapy_flag, range_mapuv_flag, i;
730 if (v->profile != PROFILE_ADVANCED) 807 if (v->profile != PROFILE_ADVANCED)
760 } 837 }
761 return 0; 838 return 0;
762 } 839 }
763 #endif 840 #endif
764 841
765 /******************************************************************************/ 842 /***********************************************************************/
766 /* Bitplane decoding: 8.7, p56 */ 843 /**
767 /******************************************************************************/ 844 * @defgroup bitplane VC9 Bitplane decoding
845 * @see 8.7, p56
846 * @{
847 */
848
849 /** @addtogroup bitplane
850 * Imode types
851 * @{
852 */
768 #define IMODE_RAW 0 853 #define IMODE_RAW 0
769 #define IMODE_NORM2 1 854 #define IMODE_NORM2 1
770 #define IMODE_DIFF2 2 855 #define IMODE_DIFF2 2
771 #define IMODE_NORM6 3 856 #define IMODE_NORM6 3
772 #define IMODE_DIFF6 4 857 #define IMODE_DIFF6 4
773 #define IMODE_ROWSKIP 5 858 #define IMODE_ROWSKIP 5
774 #define IMODE_COLSKIP 6 859 #define IMODE_COLSKIP 6
860 /** @} */ //imode defines
861
862 /** Allocate the buffer from a bitplane, given its dimensions
863 * @param bp Bitplane which buffer is to allocate
864 * @param[in] width Width of the buffer
865 * @param[in] height Height of the buffer
866 * @return Status
867 * @todo TODO: Take into account stride
868 * @todo TODO: Allow use of external buffers ?
869 */
775 int alloc_bitplane(BitPlane *bp, int width, int height) 870 int alloc_bitplane(BitPlane *bp, int width, int height)
776 { 871 {
777 if (!bp || bp->width<0 || bp->height<0) return -1; 872 if (!bp || bp->width<0 || bp->height<0) return -1;
778 bp->data = (uint8_t*)av_malloc(width*height); 873 bp->data = (uint8_t*)av_malloc(width*height);
779 if (!bp->data) return -1; 874 if (!bp->data) return -1;
780 bp->width = bp->stride = width; //FIXME Needed for aligned data ? 875 bp->width = bp->stride = width;
781 bp->height = height; 876 bp->height = height;
782 return 0; 877 return 0;
783 } 878 }
784 879
880 /** Free the bitplane's buffer
881 * @param bp Bitplane which buffer is to free
882 */
785 void free_bitplane(BitPlane *bp) 883 void free_bitplane(BitPlane *bp)
786 { 884 {
787 bp->width = bp->stride = bp->height = 0; 885 bp->width = bp->stride = bp->height = 0;
788 if (bp->data) av_freep(&bp->data); 886 if (bp->data) av_freep(&bp->data);
789 } 887 }
790 888
889 /** Decode rows by checking if they are skiped
890 * @param plane Buffer to store decoded bits
891 * @param[in] width Width of this buffer
892 * @param[in] height Height of this buffer
893 * @param[in] stride of this buffer
894 */
791 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ 895 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){
792 int x, y; 896 int x, y;
793 GetBitContext *gb = &v->s.gb; 897 GetBitContext *gb = &v->s.gb;
794 898
795 for (y=0; y<height; y++){ 899 for (y=0; y<height; y++){
800 plane[x] = get_bits(gb, 1); 904 plane[x] = get_bits(gb, 1);
801 plane += stride; 905 plane += stride;
802 } 906 }
803 } 907 }
804 908
805 //FIXME optimize 909 /** Decode columns by checking if they are skiped
910 * @param plane Buffer to store decoded bits
911 * @param[in] width Width of this buffer
912 * @param[in] height Height of this buffer
913 * @param[in] stride of this buffer
914 * @fixme FIXME: Optimize
915 */
806 static void decode_colskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ 916 static void decode_colskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){
807 int x, y; 917 int x, y;
808 GetBitContext *gb = &v->s.gb; 918 GetBitContext *gb = &v->s.gb;
809 919
810 for (x=0; x<width; x++){ 920 for (x=0; x<width; x++){
816 plane[y*stride] = get_bits(gb, 1); 926 plane[y*stride] = get_bits(gb, 1);
817 plane ++; 927 plane ++;
818 } 928 }
819 } 929 }
820 930
821 //FIXME optimize 931 /** Decode a bitplane's bits
822 //FIXME Use BitPlane struct or return if table is raw (no bits read here but 932 * @param bp Bitplane where to store the decode bits
823 // later on) 933 * @param v VC9 context for bit reading and logging
824 //Elements must be either 0 or 1 934 * @return Status
935 * @fixme FIXME: Optimize
936 * @todo TODO: Decide if a struct is needed
937 */
825 static int bitplane_decoding(BitPlane *bp, VC9Context *v) 938 static int bitplane_decoding(BitPlane *bp, VC9Context *v)
826 { 939 {
827 GetBitContext *gb = &v->s.gb; 940 GetBitContext *gb = &v->s.gb;
828 941
829 int imode, x, y, code, use_vertical_tile, tile_w, tile_h; 942 int imode, x, y, code, use_vertical_tile, tile_w, tile_h;
855 tile_w= use_vertical_tile ? 2 : 3; 968 tile_w= use_vertical_tile ? 2 : 3;
856 tile_h= use_vertical_tile ? 3 : 2; 969 tile_h= use_vertical_tile ? 3 : 2;
857 970
858 for(y= bp->height%tile_h; y< bp->height; y+=tile_h){ 971 for(y= bp->height%tile_h; y< bp->height; y+=tile_h){
859 for(x= bp->width%tile_w; x< bp->width; x+=tile_w){ 972 for(x= bp->width%tile_w; x< bp->width; x+=tile_w){
860 #if TILE_VLC_METHOD == 1 //FIXME Too much optimized ? 973 #if VLC_NORM6_METH0D == 1
861 code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2); 974 code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2);
862 if(code<0){ 975 if(code<0){
863 av_log(v->s.avctx, AV_LOG_DEBUG, "inavlid NORM-6 VLC\n"); 976 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
864 return -1; 977 return -1;
865 } 978 }
866 #endif 979 #endif
867 #if TILE_VLC_METHOD == 2 //TODO Optimize VLC decoding 980 #if VLC_NORM6_METH0D == 2 //Failure
868 code = get_vlc2(gb, vc9_norm6_first.table, VC9_NORM6_FIRST_BITS, 2); 981 code = get_vlc2(gb, vc9_norm6_first_vlc.table, VC9_NORM6_FIRST_BITS, 2);
869 if (vc9_norm6_mode[code] == 1) 982 if (code == 22)
983 {
984 code = vc9_norm6_flc_val[get_bits(gb, 5)];
985 }
986 else if (code == 23)
870 { 987 {
871 # if TRACE 988 # if TRACE
872 code = get_bits(gb, 5); 989 code = get_vlc2(gb, vc9_norm6_second_vlc.table, VC9_NORM6_SECOND_BITS, 2);
873 assert(code>-1 && code<20);
874 code = vc9_norm6_flc_val[code];
875 # else
876 code = vc9_norm6_flc_val[get_bits(gb, 5)];
877 # endif
878 }
879 else if (vc9_norm6_mode[code] == 2)
880 {
881 # if TRACE
882 code = get_vlc2(gb, vc9_norm6_second.table, VC9_NORM6_SECOND_BITS, 2);
883 assert(code>-1 && code<22); 990 assert(code>-1 && code<22);
884 code = vc9_norm6_second_val[code]; 991 code = vc9_norm6_second_val[code];
885 # else 992 # else
886 code = vc9_norm6_second_val[get_vlc2(gb, vc9_norm6_second.table, VC9_NORM6_SECOND_BITS, 2)]; 993 code = vc9_norm6_second_val[get_vlc2(gb, vc9_norm6_second_vlc.table, VC9_NORM6_SECOND_BITS, 2)];
887 # endif 994 # endif
888 #endif //TILE_VLC_METHOD == 2 995 }
996 #endif //VLC_NORM6_METH0D == 2
889 //FIXME following is a pure guess and probably wrong 997 //FIXME following is a pure guess and probably wrong
890 //FIXME A bitplane (0 | !0), so could the shifts be avoided ? 998 //FIXME A bitplane (0 | !0), so could the shifts be avoided ?
891 planep[x + 0*bp->stride]= (code>>0)&1; 999 planep[x + 0*bp->stride]= (code>>0)&1;
892 planep[x + 1 + 0*bp->stride]= (code>>1)&1; 1000 planep[x + 1 + 0*bp->stride]= (code>>1)&1;
893 //FIXME Does branch prediction help here? 1001 //FIXME Does branch prediction help here?
942 planep = bp->data; 1050 planep = bp->data;
943 for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride 1051 for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
944 } 1052 }
945 return (imode<<1) + invert; 1053 return (imode<<1) + invert;
946 } 1054 }
947 1055 /** @} */ //Bitplane group
948 /*****************************************************************************/ 1056
949 /* VOP Dquant decoding */ 1057 /***********************************************************************/
950 /*****************************************************************************/ 1058 /** VOP Dquant decoding
1059 * @param v VC9 Context
1060 */
951 static int vop_dquant_decoding(VC9Context *v) 1061 static int vop_dquant_decoding(VC9Context *v)
952 { 1062 {
953 GetBitContext *gb = &v->s.gb; 1063 GetBitContext *gb = &v->s.gb;
954 int pqdiff; 1064 int pqdiff;
955 1065
985 } 1095 }
986 } 1096 }
987 return 0; 1097 return 0;
988 } 1098 }
989 1099
990 /*****************************************************************************/ 1100 /***********************************************************************/
991 /* All Profiles picture header decoding specific functions */ 1101 /**
992 /* Only pro/epilog differs between Simple/Main and Advanced => check caller */ 1102 * @defgroup all_frame_hdr All VC9 profiles frame header
993 /*****************************************************************************/ 1103 * @brief Part of the frame header decoding from all profiles
994 1104 * @warning Only pro/epilog differs between Simple/Main and Advanced => check caller
995 /* Tables 11+12, p62-65 */ 1105 * @{
1106 */
1107 /** B and BI frame header decoding, primary part
1108 * @see Tables 11+12, p62-65
1109 * @param v VC9 context
1110 * @return Status
1111 * @warning Also handles BI frames
1112 */
996 static int decode_b_picture_primary_header(VC9Context *v) 1113 static int decode_b_picture_primary_header(VC9Context *v)
997 { 1114 {
998 GetBitContext *gb = &v->s.gb; 1115 GetBitContext *gb = &v->s.gb;
999 int pqindex, status; 1116 int pqindex, status;
1000 1117
1060 } 1177 }
1061 1178
1062 return 0; 1179 return 0;
1063 } 1180 }
1064 1181
1182 /** B and BI frame header decoding, secondary part
1183 * @see Tables 11+12, p62-65
1184 * @param v VC9 context
1185 * @return Status
1186 * @warning Also handles BI frames
1187 * @warning To call once all MB arrays are allocated
1188 */
1065 static int decode_b_picture_secondary_header(VC9Context *v) 1189 static int decode_b_picture_secondary_header(VC9Context *v)
1066 { 1190 {
1067 GetBitContext *gb = &v->s.gb; 1191 GetBitContext *gb = &v->s.gb;
1068 int status; 1192 int status;
1069 1193
1114 } 1238 }
1115 /* Epilog (AC/DC syntax) should be done in caller */ 1239 /* Epilog (AC/DC syntax) should be done in caller */
1116 return 0; 1240 return 0;
1117 } 1241 }
1118 1242
1119 /* Tables 5+7, p53-54 and 55-57 */ 1243 /** I frame header decoding, primary part
1244 * @see Tables 5+7, p53-54 and 55-57
1245 * @param v VC9 context
1246 * @return Status
1247 */
1120 static int decode_i_picture_header(VC9Context *v) 1248 static int decode_i_picture_header(VC9Context *v)
1121 { 1249 {
1122 GetBitContext *gb = &v->s.gb; 1250 GetBitContext *gb = &v->s.gb;
1123 int pqindex, status = 0; 1251 int pqindex, status = 0;
1124 1252
1177 1305
1178 /* Epilog (AC/DC syntax) should be done in caller */ 1306 /* Epilog (AC/DC syntax) should be done in caller */
1179 return status; 1307 return status;
1180 } 1308 }
1181 1309
1182 /* Table 9, p58-60 */ 1310 /** P frame header decoding, primary part
1311 * @see Tables 5+7, p53-54 and 55-57
1312 * @param v VC9 context
1313 * @return Status
1314 */
1183 static int decode_p_picture_primary_header(VC9Context *v) 1315 static int decode_p_picture_primary_header(VC9Context *v)
1184 { 1316 {
1185 /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */ 1317 /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */
1186 GetBitContext *gb = &v->s.gb; 1318 GetBitContext *gb = &v->s.gb;
1187 int lowquant, pqindex, status = 0; 1319 int lowquant, pqindex, status = 0;
1216 v->lumshift = get_bits(gb, 6); 1348 v->lumshift = get_bits(gb, 6);
1217 } 1349 }
1218 return 0; 1350 return 0;
1219 } 1351 }
1220 1352
1353 /** P frame header decoding, secondary part
1354 * @see Tables 5+7, p53-54 and 55-57
1355 * @param v VC9 context
1356 * @warning To call once all MB arrays are allocated
1357 * @return Status
1358 */
1221 static int decode_p_picture_secondary_header(VC9Context *v) 1359 static int decode_p_picture_secondary_header(VC9Context *v)
1222 { 1360 {
1223 GetBitContext *gb = &v->s.gb; 1361 GetBitContext *gb = &v->s.gb;
1224 int status = 0; 1362 int status = 0;
1225 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP && 1363 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1263 } 1401 }
1264 } 1402 }
1265 /* Epilog (AC/DC syntax) should be done in caller */ 1403 /* Epilog (AC/DC syntax) should be done in caller */
1266 return 0; 1404 return 0;
1267 } 1405 }
1268 1406 /** @} */ //End of group all_frm_hdr
1269 1407
1408
1409 /***********************************************************************/
1410 /**
1411 * @defgroup std_frame_hdr VC9 Simple/Main Profiles header decoding
1412 * @brief Part of the frame header decoding belonging to Simple/Main Profiles
1413 * @warning Only pro/epilog differs between Simple/Main and Advanced =>
1414 * check caller
1415 * @{
1416 */
1417
1418 /** Frame header decoding, first part, in Simple and Main profiles
1419 * @see Tables 5+7, p53-54 and 55-57
1420 * @param v VC9 context
1421 * @return Status
1422 */
1270 static int standard_decode_picture_primary_header(VC9Context *v) 1423 static int standard_decode_picture_primary_header(VC9Context *v)
1271 { 1424 {
1272 GetBitContext *gb = &v->s.gb; 1425 GetBitContext *gb = &v->s.gb;
1273 int status = 0; 1426 int status = 0;
1274 1427
1301 return status; 1454 return status;
1302 } 1455 }
1303 return 0; 1456 return 0;
1304 } 1457 }
1305 1458
1459 /** Frame header decoding, secondary part
1460 * @param v VC9 context
1461 * @warning To call once all MB arrays are allocated
1462 * @return Status
1463 */
1306 static int standard_decode_picture_secondary_header(VC9Context *v) 1464 static int standard_decode_picture_secondary_header(VC9Context *v)
1307 { 1465 {
1308 GetBitContext *gb = &v->s.gb; 1466 GetBitContext *gb = &v->s.gb;
1309 int status = 0, index; 1467 int status = 0, index;
1310 1468
1316 1474
1317 /* AC Syntax */ 1475 /* AC Syntax */
1318 v->ac_table_level = decode012(gb); 1476 v->ac_table_level = decode012(gb);
1319 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) 1477 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1320 { 1478 {
1321 index = decode012(gb); 1479 v->ac2_table_level = decode012(gb);
1322 v->luma_ac2_vlc = NULL + index; //FIXME Add AC2 table
1323 v->chroma_ac2_vlc = NULL + index;
1324 } 1480 }
1325 /* DC Syntax */ 1481 /* DC Syntax */
1326 index = decode012(gb); 1482 index = decode012(gb);
1327 v->luma_dc_vlc = &ff_msmp4_dc_luma_vlc[index]; 1483 v->luma_dc_vlc = &ff_msmp4_dc_luma_vlc[index];
1328 v->chroma_dc_vlc = &ff_msmp4_dc_chroma_vlc[index]; 1484 v->chroma_dc_vlc = &ff_msmp4_dc_chroma_vlc[index];
1329 1485
1330 return 0; 1486 return 0;
1331 } 1487 }
1332 1488 /** @} */ //End for group std_frame_hdr
1333 1489
1334 #if HAS_ADVANCED_PROFILE 1490 #if HAS_ADVANCED_PROFILE
1335 /******************************************************************************/ 1491 /***********************************************************************/
1336 /* Advanced Profile picture header decoding specific functions */ 1492 /**
1337 /******************************************************************************/ 1493 * @defgroup adv_frame_hdr VC9 Advanced Profile header decoding
1494 * @brief Part of the frame header decoding belonging to Advanced Profiles
1495 * @warning Only pro/epilog differs between Simple/Main and Advanced =>
1496 * check caller
1497 * @{
1498 */
1499 /** Frame header decoding, primary part
1500 * @param v VC9 context
1501 * @return Status
1502 */
1338 static int advanced_decode_picture_primary_header(VC9Context *v) 1503 static int advanced_decode_picture_primary_header(VC9Context *v)
1339 { 1504 {
1340 GetBitContext *gb = &v->s.gb; 1505 GetBitContext *gb = &v->s.gb;
1341 static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE }; 1506 static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE };
1342 int type, i; 1507 int type, i;
1390 default: break; 1555 default: break;
1391 } 1556 }
1392 return 0; 1557 return 0;
1393 } 1558 }
1394 1559
1560 /** Frame header decoding, secondary part
1561 * @param v VC9 context
1562 * @return Status
1563 */
1395 static int advanced_decode_picture_secondary_header(VC9Context *v) 1564 static int advanced_decode_picture_secondary_header(VC9Context *v)
1396 { 1565 {
1397 GetBitContext *gb = &v->s.gb; 1566 GetBitContext *gb = &v->s.gb;
1398 int index; 1567 int index;
1399 1568
1406 1575
1407 /* AC Syntax */ 1576 /* AC Syntax */
1408 v->ac_table_level = decode012(gb); 1577 v->ac_table_level = decode012(gb);
1409 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) 1578 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1410 { 1579 {
1411 index = decode012(gb); //FIXME 1580 v->ac2_table_level = decode012(gb);
1412 v->luma_ac2_vlc = NULL + index;
1413 v->chroma_ac2_vlc = NULL + index;
1414 } 1581 }
1415 /* DC Syntax */ 1582 /* DC Syntax */
1416 index = decode012(gb); 1583 index = decode012(gb);
1417 v->luma_dc_vlc = &ff_msmp4_dc_luma_vlc[index]; 1584 v->luma_dc_vlc = &ff_msmp4_dc_luma_vlc[index];
1418 v->chroma_dc_vlc = &ff_msmp4_dc_chroma_vlc[index]; 1585 v->chroma_dc_vlc = &ff_msmp4_dc_chroma_vlc[index];
1419 1586
1420 return 0; 1587 return 0;
1421 } 1588 }
1422 #endif 1589 #endif
1423 1590 /** @} */ //End for adv_frame_hdr
1424 /******************************************************************************/ 1591
1425 /* Block decoding functions */ 1592 /***********************************************************************/
1426 /******************************************************************************/ 1593 /**
1427 /* 7.1.4, p91 and 8.1.1.7, p(1)04 */ 1594 * @defgroup block VC9 Block-level functions
1428 /* FIXME proper integration (unusable and lots of parameters to send */ 1595 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1596 * @todo TODO: Integrate to MpegEncContext facilities
1597 * @{
1598 */
1599 /** Decode a luma intra block
1600 * @warning Will be removed, due to necessary integration
1601 * @see coeff scaling for Adv Profile: 8.1.1.15, p(1)13,
1602 * @param v VC9 context
1603 * @param mquant Macroblock quantizer scale
1604 * @return Status
1605 * @todo TODO: Implement Coeff scaling for Advanced Profile
1606 */
1429 int decode_luma_intra_block(VC9Context *v, int mquant) 1607 int decode_luma_intra_block(VC9Context *v, int mquant)
1430 { 1608 {
1431 GetBitContext *gb = &v->s.gb; 1609 GetBitContext *gb = &v->s.gb;
1432 int dcdiff; 1610 int dcdiff;
1433 1611
1454 } 1632 }
1455 /* FIXME: 8.1.1.15, p(1)13, coeff scaling for Adv Profile */ 1633 /* FIXME: 8.1.1.15, p(1)13, coeff scaling for Adv Profile */
1456 1634
1457 return 0; 1635 return 0;
1458 } 1636 }
1459 1637 /** @} */ //End for group block
1460 /******************************************************************************/ 1638
1461 /* MacroBlock decoding functions */ 1639 /***********************************************************************/
1462 /******************************************************************************/ 1640 /**
1463 /* 8.1.1.5, p(1)02-(1)03 */ 1641 * @defgroup std_mb VC9 Macroblock-level functions in Simple/Main Profiles
1464 /* We only need to store 3 flags, but math with 4 is easier */ 1642 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1643 * @todo TODO: Integrate to MpegEncContext facilities
1644 * @{
1645 */
1646 /**
1647 * @def GET_CBPCY(table, bits)
1648 * @brief Get the Coded Block Pattern for luma and chroma
1649 * @param table VLC table to use (get_vlc2 second parameter)
1650 * @param bits Average bitlength (third parameter to get_vlc2)
1651 * @see 8.1.1.5, p(1)02-(1)03
1652 */
1465 #define GET_CBPCY(table, bits) \ 1653 #define GET_CBPCY(table, bits) \
1466 predicted_cbpcy = get_vlc2(gb, table, bits, 2); \ 1654 predicted_cbpcy = get_vlc2(gb, table, bits, 2); \
1467 cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \ 1655 cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \
1468 ? previous_cbpcy[1] : p_cbpcy[+2]; \ 1656 ? previous_cbpcy[1] : p_cbpcy[+2]; \
1469 cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \ 1657 cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \
1473 ? previous_cbpcy[3] : cbpcy[0]; \ 1661 ? previous_cbpcy[3] : cbpcy[0]; \
1474 cbpcy[2] ^= ((predicted_cbpcy>>3)&0x01); \ 1662 cbpcy[2] ^= ((predicted_cbpcy>>3)&0x01); \
1475 cbpcy[3] = (cbpcy[1] == cbpcy[0]) ? cbpcy[2] : cbpcy[1]; \ 1663 cbpcy[3] = (cbpcy[1] == cbpcy[0]) ? cbpcy[2] : cbpcy[1]; \
1476 cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01); 1664 cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01);
1477 1665
1478 /* 8.1, p100 */ 1666 /** Decode all MBs for an I frame in Simple/Main profile
1667 * @see 8.1, p100
1668 * @todo TODO: Process the blocks
1669 * @todo TODO: Use M$ MPEG-4 cbp prediction
1670 */
1479 static int standard_decode_i_mbs(VC9Context *v) 1671 static int standard_decode_i_mbs(VC9Context *v)
1480 { 1672 {
1481 GetBitContext *gb = &v->s.gb; 1673 GetBitContext *gb = &v->s.gb;
1482 MpegEncContext *s = &v->s; 1674 MpegEncContext *s = &v->s;
1483 int current_mb = 0; /* MB/Block Position info */ 1675 int current_mb = 0; /* MB/Block Position info */
1484 /* FIXME: better to use a pointer than using (x<<4) */
1485 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy, 1676 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
1486 *p_cbpcy /* Pointer to skip some math */; 1677 *p_cbpcy /* Pointer to skip some math */;
1487 1678
1488 /* Reset CBPCY predictors */ 1679 /* Reset CBPCY predictors */
1489 memset(v->previous_line_cbpcy, 0, s->mb_stride<<2); 1680 memset(v->previous_line_cbpcy, 0, s->mb_stride<<2);
1522 } 1713 }
1523 } 1714 }
1524 return 0; 1715 return 0;
1525 } 1716 }
1526 1717
1718 /**
1719 * @def GET_MQUANT
1720 * @brief Get macroblock-level quantizer scale
1721 */
1527 #define GET_MQUANT() \ 1722 #define GET_MQUANT() \
1528 if (v->dquantfrm) \ 1723 if (v->dquantfrm) \
1529 { \ 1724 { \
1530 if (v->dqprofile == DQPROFILE_ALL_MBS) \ 1725 if (v->dqprofile == DQPROFILE_ALL_MBS) \
1531 { \ 1726 { \
1540 else mquant = get_bits(gb, 5); \ 1735 else mquant = get_bits(gb, 5); \
1541 } \ 1736 } \
1542 } \ 1737 } \
1543 } 1738 }
1544 1739
1545 /* MVDATA decoding from 8.3.5.2, p(1)20 */ 1740 /**
1741 * @def GET_MVDATA(_dmv_x, _dmv_y)
1742 * @brief Get MV differentials
1743 * @see MVDATA decoding from 8.3.5.2, p(1)20
1744 * @param dmv_x Horizontal differential for decoded MV
1745 * @param dmv_y Vertical differential for decoded MV
1746 */
1546 #define GET_MVDATA(_dmv_x, _dmv_y) \ 1747 #define GET_MVDATA(_dmv_x, _dmv_y) \
1547 index = 1 + get_vlc2(gb, vc9_mv_diff_vlc[s->mv_table_index].table,\ 1748 index = 1 + get_vlc2(gb, vc9_mv_diff_vlc[s->mv_table_index].table,\
1548 VC9_MV_DIFF_VLC_BITS, 2); \ 1749 VC9_MV_DIFF_VLC_BITS, 2); \
1549 if (index > 36) \ 1750 if (index > 36) \
1550 { \ 1751 { \
1575 val = get_bits(gb, size_table[index1] - val); \ 1776 val = get_bits(gb, size_table[index1] - val); \
1576 sign = 0 - (val&1); \ 1777 sign = 0 - (val&1); \
1577 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ 1778 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1578 } 1779 }
1579 1780
1580 /* 8.1, p(1)15 */ 1781 /** Decode all MBs for an P frame in Simple/Main profile
1782 * @see 8.1, p(1)15
1783 * @todo TODO: Process the blocks
1784 * @todo TODO: Use M$ MPEG-4 cbp prediction
1785 */
1581 static int decode_p_mbs(VC9Context *v) 1786 static int decode_p_mbs(VC9Context *v)
1582 { 1787 {
1583 MpegEncContext *s = &v->s; 1788 MpegEncContext *s = &v->s;
1584 GetBitContext *gb = &v->s.gb; 1789 GetBitContext *gb = &v->s.gb;
1585 int current_mb = 0, i; /* MB/Block Position info */ 1790 int current_mb = 0, i; /* MB/Block Position info */
1728 } 1933 }
1729 } 1934 }
1730 return 0; 1935 return 0;
1731 } 1936 }
1732 1937
1938 /** Decode all MBs for an P frame in Simple/Main profile
1939 * @todo TODO: Process the blocks
1940 * @todo TODO: Use M$ MPEG-4 cbp prediction
1941 */
1733 static int decode_b_mbs(VC9Context *v) 1942 static int decode_b_mbs(VC9Context *v)
1734 { 1943 {
1735 MpegEncContext *s = &v->s; 1944 MpegEncContext *s = &v->s;
1736 GetBitContext *gb = &v->s.gb; 1945 GetBitContext *gb = &v->s.gb;
1737 int current_mb = 0, i /* MB / B postion information */; 1946 int current_mb = 0, i /* MB / B postion information */;
1782 if (v->bfraction > 420 /*1/2*/ && 1991 if (v->bfraction > 420 /*1/2*/ &&
1783 b_mv_type < 3) b_mv_type = 1-b_mv_type; 1992 b_mv_type < 3) b_mv_type = 1-b_mv_type;
1784 } 1993 }
1785 else 1994 else
1786 { 1995 {
1787 /* FIXME getting tired commenting */
1788 GET_MVDATA(dmv1_x, dmv1_y); 1996 GET_MVDATA(dmv1_x, dmv1_y);
1789 if (!s->mb_intra /* b_mv1 tells not intra */) 1997 if (!s->mb_intra /* b_mv1 tells not intra */)
1790 { 1998 {
1791 /* FIXME: actually read it */
1792 b_mv_type = decode012(gb); 1999 b_mv_type = decode012(gb);
1793 if (v->bfraction > 420 /*1/2*/ && 2000 if (v->bfraction > 420 /*1/2*/ &&
1794 b_mv_type < 3) b_mv_type = 1-b_mv_type; 2001 b_mv_type < 3) b_mv_type = 1-b_mv_type;
1795 } 2002 }
1796 } 2003 }
1833 current_mb++; 2040 current_mb++;
1834 } 2041 }
1835 } 2042 }
1836 return 0; 2043 return 0;
1837 } 2044 }
2045 /** @} */ //End for group std_mb
1838 2046
1839 #if HAS_ADVANCED_PROFILE 2047 #if HAS_ADVANCED_PROFILE
2048 /***********************************************************************/
2049 /**
2050 * @defgroup adv_mb VC9 Macroblock-level functions in Advanced Profile
2051 * @todo TODO: Integrate to MpegEncContext facilities
2052 * @todo TODO: Code P, B and BI
2053 * @{
2054 */
1840 static int advanced_decode_i_mbs(VC9Context *v) 2055 static int advanced_decode_i_mbs(VC9Context *v)
1841 { 2056 {
1842 MpegEncContext *s = &v->s; 2057 MpegEncContext *s = &v->s;
1843 GetBitContext *gb = &v->s.gb; 2058 GetBitContext *gb = &v->s.gb;
1844 int mqdiff, mquant, current_mb = 0, over_flags_mb = 0; 2059 int mqdiff, mquant, current_mb = 0, over_flags_mb = 0;
1859 } 2074 }
1860 current_mb++; 2075 current_mb++;
1861 } 2076 }
1862 return 0; 2077 return 0;
1863 } 2078 }
1864 #endif 2079 /** @} */ //End for group adv_mb
1865 2080 #endif
2081
2082 /** Initialize a VC9/WMV3 decoder
2083 * @todo TODO: Handle VC-9 IDUs (Transport level?)
2084 * @todo TODO: Decypher remaining bits in extra_data
2085 */
1866 static int vc9_decode_init(AVCodecContext *avctx) 2086 static int vc9_decode_init(AVCodecContext *avctx)
1867 { 2087 {
1868 VC9Context *v = avctx->priv_data; 2088 VC9Context *v = avctx->priv_data;
1869 MpegEncContext *s = &v->s; 2089 MpegEncContext *s = &v->s;
1870 GetBitContext gb; 2090 GetBitContext gb;
1933 #endif 2153 #endif
1934 2154
1935 return 0; 2155 return 0;
1936 } 2156 }
1937 2157
2158 /** Decode a VC9/WMV3 frame
2159 * @todo TODO: Handle VC-9 IDUs (Transport level?)
2160 * @warning Initial try at using MpegEncContext stuff
2161 */
1938 static int vc9_decode_frame(AVCodecContext *avctx, 2162 static int vc9_decode_frame(AVCodecContext *avctx,
1939 void *data, int *data_size, 2163 void *data, int *data_size,
1940 uint8_t *buf, int buf_size) 2164 uint8_t *buf, int buf_size)
1941 { 2165 {
1942 VC9Context *v = avctx->priv_data; 2166 VC9Context *v = avctx->priv_data;
2151 /* Fake consumption of all data */ 2375 /* Fake consumption of all data */
2152 *data_size = len; 2376 *data_size = len;
2153 return buf_size; //Number of bytes consumed 2377 return buf_size; //Number of bytes consumed
2154 } 2378 }
2155 2379
2380 /** Close a VC9/WMV3 decoder
2381 * @warning Initial try at using MpegEncContext stuff
2382 */
2156 static int vc9_decode_end(AVCodecContext *avctx) 2383 static int vc9_decode_end(AVCodecContext *avctx)
2157 { 2384 {
2158 VC9Context *v = avctx->priv_data; 2385 VC9Context *v = avctx->priv_data;
2159 2386
2160 #if HAS_ADVANCED_PROFILE 2387 #if HAS_ADVANCED_PROFILE