1 /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
2 */
3 
4 /* getdata.c  scalefactor & huffman data extraction
5  *
6  * Created by: tomislav uzelac  Apr 1996
7  * Last modified by: tomislav uzelac  Feb 27 1997
8  */
9 #include "amp.h"
10 #include "audio.h"
11 #include "getbits.h"
12 #include "huffman.h"
13 
14 #define		GETDATA
15 #include "getdata.h"
16 
17 /* layer3 scalefactor decoding. should we check for the number
18  * of bits read, just in case?
19  */
decode_scalefactors(struct SIDE_INFO * info,struct AUDIO_HEADER * header,int gr,int ch)20 int decode_scalefactors(struct SIDE_INFO *info,struct AUDIO_HEADER *header,int gr,int ch)
21 {
22 int sfb,window;
23 int slen1,slen2;
24 int i1,i2,i=0;
25 int j,k;
26 	if (header->ID==1) {
27 	/* this is MPEG-1 scalefactors format, quite different than
28 	 * the MPEG-2 format.
29 	 */
30 		slen1=t_slen1[info->scalefac_compress[gr][ch]];
31 		slen2=t_slen2[info->scalefac_compress[gr][ch]];
32 		i1=3*slen1;
33 		i2=3*slen2;
34 
35 		if (info->window_switching_flag[gr][ch] && info->block_type[gr][ch]==2)
36 		{
37 			if (info->mixed_block_flag[gr][ch])
38 			{
39 				for (sfb=0;sfb<8;sfb++)
40 				{
41 					scalefac_l[gr][ch][sfb]=getbits(slen1);
42 					i+=slen1;
43 				}
44 				for (sfb=3;sfb<6;sfb++)
45 				{
46 					for (window=0;window<3;window++)
47 						scalefac_s[gr][ch][sfb][window]=getbits(slen1);
48 					i+=i1;
49 				}
50 				for (;sfb<12;sfb++)
51 				{
52 					for (window=0;window<3;window++)
53 						scalefac_s[gr][ch][sfb][window]=getbits(slen2);
54 					i+=i2;
55 				}
56 			}
57 			else
58 			{ /* !mixed_block_flag */
59 				for (sfb=0;sfb<6;sfb++)
60 				{
61 					for (window=0;window<3;window++)
62 						scalefac_s[gr][ch][sfb][window]=getbits(slen1);
63 					i+=i1;
64 				}
65 				for (;sfb<12;sfb++)
66 				{
67 					for (window=0;window<3;window++)
68 						scalefac_s[gr][ch][sfb][window]=getbits(slen2);
69 					i+=i2;
70 				}
71 			}
72 			for (window=0;window<3;window++)
73 				scalefac_s[gr][ch][12][window]=0;
74 		}
75 		else
76 		{ /* block_type!=2 */
77 			if ( !info->scfsi[ch][0] || !gr )
78 			{
79 				for (sfb=0;sfb<6;sfb++)
80 				{
81 					scalefac_l[gr][ch][sfb]=getbits(slen1);
82 					i+=slen1;
83 				}
84 			}
85 			else for (sfb=0;sfb<6;sfb++)
86 			{
87 				scalefac_l[1][ch][sfb]=scalefac_l[0][ch][sfb];
88 			}
89 			if ( !info->scfsi[ch][1] || !gr )
90 			{
91 				for (sfb=6;sfb<11;sfb++)
92 				{
93 					scalefac_l[gr][ch][sfb]=getbits(slen1);
94 					i+=slen1;
95 				}
96 			}
97 			else for (sfb=6;sfb<11;sfb++)
98 			{
99 				scalefac_l[1][ch][sfb]=scalefac_l[0][ch][sfb];
100 			}
101 			if ( !info->scfsi[ch][2] || !gr )
102 			{
103 				for (sfb=11;sfb<16;sfb++)
104 				{
105 					scalefac_l[gr][ch][sfb]=getbits(slen2);
106 					i+=slen2;
107 				}
108 			}
109 			else for (sfb=11;sfb<16;sfb++)
110 			{
111 				scalefac_l[1][ch][sfb]=scalefac_l[0][ch][sfb];
112 			}
113 			if ( !info->scfsi[ch][3] || !gr )
114 			{
115 				for (sfb=16;sfb<21;sfb++)
116 				{
117 					scalefac_l[gr][ch][sfb]=getbits(slen2);
118 					i+=slen2;
119 				}
120 			}
121 			else for (sfb=16;sfb<21;sfb++)
122 			{
123 				scalefac_l[1][ch][sfb]=scalefac_l[0][ch][sfb];
124 			}
125 			scalefac_l[gr][ch][21]=0;
126 		}
127 	}
128 	else
129 	{ /* ID==0 */
130 		int index=0,index2,spooky_index;
131 		int slen[5],nr_of_sfb[5]; /* actually, there's four of each, not five, labelled 1 through 4, but
132 					   * what's a word of storage compared to one's sanity. so [0] is irellevant.
133 					   */
134 
135 		/* ok, so we got 3 indexes.
136 		 * spooky_index - indicates whether we use the normal set of slen eqs and nr_of_sfb tables
137 		 *                or the one for the right channel of intensity stereo coded frame
138 		 * index        - corresponds to the value of scalefac_compress, as listed in the standard
139 		 * index2	- 0 for long blocks, 1 for short wo/ mixed_block_flag, 2 for short with it
140 		 */
141 		if ( (header->mode_extension==1 || header->mode_extension==3) && ch==1)
142 		{ /* right ch... */
143 			int int_scalefac_compress=info->scalefac_compress[0][ch]>>1;
144 			intensity_scale=info->scalefac_compress[0][1]&1;
145 			spooky_index=1;
146 			if (int_scalefac_compress < 180)
147 			{
148 				slen[1]=int_scalefac_compress/36;
149 				slen[2]=(int_scalefac_compress%36)/6;
150 				slen[3]=(int_scalefac_compress%36)%6;
151 				slen[4]=0;
152 				info->preflag[0][ch]=0;
153 				index=0;
154 			}
155 			if ( 180 <= int_scalefac_compress && int_scalefac_compress < 244)
156 			{
157 				slen[1]=((int_scalefac_compress-180)%64)>>4;
158 				slen[2]=((int_scalefac_compress-180)%16)>>2;
159 				slen[3]=(int_scalefac_compress-180)%4;
160 				slen[4]=0;
161 				info->preflag[0][ch]=0;
162 				index=1;
163 			}
164 			if ( 244 <= int_scalefac_compress && int_scalefac_compress < 255)
165 			{
166 				slen[1]=(int_scalefac_compress-244)/3;
167 				slen[2]=(int_scalefac_compress-244)%3;
168 				slen[3]=0;
169 				slen[4]=0;
170 				info->preflag[0][ch]=0;
171 				index=2;
172 			}
173 		}
174 		else
175 		{ /* the usual */
176 			spooky_index=0;
177 			if (info->scalefac_compress[0][ch] < 400)
178 			{
179 				slen[1]=(info->scalefac_compress[0][ch]>>4)/5;
180 				slen[2]=(info->scalefac_compress[0][ch]>>4)%5;
181 				slen[3]=(info->scalefac_compress[0][ch]%16)>>2;
182 				slen[4]=info->scalefac_compress[0][ch]%4;
183 				info->preflag[0][ch]=0;
184 				index=0;
185 			}
186 			if (info->scalefac_compress[0][ch] >= 400 && info->scalefac_compress[0][ch] < 500)
187 			{
188 				slen[1]=((info->scalefac_compress[0][ch]-400)>>2)/5;
189 				slen[2]=((info->scalefac_compress[0][ch]-400)>>2)%5;
190 				slen[3]=(info->scalefac_compress[0][ch]-400)%4;
191 				slen[4]=0;
192 				info->preflag[0][ch]=0;
193 				index=1;
194 			}
195 			if (info->scalefac_compress[0][ch] >= 500 && info->scalefac_compress[0][ch] < 512)
196 			{
197 				slen[1]=(info->scalefac_compress[0][ch]-500)/3;
198 				slen[2]=(info->scalefac_compress[0][ch]-500)%3;
199 				slen[3]=0;
200 				slen[4]=0;
201 				info->preflag[0][ch]=1;
202 				index=2;
203 			}
204 		}
205 
206 		if (info->window_switching_flag[0][ch] && info->block_type[0][ch]==2)
207 		{
208 			if (info->mixed_block_flag[0][ch])
209 				index2=2;
210 			else
211 				index2=1;
212 		}
213 		else
214 			index2=0;
215 
216 		for (j=1;j<=4;j++) nr_of_sfb[j]=spooky_table[spooky_index][index][index2][j-1];
217 
218 	/* now we'll do some actual scalefactor extraction, and a little more.
219 	 * for each scalefactor band we'll set the value of is_max to indicate
220 	 * illegal is_pos, since with MPEG2 it's not 'hardcoded' to 7.
221 	 */
222 		if (!info->window_switching_flag[0][ch] || (info->window_switching_flag[0][ch] && info->block_type[0][ch]!=2))
223 		{
224 			sfb=0;
225 			for (j=1;j<=4;j++)
226 				for (k=0;k<nr_of_sfb[j];k++)
227 				{
228 					scalefac_l[0][ch][sfb]=getbits(slen[j]);
229 					i+=slen[j];
230 					if (ch) is_max[sfb]=(1<<slen[j])-1;
231 					sfb++;
232 				}
233 		}
234 		else if (info->block_type[0][ch]==2)
235 		{
236 			if (!info->mixed_block_flag[0][ch])
237 			{
238 				sfb=0;
239 				for (j=1;j<=4;j++)
240 					for (k=0;k<nr_of_sfb[j];k+=3)
241 					{
242 						/* we assume here that nr_of_sfb is divisible by 3. it is.
243 						 */
244 						scalefac_s[0][ch][sfb][0]=getbits(slen[j]);
245 						scalefac_s[0][ch][sfb][1]=getbits(slen[j]);
246 						scalefac_s[0][ch][sfb][2]=getbits(slen[j]);
247 						i+=3*slen[j];
248 						if (ch) is_max[sfb+6]=(1<<slen[j])-1;
249 						sfb++;
250 					}
251 			}
252 			else
253 			{
254 				/* what we do here is:
255 				 * 1. assume that for every fs, the two lowest subbands are equal to the
256 				 *    six lowest scalefactor bands for long blocks/MPEG2. they are.
257 				 * 2. assume that for every fs, the two lowest subbands are equal to the
258 				 *    three lowest scalefactor bands for short blocks. they are.
259 				 */
260 				sfb=0;
261 				for (k=0;k<6;k++)
262 				{
263 					scalefac_l[0][ch][sfb]=getbits(slen[1]);
264 					i+=slen[j];
265 					if (ch) is_max[sfb]=(1<<slen[1])-1;
266 					sfb++;
267 				}
268 				nr_of_sfb[1]-=6;
269 				sfb=3;
270 				for (j=1;j<=4;j++)
271 					for (k=0;k<nr_of_sfb[j];k+=3)
272 					{
273 						scalefac_s[0][ch][sfb][0]=getbits(slen[j]);
274 						scalefac_s[0][ch][sfb][1]=getbits(slen[j]);
275 						scalefac_s[0][ch][sfb][2]=getbits(slen[j]);
276 						i+=3*slen[j];
277 						if (ch) is_max[sfb+6]=(1<<slen[j])-1;
278 						sfb++;
279 					}
280 			}
281 		}
282 	}
283 return i;
284 }
285