summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntonius Hellmann <toni@rockbox.org>2008-06-02 19:28:08 +0000
committerAntonius Hellmann <toni@rockbox.org>2008-06-02 19:28:08 +0000
commit758072173ec6e3551c451f55b744d38e87b7a9dd (patch)
tree5f9c8b12291ffc723c894708df637431aa00d8e6
parent75cac2c6045a43533e72453787c91f543e3098b8 (diff)
downloadrockbox-758072173ec6e3551c451f55b744d38e87b7a9dd.tar.gz
rockbox-758072173ec6e3551c451f55b744d38e87b7a9dd.zip
Some quality improvements in the mp3 encoder engine. For more details
see FS#9007. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17679 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/codecs/mp3_enc.c239
-rw-r--r--apps/plugins/mp3_encoder.c618
2 files changed, 529 insertions, 328 deletions
diff --git a/apps/codecs/mp3_enc.c b/apps/codecs/mp3_enc.c
index 26cc1dd73f..87d6f6871f 100644
--- a/apps/codecs/mp3_enc.c
+++ b/apps/codecs/mp3_enc.c
@@ -73,9 +73,9 @@ typedef struct {
73 uint32_t address1; 73 uint32_t address1;
74 uint32_t address2; 74 uint32_t address2;
75 uint32_t address3; 75 uint32_t address3;
76 long quantStep; 76 long quantStep;
77 long additStep; 77 long additStep;
78 long max_val; 78 uint32_t max_val;
79} side_info_t; 79} side_info_t;
80 80
81typedef struct { 81typedef struct {
@@ -93,14 +93,14 @@ typedef struct {
93} config_t; 93} config_t;
94 94
95typedef struct { 95typedef struct {
96 int bitpos; /* current bitpos for writing */ 96 int bitpos; /* current bitpos for writing */
97 uint32_t bbuf[263]; 97 uint32_t bbuf[263];
98} BF_Data; 98} BF_Data;
99 99
100struct huffcodetab { 100struct huffcodetab {
101 int len; /* max. index */ 101 int len; /* max. index */
102 const uint8_t *table; /* pointer to array[len][len] */ 102 const uint8_t *table; /* pointer to array[len][len] */
103 const uint8_t *hlen; /* pointer to array[len][len] */ 103 const uint8_t *hlen; /* pointer to array[len][len] */
104}; 104};
105 105
106struct huffcodebig { 106struct huffcodebig {
@@ -119,8 +119,9 @@ struct huffcodebig {
119 119
120static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */ 120static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */
121static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */ 121static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */
122static int mdct_freq [SAMPL2] IBSS_ATTR; /* 9216 Bytes */ 122static int mdct_freq [SAMPL2] IBSS_ATTR; /* 2304 Bytes */
123static short enc_data [SAMPL2] IBSS_ATTR; /* 4608 Bytes */ 123static char mdct_sign [SAMPL2] IBSS_ATTR; /* 576 Bytes */
124static short enc_data [SAMPL2] IBSS_ATTR; /* 1152 Bytes */
124static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */ 125static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */
125static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */ 126static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */
126static int ca [8] IBSS_ATTR; /* 32 Bytes */ 127static int ca [8] IBSS_ATTR; /* 32 Bytes */
@@ -175,6 +176,7 @@ static unsigned samp_per_frame IBSS_ATTR;
175static config_t cfg IBSS_ATTR; 176static config_t cfg IBSS_ATTR;
176static char *res_buffer; 177static char *res_buffer;
177static int32_t err IBSS_ATTR; 178static int32_t err IBSS_ATTR;
179static uint8_t band_scale_f[22];
178 180
179static const uint8_t ht_count_const[2][2][16] = 181static const uint8_t ht_count_const[2][2][16] =
180{ { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */ 182{ { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */
@@ -834,8 +836,8 @@ static const int win_const[18][4] = {
834 { 134, -146,-3352,-3072 } }; 836 { 134, -146,-3352,-3072 } };
835 837
836/* forward declarations */ 838/* forward declarations */
837static int HuffmanCode( short *ix, int *xr, uint32_t begin, uint32_t end, int table); 839static int HuffmanCode( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
838static int HuffmanCod1( short *ix, int *xr, uint32_t begin, uint32_t end, int table); 840static int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
839static void putbits(uint32_t val, uint32_t nbit); 841static void putbits(uint32_t val, uint32_t nbit);
840static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table, 842static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
841 uint32_t len, int *bits); 843 uint32_t len, int *bits);
@@ -892,14 +894,16 @@ static void encodeSideInfo( side_info_t si[2][2] )
892 { 894 {
893 side_info_t *gi = &si[gr][ch]; 895 side_info_t *gi = &si[gr][ch];
894 896
895 putlong( gi->part2_3_length, 12 ); 897 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
896 putlong( gi->address3>>1, 9 ); 898 putlong( gi->address3>>1, 9 );
897 putlong( gi->global_gain, 8 ); 899 putlong( gi->global_gain, 8 );
898 putlong( gi->table_select[0], 10 ); 900 putlong( 9, 4 ); /* set scale_facs compr type */
899 putlong( gi->table_select[1], 5 ); 901 putlong( gi->table_select[0], 6 );
900 putlong( gi->table_select[2], 5 ); 902 putlong( gi->table_select[1], 5 );
901 putlong( gi->region_0_1, 7 ); 903 putlong( gi->table_select[2], 5 );
902 putlong( gi->table_select[3], 3 ); 904 putlong( gi->region_0_1, 7 );
905 putlong( 1 , 2 ); /* set scale_facs to 1bit */
906 putlong( gi->table_select[3], 1 );
903 } 907 }
904 } 908 }
905 else 909 else
@@ -911,14 +915,16 @@ static void encodeSideInfo( side_info_t si[2][2] )
911 { 915 {
912 side_info_t *gi = &si[0][ch]; 916 side_info_t *gi = &si[0][ch];
913 917
914 putlong( gi->part2_3_length, 12); 918 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
915 putlong( gi->address3>>1, 9); 919 putlong( gi->address3>>1, 9 );
916 putlong( gi->global_gain, 8); 920 putlong( gi->global_gain, 8 );
917 putlong( gi->table_select[0], 15); 921 putlong( 0xCA, 9 ); /* set scale_facs compr type */
918 putlong( gi->table_select[1], 5); 922 putlong( gi->table_select[0], 6 );
919 putlong( gi->table_select[2], 5); 923 putlong( gi->table_select[1], 5 );
920 putlong( gi->region_0_1, 7); 924 putlong( gi->table_select[2], 5 );
921 putlong( gi->table_select[3], 2); 925 putlong( gi->region_0_1 , 7 );
926 putlong( 1 , 1 ); /* set scale_facs to 1bit */
927 putlong( gi->table_select[3], 1 );
922 } 928 }
923 } 929 }
924 /* flush remaining bits */ 930 /* flush remaining bits */
@@ -927,7 +933,7 @@ static void encodeSideInfo( side_info_t si[2][2] )
927 933
928/* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS, 934/* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS,
929 as well as the definitions of the side information on pages 26 and 27. */ 935 as well as the definitions of the side information on pages 26 and 27. */
930static void Huffmancodebits( short *ix, int *xr, side_info_t *gi ) 936static void Huffmancodebits( short *ix, char *xr_sign, side_info_t *gi )
931{ 937{
932 int region1 = gi->address1; 938 int region1 = gi->address1;
933 int region2 = gi->address2; 939 int region2 = gi->address2;
@@ -935,18 +941,27 @@ static void Huffmancodebits( short *ix, int *xr, side_info_t *gi )
935 int count1 = bigvals + (gi->count1 << 2); 941 int count1 = bigvals + (gi->count1 << 2);
936 int stuffBits = 0; 942 int stuffBits = 0;
937 int bits = 0; 943 int bits = 0;
944 int i, v;
945
946 for(i=v=0; i<32; i+=2)
947 v |= band_scale_f[i>>1] << (30-i);
948 putbits(v, 32); // store scale_facs (part1)
949
950 for(v=0; i<42; i+=2)
951 v |= band_scale_f[i>>1] << (40-i);
952 putbits(v, 10); // store scale_facs (part2)
938 953
939 if(region1 > 0) 954 if(region1 > 0)
940 bits += HuffmanCode(ix, xr, 0 , region1, gi->table_select[0]); 955 bits += HuffmanCode(ix, xr_sign, 0 , region1, gi->table_select[0]);
941 956
942 if(region2 > region1) 957 if(region2 > region1)
943 bits += HuffmanCode(ix, xr, region1, region2, gi->table_select[1]); 958 bits += HuffmanCode(ix, xr_sign, region1, region2, gi->table_select[1]);
944 959
945 if(bigvals > region2) 960 if(bigvals > region2)
946 bits += HuffmanCode(ix, xr, region2, bigvals, gi->table_select[2]); 961 bits += HuffmanCode(ix, xr_sign, region2, bigvals, gi->table_select[2]);
947 962
948 if(count1 > bigvals) 963 if(count1 > bigvals)
949 bits += HuffmanCod1(ix, xr, bigvals, count1, gi->table_select[3]); 964 bits += HuffmanCod1(ix, xr_sign, bigvals, count1, gi->table_select[3]);
950 965
951 if((stuffBits = gi->part2_3_length - bits) > 0) 966 if((stuffBits = gi->part2_3_length - bits) > 0)
952 { 967 {
@@ -961,15 +976,15 @@ static void Huffmancodebits( short *ix, int *xr, side_info_t *gi )
961 } 976 }
962} 977}
963 978
964int HuffmanCod1( short *ix, int *xr, uint32_t begin, uint32_t end, int tbl) 979int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int tbl)
965{ 980{
966 uint32_t cc=0, sz=0; 981 uint32_t cc=0, sz=0;
967 uint32_t i, d, p; 982 uint32_t i, d, p;
968 int sumbit=0, s=0, l=0, v, w, x, y; 983 int sumbit=0, s=0, l=0, v, w, x, y;
969 #define sgnv (xr[i+0] < 0 ? 1 : 0) 984 #define sgnv xr_sign[i+0]
970 #define sgnw (xr[i+1] < 0 ? 1 : 0) 985 #define sgnw xr_sign[i+1]
971 #define sgnx (xr[i+2] < 0 ? 1 : 0) 986 #define sgnx xr_sign[i+2]
972 #define sgny (xr[i+3] < 0 ? 1 : 0) 987 #define sgny xr_sign[i+3]
973 988
974 for(i=begin; i<end; i+=4) 989 for(i=begin; i<end; i+=4)
975 { 990 {
@@ -982,20 +997,20 @@ int HuffmanCod1( short *ix, int *xr, uint32_t begin, uint32_t end, int tbl)
982 switch(p) 997 switch(p)
983 { 998 {
984 case 0: l=0; s = 0; break; 999 case 0: l=0; s = 0; break;
985 case 1: l=1; s = sgnv; break; 1000 case 1: l=1; s = sgny; break;
986 case 2: l=1; s = sgnw; break; 1001 case 2: l=1; s = sgnx; break;
987 case 3: l=2; s = (sgnv << 1) + sgnw; break; 1002 case 3: l=2; s = (sgnx << 1) + sgny; break;
988 case 4: l=1; s = sgnx; break; 1003 case 4: l=1; s = sgnw; break;
989 case 5: l=2; s = (sgnv << 1) + sgnx; break; 1004 case 5: l=2; s = (sgnw << 1) + sgny; break;
990 case 6: l=2; s = (sgnw << 1) + sgnx; break; 1005 case 6: l=2; s = (sgnw << 1) + sgnx; break;
991 case 7: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break; 1006 case 7: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break;
992 case 8: l=1; s = sgny; break; 1007 case 8: l=1; s = sgnv; break;
993 case 9: l=2; s = (sgnv << 1) + sgny; break; 1008 case 9: l=2; s = (sgnv << 1) + sgny; break;
994 case 10: l=2; s = (sgnw << 1) + sgny; break; 1009 case 10: l=2; s = (sgnv << 1) + sgnx; break;
995 case 11: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break; 1010 case 11: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break;
996 case 12: l=2; s = (sgnx << 1) + sgny; break; 1011 case 12: l=2; s = (sgnv << 1) + sgnw; break;
997 case 13: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break; 1012 case 13: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break;
998 case 14: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break; 1013 case 14: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break;
999 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break; 1014 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break;
1000 } 1015 }
1001 1016
@@ -1012,13 +1027,13 @@ int HuffmanCod1( short *ix, int *xr, uint32_t begin, uint32_t end, int tbl)
1012} 1027}
1013 1028
1014/* Implements the pseudocode of page 98 of the IS */ 1029/* Implements the pseudocode of page 98 of the IS */
1015int HuffmanCode( short *ix, int *xr, uint32_t begin, uint32_t end, int table) 1030int HuffmanCode(short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table)
1016{ 1031{
1017 uint32_t cc=0, sz=0, code; 1032 uint32_t cc=0, sz=0, code;
1018 uint32_t i, xl=0, yl=0, idx; 1033 uint32_t i, xl=0, yl=0, idx;
1019 int x, y, bit, sumbit=0; 1034 int x, y, bit, sumbit=0;
1020 #define sign_x (xr[i+0] < 0 ? 1 : 0) 1035 #define sign_x xr_sign[i+0]
1021 #define sign_y (xr[i+1] < 0 ? 1 : 0) 1036 #define sign_y xr_sign[i+1]
1022 1037
1023 if(table == 0) 1038 if(table == 0)
1024 return 0; 1039 return 0;
@@ -1057,6 +1072,13 @@ int HuffmanCode( short *ix, int *xr, uint32_t begin, uint32_t end, int table)
1057 { 1072 {
1058 if(y > 14) 1073 if(y > 14)
1059 { 1074 {
1075 if(bit + linbits + 1 > 32)
1076 {
1077 putlong( code, bit );
1078 sumbit += bit;
1079 code = bit = 0;
1080 }
1081
1060 code = (code << linbits) | yl; 1082 code = (code << linbits) | yl;
1061 bit += linbits; 1083 bit += linbits;
1062 } 1084 }
@@ -1324,16 +1346,29 @@ int calc_runlen( short *ix, side_info_t *si )
1324/*************************************************************************/ 1346/*************************************************************************/
1325int quantize_int(int *xr, short *ix, side_info_t *si) 1347int quantize_int(int *xr, short *ix, side_info_t *si)
1326{ 1348{
1327 int i, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 }; 1349 unsigned int i, idx, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 };
1328 1350
1329 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4; 1351 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4;
1330 1352
1331 /* check for integer overflow */ 1353 /* check for possible 'out of range' values */
1332 if(((si->max_val + 256) >> 8) * s >= (1622 << 8)) 1354 if(((si->max_val + 256) >> 8) * s >= (65536 << 8))
1333 return 0; 1355 return 0;
1334 1356
1335 for(i=SAMPL2; i--; ) 1357 if(((si->max_val + 256) >> 8) * s < (4096 << 8))
1336 ix[i] = int2idx[(abs(xr[i]) * s + 0x8000) >> 16]; 1358 { /* all values fit the table size */
1359 for(i=SAMPL2; i--; )
1360 ix[i] = int2idx[(xr[i] * s + 0x8000) >> 16];
1361 }
1362 else
1363 { /* check each index wether it fits the table */
1364 for(i=SAMPL2; i--; )
1365 {
1366 idx = (xr[i] * s + 0x08000) >> 16;
1367
1368 if(idx > 4095) ix[i] = int2idx[(idx + 8) >> 4] << 3;
1369 else ix[i] = int2idx[idx];
1370 }
1371 }
1337 1372
1338 return 1; 1373 return 1;
1339} 1374}
@@ -1404,9 +1439,9 @@ int quantize_and_count_bits(int *xr, short *ix, side_info_t *si)
1404 return bits; 1439 return bits;
1405} 1440}
1406 1441
1407/***********************************************************************/ 1442/************************************************************************/
1408/* The code selects the best quantStep for a particular set of scalefacs */ 1443/* The code selects the best quantStep for a particular set of scalefacs*/
1409/***********************************************************************/ 1444/************************************************************************/
1410int inner_loop(int *xr, int max_bits, side_info_t *si) 1445int inner_loop(int *xr, int max_bits, side_info_t *si)
1411{ 1446{
1412 int bits; 1447 int bits;
@@ -2100,12 +2135,45 @@ static inline void byte_swap_frame32(uint32_t *dst, uint32_t *src,
2100} /* byte_swap_frame32 */ 2135} /* byte_swap_frame32 */
2101#endif /* ROCKBOX_LITTLE_ENDIAN */ 2136#endif /* ROCKBOX_LITTLE_ENDIAN */
2102 2137
2138void set_scale_facs(int *mdct_freq)
2139{
2140 unsigned int i, is, ie, k, s;
2141 int max_freq_val, avrg_freq_val;
2142
2143 /* calc average of first 256 frequency values */
2144 for(avrg_freq_val=i=0; i<256; i++)
2145 avrg_freq_val += mdct_freq[i];
2146 avrg_freq_val >>= 8;
2147
2148 /* if max of current band is smaller than average, increase precision */
2149 /* last band keeps untouched (not scaled) */
2150 for(is=k=0; is<scalefac[21]; k++)
2151 {
2152 max_freq_val = 0;
2153
2154 for(i=is, ie=scalefac[k+1]; i<ie; i++)
2155 if(max_freq_val < mdct_freq[i])
2156 max_freq_val = mdct_freq[i];
2157
2158 for(s=0; s<3; s++)
2159 if((max_freq_val<<s) > avrg_freq_val)
2160 break;
2161
2162 band_scale_f[k] = (unsigned char)s;
2163
2164 for(i=is; s && i<ie; i++)
2165 mdct_freq[i] <<= s;
2166
2167 is = ie;
2168 }
2169}
2170
2103STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk) 2171STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2104 ICODE_ATTR; 2172 ICODE_ATTR;
2105STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk) 2173STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2106{ 2174{
2107 int gr, gr_cnt; 2175 int gr, gr_cnt;
2108 int max, min; 2176 uint32_t max;
2109 2177
2110 /* encode one mp3 frame in this loop */ 2178 /* encode one mp3 frame in this loop */
2111 CodedData.bitpos = 0; 2179 CodedData.bitpos = 0;
@@ -2120,7 +2188,8 @@ STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2120 cfg.mpg.padding = 0; 2188 cfg.mpg.padding = 0;
2121 2189
2122 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding 2190 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding
2123 - cfg.sideinfo_len) / cfg.granules / cfg.channels; 2191 - cfg.sideinfo_len) / cfg.granules / cfg.channels
2192 - 42; // reserved for scale_facs
2124 2193
2125 /* shift out old samples */ 2194 /* shift out old samples */
2126 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512); 2195 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512);
@@ -2175,11 +2244,10 @@ STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2175 } 2244 }
2176 } 2245 }
2177 2246
2178 /* Perform imdct of 18 previous + 18 current subband samples 2247 /* Perform imdct of 18 previous + 18 current subband samples */
2179 for integer precision do this loop twice (if neccessary) 2248 /* for integer precision do this loop again (if neccessary) */
2180 */ 2249 shift = 14 - (cfg.cod_info[gr][ch].additStep >> 2);
2181 shift = k = 14; 2250 for(k=1,ii=0; ii<3 && k; ii++)
2182 for(ii=0; ii<2 && k; ii++)
2183 { 2251 {
2184 int *mdct = mdct_freq; 2252 int *mdct = mdct_freq;
2185 int band; 2253 int band;
@@ -2230,30 +2298,41 @@ STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk)
2230 } 2298 }
2231 } 2299 }
2232 2300
2233 max = min = 0; 2301 max = 0;
2234 for(k=0; k<576; k++) 2302 for(k=0; k<576; k++)
2235 { 2303 {
2236 mdct_freq[k] = shft13(mdct_freq[k]); 2304 if(mdct_freq[k] < 0)
2237 if(max < mdct_freq[k]) max = mdct_freq[k]; 2305 {
2238 if(min > mdct_freq[k]) min = mdct_freq[k]; 2306 mdct_sign[k] = 1; /* negative */
2239 } 2307 mdct_freq[k] = shft13(-mdct_freq[k]);
2308 }
2309 else
2310 {
2311 mdct_sign[k] = 0; /* positive */
2312 mdct_freq[k] = shft13(mdct_freq[k]);
2313 }
2240 2314
2241 max = (max > -min) ? max : -min; 2315 if(max < (uint32_t)mdct_freq[k])
2242 cfg.cod_info[gr][ch].max_val = (long)max; 2316 max = (uint32_t)mdct_freq[k];
2317 }
2318 cfg.cod_info[gr][ch].max_val = max;
2243 2319
2244 /* calc new shift for higher integer precision */ 2320 /* calc new shift for higher integer precision */
2245 for(k=0; max<(0x3c00>>k); k++); 2321 for(k=0; max<(uint32_t)(0x7800>>k); k++) shift--;
2246 shift = 12 - k; 2322 for( ; (max>>k)>=(uint32_t)0x10000; k++) shift++;
2323 if(shift < 0) shift = 0;
2247 } 2324 }
2248 2325
2249 cfg.cod_info[gr][ch].quantStep += 2326 cfg.cod_info[gr][ch].quantStep +=
2250 cfg.cod_info[gr][ch].additStep; 2327 cfg.cod_info[gr][ch].additStep;
2251 2328
2329 set_scale_facs(mdct_freq);
2330
2252 /* bit and noise allocation */ 2331 /* bit and noise allocation */
2253 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch], 2332 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch],
2254 gr_cnt--); 2333 gr_cnt--);
2255 /* write the frame to the bitstream */ 2334 /* write the frame to the bitstream */
2256 Huffmancodebits(enc_data, mdct_freq, 2335 Huffmancodebits(enc_data, mdct_sign,
2257 &cfg.cod_info[gr][ch]); 2336 &cfg.cod_info[gr][ch]);
2258 2337
2259 cfg.cod_info[gr][ch].quantStep -= 2338 cfg.cod_info[gr][ch].quantStep -=
diff --git a/apps/plugins/mp3_encoder.c b/apps/plugins/mp3_encoder.c
index db9c9d4e67..0dce32e9df 100644
--- a/apps/plugins/mp3_encoder.c
+++ b/apps/plugins/mp3_encoder.c
@@ -11,6 +11,7 @@
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details. */ 12 * Library General Public License for more details. */
13 13
14#include <inttypes.h>
14#include "plugin.h" 15#include "plugin.h"
15 16
16PLUGIN_HEADER 17PLUGIN_HEADER
@@ -29,13 +30,8 @@ MEM_FUNCTION_WRAPPERS(rb);
29 30
30enum e_byte_order { order_unknown, order_bigEndian, order_littleEndian }; 31enum e_byte_order { order_unknown, order_bigEndian, order_littleEndian };
31 32
32typedef unsigned long uint32;
33typedef unsigned short uint16;
34typedef unsigned char uint8;
35
36
37typedef struct { 33typedef struct {
38 int type; /* 0=(22.05,24,16kHz) 1=(44.1,48,32kHz) */ 34 int type; /* 0=(MPEG2 - 22.05,24,16kHz) 1=(MPEG1 - 44.1,48,32kHz) */
39 int mode; /* 0=stereo, 1=jstereo, 2=dual, 3=mono */ 35 int mode; /* 0=stereo, 1=jstereo, 2=dual, 3=mono */
40 int bitrate; 36 int bitrate;
41 int padding; 37 int padding;
@@ -46,17 +42,17 @@ typedef struct {
46 42
47/* Side information */ 43/* Side information */
48typedef struct { 44typedef struct {
49 uint32 part2_3_length; 45 uint32_t part2_3_length;
50 int count1; /* number of 0-1-quadruples */ 46 int count1; /* number of 0-1-quadruples */
51 uint32 global_gain; 47 uint32_t global_gain;
52 uint32 table_select[4]; 48 uint32_t table_select[4];
53 uint32 region_0_1; 49 uint32_t region_0_1;
54 uint32 address1; 50 uint32_t address1;
55 uint32 address2; 51 uint32_t address2;
56 uint32 address3; 52 uint32_t address3;
57 long quantStep; 53 long quantStep;
58 long additStep; 54 long additStep;
59 long max_val; 55 uint32_t max_val;
60} side_info_t; 56} side_info_t;
61 57
62typedef struct { 58typedef struct {
@@ -76,14 +72,14 @@ typedef struct {
76} config_t; 72} config_t;
77 73
78typedef struct { 74typedef struct {
79 int bitpos; /* current bitpos for writing */ 75 int bitpos; /* current bitpos for writing */
80 uint32 bbuf[263]; 76 uint32_t bbuf[263];
81} BF_Data; 77} BF_Data;
82 78
83struct huffcodetab { 79struct huffcodetab {
84 int len; /* max. index */ 80 int len; /* max. index */
85 const uint8 *table; /* pointer to array[len][len] */ 81 const uint8_t *table; /* pointer to array[len][len] */
86 const uint8 *hlen; /* pointer to array[len][len] */ 82 const uint8_t *hlen; /* pointer to array[len][len] */
87}; 83};
88 84
89struct huffcodebig { 85struct huffcodebig {
@@ -100,98 +96,99 @@ struct huffcodebig {
100#define shft_n(x,n) ((x) >> n) 96#define shft_n(x,n) ((x) >> n)
101#define SQRT 724 /* sqrt(2) * 512 */ 97#define SQRT 724 /* sqrt(2) * 512 */
102 98
103short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */ 99static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */
104int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */ 100static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */
105int mdct_freq [SAMPL2] IBSS_ATTR; /* 9216 Bytes */ 101static int mdct_freq [SAMPL2] IBSS_ATTR; /* 2304 Bytes */
106short enc_data [SAMPL2] IBSS_ATTR; /* 4608 Bytes */ 102static char mdct_sign [SAMPL2] IBSS_ATTR; /* 576 Bytes */
107uint32 scalefac [23] IBSS_ATTR; /* 92 Bytes */ 103static short enc_data [SAMPL2] IBSS_ATTR; /* 1152 Bytes */
108BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */ 104static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */
109int ca [8] IBSS_ATTR; /* 32 Bytes */ 105static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */
110int cs [8] IBSS_ATTR; /* 32 Bytes */ 106static int ca [8] IBSS_ATTR; /* 32 Bytes */
111int cx [9] IBSS_ATTR; /* 36 Bytes */ 107static int cs [8] IBSS_ATTR; /* 32 Bytes */
112int win [18][4] IBSS_ATTR; /* 288 Bytes */ 108static int cx [9] IBSS_ATTR; /* 36 Bytes */
113short enwindow [15*27+24] IBSS_ATTR; /* 862 Bytes */ 109static int win [18][4] IBSS_ATTR; /* 288 Bytes */
114short int2idx [4096] IBSS_ATTR; /* 8192 Bytes */ 110static short enwindow [15*27+24] IBSS_ATTR; /* 862 Bytes */
115uint8 ht_count [2][2][16] IBSS_ATTR; /* 64 Bytes */ 111static short int2idx [4096] IBSS_ATTR; /* 8192 Bytes */
116uint32 tab01 [ 16] IBSS_ATTR; /* 64 Bytes */ 112static uint8_t ht_count [2][2][16] IBSS_ATTR; /* 64 Bytes */
117uint32 tab23 [ 9] IBSS_ATTR; /* 36 Bytes */ 113static uint32_t tab01 [ 16] IBSS_ATTR; /* 64 Bytes */
118uint32 tab56 [ 16] IBSS_ATTR; /* 64 Bytes */ 114static uint32_t tab23 [ 9] IBSS_ATTR; /* 36 Bytes */
119uint32 tab1315 [256] IBSS_ATTR; /* 1024 Bytes */ 115static uint32_t tab56 [ 16] IBSS_ATTR; /* 64 Bytes */
120uint32 tab1624 [256] IBSS_ATTR; /* 1024 Bytes */ 116static uint32_t tab1315 [256] IBSS_ATTR; /* 1024 Bytes */
121uint32 tab789 [ 36] IBSS_ATTR; /* 144 Bytes */ 117static uint32_t tab1624 [256] IBSS_ATTR; /* 1024 Bytes */
122uint32 tabABC [ 64] IBSS_ATTR; /* 256 Bytes */ 118static uint32_t tab789 [ 36] IBSS_ATTR; /* 144 Bytes */
123uint8 t1HB [ 4] IBSS_ATTR; 119static uint32_t tabABC [ 64] IBSS_ATTR; /* 256 Bytes */
124uint8 t2HB [ 9] IBSS_ATTR; 120static uint8_t t1HB [ 4] IBSS_ATTR;
125uint8 t3HB [ 9] IBSS_ATTR; 121static uint8_t t2HB [ 9] IBSS_ATTR;
126uint8 t5HB [ 16] IBSS_ATTR; 122static uint8_t t3HB [ 9] IBSS_ATTR;
127uint8 t6HB [ 16] IBSS_ATTR; 123static uint8_t t5HB [ 16] IBSS_ATTR;
128uint8 t7HB [ 36] IBSS_ATTR; 124static uint8_t t6HB [ 16] IBSS_ATTR;
129uint8 t8HB [ 36] IBSS_ATTR; 125static uint8_t t7HB [ 36] IBSS_ATTR;
130uint8 t9HB [ 36] IBSS_ATTR; 126static uint8_t t8HB [ 36] IBSS_ATTR;
131uint8 t10HB [ 64] IBSS_ATTR; 127static uint8_t t9HB [ 36] IBSS_ATTR;
132uint8 t11HB [ 64] IBSS_ATTR; 128static uint8_t t10HB [ 64] IBSS_ATTR;
133uint8 t12HB [ 64] IBSS_ATTR; 129static uint8_t t11HB [ 64] IBSS_ATTR;
134uint8 t13HB [256] IBSS_ATTR; 130static uint8_t t12HB [ 64] IBSS_ATTR;
135uint8 t15HB [256] IBSS_ATTR; 131static uint8_t t13HB [256] IBSS_ATTR;
136uint16 t16HB [256] IBSS_ATTR; 132static uint8_t t15HB [256] IBSS_ATTR;
137uint16 t24HB [256] IBSS_ATTR; 133static uint16_t t16HB [256] IBSS_ATTR;
138uint8 t1l [ 8] IBSS_ATTR; 134static uint16_t t24HB [256] IBSS_ATTR;
139uint8 t2l [ 9] IBSS_ATTR; 135static uint8_t t1l [ 8] IBSS_ATTR;
140uint8 t3l [ 9] IBSS_ATTR; 136static uint8_t t2l [ 9] IBSS_ATTR;
141uint8 t5l [ 16] IBSS_ATTR; 137static uint8_t t3l [ 9] IBSS_ATTR;
142uint8 t6l [ 16] IBSS_ATTR; 138static uint8_t t5l [ 16] IBSS_ATTR;
143uint8 t7l [ 36] IBSS_ATTR; 139static uint8_t t6l [ 16] IBSS_ATTR;
144uint8 t8l [ 36] IBSS_ATTR; 140static uint8_t t7l [ 36] IBSS_ATTR;
145uint8 t9l [ 36] IBSS_ATTR; 141static uint8_t t8l [ 36] IBSS_ATTR;
146uint8 t10l [ 64] IBSS_ATTR; 142static uint8_t t9l [ 36] IBSS_ATTR;
147uint8 t11l [ 64] IBSS_ATTR; 143static uint8_t t10l [ 64] IBSS_ATTR;
148uint8 t12l [ 64] IBSS_ATTR; 144static uint8_t t11l [ 64] IBSS_ATTR;
149uint8 t13l [256] IBSS_ATTR; 145static uint8_t t12l [ 64] IBSS_ATTR;
150uint8 t15l [256] IBSS_ATTR; 146static uint8_t t13l [256] IBSS_ATTR;
151uint8 t16l [256] IBSS_ATTR; 147static uint8_t t15l [256] IBSS_ATTR;
152uint8 t24l [256] IBSS_ATTR; 148static uint8_t t16l [256] IBSS_ATTR;
153struct huffcodetab ht [HTN] IBSS_ATTR; 149static uint8_t t24l [256] IBSS_ATTR;
154 150static struct huffcodetab ht [HTN] IBSS_ATTR;
155static const uint8 ht_count_const[2][2][16] = 151
152static const uint8_t ht_count_const[2][2][16] =
156{ { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */ 153{ { { 1, 5, 4, 5, 6, 5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 }, /* table0 */
157 { 1, 5, 5, 7, 5, 8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } }, /* hleng0 */ 154 { 1, 5, 5, 7, 5, 8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } }, /* hleng0 */
158 { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, /* table1 */ 155 { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, /* table1 */
159 { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */ 156 { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */
160 157
161static const uint8 t1HB_const[4] = {1,1,1,0}; 158static const uint8_t t1HB_const[4] = {1,1,1,0};
162static const uint8 t2HB_const[9] = {1,2,1,3,1,1,3,2,0}; 159static const uint8_t t2HB_const[9] = {1,2,1,3,1,1,3,2,0};
163static const uint8 t3HB_const[9] = {3,2,1,1,1,1,3,2,0}; 160static const uint8_t t3HB_const[9] = {3,2,1,1,1,1,3,2,0};
164static const uint8 t5HB_const[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0}; 161static const uint8_t t5HB_const[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0};
165static const uint8 t6HB_const[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0}; 162static const uint8_t t6HB_const[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0};
166 163
167static const uint8 t7HB_const[36] = 164static const uint8_t t7HB_const[36] =
168{ 1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4, 165{ 1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4,
169 12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 }; 166 12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 };
170 167
171static const uint8 t8HB_const[36] = 168static const uint8_t t8HB_const[36] =
172{ 3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3, 169{ 3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3,
173 19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 }; 170 19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 };
174 171
175static const uint8 t9HB_const[36] = 172static const uint8_t t9HB_const[36] =
176{ 7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5, 173{ 7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5,
177 15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 }; 174 15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 };
178 175
179static const uint8 t10HB_const[64] = 176static const uint8_t t10HB_const[64] =
180{1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32, 177{1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32,
181 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22, 178 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22,
182 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 }; 179 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 };
183 180
184static const uint8 t11HB_const[64] = 181static const uint8_t t11HB_const[64] =
185{3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30, 182{3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30,
186 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26, 183 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26,
187 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 }; 184 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 };
188 185
189static const uint8 t12HB_const[64] = 186static const uint8_t t12HB_const[64] =
190{9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21, 187{9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21,
19130,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17, 18830,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17,
19231,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 }; 18931,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 };
193 190
194static const uint8 t13HB_const[256] = 191static const uint8_t t13HB_const[256] =
195{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, 192{1,5,14,21,34,51,46,71,42,52,68,52,67,44,43,19,3,4,12,19,31,26,44,33,31,24,32,
196 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, 193 24,31,35,22,14,15,13,23,36,59,49,77,65,29,40,30,40,27,33,42,16,22,20,37,61,56,
197 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, 194 79,73,64,43,76,56,37,26,31,25,14,35,16,60,57,97,75,114,91,54,73,55,41,48,53,
@@ -203,7 +200,7 @@ static const uint8 t13HB_const[256] =
203 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, 200 45,21,34,64,56,50,49,45,31,19,12,15,10,7,6,3,48,23,20,39,36,35,53,21,16,23,13,
204 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 }; 201 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 };
205 202
206static const uint8 t15HB_const[256] = 203static const uint8_t t15HB_const[256] =
207{7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51, 204{7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51,
208 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, 205 42,70,52,83,65,41,59,36,19,17,15,24,41,34,59,48,40,64,50,78,62,80,56,33,29,28,
209 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, 206 25,43,39,63,55,93,76,59,93,72,54,75,50,29,52,22,42,40,67,57,95,79,72,57,89,69,
@@ -215,7 +212,7 @@ static const uint8 t15HB_const[256] =
215 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, 212 24,16,22,13,14,7,91,44,39,38,34,63,52,45,31,52,28,19,14,8,9,3,123,60,58,53,47,
216 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}; 213 43,32,22,37,24,17,12,15,10,2,1,71,37,34,30,28,20,17,26,21,16,10,6,8,6,2,0};
217 214
218static const uint16 t16HB_const[256] = 215static const uint16_t t16HB_const[256] =
219{1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47, 216{1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47,
220 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209, 217 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209,
221 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65, 218 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65,
@@ -230,7 +227,7 @@ static const uint16 t16HB_const[256] =
230 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3, 227 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3,
231 1,3}; 228 1,3};
232 229
233static const uint16 t24HB_const[256] = 230static const uint16_t t24HB_const[256] =
234{15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71, 231{15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71,
235 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194, 232 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194,
236 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293, 233 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293,
@@ -245,7 +242,7 @@ static const uint16 t24HB_const[256] =
245 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360, 242 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360,
246 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3}; 243 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3};
247 244
248static const uint32 tab1315_const[256] = 245static const uint32_t tab1315_const[256] =
249{ 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a, 246{ 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a,
250 0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e, 247 0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e,
251 0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a, 248 0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a,
@@ -279,18 +276,18 @@ static const uint32 tab1315_const[256] =
279 0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e, 276 0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e,
280 0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f }; 277 0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f };
281 278
282static const uint32 tab01_const[16] = 279static const uint32_t tab01_const[16] =
283{ 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007, 280{ 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007,
284 0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 }; 281 0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 };
285 282
286static const uint32 tab23_const[ 9] = 283static const uint32_t tab23_const[ 9] =
287{ 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 }; 284{ 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 };
288 285
289static const uint32 tab56_const[16] = 286static const uint32_t tab56_const[16] =
290{ 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007, 287{ 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007,
291 0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 }; 288 0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 };
292 289
293static const uint32 tab789_const[36] = 290static const uint32_t tab789_const[36] =
294{0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004, 291{0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004,
295 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806, 292 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806,
296 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808, 293 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808,
@@ -298,7 +295,7 @@ static const uint32 tab789_const[36] =
298 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b, 295 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b,
299 0x00c0340b}; 296 0x00c0340b};
300 297
301static const uint32 tabABC_const[64] = 298static const uint32_t tabABC_const[64] =
302{0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a, 299{0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a,
303 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809, 300 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809,
304 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809, 301 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809,
@@ -310,7 +307,7 @@ static const uint32 tabABC_const[64] =
310 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b, 307 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b,
311 0x00d0300c}; 308 0x00d0300c};
312 309
313static const uint32 tab1624_const[256] = 310static const uint32_t tab1624_const[256] =
314{0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a, 311{0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a,
315 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c, 312 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c,
316 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009, 313 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009,
@@ -349,34 +346,34 @@ static const uint32 tab1624_const[256] =
349 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a, 346 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a,
350 0x000d000a,0x000d000a,0x000d000a,0x000a0006}; 347 0x000d000a,0x000d000a,0x000d000a,0x000a0006};
351 348
352static const uint8 t1l_const[8] = {1,3,2,3,1,4,3,5}; 349static const uint8_t t1l_const[8] = {1,3,2,3,1,4,3,5};
353static const uint8 t2l_const[9] = {1,3,6,3,3,5,5,5,6}; 350static const uint8_t t2l_const[9] = {1,3,6,3,3,5,5,5,6};
354static const uint8 t3l_const[9] = {2,2,6,3,2,5,5,5,6}; 351static const uint8_t t3l_const[9] = {2,2,6,3,2,5,5,5,6};
355static const uint8 t5l_const[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8}; 352static const uint8_t t5l_const[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8};
356static const uint8 t6l_const[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7}; 353static const uint8_t t6l_const[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7};
357 354
358static const uint8 t7l_const[36] = 355static const uint8_t t7l_const[36] =
359{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}; 356{1,3,6,8,8,9,3,4,6,7,7,8,6,5,7,8,8,9,7,7,8,9,9,9,7,7,8,9,9,10,8,8,9,10,10,10};
360 357
361static const uint8 t8l_const[36] = 358static const uint8_t t8l_const[36] =
362{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}; 359{2,3,6,8,8,9,3,2,4,8,8,8,6,4,6,8,8,9,8,8,8,9,9,10,8,7,8,9,10,10,9,8,9,9,11,11};
363 360
364static const uint8 t9l_const[36] = 361static const uint8_t t9l_const[36] =
365{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}; 362{3,3,5,6,8,9,3,3,4,5,6,8,4,4,5,6,7,8,6,5,6,7,7,8,7,6,7,7,8,9,8,7,8,8,9,9};
366 363
367static const uint8 t10l_const[64] = 364static const uint8_t t10l_const[64] =
368{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, 365{1,3,6,8,9,9,9,10,3,4,6,7,8,9,8,8,6,6,7,8,9,10,9,9,7,7,8,9,10,10,9,10,8,8,9,10,
369 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}; 366 10,10,10,10,9,9,10,10,11,11,10,11,8,8,9,10,10,10,11,11,9,8,9,10,10,11,11,11};
370 367
371static const uint8 t11l_const[64] = 368static const uint8_t t11l_const[64] =
372{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, 369{2,3,5,7,8,9,8,9,3,3,4,6,8,8,7,8,5,5,6,7,8,9,8,8,7,6,7,9,8,10,8,9,8,8,8,9,9,10,
373 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}; 370 9,10,8,8,9,10,10,11,10,11,8,7,7,8,9,10,10,10,8,7,8,9,10,10,10,10};
374 371
375static const uint8 t12l_const[64] = 372static const uint8_t t12l_const[64] =
376{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, 373{4,3,5,7,8,9,9,9,3,3,4,5,7,7,8,8,5,4,5,6,7,8,7,8,6,5,6,6,7,8,8,8,7,6,7,7,8,
377 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}; 374 8,8,9,8,7,8,8,8,9,8,9,8,7,7,8,8,9,9,10,9,8,8,9,9,9,9,10};
378 375
379static const uint8 t13l_const[256] = 376static const uint8_t t13l_const[256] =
380{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, 377{1,4,6,7,8,9,9,10,9,10,11,11,12,12,13,13,3,4,6,7,8,8,9,9,9,9,10,10,11,12,12,12,
381 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, 378 6,6,7,8,9,9,10,10,9,10,10,11,11,12,13,13,7,7,8,9,9,10,10,10,10,11,11,11,11,12,
382 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, 379 13,13,8,7,9,9,10,10,11,11,10,11,11,12,12,13,13,14,9,8,9,10,10,10,11,11,11,11,
@@ -388,7 +385,7 @@ static const uint8 t13l_const[256] =
388 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, 385 16,16,13,12,12,13,13,13,15,14,14,17,15,15,15,17,16,16,12,12,13,14,14,14,15,14,
389 15,15,16,16,19,18,19,16}; 386 15,15,16,16,19,18,19,16};
390 387
391static const uint8 t15l_const[256] = 388static const uint8_t t15l_const[256] =
392{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, 389{3,4,5,7,7,8,9,9,9,10,10,11,11,11,12,13,4,3,5,6,7,7,8,8,8,9,9,10,10,10,11,11,5,
393 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, 390 5,5,6,7,7,8,8,8,9,9,10,10,11,11,11,6,6,6,7,7,8,8,9,9,9,10,10,10,11,11,11,7,6,
394 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, 391 7,7,8,8,9,9,9,9,10,10,10,11,11,11,8,7,7,8,8,8,9,9,9,9,10,10,11,11,11,12,9,7,8,
@@ -399,7 +396,7 @@ static const uint8 t15l_const[256] =
399 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, 396 11,11,11,11,12,12,12,12,12,13,13,12,11,11,11,11,11,11,11,12,12,12,12,13,13,12,
400 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13}; 397 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13};
401 398
402static const uint8 t16l_const[256] = 399static const uint8_t t16l_const[256] =
403{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, 400{1,4,6,8,9,9,10,10,11,11,11,12,12,12,13,9,3,4,6,7,8,9,9,9,10,10,10,11,12,11,12,
404 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, 401 8,6,6,7,8,9,9,10,10,11,10,11,11,11,12,12,9,8,7,8,9,9,10,10,10,11,11,12,12,12,
405 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, 402 13,13,10,9,8,9,9,10,10,11,11,11,12,12,12,13,13,13,9,9,8,9,9,10,11,11,12,11,12,
@@ -411,7 +408,7 @@ static const uint8 t16l_const[256] =
411 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, 408 17,15,11,13,13,11,12,14,14,13,14,14,15,16,15,17,15,14,11,9,8,8,9,9,10,10,10,
412 11,11,11,11,11,11,11,8}; 409 11,11,11,11,11,11,11,8};
413 410
414static const uint8 t24l_const[256] = 411static const uint8_t t24l_const[256] =
415{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, 412{4,4,6,7,8,9,9,10,10,11,11,11,11,11,12,9,4,4,5,6,7,8,8,9,9,9,10,10,10,10,10,8,
416 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, 413 6,5,6,7,7,8,8,9,9,9,9,10,10,10,11,7,7,6,7,7,8,8,8,9,9,9,9,10,10,10,10,7,8,7,7,
417 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, 414 8,8,8,8,9,9,9,10,10,10,10,11,7,9,7,8,8,8,8,9,9,9,9,10,10,10,10,10,7,9,8,8,8,8,
@@ -460,8 +457,8 @@ static const struct huffcodebig ht_big[HTN] =
460 457
461static const struct 458static const struct
462{ 459{
463 uint32 region0_cnt; 460 uint32_t region0_cnt;
464 uint32 region1_cnt; 461 uint32_t region1_cnt;
465} subdv_table[23] = 462} subdv_table[23] =
466{ {0, 0}, /* 0 bands */ 463{ {0, 0}, /* 0 bands */
467 {0, 0}, /* 1 bands */ 464 {0, 0}, /* 1 bands */
@@ -488,7 +485,7 @@ static const struct
488 {6, 7}, /* 22 bands */ 485 {6, 7}, /* 22 bands */
489}; 486};
490 487
491static const uint32 sfBand[6][23] = 488static const uint32_t sfBand[6][23] =
492{ 489{
493/* Table B.2.b: 22.05 kHz */ 490/* Table B.2.b: 22.05 kHz */
494{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576}, 491{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
@@ -716,9 +713,13 @@ static const int order[32] =
716{ 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29, 713{ 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
717 2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 }; 714 2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 };
718 715
719static const int bitr_index[2][15] = 716static const long sampr_index[2][3] =
720{ {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160}, 717{ { 22050, 24000, 16000 }, /* MPEG 2 */
721 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} }; 718 { 44100, 48000, 32000 } }; /* MPEG 1 */
719
720static const long bitr_index[2][15] =
721{ {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160}, /* MPEG 2 */
722 {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} }; /* MPEG 1 */
722 723
723static const int num_bands[3][15] = 724static const int num_bands[3][15] =
724{ {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32}, 725{ {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32},
@@ -808,21 +809,23 @@ static const int win_const[18][4] = {
808 809
809static const char* wav_filename; 810static const char* wav_filename;
810static int mp3file, wavfile, wav_size, frames; 811static int mp3file, wavfile, wav_size, frames;
811static uint32 enc_buffer[16384]; /* storage for 65536 Bytes */ 812static uint32_t enc_buffer[16384]; /* storage for 65536 Bytes */
812static int enc_chunk = 0; /* encode chunk counter */ 813static int enc_chunk = 0; /* encode chunk counter */
813static int enc_size; 814static int enc_size;
814static config_t cfg; 815static config_t cfg;
816static uint8_t band_scale_f[22];
817
815 818
816/* forward declarations */ 819/* forward declarations */
817int HuffmanCode( short *ix, int *xr, uint32 begin, uint32 end, int table); 820static int HuffmanCode( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
818int HuffmanCod1( short *ix, int *xr, uint32 begin, uint32 end, int table); 821static int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table);
819void putbits(uint32 val, uint32 nbit); 822static void putbits(uint32_t val, uint32_t nbit);
820int find_best_2( short *ix, uint32 start, uint32 end, const uint32 *table, 823static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
821 uint32 len, int *bits); 824 uint32_t len, int *bits);
822int find_best_3( short *ix, uint32 start, uint32 end, const uint32 *table, 825static int find_best_3( short *ix, uint32_t start, uint32_t end, const uint32_t *table,
823 uint32 len, int *bits); 826 uint32_t len, int *bits);
824int count_bit1 ( short *ix, uint32 start, uint32 end, int *bits ); 827static int count_bit1 ( short *ix, uint32_t start, uint32_t end, int *bits );
825int count_bigv ( short *ix, uint32 start, uint32 end, int table0, int table1, 828static int count_bigv ( short *ix, uint32_t start, uint32_t end, int table0, int table1,
826 int *bits); 829 int *bits);
827 830
828 831
@@ -891,7 +894,7 @@ int wave_open(void)
891 return 0; 894 return 0;
892} 895}
893 896
894int read_samples(uint32 *buffer, int num_samples) 897int read_samples(uint32_t *buffer, int num_samples)
895{ 898{
896 int s, samples = rb->read(wavfile, buffer, 4 * num_samples) / 4; 899 int s, samples = rb->read(wavfile, buffer, 4 * num_samples) / 4;
897 /* Pad last sample with zeros */ 900 /* Pad last sample with zeros */
@@ -901,30 +904,50 @@ int read_samples(uint32 *buffer, int num_samples)
901 return samples; 904 return samples;
902} 905}
903 906
904inline uint32 myswap32(uint32 val) 907inline uint32_t myswap32(uint32_t val)
905{ 908{
906 const uint8* v = (const uint8*)&val; 909 const uint8_t* v = (const uint8_t*)&val;
907 910
908 return ((uint32)v[0]<<24) | ((uint32)v[1]<<16) | ((uint32)v[2]<<8) | v[3]; 911 return ((uint32_t)v[0]<<24) | ((uint32_t)v[1]<<16) | ((uint32_t)v[2]<<8) | v[3];
909} 912}
910 913
911void encodeSideInfo( side_info_t si[2][2] ) 914static void encodeSideInfo( side_info_t si[2][2] )
912{ 915{
913 int gr, ch, header; 916 int gr, ch, header;
914 uint32 cc=0, sz=0; 917 uint32_t cc=0, sz=0;
915 918
916 header = 0xfff00000; 919 /*
917 header |= cfg.mpg.type << 19; /* mp3 type: 1 */ 920 * MPEG header layout:
918 header |= 1 << 17; /* mp3 layer: 1 */ 921 * AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
919 header |= 1 << 16; /* mp3 crc: 0 */ 922 * A (31-21) = frame sync
923 * B (20-19) = MPEG type
924 * C (18-17) = MPEG layer
925 * D (16) = protection bit
926 * E (15-12) = bitrate index
927 * F (11-10) = samplerate index
928 * G (9) = padding bit
929 * H (8) = private bit
930 * I (7-6) = channel mode
931 * J (5-4) = mode extension (jstereo only)
932 * K (3) = copyright bit
933 * L (2) = original
934 * M (1-0) = emphasis
935 */
936
937 header = (0xfff00000) | /* frame sync (AAAAAAAAA AAA)
938 mp3 type (upper): 1 (B) */
939 (0x01 << 17) | /* mp3 layer: 01 (CC) */
940 ( 0x1 << 16) | /* mp3 crc: 1 (D) */
941 ( 0x1 << 2); /* mp3 org: 1 (L) */
942 header |= cfg.mpg.type << 19;
920 header |= cfg.mpg.bitr_id << 12; 943 header |= cfg.mpg.bitr_id << 12;
921 header |= cfg.mpg.smpl_id << 10; 944 header |= cfg.mpg.smpl_id << 10;
922 header |= cfg.mpg.padding << 9; 945 header |= cfg.mpg.padding << 9;
923 header |= cfg.mpg.mode << 6; 946 header |= cfg.mpg.mode << 6;
924 header |= 1 << 2; /* mp3 original: 1 */ 947 /* no emphasis (bits 0-1) */
925 putbits( header, 32 ); 948 putbits( header, 32 );
926 949
927 if(cfg.mpg.type) 950 if(cfg.mpg.type == 1)
928 { /* MPEG1 */ 951 { /* MPEG1 */
929 if(cfg.channels == 2) { putlong( 0, 20); } 952 if(cfg.channels == 2) { putlong( 0, 20); }
930 else { putlong( 0, 18); } 953 else { putlong( 0, 18); }
@@ -934,14 +957,16 @@ void encodeSideInfo( side_info_t si[2][2] )
934 { 957 {
935 side_info_t *gi = &si[gr][ch]; 958 side_info_t *gi = &si[gr][ch];
936 959
937 putlong( gi->part2_3_length, 12 ); 960 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
938 putlong( gi->address3>>1, 9 ); 961 putlong( gi->address3>>1, 9 );
939 putlong( gi->global_gain, 8 ); 962 putlong( gi->global_gain, 8 );
940 putlong( gi->table_select[0], 10 ); 963 putlong( 9, 4 ); /* set scale_facs compr type */
941 putlong( gi->table_select[1], 5 ); 964 putlong( gi->table_select[0], 6 );
942 putlong( gi->table_select[2], 5 ); 965 putlong( gi->table_select[1], 5 );
943 putlong( gi->region_0_1, 7 ); 966 putlong( gi->table_select[2], 5 );
944 putlong( gi->table_select[3], 3 ); 967 putlong( gi->region_0_1, 7 );
968 putlong( 1 , 2 ); /* set scale_facs to 1bit */
969 putlong( gi->table_select[3], 1 );
945 } 970 }
946 } 971 }
947 else 972 else
@@ -953,14 +978,16 @@ void encodeSideInfo( side_info_t si[2][2] )
953 { 978 {
954 side_info_t *gi = &si[0][ch]; 979 side_info_t *gi = &si[0][ch];
955 980
956 putlong( gi->part2_3_length, 12); 981 putlong((gi->part2_3_length+42),12 ); /* add scale_facs array size */
957 putlong( gi->address3>>1, 9); 982 putlong( gi->address3>>1, 9 );
958 putlong( gi->global_gain, 8); 983 putlong( gi->global_gain, 8 );
959 putlong( gi->table_select[0], 15); 984 putlong( 0xCA, 9 ); /* set scale_facs compr type */
960 putlong( gi->table_select[1], 5); 985 putlong( gi->table_select[0], 6 );
961 putlong( gi->table_select[2], 5); 986 putlong( gi->table_select[1], 5 );
962 putlong( gi->region_0_1, 7); 987 putlong( gi->table_select[2], 5 );
963 putlong( gi->table_select[3], 2); 988 putlong( gi->region_0_1 , 7 );
989 putlong( 1 , 1 ); /* set scale_facs to 1bit */
990 putlong( gi->table_select[3], 1 );
964 } 991 }
965 } 992 }
966 /* flush remaining bits */ 993 /* flush remaining bits */
@@ -969,7 +996,7 @@ void encodeSideInfo( side_info_t si[2][2] )
969 996
970/* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS, 997/* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS,
971 as well as the definitions of the side information on pages 26 and 27. */ 998 as well as the definitions of the side information on pages 26 and 27. */
972void Huffmancodebits( short *ix, int *xr, side_info_t *gi ) 999static void Huffmancodebits( short *ix, char *xr_sign, side_info_t *gi )
973{ 1000{
974 int region1 = gi->address1; 1001 int region1 = gi->address1;
975 int region2 = gi->address2; 1002 int region2 = gi->address2;
@@ -977,18 +1004,27 @@ void Huffmancodebits( short *ix, int *xr, side_info_t *gi )
977 int count1 = bigvals + (gi->count1 << 2); 1004 int count1 = bigvals + (gi->count1 << 2);
978 int stuffBits = 0; 1005 int stuffBits = 0;
979 int bits = 0; 1006 int bits = 0;
1007 int i, v;
1008
1009 for(i=v=0; i<32; i+=2)
1010 v |= band_scale_f[i>>1] << (30-i);
1011 putbits(v, 32); // store scale_facs (part1)
1012
1013 for(v=0; i<42; i+=2)
1014 v |= band_scale_f[i>>1] << (40-i);
1015 putbits(v, 10); // store scale_facs (part2)
980 1016
981 if(region1 > 0) 1017 if(region1 > 0)
982 bits += HuffmanCode(ix, xr, 0 , region1, gi->table_select[0]); 1018 bits += HuffmanCode(ix, xr_sign, 0 , region1, gi->table_select[0]);
983 1019
984 if(region2 > region1) 1020 if(region2 > region1)
985 bits += HuffmanCode(ix, xr, region1, region2, gi->table_select[1]); 1021 bits += HuffmanCode(ix, xr_sign, region1, region2, gi->table_select[1]);
986 1022
987 if(bigvals > region2) 1023 if(bigvals > region2)
988 bits += HuffmanCode(ix, xr, region2, bigvals, gi->table_select[2]); 1024 bits += HuffmanCode(ix, xr_sign, region2, bigvals, gi->table_select[2]);
989 1025
990 if(count1 > bigvals) 1026 if(count1 > bigvals)
991 bits += HuffmanCod1(ix, xr, bigvals, count1, gi->table_select[3]); 1027 bits += HuffmanCod1(ix, xr_sign, bigvals, count1, gi->table_select[3]);
992 1028
993 if((stuffBits = gi->part2_3_length - bits) > 0) 1029 if((stuffBits = gi->part2_3_length - bits) > 0)
994 { 1030 {
@@ -1003,15 +1039,15 @@ void Huffmancodebits( short *ix, int *xr, side_info_t *gi )
1003 } 1039 }
1004} 1040}
1005 1041
1006int HuffmanCod1( short *ix, int *xr, uint32 begin, uint32 end, int tbl) 1042int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int tbl)
1007{ 1043{
1008 uint32 cc=0, sz=0; 1044 uint32_t cc=0, sz=0;
1009 uint32 i, d, p; 1045 uint32_t i, d, p;
1010 int sumbit=0, s=0, l=0, v, w, x, y; 1046 int sumbit=0, s=0, l=0, v, w, x, y;
1011 #define sgnv (xr[i+0] < 0 ? 1 : 0) 1047 #define sgnv xr_sign[i+0]
1012 #define sgnw (xr[i+1] < 0 ? 1 : 0) 1048 #define sgnw xr_sign[i+1]
1013 #define sgnx (xr[i+2] < 0 ? 1 : 0) 1049 #define sgnx xr_sign[i+2]
1014 #define sgny (xr[i+3] < 0 ? 1 : 0) 1050 #define sgny xr_sign[i+3]
1015 1051
1016 for(i=begin; i<end; i+=4) 1052 for(i=begin; i<end; i+=4)
1017 { 1053 {
@@ -1024,20 +1060,20 @@ int HuffmanCod1( short *ix, int *xr, uint32 begin, uint32 end, int tbl)
1024 switch(p) 1060 switch(p)
1025 { 1061 {
1026 case 0: l=0; s = 0; break; 1062 case 0: l=0; s = 0; break;
1027 case 1: l=1; s = sgnv; break; 1063 case 1: l=1; s = sgny; break;
1028 case 2: l=1; s = sgnw; break; 1064 case 2: l=1; s = sgnx; break;
1029 case 3: l=2; s = (sgnv << 1) + sgnw; break; 1065 case 3: l=2; s = (sgnx << 1) + sgny; break;
1030 case 4: l=1; s = sgnx; break; 1066 case 4: l=1; s = sgnw; break;
1031 case 5: l=2; s = (sgnv << 1) + sgnx; break; 1067 case 5: l=2; s = (sgnw << 1) + sgny; break;
1032 case 6: l=2; s = (sgnw << 1) + sgnx; break; 1068 case 6: l=2; s = (sgnw << 1) + sgnx; break;
1033 case 7: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break; 1069 case 7: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break;
1034 case 8: l=1; s = sgny; break; 1070 case 8: l=1; s = sgnv; break;
1035 case 9: l=2; s = (sgnv << 1) + sgny; break; 1071 case 9: l=2; s = (sgnv << 1) + sgny; break;
1036 case 10: l=2; s = (sgnw << 1) + sgny; break; 1072 case 10: l=2; s = (sgnv << 1) + sgnx; break;
1037 case 11: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break; 1073 case 11: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break;
1038 case 12: l=2; s = (sgnx << 1) + sgny; break; 1074 case 12: l=2; s = (sgnv << 1) + sgnw; break;
1039 case 13: l=3; s = (sgnv << 2) + (sgnx << 1) + sgny; break; 1075 case 13: l=3; s = (sgnv << 2) + (sgnw << 1) + sgny; break;
1040 case 14: l=3; s = (sgnw << 2) + (sgnx << 1) + sgny; break; 1076 case 14: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break;
1041 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break; 1077 case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break;
1042 } 1078 }
1043 1079
@@ -1054,22 +1090,22 @@ int HuffmanCod1( short *ix, int *xr, uint32 begin, uint32 end, int tbl)
1054} 1090}
1055 1091
1056/* Implements the pseudocode of page 98 of the IS */ 1092/* Implements the pseudocode of page 98 of the IS */
1057int HuffmanCode( short *ix, int *xr, uint32 begin, uint32 end, int table) 1093int HuffmanCode(short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table)
1058{ 1094{
1059 uint32 cc=0, sz=0, code; 1095 uint32_t cc=0, sz=0, code;
1060 uint32 i, xl=0, yl=0, idx; 1096 uint32_t i, xl=0, yl=0, idx;
1061 int x, y, bit, sumbit=0; 1097 int x, y, bit, sumbit=0;
1062 #define sign_x (xr[i+0] < 0 ? 1 : 0) 1098 #define sign_x xr_sign[i+0]
1063 #define sign_y (xr[i+1] < 0 ? 1 : 0) 1099 #define sign_y xr_sign[i+1]
1064 1100
1065 if(table == 0) 1101 if(table == 0)
1066 return 0; 1102 return 0;
1067 1103
1068 if( table > 15 ) 1104 if( table > 15 )
1069 { /* ESC-table is used */ 1105 { /* ESC-table is used */
1070 uint32 linbits = ht_big[table-16].linbits; 1106 uint32_t linbits = ht_big[table-16].linbits;
1071 uint16 *hffcode = table < 24 ? t16HB : t24HB; 1107 uint16_t *hffcode = table < 24 ? t16HB : t24HB;
1072 uint8 *hlen = table < 24 ? t16l : t24l; 1108 uint8_t *hlen = table < 24 ? t16l : t24l;
1073 1109
1074 for(i=begin; i<end; i+=2) 1110 for(i=begin; i<end; i+=2)
1075 { 1111 {
@@ -1099,6 +1135,13 @@ int HuffmanCode( short *ix, int *xr, uint32 begin, uint32 end, int table)
1099 { 1135 {
1100 if(y > 14) 1136 if(y > 14)
1101 { 1137 {
1138 if(bit + linbits + 1 > 32)
1139 {
1140 putlong( code, bit );
1141 sumbit += bit;
1142 code = bit = 0;
1143 }
1144
1102 code = (code << linbits) | yl; 1145 code = (code << linbits) | yl;
1103 bit += linbits; 1146 bit += linbits;
1104 } 1147 }
@@ -1147,14 +1190,14 @@ int HuffmanCode( short *ix, int *xr, uint32 begin, uint32 end, int table)
1147 return sumbit; 1190 return sumbit;
1148} 1191}
1149 1192
1150void putbits(uint32 val, uint32 nbit) 1193void putbits(uint32_t val, uint32_t nbit)
1151{ 1194{
1152 int new_bitpos = CodedData.bitpos + nbit; 1195 int new_bitpos = CodedData.bitpos + nbit;
1153 int ptrpos = CodedData.bitpos >> 5; 1196 int ptrpos = CodedData.bitpos >> 5;
1154 1197
1155 val = val & (0xffffffff >> (32 - nbit)); 1198 val = val & (0xffffffff >> (32 - nbit));
1156 1199
1157 /* data fit in one uint32 */ 1200 /* data fit in one uint32_t */
1158 if(((new_bitpos - 1) >> 5) == ptrpos) 1201 if(((new_bitpos - 1) >> 5) == ptrpos)
1159 CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31); 1202 CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31);
1160 else 1203 else
@@ -1173,9 +1216,9 @@ void putbits(uint32 val, uint32 nbit)
1173/* of the Huffman tables as defined in the IS (Table B.7), and will not */ 1216/* of the Huffman tables as defined in the IS (Table B.7), and will not */
1174/* work with any arbitrary tables. */ 1217/* work with any arbitrary tables. */
1175/***************************************************************************/ 1218/***************************************************************************/
1176int choose_table( short *ix, uint32 begin, uint32 end, int *bits ) 1219int choose_table( short *ix, uint32_t begin, uint32_t end, int *bits )
1177{ 1220{
1178 uint32 i; 1221 uint32_t i;
1179 int max, table0, table1; 1222 int max, table0, table1;
1180 1223
1181 for(i=begin,max=0; i<end; i++) 1224 for(i=begin,max=0; i<end; i++)
@@ -1217,10 +1260,10 @@ int choose_table( short *ix, uint32 begin, uint32 end, int *bits )
1217 } 1260 }
1218} 1261}
1219 1262
1220int find_best_2(short *ix, uint32 start, uint32 end, const uint32 *table, 1263int find_best_2(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1221 uint32 len, int *bits) 1264 uint32_t len, int *bits)
1222{ 1265{
1223 uint32 i, sum = 0; 1266 uint32_t i, sum = 0;
1224 1267
1225 for(i=start; i<end; i+=2) 1268 for(i=start; i<end; i+=2)
1226 sum += table[ix[i] * len + ix[i+1]]; 1269 sum += table[ix[i] * len + ix[i+1]];
@@ -1237,10 +1280,10 @@ int find_best_2(short *ix, uint32 start, uint32 end, const uint32 *table,
1237 } 1280 }
1238} 1281}
1239 1282
1240int find_best_3(short *ix, uint32 start, uint32 end, const uint32 *table, 1283int find_best_3(short *ix, uint32_t start, uint32_t end, const uint32_t *table,
1241 uint32 len, int *bits) 1284 uint32_t len, int *bits)
1242{ 1285{
1243 uint32 i, j, sum = 0; 1286 uint32_t i, j, sum = 0;
1244 int sum1 = 0; 1287 int sum1 = 0;
1245 int sum2 = 0; 1288 int sum2 = 0;
1246 int sum3 = 0; 1289 int sum3 = 0;
@@ -1270,9 +1313,9 @@ int find_best_3(short *ix, uint32 start, uint32 end, const uint32 *table,
1270/*************************************************************************/ 1313/*************************************************************************/
1271/* Function: Count the number of bits necessary to code the subregion. */ 1314/* Function: Count the number of bits necessary to code the subregion. */
1272/*************************************************************************/ 1315/*************************************************************************/
1273int count_bit1(short *ix, uint32 start, uint32 end, int *bits ) 1316int count_bit1(short *ix, uint32_t start, uint32_t end, int *bits )
1274{ 1317{
1275 uint32 i, sum = 0; 1318 uint32_t i, sum = 0;
1276 1319
1277 for(i=start; i<end; i+=2) 1320 for(i=start; i<end; i+=2)
1278 sum += t1l[4 + ix[i] * 2 + ix[i+1]]; 1321 sum += t1l[4 + ix[i] * 2 + ix[i+1]];
@@ -1282,10 +1325,10 @@ int count_bit1(short *ix, uint32 start, uint32 end, int *bits )
1282 return 1; /* this is table1 */ 1325 return 1; /* this is table1 */
1283} 1326}
1284 1327
1285int count_bigv(short *ix, uint32 start, uint32 end, int table0, 1328int count_bigv(short *ix, uint32_t start, uint32_t end, int table0,
1286 int table1, int *bits ) 1329 int table1, int *bits )
1287{ 1330{
1288 uint32 i, sum0, sum1, sum=0, bigv=0, x, y; 1331 uint32_t i, sum0, sum1, sum=0, bigv=0, x, y;
1289 1332
1290 /* ESC-table is used */ 1333 /* ESC-table is used */
1291 for(i=start; i<end; i+=2) 1334 for(i=start; i<end; i+=2)
@@ -1323,7 +1366,7 @@ int calc_runlen( short *ix, side_info_t *si )
1323 int p, i, sum = 0; 1366 int p, i, sum = 0;
1324 1367
1325 for(i=SAMPL2; i-=2; ) 1368 for(i=SAMPL2; i-=2; )
1326 if(*(uint32*)&ix[i-2]) /* !!!! short *ix; !!!!! */ 1369 if(*(uint32_t*)&ix[i-2]) /* !!!! short *ix; !!!!! */
1327 break; 1370 break;
1328 1371
1329 si->count1 = 0; 1372 si->count1 = 0;
@@ -1366,16 +1409,29 @@ int calc_runlen( short *ix, side_info_t *si )
1366/*************************************************************************/ 1409/*************************************************************************/
1367int quantize_int(int *xr, short *ix, side_info_t *si) 1410int quantize_int(int *xr, short *ix, side_info_t *si)
1368{ 1411{
1369 int i, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 }; 1412 unsigned int i, idx, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 };
1370 1413
1371 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4; 1414 s = frac_pow[si->quantStep & 3] >> si->quantStep / 4;
1372 1415
1373 /* check for integer overflow */ 1416 /* check for possible 'out of range' values */
1374 if(((si->max_val + 256) >> 8) * s >= (1622 << 8)) 1417 if(((si->max_val + 256) >> 8) * s >= (65536 << 8))
1375 return 0; 1418 return 0;
1376 1419
1377 for(i=SAMPL2; i--; ) 1420 if(((si->max_val + 256) >> 8) * s < (4096 << 8))
1378 ix[i] = int2idx[(abs(xr[i]) * s + 0x8000) >> 16]; 1421 { /* all values fit the table size */
1422 for(i=SAMPL2; i--; )
1423 ix[i] = int2idx[(xr[i] * s + 0x8000) >> 16];
1424 }
1425 else
1426 { /* check each index wether it fits the table */
1427 for(i=SAMPL2; i--; )
1428 {
1429 idx = (xr[i] * s + 0x08000) >> 16;
1430
1431 if(idx > 4095) ix[i] = int2idx[(idx + 8) >> 4] << 3;
1432 else ix[i] = int2idx[idx];
1433 }
1434 }
1379 1435
1380 return 1; 1436 return 1;
1381} 1437}
@@ -1446,9 +1502,9 @@ int quantize_and_count_bits(int *xr, short *ix, side_info_t *si)
1446 return bits; 1502 return bits;
1447} 1503}
1448 1504
1449/***********************************************************************/ 1505/************************************************************************/
1450/* The code selects the best quantStep for a particular set of scalefacs */ 1506/* The code selects the best quantStep for a particular set of scalefacs*/
1451/***********************************************************************/ 1507/************************************************************************/
1452int inner_loop(int *xr, int max_bits, side_info_t *si) 1508int inner_loop(int *xr, int max_bits, side_info_t *si)
1453{ 1509{
1454 int bits; 1510 int bits;
@@ -1970,8 +2026,7 @@ static int find_bitrate_index(int type, int bitrate)
1970} 2026}
1971 2027
1972static int find_samplerate_index(long freq, int *mp3_type) 2028static int find_samplerate_index(long freq, int *mp3_type)
1973{ /* MPEG 2 */ /* MPEG 1 */ 2029{
1974 static long mpeg[2][3] = { {22050, 24000, 16000}, {44100, 48000, 32000} };
1975 int mpg, rate; 2030 int mpg, rate;
1976 2031
1977 /* set default values: MPEG1 at 44100/s */ 2032 /* set default values: MPEG1 at 44100/s */
@@ -1979,7 +2034,7 @@ static int find_samplerate_index(long freq, int *mp3_type)
1979 2034
1980 for(mpg=0; mpg<2; mpg++) 2035 for(mpg=0; mpg<2; mpg++)
1981 for(rate=0; rate<3; rate++) 2036 for(rate=0; rate<3; rate++)
1982 if(freq == mpeg[mpg][rate]) 2037 if(freq == sampr_index[mpg][rate])
1983 { *mp3_type = mpg; return rate; } 2038 { *mp3_type = mpg; return rate; }
1984 2039
1985 return 0; 2040 return 0;
@@ -1987,7 +2042,7 @@ static int find_samplerate_index(long freq, int *mp3_type)
1987 2042
1988void init_mp3_encoder_engine(bool stereo, int bitrate, int sample_rate) 2043void init_mp3_encoder_engine(bool stereo, int bitrate, int sample_rate)
1989{ 2044{
1990 uint32 avg_byte_per_frame; 2045 uint32_t avg_byte_per_frame;
1991 2046
1992#ifdef ROCKBOX_LITTLE_ENDIAN 2047#ifdef ROCKBOX_LITTLE_ENDIAN
1993 cfg.byte_order = order_littleEndian; 2048 cfg.byte_order = order_littleEndian;
@@ -2094,11 +2149,44 @@ void init_mp3_encoder_engine(bool stereo, int bitrate, int sample_rate)
2094 : (cfg.channels == 1 ? 72 : 136)); 2149 : (cfg.channels == 1 ? 72 : 136));
2095} 2150}
2096 2151
2152void set_scale_facs(int *mdct_freq)
2153{
2154 unsigned int i, is, ie, k, s;
2155 int max_freq_val, avrg_freq_val;
2156
2157 /* calc average of first 256 frequency values */
2158 for(avrg_freq_val=i=0; i<256; i++)
2159 avrg_freq_val += mdct_freq[i];
2160 avrg_freq_val >>= 8;
2161
2162 /* if max of current band is smaller than average, increase precision */
2163 /* last band keeps untouched (not scaled) */
2164 for(is=k=0; is<scalefac[21]; k++)
2165 {
2166 max_freq_val = 0;
2167
2168 for(i=is, ie=scalefac[k+1]; i<ie; i++)
2169 if(max_freq_val < mdct_freq[i])
2170 max_freq_val = mdct_freq[i];
2171
2172 for(s=0; s<3; s++)
2173 if((max_freq_val<<s) > avrg_freq_val)
2174 break;
2175
2176 band_scale_f[k] = (unsigned char)s;
2177
2178 for(i=is; s && i<ie; i++)
2179 mdct_freq[i] <<= s;
2180
2181 is = ie;
2182 }
2183}
2184
2097void compress(void) 2185void compress(void)
2098{ 2186{
2099 int i, ii, gr, k, ch, shift, gr_cnt; 2187 int i, gr, gr_cnt;
2100 int max, min;
2101 char stg[20]; 2188 char stg[20];
2189 uint32_t max;
2102 2190
2103 while(1) 2191 while(1)
2104 { 2192 {
@@ -2120,22 +2208,23 @@ void compress(void)
2120 cfg.mpg.padding = 0; 2208 cfg.mpg.padding = 0;
2121 2209
2122 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding 2210 cfg.mean_bits = (8 * cfg.byte_per_frame + 8 * cfg.mpg.padding
2123 - cfg.sideinfo_len) / cfg.granules / cfg.channels; 2211 - cfg.sideinfo_len) / cfg.granules / cfg.channels
2212 - 42; // reserved for scale_facs
2124 2213
2125 /* shift out old samples */ 2214 /* shift out old samples */
2126 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512); 2215 memcpy(mfbuf, mfbuf + 2*cfg.granules*576, 4*512);
2127 2216
2128 /* read new samples to iram for further processing */ 2217 /* read new samples to iram for further processing */
2129 if(read_samples((uint32*)(mfbuf + 2*512), SAMP_PER_FRAME) == 0) 2218 if(read_samples((uint32_t*)(mfbuf + 2*512), SAMP_PER_FRAME) == 0)
2130 break; 2219 break;
2131 2220
2132 /* swap bytes if neccessary */ 2221 /* swap bytes if neccessary */
2133 if(cfg.byte_order == order_bigEndian) 2222 if(cfg.byte_order == order_bigEndian)
2134 for(i=0; i<SAMP_PER_FRAME; i++) 2223 for(i=0; i<SAMP_PER_FRAME; i++)
2135 { 2224 {
2136 uint32 t = ((uint32*)mfbuf)[512 + i]; 2225 uint32_t t = ((uint32_t*)mfbuf)[512 + i];
2137 t = ((t >> 8) & 0xff00ff) | ((t << 8) & 0xff00ff00); 2226 t = ((t >> 8) & 0xff00ff) | ((t << 8) & 0xff00ff00);
2138 ((uint32*)mfbuf)[512 + i] = t; 2227 ((uint32_t*)mfbuf)[512 + i] = t;
2139 } 2228 }
2140 2229
2141 if(cfg.resample) /* downsample to half of original */ 2230 if(cfg.resample) /* downsample to half of original */
@@ -2152,17 +2241,18 @@ void compress(void)
2152 cfg.ResvSize = 0; 2241 cfg.ResvSize = 0;
2153 gr_cnt = cfg.granules * cfg.channels; 2242 gr_cnt = cfg.granules * cfg.channels;
2154 CodedData.bitpos = cfg.sideinfo_len; /* leave space for mp3 header */ 2243 CodedData.bitpos = cfg.sideinfo_len; /* leave space for mp3 header */
2244
2155 for(gr=0; gr<cfg.granules; gr++) 2245 for(gr=0; gr<cfg.granules; gr++)
2156 { 2246 {
2157 short *wk = mfbuf + 2*286 + gr*1152; 2247 short *wk = mfbuf + 2*286 + gr*1152;
2248 int ch;
2158 2249
2159 /* 16bit packed wav data can be windowed efficiently on coldfire */ 2250 /* 16bit packed wav data can be windowed efficiently on coldfire */
2160 window_subband1(wk, sb_data[0][1-gr][0], sb_data[1][1-gr][0]); 2251 window_subband1(wk, sb_data[0][1-gr][0], sb_data[1][1-gr][0]);
2161 2252
2162 for(ch=0; ch<cfg.channels; ch++) 2253 for(ch=0; ch<cfg.channels; ch++)
2163 { 2254 {
2164 int band; 2255 int ii, k, shift;
2165 int *mdct;
2166 2256
2167 wk = mfbuf + 2*286 + gr*1152 + ch; 2257 wk = mfbuf + 2*286 + gr*1152 + ch;
2168 2258
@@ -2172,16 +2262,21 @@ void compress(void)
2172 window_subband2(wk, sb_data[ch][1-gr][k]); 2262 window_subband2(wk, sb_data[ch][1-gr][k]);
2173 /* Compensate for inversion in the analysis filter */ 2263 /* Compensate for inversion in the analysis filter */
2174 if(k & 1) 2264 if(k & 1)
2265 {
2266 int band;
2175 for(band=1; band<32; band+=2) 2267 for(band=1; band<32; band+=2)
2176 sb_data[ch][1-gr][k][band] *= -1; 2268 sb_data[ch][1-gr][k][band] *= -1;
2269 }
2177 } 2270 }
2178 2271
2179 /* Perform imdct of 18 previous + 18 current subband samples */ 2272 /* Perform imdct of 18 previous + 18 current subband samples */
2180 /* for integer precision do this loop twice (if neccessary) */ 2273 /* for integer precision do this loop again (if neccessary) */
2181 shift = k = 14; 2274 shift = 14 - (cfg.cod_info[gr][ch].additStep >> 2);
2182 for(ii=0; ii<2 && k; ii++) 2275 for(k=1,ii=0; ii<3 && k; ii++)
2183 { 2276 {
2184 mdct = mdct_freq; 2277 int *mdct = mdct_freq;
2278 int band;
2279
2185 cfg.cod_info[gr][ch].additStep = 4 * (14 - shift); 2280 cfg.cod_info[gr][ch].additStep = 4 * (14 - shift);
2186 for(band=0; band<cfg.mpg.num_bands; band++, mdct+=18) 2281 for(band=0; band<cfg.mpg.num_bands; band++, mdct+=18)
2187 { 2282 {
@@ -2197,11 +2292,15 @@ void compress(void)
2197 int c = shft_n(band0[(k+9)*32], shift); 2292 int c = shft_n(band0[(k+9)*32], shift);
2198 int d = shft_n(band0[(8-k)*32], shift); 2293 int d = shft_n(band0[(8-k)*32], shift);
2199 2294
2200 work[k+ 9] = shft16(a * win[k+ 9][0] + b * win[k+ 9][1] 2295 work[k+ 9] = shft16(a * win[k+ 9][0] +
2201 + c * win[k+ 9][2] + d * win[k+ 9][3]); 2296 b * win[k+ 9][1] +
2297 c * win[k+ 9][2] +
2298 d * win[k+ 9][3]);
2202 2299
2203 work[k+18] = shft16(c * win[k+18][0] + d * win[k+18][1] 2300 work[k+18] = shft16(c * win[k+18][0] +
2204 + a * win[k+18][2] + b * win[k+18][3]); 2301 d * win[k+18][1] +
2302 a * win[k+18][2] +
2303 b * win[k+18][3]);
2205 } 2304 }
2206 2305
2207 /* 7200=4*18*100 */ 2306 /* 7200=4*18*100 */
@@ -2209,43 +2308,66 @@ void compress(void)
2209 2308
2210 /* Perform aliasing reduction butterfly */ 2309 /* Perform aliasing reduction butterfly */
2211 if(band != 0) 2310 if(band != 0)
2311 {
2212 for(k=7; k>=0; --k) 2312 for(k=7; k>=0; --k)
2213 { 2313 {
2214 int bu, bd; 2314 int bu, bd;
2215 bu = shft15(mdct[k]) * ca[k] + shft15(mdct[-1-k]) * cs[k]; 2315 bu = shft15(mdct[k]) * ca[k] +
2216 bd = shft15(mdct[k]) * cs[k] - shft15(mdct[-1-k]) * ca[k]; 2316 shft15(mdct[-1-k]) * cs[k];
2317 bd = shft15(mdct[k]) * cs[k] -
2318 shft15(mdct[-1-k]) * ca[k];
2217 mdct[-1-k] = bu; 2319 mdct[-1-k] = bu;
2218 mdct[ k ] = bd; 2320 mdct[ k ] = bd;
2219 } 2321 }
2322 }
2220 } 2323 }
2221 2324
2222 max = min = 0; 2325 max = 0;
2223 for(k=0; k<576; k++) 2326 for(k=0; k<576; k++)
2224 { 2327 {
2225 mdct_freq[k] = shft13(mdct_freq[k]); 2328 if(mdct_freq[k] < 0)
2226 if(max < mdct_freq[k]) max = mdct_freq[k]; 2329 {
2227 if(min > mdct_freq[k]) min = mdct_freq[k]; 2330 mdct_sign[k] = 1; /* negative */
2331 mdct_freq[k] = shft13(-mdct_freq[k]);
2332 }
2333 else
2334 {
2335 mdct_sign[k] = 0; /* positive */
2336 mdct_freq[k] = shft13(mdct_freq[k]);
2337 }
2338
2339 if(max < (uint32_t)mdct_freq[k])
2340 max = (uint32_t)mdct_freq[k];
2228 } 2341 }
2229 2342
2230 max = (max > -min) ? max : -min; 2343 cfg.cod_info[gr][ch].max_val = max;
2231 cfg.cod_info[gr][ch].max_val = (long)max;
2232 2344
2233 /* calc new shift for higher integer precision */ 2345 /* calc new shift for higher integer precision */
2234 for(k=0; max<(0x3c00>>k); k++); 2346 for(k=0; max<(uint32_t)(0x7800>>k); k++) shift--;
2235 shift = 12 - k; 2347 for( ; (max>>k)>=(uint32_t)0x10000; k++) shift++;
2348 if(shift < 0) shift = 0;
2236 } 2349 }
2237 2350
2238 cfg.cod_info[gr][ch].quantStep += cfg.cod_info[gr][ch].additStep; 2351 cfg.cod_info[gr][ch].quantStep +=
2352 cfg.cod_info[gr][ch].additStep;
2353
2354 set_scale_facs(mdct_freq);
2239 2355
2240 /* bit and noise allocation */ 2356 /* bit and noise allocation */
2241 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch], gr_cnt--); 2357 iteration_loop(mdct_freq, &cfg.cod_info[gr][ch],
2358 gr_cnt--);
2242 /* write the frame to the bitstream */ 2359 /* write the frame to the bitstream */
2243 Huffmancodebits(enc_data, mdct_freq, &cfg.cod_info[gr][ch]); 2360 Huffmancodebits(enc_data, mdct_sign,
2361 &cfg.cod_info[gr][ch]);
2244 2362
2245 cfg.cod_info[gr][ch].quantStep -= cfg.cod_info[gr][ch].additStep; 2363 cfg.cod_info[gr][ch].quantStep -=
2364 cfg.cod_info[gr][ch].additStep;
2246 2365
2247 if(cfg.granules == 1) 2366 if(cfg.granules == 1)
2248 memcpy(sb_data[ch][0], sb_data[ch][1], sizeof(sb_data[ch][0])); 2367 {
2368 memcpy(sb_data[ch][0], sb_data[ch][1],
2369 sizeof(sb_data[ch][0]));
2370 }
2249 } 2371 }
2250 } 2372 }
2251 2373