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