2 * most other tables are calculated on program start (which is (of course)
4 * Layer-3 huffman table is in huffman.h
7 #include "../libzmpeg3.h"
9 static zaudio_t::imdct_al_table_t alloc_0[] = {
10 {4,0},{5,3},{3,-3},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},{10,-511},
11 {11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},{16,-32767},
12 {4,0},{5,3},{3,-3},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},{10,-511},
13 {11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},{16,-32767},
14 {4,0},{5,3},{3,-3},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},{10,-511},
15 {11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},{16,-32767},
16 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
17 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
18 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
19 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
20 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
21 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
22 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
23 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
24 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
25 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
26 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
27 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
28 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
29 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
30 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
31 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
32 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
33 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
34 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
35 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
36 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
37 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
38 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
39 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
40 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
41 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
42 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
43 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
44 {2,0},{5,3},{7,5},{16,-32767},
45 {2,0},{5,3},{7,5},{16,-32767},
46 {2,0},{5,3},{7,5},{16,-32767},
47 {2,0},{5,3},{7,5},{16,-32767}
50 static zaudio_t::imdct_al_table_t alloc_1[] = {
51 {4,0},{5,3},{3,-3},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},{10,-511},
52 {11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},{16,-32767},
53 {4,0},{5,3},{3,-3},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},{10,-511},
54 {11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},{16,-32767},
55 {4,0},{5,3},{3,-3},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},{10,-511},
56 {11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},{16,-32767},
57 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
58 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
59 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
60 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
61 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
62 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
63 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
64 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
65 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
66 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
67 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
68 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
69 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
70 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
71 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
72 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{16,-32767},
73 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
74 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
75 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
76 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
77 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
78 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
79 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
80 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
81 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
82 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
83 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
84 {3,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{16,-32767},
85 {2,0},{5,3},{7,5},{16,-32767},
86 {2,0},{5,3},{7,5},{16,-32767},
87 {2,0},{5,3},{7,5},{16,-32767},
88 {2,0},{5,3},{7,5},{16,-32767},
89 {2,0},{5,3},{7,5},{16,-32767},
90 {2,0},{5,3},{7,5},{16,-32767},
91 {2,0},{5,3},{7,5},{16,-32767}
94 static zaudio_t::imdct_al_table_t alloc_2[] = {
95 {4,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},
96 {10,-511},{11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},
97 {4,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},
98 {10,-511},{11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},
99 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
100 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
101 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
102 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
103 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
104 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63}
107 static zaudio_t::imdct_al_table_t alloc_3[] = {
108 {4,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},
109 {10,-511},{11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},
110 {4,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},{9,-255},
111 {10,-511},{11,-1023},{12,-2047},{13,-4095},{14,-8191},{15,-16383},
112 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
113 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
114 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
115 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
116 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
117 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
118 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
119 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
120 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
121 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63}
124 static zaudio_t::imdct_al_table_t alloc_4[] = {
125 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
126 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{14,-8191},
127 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
128 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{14,-8191},
129 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
130 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{14,-8191},
131 {4,0},{5,3},{7,5},{3,-3},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},{8,-127},
132 {9,-255},{10,-511},{11,-1023},{12,-2047},{13,-4095},{14,-8191},
133 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
134 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
135 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
136 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
137 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
138 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
139 {3,0},{5,3},{7,5},{10,9},{4,-7},{5,-15},{6,-31},{7,-63},
140 {2,0},{5,3},{7,5},{10,9},
141 {2,0},{5,3},{7,5},{10,9},
142 {2,0},{5,3},{7,5},{10,9},
143 {2,0},{5,3},{7,5},{10,9},
144 {2,0},{5,3},{7,5},{10,9},
145 {2,0},{5,3},{7,5},{10,9},
146 {2,0},{5,3},{7,5},{10,9},
147 {2,0},{5,3},{7,5},{10,9},
148 {2,0},{5,3},{7,5},{10,9},
149 {2,0},{5,3},{7,5},{10,9},
150 {2,0},{5,3},{7,5},{10,9},
151 {2,0},{5,3},{7,5},{10,9},
152 {2,0},{5,3},{7,5},{10,9},
153 {2,0},{5,3},{7,5},{10,9},
154 {2,0},{5,3},{7,5},{10,9},
155 {2,0},{5,3},{7,5},{10,9},
156 {2,0},{5,3},{7,5},{10,9},
157 {2,0},{5,3},{7,5},{10,9},
158 {2,0},{5,3},{7,5},{10,9}
161 int zaudio_decoder_layer_t::
164 static int translate[3][2][16] = {
165 {{ 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0},
166 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0}},
167 {{ 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0},
168 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0}},
169 {{ 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0},
170 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0}}
173 static imdct_al_table_t *tables[5] = {
174 alloc_0, alloc_1, alloc_2, alloc_3, alloc_4
176 static int sblims[5] = {27, 30, 8, 12, 30};
181 table = translate[sampling_frequency_code][2 - channels][bitrate_index];
182 sblim = sblims[table];
184 alloc = tables[table];
189 int zaudio_decoder_layer_t::
190 step_one(uint8_t *bit_alloc, int *scale)
192 int stereo = channels - 1;
193 int sblimit = II_sblimit;
194 int sblimit2 = II_sblimit << stereo;
195 imdct_al_table_t *alloc1 = alloc;
197 uint8_t *scfsi_buf = layer2_scfsi_buf;
198 uint8_t *scfsi, *bita;
199 uint8_t *bita_end = bit_alloc + 64;
205 for( i=jsbound; --i>=0 && bita < bita_end-2; alloc1+=(1<<step) ) {
206 *bita++ = (char)stream->get_bits(step = alloc1->bits);
207 *bita++ = (char)stream->get_bits(step);
209 for( i=sblimit-jsbound; --i>=0 && bita < bita_end-2; alloc1+=(1<<step)) {
210 bita[0] = (char)stream->get_bits(step = alloc1->bits);
216 for( i=sblimit2; --i>=0 ; )
218 *scfsi++ = (char)stream->get_bits(2);
222 for( i=sblimit; --i>=0 ; alloc1+=(1<<step) )
223 *bita++ = (char)stream->get_bits(step = alloc1->bits);
226 for( i=sblimit; --i>=0 ; )
227 if( *bita++ ) *scfsi++ = (char)stream->get_bits(2);
232 for( i=sblimit2; --i>=0; ) {
233 if( *bita++ ) switch(*scfsi++) {
235 *scale++ = stream->get_bits(6);
236 *scale++ = stream->get_bits(6);
237 *scale++ = stream->get_bits(6);
240 *scale++ = sc = stream->get_bits(6);
242 *scale++ = stream->get_bits(6);
245 *scale++ = sc = stream->get_bits(6);
249 default: /* case 3 */
250 *scale++ = stream->get_bits(6);
251 *scale++ = sc = stream->get_bits(6);
259 int zaudio_decoder_layer_t::
260 step_two(uint8_t *bit_alloc, float fraction[2][4][SBLIMIT], int *scale, int x1)
262 int i, j, k, ba, result = 0;
263 int sblimit = II_sblimit;
264 imdct_al_table_t *alloc2, *alloc1 = alloc;
265 uint8_t *bita = bit_alloc;
268 for( i=0; i<jsbound; ++i, alloc1+=(1<<step) ) {
270 for( j=0; j<channels; ++j ) {
271 if( (ba=*bita++) != 0 ) {
272 k = (alloc2 = alloc1 + ba)->bits;
273 if( (d1 = alloc2->d) < 0 ) {
274 float cm = muls[k][scale[x1]];
275 fraction[j][0][i] = ((float)((int)stream->get_bits(k) + d1)) * cm;
276 fraction[j][1][i] = ((float)((int)stream->get_bits(k) + d1)) * cm;
277 fraction[j][2][i] = ((float)((int)stream->get_bits(k) + d1)) * cm;
280 static int *table[] =
281 {0, 0, 0, grp_3tab, 0, grp_5tab, 0, 0, 0, grp_9tab};
282 uint32_t idx, *tab, m = scale[x1];
283 idx = (uint32_t)stream->get_bits(k);
284 tab = (uint32_t*)(table[d1] + idx + idx + idx);
285 fraction[j][0][i] = muls[*tab++][m];
286 fraction[j][1][i] = muls[*tab++][m];
287 fraction[j][2][i] = muls[*tab][m];
292 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
296 for( i=jsbound; i<sblimit; ++i, alloc1+=(1<<step) ) {
298 /* channel 1 and channel 2 bitalloc are the same */
300 if( (ba=*bita++) != 0 ) {
301 k= (alloc2 = alloc1+ba)->bits;
302 if( (d1 = alloc2->d) < 0 ) {
303 float cm = muls[k][scale[x1 + 3]];
304 fraction[1][0][i] = (fraction[0][0][i] = (float)((int)stream->get_bits(k) + d1)) * cm;
305 fraction[1][1][i] = (fraction[0][1][i] = (float)((int)stream->get_bits(k) + d1)) * cm;
306 fraction[1][2][i] = (fraction[0][2][i] = (float)((int)stream->get_bits(k) + d1)) * cm;
307 cm = muls[k][scale[x1]];
308 fraction[0][0][i] *= cm;
309 fraction[0][1][i] *= cm;
310 fraction[0][2][i] *= cm;
313 static int *table[] = {0, 0, 0, grp_3tab, 0, grp_5tab, 0, 0, 0, grp_9tab};
314 uint8_t idx, *tab, m1, m2;
317 idx = (uint8_t)stream->get_bits(k);
318 tab = (uint8_t*)(table[d1] + idx + idx + idx);
319 fraction[0][0][i] = muls[*tab][m1];
320 fraction[1][0][i] = muls[*tab++][m2];
321 fraction[0][1][i] = muls[*tab][m1];
322 fraction[1][1][i] = muls[*tab++][m2];
323 fraction[0][2][i] = muls[*tab][m1];
324 fraction[1][2][i] = muls[*tab][m2];
329 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
330 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
333 should we use individual scalefac for channel 2 or
334 is the current way the right one , where we just copy channel 1 to
336 The current 'strange' thing is, that we throw away the scalefac
337 values for the second channel ...!!
338 -> changed .. now we use the scalefac values of channel one !!
342 if( sblimit > (int)SBLIMIT ) sblimit = SBLIMIT;
343 for( i=sblimit; i < (int)SBLIMIT; ++i )
344 for( j=0; j < channels; ++j )
345 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
350 int zaudio_decoder_layer_t::
351 do_layer2(uint8_t *zframe, int zframe_size, float **zoutput, int render)
353 int i, j, result = 0;
354 float fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
355 uint8_t bit_alloc[64];
357 int zsingle = single;
358 int output_position = 0;
361 /* Set up bitstream to use buffer */
362 stream->use_ptr(zframe);
363 if( error_protection )
364 stream->get_bits(16);
368 jsbound = (mode == md_JOINT_STEREO) ?
369 (mode_ext << 2) + 4 : II_sblimit;
371 if( channels == 1 || zsingle == 3 )
374 result |= step_one(bit_alloc, scale);
376 for( i=0; i < (int)SCALE_BLOCK && !result; ++i ) {
377 result |= step_two(bit_alloc, fraction, scale, i >> 2);
378 for( j=0; j < 3; ++j ) {
382 synth_stereo(fraction[0][j], 0, zoutput[0], &output_position);
384 output_position += 32;
388 int p1 = output_position;
391 synth_stereo(fraction[0][j], 0, zoutput[0], &p1);
393 synth_stereo(fraction[1][j], 1, zoutput[1], &output_position);
397 output_position += 32;
402 return output_position;