1 /*
2     MPEG Maaate: An Australian MPEG audio analysis toolkit
3     Copyright (C) 2000 Commonwealth Scientific and Industrial Research Organisation
4     (CSIRO), Australia.
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include "layer2.H"
26 
27 // bit allocation tables from B.2 ISO/IEC 11172-3
28 // Table alloc_0 with 27 subbands,
29 static const unsigned short maaate_alloc_0[] = {
30   0, 0, 0, 4, 0, 0,
31   0, 1, 3, 5, 1, 0,
32   0, 2, 7, 3, 3, 2,
33   0, 3, 15, 4, 3, 4,
34   0, 4, 31, 5, 3, 5,
35   0, 5, 63, 6, 3, 6,
36   0, 6, 127, 7, 3, 7,
37   0, 7, 255, 8, 3, 8,
38   0, 8, 511, 9, 3, 9,
39   0, 9, 1023, 10, 3, 10,
40   0, 10, 2047, 11, 3, 11,
41   0, 11, 4095, 12, 3, 12,
42   0, 12, 8191, 13, 3, 13,
43   0, 13, 16383, 14, 3, 14,
44   0, 14, 32767, 15, 3, 15,
45   0, 15, 65535, 16, 3, 16,
46   1, 0, 0, 4, 0, 0,
47   1, 1, 3, 5, 1, 0,
48   1, 2, 7, 3, 3, 2,
49   1, 3, 15, 4, 3, 4,
50   1, 4, 31, 5, 3, 5,
51   1, 5, 63, 6, 3, 6,
52   1, 6, 127, 7, 3, 7,
53   1, 7, 255, 8, 3, 8,
54   1, 8, 511, 9, 3, 9,
55   1, 9, 1023, 10, 3, 10,
56   1, 10, 2047, 11, 3, 11,
57   1, 11, 4095, 12, 3, 12,
58   1, 12, 8191, 13, 3, 13,
59   1, 13, 16383, 14, 3, 14,
60   1, 14, 32767, 15, 3, 15,
61   1, 15, 65535, 16, 3, 16,
62   2, 0, 0, 4, 0, 0,
63   2, 1, 3, 5, 1, 0,
64   2, 2, 7, 3, 3, 2,
65   2, 3, 15, 4, 3, 4,
66   2, 4, 31, 5, 3, 5,
67   2, 5, 63, 6, 3, 6,
68   2, 6, 127, 7, 3, 7,
69   2, 7, 255, 8, 3, 8,
70   2, 8, 511, 9, 3, 9,
71   2, 9, 1023, 10, 3, 10,
72   2, 10, 2047, 11, 3, 11,
73   2, 11, 4095, 12, 3, 12,
74   2, 12, 8191, 13, 3, 13,
75   2, 13, 16383, 14, 3, 14,
76   2, 14, 32767, 15, 3, 15,
77   2, 15, 65535, 16, 3, 16,
78   3, 0, 0, 4, 0, 0,
79   3, 1, 3, 5, 1, 0,
80   3, 2, 5, 7, 1, 1,
81   3, 3, 7, 3, 3, 2,
82   3, 4, 9, 10, 1, 3,
83   3, 5, 15, 4, 3, 4,
84   3, 6, 31, 5, 3, 5,
85   3, 7, 63, 6, 3, 6,
86   3, 8, 127, 7, 3, 7,
87   3, 9, 255, 8, 3, 8,
88   3, 10, 511, 9, 3, 9,
89   3, 11, 1023, 10, 3, 10,
90   3, 12, 2047, 11, 3, 11,
91   3, 13, 4095, 12, 3, 12,
92   3, 14, 8191, 13, 3, 13,
93   3, 15, 65535, 16, 3, 16,
94   4, 0, 0, 4, 0, 0,
95   4, 1, 3, 5, 1, 0,
96   4, 2, 5, 7, 1, 1,
97   4, 3, 7, 3, 3, 2,
98   4, 4, 9, 10, 1, 3,
99   4, 5, 15, 4, 3, 4,
100   4, 6, 31, 5, 3, 5,
101   4, 7, 63, 6, 3, 6,
102   4, 8, 127, 7, 3, 7,
103   4, 9, 255, 8, 3, 8,
104   4, 10, 511, 9, 3, 9,
105   4, 11, 1023, 10, 3, 10,
106   4, 12, 2047, 11, 3, 11,
107   4, 13, 4095, 12, 3, 12,
108   4, 14, 8191, 13, 3, 13,
109   4, 15, 65535, 16, 3, 16,
110   5, 0, 0, 4, 0, 0,
111   5, 1, 3, 5, 1, 0,
112   5, 2, 5, 7, 1, 1,
113   5, 3, 7, 3, 3, 2,
114   5, 4, 9, 10, 1, 3,
115   5, 5, 15, 4, 3, 4,
116   5, 6, 31, 5, 3, 5,
117   5, 7, 63, 6, 3, 6,
118   5, 8, 127, 7, 3, 7,
119   5, 9, 255, 8, 3, 8,
120   5, 10, 511, 9, 3, 9,
121   5, 11, 1023, 10, 3, 10,
122   5, 12, 2047, 11, 3, 11,
123   5, 13, 4095, 12, 3, 12,
124   5, 14, 8191, 13, 3, 13,
125   5, 15, 65535, 16, 3, 16,
126   6, 0, 0, 4, 0, 0,
127   6, 1, 3, 5, 1, 0,
128   6, 2, 5, 7, 1, 1,
129   6, 3, 7, 3, 3, 2,
130   6, 4, 9, 10, 1, 3,
131   6, 5, 15, 4, 3, 4,
132   6, 6, 31, 5, 3, 5,
133   6, 7, 63, 6, 3, 6,
134   6, 8, 127, 7, 3, 7,
135   6, 9, 255, 8, 3, 8,
136   6, 10, 511, 9, 3, 9,
137   6, 11, 1023, 10, 3, 10,
138   6, 12, 2047, 11, 3, 11,
139   6, 13, 4095, 12, 3, 12,
140   6, 14, 8191, 13, 3, 13,
141   6, 15, 65535, 16, 3, 16,
142   7, 0, 0, 4, 0, 0,
143   7, 1, 3, 5, 1, 0,
144   7, 2, 5, 7, 1, 1,
145   7, 3, 7, 3, 3, 2,
146   7, 4, 9, 10, 1, 3,
147   7, 5, 15, 4, 3, 4,
148   7, 6, 31, 5, 3, 5,
149   7, 7, 63, 6, 3, 6,
150   7, 8, 127, 7, 3, 7,
151   7, 9, 255, 8, 3, 8,
152   7, 10, 511, 9, 3, 9,
153   7, 11, 1023, 10, 3, 10,
154   7, 12, 2047, 11, 3, 11,
155   7, 13, 4095, 12, 3, 12,
156   7, 14, 8191, 13, 3, 13,
157   7, 15, 65535, 16, 3, 16,
158   8, 0, 0, 4, 0, 0,
159   8, 1, 3, 5, 1, 0,
160   8, 2, 5, 7, 1, 1,
161   8, 3, 7, 3, 3, 2,
162   8, 4, 9, 10, 1, 3,
163   8, 5, 15, 4, 3, 4,
164   8, 6, 31, 5, 3, 5,
165   8, 7, 63, 6, 3, 6,
166   8, 8, 127, 7, 3, 7,
167   8, 9, 255, 8, 3, 8,
168   8, 10, 511, 9, 3, 9,
169   8, 11, 1023, 10, 3, 10,
170   8, 12, 2047, 11, 3, 11,
171   8, 13, 4095, 12, 3, 12,
172   8, 14, 8191, 13, 3, 13,
173   8, 15, 65535, 16, 3, 16,
174   9, 0, 0, 4, 0, 0,
175   9, 1, 3, 5, 1, 0,
176   9, 2, 5, 7, 1, 1,
177   9, 3, 7, 3, 3, 2,
178   9, 4, 9, 10, 1, 3,
179   9, 5, 15, 4, 3, 4,
180   9, 6, 31, 5, 3, 5,
181   9, 7, 63, 6, 3, 6,
182   9, 8, 127, 7, 3, 7,
183   9, 9, 255, 8, 3, 8,
184   9, 10, 511, 9, 3, 9,
185   9, 11, 1023, 10, 3, 10,
186   9, 12, 2047, 11, 3, 11,
187   9, 13, 4095, 12, 3, 12,
188   9, 14, 8191, 13, 3, 13,
189   9, 15, 65535, 16, 3, 16,
190   10, 0, 0, 4, 0, 0,
191   10, 1, 3, 5, 1, 0,
192   10, 2, 5, 7, 1, 1,
193   10, 3, 7, 3, 3, 2,
194   10, 4, 9, 10, 1, 3,
195   10, 5, 15, 4, 3, 4,
196   10, 6, 31, 5, 3, 5,
197   10, 7, 63, 6, 3, 6,
198   10, 8, 127, 7, 3, 7,
199   10, 9, 255, 8, 3, 8,
200   10, 10, 511, 9, 3, 9,
201   10, 11, 1023, 10, 3, 10,
202   10, 12, 2047, 11, 3, 11,
203   10, 13, 4095, 12, 3, 12,
204   10, 14, 8191, 13, 3, 13,
205   10, 15, 65535, 16, 3, 16,
206   11, 0, 0, 3, 0, 0,
207   11, 1, 3, 5, 1, 0,
208   11, 2, 5, 7, 1, 1,
209   11, 3, 7, 3, 3, 2,
210   11, 4, 9, 10, 1, 3,
211   11, 5, 15, 4, 3, 4,
212   11, 6, 31, 5, 3, 5,
213   11, 7, 65535, 16, 3, 16,
214   12, 0, 0, 3, 0, 0,
215   12, 1, 3, 5, 1, 0,
216   12, 2, 5, 7, 1, 1,
217   12, 3, 7, 3, 3, 2,
218   12, 4, 9, 10, 1, 3,
219   12, 5, 15, 4, 3, 4,
220   12, 6, 31, 5, 3, 5,
221   12, 7, 65535, 16, 3, 16,
222   13, 0, 0, 3, 0, 0,
223   13, 1, 3, 5, 1, 0,
224   13, 2, 5, 7, 1, 1,
225   13, 3, 7, 3, 3, 2,
226   13, 4, 9, 10, 1, 3,
227   13, 5, 15, 4, 3, 4,
228   13, 6, 31, 5, 3, 5,
229   13, 7, 65535, 16, 3, 16,
230   14, 0, 0, 3, 0, 0,
231   14, 1, 3, 5, 1, 0,
232   14, 2, 5, 7, 1, 1,
233   14, 3, 7, 3, 3, 2,
234   14, 4, 9, 10, 1, 3,
235   14, 5, 15, 4, 3, 4,
236   14, 6, 31, 5, 3, 5,
237   14, 7, 65535, 16, 3, 16,
238   15, 0, 0, 3, 0, 0,
239   15, 1, 3, 5, 1, 0,
240   15, 2, 5, 7, 1, 1,
241   15, 3, 7, 3, 3, 2,
242   15, 4, 9, 10, 1, 3,
243   15, 5, 15, 4, 3, 4,
244   15, 6, 31, 5, 3, 5,
245   15, 7, 65535, 16, 3, 16,
246   16, 0, 0, 3, 0, 0,
247   16, 1, 3, 5, 1, 0,
248   16, 2, 5, 7, 1, 1,
249   16, 3, 7, 3, 3, 2,
250   16, 4, 9, 10, 1, 3,
251   16, 5, 15, 4, 3, 4,
252   16, 6, 31, 5, 3, 5,
253   16, 7, 65535, 16, 3, 16,
254   17, 0, 0, 3, 0, 0,
255   17, 1, 3, 5, 1, 0,
256   17, 2, 5, 7, 1, 1,
257   17, 3, 7, 3, 3, 2,
258   17, 4, 9, 10, 1, 3,
259   17, 5, 15, 4, 3, 4,
260   17, 6, 31, 5, 3, 5,
261   17, 7, 65535, 16, 3, 16,
262   18, 0, 0, 3, 0, 0,
263   18, 1, 3, 5, 1, 0,
264   18, 2, 5, 7, 1, 1,
265   18, 3, 7, 3, 3, 2,
266   18, 4, 9, 10, 1, 3,
267   18, 5, 15, 4, 3, 4,
268   18, 6, 31, 5, 3, 5,
269   18, 7, 65535, 16, 3, 16,
270   19, 0, 0, 3, 0, 0,
271   19, 1, 3, 5, 1, 0,
272   19, 2, 5, 7, 1, 1,
273   19, 3, 7, 3, 3, 2,
274   19, 4, 9, 10, 1, 3,
275   19, 5, 15, 4, 3, 4,
276   19, 6, 31, 5, 3, 5,
277   19, 7, 65535, 16, 3, 16,
278   20, 0, 0, 3, 0, 0,
279   20, 1, 3, 5, 1, 0,
280   20, 2, 5, 7, 1, 1,
281   20, 3, 7, 3, 3, 2,
282   20, 4, 9, 10, 1, 3,
283   20, 5, 15, 4, 3, 4,
284   20, 6, 31, 5, 3, 5,
285   20, 7, 65535, 16, 3, 16,
286   21, 0, 0, 3, 0, 0,
287   21, 1, 3, 5, 1, 0,
288   21, 2, 5, 7, 1, 1,
289   21, 3, 7, 3, 3, 2,
290   21, 4, 9, 10, 1, 3,
291   21, 5, 15, 4, 3, 4,
292   21, 6, 31, 5, 3, 5,
293   21, 7, 65535, 16, 3, 16,
294   22, 0, 0, 3, 0, 0,
295   22, 1, 3, 5, 1, 0,
296   22, 2, 5, 7, 1, 1,
297   22, 3, 7, 3, 3, 2,
298   22, 4, 9, 10, 1, 3,
299   22, 5, 15, 4, 3, 4,
300   22, 6, 31, 5, 3, 5,
301   22, 7, 65535, 16, 3, 16,
302   23, 0, 0, 2, 0, 0,
303   23, 1, 3, 5, 1, 0,
304   23, 2, 5, 7, 1, 1,
305   23, 3, 65535, 16, 3, 16,
306   24, 0, 0, 2, 0, 0,
307   24, 1, 3, 5, 1, 0,
308   24, 2, 5, 7, 1, 1,
309   24, 3, 65535, 16, 3, 16,
310   25, 0, 0, 2, 0, 0,
311   25, 1, 3, 5, 1, 0,
312   25, 2, 5, 7, 1, 1,
313   25, 3, 65535, 16, 3, 16,
314   26, 0, 0, 2, 0, 0,
315   26, 1, 3, 5, 1, 0,
316   26, 2, 5, 7, 1, 1,
317   26, 3, 65535, 16, 3, 16
318 };
319 
320 // Table alloc_1 with 30 subbands
321 static const unsigned short maaate_alloc_1[] = {
322   0, 0, 0, 4, 0, 0,
323   0, 1, 3, 5, 1, 0,
324   0, 2, 7, 3, 3, 2,
325   0, 3, 15, 4, 3, 4,
326   0, 4, 31, 5, 3, 5,
327   0, 5, 63, 6, 3, 6,
328   0, 6, 127, 7, 3, 7,
329   0, 7, 255, 8, 3, 8,
330   0, 8, 511, 9, 3, 9,
331   0, 9, 1023, 10, 3, 10,
332   0, 10, 2047, 11, 3, 11,
333   0, 11, 4095, 12, 3, 12,
334   0, 12, 8191, 13, 3, 13,
335   0, 13, 16383, 14, 3, 14,
336   0, 14, 32767, 15, 3, 15,
337   0, 15, 65535, 16, 3, 16,
338   1, 0, 0, 4, 0, 0,
339   1, 1, 3, 5, 1, 0,
340   1, 2, 7, 3, 3, 2,
341   1, 3, 15, 4, 3, 4,
342   1, 4, 31, 5, 3, 5,
343   1, 5, 63, 6, 3, 6,
344   1, 6, 127, 7, 3, 7,
345   1, 7, 255, 8, 3, 8,
346   1, 8, 511, 9, 3, 9,
347   1, 9, 1023, 10, 3, 10,
348   1, 10, 2047, 11, 3, 11,
349   1, 11, 4095, 12, 3, 12,
350   1, 12, 8191, 13, 3, 13,
351   1, 13, 16383, 14, 3, 14,
352   1, 14, 32767, 15, 3, 15,
353   1, 15, 65535, 16, 3, 16,
354   2, 0, 0, 4, 0, 0,
355   2, 1, 3, 5, 1, 0,
356   2, 2, 7, 3, 3, 2,
357   2, 3, 15, 4, 3, 4,
358   2, 4, 31, 5, 3, 5,
359   2, 5, 63, 6, 3, 6,
360   2, 6, 127, 7, 3, 7,
361   2, 7, 255, 8, 3, 8,
362   2, 8, 511, 9, 3, 9,
363   2, 9, 1023, 10, 3, 10,
364   2, 10, 2047, 11, 3, 11,
365   2, 11, 4095, 12, 3, 12,
366   2, 12, 8191, 13, 3, 13,
367   2, 13, 16383, 14, 3, 14,
368   2, 14, 32767, 15, 3, 15,
369   2, 15, 65535, 16, 3, 16,
370   3, 0, 0, 4, 0, 0,
371   3, 1, 3, 5, 1, 0,
372   3, 2, 5, 7, 1, 1,
373   3, 3, 7, 3, 3, 2,
374   3, 4, 9, 10, 1, 3,
375   3, 5, 15, 4, 3, 4,
376   3, 6, 31, 5, 3, 5,
377   3, 7, 63, 6, 3, 6,
378   3, 8, 127, 7, 3, 7,
379   3, 9, 255, 8, 3, 8,
380   3, 10, 511, 9, 3, 9,
381   3, 11, 1023, 10, 3, 10,
382   3, 12, 2047, 11, 3, 11,
383   3, 13, 4095, 12, 3, 12,
384   3, 14, 8191, 13, 3, 13,
385   3, 15, 65535, 16, 3, 16,
386   4, 0, 0, 4, 0, 0,
387   4, 1, 3, 5, 1, 0,
388   4, 2, 5, 7, 1, 1,
389   4, 3, 7, 3, 3, 2,
390   4, 4, 9, 10, 1, 3,
391   4, 5, 15, 4, 3, 4,
392   4, 6, 31, 5, 3, 5,
393   4, 7, 63, 6, 3, 6,
394   4, 8, 127, 7, 3, 7,
395   4, 9, 255, 8, 3, 8,
396   4, 10, 511, 9, 3, 9,
397   4, 11, 1023, 10, 3, 10,
398   4, 12, 2047, 11, 3, 11,
399   4, 13, 4095, 12, 3, 12,
400   4, 14, 8191, 13, 3, 13,
401   4, 15, 65535, 16, 3, 16,
402   5, 0, 0, 4, 0, 0,
403   5, 1, 3, 5, 1, 0,
404   5, 2, 5, 7, 1, 1,
405   5, 3, 7, 3, 3, 2,
406   5, 4, 9, 10, 1, 3,
407   5, 5, 15, 4, 3, 4,
408   5, 6, 31, 5, 3, 5,
409   5, 7, 63, 6, 3, 6,
410   5, 8, 127, 7, 3, 7,
411   5, 9, 255, 8, 3, 8,
412   5, 10, 511, 9, 3, 9,
413   5, 11, 1023, 10, 3, 10,
414   5, 12, 2047, 11, 3, 11,
415   5, 13, 4095, 12, 3, 12,
416   5, 14, 8191, 13, 3, 13,
417   5, 15, 65535, 16, 3, 16,
418   6, 0, 0, 4, 0, 0,
419   6, 1, 3, 5, 1, 0,
420   6, 2, 5, 7, 1, 1,
421   6, 3, 7, 3, 3, 2,
422   6, 4, 9, 10, 1, 3,
423   6, 5, 15, 4, 3, 4,
424   6, 6, 31, 5, 3, 5,
425   6, 7, 63, 6, 3, 6,
426   6, 8, 127, 7, 3, 7,
427   6, 9, 255, 8, 3, 8,
428   6, 10, 511, 9, 3, 9,
429   6, 11, 1023, 10, 3, 10,
430   6, 12, 2047, 11, 3, 11,
431   6, 13, 4095, 12, 3, 12,
432   6, 14, 8191, 13, 3, 13,
433   6, 15, 65535, 16, 3, 16,
434   7, 0, 0, 4, 0, 0,
435   7, 1, 3, 5, 1, 0,
436   7, 2, 5, 7, 1, 1,
437   7, 3, 7, 3, 3, 2,
438   7, 4, 9, 10, 1, 3,
439   7, 5, 15, 4, 3, 4,
440   7, 6, 31, 5, 3, 5,
441   7, 7, 63, 6, 3, 6,
442   7, 8, 127, 7, 3, 7,
443   7, 9, 255, 8, 3, 8,
444   7, 10, 511, 9, 3, 9,
445   7, 11, 1023, 10, 3, 10,
446   7, 12, 2047, 11, 3, 11,
447   7, 13, 4095, 12, 3, 12,
448   7, 14, 8191, 13, 3, 13,
449   7, 15, 65535, 16, 3, 16,
450   8, 0, 0, 4, 0, 0,
451   8, 1, 3, 5, 1, 0,
452   8, 2, 5, 7, 1, 1,
453   8, 3, 7, 3, 3, 2,
454   8, 4, 9, 10, 1, 3,
455   8, 5, 15, 4, 3, 4,
456   8, 6, 31, 5, 3, 5,
457   8, 7, 63, 6, 3, 6,
458   8, 8, 127, 7, 3, 7,
459   8, 9, 255, 8, 3, 8,
460   8, 10, 511, 9, 3, 9,
461   8, 11, 1023, 10, 3, 10,
462   8, 12, 2047, 11, 3, 11,
463   8, 13, 4095, 12, 3, 12,
464   8, 14, 8191, 13, 3, 13,
465   8, 15, 65535, 16, 3, 16,
466   9, 0, 0, 4, 0, 0,
467   9, 1, 3, 5, 1, 0,
468   9, 2, 5, 7, 1, 1,
469   9, 3, 7, 3, 3, 2,
470   9, 4, 9, 10, 1, 3,
471   9, 5, 15, 4, 3, 4,
472   9, 6, 31, 5, 3, 5,
473   9, 7, 63, 6, 3, 6,
474   9, 8, 127, 7, 3, 7,
475   9, 9, 255, 8, 3, 8,
476   9, 10, 511, 9, 3, 9,
477   9, 11, 1023, 10, 3, 10,
478   9, 12, 2047, 11, 3, 11,
479   9, 13, 4095, 12, 3, 12,
480   9, 14, 8191, 13, 3, 13,
481   9, 15, 65535, 16, 3, 16,
482   10, 0, 0, 4, 0, 0,
483   10, 1, 3, 5, 1, 0,
484   10, 2, 5, 7, 1, 1,
485   10, 3, 7, 3, 3, 2,
486   10, 4, 9, 10, 1, 3,
487   10, 5, 15, 4, 3, 4,
488   10, 6, 31, 5, 3, 5,
489   10, 7, 63, 6, 3, 6,
490   10, 8, 127, 7, 3, 7,
491   10, 9, 255, 8, 3, 8,
492   10, 10, 511, 9, 3, 9,
493   10, 11, 1023, 10, 3, 10,
494   10, 12, 2047, 11, 3, 11,
495   10, 13, 4095, 12, 3, 12,
496   10, 14, 8191, 13, 3, 13,
497   10, 15, 65535, 16, 3, 16,
498   11, 0, 0, 3, 0, 0,
499   11, 1, 3, 5, 1, 0,
500   11, 2, 5, 7, 1, 1,
501   11, 3, 7, 3, 3, 2,
502   11, 4, 9, 10, 1, 3,
503   11, 5, 15, 4, 3, 4,
504   11, 6, 31, 5, 3, 5,
505   11, 7, 65535, 16, 3, 16,
506   12, 0, 0, 3, 0, 0,
507   12, 1, 3, 5, 1, 0,
508   12, 2, 5, 7, 1, 1,
509   12, 3, 7, 3, 3, 2,
510   12, 4, 9, 10, 1, 3,
511   12, 5, 15, 4, 3, 4,
512   12, 6, 31, 5, 3, 5,
513   12, 7, 65535, 16, 3, 16,
514   13, 0, 0, 3, 0, 0,
515   13, 1, 3, 5, 1, 0,
516   13, 2, 5, 7, 1, 1,
517   13, 3, 7, 3, 3, 2,
518   13, 4, 9, 10, 1, 3,
519   13, 5, 15, 4, 3, 4,
520   13, 6, 31, 5, 3, 5,
521   13, 7, 65535, 16, 3, 16,
522   14, 0, 0, 3, 0, 0,
523   14, 1, 3, 5, 1, 0,
524   14, 2, 5, 7, 1, 1,
525   14, 3, 7, 3, 3, 2,
526   14, 4, 9, 10, 1, 3,
527   14, 5, 15, 4, 3, 4,
528   14, 6, 31, 5, 3, 5,
529   14, 7, 65535, 16, 3, 16,
530   15, 0, 0, 3, 0, 0,
531   15, 1, 3, 5, 1, 0,
532   15, 2, 5, 7, 1, 1,
533   15, 3, 7, 3, 3, 2,
534   15, 4, 9, 10, 1, 3,
535   15, 5, 15, 4, 3, 4,
536   15, 6, 31, 5, 3, 5,
537   15, 7, 65535, 16, 3, 16,
538   16, 0, 0, 3, 0, 0,
539   16, 1, 3, 5, 1, 0,
540   16, 2, 5, 7, 1, 1,
541   16, 3, 7, 3, 3, 2,
542   16, 4, 9, 10, 1, 3,
543   16, 5, 15, 4, 3, 4,
544   16, 6, 31, 5, 3, 5,
545   16, 7, 65535, 16, 3, 16,
546   17, 0, 0, 3, 0, 0,
547   17, 1, 3, 5, 1, 0,
548   17, 2, 5, 7, 1, 1,
549   17, 3, 7, 3, 3, 2,
550   17, 4, 9, 10, 1, 3,
551   17, 5, 15, 4, 3, 4,
552   17, 6, 31, 5, 3, 5,
553   17, 7, 65535, 16, 3, 16,
554   18, 0, 0, 3, 0, 0,
555   18, 1, 3, 5, 1, 0,
556   18, 2, 5, 7, 1, 1,
557   18, 3, 7, 3, 3, 2,
558   18, 4, 9, 10, 1, 3,
559   18, 5, 15, 4, 3, 4,
560   18, 6, 31, 5, 3, 5,
561   18, 7, 65535, 16, 3, 16,
562   19, 0, 0, 3, 0, 0,
563   19, 1, 3, 5, 1, 0,
564   19, 2, 5, 7, 1, 1,
565   19, 3, 7, 3, 3, 2,
566   19, 4, 9, 10, 1, 3,
567   19, 5, 15, 4, 3, 4,
568   19, 6, 31, 5, 3, 5,
569   19, 7, 65535, 16, 3, 16,
570   20, 0, 0, 3, 0, 0,
571   20, 1, 3, 5, 1, 0,
572   20, 2, 5, 7, 1, 1,
573   20, 3, 7, 3, 3, 2,
574   20, 4, 9, 10, 1, 3,
575   20, 5, 15, 4, 3, 4,
576   20, 6, 31, 5, 3, 5,
577   20, 7, 65535, 16, 3, 16,
578   21, 0, 0, 3, 0, 0,
579   21, 1, 3, 5, 1, 0,
580   21, 2, 5, 7, 1, 1,
581   21, 3, 7, 3, 3, 2,
582   21, 4, 9, 10, 1, 3,
583   21, 5, 15, 4, 3, 4,
584   21, 6, 31, 5, 3, 5,
585   21, 7, 65535, 16, 3, 16,
586   22, 0, 0, 3, 0, 0,
587   22, 1, 3, 5, 1, 0,
588   22, 2, 5, 7, 1, 1,
589   22, 3, 7, 3, 3, 2,
590   22, 4, 9, 10, 1, 3,
591   22, 5, 15, 4, 3, 4,
592   22, 6, 31, 5, 3, 5,
593   22, 7, 65535, 16, 3, 16,
594   23, 0, 0, 2, 0, 0,
595   23, 1, 3, 5, 1, 0,
596   23, 2, 5, 7, 1, 1,
597   23, 3, 65535, 16, 3, 16,
598   24, 0, 0, 2, 0, 0,
599   24, 1, 3, 5, 1, 0,
600   24, 2, 5, 7, 1, 1,
601   24, 3, 65535, 16, 3, 16,
602   25, 0, 0, 2, 0, 0,
603   25, 1, 3, 5, 1, 0,
604   25, 2, 5, 7, 1, 1,
605   25, 3, 65535, 16, 3, 16,
606   26, 0, 0, 2, 0, 0,
607   26, 1, 3, 5, 1, 0,
608   26, 2, 5, 7, 1, 1,
609   26, 3, 65535, 16, 3, 16,
610   27, 0, 0, 2, 0, 0,
611   27, 1, 3, 5, 1, 0,
612   27, 2, 5, 7, 1, 1,
613   27, 3, 65535, 16, 3, 16,
614   28, 0, 0, 2, 0, 0,
615   28, 1, 3, 5, 1, 0,
616   28, 2, 5, 7, 1, 1,
617   28, 3, 65535, 16, 3, 16,
618   29, 0, 0, 2, 0, 0,
619   29, 1, 3, 5, 1, 0,
620   29, 2, 5, 7, 1, 1,
621   29, 3, 65535, 16, 3, 16
622 };
623 
624 // Table alloc_2 with 8 subbands
625 static const unsigned short maaate_alloc_2[] = {
626   0, 0, 0, 4, 0, 0,
627   0, 1, 3, 5, 1, 0,
628   0, 2, 5, 7, 1, 1,
629   0, 3, 9, 10, 1, 3,
630   0, 4, 15, 4, 3, 4,
631   0, 5, 31, 5, 3, 5,
632   0, 6, 63, 6, 3, 6,
633   0, 7, 127, 7, 3, 7,
634   0, 8, 255, 8, 3, 8,
635   0, 9, 511, 9, 3, 9,
636   0, 10, 1023, 10, 3, 10,
637   0, 11, 2047, 11, 3, 11,
638   0, 12, 4095, 12, 3, 12,
639   0, 13, 8191, 13, 3, 13,
640   0, 14, 16383, 14, 3, 14,
641   0, 15, 32767, 15, 3, 15,
642   1, 0, 0, 4, 0, 0,
643   1, 1, 3, 5, 1, 0,
644   1, 2, 5, 7, 1, 1,
645   1, 3, 9, 10, 1, 3,
646   1, 4, 15, 4, 3, 4,
647   1, 5, 31, 5, 3, 5,
648   1, 6, 63, 6, 3, 6,
649   1, 7, 127, 7, 3, 7,
650   1, 8, 255, 8, 3, 8,
651   1, 9, 511, 9, 3, 9,
652   1, 10, 1023, 10, 3, 10,
653   1, 11, 2047, 11, 3, 11,
654   1, 12, 4095, 12, 3, 12,
655   1, 13, 8191, 13, 3, 13,
656   1, 14, 16383, 14, 3, 14,
657   1, 15, 32767, 15, 3, 15,
658   2, 0, 0, 3, 0, 0,
659   2, 1, 3, 5, 1, 0,
660   2, 2, 5, 7, 1, 1,
661   2, 3, 9, 10, 1, 3,
662   2, 4, 15, 4, 3, 4,
663   2, 5, 31, 5, 3, 5,
664   2, 6, 63, 6, 3, 6,
665   2, 7, 127, 7, 3, 7,
666   3, 0, 0, 3, 0, 0,
667   3, 1, 3, 5, 1, 0,
668   3, 2, 5, 7, 1, 1,
669   3, 3, 9, 10, 1, 3,
670   3, 4, 15, 4, 3, 4,
671   3, 5, 31, 5, 3, 5,
672   3, 6, 63, 6, 3, 6,
673   3, 7, 127, 7, 3, 7,
674   4, 0, 0, 3, 0, 0,
675   4, 1, 3, 5, 1, 0,
676   4, 2, 5, 7, 1, 1,
677   4, 3, 9, 10, 1, 3,
678   4, 4, 15, 4, 3, 4,
679   4, 5, 31, 5, 3, 5,
680   4, 6, 63, 6, 3, 6,
681   4, 7, 127, 7, 3, 7,
682   5, 0, 0, 3, 0, 0,
683   5, 1, 3, 5, 1, 0,
684   5, 2, 5, 7, 1, 1,
685   5, 3, 9, 10, 1, 3,
686   5, 4, 15, 4, 3, 4,
687   5, 5, 31, 5, 3, 5,
688   5, 6, 63, 6, 3, 6,
689   5, 7, 127, 7, 3, 7,
690   6, 0, 0, 3, 0, 0,
691   6, 1, 3, 5, 1, 0,
692   6, 2, 5, 7, 1, 1,
693   6, 3, 9, 10, 1, 3,
694   6, 4, 15, 4, 3, 4,
695   6, 5, 31, 5, 3, 5,
696   6, 6, 63, 6, 3, 6,
697   6, 7, 127, 7, 3, 7,
698   7, 0, 0, 3, 0, 0,
699   7, 1, 3, 5, 1, 0,
700   7, 2, 5, 7, 1, 1,
701   7, 3, 9, 10, 1, 3,
702   7, 4, 15, 4, 3, 4,
703   7, 5, 31, 5, 3, 5,
704   7, 6, 63, 6, 3, 6,
705   7, 7, 127, 7, 3, 7
706 };
707 
708 // Table alloc_3 with 12 subbands
709 static const unsigned short maaate_alloc_3[] = {
710   0, 0, 0, 4, 0, 0,
711   0, 1, 3, 5, 1, 0,
712   0, 2, 5, 7, 1, 1,
713   0, 3, 9, 10, 1, 3,
714   0, 4, 15, 4, 3, 4,
715   0, 5, 31, 5, 3, 5,
716   0, 6, 63, 6, 3, 6,
717   0, 7, 127, 7, 3, 7,
718   0, 8, 255, 8, 3, 8,
719   0, 9, 511, 9, 3, 9,
720   0, 10, 1023, 10, 3, 10,
721   0, 11, 2047, 11, 3, 11,
722   0, 12, 4095, 12, 3, 12,
723   0, 13, 8191, 13, 3, 13,
724   0, 14, 16383, 14, 3, 14,
725   0, 15, 32767, 15, 3, 15,
726   1, 0, 0, 4, 0, 0,
727   1, 1, 3, 5, 1, 0,
728   1, 2, 5, 7, 1, 1,
729   1, 3, 9, 10, 1, 3,
730   1, 4, 15, 4, 3, 4,
731   1, 5, 31, 5, 3, 5,
732   1, 6, 63, 6, 3, 6,
733   1, 7, 127, 7, 3, 7,
734   1, 8, 255, 8, 3, 8,
735   1, 9, 511, 9, 3, 9,
736   1, 10, 1023, 10, 3, 10,
737   1, 11, 2047, 11, 3, 11,
738   1, 12, 4095, 12, 3, 12,
739   1, 13, 8191, 13, 3, 13,
740   1, 14, 16383, 14, 3, 14,
741   1, 15, 32767, 15, 3, 15,
742   2, 0, 0, 3, 0, 0,
743   2, 1, 3, 5, 1, 0,
744   2, 2, 5, 7, 1, 1,
745   2, 3, 9, 10, 1, 3,
746   2, 4, 15, 4, 3, 4,
747   2, 5, 31, 5, 3, 5,
748   2, 6, 63, 6, 3, 6,
749   2, 7, 127, 7, 3, 7,
750   3, 0, 0, 3, 0, 0,
751   3, 1, 3, 5, 1, 0,
752   3, 2, 5, 7, 1, 1,
753   3, 3, 9, 10, 1, 3,
754   3, 4, 15, 4, 3, 4,
755   3, 5, 31, 5, 3, 5,
756   3, 6, 63, 6, 3, 6,
757   3, 7, 127, 7, 3, 7,
758   4, 0, 0, 3, 0, 0,
759   4, 1, 3, 5, 1, 0,
760   4, 2, 5, 7, 1, 1,
761   4, 3, 9, 10, 1, 3,
762   4, 4, 15, 4, 3, 4,
763   4, 5, 31, 5, 3, 5,
764   4, 6, 63, 6, 3, 6,
765   4, 7, 127, 7, 3, 7,
766   5, 0, 0, 3, 0, 0,
767   5, 1, 3, 5, 1, 0,
768   5, 2, 5, 7, 1, 1,
769   5, 3, 9, 10, 1, 3,
770   5, 4, 15, 4, 3, 4,
771   5, 5, 31, 5, 3, 5,
772   5, 6, 63, 6, 3, 6,
773   5, 7, 127, 7, 3, 7,
774   6, 0, 0, 3, 0, 0,
775   6, 1, 3, 5, 1, 0,
776   6, 2, 5, 7, 1, 1,
777   6, 3, 9, 10, 1, 3,
778   6, 4, 15, 4, 3, 4,
779   6, 5, 31, 5, 3, 5,
780   6, 6, 63, 6, 3, 6,
781   6, 7, 127, 7, 3, 7,
782   7, 0, 0, 3, 0, 0,
783   7, 1, 3, 5, 1, 0,
784   7, 2, 5, 7, 1, 1,
785   7, 3, 9, 10, 1, 3,
786   7, 4, 15, 4, 3, 4,
787   7, 5, 31, 5, 3, 5,
788   7, 6, 63, 6, 3, 6,
789   7, 7, 127, 7, 3, 7,
790   8, 0, 0, 3, 0, 0,
791   8, 1, 3, 5, 1, 0,
792   8, 2, 5, 7, 1, 1,
793   8, 3, 9, 10, 1, 3,
794   8, 4, 15, 4, 3, 4,
795   8, 5, 31, 5, 3, 5,
796   8, 6, 63, 6, 3, 6,
797   8, 7, 127, 7, 3, 7,
798   9, 0, 0, 3, 0, 0,
799   9, 1, 3, 5, 1, 0,
800   9, 2, 5, 7, 1, 1,
801   9, 3, 9, 10, 1, 3,
802   9, 4, 15, 4, 3, 4,
803   9, 5, 31, 5, 3, 5,
804   9, 6, 63, 6, 3, 6,
805   9, 7, 127, 7, 3, 7,
806   10, 0, 0, 3, 0, 0,
807   10, 1, 3, 5, 1, 0,
808   10, 2, 5, 7, 1, 1,
809   10, 3, 9, 10, 1, 3,
810   10, 4, 15, 4, 3, 4,
811   10, 5, 31, 5, 3, 5,
812   10, 6, 63, 6, 3, 6,
813   10, 7, 127, 7, 3, 7,
814   11, 0, 0, 3, 0, 0,
815   11, 1, 3, 5, 1, 0,
816   11, 2, 5, 7, 1, 1,
817   11, 3, 9, 10, 1, 3,
818   11, 4, 15, 4, 3, 4,
819   11, 5, 31, 5, 3, 5,
820   11, 6, 63, 6, 3, 6,
821   11, 7, 127, 7, 3, 7
822 };
823 
824 // Table alloc_4 with 30 subbands
825 static const unsigned short maaate_alloc_4[] = {
826   0, 0, 0, 4, 0, 0,
827   0, 1, 3, 5, 1, 0,
828   0, 2, 5, 7, 1, 1,
829   0, 3, 7, 3, 3, 2,
830   0, 4, 9, 10, 1, 3,
831   0, 5, 15, 4, 3, 4,
832   0, 6, 31, 5, 3, 5,
833   0, 7, 63, 6, 3, 6,
834   0, 8, 127, 7, 3, 7,
835   0, 9, 255, 8, 3, 8,
836   0, 10, 511, 9, 3, 9,
837   0, 11, 1023, 10, 3, 10,
838   0, 12, 2047, 11, 3, 11,
839   0, 13, 4095, 12, 3, 12,
840   0, 14, 8191, 13, 3, 13,
841   0, 15, 16383, 14, 3, 14,
842   1, 0, 0, 4, 0, 0,
843   1, 1, 3, 5, 1, 0,
844   1, 2, 5, 7, 1, 1,
845   1, 3, 7, 3, 3, 2,
846   1, 4, 9, 10, 1, 3,
847   1, 5, 15, 4, 3, 4,
848   1, 6, 31, 5, 3, 5,
849   1, 7, 63, 6, 3, 6,
850   1, 8, 127, 7, 3, 7,
851   1, 9, 255, 8, 3, 8,
852   1, 10, 511, 9, 3, 9,
853   1, 11, 1023, 10, 3, 10,
854   1, 12, 2047, 11, 3, 11,
855   1, 13, 4095, 12, 3, 12,
856   1, 14, 8191, 13, 3, 13,
857   1, 15, 16383, 14, 3, 14,
858   2, 0, 0, 4, 0, 0,
859   2, 1, 3, 5, 1, 0,
860   2, 2, 5, 7, 1, 1,
861   2, 3, 7, 3, 3, 2,
862   2, 4, 9, 10, 1, 3,
863   2, 5, 15, 4, 3, 4,
864   2, 6, 31, 5, 3, 5,
865   2, 7, 63, 6, 3, 6,
866   2, 8, 127, 7, 3, 7,
867   2, 9, 255, 8, 3, 8,
868   2, 10, 511, 9, 3, 9,
869   2, 11, 1023, 10, 3, 10,
870   2, 12, 2047, 11, 3, 11,
871   2, 13, 4095, 12, 3, 12,
872   2, 14, 8191, 13, 3, 13,
873   2, 15, 16383, 14, 3, 14,
874   3, 0, 0, 4, 0, 0,
875   3, 1, 3, 5, 1, 0,
876   3, 2, 5, 7, 1, 1,
877   3, 3, 7, 3, 3, 2,
878   3, 4, 9, 10, 1, 3,
879   3, 5, 15, 4, 3, 4,
880   3, 6, 31, 5, 3, 5,
881   3, 7, 63, 6, 3, 6,
882   3, 8, 127, 7, 3, 7,
883   3, 9, 255, 8, 3, 8,
884   3, 10, 511, 9, 3, 9,
885   3, 11, 1023, 10, 3, 10,
886   3, 12, 2047, 11, 3, 11,
887   3, 13, 4095, 12, 3, 12,
888   3, 14, 8191, 13, 3, 13,
889   3, 15, 16383, 14, 3, 14,
890   4, 0, 0, 3, 0, 0,
891   4, 1, 3, 5, 1, 0,
892   4, 2, 5, 7, 1, 1,
893   4, 3, 9, 10, 1, 3,
894   4, 4, 15, 4, 3, 4,
895   4, 5, 31, 5, 3, 5,
896   4, 6, 63, 6, 3, 6,
897   4, 7, 127, 7, 3, 7,
898   5, 0, 0, 3, 0, 0,
899   5, 1, 3, 5, 1, 0,
900   5, 2, 5, 7, 1, 1,
901   5, 3, 9, 10, 1, 3,
902   5, 4, 15, 4, 3, 4,
903   5, 5, 31, 5, 3, 5,
904   5, 6, 63, 6, 3, 6,
905   5, 7, 127, 7, 3, 7,
906   6, 0, 0, 3, 0, 0,
907   6, 1, 3, 5, 1, 0,
908   6, 2, 5, 7, 1, 1,
909   6, 3, 9, 10, 1, 3,
910   6, 4, 15, 4, 3, 4,
911   6, 5, 31, 5, 3, 5,
912   6, 6, 63, 6, 3, 6,
913   6, 7, 127, 7, 3, 7,
914   7, 0, 0, 3, 0, 0,
915   7, 1, 3, 5, 1, 0,
916   7, 2, 5, 7, 1, 1,
917   7, 3, 9, 10, 1, 3,
918   7, 4, 15, 4, 3, 4,
919   7, 5, 31, 5, 3, 5,
920   7, 6, 63, 6, 3, 6,
921   7, 7, 127, 7, 3, 7,
922   8, 0, 0, 3, 0, 0,
923   8, 1, 3, 5, 1, 0,
924   8, 2, 5, 7, 1, 1,
925   8, 3, 9, 10, 1, 3,
926   8, 4, 15, 4, 3, 4,
927   8, 5, 31, 5, 3, 5,
928   8, 6, 63, 6, 3, 6,
929   8, 7, 127, 7, 3, 7,
930   9, 0, 0, 3, 0, 0,
931   9, 1, 3, 5, 1, 0,
932   9, 2, 5, 7, 1, 1,
933   9, 3, 9, 10, 1, 3,
934   9, 4, 15, 4, 3, 4,
935   9, 5, 31, 5, 3, 5,
936   9, 6, 63, 6, 3, 6,
937   9, 7, 127, 7, 3, 7,
938   10, 0, 0, 3, 0, 0,
939   10, 1, 3, 5, 1, 0,
940   10, 2, 5, 7, 1, 1,
941   10, 3, 9, 10, 1, 3,
942   10, 4, 15, 4, 3, 4,
943   10, 5, 31, 5, 3, 5,
944   10, 6, 63, 6, 3, 6,
945   10, 7, 127, 7, 3, 7,
946   11, 0, 0, 2, 0, 0,
947   11, 1, 3, 5, 1, 0,
948   11, 2, 5, 7, 1, 1,
949   11, 3, 9, 10, 1, 3,
950   12, 0, 0, 2, 0, 0,
951   12, 1, 3, 5, 1, 0,
952   12, 2, 5, 7, 1, 1,
953   12, 3, 9, 10, 1, 3,
954   13, 0, 0, 2, 0, 0,
955   13, 1, 3, 5, 1, 0,
956   13, 2, 5, 7, 1, 1,
957   13, 3, 9, 10, 1, 3,
958   14, 0, 0, 2, 0, 0,
959   14, 1, 3, 5, 1, 0,
960   14, 2, 5, 7, 1, 1,
961   14, 3, 9, 10, 1, 3,
962   15, 0, 0, 2, 0, 0,
963   15, 1, 3, 5, 1, 0,
964   15, 2, 5, 7, 1, 1,
965   15, 3, 9, 10, 1, 3,
966   16, 0, 0, 2, 0, 0,
967   16, 1, 3, 5, 1, 0,
968   16, 2, 5, 7, 1, 1,
969   16, 3, 9, 10, 1, 3,
970   17, 0, 0, 2, 0, 0,
971   17, 1, 3, 5, 1, 0,
972   17, 2, 5, 7, 1, 1,
973   17, 3, 9, 10, 1, 3,
974   18, 0, 0, 2, 0, 0,
975   18, 1, 3, 5, 1, 0,
976   18, 2, 5, 7, 1, 1,
977   18, 3, 9, 10, 1, 3,
978   19, 0, 0, 2, 0, 0,
979   19, 1, 3, 5, 1, 0,
980   19, 2, 5, 7, 1, 1,
981   19, 3, 9, 10, 1, 3,
982   20, 0, 0, 2, 0, 0,
983   20, 1, 3, 5, 1, 0,
984   20, 2, 5, 7, 1, 1,
985   20, 3, 9, 10, 1, 3,
986   21, 0, 0, 2, 0, 0,
987   21, 1, 3, 5, 1, 0,
988   21, 2, 5, 7, 1, 1,
989   21, 3, 9, 10, 1, 3,
990   22, 0, 0, 2, 0, 0,
991   22, 1, 3, 5, 1, 0,
992   22, 2, 5, 7, 1, 1,
993   22, 3, 9, 10, 1, 3,
994   23, 0, 0, 2, 0, 0,
995   23, 1, 3, 5, 1, 0,
996   23, 2, 5, 7, 1, 1,
997   23, 3, 9, 10, 1, 3,
998   24, 0, 0, 2, 0, 0,
999   24, 1, 3, 5, 1, 0,
1000   24, 2, 5, 7, 1, 1,
1001   24, 3, 9, 10, 1, 3,
1002   25, 0, 0, 2, 0, 0,
1003   25, 1, 3, 5, 1, 0,
1004   25, 2, 5, 7, 1, 1,
1005   25, 3, 9, 10, 1, 3,
1006   26, 0, 0, 2, 0, 0,
1007   26, 1, 3, 5, 1, 0,
1008   26, 2, 5, 7, 1, 1,
1009   26, 3, 9, 10, 1, 3,
1010   27, 0, 0, 2, 0, 0,
1011   27, 1, 3, 5, 1, 0,
1012   27, 2, 5, 7, 1, 1,
1013   27, 3, 9, 10, 1, 3,
1014   28, 0, 0, 2, 0, 0,
1015   28, 1, 3, 5, 1, 0,
1016   28, 2, 5, 7, 1, 1,
1017   28, 3, 9, 10, 1, 3,
1018   29, 0, 0, 2, 0, 0,
1019   29, 1, 3, 5, 1, 0,
1020   29, 2, 5, 7, 1, 1,
1021   29, 3, 9, 10, 1, 3
1022 };
1023 
1024 
1025 //--------------------------------------------------------------------------
1026 
1027 // constructor
1028 CSAPI_MPEG
Layer2(MPEGfile * mp)1029 Layer2::Layer2 (MPEGfile *mp)
1030 {
1031   // set reference to audio frame handler
1032   frame = mp;
1033 
1034   // indicate: no table loaded
1035   table=-1;
1036 
1037   return;
1038 }
1039 
1040 // parse audio data of layer 2 frame
1041 CSAPI_MPEG bool
parse_data(DecodeLevel decode)1042 Layer2::parse_data(DecodeLevel decode)
1043 {
1044   // pick the appropriate bit allocation table for the frame
1045   pick_table();
1046 
1047     // parse bit allocation information
1048   decode_bitalloc();
1049 
1050   // parse scalefactor information
1051   decode_scale();
1052 
1053   // check crc checksum
1054   if (frame->crcprotected()) {
1055     if (frame->checksum != calc_CRC()) {
1056       cerr << "MaaateP: WARNING: wrong checksum" << endl;
1057       return false;
1058     }
1059   }
1060 
1061   // parse audio samples
1062   buffer_samples();
1063 
1064   if (decode > dec_fields) {
1065     restore_samples();
1066 
1067     if (decode > dec_subbands) {
1068       // restore pcm samples
1069       int s_ch = frame->channels();
1070       for (int no=0; no<12; no++) {
1071 	for (int ch=0; ch<s_ch; ch++) {
1072 	  for (int ss=0; ss<3; ss++) {
1073 	    subband_syn (&(restoredsamples[no][ch][ss][0]),
1074 			 ch,
1075 			 &(pcm[no][ch][ss][0]));
1076 	  }
1077 	}
1078       }
1079     }
1080   }
1081 
1082   return true;
1083 }
1084 
1085 // calculates checksum on required fields
1086 CSAPI_MPEG unsigned int
calc_CRC()1087 Layer2::calc_CRC()
1088 {
1089   unsigned int crc = 0xffff;
1090   frame->update_CRC(frame->header.bitrate,         4, &crc);
1091   frame->update_CRC(frame->header.samplingrate,    2, &crc);
1092   frame->update_CRC(frame->header.padding,         1, &crc);
1093   frame->update_CRC(frame->header.extension,       1, &crc);
1094   frame->update_CRC(frame->header.mode,            2, &crc);
1095   frame->update_CRC(frame->header.mode_ext,        2, &crc);
1096   frame->update_CRC(frame->header.copyright,       1, &crc);
1097   frame->update_CRC(frame->header.original,        1, &crc);
1098   frame->update_CRC(frame->header.emphasis,        2, &crc);
1099 
1100   int sub, ch; // index variable
1101   int s_bands = frame->subbands();
1102   int s_bound = frame->intensitystereobound();
1103   int s_ch = frame->channels();
1104   for (sub=0; sub < s_bands; sub++) {
1105     if (sub < s_bound) {
1106       for (ch = 0; ch < s_ch; ch++) {
1107 	frame->update_CRC(allocation[ch][sub],
1108 			  alloctable[sub][0].bits, &crc);
1109       }
1110     } else {
1111       frame->update_CRC(allocation[0][sub],
1112 			alloctable[sub][0].bits, &crc);
1113     }
1114   }
1115 
1116   for (sub=0; sub < s_bands; sub++) {
1117     for (ch = 0; ch < s_ch; ch++) {
1118       if (allocation[ch][sub]) {
1119 	frame->update_CRC(scfsis[ch][sub], 2, &crc);
1120       }
1121     }
1122   }
1123 
1124   return crc;
1125 }
1126 
1127 
1128 // using the decoded info, the appropriate possible quantization
1129 // per subband table is loaded
1130 CSAPI_MPEG void
pick_table()1131 Layer2::pick_table() {
1132 
1133   // decision rules refer to per-channel bitrates (kbits/sec/channel)
1134   int s_brate = frame->bitrateperchannel();
1135   double s_srate = frame->samplingrate();
1136   if (frame->version() == MPEG1) {
1137     if (s_brate == 32 || s_brate == 48) {
1138       if (s_srate == 32) {
1139 	table=3;
1140       } else {
1141 	table=2;
1142       }
1143     } else {
1144       if (s_srate == 48 || (s_brate >= 56 && s_brate <= 80)) {
1145 	table=0;
1146       } else {
1147 	table=1;
1148       }
1149     }
1150   } else { // MPEG2
1151     table=4;
1152   }
1153 
1154   // read bit allocation table
1155   read_bit_alloc();
1156   return;
1157 }
1158 
1159 // Read one of the data tables ("alloc_*") specifying the bit allocation/
1160 // quatization parameters for each subband in layer II encoding
1161 CSAPI_MPEG void
read_bit_alloc()1162 Layer2::read_bit_alloc()
1163 {
1164   unsigned int	 a, b, c, d, i, j, nitems;
1165   const unsigned short *p;
1166 
1167   switch (table) {
1168   case 0:
1169   default:
1170     p = &maaate_alloc_0[0];
1171     nitems = sizeof(maaate_alloc_0)/sizeof(unsigned short);
1172     break;
1173   case 1:
1174     p = &maaate_alloc_1[0];
1175     nitems = sizeof(maaate_alloc_1)/sizeof(unsigned short);
1176     break;
1177   case 2:
1178     p = &maaate_alloc_2[0];
1179     nitems = sizeof(maaate_alloc_2)/sizeof(unsigned short);
1180     break;
1181   case 3:
1182     p = &maaate_alloc_3[0];
1183     nitems = sizeof(maaate_alloc_3)/sizeof(unsigned short);
1184     break;
1185   case 4:
1186     p = &maaate_alloc_4[0];
1187     nitems = sizeof(maaate_alloc_4)/sizeof(unsigned short);
1188     break;
1189   }
1190 
1191   while (nitems > 0) {
1192     i = *p++; j = *p++;
1193     a = *p++; b = *p++; c = *p++; d = *p++;
1194     alloctable[i][j].steps = a;
1195     alloctable[i][j].bits  = b;
1196     alloctable[i][j].group = c;
1197     alloctable[i][j].quant = d;
1198     nitems -= 6;
1199   }
1200 
1201   return;
1202 }
1203 
1204 // The bit allocation information, i.e. how many bits are used to code
1205 // a sample, is decoded. Layer II is Ws and bit rate dependent.
1206 CSAPI_MPEG void
decode_bitalloc()1207 Layer2::decode_bitalloc()
1208 {
1209   int sub, ch; //index variable
1210   int s_bound = frame->intensitystereobound();
1211   int s_ch = frame->channels();
1212   int s_bands = frame->subbands();
1213   for (sub = 0; sub < s_bound; sub++) {
1214     for (ch = 0; ch < s_ch; ch++) {
1215       allocation[ch][sub]
1216 	= frame->readbitsfrombuffer(alloctable[sub][0].bits);
1217     }
1218   }
1219   for (sub = s_bound; sub<s_bands; sub++) {
1220     allocation[0][sub] = allocation[1][sub] =
1221       frame->readbitsfrombuffer(alloctable[sub][0].bits);
1222   }
1223   return;
1224 }
1225 
1226 
1227 // Layer 2 format of scale factor extraction involves reading
1228 // 2 bit per subband of scale factor selection information,
1229 // 6 bit per subband as scale factor.
1230 CSAPI_MPEG void
decode_scale()1231 Layer2::decode_scale()
1232 {
1233   int sub, ch; // index variable
1234 
1235   // read 2 bit scfsis
1236   int s_bands = frame->subbands();
1237   int s_ch = frame->channels();
1238   for (sub = 0; sub < s_bands; sub++) {
1239     for (ch = 0; ch < s_ch; ch++) {
1240       if (allocation[ch][sub]) {
1241 	scfsis[ch][sub] = frame->readbitsfrombuffer(2);
1242       } else {
1243 	scfsis[ch][sub] = 0;
1244       }
1245     }
1246   }
1247 
1248   // read 6 bit scale factors
1249   for (sub = 0; sub < s_bands; sub++) {
1250     for (ch = 0; ch < s_ch; ch++) {
1251       if (allocation[ch][sub]) {
1252 	switch (scfsis[ch][sub]) {
1253 	case 0 :
1254 	  /* All three scale factors transmitted */
1255 	  scale_index[ch][0][sub] = frame->readbitsfrombuffer(6);
1256 	  scale_index[ch][1][sub] = frame->readbitsfrombuffer(6);
1257 	  scale_index[ch][2][sub] = frame->readbitsfrombuffer(6);
1258 	  break;
1259 	case 1 :
1260 	  /* Scale factor 1 & 3 transmitted */
1261 	  scale_index[ch][0][sub] = scale_index[ch][1][sub]
1262 	    = frame->readbitsfrombuffer(6);
1263 	  scale_index[ch][2][sub] = frame->readbitsfrombuffer(6);
1264 	  break;
1265 	case 2 :
1266 	  /* Only one scale factor transmitted */
1267 	  scale_index[ch][0][sub] = scale_index[ch][1][sub]
1268 	    = scale_index[ch][2][sub] = frame->readbitsfrombuffer(6);
1269 	  break;
1270 	case 3 :
1271 	  /* Scale factor 1 & 2 transmitted */
1272 	  scale_index[ch][0][sub] = frame->readbitsfrombuffer(6);
1273 	  scale_index[ch][1][sub] = scale_index[ch][2][sub]
1274 	    = frame->readbitsfrombuffer(6);
1275 	  break;
1276 	default :
1277 	  break;
1278 	}
1279       } else {
1280 	scale_index[ch][0][sub] = scale_index[ch][1][sub] =
1281 	  scale_index[ch][2][sub] = SCALE_RANGE-1;
1282       }
1283     }
1284   }
1285   return;
1286 }
1287 
1288 
1289 // store compressed sample from the bit stream for layer 2:
1290 // If grouping is indicated for a particular subband, then the
1291 // sample size has to be read from the bits_group and the merged
1292 // samples have to be decomposed into the three distict samples.
1293 CSAPI_MPEG void
buffer_samples()1294 Layer2::buffer_samples()
1295 {
1296   int sub, ch;
1297   int s_bands = frame->subbands();
1298   int s_bound = frame->intensitystereobound();
1299   int s_ch = frame->channels();
1300 
1301   for (int no=0; no<12; no++) {
1302     for (sub = 0; sub < s_bands; sub++) {
1303       for (ch = 0; ch < ((sub < s_bound) ? s_ch : 1); ch++) {
1304 	if (allocation[ch][sub]) {
1305 	  int k = bits_per_sample(ch,sub);
1306 
1307 	  // Check for grouping in subband
1308 	  if (grouping(ch,sub) == 3) { // no grouping
1309 	    samples[no][ch][0][sub] = frame->readbitsfrombuffer(k);
1310 	    samples[no][ch][1][sub] = frame->readbitsfrombuffer(k);
1311 	    samples[no][ch][2][sub] = frame->readbitsfrombuffer(k);
1312 	  } else {
1313 	    // alloctable = 3, 5, 9 imply grouping
1314 	    unsigned int nlevels, c;
1315 
1316 	    nlevels = bits_per_samplecode(ch,sub);
1317 	    c = frame->readbitsfrombuffer(k);
1318 	    // Uncode three samples
1319 	    samples[no][ch][0][sub] = c % nlevels;
1320 	    // Assign both at once. Note that c is modified!!
1321 	    samples[no][ch][1][sub] = (c /= nlevels) % nlevels;
1322 	    samples[no][ch][2][sub] = (c /= nlevels) % nlevels;
1323 	  }
1324 
1325 	} else { // no samples transmitted
1326 	  samples[no][ch][0][sub] = samples[no][ch][1][sub]
1327 	    = samples[no][ch][2][sub] = 0;
1328 	}
1329       }
1330       if (stereo == 2 && sub >= s_bound) {
1331 	// joint stereo: copy L to R
1332 	samples[no][1][0][sub] = samples[no][0][0][sub];
1333 	samples[no][1][1][sub] = samples[no][0][1][sub];
1334 	samples[no][1][2][sub] = samples[no][0][2][sub];
1335       }
1336     }
1337 
1338     // faster in this order
1339     for (ch = 0; ch < s_ch; ch++) {
1340       for (sub = s_bands; sub < SBLIMIT; sub++) {
1341 	samples[no][ch][0][sub] = samples[no][ch][1][sub] =
1342 	  samples[no][ch][2][sub] = 0;
1343       }
1344     }
1345 
1346   }
1347   return;
1348 }
1349 
1350 // restore the compressed sample to a fractional number:
1351 // - complement the MSB of the sample
1352 // Use s = c * (s' + d)
1353 // where s'=fractional number, i.e. samples[][] with first bit inverted
1354 //       c =see table, index according to alloctable
1355 //       d =see table, index according to alloctable
1356 // - multiply dequantized sample by scale factor
1357 
1358 // from table B.4 ISO/IEC 11172-3
1359 static double c[17] = { 1.33333333333, 1.60000000000, 1.14285714286,
1360                         1.77777777777, 1.06666666666, 1.03225806452,
1361                         1.01587301587, 1.00787401575, 1.00392156863,
1362                         1.00195694716, 1.00097751711, 1.00048851979,
1363                         1.00024420024, 1.00012208522, 1.00006103888,
1364                         1.00003051851, 1.00001525902 };
1365 
1366 static double d[17] = { 0.500000000, 0.500000000, 0.250000000, 0.500000000,
1367                         0.125000000, 0.062500000, 0.031250000, 0.015625000,
1368                         0.007812500, 0.003906250, 0.001953125, 0.0009765625,
1369                         0.00048828125, 0.00024414063, 0.00012207031,
1370                         0.00006103516, 0.00003051758 };
1371 
1372 CSAPI_MPEG void
restore_samples()1373 Layer2::restore_samples()
1374 {
1375   int no, sub, ch, ss; // index variables
1376   int s_bands = frame->subbands();
1377   int s_ch = frame->channels();
1378   for (no=0; no<12; no++) {
1379     for (sub = 0; sub < s_bands; sub++) {
1380       for (ch = 0; ch < s_ch; ch++) {
1381 	for (ss=0; ss<3; ss++) {
1382 	  if (allocation[ch][sub]) {
1383 	    int  x = 0;
1384 	    register double r;
1385 
1386 	    // Locate MSB in the sample
1387 	    // u is set outside loop to hopefully speed up the loop
1388 	    unsigned int u = bits_per_samplecode(ch,sub);
1389 	    while (((unsigned int) 1 << x) < u)
1390 	      x++;
1391 
1392 	    // MSB inversion
1393 	    // read sample
1394 	    register unsigned int samp
1395 	      = samples[no][ch][ss][sub];
1396 	    x = 1L << (x - 1);          // for MSB test
1397 	    if ((samp & x)) {
1398 	      r = 0.0;                // MSB is set
1399 	    } else {
1400 	      r = -1.0;               // MSB is cleared
1401 	    }
1402 
1403 	    // Form a 2's complement sample
1404 	    r += (double)(samp & (x - 1)) / (double)x;
1405 
1406 	    // calculate index
1407 	    register unsigned int indx
1408 	      = alloctable[sub][allocation[ch][sub]].quant;
1409 
1410 	    // Dequantize the sample
1411 	    r += d[indx];
1412 	    r *= c[indx];
1413 
1414 	    // multiply by scale factor
1415 	    r *= scalefactor(ch,sub,no>>2);
1416 	    restoredsamples[no][ch][ss][sub] = r;
1417 
1418 	  } else {
1419 	    restoredsamples[no][ch][ss][sub] = 0.0;
1420 	  }
1421 	}
1422       }
1423     }
1424 
1425     for (sub = s_bands; sub < SBLIMIT; sub++) {
1426       for (ch = 0; ch < s_ch; ch++) {
1427 	for (ss=0; ss<3; ss++) {
1428 	  restoredsamples[no][ch][ss][sub] = 0.0;
1429 	}
1430       }
1431     }
1432   }
1433   return;
1434 }
1435 
1436 CSAPI_MPEG int
bitallocation(unsigned int channel,unsigned int subband)1437 Layer2::bitallocation (unsigned int channel, unsigned int subband)
1438 {
1439     return allocation[channel][subband];
1440 }
1441 
1442 CSAPI_MPEG int
scfsi(unsigned int channel,unsigned int subband)1443 Layer2::scfsi (unsigned int channel, unsigned int subband)
1444 {
1445     return scfsis[channel][subband];
1446 }
1447 
1448 CSAPI_MPEG unsigned int
grouping(unsigned int channel,unsigned int subband)1449 Layer2::grouping (unsigned int channel, unsigned int subband)
1450 {
1451     return alloctable[subband][allocation[channel][subband]].group;
1452 }
1453 
1454 CSAPI_MPEG unsigned int
bits_per_sample(unsigned int channel,unsigned int subband)1455 Layer2::bits_per_sample (unsigned int channel, unsigned int subband)
1456 {
1457     return alloctable[subband][allocation[channel][subband]].bits;
1458 }
1459 
1460 CSAPI_MPEG unsigned int
bits_per_samplecode(unsigned int channel,unsigned int subband)1461 Layer2::bits_per_samplecode (unsigned int channel, unsigned int subband)
1462 {
1463     return alloctable[subband][allocation[channel][subband]].steps;
1464 }
1465 
1466 CSAPI_MPEG float
scalefactor(unsigned int channel,unsigned int subband,unsigned int no)1467 Layer2::scalefactor (unsigned int channel, unsigned int subband, unsigned int no)
1468 {
1469     if (subband >= frame->subbands()) {
1470 	return 0;
1471     } else {
1472 	return maaate_scalefactors[scale_index[channel][no][subband]];
1473     }
1474 }
1475 
1476 CSAPI_MPEG int
sample(unsigned int channel,unsigned int subb,unsigned int number,unsigned int ss)1477 Layer2::sample (unsigned int channel, unsigned int subb, unsigned int number, unsigned int ss)
1478 {
1479     return samples[number][channel][ss][subb];
1480 }
1481 
1482 CSAPI_MPEG double
restored_sample(unsigned int ch,unsigned int subb,unsigned int no,unsigned int ss)1483 Layer2::restored_sample (unsigned int ch, unsigned int subb, unsigned int no, unsigned int ss)
1484 {
1485     return restoredsamples[no][ch][ss][subb];
1486 }
1487 
1488 CSAPI_MPEG short
pcm_sample(unsigned int channel,unsigned int subb,unsigned int number,unsigned int ss)1489 Layer2::pcm_sample (unsigned int channel, unsigned int subb, unsigned int number, unsigned int ss)
1490 {
1491     return pcm[number][channel][ss][subb];
1492 }
1493