Subversion Repositories Kolibri OS

Rev

Rev 1905 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1905 Rev 3960
Line 1... Line 1...
1
/*
1
/*
2
	leyer3.c: the layer 3 decoder
2
	layer3.c: the layer 3 decoder
Line 3... Line 3...
3
 
3
 
4
	copyright 1995-2008 by the mpg123 project - free software under the terms of the LGPL 2.1
4
	copyright 1995-2009 by the mpg123 project - free software under the terms of the LGPL 2.1
5
	see COPYING and AUTHORS files in distribution or http://mpg123.org
5
	see COPYING and AUTHORS files in distribution or http://mpg123.org
Line 6... Line 6...
6
	initially written by Michael Hipp
6
	initially written by Michael Hipp
7
 
7
 
Line 54... Line 54...
54
	unsigned big_values;
54
	unsigned big_values;
55
	unsigned scalefac_compress;
55
	unsigned scalefac_compress;
56
	unsigned block_type;
56
	unsigned block_type;
57
	unsigned mixed_block_flag;
57
	unsigned mixed_block_flag;
58
	unsigned table_select[3];
58
	unsigned table_select[3];
59
	unsigned subblock_gain[3];
59
	/* Making those two signed int as workaround for open64/pathscale/sun compilers, and also for consistency, since they're worked on together with other signed variables. */
60
	unsigned maxband[3];
60
	int maxband[3];
61
	unsigned maxbandl;
61
	int maxbandl;
62
	unsigned maxb;
62
	unsigned maxb;
63
	unsigned region1start;
63
	unsigned region1start;
64
	unsigned region2start;
64
	unsigned region2start;
65
	unsigned preflag;
65
	unsigned preflag;
66
	unsigned scalefac_scale;
66
	unsigned scalefac_scale;
Line 77... Line 77...
77
	struct { struct gr_info_s gr[2]; } ch[2];
77
	struct { struct gr_info_s gr[2]; } ch[2];
78
};
78
};
Line 79... Line 79...
79
 
79
 
80
struct bandInfoStruct
80
struct bandInfoStruct
81
{
81
{
82
	int longIdx[23];
82
	unsigned short longIdx[23];
83
	int longDiff[22];
83
	unsigned char longDiff[22];
84
	int shortIdx[14];
84
	unsigned short shortIdx[14];
85
	int shortDiff[13];
85
	unsigned char shortDiff[13];
Line 86... Line 86...
86
};
86
};
87
 
87
 
88
/* Techy details about our friendly MPEG data. Fairly constant over the years;-) */
88
/* Techy details about our friendly MPEG data. Fairly constant over the years;-) */
89
const struct bandInfoStruct bandInfo[9] =
89
static const struct bandInfoStruct bandInfo[9] =
90
{
90
{
91
	{ /* MPEG 1.0 */
91
	{ /* MPEG 1.0 */
92
		{0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
92
		{0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
Line 268... Line 268...
268
	for(j=0;j<9;j++)
268
	for(j=0;j<9;j++)
269
	{
269
	{
270
		const struct bandInfoStruct *bi = &bandInfo[j];
270
		const struct bandInfoStruct *bi = &bandInfo[j];
271
		int *mp;
271
		int *mp;
272
		int cb,lwin;
272
		int cb,lwin;
273
		const int *bdf;
273
		const unsigned char *bdf;
Line 274... Line 274...
274
 
274
 
275
		mp = map[j][0] = mapbuf0[j];
275
		mp = map[j][0] = mapbuf0[j];
276
		bdf = bi->longDiff;
276
		bdf = bi->longDiff;
277
		for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++)
277
		for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++)
Line 401... Line 401...
401
 
401
 
Line 402... Line 402...
402
	si->main_data_begin = getbits(fr, tab[1]);
402
	si->main_data_begin = getbits(fr, tab[1]);
403
 
403
 
404
	if(si->main_data_begin > fr->bitreservoir)
404
	if(si->main_data_begin > fr->bitreservoir)
Line 405... Line 405...
405
	{
405
	{
406
		if(VERBOSE2) fprintf(stderr, "Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num);
406
		if(!fr->to_ignore && VERBOSE2) fprintf(stderr, "Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num);
407
 
407
 
408
		/*  overwrite main_data_begin for the really available bit reservoir */
408
		/*  overwrite main_data_begin for the really available bit reservoir */
Line 449... Line 449...
449
 
449
 
450
		gr_info->part2_3_length = getbits(fr, 12);
450
		gr_info->part2_3_length = getbits(fr, 12);
451
		gr_info->big_values = getbits(fr, 9);
451
		gr_info->big_values = getbits(fr, 9);
452
		if(gr_info->big_values > 288)
452
		if(gr_info->big_values > 288)
453
		{
453
		{
454
			error("big_values too large!");
454
			if(NOQUIET) error("big_values too large!");
455
			gr_info->big_values = 288;
455
			gr_info->big_values = 288;
456
		}
456
		}
457
		gr_info->pow2gain = fr->gainpow2+256 - getbits_fast(fr, 8) + powdiff;
457
		gr_info->pow2gain = fr->gainpow2+256 - getbits_fast(fr, 8) + powdiff;
Line 474... Line 474...
474
			for(i=0;i<3;i++)
474
			for(i=0;i<3;i++)
475
			gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(fr, 3)<<3);
475
			gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(fr, 3)<<3);
Line 476... Line 476...
476
 
476
 
477
			if(gr_info->block_type == 0)
477
			if(gr_info->block_type == 0)
478
			{
478
			{
479
				error("Blocktype == 0 and window-switching == 1 not allowed.");
479
				if(NOQUIET) error("Blocktype == 0 and window-switching == 1 not allowed.");
480
				return 1;
480
				return 1;
Line 481... Line 481...
481
			}
481
			}
482
 
482
 
Line 492... Line 492...
492
				{ 
492
				{ 
493
					int r0c,r1c;
493
					int r0c,r1c;
494
					if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5;
494
					if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5;
495
					else r0c = 7;
495
					else r0c = 7;
Line -... Line 496...
-
 
496
 
496
 
497
					/* r0c+1+r1c+1 == 22, always. */
497
					r1c = 20 - r0c;
498
					r1c = 20 - r0c;
498
					gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
499
					gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
499
					gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; 
500
					gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1; 
500
				}
501
				}
Line 509... Line 510...
509
		{
510
		{
510
			int i,r0c,r1c;
511
			int i,r0c,r1c;
511
			for (i=0; i<3; i++)
512
			for (i=0; i<3; i++)
512
			gr_info->table_select[i] = getbits_fast(fr, 5);
513
			gr_info->table_select[i] = getbits_fast(fr, 5);
Line 513... Line 514...
513
 
514
 
514
			r0c = getbits_fast(fr, 4);
515
			r0c = getbits_fast(fr, 4); /* 0 .. 15 */
515
			r1c = getbits_fast(fr, 3);
516
			r1c = getbits_fast(fr, 3); /* 0 .. 7 */
516
			gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
-
 
Line -... Line 517...
-
 
517
			gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
517
			gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
518
 
518
 
519
			/* max(r0c+r1c+2) = 15+7+2 = 24 */
Line 519... Line 520...
519
			if(r0c + r1c + 2 > 22) gr_info->region2start = 576>>1;
520
			if(r0c+1+r1c+1 > 22) gr_info->region2start = 576>>1;
520
			else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
521
			else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
521
 
522
 
Line 675... Line 676...
675
	for(i=0;i
676
	for(i=0;i
Line 676... Line 677...
676
 
677
 
677
	return numbits;
678
	return numbits;
Line -... Line 679...
-
 
679
}
-
 
680
 
678
}
681
static unsigned char pretab_choice[2][22] =
679
 
682
{
-
 
683
	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
Line 680... Line 684...
680
static const int pretab1[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
684
	{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0}
681
static const int pretab2[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
685
};
682
 
686
 
683
/*
687
/*
Line 714... Line 718...
714
 
718
 
715
	{
719
	{
716
		int bv       = gr_info->big_values;
720
		int bv       = gr_info->big_values;
717
		int region1  = gr_info->region1start;
721
		int region1  = gr_info->region1start;
718
		int region2  = gr_info->region2start;
-
 
719
		if(region1 > region2)
-
 
720
		{
-
 
721
			/*
-
 
722
				That's not optimal: it fixes a segfault with fuzzed data, but also apparently triggers where it shouldn't, see bug 1641196.
-
 
723
				The benefit of not crashing / having this security risk is bigger than these few frames of a lame-3.70 file that aren't audible anyway.
-
 
724
				But still, I want to know if indeed this check or the old lame is at fault.
-
 
725
			*/
-
 
726
			error("You got some really nasty file there... region1>region2!");
-
 
727
			return 1;
-
 
728
		}
722
		int region2  = gr_info->region2start;
Line 729... Line 723...
729
		l3 = ((576>>1)-bv)>>1;   
723
		l3 = ((576>>1)-bv)>>1;   
730
 
724
 
731
		/* we may lose the 'odd' bit here !! check this later again */
725
		/* we may lose the 'odd' bit here !! check this later again */
Line 776... Line 770...
776
 
770
 
777
		mc = 0;
771
		mc = 0;
778
		for(i=0;i<2;i++)
772
		for(i=0;i<2;i++)
779
		{
773
		{
780
			int lp = l[i];
774
			int lp = l[i];
781
			struct newhuff *h = ht+gr_info->table_select[i];
775
			const struct newhuff *h = ht+gr_info->table_select[i];
782
			for(;lp;lp--,mc--)
776
			for(;lp;lp--,mc--)
783
			{
777
			{
784
				register int x,y;
778
				register int x,y;
785
				if( (!mc) )
779
				if( (!mc) )
Line 804... Line 798...
804
						v = gr_info->full_gain[lwin][(*scf++) << shift];
798
						v = gr_info->full_gain[lwin][(*scf++) << shift];
805
						step = 3;
799
						step = 3;
806
					}
800
					}
807
				}
801
				}
808
				{
802
				{
809
					register short *val = h->table;
803
					const short *val = h->table;
810
					REFRESH_MASK;
804
					REFRESH_MASK;
811
					while((y=*val++)<0)
805
					while((y=*val++)<0)
812
					{
806
					{
813
						if (mask < 0) val -= y;
807
						if (mask < 0) val -= y;
Line 869... Line 863...
869
			}
863
			}
870
		}
864
		}
Line 871... Line 865...
871
 
865
 
872
		for(;l3 && (part2remain+num > 0);l3--)
866
		for(;l3 && (part2remain+num > 0);l3--)
873
		{
867
		{
874
			struct newhuff* h;
868
			const struct newhuff* h;
875
			register short* val;
869
			const short* val;
876
			register short a;
870
			register short a;
877
			/*
871
			/*
878
				This is only a humble hack to prevent a special segfault.
872
				This is only a humble hack to prevent a special segfault.
879
				More insight into the real workings is still needed.
873
				More insight into the real workings is still needed.
Line 988... Line 982...
988
 
982
 
989
	}
983
	}
990
	else
984
	else
991
	{
985
	{
992
		/* decoding with 'long' BandIndex table (block_type != 2) */
986
		/* decoding with 'long' BandIndex table (block_type != 2) */
993
		const int *pretab = gr_info->preflag ? pretab1 : pretab2;
987
		const unsigned char *pretab = pretab_choice[gr_info->preflag];
994
		int i,max = -1;
988
		int i,max = -1;
995
		int cb = 0;
989
		int cb = 0;
996
		int *m = map[sfreq][2];
990
		int *m = map[sfreq][2];
997
		register real v = 0.0;
991
		register real v = 0.0;
Line 998... Line 992...
998
		int mc = 0;
992
		int mc = 0;
999
 
993
 
1000
		/* long hash table values */
994
		/* long hash table values */
1001
		for(i=0;i<3;i++)
995
		for(i=0;i<3;i++)
1002
		{
996
		{
Line 1003... Line 997...
1003
			int lp = l[i];
997
			int lp = l[i];
1004
			struct newhuff *h = ht+gr_info->table_select[i];
998
			const struct newhuff *h = ht+gr_info->table_select[i];
1005
 
999
 
1006
			for(;lp;lp--,mc--)
1000
			for(;lp;lp--,mc--)
Line 1021... Line 1015...
1021
#endif
1015
#endif
1022
						v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift];
1016
						v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift];
1023
					}
1017
					}
1024
				}
1018
				}
1025
				{
1019
				{
1026
					register short *val = h->table;
1020
					const short *val = h->table;
1027
					REFRESH_MASK;
1021
					REFRESH_MASK;
1028
					while((y=*val++)<0)
1022
					while((y=*val++)<0)
1029
					{
1023
					{
1030
						if (mask < 0) val -= y;
1024
						if (mask < 0) val -= y;
Line 1085... Line 1079...
1085
		}
1079
		}
Line 1086... Line 1080...
1086
 
1080
 
1087
		/* short (count1table) values */
1081
		/* short (count1table) values */
1088
		for(;l3 && (part2remain+num > 0);l3--)
1082
		for(;l3 && (part2remain+num > 0);l3--)
1089
		{
1083
		{
1090
			struct newhuff *h = htc+gr_info->count1table_select;
1084
			const struct newhuff *h = htc+gr_info->count1table_select;
-
 
1085
			const short *val = h->table;
Line 1091... Line 1086...
1091
			register short *val = h->table,a;
1086
			register short a;
1092
 
1087
 
1093
			REFRESH_MASK;
1088
			REFRESH_MASK;
1094
			while((a=*val++)<0)
1089
			while((a=*val++)<0)
Line 1931... Line 1926...
1931
 
1926
 
Line 1932... Line 1927...
1932
	set_pointer(fr,sideinfo.main_data_begin);
1927
	set_pointer(fr,sideinfo.main_data_begin);
1933
 
1928
 
1934
	for(gr=0;gr
1929
	for(gr=0;gr
-
 
1930
	{
1935
	{
1931
		/*  hybridIn[2][SBLIMIT][SSLIMIT] */
-
 
1932
		real (*hybridIn)[SBLIMIT][SSLIMIT] = fr->layer3.hybrid_in;
Line 1936... Line 1933...
1936
		ALIGNED(16) real hybridIn[2][SBLIMIT][SSLIMIT];
1933
		/*  hybridOut[2][SSLIMIT][SBLIMIT] */
1937
		ALIGNED(16) real hybridOut[2][SSLIMIT][SBLIMIT];
1934
		real (*hybridOut)[SSLIMIT][SBLIMIT] = fr->layer3.hybrid_out;
1938
 
1935
 
1939
		{
1936
		{