1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3 // Copyright (C) 2015 Cherokees of Idaho.
4 //
5 // This file is part of GNU ccAudio2.
6 //
7 // GNU ccAudio2 is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // GNU ccAudio2 is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with GNU ccAudio2.  If not, see <http://www.gnu.org/licenses/>.
19 
20 #include <ucommon/ucommon.h>
21 #include <ccaudio2-config.h>
22 #include <math.h>
23 
24 extern "C" {
25     #if defined(HAVE_GSM_GSM_H)
26     #include <gsm/gsm.h>
27     #elif defined(HAVE_GSM_H)
28     #include <gsm.h>
29     #endif
30 }
31 
32 #ifdef  HAVE_SPEEX_SPEEX_H
33 #include <speex/speex.h>
34 #endif
35 
36 #include <ucommon/export.h>
37 #include <ccaudio2.h>
38 
39 #ifdef  HAVE_PTHREAD_H
40 #include <pthread.h>
41 #endif
42 
43 #ifndef M_PI
44 #define M_PI    3.14159265358979323846
45 #endif
46 
47 namespace ucommon {
48 
49 static LinkedObject *first = NULL;
50 
AudioCodec(const char * n,Encoding e)51 AudioCodec::AudioCodec(const char *n, Encoding e) :
52 LinkedObject(&first)
53 {
54     encoding = e;
55     name = n;
56     first = this;
57 
58     info.clear();
59     info.format = raw;
60     info.encoding = e;
61 }
62 
AudioCodec()63 AudioCodec::AudioCodec()
64 {
65     name = NULL;
66 
67     info.clear();
68     info.format = raw;
69 }
70 
release(AudioCodec * codec)71 void AudioCodec::release(AudioCodec *codec)
72 {
73     if(!codec->name)
74         delete codec;
75 }
76 
begin(void)77 AudioCodec *AudioCodec::begin(void)
78 {
79     return (AudioCodec *)first;
80 }
81 
get(Encoding e,const char * format)82 AudioCodec *AudioCodec::get(Encoding e, const char *format)
83 {
84     linked_pointer<AudioCodec> codec = first;
85 
86     while(is(codec)) {
87         if(e == codec->encoding)
88             break;
89         codec.next();
90     }
91 
92     if(is(codec) && format)
93         return codec->getByFormat(format);
94 
95     return *codec;
96 }
97 
get(Info & info)98 AudioCodec *AudioCodec::get(Info &info)
99 {
100     linked_pointer<AudioCodec> codec = first;
101 
102     while(is(codec)) {
103         if(info.encoding == codec->encoding)
104             break;
105         codec.next();
106     }
107 
108     if(is(codec))
109         return codec->getByInfo(info);
110 
111     return *codec;
112 }
113 
is_silent(Level hint,void * data,unsigned samples)114 bool AudioCodec::is_silent(Level hint, void *data, unsigned samples)
115 {
116     Level power = impulse(data, samples);
117 
118     if(power < 0)
119         return true;
120 
121     if(power > hint)
122         return false;
123 
124     return true;
125 }
126 
impulse(void * data,unsigned samples)127 Audio::Level AudioCodec::impulse(void *data, unsigned samples)
128 {
129     unsigned long sum = 0;
130     Linear ldata = new Sample[samples];
131     Linear lptr = ldata;
132     long count = decode(ldata, data, samples);
133 
134     samples = count;
135     while(samples--) {
136         if(*ldata < 0)
137             sum -= *(ldata++);
138         else
139             sum += *(ldata++);
140     }
141 
142     delete[] lptr;
143     if(count)
144         return (Level)(sum / count);
145     else
146         return 0;
147 }
148 
peak(void * data,unsigned samples)149 Audio::Level AudioCodec::peak(void *data, unsigned samples)
150 {
151     Level max = 0, value;
152     Linear ldata = new Sample[samples];
153     Linear lptr = ldata;
154     long count = decode(ldata, data, samples);
155 
156     samples = count;
157     while(samples--) {
158         value = *(ldata++);
159         if(value < 0)
160             value = -value;
161         if(value > max)
162             max = value;
163     }
164 
165     delete[] lptr;
166     return max;
167 }
168 
getEstimated(void)169 unsigned AudioCodec::getEstimated(void)
170 {
171     return info.framesize;
172 }
173 
getRequired(void)174 unsigned AudioCodec::getRequired(void)
175 {
176     return info.framecount;
177 }
178 
encodeBuffered(Linear buffer,Encoded source,unsigned samples)179 unsigned AudioCodec::encodeBuffered(Linear buffer, Encoded source, unsigned samples)
180 {
181     return encode(buffer, source, samples);
182 }
183 
decodeBuffered(Linear buffer,Encoded source,unsigned bytes)184 unsigned AudioCodec::decodeBuffered(Linear buffer, Encoded source, unsigned bytes)
185 {
186     return decode(buffer, source, toSamples(info, bytes));
187 }
188 
getPacket(Encoded packet,Encoded data,unsigned bytes)189 unsigned AudioCodec::getPacket(Encoded packet, Encoded data, unsigned bytes)
190 {
191     if(bytes != info.framesize)
192         return 0;
193 
194     memcpy(packet, data, bytes);
195     return bytes;
196 }
197 
198 // g711 codecs initialized in static linkage...
199 
200 static class __LOCAL g711u : public AudioCodec
201 {
202 public:
203     g711u();
204 
205 private:
206     __DELETE_COPY(g711u);
207 
208     unsigned encode(Linear buffer, void *source, unsigned lsamples) __FINAL;
209     unsigned decode(Linear buffer, void *dest, unsigned lsamples) __FINAL;
210     Level impulse(void *buffer, unsigned samples) __FINAL;
211     Level peak(void *buffer, unsigned samples) __FINAL;
212 
213 } g711u;
214 
215 static class __LOCAL g711a : public AudioCodec
216 {
217 public:
218     g711a();
219 
220 private:
221     __DELETE_COPY(g711a);
222 
223     unsigned encode(Linear buffer, void *source, unsigned lsamples) __FINAL;
224     unsigned decode(Linear buffer, void *dest, unsigned lsamples) __FINAL;
225     Level impulse(void *buffer, unsigned samples) __FINAL;
226     Level peak(void *buffer, unsigned samples) __FINAL;
227 
228 } g711a;
229 
g711u()230 g711u::g711u() : AudioCodec("g.711", mulawAudio)
231 {
232     info.framesize = 1;
233     info.framecount = 1;
234     info.rate = 8000;
235     info.bitrate = 64000;
236     info.annotation = (char *)"mu-law";
237 }
238 
g711a()239 g711a::g711a() : AudioCodec("g.711", alawAudio)
240 {
241     info.framesize = 1;
242     info.framecount = 1;
243     info.bitrate = 64000;
244     info.rate = 8000;
245     info.annotation = (char *)"a-law";
246 }
247 
248 static unsigned ullevels[128] =
249 {
250             32124,   31100,   30076,   29052,   28028,
251     27004,   25980,   24956,   23932,   22908,   21884,   20860,
252     19836,   18812,   17788,   16764,   15996,   15484,   14972,
253     14460,   13948,   13436,   12924,   12412,   11900,   11388,
254     10876,   10364,    9852,    9340,    8828,    8316,    7932,
255     7676,    7420,    7164,    6908,    6652,    6396,    6140,
256     5884,    5628,    5372,    5116,    4860,    4604,    4348,
257     4092,    3900,    3772,    3644,    3516,    3388,    3260,
258     3132,    3004,    2876,    2748,    2620,    2492,    2364,
259     2236,    2108,    1980,    1884,    1820,    1756,    1692,
260     1628,    1564,    1500,    1436,    1372,    1308,    1244,
261     1180,    1116,    1052,     988,     924,     876,     844,
262     812,     780,     748,     716,     684,     652,     620,
263     588,     556,     524,     492,     460,     428,     396,
264     372,     356,     340,     324,     308,     292,     276,
265     260,     244,     228,     212,     196,     180,     164,
266     148,     132,     120,     112,     104,      96,      88,
267     80,      72,      64,      56,      48,      40,      32,
268     24,      16,       8,       0
269 };
270 
impulse(void * data,unsigned samples)271 Audio::Level g711u::impulse(void *data, unsigned samples)
272 {
273     unsigned long count = samples;
274     unsigned long sum = 0;
275 
276     if(!samples)
277         samples = count = 160;
278 
279     unsigned char *dp = (unsigned char *)data;
280 
281     while(samples--)
282         sum += (ullevels[*(dp++) & 0x7f]);
283 
284     return (Level)(sum / count);
285 }
286 
peak(void * data,unsigned samples)287 Audio::Level g711u::peak(void *data, unsigned samples)
288 {
289     unsigned long count = samples;
290     Level max = 0, value;
291 
292     if(!samples)
293         samples = count = 160;
294 
295     unsigned char *dp = (unsigned char *)data;
296 
297     while(samples--) {
298         value = ullevels[*(dp++) & 0x7f];
299         if(value > max)
300             max = value;
301     }
302     return max;
303 }
304 
encode(Linear buffer,void * dest,unsigned lsamples)305 unsigned g711u::encode(Linear buffer, void *dest, unsigned lsamples)
306 {
307     static int ulaw[256] = {
308         0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
309         4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
310         5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
311         5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
312         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
313         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
314         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
315         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
316         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
317         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
318         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
319         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
320         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
321         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
322         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
323         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
324 
325     Sample sample;
326     int sign, exponent, mantissa, retval;
327     unsigned char *d = (unsigned char *)dest;
328     unsigned count;
329 
330     count = lsamples;
331 
332     while(lsamples--) {
333         sample = *(buffer++);
334                 sign = (sample >> 8) & 0x80;
335             if(sign != 0) sample = -sample;
336             sample += 0x84;
337             exponent = ulaw[(sample >> 7) & 0xff];
338             mantissa = (sample >> (exponent + 3)) & 0x0f;
339             retval = ~(sign | (exponent << 4) | mantissa);
340             if(!retval)
341             retval = 0x02;
342         *(d++) = (unsigned char)retval;
343     }
344     return count;
345 }
346 
decode(Linear buffer,void * source,unsigned lsamples)347 unsigned g711u::decode(Linear buffer, void *source, unsigned lsamples)
348 {
349     unsigned char *src = (unsigned char *)source;
350     unsigned count;
351 
352     count = lsamples;
353 
354     static Sample values[256] =
355     {
356     -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
357     -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
358     -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
359     -11900, -11388, -10876, -10364,  -9852,  -9340,  -8828,  -8316,
360      -7932,  -7676,  -7420,  -7164,  -6908,  -6652,  -6396,  -6140,
361      -5884,  -5628,  -5372,  -5116,  -4860,  -4604,  -4348,  -4092,
362      -3900,  -3772,  -3644,  -3516,  -3388,  -3260,  -3132,  -3004,
363      -2876,  -2748,  -2620,  -2492,  -2364,  -2236,  -2108,  -1980,
364      -1884,  -1820,  -1756,  -1692,  -1628,  -1564,  -1500,  -1436,
365      -1372,  -1308,  -1244,  -1180,  -1116,  -1052,   -988,   -924,
366       -876,   -844,   -812,   -780,   -748,   -716,   -684,   -652,
367       -620,   -588,   -556,   -524,   -492,   -460,   -428,   -396,
368       -372,   -356,   -340,   -324,   -308,   -292,   -276,   -260,
369       -244,   -228,   -212,   -196,   -180,   -164,   -148,   -132,
370       -120,   -112,   -104,    -96,    -88,    -80,    -72,    -64,
371        -56,    -48,    -40,    -32,    -24,    -16,     -8,      0,
372      32124,  31100,  30076,  29052,  28028,  27004,  25980,  24956,
373      23932,  22908,  21884,  20860,  19836,  18812,  17788,  16764,
374      15996,  15484,  14972,  14460,  13948,  13436,  12924,  12412,
375      11900,  11388,  10876,  10364,   9852,   9340,   8828,   8316,
376       7932,   7676,   7420,   7164,   6908,   6652,   6396,   6140,
377       5884,   5628,   5372,   5116,   4860,   4604,   4348,   4092,
378       3900,   3772,   3644,   3516,   3388,   3260,   3132,   3004,
379       2876,   2748,   2620,   2492,   2364,   2236,   2108,   1980,
380       1884,   1820,   1756,   1692,   1628,   1564,   1500,   1436,
381       1372,   1308,   1244,   1180,   1116,   1052,    988,    924,
382        876,    844,    812,    780,    748,    716,    684,    652,
383        620,    588,    556,    524,    492,    460,    428,    396,
384        372,    356,    340,    324,    308,    292,    276,    260,
385        244,    228,    212,    196,    180,    164,    148,    132,
386        120,    112,    104,     96,     88,     80,     72,     64,
387         56,     48,     40,     32,     24,     16,      8,      0
388     };
389 
390     while(lsamples--)
391         *(buffer++) = values[*(src++)];
392 
393     return count;
394 }
395 
396 #define AMI_MASK    0x55
397 
encode(Linear buffer,void * dest,unsigned lsamples)398 unsigned g711a::encode(Linear buffer, void *dest, unsigned lsamples)
399 {
400     int mask, seg, pcm_val;
401     unsigned count;
402     unsigned char *d = (unsigned char *)dest;
403 
404     static int seg_end[] = {
405         0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
406 
407     count = lsamples;
408 
409     while(lsamples--) {
410         pcm_val = *(buffer++);
411         if(pcm_val >= 0)
412             mask = AMI_MASK | 0x80;
413         else {
414             mask = AMI_MASK;
415             pcm_val = -pcm_val;
416         }
417         for(seg = 0; seg < 8; seg++)
418         {
419             if(pcm_val <= seg_end[seg])
420                 break;
421         }
422         *(d++) = ((seg << 4) | ((pcm_val >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask;
423     }
424     return count;
425 }
426 
427 static unsigned allevels[128] =
428 {
429     5504,   5248,   6016,   5760,   4480,   4224,   4992,   4736,
430     7552,   7296,   8064,   7808,   6528,   6272,   7040,   6784,
431     2752,   2624,   3008,   2880,   2240,   2112,   2496,   2368,
432     3776,   3648,   4032,   3904,   3264,   3136,   3520,   3392,
433     22016,  20992,  24064,  23040,  17920,  16896,  19968,  18944,
434     30208,  29184,  32256,  31232,  26112,  25088,  28160,  27136,
435     11008,  10496,  12032,  11520,   8960,   8448,   9984,   9472,
436     15104,  14592,  16128,  15616,  13056,  12544,  14080,  13568,
437     344,    328,    376,    360,    280,    264,    312,    296,
438     472,    456,    504,    488,    408,    392,    440,    424,
439     88,     72,    120,    104,     24,      8,     56,     40,
440     216,    200,    248,    232,    152,    136,    184,    168,
441     1376,   1312,   1504,   1440,   1120,   1056,   1248,   1184,
442     1888,   1824,   2016,   1952,   1632,   1568,   1760,   1696,
443     688,    656,    752,    720,    560,    528,    624,    592,
444     944,    912,   1008,    976,    816,    784,    880,    848
445 };
446 
impulse(void * data,unsigned samples)447 Audio::Level g711a::impulse(void *data, unsigned samples)
448 {
449     unsigned long count = samples;
450     unsigned long sum = 0;
451 
452     if(!samples)
453         samples = count = 160;
454 
455 
456 
457     unsigned char *dp = (unsigned char *)data;
458 
459     while(samples--)
460         sum += (allevels[*(dp++) & 0x7f]);
461 
462     return (Level)(sum / count);
463 }
464 
peak(void * data,unsigned samples)465 Audio::Level g711a::peak(void *data, unsigned samples)
466 {
467     unsigned long count = samples;
468     Level max = 0, value;
469 
470     if(!samples)
471         samples = count = 160;
472 
473     unsigned char *dp = (unsigned char *)data;
474 
475     while(samples--) {
476         value = allevels[*(dp++) & 0x7f];
477         if(value > max)
478             max = value;
479     }
480     return max;
481 }
482 
decode(Linear buffer,void * source,unsigned lsamples)483 unsigned g711a::decode(Linear buffer, void *source, unsigned lsamples)
484 {
485     unsigned char *src = (unsigned char *)source;
486     unsigned count;
487 
488     static Sample values[256] =
489     {
490         -5504,  -5248,  -6016,  -5760,  -4480,  -4224,  -4992,  -4736,
491         -7552,  -7296,  -8064,  -7808,  -6528,  -6272,  -7040,  -6784,
492         -2752,  -2624,  -3008,  -2880,  -2240,  -2112,  -2496,  -2368,
493         -3776,  -3648,  -4032,  -3904,  -3264,  -3136,  -3520,  -3392,
494        -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
495        -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
496        -11008, -10496, -12032, -11520,  -8960,  -8448,  -9984,  -9472,
497        -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
498          -344,   -328,   -376,   -360,   -280,   -264,   -312,   -296,
499          -472,   -456,   -504,   -488,   -408,   -392,   -440,   -424,
500           -88,    -72,   -120,   -104,    -24,     -8,    -56,    -40,
501          -216,   -200,   -248,   -232,   -152,   -136,   -184,   -168,
502         -1376,  -1312,  -1504,  -1440,  -1120,  -1056,  -1248,  -1184,
503         -1888,  -1824,  -2016,  -1952,  -1632,  -1568,  -1760,  -1696,
504          -688,   -656,   -752,   -720,   -560,   -528,   -624,   -592,
505          -944,   -912,  -1008,   -976,   -816,   -784,   -880,   -848,
506          5504,   5248,   6016,   5760,   4480,   4224,   4992,   4736,
507          7552,   7296,   8064,   7808,   6528,   6272,   7040,   6784,
508          2752,   2624,   3008,   2880,   2240,   2112,   2496,   2368,
509          3776,   3648,   4032,   3904,   3264,   3136,   3520,   3392,
510         22016,  20992,  24064,  23040,  17920,  16896,  19968,  18944,
511         30208,  29184,  32256,  31232,  26112,  25088,  28160,  27136,
512         11008,  10496,  12032,  11520,   8960,   8448,   9984,   9472,
513         15104,  14592,  16128,  15616,  13056,  12544,  14080,  13568,
514           344,    328,    376,    360,    280,    264,    312,    296,
515           472,    456,    504,    488,    408,    392,    440,    424,
516            88,     72,    120,    104,     24,      8,     56,     40,
517           216,    200,    248,    232,    152,    136,    184,    168,
518          1376,   1312,   1504,   1440,   1120,   1056,   1248,   1184,
519          1888,   1824,   2016,   1952,   1632,   1568,   1760,   1696,
520           688,    656,    752,    720,    560,    528,    624,    592,
521           944,    912,   1008,    976,    816,    784,    880,    848
522     };
523 
524     count = lsamples;
525 
526     while(lsamples--)
527         *(buffer++) = values[*(src++)];
528 
529     return count;
530 }
531 
532 // adpcm codec
533 
534 static short power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
535             0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
536 
537 typedef struct state {
538     long yl;
539     short yu;
540     short dms;
541     short dml;
542     short ap;
543     short a[2];
544     short b[6];
545     short pk[2];
546     short dq[6];
547     short sr[2];
548     char td;
549 }   state_t;
550 
quan(int val,short * table,int size)551 static int quan(
552     int     val,
553     short       *table,
554     int     size)
555 {
556     int     i;
557 
558     for (i = 0; i < size; i++)
559         if (val < *table++)
560             break;
561     return (i);
562 }
563 
quantize(int d,int y,short * table,int size)564 static int quantize(
565     int     d,  /* Raw difference signal sample */
566     int     y,  /* Step size multiplier */
567     short       *table, /* quantization table */
568     int     size)   /* table size of short integers */
569 {
570     short       dqm;    /* Magnitude of 'd' */
571     short       exp;    /* Integer part of base 2 log of 'd' */
572     short       mant;   /* Fractional part of base 2 log */
573     short       dl; /* Log of magnitude of 'd' */
574     short       dln;    /* Step size scale factor normalized log */
575     int     i;
576 
577     /*
578      * LOG
579      *
580      * Compute base 2 log of 'd', and store in 'dl'.
581      */
582     dqm = abs(d);
583     exp = quan(dqm >> 1, power2, 15);
584     mant = ((dqm << 7) >> exp) & 0x7F;  /* Fractional portion. */
585     dl = (exp << 7) + mant;
586 
587     /*
588      * SUBTB
589      *
590      * "Divide" by step size multiplier.
591      */
592     dln = dl - (y >> 2);
593 
594     /*
595      * QUAN
596      *
597      * Obtain codword i for 'd'.
598      */
599     i = quan(dln, table, size);
600     if (d < 0)          /* take 1's complement of i */
601         return ((size << 1) + 1 - i);
602     else if (i == 0)        /* take 1's complement of 0 */
603         return ((size << 1) + 1); /* new in 1988 */
604     else
605         return (i);
606 }
607 
fmult(int an,int srn)608 static int fmult(
609     int     an,
610     int     srn)
611 {
612     short       anmag, anexp, anmant;
613     short       wanexp, wanmant;
614     short       retval;
615 
616     anmag = (an > 0) ? an : ((-an) & 0x1FFF);
617     anexp = quan(anmag, power2, 15) - 6;
618     anmant = (anmag == 0) ? 32 :
619         (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
620     wanexp = anexp + ((srn >> 6) & 0xF) - 13;
621 
622     wanmant = (anmant * (srn & 077) + 0x30) >> 4;
623     retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
624         (wanmant >> -wanexp);
625 
626     return (((an ^ srn) < 0) ? -retval : retval);
627 }
628 
reconstruct(int sign,int dqln,int y)629 static int reconstruct(
630     int     sign,   /* 0 for non-negative value */
631     int     dqln,   /* G.72x codeword */
632     int     y)  /* Step size multiplier */
633 {
634     short       dql;    /* Log of 'dq' magnitude */
635     short       dex;    /* Integer part of log */
636     short       dqt;
637     short       dq; /* Reconstructed difference signal sample */
638 
639     dql = dqln + (y >> 2);  /* ADDA */
640 
641     if (dql < 0) {
642         return ((sign) ? -0x8000 : 0);
643     } else {        /* ANTILOG */
644         dex = (dql >> 7) & 15;
645         dqt = 128 + (dql & 127);
646         dq = (dqt << 7) >> (14 - dex);
647         return ((sign) ? (dq - 0x8000) : dq);
648     }
649 }
650 
update(int code_size,int y,int wi,int fi,int dq,int sr,int dqsez,state_t * state_ptr)651 static void update(
652     int     code_size,  /* distinguish 723_40 with others */
653     int     y,      /* quantizer step size */
654     int     wi,     /* scale factor multiplier */
655     int     fi,     /* for long/short term energies */
656     int     dq,     /* quantized prediction difference */
657     int     sr,     /* reconstructed signal */
658     int     dqsez,      /* difference from 2-pole predictor */
659     state_t *state_ptr)     /* coder state pointer */
660 {
661     int     cnt;
662     short       mag, exp;   /* Adaptive predictor, FLOAT A */
663     short       a2p = 0;        /* LIMC */
664     short       a1ul;       /* UPA1 */
665     short       pks1;       /* UPA2 */
666     short       fa1;
667     char        tr;     /* tone/transition detector */
668     short       ylint, thr2, dqthr;
669     short       ylfrac, thr1;
670     short       pk0;
671 
672     pk0 = (dqsez < 0) ? 1 : 0;  /* needed in updating predictor poles */
673 
674     mag = dq & 0x7FFF;      /* prediction difference magnitude */
675     /* TRANS */
676     ylint = (short)(state_ptr->yl >> 15);   /* exponent part of yl */
677     ylfrac = (state_ptr->yl >> 10) & 0x1F;  /* fractional part of yl */
678     thr1 = (32 + ylfrac) << ylint;      /* threshold */
679     thr2 = (short)((ylint > 9) ? 31 << 10 : thr1);  /* limit thr2 to 31 << 10 */
680     dqthr = (thr2 + (thr2 >> 1)) >> 1;  /* dqthr = 0.75 * thr2 */
681     if (state_ptr->td == 0)     /* signal supposed voice */
682         tr = 0;
683     else if (mag <= dqthr)      /* supposed data, but small mag */
684         tr = 0;         /* treated as voice */
685     else                /* signal is data (modem) */
686         tr = 1;
687 
688     /*
689      * Quantizer scale factor adaptation.
690      */
691 
692     /* FUNCTW & FILTD & DELAY */
693     /* update non-steady state step size multiplier */
694     state_ptr->yu = y + ((wi - y) >> 5);
695 
696     /* LIMB */
697     if (state_ptr->yu < 544)    /* 544 <= yu <= 5120 */
698         state_ptr->yu = 544;
699     else if (state_ptr->yu > 5120)
700         state_ptr->yu = 5120;
701 
702     /* FILTE & DELAY */
703     /* update steady state step size multiplier */
704     state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
705 
706     /*
707      * Adaptive predictor coefficients.
708      */
709     if (tr == 1) {          /* reset a's and b's for modem signal */
710         state_ptr->a[0] = 0;
711         state_ptr->a[1] = 0;
712         state_ptr->b[0] = 0;
713         state_ptr->b[1] = 0;
714         state_ptr->b[2] = 0;
715         state_ptr->b[3] = 0;
716         state_ptr->b[4] = 0;
717         state_ptr->b[5] = 0;
718     } else {            /* update a's and b's */
719         pks1 = pk0 ^ state_ptr->pk[0];      /* UPA2 */
720 
721         /* update predictor pole a[1] */
722         a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
723         if (dqsez != 0) {
724             fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
725             if (fa1 < -8191)    /* a2p = function of fa1 */
726                 a2p -= 0x100;
727             else if (fa1 > 8191)
728                 a2p += 0xFF;
729             else
730                 a2p += fa1 >> 5;
731 
732             if (pk0 ^ state_ptr->pk[1])
733                 /* LIMC */
734                 if (a2p <= -12160)
735                     a2p = -12288;
736                 else if (a2p >= 12416)
737                     a2p = 12288;
738                 else
739                     a2p -= 0x80;
740             else if (a2p <= -12416)
741                 a2p = -12288;
742             else if (a2p >= 12160)
743                 a2p = 12288;
744             else
745                 a2p += 0x80;
746         }
747 
748         /* TRIGB & DELAY */
749         state_ptr->a[1] = a2p;
750 
751         /* UPA1 */
752         /* update predictor pole a[0] */
753         state_ptr->a[0] -= state_ptr->a[0] >> 8;
754         if (dqsez != 0) {
755             if (pks1 == 0)
756                 state_ptr->a[0] += 192;
757             else
758                 state_ptr->a[0] -= 192;
759         }
760 
761         /* LIMD */
762         a1ul = 15360 - a2p;
763         if (state_ptr->a[0] < -a1ul)
764             state_ptr->a[0] = -a1ul;
765         else if (state_ptr->a[0] > a1ul)
766             state_ptr->a[0] = a1ul;
767 
768         /* UPB : update predictor zeros b[6] */
769         for (cnt = 0; cnt < 6; cnt++) {
770             if (code_size == 5)     /* for 40Kbps G.723 */
771                 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
772             else            /* for G.721 and 24Kbps G.723 */
773                 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
774             if (dq & 0x7FFF) {          /* XOR */
775                 if ((dq ^ state_ptr->dq[cnt]) >= 0)
776                     state_ptr->b[cnt] += 128;
777                 else
778                     state_ptr->b[cnt] -= 128;
779             }
780         }
781     }
782 
783     for (cnt = 5; cnt > 0; cnt--)
784         state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
785     /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
786     if (mag == 0) {
787         state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
788     } else {
789         exp = quan(mag, power2, 15);
790         state_ptr->dq[0] = (dq >= 0) ?
791             (exp << 6) + ((mag << 6) >> exp) :
792             (exp << 6) + ((mag << 6) >> exp) - 0x400;
793     }
794 
795     state_ptr->sr[1] = state_ptr->sr[0];
796     /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
797     if (sr == 0) {
798         state_ptr->sr[0] = 0x20;
799     } else if (sr > 0) {
800         exp = quan(sr, power2, 15);
801         state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
802     } else if (sr > -32768) {
803         mag = -sr;
804         exp = quan(mag, power2, 15);
805         state_ptr->sr[0] =  (exp << 6) + ((mag << 6) >> exp) - 0x400;
806     } else
807         state_ptr->sr[0] = (short)0xFC20;
808 
809     /* DELAY A */
810     state_ptr->pk[1] = state_ptr->pk[0];
811     state_ptr->pk[0] = pk0;
812 
813     /* TONE */
814     if (tr == 1)        /* this sample has been treated as data */
815         state_ptr->td = 0;  /* next one will be treated as voice */
816     else if (a2p < -11776)  /* small sample-to-sample correlation */
817         state_ptr->td = 1;  /* signal may be data */
818     else                /* signal is voice */
819         state_ptr->td = 0;
820 
821     /*
822      * Adaptation speed control.
823      */
824     state_ptr->dms += (fi - state_ptr->dms) >> 5;       /* FILTA */
825     state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);  /* FILTB */
826 
827     if (tr == 1)
828         state_ptr->ap = 256;
829     else if (y < 1536)                  /* SUBTC */
830         state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
831     else if (state_ptr->td == 1)
832         state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
833     else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
834         (state_ptr->dml >> 3))
835         state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
836     else
837         state_ptr->ap += (-state_ptr->ap) >> 4;
838 }
839 
predictor_zero(state_t * state_ptr)840 static int predictor_zero(
841     state_t *state_ptr)
842 {
843     int     i;
844     int     sezi;
845 
846     sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
847     for (i = 1; i < 6; i++)         /* ACCUM */
848         sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
849     return (sezi);
850 }
851 
predictor_pole(state_t * state_ptr)852 static int predictor_pole(
853     state_t *state_ptr)
854 {
855     return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
856         fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
857 }
858 
step_size(state_t * state_ptr)859 static int step_size(
860     state_t *state_ptr)
861 {
862     int     y;
863     int     dif;
864     int     al;
865 
866     if (state_ptr->ap >= 256)
867         return (state_ptr->yu);
868     else {
869         y = state_ptr->yl >> 6;
870         dif = state_ptr->yu - y;
871         al = state_ptr->ap >> 2;
872         if (dif > 0)
873             y += (dif * al) >> 6;
874         else if (dif < 0)
875             y += (dif * al + 0x3F) >> 6;
876         return (y);
877     }
878 }
879 
880 static class __LOCAL g721Codec : private AudioCodec
881 {
882 private:
883     __DELETE_COPY(g721Codec);
884 
885     static short    _dqlntab[16];
886     static short    _witab[16];
887     static short    _fitab[16];
888     static short qtab_721[7];
889 
890     state_t encode_state, decode_state;
891 
892     AudioCodec *getByInfo(Info &info) __FINAL;
893     AudioCodec *getByFormat(const char *format) __FINAL;
894 
895     unsigned decode(Linear buffer, void *from, unsigned lsamples) __FINAL;
896     unsigned encode(Linear buffer, void *dest, unsigned lsamples) __FINAL;
897     short coder(state_t *state, int nib);
898     unsigned char encoder(short sl, state_t *state);
899 
900 public:
901     g721Codec(const char *id, Encoding e);
902     g721Codec();
903     ~g721Codec();
904 } g723_4("adpcm", Audio::g721ADPCM);
905 
906 static class __LOCAL g723_3Codec : private AudioCodec
907 {
908 private:
909     __DELETE_COPY(g723_3Codec);
910 
911     static short    _dqlntab[8];
912     static short    _witab[8];
913     static short    _fitab[8];
914     static short qtab_723_24[3];
915 
916     state_t encode_state, decode_state;
917 
918     AudioCodec *getByInfo(Info &info) __FINAL;
919     AudioCodec *getByFormat(const char *format) __FINAL;
920 
921     unsigned decode(Linear buffer, void *from, unsigned lsamples) __FINAL;
922     unsigned encode(Linear buffer, void *dest, unsigned lsamples) __FINAL;
923     short coder(state_t *state, int nib);
924     unsigned char encoder(short sl, state_t *state);
925 
926 public:
927     g723_3Codec(const char *id, Encoding e);
928     g723_3Codec();
929     ~g723_3Codec();
930 } g723_3("g.723", Audio::g723_3bit);
931 
932 static class __LOCAL g723_5Codec : private AudioCodec
933 {
934 private:
935     __DELETE_COPY(g723_5Codec);
936 
937     static short    _dqlntab[32];
938     static short    _witab[32];
939     static short    _fitab[32];
940     static short qtab_723_40[15];
941 
942     state_t encode_state, decode_state;
943 
944     AudioCodec *getByInfo(Info &info)  __FINAL;
945     AudioCodec *getByFormat(const char *format)  __FINAL;
946 
947     unsigned decode(Linear buffer, void *from, unsigned lsamples) __FINAL;
948     unsigned encode(Linear buffer, void *dest, unsigned lsamples) __FINAL;
949     short coder(state_t *state, int nib);
950     unsigned char encoder(short sl, state_t *state);
951 
952 public:
953     g723_5Codec(const char *id, Encoding e);
954     g723_5Codec();
955     ~g723_5Codec();
956 } g723_5("g.723", Audio::g723_5bit);
957 
958 class __LOCAL g723_2Codec : public AudioCodec
959 {
960 private:
961     __DELETE_COPY(g723_2Codec);
962 
963     static short    _dqlntab[4];
964     static short    _witab[4];
965     static short    _fitab[4];
966     static short qtab_723_16[1];
967 
968     state_t encode_state, decode_state;
969 
970     AudioCodec *getByInfo(Info &info) __FINAL;
971     AudioCodec *getByFormat(const char *format) __FINAL;
972 
973     unsigned decode(Linear buffer, void *from, unsigned lsamples) __FINAL;
974     unsigned encode(Linear buffer, void *dest, unsigned lsamples) __FINAL;
975     short coder(state_t *state, int nib);
976     unsigned char encoder(short sl, state_t *state);
977 
978 public:
979     g723_2Codec(const char *id, Encoding e);
980     g723_2Codec();
981     ~g723_2Codec();
982 } g723_2("g.723", Audio::g723_2bit);
983 
984 short g723_2Codec::_dqlntab[4] = { 116, 365, 365, 116};
985 short g723_2Codec::_witab[4] = {-704, 14048, 14048, -704};
986 short g723_2Codec::_fitab[4] = {0, 0xE00, 0xE00, 0};
987 short g723_2Codec::qtab_723_16[1] = {261};
988 
989 short g723_3Codec::_dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
990 short g723_3Codec::_witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
991 short g723_3Codec::_fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
992 short g723_3Codec::qtab_723_24[3] = {8, 218, 331};
993 
994 short g723_5Codec::_dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318,
995                 358, 395, 429, 459, 488, 514, 539, 566,
996                 566, 539, 514, 488, 459, 429, 395, 358,
997                 318, 274, 224, 169, 104, 28, -66, -2048};
998 
999 short g723_5Codec::_witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200,
1000             4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
1001             22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
1002             3200, 1856, 1312, 1280, 1248, 768, 448, 448};
1003 
1004 short g723_5Codec::_fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
1005             0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
1006             0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
1007             0x200, 0x200, 0x200, 0, 0, 0, 0, 0};
1008 
1009 short g723_5Codec::qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339,
1010                 378, 413, 445, 475, 502, 528, 553};
1011 
1012 
1013 short g721Codec::_dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
1014                 425, 373, 323, 273, 213, 135, 4, -2048};
1015 short g721Codec::_witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
1016                 1122, 355, 198, 112, 64, 41, 18, -12};
1017 short g721Codec::_fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
1018                 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
1019 short g721Codec::qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
1020 
g723_3Codec()1021 g723_3Codec::g723_3Codec() : AudioCodec()
1022 {
1023     unsigned pos;
1024 
1025     info.framesize = 3;
1026     info.framecount = 8;
1027     info.bitrate = 24000;
1028     info.encoding = g723_3bit;
1029     info.annotation = (char *)"g.723/3";
1030     info.rate = 8000;
1031     memset(&encode_state, 0, sizeof(encode_state));
1032     memset(&decode_state, 0, sizeof(decode_state));
1033     encode_state.yl = decode_state.yl = 34816;
1034     encode_state.yu = decode_state.yu = 544;
1035     encode_state.sr[0] = encode_state.sr[1] = decode_state.sr[0] = decode_state.sr[1] = 32;
1036 
1037     for(pos = 0; pos < 6; ++pos)
1038         encode_state.dq[pos] = decode_state.dq[pos] = 32;
1039 }
1040 
g723_3Codec(const char * id,Encoding e)1041 g723_3Codec::g723_3Codec(const char *id, Encoding e) : AudioCodec(id, e)
1042 {
1043     info.framesize = 3;
1044     info.framecount = 8;
1045     info.bitrate = 24000;
1046     info.rate = 8000;
1047     info.annotation = (char *)"g.723/3";
1048 }
1049 
~g723_3Codec()1050 g723_3Codec::~g723_3Codec()
1051 {}
1052 
1053 
encoder(short sl,state_t * state_ptr)1054 unsigned char g723_3Codec::encoder(short sl, state_t *state_ptr)
1055 {
1056     short sezi, se, sez, sei;
1057     short d, sr, y, dqsez, dq, i;
1058 
1059     sl >>= 2;
1060 
1061     sezi = predictor_zero(state_ptr);
1062     sez = sezi >> 1;
1063     sei = sezi + predictor_pole(state_ptr);
1064     se = sei >> 1;                  /* se = estimated signal */
1065 
1066     d = sl - se;                    /* d = estimation diff. */
1067 
1068     /* quantize prediction difference d */
1069     y = step_size(state_ptr);       /* quantizer step size */
1070     i = quantize(d, y, qtab_723_24, 3);     /* i = ADPCM code */
1071     dq = reconstruct(i & 4, _dqlntab[i], y); /* quantized diff. */
1072 
1073     sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
1074     dqsez = sr + sez - se;          /* pole prediction diff. */
1075 
1076     update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1077     return (unsigned char)(i);
1078 }
1079 
coder(state_t * state_ptr,int i)1080 short g723_3Codec::coder(state_t *state_ptr, int i)
1081 {
1082     short sezi, sei, sez, se;
1083     short y, sr, dq, dqsez;
1084 
1085     i &= 0x07;                      /* mask to get proper bits */
1086     sezi = predictor_zero(state_ptr);
1087     sez = sezi >> 1;
1088     sei = sezi + predictor_pole(state_ptr);
1089     se = sei >> 1;                  /* se = estimated signal */
1090 
1091     y = step_size(state_ptr);       /* adaptive quantizer step size */
1092     dq = reconstruct(i & 0x04, _dqlntab[i], y); /* unquantize pred diff */
1093 
1094     sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
1095 
1096     dqsez = sr - se + sez;                  /* pole prediction diff. */
1097 
1098     update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1099 
1100     return sr << 2;
1101 }
1102 
encode(Linear buffer,void * coded,unsigned lsamples)1103 unsigned g723_3Codec::encode(Linear buffer, void *coded, unsigned lsamples)
1104 {
1105     unsigned count = (lsamples / 8);
1106     Encoded dest = (Encoded)coded;
1107     unsigned i, data, byte, bits;
1108 
1109     while(count--) {
1110         bits = 0;
1111         data = 0;
1112         for(i = 0; i < 8; ++i)
1113         {
1114             byte = encoder(*(buffer++), &encode_state);
1115             data |= (byte << bits);
1116             bits += 3;
1117             if(bits >= 8) {
1118                 *(dest++) = (data & 0xff);
1119                 bits -= 8;
1120                 data >>= 8;
1121             }
1122         }
1123     }
1124     return (lsamples / 8) * 8;
1125 }
1126 
decode(Linear buffer,void * from,unsigned lsamples)1127 unsigned g723_3Codec::decode(Linear buffer, void *from, unsigned lsamples)
1128 {
1129     Encoded src = (Encoded)from;
1130     unsigned count = (lsamples / 8) * 8;
1131     unsigned char byte, nib;
1132     unsigned bits = 0, data = 0;
1133 
1134     while(count--) {
1135         if(bits < 3) {
1136             byte = *(src++);
1137             data |= (byte << bits);
1138             bits += 8;
1139         }
1140         nib = data & 0x07;
1141         data >>= 3;
1142         bits -= 3;
1143         *(buffer++) = coder(&decode_state, nib);
1144     }
1145     return (lsamples / 8) * 8;
1146 }
1147 
getByInfo(Info & info)1148 AudioCodec *g723_3Codec::getByInfo(Info &info)
1149 {
1150     return (AudioCodec *)new g723_3Codec();
1151 }
1152 
getByFormat(const char * format)1153 AudioCodec *g723_3Codec::getByFormat(const char *format)
1154 {
1155     return (AudioCodec *)new g723_3Codec();
1156 }
1157 
1158 
1159 
g723_2Codec()1160 g723_2Codec::g723_2Codec() : AudioCodec()
1161 {
1162     unsigned pos;
1163 
1164     info.framesize = 1;
1165     info.framecount = 4;
1166     info.bitrate = 16000;
1167     info.encoding = g723_3bit;
1168     info.annotation = (char *)"g.723/2";
1169     info.rate = 8000;
1170     memset(&encode_state, 0, sizeof(encode_state));
1171     memset(&decode_state, 0, sizeof(decode_state));
1172     encode_state.yl = decode_state.yl = 34816;
1173     encode_state.yu = decode_state.yu = 544;
1174     encode_state.sr[0] = encode_state.sr[1] = decode_state.sr[0] = decode_state.sr[1] = 32;
1175 
1176     for(pos = 0; pos < 6; ++pos)
1177         encode_state.dq[pos] = decode_state.dq[pos] = 32;
1178 }
1179 
g723_2Codec(const char * id,Encoding e)1180 g723_2Codec::g723_2Codec(const char *id, Encoding e) : AudioCodec(id, e)
1181 {
1182     info.framesize = 1;
1183     info.framecount = 4;
1184     info.bitrate = 16000;
1185     info.rate = 8000;
1186     info.annotation = (char *)"g.723/2";
1187 }
1188 
~g723_2Codec()1189 g723_2Codec::~g723_2Codec()
1190 {}
1191 
encoder(short sl,state_t * state_ptr)1192 unsigned char g723_2Codec::encoder(short sl, state_t *state_ptr)
1193 {
1194     short sezi, se, sez, sei;
1195     short d, sr, y, dqsez, dq, i;
1196 
1197     sl >>= 2;
1198 
1199     sezi = predictor_zero(state_ptr);
1200     sez = sezi >> 1;
1201     sei = sezi + predictor_pole(state_ptr);
1202     se = sei >> 1;                  /* se = estimated signal */
1203 
1204     d = sl - se;
1205 
1206     /* quantize prediction difference d */
1207     y = step_size(state_ptr);       /* quantizer step size */
1208     i = quantize(d, y, qtab_723_16, 1);  /* i = ADPCM code */
1209 
1210       /* Since quantize() only produces a three level output
1211        * (1, 2, or 3), we must create the fourth one on our own
1212        */
1213     if (i == 3)                          /* i code for the zero region */
1214       if ((d & 0x8000) == 0)             /* If d > 0, i=3 isn't right... */
1215     i = 0;
1216 
1217     dq = reconstruct(i & 2, _dqlntab[i], y); /* quantized diff. */
1218 
1219     sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
1220     dqsez = sr + sez - se;          /* pole prediction diff. */
1221 
1222     update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1223 
1224 
1225     return (unsigned char)(i);
1226 }
1227 
coder(state_t * state_ptr,int i)1228 short g723_2Codec::coder(state_t *state_ptr, int i)
1229 {
1230     short sezi, sei, sez, se;
1231     short y, sr, dq, dqsez;
1232 
1233     i &= 0x03;                      /* mask to get proper bits */
1234 
1235     sezi = predictor_zero(state_ptr);
1236     sez = sezi >> 1;
1237     sei = sezi + predictor_pole(state_ptr);
1238     se = sei >> 1;                  /* se = estimated signal */
1239 
1240     y = step_size(state_ptr);       /* adaptive quantizer step size */
1241     dq = reconstruct(i & 0x02, _dqlntab[i], y); /* unquantize pred diff */
1242 
1243     sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
1244 
1245     dqsez = sr - se + sez;                  /* pole prediction diff. */
1246 
1247     update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1248 
1249 
1250     return sr << 2;
1251 }
1252 
encode(Linear buffer,void * coded,unsigned lsamples)1253 unsigned g723_2Codec::encode(Linear buffer, void *coded, unsigned lsamples)
1254 {
1255     unsigned count = (lsamples / 4);
1256     Encoded dest = (Encoded)coded;
1257     unsigned i, data, byte, bits;
1258 
1259     while(count--) {
1260         bits = 0;
1261         data = 0;
1262         for(i = 0; i < 4; ++i)
1263         {
1264             byte = encoder(*(buffer++), &encode_state);
1265             data |= (byte << bits);
1266             bits += 2;
1267             if(bits >= 8) {
1268                 *(dest++) = (data & 0xff);
1269                 bits -= 8;
1270                 data >>= 8;
1271             }
1272         }
1273     }
1274     return (lsamples / 4) * 4;
1275 }
1276 
decode(Linear buffer,void * from,unsigned lsamples)1277 unsigned g723_2Codec::decode(Linear buffer, void *from, unsigned lsamples)
1278 {
1279     Encoded src = (Encoded)from;
1280     unsigned count = (lsamples / 4) * 4;
1281     unsigned char byte, nib;
1282     unsigned bits = 0, data = 0;
1283 
1284     while(count--) {
1285         if(bits < 2) {
1286             byte = *(src++);
1287             data |= (byte << bits);
1288             bits += 8;
1289         }
1290         nib = data & 0x03;
1291         data >>= 2;
1292         bits -= 2;
1293         *(buffer++) = coder(&decode_state, nib);
1294     }
1295     return (lsamples / 4) * 4;
1296 }
1297 
getByInfo(Info & info)1298 AudioCodec *g723_2Codec::getByInfo(Info &info)
1299 {
1300     return (AudioCodec *)new g723_2Codec();
1301 }
1302 
getByFormat(const char * format)1303 AudioCodec *g723_2Codec::getByFormat(const char *format)
1304 {
1305     return (AudioCodec *)new g723_2Codec();
1306 }
1307 
g723_5Codec()1308 g723_5Codec::g723_5Codec() : AudioCodec()
1309 {
1310     unsigned pos;
1311 
1312     info.framesize = 5;
1313     info.framecount = 8;
1314     info.bitrate = 40000;
1315     info.encoding = g723_5bit;
1316     info.annotation = (char *)"g.723/5";
1317     info.rate = 8000;
1318     memset(&encode_state, 0, sizeof(encode_state));
1319     memset(&decode_state, 0, sizeof(decode_state));
1320     encode_state.yl = decode_state.yl = 34816;
1321     encode_state.yu = decode_state.yu = 544;
1322     encode_state.sr[0] = encode_state.sr[1] = decode_state.sr[0] = decode_state.sr[1] = 32;
1323 
1324     for(pos = 0; pos < 6; ++pos)
1325         encode_state.dq[pos] = decode_state.dq[pos] = 32;
1326 }
1327 
g723_5Codec(const char * id,Encoding e)1328 g723_5Codec::g723_5Codec(const char *id, Encoding e) : AudioCodec(id, e)
1329 {
1330     info.framesize = 5;
1331     info.framecount = 8;
1332     info.bitrate = 40000;
1333     info.rate = 8000;
1334     info.annotation = (char *)"g.723/5";
1335 }
1336 
~g723_5Codec()1337 g723_5Codec::~g723_5Codec()
1338 {}
1339 
encoder(short sl,state_t * state_ptr)1340 unsigned char g723_5Codec::encoder(short sl, state_t *state_ptr)
1341 {
1342     short           sei, sezi, se, sez;     /* ACCUM */
1343     short           d;                      /* SUBTA */
1344     short           y;                      /* MIX */
1345     short           sr;                     /* ADDB */
1346     short           dqsez;                  /* ADDC */
1347     short           dq, i;
1348 
1349     sl >>= 2;
1350 
1351     sezi = predictor_zero(state_ptr);
1352     sez = sezi >> 1;
1353     sei = sezi + predictor_pole(state_ptr);
1354     se = sei >> 1;                  /* se = estimated signal */
1355 
1356     d = sl - se;                    /* d = estimation difference */
1357 
1358     /* quantize prediction difference */
1359     y = step_size(state_ptr);       /* adaptive quantizer step size */
1360     i = quantize(d, y, qtab_723_40, 15);    /* i = ADPCM code */
1361 
1362     dq = reconstruct(i & 0x10, _dqlntab[i], y);     /* quantized diff */
1363 
1364     sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq; /* reconstructed signal */
1365     dqsez = sr + sez - se;          /* dqsez = pole prediction diff. */
1366 
1367     update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1368 
1369     return (unsigned char)(i);
1370 }
1371 
coder(state_t * state_ptr,int i)1372 short g723_5Codec::coder(state_t *state_ptr, int i)
1373 {
1374     short           sezi, sei, sez, se;     /* ACCUM */
1375     short           y;                 /* MIX */
1376     short           sr;                     /* ADDB */
1377     short           dq;
1378     short           dqsez;
1379 
1380     i &= 0x1f;                      /* mask to get proper bits */
1381     sezi = predictor_zero(state_ptr);
1382     sez = sezi >> 1;
1383     sei = sezi + predictor_pole(state_ptr);
1384     se = sei >> 1;                  /* se = estimated signal */
1385 
1386     y = step_size(state_ptr);       /* adaptive quantizer step size */
1387     dq = reconstruct(i & 0x10, _dqlntab[i], y);     /* estimation diff. */
1388 
1389     sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq); /* reconst. signal */
1390 
1391     dqsez = sr - se + sez;          /* pole prediction diff. */
1392 
1393     update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1394     return sr << 2;
1395 }
1396 
encode(Linear buffer,void * coded,unsigned lsamples)1397 unsigned g723_5Codec::encode(Linear buffer, void *coded, unsigned lsamples)
1398 {
1399     unsigned count = (lsamples / 8);
1400     Encoded dest = (Encoded)coded;
1401     unsigned i, data, byte, bits;
1402 
1403     while(count--) {
1404         bits = 0;
1405         data = 0;
1406         for(i = 0; i < 8; ++i)
1407         {
1408             byte = encoder(*(buffer++), &encode_state);
1409             data |= (byte << bits);
1410             bits += 5;
1411             if(bits >= 8) {
1412                 *(dest++) = (data & 0xff);
1413                 bits -= 8;
1414                 data >>= 8;
1415             }
1416         }
1417     }
1418     return (lsamples / 8) * 8;
1419 }
1420 
decode(Linear buffer,void * from,unsigned lsamples)1421 unsigned g723_5Codec::decode(Linear buffer, void *from, unsigned lsamples)
1422 {
1423     Encoded src = (Encoded)from;
1424     unsigned count = (lsamples / 8) * 8;
1425     unsigned char byte, nib;
1426     unsigned bits = 0, data = 0;
1427 
1428     while(count--) {
1429         if(bits < 5) {
1430             byte = *(src++);
1431             data |= (byte << bits);
1432             bits += 8;
1433         }
1434         nib = data & 0x1f;
1435         data >>= 5;
1436         bits -= 5;
1437         *(buffer++) = coder(&decode_state, nib);
1438     }
1439     return (lsamples / 8) * 8;
1440 }
1441 
getByInfo(Info & info)1442 AudioCodec *g723_5Codec::getByInfo(Info &info)
1443 {
1444     return (AudioCodec *)new g723_5Codec();
1445 }
1446 
getByFormat(const char * format)1447 AudioCodec *g723_5Codec::getByFormat(const char *format)
1448 {
1449     return (AudioCodec *)new g723_5Codec();
1450 }
1451 
g721Codec()1452 g721Codec::g721Codec() : AudioCodec()
1453 {
1454     unsigned pos;
1455 
1456     info.framesize = 1;
1457     info.framecount = 2;
1458     info.rate = 8000;
1459     info.bitrate = 32000;
1460     info.annotation = (char *)"g.721";
1461     info.encoding = g721ADPCM;
1462 
1463     memset(&encode_state, 0, sizeof(encode_state));
1464     memset(&decode_state, 0, sizeof(decode_state));
1465     encode_state.yl = decode_state.yl = 34816;
1466     encode_state.yu = decode_state.yu = 544;
1467     encode_state.sr[0] = encode_state.sr[1] = decode_state.sr[0] = decode_state.sr[1] = 32;
1468 
1469     for(pos = 0; pos < 6; ++pos)
1470         encode_state.dq[pos] = decode_state.dq[pos] = 32;
1471 }
1472 
g721Codec(const char * id,Encoding e)1473 g721Codec::g721Codec(const char *id, Encoding e) : AudioCodec(id, e)
1474 {
1475     info.framesize = 1;
1476     info.framecount = 2;
1477     info.rate = 8000;
1478     info.bitrate = 32000;
1479     info.annotation = (char *)"g.721";
1480 }
1481 
~g721Codec()1482 g721Codec::~g721Codec()
1483 {}
1484 
encoder(short sl,state_t * state)1485 unsigned char g721Codec::encoder(short sl, state_t *state)
1486 {
1487     short sezi, se, sez;
1488     short d, sr, y, dqsez, dq, i;
1489 
1490     sl >>= 2;
1491 
1492     sezi = predictor_zero(state);
1493     sez = sezi >> 1;
1494     se = (sezi + predictor_pole(state)) >> 1;
1495 
1496     d = sl - se;
1497 
1498     y = step_size(state);
1499     i = quantize(d, y, qtab_721, 7);
1500     dq = reconstruct(i & 8, _dqlntab[i], y);
1501     sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;
1502 
1503     dqsez = sr + sez - se;
1504 
1505     update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state);
1506 
1507     return (unsigned char)(i);
1508 }
1509 
coder(state_t * state,int i)1510 short g721Codec::coder(state_t *state, int i)
1511 {
1512     short sezi, sei, sez, se;
1513     short y, sr, dq, dqsez;
1514 
1515     sezi = predictor_zero(state);
1516     sez = sezi >> 1;
1517     sei = sezi + predictor_pole(state);
1518     se = sei >> 1;
1519     y = step_size(state);
1520     dq = reconstruct(i & 0x08, _dqlntab[i], y);
1521     sr = (dq < 0) ? (se - (dq & 0x3fff)) : se + dq;
1522     dqsez = sr - se + sez;
1523     update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state);
1524     return sr << 2;
1525 }
1526 
encode(Linear buffer,void * coded,unsigned lsamples)1527 unsigned g721Codec::encode(Linear buffer, void *coded, unsigned lsamples)
1528 {
1529     unsigned count = (lsamples / 2);
1530     unsigned char byte = 0;
1531     Encoded dest = (Encoded)coded;
1532     unsigned data, bits, i;
1533 
1534     while(count--) {
1535         bits = 0;
1536         data = 0;
1537         for(i = 0; i < 2; ++i)
1538         {
1539             byte = encoder(*(buffer++), &encode_state);
1540             data |= (byte << bits);
1541             bits += 4;
1542             if(bits >= 8)
1543                 *(dest++) = (data & 0xff);
1544         }
1545     }
1546     return (lsamples / 2) * 2;
1547 }
1548 
decode(Linear buffer,void * from,unsigned lsamples)1549 unsigned g721Codec::decode(Linear buffer, void *from, unsigned lsamples)
1550 {
1551     Encoded src = (Encoded)from;
1552     unsigned count = lsamples / 2;
1553     unsigned data;
1554 
1555     while(count--) {
1556         data = *(src++);
1557         *(buffer++) = coder(&decode_state, (data & 0x0f));
1558         data >>= 4;
1559         *(buffer++) = coder(&decode_state, (data & 0x0f));
1560     }
1561     return (lsamples / 2) * 2;
1562 }
1563 
getByInfo(Info & info)1564 AudioCodec *g721Codec::getByInfo(Info &info)
1565 {
1566     return (AudioCodec *)new g721Codec();
1567 }
1568 
getByFormat(const char * format)1569 AudioCodec *g721Codec::getByFormat(const char *format)
1570 {
1571     return (AudioCodec *)new g721Codec();
1572 }
1573 
1574 static int oki_index[8] = {-1, -1, -1, -1, 2, 4, 6, 8};
1575 
1576 static int oki_steps[49] = {
1577     16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
1578     80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
1579     307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
1580     1060, 1166, 1282, 1411, 1552
1581 };
1582 
1583 static class __LOCAL okiCodec : private AudioCodec
1584 {
1585 private:
1586     __DELETE_COPY(okiCodec);
1587 
1588     typedef struct state {
1589         short last;
1590         short ssindex;
1591     }   state_t;
1592 
1593     state_t encode_state, decode_state;
1594 
1595     AudioCodec *getByInfo(Info &info) __FINAL;
1596     AudioCodec *getByFormat(const char *format) __FINAL;
1597 
1598     unsigned decode(Linear buffer, void *from, unsigned lsamples) __FINAL;
1599     unsigned encode(Linear buffer, void *dest, unsigned lsamples) __FINAL;
1600     unsigned char encode_sample(state_t *state, short sample);
1601     short decode_sample(state_t *state, unsigned char code);
1602 
1603 public:
1604     okiCodec(const char *id, Encoding e);
1605     okiCodec(Encoding e);
1606     ~okiCodec();
1607 } voxcodec("vox", Audio::voxADPCM), okicodec("oki", Audio::okiADPCM);
1608 
okiCodec(Encoding e)1609 okiCodec::okiCodec(Encoding e) : AudioCodec()
1610 {
1611     info.framesize = 1;
1612     info.framecount = 2;
1613     info.encoding = e;
1614 
1615     if(encoding == voxADPCM) {
1616         info.rate = 6000;
1617         info.bitrate = 24000;
1618         info.annotation = (char *)"vox";
1619     }
1620     else {
1621         info.rate = 8000;
1622         info.bitrate = 24000;
1623         info.annotation = (char *)"oki";
1624     }
1625 
1626     memset(&encode_state, 0, sizeof(encode_state));
1627     memset(&decode_state, 0, sizeof(decode_state));
1628     info.set();
1629 }
1630 
okiCodec(const char * id,Encoding e)1631 okiCodec::okiCodec(const char *id, Encoding e) : AudioCodec(id, e)
1632 {
1633     info.framesize = 1;
1634     info.framecount = 2;
1635 
1636     if(encoding == voxADPCM) {
1637         info.rate = 6000;
1638         info.bitrate = 24000;
1639         info.annotation = (char *)"vox";
1640     }
1641     else {
1642         info.rate = 8000;
1643         info.bitrate = 24000;
1644         info.annotation = (char *)"oki";
1645     }
1646     memset(&encode_state, 0, sizeof(encode_state));
1647     memset(&decode_state, 0, sizeof(decode_state));
1648     info.set();
1649 }
1650 
~okiCodec()1651 okiCodec::~okiCodec()
1652 {}
1653 
encode_sample(state_t * state,short sample)1654 unsigned char okiCodec::encode_sample(state_t *state, short sample)
1655 {
1656     unsigned char code = 0;
1657     short diff, step;
1658 
1659     step = oki_steps[state->ssindex];
1660     diff = sample - state->last;
1661     if(diff < 0) {
1662         diff = -diff;
1663         code = 0x08;
1664     }
1665 
1666     if(diff >= step) {
1667         code |= 0x04;
1668         diff -= step;
1669     }
1670     if(diff >= step/2) {
1671         code |= 0x02;
1672         diff -= step/2;
1673     }
1674     if(diff >= step/4)
1675         code |= 0x01;
1676 
1677     decode_sample(state, code);
1678     return code;
1679 }
1680 
decode_sample(state_t * state,unsigned char code)1681 short okiCodec::decode_sample(state_t *state, unsigned char code)
1682 {
1683     short diff, step, sample;
1684 
1685     step = oki_steps[state->ssindex];
1686     diff = step / 8;
1687     if(code & 0x01)
1688         diff += step / 4;
1689     if(code & 0x02)
1690         diff += step / 2;
1691     if(code & 0x04)
1692         diff += step;
1693     if(code & 0x08)
1694         diff = -diff;
1695     sample = state->last + diff;
1696     if(sample > 2047)
1697         sample = 2047;
1698     else if(sample < -2047)
1699         sample = -2047;
1700     state->last = sample;
1701     state->ssindex += oki_index[code & 0x07];
1702     if(state->ssindex < 0)
1703         state->ssindex = 0;
1704     if(state->ssindex > 48)
1705         state->ssindex = 48;
1706     return sample;
1707 }
1708 
encode(Linear buffer,void * coded,unsigned lsamples)1709 unsigned okiCodec::encode(Linear buffer, void *coded, unsigned lsamples)
1710 {
1711     unsigned count = (lsamples / 2) * 2;
1712     bool hi = false;
1713     unsigned char byte = 0;
1714     Encoded dest = (Encoded)coded;
1715 
1716     while(count--) {
1717         if(hi) {
1718             byte |= encode_sample(&encode_state, *(buffer++) / 16 );
1719             *(dest++) = byte;
1720         }
1721         else
1722             byte = encode_sample(&encode_state, *(buffer++) / 16 ) << 4 ;
1723     }
1724     return (lsamples / 2) * 2;
1725 }
1726 
decode(Linear buffer,void * from,unsigned lsamples)1727 unsigned okiCodec::decode(Linear buffer, void *from, unsigned lsamples)
1728 {
1729     Encoded src = (Encoded)from;
1730     unsigned count = lsamples / 2;
1731     unsigned char byte;
1732 
1733     while(count--) {
1734         byte = ((*src >> 4) & 0x0f);
1735         *(buffer++) = (decode_sample(&decode_state, byte) * 16);
1736         byte = (*src & 0x0f);
1737         *(buffer++) = (decode_sample(&decode_state, byte) * 16);
1738         ++src;
1739     }
1740     return (lsamples / 2) * 2;
1741 }
1742 
getByInfo(Info & info)1743 AudioCodec *okiCodec::getByInfo(Info &info)
1744 {
1745     return (AudioCodec *)new okiCodec(info.encoding);
1746 }
1747 
getByFormat(const char * format)1748 AudioCodec *okiCodec::getByFormat(const char *format)
1749 {
1750     return (AudioCodec *)new okiCodec(info.encoding);
1751 }
1752 
1753 #if defined(HAVE_GSM_H) || defined(HAVE_GSM_GSM_H)
1754 
1755 static class __LOCAL GSMCodec : private AudioCodec
1756 {
1757 private:
1758     __DELETE_COPY(GSMCodec);
1759 
1760     gsm encoder, decoder;
1761     AudioCodec *getByInfo(Info &info) __FINAL;
1762     AudioCodec *getByFormat(const char *format) __FINAL;
1763 
1764     unsigned encode(Linear data, void *dest, unsigned samples) __FINAL;
1765     unsigned decode(Linear data, void *source, unsigned samples) __FINAL;
1766 
1767 public:
1768     GSMCodec(const char *id, Encoding e);
1769     GSMCodec();
1770     ~GSMCodec();
1771 } gsm_codec("gsm", Audio::gsmVoice);
1772 
GSMCodec()1773 GSMCodec::GSMCodec()
1774 {
1775     encoder = gsm_create();
1776     decoder = gsm_create();
1777     info.framesize = 33;
1778     info.framecount = 160;
1779     info.rate = 8000;
1780     info.bitrate = 13200;
1781     info.annotation = (char *)"gsm";
1782     info.encoding = gsmVoice;
1783 }
1784 
GSMCodec(const char * id,Encoding e)1785 GSMCodec::GSMCodec(const char *id, Encoding e) : AudioCodec(id, e)
1786 {
1787     encoder = gsm_create();
1788     decoder = gsm_create();
1789     info.framesize = 33;
1790     info.framecount = 160;
1791     info.rate = 8000;
1792     info.bitrate = 13200;
1793     info.annotation = (char *)"gsm";
1794 }
1795 
~GSMCodec()1796 GSMCodec::~GSMCodec()
1797 {
1798     gsm_destroy(encoder);
1799     gsm_destroy(decoder);
1800 }
1801 
getByInfo(Info & info)1802 AudioCodec *GSMCodec::getByInfo(Info &info)
1803 {
1804     return (AudioCodec *)new GSMCodec();
1805 }
1806 
getByFormat(const char * format)1807 AudioCodec *GSMCodec::getByFormat(const char *format)
1808 {
1809     return (AudioCodec *)new GSMCodec();
1810 }
1811 
encode(Linear from,void * dest,unsigned samples)1812 unsigned GSMCodec::encode(Linear from, void *dest, unsigned samples)
1813 {
1814     unsigned count = samples / 160;
1815     unsigned result = count * 33;
1816     gsm_byte *encoded = (gsm_byte *)dest;
1817 
1818     if(!count)
1819         return 0;
1820 
1821     while(count--) {
1822         gsm_encode(encoder, from, encoded);
1823         from += 160;
1824         encoded += 33;
1825     }
1826     return result;
1827 }
1828 
decode(Linear dest,void * from,unsigned samples)1829 unsigned GSMCodec::decode(Linear dest, void *from, unsigned samples)
1830 {
1831     unsigned count = samples / 160;
1832     unsigned result = count * 33;
1833     gsm_byte *encoded = (gsm_byte *)from;
1834     if(!count)
1835         return 0;
1836 
1837     while(count--) {
1838         gsm_decode(decoder, encoded, dest);
1839         encoded += 160;
1840         dest += 160;
1841     }
1842     return result;
1843 }
1844 
1845 #endif
1846 
1847 #ifdef  HAVE_SPEEX_SPEEX_H
1848 
1849 static class __LOCAL SpeexCommon: public AudioCodec
1850 {
1851 protected:
1852     const SpeexMode *spx_mode;
1853     SpeexBits enc_bits, dec_bits;
1854     unsigned int spx_clock, spx_channel;
1855     void *encoder, *decoder;
1856     int spx_frame;
1857 
1858 public:
1859     SpeexCommon(Encoding enc, const char *name);
1860     SpeexCommon();
1861     ~SpeexCommon();
1862 
1863 protected:
1864     unsigned encode(Linear buffer, void *dest, unsigned lsamples) __OVERRIDE;
1865     unsigned decode(Linear buffer, void *source, unsigned lsamples) __OVERRIDE;
1866 
1867     AudioCodec *getByInfo(Info &info) __OVERRIDE;
1868     AudioCodec *getByFormat(const char *format) __OVERRIDE;
1869 } speex_codec(Audio::speexVoice, "speex");
1870 
1871 class __LOCAL SpeexAudio: public SpeexCommon
1872 {
1873 private:
1874     __DELETE_COPY(SpeexAudio);
1875 
1876 public:
1877     SpeexAudio();
1878 };
1879 
1880 class __LOCAL SpeexVoice: public SpeexCommon
1881 {
1882 private:
1883     __DELETE_COPY(SpeexVoice);
1884 
1885 public:
1886     SpeexVoice();
1887 };
1888 
SpeexCommon(Encoding enc,const char * id)1889 SpeexCommon::SpeexCommon(Encoding enc, const char *id) :
1890 AudioCodec("speex", enc)
1891 {
1892     info.framesize = 20;
1893     info.framecount = 160;
1894     info.rate = 8000;
1895     info.bitrate = 24000;
1896     info.annotation = (char *)"speex/8000";
1897 
1898     spx_channel = 1;
1899 
1900     switch(enc) {
1901     case speexVoice:
1902         spx_clock = 8000;
1903         spx_mode = &speex_nb_mode;
1904         break;
1905     case speexAudio:
1906         info.annotation = (char *)"speex/16000";
1907         info.framesize = 40;
1908         info.rate = 16000;
1909         spx_clock = 16000;
1910         spx_mode = &speex_wb_mode;
1911     default:
1912         break;
1913     }
1914 
1915     encoder = decoder = NULL;
1916 }
1917 
SpeexCommon()1918 SpeexCommon::SpeexCommon() :
1919 AudioCodec()
1920 {
1921 }
1922 
~SpeexCommon()1923 SpeexCommon::~SpeexCommon()
1924 {
1925     if(decoder) {
1926         speex_bits_destroy(&dec_bits);
1927         speex_decoder_destroy(decoder);
1928     }
1929     if(encoder) {
1930         speex_bits_destroy(&enc_bits);
1931         speex_encoder_destroy(encoder);
1932     }
1933     decoder = encoder = NULL;
1934 }
1935 
getByFormat(const char * format)1936 AudioCodec *SpeexCommon::getByFormat(const char *format)
1937 {
1938     if(!strnicmp(format, "speex/16", 8))
1939         return (AudioCodec *)new SpeexAudio();
1940     return (AudioCodec *)new SpeexVoice();
1941 }
1942 
getByInfo(Info & info)1943 AudioCodec *SpeexCommon::getByInfo(Info &info)
1944 {
1945     switch(info.encoding) {
1946     case speexAudio:
1947             return (AudioCodec *)new SpeexAudio();
1948     default:
1949         return (AudioCodec *)new SpeexVoice();
1950     }
1951 }
1952 
decode(Linear buffer,void * src,unsigned lsamples)1953 unsigned SpeexCommon::decode(Linear buffer, void *src, unsigned lsamples)
1954 {
1955     unsigned count = lsamples / info.framecount;
1956     unsigned result = 0;
1957     char *encoded = (char *)src;
1958 
1959     if(!count)
1960         return 0;
1961 
1962     while(count--) {
1963         speex_bits_read_from(&dec_bits, encoded, info.framesize);
1964         if(speex_decode_int(decoder, &dec_bits, buffer))
1965             break;
1966         result += info.framesize;
1967     }
1968     return result;
1969 }
1970 
encode(Linear buffer,void * dest,unsigned lsamples)1971 unsigned SpeexCommon::encode(Linear buffer, void *dest, unsigned lsamples)
1972 {
1973     unsigned count = lsamples / info.framecount;
1974     unsigned result = 0;
1975     char *encoded = (char *)dest;
1976 
1977     if(!count)
1978         return 0;
1979 
1980     while(count--) {
1981         speex_bits_reset(&enc_bits);
1982         speex_encoder_ctl(encoder, SPEEX_SET_SAMPLING_RATE, &spx_clock);
1983         speex_encode_int(encoder, buffer, &enc_bits);
1984         int nb = speex_bits_write(&enc_bits, encoded, info.framesize);
1985         buffer += 160;
1986         encoded += nb;
1987         result += nb;
1988     }
1989     return result;
1990 }
1991 
SpeexAudio()1992 SpeexAudio::SpeexAudio() :
1993 SpeexCommon()
1994 {
1995     info.encoding = speexVoice;
1996     info.framesize = 40;
1997     info.framecount = 160;
1998     info.rate = 16000;
1999     info.bitrate = 48000;
2000     info.annotation = (char *)"SPEEX/16000";
2001     spx_clock = 16000;
2002     spx_channel = 1;
2003     spx_mode = &speex_wb_mode;
2004     speex_bits_init(&dec_bits);
2005     decoder = speex_decoder_init(spx_mode);
2006     speex_bits_init(&enc_bits);
2007     encoder = speex_encoder_init(spx_mode);
2008     speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &spx_frame);
2009     info.framecount = spx_frame;
2010     info.set();
2011 }
2012 
SpeexVoice()2013 SpeexVoice::SpeexVoice() :
2014 SpeexCommon()
2015 {
2016     info.encoding = speexVoice;
2017     info.framesize = 20;
2018     info.framecount = 160;
2019     info.rate = 8000;
2020     info.bitrate = 24000;
2021     info.annotation = (char *)"SPEEX/8000";
2022     spx_clock = 8000;
2023     spx_channel = 1;
2024     spx_mode = &speex_nb_mode;
2025     speex_bits_init(&dec_bits);
2026     decoder = speex_decoder_init(spx_mode);
2027     speex_bits_init(&enc_bits);
2028     encoder = speex_encoder_init(spx_mode);
2029     speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &spx_frame);
2030     info.framecount = spx_frame;
2031     info.set();
2032 }
2033 
2034 #endif
2035 
2036 } // namespace ucommon
2037