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};
46 float step_size[8] = {
56 float step_size_inverse[8];
58 static int siren_initialized = 0;
61 STEPSIZE = 2.0 * log(sqrt(2));
63 #define STEPSIZE 0.3010299957
69 if (siren_initialized == 1)
73 region_size_inverse = 1.0f/region_size;
75 for (i = 0; i < 64; i++) {
76 region_power = (float) pow(10, (i-24) * STEPSIZE);
77 standard_deviation[i] = (float) sqrt(region_power);
78 deviation_inverse[i] = (float) 1.0 / standard_deviation[i];
81 for (i = 0; i < 63; i++)
82 region_power_table_boundary[i] = (float) pow(10, (i-24 + 0.5) * STEPSIZE);
84 for (i = 0; i < 8; i++)
85 step_size_inverse[i] = (float) 1.0 / step_size[i];
90 siren_initialized = 1;
94 int categorize_regions(int number_of_regions, int number_of_available_bits, int *absolute_region_power_index, int *power_categories, int *category_balance) {
95 int region, delta, i, temp;
96 int expected_number_of_code_bits;
99 num_rate_control_possibilities,
103 int max_rate_categories[28];
104 int min_rate_categories[28];
105 int temp_category_balances[64];
106 int *min_rate_ptr = NULL;
107 int *max_rate_ptr = NULL;
109 if (number_of_regions == 14) {
110 num_rate_control_possibilities = 16;
111 if ( number_of_available_bits > 320)
112 number_of_available_bits = ((number_of_available_bits - 320) * 5/8) + 320;
114 num_rate_control_possibilities = 32;
115 if (number_of_regions == 28 && number_of_available_bits > 640)
116 number_of_available_bits = ((number_of_available_bits - 640) * 5/8) + 640;
120 for (delta = 32; number_of_regions > 0 && delta > 0; delta /= 2) {
121 expected_number_of_code_bits = 0;
122 for (region = 0; region < number_of_regions; region++) {
123 i = (delta + offset - absolute_region_power_index[region]) >> 1;
129 power_categories[region] = i;
130 expected_number_of_code_bits += expected_bits_table[i];
133 if (expected_number_of_code_bits >= number_of_available_bits-32)
137 expected_number_of_code_bits = 0;
138 for (region = 0; region < number_of_regions; region++) {
139 i = (offset - absolute_region_power_index[region]) >> 1;
144 max_rate_categories[region] = min_rate_categories[region] = power_categories[region] = i;
145 expected_number_of_code_bits += expected_bits_table[i];
149 min = max = expected_number_of_code_bits;
150 min_rate_ptr = max_rate_ptr = temp_category_balances + num_rate_control_possibilities;
151 for (i = 0; i < num_rate_control_possibilities -1; i++) {
152 if (min + max > number_of_available_bits * 2) {
154 for (region = number_of_regions-1; region >= 0; region--) {
155 if (min_rate_categories[region] < 7) {
156 temp = offset - absolute_region_power_index[region] - 2*min_rate_categories[region];
157 if (temp > raw_value) {
159 raw_min_idx = region;
163 *min_rate_ptr++ = raw_min_idx;
164 min += expected_bits_table[min_rate_categories[raw_min_idx] + 1] - expected_bits_table[min_rate_categories[raw_min_idx]];
165 min_rate_categories[raw_min_idx]++;
168 for (region = 0; region < number_of_regions; region++) {
169 if (max_rate_categories[region] > 0 ) {
170 temp = offset - absolute_region_power_index[region] - 2*max_rate_categories[region];
171 if (temp < raw_value) {
173 raw_max_idx = region;
178 *--max_rate_ptr = raw_max_idx;
179 max += expected_bits_table[max_rate_categories[raw_max_idx] - 1] - expected_bits_table[max_rate_categories[raw_max_idx]];
180 max_rate_categories[raw_max_idx]--;
184 for (region = 0; region < number_of_regions; region++)
185 power_categories[region] = max_rate_categories[region];
187 for (i = 0; i < num_rate_control_possibilities-1; i++)
188 category_balance[i] = *max_rate_ptr++;
197 Looks like the flag means what kind of encoding is used
198 for now, it looks like :
199 0 : the sample rate is not encoded in the frame
200 1 - 2 : the sample rate is fixed in the frame
201 3 : sample rate is variable and there is one for each frame
204 int GetSirenCodecInfo(int flag, int sample_rate, int *number_of_coefs, int *sample_rate_bits, int *rate_control_bits, int *rate_control_possibilities, int *checksum_bits, int *esf_adjustment, int *scale_factor, int *number_of_regions, int *sample_rate_code, int *bits_per_frame ) {
207 *number_of_coefs = 320;
208 *sample_rate_bits = 0;
209 *rate_control_bits = 4;
210 *rate_control_possibilities = 16;
213 *number_of_regions = 14;
214 *sample_rate_code = 0;
218 *number_of_coefs = 320;
219 *sample_rate_bits = 2;
220 *rate_control_bits = 4;
221 *rate_control_possibilities = 16;
223 *esf_adjustment = -2;
224 *number_of_regions = 14;
226 if (sample_rate == 16000)
227 *sample_rate_code = 1;
228 else if (sample_rate == 24000)
229 *sample_rate_code = 2;
230 else if (sample_rate == 32000)
231 *sample_rate_code = 3;
236 *number_of_coefs = 640;
237 *sample_rate_bits = 2;
238 *rate_control_bits = 5;
239 *rate_control_possibilities = 32;
242 *number_of_regions = 28;
245 if (sample_rate == 24000)
246 *sample_rate_code = 1;
247 else if (sample_rate == 24000)
248 *sample_rate_code = 2;
249 else if (sample_rate == 48000)
250 *sample_rate_code = 3;
256 *number_of_coefs = 640;
257 *sample_rate_bits = 6;
258 *rate_control_bits = 5;
259 *rate_control_possibilities = 32;
264 switch (sample_rate) {
266 *number_of_regions = 12;
267 *sample_rate_code = 59;
270 *number_of_regions = 12;
271 *sample_rate_code = 1;
274 *number_of_regions = 12;
275 *sample_rate_code = 13;
278 *number_of_regions = 12;
279 *sample_rate_code = 14;
282 *number_of_regions = 12;
283 *sample_rate_code = 15;
286 *number_of_regions = 12;
287 *sample_rate_code = 16;
290 *number_of_regions = 12;
291 *sample_rate_code = 2;
294 *number_of_regions = 12;
295 *sample_rate_code = 17;
298 *number_of_regions = 12;
299 *sample_rate_code = 18;
302 *number_of_regions = 12;
303 *sample_rate_code = 19;
306 *number_of_regions = 12;
307 *sample_rate_code = 20;
310 *number_of_regions = 12;
311 *sample_rate_code = 21;
314 *number_of_regions = 16;
315 *sample_rate_code = 3;
318 *number_of_regions = 16;
319 *sample_rate_code = 22;
322 *number_of_regions = 16;
323 *sample_rate_code = 23;
326 *number_of_regions = 16;
327 *sample_rate_code = 24;
330 *number_of_regions = 16;
331 *sample_rate_code = 25;
334 *number_of_regions = 16;
335 *sample_rate_code = 26;
338 *number_of_regions = 18;
339 *sample_rate_code = 4;
342 *number_of_regions = 18;
343 *sample_rate_code = 27;
346 *number_of_regions = 18;
347 *sample_rate_code = 28;
350 *number_of_regions = 18;
351 *sample_rate_code = 29;
354 *number_of_regions = 18;
355 *sample_rate_code = 30;
358 *number_of_regions = 18;
359 *sample_rate_code = 31;
362 *number_of_regions = 20;
363 *sample_rate_code = 5;
366 *number_of_regions = 20;
367 *sample_rate_code = 32;
370 *number_of_regions = 20;
371 *sample_rate_code = 33;
374 *number_of_regions = 20;
375 *sample_rate_code = 34;
378 *number_of_regions = 20;
379 *sample_rate_code = 35;
382 *number_of_regions = 20;
383 *sample_rate_code = 36;
386 *number_of_regions = 22;
387 *sample_rate_code = 6;
390 *number_of_regions = 22;
391 *sample_rate_code = 37;
394 *number_of_regions = 22;
395 *sample_rate_code = 38;
398 *number_of_regions = 22;
399 *sample_rate_code = 39;
402 *number_of_regions = 22;
403 *sample_rate_code = 40;
406 *number_of_regions = 22;
407 *sample_rate_code = 41;
410 *number_of_regions = 24;
411 *sample_rate_code = 7;
414 *number_of_regions = 24;
415 *sample_rate_code = 42;
418 *number_of_regions = 24;
419 *sample_rate_code = 43;
422 *number_of_regions = 24;
423 *sample_rate_code = 44;
426 *number_of_regions = 24;
427 *sample_rate_code = 45;
430 *number_of_regions = 24;
431 *sample_rate_code = 46;
434 *number_of_regions = 26;
435 *sample_rate_code = 8;
438 *number_of_regions = 26;
439 *sample_rate_code = 47;
442 *number_of_regions = 26;
443 *sample_rate_code = 48;
446 *number_of_regions = 26;
447 *sample_rate_code = 49;
450 *number_of_regions = 26;
451 *sample_rate_code = 50;
454 *number_of_regions = 26;
455 *sample_rate_code = 51;
458 *number_of_regions = 28;
459 *sample_rate_code = 9;
462 *number_of_regions = 28;
463 *sample_rate_code = 52;
466 *number_of_regions = 28;
467 *sample_rate_code = 53;
470 *number_of_regions = 28;
471 *sample_rate_code = 54;
474 *number_of_regions = 28;
475 *sample_rate_code = 55;
478 *number_of_regions = 28;
479 *sample_rate_code = 56;
482 *number_of_regions = 28;
483 *sample_rate_code = 10;
486 *number_of_regions = 28;
487 *sample_rate_code = 57;
490 *number_of_regions = 28;
491 *sample_rate_code = 58;
502 *bits_per_frame = sample_rate / 50;