blob: 2f5528f74cd891a208080f5aac605ae77abe8f5b [file] [log] [blame]
</
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 Antonius Hellmann
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Michael Sevakis4fc717a2006-08-28 22:38:41 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22// Shine is an MP3 encoder
23// Copyright (C) 1999-2000 Gabriel Bouvigne
24//
25// This library is free software; you can redistribute it and/or
26// modify it under the terms of the GNU Library General Public
27// License as published by the Free Software Foundation; either
28// version 2 of the License, or (at your option) any later version.
29//
30// This library is distributed in the hope that it will be useful,
31// but WITHOUT ANY WARRANTY; without even the implied warranty of
32// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33// Library General Public License for more details.
34
Michael Sevakis0f5cb942006-11-06 18:07:30 +000035#include <inttypes.h>
Thom Johansen6e219f82006-09-09 01:35:12 +000036#include "codeclib.h"
37
Michael Sevakis0f5cb942006-11-06 18:07:30 +000038CODEC_ENC_HEADER
39
40#define ENC_PADDING_FRAMES1 2
41#define ENC_PADDING_FRAMES2 4
42#define ENC_DELAY_SAMP 576
43#define ENC_DELAY_SIZE (ENC_DELAY_SAMP*4)
44#define SAMP_PER_FRAME1 1152
45#define SAMP_PER_FRAME2 576
46#define PCM_CHUNK_SIZE1 (SAMP_PER_FRAME1*4)
47#define PCM_CHUNK_SIZE2 (SAMP_PER_FRAME2*4)
48#define SAMPL2 576
49#define SBLIMIT 32
50#define HTN 16
51#define memcpy ci->memcpy
52#define memset ci->memset
53#define putlong(c, s) if(s+sz <= 32) { cc = (cc << s) | c; sz+= s; } \
54 else { putbits(cc, sz); cc = c; sz = s; }
Michael Sevakis4fc717a2006-08-28 22:38:41 +000055
Michael Sevakis4fc717a2006-08-28 22:38:41 +000056typedef struct {
Michael Sevakis0f5cb942006-11-06 18:07:30 +000057 int type; /* 0=(MPEG2 - 22.05,24,16kHz) 1=(MPEG1 - 44.1,48,32kHz) */
Thom Johansen6e219f82006-09-09 01:35:12 +000058 int mode; /* 0=stereo, 1=jstereo, 2=dual, 3=mono */
59 int bitrate;
60 int padding;
61 int num_bands;
62 long bitr_id;
63 int smpl_id;
Michael Sevakis4fc717a2006-08-28 22:38:41 +000064} mpeg_t;
65
66/* Side information */
67typedef struct {
Michael Sevakis0f5cb942006-11-06 18:07:30 +000068 uint32_t part2_3_length;
Thom Johansen6e219f82006-09-09 01:35:12 +000069 int count1; /* number of 0-1-quadruples */
Michael Sevakis0f5cb942006-11-06 18:07:30 +000070 uint32_t global_gain;
71 uint32_t table_select[4];
72 uint32_t region_0_1;
73 uint32_t address1;
74 uint32_t address2;
75 uint32_t address3;
Antonius Hellmann75807212008-06-02 19:28:08 +000076 long quantStep;
77 long additStep;
78 uint32_t max_val;
Michael Sevakis4fc717a2006-08-28 22:38:41 +000079} side_info_t;
80
81typedef struct {
Michael Sevakis4fc717a2006-08-28 22:38:41 +000082 side_info_t cod_info[2][2];
Thom Johansen6e219f82006-09-09 01:35:12 +000083 mpeg_t mpg;
Michael Sevakis4fc717a2006-08-28 22:38:41 +000084 long frac_per_frame;
85 long byte_per_frame;
86 long slot_lag;
87 int sideinfo_len;
88 int mean_bits;
Thom Johansen6e219f82006-09-09 01:35:12 +000089 int ResvSize;
Michael Sevakis4fc717a2006-08-28 22:38:41 +000090 int channels;
Peter D'Hoye528fe442008-10-08 22:18:16 +000091 int rec_mono_mode;
Thom Johansen6e219f82006-09-09 01:35:12 +000092 int granules;
Michael Sevakis4fc717a2006-08-28 22:38:41 +000093 long samplerate;
Michael Sevakis4fc717a2006-08-28 22:38:41 +000094} config_t;
95
96typedef struct {
Antonius Hellmann75807212008-06-02 19:28:08 +000097 int bitpos; /* current bitpos for writing */
Michael Sevakis0f5cb942006-11-06 18:07:30 +000098 uint32_t bbuf[263];
Michael Sevakis4fc717a2006-08-28 22:38:41 +000099} BF_Data;
100
101struct huffcodetab {
Antonius Hellmann75807212008-06-02 19:28:08 +0000102 int len; /* max. index */
103 const uint8_t *table; /* pointer to array[len][len] */
104 const uint8_t *hlen; /* pointer to array[len][len] */
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000105};
106
Thom Johansen6e219f82006-09-09 01:35:12 +0000107struct huffcodebig {
108 int len; /* max. index */
109 int linbits; /* number of linbits */
110 int linmax; /* max number stored in linbits */
111};
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000112
Thom Johansen6e219f82006-09-09 01:35:12 +0000113#define shft4(x) ((x + 8) >> 4)
114#define shft9(x) ((x + 256) >> 9)
115#define shft13(x) ((x + 4096) >> 13)
116#define shft15(x) ((x + 16384) >> 15)
117#define shft16(x) ((x + 32768) >> 16)
118#define shft_n(x,n) ((x) >> n)
119#define SQRT 724 /* sqrt(2) * 512 */
120
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000121static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */
122static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */
Antonius Hellmann75807212008-06-02 19:28:08 +0000123static int mdct_freq [SAMPL2] IBSS_ATTR; /* 2304 Bytes */
124static char mdct_sign [SAMPL2] IBSS_ATTR; /* 576 Bytes */
125static short enc_data [SAMPL2] IBSS_ATTR; /* 1152 Bytes */
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000126static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */
127static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */
128static int ca [8] IBSS_ATTR; /* 32 Bytes */
129static int cs [8] IBSS_ATTR; /* 32 Bytes */
130static int cx [9] IBSS_ATTR; /* 36 Bytes */
131static int win [18][4] IBSS_ATTR; /* 288 Bytes */
132static short enwindow [15*27+24] IBSS_ATTR; /* 862 Bytes */
133static short int2idx [4096] IBSS_ATTR; /* 8192 Bytes */
134static uint8_t ht_count [2][2][16] IBSS_ATTR; /* 64 Bytes */
135static uint32_t tab01 [ 16] IBSS_ATTR; /* 64 Bytes */
136static uint32_t tab23 [ 9] IBSS_ATTR; /* 36 Bytes */
137static uint32_t tab56 [ 16] IBSS_ATTR; /* 64 Bytes */
138static uint32_t tab1315 [256] IBSS_ATTR; /* 1024 Bytes */
139static uint32_t tab1624 [256] IBSS_ATTR; /* 1024 Bytes */
140static uint32_t tab789 [ 36] IBSS_ATTR; /* 144 Bytes */
141static uint32_t tabABC [ 64] IBSS_ATTR; /* 256 Bytes */
142static uint8_t t1HB [ 4] IBSS_ATTR;
143static uint8_t t2HB [ 9] IBSS_ATTR;
144static uint8_t t3HB [ 9] IBSS_ATTR;
145static uint8_t t5HB [ 16] IBSS_ATTR;
146static uint8_t t6HB [ 16] IBSS_ATTR;
147static uint8_t t7HB [ 36] IBSS_ATTR;
148static uint8_t t8HB [ 36] IBSS_ATTR;
149static uint8_t t9HB [ 36] IBSS_ATTR;
150static uint8_t t10HB [ 64] IBSS_ATTR;
151static uint8_t t11HB [ 64] IBSS_ATTR;
152static uint8_t t12HB [ 64] IBSS_ATTR;
153static uint8_t t13HB [256] IBSS_ATTR;
154static uint8_t t15HB [256] IBSS_ATTR;
155static uint16_t t16HB [256] IBSS_ATTR;
156static uint16_t t24HB [256] IBSS_ATTR;
157static uint8_t t1l [ 8] IBSS_ATTR;
158static uint8_t t2l [ 9] IBSS_ATTR;
159static uint8_t t3l [ 9] IBSS_ATTR;
160static uint8_t t5l [ 16] IBSS_ATTR;
161static uint8_t t6l [ 16] IBSS_ATTR;
162static uint8_t t7l [ 36] IBSS_ATTR;
163static uint8_t t8l [ 36] IBSS_ATTR;
164static uint8_t t9l [ 36] IBSS_ATTR;
165static uint8_t t10l [ 64] IBSS_ATTR;
166static uint8_t t11l [ 64] IBSS_ATTR;
167static uint8_t t12l [ 64] IBSS_ATTR;
168static uint8_t t13l [256] IBSS_ATTR;
169static uint8_t t15l [256] IBSS_ATTR;
170static uint8_t t16l [256] IBSS_ATTR;
171static uint8_t t24l [256] IBSS_ATTR;
172static struct huffcodetab ht [HTN] IBSS_ATTR;
Thom Johansen6e219f82006-09-09 01:35:12 +0000173
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000174static unsigned pcm_chunk_size IBSS_ATTR;
175static unsigned samp_per_frame IBSS_ATTR;
176
177static config_t cfg IBSS_ATTR;
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000178static char *res_buffer;
Michael Sevakisd52f2e42007-02-09 18:11:11 +0000179static int32_t err IBSS_ATTR;
Antonius Hellmann75807212008-06-02 19:28:08 +0000180static uint8_t band_scale_f[22];
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000181
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000182static const uint8_t ht_count_const[2][2][16] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000183{ { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */
184 { 1, 5, 5, 7, 5, 8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } }, /* hleng0 */
185 { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, /* table1 */
186 { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000187
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000188static const uint8_t t1HB_const[4] = {1,1,1,0};
189static const uint8_t t2HB_const[9] = {1,2,1,3,1,1,3,2,0};
190static const uint8_t t3HB_const[9] = {3,2,1,1,1,1,3,2,0};
191static const uint8_t t5HB_const[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0};
192static const uint8_t t6HB_const[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0};
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000193
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000194static const uint8_t t7HB_const[36] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000195{ 1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4,
196 12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 };
197
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000198static const uint8_t t8HB_const[36] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000199{ 3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3,
200 19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 };
201
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000202static const uint8_t t9HB_const[36] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000203{ 7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5,
204 15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 };
205
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000206static const uint8_t t10HB_const[64] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000207{1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32,
208 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22,
209 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 };
210
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000211static const uint8_t t11HB_const[64] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000212{3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30,
213 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26,
214 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 };
215
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000216static const uint8_t t12HB_const[64] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000217{9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21,
21830,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17,
21931,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 };
220
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000221static const uint8_t t13HB_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000222{1,5,14,21,34,51,46,71,42,52,68,52,67,44,43,19,3,4,12,19,31,26,44,33,31,24,32,
223 24,31,35,22,14,15,13,23,36,59,49,77,65,29,40,30,40,27,33,42,16,22,20,37,61,56,
224 79,73,64,43,76,56,37,26,31,25,14,35,16,60,57,97,75,114,91,54,73,55,41,48,53,
225 23,24,58,27,50,96,76,70,93,84,77,58,79,29,74,49,41,17,47,45,78,74,115,94,90,
226 79,69,83,71,50,59,38,36,15,72,34,56,95,92,85,91,90,86,73,77,65,51,44,43,42,43,
227 20,30,44,55,78,72,87,78,61,46,54,37,30,20,16,53,25,41,37,44,59,54,81,66,76,57,
228 54,37,18,39,11,35,33,31,57,42,82,72,80,47,58,55,21,22,26,38,22,53,25,23,38,70,
229 60,51,36,55,26,34,23,27,14,9,7,34,32,28,39,49,75,30,52,48,40,52,28,18,17,9,5,
230 45,21,34,64,56,50,49,45,31,19,12,15,10,7,6,3,48,23,20,39,36,35,53,21,16,23,13,
231 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 };
232
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000233static const uint8_t t15HB_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000234{7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51,
235 42,70,52,83,65,41,59,36,19,17,15,24,41,34,59,48,40,64,50,78,62,80,56,33,29,28,
236 25,43,39,63,55,93,76,59,93,72,54,75,50,29,52,22,42,40,67,57,95,79,72,57,89,69,
237 49,66,46,27,77,37,35,66,58,52,91,74,62,48,79,63,90,62,40,38,125,32,60,56,50,
238 92,78,65,55,87,71,51,73,51,70,30,109,53,49,94,88,75,66,122,91,73,56,42,64,44,
239 21,25,90,43,41,77,73,63,56,92,77,66,47,67,48,53,36,20,71,34,67,60,58,49,88,76,
240 67,106,71,54,38,39,23,15,109,53,51,47,90,82,58,57,48,72,57,41,23,27,62,9,86,
241 42,40,37,70,64,52,43,70,55,42,25,29,18,11,11,118,68,30,55,50,46,74,65,49,39,
242 24,16,22,13,14,7,91,44,39,38,34,63,52,45,31,52,28,19,14,8,9,3,123,60,58,53,47,
243 43,32,22,37,24,17,12,15,10,2,1,71,37,34,30,28,20,17,26,21,16,10,6,8,6,2,0};
244
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000245static const uint16_t t16HB_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000246{1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47,
247 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209,
248 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65,
249 115,101,179,164,155,264,246,226,395,382,362,9,66,30,59,56,102,185,173,265,142,
250 253,232,400,388,378,445,16,111,54,52,100,184,178,160,133,257,244,228,217,385,
251 366,715,10,98,48,91,88,165,157,148,261,248,407,397,372,380,889,884,8,85,84,81,
252 159,156,143,260,249,427,401,392,383,727,713,708,7,154,76,73,141,131,256,245,
253 426,406,394,384,735,359,710,352,11,139,129,67,125,247,233,229,219,393,743,737,
254 720,885,882,439,4,243,120,118,115,227,223,396,746,742,736,721,712,706,223,436,
255 6,202,224,222,218,216,389,386,381,364,888,443,707,440,437,1728,4,747,211,210,
256 208,370,379,734,723,714,1735,883,877,876,3459,865,2,377,369,102,187,726,722,
257 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3,
258 1,3};
259
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000260static const uint16_t t24HB_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000261{15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71,
262 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194,
263 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293,
264 271,16,147,72,69,135,127,118,112,210,200,188,352,323,306,285,540,14,263,66,
265 129,126,119,114,214,202,192,180,341,317,301,281,262,12,249,123,121,117,113,
266 215,206,195,185,347,330,308,291,272,520,10,435,115,111,109,211,203,196,187,
267 353,332,313,298,283,531,381,17,427,212,208,205,201,193,186,177,169,320,303,
268 286,268,514,377,16,335,199,197,191,189,181,174,333,321,305,289,275,521,379,
269 371,11,668,184,183,179,175,344,331,314,304,290,277,530,383,373,366,10,652,346,
270 171,168,164,318,309,299,287,276,263,513,375,368,362,6,648,322,316,312,307,302,
271 292,284,269,261,512,376,370,364,359,4,620,300,296,294,288,282,273,266,515,380,
272 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360,
273 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3};
274
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000275static const uint32_t tab1315_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000276{ 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a,
277 0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e,
278 0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a,
279 0x0b000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0e000c,0x0e000d,0x0e000d,
280 0x070006,0x080007,0x090007,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,
281 0x0b000a,0x0c000b,0x0c000b,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
282 0x080007,0x090008,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0c000b,
283 0x0c000b,0x0d000b,0x0d000c,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
284 0x090008,0x090008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
285 0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000d,
286 0x0a0009,0x0a0009,0x0b0009,0x0c000a,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
287 0x0d000b,0x0d000b,0x0e000c,0x0d000c,0x0f000d,0x0f000d,0x10000d,0x10000e,
288 0x0a000a,0x0b0009,0x0c000a,0x0c000a,0x0d000a,0x0d000b,0x0d000b,0x0d000b,
289 0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x10000e,
290 0x0b000a,0x0b000a,0x0c000a,0x0d000b,0x0d000b,0x0d000b,0x0e000b,0x0e000c,
291 0x0e000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x12000d,0x12000e,
292 0x0a000a,0x0a000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0e000c,
293 0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x11000e,0x11000e,
294 0x0b000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0d000c,0x0f000c,
295 0x0e000c,0x0f000d,0x0f000d,0x10000d,0x10000d,0x10000e,0x12000e,0x11000e,
296 0x0b000b,0x0c000b,0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000c,
297 0x0e000c,0x0f000d,0x10000d,0x0f000d,0x10000d,0x11000e,0x12000f,0x13000e,
298 0x0c000b,0x0c000b,0x0c000b,0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0e000c,
299 0x0f000d,0x0f000d,0x0f000d,0x10000d,0x11000e,0x11000e,0x11000e,0x12000f,
300 0x0c000c,0x0d000c,0x0d000b,0x0e000c,0x0e000c,0x0f000c,0x0e000d,0x0f000d,
301 0x10000d,0x10000d,0x11000d,0x11000d,0x11000e,0x12000e,0x12000f,0x12000f,
302 0x0d000c,0x0d000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x10000d,
303 0x10000d,0x10000e,0x10000e,0x11000e,0x12000e,0x11000e,0x12000f,0x12000f,
304 0x0e000d,0x0e000d,0x0e000d,0x0f000d,0x0f000d,0x0f000d,0x11000d,0x10000d,
305 0x10000e,0x13000e,0x11000e,0x11000e,0x11000f,0x13000f,0x12000e,0x12000f,
306 0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e,
307 0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f };
308
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000309static const uint32_t tab01_const[16] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000310{ 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007,
311 0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 };
312
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000313static const uint32_t tab23_const[ 9] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000314{ 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 };
315
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000316static const uint32_t tab56_const[16] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000317{ 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007,
318 0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 };
319
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000320static const uint32_t tab789_const[36] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000321{0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004,
322 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806,
323 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808,
324 0x00b02c09,0x00b02c09,0x00b0300a,0x00802408,0x00902408,0x00a02809,0x00b02c09,
325 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b,
326 0x00c0340b};
327
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000328static const uint32_t tabABC_const[64] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000329{0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a,
330 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809,
331 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809,
332 0x00c02c0a,0x00b02809,0x00b0280a,0x00802007,0x00902007,0x00a02408,0x00b02c08,
333 0x00c02809,0x00c0300a,0x00b0280a,0x00c02c0a,0x00902408,0x00a02808,0x00b02809,
334 0x00c02c09,0x00c02c0a,0x00c0300a,0x00c02c0a,0x00c0300b,0x00a02409,0x00b02809,
335 0x00c02c0a,0x00c0300a,0x00d0300a,0x00d0340b,0x00c0300a,0x00d0340b,0x00902409,
336 0x00a02409,0x00b02409,0x00c0280a,0x00c02c0a,0x00c0300b,0x00d0300b,0x00d0300c,
337 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b,
338 0x00d0300c};
339
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000340static const uint32_t tab1624_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000341{0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a,
342 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c,
343 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009,
344 0x000b000a,0x000b000a,0x000b000b,0x000c000b,0x000c000b,0x000c000c,0x000d000c,
345 0x000e000c,0x000d000c,0x000e000c,0x000a000a,0x00070007,0x00080007,0x00090008,
346 0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000b,0x000d000b,0x000c000b,
347 0x000d000b,0x000d000c,0x000d000c,0x000e000c,0x000e000d,0x000b0009,0x00090008,
348 0x00090008,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,0x000c000b,
349 0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,0x000f000c,
350 0x000c0009,0x000a0009,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,
351 0x000d000a,0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,
352 0x000f000c,0x000f000d,0x000b0009,0x000a000a,0x000a0009,0x000b000a,0x000b000a,
353 0x000c000a,0x000d000a,0x000d000b,0x000e000b,0x000d000b,0x000e000b,0x000e000c,
354 0x000f000c,0x000f000c,0x000f000c,0x0010000c,0x000c0009,0x000b000a,0x000b000a,
355 0x000b000a,0x000c000a,0x000d000a,0x000d000b,0x000d000b,0x000d000b,0x000e000b,
356 0x000e000c,0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000d,0x000c0009,
357 0x000b000b,0x000b000a,0x000c000a,0x000c000a,0x000d000b,0x000d000b,0x000d000b,
358 0x000e000b,0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000d,
359 0x0011000d,0x000c000a,0x000b000b,0x000c000b,0x000c000b,0x000d000b,0x000d000b,
360 0x000d000b,0x000e000b,0x000e000b,0x000f000b,0x000f000c,0x000f000c,0x000f000c,
361 0x0010000c,0x0010000d,0x0010000d,0x000c000a,0x000c000b,0x000c000b,0x000c000b,
362 0x000d000b,0x000d000b,0x000e000b,0x000e000b,0x000f000c,0x000f000c,0x000f000c,
363 0x000f000c,0x0010000c,0x000f000d,0x0010000d,0x000f000d,0x000d000a,0x000c000c,
364 0x000d000b,0x000c000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000e000c,
365 0x000f000c,0x0010000c,0x0010000c,0x0010000d,0x0011000d,0x0011000d,0x0010000d,
366 0x000c000a,0x000d000c,0x000d000c,0x000d000b,0x000d000b,0x000e000b,0x000e000c,
367 0x000f000c,0x0010000c,0x0010000c,0x0010000c,0x0010000c,0x0010000d,0x0010000d,
368 0x000f000d,0x0010000d,0x000d000a,0x000d000c,0x000e000c,0x000e000c,0x000e000c,
369 0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000c,0x0010000d,
370 0x0010000d,0x0010000d,0x0010000d,0x0012000d,0x000d000a,0x000f000c,0x000e000c,
371 0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000c,0x0010000c,0x0010000d,
372 0x0012000d,0x0011000d,0x0011000d,0x0011000d,0x0013000d,0x0011000d,0x000d000a,
373 0x000e000d,0x000f000c,0x000d000c,0x000e000c,0x0010000c,0x0010000c,0x000f000c,
374 0x0010000d,0x0010000d,0x0011000d,0x0012000d,0x0011000d,0x0013000d,0x0011000d,
375 0x0010000d,0x000d000a,0x000a0009,0x000a0009,0x000a0009,0x000b0009,0x000b0009,
376 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a,
377 0x000d000a,0x000d000a,0x000d000a,0x000a0006};
378
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000379static const uint8_t t1l_const[8] = {1,3,2,3,1,4,3,5};
380static const uint8_t t2l_const[9] = {1,3,6,3,3,5,5,5,6};
381static const uint8_t t3l_const[9] = {2,2,6,3,2,5,5,5,6};
382static const uint8_t t5l_const[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8};
383static const uint8_t t6l_const[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7};
Thom Johansen6e219f82006-09-09 01:35:12 +0000384
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000385static const uint8_t t7l_const[36] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000386{1,3,6,8,8,9,3,4,6,7,7,8,6,5,7,8,8,9,7,7,8,9,9,9,7,7,8,9,9,10,8,8,9,10,10,10};
387
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000388static const uint8_t t8l_const[36] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000389{2,3,6,8,8,9,3,2,4,8,8,8,6,4,6,8,8,9,8,8,8,9,9,10,8,7,8,9,10,10,9,8,9,9,11,11};
390
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000391static const uint8_t t9l_const[36] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000392{3,3,5,6,8,9,3,3,4,5,6,8,4,4,5,6,7,8,6,5,6,7,7,8,7,6,7,7,8,9,8,7,8,8,9,9};
393
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000394static const uint8_t t10l_const[64] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000395{1,3,6,8,9,9,9,10,3,4,6,7,8,9,8,8,6,6,7,8,9,10,9,9,7,7,8,9,10,10,9,10,8,8,9,10,
396 10,10,10,10,9,9,10,10,11,11,10,11,8,8,9,10,10,10,11,11,9,8,9,10,10,11,11,11};
397
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000398static const uint8_t t11l_const[64] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000399{2,3,5,7,8,9,8,9,3,3,4,6,8,8,7,8,5,5,6,7,8,9,8,8,7,6,7,9,8,10,8,9,8,8,8,9,9,10,
400 9,10,8,8,9,10,10,11,10,11,8,7,7,8,9,10,10,10,8,7,8,9,10,10,10,10};
401
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000402static const uint8_t t12l_const[64] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000403{4,3,5,7,8,9,9,9,3,3,4,5,7,7,8,8,5,4,5,6,7,8,7,8,6,5,6,6,7,8,8,8,7,6,7,7,8,
404 8,8,9,8,7,8,8,8,9,8,9,8,7,7,8,8,9,9,10,9,8,8,9,9,9,9,10};
405
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000406static const uint8_t t13l_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000407{1,4,6,7,8,9,9,10,9,10,11,11,12,12,13,13,3,4,6,7,8,8,9,9,9,9,10,10,11,12,12,12,
408 6,6,7,8,9,9,10,10,9,10,10,11,11,12,13,13,7,7,8,9,9,10,10,10,10,11,11,11,11,12,
409 13,13,8,7,9,9,10,10,11,11,10,11,11,12,12,13,13,14,9,8,9,10,10,10,11,11,11,11,
410 12,11,13,13,14,14,9,9,10,10,11,11,11,11,11,12,12,12,13,13,14,14,10,9,10,11,11,
411 11,12,12,12,12,13,13,13,14,16,16,9,8,9,10,10,11,11,12,12,12,12,13,13,14,15,15,
412 10,9,10,10,11,11,11,13,12,13,13,14,14,14,16,15,10,10,10,11,11,12,12,13,12,13,
413 14,13,14,15,16,17,11,10,10,11,12,12,12,12,13,13,13,14,15,15,15,16,11,11,11,12,
414 12,13,12,13,14,14,15,15,15,16,16,16,12,11,12,13,13,13,14,14,14,14,14,15,16,15,
415 16,16,13,12,12,13,13,13,15,14,14,17,15,15,15,17,16,16,12,12,13,14,14,14,15,14,
416 15,15,16,16,19,18,19,16};
417
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000418static const uint8_t t15l_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000419{3,4,5,7,7,8,9,9,9,10,10,11,11,11,12,13,4,3,5,6,7,7,8,8,8,9,9,10,10,10,11,11,5,
420 5,5,6,7,7,8,8,8,9,9,10,10,11,11,11,6,6,6,7,7,8,8,9,9,9,10,10,10,11,11,11,7,6,
421 7,7,8,8,9,9,9,9,10,10,10,11,11,11,8,7,7,8,8,8,9,9,9,9,10,10,11,11,11,12,9,7,8,
422 8,8,9,9,9,9,10,10,10,11,11,12,12,9,8,8,9,9,9,9,10,10,10,10,10,11,11,11,12,9,8,
423 8,9,9,9,9,10,10,10,10,11,11,12,12,12,9,8,9,9,9,9,10,10,10,11,11,11,11,12,12,
424 12,10,9,9,9,10,10,10,10,10,11,11,11,11,12,13,12,10,9,9,9,10,10,10,10,11,11,11,
425 11,12,12,12,13,11,10,9,10,10,10,11,11,11,11,11,11,12,12,13,13,11,10,10,10,10,
426 11,11,11,11,12,12,12,12,12,13,13,12,11,11,11,11,11,11,11,12,12,12,12,13,13,12,
427 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13};
428
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000429static const uint8_t t16l_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000430{1,4,6,8,9,9,10,10,11,11,11,12,12,12,13,9,3,4,6,7,8,9,9,9,10,10,10,11,12,11,12,
431 8,6,6,7,8,9,9,10,10,11,10,11,11,11,12,12,9,8,7,8,9,9,10,10,10,11,11,12,12,12,
432 13,13,10,9,8,9,9,10,10,11,11,11,12,12,12,13,13,13,9,9,8,9,9,10,11,11,12,11,12,
433 12,13,13,13,14,10,10,9,9,10,11,11,11,11,12,12,12,12,13,13,14,10,10,9,10,10,11,
434 11,11,12,12,13,13,13,13,15,15,10,10,10,10,11,11,11,12,12,13,13,13,13,14,14,14,
435 10,11,10,10,11,11,12,12,13,13,13,13,14,13,14,13,11,11,11,10,11,12,12,12,12,13,
436 14,14,14,15,15,14,10,12,11,11,11,12,12,13,14,14,14,14,14,14,13,14,11,12,12,12,
437 12,12,13,13,13,13,15,14,14,14,14,16,11,14,12,12,12,13,13,14,14,14,16,15,15,15,
438 17,15,11,13,13,11,12,14,14,13,14,14,15,16,15,17,15,14,11,9,8,8,9,9,10,10,10,
439 11,11,11,11,11,11,11,8};
440
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000441static const uint8_t t24l_const[256] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000442{4,4,6,7,8,9,9,10,10,11,11,11,11,11,12,9,4,4,5,6,7,8,8,9,9,9,10,10,10,10,10,8,
443 6,5,6,7,7,8,8,9,9,9,9,10,10,10,11,7,7,6,7,7,8,8,8,9,9,9,9,10,10,10,10,7,8,7,7,
444 8,8,8,8,9,9,9,10,10,10,10,11,7,9,7,8,8,8,8,9,9,9,9,10,10,10,10,10,7,9,8,8,8,8,
445 9,9,9,9,10,10,10,10,10,11,7,10,8,8,8,9,9,9,9,10,10,10,10,10,11,11,8,10,9,9,9,
446 9,9,9,9,9,10,10,10,10,11,11,8,10,9,9,9,9,9,9,10,10,10,10,10,11,11,11,8,11,9,9,
447 9,9,10,10,10,10,10,10,11,11,11,11,8,11,10,9,9,9,10,10,10,10,10,10,11,11,11,11,
448 8,11,10,10,10,10,10,10,10,10,10,11,11,11,11,11,8,11,10,10,10,10,10,10,10,11,
449 11,11,11,11,11,11,8,12,10,10,10,10,10,10,11,11,11,11,11,11,11,11,8,8,7,7,7,7,
450 7,7,7,7,7,7,8,8,8,8,4};
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000451
452static const struct huffcodetab ht_const[HTN] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000453{ { 0, NULL, NULL}, /* Apparently not used */
454 { 2, t1HB, t1l},
455 { 3, t2HB, t2l},
456 { 3, t3HB, t3l},
457 { 0, NULL, NULL}, /* Apparently not used */
458 { 4, t5HB, t5l},
459 { 4, t6HB, t6l},
460 { 6, t7HB, t7l},
461 { 6, t8HB, t8l},
462 { 6, t9HB, t9l},
463 { 8, t10HB, t10l},
464 { 8, t11HB, t11l},
465 { 8, t12HB, t12l},
466 {16, t13HB, t13l},
467 { 0, NULL, NULL}, /* Apparently not used */
468 {16, t15HB, t15l} };
469
470static const struct huffcodebig ht_big[HTN] =
471{ { 16, 1, 1 },
472 { 16, 2, 3 },
473 { 16, 3, 7 },
474 { 16, 4, 15 },
475 { 16, 6, 63 },
476 { 16, 8, 255 },
477 { 16, 10, 1023 },
478 { 16, 13, 8191 },
479 { 16, 4, 15 },
480 { 16, 5, 31 },
481 { 16, 6, 63 },
482 { 16, 7, 127 },
483 { 16, 8, 255 },
484 { 16, 9, 511 },
485 { 16, 11, 2047 },
486 { 16, 13, 8191 } };
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000487
488static const struct
489{
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000490 uint32_t region0_cnt;
491 uint32_t region1_cnt;
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000492} subdv_table[23] =
Thom Johansen6e219f82006-09-09 01:35:12 +0000493{ {0, 0}, /* 0 bands */
494 {0, 0}, /* 1 bands */
495 {0, 0}, /* 2 bands */
496 {0, 0}, /* 3 bands */
497 {0, 0}, /* 4 bands */
498 {0, 1}, /* 5 bands */
499 {1, 1}, /* 6 bands */
500 {1, 1}, /* 7 bands */
501 {1, 2}, /* 8 bands */
502 {2, 2}, /* 9 bands */
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000503 {2, 3}, /* 10 bands */
504 {2, 3}, /* 11 bands */
505 {3, 4}, /* 12 bands */
506 {3, 4}, /* 13 bands */
507 {3, 4}, /* 14 bands */
508 {4, 5}, /* 15 bands */
509 {4, 5}, /* 16 bands */
510 {4, 6}, /* 17 bands */
511 {5, 6}, /* 18 bands */
512 {5, 6}, /* 19 bands */
513 {5, 7}, /* 20 bands */
514 {6, 7}, /* 21 bands */
515 {6, 7}, /* 22 bands */
516};
517
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000518static const uint32_t sfBand[6][23] =
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000519{
520/* Table B.2.b: 22.05 kHz */
521{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
522/* Table B.2.c: 24 kHz */
523{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
524/* Table B.2.a: 16 kHz */
525{0,6,12,18,24,30,36,44,45,66,80,96,116,140,168,200,238,248,336,396,464,522,576},
526/* Table B.8.b: 44.1 kHz */
527{0,4, 8,12,16,20,24,30,36,44,52,62, 74, 90,110,134,162,196,238,288,342,418,576},
528/* Table B.8.c: 48 kHz */
529{0,4, 8,12,16,20,24,30,36,42,50,60, 72, 88,106,128,156,190,230,276,330,384,576},
530/* Table B.8.a: 32 kHz */
531{0,4, 8,12,16,20,24,30,36,44,54,66, 82,102,126,156,194,240,296,364,448,550,576} };
532
Thom Johansen6e219f82006-09-09 01:35:12 +0000533
534static const short int2idx_const[4096] = /* int2idx[i] = sqrt(i*sqrt(i)); */
535{
536 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9,
537 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16,
538 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21,
539 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26,
540 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
541 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36,
542 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 40,
543 41, 41, 41, 41, 42, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45,
544 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49,
545 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53,
546 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57,
547 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61,
548 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65,
549 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68,
550 68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 72, 72, 72,
551 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75,
552 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 79, 79, 79,
553 79, 79, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82,
554 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 86, 86, 86,
555 86, 86, 86, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89,
556 89, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 93,
557 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, 95, 96, 96, 96,
558 96, 96, 96, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99,
559 99, 99,100,100,100,100,100,100,101,101,101,101,101,101,102,102,102,102,102,102,
560103,103,103,103,103,103,104,104,104,104,104,104,104,105,105,105,105,105,105,106,
561106,106,106,106,106,107,107,107,107,107,107,107,108,108,108,108,108,108,109,109,
562109,109,109,109,110,110,110,110,110,110,110,111,111,111,111,111,111,112,112,112,
563112,112,112,112,113,113,113,113,113,113,114,114,114,114,114,114,115,115,115,115,
564115,115,115,116,116,116,116,116,116,117,117,117,117,117,117,117,118,118,118,118,
565118,118,118,119,119,119,119,119,119,120,120,120,120,120,120,120,121,121,121,121,
566121,121,122,122,122,122,122,122,122,123,123,123,123,123,123,123,124,124,124,124,
567124,124,125,125,125,125,125,125,125,126,126,126,126,126,126,126,127,127,127,127,
568127,127,128,128,128,128,128,128,128,129,129,129,129,129,129,129,130,130,130,130,
569130,130,131,131,131,131,131,131,131,132,132,132,132,132,132,132,133,133,133,133,
570133,133,133,134,134,134,134,134,134,134,135,135,135,135,135,135,136,136,136,136,
571136,136,136,137,137,137,137,137,137,137,138,138,138,138,138,138,138,139,139,139,
572139,139,139,139,140,140,140,140,140,140,140,141,141,141,141,141,141,141,142,142,
573142,142,142,142,142,143,143,143,143,143,143,143,144,144,144,144,144,144,144,145,
574145,145,145,145,145,145,146,146,146,146,146,146,146,147,147,147,147,147,147,147,
575148,148,148,148,148,148,148,149,149,149,149,149,149,149,150,150,150,150,150,150,
576150,151,151,151,151,151,151,151,152,152,152,152,152,152,152,153,153,153,153,153,
577153,153,154,154,154,154,154,154,154,154,155,155,155,155,155,155,155,156,156,156,
578156,156,156,156,157,157,157,157,157,157,157,158,158,158,158,158,158,158,159,159,
579159,159,159,159,159,160,160,160,160,160,160,160,160,161,161,161,161,161,161,161,
580162,162,162,162,162,162,162,163,163,163,163,163,163,163,163,164,164,164,164,164,
581164,164,165,165,165,165,165,165,165,166,166,166,166,166,166,166,167,167,167,167,
582167,167,167,167,168,168,168,168,168,168,168,169,169,169,169,169,169,169,169,170,
583170,170,170,170,170,170,171,171,171,171,171,171,171,172,172,172,172,172,172,172,
584172,173,173,173,173,173,173,173,174,174,174,174,174,174,174,174,175,175,175,175,
585175,175,175,176,176,176,176,176,176,176,176,177,177,177,177,177,177,177,178,178,
586178,178,178,178,178,178,179,179,179,179,179,179,179,180,180,180,180,180,180,180,
587180,181,181,181,181,181,181,181,182,182,182,182,182,182,182,182,183,183,183,183,
588183,183,183,184,184,184,184,184,184,184,184,185,185,185,185,185,185,185,186,186,
589186,186,186,186,186,186,187,187,187,187,187,187,187,187,188,188,188,188,188,188,
590188,189,189,189,189,189,189,189,189,190,190,190,190,190,190,190,190,191,191,191,
591191,191,191,191,192,192,192,192,192,192,192,192,193,193,193,193,193,193,193,193,
592194,194,194,194,194,194,194,195,195,195,195,195,195,195,195,196,196,196,196,196,
593196,196,196,197,197,197,197,197,197,197,197,198,198,198,198,198,198,198,199,199,
594199,199,199,199,199,199,200,200,200,200,200,200,200,200,201,201,201,201,201,201,
595201,201,202,202,202,202,202,202,202,202,203,203,203,203,203,203,203,204,204,204,
596204,204,204,204,204,205,205,205,205,205,205,205,205,206,206,206,206,206,206,206,
597206,207,207,207,207,207,207,207,207,208,208,208,208,208,208,208,208,209,209,209,
598209,209,209,209,209,210,210,210,210,210,210,210,210,211,211,211,211,211,211,211,
599211,212,212,212,212,212,212,212,212,213,213,213,213,213,213,213,213,214,214,214,
600214,214,214,214,214,215,215,215,215,215,215,215,215,216,216,216,216,216,216,216,
601216,217,217,217,217,217,217,217,217,218,218,218,218,218,218,218,218,219,219,219,
602219,219,219,219,219,220,220,220,220,220,220,220,220,221,221,221,221,221,221,221,
603221,222,222,222,222,222,222,222,222,223,223,223,223,223,223,223,223,224,224,224,
604224,224,224,224,224,225,225,225,225,225,225,225,225,226,226,226,226,226,226,226,
605226,227,227,227,227,227,227,227,227,228,228,228,228,228,228,228,228,229,229,229,
606229,229,229,229,229,229,230,230,230,230,230,230,230,230,231,231,231,231,231,231,
607231,231,232,232,232,232,232,232,232,232,233,233,233,233,233,233,233,233,234,234,
608234,234,234,234,234,234,234,235,235,235,235,235,235,235,235,236,236,236,236,236,
609236,236,236,237,237,237,237,237,237,237,237,238,238,238,238,238,238,238,238,238,
610239,239,239,239,239,239,239,239,240,240,240,240,240,240,240,240,241,241,241,241,
611241,241,241,241,242,242,242,242,242,242,242,242,242,243,243,243,243,243,243,243,
612243,244,244,244,244,244,244,244,244,245,245,245,245,245,245,245,245,245,246,246,
613246,246,246,246,246,246,247,247,247,247,247,247,247,247,248,248,248,248,248,248,
614248,248,248,249,249,249,249,249,249,249,249,250,250,250,250,250,250,250,250,250,
615251,251,251,251,251,251,251,251,252,252,252,252,252,252,252,252,253,253,253,253,
616253,253,253,253,253,254,254,254,254,254,254,254,254,255,255,255,255,255,255,255,
617255,255,256,256,256,256,256,256,256,256,257,257,257,257,257,257,257,257,257,258,
618258,258,258,258,258,258,258,259,259,259,259,259,259,259,259,259,260,260,260,260,
619260,260,260,260,261,261,261,261,261,261,261,261,261,262,262,262,262,262,262,262,
620262,263,263,263,263,263,263,263,263,263,264,264,264,264,264,264,264,264,265,265,
621265,265,265,265,265,265,265,266,266,266,266,266,266,266,266,267,267,267,267,267,
622267,267,267,267,268,268,268,268,268,268,268,268,268,269,269,269,269,269,269,269,
623269,270,270,270,270,270,270,270,270,270,271,271,271,271,271,271,271,271,271,272,
624272,272,272,272,272,272,272,273,273,273,273,273,273,273,273,273,274,274,274,274,
625274,274,274,274,275,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276,
626276,276,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,279,
627279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,281,281,281,
628281,281,281,281,281,282,282,282,282,282,282,282,282,282,283,283,283,283,283,283,
629283,283,283,284,284,284,284,284,284,284,284,284,285,285,285,285,285,285,285,285,
630286,286,286,286,286,286,286,286,286,287,287,287,287,287,287,287,287,287,288,288,
631288,288,288,288,288,288,288,289,289,289,289,289,289,289,289,289,290,290,290,290,
632290,290,290,290,291,291,291,291,291,291,291,291,291,292,292,292,292,292,292,292,
633292,292,293,293,293,293,293,293,293,293,293,294,294,294,294,294,294,294,294,294,
634295,295,295,295,295,295,295,295,295,296,296,296,296,296,296,296,296,296,297,297,
635297,297,297,297,297,297,297,298,298,298,298,298,298,298,298,299,299,299,299,299,
636299,299,299,299,300,300,300,300,300,300,300,300,300,301,301,301,301,301,301,301,
637301,301,302,302,302,302,302,302,302,302,302,303,303,303,303,303,303,303,303,303,
638304,304,304,304,304,304,304,304,304,305,305,305,305,305,305,305,305,305,306,306,
639306,306,306,306,306,306,306,307,307,307,307,307,307,307,307,307,308,308,308,308,
640308,308,308,308,308,309,309,309,309,309,309,309,309,309,310,310,310,310,310,310,
641310,310,310,311,311,311,311,311,311,311,311,311,312,312,312,312,312,312,312,312,
642312,313,313,313,313,313,313,313,313,313,314,314,314,314,314,314,314,314,314,315,
643315,315,315,315,315,315,315,315,316,316,316,316,316,316,316,316,316,317,317,317,
644317,317,317,317,317,317,318,318,318,318,318,318,318,318,318,318,319,319,319,319,
645319,319,319,319,319,320,320,320,320,320,320,320,320,320,321,321,321,321,321,321,
646321,321,321,322,322,322,322,322,322,322,322,322,323,323,323,323,323,323,323,323,
647323,324,324,324,324,324,324,324,324,324,325,325,325,325,325,325,325,325,325,325,
648326,326,326,326,326,326,326,326,326,327,327,327,327,327,327,327,327,327,328,328,
649328,328,328,328,328,328,328,329,329,329,329,329,329,329,329,329,330,330,330,330,
650330,330,330,330,330,330,331,331,331,331,331,331,331,331,331,332,332,332,332,332,
651332,332,332,332,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,
652334,334,335,335,335,335,335,335,335,335,335,335,336,336,336,336,336,336,336,336,
653336,337,337,337,337,337,337,337,337,337,338,338,338,338,338,338,338,338,338,338,
654339,339,339,339,339,339,339,339,339,340,340,340,340,340,340,340,340,340,341,341,
655341,341,341,341,341,341,341,341,342,342,342,342,342,342,342,342,342,343,343,343,
656343,343,343,343,343,343,344,344,344,344,344,344,344,344,344,344,345,345,345,345,
657345,345,345,345,345,346,346,346,346,346,346,346,346,346,347,347,347,347,347,347,
658347,347,347,347,348,348,348,348,348,348,348,348,348,349,349,349,349,349,349,349,
659349,349,350,350,350,350,350,350,350,350,350,350,351,351,351,351,351,351,351,351,
660351,352,352,352,352,352,352,352,352,352,352,353,353,353,353,353,353,353,353,353,
661354,354,354,354,354,354,354,354,354,355,355,355,355,355,355,355,355,355,355,356,
662356,356,356,356,356,356,356,356,357,357,357,357,357,357,357,357,357,357,358,358,
663358,358,358,358,358,358,358,359,359,359,359,359,359,359,359,359,359,360,360,360,
664360,360,360,360,360,360,361,361,361,361,361,361,361,361,361,361,362,362,362,362,
665362,362,362,362,362,363,363,363,363,363,363,363,363,363,363,364,364,364,364,364,
666364,364,364,364,365,365,365,365,365,365,365,365,365,365,366,366,366,366,366,366,
667366,366,366,367,367,367,367,367,367,367,367,367,367,368,368,368,368,368,368,368,
668368,368,369,369,369,369,369,369,369,369,369,369,370,370,370,370,370,370,370,370,
669370,370,371,371,371,371,371,371,371,371,371,372,372,372,372,372,372,372,372,372,
670372,373,373,373,373,373,373,373,373,373,374,374,374,374,374,374,374,374,374,374,
671375,375,375,375,375,375,375,375,375,375,376,376,376,376,376,376,376,376,376,377,
672377,377,377,377,377,377,377,377,377,378,378,378,378,378,378,378,378,378,379,379,
673379,379,379,379,379,379,379,379,380,380,380,380,380,380,380,380,380,380,381,381,
674381,381,381,381,381,381,381,382,382,382,382,382,382,382,382,382,382,383,383,383,
675383,383,383,383,383,383,383,384,384,384,384,384,384,384,384,384,385,385,385,385,
676385,385,385,385,385,385,386,386,386,386,386,386,386,386,386,386,387,387,387,387,
677387,387,387,387,387,387,388,388,388,388,388,388,388,388,388,389,389,389,389,389,
678389,389,389,389,389,390,390,390,390,390,390,390,390,390,390,391,391,391,391,391,
679391,391,391,391,391,392,392,392,392,392,392,392,392,392,393,393,393,393,393,393,
680393,393,393,393,394,394,394,394,394,394,394,394,394,394,395,395,395,395,395,395,
681395,395,395,395,396,396,396,396,396,396,396,396,396,397,397,397,397,397,397,397,
682397,397,397,398,398,398,398,398,398,398,398,398,398,399,399,399,399,399,399,399,
683399,399,399,400,400,400,400,400,400,400,400,400,400,401,401,401,401,401,401,401,
684401,401,402,402,402,402,402,402,402,402,402,402,403,403,403,403,403,403,403,403,
685403,403,404,404,404,404,404,404,404,404,404,404,405,405,405,405,405,405,405,405,
686405,405,406,406,406,406,406,406,406,406,406,406,407,407,407,407,407,407,407,407,
687407,407,408,408,408,408,408,408,408,408,408,408,409,409,409,409,409,409,409,409,
688409,410,410,410,410,410,410,410,410,410,410,411,411,411,411,411,411,411,411,411,
689411,412,412,412,412,412,412,412,412,412,412,413,413,413,413,413,413,413,413,413,
690413,414,414,414,414,414,414,414,414,414,414,415,415,415,415,415,415,415,415,415,
691415,416,416,416,416,416,416,416,416,416,416,417,417,417,417,417,417,417,417,417,
692417,418,418,418,418,418,418,418,418,418,418,419,419,419,419,419,419,419,419,419,
693419,420,420,420,420,420,420,420,420,420,420,421,421,421,421,421,421,421,421,421,
694421,422,422,422,422,422,422,422,422,422,422,423,423,423,423,423,423,423,423,423,
695423,424,424,424,424,424,424,424,424,424,424,425,425,425,425,425,425,425,425,425,
696425,426,426,426,426,426,426,426,426,426,426,427,427,427,427,427,427,427,427,427,
697427,428,428,428,428,428,428,428,428,428,428,429,429,429,429,429,429,429,429,429,
698429,430,430,430,430,430,430,430,430,430,430,431,431,431,431,431,431,431,431,431,
699431,432,432,432,432,432,432,432,432,432,432,433,433,433,433,433,433,433,433,433,
700433,434,434,434,434,434,434,434,434,434,434,435,435,435,435,435,435,435,435,435,
701435,435,436,436,436,436,436,436,436,436,436,436,437,437,437,437,437,437,437,437,
702437,437,438,438,438,438,438,438,438,438,438,438,439,439,439,439,439,439,439,439,
703439,439,440,440,440,440,440,440,440,440,440,440,441,441,441,441,441,441,441,441,
704441,441,442,442,442,442,442,442,442,442,442,442,443,443,443,443,443,443,443,443,
705443,443,443,444,444,444,444,444,444,444,444,444,444,445,445,445,445,445,445,445,
706445,445,445,446,446,446,446,446,446,446,446,446,446,447,447,447,447,447,447,447,
707447,447,447,448,448,448,448,448,448,448,448,448,448,448,449,449,449,449,449,449,
708449,449,449,449,450,450,450,450,450,450,450,450,450,450,451,451,451,451,451,451,
709451,451,451,451,452,452,452,452,452,452,452,452,452,452,453,453,453,453,453,453,
710453,453,453,453,453,454,454,454,454,454,454,454,454,454,454,455,455,455,455,455,
711455,455,455,455,455,456,456,456,456,456,456,456,456,456,456,457,457,457,457,457,
712457,457,457,457,457,457,458,458,458,458,458,458,458,458,458,458,459,459,459,459,
713459,459,459,459,459,459,460,460,460,460,460,460,460,460,460,460,460,461,461,461,
714461,461,461,461,461,461,461,462,462,462,462,462,462,462,462,462,462,463,463,463,
715463,463,463,463,463,463,463,463,464,464,464,464,464,464,464,464,464,464,465,465,
716465,465,465,465,465,465,465,465,466,466,466,466,466,466,466,466,466,466,466,467,
717467,467,467,467,467,467,467,467,467,468,468,468,468,468,468,468,468,468,468,469,
718469,469,469,469,469,469,469,469,469,469,470,470,470,470,470,470,470,470,470,470,
719471,471,471,471,471,471,471,471,471,471,472,472,472,472,472,472,472,472,472,472,
720472,473,473,473,473,473,473,473,473,473,473,474,474,474,474,474,474,474,474,474,
721474,475,475,475,475,475,475,475,475,475,475,475,476,476,476,476,476,476,476,476,
722476,476,477,477,477,477,477,477,477,477,477,477,477,478,478,478,478,478,478,478,
723478,478,478,479,479,479,479,479,479,479,479,479,479,479,480,480,480,480,480,480,
724480,480,480,480,481,481,481,481,481,481,481,481,481,481,482,482,482,482,482,482,
725482,482,482,482,482,483,483,483,483,483,483,483,483,483,483,484,484,484,484,484,
726484,484,484,484,484,484,485,485,485,485,485,485,485,485,485,485,486,486,486,486,
727486,486,486,486,486,486,486,487,487,487,487,487,487,487,487,487,487,488,488,488,
728488,488,488,488,488,488,488,488,489,489,489,489,489,489,489,489,489,489,490,490,
729490,490,490,490,490,490,490,490,490,491,491,491,491,491,491,491,491,491,491,492,
730492,492,492,492,492,492,492,492,492,492,493,493,493,493,493,493,493,493,493,493,
731494,494,494,494,494,494,494,494,494,494,494,495,495,495,495,495,495,495,495,495,
732495,496,496,496,496,496,496,496,496,496,496,496,497,497,497,497,497,497,497,497,
733497,497,497,498,498,498,498,498,498,498,498,498,498,499,499,499,499,499,499,499,
734499,499,499,499,500,500,500,500,500,500,500,500,500,500,501,501,501,501,501,501,
735501,501,501,501,501,502,502,502,502,502,502,502,502,502,502,503,503,503,503,503,
736503,503,503,503,503,503,504,504,504,504,504,504,504,504,504,504,504,505,505,505,
737505,505,505,505,505,505,505,506,506,506,506,506,506,506,506,506,506,506,507,507,
738507,507,507,507,507,507,507,507,507,508,508,508,508,508,508,508,508,508,508,509,
739509,509,509,509,509,509,509,509,509,509,510,510,510,510,510,510,510,510,510,510,
740510,511,511,511,511,511,511,511,511,511,511,512,512,512,512,512 };
741
742static const int order[32] =
743{ 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
744 2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 };
745
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000746static const long sampr_index[2][3] =
747{ { 22050, 24000, 16000 }, /* MPEG 2 */
748 { 44100, 48000, 32000 } }; /* MPEG 1 */
749
750static const long bitr_index[2][15] =
751{ {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160}, /* MPEG 2 */
752 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} }; /* MPEG 1 */
Thom Johansen6e219f82006-09-09 01:35:12 +0000753
754static const int num_bands[3][15] =
755{ {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32},
756 {0,10,10,10,10,10,12,14, 18, 24, 26, 28, 30, 32, 32},
757 {0,10,12,14,18,24,26,28, 30, 32, 32, 32, 32, 32, 32} };
758
759static const int cx_const[9] =
760{ 16135, 10531, 5604, 15396, -2845,-12551, 14189, 8192, 16384 };
761
762static const int ca_const[8] =
763{-16859,-15458,-10269, -5961, -3099, -1342, -465, -121 };
764
765static const int cs_const[8] =
766{ 28098, 28893, 31117, 32221, 32621, 32740, 32765, 32768 };
767
768static const short enwindow_const[15*27+24] =
769{ 0, 65, 593, 1766, 22228, 2115, 611, 62,
770 8, 119, 1419, 10564,-11659,-1635,-154, -9,
771 -8, -119,-1419,-10564, 11659, 1635, 154, 9, 464, 100, 91,
772 0, 69, 604, 1635, 23148, 2363, 643, 62,
773 7, 107, 1368, 10449,-12733,-1818,-180,-11,
774 -7, -107,-1368,-10449, 12733, 1818, 180, 11, 420, 200, 164,
775 0, 72, 608, 1465, 23979, 2600, 671, 63,
776 7, 94, 1305, 10265,-13818,-2004,-207,-12,
777 -7, -94,-1305,-10265, 13818, 2004, 207, 12, 380, 297, 220,
778 0, 76, 606, 1256, 24718, 2825, 693, 63,
779 6, 81, 1232, 10016,-14908,-2192,-236,-14,
780 -6, -81,-1232,-10016, 14908, 2192, 236, 14, 342, 392, 262,
781 0, 78, 597, 1007, 25359, 3033, 712, 63,
782 6, 68, 1150, 9706,-15995,-2380,-267,-15,
783 -6, -68,-1150, -9706, 15995, 2380, 267, 15, 307, 483, 289,
784 0, 80, 580, 719, 25901, 3224, 726, 62,
785 6, 54, 1060, 9343,-17072,-2565,-299,-17,
786 -6, -54,-1060, -9343, 17072, 2565, 299, 17, 274, 569, 304,
787 -1, 82, 555, 391, 26339, 3395, 735, 61,
788 5, 40, 963, 8930,-18131,-2747,-332,-19,
789 -5, -40, -963, -8930, 18131, 2747, 332, 19, 242, 650, 307,
790 -1, 83, 523, 26, 26672, 3545, 740, 60,
791 5, 27, 861, 8474,-19164,-2923,-366,-21,
792 -5, -27, -861, -8474, 19164, 2923, 366, 21, 212, 724, 300,
793 -1, 83, 482, -376, 26900, 3672, 739, 58,
794 4, 14, 756, 7981,-20163,-3092,-401,-24,
795 -4, -14, -756, -7981, 20163, 3092, 401, 24, 183, 792, 283,
796 -1, 82, 433, -812, 27022, 3776, 735, 56,
797 4, 1, 648, 7456,-21122,-3250,-435,-26,
798 -4, -1, -648, -7456, 21122, 3250, 435, 26, 155, 851, 258,
799 -1, 81, 376, -1281, 27038, 3855, 726, 54,
800 3, -11, 539, 6907,-22032,-3397,-470,-28,
801 -3, 11, -539, -6907, 22032, 3397, 470, 28, 128, 903, 226,
802 -1, 78, 312, -1778, 26951, 3910, 713, 52,
803 3, -22, 430, 6338,-22887,-3530,-503,-31,
804 -3, 22, -430, -6338, 22887, 3530, 503, 31, 102, 946, 188,
805 -2, 75, 239, -2302, 26761, 3941, 696, 49,
806 3, -33, 322, 5757,-23678,-3648,-537,-34,
807 -3, 33, -322, -5757, 23678, 3648, 537, 34, 76, 980, 145,
808 -2, 70, 160, -2848, 26472, 3948, 676, 47,
809 3, -42, 217, 5167,-24399,-3749,-568,-36,
810 -3, 42, -217, -5167, 24399, 3749, 568, 36, 50, 1004, 99,
811 -2, 65, 74, -3412, 26087, 3931, 653, 44,
812 2, -51, 115, 4577,-25045,-3830,-599,-39,
813 -2, 51, -115, -4577, 25045, 3830, 599, 39, 25, 1019, 50,
814
815 25610,3891,627,42,-3990,-18,58,-2,
816 21226,-21226,10604,-10604,1860,-1860,1458,-1458,576,-576,130,-130,60,-60,8,-8
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000817};
818
Thom Johansen6e219f82006-09-09 01:35:12 +0000819static const int win_const[18][4] = {
820 { -3072, -134, -146, 3352 },
821 { -2747, -362, -471, 3579 },
822 { -2387, -529, -831, 3747 },
823 { -2004, -632,-1214, 3850 },
824 { -1609, -666,-1609, 3884 },
825 { -1214, -632,-2004, 3850 },
826 { -831, -529,-2387, 3747 },
827 { -471, -362,-2747, 3579 },
828 { -146, -134,-3072, 3352 },
829 { 134,-3072,-3352, -146 },
830 { 362,-2747,-3579, -471 },
831 { 529,-2387,-3747, -831 },
832 { 632,-2004,-3850,-1214 },
833 { 666,-1609,-3884,-1609 },
834 { 632,-1214,-3850,-2004 },
835 { 529, -831,-3747,-2387 },
836 { 362, -471,-3579,-2747 },
837 { 134, -146,-3352,-3072 } };
Michael Sevakis4fc717a2006-08-28 22:38:41 +0000838
839/* forward declarations */
Antonius Hellmann75807212008-06-02 19:28:08 +0000840static int HuffmanCode( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
841static int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000842static void putbits(uint32_t val, uint32_t nbit);
843static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
844 uint32_t len, int *bits);
845static int find_best_3( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
846 uint32_t len, int *bits);
847static int count_bit1 ( short *ix, uint32_t start, uint32_t end, int *bits );
848static int count_bigv ( short *ix, uint32_t start, uint32_t end, int table0, int table1,
Thom Johansen6e219f82006-09-09 01:35:12 +0000849 int *bits);
850
851
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000852static void encodeSideInfo( side_info_t si[2][2] )
Thom Johansen6e219f82006-09-09 01:35:12 +0000853{
854 int gr, ch, header;
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000855 uint32_t cc=0, sz=0;
Thom Johansen6e219f82006-09-09 01:35:12 +0000856
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000857 /*
858 * MPEG header layout:
859 * AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
860 * A (31-21) = frame sync
861 * B (20-19) = MPEG type
862 * C (18-17) = MPEG layer
863 * D (16) = protection bit
864 * E (15-12) = bitrate index
865 * F (11-10) = samplerate index
866 * G (9) = padding bit
867 * H (8) = private bit
868 * I (7-6) = channel mode
869 * J (5-4) = mode extension (jstereo only)
870 * K (3) = copyright bit
871 * L (2) = original
872 * M (1-0) = emphasis
873 */
874
875 header = (0xfff00000) | /* frame sync (AAAAAAAAA AAA)
876 mp3 type (upper): 1 (B) */
877 (0x01 << 17) | /* mp3 layer: 01 (CC) */
878 ( 0x1 << 16) | /* mp3 crc: 1 (D) */
879 ( 0x1 << 2); /* mp3 org: 1 (L) */
880 header |= cfg.mpg.type << 19;
Thom Johansen6e219f82006-09-09 01:35:12 +0000881 header |= cfg.mpg.bitr_id << 12;
882 header |= cfg.mpg.smpl_id << 10;
883 header |= cfg.mpg.padding << 9;
884 header |= cfg.mpg.mode << 6;
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000885 /* no emphasis (bits 0-1) */
Thom Johansen6e219f82006-09-09 01:35:12 +0000886 putbits( header, 32 );
887
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000888 if(cfg.mpg.type == 1)
Thom Johansen6e219f82006-09-09 01:35:12 +0000889 { /* MPEG1 */
890 if(cfg.channels == 2) { putlong( 0, 20); }
891 else { putlong( 0, 18); }
892
893 for(gr=0; gr<cfg.granules; gr++)
894 for(ch=0; ch<cfg.channels; ch++)
895 {
896 side_info_t *gi = &si[gr][ch];
897
Antonius Hellmann75807212008-06-02 19:28:08 +0000898 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
899 putlong( gi->address3>>1, 9 );
900 putlong( gi->global_gain, 8 );
901 putlong( 9, 4 ); /* set scale_facs compr type */
902 putlong( gi->table_select[0], 6 );
903 putlong( gi->table_select[1], 5 );
904 putlong( gi->table_select[2], 5 );
905 putlong( gi->region_0_1, 7 );
906 putlong( 1 , 2 ); /* set scale_facs to 1bit */
907 putlong( gi->table_select[3], 1 );
Thom Johansen6e219f82006-09-09 01:35:12 +0000908 }
909 }
910 else
911 { /* MPEG2 */
912 if(cfg.channels == 2) { putlong( 0, 10); }
913 else { putlong( 0, 9); }
914
915 for(ch=0; ch<cfg.channels; ch++)
916 {
917 side_info_t *gi = &si[0][ch];
918
Antonius Hellmann75807212008-06-02 19:28:08 +0000919 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
920 putlong( gi->address3>>1, 9 );
921 putlong( gi->global_gain, 8 );
922 putlong( 0xCA, 9 ); /* set scale_facs compr type */
923 putlong( gi->table_select[0], 6 );
924 putlong( gi->table_select[1], 5 );
925 putlong( gi->table_select[2], 5 );
926 putlong( gi->region_0_1 , 7 );
927 putlong( 1 , 1 ); /* set scale_facs to 1bit */
928 putlong( gi->table_select[3], 1 );
Thom Johansen6e219f82006-09-09 01:35:12 +0000929 }
930 }
931 /* flush remaining bits */
932 putbits(cc, sz);
933}
934
935/* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS,
936 as well as the definitions of the side information on pages 26 and 27. */
Antonius Hellmann75807212008-06-02 19:28:08 +0000937static void Huffmancodebits( short *ix, char *xr_sign, side_info_t *gi )
Thom Johansen6e219f82006-09-09 01:35:12 +0000938{
939 int region1 = gi->address1;
940 int region2 = gi->address2;
941 int bigvals = gi->address3;
942 int count1 = bigvals + (gi->count1 << 2);
943 int stuffBits = 0;
944 int bits = 0;
Antonius Hellmann75807212008-06-02 19:28:08 +0000945 int i, v;
946
947 for(i=v=0; i<32; i+=2)
948 v |= band_scale_f[i>>1] << (30-i);
949 putbits(v, 32); // store scale_facs (part1)
950
951 for(v=0; i<42; i+=2)
952 v |= band_scale_f[i>>1] << (40-i);
953 putbits(v, 10); // store scale_facs (part2)
Thom Johansen6e219f82006-09-09 01:35:12 +0000954
955 if(region1 > 0)
Antonius Hellmann75807212008-06-02 19:28:08 +0000956 bits += HuffmanCode(ix, xr_sign, 0 , region1, gi->table_select[0]);
Thom Johansen6e219f82006-09-09 01:35:12 +0000957
958 if(region2 > region1)
Antonius Hellmann75807212008-06-02 19:28:08 +0000959 bits += HuffmanCode(ix, xr_sign, region1, region2, gi->table_select[1]);
Thom Johansen6e219f82006-09-09 01:35:12 +0000960
961 if(bigvals > region2)
Antonius Hellmann75807212008-06-02 19:28:08 +0000962 bits += HuffmanCode(ix, xr_sign, region2, bigvals, gi->table_select[2]);
Thom Johansen6e219f82006-09-09 01:35:12 +0000963
964 if(count1 > bigvals)
Antonius Hellmann75807212008-06-02 19:28:08 +0000965 bits += HuffmanCod1(ix, xr_sign, bigvals, count1, gi->table_select[3]);
Thom Johansen6e219f82006-09-09 01:35:12 +0000966
967 if((stuffBits = gi->part2_3_length - bits) > 0)
968 {
969 int stuffWords = stuffBits >> 5;
970 int remainBits = stuffBits & 31;
971
972 if( remainBits )
973 putbits( ~0, remainBits );
974
975 while( stuffWords-- )
976 putbits( ~0, 32 ); /* Huffman code tables leed to padding ones */
977 }
978}
979
Antonius Hellmann75807212008-06-02 19:28:08 +0000980int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int tbl)
Thom Johansen6e219f82006-09-09 01:35:12 +0000981{
Michael Sevakis0f5cb942006-11-06 18:07:30 +0000982 uint32_t cc=0, sz=0;
983 uint32_t i, d, p;
Thom Johansen6e219f82006-09-09 01:35:12 +0000984 int sumbit=0, s=0, l=0, v, w, x, y;
Antonius Hellmann75807212008-06-02 19:28:08 +0000985 #define sgnv xr_sign[i+0]
986 #define sgnw xr_sign[i+1]
987 #define sgnx xr_sign[i+2]
988 #define sgny xr_sign[i+3]
Thom Johansen6e219f82006-09-09 01:35:12 +0000989
990 for(i=begin; i<end; i+=4)
991 {
992 v = ix[i+0];
993 w = ix[i+1];
994 x = ix[i+2];
995 y = ix[i+3];
996 p = (v << 3) + (w << 2) + (x << 1) + y;
997
998 switch(p)
999 {
1000 case 0: l=0; s = 0; break;
Antonius Hellmann75807212008-06-02 19:28:08 +00001001 case 1: l=1; s = sgny; break;
1002 case 2: l=1; s = sgnx; break;
1003 case 3: l=2; s = (sgnx << 1) + sgny; break;
1004 case 4: l=1; s = sgnw; break;
1005 case 5: l=2; s = (sgnw << 1) + sgny; break;
1006 case 6: l=2; s = (sgnw << 1) + sgnx; break;
1007 case 7: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break;
1008 case 8: l=1; s = sgnv; break;
Thom Johansen6e219f82006-09-09 01:35:12 +00001009 case 9: l=2; s = (sgnv << 1) + sgny; break;
Antonius Hellmann75807212008-06-02 19:28:08 +00001010 case 10: l=2; s = (sgnv << 1) + sgnx; break;
1011 case 11: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break;
1012 case 12: l=2; s = (sgnv << 1) + sgnw; break;
1013 case 13: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break;
1014 case 14: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break;
Thom Johansen6e219f82006-09-09 01:35:12 +00001015 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break;
1016 }
1017
1018 d = (ht_count[tbl][0][p] << l) + s;
1019 l = ht_count[tbl][1][p];
1020 putlong( d, l );
1021 sumbit += l;
1022 }
1023
1024 /* flush remaining bits */
1025 putbits(cc, sz);
1026
1027 return sumbit;
1028}
1029
1030/* Implements the pseudocode of page 98 of the IS */
Antonius Hellmann75807212008-06-02 19:28:08 +00001031int HuffmanCode(short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table)
Thom Johansen6e219f82006-09-09 01:35:12 +00001032{
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001033 uint32_t cc=0, sz=0, code;
1034 uint32_t i, xl=0, yl=0, idx;
Antonius Hellmann75807212008-06-02 19:28:08 +00001035 int x, y, bit, sumbit=0;
1036 #define sign_x xr_sign[i+0]
1037 #define sign_y xr_sign[i+1]
Thom Johansen6e219f82006-09-09 01:35:12 +00001038
1039 if(table == 0)
1040 return 0;
1041
1042 if( table > 15 )
1043 { /* ESC-table is used */
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001044 uint32_t linbits = ht_big[table-16].linbits;
1045 uint16_t *hffcode = table < 24 ? t16HB : t24HB;
1046 uint8_t *hlen = table < 24 ? t16l : t24l;
Thom Johansen6e219f82006-09-09 01:35:12 +00001047
1048 for(i=begin; i<end; i+=2)
1049 {
1050 x = ix[ i ];
1051 y = ix[i+1];
1052
1053 if(x > 14) { xl = x - 15; x = 15; }
1054 if(y > 14) { yl = y - 15; y = 15; }
1055
1056 idx = x * 16 + y;
1057 code = hffcode[idx];
1058 bit = hlen [idx];
1059
1060 if(x)
1061 {
1062 if(x > 14)
1063 {
1064 code = (code << linbits) | xl;
1065 bit += linbits;
1066 }
1067
1068 code = (code << 1) | sign_x;
1069 bit += 1;
1070 }
1071
1072 if(y)
1073 {
1074 if(y > 14)
1075 {
Antonius Hellmann75807212008-06-02 19:28:08 +00001076 if(bit + linbits + 1 > 32)
1077 {
1078 putlong( code, bit );
1079 sumbit += bit;
1080 code = bit = 0;
1081 }
1082
Thom Johansen6e219f82006-09-09 01:35:12 +00001083 code = (code << linbits) | yl;
1084 bit += linbits;
1085 }
1086
1087 code = (code << 1) | sign_y;
1088 bit += 1;
1089 }
1090
1091 putlong( code, bit );
1092 sumbit += bit;
1093 }
1094 }
1095 else
1096 { /* No ESC-words */
1097 const struct huffcodetab *h = &ht[table];
1098
1099 for(i=begin; i<end; i+=2)
1100 {
1101 x = ix[i];
1102 y = ix[i+1];
1103
1104 idx = x * h->len + y;
1105 code = h->table[idx];
1106 bit = h->hlen [idx];
1107
1108 if(x)
1109 {
1110 code = (code << 1) | sign_x;
1111 bit += 1;
1112 }
1113
1114 if(y)
1115 {
1116 code = (code << 1) | sign_y;
1117 bit += 1;
1118 }
1119
1120 putlong( code, bit );
1121 sumbit += bit;
1122 }
1123 }
1124
1125 /* flush remaining bits */
1126 putbits(cc, sz);
1127
1128 return sumbit;
1129}
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001130
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001131void putbits(uint32_t val, uint32_t nbit)
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001132{
1133 int new_bitpos = CodedData.bitpos + nbit;
1134 int ptrpos = CodedData.bitpos >> 5;
1135
1136 val = val & (0xffffffff >> (32 - nbit));
1137
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001138 /* data fit in one uint32_t */
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001139 if(((new_bitpos - 1) >> 5) == ptrpos)
1140 CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31);
1141 else
1142 {
1143 CodedData.bbuf[ptrpos ] |= val >> ((new_bitpos - 32) & 31);
Thom Johansen6e219f82006-09-09 01:35:12 +00001144 CodedData.bbuf[ptrpos+1] |= val << ((32 - new_bitpos) & 31);
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001145 }
1146
1147 CodedData.bitpos = new_bitpos;
1148}
1149
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001150/***************************************************************************/
1151/* Choose the Huffman table that will encode ix[begin..end] with */
1152/* the fewest bits. */
1153/* Note: This code contains knowledge about the sizes and characteristic */
1154/* of the Huffman tables as defined in the IS (Table B.7), and will not */
1155/* work with any arbitrary tables. */
1156/***************************************************************************/
Bertrik Sikken8e22f7f2008-12-29 19:49:48 +00001157static int choose_table( short *ix, uint32_t begin, uint32_t end, int *bits )
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001158{
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001159 uint32_t i;
Thom Johansen6e219f82006-09-09 01:35:12 +00001160 int max, table0, table1;
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001161
1162 for(i=begin,max=0; i<end; i++)
1163 if(ix[i] > max)
1164 max = ix[i];
1165
Thom Johansen6e219f82006-09-09 01:35:12 +00001166 if(max < 16)
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001167 {
Thom Johansen6e219f82006-09-09 01:35:12 +00001168 /* tables without linbits */
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001169 /* indx: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
Thom Johansen6e219f82006-09-09 01:35:12 +00001170 /* len: 0, 2, 3, 3, 0, 4, 4, 6, 6, 6, 8, 8, 8, 16, 0, 16 */
1171 switch(max)
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001172 {
Thom Johansen6e219f82006-09-09 01:35:12 +00001173 case 0: return 0;
1174 case 1: return count_bit1(ix, begin, end, bits);
1175 case 2: return 2 + find_best_2(ix, begin, end, tab23, 3, bits);
1176 case 3: return 5 + find_best_2(ix, begin, end, tab56, 4, bits);
1177 case 4:
1178 case 5: return 7 + find_best_3(ix, begin, end, tab789, 6, bits);
1179 case 6:
1180 case 7: return 10 + find_best_3(ix, begin, end, tabABC, 8, bits);
1181 default: return 13 + find_best_2(ix, begin, end, tab1315, 16, bits) * 2;
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001182 }
1183 }
1184 else
1185 {
Thom Johansen6e219f82006-09-09 01:35:12 +00001186 /* tables with linbits */
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001187 max -= 15;
1188
Thom Johansen6e219f82006-09-09 01:35:12 +00001189 for(table0=0; table0<8; table0++)
1190 if(ht_big[table0].linmax >= max)
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001191 break;
1192
Thom Johansen6e219f82006-09-09 01:35:12 +00001193 for(table1=8; table1<16; table1++)
1194 if(ht_big[table1].linmax >= max)
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001195 break;
1196
Thom Johansen6e219f82006-09-09 01:35:12 +00001197 return 16 + count_bigv(ix, begin, end, table0, table1, bits);
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001198 }
Thom Johansen6e219f82006-09-09 01:35:12 +00001199}
1200
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001201int find_best_2(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1202 uint32_t len, int *bits)
Thom Johansen6e219f82006-09-09 01:35:12 +00001203{
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001204 uint32_t i, sum = 0;
Thom Johansen6e219f82006-09-09 01:35:12 +00001205
1206 for(i=start; i<end; i+=2)
1207 sum += table[ix[i] * len + ix[i+1]];
1208
1209 if((sum & 0xffff) <= (sum >> 16))
1210 {
1211 *bits = (sum & 0xffff);
1212 return 1;
1213 }
1214 else
1215 {
1216 *bits = sum >> 16;
1217 return 0;
1218 }
1219}
1220
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001221int find_best_3(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1222 uint32_t len, int *bits)
Thom Johansen6e219f82006-09-09 01:35:12 +00001223{
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001224 uint32_t i, j, sum = 0;
Thom Johansen6e219f82006-09-09 01:35:12 +00001225 int sum1 = 0;
1226 int sum2 = 0;
1227 int sum3 = 0;
1228
1229 /* avoid overflow in packed additions: 78*13 < 1024 */
1230 for(i=start; i<end; )
1231 {
1232 j = i + 2*78 > end ? end : i + 2*78;
1233
1234 for(sum=0; i<j; i+=2)
1235 sum += table[ix[i] * len + ix[i+1]];
1236
1237 sum1 += (sum >> 20);
1238 sum2 += (sum >> 10) & 0x3ff;
1239 sum3 += (sum >> 0) & 0x3ff;
1240 }
1241
1242 i = 0;
1243 if(sum1 > sum2) { sum1 = sum2; i = 1; }
1244 if(sum1 > sum3) { sum1 = sum3; i = 2; }
1245
1246 *bits = sum1;
1247
1248 return i;
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001249}
1250
1251/*************************************************************************/
1252/* Function: Count the number of bits necessary to code the subregion. */
1253/*************************************************************************/
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001254int count_bit1(short *ix, uint32_t start, uint32_t end, int *bits )
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001255{
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001256 uint32_t i, sum = 0;
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001257
Thom Johansen6e219f82006-09-09 01:35:12 +00001258 for(i=start; i<end; i+=2)
1259 sum += t1l[4 + ix[i] * 2 + ix[i+1]];
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001260
Thom Johansen6e219f82006-09-09 01:35:12 +00001261 *bits = sum;
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001262
Thom Johansen6e219f82006-09-09 01:35:12 +00001263 return 1; /* this is table1 */
1264}
Michael Sevakis4fc717a2006-08-28 22:38:41 +00001265
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001266int count_bigv(short *ix, uint32_t start, uint32_t end, int table0,
Thom Johansen6e219f82006-09-09 01:35:12 +00001267 int table1, int *bits )
1268{
Michael Sevakis0f5cb942006-11-06 18:07:30 +00001269 uint32_t i, sum0, sum1, sum=0, bigv=0, x, y;
Thom Johansen6e219f82006-09-09 01:35:12 +00001270
1271 /* ESC-table is used */
1272 for(i=start; i<end; i+=2)
1273 {
1274 x = ix[i];
1275 y = ix[i+1];
1276
1277 if(x > 14) { x = 15; bigv++; }
1278 if(y > 14) { y = 15; bigv++; }
1279
1280 sum += tab1624[x * 16 + y];
1281 }