2 * layer2.c: Mpeg Layer-2 audio decoder
4 * Copyright (C) 1999-2010 The L.A.M.E. project
6 * Initially written by Michael Hipp, see also AUTHORS and README.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
23 /* $Id: layer2.c,v 1.32 2011/05/24 20:40:56 robert Exp $ */
32 #include "decode_i386.h"
39 static int gd_are_hip_tables_layer2_initialized = 0;
41 static unsigned char grp_3tab[32 * 3] = { 0, }; /* used: 27 */
42 static unsigned char grp_5tab[128 * 3] = { 0, }; /* used: 125 */
43 static unsigned char grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
47 hip_init_tables_layer2(void)
49 static const double mulmul[27] = {
50 0.0, -2.0 / 3.0, 2.0 / 3.0,
51 2.0 / 7.0, 2.0 / 15.0, 2.0 / 31.0, 2.0 / 63.0, 2.0 / 127.0, 2.0 / 255.0,
52 2.0 / 511.0, 2.0 / 1023.0, 2.0 / 2047.0, 2.0 / 4095.0, 2.0 / 8191.0,
53 2.0 / 16383.0, 2.0 / 32767.0, 2.0 / 65535.0,
54 -4.0 / 5.0, -2.0 / 5.0, 2.0 / 5.0, 4.0 / 5.0,
55 -8.0 / 9.0, -4.0 / 9.0, -2.0 / 9.0, 2.0 / 9.0, 4.0 / 9.0, 8.0 / 9.0
57 static const unsigned char base[3][9] = {
60 {21, 1, 22, 23, 0, 24, 25, 2, 26}
64 static const int tablen[3] = { 3, 5, 9 };
65 static unsigned char *itable, *tables[3] = { grp_3tab, grp_5tab, grp_9tab };
67 if (gd_are_hip_tables_layer2_initialized) {
70 gd_are_hip_tables_layer2_initialized = 1;
72 for (i = 0; i < 3; i++) {
75 for (j = 0; j < len; j++)
76 for (k = 0; k < len; k++)
77 for (l = 0; l < len; l++) {
78 *itable++ = base[i][l];
79 *itable++ = base[i][k];
80 *itable++ = base[i][j];
84 for (k = 0; k < 27; k++) {
87 for (j = 3, i = 0; i < 63; i++, j--)
88 *table++ = (real) (m * pow(2.0, (double) j / 3.0));
95 grp_table_select(short d1, unsigned int idx)
97 /* RH: it seems to be common, that idx is larger than the table's sizes.
98 is it OK to return a zero vector in this case? FIXME
100 static unsigned char dummy_table[] = { 0,0,0 };
105 idx = idx < x ? idx : x;
106 return &grp_3tab[3 * idx];
109 idx = idx < x ? idx : x;
110 return &grp_5tab[3 * idx];
113 idx = idx < x ? idx : x;
114 return &grp_9tab[3 * idx];
119 return &dummy_table[0];
122 typedef struct sideinfo_layer_II_struct
124 unsigned char allocation[SBLIMIT][2];
125 unsigned char scalefactor[SBLIMIT][2][3]; /* subband / channel / block */
131 II_step_one(PMPSTR mp, sideinfo_layer_II *si, struct frame *fr)
133 int nch = fr->stereo;
134 int sblimit = fr->II_sblimit;
135 int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit;
136 struct al_table2 const *alloc1 = fr->alloc;
137 unsigned char scfsi[SBLIMIT][2];
140 memset(si, 0, sizeof(*si));
143 for (i = 0; i < jsbound; ++i) {
144 short step = alloc1->bits;
145 unsigned char b0 = get_leq_8_bits(mp, step);
146 unsigned char b1 = get_leq_8_bits(mp, step);
147 alloc1 += (1 << step);
148 si->allocation[i][0] = b0;
149 si->allocation[i][1] = b1;
151 for (i = jsbound; i < sblimit; ++i) {
152 short step = alloc1->bits;
153 unsigned char b0 = get_leq_8_bits(mp, step);
154 alloc1 += (1 << step);
155 si->allocation[i][0] = b0;
156 si->allocation[i][1] = b0;
158 for (i = 0; i < sblimit; ++i) {
159 unsigned char n0 = si->allocation[i][0];
160 unsigned char n1 = si->allocation[i][1];
161 unsigned char b0 = n0 ? get_leq_8_bits(mp, 2) : 0;
162 unsigned char b1 = n1 ? get_leq_8_bits(mp, 2) : 0;
168 for (i = 0; i < sblimit; ++i) {
169 short step = alloc1->bits;
170 unsigned char b0 = get_leq_8_bits(mp, step);
171 alloc1 += (1 << step);
172 si->allocation[i][0] = b0;
174 for (i = 0; i < sblimit; ++i) {
175 unsigned char n0 = si->allocation[i][0];
176 unsigned char b0 = n0 ? get_leq_8_bits(mp, 2) : 0;
180 for (i = 0; i < sblimit; ++i) {
181 for (ch = 0; ch < nch; ++ch) {
182 unsigned char s0 = 0, s1 = 0, s2 = 0;
183 if (si->allocation[i][ch]) {
184 switch (scfsi[i][ch]) {
186 s0 = get_leq_8_bits(mp, 6);
187 s1 = get_leq_8_bits(mp, 6);
188 s2 = get_leq_8_bits(mp, 6);
191 s0 = get_leq_8_bits(mp, 6);
193 s2 = get_leq_8_bits(mp, 6);
196 s0 = get_leq_8_bits(mp, 6);
201 s0 = get_leq_8_bits(mp, 6);
202 s1 = get_leq_8_bits(mp, 6);
209 si->scalefactor[i][ch][0] = s0;
210 si->scalefactor[i][ch][1] = s1;
211 si->scalefactor[i][ch][2] = s2;
217 II_step_two(PMPSTR mp, sideinfo_layer_II* si, struct frame *fr, int gr, real fraction[2][4][SBLIMIT])
219 struct al_table2 const *alloc1 = fr->alloc;
220 int sblimit = fr->II_sblimit;
221 int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit;
222 int i, ch, nch = fr->stereo;
223 double cm, r0, r1, r2;
225 for (i = 0; i < jsbound; ++i) {
226 short step = alloc1->bits;
227 for (ch = 0; ch < nch; ++ch) {
228 unsigned char ba = si->allocation[i][ch];
230 unsigned char x1 = si->scalefactor[i][ch][gr];
231 struct al_table2 const *alloc2 = alloc1 + ba;
232 short k = alloc2->bits;
233 short d1 = alloc2->d;
235 k = (k <= 16) ? k : 16;
237 x1 = (x1 < 64) ? x1 : 63;
239 int v0 = getbits(mp, k);
240 int v1 = getbits(mp, k);
241 int v2 = getbits(mp, k);
248 unsigned int idx = getbits(mp, k);
249 unsigned char *tab = grp_table_select(d1, idx);
250 unsigned char k0 = tab[0];
251 unsigned char k1 = tab[1];
252 unsigned char k2 = tab[2];
257 fraction[ch][0][i] = (real) r0;
258 fraction[ch][1][i] = (real) r1;
259 fraction[ch][2][i] = (real) r2;
262 fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0.0;
265 alloc1 += (1 << step);
268 for (i = jsbound; i < sblimit; i++) {
269 short step = alloc1->bits;
270 unsigned char ba = si->allocation[i][0];
272 struct al_table2 const *alloc2 = alloc1 + ba;
273 short k = alloc2->bits;
274 short d1 = alloc2->d;
276 k = (k <= 16) ? k : 16;
278 int v0 = getbits(mp, k);
279 int v1 = getbits(mp, k);
280 int v2 = getbits(mp, k);
281 for (ch = 0; ch < nch; ++ch) {
282 unsigned char x1 = si->scalefactor[i][ch][gr];
284 x1 = (x1 < 64) ? x1 : 63;
289 fraction[ch][0][i] = (real) r0;
290 fraction[ch][1][i] = (real) r1;
291 fraction[ch][2][i] = (real) r2;
295 unsigned int idx = getbits(mp, k);
296 unsigned char *tab = grp_table_select(d1, idx);
297 unsigned char k0 = tab[0];
298 unsigned char k1 = tab[1];
299 unsigned char k2 = tab[2];
300 for (ch = 0; ch < nch; ++ch) {
301 unsigned char x1 = si->scalefactor[i][ch][gr];
303 x1 = (x1 < 64) ? x1 : 63;
307 fraction[ch][0][i] = (real) r0;
308 fraction[ch][1][i] = (real) r1;
309 fraction[ch][2][i] = (real) r2;
314 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = 0.0;
315 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
317 alloc1 += (1 << step);
319 if (sblimit > fr->down_sample_sblimit) {
320 sblimit = fr->down_sample_sblimit;
322 for (ch = 0; ch < nch; ++ch) {
323 for (i = sblimit; i < SBLIMIT; ++i) {
324 fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0.0;
330 II_select_table(struct frame *fr)
333 static const int translate[3][2][16] =
334 { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
335 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
336 { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
337 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
338 { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
339 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
343 static const struct al_table2 *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
344 static const int sblims[5] = { 27, 30, 8, 12, 30 };
349 table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
350 sblim = sblims[table];
352 fr->alloc = (struct al_table2 const *) tables[table];
353 fr->II_sblimit = sblim;
358 decode_layer2_sideinfo(PMPSTR mp)
361 /* FIXME: extract side information and check values */
366 decode_layer2_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point)
368 real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
369 sideinfo_layer_II si;
370 struct frame *fr = &(mp->fr);
371 int single = fr->single;
375 II_step_one(mp, &si, fr);
377 if (fr->stereo == 1 || single == 3)
381 for (i = 0; i < SCALE_BLOCK; i++) {
382 II_step_two(mp, &si, fr, i >> 2, fraction);
383 for (j = 0; j < 3; j++) {
384 clip += synth_1to1_mono(mp, fraction[single][j], pcm_sample, pcm_point);
389 for (i = 0; i < SCALE_BLOCK; i++) {
390 II_step_two(mp, &si, fr, i >> 2, fraction);
391 for (j = 0; j < 3; j++) {
393 clip += synth_1to1(mp, fraction[0][j], 0, pcm_sample, &p1);
394 clip += synth_1to1(mp, fraction[1][j], 1, pcm_sample, pcm_point);