diff options
Diffstat (limited to 'apps/codecs/mp3_enc.c')
-rw-r--r-- | apps/codecs/mp3_enc.c | 239 |
1 files changed, 159 insertions, 80 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 | ||
81 | typedef struct { | 81 | typedef struct { |
@@ -93,14 +93,14 @@ typedef struct { | |||
93 | } config_t; | 93 | } config_t; |
94 | 94 | ||
95 | typedef struct { | 95 | typedef 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 | ||
100 | struct huffcodetab { | 100 | struct 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 | ||
106 | struct huffcodebig { | 106 | struct huffcodebig { |
@@ -119,8 +119,9 @@ struct huffcodebig { | |||
119 | 119 | ||
120 | static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */ | 120 | static short mfbuf [2*(1152+512)] IBSS_ATTR; /* 3328 Bytes */ |
121 | static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */ | 121 | static int sb_data [2][2][18][SBLIMIT] IBSS_ATTR; /* 13824 Bytes */ |
122 | static int mdct_freq [SAMPL2] IBSS_ATTR; /* 9216 Bytes */ | 122 | static int mdct_freq [SAMPL2] IBSS_ATTR; /* 2304 Bytes */ |
123 | static short enc_data [SAMPL2] IBSS_ATTR; /* 4608 Bytes */ | 123 | static char mdct_sign [SAMPL2] IBSS_ATTR; /* 576 Bytes */ |
124 | static short enc_data [SAMPL2] IBSS_ATTR; /* 1152 Bytes */ | ||
124 | static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */ | 125 | static uint32_t scalefac [23] IBSS_ATTR; /* 92 Bytes */ |
125 | static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */ | 126 | static BF_Data CodedData IBSS_ATTR; /* 1056 Bytes */ |
126 | static int ca [8] IBSS_ATTR; /* 32 Bytes */ | 127 | static int ca [8] IBSS_ATTR; /* 32 Bytes */ |
@@ -175,6 +176,7 @@ static unsigned samp_per_frame IBSS_ATTR; | |||
175 | static config_t cfg IBSS_ATTR; | 176 | static config_t cfg IBSS_ATTR; |
176 | static char *res_buffer; | 177 | static char *res_buffer; |
177 | static int32_t err IBSS_ATTR; | 178 | static int32_t err IBSS_ATTR; |
179 | static uint8_t band_scale_f[22]; | ||
178 | 180 | ||
179 | static const uint8_t ht_count_const[2][2][16] = | 181 | static 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 */ |
837 | static int HuffmanCode( short *ix, int *xr, uint32_t begin, uint32_t end, int table); | 839 | static int HuffmanCode( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table); |
838 | static int HuffmanCod1( short *ix, int *xr, uint32_t begin, uint32_t end, int table); | 840 | static int HuffmanCod1( short *ix, char *xr_sign, uint32_t begin, uint32_t end, int table); |
839 | static void putbits(uint32_t val, uint32_t nbit); | 841 | static void putbits(uint32_t val, uint32_t nbit); |
840 | static int find_best_2( short *ix, uint32_t start, uint32_t end, const uint32_t *table, | 842 | static 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. */ |
930 | static void Huffmancodebits( short *ix, int *xr, side_info_t *gi ) | 936 | static 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 | ||
964 | int HuffmanCod1( short *ix, int *xr, uint32_t begin, uint32_t end, int tbl) | 979 | int 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 */ |
1015 | int HuffmanCode( short *ix, int *xr, uint32_t begin, uint32_t end, int table) | 1030 | int 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 | /*************************************************************************/ |
1325 | int quantize_int(int *xr, short *ix, side_info_t *si) | 1347 | int 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 | /************************************************************************/ |
1410 | int inner_loop(int *xr, int max_bits, side_info_t *si) | 1445 | int 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 | ||
2138 | void 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 | |||
2103 | STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk) | 2171 | STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk) |
2104 | ICODE_ATTR; | 2172 | ICODE_ATTR; |
2105 | STATICIRAM void encode_frame(char *buffer, struct enc_chunk_hdr *chunk) | 2173 | STATICIRAM 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 -= |