35a4ae05c74935b2542d25b9d887b80237db0849
[platform/upstream/gstreamer.git] / gst / siren / huffman.c
1 /*
2  * Siren Encoder/Decoder library
3  *
4  *   @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22
23 #include "siren7.h"
24 #include "huffman_consts.h"
25
26
27 static short current_word = 0;
28 static int bit_idx = 0;
29 static int *bitstream_ptr = NULL;
30
31 int next_bit() {
32   if (bitstream_ptr == NULL)
33     return -1;
34
35   if (bit_idx == 0) {
36     current_word = *bitstream_ptr++;
37     bit_idx = 16;
38   }
39
40   return (current_word >> --bit_idx) & 1;
41 }
42
43 void set_bitstream(int *stream) {
44   bitstream_ptr = stream;
45   current_word =  *bitstream_ptr;
46   bit_idx = 0;
47 }
48
49
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;
52   int num_bits;
53   int idx;
54   int max_idx, min_idx;
55   int region, i;
56
57   for (region = 0; region < number_of_regions; region++) {
58     region_power = 0.0f;
59     for (i = 0 ; i < region_size; i++) {
60       region_power += coefs[(region*region_size)+i] * coefs[(region*region_size)+i];
61     }
62     region_power *= region_size_inverse;
63
64     min_idx = 0;
65     max_idx = 64;
66     for (i = 0; i < 6; i++) {
67       idx = (min_idx + max_idx) / 2;
68       if (region_power_table_boundary[idx-1] <= region_power) {
69         min_idx = idx;
70       } else {
71         max_idx = idx;
72       }
73     }
74     absolute_region_power_index[region] = min_idx - 24;
75
76   }
77
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;
81   }
82
83   if (absolute_region_power_index[0] < (1-esf_adjustment))
84     absolute_region_power_index[0] = (1-esf_adjustment);
85
86   if (absolute_region_power_index[0] > (31-esf_adjustment))
87     absolute_region_power_index[0] = (31-esf_adjustment);
88
89   drp_num_bits[0] = 5;
90   drp_code_bits[0] = absolute_region_power_index[0] + esf_adjustment;
91
92
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);
98   }
99
100   num_bits = 5;
101
102   for(region = 0; region < number_of_regions-1; region++) {
103     idx = absolute_region_power_index[region+1] - absolute_region_power_index[region] + 12;
104     if (idx < 0)
105       idx = 0;
106
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];
111   }
112
113   return num_bits;
114 }
115
116
117 int decode_envelope(int number_of_regions, float *decoder_standard_deviation, int *absolute_region_power_index, int esf_adjustment) {
118   int index;
119   int i;
120   int envelope_bits = 0;
121
122   index = 0;
123   for (i = 0; i < 5; i++)
124     index = (index<<1) | next_bit();
125   envelope_bits = 5;
126
127   absolute_region_power_index[0] = index - esf_adjustment;
128   decoder_standard_deviation[0] = standard_deviation[absolute_region_power_index[0] + 24];
129
130   for (i = 1; i < number_of_regions; i++) {
131     index = 0;
132     do {
133       index = differential_decoder_tree[i-1][index][next_bit()];
134       envelope_bits++;
135     } while (index > 0);
136
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];
139   }
140
141   return envelope_bits;
142 }
143
144
145
146 static int huffman_vector(int category, int power_idx, float *mlts, int *out) {
147   int i, j;
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;
151   int region_bits = 0;
152
153   bits_available = 32;
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]);
158       if (max != 0) {
159         sign_idx <<= 1;
160         non_zeroes++;
161         if (*mlts  > 0)
162           sign_idx++;
163         if (max > max_bin[category] || max < 0)
164           max = max_bin[category];
165
166       }
167       mlts++;
168       idx = (idx * (max_bin[category] + 1)) + max;
169     }
170
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;
177     } else {
178       current_word += ((code_tables[category][idx] << non_zeroes) + sign_idx) << bits_available;
179     }
180
181   }
182
183   *out = current_word;
184   return region_bits;
185 }
186
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) {
188   int region;
189   int mlt_bits = 0;
190   int rate_control;
191
192   for (rate_control = 0; rate_control < ((rate_control_possibilities >> 1) - 1); rate_control++)
193     power_categories[category_balance[rate_control]]++;
194
195   for (region = 0; region < number_of_regions; region++) {
196     if (power_categories[region] > 6)
197       region_mlt_bit_counts[region] = 0;
198     else
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];
202   }
203
204   while (mlt_bits < number_of_available_bits && rate_control > 0) {
205     rate_control--;
206     region = category_balance[rate_control];
207     power_categories[region]--;
208
209     if (power_categories[region] < 0)
210       power_categories[region] = 0;
211
212     mlt_bits -= region_mlt_bit_counts[region];
213
214     if (power_categories[region] > 6)
215       region_mlt_bit_counts[region] = 0;
216     else
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));
219
220     mlt_bits += region_mlt_bit_counts[region];
221   }
222
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];
227
228     if (power_categories[region] > 6)
229       region_mlt_bit_counts[region] = 0;
230     else
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));
233
234     mlt_bits += region_mlt_bit_counts[region];
235
236     rate_control++;
237   }
238
239   return rate_control;
240 }
241
242 static int get_dw(SirenDecoder decoder) {
243   int ret = decoder->dw1 + decoder->dw4;
244
245   if ((ret & 0x8000) != 0)
246     ret++;
247
248   decoder->dw1 = decoder->dw2;
249   decoder->dw2 = decoder->dw3;
250   decoder->dw3 = decoder->dw4;
251   decoder->dw4 = ret;
252
253   return ret;
254 }
255
256
257
258
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) {
260   float *coefs_ptr;
261   float decoded_value;
262   float noise;
263   int *decoder_tree;
264
265   int region;
266   int category;
267   int i, j;
268   int index;
269   int error;
270   int dw1;
271   int dw2;
272
273   error = 0;
274   for (region = 0; region < number_of_regions; region++) {
275     category = power_categories[region];
276     coefs_ptr = coefs + (region * region_size);
277
278     if (category < 7) {
279       decoder_tree = decoder_tables[category];
280
281       for (i = 0; i < number_of_vectors[category]; i++) {
282         index = 0;
283         do {
284           if (number_of_available_bits <= 0) {
285             error = 1;
286             break;
287           }
288
289           index = decoder_tree[index + next_bit()];
290           number_of_available_bits--;
291         } while ((index & 1) == 0);
292
293         index >>= 1;
294
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];
299
300             if (decoded_value != 0) {
301               if (next_bit() == 0)
302                 decoded_value *= -decoder_standard_deviation[region];
303               else
304                 decoded_value *= decoder_standard_deviation[region];
305               number_of_available_bits--;
306             }
307
308             *coefs_ptr++ = decoded_value * scale_factor;
309           }
310         } else {
311           error = 1;
312           break;
313         }
314       }
315
316       if (error == 1) {
317         for (j = region + 1; j < number_of_regions; j++)
318           power_categories[j] = 7;
319         category = 7;
320       }
321     }
322
323
324     coefs_ptr = coefs + (region * region_size);
325
326     if (category == 5) {
327       i = 0;
328       for (j = 0; j < region_size; j++) {
329         if (*coefs_ptr != 0) {
330           i++;
331           if (fabs(*coefs_ptr) > 2.0 * decoder_standard_deviation[region]) {
332             i += 3;
333           }
334         }
335         coefs_ptr++;
336       }
337
338       noise = decoder_standard_deviation[region] * noise_category5[i];
339     } else if (category == 6) {
340       i = 0;
341       for (j = 0; j < region_size; j++) {
342         if (*coefs_ptr++ != 0)
343           i++;
344       }
345
346       noise = decoder_standard_deviation[region] * noise_category6[i];
347     } else if (category == 7) {
348       noise =  decoder_standard_deviation[region] * noise_category7;
349     } else {
350       noise = 0;
351     }
352
353     coefs_ptr = coefs + (region * region_size);
354
355     if (category == 5 || category == 6 || category == 7) {
356       dw1 = get_dw(decoder);
357       dw2 = get_dw(decoder);
358
359       for (j=0; j<10; j++) {
360         if (category == 7 || *coefs_ptr == 0) {
361           if ((dw1 & 1))
362             *coefs_ptr = noise;
363           else
364             *coefs_ptr = -noise;
365         }
366         coefs_ptr++;
367         dw1 >>= 1;
368
369         if (category == 7 || *coefs_ptr == 0) {
370           if ((dw2 & 1))
371             *coefs_ptr = noise;
372           else
373             *coefs_ptr = -noise;
374         }
375         coefs_ptr++;
376         dw2 >>= 1;
377       }
378     }
379   }
380
381   return error == 1 ? -1 : number_of_available_bits;
382 }