check for a disc before mounting to avoid IOS breakage
[libogc.git] / libmad / layer12.c
blob6c2c2f1c652097790e0d3cd7497090f06aabb622
1 /*
2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2003 Underbit Technologies, Inc.
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.
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.
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
21 # ifdef HAVE_CONFIG_H
22 # include "config.h"
23 # endif
25 # include "global.h"
27 # ifdef HAVE_LIMITS_H
28 # include <limits.h>
29 # else
30 # define CHAR_BIT 8
31 # endif
33 # include "fixed.h"
34 # include "bit.h"
35 # include "stream.h"
36 # include "frame.h"
37 # include "layer12.h"
40 * scalefactor table
41 * used in both Layer I and Layer II decoding
43 static
44 mad_fixed_t const sf_table[64] = {
45 # include "sf_table.dat"
48 /* --- Layer I ------------------------------------------------------------- */
50 /* linear scaling table */
51 static
52 mad_fixed_t const linear_table[14] = {
53 MAD_F(0x15555555), /* 2^2 / (2^2 - 1) == 1.33333333333333 */
54 MAD_F(0x12492492), /* 2^3 / (2^3 - 1) == 1.14285714285714 */
55 MAD_F(0x11111111), /* 2^4 / (2^4 - 1) == 1.06666666666667 */
56 MAD_F(0x10842108), /* 2^5 / (2^5 - 1) == 1.03225806451613 */
57 MAD_F(0x10410410), /* 2^6 / (2^6 - 1) == 1.01587301587302 */
58 MAD_F(0x10204081), /* 2^7 / (2^7 - 1) == 1.00787401574803 */
59 MAD_F(0x10101010), /* 2^8 / (2^8 - 1) == 1.00392156862745 */
60 MAD_F(0x10080402), /* 2^9 / (2^9 - 1) == 1.00195694716243 */
61 MAD_F(0x10040100), /* 2^10 / (2^10 - 1) == 1.00097751710655 */
62 MAD_F(0x10020040), /* 2^11 / (2^11 - 1) == 1.00048851978505 */
63 MAD_F(0x10010010), /* 2^12 / (2^12 - 1) == 1.00024420024420 */
64 MAD_F(0x10008004), /* 2^13 / (2^13 - 1) == 1.00012208521548 */
65 MAD_F(0x10004001), /* 2^14 / (2^14 - 1) == 1.00006103888177 */
66 MAD_F(0x10002000) /* 2^15 / (2^15 - 1) == 1.00003051850948 */
70 * NAME: I_sample()
71 * DESCRIPTION: decode one requantized Layer I sample from a bitstream
73 static
74 mad_fixed_t I_sample(struct mad_bitptr *ptr, u32 nb)
76 mad_fixed_t sample;
78 sample = mad_bit_read(ptr, nb);
80 /* invert most significant bit, extend sign, then scale to fixed format */
82 sample ^= 1 << (nb - 1);
83 sample |= -(sample & (1 << (nb - 1)));
85 sample <<= MAD_F_FRACBITS - (nb - 1);
87 /* requantize the sample */
89 /* s'' = (2^nb / (2^nb - 1)) * (s''' + 2^(-nb + 1)) */
91 sample += MAD_F_ONE >> (nb - 1);
93 return mad_f_mul(sample, linear_table[nb - 2]);
95 /* s' = factor * s'' */
96 /* (to be performed by caller) */
100 * NAME: layer->I()
101 * DESCRIPTION: decode a single Layer I frame
103 s32 mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
105 struct mad_header *header = &frame->header;
106 u32 nch, bound, ch, s, sb, nb;
107 u8 allocation[2][32], scalefactor[2][32];
109 nch = MAD_NCHANNELS(header);
111 bound = 32;
112 if (header->mode == MAD_MODE_JOINT_STEREO) {
113 header->flags |= MAD_FLAG_I_STEREO;
114 bound = 4 + header->mode_extension * 4;
117 /* check CRC word */
119 if (header->flags & MAD_FLAG_PROTECTION) {
120 header->crc_check =
121 mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)),
122 header->crc_check);
124 if (header->crc_check != header->crc_target &&
125 !(frame->options & MAD_OPTION_IGNORECRC)) {
126 stream->error = MAD_ERROR_BADCRC;
127 return -1;
131 /* decode bit allocations */
133 for (sb = 0; sb < bound; ++sb) {
134 for (ch = 0; ch < nch; ++ch) {
135 nb = mad_bit_read(&stream->ptr, 4);
137 if (nb == 15) {
138 stream->error = MAD_ERROR_BADBITALLOC;
139 return -1;
142 allocation[ch][sb] = nb ? nb + 1 : 0;
146 for (sb = bound; sb < 32; ++sb) {
147 nb = mad_bit_read(&stream->ptr, 4);
149 if (nb == 15) {
150 stream->error = MAD_ERROR_BADBITALLOC;
151 return -1;
154 allocation[0][sb] =
155 allocation[1][sb] = nb ? nb + 1 : 0;
158 /* decode scalefactors */
160 for (sb = 0; sb < 32; ++sb) {
161 for (ch = 0; ch < nch; ++ch) {
162 if (allocation[ch][sb]) {
163 scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6);
165 # if defined(OPT_STRICT)
167 * Scalefactor index 63 does not appear in Table B.1 of
168 * ISO/IEC 11172-3. Nonetheless, other implementations accept it,
169 * so we only reject it if OPT_STRICT is defined.
171 if (scalefactor[ch][sb] == 63) {
172 stream->error = MAD_ERROR_BADSCALEFACTOR;
173 return -1;
175 # endif
180 /* decode samples */
182 for (s = 0; s < 12; ++s) {
183 for (sb = 0; sb < bound; ++sb) {
184 for (ch = 0; ch < nch; ++ch) {
185 nb = allocation[ch][sb];
186 frame->sbsample[ch][s][sb] = nb ?
187 mad_f_mul(I_sample(&stream->ptr, nb),
188 sf_table[scalefactor[ch][sb]]) : 0;
192 for (sb = bound; sb < 32; ++sb) {
193 if ((nb = allocation[0][sb])) {
194 mad_fixed_t sample;
196 sample = I_sample(&stream->ptr, nb);
198 for (ch = 0; ch < nch; ++ch) {
199 frame->sbsample[ch][s][sb] =
200 mad_f_mul(sample, sf_table[scalefactor[ch][sb]]);
203 else {
204 for (ch = 0; ch < nch; ++ch)
205 frame->sbsample[ch][s][sb] = 0;
210 return 0;
213 /* --- Layer II ------------------------------------------------------------ */
215 /* possible quantization per subband table */
216 static
217 struct {
218 u32 sblimit;
219 u8 const offsets[30];
220 } const sbquant_table[5] = {
221 /* ISO/IEC 11172-3 Table B.2a */
222 { 27, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 0 */
223 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0 } },
224 /* ISO/IEC 11172-3 Table B.2b */
225 { 30, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 1 */
226 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 } },
227 /* ISO/IEC 11172-3 Table B.2c */
228 { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } }, /* 2 */
229 /* ISO/IEC 11172-3 Table B.2d */
230 { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }, /* 3 */
231 /* ISO/IEC 13818-3 Table B.1 */
232 { 30, { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, /* 4 */
233 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }
236 /* bit allocation table */
237 static
238 struct {
239 u16 nbal;
240 u16 offset;
241 } const bitalloc_table[8] = {
242 { 2, 0 }, /* 0 */
243 { 2, 3 }, /* 1 */
244 { 3, 3 }, /* 2 */
245 { 3, 1 }, /* 3 */
246 { 4, 2 }, /* 4 */
247 { 4, 3 }, /* 5 */
248 { 4, 4 }, /* 6 */
249 { 4, 5 } /* 7 */
252 /* offsets into quantization class table */
253 static
254 u8 const offset_table[6][15] = {
255 { 0, 1, 16 }, /* 0 */
256 { 0, 1, 2, 3, 4, 5, 16 }, /* 1 */
257 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, /* 2 */
258 { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, /* 3 */
259 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16 }, /* 4 */
260 { 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } /* 5 */
263 /* quantization class table */
264 static
265 struct quantclass {
266 u16 nlevels;
267 u8 group;
268 u8 bits;
269 mad_fixed_t C;
270 mad_fixed_t D;
271 } const qc_table[17] = {
272 # include "qc_table.dat"
276 * NAME: II_samples()
277 * DESCRIPTION: decode three requantized Layer II samples from a bitstream
279 static
280 void II_samples(struct mad_bitptr *ptr,
281 struct quantclass const *quantclass,
282 mad_fixed_t output[3])
284 u32 nb, s, sample[3];
286 if ((nb = quantclass->group)) {
287 u32 c, nlevels;
289 /* degrouping */
290 c = mad_bit_read(ptr, quantclass->bits);
291 nlevels = quantclass->nlevels;
293 for (s = 0; s < 3; ++s) {
294 sample[s] = c % nlevels;
295 c /= nlevels;
298 else {
299 nb = quantclass->bits;
301 for (s = 0; s < 3; ++s)
302 sample[s] = mad_bit_read(ptr, nb);
305 for (s = 0; s < 3; ++s) {
306 mad_fixed_t requantized;
308 /* invert most significant bit, extend sign, then scale to fixed format */
310 requantized = sample[s] ^ (1 << (nb - 1));
311 requantized |= -(requantized & (1 << (nb - 1)));
313 requantized <<= MAD_F_FRACBITS - (nb - 1);
315 /* requantize the sample */
317 /* s'' = C * (s''' + D) */
319 output[s] = mad_f_mul(requantized + quantclass->D, quantclass->C);
321 /* s' = factor * s'' */
322 /* (to be performed by caller) */
327 * NAME: layer->II()
328 * DESCRIPTION: decode a single Layer II frame
330 s32 mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
332 struct mad_header *header = &frame->header;
333 struct mad_bitptr start;
334 u32 index, sblimit, nbal, nch, bound, gr, ch, s, sb;
335 u8 const *offsets;
336 u8 allocation[2][32], scfsi[2][32], scalefactor[2][32][3];
337 mad_fixed_t samples[3];
339 nch = MAD_NCHANNELS(header);
341 if (header->flags & MAD_FLAG_LSF_EXT)
342 index = 4;
343 else {
344 switch (nch == 2 ? header->bitrate / 2 : header->bitrate) {
345 case 32000:
346 case 48000:
347 index = (header->samplerate == 32000) ? 3 : 2;
348 break;
350 case 56000:
351 case 64000:
352 case 80000:
353 index = 0;
354 break;
356 default:
357 index = (header->samplerate == 48000) ? 0 : 1;
361 sblimit = sbquant_table[index].sblimit;
362 offsets = sbquant_table[index].offsets;
364 bound = 32;
365 if (header->mode == MAD_MODE_JOINT_STEREO) {
366 header->flags |= MAD_FLAG_I_STEREO;
367 bound = 4 + header->mode_extension * 4;
370 if (bound > sblimit)
371 bound = sblimit;
373 start = stream->ptr;
375 /* decode bit allocations */
377 for (sb = 0; sb < bound; ++sb) {
378 nbal = bitalloc_table[offsets[sb]].nbal;
380 for (ch = 0; ch < nch; ++ch)
381 allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal);
384 for (sb = bound; sb < sblimit; ++sb) {
385 nbal = bitalloc_table[offsets[sb]].nbal;
387 allocation[0][sb] =
388 allocation[1][sb] = mad_bit_read(&stream->ptr, nbal);
391 /* decode scalefactor selection info */
393 for (sb = 0; sb < sblimit; ++sb) {
394 for (ch = 0; ch < nch; ++ch) {
395 if (allocation[ch][sb])
396 scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
400 /* check CRC word */
402 if (header->flags & MAD_FLAG_PROTECTION) {
403 header->crc_check =
404 mad_bit_crc(start, mad_bit_length(&start, &stream->ptr),
405 header->crc_check);
407 if (header->crc_check != header->crc_target &&
408 !(frame->options & MAD_OPTION_IGNORECRC)) {
409 stream->error = MAD_ERROR_BADCRC;
410 return -1;
414 /* decode scalefactors */
416 for (sb = 0; sb < sblimit; ++sb) {
417 for (ch = 0; ch < nch; ++ch) {
418 if (allocation[ch][sb]) {
419 scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);
421 switch (scfsi[ch][sb]) {
422 case 2:
423 scalefactor[ch][sb][2] =
424 scalefactor[ch][sb][1] =
425 scalefactor[ch][sb][0];
426 break;
428 case 0:
429 scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
430 /* fall through */
432 case 1:
433 case 3:
434 scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
437 if (scfsi[ch][sb] & 1)
438 scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1];
440 # if defined(OPT_STRICT)
442 * Scalefactor index 63 does not appear in Table B.1 of
443 * ISO/IEC 11172-3. Nonetheless, other implementations accept it,
444 * so we only reject it if OPT_STRICT is defined.
446 if (scalefactor[ch][sb][0] == 63 ||
447 scalefactor[ch][sb][1] == 63 ||
448 scalefactor[ch][sb][2] == 63) {
449 stream->error = MAD_ERROR_BADSCALEFACTOR;
450 return -1;
452 # endif
457 /* decode samples */
459 for (gr = 0; gr < 12; ++gr) {
460 for (sb = 0; sb < bound; ++sb) {
461 for (ch = 0; ch < nch; ++ch) {
462 if ((index = allocation[ch][sb])) {
463 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
465 II_samples(&stream->ptr, &qc_table[index], samples);
467 for (s = 0; s < 3; ++s) {
468 frame->sbsample[ch][3 * gr + s][sb] =
469 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
472 else {
473 for (s = 0; s < 3; ++s)
474 frame->sbsample[ch][3 * gr + s][sb] = 0;
479 for (sb = bound; sb < sblimit; ++sb) {
480 if ((index = allocation[0][sb])) {
481 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
483 II_samples(&stream->ptr, &qc_table[index], samples);
485 for (ch = 0; ch < nch; ++ch) {
486 for (s = 0; s < 3; ++s) {
487 frame->sbsample[ch][3 * gr + s][sb] =
488 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
492 else {
493 for (ch = 0; ch < nch; ++ch) {
494 for (s = 0; s < 3; ++s)
495 frame->sbsample[ch][3 * gr + s][sb] = 0;
500 for (ch = 0; ch < nch; ++ch) {
501 for (s = 0; s < 3; ++s) {
502 for (sb = sblimit; sb < 32; ++sb)
503 frame->sbsample[ch][3 * gr + s][sb] = 0;
508 return 0;