Add gigabeat F/S volume limits to the manual, and a few minor formatting changes.
[kugel-rb.git] / apps / codecs / mp3_enc.c
blob82a862002704eb6927c32aef2363806dc21de716
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Antonius Hellmann
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 // Shine is an MP3 encoder
23 // Copyright (C) 1999-2000 Gabriel Bouvigne
25 // This library is free software; you can redistribute it and/or
26 // modify it under the terms of the GNU Library General Public
27 // License as published by the Free Software Foundation; either
28 // version 2 of the License, or (at your option) any later version.
30 // This library is distributed in the hope that it will be useful,
31 // but WITHOUT ANY WARRANTY; without even the implied warranty of
32 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 // Library General Public License for more details.
35 #ifndef SIMULATOR
37 #include <inttypes.h>
38 #include "codeclib.h"
40 CODEC_ENC_HEADER
42 #define ENC_PADDING_FRAMES1 2
43 #define ENC_PADDING_FRAMES2 4
44 #define ENC_DELAY_SAMP 576
45 #define ENC_DELAY_SIZE (ENC_DELAY_SAMP*4)
46 #define SAMP_PER_FRAME1 1152
47 #define SAMP_PER_FRAME2 576
48 #define PCM_CHUNK_SIZE1 (SAMP_PER_FRAME1*4)
49 #define PCM_CHUNK_SIZE2 (SAMP_PER_FRAME2*4)
50 #define SAMPL2 576
51 #define SBLIMIT 32
52 #define HTN 16
53 #define memcpy ci->memcpy
54 #define memset ci->memset
55 #define putlong(c, s) if(s+sz <= 32) { cc = (cc << s) | c; sz+= s; } \
56 else { putbits(cc, sz); cc = c; sz = s; }
58 typedef struct {
59 int type; /* 0=(MPEG2 - 22.05,24,16kHz) 1=(MPEG1 - 44.1,48,32kHz) */
60 int mode; /* 0=stereo, 1=jstereo, 2=dual, 3=mono */
61 int bitrate;
62 int padding;
63 int num_bands;
64 long bitr_id;
65 int smpl_id;
66 } mpeg_t;
68 /* Side information */
69 typedef struct {
70 uint32_t part2_3_length;
71 int count1; /* number of 0-1-quadruples */
72 uint32_t global_gain;
73 uint32_t table_select[4];
74 uint32_t region_0_1;
75 uint32_t address1;
76 uint32_t address2;
77 uint32_t address3;
78 long quantStep;
79 long additStep;
80 uint32_t max_val;
81 } side_info_t;
83 typedef struct {
84 side_info_t cod_info[2][2];
85 mpeg_t mpg;
86 long frac_per_frame;
87 long byte_per_frame;
88 long slot_lag;
89 int sideinfo_len;
90 int mean_bits;
91 int ResvSize;
92 int channels;
93 int rec_mono_mode;
94 int granules;
95 long samplerate;
96 } config_t;
98 typedef struct {
99 int bitpos; /* current bitpos for writing */
100 uint32_t bbuf[263];
101 } BF_Data;
103 struct huffcodetab {
104 int len; /* max. index */
105 const uint8_t *table; /* pointer to array[len][len] */
106 const uint8_t *hlen; /* pointer to array[len][len] */
109 struct huffcodebig {
110 int len; /* max. index */
111 int linbits; /* number of linbits */
112 int linmax; /* max number stored in linbits */
115 #define shft4(x) ((x + 8) >> 4)
116 #define shft9(x) ((x + 256) >> 9)
117 #define shft13(x) ((x + 4096) >> 13)
118 #define shft15(x) ((x + 16384) >> 15)
119 #define shft16(x) ((x + 32768) >> 16)
120 #define shft_n(x,n) ((x) >> n)
121 #define SQRT 724 /* sqrt(2) * 512 */
123 static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */
124 static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */
125 static int mdct_freq [SAMPL2] IBSS_ATTR; /* 2304 Bytes */
126 static char mdct_sign [SAMPL2] IBSS_ATTR; /* 576 Bytes */
127 static short enc_data [SAMPL2] IBSS_ATTR; /* 1152 Bytes */
128 static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */
129 static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */
130 static int ca [8] IBSS_ATTR; /* 32 Bytes */
131 static int cs [8] IBSS_ATTR; /* 32 Bytes */
132 static int cx [9] IBSS_ATTR; /* 36 Bytes */
133 static int win [18][4] IBSS_ATTR; /* 288 Bytes */
134 static short enwindow [15*27+24] IBSS_ATTR; /* 862 Bytes */
135 static short int2idx [4096] IBSS_ATTR; /* 8192 Bytes */
136 static uint8_t ht_count [2][2][16] IBSS_ATTR; /* 64 Bytes */
137 static uint32_t tab01 [ 16] IBSS_ATTR; /* 64 Bytes */
138 static uint32_t tab23 [ 9] IBSS_ATTR; /* 36 Bytes */
139 static uint32_t tab56 [ 16] IBSS_ATTR; /* 64 Bytes */
140 static uint32_t tab1315 [256] IBSS_ATTR; /* 1024 Bytes */
141 static uint32_t tab1624 [256] IBSS_ATTR; /* 1024 Bytes */
142 static uint32_t tab789 [ 36] IBSS_ATTR; /* 144 Bytes */
143 static uint32_t tabABC [ 64] IBSS_ATTR; /* 256 Bytes */
144 static uint8_t t1HB [ 4] IBSS_ATTR;
145 static uint8_t t2HB [ 9] IBSS_ATTR;
146 static uint8_t t3HB [ 9] IBSS_ATTR;
147 static uint8_t t5HB [ 16] IBSS_ATTR;
148 static uint8_t t6HB [ 16] IBSS_ATTR;
149 static uint8_t t7HB [ 36] IBSS_ATTR;
150 static uint8_t t8HB [ 36] IBSS_ATTR;
151 static uint8_t t9HB [ 36] IBSS_ATTR;
152 static uint8_t t10HB [ 64] IBSS_ATTR;
153 static uint8_t t11HB [ 64] IBSS_ATTR;
154 static uint8_t t12HB [ 64] IBSS_ATTR;
155 static uint8_t t13HB [256] IBSS_ATTR;
156 static uint8_t t15HB [256] IBSS_ATTR;
157 static uint16_t t16HB [256] IBSS_ATTR;
158 static uint16_t t24HB [256] IBSS_ATTR;
159 static uint8_t t1l [ 8] IBSS_ATTR;
160 static uint8_t t2l [ 9] IBSS_ATTR;
161 static uint8_t t3l [ 9] IBSS_ATTR;
162 static uint8_t t5l [ 16] IBSS_ATTR;
163 static uint8_t t6l [ 16] IBSS_ATTR;
164 static uint8_t t7l [ 36] IBSS_ATTR;
165 static uint8_t t8l [ 36] IBSS_ATTR;
166 static uint8_t t9l [ 36] IBSS_ATTR;
167 static uint8_t t10l [ 64] IBSS_ATTR;
168 static uint8_t t11l [ 64] IBSS_ATTR;
169 static uint8_t t12l [ 64] IBSS_ATTR;
170 static uint8_t t13l [256] IBSS_ATTR;
171 static uint8_t t15l [256] IBSS_ATTR;
172 static uint8_t t16l [256] IBSS_ATTR;
173 static uint8_t t24l [256] IBSS_ATTR;
174 static struct huffcodetab ht [HTN] IBSS_ATTR;
176 static unsigned pcm_chunk_size IBSS_ATTR;
177 static unsigned samp_per_frame IBSS_ATTR;
179 static config_t cfg IBSS_ATTR;
180 static char *res_buffer;
181 static int32_t err IBSS_ATTR;
182 static uint8_t band_scale_f[22];
184 static const uint8_t ht_count_const[2][2][16] =
185 { { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */
186 { 1, 5, 5, 7, 5, 8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } }, /* hleng0 */
187 { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, /* table1 */
188 { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */
190 static const uint8_t t1HB_const[4] = {1,1,1,0};
191 static const uint8_t t2HB_const[9] = {1,2,1,3,1,1,3,2,0};
192 static const uint8_t t3HB_const[9] = {3,2,1,1,1,1,3,2,0};
193 static const uint8_t t5HB_const[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0};
194 static const uint8_t t6HB_const[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0};
196 static const uint8_t t7HB_const[36] =
197 { 1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4,
198 12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 };
200 static const uint8_t t8HB_const[36] =
201 { 3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3,
202 19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 };
204 static const uint8_t t9HB_const[36] =
205 { 7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5,
206 15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 };
208 static const uint8_t t10HB_const[64] =
209 {1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32,
210 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22,
211 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 };
213 static const uint8_t t11HB_const[64] =
214 {3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30,
215 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26,
216 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 };
218 static const uint8_t t12HB_const[64] =
219 {9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21,
220 30,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17,
221 31,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 };
223 static const uint8_t t13HB_const[256] =
224 {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,
225 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,
226 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,
227 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,
228 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,
229 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,
230 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,
231 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,
232 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,
233 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 };
235 static const uint8_t t15HB_const[256] =
236 {7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51,
237 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,
238 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,
239 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,
240 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,
241 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,
242 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,
243 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,
244 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,
245 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};
247 static const uint16_t t16HB_const[256] =
248 {1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47,
249 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209,
250 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65,
251 115,101,179,164,155,264,246,226,395,382,362,9,66,30,59,56,102,185,173,265,142,
252 253,232,400,388,378,445,16,111,54,52,100,184,178,160,133,257,244,228,217,385,
253 366,715,10,98,48,91,88,165,157,148,261,248,407,397,372,380,889,884,8,85,84,81,
254 159,156,143,260,249,427,401,392,383,727,713,708,7,154,76,73,141,131,256,245,
255 426,406,394,384,735,359,710,352,11,139,129,67,125,247,233,229,219,393,743,737,
256 720,885,882,439,4,243,120,118,115,227,223,396,746,742,736,721,712,706,223,436,
257 6,202,224,222,218,216,389,386,381,364,888,443,707,440,437,1728,4,747,211,210,
258 208,370,379,734,723,714,1735,883,877,876,3459,865,2,377,369,102,187,726,722,
259 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3,
260 1,3};
262 static const uint16_t t24HB_const[256] =
263 {15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71,
264 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194,
265 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293,
266 271,16,147,72,69,135,127,118,112,210,200,188,352,323,306,285,540,14,263,66,
267 129,126,119,114,214,202,192,180,341,317,301,281,262,12,249,123,121,117,113,
268 215,206,195,185,347,330,308,291,272,520,10,435,115,111,109,211,203,196,187,
269 353,332,313,298,283,531,381,17,427,212,208,205,201,193,186,177,169,320,303,
270 286,268,514,377,16,335,199,197,191,189,181,174,333,321,305,289,275,521,379,
271 371,11,668,184,183,179,175,344,331,314,304,290,277,530,383,373,366,10,652,346,
272 171,168,164,318,309,299,287,276,263,513,375,368,362,6,648,322,316,312,307,302,
273 292,284,269,261,512,376,370,364,359,4,620,300,296,294,288,282,273,266,515,380,
274 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360,
275 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3};
277 static const uint32_t tab1315_const[256] =
278 { 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a,
279 0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e,
280 0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a,
281 0x0b000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0e000c,0x0e000d,0x0e000d,
282 0x070006,0x080007,0x090007,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,
283 0x0b000a,0x0c000b,0x0c000b,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
284 0x080007,0x090008,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0c000b,
285 0x0c000b,0x0d000b,0x0d000c,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
286 0x090008,0x090008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
287 0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000d,
288 0x0a0009,0x0a0009,0x0b0009,0x0c000a,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
289 0x0d000b,0x0d000b,0x0e000c,0x0d000c,0x0f000d,0x0f000d,0x10000d,0x10000e,
290 0x0a000a,0x0b0009,0x0c000a,0x0c000a,0x0d000a,0x0d000b,0x0d000b,0x0d000b,
291 0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x10000e,
292 0x0b000a,0x0b000a,0x0c000a,0x0d000b,0x0d000b,0x0d000b,0x0e000b,0x0e000c,
293 0x0e000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x12000d,0x12000e,
294 0x0a000a,0x0a000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0e000c,
295 0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x11000e,0x11000e,
296 0x0b000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0d000c,0x0f000c,
297 0x0e000c,0x0f000d,0x0f000d,0x10000d,0x10000d,0x10000e,0x12000e,0x11000e,
298 0x0b000b,0x0c000b,0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000c,
299 0x0e000c,0x0f000d,0x10000d,0x0f000d,0x10000d,0x11000e,0x12000f,0x13000e,
300 0x0c000b,0x0c000b,0x0c000b,0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0e000c,
301 0x0f000d,0x0f000d,0x0f000d,0x10000d,0x11000e,0x11000e,0x11000e,0x12000f,
302 0x0c000c,0x0d000c,0x0d000b,0x0e000c,0x0e000c,0x0f000c,0x0e000d,0x0f000d,
303 0x10000d,0x10000d,0x11000d,0x11000d,0x11000e,0x12000e,0x12000f,0x12000f,
304 0x0d000c,0x0d000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x10000d,
305 0x10000d,0x10000e,0x10000e,0x11000e,0x12000e,0x11000e,0x12000f,0x12000f,
306 0x0e000d,0x0e000d,0x0e000d,0x0f000d,0x0f000d,0x0f000d,0x11000d,0x10000d,
307 0x10000e,0x13000e,0x11000e,0x11000e,0x11000f,0x13000f,0x12000e,0x12000f,
308 0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e,
309 0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f };
311 static const uint32_t tab01_const[16] =
312 { 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007,
313 0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 };
315 static const uint32_t tab23_const[ 9] =
316 { 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 };
318 static const uint32_t tab56_const[16] =
319 { 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007,
320 0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 };
322 static const uint32_t tab789_const[36] =
323 {0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004,
324 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806,
325 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808,
326 0x00b02c09,0x00b02c09,0x00b0300a,0x00802408,0x00902408,0x00a02809,0x00b02c09,
327 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b,
328 0x00c0340b};
330 static const uint32_t tabABC_const[64] =
331 {0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a,
332 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809,
333 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809,
334 0x00c02c0a,0x00b02809,0x00b0280a,0x00802007,0x00902007,0x00a02408,0x00b02c08,
335 0x00c02809,0x00c0300a,0x00b0280a,0x00c02c0a,0x00902408,0x00a02808,0x00b02809,
336 0x00c02c09,0x00c02c0a,0x00c0300a,0x00c02c0a,0x00c0300b,0x00a02409,0x00b02809,
337 0x00c02c0a,0x00c0300a,0x00d0300a,0x00d0340b,0x00c0300a,0x00d0340b,0x00902409,
338 0x00a02409,0x00b02409,0x00c0280a,0x00c02c0a,0x00c0300b,0x00d0300b,0x00d0300c,
339 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b,
340 0x00d0300c};
342 static const uint32_t tab1624_const[256] =
343 {0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a,
344 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c,
345 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009,
346 0x000b000a,0x000b000a,0x000b000b,0x000c000b,0x000c000b,0x000c000c,0x000d000c,
347 0x000e000c,0x000d000c,0x000e000c,0x000a000a,0x00070007,0x00080007,0x00090008,
348 0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000b,0x000d000b,0x000c000b,
349 0x000d000b,0x000d000c,0x000d000c,0x000e000c,0x000e000d,0x000b0009,0x00090008,
350 0x00090008,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,0x000c000b,
351 0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,0x000f000c,
352 0x000c0009,0x000a0009,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,
353 0x000d000a,0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,
354 0x000f000c,0x000f000d,0x000b0009,0x000a000a,0x000a0009,0x000b000a,0x000b000a,
355 0x000c000a,0x000d000a,0x000d000b,0x000e000b,0x000d000b,0x000e000b,0x000e000c,
356 0x000f000c,0x000f000c,0x000f000c,0x0010000c,0x000c0009,0x000b000a,0x000b000a,
357 0x000b000a,0x000c000a,0x000d000a,0x000d000b,0x000d000b,0x000d000b,0x000e000b,
358 0x000e000c,0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000d,0x000c0009,
359 0x000b000b,0x000b000a,0x000c000a,0x000c000a,0x000d000b,0x000d000b,0x000d000b,
360 0x000e000b,0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000d,
361 0x0011000d,0x000c000a,0x000b000b,0x000c000b,0x000c000b,0x000d000b,0x000d000b,
362 0x000d000b,0x000e000b,0x000e000b,0x000f000b,0x000f000c,0x000f000c,0x000f000c,
363 0x0010000c,0x0010000d,0x0010000d,0x000c000a,0x000c000b,0x000c000b,0x000c000b,
364 0x000d000b,0x000d000b,0x000e000b,0x000e000b,0x000f000c,0x000f000c,0x000f000c,
365 0x000f000c,0x0010000c,0x000f000d,0x0010000d,0x000f000d,0x000d000a,0x000c000c,
366 0x000d000b,0x000c000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000e000c,
367 0x000f000c,0x0010000c,0x0010000c,0x0010000d,0x0011000d,0x0011000d,0x0010000d,
368 0x000c000a,0x000d000c,0x000d000c,0x000d000b,0x000d000b,0x000e000b,0x000e000c,
369 0x000f000c,0x0010000c,0x0010000c,0x0010000c,0x0010000c,0x0010000d,0x0010000d,
370 0x000f000d,0x0010000d,0x000d000a,0x000d000c,0x000e000c,0x000e000c,0x000e000c,
371 0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000c,0x0010000d,
372 0x0010000d,0x0010000d,0x0010000d,0x0012000d,0x000d000a,0x000f000c,0x000e000c,
373 0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000c,0x0010000c,0x0010000d,
374 0x0012000d,0x0011000d,0x0011000d,0x0011000d,0x0013000d,0x0011000d,0x000d000a,
375 0x000e000d,0x000f000c,0x000d000c,0x000e000c,0x0010000c,0x0010000c,0x000f000c,
376 0x0010000d,0x0010000d,0x0011000d,0x0012000d,0x0011000d,0x0013000d,0x0011000d,
377 0x0010000d,0x000d000a,0x000a0009,0x000a0009,0x000a0009,0x000b0009,0x000b0009,
378 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a,
379 0x000d000a,0x000d000a,0x000d000a,0x000a0006};
381 static const uint8_t t1l_const[8] = {1,3,2,3,1,4,3,5};
382 static const uint8_t t2l_const[9] = {1,3,6,3,3,5,5,5,6};
383 static const uint8_t t3l_const[9] = {2,2,6,3,2,5,5,5,6};
384 static const uint8_t t5l_const[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8};
385 static const uint8_t t6l_const[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7};
387 static const uint8_t t7l_const[36] =
388 {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};
390 static const uint8_t t8l_const[36] =
391 {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};
393 static const uint8_t t9l_const[36] =
394 {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};
396 static const uint8_t t10l_const[64] =
397 {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,
398 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};
400 static const uint8_t t11l_const[64] =
401 {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,
402 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};
404 static const uint8_t t12l_const[64] =
405 {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,
406 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};
408 static const uint8_t t13l_const[256] =
409 {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,
410 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,
411 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,
412 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,
413 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,
414 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,
415 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,
416 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,
417 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,
418 15,15,16,16,19,18,19,16};
420 static const uint8_t t15l_const[256] =
421 {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,
422 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,
423 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,
424 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,
425 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,
426 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,
427 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,
428 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,
429 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13};
431 static const uint8_t t16l_const[256] =
432 {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,
433 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,
434 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,
435 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,
436 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,
437 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,
438 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,
439 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,
440 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,
441 11,11,11,11,11,11,11,8};
443 static const uint8_t t24l_const[256] =
444 {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,
445 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,
446 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,
447 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,
448 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,
449 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,
450 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,
451 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,
452 7,7,7,7,7,7,8,8,8,8,4};
454 static const struct huffcodetab ht_const[HTN] =
455 { { 0, NULL, NULL}, /* Apparently not used */
456 { 2, t1HB, t1l},
457 { 3, t2HB, t2l},
458 { 3, t3HB, t3l},
459 { 0, NULL, NULL}, /* Apparently not used */
460 { 4, t5HB, t5l},
461 { 4, t6HB, t6l},
462 { 6, t7HB, t7l},
463 { 6, t8HB, t8l},
464 { 6, t9HB, t9l},
465 { 8, t10HB, t10l},
466 { 8, t11HB, t11l},
467 { 8, t12HB, t12l},
468 {16, t13HB, t13l},
469 { 0, NULL, NULL}, /* Apparently not used */
470 {16, t15HB, t15l} };
472 static const struct huffcodebig ht_big[HTN] =
473 { { 16, 1, 1 },
474 { 16, 2, 3 },
475 { 16, 3, 7 },
476 { 16, 4, 15 },
477 { 16, 6, 63 },
478 { 16, 8, 255 },
479 { 16, 10, 1023 },
480 { 16, 13, 8191 },
481 { 16, 4, 15 },
482 { 16, 5, 31 },
483 { 16, 6, 63 },
484 { 16, 7, 127 },
485 { 16, 8, 255 },
486 { 16, 9, 511 },
487 { 16, 11, 2047 },
488 { 16, 13, 8191 } };
490 static const struct
492 uint32_t region0_cnt;
493 uint32_t region1_cnt;
494 } subdv_table[23] =
495 { {0, 0}, /* 0 bands */
496 {0, 0}, /* 1 bands */
497 {0, 0}, /* 2 bands */
498 {0, 0}, /* 3 bands */
499 {0, 0}, /* 4 bands */
500 {0, 1}, /* 5 bands */
501 {1, 1}, /* 6 bands */
502 {1, 1}, /* 7 bands */
503 {1, 2}, /* 8 bands */
504 {2, 2}, /* 9 bands */
505 {2, 3}, /* 10 bands */
506 {2, 3}, /* 11 bands */
507 {3, 4}, /* 12 bands */
508 {3, 4}, /* 13 bands */
509 {3, 4}, /* 14 bands */
510 {4, 5}, /* 15 bands */
511 {4, 5}, /* 16 bands */
512 {4, 6}, /* 17 bands */
513 {5, 6}, /* 18 bands */
514 {5, 6}, /* 19 bands */
515 {5, 7}, /* 20 bands */
516 {6, 7}, /* 21 bands */
517 {6, 7}, /* 22 bands */
520 static const uint32_t sfBand[6][23] =
522 /* Table B.2.b: 22.05 kHz */
523 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
524 /* Table B.2.c: 24 kHz */
525 {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
526 /* Table B.2.a: 16 kHz */
527 {0,6,12,18,24,30,36,44,45,66,80,96,116,140,168,200,238,248,336,396,464,522,576},
528 /* Table B.8.b: 44.1 kHz */
529 {0,4, 8,12,16,20,24,30,36,44,52,62, 74, 90,110,134,162,196,238,288,342,418,576},
530 /* Table B.8.c: 48 kHz */
531 {0,4, 8,12,16,20,24,30,36,42,50,60, 72, 88,106,128,156,190,230,276,330,384,576},
532 /* Table B.8.a: 32 kHz */
533 {0,4, 8,12,16,20,24,30,36,44,54,66, 82,102,126,156,194,240,296,364,448,550,576} };
536 static const short int2idx_const[4096] = /* int2idx[i] = sqrt(i*sqrt(i)); */
538 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9,
539 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16,
540 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21,
541 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26,
542 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
543 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36,
544 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 40,
545 41, 41, 41, 41, 42, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45,
546 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49,
547 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53,
548 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57,
549 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61,
550 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65,
551 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68,
552 68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 72, 72, 72,
553 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75,
554 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 79, 79, 79,
555 79, 79, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82,
556 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 86, 86, 86,
557 86, 86, 86, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89,
558 89, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 93,
559 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, 95, 96, 96, 96,
560 96, 96, 96, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99,
561 99, 99,100,100,100,100,100,100,101,101,101,101,101,101,102,102,102,102,102,102,
562 103,103,103,103,103,103,104,104,104,104,104,104,104,105,105,105,105,105,105,106,
563 106,106,106,106,106,107,107,107,107,107,107,107,108,108,108,108,108,108,109,109,
564 109,109,109,109,110,110,110,110,110,110,110,111,111,111,111,111,111,112,112,112,
565 112,112,112,112,113,113,113,113,113,113,114,114,114,114,114,114,115,115,115,115,
566 115,115,115,116,116,116,116,116,116,117,117,117,117,117,117,117,118,118,118,118,
567 118,118,118,119,119,119,119,119,119,120,120,120,120,120,120,120,121,121,121,121,
568 121,121,122,122,122,122,122,122,122,123,123,123,123,123,123,123,124,124,124,124,
569 124,124,125,125,125,125,125,125,125,126,126,126,126,126,126,126,127,127,127,127,
570 127,127,128,128,128,128,128,128,128,129,129,129,129,129,129,129,130,130,130,130,
571 130,130,131,131,131,131,131,131,131,132,132,132,132,132,132,132,133,133,133,133,
572 133,133,133,134,134,134,134,134,134,134,135,135,135,135,135,135,136,136,136,136,
573 136,136,136,137,137,137,137,137,137,137,138,138,138,138,138,138,138,139,139,139,
574 139,139,139,139,140,140,140,140,140,140,140,141,141,141,141,141,141,141,142,142,
575 142,142,142,142,142,143,143,143,143,143,143,143,144,144,144,144,144,144,144,145,
576 145,145,145,145,145,145,146,146,146,146,146,146,146,147,147,147,147,147,147,147,
577 148,148,148,148,148,148,148,149,149,149,149,149,149,149,150,150,150,150,150,150,
578 150,151,151,151,151,151,151,151,152,152,152,152,152,152,152,153,153,153,153,153,
579 153,153,154,154,154,154,154,154,154,154,155,155,155,155,155,155,155,156,156,156,
580 156,156,156,156,157,157,157,157,157,157,157,158,158,158,158,158,158,158,159,159,
581 159,159,159,159,159,160,160,160,160,160,160,160,160,161,161,161,161,161,161,161,
582 162,162,162,162,162,162,162,163,163,163,163,163,163,163,163,164,164,164,164,164,
583 164,164,165,165,165,165,165,165,165,166,166,166,166,166,166,166,167,167,167,167,
584 167,167,167,167,168,168,168,168,168,168,168,169,169,169,169,169,169,169,169,170,
585 170,170,170,170,170,170,171,171,171,171,171,171,171,172,172,172,172,172,172,172,
586 172,173,173,173,173,173,173,173,174,174,174,174,174,174,174,174,175,175,175,175,
587 175,175,175,176,176,176,176,176,176,176,176,177,177,177,177,177,177,177,178,178,
588 178,178,178,178,178,178,179,179,179,179,179,179,179,180,180,180,180,180,180,180,
589 180,181,181,181,181,181,181,181,182,182,182,182,182,182,182,182,183,183,183,183,
590 183,183,183,184,184,184,184,184,184,184,184,185,185,185,185,185,185,185,186,186,
591 186,186,186,186,186,186,187,187,187,187,187,187,187,187,188,188,188,188,188,188,
592 188,189,189,189,189,189,189,189,189,190,190,190,190,190,190,190,190,191,191,191,
593 191,191,191,191,192,192,192,192,192,192,192,192,193,193,193,193,193,193,193,193,
594 194,194,194,194,194,194,194,195,195,195,195,195,195,195,195,196,196,196,196,196,
595 196,196,196,197,197,197,197,197,197,197,197,198,198,198,198,198,198,198,199,199,
596 199,199,199,199,199,199,200,200,200,200,200,200,200,200,201,201,201,201,201,201,
597 201,201,202,202,202,202,202,202,202,202,203,203,203,203,203,203,203,204,204,204,
598 204,204,204,204,204,205,205,205,205,205,205,205,205,206,206,206,206,206,206,206,
599 206,207,207,207,207,207,207,207,207,208,208,208,208,208,208,208,208,209,209,209,
600 209,209,209,209,209,210,210,210,210,210,210,210,210,211,211,211,211,211,211,211,
601 211,212,212,212,212,212,212,212,212,213,213,213,213,213,213,213,213,214,214,214,
602 214,214,214,214,214,215,215,215,215,215,215,215,215,216,216,216,216,216,216,216,
603 216,217,217,217,217,217,217,217,217,218,218,218,218,218,218,218,218,219,219,219,
604 219,219,219,219,219,220,220,220,220,220,220,220,220,221,221,221,221,221,221,221,
605 221,222,222,222,222,222,222,222,222,223,223,223,223,223,223,223,223,224,224,224,
606 224,224,224,224,224,225,225,225,225,225,225,225,225,226,226,226,226,226,226,226,
607 226,227,227,227,227,227,227,227,227,228,228,228,228,228,228,228,228,229,229,229,
608 229,229,229,229,229,229,230,230,230,230,230,230,230,230,231,231,231,231,231,231,
609 231,231,232,232,232,232,232,232,232,232,233,233,233,233,233,233,233,233,234,234,
610 234,234,234,234,234,234,234,235,235,235,235,235,235,235,235,236,236,236,236,236,
611 236,236,236,237,237,237,237,237,237,237,237,238,238,238,238,238,238,238,238,238,
612 239,239,239,239,239,239,239,239,240,240,240,240,240,240,240,240,241,241,241,241,
613 241,241,241,241,242,242,242,242,242,242,242,242,242,243,243,243,243,243,243,243,
614 243,244,244,244,244,244,244,244,244,245,245,245,245,245,245,245,245,245,246,246,
615 246,246,246,246,246,246,247,247,247,247,247,247,247,247,248,248,248,248,248,248,
616 248,248,248,249,249,249,249,249,249,249,249,250,250,250,250,250,250,250,250,250,
617 251,251,251,251,251,251,251,251,252,252,252,252,252,252,252,252,253,253,253,253,
618 253,253,253,253,253,254,254,254,254,254,254,254,254,255,255,255,255,255,255,255,
619 255,255,256,256,256,256,256,256,256,256,257,257,257,257,257,257,257,257,257,258,
620 258,258,258,258,258,258,258,259,259,259,259,259,259,259,259,259,260,260,260,260,
621 260,260,260,260,261,261,261,261,261,261,261,261,261,262,262,262,262,262,262,262,
622 262,263,263,263,263,263,263,263,263,263,264,264,264,264,264,264,264,264,265,265,
623 265,265,265,265,265,265,265,266,266,266,266,266,266,266,266,267,267,267,267,267,
624 267,267,267,267,268,268,268,268,268,268,268,268,268,269,269,269,269,269,269,269,
625 269,270,270,270,270,270,270,270,270,270,271,271,271,271,271,271,271,271,271,272,
626 272,272,272,272,272,272,272,273,273,273,273,273,273,273,273,273,274,274,274,274,
627 274,274,274,274,275,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276,
628 276,276,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,279,
629 279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,281,281,281,
630 281,281,281,281,281,282,282,282,282,282,282,282,282,282,283,283,283,283,283,283,
631 283,283,283,284,284,284,284,284,284,284,284,284,285,285,285,285,285,285,285,285,
632 286,286,286,286,286,286,286,286,286,287,287,287,287,287,287,287,287,287,288,288,
633 288,288,288,288,288,288,288,289,289,289,289,289,289,289,289,289,290,290,290,290,
634 290,290,290,290,291,291,291,291,291,291,291,291,291,292,292,292,292,292,292,292,
635 292,292,293,293,293,293,293,293,293,293,293,294,294,294,294,294,294,294,294,294,
636 295,295,295,295,295,295,295,295,295,296,296,296,296,296,296,296,296,296,297,297,
637 297,297,297,297,297,297,297,298,298,298,298,298,298,298,298,299,299,299,299,299,
638 299,299,299,299,300,300,300,300,300,300,300,300,300,301,301,301,301,301,301,301,
639 301,301,302,302,302,302,302,302,302,302,302,303,303,303,303,303,303,303,303,303,
640 304,304,304,304,304,304,304,304,304,305,305,305,305,305,305,305,305,305,306,306,
641 306,306,306,306,306,306,306,307,307,307,307,307,307,307,307,307,308,308,308,308,
642 308,308,308,308,308,309,309,309,309,309,309,309,309,309,310,310,310,310,310,310,
643 310,310,310,311,311,311,311,311,311,311,311,311,312,312,312,312,312,312,312,312,
644 312,313,313,313,313,313,313,313,313,313,314,314,314,314,314,314,314,314,314,315,
645 315,315,315,315,315,315,315,315,316,316,316,316,316,316,316,316,316,317,317,317,
646 317,317,317,317,317,317,318,318,318,318,318,318,318,318,318,318,319,319,319,319,
647 319,319,319,319,319,320,320,320,320,320,320,320,320,320,321,321,321,321,321,321,
648 321,321,321,322,322,322,322,322,322,322,322,322,323,323,323,323,323,323,323,323,
649 323,324,324,324,324,324,324,324,324,324,325,325,325,325,325,325,325,325,325,325,
650 326,326,326,326,326,326,326,326,326,327,327,327,327,327,327,327,327,327,328,328,
651 328,328,328,328,328,328,328,329,329,329,329,329,329,329,329,329,330,330,330,330,
652 330,330,330,330,330,330,331,331,331,331,331,331,331,331,331,332,332,332,332,332,
653 332,332,332,332,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,
654 334,334,335,335,335,335,335,335,335,335,335,335,336,336,336,336,336,336,336,336,
655 336,337,337,337,337,337,337,337,337,337,338,338,338,338,338,338,338,338,338,338,
656 339,339,339,339,339,339,339,339,339,340,340,340,340,340,340,340,340,340,341,341,
657 341,341,341,341,341,341,341,341,342,342,342,342,342,342,342,342,342,343,343,343,
658 343,343,343,343,343,343,344,344,344,344,344,344,344,344,344,344,345,345,345,345,
659 345,345,345,345,345,346,346,346,346,346,346,346,346,346,347,347,347,347,347,347,
660 347,347,347,347,348,348,348,348,348,348,348,348,348,349,349,349,349,349,349,349,
661 349,349,350,350,350,350,350,350,350,350,350,350,351,351,351,351,351,351,351,351,
662 351,352,352,352,352,352,352,352,352,352,352,353,353,353,353,353,353,353,353,353,
663 354,354,354,354,354,354,354,354,354,355,355,355,355,355,355,355,355,355,355,356,
664 356,356,356,356,356,356,356,356,357,357,357,357,357,357,357,357,357,357,358,358,
665 358,358,358,358,358,358,358,359,359,359,359,359,359,359,359,359,359,360,360,360,
666 360,360,360,360,360,360,361,361,361,361,361,361,361,361,361,361,362,362,362,362,
667 362,362,362,362,362,363,363,363,363,363,363,363,363,363,363,364,364,364,364,364,
668 364,364,364,364,365,365,365,365,365,365,365,365,365,365,366,366,366,366,366,366,
669 366,366,366,367,367,367,367,367,367,367,367,367,367,368,368,368,368,368,368,368,
670 368,368,369,369,369,369,369,369,369,369,369,369,370,370,370,370,370,370,370,370,
671 370,370,371,371,371,371,371,371,371,371,371,372,372,372,372,372,372,372,372,372,
672 372,373,373,373,373,373,373,373,373,373,374,374,374,374,374,374,374,374,374,374,
673 375,375,375,375,375,375,375,375,375,375,376,376,376,376,376,376,376,376,376,377,
674 377,377,377,377,377,377,377,377,377,378,378,378,378,378,378,378,378,378,379,379,
675 379,379,379,379,379,379,379,379,380,380,380,380,380,380,380,380,380,380,381,381,
676 381,381,381,381,381,381,381,382,382,382,382,382,382,382,382,382,382,383,383,383,
677 383,383,383,383,383,383,383,384,384,384,384,384,384,384,384,384,385,385,385,385,
678 385,385,385,385,385,385,386,386,386,386,386,386,386,386,386,386,387,387,387,387,
679 387,387,387,387,387,387,388,388,388,388,388,388,388,388,388,389,389,389,389,389,
680 389,389,389,389,389,390,390,390,390,390,390,390,390,390,390,391,391,391,391,391,
681 391,391,391,391,391,392,392,392,392,392,392,392,392,392,393,393,393,393,393,393,
682 393,393,393,393,394,394,394,394,394,394,394,394,394,394,395,395,395,395,395,395,
683 395,395,395,395,396,396,396,396,396,396,396,396,396,397,397,397,397,397,397,397,
684 397,397,397,398,398,398,398,398,398,398,398,398,398,399,399,399,399,399,399,399,
685 399,399,399,400,400,400,400,400,400,400,400,400,400,401,401,401,401,401,401,401,
686 401,401,402,402,402,402,402,402,402,402,402,402,403,403,403,403,403,403,403,403,
687 403,403,404,404,404,404,404,404,404,404,404,404,405,405,405,405,405,405,405,405,
688 405,405,406,406,406,406,406,406,406,406,406,406,407,407,407,407,407,407,407,407,
689 407,407,408,408,408,408,408,408,408,408,408,408,409,409,409,409,409,409,409,409,
690 409,410,410,410,410,410,410,410,410,410,410,411,411,411,411,411,411,411,411,411,
691 411,412,412,412,412,412,412,412,412,412,412,413,413,413,413,413,413,413,413,413,
692 413,414,414,414,414,414,414,414,414,414,414,415,415,415,415,415,415,415,415,415,
693 415,416,416,416,416,416,416,416,416,416,416,417,417,417,417,417,417,417,417,417,
694 417,418,418,418,418,418,418,418,418,418,418,419,419,419,419,419,419,419,419,419,
695 419,420,420,420,420,420,420,420,420,420,420,421,421,421,421,421,421,421,421,421,
696 421,422,422,422,422,422,422,422,422,422,422,423,423,423,423,423,423,423,423,423,
697 423,424,424,424,424,424,424,424,424,424,424,425,425,425,425,425,425,425,425,425,
698 425,426,426,426,426,426,426,426,426,426,426,427,427,427,427,427,427,427,427,427,
699 427,428,428,428,428,428,428,428,428,428,428,429,429,429,429,429,429,429,429,429,
700 429,430,430,430,430,430,430,430,430,430,430,431,431,431,431,431,431,431,431,431,
701 431,432,432,432,432,432,432,432,432,432,432,433,433,433,433,433,433,433,433,433,
702 433,434,434,434,434,434,434,434,434,434,434,435,435,435,435,435,435,435,435,435,
703 435,435,436,436,436,436,436,436,436,436,436,436,437,437,437,437,437,437,437,437,
704 437,437,438,438,438,438,438,438,438,438,438,438,439,439,439,439,439,439,439,439,
705 439,439,440,440,440,440,440,440,440,440,440,440,441,441,441,441,441,441,441,441,
706 441,441,442,442,442,442,442,442,442,442,442,442,443,443,443,443,443,443,443,443,
707 443,443,443,444,444,444,444,444,444,444,444,444,444,445,445,445,445,445,445,445,
708 445,445,445,446,446,446,446,446,446,446,446,446,446,447,447,447,447,447,447,447,
709 447,447,447,448,448,448,448,448,448,448,448,448,448,448,449,449,449,449,449,449,
710 449,449,449,449,450,450,450,450,450,450,450,450,450,450,451,451,451,451,451,451,
711 451,451,451,451,452,452,452,452,452,452,452,452,452,452,453,453,453,453,453,453,
712 453,453,453,453,453,454,454,454,454,454,454,454,454,454,454,455,455,455,455,455,
713 455,455,455,455,455,456,456,456,456,456,456,456,456,456,456,457,457,457,457,457,
714 457,457,457,457,457,457,458,458,458,458,458,458,458,458,458,458,459,459,459,459,
715 459,459,459,459,459,459,460,460,460,460,460,460,460,460,460,460,460,461,461,461,
716 461,461,461,461,461,461,461,462,462,462,462,462,462,462,462,462,462,463,463,463,
717 463,463,463,463,463,463,463,463,464,464,464,464,464,464,464,464,464,464,465,465,
718 465,465,465,465,465,465,465,465,466,466,466,466,466,466,466,466,466,466,466,467,
719 467,467,467,467,467,467,467,467,467,468,468,468,468,468,468,468,468,468,468,469,
720 469,469,469,469,469,469,469,469,469,469,470,470,470,470,470,470,470,470,470,470,
721 471,471,471,471,471,471,471,471,471,471,472,472,472,472,472,472,472,472,472,472,
722 472,473,473,473,473,473,473,473,473,473,473,474,474,474,474,474,474,474,474,474,
723 474,475,475,475,475,475,475,475,475,475,475,475,476,476,476,476,476,476,476,476,
724 476,476,477,477,477,477,477,477,477,477,477,477,477,478,478,478,478,478,478,478,
725 478,478,478,479,479,479,479,479,479,479,479,479,479,479,480,480,480,480,480,480,
726 480,480,480,480,481,481,481,481,481,481,481,481,481,481,482,482,482,482,482,482,
727 482,482,482,482,482,483,483,483,483,483,483,483,483,483,483,484,484,484,484,484,
728 484,484,484,484,484,484,485,485,485,485,485,485,485,485,485,485,486,486,486,486,
729 486,486,486,486,486,486,486,487,487,487,487,487,487,487,487,487,487,488,488,488,
730 488,488,488,488,488,488,488,488,489,489,489,489,489,489,489,489,489,489,490,490,
731 490,490,490,490,490,490,490,490,490,491,491,491,491,491,491,491,491,491,491,492,
732 492,492,492,492,492,492,492,492,492,492,493,493,493,493,493,493,493,493,493,493,
733 494,494,494,494,494,494,494,494,494,494,494,495,495,495,495,495,495,495,495,495,
734 495,496,496,496,496,496,496,496,496,496,496,496,497,497,497,497,497,497,497,497,
735 497,497,497,498,498,498,498,498,498,498,498,498,498,499,499,499,499,499,499,499,
736 499,499,499,499,500,500,500,500,500,500,500,500,500,500,501,501,501,501,501,501,
737 501,501,501,501,501,502,502,502,502,502,502,502,502,502,502,503,503,503,503,503,
738 503,503,503,503,503,503,504,504,504,504,504,504,504,504,504,504,504,505,505,505,
739 505,505,505,505,505,505,505,506,506,506,506,506,506,506,506,506,506,506,507,507,
740 507,507,507,507,507,507,507,507,507,508,508,508,508,508,508,508,508,508,508,509,
741 509,509,509,509,509,509,509,509,509,509,510,510,510,510,510,510,510,510,510,510,
742 510,511,511,511,511,511,511,511,511,511,511,512,512,512,512,512 };
744 static const int order[32] =
745 { 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
746 2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 };
748 static const long sampr_index[2][3] =
749 { { 22050, 24000, 16000 }, /* MPEG 2 */
750 { 44100, 48000, 32000 } }; /* MPEG 1 */
752 static const long bitr_index[2][15] =
753 { {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160}, /* MPEG 2 */
754 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} }; /* MPEG 1 */
756 static const int num_bands[3][15] =
757 { {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32},
758 {0,10,10,10,10,10,12,14, 18, 24, 26, 28, 30, 32, 32},
759 {0,10,12,14,18,24,26,28, 30, 32, 32, 32, 32, 32, 32} };
761 static const int cx_const[9] =
762 { 16135, 10531, 5604, 15396, -2845,-12551, 14189, 8192, 16384 };
764 static const int ca_const[8] =
765 {-16859,-15458,-10269, -5961, -3099, -1342, -465, -121 };
767 static const int cs_const[8] =
768 { 28098, 28893, 31117, 32221, 32621, 32740, 32765, 32768 };
770 static const short enwindow_const[15*27+24] =
771 { 0, 65, 593, 1766, 22228, 2115, 611, 62,
772 8, 119, 1419, 10564,-11659,-1635,-154, -9,
773 -8, -119,-1419,-10564, 11659, 1635, 154, 9, 464, 100, 91,
774 0, 69, 604, 1635, 23148, 2363, 643, 62,
775 7, 107, 1368, 10449,-12733,-1818,-180,-11,
776 -7, -107,-1368,-10449, 12733, 1818, 180, 11, 420, 200, 164,
777 0, 72, 608, 1465, 23979, 2600, 671, 63,
778 7, 94, 1305, 10265,-13818,-2004,-207,-12,
779 -7, -94,-1305,-10265, 13818, 2004, 207, 12, 380, 297, 220,
780 0, 76, 606, 1256, 24718, 2825, 693, 63,
781 6, 81, 1232, 10016,-14908,-2192,-236,-14,
782 -6, -81,-1232,-10016, 14908, 2192, 236, 14, 342, 392, 262,
783 0, 78, 597, 1007, 25359, 3033, 712, 63,
784 6, 68, 1150, 9706,-15995,-2380,-267,-15,
785 -6, -68,-1150, -9706, 15995, 2380, 267, 15, 307, 483, 289,
786 0, 80, 580, 719, 25901, 3224, 726, 62,
787 6, 54, 1060, 9343,-17072,-2565,-299,-17,
788 -6, -54,-1060, -9343, 17072, 2565, 299, 17, 274, 569, 304,
789 -1, 82, 555, 391, 26339, 3395, 735, 61,
790 5, 40, 963, 8930,-18131,-2747,-332,-19,
791 -5, -40, -963, -8930, 18131, 2747, 332, 19, 242, 650, 307,
792 -1, 83, 523, 26, 26672, 3545, 740, 60,
793 5, 27, 861, 8474,-19164,-2923,-366,-21,
794 -5, -27, -861, -8474, 19164, 2923, 366, 21, 212, 724, 300,
795 -1, 83, 482, -376, 26900, 3672, 739, 58,
796 4, 14, 756, 7981,-20163,-3092,-401,-24,
797 -4, -14, -756, -7981, 20163, 3092, 401, 24, 183, 792, 283,
798 -1, 82, 433, -812, 27022, 3776, 735, 56,
799 4, 1, 648, 7456,-21122,-3250,-435,-26,
800 -4, -1, -648, -7456, 21122, 3250, 435, 26, 155, 851, 258,
801 -1, 81, 376, -1281, 27038, 3855, 726, 54,
802 3, -11, 539, 6907,-22032,-3397,-470,-28,
803 -3, 11, -539, -6907, 22032, 3397, 470, 28, 128, 903, 226,
804 -1, 78, 312, -1778, 26951, 3910, 713, 52,
805 3, -22, 430, 6338,-22887,-3530,-503,-31,
806 -3, 22, -430, -6338, 22887, 3530, 503, 31, 102, 946, 188,
807 -2, 75, 239, -2302, 26761, 3941, 696, 49,
808 3, -33, 322, 5757,-23678,-3648,-537,-34,
809 -3, 33, -322, -5757, 23678, 3648, 537, 34, 76, 980, 145,
810 -2, 70, 160, -2848, 26472, 3948, 676, 47,
811 3, -42, 217, 5167,-24399,-3749,-568,-36,
812 -3, 42, -217, -5167, 24399, 3749, 568, 36, 50, 1004, 99,
813 -2, 65, 74, -3412, 26087, 3931, 653, 44,
814 2, -51, 115, 4577,-25045,-3830,-599,-39,
815 -2, 51, -115, -4577, 25045, 3830, 599, 39, 25, 1019, 50,
817 25610,3891,627,42,-3990,-18,58,-2,
818 21226,-21226,10604,-10604,1860,-1860,1458,-1458,576,-576,130,-130,60,-60,8,-8
821 static const int win_const[18][4] = {
822 { -3072, -134, -146, 3352 },
823 { -2747, -362, -471, 3579 },
824 { -2387, -529, -831, 3747 },
825 { -2004, -632,-1214, 3850 },
826 { -1609, -666,-1609, 3884 },
827 { -1214, -632,-2004, 3850 },
828 { -831, -529,-2387, 3747 },
829 { -471, -362,-2747, 3579 },
830 { -146, -134,-3072, 3352 },
831 { 134,-3072,-3352, -146 },
832 { 362,-2747,-3579, -471 },
833 { 529,-2387,-3747, -831 },
834 { 632,-2004,-3850,-1214 },
835 { 666,-1609,-3884,-1609 },
836 { 632,-1214,-3850,-2004 },
837 { 529, -831,-3747,-2387 },
838 { 362, -471,-3579,-2747 },
839 { 134, -146,-3352,-3072 } };
841 /* forward declarations */
842 static int HuffmanCode( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
843 static int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
844 static void putbits(uint32_t val, uint32_t nbit);
845 static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
846 uint32_t len, int *bits);
847 static int find_best_3( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
848 uint32_t len, int *bits);
849 static int count_bit1 ( short *ix, uint32_t start, uint32_t end, int *bits );
850 static int count_bigv ( short *ix, uint32_t start, uint32_t end, int table0, int table1,
851 int *bits);
854 static void encodeSideInfo( side_info_t si[2][2] )
856 int gr, ch, header;
857 uint32_t cc=0, sz=0;
860 * MPEG header layout:
861 * AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
862 * A (31-21) = frame sync
863 * B (20-19) = MPEG type
864 * C (18-17) = MPEG layer
865 * D (16) = protection bit
866 * E (15-12) = bitrate index
867 * F (11-10) = samplerate index
868 * G (9) = padding bit
869 * H (8) = private bit
870 * I (7-6) = channel mode
871 * J (5-4) = mode extension (jstereo only)
872 * K (3) = copyright bit
873 * L (2) = original
874 * M (1-0) = emphasis
877 header = (0xfff00000) | /* frame sync (AAAAAAAAA AAA)
878 mp3 type (upper): 1 (B) */
879 (0x01 << 17) | /* mp3 layer: 01 (CC) */
880 ( 0x1 << 16) | /* mp3 crc: 1 (D) */
881 ( 0x1 << 2); /* mp3 org: 1 (L) */
882 header |= cfg.mpg.type << 19;
883 header |= cfg.mpg.bitr_id << 12;
884 header |= cfg.mpg.smpl_id << 10;
885 header |= cfg.mpg.padding << 9;
886 header |= cfg.mpg.mode << 6;
887 /* no emphasis (bits 0-1) */
888 putbits( header, 32 );
890 if(cfg.mpg.type == 1)
891 { /* MPEG1 */
892 if(cfg.channels == 2) { putlong( 0, 20); }
893 else { putlong( 0, 18); }
895 for(gr=0; gr<cfg.granules; gr++)
896 for(ch=0; ch<cfg.channels; ch++)
898 side_info_t *gi = &si[gr][ch];
900 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
901 putlong( gi->address3>>1, 9 );
902 putlong( gi->global_gain, 8 );
903 putlong( 9, 4 ); /* set scale_facs compr type */
904 putlong( gi->table_select[0], 6 );
905 putlong( gi->table_select[1], 5 );
906 putlong( gi->table_select[2], 5 );
907 putlong( gi->region_0_1, 7 );
908 putlong( 1 , 2 ); /* set scale_facs to 1bit */
909 putlong( gi->table_select[3], 1 );
912 else
913 { /* MPEG2 */
914 if(cfg.channels == 2) { putlong( 0, 10); }
915 else { putlong( 0, 9); }
917 for(ch=0; ch<cfg.channels; ch++)
919 side_info_t *gi = &si[0][ch];
921 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
922 putlong( gi->address3>>1, 9 );
923 putlong( gi->global_gain, 8 );
924 putlong( 0xCA, 9 ); /* set scale_facs compr type */
925 putlong( gi->table_select[0], 6 );
926 putlong( gi->table_select[1], 5 );
927 putlong( gi->table_select[2], 5 );
928 putlong( gi->region_0_1 , 7 );
929 putlong( 1 , 1 ); /* set scale_facs to 1bit */
930 putlong( gi->table_select[3], 1 );
933 /* flush remaining bits */
934 putbits(cc, sz);
937 /* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS,
938 as well as the definitions of the side information on pages 26 and 27. */
939 static void Huffmancodebits( short *ix, char *xr_sign, side_info_t *gi )
941 int region1 = gi->address1;
942 int region2 = gi->address2;
943 int bigvals = gi->address3;
944 int count1 = bigvals + (gi->count1 << 2);
945 int stuffBits = 0;
946 int bits = 0;
947 int i, v;
949 for(i=v=0; i<32; i+=2)
950 v |= band_scale_f[i>>1] << (30-i);
951 putbits(v, 32); // store scale_facs (part1)
953 for(v=0; i<42; i+=2)
954 v |= band_scale_f[i>>1] << (40-i);
955 putbits(v, 10); // store scale_facs (part2)
957 if(region1 > 0)
958 bits += HuffmanCode(ix, xr_sign, 0 , region1, gi->table_select[0]);
960 if(region2 > region1)
961 bits += HuffmanCode(ix, xr_sign, region1, region2, gi->table_select[1]);
963 if(bigvals > region2)
964 bits += HuffmanCode(ix, xr_sign, region2, bigvals, gi->table_select[2]);
966 if(count1 > bigvals)
967 bits += HuffmanCod1(ix, xr_sign, bigvals, count1, gi->table_select[3]);
969 if((stuffBits = gi->part2_3_length - bits) > 0)
971 int stuffWords = stuffBits >> 5;
972 int remainBits = stuffBits & 31;
974 if( remainBits )
975 putbits( ~0, remainBits );
977 while( stuffWords-- )
978 putbits( ~0, 32 ); /* Huffman code tables leed to padding ones */
982 int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int tbl)
984 uint32_t cc=0, sz=0;
985 uint32_t i, d, p;
986 int sumbit=0, s=0, l=0, v, w, x, y;
987 #define sgnv xr_sign[i+0]
988 #define sgnw xr_sign[i+1]
989 #define sgnx xr_sign[i+2]
990 #define sgny xr_sign[i+3]
992 for(i=begin; i<end; i+=4)
994 v = ix[i+0];
995 w = ix[i+1];
996 x = ix[i+2];
997 y = ix[i+3];
998 p = (v << 3) + (w << 2) + (x << 1) + y;
1000 switch(p)
1002 case 0: l=0; s = 0; break;
1003 case 1: l=1; s = sgny; break;
1004 case 2: l=1; s = sgnx; break;
1005 case 3: l=2; s = (sgnx << 1) + sgny; break;
1006 case 4: l=1; s = sgnw; break;
1007 case 5: l=2; s = (sgnw << 1) + sgny; break;
1008 case 6: l=2; s = (sgnw << 1) + sgnx; break;
1009 case 7: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break;
1010 case 8: l=1; s = sgnv; break;
1011 case 9: l=2; s = (sgnv << 1) + sgny; break;
1012 case 10: l=2; s = (sgnv << 1) + sgnx; break;
1013 case 11: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break;
1014 case 12: l=2; s = (sgnv << 1) + sgnw; break;
1015 case 13: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break;
1016 case 14: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break;
1017 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break;
1020 d = (ht_count[tbl][0][p] << l) + s;
1021 l = ht_count[tbl][1][p];
1022 putlong( d, l );
1023 sumbit += l;
1026 /* flush remaining bits */
1027 putbits(cc, sz);
1029 return sumbit;
1032 /* Implements the pseudocode of page 98 of the IS */
1033 int HuffmanCode(short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table)
1035 uint32_t cc=0, sz=0, code;
1036 uint32_t i, xl=0, yl=0, idx;
1037 int x, y, bit, sumbit=0;
1038 #define sign_x xr_sign[i+0]
1039 #define sign_y xr_sign[i+1]
1041 if(table == 0)
1042 return 0;
1044 if( table > 15 )
1045 { /* ESC-table is used */
1046 uint32_t linbits = ht_big[table-16].linbits;
1047 uint16_t *hffcode = table < 24 ? t16HB : t24HB;
1048 uint8_t *hlen = table < 24 ? t16l : t24l;
1050 for(i=begin; i<end; i+=2)
1052 x = ix[ i ];
1053 y = ix[i+1];
1055 if(x > 14) { xl = x - 15; x = 15; }
1056 if(y > 14) { yl = y - 15; y = 15; }
1058 idx = x * 16 + y;
1059 code = hffcode[idx];
1060 bit = hlen [idx];
1062 if(x)
1064 if(x > 14)
1066 code = (code << linbits) | xl;
1067 bit += linbits;
1070 code = (code << 1) | sign_x;
1071 bit += 1;
1074 if(y)
1076 if(y > 14)
1078 if(bit + linbits + 1 > 32)
1080 putlong( code, bit );
1081 sumbit += bit;
1082 code = bit = 0;
1085 code = (code << linbits) | yl;
1086 bit += linbits;
1089 code = (code << 1) | sign_y;
1090 bit += 1;
1093 putlong( code, bit );
1094 sumbit += bit;
1097 else
1098 { /* No ESC-words */
1099 const struct huffcodetab *h = &ht[table];
1101 for(i=begin; i<end; i+=2)
1103 x = ix[i];
1104 y = ix[i+1];
1106 idx = x * h->len + y;
1107 code = h->table[idx];
1108 bit = h->hlen [idx];
1110 if(x)
1112 code = (code << 1) | sign_x;
1113 bit += 1;
1116 if(y)
1118 code = (code << 1) | sign_y;
1119 bit += 1;
1122 putlong( code, bit );
1123 sumbit += bit;
1127 /* flush remaining bits */
1128 putbits(cc, sz);
1130 return sumbit;
1133 void putbits(uint32_t val, uint32_t nbit)
1135 int new_bitpos = CodedData.bitpos + nbit;
1136 int ptrpos = CodedData.bitpos >> 5;
1138 val = val & (0xffffffff >> (32 - nbit));
1140 /* data fit in one uint32_t */
1141 if(((new_bitpos - 1) >> 5) == ptrpos)
1142 CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31);
1143 else
1145 CodedData.bbuf[ptrpos ] |= val >> ((new_bitpos - 32) & 31);
1146 CodedData.bbuf[ptrpos+1] |= val << ((32 - new_bitpos) & 31);
1149 CodedData.bitpos = new_bitpos;
1152 /***************************************************************************/
1153 /* Choose the Huffman table that will encode ix[begin..end] with */
1154 /* the fewest bits. */
1155 /* Note: This code contains knowledge about the sizes and characteristic */
1156 /* of the Huffman tables as defined in the IS (Table B.7), and will not */
1157 /* work with any arbitrary tables. */
1158 /***************************************************************************/
1159 static int choose_table( short *ix, uint32_t begin, uint32_t end, int *bits )
1161 uint32_t i;
1162 int max, table0, table1;
1164 for(i=begin,max=0; i<end; i++)
1165 if(ix[i] > max)
1166 max = ix[i];
1168 if(max < 16)
1170 /* tables without linbits */
1171 /* indx: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1172 /* len: 0, 2, 3, 3, 0, 4, 4, 6, 6, 6, 8, 8, 8, 16, 0, 16 */
1173 switch(max)
1175 case 0: return 0;
1176 case 1: return count_bit1(ix, begin, end, bits);
1177 case 2: return 2 + find_best_2(ix, begin, end, tab23, 3, bits);
1178 case 3: return 5 + find_best_2(ix, begin, end, tab56, 4, bits);
1179 case 4:
1180 case 5: return 7 + find_best_3(ix, begin, end, tab789, 6, bits);
1181 case 6:
1182 case 7: return 10 + find_best_3(ix, begin, end, tabABC, 8, bits);
1183 default: return 13 + find_best_2(ix, begin, end, tab1315, 16, bits) * 2;
1186 else
1188 /* tables with linbits */
1189 max -= 15;
1191 for(table0=0; table0<8; table0++)
1192 if(ht_big[table0].linmax >= max)
1193 break;
1195 for(table1=8; table1<16; table1++)
1196 if(ht_big[table1].linmax >= max)
1197 break;
1199 return 16 + count_bigv(ix, begin, end, table0, table1, bits);
1203 int find_best_2(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1204 uint32_t len, int *bits)
1206 uint32_t i, sum = 0;
1208 for(i=start; i<end; i+=2)
1209 sum += table[ix[i] * len + ix[i+1]];
1211 if((sum & 0xffff) <= (sum >> 16))
1213 *bits = (sum & 0xffff);
1214 return 1;
1216 else
1218 *bits = sum >> 16;
1219 return 0;
1223 int find_best_3(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1224 uint32_t len, int *bits)
1226 uint32_t i, j, sum = 0;
1227 int sum1 = 0;
1228 int sum2 = 0;
1229 int sum3 = 0;
1231 /* avoid overflow in packed additions: 78*13 < 1024 */
1232 for(i=start; i<end; )
1234 j = i + 2*78 > end ? end : i + 2*78;
1236 for(sum=0; i<j; i+=2)
1237 sum += table[ix[i] * len + ix[i+1]];
1239 sum1 += (sum >> 20);
1240 sum2 += (sum >> 10) & 0x3ff;
1241 sum3 += (sum >> 0) & 0x3ff;
1244 i = 0;
1245 if(sum1 > sum2) { sum1 = sum2; i = 1; }
1246 if(sum1 > sum3) { sum1 = sum3; i = 2; }
1248 *bits = sum1;
1250 return i;
1253 /*************************************************************************/
1254 /* Function: Count the number of bits necessary to code the subregion. */
1255 /*************************************************************************/
1256 int count_bit1(short *ix, uint32_t start, uint32_t end, int *bits )
1258 uint32_t i, sum = 0;
1260 for(i=start; i<end; i+=2)
1261 sum += t1l[4 + ix[i] * 2 + ix[i+1]];
1263 *bits = sum;
1265 return 1; /* this is table1 */
1268 int count_bigv(short *ix, uint32_t start, uint32_t end, int table0,
1269 int table1, int *bits )
1271 uint32_t i, sum0, sum1, sum=0, bigv=0, x, y;
1273 /* ESC-table is used */
1274 for(i=start; i<end; i+=2)
1276 x = ix[i];
1277 y = ix[i+1];
1279 if(x > 14) { x = 15; bigv++; }
1280 if(y > 14) { y = 15; bigv++; }
1282 sum += tab1624[x * 16 + y];
1285 sum0 = (sum >> 16) + bigv * ht_big[table0].linbits;
1286 sum1 = (sum & 0xffff) + bigv * ht_big[table1].linbits;
1288 if(sum0 <= sum1)
1290 *bits = sum0;
1291 return table0;
1293 else
1295 *bits = sum1;
1296 return table1;
1300 /*************************************************************************/
1301 /* Function: Calculation of rzero, count1, address3 */
1302 /* (Partitions ix into big values, quadruples and zeros). */
1303 /*************************************************************************/
1304 static int calc_runlen( short *ix, side_info_t *si )
1306 int p, i, sum = 0;
1308 for(i=SAMPL2; i-=2; )
1309 if(*(uint32_t*)&ix[i-2]) /* !!!! short *ix; !!!!! */
1310 break;
1312 si->count1 = 0;
1314 for( ; i>3; i-=4)
1316 int v = ix[i-1];
1317 int w = ix[i-2];
1318 int x = ix[i-3];
1319 int y = ix[i-4];
1321 if((v | w | x | y) <= 1)
1323 p = (y<<3) + (x<<2) + (w<<1) + (v);
1325 sum += tab01[p];
1327 si->count1++;
1329 else break;
1332 si->address3 = i;
1334 if((sum >> 16) < (sum & 0xffff))
1336 si->table_select[3] = 0;
1337 return sum >> 16;
1339 else
1341 si->table_select[3] = 1;
1342 return sum & 0xffff;
1347 /*************************************************************************/
1348 /* Function: Quantization of the vector xr ( -> ix) */
1349 /*************************************************************************/
1350 static int quantize_int(int *xr, short *ix, side_info_t *si)
1352 unsigned int i, idx, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 };
1354 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4;
1356 /* check for possible 'out of range' values */
1357 if(((si->max_val + 256) >> 8) * s >= (65536 << 8))
1358 return 0;
1360 if(((si->max_val + 256) >> 8) * s < (4096 << 8))
1361 { /* all values fit the table size */
1362 for(i=SAMPL2; i--; )
1363 ix[i] = int2idx[(xr[i] * s + 0x8000) >> 16];
1365 else
1366 { /* check each index wether it fits the table */
1367 for(i=SAMPL2; i--; )
1369 idx = (xr[i] * s + 0x08000) >> 16;
1371 if(idx > 4095) ix[i] = int2idx[(idx + 8) >> 4] << 3;
1372 else ix[i] = int2idx[idx];
1376 return 1;
1379 /*************************************************************************/
1380 /* subdivides the bigvalue region which will use separate Huffman tables */
1381 /*************************************************************************/
1382 static void subdivide(side_info_t *si)
1384 int scfb, count0, count1;
1386 if( !si->address3 )
1387 { /* no bigvalue region */
1388 si->region_0_1 = 0;
1389 si->address1 = 0;
1390 si->address2 = 0;
1392 else
1394 /* Calculate scale factor band index */
1395 for(scfb=0; scalefac[scfb] < si->address3; )
1396 scfb++;
1398 count0 = subdv_table[scfb].region0_cnt;
1399 count1 = subdv_table[scfb].region1_cnt;
1401 si->region_0_1 = (count0 << 3) | count1;
1402 si->address1 = scalefac[count0 + 1];
1403 si->address2 = scalefac[count0 + 1 + count1 + 1];
1407 /*******************************************************************/
1408 /* Count the number of bits necessary to code the bigvalues region */
1409 /*******************************************************************/
1410 static int bigv_bitcount(short *ix, side_info_t *gi)
1412 int b1=0, b2=0, b3=0;
1414 /* Select huffman code tables for bigvalues regions */
1415 gi->table_select[0] = 0;
1416 gi->table_select[1] = 0;
1417 gi->table_select[2] = 0;
1419 if( gi->address1 > 0 ) /* region0 */
1420 gi->table_select[0] = choose_table(ix, 0 , gi->address1, &b1);
1422 if( gi->address2 > gi->address1 ) /* region1 */
1423 gi->table_select[1] = choose_table(ix, gi->address1, gi->address2, &b2);
1425 if( gi->address3 > gi->address2 ) /* region2 */
1426 gi->table_select[2] = choose_table(ix, gi->address2, gi->address3, &b3);
1428 return b1+b2+b3;
1431 static int quantize_and_count_bits(int *xr, short *ix, side_info_t *si)
1433 int bits = 10000;
1435 if(quantize_int(xr, ix, si))
1437 bits = calc_runlen(ix, si); /* rzero,count1,address3 */
1438 subdivide(si); /* bigvalues sfb division */
1439 bits += bigv_bitcount(ix,si); /* bit count */
1442 return bits;
1445 /************************************************************************/
1446 /* The code selects the best quantStep for a particular set of scalefacs*/
1447 /************************************************************************/
1448 static int inner_loop(int *xr, int max_bits, side_info_t *si)
1450 int bits;
1452 while((bits=quantize_and_count_bits(xr, enc_data, si)) < max_bits-64)
1454 if(si->quantStep == 0)
1455 break;
1457 if(si->quantStep <= 2)
1458 si->quantStep = 0;
1459 else
1460 si->quantStep -= 2;
1463 while(bits > max_bits)
1465 si->quantStep++;
1466 bits = quantize_and_count_bits(xr, enc_data, si);
1469 return bits;
1472 static void iteration_loop(int *xr, side_info_t *si, int gr_cnt)
1474 int remain, tar_bits, max_bits = cfg.mean_bits;
1476 /* distribute reserved bits to remaining granules */
1477 tar_bits = max_bits + (cfg.ResvSize / gr_cnt & ~7);
1478 if(tar_bits > max_bits + max_bits/2)
1479 tar_bits = max_bits + max_bits/2;
1481 si->part2_3_length = inner_loop(xr, tar_bits, si);
1482 si->global_gain = si->quantStep + 142 - si->additStep;
1484 /* unused bits of the reservoir can be used for remaining granules */
1485 cfg.ResvSize += max_bits - si->part2_3_length;
1487 /* end: distribute the reserved bits to one or two granules */
1488 if(gr_cnt == 1)
1490 si->part2_3_length += cfg.ResvSize;
1491 /* mp3 format allows max 12bits for granule length */
1492 if(si->part2_3_length > 4092)
1494 remain = (si->part2_3_length - 4092 + 31) >> 5;
1495 si->part2_3_length -= remain << 5;
1496 si[-1].part2_3_length += remain << 5;
1498 while(remain--)
1499 putbits(~0, 32);
1505 /* returns sum_j=0^31 a[j]*cos(PI*j*(k+1/2)/32), 0<=k<32 */
1506 void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT]) ICODE_ATTR;
1507 void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT])
1509 int k, i, u, v;
1510 short *wp, *x1, *x2;
1512 #ifdef CPU_COLDFIRE
1513 int s0, s1, t0, t1;
1515 for(k=0; k<18; k++, wk+=64, sb0+=SBLIMIT, sb1+=SBLIMIT)
1517 wp = enwindow;
1518 x1 = wk;
1519 x2 = x1 - 124;
1521 for(i=-15; i<0; i++)
1523 asm volatile(
1524 "move.l (-224*4,%[x2]), %%d4\n" /* d4 = x2[-224] */
1525 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1526 "mac.w %%d0u, %%d4u, %%acc0\n"
1527 "mac.w %%d0u, %%d4l, (-160*4,%[x2]), %%d4, %%acc1\n"
1528 "mac.w %%d0l, %%d4u, %%acc0\n"
1529 "mac.w %%d0l, %%d4l, ( -96*4,%[x2]), %%d4, %%acc1\n"
1530 "mac.w %%d1u, %%d4u, %%acc0\n"
1531 "mac.w %%d1u, %%d4l, ( -32*4,%[x2]), %%d4, %%acc1\n"
1532 "mac.w %%d1l, %%d4u, %%acc0\n"
1533 "mac.w %%d1l, %%d4l, ( 32*4,%[x2]), %%d4, %%acc1\n"
1534 "mac.w %%d2u, %%d4u, %%acc0\n"
1535 "mac.w %%d2u, %%d4l, ( 96*4,%[x2]), %%d4, %%acc1\n"
1536 "mac.w %%d2l, %%d4u, %%acc0\n"
1537 "mac.w %%d2l, %%d4l, ( 160*4,%[x2]), %%d4, %%acc1\n"
1538 "mac.w %%d3u, %%d4u, %%acc0\n"
1539 "mac.w %%d3u, %%d4l, ( 224*4,%[x2]), %%d4, %%acc1\n"
1540 "mac.w %%d3l, %%d4u, %%acc0\n"
1541 "mac.w %%d3l, %%d4l, (-256*4,%[x1]), %%d4, %%acc1\n"
1542 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */
1543 "mac.w %%d0u, %%d4u, %%acc0\n"
1544 "mac.w %%d0u, %%d4l, (-192*4,%[x1]), %%d4, %%acc1\n"
1545 "mac.w %%d0l, %%d4u, %%acc0\n"
1546 "mac.w %%d0l, %%d4l, (-128*4,%[x1]), %%d4, %%acc1\n"
1547 "mac.w %%d1u, %%d4u, %%acc0\n"
1548 "mac.w %%d1u, %%d4l, ( -64*4,%[x1]), %%d4, %%acc1\n"
1549 "mac.w %%d1l, %%d4u, %%acc0\n"
1550 "mac.w %%d1l, %%d4l, ( 0*4,%[x1]), %%d4, %%acc1\n"
1551 "mac.w %%d2u, %%d4u, %%acc0\n"
1552 "mac.w %%d2u, %%d4l, ( 64*4,%[x1]), %%d4, %%acc1\n"
1553 "mac.w %%d2l, %%d4u, %%acc0\n"
1554 "mac.w %%d2l, %%d4l, ( 128*4,%[x1]), %%d4, %%acc1\n"
1555 "mac.w %%d3u, %%d4u, %%acc0\n"
1556 "mac.w %%d3u, %%d4l, ( 192*4,%[x1]), %%d4, %%acc1\n"
1557 "mac.w %%d3l, %%d4u, %%acc0\n"
1558 "mac.w %%d3l, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n"
1559 "movclr.l %%acc0, %%d0\n"
1560 "move.l %%d0, %[s0]\n"
1561 "movclr.l %%acc1, %%d0\n"
1562 "move.l %%d0, %[s1]\n"
1564 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1565 "mac.w %%d0u, %%d4u, %%acc0\n"
1566 "mac.w %%d0u, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n"
1567 "mac.w %%d0l, %%d4u, %%acc0\n"
1568 "mac.w %%d0l, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n"
1569 "mac.w %%d1u, %%d4u, %%acc0\n"
1570 "mac.w %%d1u, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n"
1571 "mac.w %%d1l, %%d4u, %%acc0\n"
1572 "mac.w %%d1l, %%d4l, ( -32*4,%[x1]), %%d4, %%acc1\n"
1573 "mac.w %%d2u, %%d4u, %%acc0\n"
1574 "mac.w %%d2u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n"
1575 "mac.w %%d2l, %%d4u, %%acc0\n"
1576 "mac.w %%d2l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n"
1577 "mac.w %%d3u, %%d4u, %%acc0\n"
1578 "mac.w %%d3u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n"
1579 "mac.w %%d3l, %%d4u, %%acc0\n"
1580 "mac.w %%d3l, %%d4l, ( 256*4,%[x2]), %%d4, %%acc1\n"
1581 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */
1582 "mac.w %%d0u, %%d4u, %%acc0\n"
1583 "mac.w %%d0u, %%d4l, ( 192*4,%[x2]), %%d4, %%acc1\n"
1584 "mac.w %%d0l, %%d4u, %%acc0\n"
1585 "mac.w %%d0l, %%d4l, ( 128*4,%[x2]), %%d4, %%acc1\n"
1586 "mac.w %%d1u, %%d4u, %%acc0\n"
1587 "mac.w %%d1u, %%d4l, ( 64*4,%[x2]), %%d4, %%acc1\n"
1588 "mac.w %%d1l, %%d4u, %%acc0\n"
1589 "mac.w %%d1l, %%d4l, ( 0*4,%[x2]), %%d4, %%acc1\n"
1590 "mac.w %%d2u, %%d4u, %%acc0\n"
1591 "mac.w %%d2u, %%d4l, ( -64*4,%[x2]), %%d4, %%acc1\n"
1592 "mac.w %%d2l, %%d4u, %%acc0\n"
1593 "mac.w %%d2l, %%d4l, (-128*4,%[x2]), %%d4, %%acc1\n"
1594 "mac.w %%d3u, %%d4u, %%acc0\n"
1595 "mac.w %%d3u, %%d4l, (-192*4,%[x2]), %%d4, %%acc1\n"
1596 "mac.w %%d3l, %%d4u, %%acc0\n"
1597 "mac.w %%d3l, %%d4l, %%acc1\n"
1598 "movclr.l %%acc0, %%d0\n"
1599 "move.l %%d0, %[t0]\n"
1600 "movclr.l %%acc1, %%d0\n"
1601 "move.l %%d0, %[t1]\n"
1603 : [x1] "+a" (x1), [x2] "+a" (x2), [s0] "+m" (s0), [t0] "+m" (t0),
1604 [s1] "+m" (s1), [t1] "+m" (t1)
1605 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4");
1607 sb0[30+i*2] = shft4(t0) + shft13(s0) * wp[24];
1608 sb0[31+i*2] = shft13(t0) * wp[25] - shft13(s0) * wp[26];
1609 sb1[30+i*2] = shft4(t1) + shft13(s1) * wp[24];
1610 sb1[31+i*2] = shft13(t1) * wp[25] - shft13(s1) * wp[26];
1611 wp += 27;
1612 x1 -= 2;
1613 x2 += 2;
1616 asm volatile(
1617 "move.l ( -32*4,%[x1]), %%d4\n" /* d4 = x1[-32] */
1618 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1620 "mac.w %%d0u, %%d4u, %%acc0\n"
1621 "mac.w %%d0u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n"
1622 "mac.w %%d0l, %%d4u, %%acc0\n"
1623 "mac.w %%d0l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n"
1624 "mac.w %%d1u, %%d4u, %%acc0\n"
1625 "mac.w %%d1u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n"
1626 "mac.w %%d1l, %%d4u, %%acc0\n"
1627 "mac.w %%d1l, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n"
1628 "mac.w %%d2u, %%d4u, %%acc0\n"
1629 "mac.w %%d2u, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n"
1630 "mac.w %%d2l, %%d4u, %%acc0\n"
1631 "mac.w %%d2l, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n"
1632 "mac.w %%d3u, %%d4u, %%acc0\n"
1633 "mac.w %%d3u, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n"
1634 "mac.w %%d3l, %%d4u, %%acc0\n"
1635 "mac.w %%d3l, %%d4l, ( -16*4,%[x1]), %%d4, %%acc1\n"
1636 "movclr.l %%acc0, %%d0\n"
1637 "move.l %%d0, %[s0]\n"
1638 "movclr.l %%acc1, %%d0\n"
1639 "move.l %%d0, %[s1]\n"
1641 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */
1642 "mac.w %%d0u, %%d4u, %%acc0\n"
1643 "mac.w %%d0u, %%d4l, ( -48*4,%[x1]), %%d4, %%acc1\n"
1644 "mac.w %%d1u, %%d4u, %%acc0\n"
1645 "mac.w %%d1u, %%d4l, ( 16*4,%[x1]), %%d4, %%acc1\n"
1646 "mac.w %%d1l, %%d4u, %%acc0\n"
1647 "mac.w %%d1l, %%d4l, ( -80*4,%[x1]), %%d4, %%acc1\n"
1648 "mac.w %%d2u, %%d4u, %%acc0\n"
1649 "mac.w %%d2u, %%d4l, ( 48*4,%[x1]), %%d4, %%acc1\n"
1650 "mac.w %%d2u, %%d4u, %%acc0\n"
1651 "mac.w %%d2u, %%d4l, (-112*4,%[x1]), %%d4, %%acc1\n"
1652 "mac.w %%d3u, %%d4u, %%acc0\n"
1653 "mac.w %%d3u, %%d4l, ( 80*4,%[x1]), %%d4, %%acc1\n"
1654 "mac.w %%d3l, %%d4u, %%acc0\n"
1655 "mac.w %%d3l, %%d4l, (-144*4,%[x1]), %%d4, %%acc1\n"
1656 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */
1657 "mac.w %%d0u, %%d4u, %%acc0\n"
1658 "mac.w %%d0u, %%d4l, ( 112*4,%[x1]), %%d4, %%acc1\n"
1659 "mac.w %%d0u, %%d4u, %%acc0\n"
1660 "mac.w %%d0u, %%d4l, (-176*4,%[x1]), %%d4, %%acc1\n"
1661 "mac.w %%d1u, %%d4u, %%acc0\n"
1662 "mac.w %%d1u, %%d4l, ( 144*4,%[x1]), %%d4, %%acc1\n"
1663 "mac.w %%d1l, %%d4u, %%acc0\n"
1664 "mac.w %%d1l, %%d4l, (-208*4,%[x1]), %%d4, %%acc1\n"
1665 "mac.w %%d2u, %%d4u, %%acc0\n"
1666 "mac.w %%d2u, %%d4l, ( 176*4,%[x1]), %%d4, %%acc1\n"
1667 "mac.w %%d2u, %%d4u, %%acc0\n"
1668 "mac.w %%d2u, %%d4l, (-240*4,%[x1]), %%d4, %%acc1\n"
1669 "mac.w %%d3u, %%d4u, %%acc0\n"
1670 "mac.w %%d3u, %%d4l, ( 208*4,%[x1]), %%d4, %%acc1\n"
1671 "mac.w %%d3l, %%d4u, %%acc0\n"
1672 "mac.w %%d3l, %%d4l, %%acc1\n"
1673 "movclr.l %%acc0, %%d0\n"
1674 "move.l %%d0, %[t0]\n"
1675 "movclr.l %%acc1, %%d0\n"
1676 "move.l %%d0, %[t1]\n"
1678 : [x1] "+a" (x1), [s0] "+m" (s0), [t0] "+m" (t0),
1679 [s1] "+m" (s1), [t1] "+m" (t1)
1680 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4");
1682 u = shft4(s0 - t0);
1683 v = shft4(s0 + t0);
1684 t0 = sb0[14];
1685 s0 = sb0[15] - t0;
1687 sb0[31] = v + t0; /* A0 */
1688 sb0[30] = u + s0; /* A1 */
1689 sb0[15] = u - s0; /* A2 */
1690 sb0[14] = v - t0; /* A3 */
1692 u = shft4(s1 - t1);
1693 v = shft4(s1 + t1);
1694 t1 = sb1[14];
1695 s1 = sb1[15] - t1;
1697 sb1[31] = v + t1; /* A0 */
1698 sb1[30] = u + s1; /* A1 */
1699 sb1[15] = u - s1; /* A2 */
1700 sb1[14] = v - t1; /* A3 */
1702 #else
1703 int ch, s, t, *a;
1705 for(ch=0; ch<cfg.channels; ch++)
1707 a = ch ? sb1 : sb0;
1708 for(k=0; k<18; k++, wk+=64, a+=SBLIMIT)
1710 wp = enwindow;
1711 x1 = wk;
1712 x2 = x1 - 124;
1714 /* x1[-572] .... x1[448] = 1022 */
1715 /* 18*4*16*32 */
1716 for(i=-15; i<0; i++)
1718 s = (int)x2[-224*2] * wp[ 0]; t = (int)x1[ 224*2] * wp[ 0];
1719 s += (int)x2[-160*2] * wp[ 1]; t += (int)x1[ 160*2] * wp[ 1];
1720 s += (int)x2[- 96*2] * wp[ 2]; t += (int)x1[ 96*2] * wp[ 2];
1721 s += (int)x2[- 32*2] * wp[ 3]; t += (int)x1[ 32*2] * wp[ 3];
1722 s += (int)x2[ 32*2] * wp[ 4]; t += (int)x1[- 32*2] * wp[ 4];
1723 s += (int)x2[ 96*2] * wp[ 5]; t += (int)x1[- 96*2] * wp[ 5];
1724 s += (int)x2[ 160*2] * wp[ 6]; t += (int)x1[-160*2] * wp[ 6];
1725 s += (int)x2[ 224*2] * wp[ 7]; t += (int)x1[-224*2] * wp[ 7];
1726 s += (int)x1[-256*2] * wp[ 8]; t += (int)x2[ 256*2] * wp[16];
1727 s += (int)x1[-192*2] * wp[ 9]; t += (int)x2[ 192*2] * wp[17];
1728 s += (int)x1[-128*2] * wp[10]; t += (int)x2[ 128*2] * wp[18];
1729 s += (int)x1[- 64*2] * wp[11]; t += (int)x2[ 64*2] * wp[19];
1730 s += (int)x1[ 0*2] * wp[12]; t += (int)x2[ 0*2] * wp[20];
1731 s += (int)x1[ 64*2] * wp[13]; t += (int)x2[- 64*2] * wp[21];
1732 s += (int)x1[ 128*2] * wp[14]; t += (int)x2[-128*2] * wp[22];
1733 s += (int)x1[ 192*2] * wp[15]; t += (int)x2[-192*2] * wp[23];
1735 a[30+i*2] = shft4(t) + shft13(s) * wp[24];
1736 a[31+i*2] = shft13(t) * wp[25] - shft13(s) * wp[26];
1737 wp += 27;
1738 x1 -= 2;
1739 x2 += 2;
1742 t = (int)x1[- 16*2] * wp[ 8]; s = (int)x1[ -32*2] * wp[0];
1743 t += ((int)x1[- 48*2]-x1[ 16*2]) * wp[10]; s += (int)x1[ -96*2] * wp[1];
1744 t += ((int)x1[- 80*2]+x1[ 48*2]) * wp[12]; s += (int)x1[-160*2] * wp[2];
1745 t += ((int)x1[-112*2]-x1[ 80*2]) * wp[14]; s += (int)x1[-224*2] * wp[3];
1746 t += ((int)x1[-144*2]+x1[112*2]) * wp[16]; s += (int)x1[ 32*2] * wp[4];
1747 t += ((int)x1[-176*2]-x1[144*2]) * wp[18]; s += (int)x1[ 96*2] * wp[5];
1748 t += ((int)x1[-208*2]+x1[176*2]) * wp[20]; s += (int)x1[ 160*2] * wp[6];
1749 t += ((int)x1[-240*2]-x1[208*2]) * wp[22]; s += (int)x1[ 224*2] * wp[7];
1751 u = shft4(s - t);
1752 v = shft4(s + t);
1753 t = a[14];
1754 s = a[15] - t;
1756 a[31] = v + t; /* A0 */
1757 a[30] = u + s; /* A1 */
1758 a[15] = u - s; /* A2 */
1759 a[14] = v - t; /* A3 */
1761 wk -= 18 * 64 - 1; /* rewind wk (to next channel start) */
1763 #endif
1766 void window_subband2(short *x1, int a[SBLIMIT]) ICODE_ATTR;
1767 void window_subband2(short *x1, int a[SBLIMIT])
1769 int xr;
1770 short *wp = enwindow;
1771 short *x2 = x1 - 124;
1773 wp += 27 * 15;
1774 x1 -= 2 * 15;
1775 x2 += 2 * 15;
1777 xr = a[28] - a[0]; a[0] += a[28]; a[28] = shft9(xr) * wp[-2*27+25];
1778 xr = a[29] - a[1]; a[1] += a[29]; a[29] = shft9(xr) * wp[-2*27+25];
1779 xr = a[26] - a[2]; a[2] += a[26]; a[26] = shft9(xr) * wp[-4*27+25];
1780 xr = a[27] - a[3]; a[3] += a[27]; a[27] = shft9(xr) * wp[-4*27+25];
1781 xr = a[24] - a[4]; a[4] += a[24]; a[24] = shft9(xr) * wp[-6*27+25];
1782 xr = a[25] - a[5]; a[5] += a[25]; a[25] = shft9(xr) * wp[-6*27+25];
1783 xr = a[22] - a[6]; a[6] += a[22]; a[22] = shft9(xr) * SQRT ;
1784 xr = a[23] - a[7]; a[7] += a[23]; a[23] = shft9(xr) * SQRT - a[7];
1785 a[ 7] -= a[ 6];
1786 a[22] -= a[ 7];
1787 a[23] -= a[22];
1789 xr = a[ 6]; a[ 6] = a[31] - xr; a[31] = a[31] + xr;
1790 xr = a[ 7]; a[ 7] = a[30] - xr; a[30] = a[30] + xr;
1791 xr = a[22]; a[22] = a[15] - xr; a[15] = a[15] + xr;
1792 xr = a[23]; a[23] = a[14] - xr; a[14] = a[14] + xr;
1794 xr = a[20] - a[ 8]; a[ 8] += a[20]; a[20] = shft9(xr) * wp[-10*27+25];
1795 xr = a[21] - a[ 9]; a[ 9] += a[21]; a[21] = shft9(xr) * wp[-10*27+25];
1796 xr = a[18] - a[10]; a[10] += a[18]; a[18] = shft9(xr) * wp[-12*27+25];
1797 xr = a[19] - a[11]; a[11] += a[19]; a[19] = shft9(xr) * wp[-12*27+25];
1798 xr = a[16] - a[12]; a[12] += a[16]; a[16] = shft9(xr) * wp[-14*27+25];
1799 xr = a[17] - a[13]; a[13] += a[17]; a[17] = shft9(xr) * wp[-14*27+25];
1800 xr =-a[20] + a[24]; a[20] += a[24]; a[24] = shft9(xr) * wp[-12*27+25];
1801 xr =-a[21] + a[25]; a[21] += a[25]; a[25] = shft9(xr) * wp[-12*27+25];
1802 xr = a[ 4] - a[ 8]; a[ 4] += a[ 8]; a[ 8] = shft9(xr) * wp[-12*27+25];
1803 xr = a[ 5] - a[ 9]; a[ 5] += a[ 9]; a[ 9] = shft9(xr) * wp[-12*27+25];
1804 xr = a[ 0] - a[12]; a[ 0] += a[12]; a[12] = shft9(xr) * wp[ -4*27+25];
1805 xr = a[ 1] - a[13]; a[ 1] += a[13]; a[13] = shft9(xr) * wp[ -4*27+25];
1806 xr = a[16] - a[28]; a[16] += a[28]; a[28] = shft9(xr) * wp[ -4*27+25];
1807 xr =-a[17] + a[29]; a[17] += a[29]; a[29] = shft9(xr) * wp[ -4*27+25];
1809 xr = SQRT * shft9(a[ 2] - a[10]); a[ 2] += a[10]; a[10] = xr;
1810 xr = SQRT * shft9(a[ 3] - a[11]); a[ 3] += a[11]; a[11] = xr;
1811 xr = SQRT * shft9(a[26] - a[18]); a[18] += a[26]; a[26] = xr - a[18];
1812 xr = SQRT * shft9(a[27] - a[19]); a[19] += a[27]; a[27] = xr - a[19];
1814 xr = a[ 2]; a[19] -= a[ 3]; a[ 3] -= xr; a[ 2] = a[31] - xr; a[31] += xr;
1815 xr = a[ 3]; a[11] -= a[19]; a[18] -= xr; a[ 3] = a[30] - xr; a[30] += xr;
1816 xr = a[18]; a[27] -= a[11]; a[19] -= xr; a[18] = a[15] - xr; a[15] += xr;
1818 xr = a[19]; a[10] -= xr; a[19] = a[14] - xr; a[14] += xr;
1819 xr = a[10]; a[11] -= xr; a[10] = a[23] - xr; a[23] += xr;
1820 xr = a[11]; a[26] -= xr; a[11] = a[22] - xr; a[22] += xr;
1821 xr = a[26]; a[27] -= xr; a[26] = a[ 7] - xr; a[ 7] += xr;
1823 xr = a[27]; a[27] = a[6] - xr; a[6] += xr;
1825 xr = SQRT * shft9(a[ 0] - a[ 4]); a[ 0] += a[ 4]; a[ 4] = xr;
1826 xr = SQRT * shft9(a[ 1] - a[ 5]); a[ 1] += a[ 5]; a[ 5] = xr;
1827 xr = SQRT * shft9(a[16] - a[20]); a[16] += a[20]; a[20] = xr;
1828 xr = SQRT * shft9(a[17] - a[21]); a[17] += a[21]; a[21] = xr;
1829 xr =-SQRT * shft9(a[ 8] - a[12]); a[ 8] += a[12]; a[12] = xr - a[ 8];
1830 xr =-SQRT * shft9(a[ 9] - a[13]); a[ 9] += a[13]; a[13] = xr - a[ 9];
1831 xr =-SQRT * shft9(a[25] - a[29]); a[25] += a[29]; a[29] = xr - a[25];
1832 xr =-SQRT * shft9(a[24] + a[28]); a[24] -= a[28]; a[28] = xr - a[24];
1834 xr = a[24] - a[16]; a[24] = xr;
1835 xr = a[20] - xr; a[20] = xr;
1836 xr = a[28] - xr; a[28] = xr;
1838 xr = a[25] - a[17]; a[25] = xr;
1839 xr = a[21] - xr; a[21] = xr;
1840 xr = a[29] - xr; a[29] = xr;
1842 xr = a[17] - a[1]; a[17] = xr;
1843 xr = a[ 9] - xr; a[ 9] = xr;
1844 xr = a[25] - xr; a[25] = xr;
1845 xr = a[ 5] - xr; a[ 5] = xr;
1846 xr = a[21] - xr; a[21] = xr;
1847 xr = a[13] - xr; a[13] = xr;
1848 xr = a[29] - xr; a[29] = xr;
1850 xr = a[ 1] - a[0]; a[ 1] = xr;
1851 xr = a[16] - xr; a[16] = xr;
1852 xr = a[17] - xr; a[17] = xr;
1853 xr = a[ 8] - xr; a[ 8] = xr;
1854 xr = a[ 9] - xr; a[ 9] = xr;
1855 xr = a[24] - xr; a[24] = xr;
1856 xr = a[25] - xr; a[25] = xr;
1857 xr = a[ 4] - xr; a[ 4] = xr;
1858 xr = a[ 5] - xr; a[ 5] = xr;
1859 xr = a[20] - xr; a[20] = xr;
1860 xr = a[21] - xr; a[21] = xr;
1861 xr = a[12] - xr; a[12] = xr;
1862 xr = a[13] - xr; a[13] = xr;
1863 xr = a[28] - xr; a[28] = xr;
1864 xr = a[29] - xr; a[29] = xr;
1866 xr = a[ 0]; a[ 0] += a[31]; a[31] -= xr;
1867 xr = a[ 1]; a[ 1] += a[30]; a[30] -= xr;
1868 xr = a[16]; a[16] += a[15]; a[15] -= xr;
1869 xr = a[17]; a[17] += a[14]; a[14] -= xr;
1870 xr = a[ 8]; a[ 8] += a[23]; a[23] -= xr;
1871 xr = a[ 9]; a[ 9] += a[22]; a[22] -= xr;
1872 xr = a[24]; a[24] += a[ 7]; a[ 7] -= xr;
1873 xr = a[25]; a[25] += a[ 6]; a[ 6] -= xr;
1874 xr = a[ 4]; a[ 4] += a[27]; a[27] -= xr;
1875 xr = a[ 5]; a[ 5] += a[26]; a[26] -= xr;
1876 xr = a[20]; a[20] += a[11]; a[11] -= xr;
1877 xr = a[21]; a[21] += a[10]; a[10] -= xr;
1878 xr = a[12]; a[12] += a[19]; a[19] -= xr;
1879 xr = a[13]; a[13] += a[18]; a[18] -= xr;
1880 xr = a[28]; a[28] += a[ 3]; a[ 3] -= xr;
1881 xr = a[29]; a[29] += a[ 2]; a[ 2] -= xr;
1884 void mdct_long(int *out, int *in) ICODE_ATTR;
1885 void mdct_long(int *out, int *in)
1887 int ct,st;
1888 int tc1, tc2, tc3, tc4, ts5, ts6, ts7, ts8;
1889 int ts1, ts2, ts3, ts4, tc5, tc6, tc7, tc8;
1891 /* 1,2, 5,6, 9,10, 13,14, 17 */
1892 tc1 = in[17] - in[ 9];
1893 tc3 = in[15] - in[11];
1894 tc4 = in[14] - in[12];
1895 ts5 = in[ 0] + in[ 8];
1896 ts6 = in[ 1] + in[ 7];
1897 ts7 = in[ 2] + in[ 6];
1898 ts8 = in[ 3] + in[ 5];
1900 out[17] = (ts5 + ts7 - ts8) * cx[8] - (ts6 - in[4]) * cx[8];
1901 st = (ts5 + ts7 - ts8) * cx[7] + (ts6 - in[4]) * cx[8];
1902 ct = (tc1 - tc3 - tc4) * cx[6];
1903 out[5] = ct + st;
1904 out[6] = ct - st;
1906 tc2 = (in[16] - in[10]) * cx[6];
1907 ts6 = ts6 * cx[7] + in[4] * cx[8];
1909 ct = tc1 * cx[0] + tc2 + tc3 * cx[1] + tc4 * cx[2];
1910 st = -ts5 * cx[4] + ts6 - ts7 * cx[5] + ts8 * cx[3];
1911 out[1] = ct + st;
1912 out[2] = ct - st;
1914 ct = tc1 * cx[1] - tc2 - tc3 * cx[2] + tc4 * cx[0];
1915 st = -ts5 * cx[5] + ts6 - ts7 * cx[3] + ts8 * cx[4];
1916 out[ 9] = ct + st;
1917 out[10] = ct - st;
1919 ct = tc1 * cx[2] - tc2 + tc3 * cx[0] - tc4 * cx[1];
1920 st = ts5 * cx[3] - ts6 + ts7 * cx[4] - ts8 * cx[5];
1921 out[13] = ct + st;
1922 out[14] = ct - st;
1924 ts1 = in[ 8] - in[ 0];
1925 ts3 = in[ 6] - in[ 2];
1926 ts4 = in[ 5] - in[ 3];
1927 tc5 = in[17] + in[ 9];
1928 tc6 = in[16] + in[10];
1929 tc7 = in[15] + in[11];
1930 tc8 = in[14] + in[12];
1932 out[0] = (tc5 + tc7 + tc8) * cx[8] + (tc6 + in[13]) * cx[8];
1933 ct = (tc5 + tc7 + tc8) * cx[7] - (tc6 + in[13]) * cx[8];
1934 st = (ts1 - ts3 + ts4) * cx[6];
1935 out[11] = ct + st;
1936 out[12] = ct - st;
1938 ts2 = (in[7] - in[1]) * cx[6];
1939 tc6 = in[13] * cx[8] - tc6 * cx[7];
1941 ct = tc5 * cx[3] - tc6 + tc7 * cx[4] + tc8 * cx[5];
1942 st = ts1 * cx[2] + ts2 + ts3 * cx[0] + ts4 * cx[1];
1943 out[3] = ct + st;
1944 out[4] = ct - st;
1946 ct =-tc5 * cx[5] + tc6 - tc7 * cx[3] - tc8 * cx[4];
1947 st = ts1 * cx[1] + ts2 - ts3 * cx[2] - ts4 * cx[0];
1948 out[7] = ct + st;
1949 out[8] = ct - st;
1951 ct =-tc5 * cx[4] + tc6 - tc7 * cx[5] - tc8 * cx[3];
1952 st = ts1 * cx[0] - ts2 + ts3 * cx[1] - ts4 * cx[2];
1953 out[15] = ct + st;
1954 out[16] = ct - st;
1957 static int find_bitrate_index(int type, int bitrate, bool stereo)
1959 if (type == 1 && !stereo && bitrate > 160)
1960 bitrate = 160;
1962 return ci->round_value_to_list32(bitrate,
1963 &bitr_index[type][1], 14, true) + 1;
1966 static int find_samplerate_index(long freq, int *mp3_type)
1968 int mpeg = freq >= (32000+24000)/2 ? 1 : 0;
1969 int i = ci->round_value_to_list32(freq, sampr_index[mpeg], 3, true);
1970 *mp3_type = mpeg;
1971 return i;
1974 static bool init_mp3_encoder_engine(int sample_rate,
1975 int num_channels,
1976 int rec_mono_mode,
1977 struct encoder_config *enc_cfg)
1979 const bool stereo = num_channels > 1;
1980 uint32_t avg_byte_per_frame;
1982 cfg.channels = stereo ? 2 : 1;
1983 cfg.rec_mono_mode = rec_mono_mode;
1984 cfg.mpg.mode = stereo ? 0 : 3; /* 0=stereo, 3=mono */
1985 cfg.mpg.smpl_id = find_samplerate_index(sample_rate, &cfg.mpg.type);
1986 cfg.samplerate = sampr_index[cfg.mpg.type][cfg.mpg.smpl_id];
1987 cfg.mpg.bitr_id = find_bitrate_index(cfg.mpg.type,
1988 enc_cfg->mp3_enc.bitrate,
1989 stereo);
1990 cfg.mpg.bitrate = bitr_index[cfg.mpg.type][cfg.mpg.bitr_id];
1991 cfg.mpg.num_bands = num_bands[stereo ? cfg.mpg.type : 2][cfg.mpg.bitr_id];
1993 if (cfg.mpg.type == 1)
1995 cfg.granules = 2;
1996 pcm_chunk_size = PCM_CHUNK_SIZE1;
1997 samp_per_frame = SAMP_PER_FRAME1;
1999 else
2001 cfg.granules = 1;
2002 pcm_chunk_size = PCM_CHUNK_SIZE2;
2003 samp_per_frame = SAMP_PER_FRAME2;
2006 memcpy(scalefac, sfBand[cfg.mpg.smpl_id + 3*cfg.mpg.type], sizeof(scalefac));
2007 memset(mfbuf , 0 , sizeof(mfbuf ));
2008 memset(mdct_freq , 0 , sizeof(mdct_freq ));
2009 memset(enc_data , 0 , sizeof(enc_data ));
2010 memset(sb_data , 0 , sizeof(sb_data ));
2011 memset(&CodedData, 0 , sizeof(CodedData ));
2012 memcpy(ca , ca_const , sizeof(ca ));
2013 memcpy(cs , cs_const , sizeof(cs ));
2014 memcpy(cx , cx_const , sizeof(cx ));
2015 memcpy(win , win_const , sizeof(win ));
2016 memcpy(enwindow , enwindow_const , sizeof(enwindow ));
2017 memcpy(int2idx , int2idx_const , sizeof(int2idx ));
2018 memcpy(ht_count , ht_count_const , sizeof(ht_count ));
2019 memcpy( tab01 , tab01_const , sizeof(tab01 ));
2020 memcpy( tab23 , tab23_const , sizeof(tab23 ));
2021 memcpy( tab56 , tab56_const , sizeof(tab56 ));
2022 memcpy( tab1315 , tab1315_const , sizeof(tab1315 ));
2023 memcpy( tab1624 , tab1624_const , sizeof(tab1624 ));
2024 memcpy( tab789 , tab789_const , sizeof(tab789 ));
2025 memcpy( tabABC , tabABC_const , sizeof(tabABC ));
2026 memcpy( t1HB , t1HB_const , sizeof(t1HB ));
2027 memcpy( t2HB , t2HB_const , sizeof(t2HB ));
2028 memcpy( t3HB , t3HB_const , sizeof(t3HB ));
2029 memcpy( t5HB , t5HB_const , sizeof(t5HB ));
2030 memcpy( t6HB , t6HB_const , sizeof(t6HB ));
2031 memcpy( t7HB , t7HB_const , sizeof(t7HB ));
2032 memcpy( t8HB , t8HB_const , sizeof(t8HB ));
2033 memcpy( t9HB , t9HB_const , sizeof(t9HB ));
2034 memcpy(t10HB , t10HB_const , sizeof(t10HB ));
2035 memcpy(t11HB , t11HB_const , sizeof(t11HB ));
2036 memcpy(t12HB , t12HB_const , sizeof(t12HB ));
2037 memcpy(t13HB , t13HB_const , sizeof(t13HB ));
2038 memcpy(t15HB , t15HB_const , sizeof(t15HB ));
2039 memcpy(t16HB , t16HB_const , sizeof(t16HB ));
2040 memcpy(t24HB , t24HB_const , sizeof(t24HB ));
2041 memcpy( t1l , t1l_const , sizeof(t1l ));
2042 memcpy( t2l , t2l_const , sizeof(t2l ));
2043 memcpy( t3l , t3l_const , sizeof(t3l ));
2044 memcpy( t5l , t5l_const , sizeof(t5l ));
2045 memcpy( t6l , t6l_const , sizeof(t6l ));
2046 memcpy( t7l , t7l_const , sizeof(t7l ));
2047 memcpy( t8l , t8l_const , sizeof(t8l ));
2048 memcpy( t9l , t9l_const , sizeof(t9l ));
2049 memcpy(t10l , t10l_const , sizeof(t10l ));
2050 memcpy(t11l , t11l_const , sizeof(t11l ));
2051 memcpy(t12l , t12l_const , sizeof(t12l ));
2052 memcpy(t13l , t13l_const , sizeof(t13l ));
2053 memcpy(t15l , t15l_const , sizeof(t15l ));
2054 memcpy(t16l , t16l_const , sizeof(t16l ));
2055 memcpy(t24l , t24l_const , sizeof(t24l ));
2056 memcpy(ht , ht_const , sizeof(ht ));
2058 ht[ 0].table = NULL; ht[ 0].hlen = NULL; /* Apparently not used */
2059 ht[ 1].table = t1HB; ht[ 1].hlen = t1l;
2060 ht[ 2].table = t2HB; ht[ 2].hlen = t2l;
2061 ht[ 3].table = t3HB; ht[ 3].hlen = t3l;
2062 ht[ 4].table = NULL; ht[ 4].hlen = NULL; /* Apparently not used */
2063 ht[ 5].table = t5HB; ht[ 5].hlen = t5l;
2064 ht[ 6].table = t6HB; ht[ 6].hlen = t6l;
2065 ht[ 7].table = t7HB; ht[ 7].hlen = t7l;
2066 ht[ 8].table = t8HB; ht[ 8].hlen = t8l;
2067 ht[ 9].table = t9HB; ht[ 9].hlen = t9l;
2068 ht[10].table = t10HB; ht[10].hlen = t10l;
2069 ht[11].table = t11HB; ht[11].hlen = t11l;
2070 ht[12].table = t12HB; ht[12].hlen = t12l;
2071 ht[13].table = t13HB; ht[13].hlen = t13l;
2072 ht[14].table = NULL; ht[14].hlen = NULL; /* Apparently not used */
2073 ht[15].table = t15HB; ht[15].hlen = t15l;
2075 /* Figure average number of 'bytes' per frame */
2076 avg_byte_per_frame = SAMPL2 * 16000 * cfg.mpg.bitrate / (2 - cfg.mpg.type);
2077 avg_byte_per_frame = avg_byte_per_frame / cfg.samplerate;
2078 cfg.byte_per_frame = avg_byte_per_frame / 64;
2079 cfg.frac_per_frame = avg_byte_per_frame & 63;
2080 cfg.slot_lag = 0;
2081 cfg.sideinfo_len = 32 + (cfg.mpg.type ? (cfg.channels == 1 ? 136 : 256)
2082 : (cfg.channels == 1 ? 72 : 136));
2084 return true;
2087 STATICIRAM void to_mono_mm(void) ICODE_ATTR;
2088 STATICIRAM void to_mono_mm(void)
2090 /* |llllllllllllllll|rrrrrrrrrrrrrrrr| =>
2091 * |mmmmmmmmmmmmmmmm|mmmmmmmmmmmmmmmm|
2093 uint32_t *samp = (uint32_t *)&mfbuf[2*512];
2094 uint32_t *samp_end = samp + samp_per_frame;
2096 inline void to_mono(uint32_t **samp)
2098 int32_t lr = **samp;
2099 int32_t m;
2101 switch(cfg.rec_mono_mode)
2103 case 1:
2104 /* mono = L */
2105 m = lr >> 16;
2106 break;
2107 case 2:
2108 /* mono = R */
2109 m = (int16_t)lr;
2110 break;
2111 case 0:
2112 default:
2113 /* mono = (L+R)/2 */
2114 m = (int16_t)lr + (lr >> 16) + err;
2115 err = m & 1;
2116 m >>= 1;
2117 break;
2119 *(*samp)++ = (m << 16) | (uint16_t)m;
2120 } /* to_mono */
2124 to_mono(&samp);
2125 to_mono(&samp);
2126 to_mono(&samp);
2127 to_mono(&samp);
2128 to_mono(&samp);
2129 to_mono(&samp);
2130 to_mono(&samp);
2131 to_mono(&samp);
2133 while (samp < samp_end);
2134 } /* to_mono_mm */
2136 #ifdef ROCKBOX_LITTLE_ENDIAN
2137 /* Swaps a frame to big endian */
2138 static inline void byte_swap_frame32(uint32_t *dst, uint32_t *src,
2139 size_t size) ICODE_ATTR;
2140 static inline void byte_swap_frame32(uint32_t *dst, uint32_t *src,
2141 size_t size)
2143 uint32_t *src_end = SKIPBYTES(src, size);
2147 *dst++ = swap32(*src++);
2148 *dst++ = swap32(*src++);
2149 *dst++ = swap32(*src++);
2150 *dst++ = swap32(*src++);
2151 *dst++ = swap32(*src++);
2152 *dst++ = swap32(*src++);
2153 *dst++ = swap32(*src++);
2154 *dst++ = swap32(*src++);
2156 while(src < src_end);
2157 } /* byte_swap_frame32 */
2158 #endif /* ROCKBOX_LITTLE_ENDIAN */
2160 static void set_scale_facs(int *mdct_freq)
2162 unsigned int i, is, ie, k, s;
2163 int max_freq_val, avrg_freq_val;
2165 /* calc average of first 256 frequency values */
2166 for(avrg_freq_val=i=0; i<256; i++)
2167 avrg_freq_val += mdct_freq[i];
2168 avrg_freq_val >>= 8;
2170 /* if max of current band is smaller than average, increase precision */
2171 /* last band keeps untouched (not scaled) */
2172 for(is=k=0; is<scalefac[21]; k++)
2174 max_freq_val = 0;
2176 for(i=is, ie=scalefac[k+1]; i<ie; i++)
2177 if(max_freq_val < mdct_freq[i])
2178 max_freq_val = mdct_freq[i];
2180 for(s=0; s<3; s++)
2181 if((max_freq_val<<s) > avrg_freq_val)
2182 break;
2184 band_scale_f[k] = (unsigned char)s;
2186 for(i=is; s && i<ie; i++)
2187 mdct_freq[i] <<= s;
2189 is = ie;
2193 STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2194 ICODE_ATTR;
2195 STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2197 int gr, gr_cnt;
2198 uint32_t max;
2200 /* encode one mp3 frame in this loop */
2201 CodedData.bitpos = 0;
2202 memset(CodedData.bbuf, 0, sizeof(CodedData.bbuf));
2204 if((cfg.slot_lag += cfg.frac_per_frame) >= 64)
2205 { /* Padding for this frame */
2206 cfg.slot_lag -= 64;
2207 cfg.mpg.padding = 1;
2209 else
2210 cfg.mpg.padding = 0;
2212 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding
2213 - cfg.sideinfo_len) / cfg.granules / cfg.channels
2214 - 42; // reserved for scale_facs
2216 /* shift out old samples */
2217 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512);
2219 if (chunk->flags & CHUNKF_START_FILE)
2221 /* prefix silent samples for encoder delay */
2222 memset(mfbuf + 2*512, 0, ENC_DELAY_SIZE);
2223 /* read new samples to iram for further processing */
2224 memcpy(mfbuf + 2*512 + ENC_DELAY_SIZE/2,
2225 buffer, pcm_chunk_size - ENC_DELAY_SIZE);
2226 chunk->num_pcm = samp_per_frame - ENC_DELAY_SAMP;
2228 else
2230 /* read new samples to iram for further processing */
2231 memcpy(mfbuf + 2*512, buffer, pcm_chunk_size);
2232 chunk->num_pcm = samp_per_frame;
2235 if (cfg.channels == 1)
2236 to_mono_mm();
2238 cfg.ResvSize = 0;
2239 gr_cnt = cfg.granules * cfg.channels;
2240 CodedData.bitpos = cfg.sideinfo_len; /* leave space for mp3 header */
2242 for(gr=0; gr<cfg.granules; gr++)
2244 short *wk = mfbuf + 2*286 + gr*1152;
2245 int ch;
2247 /* 16bit packed wav data can be windowed efficiently on coldfire */
2248 window_subband1(wk, sb_data[0][1-gr][0], sb_data[1][1-gr][0]);
2250 for(ch=0; ch<cfg.channels; ch++)
2252 int ii, k, shift;
2254 wk = mfbuf + 2*286 + gr*1152 + ch;
2256 /* 36864=4*18*16*32 */
2257 for(k=0; k<18; k++, wk+=64)
2259 window_subband2(wk, sb_data[ch][1-gr][k]);
2260 /* Compensate for inversion in the analysis filter */
2261 if(k & 1)
2263 int band;
2264 for(band=1; band<32; band+=2)
2265 sb_data[ch][1-gr][k][band] *= -1;
2269 /* Perform imdct of 18 previous + 18 current subband samples */
2270 /* for integer precision do this loop again (if neccessary) */
2271 shift = 14 - (cfg.cod_info[gr][ch].additStep >> 2);
2272 for(k=1,ii=0; ii<3 && k; ii++)
2274 int *mdct = mdct_freq;
2275 int band;
2277 cfg.cod_info[gr][ch].additStep = 4 * (14 - shift);
2279 for(band=0; band<cfg.mpg.num_bands; band++, mdct+=18)
2281 int *band0 = sb_data[ch][ gr][0] + order[band];
2282 int *band1 = sb_data[ch][1-gr][0] + order[band];
2283 int work[18];
2285 /* 9216=4*32*9*8 */
2286 for(k=-9; k<0; k++)
2288 int a = shft_n(band1[(k+9)*32], shift);
2289 int b = shft_n(band1[(8-k)*32], shift);
2290 int c = shft_n(band0[(k+9)*32], shift);
2291 int d = shft_n(band0[(8-k)*32], shift);
2293 work[k+ 9] = shft16(a * win[k+ 9][0] +
2294 b * win[k+ 9][1] +
2295 c * win[k+ 9][2] +
2296 d * win[k+ 9][3]);
2298 work[k+18] = shft16(c * win[k+18][0] +
2299 d * win[k+18][1] +
2300 a * win[k+18][2] +
2301 b * win[k+18][3]);
2304 /* 7200=4*18*100 */
2305 mdct_long(mdct, work);
2307 /* Perform aliasing reduction butterfly */
2308 if(band != 0)
2310 for(k=7; k>=0; --k)
2312 int bu, bd;
2313 bu = shft15(mdct[k]) * ca[k] +
2314 shft15(mdct[-1-k]) * cs[k];
2315 bd = shft15(mdct[k]) * cs[k] -
2316 shft15(mdct[-1-k]) * ca[k];
2317 mdct[-1-k] = bu;
2318 mdct[ k ] = bd;
2323 max = 0;
2324 for(k=0; k<576; k++)
2326 if(mdct_freq[k] < 0)
2328 mdct_sign[k] = 1; /* negative */
2329 mdct_freq[k] = shft13(-mdct_freq[k]);
2331 else
2333 mdct_sign[k] = 0; /* positive */
2334 mdct_freq[k] = shft13(mdct_freq[k]);
2337 if(max < (uint32_t)mdct_freq[k])
2338 max = (uint32_t)mdct_freq[k];
2340 cfg.cod_info[gr][ch].max_val = max;
2342 /* calc new shift for higher integer precision */
2343 for(k=0; max<(uint32_t)(0x7800>>k); k++) shift--;
2344 for( ; (max>>k)>=(uint32_t)0x10000; k++) shift++;
2345 if(shift < 0) shift = 0;
2348 cfg.cod_info[gr][ch].quantStep +=
2349 cfg.cod_info[gr][ch].additStep;
2351 set_scale_facs(mdct_freq);
2353 /* bit and noise allocation */
2354 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch],
2355 gr_cnt--);
2356 /* write the frame to the bitstream */
2357 Huffmancodebits(enc_data, mdct_sign,
2358 &cfg.cod_info[gr][ch]);
2360 cfg.cod_info[gr][ch].quantStep -=
2361 cfg.cod_info[gr][ch].additStep;
2363 if(cfg.granules == 1)
2365 memcpy(sb_data[ch][0], sb_data[ch][1],
2366 sizeof(sb_data[ch][0]));
2371 chunk->enc_size = cfg.byte_per_frame + cfg.mpg.padding;
2373 /* finish this chunk by adding sideinfo header data */
2374 CodedData.bitpos = 0;
2375 encodeSideInfo( cfg.cod_info );
2377 #ifdef ROCKBOX_BIG_ENDIAN
2378 /* copy chunk to enc_buffer */
2379 memcpy(chunk->enc_data, CodedData.bbuf, chunk->enc_size);
2380 #else
2381 /* swap frame to big endian */
2382 byte_swap_frame32((uint32_t *)chunk->enc_data, CodedData.bbuf, chunk->enc_size);
2383 #endif
2384 } /* encode_frame */
2386 /* called very often - inline */
2387 static inline bool is_file_data_ok(struct enc_file_event_data *filed) ICODE_ATTR;
2388 static inline bool is_file_data_ok(struct enc_file_event_data *filed)
2390 return filed->rec_file >= 0 && (long)filed->chunk->flags >= 0;
2391 } /* is_event_ok */
2393 static unsigned char mp3_data[16384] __attribute__((aligned(4)));
2394 static unsigned int mp3_data_len; /* current data size in buffer */
2396 /* called very often - inline */
2397 static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
2398 static inline bool on_write_chunk(struct enc_file_event_data *data)
2400 if (!is_file_data_ok(data))
2401 return false;
2403 if (data->chunk->enc_data == NULL)
2405 #ifdef ROCKBOX_HAS_LOGF
2406 ci->logf("mp3 enc: NULL data");
2407 #endif
2408 return true;
2411 /* if current chunk doesn't fit => write collected data */
2412 if (mp3_data_len + data->chunk->enc_size > sizeof(mp3_data))
2414 if (ci->write(data->rec_file, mp3_data,
2415 mp3_data_len) != (ssize_t)mp3_data_len)
2416 return false;
2418 mp3_data_len = 0;
2421 memcpy(mp3_data+mp3_data_len, data->chunk->enc_data,
2422 data->chunk->enc_size);
2424 mp3_data_len += data->chunk->enc_size;
2426 data->num_pcm_samples += data->chunk->num_pcm;
2427 return true;
2428 } /* on_write_chunk */
2430 static bool on_start_file(struct enc_file_event_data *data)
2432 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
2433 return false;
2435 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC);
2437 if (data->rec_file < 0)
2438 return false;
2440 /* reset sample count */
2441 data->num_pcm_samples = 0;
2443 /* reset buffer write position */
2444 mp3_data_len = 0;
2446 return true;
2447 } /* on_start_file */
2449 static bool on_end_file(struct enc_file_event_data *data)
2451 if (data->rec_file < 0)
2452 return false; /* file already closed, nothing more we can do */
2454 /* write the remaining mp3_data */
2455 if (ci->write(data->rec_file, mp3_data, mp3_data_len)
2456 != (ssize_t)mp3_data_len)
2457 return false;
2459 /* reset buffer write position */
2460 mp3_data_len = 0;
2462 /* always _try_ to write the file header, even on error */
2463 if (ci->close(data->rec_file) != 0)
2464 return false;
2466 data->rec_file = -1;
2468 return true;
2469 } /* on_end_file */
2471 static void on_rec_new_stream(struct enc_buffer_event_data *data)
2473 int num_frames = cfg.mpg.type == 1 ?
2474 ENC_PADDING_FRAMES1 : ENC_PADDING_FRAMES2;
2476 if (data->flags & CHUNKF_END_FILE)
2478 /* add silent frames to end - encoder will also be flushed for start
2479 of next file if any */
2480 memset(res_buffer, 0, pcm_chunk_size);
2482 /* the initial chunk given for the end is at enc_wr_index */
2483 while (num_frames-- > 0)
2485 data->chunk->enc_data = ENC_CHUNK_SKIP_HDR(data->chunk->enc_data,
2486 data->chunk);
2488 encode_frame(res_buffer, data->chunk);
2489 data->chunk->num_pcm = samp_per_frame;
2491 ci->enc_finish_chunk();
2492 data->chunk = ci->enc_get_chunk();
2495 else if (data->flags & CHUNKF_PRERECORD)
2497 /* nothing to add and we cannot change prerecorded data */
2499 else if (data->flags & CHUNKF_START_FILE)
2501 /* starting fresh ... be sure to flush encoder first */
2502 struct enc_chunk_hdr *chunk = ENC_CHUNK_HDR(res_buffer);
2504 chunk->flags = 0;
2505 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
2507 while (num_frames-- > 0)
2509 memset(chunk->enc_data, 0, pcm_chunk_size);
2510 encode_frame(chunk->enc_data, chunk);
2513 } /* on_rec_new_stream */
2515 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
2516 ICODE_ATTR;
2517 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
2519 switch (event)
2521 case ENC_WRITE_CHUNK:
2522 if (on_write_chunk((struct enc_file_event_data *)data))
2523 return;
2525 break;
2527 case ENC_START_FILE:
2528 if (on_start_file((struct enc_file_event_data *)data))
2529 return;
2531 break;
2533 case ENC_END_FILE:
2534 if (on_end_file((struct enc_file_event_data *)data))
2535 return;
2537 break;
2539 case ENC_REC_NEW_STREAM:
2540 on_rec_new_stream((struct enc_buffer_event_data *)data);
2541 return;
2543 default:
2544 return;
2547 /* Something failed above. Signal error back to core. */
2548 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
2549 } /* enc_events_callback */
2551 static bool enc_init(void)
2553 struct enc_inputs inputs;
2554 struct enc_parameters params;
2556 if (ci->enc_get_inputs == NULL ||
2557 ci->enc_set_parameters == NULL ||
2558 ci->enc_get_chunk == NULL ||
2559 ci->enc_finish_chunk == NULL ||
2560 ci->enc_get_pcm_data == NULL ||
2561 ci->enc_unget_pcm_data == NULL )
2562 return false;
2564 ci->enc_get_inputs(&inputs);
2566 if (inputs.config->afmt != AFMT_MPA_L3)
2567 return false;
2569 init_mp3_encoder_engine(inputs.sample_rate, inputs.num_channels,
2570 inputs.rec_mono_mode, inputs.config);
2572 err = 0;
2574 /* configure the buffer system */
2575 params.afmt = AFMT_MPA_L3;
2576 params.chunk_size = cfg.byte_per_frame + 1;
2577 params.enc_sample_rate = cfg.samplerate;
2578 /* need enough reserved bytes to hold one frame of pcm samples + hdr
2579 for padding and flushing */
2580 params.reserve_bytes = ENC_CHUNK_HDR_SIZE + pcm_chunk_size;
2581 params.events_callback = enc_events_callback;
2582 ci->enc_set_parameters(&params);
2584 res_buffer = params.reserve_buffer;
2586 #ifdef CPU_COLDFIRE
2587 asm volatile ("move.l #0, %macsr"); /* integer mode */
2588 #endif
2590 return true;
2591 } /* enc_init */
2593 enum codec_status codec_main(void)
2595 /* Generic codec initialisation */
2596 if (!enc_init())
2598 ci->enc_codec_loaded = -1;
2599 return CODEC_ERROR;
2602 /* main application waits for this flag during encoder loading */
2603 ci->enc_codec_loaded = 1;
2605 /* main encoding loop */
2606 while (!ci->stop_encoder)
2608 char *buffer;
2610 while ((buffer = ci->enc_get_pcm_data(pcm_chunk_size)) != NULL)
2612 struct enc_chunk_hdr *chunk;
2614 if (ci->stop_encoder)
2615 break;
2617 chunk = ci->enc_get_chunk();
2618 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
2620 encode_frame(buffer, chunk);
2622 if (chunk->num_pcm < samp_per_frame)
2624 ci->enc_unget_pcm_data(pcm_chunk_size - chunk->num_pcm*4);
2625 chunk->num_pcm = samp_per_frame;
2628 ci->enc_finish_chunk();
2630 ci->yield();
2633 ci->yield();
2636 /* reset parameters to initial state */
2637 ci->enc_set_parameters(NULL);
2639 /* main application waits for this flag during encoder removing */
2640 ci->enc_codec_loaded = 0;
2642 return CODEC_OK;
2643 } /* codec_start */
2645 #endif /* ndef SIMULATOR */