streo mp3 player see: http://mbed.org/users/okini3939/notebook/I2S_AUDIO
Dependencies: FatFileSystemCpp I2SSlave TLV320 mbed
Fork of madplayer by
layer12.cpp
00001 /* 00002 * libmad - MPEG audio decoder library 00003 * Copyright (C) 2000-2004 Underbit Technologies, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 * 00019 * $Id: layer12.c,v 1.1 2010/11/23 20:12:57 andy Exp $ 00020 */ 00021 00022 # include "config.h" 00023 00024 # include "global.h" 00025 00026 # ifdef HAVE_LIMITS_H 00027 # include <limits.h> 00028 # else 00029 # define CHAR_BIT 8 00030 # endif 00031 00032 # include "fixed.h" 00033 # include "bit.h" 00034 # include "stream.h" 00035 # include "frame.h" 00036 # include "layer12.h" 00037 00038 /* 00039 * scalefactor table 00040 * used in both Layer I and Layer II decoding 00041 */ 00042 # include "sf_table.h" 00043 00044 /* --- Layer I ------------------------------------------------------------- */ 00045 00046 /* linear scaling table */ 00047 static 00048 mad_fixed_t const linear_table[14] = { 00049 MAD_F(0x15555555), /* 2^2 / (2^2 - 1) == 1.33333333333333 */ 00050 MAD_F(0x12492492), /* 2^3 / (2^3 - 1) == 1.14285714285714 */ 00051 MAD_F(0x11111111), /* 2^4 / (2^4 - 1) == 1.06666666666667 */ 00052 MAD_F(0x10842108), /* 2^5 / (2^5 - 1) == 1.03225806451613 */ 00053 MAD_F(0x10410410), /* 2^6 / (2^6 - 1) == 1.01587301587302 */ 00054 MAD_F(0x10204081), /* 2^7 / (2^7 - 1) == 1.00787401574803 */ 00055 MAD_F(0x10101010), /* 2^8 / (2^8 - 1) == 1.00392156862745 */ 00056 MAD_F(0x10080402), /* 2^9 / (2^9 - 1) == 1.00195694716243 */ 00057 MAD_F(0x10040100), /* 2^10 / (2^10 - 1) == 1.00097751710655 */ 00058 MAD_F(0x10020040), /* 2^11 / (2^11 - 1) == 1.00048851978505 */ 00059 MAD_F(0x10010010), /* 2^12 / (2^12 - 1) == 1.00024420024420 */ 00060 MAD_F(0x10008004), /* 2^13 / (2^13 - 1) == 1.00012208521548 */ 00061 MAD_F(0x10004001), /* 2^14 / (2^14 - 1) == 1.00006103888177 */ 00062 MAD_F(0x10002000) /* 2^15 / (2^15 - 1) == 1.00003051850948 */ 00063 }; 00064 00065 /* 00066 * NAME: I_sample() 00067 * DESCRIPTION: decode one requantized Layer I sample from a bitstream 00068 */ 00069 static 00070 mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb) 00071 { 00072 mad_fixed_t sample; 00073 00074 sample = mad_bit_read(ptr, nb); 00075 00076 /* invert most significant bit, extend sign, then scale to fixed format */ 00077 00078 sample ^= 1 << (nb - 1); 00079 sample |= -(sample & (1 << (nb - 1))); 00080 00081 sample <<= MAD_F_FRACBITS - (nb - 1); 00082 00083 /* requantize the sample */ 00084 00085 /* s'' = (2^nb / (2^nb - 1)) * (s''' + 2^(-nb + 1)) */ 00086 00087 sample += MAD_F_ONE >> (nb - 1); 00088 00089 return mad_f_mul(sample, linear_table[nb - 2]); 00090 00091 /* s' = factor * s'' */ 00092 /* (to be performed by caller) */ 00093 } 00094 00095 /* 00096 * NAME: layer->I() 00097 * DESCRIPTION: decode a single Layer I frame 00098 */ 00099 int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame) 00100 { 00101 struct mad_header *header = &frame->header; 00102 unsigned int nch, bound, ch, s, sb, nb; 00103 unsigned char allocation[2][32], scalefactor[2][32]; 00104 00105 nch = MAD_NCHANNELS(header); 00106 00107 bound = 32; 00108 if (header->mode == MAD_MODE_JOINT_STEREO) { 00109 header->flags |= MAD_FLAG_I_STEREO; 00110 bound = 4 + header->mode_extension * 4; 00111 } 00112 00113 /* check CRC word */ 00114 00115 if (header->flags & MAD_FLAG_PROTECTION) { 00116 header->crc_check = 00117 mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)), 00118 header->crc_check); 00119 00120 if (header->crc_check != header->crc_target && 00121 !(frame->options & MAD_OPTION_IGNORECRC)) { 00122 stream->error = MAD_ERROR_BADCRC; 00123 return -1; 00124 } 00125 } 00126 00127 /* decode bit allocations */ 00128 00129 for (sb = 0; sb < bound; ++sb) { 00130 for (ch = 0; ch < nch; ++ch) { 00131 nb = mad_bit_read(&stream->ptr, 4); 00132 00133 if (nb == 15) { 00134 stream->error = MAD_ERROR_BADBITALLOC; 00135 return -1; 00136 } 00137 00138 allocation[ch][sb] = nb ? nb + 1 : 0; 00139 } 00140 } 00141 00142 for (sb = bound; sb < 32; ++sb) { 00143 nb = mad_bit_read(&stream->ptr, 4); 00144 00145 if (nb == 15) { 00146 stream->error = MAD_ERROR_BADBITALLOC; 00147 return -1; 00148 } 00149 00150 allocation[0][sb] = 00151 allocation[1][sb] = nb ? nb + 1 : 0; 00152 } 00153 00154 /* decode scalefactors */ 00155 00156 for (sb = 0; sb < 32; ++sb) { 00157 for (ch = 0; ch < nch; ++ch) { 00158 if (allocation[ch][sb]) { 00159 scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6); 00160 00161 # if defined(OPT_STRICT) 00162 /* 00163 * Scalefactor index 63 does not appear in Table B.1 of 00164 * ISO/IEC 11172-3. Nonetheless, other implementations accept it, 00165 * so we only reject it if OPT_STRICT is defined. 00166 */ 00167 if (scalefactor[ch][sb] == 63) { 00168 stream->error = MAD_ERROR_BADSCALEFACTOR; 00169 return -1; 00170 } 00171 # endif 00172 } 00173 } 00174 } 00175 00176 /* decode samples */ 00177 00178 for (s = 0; s < 12; ++s) { 00179 for (sb = 0; sb < bound; ++sb) { 00180 for (ch = 0; ch < nch; ++ch) { 00181 nb = allocation[ch][sb]; 00182 frame->sbsample[ch][s][sb] = nb ? 00183 mad_f_mul(I_sample(&stream->ptr, nb), 00184 sf_table[scalefactor[ch][sb]]) : 0; 00185 } 00186 } 00187 00188 for (sb = bound; sb < 32; ++sb) { 00189 if ((nb = allocation[0][sb]) != 0) { 00190 mad_fixed_t sample; 00191 00192 sample = I_sample(&stream->ptr, nb); 00193 00194 for (ch = 0; ch < nch; ++ch) { 00195 frame->sbsample[ch][s][sb] = 00196 mad_f_mul(sample, sf_table[scalefactor[ch][sb]]); 00197 } 00198 } 00199 else { 00200 for (ch = 0; ch < nch; ++ch) 00201 frame->sbsample[ch][s][sb] = 0; 00202 } 00203 } 00204 } 00205 00206 return 0; 00207 } 00208 00209 /* --- Layer II ------------------------------------------------------------ */ 00210 00211 /* possible quantization per subband table */ 00212 static 00213 const struct { 00214 unsigned int sblimit; 00215 unsigned char offsets[30]; 00216 } sbquant_table[5] = { 00217 /* ISO/IEC 11172-3 Table B.2a */ 00218 { 27, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 0 */ 00219 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0 } }, 00220 /* ISO/IEC 11172-3 Table B.2b */ 00221 { 30, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 1 */ 00222 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 } }, 00223 /* ISO/IEC 11172-3 Table B.2c */ 00224 { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } }, /* 2 */ 00225 /* ISO/IEC 11172-3 Table B.2d */ 00226 { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }, /* 3 */ 00227 /* ISO/IEC 13818-3 Table B.1 */ 00228 { 30, { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, /* 4 */ 00229 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } } 00230 }; 00231 00232 /* bit allocation table */ 00233 static 00234 struct { 00235 unsigned short nbal; 00236 unsigned short offset; 00237 } const bitalloc_table[8] = { 00238 { 2, 0 }, /* 0 */ 00239 { 2, 3 }, /* 1 */ 00240 { 3, 3 }, /* 2 */ 00241 { 3, 1 }, /* 3 */ 00242 { 4, 2 }, /* 4 */ 00243 { 4, 3 }, /* 5 */ 00244 { 4, 4 }, /* 6 */ 00245 { 4, 5 } /* 7 */ 00246 }; 00247 00248 /* offsets into quantization class table */ 00249 static 00250 unsigned char const offset_table[6][15] = { 00251 { 0, 1, 16 }, /* 0 */ 00252 { 0, 1, 2, 3, 4, 5, 16 }, /* 1 */ 00253 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, /* 2 */ 00254 { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, /* 3 */ 00255 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16 }, /* 4 */ 00256 { 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } /* 5 */ 00257 }; 00258 00259 /* quantization class table */ 00260 static 00261 struct quantclass { 00262 unsigned short nlevels; 00263 unsigned char group; 00264 unsigned char bits; 00265 mad_fixed_t C; 00266 mad_fixed_t D; 00267 } const qc_table[17] = { 00268 # include "qc_table.h" 00269 }; 00270 00271 /* 00272 * NAME: II_samples() 00273 * DESCRIPTION: decode three requantized Layer II samples from a bitstream 00274 */ 00275 static 00276 void II_samples(struct mad_bitptr *ptr, 00277 struct quantclass const *quantclass, 00278 mad_fixed_t output[3]) 00279 { 00280 unsigned int nb, s, sample[3]; 00281 00282 if ((nb = quantclass->group) != 0) { 00283 unsigned int c, nlevels; 00284 00285 /* degrouping */ 00286 c = mad_bit_read(ptr, quantclass->bits); 00287 nlevels = quantclass->nlevels; 00288 00289 for (s = 0; s < 3; ++s) { 00290 sample[s] = c % nlevels; 00291 c /= nlevels; 00292 } 00293 } 00294 else { 00295 nb = quantclass->bits; 00296 00297 for (s = 0; s < 3; ++s) 00298 sample[s] = mad_bit_read(ptr, nb); 00299 } 00300 00301 for (s = 0; s < 3; ++s) { 00302 mad_fixed_t requantized; 00303 00304 /* invert most significant bit, extend sign, then scale to fixed format */ 00305 00306 requantized = sample[s] ^ (1 << (nb - 1)); 00307 requantized |= -(requantized & (1 << (nb - 1))); 00308 00309 requantized <<= MAD_F_FRACBITS - (nb - 1); 00310 00311 /* requantize the sample */ 00312 00313 /* s'' = C * (s''' + D) */ 00314 00315 output[s] = mad_f_mul(requantized + quantclass->D, quantclass->C); 00316 00317 /* s' = factor * s'' */ 00318 /* (to be performed by caller) */ 00319 } 00320 } 00321 00322 /* 00323 * NAME: layer->II() 00324 * DESCRIPTION: decode a single Layer II frame 00325 */ 00326 int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame) 00327 { 00328 struct mad_header *header = &frame->header; 00329 struct mad_bitptr start; 00330 unsigned int index, sblimit, nbal, nch, bound, gr, ch, s, sb; 00331 unsigned char const *offsets; 00332 unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3]; 00333 mad_fixed_t samples[3]; 00334 00335 nch = MAD_NCHANNELS(header); 00336 00337 if (header->flags & MAD_FLAG_LSF_EXT) 00338 index = 4; 00339 else if (header->flags & MAD_FLAG_FREEFORMAT) 00340 goto freeformat; 00341 else { 00342 unsigned long bitrate_per_channel; 00343 00344 bitrate_per_channel = header->bitrate; 00345 if (nch == 2) { 00346 bitrate_per_channel /= 2; 00347 00348 # if defined(OPT_STRICT) 00349 /* 00350 * ISO/IEC 11172-3 allows only single channel mode for 32, 48, 56, and 00351 * 80 kbps bitrates in Layer II, but some encoders ignore this 00352 * restriction. We enforce it if OPT_STRICT is defined. 00353 */ 00354 if (bitrate_per_channel <= 28000 || bitrate_per_channel == 40000) { 00355 stream->error = MAD_ERROR_BADMODE; 00356 return -1; 00357 } 00358 # endif 00359 } 00360 else { /* nch == 1 */ 00361 if (bitrate_per_channel > 192000) { 00362 /* 00363 * ISO/IEC 11172-3 does not allow single channel mode for 224, 256, 00364 * 320, or 384 kbps bitrates in Layer II. 00365 */ 00366 stream->error = MAD_ERROR_BADMODE; 00367 return -1; 00368 } 00369 } 00370 00371 if (bitrate_per_channel <= 48000) 00372 index = (header->samplerate == 32000) ? 3 : 2; 00373 else if (bitrate_per_channel <= 80000) 00374 index = 0; 00375 else { 00376 freeformat: 00377 index = (header->samplerate == 48000) ? 0 : 1; 00378 } 00379 } 00380 00381 sblimit = sbquant_table[index].sblimit; 00382 offsets = sbquant_table[index].offsets; 00383 00384 bound = 32; 00385 if (header->mode == MAD_MODE_JOINT_STEREO) { 00386 header->flags |= MAD_FLAG_I_STEREO; 00387 bound = 4 + header->mode_extension * 4; 00388 } 00389 00390 if (bound > sblimit) 00391 bound = sblimit; 00392 00393 start = stream->ptr; 00394 00395 /* decode bit allocations */ 00396 00397 for (sb = 0; sb < bound; ++sb) { 00398 nbal = bitalloc_table[offsets[sb]].nbal; 00399 00400 for (ch = 0; ch < nch; ++ch) 00401 allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal); 00402 } 00403 00404 for (sb = bound; sb < sblimit; ++sb) { 00405 nbal = bitalloc_table[offsets[sb]].nbal; 00406 00407 allocation[0][sb] = 00408 allocation[1][sb] = mad_bit_read(&stream->ptr, nbal); 00409 } 00410 00411 /* decode scalefactor selection info */ 00412 00413 for (sb = 0; sb < sblimit; ++sb) { 00414 for (ch = 0; ch < nch; ++ch) { 00415 if (allocation[ch][sb]) 00416 scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2); 00417 } 00418 } 00419 00420 /* check CRC word */ 00421 00422 if (header->flags & MAD_FLAG_PROTECTION) { 00423 header->crc_check = 00424 mad_bit_crc(start, mad_bit_length(&start, &stream->ptr), 00425 header->crc_check); 00426 00427 if (header->crc_check != header->crc_target && 00428 !(frame->options & MAD_OPTION_IGNORECRC)) { 00429 stream->error = MAD_ERROR_BADCRC; 00430 return -1; 00431 } 00432 } 00433 00434 /* decode scalefactors */ 00435 00436 for (sb = 0; sb < sblimit; ++sb) { 00437 for (ch = 0; ch < nch; ++ch) { 00438 if (allocation[ch][sb]) { 00439 scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6); 00440 00441 switch (scfsi[ch][sb]) { 00442 case 2: 00443 scalefactor[ch][sb][2] = 00444 scalefactor[ch][sb][1] = 00445 scalefactor[ch][sb][0]; 00446 break; 00447 00448 case 0: 00449 scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6); 00450 /* fall through */ 00451 00452 case 1: 00453 case 3: 00454 scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6); 00455 } 00456 00457 if (scfsi[ch][sb] & 1) 00458 scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1]; 00459 00460 # if defined(OPT_STRICT) 00461 /* 00462 * Scalefactor index 63 does not appear in Table B.1 of 00463 * ISO/IEC 11172-3. Nonetheless, other implementations accept it, 00464 * so we only reject it if OPT_STRICT is defined. 00465 */ 00466 if (scalefactor[ch][sb][0] == 63 || 00467 scalefactor[ch][sb][1] == 63 || 00468 scalefactor[ch][sb][2] == 63) { 00469 stream->error = MAD_ERROR_BADSCALEFACTOR; 00470 return -1; 00471 } 00472 # endif 00473 } 00474 } 00475 } 00476 00477 /* decode samples */ 00478 00479 for (gr = 0; gr < 12; ++gr) { 00480 for (sb = 0; sb < bound; ++sb) { 00481 for (ch = 0; ch < nch; ++ch) { 00482 if ((index = allocation[ch][sb]) != 0) { 00483 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1]; 00484 00485 II_samples(&stream->ptr, &qc_table[index], samples); 00486 00487 for (s = 0; s < 3; ++s) { 00488 frame->sbsample[ch][3 * gr + s][sb] = 00489 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]); 00490 } 00491 } 00492 else { 00493 for (s = 0; s < 3; ++s) 00494 frame->sbsample[ch][3 * gr + s][sb] = 0; 00495 } 00496 } 00497 } 00498 00499 for (sb = bound; sb < sblimit; ++sb) { 00500 if ((index = allocation[0][sb])!= 0) { 00501 index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1]; 00502 00503 II_samples(&stream->ptr, &qc_table[index], samples); 00504 00505 for (ch = 0; ch < nch; ++ch) { 00506 for (s = 0; s < 3; ++s) { 00507 frame->sbsample[ch][3 * gr + s][sb] = 00508 mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]); 00509 } 00510 } 00511 } 00512 else { 00513 for (ch = 0; ch < nch; ++ch) { 00514 for (s = 0; s < 3; ++s) 00515 frame->sbsample[ch][3 * gr + s][sb] = 0; 00516 } 00517 } 00518 } 00519 00520 for (ch = 0; ch < nch; ++ch) { 00521 for (s = 0; s < 3; ++s) { 00522 for (sb = sblimit; sb < 32; ++sb) 00523 frame->sbsample[ch][3 * gr + s][sb] = 0; 00524 } 00525 } 00526 } 00527 00528 return 0; 00529 }
Generated on Tue Jul 12 2022 22:28:17 by 1.7.2