45mad_fixed_t
const sf_table[64] = {
46# include "sf_table.dat"
53mad_fixed_t
const linear_table[14] = {
75mad_fixed_t I_sample(
struct mad_bitptr *ptr,
unsigned int nb)
79 sample = mad_bit_read(ptr, nb);
83 sample ^= 1 << (nb - 1);
84 sample |= -(sample & (1 << (nb - 1)));
86 sample <<= MAD_F_FRACBITS - (nb - 1);
92 sample += MAD_F_ONE >> (nb - 1);
94 return mad_f_mul(sample, linear_table[nb - 2]);
107 unsigned int nch, bound, ch, s, sb, nb;
108 unsigned char allocation[2][32], scalefactor[2][32];
110 nch = MAD_NCHANNELS(header);
113 if (header->mode == MAD_MODE_JOINT_STEREO) {
114 header->flags |= MAD_FLAG_I_STEREO;
115 bound = 4 + header->mode_extension * 4;
120 if (header->flags & MAD_FLAG_PROTECTION) {
122 mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)),
125 if (header->crc_check != header->crc_target &&
126 !(frame->options & MAD_OPTION_IGNORECRC)) {
127 stream->error = MAD_ERROR_BADCRC;
134 for (sb = 0; sb < bound; ++sb) {
135 for (ch = 0; ch < nch; ++ch) {
136 nb = mad_bit_read(&stream->ptr, 4);
139 stream->error = MAD_ERROR_BADBITALLOC;
143 allocation[ch][sb] = nb ? nb + 1 : 0;
147 for (sb = bound; sb < 32; ++sb) {
148 nb = mad_bit_read(&stream->ptr, 4);
151 stream->error = MAD_ERROR_BADBITALLOC;
156 allocation[1][sb] = nb ? nb + 1 : 0;
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);
166# if defined(OPT_STRICT)
172 if (scalefactor[ch][sb] == 63) {
173 stream->error = MAD_ERROR_BADSCALEFACTOR;
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;
193 for (sb = bound; sb < 32; ++sb) {
194 if ((nb = allocation[0][sb])) {
197 sample = I_sample(&stream->ptr, nb);
199 for (ch = 0; ch < nch; ++ch) {
200 frame->sbsample[ch][s][sb] =
201 mad_f_mul(sample, sf_table[scalefactor[ch][sb]]);
205 for (ch = 0; ch < nch; ++ch)
206 frame->sbsample[ch][s][sb] = 0;
219 unsigned int sblimit;
220 unsigned char const offsets[30];
221}
const sbquant_table[5] = {
223 { 27, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3,
224 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0 } },
226 { 30, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3,
227 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 } },
229 { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } },
231 { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } },
233 { 30, { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
234 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }
241 unsigned short offset;
242}
const bitalloc_table[8] = {
255unsigned char const offset_table[6][15] = {
257 { 0, 1, 2, 3, 4, 5, 16 },
258 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 },
259 { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
260 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16 },
261 { 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
267 unsigned short nlevels;
272}
const qc_table[17] = {
273# include "qc_table.dat"
282 struct quantclass
const *quantclass,
283 mad_fixed_t output[3])
285 unsigned int nb, s, sample[3];
287 if ((nb = quantclass->group)) {
288 unsigned int c, nlevels;
291 c = mad_bit_read(ptr, quantclass->bits);
292 nlevels = quantclass->nlevels;
294 for (s = 0; s < 3; ++s) {
295 sample[s] = c % nlevels;
300 nb = quantclass->bits;
302 for (s = 0; s < 3; ++s)
303 sample[s] = mad_bit_read(ptr, nb);
306 for (s = 0; s < 3; ++s) {
307 mad_fixed_t requantized;
311 requantized = sample[s] ^ (1 << (nb - 1));
312 requantized |= -(requantized & (1 << (nb - 1)));
314 requantized <<= MAD_F_FRACBITS - (nb - 1);
320 output[s] = mad_f_mul(requantized + quantclass->D, quantclass->C);
335 unsigned int index, sblimit, nbal, nch, bound, gr, ch, s, sb;
336 unsigned char const *offsets;
337 unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3];
338 mad_fixed_t samples[3];
340 nch = MAD_NCHANNELS(header);
342 if (header->flags & MAD_FLAG_LSF_EXT)
344 else if (header->flags & MAD_FLAG_FREEFORMAT)
347 unsigned long bitrate_per_channel;
349 bitrate_per_channel = header->bitrate;
351 bitrate_per_channel /= 2;
353# if defined(OPT_STRICT)
359 if (bitrate_per_channel <= 28000 || bitrate_per_channel == 40000) {
360 stream->error = MAD_ERROR_BADMODE;
366 if (bitrate_per_channel > 192000) {
371 stream->error = MAD_ERROR_BADMODE;
376 if (bitrate_per_channel <= 48000)
377 index = (header->samplerate == 32000) ? 3 : 2;
378 else if (bitrate_per_channel <= 80000)
382 index = (header->samplerate == 48000) ? 0 : 1;
386 sblimit = sbquant_table[index].sblimit;
387 offsets = sbquant_table[index].offsets;
390 if (header->mode == MAD_MODE_JOINT_STEREO) {
391 header->flags |= MAD_FLAG_I_STEREO;
392 bound = 4 + header->mode_extension * 4;
402 for (sb = 0; sb < bound; ++sb) {
403 nbal = bitalloc_table[offsets[sb]].nbal;
405 for (ch = 0; ch < nch; ++ch)
406 allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal);
409 for (sb = bound; sb < sblimit; ++sb) {
410 nbal = bitalloc_table[offsets[sb]].nbal;
413 allocation[1][sb] = mad_bit_read(&stream->ptr, nbal);
418 for (sb = 0; sb < sblimit; ++sb) {
419 for (ch = 0; ch < nch; ++ch) {
420 if (allocation[ch][sb])
421 scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
427 if (header->flags & MAD_FLAG_PROTECTION) {
429 mad_bit_crc(start, mad_bit_length(&start, &stream->ptr),
432 if (header->crc_check != header->crc_target &&
433 !(frame->options & MAD_OPTION_IGNORECRC)) {
434 stream->error = MAD_ERROR_BADCRC;
441 for (sb = 0; sb < sblimit; ++sb) {
442 for (ch = 0; ch < nch; ++ch) {
443 if (allocation[ch][sb]) {
444 scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);
446 switch (scfsi[ch][sb]) {
448 scalefactor[ch][sb][2] =
449 scalefactor[ch][sb][1] =
450 scalefactor[ch][sb][0];
454 scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
459 scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
462 if (scfsi[ch][sb] & 1)
463 scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1];
465# if defined(OPT_STRICT)
471 if (scalefactor[ch][sb][0] == 63 ||
472 scalefactor[ch][sb][1] == 63 ||
473 scalefactor[ch][sb][2] == 63) {
474 stream->error = MAD_ERROR_BADSCALEFACTOR;
484 for (gr = 0; gr < 12; ++gr) {
485 for (sb = 0; sb < bound; ++sb) {
486 for (ch = 0; ch < nch; ++ch) {
487 if ((index = allocation[ch][sb])) {
488 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
490 II_samples(&stream->ptr, &qc_table[index], samples);
492 for (s = 0; s < 3; ++s) {
493 frame->sbsample[ch][3 * gr + s][sb] =
494 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
498 for (s = 0; s < 3; ++s)
499 frame->sbsample[ch][3 * gr + s][sb] = 0;
504 for (sb = bound; sb < sblimit; ++sb) {
505 if ((index = allocation[0][sb])) {
506 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
508 II_samples(&stream->ptr, &qc_table[index], samples);
510 for (ch = 0; ch < nch; ++ch) {
511 for (s = 0; s < 3; ++s) {
512 frame->sbsample[ch][3 * gr + s][sb] =
513 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
518 for (ch = 0; ch < nch; ++ch) {
519 for (s = 0; s < 3; ++s)
520 frame->sbsample[ch][3 * gr + s][sb] = 0;
525 for (ch = 0; ch < nch; ++ch) {
526 for (s = 0; s < 3; ++s) {
527 for (sb = sblimit; sb < 32; ++sb)
528 frame->sbsample[ch][3 * gr + s][sb] = 0;