2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
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
19 * $Id: layer3.c,v 1.43 2004/01/23 09:41:32 rob Exp $
42 #if TARGET_MSDOS == 16
53 /* --- Layer III ----------------------------------------------------------- */
56 count1table_select = 0x01,
57 scalefac_scale = 0x02,
59 mixed_block_flag = 0x08
68 unsigned int main_data_begin;
69 unsigned int private_bits;
71 unsigned char scfsi[2];
76 unsigned short part2_3_length;
77 unsigned short big_values;
78 unsigned short global_gain;
79 unsigned short scalefac_compress;
82 unsigned char block_type;
83 unsigned char table_select[3];
84 unsigned char subblock_gain[3];
85 unsigned char region0_count;
86 unsigned char region1_count;
89 unsigned char scalefac[39]; /* scalefac_l and/or scalefac_s */
95 * scalefactor bit lengths
96 * derived from section 2.4.2.7 of ISO/IEC 11172-3
102 } const sflen_table[16] = {
103 { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
104 { 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
105 { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
106 { 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
110 * number of LSF scalefactor band values
111 * derived from section 2.4.3.2 of ISO/IEC 13818-3
114 unsigned char const nsfb_table[6][3][4] = {
141 * MPEG-1 scalefactor band widths
142 * derived from Table B.8 of ISO/IEC 11172-3
145 unsigned char const sfb_48000_long[] = {
146 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
147 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192
151 unsigned char const sfb_44100_long[] = {
152 4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
153 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158
157 unsigned char const sfb_32000_long[] = {
158 4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
159 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26
163 unsigned char const sfb_48000_short[] = {
164 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
165 6, 6, 6, 6, 6, 10, 10, 10, 12, 12, 12, 14, 14,
166 14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
170 unsigned char const sfb_44100_short[] = {
171 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
172 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14,
173 14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
177 unsigned char const sfb_32000_short[] = {
178 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
179 6, 6, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20,
180 20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
184 unsigned char const sfb_48000_mixed[] = {
185 /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
186 /* short */ 4, 4, 4, 6, 6, 6, 6, 6, 6, 10,
187 10, 10, 12, 12, 12, 14, 14, 14, 16, 16,
188 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
192 unsigned char const sfb_44100_mixed[] = {
193 /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
194 /* short */ 4, 4, 4, 6, 6, 6, 8, 8, 8, 10,
195 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
196 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
200 unsigned char const sfb_32000_mixed[] = {
201 /* long */ 4, 4, 4, 4, 4, 4, 6, 6,
202 /* short */ 4, 4, 4, 6, 6, 6, 8, 8, 8, 12,
203 12, 12, 16, 16, 16, 20, 20, 20, 26, 26,
204 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
208 * MPEG-2 scalefactor band widths
209 * derived from Table B.2 of ISO/IEC 13818-3
212 unsigned char const sfb_24000_long[] = {
213 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
214 18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36
218 unsigned char const sfb_22050_long[] = {
219 6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
220 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54
223 # define sfb_16000_long sfb_22050_long
226 unsigned char const sfb_24000_short[] = {
227 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8,
228 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
229 18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
233 unsigned char const sfb_22050_short[] = {
234 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6,
235 6, 6, 8, 8, 8, 10, 10, 10, 14, 14, 14, 18, 18,
236 18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
240 unsigned char const sfb_16000_short[] = {
241 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8,
242 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
243 18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
247 unsigned char const sfb_24000_mixed[] = {
248 /* long */ 6, 6, 6, 6, 6, 6,
249 /* short */ 6, 6, 6, 8, 8, 8, 10, 10, 10, 12,
250 12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
251 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
255 unsigned char const sfb_22050_mixed[] = {
256 /* long */ 6, 6, 6, 6, 6, 6,
257 /* short */ 6, 6, 6, 6, 6, 6, 8, 8, 8, 10,
258 10, 10, 14, 14, 14, 18, 18, 18, 26, 26,
259 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
263 unsigned char const sfb_16000_mixed[] = {
264 /* long */ 6, 6, 6, 6, 6, 6,
265 /* short */ 6, 6, 6, 8, 8, 8, 10, 10, 10, 12,
266 12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
267 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
271 * MPEG 2.5 scalefactor band widths
272 * derived from public sources
274 # define sfb_12000_long sfb_16000_long
275 # define sfb_11025_long sfb_12000_long
278 unsigned char const sfb_8000_long[] = {
279 12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
280 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2
283 # define sfb_12000_short sfb_16000_short
284 # define sfb_11025_short sfb_12000_short
287 unsigned char const sfb_8000_short[] = {
288 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12, 16,
289 16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36,
290 36, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26
293 # define sfb_12000_mixed sfb_16000_mixed
294 # define sfb_11025_mixed sfb_12000_mixed
296 /* the 8000 Hz short block scalefactor bands do not break after
297 the first 36 frequency lines, so this is probably wrong */
299 unsigned char const sfb_8000_mixed[] = {
300 /* long */ 12, 12, 12,
301 /* short */ 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16,
302 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36,
303 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26
308 unsigned char const *l;
309 unsigned char const *s;
310 unsigned char const *m;
311 } const sfbwidth_table[9] = {
312 { sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
313 { sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
314 { sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
315 { sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
316 { sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
317 { sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
318 { sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
319 { sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
320 { sfb_8000_long, sfb_8000_short, sfb_8000_mixed }
324 * scalefactor band preemphasis (used only when preflag is set)
325 * derived from Table B.6 of ISO/IEC 11172-3
328 unsigned char const pretab[22] = {
329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
333 * table for requantization
335 * rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
339 unsigned long mantissa : 27;
340 unsigned short exponent : 5;
341 } const rq_table[8207] = {
342 # include "rq_table.dat"
346 * fractional powers of two
347 * used for requantization and joint stereo decoding
349 * root_table[3 + x] = 2^(x/4)
352 mad_fixed_t const root_table[7] = {
353 MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
354 MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
355 MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
356 MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
357 MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
358 MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
359 MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
363 * coefficients for aliasing reduction
364 * derived from Table B.9 of ISO/IEC 11172-3
366 * c[] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
367 * cs[i] = 1 / sqrt(1 + c[i]^2)
368 * ca[i] = c[i] / sqrt(1 + c[i]^2)
371 mad_fixed_t const cs[8] = {
372 +MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
373 +MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
374 +MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
375 +MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
379 mad_fixed_t const ca[8] = {
380 -MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
381 -MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
382 -MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
383 -MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
387 * IMDCT coefficients for short blocks
388 * derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
390 * imdct_s[i/even][k] = cos((PI / 24) * (2 * (i / 2) + 7) * (2 * k + 1))
391 * imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1))
394 mad_fixed_t const imdct_s[6][6] = {
395 # include "imdct_s.dat"
398 # if !defined(ASO_IMDCT)
400 * windowing coefficients for long blocks
401 * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
403 * window_l[i] = sin((PI / 36) * (i + 1/2))
406 mad_fixed_t const window_l[36] = {
407 MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
408 MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
409 MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
410 MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
411 MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
412 MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
414 MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
415 MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
416 MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
417 MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
418 MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
419 MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
421 MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
422 MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
423 MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
424 MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
425 MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
426 MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
428 # endif /* ASO_IMDCT */
431 * windowing coefficients for short blocks
432 * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
434 * window_s[i] = sin((PI / 12) * (i + 1/2))
437 mad_fixed_t const window_s[12] = {
438 MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
439 MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
440 MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
441 MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
442 MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
443 MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
447 * coefficients for intensity stereo processing
448 * derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
450 * is_ratio[i] = tan(i * (PI / 12))
451 * is_table[i] = is_ratio[i] / (1 + is_ratio[i])
454 mad_fixed_t const is_table[7] = {
455 MAD_F(0x00000000) /* 0.000000000 */,
456 MAD_F(0x0361962f) /* 0.211324865 */,
457 MAD_F(0x05db3d74) /* 0.366025404 */,
458 MAD_F(0x08000000) /* 0.500000000 */,
459 MAD_F(0x0a24c28c) /* 0.633974596 */,
460 MAD_F(0x0c9e69d1) /* 0.788675135 */,
461 MAD_F(0x10000000) /* 1.000000000 */
465 * coefficients for LSF intensity stereo processing
466 * derived from section 2.4.3.2 of ISO/IEC 13818-3
468 * is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
469 * is_lsf_table[1][i] = (1 / sqrt(2)) ^(i + 1)
472 mad_fixed_t const is_lsf_table[2][15] = {
474 MAD_F(0x0d744fcd) /* 0.840896415 */,
475 MAD_F(0x0b504f33) /* 0.707106781 */,
476 MAD_F(0x09837f05) /* 0.594603558 */,
477 MAD_F(0x08000000) /* 0.500000000 */,
478 MAD_F(0x06ba27e6) /* 0.420448208 */,
479 MAD_F(0x05a8279a) /* 0.353553391 */,
480 MAD_F(0x04c1bf83) /* 0.297301779 */,
481 MAD_F(0x04000000) /* 0.250000000 */,
482 MAD_F(0x035d13f3) /* 0.210224104 */,
483 MAD_F(0x02d413cd) /* 0.176776695 */,
484 MAD_F(0x0260dfc1) /* 0.148650889 */,
485 MAD_F(0x02000000) /* 0.125000000 */,
486 MAD_F(0x01ae89fa) /* 0.105112052 */,
487 MAD_F(0x016a09e6) /* 0.088388348 */,
488 MAD_F(0x01306fe1) /* 0.074325445 */
490 MAD_F(0x0b504f33) /* 0.707106781 */,
491 MAD_F(0x08000000) /* 0.500000000 */,
492 MAD_F(0x05a8279a) /* 0.353553391 */,
493 MAD_F(0x04000000) /* 0.250000000 */,
494 MAD_F(0x02d413cd) /* 0.176776695 */,
495 MAD_F(0x02000000) /* 0.125000000 */,
496 MAD_F(0x016a09e6) /* 0.088388348 */,
497 MAD_F(0x01000000) /* 0.062500000 */,
498 MAD_F(0x00b504f3) /* 0.044194174 */,
499 MAD_F(0x00800000) /* 0.031250000 */,
500 MAD_F(0x005a827a) /* 0.022097087 */,
501 MAD_F(0x00400000) /* 0.015625000 */,
502 MAD_F(0x002d413d) /* 0.011048543 */,
503 MAD_F(0x00200000) /* 0.007812500 */,
504 MAD_F(0x0016a09e) /* 0.005524272 */
509 * NAME: III_sideinfo()
510 * DESCRIPTION: decode frame side information from a bitstream
513 enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
514 int lsf, struct sideinfo *si,
515 unsigned int *data_bitlen,
516 unsigned int *priv_bitlen)
518 unsigned int ngr, gr, ch, i;
519 enum mad_error result = MAD_ERROR_NONE;
522 *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
524 si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
525 si->private_bits = mad_bit_read(ptr, *priv_bitlen);
531 for (ch = 0; ch < nch; ++ch)
532 si->scfsi[ch] = mad_bit_read(ptr, 4);
535 for (gr = 0; gr < ngr; ++gr) {
536 struct granule *granule = &si->gr[gr];
538 for (ch = 0; ch < nch; ++ch) {
539 struct channel *channel = &granule->ch[ch];
541 channel->part2_3_length = mad_bit_read(ptr, 12);
542 channel->big_values = mad_bit_read(ptr, 9);
543 channel->global_gain = mad_bit_read(ptr, 8);
544 channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
546 *data_bitlen += channel->part2_3_length;
548 if (channel->big_values > 288 && result == 0)
549 result = MAD_ERROR_BADBIGVALUES;
553 /* window_switching_flag */
554 if (mad_bit_read(ptr, 1)) {
555 channel->block_type = mad_bit_read(ptr, 2);
557 if (channel->block_type == 0 && result == 0)
558 result = MAD_ERROR_BADBLOCKTYPE;
560 if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
561 result = MAD_ERROR_BADSCFSI;
563 channel->region0_count = 7;
564 channel->region1_count = 36;
566 if (mad_bit_read(ptr, 1))
567 channel->flags |= mixed_block_flag;
568 else if (channel->block_type == 2)
569 channel->region0_count = 8;
571 for (i = 0; i < 2; ++i)
572 channel->table_select[i] = mad_bit_read(ptr, 5);
575 channel->table_select[2] = 4; /* not used */
578 for (i = 0; i < 3; ++i)
579 channel->subblock_gain[i] = mad_bit_read(ptr, 3);
582 channel->block_type = 0;
584 for (i = 0; i < 3; ++i)
585 channel->table_select[i] = mad_bit_read(ptr, 5);
587 channel->region0_count = mad_bit_read(ptr, 4);
588 channel->region1_count = mad_bit_read(ptr, 3);
591 /* [preflag,] scalefac_scale, count1table_select */
592 channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
600 * NAME: III_scalefactors_lsf()
601 * DESCRIPTION: decode channel scalefactors for LSF from a bitstream
604 unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
605 struct channel *channel,
606 struct channel *gr1ch, int mode_extension)
608 struct mad_bitptr start;
609 unsigned int scalefac_compress, index, slen[4], part, n, i;
610 unsigned char const *nsfb;
614 scalefac_compress = channel->scalefac_compress;
615 index = (channel->block_type == 2) ?
616 ((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
618 if (!((mode_extension & I_STEREO) && gr1ch)) {
619 if (scalefac_compress < 400) {
620 slen[0] = (scalefac_compress >> 4) / 5;
621 slen[1] = (scalefac_compress >> 4) % 5;
622 slen[2] = (scalefac_compress % 16) >> 2;
623 slen[3] = scalefac_compress % 4;
625 nsfb = nsfb_table[0][index];
627 else if (scalefac_compress < 500) {
628 scalefac_compress -= 400;
630 slen[0] = (scalefac_compress >> 2) / 5;
631 slen[1] = (scalefac_compress >> 2) % 5;
632 slen[2] = scalefac_compress % 4;
635 nsfb = nsfb_table[1][index];
638 scalefac_compress -= 500;
640 slen[0] = scalefac_compress / 3;
641 slen[1] = scalefac_compress % 3;
645 channel->flags |= preflag;
647 nsfb = nsfb_table[2][index];
651 for (part = 0; part < 4; ++part) {
652 for (i = 0; i < nsfb[part]; ++i)
653 channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
657 channel->scalefac[n++] = 0;
659 else { /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
660 scalefac_compress >>= 1;
662 if (scalefac_compress < 180) {
663 slen[0] = scalefac_compress / 36;
664 slen[1] = (scalefac_compress % 36) / 6;
665 slen[2] = (scalefac_compress % 36) % 6;
668 nsfb = nsfb_table[3][index];
670 else if (scalefac_compress < 244) {
671 scalefac_compress -= 180;
673 slen[0] = (scalefac_compress % 64) >> 4;
674 slen[1] = (scalefac_compress % 16) >> 2;
675 slen[2] = scalefac_compress % 4;
678 nsfb = nsfb_table[4][index];
681 scalefac_compress -= 244;
683 slen[0] = scalefac_compress / 3;
684 slen[1] = scalefac_compress % 3;
688 nsfb = nsfb_table[5][index];
692 for (part = 0; part < 4; ++part) {
693 unsigned int max, is_pos;
695 max = (1 << slen[part]) - 1;
697 for (i = 0; i < nsfb[part]; ++i) {
698 is_pos = mad_bit_read(ptr, slen[part]);
700 channel->scalefac[n] = is_pos;
701 gr1ch->scalefac[n++] = (is_pos == max);
706 channel->scalefac[n] = 0;
707 gr1ch->scalefac[n++] = 0; /* apparently not illegal */
711 return mad_bit_length(&start, ptr);
715 * NAME: III_scalefactors()
716 * DESCRIPTION: decode channel scalefactors of one granule from a bitstream
719 unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
720 struct channel const *gr0ch, unsigned int scfsi)
722 struct mad_bitptr start;
723 unsigned int slen1, slen2, sfbi;
727 slen1 = sflen_table[channel->scalefac_compress].slen1;
728 slen2 = sflen_table[channel->scalefac_compress].slen2;
730 if (channel->block_type == 2) {
735 nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
737 channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
741 channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
745 channel->scalefac[sfbi++] = 0;
747 else { /* channel->block_type != 2 */
749 for (sfbi = 0; sfbi < 6; ++sfbi)
750 channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
753 for (sfbi = 0; sfbi < 6; ++sfbi)
754 channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
758 for (sfbi = 6; sfbi < 11; ++sfbi)
759 channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
762 for (sfbi = 6; sfbi < 11; ++sfbi)
763 channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
767 for (sfbi = 11; sfbi < 16; ++sfbi)
768 channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
771 for (sfbi = 11; sfbi < 16; ++sfbi)
772 channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
776 for (sfbi = 16; sfbi < 21; ++sfbi)
777 channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
780 for (sfbi = 16; sfbi < 21; ++sfbi)
781 channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
784 channel->scalefac[21] = 0;
787 return mad_bit_length(&start, ptr);
791 * The Layer III formula for requantization and scaling is defined by
792 * section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
795 * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
796 * 2^((1/4) * (global_gain - 210)) *
797 * 2^-(scalefac_multiplier *
798 * (scalefac_l[sfb] + preflag * pretab[sfb]))
801 * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
802 * 2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
803 * 2^-(scalefac_multiplier * scalefac_s[sfb][w])
806 * scalefac_multiplier = (scalefac_scale + 1) / 2
808 * The routines III_exponents() and III_requantize() facilitate this
813 * NAME: III_exponents()
814 * DESCRIPTION: calculate scalefactor exponents
817 void III_exponents(struct channel const *channel,
818 unsigned char const *sfbwidth, signed int exponents[39])
821 unsigned int scalefac_multiplier, sfbi;
823 gain = (signed int) channel->global_gain - 210;
824 scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
826 if (channel->block_type == 2) {
828 signed int gain0, gain1, gain2;
832 if (channel->flags & mixed_block_flag) {
833 unsigned int premask;
835 premask = (channel->flags & preflag) ? ~0 : 0;
837 /* long block subbands 0-1 */
840 exponents[sfbi] = gain -
841 (signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
842 scalefac_multiplier);
844 l += sfbwidth[sfbi++];
848 /* this is probably wrong for 8000 Hz short/mixed blocks */
850 gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
851 gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
852 gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
855 exponents[sfbi + 0] = gain0 -
856 (signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
857 exponents[sfbi + 1] = gain1 -
858 (signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
859 exponents[sfbi + 2] = gain2 -
860 (signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
862 l += 3 * sfbwidth[sfbi];
866 else { /* channel->block_type != 2 */
867 if (channel->flags & preflag) {
868 for (sfbi = 0; sfbi < 22; ++sfbi) {
869 exponents[sfbi] = gain -
870 (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
871 scalefac_multiplier);
875 for (sfbi = 0; sfbi < 22; ++sfbi) {
876 exponents[sfbi] = gain -
877 (signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
884 * NAME: III_requantize()
885 * DESCRIPTION: requantize one (positive) value
888 mad_fixed_t III_requantize(unsigned int value, signed int exp)
890 mad_fixed_t requantized;
892 struct fixedfloat const *power;
894 frac = exp % 4; /* assumes sign(frac) == sign(exp) */
897 power = &rq_table[value];
898 requantized = power->mantissa;
899 exp += power->exponent;
902 if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
907 requantized += 1L << (-exp - 1);
908 requantized >>= -exp;
915 fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
916 mad_f_todouble(requantized), exp);
918 requantized = MAD_F_MAX;
924 return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
927 /* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
928 # define MASK(cache, sz, bits) \
929 (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
930 # define MASK1BIT(cache, sz) \
931 ((cache) & (1 << ((sz) - 1)))
934 * NAME: III_huffdecode()
935 * DESCRIPTION: decode Huffman code words of one channel of one granule
938 enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
939 struct channel *channel,
940 unsigned char const *sfbwidth,
941 unsigned int part2_length)
943 signed int exponents[39], exp;
944 signed int const *expptr;
945 struct mad_bitptr peek;
946 signed int bits_left, cachesz;
947 register mad_fixed_t *xrptr;
948 mad_fixed_t const *sfbound;
949 register unsigned long bitcache;
951 bits_left = (signed) channel->part2_3_length - (signed) part2_length;
953 return MAD_ERROR_BADPART3LEN;
955 III_exponents(channel, sfbwidth, exponents);
958 mad_bit_skip(ptr, bits_left);
960 /* align bit reads to byte boundaries */
961 cachesz = mad_bit_bitsleft(&peek);
962 cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
964 bitcache = mad_bit_read(&peek, cachesz);
965 bits_left -= cachesz;
971 unsigned int region, rcount;
972 struct hufftable const *entry;
973 union huffpair const *table;
974 unsigned int linbits, startbits, big_values, reqhits;
975 mad_fixed_t reqcache[16];
977 sfbound = xrptr + *sfbwidth++;
978 rcount = channel->region0_count + 1;
980 entry = &mad_huff_pair_table[channel->table_select[region = 0]];
981 table = entry->table;
982 linbits = entry->linbits;
983 startbits = entry->startbits;
986 return MAD_ERROR_BADHUFFTABLE;
988 expptr = &exponents[0];
992 big_values = channel->big_values;
994 while (big_values-- && cachesz + bits_left > 0) {
995 union huffpair const *pair;
996 unsigned int clumpsz, value;
997 register mad_fixed_t requantized;
999 if (xrptr == sfbound) {
1000 sfbound += *sfbwidth++;
1002 /* change table if region boundary */
1004 if (--rcount == 0) {
1006 rcount = channel->region1_count + 1;
1008 rcount = 0; /* all remaining */
1010 entry = &mad_huff_pair_table[channel->table_select[++region]];
1011 table = entry->table;
1012 linbits = entry->linbits;
1013 startbits = entry->startbits;
1016 return MAD_ERROR_BADHUFFTABLE;
1019 if (exp != *expptr) {
1030 bits = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
1031 bitcache = (bitcache << bits) | mad_bit_read(&peek, bits);
1038 clumpsz = startbits;
1039 pair = &table[MASK(bitcache, cachesz, clumpsz)];
1041 while (!pair->final) {
1044 clumpsz = pair->ptr.bits;
1045 pair = &table[pair->ptr.offset + MASK(bitcache, cachesz, clumpsz)];
1048 cachesz -= pair->value.hlen;
1053 value = pair->value.x;
1061 if (cachesz < linbits + 2) {
1062 bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
1067 value += MASK(bitcache, cachesz, linbits);
1070 requantized = III_requantize(value, exp);
1074 if (reqhits & (1 << value))
1075 requantized = reqcache[value];
1077 reqhits |= (1 << value);
1078 requantized = reqcache[value] = III_requantize(value, exp);
1082 xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
1083 -requantized : requantized;
1088 value = pair->value.y;
1096 if (cachesz < linbits + 1) {
1097 bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
1102 value += MASK(bitcache, cachesz, linbits);
1105 requantized = III_requantize(value, exp);
1109 if (reqhits & (1 << value))
1110 requantized = reqcache[value];
1112 reqhits |= (1 << value);
1113 requantized = reqcache[value] = III_requantize(value, exp);
1117 xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
1118 -requantized : requantized;
1124 value = pair->value.x;
1129 if (reqhits & (1 << value))
1130 requantized = reqcache[value];
1132 reqhits |= (1 << value);
1133 requantized = reqcache[value] = III_requantize(value, exp);
1136 xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
1137 -requantized : requantized;
1142 value = pair->value.y;
1147 if (reqhits & (1 << value))
1148 requantized = reqcache[value];
1150 reqhits |= (1 << value);
1151 requantized = reqcache[value] = III_requantize(value, exp);
1154 xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
1155 -requantized : requantized;
1163 if (cachesz + bits_left < 0)
1164 return MAD_ERROR_BADHUFFDATA; /* big_values overrun */
1168 union huffquad const *table;
1169 register mad_fixed_t requantized;
1171 table = mad_huff_quad_table[channel->flags & count1table_select];
1173 requantized = III_requantize(1, exp);
1175 while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
1176 union huffquad const *quad;
1181 bitcache = (bitcache << 16) | mad_bit_read(&peek, 16);
1186 quad = &table[MASK(bitcache, cachesz, 4)];
1188 /* quad tables guaranteed to have at most one extra lookup */
1192 quad = &table[quad->ptr.offset +
1193 MASK(bitcache, cachesz, quad->ptr.bits)];
1196 cachesz -= quad->value.hlen;
1198 if (xrptr == sfbound) {
1199 sfbound += *sfbwidth++;
1201 if (exp != *expptr) {
1203 requantized = III_requantize(1, exp);
1211 xrptr[0] = quad->value.v ?
1212 (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1216 xrptr[1] = quad->value.w ?
1217 (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1221 if (xrptr == sfbound) {
1222 sfbound += *sfbwidth++;
1224 if (exp != *expptr) {
1226 requantized = III_requantize(1, exp);
1234 xrptr[0] = quad->value.x ?
1235 (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1239 xrptr[1] = quad->value.y ?
1240 (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1245 if (cachesz + bits_left < 0) {
1246 # if 0 && defined(DEBUG)
1247 fprintf(stderr, "huffman count1 overrun (%d bits)\n",
1248 -(cachesz + bits_left));
1251 /* technically the bitstream is misformatted, but apparently
1252 some encoders are just a bit sloppy with stuffing bits */
1258 assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
1260 # if 0 && defined(DEBUG)
1262 fprintf(stderr, "read %d bits too many\n", -bits_left);
1263 else if (cachesz + bits_left > 0)
1264 fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left);
1268 while (xrptr < &xr[576]) {
1275 return MAD_ERROR_NONE;
1281 #if TARGET_MSDOS == 16
1282 static mad_fixed_t III_reorder_tmp[32][3][6]; /* FIXME */
1286 * NAME: III_reorder()
1287 * DESCRIPTION: reorder frequency lines of a short block into subband order
1290 void III_reorder(mad_fixed_t xr[576], struct channel const *channel,
1291 unsigned char const sfbwidth[39])
1293 #if TARGET_MSDOS == 16
1294 mad_fixed_t (*tmp)[3][6] = III_reorder_tmp;
1296 mad_fixed_t tmp[32][3][6];
1298 unsigned int sb, l, f, w, sbw[3], sw[3];
1300 /* this is probably wrong for 8000 Hz mixed blocks */
1303 if (channel->flags & mixed_block_flag) {
1311 for (w = 0; w < 3; ++w) {
1319 for (l = 18 * sb; l < 576; ++l) {
1321 f = *sfbwidth++ - 1;
1325 tmp[sbw[w]][w][sw[w]++] = xr[l];
1333 memcpy(&xr[18 * sb], &tmp[sb], (576 - 18 * sb) * sizeof(mad_fixed_t));
1337 * NAME: III_stereo()
1338 * DESCRIPTION: perform joint stereo processing on a granule
1341 enum mad_error III_stereo(mad_fixed_t xr[2][576],
1342 struct granule const *granule,
1343 struct mad_header *header,
1344 unsigned char const *sfbwidth)
1347 unsigned int sfbi, l, n, i;
1349 if (granule->ch[0].block_type !=
1350 granule->ch[1].block_type ||
1351 (granule->ch[0].flags & mixed_block_flag) !=
1352 (granule->ch[1].flags & mixed_block_flag))
1353 return MAD_ERROR_BADSTEREO;
1355 for (i = 0; i < 39; ++i)
1356 modes[i] = header->mode_extension;
1358 /* intensity stereo */
1360 if (header->mode_extension & I_STEREO) {
1361 struct channel const *right_ch = &granule->ch[1];
1362 mad_fixed_t const *right_xr = xr[1];
1363 unsigned int is_pos;
1365 header->flags |= MAD_FLAG_I_STEREO;
1367 /* first determine which scalefactor bands are to be processed */
1369 if (right_ch->block_type == 2) {
1370 unsigned int lower, start, max, bound[3], w;
1372 lower = start = max = bound[0] = bound[1] = bound[2] = 0;
1376 if (right_ch->flags & mixed_block_flag) {
1378 n = sfbwidth[sfbi++];
1380 for (i = 0; i < n; ++i) {
1396 n = sfbwidth[sfbi++];
1398 for (i = 0; i < n; ++i) {
1400 max = bound[w] = sfbi;
1415 for (i = 0; i < lower; ++i)
1416 modes[i] = header->mode_extension & ~I_STEREO;
1421 for (i = start; i < max; ++i) {
1423 modes[i] = header->mode_extension & ~I_STEREO;
1428 else { /* right_ch->block_type != 2 */
1432 for (sfbi = l = 0; l < 576; l += n) {
1433 n = sfbwidth[sfbi++];
1435 for (i = 0; i < n; ++i) {
1445 for (i = 0; i < bound; ++i)
1446 modes[i] = header->mode_extension & ~I_STEREO;
1449 /* now do the actual processing */
1451 if (header->flags & MAD_FLAG_LSF_EXT) {
1452 unsigned char const *illegal_pos = granule[1].ch[1].scalefac;
1453 mad_fixed_t const *lsf_scale;
1455 /* intensity_scale */
1456 lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1];
1458 for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1461 if (!(modes[sfbi] & I_STEREO))
1464 if (illegal_pos[sfbi]) {
1465 modes[sfbi] &= ~I_STEREO;
1469 is_pos = right_ch->scalefac[sfbi];
1471 for (i = 0; i < n; ++i) {
1472 register mad_fixed_t left;
1474 left = xr[0][l + i];
1477 xr[1][l + i] = left;
1479 register mad_fixed_t opposite;
1481 opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]);
1484 xr[0][l + i] = opposite;
1485 xr[1][l + i] = left;
1488 xr[1][l + i] = opposite;
1493 else { /* !(header->flags & MAD_FLAG_LSF_EXT) */
1494 for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1497 if (!(modes[sfbi] & I_STEREO))
1500 is_pos = right_ch->scalefac[sfbi];
1502 if (is_pos >= 7) { /* illegal intensity position */
1503 modes[sfbi] &= ~I_STEREO;
1507 for (i = 0; i < n; ++i) {
1508 register mad_fixed_t left;
1510 left = xr[0][l + i];
1512 xr[0][l + i] = mad_f_mul(left, is_table[ is_pos]);
1513 xr[1][l + i] = mad_f_mul(left, is_table[6 - is_pos]);
1519 /* middle/side stereo */
1521 if (header->mode_extension & MS_STEREO) {
1522 register mad_fixed_t invsqrt2;
1524 header->flags |= MAD_FLAG_MS_STEREO;
1526 invsqrt2 = root_table[3 + -2];
1528 for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1531 if (modes[sfbi] != MS_STEREO)
1534 for (i = 0; i < n; ++i) {
1535 register mad_fixed_t m, s;
1540 xr[0][l + i] = mad_f_mul(m + s, invsqrt2); /* l = (m + s) / sqrt(2) */
1541 xr[1][l + i] = mad_f_mul(m - s, invsqrt2); /* r = (m - s) / sqrt(2) */
1546 return MAD_ERROR_NONE;
1550 * NAME: III_aliasreduce()
1551 * DESCRIPTION: perform frequency line alias reduction
1554 void III_aliasreduce(mad_fixed_t xr[576], int lines)
1556 mad_fixed_t const *bound;
1559 /* NTS: Testing shows this function is NOT responsible for 16-bit build memory corruption */
1562 for (xr += 18; xr < bound; xr += 18) {
1563 for (i = 0; i < 8; ++i) {
1564 register mad_fixed_t a, b;
1565 register mad_fixed64hi_t hi;
1566 register mad_fixed64lo_t lo;
1571 # if defined(ASO_ZEROCHECK)
1574 MAD_F_ML0(hi, lo, a, cs[i]);
1575 MAD_F_MLA(hi, lo, -b, ca[i]);
1577 xr[-1 - i] = MAD_F_MLZ(hi, lo);
1579 MAD_F_ML0(hi, lo, b, cs[i]);
1580 MAD_F_MLA(hi, lo, a, ca[i]);
1582 xr[ i] = MAD_F_MLZ(hi, lo);
1583 # if defined(ASO_ZEROCHECK)
1590 # if defined(ASO_IMDCT)
1591 void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int);
1595 void fastsdct(mad_fixed_t const x[9], mad_fixed_t y[18])
1597 mad_fixed_t a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12;
1598 mad_fixed_t a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25;
1599 mad_fixed_t m0, m1, m2, m3, m4, m5, m6, m7;
1602 c0 = MAD_F(0x1f838b8d), /* 2 * cos( 1 * PI / 18) */
1603 c1 = MAD_F(0x1bb67ae8), /* 2 * cos( 3 * PI / 18) */
1604 c2 = MAD_F(0x18836fa3), /* 2 * cos( 4 * PI / 18) */
1605 c3 = MAD_F(0x1491b752), /* 2 * cos( 5 * PI / 18) */
1606 c4 = MAD_F(0x0af1d43a), /* 2 * cos( 7 * PI / 18) */
1607 c5 = MAD_F(0x058e86a0), /* 2 * cos( 8 * PI / 18) */
1608 c6 = -MAD_F(0x1e11f642) /* 2 * cos(16 * PI / 18) */
1631 m0 = mad_f_mul(a17, -c3);
1632 m1 = mad_f_mul(a16, -c0);
1633 m2 = mad_f_mul(a15, -c4);
1634 m3 = mad_f_mul(a14, -c1);
1635 m4 = mad_f_mul(a5, -c1);
1636 m5 = mad_f_mul(a11, -c6);
1637 m6 = mad_f_mul(a10, -c5);
1638 m7 = mad_f_mul(a9, -c2);
1641 a19 = 2 * x[4] - a4;
1649 /* output to every other slot for convenience */
1657 y[12] = a12 - 2 * a18;
1663 void sdctII(mad_fixed_t const x[18], mad_fixed_t X[18])
1668 /* scale[i] = 2 * cos(PI * (2 * i + 1) / (2 * 18)) */
1669 static mad_fixed_t const scale[9] = {
1670 MAD_F(0x1fe0d3b4), MAD_F(0x1ee8dd47), MAD_F(0x1d007930),
1671 MAD_F(0x1a367e59), MAD_F(0x16a09e66), MAD_F(0x125abcf8),
1672 MAD_F(0x0d8616bc), MAD_F(0x08483ee1), MAD_F(0x02c9fad7)
1675 /* divide the 18-point SDCT-II into two 9-point SDCT-IIs */
1677 /* even input butterfly */
1679 for (i = 0; i < 9; i += 3) {
1680 tmp[i + 0] = x[i + 0] + x[18 - (i + 0) - 1];
1681 tmp[i + 1] = x[i + 1] + x[18 - (i + 1) - 1];
1682 tmp[i + 2] = x[i + 2] + x[18 - (i + 2) - 1];
1685 fastsdct(tmp, &X[0]);
1687 /* odd input butterfly and scaling */
1689 for (i = 0; i < 9; i += 3) {
1690 tmp[i + 0] = mad_f_mul(x[i + 0] - x[18 - (i + 0) - 1], scale[i + 0]);
1691 tmp[i + 1] = mad_f_mul(x[i + 1] - x[18 - (i + 1) - 1], scale[i + 1]);
1692 tmp[i + 2] = mad_f_mul(x[i + 2] - x[18 - (i + 2) - 1], scale[i + 2]);
1695 fastsdct(tmp, &X[1]);
1697 /* output accumulation */
1699 for (i = 3; i < 18; i += 8) {
1700 X[i + 0] -= X[(i + 0) - 2];
1701 X[i + 2] -= X[(i + 2) - 2];
1702 X[i + 4] -= X[(i + 4) - 2];
1703 X[i + 6] -= X[(i + 6) - 2];
1708 void dctIV(mad_fixed_t const y[18], mad_fixed_t X[18])
1710 mad_fixed_t tmp[18];
1713 /* scale[i] = 2 * cos(PI * (2 * i + 1) / (4 * 18)) */
1714 static mad_fixed_t const scale[18] = {
1715 MAD_F(0x1ff833fa), MAD_F(0x1fb9ea93), MAD_F(0x1f3dd120),
1716 MAD_F(0x1e84d969), MAD_F(0x1d906bcf), MAD_F(0x1c62648b),
1717 MAD_F(0x1afd100f), MAD_F(0x1963268b), MAD_F(0x1797c6a4),
1718 MAD_F(0x159e6f5b), MAD_F(0x137af940), MAD_F(0x11318ef3),
1719 MAD_F(0x0ec6a507), MAD_F(0x0c3ef153), MAD_F(0x099f61c5),
1720 MAD_F(0x06ed12c5), MAD_F(0x042d4544), MAD_F(0x0165547c)
1725 for (i = 0; i < 18; i += 3) {
1726 tmp[i + 0] = mad_f_mul(y[i + 0], scale[i + 0]);
1727 tmp[i + 1] = mad_f_mul(y[i + 1], scale[i + 1]);
1728 tmp[i + 2] = mad_f_mul(y[i + 2], scale[i + 2]);
1735 /* scale reduction and output accumulation */
1738 for (i = 1; i < 17; i += 4) {
1739 X[i + 0] = X[i + 0] / 2 - X[(i + 0) - 1];
1740 X[i + 1] = X[i + 1] / 2 - X[(i + 1) - 1];
1741 X[i + 2] = X[i + 2] / 2 - X[(i + 2) - 1];
1742 X[i + 3] = X[i + 3] / 2 - X[(i + 3) - 1];
1744 X[17] = X[17] / 2 - X[16];
1749 * DESCRIPTION: perform X[18]->x[36] IMDCT using Szu-Wei Lee's fast algorithm
1752 void imdct36(mad_fixed_t const x[18], mad_fixed_t y[36])
1754 mad_fixed_t tmp[18];
1761 /* convert 18-point DCT-IV to 36-point IMDCT */
1763 for (i = 0; i < 9; i += 3) {
1764 y[i + 0] = tmp[9 + (i + 0)];
1765 y[i + 1] = tmp[9 + (i + 1)];
1766 y[i + 2] = tmp[9 + (i + 2)];
1768 for (i = 9; i < 27; i += 3) {
1769 y[i + 0] = -tmp[36 - (9 + (i + 0)) - 1];
1770 y[i + 1] = -tmp[36 - (9 + (i + 1)) - 1];
1771 y[i + 2] = -tmp[36 - (9 + (i + 2)) - 1];
1773 for (i = 27; i < 36; i += 3) {
1774 y[i + 0] = -tmp[(i + 0) - 27];
1775 y[i + 1] = -tmp[(i + 1) - 27];
1776 y[i + 2] = -tmp[(i + 2) - 27];
1782 * DESCRIPTION: perform X[18]->x[36] IMDCT
1785 void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36])
1787 mad_fixed_t t0, t1, t2, t3, t4, t5, t6, t7;
1788 mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
1789 register mad_fixed64hi_t hi;
1790 register mad_fixed64lo_t lo;
1792 MAD_F_ML0(hi, lo, X[4], MAD_F(0x0ec835e8));
1793 MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa));
1795 t6 = MAD_F_MLZ(hi, lo);
1797 MAD_F_MLA(hi, lo, (t14 = X[1] - X[10]), -MAD_F(0x061f78aa));
1798 MAD_F_MLA(hi, lo, (t15 = X[7] + X[16]), -MAD_F(0x0ec835e8));
1800 t0 = MAD_F_MLZ(hi, lo);
1802 MAD_F_MLA(hi, lo, (t8 = X[0] - X[11] - X[12]), MAD_F(0x0216a2a2));
1803 MAD_F_MLA(hi, lo, (t9 = X[2] - X[9] - X[14]), MAD_F(0x09bd7ca0));
1804 MAD_F_MLA(hi, lo, (t10 = X[3] - X[8] - X[15]), -MAD_F(0x0cb19346));
1805 MAD_F_MLA(hi, lo, (t11 = X[5] - X[6] - X[17]), -MAD_F(0x0fdcf549));
1807 x[7] = MAD_F_MLZ(hi, lo);
1810 MAD_F_ML0(hi, lo, t8, -MAD_F(0x0cb19346));
1811 MAD_F_MLA(hi, lo, t9, MAD_F(0x0fdcf549));
1812 MAD_F_MLA(hi, lo, t10, MAD_F(0x0216a2a2));
1813 MAD_F_MLA(hi, lo, t11, -MAD_F(0x09bd7ca0));
1815 x[19] = x[34] = MAD_F_MLZ(hi, lo) - t0;
1817 t12 = X[0] - X[3] + X[8] - X[11] - X[12] + X[15];
1818 t13 = X[2] + X[5] - X[6] - X[9] - X[14] - X[17];
1820 MAD_F_ML0(hi, lo, t12, -MAD_F(0x0ec835e8));
1821 MAD_F_MLA(hi, lo, t13, MAD_F(0x061f78aa));
1823 x[22] = x[31] = MAD_F_MLZ(hi, lo) + t0;
1825 MAD_F_ML0(hi, lo, X[1], -MAD_F(0x09bd7ca0));
1826 MAD_F_MLA(hi, lo, X[7], MAD_F(0x0216a2a2));
1827 MAD_F_MLA(hi, lo, X[10], -MAD_F(0x0fdcf549));
1828 MAD_F_MLA(hi, lo, X[16], MAD_F(0x0cb19346));
1830 t1 = MAD_F_MLZ(hi, lo) + t6;
1832 MAD_F_ML0(hi, lo, X[0], MAD_F(0x03768962));
1833 MAD_F_MLA(hi, lo, X[2], MAD_F(0x0e313245));
1834 MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0ffc19fd));
1835 MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0acf37ad));
1836 MAD_F_MLA(hi, lo, X[6], MAD_F(0x04cfb0e2));
1837 MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0898c779));
1838 MAD_F_MLA(hi, lo, X[9], MAD_F(0x0d7e8807));
1839 MAD_F_MLA(hi, lo, X[11], MAD_F(0x0f426cb5));
1840 MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0bcbe352));
1841 MAD_F_MLA(hi, lo, X[14], MAD_F(0x00b2aa3e));
1842 MAD_F_MLA(hi, lo, X[15], -MAD_F(0x07635284));
1843 MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0f9ee890));
1845 x[6] = MAD_F_MLZ(hi, lo) + t1;
1848 MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0f426cb5));
1849 MAD_F_MLA(hi, lo, X[2], -MAD_F(0x00b2aa3e));
1850 MAD_F_MLA(hi, lo, X[3], MAD_F(0x0898c779));
1851 MAD_F_MLA(hi, lo, X[5], MAD_F(0x0f9ee890));
1852 MAD_F_MLA(hi, lo, X[6], MAD_F(0x0acf37ad));
1853 MAD_F_MLA(hi, lo, X[8], -MAD_F(0x07635284));
1854 MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0e313245));
1855 MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0bcbe352));
1856 MAD_F_MLA(hi, lo, X[12], -MAD_F(0x03768962));
1857 MAD_F_MLA(hi, lo, X[14], MAD_F(0x0d7e8807));
1858 MAD_F_MLA(hi, lo, X[15], MAD_F(0x0ffc19fd));
1859 MAD_F_MLA(hi, lo, X[17], MAD_F(0x04cfb0e2));
1861 x[23] = x[30] = MAD_F_MLZ(hi, lo) + t1;
1863 MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0bcbe352));
1864 MAD_F_MLA(hi, lo, X[2], MAD_F(0x0d7e8807));
1865 MAD_F_MLA(hi, lo, X[3], -MAD_F(0x07635284));
1866 MAD_F_MLA(hi, lo, X[5], MAD_F(0x04cfb0e2));
1867 MAD_F_MLA(hi, lo, X[6], MAD_F(0x0f9ee890));
1868 MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0ffc19fd));
1869 MAD_F_MLA(hi, lo, X[9], -MAD_F(0x00b2aa3e));
1870 MAD_F_MLA(hi, lo, X[11], MAD_F(0x03768962));
1871 MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0f426cb5));
1872 MAD_F_MLA(hi, lo, X[14], MAD_F(0x0e313245));
1873 MAD_F_MLA(hi, lo, X[15], MAD_F(0x0898c779));
1874 MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0acf37ad));
1876 x[18] = x[35] = MAD_F_MLZ(hi, lo) - t1;
1878 MAD_F_ML0(hi, lo, X[4], MAD_F(0x061f78aa));
1879 MAD_F_MLA(hi, lo, X[13], -MAD_F(0x0ec835e8));
1881 t7 = MAD_F_MLZ(hi, lo);
1883 MAD_F_MLA(hi, lo, X[1], -MAD_F(0x0cb19346));
1884 MAD_F_MLA(hi, lo, X[7], MAD_F(0x0fdcf549));
1885 MAD_F_MLA(hi, lo, X[10], MAD_F(0x0216a2a2));
1886 MAD_F_MLA(hi, lo, X[16], -MAD_F(0x09bd7ca0));
1888 t2 = MAD_F_MLZ(hi, lo);
1890 MAD_F_MLA(hi, lo, X[0], MAD_F(0x04cfb0e2));
1891 MAD_F_MLA(hi, lo, X[2], MAD_F(0x0ffc19fd));
1892 MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0d7e8807));
1893 MAD_F_MLA(hi, lo, X[5], MAD_F(0x03768962));
1894 MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0bcbe352));
1895 MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0e313245));
1896 MAD_F_MLA(hi, lo, X[9], MAD_F(0x07635284));
1897 MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0acf37ad));
1898 MAD_F_MLA(hi, lo, X[12], MAD_F(0x0f9ee890));
1899 MAD_F_MLA(hi, lo, X[14], MAD_F(0x0898c779));
1900 MAD_F_MLA(hi, lo, X[15], MAD_F(0x00b2aa3e));
1901 MAD_F_MLA(hi, lo, X[17], MAD_F(0x0f426cb5));
1903 x[5] = MAD_F_MLZ(hi, lo);
1906 MAD_F_ML0(hi, lo, X[0], MAD_F(0x0acf37ad));
1907 MAD_F_MLA(hi, lo, X[2], -MAD_F(0x0898c779));
1908 MAD_F_MLA(hi, lo, X[3], MAD_F(0x0e313245));
1909 MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0f426cb5));
1910 MAD_F_MLA(hi, lo, X[6], -MAD_F(0x03768962));
1911 MAD_F_MLA(hi, lo, X[8], MAD_F(0x00b2aa3e));
1912 MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0ffc19fd));
1913 MAD_F_MLA(hi, lo, X[11], MAD_F(0x0f9ee890));
1914 MAD_F_MLA(hi, lo, X[12], -MAD_F(0x04cfb0e2));
1915 MAD_F_MLA(hi, lo, X[14], MAD_F(0x07635284));
1916 MAD_F_MLA(hi, lo, X[15], MAD_F(0x0d7e8807));
1917 MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0bcbe352));
1919 x[0] = MAD_F_MLZ(hi, lo) + t2;
1922 MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0f9ee890));
1923 MAD_F_MLA(hi, lo, X[2], -MAD_F(0x07635284));
1924 MAD_F_MLA(hi, lo, X[3], -MAD_F(0x00b2aa3e));
1925 MAD_F_MLA(hi, lo, X[5], MAD_F(0x0bcbe352));
1926 MAD_F_MLA(hi, lo, X[6], MAD_F(0x0f426cb5));
1927 MAD_F_MLA(hi, lo, X[8], MAD_F(0x0d7e8807));
1928 MAD_F_MLA(hi, lo, X[9], MAD_F(0x0898c779));
1929 MAD_F_MLA(hi, lo, X[11], -MAD_F(0x04cfb0e2));
1930 MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0acf37ad));
1931 MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0ffc19fd));
1932 MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0e313245));
1933 MAD_F_MLA(hi, lo, X[17], -MAD_F(0x03768962));
1935 x[24] = x[29] = MAD_F_MLZ(hi, lo) + t2;
1937 MAD_F_ML0(hi, lo, X[1], -MAD_F(0x0216a2a2));
1938 MAD_F_MLA(hi, lo, X[7], -MAD_F(0x09bd7ca0));
1939 MAD_F_MLA(hi, lo, X[10], MAD_F(0x0cb19346));
1940 MAD_F_MLA(hi, lo, X[16], MAD_F(0x0fdcf549));
1942 t3 = MAD_F_MLZ(hi, lo) + t7;
1944 MAD_F_ML0(hi, lo, X[0], MAD_F(0x00b2aa3e));
1945 MAD_F_MLA(hi, lo, X[2], MAD_F(0x03768962));
1946 MAD_F_MLA(hi, lo, X[3], -MAD_F(0x04cfb0e2));
1947 MAD_F_MLA(hi, lo, X[5], -MAD_F(0x07635284));
1948 MAD_F_MLA(hi, lo, X[6], MAD_F(0x0898c779));
1949 MAD_F_MLA(hi, lo, X[8], MAD_F(0x0acf37ad));
1950 MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0bcbe352));
1951 MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0d7e8807));
1952 MAD_F_MLA(hi, lo, X[12], MAD_F(0x0e313245));
1953 MAD_F_MLA(hi, lo, X[14], MAD_F(0x0f426cb5));
1954 MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0f9ee890));
1955 MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0ffc19fd));
1957 x[8] = MAD_F_MLZ(hi, lo) + t3;
1960 MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0e313245));
1961 MAD_F_MLA(hi, lo, X[2], MAD_F(0x0bcbe352));
1962 MAD_F_MLA(hi, lo, X[3], MAD_F(0x0f9ee890));
1963 MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0898c779));
1964 MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0ffc19fd));
1965 MAD_F_MLA(hi, lo, X[8], MAD_F(0x04cfb0e2));
1966 MAD_F_MLA(hi, lo, X[9], MAD_F(0x0f426cb5));
1967 MAD_F_MLA(hi, lo, X[11], -MAD_F(0x00b2aa3e));
1968 MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0d7e8807));
1969 MAD_F_MLA(hi, lo, X[14], -MAD_F(0x03768962));
1970 MAD_F_MLA(hi, lo, X[15], MAD_F(0x0acf37ad));
1971 MAD_F_MLA(hi, lo, X[17], MAD_F(0x07635284));
1973 x[21] = x[32] = MAD_F_MLZ(hi, lo) + t3;
1975 MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0d7e8807));
1976 MAD_F_MLA(hi, lo, X[2], MAD_F(0x0f426cb5));
1977 MAD_F_MLA(hi, lo, X[3], MAD_F(0x0acf37ad));
1978 MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0ffc19fd));
1979 MAD_F_MLA(hi, lo, X[6], -MAD_F(0x07635284));
1980 MAD_F_MLA(hi, lo, X[8], MAD_F(0x0f9ee890));
1981 MAD_F_MLA(hi, lo, X[9], MAD_F(0x03768962));
1982 MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0e313245));
1983 MAD_F_MLA(hi, lo, X[12], MAD_F(0x00b2aa3e));
1984 MAD_F_MLA(hi, lo, X[14], MAD_F(0x0bcbe352));
1985 MAD_F_MLA(hi, lo, X[15], -MAD_F(0x04cfb0e2));
1986 MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0898c779));
1988 x[20] = x[33] = MAD_F_MLZ(hi, lo) - t3;
1990 MAD_F_ML0(hi, lo, t14, -MAD_F(0x0ec835e8));
1991 MAD_F_MLA(hi, lo, t15, MAD_F(0x061f78aa));
1993 t4 = MAD_F_MLZ(hi, lo) - t7;
1995 MAD_F_ML0(hi, lo, t12, MAD_F(0x061f78aa));
1996 MAD_F_MLA(hi, lo, t13, MAD_F(0x0ec835e8));
1998 x[4] = MAD_F_MLZ(hi, lo) + t4;
2001 MAD_F_ML0(hi, lo, t8, MAD_F(0x09bd7ca0));
2002 MAD_F_MLA(hi, lo, t9, -MAD_F(0x0216a2a2));
2003 MAD_F_MLA(hi, lo, t10, MAD_F(0x0fdcf549));
2004 MAD_F_MLA(hi, lo, t11, -MAD_F(0x0cb19346));
2006 x[1] = MAD_F_MLZ(hi, lo) + t4;
2009 MAD_F_ML0(hi, lo, t8, -MAD_F(0x0fdcf549));
2010 MAD_F_MLA(hi, lo, t9, -MAD_F(0x0cb19346));
2011 MAD_F_MLA(hi, lo, t10, -MAD_F(0x09bd7ca0));
2012 MAD_F_MLA(hi, lo, t11, -MAD_F(0x0216a2a2));
2014 x[25] = x[28] = MAD_F_MLZ(hi, lo) + t4;
2016 MAD_F_ML0(hi, lo, X[1], -MAD_F(0x0fdcf549));
2017 MAD_F_MLA(hi, lo, X[7], -MAD_F(0x0cb19346));
2018 MAD_F_MLA(hi, lo, X[10], -MAD_F(0x09bd7ca0));
2019 MAD_F_MLA(hi, lo, X[16], -MAD_F(0x0216a2a2));
2021 t5 = MAD_F_MLZ(hi, lo) - t6;
2023 MAD_F_ML0(hi, lo, X[0], MAD_F(0x0898c779));
2024 MAD_F_MLA(hi, lo, X[2], MAD_F(0x04cfb0e2));
2025 MAD_F_MLA(hi, lo, X[3], MAD_F(0x0bcbe352));
2026 MAD_F_MLA(hi, lo, X[5], MAD_F(0x00b2aa3e));
2027 MAD_F_MLA(hi, lo, X[6], MAD_F(0x0e313245));
2028 MAD_F_MLA(hi, lo, X[8], -MAD_F(0x03768962));
2029 MAD_F_MLA(hi, lo, X[9], MAD_F(0x0f9ee890));
2030 MAD_F_MLA(hi, lo, X[11], -MAD_F(0x07635284));
2031 MAD_F_MLA(hi, lo, X[12], MAD_F(0x0ffc19fd));
2032 MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0acf37ad));
2033 MAD_F_MLA(hi, lo, X[15], MAD_F(0x0f426cb5));
2034 MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0d7e8807));
2036 x[2] = MAD_F_MLZ(hi, lo) + t5;
2039 MAD_F_ML0(hi, lo, X[0], MAD_F(0x07635284));
2040 MAD_F_MLA(hi, lo, X[2], MAD_F(0x0acf37ad));
2041 MAD_F_MLA(hi, lo, X[3], MAD_F(0x03768962));
2042 MAD_F_MLA(hi, lo, X[5], MAD_F(0x0d7e8807));
2043 MAD_F_MLA(hi, lo, X[6], -MAD_F(0x00b2aa3e));
2044 MAD_F_MLA(hi, lo, X[8], MAD_F(0x0f426cb5));
2045 MAD_F_MLA(hi, lo, X[9], -MAD_F(0x04cfb0e2));
2046 MAD_F_MLA(hi, lo, X[11], MAD_F(0x0ffc19fd));
2047 MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0898c779));
2048 MAD_F_MLA(hi, lo, X[14], MAD_F(0x0f9ee890));
2049 MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0bcbe352));
2050 MAD_F_MLA(hi, lo, X[17], MAD_F(0x0e313245));
2052 x[3] = MAD_F_MLZ(hi, lo) + t5;
2055 MAD_F_ML0(hi, lo, X[0], -MAD_F(0x0ffc19fd));
2056 MAD_F_MLA(hi, lo, X[2], -MAD_F(0x0f9ee890));
2057 MAD_F_MLA(hi, lo, X[3], -MAD_F(0x0f426cb5));
2058 MAD_F_MLA(hi, lo, X[5], -MAD_F(0x0e313245));
2059 MAD_F_MLA(hi, lo, X[6], -MAD_F(0x0d7e8807));
2060 MAD_F_MLA(hi, lo, X[8], -MAD_F(0x0bcbe352));
2061 MAD_F_MLA(hi, lo, X[9], -MAD_F(0x0acf37ad));
2062 MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0898c779));
2063 MAD_F_MLA(hi, lo, X[12], -MAD_F(0x07635284));
2064 MAD_F_MLA(hi, lo, X[14], -MAD_F(0x04cfb0e2));
2065 MAD_F_MLA(hi, lo, X[15], -MAD_F(0x03768962));
2066 MAD_F_MLA(hi, lo, X[17], -MAD_F(0x00b2aa3e));
2068 x[26] = x[27] = MAD_F_MLZ(hi, lo) + t5;
2073 * NAME: III_imdct_l()
2074 * DESCRIPTION: perform IMDCT and windowing for long blocks
2077 void III_imdct_l(mad_fixed_t const X[18], mad_fixed_t z[36],
2078 unsigned int block_type)
2088 switch (block_type) {
2089 case 0: /* normal window */
2090 # if defined(ASO_INTERLEAVE1)
2092 register mad_fixed_t tmp1, tmp2;
2097 for (i = 0; i < 34; i += 2) {
2098 z[i + 0] = mad_f_mul(z[i + 0], tmp1);
2099 tmp1 = window_l[i + 2];
2100 z[i + 1] = mad_f_mul(z[i + 1], tmp2);
2101 tmp2 = window_l[i + 3];
2104 z[34] = mad_f_mul(z[34], tmp1);
2105 z[35] = mad_f_mul(z[35], tmp2);
2107 # elif defined(ASO_INTERLEAVE2)
2109 register mad_fixed_t tmp1, tmp2;
2114 for (i = 0; i < 35; ++i) {
2115 z[i] = mad_f_mul(tmp1, tmp2);
2117 tmp2 = window_l[i + 1];
2120 z[35] = mad_f_mul(tmp1, tmp2);
2123 for (i = 0; i < 36; i += 4) {
2124 z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
2125 z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
2126 z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
2127 z[i + 3] = mad_f_mul(z[i + 3], window_l[i + 3]);
2130 for (i = 0; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
2134 case 1: /* start block */
2135 for (i = 0; i < 18; i += 3) {
2136 z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
2137 z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
2138 z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
2140 /* (i = 18; i < 24; ++i) z[i] unchanged */
2141 for (i = 24; i < 30; ++i) z[i] = mad_f_mul(z[i], window_s[i - 18]);
2142 for (i = 30; i < 36; ++i) z[i] = 0;
2145 case 3: /* stop block */
2146 for (i = 0; i < 6; ++i) z[i] = 0;
2147 for (i = 6; i < 12; ++i) z[i] = mad_f_mul(z[i], window_s[i - 6]);
2148 /* (i = 12; i < 18; ++i) z[i] unchanged */
2149 for (i = 18; i < 36; i += 3) {
2150 z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
2151 z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
2152 z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
2157 # endif /* ASO_IMDCT */
2160 * NAME: III_imdct_s()
2161 * DESCRIPTION: perform IMDCT and windowing for short blocks
2164 void III_imdct_s(mad_fixed_t const X[18], mad_fixed_t z[36])
2166 mad_fixed_t y[36], *yptr;
2167 mad_fixed_t const *wptr;
2169 register mad_fixed64hi_t hi;
2170 register mad_fixed64lo_t lo;
2176 for (w = 0; w < 3; ++w) {
2177 register mad_fixed_t const (*s)[6];
2181 for (i = 0; i < 3; ++i) {
2182 MAD_F_ML0(hi, lo, X[0], (*s)[0]);
2183 MAD_F_MLA(hi, lo, X[1], (*s)[1]);
2184 MAD_F_MLA(hi, lo, X[2], (*s)[2]);
2185 MAD_F_MLA(hi, lo, X[3], (*s)[3]);
2186 MAD_F_MLA(hi, lo, X[4], (*s)[4]);
2187 MAD_F_MLA(hi, lo, X[5], (*s)[5]);
2189 yptr[i + 0] = MAD_F_MLZ(hi, lo);
2190 yptr[5 - i] = -yptr[i + 0];
2194 MAD_F_ML0(hi, lo, X[0], (*s)[0]);
2195 MAD_F_MLA(hi, lo, X[1], (*s)[1]);
2196 MAD_F_MLA(hi, lo, X[2], (*s)[2]);
2197 MAD_F_MLA(hi, lo, X[3], (*s)[3]);
2198 MAD_F_MLA(hi, lo, X[4], (*s)[4]);
2199 MAD_F_MLA(hi, lo, X[5], (*s)[5]);
2201 yptr[ i + 6] = MAD_F_MLZ(hi, lo);
2202 yptr[11 - i] = yptr[i + 6];
2211 /* windowing, overlapping and concatenation */
2214 wptr = &window_s[0];
2216 for (i = 0; i < 6; ++i) {
2218 z[i + 6] = mad_f_mul(yptr[ 0 + 0], wptr[0]);
2220 MAD_F_ML0(hi, lo, yptr[ 0 + 6], wptr[6]);
2221 MAD_F_MLA(hi, lo, yptr[12 + 0], wptr[0]);
2223 z[i + 12] = MAD_F_MLZ(hi, lo);
2225 MAD_F_ML0(hi, lo, yptr[12 + 6], wptr[6]);
2226 MAD_F_MLA(hi, lo, yptr[24 + 0], wptr[0]);
2228 z[i + 18] = MAD_F_MLZ(hi, lo);
2230 z[i + 24] = mad_f_mul(yptr[24 + 6], wptr[6]);
2239 * NAME: III_overlap()
2240 * DESCRIPTION: perform overlap-add of windowed IMDCT outputs
2243 void III_overlap(mad_fixed_t const output[36], mad_fixed_t overlap[18],
2244 mad_fixed_t sample[18][32], unsigned int sb)
2248 # if defined(ASO_INTERLEAVE2)
2250 register mad_fixed_t tmp1, tmp2;
2255 for (i = 0; i < 16; i += 2) {
2256 sample[i + 0][sb] = output[i + 0 + 0] + tmp1;
2257 overlap[i + 0] = output[i + 0 + 18];
2258 tmp1 = overlap[i + 2];
2260 sample[i + 1][sb] = output[i + 1 + 0] + tmp2;
2261 overlap[i + 1] = output[i + 1 + 18];
2262 tmp2 = overlap[i + 3];
2265 sample[16][sb] = output[16 + 0] + tmp1;
2266 overlap[16] = output[16 + 18];
2267 sample[17][sb] = output[17 + 0] + tmp2;
2268 overlap[17] = output[17 + 18];
2271 for (i = 0; i < 18; i += 2) {
2272 sample[i + 0][sb] = output[i + 0 + 0] + overlap[i + 0];
2273 overlap[i + 0] = output[i + 0 + 18];
2275 sample[i + 1][sb] = output[i + 1 + 0] + overlap[i + 1];
2276 overlap[i + 1] = output[i + 1 + 18];
2279 for (i = 0; i < 18; ++i) {
2280 sample[i][sb] = output[i + 0] + overlap[i];
2281 overlap[i] = output[i + 18];
2287 * NAME: III_overlap_z()
2288 * DESCRIPTION: perform "overlap-add" of zero IMDCT outputs
2291 void III_overlap_z(mad_fixed_t overlap[18],
2292 mad_fixed_t sample[18][32], unsigned int sb)
2296 # if defined(ASO_INTERLEAVE2)
2298 register mad_fixed_t tmp1, tmp2;
2303 for (i = 0; i < 16; i += 2) {
2304 sample[i + 0][sb] = tmp1;
2306 tmp1 = overlap[i + 2];
2308 sample[i + 1][sb] = tmp2;
2310 tmp2 = overlap[i + 3];
2313 sample[16][sb] = tmp1;
2315 sample[17][sb] = tmp2;
2319 for (i = 0; i < 18; ++i) {
2320 sample[i][sb] = overlap[i];
2327 * NAME: III_freqinver()
2328 * DESCRIPTION: perform subband frequency inversion for odd sample lines
2331 void III_freqinver(mad_fixed_t sample[18][32], unsigned int sb)
2335 # if 1 || defined(ASO_INTERLEAVE1) || defined(ASO_INTERLEAVE2)
2337 register mad_fixed_t tmp1, tmp2;
2339 tmp1 = sample[1][sb];
2340 tmp2 = sample[3][sb];
2342 for (i = 1; i < 13; i += 4) {
2343 sample[i + 0][sb] = -tmp1;
2344 tmp1 = sample[i + 4][sb];
2345 sample[i + 2][sb] = -tmp2;
2346 tmp2 = sample[i + 6][sb];
2349 sample[13][sb] = -tmp1;
2350 tmp1 = sample[17][sb];
2351 sample[15][sb] = -tmp2;
2352 sample[17][sb] = -tmp1;
2355 for (i = 1; i < 18; i += 2)
2356 sample[i][sb] = -sample[i][sb];
2360 #if TARGET_MSDOS == 16
2361 typedef mad_fixed_t mad_fixed_t_2x576[576];
2362 static mad_fixed_t_2x576* III_xr = NULL;
2366 * NAME: III_decode()
2367 * DESCRIPTION: decode frame main_data
2369 /* FIXME: The memory corruption issues seem to occur within this function
2370 * or within one of the subroutines. The compiler is evalulating
2371 * (*ptr)[2][3] etc... correctly, so that's not the problem.
2373 * The major culprit seems to be III_overlap_z, though that might
2374 * just be a side effect.
2376 * As far as I know, these issues do not occur in the 32-bit flat
2377 * mode builds. They only seem to happen when compiled as 16-bit
2378 * real mode code (large model). */
2380 enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
2381 struct sideinfo *si, unsigned int nch)
2383 struct mad_header *header = &frame->header;
2384 unsigned int sfreqi, ngr, gr;
2389 sfreq = header->samplerate;
2390 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2393 /* 48000 => 0, 44100 => 1, 32000 => 2,
2394 24000 => 3, 22050 => 4, 16000 => 5 */
2395 sfreqi = ((sfreq >> 7) & 0x000f) +
2396 ((sfreq >> 15) & 0x0001) - 8;
2398 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2402 /* scalefactors, Huffman decoding, requantization */
2404 ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2;
2406 #if TARGET_MSDOS == 16
2407 if (III_xr == NULL) {
2408 if ((III_xr = malloc(sizeof(*III_xr))) == NULL)
2409 return MAD_ERROR_NOMEM;
2413 for (gr = 0; gr < ngr; ++gr) {
2414 struct granule *granule = &si->gr[gr];
2415 unsigned char const *sfbwidth[2];
2416 #if TARGET_MSDOS == 16
2417 mad_fixed_t_2x576 *xr = III_xr; /* 16-bit real mode stacks are small: malloc() it instead */
2419 mad_fixed_t xr[2][576];
2422 enum mad_error error;
2424 for (ch = 0; ch < nch; ++ch) {
2425 struct channel *channel = &granule->ch[ch];
2426 unsigned int part2_length;
2428 sfbwidth[ch] = sfbwidth_table[sfreqi].l;
2429 if (channel->block_type == 2) {
2430 sfbwidth[ch] = (channel->flags & mixed_block_flag) ?
2431 sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s;
2434 if (header->flags & MAD_FLAG_LSF_EXT) {
2435 part2_length = III_scalefactors_lsf(ptr, channel,
2436 ch == 0 ? 0 : &si->gr[1].ch[1],
2437 header->mode_extension);
2440 part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
2441 gr == 0 ? 0 : si->scfsi[ch]);
2444 error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
2449 /* joint stereo processing */
2451 if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) {
2452 error = III_stereo(xr, granule, header, sfbwidth[0]);
2457 /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */
2459 for (ch = 0; ch < nch; ++ch) {
2460 struct channel const *channel = &granule->ch[ch];
2461 mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr];
2462 unsigned int sb, l, i, sblimit;
2463 mad_fixed_t output[36];
2465 if (channel->block_type == 2) {
2466 III_reorder(xr[ch], channel, sfbwidth[ch]);
2468 # if !defined(OPT_STRICT)
2470 * According to ISO/IEC 11172-3, "Alias reduction is not applied for
2471 * granules with block_type == 2 (short block)." However, other
2472 * sources suggest alias reduction should indeed be performed on the
2473 * lower two subbands of mixed blocks. Most other implementations do
2474 * this, so by default we will too.
2476 if (channel->flags & mixed_block_flag)
2477 III_aliasreduce(xr[ch], 36);
2481 III_aliasreduce(xr[ch], 576);
2487 if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) {
2488 unsigned int block_type;
2490 block_type = channel->block_type;
2491 if (channel->flags & mixed_block_flag)
2495 for (sb = 0; sb < 2; ++sb, l += 18) {
2496 III_imdct_l(&xr[ch][l], output, block_type);
2497 III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2502 for (sb = 0; sb < 2; ++sb, l += 18) {
2503 III_imdct_s(&xr[ch][l], output);
2504 III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2508 III_freqinver(sample, 1);
2510 /* (nonzero) subbands 2-31 */
2513 while (i > 36 && xr[ch][i - 1] == 0)
2516 sblimit = 32 - (576 - i) / 18;
2518 if (channel->block_type != 2) {
2520 for (sb = 2; sb < sblimit; ++sb, l += 18) {
2521 III_imdct_l(&xr[ch][l], output, channel->block_type);
2522 III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2525 III_freqinver(sample, sb);
2530 for (sb = 2; sb < sblimit; ++sb, l += 18) {
2531 III_imdct_s(&xr[ch][l], output);
2532 III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2535 III_freqinver(sample, sb);
2539 /* remaining (zero) subbands */
2541 for (sb = sblimit; sb < 32; ++sb) {
2542 III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
2545 III_freqinver(sample, sb);
2550 return MAD_ERROR_NONE;
2554 * NAME: layer->III()
2555 * DESCRIPTION: decode a single Layer III frame
2557 int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
2559 struct mad_header *header = &frame->header;
2560 unsigned int nch, priv_bitlen, next_md_begin = 0;
2561 unsigned int si_len, data_bitlen, md_len;
2562 unsigned int frame_space, frame_used, frame_free;
2563 struct mad_bitptr ptr;
2565 enum mad_error error;
2568 /* allocate Layer III dynamic structures */
2570 if (stream->main_data == 0) {
2571 stream->main_data = malloc(MAD_BUFFER_MDLEN);
2572 if (stream->main_data == 0) {
2573 stream->error = MAD_ERROR_NOMEM;
2578 if (frame->overlap == 0) {
2579 frame->overlap = calloc(2 * 32 * 18, sizeof(mad_fixed_t));
2580 if (frame->overlap == 0) {
2581 stream->error = MAD_ERROR_NOMEM;
2586 nch = MAD_NCHANNELS(header);
2587 si_len = (header->flags & MAD_FLAG_LSF_EXT) ?
2588 (nch == 1 ? 9 : 17) : (nch == 1 ? 17 : 32);
2590 /* check frame sanity */
2592 if (stream->next_frame - mad_bit_nextbyte(&stream->ptr) <
2593 (signed int) si_len) {
2594 stream->error = MAD_ERROR_BADFRAMELEN;
2599 /* check CRC word */
2601 if (header->flags & MAD_FLAG_PROTECTION) {
2603 mad_bit_crc(stream->ptr, si_len * CHAR_BIT, header->crc_check);
2605 if (header->crc_check != header->crc_target &&
2606 !(frame->options & MAD_OPTION_IGNORECRC)) {
2607 stream->error = MAD_ERROR_BADCRC;
2612 /* decode frame side information */
2614 error = III_sideinfo(&stream->ptr, nch, header->flags & MAD_FLAG_LSF_EXT,
2615 &si, &data_bitlen, &priv_bitlen);
2616 if (error && result == 0) {
2617 stream->error = error;
2621 header->flags |= priv_bitlen;
2622 header->private_bits |= si.private_bits;
2624 /* find main_data of next frame */
2627 struct mad_bitptr peek;
2628 unsigned long header;
2630 mad_bit_init(&peek, stream->next_frame);
2632 header = mad_bit_read(&peek, 32);
2633 if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
2634 if (!(header & 0x00010000L)) /* protection_bit */
2635 mad_bit_skip(&peek, 16); /* crc_check */
2638 mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
2641 mad_bit_finish(&peek);
2644 /* find main_data of this frame */
2646 frame_space = stream->next_frame - mad_bit_nextbyte(&stream->ptr);
2648 if (next_md_begin > si.main_data_begin + frame_space)
2651 md_len = si.main_data_begin + frame_space - next_md_begin;
2655 if (si.main_data_begin == 0) {
2659 frame_used = md_len;
2662 if (si.main_data_begin > stream->md_len) {
2664 stream->error = MAD_ERROR_BADDATAPTR;
2670 *stream->main_data + stream->md_len - si.main_data_begin);
2672 if (md_len > si.main_data_begin) {
2673 assert(stream->md_len + md_len -
2674 si.main_data_begin <= MAD_BUFFER_MDLEN);
2676 memcpy(*stream->main_data + stream->md_len,
2677 mad_bit_nextbyte(&stream->ptr),
2678 frame_used = md_len - si.main_data_begin);
2679 stream->md_len += frame_used;
2684 frame_free = frame_space - frame_used;
2686 /* decode main_data */
2689 error = III_decode(&ptr, frame, &si, nch);
2691 stream->error = error;
2695 /* designate ancillary bits */
2697 stream->anc_ptr = ptr;
2698 stream->anc_bitlen = md_len * CHAR_BIT - data_bitlen;
2701 # if 0 && defined(DEBUG)
2703 "main_data_begin:%u, md_len:%u, frame_free:%u, "
2704 "data_bitlen:%u, anc_bitlen: %u\n",
2705 si.main_data_begin, md_len, frame_free,
2706 data_bitlen, stream->anc_bitlen);
2709 /* preload main_data buffer with up to 511 bytes for next frame(s) */
2711 if (frame_free >= next_md_begin) {
2712 memcpy(*stream->main_data,
2713 stream->next_frame - next_md_begin, next_md_begin);
2714 stream->md_len = next_md_begin;
2717 if (md_len < si.main_data_begin) {
2720 extra = si.main_data_begin - md_len;
2721 if (extra + frame_free > next_md_begin)
2722 extra = next_md_begin - frame_free;
2724 if (extra < stream->md_len) {
2725 memmove(*stream->main_data,
2726 *stream->main_data + stream->md_len - extra, extra);
2727 stream->md_len = extra;
2733 memcpy(*stream->main_data + stream->md_len,
2734 stream->next_frame - frame_free, frame_free);
2735 stream->md_len += frame_free;