Rename variables sectorbuf and verbose to avoid clashes in rbutil. Cleanup exports...
[Rockbox.git] / apps / plugins / mp3_encoder.c
blob0dce32e9df9843d5e94a3509adba5dd4cfc8f76a
1 /* Shine is an MP3 encoder
2 * Copyright (C) 1999-2000 Gabriel Bouvigne
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details. */
14 #include <inttypes.h>
15 #include "plugin.h"
17 PLUGIN_HEADER
18 PLUGIN_IRAM_DECLARE
20 static const struct plugin_api* rb;
22 MEM_FUNCTION_WRAPPERS(rb);
24 #define SAMP_PER_FRAME 1152
25 #define SAMPL2 576
26 #define SBLIMIT 32
27 #define HTN 16
28 #define putlong(c, s) if(s+sz <= 32) { cc = (cc << s) | c; sz+= s; } \
29 else { putbits(cc, sz); cc = c; sz = s; }
31 enum e_byte_order { order_unknown, order_bigEndian, order_littleEndian };
33 typedef struct {
34 int type; /* 0=(MPEG2 - 22.05,24,16kHz) 1=(MPEG1 - 44.1,48,32kHz) */
35 int mode; /* 0=stereo, 1=jstereo, 2=dual, 3=mono */
36 int bitrate;
37 int padding;
38 int num_bands;
39 long bitr_id;
40 int smpl_id;
41 } mpeg_t;
43 /* Side information */
44 typedef struct {
45 uint32_t part2_3_length;
46 int count1; /* number of 0-1-quadruples */
47 uint32_t global_gain;
48 uint32_t table_select[4];
49 uint32_t region_0_1;
50 uint32_t address1;
51 uint32_t address2;
52 uint32_t address3;
53 long quantStep;
54 long additStep;
55 uint32_t max_val;
56 } side_info_t;
58 typedef struct {
59 enum e_byte_order byte_order;
60 side_info_t cod_info[2][2];
61 mpeg_t mpg;
62 long frac_per_frame;
63 long byte_per_frame;
64 long slot_lag;
65 int sideinfo_len;
66 int mean_bits;
67 int ResvSize;
68 int channels;
69 int granules;
70 int resample;
71 long samplerate;
72 } config_t;
74 typedef struct {
75 int bitpos; /* current bitpos for writing */
76 uint32_t bbuf[263];
77 } BF_Data;
79 struct huffcodetab {
80 int len; /* max. index */
81 const uint8_t *table; /* pointer to array[len][len] */
82 const uint8_t *hlen; /* pointer to array[len][len] */
85 struct huffcodebig {
86 int len; /* max. index */
87 int linbits; /* number of linbits */
88 int linmax; /* max number stored in linbits */
91 #define shft4(x) ((x + 8) >> 4)
92 #define shft9(x) ((x + 256) >> 9)
93 #define shft13(x) ((x + 4096) >> 13)
94 #define shft15(x) ((x + 16384) >> 15)
95 #define shft16(x) ((x + 32768) >> 16)
96 #define shft_n(x,n) ((x) >> n)
97 #define SQRT 724 /* sqrt(2) * 512 */
99 static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */
100 static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */
101 static int mdct_freq [SAMPL2] IBSS_ATTR; /* 2304 Bytes */
102 static char mdct_sign [SAMPL2] IBSS_ATTR; /* 576 Bytes */
103 static short enc_data [SAMPL2] IBSS_ATTR; /* 1152 Bytes */
104 static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */
105 static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */
106 static int ca [8] IBSS_ATTR; /* 32 Bytes */
107 static int cs [8] IBSS_ATTR; /* 32 Bytes */
108 static int cx [9] IBSS_ATTR; /* 36 Bytes */
109 static int win [18][4] IBSS_ATTR; /* 288 Bytes */
110 static short enwindow [15*27+24] IBSS_ATTR; /* 862 Bytes */
111 static short int2idx [4096] IBSS_ATTR; /* 8192 Bytes */
112 static uint8_t ht_count [2][2][16] IBSS_ATTR; /* 64 Bytes */
113 static uint32_t tab01 [ 16] IBSS_ATTR; /* 64 Bytes */
114 static uint32_t tab23 [ 9] IBSS_ATTR; /* 36 Bytes */
115 static uint32_t tab56 [ 16] IBSS_ATTR; /* 64 Bytes */
116 static uint32_t tab1315 [256] IBSS_ATTR; /* 1024 Bytes */
117 static uint32_t tab1624 [256] IBSS_ATTR; /* 1024 Bytes */
118 static uint32_t tab789 [ 36] IBSS_ATTR; /* 144 Bytes */
119 static uint32_t tabABC [ 64] IBSS_ATTR; /* 256 Bytes */
120 static uint8_t t1HB [ 4] IBSS_ATTR;
121 static uint8_t t2HB [ 9] IBSS_ATTR;
122 static uint8_t t3HB [ 9] IBSS_ATTR;
123 static uint8_t t5HB [ 16] IBSS_ATTR;
124 static uint8_t t6HB [ 16] IBSS_ATTR;
125 static uint8_t t7HB [ 36] IBSS_ATTR;
126 static uint8_t t8HB [ 36] IBSS_ATTR;
127 static uint8_t t9HB [ 36] IBSS_ATTR;
128 static uint8_t t10HB [ 64] IBSS_ATTR;
129 static uint8_t t11HB [ 64] IBSS_ATTR;
130 static uint8_t t12HB [ 64] IBSS_ATTR;
131 static uint8_t t13HB [256] IBSS_ATTR;
132 static uint8_t t15HB [256] IBSS_ATTR;
133 static uint16_t t16HB [256] IBSS_ATTR;
134 static uint16_t t24HB [256] IBSS_ATTR;
135 static uint8_t t1l [ 8] IBSS_ATTR;
136 static uint8_t t2l [ 9] IBSS_ATTR;
137 static uint8_t t3l [ 9] IBSS_ATTR;
138 static uint8_t t5l [ 16] IBSS_ATTR;
139 static uint8_t t6l [ 16] IBSS_ATTR;
140 static uint8_t t7l [ 36] IBSS_ATTR;
141 static uint8_t t8l [ 36] IBSS_ATTR;
142 static uint8_t t9l [ 36] IBSS_ATTR;
143 static uint8_t t10l [ 64] IBSS_ATTR;
144 static uint8_t t11l [ 64] IBSS_ATTR;
145 static uint8_t t12l [ 64] IBSS_ATTR;
146 static uint8_t t13l [256] IBSS_ATTR;
147 static uint8_t t15l [256] IBSS_ATTR;
148 static uint8_t t16l [256] IBSS_ATTR;
149 static uint8_t t24l [256] IBSS_ATTR;
150 static struct huffcodetab ht [HTN] IBSS_ATTR;
152 static const uint8_t ht_count_const[2][2][16] =
153 { { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */
154 { 1, 5, 5, 7, 5, 8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } }, /* hleng0 */
155 { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, /* table1 */
156 { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */
158 static const uint8_t t1HB_const[4] = {1,1,1,0};
159 static const uint8_t t2HB_const[9] = {1,2,1,3,1,1,3,2,0};
160 static const uint8_t t3HB_const[9] = {3,2,1,1,1,1,3,2,0};
161 static const uint8_t t5HB_const[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0};
162 static const uint8_t t6HB_const[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0};
164 static const uint8_t t7HB_const[36] =
165 { 1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4,
166 12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 };
168 static const uint8_t t8HB_const[36] =
169 { 3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3,
170 19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 };
172 static const uint8_t t9HB_const[36] =
173 { 7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5,
174 15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 };
176 static const uint8_t t10HB_const[64] =
177 {1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32,
178 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22,
179 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 };
181 static const uint8_t t11HB_const[64] =
182 {3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30,
183 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26,
184 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 };
186 static const uint8_t t12HB_const[64] =
187 {9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21,
188 30,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17,
189 31,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 };
191 static const uint8_t t13HB_const[256] =
192 {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,
193 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,
194 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,
195 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,
196 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,
197 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,
198 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,
199 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,
200 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,
201 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 };
203 static const uint8_t t15HB_const[256] =
204 {7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51,
205 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,
206 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,
207 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,
208 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,
209 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,
210 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,
211 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,
212 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,
213 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};
215 static const uint16_t t16HB_const[256] =
216 {1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47,
217 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209,
218 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65,
219 115,101,179,164,155,264,246,226,395,382,362,9,66,30,59,56,102,185,173,265,142,
220 253,232,400,388,378,445,16,111,54,52,100,184,178,160,133,257,244,228,217,385,
221 366,715,10,98,48,91,88,165,157,148,261,248,407,397,372,380,889,884,8,85,84,81,
222 159,156,143,260,249,427,401,392,383,727,713,708,7,154,76,73,141,131,256,245,
223 426,406,394,384,735,359,710,352,11,139,129,67,125,247,233,229,219,393,743,737,
224 720,885,882,439,4,243,120,118,115,227,223,396,746,742,736,721,712,706,223,436,
225 6,202,224,222,218,216,389,386,381,364,888,443,707,440,437,1728,4,747,211,210,
226 208,370,379,734,723,714,1735,883,877,876,3459,865,2,377,369,102,187,726,722,
227 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3,
228 1,3};
230 static const uint16_t t24HB_const[256] =
231 {15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71,
232 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194,
233 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293,
234 271,16,147,72,69,135,127,118,112,210,200,188,352,323,306,285,540,14,263,66,
235 129,126,119,114,214,202,192,180,341,317,301,281,262,12,249,123,121,117,113,
236 215,206,195,185,347,330,308,291,272,520,10,435,115,111,109,211,203,196,187,
237 353,332,313,298,283,531,381,17,427,212,208,205,201,193,186,177,169,320,303,
238 286,268,514,377,16,335,199,197,191,189,181,174,333,321,305,289,275,521,379,
239 371,11,668,184,183,179,175,344,331,314,304,290,277,530,383,373,366,10,652,346,
240 171,168,164,318,309,299,287,276,263,513,375,368,362,6,648,322,316,312,307,302,
241 292,284,269,261,512,376,370,364,359,4,620,300,296,294,288,282,273,266,515,380,
242 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360,
243 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3};
245 static const uint32_t tab1315_const[256] =
246 { 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a,
247 0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e,
248 0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a,
249 0x0b000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0e000c,0x0e000d,0x0e000d,
250 0x070006,0x080007,0x090007,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,
251 0x0b000a,0x0c000b,0x0c000b,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
252 0x080007,0x090008,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0c000b,
253 0x0c000b,0x0d000b,0x0d000c,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
254 0x090008,0x090008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
255 0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000d,
256 0x0a0009,0x0a0009,0x0b0009,0x0c000a,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
257 0x0d000b,0x0d000b,0x0e000c,0x0d000c,0x0f000d,0x0f000d,0x10000d,0x10000e,
258 0x0a000a,0x0b0009,0x0c000a,0x0c000a,0x0d000a,0x0d000b,0x0d000b,0x0d000b,
259 0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x10000e,
260 0x0b000a,0x0b000a,0x0c000a,0x0d000b,0x0d000b,0x0d000b,0x0e000b,0x0e000c,
261 0x0e000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x12000d,0x12000e,
262 0x0a000a,0x0a000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0e000c,
263 0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x11000e,0x11000e,
264 0x0b000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0d000c,0x0f000c,
265 0x0e000c,0x0f000d,0x0f000d,0x10000d,0x10000d,0x10000e,0x12000e,0x11000e,
266 0x0b000b,0x0c000b,0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000c,
267 0x0e000c,0x0f000d,0x10000d,0x0f000d,0x10000d,0x11000e,0x12000f,0x13000e,
268 0x0c000b,0x0c000b,0x0c000b,0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0e000c,
269 0x0f000d,0x0f000d,0x0f000d,0x10000d,0x11000e,0x11000e,0x11000e,0x12000f,
270 0x0c000c,0x0d000c,0x0d000b,0x0e000c,0x0e000c,0x0f000c,0x0e000d,0x0f000d,
271 0x10000d,0x10000d,0x11000d,0x11000d,0x11000e,0x12000e,0x12000f,0x12000f,
272 0x0d000c,0x0d000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x10000d,
273 0x10000d,0x10000e,0x10000e,0x11000e,0x12000e,0x11000e,0x12000f,0x12000f,
274 0x0e000d,0x0e000d,0x0e000d,0x0f000d,0x0f000d,0x0f000d,0x11000d,0x10000d,
275 0x10000e,0x13000e,0x11000e,0x11000e,0x11000f,0x13000f,0x12000e,0x12000f,
276 0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e,
277 0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f };
279 static const uint32_t tab01_const[16] =
280 { 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007,
281 0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 };
283 static const uint32_t tab23_const[ 9] =
284 { 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 };
286 static const uint32_t tab56_const[16] =
287 { 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007,
288 0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 };
290 static const uint32_t tab789_const[36] =
291 {0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004,
292 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806,
293 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808,
294 0x00b02c09,0x00b02c09,0x00b0300a,0x00802408,0x00902408,0x00a02809,0x00b02c09,
295 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b,
296 0x00c0340b};
298 static const uint32_t tabABC_const[64] =
299 {0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a,
300 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809,
301 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809,
302 0x00c02c0a,0x00b02809,0x00b0280a,0x00802007,0x00902007,0x00a02408,0x00b02c08,
303 0x00c02809,0x00c0300a,0x00b0280a,0x00c02c0a,0x00902408,0x00a02808,0x00b02809,
304 0x00c02c09,0x00c02c0a,0x00c0300a,0x00c02c0a,0x00c0300b,0x00a02409,0x00b02809,
305 0x00c02c0a,0x00c0300a,0x00d0300a,0x00d0340b,0x00c0300a,0x00d0340b,0x00902409,
306 0x00a02409,0x00b02409,0x00c0280a,0x00c02c0a,0x00c0300b,0x00d0300b,0x00d0300c,
307 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b,
308 0x00d0300c};
310 static const uint32_t tab1624_const[256] =
311 {0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a,
312 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c,
313 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009,
314 0x000b000a,0x000b000a,0x000b000b,0x000c000b,0x000c000b,0x000c000c,0x000d000c,
315 0x000e000c,0x000d000c,0x000e000c,0x000a000a,0x00070007,0x00080007,0x00090008,
316 0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000b,0x000d000b,0x000c000b,
317 0x000d000b,0x000d000c,0x000d000c,0x000e000c,0x000e000d,0x000b0009,0x00090008,
318 0x00090008,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,0x000c000b,
319 0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,0x000f000c,
320 0x000c0009,0x000a0009,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,
321 0x000d000a,0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,
322 0x000f000c,0x000f000d,0x000b0009,0x000a000a,0x000a0009,0x000b000a,0x000b000a,
323 0x000c000a,0x000d000a,0x000d000b,0x000e000b,0x000d000b,0x000e000b,0x000e000c,
324 0x000f000c,0x000f000c,0x000f000c,0x0010000c,0x000c0009,0x000b000a,0x000b000a,
325 0x000b000a,0x000c000a,0x000d000a,0x000d000b,0x000d000b,0x000d000b,0x000e000b,
326 0x000e000c,0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000d,0x000c0009,
327 0x000b000b,0x000b000a,0x000c000a,0x000c000a,0x000d000b,0x000d000b,0x000d000b,
328 0x000e000b,0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000d,
329 0x0011000d,0x000c000a,0x000b000b,0x000c000b,0x000c000b,0x000d000b,0x000d000b,
330 0x000d000b,0x000e000b,0x000e000b,0x000f000b,0x000f000c,0x000f000c,0x000f000c,
331 0x0010000c,0x0010000d,0x0010000d,0x000c000a,0x000c000b,0x000c000b,0x000c000b,
332 0x000d000b,0x000d000b,0x000e000b,0x000e000b,0x000f000c,0x000f000c,0x000f000c,
333 0x000f000c,0x0010000c,0x000f000d,0x0010000d,0x000f000d,0x000d000a,0x000c000c,
334 0x000d000b,0x000c000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000e000c,
335 0x000f000c,0x0010000c,0x0010000c,0x0010000d,0x0011000d,0x0011000d,0x0010000d,
336 0x000c000a,0x000d000c,0x000d000c,0x000d000b,0x000d000b,0x000e000b,0x000e000c,
337 0x000f000c,0x0010000c,0x0010000c,0x0010000c,0x0010000c,0x0010000d,0x0010000d,
338 0x000f000d,0x0010000d,0x000d000a,0x000d000c,0x000e000c,0x000e000c,0x000e000c,
339 0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000c,0x0010000d,
340 0x0010000d,0x0010000d,0x0010000d,0x0012000d,0x000d000a,0x000f000c,0x000e000c,
341 0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000c,0x0010000c,0x0010000d,
342 0x0012000d,0x0011000d,0x0011000d,0x0011000d,0x0013000d,0x0011000d,0x000d000a,
343 0x000e000d,0x000f000c,0x000d000c,0x000e000c,0x0010000c,0x0010000c,0x000f000c,
344 0x0010000d,0x0010000d,0x0011000d,0x0012000d,0x0011000d,0x0013000d,0x0011000d,
345 0x0010000d,0x000d000a,0x000a0009,0x000a0009,0x000a0009,0x000b0009,0x000b0009,
346 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a,
347 0x000d000a,0x000d000a,0x000d000a,0x000a0006};
349 static const uint8_t t1l_const[8] = {1,3,2,3,1,4,3,5};
350 static const uint8_t t2l_const[9] = {1,3,6,3,3,5,5,5,6};
351 static const uint8_t t3l_const[9] = {2,2,6,3,2,5,5,5,6};
352 static const uint8_t t5l_const[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8};
353 static const uint8_t t6l_const[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7};
355 static const uint8_t t7l_const[36] =
356 {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};
358 static const uint8_t t8l_const[36] =
359 {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};
361 static const uint8_t t9l_const[36] =
362 {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};
364 static const uint8_t t10l_const[64] =
365 {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,
366 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};
368 static const uint8_t t11l_const[64] =
369 {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,
370 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};
372 static const uint8_t t12l_const[64] =
373 {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,
374 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};
376 static const uint8_t t13l_const[256] =
377 {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,
378 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,
379 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,
380 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,
381 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,
382 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,
383 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,
384 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,
385 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,
386 15,15,16,16,19,18,19,16};
388 static const uint8_t t15l_const[256] =
389 {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,
390 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,
391 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,
392 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,
393 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,
394 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,
395 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,
396 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,
397 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13};
399 static const uint8_t t16l_const[256] =
400 {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,
401 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,
402 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,
403 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,
404 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,
405 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,
406 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,
407 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,
408 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,
409 11,11,11,11,11,11,11,8};
411 static const uint8_t t24l_const[256] =
412 {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,
413 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,
414 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,
415 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,
416 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,
417 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,
418 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,
419 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,
420 7,7,7,7,7,7,8,8,8,8,4};
422 static const struct huffcodetab ht_const[HTN] =
423 { { 0, NULL, NULL}, /* Apparently not used */
424 { 2, t1HB, t1l},
425 { 3, t2HB, t2l},
426 { 3, t3HB, t3l},
427 { 0, NULL, NULL}, /* Apparently not used */
428 { 4, t5HB, t5l},
429 { 4, t6HB, t6l},
430 { 6, t7HB, t7l},
431 { 6, t8HB, t8l},
432 { 6, t9HB, t9l},
433 { 8, t10HB, t10l},
434 { 8, t11HB, t11l},
435 { 8, t12HB, t12l},
436 {16, t13HB, t13l},
437 { 0, NULL, NULL}, /* Apparently not used */
438 {16, t15HB, t15l} };
440 static const struct huffcodebig ht_big[HTN] =
441 { { 16, 1, 1 },
442 { 16, 2, 3 },
443 { 16, 3, 7 },
444 { 16, 4, 15 },
445 { 16, 6, 63 },
446 { 16, 8, 255 },
447 { 16, 10, 1023 },
448 { 16, 13, 8191 },
449 { 16, 4, 15 },
450 { 16, 5, 31 },
451 { 16, 6, 63 },
452 { 16, 7, 127 },
453 { 16, 8, 255 },
454 { 16, 9, 511 },
455 { 16, 11, 2047 },
456 { 16, 13, 8191 } };
458 static const struct
460 uint32_t region0_cnt;
461 uint32_t region1_cnt;
462 } subdv_table[23] =
463 { {0, 0}, /* 0 bands */
464 {0, 0}, /* 1 bands */
465 {0, 0}, /* 2 bands */
466 {0, 0}, /* 3 bands */
467 {0, 0}, /* 4 bands */
468 {0, 1}, /* 5 bands */
469 {1, 1}, /* 6 bands */
470 {1, 1}, /* 7 bands */
471 {1, 2}, /* 8 bands */
472 {2, 2}, /* 9 bands */
473 {2, 3}, /* 10 bands */
474 {2, 3}, /* 11 bands */
475 {3, 4}, /* 12 bands */
476 {3, 4}, /* 13 bands */
477 {3, 4}, /* 14 bands */
478 {4, 5}, /* 15 bands */
479 {4, 5}, /* 16 bands */
480 {4, 6}, /* 17 bands */
481 {5, 6}, /* 18 bands */
482 {5, 6}, /* 19 bands */
483 {5, 7}, /* 20 bands */
484 {6, 7}, /* 21 bands */
485 {6, 7}, /* 22 bands */
488 static const uint32_t sfBand[6][23] =
490 /* Table B.2.b: 22.05 kHz */
491 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
492 /* Table B.2.c: 24 kHz */
493 {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
494 /* Table B.2.a: 16 kHz */
495 {0,6,12,18,24,30,36,44,45,66,80,96,116,140,168,200,238,248,336,396,464,522,576},
496 /* Table B.8.b: 44.1 kHz */
497 {0,4, 8,12,16,20,24,30,36,44,52,62, 74, 90,110,134,162,196,238,288,342,418,576},
498 /* Table B.8.c: 48 kHz */
499 {0,4, 8,12,16,20,24,30,36,42,50,60, 72, 88,106,128,156,190,230,276,330,384,576},
500 /* Table B.8.a: 32 kHz */
501 {0,4, 8,12,16,20,24,30,36,44,54,66, 82,102,126,156,194,240,296,364,448,550,576} };
504 static const short int2idx_const[4096] = /* int2idx[i] = sqrt(i*sqrt(i)); */
506 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9,
507 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16,
508 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21,
509 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26,
510 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
511 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36,
512 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 40,
513 41, 41, 41, 41, 42, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45,
514 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49,
515 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53,
516 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57,
517 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61,
518 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65,
519 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68,
520 68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 72, 72, 72,
521 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75,
522 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 79, 79, 79,
523 79, 79, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82,
524 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 86, 86, 86,
525 86, 86, 86, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89,
526 89, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 93,
527 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, 95, 96, 96, 96,
528 96, 96, 96, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99,
529 99, 99,100,100,100,100,100,100,101,101,101,101,101,101,102,102,102,102,102,102,
530 103,103,103,103,103,103,104,104,104,104,104,104,104,105,105,105,105,105,105,106,
531 106,106,106,106,106,107,107,107,107,107,107,107,108,108,108,108,108,108,109,109,
532 109,109,109,109,110,110,110,110,110,110,110,111,111,111,111,111,111,112,112,112,
533 112,112,112,112,113,113,113,113,113,113,114,114,114,114,114,114,115,115,115,115,
534 115,115,115,116,116,116,116,116,116,117,117,117,117,117,117,117,118,118,118,118,
535 118,118,118,119,119,119,119,119,119,120,120,120,120,120,120,120,121,121,121,121,
536 121,121,122,122,122,122,122,122,122,123,123,123,123,123,123,123,124,124,124,124,
537 124,124,125,125,125,125,125,125,125,126,126,126,126,126,126,126,127,127,127,127,
538 127,127,128,128,128,128,128,128,128,129,129,129,129,129,129,129,130,130,130,130,
539 130,130,131,131,131,131,131,131,131,132,132,132,132,132,132,132,133,133,133,133,
540 133,133,133,134,134,134,134,134,134,134,135,135,135,135,135,135,136,136,136,136,
541 136,136,136,137,137,137,137,137,137,137,138,138,138,138,138,138,138,139,139,139,
542 139,139,139,139,140,140,140,140,140,140,140,141,141,141,141,141,141,141,142,142,
543 142,142,142,142,142,143,143,143,143,143,143,143,144,144,144,144,144,144,144,145,
544 145,145,145,145,145,145,146,146,146,146,146,146,146,147,147,147,147,147,147,147,
545 148,148,148,148,148,148,148,149,149,149,149,149,149,149,150,150,150,150,150,150,
546 150,151,151,151,151,151,151,151,152,152,152,152,152,152,152,153,153,153,153,153,
547 153,153,154,154,154,154,154,154,154,154,155,155,155,155,155,155,155,156,156,156,
548 156,156,156,156,157,157,157,157,157,157,157,158,158,158,158,158,158,158,159,159,
549 159,159,159,159,159,160,160,160,160,160,160,160,160,161,161,161,161,161,161,161,
550 162,162,162,162,162,162,162,163,163,163,163,163,163,163,163,164,164,164,164,164,
551 164,164,165,165,165,165,165,165,165,166,166,166,166,166,166,166,167,167,167,167,
552 167,167,167,167,168,168,168,168,168,168,168,169,169,169,169,169,169,169,169,170,
553 170,170,170,170,170,170,171,171,171,171,171,171,171,172,172,172,172,172,172,172,
554 172,173,173,173,173,173,173,173,174,174,174,174,174,174,174,174,175,175,175,175,
555 175,175,175,176,176,176,176,176,176,176,176,177,177,177,177,177,177,177,178,178,
556 178,178,178,178,178,178,179,179,179,179,179,179,179,180,180,180,180,180,180,180,
557 180,181,181,181,181,181,181,181,182,182,182,182,182,182,182,182,183,183,183,183,
558 183,183,183,184,184,184,184,184,184,184,184,185,185,185,185,185,185,185,186,186,
559 186,186,186,186,186,186,187,187,187,187,187,187,187,187,188,188,188,188,188,188,
560 188,189,189,189,189,189,189,189,189,190,190,190,190,190,190,190,190,191,191,191,
561 191,191,191,191,192,192,192,192,192,192,192,192,193,193,193,193,193,193,193,193,
562 194,194,194,194,194,194,194,195,195,195,195,195,195,195,195,196,196,196,196,196,
563 196,196,196,197,197,197,197,197,197,197,197,198,198,198,198,198,198,198,199,199,
564 199,199,199,199,199,199,200,200,200,200,200,200,200,200,201,201,201,201,201,201,
565 201,201,202,202,202,202,202,202,202,202,203,203,203,203,203,203,203,204,204,204,
566 204,204,204,204,204,205,205,205,205,205,205,205,205,206,206,206,206,206,206,206,
567 206,207,207,207,207,207,207,207,207,208,208,208,208,208,208,208,208,209,209,209,
568 209,209,209,209,209,210,210,210,210,210,210,210,210,211,211,211,211,211,211,211,
569 211,212,212,212,212,212,212,212,212,213,213,213,213,213,213,213,213,214,214,214,
570 214,214,214,214,214,215,215,215,215,215,215,215,215,216,216,216,216,216,216,216,
571 216,217,217,217,217,217,217,217,217,218,218,218,218,218,218,218,218,219,219,219,
572 219,219,219,219,219,220,220,220,220,220,220,220,220,221,221,221,221,221,221,221,
573 221,222,222,222,222,222,222,222,222,223,223,223,223,223,223,223,223,224,224,224,
574 224,224,224,224,224,225,225,225,225,225,225,225,225,226,226,226,226,226,226,226,
575 226,227,227,227,227,227,227,227,227,228,228,228,228,228,228,228,228,229,229,229,
576 229,229,229,229,229,229,230,230,230,230,230,230,230,230,231,231,231,231,231,231,
577 231,231,232,232,232,232,232,232,232,232,233,233,233,233,233,233,233,233,234,234,
578 234,234,234,234,234,234,234,235,235,235,235,235,235,235,235,236,236,236,236,236,
579 236,236,236,237,237,237,237,237,237,237,237,238,238,238,238,238,238,238,238,238,
580 239,239,239,239,239,239,239,239,240,240,240,240,240,240,240,240,241,241,241,241,
581 241,241,241,241,242,242,242,242,242,242,242,242,242,243,243,243,243,243,243,243,
582 243,244,244,244,244,244,244,244,244,245,245,245,245,245,245,245,245,245,246,246,
583 246,246,246,246,246,246,247,247,247,247,247,247,247,247,248,248,248,248,248,248,
584 248,248,248,249,249,249,249,249,249,249,249,250,250,250,250,250,250,250,250,250,
585 251,251,251,251,251,251,251,251,252,252,252,252,252,252,252,252,253,253,253,253,
586 253,253,253,253,253,254,254,254,254,254,254,254,254,255,255,255,255,255,255,255,
587 255,255,256,256,256,256,256,256,256,256,257,257,257,257,257,257,257,257,257,258,
588 258,258,258,258,258,258,258,259,259,259,259,259,259,259,259,259,260,260,260,260,
589 260,260,260,260,261,261,261,261,261,261,261,261,261,262,262,262,262,262,262,262,
590 262,263,263,263,263,263,263,263,263,263,264,264,264,264,264,264,264,264,265,265,
591 265,265,265,265,265,265,265,266,266,266,266,266,266,266,266,267,267,267,267,267,
592 267,267,267,267,268,268,268,268,268,268,268,268,268,269,269,269,269,269,269,269,
593 269,270,270,270,270,270,270,270,270,270,271,271,271,271,271,271,271,271,271,272,
594 272,272,272,272,272,272,272,273,273,273,273,273,273,273,273,273,274,274,274,274,
595 274,274,274,274,275,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276,
596 276,276,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,279,
597 279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,281,281,281,
598 281,281,281,281,281,282,282,282,282,282,282,282,282,282,283,283,283,283,283,283,
599 283,283,283,284,284,284,284,284,284,284,284,284,285,285,285,285,285,285,285,285,
600 286,286,286,286,286,286,286,286,286,287,287,287,287,287,287,287,287,287,288,288,
601 288,288,288,288,288,288,288,289,289,289,289,289,289,289,289,289,290,290,290,290,
602 290,290,290,290,291,291,291,291,291,291,291,291,291,292,292,292,292,292,292,292,
603 292,292,293,293,293,293,293,293,293,293,293,294,294,294,294,294,294,294,294,294,
604 295,295,295,295,295,295,295,295,295,296,296,296,296,296,296,296,296,296,297,297,
605 297,297,297,297,297,297,297,298,298,298,298,298,298,298,298,299,299,299,299,299,
606 299,299,299,299,300,300,300,300,300,300,300,300,300,301,301,301,301,301,301,301,
607 301,301,302,302,302,302,302,302,302,302,302,303,303,303,303,303,303,303,303,303,
608 304,304,304,304,304,304,304,304,304,305,305,305,305,305,305,305,305,305,306,306,
609 306,306,306,306,306,306,306,307,307,307,307,307,307,307,307,307,308,308,308,308,
610 308,308,308,308,308,309,309,309,309,309,309,309,309,309,310,310,310,310,310,310,
611 310,310,310,311,311,311,311,311,311,311,311,311,312,312,312,312,312,312,312,312,
612 312,313,313,313,313,313,313,313,313,313,314,314,314,314,314,314,314,314,314,315,
613 315,315,315,315,315,315,315,315,316,316,316,316,316,316,316,316,316,317,317,317,
614 317,317,317,317,317,317,318,318,318,318,318,318,318,318,318,318,319,319,319,319,
615 319,319,319,319,319,320,320,320,320,320,320,320,320,320,321,321,321,321,321,321,
616 321,321,321,322,322,322,322,322,322,322,322,322,323,323,323,323,323,323,323,323,
617 323,324,324,324,324,324,324,324,324,324,325,325,325,325,325,325,325,325,325,325,
618 326,326,326,326,326,326,326,326,326,327,327,327,327,327,327,327,327,327,328,328,
619 328,328,328,328,328,328,328,329,329,329,329,329,329,329,329,329,330,330,330,330,
620 330,330,330,330,330,330,331,331,331,331,331,331,331,331,331,332,332,332,332,332,
621 332,332,332,332,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,
622 334,334,335,335,335,335,335,335,335,335,335,335,336,336,336,336,336,336,336,336,
623 336,337,337,337,337,337,337,337,337,337,338,338,338,338,338,338,338,338,338,338,
624 339,339,339,339,339,339,339,339,339,340,340,340,340,340,340,340,340,340,341,341,
625 341,341,341,341,341,341,341,341,342,342,342,342,342,342,342,342,342,343,343,343,
626 343,343,343,343,343,343,344,344,344,344,344,344,344,344,344,344,345,345,345,345,
627 345,345,345,345,345,346,346,346,346,346,346,346,346,346,347,347,347,347,347,347,
628 347,347,347,347,348,348,348,348,348,348,348,348,348,349,349,349,349,349,349,349,
629 349,349,350,350,350,350,350,350,350,350,350,350,351,351,351,351,351,351,351,351,
630 351,352,352,352,352,352,352,352,352,352,352,353,353,353,353,353,353,353,353,353,
631 354,354,354,354,354,354,354,354,354,355,355,355,355,355,355,355,355,355,355,356,
632 356,356,356,356,356,356,356,356,357,357,357,357,357,357,357,357,357,357,358,358,
633 358,358,358,358,358,358,358,359,359,359,359,359,359,359,359,359,359,360,360,360,
634 360,360,360,360,360,360,361,361,361,361,361,361,361,361,361,361,362,362,362,362,
635 362,362,362,362,362,363,363,363,363,363,363,363,363,363,363,364,364,364,364,364,
636 364,364,364,364,365,365,365,365,365,365,365,365,365,365,366,366,366,366,366,366,
637 366,366,366,367,367,367,367,367,367,367,367,367,367,368,368,368,368,368,368,368,
638 368,368,369,369,369,369,369,369,369,369,369,369,370,370,370,370,370,370,370,370,
639 370,370,371,371,371,371,371,371,371,371,371,372,372,372,372,372,372,372,372,372,
640 372,373,373,373,373,373,373,373,373,373,374,374,374,374,374,374,374,374,374,374,
641 375,375,375,375,375,375,375,375,375,375,376,376,376,376,376,376,376,376,376,377,
642 377,377,377,377,377,377,377,377,377,378,378,378,378,378,378,378,378,378,379,379,
643 379,379,379,379,379,379,379,379,380,380,380,380,380,380,380,380,380,380,381,381,
644 381,381,381,381,381,381,381,382,382,382,382,382,382,382,382,382,382,383,383,383,
645 383,383,383,383,383,383,383,384,384,384,384,384,384,384,384,384,385,385,385,385,
646 385,385,385,385,385,385,386,386,386,386,386,386,386,386,386,386,387,387,387,387,
647 387,387,387,387,387,387,388,388,388,388,388,388,388,388,388,389,389,389,389,389,
648 389,389,389,389,389,390,390,390,390,390,390,390,390,390,390,391,391,391,391,391,
649 391,391,391,391,391,392,392,392,392,392,392,392,392,392,393,393,393,393,393,393,
650 393,393,393,393,394,394,394,394,394,394,394,394,394,394,395,395,395,395,395,395,
651 395,395,395,395,396,396,396,396,396,396,396,396,396,397,397,397,397,397,397,397,
652 397,397,397,398,398,398,398,398,398,398,398,398,398,399,399,399,399,399,399,399,
653 399,399,399,400,400,400,400,400,400,400,400,400,400,401,401,401,401,401,401,401,
654 401,401,402,402,402,402,402,402,402,402,402,402,403,403,403,403,403,403,403,403,
655 403,403,404,404,404,404,404,404,404,404,404,404,405,405,405,405,405,405,405,405,
656 405,405,406,406,406,406,406,406,406,406,406,406,407,407,407,407,407,407,407,407,
657 407,407,408,408,408,408,408,408,408,408,408,408,409,409,409,409,409,409,409,409,
658 409,410,410,410,410,410,410,410,410,410,410,411,411,411,411,411,411,411,411,411,
659 411,412,412,412,412,412,412,412,412,412,412,413,413,413,413,413,413,413,413,413,
660 413,414,414,414,414,414,414,414,414,414,414,415,415,415,415,415,415,415,415,415,
661 415,416,416,416,416,416,416,416,416,416,416,417,417,417,417,417,417,417,417,417,
662 417,418,418,418,418,418,418,418,418,418,418,419,419,419,419,419,419,419,419,419,
663 419,420,420,420,420,420,420,420,420,420,420,421,421,421,421,421,421,421,421,421,
664 421,422,422,422,422,422,422,422,422,422,422,423,423,423,423,423,423,423,423,423,
665 423,424,424,424,424,424,424,424,424,424,424,425,425,425,425,425,425,425,425,425,
666 425,426,426,426,426,426,426,426,426,426,426,427,427,427,427,427,427,427,427,427,
667 427,428,428,428,428,428,428,428,428,428,428,429,429,429,429,429,429,429,429,429,
668 429,430,430,430,430,430,430,430,430,430,430,431,431,431,431,431,431,431,431,431,
669 431,432,432,432,432,432,432,432,432,432,432,433,433,433,433,433,433,433,433,433,
670 433,434,434,434,434,434,434,434,434,434,434,435,435,435,435,435,435,435,435,435,
671 435,435,436,436,436,436,436,436,436,436,436,436,437,437,437,437,437,437,437,437,
672 437,437,438,438,438,438,438,438,438,438,438,438,439,439,439,439,439,439,439,439,
673 439,439,440,440,440,440,440,440,440,440,440,440,441,441,441,441,441,441,441,441,
674 441,441,442,442,442,442,442,442,442,442,442,442,443,443,443,443,443,443,443,443,
675 443,443,443,444,444,444,444,444,444,444,444,444,444,445,445,445,445,445,445,445,
676 445,445,445,446,446,446,446,446,446,446,446,446,446,447,447,447,447,447,447,447,
677 447,447,447,448,448,448,448,448,448,448,448,448,448,448,449,449,449,449,449,449,
678 449,449,449,449,450,450,450,450,450,450,450,450,450,450,451,451,451,451,451,451,
679 451,451,451,451,452,452,452,452,452,452,452,452,452,452,453,453,453,453,453,453,
680 453,453,453,453,453,454,454,454,454,454,454,454,454,454,454,455,455,455,455,455,
681 455,455,455,455,455,456,456,456,456,456,456,456,456,456,456,457,457,457,457,457,
682 457,457,457,457,457,457,458,458,458,458,458,458,458,458,458,458,459,459,459,459,
683 459,459,459,459,459,459,460,460,460,460,460,460,460,460,460,460,460,461,461,461,
684 461,461,461,461,461,461,461,462,462,462,462,462,462,462,462,462,462,463,463,463,
685 463,463,463,463,463,463,463,463,464,464,464,464,464,464,464,464,464,464,465,465,
686 465,465,465,465,465,465,465,465,466,466,466,466,466,466,466,466,466,466,466,467,
687 467,467,467,467,467,467,467,467,467,468,468,468,468,468,468,468,468,468,468,469,
688 469,469,469,469,469,469,469,469,469,469,470,470,470,470,470,470,470,470,470,470,
689 471,471,471,471,471,471,471,471,471,471,472,472,472,472,472,472,472,472,472,472,
690 472,473,473,473,473,473,473,473,473,473,473,474,474,474,474,474,474,474,474,474,
691 474,475,475,475,475,475,475,475,475,475,475,475,476,476,476,476,476,476,476,476,
692 476,476,477,477,477,477,477,477,477,477,477,477,477,478,478,478,478,478,478,478,
693 478,478,478,479,479,479,479,479,479,479,479,479,479,479,480,480,480,480,480,480,
694 480,480,480,480,481,481,481,481,481,481,481,481,481,481,482,482,482,482,482,482,
695 482,482,482,482,482,483,483,483,483,483,483,483,483,483,483,484,484,484,484,484,
696 484,484,484,484,484,484,485,485,485,485,485,485,485,485,485,485,486,486,486,486,
697 486,486,486,486,486,486,486,487,487,487,487,487,487,487,487,487,487,488,488,488,
698 488,488,488,488,488,488,488,488,489,489,489,489,489,489,489,489,489,489,490,490,
699 490,490,490,490,490,490,490,490,490,491,491,491,491,491,491,491,491,491,491,492,
700 492,492,492,492,492,492,492,492,492,492,493,493,493,493,493,493,493,493,493,493,
701 494,494,494,494,494,494,494,494,494,494,494,495,495,495,495,495,495,495,495,495,
702 495,496,496,496,496,496,496,496,496,496,496,496,497,497,497,497,497,497,497,497,
703 497,497,497,498,498,498,498,498,498,498,498,498,498,499,499,499,499,499,499,499,
704 499,499,499,499,500,500,500,500,500,500,500,500,500,500,501,501,501,501,501,501,
705 501,501,501,501,501,502,502,502,502,502,502,502,502,502,502,503,503,503,503,503,
706 503,503,503,503,503,503,504,504,504,504,504,504,504,504,504,504,504,505,505,505,
707 505,505,505,505,505,505,505,506,506,506,506,506,506,506,506,506,506,506,507,507,
708 507,507,507,507,507,507,507,507,507,508,508,508,508,508,508,508,508,508,508,509,
709 509,509,509,509,509,509,509,509,509,509,510,510,510,510,510,510,510,510,510,510,
710 510,511,511,511,511,511,511,511,511,511,511,512,512,512,512,512 };
712 static const int order[32] =
713 { 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
714 2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 };
716 static const long sampr_index[2][3] =
717 { { 22050, 24000, 16000 }, /* MPEG 2 */
718 { 44100, 48000, 32000 } }; /* MPEG 1 */
720 static const long bitr_index[2][15] =
721 { {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160}, /* MPEG 2 */
722 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} }; /* MPEG 1 */
724 static const int num_bands[3][15] =
725 { {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32},
726 {0,10,10,10,10,10,12,14, 18, 24, 26, 28, 30, 32, 32},
727 {0,10,12,14,18,24,26,28, 30, 32, 32, 32, 32, 32, 32} };
729 static const int cx_const[9] =
730 { 16135, 10531, 5604, 15396, -2845,-12551, 14189, 8192, 16384 };
732 static const int ca_const[8] =
733 {-16859,-15458,-10269, -5961, -3099, -1342, -465, -121 };
735 static const int cs_const[8] =
736 { 28098, 28893, 31117, 32221, 32621, 32740, 32765, 32768 };
738 static const short enwindow_const[15*27+24] =
739 { 0, 65, 593, 1766, 22228, 2115, 611, 62,
740 8, 119, 1419, 10564,-11659,-1635,-154, -9,
741 -8, -119,-1419,-10564, 11659, 1635, 154, 9, 464, 100, 91,
742 0, 69, 604, 1635, 23148, 2363, 643, 62,
743 7, 107, 1368, 10449,-12733,-1818,-180,-11,
744 -7, -107,-1368,-10449, 12733, 1818, 180, 11, 420, 200, 164,
745 0, 72, 608, 1465, 23979, 2600, 671, 63,
746 7, 94, 1305, 10265,-13818,-2004,-207,-12,
747 -7, -94,-1305,-10265, 13818, 2004, 207, 12, 380, 297, 220,
748 0, 76, 606, 1256, 24718, 2825, 693, 63,
749 6, 81, 1232, 10016,-14908,-2192,-236,-14,
750 -6, -81,-1232,-10016, 14908, 2192, 236, 14, 342, 392, 262,
751 0, 78, 597, 1007, 25359, 3033, 712, 63,
752 6, 68, 1150, 9706,-15995,-2380,-267,-15,
753 -6, -68,-1150, -9706, 15995, 2380, 267, 15, 307, 483, 289,
754 0, 80, 580, 719, 25901, 3224, 726, 62,
755 6, 54, 1060, 9343,-17072,-2565,-299,-17,
756 -6, -54,-1060, -9343, 17072, 2565, 299, 17, 274, 569, 304,
757 -1, 82, 555, 391, 26339, 3395, 735, 61,
758 5, 40, 963, 8930,-18131,-2747,-332,-19,
759 -5, -40, -963, -8930, 18131, 2747, 332, 19, 242, 650, 307,
760 -1, 83, 523, 26, 26672, 3545, 740, 60,
761 5, 27, 861, 8474,-19164,-2923,-366,-21,
762 -5, -27, -861, -8474, 19164, 2923, 366, 21, 212, 724, 300,
763 -1, 83, 482, -376, 26900, 3672, 739, 58,
764 4, 14, 756, 7981,-20163,-3092,-401,-24,
765 -4, -14, -756, -7981, 20163, 3092, 401, 24, 183, 792, 283,
766 -1, 82, 433, -812, 27022, 3776, 735, 56,
767 4, 1, 648, 7456,-21122,-3250,-435,-26,
768 -4, -1, -648, -7456, 21122, 3250, 435, 26, 155, 851, 258,
769 -1, 81, 376, -1281, 27038, 3855, 726, 54,
770 3, -11, 539, 6907,-22032,-3397,-470,-28,
771 -3, 11, -539, -6907, 22032, 3397, 470, 28, 128, 903, 226,
772 -1, 78, 312, -1778, 26951, 3910, 713, 52,
773 3, -22, 430, 6338,-22887,-3530,-503,-31,
774 -3, 22, -430, -6338, 22887, 3530, 503, 31, 102, 946, 188,
775 -2, 75, 239, -2302, 26761, 3941, 696, 49,
776 3, -33, 322, 5757,-23678,-3648,-537,-34,
777 -3, 33, -322, -5757, 23678, 3648, 537, 34, 76, 980, 145,
778 -2, 70, 160, -2848, 26472, 3948, 676, 47,
779 3, -42, 217, 5167,-24399,-3749,-568,-36,
780 -3, 42, -217, -5167, 24399, 3749, 568, 36, 50, 1004, 99,
781 -2, 65, 74, -3412, 26087, 3931, 653, 44,
782 2, -51, 115, 4577,-25045,-3830,-599,-39,
783 -2, 51, -115, -4577, 25045, 3830, 599, 39, 25, 1019, 50,
785 25610,3891,627,42,-3990,-18,58,-2,
786 21226,-21226,10604,-10604,1860,-1860,1458,-1458,576,-576,130,-130,60,-60,8,-8
789 static const int win_const[18][4] = {
790 { -3072, -134, -146, 3352 },
791 { -2747, -362, -471, 3579 },
792 { -2387, -529, -831, 3747 },
793 { -2004, -632,-1214, 3850 },
794 { -1609, -666,-1609, 3884 },
795 { -1214, -632,-2004, 3850 },
796 { -831, -529,-2387, 3747 },
797 { -471, -362,-2747, 3579 },
798 { -146, -134,-3072, 3352 },
799 { 134,-3072,-3352, -146 },
800 { 362,-2747,-3579, -471 },
801 { 529,-2387,-3747, -831 },
802 { 632,-2004,-3850,-1214 },
803 { 666,-1609,-3884,-1609 },
804 { 632,-1214,-3850,-2004 },
805 { 529, -831,-3747,-2387 },
806 { 362, -471,-3579,-2747 },
807 { 134, -146,-3352,-3072 } };
810 static const char* wav_filename;
811 static int mp3file, wavfile, wav_size, frames;
812 static uint32_t enc_buffer[16384]; /* storage for 65536 Bytes */
813 static int enc_chunk = 0; /* encode chunk counter */
814 static int enc_size;
815 static config_t cfg;
816 static uint8_t band_scale_f[22];
819 /* forward declarations */
820 static int HuffmanCode( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
821 static int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
822 static void putbits(uint32_t val, uint32_t nbit);
823 static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
824 uint32_t len, int *bits);
825 static int find_best_3( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
826 uint32_t len, int *bits);
827 static int count_bit1 ( short *ix, uint32_t start, uint32_t end, int *bits );
828 static int count_bigv ( short *ix, uint32_t start, uint32_t end, int table0, int table1,
829 int *bits);
832 bool checkString(int fd, char *string)
834 char temp[4];
836 rb->read(fd, temp, 4);
838 return (*(long*)temp == *(long*)string) ? 1 : 0;
841 int Read16BitsLowHigh(int fd)
843 char first, second;
845 rb->read(fd, &first, 1);
846 rb->read(fd, &second, 1);
848 return ((int)second << 8) | (first & 0xff);
852 int Read32BitsLowHigh(int fd)
854 int first = 0xffff & Read16BitsLowHigh(fd);
855 int second = 0xffff & Read16BitsLowHigh(fd);
857 return (second << 16) + first;
860 int wave_open(void)
862 unsigned short wFormatTag;
863 unsigned long dAvgBytesPerSec;
864 unsigned short wBlockAlign;
865 unsigned short bits_per_samp;
866 long header_size;
868 if((wavfile = rb->open(wav_filename, O_RDONLY)) < 0)
869 return -1;
871 if(!checkString(wavfile,"RIFF")) return -2;
872 Read32BitsLowHigh(wavfile); /* complete wave chunk size */
873 if(!checkString(wavfile,"WAVE")) return -3;
874 if(!checkString(wavfile,"fmt ")) return -4;
876 header_size = Read32BitsLowHigh(wavfile); /* chunk size */
877 wFormatTag = Read16BitsLowHigh(wavfile);
879 cfg.channels = Read16BitsLowHigh(wavfile);
880 cfg.samplerate = Read32BitsLowHigh(wavfile);
881 dAvgBytesPerSec = Read32BitsLowHigh(wavfile);
882 wBlockAlign = Read16BitsLowHigh(wavfile);
883 bits_per_samp = Read16BitsLowHigh(wavfile);
885 if(wFormatTag != 0x0001) return -5;
886 if(bits_per_samp != 16) return -6;
887 if(cfg.channels > 2) return -7;
888 if(!checkString(wavfile,"data")) return -8;
890 header_size = 0x28;
891 wav_size = rb->filesize(wavfile);
892 rb->lseek(wavfile, header_size, SEEK_SET);
894 return 0;
897 int read_samples(uint32_t *buffer, int num_samples)
899 int s, samples = rb->read(wavfile, buffer, 4 * num_samples) / 4;
900 /* Pad last sample with zeros */
901 for(s=samples; s<num_samples; s++)
902 buffer[s] = 0;
904 return samples;
907 inline uint32_t myswap32(uint32_t val)
909 const uint8_t* v = (const uint8_t*)&val;
911 return ((uint32_t)v[0]<<24) | ((uint32_t)v[1]<<16) | ((uint32_t)v[2]<<8) | v[3];
914 static void encodeSideInfo( side_info_t si[2][2] )
916 int gr, ch, header;
917 uint32_t cc=0, sz=0;
920 * MPEG header layout:
921 * AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
922 * A (31-21) = frame sync
923 * B (20-19) = MPEG type
924 * C (18-17) = MPEG layer
925 * D (16) = protection bit
926 * E (15-12) = bitrate index
927 * F (11-10) = samplerate index
928 * G (9) = padding bit
929 * H (8) = private bit
930 * I (7-6) = channel mode
931 * J (5-4) = mode extension (jstereo only)
932 * K (3) = copyright bit
933 * L (2) = original
934 * M (1-0) = emphasis
937 header = (0xfff00000) | /* frame sync (AAAAAAAAA AAA)
938 mp3 type (upper): 1 (B) */
939 (0x01 << 17) | /* mp3 layer: 01 (CC) */
940 ( 0x1 << 16) | /* mp3 crc: 1 (D) */
941 ( 0x1 << 2); /* mp3 org: 1 (L) */
942 header |= cfg.mpg.type << 19;
943 header |= cfg.mpg.bitr_id << 12;
944 header |= cfg.mpg.smpl_id << 10;
945 header |= cfg.mpg.padding << 9;
946 header |= cfg.mpg.mode << 6;
947 /* no emphasis (bits 0-1) */
948 putbits( header, 32 );
950 if(cfg.mpg.type == 1)
951 { /* MPEG1 */
952 if(cfg.channels == 2) { putlong( 0, 20); }
953 else { putlong( 0, 18); }
955 for(gr=0; gr<cfg.granules; gr++)
956 for(ch=0; ch<cfg.channels; ch++)
958 side_info_t *gi = &si[gr][ch];
960 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
961 putlong( gi->address3>>1, 9 );
962 putlong( gi->global_gain, 8 );
963 putlong( 9, 4 ); /* set scale_facs compr type */
964 putlong( gi->table_select[0], 6 );
965 putlong( gi->table_select[1], 5 );
966 putlong( gi->table_select[2], 5 );
967 putlong( gi->region_0_1, 7 );
968 putlong( 1 , 2 ); /* set scale_facs to 1bit */
969 putlong( gi->table_select[3], 1 );
972 else
973 { /* MPEG2 */
974 if(cfg.channels == 2) { putlong( 0, 10); }
975 else { putlong( 0, 9); }
977 for(ch=0; ch<cfg.channels; ch++)
979 side_info_t *gi = &si[0][ch];
981 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
982 putlong( gi->address3>>1, 9 );
983 putlong( gi->global_gain, 8 );
984 putlong( 0xCA, 9 ); /* set scale_facs compr type */
985 putlong( gi->table_select[0], 6 );
986 putlong( gi->table_select[1], 5 );
987 putlong( gi->table_select[2], 5 );
988 putlong( gi->region_0_1 , 7 );
989 putlong( 1 , 1 ); /* set scale_facs to 1bit */
990 putlong( gi->table_select[3], 1 );
993 /* flush remaining bits */
994 putbits(cc, sz);
997 /* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS,
998 as well as the definitions of the side information on pages 26 and 27. */
999 static void Huffmancodebits( short *ix, char *xr_sign, side_info_t *gi )
1001 int region1 = gi->address1;
1002 int region2 = gi->address2;
1003 int bigvals = gi->address3;
1004 int count1 = bigvals + (gi->count1 << 2);
1005 int stuffBits = 0;
1006 int bits = 0;
1007 int i, v;
1009 for(i=v=0; i<32; i+=2)
1010 v |= band_scale_f[i>>1] << (30-i);
1011 putbits(v, 32); // store scale_facs (part1)
1013 for(v=0; i<42; i+=2)
1014 v |= band_scale_f[i>>1] << (40-i);
1015 putbits(v, 10); // store scale_facs (part2)
1017 if(region1 > 0)
1018 bits += HuffmanCode(ix, xr_sign, 0 , region1, gi->table_select[0]);
1020 if(region2 > region1)
1021 bits += HuffmanCode(ix, xr_sign, region1, region2, gi->table_select[1]);
1023 if(bigvals > region2)
1024 bits += HuffmanCode(ix, xr_sign, region2, bigvals, gi->table_select[2]);
1026 if(count1 > bigvals)
1027 bits += HuffmanCod1(ix, xr_sign, bigvals, count1, gi->table_select[3]);
1029 if((stuffBits = gi->part2_3_length - bits) > 0)
1031 int stuffWords = stuffBits >> 5;
1032 int remainBits = stuffBits & 31;
1034 if( remainBits )
1035 putbits( ~0, remainBits );
1037 while( stuffWords-- )
1038 putbits( ~0, 32 ); /* Huffman code tables leed to padding ones */
1042 int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int tbl)
1044 uint32_t cc=0, sz=0;
1045 uint32_t i, d, p;
1046 int sumbit=0, s=0, l=0, v, w, x, y;
1047 #define sgnv xr_sign[i+0]
1048 #define sgnw xr_sign[i+1]
1049 #define sgnx xr_sign[i+2]
1050 #define sgny xr_sign[i+3]
1052 for(i=begin; i<end; i+=4)
1054 v = ix[i+0];
1055 w = ix[i+1];
1056 x = ix[i+2];
1057 y = ix[i+3];
1058 p = (v << 3) + (w << 2) + (x << 1) + y;
1060 switch(p)
1062 case 0: l=0; s = 0; break;
1063 case 1: l=1; s = sgny; break;
1064 case 2: l=1; s = sgnx; break;
1065 case 3: l=2; s = (sgnx << 1) + sgny; break;
1066 case 4: l=1; s = sgnw; break;
1067 case 5: l=2; s = (sgnw << 1) + sgny; break;
1068 case 6: l=2; s = (sgnw << 1) + sgnx; break;
1069 case 7: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break;
1070 case 8: l=1; s = sgnv; break;
1071 case 9: l=2; s = (sgnv << 1) + sgny; break;
1072 case 10: l=2; s = (sgnv << 1) + sgnx; break;
1073 case 11: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break;
1074 case 12: l=2; s = (sgnv << 1) + sgnw; break;
1075 case 13: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break;
1076 case 14: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break;
1077 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break;
1080 d = (ht_count[tbl][0][p] << l) + s;
1081 l = ht_count[tbl][1][p];
1082 putlong( d, l );
1083 sumbit += l;
1086 /* flush remaining bits */
1087 putbits(cc, sz);
1089 return sumbit;
1092 /* Implements the pseudocode of page 98 of the IS */
1093 int HuffmanCode(short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table)
1095 uint32_t cc=0, sz=0, code;
1096 uint32_t i, xl=0, yl=0, idx;
1097 int x, y, bit, sumbit=0;
1098 #define sign_x xr_sign[i+0]
1099 #define sign_y xr_sign[i+1]
1101 if(table == 0)
1102 return 0;
1104 if( table > 15 )
1105 { /* ESC-table is used */
1106 uint32_t linbits = ht_big[table-16].linbits;
1107 uint16_t *hffcode = table < 24 ? t16HB : t24HB;
1108 uint8_t *hlen = table < 24 ? t16l : t24l;
1110 for(i=begin; i<end; i+=2)
1112 x = ix[ i ];
1113 y = ix[i+1];
1115 if(x > 14) { xl = x - 15; x = 15; }
1116 if(y > 14) { yl = y - 15; y = 15; }
1118 idx = x * 16 + y;
1119 code = hffcode[idx];
1120 bit = hlen [idx];
1122 if(x)
1124 if(x > 14)
1126 code = (code << linbits) | xl;
1127 bit += linbits;
1130 code = (code << 1) | sign_x;
1131 bit += 1;
1134 if(y)
1136 if(y > 14)
1138 if(bit + linbits + 1 > 32)
1140 putlong( code, bit );
1141 sumbit += bit;
1142 code = bit = 0;
1145 code = (code << linbits) | yl;
1146 bit += linbits;
1149 code = (code << 1) | sign_y;
1150 bit += 1;
1153 putlong( code, bit );
1154 sumbit += bit;
1157 else
1158 { /* No ESC-words */
1159 const struct huffcodetab *h = &ht[table];
1161 for(i=begin; i<end; i+=2)
1163 x = ix[i];
1164 y = ix[i+1];
1166 idx = x * h->len + y;
1167 code = h->table[idx];
1168 bit = h->hlen [idx];
1170 if(x)
1172 code = (code << 1) | sign_x;
1173 bit += 1;
1176 if(y)
1178 code = (code << 1) | sign_y;
1179 bit += 1;
1182 putlong( code, bit );
1183 sumbit += bit;
1187 /* flush remaining bits */
1188 putbits(cc, sz);
1190 return sumbit;
1193 void putbits(uint32_t val, uint32_t nbit)
1195 int new_bitpos = CodedData.bitpos + nbit;
1196 int ptrpos = CodedData.bitpos >> 5;
1198 val = val & (0xffffffff >> (32 - nbit));
1200 /* data fit in one uint32_t */
1201 if(((new_bitpos - 1) >> 5) == ptrpos)
1202 CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31);
1203 else
1205 CodedData.bbuf[ptrpos ] |= val >> ((new_bitpos - 32) & 31);
1206 CodedData.bbuf[ptrpos+1] |= val << ((32 - new_bitpos) & 31);
1209 CodedData.bitpos = new_bitpos;
1212 /***************************************************************************/
1213 /* Choose the Huffman table that will encode ix[begin..end] with */
1214 /* the fewest bits. */
1215 /* Note: This code contains knowledge about the sizes and characteristic */
1216 /* of the Huffman tables as defined in the IS (Table B.7), and will not */
1217 /* work with any arbitrary tables. */
1218 /***************************************************************************/
1219 int choose_table( short *ix, uint32_t begin, uint32_t end, int *bits )
1221 uint32_t i;
1222 int max, table0, table1;
1224 for(i=begin,max=0; i<end; i++)
1225 if(ix[i] > max)
1226 max = ix[i];
1228 if(max < 16)
1230 /* tables without linbits */
1231 /* indx: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1232 /* len: 0, 2, 3, 3, 0, 4, 4, 6, 6, 6, 8, 8, 8, 16, 0, 16 */
1233 switch(max)
1235 case 0: return 0;
1236 case 1: return count_bit1(ix, begin, end, bits);
1237 case 2: return 2 + find_best_2(ix, begin, end, tab23, 3, bits);
1238 case 3: return 5 + find_best_2(ix, begin, end, tab56, 4, bits);
1239 case 4:
1240 case 5: return 7 + find_best_3(ix, begin, end, tab789, 6, bits);
1241 case 6:
1242 case 7: return 10 + find_best_3(ix, begin, end, tabABC, 8, bits);
1243 default: return 13 + find_best_2(ix, begin, end, tab1315, 16, bits) * 2;
1246 else
1248 /* tables with linbits */
1249 max -= 15;
1251 for(table0=0; table0<8; table0++)
1252 if(ht_big[table0].linmax >= max)
1253 break;
1255 for(table1=8; table1<16; table1++)
1256 if(ht_big[table1].linmax >= max)
1257 break;
1259 return 16 + count_bigv(ix, begin, end, table0, table1, bits);
1263 int find_best_2(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1264 uint32_t len, int *bits)
1266 uint32_t i, sum = 0;
1268 for(i=start; i<end; i+=2)
1269 sum += table[ix[i] * len + ix[i+1]];
1271 if((sum & 0xffff) <= (sum >> 16))
1273 *bits = (sum & 0xffff);
1274 return 1;
1276 else
1278 *bits = sum >> 16;
1279 return 0;
1283 int find_best_3(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1284 uint32_t len, int *bits)
1286 uint32_t i, j, sum = 0;
1287 int sum1 = 0;
1288 int sum2 = 0;
1289 int sum3 = 0;
1291 /* avoid overflow in packed additions: 78*13 < 1024 */
1292 for(i=start; i<end; )
1294 j = i + 2*78 > end ? end : i + 2*78;
1296 for(sum=0; i<j; i+=2)
1297 sum += table[ix[i] * len + ix[i+1]];
1299 sum1 += (sum >> 20);
1300 sum2 += (sum >> 10) & 0x3ff;
1301 sum3 += (sum >> 0) & 0x3ff;
1304 i = 0;
1305 if(sum1 > sum2) { sum1 = sum2; i = 1; }
1306 if(sum1 > sum3) { sum1 = sum3; i = 2; }
1308 *bits = sum1;
1310 return i;
1313 /*************************************************************************/
1314 /* Function: Count the number of bits necessary to code the subregion. */
1315 /*************************************************************************/
1316 int count_bit1(short *ix, uint32_t start, uint32_t end, int *bits )
1318 uint32_t i, sum = 0;
1320 for(i=start; i<end; i+=2)
1321 sum += t1l[4 + ix[i] * 2 + ix[i+1]];
1323 *bits = sum;
1325 return 1; /* this is table1 */
1328 int count_bigv(short *ix, uint32_t start, uint32_t end, int table0,
1329 int table1, int *bits )
1331 uint32_t i, sum0, sum1, sum=0, bigv=0, x, y;
1333 /* ESC-table is used */
1334 for(i=start; i<end; i+=2)
1336 x = ix[i];
1337 y = ix[i+1];
1339 if(x > 14) { x = 15; bigv++; }
1340 if(y > 14) { y = 15; bigv++; }
1342 sum += tab1624[x * 16 + y];
1345 sum0 = (sum >> 16) + bigv * ht_big[table0].linbits;
1346 sum1 = (sum & 0xffff) + bigv * ht_big[table1].linbits;
1348 if(sum0 <= sum1)
1350 *bits = sum0;
1351 return table0;
1353 else
1355 *bits = sum1;
1356 return table1;
1360 /*************************************************************************/
1361 /* Function: Calculation of rzero, count1, address3 */
1362 /* (Partitions ix into big values, quadruples and zeros). */
1363 /*************************************************************************/
1364 int calc_runlen( short *ix, side_info_t *si )
1366 int p, i, sum = 0;
1368 for(i=SAMPL2; i-=2; )
1369 if(*(uint32_t*)&ix[i-2]) /* !!!! short *ix; !!!!! */
1370 break;
1372 si->count1 = 0;
1374 for( ; i>3; i-=4)
1376 int v = ix[i-1];
1377 int w = ix[i-2];
1378 int x = ix[i-3];
1379 int y = ix[i-4];
1381 if((v | w | x | y) <= 1)
1383 p = (y<<3) + (x<<2) + (w<<1) + (v);
1385 sum += tab01[p];
1387 si->count1++;
1389 else break;
1392 si->address3 = i;
1394 if((sum >> 16) < (sum & 0xffff))
1396 si->table_select[3] = 0;
1397 return sum >> 16;
1399 else
1401 si->table_select[3] = 1;
1402 return sum & 0xffff;
1407 /*************************************************************************/
1408 /* Function: Quantization of the vector xr ( -> ix) */
1409 /*************************************************************************/
1410 int quantize_int(int *xr, short *ix, side_info_t *si)
1412 unsigned int i, idx, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 };
1414 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4;
1416 /* check for possible 'out of range' values */
1417 if(((si->max_val + 256) >> 8) * s >= (65536 << 8))
1418 return 0;
1420 if(((si->max_val + 256) >> 8) * s < (4096 << 8))
1421 { /* all values fit the table size */
1422 for(i=SAMPL2; i--; )
1423 ix[i] = int2idx[(xr[i] * s + 0x8000) >> 16];
1425 else
1426 { /* check each index wether it fits the table */
1427 for(i=SAMPL2; i--; )
1429 idx = (xr[i] * s + 0x08000) >> 16;
1431 if(idx > 4095) ix[i] = int2idx[(idx + 8) >> 4] << 3;
1432 else ix[i] = int2idx[idx];
1436 return 1;
1439 /*************************************************************************/
1440 /* subdivides the bigvalue region which will use separate Huffman tables */
1441 /*************************************************************************/
1442 void subdivide(side_info_t *si)
1444 int scfb, count0, count1;
1446 if( !si->address3 )
1447 { /* no bigvalue region */
1448 si->region_0_1 = 0;
1449 si->address1 = 0;
1450 si->address2 = 0;
1452 else
1454 /* Calculate scale factor band index */
1455 for(scfb=0; scalefac[scfb] < si->address3; )
1456 scfb++;
1458 count0 = subdv_table[scfb].region0_cnt;
1459 count1 = subdv_table[scfb].region1_cnt;
1461 si->region_0_1 = (count0 << 3) | count1;
1462 si->address1 = scalefac[count0 + 1];
1463 si->address2 = scalefac[count0 + 1 + count1 + 1];
1467 /*******************************************************************/
1468 /* Count the number of bits necessary to code the bigvalues region */
1469 /*******************************************************************/
1470 int bigv_bitcount(short *ix, side_info_t *gi)
1472 int b1=0, b2=0, b3=0;
1474 /* Select huffman code tables for bigvalues regions */
1475 gi->table_select[0] = 0;
1476 gi->table_select[1] = 0;
1477 gi->table_select[2] = 0;
1479 if( gi->address1 > 0 ) /* region0 */
1480 gi->table_select[0] = choose_table(ix, 0 , gi->address1, &b1);
1482 if( gi->address2 > gi->address1 ) /* region1 */
1483 gi->table_select[1] = choose_table(ix, gi->address1, gi->address2, &b2);
1485 if( gi->address3 > gi->address2 ) /* region2 */
1486 gi->table_select[2] = choose_table(ix, gi->address2, gi->address3, &b3);
1488 return b1+b2+b3;
1491 int quantize_and_count_bits(int *xr, short *ix, side_info_t *si)
1493 int bits = 10000;
1495 if(quantize_int(xr, ix, si))
1497 bits = calc_runlen(ix, si); /* rzero,count1,address3 */
1498 subdivide(si); /* bigvalues sfb division */
1499 bits += bigv_bitcount(ix,si); /* bit count */
1502 return bits;
1505 /************************************************************************/
1506 /* The code selects the best quantStep for a particular set of scalefacs*/
1507 /************************************************************************/
1508 int inner_loop(int *xr, int max_bits, side_info_t *si)
1510 int bits;
1512 while((bits=quantize_and_count_bits(xr, enc_data, si)) < max_bits-64)
1514 if(si->quantStep == 0)
1515 break;
1517 if(si->quantStep <= 2)
1518 si->quantStep = 0;
1519 else
1520 si->quantStep -= 2;
1523 while(bits > max_bits)
1525 si->quantStep++;
1526 bits = quantize_and_count_bits(xr, enc_data, si);
1529 return bits;
1532 void iteration_loop(int *xr, side_info_t *si, int gr_cnt)
1534 int remain, tar_bits, max_bits = cfg.mean_bits;
1536 /* distribute reserved bits to remaining granules */
1537 tar_bits = max_bits + (cfg.ResvSize / gr_cnt & ~7);
1538 if(tar_bits > max_bits + max_bits/2)
1539 tar_bits = max_bits + max_bits/2;
1541 si->part2_3_length = inner_loop(xr, tar_bits, si);
1542 si->global_gain = si->quantStep + 142 - si->additStep;
1544 /* unused bits of the reservoir can be used for remaining granules */
1545 cfg.ResvSize += max_bits - si->part2_3_length;
1547 /* end: distribute the reserved bits to one or two granules */
1548 if(gr_cnt == 1)
1550 si->part2_3_length += cfg.ResvSize;
1551 /* mp3 format allows max 12bits for granule length */
1552 if(si->part2_3_length > 4092)
1554 remain = (si->part2_3_length - 4092 + 31) >> 5;
1555 si->part2_3_length -= remain << 5;
1556 si[-1].part2_3_length += remain << 5;
1558 while(remain--)
1559 putbits(~0, 32);
1565 /* returns sum_j=0^31 a[j]*cos(PI*j*(k+1/2)/32), 0<=k<32 */
1566 void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT]) ICODE_ATTR;
1567 void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT])
1569 int k, i, u, v;
1570 short *wp, *x1, *x2;
1572 #ifdef CPU_COLDFIRE
1573 int s0, s1, t0, t1;
1575 for(k=0; k<18; k++, wk+=64, sb0+=SBLIMIT, sb1+=SBLIMIT)
1577 wp = enwindow;
1578 x1 = wk;
1579 x2 = x1 - 124;
1581 for(i=-15; i<0; i++)
1583 asm volatile(
1584 "move.l (-224*4,%[x2]), %%d4\n" /* d4 = x2[-224] */
1585 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1586 "mac.w %%d0u, %%d4u, %%acc0\n"
1587 "mac.w %%d0u, %%d4l, (-160*4,%[x2]), %%d4, %%acc1\n"
1588 "mac.w %%d0l, %%d4u, %%acc0\n"
1589 "mac.w %%d0l, %%d4l, ( -96*4,%[x2]), %%d4, %%acc1\n"
1590 "mac.w %%d1u, %%d4u, %%acc0\n"
1591 "mac.w %%d1u, %%d4l, ( -32*4,%[x2]), %%d4, %%acc1\n"
1592 "mac.w %%d1l, %%d4u, %%acc0\n"
1593 "mac.w %%d1l, %%d4l, ( 32*4,%[x2]), %%d4, %%acc1\n"
1594 "mac.w %%d2u, %%d4u, %%acc0\n"
1595 "mac.w %%d2u, %%d4l, ( 96*4,%[x2]), %%d4, %%acc1\n"
1596 "mac.w %%d2l, %%d4u, %%acc0\n"
1597 "mac.w %%d2l, %%d4l, ( 160*4,%[x2]), %%d4, %%acc1\n"
1598 "mac.w %%d3u, %%d4u, %%acc0\n"
1599 "mac.w %%d3u, %%d4l, ( 224*4,%[x2]), %%d4, %%acc1\n"
1600 "mac.w %%d3l, %%d4u, %%acc0\n"
1601 "mac.w %%d3l, %%d4l, (-256*4,%[x1]), %%d4, %%acc1\n"
1602 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */
1603 "mac.w %%d0u, %%d4u, %%acc0\n"
1604 "mac.w %%d0u, %%d4l, (-192*4,%[x1]), %%d4, %%acc1\n"
1605 "mac.w %%d0l, %%d4u, %%acc0\n"
1606 "mac.w %%d0l, %%d4l, (-128*4,%[x1]), %%d4, %%acc1\n"
1607 "mac.w %%d1u, %%d4u, %%acc0\n"
1608 "mac.w %%d1u, %%d4l, ( -64*4,%[x1]), %%d4, %%acc1\n"
1609 "mac.w %%d1l, %%d4u, %%acc0\n"
1610 "mac.w %%d1l, %%d4l, ( 0*4,%[x1]), %%d4, %%acc1\n"
1611 "mac.w %%d2u, %%d4u, %%acc0\n"
1612 "mac.w %%d2u, %%d4l, ( 64*4,%[x1]), %%d4, %%acc1\n"
1613 "mac.w %%d2l, %%d4u, %%acc0\n"
1614 "mac.w %%d2l, %%d4l, ( 128*4,%[x1]), %%d4, %%acc1\n"
1615 "mac.w %%d3u, %%d4u, %%acc0\n"
1616 "mac.w %%d3u, %%d4l, ( 192*4,%[x1]), %%d4, %%acc1\n"
1617 "mac.w %%d3l, %%d4u, %%acc0\n"
1618 "mac.w %%d3l, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n"
1619 "movclr.l %%acc0, %%d0\n"
1620 "move.l %%d0, %[s0]\n"
1621 "movclr.l %%acc1, %%d0\n"
1622 "move.l %%d0, %[s1]\n"
1624 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1625 "mac.w %%d0u, %%d4u, %%acc0\n"
1626 "mac.w %%d0u, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n"
1627 "mac.w %%d0l, %%d4u, %%acc0\n"
1628 "mac.w %%d0l, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n"
1629 "mac.w %%d1u, %%d4u, %%acc0\n"
1630 "mac.w %%d1u, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n"
1631 "mac.w %%d1l, %%d4u, %%acc0\n"
1632 "mac.w %%d1l, %%d4l, ( -32*4,%[x1]), %%d4, %%acc1\n"
1633 "mac.w %%d2u, %%d4u, %%acc0\n"
1634 "mac.w %%d2u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n"
1635 "mac.w %%d2l, %%d4u, %%acc0\n"
1636 "mac.w %%d2l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n"
1637 "mac.w %%d3u, %%d4u, %%acc0\n"
1638 "mac.w %%d3u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n"
1639 "mac.w %%d3l, %%d4u, %%acc0\n"
1640 "mac.w %%d3l, %%d4l, ( 256*4,%[x2]), %%d4, %%acc1\n"
1641 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */
1642 "mac.w %%d0u, %%d4u, %%acc0\n"
1643 "mac.w %%d0u, %%d4l, ( 192*4,%[x2]), %%d4, %%acc1\n"
1644 "mac.w %%d0l, %%d4u, %%acc0\n"
1645 "mac.w %%d0l, %%d4l, ( 128*4,%[x2]), %%d4, %%acc1\n"
1646 "mac.w %%d1u, %%d4u, %%acc0\n"
1647 "mac.w %%d1u, %%d4l, ( 64*4,%[x2]), %%d4, %%acc1\n"
1648 "mac.w %%d1l, %%d4u, %%acc0\n"
1649 "mac.w %%d1l, %%d4l, ( 0*4,%[x2]), %%d4, %%acc1\n"
1650 "mac.w %%d2u, %%d4u, %%acc0\n"
1651 "mac.w %%d2u, %%d4l, ( -64*4,%[x2]), %%d4, %%acc1\n"
1652 "mac.w %%d2l, %%d4u, %%acc0\n"
1653 "mac.w %%d2l, %%d4l, (-128*4,%[x2]), %%d4, %%acc1\n"
1654 "mac.w %%d3u, %%d4u, %%acc0\n"
1655 "mac.w %%d3u, %%d4l, (-192*4,%[x2]), %%d4, %%acc1\n"
1656 "mac.w %%d3l, %%d4u, %%acc0\n"
1657 "mac.w %%d3l, %%d4l, %%acc1\n"
1658 "movclr.l %%acc0, %%d0\n"
1659 "move.l %%d0, %[t0]\n"
1660 "movclr.l %%acc1, %%d0\n"
1661 "move.l %%d0, %[t1]\n"
1663 : [x1] "+a" (x1), [x2] "+a" (x2), [s0] "+m" (s0), [t0] "+m" (t0),
1664 [s1] "+m" (s1), [t1] "+m" (t1)
1665 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4");
1667 sb0[30+i*2] = shft4(t0) + shft13(s0) * wp[24];
1668 sb0[31+i*2] = shft13(t0) * wp[25] - shft13(s0) * wp[26];
1669 sb1[30+i*2] = shft4(t1) + shft13(s1) * wp[24];
1670 sb1[31+i*2] = shft13(t1) * wp[25] - shft13(s1) * wp[26];
1671 wp += 27;
1672 x1 -= 2;
1673 x2 += 2;
1676 asm volatile(
1677 "move.l ( -32*4,%[x1]), %%d4\n" /* d4 = x1[-32] */
1678 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1680 "mac.w %%d0u, %%d4u, %%acc0\n"
1681 "mac.w %%d0u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n"
1682 "mac.w %%d0l, %%d4u, %%acc0\n"
1683 "mac.w %%d0l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n"
1684 "mac.w %%d1u, %%d4u, %%acc0\n"
1685 "mac.w %%d1u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n"
1686 "mac.w %%d1l, %%d4u, %%acc0\n"
1687 "mac.w %%d1l, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n"
1688 "mac.w %%d2u, %%d4u, %%acc0\n"
1689 "mac.w %%d2u, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n"
1690 "mac.w %%d2l, %%d4u, %%acc0\n"
1691 "mac.w %%d2l, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n"
1692 "mac.w %%d3u, %%d4u, %%acc0\n"
1693 "mac.w %%d3u, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n"
1694 "mac.w %%d3l, %%d4u, %%acc0\n"
1695 "mac.w %%d3l, %%d4l, ( -16*4,%[x1]), %%d4, %%acc1\n"
1696 "movclr.l %%acc0, %%d0\n"
1697 "move.l %%d0, %[s0]\n"
1698 "movclr.l %%acc1, %%d0\n"
1699 "move.l %%d0, %[s1]\n"
1701 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */
1702 "mac.w %%d0u, %%d4u, %%acc0\n"
1703 "mac.w %%d0u, %%d4l, ( -48*4,%[x1]), %%d4, %%acc1\n"
1704 "mac.w %%d1u, %%d4u, %%acc0\n"
1705 "mac.w %%d1u, %%d4l, ( 16*4,%[x1]), %%d4, %%acc1\n"
1706 "mac.w %%d1l, %%d4u, %%acc0\n"
1707 "mac.w %%d1l, %%d4l, ( -80*4,%[x1]), %%d4, %%acc1\n"
1708 "mac.w %%d2u, %%d4u, %%acc0\n"
1709 "mac.w %%d2u, %%d4l, ( 48*4,%[x1]), %%d4, %%acc1\n"
1710 "mac.w %%d2u, %%d4u, %%acc0\n"
1711 "mac.w %%d2u, %%d4l, (-112*4,%[x1]), %%d4, %%acc1\n"
1712 "mac.w %%d3u, %%d4u, %%acc0\n"
1713 "mac.w %%d3u, %%d4l, ( 80*4,%[x1]), %%d4, %%acc1\n"
1714 "mac.w %%d3l, %%d4u, %%acc0\n"
1715 "mac.w %%d3l, %%d4l, (-144*4,%[x1]), %%d4, %%acc1\n"
1716 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */
1717 "mac.w %%d0u, %%d4u, %%acc0\n"
1718 "mac.w %%d0u, %%d4l, ( 112*4,%[x1]), %%d4, %%acc1\n"
1719 "mac.w %%d0u, %%d4u, %%acc0\n"
1720 "mac.w %%d0u, %%d4l, (-176*4,%[x1]), %%d4, %%acc1\n"
1721 "mac.w %%d1u, %%d4u, %%acc0\n"
1722 "mac.w %%d1u, %%d4l, ( 144*4,%[x1]), %%d4, %%acc1\n"
1723 "mac.w %%d1l, %%d4u, %%acc0\n"
1724 "mac.w %%d1l, %%d4l, (-208*4,%[x1]), %%d4, %%acc1\n"
1725 "mac.w %%d2u, %%d4u, %%acc0\n"
1726 "mac.w %%d2u, %%d4l, ( 176*4,%[x1]), %%d4, %%acc1\n"
1727 "mac.w %%d2u, %%d4u, %%acc0\n"
1728 "mac.w %%d2u, %%d4l, (-240*4,%[x1]), %%d4, %%acc1\n"
1729 "mac.w %%d3u, %%d4u, %%acc0\n"
1730 "mac.w %%d3u, %%d4l, ( 208*4,%[x1]), %%d4, %%acc1\n"
1731 "mac.w %%d3l, %%d4u, %%acc0\n"
1732 "mac.w %%d3l, %%d4l, %%acc1\n"
1733 "movclr.l %%acc0, %%d0\n"
1734 "move.l %%d0, %[t0]\n"
1735 "movclr.l %%acc1, %%d0\n"
1736 "move.l %%d0, %[t1]\n"
1738 : [x1] "+a" (x1), [s0] "+m" (s0), [t0] "+m" (t0),
1739 [s1] "+m" (s1), [t1] "+m" (t1)
1740 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4");
1742 u = shft4(s0 - t0);
1743 v = shft4(s0 + t0);
1744 t0 = sb0[14];
1745 s0 = sb0[15] - t0;
1747 sb0[31] = v + t0; /* A0 */
1748 sb0[30] = u + s0; /* A1 */
1749 sb0[15] = u - s0; /* A2 */
1750 sb0[14] = v - t0; /* A3 */
1752 u = shft4(s1 - t1);
1753 v = shft4(s1 + t1);
1754 t1 = sb1[14];
1755 s1 = sb1[15] - t1;
1757 sb1[31] = v + t1; /* A0 */
1758 sb1[30] = u + s1; /* A1 */
1759 sb1[15] = u - s1; /* A2 */
1760 sb1[14] = v - t1; /* A3 */
1762 #else
1763 int ch, s, t, *a;
1765 for(ch=0; ch<cfg.channels; ch++)
1767 a = ch ? sb1 : sb0;
1768 for(k=0; k<18; k++, wk+=64, a+=SBLIMIT)
1770 wp = enwindow;
1771 x1 = wk;
1772 x2 = x1 - 124;
1774 /* x1[-572] .... x1[448] = 1022 */
1775 /* 18*4*16*32 */
1776 for(i=-15; i<0; i++)
1778 s = (int)x2[-224*2] * wp[ 0]; t = (int)x1[ 224*2] * wp[ 0];
1779 s += (int)x2[-160*2] * wp[ 1]; t += (int)x1[ 160*2] * wp[ 1];
1780 s += (int)x2[- 96*2] * wp[ 2]; t += (int)x1[ 96*2] * wp[ 2];
1781 s += (int)x2[- 32*2] * wp[ 3]; t += (int)x1[ 32*2] * wp[ 3];
1782 s += (int)x2[ 32*2] * wp[ 4]; t += (int)x1[- 32*2] * wp[ 4];
1783 s += (int)x2[ 96*2] * wp[ 5]; t += (int)x1[- 96*2] * wp[ 5];
1784 s += (int)x2[ 160*2] * wp[ 6]; t += (int)x1[-160*2] * wp[ 6];
1785 s += (int)x2[ 224*2] * wp[ 7]; t += (int)x1[-224*2] * wp[ 7];
1786 s += (int)x1[-256*2] * wp[ 8]; t += (int)x2[ 256*2] * wp[16];
1787 s += (int)x1[-192*2] * wp[ 9]; t += (int)x2[ 192*2] * wp[17];
1788 s += (int)x1[-128*2] * wp[10]; t += (int)x2[ 128*2] * wp[18];
1789 s += (int)x1[- 64*2] * wp[11]; t += (int)x2[ 64*2] * wp[19];
1790 s += (int)x1[ 0*2] * wp[12]; t += (int)x2[ 0*2] * wp[20];
1791 s += (int)x1[ 64*2] * wp[13]; t += (int)x2[- 64*2] * wp[21];
1792 s += (int)x1[ 128*2] * wp[14]; t += (int)x2[-128*2] * wp[22];
1793 s += (int)x1[ 192*2] * wp[15]; t += (int)x2[-192*2] * wp[23];
1795 a[30+i*2] = shft4(t) + shft13(s) * wp[24];
1796 a[31+i*2] = shft13(t) * wp[25] - shft13(s) * wp[26];
1797 wp += 27;
1798 x1 -= 2;
1799 x2 += 2;
1802 t = (int)x1[- 16*2] * wp[ 8]; s = (int)x1[ -32*2] * wp[0];
1803 t += ((int)x1[- 48*2]-x1[ 16*2]) * wp[10]; s += (int)x1[ -96*2] * wp[1];
1804 t += ((int)x1[- 80*2]+x1[ 48*2]) * wp[12]; s += (int)x1[-160*2] * wp[2];
1805 t += ((int)x1[-112*2]-x1[ 80*2]) * wp[14]; s += (int)x1[-224*2] * wp[3];
1806 t += ((int)x1[-144*2]+x1[112*2]) * wp[16]; s += (int)x1[ 32*2] * wp[4];
1807 t += ((int)x1[-176*2]-x1[144*2]) * wp[18]; s += (int)x1[ 96*2] * wp[5];
1808 t += ((int)x1[-208*2]+x1[176*2]) * wp[20]; s += (int)x1[ 160*2] * wp[6];
1809 t += ((int)x1[-240*2]-x1[208*2]) * wp[22]; s += (int)x1[ 224*2] * wp[7];
1811 u = shft4(s - t);
1812 v = shft4(s + t);
1813 t = a[14];
1814 s = a[15] - t;
1816 a[31] = v + t; /* A0 */
1817 a[30] = u + s; /* A1 */
1818 a[15] = u - s; /* A2 */
1819 a[14] = v - t; /* A3 */
1821 wk -= 18 * 64 - 1; /* rewind wk (to next channel start) */
1823 #endif
1826 void window_subband2(short *x1, int a[SBLIMIT]) ICODE_ATTR;
1827 void window_subband2(short *x1, int a[SBLIMIT])
1829 int xr;
1830 short *wp = enwindow;
1831 short *x2 = x1 - 124;
1833 wp += 27 * 15;
1834 x1 -= 2 * 15;
1835 x2 += 2 * 15;
1837 xr = a[28] - a[0]; a[0] += a[28]; a[28] = shft9(xr) * wp[-2*27+25];
1838 xr = a[29] - a[1]; a[1] += a[29]; a[29] = shft9(xr) * wp[-2*27+25];
1839 xr = a[26] - a[2]; a[2] += a[26]; a[26] = shft9(xr) * wp[-4*27+25];
1840 xr = a[27] - a[3]; a[3] += a[27]; a[27] = shft9(xr) * wp[-4*27+25];
1841 xr = a[24] - a[4]; a[4] += a[24]; a[24] = shft9(xr) * wp[-6*27+25];
1842 xr = a[25] - a[5]; a[5] += a[25]; a[25] = shft9(xr) * wp[-6*27+25];
1843 xr = a[22] - a[6]; a[6] += a[22]; a[22] = shft9(xr) * SQRT ;
1844 xr = a[23] - a[7]; a[7] += a[23]; a[23] = shft9(xr) * SQRT - a[7];
1845 a[ 7] -= a[ 6];
1846 a[22] -= a[ 7];
1847 a[23] -= a[22];
1849 xr = a[ 6]; a[ 6] = a[31] - xr; a[31] = a[31] + xr;
1850 xr = a[ 7]; a[ 7] = a[30] - xr; a[30] = a[30] + xr;
1851 xr = a[22]; a[22] = a[15] - xr; a[15] = a[15] + xr;
1852 xr = a[23]; a[23] = a[14] - xr; a[14] = a[14] + xr;
1854 xr = a[20] - a[ 8]; a[ 8] += a[20]; a[20] = shft9(xr) * wp[-10*27+25];
1855 xr = a[21] - a[ 9]; a[ 9] += a[21]; a[21] = shft9(xr) * wp[-10*27+25];
1856 xr = a[18] - a[10]; a[10] += a[18]; a[18] = shft9(xr) * wp[-12*27+25];
1857 xr = a[19] - a[11]; a[11] += a[19]; a[19] = shft9(xr) * wp[-12*27+25];
1858 xr = a[16] - a[12]; a[12] += a[16]; a[16] = shft9(xr) * wp[-14*27+25];
1859 xr = a[17] - a[13]; a[13] += a[17]; a[17] = shft9(xr) * wp[-14*27+25];
1860 xr =-a[20] + a[24]; a[20] += a[24]; a[24] = shft9(xr) * wp[-12*27+25];
1861 xr =-a[21] + a[25]; a[21] += a[25]; a[25] = shft9(xr) * wp[-12*27+25];
1862 xr = a[ 4] - a[ 8]; a[ 4] += a[ 8]; a[ 8] = shft9(xr) * wp[-12*27+25];
1863 xr = a[ 5] - a[ 9]; a[ 5] += a[ 9]; a[ 9] = shft9(xr) * wp[-12*27+25];
1864 xr = a[ 0] - a[12]; a[ 0] += a[12]; a[12] = shft9(xr) * wp[ -4*27+25];
1865 xr = a[ 1] - a[13]; a[ 1] += a[13]; a[13] = shft9(xr) * wp[ -4*27+25];
1866 xr = a[16] - a[28]; a[16] += a[28]; a[28] = shft9(xr) * wp[ -4*27+25];
1867 xr =-a[17] + a[29]; a[17] += a[29]; a[29] = shft9(xr) * wp[ -4*27+25];
1869 xr = SQRT * shft9(a[ 2] - a[10]); a[ 2] += a[10]; a[10] = xr;
1870 xr = SQRT * shft9(a[ 3] - a[11]); a[ 3] += a[11]; a[11] = xr;
1871 xr = SQRT * shft9(a[26] - a[18]); a[18] += a[26]; a[26] = xr - a[18];
1872 xr = SQRT * shft9(a[27] - a[19]); a[19] += a[27]; a[27] = xr - a[19];
1874 xr = a[ 2]; a[19] -= a[ 3]; a[ 3] -= xr; a[ 2] = a[31] - xr; a[31] += xr;
1875 xr = a[ 3]; a[11] -= a[19]; a[18] -= xr; a[ 3] = a[30] - xr; a[30] += xr;
1876 xr = a[18]; a[27] -= a[11]; a[19] -= xr; a[18] = a[15] - xr; a[15] += xr;
1878 xr = a[19]; a[10] -= xr; a[19] = a[14] - xr; a[14] += xr;
1879 xr = a[10]; a[11] -= xr; a[10] = a[23] - xr; a[23] += xr;
1880 xr = a[11]; a[26] -= xr; a[11] = a[22] - xr; a[22] += xr;
1881 xr = a[26]; a[27] -= xr; a[26] = a[ 7] - xr; a[ 7] += xr;
1883 xr = a[27]; a[27] = a[6] - xr; a[6] += xr;
1885 xr = SQRT * shft9(a[ 0] - a[ 4]); a[ 0] += a[ 4]; a[ 4] = xr;
1886 xr = SQRT * shft9(a[ 1] - a[ 5]); a[ 1] += a[ 5]; a[ 5] = xr;
1887 xr = SQRT * shft9(a[16] - a[20]); a[16] += a[20]; a[20] = xr;
1888 xr = SQRT * shft9(a[17] - a[21]); a[17] += a[21]; a[21] = xr;
1889 xr =-SQRT * shft9(a[ 8] - a[12]); a[ 8] += a[12]; a[12] = xr - a[ 8];
1890 xr =-SQRT * shft9(a[ 9] - a[13]); a[ 9] += a[13]; a[13] = xr - a[ 9];
1891 xr =-SQRT * shft9(a[25] - a[29]); a[25] += a[29]; a[29] = xr - a[25];
1892 xr =-SQRT * shft9(a[24] + a[28]); a[24] -= a[28]; a[28] = xr - a[24];
1894 xr = a[24] - a[16]; a[24] = xr;
1895 xr = a[20] - xr; a[20] = xr;
1896 xr = a[28] - xr; a[28] = xr;
1898 xr = a[25] - a[17]; a[25] = xr;
1899 xr = a[21] - xr; a[21] = xr;
1900 xr = a[29] - xr; a[29] = xr;
1902 xr = a[17] - a[1]; a[17] = xr;
1903 xr = a[ 9] - xr; a[ 9] = xr;
1904 xr = a[25] - xr; a[25] = xr;
1905 xr = a[ 5] - xr; a[ 5] = xr;
1906 xr = a[21] - xr; a[21] = xr;
1907 xr = a[13] - xr; a[13] = xr;
1908 xr = a[29] - xr; a[29] = xr;
1910 xr = a[ 1] - a[0]; a[ 1] = xr;
1911 xr = a[16] - xr; a[16] = xr;
1912 xr = a[17] - xr; a[17] = xr;
1913 xr = a[ 8] - xr; a[ 8] = xr;
1914 xr = a[ 9] - xr; a[ 9] = xr;
1915 xr = a[24] - xr; a[24] = xr;
1916 xr = a[25] - xr; a[25] = xr;
1917 xr = a[ 4] - xr; a[ 4] = xr;
1918 xr = a[ 5] - xr; a[ 5] = xr;
1919 xr = a[20] - xr; a[20] = xr;
1920 xr = a[21] - xr; a[21] = xr;
1921 xr = a[12] - xr; a[12] = xr;
1922 xr = a[13] - xr; a[13] = xr;
1923 xr = a[28] - xr; a[28] = xr;
1924 xr = a[29] - xr; a[29] = xr;
1926 xr = a[ 0]; a[ 0] += a[31]; a[31] -= xr;
1927 xr = a[ 1]; a[ 1] += a[30]; a[30] -= xr;
1928 xr = a[16]; a[16] += a[15]; a[15] -= xr;
1929 xr = a[17]; a[17] += a[14]; a[14] -= xr;
1930 xr = a[ 8]; a[ 8] += a[23]; a[23] -= xr;
1931 xr = a[ 9]; a[ 9] += a[22]; a[22] -= xr;
1932 xr = a[24]; a[24] += a[ 7]; a[ 7] -= xr;
1933 xr = a[25]; a[25] += a[ 6]; a[ 6] -= xr;
1934 xr = a[ 4]; a[ 4] += a[27]; a[27] -= xr;
1935 xr = a[ 5]; a[ 5] += a[26]; a[26] -= xr;
1936 xr = a[20]; a[20] += a[11]; a[11] -= xr;
1937 xr = a[21]; a[21] += a[10]; a[10] -= xr;
1938 xr = a[12]; a[12] += a[19]; a[19] -= xr;
1939 xr = a[13]; a[13] += a[18]; a[18] -= xr;
1940 xr = a[28]; a[28] += a[ 3]; a[ 3] -= xr;
1941 xr = a[29]; a[29] += a[ 2]; a[ 2] -= xr;
1944 void mdct_long(int *out, int *in) ICODE_ATTR;
1945 void mdct_long(int *out, int *in)
1947 int ct,st;
1948 int tc1, tc2, tc3, tc4, ts5, ts6, ts7, ts8;
1949 int ts1, ts2, ts3, ts4, tc5, tc6, tc7, tc8;
1951 /* 1,2, 5,6, 9,10, 13,14, 17 */
1952 tc1 = in[17] - in[ 9];
1953 tc3 = in[15] - in[11];
1954 tc4 = in[14] - in[12];
1955 ts5 = in[ 0] + in[ 8];
1956 ts6 = in[ 1] + in[ 7];
1957 ts7 = in[ 2] + in[ 6];
1958 ts8 = in[ 3] + in[ 5];
1960 out[17] = (ts5 + ts7 - ts8) * cx[8] - (ts6 - in[4]) * cx[8];
1961 st = (ts5 + ts7 - ts8) * cx[7] + (ts6 - in[4]) * cx[8];
1962 ct = (tc1 - tc3 - tc4) * cx[6];
1963 out[5] = ct + st;
1964 out[6] = ct - st;
1966 tc2 = (in[16] - in[10]) * cx[6];
1967 ts6 = ts6 * cx[7] + in[4] * cx[8];
1969 ct = tc1 * cx[0] + tc2 + tc3 * cx[1] + tc4 * cx[2];
1970 st = -ts5 * cx[4] + ts6 - ts7 * cx[5] + ts8 * cx[3];
1971 out[1] = ct + st;
1972 out[2] = ct - st;
1974 ct = tc1 * cx[1] - tc2 - tc3 * cx[2] + tc4 * cx[0];
1975 st = -ts5 * cx[5] + ts6 - ts7 * cx[3] + ts8 * cx[4];
1976 out[ 9] = ct + st;
1977 out[10] = ct - st;
1979 ct = tc1 * cx[2] - tc2 + tc3 * cx[0] - tc4 * cx[1];
1980 st = ts5 * cx[3] - ts6 + ts7 * cx[4] - ts8 * cx[5];
1981 out[13] = ct + st;
1982 out[14] = ct - st;
1984 ts1 = in[ 8] - in[ 0];
1985 ts3 = in[ 6] - in[ 2];
1986 ts4 = in[ 5] - in[ 3];
1987 tc5 = in[17] + in[ 9];
1988 tc6 = in[16] + in[10];
1989 tc7 = in[15] + in[11];
1990 tc8 = in[14] + in[12];
1992 out[0] = (tc5 + tc7 + tc8) * cx[8] + (tc6 + in[13]) * cx[8];
1993 ct = (tc5 + tc7 + tc8) * cx[7] - (tc6 + in[13]) * cx[8];
1994 st = (ts1 - ts3 + ts4) * cx[6];
1995 out[11] = ct + st;
1996 out[12] = ct - st;
1998 ts2 = (in[7] - in[1]) * cx[6];
1999 tc6 = in[13] * cx[8] - tc6 * cx[7];
2001 ct = tc5 * cx[3] - tc6 + tc7 * cx[4] + tc8 * cx[5];
2002 st = ts1 * cx[2] + ts2 + ts3 * cx[0] + ts4 * cx[1];
2003 out[3] = ct + st;
2004 out[4] = ct - st;
2006 ct =-tc5 * cx[5] + tc6 - tc7 * cx[3] - tc8 * cx[4];
2007 st = ts1 * cx[1] + ts2 - ts3 * cx[2] - ts4 * cx[0];
2008 out[7] = ct + st;
2009 out[8] = ct - st;
2011 ct =-tc5 * cx[4] + tc6 - tc7 * cx[5] - tc8 * cx[3];
2012 st = ts1 * cx[0] - ts2 + ts3 * cx[1] - ts4 * cx[2];
2013 out[15] = ct + st;
2014 out[16] = ct - st;
2017 static int find_bitrate_index(int type, int bitrate)
2019 int i;
2021 for(i=0;i<14;i++)
2022 if(bitrate == bitr_index[type][i])
2023 break;
2025 return i;
2028 static int find_samplerate_index(long freq, int *mp3_type)
2030 int mpg, rate;
2032 /* set default values: MPEG1 at 44100/s */
2033 *mp3_type = 1;
2035 for(mpg=0; mpg<2; mpg++)
2036 for(rate=0; rate<3; rate++)
2037 if(freq == sampr_index[mpg][rate])
2038 { *mp3_type = mpg; return rate; }
2040 return 0;
2043 void init_mp3_encoder_engine(bool stereo, int bitrate, int sample_rate)
2045 uint32_t avg_byte_per_frame;
2047 #ifdef ROCKBOX_LITTLE_ENDIAN
2048 cfg.byte_order = order_littleEndian;
2049 #else
2050 cfg.byte_order = order_bigEndian;
2051 #endif
2053 if(bitrate < 96 && stereo && sample_rate >= 32000)
2054 { /* use MPEG2 format */
2055 sample_rate >>= 1;
2056 cfg.resample = 1;
2057 cfg.granules = 1;
2059 else
2060 { /* use MPEG1 format */
2061 cfg.resample = 0;
2062 cfg.granules = 2;
2065 cfg.samplerate = sample_rate;
2066 cfg.channels = stereo ? 2 : 1;
2067 cfg.mpg.mode = stereo ? 0 : 3; /* 0=stereo, 3=mono */
2068 cfg.mpg.bitrate = stereo ? bitrate : bitrate > 160 ? 160 : bitrate;
2069 cfg.mpg.smpl_id = find_samplerate_index(cfg.samplerate, &cfg.mpg.type);
2070 cfg.mpg.bitr_id = find_bitrate_index(cfg.mpg.type, cfg.mpg.bitrate);
2071 cfg.mpg.num_bands = num_bands[stereo ? cfg.mpg.type : 2][cfg.mpg.bitr_id];
2073 memcpy(scalefac, sfBand[cfg.mpg.smpl_id + 3*cfg.mpg.type], sizeof(scalefac));
2074 memset(mfbuf , 0 , sizeof(mfbuf ));
2075 memset(mdct_freq , 0 , sizeof(mdct_freq ));
2076 memset(enc_data , 0 , sizeof(enc_data ));
2077 memset(sb_data , 0 , sizeof(sb_data ));
2078 memset(&CodedData, 0 , sizeof(CodedData ));
2079 memcpy(ca , ca_const , sizeof(ca ));
2080 memcpy(cs , cs_const , sizeof(cs ));
2081 memcpy(cx , cx_const , sizeof(cx ));
2082 memcpy(win , win_const , sizeof(win ));
2083 memcpy(enwindow , enwindow_const , sizeof(enwindow ));
2084 memcpy(int2idx , int2idx_const , sizeof(int2idx ));
2085 memcpy(ht_count , ht_count_const , sizeof(ht_count ));
2086 memcpy( tab01 , tab01_const , sizeof(tab01 ));
2087 memcpy( tab23 , tab23_const , sizeof(tab23 ));
2088 memcpy( tab56 , tab56_const , sizeof(tab56 ));
2089 memcpy( tab1315 , tab1315_const , sizeof(tab1315 ));
2090 memcpy( tab1624 , tab1624_const , sizeof(tab1624 ));
2091 memcpy( tab789 , tab789_const , sizeof(tab789 ));
2092 memcpy( tabABC , tabABC_const , sizeof(tabABC ));
2093 memcpy( t1HB , t1HB_const , sizeof(t1HB ));
2094 memcpy( t2HB , t2HB_const , sizeof(t2HB ));
2095 memcpy( t3HB , t3HB_const , sizeof(t3HB ));
2096 memcpy( t5HB , t5HB_const , sizeof(t5HB ));
2097 memcpy( t6HB , t6HB_const , sizeof(t6HB ));
2098 memcpy( t7HB , t7HB_const , sizeof(t7HB ));
2099 memcpy( t8HB , t8HB_const , sizeof(t8HB ));
2100 memcpy( t9HB , t9HB_const , sizeof(t9HB ));
2101 memcpy(t10HB , t10HB_const , sizeof(t10HB ));
2102 memcpy(t11HB , t11HB_const , sizeof(t11HB ));
2103 memcpy(t12HB , t12HB_const , sizeof(t12HB ));
2104 memcpy(t13HB , t13HB_const , sizeof(t13HB ));
2105 memcpy(t15HB , t15HB_const , sizeof(t15HB ));
2106 memcpy(t16HB , t16HB_const , sizeof(t16HB ));
2107 memcpy(t24HB , t24HB_const , sizeof(t24HB ));
2108 memcpy( t1l , t1l_const , sizeof(t1l ));
2109 memcpy( t2l , t2l_const , sizeof(t2l ));
2110 memcpy( t3l , t3l_const , sizeof(t3l ));
2111 memcpy( t5l , t5l_const , sizeof(t5l ));
2112 memcpy( t6l , t6l_const , sizeof(t6l ));
2113 memcpy( t7l , t7l_const , sizeof(t7l ));
2114 memcpy( t8l , t8l_const , sizeof(t8l ));
2115 memcpy( t9l , t9l_const , sizeof(t9l ));
2116 memcpy(t10l , t10l_const , sizeof(t10l ));
2117 memcpy(t11l , t11l_const , sizeof(t11l ));
2118 memcpy(t12l , t12l_const , sizeof(t12l ));
2119 memcpy(t13l , t13l_const , sizeof(t13l ));
2120 memcpy(t15l , t15l_const , sizeof(t15l ));
2121 memcpy(t16l , t16l_const , sizeof(t16l ));
2122 memcpy(t24l , t24l_const , sizeof(t24l ));
2123 memcpy(ht , ht_const , sizeof(ht ));
2125 ht[ 0].table = NULL; ht[ 0].hlen = NULL; /* Apparently not used */
2126 ht[ 1].table = t1HB; ht[ 1].hlen = t1l;
2127 ht[ 2].table = t2HB; ht[ 2].hlen = t2l;
2128 ht[ 3].table = t3HB; ht[ 3].hlen = t3l;
2129 ht[ 4].table = NULL; ht[ 4].hlen = NULL; /* Apparently not used */
2130 ht[ 5].table = t5HB; ht[ 5].hlen = t5l;
2131 ht[ 6].table = t6HB; ht[ 6].hlen = t6l;
2132 ht[ 7].table = t7HB; ht[ 7].hlen = t7l;
2133 ht[ 8].table = t8HB; ht[ 8].hlen = t8l;
2134 ht[ 9].table = t9HB; ht[ 9].hlen = t9l;
2135 ht[10].table = t10HB; ht[10].hlen = t10l;
2136 ht[11].table = t11HB; ht[11].hlen = t11l;
2137 ht[12].table = t12HB; ht[12].hlen = t12l;
2138 ht[13].table = t13HB; ht[13].hlen = t13l;
2139 ht[14].table = NULL; ht[14].hlen = NULL; /* Apparently not used */
2140 ht[15].table = t15HB; ht[15].hlen = t15l;
2142 /* Figure average number of 'bytes' per frame */
2143 avg_byte_per_frame = SAMPL2 * 16000 * cfg.mpg.bitrate / (2 - cfg.mpg.type);
2144 avg_byte_per_frame = avg_byte_per_frame / cfg.samplerate;
2145 cfg.byte_per_frame = avg_byte_per_frame / 64;
2146 cfg.frac_per_frame = avg_byte_per_frame & 63;
2147 cfg.slot_lag = 0;
2148 cfg.sideinfo_len = 32 + (cfg.mpg.type ? (cfg.channels == 1 ? 136 : 256)
2149 : (cfg.channels == 1 ? 72 : 136));
2152 void set_scale_facs(int *mdct_freq)
2154 unsigned int i, is, ie, k, s;
2155 int max_freq_val, avrg_freq_val;
2157 /* calc average of first 256 frequency values */
2158 for(avrg_freq_val=i=0; i<256; i++)
2159 avrg_freq_val += mdct_freq[i];
2160 avrg_freq_val >>= 8;
2162 /* if max of current band is smaller than average, increase precision */
2163 /* last band keeps untouched (not scaled) */
2164 for(is=k=0; is<scalefac[21]; k++)
2166 max_freq_val = 0;
2168 for(i=is, ie=scalefac[k+1]; i<ie; i++)
2169 if(max_freq_val < mdct_freq[i])
2170 max_freq_val = mdct_freq[i];
2172 for(s=0; s<3; s++)
2173 if((max_freq_val<<s) > avrg_freq_val)
2174 break;
2176 band_scale_f[k] = (unsigned char)s;
2178 for(i=is; s && i<ie; i++)
2179 mdct_freq[i] <<= s;
2181 is = ie;
2185 void compress(void)
2187 int i, gr, gr_cnt;
2188 char stg[20];
2189 uint32_t max;
2191 while(1)
2193 if((frames & 7) == 0)
2194 { rb->lcd_clear_display();
2195 rb->snprintf(stg, 20, "Frame %d / %d", frames, wav_size/SAMPL2/8);
2196 rb->lcd_putsxy(4, 20, stg);
2197 rb->lcd_update();
2199 /* encode one mp3 frame in this loop */
2200 memset(CodedData.bbuf, 0, sizeof(CodedData.bbuf));
2202 if((cfg.slot_lag += cfg.frac_per_frame) >= 64)
2203 { /* Padding for this frame */
2204 cfg.slot_lag -= 64;
2205 cfg.mpg.padding = 1;
2207 else
2208 cfg.mpg.padding = 0;
2210 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding
2211 - cfg.sideinfo_len) / cfg.granules / cfg.channels
2212 - 42; // reserved for scale_facs
2214 /* shift out old samples */
2215 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512);
2217 /* read new samples to iram for further processing */
2218 if(read_samples((uint32_t*)(mfbuf + 2*512), SAMP_PER_FRAME) == 0)
2219 break;
2221 /* swap bytes if neccessary */
2222 if(cfg.byte_order == order_bigEndian)
2223 for(i=0; i<SAMP_PER_FRAME; i++)
2225 uint32_t t = ((uint32_t*)mfbuf)[512 + i];
2226 t = ((t >> 8) & 0xff00ff) | ((t << 8) & 0xff00ff00);
2227 ((uint32_t*)mfbuf)[512 + i] = t;
2230 if(cfg.resample) /* downsample to half of original */
2231 for(i=2*512; i<2*512+2*SAMP_PER_FRAME; i+=4)
2233 mfbuf[i/2+512] = (short)(((int)mfbuf[i+0] + mfbuf[i+2]) >> 1);
2234 mfbuf[i/2+513] = (short)(((int)mfbuf[i+1] + mfbuf[i+3]) >> 1);
2237 if(cfg.channels == 1) /* mix left and right channels to mono */
2238 for(i=2*512; i<2*512+2*SAMP_PER_FRAME; i+=2)
2239 mfbuf[i] = mfbuf[i+1] = (short)(((int)mfbuf[i] + mfbuf[i+1]) >> 1);
2241 cfg.ResvSize = 0;
2242 gr_cnt = cfg.granules * cfg.channels;
2243 CodedData.bitpos = cfg.sideinfo_len; /* leave space for mp3 header */
2245 for(gr=0; gr<cfg.granules; gr++)
2247 short *wk = mfbuf + 2*286 + gr*1152;
2248 int ch;
2250 /* 16bit packed wav data can be windowed efficiently on coldfire */
2251 window_subband1(wk, sb_data[0][1-gr][0], sb_data[1][1-gr][0]);
2253 for(ch=0; ch<cfg.channels; ch++)
2255 int ii, k, shift;
2257 wk = mfbuf + 2*286 + gr*1152 + ch;
2259 /* 36864=4*18*16*32 */
2260 for(k=0; k<18; k++, wk+=64)
2262 window_subband2(wk, sb_data[ch][1-gr][k]);
2263 /* Compensate for inversion in the analysis filter */
2264 if(k & 1)
2266 int band;
2267 for(band=1; band<32; band+=2)
2268 sb_data[ch][1-gr][k][band] *= -1;
2272 /* Perform imdct of 18 previous + 18 current subband samples */
2273 /* for integer precision do this loop again (if neccessary) */
2274 shift = 14 - (cfg.cod_info[gr][ch].additStep >> 2);
2275 for(k=1,ii=0; ii<3 && k; ii++)
2277 int *mdct = mdct_freq;
2278 int band;
2280 cfg.cod_info[gr][ch].additStep = 4 * (14 - shift);
2281 for(band=0; band<cfg.mpg.num_bands; band++, mdct+=18)
2283 int *band0 = sb_data[ch][ gr][0] + order[band];
2284 int *band1 = sb_data[ch][1-gr][0] + order[band];
2285 int work[18];
2287 /* 9216=4*32*9*8 */
2288 for(k=-9; k<0; k++)
2290 int a = shft_n(band1[(k+9)*32], shift);
2291 int b = shft_n(band1[(8-k)*32], shift);
2292 int c = shft_n(band0[(k+9)*32], shift);
2293 int d = shft_n(band0[(8-k)*32], shift);
2295 work[k+ 9] = shft16(a * win[k+ 9][0] +
2296 b * win[k+ 9][1] +
2297 c * win[k+ 9][2] +
2298 d * win[k+ 9][3]);
2300 work[k+18] = shft16(c * win[k+18][0] +
2301 d * win[k+18][1] +
2302 a * win[k+18][2] +
2303 b * win[k+18][3]);
2306 /* 7200=4*18*100 */
2307 mdct_long(mdct, work);
2309 /* Perform aliasing reduction butterfly */
2310 if(band != 0)
2312 for(k=7; k>=0; --k)
2314 int bu, bd;
2315 bu = shft15(mdct[k]) * ca[k] +
2316 shft15(mdct[-1-k]) * cs[k];
2317 bd = shft15(mdct[k]) * cs[k] -
2318 shft15(mdct[-1-k]) * ca[k];
2319 mdct[-1-k] = bu;
2320 mdct[ k ] = bd;
2325 max = 0;
2326 for(k=0; k<576; k++)
2328 if(mdct_freq[k] < 0)
2330 mdct_sign[k] = 1; /* negative */
2331 mdct_freq[k] = shft13(-mdct_freq[k]);
2333 else
2335 mdct_sign[k] = 0; /* positive */
2336 mdct_freq[k] = shft13(mdct_freq[k]);
2339 if(max < (uint32_t)mdct_freq[k])
2340 max = (uint32_t)mdct_freq[k];
2343 cfg.cod_info[gr][ch].max_val = max;
2345 /* calc new shift for higher integer precision */
2346 for(k=0; max<(uint32_t)(0x7800>>k); k++) shift--;
2347 for( ; (max>>k)>=(uint32_t)0x10000; k++) shift++;
2348 if(shift < 0) shift = 0;
2351 cfg.cod_info[gr][ch].quantStep +=
2352 cfg.cod_info[gr][ch].additStep;
2354 set_scale_facs(mdct_freq);
2356 /* bit and noise allocation */
2357 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch],
2358 gr_cnt--);
2359 /* write the frame to the bitstream */
2360 Huffmancodebits(enc_data, mdct_sign,
2361 &cfg.cod_info[gr][ch]);
2363 cfg.cod_info[gr][ch].quantStep -=
2364 cfg.cod_info[gr][ch].additStep;
2366 if(cfg.granules == 1)
2368 memcpy(sb_data[ch][0], sb_data[ch][1],
2369 sizeof(sb_data[ch][0]));
2374 enc_size = (CodedData.bitpos + 7) >> 3;
2375 /* finish this chunk by adding sideinfo header data */
2376 CodedData.bitpos = 0;
2377 encodeSideInfo( cfg.cod_info );
2379 if(cfg.byte_order != order_bigEndian)
2380 for(i=0; i<(enc_size+3)/4; i++)
2381 CodedData.bbuf[i] = myswap32(CodedData.bbuf[i]);
2383 if(enc_chunk + enc_size > 65536)
2385 /* copy iram mp3 buffer to sdram/file */
2386 rb->write(mp3file, enc_buffer, enc_chunk & ~3);
2387 memcpy(enc_buffer, enc_buffer + (enc_chunk >> 2), enc_chunk & 3);
2388 enc_chunk &= 3;
2391 memcpy((char*)enc_buffer + enc_chunk, CodedData.bbuf, enc_size);
2392 enc_chunk += enc_size;
2393 frames++;
2395 /* write last chunks to disk */
2396 rb->write(mp3file, enc_buffer, enc_chunk);
2400 int num_file;
2401 char mp3_name[80];
2403 void get_mp3_filename(const char *wav_name)
2405 int slen = rb->strlen(wav_name);
2406 rb->strncpy(mp3_name, wav_name, 79);
2407 rb->strncpy(mp3_name + slen - 4, ".mp3", 5);
2410 #if CONFIG_KEYPAD == IRIVER_H100_PAD || CONFIG_KEYPAD == IRIVER_H300_PAD
2411 #define MP3ENC_PREV BUTTON_UP
2412 #define MP3ENC_NEXT BUTTON_DOWN
2413 #define MP3ENC_DONE BUTTON_OFF
2414 #define MP3ENC_SELECT BUTTON_SELECT
2415 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
2416 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
2417 #define MP3ENC_PREV BUTTON_SCROLL_BACK
2418 #define MP3ENC_NEXT BUTTON_SCROLL_FWD
2419 #define MP3ENC_DONE BUTTON_MENU
2420 #define MP3ENC_SELECT BUTTON_SELECT
2421 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
2422 #define MP3ENC_PREV BUTTON_UP
2423 #define MP3ENC_NEXT BUTTON_DOWN
2424 #define MP3ENC_DONE BUTTON_POWER
2425 #define MP3ENC_SELECT BUTTON_SELECT
2426 #elif CONFIG_KEYPAD == GIGABEAT_PAD
2427 #define MP3ENC_PREV BUTTON_UP
2428 #define MP3ENC_NEXT BUTTON_DOWN
2429 #define MP3ENC_DONE BUTTON_POWER
2430 #define MP3ENC_SELECT BUTTON_SELECT
2431 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
2432 (CONFIG_KEYPAD == SANSA_C200_PAD)
2433 #define MP3ENC_PREV BUTTON_UP
2434 #define MP3ENC_NEXT BUTTON_DOWN
2435 #define MP3ENC_DONE BUTTON_POWER
2436 #define MP3ENC_SELECT BUTTON_SELECT
2437 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
2438 #define MP3ENC_PREV BUTTON_SCROLL_UP
2439 #define MP3ENC_NEXT BUTTON_SCROLL_DOWN
2440 #define MP3ENC_DONE BUTTON_POWER
2441 #define MP3ENC_SELECT BUTTON_PLAY
2442 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
2443 #define MP3ENC_PREV BUTTON_UP
2444 #define MP3ENC_NEXT BUTTON_DOWN
2445 #define MP3ENC_DONE BUTTON_BACK
2446 #define MP3ENC_SELECT BUTTON_SELECT
2447 #elif CONFIG_KEYPAD == MROBE100_PAD
2448 #define MP3ENC_PREV BUTTON_UP
2449 #define MP3ENC_NEXT BUTTON_DOWN
2450 #define MP3ENC_DONE BUTTON_POWER
2451 #define MP3ENC_SELECT BUTTON_SELECT
2452 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
2453 #define MP3ENC_PREV BUTTON_RC_VOL_UP
2454 #define MP3ENC_NEXT BUTTON_RC_VOL_DOWN
2455 #define MP3ENC_DONE BUTTON_RC_REC
2456 #define MP3ENC_SELECT BUTTON_RC_FF
2457 #elif CONFIG_KEYPAD == COWOND2_PAD
2458 #define MP3ENC_DONE BUTTON_POWER
2459 #else
2460 #error No keymap defined!
2461 #endif
2463 #ifdef HAVE_TOUCHPAD
2464 #ifndef MP3ENC_PREV
2465 #define MP3ENC_PREV BUTTON_MIDLEFT
2466 #endif
2467 #ifndef MP3ENC_NEXT
2468 #define MP3ENC_NEXT BUTTON_MIDRIGHT
2469 #endif
2470 #ifndef MP3ENC_DONE
2471 #define MP3ENC_DONE BUTTON_TOPLEFT
2472 #endif
2473 #ifndef MP3ENC_SELECT
2474 #define MP3ENC_SELECT BUTTON_CENTER
2475 #endif
2476 #endif
2478 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
2480 int rat, srat, nrat; /* for rate selection */
2481 int cont = 1, butt;
2482 long tim = 0;
2483 char stg[40];
2484 static const char* bstrg[] = {
2485 "64", "80", "96", "112", "128", "160", "192", "224", "256", "320"
2487 static const int brate[] = {
2488 64, 80, 96, 112, 128, 160, 192, 224, 256, 320
2491 if (parameter == NULL)
2492 return PLUGIN_ERROR;
2494 PLUGIN_IRAM_INIT(api)
2496 #ifdef CPU_COLDFIRE
2497 coldfire_set_macsr(0); /* integer mode */
2498 #endif
2500 rb = api;
2501 rb->lcd_setfont(FONT_SYSFIXED);
2503 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
2504 rb->cpu_boost(true);
2505 #endif
2506 rb->button_clear_queue();
2508 nrat = 9;
2509 srat = 4; /* set 128kBit as default */
2511 while(cont && (butt = rb->button_get_w_tmo(HZ/10)) != MP3ENC_SELECT)
2513 switch(butt)
2515 case MP3ENC_DONE: cont = 0; break;
2516 case MP3ENC_PREV|BUTTON_REPEAT:
2517 case MP3ENC_PREV: if(srat > 0 ) srat--; break;
2518 case MP3ENC_NEXT|BUTTON_REPEAT:
2519 case MP3ENC_NEXT: if(srat < nrat) srat++; break;
2522 rb->lcd_clear_display();
2523 rb->lcd_putsxy(2, 2, "-- Select Bitrate --");
2525 for(rat=0; rat<=nrat; rat++)
2526 rb->lcd_putsxy(2, 12 + rat*8, bstrg[rat]);
2527 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2528 rb->lcd_fillrect(0, 12 + srat*8, 127, 8);
2529 rb->lcd_set_drawmode(DRMODE_SOLID);
2530 rb->lcd_update();
2533 wav_filename = parameter;
2535 if(cont)
2537 if(wave_open() == 0)
2539 init_mp3_encoder_engine(true, brate[srat], cfg.samplerate);
2540 get_mp3_filename(wav_filename);
2541 mp3file = rb->open(mp3_name , O_WRONLY|O_CREAT|O_TRUNC);
2542 frames = 0;
2544 tim = *rb->current_tick;
2545 compress();
2546 tim = *rb->current_tick - tim;
2548 rb->close(wavfile);
2549 rb->close(mp3file);
2551 else
2553 rb->close(wavfile);
2554 rb->snprintf(stg, 20, "WaveOpen failed %d", wave_open());
2555 rb->lcd_putsxy(0, 20, stg);
2556 rb->lcd_update();
2557 rb->sleep(5*HZ);
2560 rb->lcd_clear_display();
2561 rb->snprintf(stg, 30, " Conversion: %ld.%02lds ", tim/100, tim%100);
2562 rb->lcd_putsxy(0, 30, stg);
2563 tim = frames * SAMP_PER_FRAME * 100 / 44100; /* unit=.01s */
2564 rb->snprintf(stg, 30, " WAV-Length: %ld.%02lds ", tim/100, tim%100);
2565 rb->lcd_putsxy(0, 20, stg);
2566 rb->lcd_update();
2567 rb->sleep(5*HZ);
2570 rb->lcd_setfont(FONT_UI);
2571 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
2572 rb->cpu_boost(false);
2573 #endif
2574 return PLUGIN_OK;