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;
34 if (bitstream_ptr == NULL)
38 current_word = *bitstream_ptr++;
42 return (current_word >> --bit_idx) & 1;
46 set_bitstream (int *stream)
48 bitstream_ptr = stream;
49 current_word = *bitstream_ptr;
55 compute_region_powers (int number_of_regions, float *coefs, int *drp_num_bits,
56 int *drp_code_bits, int *absolute_region_power_index, int esf_adjustment)
58 float region_power = 0;
64 for (region = 0; region < number_of_regions; region++) {
66 for (i = 0; i < region_size; i++) {
68 coefs[(region * region_size) + i] * coefs[(region * region_size) + i];
70 region_power *= region_size_inverse;
74 for (i = 0; i < 6; i++) {
75 idx = (min_idx + max_idx) / 2;
76 if (region_power_table_boundary[idx - 1] <= region_power) {
82 absolute_region_power_index[region] = min_idx - 24;
86 for (region = number_of_regions - 2; region >= 0; region--) {
87 if (absolute_region_power_index[region] <
88 absolute_region_power_index[region + 1] - 11)
89 absolute_region_power_index[region] =
90 absolute_region_power_index[region + 1] - 11;
93 if (absolute_region_power_index[0] < (1 - esf_adjustment))
94 absolute_region_power_index[0] = (1 - esf_adjustment);
96 if (absolute_region_power_index[0] > (31 - esf_adjustment))
97 absolute_region_power_index[0] = (31 - esf_adjustment);
100 drp_code_bits[0] = absolute_region_power_index[0] + esf_adjustment;
103 for (region = 1; region < number_of_regions; region++) {
104 if (absolute_region_power_index[region] < (-8 - esf_adjustment))
105 absolute_region_power_index[region] = (-8 - esf_adjustment);
106 if (absolute_region_power_index[region] > (31 - esf_adjustment))
107 absolute_region_power_index[region] = (31 - esf_adjustment);
112 for (region = 0; region < number_of_regions - 1; region++) {
114 absolute_region_power_index[region + 1] -
115 absolute_region_power_index[region] + 12;
119 absolute_region_power_index[region + 1] =
120 absolute_region_power_index[region] + idx - 12;
121 drp_num_bits[region + 1] = differential_region_power_bits[region][idx];
122 drp_code_bits[region + 1] = differential_region_power_codes[region][idx];
123 num_bits += drp_num_bits[region + 1];
131 decode_envelope (int number_of_regions, float *decoder_standard_deviation,
132 int *absolute_region_power_index, int esf_adjustment)
136 int envelope_bits = 0;
139 for (i = 0; i < 5; i++)
140 index = (index << 1) | next_bit ();
143 absolute_region_power_index[0] = index - esf_adjustment;
144 decoder_standard_deviation[0] =
145 standard_deviation[absolute_region_power_index[0] + 24];
147 for (i = 1; i < number_of_regions; i++) {
150 index = differential_decoder_tree[i - 1][index][next_bit ()];
154 absolute_region_power_index[i] =
155 absolute_region_power_index[i - 1] - index - 12;
156 decoder_standard_deviation[i] =
157 standard_deviation[absolute_region_power_index[i] + 24];
160 return envelope_bits;
166 huffman_vector (int category, int power_idx, float *mlts, int *out)
169 float temp_value = deviation_inverse[power_idx] * step_size_inverse[category];
170 int sign_idx, idx, non_zeroes, max, bits_available;
171 int current_word = 0;
175 for (i = 0; i < number_of_vectors[category]; i++) {
176 sign_idx = idx = non_zeroes = 0;
177 for (j = 0; j < vector_dimension[category]; j++) {
178 max = (int) ((fabs (*mlts) * temp_value) + dead_zone[category]);
184 if (max > max_bin[category] || max < 0)
185 max = max_bin[category];
189 idx = (idx * (max_bin[category] + 1)) + max;
192 region_bits += bitcount_tables[category][idx] + non_zeroes;
193 bits_available -= bitcount_tables[category][idx] + non_zeroes;
194 if (bits_available < 0) {
196 current_word + (((code_tables[category][idx] << non_zeroes) +
197 sign_idx) >> -bits_available);
198 bits_available += 32;
200 ((code_tables[category][idx] << non_zeroes) +
201 sign_idx) << bits_available;
204 ((code_tables[category][idx] << non_zeroes) +
205 sign_idx) << bits_available;
215 quantize_mlt (int number_of_regions, int rate_control_possibilities,
216 int number_of_available_bits, float *coefs,
217 int *absolute_region_power_index, int *power_categories,
218 int *category_balance, int *region_mlt_bit_counts, int *region_mlt_bits)
224 for (rate_control = 0; rate_control < ((rate_control_possibilities >> 1) - 1);
226 power_categories[category_balance[rate_control]]++;
228 for (region = 0; region < number_of_regions; region++) {
229 if (power_categories[region] > 6)
230 region_mlt_bit_counts[region] = 0;
232 region_mlt_bit_counts[region] =
233 huffman_vector (power_categories[region],
234 absolute_region_power_index[region], coefs + (region_size * region),
235 region_mlt_bits + (4 * region));
236 mlt_bits += region_mlt_bit_counts[region];
239 while (mlt_bits < number_of_available_bits && rate_control > 0) {
241 region = category_balance[rate_control];
242 power_categories[region]--;
244 if (power_categories[region] < 0)
245 power_categories[region] = 0;
247 mlt_bits -= region_mlt_bit_counts[region];
249 if (power_categories[region] > 6)
250 region_mlt_bit_counts[region] = 0;
252 region_mlt_bit_counts[region] =
253 huffman_vector (power_categories[region],
254 absolute_region_power_index[region], coefs + (region_size * region),
255 region_mlt_bits + (4 * region));
257 mlt_bits += region_mlt_bit_counts[region];
260 while (mlt_bits > number_of_available_bits
261 && rate_control < rate_control_possibilities) {
262 region = category_balance[rate_control];
263 power_categories[region]++;
264 mlt_bits -= region_mlt_bit_counts[region];
266 if (power_categories[region] > 6)
267 region_mlt_bit_counts[region] = 0;
269 region_mlt_bit_counts[region] =
270 huffman_vector (power_categories[region],
271 absolute_region_power_index[region], coefs + (region_size * region),
272 region_mlt_bits + (4 * region));
274 mlt_bits += region_mlt_bit_counts[region];
283 get_dw (SirenDecoder decoder)
285 int ret = decoder->dw1 + decoder->dw4;
287 if ((ret & 0x8000) != 0)
290 decoder->dw1 = decoder->dw2;
291 decoder->dw2 = decoder->dw3;
292 decoder->dw3 = decoder->dw4;
302 decode_vector (SirenDecoder decoder, int number_of_regions,
303 int number_of_available_bits, float *decoder_standard_deviation,
304 int *power_categories, float *coefs, int scale_factor)
320 for (region = 0; region < number_of_regions; region++) {
321 category = power_categories[region];
322 coefs_ptr = coefs + (region * region_size);
325 decoder_tree = decoder_tables[category];
327 for (i = 0; i < number_of_vectors[category]; i++) {
330 if (number_of_available_bits <= 0) {
335 index = decoder_tree[index + next_bit ()];
336 number_of_available_bits--;
337 } while ((index & 1) == 0);
341 if (error == 0 && number_of_available_bits >= 0) {
342 for (j = 0; j < vector_dimension[category]; j++) {
344 mlt_quant[category][index & ((1 << index_table[category]) - 1)];
345 index >>= index_table[category];
347 if (decoded_value != 0) {
348 if (next_bit () == 0)
349 decoded_value *= -decoder_standard_deviation[region];
351 decoded_value *= decoder_standard_deviation[region];
352 number_of_available_bits--;
355 *coefs_ptr++ = decoded_value * scale_factor;
364 for (j = region + 1; j < number_of_regions; j++)
365 power_categories[j] = 7;
371 coefs_ptr = coefs + (region * region_size);
375 for (j = 0; j < region_size; j++) {
376 if (*coefs_ptr != 0) {
378 if (fabs (*coefs_ptr) > 2.0 * decoder_standard_deviation[region]) {
385 noise = decoder_standard_deviation[region] * noise_category5[i];
386 } else if (category == 6) {
388 for (j = 0; j < region_size; j++) {
389 if (*coefs_ptr++ != 0)
393 noise = decoder_standard_deviation[region] * noise_category6[i];
394 } else if (category == 7) {
395 noise = decoder_standard_deviation[region] * noise_category7;
400 coefs_ptr = coefs + (region * region_size);
402 if (category == 5 || category == 6 || category == 7) {
403 dw1 = get_dw (decoder);
404 dw2 = get_dw (decoder);
406 for (j = 0; j < 10; j++) {
407 if (category == 7 || *coefs_ptr == 0) {
416 if (category == 7 || *coefs_ptr == 0) {
428 return error == 1 ? -1 : number_of_available_bits;