siren: Run gst-indent script
[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
32 next_bit ()
33 {
34   if (bitstream_ptr == NULL)
35     return -1;
36
37   if (bit_idx == 0) {
38     current_word = *bitstream_ptr++;
39     bit_idx = 16;
40   }
41
42   return (current_word >> --bit_idx) & 1;
43 }
44
45 void
46 set_bitstream (int *stream)
47 {
48   bitstream_ptr = stream;
49   current_word = *bitstream_ptr;
50   bit_idx = 0;
51 }
52
53
54 int
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)
57 {
58   float region_power = 0;
59   int num_bits;
60   int idx;
61   int max_idx, min_idx;
62   int region, i;
63
64   for (region = 0; region < number_of_regions; region++) {
65     region_power = 0.0f;
66     for (i = 0; i < region_size; i++) {
67       region_power +=
68           coefs[(region * region_size) + i] * coefs[(region * region_size) + i];
69     }
70     region_power *= region_size_inverse;
71
72     min_idx = 0;
73     max_idx = 64;
74     for (i = 0; i < 6; i++) {
75       idx = (min_idx + max_idx) / 2;
76       if (region_power_table_boundary[idx - 1] <= region_power) {
77         min_idx = idx;
78       } else {
79         max_idx = idx;
80       }
81     }
82     absolute_region_power_index[region] = min_idx - 24;
83
84   }
85
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;
91   }
92
93   if (absolute_region_power_index[0] < (1 - esf_adjustment))
94     absolute_region_power_index[0] = (1 - esf_adjustment);
95
96   if (absolute_region_power_index[0] > (31 - esf_adjustment))
97     absolute_region_power_index[0] = (31 - esf_adjustment);
98
99   drp_num_bits[0] = 5;
100   drp_code_bits[0] = absolute_region_power_index[0] + esf_adjustment;
101
102
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);
108   }
109
110   num_bits = 5;
111
112   for (region = 0; region < number_of_regions - 1; region++) {
113     idx =
114         absolute_region_power_index[region + 1] -
115         absolute_region_power_index[region] + 12;
116     if (idx < 0)
117       idx = 0;
118
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];
124   }
125
126   return num_bits;
127 }
128
129
130 int
131 decode_envelope (int number_of_regions, float *decoder_standard_deviation,
132     int *absolute_region_power_index, int esf_adjustment)
133 {
134   int index;
135   int i;
136   int envelope_bits = 0;
137
138   index = 0;
139   for (i = 0; i < 5; i++)
140     index = (index << 1) | next_bit ();
141   envelope_bits = 5;
142
143   absolute_region_power_index[0] = index - esf_adjustment;
144   decoder_standard_deviation[0] =
145       standard_deviation[absolute_region_power_index[0] + 24];
146
147   for (i = 1; i < number_of_regions; i++) {
148     index = 0;
149     do {
150       index = differential_decoder_tree[i - 1][index][next_bit ()];
151       envelope_bits++;
152     } while (index > 0);
153
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];
158   }
159
160   return envelope_bits;
161 }
162
163
164
165 static int
166 huffman_vector (int category, int power_idx, float *mlts, int *out)
167 {
168   int i, j;
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;
172   int region_bits = 0;
173
174   bits_available = 32;
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]);
179       if (max != 0) {
180         sign_idx <<= 1;
181         non_zeroes++;
182         if (*mlts > 0)
183           sign_idx++;
184         if (max > max_bin[category] || max < 0)
185           max = max_bin[category];
186
187       }
188       mlts++;
189       idx = (idx * (max_bin[category] + 1)) + max;
190     }
191
192     region_bits += bitcount_tables[category][idx] + non_zeroes;
193     bits_available -= bitcount_tables[category][idx] + non_zeroes;
194     if (bits_available < 0) {
195       *out++ =
196           current_word + (((code_tables[category][idx] << non_zeroes) +
197               sign_idx) >> -bits_available);
198       bits_available += 32;
199       current_word =
200           ((code_tables[category][idx] << non_zeroes) +
201           sign_idx) << bits_available;
202     } else {
203       current_word +=
204           ((code_tables[category][idx] << non_zeroes) +
205           sign_idx) << bits_available;
206     }
207
208   }
209
210   *out = current_word;
211   return region_bits;
212 }
213
214 int
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)
219 {
220   int region;
221   int mlt_bits = 0;
222   int rate_control;
223
224   for (rate_control = 0; rate_control < ((rate_control_possibilities >> 1) - 1);
225       rate_control++)
226     power_categories[category_balance[rate_control]]++;
227
228   for (region = 0; region < number_of_regions; region++) {
229     if (power_categories[region] > 6)
230       region_mlt_bit_counts[region] = 0;
231     else
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];
237   }
238
239   while (mlt_bits < number_of_available_bits && rate_control > 0) {
240     rate_control--;
241     region = category_balance[rate_control];
242     power_categories[region]--;
243
244     if (power_categories[region] < 0)
245       power_categories[region] = 0;
246
247     mlt_bits -= region_mlt_bit_counts[region];
248
249     if (power_categories[region] > 6)
250       region_mlt_bit_counts[region] = 0;
251     else
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));
256
257     mlt_bits += region_mlt_bit_counts[region];
258   }
259
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];
265
266     if (power_categories[region] > 6)
267       region_mlt_bit_counts[region] = 0;
268     else
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));
273
274     mlt_bits += region_mlt_bit_counts[region];
275
276     rate_control++;
277   }
278
279   return rate_control;
280 }
281
282 static int
283 get_dw (SirenDecoder decoder)
284 {
285   int ret = decoder->dw1 + decoder->dw4;
286
287   if ((ret & 0x8000) != 0)
288     ret++;
289
290   decoder->dw1 = decoder->dw2;
291   decoder->dw2 = decoder->dw3;
292   decoder->dw3 = decoder->dw4;
293   decoder->dw4 = ret;
294
295   return ret;
296 }
297
298
299
300
301 int
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)
305 {
306   float *coefs_ptr;
307   float decoded_value;
308   float noise;
309   int *decoder_tree;
310
311   int region;
312   int category;
313   int i, j;
314   int index;
315   int error;
316   int dw1;
317   int dw2;
318
319   error = 0;
320   for (region = 0; region < number_of_regions; region++) {
321     category = power_categories[region];
322     coefs_ptr = coefs + (region * region_size);
323
324     if (category < 7) {
325       decoder_tree = decoder_tables[category];
326
327       for (i = 0; i < number_of_vectors[category]; i++) {
328         index = 0;
329         do {
330           if (number_of_available_bits <= 0) {
331             error = 1;
332             break;
333           }
334
335           index = decoder_tree[index + next_bit ()];
336           number_of_available_bits--;
337         } while ((index & 1) == 0);
338
339         index >>= 1;
340
341         if (error == 0 && number_of_available_bits >= 0) {
342           for (j = 0; j < vector_dimension[category]; j++) {
343             decoded_value =
344                 mlt_quant[category][index & ((1 << index_table[category]) - 1)];
345             index >>= index_table[category];
346
347             if (decoded_value != 0) {
348               if (next_bit () == 0)
349                 decoded_value *= -decoder_standard_deviation[region];
350               else
351                 decoded_value *= decoder_standard_deviation[region];
352               number_of_available_bits--;
353             }
354
355             *coefs_ptr++ = decoded_value * scale_factor;
356           }
357         } else {
358           error = 1;
359           break;
360         }
361       }
362
363       if (error == 1) {
364         for (j = region + 1; j < number_of_regions; j++)
365           power_categories[j] = 7;
366         category = 7;
367       }
368     }
369
370
371     coefs_ptr = coefs + (region * region_size);
372
373     if (category == 5) {
374       i = 0;
375       for (j = 0; j < region_size; j++) {
376         if (*coefs_ptr != 0) {
377           i++;
378           if (fabs (*coefs_ptr) > 2.0 * decoder_standard_deviation[region]) {
379             i += 3;
380           }
381         }
382         coefs_ptr++;
383       }
384
385       noise = decoder_standard_deviation[region] * noise_category5[i];
386     } else if (category == 6) {
387       i = 0;
388       for (j = 0; j < region_size; j++) {
389         if (*coefs_ptr++ != 0)
390           i++;
391       }
392
393       noise = decoder_standard_deviation[region] * noise_category6[i];
394     } else if (category == 7) {
395       noise = decoder_standard_deviation[region] * noise_category7;
396     } else {
397       noise = 0;
398     }
399
400     coefs_ptr = coefs + (region * region_size);
401
402     if (category == 5 || category == 6 || category == 7) {
403       dw1 = get_dw (decoder);
404       dw2 = get_dw (decoder);
405
406       for (j = 0; j < 10; j++) {
407         if (category == 7 || *coefs_ptr == 0) {
408           if ((dw1 & 1))
409             *coefs_ptr = noise;
410           else
411             *coefs_ptr = -noise;
412         }
413         coefs_ptr++;
414         dw1 >>= 1;
415
416         if (category == 7 || *coefs_ptr == 0) {
417           if ((dw2 & 1))
418             *coefs_ptr = noise;
419           else
420             *coefs_ptr = -noise;
421         }
422         coefs_ptr++;
423         dw2 >>= 1;
424       }
425     }
426   }
427
428   return error == 1 ? -1 : number_of_available_bits;
429 }