2 * mad - MPEG audio decoder
3 * Copyright (C) 2000-2001 Robert Leslie
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.
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.
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
42 * used in both Layer I and Layer II decoding
45 mad_fixed_t
const sf_table
[63] = {
46 # include "sf_table.dat"
49 /* --- Layer I ------------------------------------------------------------- */
51 /* linear scaling table */
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 */
72 * DESCRIPTION: decode one requantized Layer I sample from a bitstream
75 mad_fixed_t
I_sample(struct mad_bitptr
*ptr
, unsigned int nb
)
79 sample
= mad_bit_read(ptr
, nb
);
81 /* invert most significant bit, extend sign, then scale to fixed format */
83 sample
^= 1 << (nb
- 1);
84 sample
|= -(sample
& (1 << (nb
- 1)));
86 sample
<<= MAD_F_FRACBITS
- (nb
- 1);
88 /* requantize the sample */
90 /* s'' = (2^nb / (2^nb - 1)) * (s''' + 2^(-nb + 1)) */
92 sample
+= MAD_F_ONE
>> (nb
- 1);
94 return mad_f_mul(sample
, linear_table
[nb
- 2]);
96 /* s' = factor * s'' */
97 /* (to be performed by caller) */
102 * DESCRIPTION: decode a single Layer I frame
104 int mad_layer_I(struct mad_stream
*stream
, struct mad_frame
*frame
)
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];
110 nch
= MAD_NCHANNELS(header
);
113 if (header
->mode
== MAD_MODE_JOINT_STEREO
) {
114 header
->flags
|= MAD_FLAG_I_STEREO
;
115 bound
= 4 + header
->mode_extension
* 4;
120 if (header
->flags
& MAD_FLAG_PROTECTION
) {
122 mad_bit_crc(stream
->ptr
, 4 * (bound
* nch
+ (32 - bound
)),
125 if (header
->crc_check
!= header
->crc_target
&&
126 !(frame
->options
& MAD_OPTION_IGNORECRC
)) {
127 stream
->error
= MAD_ERROR_BADCRC
;
132 /* decode bit allocations */
134 for (sb
= 0; sb
< bound
; ++sb
) {
135 for (ch
= 0; ch
< nch
; ++ch
) {
136 nb
= mad_bit_read(&stream
->ptr
, 4);
139 stream
->error
= MAD_ERROR_BADBITALLOC
;
143 allocation
[ch
][sb
] = nb
? nb
+ 1 : 0;
147 for (sb
= bound
; sb
< 32; ++sb
) {
148 nb
= mad_bit_read(&stream
->ptr
, 4);
151 stream
->error
= MAD_ERROR_BADBITALLOC
;
156 allocation
[1][sb
] = nb
? nb
+ 1 : 0;
159 /* decode scalefactors */
161 for (sb
= 0; sb
< 32; ++sb
) {
162 for (ch
= 0; ch
< nch
; ++ch
) {
163 if (allocation
[ch
][sb
]) {
164 scalefactor
[ch
][sb
] = mad_bit_read(&stream
->ptr
, 6);
166 if (scalefactor
[ch
][sb
] == 63) {
167 stream
->error
= MAD_ERROR_BADSCALEFACTOR
;
176 for (s
= 0; s
< 12; ++s
) {
177 for (sb
= 0; sb
< bound
; ++sb
) {
178 for (ch
= 0; ch
< nch
; ++ch
) {
179 nb
= allocation
[ch
][sb
];
180 frame
->sbsample
[ch
][s
][sb
] = nb
?
181 mad_f_mul(I_sample(&stream
->ptr
, nb
),
182 sf_table
[scalefactor
[ch
][sb
]]) : 0;
186 for (sb
= bound
; sb
< 32; ++sb
) {
187 if ((nb
= allocation
[0][sb
])) {
190 sample
= I_sample(&stream
->ptr
, nb
);
192 for (ch
= 0; ch
< nch
; ++ch
) {
193 frame
->sbsample
[ch
][s
][sb
] =
194 mad_f_mul(sample
, sf_table
[scalefactor
[ch
][sb
]]);
198 for (ch
= 0; ch
< nch
; ++ch
)
199 frame
->sbsample
[ch
][s
][sb
] = 0;
207 /* --- Layer II ------------------------------------------------------------ */
209 /* possible quantization per subband table */
212 unsigned int sblimit
;
213 unsigned char const offsets
[30];
214 } const sbquant_table
[5] = {
215 /* ISO/IEC 11172-3 Table B.2a */
216 { 27, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 0 */
217 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0 } },
218 /* ISO/IEC 11172-3 Table B.2b */
219 { 30, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 1 */
220 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 } },
221 /* ISO/IEC 11172-3 Table B.2c */
222 { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } }, /* 2 */
223 /* ISO/IEC 11172-3 Table B.2d */
224 { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }, /* 3 */
225 /* ISO/IEC 13818-3 Table B.1 */
226 { 30, { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, /* 4 */
227 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }
230 /* bit allocation table */
234 unsigned short offset
;
235 } const bitalloc_table
[8] = {
246 /* offsets into quantization class table */
248 unsigned char const offset_table
[6][15] = {
249 { 0, 1, 16 }, /* 0 */
250 { 0, 1, 2, 3, 4, 5, 16 }, /* 1 */
251 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, /* 2 */
252 { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, /* 3 */
253 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16 }, /* 4 */
254 { 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } /* 5 */
257 /* quantization class table */
260 unsigned short nlevels
;
265 } const qc_table
[17] = {
266 # include "qc_table.dat"
271 * DESCRIPTION: decode three requantized Layer II samples from a bitstream
274 void II_samples(struct mad_bitptr
*ptr
,
275 struct quantclass
const *quantclass
,
276 mad_fixed_t output
[3])
278 unsigned int nb
, s
, sample
[3];
280 if ((nb
= quantclass
->group
)) {
281 unsigned int c
, nlevels
;
284 c
= mad_bit_read(ptr
, quantclass
->bits
);
285 nlevels
= quantclass
->nlevels
;
287 for (s
= 0; s
< 3; ++s
) {
288 sample
[s
] = c
% nlevels
;
293 nb
= quantclass
->bits
;
295 for (s
= 0; s
< 3; ++s
)
296 sample
[s
] = mad_bit_read(ptr
, nb
);
299 for (s
= 0; s
< 3; ++s
) {
300 mad_fixed_t requantized
;
302 /* invert most significant bit, extend sign, then scale to fixed format */
304 requantized
= sample
[s
] ^ (1 << (nb
- 1));
305 requantized
|= -(requantized
& (1 << (nb
- 1)));
307 requantized
<<= MAD_F_FRACBITS
- (nb
- 1);
309 /* requantize the sample */
311 /* s'' = C * (s''' + D) */
313 output
[s
] = mad_f_mul(requantized
+ quantclass
->D
, quantclass
->C
);
315 /* s' = factor * s'' */
316 /* (to be performed by caller) */
322 * DESCRIPTION: decode a single Layer II frame
324 int mad_layer_II(struct mad_stream
*stream
, struct mad_frame
*frame
)
326 struct mad_header
*header
= &frame
->header
;
327 struct mad_bitptr start
;
328 unsigned int index
, sblimit
, nbal
, nch
, bound
, gr
, ch
, s
, sb
;
329 unsigned char const *offsets
;
330 unsigned char allocation
[2][32], scfsi
[2][32], scalefactor
[2][32][3];
331 mad_fixed_t samples
[3];
333 nch
= MAD_NCHANNELS(header
);
335 if (header
->flags
& MAD_FLAG_LSF_EXT
)
338 switch (nch
== 2 ? header
->bitrate
/ 2 : header
->bitrate
) {
341 index
= (header
->samplerate
== 32000) ? 3 : 2;
351 index
= (header
->samplerate
== 48000) ? 0 : 1;
355 sblimit
= sbquant_table
[index
].sblimit
;
356 offsets
= sbquant_table
[index
].offsets
;
359 if (header
->mode
== MAD_MODE_JOINT_STEREO
) {
360 header
->flags
|= MAD_FLAG_I_STEREO
;
361 bound
= 4 + header
->mode_extension
* 4;
369 /* decode bit allocations */
371 for (sb
= 0; sb
< bound
; ++sb
) {
372 nbal
= bitalloc_table
[offsets
[sb
]].nbal
;
374 for (ch
= 0; ch
< nch
; ++ch
)
375 allocation
[ch
][sb
] = mad_bit_read(&stream
->ptr
, nbal
);
378 for (sb
= bound
; sb
< sblimit
; ++sb
) {
379 nbal
= bitalloc_table
[offsets
[sb
]].nbal
;
382 allocation
[1][sb
] = mad_bit_read(&stream
->ptr
, nbal
);
385 /* decode scalefactor selection info */
387 for (sb
= 0; sb
< sblimit
; ++sb
) {
388 for (ch
= 0; ch
< nch
; ++ch
) {
389 if (allocation
[ch
][sb
])
390 scfsi
[ch
][sb
] = mad_bit_read(&stream
->ptr
, 2);
396 if (header
->flags
& MAD_FLAG_PROTECTION
) {
398 mad_bit_crc(start
, mad_bit_length(&start
, &stream
->ptr
),
401 if (header
->crc_check
!= header
->crc_target
&&
402 !(frame
->options
& MAD_OPTION_IGNORECRC
)) {
403 stream
->error
= MAD_ERROR_BADCRC
;
408 /* decode scalefactors */
410 for (sb
= 0; sb
< sblimit
; ++sb
) {
411 for (ch
= 0; ch
< nch
; ++ch
) {
412 if (allocation
[ch
][sb
]) {
413 scalefactor
[ch
][sb
][0] = mad_bit_read(&stream
->ptr
, 6);
415 switch (scfsi
[ch
][sb
]) {
417 scalefactor
[ch
][sb
][2] =
418 scalefactor
[ch
][sb
][1] =
419 scalefactor
[ch
][sb
][0];
423 scalefactor
[ch
][sb
][1] = mad_bit_read(&stream
->ptr
, 6);
428 scalefactor
[ch
][sb
][2] = mad_bit_read(&stream
->ptr
, 6);
431 if (scfsi
[ch
][sb
] & 1)
432 scalefactor
[ch
][sb
][1] = scalefactor
[ch
][sb
][scfsi
[ch
][sb
] - 1];
434 if (scalefactor
[ch
][sb
][0] == 63 ||
435 scalefactor
[ch
][sb
][1] == 63 ||
436 scalefactor
[ch
][sb
][2] == 63) {
437 stream
->error
= MAD_ERROR_BADSCALEFACTOR
;
446 for (gr
= 0; gr
< 12; ++gr
) {
447 for (sb
= 0; sb
< bound
; ++sb
) {
448 for (ch
= 0; ch
< nch
; ++ch
) {
449 if ((index
= allocation
[ch
][sb
])) {
450 index
= offset_table
[bitalloc_table
[offsets
[sb
]].offset
][index
- 1];
452 II_samples(&stream
->ptr
, &qc_table
[index
], samples
);
454 for (s
= 0; s
< 3; ++s
) {
455 frame
->sbsample
[ch
][3 * gr
+ s
][sb
] =
456 mad_f_mul(samples
[s
], sf_table
[scalefactor
[ch
][sb
][gr
/ 4]]);
460 for (s
= 0; s
< 3; ++s
)
461 frame
->sbsample
[ch
][3 * gr
+ s
][sb
] = 0;
466 for (sb
= bound
; sb
< sblimit
; ++sb
) {
467 if ((index
= allocation
[0][sb
])) {
468 index
= offset_table
[bitalloc_table
[offsets
[sb
]].offset
][index
- 1];
470 II_samples(&stream
->ptr
, &qc_table
[index
], samples
);
472 for (ch
= 0; ch
< nch
; ++ch
) {
473 for (s
= 0; s
< 3; ++s
) {
474 frame
->sbsample
[ch
][3 * gr
+ s
][sb
] =
475 mad_f_mul(samples
[s
], sf_table
[scalefactor
[ch
][sb
][gr
/ 4]]);
480 for (ch
= 0; ch
< nch
; ++ch
) {
481 for (s
= 0; s
< 3; ++s
)
482 frame
->sbsample
[ch
][3 * gr
+ s
][sb
] = 0;
487 for (ch
= 0; ch
< nch
; ++ch
) {
488 for (s
= 0; s
< 3; ++s
) {
489 for (sb
= sblimit
; sb
< 32; ++sb
)
490 frame
->sbsample
[ch
][3 * gr
+ s
][sb
] = 0;