rtoss - Diff between revs 14 and 26

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