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