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