comparison vc9.c @ 2462:5fd4de95731f libavcodec

Fixes: - i/p/b_decode_mbs functions now checks the proper values (from bitplane). I didn't find it very clear, but it seems "raw" = "to read at the MB layer". I used a structure, but we could spare that indirection and add a flag in the VC9Context for each array. - Properly decode (or so I think) the CBPCY (as it is predicted) - many more things patch by anonymous
author michael
date Mon, 24 Jan 2005 23:01:18 +0000
parents 1d818bf32736
children 9baa47d8297b
comparison
equal deleted inserted replaced
2461:1d818bf32736 2462:5fd4de95731f
1 /* 1 /*
2 * VC-9 and WMV3 decoder 2 * VC-9 and WMV3 decoder
3 * Copyright (c) 2005 Anonymous 3 * Copyright (c) 2005 Anonymous
4 * Copyright (c) 2005 Alex Beregszaszi 4 * Copyright (c) 2005 Alex Beregszaszi
5 * Copyright (c) 2005 Michael Niedermayer
5 * 6 *
6 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
23 * @file vc9.c 24 * @file vc9.c
24 * VC-9 and WMV3 decoder 25 * VC-9 and WMV3 decoder
25 * 26 *
26 * TODO: Norm-6 bitplane imode, most AP stuff, optimize, all of MB layer :) 27 * TODO: Norm-6 bitplane imode, most AP stuff, optimize, all of MB layer :)
27 * TODO: use MPV_ !! 28 * TODO: use MPV_ !!
29 * TODO: export decode012 in bitstream.h ?
28 */ 30 */
29 #include "common.h" 31 #include "common.h"
30 #include "dsputil.h" 32 #include "dsputil.h"
31 #include "avcodec.h" 33 #include "avcodec.h"
32 #include "mpegvideo.h" 34 #include "mpegvideo.h"
77 #define DQDOUBLE_BEDGE_TOPLEFT 0 79 #define DQDOUBLE_BEDGE_TOPLEFT 0
78 #define DQDOUBLE_BEDGE_TOPRIGHT 1 80 #define DQDOUBLE_BEDGE_TOPRIGHT 1
79 #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2 81 #define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
80 #define DQDOUBLE_BEDGE_BOTTOMLEFT 3 82 #define DQDOUBLE_BEDGE_BOTTOMLEFT 3
81 83
82
83 /* Start Codes */
84 #define SEQ_SC 0x00000010F /* Sequence Start Code */
85 #define SEQ_EC 0x00000000A /* Sequence End code */
86 #define SEQ_HDR /* Sequence Header */
87 #define ENTRY_SC 0x00000010E /* Entry Point Start Code */
88 #define ENTRY_HDR /* Entry Point Header */
89 #define FRM_SC 0x00000010D /* Frame Start Code */
90 #define FRM_DAT /* Frame Data (includes a Frame Header) */
91 #define FLD_SC 0x00000010C /* Field Start Code */
92 #define FLD1_DAT /* Field 1 Data (includes a Frame Header) */
93 #define FLD2_DAT /* Field 2 Data (includes a Field Header) */
94 #define SLC_SC 0x00000010B /* Slice Start Code */
95 #define SLC_HDR /* Slice Header */
96 #define SLC_DAT /* Slice Data (FrH or FiH possible) */
97
98 /* MV P modes */ 84 /* MV P modes */
99 #define MV_PMODE_1MV_HPEL_BILIN 0 85 #define MV_PMODE_1MV_HPEL_BILIN 0
100 #define MV_PMODE_1MV 1 86 #define MV_PMODE_1MV 1
101 #define MV_PMODE_1MV_HPEL 2 87 #define MV_PMODE_1MV_HPEL 2
102 #define MV_PMODE_MIXED_MV 3 88 #define MV_PMODE_MIXED_MV 3
103 #define MV_PMODE_INTENSITY_COMP 4 89 #define MV_PMODE_INTENSITY_COMP 4
90
91 #define BMV_TYPE_BACKWARD 0
92 #define BMV_TYPE_FORWARD 1
93 #define BMV_TYPE_INTERPOLATED 3
104 94
105 /* MV P mode - the 5th element is only used for mode 1 */ 95 /* MV P mode - the 5th element is only used for mode 1 */
106 static const uint8_t mv_pmode_table[2][5] = { 96 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 }, 97 { 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 } 98 { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
137 #define VC9_NORM2_VLC_BITS 3 127 #define VC9_NORM2_VLC_BITS 3
138 static VLC vc9_norm2_vlc; 128 static VLC vc9_norm2_vlc;
139 #define VC9_NORM6_VLC_BITS 9 129 #define VC9_NORM6_VLC_BITS 9
140 static VLC vc9_norm6_vlc; 130 static VLC vc9_norm6_vlc;
141 /* Could be optimized, one table only needs 8 bits */ 131 /* Could be optimized, one table only needs 8 bits */
142 #define VC9_TTMB_VLC_BITS 12 132 #define VC9_TTMB_VLC_BITS 9 //12
143 static VLC vc9_ttmb_vlc[3]; 133 static VLC vc9_ttmb_vlc[3];
144 #define VC9_MV_DIFF_VLC_BITS 15 134 #define VC9_MV_DIFF_VLC_BITS 9 //15
145 static VLC vc9_mv_diff_vlc[4]; 135 static VLC vc9_mv_diff_vlc[4];
146 #define VC9_CBPCY_I_VLC_BITS 13 136 #define VC9_CBPCY_I_VLC_BITS 9 //13
147 static VLC vc9_cbpcy_i_vlc; 137 static VLC vc9_cbpcy_i_vlc;
148 #define VC9_CBPCY_P_VLC_BITS 14 138 #define VC9_CBPCY_P_VLC_BITS 9 //14
149 static VLC vc9_cbpcy_p_vlc[4]; 139 static VLC vc9_cbpcy_p_vlc[4];
150 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6 140 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6
151 static VLC vc9_4mv_block_pattern_vlc[4]; 141 static VLC vc9_4mv_block_pattern_vlc[4];
152 #define VC9_LUMA_DC_VLC_BITS 9 142 #define VC9_LUMA_DC_VLC_BITS 9
153 static VLC vc9_luma_dc_vlc[2]; 143 static VLC vc9_luma_dc_vlc[2];
144 #define VC9_CHROMA_DC_VLC_BITS 9
145 static VLC vc9_chroma_dc_vlc[2];
146
147 //We mainly need data and is_raw, so this struct could be avoided
148 //to save a level of indirection; feel free to modify
149 typedef struct BitPlane {
150 uint8_t *data;
151 int width, stride;
152 int height;
153 uint8_t is_raw;
154 } BitPlane;
154 155
155 typedef struct VC9Context{ 156 typedef struct VC9Context{
156 /* No MpegEnc context, might be good to use it */ 157 /* No MpegEnc context, might be good to use it */
157 GetBitContext gb; 158 GetBitContext gb;
158 AVCodecContext *avctx; 159 AVCodecContext *avctx;
211 uint8_t pict_type; /* Picture type, mapped on MPEG types */ 212 uint8_t pict_type; /* Picture type, mapped on MPEG types */
212 uint8_t pq, altpq; /* Quantizers */ 213 uint8_t pq, altpq; /* Quantizers */
213 uint8_t dquantfrm, dqprofile, dqsbedge, dqbilevel; /* pquant parameters */ 214 uint8_t dquantfrm, dqprofile, dqsbedge, dqbilevel; /* pquant parameters */
214 int width_mb, height_mb; 215 int width_mb, height_mb;
215 int tile; /* 3x2 if (width_mb%3) else 2x3 */ 216 int tile; /* 3x2 if (width_mb%3) else 2x3 */
216 int transacfrm2, transacfrm, transacdctab; //1bit elements 217 VLC *luma_ac_vlc, *chroma_ac_vlc,
218 *luma_dc_vlc, *chroma_dc_vlc; /* transac/dcfrm bits are indexes */
217 uint8_t ttmbf, ttfrm; /* Transform type */ 219 uint8_t ttmbf, ttfrm; /* Transform type */
218 uint8_t lumscale, lumshift; /* Luma compensation parameters */ 220 uint8_t lumscale, lumshift; /* Luma compensation parameters */
219 int16_t bfraction; /* Relative position % anchors=> how to scale MVs */ 221 int16_t bfraction; /* Relative position % anchors=> how to scale MVs */
220 uint8_t halfpq; /* Uniform quant over image and qp+.5 */ 222 uint8_t halfpq; /* Uniform quant over image and qp+.5 */
221 uint8_t respic; 223 uint8_t respic;
225 * 2 -> [-512, 511.f] x [-128, 127.f] 227 * 2 -> [-512, 511.f] x [-128, 127.f]
226 * 3 -> [-1024, 1023.f] x [-256, 255.f] 228 * 3 -> [-1024, 1023.f] x [-256, 255.f]
227 */ 229 */
228 uint8_t mvrange; 230 uint8_t mvrange;
229 uint8_t pquantizer; 231 uint8_t pquantizer;
232 uint8_t *previous_line_cbpcy; /* To use for predicted CBPCY */
230 VLC *cbpcy_vlc /* Current CBPCY VLC table */, 233 VLC *cbpcy_vlc /* Current CBPCY VLC table */,
231 *mv_diff_vlc /* Current MV Diff VLC table */, 234 *mv_diff_vlc /* Current MV Diff VLC table */,
232 *ttmb_vlc /* Current MB Transform Type VLC table */; 235 *ttmb_vlc /* Current MB Transform Type VLC table */;
233 uint8_t *mv_type_mb_plane; /* bitplane for mv_type == "raw" */ 236 BitPlane mv_type_mb_plane; /* bitplane for mv_type == (4MV) */
234 uint8_t *skip_mb_plane, /* bitplane for skipped MBs */ 237 BitPlane skip_mb_plane, /* bitplane for skipped MBs */
235 *direct_mb_plane; /* bitplane for "direct" MBs */ 238 direct_mb_plane; /* bitplane for "direct" MBs */
236 239
237 /* S/M only ? */ 240 /* S/M only ? */
238 uint8_t rangeredfrm, interpfrm; 241 uint8_t rangeredfrm; /* out_sample = CLIP((in_sample-128)*2+128) */
242 uint8_t interpfrm;
239 243
240 #if HAS_ADVANCED_PROFILE 244 #if HAS_ADVANCED_PROFILE
241 /* Advanced */ 245 /* Advanced */
242 uint8_t fcm; //0->Progressive, 2->Frame-Interlace, 3->Field-Interlace 246 uint8_t fcm; //0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
243 uint8_t numpanscanwin; 247 uint8_t numpanscanwin;
251 uint8_t uvsamp; 255 uint8_t uvsamp;
252 uint8_t postproc; 256 uint8_t postproc;
253 int hrd_num_leaky_buckets; 257 int hrd_num_leaky_buckets;
254 uint8_t bit_rate_exponent; 258 uint8_t bit_rate_exponent;
255 uint8_t buffer_size_exponent; 259 uint8_t buffer_size_exponent;
256 uint8_t *ac_pred_plane; 260 BitPlane ac_pred_plane; //AC prediction flags bitplane
257 uint8_t *over_flags_plane; 261 BitPlane over_flags_plane; //Overflags bitplane
262 uint8_t condover;
258 uint16_t *hrd_rate, *hrd_buffer; 263 uint16_t *hrd_rate, *hrd_buffer;
264 VLC *luma_ac2_vlc, *chroma_ac2_vlc;
259 #endif 265 #endif
260 } VC9Context; 266 } VC9Context;
261 267
262 /* FIXME Slow and ugly */ 268 /* FIXME Slow and ugly */
263 static int get_prefix(GetBitContext *gb, int stop, int len) 269 static int get_prefix(GetBitContext *gb, int stop, int len)
264 { 270 {
271 #if 1
265 int i = 0, tmp = !stop; 272 int i = 0, tmp = !stop;
266 273
267 while (i != len && tmp != stop) 274 while (i != len && tmp != stop)
268 { 275 {
269 tmp = get_bits(gb, 1); 276 tmp = get_bits(gb, 1);
270 i++; 277 i++;
271 } 278 }
272 return i; 279 return i;
280 #else
281 unsigned int buf;
282 int log;
283
284 OPEN_READER(re, gb);
285 UPDATE_CACHE(re, gb);
286 buf=GET_CACHE(re, gb); //Still not sure
287 if (stop) buf = ~buf;
288
289 log= av_log2(-buf); //FIXME: -?
290 if (log < limit){
291 LAST_SKIP_BITS(re, gb, log+1);
292 CLOSE_READER(re, gb);
293 return log;
294 }
295
296 LAST_SKIP_BITS(re, gb, limit);
297 CLOSE_READER(re, gb);
298 return limit;
299 #endif
300 }
301
302 static int decode012(GetBitContext *gb)
303 {
304 int n;
305 n = get_bits1(gb);
306 if (n == 0)
307 return 0;
308 else
309 return get_bits1(gb) + 1;
273 } 310 }
274 311
275 static int init_common(VC9Context *v) 312 static int init_common(VC9Context *v)
276 { 313 {
277 static int done = 0; 314 static int done = 0;
278 int i; 315 int i;
279 316
280 v->mv_type_mb_plane = v->direct_mb_plane = v->skip_mb_plane = NULL; 317 /* Set the bit planes */
281 v->pq = -1; 318 /* FIXME memset better ? (16bytes) */
319 v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
320 v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
321 v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
282 #if HAS_ADVANCED_PROFILE 322 #if HAS_ADVANCED_PROFILE
283 v->ac_pred_plane = v->over_flags_plane = NULL; 323 v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
284 v->hrd_rate = v->hrd_buffer = NULL; 324 v->hrd_rate = v->hrd_buffer = NULL;
285 #endif 325 #endif
326
327 /* VLC tables */
286 #if 0 // spec -> actual tables converter 328 #if 0 // spec -> actual tables converter
287 for(i=0; i<64; i++){ 329 for(i=0; i<64; i++){
288 int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3]; 330 int code= (vc9_norm6_spec[i][1] << vc9_norm6_spec[i][4]) + vc9_norm6_spec[i][3];
289 av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code); 331 av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
290 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n"); 332 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
311 vc9_cbpcy_i_bits, 1, 1, 353 vc9_cbpcy_i_bits, 1, 1,
312 vc9_cbpcy_i_codes, 2, 2, 1); 354 vc9_cbpcy_i_codes, 2, 2, 1);
313 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7, 355 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7,
314 vc9_imode_bits, 1, 1, 356 vc9_imode_bits, 1, 1,
315 vc9_imode_codes, 1, 1, 1); 357 vc9_imode_codes, 1, 1, 1);
316 for(i=0; i<3; i++) 358 for (i=0; i<2; i++)
359 {
360 INIT_VLC(&vc9_luma_dc_vlc[i], VC9_LUMA_DC_VLC_BITS, 26,
361 vc9_luma_dc_bits[i], 1, 1,
362 vc9_luma_dc_codes[i], 4, 4, 1);
363 INIT_VLC(&vc9_chroma_dc_vlc[i], VC9_CHROMA_DC_VLC_BITS, 26,
364 vc9_chroma_dc_bits[i], 1, 1,
365 vc9_chroma_dc_codes[i], 4, 4, 1);
366 }
367 for (i=0; i<3; i++)
368 {
369 INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16,
370 vc9_ttmb_bits[i], 1, 1,
371 vc9_ttmb_codes[i], 2, 2, 1);
372 }
373 for(i=0; i<4; i++)
317 { 374 {
318 INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16, 375 INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16,
319 vc9_4mv_block_pattern_bits[i], 1, 1, 376 vc9_4mv_block_pattern_bits[i], 1, 1,
320 vc9_4mv_block_pattern_codes[i], 1, 1, 1); 377 vc9_4mv_block_pattern_codes[i], 1, 1, 1);
321 INIT_VLC(&vc9_cbpcy_p_vlc[i], VC9_CBPCY_P_VLC_BITS, 64, 378 INIT_VLC(&vc9_cbpcy_p_vlc[i], VC9_CBPCY_P_VLC_BITS, 64,
322 vc9_cbpcy_p_bits[i], 1, 1, 379 vc9_cbpcy_p_bits[i], 1, 1,
323 vc9_cbpcy_p_codes[i], 2, 2, 1); 380 vc9_cbpcy_p_codes[i], 2, 2, 1);
324 }
325 for (i=0; i<2; i++)
326 {
327 INIT_VLC(&vc9_mv_diff_vlc[i], VC9_MV_DIFF_VLC_BITS, 73, 381 INIT_VLC(&vc9_mv_diff_vlc[i], VC9_MV_DIFF_VLC_BITS, 73,
328 vc9_mv_diff_bits[i], 1, 1, 382 vc9_mv_diff_bits[i], 1, 1,
329 vc9_mv_diff_codes[i], 2, 2, 1); 383 vc9_mv_diff_codes[i], 2, 2, 1);
330 INIT_VLC(&vc9_luma_dc_vlc[i], VC9_LUMA_DC_VLC_BITS, 120, 384 }
331 vc9_luma_dc_bits[i], 1, 1, 385 }
332 vc9_luma_dc_codes[i], 4, 4, 1); 386
333 INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16, 387 /* Other defaults */
334 vc9_ttmb_bits[i], 1, 1, 388 v->pq = -1;
335 vc9_ttmb_codes[i], 2, 2, 1); 389 v->mvrange = 0; /* 7.1.1.18, p80 */
336 }
337 }
338 390
339 return 0; 391 return 0;
340 } 392 }
341 393
342 #if HAS_ADVANCED_PROFILE 394 #if HAS_ADVANCED_PROFILE
395 /* 6.2.1, p32 */
343 static int decode_hrd(VC9Context *v, GetBitContext *gb) 396 static int decode_hrd(VC9Context *v, GetBitContext *gb)
344 { 397 {
345 int i, num; 398 int i, num;
346 399
347 num = get_bits(gb, 5); 400 num = get_bits(gb, 5);
348 401
349 if (v->hrd_rate || num != v->hrd_num_leaky_buckets) 402 if (v->hrd_rate || num != v->hrd_num_leaky_buckets)
350 { 403 {
351 av_freep(&v->hrd_rate); 404 av_freep(&v->hrd_rate);
352 } 405 }
353 if (!v->hrd_rate) v->hrd_rate = av_malloc(num); 406 if (!v->hrd_rate) v->hrd_rate = av_malloc(num*sizeof(uint16_t));
354 if (!v->hrd_rate) return -1; 407 if (!v->hrd_rate) return -1;
355 408
356 if (v->hrd_buffer || num != v->hrd_num_leaky_buckets) 409 if (v->hrd_buffer || num != v->hrd_num_leaky_buckets)
357 { 410 {
358 av_freep(&v->hrd_buffer); 411 av_freep(&v->hrd_buffer);
359 } 412 }
360 if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num); 413 if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num*sizeof(uint16_t));
361 if (!v->hrd_buffer) return -1; 414 if (!v->hrd_buffer) return -1;
362 415
363 v->hrd_num_leaky_buckets = num; 416 v->hrd_num_leaky_buckets = num;
364 417
365 //exponent in base-2 for rate 418 //exponent in base-2 for rate
386 } 439 }
387 } 440 }
388 return 0; 441 return 0;
389 } 442 }
390 443
444 /* Table 2, p18 */
391 static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb) 445 static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
392 { 446 {
393 VC9Context *v = avctx->priv_data; 447 VC9Context *v = avctx->priv_data;
394 int nr, dr, aspect_ratio; 448 int nr, dr, aspect_ratio;
395 449
408 return -1; 462 return -1;
409 } 463 }
410 if (v->extended_mv) 464 if (v->extended_mv)
411 v->extended_dmv = get_bits(gb, 1); 465 v->extended_dmv = get_bits(gb, 1);
412 466
467 /* 6.1.7, p21 */
413 if (get_bits(gb, 1) /* pic_size_flag */) 468 if (get_bits(gb, 1) /* pic_size_flag */)
414 { 469 {
415 avctx->coded_width = get_bits(gb, 12); 470 avctx->coded_width = get_bits(gb, 12);
416 avctx->coded_height = get_bits(gb, 12); 471 avctx->coded_height = get_bits(gb, 12);
417 if ( get_bits(gb, 1) /* disp_size_flag */) 472 if ( get_bits(gb, 1) /* disp_size_flag */)
418 { 473 {
419 avctx->width = get_bits(gb, 14); 474 avctx->width = get_bits(gb, 14);
420 avctx->height = get_bits(gb, 14); 475 avctx->height = get_bits(gb, 14);
421 } 476 }
422 477
478 /* 6.1.7.4, p22 */
423 if ( get_bits(gb, 1) /* aspect_ratio_flag */) 479 if ( get_bits(gb, 1) /* aspect_ratio_flag */)
424 { 480 {
425 aspect_ratio = get_bits(gb, 4); //SAR 481 aspect_ratio = get_bits(gb, 4); //SAR
426 if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED 482 if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED
427 { 483 {
442 { 498 {
443 avctx->coded_width = avctx->width; 499 avctx->coded_width = avctx->width;
444 avctx->coded_height = avctx->height; 500 avctx->coded_height = avctx->height;
445 } 501 }
446 502
503 /* 6.1.8, p23 */
447 if ( get_bits(gb, 1) /* framerateflag */) 504 if ( get_bits(gb, 1) /* framerateflag */)
448 { 505 {
449 if ( get_bits(gb, 1) /* framerateind */) 506 if ( get_bits(gb, 1) /* framerateind */)
450 { 507 {
451 nr = get_bits(gb, 8); 508 nr = get_bits(gb, 8);
479 avctx->frame_rate = 1000000; 536 avctx->frame_rate = 1000000;
480 avctx->frame_rate_base = 31250*(1+nr); 537 avctx->frame_rate_base = 31250*(1+nr);
481 } 538 }
482 } 539 }
483 540
541 /* 6.1.9, p25 */
484 if ( get_bits(gb, 1) /* color_format_flag */) 542 if ( get_bits(gb, 1) /* color_format_flag */)
485 { 543 {
486 //Chromacity coordinates of color primaries 544 //Chromacity coordinates of color primaries
487 //like ITU-R BT.709-2, BT.470-2, ... 545 //like ITU-R BT.709-2, BT.470-2, ...
488 v->color_prim = get_bits(gb, 8); 546 v->color_prim = get_bits(gb, 8);
528 av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n"); 586 av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
529 return -1; 587 return -1;
530 } 588 }
531 #endif 589 #endif
532 590
591 /* Figure 7-8, p16-17 */
533 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb) 592 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
534 { 593 {
535 VC9Context *v = avctx->priv_data; 594 VC9Context *v = avctx->priv_data;
536 595
537 v->profile = get_bits(gb, 2); 596 v->profile = get_bits(gb, 2);
652 v->profile, v->frmrtq_postproc, v->bitrtq_postproc, 711 v->profile, v->frmrtq_postproc, v->bitrtq_postproc,
653 v->loopfilter, v->multires, v->fastuvmc, v->extended_mv, 712 v->loopfilter, v->multires, v->fastuvmc, v->extended_mv,
654 v->rangered, v->vstransform, v->overlap, v->syncmarker, 713 v->rangered, v->vstransform, v->overlap, v->syncmarker,
655 v->dquant, v->quantizer_mode, avctx->max_b_frames 714 v->dquant, v->quantizer_mode, avctx->max_b_frames
656 ); 715 );
716 return 0;
657 #endif 717 #endif
658 } 718 }
659 #if HAS_ADVANCED_PROFILE 719 #if HAS_ADVANCED_PROFILE
660 else decode_advanced_sequence_header(avctx, gb); 720 else return decode_advanced_sequence_header(avctx, gb);
661 #endif 721 #endif
662 } 722 }
663 723
664 724
665 #if HAS_ADVANCED_PROFILE 725 #if HAS_ADVANCED_PROFILE
704 return 0; 764 return 0;
705 } 765 }
706 #endif 766 #endif
707 767
708 /******************************************************************************/ 768 /******************************************************************************/
709 /* Bitplane decoding */ 769 /* Bitplane decoding: 8.7, p56 */
710 /******************************************************************************/ 770 /******************************************************************************/
711 #define IMODE_RAW 0 771 #define IMODE_RAW 0
712 #define IMODE_NORM2 1 772 #define IMODE_NORM2 1
713 #define IMODE_DIFF2 2 773 #define IMODE_DIFF2 2
714 #define IMODE_NORM6 3 774 #define IMODE_NORM6 3
715 #define IMODE_DIFF6 4 775 #define IMODE_DIFF6 4
716 #define IMODE_ROWSKIP 5 776 #define IMODE_ROWSKIP 5
717 #define IMODE_COLSKIP 6 777 #define IMODE_COLSKIP 6
778 int alloc_bitplane(BitPlane *bp, int width, int height)
779 {
780 if (!bp || bp->width<0 || bp->height<0) return -1;
781 bp->data = (uint8_t*)av_malloc(width*height);
782 if (!bp->data) return -1;
783 bp->width = bp->stride = width; //FIXME Needed for aligned data ?
784 bp->height = height;
785 return 0;
786 }
718 787
719 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ 788 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){
720 int x, y; 789 int x, y;
721 790
722 for (y=0; y<height; y++){ 791 for (y=0; y<height; y++){
743 plane ++; 812 plane ++;
744 } 813 }
745 } 814 }
746 815
747 //FIXME optimize 816 //FIXME optimize
748 //FIXME is this supposed to set elements to 0/FF or 0/1? 817 //FIXME is this supposed to set elements to 0/FF or 0/1? 0/x!=0, not used for
749 static int bitplane_decoding(uint8_t* plane, int width, int height, VC9Context *v) 818 // prediction
750 { 819 //FIXME Use BitPlane struct or return if table is raw (no bits read here but
751 int imode, x, y, i, code, use_vertical_tile, tile_w, tile_h; 820 // later on)
752 uint8_t invert, *planep = plane; 821 static int bitplane_decoding(BitPlane *bp, VC9Context *v)
753 int stride= width; 822 {
823 int imode, x, y, code, use_vertical_tile, tile_w, tile_h;
824 uint8_t invert, *planep = bp->data;
754 825
755 invert = get_bits(&v->gb, 1); 826 invert = get_bits(&v->gb, 1);
756 imode = get_vlc2(&v->gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2); 827 imode = get_vlc2(&v->gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2);
757 av_log(v->avctx, AV_LOG_DEBUG, "Bitplane: imode=%i, invert=%i\n", 828
758 imode, invert); 829 bp->is_raw = 0;
759
760 switch (imode) 830 switch (imode)
761 { 831 {
762 case IMODE_RAW: 832 case IMODE_RAW:
763 for (y=0; y<height; y++) 833 //Data is actually read in the MB layer (same for all tests == "raw")
764 { 834 bp->is_raw = 1; //invert ignored
765 for (x=0; x<width; x++) 835 return invert;
766 planep[x] = (-get_bits(&v->gb, 1)); //-1=0xFF
767 planep += stride;
768 }
769 invert=0; //spec says ignore invert if raw
770 break;
771 case IMODE_DIFF2: 836 case IMODE_DIFF2:
772 case IMODE_NORM2: 837 case IMODE_NORM2:
773 if ((height*width) & 1) *(++planep) = get_bits(&v->gb, 1); 838 if ((bp->height*bp->width) & 1) *(++planep) = get_bits(&v->gb, 1);
774 for(i=0; i<(height*width)>>1; i++){ 839 for(x=0; x<(bp->height*bp->width)>>1; x++){
775 code = get_vlc2(&v->gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2); 840 code = get_vlc2(&v->gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
776 *(++planep) = code&1; //lsb => left 841 *(++planep) = code&1; //lsb => left
777 *(++planep) = code&2; //msb => right - this is a bitplane, so only !0 matters 842 *(++planep) = code&2; //msb => right - bitplane => only !0 matters
778 //FIXME width->stride 843 //FIXME width->stride
779 } 844 }
780 break; 845 break;
781 case IMODE_DIFF6: 846 case IMODE_DIFF6:
782 case IMODE_NORM6: 847 case IMODE_NORM6:
783 use_vertical_tile= height%3==0 && width%3!=0; 848 use_vertical_tile= bp->height%3==0 && bp->width%3!=0;
784 tile_w= use_vertical_tile ? 2 : 3; 849 tile_w= use_vertical_tile ? 2 : 3;
785 tile_h= use_vertical_tile ? 3 : 2; 850 tile_h= use_vertical_tile ? 3 : 2;
786 851
787 for(y= height%tile_h; y<height; y+=tile_h){ 852 for(y= bp->height%tile_h; y< bp->height; y+=tile_h){
788 for(x= width%tile_w; x<width; x+=tile_w){ 853 for(x= bp->width%tile_w; x< bp->width; x+=tile_w){
789 code = get_vlc2(&v->gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2); 854 code = get_vlc2(&v->gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2);
790 //FIXME following is a pure guess and probably wrong 855 //FIXME following is a pure guess and probably wrong
791 planep[x + 0*stride]= (code>>0)&1; 856 //FIXME A bitplane (0 | !0), so could the shifts be avoided ?
792 planep[x + 1 + 0*stride]= (code>>1)&1; 857 planep[x + 0*bp->stride]= (code>>0)&1;
858 planep[x + 1 + 0*bp->stride]= (code>>1)&1;
793 if(use_vertical_tile){ 859 if(use_vertical_tile){
794 planep[x + 0 + 1*stride]= (code>>2)&1; 860 planep[x + 0 + 1*bp->stride]= (code>>2)&1;
795 planep[x + 1 + 1*stride]= (code>>3)&1; 861 planep[x + 1 + 1*bp->stride]= (code>>3)&1;
796 planep[x + 0 + 2*stride]= (code>>4)&1; 862 planep[x + 0 + 2*bp->stride]= (code>>4)&1;
797 planep[x + 1 + 2*stride]= (code>>5)&1; 863 planep[x + 1 + 2*bp->stride]= (code>>5)&1;
798 }else{ 864 }else{
799 planep[x + 2 + 0*stride]= (code>>2)&1; 865 planep[x + 2 + 0*bp->stride]= (code>>2)&1;
800 planep[x + 0 + 1*stride]= (code>>3)&1; 866 planep[x + 0 + 1*bp->stride]= (code>>3)&1;
801 planep[x + 1 + 1*stride]= (code>>4)&1; 867 planep[x + 1 + 1*bp->stride]= (code>>4)&1;
802 planep[x + 2 + 1*stride]= (code>>5)&1; 868 planep[x + 2 + 1*bp->stride]= (code>>5)&1;
803 } 869 }
804 } 870 }
805 } 871 }
806 872
807 x= width % tile_w; 873 x= bp->width % tile_w;
808 decode_colskip(plane , x, height , stride, v); 874 decode_colskip(bp->data , x, bp->height , bp->stride, v);
809 decode_rowskip(plane+x, width - x, height % tile_h, stride, v); 875 decode_rowskip(bp->data+x, bp->width - x, bp->height % tile_h, bp->stride, v);
810 876
811 break; 877 break;
812 case IMODE_ROWSKIP: 878 case IMODE_ROWSKIP:
813 decode_rowskip(plane, width, height, stride, v); 879 decode_rowskip(bp->data, bp->width, bp->height, bp->stride, v);
814 break; 880 break;
815 case IMODE_COLSKIP: //Teh ugly 881 case IMODE_COLSKIP: //Teh ugly
816 decode_colskip(plane, width, height, stride, v); 882 decode_colskip(bp->data, bp->width, bp->height, bp->stride, v);
817 break; 883 break;
818 default: break; 884 default: break;
819 } 885 }
820 886
821 /* Applying diff operator */ 887 /* Applying diff operator */
822 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6) 888 if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
823 { 889 {
824 planep = plane; 890 planep = bp->data;
825 planep[0] ^= invert; 891 planep[0] ^= invert;
826 for (x=1; x<width; x++) 892 for (x=1; x<bp->width; x++)
827 planep[x] ^= planep[x-1]; 893 planep[x] ^= planep[x-1];
828 for (y=1; y<height; y++) 894 for (y=1; y<bp->height; y++)
829 { 895 {
830 planep += stride; 896 planep += bp->stride;
831 planep[0] ^= planep[-stride]; 897 planep[0] ^= planep[-bp->stride];
832 for (x=1; x<width; x++) 898 for (x=1; x<bp->width; x++)
833 { 899 {
834 if (planep[x-1] != planep[x-stride]) planep[x] ^= invert; 900 if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
835 else planep[x] ^= planep[x-1]; 901 else planep[x] ^= planep[x-1];
836 } 902 }
837 } 903 }
838 } 904 }
839 else if (invert) 905 else if (invert)
840 { 906 {
841 planep = plane; 907 planep = bp->data;
842 for (x=0; x<width*height; x++) planep[x] = !planep[x]; //FIXME stride 908 for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
843 } 909 }
844 return 0; 910 return (imode<<1) + invert;
845 } 911 }
846 912
847 /*****************************************************************************/ 913 /*****************************************************************************/
848 /* VOP Dquant decoding */ 914 /* VOP Dquant decoding */
849 /*****************************************************************************/ 915 /*****************************************************************************/
924 av_log(v->avctx, AV_LOG_ERROR, "BI_TYPE not supported yet\n"); 990 av_log(v->avctx, AV_LOG_ERROR, "BI_TYPE not supported yet\n");
925 /* Epilog should be done in caller */ 991 /* Epilog should be done in caller */
926 return -1; 992 return -1;
927 } 993 }
928 994
995 /* Tables 11+12, p62-65 */
929 static int decode_b_picture_header(VC9Context *v) 996 static int decode_b_picture_header(VC9Context *v)
930 { 997 {
931 int pqindex; 998 int pqindex, status;
932 999
933 /* Prolog common to all frametypes should be done in caller */ 1000 /* Prolog common to all frametypes should be done in caller */
934 if (v->profile == PROFILE_SIMPLE) 1001 if (v->profile == PROFILE_SIMPLE)
935 { 1002 {
936 av_log(v, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n"); 1003 av_log(v, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n");
987 v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping 1054 v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping
988 } 1055 }
989 1056
990 if (v->mv_mode == MV_PMODE_MIXED_MV) 1057 if (v->mv_mode == MV_PMODE_MIXED_MV)
991 { 1058 {
992 if (bitplane_decoding( v->mv_type_mb_plane, v->width_mb, 1059 status = bitplane_decoding(&v->mv_type_mb_plane, v);
993 v->height_mb, v)<0) 1060 if (status < 0)
994 return -1; 1061 return -1;
1062 #if TRACE
1063 av_log(v->avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1064 "Imode: %i, Invert: %i\n", status>>1, status&1);
1065 #endif
995 } 1066 }
996 1067
997 //bitplane 1068 //bitplane
998 bitplane_decoding(v->direct_mb_plane, v->width_mb, v->height_mb, v); 1069 status = bitplane_decoding(&v->direct_mb_plane, v);
999 bitplane_decoding(v->skip_mb_plane, v->width_mb, v->height_mb, v); 1070 if (status < 0) return -1;
1071 #if TRACE
1072 av_log(v->avctx, AV_LOG_DEBUG, "MB Direct plane encoding: "
1073 "Imode: %i, Invert: %i\n", status>>1, status&1);
1074 #endif
1075
1076 bitplane_decoding(&v->skip_mb_plane, v);
1077 if (status < 0) return -1;
1078 #if TRACE
1079 av_log(v->avctx, AV_LOG_DEBUG, "Skip MB plane encoding: "
1080 "Imode: %i, Invert: %i\n", status>>1, status&1);
1081 #endif
1000 1082
1001 /* FIXME: what is actually chosen for B frames ? */ 1083 /* FIXME: what is actually chosen for B frames ? */
1002 v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(&v->gb, 2)]; 1084 v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(&v->gb, 2)];
1003 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(&v->gb, 2)]; 1085 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(&v->gb, 2)];
1004 if (v->dquant) 1086 if (v->dquant)
1018 } 1100 }
1019 /* Epilog should be done in caller */ 1101 /* Epilog should be done in caller */
1020 return 0; 1102 return 0;
1021 } 1103 }
1022 1104
1105 /* Tables 5+7, p53-54 and 55-57 */
1023 static int decode_i_picture_header(VC9Context *v) 1106 static int decode_i_picture_header(VC9Context *v)
1024 { 1107 {
1025 int pqindex, status = 0, ac_pred, condover; 1108 int pqindex, status = 0, ac_pred;
1026 1109
1027 /* Prolog common to all frametypes should be done in caller */ 1110 /* Prolog common to all frametypes should be done in caller */
1028 //BF = Buffer Fullness 1111 //BF = Buffer Fullness
1029 if (v->profile <= PROFILE_MAIN && get_bits(&v->gb, 7)) 1112 if (v->profile <= PROFILE_MAIN && get_bits(&v->gb, 7))
1030 { 1113 {
1057 ac_pred = get_bits(&v->gb, 1); 1140 ac_pred = get_bits(&v->gb, 1);
1058 if (v->postprocflag) v->postproc = get_bits(&v->gb, 1); 1141 if (v->postprocflag) v->postproc = get_bits(&v->gb, 1);
1059 /* 7.1.1.34 + 8.5.2 */ 1142 /* 7.1.1.34 + 8.5.2 */
1060 if (v->overlap && v->pq<9) 1143 if (v->overlap && v->pq<9)
1061 { 1144 {
1062 condover = get_bits(&v->gb, 1); 1145 v->condover = get_bits(&v->gb, 1);
1063 if (condover) 1146 if (v->condover)
1064 { 1147 {
1065 condover = 2+get_bits(&v->gb, 1); 1148 v->condover = 2+get_bits(&v->gb, 1);
1066 if (condover == 3) 1149 if (v->condover == 3)
1067 status = bitplane_decoding(v->over_flags_plane, 1150 {
1068 v->width_mb, v->height_mb, v); 1151 status = bitplane_decoding(&v->over_flags_plane, v);
1152 if (status < 0) return -1;
1153 #if TRACE
1154 av_log(v->avctx, AV_LOG_DEBUG, "Overflags plane encoding: "
1155 "Imode: %i, Invert: %i\n", status>>1, status&1);
1156 #endif
1157 }
1069 } 1158 }
1070 } 1159 }
1071 } 1160 }
1072 #endif 1161 #endif
1073 1162
1074 /* Epilog should be done in caller */ 1163 /* Epilog should be done in caller */
1075 return status; 1164 return status;
1076 } 1165 }
1077 1166
1167 /* Table 9, p58-60 */
1078 static int decode_p_picture_header(VC9Context *v) 1168 static int decode_p_picture_header(VC9Context *v)
1079 { 1169 {
1080 /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */ 1170 /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */
1081 int lowquant, pqindex; 1171 int lowquant, pqindex, status = 0;
1082 1172
1083 pqindex = get_bits(&v->gb, 5); 1173 pqindex = get_bits(&v->gb, 5);
1084 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) 1174 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1085 v->pq = pquant_table[0][pqindex]; 1175 v->pq = pquant_table[0][pqindex];
1086 else 1176 else
1112 1202
1113 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP && 1203 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1114 v->mv_mode2 == MV_PMODE_MIXED_MV) 1204 v->mv_mode2 == MV_PMODE_MIXED_MV)
1115 || v->mv_mode == MV_PMODE_MIXED_MV) 1205 || v->mv_mode == MV_PMODE_MIXED_MV)
1116 { 1206 {
1117 if (bitplane_decoding(v->mv_type_mb_plane, v->width_mb, 1207 status = bitplane_decoding(&v->mv_type_mb_plane, v);
1118 v->height_mb, v) < 0) 1208 if (status < 0) return -1;
1119 return -1; 1209 #if TRACE
1120 } 1210 av_log(v->avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1121 1211 "Imode: %i, Invert: %i\n", status>>1, status&1);
1122 if (bitplane_decoding(v->skip_mb_plane, v->width_mb, 1212 #endif
1123 v->height_mb, v) < 0) 1213 }
1124 return -1; 1214
1215 status = bitplane_decoding(&v->skip_mb_plane, v);
1216 if (status < 0) return -1;
1217 #if TRACE
1218 av_log(v->avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1219 "Imode: %i, Invert: %i\n", status>>1, status&1);
1220 #endif
1125 1221
1126 /* Hopefully this is correct for P frames */ 1222 /* Hopefully this is correct for P frames */
1127 v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(&v->gb, 2)]; 1223 v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(&v->gb, 2)];
1128 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(&v->gb, 2)]; 1224 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(&v->gb, 2)];
1129 1225
1148 } 1244 }
1149 1245
1150 1246
1151 static int standard_decode_picture_header(VC9Context *v) 1247 static int standard_decode_picture_header(VC9Context *v)
1152 { 1248 {
1153 int status = 0; 1249 int status = 0, index;
1154 1250
1155 if (v->finterpflag) v->interpfrm = get_bits(&v->gb, 1); 1251 if (v->finterpflag) v->interpfrm = get_bits(&v->gb, 1);
1156 skip_bits(&v->gb, 2); //framecnt unused 1252 skip_bits(&v->gb, 2); //framecnt unused
1157 if (v->rangered) v->rangeredfrm = get_bits(&v->gb, 1); 1253 if (v->rangered) v->rangeredfrm = get_bits(&v->gb, 1);
1158 v->pict_type = get_bits(&v->gb, 1); 1254 v->pict_type = get_bits(&v->gb, 1);
1175 { 1271 {
1176 av_log(v, AV_LOG_INFO, "Skipping frame...\n"); 1272 av_log(v, AV_LOG_INFO, "Skipping frame...\n");
1177 return status; 1273 return status;
1178 } 1274 }
1179 1275
1180 /* AC/DC Syntax */ 1276 /* AC Syntax */
1181 v->transacfrm = get_bits(&v->gb, 1); 1277 index = decode012(&v->gb);
1182 if (v->transacfrm) v->transacfrm += get_bits(&v->gb, 1); 1278 v->luma_ac_vlc = NULL + index; //FIXME Add AC table
1279 v->chroma_ac_vlc = NULL + index;
1183 if (v->pict_type == I_TYPE || v->pict_type == BI_TYPE) 1280 if (v->pict_type == I_TYPE || v->pict_type == BI_TYPE)
1184 { 1281 {
1185 v->transacfrm2 = get_bits(&v->gb, 1); 1282 index = decode012(&v->gb);
1186 if (v->transacfrm2) v->transacfrm2 += get_bits(&v->gb, 1); 1283 v->luma_ac2_vlc = NULL + index; //FIXME Add AC2 table
1187 } 1284 v->chroma_ac2_vlc = NULL + index;
1188 v->transacdctab = get_bits(&v->gb, 1); 1285 }
1286 /* DC Syntax */
1287 index = decode012(&v->gb);
1288 v->luma_dc_vlc = vc9_luma_dc_vlc + index;
1289 v->chroma_dc_vlc = vc9_chroma_dc_vlc + index;
1189 1290
1190 return 0; 1291 return 0;
1191 } 1292 }
1192 1293
1193 1294
1196 /* Advanced Profile picture header decoding specific functions */ 1297 /* Advanced Profile picture header decoding specific functions */
1197 /******************************************************************************/ 1298 /******************************************************************************/
1198 static int advanced_decode_picture_header(VC9Context *v) 1299 static int advanced_decode_picture_header(VC9Context *v)
1199 { 1300 {
1200 static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE }; 1301 static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE };
1201 int type, i, ret; 1302 int type, i, index;
1202 1303
1203 if (v->interlace) 1304 if (v->interlace)
1204 { 1305 {
1205 v->fcm = get_bits(&v->gb, 1); 1306 v->fcm = get_bits(&v->gb, 1);
1206 if (v->fcm) v->fcm = 2+get_bits(&v->gb, 1); 1307 if (v->fcm) v->fcm = 2+get_bits(&v->gb, 1);
1247 case BI_TYPE: 1348 case BI_TYPE:
1248 case B_TYPE: if (decode_b_picture_header(v) < 0) return FRAME_SKIPED; 1349 case B_TYPE: if (decode_b_picture_header(v) < 0) return FRAME_SKIPED;
1249 default: break; 1350 default: break;
1250 } 1351 }
1251 1352
1252 /* AC/DC Syntax */ 1353 /* AC Syntax */
1253 v->transacfrm = get_bits(&v->gb, 1); 1354 index = decode012(&v->gb);
1254 if (v->transacfrm) v->transacfrm += get_bits(&v->gb, 1); 1355 v->luma_ac_vlc = NULL + index; //FIXME
1356 v->chroma_ac_vlc = NULL + index; //FIXME
1255 if (v->pict_type == I_TYPE || v->pict_type == BI_TYPE) 1357 if (v->pict_type == I_TYPE || v->pict_type == BI_TYPE)
1256 { 1358 {
1257 v->transacfrm2 = get_bits(&v->gb, 1); 1359 index = decode012(&v->gb); //FIXME
1258 if (v->transacfrm2) v->transacfrm2 += get_bits(&v->gb, 1); 1360 v->luma_ac2_vlc = NULL + index;
1259 } 1361 v->chroma_ac2_vlc = NULL + index;
1260 v->transacdctab = get_bits(&v->gb, 1); 1362 }
1261 if (v->pict_type == I_TYPE) vop_dquant_decoding(v); 1363 /* DC Syntax */
1364 index = decode012(&v->gb);
1365 v->luma_dc_vlc = vc9_luma_dc_vlc + index;
1366 v->chroma_dc_vlc = vc9_chroma_dc_vlc + index;
1262 1367
1263 return 0; 1368 return 0;
1264 } 1369 }
1265 #endif 1370 #endif
1371
1372 /******************************************************************************/
1373 /* Block decoding functions */
1374 /******************************************************************************/
1375 /* 7.1.4, p91 and 8.1.1.7, p(1)04 */
1376 /* FIXME proper integration (unusable and lots of parameters to send */
1377 int decode_luma_intra_block(VC9Context *v, int mquant)
1378 {
1379 int dcdiff;
1380
1381 dcdiff = get_vlc2(&v->gb, v->luma_dc_vlc->table,
1382 VC9_LUMA_DC_VLC_BITS, 2);
1383 if (dcdiff)
1384 {
1385 if (dcdiff == 119 /* ESC index value */)
1386 {
1387 /* TODO: Optimize */
1388 if (mquant == 1) dcdiff = get_bits(&v->gb, 10);
1389 else if (mquant == 2) dcdiff = get_bits(&v->gb, 9);
1390 else dcdiff = get_bits(&v->gb, 8);
1391 }
1392 else
1393 {
1394 if (mquant == 1)
1395 dcdiff = (dcdiff<<2) + get_bits(&v->gb, 2) - 3;
1396 else if (mquant == 2)
1397 dcdiff = (dcdiff<<1) + get_bits(&v->gb, 1) - 1;
1398 }
1399 if (get_bits(&v->gb, 1))
1400 dcdiff = -dcdiff;
1401 }
1402 /* FIXME: 8.1.1.15, p(1)13, coeff scaling for Adv Profile */
1403
1404 return 0;
1405 }
1266 1406
1267 /******************************************************************************/ 1407 /******************************************************************************/
1268 /* MacroBlock decoding functions */ 1408 /* MacroBlock decoding functions */
1269 /******************************************************************************/ 1409 /******************************************************************************/
1410 /* 8.1.1.5, p(1)02-(1)03 */
1411 /* We only need to store 3 flags, but math with 4 is easier */
1412 #define GET_CBPCY(table, bits) \
1413 predicted_cbpcy = get_vlc2(&v->gb, table, bits, 2); \
1414 cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \
1415 ? previous_cbpcy[1] : p_cbpcy[+2]; \
1416 cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \
1417 cbpcy[1] = (p_cbpcy[2] == p_cbpcy[3]) ? cbpcy[0] : p_cbpcy[3]; \
1418 cbpcy[1] ^= ((predicted_cbpcy>>4)&0x01); \
1419 cbpcy[2] = (previous_cbpcy[1] == cbpcy[0]) \
1420 ? previous_cbpcy[3] : cbpcy[0]; \
1421 cbpcy[2] ^= ((predicted_cbpcy>>3)&0x01); \
1422 cbpcy[3] = (cbpcy[1] == cbpcy[0]) ? cbpcy[2] : cbpcy[1]; \
1423 cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01);
1424
1425 /* 8.1, p100 */
1270 static int standard_decode_i_mbs(VC9Context *v) 1426 static int standard_decode_i_mbs(VC9Context *v)
1271 { 1427 {
1272 int x, y, ac_pred, cbpcy; 1428 int x, y, current_mb = 0; /* MB/Block Position info */
1429 int ac_pred;
1430 /* FIXME: better to use a pointer than using (x<<4) */
1431 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
1432 *p_cbpcy /* Pointer to skip some math */;
1433
1434 /* Reset CBPCY predictors */
1435 memset(v->previous_line_cbpcy, 0, (v->width_mb+1)<<2);
1273 1436
1274 /* Select ttmb table depending on pq */ 1437 /* Select ttmb table depending on pq */
1275 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0]; 1438 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0];
1276 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1]; 1439 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1];
1277 else v->ttmb_vlc = &vc9_ttmb_vlc[2]; 1440 else v->ttmb_vlc = &vc9_ttmb_vlc[2];
1278 1441
1279 for (y=0; y<v->height_mb; y++) 1442 for (y=0; y<v->height_mb; y++)
1280 { 1443 {
1281 for (x=0; x<v->width_mb; x++) 1444 /* Init CBPCY for line */
1282 { 1445 *((uint32_t*)previous_cbpcy) = 0x00000000;
1283 cbpcy = get_vlc2(&v->gb, vc9_cbpcy_i_vlc.table, 1446 p_cbpcy = v->previous_line_cbpcy+4;
1284 VC9_CBPCY_I_VLC_BITS, 2); 1447
1448 for (x=0; x<v->width_mb; x++, p_cbpcy += 4)
1449 {
1450 /* Get CBPCY */
1451 GET_CBPCY(vc9_cbpcy_i_vlc.table, VC9_CBPCY_I_VLC_BITS);
1452
1285 ac_pred = get_bits(&v->gb, 1); 1453 ac_pred = get_bits(&v->gb, 1);
1286 //Decode blocks from that mb wrt cbpcy 1454
1455 /* TODO: Decode blocks from that mb wrt cbpcy */
1456
1457 /* Update for next block */
1458 *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
1459 *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
1460 current_mb++;
1287 } 1461 }
1288 } 1462 }
1289 return 0; 1463 return 0;
1290 } 1464 }
1291 1465
1305 else mquant = get_bits(&v->gb, 5); \ 1479 else mquant = get_bits(&v->gb, 5); \
1306 } \ 1480 } \
1307 } \ 1481 } \
1308 } 1482 }
1309 1483
1310 /* MVDATA decoding from 8.3.5.2 */ 1484 /* MVDATA decoding from 8.3.5.2, p(1)20 */
1311 #define GET_MVDATA() \ 1485 #define GET_MVDATA(_dmv_x, _dmv_y) \
1312 index = 1 + get_vlc2(&v->gb, v->mv_diff_vlc->table, \ 1486 index = 1 + get_vlc2(&v->gb, v->mv_diff_vlc->table, \
1313 VC9_MV_DIFF_VLC_BITS, 2); \ 1487 VC9_MV_DIFF_VLC_BITS, 2); \
1314 if (index > 36) \ 1488 if (index > 36) \
1315 { \ 1489 { \
1316 mb_has_coeffs = 1; \ 1490 mb_has_coeffs = 1; \
1317 index -= 37; \ 1491 index -= 37; \
1318 } \ 1492 } \
1319 else mb_has_coeffs = 0; \ 1493 else mb_has_coeffs = 0; \
1320 mb_is_intra = 0; \ 1494 mb_is_intra = 0; \
1321 if (!index) { dmv_x = dmv_y = 0; } \ 1495 if (!index) { _dmv_x = _dmv_y = 0; } \
1322 else if (index == 35) \ 1496 else if (index == 35) \
1323 { \ 1497 { \
1324 dmv_x = get_bits(&v->gb, k_x); \ 1498 _dmv_x = get_bits(&v->gb, k_x); \
1325 dmv_y = get_bits(&v->gb, k_y); \ 1499 _dmv_y = get_bits(&v->gb, k_y); \
1326 mb_is_intra = 1; \ 1500 mb_is_intra = 1; \
1327 } \ 1501 } \
1328 else \ 1502 else \
1329 { \ 1503 { \
1330 index1 = index%6; \ 1504 index1 = index%6; \
1331 if (hpel_flag && index1 == 5) val = 1; \ 1505 if (hpel_flag && index1 == 5) val = 1; \
1332 else val = 0; \ 1506 else val = 0; \
1333 val = get_bits(&v->gb, size_table[index1] - val); \ 1507 val = get_bits(&v->gb, size_table[index1] - val); \
1334 sign = 0 - (val&1); \ 1508 sign = 0 - (val&1); \
1335 dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ 1509 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1336 \ 1510 \
1337 index1 = index/6; \ 1511 index1 = index/6; \
1338 if (hpel_flag && index1 == 5) val = 1; \ 1512 if (hpel_flag && index1 == 5) val = 1; \
1339 else val = 0; \ 1513 else val = 0; \
1340 val = get_bits(&v->gb, size_table[index1] - val); \ 1514 val = get_bits(&v->gb, size_table[index1] - val); \
1341 sign = 0 - (val&1); \ 1515 sign = 0 - (val&1); \
1342 dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ 1516 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1343 } 1517 }
1344 1518
1519 /* 8.1, p(1)15 */
1345 static int decode_p_mbs(VC9Context *v) 1520 static int decode_p_mbs(VC9Context *v)
1346 { 1521 {
1347 int x, y, current_mb = 0, i; /* MB/Block Position info */ 1522 int x, y, current_mb = 0, i; /* MB/Block Position info */
1348 int skip_mb_bit = 0, cbpcy; /* MB/B skip */ 1523 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
1524 *p_cbpcy /* Pointer to skip some math */;
1349 int hybrid_pred, ac_pred; /* Prediction types */ 1525 int hybrid_pred, ac_pred; /* Prediction types */
1350 int mb_has_coeffs = 1 /* last_flag */, mb_is_intra; 1526 int mv_mode_bit = 0;
1351 int dmv_x, dmv_y; /* Differential MV components */
1352 int mv_mode_bit = 0; /* mv_mode_bit: 1MV=0, 4MV=0 */
1353 int mqdiff, mquant; /* MB quantization */ 1527 int mqdiff, mquant; /* MB quantization */
1354 int tt_block; /* MB Transform type */ 1528 int ttmb; /* MB Transform type */
1529
1355 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, 1530 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
1356 offset_table[6] = { 0, 1, 3, 7, 15, 31 }; 1531 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
1532 int mb_has_coeffs = 1 /* last_flag */, mb_is_intra;
1533 int dmv_x, dmv_y; /* Differential MV components */
1357 int k_x, k_y; /* Long MV fixed bitlength */ 1534 int k_x, k_y; /* Long MV fixed bitlength */
1358 int hpel_flag, intra_flag; /* Some MB properties */ 1535 int hpel_flag; /* Some MB properties */
1359 int index, index1; /* LUT indices */ 1536 int index, index1; /* LUT indices */
1360 int val, sign; 1537 int val, sign; /* MVDATA temp values */
1361 1538
1362 /* Select ttmb table depending on pq */ 1539 /* Select ttmb table depending on pq */
1363 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0]; 1540 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0];
1364 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1]; 1541 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1];
1365 else v->ttmb_vlc = &vc9_ttmb_vlc[2]; 1542 else v->ttmb_vlc = &vc9_ttmb_vlc[2];
1375 1552
1376 hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL 1553 hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL
1377 k_x -= hpel_flag; 1554 k_x -= hpel_flag;
1378 k_y -= hpel_flag; 1555 k_y -= hpel_flag;
1379 1556
1557 /* Reset CBPCY predictors */
1558 memset(v->previous_line_cbpcy, 0, (v->width_mb+1)<<2);
1559
1380 for (y=0; y<v->height_mb; y++) 1560 for (y=0; y<v->height_mb; y++)
1381 { 1561 {
1562 /* Init CBPCY for line */
1563 *((uint32_t*)previous_cbpcy) = 0x00000000;
1564 p_cbpcy = v->previous_line_cbpcy+4;
1565
1382 for (x=0; x<v->width_mb; x++) 1566 for (x=0; x<v->width_mb; x++)
1383 { 1567 {
1384 if (v->mv_type_mb_plane[current_mb]) 1568 if (v->mv_type_mb_plane.is_raw)
1385 mv_mode_bit = get_bits(&v->gb, 1); 1569 v->mv_type_mb_plane.data[current_mb] = get_bits(&v->gb, 1);
1386 if (0) //skipmb is rawmode 1570 if (v->skip_mb_plane.is_raw)
1387 skip_mb_bit = get_bits(&v->gb, 1); 1571 v->skip_mb_plane.data[current_mb] = get_bits(&v->gb, 1);
1388 if (!mv_mode_bit) /* 1MV mode */ 1572 if (!mv_mode_bit) /* 1MV mode */
1389 { 1573 {
1390 if (!v->skip_mb_plane[current_mb]) 1574 if (!v->skip_mb_plane.data[current_mb])
1391 { 1575 {
1392 GET_MVDATA(); 1576 GET_MVDATA(dmv_x, dmv_y);
1393 1577
1394 /* hybrid mv pred, 8.3.5.3.4 */ 1578 /* hybrid mv pred, 8.3.5.3.4 */
1395 if (v->mv_mode == MV_PMODE_1MV || 1579 if (v->mv_mode == MV_PMODE_1MV ||
1396 v->mv_mode == MV_PMODE_MIXED_MV) 1580 v->mv_mode == MV_PMODE_MIXED_MV)
1397 hybrid_pred = get_bits(&v->gb, 1); 1581 hybrid_pred = get_bits(&v->gb, 1);
1401 ac_pred = get_bits(&v->gb, 1); 1585 ac_pred = get_bits(&v->gb, 1);
1402 } 1586 }
1403 else if (mb_has_coeffs) 1587 else if (mb_has_coeffs)
1404 { 1588 {
1405 if (mb_is_intra) ac_pred = get_bits(&v->gb, 1); 1589 if (mb_is_intra) ac_pred = get_bits(&v->gb, 1);
1406 cbpcy = get_vlc2(&v->gb, v->cbpcy_vlc->table, 1590 GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
1407 VC9_CBPCY_P_VLC_BITS, 2);
1408 GET_MQUANT(); 1591 GET_MQUANT();
1409 } 1592 }
1410 if (!v->ttmbf) 1593 if (!v->ttmbf)
1411 v->ttfrm = get_vlc2(&v->gb, v->ttmb_vlc->table, 1594 ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table,
1412 VC9_TTMB_VLC_BITS, 2); 1595 VC9_TTMB_VLC_BITS, 12);
1413 //Decode blocks from that mb wrt cbpcy 1596 /* TODO: decode blocks from that mb wrt cbpcy */
1414 } 1597 }
1415 else //Skipped 1598 else //Skipped
1416 { 1599 {
1417 /* hybrid mv pred, 8.3.5.3.4 */ 1600 /* hybrid mv pred, 8.3.5.3.4 */
1418 if (v->mv_mode == MV_PMODE_1MV || 1601 if (v->mv_mode == MV_PMODE_1MV ||
1420 hybrid_pred = get_bits(&v->gb, 1); 1603 hybrid_pred = get_bits(&v->gb, 1);
1421 } 1604 }
1422 } //1MV mode 1605 } //1MV mode
1423 else //4MV mode 1606 else //4MV mode
1424 { 1607 {
1425 if (!v->skip_mb_plane[current_mb] /* unskipped MB */) 1608 if (!v->skip_mb_plane.data[current_mb] /* unskipped MB */)
1426 { 1609 {
1427 cbpcy = get_vlc2(&v->gb, v->cbpcy_vlc->table, 1610 /* Get CBPCY */
1428 VC9_CBPCY_P_VLC_BITS, 2); 1611 GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
1429 for (i=0; i<4; i++) //For all 4 Y blocks 1612 for (i=0; i<4; i++) //For all 4 Y blocks
1430 { 1613 {
1431 if (cbpcy & (1<<6) /* cbpcy set for this block */) 1614 if (cbpcy[i] /* cbpcy set for this block */)
1432 { 1615 {
1433 GET_MVDATA(); 1616 GET_MVDATA(dmv_x, dmv_y);
1434 } 1617 }
1435 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */) 1618 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
1436 hybrid_pred = get_bits(&v->gb, 1); 1619 hybrid_pred = get_bits(&v->gb, 1);
1437 GET_MQUANT(); 1620 GET_MQUANT();
1438 if (mb_is_intra /* One of the 4 blocks is intra */ && 1621 if (mb_is_intra /* One of the 4 blocks is intra */ &&
1439 index /* non-zero pred for that block */) 1622 index /* non-zero pred for that block */)
1440 ac_pred = get_bits(&v->gb, 1); 1623 ac_pred = get_bits(&v->gb, 1);
1441 if (!v->ttmbf) 1624 if (!v->ttmbf)
1442 tt_block = get_vlc2(&v->gb, v->ttmb_vlc->table, 1625 ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table,
1443 VC9_TTMB_VLC_BITS, 2); 1626 VC9_TTMB_VLC_BITS, 12);
1444 1627
1445 /* TODO: Process blocks wrt cbpcy */ 1628 /* TODO: Process blocks wrt cbpcy */
1446 1629
1447 /* Prepare cbpcy for next block */
1448 cbpcy <<= 1;
1449 } 1630 }
1450 } 1631 }
1451 else //Skipped MB 1632 else //Skipped MB
1452 { 1633 {
1453 for (i=0; i<4; i++) //All 4 Y blocks 1634 for (i=0; i<4; i++) //All 4 Y blocks
1454 { 1635 {
1455 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */) 1636 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
1456 hybrid_pred = get_bits(&v->gb, 1); 1637 hybrid_pred = get_bits(&v->gb, 1);
1457 1638
1458 /* FIXME: do something */ 1639 /* TODO: do something */
1459 } 1640 }
1460 } 1641 }
1461 } 1642 }
1462 } 1643
1463 current_mb++; 1644 /* Update for next block */
1645 #if TRACE > 2
1646 av_log(v->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i,"
1647 " cbpcy=%i%i%i%i\n", current_mb,
1648 p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3],
1649 previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3],
1650 cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]);
1651 #endif
1652 *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
1653 *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
1654 current_mb++;
1655 }
1464 } 1656 }
1465 return 0; 1657 return 0;
1466 } 1658 }
1467 1659
1468 static int decode_b_mbs(VC9Context *v) 1660 static int decode_b_mbs(VC9Context *v)
1469 { 1661 {
1470 int x, y, current_mb = 0 , last_mb = v->height_mb*v->width_mb, 1662 int x, y, current_mb = 0, i /* MB / B postion information */;
1471 i /* MB / B postion information */;
1472 int direct_b_bit = 0, skip_mb_bit = 0;
1473 int ac_pred; 1663 int ac_pred;
1474 int b_mv1 = 0, b_mv2 = 0, b_mv_type = 0; 1664 int b_mv_type = BMV_TYPE_BACKWARD;
1475 int mquant, mqdiff; /* MB quant stuff */ 1665 int mquant, mqdiff; /* MB quant stuff */
1476 int tt_block; /* Block transform type */ 1666 int ttmb; /* MacroBlock transform type */
1477 1667
1668 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
1669 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
1670 int mb_has_coeffs = 1 /* last_flag */, mb_is_intra = 1;
1671 int dmv1_x, dmv1_y, dmv2_x, dmv2_y; /* Differential MV components */
1672 int k_x, k_y; /* Long MV fixed bitlength */
1673 int hpel_flag; /* Some MB properties */
1674 int index, index1; /* LUT indices */
1675 int val, sign; /* MVDATA temp values */
1676
1677 /* Select proper long MV range */
1678 switch (v->mvrange)
1679 {
1680 case 1: k_x = 10; k_y = 9; break;
1681 case 2: k_x = 12; k_y = 10; break;
1682 case 3: k_x = 13; k_y = 11; break;
1683 default: /*case 0 too */ k_x = 9; k_y = 8; break;
1684 }
1685 hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL
1686 k_x -= hpel_flag;
1687 k_y -= hpel_flag;
1688
1689 /* Select ttmb table depending on pq */
1690 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0];
1691 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1];
1692 else v->ttmb_vlc = &vc9_ttmb_vlc[2];
1693
1478 for (y=0; y<v->height_mb; y++) 1694 for (y=0; y<v->height_mb; y++)
1479 { 1695 {
1480 for (x=0; x<v->width_mb; x++) 1696 for (x=0; x<v->width_mb; x++)
1481 { 1697 {
1482 if (v->direct_mb_plane[current_mb]) 1698 if (v->direct_mb_plane.is_raw)
1483 direct_b_bit = get_bits(&v->gb, 1); 1699 v->direct_mb_plane.data[current_mb] = get_bits(&v->gb, 1);
1484 if (1 /* Skip mode is raw */) 1700 if (v->skip_mb_plane.is_raw)
1701 v->skip_mb_plane.data[current_mb] = get_bits(&v->gb, 1);
1702
1703 if (!v->direct_mb_plane.data[current_mb])
1485 { 1704 {
1486 /* FIXME getting tired commenting */ 1705 if (v->skip_mb_plane.data[current_mb])
1487 #if 0
1488 skip_mb_bit = get_bits(&v->gb, n); //vlc
1489 #endif
1490 }
1491 if (!direct_b_bit)
1492 {
1493 if (skip_mb_bit)
1494 { 1706 {
1495 /* FIXME getting tired commenting */ 1707 b_mv_type = decode012(&v->gb);
1496 #if 0 1708 if (v->bfraction > 420 /*1/2*/ &&
1497 b_mv_type = get_bits(&v->gb, n); //vlc 1709 b_mv_type < 3) b_mv_type = 1-b_mv_type;
1498 #endif
1499 } 1710 }
1500 else 1711 else
1501 { 1712 {
1502 /* FIXME getting tired commenting */ 1713 /* FIXME getting tired commenting */
1503 #if 0 1714 GET_MVDATA(dmv1_x, dmv1_y);
1504 b_mv1 = get_bits(&v->gb, n); //VLC 1715 if (!mb_is_intra /* b_mv1 tells not intra */)
1505 #endif
1506 if (1 /* b_mv1 isn't intra */)
1507 { 1716 {
1508 /* FIXME: actually read it */ 1717 /* FIXME: actually read it */
1509 b_mv_type = 0; //vlc 1718 b_mv_type = decode012(&v->gb);
1719 if (v->bfraction > 420 /*1/2*/ &&
1720 b_mv_type < 3) b_mv_type = 1-b_mv_type;
1510 } 1721 }
1511 } 1722 }
1512 } 1723 }
1513 if (!skip_mb_bit) 1724 if (!v->skip_mb_plane.data[current_mb])
1514 { 1725 {
1515 if (b_mv1 != last_mb) 1726 if (mb_has_coeffs /* BMV1 == "last" */)
1516 { 1727 {
1517 GET_MQUANT(); 1728 GET_MQUANT();
1518 if (1 /* intra mb */) 1729 if (mb_is_intra /* intra mb */)
1519 ac_pred = get_bits(&v->gb, 1); 1730 ac_pred = get_bits(&v->gb, 1);
1520 } 1731 }
1521 else 1732 else
1522 { 1733 {
1523 if (1 /* forward_mb is interpolate */) 1734 /* if bmv1 tells MVs are interpolated */
1735 if (b_mv_type == BMV_TYPE_INTERPOLATED)
1524 { 1736 {
1525 /* FIXME: actually read it */ 1737 GET_MVDATA(dmv2_x, dmv2_y);
1526 b_mv2 = 0; //vlc
1527 } 1738 }
1528 if (1 /* b_mv2 isn't the last */) 1739 /* GET_MVDATA has reset some stuff */
1740 if (mb_has_coeffs /* b_mv2 == "last" */)
1529 { 1741 {
1530 if (1 /* intra_mb */) 1742 if (mb_is_intra /* intra_mb */)
1531 ac_pred = get_bits(&v->gb, 1); 1743 ac_pred = get_bits(&v->gb, 1);
1532 GET_MQUANT(); 1744 GET_MQUANT();
1533 } 1745 }
1534 } 1746 }
1535 } 1747 }
1536 //End1 1748 //End1
1537 /* FIXME getting tired, commenting */
1538 #if 0
1539 if (v->ttmbf) 1749 if (v->ttmbf)
1540 v->ttmb = get_bits(&v->gb, n); //vlc 1750 ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table,
1541 #endif 1751 VC9_TTMB_VLC_BITS, 12);
1542 } 1752
1543 //End2 1753 //End2
1544 for (i=0; i<6; i++) 1754 for (i=0; i<6; i++)
1545 { 1755 {
1546 /* FIXME: process the block */ 1756 /* FIXME: process the block */
1547 } 1757 }
1548 1758
1549 current_mb++; 1759 current_mb++;
1760 }
1550 } 1761 }
1551 return 0; 1762 return 0;
1552 } 1763 }
1553 1764
1554 #if HAS_ADVANCED_PROFILE 1765 #if HAS_ADVANCED_PROFILE
1555 static int advanced_decode_i_mbs(VC9Context *v) 1766 static int advanced_decode_i_mbs(VC9Context *v)
1556 { 1767 {
1557 int i, x, y, cbpcy, mqdiff, absmq, mquant, ac_pred, condover, 1768 int x, y, mqdiff, mquant, ac_pred, current_mb = 0, over_flags_mb = 0;
1558 current_mb = 0, over_flags_mb = 0;
1559 1769
1560 for (y=0; y<v->height_mb; y++) 1770 for (y=0; y<v->height_mb; y++)
1561 { 1771 {
1562 for (x=0; x<v->width_mb; x++) 1772 for (x=0; x<v->width_mb; x++)
1563 { 1773 {
1564 if (v->ac_pred_plane[i]) 1774 if (v->ac_pred_plane.data[current_mb])
1565 ac_pred = get_bits(&v->gb, 1); 1775 ac_pred = get_bits(&v->gb, 1);
1566 if (condover == 3 && v->over_flags_plane) 1776 if (v->condover == 3 && v->over_flags_plane.is_raw)
1567 over_flags_mb = get_bits(&v->gb, 1); 1777 over_flags_mb = get_bits(&v->gb, 1);
1568 GET_MQUANT(); 1778 GET_MQUANT();
1779
1780 /* TODO: lots */
1569 } 1781 }
1570 current_mb++; 1782 current_mb++;
1571 } 1783 }
1572 return 0; 1784 return 0;
1573 } 1785 }
1582 avctx->pix_fmt = PIX_FMT_YUV420P; 1794 avctx->pix_fmt = PIX_FMT_YUV420P;
1583 v->avctx = avctx; 1795 v->avctx = avctx;
1584 1796
1585 if (init_common(v) < 0) return -1; 1797 if (init_common(v) < 0) return -1;
1586 1798
1799 avctx->coded_width = avctx->width;
1800 avctx->coded_height = avctx->height;
1587 if (avctx->codec_id == CODEC_ID_WMV3) 1801 if (avctx->codec_id == CODEC_ID_WMV3)
1588 { 1802 {
1589 int count = 0; 1803 int count = 0;
1590 1804
1591 // looks like WMV3 has a sequence header stored in the extradata 1805 // looks like WMV3 has a sequence header stored in the extradata
1613 //FIXME I feel like this is wrong 1827 //FIXME I feel like this is wrong
1614 v->width_mb = (avctx->coded_width+15)>>4; 1828 v->width_mb = (avctx->coded_width+15)>>4;
1615 v->height_mb = (avctx->coded_height+15)>>4; 1829 v->height_mb = (avctx->coded_height+15)>>4;
1616 1830
1617 /* Allocate mb bitplanes */ 1831 /* Allocate mb bitplanes */
1618 v->mv_type_mb_plane = (uint8_t *)av_malloc(v->width_mb*v->height_mb); 1832 if (alloc_bitplane(&v->mv_type_mb_plane, v->width_mb, v->height_mb) < 0)
1619 if (!v->mv_type_mb_plane) return -1; 1833 return -1;
1620 v->skip_mb_plane = (uint8_t *)av_malloc(v->width_mb*v->height_mb); 1834 if (alloc_bitplane(&v->mv_type_mb_plane, v->width_mb, v->height_mb) < 0)
1621 if (!v->skip_mb_plane) return -1; 1835 return -1;
1622 v->direct_mb_plane = (uint8_t *)av_malloc(v->width_mb*v->height_mb); 1836 if (alloc_bitplane(&v->skip_mb_plane, v->width_mb, v->height_mb) < 0)
1623 if (!v->direct_mb_plane) return -1; 1837 return -1;
1838 if (alloc_bitplane(&v->direct_mb_plane, v->width_mb, v->height_mb) < 0)
1839 return -1;
1840
1841 /* For predictors */
1842 v->previous_line_cbpcy = (uint8_t *)av_malloc((v->width_mb+1)*4);
1843 if (!v->previous_line_cbpcy) return -1;
1624 1844
1625 #if HAS_ADVANCED_PROFILE 1845 #if HAS_ADVANCED_PROFILE
1626 if (v->profile > PROFILE_MAIN) 1846 if (v->profile > PROFILE_MAIN)
1627 { 1847 {
1628 v->over_flags_plane = (uint8_t *)av_malloc(v->width_mb*v->height_mb); 1848 if (alloc_bitplane(&v->over_flags_plane, v->width_mb, v->height_mb) < 0)
1629 if (!v->over_flags_plane) return -1; 1849 return -1;
1630 v->ac_pred_plane = (uint8_t *)av_malloc(v->width_mb*v->height_mb); 1850 if (alloc_bitplane(&v->ac_pred_plane, v->width_mb, v->height_mb) < 0)
1631 if (!v->ac_pred_plane) return -1; 1851 return -1;
1632 } 1852 }
1633 #endif 1853 #endif
1634 1854
1635 return 0; 1855 return 0;
1636 } 1856 }
1654 avpicture_fill((AVPicture *)pict, tmp_buf, avctx->pix_fmt, 1874 avpicture_fill((AVPicture *)pict, tmp_buf, avctx->pix_fmt,
1655 avctx->width, avctx->height); 1875 avctx->width, avctx->height);
1656 1876
1657 if (avctx->codec_id == CODEC_ID_WMV3) 1877 if (avctx->codec_id == CODEC_ID_WMV3)
1658 { 1878 {
1879 //No IDU
1659 init_get_bits(&v->gb, buf, buf_size*8); 1880 init_get_bits(&v->gb, buf, buf_size*8);
1660 av_log(avctx, AV_LOG_INFO, "Frame: %i bits to decode\n", buf_size*8);
1661 1881
1662 #if HAS_ADVANCED_PROFILE 1882 #if HAS_ADVANCED_PROFILE
1663 if (v->profile > PROFILE_MAIN) 1883 if (v->profile > PROFILE_MAIN)
1664 { 1884 {
1665 if (advanced_decode_picture_header(v) == FRAME_SKIPED) return buf_size; 1885 if (advanced_decode_picture_header(v) == FRAME_SKIPED) return buf_size;
1685 case BI_TYPE: ret = decode_b_mbs(v); break; 1905 case BI_TYPE: ret = decode_b_mbs(v); break;
1686 default: ret = FRAME_SKIPED; 1906 default: ret = FRAME_SKIPED;
1687 } 1907 }
1688 if (ret == FRAME_SKIPED) return buf_size; 1908 if (ret == FRAME_SKIPED) return buf_size;
1689 } 1909 }
1690
1691 /* Size of the output data = image */
1692 av_log(avctx, AV_LOG_DEBUG, "Consumed %i/%i bits\n",
1693 get_bits_count(&v->gb), buf_size*8);
1694 } 1910 }
1695 else 1911 else
1696 { 1912 {
1697 #if 0 1913 #if 0
1698 // search for IDU's 1914 // search for IDU's
1712 1928
1713 init_get_bits(&v->gb, buf+i, (buf_size-i)*8); 1929 init_get_bits(&v->gb, buf+i, (buf_size-i)*8);
1714 1930
1715 switch(scs) 1931 switch(scs)
1716 { 1932 {
1717 case 0xf: 1933 case 0x0A: //Sequence End Code
1718 decode_sequence_header(avctx, &v->gb); 1934 return 0;
1719 break; 1935 case 0x0B: //Slice Start Code
1720 // to be finished 1936 av_log(avctx, AV_LOG_ERROR, "Slice coding not supported\n");
1721 } 1937 return -1;
1938 case 0x0C: //Field start code
1939 av_log(avctx, AV_LOG_ERROR, "Interlaced coding not supported\n");
1940 return -1;
1941 case 0x0D: //Frame start code
1942 break;
1943 case 0x0E: //Entry point Start Code
1944 if (v->profile <= MAIN_PROFILE)
1945 av_log(avctx, AV_LOG_ERROR,
1946 "Found an entry point in profile %i\n", v->profile);
1947 advanced_entry_point_process(avctx, &v->gb);
1948 break;
1949 case 0x0F: //Sequence header Start Code
1950 decode_sequence_header(avctx, &v->gb);
1951 break;
1952 default:
1953 av_log(avctx, AV_LOG_ERROR,
1954 "Unsupported IDU suffix %lX\n", scs);
1955 }
1722 1956
1723 i += get_bits_count(&v->gb)*8; 1957 i += get_bits_count(&v->gb)*8;
1724 } 1958 }
1725 #else 1959 #else
1726 av_abort(); 1960 av_abort();
1727 #endif 1961 #endif
1728 } 1962 }
1729 1963 av_log(avctx, AV_LOG_DEBUG, "Consumed %i/%i bits\n",
1964 get_bits_count(&v->gb), buf_size*8);
1965
1966 /* Fake consumption of all data */
1730 *data_size = len; 1967 *data_size = len;
1731
1732 /* Fake consumption of all data */
1733 return buf_size; //Number of bytes consumed 1968 return buf_size; //Number of bytes consumed
1734 } 1969 }
1735 1970
1736 static int vc9_decode_end(AVCodecContext *avctx) 1971 static int vc9_decode_end(AVCodecContext *avctx)
1737 { 1972 {