Hide symbols by default on 64 bit sim buildsto avoid clashing, fixes crashing on...
[kugel-rb.git] / apps / plugins / mp3_encoder.c
blob453c755f10c9c3f53fd71161a350adee1d0d8363
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 "plugin.h"
16 PLUGIN_HEADER
17 PLUGIN_IRAM_DECLARE
19 static struct plugin_api* rb;
21 MEM_FUNCTION_WRAPPERS(rb);
23 #define SAMP_PER_FRAME 1152
24 #define SAMPL2 576
25 #define SBLIMIT 32
26 #define HTN 16
27 #define memcpy rb->memcpy
28 #define memset rb->memset
29 #define putlong(c, s) if(s+sz <= 32) { cc = (cc << s) | c; sz+= s; } \
30 else { putbits(cc, sz); cc = c; sz = s; }
32 enum e_byte_order { order_unknown, order_bigEndian, order_littleEndian };
34 typedef unsigned long uint32;
35 typedef unsigned short uint16;
36 typedef unsigned char uint8;
39 typedef struct {
40 int type; /* 0=(22.05,24,16kHz) 1=(44.1,48,32kHz) */
41 int mode; /* 0=stereo, 1=jstereo, 2=dual, 3=mono */
42 int bitrate;
43 int padding;
44 int num_bands;
45 long bitr_id;
46 int smpl_id;
47 } mpeg_t;
49 /* Side information */
50 typedef struct {
51 uint32 part2_3_length;
52 int count1; /* number of 0-1-quadruples */
53 uint32 global_gain;
54 uint32 table_select[4];
55 uint32 region_0_1;
56 uint32 address1;
57 uint32 address2;
58 uint32 address3;
59 long quantStep;
60 long additStep;
61 long max_val;
62 } side_info_t;
64 typedef struct {
65 enum e_byte_order byte_order;
66 side_info_t cod_info[2][2];
67 mpeg_t mpg;
68 long frac_per_frame;
69 long byte_per_frame;
70 long slot_lag;
71 int sideinfo_len;
72 int mean_bits;
73 int ResvSize;
74 int channels;
75 int granules;
76 int resample;
77 long samplerate;
78 } config_t;
80 typedef struct {
81 int bitpos; /* current bitpos for writing */
82 uint32 bbuf[263];
83 } BF_Data;
85 struct huffcodetab {
86 int len; /* max. index */
87 const uint8 *table; /* pointer to array[len][len] */
88 const uint8 *hlen; /* pointer to array[len][len] */
91 struct huffcodebig {
92 int len; /* max. index */
93 int linbits; /* number of linbits */
94 int linmax; /* max number stored in linbits */
97 #define shft4(x) ((x + 8) >> 4)
98 #define shft9(x) ((x + 256) >> 9)
99 #define shft13(x) ((x + 4096) >> 13)
100 #define shft15(x) ((x + 16384) >> 15)
101 #define shft16(x) ((x + 32768) >> 16)
102 #define shft_n(x,n) ((x) >> n)
103 #define SQRT 724 /* sqrt(2) * 512 */
105 short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */
106 int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */
107 int mdct_freq [SAMPL2] IBSS_ATTR; /* 9216 Bytes */
108 short enc_data [SAMPL2] IBSS_ATTR; /* 4608 Bytes */
109 uint32 scalefac [23] IBSS_ATTR; /* 92 Bytes */
110 BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */
111 int ca [8] IBSS_ATTR; /* 32 Bytes */
112 int cs [8] IBSS_ATTR; /* 32 Bytes */
113 int cx [9] IBSS_ATTR; /* 36 Bytes */
114 int win [18][4] IBSS_ATTR; /* 288 Bytes */
115 short enwindow [15*27+24] IBSS_ATTR; /* 862 Bytes */
116 short int2idx [4096] IBSS_ATTR; /* 8192 Bytes */
117 uint8 ht_count [2][2][16] IBSS_ATTR; /* 64 Bytes */
118 uint32 tab01 [ 16] IBSS_ATTR; /* 64 Bytes */
119 uint32 tab23 [ 9] IBSS_ATTR; /* 36 Bytes */
120 uint32 tab56 [ 16] IBSS_ATTR; /* 64 Bytes */
121 uint32 tab1315 [256] IBSS_ATTR; /* 1024 Bytes */
122 uint32 tab1624 [256] IBSS_ATTR; /* 1024 Bytes */
123 uint32 tab789 [ 36] IBSS_ATTR; /* 144 Bytes */
124 uint32 tabABC [ 64] IBSS_ATTR; /* 256 Bytes */
125 uint8 t1HB [ 4] IBSS_ATTR;
126 uint8 t2HB [ 9] IBSS_ATTR;
127 uint8 t3HB [ 9] IBSS_ATTR;
128 uint8 t5HB [ 16] IBSS_ATTR;
129 uint8 t6HB [ 16] IBSS_ATTR;
130 uint8 t7HB [ 36] IBSS_ATTR;
131 uint8 t8HB [ 36] IBSS_ATTR;
132 uint8 t9HB [ 36] IBSS_ATTR;
133 uint8 t10HB [ 64] IBSS_ATTR;
134 uint8 t11HB [ 64] IBSS_ATTR;
135 uint8 t12HB [ 64] IBSS_ATTR;
136 uint8 t13HB [256] IBSS_ATTR;
137 uint8 t15HB [256] IBSS_ATTR;
138 uint16 t16HB [256] IBSS_ATTR;
139 uint16 t24HB [256] IBSS_ATTR;
140 uint8 t1l [ 8] IBSS_ATTR;
141 uint8 t2l [ 9] IBSS_ATTR;
142 uint8 t3l [ 9] IBSS_ATTR;
143 uint8 t5l [ 16] IBSS_ATTR;
144 uint8 t6l [ 16] IBSS_ATTR;
145 uint8 t7l [ 36] IBSS_ATTR;
146 uint8 t8l [ 36] IBSS_ATTR;
147 uint8 t9l [ 36] IBSS_ATTR;
148 uint8 t10l [ 64] IBSS_ATTR;
149 uint8 t11l [ 64] IBSS_ATTR;
150 uint8 t12l [ 64] IBSS_ATTR;
151 uint8 t13l [256] IBSS_ATTR;
152 uint8 t15l [256] IBSS_ATTR;
153 uint8 t16l [256] IBSS_ATTR;
154 uint8 t24l [256] IBSS_ATTR;
155 struct huffcodetab ht [HTN] IBSS_ATTR;
157 static const uint8 ht_count_const[2][2][16] =
158 { { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */
159 { 1, 5, 5, 7, 5, 8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } }, /* hleng0 */
160 { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, /* table1 */
161 { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */
163 static const uint8 t1HB_const[4] = {1,1,1,0};
164 static const uint8 t2HB_const[9] = {1,2,1,3,1,1,3,2,0};
165 static const uint8 t3HB_const[9] = {3,2,1,1,1,1,3,2,0};
166 static const uint8 t5HB_const[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0};
167 static const uint8 t6HB_const[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0};
169 static const uint8 t7HB_const[36] =
170 { 1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4,
171 12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 };
173 static const uint8 t8HB_const[36] =
174 { 3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3,
175 19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 };
177 static const uint8 t9HB_const[36] =
178 { 7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5,
179 15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 };
181 static const uint8 t10HB_const[64] =
182 {1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32,
183 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22,
184 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 };
186 static const uint8 t11HB_const[64] =
187 {3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30,
188 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26,
189 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 };
191 static const uint8 t12HB_const[64] =
192 {9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21,
193 30,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17,
194 31,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 };
196 static const uint8 t13HB_const[256] =
197 {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,
198 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,
199 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,
200 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,
201 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,
202 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,
203 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,
204 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,
205 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,
206 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 };
208 static const uint8 t15HB_const[256] =
209 {7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51,
210 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,
211 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,
212 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,
213 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,
214 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,
215 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,
216 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,
217 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,
218 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};
220 static const uint16 t16HB_const[256] =
221 {1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47,
222 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209,
223 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65,
224 115,101,179,164,155,264,246,226,395,382,362,9,66,30,59,56,102,185,173,265,142,
225 253,232,400,388,378,445,16,111,54,52,100,184,178,160,133,257,244,228,217,385,
226 366,715,10,98,48,91,88,165,157,148,261,248,407,397,372,380,889,884,8,85,84,81,
227 159,156,143,260,249,427,401,392,383,727,713,708,7,154,76,73,141,131,256,245,
228 426,406,394,384,735,359,710,352,11,139,129,67,125,247,233,229,219,393,743,737,
229 720,885,882,439,4,243,120,118,115,227,223,396,746,742,736,721,712,706,223,436,
230 6,202,224,222,218,216,389,386,381,364,888,443,707,440,437,1728,4,747,211,210,
231 208,370,379,734,723,714,1735,883,877,876,3459,865,2,377,369,102,187,726,722,
232 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3,
233 1,3};
235 static const uint16 t24HB_const[256] =
236 {15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71,
237 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194,
238 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293,
239 271,16,147,72,69,135,127,118,112,210,200,188,352,323,306,285,540,14,263,66,
240 129,126,119,114,214,202,192,180,341,317,301,281,262,12,249,123,121,117,113,
241 215,206,195,185,347,330,308,291,272,520,10,435,115,111,109,211,203,196,187,
242 353,332,313,298,283,531,381,17,427,212,208,205,201,193,186,177,169,320,303,
243 286,268,514,377,16,335,199,197,191,189,181,174,333,321,305,289,275,521,379,
244 371,11,668,184,183,179,175,344,331,314,304,290,277,530,383,373,366,10,652,346,
245 171,168,164,318,309,299,287,276,263,513,375,368,362,6,648,322,316,312,307,302,
246 292,284,269,261,512,376,370,364,359,4,620,300,296,294,288,282,273,266,515,380,
247 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360,
248 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3};
250 static const uint32 tab1315_const[256] =
251 { 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a,
252 0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e,
253 0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a,
254 0x0b000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0e000c,0x0e000d,0x0e000d,
255 0x070006,0x080007,0x090007,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,
256 0x0b000a,0x0c000b,0x0c000b,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
257 0x080007,0x090008,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0c000b,
258 0x0c000b,0x0d000b,0x0d000c,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
259 0x090008,0x090008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
260 0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000d,
261 0x0a0009,0x0a0009,0x0b0009,0x0c000a,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
262 0x0d000b,0x0d000b,0x0e000c,0x0d000c,0x0f000d,0x0f000d,0x10000d,0x10000e,
263 0x0a000a,0x0b0009,0x0c000a,0x0c000a,0x0d000a,0x0d000b,0x0d000b,0x0d000b,
264 0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x10000e,
265 0x0b000a,0x0b000a,0x0c000a,0x0d000b,0x0d000b,0x0d000b,0x0e000b,0x0e000c,
266 0x0e000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x12000d,0x12000e,
267 0x0a000a,0x0a000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0e000c,
268 0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x11000e,0x11000e,
269 0x0b000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0d000c,0x0f000c,
270 0x0e000c,0x0f000d,0x0f000d,0x10000d,0x10000d,0x10000e,0x12000e,0x11000e,
271 0x0b000b,0x0c000b,0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000c,
272 0x0e000c,0x0f000d,0x10000d,0x0f000d,0x10000d,0x11000e,0x12000f,0x13000e,
273 0x0c000b,0x0c000b,0x0c000b,0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0e000c,
274 0x0f000d,0x0f000d,0x0f000d,0x10000d,0x11000e,0x11000e,0x11000e,0x12000f,
275 0x0c000c,0x0d000c,0x0d000b,0x0e000c,0x0e000c,0x0f000c,0x0e000d,0x0f000d,
276 0x10000d,0x10000d,0x11000d,0x11000d,0x11000e,0x12000e,0x12000f,0x12000f,
277 0x0d000c,0x0d000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x10000d,
278 0x10000d,0x10000e,0x10000e,0x11000e,0x12000e,0x11000e,0x12000f,0x12000f,
279 0x0e000d,0x0e000d,0x0e000d,0x0f000d,0x0f000d,0x0f000d,0x11000d,0x10000d,
280 0x10000e,0x13000e,0x11000e,0x11000e,0x11000f,0x13000f,0x12000e,0x12000f,
281 0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e,
282 0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f };
284 static const uint32 tab01_const[16] =
285 { 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007,
286 0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 };
288 static const uint32 tab23_const[ 9] =
289 { 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 };
291 static const uint32 tab56_const[16] =
292 { 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007,
293 0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 };
295 static const uint32 tab789_const[36] =
296 {0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004,
297 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806,
298 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808,
299 0x00b02c09,0x00b02c09,0x00b0300a,0x00802408,0x00902408,0x00a02809,0x00b02c09,
300 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b,
301 0x00c0340b};
303 static const uint32 tabABC_const[64] =
304 {0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a,
305 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809,
306 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809,
307 0x00c02c0a,0x00b02809,0x00b0280a,0x00802007,0x00902007,0x00a02408,0x00b02c08,
308 0x00c02809,0x00c0300a,0x00b0280a,0x00c02c0a,0x00902408,0x00a02808,0x00b02809,
309 0x00c02c09,0x00c02c0a,0x00c0300a,0x00c02c0a,0x00c0300b,0x00a02409,0x00b02809,
310 0x00c02c0a,0x00c0300a,0x00d0300a,0x00d0340b,0x00c0300a,0x00d0340b,0x00902409,
311 0x00a02409,0x00b02409,0x00c0280a,0x00c02c0a,0x00c0300b,0x00d0300b,0x00d0300c,
312 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b,
313 0x00d0300c};
315 static const uint32 tab1624_const[256] =
316 {0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a,
317 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c,
318 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009,
319 0x000b000a,0x000b000a,0x000b000b,0x000c000b,0x000c000b,0x000c000c,0x000d000c,
320 0x000e000c,0x000d000c,0x000e000c,0x000a000a,0x00070007,0x00080007,0x00090008,
321 0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000b,0x000d000b,0x000c000b,
322 0x000d000b,0x000d000c,0x000d000c,0x000e000c,0x000e000d,0x000b0009,0x00090008,
323 0x00090008,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,0x000c000b,
324 0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,0x000f000c,
325 0x000c0009,0x000a0009,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,
326 0x000d000a,0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,
327 0x000f000c,0x000f000d,0x000b0009,0x000a000a,0x000a0009,0x000b000a,0x000b000a,
328 0x000c000a,0x000d000a,0x000d000b,0x000e000b,0x000d000b,0x000e000b,0x000e000c,
329 0x000f000c,0x000f000c,0x000f000c,0x0010000c,0x000c0009,0x000b000a,0x000b000a,
330 0x000b000a,0x000c000a,0x000d000a,0x000d000b,0x000d000b,0x000d000b,0x000e000b,
331 0x000e000c,0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000d,0x000c0009,
332 0x000b000b,0x000b000a,0x000c000a,0x000c000a,0x000d000b,0x000d000b,0x000d000b,
333 0x000e000b,0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000d,
334 0x0011000d,0x000c000a,0x000b000b,0x000c000b,0x000c000b,0x000d000b,0x000d000b,
335 0x000d000b,0x000e000b,0x000e000b,0x000f000b,0x000f000c,0x000f000c,0x000f000c,
336 0x0010000c,0x0010000d,0x0010000d,0x000c000a,0x000c000b,0x000c000b,0x000c000b,
337 0x000d000b,0x000d000b,0x000e000b,0x000e000b,0x000f000c,0x000f000c,0x000f000c,
338 0x000f000c,0x0010000c,0x000f000d,0x0010000d,0x000f000d,0x000d000a,0x000c000c,
339 0x000d000b,0x000c000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000e000c,
340 0x000f000c,0x0010000c,0x0010000c,0x0010000d,0x0011000d,0x0011000d,0x0010000d,
341 0x000c000a,0x000d000c,0x000d000c,0x000d000b,0x000d000b,0x000e000b,0x000e000c,
342 0x000f000c,0x0010000c,0x0010000c,0x0010000c,0x0010000c,0x0010000d,0x0010000d,
343 0x000f000d,0x0010000d,0x000d000a,0x000d000c,0x000e000c,0x000e000c,0x000e000c,
344 0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000c,0x0010000d,
345 0x0010000d,0x0010000d,0x0010000d,0x0012000d,0x000d000a,0x000f000c,0x000e000c,
346 0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000c,0x0010000c,0x0010000d,
347 0x0012000d,0x0011000d,0x0011000d,0x0011000d,0x0013000d,0x0011000d,0x000d000a,
348 0x000e000d,0x000f000c,0x000d000c,0x000e000c,0x0010000c,0x0010000c,0x000f000c,
349 0x0010000d,0x0010000d,0x0011000d,0x0012000d,0x0011000d,0x0013000d,0x0011000d,
350 0x0010000d,0x000d000a,0x000a0009,0x000a0009,0x000a0009,0x000b0009,0x000b0009,
351 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a,
352 0x000d000a,0x000d000a,0x000d000a,0x000a0006};
354 static const uint8 t1l_const[8] = {1,3,2,3,1,4,3,5};
355 static const uint8 t2l_const[9] = {1,3,6,3,3,5,5,5,6};
356 static const uint8 t3l_const[9] = {2,2,6,3,2,5,5,5,6};
357 static const uint8 t5l_const[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8};
358 static const uint8 t6l_const[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7};
360 static const uint8 t7l_const[36] =
361 {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};
363 static const uint8 t8l_const[36] =
364 {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};
366 static const uint8 t9l_const[36] =
367 {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};
369 static const uint8 t10l_const[64] =
370 {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,
371 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};
373 static const uint8 t11l_const[64] =
374 {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,
375 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};
377 static const uint8 t12l_const[64] =
378 {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,
379 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};
381 static const uint8 t13l_const[256] =
382 {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,
383 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,
384 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,
385 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,
386 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,
387 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,
388 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,
389 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,
390 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,
391 15,15,16,16,19,18,19,16};
393 static const uint8 t15l_const[256] =
394 {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,
395 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,
396 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,
397 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,
398 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,
399 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,
400 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,
401 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,
402 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13};
404 static const uint8 t16l_const[256] =
405 {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,
406 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,
407 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,
408 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,
409 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,
410 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,
411 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,
412 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,
413 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,
414 11,11,11,11,11,11,11,8};
416 static const uint8 t24l_const[256] =
417 {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,
418 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,
419 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,
420 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,
421 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,
422 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,
423 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,
424 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,
425 7,7,7,7,7,7,8,8,8,8,4};
427 static const struct huffcodetab ht_const[HTN] =
428 { { 0, NULL, NULL}, /* Apparently not used */
429 { 2, t1HB, t1l},
430 { 3, t2HB, t2l},
431 { 3, t3HB, t3l},
432 { 0, NULL, NULL}, /* Apparently not used */
433 { 4, t5HB, t5l},
434 { 4, t6HB, t6l},
435 { 6, t7HB, t7l},
436 { 6, t8HB, t8l},
437 { 6, t9HB, t9l},
438 { 8, t10HB, t10l},
439 { 8, t11HB, t11l},
440 { 8, t12HB, t12l},
441 {16, t13HB, t13l},
442 { 0, NULL, NULL}, /* Apparently not used */
443 {16, t15HB, t15l} };
445 static const struct huffcodebig ht_big[HTN] =
446 { { 16, 1, 1 },
447 { 16, 2, 3 },
448 { 16, 3, 7 },
449 { 16, 4, 15 },
450 { 16, 6, 63 },
451 { 16, 8, 255 },
452 { 16, 10, 1023 },
453 { 16, 13, 8191 },
454 { 16, 4, 15 },
455 { 16, 5, 31 },
456 { 16, 6, 63 },
457 { 16, 7, 127 },
458 { 16, 8, 255 },
459 { 16, 9, 511 },
460 { 16, 11, 2047 },
461 { 16, 13, 8191 } };
463 static const struct
465 uint32 region0_cnt;
466 uint32 region1_cnt;
467 } subdv_table[23] =
468 { {0, 0}, /* 0 bands */
469 {0, 0}, /* 1 bands */
470 {0, 0}, /* 2 bands */
471 {0, 0}, /* 3 bands */
472 {0, 0}, /* 4 bands */
473 {0, 1}, /* 5 bands */
474 {1, 1}, /* 6 bands */
475 {1, 1}, /* 7 bands */
476 {1, 2}, /* 8 bands */
477 {2, 2}, /* 9 bands */
478 {2, 3}, /* 10 bands */
479 {2, 3}, /* 11 bands */
480 {3, 4}, /* 12 bands */
481 {3, 4}, /* 13 bands */
482 {3, 4}, /* 14 bands */
483 {4, 5}, /* 15 bands */
484 {4, 5}, /* 16 bands */
485 {4, 6}, /* 17 bands */
486 {5, 6}, /* 18 bands */
487 {5, 6}, /* 19 bands */
488 {5, 7}, /* 20 bands */
489 {6, 7}, /* 21 bands */
490 {6, 7}, /* 22 bands */
493 static const uint32 sfBand[6][23] =
495 /* Table B.2.b: 22.05 kHz */
496 {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
497 /* Table B.2.c: 24 kHz */
498 {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
499 /* Table B.2.a: 16 kHz */
500 {0,6,12,18,24,30,36,44,45,66,80,96,116,140,168,200,238,248,336,396,464,522,576},
501 /* Table B.8.b: 44.1 kHz */
502 {0,4, 8,12,16,20,24,30,36,44,52,62, 74, 90,110,134,162,196,238,288,342,418,576},
503 /* Table B.8.c: 48 kHz */
504 {0,4, 8,12,16,20,24,30,36,42,50,60, 72, 88,106,128,156,190,230,276,330,384,576},
505 /* Table B.8.a: 32 kHz */
506 {0,4, 8,12,16,20,24,30,36,44,54,66, 82,102,126,156,194,240,296,364,448,550,576} };
509 static const short int2idx_const[4096] = /* int2idx[i] = sqrt(i*sqrt(i)); */
511 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9,
512 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16,
513 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21,
514 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26,
515 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
516 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36,
517 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 40,
518 41, 41, 41, 41, 42, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45,
519 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49,
520 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53,
521 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57,
522 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61,
523 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65,
524 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68,
525 68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 72, 72, 72,
526 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75,
527 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 79, 79, 79,
528 79, 79, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82,
529 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 86, 86, 86,
530 86, 86, 86, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89,
531 89, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 93,
532 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, 95, 96, 96, 96,
533 96, 96, 96, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99,
534 99, 99,100,100,100,100,100,100,101,101,101,101,101,101,102,102,102,102,102,102,
535 103,103,103,103,103,103,104,104,104,104,104,104,104,105,105,105,105,105,105,106,
536 106,106,106,106,106,107,107,107,107,107,107,107,108,108,108,108,108,108,109,109,
537 109,109,109,109,110,110,110,110,110,110,110,111,111,111,111,111,111,112,112,112,
538 112,112,112,112,113,113,113,113,113,113,114,114,114,114,114,114,115,115,115,115,
539 115,115,115,116,116,116,116,116,116,117,117,117,117,117,117,117,118,118,118,118,
540 118,118,118,119,119,119,119,119,119,120,120,120,120,120,120,120,121,121,121,121,
541 121,121,122,122,122,122,122,122,122,123,123,123,123,123,123,123,124,124,124,124,
542 124,124,125,125,125,125,125,125,125,126,126,126,126,126,126,126,127,127,127,127,
543 127,127,128,128,128,128,128,128,128,129,129,129,129,129,129,129,130,130,130,130,
544 130,130,131,131,131,131,131,131,131,132,132,132,132,132,132,132,133,133,133,133,
545 133,133,133,134,134,134,134,134,134,134,135,135,135,135,135,135,136,136,136,136,
546 136,136,136,137,137,137,137,137,137,137,138,138,138,138,138,138,138,139,139,139,
547 139,139,139,139,140,140,140,140,140,140,140,141,141,141,141,141,141,141,142,142,
548 142,142,142,142,142,143,143,143,143,143,143,143,144,144,144,144,144,144,144,145,
549 145,145,145,145,145,145,146,146,146,146,146,146,146,147,147,147,147,147,147,147,
550 148,148,148,148,148,148,148,149,149,149,149,149,149,149,150,150,150,150,150,150,
551 150,151,151,151,151,151,151,151,152,152,152,152,152,152,152,153,153,153,153,153,
552 153,153,154,154,154,154,154,154,154,154,155,155,155,155,155,155,155,156,156,156,
553 156,156,156,156,157,157,157,157,157,157,157,158,158,158,158,158,158,158,159,159,
554 159,159,159,159,159,160,160,160,160,160,160,160,160,161,161,161,161,161,161,161,
555 162,162,162,162,162,162,162,163,163,163,163,163,163,163,163,164,164,164,164,164,
556 164,164,165,165,165,165,165,165,165,166,166,166,166,166,166,166,167,167,167,167,
557 167,167,167,167,168,168,168,168,168,168,168,169,169,169,169,169,169,169,169,170,
558 170,170,170,170,170,170,171,171,171,171,171,171,171,172,172,172,172,172,172,172,
559 172,173,173,173,173,173,173,173,174,174,174,174,174,174,174,174,175,175,175,175,
560 175,175,175,176,176,176,176,176,176,176,176,177,177,177,177,177,177,177,178,178,
561 178,178,178,178,178,178,179,179,179,179,179,179,179,180,180,180,180,180,180,180,
562 180,181,181,181,181,181,181,181,182,182,182,182,182,182,182,182,183,183,183,183,
563 183,183,183,184,184,184,184,184,184,184,184,185,185,185,185,185,185,185,186,186,
564 186,186,186,186,186,186,187,187,187,187,187,187,187,187,188,188,188,188,188,188,
565 188,189,189,189,189,189,189,189,189,190,190,190,190,190,190,190,190,191,191,191,
566 191,191,191,191,192,192,192,192,192,192,192,192,193,193,193,193,193,193,193,193,
567 194,194,194,194,194,194,194,195,195,195,195,195,195,195,195,196,196,196,196,196,
568 196,196,196,197,197,197,197,197,197,197,197,198,198,198,198,198,198,198,199,199,
569 199,199,199,199,199,199,200,200,200,200,200,200,200,200,201,201,201,201,201,201,
570 201,201,202,202,202,202,202,202,202,202,203,203,203,203,203,203,203,204,204,204,
571 204,204,204,204,204,205,205,205,205,205,205,205,205,206,206,206,206,206,206,206,
572 206,207,207,207,207,207,207,207,207,208,208,208,208,208,208,208,208,209,209,209,
573 209,209,209,209,209,210,210,210,210,210,210,210,210,211,211,211,211,211,211,211,
574 211,212,212,212,212,212,212,212,212,213,213,213,213,213,213,213,213,214,214,214,
575 214,214,214,214,214,215,215,215,215,215,215,215,215,216,216,216,216,216,216,216,
576 216,217,217,217,217,217,217,217,217,218,218,218,218,218,218,218,218,219,219,219,
577 219,219,219,219,219,220,220,220,220,220,220,220,220,221,221,221,221,221,221,221,
578 221,222,222,222,222,222,222,222,222,223,223,223,223,223,223,223,223,224,224,224,
579 224,224,224,224,224,225,225,225,225,225,225,225,225,226,226,226,226,226,226,226,
580 226,227,227,227,227,227,227,227,227,228,228,228,228,228,228,228,228,229,229,229,
581 229,229,229,229,229,229,230,230,230,230,230,230,230,230,231,231,231,231,231,231,
582 231,231,232,232,232,232,232,232,232,232,233,233,233,233,233,233,233,233,234,234,
583 234,234,234,234,234,234,234,235,235,235,235,235,235,235,235,236,236,236,236,236,
584 236,236,236,237,237,237,237,237,237,237,237,238,238,238,238,238,238,238,238,238,
585 239,239,239,239,239,239,239,239,240,240,240,240,240,240,240,240,241,241,241,241,
586 241,241,241,241,242,242,242,242,242,242,242,242,242,243,243,243,243,243,243,243,
587 243,244,244,244,244,244,244,244,244,245,245,245,245,245,245,245,245,245,246,246,
588 246,246,246,246,246,246,247,247,247,247,247,247,247,247,248,248,248,248,248,248,
589 248,248,248,249,249,249,249,249,249,249,249,250,250,250,250,250,250,250,250,250,
590 251,251,251,251,251,251,251,251,252,252,252,252,252,252,252,252,253,253,253,253,
591 253,253,253,253,253,254,254,254,254,254,254,254,254,255,255,255,255,255,255,255,
592 255,255,256,256,256,256,256,256,256,256,257,257,257,257,257,257,257,257,257,258,
593 258,258,258,258,258,258,258,259,259,259,259,259,259,259,259,259,260,260,260,260,
594 260,260,260,260,261,261,261,261,261,261,261,261,261,262,262,262,262,262,262,262,
595 262,263,263,263,263,263,263,263,263,263,264,264,264,264,264,264,264,264,265,265,
596 265,265,265,265,265,265,265,266,266,266,266,266,266,266,266,267,267,267,267,267,
597 267,267,267,267,268,268,268,268,268,268,268,268,268,269,269,269,269,269,269,269,
598 269,270,270,270,270,270,270,270,270,270,271,271,271,271,271,271,271,271,271,272,
599 272,272,272,272,272,272,272,273,273,273,273,273,273,273,273,273,274,274,274,274,
600 274,274,274,274,275,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276,
601 276,276,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,279,
602 279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,281,281,281,
603 281,281,281,281,281,282,282,282,282,282,282,282,282,282,283,283,283,283,283,283,
604 283,283,283,284,284,284,284,284,284,284,284,284,285,285,285,285,285,285,285,285,
605 286,286,286,286,286,286,286,286,286,287,287,287,287,287,287,287,287,287,288,288,
606 288,288,288,288,288,288,288,289,289,289,289,289,289,289,289,289,290,290,290,290,
607 290,290,290,290,291,291,291,291,291,291,291,291,291,292,292,292,292,292,292,292,
608 292,292,293,293,293,293,293,293,293,293,293,294,294,294,294,294,294,294,294,294,
609 295,295,295,295,295,295,295,295,295,296,296,296,296,296,296,296,296,296,297,297,
610 297,297,297,297,297,297,297,298,298,298,298,298,298,298,298,299,299,299,299,299,
611 299,299,299,299,300,300,300,300,300,300,300,300,300,301,301,301,301,301,301,301,
612 301,301,302,302,302,302,302,302,302,302,302,303,303,303,303,303,303,303,303,303,
613 304,304,304,304,304,304,304,304,304,305,305,305,305,305,305,305,305,305,306,306,
614 306,306,306,306,306,306,306,307,307,307,307,307,307,307,307,307,308,308,308,308,
615 308,308,308,308,308,309,309,309,309,309,309,309,309,309,310,310,310,310,310,310,
616 310,310,310,311,311,311,311,311,311,311,311,311,312,312,312,312,312,312,312,312,
617 312,313,313,313,313,313,313,313,313,313,314,314,314,314,314,314,314,314,314,315,
618 315,315,315,315,315,315,315,315,316,316,316,316,316,316,316,316,316,317,317,317,
619 317,317,317,317,317,317,318,318,318,318,318,318,318,318,318,318,319,319,319,319,
620 319,319,319,319,319,320,320,320,320,320,320,320,320,320,321,321,321,321,321,321,
621 321,321,321,322,322,322,322,322,322,322,322,322,323,323,323,323,323,323,323,323,
622 323,324,324,324,324,324,324,324,324,324,325,325,325,325,325,325,325,325,325,325,
623 326,326,326,326,326,326,326,326,326,327,327,327,327,327,327,327,327,327,328,328,
624 328,328,328,328,328,328,328,329,329,329,329,329,329,329,329,329,330,330,330,330,
625 330,330,330,330,330,330,331,331,331,331,331,331,331,331,331,332,332,332,332,332,
626 332,332,332,332,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,
627 334,334,335,335,335,335,335,335,335,335,335,335,336,336,336,336,336,336,336,336,
628 336,337,337,337,337,337,337,337,337,337,338,338,338,338,338,338,338,338,338,338,
629 339,339,339,339,339,339,339,339,339,340,340,340,340,340,340,340,340,340,341,341,
630 341,341,341,341,341,341,341,341,342,342,342,342,342,342,342,342,342,343,343,343,
631 343,343,343,343,343,343,344,344,344,344,344,344,344,344,344,344,345,345,345,345,
632 345,345,345,345,345,346,346,346,346,346,346,346,346,346,347,347,347,347,347,347,
633 347,347,347,347,348,348,348,348,348,348,348,348,348,349,349,349,349,349,349,349,
634 349,349,350,350,350,350,350,350,350,350,350,350,351,351,351,351,351,351,351,351,
635 351,352,352,352,352,352,352,352,352,352,352,353,353,353,353,353,353,353,353,353,
636 354,354,354,354,354,354,354,354,354,355,355,355,355,355,355,355,355,355,355,356,
637 356,356,356,356,356,356,356,356,357,357,357,357,357,357,357,357,357,357,358,358,
638 358,358,358,358,358,358,358,359,359,359,359,359,359,359,359,359,359,360,360,360,
639 360,360,360,360,360,360,361,361,361,361,361,361,361,361,361,361,362,362,362,362,
640 362,362,362,362,362,363,363,363,363,363,363,363,363,363,363,364,364,364,364,364,
641 364,364,364,364,365,365,365,365,365,365,365,365,365,365,366,366,366,366,366,366,
642 366,366,366,367,367,367,367,367,367,367,367,367,367,368,368,368,368,368,368,368,
643 368,368,369,369,369,369,369,369,369,369,369,369,370,370,370,370,370,370,370,370,
644 370,370,371,371,371,371,371,371,371,371,371,372,372,372,372,372,372,372,372,372,
645 372,373,373,373,373,373,373,373,373,373,374,374,374,374,374,374,374,374,374,374,
646 375,375,375,375,375,375,375,375,375,375,376,376,376,376,376,376,376,376,376,377,
647 377,377,377,377,377,377,377,377,377,378,378,378,378,378,378,378,378,378,379,379,
648 379,379,379,379,379,379,379,379,380,380,380,380,380,380,380,380,380,380,381,381,
649 381,381,381,381,381,381,381,382,382,382,382,382,382,382,382,382,382,383,383,383,
650 383,383,383,383,383,383,383,384,384,384,384,384,384,384,384,384,385,385,385,385,
651 385,385,385,385,385,385,386,386,386,386,386,386,386,386,386,386,387,387,387,387,
652 387,387,387,387,387,387,388,388,388,388,388,388,388,388,388,389,389,389,389,389,
653 389,389,389,389,389,390,390,390,390,390,390,390,390,390,390,391,391,391,391,391,
654 391,391,391,391,391,392,392,392,392,392,392,392,392,392,393,393,393,393,393,393,
655 393,393,393,393,394,394,394,394,394,394,394,394,394,394,395,395,395,395,395,395,
656 395,395,395,395,396,396,396,396,396,396,396,396,396,397,397,397,397,397,397,397,
657 397,397,397,398,398,398,398,398,398,398,398,398,398,399,399,399,399,399,399,399,
658 399,399,399,400,400,400,400,400,400,400,400,400,400,401,401,401,401,401,401,401,
659 401,401,402,402,402,402,402,402,402,402,402,402,403,403,403,403,403,403,403,403,
660 403,403,404,404,404,404,404,404,404,404,404,404,405,405,405,405,405,405,405,405,
661 405,405,406,406,406,406,406,406,406,406,406,406,407,407,407,407,407,407,407,407,
662 407,407,408,408,408,408,408,408,408,408,408,408,409,409,409,409,409,409,409,409,
663 409,410,410,410,410,410,410,410,410,410,410,411,411,411,411,411,411,411,411,411,
664 411,412,412,412,412,412,412,412,412,412,412,413,413,413,413,413,413,413,413,413,
665 413,414,414,414,414,414,414,414,414,414,414,415,415,415,415,415,415,415,415,415,
666 415,416,416,416,416,416,416,416,416,416,416,417,417,417,417,417,417,417,417,417,
667 417,418,418,418,418,418,418,418,418,418,418,419,419,419,419,419,419,419,419,419,
668 419,420,420,420,420,420,420,420,420,420,420,421,421,421,421,421,421,421,421,421,
669 421,422,422,422,422,422,422,422,422,422,422,423,423,423,423,423,423,423,423,423,
670 423,424,424,424,424,424,424,424,424,424,424,425,425,425,425,425,425,425,425,425,
671 425,426,426,426,426,426,426,426,426,426,426,427,427,427,427,427,427,427,427,427,
672 427,428,428,428,428,428,428,428,428,428,428,429,429,429,429,429,429,429,429,429,
673 429,430,430,430,430,430,430,430,430,430,430,431,431,431,431,431,431,431,431,431,
674 431,432,432,432,432,432,432,432,432,432,432,433,433,433,433,433,433,433,433,433,
675 433,434,434,434,434,434,434,434,434,434,434,435,435,435,435,435,435,435,435,435,
676 435,435,436,436,436,436,436,436,436,436,436,436,437,437,437,437,437,437,437,437,
677 437,437,438,438,438,438,438,438,438,438,438,438,439,439,439,439,439,439,439,439,
678 439,439,440,440,440,440,440,440,440,440,440,440,441,441,441,441,441,441,441,441,
679 441,441,442,442,442,442,442,442,442,442,442,442,443,443,443,443,443,443,443,443,
680 443,443,443,444,444,444,444,444,444,444,444,444,444,445,445,445,445,445,445,445,
681 445,445,445,446,446,446,446,446,446,446,446,446,446,447,447,447,447,447,447,447,
682 447,447,447,448,448,448,448,448,448,448,448,448,448,448,449,449,449,449,449,449,
683 449,449,449,449,450,450,450,450,450,450,450,450,450,450,451,451,451,451,451,451,
684 451,451,451,451,452,452,452,452,452,452,452,452,452,452,453,453,453,453,453,453,
685 453,453,453,453,453,454,454,454,454,454,454,454,454,454,454,455,455,455,455,455,
686 455,455,455,455,455,456,456,456,456,456,456,456,456,456,456,457,457,457,457,457,
687 457,457,457,457,457,457,458,458,458,458,458,458,458,458,458,458,459,459,459,459,
688 459,459,459,459,459,459,460,460,460,460,460,460,460,460,460,460,460,461,461,461,
689 461,461,461,461,461,461,461,462,462,462,462,462,462,462,462,462,462,463,463,463,
690 463,463,463,463,463,463,463,463,464,464,464,464,464,464,464,464,464,464,465,465,
691 465,465,465,465,465,465,465,465,466,466,466,466,466,466,466,466,466,466,466,467,
692 467,467,467,467,467,467,467,467,467,468,468,468,468,468,468,468,468,468,468,469,
693 469,469,469,469,469,469,469,469,469,469,470,470,470,470,470,470,470,470,470,470,
694 471,471,471,471,471,471,471,471,471,471,472,472,472,472,472,472,472,472,472,472,
695 472,473,473,473,473,473,473,473,473,473,473,474,474,474,474,474,474,474,474,474,
696 474,475,475,475,475,475,475,475,475,475,475,475,476,476,476,476,476,476,476,476,
697 476,476,477,477,477,477,477,477,477,477,477,477,477,478,478,478,478,478,478,478,
698 478,478,478,479,479,479,479,479,479,479,479,479,479,479,480,480,480,480,480,480,
699 480,480,480,480,481,481,481,481,481,481,481,481,481,481,482,482,482,482,482,482,
700 482,482,482,482,482,483,483,483,483,483,483,483,483,483,483,484,484,484,484,484,
701 484,484,484,484,484,484,485,485,485,485,485,485,485,485,485,485,486,486,486,486,
702 486,486,486,486,486,486,486,487,487,487,487,487,487,487,487,487,487,488,488,488,
703 488,488,488,488,488,488,488,488,489,489,489,489,489,489,489,489,489,489,490,490,
704 490,490,490,490,490,490,490,490,490,491,491,491,491,491,491,491,491,491,491,492,
705 492,492,492,492,492,492,492,492,492,492,493,493,493,493,493,493,493,493,493,493,
706 494,494,494,494,494,494,494,494,494,494,494,495,495,495,495,495,495,495,495,495,
707 495,496,496,496,496,496,496,496,496,496,496,496,497,497,497,497,497,497,497,497,
708 497,497,497,498,498,498,498,498,498,498,498,498,498,499,499,499,499,499,499,499,
709 499,499,499,499,500,500,500,500,500,500,500,500,500,500,501,501,501,501,501,501,
710 501,501,501,501,501,502,502,502,502,502,502,502,502,502,502,503,503,503,503,503,
711 503,503,503,503,503,503,504,504,504,504,504,504,504,504,504,504,504,505,505,505,
712 505,505,505,505,505,505,505,506,506,506,506,506,506,506,506,506,506,506,507,507,
713 507,507,507,507,507,507,507,507,507,508,508,508,508,508,508,508,508,508,508,509,
714 509,509,509,509,509,509,509,509,509,509,510,510,510,510,510,510,510,510,510,510,
715 510,511,511,511,511,511,511,511,511,511,511,512,512,512,512,512 };
717 static const int order[32] =
718 { 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
719 2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 };
721 static const int bitr_index[2][15] =
722 { {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160},
723 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} };
725 static const int num_bands[3][15] =
726 { {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32},
727 {0,10,10,10,10,10,12,14, 18, 24, 26, 28, 30, 32, 32},
728 {0,10,12,14,18,24,26,28, 30, 32, 32, 32, 32, 32, 32} };
730 static const int cx_const[9] =
731 { 16135, 10531, 5604, 15396, -2845,-12551, 14189, 8192, 16384 };
733 static const int ca_const[8] =
734 {-16859,-15458,-10269, -5961, -3099, -1342, -465, -121 };
736 static const int cs_const[8] =
737 { 28098, 28893, 31117, 32221, 32621, 32740, 32765, 32768 };
739 static const short enwindow_const[15*27+24] =
740 { 0, 65, 593, 1766, 22228, 2115, 611, 62,
741 8, 119, 1419, 10564,-11659,-1635,-154, -9,
742 -8, -119,-1419,-10564, 11659, 1635, 154, 9, 464, 100, 91,
743 0, 69, 604, 1635, 23148, 2363, 643, 62,
744 7, 107, 1368, 10449,-12733,-1818,-180,-11,
745 -7, -107,-1368,-10449, 12733, 1818, 180, 11, 420, 200, 164,
746 0, 72, 608, 1465, 23979, 2600, 671, 63,
747 7, 94, 1305, 10265,-13818,-2004,-207,-12,
748 -7, -94,-1305,-10265, 13818, 2004, 207, 12, 380, 297, 220,
749 0, 76, 606, 1256, 24718, 2825, 693, 63,
750 6, 81, 1232, 10016,-14908,-2192,-236,-14,
751 -6, -81,-1232,-10016, 14908, 2192, 236, 14, 342, 392, 262,
752 0, 78, 597, 1007, 25359, 3033, 712, 63,
753 6, 68, 1150, 9706,-15995,-2380,-267,-15,
754 -6, -68,-1150, -9706, 15995, 2380, 267, 15, 307, 483, 289,
755 0, 80, 580, 719, 25901, 3224, 726, 62,
756 6, 54, 1060, 9343,-17072,-2565,-299,-17,
757 -6, -54,-1060, -9343, 17072, 2565, 299, 17, 274, 569, 304,
758 -1, 82, 555, 391, 26339, 3395, 735, 61,
759 5, 40, 963, 8930,-18131,-2747,-332,-19,
760 -5, -40, -963, -8930, 18131, 2747, 332, 19, 242, 650, 307,
761 -1, 83, 523, 26, 26672, 3545, 740, 60,
762 5, 27, 861, 8474,-19164,-2923,-366,-21,
763 -5, -27, -861, -8474, 19164, 2923, 366, 21, 212, 724, 300,
764 -1, 83, 482, -376, 26900, 3672, 739, 58,
765 4, 14, 756, 7981,-20163,-3092,-401,-24,
766 -4, -14, -756, -7981, 20163, 3092, 401, 24, 183, 792, 283,
767 -1, 82, 433, -812, 27022, 3776, 735, 56,
768 4, 1, 648, 7456,-21122,-3250,-435,-26,
769 -4, -1, -648, -7456, 21122, 3250, 435, 26, 155, 851, 258,
770 -1, 81, 376, -1281, 27038, 3855, 726, 54,
771 3, -11, 539, 6907,-22032,-3397,-470,-28,
772 -3, 11, -539, -6907, 22032, 3397, 470, 28, 128, 903, 226,
773 -1, 78, 312, -1778, 26951, 3910, 713, 52,
774 3, -22, 430, 6338,-22887,-3530,-503,-31,
775 -3, 22, -430, -6338, 22887, 3530, 503, 31, 102, 946, 188,
776 -2, 75, 239, -2302, 26761, 3941, 696, 49,
777 3, -33, 322, 5757,-23678,-3648,-537,-34,
778 -3, 33, -322, -5757, 23678, 3648, 537, 34, 76, 980, 145,
779 -2, 70, 160, -2848, 26472, 3948, 676, 47,
780 3, -42, 217, 5167,-24399,-3749,-568,-36,
781 -3, 42, -217, -5167, 24399, 3749, 568, 36, 50, 1004, 99,
782 -2, 65, 74, -3412, 26087, 3931, 653, 44,
783 2, -51, 115, 4577,-25045,-3830,-599,-39,
784 -2, 51, -115, -4577, 25045, 3830, 599, 39, 25, 1019, 50,
786 25610,3891,627,42,-3990,-18,58,-2,
787 21226,-21226,10604,-10604,1860,-1860,1458,-1458,576,-576,130,-130,60,-60,8,-8
790 static const int win_const[18][4] = {
791 { -3072, -134, -146, 3352 },
792 { -2747, -362, -471, 3579 },
793 { -2387, -529, -831, 3747 },
794 { -2004, -632,-1214, 3850 },
795 { -1609, -666,-1609, 3884 },
796 { -1214, -632,-2004, 3850 },
797 { -831, -529,-2387, 3747 },
798 { -471, -362,-2747, 3579 },
799 { -146, -134,-3072, 3352 },
800 { 134,-3072,-3352, -146 },
801 { 362,-2747,-3579, -471 },
802 { 529,-2387,-3747, -831 },
803 { 632,-2004,-3850,-1214 },
804 { 666,-1609,-3884,-1609 },
805 { 632,-1214,-3850,-2004 },
806 { 529, -831,-3747,-2387 },
807 { 362, -471,-3579,-2747 },
808 { 134, -146,-3352,-3072 } };
811 static char* wav_filename;
812 static int mp3file, wavfile, wav_size, frames;
813 static uint32 enc_buffer[16384]; /* storage for 65536 Bytes */
814 static int enc_chunk = 0; /* encode chunk counter */
815 static int enc_size;
816 static config_t cfg;
818 /* forward declarations */
819 int HuffmanCode( short *ix, int *xr, uint32 begin, uint32 end, int table);
820 int HuffmanCod1( short *ix, int *xr, uint32 begin, uint32 end, int table);
821 void putbits(uint32 val, uint32 nbit);
822 int find_best_2( short *ix, uint32 start, uint32 end, const uint32 *table,
823 uint32 len, int *bits);
824 int find_best_3( short *ix, uint32 start, uint32 end, const uint32 *table,
825 uint32 len, int *bits);
826 int count_bit1 ( short *ix, uint32 start, uint32 end, int *bits );
827 int count_bigv ( short *ix, uint32 start, uint32 end, int table0, int table1,
828 int *bits);
831 bool checkString(int fd, char *string)
833 char temp[4];
835 rb->read(fd, temp, 4);
837 return (*(long*)temp == *(long*)string) ? 1 : 0;
840 int Read16BitsLowHigh(int fd)
842 char first, second;
844 rb->read(fd, &first, 1);
845 rb->read(fd, &second, 1);
847 return ((int)second << 8) | (first & 0xff);
851 int Read32BitsLowHigh(int fd)
853 int first = 0xffff & Read16BitsLowHigh(fd);
854 int second = 0xffff & Read16BitsLowHigh(fd);
856 return (second << 16) + first;
859 int wave_open(void)
861 unsigned short wFormatTag;
862 unsigned long dAvgBytesPerSec;
863 unsigned short wBlockAlign;
864 unsigned short bits_per_samp;
865 long header_size;
867 if((wavfile = rb->open(wav_filename, O_RDONLY)) < 0)
868 return -1;
870 if(!checkString(wavfile,"RIFF")) return -2;
871 Read32BitsLowHigh(wavfile); /* complete wave chunk size */
872 if(!checkString(wavfile,"WAVE")) return -3;
873 if(!checkString(wavfile,"fmt ")) return -4;
875 header_size = Read32BitsLowHigh(wavfile); /* chunk size */
876 wFormatTag = Read16BitsLowHigh(wavfile);
878 cfg.channels = Read16BitsLowHigh(wavfile);
879 cfg.samplerate = Read32BitsLowHigh(wavfile);
880 dAvgBytesPerSec = Read32BitsLowHigh(wavfile);
881 wBlockAlign = Read16BitsLowHigh(wavfile);
882 bits_per_samp = Read16BitsLowHigh(wavfile);
884 if(wFormatTag != 0x0001) return -5;
885 if(bits_per_samp != 16) return -6;
886 if(cfg.channels > 2) return -7;
887 if(!checkString(wavfile,"data")) return -8;
889 header_size = 0x28;
890 wav_size = rb->filesize(wavfile);
891 rb->lseek(wavfile, header_size, SEEK_SET);
893 return 0;
896 int read_samples(uint32 *buffer, int num_samples)
898 int s, samples = rb->read(wavfile, buffer, 4 * num_samples) / 4;
899 /* Pad last sample with zeros */
900 for(s=samples; s<num_samples; s++)
901 buffer[s] = 0;
903 return samples;
906 inline uint32 myswap32(uint32 val)
908 const uint8* v = (const uint8*)&val;
910 return ((uint32)v[0]<<24) | ((uint32)v[1]<<16) | ((uint32)v[2]<<8) | v[3];
913 void encodeSideInfo( side_info_t si[2][2] )
915 int gr, ch, header;
916 uint32 cc=0, sz=0;
918 header = 0xfff00000;
919 header |= cfg.mpg.type << 19; /* mp3 type: 1 */
920 header |= 1 << 17; /* mp3 layer: 1 */
921 header |= 1 << 16; /* mp3 crc: 0 */
922 header |= cfg.mpg.bitr_id << 12;
923 header |= cfg.mpg.smpl_id << 10;
924 header |= cfg.mpg.padding << 9;
925 header |= cfg.mpg.mode << 6;
926 header |= 1 << 2; /* mp3 original: 1 */
927 putbits( header, 32 );
929 if(cfg.mpg.type)
930 { /* MPEG1 */
931 if(cfg.channels == 2) { putlong( 0, 20); }
932 else { putlong( 0, 18); }
934 for(gr=0; gr<cfg.granules; gr++)
935 for(ch=0; ch<cfg.channels; ch++)
937 side_info_t *gi = &si[gr][ch];
939 putlong( gi->part2_3_length, 12 );
940 putlong( gi->address3>>1, 9 );
941 putlong( gi->global_gain, 8 );
942 putlong( gi->table_select[0], 10 );
943 putlong( gi->table_select[1], 5 );
944 putlong( gi->table_select[2], 5 );
945 putlong( gi->region_0_1, 7 );
946 putlong( gi->table_select[3], 3 );
949 else
950 { /* MPEG2 */
951 if(cfg.channels == 2) { putlong( 0, 10); }
952 else { putlong( 0, 9); }
954 for(ch=0; ch<cfg.channels; ch++)
956 side_info_t *gi = &si[0][ch];
958 putlong( gi->part2_3_length, 12);
959 putlong( gi->address3>>1, 9);
960 putlong( gi->global_gain, 8);
961 putlong( gi->table_select[0], 15);
962 putlong( gi->table_select[1], 5);
963 putlong( gi->table_select[2], 5);
964 putlong( gi->region_0_1, 7);
965 putlong( gi->table_select[3], 2);
968 /* flush remaining bits */
969 putbits(cc, sz);
972 /* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS,
973 as well as the definitions of the side information on pages 26 and 27. */
974 void Huffmancodebits( short *ix, int *xr, side_info_t *gi )
976 int region1 = gi->address1;
977 int region2 = gi->address2;
978 int bigvals = gi->address3;
979 int count1 = bigvals + (gi->count1 << 2);
980 int stuffBits = 0;
981 int bits = 0;
983 if(region1 > 0)
984 bits += HuffmanCode(ix, xr, 0 , region1, gi->table_select[0]);
986 if(region2 > region1)
987 bits += HuffmanCode(ix, xr, region1, region2, gi->table_select[1]);
989 if(bigvals > region2)
990 bits += HuffmanCode(ix, xr, region2, bigvals, gi->table_select[2]);
992 if(count1 > bigvals)
993 bits += HuffmanCod1(ix, xr, bigvals, count1, gi->table_select[3]);
995 if((stuffBits = gi->part2_3_length - bits) > 0)
997 int stuffWords = stuffBits >> 5;
998 int remainBits = stuffBits & 31;
1000 if( remainBits )
1001 putbits( ~0, remainBits );
1003 while( stuffWords-- )
1004 putbits( ~0, 32 ); /* Huffman code tables leed to padding ones */
1008 int HuffmanCod1( short *ix, int *xr, uint32 begin, uint32 end, int tbl)
1010 uint32 cc=0, sz=0;
1011 uint32 i, d, p;
1012 int sumbit=0, s=0, l=0, v, w, x, y;
1013 #define sgnv (xr[i+0] < 0 ? 1 : 0)
1014 #define sgnw (xr[i+1] < 0 ? 1 : 0)
1015 #define sgnx (xr[i+2] < 0 ? 1 : 0)
1016 #define sgny (xr[i+3] < 0 ? 1 : 0)
1018 for(i=begin; i<end; i+=4)
1020 v = ix[i+0];
1021 w = ix[i+1];
1022 x = ix[i+2];
1023 y = ix[i+3];
1024 p = (v << 3) + (w << 2) + (x << 1) + y;
1026 switch(p)
1028 case 0: l=0; s = 0; break;
1029 case 1: l=1; s = sgnv; break;
1030 case 2: l=1; s = sgnw; break;
1031 case 3: l=2; s = (sgnv << 1) + sgnw; break;
1032 case 4: l=1; s = sgnx; break;
1033 case 5: l=2; s = (sgnv << 1) + sgnx; break;
1034 case 6: l=2; s = (sgnw << 1) + sgnx; break;
1035 case 7: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break;
1036 case 8: l=1; s = sgny; break;
1037 case 9: l=2; s = (sgnv << 1) + sgny; break;
1038 case 10: l=2; s = (sgnw << 1) + sgny; break;
1039 case 11: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break;
1040 case 12: l=2; s = (sgnx << 1) + sgny; break;
1041 case 13: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break;
1042 case 14: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break;
1043 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break;
1046 d = (ht_count[tbl][0][p] << l) + s;
1047 l = ht_count[tbl][1][p];
1048 putlong( d, l );
1049 sumbit += l;
1052 /* flush remaining bits */
1053 putbits(cc, sz);
1055 return sumbit;
1058 /* Implements the pseudocode of page 98 of the IS */
1059 int HuffmanCode( short *ix, int *xr, uint32 begin, uint32 end, int table)
1061 uint32 cc=0, sz=0, code;
1062 uint32 i, xl=0, yl=0, idx;
1063 int x, y, bit, sumbit=0;
1064 #define sign_x (xr[i+0] < 0 ? 1 : 0)
1065 #define sign_y (xr[i+1] < 0 ? 1 : 0)
1067 if(table == 0)
1068 return 0;
1070 if( table > 15 )
1071 { /* ESC-table is used */
1072 uint32 linbits = ht_big[table-16].linbits;
1073 uint16 *hffcode = table < 24 ? t16HB : t24HB;
1074 uint8 *hlen = table < 24 ? t16l : t24l;
1076 for(i=begin; i<end; i+=2)
1078 x = ix[ i ];
1079 y = ix[i+1];
1081 if(x > 14) { xl = x - 15; x = 15; }
1082 if(y > 14) { yl = y - 15; y = 15; }
1084 idx = x * 16 + y;
1085 code = hffcode[idx];
1086 bit = hlen [idx];
1088 if(x)
1090 if(x > 14)
1092 code = (code << linbits) | xl;
1093 bit += linbits;
1096 code = (code << 1) | sign_x;
1097 bit += 1;
1100 if(y)
1102 if(y > 14)
1104 code = (code << linbits) | yl;
1105 bit += linbits;
1108 code = (code << 1) | sign_y;
1109 bit += 1;
1112 putlong( code, bit );
1113 sumbit += bit;
1116 else
1117 { /* No ESC-words */
1118 const struct huffcodetab *h = &ht[table];
1120 for(i=begin; i<end; i+=2)
1122 x = ix[i];
1123 y = ix[i+1];
1125 idx = x * h->len + y;
1126 code = h->table[idx];
1127 bit = h->hlen [idx];
1129 if(x)
1131 code = (code << 1) | sign_x;
1132 bit += 1;
1135 if(y)
1137 code = (code << 1) | sign_y;
1138 bit += 1;
1141 putlong( code, bit );
1142 sumbit += bit;
1146 /* flush remaining bits */
1147 putbits(cc, sz);
1149 return sumbit;
1152 void putbits(uint32 val, uint32 nbit)
1154 int new_bitpos = CodedData.bitpos + nbit;
1155 int ptrpos = CodedData.bitpos >> 5;
1157 val = val & (0xffffffff >> (32 - nbit));
1159 /* data fit in one uint32 */
1160 if(((new_bitpos - 1) >> 5) == ptrpos)
1161 CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31);
1162 else
1164 CodedData.bbuf[ptrpos ] |= val >> ((new_bitpos - 32) & 31);
1165 CodedData.bbuf[ptrpos+1] |= val << ((32 - new_bitpos) & 31);
1168 CodedData.bitpos = new_bitpos;
1171 /***************************************************************************/
1172 /* Choose the Huffman table that will encode ix[begin..end] with */
1173 /* the fewest bits. */
1174 /* Note: This code contains knowledge about the sizes and characteristic */
1175 /* of the Huffman tables as defined in the IS (Table B.7), and will not */
1176 /* work with any arbitrary tables. */
1177 /***************************************************************************/
1178 int choose_table( short *ix, uint32 begin, uint32 end, int *bits )
1180 uint32 i;
1181 int max, table0, table1;
1183 for(i=begin,max=0; i<end; i++)
1184 if(ix[i] > max)
1185 max = ix[i];
1187 if(max < 16)
1189 /* tables without linbits */
1190 /* indx: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1191 /* len: 0, 2, 3, 3, 0, 4, 4, 6, 6, 6, 8, 8, 8, 16, 0, 16 */
1192 switch(max)
1194 case 0: return 0;
1195 case 1: return count_bit1(ix, begin, end, bits);
1196 case 2: return 2 + find_best_2(ix, begin, end, tab23, 3, bits);
1197 case 3: return 5 + find_best_2(ix, begin, end, tab56, 4, bits);
1198 case 4:
1199 case 5: return 7 + find_best_3(ix, begin, end, tab789, 6, bits);
1200 case 6:
1201 case 7: return 10 + find_best_3(ix, begin, end, tabABC, 8, bits);
1202 default: return 13 + find_best_2(ix, begin, end, tab1315, 16, bits) * 2;
1205 else
1207 /* tables with linbits */
1208 max -= 15;
1210 for(table0=0; table0<8; table0++)
1211 if(ht_big[table0].linmax >= max)
1212 break;
1214 for(table1=8; table1<16; table1++)
1215 if(ht_big[table1].linmax >= max)
1216 break;
1218 return 16 + count_bigv(ix, begin, end, table0, table1, bits);
1222 int find_best_2(short *ix, uint32 start, uint32 end, const uint32 *table,
1223 uint32 len, int *bits)
1225 uint32 i, sum = 0;
1227 for(i=start; i<end; i+=2)
1228 sum += table[ix[i] * len + ix[i+1]];
1230 if((sum & 0xffff) <= (sum >> 16))
1232 *bits = (sum & 0xffff);
1233 return 1;
1235 else
1237 *bits = sum >> 16;
1238 return 0;
1242 int find_best_3(short *ix, uint32 start, uint32 end, const uint32 *table,
1243 uint32 len, int *bits)
1245 uint32 i, j, sum = 0;
1246 int sum1 = 0;
1247 int sum2 = 0;
1248 int sum3 = 0;
1250 /* avoid overflow in packed additions: 78*13 < 1024 */
1251 for(i=start; i<end; )
1253 j = i + 2*78 > end ? end : i + 2*78;
1255 for(sum=0; i<j; i+=2)
1256 sum += table[ix[i] * len + ix[i+1]];
1258 sum1 += (sum >> 20);
1259 sum2 += (sum >> 10) & 0x3ff;
1260 sum3 += (sum >> 0) & 0x3ff;
1263 i = 0;
1264 if(sum1 > sum2) { sum1 = sum2; i = 1; }
1265 if(sum1 > sum3) { sum1 = sum3; i = 2; }
1267 *bits = sum1;
1269 return i;
1272 /*************************************************************************/
1273 /* Function: Count the number of bits necessary to code the subregion. */
1274 /*************************************************************************/
1275 int count_bit1(short *ix, uint32 start, uint32 end, int *bits )
1277 uint32 i, sum = 0;
1279 for(i=start; i<end; i+=2)
1280 sum += t1l[4 + ix[i] * 2 + ix[i+1]];
1282 *bits = sum;
1284 return 1; /* this is table1 */
1287 int count_bigv(short *ix, uint32 start, uint32 end, int table0,
1288 int table1, int *bits )
1290 uint32 i, sum0, sum1, sum=0, bigv=0, x, y;
1292 /* ESC-table is used */
1293 for(i=start; i<end; i+=2)
1295 x = ix[i];
1296 y = ix[i+1];
1298 if(x > 14) { x = 15; bigv++; }
1299 if(y > 14) { y = 15; bigv++; }
1301 sum += tab1624[x * 16 + y];
1304 sum0 = (sum >> 16) + bigv * ht_big[table0].linbits;
1305 sum1 = (sum & 0xffff) + bigv * ht_big[table1].linbits;
1307 if(sum0 <= sum1)
1309 *bits = sum0;
1310 return table0;
1312 else
1314 *bits = sum1;
1315 return table1;
1319 /*************************************************************************/
1320 /* Function: Calculation of rzero, count1, address3 */
1321 /* (Partitions ix into big values, quadruples and zeros). */
1322 /*************************************************************************/
1323 int calc_runlen( short *ix, side_info_t *si )
1325 int p, i, sum = 0;
1327 for(i=SAMPL2; i-=2; )
1328 if(*(uint32*)&ix[i-2]) /* !!!! short *ix; !!!!! */
1329 break;
1331 si->count1 = 0;
1333 for( ; i>3; i-=4)
1335 int v = ix[i-1];
1336 int w = ix[i-2];
1337 int x = ix[i-3];
1338 int y = ix[i-4];
1340 if((v | w | x | y) <= 1)
1342 p = (y<<3) + (x<<2) + (w<<1) + (v);
1344 sum += tab01[p];
1346 si->count1++;
1348 else break;
1351 si->address3 = i;
1353 if((sum >> 16) < (sum & 0xffff))
1355 si->table_select[3] = 0;
1356 return sum >> 16;
1358 else
1360 si->table_select[3] = 1;
1361 return sum & 0xffff;
1366 /*************************************************************************/
1367 /* Function: Quantization of the vector xr ( -> ix) */
1368 /*************************************************************************/
1369 int quantize_int(int *xr, short *ix, side_info_t *si)
1371 int i, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 };
1373 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4;
1375 /* check for integer overflow */
1376 if(((si->max_val + 256) >> 8) * s >= (1622 << 8))
1377 return 0;
1379 for(i=SAMPL2; i--; )
1380 ix[i] = int2idx[(abs(xr[i]) * s + 0x8000) >> 16];
1382 return 1;
1385 /*************************************************************************/
1386 /* subdivides the bigvalue region which will use separate Huffman tables */
1387 /*************************************************************************/
1388 void subdivide(side_info_t *si)
1390 int scfb, count0, count1;
1392 if( !si->address3 )
1393 { /* no bigvalue region */
1394 si->region_0_1 = 0;
1395 si->address1 = 0;
1396 si->address2 = 0;
1398 else
1400 /* Calculate scale factor band index */
1401 for(scfb=0; scalefac[scfb] < si->address3; )
1402 scfb++;
1404 count0 = subdv_table[scfb].region0_cnt;
1405 count1 = subdv_table[scfb].region1_cnt;
1407 si->region_0_1 = (count0 << 3) | count1;
1408 si->address1 = scalefac[count0 + 1];
1409 si->address2 = scalefac[count0 + 1 + count1 + 1];
1413 /*******************************************************************/
1414 /* Count the number of bits necessary to code the bigvalues region */
1415 /*******************************************************************/
1416 int bigv_bitcount(short *ix, side_info_t *gi)
1418 int b1=0, b2=0, b3=0;
1420 /* Select huffman code tables for bigvalues regions */
1421 gi->table_select[0] = 0;
1422 gi->table_select[1] = 0;
1423 gi->table_select[2] = 0;
1425 if( gi->address1 > 0 ) /* region0 */
1426 gi->table_select[0] = choose_table(ix, 0 , gi->address1, &b1);
1428 if( gi->address2 > gi->address1 ) /* region1 */
1429 gi->table_select[1] = choose_table(ix, gi->address1, gi->address2, &b2);
1431 if( gi->address3 > gi->address2 ) /* region2 */
1432 gi->table_select[2] = choose_table(ix, gi->address2, gi->address3, &b3);
1434 return b1+b2+b3;
1437 int quantize_and_count_bits(int *xr, short *ix, side_info_t *si)
1439 int bits = 10000;
1441 if(quantize_int(xr, ix, si))
1443 bits = calc_runlen(ix, si); /* rzero,count1,address3 */
1444 subdivide(si); /* bigvalues sfb division */
1445 bits += bigv_bitcount(ix,si); /* bit count */
1448 return bits;
1451 /***********************************************************************/
1452 /* The code selects the best quantStep for a particular set of scalefacs */
1453 /***********************************************************************/
1454 int inner_loop(int *xr, int max_bits, side_info_t *si)
1456 int bits;
1458 while((bits=quantize_and_count_bits(xr, enc_data, si)) < max_bits-64)
1460 if(si->quantStep == 0)
1461 break;
1463 if(si->quantStep <= 2)
1464 si->quantStep = 0;
1465 else
1466 si->quantStep -= 2;
1469 while(bits > max_bits)
1471 si->quantStep++;
1472 bits = quantize_and_count_bits(xr, enc_data, si);
1475 return bits;
1478 void iteration_loop(int *xr, side_info_t *si, int gr_cnt)
1480 int remain, tar_bits, max_bits = cfg.mean_bits;
1482 /* distribute reserved bits to remaining granules */
1483 tar_bits = max_bits + (cfg.ResvSize / gr_cnt & ~7);
1484 if(tar_bits > max_bits + max_bits/2)
1485 tar_bits = max_bits + max_bits/2;
1487 si->part2_3_length = inner_loop(xr, tar_bits, si);
1488 si->global_gain = si->quantStep + 142 - si->additStep;
1490 /* unused bits of the reservoir can be used for remaining granules */
1491 cfg.ResvSize += max_bits - si->part2_3_length;
1493 /* end: distribute the reserved bits to one or two granules */
1494 if(gr_cnt == 1)
1496 si->part2_3_length += cfg.ResvSize;
1497 /* mp3 format allows max 12bits for granule length */
1498 if(si->part2_3_length > 4092)
1500 remain = (si->part2_3_length - 4092 + 31) >> 5;
1501 si->part2_3_length -= remain << 5;
1502 si[-1].part2_3_length += remain << 5;
1504 while(remain--)
1505 putbits(~0, 32);
1511 /* returns sum_j=0^31 a[j]*cos(PI*j*(k+1/2)/32), 0<=k<32 */
1512 void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT]) ICODE_ATTR;
1513 void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT])
1515 int k, i, u, v;
1516 short *wp, *x1, *x2;
1518 #ifdef CPU_COLDFIRE
1519 int s0, s1, t0, t1;
1521 for(k=0; k<18; k++, wk+=64, sb0+=SBLIMIT, sb1+=SBLIMIT)
1523 wp = enwindow;
1524 x1 = wk;
1525 x2 = x1 - 124;
1527 for(i=-15; i<0; i++)
1529 asm volatile(
1530 "move.l (-224*4,%[x2]), %%d4\n" /* d4 = x2[-224] */
1531 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1532 "mac.w %%d0u, %%d4u, %%acc0\n"
1533 "mac.w %%d0u, %%d4l, (-160*4,%[x2]), %%d4, %%acc1\n"
1534 "mac.w %%d0l, %%d4u, %%acc0\n"
1535 "mac.w %%d0l, %%d4l, ( -96*4,%[x2]), %%d4, %%acc1\n"
1536 "mac.w %%d1u, %%d4u, %%acc0\n"
1537 "mac.w %%d1u, %%d4l, ( -32*4,%[x2]), %%d4, %%acc1\n"
1538 "mac.w %%d1l, %%d4u, %%acc0\n"
1539 "mac.w %%d1l, %%d4l, ( 32*4,%[x2]), %%d4, %%acc1\n"
1540 "mac.w %%d2u, %%d4u, %%acc0\n"
1541 "mac.w %%d2u, %%d4l, ( 96*4,%[x2]), %%d4, %%acc1\n"
1542 "mac.w %%d2l, %%d4u, %%acc0\n"
1543 "mac.w %%d2l, %%d4l, ( 160*4,%[x2]), %%d4, %%acc1\n"
1544 "mac.w %%d3u, %%d4u, %%acc0\n"
1545 "mac.w %%d3u, %%d4l, ( 224*4,%[x2]), %%d4, %%acc1\n"
1546 "mac.w %%d3l, %%d4u, %%acc0\n"
1547 "mac.w %%d3l, %%d4l, (-256*4,%[x1]), %%d4, %%acc1\n"
1548 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */
1549 "mac.w %%d0u, %%d4u, %%acc0\n"
1550 "mac.w %%d0u, %%d4l, (-192*4,%[x1]), %%d4, %%acc1\n"
1551 "mac.w %%d0l, %%d4u, %%acc0\n"
1552 "mac.w %%d0l, %%d4l, (-128*4,%[x1]), %%d4, %%acc1\n"
1553 "mac.w %%d1u, %%d4u, %%acc0\n"
1554 "mac.w %%d1u, %%d4l, ( -64*4,%[x1]), %%d4, %%acc1\n"
1555 "mac.w %%d1l, %%d4u, %%acc0\n"
1556 "mac.w %%d1l, %%d4l, ( 0*4,%[x1]), %%d4, %%acc1\n"
1557 "mac.w %%d2u, %%d4u, %%acc0\n"
1558 "mac.w %%d2u, %%d4l, ( 64*4,%[x1]), %%d4, %%acc1\n"
1559 "mac.w %%d2l, %%d4u, %%acc0\n"
1560 "mac.w %%d2l, %%d4l, ( 128*4,%[x1]), %%d4, %%acc1\n"
1561 "mac.w %%d3u, %%d4u, %%acc0\n"
1562 "mac.w %%d3u, %%d4l, ( 192*4,%[x1]), %%d4, %%acc1\n"
1563 "mac.w %%d3l, %%d4u, %%acc0\n"
1564 "mac.w %%d3l, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n"
1565 "movclr.l %%acc0, %%d0\n"
1566 "move.l %%d0, %[s0]\n"
1567 "movclr.l %%acc1, %%d0\n"
1568 "move.l %%d0, %[s1]\n"
1570 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1571 "mac.w %%d0u, %%d4u, %%acc0\n"
1572 "mac.w %%d0u, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n"
1573 "mac.w %%d0l, %%d4u, %%acc0\n"
1574 "mac.w %%d0l, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n"
1575 "mac.w %%d1u, %%d4u, %%acc0\n"
1576 "mac.w %%d1u, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n"
1577 "mac.w %%d1l, %%d4u, %%acc0\n"
1578 "mac.w %%d1l, %%d4l, ( -32*4,%[x1]), %%d4, %%acc1\n"
1579 "mac.w %%d2u, %%d4u, %%acc0\n"
1580 "mac.w %%d2u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n"
1581 "mac.w %%d2l, %%d4u, %%acc0\n"
1582 "mac.w %%d2l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n"
1583 "mac.w %%d3u, %%d4u, %%acc0\n"
1584 "mac.w %%d3u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n"
1585 "mac.w %%d3l, %%d4u, %%acc0\n"
1586 "mac.w %%d3l, %%d4l, ( 256*4,%[x2]), %%d4, %%acc1\n"
1587 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */
1588 "mac.w %%d0u, %%d4u, %%acc0\n"
1589 "mac.w %%d0u, %%d4l, ( 192*4,%[x2]), %%d4, %%acc1\n"
1590 "mac.w %%d0l, %%d4u, %%acc0\n"
1591 "mac.w %%d0l, %%d4l, ( 128*4,%[x2]), %%d4, %%acc1\n"
1592 "mac.w %%d1u, %%d4u, %%acc0\n"
1593 "mac.w %%d1u, %%d4l, ( 64*4,%[x2]), %%d4, %%acc1\n"
1594 "mac.w %%d1l, %%d4u, %%acc0\n"
1595 "mac.w %%d1l, %%d4l, ( 0*4,%[x2]), %%d4, %%acc1\n"
1596 "mac.w %%d2u, %%d4u, %%acc0\n"
1597 "mac.w %%d2u, %%d4l, ( -64*4,%[x2]), %%d4, %%acc1\n"
1598 "mac.w %%d2l, %%d4u, %%acc0\n"
1599 "mac.w %%d2l, %%d4l, (-128*4,%[x2]), %%d4, %%acc1\n"
1600 "mac.w %%d3u, %%d4u, %%acc0\n"
1601 "mac.w %%d3u, %%d4l, (-192*4,%[x2]), %%d4, %%acc1\n"
1602 "mac.w %%d3l, %%d4u, %%acc0\n"
1603 "mac.w %%d3l, %%d4l, %%acc1\n"
1604 "movclr.l %%acc0, %%d0\n"
1605 "move.l %%d0, %[t0]\n"
1606 "movclr.l %%acc1, %%d0\n"
1607 "move.l %%d0, %[t1]\n"
1609 : [x1] "+a" (x1), [x2] "+a" (x2), [s0] "+m" (s0), [t0] "+m" (t0),
1610 [s1] "+m" (s1), [t1] "+m" (t1)
1611 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4");
1613 sb0[30+i*2] = shft4(t0) + shft13(s0) * wp[24];
1614 sb0[31+i*2] = shft13(t0) * wp[25] - shft13(s0) * wp[26];
1615 sb1[30+i*2] = shft4(t1) + shft13(s1) * wp[24];
1616 sb1[31+i*2] = shft13(t1) * wp[25] - shft13(s1) * wp[26];
1617 wp += 27;
1618 x1 -= 2;
1619 x2 += 2;
1622 asm volatile(
1623 "move.l ( -32*4,%[x1]), %%d4\n" /* d4 = x1[-32] */
1624 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */
1626 "mac.w %%d0u, %%d4u, %%acc0\n"
1627 "mac.w %%d0u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n"
1628 "mac.w %%d0l, %%d4u, %%acc0\n"
1629 "mac.w %%d0l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n"
1630 "mac.w %%d1u, %%d4u, %%acc0\n"
1631 "mac.w %%d1u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n"
1632 "mac.w %%d1l, %%d4u, %%acc0\n"
1633 "mac.w %%d1l, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n"
1634 "mac.w %%d2u, %%d4u, %%acc0\n"
1635 "mac.w %%d2u, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n"
1636 "mac.w %%d2l, %%d4u, %%acc0\n"
1637 "mac.w %%d2l, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n"
1638 "mac.w %%d3u, %%d4u, %%acc0\n"
1639 "mac.w %%d3u, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n"
1640 "mac.w %%d3l, %%d4u, %%acc0\n"
1641 "mac.w %%d3l, %%d4l, ( -16*4,%[x1]), %%d4, %%acc1\n"
1642 "movclr.l %%acc0, %%d0\n"
1643 "move.l %%d0, %[s0]\n"
1644 "movclr.l %%acc1, %%d0\n"
1645 "move.l %%d0, %[s1]\n"
1647 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */
1648 "mac.w %%d0u, %%d4u, %%acc0\n"
1649 "mac.w %%d0u, %%d4l, ( -48*4,%[x1]), %%d4, %%acc1\n"
1650 "mac.w %%d1u, %%d4u, %%acc0\n"
1651 "mac.w %%d1u, %%d4l, ( 16*4,%[x1]), %%d4, %%acc1\n"
1652 "mac.w %%d1l, %%d4u, %%acc0\n"
1653 "mac.w %%d1l, %%d4l, ( -80*4,%[x1]), %%d4, %%acc1\n"
1654 "mac.w %%d2u, %%d4u, %%acc0\n"
1655 "mac.w %%d2u, %%d4l, ( 48*4,%[x1]), %%d4, %%acc1\n"
1656 "mac.w %%d2u, %%d4u, %%acc0\n"
1657 "mac.w %%d2u, %%d4l, (-112*4,%[x1]), %%d4, %%acc1\n"
1658 "mac.w %%d3u, %%d4u, %%acc0\n"
1659 "mac.w %%d3u, %%d4l, ( 80*4,%[x1]), %%d4, %%acc1\n"
1660 "mac.w %%d3l, %%d4u, %%acc0\n"
1661 "mac.w %%d3l, %%d4l, (-144*4,%[x1]), %%d4, %%acc1\n"
1662 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */
1663 "mac.w %%d0u, %%d4u, %%acc0\n"
1664 "mac.w %%d0u, %%d4l, ( 112*4,%[x1]), %%d4, %%acc1\n"
1665 "mac.w %%d0u, %%d4u, %%acc0\n"
1666 "mac.w %%d0u, %%d4l, (-176*4,%[x1]), %%d4, %%acc1\n"
1667 "mac.w %%d1u, %%d4u, %%acc0\n"
1668 "mac.w %%d1u, %%d4l, ( 144*4,%[x1]), %%d4, %%acc1\n"
1669 "mac.w %%d1l, %%d4u, %%acc0\n"
1670 "mac.w %%d1l, %%d4l, (-208*4,%[x1]), %%d4, %%acc1\n"
1671 "mac.w %%d2u, %%d4u, %%acc0\n"
1672 "mac.w %%d2u, %%d4l, ( 176*4,%[x1]), %%d4, %%acc1\n"
1673 "mac.w %%d2u, %%d4u, %%acc0\n"
1674 "mac.w %%d2u, %%d4l, (-240*4,%[x1]), %%d4, %%acc1\n"
1675 "mac.w %%d3u, %%d4u, %%acc0\n"
1676 "mac.w %%d3u, %%d4l, ( 208*4,%[x1]), %%d4, %%acc1\n"
1677 "mac.w %%d3l, %%d4u, %%acc0\n"
1678 "mac.w %%d3l, %%d4l, %%acc1\n"
1679 "movclr.l %%acc0, %%d0\n"
1680 "move.l %%d0, %[t0]\n"
1681 "movclr.l %%acc1, %%d0\n"
1682 "move.l %%d0, %[t1]\n"
1684 : [x1] "+a" (x1), [s0] "+m" (s0), [t0] "+m" (t0),
1685 [s1] "+m" (s1), [t1] "+m" (t1)
1686 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4");
1688 u = shft4(s0 - t0);
1689 v = shft4(s0 + t0);
1690 t0 = sb0[14];
1691 s0 = sb0[15] - t0;
1693 sb0[31] = v + t0; /* A0 */
1694 sb0[30] = u + s0; /* A1 */
1695 sb0[15] = u - s0; /* A2 */
1696 sb0[14] = v - t0; /* A3 */
1698 u = shft4(s1 - t1);
1699 v = shft4(s1 + t1);
1700 t1 = sb1[14];
1701 s1 = sb1[15] - t1;
1703 sb1[31] = v + t1; /* A0 */
1704 sb1[30] = u + s1; /* A1 */
1705 sb1[15] = u - s1; /* A2 */
1706 sb1[14] = v - t1; /* A3 */
1708 #else
1709 int ch, s, t, *a;
1711 for(ch=0; ch<cfg.channels; ch++)
1713 a = ch ? sb1 : sb0;
1714 for(k=0; k<18; k++, wk+=64, a+=SBLIMIT)
1716 wp = enwindow;
1717 x1 = wk;
1718 x2 = x1 - 124;
1720 /* x1[-572] .... x1[448] = 1022 */
1721 /* 18*4*16*32 */
1722 for(i=-15; i<0; i++)
1724 s = (int)x2[-224*2] * wp[ 0]; t = (int)x1[ 224*2] * wp[ 0];
1725 s += (int)x2[-160*2] * wp[ 1]; t += (int)x1[ 160*2] * wp[ 1];
1726 s += (int)x2[- 96*2] * wp[ 2]; t += (int)x1[ 96*2] * wp[ 2];
1727 s += (int)x2[- 32*2] * wp[ 3]; t += (int)x1[ 32*2] * wp[ 3];
1728 s += (int)x2[ 32*2] * wp[ 4]; t += (int)x1[- 32*2] * wp[ 4];
1729 s += (int)x2[ 96*2] * wp[ 5]; t += (int)x1[- 96*2] * wp[ 5];
1730 s += (int)x2[ 160*2] * wp[ 6]; t += (int)x1[-160*2] * wp[ 6];
1731 s += (int)x2[ 224*2] * wp[ 7]; t += (int)x1[-224*2] * wp[ 7];
1732 s += (int)x1[-256*2] * wp[ 8]; t += (int)x2[ 256*2] * wp[16];
1733 s += (int)x1[-192*2] * wp[ 9]; t += (int)x2[ 192*2] * wp[17];
1734 s += (int)x1[-128*2] * wp[10]; t += (int)x2[ 128*2] * wp[18];
1735 s += (int)x1[- 64*2] * wp[11]; t += (int)x2[ 64*2] * wp[19];
1736 s += (int)x1[ 0*2] * wp[12]; t += (int)x2[ 0*2] * wp[20];
1737 s += (int)x1[ 64*2] * wp[13]; t += (int)x2[- 64*2] * wp[21];
1738 s += (int)x1[ 128*2] * wp[14]; t += (int)x2[-128*2] * wp[22];
1739 s += (int)x1[ 192*2] * wp[15]; t += (int)x2[-192*2] * wp[23];
1741 a[30+i*2] = shft4(t) + shft13(s) * wp[24];
1742 a[31+i*2] = shft13(t) * wp[25] - shft13(s) * wp[26];
1743 wp += 27;
1744 x1 -= 2;
1745 x2 += 2;
1748 t = (int)x1[- 16*2] * wp[ 8]; s = (int)x1[ -32*2] * wp[0];
1749 t += ((int)x1[- 48*2]-x1[ 16*2]) * wp[10]; s += (int)x1[ -96*2] * wp[1];
1750 t += ((int)x1[- 80*2]+x1[ 48*2]) * wp[12]; s += (int)x1[-160*2] * wp[2];
1751 t += ((int)x1[-112*2]-x1[ 80*2]) * wp[14]; s += (int)x1[-224*2] * wp[3];
1752 t += ((int)x1[-144*2]+x1[112*2]) * wp[16]; s += (int)x1[ 32*2] * wp[4];
1753 t += ((int)x1[-176*2]-x1[144*2]) * wp[18]; s += (int)x1[ 96*2] * wp[5];
1754 t += ((int)x1[-208*2]+x1[176*2]) * wp[20]; s += (int)x1[ 160*2] * wp[6];
1755 t += ((int)x1[-240*2]-x1[208*2]) * wp[22]; s += (int)x1[ 224*2] * wp[7];
1757 u = shft4(s - t);
1758 v = shft4(s + t);
1759 t = a[14];
1760 s = a[15] - t;
1762 a[31] = v + t; /* A0 */
1763 a[30] = u + s; /* A1 */
1764 a[15] = u - s; /* A2 */
1765 a[14] = v - t; /* A3 */
1767 wk -= 18 * 64 - 1; /* rewind wk (to next channel start) */
1769 #endif
1772 void window_subband2(short *x1, int a[SBLIMIT]) ICODE_ATTR;
1773 void window_subband2(short *x1, int a[SBLIMIT])
1775 int xr;
1776 short *wp = enwindow;
1777 short *x2 = x1 - 124;
1779 wp += 27 * 15;
1780 x1 -= 2 * 15;
1781 x2 += 2 * 15;
1783 xr = a[28] - a[0]; a[0] += a[28]; a[28] = shft9(xr) * wp[-2*27+25];
1784 xr = a[29] - a[1]; a[1] += a[29]; a[29] = shft9(xr) * wp[-2*27+25];
1785 xr = a[26] - a[2]; a[2] += a[26]; a[26] = shft9(xr) * wp[-4*27+25];
1786 xr = a[27] - a[3]; a[3] += a[27]; a[27] = shft9(xr) * wp[-4*27+25];
1787 xr = a[24] - a[4]; a[4] += a[24]; a[24] = shft9(xr) * wp[-6*27+25];
1788 xr = a[25] - a[5]; a[5] += a[25]; a[25] = shft9(xr) * wp[-6*27+25];
1789 xr = a[22] - a[6]; a[6] += a[22]; a[22] = shft9(xr) * SQRT ;
1790 xr = a[23] - a[7]; a[7] += a[23]; a[23] = shft9(xr) * SQRT - a[7];
1791 a[ 7] -= a[ 6];
1792 a[22] -= a[ 7];
1793 a[23] -= a[22];
1795 xr = a[ 6]; a[ 6] = a[31] - xr; a[31] = a[31] + xr;
1796 xr = a[ 7]; a[ 7] = a[30] - xr; a[30] = a[30] + xr;
1797 xr = a[22]; a[22] = a[15] - xr; a[15] = a[15] + xr;
1798 xr = a[23]; a[23] = a[14] - xr; a[14] = a[14] + xr;
1800 xr = a[20] - a[ 8]; a[ 8] += a[20]; a[20] = shft9(xr) * wp[-10*27+25];
1801 xr = a[21] - a[ 9]; a[ 9] += a[21]; a[21] = shft9(xr) * wp[-10*27+25];
1802 xr = a[18] - a[10]; a[10] += a[18]; a[18] = shft9(xr) * wp[-12*27+25];
1803 xr = a[19] - a[11]; a[11] += a[19]; a[19] = shft9(xr) * wp[-12*27+25];
1804 xr = a[16] - a[12]; a[12] += a[16]; a[16] = shft9(xr) * wp[-14*27+25];
1805 xr = a[17] - a[13]; a[13] += a[17]; a[17] = shft9(xr) * wp[-14*27+25];
1806 xr =-a[20] + a[24]; a[20] += a[24]; a[24] = shft9(xr) * wp[-12*27+25];
1807 xr =-a[21] + a[25]; a[21] += a[25]; a[25] = shft9(xr) * wp[-12*27+25];
1808 xr = a[ 4] - a[ 8]; a[ 4] += a[ 8]; a[ 8] = shft9(xr) * wp[-12*27+25];
1809 xr = a[ 5] - a[ 9]; a[ 5] += a[ 9]; a[ 9] = shft9(xr) * wp[-12*27+25];
1810 xr = a[ 0] - a[12]; a[ 0] += a[12]; a[12] = shft9(xr) * wp[ -4*27+25];
1811 xr = a[ 1] - a[13]; a[ 1] += a[13]; a[13] = shft9(xr) * wp[ -4*27+25];
1812 xr = a[16] - a[28]; a[16] += a[28]; a[28] = shft9(xr) * wp[ -4*27+25];
1813 xr =-a[17] + a[29]; a[17] += a[29]; a[29] = shft9(xr) * wp[ -4*27+25];
1815 xr = SQRT * shft9(a[ 2] - a[10]); a[ 2] += a[10]; a[10] = xr;
1816 xr = SQRT * shft9(a[ 3] - a[11]); a[ 3] += a[11]; a[11] = xr;
1817 xr = SQRT * shft9(a[26] - a[18]); a[18] += a[26]; a[26] = xr - a[18];
1818 xr = SQRT * shft9(a[27] - a[19]); a[19] += a[27]; a[27] = xr - a[19];
1820 xr = a[ 2]; a[19] -= a[ 3]; a[ 3] -= xr; a[ 2] = a[31] - xr; a[31] += xr;
1821 xr = a[ 3]; a[11] -= a[19]; a[18] -= xr; a[ 3] = a[30] - xr; a[30] += xr;
1822 xr = a[18]; a[27] -= a[11]; a[19] -= xr; a[18] = a[15] - xr; a[15] += xr;
1824 xr = a[19]; a[10] -= xr; a[19] = a[14] - xr; a[14] += xr;
1825 xr = a[10]; a[11] -= xr; a[10] = a[23] - xr; a[23] += xr;
1826 xr = a[11]; a[26] -= xr; a[11] = a[22] - xr; a[22] += xr;
1827 xr = a[26]; a[27] -= xr; a[26] = a[ 7] - xr; a[ 7] += xr;
1829 xr = a[27]; a[27] = a[6] - xr; a[6] += xr;
1831 xr = SQRT * shft9(a[ 0] - a[ 4]); a[ 0] += a[ 4]; a[ 4] = xr;
1832 xr = SQRT * shft9(a[ 1] - a[ 5]); a[ 1] += a[ 5]; a[ 5] = xr;
1833 xr = SQRT * shft9(a[16] - a[20]); a[16] += a[20]; a[20] = xr;
1834 xr = SQRT * shft9(a[17] - a[21]); a[17] += a[21]; a[21] = xr;
1835 xr =-SQRT * shft9(a[ 8] - a[12]); a[ 8] += a[12]; a[12] = xr - a[ 8];
1836 xr =-SQRT * shft9(a[ 9] - a[13]); a[ 9] += a[13]; a[13] = xr - a[ 9];
1837 xr =-SQRT * shft9(a[25] - a[29]); a[25] += a[29]; a[29] = xr - a[25];
1838 xr =-SQRT * shft9(a[24] + a[28]); a[24] -= a[28]; a[28] = xr - a[24];
1840 xr = a[24] - a[16]; a[24] = xr;
1841 xr = a[20] - xr; a[20] = xr;
1842 xr = a[28] - xr; a[28] = xr;
1844 xr = a[25] - a[17]; a[25] = xr;
1845 xr = a[21] - xr; a[21] = xr;
1846 xr = a[29] - xr; a[29] = xr;
1848 xr = a[17] - a[1]; a[17] = xr;
1849 xr = a[ 9] - xr; a[ 9] = xr;
1850 xr = a[25] - xr; a[25] = xr;
1851 xr = a[ 5] - xr; a[ 5] = xr;
1852 xr = a[21] - xr; a[21] = xr;
1853 xr = a[13] - xr; a[13] = xr;
1854 xr = a[29] - xr; a[29] = xr;
1856 xr = a[ 1] - a[0]; a[ 1] = xr;
1857 xr = a[16] - xr; a[16] = xr;
1858 xr = a[17] - xr; a[17] = xr;
1859 xr = a[ 8] - xr; a[ 8] = xr;
1860 xr = a[ 9] - xr; a[ 9] = xr;
1861 xr = a[24] - xr; a[24] = xr;
1862 xr = a[25] - xr; a[25] = xr;
1863 xr = a[ 4] - xr; a[ 4] = xr;
1864 xr = a[ 5] - xr; a[ 5] = xr;
1865 xr = a[20] - xr; a[20] = xr;
1866 xr = a[21] - xr; a[21] = xr;
1867 xr = a[12] - xr; a[12] = xr;
1868 xr = a[13] - xr; a[13] = xr;
1869 xr = a[28] - xr; a[28] = xr;
1870 xr = a[29] - xr; a[29] = xr;
1872 xr = a[ 0]; a[ 0] += a[31]; a[31] -= xr;
1873 xr = a[ 1]; a[ 1] += a[30]; a[30] -= xr;
1874 xr = a[16]; a[16] += a[15]; a[15] -= xr;
1875 xr = a[17]; a[17] += a[14]; a[14] -= xr;
1876 xr = a[ 8]; a[ 8] += a[23]; a[23] -= xr;
1877 xr = a[ 9]; a[ 9] += a[22]; a[22] -= xr;
1878 xr = a[24]; a[24] += a[ 7]; a[ 7] -= xr;
1879 xr = a[25]; a[25] += a[ 6]; a[ 6] -= xr;
1880 xr = a[ 4]; a[ 4] += a[27]; a[27] -= xr;
1881 xr = a[ 5]; a[ 5] += a[26]; a[26] -= xr;
1882 xr = a[20]; a[20] += a[11]; a[11] -= xr;
1883 xr = a[21]; a[21] += a[10]; a[10] -= xr;
1884 xr = a[12]; a[12] += a[19]; a[19] -= xr;
1885 xr = a[13]; a[13] += a[18]; a[18] -= xr;
1886 xr = a[28]; a[28] += a[ 3]; a[ 3] -= xr;
1887 xr = a[29]; a[29] += a[ 2]; a[ 2] -= xr;
1890 void mdct_long(int *out, int *in) ICODE_ATTR;
1891 void mdct_long(int *out, int *in)
1893 int ct,st;
1894 int tc1, tc2, tc3, tc4, ts5, ts6, ts7, ts8;
1895 int ts1, ts2, ts3, ts4, tc5, tc6, tc7, tc8;
1897 /* 1,2, 5,6, 9,10, 13,14, 17 */
1898 tc1 = in[17] - in[ 9];
1899 tc3 = in[15] - in[11];
1900 tc4 = in[14] - in[12];
1901 ts5 = in[ 0] + in[ 8];
1902 ts6 = in[ 1] + in[ 7];
1903 ts7 = in[ 2] + in[ 6];
1904 ts8 = in[ 3] + in[ 5];
1906 out[17] = (ts5 + ts7 - ts8) * cx[8] - (ts6 - in[4]) * cx[8];
1907 st = (ts5 + ts7 - ts8) * cx[7] + (ts6 - in[4]) * cx[8];
1908 ct = (tc1 - tc3 - tc4) * cx[6];
1909 out[5] = ct + st;
1910 out[6] = ct - st;
1912 tc2 = (in[16] - in[10]) * cx[6];
1913 ts6 = ts6 * cx[7] + in[4] * cx[8];
1915 ct = tc1 * cx[0] + tc2 + tc3 * cx[1] + tc4 * cx[2];
1916 st = -ts5 * cx[4] + ts6 - ts7 * cx[5] + ts8 * cx[3];
1917 out[1] = ct + st;
1918 out[2] = ct - st;
1920 ct = tc1 * cx[1] - tc2 - tc3 * cx[2] + tc4 * cx[0];
1921 st = -ts5 * cx[5] + ts6 - ts7 * cx[3] + ts8 * cx[4];
1922 out[ 9] = ct + st;
1923 out[10] = ct - st;
1925 ct = tc1 * cx[2] - tc2 + tc3 * cx[0] - tc4 * cx[1];
1926 st = ts5 * cx[3] - ts6 + ts7 * cx[4] - ts8 * cx[5];
1927 out[13] = ct + st;
1928 out[14] = ct - st;
1930 ts1 = in[ 8] - in[ 0];
1931 ts3 = in[ 6] - in[ 2];
1932 ts4 = in[ 5] - in[ 3];
1933 tc5 = in[17] + in[ 9];
1934 tc6 = in[16] + in[10];
1935 tc7 = in[15] + in[11];
1936 tc8 = in[14] + in[12];
1938 out[0] = (tc5 + tc7 + tc8) * cx[8] + (tc6 + in[13]) * cx[8];
1939 ct = (tc5 + tc7 + tc8) * cx[7] - (tc6 + in[13]) * cx[8];
1940 st = (ts1 - ts3 + ts4) * cx[6];
1941 out[11] = ct + st;
1942 out[12] = ct - st;
1944 ts2 = (in[7] - in[1]) * cx[6];
1945 tc6 = in[13] * cx[8] - tc6 * cx[7];
1947 ct = tc5 * cx[3] - tc6 + tc7 * cx[4] + tc8 * cx[5];
1948 st = ts1 * cx[2] + ts2 + ts3 * cx[0] + ts4 * cx[1];
1949 out[3] = ct + st;
1950 out[4] = ct - st;
1952 ct =-tc5 * cx[5] + tc6 - tc7 * cx[3] - tc8 * cx[4];
1953 st = ts1 * cx[1] + ts2 - ts3 * cx[2] - ts4 * cx[0];
1954 out[7] = ct + st;
1955 out[8] = ct - st;
1957 ct =-tc5 * cx[4] + tc6 - tc7 * cx[5] - tc8 * cx[3];
1958 st = ts1 * cx[0] - ts2 + ts3 * cx[1] - ts4 * cx[2];
1959 out[15] = ct + st;
1960 out[16] = ct - st;
1963 static int find_bitrate_index(int type, int bitrate)
1965 int i;
1967 for(i=0;i<14;i++)
1968 if(bitrate == bitr_index[type][i])
1969 break;
1971 return i;
1974 static int find_samplerate_index(long freq, int *mp3_type)
1975 { /* MPEG 2 */ /* MPEG 1 */
1976 static long mpeg[2][3] = { {22050, 24000, 16000}, {44100, 48000, 32000} };
1977 int mpg, rate;
1979 /* set default values: MPEG1 at 44100/s */
1980 *mp3_type = 1;
1982 for(mpg=0; mpg<2; mpg++)
1983 for(rate=0; rate<3; rate++)
1984 if(freq == mpeg[mpg][rate])
1985 { *mp3_type = mpg; return rate; }
1987 return 0;
1990 void init_mp3_encoder_engine(bool stereo, int bitrate, int sample_rate)
1992 uint32 avg_byte_per_frame;
1994 #ifdef ROCKBOX_LITTLE_ENDIAN
1995 cfg.byte_order = order_littleEndian;
1996 #else
1997 cfg.byte_order = order_bigEndian;
1998 #endif
2000 if(bitrate < 96 && stereo && sample_rate >= 32000)
2001 { /* use MPEG2 format */
2002 sample_rate >>= 1;
2003 cfg.resample = 1;
2004 cfg.granules = 1;
2006 else
2007 { /* use MPEG1 format */
2008 cfg.resample = 0;
2009 cfg.granules = 2;
2012 cfg.samplerate = sample_rate;
2013 cfg.channels = stereo ? 2 : 1;
2014 cfg.mpg.mode = stereo ? 0 : 3; /* 0=stereo, 3=mono */
2015 cfg.mpg.bitrate = stereo ? bitrate : bitrate > 160 ? 160 : bitrate;
2016 cfg.mpg.smpl_id = find_samplerate_index(cfg.samplerate, &cfg.mpg.type);
2017 cfg.mpg.bitr_id = find_bitrate_index(cfg.mpg.type, cfg.mpg.bitrate);
2018 cfg.mpg.num_bands = num_bands[stereo ? cfg.mpg.type : 2][cfg.mpg.bitr_id];
2020 memcpy(scalefac, sfBand[cfg.mpg.smpl_id + 3*cfg.mpg.type], sizeof(scalefac));
2021 memset(mfbuf , 0 , sizeof(mfbuf ));
2022 memset(mdct_freq , 0 , sizeof(mdct_freq ));
2023 memset(enc_data , 0 , sizeof(enc_data ));
2024 memset(sb_data , 0 , sizeof(sb_data ));
2025 memset(&CodedData, 0 , sizeof(CodedData ));
2026 memcpy(ca , ca_const , sizeof(ca ));
2027 memcpy(cs , cs_const , sizeof(cs ));
2028 memcpy(cx , cx_const , sizeof(cx ));
2029 memcpy(win , win_const , sizeof(win ));
2030 memcpy(enwindow , enwindow_const , sizeof(enwindow ));
2031 memcpy(int2idx , int2idx_const , sizeof(int2idx ));
2032 memcpy(ht_count , ht_count_const , sizeof(ht_count ));
2033 memcpy( tab01 , tab01_const , sizeof(tab01 ));
2034 memcpy( tab23 , tab23_const , sizeof(tab23 ));
2035 memcpy( tab56 , tab56_const , sizeof(tab56 ));
2036 memcpy( tab1315 , tab1315_const , sizeof(tab1315 ));
2037 memcpy( tab1624 , tab1624_const , sizeof(tab1624 ));
2038 memcpy( tab789 , tab789_const , sizeof(tab789 ));
2039 memcpy( tabABC , tabABC_const , sizeof(tabABC ));
2040 memcpy( t1HB , t1HB_const , sizeof(t1HB ));
2041 memcpy( t2HB , t2HB_const , sizeof(t2HB ));
2042 memcpy( t3HB , t3HB_const , sizeof(t3HB ));
2043 memcpy( t5HB , t5HB_const , sizeof(t5HB ));
2044 memcpy( t6HB , t6HB_const , sizeof(t6HB ));
2045 memcpy( t7HB , t7HB_const , sizeof(t7HB ));
2046 memcpy( t8HB , t8HB_const , sizeof(t8HB ));
2047 memcpy( t9HB , t9HB_const , sizeof(t9HB ));
2048 memcpy(t10HB , t10HB_const , sizeof(t10HB ));
2049 memcpy(t11HB , t11HB_const , sizeof(t11HB ));
2050 memcpy(t12HB , t12HB_const , sizeof(t12HB ));
2051 memcpy(t13HB , t13HB_const , sizeof(t13HB ));
2052 memcpy(t15HB , t15HB_const , sizeof(t15HB ));
2053 memcpy(t16HB , t16HB_const , sizeof(t16HB ));
2054 memcpy(t24HB , t24HB_const , sizeof(t24HB ));
2055 memcpy( t1l , t1l_const , sizeof(t1l ));
2056 memcpy( t2l , t2l_const , sizeof(t2l ));
2057 memcpy( t3l , t3l_const , sizeof(t3l ));
2058 memcpy( t5l , t5l_const , sizeof(t5l ));
2059 memcpy( t6l , t6l_const , sizeof(t6l ));
2060 memcpy( t7l , t7l_const , sizeof(t7l ));
2061 memcpy( t8l , t8l_const , sizeof(t8l ));
2062 memcpy( t9l , t9l_const , sizeof(t9l ));
2063 memcpy(t10l , t10l_const , sizeof(t10l ));
2064 memcpy(t11l , t11l_const , sizeof(t11l ));
2065 memcpy(t12l , t12l_const , sizeof(t12l ));
2066 memcpy(t13l , t13l_const , sizeof(t13l ));
2067 memcpy(t15l , t15l_const , sizeof(t15l ));
2068 memcpy(t16l , t16l_const , sizeof(t16l ));
2069 memcpy(t24l , t24l_const , sizeof(t24l ));
2070 memcpy(ht , ht_const , sizeof(ht ));
2072 ht[ 0].table = NULL; ht[ 0].hlen = NULL; /* Apparently not used */
2073 ht[ 1].table = t1HB; ht[ 1].hlen = t1l;
2074 ht[ 2].table = t2HB; ht[ 2].hlen = t2l;
2075 ht[ 3].table = t3HB; ht[ 3].hlen = t3l;
2076 ht[ 4].table = NULL; ht[ 4].hlen = NULL; /* Apparently not used */
2077 ht[ 5].table = t5HB; ht[ 5].hlen = t5l;
2078 ht[ 6].table = t6HB; ht[ 6].hlen = t6l;
2079 ht[ 7].table = t7HB; ht[ 7].hlen = t7l;
2080 ht[ 8].table = t8HB; ht[ 8].hlen = t8l;
2081 ht[ 9].table = t9HB; ht[ 9].hlen = t9l;
2082 ht[10].table = t10HB; ht[10].hlen = t10l;
2083 ht[11].table = t11HB; ht[11].hlen = t11l;
2084 ht[12].table = t12HB; ht[12].hlen = t12l;
2085 ht[13].table = t13HB; ht[13].hlen = t13l;
2086 ht[14].table = NULL; ht[14].hlen = NULL; /* Apparently not used */
2087 ht[15].table = t15HB; ht[15].hlen = t15l;
2089 /* Figure average number of 'bytes' per frame */
2090 avg_byte_per_frame = SAMPL2 * 16000 * cfg.mpg.bitrate / (2 - cfg.mpg.type);
2091 avg_byte_per_frame = avg_byte_per_frame / cfg.samplerate;
2092 cfg.byte_per_frame = avg_byte_per_frame / 64;
2093 cfg.frac_per_frame = avg_byte_per_frame & 63;
2094 cfg.slot_lag = 0;
2095 cfg.sideinfo_len = 32 + (cfg.mpg.type ? (cfg.channels == 1 ? 136 : 256)
2096 : (cfg.channels == 1 ? 72 : 136));
2099 void compress(void)
2101 int i, ii, gr, k, ch, shift, gr_cnt;
2102 int max, min;
2103 char stg[20];
2105 while(1)
2107 if((frames & 7) == 0)
2108 { rb->lcd_clear_display();
2109 rb->snprintf(stg, 20, "Frame %d / %d", frames, wav_size/SAMPL2/8);
2110 rb->lcd_putsxy(4, 20, stg);
2111 rb->lcd_update();
2113 /* encode one mp3 frame in this loop */
2114 memset(CodedData.bbuf, 0, sizeof(CodedData.bbuf));
2116 if((cfg.slot_lag += cfg.frac_per_frame) >= 64)
2117 { /* Padding for this frame */
2118 cfg.slot_lag -= 64;
2119 cfg.mpg.padding = 1;
2121 else
2122 cfg.mpg.padding = 0;
2124 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding
2125 - cfg.sideinfo_len) / cfg.granules / cfg.channels;
2127 /* shift out old samples */
2128 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512);
2130 /* read new samples to iram for further processing */
2131 if(read_samples((uint32*)(mfbuf + 2*512), SAMP_PER_FRAME) == 0)
2132 break;
2134 /* swap bytes if neccessary */
2135 if(cfg.byte_order == order_bigEndian)
2136 for(i=0; i<SAMP_PER_FRAME; i++)
2138 uint32 t = ((uint32*)mfbuf)[512 + i];
2139 t = ((t >> 8) & 0xff00ff) | ((t << 8) & 0xff00ff00);
2140 ((uint32*)mfbuf)[512 + i] = t;
2143 if(cfg.resample) /* downsample to half of original */
2144 for(i=2*512; i<2*512+2*SAMP_PER_FRAME; i+=4)
2146 mfbuf[i/2+512] = (short)(((int)mfbuf[i+0] + mfbuf[i+2]) >> 1);
2147 mfbuf[i/2+513] = (short)(((int)mfbuf[i+1] + mfbuf[i+3]) >> 1);
2150 if(cfg.channels == 1) /* mix left and right channels to mono */
2151 for(i=2*512; i<2*512+2*SAMP_PER_FRAME; i+=2)
2152 mfbuf[i] = mfbuf[i+1] = (short)(((int)mfbuf[i] + mfbuf[i+1]) >> 1);
2154 cfg.ResvSize = 0;
2155 gr_cnt = cfg.granules * cfg.channels;
2156 CodedData.bitpos = cfg.sideinfo_len; /* leave space for mp3 header */
2157 for(gr=0; gr<cfg.granules; gr++)
2159 short *wk = mfbuf + 2*286 + gr*1152;
2161 /* 16bit packed wav data can be windowed efficiently on coldfire */
2162 window_subband1(wk, sb_data[0][1-gr][0], sb_data[1][1-gr][0]);
2164 for(ch=0; ch<cfg.channels; ch++)
2166 int band;
2167 int *mdct;
2169 wk = mfbuf + 2*286 + gr*1152 + ch;
2171 /* 36864=4*18*16*32 */
2172 for(k=0; k<18; k++, wk+=64)
2174 window_subband2(wk, sb_data[ch][1-gr][k]);
2175 /* Compensate for inversion in the analysis filter */
2176 if(k & 1)
2177 for(band=1; band<32; band+=2)
2178 sb_data[ch][1-gr][k][band] *= -1;
2181 /* Perform imdct of 18 previous + 18 current subband samples */
2182 /* for integer precision do this loop twice (if neccessary) */
2183 shift = k = 14;
2184 for(ii=0; ii<2 && k; ii++)
2186 mdct = mdct_freq;
2187 cfg.cod_info[gr][ch].additStep = 4 * (14 - shift);
2188 for(band=0; band<cfg.mpg.num_bands; band++, mdct+=18)
2190 int *band0 = sb_data[ch][ gr][0] + order[band];
2191 int *band1 = sb_data[ch][1-gr][0] + order[band];
2192 int work[18];
2194 /* 9216=4*32*9*8 */
2195 for(k=-9; k<0; k++)
2197 int a = shft_n(band1[(k+9)*32], shift);
2198 int b = shft_n(band1[(8-k)*32], shift);
2199 int c = shft_n(band0[(k+9)*32], shift);
2200 int d = shft_n(band0[(8-k)*32], shift);
2202 work[k+ 9] = shft16(a * win[k+ 9][0] + b * win[k+ 9][1]
2203 + c * win[k+ 9][2] + d * win[k+ 9][3]);
2205 work[k+18] = shft16(c * win[k+18][0] + d * win[k+18][1]
2206 + a * win[k+18][2] + b * win[k+18][3]);
2209 /* 7200=4*18*100 */
2210 mdct_long(mdct, work);
2212 /* Perform aliasing reduction butterfly */
2213 if(band != 0)
2214 for(k=7; k>=0; --k)
2216 int bu, bd;
2217 bu = shft15(mdct[k]) * ca[k] + shft15(mdct[-1-k]) * cs[k];
2218 bd = shft15(mdct[k]) * cs[k] - shft15(mdct[-1-k]) * ca[k];
2219 mdct[-1-k] = bu;
2220 mdct[ k ] = bd;
2224 max = min = 0;
2225 for(k=0; k<576; k++)
2227 mdct_freq[k] = shft13(mdct_freq[k]);
2228 if(max < mdct_freq[k]) max = mdct_freq[k];
2229 if(min > mdct_freq[k]) min = mdct_freq[k];
2232 max = (max > -min) ? max : -min;
2233 cfg.cod_info[gr][ch].max_val = (long)max;
2235 /* calc new shift for higher integer precision */
2236 for(k=0; max<(0x3c00>>k); k++);
2237 shift = 12 - k;
2240 cfg.cod_info[gr][ch].quantStep += cfg.cod_info[gr][ch].additStep;
2242 /* bit and noise allocation */
2243 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch], gr_cnt--);
2244 /* write the frame to the bitstream */
2245 Huffmancodebits(enc_data, mdct_freq, &cfg.cod_info[gr][ch]);
2247 cfg.cod_info[gr][ch].quantStep -= cfg.cod_info[gr][ch].additStep;
2249 if(cfg.granules == 1)
2250 memcpy(sb_data[ch][0], sb_data[ch][1], sizeof(sb_data[ch][0]));
2254 enc_size = (CodedData.bitpos + 7) >> 3;
2255 /* finish this chunk by adding sideinfo header data */
2256 CodedData.bitpos = 0;
2257 encodeSideInfo( cfg.cod_info );
2259 if(cfg.byte_order != order_bigEndian)
2260 for(i=0; i<(enc_size+3)/4; i++)
2261 CodedData.bbuf[i] = myswap32(CodedData.bbuf[i]);
2263 if(enc_chunk + enc_size > 65536)
2265 /* copy iram mp3 buffer to sdram/file */
2266 rb->write(mp3file, enc_buffer, enc_chunk & ~3);
2267 memcpy(enc_buffer, enc_buffer + (enc_chunk >> 2), enc_chunk & 3);
2268 enc_chunk &= 3;
2271 memcpy((char*)enc_buffer + enc_chunk, CodedData.bbuf, enc_size);
2272 enc_chunk += enc_size;
2273 frames++;
2275 /* write last chunks to disk */
2276 rb->write(mp3file, enc_buffer, enc_chunk);
2280 int num_file;
2281 char filename[12][80];
2282 char mp3_name[80];
2284 void read_wav_files(char *dirname)
2286 DIR *dir = rb->opendir(dirname);
2288 if(!dir)
2289 return;
2291 while(true)
2293 struct dirent *entry;
2295 entry = rb->readdir(dir);
2296 if(!entry)
2297 break;
2299 if( !(entry->attribute & ATTR_DIRECTORY) )
2301 int slen = rb->strlen(entry->d_name);
2303 /* add all wav audio files */
2304 if(!rb->strcasecmp(entry->d_name + slen - 4, ".wav"))
2306 if(num_file >= 12)
2307 break;
2309 rb->strncpy(filename[num_file], dirname, 79);
2310 slen = rb->strlen(filename[num_file]);
2311 rb->strncpy(filename[num_file++] + slen, entry->d_name, 79);
2315 rb->closedir(dir);
2318 void get_mp3_filename(char *wav_name)
2320 int slen = rb->strlen(wav_name);
2321 rb->strncpy(mp3_name, wav_name, 79);
2322 rb->strncpy(mp3_name + slen - 4, ".mp3", 5);
2325 #if CONFIG_KEYPAD == IRIVER_H100_PAD || CONFIG_KEYPAD == IRIVER_H300_PAD
2326 #define MP3ENC_PREV BUTTON_UP
2327 #define MP3ENC_NEXT BUTTON_DOWN
2328 #define MP3ENC_DONE BUTTON_OFF
2329 #define MP3ENC_SELECT BUTTON_SELECT
2330 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
2331 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
2332 #define MP3ENC_PREV BUTTON_SCROLL_BACK
2333 #define MP3ENC_NEXT BUTTON_SCROLL_FWD
2334 #define MP3ENC_DONE BUTTON_MENU
2335 #define MP3ENC_SELECT BUTTON_SELECT
2336 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
2337 #define MP3ENC_PREV BUTTON_UP
2338 #define MP3ENC_NEXT BUTTON_DOWN
2339 #define MP3ENC_DONE BUTTON_POWER
2340 #define MP3ENC_SELECT BUTTON_SELECT
2341 #elif CONFIG_KEYPAD == GIGABEAT_PAD
2342 #define MP3ENC_PREV BUTTON_UP
2343 #define MP3ENC_NEXT BUTTON_DOWN
2344 #define MP3ENC_DONE BUTTON_POWER
2345 #define MP3ENC_SELECT BUTTON_SELECT
2346 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
2347 (CONFIG_KEYPAD == SANSA_C200_PAD)
2348 #define MP3ENC_PREV BUTTON_UP
2349 #define MP3ENC_NEXT BUTTON_DOWN
2350 #define MP3ENC_DONE BUTTON_POWER
2351 #define MP3ENC_SELECT BUTTON_SELECT
2352 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
2353 #define MP3ENC_PREV BUTTON_SCROLL_UP
2354 #define MP3ENC_NEXT BUTTON_SCROLL_DOWN
2355 #define MP3ENC_DONE BUTTON_POWER
2356 #define MP3ENC_SELECT BUTTON_PLAY
2357 #endif
2359 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
2361 int fil, sfil, nfil; /* for file selection */
2362 int rat, srat, nrat; /* for rate selection */
2363 int cont = 1, butt;
2364 long tim = 0;
2365 char stg[40];
2366 char* bstrg[] = {"64","80","96","112","128","160","192","224","256","320"};
2367 int brate[] = { 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 };
2369 (void)parameter;
2371 PLUGIN_IRAM_INIT(api)
2373 #ifdef CPU_COLDFIRE
2374 asm volatile ("move.l #0, %macsr"); /* integer mode */
2375 #endif
2377 rb = api;
2378 rb->lcd_setfont(FONT_SYSFIXED);
2380 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
2381 rb->cpu_boost(true);
2382 #endif
2383 rb->button_clear_queue();
2385 /* check 'viewer' or 'plugin' call */
2386 if(parameter == NULL || *(char*)parameter == 0)
2388 read_wav_files("/");
2389 read_wav_files(REC_BASE_DIR"/");
2391 nfil = num_file - 1;
2392 sfil = 0; /* set first file as default */
2394 while(cont && (butt = rb->button_get_w_tmo(HZ/10)) != MP3ENC_SELECT)
2396 switch(butt)
2398 case MP3ENC_DONE: cont = 0; break;
2399 case MP3ENC_PREV: if(sfil > 0 ) sfil--; break;
2400 case MP3ENC_NEXT: if(sfil < nfil) sfil++; break;
2403 rb->lcd_clear_display();
2404 rb->lcd_putsxy(2, 2, "-- Select WAV-File --");
2406 for(fil=0; fil<=nfil; fil++)
2407 rb->lcd_putsxy(2, 12 + fil*8, filename[fil]);
2409 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2410 rb->lcd_fillrect(0, 12 + sfil*8, 127, 8);
2411 rb->lcd_set_drawmode(DRMODE_SOLID);
2412 rb->lcd_update();
2415 else
2417 sfil = 0;
2418 rb->strncpy(filename[0], (char*)parameter, 79);
2421 nrat = 9;
2422 srat = 4; /* set 128kBit as default */
2424 while(cont && (butt = rb->button_get_w_tmo(HZ/10)) != MP3ENC_SELECT)
2426 switch(butt)
2428 case MP3ENC_DONE: cont = 0; break;
2429 case MP3ENC_PREV: if(srat > 0 ) srat--; break;
2430 case MP3ENC_NEXT: if(srat < nrat) srat++; break;
2433 rb->lcd_clear_display();
2434 rb->lcd_putsxy(2, 2, "-- Select Bitrate --");
2436 for(rat=0; rat<=nrat; rat++)
2437 rb->lcd_putsxy(2, 12 + rat*8, bstrg[rat]);
2438 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
2439 rb->lcd_fillrect(0, 12 + srat*8, 127, 8);
2440 rb->lcd_set_drawmode(DRMODE_SOLID);
2441 rb->lcd_update();
2444 wav_filename = filename[sfil];
2446 if(cont)
2448 if(wave_open() == 0)
2450 init_mp3_encoder_engine(true, brate[srat], cfg.samplerate);
2451 get_mp3_filename(wav_filename);
2452 mp3file = rb->open(mp3_name , O_WRONLY|O_CREAT|O_TRUNC);
2453 frames = 0;
2455 tim = *rb->current_tick;
2456 compress();
2457 tim = *rb->current_tick - tim;
2459 rb->close(wavfile);
2460 rb->close(mp3file);
2462 else
2464 rb->close(wavfile);
2465 rb->snprintf(stg, 20, "WaveOpen failed %d", wave_open());
2466 rb->lcd_putsxy(0, 20, stg);
2467 rb->lcd_update();
2468 rb->sleep(5*HZ);
2471 rb->lcd_clear_display();
2472 rb->snprintf(stg, 30, " Conversion: %ld.%02lds ", tim/100, tim%100);
2473 rb->lcd_putsxy(0, 30, stg);
2474 tim = frames * SAMP_PER_FRAME * 100 / 44100; /* unit=.01s */
2475 rb->snprintf(stg, 30, " WAV-Length: %ld.%02lds ", tim/100, tim%100);
2476 rb->lcd_putsxy(0, 20, stg);
2477 rb->lcd_update();
2478 rb->sleep(5*HZ);
2481 rb->lcd_setfont(FONT_UI);
2482 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
2483 rb->cpu_boost(false);
2484 #endif
2485 return PLUGIN_OK;