layer12.c

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

Generated on Tue Dec 13 14:47:48 2005 for guliverkli by  doxygen 1.4.5