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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #include "siren7.h"
23
24 int region_size;
25 float region_size_inverse;
26
27 float standard_deviation[64];
28 float deviation_inverse[64];
29 float region_power_table_boundary[63];
30
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 };
35
36 int max_bin[8] = {
37 13,
38 9,
39 6,
40 4,
41 3,
42 2,
43 1,
44 1
45 };
46
47 float step_size[8] = {
48 0.3536f,
49 0.5f,
50 0.70709997f,
51 1.0f,
52 1.4141999f,
53 2.0f,
54 2.8283999f,
55 2.8283999f
56 };
57
58 float step_size_inverse[8];
59
60 static int siren_initialized = 0;
61
62 /*
63 STEPSIZE = 2.0 * log(sqrt(2));
64 */
65 #define STEPSIZE 0.3010299957f
66
67 void
siren_init(void)68 siren_init (void)
69 {
70 int i;
71 float region_power;
72
73 if (siren_initialized == 1)
74 return;
75
76 region_size = 20;
77 region_size_inverse = 1.0f / region_size;
78
79 for (i = 0; i < 64; i++) {
80 region_power = powf (10.0f, (i - 24) * STEPSIZE);
81 standard_deviation[i] = sqrtf (region_power);
82 deviation_inverse[i] = 1.0f / standard_deviation[i];
83 }
84
85 for (i = 0; i < 63; i++)
86 region_power_table_boundary[i] =
87 (float) pow (10, (i - 24 + 0.5) * STEPSIZE);
88
89 for (i = 0; i < 8; i++)
90 step_size_inverse[i] = (float) 1.0 / step_size[i];
91
92 siren_dct4_init ();
93 siren_rmlt_init ();
94
95 siren_initialized = 1;
96 }
97
98
99 int
categorize_regions(int number_of_regions,int number_of_available_bits,int * absolute_region_power_index,int * power_categories,int * category_balance)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)
103 {
104 int region, delta, i, temp;
105 int expected_number_of_code_bits;
106 int min, max;
107 int offset,
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;
115
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;
121 } else {
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;
126 }
127
128 offset = -32;
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;
133 if (i > 7)
134 i = 7;
135 else if (i < 0)
136 i = 0;
137
138 power_categories[region] = i;
139 expected_number_of_code_bits += expected_bits_table[i];
140
141 }
142 if (expected_number_of_code_bits >= number_of_available_bits - 32)
143 offset += delta;
144 }
145
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;
149 if (i > 7)
150 i = 7;
151 else if (i < 0)
152 i = 0;
153 max_rate_categories[region] = min_rate_categories[region] =
154 power_categories[region] = i;
155 expected_number_of_code_bits += expected_bits_table[i];
156 }
157
158
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) {
164 raw_value = -99;
165 for (region = number_of_regions - 1; region >= 0; region--) {
166 if (min_rate_categories[region] < 7) {
167 temp =
168 offset - absolute_region_power_index[region] -
169 2 * min_rate_categories[region];
170 if (temp > raw_value) {
171 raw_value = temp;
172 raw_min_idx = region;
173 }
174 }
175 }
176 *min_rate_ptr++ = raw_min_idx;
177 min +=
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]++;
181 } else {
182 raw_value = 99;
183 for (region = 0; region < number_of_regions; region++) {
184 if (max_rate_categories[region] > 0) {
185 temp =
186 offset - absolute_region_power_index[region] -
187 2 * max_rate_categories[region];
188 if (temp < raw_value) {
189 raw_value = temp;
190 raw_max_idx = region;
191 }
192 }
193 }
194
195 *--max_rate_ptr = raw_max_idx;
196 max +=
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]--;
200 }
201 }
202
203 for (region = 0; region < number_of_regions; region++)
204 power_categories[region] = max_rate_categories[region];
205
206 for (i = 0; i < num_rate_control_possibilities - 1; i++)
207 category_balance[i] = *max_rate_ptr++;
208
209
210 return 0;
211 }
212
213
214
215 /*
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
221 */
222
223 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)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,
228 int *bits_per_frame)
229 {
230 switch (flag) {
231 case 0:
232 *number_of_coefs = 320;
233 *sample_rate_bits = 0;
234 *rate_control_bits = 4;
235 *rate_control_possibilities = 16;
236 *checksum_bits = 0;
237 *esf_adjustment = 7;
238 *number_of_regions = 14;
239 *sample_rate_code = 0;
240 *scale_factor = 22;
241 break;
242 case 1:
243 *number_of_coefs = 320;
244 *sample_rate_bits = 2;
245 *rate_control_bits = 4;
246 *rate_control_possibilities = 16;
247 *checksum_bits = 4;
248 *esf_adjustment = -2;
249 *number_of_regions = 14;
250 *scale_factor = 1;
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;
257 else
258 return 3;
259 break;
260 case 2:
261 *number_of_coefs = 640;
262 *sample_rate_bits = 2;
263 *rate_control_bits = 5;
264 *rate_control_possibilities = 32;
265 *checksum_bits = 4;
266 *esf_adjustment = 7;
267 *number_of_regions = 28;
268 *scale_factor = 33;
269
270 if (sample_rate == 24000)
271 *sample_rate_code = 1;
272 else if (sample_rate == 32000)
273 *sample_rate_code = 2;
274 else if (sample_rate == 48000)
275 *sample_rate_code = 3;
276 else
277 return 3;
278
279 break;
280 case 3:
281 *number_of_coefs = 640;
282 *sample_rate_bits = 6;
283 *rate_control_bits = 5;
284 *rate_control_possibilities = 32;
285 *checksum_bits = 4;
286 *esf_adjustment = 7;
287 *scale_factor = 33;
288
289 switch (sample_rate) {
290 case 8800:
291 *number_of_regions = 12;
292 *sample_rate_code = 59;
293 break;
294 case 9600:
295 *number_of_regions = 12;
296 *sample_rate_code = 1;
297 break;
298 case 10400:
299 *number_of_regions = 12;
300 *sample_rate_code = 13;
301 break;
302 case 10800:
303 *number_of_regions = 12;
304 *sample_rate_code = 14;
305 break;
306 case 11200:
307 *number_of_regions = 12;
308 *sample_rate_code = 15;
309 break;
310 case 11600:
311 *number_of_regions = 12;
312 *sample_rate_code = 16;
313 break;
314 case 12000:
315 *number_of_regions = 12;
316 *sample_rate_code = 2;
317 break;
318 case 12400:
319 *number_of_regions = 12;
320 *sample_rate_code = 17;
321 break;
322 case 12800:
323 *number_of_regions = 12;
324 *sample_rate_code = 18;
325 break;
326 case 13200:
327 *number_of_regions = 12;
328 *sample_rate_code = 19;
329 break;
330 case 13600:
331 *number_of_regions = 12;
332 *sample_rate_code = 20;
333 break;
334 case 14000:
335 *number_of_regions = 12;
336 *sample_rate_code = 21;
337 break;
338 case 14400:
339 *number_of_regions = 16;
340 *sample_rate_code = 3;
341 break;
342 case 14800:
343 *number_of_regions = 16;
344 *sample_rate_code = 22;
345 break;
346 case 15200:
347 *number_of_regions = 16;
348 *sample_rate_code = 23;
349 break;
350 case 15600:
351 *number_of_regions = 16;
352 *sample_rate_code = 24;
353 break;
354 case 16000:
355 *number_of_regions = 16;
356 *sample_rate_code = 25;
357 break;
358 case 16400:
359 *number_of_regions = 16;
360 *sample_rate_code = 26;
361 break;
362 case 16800:
363 *number_of_regions = 18;
364 *sample_rate_code = 4;
365 break;
366 case 17200:
367 *number_of_regions = 18;
368 *sample_rate_code = 27;
369 break;
370 case 17600:
371 *number_of_regions = 18;
372 *sample_rate_code = 28;
373 break;
374 case 18000:
375 *number_of_regions = 18;
376 *sample_rate_code = 29;
377 break;
378 case 18400:
379 *number_of_regions = 18;
380 *sample_rate_code = 30;
381 break;
382 case 18800:
383 *number_of_regions = 18;
384 *sample_rate_code = 31;
385 break;
386 case 19200:
387 *number_of_regions = 20;
388 *sample_rate_code = 5;
389 break;
390 case 19600:
391 *number_of_regions = 20;
392 *sample_rate_code = 32;
393 break;
394 case 20000:
395 *number_of_regions = 20;
396 *sample_rate_code = 33;
397 break;
398 case 20400:
399 *number_of_regions = 20;
400 *sample_rate_code = 34;
401 break;
402 case 20800:
403 *number_of_regions = 20;
404 *sample_rate_code = 35;
405 break;
406 case 21200:
407 *number_of_regions = 20;
408 *sample_rate_code = 36;
409 break;
410 case 21600:
411 *number_of_regions = 22;
412 *sample_rate_code = 6;
413 break;
414 case 22000:
415 *number_of_regions = 22;
416 *sample_rate_code = 37;
417 break;
418 case 22400:
419 *number_of_regions = 22;
420 *sample_rate_code = 38;
421 break;
422 case 22800:
423 *number_of_regions = 22;
424 *sample_rate_code = 39;
425 break;
426 case 23200:
427 *number_of_regions = 22;
428 *sample_rate_code = 40;
429 break;
430 case 23600:
431 *number_of_regions = 22;
432 *sample_rate_code = 41;
433 break;
434 case 24000:
435 *number_of_regions = 24;
436 *sample_rate_code = 7;
437 break;
438 case 24400:
439 *number_of_regions = 24;
440 *sample_rate_code = 42;
441 break;
442 case 24800:
443 *number_of_regions = 24;
444 *sample_rate_code = 43;
445 break;
446 case 25200:
447 *number_of_regions = 24;
448 *sample_rate_code = 44;
449 break;
450 case 25600:
451 *number_of_regions = 24;
452 *sample_rate_code = 45;
453 break;
454 case 26000:
455 *number_of_regions = 24;
456 *sample_rate_code = 46;
457 break;
458 case 26400:
459 *number_of_regions = 26;
460 *sample_rate_code = 8;
461 break;
462 case 26800:
463 *number_of_regions = 26;
464 *sample_rate_code = 47;
465 break;
466 case 27200:
467 *number_of_regions = 26;
468 *sample_rate_code = 48;
469 break;
470 case 27600:
471 *number_of_regions = 26;
472 *sample_rate_code = 49;
473 break;
474 case 28000:
475 *number_of_regions = 26;
476 *sample_rate_code = 50;
477 break;
478 case 28400:
479 *number_of_regions = 26;
480 *sample_rate_code = 51;
481 break;
482 case 28800:
483 *number_of_regions = 28;
484 *sample_rate_code = 9;
485 break;
486 case 29200:
487 *number_of_regions = 28;
488 *sample_rate_code = 52;
489 break;
490 case 29600:
491 *number_of_regions = 28;
492 *sample_rate_code = 53;
493 break;
494 case 30000:
495 *number_of_regions = 28;
496 *sample_rate_code = 54;
497 break;
498 case 30400:
499 *number_of_regions = 28;
500 *sample_rate_code = 55;
501 break;
502 case 30800:
503 *number_of_regions = 28;
504 *sample_rate_code = 56;
505 break;
506 case 31200:
507 *number_of_regions = 28;
508 *sample_rate_code = 10;
509 break;
510 case 31600:
511 *number_of_regions = 28;
512 *sample_rate_code = 57;
513 break;
514 case 32000:
515 *number_of_regions = 28;
516 *sample_rate_code = 58;
517 break;
518 default:
519 return 3;
520 break;
521 }
522 break;
523 default:
524 return 6;
525 }
526
527 *bits_per_frame = sample_rate / 50;
528 return 0;
529 }
530