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.
25 float region_size_inverse;
27 float standard_deviation[64];
28 float deviation_inverse[64];
29 float region_power_table_boundary[63];
31 int expected_bits_table[8] = { 52, 47, 43, 37, 29, 22, 16, 0 };
32 int vector_dimension[8] = { 2, 2, 2, 4, 4, 5, 5, 1 };
33 int number_of_vectors[8] = { 10, 10, 10, 5, 5, 4, 4, 20 };
34 float dead_zone[8] = { 0.3f, 0.33f, 0.36f, 0.39f, 0.42f, 0.45f, 0.5f, 0.5f };
47 float step_size[8] = {
58 float step_size_inverse[8];
60 static int siren_initialized = 0;
63 STEPSIZE = 2.0 * log(sqrt(2));
65 #define STEPSIZE 0.3010299957
73 if (siren_initialized == 1)
77 region_size_inverse = 1.0f / region_size;
79 for (i = 0; i < 64; i++) {
80 region_power = (float) pow (10, (i - 24) * STEPSIZE);
81 standard_deviation[i] = (float) sqrt (region_power);
82 deviation_inverse[i] = (float) 1.0 / standard_deviation[i];
85 for (i = 0; i < 63; i++)
86 region_power_table_boundary[i] =
87 (float) pow (10, (i - 24 + 0.5) * STEPSIZE);
89 for (i = 0; i < 8; i++)
90 step_size_inverse[i] = (float) 1.0 / step_size[i];
95 siren_initialized = 1;
100 categorize_regions (int number_of_regions, int number_of_available_bits,
101 int *absolute_region_power_index, int *power_categories,
102 int *category_balance)
104 int region, delta, i, temp;
105 int expected_number_of_code_bits;
108 num_rate_control_possibilities,
109 raw_value, raw_max_idx = 0, raw_min_idx = 0;
110 int max_rate_categories[28];
111 int min_rate_categories[28];
112 int temp_category_balances[64];
113 int *min_rate_ptr = NULL;
114 int *max_rate_ptr = NULL;
116 if (number_of_regions == 14) {
117 num_rate_control_possibilities = 16;
118 if (number_of_available_bits > 320)
119 number_of_available_bits =
120 ((number_of_available_bits - 320) * 5 / 8) + 320;
122 num_rate_control_possibilities = 32;
123 if (number_of_regions == 28 && number_of_available_bits > 640)
124 number_of_available_bits =
125 ((number_of_available_bits - 640) * 5 / 8) + 640;
129 for (delta = 32; number_of_regions > 0 && delta > 0; delta /= 2) {
130 expected_number_of_code_bits = 0;
131 for (region = 0; region < number_of_regions; region++) {
132 i = (delta + offset - absolute_region_power_index[region]) >> 1;
138 power_categories[region] = i;
139 expected_number_of_code_bits += expected_bits_table[i];
142 if (expected_number_of_code_bits >= number_of_available_bits - 32)
146 expected_number_of_code_bits = 0;
147 for (region = 0; region < number_of_regions; region++) {
148 i = (offset - absolute_region_power_index[region]) >> 1;
153 max_rate_categories[region] = min_rate_categories[region] =
154 power_categories[region] = i;
155 expected_number_of_code_bits += expected_bits_table[i];
159 min = max = expected_number_of_code_bits;
160 min_rate_ptr = max_rate_ptr =
161 temp_category_balances + num_rate_control_possibilities;
162 for (i = 0; i < num_rate_control_possibilities - 1; i++) {
163 if (min + max > number_of_available_bits * 2) {
165 for (region = number_of_regions - 1; region >= 0; region--) {
166 if (min_rate_categories[region] < 7) {
168 offset - absolute_region_power_index[region] -
169 2 * min_rate_categories[region];
170 if (temp > raw_value) {
172 raw_min_idx = region;
176 *min_rate_ptr++ = raw_min_idx;
178 expected_bits_table[min_rate_categories[raw_min_idx] + 1] -
179 expected_bits_table[min_rate_categories[raw_min_idx]];
180 min_rate_categories[raw_min_idx]++;
183 for (region = 0; region < number_of_regions; region++) {
184 if (max_rate_categories[region] > 0) {
186 offset - absolute_region_power_index[region] -
187 2 * max_rate_categories[region];
188 if (temp < raw_value) {
190 raw_max_idx = region;
195 *--max_rate_ptr = raw_max_idx;
197 expected_bits_table[max_rate_categories[raw_max_idx] - 1] -
198 expected_bits_table[max_rate_categories[raw_max_idx]];
199 max_rate_categories[raw_max_idx]--;
203 for (region = 0; region < number_of_regions; region++)
204 power_categories[region] = max_rate_categories[region];
206 for (i = 0; i < num_rate_control_possibilities - 1; i++)
207 category_balance[i] = *max_rate_ptr++;
216 Looks like the flag means what kind of encoding is used
217 for now, it looks like :
218 0 : the sample rate is not encoded in the frame
219 1 - 2 : the sample rate is fixed in the frame
220 3 : sample rate is variable and there is one for each frame
224 GetSirenCodecInfo (int flag, int sample_rate, int *number_of_coefs,
225 int *sample_rate_bits, int *rate_control_bits,
226 int *rate_control_possibilities, int *checksum_bits, int *esf_adjustment,
227 int *scale_factor, int *number_of_regions, int *sample_rate_code,
232 *number_of_coefs = 320;
233 *sample_rate_bits = 0;
234 *rate_control_bits = 4;
235 *rate_control_possibilities = 16;
238 *number_of_regions = 14;
239 *sample_rate_code = 0;
243 *number_of_coefs = 320;
244 *sample_rate_bits = 2;
245 *rate_control_bits = 4;
246 *rate_control_possibilities = 16;
248 *esf_adjustment = -2;
249 *number_of_regions = 14;
251 if (sample_rate == 16000)
252 *sample_rate_code = 1;
253 else if (sample_rate == 24000)
254 *sample_rate_code = 2;
255 else if (sample_rate == 32000)
256 *sample_rate_code = 3;
261 *number_of_coefs = 640;
262 *sample_rate_bits = 2;
263 *rate_control_bits = 5;
264 *rate_control_possibilities = 32;
267 *number_of_regions = 28;
270 if (sample_rate == 24000)
271 *sample_rate_code = 1;
272 else if (sample_rate == 24000)
273 *sample_rate_code = 2;
274 else if (sample_rate == 48000)
275 *sample_rate_code = 3;
281 *number_of_coefs = 640;
282 *sample_rate_bits = 6;
283 *rate_control_bits = 5;
284 *rate_control_possibilities = 32;
289 switch (sample_rate) {
291 *number_of_regions = 12;
292 *sample_rate_code = 59;
295 *number_of_regions = 12;
296 *sample_rate_code = 1;
299 *number_of_regions = 12;
300 *sample_rate_code = 13;
303 *number_of_regions = 12;
304 *sample_rate_code = 14;
307 *number_of_regions = 12;
308 *sample_rate_code = 15;
311 *number_of_regions = 12;
312 *sample_rate_code = 16;
315 *number_of_regions = 12;
316 *sample_rate_code = 2;
319 *number_of_regions = 12;
320 *sample_rate_code = 17;
323 *number_of_regions = 12;
324 *sample_rate_code = 18;
327 *number_of_regions = 12;
328 *sample_rate_code = 19;
331 *number_of_regions = 12;
332 *sample_rate_code = 20;
335 *number_of_regions = 12;
336 *sample_rate_code = 21;
339 *number_of_regions = 16;
340 *sample_rate_code = 3;
343 *number_of_regions = 16;
344 *sample_rate_code = 22;
347 *number_of_regions = 16;
348 *sample_rate_code = 23;
351 *number_of_regions = 16;
352 *sample_rate_code = 24;
355 *number_of_regions = 16;
356 *sample_rate_code = 25;
359 *number_of_regions = 16;
360 *sample_rate_code = 26;
363 *number_of_regions = 18;
364 *sample_rate_code = 4;
367 *number_of_regions = 18;
368 *sample_rate_code = 27;
371 *number_of_regions = 18;
372 *sample_rate_code = 28;
375 *number_of_regions = 18;
376 *sample_rate_code = 29;
379 *number_of_regions = 18;
380 *sample_rate_code = 30;
383 *number_of_regions = 18;
384 *sample_rate_code = 31;
387 *number_of_regions = 20;
388 *sample_rate_code = 5;
391 *number_of_regions = 20;
392 *sample_rate_code = 32;
395 *number_of_regions = 20;
396 *sample_rate_code = 33;
399 *number_of_regions = 20;
400 *sample_rate_code = 34;
403 *number_of_regions = 20;
404 *sample_rate_code = 35;
407 *number_of_regions = 20;
408 *sample_rate_code = 36;
411 *number_of_regions = 22;
412 *sample_rate_code = 6;
415 *number_of_regions = 22;
416 *sample_rate_code = 37;
419 *number_of_regions = 22;
420 *sample_rate_code = 38;
423 *number_of_regions = 22;
424 *sample_rate_code = 39;
427 *number_of_regions = 22;
428 *sample_rate_code = 40;
431 *number_of_regions = 22;
432 *sample_rate_code = 41;
435 *number_of_regions = 24;
436 *sample_rate_code = 7;
439 *number_of_regions = 24;
440 *sample_rate_code = 42;
443 *number_of_regions = 24;
444 *sample_rate_code = 43;
447 *number_of_regions = 24;
448 *sample_rate_code = 44;
451 *number_of_regions = 24;
452 *sample_rate_code = 45;
455 *number_of_regions = 24;
456 *sample_rate_code = 46;
459 *number_of_regions = 26;
460 *sample_rate_code = 8;
463 *number_of_regions = 26;
464 *sample_rate_code = 47;
467 *number_of_regions = 26;
468 *sample_rate_code = 48;
471 *number_of_regions = 26;
472 *sample_rate_code = 49;
475 *number_of_regions = 26;
476 *sample_rate_code = 50;
479 *number_of_regions = 26;
480 *sample_rate_code = 51;
483 *number_of_regions = 28;
484 *sample_rate_code = 9;
487 *number_of_regions = 28;
488 *sample_rate_code = 52;
491 *number_of_regions = 28;
492 *sample_rate_code = 53;
495 *number_of_regions = 28;
496 *sample_rate_code = 54;
499 *number_of_regions = 28;
500 *sample_rate_code = 55;
503 *number_of_regions = 28;
504 *sample_rate_code = 56;
507 *number_of_regions = 28;
508 *sample_rate_code = 10;
511 *number_of_regions = 28;
512 *sample_rate_code = 57;
515 *number_of_regions = 28;
516 *sample_rate_code = 58;
527 *bits_per_frame = sample_rate / 50;