A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 2730 lines 107 kB view raw
1/* Shine is an MP3 encoder 2 * Copyright (C) 1999-2000 Gabriel Bouvigne 3 * 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. 8 * 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. */ 13 14#include <inttypes.h> 15#include "plugin.h" 16 17 18#define MAX_SAMP_PER_FRAME 1152 19#define SAMPL2 576 20#define SBLIMIT 32 21#define HTN 16 22#define putlong(c, s) if(s+sz <= 32) { cc = (cc << s) | c; sz+= s; } \ 23 else { putbits(cc, sz); cc = c; sz = s; } 24 25enum e_byte_order { order_unknown, order_bigEndian, order_littleEndian }; 26 27typedef struct { 28 int type; /* 0=(MPEG2 - 22.05,24,16kHz) 1=(MPEG1 - 44.1,48,32kHz) */ 29 int mode; /* 0=stereo, 1=jstereo, 2=dual, 3=mono */ 30 int padding; 31 long bitr_id; 32 int smpl_id; 33 uint16_t bitrate; 34 uint8_t num_bands; 35} mpeg_t; 36 37/* Side information */ 38typedef struct { 39 uint32_t part2_3_length; 40 int count1; /* number of 0-1-quadruples */ 41 uint32_t global_gain; 42 uint32_t table_select[4]; 43 uint32_t address1; 44 uint32_t address2; 45 uint32_t address3; 46 long quantStep; 47 long additStep; 48 uint32_t max_val; 49 uint8_t region_0_1; 50} side_info_t; 51 52typedef struct { 53 enum e_byte_order byte_order; 54 side_info_t cod_info[2][2]; 55 mpeg_t mpg; 56 long frac_per_frame; 57 long byte_per_frame; 58 long slot_lag; 59 int sideinfo_len; 60 int mean_bits; 61 int ResvSize; 62 int channels; 63 int granules; 64 int smpl_per_frm; 65 uint16_t samplerate; 66} config_t; 67 68typedef struct { 69 int bitpos; /* current bitpos for writing */ 70 uint32_t bbuf[263]; 71} BF_Data; 72 73struct huffcodetab { 74 const uint8_t len; /* max. index */ 75 const uint8_t *table; /* pointer to array[len][len] */ 76 const uint8_t *hlen; /* pointer to array[len][len] */ 77}; 78 79struct huffcodebig { 80 const uint8_t len; /* max. index */ 81 const uint8_t linbits; /* number of linbits */ 82 const uint16_t linmax; /* max number stored in linbits */ 83}; 84 85#define shft4(x) ((x + 8) >> 4) 86#define shft9(x) ((x + 256) >> 9) 87#define shft13(x) ((x + 4096) >> 13) 88#define shft15(x) ((x + 16384) >> 15) 89#define shft16(x) ((x + 32768) >> 16) 90#define shft_n(x,n) ((x) >> n) 91#define SQRT 724 /* sqrt(2) * 512 */ 92 93static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 6656 Bytes */ 94static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 9216 Bytes */ 95static int mdct_freq [SAMPL2] IBSS_ATTR; /* 2304 Bytes */ 96static char mdct_sign [SAMPL2] IBSS_ATTR; /* 576 Bytes */ 97static short enc_data [SAMPL2] IBSS_ATTR; /* 1152 Bytes */ 98static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */ 99 100static const uint16_t sfBand[6][23] ICONST_ATTR; 101static const uint16_t *scalefac IBSS_ATTR; 102 103static const int16_t ca [8] ICONST_ATTR; /* 16 Bytes */ 104static const uint16_t cs [8] ICONST_ATTR; /* 16 Bytes */ 105static const int16_t cx [9] ICONST_ATTR; /* 18 Bytes */ 106static const int16_t win [18][4] ICONST_ATTR; /* 144 Bytes */ 107static const int16_t enwindow [15*27+24] ICONST_ATTR; /* 862 Bytes */ 108static const uint16_t int2idx [4096] ICONST_ATTR; /* 8192 Bytes */ 109static const uint8_t ht_count[2][2][16] ICONST_ATTR; /* 64 Bytes */ 110static const uint32_t tab01 [ 16] ICONST_ATTR; /* 64 Bytes */ 111static const uint32_t tab23 [ 9] ICONST_ATTR; /* 36 Bytes */ 112static const uint32_t tab56 [ 16] ICONST_ATTR; /* 64 Bytes */ 113static const uint32_t tab1315 [256] ICONST_ATTR; /* 1024 Bytes */ 114static const uint32_t tab1624 [256] ICONST_ATTR; /* 1024 Bytes */ 115static const uint32_t tab789 [ 36] ICONST_ATTR; /* 144 Bytes */ 116static const uint32_t tabABC [ 64] ICONST_ATTR; /* 256 Bytes */ 117static const uint8_t t1HB [ 4] ICONST_ATTR; 118static const uint8_t t2HB [ 9] ICONST_ATTR; 119static const uint8_t t3HB [ 9] ICONST_ATTR; 120static const uint8_t t5HB [ 16] ICONST_ATTR; 121static const uint8_t t6HB [ 16] ICONST_ATTR; 122static const uint8_t t7HB [ 36] ICONST_ATTR; 123static const uint8_t t8HB [ 36] ICONST_ATTR; 124static const uint8_t t9HB [ 36] ICONST_ATTR; 125static const uint8_t t10HB [ 64] ICONST_ATTR; 126static const uint8_t t11HB [ 64] ICONST_ATTR; 127static const uint8_t t12HB [ 64] ICONST_ATTR; 128static const uint8_t t13HB [256] ICONST_ATTR; 129static const uint8_t t15HB [256] ICONST_ATTR; 130static const uint16_t t16HB [256] ICONST_ATTR; 131static const uint16_t t24HB [256] ICONST_ATTR; 132static const uint8_t t1l [ 8] ICONST_ATTR; 133static const uint8_t t2l [ 9] ICONST_ATTR; 134static const uint8_t t3l [ 9] ICONST_ATTR; 135static const uint8_t t5l [ 16] ICONST_ATTR; 136static const uint8_t t6l [ 16] ICONST_ATTR; 137static const uint8_t t7l [ 36] ICONST_ATTR; 138static const uint8_t t8l [ 36] ICONST_ATTR; 139static const uint8_t t9l [ 36] ICONST_ATTR; 140static const uint8_t t10l [ 64] ICONST_ATTR; 141static const uint8_t t11l [ 64] ICONST_ATTR; 142static const uint8_t t12l [ 64] ICONST_ATTR; 143static const uint8_t t13l [256] ICONST_ATTR; 144static const uint8_t t15l [256] ICONST_ATTR; 145static const uint8_t t16l [256] ICONST_ATTR; 146static const uint8_t t24l [256] ICONST_ATTR; 147static struct huffcodetab ht [HTN] IDATA_ATTR; 148 149static const uint8_t ht_count[2][2][16] = 150{ { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */ 151 { 1, 5, 5, 7, 5, 8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } }, /* hleng0 */ 152 { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, /* table1 */ 153 { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */ 154 155static const uint8_t t1HB[4] = {1,1,1,0}; 156static const uint8_t t2HB[9] = {1,2,1,3,1,1,3,2,0}; 157static const uint8_t t3HB[9] = {3,2,1,1,1,1,3,2,0}; 158static const uint8_t t5HB[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0}; 159static const uint8_t t6HB[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0}; 160 161static const uint8_t t7HB[36] = 162{ 1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4, 163 12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 }; 164 165static const uint8_t t8HB[36] = 166{ 3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3, 167 19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 }; 168 169static const uint8_t t9HB[36] = 170{ 7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5, 171 15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 }; 172 173static const uint8_t t10HB[64] = 174{1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32, 175 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22, 176 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 }; 177 178static const uint8_t t11HB[64] = 179{3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30, 180 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26, 181 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 }; 182 183static const uint8_t t12HB[64] = 184{9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21, 18530,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17, 18631,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 }; 187 188static const uint8_t t13HB[256] = 189{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, 190 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, 191 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, 192 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, 193 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, 194 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, 195 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, 196 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, 197 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, 198 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 }; 199 200static const uint8_t t15HB[256] = 201{7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51, 202 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, 203 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, 204 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, 205 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, 206 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, 207 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, 208 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, 209 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, 210 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}; 211 212static const uint16_t t16HB[256] = 213{1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47, 214 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209, 215 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65, 216 115,101,179,164,155,264,246,226,395,382,362,9,66,30,59,56,102,185,173,265,142, 217 253,232,400,388,378,445,16,111,54,52,100,184,178,160,133,257,244,228,217,385, 218 366,715,10,98,48,91,88,165,157,148,261,248,407,397,372,380,889,884,8,85,84,81, 219 159,156,143,260,249,427,401,392,383,727,713,708,7,154,76,73,141,131,256,245, 220 426,406,394,384,735,359,710,352,11,139,129,67,125,247,233,229,219,393,743,737, 221 720,885,882,439,4,243,120,118,115,227,223,396,746,742,736,721,712,706,223,436, 222 6,202,224,222,218,216,389,386,381,364,888,443,707,440,437,1728,4,747,211,210, 223 208,370,379,734,723,714,1735,883,877,876,3459,865,2,377,369,102,187,726,722, 224 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3, 225 1,3}; 226 227static const uint16_t t24HB[256] = 228{15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71, 229 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194, 230 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293, 231 271,16,147,72,69,135,127,118,112,210,200,188,352,323,306,285,540,14,263,66, 232 129,126,119,114,214,202,192,180,341,317,301,281,262,12,249,123,121,117,113, 233 215,206,195,185,347,330,308,291,272,520,10,435,115,111,109,211,203,196,187, 234 353,332,313,298,283,531,381,17,427,212,208,205,201,193,186,177,169,320,303, 235 286,268,514,377,16,335,199,197,191,189,181,174,333,321,305,289,275,521,379, 236 371,11,668,184,183,179,175,344,331,314,304,290,277,530,383,373,366,10,652,346, 237 171,168,164,318,309,299,287,276,263,513,375,368,362,6,648,322,316,312,307,302, 238 292,284,269,261,512,376,370,364,359,4,620,300,296,294,288,282,273,266,515,380, 239 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360, 240 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3}; 241 242static const uint32_t tab1315[256] = 243{ 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a, 244 0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e, 245 0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a, 246 0x0b000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0e000c,0x0e000d,0x0e000d, 247 0x070006,0x080007,0x090007,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a, 248 0x0b000a,0x0c000b,0x0c000b,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d, 249 0x080007,0x090008,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0c000b, 250 0x0c000b,0x0d000b,0x0d000c,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d, 251 0x090008,0x090008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0d000b,0x0d000b, 252 0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000d, 253 0x0a0009,0x0a0009,0x0b0009,0x0c000a,0x0c000a,0x0c000a,0x0d000b,0x0d000b, 254 0x0d000b,0x0d000b,0x0e000c,0x0d000c,0x0f000d,0x0f000d,0x10000d,0x10000e, 255 0x0a000a,0x0b0009,0x0c000a,0x0c000a,0x0d000a,0x0d000b,0x0d000b,0x0d000b, 256 0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x10000e, 257 0x0b000a,0x0b000a,0x0c000a,0x0d000b,0x0d000b,0x0d000b,0x0e000b,0x0e000c, 258 0x0e000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x12000d,0x12000e, 259 0x0a000a,0x0a000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0e000c, 260 0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x11000e,0x11000e, 261 0x0b000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0d000c,0x0f000c, 262 0x0e000c,0x0f000d,0x0f000d,0x10000d,0x10000d,0x10000e,0x12000e,0x11000e, 263 0x0b000b,0x0c000b,0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000c, 264 0x0e000c,0x0f000d,0x10000d,0x0f000d,0x10000d,0x11000e,0x12000f,0x13000e, 265 0x0c000b,0x0c000b,0x0c000b,0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0e000c, 266 0x0f000d,0x0f000d,0x0f000d,0x10000d,0x11000e,0x11000e,0x11000e,0x12000f, 267 0x0c000c,0x0d000c,0x0d000b,0x0e000c,0x0e000c,0x0f000c,0x0e000d,0x0f000d, 268 0x10000d,0x10000d,0x11000d,0x11000d,0x11000e,0x12000e,0x12000f,0x12000f, 269 0x0d000c,0x0d000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x10000d, 270 0x10000d,0x10000e,0x10000e,0x11000e,0x12000e,0x11000e,0x12000f,0x12000f, 271 0x0e000d,0x0e000d,0x0e000d,0x0f000d,0x0f000d,0x0f000d,0x11000d,0x10000d, 272 0x10000e,0x13000e,0x11000e,0x11000e,0x11000f,0x13000f,0x12000e,0x12000f, 273 0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e, 274 0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f }; 275 276static const uint32_t tab01[16] = 277{ 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007, 278 0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 }; 279 280static const uint32_t tab23[ 9] = 281{ 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 }; 282 283static const uint32_t tab56[16] = 284{ 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007, 285 0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 }; 286 287static const uint32_t tab789[36] = 288{0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004, 289 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806, 290 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808, 291 0x00b02c09,0x00b02c09,0x00b0300a,0x00802408,0x00902408,0x00a02809,0x00b02c09, 292 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b, 293 0x00c0340b}; 294 295static const uint32_t tabABC[64] = 296{0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a, 297 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809, 298 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809, 299 0x00c02c0a,0x00b02809,0x00b0280a,0x00802007,0x00902007,0x00a02408,0x00b02c08, 300 0x00c02809,0x00c0300a,0x00b0280a,0x00c02c0a,0x00902408,0x00a02808,0x00b02809, 301 0x00c02c09,0x00c02c0a,0x00c0300a,0x00c02c0a,0x00c0300b,0x00a02409,0x00b02809, 302 0x00c02c0a,0x00c0300a,0x00d0300a,0x00d0340b,0x00c0300a,0x00d0340b,0x00902409, 303 0x00a02409,0x00b02409,0x00c0280a,0x00c02c0a,0x00c0300b,0x00d0300b,0x00d0300c, 304 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b, 305 0x00d0300c}; 306 307static const uint32_t tab1624[256] = 308{0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a, 309 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c, 310 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009, 311 0x000b000a,0x000b000a,0x000b000b,0x000c000b,0x000c000b,0x000c000c,0x000d000c, 312 0x000e000c,0x000d000c,0x000e000c,0x000a000a,0x00070007,0x00080007,0x00090008, 313 0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000b,0x000d000b,0x000c000b, 314 0x000d000b,0x000d000c,0x000d000c,0x000e000c,0x000e000d,0x000b0009,0x00090008, 315 0x00090008,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,0x000c000b, 316 0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,0x000f000c, 317 0x000c0009,0x000a0009,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a, 318 0x000d000a,0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c, 319 0x000f000c,0x000f000d,0x000b0009,0x000a000a,0x000a0009,0x000b000a,0x000b000a, 320 0x000c000a,0x000d000a,0x000d000b,0x000e000b,0x000d000b,0x000e000b,0x000e000c, 321 0x000f000c,0x000f000c,0x000f000c,0x0010000c,0x000c0009,0x000b000a,0x000b000a, 322 0x000b000a,0x000c000a,0x000d000a,0x000d000b,0x000d000b,0x000d000b,0x000e000b, 323 0x000e000c,0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000d,0x000c0009, 324 0x000b000b,0x000b000a,0x000c000a,0x000c000a,0x000d000b,0x000d000b,0x000d000b, 325 0x000e000b,0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000d, 326 0x0011000d,0x000c000a,0x000b000b,0x000c000b,0x000c000b,0x000d000b,0x000d000b, 327 0x000d000b,0x000e000b,0x000e000b,0x000f000b,0x000f000c,0x000f000c,0x000f000c, 328 0x0010000c,0x0010000d,0x0010000d,0x000c000a,0x000c000b,0x000c000b,0x000c000b, 329 0x000d000b,0x000d000b,0x000e000b,0x000e000b,0x000f000c,0x000f000c,0x000f000c, 330 0x000f000c,0x0010000c,0x000f000d,0x0010000d,0x000f000d,0x000d000a,0x000c000c, 331 0x000d000b,0x000c000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000e000c, 332 0x000f000c,0x0010000c,0x0010000c,0x0010000d,0x0011000d,0x0011000d,0x0010000d, 333 0x000c000a,0x000d000c,0x000d000c,0x000d000b,0x000d000b,0x000e000b,0x000e000c, 334 0x000f000c,0x0010000c,0x0010000c,0x0010000c,0x0010000c,0x0010000d,0x0010000d, 335 0x000f000d,0x0010000d,0x000d000a,0x000d000c,0x000e000c,0x000e000c,0x000e000c, 336 0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000c,0x0010000d, 337 0x0010000d,0x0010000d,0x0010000d,0x0012000d,0x000d000a,0x000f000c,0x000e000c, 338 0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000c,0x0010000c,0x0010000d, 339 0x0012000d,0x0011000d,0x0011000d,0x0011000d,0x0013000d,0x0011000d,0x000d000a, 340 0x000e000d,0x000f000c,0x000d000c,0x000e000c,0x0010000c,0x0010000c,0x000f000c, 341 0x0010000d,0x0010000d,0x0011000d,0x0012000d,0x0011000d,0x0013000d,0x0011000d, 342 0x0010000d,0x000d000a,0x000a0009,0x000a0009,0x000a0009,0x000b0009,0x000b0009, 343 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a, 344 0x000d000a,0x000d000a,0x000d000a,0x000a0006}; 345 346static const uint8_t t1l[8] = {1,3,2,3,1,4,3,5}; 347static const uint8_t t2l[9] = {1,3,6,3,3,5,5,5,6}; 348static const uint8_t t3l[9] = {2,2,6,3,2,5,5,5,6}; 349static const uint8_t t5l[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8}; 350static const uint8_t t6l[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7}; 351 352static const uint8_t t7l[36] = 353{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}; 354 355static const uint8_t t8l[36] = 356{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}; 357 358static const uint8_t t9l[36] = 359{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}; 360 361static const uint8_t t10l[64] = 362{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, 363 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}; 364 365static const uint8_t t11l[64] = 366{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, 367 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}; 368 369static const uint8_t t12l[64] = 370{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, 371 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}; 372 373static const uint8_t t13l[256] = 374{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, 375 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, 376 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, 377 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, 378 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, 379 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, 380 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, 381 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, 382 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, 383 15,15,16,16,19,18,19,16}; 384 385static const uint8_t t15l[256] = 386{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, 387 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, 388 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, 389 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, 390 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, 391 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, 392 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, 393 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, 394 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13}; 395 396static const uint8_t t16l[256] = 397{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, 398 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, 399 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, 400 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, 401 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, 402 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, 403 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, 404 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, 405 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, 406 11,11,11,11,11,11,11,8}; 407 408static const uint8_t t24l[256] = 409{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, 410 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, 411 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, 412 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, 413 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, 414 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, 415 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, 416 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, 417 7,7,7,7,7,7,8,8,8,8,4}; 418 419static struct huffcodetab ht[HTN] = 420{ { 0, NULL, NULL}, /* Apparently not used */ 421 { 2, t1HB, t1l}, 422 { 3, t2HB, t2l}, 423 { 3, t3HB, t3l}, 424 { 0, NULL, NULL}, /* Apparently not used */ 425 { 4, t5HB, t5l}, 426 { 4, t6HB, t6l}, 427 { 6, t7HB, t7l}, 428 { 6, t8HB, t8l}, 429 { 6, t9HB, t9l}, 430 { 8, t10HB, t10l}, 431 { 8, t11HB, t11l}, 432 { 8, t12HB, t12l}, 433 {16, t13HB, t13l}, 434 { 0, NULL, NULL}, /* Apparently not used */ 435 {16, t15HB, t15l} }; 436 437static const struct huffcodebig ht_big[HTN] = 438{ { 16, 1, 1 }, 439 { 16, 2, 3 }, 440 { 16, 3, 7 }, 441 { 16, 4, 15 }, 442 { 16, 6, 63 }, 443 { 16, 8, 255 }, 444 { 16, 10, 1023 }, 445 { 16, 13, 8191 }, 446 { 16, 4, 15 }, 447 { 16, 5, 31 }, 448 { 16, 6, 63 }, 449 { 16, 7, 127 }, 450 { 16, 8, 255 }, 451 { 16, 9, 511 }, 452 { 16, 11, 2047 }, 453 { 16, 13, 8191 } }; 454 455static const struct 456{ 457 const uint8_t region0_cnt; 458 const uint8_t region1_cnt; 459} subdv_table[23] = 460{ {0, 0}, /* 0 bands */ 461 {0, 0}, /* 1 bands */ 462 {0, 0}, /* 2 bands */ 463 {0, 0}, /* 3 bands */ 464 {0, 0}, /* 4 bands */ 465 {0, 1}, /* 5 bands */ 466 {1, 1}, /* 6 bands */ 467 {1, 1}, /* 7 bands */ 468 {1, 2}, /* 8 bands */ 469 {2, 2}, /* 9 bands */ 470 {2, 3}, /* 10 bands */ 471 {2, 3}, /* 11 bands */ 472 {3, 4}, /* 12 bands */ 473 {3, 4}, /* 13 bands */ 474 {3, 4}, /* 14 bands */ 475 {4, 5}, /* 15 bands */ 476 {4, 5}, /* 16 bands */ 477 {4, 6}, /* 17 bands */ 478 {5, 6}, /* 18 bands */ 479 {5, 6}, /* 19 bands */ 480 {5, 7}, /* 20 bands */ 481 {6, 7}, /* 21 bands */ 482 {6, 7}, /* 22 bands */ 483}; 484 485static const uint16_t sfBand[6][23] = 486{ 487/* Table B.2.b: 22.05 kHz */ 488{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 489/* Table B.2.c: 24 kHz */ 490{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576}, 491/* Table B.2.a: 16 kHz */ 492{0,6,12,18,24,30,36,44,45,66,80,96,116,140,168,200,238,248,336,396,464,522,576}, 493/* Table B.8.b: 44.1 kHz */ 494{0,4, 8,12,16,20,24,30,36,44,52,62, 74, 90,110,134,162,196,238,288,342,418,576}, 495/* Table B.8.c: 48 kHz */ 496{0,4, 8,12,16,20,24,30,36,42,50,60, 72, 88,106,128,156,190,230,276,330,384,576}, 497/* Table B.8.a: 32 kHz */ 498{0,4, 8,12,16,20,24,30,36,44,54,66, 82,102,126,156,194,240,296,364,448,550,576} }; 499 500 501static const uint16_t int2idx[4096] = /* int2idx[i] = sqrt(i*sqrt(i)); */ 502{ 503 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 504 9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 505 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 506 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 507 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31, 508 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36, 509 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 40, 510 41, 41, 41, 41, 42, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45, 511 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49, 512 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53, 513 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57, 514 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61, 515 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65, 516 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 517 68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 72, 72, 72, 518 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75, 519 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 79, 79, 79, 520 79, 79, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82, 521 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 86, 86, 86, 522 86, 86, 86, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89, 523 89, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 93, 524 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, 95, 96, 96, 96, 525 96, 96, 96, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99, 526 99, 99,100,100,100,100,100,100,101,101,101,101,101,101,102,102,102,102,102,102, 527103,103,103,103,103,103,104,104,104,104,104,104,104,105,105,105,105,105,105,106, 528106,106,106,106,106,107,107,107,107,107,107,107,108,108,108,108,108,108,109,109, 529109,109,109,109,110,110,110,110,110,110,110,111,111,111,111,111,111,112,112,112, 530112,112,112,112,113,113,113,113,113,113,114,114,114,114,114,114,115,115,115,115, 531115,115,115,116,116,116,116,116,116,117,117,117,117,117,117,117,118,118,118,118, 532118,118,118,119,119,119,119,119,119,120,120,120,120,120,120,120,121,121,121,121, 533121,121,122,122,122,122,122,122,122,123,123,123,123,123,123,123,124,124,124,124, 534124,124,125,125,125,125,125,125,125,126,126,126,126,126,126,126,127,127,127,127, 535127,127,128,128,128,128,128,128,128,129,129,129,129,129,129,129,130,130,130,130, 536130,130,131,131,131,131,131,131,131,132,132,132,132,132,132,132,133,133,133,133, 537133,133,133,134,134,134,134,134,134,134,135,135,135,135,135,135,136,136,136,136, 538136,136,136,137,137,137,137,137,137,137,138,138,138,138,138,138,138,139,139,139, 539139,139,139,139,140,140,140,140,140,140,140,141,141,141,141,141,141,141,142,142, 540142,142,142,142,142,143,143,143,143,143,143,143,144,144,144,144,144,144,144,145, 541145,145,145,145,145,145,146,146,146,146,146,146,146,147,147,147,147,147,147,147, 542148,148,148,148,148,148,148,149,149,149,149,149,149,149,150,150,150,150,150,150, 543150,151,151,151,151,151,151,151,152,152,152,152,152,152,152,153,153,153,153,153, 544153,153,154,154,154,154,154,154,154,154,155,155,155,155,155,155,155,156,156,156, 545156,156,156,156,157,157,157,157,157,157,157,158,158,158,158,158,158,158,159,159, 546159,159,159,159,159,160,160,160,160,160,160,160,160,161,161,161,161,161,161,161, 547162,162,162,162,162,162,162,163,163,163,163,163,163,163,163,164,164,164,164,164, 548164,164,165,165,165,165,165,165,165,166,166,166,166,166,166,166,167,167,167,167, 549167,167,167,167,168,168,168,168,168,168,168,169,169,169,169,169,169,169,169,170, 550170,170,170,170,170,170,171,171,171,171,171,171,171,172,172,172,172,172,172,172, 551172,173,173,173,173,173,173,173,174,174,174,174,174,174,174,174,175,175,175,175, 552175,175,175,176,176,176,176,176,176,176,176,177,177,177,177,177,177,177,178,178, 553178,178,178,178,178,178,179,179,179,179,179,179,179,180,180,180,180,180,180,180, 554180,181,181,181,181,181,181,181,182,182,182,182,182,182,182,182,183,183,183,183, 555183,183,183,184,184,184,184,184,184,184,184,185,185,185,185,185,185,185,186,186, 556186,186,186,186,186,186,187,187,187,187,187,187,187,187,188,188,188,188,188,188, 557188,189,189,189,189,189,189,189,189,190,190,190,190,190,190,190,190,191,191,191, 558191,191,191,191,192,192,192,192,192,192,192,192,193,193,193,193,193,193,193,193, 559194,194,194,194,194,194,194,195,195,195,195,195,195,195,195,196,196,196,196,196, 560196,196,196,197,197,197,197,197,197,197,197,198,198,198,198,198,198,198,199,199, 561199,199,199,199,199,199,200,200,200,200,200,200,200,200,201,201,201,201,201,201, 562201,201,202,202,202,202,202,202,202,202,203,203,203,203,203,203,203,204,204,204, 563204,204,204,204,204,205,205,205,205,205,205,205,205,206,206,206,206,206,206,206, 564206,207,207,207,207,207,207,207,207,208,208,208,208,208,208,208,208,209,209,209, 565209,209,209,209,209,210,210,210,210,210,210,210,210,211,211,211,211,211,211,211, 566211,212,212,212,212,212,212,212,212,213,213,213,213,213,213,213,213,214,214,214, 567214,214,214,214,214,215,215,215,215,215,215,215,215,216,216,216,216,216,216,216, 568216,217,217,217,217,217,217,217,217,218,218,218,218,218,218,218,218,219,219,219, 569219,219,219,219,219,220,220,220,220,220,220,220,220,221,221,221,221,221,221,221, 570221,222,222,222,222,222,222,222,222,223,223,223,223,223,223,223,223,224,224,224, 571224,224,224,224,224,225,225,225,225,225,225,225,225,226,226,226,226,226,226,226, 572226,227,227,227,227,227,227,227,227,228,228,228,228,228,228,228,228,229,229,229, 573229,229,229,229,229,229,230,230,230,230,230,230,230,230,231,231,231,231,231,231, 574231,231,232,232,232,232,232,232,232,232,233,233,233,233,233,233,233,233,234,234, 575234,234,234,234,234,234,234,235,235,235,235,235,235,235,235,236,236,236,236,236, 576236,236,236,237,237,237,237,237,237,237,237,238,238,238,238,238,238,238,238,238, 577239,239,239,239,239,239,239,239,240,240,240,240,240,240,240,240,241,241,241,241, 578241,241,241,241,242,242,242,242,242,242,242,242,242,243,243,243,243,243,243,243, 579243,244,244,244,244,244,244,244,244,245,245,245,245,245,245,245,245,245,246,246, 580246,246,246,246,246,246,247,247,247,247,247,247,247,247,248,248,248,248,248,248, 581248,248,248,249,249,249,249,249,249,249,249,250,250,250,250,250,250,250,250,250, 582251,251,251,251,251,251,251,251,252,252,252,252,252,252,252,252,253,253,253,253, 583253,253,253,253,253,254,254,254,254,254,254,254,254,255,255,255,255,255,255,255, 584255,255,256,256,256,256,256,256,256,256,257,257,257,257,257,257,257,257,257,258, 585258,258,258,258,258,258,258,259,259,259,259,259,259,259,259,259,260,260,260,260, 586260,260,260,260,261,261,261,261,261,261,261,261,261,262,262,262,262,262,262,262, 587262,263,263,263,263,263,263,263,263,263,264,264,264,264,264,264,264,264,265,265, 588265,265,265,265,265,265,265,266,266,266,266,266,266,266,266,267,267,267,267,267, 589267,267,267,267,268,268,268,268,268,268,268,268,268,269,269,269,269,269,269,269, 590269,270,270,270,270,270,270,270,270,270,271,271,271,271,271,271,271,271,271,272, 591272,272,272,272,272,272,272,273,273,273,273,273,273,273,273,273,274,274,274,274, 592274,274,274,274,275,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276, 593276,276,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,279, 594279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,281,281,281, 595281,281,281,281,281,282,282,282,282,282,282,282,282,282,283,283,283,283,283,283, 596283,283,283,284,284,284,284,284,284,284,284,284,285,285,285,285,285,285,285,285, 597286,286,286,286,286,286,286,286,286,287,287,287,287,287,287,287,287,287,288,288, 598288,288,288,288,288,288,288,289,289,289,289,289,289,289,289,289,290,290,290,290, 599290,290,290,290,291,291,291,291,291,291,291,291,291,292,292,292,292,292,292,292, 600292,292,293,293,293,293,293,293,293,293,293,294,294,294,294,294,294,294,294,294, 601295,295,295,295,295,295,295,295,295,296,296,296,296,296,296,296,296,296,297,297, 602297,297,297,297,297,297,297,298,298,298,298,298,298,298,298,299,299,299,299,299, 603299,299,299,299,300,300,300,300,300,300,300,300,300,301,301,301,301,301,301,301, 604301,301,302,302,302,302,302,302,302,302,302,303,303,303,303,303,303,303,303,303, 605304,304,304,304,304,304,304,304,304,305,305,305,305,305,305,305,305,305,306,306, 606306,306,306,306,306,306,306,307,307,307,307,307,307,307,307,307,308,308,308,308, 607308,308,308,308,308,309,309,309,309,309,309,309,309,309,310,310,310,310,310,310, 608310,310,310,311,311,311,311,311,311,311,311,311,312,312,312,312,312,312,312,312, 609312,313,313,313,313,313,313,313,313,313,314,314,314,314,314,314,314,314,314,315, 610315,315,315,315,315,315,315,315,316,316,316,316,316,316,316,316,316,317,317,317, 611317,317,317,317,317,317,318,318,318,318,318,318,318,318,318,318,319,319,319,319, 612319,319,319,319,319,320,320,320,320,320,320,320,320,320,321,321,321,321,321,321, 613321,321,321,322,322,322,322,322,322,322,322,322,323,323,323,323,323,323,323,323, 614323,324,324,324,324,324,324,324,324,324,325,325,325,325,325,325,325,325,325,325, 615326,326,326,326,326,326,326,326,326,327,327,327,327,327,327,327,327,327,328,328, 616328,328,328,328,328,328,328,329,329,329,329,329,329,329,329,329,330,330,330,330, 617330,330,330,330,330,330,331,331,331,331,331,331,331,331,331,332,332,332,332,332, 618332,332,332,332,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334, 619334,334,335,335,335,335,335,335,335,335,335,335,336,336,336,336,336,336,336,336, 620336,337,337,337,337,337,337,337,337,337,338,338,338,338,338,338,338,338,338,338, 621339,339,339,339,339,339,339,339,339,340,340,340,340,340,340,340,340,340,341,341, 622341,341,341,341,341,341,341,341,342,342,342,342,342,342,342,342,342,343,343,343, 623343,343,343,343,343,343,344,344,344,344,344,344,344,344,344,344,345,345,345,345, 624345,345,345,345,345,346,346,346,346,346,346,346,346,346,347,347,347,347,347,347, 625347,347,347,347,348,348,348,348,348,348,348,348,348,349,349,349,349,349,349,349, 626349,349,350,350,350,350,350,350,350,350,350,350,351,351,351,351,351,351,351,351, 627351,352,352,352,352,352,352,352,352,352,352,353,353,353,353,353,353,353,353,353, 628354,354,354,354,354,354,354,354,354,355,355,355,355,355,355,355,355,355,355,356, 629356,356,356,356,356,356,356,356,357,357,357,357,357,357,357,357,357,357,358,358, 630358,358,358,358,358,358,358,359,359,359,359,359,359,359,359,359,359,360,360,360, 631360,360,360,360,360,360,361,361,361,361,361,361,361,361,361,361,362,362,362,362, 632362,362,362,362,362,363,363,363,363,363,363,363,363,363,363,364,364,364,364,364, 633364,364,364,364,365,365,365,365,365,365,365,365,365,365,366,366,366,366,366,366, 634366,366,366,367,367,367,367,367,367,367,367,367,367,368,368,368,368,368,368,368, 635368,368,369,369,369,369,369,369,369,369,369,369,370,370,370,370,370,370,370,370, 636370,370,371,371,371,371,371,371,371,371,371,372,372,372,372,372,372,372,372,372, 637372,373,373,373,373,373,373,373,373,373,374,374,374,374,374,374,374,374,374,374, 638375,375,375,375,375,375,375,375,375,375,376,376,376,376,376,376,376,376,376,377, 639377,377,377,377,377,377,377,377,377,378,378,378,378,378,378,378,378,378,379,379, 640379,379,379,379,379,379,379,379,380,380,380,380,380,380,380,380,380,380,381,381, 641381,381,381,381,381,381,381,382,382,382,382,382,382,382,382,382,382,383,383,383, 642383,383,383,383,383,383,383,384,384,384,384,384,384,384,384,384,385,385,385,385, 643385,385,385,385,385,385,386,386,386,386,386,386,386,386,386,386,387,387,387,387, 644387,387,387,387,387,387,388,388,388,388,388,388,388,388,388,389,389,389,389,389, 645389,389,389,389,389,390,390,390,390,390,390,390,390,390,390,391,391,391,391,391, 646391,391,391,391,391,392,392,392,392,392,392,392,392,392,393,393,393,393,393,393, 647393,393,393,393,394,394,394,394,394,394,394,394,394,394,395,395,395,395,395,395, 648395,395,395,395,396,396,396,396,396,396,396,396,396,397,397,397,397,397,397,397, 649397,397,397,398,398,398,398,398,398,398,398,398,398,399,399,399,399,399,399,399, 650399,399,399,400,400,400,400,400,400,400,400,400,400,401,401,401,401,401,401,401, 651401,401,402,402,402,402,402,402,402,402,402,402,403,403,403,403,403,403,403,403, 652403,403,404,404,404,404,404,404,404,404,404,404,405,405,405,405,405,405,405,405, 653405,405,406,406,406,406,406,406,406,406,406,406,407,407,407,407,407,407,407,407, 654407,407,408,408,408,408,408,408,408,408,408,408,409,409,409,409,409,409,409,409, 655409,410,410,410,410,410,410,410,410,410,410,411,411,411,411,411,411,411,411,411, 656411,412,412,412,412,412,412,412,412,412,412,413,413,413,413,413,413,413,413,413, 657413,414,414,414,414,414,414,414,414,414,414,415,415,415,415,415,415,415,415,415, 658415,416,416,416,416,416,416,416,416,416,416,417,417,417,417,417,417,417,417,417, 659417,418,418,418,418,418,418,418,418,418,418,419,419,419,419,419,419,419,419,419, 660419,420,420,420,420,420,420,420,420,420,420,421,421,421,421,421,421,421,421,421, 661421,422,422,422,422,422,422,422,422,422,422,423,423,423,423,423,423,423,423,423, 662423,424,424,424,424,424,424,424,424,424,424,425,425,425,425,425,425,425,425,425, 663425,426,426,426,426,426,426,426,426,426,426,427,427,427,427,427,427,427,427,427, 664427,428,428,428,428,428,428,428,428,428,428,429,429,429,429,429,429,429,429,429, 665429,430,430,430,430,430,430,430,430,430,430,431,431,431,431,431,431,431,431,431, 666431,432,432,432,432,432,432,432,432,432,432,433,433,433,433,433,433,433,433,433, 667433,434,434,434,434,434,434,434,434,434,434,435,435,435,435,435,435,435,435,435, 668435,435,436,436,436,436,436,436,436,436,436,436,437,437,437,437,437,437,437,437, 669437,437,438,438,438,438,438,438,438,438,438,438,439,439,439,439,439,439,439,439, 670439,439,440,440,440,440,440,440,440,440,440,440,441,441,441,441,441,441,441,441, 671441,441,442,442,442,442,442,442,442,442,442,442,443,443,443,443,443,443,443,443, 672443,443,443,444,444,444,444,444,444,444,444,444,444,445,445,445,445,445,445,445, 673445,445,445,446,446,446,446,446,446,446,446,446,446,447,447,447,447,447,447,447, 674447,447,447,448,448,448,448,448,448,448,448,448,448,448,449,449,449,449,449,449, 675449,449,449,449,450,450,450,450,450,450,450,450,450,450,451,451,451,451,451,451, 676451,451,451,451,452,452,452,452,452,452,452,452,452,452,453,453,453,453,453,453, 677453,453,453,453,453,454,454,454,454,454,454,454,454,454,454,455,455,455,455,455, 678455,455,455,455,455,456,456,456,456,456,456,456,456,456,456,457,457,457,457,457, 679457,457,457,457,457,457,458,458,458,458,458,458,458,458,458,458,459,459,459,459, 680459,459,459,459,459,459,460,460,460,460,460,460,460,460,460,460,460,461,461,461, 681461,461,461,461,461,461,461,462,462,462,462,462,462,462,462,462,462,463,463,463, 682463,463,463,463,463,463,463,463,464,464,464,464,464,464,464,464,464,464,465,465, 683465,465,465,465,465,465,465,465,466,466,466,466,466,466,466,466,466,466,466,467, 684467,467,467,467,467,467,467,467,467,468,468,468,468,468,468,468,468,468,468,469, 685469,469,469,469,469,469,469,469,469,469,470,470,470,470,470,470,470,470,470,470, 686471,471,471,471,471,471,471,471,471,471,472,472,472,472,472,472,472,472,472,472, 687472,473,473,473,473,473,473,473,473,473,473,474,474,474,474,474,474,474,474,474, 688474,475,475,475,475,475,475,475,475,475,475,475,476,476,476,476,476,476,476,476, 689476,476,477,477,477,477,477,477,477,477,477,477,477,478,478,478,478,478,478,478, 690478,478,478,479,479,479,479,479,479,479,479,479,479,479,480,480,480,480,480,480, 691480,480,480,480,481,481,481,481,481,481,481,481,481,481,482,482,482,482,482,482, 692482,482,482,482,482,483,483,483,483,483,483,483,483,483,483,484,484,484,484,484, 693484,484,484,484,484,484,485,485,485,485,485,485,485,485,485,485,486,486,486,486, 694486,486,486,486,486,486,486,487,487,487,487,487,487,487,487,487,487,488,488,488, 695488,488,488,488,488,488,488,488,489,489,489,489,489,489,489,489,489,489,490,490, 696490,490,490,490,490,490,490,490,490,491,491,491,491,491,491,491,491,491,491,492, 697492,492,492,492,492,492,492,492,492,492,493,493,493,493,493,493,493,493,493,493, 698494,494,494,494,494,494,494,494,494,494,494,495,495,495,495,495,495,495,495,495, 699495,496,496,496,496,496,496,496,496,496,496,496,497,497,497,497,497,497,497,497, 700497,497,497,498,498,498,498,498,498,498,498,498,498,499,499,499,499,499,499,499, 701499,499,499,499,500,500,500,500,500,500,500,500,500,500,501,501,501,501,501,501, 702501,501,501,501,501,502,502,502,502,502,502,502,502,502,502,503,503,503,503,503, 703503,503,503,503,503,503,504,504,504,504,504,504,504,504,504,504,504,505,505,505, 704505,505,505,505,505,505,505,506,506,506,506,506,506,506,506,506,506,506,507,507, 705507,507,507,507,507,507,507,507,507,508,508,508,508,508,508,508,508,508,508,509, 706509,509,509,509,509,509,509,509,509,509,510,510,510,510,510,510,510,510,510,510, 707510,511,511,511,511,511,511,511,511,511,511,512,512,512,512,512 }; 708 709static const uint8_t order[32] = 710{ 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29, 711 2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 }; 712 713static const uint16_t sampr_index[2][3] = 714{ { 22050, 24000, 16000 }, /* MPEG 2 */ 715 { 44100, 48000, 32000 } }; /* MPEG 1 */ 716 717static const uint16_t bitr_index[2][15] = 718{ {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160}, /* MPEG 2 */ 719 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} }; /* MPEG 1 */ 720 721static const uint8_t num_bands[3][15] = 722{ {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32}, 723 {0,10,10,10,10,10,12,14, 18, 24, 26, 28, 30, 32, 32}, 724 {0,10,12,14,18,24,26,28, 30, 32, 32, 32, 32, 32, 32} }; 725 726static const int16_t cx[9] = 727{ 16135, 10531, 5604, 15396, -2845,-12551, 14189, 8192, 16384 }; 728 729static const int16_t ca[8] = 730{-16859,-15458,-10269, -5961, -3099, -1342, -465, -121 }; 731 732static const uint16_t cs[8] = 733{ 28098, 28893, 31117, 32221, 32621, 32740, 32765, 32768 }; 734 735static const int16_t enwindow[15*27+24] = 736{ 0, 65, 593, 1766, 22228, 2115, 611, 62, 737 8, 119, 1419, 10564,-11659,-1635,-154, -9, 738 -8, -119,-1419,-10564, 11659, 1635, 154, 9, 464, 100, 91, 739 0, 69, 604, 1635, 23148, 2363, 643, 62, 740 7, 107, 1368, 10449,-12733,-1818,-180,-11, 741 -7, -107,-1368,-10449, 12733, 1818, 180, 11, 420, 200, 164, 742 0, 72, 608, 1465, 23979, 2600, 671, 63, 743 7, 94, 1305, 10265,-13818,-2004,-207,-12, 744 -7, -94,-1305,-10265, 13818, 2004, 207, 12, 380, 297, 220, 745 0, 76, 606, 1256, 24718, 2825, 693, 63, 746 6, 81, 1232, 10016,-14908,-2192,-236,-14, 747 -6, -81,-1232,-10016, 14908, 2192, 236, 14, 342, 392, 262, 748 0, 78, 597, 1007, 25359, 3033, 712, 63, 749 6, 68, 1150, 9706,-15995,-2380,-267,-15, 750 -6, -68,-1150, -9706, 15995, 2380, 267, 15, 307, 483, 289, 751 0, 80, 580, 719, 25901, 3224, 726, 62, 752 6, 54, 1060, 9343,-17072,-2565,-299,-17, 753 -6, -54,-1060, -9343, 17072, 2565, 299, 17, 274, 569, 304, 754 -1, 82, 555, 391, 26339, 3395, 735, 61, 755 5, 40, 963, 8930,-18131,-2747,-332,-19, 756 -5, -40, -963, -8930, 18131, 2747, 332, 19, 242, 650, 307, 757 -1, 83, 523, 26, 26672, 3545, 740, 60, 758 5, 27, 861, 8474,-19164,-2923,-366,-21, 759 -5, -27, -861, -8474, 19164, 2923, 366, 21, 212, 724, 300, 760 -1, 83, 482, -376, 26900, 3672, 739, 58, 761 4, 14, 756, 7981,-20163,-3092,-401,-24, 762 -4, -14, -756, -7981, 20163, 3092, 401, 24, 183, 792, 283, 763 -1, 82, 433, -812, 27022, 3776, 735, 56, 764 4, 1, 648, 7456,-21122,-3250,-435,-26, 765 -4, -1, -648, -7456, 21122, 3250, 435, 26, 155, 851, 258, 766 -1, 81, 376, -1281, 27038, 3855, 726, 54, 767 3, -11, 539, 6907,-22032,-3397,-470,-28, 768 -3, 11, -539, -6907, 22032, 3397, 470, 28, 128, 903, 226, 769 -1, 78, 312, -1778, 26951, 3910, 713, 52, 770 3, -22, 430, 6338,-22887,-3530,-503,-31, 771 -3, 22, -430, -6338, 22887, 3530, 503, 31, 102, 946, 188, 772 -2, 75, 239, -2302, 26761, 3941, 696, 49, 773 3, -33, 322, 5757,-23678,-3648,-537,-34, 774 -3, 33, -322, -5757, 23678, 3648, 537, 34, 76, 980, 145, 775 -2, 70, 160, -2848, 26472, 3948, 676, 47, 776 3, -42, 217, 5167,-24399,-3749,-568,-36, 777 -3, 42, -217, -5167, 24399, 3749, 568, 36, 50, 1004, 99, 778 -2, 65, 74, -3412, 26087, 3931, 653, 44, 779 2, -51, 115, 4577,-25045,-3830,-599,-39, 780 -2, 51, -115, -4577, 25045, 3830, 599, 39, 25, 1019, 50, 781 782 25610,3891,627,42,-3990,-18,58,-2, 783 21226,-21226,10604,-10604,1860,-1860,1458,-1458,576,-576,130,-130,60,-60,8,-8 784}; 785 786static const int16_t win[18][4] = { 787 { -3072, -134, -146, 3352 }, 788 { -2747, -362, -471, 3579 }, 789 { -2387, -529, -831, 3747 }, 790 { -2004, -632,-1214, 3850 }, 791 { -1609, -666,-1609, 3884 }, 792 { -1214, -632,-2004, 3850 }, 793 { -831, -529,-2387, 3747 }, 794 { -471, -362,-2747, 3579 }, 795 { -146, -134,-3072, 3352 }, 796 { 134,-3072,-3352, -146 }, 797 { 362,-2747,-3579, -471 }, 798 { 529,-2387,-3747, -831 }, 799 { 632,-2004,-3850,-1214 }, 800 { 666,-1609,-3884,-1609 }, 801 { 632,-1214,-3850,-2004 }, 802 { 529, -831,-3747,-2387 }, 803 { 362, -471,-3579,-2747 }, 804 { 134, -146,-3352,-3072 } }; 805 806static char* mp3_enc_err[] = { 807 /* 0 */ "", 808 /* 1 */ "Cannot open file.", 809 /* 2 */ "'RIFF' missing.", 810 /* 3 */ "'WAVE' missing.", 811 /* 4 */ "'fmt ' missing.", 812 /* 5 */ "Linear PCM required.", 813 /* 6 */ "16 bit per sample required.", 814 /* 7 */ "<=2 channels required.", 815 /* 8 */ "'data' missing.", 816 /* 9 */ "Samplerate not supported." 817}; 818 819static const char* wav_filename; 820static int mp3file, wavfile, wav_size, frames; 821static void *enc_buffer; 822static size_t enc_buffer_size; 823static int enc_chunk = 0; /* encode chunk counter */ 824static int enc_size; 825static config_t cfg; 826static uint8_t band_scale_f[22]; 827 828 829/* forward declarations */ 830static int HuffmanCode( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table); 831static int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table); 832static void putbits(uint32_t val, uint32_t nbit); 833static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table, 834 uint32_t len, int *bits); 835static int find_best_3( short *ix, uint32_t start, uint32_t end, const uint32_t *table, 836 uint32_t len, int *bits); 837static int count_bit1 ( short *ix, uint32_t start, uint32_t end, int *bits ); 838static int count_bigv ( short *ix, uint32_t start, uint32_t end, int table0, int table1, 839 int *bits); 840 841 842static bool checkString(int fd, char *string) 843{ 844 char temp[4]; 845 846 rb->read(fd, temp, 4); 847 848 /* return 1 on match, 0 on no match */ 849 return !rb->memcmp(temp, string, 4); 850} 851 852static int Read16BitsLowHigh(int fd) 853{ 854 char first, second; 855 856 rb->read(fd, &first, 1); 857 rb->read(fd, &second, 1); 858 859 return ((int)second << 8) | (first & 0xff); 860} 861 862 863static int Read32BitsLowHigh(int fd) 864{ 865 int first = 0xffff & Read16BitsLowHigh(fd); 866 int second = 0xffff & Read16BitsLowHigh(fd); 867 868 return (second << 16) + first; 869} 870 871static int wave_open(void) 872{ 873 unsigned short wFormatTag; 874 /* rockbox: comment 'set but unused" variable 875 unsigned long dAvgBytesPerSec; 876 unsigned short wBlockAlign; 877 */ 878 unsigned short bits_per_samp; 879 long header_size; 880 881 if((wavfile = rb->open(wav_filename, O_RDONLY)) < 0) 882 return -1; 883 884 if(!checkString(wavfile,"RIFF")) return -2; 885 Read32BitsLowHigh(wavfile); /* complete wave chunk size */ 886 if(!checkString(wavfile,"WAVE")) return -3; 887 if(!checkString(wavfile,"fmt ")) return -4; 888 889 header_size = Read32BitsLowHigh(wavfile); /* chunk size */ 890 wFormatTag = Read16BitsLowHigh(wavfile); 891 892 cfg.channels = Read16BitsLowHigh(wavfile); 893 cfg.samplerate = Read32BitsLowHigh(wavfile); 894 /*dAvgBytesPerSec*/ Read32BitsLowHigh(wavfile); 895 /*wBlockAlign */ Read16BitsLowHigh(wavfile); 896 bits_per_samp = Read16BitsLowHigh(wavfile); 897 898 if(wFormatTag != 0x0001) return -5; /* linear PCM required */ 899 if(bits_per_samp != 16) return -6; /* 16 bps required */ 900 if(cfg.channels > 2) return -7; /* <=2 channels required */ 901 if(!checkString(wavfile,"data")) return -8; 902 903 /* Sample rates != 16/22.05/24/32/44.1/48 kHz are not supported. */ 904 if((cfg.samplerate != 16000) && (cfg.samplerate != 22050) && 905 (cfg.samplerate != 24000) && (cfg.samplerate != 32000) && 906 (cfg.samplerate != 44100) && (cfg.samplerate != 48000)) return -9; 907 908 header_size = 0x28; 909 wav_size = rb->filesize(wavfile); 910 rb->lseek(wavfile, header_size, SEEK_SET); 911 912 return 0; 913} 914 915static int read_samples(uint16_t *buffer, int num_samples) 916{ 917 uint16_t tmpbuf[MAX_SAMP_PER_FRAME*2]; /* SAMP_PER_FRAME*MAX_CHANNELS */ 918 int byte_per_sample = cfg.channels * 2; /* requires bits_per_sample==16 */ 919 int s, samples = rb->read(wavfile, tmpbuf, byte_per_sample * num_samples) / byte_per_sample; 920 /* Pad last sample with zeros */ 921 memset(tmpbuf + samples*cfg.channels, 0, (num_samples-samples)*cfg.channels); 922 923 if (cfg.channels==1) 924 { 925 /* interleave the mono samples to stereo as required by encoder */ 926 for(s=0; s<num_samples; s++) 927 buffer[2*s] = tmpbuf[s]; 928 } 929 else 930 { 931 /* interleaving is correct for stereo */ 932 memcpy(buffer, tmpbuf, sizeof(tmpbuf)); 933 } 934 935 return samples; 936} 937 938static inline uint32_t myswap32(uint32_t val) 939{ 940 const uint8_t* v = (const uint8_t*)&val; 941 942 return ((uint32_t)v[0]<<24) | ((uint32_t)v[1]<<16) | ((uint32_t)v[2]<<8) | v[3]; 943} 944 945static void encodeSideInfo( side_info_t si[2][2] ) 946{ 947 int gr, ch, header; 948 uint32_t cc=0, sz=0; 949 950 /* 951 * MPEG header layout: 952 * AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM 953 * A (31-21) = frame sync 954 * B (20-19) = MPEG type 955 * C (18-17) = MPEG layer 956 * D (16) = protection bit 957 * E (15-12) = bitrate index 958 * F (11-10) = samplerate index 959 * G (9) = padding bit 960 * H (8) = private bit 961 * I (7-6) = channel mode 962 * J (5-4) = mode extension (jstereo only) 963 * K (3) = copyright bit 964 * L (2) = original 965 * M (1-0) = emphasis 966 */ 967 968 header = (0xfff00000) | /* frame sync (AAAAAAAAA AAA) 969 mp3 type (upper): 1 (B) */ 970 (0x01 << 17) | /* mp3 layer: 01 (CC) */ 971 ( 0x1 << 16) | /* mp3 crc: 1 (D) */ 972 ( 0x1 << 2); /* mp3 org: 1 (L) */ 973 header |= cfg.mpg.type << 19; 974 header |= cfg.mpg.bitr_id << 12; 975 header |= cfg.mpg.smpl_id << 10; 976 header |= cfg.mpg.padding << 9; 977 header |= cfg.mpg.mode << 6; 978 /* no emphasis (bits 0-1) */ 979 putbits( header, 32 ); 980 981 if(cfg.mpg.type == 1) 982 { /* MPEG1 */ 983 if(cfg.channels == 2) { putlong( 0, 20); } 984 else { putlong( 0, 18); } 985 986 for(gr=0; gr<cfg.granules; gr++) 987 for(ch=0; ch<cfg.channels; ch++) 988 { 989 side_info_t *gi = &si[gr][ch]; 990 991 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */ 992 putlong( gi->address3>>1, 9 ); 993 putlong( gi->global_gain, 8 ); 994 putlong( 9, 4 ); /* set scale_facs compr type */ 995 putlong( gi->table_select[0], 6 ); 996 putlong( gi->table_select[1], 5 ); 997 putlong( gi->table_select[2], 5 ); 998 putlong( gi->region_0_1, 7 ); 999 putlong( 1 , 2 ); /* set scale_facs to 1bit */ 1000 putlong( gi->table_select[3], 1 ); 1001 } 1002 } 1003 else 1004 { /* MPEG2 */ 1005 if(cfg.channels == 2) { putlong( 0, 10); } 1006 else { putlong( 0, 9); } 1007 1008 for(ch=0; ch<cfg.channels; ch++) 1009 { 1010 side_info_t *gi = &si[0][ch]; 1011 1012 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */ 1013 putlong( gi->address3>>1, 9 ); 1014 putlong( gi->global_gain, 8 ); 1015 putlong( 0xCA, 9 ); /* set scale_facs compr type */ 1016 putlong( gi->table_select[0], 6 ); 1017 putlong( gi->table_select[1], 5 ); 1018 putlong( gi->table_select[2], 5 ); 1019 putlong( gi->region_0_1 , 7 ); 1020 putlong( 1 , 1 ); /* set scale_facs to 1bit */ 1021 putlong( gi->table_select[3], 1 ); 1022 } 1023 } 1024 /* flush remaining bits */ 1025 putbits(cc, sz); 1026} 1027 1028/* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS, 1029 as well as the definitions of the side information on pages 26 and 27. */ 1030static void Huffmancodebits( short *ix, char *xr_sign, side_info_t *gi ) 1031{ 1032 int region1 = gi->address1; 1033 int region2 = gi->address2; 1034 int bigvals = gi->address3; 1035 int count1 = bigvals + (gi->count1 << 2); 1036 int stuffBits = 0; 1037 int bits = 0; 1038 int i, v; 1039 1040 for(i=v=0; i<32; i+=2) 1041 v |= band_scale_f[i>>1] << (30-i); 1042 putbits(v, 32); // store scale_facs (part1) 1043 1044 for(v=0; i<42; i+=2) 1045 v |= band_scale_f[i>>1] << (40-i); 1046 putbits(v, 10); // store scale_facs (part2) 1047 1048 if(region1 > 0) 1049 bits += HuffmanCode(ix, xr_sign, 0 , region1, gi->table_select[0]); 1050 1051 if(region2 > region1) 1052 bits += HuffmanCode(ix, xr_sign, region1, region2, gi->table_select[1]); 1053 1054 if(bigvals > region2) 1055 bits += HuffmanCode(ix, xr_sign, region2, bigvals, gi->table_select[2]); 1056 1057 if(count1 > bigvals) 1058 bits += HuffmanCod1(ix, xr_sign, bigvals, count1, gi->table_select[3]); 1059 1060 if((stuffBits = gi->part2_3_length - bits) > 0) 1061 { 1062 int stuffWords = stuffBits >> 5; 1063 int remainBits = stuffBits & 31; 1064 1065 if( remainBits ) 1066 putbits( ~0, remainBits ); 1067 1068 while( stuffWords-- ) 1069 putbits( ~0, 32 ); /* Huffman code tables leed to padding ones */ 1070 } 1071} 1072 1073int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int tbl) 1074{ 1075 uint32_t cc=0, sz=0; 1076 uint32_t i, d, p; 1077 int sumbit=0, s=0, l=0, v, w, x, y; 1078 #define sgnv xr_sign[i+0] 1079 #define sgnw xr_sign[i+1] 1080 #define sgnx xr_sign[i+2] 1081 #define sgny xr_sign[i+3] 1082 1083 for(i=begin; i<end; i+=4) 1084 { 1085 v = ix[i+0]; 1086 w = ix[i+1]; 1087 x = ix[i+2]; 1088 y = ix[i+3]; 1089 p = (v << 3) + (w << 2) + (x << 1) + y; 1090 1091 switch(p) 1092 { 1093 case 0: l=0; s = 0; break; 1094 case 1: l=1; s = sgny; break; 1095 case 2: l=1; s = sgnx; break; 1096 case 3: l=2; s = (sgnx << 1) + sgny; break; 1097 case 4: l=1; s = sgnw; break; 1098 case 5: l=2; s = (sgnw << 1) + sgny; break; 1099 case 6: l=2; s = (sgnw << 1) + sgnx; break; 1100 case 7: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break; 1101 case 8: l=1; s = sgnv; break; 1102 case 9: l=2; s = (sgnv << 1) + sgny; break; 1103 case 10: l=2; s = (sgnv << 1) + sgnx; break; 1104 case 11: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break; 1105 case 12: l=2; s = (sgnv << 1) + sgnw; break; 1106 case 13: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break; 1107 case 14: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break; 1108 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break; 1109 default: /* bug fix */ 1110 rb->splashf(HZ * 2, "bad input %lu < or > array bounds", (unsigned long)p); 1111 return 0; 1112 } 1113 1114 d = (ht_count[tbl][0][p] << l) + s; 1115 l = ht_count[tbl][1][p]; 1116 putlong( d, l ); 1117 sumbit += l; 1118 } 1119 1120 /* flush remaining bits */ 1121 putbits(cc, sz); 1122 1123 return sumbit; 1124} 1125 1126/* Implements the pseudocode of page 98 of the IS */ 1127int HuffmanCode(short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table) 1128{ 1129 uint32_t cc=0, sz=0, code; 1130 uint32_t i, xl=0, yl=0, idx; 1131 int x, y, bit, sumbit=0; 1132 #define sign_x xr_sign[i+0] 1133 #define sign_y xr_sign[i+1] 1134 1135 if(table == 0) 1136 return 0; 1137 1138 if( table > 15 ) 1139 { /* ESC-table is used */ 1140 uint32_t linbits = ht_big[table-16].linbits; 1141 const uint16_t *hffcode = table < 24 ? t16HB : t24HB; 1142 const uint8_t *hlen = table < 24 ? t16l : t24l; 1143 1144 for(i=begin; i<end; i+=2) 1145 { 1146 x = ix[ i ]; 1147 y = ix[i+1]; 1148 1149 if(x > 14) { xl = x - 15; x = 15; } 1150 if(y > 14) { yl = y - 15; y = 15; } 1151 1152 idx = x * 16 + y; 1153 code = hffcode[idx]; 1154 bit = hlen [idx]; 1155 1156 if(x) 1157 { 1158 if(x > 14) 1159 { 1160 code = (code << linbits) | xl; 1161 bit += linbits; 1162 } 1163 1164 code = (code << 1) | sign_x; 1165 bit += 1; 1166 } 1167 1168 if(y) 1169 { 1170 if(y > 14) 1171 { 1172 if(bit + linbits + 1 > 32) 1173 { 1174 putlong( code, bit ); 1175 sumbit += bit; 1176 code = bit = 0; 1177 } 1178 1179 code = (code << linbits) | yl; 1180 bit += linbits; 1181 } 1182 1183 code = (code << 1) | sign_y; 1184 bit += 1; 1185 } 1186 1187 putlong( code, bit ); 1188 sumbit += bit; 1189 } 1190 } 1191 else 1192 { /* No ESC-words */ 1193 const struct huffcodetab *h = &ht[table]; 1194 1195 for(i=begin; i<end; i+=2) 1196 { 1197 x = ix[i]; 1198 y = ix[i+1]; 1199 1200 idx = x * h->len + y; 1201 code = h->table[idx]; 1202 bit = h->hlen [idx]; 1203 1204 if(x) 1205 { 1206 code = (code << 1) | sign_x; 1207 bit += 1; 1208 } 1209 1210 if(y) 1211 { 1212 code = (code << 1) | sign_y; 1213 bit += 1; 1214 } 1215 1216 putlong( code, bit ); 1217 sumbit += bit; 1218 } 1219 } 1220 1221 /* flush remaining bits */ 1222 putbits(cc, sz); 1223 1224 return sumbit; 1225} 1226 1227void putbits(uint32_t val, uint32_t nbit) 1228{ 1229 int new_bitpos = CodedData.bitpos + nbit; 1230 int ptrpos = CodedData.bitpos >> 5; 1231 1232 val = val & (0xffffffff >> (32 - nbit)); 1233 1234 /* data fit in one uint32_t */ 1235 if(((new_bitpos - 1) >> 5) == ptrpos) 1236 CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31); 1237 else 1238 { 1239 CodedData.bbuf[ptrpos ] |= val >> ((new_bitpos - 32) & 31); 1240 CodedData.bbuf[ptrpos+1] |= val << ((32 - new_bitpos) & 31); 1241 } 1242 1243 CodedData.bitpos = new_bitpos; 1244} 1245 1246/***************************************************************************/ 1247/* Choose the Huffman table that will encode ix[begin..end] with */ 1248/* the fewest bits. */ 1249/* Note: This code contains knowledge about the sizes and characteristic */ 1250/* of the Huffman tables as defined in the IS (Table B.7), and will not */ 1251/* work with any arbitrary tables. */ 1252/***************************************************************************/ 1253static int choose_table( short *ix, uint32_t begin, uint32_t end, int *bits ) 1254{ 1255 uint32_t i; 1256 int max, table0, table1; 1257 1258 for(i=begin,max=0; i<end; i++) 1259 if(ix[i] > max) 1260 max = ix[i]; 1261 1262 if(max < 16) 1263 { 1264 /* tables without linbits */ 1265 /* indx: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 1266 /* len: 0, 2, 3, 3, 0, 4, 4, 6, 6, 6, 8, 8, 8, 16, 0, 16 */ 1267 switch(max) 1268 { 1269 case 0: return 0; 1270 case 1: return count_bit1(ix, begin, end, bits); 1271 case 2: return 2 + find_best_2(ix, begin, end, tab23, 3, bits); 1272 case 3: return 5 + find_best_2(ix, begin, end, tab56, 4, bits); 1273 case 4: 1274 case 5: return 7 + find_best_3(ix, begin, end, tab789, 6, bits); 1275 case 6: 1276 case 7: return 10 + find_best_3(ix, begin, end, tabABC, 8, bits); 1277 default: return 13 + find_best_2(ix, begin, end, tab1315, 16, bits) * 2; 1278 } 1279 } 1280 else 1281 { 1282 /* tables with linbits */ 1283 max -= 15; 1284 1285 for(table0=0; table0<8; table0++) 1286 if(ht_big[table0].linmax >= max) 1287 break; 1288 1289 for(table1=8; table1<16; table1++) 1290 if(ht_big[table1].linmax >= max) 1291 break; 1292 1293 return 16 + count_bigv(ix, begin, end, table0, table1, bits); 1294 } 1295} 1296 1297int find_best_2(short *ix, uint32_t start, uint32_t end, const uint32_t *table, 1298 uint32_t len, int *bits) 1299{ 1300 uint32_t i, sum = 0; 1301 1302 for(i=start; i<end; i+=2) 1303 sum += table[ix[i] * len + ix[i+1]]; 1304 1305 if((sum & 0xffff) <= (sum >> 16)) 1306 { 1307 *bits = (sum & 0xffff); 1308 return 1; 1309 } 1310 else 1311 { 1312 *bits = sum >> 16; 1313 return 0; 1314 } 1315} 1316 1317int find_best_3(short *ix, uint32_t start, uint32_t end, const uint32_t *table, 1318 uint32_t len, int *bits) 1319{ 1320 uint32_t i, j, sum = 0; 1321 int sum1 = 0; 1322 int sum2 = 0; 1323 int sum3 = 0; 1324 1325 /* avoid overflow in packed additions: 78*13 < 1024 */ 1326 for(i=start; i<end; ) 1327 { 1328 j = i + 2*78 > end ? end : i + 2*78; 1329 1330 for(sum=0; i<j; i+=2) 1331 sum += table[ix[i] * len + ix[i+1]]; 1332 1333 sum1 += (sum >> 20); 1334 sum2 += (sum >> 10) & 0x3ff; 1335 sum3 += (sum >> 0) & 0x3ff; 1336 } 1337 1338 i = 0; 1339 if(sum1 > sum2) { sum1 = sum2; i = 1; } 1340 if(sum1 > sum3) { sum1 = sum3; i = 2; } 1341 1342 *bits = sum1; 1343 1344 return i; 1345} 1346 1347/*************************************************************************/ 1348/* Function: Count the number of bits necessary to code the subregion. */ 1349/*************************************************************************/ 1350int count_bit1(short *ix, uint32_t start, uint32_t end, int *bits ) 1351{ 1352 uint32_t i, sum = 0; 1353 1354 for(i=start; i<end; i+=2) 1355 sum += t1l[4 + ix[i] * 2 + ix[i+1]]; 1356 1357 *bits = sum; 1358 1359 return 1; /* this is table1 */ 1360} 1361 1362int count_bigv(short *ix, uint32_t start, uint32_t end, int table0, 1363 int table1, int *bits ) 1364{ 1365 uint32_t i, sum0, sum1, sum=0, bigv=0, x, y; 1366 1367 /* ESC-table is used */ 1368 for(i=start; i<end; i+=2) 1369 { 1370 x = ix[i]; 1371 y = ix[i+1]; 1372 1373 if(x > 14) { x = 15; bigv++; } 1374 if(y > 14) { y = 15; bigv++; } 1375 1376 sum += tab1624[x * 16 + y]; 1377 } 1378 1379 sum0 = (sum >> 16) + bigv * ht_big[table0].linbits; 1380 sum1 = (sum & 0xffff) + bigv * ht_big[table1].linbits; 1381 1382 if(sum0 <= sum1) 1383 { 1384 *bits = sum0; 1385 return table0; 1386 } 1387 else 1388 { 1389 *bits = sum1; 1390 return table1; 1391 } 1392} 1393 1394/*************************************************************************/ 1395/* Function: Calculation of rzero, count1, address3 */ 1396/* (Partitions ix into big values, quadruples and zeros). */ 1397/*************************************************************************/ 1398static int calc_runlen( short *ix, side_info_t *si ) 1399{ 1400 int p, i, sum = 0; 1401 1402 for(i=SAMPL2; i-=2; ) 1403 if(*(uint32_t*)&ix[i-2]) /* !!!! short *ix; !!!!! */ 1404 break; 1405 1406 si->count1 = 0; 1407 1408 for( ; i>3; i-=4) 1409 { 1410 int v = ix[i-1]; 1411 int w = ix[i-2]; 1412 int x = ix[i-3]; 1413 int y = ix[i-4]; 1414 1415 if((v | w | x | y) <= 1) 1416 { 1417 p = (y<<3) + (x<<2) + (w<<1) + (v); 1418 1419 sum += tab01[p]; 1420 1421 si->count1++; 1422 } 1423 else break; 1424 } 1425 1426 si->address3 = i; 1427 1428 if((sum >> 16) < (sum & 0xffff)) 1429 { 1430 si->table_select[3] = 0; 1431 return sum >> 16; 1432 } 1433 else 1434 { 1435 si->table_select[3] = 1; 1436 return sum & 0xffff; 1437 } 1438} 1439 1440 1441/*************************************************************************/ 1442/* Function: Quantization of the vector xr ( -> ix) */ 1443/*************************************************************************/ 1444static int quantize_int(int *xr, short *ix, side_info_t *si) 1445{ 1446 unsigned int i, idx, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 }; 1447 1448 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4; 1449 1450 /* check for possible 'out of range' values */ 1451 if(((si->max_val + 256) >> 8) * s >= (65536 << 8)) 1452 return 0; 1453 1454 if(((si->max_val + 256) >> 8) * s < (4096 << 8)) 1455 { /* all values fit the table size */ 1456 for(i=SAMPL2; i--; ) 1457 ix[i] = int2idx[(xr[i] * s + 0x8000) >> 16]; 1458 } 1459 else 1460 { /* check each index wether it fits the table */ 1461 for(i=SAMPL2; i--; ) 1462 { 1463 idx = (xr[i] * s + 0x08000) >> 16; 1464 1465 if(idx > 4095) ix[i] = int2idx[(idx + 8) >> 4] << 3; 1466 else ix[i] = int2idx[idx]; 1467 } 1468 } 1469 1470 return 1; 1471} 1472 1473/*************************************************************************/ 1474/* subdivides the bigvalue region which will use separate Huffman tables */ 1475/*************************************************************************/ 1476static void subdivide(side_info_t *si) 1477{ 1478 int scfb, count0, count1; 1479 1480 if( !si->address3 ) 1481 { /* no bigvalue region */ 1482 si->region_0_1 = 0; 1483 si->address1 = 0; 1484 si->address2 = 0; 1485 } 1486 else 1487 { 1488 /* Calculate scale factor band index */ 1489 for(scfb=0; scalefac[scfb] < si->address3; ) 1490 scfb++; 1491 1492 count0 = subdv_table[scfb].region0_cnt; 1493 count1 = subdv_table[scfb].region1_cnt; 1494 1495 si->region_0_1 = (count0 << 3) | count1; 1496 si->address1 = scalefac[count0 + 1]; 1497 si->address2 = scalefac[count0 + 1 + count1 + 1]; 1498 } 1499} 1500 1501/*******************************************************************/ 1502/* Count the number of bits necessary to code the bigvalues region */ 1503/*******************************************************************/ 1504static int bigv_bitcount(short *ix, side_info_t *gi) 1505{ 1506 int b1=0, b2=0, b3=0; 1507 1508 /* Select huffman code tables for bigvalues regions */ 1509 gi->table_select[0] = 0; 1510 gi->table_select[1] = 0; 1511 gi->table_select[2] = 0; 1512 1513 if( gi->address1 > 0 ) /* region0 */ 1514 gi->table_select[0] = choose_table(ix, 0 , gi->address1, &b1); 1515 1516 if( gi->address2 > gi->address1 ) /* region1 */ 1517 gi->table_select[1] = choose_table(ix, gi->address1, gi->address2, &b2); 1518 1519 if( gi->address3 > gi->address2 ) /* region2 */ 1520 gi->table_select[2] = choose_table(ix, gi->address2, gi->address3, &b3); 1521 1522 return b1+b2+b3; 1523} 1524 1525static int quantize_and_count_bits(int *xr, short *ix, side_info_t *si) 1526{ 1527 int bits = 10000; 1528 1529 if(quantize_int(xr, ix, si)) 1530 { 1531 bits = calc_runlen(ix, si); /* rzero,count1,address3 */ 1532 subdivide(si); /* bigvalues sfb division */ 1533 bits += bigv_bitcount(ix,si); /* bit count */ 1534 } 1535 1536 return bits; 1537} 1538 1539/************************************************************************/ 1540/* The code selects the best quantStep for a particular set of scalefacs*/ 1541/************************************************************************/ 1542static int inner_loop(int *xr, int max_bits, side_info_t *si) 1543{ 1544 int bits; 1545 1546 while((bits=quantize_and_count_bits(xr, enc_data, si)) < max_bits-64) 1547 { 1548 if(si->quantStep == 0) 1549 break; 1550 1551 if(si->quantStep <= 2) 1552 si->quantStep = 0; 1553 else 1554 si->quantStep -= 2; 1555 } 1556 1557 while(bits > max_bits) 1558 { 1559 si->quantStep++; 1560 bits = quantize_and_count_bits(xr, enc_data, si); 1561 } 1562 1563 return bits; 1564} 1565 1566static void iteration_loop(int *xr, side_info_t *si, int gr_cnt) 1567{ 1568 int remain, tar_bits, max_bits = cfg.mean_bits; 1569 1570 /* distribute reserved bits to remaining granules */ 1571 tar_bits = max_bits + (cfg.ResvSize / gr_cnt & ~7); 1572 if(tar_bits > max_bits + max_bits/2) 1573 tar_bits = max_bits + max_bits/2; 1574 1575 si->part2_3_length = inner_loop(xr, tar_bits, si); 1576 si->global_gain = si->quantStep + 142 - si->additStep; 1577 1578 /* unused bits of the reservoir can be used for remaining granules */ 1579 cfg.ResvSize += max_bits - si->part2_3_length; 1580 1581 /* end: distribute the reserved bits to one or two granules */ 1582 if(gr_cnt == 1) 1583 { 1584 si->part2_3_length += cfg.ResvSize; 1585 /* mp3 format allows max 12bits for granule length */ 1586 if(si->part2_3_length > 4092) 1587 { 1588 remain = (si->part2_3_length - 4092 + 31) >> 5; 1589 si->part2_3_length -= remain << 5; 1590 si[-1].part2_3_length += remain << 5; 1591 1592 while(remain--) 1593 putbits(~0, 32); 1594 } 1595 } 1596} 1597 1598 1599/* returns sum_j=0^31 a[j]*cos(PI*j*(k+1/2)/32), 0<=k<32 */ 1600void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT]) ICODE_ATTR; 1601void window_subband1(short *wk, int sb0[SBLIMIT], int sb1[SBLIMIT]) 1602{ 1603 int k, i, u, v; 1604 short *x1, *x2; 1605 short const *wp; 1606 1607#ifdef CPU_COLDFIRE 1608 int s0, s1, t0, t1; 1609 1610 for(k=0; k<18; k++, wk+=64, sb0+=SBLIMIT, sb1+=SBLIMIT) 1611 { 1612 wp = enwindow; 1613 x1 = wk; 1614 x2 = x1 - 124; 1615 1616 for(i=-15; i<0; i++) 1617 { 1618 asm volatile( 1619 "move.l (-224*4,%[x2]), %%d4\n" /* d4 = x2[-224] */ 1620 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */ 1621 "mac.w %%d0u, %%d4u, %%acc0\n" 1622 "mac.w %%d0u, %%d4l, (-160*4,%[x2]), %%d4, %%acc1\n" 1623 "mac.w %%d0l, %%d4u, %%acc0\n" 1624 "mac.w %%d0l, %%d4l, ( -96*4,%[x2]), %%d4, %%acc1\n" 1625 "mac.w %%d1u, %%d4u, %%acc0\n" 1626 "mac.w %%d1u, %%d4l, ( -32*4,%[x2]), %%d4, %%acc1\n" 1627 "mac.w %%d1l, %%d4u, %%acc0\n" 1628 "mac.w %%d1l, %%d4l, ( 32*4,%[x2]), %%d4, %%acc1\n" 1629 "mac.w %%d2u, %%d4u, %%acc0\n" 1630 "mac.w %%d2u, %%d4l, ( 96*4,%[x2]), %%d4, %%acc1\n" 1631 "mac.w %%d2l, %%d4u, %%acc0\n" 1632 "mac.w %%d2l, %%d4l, ( 160*4,%[x2]), %%d4, %%acc1\n" 1633 "mac.w %%d3u, %%d4u, %%acc0\n" 1634 "mac.w %%d3u, %%d4l, ( 224*4,%[x2]), %%d4, %%acc1\n" 1635 "mac.w %%d3l, %%d4u, %%acc0\n" 1636 "mac.w %%d3l, %%d4l, (-256*4,%[x1]), %%d4, %%acc1\n" 1637 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */ 1638 "mac.w %%d0u, %%d4u, %%acc0\n" 1639 "mac.w %%d0u, %%d4l, (-192*4,%[x1]), %%d4, %%acc1\n" 1640 "mac.w %%d0l, %%d4u, %%acc0\n" 1641 "mac.w %%d0l, %%d4l, (-128*4,%[x1]), %%d4, %%acc1\n" 1642 "mac.w %%d1u, %%d4u, %%acc0\n" 1643 "mac.w %%d1u, %%d4l, ( -64*4,%[x1]), %%d4, %%acc1\n" 1644 "mac.w %%d1l, %%d4u, %%acc0\n" 1645 "mac.w %%d1l, %%d4l, ( 0*4,%[x1]), %%d4, %%acc1\n" 1646 "mac.w %%d2u, %%d4u, %%acc0\n" 1647 "mac.w %%d2u, %%d4l, ( 64*4,%[x1]), %%d4, %%acc1\n" 1648 "mac.w %%d2l, %%d4u, %%acc0\n" 1649 "mac.w %%d2l, %%d4l, ( 128*4,%[x1]), %%d4, %%acc1\n" 1650 "mac.w %%d3u, %%d4u, %%acc0\n" 1651 "mac.w %%d3u, %%d4l, ( 192*4,%[x1]), %%d4, %%acc1\n" 1652 "mac.w %%d3l, %%d4u, %%acc0\n" 1653 "mac.w %%d3l, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n" 1654 "movclr.l %%acc0, %%d0\n" 1655 "move.l %%d0, %[s0]\n" 1656 "movclr.l %%acc1, %%d0\n" 1657 "move.l %%d0, %[s1]\n" 1658 1659 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */ 1660 "mac.w %%d0u, %%d4u, %%acc0\n" 1661 "mac.w %%d0u, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n" 1662 "mac.w %%d0l, %%d4u, %%acc0\n" 1663 "mac.w %%d0l, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n" 1664 "mac.w %%d1u, %%d4u, %%acc0\n" 1665 "mac.w %%d1u, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n" 1666 "mac.w %%d1l, %%d4u, %%acc0\n" 1667 "mac.w %%d1l, %%d4l, ( -32*4,%[x1]), %%d4, %%acc1\n" 1668 "mac.w %%d2u, %%d4u, %%acc0\n" 1669 "mac.w %%d2u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n" 1670 "mac.w %%d2l, %%d4u, %%acc0\n" 1671 "mac.w %%d2l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n" 1672 "mac.w %%d3u, %%d4u, %%acc0\n" 1673 "mac.w %%d3u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n" 1674 "mac.w %%d3l, %%d4u, %%acc0\n" 1675 "mac.w %%d3l, %%d4l, ( 256*4,%[x2]), %%d4, %%acc1\n" 1676 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */ 1677 "mac.w %%d0u, %%d4u, %%acc0\n" 1678 "mac.w %%d0u, %%d4l, ( 192*4,%[x2]), %%d4, %%acc1\n" 1679 "mac.w %%d0l, %%d4u, %%acc0\n" 1680 "mac.w %%d0l, %%d4l, ( 128*4,%[x2]), %%d4, %%acc1\n" 1681 "mac.w %%d1u, %%d4u, %%acc0\n" 1682 "mac.w %%d1u, %%d4l, ( 64*4,%[x2]), %%d4, %%acc1\n" 1683 "mac.w %%d1l, %%d4u, %%acc0\n" 1684 "mac.w %%d1l, %%d4l, ( 0*4,%[x2]), %%d4, %%acc1\n" 1685 "mac.w %%d2u, %%d4u, %%acc0\n" 1686 "mac.w %%d2u, %%d4l, ( -64*4,%[x2]), %%d4, %%acc1\n" 1687 "mac.w %%d2l, %%d4u, %%acc0\n" 1688 "mac.w %%d2l, %%d4l, (-128*4,%[x2]), %%d4, %%acc1\n" 1689 "mac.w %%d3u, %%d4u, %%acc0\n" 1690 "mac.w %%d3u, %%d4l, (-192*4,%[x2]), %%d4, %%acc1\n" 1691 "mac.w %%d3l, %%d4u, %%acc0\n" 1692 "mac.w %%d3l, %%d4l, %%acc1\n" 1693 "movclr.l %%acc0, %%d0\n" 1694 "move.l %%d0, %[t0]\n" 1695 "movclr.l %%acc1, %%d0\n" 1696 "move.l %%d0, %[t1]\n" 1697 1698 : [x1] "+a" (x1), [x2] "+a" (x2), [s0] "+m" (s0), [t0] "+m" (t0), 1699 [s1] "+m" (s1), [t1] "+m" (t1) 1700 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4"); 1701 1702 sb0[30+i*2] = shft4(t0) + shft13(s0) * wp[24]; 1703 sb0[31+i*2] = shft13(t0) * wp[25] - shft13(s0) * wp[26]; 1704 sb1[30+i*2] = shft4(t1) + shft13(s1) * wp[24]; 1705 sb1[31+i*2] = shft13(t1) * wp[25] - shft13(s1) * wp[26]; 1706 wp += 27; 1707 x1 -= 2; 1708 x2 += 2; 1709 } 1710 1711 asm volatile( 1712 "move.l ( -32*4,%[x1]), %%d4\n" /* d4 = x1[-32] */ 1713 "movem.l (%[wp]), %%d0-%%d3\n" /* load 8 values */ 1714 1715 "mac.w %%d0u, %%d4u, %%acc0\n" 1716 "mac.w %%d0u, %%d4l, ( -96*4,%[x1]), %%d4, %%acc1\n" 1717 "mac.w %%d0l, %%d4u, %%acc0\n" 1718 "mac.w %%d0l, %%d4l, (-160*4,%[x1]), %%d4, %%acc1\n" 1719 "mac.w %%d1u, %%d4u, %%acc0\n" 1720 "mac.w %%d1u, %%d4l, (-224*4,%[x1]), %%d4, %%acc1\n" 1721 "mac.w %%d1l, %%d4u, %%acc0\n" 1722 "mac.w %%d1l, %%d4l, ( 32*4,%[x1]), %%d4, %%acc1\n" 1723 "mac.w %%d2u, %%d4u, %%acc0\n" 1724 "mac.w %%d2u, %%d4l, ( 96*4,%[x1]), %%d4, %%acc1\n" 1725 "mac.w %%d2l, %%d4u, %%acc0\n" 1726 "mac.w %%d2l, %%d4l, ( 160*4,%[x1]), %%d4, %%acc1\n" 1727 "mac.w %%d3u, %%d4u, %%acc0\n" 1728 "mac.w %%d3u, %%d4l, ( 224*4,%[x1]), %%d4, %%acc1\n" 1729 "mac.w %%d3l, %%d4u, %%acc0\n" 1730 "mac.w %%d3l, %%d4l, ( -16*4,%[x1]), %%d4, %%acc1\n" 1731 "movclr.l %%acc0, %%d0\n" 1732 "move.l %%d0, %[s0]\n" 1733 "movclr.l %%acc1, %%d0\n" 1734 "move.l %%d0, %[s1]\n" 1735 1736 "movem.l (16,%[wp]), %%d0-%%d3\n" /* load 8 values */ 1737 "mac.w %%d0u, %%d4u, %%acc0\n" 1738 "mac.w %%d0u, %%d4l, ( -48*4,%[x1]), %%d4, %%acc1\n" 1739 "mac.w %%d1u, %%d4u, %%acc0\n" 1740 "mac.w %%d1u, %%d4l, ( 16*4,%[x1]), %%d4, %%acc1\n" 1741 "mac.w %%d1l, %%d4u, %%acc0\n" 1742 "mac.w %%d1l, %%d4l, ( -80*4,%[x1]), %%d4, %%acc1\n" 1743 "mac.w %%d2u, %%d4u, %%acc0\n" 1744 "mac.w %%d2u, %%d4l, ( 48*4,%[x1]), %%d4, %%acc1\n" 1745 "mac.w %%d2u, %%d4u, %%acc0\n" 1746 "mac.w %%d2u, %%d4l, (-112*4,%[x1]), %%d4, %%acc1\n" 1747 "mac.w %%d3u, %%d4u, %%acc0\n" 1748 "mac.w %%d3u, %%d4l, ( 80*4,%[x1]), %%d4, %%acc1\n" 1749 "mac.w %%d3l, %%d4u, %%acc0\n" 1750 "mac.w %%d3l, %%d4l, (-144*4,%[x1]), %%d4, %%acc1\n" 1751 "movem.l (32,%[wp]), %%d0-%%d3\n" /* load 8 values */ 1752 "mac.w %%d0u, %%d4u, %%acc0\n" 1753 "mac.w %%d0u, %%d4l, ( 112*4,%[x1]), %%d4, %%acc1\n" 1754 "mac.w %%d0u, %%d4u, %%acc0\n" 1755 "mac.w %%d0u, %%d4l, (-176*4,%[x1]), %%d4, %%acc1\n" 1756 "mac.w %%d1u, %%d4u, %%acc0\n" 1757 "mac.w %%d1u, %%d4l, ( 144*4,%[x1]), %%d4, %%acc1\n" 1758 "mac.w %%d1l, %%d4u, %%acc0\n" 1759 "mac.w %%d1l, %%d4l, (-208*4,%[x1]), %%d4, %%acc1\n" 1760 "mac.w %%d2u, %%d4u, %%acc0\n" 1761 "mac.w %%d2u, %%d4l, ( 176*4,%[x1]), %%d4, %%acc1\n" 1762 "mac.w %%d2u, %%d4u, %%acc0\n" 1763 "mac.w %%d2u, %%d4l, (-240*4,%[x1]), %%d4, %%acc1\n" 1764 "mac.w %%d3u, %%d4u, %%acc0\n" 1765 "mac.w %%d3u, %%d4l, ( 208*4,%[x1]), %%d4, %%acc1\n" 1766 "mac.w %%d3l, %%d4u, %%acc0\n" 1767 "mac.w %%d3l, %%d4l, %%acc1\n" 1768 "movclr.l %%acc0, %%d0\n" 1769 "move.l %%d0, %[t0]\n" 1770 "movclr.l %%acc1, %%d0\n" 1771 "move.l %%d0, %[t1]\n" 1772 1773 : [x1] "+a" (x1), [s0] "+m" (s0), [t0] "+m" (t0), 1774 [s1] "+m" (s1), [t1] "+m" (t1) 1775 : [wp] "a" (wp) : "d0", "d1", "d2", "d3", "d4"); 1776 1777 u = shft4(s0 - t0); 1778 v = shft4(s0 + t0); 1779 t0 = sb0[14]; 1780 s0 = sb0[15] - t0; 1781 1782 sb0[31] = v + t0; /* A0 */ 1783 sb0[30] = u + s0; /* A1 */ 1784 sb0[15] = u - s0; /* A2 */ 1785 sb0[14] = v - t0; /* A3 */ 1786 1787 u = shft4(s1 - t1); 1788 v = shft4(s1 + t1); 1789 t1 = sb1[14]; 1790 s1 = sb1[15] - t1; 1791 1792 sb1[31] = v + t1; /* A0 */ 1793 sb1[30] = u + s1; /* A1 */ 1794 sb1[15] = u - s1; /* A2 */ 1795 sb1[14] = v - t1; /* A3 */ 1796 } 1797#else 1798 int ch, s, t, *a; 1799 1800 for(ch=0; ch<cfg.channels; ch++) 1801 { 1802 a = ch ? sb1 : sb0; 1803 for(k=0; k<18; k++, wk+=64, a+=SBLIMIT) 1804 { 1805 wp = enwindow; 1806 x1 = wk; 1807 x2 = x1 - 124; 1808 1809 /* x1[-572] .... x1[448] = 1022 */ 1810 /* 18*4*16*32 */ 1811 for(i=-15; i<0; i++) 1812 { 1813 s = (int)x2[-224*2] * wp[ 0]; t = (int)x1[ 224*2] * wp[ 0]; 1814 s += (int)x2[-160*2] * wp[ 1]; t += (int)x1[ 160*2] * wp[ 1]; 1815 s += (int)x2[- 96*2] * wp[ 2]; t += (int)x1[ 96*2] * wp[ 2]; 1816 s += (int)x2[- 32*2] * wp[ 3]; t += (int)x1[ 32*2] * wp[ 3]; 1817 s += (int)x2[ 32*2] * wp[ 4]; t += (int)x1[- 32*2] * wp[ 4]; 1818 s += (int)x2[ 96*2] * wp[ 5]; t += (int)x1[- 96*2] * wp[ 5]; 1819 s += (int)x2[ 160*2] * wp[ 6]; t += (int)x1[-160*2] * wp[ 6]; 1820 s += (int)x2[ 224*2] * wp[ 7]; t += (int)x1[-224*2] * wp[ 7]; 1821 s += (int)x1[-256*2] * wp[ 8]; t += (int)x2[ 256*2] * wp[16]; 1822 s += (int)x1[-192*2] * wp[ 9]; t += (int)x2[ 192*2] * wp[17]; 1823 s += (int)x1[-128*2] * wp[10]; t += (int)x2[ 128*2] * wp[18]; 1824 s += (int)x1[- 64*2] * wp[11]; t += (int)x2[ 64*2] * wp[19]; 1825 s += (int)x1[ 0*2] * wp[12]; t += (int)x2[ 0*2] * wp[20]; 1826 s += (int)x1[ 64*2] * wp[13]; t += (int)x2[- 64*2] * wp[21]; 1827 s += (int)x1[ 128*2] * wp[14]; t += (int)x2[-128*2] * wp[22]; 1828 s += (int)x1[ 192*2] * wp[15]; t += (int)x2[-192*2] * wp[23]; 1829 1830 a[30+i*2] = shft4(t) + shft13(s) * wp[24]; 1831 a[31+i*2] = shft13(t) * wp[25] - shft13(s) * wp[26]; 1832 wp += 27; 1833 x1 -= 2; 1834 x2 += 2; 1835 } 1836 1837 t = (int)x1[- 16*2] * wp[ 8]; s = (int)x1[ -32*2] * wp[0]; 1838 t += ((int)x1[- 48*2]-x1[ 16*2]) * wp[10]; s += (int)x1[ -96*2] * wp[1]; 1839 t += ((int)x1[- 80*2]+x1[ 48*2]) * wp[12]; s += (int)x1[-160*2] * wp[2]; 1840 t += ((int)x1[-112*2]-x1[ 80*2]) * wp[14]; s += (int)x1[-224*2] * wp[3]; 1841 t += ((int)x1[-144*2]+x1[112*2]) * wp[16]; s += (int)x1[ 32*2] * wp[4]; 1842 t += ((int)x1[-176*2]-x1[144*2]) * wp[18]; s += (int)x1[ 96*2] * wp[5]; 1843 t += ((int)x1[-208*2]+x1[176*2]) * wp[20]; s += (int)x1[ 160*2] * wp[6]; 1844 t += ((int)x1[-240*2]-x1[208*2]) * wp[22]; s += (int)x1[ 224*2] * wp[7]; 1845 1846 u = shft4(s - t); 1847 v = shft4(s + t); 1848 t = a[14]; 1849 s = a[15] - t; 1850 1851 a[31] = v + t; /* A0 */ 1852 a[30] = u + s; /* A1 */ 1853 a[15] = u - s; /* A2 */ 1854 a[14] = v - t; /* A3 */ 1855 } 1856 wk -= 18 * 64 - 1; /* rewind wk (to next channel start) */ 1857 } 1858#endif 1859} 1860 1861void window_subband2(short *x1, int a[SBLIMIT]) ICODE_ATTR; 1862void window_subband2(short *x1, int a[SBLIMIT]) 1863{ 1864 int xr; 1865 short const *wp = enwindow; 1866 short *x2 = x1 - 124; 1867 1868 wp += 27 * 15; 1869 x1 -= 2 * 15; 1870 x2 += 2 * 15; 1871 1872 xr = a[28] - a[0]; a[0] += a[28]; a[28] = shft9(xr) * wp[-2*27+25]; 1873 xr = a[29] - a[1]; a[1] += a[29]; a[29] = shft9(xr) * wp[-2*27+25]; 1874 xr = a[26] - a[2]; a[2] += a[26]; a[26] = shft9(xr) * wp[-4*27+25]; 1875 xr = a[27] - a[3]; a[3] += a[27]; a[27] = shft9(xr) * wp[-4*27+25]; 1876 xr = a[24] - a[4]; a[4] += a[24]; a[24] = shft9(xr) * wp[-6*27+25]; 1877 xr = a[25] - a[5]; a[5] += a[25]; a[25] = shft9(xr) * wp[-6*27+25]; 1878 xr = a[22] - a[6]; a[6] += a[22]; a[22] = shft9(xr) * SQRT ; 1879 xr = a[23] - a[7]; a[7] += a[23]; a[23] = shft9(xr) * SQRT - a[7]; 1880 a[ 7] -= a[ 6]; 1881 a[22] -= a[ 7]; 1882 a[23] -= a[22]; 1883 1884 xr = a[ 6]; a[ 6] = a[31] - xr; a[31] = a[31] + xr; 1885 xr = a[ 7]; a[ 7] = a[30] - xr; a[30] = a[30] + xr; 1886 xr = a[22]; a[22] = a[15] - xr; a[15] = a[15] + xr; 1887 xr = a[23]; a[23] = a[14] - xr; a[14] = a[14] + xr; 1888 1889 xr = a[20] - a[ 8]; a[ 8] += a[20]; a[20] = shft9(xr) * wp[-10*27+25]; 1890 xr = a[21] - a[ 9]; a[ 9] += a[21]; a[21] = shft9(xr) * wp[-10*27+25]; 1891 xr = a[18] - a[10]; a[10] += a[18]; a[18] = shft9(xr) * wp[-12*27+25]; 1892 xr = a[19] - a[11]; a[11] += a[19]; a[19] = shft9(xr) * wp[-12*27+25]; 1893 xr = a[16] - a[12]; a[12] += a[16]; a[16] = shft9(xr) * wp[-14*27+25]; 1894 xr = a[17] - a[13]; a[13] += a[17]; a[17] = shft9(xr) * wp[-14*27+25]; 1895 xr =-a[20] + a[24]; a[20] += a[24]; a[24] = shft9(xr) * wp[-12*27+25]; 1896 xr =-a[21] + a[25]; a[21] += a[25]; a[25] = shft9(xr) * wp[-12*27+25]; 1897 xr = a[ 4] - a[ 8]; a[ 4] += a[ 8]; a[ 8] = shft9(xr) * wp[-12*27+25]; 1898 xr = a[ 5] - a[ 9]; a[ 5] += a[ 9]; a[ 9] = shft9(xr) * wp[-12*27+25]; 1899 xr = a[ 0] - a[12]; a[ 0] += a[12]; a[12] = shft9(xr) * wp[ -4*27+25]; 1900 xr = a[ 1] - a[13]; a[ 1] += a[13]; a[13] = shft9(xr) * wp[ -4*27+25]; 1901 xr = a[16] - a[28]; a[16] += a[28]; a[28] = shft9(xr) * wp[ -4*27+25]; 1902 xr =-a[17] + a[29]; a[17] += a[29]; a[29] = shft9(xr) * wp[ -4*27+25]; 1903 1904 xr = SQRT * shft9(a[ 2] - a[10]); a[ 2] += a[10]; a[10] = xr; 1905 xr = SQRT * shft9(a[ 3] - a[11]); a[ 3] += a[11]; a[11] = xr; 1906 xr = SQRT * shft9(a[26] - a[18]); a[18] += a[26]; a[26] = xr - a[18]; 1907 xr = SQRT * shft9(a[27] - a[19]); a[19] += a[27]; a[27] = xr - a[19]; 1908 1909 xr = a[ 2]; a[19] -= a[ 3]; a[ 3] -= xr; a[ 2] = a[31] - xr; a[31] += xr; 1910 xr = a[ 3]; a[11] -= a[19]; a[18] -= xr; a[ 3] = a[30] - xr; a[30] += xr; 1911 xr = a[18]; a[27] -= a[11]; a[19] -= xr; a[18] = a[15] - xr; a[15] += xr; 1912 1913 xr = a[19]; a[10] -= xr; a[19] = a[14] - xr; a[14] += xr; 1914 xr = a[10]; a[11] -= xr; a[10] = a[23] - xr; a[23] += xr; 1915 xr = a[11]; a[26] -= xr; a[11] = a[22] - xr; a[22] += xr; 1916 xr = a[26]; a[27] -= xr; a[26] = a[ 7] - xr; a[ 7] += xr; 1917 1918 xr = a[27]; a[27] = a[6] - xr; a[6] += xr; 1919 1920 xr = SQRT * shft9(a[ 0] - a[ 4]); a[ 0] += a[ 4]; a[ 4] = xr; 1921 xr = SQRT * shft9(a[ 1] - a[ 5]); a[ 1] += a[ 5]; a[ 5] = xr; 1922 xr = SQRT * shft9(a[16] - a[20]); a[16] += a[20]; a[20] = xr; 1923 xr = SQRT * shft9(a[17] - a[21]); a[17] += a[21]; a[21] = xr; 1924 xr =-SQRT * shft9(a[ 8] - a[12]); a[ 8] += a[12]; a[12] = xr - a[ 8]; 1925 xr =-SQRT * shft9(a[ 9] - a[13]); a[ 9] += a[13]; a[13] = xr - a[ 9]; 1926 xr =-SQRT * shft9(a[25] - a[29]); a[25] += a[29]; a[29] = xr - a[25]; 1927 xr =-SQRT * shft9(a[24] + a[28]); a[24] -= a[28]; a[28] = xr - a[24]; 1928 1929 xr = a[24] - a[16]; a[24] = xr; 1930 xr = a[20] - xr; a[20] = xr; 1931 xr = a[28] - xr; a[28] = xr; 1932 1933 xr = a[25] - a[17]; a[25] = xr; 1934 xr = a[21] - xr; a[21] = xr; 1935 xr = a[29] - xr; a[29] = xr; 1936 1937 xr = a[17] - a[1]; a[17] = xr; 1938 xr = a[ 9] - xr; a[ 9] = xr; 1939 xr = a[25] - xr; a[25] = xr; 1940 xr = a[ 5] - xr; a[ 5] = xr; 1941 xr = a[21] - xr; a[21] = xr; 1942 xr = a[13] - xr; a[13] = xr; 1943 xr = a[29] - xr; a[29] = xr; 1944 1945 xr = a[ 1] - a[0]; a[ 1] = xr; 1946 xr = a[16] - xr; a[16] = xr; 1947 xr = a[17] - xr; a[17] = xr; 1948 xr = a[ 8] - xr; a[ 8] = xr; 1949 xr = a[ 9] - xr; a[ 9] = xr; 1950 xr = a[24] - xr; a[24] = xr; 1951 xr = a[25] - xr; a[25] = xr; 1952 xr = a[ 4] - xr; a[ 4] = xr; 1953 xr = a[ 5] - xr; a[ 5] = xr; 1954 xr = a[20] - xr; a[20] = xr; 1955 xr = a[21] - xr; a[21] = xr; 1956 xr = a[12] - xr; a[12] = xr; 1957 xr = a[13] - xr; a[13] = xr; 1958 xr = a[28] - xr; a[28] = xr; 1959 xr = a[29] - xr; a[29] = xr; 1960 1961 xr = a[ 0]; a[ 0] += a[31]; a[31] -= xr; 1962 xr = a[ 1]; a[ 1] += a[30]; a[30] -= xr; 1963 xr = a[16]; a[16] += a[15]; a[15] -= xr; 1964 xr = a[17]; a[17] += a[14]; a[14] -= xr; 1965 xr = a[ 8]; a[ 8] += a[23]; a[23] -= xr; 1966 xr = a[ 9]; a[ 9] += a[22]; a[22] -= xr; 1967 xr = a[24]; a[24] += a[ 7]; a[ 7] -= xr; 1968 xr = a[25]; a[25] += a[ 6]; a[ 6] -= xr; 1969 xr = a[ 4]; a[ 4] += a[27]; a[27] -= xr; 1970 xr = a[ 5]; a[ 5] += a[26]; a[26] -= xr; 1971 xr = a[20]; a[20] += a[11]; a[11] -= xr; 1972 xr = a[21]; a[21] += a[10]; a[10] -= xr; 1973 xr = a[12]; a[12] += a[19]; a[19] -= xr; 1974 xr = a[13]; a[13] += a[18]; a[18] -= xr; 1975 xr = a[28]; a[28] += a[ 3]; a[ 3] -= xr; 1976 xr = a[29]; a[29] += a[ 2]; a[ 2] -= xr; 1977} 1978 1979void mdct_long(int *out, int *in) ICODE_ATTR; 1980void mdct_long(int *out, int *in) 1981{ 1982 int ct,st; 1983 int tc1, tc2, tc3, tc4, ts5, ts6, ts7, ts8; 1984 int ts1, ts2, ts3, ts4, tc5, tc6, tc7, tc8; 1985 1986 /* 1,2, 5,6, 9,10, 13,14, 17 */ 1987 tc1 = in[17] - in[ 9]; 1988 tc3 = in[15] - in[11]; 1989 tc4 = in[14] - in[12]; 1990 ts5 = in[ 0] + in[ 8]; 1991 ts6 = in[ 1] + in[ 7]; 1992 ts7 = in[ 2] + in[ 6]; 1993 ts8 = in[ 3] + in[ 5]; 1994 1995 out[17] = (ts5 + ts7 - ts8) * cx[8] - (ts6 - in[4]) * cx[8]; 1996 st = (ts5 + ts7 - ts8) * cx[7] + (ts6 - in[4]) * cx[8]; 1997 ct = (tc1 - tc3 - tc4) * cx[6]; 1998 out[5] = ct + st; 1999 out[6] = ct - st; 2000 2001 tc2 = (in[16] - in[10]) * cx[6]; 2002 ts6 = ts6 * cx[7] + in[4] * cx[8]; 2003 2004 ct = tc1 * cx[0] + tc2 + tc3 * cx[1] + tc4 * cx[2]; 2005 st = -ts5 * cx[4] + ts6 - ts7 * cx[5] + ts8 * cx[3]; 2006 out[1] = ct + st; 2007 out[2] = ct - st; 2008 2009 ct = tc1 * cx[1] - tc2 - tc3 * cx[2] + tc4 * cx[0]; 2010 st = -ts5 * cx[5] + ts6 - ts7 * cx[3] + ts8 * cx[4]; 2011 out[ 9] = ct + st; 2012 out[10] = ct - st; 2013 2014 ct = tc1 * cx[2] - tc2 + tc3 * cx[0] - tc4 * cx[1]; 2015 st = ts5 * cx[3] - ts6 + ts7 * cx[4] - ts8 * cx[5]; 2016 out[13] = ct + st; 2017 out[14] = ct - st; 2018 2019 ts1 = in[ 8] - in[ 0]; 2020 ts3 = in[ 6] - in[ 2]; 2021 ts4 = in[ 5] - in[ 3]; 2022 tc5 = in[17] + in[ 9]; 2023 tc6 = in[16] + in[10]; 2024 tc7 = in[15] + in[11]; 2025 tc8 = in[14] + in[12]; 2026 2027 out[0] = (tc5 + tc7 + tc8) * cx[8] + (tc6 + in[13]) * cx[8]; 2028 ct = (tc5 + tc7 + tc8) * cx[7] - (tc6 + in[13]) * cx[8]; 2029 st = (ts1 - ts3 + ts4) * cx[6]; 2030 out[11] = ct + st; 2031 out[12] = ct - st; 2032 2033 ts2 = (in[7] - in[1]) * cx[6]; 2034 tc6 = in[13] * cx[8] - tc6 * cx[7]; 2035 2036 ct = tc5 * cx[3] - tc6 + tc7 * cx[4] + tc8 * cx[5]; 2037 st = ts1 * cx[2] + ts2 + ts3 * cx[0] + ts4 * cx[1]; 2038 out[3] = ct + st; 2039 out[4] = ct - st; 2040 2041 ct =-tc5 * cx[5] + tc6 - tc7 * cx[3] - tc8 * cx[4]; 2042 st = ts1 * cx[1] + ts2 - ts3 * cx[2] - ts4 * cx[0]; 2043 out[7] = ct + st; 2044 out[8] = ct - st; 2045 2046 ct =-tc5 * cx[4] + tc6 - tc7 * cx[5] - tc8 * cx[3]; 2047 st = ts1 * cx[0] - ts2 + ts3 * cx[1] - ts4 * cx[2]; 2048 out[15] = ct + st; 2049 out[16] = ct - st; 2050} 2051 2052static int find_bitrate_index(int type, uint16_t bitrate) 2053{ 2054 int i; 2055 2056 for(i=0;i<14;i++) 2057 if(bitrate == bitr_index[type][i]) 2058 break; 2059 2060 return i; 2061} 2062 2063static int find_samplerate_index(uint16_t freq, int *mp3_type) 2064{ 2065 int mpg, rate; 2066 2067 /* set default values: MPEG1 at 44100/s */ 2068 *mp3_type = 1; 2069 2070 for(mpg=0; mpg<2; mpg++) 2071 for(rate=0; rate<3; rate++) 2072 if(freq == sampr_index[mpg][rate]) 2073 { *mp3_type = mpg; return rate; } 2074 2075 return 0; 2076} 2077 2078static void init_mp3_encoder_engine(bool stereo, int bitrate, uint16_t sample_rate) 2079{ 2080 uint32_t avg_byte_per_frame; 2081 2082#ifdef ROCKBOX_LITTLE_ENDIAN 2083 cfg.byte_order = order_littleEndian; 2084#else 2085 cfg.byte_order = order_bigEndian; 2086#endif 2087 2088 cfg.samplerate = sample_rate; 2089 cfg.channels = stereo ? 2 : 1; 2090 cfg.mpg.mode = stereo ? 0 : 3; /* 0=stereo, 3=mono */ 2091 cfg.mpg.bitrate = stereo ? bitrate : bitrate > 160 ? 160 : bitrate; 2092 cfg.mpg.smpl_id = find_samplerate_index(cfg.samplerate, &cfg.mpg.type); 2093 cfg.mpg.bitr_id = find_bitrate_index(cfg.mpg.type, cfg.mpg.bitrate); 2094 cfg.mpg.num_bands = num_bands[stereo ? cfg.mpg.type : 2][cfg.mpg.bitr_id]; 2095 2096 if(0 == cfg.mpg.type) 2097 { /* use MPEG2 format */ 2098 cfg.smpl_per_frm = MAX_SAMP_PER_FRAME/2; 2099 cfg.granules = 1; 2100 } 2101 else 2102 { /* use MPEG1 format */ 2103 cfg.smpl_per_frm = MAX_SAMP_PER_FRAME; 2104 cfg.granules = 2; 2105 } 2106 2107 scalefac = sfBand[cfg.mpg.smpl_id + 3*cfg.mpg.type]; 2108 2109 ht[ 0].table = NULL; ht[ 0].hlen = NULL; /* Apparently not used */ 2110 ht[ 1].table = t1HB; ht[ 1].hlen = t1l; 2111 ht[ 2].table = t2HB; ht[ 2].hlen = t2l; 2112 ht[ 3].table = t3HB; ht[ 3].hlen = t3l; 2113 ht[ 4].table = NULL; ht[ 4].hlen = NULL; /* Apparently not used */ 2114 ht[ 5].table = t5HB; ht[ 5].hlen = t5l; 2115 ht[ 6].table = t6HB; ht[ 6].hlen = t6l; 2116 ht[ 7].table = t7HB; ht[ 7].hlen = t7l; 2117 ht[ 8].table = t8HB; ht[ 8].hlen = t8l; 2118 ht[ 9].table = t9HB; ht[ 9].hlen = t9l; 2119 ht[10].table = t10HB; ht[10].hlen = t10l; 2120 ht[11].table = t11HB; ht[11].hlen = t11l; 2121 ht[12].table = t12HB; ht[12].hlen = t12l; 2122 ht[13].table = t13HB; ht[13].hlen = t13l; 2123 ht[14].table = NULL; ht[14].hlen = NULL; /* Apparently not used */ 2124 ht[15].table = t15HB; ht[15].hlen = t15l; 2125 2126 /* Figure average number of 'bytes' per frame */ 2127 avg_byte_per_frame = SAMPL2 * 16000 * cfg.mpg.bitrate / (2 - cfg.mpg.type); 2128 avg_byte_per_frame = avg_byte_per_frame / cfg.samplerate; 2129 cfg.byte_per_frame = avg_byte_per_frame / 64; 2130 cfg.frac_per_frame = avg_byte_per_frame & 63; 2131 cfg.slot_lag = 0; 2132 cfg.sideinfo_len = 32 + (cfg.mpg.type ? (cfg.channels == 1 ? 136 : 256) 2133 : (cfg.channels == 1 ? 72 : 136)); 2134} 2135 2136static void set_scale_facs(int *mdct_freq) 2137{ 2138 unsigned int i, is, ie, k, s; 2139 int max_freq_val, avrg_freq_val; 2140 2141 /* calc average of first 256 frequency values */ 2142 for(avrg_freq_val=i=0; i<256; i++) 2143 avrg_freq_val += mdct_freq[i]; 2144 avrg_freq_val >>= 8; 2145 2146 /* if max of current band is smaller than average, increase precision */ 2147 /* last band keeps untouched (not scaled) */ 2148 for(is=k=0; is<scalefac[21]; k++) 2149 { 2150 max_freq_val = 0; 2151 2152 for(i=is, ie=scalefac[k+1]; i<ie; i++) 2153 if(max_freq_val < mdct_freq[i]) 2154 max_freq_val = mdct_freq[i]; 2155 2156 for(s=0; s<3; s++) 2157 if((max_freq_val<<s) > avrg_freq_val) 2158 break; 2159 2160 band_scale_f[k] = (unsigned char)s; 2161 2162 for(i=is; s && i<ie; i++) 2163 mdct_freq[i] <<= s; 2164 2165 is = ie; 2166 } 2167} 2168 2169static void compress(void) 2170{ 2171 int i, gr, gr_cnt; 2172 uint32_t max; 2173 2174 while(1) 2175 { 2176 if((frames & 7) == 0) 2177 { rb->lcd_clear_display(); 2178 rb->lcd_putsxyf(4, 20, "Frame %d / %d", frames, 2179 wav_size/cfg.smpl_per_frm/cfg.channels/2); 2180 rb->lcd_update(); 2181 } 2182 /* encode one mp3 frame in this loop */ 2183 memset(CodedData.bbuf, 0, sizeof(CodedData.bbuf)); 2184 2185 if((cfg.slot_lag += cfg.frac_per_frame) >= 64) 2186 { /* Padding for this frame */ 2187 cfg.slot_lag -= 64; 2188 cfg.mpg.padding = 1; 2189 } 2190 else 2191 cfg.mpg.padding = 0; 2192 2193 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding 2194 - cfg.sideinfo_len) / cfg.granules / cfg.channels 2195 - 42; // reserved for scale_facs 2196 2197 /* shift out old samples */ 2198 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512); 2199 2200 /* read new samples to iram for further processing */ 2201 if(read_samples((mfbuf + 2*512), cfg.smpl_per_frm) == 0) 2202 break; 2203 2204 /* swap bytes if neccessary */ 2205 if(cfg.byte_order == order_bigEndian) 2206 for(i=0; i<cfg.smpl_per_frm; i++) 2207 { 2208 uint32_t t = ((uint32_t*)mfbuf)[512 + i]; 2209 t = ((t >> 8) & 0xff00ff) | ((t << 8) & 0xff00ff00); 2210 ((uint32_t*)mfbuf)[512 + i] = t; 2211 } 2212 2213 cfg.ResvSize = 0; 2214 gr_cnt = cfg.granules * cfg.channels; 2215 CodedData.bitpos = cfg.sideinfo_len; /* leave space for mp3 header */ 2216 2217 for(gr=0; gr<cfg.granules; gr++) 2218 { 2219 short *wk = mfbuf + 2*286 + gr*1152; 2220 int ch; 2221 2222 /* 16bit packed wav data can be windowed efficiently on coldfire */ 2223 window_subband1(wk, sb_data[0][1-gr][0], sb_data[1][1-gr][0]); 2224 2225 for(ch=0; ch<cfg.channels; ch++) 2226 { 2227 int ii, k, shift; 2228 2229 wk = mfbuf + 2*286 + gr*1152 + ch; 2230 2231 /* 36864=4*18*16*32 */ 2232 for(k=0; k<18; k++, wk+=64) 2233 { 2234 window_subband2(wk, sb_data[ch][1-gr][k]); 2235 /* Compensate for inversion in the analysis filter */ 2236 if(k & 1) 2237 { 2238 int band; 2239 for(band=1; band<32; band+=2) 2240 sb_data[ch][1-gr][k][band] *= -1; 2241 } 2242 } 2243 2244 /* Perform imdct of 18 previous + 18 current subband samples */ 2245 /* for integer precision do this loop again (if neccessary) */ 2246 shift = 14 - (cfg.cod_info[gr][ch].additStep >> 2); 2247 for(k=1,ii=0; ii<3 && k; ii++) 2248 { 2249 int *mdct = mdct_freq; 2250 int band; 2251 2252 cfg.cod_info[gr][ch].additStep = 4 * (14 - shift); 2253 for(band=0; band<cfg.mpg.num_bands; band++, mdct+=18) 2254 { 2255 int *band0 = sb_data[ch][ gr][0] + order[band]; 2256 int *band1 = sb_data[ch][1-gr][0] + order[band]; 2257 int work[18]; 2258 2259 /* 9216=4*32*9*8 */ 2260 for(k=-9; k<0; k++) 2261 { 2262 int a = shft_n(band1[(k+9)*32], shift); 2263 int b = shft_n(band1[(8-k)*32], shift); 2264 int c = shft_n(band0[(k+9)*32], shift); 2265 int d = shft_n(band0[(8-k)*32], shift); 2266 2267 work[k+ 9] = shft16(a * win[k+ 9][0] + 2268 b * win[k+ 9][1] + 2269 c * win[k+ 9][2] + 2270 d * win[k+ 9][3]); 2271 2272 work[k+18] = shft16(c * win[k+18][0] + 2273 d * win[k+18][1] + 2274 a * win[k+18][2] + 2275 b * win[k+18][3]); 2276 } 2277 2278 /* 7200=4*18*100 */ 2279 mdct_long(mdct, work); 2280 2281 /* Perform aliasing reduction butterfly */ 2282 if(band != 0) 2283 { 2284 for(k=7; k>=0; --k) 2285 { 2286 int bu, bd; 2287 bu = shft15(mdct[k]) * ca[k] + 2288 shft15(mdct[-1-k]) * cs[k]; 2289 bd = shft15(mdct[k]) * cs[k] - 2290 shft15(mdct[-1-k]) * ca[k]; 2291 mdct[-1-k] = bu; 2292 mdct[ k ] = bd; 2293 } 2294 } 2295 } 2296 2297 max = 0; 2298 for(k=0; k<576; k++) 2299 { 2300 if(mdct_freq[k] < 0) 2301 { 2302 mdct_sign[k] = 1; /* negative */ 2303 mdct_freq[k] = shft13(-mdct_freq[k]); 2304 } 2305 else 2306 { 2307 mdct_sign[k] = 0; /* positive */ 2308 mdct_freq[k] = shft13(mdct_freq[k]); 2309 } 2310 2311 if(max < (uint32_t)mdct_freq[k]) 2312 max = (uint32_t)mdct_freq[k]; 2313 } 2314 2315 cfg.cod_info[gr][ch].max_val = max; 2316 2317 /* calc new shift for higher integer precision */ 2318 for(k=0; max<(uint32_t)(0x7800>>k); k++) shift--; 2319 for( ; (max>>k)>=(uint32_t)0x10000; k++) shift++; 2320 if(shift < 0) shift = 0; 2321 } 2322 2323 cfg.cod_info[gr][ch].quantStep += 2324 cfg.cod_info[gr][ch].additStep; 2325 2326 set_scale_facs(mdct_freq); 2327 2328 /* bit and noise allocation */ 2329 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch], 2330 gr_cnt--); 2331 /* write the frame to the bitstream */ 2332 Huffmancodebits(enc_data, mdct_sign, 2333 &cfg.cod_info[gr][ch]); 2334 2335 cfg.cod_info[gr][ch].quantStep -= 2336 cfg.cod_info[gr][ch].additStep; 2337 2338 if(cfg.granules == 1) 2339 { 2340 memcpy(sb_data[ch][0], sb_data[ch][1], 2341 sizeof(sb_data[ch][0])); 2342 } 2343 } 2344 } 2345 2346 enc_size = (CodedData.bitpos + 7) >> 3; 2347 /* finish this chunk by adding sideinfo header data */ 2348 CodedData.bitpos = 0; 2349 encodeSideInfo( cfg.cod_info ); 2350 2351 if(cfg.byte_order != order_bigEndian) 2352 for(i=0; i<(enc_size+3)/4; i++) 2353 CodedData.bbuf[i] = myswap32(CodedData.bbuf[i]); 2354 2355 if(enc_chunk + enc_size > (int)enc_buffer_size) 2356 { 2357 /* copy iram mp3 buffer to sdram/file */ 2358 rb->write(mp3file, enc_buffer, enc_chunk & ~3); 2359 memcpy(enc_buffer, enc_buffer + (enc_chunk >> 2), enc_chunk & 3); 2360 enc_chunk &= 3; 2361 } 2362 2363 memcpy(enc_buffer + enc_chunk, CodedData.bbuf, enc_size); 2364 enc_chunk += enc_size; 2365 frames++; 2366 } 2367 /* write last chunks to disk */ 2368 rb->write(mp3file, enc_buffer, enc_chunk); 2369} 2370 2371 2372int num_file; 2373char mp3_name[80]; 2374 2375static void get_mp3_filename(const char *wav_name) 2376{ 2377 rb->strlcpy(mp3_name, wav_name, sizeof(mp3_name)); 2378 rb->strlcpy(mp3_name + rb->strlen(mp3_name) - 4, ".mp3", 5); 2379} 2380 2381#if CONFIG_KEYPAD == IRIVER_H100_PAD || CONFIG_KEYPAD == IRIVER_H300_PAD 2382#define MP3ENC_PREV BUTTON_UP 2383#define MP3ENC_NEXT BUTTON_DOWN 2384#define MP3ENC_DONE BUTTON_OFF 2385#define MP3ENC_SELECT BUTTON_SELECT 2386 2387#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \ 2388 (CONFIG_KEYPAD == IPOD_1G2G_PAD) 2389#define MP3ENC_PREV BUTTON_SCROLL_BACK 2390#define MP3ENC_NEXT BUTTON_SCROLL_FWD 2391#define MP3ENC_DONE BUTTON_MENU 2392#define MP3ENC_SELECT BUTTON_SELECT 2393 2394#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD 2395#define MP3ENC_PREV BUTTON_UP 2396#define MP3ENC_NEXT BUTTON_DOWN 2397#define MP3ENC_DONE BUTTON_POWER 2398#define MP3ENC_SELECT BUTTON_SELECT 2399 2400#elif CONFIG_KEYPAD == GIGABEAT_PAD || \ 2401 CONFIG_KEYPAD == SAMSUNG_YPR0_PAD 2402#define MP3ENC_PREV BUTTON_UP 2403#define MP3ENC_NEXT BUTTON_DOWN 2404#define MP3ENC_DONE BUTTON_POWER 2405#define MP3ENC_SELECT BUTTON_SELECT 2406 2407#elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \ 2408 (CONFIG_KEYPAD == SANSA_C200_PAD) || \ 2409 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \ 2410 (CONFIG_KEYPAD == SANSA_M200_PAD) 2411#define MP3ENC_PREV BUTTON_UP 2412#define MP3ENC_NEXT BUTTON_DOWN 2413#define MP3ENC_DONE BUTTON_POWER 2414#define MP3ENC_SELECT BUTTON_SELECT 2415 2416#elif (CONFIG_KEYPAD == SANSA_FUZE_PAD) 2417#define MP3ENC_PREV BUTTON_SCROLL_BACK 2418#define MP3ENC_NEXT BUTTON_SCROLL_FWD 2419#define MP3ENC_DONE BUTTON_UP 2420#define MP3ENC_SELECT BUTTON_SELECT 2421 2422#elif CONFIG_KEYPAD == IRIVER_H10_PAD 2423#define MP3ENC_PREV BUTTON_SCROLL_UP 2424#define MP3ENC_NEXT BUTTON_SCROLL_DOWN 2425#define MP3ENC_DONE BUTTON_POWER 2426#define MP3ENC_SELECT BUTTON_PLAY 2427 2428#elif CONFIG_KEYPAD == GIGABEAT_S_PAD 2429#define MP3ENC_PREV BUTTON_UP 2430#define MP3ENC_NEXT BUTTON_DOWN 2431#define MP3ENC_DONE BUTTON_BACK 2432#define MP3ENC_SELECT BUTTON_SELECT 2433 2434#elif CONFIG_KEYPAD == MROBE100_PAD 2435#define MP3ENC_PREV BUTTON_UP 2436#define MP3ENC_NEXT BUTTON_DOWN 2437#define MP3ENC_DONE BUTTON_POWER 2438#define MP3ENC_SELECT BUTTON_SELECT 2439 2440#elif CONFIG_KEYPAD == IAUDIO_M3_PAD 2441#define MP3ENC_PREV BUTTON_RC_VOL_UP 2442#define MP3ENC_NEXT BUTTON_RC_VOL_DOWN 2443#define MP3ENC_DONE BUTTON_RC_REC 2444#define MP3ENC_SELECT BUTTON_RC_FF 2445 2446#elif CONFIG_KEYPAD == COWON_D2_PAD 2447#define MP3ENC_DONE BUTTON_POWER 2448 2449#elif CONFIG_KEYPAD == CREATIVEZVM_PAD 2450#define MP3ENC_PREV BUTTON_UP 2451#define MP3ENC_NEXT BUTTON_DOWN 2452#define MP3ENC_DONE BUTTON_BACK 2453#define MP3ENC_SELECT BUTTON_SELECT 2454 2455#elif CONFIG_KEYPAD == CREATIVE_ZENXFI3_PAD 2456#define MP3ENC_PREV BUTTON_UP 2457#define MP3ENC_NEXT BUTTON_DOWN 2458#define MP3ENC_DONE BUTTON_POWER 2459#define MP3ENC_SELECT BUTTON_PLAY 2460 2461#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD 2462#define MP3ENC_PREV BUTTON_UP 2463#define MP3ENC_NEXT BUTTON_DOWN 2464#define MP3ENC_DONE BUTTON_POWER 2465#define MP3ENC_SELECT BUTTON_SELECT 2466 2467#elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD 2468#define MP3ENC_PREV BUTTON_UP 2469#define MP3ENC_NEXT BUTTON_DOWN 2470#define MP3ENC_DONE BUTTON_POWER 2471#define MP3ENC_SELECT BUTTON_PLAY 2472 2473#elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD 2474#define MP3ENC_PREV BUTTON_UP 2475#define MP3ENC_NEXT BUTTON_DOWN 2476#define MP3ENC_DONE BUTTON_POWER 2477#define MP3ENC_SELECT BUTTON_PLAY 2478 2479#elif CONFIG_KEYPAD == ONDAVX747_PAD || \ 2480CONFIG_KEYPAD == ONDAVX777_PAD || \ 2481CONFIG_KEYPAD == MROBE500_PAD 2482#define MP3ENC_DONE BUTTON_POWER 2483 2484#elif (CONFIG_KEYPAD == SAMSUNG_YH820_PAD) || \ 2485 (CONFIG_KEYPAD == SAMSUNG_YH92X_PAD) 2486#define MP3ENC_PREV BUTTON_UP 2487#define MP3ENC_NEXT BUTTON_DOWN 2488#define MP3ENC_DONE BUTTON_PLAY 2489#define MP3ENC_SELECT BUTTON_RIGHT 2490 2491#elif CONFIG_KEYPAD == PBELL_VIBE500_PAD 2492#define MP3ENC_PREV BUTTON_UP 2493#define MP3ENC_NEXT BUTTON_DOWN 2494#define MP3ENC_DONE BUTTON_REC 2495#define MP3ENC_SELECT BUTTON_OK 2496 2497#elif CONFIG_KEYPAD == MPIO_HD200_PAD 2498#define MP3ENC_PREV BUTTON_REW 2499#define MP3ENC_NEXT BUTTON_FF 2500#define MP3ENC_DONE BUTTON_PLAY 2501#define MP3ENC_SELECT BUTTON_FUNC 2502 2503#elif CONFIG_KEYPAD == MPIO_HD300_PAD 2504#define MP3ENC_PREV BUTTON_REW 2505#define MP3ENC_NEXT BUTTON_FF 2506#define MP3ENC_DONE BUTTON_PLAY 2507#define MP3ENC_SELECT BUTTON_ENTER 2508 2509#elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD 2510#define MP3ENC_PREV BUTTON_LEFT 2511#define MP3ENC_NEXT BUTTON_RIGHT 2512#define MP3ENC_DONE BUTTON_PLAYPAUSE 2513#define MP3ENC_SELECT BUTTON_SELECT 2514 2515#elif CONFIG_KEYPAD == SANSA_CONNECT_PAD 2516#define MP3ENC_PREV BUTTON_PREV 2517#define MP3ENC_NEXT BUTTON_NEXT 2518#define MP3ENC_DONE BUTTON_DOWN 2519#define MP3ENC_SELECT BUTTON_SELECT 2520 2521#elif (CONFIG_KEYPAD == HM60X_PAD) || \ 2522 (CONFIG_KEYPAD == HM801_PAD) 2523#define MP3ENC_PREV BUTTON_LEFT 2524#define MP3ENC_NEXT BUTTON_RIGHT 2525#define MP3ENC_DONE BUTTON_DOWN 2526#define MP3ENC_SELECT BUTTON_SELECT 2527 2528#elif (CONFIG_KEYPAD == SONY_NWZ_PAD) 2529#define MP3ENC_PREV BUTTON_LEFT 2530#define MP3ENC_NEXT BUTTON_RIGHT 2531#define MP3ENC_DONE BUTTON_DOWN 2532#define MP3ENC_SELECT BUTTON_PLAY 2533 2534#elif (CONFIG_KEYPAD == CREATIVE_ZEN_PAD) 2535#define MP3ENC_PREV BUTTON_LEFT 2536#define MP3ENC_NEXT BUTTON_RIGHT 2537#define MP3ENC_DONE BUTTON_PLAYPAUSE 2538#define MP3ENC_SELECT BUTTON_SELECT 2539 2540#elif (CONFIG_KEYPAD == DX50_PAD) 2541#define MP3ENC_PREV BUTTON_LEFT 2542#define MP3ENC_NEXT BUTTON_RIGHT 2543#define MP3ENC_DONE BUTTON_POWER 2544#define MP3ENC_SELECT BUTTON_PLAY 2545 2546#elif CONFIG_KEYPAD == CREATIVE_ZENXFI2_PAD 2547#define MP3ENC_DONE BUTTON_POWER 2548#define MP3ENC_SELECT BUTTON_MENU 2549 2550#elif (CONFIG_KEYPAD == AGPTEK_ROCKER_PAD) 2551#define MP3ENC_PREV BUTTON_LEFT 2552#define MP3ENC_NEXT BUTTON_RIGHT 2553#define MP3ENC_DONE BUTTON_POWER 2554#define MP3ENC_SELECT BUTTON_SELECT 2555 2556#elif CONFIG_KEYPAD == XDUOO_X3_PAD || CONFIG_KEYPAD == XDUOO_X3II_PAD || CONFIG_KEYPAD == XDUOO_X20_PAD 2557#define MP3ENC_PREV BUTTON_HOME 2558#define MP3ENC_NEXT BUTTON_OPTION 2559#define MP3ENC_DONE BUTTON_POWER 2560#define MP3ENC_SELECT BUTTON_PLAY 2561 2562#elif CONFIG_KEYPAD == FIIO_M3K_LINUX_PAD 2563#define MP3ENC_PREV BUTTON_HOME 2564#define MP3ENC_NEXT BUTTON_OPTION 2565#define MP3ENC_DONE BUTTON_POWER 2566#define MP3ENC_SELECT BUTTON_PLAY 2567 2568#elif CONFIG_KEYPAD == IHIFI_770_PAD || CONFIG_KEYPAD == IHIFI_800_PAD 2569#define MP3ENC_PREV BUTTON_PREV 2570#define MP3ENC_NEXT BUTTON_NEXT 2571#define MP3ENC_DONE BUTTON_POWER 2572#define MP3ENC_SELECT BUTTON_PLAY 2573 2574#elif CONFIG_KEYPAD == EROSQ_PAD 2575#define MP3ENC_PREV BUTTON_PREV 2576#define MP3ENC_NEXT BUTTON_NEXT 2577#define MP3ENC_DONE BUTTON_POWER 2578#define MP3ENC_SELECT BUTTON_PLAY 2579 2580#elif CONFIG_KEYPAD == FIIO_M3K_PAD 2581#define MP3ENC_PREV BUTTON_LEFT 2582#define MP3ENC_NEXT BUTTON_RIGHT 2583#define MP3ENC_DONE BUTTON_POWER 2584#define MP3ENC_SELECT BUTTON_SELECT 2585 2586#elif CONFIG_KEYPAD == SHANLING_Q1_PAD 2587/* use touchscreen */ 2588 2589#elif CONFIG_KEYPAD == SDL_PAD 2590#define MP3ENC_PREV BUTTON_MIDLEFT 2591#define MP3ENC_NEXT BUTTON_MIDRIGHT 2592#define MP3ENC_DONE BUTTON_TOPLEFT 2593#define MP3ENC_SELECT BUTTON_CENTER 2594#elif CONFIG_KEYPAD == MA_PAD 2595#define MP3ENC_PREV BUTTON_LEFT 2596#define MP3ENC_NEXT BUTTON_RIGHT 2597#define MP3ENC_DONE BUTTON_BACK 2598#define MP3ENC_SELECT BUTTON_PLAY 2599 2600#elif CONFIG_KEYPAD == RG_NANO_PAD 2601#define MP3ENC_PREV BUTTON_UP 2602#define MP3ENC_NEXT BUTTON_DOWN 2603#define MP3ENC_DONE BUTTON_START 2604#define MP3ENC_SELECT BUTTON_A 2605 2606#else 2607#error No keymap defined! 2608#endif 2609 2610#ifdef HAVE_TOUCHSCREEN 2611#ifndef MP3ENC_PREV 2612#define MP3ENC_PREV BUTTON_MIDLEFT 2613#endif 2614#ifndef MP3ENC_NEXT 2615#define MP3ENC_NEXT BUTTON_MIDRIGHT 2616#endif 2617#ifndef MP3ENC_DONE 2618#define MP3ENC_DONE BUTTON_TOPLEFT 2619#endif 2620#ifndef MP3ENC_SELECT 2621#define MP3ENC_SELECT BUTTON_CENTER 2622#endif 2623#endif 2624 2625enum plugin_status plugin_start(const void* parameter) 2626{ 2627 int rat, srat, nrat; /* for rate selection */ 2628 int cont = 1, butt; 2629 int ret; 2630 long tim = 0; 2631 static const char* bstrg[] = { 2632 "64", "80", "96", "112", "128", "160", "192", "224", "256", "320" 2633 }; 2634 static const int brate[] = { 2635 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 2636 }; 2637 2638 if (parameter == NULL) 2639 return PLUGIN_ERROR; 2640 2641 enc_buffer = rb->plugin_get_audio_buffer(&enc_buffer_size); 2642 2643#ifdef CPU_COLDFIRE 2644 coldfire_set_macsr(0); /* integer mode */ 2645#endif 2646 2647 rb->lcd_setfont(FONT_SYSFIXED); 2648 2649#ifdef HAVE_ADJUSTABLE_CPU_FREQ 2650 rb->cpu_boost(true); 2651#endif 2652 rb->button_clear_queue(); 2653 2654 nrat = 9; 2655 srat = 4; /* set 128kBit as default */ 2656 2657 while(cont && (butt = rb->button_get_w_tmo(HZ/10)) != MP3ENC_SELECT) 2658 { 2659 switch(butt) 2660 { 2661 case MP3ENC_DONE: cont = 0; break; 2662 case MP3ENC_PREV|BUTTON_REPEAT: 2663 case MP3ENC_PREV: if(srat > 0 ) srat--; break; 2664 case MP3ENC_NEXT|BUTTON_REPEAT: 2665 case MP3ENC_NEXT: if(srat < nrat) srat++; break; 2666 } 2667 2668 rb->lcd_clear_display(); 2669 rb->lcd_putsxy(2, 2, "-- Select Bitrate --"); 2670 2671 for(rat=0; rat<=nrat; rat++) 2672 rb->lcd_putsxy(2, 12 + rat*8, bstrg[rat]); 2673 rb->lcd_set_drawmode(DRMODE_COMPLEMENT); 2674 rb->lcd_fillrect(0, 12 + srat*8, 127, 8); 2675 rb->lcd_set_drawmode(DRMODE_SOLID); 2676 rb->lcd_update(); 2677 } 2678 2679 wav_filename = parameter; 2680 2681 if(cont) 2682 { 2683 ret = wave_open(); 2684 if(ret == 0) 2685 { 2686 init_mp3_encoder_engine((cfg.channels==2), brate[srat], cfg.samplerate); 2687 get_mp3_filename(wav_filename); 2688 mp3file = rb->open(mp3_name , O_WRONLY|O_CREAT|O_TRUNC, 0666); 2689 frames = 0; 2690 2691 tim = *rb->current_tick; 2692 compress(); 2693 tim = *rb->current_tick - tim; 2694 2695 rb->close(wavfile); 2696 rb->close(mp3file); 2697 rb->reload_directory(); 2698 } 2699 else 2700 { 2701 rb->close(wavfile); 2702 rb->lcd_clear_display(); 2703 rb->lcd_putsxyf(0, 20, "WaveOpen failed %d", ret); 2704 rb->lcd_putsxyf(0, 30, "%s", mp3_enc_err[-ret]); 2705 rb->lcd_update(); 2706 rb->sleep(5*HZ); 2707 } 2708 2709 rb->lcd_clear_display(); 2710#if LCD_WIDTH <= 128 2711 rb->lcd_putsxy(0, 30, "Conversion:"); 2712 rb->lcd_putsxyf(0, 40,"%ld.%02lds ", tim/100, tim%100); 2713 tim = frames * cfg.smpl_per_frm * 100 / (cfg.samplerate != 0 ? cfg.samplerate : 1); /* unit=.01s */ 2714 rb->lcd_putsxy(0, 10, "WAV-Length:"); 2715 rb->lcd_putsxyf(0, 20, "%ld.%02lds ", tim/100, tim%100); 2716#else 2717 rb->lcd_putsxyf(0, 30, " Conversion: %ld.%02lds ", tim/100, tim%100); 2718 tim = frames * cfg.smpl_per_frm * 100 / (cfg.samplerate != 0 ? cfg.samplerate : 1); /* unit=.01s */ 2719 rb->lcd_putsxyf(0, 20, " WAV-Length: %ld.%02lds ", tim/100, tim%100); 2720#endif 2721 rb->lcd_update(); 2722 rb->sleep(5*HZ); 2723 } 2724 2725 rb->lcd_setfont(FONT_UI); 2726#ifdef HAVE_ADJUSTABLE_CPU_FREQ 2727 rb->cpu_boost(false); 2728#endif 2729 return PLUGIN_OK; 2730}