2 * Siren Encoder/Decoder library
4 * @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "huffman_consts.h"
27 static short current_word = 0;
28 static int bit_idx = 0;
29 static int *bitstream_ptr = NULL;
32 if (bitstream_ptr == NULL)
36 current_word = *bitstream_ptr++;
40 return (current_word >> --bit_idx) & 1;
43 void set_bitstream(int *stream) {
44 bitstream_ptr = stream;
45 current_word = *bitstream_ptr;
50 int compute_region_powers(int number_of_regions, float *coefs, int *drp_num_bits, int *drp_code_bits, int *absolute_region_power_index, int esf_adjustment) {
51 float region_power = 0;
57 for (region = 0; region < number_of_regions; region++) {
59 for (i = 0 ; i < region_size; i++) {
60 region_power += coefs[(region*region_size)+i] * coefs[(region*region_size)+i];
62 region_power *= region_size_inverse;
66 for (i = 0; i < 6; i++) {
67 idx = (min_idx + max_idx) / 2;
68 if (region_power_table_boundary[idx-1] <= region_power) {
74 absolute_region_power_index[region] = min_idx - 24;
78 for (region = number_of_regions-2; region >= 0; region--) {
79 if (absolute_region_power_index[region] < absolute_region_power_index[region+1] - 11)
80 absolute_region_power_index[region] = absolute_region_power_index[region+1] - 11;
83 if (absolute_region_power_index[0] < (1-esf_adjustment))
84 absolute_region_power_index[0] = (1-esf_adjustment);
86 if (absolute_region_power_index[0] > (31-esf_adjustment))
87 absolute_region_power_index[0] = (31-esf_adjustment);
90 drp_code_bits[0] = absolute_region_power_index[0] + esf_adjustment;
93 for(region = 1; region < number_of_regions; region++) {
94 if (absolute_region_power_index[region] < (-8 - esf_adjustment))
95 absolute_region_power_index[region] = (-8 - esf_adjustment);
96 if (absolute_region_power_index[region] > (31-esf_adjustment))
97 absolute_region_power_index[region] = (31-esf_adjustment);
102 for(region = 0; region < number_of_regions-1; region++) {
103 idx = absolute_region_power_index[region+1] - absolute_region_power_index[region] + 12;
107 absolute_region_power_index[region+1] = absolute_region_power_index[region] + idx - 12;
108 drp_num_bits[region+1] = differential_region_power_bits[region][idx];
109 drp_code_bits[region+1] = differential_region_power_codes[region][idx];
110 num_bits += drp_num_bits[region+1];
117 int decode_envelope(int number_of_regions, float *decoder_standard_deviation, int *absolute_region_power_index, int esf_adjustment) {
120 int envelope_bits = 0;
123 for (i = 0; i < 5; i++)
124 index = (index<<1) | next_bit();
127 absolute_region_power_index[0] = index - esf_adjustment;
128 decoder_standard_deviation[0] = standard_deviation[absolute_region_power_index[0] + 24];
130 for (i = 1; i < number_of_regions; i++) {
133 index = differential_decoder_tree[i-1][index][next_bit()];
137 absolute_region_power_index[i] = absolute_region_power_index[i-1] - index - 12;
138 decoder_standard_deviation[i] = standard_deviation[absolute_region_power_index[i] + 24];
141 return envelope_bits;
146 static int huffman_vector(int category, int power_idx, float *mlts, int *out) {
148 float temp_value = deviation_inverse[power_idx] * step_size_inverse[category];
149 int sign_idx, idx, non_zeroes, max, bits_available;
150 int current_word = 0;
154 for (i = 0; i < number_of_vectors[category]; i++) {
155 sign_idx = idx = non_zeroes = 0;
156 for (j = 0; j < vector_dimension[category]; j++) {
157 max = (int) ((fabs(*mlts) * temp_value) + dead_zone[category]);
163 if (max > max_bin[category] || max < 0)
164 max = max_bin[category];
168 idx = (idx * (max_bin[category] + 1)) + max;
171 region_bits += bitcount_tables[category][idx] + non_zeroes;
172 bits_available -= bitcount_tables[category][idx] + non_zeroes;
173 if (bits_available < 0) {
174 *out++ = current_word + (((code_tables[category][idx] << non_zeroes) + sign_idx) >> -bits_available);
175 bits_available += 32;
176 current_word = ((code_tables[category][idx] << non_zeroes) + sign_idx) << bits_available;
178 current_word += ((code_tables[category][idx] << non_zeroes) + sign_idx) << bits_available;
187 int quantize_mlt(int number_of_regions, int rate_control_possibilities, int number_of_available_bits, float *coefs, int *absolute_region_power_index, int *power_categories, int *category_balance, int *region_mlt_bit_counts, int *region_mlt_bits) {
192 for (rate_control = 0; rate_control < ((rate_control_possibilities >> 1) - 1); rate_control++)
193 power_categories[category_balance[rate_control]]++;
195 for (region = 0; region < number_of_regions; region++) {
196 if (power_categories[region] > 6)
197 region_mlt_bit_counts[region] = 0;
199 region_mlt_bit_counts[region] = huffman_vector(power_categories[region], absolute_region_power_index[region], coefs + (region_size * region),
200 region_mlt_bits + (4*region));
201 mlt_bits += region_mlt_bit_counts[region];
204 while (mlt_bits < number_of_available_bits && rate_control > 0) {
206 region = category_balance[rate_control];
207 power_categories[region]--;
209 if (power_categories[region] < 0)
210 power_categories[region] = 0;
212 mlt_bits -= region_mlt_bit_counts[region];
214 if (power_categories[region] > 6)
215 region_mlt_bit_counts[region] = 0;
217 region_mlt_bit_counts[region] = huffman_vector(power_categories[region], absolute_region_power_index[region], coefs + (region_size * region),
218 region_mlt_bits + (4*region));
220 mlt_bits += region_mlt_bit_counts[region];
223 while(mlt_bits > number_of_available_bits && rate_control < rate_control_possibilities) {
224 region = category_balance[rate_control];
225 power_categories[region]++;
226 mlt_bits -= region_mlt_bit_counts[region];
228 if (power_categories[region] > 6)
229 region_mlt_bit_counts[region] = 0;
231 region_mlt_bit_counts[region] = huffman_vector(power_categories[region], absolute_region_power_index[region], coefs + (region_size * region),
232 region_mlt_bits + (4*region));
234 mlt_bits += region_mlt_bit_counts[region];
242 static int get_dw(SirenDecoder decoder) {
243 int ret = decoder->dw1 + decoder->dw4;
245 if ((ret & 0x8000) != 0)
248 decoder->dw1 = decoder->dw2;
249 decoder->dw2 = decoder->dw3;
250 decoder->dw3 = decoder->dw4;
259 int decode_vector(SirenDecoder decoder, int number_of_regions, int number_of_available_bits, float *decoder_standard_deviation, int *power_categories, float *coefs, int scale_factor) {
274 for (region = 0; region < number_of_regions; region++) {
275 category = power_categories[region];
276 coefs_ptr = coefs + (region * region_size);
279 decoder_tree = decoder_tables[category];
281 for (i = 0; i < number_of_vectors[category]; i++) {
284 if (number_of_available_bits <= 0) {
289 index = decoder_tree[index + next_bit()];
290 number_of_available_bits--;
291 } while ((index & 1) == 0);
295 if (error == 0 && number_of_available_bits >= 0) {
296 for (j = 0; j < vector_dimension[category]; j++) {
297 decoded_value = mlt_quant[category][index & ((1 << index_table[category]) - 1)];
298 index >>= index_table[category];
300 if (decoded_value != 0) {
302 decoded_value *= -decoder_standard_deviation[region];
304 decoded_value *= decoder_standard_deviation[region];
305 number_of_available_bits--;
308 *coefs_ptr++ = decoded_value * scale_factor;
317 for (j = region + 1; j < number_of_regions; j++)
318 power_categories[j] = 7;
324 coefs_ptr = coefs + (region * region_size);
328 for (j = 0; j < region_size; j++) {
329 if (*coefs_ptr != 0) {
331 if (fabs(*coefs_ptr) > 2.0 * decoder_standard_deviation[region]) {
338 noise = decoder_standard_deviation[region] * noise_category5[i];
339 } else if (category == 6) {
341 for (j = 0; j < region_size; j++) {
342 if (*coefs_ptr++ != 0)
346 noise = decoder_standard_deviation[region] * noise_category6[i];
347 } else if (category == 7) {
348 noise = decoder_standard_deviation[region] * noise_category7;
353 coefs_ptr = coefs + (region * region_size);
355 if (category == 5 || category == 6 || category == 7) {
356 dw1 = get_dw(decoder);
357 dw2 = get_dw(decoder);
359 for (j=0; j<10; j++) {
360 if (category == 7 || *coefs_ptr == 0) {
369 if (category == 7 || *coefs_ptr == 0) {
381 return error == 1 ? -1 : number_of_available_bits;