Mercurial > libavcodec.hg
comparison vc9.c @ 2474:81a9f883a17a libavcodec
In that patch:
- avctx and gb elements were removed from VC9Context, hence a larger diff
- some code was added to h263dec.c regarding CODEC_ID_WMV3 (should apply to CODEC_ID_VC9 too)
- VLC tables and other related tables were made global whenever this seemed necessary; appropriate changes were therefore made to other parts of the code using those tables
- the change for the bitplane management to a struct (some of them should eventually be mapped to MpegEncContext arrays) wasn't associated with the proper frees; should be fixed now
patch by anonymous
better names for globalized tables by me
author | michael |
---|---|
date | Sun, 30 Jan 2005 16:34:57 +0000 |
parents | 5565203c95ee |
children | db05cb59c6fc |
comparison
equal
deleted
inserted
replaced
2473:73afecc117a3 | 2474:81a9f883a17a |
---|---|
30 #include "common.h" | 30 #include "common.h" |
31 #include "dsputil.h" | 31 #include "dsputil.h" |
32 #include "avcodec.h" | 32 #include "avcodec.h" |
33 #include "mpegvideo.h" | 33 #include "mpegvideo.h" |
34 #include "vc9data.h" | 34 #include "vc9data.h" |
35 | |
35 extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2]; | 36 extern const uint32_t ff_table0_dc_lum[120][2], ff_table1_dc_lum[120][2]; |
36 extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2]; | 37 extern const uint32_t ff_table0_dc_chroma[120][2], ff_table1_dc_chroma[120][2]; |
38 extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2]; | |
39 #define MB_INTRA_VLC_BITS 9 | |
40 extern VLC ff_msmp4_mb_i_vlc; | |
41 #define DC_VLC_BITS 9 | |
42 static const uint16_t table_mb_intra[64][2]; | |
37 | 43 |
38 /* Some inhibiting stuff */ | 44 /* Some inhibiting stuff */ |
39 #define HAS_ADVANCED_PROFILE 1 | 45 #define HAS_ADVANCED_PROFILE 1 |
40 #define TRACE 1 | 46 #define TRACE 1 |
41 | 47 |
43 # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \ | 49 # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \ |
44 codes, codes_wrap, codes_size, use_static) \ | 50 codes, codes_wrap, codes_size, use_static) \ |
45 if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \ | 51 if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \ |
46 codes, codes_wrap, codes_size, use_static) < 0) \ | 52 codes, codes_wrap, codes_size, use_static) < 0) \ |
47 { \ | 53 { \ |
48 av_log(v->avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i); \ | 54 av_log(v->s.avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i); \ |
49 return -1; \ | 55 return -1; \ |
50 } | 56 } |
51 #else | 57 #else |
52 # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \ | 58 # define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \ |
53 codes, codes_wrap, codes_size, use_static) \ | 59 codes, codes_wrap, codes_size, use_static) \ |
132 /* Could be optimized, one table only needs 8 bits */ | 138 /* Could be optimized, one table only needs 8 bits */ |
133 #define VC9_TTMB_VLC_BITS 9 //12 | 139 #define VC9_TTMB_VLC_BITS 9 //12 |
134 static VLC vc9_ttmb_vlc[3]; | 140 static VLC vc9_ttmb_vlc[3]; |
135 #define VC9_MV_DIFF_VLC_BITS 9 //15 | 141 #define VC9_MV_DIFF_VLC_BITS 9 //15 |
136 static VLC vc9_mv_diff_vlc[4]; | 142 static VLC vc9_mv_diff_vlc[4]; |
137 #define VC9_CBPCY_I_VLC_BITS 9 //13 | |
138 static VLC vc9_cbpcy_i_vlc; | |
139 #define VC9_CBPCY_P_VLC_BITS 9 //14 | 143 #define VC9_CBPCY_P_VLC_BITS 9 //14 |
140 static VLC vc9_cbpcy_p_vlc[4]; | 144 static VLC vc9_cbpcy_p_vlc[4]; |
141 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6 | 145 #define VC9_4MV_BLOCK_PATTERN_VLC_BITS 6 |
142 static VLC vc9_4mv_block_pattern_vlc[4]; | 146 static VLC vc9_4mv_block_pattern_vlc[4]; |
143 #define VC9_DC_VLC_BITS 9 | |
144 static VLC vc9_luma_dc_vlc[2]; | |
145 static VLC vc9_chroma_dc_vlc[2]; | |
146 | 147 |
147 //We mainly need data and is_raw, so this struct could be avoided | 148 //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 //to save a level of indirection; feel free to modify |
149 typedef struct BitPlane { | 150 typedef struct BitPlane { |
150 uint8_t *data; | 151 uint8_t *data; |
153 uint8_t is_raw; | 154 uint8_t is_raw; |
154 } BitPlane; | 155 } BitPlane; |
155 | 156 |
156 typedef struct VC9Context{ | 157 typedef struct VC9Context{ |
157 /* No MpegEnc context, might be good to use it */ | 158 /* No MpegEnc context, might be good to use it */ |
158 GetBitContext gb; | 159 MpegEncContext s; |
159 AVCodecContext *avctx; | |
160 | 160 |
161 /***************************/ | 161 /***************************/ |
162 /* Sequence Header */ | 162 /* Sequence Header */ |
163 /***************************/ | 163 /***************************/ |
164 /* Simple/Main Profile */ | 164 /* Simple/Main Profile */ |
165 int res_sm; //reserved, 2b | 165 int res_sm; //reserved, 2b |
166 int res_x8; //reserved | 166 int res_x8; //reserved |
167 int multires; //frame-level RESPIC syntax element present | 167 int multires; //frame-level RESPIC syntax element present |
168 int res_fasttx; //always 1 | 168 int res_fasttx; //always 1 |
169 int res_transtab; //always 0 | 169 int res_transtab; //always 0 |
170 int syncmarker; //Sync markers presents | |
171 int rangered; //RANGEREDFRM (range reduction) syntax element present | 170 int rangered; //RANGEREDFRM (range reduction) syntax element present |
172 int res_rtm_flag; //reserved, set to 1 | 171 int res_rtm_flag; //reserved, set to 1 |
173 int reserved; //duh | 172 int reserved; //duh |
174 | 173 |
175 #if HAS_ADVANCED_PROFILE | 174 #if HAS_ADVANCED_PROFILE |
194 int frmrtq_postproc; //3 | 193 int frmrtq_postproc; //3 |
195 int bitrtq_postproc; //5 | 194 int bitrtq_postproc; //5 |
196 int loopfilter; | 195 int loopfilter; |
197 int fastuvmc; //Rounding of qpel vector to hpel ? (not in Simple) | 196 int fastuvmc; //Rounding of qpel vector to hpel ? (not in Simple) |
198 int extended_mv; //Ext MV in P/B (not in Simple) | 197 int extended_mv; //Ext MV in P/B (not in Simple) |
199 int dquant; //Q varies with MBs, 2bits (not in Simple) | 198 int dquant; //How qscale varies with MBs, 2bits (not in Simple) |
200 int vstransform; //variable-size transform46 | 199 int vstransform; //variable-size transform46 |
200 | |
201 | 201 |
202 int overlap; //overlapped transforms in use | 202 int overlap; //overlapped transforms in use |
203 int quantizer_mode; //2, quantizer mode used for sequence, see QUANT_* | 203 int quantizer_mode; //2, quantizer mode used for sequence, see QUANT_* |
204 int finterpflag; //INTERPFRM present | 204 int finterpflag; //INTERPFRM present |
205 | 205 |
207 /*****************************/ | 207 /*****************************/ |
208 /* Frame decoding */ | 208 /* Frame decoding */ |
209 /*****************************/ | 209 /*****************************/ |
210 /* All profiles */ | 210 /* All profiles */ |
211 uint8_t mv_mode, mv_mode2; /* MV coding mode */ | 211 uint8_t mv_mode, mv_mode2; /* MV coding mode */ |
212 uint8_t pict_type; /* Picture type, mapped on MPEG types */ | |
213 uint8_t pq, altpq; /* Quantizers */ | 212 uint8_t pq, altpq; /* Quantizers */ |
214 uint8_t dquantfrm, dqprofile, dqsbedge, dqbilevel; /* pquant parameters */ | 213 uint8_t dquantfrm, dqprofile, dqsbedge, dqbilevel; /* pquant parameters */ |
215 int width_mb, height_mb; | |
216 int tile; /* 3x2 if (width_mb%3) else 2x3 */ | 214 int tile; /* 3x2 if (width_mb%3) else 2x3 */ |
217 VLC *luma_ac_vlc, *chroma_ac_vlc, | 215 VLC *luma_ac_vlc, *chroma_ac_vlc, |
218 *luma_dc_vlc, *chroma_dc_vlc; /* transac/dcfrm bits are indexes */ | 216 *luma_dc_vlc, *chroma_dc_vlc; /* transac/dcfrm bits are indexes */ |
219 uint8_t ttmbf, ttfrm; /* Transform type */ | 217 uint8_t ttmbf, ttfrm; /* Transform type */ |
220 uint8_t lumscale, lumshift; /* Luma compensation parameters */ | 218 uint8_t lumscale, lumshift; /* Luma compensation parameters */ |
249 uint8_t rptfrm, tff, rff; | 247 uint8_t rptfrm, tff, rff; |
250 uint8_t topleftx; | 248 uint8_t topleftx; |
251 uint8_t toplefty; | 249 uint8_t toplefty; |
252 uint8_t bottomrightx; | 250 uint8_t bottomrightx; |
253 uint8_t bottomrighty; | 251 uint8_t bottomrighty; |
254 uint8_t rndctrl; | |
255 uint8_t uvsamp; | 252 uint8_t uvsamp; |
256 uint8_t postproc; | 253 uint8_t postproc; |
257 int hrd_num_leaky_buckets; | 254 int hrd_num_leaky_buckets; |
258 uint8_t bit_rate_exponent; | 255 uint8_t bit_rate_exponent; |
259 uint8_t buffer_size_exponent; | 256 uint8_t buffer_size_exponent; |
297 CLOSE_READER(re, gb); | 294 CLOSE_READER(re, gb); |
298 return limit; | 295 return limit; |
299 #endif | 296 #endif |
300 } | 297 } |
301 | 298 |
302 static int init_common(VC9Context *v) | 299 static int vc9_init_common(VC9Context *v) |
303 { | 300 { |
304 static int done = 0; | 301 static int done = 0; |
305 int i; | 302 int i; |
306 | 303 |
307 /* Set the bit planes */ | 304 /* Set the bit planes */ |
337 vc9_norm2_bits, 1, 1, | 334 vc9_norm2_bits, 1, 1, |
338 vc9_norm2_codes, 1, 1, 1); | 335 vc9_norm2_codes, 1, 1, 1); |
339 INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64, | 336 INIT_VLC(&vc9_norm6_vlc, VC9_NORM6_VLC_BITS, 64, |
340 vc9_norm6_bits, 1, 1, | 337 vc9_norm6_bits, 1, 1, |
341 vc9_norm6_codes, 2, 2, 1); | 338 vc9_norm6_codes, 2, 2, 1); |
342 INIT_VLC(&vc9_cbpcy_i_vlc, VC9_CBPCY_I_VLC_BITS, 64, | |
343 vc9_cbpcy_i_bits, 1, 1, | |
344 vc9_cbpcy_i_codes, 2, 2, 1); | |
345 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7, | 339 INIT_VLC(&vc9_imode_vlc, VC9_IMODE_VLC_BITS, 7, |
346 vc9_imode_bits, 1, 1, | 340 vc9_imode_bits, 1, 1, |
347 vc9_imode_codes, 1, 1, 1); | 341 vc9_imode_codes, 1, 1, 1); |
348 INIT_VLC(&vc9_luma_dc_vlc[0], VC9_DC_VLC_BITS, 120, | |
349 &ff_table0_dc_lum[0][1], 8, 4, | |
350 &ff_table0_dc_lum[0][0], 8, 4, 1); | |
351 INIT_VLC(&vc9_chroma_dc_vlc[0], VC9_DC_VLC_BITS, 120, | |
352 &ff_table0_dc_chroma[0][1], 8, 4, | |
353 &ff_table0_dc_chroma[0][0], 8, 4, 1); | |
354 INIT_VLC(&vc9_luma_dc_vlc[1], VC9_DC_VLC_BITS, 120, | |
355 &ff_table1_dc_lum[0][1], 8, 4, | |
356 &ff_table1_dc_lum[0][0], 8, 4, 1); | |
357 INIT_VLC(&vc9_chroma_dc_vlc[1], VC9_DC_VLC_BITS, 120, | |
358 &ff_table1_dc_chroma[0][1], 8, 4, | |
359 &ff_table1_dc_chroma[0][0], 8, 4, 1); | |
360 for (i=0; i<3; i++) | 342 for (i=0; i<3; i++) |
361 { | 343 { |
362 INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16, | 344 INIT_VLC(&vc9_ttmb_vlc[i], VC9_TTMB_VLC_BITS, 16, |
363 vc9_ttmb_bits[i], 1, 1, | 345 vc9_ttmb_bits[i], 1, 1, |
364 vc9_ttmb_codes[i], 2, 2, 1); | 346 vc9_ttmb_codes[i], 2, 2, 1); |
674 | 656 |
675 #if HAS_ADVANCED_PROFILE | 657 #if HAS_ADVANCED_PROFILE |
676 if (v->profile <= PROFILE_MAIN) | 658 if (v->profile <= PROFILE_MAIN) |
677 #endif | 659 #endif |
678 { | 660 { |
679 v->syncmarker = get_bits(gb, 1); | 661 v->s.resync_marker = get_bits(gb, 1); |
680 v->rangered = get_bits(gb, 1); | 662 v->rangered = get_bits(gb, 1); |
681 } | 663 } |
682 | 664 |
683 avctx->max_b_frames = get_bits(gb, 3); //common | 665 v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common |
684 v->quantizer_mode = get_bits(gb, 2); //common | 666 v->quantizer_mode = get_bits(gb, 2); //common |
685 | 667 |
686 #if HAS_ADVANCED_PROFILE | 668 #if HAS_ADVANCED_PROFILE |
687 if (v->profile <= PROFILE_MAIN) | 669 if (v->profile <= PROFILE_MAIN) |
688 #endif | 670 #endif |
701 "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n" | 683 "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n" |
702 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n" | 684 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n" |
703 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n", | 685 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n", |
704 v->profile, v->frmrtq_postproc, v->bitrtq_postproc, | 686 v->profile, v->frmrtq_postproc, v->bitrtq_postproc, |
705 v->loopfilter, v->multires, v->fastuvmc, v->extended_mv, | 687 v->loopfilter, v->multires, v->fastuvmc, v->extended_mv, |
706 v->rangered, v->vstransform, v->overlap, v->syncmarker, | 688 v->rangered, v->vstransform, v->overlap, v->s.resync_marker, |
707 v->dquant, v->quantizer_mode, avctx->max_b_frames | 689 v->dquant, v->quantizer_mode, avctx->max_b_frames |
708 ); | 690 ); |
709 return 0; | 691 return 0; |
710 #endif | 692 #endif |
711 } | 693 } |
776 bp->width = bp->stride = width; //FIXME Needed for aligned data ? | 758 bp->width = bp->stride = width; //FIXME Needed for aligned data ? |
777 bp->height = height; | 759 bp->height = height; |
778 return 0; | 760 return 0; |
779 } | 761 } |
780 | 762 |
763 void free_bitplane(BitPlane *bp) | |
764 { | |
765 bp->width = bp->stride = bp->height = 0; | |
766 if (bp->data) av_freep(&bp->data); | |
767 } | |
768 | |
781 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ | 769 static void decode_rowskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ |
782 int x, y; | 770 int x, y; |
771 GetBitContext *gb = &v->s.gb; | |
783 | 772 |
784 for (y=0; y<height; y++){ | 773 for (y=0; y<height; y++){ |
785 if (!get_bits(&v->gb, 1)) //rowskip | 774 if (!get_bits(gb, 1)) //rowskip |
786 memset(plane, 0, width); | 775 memset(plane, 0, width); |
787 else | 776 else |
788 for (x=0; x<width; x++) | 777 for (x=0; x<width; x++) |
789 plane[x] = get_bits(&v->gb, 1); | 778 plane[x] = get_bits(gb, 1); |
790 plane += stride; | 779 plane += stride; |
791 } | 780 } |
792 } | 781 } |
793 | 782 |
794 //FIXME optimize | 783 //FIXME optimize |
795 static void decode_colskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ | 784 static void decode_colskip(uint8_t* plane, int width, int height, int stride, VC9Context *v){ |
796 int x, y; | 785 int x, y; |
786 GetBitContext *gb = &v->s.gb; | |
797 | 787 |
798 for (x=0; x<width; x++){ | 788 for (x=0; x<width; x++){ |
799 if (!get_bits(&v->gb, 1)) //colskip | 789 if (!get_bits(gb, 1)) //colskip |
800 for (y=0; y<height; y++) | 790 for (y=0; y<height; y++) |
801 plane[y*stride] = 0; | 791 plane[y*stride] = 0; |
802 else | 792 else |
803 for (y=0; y<height; y++) | 793 for (y=0; y<height; y++) |
804 plane[y*stride] = get_bits(&v->gb, 1); | 794 plane[y*stride] = get_bits(gb, 1); |
805 plane ++; | 795 plane ++; |
806 } | 796 } |
807 } | 797 } |
808 | 798 |
809 //FIXME optimize | 799 //FIXME optimize |
811 // prediction | 801 // prediction |
812 //FIXME Use BitPlane struct or return if table is raw (no bits read here but | 802 //FIXME Use BitPlane struct or return if table is raw (no bits read here but |
813 // later on) | 803 // later on) |
814 static int bitplane_decoding(BitPlane *bp, VC9Context *v) | 804 static int bitplane_decoding(BitPlane *bp, VC9Context *v) |
815 { | 805 { |
806 GetBitContext *gb = &v->s.gb; | |
807 | |
816 int imode, x, y, code, use_vertical_tile, tile_w, tile_h; | 808 int imode, x, y, code, use_vertical_tile, tile_w, tile_h; |
817 uint8_t invert, *planep = bp->data; | 809 uint8_t invert, *planep = bp->data; |
818 | 810 |
819 invert = get_bits(&v->gb, 1); | 811 invert = get_bits(gb, 1); |
820 imode = get_vlc2(&v->gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2); | 812 imode = get_vlc2(gb, vc9_imode_vlc.table, VC9_IMODE_VLC_BITS, 2); |
821 | 813 |
822 bp->is_raw = 0; | 814 bp->is_raw = 0; |
823 switch (imode) | 815 switch (imode) |
824 { | 816 { |
825 case IMODE_RAW: | 817 case IMODE_RAW: |
826 //Data is actually read in the MB layer (same for all tests == "raw") | 818 //Data is actually read in the MB layer (same for all tests == "raw") |
827 bp->is_raw = 1; //invert ignored | 819 bp->is_raw = 1; //invert ignored |
828 return invert; | 820 return invert; |
829 case IMODE_DIFF2: | 821 case IMODE_DIFF2: |
830 case IMODE_NORM2: | 822 case IMODE_NORM2: |
831 if ((bp->height*bp->width) & 1) *(++planep) = get_bits(&v->gb, 1); | 823 if ((bp->height*bp->width) & 1) *(++planep) = get_bits(gb, 1); |
832 for(x=0; x<(bp->height*bp->width)>>1; x++){ | 824 for(x=0; x<(bp->height*bp->width)>>1; x++){ |
833 code = get_vlc2(&v->gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2); | 825 code = get_vlc2(gb, vc9_norm2_vlc.table, VC9_NORM2_VLC_BITS, 2); |
834 *(++planep) = code&1; //lsb => left | 826 *(++planep) = code&1; //lsb => left |
835 *(++planep) = code&2; //msb => right - bitplane => only !0 matters | 827 *(++planep) = code&2; //msb => right - bitplane => only !0 matters |
836 //FIXME width->stride | 828 //FIXME width->stride |
837 } | 829 } |
838 break; | 830 break; |
842 tile_w= use_vertical_tile ? 2 : 3; | 834 tile_w= use_vertical_tile ? 2 : 3; |
843 tile_h= use_vertical_tile ? 3 : 2; | 835 tile_h= use_vertical_tile ? 3 : 2; |
844 | 836 |
845 for(y= bp->height%tile_h; y< bp->height; y+=tile_h){ | 837 for(y= bp->height%tile_h; y< bp->height; y+=tile_h){ |
846 for(x= bp->width%tile_w; x< bp->width; x+=tile_w){ | 838 for(x= bp->width%tile_w; x< bp->width; x+=tile_w){ |
847 code = get_vlc2(&v->gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2); | 839 code = get_vlc2(gb, vc9_norm6_vlc.table, VC9_NORM6_VLC_BITS, 2); |
848 if(code<0){ | 840 if(code<0){ |
849 av_log(v->avctx, AV_LOG_DEBUG, "inavlid NORM-6 VLC\n"); | 841 av_log(v->s.avctx, AV_LOG_DEBUG, "inavlid NORM-6 VLC\n"); |
850 return -1; | 842 return -1; |
851 } | 843 } |
852 //FIXME following is a pure guess and probably wrong | 844 //FIXME following is a pure guess and probably wrong |
853 //FIXME A bitplane (0 | !0), so could the shifts be avoided ? | 845 //FIXME A bitplane (0 | !0), so could the shifts be avoided ? |
854 planep[x + 0*bp->stride]= (code>>0)&1; | 846 planep[x + 0*bp->stride]= (code>>0)&1; |
910 /*****************************************************************************/ | 902 /*****************************************************************************/ |
911 /* VOP Dquant decoding */ | 903 /* VOP Dquant decoding */ |
912 /*****************************************************************************/ | 904 /*****************************************************************************/ |
913 static int vop_dquant_decoding(VC9Context *v) | 905 static int vop_dquant_decoding(VC9Context *v) |
914 { | 906 { |
907 GetBitContext *gb = &v->s.gb; | |
915 int pqdiff; | 908 int pqdiff; |
916 | 909 |
917 //variable size | 910 //variable size |
918 if (v->dquant == 2) | 911 if (v->dquant == 2) |
919 { | 912 { |
920 pqdiff = get_bits(&v->gb, 3); | 913 pqdiff = get_bits(gb, 3); |
921 if (pqdiff == 7) v->altpq = get_bits(&v->gb, 5); | 914 if (pqdiff == 7) v->altpq = get_bits(gb, 5); |
922 else v->altpq = v->pq + pqdiff + 1; | 915 else v->altpq = v->pq + pqdiff + 1; |
923 } | 916 } |
924 else | 917 else |
925 { | 918 { |
926 v->dquantfrm = get_bits(&v->gb, 1); | 919 v->dquantfrm = get_bits(gb, 1); |
927 if ( v->dquantfrm ) | 920 if ( v->dquantfrm ) |
928 { | 921 { |
929 v->dqprofile = get_bits(&v->gb, 2); | 922 v->dqprofile = get_bits(gb, 2); |
930 switch (v->dqprofile) | 923 switch (v->dqprofile) |
931 { | 924 { |
932 case DQPROFILE_SINGLE_EDGE: | 925 case DQPROFILE_SINGLE_EDGE: |
933 case DQPROFILE_DOUBLE_EDGES: | 926 case DQPROFILE_DOUBLE_EDGES: |
934 v->dqsbedge = get_bits(&v->gb, 2); | 927 v->dqsbedge = get_bits(gb, 2); |
935 break; | 928 break; |
936 case DQPROFILE_ALL_MBS: | 929 case DQPROFILE_ALL_MBS: |
937 v->dqbilevel = get_bits(&v->gb, 1); | 930 v->dqbilevel = get_bits(gb, 1); |
938 default: break; //Forbidden ? | 931 default: break; //Forbidden ? |
939 } | 932 } |
940 if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS) | 933 if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS) |
941 { | 934 { |
942 pqdiff = get_bits(&v->gb, 3); | 935 pqdiff = get_bits(gb, 3); |
943 if (pqdiff == 7) v->altpq = get_bits(&v->gb, 5); | 936 if (pqdiff == 7) v->altpq = get_bits(gb, 5); |
944 else v->altpq = v->pq + pqdiff + 1; | 937 else v->altpq = v->pq + pqdiff + 1; |
945 } | 938 } |
946 } | 939 } |
947 } | 940 } |
948 return 0; | 941 return 0; |
959 bfraction then determine if this is a BI frame, calling | 952 bfraction then determine if this is a BI frame, calling |
960 this function afterwards | 953 this function afterwards |
961 - for A Profile, PTYPE already tells so and we can go | 954 - for A Profile, PTYPE already tells so and we can go |
962 directly there | 955 directly there |
963 */ | 956 */ |
957 GetBitContext *gb = &v->s.gb; | |
964 int pqindex; | 958 int pqindex; |
965 | 959 |
966 /* Read the quantization stuff */ | 960 /* Read the quantization stuff */ |
967 pqindex = get_bits(&v->gb, 5); | 961 pqindex = get_bits(gb, 5); |
968 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) | 962 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) |
969 v->pq = pquant_table[0][pqindex]; | 963 v->pq = pquant_table[0][pqindex]; |
970 else | 964 else |
971 { | 965 { |
972 v->pq = pquant_table[v->quantizer_mode-1][pqindex]; | 966 v->pq = pquant_table[v->quantizer_mode-1][pqindex]; |
973 } | 967 } |
974 if (pqindex < 9) v->halfpq = get_bits(&v->gb, 1); | 968 if (pqindex < 9) v->halfpq = get_bits(gb, 1); |
975 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) | 969 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) |
976 v->pquantizer = get_bits(&v->gb, 1); | 970 v->pquantizer = get_bits(gb, 1); |
977 | 971 |
978 /* Read the MV type/mode */ | 972 /* Read the MV type/mode */ |
979 if (v->extended_mv == 1) | 973 if (v->extended_mv == 1) |
980 v->mvrange = get_prefix(&v->gb, 0, 3); | 974 v->mvrange = get_prefix(gb, 0, 3); |
981 | 975 |
982 /* FIXME: what table are used in that case ? */ | 976 /* FIXME: what table are used in that case ? */ |
983 v->mv_diff_vlc = &vc9_mv_diff_vlc[0]; | 977 v->mv_diff_vlc = &vc9_mv_diff_vlc[0]; |
984 v->cbpcy_vlc = &vc9_cbpcy_i_vlc; | 978 v->cbpcy_vlc = &ff_msmp4_mb_i_vlc; |
985 | 979 |
986 av_log(v->avctx, AV_LOG_DEBUG, "B frame, QP=%i\n", v->pq); | 980 av_log(v->s.avctx, AV_LOG_DEBUG, "B frame, QP=%i\n", v->pq); |
987 av_log(v->avctx, AV_LOG_ERROR, "BI_TYPE not supported yet\n"); | 981 av_log(v->s.avctx, AV_LOG_ERROR, "BI_TYPE not supported yet\n"); |
988 /* Epilog should be done in caller */ | 982 /* Epilog should be done in caller */ |
989 return -1; | 983 return -1; |
990 } | 984 } |
991 | 985 |
992 /* Tables 11+12, p62-65 */ | 986 /* Tables 11+12, p62-65 */ |
993 static int decode_b_picture_header(VC9Context *v) | 987 static int decode_b_picture_primary_header(VC9Context *v) |
994 { | 988 { |
995 int pqindex, status; | 989 GetBitContext *gb = &v->s.gb; |
990 int pqindex, status; | |
996 | 991 |
997 /* Prolog common to all frametypes should be done in caller */ | 992 /* Prolog common to all frametypes should be done in caller */ |
998 if (v->profile == PROFILE_SIMPLE) | 993 if (v->profile == PROFILE_SIMPLE) |
999 { | 994 { |
1000 av_log(v, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n"); | 995 av_log(v, AV_LOG_ERROR, "Found a B frame while in Simple Profile!\n"); |
1001 return FRAME_SKIPED; | 996 return FRAME_SKIPED; |
1002 } | 997 } |
1003 | 998 |
1004 v->bfraction = vc9_bfraction_lut[get_vlc2(&v->gb, vc9_bfraction_vlc.table, | 999 v->bfraction = vc9_bfraction_lut[get_vlc2(gb, vc9_bfraction_vlc.table, |
1005 VC9_BFRACTION_VLC_BITS, 2)]; | 1000 VC9_BFRACTION_VLC_BITS, 2)]; |
1006 if (v->bfraction < -1) | 1001 if (v->bfraction < -1) |
1007 { | 1002 { |
1008 av_log(v, AV_LOG_ERROR, "Invalid BFRaction\n"); | 1003 av_log(v, AV_LOG_ERROR, "Invalid BFRaction\n"); |
1009 return FRAME_SKIPED; | 1004 return FRAME_SKIPED; |
1013 /* We actually have a BI frame */ | 1008 /* We actually have a BI frame */ |
1014 return decode_bi_picture_header(v); | 1009 return decode_bi_picture_header(v); |
1015 } | 1010 } |
1016 | 1011 |
1017 /* Read the quantization stuff */ | 1012 /* Read the quantization stuff */ |
1018 pqindex = get_bits(&v->gb, 5); | 1013 pqindex = get_bits(gb, 5); |
1019 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) | 1014 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) |
1020 v->pq = pquant_table[0][pqindex]; | 1015 v->pq = pquant_table[0][pqindex]; |
1021 else | 1016 else |
1022 { | 1017 { |
1023 v->pq = pquant_table[v->quantizer_mode-1][pqindex]; | 1018 v->pq = pquant_table[v->quantizer_mode-1][pqindex]; |
1024 } | 1019 } |
1025 if (pqindex < 9) v->halfpq = get_bits(&v->gb, 1); | 1020 if (pqindex < 9) v->halfpq = get_bits(gb, 1); |
1026 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) | 1021 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) |
1027 v->pquantizer = get_bits(&v->gb, 1); | 1022 v->pquantizer = get_bits(gb, 1); |
1028 | 1023 |
1029 /* Read the MV type/mode */ | 1024 /* Read the MV type/mode */ |
1030 if (v->extended_mv == 1) | 1025 if (v->extended_mv == 1) |
1031 v->mvrange = get_prefix(&v->gb, 0, 3); | 1026 v->mvrange = get_prefix(gb, 0, 3); |
1032 v->mv_mode = get_bits(&v->gb, 1); | 1027 v->mv_mode = get_bits(gb, 1); |
1033 if (v->pq < 13) | 1028 if (v->pq < 13) |
1034 { | 1029 { |
1035 if (!v->mv_mode) | 1030 if (!v->mv_mode) |
1036 { | 1031 { |
1037 v->mv_mode = get_bits(&v->gb, 2); | 1032 v->mv_mode = get_bits(gb, 2); |
1038 if (v->mv_mode) | 1033 if (v->mv_mode) |
1039 av_log(v, AV_LOG_ERROR, | 1034 av_log(v, AV_LOG_ERROR, |
1040 "mv_mode for lowquant B frame was %i\n", v->mv_mode); | 1035 "mv_mode for lowquant B frame was %i\n", v->mv_mode); |
1041 } | 1036 } |
1042 } | 1037 } |
1043 else | 1038 else |
1044 { | 1039 { |
1045 if (!v->mv_mode) | 1040 if (!v->mv_mode) |
1046 { | 1041 { |
1047 if (get_bits(&v->gb, 1)) | 1042 if (get_bits(gb, 1)) |
1048 av_log(v, AV_LOG_ERROR, | 1043 av_log(v, AV_LOG_ERROR, |
1049 "mv_mode for highquant B frame was %i\n", v->mv_mode); | 1044 "mv_mode for highquant B frame was %i\n", v->mv_mode); |
1050 } | 1045 } |
1051 v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping | 1046 v->mv_mode = 1-v->mv_mode; //To match (pq < 13) mapping |
1052 } | 1047 } |
1053 | 1048 |
1049 return 0; | |
1050 } | |
1051 | |
1052 static int decode_b_picture_secondary_header(VC9Context *v) | |
1053 { | |
1054 GetBitContext *gb = &v->s.gb; | |
1055 int status; | |
1056 | |
1057 bitplane_decoding(&v->skip_mb_plane, v); | |
1058 if (status < 0) return -1; | |
1059 #if TRACE | |
1054 if (v->mv_mode == MV_PMODE_MIXED_MV) | 1060 if (v->mv_mode == MV_PMODE_MIXED_MV) |
1055 { | 1061 { |
1056 status = bitplane_decoding(&v->mv_type_mb_plane, v); | 1062 status = bitplane_decoding(&v->mv_type_mb_plane, v); |
1057 if (status < 0) | 1063 if (status < 0) |
1058 return -1; | 1064 return -1; |
1059 #if TRACE | 1065 #if TRACE |
1060 av_log(v->avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: " | 1066 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: " |
1061 "Imode: %i, Invert: %i\n", status>>1, status&1); | 1067 "Imode: %i, Invert: %i\n", status>>1, status&1); |
1062 #endif | 1068 #endif |
1063 } | 1069 } |
1064 | 1070 |
1065 //bitplane | 1071 //bitplane |
1066 status = bitplane_decoding(&v->direct_mb_plane, v); | 1072 status = bitplane_decoding(&v->direct_mb_plane, v); |
1067 if (status < 0) return -1; | 1073 if (status < 0) return -1; |
1068 #if TRACE | 1074 #if TRACE |
1069 av_log(v->avctx, AV_LOG_DEBUG, "MB Direct plane encoding: " | 1075 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Direct plane encoding: " |
1070 "Imode: %i, Invert: %i\n", status>>1, status&1); | 1076 "Imode: %i, Invert: %i\n", status>>1, status&1); |
1071 #endif | 1077 #endif |
1072 | 1078 |
1073 bitplane_decoding(&v->skip_mb_plane, v); | 1079 av_log(v->s.avctx, AV_LOG_DEBUG, "Skip MB plane encoding: " |
1074 if (status < 0) return -1; | |
1075 #if TRACE | |
1076 av_log(v->avctx, AV_LOG_DEBUG, "Skip MB plane encoding: " | |
1077 "Imode: %i, Invert: %i\n", status>>1, status&1); | 1080 "Imode: %i, Invert: %i\n", status>>1, status&1); |
1078 #endif | 1081 #endif |
1079 | 1082 |
1080 /* FIXME: what is actually chosen for B frames ? */ | 1083 /* FIXME: what is actually chosen for B frames ? */ |
1081 v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(&v->gb, 2)]; | 1084 v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(gb, 2)]; |
1082 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(&v->gb, 2)]; | 1085 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(gb, 2)]; |
1083 if (v->dquant) | 1086 if (v->dquant) |
1084 { | 1087 { |
1085 vop_dquant_decoding(v); | 1088 vop_dquant_decoding(v); |
1086 } | 1089 } |
1087 | 1090 |
1088 if (v->vstransform) | 1091 if (v->vstransform) |
1089 { | 1092 { |
1090 v->ttmbf = get_bits(&v->gb, 1); | 1093 v->ttmbf = get_bits(gb, 1); |
1091 if (v->ttmbf) | 1094 if (v->ttmbf) |
1092 { | 1095 { |
1093 v->ttfrm = get_bits(&v->gb, 2); | 1096 v->ttfrm = get_bits(gb, 2); |
1094 av_log(v, AV_LOG_INFO, "Transform used: %ix%i\n", | 1097 av_log(v, AV_LOG_INFO, "Transform used: %ix%i\n", |
1095 (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8); | 1098 (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8); |
1096 } | 1099 } |
1097 } | 1100 } |
1098 /* Epilog should be done in caller */ | 1101 /* Epilog should be done in caller */ |
1100 } | 1103 } |
1101 | 1104 |
1102 /* Tables 5+7, p53-54 and 55-57 */ | 1105 /* Tables 5+7, p53-54 and 55-57 */ |
1103 static int decode_i_picture_header(VC9Context *v) | 1106 static int decode_i_picture_header(VC9Context *v) |
1104 { | 1107 { |
1105 int pqindex, status = 0, ac_pred; | 1108 GetBitContext *gb = &v->s.gb; |
1109 int pqindex, status = 0; | |
1106 | 1110 |
1107 /* Prolog common to all frametypes should be done in caller */ | 1111 /* Prolog common to all frametypes should be done in caller */ |
1108 //BF = Buffer Fullness | 1112 //BF = Buffer Fullness |
1109 if (v->profile <= PROFILE_MAIN && get_bits(&v->gb, 7)) | 1113 if (v->profile <= PROFILE_MAIN && get_bits(gb, 7)) |
1110 { | 1114 { |
1111 av_log(v, AV_LOG_DEBUG, "I BufferFullness not 0\n"); | 1115 av_log(v, AV_LOG_DEBUG, "I BufferFullness not 0\n"); |
1112 } | 1116 } |
1113 | 1117 |
1114 /* Quantizer stuff */ | 1118 /* Quantizer stuff */ |
1115 pqindex = get_bits(&v->gb, 5); | 1119 pqindex = get_bits(gb, 5); |
1116 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) | 1120 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) |
1117 v->pq = pquant_table[0][pqindex]; | 1121 v->pq = pquant_table[0][pqindex]; |
1118 else | 1122 else |
1119 { | 1123 { |
1120 v->pq = pquant_table[v->quantizer_mode-1][pqindex]; | 1124 v->pq = pquant_table[v->quantizer_mode-1][pqindex]; |
1121 } | 1125 } |
1122 if (pqindex < 9) v->halfpq = get_bits(&v->gb, 1); | 1126 if (pqindex < 9) v->halfpq = get_bits(gb, 1); |
1123 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) | 1127 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) |
1124 v->pquantizer = get_bits(&v->gb, 1); | 1128 v->pquantizer = get_bits(gb, 1); |
1125 av_log(v->avctx, AV_LOG_DEBUG, "I frame: QP=%i (+%i/2)\n", | 1129 av_log(v->s.avctx, AV_LOG_DEBUG, "I frame: QP=%i (+%i/2)\n", |
1126 v->pq, v->halfpq); | 1130 v->pq, v->halfpq); |
1127 #if HAS_ADVANCED_PROFILE | 1131 #if HAS_ADVANCED_PROFILE |
1128 if (v->profile <= PROFILE_MAIN) | 1132 if (v->profile <= PROFILE_MAIN) |
1129 #endif | 1133 #endif |
1130 { | 1134 { |
1131 if (v->extended_mv) v->mvrange = get_prefix(&v->gb, 0, 3); | 1135 if (v->extended_mv) v->mvrange = get_prefix(gb, 0, 3); |
1132 if (v->multires) v->respic = get_bits(&v->gb, 2); | 1136 if (v->multires) v->respic = get_bits(gb, 2); |
1133 } | 1137 } |
1134 #if HAS_ADVANCED_PROFILE | 1138 #if HAS_ADVANCED_PROFILE |
1135 else | 1139 else |
1136 { | 1140 { |
1137 ac_pred = get_bits(&v->gb, 1); | 1141 v->s.ac_pred = get_bits(gb, 1); |
1138 if (v->postprocflag) v->postproc = get_bits(&v->gb, 1); | 1142 if (v->postprocflag) v->postproc = get_bits(gb, 1); |
1139 /* 7.1.1.34 + 8.5.2 */ | 1143 /* 7.1.1.34 + 8.5.2 */ |
1140 if (v->overlap && v->pq<9) | 1144 if (v->overlap && v->pq<9) |
1141 { | 1145 { |
1142 v->condover = get_bits(&v->gb, 1); | 1146 v->condover = get_bits(gb, 1); |
1143 if (v->condover) | 1147 if (v->condover) |
1144 { | 1148 { |
1145 v->condover = 2+get_bits(&v->gb, 1); | 1149 v->condover = 2+get_bits(gb, 1); |
1146 if (v->condover == 3) | 1150 if (v->condover == 3) |
1147 { | 1151 { |
1148 status = bitplane_decoding(&v->over_flags_plane, v); | 1152 status = bitplane_decoding(&v->over_flags_plane, v); |
1149 if (status < 0) return -1; | 1153 if (status < 0) return -1; |
1150 #if TRACE | 1154 #if TRACE |
1151 av_log(v->avctx, AV_LOG_DEBUG, "Overflags plane encoding: " | 1155 av_log(v->s.avctx, AV_LOG_DEBUG, "Overflags plane encoding: " |
1152 "Imode: %i, Invert: %i\n", status>>1, status&1); | 1156 "Imode: %i, Invert: %i\n", status>>1, status&1); |
1153 #endif | 1157 #endif |
1154 } | 1158 } |
1155 } | 1159 } |
1156 } | 1160 } |
1160 /* Epilog should be done in caller */ | 1164 /* Epilog should be done in caller */ |
1161 return status; | 1165 return status; |
1162 } | 1166 } |
1163 | 1167 |
1164 /* Table 9, p58-60 */ | 1168 /* Table 9, p58-60 */ |
1165 static int decode_p_picture_header(VC9Context *v) | 1169 static int decode_p_picture_primary_header(VC9Context *v) |
1166 { | 1170 { |
1167 /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */ | 1171 /* INTERFRM, FRMCNT, RANGEREDFRM read in caller */ |
1172 GetBitContext *gb = &v->s.gb; | |
1168 int lowquant, pqindex, status = 0; | 1173 int lowquant, pqindex, status = 0; |
1169 | 1174 |
1170 pqindex = get_bits(&v->gb, 5); | 1175 pqindex = get_bits(gb, 5); |
1171 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) | 1176 if (v->quantizer_mode == QUANT_FRAME_IMPLICIT) |
1172 v->pq = pquant_table[0][pqindex]; | 1177 v->pq = pquant_table[0][pqindex]; |
1173 else | 1178 else |
1174 { | 1179 { |
1175 v->pq = pquant_table[v->quantizer_mode-1][pqindex]; | 1180 v->pq = pquant_table[v->quantizer_mode-1][pqindex]; |
1176 } | 1181 } |
1177 if (pqindex < 9) v->halfpq = get_bits(&v->gb, 1); | 1182 if (pqindex < 9) v->halfpq = get_bits(gb, 1); |
1178 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) | 1183 if (v->quantizer_mode == QUANT_FRAME_EXPLICIT) |
1179 v->pquantizer = get_bits(&v->gb, 1); | 1184 v->pquantizer = get_bits(gb, 1); |
1180 av_log(v->avctx, AV_LOG_DEBUG, "P Frame: QP=%i (+%i/2)\n", | 1185 av_log(v->s.avctx, AV_LOG_DEBUG, "P Frame: QP=%i (+%i/2)\n", |
1181 v->pq, v->halfpq); | 1186 v->pq, v->halfpq); |
1182 if (v->extended_mv == 1) v->mvrange = get_prefix(&v->gb, 0, 3); | 1187 if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3); |
1183 #if HAS_ADVANCED_PROFILE | 1188 #if HAS_ADVANCED_PROFILE |
1184 if (v->profile > PROFILE_MAIN) | 1189 if (v->profile > PROFILE_MAIN) |
1185 { | 1190 { |
1186 if (v->postprocflag) v->postproc = get_bits(&v->gb, 1); | 1191 if (v->postprocflag) v->postproc = get_bits(gb, 1); |
1187 } | 1192 } |
1188 else | 1193 else |
1189 #endif | 1194 #endif |
1190 if (v->multires) v->respic = get_bits(&v->gb, 2); | 1195 if (v->multires) v->respic = get_bits(gb, 2); |
1191 lowquant = (v->pquantizer>12) ? 0 : 1; | 1196 lowquant = (v->pquantizer>12) ? 0 : 1; |
1192 v->mv_mode = mv_pmode_table[lowquant][get_prefix(&v->gb, 1, 4)]; | 1197 v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)]; |
1193 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) | 1198 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) |
1194 { | 1199 { |
1195 v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(&v->gb, 1, 3)]; | 1200 v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(gb, 1, 3)]; |
1196 v->lumscale = get_bits(&v->gb, 6); | 1201 v->lumscale = get_bits(gb, 6); |
1197 v->lumshift = get_bits(&v->gb, 6); | 1202 v->lumshift = get_bits(gb, 6); |
1198 } | 1203 } |
1199 | 1204 return 0; |
1205 } | |
1206 | |
1207 static int decode_p_picture_secondary_header(VC9Context *v) | |
1208 { | |
1209 GetBitContext *gb = &v->s.gb; | |
1210 int status = 0; | |
1200 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP && | 1211 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP && |
1201 v->mv_mode2 == MV_PMODE_MIXED_MV) | 1212 v->mv_mode2 == MV_PMODE_MIXED_MV) |
1202 || v->mv_mode == MV_PMODE_MIXED_MV) | 1213 || v->mv_mode == MV_PMODE_MIXED_MV) |
1203 { | 1214 { |
1204 status = bitplane_decoding(&v->mv_type_mb_plane, v); | 1215 status = bitplane_decoding(&v->mv_type_mb_plane, v); |
1205 if (status < 0) return -1; | 1216 if (status < 0) return -1; |
1206 #if TRACE | 1217 #if TRACE |
1207 av_log(v->avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: " | 1218 av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: " |
1208 "Imode: %i, Invert: %i\n", status>>1, status&1); | 1219 "Imode: %i, Invert: %i\n", status>>1, status&1); |
1209 #endif | 1220 #endif |
1210 } | 1221 } |
1211 | 1222 |
1212 status = bitplane_decoding(&v->skip_mb_plane, v); | 1223 status = bitplane_decoding(&v->skip_mb_plane, v); |
1213 if (status < 0) return -1; | 1224 if (status < 0) return -1; |
1214 #if TRACE | 1225 #if TRACE |
1215 av_log(v->avctx, AV_LOG_DEBUG, "MB Skip plane encoding: " | 1226 av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: " |
1216 "Imode: %i, Invert: %i\n", status>>1, status&1); | 1227 "Imode: %i, Invert: %i\n", status>>1, status&1); |
1217 #endif | 1228 #endif |
1218 | 1229 |
1219 /* Hopefully this is correct for P frames */ | 1230 /* Hopefully this is correct for P frames */ |
1220 v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(&v->gb, 2)]; | 1231 v->mv_diff_vlc = &vc9_mv_diff_vlc[get_bits(gb, 2)]; |
1221 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(&v->gb, 2)]; | 1232 v->cbpcy_vlc = &vc9_cbpcy_p_vlc[get_bits(gb, 2)]; |
1222 | 1233 |
1223 if (v->dquant) | 1234 if (v->dquant) |
1224 { | 1235 { |
1225 av_log(v->avctx, AV_LOG_INFO, "VOP DQuant info\n"); | 1236 av_log(v->s.avctx, AV_LOG_INFO, "VOP DQuant info\n"); |
1226 vop_dquant_decoding(v); | 1237 vop_dquant_decoding(v); |
1227 } | 1238 } |
1228 | 1239 |
1229 if (v->vstransform) | 1240 if (v->vstransform) |
1230 { | 1241 { |
1231 v->ttmbf = get_bits(&v->gb, 1); | 1242 v->ttmbf = get_bits(gb, 1); |
1232 if (v->ttmbf) | 1243 if (v->ttmbf) |
1233 { | 1244 { |
1234 v->ttfrm = get_bits(&v->gb, 2); | 1245 v->ttfrm = get_bits(gb, 2); |
1235 av_log(v->avctx, AV_LOG_INFO, "Transform used: %ix%i\n", | 1246 av_log(v->s.avctx, AV_LOG_INFO, "Transform used: %ix%i\n", |
1236 (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8); | 1247 (v->ttfrm & 2) ? 4 : 8, (v->ttfrm & 1) ? 4 : 8); |
1237 } | 1248 } |
1238 } | 1249 } |
1239 /* Epilog should be done in caller */ | 1250 /* Epilog should be done in caller */ |
1240 return 0; | 1251 return 0; |
1241 } | 1252 } |
1242 | 1253 |
1243 | 1254 |
1244 static int standard_decode_picture_header(VC9Context *v) | 1255 static int standard_decode_picture_primary_header(VC9Context *v) |
1245 { | 1256 { |
1246 int status = 0, index; | 1257 GetBitContext *gb = &v->s.gb; |
1247 | 1258 int status = 0; |
1248 if (v->finterpflag) v->interpfrm = get_bits(&v->gb, 1); | 1259 |
1249 skip_bits(&v->gb, 2); //framecnt unused | 1260 if (v->finterpflag) v->interpfrm = get_bits(gb, 1); |
1250 if (v->rangered) v->rangeredfrm = get_bits(&v->gb, 1); | 1261 skip_bits(gb, 2); //framecnt unused |
1251 v->pict_type = get_bits(&v->gb, 1); | 1262 if (v->rangered) v->rangeredfrm = get_bits(gb, 1); |
1252 if (v->avctx->max_b_frames && !v->pict_type) | 1263 v->s.pict_type = get_bits(gb, 1); |
1253 { | 1264 if (v->s.avctx->max_b_frames && !v->s.pict_type) |
1254 if (get_bits(&v->gb, 1)) v->pict_type = I_TYPE; | 1265 { |
1255 else v->pict_type = P_TYPE; | 1266 if (get_bits(gb, 1)) v->s.pict_type = I_TYPE; |
1256 } | 1267 else v->s.pict_type = P_TYPE; |
1257 else v->pict_type++; //P_TYPE | 1268 } |
1258 | 1269 else v->s.pict_type++; //P_TYPE |
1259 switch (v->pict_type) | 1270 |
1271 switch (v->s.pict_type) | |
1260 { | 1272 { |
1261 case I_TYPE: status = decode_i_picture_header(v); break; | 1273 case I_TYPE: status = decode_i_picture_header(v); break; |
1262 case BI_TYPE: status = decode_b_picture_header(v); break; | 1274 case BI_TYPE: status = decode_bi_picture_header(v); break; |
1263 case P_TYPE: status = decode_p_picture_header(v); break; | 1275 case P_TYPE: status = decode_p_picture_primary_header(v); break; |
1264 case B_TYPE: status = decode_b_picture_header(v); break; | 1276 case B_TYPE: status = decode_b_picture_primary_header(v); break; |
1265 } | 1277 } |
1266 | 1278 |
1267 if (status == FRAME_SKIPED) | 1279 if (status == FRAME_SKIPED) |
1268 { | 1280 { |
1269 av_log(v, AV_LOG_INFO, "Skipping frame...\n"); | 1281 av_log(v, AV_LOG_INFO, "Skipping frame...\n"); |
1270 return status; | 1282 return status; |
1271 } | 1283 } |
1284 return 0; | |
1285 } | |
1286 | |
1287 static int standard_decode_picture_secondary_header(VC9Context *v) | |
1288 { | |
1289 GetBitContext *gb = &v->s.gb; | |
1290 int status = 0, index; | |
1291 | |
1292 switch (v->s.pict_type) | |
1293 { | |
1294 case P_TYPE: status = decode_p_picture_secondary_header(v); break; | |
1295 case B_TYPE: status = decode_b_picture_secondary_header(v); break; | |
1296 } | |
1272 | 1297 |
1273 /* AC Syntax */ | 1298 /* AC Syntax */ |
1274 index = decode012(&v->gb); | 1299 index = decode012(gb); |
1275 v->luma_ac_vlc = NULL + index; //FIXME Add AC table | 1300 v->luma_ac_vlc = NULL + index; //FIXME Add AC table |
1276 v->chroma_ac_vlc = NULL + index; | 1301 v->chroma_ac_vlc = NULL + index; |
1277 if (v->pict_type == I_TYPE || v->pict_type == BI_TYPE) | 1302 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) |
1278 { | 1303 { |
1279 index = decode012(&v->gb); | 1304 index = decode012(gb); |
1280 v->luma_ac2_vlc = NULL + index; //FIXME Add AC2 table | 1305 v->luma_ac2_vlc = NULL + index; //FIXME Add AC2 table |
1281 v->chroma_ac2_vlc = NULL + index; | 1306 v->chroma_ac2_vlc = NULL + index; |
1282 } | 1307 } |
1283 /* DC Syntax */ | 1308 /* DC Syntax */ |
1284 index = decode012(&v->gb); | 1309 index = decode012(gb); |
1285 v->luma_dc_vlc = vc9_luma_dc_vlc + index; | 1310 v->luma_dc_vlc = &ff_msmp4_dc_luma_vlc[index]; |
1286 v->chroma_dc_vlc = vc9_chroma_dc_vlc + index; | 1311 v->chroma_dc_vlc = &ff_msmp4_dc_chroma_vlc[index]; |
1287 | 1312 |
1288 return 0; | 1313 return 0; |
1289 } | 1314 } |
1290 | 1315 |
1291 | 1316 |
1292 #if HAS_ADVANCED_PROFILE | 1317 #if HAS_ADVANCED_PROFILE |
1293 /******************************************************************************/ | 1318 /******************************************************************************/ |
1294 /* Advanced Profile picture header decoding specific functions */ | 1319 /* Advanced Profile picture header decoding specific functions */ |
1295 /******************************************************************************/ | 1320 /******************************************************************************/ |
1296 static int advanced_decode_picture_header(VC9Context *v) | 1321 static int advanced_decode_picture_primary_header(VC9Context *v) |
1297 { | 1322 { |
1323 GetBitContext *gb = &v->s.gb; | |
1298 static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE }; | 1324 static const int type_table[4] = { P_TYPE, B_TYPE, I_TYPE, BI_TYPE }; |
1299 int type, i, index; | 1325 int type, i; |
1300 | 1326 |
1301 if (v->interlace) | 1327 if (v->interlace) |
1302 { | 1328 { |
1303 v->fcm = get_bits(&v->gb, 1); | 1329 v->fcm = get_bits(gb, 1); |
1304 if (v->fcm) v->fcm = 2+get_bits(&v->gb, 1); | 1330 if (v->fcm) v->fcm = 2+get_bits(gb, 1); |
1305 } | 1331 } |
1306 | 1332 |
1307 type = get_prefix(&v->gb, 0, 4); | 1333 type = get_prefix(gb, 0, 4); |
1308 if (type > 4 || type < 0) return FRAME_SKIPED; | 1334 if (type > 4 || type < 0) return FRAME_SKIPED; |
1309 v->pict_type = type_table[type]; | 1335 v->s.pict_type = type_table[type]; |
1310 av_log(v->avctx, AV_LOG_INFO, "AP Frame Type: %i\n", v->pict_type); | 1336 av_log(v->s.avctx, AV_LOG_INFO, "AP Frame Type: %i\n", v->s.pict_type); |
1311 | 1337 |
1312 if (v->tfcntrflag) v->tfcntr = get_bits(&v->gb, 8); | 1338 if (v->tfcntrflag) v->tfcntr = get_bits(gb, 8); |
1313 if (v->broadcast) | 1339 if (v->broadcast) |
1314 { | 1340 { |
1315 if (!v->interlace) v->rptfrm = get_bits(&v->gb, 2); | 1341 if (!v->interlace) v->rptfrm = get_bits(gb, 2); |
1316 else | 1342 else |
1317 { | 1343 { |
1318 v->tff = get_bits(&v->gb, 1); | 1344 v->tff = get_bits(gb, 1); |
1319 v->rff = get_bits(&v->gb, 1); | 1345 v->rff = get_bits(gb, 1); |
1320 } | 1346 } |
1321 } | 1347 } |
1322 | 1348 |
1323 if (v->panscanflag) | 1349 if (v->panscanflag) |
1324 { | 1350 { |
1325 #if 0 | 1351 #if 0 |
1326 for (i=0; i<v->numpanscanwin; i++) | 1352 for (i=0; i<v->numpanscanwin; i++) |
1327 { | 1353 { |
1328 v->topleftx[i] = get_bits(&v->gb, 16); | 1354 v->topleftx[i] = get_bits(gb, 16); |
1329 v->toplefty[i] = get_bits(&v->gb, 16); | 1355 v->toplefty[i] = get_bits(gb, 16); |
1330 v->bottomrightx[i] = get_bits(&v->gb, 16); | 1356 v->bottomrightx[i] = get_bits(gb, 16); |
1331 v->bottomrighty[i] = get_bits(&v->gb, 16); | 1357 v->bottomrighty[i] = get_bits(gb, 16); |
1332 } | 1358 } |
1333 #else | 1359 #else |
1334 skip_bits(&v->gb, 16*4*v->numpanscanwin); | 1360 skip_bits(gb, 16*4*v->numpanscanwin); |
1335 #endif | 1361 #endif |
1336 } | 1362 } |
1337 v->rndctrl = get_bits(&v->gb, 1); | 1363 v->s.no_rounding = !get_bits(gb, 1); |
1338 v->uvsamp = get_bits(&v->gb, 1); | 1364 v->uvsamp = get_bits(gb, 1); |
1339 if (v->finterpflag == 1) v->interpfrm = get_bits(&v->gb, 1); | 1365 if (v->finterpflag == 1) v->interpfrm = get_bits(gb, 1); |
1340 | 1366 |
1341 switch(v->pict_type) | 1367 switch(v->s.pict_type) |
1342 { | 1368 { |
1343 case I_TYPE: if (decode_i_picture_header(v) < 0) return -1; | 1369 case I_TYPE: if (decode_i_picture_header(v) < 0) return -1; |
1344 case P_TYPE: if (decode_p_picture_header(v) < 0) return -1; | 1370 case P_TYPE: if (decode_p_picture_primary_header(v) < 0) return -1; |
1345 case BI_TYPE: | 1371 case BI_TYPE: |
1346 case B_TYPE: if (decode_b_picture_header(v) < 0) return FRAME_SKIPED; | 1372 case B_TYPE: if (decode_b_picture_primary_header(v) < 0) return FRAME_SKIPED; |
1347 default: break; | 1373 default: break; |
1348 } | 1374 } |
1375 return 0; | |
1376 } | |
1377 | |
1378 static int advanced_decode_picture_secondary_header(VC9Context *v) | |
1379 { | |
1380 GetBitContext *gb = &v->s.gb; | |
1381 int index; | |
1382 | |
1383 switch(v->s.pict_type) | |
1384 { | |
1385 case P_TYPE: if (decode_p_picture_secondary_header(v) < 0) return -1; | |
1386 case B_TYPE: if (decode_b_picture_secondary_header(v) < 0) return FRAME_SKIPED; | |
1387 default: break; | |
1388 } | |
1349 | 1389 |
1350 /* AC Syntax */ | 1390 /* AC Syntax */ |
1351 index = decode012(&v->gb); | 1391 index = decode012(gb); |
1352 v->luma_ac_vlc = NULL + index; //FIXME | 1392 v->luma_ac_vlc = NULL + index; //FIXME |
1353 v->chroma_ac_vlc = NULL + index; //FIXME | 1393 v->chroma_ac_vlc = NULL + index; //FIXME |
1354 if (v->pict_type == I_TYPE || v->pict_type == BI_TYPE) | 1394 if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE) |
1355 { | 1395 { |
1356 index = decode012(&v->gb); //FIXME | 1396 index = decode012(gb); //FIXME |
1357 v->luma_ac2_vlc = NULL + index; | 1397 v->luma_ac2_vlc = NULL + index; |
1358 v->chroma_ac2_vlc = NULL + index; | 1398 v->chroma_ac2_vlc = NULL + index; |
1359 } | 1399 } |
1360 /* DC Syntax */ | 1400 /* DC Syntax */ |
1361 index = decode012(&v->gb); | 1401 index = decode012(gb); |
1362 v->luma_dc_vlc = vc9_luma_dc_vlc + index; | 1402 v->luma_dc_vlc = &ff_msmp4_dc_luma_vlc[index]; |
1363 v->chroma_dc_vlc = vc9_chroma_dc_vlc + index; | 1403 v->chroma_dc_vlc = &ff_msmp4_dc_chroma_vlc[index]; |
1364 | 1404 |
1365 return 0; | 1405 return 0; |
1366 } | 1406 } |
1367 #endif | 1407 #endif |
1368 | 1408 |
1371 /******************************************************************************/ | 1411 /******************************************************************************/ |
1372 /* 7.1.4, p91 and 8.1.1.7, p(1)04 */ | 1412 /* 7.1.4, p91 and 8.1.1.7, p(1)04 */ |
1373 /* FIXME proper integration (unusable and lots of parameters to send */ | 1413 /* FIXME proper integration (unusable and lots of parameters to send */ |
1374 int decode_luma_intra_block(VC9Context *v, int mquant) | 1414 int decode_luma_intra_block(VC9Context *v, int mquant) |
1375 { | 1415 { |
1416 GetBitContext *gb = &v->s.gb; | |
1376 int dcdiff; | 1417 int dcdiff; |
1377 | 1418 |
1378 dcdiff = get_vlc2(&v->gb, v->luma_dc_vlc->table, | 1419 dcdiff = get_vlc2(gb, v->luma_dc_vlc->table, |
1379 VC9_DC_VLC_BITS, 2); | 1420 DC_VLC_BITS, 2); |
1380 if (dcdiff) | 1421 if (dcdiff) |
1381 { | 1422 { |
1382 if (dcdiff == 119 /* ESC index value */) | 1423 if (dcdiff == 119 /* ESC index value */) |
1383 { | 1424 { |
1384 /* TODO: Optimize */ | 1425 /* TODO: Optimize */ |
1385 if (mquant == 1) dcdiff = get_bits(&v->gb, 10); | 1426 if (mquant == 1) dcdiff = get_bits(gb, 10); |
1386 else if (mquant == 2) dcdiff = get_bits(&v->gb, 9); | 1427 else if (mquant == 2) dcdiff = get_bits(gb, 9); |
1387 else dcdiff = get_bits(&v->gb, 8); | 1428 else dcdiff = get_bits(gb, 8); |
1388 } | 1429 } |
1389 else | 1430 else |
1390 { | 1431 { |
1391 if (mquant == 1) | 1432 if (mquant == 1) |
1392 dcdiff = (dcdiff<<2) + get_bits(&v->gb, 2) - 3; | 1433 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3; |
1393 else if (mquant == 2) | 1434 else if (mquant == 2) |
1394 dcdiff = (dcdiff<<1) + get_bits(&v->gb, 1) - 1; | 1435 dcdiff = (dcdiff<<1) + get_bits(gb, 1) - 1; |
1395 } | 1436 } |
1396 if (get_bits(&v->gb, 1)) | 1437 if (get_bits(gb, 1)) |
1397 dcdiff = -dcdiff; | 1438 dcdiff = -dcdiff; |
1398 } | 1439 } |
1399 /* FIXME: 8.1.1.15, p(1)13, coeff scaling for Adv Profile */ | 1440 /* FIXME: 8.1.1.15, p(1)13, coeff scaling for Adv Profile */ |
1400 | 1441 |
1401 return 0; | 1442 return 0; |
1405 /* MacroBlock decoding functions */ | 1446 /* MacroBlock decoding functions */ |
1406 /******************************************************************************/ | 1447 /******************************************************************************/ |
1407 /* 8.1.1.5, p(1)02-(1)03 */ | 1448 /* 8.1.1.5, p(1)02-(1)03 */ |
1408 /* We only need to store 3 flags, but math with 4 is easier */ | 1449 /* We only need to store 3 flags, but math with 4 is easier */ |
1409 #define GET_CBPCY(table, bits) \ | 1450 #define GET_CBPCY(table, bits) \ |
1410 predicted_cbpcy = get_vlc2(&v->gb, table, bits, 2); \ | 1451 predicted_cbpcy = get_vlc2(gb, table, bits, 2); \ |
1411 cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \ | 1452 cbpcy[0] = (p_cbpcy[-1] == p_cbpcy[2]) \ |
1412 ? previous_cbpcy[1] : p_cbpcy[+2]; \ | 1453 ? previous_cbpcy[1] : p_cbpcy[+2]; \ |
1413 cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \ | 1454 cbpcy[0] ^= ((predicted_cbpcy>>5)&0x01); \ |
1414 cbpcy[1] = (p_cbpcy[2] == p_cbpcy[3]) ? cbpcy[0] : p_cbpcy[3]; \ | 1455 cbpcy[1] = (p_cbpcy[2] == p_cbpcy[3]) ? cbpcy[0] : p_cbpcy[3]; \ |
1415 cbpcy[1] ^= ((predicted_cbpcy>>4)&0x01); \ | 1456 cbpcy[1] ^= ((predicted_cbpcy>>4)&0x01); \ |
1420 cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01); | 1461 cbpcy[3] ^= ((predicted_cbpcy>>2)&0x01); |
1421 | 1462 |
1422 /* 8.1, p100 */ | 1463 /* 8.1, p100 */ |
1423 static int standard_decode_i_mbs(VC9Context *v) | 1464 static int standard_decode_i_mbs(VC9Context *v) |
1424 { | 1465 { |
1425 int x, y, current_mb = 0; /* MB/Block Position info */ | 1466 GetBitContext *gb = &v->s.gb; |
1426 int ac_pred; | 1467 MpegEncContext *s = &v->s; |
1468 int current_mb = 0; /* MB/Block Position info */ | |
1427 /* FIXME: better to use a pointer than using (x<<4) */ | 1469 /* FIXME: better to use a pointer than using (x<<4) */ |
1428 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy, | 1470 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy, |
1429 *p_cbpcy /* Pointer to skip some math */; | 1471 *p_cbpcy /* Pointer to skip some math */; |
1430 | 1472 |
1431 /* Reset CBPCY predictors */ | 1473 /* Reset CBPCY predictors */ |
1432 memset(v->previous_line_cbpcy, 0, (v->width_mb+1)<<2); | 1474 memset(v->previous_line_cbpcy, 0, s->mb_stride<<2); |
1433 | 1475 |
1434 /* Select ttmb table depending on pq */ | 1476 /* Select ttmb table depending on pq */ |
1435 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0]; | 1477 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0]; |
1436 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1]; | 1478 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1]; |
1437 else v->ttmb_vlc = &vc9_ttmb_vlc[2]; | 1479 else v->ttmb_vlc = &vc9_ttmb_vlc[2]; |
1438 | 1480 |
1439 for (y=0; y<v->height_mb; y++) | 1481 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++) |
1440 { | 1482 { |
1441 /* Init CBPCY for line */ | 1483 /* Init CBPCY for line */ |
1442 *((uint32_t*)previous_cbpcy) = 0x00000000; | 1484 *((uint32_t*)previous_cbpcy) = 0x00000000; |
1443 p_cbpcy = v->previous_line_cbpcy+4; | 1485 p_cbpcy = v->previous_line_cbpcy+4; |
1444 | 1486 |
1445 for (x=0; x<v->width_mb; x++, p_cbpcy += 4) | 1487 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++, p_cbpcy += 4) |
1446 { | 1488 { |
1447 /* Get CBPCY */ | 1489 /* Get CBPCY */ |
1448 GET_CBPCY(vc9_cbpcy_i_vlc.table, VC9_CBPCY_I_VLC_BITS); | 1490 GET_CBPCY(ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS); |
1449 | 1491 |
1450 ac_pred = get_bits(&v->gb, 1); | 1492 s->ac_pred = get_bits(gb, 1); |
1451 | 1493 |
1452 /* TODO: Decode blocks from that mb wrt cbpcy */ | 1494 /* TODO: Decode blocks from that mb wrt cbpcy */ |
1453 | 1495 |
1454 /* Update for next block */ | 1496 /* Update for next block */ |
1455 *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy); | 1497 *((uint32_t*)p_cbpcy) = *((uint32_t*)previous_cbpcy); |
1465 { \ | 1507 { \ |
1466 if (v->dqprofile == DQPROFILE_ALL_MBS) \ | 1508 if (v->dqprofile == DQPROFILE_ALL_MBS) \ |
1467 { \ | 1509 { \ |
1468 if (v->dqbilevel) \ | 1510 if (v->dqbilevel) \ |
1469 { \ | 1511 { \ |
1470 mquant = (get_bits(&v->gb, 1)) ? v->pq : v->altpq; \ | 1512 mquant = (get_bits(gb, 1)) ? v->pq : v->altpq; \ |
1471 } \ | 1513 } \ |
1472 else \ | 1514 else \ |
1473 { \ | 1515 { \ |
1474 mqdiff = get_bits(&v->gb, 3); \ | 1516 mqdiff = get_bits(gb, 3); \ |
1475 if (mqdiff != 7) mquant = v->pq + mqdiff; \ | 1517 if (mqdiff != 7) mquant = v->pq + mqdiff; \ |
1476 else mquant = get_bits(&v->gb, 5); \ | 1518 else mquant = get_bits(gb, 5); \ |
1477 } \ | 1519 } \ |
1478 } \ | 1520 } \ |
1479 } | 1521 } |
1480 | 1522 |
1481 /* MVDATA decoding from 8.3.5.2, p(1)20 */ | 1523 /* MVDATA decoding from 8.3.5.2, p(1)20 */ |
1482 #define GET_MVDATA(_dmv_x, _dmv_y) \ | 1524 #define GET_MVDATA(_dmv_x, _dmv_y) \ |
1483 index = 1 + get_vlc2(&v->gb, v->mv_diff_vlc->table, \ | 1525 index = 1 + get_vlc2(gb, v->mv_diff_vlc->table, \ |
1484 VC9_MV_DIFF_VLC_BITS, 2); \ | 1526 VC9_MV_DIFF_VLC_BITS, 2); \ |
1485 if (index > 36) \ | 1527 if (index > 36) \ |
1486 { \ | 1528 { \ |
1487 mb_has_coeffs = 1; \ | 1529 mb_has_coeffs = 1; \ |
1488 index -= 37; \ | 1530 index -= 37; \ |
1490 else mb_has_coeffs = 0; \ | 1532 else mb_has_coeffs = 0; \ |
1491 mb_is_intra = 0; \ | 1533 mb_is_intra = 0; \ |
1492 if (!index) { _dmv_x = _dmv_y = 0; } \ | 1534 if (!index) { _dmv_x = _dmv_y = 0; } \ |
1493 else if (index == 35) \ | 1535 else if (index == 35) \ |
1494 { \ | 1536 { \ |
1495 _dmv_x = get_bits(&v->gb, k_x); \ | 1537 _dmv_x = get_bits(gb, k_x); \ |
1496 _dmv_y = get_bits(&v->gb, k_y); \ | 1538 _dmv_y = get_bits(gb, k_y); \ |
1497 mb_is_intra = 1; \ | 1539 mb_is_intra = 1; \ |
1498 } \ | 1540 } \ |
1499 else \ | 1541 else \ |
1500 { \ | 1542 { \ |
1501 index1 = index%6; \ | 1543 index1 = index%6; \ |
1502 if (hpel_flag && index1 == 5) val = 1; \ | 1544 if (hpel_flag && index1 == 5) val = 1; \ |
1503 else val = 0; \ | 1545 else val = 0; \ |
1504 val = get_bits(&v->gb, size_table[index1] - val); \ | 1546 val = get_bits(gb, size_table[index1] - val); \ |
1505 sign = 0 - (val&1); \ | 1547 sign = 0 - (val&1); \ |
1506 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ | 1548 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ |
1507 \ | 1549 \ |
1508 index1 = index/6; \ | 1550 index1 = index/6; \ |
1509 if (hpel_flag && index1 == 5) val = 1; \ | 1551 if (hpel_flag && index1 == 5) val = 1; \ |
1510 else val = 0; \ | 1552 else val = 0; \ |
1511 val = get_bits(&v->gb, size_table[index1] - val); \ | 1553 val = get_bits(gb, size_table[index1] - val); \ |
1512 sign = 0 - (val&1); \ | 1554 sign = 0 - (val&1); \ |
1513 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ | 1555 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \ |
1514 } | 1556 } |
1515 | 1557 |
1516 /* 8.1, p(1)15 */ | 1558 /* 8.1, p(1)15 */ |
1517 static int decode_p_mbs(VC9Context *v) | 1559 static int decode_p_mbs(VC9Context *v) |
1518 { | 1560 { |
1519 int x, y, current_mb = 0, i; /* MB/Block Position info */ | 1561 MpegEncContext *s = &v->s; |
1562 GetBitContext *gb = &v->s.gb; | |
1563 int current_mb = 0, i; /* MB/Block Position info */ | |
1520 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy, | 1564 uint8_t cbpcy[4], previous_cbpcy[4], predicted_cbpcy, |
1521 *p_cbpcy /* Pointer to skip some math */; | 1565 *p_cbpcy /* Pointer to skip some math */; |
1522 int hybrid_pred, ac_pred; /* Prediction types */ | 1566 int hybrid_pred; /* Prediction types */ |
1523 int mv_mode_bit = 0; | 1567 int mv_mode_bit = 0; |
1524 int mqdiff, mquant; /* MB quantization */ | 1568 int mqdiff, mquant; /* MB quantization */ |
1525 int ttmb; /* MB Transform type */ | 1569 int ttmb; /* MB Transform type */ |
1526 | 1570 |
1527 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, | 1571 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, |
1550 hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL | 1594 hpel_flag = v->mv_mode & 1; //MV_PMODE is HPEL |
1551 k_x -= hpel_flag; | 1595 k_x -= hpel_flag; |
1552 k_y -= hpel_flag; | 1596 k_y -= hpel_flag; |
1553 | 1597 |
1554 /* Reset CBPCY predictors */ | 1598 /* Reset CBPCY predictors */ |
1555 memset(v->previous_line_cbpcy, 0, (v->width_mb+1)<<2); | 1599 memset(v->previous_line_cbpcy, 0, s->mb_stride<<2); |
1556 | 1600 |
1557 for (y=0; y<v->height_mb; y++) | 1601 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++) |
1558 { | 1602 { |
1559 /* Init CBPCY for line */ | 1603 /* Init CBPCY for line */ |
1560 *((uint32_t*)previous_cbpcy) = 0x00000000; | 1604 *((uint32_t*)previous_cbpcy) = 0x00000000; |
1561 p_cbpcy = v->previous_line_cbpcy+4; | 1605 p_cbpcy = v->previous_line_cbpcy+4; |
1562 | 1606 |
1563 for (x=0; x<v->width_mb; x++) | 1607 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++) |
1564 { | 1608 { |
1565 if (v->mv_type_mb_plane.is_raw) | 1609 if (v->mv_type_mb_plane.is_raw) |
1566 v->mv_type_mb_plane.data[current_mb] = get_bits(&v->gb, 1); | 1610 v->mv_type_mb_plane.data[current_mb] = get_bits(gb, 1); |
1567 if (v->skip_mb_plane.is_raw) | 1611 if (v->skip_mb_plane.is_raw) |
1568 v->skip_mb_plane.data[current_mb] = get_bits(&v->gb, 1); | 1612 v->skip_mb_plane.data[current_mb] = get_bits(gb, 1); |
1569 if (!mv_mode_bit) /* 1MV mode */ | 1613 if (!mv_mode_bit) /* 1MV mode */ |
1570 { | 1614 { |
1571 if (!v->skip_mb_plane.data[current_mb]) | 1615 if (!v->skip_mb_plane.data[current_mb]) |
1572 { | 1616 { |
1573 GET_MVDATA(dmv_x, dmv_y); | 1617 GET_MVDATA(dmv_x, dmv_y); |
1574 | 1618 |
1575 /* hybrid mv pred, 8.3.5.3.4 */ | 1619 /* hybrid mv pred, 8.3.5.3.4 */ |
1576 if (v->mv_mode == MV_PMODE_1MV || | 1620 if (v->mv_mode == MV_PMODE_1MV || |
1577 v->mv_mode == MV_PMODE_MIXED_MV) | 1621 v->mv_mode == MV_PMODE_MIXED_MV) |
1578 hybrid_pred = get_bits(&v->gb, 1); | 1622 hybrid_pred = get_bits(gb, 1); |
1579 if (mb_is_intra && !mb_has_coeffs) | 1623 if (mb_is_intra && !mb_has_coeffs) |
1580 { | 1624 { |
1581 GET_MQUANT(); | 1625 GET_MQUANT(); |
1582 ac_pred = get_bits(&v->gb, 1); | 1626 s->ac_pred = get_bits(gb, 1); |
1583 } | 1627 } |
1584 else if (mb_has_coeffs) | 1628 else if (mb_has_coeffs) |
1585 { | 1629 { |
1586 if (mb_is_intra) ac_pred = get_bits(&v->gb, 1); | 1630 if (mb_is_intra) s->ac_pred = get_bits(gb, 1); |
1587 GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS); | 1631 GET_CBPCY(v->cbpcy_vlc->table, VC9_CBPCY_P_VLC_BITS); |
1588 GET_MQUANT(); | 1632 GET_MQUANT(); |
1589 } | 1633 } |
1590 if (!v->ttmbf) | 1634 if (!v->ttmbf) |
1591 ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table, | 1635 ttmb = get_vlc2(gb, v->ttmb_vlc->table, |
1592 VC9_TTMB_VLC_BITS, 12); | 1636 VC9_TTMB_VLC_BITS, 12); |
1593 /* TODO: decode blocks from that mb wrt cbpcy */ | 1637 /* TODO: decode blocks from that mb wrt cbpcy */ |
1594 } | 1638 } |
1595 else //Skipped | 1639 else //Skipped |
1596 { | 1640 { |
1597 /* hybrid mv pred, 8.3.5.3.4 */ | 1641 /* hybrid mv pred, 8.3.5.3.4 */ |
1598 if (v->mv_mode == MV_PMODE_1MV || | 1642 if (v->mv_mode == MV_PMODE_1MV || |
1599 v->mv_mode == MV_PMODE_MIXED_MV) | 1643 v->mv_mode == MV_PMODE_MIXED_MV) |
1600 hybrid_pred = get_bits(&v->gb, 1); | 1644 hybrid_pred = get_bits(gb, 1); |
1601 } | 1645 } |
1602 } //1MV mode | 1646 } //1MV mode |
1603 else //4MV mode | 1647 else //4MV mode |
1604 { | 1648 { |
1605 if (!v->skip_mb_plane.data[current_mb] /* unskipped MB */) | 1649 if (!v->skip_mb_plane.data[current_mb] /* unskipped MB */) |
1611 if (cbpcy[i] /* cbpcy set for this block */) | 1655 if (cbpcy[i] /* cbpcy set for this block */) |
1612 { | 1656 { |
1613 GET_MVDATA(dmv_x, dmv_y); | 1657 GET_MVDATA(dmv_x, dmv_y); |
1614 } | 1658 } |
1615 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */) | 1659 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */) |
1616 hybrid_pred = get_bits(&v->gb, 1); | 1660 hybrid_pred = get_bits(gb, 1); |
1617 GET_MQUANT(); | 1661 GET_MQUANT(); |
1618 if (mb_is_intra /* One of the 4 blocks is intra */ && | 1662 if (mb_is_intra /* One of the 4 blocks is intra */ && |
1619 index /* non-zero pred for that block */) | 1663 index /* non-zero pred for that block */) |
1620 ac_pred = get_bits(&v->gb, 1); | 1664 s->ac_pred = get_bits(gb, 1); |
1621 if (!v->ttmbf) | 1665 if (!v->ttmbf) |
1622 ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table, | 1666 ttmb = get_vlc2(gb, v->ttmb_vlc->table, |
1623 VC9_TTMB_VLC_BITS, 12); | 1667 VC9_TTMB_VLC_BITS, 12); |
1624 | 1668 |
1625 /* TODO: Process blocks wrt cbpcy */ | 1669 /* TODO: Process blocks wrt cbpcy */ |
1626 | 1670 |
1627 } | 1671 } |
1629 else //Skipped MB | 1673 else //Skipped MB |
1630 { | 1674 { |
1631 for (i=0; i<4; i++) //All 4 Y blocks | 1675 for (i=0; i<4; i++) //All 4 Y blocks |
1632 { | 1676 { |
1633 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */) | 1677 if (v->mv_mode == MV_PMODE_MIXED_MV /* Hybrid pred */) |
1634 hybrid_pred = get_bits(&v->gb, 1); | 1678 hybrid_pred = get_bits(gb, 1); |
1635 | 1679 |
1636 /* TODO: do something */ | 1680 /* TODO: do something */ |
1637 } | 1681 } |
1638 } | 1682 } |
1639 } | 1683 } |
1640 | 1684 |
1641 /* Update for next block */ | 1685 /* Update for next block */ |
1642 #if TRACE > 2 | 1686 #if TRACE > 2 |
1643 av_log(v->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i," | 1687 av_log(s->avctx, AV_LOG_DEBUG, "Block %4i: p_cbpcy=%i%i%i%i, previous_cbpcy=%i%i%i%i," |
1644 " cbpcy=%i%i%i%i\n", current_mb, | 1688 " cbpcy=%i%i%i%i\n", current_mb, |
1645 p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3], | 1689 p_cbpcy[0], p_cbpcy[1], p_cbpcy[2], p_cbpcy[3], |
1646 previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3], | 1690 previous_cbpcy[0], previous_cbpcy[1], previous_cbpcy[2], previous_cbpcy[3], |
1647 cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]); | 1691 cbpcy[0], cbpcy[1], cbpcy[2], cbpcy[3]); |
1648 #endif | 1692 #endif |
1654 return 0; | 1698 return 0; |
1655 } | 1699 } |
1656 | 1700 |
1657 static int decode_b_mbs(VC9Context *v) | 1701 static int decode_b_mbs(VC9Context *v) |
1658 { | 1702 { |
1659 int x, y, current_mb = 0, i /* MB / B postion information */; | 1703 MpegEncContext *s = &v->s; |
1660 int ac_pred; | 1704 GetBitContext *gb = &v->s.gb; |
1705 int current_mb = 0, i /* MB / B postion information */; | |
1661 int b_mv_type = BMV_TYPE_BACKWARD; | 1706 int b_mv_type = BMV_TYPE_BACKWARD; |
1662 int mquant, mqdiff; /* MB quant stuff */ | 1707 int mquant, mqdiff; /* MB quant stuff */ |
1663 int ttmb; /* MacroBlock transform type */ | 1708 int ttmb; /* MacroBlock transform type */ |
1664 | 1709 |
1665 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, | 1710 static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, |
1686 /* Select ttmb table depending on pq */ | 1731 /* Select ttmb table depending on pq */ |
1687 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0]; | 1732 if (v->pq < 5) v->ttmb_vlc = &vc9_ttmb_vlc[0]; |
1688 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1]; | 1733 else if (v->pq < 13) v->ttmb_vlc = &vc9_ttmb_vlc[1]; |
1689 else v->ttmb_vlc = &vc9_ttmb_vlc[2]; | 1734 else v->ttmb_vlc = &vc9_ttmb_vlc[2]; |
1690 | 1735 |
1691 for (y=0; y<v->height_mb; y++) | 1736 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++) |
1692 { | 1737 { |
1693 for (x=0; x<v->width_mb; x++) | 1738 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++) |
1694 { | 1739 { |
1695 if (v->direct_mb_plane.is_raw) | 1740 if (v->direct_mb_plane.is_raw) |
1696 v->direct_mb_plane.data[current_mb] = get_bits(&v->gb, 1); | 1741 v->direct_mb_plane.data[current_mb] = get_bits(gb, 1); |
1697 if (v->skip_mb_plane.is_raw) | 1742 if (v->skip_mb_plane.is_raw) |
1698 v->skip_mb_plane.data[current_mb] = get_bits(&v->gb, 1); | 1743 v->skip_mb_plane.data[current_mb] = get_bits(gb, 1); |
1699 | 1744 |
1700 if (!v->direct_mb_plane.data[current_mb]) | 1745 if (!v->direct_mb_plane.data[current_mb]) |
1701 { | 1746 { |
1702 if (v->skip_mb_plane.data[current_mb]) | 1747 if (v->skip_mb_plane.data[current_mb]) |
1703 { | 1748 { |
1704 b_mv_type = decode012(&v->gb); | 1749 b_mv_type = decode012(gb); |
1705 if (v->bfraction > 420 /*1/2*/ && | 1750 if (v->bfraction > 420 /*1/2*/ && |
1706 b_mv_type < 3) b_mv_type = 1-b_mv_type; | 1751 b_mv_type < 3) b_mv_type = 1-b_mv_type; |
1707 } | 1752 } |
1708 else | 1753 else |
1709 { | 1754 { |
1710 /* FIXME getting tired commenting */ | 1755 /* FIXME getting tired commenting */ |
1711 GET_MVDATA(dmv1_x, dmv1_y); | 1756 GET_MVDATA(dmv1_x, dmv1_y); |
1712 if (!mb_is_intra /* b_mv1 tells not intra */) | 1757 if (!mb_is_intra /* b_mv1 tells not intra */) |
1713 { | 1758 { |
1714 /* FIXME: actually read it */ | 1759 /* FIXME: actually read it */ |
1715 b_mv_type = decode012(&v->gb); | 1760 b_mv_type = decode012(gb); |
1716 if (v->bfraction > 420 /*1/2*/ && | 1761 if (v->bfraction > 420 /*1/2*/ && |
1717 b_mv_type < 3) b_mv_type = 1-b_mv_type; | 1762 b_mv_type < 3) b_mv_type = 1-b_mv_type; |
1718 } | 1763 } |
1719 } | 1764 } |
1720 } | 1765 } |
1722 { | 1767 { |
1723 if (mb_has_coeffs /* BMV1 == "last" */) | 1768 if (mb_has_coeffs /* BMV1 == "last" */) |
1724 { | 1769 { |
1725 GET_MQUANT(); | 1770 GET_MQUANT(); |
1726 if (mb_is_intra /* intra mb */) | 1771 if (mb_is_intra /* intra mb */) |
1727 ac_pred = get_bits(&v->gb, 1); | 1772 s->ac_pred = get_bits(gb, 1); |
1728 } | 1773 } |
1729 else | 1774 else |
1730 { | 1775 { |
1731 /* if bmv1 tells MVs are interpolated */ | 1776 /* if bmv1 tells MVs are interpolated */ |
1732 if (b_mv_type == BMV_TYPE_INTERPOLATED) | 1777 if (b_mv_type == BMV_TYPE_INTERPOLATED) |
1735 } | 1780 } |
1736 /* GET_MVDATA has reset some stuff */ | 1781 /* GET_MVDATA has reset some stuff */ |
1737 if (mb_has_coeffs /* b_mv2 == "last" */) | 1782 if (mb_has_coeffs /* b_mv2 == "last" */) |
1738 { | 1783 { |
1739 if (mb_is_intra /* intra_mb */) | 1784 if (mb_is_intra /* intra_mb */) |
1740 ac_pred = get_bits(&v->gb, 1); | 1785 s->ac_pred = get_bits(gb, 1); |
1741 GET_MQUANT(); | 1786 GET_MQUANT(); |
1742 } | 1787 } |
1743 } | 1788 } |
1744 } | 1789 } |
1745 //End1 | 1790 //End1 |
1746 if (v->ttmbf) | 1791 if (v->ttmbf) |
1747 ttmb = get_vlc2(&v->gb, v->ttmb_vlc->table, | 1792 ttmb = get_vlc2(gb, v->ttmb_vlc->table, |
1748 VC9_TTMB_VLC_BITS, 12); | 1793 VC9_TTMB_VLC_BITS, 12); |
1749 | 1794 |
1750 //End2 | 1795 //End2 |
1751 for (i=0; i<6; i++) | 1796 for (i=0; i<6; i++) |
1752 { | 1797 { |
1760 } | 1805 } |
1761 | 1806 |
1762 #if HAS_ADVANCED_PROFILE | 1807 #if HAS_ADVANCED_PROFILE |
1763 static int advanced_decode_i_mbs(VC9Context *v) | 1808 static int advanced_decode_i_mbs(VC9Context *v) |
1764 { | 1809 { |
1765 int x, y, mqdiff, mquant, ac_pred, current_mb = 0, over_flags_mb = 0; | 1810 MpegEncContext *s = &v->s; |
1766 | 1811 GetBitContext *gb = &v->s.gb; |
1767 for (y=0; y<v->height_mb; y++) | 1812 int mqdiff, mquant, current_mb = 0, over_flags_mb = 0; |
1768 { | 1813 |
1769 for (x=0; x<v->width_mb; x++) | 1814 for (s->mb_y=0; s->mb_y<s->mb_height; s->mb_y++) |
1770 { | 1815 { |
1771 if (v->ac_pred_plane.data[current_mb]) | 1816 for (s->mb_x=0; s->mb_x<s->mb_width; s->mb_x++) |
1772 ac_pred = get_bits(&v->gb, 1); | 1817 { |
1818 if (v->ac_pred_plane.is_raw) | |
1819 s->ac_pred = get_bits(gb, 1); | |
1820 else | |
1821 s->ac_pred = v->ac_pred_plane.data[current_mb]; | |
1773 if (v->condover == 3 && v->over_flags_plane.is_raw) | 1822 if (v->condover == 3 && v->over_flags_plane.is_raw) |
1774 over_flags_mb = get_bits(&v->gb, 1); | 1823 over_flags_mb = get_bits(gb, 1); |
1775 GET_MQUANT(); | 1824 GET_MQUANT(); |
1776 | 1825 |
1777 /* TODO: lots */ | 1826 /* TODO: lots */ |
1778 } | 1827 } |
1779 current_mb++; | 1828 current_mb++; |
1783 #endif | 1832 #endif |
1784 | 1833 |
1785 static int vc9_decode_init(AVCodecContext *avctx) | 1834 static int vc9_decode_init(AVCodecContext *avctx) |
1786 { | 1835 { |
1787 VC9Context *v = avctx->priv_data; | 1836 VC9Context *v = avctx->priv_data; |
1837 MpegEncContext *s = &v->s; | |
1788 GetBitContext gb; | 1838 GetBitContext gb; |
1789 | 1839 |
1790 if (!avctx->extradata_size || !avctx->extradata) return -1; | 1840 if (!avctx->extradata_size || !avctx->extradata) return -1; |
1791 avctx->pix_fmt = PIX_FMT_YUV420P; | 1841 avctx->pix_fmt = PIX_FMT_YUV420P; |
1792 v->avctx = avctx; | 1842 v->s.avctx = avctx; |
1793 | 1843 |
1794 if (init_common(v) < 0) return -1; | 1844 if(ff_h263_decode_init(avctx) < 0) |
1845 return -1; | |
1846 if (vc9_init_common(v) < 0) return -1; | |
1795 | 1847 |
1796 avctx->coded_width = avctx->width; | 1848 avctx->coded_width = avctx->width; |
1797 avctx->coded_height = avctx->height; | 1849 avctx->coded_height = avctx->height; |
1798 if (avctx->codec_id == CODEC_ID_WMV3) | 1850 if (avctx->codec_id == CODEC_ID_WMV3) |
1799 { | 1851 { |
1800 int count = 0; | 1852 int count = 0; |
1801 | 1853 |
1802 // looks like WMV3 has a sequence header stored in the extradata | 1854 // looks like WMV3 has a sequence header stored in the extradata |
1803 // advanced sequence header may be before the first frame | 1855 // advanced sequence header may be before the first frame |
1804 // the last byte of the extradata is a version number, 1 for the | 1856 // the last byte of the extradata is a version number, 1 for the |
1805 // samples we can decode | 1857 // samples we can decode |
1806 | 1858 |
1807 init_get_bits(&gb, avctx->extradata, avctx->extradata_size); | 1859 init_get_bits(&gb, avctx->extradata, avctx->extradata_size); |
1808 | 1860 |
1809 decode_sequence_header(avctx, &gb); | 1861 decode_sequence_header(avctx, &gb); |
1810 | 1862 |
1811 count = avctx->extradata_size*8 - get_bits_count(&gb); | 1863 count = avctx->extradata_size*8 - get_bits_count(&gb); |
1812 if (count>0) | 1864 if (count>0) |
1813 { | 1865 { |
1814 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n", | 1866 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n", |
1815 count, get_bits(&gb, count)); | 1867 count, get_bits(&gb, count)); |
1816 } | 1868 } |
1817 else | 1869 else |
1818 { | 1870 { |
1819 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count); | 1871 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count); |
1820 } | 1872 } |
1821 } | 1873 } |
1822 | 1874 avctx->has_b_frames= !!(avctx->max_b_frames); |
1823 /* Done with header parsing */ | 1875 |
1824 //FIXME I feel like this is wrong | 1876 s->mb_width = (avctx->coded_width+15)>>4; |
1825 v->width_mb = (avctx->coded_width+15)>>4; | 1877 s->mb_height = (avctx->coded_height+15)>>4; |
1826 v->height_mb = (avctx->coded_height+15)>>4; | |
1827 | 1878 |
1828 /* Allocate mb bitplanes */ | 1879 /* Allocate mb bitplanes */ |
1829 if (alloc_bitplane(&v->mv_type_mb_plane, v->width_mb, v->height_mb) < 0) | 1880 if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0) |
1830 return -1; | 1881 return -1; |
1831 if (alloc_bitplane(&v->mv_type_mb_plane, v->width_mb, v->height_mb) < 0) | 1882 if (alloc_bitplane(&v->mv_type_mb_plane, s->mb_width, s->mb_height) < 0) |
1832 return -1; | 1883 return -1; |
1833 if (alloc_bitplane(&v->skip_mb_plane, v->width_mb, v->height_mb) < 0) | 1884 if (alloc_bitplane(&v->skip_mb_plane, s->mb_width, s->mb_height) < 0) |
1834 return -1; | 1885 return -1; |
1835 if (alloc_bitplane(&v->direct_mb_plane, v->width_mb, v->height_mb) < 0) | 1886 if (alloc_bitplane(&v->direct_mb_plane, s->mb_width, s->mb_height) < 0) |
1836 return -1; | 1887 return -1; |
1837 | 1888 |
1838 /* For predictors */ | 1889 /* For predictors */ |
1839 v->previous_line_cbpcy = (uint8_t *)av_malloc((v->width_mb+1)*4); | 1890 v->previous_line_cbpcy = (uint8_t *)av_malloc(s->mb_stride*4); |
1840 if (!v->previous_line_cbpcy) return -1; | 1891 if (!v->previous_line_cbpcy) return -1; |
1841 | 1892 |
1842 #if HAS_ADVANCED_PROFILE | 1893 #if HAS_ADVANCED_PROFILE |
1843 if (v->profile > PROFILE_MAIN) | 1894 if (v->profile > PROFILE_MAIN) |
1844 { | 1895 { |
1845 if (alloc_bitplane(&v->over_flags_plane, v->width_mb, v->height_mb) < 0) | 1896 if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0) |
1846 return -1; | 1897 return -1; |
1847 if (alloc_bitplane(&v->ac_pred_plane, v->width_mb, v->height_mb) < 0) | 1898 if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0) |
1848 return -1; | 1899 return -1; |
1849 } | 1900 } |
1850 #endif | 1901 #endif |
1851 | 1902 |
1852 return 0; | 1903 return 0; |
1853 } | 1904 } |
1854 | 1905 |
1855 static int vc9_decode_frame(AVCodecContext *avctx, | 1906 static int vc9_decode_frame(AVCodecContext *avctx, |
1856 void *data, int *data_size, | 1907 void *data, int *data_size, |
1857 uint8_t *buf, int buf_size) | 1908 uint8_t *buf, int buf_size) |
1858 { | 1909 { |
1859 VC9Context *v = avctx->priv_data; | 1910 VC9Context *v = avctx->priv_data; |
1911 MpegEncContext *s = &v->s; | |
1860 int ret = FRAME_SKIPED, len, start_code; | 1912 int ret = FRAME_SKIPED, len, start_code; |
1861 AVFrame *pict = data; | 1913 AVFrame *pict = data; |
1862 uint8_t *tmp_buf; | 1914 uint8_t *tmp_buf; |
1863 v->avctx = avctx; | 1915 v->s.avctx = avctx; |
1864 | 1916 |
1865 //buf_size = 0 -> last frame | 1917 //buf_size = 0 -> last frame |
1866 if (!buf_size) return 0; | 1918 if (!buf_size) return 0; |
1867 | 1919 |
1868 len = avpicture_get_size(avctx->pix_fmt, avctx->width, | 1920 len = avpicture_get_size(avctx->pix_fmt, avctx->width, |
1869 avctx->height); | 1921 avctx->height); |
1870 tmp_buf = (uint8_t *)av_mallocz(len); | 1922 tmp_buf = (uint8_t *)av_mallocz(len); |
1871 avpicture_fill((AVPicture *)pict, tmp_buf, avctx->pix_fmt, | 1923 avpicture_fill((AVPicture *)pict, tmp_buf, avctx->pix_fmt, |
1872 avctx->width, avctx->height); | 1924 avctx->width, avctx->height); |
1873 | 1925 |
1874 if (avctx->codec_id == CODEC_ID_WMV3) | 1926 if (avctx->codec_id == CODEC_ID_VC9) |
1875 { | |
1876 //No IDU | |
1877 init_get_bits(&v->gb, buf, buf_size*8); | |
1878 | |
1879 #if HAS_ADVANCED_PROFILE | |
1880 if (v->profile > PROFILE_MAIN) | |
1881 { | |
1882 if (advanced_decode_picture_header(v) == FRAME_SKIPED) return buf_size; | |
1883 switch(v->pict_type) | |
1884 { | |
1885 case I_TYPE: ret = advanced_decode_i_mbs(v); break; | |
1886 case P_TYPE: ret = decode_p_mbs(v); break; | |
1887 case B_TYPE: | |
1888 case BI_TYPE: ret = decode_b_mbs(v); break; | |
1889 default: ret = FRAME_SKIPED; | |
1890 } | |
1891 if (ret == FRAME_SKIPED) return buf_size; //We ignore for now failures | |
1892 } | |
1893 else | |
1894 #endif | |
1895 { | |
1896 if (standard_decode_picture_header(v) == FRAME_SKIPED) return buf_size; | |
1897 switch(v->pict_type) | |
1898 { | |
1899 case I_TYPE: ret = standard_decode_i_mbs(v); break; | |
1900 case P_TYPE: ret = decode_p_mbs(v); break; | |
1901 case B_TYPE: | |
1902 case BI_TYPE: ret = decode_b_mbs(v); break; | |
1903 default: ret = FRAME_SKIPED; | |
1904 } | |
1905 if (ret == FRAME_SKIPED) return buf_size; | |
1906 } | |
1907 } | |
1908 else | |
1909 { | 1927 { |
1910 #if 0 | 1928 #if 0 |
1911 // search for IDU's | 1929 // search for IDU's |
1912 // FIXME | 1930 // FIXME |
1913 uint32_t scp = 0; | 1931 uint32_t scp = 0; |
1921 if (scp != 0x000001) | 1939 if (scp != 0x000001) |
1922 break; // eof ? | 1940 break; // eof ? |
1923 | 1941 |
1924 scs = buf[i++]; | 1942 scs = buf[i++]; |
1925 | 1943 |
1926 init_get_bits(&v->gb, buf+i, (buf_size-i)*8); | 1944 init_get_bits(gb, buf+i, (buf_size-i)*8); |
1927 | 1945 |
1928 switch(scs) | 1946 switch(scs) |
1929 { | 1947 { |
1930 case 0x0A: //Sequence End Code | 1948 case 0x0A: //Sequence End Code |
1931 return 0; | 1949 return 0; |
1939 break; | 1957 break; |
1940 case 0x0E: //Entry point Start Code | 1958 case 0x0E: //Entry point Start Code |
1941 if (v->profile <= MAIN_PROFILE) | 1959 if (v->profile <= MAIN_PROFILE) |
1942 av_log(avctx, AV_LOG_ERROR, | 1960 av_log(avctx, AV_LOG_ERROR, |
1943 "Found an entry point in profile %i\n", v->profile); | 1961 "Found an entry point in profile %i\n", v->profile); |
1944 advanced_entry_point_process(avctx, &v->gb); | 1962 advanced_entry_point_process(avctx, gb); |
1945 break; | 1963 break; |
1946 case 0x0F: //Sequence header Start Code | 1964 case 0x0F: //Sequence header Start Code |
1947 decode_sequence_header(avctx, &v->gb); | 1965 decode_sequence_header(avctx, gb); |
1948 break; | 1966 break; |
1949 default: | 1967 default: |
1950 av_log(avctx, AV_LOG_ERROR, | 1968 av_log(avctx, AV_LOG_ERROR, |
1951 "Unsupported IDU suffix %lX\n", scs); | 1969 "Unsupported IDU suffix %lX\n", scs); |
1952 } | 1970 } |
1953 | 1971 |
1954 i += get_bits_count(&v->gb)*8; | 1972 i += get_bits_count(gb)*8; |
1955 } | 1973 } |
1956 #else | 1974 #else |
1957 av_abort(); | 1975 av_abort(); |
1958 #endif | 1976 #endif |
1959 } | 1977 } |
1978 else | |
1979 init_get_bits(&v->s.gb, buf, buf_size*8); | |
1980 | |
1981 s->flags= avctx->flags; | |
1982 s->flags2= avctx->flags2; | |
1983 | |
1984 /* no supplementary picture */ | |
1985 if (buf_size == 0) { | |
1986 /* special case for last picture */ | |
1987 if (s->low_delay==0 && s->next_picture_ptr) { | |
1988 *pict= *(AVFrame*)s->next_picture_ptr; | |
1989 s->next_picture_ptr= NULL; | |
1990 | |
1991 *data_size = sizeof(AVFrame); | |
1992 } | |
1993 | |
1994 return 0; | |
1995 } | |
1996 | |
1997 //No IDU - we mimic ff_h263_decode_frame | |
1998 s->bitstream_buffer_size=0; | |
1999 | |
2000 if (!s->context_initialized) { | |
2001 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix | |
2002 return -1; | |
2003 } | |
2004 | |
2005 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there | |
2006 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){ | |
2007 s->current_picture_ptr= &s->picture[ff_find_unused_picture(s, 0)]; | |
2008 } | |
2009 #if HAS_ADVANCED_PROFILE | |
2010 if (v->profile > PROFILE_MAIN) | |
2011 ret= advanced_decode_picture_primary_header(v); | |
2012 else | |
2013 #endif | |
2014 ret= standard_decode_picture_primary_header(v); | |
2015 if (ret == FRAME_SKIPED) return buf_size; | |
2016 /* skip if the header was thrashed */ | |
2017 if (ret < 0){ | |
2018 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n"); | |
2019 return -1; | |
2020 } | |
2021 | |
2022 //No bug workaround yet, no DCT conformance | |
2023 | |
2024 //WMV9 does have resized images | |
2025 if (v->profile <= PROFILE_MAIN && v->multires){ | |
2026 //Parse context stuff in here, don't know how appliable it is | |
2027 } | |
2028 //Not sure about context initialization | |
2029 | |
2030 // for hurry_up==5 | |
2031 s->current_picture.pict_type= s->pict_type; | |
2032 s->current_picture.key_frame= s->pict_type == I_TYPE; | |
2033 | |
2034 /* skip b frames if we dont have reference frames */ | |
2035 if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) | |
2036 return buf_size; //FIXME simulating all buffer consumed | |
2037 /* skip b frames if we are in a hurry */ | |
2038 if(avctx->hurry_up && s->pict_type==B_TYPE) | |
2039 return buf_size; //FIXME simulating all buffer consumed | |
2040 /* skip everything if we are in a hurry>=5 */ | |
2041 if(avctx->hurry_up>=5) | |
2042 return buf_size; //FIXME simulating all buffer consumed | |
2043 | |
2044 if(s->next_p_frame_damaged){ | |
2045 if(s->pict_type==B_TYPE) | |
2046 return buf_size; //FIXME simulating all buffer consumed | |
2047 else | |
2048 s->next_p_frame_damaged=0; | |
2049 } | |
2050 | |
2051 if(MPV_frame_start(s, avctx) < 0) | |
2052 return -1; | |
2053 | |
2054 ff_er_frame_start(s); | |
2055 | |
2056 //wmv9 may or may not have skip bits | |
2057 #if HAS_ADVANCED_PROFILE | |
2058 if (v->profile > PROFILE_MAIN) | |
2059 ret= advanced_decode_picture_secondary_header(v); | |
2060 else | |
2061 #endif | |
2062 ret = standard_decode_picture_secondary_header(v); | |
2063 if (ret<0) return FRAME_SKIPED; //FIXME Non fatal for now | |
2064 | |
2065 //We consider the image coded in only one slice | |
2066 #if HAS_ADVANCED_PROFILE | |
2067 if (v->profile > PROFILE_MAIN) | |
2068 { | |
2069 switch(s->pict_type) | |
2070 { | |
2071 case I_TYPE: ret = advanced_decode_i_mbs(v); break; | |
2072 case P_TYPE: ret = decode_p_mbs(v); break; | |
2073 case B_TYPE: | |
2074 case BI_TYPE: ret = decode_b_mbs(v); break; | |
2075 default: ret = FRAME_SKIPED; | |
2076 } | |
2077 if (ret == FRAME_SKIPED) return buf_size; //We ignore for now failures | |
2078 } | |
2079 else | |
2080 #endif | |
2081 { | |
2082 switch(s->pict_type) | |
2083 { | |
2084 case I_TYPE: ret = standard_decode_i_mbs(v); break; | |
2085 case P_TYPE: ret = decode_p_mbs(v); break; | |
2086 case B_TYPE: | |
2087 case BI_TYPE: ret = decode_b_mbs(v); break; | |
2088 default: ret = FRAME_SKIPED; | |
2089 } | |
2090 if (ret == FRAME_SKIPED) return buf_size; | |
2091 } | |
2092 | |
2093 ff_er_frame_end(s); | |
2094 | |
2095 MPV_frame_end(s); | |
2096 | |
2097 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type); | |
2098 assert(s->current_picture.pict_type == s->pict_type); | |
2099 if(s->pict_type==B_TYPE || s->low_delay){ | |
2100 *pict= *(AVFrame*)&s->current_picture; | |
2101 ff_print_debug_info(s, pict); | |
2102 } else { | |
2103 *pict= *(AVFrame*)&s->last_picture; | |
2104 if(pict) | |
2105 ff_print_debug_info(s, pict); | |
2106 } | |
2107 | |
2108 /* Return the Picture timestamp as the frame number */ | |
2109 /* we substract 1 because it is added on utils.c */ | |
2110 avctx->frame_number = s->picture_number - 1; | |
2111 | |
2112 /* dont output the last pic after seeking */ | |
2113 if(s->last_picture_ptr || s->low_delay) | |
2114 *data_size = sizeof(AVFrame); | |
2115 | |
1960 av_log(avctx, AV_LOG_DEBUG, "Consumed %i/%i bits\n", | 2116 av_log(avctx, AV_LOG_DEBUG, "Consumed %i/%i bits\n", |
1961 get_bits_count(&v->gb), buf_size*8); | 2117 get_bits_count(&s->gb), buf_size*8); |
1962 | 2118 |
1963 /* Fake consumption of all data */ | 2119 /* Fake consumption of all data */ |
1964 *data_size = len; | 2120 *data_size = len; |
1965 return buf_size; //Number of bytes consumed | 2121 return buf_size; //Number of bytes consumed |
1966 } | 2122 } |
1971 | 2127 |
1972 #if HAS_ADVANCED_PROFILE | 2128 #if HAS_ADVANCED_PROFILE |
1973 av_freep(&v->hrd_rate); | 2129 av_freep(&v->hrd_rate); |
1974 av_freep(&v->hrd_buffer); | 2130 av_freep(&v->hrd_buffer); |
1975 #endif | 2131 #endif |
1976 av_freep(&v->mv_type_mb_plane); | 2132 MPV_common_end(&v->s); |
1977 av_freep(&v->skip_mb_plane); | 2133 free_bitplane(&v->mv_type_mb_plane); |
1978 av_freep(&v->direct_mb_plane); | 2134 free_bitplane(&v->skip_mb_plane); |
2135 free_bitplane(&v->direct_mb_plane); | |
1979 return 0; | 2136 return 0; |
1980 } | 2137 } |
1981 | 2138 |
1982 AVCodec vc9_decoder = { | 2139 AVCodec vc9_decoder = { |
1983 "vc9", | 2140 "vc9", |