comparison vc9.c @ 2497:69adfbbdcdeb libavcodec

- samples from mplayer ftp in the "adv" profile seem to have profile=2, which isn't the advanced one; and indeed, using adv. profile parser fails. Using normal parser works, and that's what is done - attempt at taking care of stride for NORM2 bitplane decoding - duplication of much code from msmpeg4.c; this code isn't yet used, but goes down as far as the block layer (mainly Transform Type stuff, the remains are wild editing without checking). Unusable yet, and lacks the AC decoding (but a step further in bitstream parsing) patch by anonymous
author michael
date Fri, 04 Feb 2005 02:20:38 +0000
parents 4eb9d61cad4e
children 2c794e3903a2
comparison
equal deleted inserted replaced
2496:74d7fd7b49c5 2497:69adfbbdcdeb
22 22
23 /** 23 /**
24 * @file vc9.c 24 * @file vc9.c
25 * VC-9 and WMV3 decoder 25 * VC-9 and WMV3 decoder
26 * 26 *
27 * TODO: Norm-6 bitplane imode, most AP stuff, optimize, all of MB layer :) 27 * TODO: most AP stuff, optimize, most of MB layer, transform, filtering and motion compensation, etc
28 * TODO: use MPV_ !! 28 * TODO: use MPV_ !!
29 */ 29 */
30 #include "common.h" 30 #include "common.h"
31 #include "dsputil.h" 31 #include "dsputil.h"
32 #include "avcodec.h" 32 #include "avcodec.h"
66 66
67 /** Available Profiles */ 67 /** Available Profiles */
68 //@{ 68 //@{
69 #define PROFILE_SIMPLE 0 69 #define PROFILE_SIMPLE 0
70 #define PROFILE_MAIN 1 70 #define PROFILE_MAIN 1
71 #define PROFILE_COMPLEX 2 ///< TODO: WMV9 specific
71 #define PROFILE_ADVANCED 3 72 #define PROFILE_ADVANCED 3
72 //@} 73 //@}
73 74
74 /** Sequence quantizer mode */ 75 /** Sequence quantizer mode */
75 //@{ 76 //@{
156 static VLC vc9_bfraction_vlc; 157 static VLC vc9_bfraction_vlc;
157 #define VC9_IMODE_VLC_BITS 4 158 #define VC9_IMODE_VLC_BITS 4
158 static VLC vc9_imode_vlc; 159 static VLC vc9_imode_vlc;
159 #define VC9_NORM2_VLC_BITS 3 160 #define VC9_NORM2_VLC_BITS 3
160 static VLC vc9_norm2_vlc; 161 static VLC vc9_norm2_vlc;
161 #if VLC_NORM6_METH0D == 1
162 #define VC9_NORM6_VLC_BITS 9 162 #define VC9_NORM6_VLC_BITS 9
163 static VLC vc9_norm6_vlc; 163 static VLC vc9_norm6_vlc;
164 #endif
165 #if VLC_NORM6_METH0D == 2
166 #define VC9_NORM6_FIRST_BITS 8
167 #define VC9_NORM6_SECOND_BITS 8
168 static VLC vc9_norm6_first_vlc, vc9_norm6_second_vlc;
169 #endif
170 /* Could be optimized, one table only needs 8 bits */ 164 /* Could be optimized, one table only needs 8 bits */
171 #define VC9_TTMB_VLC_BITS 9 //12 165 #define VC9_TTMB_VLC_BITS 9 //12
172 static VLC vc9_ttmb_vlc[3]; 166 static VLC vc9_ttmb_vlc[3];
173 #define VC9_MV_DIFF_VLC_BITS 9 //15 167 #define VC9_MV_DIFF_VLC_BITS 9 //15
174 static VLC vc9_mv_diff_vlc[4]; 168 static VLC vc9_mv_diff_vlc[4];
175 #define VC9_CBPCY_P_VLC_BITS 9 //14 169 #define VC9_CBPCY_P_VLC_BITS 9 //14
176 static VLC vc9_cbpcy_p_vlc[4]; 170 static VLC vc9_cbpcy_p_vlc[4];
177 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6 171 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6
178 static VLC vc9_4mv_block_pattern_vlc[4]; 172 static VLC vc9_4mv_block_pattern_vlc[4];
173 #define VC9_TTBLK_VLC_BITS 5
174 static VLC vc9_ttblk_vlc[3];
175 #define VC9_SUBBLKPAT_VLC_BITS 6
176 static VLC vc9_subblkpat_vlc[3];
179 //@} 177 //@}
180 178
181 /** Bitplane struct 179 /** Bitplane struct
182 * We mainly need data and is_raw, so this struct could be avoided 180 * We mainly need data and is_raw, so this struct could be avoided
183 * to save a level of indirection; feel free to modify 181 * to save a level of indirection; feel free to modify
247 245
248 /** Frame decoding info for all profiles */ 246 /** Frame decoding info for all profiles */
249 //@{ 247 //@{
250 uint8_t mv_mode; ///< MV coding monde 248 uint8_t mv_mode; ///< MV coding monde
251 uint8_t mv_mode2; ///< Secondary MV coding mode (B frames) 249 uint8_t mv_mode2; ///< Secondary MV coding mode (B frames)
250 int k_x; ///< Number of bits for MVs (depends on MV range)
251 int k_y; ///< Number of bits for MVs (depends on MV range)
252 uint8_t pq, altpq; ///< Current/alternate frame quantizer scale 252 uint8_t pq, altpq; ///< Current/alternate frame quantizer scale
253 /** pquant parameters */ 253 /** pquant parameters */
254 //@{ 254 //@{
255 uint8_t dquantfrm; 255 uint8_t dquantfrm;
256 uint8_t dqprofile; 256 uint8_t dqprofile;
257 uint8_t dqsbedge; 257 uint8_t dqsbedge;
258 uint8_t dqbilevel; 258 uint8_t dqbilevel;
259 //@} 259 //@}
260 int ac_table_level; ///< Index for AC tables from ACFRM element 260 int ac_table_level; ///< Index for AC tables from ACFRM element
261 VLC *luma_dc_vlc; ///< Pointer to current luma DC VLC table
262 VLC *chroma_dc_vlc; ///< Pointer to current luma AC VLC table
263 int ttfrm; ///< Transform type info present at frame level 261 int ttfrm; ///< Transform type info present at frame level
264 uint8_t ttmbf; ///< Transform type 262 uint8_t ttmbf; ///< Transform type flag
263 int ttmb; ///< Transform type
264 uint8_t ttblk4x4; ///< Value of ttblk which indicates a 4x4 transform
265 /** Luma compensation parameters */ 265 /** Luma compensation parameters */
266 //@{ 266 //@{
267 uint8_t lumscale; 267 uint8_t lumscale;
268 uint8_t lumshift; 268 uint8_t lumshift;
269 //@} 269 //@}
278 * -# 3 -> [-1024, 1023.f] x [-256, 255.f] 278 * -# 3 -> [-1024, 1023.f] x [-256, 255.f]
279 */ 279 */
280 uint8_t mvrange; 280 uint8_t mvrange;
281 uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use 281 uint8_t pquantizer; ///< Uniform (over sequence) quantizer in use
282 uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY 282 uint8_t *previous_line_cbpcy; ///< To use for predicted CBPCY
283 VLC *cbpcy_vlc; ///< Current CBPCY VLC table 283 VLC *cbpcy_vlc; ///< CBPCY VLC table
284 VLC *ttmb_vlc; ///< Current MB Transform Type VLC table 284 int tt_index; ///< Index for Transform Type tables
285 BitPlane mv_type_mb_plane; ///< bitplane for mv_type == (4MV) 285 BitPlane mv_type_mb_plane; ///< bitplane for mv_type == (4MV)
286 BitPlane skip_mb_plane; ///< bitplane for skipped MBs 286 BitPlane skip_mb_plane; ///< bitplane for skipped MBs
287 BitPlane direct_mb_plane; ///< bitplane for "direct" MBs 287 BitPlane direct_mb_plane; ///< bitplane for "direct" MBs
288 288
289 /** Frame decoding info for S/M profiles only */ 289 /** Frame decoding info for S/M profiles only */
377 v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 }; 377 v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
378 v->hrd_rate = v->hrd_buffer = NULL; 378 v->hrd_rate = v->hrd_buffer = NULL;
379 #endif 379 #endif
380 380
381 /* VLC tables */ 381 /* VLC tables */
382 #if VLC_NORM6_METH0D == 1 382 #if 0 // spec -> actual tables converter
383 # if 0 // spec -> actual tables converter
384 for(i=0; i<64; i++){ 383 for(i=0; i<64; i++){
385 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];
386 av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code); 385 av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
387 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n"); 386 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
388 } 387 }
389 for(i=0; i<64; i++){ 388 for(i=0; i<64; i++){
390 int code= vc9_norm6_spec[i][2] + vc9_norm6_spec[i][4]; 389 int code= vc9_norm6_spec[i][2] + vc9_norm6_spec[i][4];
391 av_log(NULL, AV_LOG_DEBUG, "%2d, ", code); 390 av_log(NULL, AV_LOG_DEBUG, "%2d, ", code);
392 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n"); 391 if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
393 } 392 }
394 # endif
395 #endif 393 #endif
396 if(!done) 394 if(!done)
397 { 395 {
398 done = 1; 396 done = 1;
399 INIT_VLC(&vc9_bfraction_vlc, VC9_BFRACTION_VLC_BITS, 23, 397 INIT_VLC(&vc9_bfraction_vlc, VC9_BFRACTION_VLC_BITS, 23,
400 vc9_bfraction_bits, 1, 1, 398 vc9_bfraction_bits, 1, 1,
401 vc9_bfraction_codes, 1, 1, 1); 399 vc9_bfraction_codes, 1, 1, 1);
402 INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4, 400 INIT_VLC(&vc9_norm2_vlc, VC9_NORM2_VLC_BITS, 4,
403 vc9_norm2_bits, 1, 1, 401 vc9_norm2_bits, 1, 1,
404 vc9_norm2_codes, 1, 1, 1); 402 vc9_norm2_codes, 1, 1, 1);
405 #if VLC_NORM6_METH0D == 1
406 INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64, 403 INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64,
407 vc9_norm6_bits, 1, 1, 404 vc9_norm6_bits, 1, 1,
408 vc9_norm6_codes, 2, 2, 1); 405 vc9_norm6_codes, 2, 2, 1);
409 #endif
410 #if VLC_NORM6_METH0D == 2
411 INIT_VLC(&vc9_norm6_first_vlc, VC9_NORM6_FIRST_BITS, 24,
412 &vc9_norm6_first[0][1], 1, 1,
413 &vc9_norm6_first[0][0], 1, 1, 1);
414 INIT_VLC(&vc9_norm6_second_vlc, VC9_NORM6_SECOND_BITS, 22,
415 &vc9_norm6_second[0][1], 1, 1,
416 &vc9_norm6_second[0][0], 1, 1, 1);
417 #endif
418 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7, 406 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7,
419 vc9_imode_bits, 1, 1, 407 vc9_imode_bits, 1, 1,
420 vc9_imode_codes, 1, 1, 1); 408 vc9_imode_codes, 1, 1, 1);
421 for (i=0; i<3; i++) 409 for (i=0; i<3; i++)
422 { 410 {
423 INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16, 411 INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16,
424 vc9_ttmb_bits[i], 1, 1, 412 vc9_ttmb_bits[i], 1, 1,
425 vc9_ttmb_codes[i], 2, 2, 1); 413 vc9_ttmb_codes[i], 2, 2, 1);
414 INIT_VLC(&vc9_ttblk_vlc[i], VC9_TTBLK_VLC_BITS, 8,
415 vc9_ttblk_bits[i], 1, 1,
416 vc9_ttblk_codes[i], 1, 1, 1);
417 INIT_VLC(&vc9_subblkpat_vlc[i], VC9_SUBBLKPAT_VLC_BITS, 15,
418 vc9_subblkpat_bits[i], 1, 1,
419 vc9_subblkpat_codes[i], 1, 1, 1);
426 } 420 }
427 for(i=0; i<4; i++) 421 for(i=0; i<4; i++)
428 { 422 {
429 INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16, 423 INIT_VLC(&vc9_4mv_block_pattern_vlc[i], VC9_4MV_BLOCK_PATTERN_VLC_BITS, 16,
430 vc9_4mv_block_pattern_bits[i], 1, 1, 424 vc9_4mv_block_pattern_bits[i], 1, 1,
664 */ 658 */
665 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb) 659 static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
666 { 660 {
667 VC9Context *v = avctx->priv_data; 661 VC9Context *v = avctx->priv_data;
668 662
663 av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
669 v->profile = get_bits(gb, 2); 664 v->profile = get_bits(gb, 2);
670 av_log(avctx, AV_LOG_DEBUG, "Profile: %i\n", v->profile); 665 if (v->profile == 2)
666 av_log(avctx, AV_LOG_ERROR, "Profile 2 is reserved\n");
671 667
672 #if HAS_ADVANCED_PROFILE 668 #if HAS_ADVANCED_PROFILE
673 if (v->profile > PROFILE_MAIN) 669 if (v->profile == PROFILE_ADVANCED)
674 { 670 {
675 v->level = get_bits(gb, 3); 671 v->level = get_bits(gb, 3);
676 v->chromaformat = get_bits(gb, 2); 672 v->chromaformat = get_bits(gb, 2);
677 if (v->chromaformat != 1) 673 if (v->chromaformat != 1)
678 { 674 {
687 v->res_sm = get_bits(gb, 2); //reserved 683 v->res_sm = get_bits(gb, 2); //reserved
688 if (v->res_sm) 684 if (v->res_sm)
689 { 685 {
690 av_log(avctx, AV_LOG_ERROR, 686 av_log(avctx, AV_LOG_ERROR,
691 "Reserved RES_SM=%i is forbidden\n", v->res_sm); 687 "Reserved RES_SM=%i is forbidden\n", v->res_sm);
692 //return -1; 688 return -1;
693 } 689 }
694 } 690 }
695 691
696 // (fps-2)/4 (->30) 692 // (fps-2)/4 (->30)
697 v->frmrtq_postproc = get_bits(gb, 3); //common 693 v->frmrtq_postproc = get_bits(gb, 3); //common
698 // (bitrate-32kbps)/64kbps 694 // (bitrate-32kbps)/64kbps
699 v->bitrtq_postproc = get_bits(gb, 5); //common 695 v->bitrtq_postproc = get_bits(gb, 5); //common
700 v->s.loop_filter = get_bits(gb, 1); //common 696 v->s.loop_filter = get_bits(gb, 1); //common
701 697
702 #if HAS_ADVANCED_PROFILE 698 #if HAS_ADVANCED_PROFILE
703 if (v->profile <= PROFILE_MAIN) 699 if (v->profile < PROFILE_ADVANCED)
704 #endif 700 #endif
705 { 701 {
706 v->res_x8 = get_bits(gb, 1); //reserved 702 v->res_x8 = get_bits(gb, 1); //reserved
707 if (v->res_x8) 703 if (v->res_x8)
708 { 704 {
736 } 732 }
737 v->dquant = get_bits(gb, 2); //common 733 v->dquant = get_bits(gb, 2); //common
738 v->vstransform = get_bits(gb, 1); //common 734 v->vstransform = get_bits(gb, 1); //common
739 735
740 #if HAS_ADVANCED_PROFILE 736 #if HAS_ADVANCED_PROFILE
741 if (v->profile <= PROFILE_MAIN) 737 if (v->profile < PROFILE_ADVANCED)
742 #endif 738 #endif
743 { 739 {
744 v->res_transtab = get_bits(gb, 1); 740 v->res_transtab = get_bits(gb, 1);
745 if (v->res_transtab) 741 if (v->res_transtab)
746 { 742 {
751 } 747 }
752 748
753 v->overlap = get_bits(gb, 1); //common 749 v->overlap = get_bits(gb, 1); //common
754 750
755 #if HAS_ADVANCED_PROFILE 751 #if HAS_ADVANCED_PROFILE
756 if (v->profile <= PROFILE_MAIN) 752 if (v->profile < PROFILE_ADVANCED)
757 #endif 753 #endif
758 { 754 {
759 v->s.resync_marker = get_bits(gb, 1); 755 v->s.resync_marker = get_bits(gb, 1);
760 v->rangered = get_bits(gb, 1); 756 v->rangered = get_bits(gb, 1);
761 } 757 }
762 758
763 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common 759 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
764 v->quantizer_mode = get_bits(gb, 2); //common 760 v->quantizer_mode = get_bits(gb, 2); //common
765 761
766 #if HAS_ADVANCED_PROFILE 762 #if HAS_ADVANCED_PROFILE
767 if (v->profile <= PROFILE_MAIN) 763 if (v->profile < PROFILE_ADVANCED)
768 #endif 764 #endif
769 { 765 {
770 v->finterpflag = get_bits(gb, 1); //common 766 v->finterpflag = get_bits(gb, 1); //common
771 v->res_rtm_flag = get_bits(gb, 1); //reserved 767 v->res_rtm_flag = get_bits(gb, 1); //reserved
772 if (!v->res_rtm_flag) 768 if (!v->res_rtm_flag)
891 * @param plane Buffer to store decoded bits 887 * @param plane Buffer to store decoded bits
892 * @param[in] width Width of this buffer 888 * @param[in] width Width of this buffer
893 * @param[in] height Height of this buffer 889 * @param[in] height Height of this buffer
894 * @param[in] stride of this buffer 890 * @param[in] stride of this buffer
895 */ 891 */
896 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ 892 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
897 int x, y; 893 int x, y;
898 GetBitContext *gb = &v->s.gb;
899 894
900 for (y=0; y<height; y++){ 895 for (y=0; y<height; y++){
901 if (!get_bits(gb, 1)) //rowskip 896 if (!get_bits(gb, 1)) //rowskip
902 memset(plane, 0, width); 897 memset(plane, 0, width);
903 else 898 else
912 * @param[in] width Width of this buffer 907 * @param[in] width Width of this buffer
913 * @param[in] height Height of this buffer 908 * @param[in] height Height of this buffer
914 * @param[in] stride of this buffer 909 * @param[in] stride of this buffer
915 * @fixme FIXME: Optimize 910 * @fixme FIXME: Optimize
916 */ 911 */
917 static void decode_colskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ 912 static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
918 int x, y; 913 int x, y;
919 GetBitContext *gb = &v->s.gb;
920 914
921 for (x=0; x<width; x++){ 915 for (x=0; x<width; x++){
922 if (!get_bits(gb, 1)) //colskip 916 if (!get_bits(gb, 1)) //colskip
923 for (y=0; y<height; y++) 917 for (y=0; y<height; y++)
924 plane[y*stride] = 0; 918 plane[y*stride] = 0;
938 */ 932 */
939 static int bitplane_decoding(BitPlane *bp, VC9Context *v) 933 static int bitplane_decoding(BitPlane *bp, VC9Context *v)
940 { 934 {
941 GetBitContext *gb = &v->s.gb; 935 GetBitContext *gb = &v->s.gb;
942 936
943 int imode, x, y, code, use_vertical_tile, tile_w, tile_h; 937 int imode, x, y, code, use_vertical_tile, tile_w, tile_h, offset;
944 uint8_t invert, *planep = bp->data; 938 uint8_t invert, *planep = bp->data;
945 939
946 invert = get_bits(gb, 1); 940 invert = get_bits(gb, 1);
947 imode = get_vlc2(gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2); 941 imode = get_vlc2(gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2);
948 942
953 //Data is actually read in the MB layer (same for all tests == "raw") 947 //Data is actually read in the MB layer (same for all tests == "raw")
954 bp->is_raw = 1; //invert ignored 948 bp->is_raw = 1; //invert ignored
955 return invert; 949 return invert;
956 case IMODE_DIFF2: 950 case IMODE_DIFF2:
957 case IMODE_NORM2: 951 case IMODE_NORM2:
958 if ((bp->height*bp->width) & 1) *(++planep) = get_bits(gb, 1); 952 if ((bp->height*bp->width) & 1)
959 for(x=0; x<(bp->height*bp->width)>>1; x++){ 953 {
960 code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2); 954 *(++planep) = get_bits(gb, 1);
961 *(++planep) = code&1; //lsb => left 955 offset = x = 1;
962 *(++planep) = (code>>1)&1; //msb => right 956 }
963 //FIXME width->stride 957 else offset = x = 0;
958
959 for (y=0; y<bp->height; y++)
960 {
961 for(; x<bp->width; x+=2)
962 {
963 code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
964 *(++planep) = code&1; //lsb => left
965 *(++planep) = (code>>1)&1; //msb => right
966 }
967 planep += bp->stride-bp->width;
968 if ((bp->width-offset)&1) //Odd number previously processed
969 {
970 code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2);
971 *planep = code&1;
972 planep += bp->stride-bp->width;
973 *planep = (code>>1)&1; //msb => right
974 offset = x = 1;
975 }
976 else
977 {
978 offset = x = 0;
979 planep += bp->stride-bp->width;
980 }
964 } 981 }
965 break; 982 break;
966 case IMODE_DIFF6: 983 case IMODE_DIFF6:
967 case IMODE_NORM6: 984 case IMODE_NORM6:
968 use_vertical_tile= bp->height%3==0 && bp->width%3!=0; 985 use_vertical_tile= bp->height%3==0 && bp->width%3!=0;
969 tile_w= use_vertical_tile ? 2 : 3; 986 tile_w= use_vertical_tile ? 2 : 3;
970 tile_h= use_vertical_tile ? 3 : 2; 987 tile_h= use_vertical_tile ? 3 : 2;
971 988
972 for(y= bp->height%tile_h; y< bp->height; y+=tile_h){ 989 for(y= bp->height%tile_h; y< bp->height; y+=tile_h){
973 for(x= bp->width%tile_w; x< bp->width; x+=tile_w){ 990 for(x= bp->width%tile_w; x< bp->width; x+=tile_w){
974 #if VLC_NORM6_METH0D == 1
975 code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2); 991 code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2);
976 if(code<0){ 992 if(code<0){
977 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n"); 993 av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
978 return -1; 994 return -1;
979 } 995 }
980 #endif
981 #if VLC_NORM6_METH0D == 2 //Failure
982 code = get_vlc2(gb, vc9_norm6_first_vlc.table, VC9_NORM6_FIRST_BITS, 2);
983 if (code == 22)
984 {
985 code = vc9_norm6_flc_val[get_bits(gb, 5)];
986 }
987 else if (code == 23)
988 {
989 # if TRACE
990 code = get_vlc2(gb, vc9_norm6_second_vlc.table, VC9_NORM6_SECOND_BITS, 2);
991 assert(code>-1 && code<22);
992 code = vc9_norm6_second_val[code];
993 # else
994 code = vc9_norm6_second_val[get_vlc2(gb, vc9_norm6_second_vlc.table, VC9_NORM6_SECOND_BITS, 2)];
995 # endif
996 }
997 #endif //VLC_NORM6_METH0D == 2
998 //FIXME following is a pure guess and probably wrong 996 //FIXME following is a pure guess and probably wrong
999 //FIXME A bitplane (0 | !0), so could the shifts be avoided ? 997 //FIXME A bitplane (0 | !0), so could the shifts be avoided ?
1000 planep[x + 0*bp->stride]= (code>>0)&1; 998 planep[x + 0*bp->stride]= (code>>0)&1;
1001 planep[x + 1 + 0*bp->stride]= (code>>1)&1; 999 planep[x + 1 + 0*bp->stride]= (code>>1)&1;
1002 //FIXME Does branch prediction help here? 1000 //FIXME Does branch prediction help here?
1013 } 1011 }
1014 } 1012 }
1015 } 1013 }
1016 1014
1017 x= bp->width % tile_w; 1015 x= bp->width % tile_w;
1018 decode_colskip(bp->data , x, bp->height , bp->stride, v); 1016 decode_colskip(bp->data , x, bp->height , bp->stride, &v->s.gb);
1019 decode_rowskip(bp->data+x, bp->width - x, bp->height % tile_h, bp->stride, v); 1017 decode_rowskip(bp->data+x, bp->width - x, bp->height % tile_h, bp->stride, &v->s.gb);
1020 1018
1021 break; 1019 break;
1022 case IMODE_ROWSKIP: 1020 case IMODE_ROWSKIP:
1023 decode_rowskip(bp->data, bp->width, bp->height, bp->stride, v); 1021 decode_rowskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
1024 break; 1022 break;
1025 case IMODE_COLSKIP: //Teh ugly 1023 case IMODE_COLSKIP:
1026 decode_colskip(bp->data, bp->width, bp->height, bp->stride, v); 1024 decode_colskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
1027 break; 1025 break;
1028 default: break; 1026 default: break;
1029 } 1027 }
1030 1028
1031 /* Applying diff operator */ 1029 /* Applying diff operator */
1146 } 1144 }
1147 if (pqindex < 9) v->halfpq = get_bits(gb, 1); 1145 if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1148 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) 1146 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1149 v->pquantizer = get_bits(gb, 1); 1147 v->pquantizer = get_bits(gb, 1);
1150 1148
1151 if (v->profile > PROFILE_MAIN) 1149 if (v->profile == PROFILE_ADVANCED)
1152 { 1150 {
1153 if (v->postprocflag) v->postproc = get_bits(gb, 2); 1151 if (v->postprocflag) v->postproc = get_bits(gb, 2);
1154 if (v->extended_mv == 1 && v->s.pict_type != BI_TYPE) 1152 if (v->extended_mv == 1 && v->s.pict_type != BI_TYPE)
1155 v->mvrange = get_prefix(gb, 0, 3); 1153 v->mvrange = get_prefix(gb, 0, 3);
1156 } 1154 }
1261 GetBitContext *gb = &v->s.gb; 1259 GetBitContext *gb = &v->s.gb;
1262 int pqindex; 1260 int pqindex;
1263 1261
1264 /* Prolog common to all frametypes should be done in caller */ 1262 /* Prolog common to all frametypes should be done in caller */
1265 //BF = Buffer Fullness 1263 //BF = Buffer Fullness
1266 if (v->profile <= PROFILE_MAIN && get_bits(gb, 7)) 1264 if (v->profile < PROFILE_ADVANCED && get_bits(gb, 7))
1267 { 1265 {
1268 av_log(v->s.avctx, AV_LOG_DEBUG, "I BufferFullness not 0\n"); 1266 av_log(v->s.avctx, AV_LOG_DEBUG, "I BufferFullness not 0\n");
1269 } 1267 }
1270 1268
1271 /* Quantizer stuff */ 1269 /* Quantizer stuff */
1291 */ 1289 */
1292 static int decode_i_picture_secondary_header(VC9Context *v) 1290 static int decode_i_picture_secondary_header(VC9Context *v)
1293 { 1291 {
1294 int status; 1292 int status;
1295 #if HAS_ADVANCED_PROFILE 1293 #if HAS_ADVANCED_PROFILE
1296 if (v->profile > PROFILE_MAIN) 1294 if (v->profile == PROFILE_ADVANCED)
1297 { 1295 {
1298 v->s.ac_pred = get_bits(&v->s.gb, 1); 1296 v->s.ac_pred = get_bits(&v->s.gb, 1);
1299 if (v->postprocflag) v->postproc = get_bits(&v->s.gb, 1); 1297 if (v->postprocflag) v->postproc = get_bits(&v->s.gb, 1);
1300 /* 7.1.1.34 + 8.5.2 */ 1298 /* 7.1.1.34 + 8.5.2 */
1301 if (v->overlap && v->pq<9) 1299 if (v->overlap && v->pq<9)
1346 v->pquantizer = get_bits(gb, 1); 1344 v->pquantizer = get_bits(gb, 1);
1347 av_log(v->s.avctx, AV_LOG_DEBUG, "P Frame: QP=%i (+%i/2)\n", 1345 av_log(v->s.avctx, AV_LOG_DEBUG, "P Frame: QP=%i (+%i/2)\n",
1348 v->pq, v->halfpq); 1346 v->pq, v->halfpq);
1349 if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3); 1347 if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1350 #if HAS_ADVANCED_PROFILE 1348 #if HAS_ADVANCED_PROFILE
1351 if (v->profile > PROFILE_MAIN) 1349 if (v->profile == PROFILE_ADVANCED)
1352 { 1350 {
1353 if (v->postprocflag) v->postproc = get_bits(gb, 1); 1351 if (v->postprocflag) v->postproc = get_bits(gb, 1);
1354 } 1352 }
1355 else 1353 else
1356 #endif 1354 #endif
1480 * @return Status 1478 * @return Status
1481 */ 1479 */
1482 static int standard_decode_picture_secondary_header(VC9Context *v) 1480 static int standard_decode_picture_secondary_header(VC9Context *v)
1483 { 1481 {
1484 GetBitContext *gb = &v->s.gb; 1482 GetBitContext *gb = &v->s.gb;
1485 int status = 0, index; 1483 int status = 0;
1486 1484
1487 switch (v->s.pict_type) 1485 switch (v->s.pict_type)
1488 { 1486 {
1489 case P_TYPE: status = decode_p_picture_secondary_header(v); break; 1487 case P_TYPE: status = decode_p_picture_secondary_header(v); break;
1490 case B_TYPE: status = decode_b_picture_secondary_header(v); break; 1488 case B_TYPE: status = decode_b_picture_secondary_header(v); break;
1498 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) 1496 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1499 { 1497 {
1500 v->ac2_table_level = decode012(gb); 1498 v->ac2_table_level = decode012(gb);
1501 } 1499 }
1502 /* DC Syntax */ 1500 /* DC Syntax */
1503 index = decode012(gb); 1501 v->s.dc_table_index = decode012(gb);
1504 v->luma_dc_vlc = &ff_msmp4_dc_luma_vlc[index]; 1502
1505 v->chroma_dc_vlc = &ff_msmp4_dc_chroma_vlc[index];
1506
1507 return 0; 1503 return 0;
1508 } 1504 }
1509 /** @} */ //End for group std_frame_hdr 1505 /** @} */ //End for group std_frame_hdr
1510 1506
1511 #if HAS_ADVANCED_PROFILE 1507 #if HAS_ADVANCED_PROFILE
1582 * @return Status 1578 * @return Status
1583 */ 1579 */
1584 static int advanced_decode_picture_secondary_header(VC9Context *v) 1580 static int advanced_decode_picture_secondary_header(VC9Context *v)
1585 { 1581 {
1586 GetBitContext *gb = &v->s.gb; 1582 GetBitContext *gb = &v->s.gb;
1587 int index, status = 0; 1583 int status = 0;
1588 1584
1589 switch(v->s.pict_type) 1585 switch(v->s.pict_type)
1590 { 1586 {
1591 case P_TYPE: status = decode_p_picture_secondary_header(v); break; 1587 case P_TYPE: status = decode_p_picture_secondary_header(v); break;
1592 case B_TYPE: status = decode_b_picture_secondary_header(v); break; 1588 case B_TYPE: status = decode_b_picture_secondary_header(v); break;
1600 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) 1596 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
1601 { 1597 {
1602 v->ac2_table_level = decode012(gb); 1598 v->ac2_table_level = decode012(gb);
1603 } 1599 }
1604 /* DC Syntax */ 1600 /* DC Syntax */
1605 index = decode012(gb); 1601 v->s.dc_table_index = decode012(gb);
1606 v->luma_dc_vlc = &ff_msmp4_dc_luma_vlc[index];
1607 v->chroma_dc_vlc = &ff_msmp4_dc_chroma_vlc[index];
1608 1602
1609 return 0; 1603 return 0;
1610 } 1604 }
1611 #endif 1605 #endif
1612 /** @} */ //End for adv_frame_hdr 1606 /** @} */ //End for adv_frame_hdr
1616 * @defgroup block VC9 Block-level functions 1610 * @defgroup block VC9 Block-level functions
1617 * @see 7.1.4, p91 and 8.1.1.7, p(1)04 1611 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1618 * @todo TODO: Integrate to MpegEncContext facilities 1612 * @todo TODO: Integrate to MpegEncContext facilities
1619 * @{ 1613 * @{
1620 */ 1614 */
1621 /** Decode a luma intra block
1622 * @warning Will be removed, due to necessary integration
1623 * @see coeff scaling for Adv Profile: 8.1.1.15, p(1)13,
1624 * @param v VC9 context
1625 * @param mquant Macroblock quantizer scale
1626 * @return Status
1627 * @todo TODO: Implement Coeff scaling for Advanced Profile
1628 */
1629 int decode_luma_intra_block(VC9Context *v, int mquant)
1630 {
1631 GetBitContext *gb = &v->s.gb;
1632 int dcdiff;
1633
1634 dcdiff = get_vlc2(gb, v->luma_dc_vlc->table,
1635 DC_VLC_BITS, 2);
1636 if (dcdiff)
1637 {
1638 if (dcdiff == 119 /* ESC index value */)
1639 {
1640 /* TODO: Optimize */
1641 if (mquant == 1) dcdiff = get_bits(gb, 10);
1642 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1643 else dcdiff = get_bits(gb, 8);
1644 }
1645 else
1646 {
1647 if (mquant == 1)
1648 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1649 else if (mquant == 2)
1650 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
1651 }
1652 if (get_bits(gb, 1))
1653 dcdiff = -dcdiff;
1654 }
1655 /* FIXME: 8.1.1.15, p(1)13, coeff scaling for Adv Profile */
1656
1657 return 0;
1658 }
1659 /** @} */ //End for group block
1660
1661 /***********************************************************************/
1662 /**
1663 * @defgroup std_mb VC9 Macroblock-level functions in Simple/Main Profiles
1664 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1665 * @todo TODO: Integrate to MpegEncContext facilities
1666 * @{
1667 */
1668 /**
1669 * @def GET_CBPCY(table, bits)
1670 * @brief Get the Coded Block Pattern for luma and chroma
1671 * @param table VLC table to use (get_vlc2 second parameter)
1672 * @param bits Average bitlength (third parameter to get_vlc2)
1673 * @see 8.1.1.5, p(1)02-(1)03
1674 */
1675 #define GET_CBPCY(table, bits) \
1676 predicted_cbpcy = get_vlc2(gb, table, bits, 2); \
1677 cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \
1678 ? previous_cbpcy[1] : p_cbpcy[+2]; \
1679 cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \
1680 cbpcy[1] = (p_cbpcy[2] == p_cbpcy[3]) ? cbpcy[0] : p_cbpcy[3]; \
1681 cbpcy[1] ^= ((predicted_cbpcy>>4)&0x01); \
1682 cbpcy[2] = (previous_cbpcy[1] == cbpcy[0]) \
1683 ? previous_cbpcy[3] : cbpcy[0]; \
1684 cbpcy[2] ^= ((predicted_cbpcy>>3)&0x01); \
1685 cbpcy[3] = (cbpcy[1] == cbpcy[0]) ? cbpcy[2] : cbpcy[1]; \
1686 cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01);
1687
1688 /** Decode all MBs for an I frame in Simple/Main profile
1689 * @see 8.1, p100
1690 * @todo TODO: Process the blocks
1691 * @todo TODO: Use M$ MPEG-4 cbp prediction
1692 */
1693 static int standard_decode_i_mbs(VC9Context *v)
1694 {
1695 GetBitContext *gb = &v->s.gb;
1696 MpegEncContext *s = &v->s;
1697 int current_mb = 0; /* MB/Block Position info */
1698 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
1699 *p_cbpcy /* Pointer to skip some math */;
1700
1701 /* Reset CBPCY predictors */
1702 memset(v->previous_line_cbpcy, 0, s->mb_stride<<2);
1703
1704 /* Select ttmb table depending on pq */
1705 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0];
1706 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1];
1707 else v->ttmb_vlc = &vc9_ttmb_vlc[2];
1708
1709 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
1710 {
1711 /* Init CBPCY for line */
1712 *((uint32_t*)previous_cbpcy) = 0x00000000;
1713 p_cbpcy = v->previous_line_cbpcy+4;
1714
1715 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++, p_cbpcy += 4)
1716 {
1717 /* Get CBPCY */
1718 GET_CBPCY(ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS);
1719
1720 s->ac_pred = get_bits(gb, 1);
1721
1722 /* TODO: Decode blocks from that mb wrt cbpcy */
1723
1724 /* Update for next block */
1725 #if TRACE > 2
1726 av_log(s->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i,"
1727 " cbpcy=%i%i%i%i\n", current_mb,
1728 p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3],
1729 previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3],
1730 cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]);
1731 #endif
1732 *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
1733 *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
1734 current_mb++;
1735 }
1736 }
1737 return 0;
1738 }
1739 1615
1740 /** 1616 /**
1741 * @def GET_MQUANT 1617 * @def GET_MQUANT
1742 * @brief Get macroblock-level quantizer scale 1618 * @brief Get macroblock-level quantizer scale
1743 */ 1619 */
1777 else mb_has_coeffs = 0; \ 1653 else mb_has_coeffs = 0; \
1778 s->mb_intra = 0; \ 1654 s->mb_intra = 0; \
1779 if (!index) { _dmv_x = _dmv_y = 0; } \ 1655 if (!index) { _dmv_x = _dmv_y = 0; } \
1780 else if (index == 35) \ 1656 else if (index == 35) \
1781 { \ 1657 { \
1782 _dmv_x = get_bits(gb, k_x); \ 1658 _dmv_x = get_bits(gb, v->k_x); \
1783 _dmv_y = get_bits(gb, k_y); \ 1659 _dmv_y = get_bits(gb, v->k_y); \
1784 s->mb_intra = 1; \ 1660 s->mb_intra = 1; \
1785 } \ 1661 } \
1786 else \ 1662 else \
1787 { \ 1663 { \
1788 index1 = index%6; \ 1664 index1 = index%6; \
1789 if (hpel_flag && index1 == 5) val = 1; \ 1665 if (s->mspel && index1 == 5) val = 1; \
1790 else val = 0; \ 1666 else val = 0; \
1791 val = get_bits(gb, size_table[index1] - val); \ 1667 val = get_bits(gb, size_table[index1] - val); \
1792 sign = 0 - (val&1); \ 1668 sign = 0 - (val&1); \
1793 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ 1669 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1794 \ 1670 \
1795 index1 = index/6; \ 1671 index1 = index/6; \
1796 if (hpel_flag && index1 == 5) val = 1; \ 1672 if (s->mspel && index1 == 5) val = 1; \
1797 else val = 0; \ 1673 else val = 0; \
1798 val = get_bits(gb, size_table[index1] - val); \ 1674 val = get_bits(gb, size_table[index1] - val); \
1799 sign = 0 - (val&1); \ 1675 sign = 0 - (val&1); \
1800 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ 1676 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1801 } 1677 }
1678
1679 /** Get predicted DC value
1680 * prediction dir: left=0, top=1
1681 */
1682 static inline int vc9_pred_dc(MpegEncContext *s, int n,
1683 uint16_t **dc_val_ptr, int *dir_ptr)
1684 {
1685 int a, b, c, wrap, pred, scale;
1686 int16_t *dc_val;
1687
1688 /* find prediction */
1689 if (n < 4) {
1690 scale = s->y_dc_scale;
1691 } else {
1692 scale = s->c_dc_scale;
1693 }
1694
1695 wrap = s->block_wrap[n];
1696 dc_val= s->dc_val[0] + s->block_index[n];
1697
1698 /* B C
1699 * A X
1700 */
1701 a = dc_val[ - 1];
1702 b = dc_val[ - 1 - wrap];
1703 c = dc_val[ - wrap];
1704
1705 if(s->first_slice_line && (n&2)==0) b=c=1024;
1706
1707 /* XXX: the following solution consumes divisions, but it does not
1708 necessitate to modify mpegvideo.c. The problem comes from the
1709 fact they decided to store the quantized DC (which would lead
1710 to problems if Q could vary !) */
1711 #if (defined(ARCH_X86) || defined(ARCH_X86_64)) && !defined PIC
1712 asm volatile(
1713 "movl %3, %%eax \n\t"
1714 "shrl $1, %%eax \n\t"
1715 "addl %%eax, %2 \n\t"
1716 "addl %%eax, %1 \n\t"
1717 "addl %0, %%eax \n\t"
1718 "mull %4 \n\t"
1719 "movl %%edx, %0 \n\t"
1720 "movl %1, %%eax \n\t"
1721 "mull %4 \n\t"
1722 "movl %%edx, %1 \n\t"
1723 "movl %2, %%eax \n\t"
1724 "mull %4 \n\t"
1725 "movl %%edx, %2 \n\t"
1726 : "+b" (a), "+c" (b), "+D" (c)
1727 : "g" (scale), "S" (inverse[scale])
1728 : "%eax", "%edx"
1729 );
1730 #else
1731 /* #elif defined (ARCH_ALPHA) */
1732 /* Divisions are extremely costly on Alpha; optimize the most
1733 common case. But they are costly everywhere...
1734 */
1735 if (scale == 8) {
1736 a = (a + (8 >> 1)) / 8;
1737 b = (b + (8 >> 1)) / 8;
1738 c = (c + (8 >> 1)) / 8;
1739 } else {
1740 a = FASTDIV((a + (scale >> 1)), scale);
1741 b = FASTDIV((b + (scale >> 1)), scale);
1742 c = FASTDIV((c + (scale >> 1)), scale);
1743 }
1744 #endif
1745
1746 if (abs(a - b) <= abs(b - c)) {
1747 pred = c;
1748 *dir_ptr = 1;
1749 } else {
1750 pred = a;
1751 *dir_ptr = 0;
1752 }
1753
1754 /* update predictor */
1755 *dc_val_ptr = &dc_val[0];
1756 return pred;
1757 }
1758
1759 /** Decode one block, inter or intra
1760 * @param v The VC9 context
1761 * @param block 8x8 DCT block
1762 * @param n Block index in the current MB (<4=>luma)
1763 * @param coded If the block is coded
1764 * @param MB quant, if decoded at the MB layer
1765 * @see Inter TT: Table 21, p73 + p91-85
1766 * @see Intra TT: Table 20, p72 + p(1)05-(1)07
1767 * @todo TODO: Process the blocks
1768 * @todo TODO: Use M$ MPEG-4 cbp prediction
1769 */
1770 int vc9_decode_block(VC9Context *v, DCTELEM block[64], int n, int coded, int mquant)
1771 {
1772 GetBitContext *gb = &v->s.gb;
1773 MpegEncContext *s = &v->s;
1774 int ttblk; /* Transform Type per Block */
1775 int subblkpat; /* Sub-block Transform Type Pattern */
1776 int dc_pred_dir; /* Direction of the DC prediction used */
1777 int run_diff, i;
1778
1779 if (s->mb_intra)
1780 {
1781 int dcdiff;
1782 uint16_t *dc_val;
1783
1784 /* Get DC differential */
1785 if (n < 4) {
1786 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1787 } else {
1788 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1789 }
1790 if (dcdiff < 0){
1791 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
1792 return -1;
1793 }
1794 if (dcdiff)
1795 {
1796 if (dcdiff == 119 /* ESC index value */)
1797 {
1798 /* TODO: Optimize */
1799 if (mquant == 1) dcdiff = get_bits(gb, 10);
1800 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1801 else dcdiff = get_bits(gb, 8);
1802 }
1803 else
1804 {
1805 if (mquant == 1)
1806 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1807 else if (mquant == 2)
1808 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1;
1809 }
1810 if (get_bits(gb, 1))
1811 dcdiff = -dcdiff;
1812 }
1813
1814 /* Prediction */
1815 dcdiff += vc9_pred_dc(s, n, &dc_val, &dc_pred_dir);
1816 if (n < 4) {
1817 *dc_val = dcdiff * s->y_dc_scale;
1818 } else {
1819 *dc_val = dcdiff * s->c_dc_scale;
1820 }
1821 if (dcdiff < 0)
1822 {
1823 av_log(s->avctx, AV_LOG_ERROR, "DC=%i<0\n", dcdiff);
1824 return -1;
1825 }
1826 block[0] = dcdiff; //XXX: Must be > 0
1827
1828 /* Skip ? */
1829 run_diff = 0;
1830 i = 0;
1831 if (!coded) {
1832 goto not_coded;
1833 }
1834 }
1835 else
1836 {
1837 mquant = v->pq;
1838
1839 /* Get TTBLK */
1840 if (v->ttmb < 8) /* per block */
1841 ttblk = get_vlc2(gb, vc9_ttblk_vlc[v->tt_index].table, VC9_TTBLK_VLC_BITS, 2);
1842 else /* Per frame */
1843 ttblk = 0; //FIXME, depends on ttfrm
1844
1845 /* Get SUBBLKPAT */
1846 if (ttblk == v->ttblk4x4) /* 4x4 transform for that qp value */
1847 subblkpat = 1+get_vlc2(gb, vc9_subblkpat_vlc[v->tt_index].table,
1848 VC9_SUBBLKPAT_VLC_BITS, 2);
1849 else /* All others: 8x8, 4x8, 8x4 */
1850 subblkpat = decode012(gb);
1851 }
1852
1853 //TODO AC Decoding
1854
1855
1856 not_coded:
1857 if (s->mb_intra) {
1858 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1859 if (s->ac_pred) {
1860 i = 63; /* XXX: not optimal */
1861 }
1862 }
1863 if(i>0) i=63; //FIXME/XXX optimize
1864 s->block_last_index[n] = i;
1865 return 0;
1866 }
1867
1868 /** @} */ //End for group block
1869
1870 /***********************************************************************/
1871 /**
1872 * @defgroup std_mb VC9 Macroblock-level functions in Simple/Main Profiles
1873 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1874 * @todo TODO: Integrate to MpegEncContext facilities
1875 * @{
1876 */
1877
1878 static inline int vc9_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1879 {
1880 int xy, wrap, pred, a, b, c;
1881
1882 xy = s->block_index[n];
1883 wrap = s->b8_stride;
1884
1885 /* B C
1886 * A X
1887 */
1888 a = s->coded_block[xy - 1 ];
1889 b = s->coded_block[xy - 1 - wrap];
1890 c = s->coded_block[xy - wrap];
1891
1892 if (b == c) {
1893 pred = a;
1894 } else {
1895 pred = c;
1896 }
1897
1898 /* store value */
1899 *coded_block_ptr = &s->coded_block[xy];
1900
1901 return pred;
1902 }
1903
1904 int vc9_decode_i_mb(VC9Context *v, DCTELEM block[6][64])
1905 {
1906 int i, cbp, val;
1907 uint8_t *coded_val;
1908 uint32_t * const mb_type_ptr= &v->s.current_picture.mb_type[ v->s.mb_x + v->s.mb_y*v->s.mb_stride ];
1909
1910 v->s.mb_intra = 1;
1911 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
1912 if (cbp < 0) return -1;
1913 v->s.ac_pred = get_bits(&v->s.gb, 1);
1914
1915 for (i=0; i<6; i++)
1916 {
1917 val = ((cbp >> (5 - i)) & 1);
1918 if (i < 4) {
1919 int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
1920 val = val ^ pred;
1921 *coded_val = val;
1922 }
1923 cbp |= val << (5 - i);
1924 if (vc9_decode_block(v, block[i], i, val, v->pq) < 0) //FIXME Should be mquant
1925 {
1926 av_log(v->s.avctx, AV_LOG_ERROR,
1927 "\nerror while decoding block: %d x %d (%d)\n", v->s.mb_x, v->s.mb_y, i);
1928 return -1;
1929 }
1930 }
1931 return 0;
1932 }
1933
1934 int vc9_decode_p_mb(VC9Context *v, DCTELEM block[6][64])
1935 {
1936 MpegEncContext *s = &v->s;
1937 GetBitContext *gb = &s->gb;
1938 int i, mb_offset = s->mb_x + s->mb_y*s->mb_width; /* XXX: mb_stride */
1939 int cbp; /* cbp decoding stuff */
1940 int hybrid_pred; /* Prediction types */
1941 int mv_mode_bit = 0;
1942 int mqdiff, mquant; /* MB quantization */
1943 int ttmb; /* MB Transform type */
1944 int status;
1945 uint8_t *coded_val;
1946
1947 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
1948 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
1949 int mb_has_coeffs = 1; /* last_flag */
1950 int dmv_x, dmv_y; /* Differential MV components */
1951 int index, index1; /* LUT indices */
1952 int val, sign; /* temp values */
1953
1954 if (v->mv_type_mb_plane.is_raw)
1955 v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
1956 if (v->skip_mb_plane.is_raw)
1957 v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
1958 if (!mv_mode_bit) /* 1MV mode */
1959 {
1960 if (!v->skip_mb_plane.data[mb_offset])
1961 {
1962 GET_MVDATA(dmv_x, dmv_y);
1963
1964 /* hybrid mv pred, 8.3.5.3.4 */
1965 if (v->mv_mode == MV_PMODE_1MV ||
1966 v->mv_mode == MV_PMODE_MIXED_MV)
1967 hybrid_pred = get_bits(gb, 1);
1968 if (s->mb_intra && !mb_has_coeffs)
1969 {
1970 GET_MQUANT();
1971 s->ac_pred = get_bits(gb, 1);
1972 /* XXX: how to handle cbp ? */
1973 }
1974 else if (mb_has_coeffs)
1975 {
1976 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
1977 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
1978 GET_MQUANT();
1979 }
1980 else
1981 {
1982 mquant = v->pq;
1983 /* XXX: How to handle cbp ? */
1984 }
1985
1986 if (!v->ttmbf)
1987 ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
1988 VC9_TTMB_VLC_BITS, 12);
1989 for (i=0; i<6; i++)
1990 {
1991 val = ((cbp >> (5 - i)) & 1);
1992 if (i < 4) {
1993 int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
1994 val = val ^ pred;
1995 *coded_val = val;
1996 }
1997 vc9_decode_block(v, block[i], i, val, mquant); //FIXME
1998 }
1999 }
2000 else //Skipped
2001 {
2002 /* hybrid mv pred, 8.3.5.3.4 */
2003 if (v->mv_mode == MV_PMODE_1MV ||
2004 v->mv_mode == MV_PMODE_MIXED_MV)
2005 hybrid_pred = get_bits(gb, 1);
2006
2007 /* TODO: blah */
2008 return 0;
2009 }
2010 } //1MV mode
2011 else //4MV mode
2012 {
2013 if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
2014 {
2015 /* Get CBPCY */
2016 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
2017 for (i=0; i<6; i++)
2018 {
2019 val = ((cbp >> (5 - i)) & 1);
2020 if (i < 4) {
2021 int pred = vc9_coded_block_pred(&v->s, i, &coded_val);
2022 val = val ^ pred;
2023 *coded_val = val;
2024 }
2025 if (i<4 && val)
2026 {
2027 GET_MVDATA(dmv_x, dmv_y);
2028 }
2029 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
2030 hybrid_pred = get_bits(gb, 1);
2031 GET_MQUANT();
2032 if (s->mb_intra /* One of the 4 blocks is intra */ &&
2033 index /* non-zero pred for that block */)
2034 s->ac_pred = get_bits(gb, 1);
2035 if (!v->ttmbf)
2036 ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
2037 VC9_TTMB_VLC_BITS, 12);
2038 status = vc9_decode_block(v, block[i], i, val, mquant);
2039 }
2040 return status;
2041 }
2042 else //Skipped MB
2043 {
2044 for (i=0; i<4; i++)
2045 {
2046 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */)
2047 hybrid_pred = get_bits(gb, 1);
2048 vc9_decode_block(v, block[i], i, 0 /*cbp[i]*/, mquant); //FIXME
2049 }
2050 /* TODO: blah */
2051 return 0;
2052 }
2053 }
2054
2055 /* Should never happen */
2056 return -1;
2057 }
2058
2059 int vc9_decode_b_mb(VC9Context *v, DCTELEM block[6][64])
2060 {
2061 int i;
2062 //Decode CBP
2063 for (i=0; i<6; i++)
2064 {
2065 vc9_decode_block(v, block[i], i, 0 /*cbp[i]*/, v->pq /*Should be mquant*/); //FIXME
2066 }
2067 return 0;
2068 }
2069
2070 static int standard_decode_mbs(VC9Context *v)
2071 {
2072 GetBitContext *gb = &v->s.gb;
2073 MpegEncContext *s = &v->s;
2074
2075 /* Set transform type info depending on pq */
2076 if (v->pq < 5)
2077 {
2078 v->tt_index = 0;
2079 v->ttblk4x4 = 3;
2080 }
2081 else if (v->pq < 13)
2082 {
2083 v->tt_index = 1;
2084 v->ttblk4x4 = 3;
2085 }
2086 else
2087 {
2088 v->tt_index = 2;
2089 v->ttblk4x4 = 2;
2090 }
2091
2092 if (s->pict_type != I_TYPE)
2093 {
2094 /* Select proper long MV range */
2095 switch (v->mvrange)
2096 {
2097 case 1: v->k_x = 10; v->k_y = 9; break;
2098 case 2: v->k_x = 12; v->k_y = 10; break;
2099 case 3: v->k_x = 13; v->k_y = 11; break;
2100 default: /*case 0 too */ v->k_x = 9; v->k_y = 8; break;
2101 }
2102
2103 s->mspel = v->mv_mode & 1; //MV_PMODE is HPEL
2104 v->k_x -= s->mspel;
2105 v->k_y -= s->mspel;
2106 }
2107
2108 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
2109 {
2110 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
2111 {
2112 //FIXME Get proper MB DCTELEM
2113 //TODO Move out of the loop
2114 switch (s->pict_type)
2115 {
2116 case I_TYPE: vc9_decode_i_mb(v, NULL); break;
2117 case P_TYPE: vc9_decode_i_mb(v, NULL); break;
2118 case BI_TYPE:
2119 case B_TYPE: vc9_decode_i_mb(v, NULL); break;
2120 }
2121 }
2122 //Add a check for overconsumption ?
2123 }
2124 return 0;
2125 }
2126
2127
2128 /**
2129 * @def GET_CBPCY(table, bits)
2130 * @brief Get the Coded Block Pattern for luma and chroma
2131 * @param table VLC table to use (get_vlc2 second parameter)
2132 * @param bits Average bitlength (third parameter to get_vlc2)
2133 * @see 8.1.1.5, p(1)02-(1)03
2134 */
2135 #define GET_CBPCY(table, bits) \
2136 predicted_cbpcy = get_vlc2(gb, table, bits, 2); \
2137 cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \
2138 ? previous_cbpcy[1] : p_cbpcy[+2]; \
2139 cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \
2140 cbpcy[1] = (p_cbpcy[2] == p_cbpcy[3]) ? cbpcy[0] : p_cbpcy[3]; \
2141 cbpcy[1] ^= ((predicted_cbpcy>>4)&0x01); \
2142 cbpcy[2] = (previous_cbpcy[1] == cbpcy[0]) \
2143 ? previous_cbpcy[3] : cbpcy[0]; \
2144 cbpcy[2] ^= ((predicted_cbpcy>>3)&0x01); \
2145 cbpcy[3] = (cbpcy[1] == cbpcy[0]) ? cbpcy[2] : cbpcy[1]; \
2146 cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01);
2147
2148 /** Decode all MBs for an I frame in Simple/Main profile
2149 * @see 8.1, p100
2150 * @todo TODO: Process the blocks
2151 * @todo TODO: Use M$ MPEG-4 cbp prediction
2152 */
2153 static int standard_decode_i_mbs(VC9Context *v)
2154 {
2155 GetBitContext *gb = &v->s.gb;
2156 MpegEncContext *s = &v->s;
2157 int mb_offset = 0; /* MB/Block Position info */
2158 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
2159 *p_cbpcy /* Pointer to skip some math */;
2160
2161 /* Reset CBPCY predictors */
2162 memset(v->previous_line_cbpcy, 0, s->mb_stride<<2);
2163
2164 /* Select ttmb table depending on pq */
2165 if (v->pq < 5)
2166 {
2167 v->tt_index = 0;
2168 v->ttblk4x4 = 3;
2169 }
2170 else if (v->pq < 13)
2171 {
2172 v->tt_index = 1;
2173 v->ttblk4x4 = 3;
2174 }
2175 else
2176 {
2177 v->tt_index = 2;
2178 v->ttblk4x4 = 2;
2179 }
2180
2181 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
2182 {
2183 /* Init CBPCY for line */
2184 *((uint32_t*)previous_cbpcy) = 0x00000000;
2185 p_cbpcy = v->previous_line_cbpcy+4;
2186
2187 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++, p_cbpcy += 4)
2188 {
2189 /* Get CBPCY */
2190 GET_CBPCY(ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS);
2191
2192 s->ac_pred = get_bits(gb, 1);
2193
2194 /* TODO: Decode blocks from that mb wrt cbpcy */
2195
2196 /* Update for next block */
2197 #if TRACE > 2
2198 av_log(s->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i,"
2199 " cbpcy=%i%i%i%i\n", mb_offset,
2200 p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3],
2201 previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3],
2202 cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]);
2203 #endif
2204 *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
2205 *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
2206 mb_offset++;
2207 }
2208 }
2209 return 0;
2210 }
2211
1802 2212
1803 /** Decode all MBs for an P frame in Simple/Main profile 2213 /** Decode all MBs for an P frame in Simple/Main profile
1804 * @see 8.1, p(1)15 2214 * @see 8.1, p(1)15
1805 * @todo TODO: Process the blocks 2215 * @todo TODO: Process the blocks
1806 * @todo TODO: Use M$ MPEG-4 cbp prediction 2216 * @todo TODO: Use M$ MPEG-4 cbp prediction
1807 */ 2217 */
1808 static int decode_p_mbs(VC9Context *v) 2218 static int decode_p_mbs(VC9Context *v)
1809 { 2219 {
1810 MpegEncContext *s = &v->s; 2220 MpegEncContext *s = &v->s;
1811 GetBitContext *gb = &v->s.gb; 2221 GetBitContext *gb = &v->s.gb;
1812 int current_mb = 0, i; /* MB/Block Position info */ 2222 int mb_offset = 0, i; /* MB/Block Position info */
1813 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy, 2223 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy,
1814 *p_cbpcy /* Pointer to skip some math */; 2224 *p_cbpcy /* Pointer to skip some math */;
1815 int hybrid_pred; /* Prediction types */ 2225 int hybrid_pred; /* Prediction types */
1816 int mv_mode_bit = 0; 2226 int mv_mode_bit = 0;
1817 int mqdiff, mquant; /* MB quantization */ 2227 int mqdiff, mquant; /* MB quantization */
1819 2229
1820 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, 2230 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
1821 offset_table[6] = { 0, 1, 3, 7, 15, 31 }; 2231 offset_table[6] = { 0, 1, 3, 7, 15, 31 };
1822 int mb_has_coeffs = 1; /* last_flag */ 2232 int mb_has_coeffs = 1; /* last_flag */
1823 int dmv_x, dmv_y; /* Differential MV components */ 2233 int dmv_x, dmv_y; /* Differential MV components */
1824 int k_x, k_y; /* Long MV fixed bitlength */
1825 int hpel_flag; /* Some MB properties */
1826 int index, index1; /* LUT indices */ 2234 int index, index1; /* LUT indices */
1827 int val, sign; /* MVDATA temp values */ 2235 int val, sign; /* MVDATA temp values */
1828 2236
1829 /* Select ttmb table depending on pq */ 2237 /* Select ttmb table depending on pq */
1830 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0]; 2238 if (v->pq < 5)
1831 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1]; 2239 {
1832 else v->ttmb_vlc = &vc9_ttmb_vlc[2]; 2240 v->tt_index = 0;
2241 v->ttblk4x4 = 3;
2242 }
2243 else if (v->pq < 13)
2244 {
2245 v->tt_index = 1;
2246 v->ttblk4x4 = 3;
2247 }
2248 else
2249 {
2250 v->tt_index = 2;
2251 v->ttblk4x4 = 2;
2252 }
1833 2253
1834 /* Select proper long MV range */ 2254 /* Select proper long MV range */
1835 switch (v->mvrange) 2255 switch (v->mvrange)
1836 { 2256 {
1837 case 1: k_x = 10; k_y = 9; break; 2257 case 1: v->k_x = 10; v->k_y = 9; break;
1838 case 2: k_x = 12; k_y = 10; break; 2258 case 2: v->k_x = 12; v->k_y = 10; break;
1839 case 3: k_x = 13; k_y = 11; break; 2259 case 3: v->k_x = 13; v->k_y = 11; break;
1840 default: /*case 0 too */ k_x = 9; k_y = 8; break; 2260 default: /*case 0 too */ v->k_x = 9; v->k_y = 8; break;
1841 } 2261 }
1842 2262
1843 hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL 2263 s->mspel = v->mv_mode & 1; //MV_PMODE is HPEL
1844 k_x -= hpel_flag; 2264 v->k_x -= s->mspel;
1845 k_y -= hpel_flag; 2265 v->k_y -= s->mspel;
1846 2266
1847 /* Reset CBPCY predictors */ 2267 /* Reset CBPCY predictors */
1848 memset(v->previous_line_cbpcy, 0, s->mb_stride<<2); 2268 memset(v->previous_line_cbpcy, 0, s->mb_stride<<2);
1849 2269
1850 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++) 2270 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
1854 p_cbpcy = v->previous_line_cbpcy+4; 2274 p_cbpcy = v->previous_line_cbpcy+4;
1855 2275
1856 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++, p_cbpcy += 4) 2276 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++, p_cbpcy += 4)
1857 { 2277 {
1858 if (v->mv_type_mb_plane.is_raw) 2278 if (v->mv_type_mb_plane.is_raw)
1859 v->mv_type_mb_plane.data[current_mb] = get_bits(gb, 1); 2279 v->mv_type_mb_plane.data[mb_offset] = get_bits(gb, 1);
1860 if (v->skip_mb_plane.is_raw) 2280 if (v->skip_mb_plane.is_raw)
1861 v->skip_mb_plane.data[current_mb] = get_bits(gb, 1); 2281 v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
1862 if (!mv_mode_bit) /* 1MV mode */ 2282 if (!mv_mode_bit) /* 1MV mode */
1863 { 2283 {
1864 if (!v->skip_mb_plane.data[current_mb]) 2284 if (!v->skip_mb_plane.data[mb_offset])
1865 { 2285 {
1866 GET_MVDATA(dmv_x, dmv_y); 2286 GET_MVDATA(dmv_x, dmv_y);
1867 2287
1868 /* hybrid mv pred, 8.3.5.3.4 */ 2288 /* hybrid mv pred, 8.3.5.3.4 */
1869 if (v->mv_mode == MV_PMODE_1MV || 2289 if (v->mv_mode == MV_PMODE_1MV ||
1876 } 2296 }
1877 else if (mb_has_coeffs) 2297 else if (mb_has_coeffs)
1878 { 2298 {
1879 if (s->mb_intra) s->ac_pred = get_bits(gb, 1); 2299 if (s->mb_intra) s->ac_pred = get_bits(gb, 1);
1880 predicted_cbpcy = get_vlc2(gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2); 2300 predicted_cbpcy = get_vlc2(gb, v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS, 2);
1881 cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) ? previous_cbpcy[1] : p_cbpcy[2]; 2301 GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
1882 cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01);
1883 cbpcy[1] = (p_cbpcy[2] == p_cbpcy[3]) ? cbpcy[0] : p_cbpcy[3];
1884 cbpcy[1] ^= ((predicted_cbpcy>>4)&0x01);
1885 cbpcy[2] = (previous_cbpcy[1] == cbpcy[0]) ? previous_cbpcy[3] : cbpcy[0];
1886 cbpcy[2] ^= ((predicted_cbpcy>>3)&0x01);
1887 cbpcy[3] = (cbpcy[1] == cbpcy[0]) ? cbpcy[2] : cbpcy[1];
1888 cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01);
1889 //GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
1890 2302
1891 GET_MQUANT(); 2303 GET_MQUANT();
1892 } 2304 }
1893 if (!v->ttmbf) 2305 if (!v->ttmbf)
1894 ttmb = get_vlc2(gb, v->ttmb_vlc->table, 2306 ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
1895 VC9_TTMB_VLC_BITS, 12); 2307 VC9_TTMB_VLC_BITS, 12);
1896 /* TODO: decode blocks from that mb wrt cbpcy */ 2308 /* TODO: decode blocks from that mb wrt cbpcy */
1897 } 2309 }
1898 else //Skipped 2310 else //Skipped
1899 { 2311 {
1903 hybrid_pred = get_bits(gb, 1); 2315 hybrid_pred = get_bits(gb, 1);
1904 } 2316 }
1905 } //1MV mode 2317 } //1MV mode
1906 else //4MV mode 2318 else //4MV mode
1907 { 2319 {
1908 if (!v->skip_mb_plane.data[current_mb] /* unskipped MB */) 2320 if (!v->skip_mb_plane.data[mb_offset] /* unskipped MB */)
1909 { 2321 {
1910 /* Get CBPCY */ 2322 /* Get CBPCY */
1911 GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS); 2323 GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS);
1912 for (i=0; i<4; i++) //For all 4 Y blocks 2324 for (i=0; i<4; i++) //For all 4 Y blocks
1913 { 2325 {
1920 GET_MQUANT(); 2332 GET_MQUANT();
1921 if (s->mb_intra /* One of the 4 blocks is intra */ && 2333 if (s->mb_intra /* One of the 4 blocks is intra */ &&
1922 index /* non-zero pred for that block */) 2334 index /* non-zero pred for that block */)
1923 s->ac_pred = get_bits(gb, 1); 2335 s->ac_pred = get_bits(gb, 1);
1924 if (!v->ttmbf) 2336 if (!v->ttmbf)
1925 ttmb = get_vlc2(gb, v->ttmb_vlc->table, 2337 ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
1926 VC9_TTMB_VLC_BITS, 12); 2338 VC9_TTMB_VLC_BITS, 12);
1927 2339
1928 /* TODO: Process blocks wrt cbpcy */ 2340 /* TODO: Process blocks wrt cbpcy */
1929 2341
1930 } 2342 }
1942 } 2354 }
1943 2355
1944 /* Update for next block */ 2356 /* Update for next block */
1945 #if TRACE > 2 2357 #if TRACE > 2
1946 av_log(s->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i," 2358 av_log(s->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i,"
1947 " cbpcy=%i%i%i%i\n", current_mb, 2359 " cbpcy=%i%i%i%i\n", mb_offset,
1948 p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3], 2360 p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3],
1949 previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3], 2361 previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3],
1950 cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]); 2362 cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]);
1951 #endif 2363 #endif
1952 *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy); 2364 *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy);
1953 *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy); 2365 *((uint32_t*)previous_cbpcy) = *((uint32_t*)cbpcy);
1954 current_mb++; 2366 mb_offset++;
1955 } 2367 }
1956 } 2368 }
1957 return 0; 2369 return 0;
1958 } 2370 }
1959 2371
1963 */ 2375 */
1964 static int decode_b_mbs(VC9Context *v) 2376 static int decode_b_mbs(VC9Context *v)
1965 { 2377 {
1966 MpegEncContext *s = &v->s; 2378 MpegEncContext *s = &v->s;
1967 GetBitContext *gb = &v->s.gb; 2379 GetBitContext *gb = &v->s.gb;
1968 int current_mb = 0, i /* MB / B postion information */; 2380 int mb_offset = 0, i /* MB / B postion information */;
1969 int b_mv_type = BMV_TYPE_BACKWARD; 2381 int b_mv_type = BMV_TYPE_BACKWARD;
1970 int mquant, mqdiff; /* MB quant stuff */ 2382 int mquant, mqdiff; /* MB quant stuff */
1971 int ttmb; /* MacroBlock transform type */ 2383 int ttmb; /* MacroBlock transform type */
1972 2384
1973 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, 2385 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },
1990 hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL 2402 hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL
1991 k_x -= hpel_flag; 2403 k_x -= hpel_flag;
1992 k_y -= hpel_flag; 2404 k_y -= hpel_flag;
1993 2405
1994 /* Select ttmb table depending on pq */ 2406 /* Select ttmb table depending on pq */
1995 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0]; 2407 if (v->pq < 5)
1996 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1]; 2408 {
1997 else v->ttmb_vlc = &vc9_ttmb_vlc[2]; 2409 v->tt_index = 0;
2410 v->ttblk4x4 = 3;
2411 }
2412 else if (v->pq < 13)
2413 {
2414 v->tt_index = 1;
2415 v->ttblk4x4 = 3;
2416 }
2417 else
2418 {
2419 v->tt_index = 2;
2420 v->ttblk4x4 = 2;
2421 }
1998 2422
1999 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++) 2423 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
2000 { 2424 {
2001 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++) 2425 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
2002 { 2426 {
2003 if (v->direct_mb_plane.is_raw) 2427 if (v->direct_mb_plane.is_raw)
2004 v->direct_mb_plane.data[current_mb] = get_bits(gb, 1); 2428 v->direct_mb_plane.data[mb_offset] = get_bits(gb, 1);
2005 if (v->skip_mb_plane.is_raw) 2429 if (v->skip_mb_plane.is_raw)
2006 v->skip_mb_plane.data[current_mb] = get_bits(gb, 1); 2430 v->skip_mb_plane.data[mb_offset] = get_bits(gb, 1);
2007 2431
2008 if (!v->direct_mb_plane.data[current_mb]) 2432 if (!v->direct_mb_plane.data[mb_offset])
2009 { 2433 {
2010 if (v->skip_mb_plane.data[current_mb]) 2434 if (v->skip_mb_plane.data[mb_offset])
2011 { 2435 {
2012 b_mv_type = decode012(gb); 2436 b_mv_type = decode012(gb);
2013 if (v->bfraction > 420 /*1/2*/ && 2437 if (v->bfraction > 420 /*1/2*/ &&
2014 b_mv_type < 3) b_mv_type = 1-b_mv_type; 2438 b_mv_type < 3) b_mv_type = 1-b_mv_type;
2015 } 2439 }
2022 if (v->bfraction > 420 /*1/2*/ && 2446 if (v->bfraction > 420 /*1/2*/ &&
2023 b_mv_type < 3) b_mv_type = 1-b_mv_type; 2447 b_mv_type < 3) b_mv_type = 1-b_mv_type;
2024 } 2448 }
2025 } 2449 }
2026 } 2450 }
2027 if (!v->skip_mb_plane.data[current_mb]) 2451 if (!v->skip_mb_plane.data[mb_offset])
2028 { 2452 {
2029 if (mb_has_coeffs /* BMV1 == "last" */) 2453 if (mb_has_coeffs /* BMV1 == "last" */)
2030 { 2454 {
2031 GET_MQUANT(); 2455 GET_MQUANT();
2032 if (s->mb_intra /* intra mb */) 2456 if (s->mb_intra /* intra mb */)
2048 } 2472 }
2049 } 2473 }
2050 } 2474 }
2051 //End1 2475 //End1
2052 if (v->ttmbf) 2476 if (v->ttmbf)
2053 ttmb = get_vlc2(gb, v->ttmb_vlc->table, 2477 ttmb = get_vlc2(gb, vc9_ttmb_vlc[v->tt_index].table,
2054 VC9_TTMB_VLC_BITS, 12); 2478 VC9_TTMB_VLC_BITS, 12);
2055 2479
2056 //End2 2480 //End2
2057 for (i=0; i<6; i++) 2481 for (i=0; i<6; i++)
2058 { 2482 {
2059 /* FIXME: process the block */ 2483 /* FIXME: process the block */
2060 } 2484 }
2061 2485
2062 current_mb++; 2486 mb_offset++;
2063 } 2487 }
2064 } 2488 }
2065 return 0; 2489 return 0;
2066 } 2490 }
2067 /** @} */ //End for group std_mb 2491 /** @} */ //End for group std_mb
2076 */ 2500 */
2077 static int advanced_decode_i_mbs(VC9Context *v) 2501 static int advanced_decode_i_mbs(VC9Context *v)
2078 { 2502 {
2079 MpegEncContext *s = &v->s; 2503 MpegEncContext *s = &v->s;
2080 GetBitContext *gb = &v->s.gb; 2504 GetBitContext *gb = &v->s.gb;
2081 int mqdiff, mquant, current_mb = 0, over_flags_mb = 0; 2505 int mqdiff, mquant, mb_offset = 0, over_flags_mb = 0;
2082 2506
2083 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++) 2507 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++)
2084 { 2508 {
2085 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++) 2509 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++)
2086 { 2510 {
2087 if (v->ac_pred_plane.is_raw) 2511 if (v->ac_pred_plane.is_raw)
2088 s->ac_pred = get_bits(gb, 1); 2512 s->ac_pred = get_bits(gb, 1);
2089 else 2513 else
2090 s->ac_pred = v->ac_pred_plane.data[current_mb]; 2514 s->ac_pred = v->ac_pred_plane.data[mb_offset];
2091 if (v->condover == 3 && v->over_flags_plane.is_raw) 2515 if (v->condover == 3 && v->over_flags_plane.is_raw)
2092 over_flags_mb = get_bits(gb, 1); 2516 over_flags_mb = get_bits(gb, 1);
2093 GET_MQUANT(); 2517 GET_MQUANT();
2094 2518
2095 /* TODO: lots */ 2519 /* TODO: lots */
2096 } 2520 }
2097 current_mb++; 2521 mb_offset++;
2098 } 2522 }
2099 return 0; 2523 return 0;
2100 } 2524 }
2101 /** @} */ //End for group adv_mb 2525 /** @} */ //End for group adv_mb
2102 #endif 2526 #endif
2128 // looks like WMV3 has a sequence header stored in the extradata 2552 // looks like WMV3 has a sequence header stored in the extradata
2129 // advanced sequence header may be before the first frame 2553 // advanced sequence header may be before the first frame
2130 // the last byte of the extradata is a version number, 1 for the 2554 // the last byte of the extradata is a version number, 1 for the
2131 // samples we can decode 2555 // samples we can decode
2132 2556
2133 init_get_bits(&gb, avctx->extradata, avctx->extradata_size); 2557 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
2134 2558
2135 decode_sequence_header(avctx, &gb); 2559 if (decode_sequence_header(avctx, &gb) < 0)
2560 return -1;
2136 2561
2137 count = avctx->extradata_size*8 - get_bits_count(&gb); 2562 count = avctx->extradata_size*8 - get_bits_count(&gb);
2138 if (count>0) 2563 if (count>0)
2139 { 2564 {
2140 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n", 2565 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
2141 count, get_bits(&gb, count)); 2566 count, get_bits(&gb, count));
2142 } 2567 }
2143 else 2568 else if (count < 0)
2144 { 2569 {
2145 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count); 2570 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
2146 } 2571 }
2147 } 2572 }
2148 avctx->has_b_frames= !!(avctx->max_b_frames); 2573 avctx->has_b_frames= !!(avctx->max_b_frames);
2163 /* For predictors */ 2588 /* For predictors */
2164 v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4); 2589 v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4);
2165 if (!v->previous_line_cbpcy) return -1; 2590 if (!v->previous_line_cbpcy) return -1;
2166 2591
2167 #if HAS_ADVANCED_PROFILE 2592 #if HAS_ADVANCED_PROFILE
2168 if (v->profile > PROFILE_MAIN) 2593 if (v->profile == PROFILE_ADVANCED)
2169 { 2594 {
2170 if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0) 2595 if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
2171 return -1; 2596 return -1;
2172 if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0) 2597 if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
2173 return -1; 2598 return -1;
2232 av_log(avctx, AV_LOG_ERROR, "Interlaced coding not supported\n"); 2657 av_log(avctx, AV_LOG_ERROR, "Interlaced coding not supported\n");
2233 return -1; 2658 return -1;
2234 case 0x0D: //Frame start code 2659 case 0x0D: //Frame start code
2235 break; 2660 break;
2236 case 0x0E: //Entry point Start Code 2661 case 0x0E: //Entry point Start Code
2237 if (v->profile <= MAIN_PROFILE) 2662 if (v->profile < PROFILE_ADVANCED)
2238 av_log(avctx, AV_LOG_ERROR, 2663 av_log(avctx, AV_LOG_ERROR,
2239 "Found an entry point in profile %i\n", v->profile); 2664 "Found an entry point in profile %i\n", v->profile);
2240 advanced_entry_point_process(avctx, gb); 2665 advanced_entry_point_process(avctx, gb);
2241 break; 2666 break;
2242 case 0x0F: //Sequence header Start Code 2667 case 0x0F: //Sequence header Start Code
2283 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there 2708 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
2284 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){ 2709 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
2285 s->current_picture_ptr= &s->picture[ff_find_unused_picture(s, 0)]; 2710 s->current_picture_ptr= &s->picture[ff_find_unused_picture(s, 0)];
2286 } 2711 }
2287 #if HAS_ADVANCED_PROFILE 2712 #if HAS_ADVANCED_PROFILE
2288 if (v->profile > PROFILE_MAIN) 2713 if (v->profile == PROFILE_ADVANCED)
2289 ret= advanced_decode_picture_primary_header(v); 2714 ret= advanced_decode_picture_primary_header(v);
2290 else 2715 else
2291 #endif 2716 #endif
2292 ret= standard_decode_picture_primary_header(v); 2717 ret= standard_decode_picture_primary_header(v);
2293 if (ret == FRAME_SKIPED) return buf_size; 2718 if (ret == FRAME_SKIPED) return buf_size;
2298 } 2723 }
2299 2724
2300 //No bug workaround yet, no DCT conformance 2725 //No bug workaround yet, no DCT conformance
2301 2726
2302 //WMV9 does have resized images 2727 //WMV9 does have resized images
2303 if (v->profile <= PROFILE_MAIN && v->multires){ 2728 if (v->profile < PROFILE_ADVANCED && v->multires){
2304 //Parse context stuff in here, don't know how appliable it is 2729 //Parse context stuff in here, don't know how appliable it is
2305 } 2730 }
2306 //Not sure about context initialization 2731 //Not sure about context initialization
2307 2732
2308 // for hurry_up==5 2733 // for hurry_up==5
2331 2756
2332 ff_er_frame_start(s); 2757 ff_er_frame_start(s);
2333 2758
2334 //wmv9 may or may not have skip bits 2759 //wmv9 may or may not have skip bits
2335 #if HAS_ADVANCED_PROFILE 2760 #if HAS_ADVANCED_PROFILE
2336 if (v->profile > PROFILE_MAIN) 2761 if (v->profile == PROFILE_ADVANCED)
2337 ret= advanced_decode_picture_secondary_header(v); 2762 ret= advanced_decode_picture_secondary_header(v);
2338 else 2763 else
2339 #endif 2764 #endif
2340 ret = standard_decode_picture_secondary_header(v); 2765 ret = standard_decode_picture_secondary_header(v);
2341 if (ret<0) return FRAME_SKIPED; //FIXME Non fatal for now 2766 if (ret<0) return FRAME_SKIPED; //FIXME Non fatal for now
2342 2767
2343 //We consider the image coded in only one slice 2768 //We consider the image coded in only one slice
2344 #if HAS_ADVANCED_PROFILE 2769 #if HAS_ADVANCED_PROFILE
2345 if (v->profile > PROFILE_MAIN) 2770 if (v->profile == PROFILE_ADVANCED)
2346 { 2771 {
2347 switch(s->pict_type) 2772 switch(s->pict_type)
2348 { 2773 {
2349 case I_TYPE: ret = advanced_decode_i_mbs(v); break; 2774 case I_TYPE: ret = advanced_decode_i_mbs(v); break;
2350 case P_TYPE: ret = decode_p_mbs(v); break; 2775 case P_TYPE: ret = decode_p_mbs(v); break;