Redo my previous segfault fix in a better way.
[Rockbox.git] / apps / codecs / mp3_enc.c
blob26cc1dd73f0d002ddd2d65cbc15110d997c82e41
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Antonius Hellmann
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 // Shine is an MP3 encoder
21 // Copyright (C) 1999-2000 Gabriel Bouvigne
23 // This library is free software; you can redistribute it and/or
24 // modify it under the terms of the GNU Library General Public
25 // License as published by the Free Software Foundation; either
26 // version 2 of the License, or (at your option) any later version.
28 // This library is distributed in the hope that it will be useful,
29 // but WITHOUT ANY WARRANTY; without even the implied warranty of
30 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 // Library General Public License for more details.
33 #ifndef SIMULATOR
35 #include <inttypes.h>
36 #include "codeclib.h"
38 CODEC_ENC_HEADER
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; }
56 typedef struct {
57 int type; /* 0=(MPEG2 - 22.05,24,16kHz) 1=(MPEG1 - 44.1,48,32kHz) */
58 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;
64 } mpeg_t;
66 /* Side information */
67 typedef struct {
68 uint32_t part2_3_length;
69 int count1; /* number of 0-1-quadruples */
70 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;
76 long quantStep;
77 long additStep;
78 long max_val;
79 } side_info_t;
81 typedef struct {
82 side_info_t cod_info[2][2];
83 mpeg_t mpg;
84 long frac_per_frame;
85 long byte_per_frame;
86 long slot_lag;
87 int sideinfo_len;
88 int mean_bits;
89 int ResvSize;
90 int channels;
91 int granules;
92 long samplerate;
93 } config_t;
95 typedef struct {
96 int bitpos; /* current bitpos for writing */
97 uint32_t bbuf[263];
98 } BF_Data;
100 struct huffcodetab {
101 int len; /* max. index */
102 const uint8_t *table; /* pointer to array[len][len] */
103 const uint8_t *hlen; /* pointer to array[len][len] */
106 struct huffcodebig {
107 int len; /* max. index */
108 int linbits; /* number of linbits */
109 int linmax; /* max number stored in linbits */
112 #define shft4(x) ((x + 8) >> 4)
113 #define shft9(x) ((x + 256) >> 9)
114 #define shft13(x) ((x + 4096) >> 13)
115 #define shft15(x) ((x + 16384) >> 15)
116 #define shft16(x) ((x + 32768) >> 16)
117 #define shft_n(x,n) ((x) >> n)
118 #define SQRT 724 /* sqrt(2) * 512 */
120 static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */
121 static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */
122 static int mdct_freq [SAMPL2] IBSS_ATTR; /* 9216 Bytes */
123 static short enc_data [SAMPL2] IBSS_ATTR; /* 4608 Bytes */
124 static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */
125 static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */
126 static int ca [8] IBSS_ATTR; /* 32 Bytes */
127 static int cs [8] IBSS_ATTR; /* 32 Bytes */
128 static int cx [9] IBSS_ATTR; /* 36 Bytes */
129 static int win [18][4] IBSS_ATTR; /* 288 Bytes */
130 static short enwindow [15*27+24] IBSS_ATTR; /* 862 Bytes */
131 static short int2idx [4096] IBSS_ATTR; /* 8192 Bytes */
132 static uint8_t ht_count [2][2][16] IBSS_ATTR; /* 64 Bytes */
133 static uint32_t tab01 [ 16] IBSS_ATTR; /* 64 Bytes */
134 static uint32_t tab23 [ 9] IBSS_ATTR; /* 36 Bytes */
135 static uint32_t tab56 [ 16] IBSS_ATTR; /* 64 Bytes */
136 static uint32_t tab1315 [256] IBSS_ATTR; /* 1024 Bytes */
137 static uint32_t tab1624 [256] IBSS_ATTR; /* 1024 Bytes */
138 static uint32_t tab789 [ 36] IBSS_ATTR; /* 144 Bytes */
139 static uint32_t tabABC [ 64] IBSS_ATTR; /* 256 Bytes */
140 static uint8_t t1HB [ 4] IBSS_ATTR;
141 static uint8_t t2HB [ 9] IBSS_ATTR;
142 static uint8_t t3HB [ 9] IBSS_ATTR;
143 static uint8_t t5HB [ 16] IBSS_ATTR;
144 static uint8_t t6HB [ 16] IBSS_ATTR;
145 static uint8_t t7HB [ 36] IBSS_ATTR;
146 static uint8_t t8HB [ 36] IBSS_ATTR;
147 static uint8_t t9HB [ 36] IBSS_ATTR;
148 static uint8_t t10HB [ 64] IBSS_ATTR;
149 static uint8_t t11HB [ 64] IBSS_ATTR;
150 static uint8_t t12HB [ 64] IBSS_ATTR;
151 static uint8_t t13HB [256] IBSS_ATTR;
152 static uint8_t t15HB [256] IBSS_ATTR;
153 static uint16_t t16HB [256] IBSS_ATTR;
154 static uint16_t t24HB [256] IBSS_ATTR;
155 static uint8_t t1l [ 8] IBSS_ATTR;
156 static uint8_t t2l [ 9] IBSS_ATTR;
157 static uint8_t t3l [ 9] IBSS_ATTR;
158 static uint8_t t5l [ 16] IBSS_ATTR;
159 static uint8_t t6l [ 16] IBSS_ATTR;
160 static uint8_t t7l [ 36] IBSS_ATTR;
161 static uint8_t t8l [ 36] IBSS_ATTR;
162 static uint8_t t9l [ 36] IBSS_ATTR;
163 static uint8_t t10l [ 64] IBSS_ATTR;
164 static uint8_t t11l [ 64] IBSS_ATTR;
165 static uint8_t t12l [ 64] IBSS_ATTR;
166 static uint8_t t13l [256] IBSS_ATTR;
167 static uint8_t t15l [256] IBSS_ATTR;
168 static uint8_t t16l [256] IBSS_ATTR;
169 static uint8_t t24l [256] IBSS_ATTR;
170 static struct huffcodetab ht [HTN] IBSS_ATTR;
172 static unsigned pcm_chunk_size IBSS_ATTR;
173 static unsigned samp_per_frame IBSS_ATTR;
175 static config_t cfg IBSS_ATTR;
176 static char *res_buffer;
177 static int32_t err IBSS_ATTR;
179 static const uint8_t ht_count_const[2][2][16] =
180 { { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */
181 { 1, 5, 5, 7, 5, 8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } }, /* hleng0 */
182 { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, /* table1 */
183 { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */
185 static const uint8_t t1HB_const[4] = {1,1,1,0};
186 static const uint8_t t2HB_const[9] = {1,2,1,3,1,1,3,2,0};
187 static const uint8_t t3HB_const[9] = {3,2,1,1,1,1,3,2,0};
188 static const uint8_t t5HB_const[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0};
189 static const uint8_t t6HB_const[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0};
191 static const uint8_t t7HB_const[36] =
192 { 1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4,
193 12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 };
195 static const uint8_t t8HB_const[36] =
196 { 3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3,
197 19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 };
199 static const uint8_t t9HB_const[36] =
200 { 7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5,
201 15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 };
203 static const uint8_t t10HB_const[64] =
204 {1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32,
205 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22,
206 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 };
208 static const uint8_t t11HB_const[64] =
209 {3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30,
210 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26,
211 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 };
213 static const uint8_t t12HB_const[64] =
214 {9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21,
215 30,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17,
216 31,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 };
218 static const uint8_t t13HB_const[256] =
219 {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,
220 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,
221 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,
222 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,
223 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,
224 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,
225 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,
226 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,
227 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,
228 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 };
230 static const uint8_t t15HB_const[256] =
231 {7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51,
232 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,
233 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,
234 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,
235 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,
236 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,
237 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,
238 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,
239 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,
240 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};
242 static const uint16_t t16HB_const[256] =
243 {1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47,
244 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209,
245 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65,
246 115,101,179,164,155,264,246,226,395,382,362,9,66,30,59,56,102,185,173,265,142,
247 253,232,400,388,378,445,16,111,54,52,100,184,178,160,133,257,244,228,217,385,
248 366,715,10,98,48,91,88,165,157,148,261,248,407,397,372,380,889,884,8,85,84,81,
249 159,156,143,260,249,427,401,392,383,727,713,708,7,154,76,73,141,131,256,245,
250 426,406,394,384,735,359,710,352,11,139,129,67,125,247,233,229,219,393,743,737,
251 720,885,882,439,4,243,120,118,115,227,223,396,746,742,736,721,712,706,223,436,
252 6,202,224,222,218,216,389,386,381,364,888,443,707,440,437,1728,4,747,211,210,
253 208,370,379,734,723,714,1735,883,877,876,3459,865,2,377,369,102,187,726,722,
254 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3,
255 1,3};
257 static const uint16_t t24HB_const[256] =
258 {15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71,
259 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194,
260 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293,
261 271,16,147,72,69,135,127,118,112,210,200,188,352,323,306,285,540,14,263,66,
262 129,126,119,114,214,202,192,180,341,317,301,281,262,12,249,123,121,117,113,
263 215,206,195,185,347,330,308,291,272,520,10,435,115,111,109,211,203,196,187,
264 353,332,313,298,283,531,381,17,427,212,208,205,201,193,186,177,169,320,303,
265 286,268,514,377,16,335,199,197,191,189,181,174,333,321,305,289,275,521,379,
266 371,11,668,184,183,179,175,344,331,314,304,290,277,530,383,373,366,10,652,346,
267 171,168,164,318,309,299,287,276,263,513,375,368,362,6,648,322,316,312,307,302,
268 292,284,269,261,512,376,370,364,359,4,620,300,296,294,288,282,273,266,515,380,
269 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360,
270 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3};
272 static const uint32_t tab1315_const[256] =
273 { 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a,
274 0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e,
275 0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a,
276 0x0b000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0e000c,0x0e000d,0x0e000d,
277 0x070006,0x080007,0x090007,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,
278 0x0b000a,0x0c000b,0x0c000b,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
279 0x080007,0x090008,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0c000b,
280 0x0c000b,0x0d000b,0x0d000c,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
281 0x090008,0x090008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
282 0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000d,
283 0x0a0009,0x0a0009,0x0b0009,0x0c000a,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
284 0x0d000b,0x0d000b,0x0e000c,0x0d000c,0x0f000d,0x0f000d,0x10000d,0x10000e,
285 0x0a000a,0x0b0009,0x0c000a,0x0c000a,0x0d000a,0x0d000b,0x0d000b,0x0d000b,
286 0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x10000e,
287 0x0b000a,0x0b000a,0x0c000a,0x0d000b,0x0d000b,0x0d000b,0x0e000b,0x0e000c,
288 0x0e000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x12000d,0x12000e,
289 0x0a000a,0x0a000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0e000c,
290 0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x11000e,0x11000e,
291 0x0b000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0d000c,0x0f000c,
292 0x0e000c,0x0f000d,0x0f000d,0x10000d,0x10000d,0x10000e,0x12000e,0x11000e,
293 0x0b000b,0x0c000b,0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000c,
294 0x0e000c,0x0f000d,0x10000d,0x0f000d,0x10000d,0x11000e,0x12000f,0x13000e,
295 0x0c000b,0x0c000b,0x0c000b,0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0e000c,
296 0x0f000d,0x0f000d,0x0f000d,0x10000d,0x11000e,0x11000e,0x11000e,0x12000f,
297 0x0c000c,0x0d000c,0x0d000b,0x0e000c,0x0e000c,0x0f000c,0x0e000d,0x0f000d,
298 0x10000d,0x10000d,0x11000d,0x11000d,0x11000e,0x12000e,0x12000f,0x12000f,
299 0x0d000c,0x0d000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x10000d,
300 0x10000d,0x10000e,0x10000e,0x11000e,0x12000e,0x11000e,0x12000f,0x12000f,
301 0x0e000d,0x0e000d,0x0e000d,0x0f000d,0x0f000d,0x0f000d,0x11000d,0x10000d,
302 0x10000e,0x13000e,0x11000e,0x11000e,0x11000f,0x13000f,0x12000e,0x12000f,
303 0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e,
304 0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f };
306 static const uint32_t tab01_const[16] =
307 { 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007,
308 0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 };
310 static const uint32_t tab23_const[ 9] =
311 { 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 };
313 static const uint32_t tab56_const[16] =
314 { 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007,
315 0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 };
317 static const uint32_t tab789_const[36] =
318 {0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004,
319 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806,
320 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808,
321 0x00b02c09,0x00b02c09,0x00b0300a,0x00802408,0x00902408,0x00a02809,0x00b02c09,
322 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b,
323 0x00c0340b};
325 static const uint32_t tabABC_const[64] =
326 {0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a,
327 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809,
328 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809,
329 0x00c02c0a,0x00b02809,0x00b0280a,0x00802007,0x00902007,0x00a02408,0x00b02c08,
330 0x00c02809,0x00c0300a,0x00b0280a,0x00c02c0a,0x00902408,0x00a02808,0x00b02809,
331 0x00c02c09,0x00c02c0a,0x00c0300a,0x00c02c0a,0x00c0300b,0x00a02409,0x00b02809,
332 0x00c02c0a,0x00c0300a,0x00d0300a,0x00d0340b,0x00c0300a,0x00d0340b,0x00902409,
333 0x00a02409,0x00b02409,0x00c0280a,0x00c02c0a,0x00c0300b,0x00d0300b,0x00d0300c,
334 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b,
335 0x00d0300c};
337 static const uint32_t tab1624_const[256] =
338 {0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a,
339 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c,
340 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009,
341 0x000b000a,0x000b000a,0x000b000b,0x000c000b,0x000c000b,0x000c000c,0x000d000c,
342 0x000e000c,0x000d000c,0x000e000c,0x000a000a,0x00070007,0x00080007,0x00090008,
343 0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000b,0x000d000b,0x000c000b,
344 0x000d000b,0x000d000c,0x000d000c,0x000e000c,0x000e000d,0x000b0009,0x00090008,
345 0x00090008,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,0x000c000b,
346 0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,0x000f000c,
347 0x000c0009,0x000a0009,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,
348 0x000d000a,0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,
349 0x000f000c,0x000f000d,0x000b0009,0x000a000a,0x000a0009,0x000b000a,0x000b000a,
350 0x000c000a,0x000d000a,0x000d000b,0x000e000b,0x000d000b,0x000e000b,0x000e000c,
351 0x000f000c,0x000f000c,0x000f000c,0x0010000c,0x000c0009,0x000b000a,0x000b000a,
352 0x000b000a,0x000c000a,0x000d000a,0x000d000b,0x000d000b,0x000d000b,0x000e000b,
353 0x000e000c,0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000d,0x000c0009,
354 0x000b000b,0x000b000a,0x000c000a,0x000c000a,0x000d000b,0x000d000b,0x000d000b,
355 0x000e000b,0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000d,
356 0x0011000d,0x000c000a,0x000b000b,0x000c000b,0x000c000b,0x000d000b,0x000d000b,
357 0x000d000b,0x000e000b,0x000e000b,0x000f000b,0x000f000c,0x000f000c,0x000f000c,
358 0x0010000c,0x0010000d,0x0010000d,0x000c000a,0x000c000b,0x000c000b,0x000c000b,
359 0x000d000b,0x000d000b,0x000e000b,0x000e000b,0x000f000c,0x000f000c,0x000f000c,
360 0x000f000c,0x0010000c,0x000f000d,0x0010000d,0x000f000d,0x000d000a,0x000c000c,
361 0x000d000b,0x000c000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000e000c,
362 0x000f000c,0x0010000c,0x0010000c,0x0010000d,0x0011000d,0x0011000d,0x0010000d,
363 0x000c000a,0x000d000c,0x000d000c,0x000d000b,0x000d000b,0x000e000b,0x000e000c,
364 0x000f000c,0x0010000c,0x0010000c,0x0010000c,0x0010000c,0x0010000d,0x0010000d,
365 0x000f000d,0x0010000d,0x000d000a,0x000d000c,0x000e000c,0x000e000c,0x000e000c,
366 0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000c,0x0010000d,
367 0x0010000d,0x0010000d,0x0010000d,0x0012000d,0x000d000a,0x000f000c,0x000e000c,
368 0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000c,0x0010000c,0x0010000d,
369 0x0012000d,0x0011000d,0x0011000d,0x0011000d,0x0013000d,0x0011000d,0x000d000a,
370 0x000e000d,0x000f000c,0x000d000c,0x000e000c,0x0010000c,0x0010000c,0x000f000c,
371 0x0010000d,0x0010000d,0x0011000d,0x0012000d,0x0011000d,0x0013000d,0x0011000d,
372 0x0010000d,0x000d000a,0x000a0009,0x000a0009,0x000a0009,0x000b0009,0x000b0009,
373 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a,
374 0x000d000a,0x000d000a,0x000d000a,0x000a0006};
376 static const uint8_t t1l_const[8] = {1,3,2,3,1,4,3,5};
377 static const uint8_t t2l_const[9] = {1,3,6,3,3,5,5,5,6};
378 static const uint8_t t3l_const[9] = {2,2,6,3,2,5,5,5,6};
379 static const uint8_t t5l_const[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8};
380 static const uint8_t t6l_const[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7};
382 static const uint8_t t7l_const[36] =
383 {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};
385 static const uint8_t t8l_const[36] =
386 {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};
388 static const uint8_t t9l_const[36] =
389 {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};
391 static const uint8_t t10l_const[64] =
392 {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,
393 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};
395 static const uint8_t t11l_const[64] =
396 {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,
397 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};
399 static const uint8_t t12l_const[64] =
400 {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,
401 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};
403 static const uint8_t t13l_const[256] =
404 {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,
405 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,
406 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,
407 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,
408 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,
409 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,
410 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,
411 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,
412 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,
413 15,15,16,16,19,18,19,16};
415 static const uint8_t t15l_const[256] =
416 {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,
417 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,
418 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,
419 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,
420 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,
421 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,
422 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,
423 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,
424 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13};
426 static const uint8_t t16l_const[256] =
427 {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,
428 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,
429 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,
430 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,
431 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,
432 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,
433 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,
434 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,
435 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,
436 11,11,11,11,11,11,11,8};
438 static const uint8_t t24l_const[256] =
439 {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,
440 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,
441 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,
442 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,
443 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,
444 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,
445 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,
446 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,
447 7,7,7,7,7,7,8,8,8,8,4};
449 static const struct huffcodetab ht_const[HTN] =
450 { { 0, NULL, NULL}, /* Apparently not used */
451 { 2, t1HB, t1l},
452 { 3, t2HB, t2l},
453 { 3, t3HB, t3l},
454 { 0, NULL, NULL}, /* Apparently not used */
455 { 4, t5HB, t5l},
456 { 4, t6HB, t6l},
457 { 6, t7HB, t7l},
458 { 6, t8HB, t8l},
459 { 6, t9HB, t9l},
460 { 8, t10HB, t10l},
461 { 8, t11HB, t11l},
462 { 8, t12HB, t12l},
463 {16, t13HB, t13l},
464 { 0, NULL, NULL}, /* Apparently not used */
465 {16, t15HB, t15l} };
467 static const struct huffcodebig ht_big[HTN] =
468 { { 16, 1, 1 },
469 { 16, 2, 3 },
470 { 16, 3, 7 },
471 { 16, 4, 15 },
472 { 16, 6, 63 },
473 { 16, 8, 255 },
474 { 16, 10, 1023 },
475 { 16, 13, 8191 },
476 { 16, 4, 15 },
477 { 16, 5, 31 },
478 { 16, 6, 63 },
479 { 16, 7, 127 },
480 { 16, 8, 255 },
481 { 16, 9, 511 },
482 { 16, 11, 2047 },
483 { 16, 13, 8191 } };
485 static const struct
487 uint32_t region0_cnt;
488 uint32_t region1_cnt;
489 } subdv_table[23] =
490 { {0, 0}, /* 0 bands */
491 {0, 0}, /* 1 bands */
492 {0, 0}, /* 2 bands */
493 {0, 0}, /* 3 bands */
494 {0, 0}, /* 4 bands */
495 {0, 1}, /* 5 bands */
496 {1, 1}, /* 6 bands */
497 {1, 1}, /* 7 bands */
498 {1, 2}, /* 8 bands */
499 {2, 2}, /* 9 bands */
500 {2, 3}, /* 10 bands */
501 {2, 3}, /* 11 bands */
502 {3, 4}, /* 12 bands */
503 {3, 4}, /* 13 bands */
504 {3, 4}, /* 14 bands */
505 {4, 5}, /* 15 bands */
506 {4, 5}, /* 16 bands */
507 {4, 6}, /* 17 bands */
508 {5, 6}, /* 18 bands */
509 {5, 6}, /* 19 bands */
510 {5, 7}, /* 20 bands */
511 {6, 7}, /* 21 bands */
512 {6, 7}, /* 22 bands */
515 static const uint32_t sfBand[6][23] =
517 /* Table B.2.b: 22.05 kHz */
518 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
519 /* Table B.2.c: 24 kHz */
520 {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
521 /* Table B.2.a: 16 kHz */
522 {0,6,12,18,24,30,36,44,45,66,80,96,116,140,168,200,238,248,336,396,464,522,576},
523 /* Table B.8.b: 44.1 kHz */
524 {0,4, 8,12,16,20,24,30,36,44,52,62, 74, 90,110,134,162,196,238,288,342,418,576},
525 /* Table B.8.c: 48 kHz */
526 {0,4, 8,12,16,20,24,30,36,42,50,60, 72, 88,106,128,156,190,230,276,330,384,576},
527 /* Table B.8.a: 32 kHz */
528 {0,4, 8,12,16,20,24,30,36,44,54,66, 82,102,126,156,194,240,296,364,448,550,576} };
531 static const short int2idx_const[4096] = /* int2idx[i] = sqrt(i*sqrt(i)); */
533 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9,
534 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16,
535 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21,
536 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26,
537 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
538 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36,
539 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 40,
540 41, 41, 41, 41, 42, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45,
541 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49,
542 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53,
543 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57,
544 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61,
545 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65,
546 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68,
547 68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 72, 72, 72,
548 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75,
549 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 79, 79, 79,
550 79, 79, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82,
551 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 86, 86, 86,
552 86, 86, 86, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89,
553 89, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 93,
554 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, 95, 96, 96, 96,
555 96, 96, 96, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99,
556 99, 99,100,100,100,100,100,100,101,101,101,101,101,101,102,102,102,102,102,102,
557 103,103,103,103,103,103,104,104,104,104,104,104,104,105,105,105,105,105,105,106,
558 106,106,106,106,106,107,107,107,107,107,107,107,108,108,108,108,108,108,109,109,
559 109,109,109,109,110,110,110,110,110,110,110,111,111,111,111,111,111,112,112,112,
560 112,112,112,112,113,113,113,113,113,113,114,114,114,114,114,114,115,115,115,115,
561 115,115,115,116,116,116,116,116,116,117,117,117,117,117,117,117,118,118,118,118,
562 118,118,118,119,119,119,119,119,119,120,120,120,120,120,120,120,121,121,121,121,
563 121,121,122,122,122,122,122,122,122,123,123,123,123,123,123,123,124,124,124,124,
564 124,124,125,125,125,125,125,125,125,126,126,126,126,126,126,126,127,127,127,127,
565 127,127,128,128,128,128,128,128,128,129,129,129,129,129,129,129,130,130,130,130,
566 130,130,131,131,131,131,131,131,131,132,132,132,132,132,132,132,133,133,133,133,
567 133,133,133,134,134,134,134,134,134,134,135,135,135,135,135,135,136,136,136,136,
568 136,136,136,137,137,137,137,137,137,137,138,138,138,138,138,138,138,139,139,139,
569 139,139,139,139,140,140,140,140,140,140,140,141,141,141,141,141,141,141,142,142,
570 142,142,142,142,142,143,143,143,143,143,143,143,144,144,144,144,144,144,144,145,
571 145,145,145,145,145,145,146,146,146,146,146,146,146,147,147,147,147,147,147,147,
572 148,148,148,148,148,148,148,149,149,149,149,149,149,149,150,150,150,150,150,150,
573 150,151,151,151,151,151,151,151,152,152,152,152,152,152,152,153,153,153,153,153,
574 153,153,154,154,154,154,154,154,154,154,155,155,155,155,155,155,155,156,156,156,
575 156,156,156,156,157,157,157,157,157,157,157,158,158,158,158,158,158,158,159,159,
576 159,159,159,159,159,160,160,160,160,160,160,160,160,161,161,161,161,161,161,161,
577 162,162,162,162,162,162,162,163,163,163,163,163,163,163,163,164,164,164,164,164,
578 164,164,165,165,165,165,165,165,165,166,166,166,166,166,166,166,167,167,167,167,
579 167,167,167,167,168,168,168,168,168,168,168,169,169,169,169,169,169,169,169,170,
580 170,170,170,170,170,170,171,171,171,171,171,171,171,172,172,172,172,172,172,172,
581 172,173,173,173,173,173,173,173,174,174,174,174,174,174,174,174,175,175,175,175,
582 175,175,175,176,176,176,176,176,176,176,176,177,177,177,177,177,177,177,178,178,
583 178,178,178,178,178,178,179,179,179,179,179,179,179,180,180,180,180,180,180,180,
584 180,181,181,181,181,181,181,181,182,182,182,182,182,182,182,182,183,183,183,183,
585 183,183,183,184,184,184,184,184,184,184,184,185,185,185,185,185,185,185,186,186,
586 186,186,186,186,186,186,187,187,187,187,187,187,187,187,188,188,188,188,188,188,
587 188,189,189,189,189,189,189,189,189,190,190,190,190,190,190,190,190,191,191,191,
588 191,191,191,191,192,192,192,192,192,192,192,192,193,193,193,193,193,193,193,193,
589 194,194,194,194,194,194,194,195,195,195,195,195,195,195,195,196,196,196,196,196,
590 196,196,196,197,197,197,197,197,197,197,197,198,198,198,198,198,198,198,199,199,
591 199,199,199,199,199,199,200,200,200,200,200,200,200,200,201,201,201,201,201,201,
592 201,201,202,202,202,202,202,202,202,202,203,203,203,203,203,203,203,204,204,204,
593 204,204,204,204,204,205,205,205,205,205,205,205,205,206,206,206,206,206,206,206,
594 206,207,207,207,207,207,207,207,207,208,208,208,208,208,208,208,208,209,209,209,
595 209,209,209,209,209,210,210,210,210,210,210,210,210,211,211,211,211,211,211,211,
596 211,212,212,212,212,212,212,212,212,213,213,213,213,213,213,213,213,214,214,214,
597 214,214,214,214,214,215,215,215,215,215,215,215,215,216,216,216,216,216,216,216,
598 216,217,217,217,217,217,217,217,217,218,218,218,218,218,218,218,218,219,219,219,
599 219,219,219,219,219,220,220,220,220,220,220,220,220,221,221,221,221,221,221,221,
600 221,222,222,222,222,222,222,222,222,223,223,223,223,223,223,223,223,224,224,224,
601 224,224,224,224,224,225,225,225,225,225,225,225,225,226,226,226,226,226,226,226,
602 226,227,227,227,227,227,227,227,227,228,228,228,228,228,228,228,228,229,229,229,
603 229,229,229,229,229,229,230,230,230,230,230,230,230,230,231,231,231,231,231,231,
604 231,231,232,232,232,232,232,232,232,232,233,233,233,233,233,233,233,233,234,234,
605 234,234,234,234,234,234,234,235,235,235,235,235,235,235,235,236,236,236,236,236,
606 236,236,236,237,237,237,237,237,237,237,237,238,238,238,238,238,238,238,238,238,
607 239,239,239,239,239,239,239,239,240,240,240,240,240,240,240,240,241,241,241,241,
608 241,241,241,241,242,242,242,242,242,242,242,242,242,243,243,243,243,243,243,243,
609 243,244,244,244,244,244,244,244,244,245,245,245,245,245,245,245,245,245,246,246,
610 246,246,246,246,246,246,247,247,247,247,247,247,247,247,248,248,248,248,248,248,
611 248,248,248,249,249,249,249,249,249,249,249,250,250,250,250,250,250,250,250,250,
612 251,251,251,251,251,251,251,251,252,252,252,252,252,252,252,252,253,253,253,253,
613 253,253,253,253,253,254,254,254,254,254,254,254,254,255,255,255,255,255,255,255,
614 255,255,256,256,256,256,256,256,256,256,257,257,257,257,257,257,257,257,257,258,
615 258,258,258,258,258,258,258,259,259,259,259,259,259,259,259,259,260,260,260,260,
616 260,260,260,260,261,261,261,261,261,261,261,261,261,262,262,262,262,262,262,262,
617 262,263,263,263,263,263,263,263,263,263,264,264,264,264,264,264,264,264,265,265,
618 265,265,265,265,265,265,265,266,266,266,266,266,266,266,266,267,267,267,267,267,
619 267,267,267,267,268,268,268,268,268,268,268,268,268,269,269,269,269,269,269,269,
620 269,270,270,270,270,270,270,270,270,270,271,271,271,271,271,271,271,271,271,272,
621 272,272,272,272,272,272,272,273,273,273,273,273,273,273,273,273,274,274,274,274,
622 274,274,274,274,275,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276,
623 276,276,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,279,
624 279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,281,281,281,
625 281,281,281,281,281,282,282,282,282,282,282,282,282,282,283,283,283,283,283,283,
626 283,283,283,284,284,284,284,284,284,284,284,284,285,285,285,285,285,285,285,285,
627 286,286,286,286,286,286,286,286,286,287,287,287,287,287,287,287,287,287,288,288,
628 288,288,288,288,288,288,288,289,289,289,289,289,289,289,289,289,290,290,290,290,
629 290,290,290,290,291,291,291,291,291,291,291,291,291,292,292,292,292,292,292,292,
630 292,292,293,293,293,293,293,293,293,293,293,294,294,294,294,294,294,294,294,294,
631 295,295,295,295,295,295,295,295,295,296,296,296,296,296,296,296,296,296,297,297,
632 297,297,297,297,297,297,297,298,298,298,298,298,298,298,298,299,299,299,299,299,
633 299,299,299,299,300,300,300,300,300,300,300,300,300,301,301,301,301,301,301,301,
634 301,301,302,302,302,302,302,302,302,302,302,303,303,303,303,303,303,303,303,303,
635 304,304,304,304,304,304,304,304,304,305,305,305,305,305,305,305,305,305,306,306,
636 306,306,306,306,306,306,306,307,307,307,307,307,307,307,307,307,308,308,308,308,
637 308,308,308,308,308,309,309,309,309,309,309,309,309,309,310,310,310,310,310,310,
638 310,310,310,311,311,311,311,311,311,311,311,311,312,312,312,312,312,312,312,312,
639 312,313,313,313,313,313,313,313,313,313,314,314,314,314,314,314,314,314,314,315,
640 315,315,315,315,315,315,315,315,316,316,316,316,316,316,316,316,316,317,317,317,
641 317,317,317,317,317,317,318,318,318,318,318,318,318,318,318,318,319,319,319,319,
642 319,319,319,319,319,320,320,320,320,320,320,320,320,320,321,321,321,321,321,321,
643 321,321,321,322,322,322,322,322,322,322,322,322,323,323,323,323,323,323,323,323,
644 323,324,324,324,324,324,324,324,324,324,325,325,325,325,325,325,325,325,325,325,
645 326,326,326,326,326,326,326,326,326,327,327,327,327,327,327,327,327,327,328,328,
646 328,328,328,328,328,328,328,329,329,329,329,329,329,329,329,329,330,330,330,330,
647 330,330,330,330,330,330,331,331,331,331,331,331,331,331,331,332,332,332,332,332,
648 332,332,332,332,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,
649 334,334,335,335,335,335,335,335,335,335,335,335,336,336,336,336,336,336,336,336,
650 336,337,337,337,337,337,337,337,337,337,338,338,338,338,338,338,338,338,338,338,
651 339,339,339,339,339,339,339,339,339,340,340,340,340,340,340,340,340,340,341,341,
652 341,341,341,341,341,341,341,341,342,342,342,342,342,342,342,342,342,343,343,343,
653 343,343,343,343,343,343,344,344,344,344,344,344,344,344,344,344,345,345,345,345,
654 345,345,345,345,345,346,346,346,346,346,346,346,346,346,347,347,347,347,347,347,
655 347,347,347,347,348,348,348,348,348,348,348,348,348,349,349,349,349,349,349,349,
656 349,349,350,350,350,350,350,350,350,350,350,350,351,351,351,351,351,351,351,351,
657 351,352,352,352,352,352,352,352,352,352,352,353,353,353,353,353,353,353,353,353,
658 354,354,354,354,354,354,354,354,354,355,355,355,355,355,355,355,355,355,355,356,
659 356,356,356,356,356,356,356,356,357,357,357,357,357,357,357,357,357,357,358,358,
660 358,358,358,358,358,358,358,359,359,359,359,359,359,359,359,359,359,360,360,360,
661 360,360,360,360,360,360,361,361,361,361,361,361,361,361,361,361,362,362,362,362,
662 362,362,362,362,362,363,363,363,363,363,363,363,363,363,363,364,364,364,364,364,
663 364,364,364,364,365,365,365,365,365,365,365,365,365,365,366,366,366,366,366,366,
664 366,366,366,367,367,367,367,367,367,367,367,367,367,368,368,368,368,368,368,368,
665 368,368,369,369,369,369,369,369,369,369,369,369,370,370,370,370,370,370,370,370,
666 370,370,371,371,371,371,371,371,371,371,371,372,372,372,372,372,372,372,372,372,
667 372,373,373,373,373,373,373,373,373,373,374,374,374,374,374,374,374,374,374,374,
668 375,375,375,375,375,375,375,375,375,375,376,376,376,376,376,376,376,376,376,377,
669 377,377,377,377,377,377,377,377,377,378,378,378,378,378,378,378,378,378,379,379,
670 379,379,379,379,379,379,379,379,380,380,380,380,380,380,380,380,380,380,381,381,
671 381,381,381,381,381,381,381,382,382,382,382,382,382,382,382,382,382,383,383,383,
672 383,383,383,383,383,383,383,384,384,384,384,384,384,384,384,384,385,385,385,385,
673 385,385,385,385,385,385,386,386,386,386,386,386,386,386,386,386,387,387,387,387,
674 387,387,387,387,387,387,388,388,388,388,388,388,388,388,388,389,389,389,389,389,
675 389,389,389,389,389,390,390,390,390,390,390,390,390,390,390,391,391,391,391,391,
676 391,391,391,391,391,392,392,392,392,392,392,392,392,392,393,393,393,393,393,393,
677 393,393,393,393,394,394,394,394,394,394,394,394,394,394,395,395,395,395,395,395,
678 395,395,395,395,396,396,396,396,396,396,396,396,396,397,397,397,397,397,397,397,
679 397,397,397,398,398,398,398,398,398,398,398,398,398,399,399,399,399,399,399,399,
680 399,399,399,400,400,400,400,400,400,400,400,400,400,401,401,401,401,401,401,401,
681 401,401,402,402,402,402,402,402,402,402,402,402,403,403,403,403,403,403,403,403,
682 403,403,404,404,404,404,404,404,404,404,404,404,405,405,405,405,405,405,405,405,
683 405,405,406,406,406,406,406,406,406,406,406,406,407,407,407,407,407,407,407,407,
684 407,407,408,408,408,408,408,408,408,408,408,408,409,409,409,409,409,409,409,409,
685 409,410,410,410,410,410,410,410,410,410,410,411,411,411,411,411,411,411,411,411,
686 411,412,412,412,412,412,412,412,412,412,412,413,413,413,413,413,413,413,413,413,
687 413,414,414,414,414,414,414,414,414,414,414,415,415,415,415,415,415,415,415,415,
688 415,416,416,416,416,416,416,416,416,416,416,417,417,417,417,417,417,417,417,417,
689 417,418,418,418,418,418,418,418,418,418,418,419,419,419,419,419,419,419,419,419,
690 419,420,420,420,420,420,420,420,420,420,420,421,421,421,421,421,421,421,421,421,
691 421,422,422,422,422,422,422,422,422,422,422,423,423,423,423,423,423,423,423,423,
692 423,424,424,424,424,424,424,424,424,424,424,425,425,425,425,425,425,425,425,425,
693 425,426,426,426,426,426,426,426,426,426,426,427,427,427,427,427,427,427,427,427,
694 427,428,428,428,428,428,428,428,428,428,428,429,429,429,429,429,429,429,429,429,
695 429,430,430,430,430,430,430,430,430,430,430,431,431,431,431,431,431,431,431,431,
696 431,432,432,432,432,432,432,432,432,432,432,433,433,433,433,433,433,433,433,433,
697 433,434,434,434,434,434,434,434,434,434,434,435,435,435,435,435,435,435,435,435,
698 435,435,436,436,436,436,436,436,436,436,436,436,437,437,437,437,437,437,437,437,
699 437,437,438,438,438,438,438,438,438,438,438,438,439,439,439,439,439,439,439,439,
700 439,439,440,440,440,440,440,440,440,440,440,440,441,441,441,441,441,441,441,441,
701 441,441,442,442,442,442,442,442,442,442,442,442,443,443,443,443,443,443,443,443,
702 443,443,443,444,444,444,444,444,444,444,444,444,444,445,445,445,445,445,445,445,
703 445,445,445,446,446,446,446,446,446,446,446,446,446,447,447,447,447,447,447,447,
704 447,447,447,448,448,448,448,448,448,448,448,448,448,448,449,449,449,449,449,449,
705 449,449,449,449,450,450,450,450,450,450,450,450,450,450,451,451,451,451,451,451,
706 451,451,451,451,452,452,452,452,452,452,452,452,452,452,453,453,453,453,453,453,
707 453,453,453,453,453,454,454,454,454,454,454,454,454,454,454,455,455,455,455,455,
708 455,455,455,455,455,456,456,456,456,456,456,456,456,456,456,457,457,457,457,457,
709 457,457,457,457,457,457,458,458,458,458,458,458,458,458,458,458,459,459,459,459,
710 459,459,459,459,459,459,460,460,460,460,460,460,460,460,460,460,460,461,461,461,
711 461,461,461,461,461,461,461,462,462,462,462,462,462,462,462,462,462,463,463,463,
712 463,463,463,463,463,463,463,463,464,464,464,464,464,464,464,464,464,464,465,465,
713 465,465,465,465,465,465,465,465,466,466,466,466,466,466,466,466,466,466,466,467,
714 467,467,467,467,467,467,467,467,467,468,468,468,468,468,468,468,468,468,468,469,
715 469,469,469,469,469,469,469,469,469,469,470,470,470,470,470,470,470,470,470,470,
716 471,471,471,471,471,471,471,471,471,471,472,472,472,472,472,472,472,472,472,472,
717 472,473,473,473,473,473,473,473,473,473,473,474,474,474,474,474,474,474,474,474,
718 474,475,475,475,475,475,475,475,475,475,475,475,476,476,476,476,476,476,476,476,
719 476,476,477,477,477,477,477,477,477,477,477,477,477,478,478,478,478,478,478,478,
720 478,478,478,479,479,479,479,479,479,479,479,479,479,479,480,480,480,480,480,480,
721 480,480,480,480,481,481,481,481,481,481,481,481,481,481,482,482,482,482,482,482,
722 482,482,482,482,482,483,483,483,483,483,483,483,483,483,483,484,484,484,484,484,
723 484,484,484,484,484,484,485,485,485,485,485,485,485,485,485,485,486,486,486,486,
724 486,486,486,486,486,486,486,487,487,487,487,487,487,487,487,487,487,488,488,488,
725 488,488,488,488,488,488,488,488,489,489,489,489,489,489,489,489,489,489,490,490,
726 490,490,490,490,490,490,490,490,490,491,491,491,491,491,491,491,491,491,491,492,
727 492,492,492,492,492,492,492,492,492,492,493,493,493,493,493,493,493,493,493,493,
728 494,494,494,494,494,494,494,494,494,494,494,495,495,495,495,495,495,495,495,495,
729 495,496,496,496,496,496,496,496,496,496,496,496,497,497,497,497,497,497,497,497,
730 497,497,497,498,498,498,498,498,498,498,498,498,498,499,499,499,499,499,499,499,
731 499,499,499,499,500,500,500,500,500,500,500,500,500,500,501,501,501,501,501,501,
732 501,501,501,501,501,502,502,502,502,502,502,502,502,502,502,503,503,503,503,503,
733 503,503,503,503,503,503,504,504,504,504,504,504,504,504,504,504,504,505,505,505,
734 505,505,505,505,505,505,505,506,506,506,506,506,506,506,506,506,506,506,507,507,
735 507,507,507,507,507,507,507,507,507,508,508,508,508,508,508,508,508,508,508,509,
736 509,509,509,509,509,509,509,509,509,509,510,510,510,510,510,510,510,510,510,510,
737 510,511,511,511,511,511,511,511,511,511,511,512,512,512,512,512 };
739 static const int order[32] =
740 { 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
741 2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 };
743 static const long sampr_index[2][3] =
744 { { 22050, 24000, 16000 }, /* MPEG 2 */
745 { 44100, 48000, 32000 } }; /* MPEG 1 */
747 static const long bitr_index[2][15] =
748 { {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160}, /* MPEG 2 */
749 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} }; /* MPEG 1 */
751 static const int num_bands[3][15] =
752 { {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32},
753 {0,10,10,10,10,10,12,14, 18, 24, 26, 28, 30, 32, 32},
754 {0,10,12,14,18,24,26,28, 30, 32, 32, 32, 32, 32, 32} };
756 static const int cx_const[9] =
757 { 16135, 10531, 5604, 15396, -2845,-12551, 14189, 8192, 16384 };
759 static const int ca_const[8] =
760 {-16859,-15458,-10269, -5961, -3099, -1342, -465, -121 };
762 static const int cs_const[8] =
763 { 28098, 28893, 31117, 32221, 32621, 32740, 32765, 32768 };
765 static const short enwindow_const[15*27+24] =
766 { 0, 65, 593, 1766, 22228, 2115, 611, 62,
767 8, 119, 1419, 10564,-11659,-1635,-154, -9,
768 -8, -119,-1419,-10564, 11659, 1635, 154, 9, 464, 100, 91,
769 0, 69, 604, 1635, 23148, 2363, 643, 62,
770 7, 107, 1368, 10449,-12733,-1818,-180,-11,
771 -7, -107,-1368,-10449, 12733, 1818, 180, 11, 420, 200, 164,
772 0, 72, 608, 1465, 23979, 2600, 671, 63,
773 7, 94, 1305, 10265,-13818,-2004,-207,-12,
774 -7, -94,-1305,-10265, 13818, 2004, 207, 12, 380, 297, 220,
775 0, 76, 606, 1256, 24718, 2825, 693, 63,
776 6, 81, 1232, 10016,-14908,-2192,-236,-14,
777 -6, -81,-1232,-10016, 14908, 2192, 236, 14, 342, 392, 262,
778 0, 78, 597, 1007, 25359, 3033, 712, 63,
779 6, 68, 1150, 9706,-15995,-2380,-267,-15,
780 -6, -68,-1150, -9706, 15995, 2380, 267, 15, 307, 483, 289,
781 0, 80, 580, 719, 25901, 3224, 726, 62,
782 6, 54, 1060, 9343,-17072,-2565,-299,-17,
783 -6, -54,-1060, -9343, 17072, 2565, 299, 17, 274, 569, 304,
784 -1, 82, 555, 391, 26339, 3395, 735, 61,
785 5, 40, 963, 8930,-18131,-2747,-332,-19,
786 -5, -40, -963, -8930, 18131, 2747, 332, 19, 242, 650, 307,
787 -1, 83, 523, 26, 26672, 3545, 740, 60,
788 5, 27, 861, 8474,-19164,-2923,-366,-21,
789 -5, -27, -861, -8474, 19164, 2923, 366, 21, 212, 724, 300,
790 -1, 83, 482, -376, 26900, 3672, 739, 58,
791 4, 14, 756, 7981,-20163,-3092,-401,-24,
792 -4, -14, -756, -7981, 20163, 3092, 401, 24, 183, 792, 283,
793 -1, 82, 433, -812, 27022, 3776, 735, 56,
794 4, 1, 648, 7456,-21122,-3250,-435,-26,
795 -4, -1, -648, -7456, 21122, 3250, 435, 26, 155, 851, 258,
796 -1, 81, 376, -1281, 27038, 3855, 726, 54,
797 3, -11, 539, 6907,-22032,-3397,-470,-28,
798 -3, 11, -539, -6907, 22032, 3397, 470, 28, 128, 903, 226,
799 -1, 78, 312, -1778, 26951, 3910, 713, 52,
800 3, -22, 430, 6338,-22887,-3530,-503,-31,
801 -3, 22, -430, -6338, 22887, 3530, 503, 31, 102, 946, 188,
802 -2, 75, 239, -2302, 26761, 3941, 696, 49,
803 3, -33, 322, 5757,-23678,-3648,-537,-34,
804 -3, 33, -322, -5757, 23678, 3648, 537, 34, 76, 980, 145,
805 -2, 70, 160, -2848, 26472, 3948, 676, 47,
806 3, -42, 217, 5167,-24399,-3749,-568,-36,
807 -3, 42, -217, -5167, 24399, 3749, 568, 36, 50, 1004, 99,
808 -2, 65, 74, -3412, 26087, 3931, 653, 44,
809 2, -51, 115, 4577,-25045,-3830,-599,-39,
810 -2, 51, -115, -4577, 25045, 3830, 599, 39, 25, 1019, 50,
812 25610,3891,627,42,-3990,-18,58,-2,
813 21226,-21226,10604,-10604,1860,-1860,1458,-1458,576,-576,130,-130,60,-60,8,-8
816 static const int win_const[18][4] = {
817 { -3072, -134, -146, 3352 },
818 { -2747, -362, -471, 3579 },
819 { -2387, -529, -831, 3747 },
820 { -2004, -632,-1214, 3850 },
821 { -1609, -666,-1609, 3884 },
822 { -1214, -632,-2004, 3850 },
823 { -831, -529,-2387, 3747 },
824 { -471, -362,-2747, 3579 },
825 { -146, -134,-3072, 3352 },
826 { 134,-3072,-3352, -146 },
827 { 362,-2747,-3579, -471 },
828 { 529,-2387,-3747, -831 },
829 { 632,-2004,-3850,-1214 },
830 { 666,-1609,-3884,-1609 },
831 { 632,-1214,-3850,-2004 },
832 { 529, -831,-3747,-2387 },
833 { 362, -471,-3579,-2747 },
834 { 134, -146,-3352,-3072 } };
836 /* forward declarations */
837 static int HuffmanCode( short *ix, int *xr, uint32_t begin, uint32_t end, int table);
838 static int HuffmanCod1( short *ix, int *xr, uint32_t begin, uint32_t end, int table);
839 static void putbits(uint32_t val, uint32_t nbit);
840 static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
841 uint32_t len, int *bits);
842 static int find_best_3( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
843 uint32_t len, int *bits);
844 static int count_bit1 ( short *ix, uint32_t start, uint32_t end, int *bits );
845 static int count_bigv ( short *ix, uint32_t start, uint32_t end, int table0, int table1,
846 int *bits);
849 static void encodeSideInfo( side_info_t si[2][2] )
851 int gr, ch, header;
852 uint32_t cc=0, sz=0;
855 * MPEG header layout:
856 * AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
857 * A (31-21) = frame sync
858 * B (20-19) = MPEG type
859 * C (18-17) = MPEG layer
860 * D (16) = protection bit
861 * E (15-12) = bitrate index
862 * F (11-10) = samplerate index
863 * G (9) = padding bit
864 * H (8) = private bit
865 * I (7-6) = channel mode
866 * J (5-4) = mode extension (jstereo only)
867 * K (3) = copyright bit
868 * L (2) = original
869 * M (1-0) = emphasis
872 header = (0xfff00000) | /* frame sync (AAAAAAAAA AAA)
873 mp3 type (upper): 1 (B) */
874 (0x01 << 17) | /* mp3 layer: 01 (CC) */
875 ( 0x1 << 16) | /* mp3 crc: 1 (D) */
876 ( 0x1 << 2); /* mp3 org: 1 (L) */
877 header |= cfg.mpg.type << 19;
878 header |= cfg.mpg.bitr_id << 12;
879 header |= cfg.mpg.smpl_id << 10;
880 header |= cfg.mpg.padding << 9;
881 header |= cfg.mpg.mode << 6;
882 /* no emphasis (bits 0-1) */
883 putbits( header, 32 );
885 if(cfg.mpg.type == 1)
886 { /* MPEG1 */
887 if(cfg.channels == 2) { putlong( 0, 20); }
888 else { putlong( 0, 18); }
890 for(gr=0; gr<cfg.granules; gr++)
891 for(ch=0; ch<cfg.channels; ch++)
893 side_info_t *gi = &si[gr][ch];
895 putlong( gi->part2_3_length, 12 );
896 putlong( gi->address3>>1, 9 );
897 putlong( gi->global_gain, 8 );
898 putlong( gi->table_select[0], 10 );
899 putlong( gi->table_select[1], 5 );
900 putlong( gi->table_select[2], 5 );
901 putlong( gi->region_0_1, 7 );
902 putlong( gi->table_select[3], 3 );
905 else
906 { /* MPEG2 */
907 if(cfg.channels == 2) { putlong( 0, 10); }
908 else { putlong( 0, 9); }
910 for(ch=0; ch<cfg.channels; ch++)
912 side_info_t *gi = &si[0][ch];
914 putlong( gi->part2_3_length, 12);
915 putlong( gi->address3>>1, 9);
916 putlong( gi->global_gain, 8);
917 putlong( gi->table_select[0], 15);
918 putlong( gi->table_select[1], 5);
919 putlong( gi->table_select[2], 5);
920 putlong( gi->region_0_1, 7);
921 putlong( gi->table_select[3], 2);
924 /* flush remaining bits */
925 putbits(cc, sz);
928 /* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS,
929 as well as the definitions of the side information on pages 26 and 27. */
930 static void Huffmancodebits( short *ix, int *xr, side_info_t *gi )
932 int region1 = gi->address1;
933 int region2 = gi->address2;
934 int bigvals = gi->address3;
935 int count1 = bigvals + (gi->count1 << 2);
936 int stuffBits = 0;
937 int bits = 0;
939 if(region1 > 0)
940 bits += HuffmanCode(ix, xr, 0 , region1, gi->table_select[0]);
942 if(region2 > region1)
943 bits += HuffmanCode(ix, xr, region1, region2, gi->table_select[1]);
945 if(bigvals > region2)
946 bits += HuffmanCode(ix, xr, region2, bigvals, gi->table_select[2]);
948 if(count1 > bigvals)
949 bits += HuffmanCod1(ix, xr, bigvals, count1, gi->table_select[3]);
951 if((stuffBits = gi->part2_3_length - bits) > 0)
953 int stuffWords = stuffBits >> 5;
954 int remainBits = stuffBits & 31;
956 if( remainBits )
957 putbits( ~0, remainBits );
959 while( stuffWords-- )
960 putbits( ~0, 32 ); /* Huffman code tables leed to padding ones */
964 int HuffmanCod1( short *ix, int *xr, uint32_t begin, uint32_t end, int tbl)
966 uint32_t cc=0, sz=0;
967 uint32_t i, d, p;
968 int sumbit=0, s=0, l=0, v, w, x, y;
969 #define sgnv (xr[i+0] < 0 ? 1 : 0)
970 #define sgnw (xr[i+1] < 0 ? 1 : 0)
971 #define sgnx (xr[i+2] < 0 ? 1 : 0)
972 #define sgny (xr[i+3] < 0 ? 1 : 0)
974 for(i=begin; i<end; i+=4)
976 v = ix[i+0];
977 w = ix[i+1];
978 x = ix[i+2];
979 y = ix[i+3];
980 p = (v << 3) + (w << 2) + (x << 1) + y;
982 switch(p)
984 case 0: l=0; s = 0; break;
985 case 1: l=1; s = sgnv; break;
986 case 2: l=1; s = sgnw; break;
987 case 3: l=2; s = (sgnv << 1) + sgnw; break;
988 case 4: l=1; s = sgnx; break;
989 case 5: l=2; s = (sgnv << 1) + sgnx; break;
990 case 6: l=2; s = (sgnw << 1) + sgnx; break;
991 case 7: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break;
992 case 8: l=1; s = sgny; break;
993 case 9: l=2; s = (sgnv << 1) + sgny; break;
994 case 10: l=2; s = (sgnw << 1) + sgny; break;
995 case 11: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break;
996 case 12: l=2; s = (sgnx << 1) + sgny; break;
997 case 13: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break;
998 case 14: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break;
999 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break;
1002 d = (ht_count[tbl][0][p] << l) + s;
1003 l = ht_count[tbl][1][p];
1004 putlong( d, l );
1005 sumbit += l;
1008 /* flush remaining bits */
1009 putbits(cc, sz);
1011 return sumbit;
1014 /* Implements the pseudocode of page 98 of the IS */
1015 int HuffmanCode( short *ix, int *xr, uint32_t begin, uint32_t end, int table)
1017 uint32_t cc=0, sz=0, code;
1018 uint32_t i, xl=0, yl=0, idx;
1019 int x, y, bit, sumbit=0;
1020 #define sign_x (xr[i+0] < 0 ? 1 : 0)
1021 #define sign_y (xr[i+1] < 0 ? 1 : 0)
1023 if(table == 0)
1024 return 0;
1026 if( table > 15 )
1027 { /* ESC-table is used */
1028 uint32_t linbits = ht_big[table-16].linbits;
1029 uint16_t *hffcode = table < 24 ? t16HB : t24HB;
1030 uint8_t *hlen = table < 24 ? t16l : t24l;
1032 for(i=begin; i<end; i+=2)
1034 x = ix[ i ];
1035 y = ix[i+1];
1037 if(x > 14) { xl = x - 15; x = 15; }
1038 if(y > 14) { yl = y - 15; y = 15; }
1040 idx = x * 16 + y;
1041 code = hffcode[idx];
1042 bit = hlen [idx];
1044 if(x)
1046 if(x > 14)
1048 code = (code << linbits) | xl;
1049 bit += linbits;
1052 code = (code << 1) | sign_x;
1053 bit += 1;
1056 if(y)
1058 if(y > 14)
1060 code = (code << linbits) | yl;
1061 bit += linbits;
1064 code = (code << 1) | sign_y;
1065 bit += 1;
1068 putlong( code, bit );
1069 sumbit += bit;
1072 else
1073 { /* No ESC-words */
1074 const struct huffcodetab *h = &ht[table];
1076 for(i=begin; i<end; i+=2)
1078 x = ix[i];
1079 y = ix[i+1];
1081 idx = x * h->len + y;
1082 code = h->table[idx];
1083 bit = h->hlen [idx];
1085 if(x)
1087 code = (code << 1) | sign_x;
1088 bit += 1;
1091 if(y)
1093 code = (code << 1) | sign_y;
1094 bit += 1;
1097 putlong( code, bit );
1098 sumbit += bit;
1102 /* flush remaining bits */
1103 putbits(cc, sz);
1105 return sumbit;
1108 void putbits(uint32_t val, uint32_t nbit)
1110 int new_bitpos = CodedData.bitpos + nbit;
1111 int ptrpos = CodedData.bitpos >> 5;
1113 val = val & (0xffffffff >> (32 - nbit));
1115 /* data fit in one uint32_t */
1116 if(((new_bitpos - 1) >> 5) == ptrpos)
1117 CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31);
1118 else
1120 CodedData.bbuf[ptrpos ] |= val >> ((new_bitpos - 32) & 31);
1121 CodedData.bbuf[ptrpos+1] |= val << ((32 - new_bitpos) & 31);
1124 CodedData.bitpos = new_bitpos;
1127 /***************************************************************************/
1128 /* Choose the Huffman table that will encode ix[begin..end] with */
1129 /* the fewest bits. */
1130 /* Note: This code contains knowledge about the sizes and characteristic */
1131 /* of the Huffman tables as defined in the IS (Table B.7), and will not */
1132 /* work with any arbitrary tables. */
1133 /***************************************************************************/
1134 int choose_table( short *ix, uint32_t begin, uint32_t end, int *bits )
1136 uint32_t i;
1137 int max, table0, table1;
1139 for(i=begin,max=0; i<end; i++)
1140 if(ix[i] > max)
1141 max = ix[i];
1143 if(max < 16)
1145 /* tables without linbits */
1146 /* indx: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1147 /* len: 0, 2, 3, 3, 0, 4, 4, 6, 6, 6, 8, 8, 8, 16, 0, 16 */
1148 switch(max)
1150 case 0: return 0;
1151 case 1: return count_bit1(ix, begin, end, bits);
1152 case 2: return 2 + find_best_2(ix, begin, end, tab23, 3, bits);
1153 case 3: return 5 + find_best_2(ix, begin, end, tab56, 4, bits);
1154 case 4:
1155 case 5: return 7 + find_best_3(ix, begin, end, tab789, 6, bits);
1156 case 6:
1157 case 7: return 10 + find_best_3(ix, begin, end, tabABC, 8, bits);
1158 default: return 13 + find_best_2(ix, begin, end, tab1315, 16, bits) * 2;
1161 else
1163 /* tables with linbits */
1164 max -= 15;
1166 for(table0=0; table0<8; table0++)
1167 if(ht_big[table0].linmax >= max)
1168 break;
1170 for(table1=8; table1<16; table1++)
1171 if(ht_big[table1].linmax >= max)
1172 break;
1174 return 16 + count_bigv(ix, begin, end, table0, table1, bits);
1178 int find_best_2(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1179 uint32_t len, int *bits)
1181 uint32_t i, sum = 0;
1183 for(i=start; i<end; i+=2)
1184 sum += table[ix[i] * len + ix[i+1]];
1186 if((sum & 0xffff) <= (sum >> 16))
1188 *bits = (sum & 0xffff);
1189 return 1;
1191 else
1193 *bits = sum >> 16;
1194 return 0;
1198 int find_best_3(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1199 uint32_t len, int *bits)
1201 uint32_t i, j, sum = 0;
1202 int sum1 = 0;
1203 int sum2 = 0;
1204 int sum3 = 0;
1206 /* avoid overflow in packed additions: 78*13 < 1024 */
1207 for(i=start; i<end; )
1209 j = i + 2*78 > end ? end : i + 2*78;
1211 for(sum=0; i<j; i+=2)
1212 sum += table[ix[i] * len + ix[i+1]];
1214 sum1 += (sum >> 20);
1215 sum2 += (sum >> 10) & 0x3ff;
1216 sum3 += (sum >> 0) & 0x3ff;
1219 i = 0;
1220 if(sum1 > sum2) { sum1 = sum2; i = 1; }
1221 if(sum1 > sum3) { sum1 = sum3; i = 2; }
1223 *bits = sum1;
1225 return i;
1228 /*************************************************************************/
1229 /* Function: Count the number of bits necessary to code the subregion. */
1230 /*************************************************************************/
1231 int count_bit1(short *ix, uint32_t start, uint32_t end, int *bits )
1233 uint32_t i, sum = 0;
1235 for(i=start; i<end; i+=2)
1236 sum += t1l[4 + ix[i] * 2 + ix[i+1]];
1238 *bits = sum;
1240 return 1; /* this is table1 */
1243 int count_bigv(short *ix, uint32_t start, uint32_t end, int table0,
1244 int table1, int *bits )
1246 uint32_t i, sum0, sum1, sum=0, bigv=0, x, y;
1248 /* ESC-table is used */
1249 for(i=start; i<end; i+=2)
1251 x = ix[i];
1252 y = ix[i+1];
1254 if(x > 14) { x = 15; bigv++; }
1255 if(y > 14) { y = 15; bigv++; }
1257 sum += tab1624[x * 16 + y];
1260 sum0 = (sum >> 16) + bigv * ht_big[table0].linbits;
1261 sum1 = (sum & 0xffff) + bigv * ht_big[table1].linbits;
1263 if(sum0 <= sum1)
1265 *bits = sum0;
1266 return table0;
1268 else
1270 *bits = sum1;
1271 return table1;
1275 /*************************************************************************/
1276 /* Function: Calculation of rzero, count1, address3 */
1277 /* (Partitions ix into big values, quadruples and zeros). */
1278 /*************************************************************************/
1279 int calc_runlen( short *ix, side_info_t *si )
1281 int p, i, sum = 0;
1283 for(i=SAMPL2; i-=2; )
1284 if(*(uint32_t*)&ix[i-2]) /* !!!! short *ix; !!!!! */
1285 break;
1287 si->count1 = 0;
1289 for( ; i>3; i-=4)
1291 int v = ix[i-1];
1292 int w = ix[i-2];
1293 int x = ix[i-3];
1294 int y = ix[i-4];
1296 if((v | w | x | y) <= 1)
1298 p = (y<<3) + (x<<2) + (w<<1) + (v);
1300 sum += tab01[p];
1302 si->count1++;
1304 else break;
1307 si->address3 = i;
1309 if((sum >> 16) < (sum & 0xffff))
1311 si->table_select[3] = 0;
1312 return sum >> 16;
1314 else
1316 si->table_select[3] = 1;
1317 return sum & 0xffff;
1322 /*************************************************************************/
1323 /* Function: Quantization of the vector xr ( -> ix) */
1324 /*************************************************************************/
1325 int quantize_int(int *xr, short *ix, side_info_t *si)
1327 int i, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 };
1329 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4;
1331 /* check for integer overflow */
1332 if(((si->max_val + 256) >> 8) * s >= (1622 << 8))
1333 return 0;
1335 for(i=SAMPL2; i--; )
1336 ix[i] = int2idx[(abs(xr[i]) * s + 0x8000) >> 16];
1338 return 1;
1341 /*************************************************************************/
1342 /* subdivides the bigvalue region which will use separate Huffman tables */
1343 /*************************************************************************/
1344 void subdivide(side_info_t *si)
1346 int scfb, count0, count1;
1348 if( !si->address3 )
1349 { /* no bigvalue region */
1350 si->region_0_1 = 0;
1351 si->address1 = 0;
1352 si->address2 = 0;
1354 else
1356 /* Calculate scale factor band index */
1357 for(scfb=0; scalefac[scfb] < si->address3; )
1358 scfb++;
1360 count0 = subdv_table[scfb].region0_cnt;
1361 count1 = subdv_table[scfb].region1_cnt;
1363 si->region_0_1 = (count0 << 3) | count1;
1364 si->address1 = scalefac[count0 + 1];
1365 si->address2 = scalefac[count0 + 1 + count1 + 1];
1369 /*******************************************************************/
1370 /* Count the number of bits necessary to code the bigvalues region */
1371 /*******************************************************************/
1372 int bigv_bitcount(short *ix, side_info_t *gi)
1374 int b1=0, b2=0, b3=0;
1376 /* Select huffman code tables for bigvalues regions */
1377 gi->table_select[0] = 0;
1378 gi->table_select[1] = 0;
1379 gi->table_select[2] = 0;
1381 if( gi->address1 > 0 ) /* region0 */
1382 gi->table_select[0] = choose_table(ix, 0 , gi->address1, &b1);
1384 if( gi->address2 > gi->address1 ) /* region1 */
1385 gi->table_select[1] = choose_table(ix, gi->address1, gi->address2, &b2);
1387 if( gi->address3 > gi->address2 ) /* region2 */
1388 gi->table_select[2] = choose_table(ix, gi->address2, gi->address3, &b3);
1390 return b1+b2+b3;
1393 int quantize_and_count_bits(int *xr, short *ix, side_info_t *si)
1395 int bits = 10000;
1397 if(quantize_int(xr, ix, si))
1399 bits = calc_runlen(ix, si); /* rzero,count1,address3 */
1400 subdivide(si); /* bigvalues sfb division */
1401 bits += bigv_bitcount(ix,si); /* bit count */
1404 return bits;
1407 /***********************************************************************/
1408 /* The code selects the best quantStep for a particular set of scalefacs */
1409 /***********************************************************************/
1410 int inner_loop(int *xr, int max_bits, side_info_t *si)
1412 int bits;
1414 while((bits=quantize_and_count_bits(xr, enc_data, si)) < max_bits-64)
1416 if(si->quantStep == 0)
1417 break;
1419 if(si->quantStep <= 2)
1420 si->quantStep = 0;
1421 else
1422 si->quantStep -= 2;
1425 while(bits > max_bits)
1427 si->quantStep++;
1428 bits = quantize_and_count_bits(xr, enc_data, si);
1431 return bits;
1434 void iteration_loop(int *xr, side_info_t *si, int gr_cnt)
1436 int remain, tar_bits, max_bits = cfg.mean_bits;
1438 /* distribute reserved bits to remaining granules */
1439 tar_bits = max_bits + (cfg.ResvSize / gr_cnt & ~7);
1440 if(tar_bits > max_bits + max_bits/2)
1441 tar_bits = max_bits + max_bits/2;
1443 si->part2_3_length = inner_loop(xr, tar_bits, si);
1444 si->global_gain = si->quantStep + 142 - si->additStep;
1446 /* unused bits of the reservoir can be used for remaining granules */
1447 cfg.ResvSize += max_bits - si->part2_3_length;
1449 /* end: distribute the reserved bits to one or two granules */
1450 if(gr_cnt == 1)
1452 si->part2_3_length += cfg.ResvSize;
1453 /* mp3 format allows max 12bits for granule length */
1454 if(si->part2_3_length > 4092)
1456 remain = (si->part2_3_length - 4092 + 31) >> 5;
1457 si->part2_3_length -= remain << 5;
1458 si[-1].part2_3_length += remain << 5;
1460 while(remain--)
1461 putbits(~0, 32);
1467 /* returns sum_j=0^31 a[j]*cos(PI*j*(k+1/2)/32), 0<=k<32 */
1468 void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT]) ICODE_ATTR;
1469 void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT])
1471 int k, i, u, v;
1472 short *wp, *x1, *x2;
1474 #ifdef CPU_COLDFIRE
1475 int s0, s1, t0, t1;
1477 for(k=0; k<18; k++, wk+=64, sb0+=SBLIMIT, sb1+=SBLIMIT)
1479 wp = enwindow;
1480 x1 = wk;
1481 x2 = x1 - 124;
1483 for(i=-15; i<0; i++)
1485 asm volatile(
1486 "move.l (-224*4,%[x2]), %%d4\n" /* d4 = x2[-224] */
1487 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1488 "mac.w %%d0u, %%d4u, %%acc0\n"
1489 "mac.w %%d0u, %%d4l, (-160*4,%[x2]), %%d4, %%acc1\n"
1490 "mac.w %%d0l, %%d4u, %%acc0\n"
1491 "mac.w %%d0l, %%d4l, ( -96*4,%[x2]), %%d4, %%acc1\n"
1492 "mac.w %%d1u, %%d4u, %%acc0\n"
1493 "mac.w %%d1u, %%d4l, ( -32*4,%[x2]), %%d4, %%acc1\n"
1494 "mac.w %%d1l, %%d4u, %%acc0\n"
1495 "mac.w %%d1l, %%d4l, ( 32*4,%[x2]), %%d4, %%acc1\n"
1496 "mac.w %%d2u, %%d4u, %%acc0\n"
1497 "mac.w %%d2u, %%d4l, ( 96*4,%[x2]), %%d4, %%acc1\n"
1498 "mac.w %%d2l, %%d4u, %%acc0\n"
1499 "mac.w %%d2l, %%d4l, ( 160*4,%[x2]), %%d4, %%acc1\n"
1500 "mac.w %%d3u, %%d4u, %%acc0\n"
1501 "mac.w %%d3u, %%d4l, ( 224*4,%[x2]), %%d4, %%acc1\n"
1502 "mac.w %%d3l, %%d4u, %%acc0\n"
1503 "mac.w %%d3l, %%d4l, (-256*4,%[x1]), %%d4, %%acc1\n"
1504 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */
1505 "mac.w %%d0u, %%d4u, %%acc0\n"
1506 "mac.w %%d0u, %%d4l, (-192*4,%[x1]), %%d4, %%acc1\n"
1507 "mac.w %%d0l, %%d4u, %%acc0\n"
1508 "mac.w %%d0l, %%d4l, (-128*4,%[x1]), %%d4, %%acc1\n"
1509 "mac.w %%d1u, %%d4u, %%acc0\n"
1510 "mac.w %%d1u, %%d4l, ( -64*4,%[x1]), %%d4, %%acc1\n"
1511 "mac.w %%d1l, %%d4u, %%acc0\n"
1512 "mac.w %%d1l, %%d4l, ( 0*4,%[x1]), %%d4, %%acc1\n"
1513 "mac.w %%d2u, %%d4u, %%acc0\n"
1514 "mac.w %%d2u, %%d4l, ( 64*4,%[x1]), %%d4, %%acc1\n"
1515 "mac.w %%d2l, %%d4u, %%acc0\n"
1516 "mac.w %%d2l, %%d4l, ( 128*4,%[x1]), %%d4, %%acc1\n"
1517 "mac.w %%d3u, %%d4u, %%acc0\n"
1518 "mac.w %%d3u, %%d4l, ( 192*4,%[x1]), %%d4, %%acc1\n"
1519 "mac.w %%d3l, %%d4u, %%acc0\n"
1520 "mac.w %%d3l, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n"
1521 "movclr.l %%acc0, %%d0\n"
1522 "move.l %%d0, %[s0]\n"
1523 "movclr.l %%acc1, %%d0\n"
1524 "move.l %%d0, %[s1]\n"
1526 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1527 "mac.w %%d0u, %%d4u, %%acc0\n"
1528 "mac.w %%d0u, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n"
1529 "mac.w %%d0l, %%d4u, %%acc0\n"
1530 "mac.w %%d0l, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n"
1531 "mac.w %%d1u, %%d4u, %%acc0\n"
1532 "mac.w %%d1u, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n"
1533 "mac.w %%d1l, %%d4u, %%acc0\n"
1534 "mac.w %%d1l, %%d4l, ( -32*4,%[x1]), %%d4, %%acc1\n"
1535 "mac.w %%d2u, %%d4u, %%acc0\n"
1536 "mac.w %%d2u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n"
1537 "mac.w %%d2l, %%d4u, %%acc0\n"
1538 "mac.w %%d2l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n"
1539 "mac.w %%d3u, %%d4u, %%acc0\n"
1540 "mac.w %%d3u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n"
1541 "mac.w %%d3l, %%d4u, %%acc0\n"
1542 "mac.w %%d3l, %%d4l, ( 256*4,%[x2]), %%d4, %%acc1\n"
1543 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */
1544 "mac.w %%d0u, %%d4u, %%acc0\n"
1545 "mac.w %%d0u, %%d4l, ( 192*4,%[x2]), %%d4, %%acc1\n"
1546 "mac.w %%d0l, %%d4u, %%acc0\n"
1547 "mac.w %%d0l, %%d4l, ( 128*4,%[x2]), %%d4, %%acc1\n"
1548 "mac.w %%d1u, %%d4u, %%acc0\n"
1549 "mac.w %%d1u, %%d4l, ( 64*4,%[x2]), %%d4, %%acc1\n"
1550 "mac.w %%d1l, %%d4u, %%acc0\n"
1551 "mac.w %%d1l, %%d4l, ( 0*4,%[x2]), %%d4, %%acc1\n"
1552 "mac.w %%d2u, %%d4u, %%acc0\n"
1553 "mac.w %%d2u, %%d4l, ( -64*4,%[x2]), %%d4, %%acc1\n"
1554 "mac.w %%d2l, %%d4u, %%acc0\n"
1555 "mac.w %%d2l, %%d4l, (-128*4,%[x2]), %%d4, %%acc1\n"
1556 "mac.w %%d3u, %%d4u, %%acc0\n"
1557 "mac.w %%d3u, %%d4l, (-192*4,%[x2]), %%d4, %%acc1\n"
1558 "mac.w %%d3l, %%d4u, %%acc0\n"
1559 "mac.w %%d3l, %%d4l, %%acc1\n"
1560 "movclr.l %%acc0, %%d0\n"
1561 "move.l %%d0, %[t0]\n"
1562 "movclr.l %%acc1, %%d0\n"
1563 "move.l %%d0, %[t1]\n"
1565 : [x1] "+a" (x1), [x2] "+a" (x2), [s0] "+m" (s0), [t0] "+m" (t0),
1566 [s1] "+m" (s1), [t1] "+m" (t1)
1567 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4");
1569 sb0[30+i*2] = shft4(t0) + shft13(s0) * wp[24];
1570 sb0[31+i*2] = shft13(t0) * wp[25] - shft13(s0) * wp[26];
1571 sb1[30+i*2] = shft4(t1) + shft13(s1) * wp[24];
1572 sb1[31+i*2] = shft13(t1) * wp[25] - shft13(s1) * wp[26];
1573 wp += 27;
1574 x1 -= 2;
1575 x2 += 2;
1578 asm volatile(
1579 "move.l ( -32*4,%[x1]), %%d4\n" /* d4 = x1[-32] */
1580 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1582 "mac.w %%d0u, %%d4u, %%acc0\n"
1583 "mac.w %%d0u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n"
1584 "mac.w %%d0l, %%d4u, %%acc0\n"
1585 "mac.w %%d0l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n"
1586 "mac.w %%d1u, %%d4u, %%acc0\n"
1587 "mac.w %%d1u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n"
1588 "mac.w %%d1l, %%d4u, %%acc0\n"
1589 "mac.w %%d1l, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n"
1590 "mac.w %%d2u, %%d4u, %%acc0\n"
1591 "mac.w %%d2u, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n"
1592 "mac.w %%d2l, %%d4u, %%acc0\n"
1593 "mac.w %%d2l, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n"
1594 "mac.w %%d3u, %%d4u, %%acc0\n"
1595 "mac.w %%d3u, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n"
1596 "mac.w %%d3l, %%d4u, %%acc0\n"
1597 "mac.w %%d3l, %%d4l, ( -16*4,%[x1]), %%d4, %%acc1\n"
1598 "movclr.l %%acc0, %%d0\n"
1599 "move.l %%d0, %[s0]\n"
1600 "movclr.l %%acc1, %%d0\n"
1601 "move.l %%d0, %[s1]\n"
1603 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */
1604 "mac.w %%d0u, %%d4u, %%acc0\n"
1605 "mac.w %%d0u, %%d4l, ( -48*4,%[x1]), %%d4, %%acc1\n"
1606 "mac.w %%d1u, %%d4u, %%acc0\n"
1607 "mac.w %%d1u, %%d4l, ( 16*4,%[x1]), %%d4, %%acc1\n"
1608 "mac.w %%d1l, %%d4u, %%acc0\n"
1609 "mac.w %%d1l, %%d4l, ( -80*4,%[x1]), %%d4, %%acc1\n"
1610 "mac.w %%d2u, %%d4u, %%acc0\n"
1611 "mac.w %%d2u, %%d4l, ( 48*4,%[x1]), %%d4, %%acc1\n"
1612 "mac.w %%d2u, %%d4u, %%acc0\n"
1613 "mac.w %%d2u, %%d4l, (-112*4,%[x1]), %%d4, %%acc1\n"
1614 "mac.w %%d3u, %%d4u, %%acc0\n"
1615 "mac.w %%d3u, %%d4l, ( 80*4,%[x1]), %%d4, %%acc1\n"
1616 "mac.w %%d3l, %%d4u, %%acc0\n"
1617 "mac.w %%d3l, %%d4l, (-144*4,%[x1]), %%d4, %%acc1\n"
1618 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */
1619 "mac.w %%d0u, %%d4u, %%acc0\n"
1620 "mac.w %%d0u, %%d4l, ( 112*4,%[x1]), %%d4, %%acc1\n"
1621 "mac.w %%d0u, %%d4u, %%acc0\n"
1622 "mac.w %%d0u, %%d4l, (-176*4,%[x1]), %%d4, %%acc1\n"
1623 "mac.w %%d1u, %%d4u, %%acc0\n"
1624 "mac.w %%d1u, %%d4l, ( 144*4,%[x1]), %%d4, %%acc1\n"
1625 "mac.w %%d1l, %%d4u, %%acc0\n"
1626 "mac.w %%d1l, %%d4l, (-208*4,%[x1]), %%d4, %%acc1\n"
1627 "mac.w %%d2u, %%d4u, %%acc0\n"
1628 "mac.w %%d2u, %%d4l, ( 176*4,%[x1]), %%d4, %%acc1\n"
1629 "mac.w %%d2u, %%d4u, %%acc0\n"
1630 "mac.w %%d2u, %%d4l, (-240*4,%[x1]), %%d4, %%acc1\n"
1631 "mac.w %%d3u, %%d4u, %%acc0\n"
1632 "mac.w %%d3u, %%d4l, ( 208*4,%[x1]), %%d4, %%acc1\n"
1633 "mac.w %%d3l, %%d4u, %%acc0\n"
1634 "mac.w %%d3l, %%d4l, %%acc1\n"
1635 "movclr.l %%acc0, %%d0\n"
1636 "move.l %%d0, %[t0]\n"
1637 "movclr.l %%acc1, %%d0\n"
1638 "move.l %%d0, %[t1]\n"
1640 : [x1] "+a" (x1), [s0] "+m" (s0), [t0] "+m" (t0),
1641 [s1] "+m" (s1), [t1] "+m" (t1)
1642 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4");
1644 u = shft4(s0 - t0);
1645 v = shft4(s0 + t0);
1646 t0 = sb0[14];
1647 s0 = sb0[15] - t0;
1649 sb0[31] = v + t0; /* A0 */
1650 sb0[30] = u + s0; /* A1 */
1651 sb0[15] = u - s0; /* A2 */
1652 sb0[14] = v - t0; /* A3 */
1654 u = shft4(s1 - t1);
1655 v = shft4(s1 + t1);
1656 t1 = sb1[14];
1657 s1 = sb1[15] - t1;
1659 sb1[31] = v + t1; /* A0 */
1660 sb1[30] = u + s1; /* A1 */
1661 sb1[15] = u - s1; /* A2 */
1662 sb1[14] = v - t1; /* A3 */
1664 #else
1665 int ch, s, t, *a;
1667 for(ch=0; ch<cfg.channels; ch++)
1669 a = ch ? sb1 : sb0;
1670 for(k=0; k<18; k++, wk+=64, a+=SBLIMIT)
1672 wp = enwindow;
1673 x1 = wk;
1674 x2 = x1 - 124;
1676 /* x1[-572] .... x1[448] = 1022 */
1677 /* 18*4*16*32 */
1678 for(i=-15; i<0; i++)
1680 s = (int)x2[-224*2] * wp[ 0]; t = (int)x1[ 224*2] * wp[ 0];
1681 s += (int)x2[-160*2] * wp[ 1]; t += (int)x1[ 160*2] * wp[ 1];
1682 s += (int)x2[- 96*2] * wp[ 2]; t += (int)x1[ 96*2] * wp[ 2];
1683 s += (int)x2[- 32*2] * wp[ 3]; t += (int)x1[ 32*2] * wp[ 3];
1684 s += (int)x2[ 32*2] * wp[ 4]; t += (int)x1[- 32*2] * wp[ 4];
1685 s += (int)x2[ 96*2] * wp[ 5]; t += (int)x1[- 96*2] * wp[ 5];
1686 s += (int)x2[ 160*2] * wp[ 6]; t += (int)x1[-160*2] * wp[ 6];
1687 s += (int)x2[ 224*2] * wp[ 7]; t += (int)x1[-224*2] * wp[ 7];
1688 s += (int)x1[-256*2] * wp[ 8]; t += (int)x2[ 256*2] * wp[16];
1689 s += (int)x1[-192*2] * wp[ 9]; t += (int)x2[ 192*2] * wp[17];
1690 s += (int)x1[-128*2] * wp[10]; t += (int)x2[ 128*2] * wp[18];
1691 s += (int)x1[- 64*2] * wp[11]; t += (int)x2[ 64*2] * wp[19];
1692 s += (int)x1[ 0*2] * wp[12]; t += (int)x2[ 0*2] * wp[20];
1693 s += (int)x1[ 64*2] * wp[13]; t += (int)x2[- 64*2] * wp[21];
1694 s += (int)x1[ 128*2] * wp[14]; t += (int)x2[-128*2] * wp[22];
1695 s += (int)x1[ 192*2] * wp[15]; t += (int)x2[-192*2] * wp[23];
1697 a[30+i*2] = shft4(t) + shft13(s) * wp[24];
1698 a[31+i*2] = shft13(t) * wp[25] - shft13(s) * wp[26];
1699 wp += 27;
1700 x1 -= 2;
1701 x2 += 2;
1704 t = (int)x1[- 16*2] * wp[ 8]; s = (int)x1[ -32*2] * wp[0];
1705 t += ((int)x1[- 48*2]-x1[ 16*2]) * wp[10]; s += (int)x1[ -96*2] * wp[1];
1706 t += ((int)x1[- 80*2]+x1[ 48*2]) * wp[12]; s += (int)x1[-160*2] * wp[2];
1707 t += ((int)x1[-112*2]-x1[ 80*2]) * wp[14]; s += (int)x1[-224*2] * wp[3];
1708 t += ((int)x1[-144*2]+x1[112*2]) * wp[16]; s += (int)x1[ 32*2] * wp[4];
1709 t += ((int)x1[-176*2]-x1[144*2]) * wp[18]; s += (int)x1[ 96*2] * wp[5];
1710 t += ((int)x1[-208*2]+x1[176*2]) * wp[20]; s += (int)x1[ 160*2] * wp[6];
1711 t += ((int)x1[-240*2]-x1[208*2]) * wp[22]; s += (int)x1[ 224*2] * wp[7];
1713 u = shft4(s - t);
1714 v = shft4(s + t);
1715 t = a[14];
1716 s = a[15] - t;
1718 a[31] = v + t; /* A0 */
1719 a[30] = u + s; /* A1 */
1720 a[15] = u - s; /* A2 */
1721 a[14] = v - t; /* A3 */
1723 wk -= 18 * 64 - 1; /* rewind wk (to next channel start) */
1725 #endif
1728 void window_subband2(short *x1, int a[SBLIMIT]) ICODE_ATTR;
1729 void window_subband2(short *x1, int a[SBLIMIT])
1731 int xr;
1732 short *wp = enwindow;
1733 short *x2 = x1 - 124;
1735 wp += 27 * 15;
1736 x1 -= 2 * 15;
1737 x2 += 2 * 15;
1739 xr = a[28] - a[0]; a[0] += a[28]; a[28] = shft9(xr) * wp[-2*27+25];
1740 xr = a[29] - a[1]; a[1] += a[29]; a[29] = shft9(xr) * wp[-2*27+25];
1741 xr = a[26] - a[2]; a[2] += a[26]; a[26] = shft9(xr) * wp[-4*27+25];
1742 xr = a[27] - a[3]; a[3] += a[27]; a[27] = shft9(xr) * wp[-4*27+25];
1743 xr = a[24] - a[4]; a[4] += a[24]; a[24] = shft9(xr) * wp[-6*27+25];
1744 xr = a[25] - a[5]; a[5] += a[25]; a[25] = shft9(xr) * wp[-6*27+25];
1745 xr = a[22] - a[6]; a[6] += a[22]; a[22] = shft9(xr) * SQRT ;
1746 xr = a[23] - a[7]; a[7] += a[23]; a[23] = shft9(xr) * SQRT - a[7];
1747 a[ 7] -= a[ 6];
1748 a[22] -= a[ 7];
1749 a[23] -= a[22];
1751 xr = a[ 6]; a[ 6] = a[31] - xr; a[31] = a[31] + xr;
1752 xr = a[ 7]; a[ 7] = a[30] - xr; a[30] = a[30] + xr;
1753 xr = a[22]; a[22] = a[15] - xr; a[15] = a[15] + xr;
1754 xr = a[23]; a[23] = a[14] - xr; a[14] = a[14] + xr;
1756 xr = a[20] - a[ 8]; a[ 8] += a[20]; a[20] = shft9(xr) * wp[-10*27+25];
1757 xr = a[21] - a[ 9]; a[ 9] += a[21]; a[21] = shft9(xr) * wp[-10*27+25];
1758 xr = a[18] - a[10]; a[10] += a[18]; a[18] = shft9(xr) * wp[-12*27+25];
1759 xr = a[19] - a[11]; a[11] += a[19]; a[19] = shft9(xr) * wp[-12*27+25];
1760 xr = a[16] - a[12]; a[12] += a[16]; a[16] = shft9(xr) * wp[-14*27+25];
1761 xr = a[17] - a[13]; a[13] += a[17]; a[17] = shft9(xr) * wp[-14*27+25];
1762 xr =-a[20] + a[24]; a[20] += a[24]; a[24] = shft9(xr) * wp[-12*27+25];
1763 xr =-a[21] + a[25]; a[21] += a[25]; a[25] = shft9(xr) * wp[-12*27+25];
1764 xr = a[ 4] - a[ 8]; a[ 4] += a[ 8]; a[ 8] = shft9(xr) * wp[-12*27+25];
1765 xr = a[ 5] - a[ 9]; a[ 5] += a[ 9]; a[ 9] = shft9(xr) * wp[-12*27+25];
1766 xr = a[ 0] - a[12]; a[ 0] += a[12]; a[12] = shft9(xr) * wp[ -4*27+25];
1767 xr = a[ 1] - a[13]; a[ 1] += a[13]; a[13] = shft9(xr) * wp[ -4*27+25];
1768 xr = a[16] - a[28]; a[16] += a[28]; a[28] = shft9(xr) * wp[ -4*27+25];
1769 xr =-a[17] + a[29]; a[17] += a[29]; a[29] = shft9(xr) * wp[ -4*27+25];
1771 xr = SQRT * shft9(a[ 2] - a[10]); a[ 2] += a[10]; a[10] = xr;
1772 xr = SQRT * shft9(a[ 3] - a[11]); a[ 3] += a[11]; a[11] = xr;
1773 xr = SQRT * shft9(a[26] - a[18]); a[18] += a[26]; a[26] = xr - a[18];
1774 xr = SQRT * shft9(a[27] - a[19]); a[19] += a[27]; a[27] = xr - a[19];
1776 xr = a[ 2]; a[19] -= a[ 3]; a[ 3] -= xr; a[ 2] = a[31] - xr; a[31] += xr;
1777 xr = a[ 3]; a[11] -= a[19]; a[18] -= xr; a[ 3] = a[30] - xr; a[30] += xr;
1778 xr = a[18]; a[27] -= a[11]; a[19] -= xr; a[18] = a[15] - xr; a[15] += xr;
1780 xr = a[19]; a[10] -= xr; a[19] = a[14] - xr; a[14] += xr;
1781 xr = a[10]; a[11] -= xr; a[10] = a[23] - xr; a[23] += xr;
1782 xr = a[11]; a[26] -= xr; a[11] = a[22] - xr; a[22] += xr;
1783 xr = a[26]; a[27] -= xr; a[26] = a[ 7] - xr; a[ 7] += xr;
1785 xr = a[27]; a[27] = a[6] - xr; a[6] += xr;
1787 xr = SQRT * shft9(a[ 0] - a[ 4]); a[ 0] += a[ 4]; a[ 4] = xr;
1788 xr = SQRT * shft9(a[ 1] - a[ 5]); a[ 1] += a[ 5]; a[ 5] = xr;
1789 xr = SQRT * shft9(a[16] - a[20]); a[16] += a[20]; a[20] = xr;
1790 xr = SQRT * shft9(a[17] - a[21]); a[17] += a[21]; a[21] = xr;
1791 xr =-SQRT * shft9(a[ 8] - a[12]); a[ 8] += a[12]; a[12] = xr - a[ 8];
1792 xr =-SQRT * shft9(a[ 9] - a[13]); a[ 9] += a[13]; a[13] = xr - a[ 9];
1793 xr =-SQRT * shft9(a[25] - a[29]); a[25] += a[29]; a[29] = xr - a[25];
1794 xr =-SQRT * shft9(a[24] + a[28]); a[24] -= a[28]; a[28] = xr - a[24];
1796 xr = a[24] - a[16]; a[24] = xr;
1797 xr = a[20] - xr; a[20] = xr;
1798 xr = a[28] - xr; a[28] = xr;
1800 xr = a[25] - a[17]; a[25] = xr;
1801 xr = a[21] - xr; a[21] = xr;
1802 xr = a[29] - xr; a[29] = xr;
1804 xr = a[17] - a[1]; a[17] = xr;
1805 xr = a[ 9] - xr; a[ 9] = xr;
1806 xr = a[25] - xr; a[25] = xr;
1807 xr = a[ 5] - xr; a[ 5] = xr;
1808 xr = a[21] - xr; a[21] = xr;
1809 xr = a[13] - xr; a[13] = xr;
1810 xr = a[29] - xr; a[29] = xr;
1812 xr = a[ 1] - a[0]; a[ 1] = xr;
1813 xr = a[16] - xr; a[16] = xr;
1814 xr = a[17] - xr; a[17] = xr;
1815 xr = a[ 8] - xr; a[ 8] = xr;
1816 xr = a[ 9] - xr; a[ 9] = xr;
1817 xr = a[24] - xr; a[24] = xr;
1818 xr = a[25] - xr; a[25] = xr;
1819 xr = a[ 4] - xr; a[ 4] = xr;
1820 xr = a[ 5] - xr; a[ 5] = xr;
1821 xr = a[20] - xr; a[20] = xr;
1822 xr = a[21] - xr; a[21] = xr;
1823 xr = a[12] - xr; a[12] = xr;
1824 xr = a[13] - xr; a[13] = xr;
1825 xr = a[28] - xr; a[28] = xr;
1826 xr = a[29] - xr; a[29] = xr;
1828 xr = a[ 0]; a[ 0] += a[31]; a[31] -= xr;
1829 xr = a[ 1]; a[ 1] += a[30]; a[30] -= xr;
1830 xr = a[16]; a[16] += a[15]; a[15] -= xr;
1831 xr = a[17]; a[17] += a[14]; a[14] -= xr;
1832 xr = a[ 8]; a[ 8] += a[23]; a[23] -= xr;
1833 xr = a[ 9]; a[ 9] += a[22]; a[22] -= xr;
1834 xr = a[24]; a[24] += a[ 7]; a[ 7] -= xr;
1835 xr = a[25]; a[25] += a[ 6]; a[ 6] -= xr;
1836 xr = a[ 4]; a[ 4] += a[27]; a[27] -= xr;
1837 xr = a[ 5]; a[ 5] += a[26]; a[26] -= xr;
1838 xr = a[20]; a[20] += a[11]; a[11] -= xr;
1839 xr = a[21]; a[21] += a[10]; a[10] -= xr;
1840 xr = a[12]; a[12] += a[19]; a[19] -= xr;
1841 xr = a[13]; a[13] += a[18]; a[18] -= xr;
1842 xr = a[28]; a[28] += a[ 3]; a[ 3] -= xr;
1843 xr = a[29]; a[29] += a[ 2]; a[ 2] -= xr;
1846 void mdct_long(int *out, int *in) ICODE_ATTR;
1847 void mdct_long(int *out, int *in)
1849 int ct,st;
1850 int tc1, tc2, tc3, tc4, ts5, ts6, ts7, ts8;
1851 int ts1, ts2, ts3, ts4, tc5, tc6, tc7, tc8;
1853 /* 1,2, 5,6, 9,10, 13,14, 17 */
1854 tc1 = in[17] - in[ 9];
1855 tc3 = in[15] - in[11];
1856 tc4 = in[14] - in[12];
1857 ts5 = in[ 0] + in[ 8];
1858 ts6 = in[ 1] + in[ 7];
1859 ts7 = in[ 2] + in[ 6];
1860 ts8 = in[ 3] + in[ 5];
1862 out[17] = (ts5 + ts7 - ts8) * cx[8] - (ts6 - in[4]) * cx[8];
1863 st = (ts5 + ts7 - ts8) * cx[7] + (ts6 - in[4]) * cx[8];
1864 ct = (tc1 - tc3 - tc4) * cx[6];
1865 out[5] = ct + st;
1866 out[6] = ct - st;
1868 tc2 = (in[16] - in[10]) * cx[6];
1869 ts6 = ts6 * cx[7] + in[4] * cx[8];
1871 ct = tc1 * cx[0] + tc2 + tc3 * cx[1] + tc4 * cx[2];
1872 st = -ts5 * cx[4] + ts6 - ts7 * cx[5] + ts8 * cx[3];
1873 out[1] = ct + st;
1874 out[2] = ct - st;
1876 ct = tc1 * cx[1] - tc2 - tc3 * cx[2] + tc4 * cx[0];
1877 st = -ts5 * cx[5] + ts6 - ts7 * cx[3] + ts8 * cx[4];
1878 out[ 9] = ct + st;
1879 out[10] = ct - st;
1881 ct = tc1 * cx[2] - tc2 + tc3 * cx[0] - tc4 * cx[1];
1882 st = ts5 * cx[3] - ts6 + ts7 * cx[4] - ts8 * cx[5];
1883 out[13] = ct + st;
1884 out[14] = ct - st;
1886 ts1 = in[ 8] - in[ 0];
1887 ts3 = in[ 6] - in[ 2];
1888 ts4 = in[ 5] - in[ 3];
1889 tc5 = in[17] + in[ 9];
1890 tc6 = in[16] + in[10];
1891 tc7 = in[15] + in[11];
1892 tc8 = in[14] + in[12];
1894 out[0] = (tc5 + tc7 + tc8) * cx[8] + (tc6 + in[13]) * cx[8];
1895 ct = (tc5 + tc7 + tc8) * cx[7] - (tc6 + in[13]) * cx[8];
1896 st = (ts1 - ts3 + ts4) * cx[6];
1897 out[11] = ct + st;
1898 out[12] = ct - st;
1900 ts2 = (in[7] - in[1]) * cx[6];
1901 tc6 = in[13] * cx[8] - tc6 * cx[7];
1903 ct = tc5 * cx[3] - tc6 + tc7 * cx[4] + tc8 * cx[5];
1904 st = ts1 * cx[2] + ts2 + ts3 * cx[0] + ts4 * cx[1];
1905 out[3] = ct + st;
1906 out[4] = ct - st;
1908 ct =-tc5 * cx[5] + tc6 - tc7 * cx[3] - tc8 * cx[4];
1909 st = ts1 * cx[1] + ts2 - ts3 * cx[2] - ts4 * cx[0];
1910 out[7] = ct + st;
1911 out[8] = ct - st;
1913 ct =-tc5 * cx[4] + tc6 - tc7 * cx[5] - tc8 * cx[3];
1914 st = ts1 * cx[0] - ts2 + ts3 * cx[1] - ts4 * cx[2];
1915 out[15] = ct + st;
1916 out[16] = ct - st;
1919 static int find_bitrate_index(int type, int bitrate, bool stereo)
1921 if (type == 1 && !stereo && bitrate > 160)
1922 bitrate = 160;
1924 return ci->round_value_to_list32(bitrate,
1925 &bitr_index[type][1], 14, true) + 1;
1928 static int find_samplerate_index(long freq, int *mp3_type)
1930 int mpeg = freq >= (32000+24000)/2 ? 1 : 0;
1931 int i = ci->round_value_to_list32(freq, sampr_index[mpeg], 3, true);
1932 *mp3_type = mpeg;
1933 return i;
1936 bool init_mp3_encoder_engine(int sample_rate,
1937 int num_channels,
1938 struct encoder_config *enc_cfg)
1940 const bool stereo = num_channels > 1;
1941 uint32_t avg_byte_per_frame;
1943 cfg.channels = stereo ? 2 : 1;
1944 cfg.mpg.mode = stereo ? 0 : 3; /* 0=stereo, 3=mono */
1945 cfg.mpg.smpl_id = find_samplerate_index(sample_rate, &cfg.mpg.type);
1946 cfg.samplerate = sampr_index[cfg.mpg.type][cfg.mpg.smpl_id];
1947 cfg.mpg.bitr_id = find_bitrate_index(cfg.mpg.type,
1948 enc_cfg->mp3_enc.bitrate,
1949 stereo);
1950 cfg.mpg.bitrate = bitr_index[cfg.mpg.type][cfg.mpg.bitr_id];
1951 cfg.mpg.num_bands = num_bands[stereo ? cfg.mpg.type : 2][cfg.mpg.bitr_id];
1953 if (cfg.mpg.type == 1)
1955 cfg.granules = 2;
1956 pcm_chunk_size = PCM_CHUNK_SIZE1;
1957 samp_per_frame = SAMP_PER_FRAME1;
1959 else
1961 cfg.granules = 1;
1962 pcm_chunk_size = PCM_CHUNK_SIZE2;
1963 samp_per_frame = SAMP_PER_FRAME2;
1966 memcpy(scalefac, sfBand[cfg.mpg.smpl_id + 3*cfg.mpg.type], sizeof(scalefac));
1967 memset(mfbuf , 0 , sizeof(mfbuf ));
1968 memset(mdct_freq , 0 , sizeof(mdct_freq ));
1969 memset(enc_data , 0 , sizeof(enc_data ));
1970 memset(sb_data , 0 , sizeof(sb_data ));
1971 memset(&CodedData, 0 , sizeof(CodedData ));
1972 memcpy(ca , ca_const , sizeof(ca ));
1973 memcpy(cs , cs_const , sizeof(cs ));
1974 memcpy(cx , cx_const , sizeof(cx ));
1975 memcpy(win , win_const , sizeof(win ));
1976 memcpy(enwindow , enwindow_const , sizeof(enwindow ));
1977 memcpy(int2idx , int2idx_const , sizeof(int2idx ));
1978 memcpy(ht_count , ht_count_const , sizeof(ht_count ));
1979 memcpy( tab01 , tab01_const , sizeof(tab01 ));
1980 memcpy( tab23 , tab23_const , sizeof(tab23 ));
1981 memcpy( tab56 , tab56_const , sizeof(tab56 ));
1982 memcpy( tab1315 , tab1315_const , sizeof(tab1315 ));
1983 memcpy( tab1624 , tab1624_const , sizeof(tab1624 ));
1984 memcpy( tab789 , tab789_const , sizeof(tab789 ));
1985 memcpy( tabABC , tabABC_const , sizeof(tabABC ));
1986 memcpy( t1HB , t1HB_const , sizeof(t1HB ));
1987 memcpy( t2HB , t2HB_const , sizeof(t2HB ));
1988 memcpy( t3HB , t3HB_const , sizeof(t3HB ));
1989 memcpy( t5HB , t5HB_const , sizeof(t5HB ));
1990 memcpy( t6HB , t6HB_const , sizeof(t6HB ));
1991 memcpy( t7HB , t7HB_const , sizeof(t7HB ));
1992 memcpy( t8HB , t8HB_const , sizeof(t8HB ));
1993 memcpy( t9HB , t9HB_const , sizeof(t9HB ));
1994 memcpy(t10HB , t10HB_const , sizeof(t10HB ));
1995 memcpy(t11HB , t11HB_const , sizeof(t11HB ));
1996 memcpy(t12HB , t12HB_const , sizeof(t12HB ));
1997 memcpy(t13HB , t13HB_const , sizeof(t13HB ));
1998 memcpy(t15HB , t15HB_const , sizeof(t15HB ));
1999 memcpy(t16HB , t16HB_const , sizeof(t16HB ));
2000 memcpy(t24HB , t24HB_const , sizeof(t24HB ));
2001 memcpy( t1l , t1l_const , sizeof(t1l ));
2002 memcpy( t2l , t2l_const , sizeof(t2l ));
2003 memcpy( t3l , t3l_const , sizeof(t3l ));
2004 memcpy( t5l , t5l_const , sizeof(t5l ));
2005 memcpy( t6l , t6l_const , sizeof(t6l ));
2006 memcpy( t7l , t7l_const , sizeof(t7l ));
2007 memcpy( t8l , t8l_const , sizeof(t8l ));
2008 memcpy( t9l , t9l_const , sizeof(t9l ));
2009 memcpy(t10l , t10l_const , sizeof(t10l ));
2010 memcpy(t11l , t11l_const , sizeof(t11l ));
2011 memcpy(t12l , t12l_const , sizeof(t12l ));
2012 memcpy(t13l , t13l_const , sizeof(t13l ));
2013 memcpy(t15l , t15l_const , sizeof(t15l ));
2014 memcpy(t16l , t16l_const , sizeof(t16l ));
2015 memcpy(t24l , t24l_const , sizeof(t24l ));
2016 memcpy(ht , ht_const , sizeof(ht ));
2018 ht[ 0].table = NULL; ht[ 0].hlen = NULL; /* Apparently not used */
2019 ht[ 1].table = t1HB; ht[ 1].hlen = t1l;
2020 ht[ 2].table = t2HB; ht[ 2].hlen = t2l;
2021 ht[ 3].table = t3HB; ht[ 3].hlen = t3l;
2022 ht[ 4].table = NULL; ht[ 4].hlen = NULL; /* Apparently not used */
2023 ht[ 5].table = t5HB; ht[ 5].hlen = t5l;
2024 ht[ 6].table = t6HB; ht[ 6].hlen = t6l;
2025 ht[ 7].table = t7HB; ht[ 7].hlen = t7l;
2026 ht[ 8].table = t8HB; ht[ 8].hlen = t8l;
2027 ht[ 9].table = t9HB; ht[ 9].hlen = t9l;
2028 ht[10].table = t10HB; ht[10].hlen = t10l;
2029 ht[11].table = t11HB; ht[11].hlen = t11l;
2030 ht[12].table = t12HB; ht[12].hlen = t12l;
2031 ht[13].table = t13HB; ht[13].hlen = t13l;
2032 ht[14].table = NULL; ht[14].hlen = NULL; /* Apparently not used */
2033 ht[15].table = t15HB; ht[15].hlen = t15l;
2035 /* Figure average number of 'bytes' per frame */
2036 avg_byte_per_frame = SAMPL2 * 16000 * cfg.mpg.bitrate / (2 - cfg.mpg.type);
2037 avg_byte_per_frame = avg_byte_per_frame / cfg.samplerate;
2038 cfg.byte_per_frame = avg_byte_per_frame / 64;
2039 cfg.frac_per_frame = avg_byte_per_frame & 63;
2040 cfg.slot_lag = 0;
2041 cfg.sideinfo_len = 32 + (cfg.mpg.type ? (cfg.channels == 1 ? 136 : 256)
2042 : (cfg.channels == 1 ? 72 : 136));
2044 return true;
2047 STATICIRAM void to_mono_mm(void) ICODE_ATTR;
2048 STATICIRAM void to_mono_mm(void)
2050 /* |llllllllllllllll|rrrrrrrrrrrrrrrr| =>
2051 * |mmmmmmmmmmmmmmmm|mmmmmmmmmmmmmmmm|
2053 uint32_t *samp = (uint32_t *)&mfbuf[2*512];
2054 uint32_t *samp_end = samp + samp_per_frame;
2056 inline void to_mono(uint32_t **samp)
2058 int32_t lr = **samp;
2059 int32_t m = (int16_t)lr + (lr >> 16) + err;
2060 err = m & 1;
2061 m >>= 1;
2062 *(*samp)++ = (m << 16) | (uint16_t)m;
2063 } /* to_mono */
2067 to_mono(&samp);
2068 to_mono(&samp);
2069 to_mono(&samp);
2070 to_mono(&samp);
2071 to_mono(&samp);
2072 to_mono(&samp);
2073 to_mono(&samp);
2074 to_mono(&samp);
2076 while (samp < samp_end);
2077 } /* to_mono_mm */
2079 #ifdef ROCKBOX_LITTLE_ENDIAN
2080 /* Swaps a frame to big endian */
2081 static inline void byte_swap_frame32(uint32_t *dst, uint32_t *src,
2082 size_t size) ICODE_ATTR;
2083 static inline void byte_swap_frame32(uint32_t *dst, uint32_t *src,
2084 size_t size)
2086 uint32_t *src_end = SKIPBYTES(src, size);
2090 *dst++ = swap32(*src++);
2091 *dst++ = swap32(*src++);
2092 *dst++ = swap32(*src++);
2093 *dst++ = swap32(*src++);
2094 *dst++ = swap32(*src++);
2095 *dst++ = swap32(*src++);
2096 *dst++ = swap32(*src++);
2097 *dst++ = swap32(*src++);
2099 while(src < src_end);
2100 } /* byte_swap_frame32 */
2101 #endif /* ROCKBOX_LITTLE_ENDIAN */
2103 STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2104 ICODE_ATTR;
2105 STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2107 int gr, gr_cnt;
2108 int max, min;
2110 /* encode one mp3 frame in this loop */
2111 CodedData.bitpos = 0;
2112 memset(CodedData.bbuf, 0, sizeof(CodedData.bbuf));
2114 if((cfg.slot_lag += cfg.frac_per_frame) >= 64)
2115 { /* Padding for this frame */
2116 cfg.slot_lag -= 64;
2117 cfg.mpg.padding = 1;
2119 else
2120 cfg.mpg.padding = 0;
2122 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding
2123 - cfg.sideinfo_len) / cfg.granules / cfg.channels;
2125 /* shift out old samples */
2126 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512);
2128 if (chunk->flags & CHUNKF_START_FILE)
2130 /* prefix silent samples for encoder delay */
2131 memset(mfbuf + 2*512, 0, ENC_DELAY_SIZE);
2132 /* read new samples to iram for further processing */
2133 memcpy(mfbuf + 2*512 + ENC_DELAY_SIZE/2,
2134 buffer, pcm_chunk_size - ENC_DELAY_SIZE);
2135 chunk->num_pcm = samp_per_frame - ENC_DELAY_SAMP;
2137 else
2139 /* read new samples to iram for further processing */
2140 memcpy(mfbuf + 2*512, buffer, pcm_chunk_size);
2141 chunk->num_pcm = samp_per_frame;
2144 if (cfg.channels == 1)
2145 to_mono_mm();
2147 cfg.ResvSize = 0;
2148 gr_cnt = cfg.granules * cfg.channels;
2149 CodedData.bitpos = cfg.sideinfo_len; /* leave space for mp3 header */
2151 for(gr=0; gr<cfg.granules; gr++)
2153 short *wk = mfbuf + 2*286 + gr*1152;
2154 int ch;
2156 /* 16bit packed wav data can be windowed efficiently on coldfire */
2157 window_subband1(wk, sb_data[0][1-gr][0], sb_data[1][1-gr][0]);
2159 for(ch=0; ch<cfg.channels; ch++)
2161 int ii, k, shift;
2163 wk = mfbuf + 2*286 + gr*1152 + ch;
2165 /* 36864=4*18*16*32 */
2166 for(k=0; k<18; k++, wk+=64)
2168 window_subband2(wk, sb_data[ch][1-gr][k]);
2169 /* Compensate for inversion in the analysis filter */
2170 if(k & 1)
2172 int band;
2173 for(band=1; band<32; band+=2)
2174 sb_data[ch][1-gr][k][band] *= -1;
2178 /* Perform imdct of 18 previous + 18 current subband samples
2179 for integer precision do this loop twice (if neccessary)
2181 shift = k = 14;
2182 for(ii=0; ii<2 && k; ii++)
2184 int *mdct = mdct_freq;
2185 int band;
2187 cfg.cod_info[gr][ch].additStep = 4 * (14 - shift);
2189 for(band=0; band<cfg.mpg.num_bands; band++, mdct+=18)
2191 int *band0 = sb_data[ch][ gr][0] + order[band];
2192 int *band1 = sb_data[ch][1-gr][0] + order[band];
2193 int work[18];
2195 /* 9216=4*32*9*8 */
2196 for(k=-9; k<0; k++)
2198 int a = shft_n(band1[(k+9)*32], shift);
2199 int b = shft_n(band1[(8-k)*32], shift);
2200 int c = shft_n(band0[(k+9)*32], shift);
2201 int d = shft_n(band0[(8-k)*32], shift);
2203 work[k+ 9] = shft16(a * win[k+ 9][0] +
2204 b * win[k+ 9][1] +
2205 c * win[k+ 9][2] +
2206 d * win[k+ 9][3]);
2208 work[k+18] = shft16(c * win[k+18][0] +
2209 d * win[k+18][1] +
2210 a * win[k+18][2] +
2211 b * win[k+18][3]);
2214 /* 7200=4*18*100 */
2215 mdct_long(mdct, work);
2217 /* Perform aliasing reduction butterfly */
2218 if(band != 0)
2220 for(k=7; k>=0; --k)
2222 int bu, bd;
2223 bu = shft15(mdct[k]) * ca[k] +
2224 shft15(mdct[-1-k]) * cs[k];
2225 bd = shft15(mdct[k]) * cs[k] -
2226 shft15(mdct[-1-k]) * ca[k];
2227 mdct[-1-k] = bu;
2228 mdct[ k ] = bd;
2233 max = min = 0;
2234 for(k=0; k<576; k++)
2236 mdct_freq[k] = shft13(mdct_freq[k]);
2237 if(max < mdct_freq[k]) max = mdct_freq[k];
2238 if(min > mdct_freq[k]) min = mdct_freq[k];
2241 max = (max > -min) ? max : -min;
2242 cfg.cod_info[gr][ch].max_val = (long)max;
2244 /* calc new shift for higher integer precision */
2245 for(k=0; max<(0x3c00>>k); k++);
2246 shift = 12 - k;
2249 cfg.cod_info[gr][ch].quantStep +=
2250 cfg.cod_info[gr][ch].additStep;
2252 /* bit and noise allocation */
2253 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch],
2254 gr_cnt--);
2255 /* write the frame to the bitstream */
2256 Huffmancodebits(enc_data, mdct_freq,
2257 &cfg.cod_info[gr][ch]);
2259 cfg.cod_info[gr][ch].quantStep -=
2260 cfg.cod_info[gr][ch].additStep;
2262 if(cfg.granules == 1)
2264 memcpy(sb_data[ch][0], sb_data[ch][1],
2265 sizeof(sb_data[ch][0]));
2270 chunk->enc_size = cfg.byte_per_frame + cfg.mpg.padding;
2272 /* finish this chunk by adding sideinfo header data */
2273 CodedData.bitpos = 0;
2274 encodeSideInfo( cfg.cod_info );
2276 #ifdef ROCKBOX_BIG_ENDIAN
2277 /* copy chunk to enc_buffer */
2278 memcpy(chunk->enc_data, CodedData.bbuf, chunk->enc_size);
2279 #else
2280 /* swap frame to big endian */
2281 byte_swap_frame32((uint32_t *)chunk->enc_data, CodedData.bbuf, chunk->enc_size);
2282 #endif
2283 } /* encode_frame */
2285 /* called very often - inline */
2286 static inline bool is_file_data_ok(struct enc_file_event_data *filed) ICODE_ATTR;
2287 static inline bool is_file_data_ok(struct enc_file_event_data *filed)
2289 return filed->rec_file >= 0 && (long)filed->chunk->flags >= 0;
2290 } /* is_event_ok */
2292 /* called very often - inline */
2293 static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
2294 static inline bool on_write_chunk(struct enc_file_event_data *data)
2296 if (!is_file_data_ok(data))
2297 return false;
2299 if (data->chunk->enc_data == NULL)
2301 #ifdef ROCKBOX_HAS_LOGF
2302 ci->logf("mp3 enc: NULL data");
2303 #endif
2304 return true;
2307 if (ci->write(data->rec_file, data->chunk->enc_data,
2308 data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
2309 return false;
2311 data->num_pcm_samples += data->chunk->num_pcm;
2312 return true;
2313 } /* on_write_chunk */
2315 static bool on_start_file(struct enc_file_event_data *data)
2317 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
2318 return false;
2320 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC);
2322 if (data->rec_file < 0)
2323 return false;
2325 /* reset sample count */
2326 data->num_pcm_samples = 0;
2327 return true;
2328 } /* on_start_file */
2330 static bool on_end_file(struct enc_file_event_data *data)
2332 if (data->rec_file < 0)
2333 return false; /* file already closed, nothing more we can do */
2335 /* always _try_ to write the file header, even on error */
2336 if (ci->close(data->rec_file) != 0)
2337 return false;
2339 data->rec_file = -1;
2341 return true;
2342 } /* on_end_file */
2344 static void on_rec_new_stream(struct enc_buffer_event_data *data)
2346 int num_frames = cfg.mpg.type == 1 ?
2347 ENC_PADDING_FRAMES1 : ENC_PADDING_FRAMES2;
2349 if (data->flags & CHUNKF_END_FILE)
2351 /* add silent frames to end - encoder will also be flushed for start
2352 of next file if any */
2353 memset(res_buffer, 0, pcm_chunk_size);
2355 /* the initial chunk given for the end is at enc_wr_index */
2356 while (num_frames-- > 0)
2358 data->chunk->enc_data = ENC_CHUNK_SKIP_HDR(data->chunk->enc_data,
2359 data->chunk);
2361 encode_frame(res_buffer, data->chunk);
2362 data->chunk->num_pcm = samp_per_frame;
2364 ci->enc_finish_chunk();
2365 data->chunk = ci->enc_get_chunk();
2368 else if (data->flags & CHUNKF_PRERECORD)
2370 /* nothing to add and we cannot change prerecorded data */
2372 else if (data->flags & CHUNKF_START_FILE)
2374 /* starting fresh ... be sure to flush encoder first */
2375 struct enc_chunk_hdr *chunk = ENC_CHUNK_HDR(res_buffer);
2377 chunk->flags = 0;
2378 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
2380 while (num_frames-- > 0)
2382 memset(chunk->enc_data, 0, pcm_chunk_size);
2383 encode_frame(chunk->enc_data, chunk);
2386 } /* on_rec_new_stream */
2388 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
2389 ICODE_ATTR;
2390 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
2392 if (event == ENC_WRITE_CHUNK)
2394 if (on_write_chunk((struct enc_file_event_data *)data))
2395 return;
2397 else if (event == ENC_START_FILE)
2399 if (on_start_file((struct enc_file_event_data *)data))
2400 return;
2402 else if (event == ENC_END_FILE)
2404 if (on_end_file((struct enc_file_event_data *)data))
2405 return;
2407 else if (event == ENC_REC_NEW_STREAM)
2409 on_rec_new_stream((struct enc_buffer_event_data *)data);
2410 return;
2412 else
2414 return;
2417 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
2418 } /* enc_events_callback */
2420 static bool enc_init(void)
2422 struct enc_inputs inputs;
2423 struct enc_parameters params;
2425 if (ci->enc_get_inputs == NULL ||
2426 ci->enc_set_parameters == NULL ||
2427 ci->enc_get_chunk == NULL ||
2428 ci->enc_finish_chunk == NULL ||
2429 ci->enc_get_pcm_data == NULL ||
2430 ci->enc_unget_pcm_data == NULL )
2431 return false;
2433 ci->enc_get_inputs(&inputs);
2435 if (inputs.config->afmt != AFMT_MPA_L3)
2436 return false;
2438 init_mp3_encoder_engine(inputs.sample_rate, inputs.num_channels,
2439 inputs.config);
2441 err = 0;
2443 /* configure the buffer system */
2444 params.afmt = AFMT_MPA_L3;
2445 params.chunk_size = cfg.byte_per_frame + 1;
2446 params.enc_sample_rate = cfg.samplerate;
2447 /* need enough reserved bytes to hold one frame of pcm samples + hdr
2448 for padding and flushing */
2449 params.reserve_bytes = ENC_CHUNK_HDR_SIZE + pcm_chunk_size;
2450 params.events_callback = enc_events_callback;
2451 ci->enc_set_parameters(&params);
2453 res_buffer = params.reserve_buffer;
2455 #ifdef CPU_COLDFIRE
2456 asm volatile ("move.l #0, %macsr"); /* integer mode */
2457 #endif
2459 return true;
2460 } /* enc_init */
2462 enum codec_status codec_main(void)
2464 /* Generic codec initialisation */
2465 if (!enc_init())
2467 ci->enc_codec_loaded = -1;
2468 return CODEC_ERROR;
2471 /* main application waits for this flag during encoder loading */
2472 ci->enc_codec_loaded = 1;
2474 /* main encoding loop */
2475 while (!ci->stop_encoder)
2477 char *buffer;
2479 while ((buffer = ci->enc_get_pcm_data(pcm_chunk_size)) != NULL)
2481 struct enc_chunk_hdr *chunk;
2483 if (ci->stop_encoder)
2484 break;
2486 chunk = ci->enc_get_chunk();
2487 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
2489 encode_frame(buffer, chunk);
2491 if (chunk->num_pcm < samp_per_frame)
2493 ci->enc_unget_pcm_data(pcm_chunk_size - chunk->num_pcm*4);
2494 chunk->num_pcm = samp_per_frame;
2497 ci->enc_finish_chunk();
2499 ci->yield();
2502 ci->yield();
2505 /* reset parameters to initial state */
2506 ci->enc_set_parameters(NULL);
2508 /* main application waits for this flag during encoder removing */
2509 ci->enc_codec_loaded = 0;
2511 return CODEC_OK;
2512 } /* codec_start */
2514 #endif /* ndef SIMULATOR */