8000 xbmc/lib/libmad/layer3.c at Frodo · AIahmedshrf/xbmc · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2698 lines (2189 loc) · 68.5 KB

layer3.c

File metadata and controls

2698 lines (2189 loc) · 68.5 KB
/*
* libmad - MPEG audio decoder library
* Copyright (C) 2000-2004 Underbit Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: layer3.c,v 1.43 2004/01/23 09:41:32 rob Exp $
*/
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
# include "global.h"
# include <stdlib.h>
# include <string.h>
# ifdef HAVE_ASSERT_H
# include <assert.h>
# endif
# ifdef HAVE_LIMITS_H
# include <limits.h>
# else
# define CHAR_BIT 8
# endif
# include "fixed.h"
# include "bit.h"
# include "stream.h"
# include "frame.h"
# include "huffman.h"
# include "layer3.h"
/* --- Layer III ----------------------------------------------------------- */
enum {
count1table_select = 0x01,
scalefac_scale = 0x02,
preflag = 0x04,
mixed_block_flag = 0x08
};
enum {
I_STEREO = 0x1,
MS_STEREO = 0x2
};
struct sideinfo {
unsigned int main_data_begin;
unsigned int private_bits;
unsigned char scfsi[2];
struct granule {
struct channel {
/* from side info */
unsigned short part2_3_length;
unsigned short big_values;
unsigned short global_gain;
unsigned short scalefac_compress;
unsigned char flags;
unsigned char block_type;
unsigned char table_select[3];
unsigned char subblock_gain[3];
unsigned char region0_count;
unsigned char region1_count;
/* from main_data */
unsigned char scalefac[39]; /* scalefac_l and/or scalefac_s */
} ch[2];
} gr[2];
};
/*
* scalefactor bit lengths
* derived from section 2.4.2.7 of ISO/IEC 11172-3
*/
static
struct {
unsigned char slen1;
unsigned char slen2;
} const sflen_table[16] = {
{ 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
{ 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
{ 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
{ 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
};
/*
* number of LSF scalefactor band values
* derived from section 2.4.3.2 of ISO/IEC 13818-3
*/
static
unsigned char const nsfb_table[6][3][4] = {
{ { 6, 5, 5, 5 },
{ 9, 9, 9, 9 },
{ 6, 9, 9, 9 } },
{ { 6, 5, 7, 3 },
{ 9, 9, 12, 6 },
{ 6, 9, 12, 6 } },
{ { 11, 10, 0, 0 },
{ 18, 18, 0, 0 },
{ 15, 18, 0, 0 } },
{ { 7, 7, 7, 0 },
{ 12, 12, 12, 0 },
{ 6, 15, 12, 0 } },
{ { 6, 6, 6, 3 },
{ 12, 9, 9, 6 },
{ 6, 12, 9, 6 } },
{ { 8, 8, 5, 0 },
{ 15, 12, 9, 0 },
{ 6, 18, 9, 0 } }
};
/*
* MPEG-1 scalefactor band widths
* derived from Table B.8 of ISO/IEC 11172-3
*/
static
unsigned char const sfb_48000_long[] = {
4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192
};
static
unsigned char const sfb_44100_long[] = {
4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158
};
static
unsigned char const sfb_32000_long[] = {
4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26
};
static
unsigned char const sfb_48000_short[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
6, 6, 6, 6, 6, 10, 10, 10, 12, 12, 12, 14, 14,
14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
};
static
unsigned char const sfb_44100_short[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14,
14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
};
static
unsigned char const sfb_32000_short[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6,
6, 6, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20,
20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
};
static
unsigned char const sfb_48000_mixed[] = {
/* long */ 4, 4, 4, 4, 4, 4, 6, 6,
/* short */ 4, 4, 4, 6, 6, 6, 6, 6, 6, 10,
10, 10, 12, 12, 12, 14, 14, 14, 16, 16,
16, 20, 20, 20, 26, 26, 26, 66, 66, 66
};
static
unsigned char const sfb_44100_mixed[] = {
/* long */ 4, 4, 4, 4, 4, 4, 6, 6,
8
/* short */ 4, 4, 4, 6, 6, 6, 8, 8, 8, 10,
10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
18, 22, 22, 22, 30, 30, 30, 56, 56, 56
};
static
unsigned char const sfb_32000_mixed[] = {
/* long */ 4, 4, 4, 4, 4, 4, 6, 6,
/* short */ 4, 4, 4, 6, 6, 6, 8, 8, 8, 12,
12, 12, 16, 16, 16, 20, 20, 20, 26, 26,
26, 34, 34, 34, 42, 42, 42, 12, 12, 12
};
/*
* MPEG-2 scalefactor band widths
* derived from Table B.2 of ISO/IEC 13818-3
*/
static
unsigned char const sfb_24000_long[] = {
6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36
};
static
unsigned char const sfb_22050_long[] = {
6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54
};
# define sfb_16000_long sfb_22050_long
static
unsigned char const sfb_24000_short[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8,
8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
};
static
unsigned char const sfb_22050_short[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6,
6, 6, 8, 8, 8, 10, 10, 10, 14, 14, 14, 18, 18,
18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
};
static
unsigned char const sfb_16000_short[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8,
8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
};
static
unsigned char const sfb_24000_mixed[] = {
/* long */ 6, 6, 6, 6, 6, 6,
/* short */ 6, 6, 6, 8, 8, 8, 10, 10, 10, 12,
12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
24, 32, 32, 32, 44, 44, 44, 12, 12, 12
};
static
unsigned char const sfb_22050_mixed[] = {
/* long */ 6, 6, 6, 6, 6, 6,
/* short */ 6, 6, 6, 6, 6, 6, 8, 8, 8, 10,
10, 10, 14, 14, 14, 18, 18, 18, 26, 26,
26, 32, 32, 32, 42, 42, 42, 18, 18, 18
};
static
unsigned char const sfb_16000_mixed[] = {
/* long */ 6, 6, 6, 6, 6, 6,
/* short */ 6, 6, 6, 8, 8, 8, 10, 10, 10, 12,
12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
24, 30, 30, 30, 40, 40, 40, 18, 18, 18
};
/*
* MPEG 2.5 scalefactor band widths
* derived from public sources
*/
# define sfb_12000_long sfb_16000_long
# define sfb_11025_long sfb_12000_long
static
unsigned char const sfb_8000_long[] = {
12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2
};
# define sfb_12000_short sfb_16000_short
# define sfb_11025_short sfb_12000_short
static
unsigned char const sfb_8000_short[] = {
8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12, 16,
16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36,
36, 2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26
};
# define sfb_12000_mixed sfb_16000_mixed
# define sfb_11025_mixed sfb_12000_mixed
/* the 8000 Hz short block scalefactor bands do not break after
the first 36 frequency lines, so this is probably wrong */
static
unsigned char const sfb_8000_mixed[] = {
/* long */ 12, 12, 12,
/* short */ 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16,
20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36,
2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26
};
static
struct {
unsigned char const *l;
unsigned char const *s;
unsigned char const *m;
} const sfbwidth_table[9] = {
{ sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
{ sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
{ sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
{ sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
{ sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
{ sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
{ sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
{ sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
{ sfb_8000_long, sfb_8000_short, sfb_8000_mixed }
};
/*
* scalefactor band preemphasis (used only when preflag is set)
* derived from Table B.6 of ISO/IEC 11172-3
*/
static
unsigned char const pretab[22] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
};
/*
* table for requantization
*
* rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
*/
static
struct fixedfloat {
unsigned long mantissa : 27;
unsigned short exponent : 5;
} const rq_table[8207] = {
# include "rq_table.dat"
};
/*
* fractional powers of two
* used for requantization and joint stereo decoding
*
* root_table[3 + x] = 2^(x/4)
*/
static
mad_fixed_t const root_table[7] = {
MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
};
/*
* coefficients for aliasing reduction
* derived from Table B.9 of ISO/IEC 11172-3
*
* c[] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
* cs[i] = 1 / sqrt(1 + c[i]^2)
* ca[i] = c[i] / sqrt(1 + c[i]^2)
*/
static
mad_fixed_t const cs[8] = {
+MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
+MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
+MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
+MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
};
static
mad_fixed_t const ca[8] = {
-MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
-MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
-MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
-MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
};
/*
* IMDCT coefficients for short blocks
* derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
*
* imdct_s[i/even][k] = cos((PI / 24) * (2 * (i / 2) + 7) * (2 * k + 1))
* imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1))
*/
static
mad_fixed_t const imdct_s[6][6] = {
# include "imdct_s.dat"
};
# if !defined(ASO_IMDCT)
/*
* windowing coefficients for long blocks
* derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
*
* window_l[i] = sin((PI / 36) * (i + 1/2))
*/
static
mad_fixed_t const window_l[36] = {
MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
};
# endif /* ASO_IMDCT */
/*
* windowing coefficients for short blocks
* derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
*
* window_s[i] = sin((PI / 12) * (i + 1/2))
*/
static
mad_fixed_t const window_s[12] = {
MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
};
/*
* coefficients for intensity stereo processing
* derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
*
* is_ratio[i] = tan(i * (PI / 12))
* is_table[i] = is_ratio[i] / (1 + is_ratio[i])
*/
static
mad_fixed_t const is_table[7] = {
MAD_F(0x00000000) /* 0.000000000 */,
MAD_F(0x0361962f) /* 0.211324865 */,
MAD_F(0x05db3d74) /* 0.366025404 */,
MAD_F(0x08000000) /* 0.500000000 */,
MAD_F(0x0a24c28c) /* 0.633974596 */,
MAD_F(0x0c9e69d1) /* 0.788675135 */,
MAD_F(0x100 10BC8 00000) /* 1.000000000 */
};
/*
* coefficients for LSF intensity stereo processing
* derived from section 2.4.3.2 of ISO/IEC 13818-3
*
* is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
* is_lsf_table[1][i] = (1 / sqrt(2)) ^(i + 1)
*/
static
mad_fixed_t const is_lsf_table[2][15] = {
{
MAD_F(0x0d744fcd) /* 0.840896415 */,
MAD_F(0x0b504f33) /* 0.707106781 */,
MAD_F(0x09837f05) /* 0.594603558 */,
MAD_F(0x08000000) /* 0.500000000 */,
MAD_F(0x06ba27e6) /* 0.420448208 */,
MAD_F(0x05a8279a) /* 0.353553391 */,
MAD_F(0x04c1bf83) /* 0.297301779 */,
MAD_F(0x04000000) /* 0.250000000 */,
MAD_F(0x035d13f3) /* 0.210224104 */,
MAD_F(0x02d413cd) /* 0.176776695 */,
MAD_F(0x0260dfc1) /* 0.148650889 */,
MAD_F(0x02000000) /* 0.125000000 */,
MAD_F(0x01ae89fa) /* 0.105112052 */,
MAD_F(0x016a09e6) /* 0.088388348 */,
MAD_F(0x01306fe1) /* 0.074325445 */
}, {
MAD_F(0x0b504f33) /* 0.707106781 */,
MAD_F(0x08000000) /* 0.500000000 */,
MAD_F(0x05a8279a) /* 0.353553391 */,
MAD_F(0x04000000) /* 0.250000000 */,
MAD_F(0x02d413cd) /* 0.176776695 */,
MAD_F(0x02000000) /* 0.125000000 */,
MAD_F(0x016a09e6) /* 0.088388348 */,
MAD_F(0x01000000) /* 0.062500000 */,
MAD_F(0x00b504f3) /* 0.044194174 */,
MAD_F(0x00800000) /* 0.031250000 */,
MAD_F(0x005a827a) /* 0.022097087 */,
MAD_F(0x00400000) /* 0.015625000 */,
MAD_F(0x002d413d) /* 0.011048543 */,
MAD_F(0x00200000) /* 0.007812500 */,
MAD_F(0x0016a09e) /* 0.005524272 */
}
};
/*
* NAME: III_sideinfo()
* DESCRIPTION: decode frame side information from a bitstream
*/
static
enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
int lsf, struct sideinfo *si,
unsigned int *data_bitlen,
unsigned int *priv_bitlen)
{
unsigned int ngr, gr, ch, i;
enum mad_error result = MAD_ERROR_NONE;
*data_bitlen = 0;
*priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
si->private_bits = mad_bit_read(ptr, *priv_bitlen);
ngr = 1;
if (!lsf) {
ngr = 2;
for (ch = 0; ch < nch; ++ch)
si->scfsi[ch] = mad_bit_read(ptr, 4);
}
for (gr = 0; gr < ngr; ++gr) {
struct granule *granule = &si->gr[gr];
for (ch = 0; ch < nch; ++ch) {
struct channel *channel = &granule->ch[ch];
channel->part2_3_length = mad_bit_read(ptr, 12);
channel->big_values = mad_bit_read(ptr, 9);
channel->global_gain = mad_bit_read(ptr, 8);
channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
*data_bitlen += channel->part2_3_length;
if (channel->big_values > 288 && result == 0)
result = MAD_ERROR_BADBIGVALUES;
channel->flags = 0;
/* window_switching_flag */
if (mad_bit_read(ptr, 1)) {
channel->block_type = mad_bit_read(ptr, 2);
if (channel->block_type == 0 && result == 0)
result = MAD_ERROR_BADBLOCKTYPE;
if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
result = MAD_ERROR_BADSCFSI;
channel->region0_count = 7;
channel->region1_count = 36;
if (mad_bit_read(ptr, 1))
channel->flags |= mixed_block_flag;
else if (channel->block_type == 2)
channel->region0_count = 8;
for (i = 0; i < 2; ++i)
channel->table_select[i] = mad_bit_read(ptr, 5);
# if defined(DEBUG)
channel->table_select[2] = 4; /* not used */
# endif
for (i = 0; i < 3; ++i)
channel->subblock_gain[i] = mad_bit_read(ptr, 3);
}
else {
channel->block_type = 0;
for (i = 0; i < 3; ++i)
channel->table_select[i] = mad_bit_read(ptr, 5);
channel->region0_count = mad_bit_read(ptr, 4);
channel->region1_count = mad_bit_read(ptr, 3);
}
/* [preflag,] scalefac_scale, count1table_select */
channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
}
}
return result;
}
/*
* NAME: III_scalefactors_lsf()
* DESCRIPTION: decode channel scalefactors for LSF from a bitstream
*/
static
unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
struct channel *channel,
struct channel *gr1ch, int mode_extension)
{
struct mad_bitptr start;
unsigned int scalefac_compress, index, slen[4], part, n, i;
unsigned char const *nsfb;
start = *ptr;
scalefac_compress = channel->scalefac_compress;
index = (channel->block_type == 2) ?
((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
if (!((mode_extension & I_STEREO) && gr1ch)) {
if (scalefac_compress < 400) {
slen[0] = (scalefac_compress >> 4) / 5;
slen[1] = (scalefac_compress >> 4) % 5;
slen[2] = (scalefac_compress % 16) >> 2;
slen[3] = scalefac_compress % 4;
nsfb = nsfb_table[0][index];
}
else if (scalefac_compress < 500) {
scalefac_compress -= 400;
slen[0] = (scalefac_compress >> 2) / 5;
slen[1] = (scalefac_compress >> 2) % 5;
slen[2] = scalefac_compress % 4;
slen[3] = 0;
nsfb = nsfb_table[1][index];
}
else {
scalefac_compress -= 500;
slen[0] = scalefac_compress / 3;
slen[1] = scalefac_compress % 3;
slen[2] = 0;
slen[3] = 0;
channel->flags |= preflag;
nsfb = nsfb_table[2][index];
}
n = 0;
for (part = 0; part < 4; ++part) {
for (i = 0; i < nsfb[part]; ++i)
channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
}
while (n < 39)
channel->scalefac[n++] = 0;
}
else { /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
scalefac_compress >>= 1;
if (scalefac_compress < 180) {
slen[0] = scalefac_compress / 36;
slen[1] = (scalefac_compress % 36) / 6;
slen[2] = (scalefac_compress % 36) % 6;
slen[3] = 0;
nsfb = nsfb_table[3][index];
}
else if (scalefac_compress < 244) {
scalefac_compress -= 180;
slen[0] = (scalefac_compress % 64) >> 4;
slen[1] = (scalefac_compress % 16) >> 2;
slen[2] = scalefac_compress % 4;
slen[3] = 0;
nsfb = nsfb_table[4][index];
}
else {
scalefac_compress -= 244;
slen[0] = scalefac_compress / 3;
slen[1] = scalefac_compress % 3;
slen[2] = 0;
slen[3] = 0;
nsfb = nsfb_table[5][index];
}
n = 0;
for (part = 0; part < 4; ++part) {
unsigned int max, is_pos;
max = (1 << slen[part]) - 1;
for (i = 0; i < nsfb[part]; ++i) {
is_pos = mad_bit_read(ptr, slen[part]);
channel->scalefac[n] = is_pos;
gr1ch->scalefac[n++] = (is_pos == max);
}
}
while (n < 39) {
channel->scalefac[n] = 0;
gr1ch->scalefac[n++] = 0; /* apparently not illegal */
}
}
return mad_bit_length(&start, ptr);
}
/*
* NAME: III_scalefactors()
* DESCRIPTION: decode channel scalefactors of one granule from a bitstream
*/
static
unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
struct channel const *gr0ch, unsigned int scfsi)
{
struct mad_bitptr start;
unsigned int slen1, slen2, sfbi;
start = *ptr;
slen1 = sflen_table[channel->scalefac_compress].slen1;
slen2 = sflen_table[channel->scalefac_compress].slen2;
if (channel->block_type == 2) {
unsigned int nsfb;
sfbi = 0;
nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
while (nsfb--)
channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
nsfb = 6 * 3;
while (nsfb--)
channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
nsfb = 1 * 3;
while (nsfb--)
channel->scalefac[sfbi++] = 0;
}
else { /* channel->block_type != 2 */
if (scfsi & 0x8) {
for (sfbi = 0; sfbi < 6; ++sfbi)
channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
}
else {
for (sfbi = 0; sfbi < 6; ++sfbi)
channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
}
if (scfsi & 0x4) {
for (sfbi = 6; sfbi < 11; ++sfbi)
channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
}
else {
for (sfbi = 6; sfbi < 11; ++sfbi)
channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
}
if (scfsi & 0x2) {
for (sfbi = 11; sfbi < 16; ++sfbi)
channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
}
else {
for (sfbi = 11; sfbi < 16; ++sfbi)
channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
}
if (scfsi & 0x1) {
for (sfbi = 16; sfbi < 21; ++sfbi)
channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
}
else {
for (sfbi = 16; sfbi < 21; ++sfbi)
channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
}
channel->scalefac[21] = 0;
}
return mad_bit_length(&start, ptr);
}
/*
* The Layer III formula for requantization and scaling is defined by
* section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
*
* long blocks:
* xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
* 2^((1/4) * (global_gain - 210)) *
* 2^-(scalefac_multiplier *
* (scalefac_l[sfb] + preflag * pretab[sfb]))
*
* short blocks:
* xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
* 2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
* 2^-(scalefac_multiplier * scalefac_s[sfb][w])
*
* where:
* scalefac_multiplier = (scalefac_scale + 1) / 2
*
* The routines III_exponents() and III_requantize() facilitate this
* calculation.
*/
/*
* NAME: III_exponents()
* DESCRIPTION: calculate scalefactor exponents
*/
static
void III_exponents(struct channel const *channel,
unsigned char const *sfbwidth, signed int exponents[39])
{
signed int gain;
unsigned int scalefac_multiplier, sfbi;
gain = (signed int) channel->global_gain - 210;
scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
if (channel->block_type == 2) {
unsigned int l;
signed int gain0, gain1, gain2;
sfbi = l = 0;
if (channel->flags & mixed_block_flag) {
unsigned int premask;
premask = (channel->flags & preflag) ? ~0 : 0;
/* long block subbands 0-1 */
while (l < 36) {
exponents[sfbi] = gain -
(signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
scalefac_multiplier);
l += sfbwidth[sfbi++];
}
}
/* this is probably wrong for 8000 Hz short/mixed blocks */
gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
while (l < 576) {
exponents[sfbi + 0] = gain0 -
(signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
exponents[sfbi + 1] = gain1 -
(signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
exponents[sfbi + 2] = gain2 -
(signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
l += 3 * sfbwidth[sfbi];
sfbi += 3;
}
}
else { /* channel->block_type != 2 */
if (channel->flags & preflag) {
for (sfbi = 0; sfbi < 22; ++sfbi) {
exponents[sfbi] = gain -
(signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
scalefac_multiplier);
}
}
else {
for (sfbi = 0; sfbi < 22; ++sfbi) {
exponents[sfbi] = gain -
(signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
}
}
}
}
/*
* NAME: III_requantize()
* DESCRIPTION: requantize one (positive) value
*/
static
mad_fixed_t III_requantize(unsigned int value, signed int exp)
{
mad_fixed_t requantized;
signed int frac;
struct fixedfloat const *power;
frac = exp % 4; /* assumes sign(frac) == sign(exp) */
exp /= 4;
power = &rq_table[value];
requantized = power->mantissa;
exp += power->exponent;
if (exp < 0) {
if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
/* underflow */
requantized = 0;
}
else {
requantized += 1L << (-exp - 1);
requantized >>= -exp;
}
}
else {
if (exp >= 5) {
/* overflow */
# if defined(DEBUG)
fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
mad_f_todouble(requantized), exp);
# endif
requantized = MAD_F_MAX;
}
else
requantized <<= exp;
}
return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
}
/* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
# define MASK(cache, sz, bits) \
(((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
# define MASK1BIT(cache, sz) \
((cache) & (1 << ((sz) - 1)))
/*
* NAME: III_huffdecode()
* DESCRIPTION: decode Huffman code words of one channel of one granule
*/
static
enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
struct channel *channel,
unsigned char const *sfbwidth,
unsigned int part2_length)
{
signed int exponents[39], exp;
signed int const *expptr;
struct mad_bitptr peek;
signed int bits_left, cachesz;
register mad_fixed_t *xrptr;
mad_fixed_t const *sfbound;
register unsigned long bitcache;
bits_left = (signed) channel->part2_3_length - (signed) part2_length;
if (bits_left < 0)
return MAD_ERROR_BADPART3LEN;
III_exponents(channel, sfbwidth, exponents);
peek = *ptr;
mad_bit_skip(ptr, bits_left);
/* align bit reads to byte boundaries */
cachesz = mad_bit_bitsleft(&peek);
cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
bitcache = mad_bit_read(&peek, cachesz);
bits_left -= cachesz;
xrptr = &xr[0];
/* big_values */
{
unsigned int region, rcount;
struct hufftable const *entry;
union huffpair const *table;
unsigned int linbits, startbits, big_values, reqhits;
mad_fixed_t reqcache[16];
sfbound = xrptr + *sfbwidth++;
rcount = channel->region0_count + 1;
entry = &mad_huff_pair_table[channel->table_select[region = 0]];
table = entry->table;
linbits = entry->linbits;
startbits = entry->startbits;
if (table == 0)
return MAD_ERROR_BADHUFFTABLE;
expptr = &exponents[0];
exp = *expptr++;
reqhits = 0;
big_values = channel->big_values;
while (big_values-- && cachesz + bits_left > 0) {
union huffpair const *pair;
unsigned int clumpsz, value;
register mad_fixed_t requantized;
if (xrptr == sfbound) {
sfbound += *sfbwidth++;
/* change table if region boundary */
if (--rcount == 0) {
if (region == 0)
0