summaryrefslogtreecommitdiff
path: root/audio_codec/libmad/layer12.c (plain)
blob: d52f2005f87a853fbc880474dbf1b6ca4d0d5ee2
1/*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * $Id: layer12.c,v 1.17 2004/02/05 09:02:39 rob Exp $
20 */
21
22# ifdef HAVE_CONFIG_H
23# include "config.h"
24# endif
25
26# include "global.h"
27
28# ifdef HAVE_LIMITS_H
29# include <limits.h>
30# else
31# define CHAR_BIT 8
32# endif
33
34# include "fixed.h"
35# include "bit.h"
36# include "stream.h"
37# include "frame.h"
38# include "layer12.h"
39
40/*
41 * scalefactor table
42 * used in both Layer I and Layer II decoding
43 */
44static
45mad_fixed_t const sf_table[64] = {
46# include "sf_table.dat"
47};
48
49/* --- Layer I ------------------------------------------------------------- */
50
51/* linear scaling table */
52static
53mad_fixed_t const linear_table[14] = {
54 MAD_F(0x15555555), /* 2^2 / (2^2 - 1) == 1.33333333333333 */
55 MAD_F(0x12492492), /* 2^3 / (2^3 - 1) == 1.14285714285714 */
56 MAD_F(0x11111111), /* 2^4 / (2^4 - 1) == 1.06666666666667 */
57 MAD_F(0x10842108), /* 2^5 / (2^5 - 1) == 1.03225806451613 */
58 MAD_F(0x10410410), /* 2^6 / (2^6 - 1) == 1.01587301587302 */
59 MAD_F(0x10204081), /* 2^7 / (2^7 - 1) == 1.00787401574803 */
60 MAD_F(0x10101010), /* 2^8 / (2^8 - 1) == 1.00392156862745 */
61 MAD_F(0x10080402), /* 2^9 / (2^9 - 1) == 1.00195694716243 */
62 MAD_F(0x10040100), /* 2^10 / (2^10 - 1) == 1.00097751710655 */
63 MAD_F(0x10020040), /* 2^11 / (2^11 - 1) == 1.00048851978505 */
64 MAD_F(0x10010010), /* 2^12 / (2^12 - 1) == 1.00024420024420 */
65 MAD_F(0x10008004), /* 2^13 / (2^13 - 1) == 1.00012208521548 */
66 MAD_F(0x10004001), /* 2^14 / (2^14 - 1) == 1.00006103888177 */
67 MAD_F(0x10002000) /* 2^15 / (2^15 - 1) == 1.00003051850948 */
68};
69
70/*
71 * NAME: I_sample()
72 * DESCRIPTION: decode one requantized Layer I sample from a bitstream
73 */
74static
75mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb)
76{
77 mad_fixed_t sample;
78
79 sample = mad_bit_read(ptr, nb);
80
81 /* invert most significant bit, extend sign, then scale to fixed format */
82
83 sample ^= 1 << (nb - 1);
84 sample |= -(sample & (1 << (nb - 1)));
85
86 sample <<= MAD_F_FRACBITS - (nb - 1);
87
88 /* requantize the sample */
89
90 /* s'' = (2^nb / (2^nb - 1)) * (s''' + 2^(-nb + 1)) */
91
92 sample += MAD_F_ONE >> (nb - 1);
93
94 return mad_f_mul(sample, linear_table[nb - 2]);
95
96 /* s' = factor * s'' */
97 /* (to be performed by caller) */
98}
99
100/*
101 * NAME: layer->I()
102 * DESCRIPTION: decode a single Layer I frame
103 */
104int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
105{
106 struct mad_header *header = &frame->header;
107 unsigned int nch, bound, ch, s, sb, nb;
108 unsigned char allocation[2][32], scalefactor[2][32];
109
110 nch = MAD_NCHANNELS(header);
111
112 bound = 32;
113 if (header->mode == MAD_MODE_JOINT_STEREO) {
114 header->flags |= MAD_FLAG_I_STEREO;
115 bound = 4 + header->mode_extension * 4;
116 }
117
118 /* check CRC word */
119
120 if (header->flags & MAD_FLAG_PROTECTION) {
121 header->crc_check =
122 mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)),
123 header->crc_check);
124
125 if (header->crc_check != header->crc_target &&
126 !(frame->options & MAD_OPTION_IGNORECRC)) {
127 stream->error = MAD_ERROR_BADCRC;
128 return -1;
129 }
130 }
131
132 /* decode bit allocations */
133
134 for (sb = 0; sb < bound; ++sb) {
135 for (ch = 0; ch < nch; ++ch) {
136 nb = mad_bit_read(&stream->ptr, 4);
137
138 if (nb == 15) {
139 stream->error = MAD_ERROR_BADBITALLOC;
140 return -1;
141 }
142
143 allocation[ch][sb] = nb ? nb + 1 : 0;
144 }
145 }
146
147 for (sb = bound; sb < 32; ++sb) {
148 nb = mad_bit_read(&stream->ptr, 4);
149
150 if (nb == 15) {
151 stream->error = MAD_ERROR_BADBITALLOC;
152 return -1;
153 }
154
155 allocation[0][sb] =
156 allocation[1][sb] = nb ? nb + 1 : 0;
157 }
158
159 /* decode scalefactors */
160
161 for (sb = 0; sb < 32; ++sb) {
162 for (ch = 0; ch < nch; ++ch) {
163 if (allocation[ch][sb]) {
164 scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6);
165
166# if defined(OPT_STRICT)
167 /*
168 * Scalefactor index 63 does not appear in Table B.1 of
169 * ISO/IEC 11172-3. Nonetheless, other implementations accept it,
170 * so we only reject it if OPT_STRICT is defined.
171 */
172 if (scalefactor[ch][sb] == 63) {
173 stream->error = MAD_ERROR_BADSCALEFACTOR;
174 return -1;
175 }
176# endif
177 }
178 }
179 }
180
181 /* decode samples */
182
183 for (s = 0; s < 12; ++s) {
184 for (sb = 0; sb < bound; ++sb) {
185 for (ch = 0; ch < nch; ++ch) {
186 nb = allocation[ch][sb];
187 frame->sbsample[ch][s][sb] = nb ?
188 mad_f_mul(I_sample(&stream->ptr, nb),
189 sf_table[scalefactor[ch][sb]]) : 0;
190 }
191 }
192
193 for (sb = bound; sb < 32; ++sb) {
194 if ((nb = allocation[0][sb])) {
195 mad_fixed_t sample;
196
197 sample = I_sample(&stream->ptr, nb);
198
199 for (ch = 0; ch < nch; ++ch) {
200 frame->sbsample[ch][s][sb] =
201 mad_f_mul(sample, sf_table[scalefactor[ch][sb]]);
202 }
203 } else {
204 for (ch = 0; ch < nch; ++ch) {
205 frame->sbsample[ch][s][sb] = 0;
206 }
207 }
208 }
209 }
210
211 return 0;
212}
213
214/* --- Layer II ------------------------------------------------------------ */
215
216/* possible quantization per subband table */
217static
218struct {
219 unsigned int sblimit;
220 unsigned char const offsets[30];
221} const sbquant_table[5] = {
222 /* ISO/IEC 11172-3 Table B.2a */
223 {
224 27, {
225 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 0 */
226 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0
227 }
228 },
229 /* ISO/IEC 11172-3 Table B.2b */
230 {
231 30, {
232 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 1 */
233 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0
234 }
235 },
236 /* ISO/IEC 11172-3 Table B.2c */
237 { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } }, /* 2 */
238 /* ISO/IEC 11172-3 Table B.2d */
239 { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }, /* 3 */
240 /* ISO/IEC 13818-3 Table B.1 */
241 {
242 30, {
243 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, /* 4 */
244 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
245 }
246 }
247};
248
249/* bit allocation table */
250static
251struct {
252 unsigned short nbal;
253 unsigned short offset;
254} const bitalloc_table[8] = {
255 { 2, 0 }, /* 0 */
256 { 2, 3 }, /* 1 */
257 { 3, 3 }, /* 2 */
258 { 3, 1 }, /* 3 */
259 { 4, 2 }, /* 4 */
260 { 4, 3 }, /* 5 */
261 { 4, 4 }, /* 6 */
262 { 4, 5 } /* 7 */
263};
264
265/* offsets into quantization class table */
266static
267unsigned char const offset_table[6][15] = {
268 { 0, 1, 16 }, /* 0 */
269 { 0, 1, 2, 3, 4, 5, 16 }, /* 1 */
270 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, /* 2 */
271 { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, /* 3 */
272 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16 }, /* 4 */
273 { 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } /* 5 */
274};
275
276/* quantization class table */
277static
278struct quantclass {
279 unsigned short nlevels;
280 unsigned char group;
281 unsigned char bits;
282 mad_fixed_t C;
283 mad_fixed_t D;
284} const qc_table[17] = {
285# include "qc_table.dat"
286};
287
288/*
289 * NAME: II_samples()
290 * DESCRIPTION: decode three requantized Layer II samples from a bitstream
291 */
292static
293void II_samples(struct mad_bitptr *ptr,
294 struct quantclass const *quantclass,
295 mad_fixed_t output[3])
296{
297 unsigned int nb, s, sample[3];
298
299 if ((nb = quantclass->group)) {
300 unsigned int c, nlevels;
301
302 /* degrouping */
303 c = mad_bit_read(ptr, quantclass->bits);
304 nlevels = quantclass->nlevels;
305
306 for (s = 0; s < 3; ++s) {
307 sample[s] = c % nlevels;
308 c /= nlevels;
309 }
310 } else {
311 nb = quantclass->bits;
312
313 for (s = 0; s < 3; ++s) {
314 sample[s] = mad_bit_read(ptr, nb);
315 }
316 }
317
318 for (s = 0; s < 3; ++s) {
319 mad_fixed_t requantized;
320
321 /* invert most significant bit, extend sign, then scale to fixed format */
322
323 requantized = sample[s] ^(1 << (nb - 1));
324 requantized |= -(requantized & (1 << (nb - 1)));
325
326 requantized <<= MAD_F_FRACBITS - (nb - 1);
327
328 /* requantize the sample */
329
330 /* s'' = C * (s''' + D) */
331
332 output[s] = mad_f_mul(requantized + quantclass->D, quantclass->C);
333
334 /* s' = factor * s'' */
335 /* (to be performed by caller) */
336 }
337}
338
339/*
340 * NAME: layer->II()
341 * DESCRIPTION: decode a single Layer II frame
342 */
343int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
344{
345 struct mad_header *header = &frame->header;
346 struct mad_bitptr start;
347 unsigned int index, sblimit, nbal, nch, bound, gr, ch, s, sb;
348 unsigned char const *offsets;
349 unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3];
350 mad_fixed_t samples[3];
351
352 nch = MAD_NCHANNELS(header);
353
354 if (header->flags & MAD_FLAG_LSF_EXT) {
355 index = 4;
356 } else if (header->flags & MAD_FLAG_FREEFORMAT) {
357 goto freeformat;
358 } else {
359 unsigned long bitrate_per_channel;
360
361 bitrate_per_channel = header->bitrate;
362 if (nch == 2) {
363 bitrate_per_channel /= 2;
364
365# if defined(OPT_STRICT)
366 /*
367 * ISO/IEC 11172-3 allows only single channel mode for 32, 48, 56, and
368 * 80 kbps bitrates in Layer II, but some encoders ignore this
369 * restriction. We enforce it if OPT_STRICT is defined.
370 */
371 if (bitrate_per_channel <= 28000 || bitrate_per_channel == 40000) {
372 stream->error = MAD_ERROR_BADMODE;
373 return -1;
374 }
375# endif
376 } else { /* nch == 1 */
377#if 0
378 if (bitrate_per_channel > 192000) {
379 /*
380 * ISO/IEC 11172-3 does not allow single channel mode for 224, 256,
381 * 320, or 384 kbps bitrates in Layer II.
382 */
383 stream->error = MAD_ERROR_BADMODE;
384 return -1;
385 }
386#endif
387 }
388
389 if (bitrate_per_channel <= 48000) {
390 index = (header->samplerate == 32000) ? 3 : 2;
391 } else if (bitrate_per_channel <= 80000) {
392 index = 0;
393 } else {
394freeformat:
395 index = (header->samplerate == 48000) ? 0 : 1;
396 }
397 }
398
399 sblimit = sbquant_table[index].sblimit;
400 offsets = sbquant_table[index].offsets;
401
402 bound = 32;
403 if (header->mode == MAD_MODE_JOINT_STEREO) {
404 header->flags |= MAD_FLAG_I_STEREO;
405 bound = 4 + header->mode_extension * 4;
406 }
407
408 if (bound > sblimit) {
409 bound = sblimit;
410 }
411
412 start = stream->ptr;
413
414 /* decode bit allocations */
415
416 for (sb = 0; sb < bound; ++sb) {
417 nbal = bitalloc_table[offsets[sb]].nbal;
418
419 for (ch = 0; ch < nch; ++ch) {
420 allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal);
421 }
422 }
423
424 for (sb = bound; sb < sblimit; ++sb) {
425 nbal = bitalloc_table[offsets[sb]].nbal;
426
427 allocation[0][sb] =
428 allocation[1][sb] = mad_bit_read(&stream->ptr, nbal);
429 }
430
431 /* decode scalefactor selection info */
432
433 for (sb = 0; sb < sblimit; ++sb) {
434 for (ch = 0; ch < nch; ++ch) {
435 if (allocation[ch][sb]) {
436 scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
437 }
438 }
439 }
440
441 /* check CRC word */
442
443 if (header->flags & MAD_FLAG_PROTECTION) {
444 header->crc_check =
445 mad_bit_crc(start, mad_bit_length(&start, &stream->ptr),
446 header->crc_check);
447
448 if (header->crc_check != header->crc_target &&
449 !(frame->options & MAD_OPTION_IGNORECRC)) {
450 stream->error = MAD_ERROR_BADCRC;
451 return -1;
452 }
453 }
454
455 /* decode scalefactors */
456
457 for (sb = 0; sb < sblimit; ++sb) {
458 for (ch = 0; ch < nch; ++ch) {
459 if (allocation[ch][sb]) {
460 scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);
461
462 switch (scfsi[ch][sb]) {
463 case 2:
464 scalefactor[ch][sb][2] =
465 scalefactor[ch][sb][1] =
466 scalefactor[ch][sb][0];
467 break;
468
469 case 0:
470 scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
471 /* fall through */
472
473 case 1:
474 case 3:
475 scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
476 }
477
478 if (scfsi[ch][sb] & 1) {
479 scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1];
480 }
481
482# if defined(OPT_STRICT)
483 /*
484 * Scalefactor index 63 does not appear in Table B.1 of
485 * ISO/IEC 11172-3. Nonetheless, other implementations accept it,
486 * so we only reject it if OPT_STRICT is defined.
487 */
488 if (scalefactor[ch][sb][0] == 63 ||
489 scalefactor[ch][sb][1] == 63 ||
490 scalefactor[ch][sb][2] == 63) {
491 stream->error = MAD_ERROR_BADSCALEFACTOR;
492 return -1;
493 }
494# endif
495 }
496 }
497 }
498
499 /* decode samples */
500
501 for (gr = 0; gr < 12; ++gr) {
502 for (sb = 0; sb < bound; ++sb) {
503 for (ch = 0; ch < nch; ++ch) {
504 if ((index = allocation[ch][sb])) {
505 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
506
507 II_samples(&stream->ptr, &qc_table[index], samples);
508
509 for (s = 0; s < 3; ++s) {
510 frame->sbsample[ch][3 * gr + s][sb] =
511 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
512 }
513 } else {
514 for (s = 0; s < 3; ++s) {
515 frame->sbsample[ch][3 * gr + s][sb] = 0;
516 }
517 }
518 }
519 }
520
521 for (sb = bound; sb < sblimit; ++sb) {
522 if ((index = allocation[0][sb])) {
523 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
524
525 II_samples(&stream->ptr, &qc_table[index], samples);
526
527 for (ch = 0; ch < nch; ++ch) {
528 for (s = 0; s < 3; ++s) {
529 frame->sbsample[ch][3 * gr + s][sb] =
530 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
531 }
532 }
533 } else {
534 for (ch = 0; ch < nch; ++ch) {
535 for (s = 0; s < 3; ++s) {
536 frame->sbsample[ch][3 * gr + s][sb] = 0;
537 }
538 }
539 }
540 }
541
542 for (ch = 0; ch < nch; ++ch) {
543 for (s = 0; s < 3; ++s) {
544 for (sb = sblimit; sb < 32; ++sb) {
545 frame->sbsample[ch][3 * gr + s][sb] = 0;
546 }
547 }
548 }
549 }
550
551 return 0;
552}
553