19 #include <ucommon/ucommon.h>
26 #if defined(HAVE_GSM_GSM_H)
28 #elif defined(HAVE_GSM_H)
33 #ifdef HAVE_SPEEX_SPEEX_H
34 #include <speex/speex.h>
37 #include <ucommon/export.h>
41 #define M_PI 3.14159265358979323846
45 using namespace UCOMMON_NAMESPACE;
47 static LinkedObject *first = NULL;
49 AudioCodec::AudioCodec(
const char *n, encoding_t e) :
61 AudioCodec::AudioCodec()
80 AudioCodec *AudioCodec::get(encoding_t e,
const char *format)
82 linked_pointer<AudioCodec> codec = first;
85 if(e == codec->encoding)
90 if(is(codec) && format)
91 return codec->getByFormat(format);
98 linked_pointer<AudioCodec> codec = first;
101 if(info.encoding == codec->encoding)
107 return codec->getByInfo(info);
112 bool AudioCodec::is_silent(level_t hint,
void *data,
unsigned samples)
114 level_t power = impulse(data, samples);
125 Audio::level_t AudioCodec::impulse(
void *data,
unsigned samples)
127 unsigned long sum = 0;
128 linear_t ldata =
new sample_t[samples];
129 linear_t lptr = ldata;
130 long count = decode(ldata, data, samples);
141 return (level_t)(sum / count);
144 Audio::level_t AudioCodec::peak(
void *data,
unsigned samples)
146 level_t max = 0, value;
147 linear_t ldata =
new sample_t[samples];
148 linear_t lptr = ldata;
149 long count = decode(ldata, data, samples);
164 unsigned AudioCodec::getEstimated(
void)
166 return info.framesize;
169 unsigned AudioCodec::getRequired(
void)
171 return info.framecount;
174 unsigned AudioCodec::encodeBuffered(linear_t buffer, encoded_t source,
unsigned samples)
176 return encode(buffer, source, samples);
179 unsigned AudioCodec::decodeBuffered(linear_t buffer, encoded_t source,
unsigned bytes)
181 return decode(buffer, source, toSamples(info, bytes));
184 unsigned AudioCodec::getPacket(encoded_t packet, encoded_t data,
unsigned bytes)
186 if(bytes != info.framesize)
189 memcpy(packet, data, bytes);
195 static class __LOCAL g711u :
public AudioCodec {
199 unsigned encode(linear_t buffer,
void *source,
unsigned lsamples);
200 unsigned decode(linear_t buffer,
void *dest,
unsigned lsamples);
201 level_t impulse(
void *buffer,
unsigned samples);
202 level_t peak(
void *buffer,
unsigned samples);
206 static class __LOCAL g711a :
public AudioCodec {
210 unsigned encode(linear_t buffer,
void *source,
unsigned lsamples);
211 unsigned decode(linear_t buffer,
void *dest,
unsigned lsamples);
212 level_t impulse(
void *buffer,
unsigned samples);
213 level_t peak(
void *buffer,
unsigned samples);
217 g711u::g711u() :
AudioCodec(
"g.711", mulawAudio)
222 info.bitrate = 64000;
223 info.annotation = (
char *)
"mu-law";
226 g711a::g711a() :
AudioCodec(
"g.711", alawAudio)
230 info.bitrate = 64000;
232 info.annotation = (
char *)
"a-law";
235 static unsigned ullevels[128] =
237 32124, 31100, 30076, 29052, 28028,
238 27004, 25980, 24956, 23932, 22908, 21884, 20860,
239 19836, 18812, 17788, 16764, 15996, 15484, 14972,
240 14460, 13948, 13436, 12924, 12412, 11900, 11388,
241 10876, 10364, 9852, 9340, 8828, 8316, 7932,
242 7676, 7420, 7164, 6908, 6652, 6396, 6140,
243 5884, 5628, 5372, 5116, 4860, 4604, 4348,
244 4092, 3900, 3772, 3644, 3516, 3388, 3260,
245 3132, 3004, 2876, 2748, 2620, 2492, 2364,
246 2236, 2108, 1980, 1884, 1820, 1756, 1692,
247 1628, 1564, 1500, 1436, 1372, 1308, 1244,
248 1180, 1116, 1052, 988, 924, 876, 844,
249 812, 780, 748, 716, 684, 652, 620,
250 588, 556, 524, 492, 460, 428, 396,
251 372, 356, 340, 324, 308, 292, 276,
252 260, 244, 228, 212, 196, 180, 164,
253 148, 132, 120, 112, 104, 96, 88,
254 80, 72, 64, 56, 48, 40, 32,
258 Audio::level_t g711u::impulse(
void *data,
unsigned samples)
260 unsigned long count = samples;
261 unsigned long sum = 0;
264 samples = count = 160;
266 unsigned char *dp = (
unsigned char *)data;
269 sum += (ullevels[*(dp++) & 0x7f]);
271 return (level_t)(sum / count);
274 Audio::level_t g711u::peak(
void *data,
unsigned samples)
276 unsigned long count = samples;
277 level_t max = 0, value;
280 samples = count = 160;
282 unsigned char *dp = (
unsigned char *)data;
285 value = ullevels[*(dp++) & 0x7f];
292 unsigned g711u::encode(linear_t buffer,
void *dest,
unsigned lsamples)
294 static int ulaw[256] = {
295 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
296 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
297 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
298 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
299 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
300 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
301 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
302 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
303 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
304 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
305 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
306 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
307 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
308 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
309 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
310 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
312 register sample_t sample;
313 int sign, exponent, mantissa, retval;
314 register unsigned char *d = (
unsigned char *)dest;
320 sample = *(buffer++);
321 sign = (sample >> 8) & 0x80;
322 if(sign != 0) sample = -sample;
324 exponent = ulaw[(sample >> 7) & 0xff];
325 mantissa = (sample >> (exponent + 3)) & 0x0f;
326 retval = ~(sign | (exponent << 4) | mantissa);
329 *(d++) = (
unsigned char)retval;
334 unsigned g711u::decode(linear_t buffer,
void *source,
unsigned lsamples)
336 register unsigned char *src = (
unsigned char *)source;
341 static sample_t values[256] =
343 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
344 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
345 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
346 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
347 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
348 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
349 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
350 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
351 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
352 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
353 -876, -844, -812, -780, -748, -716, -684, -652,
354 -620, -588, -556, -524, -492, -460, -428, -396,
355 -372, -356, -340, -324, -308, -292, -276, -260,
356 -244, -228, -212, -196, -180, -164, -148, -132,
357 -120, -112, -104, -96, -88, -80, -72, -64,
358 -56, -48, -40, -32, -24, -16, -8, 0,
359 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
360 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
361 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
362 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
363 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
364 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
365 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
366 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
367 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
368 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
369 876, 844, 812, 780, 748, 716, 684, 652,
370 620, 588, 556, 524, 492, 460, 428, 396,
371 372, 356, 340, 324, 308, 292, 276, 260,
372 244, 228, 212, 196, 180, 164, 148, 132,
373 120, 112, 104, 96, 88, 80, 72, 64,
374 56, 48, 40, 32, 24, 16, 8, 0
378 *(buffer++) = values[*(src++)];
383 #define AMI_MASK 0x55
385 unsigned g711a::encode(linear_t buffer,
void *dest,
unsigned lsamples)
387 int mask, seg, pcm_val;
389 unsigned char *d = (
unsigned char *)dest;
391 static int seg_end[] = {
392 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
397 pcm_val = *(buffer++);
404 for(seg = 0; seg < 8; seg++)
406 if(pcm_val <= seg_end[seg])
409 *(d++) = ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask;
414 static unsigned allevels[128] =
416 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
417 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
418 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
419 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
420 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
421 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
422 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
423 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
424 344, 328, 376, 360, 280, 264, 312, 296,
425 472, 456, 504, 488, 408, 392, 440, 424,
426 88, 72, 120, 104, 24, 8, 56, 40,
427 216, 200, 248, 232, 152, 136, 184, 168,
428 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
429 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
430 688, 656, 752, 720, 560, 528, 624, 592,
431 944, 912, 1008, 976, 816, 784, 880, 848
434 Audio::level_t g711a::impulse(
void *data,
unsigned samples)
436 unsigned long count = samples;
437 unsigned long sum = 0;
440 samples = count = 160;
444 unsigned char *dp = (
unsigned char *)data;
447 sum += (allevels[*(dp++) & 0x7f]);
449 return (level_t)(sum / count);
452 Audio::level_t g711a::peak(
void *data,
unsigned samples)
454 unsigned long count = samples;
455 level_t max = 0, value;
458 samples = count = 160;
460 unsigned char *dp = (
unsigned char *)data;
463 value = allevels[*(dp++) & 0x7f];
470 unsigned g711a::decode(linear_t buffer,
void *source,
unsigned lsamples)
472 register unsigned char *src = (
unsigned char *)source;
475 static sample_t values[256] =
477 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
478 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
479 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
480 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
481 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
482 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
483 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
484 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
485 -344, -328, -376, -360, -280, -264, -312, -296,
486 -472, -456, -504, -488, -408, -392, -440, -424,
487 -88, -72, -120, -104, -24, -8, -56, -40,
488 -216, -200, -248, -232, -152, -136, -184, -168,
489 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
490 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
491 -688, -656, -752, -720, -560, -528, -624, -592,
492 -944, -912, -1008, -976, -816, -784, -880, -848,
493 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
494 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
495 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
496 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
497 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
498 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
499 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
500 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
501 344, 328, 376, 360, 280, 264, 312, 296,
502 472, 456, 504, 488, 408, 392, 440, 424,
503 88, 72, 120, 104, 24, 8, 56, 40,
504 216, 200, 248, 232, 152, 136, 184, 168,
505 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
506 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
507 688, 656, 752, 720, 560, 528, 624, 592,
508 944, 912, 1008, 976, 816, 784, 880, 848
514 *(buffer++) = values[*(src++)];
521 static short power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
522 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
545 for (i = 0; i < size; i++)
570 exp = quan(dqm >> 1, power2, 15);
571 mant = ((dqm << 7) >> exp) & 0x7F;
572 dl = (exp << 7) + mant;
586 i = quan(dln, table, size);
588 return ((size << 1) + 1 - i);
590 return ((size << 1) + 1);
599 short anmag, anexp, anmant;
600 short wanexp, wanmant;
603 anmag = (an > 0) ? an : ((-an) & 0x1FFF);
604 anexp = quan(anmag, power2, 15) - 6;
605 anmant = (anmag == 0) ? 32 :
606 (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
607 wanexp = anexp + ((srn >> 6) & 0xF) - 13;
609 wanmant = (anmant * (srn & 077) + 0x30) >> 4;
610 retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
611 (wanmant >> -wanexp);
613 return (((an ^ srn) < 0) ? -retval : retval);
616 static int reconstruct(
626 dql = dqln + (y >> 2);
629 return ((sign) ? -0x8000 : 0);
631 dex = (dql >> 7) & 15;
632 dqt = 128 + (dql & 127);
633 dq = (dqt << 7) >> (14 - dex);
634 return ((sign) ? (dq - 0x8000) : dq);
655 short ylint, thr2, dqthr;
659 pk0 = (dqsez < 0) ? 1 : 0;
663 ylint = (short)(state_ptr->
yl >> 15);
664 ylfrac = (state_ptr->
yl >> 10) & 0x1F;
665 thr1 = (32 + ylfrac) << ylint;
666 thr2 = (short)((ylint > 9) ? 31 << 10 : thr1);
667 dqthr = (thr2 + (thr2 >> 1)) >> 1;
668 if (state_ptr->
td == 0)
670 else if (mag <= dqthr)
681 state_ptr->
yu = y + ((wi - y) >> 5);
684 if (state_ptr->
yu < 544)
686 else if (state_ptr->
yu > 5120)
687 state_ptr->
yu = 5120;
691 state_ptr->
yl += state_ptr->
yu + ((-state_ptr->
yl) >> 6);
706 pks1 = pk0 ^ state_ptr->
pk[0];
709 a2p = state_ptr->
a[1] - (state_ptr->
a[1] >> 7);
711 fa1 = (pks1) ? state_ptr->
a[0] : -state_ptr->
a[0];
719 if (pk0 ^ state_ptr->
pk[1])
723 else if (a2p >= 12416)
727 else if (a2p <= -12416)
729 else if (a2p >= 12160)
736 state_ptr->
a[1] = a2p;
740 state_ptr->
a[0] -= state_ptr->
a[0] >> 8;
743 state_ptr->
a[0] += 192;
745 state_ptr->
a[0] -= 192;
750 if (state_ptr->
a[0] < -a1ul)
751 state_ptr->
a[0] = -a1ul;
752 else if (state_ptr->
a[0] > a1ul)
753 state_ptr->
a[0] = a1ul;
756 for (cnt = 0; cnt < 6; cnt++) {
758 state_ptr->
b[cnt] -= state_ptr->
b[cnt] >> 9;
760 state_ptr->
b[cnt] -= state_ptr->
b[cnt] >> 8;
762 if ((dq ^ state_ptr->
dq[cnt]) >= 0)
763 state_ptr->
b[cnt] += 128;
765 state_ptr->
b[cnt] -= 128;
770 for (cnt = 5; cnt > 0; cnt--)
771 state_ptr->
dq[cnt] = state_ptr->
dq[cnt-1];
774 state_ptr->
dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
776 exp = quan(mag, power2, 15);
777 state_ptr->
dq[0] = (dq >= 0) ?
778 (exp << 6) + ((mag << 6) >> exp) :
779 (exp << 6) + ((mag << 6) >> exp) - 0x400;
782 state_ptr->
sr[1] = state_ptr->
sr[0];
785 state_ptr->
sr[0] = 0x20;
787 exp = quan(sr, power2, 15);
788 state_ptr->
sr[0] = (exp << 6) + ((sr << 6) >> exp);
789 }
else if (sr > -32768) {
791 exp = quan(mag, power2, 15);
792 state_ptr->
sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
794 state_ptr->
sr[0] = (short)0xFC20;
797 state_ptr->
pk[1] = state_ptr->
pk[0];
798 state_ptr->
pk[0] = pk0;
803 else if (a2p < -11776)
811 state_ptr->
dms += (fi - state_ptr->
dms) >> 5;
812 state_ptr->
dml += (((fi << 2) - state_ptr->
dml) >> 7);
817 state_ptr->
ap += (0x200 - state_ptr->
ap) >> 4;
818 else if (state_ptr->
td == 1)
819 state_ptr->
ap += (0x200 - state_ptr->
ap) >> 4;
820 else if (abs((state_ptr->
dms << 2) - state_ptr->
dml) >=
821 (state_ptr->
dml >> 3))
822 state_ptr->
ap += (0x200 - state_ptr->
ap) >> 4;
824 state_ptr->
ap += (-state_ptr->
ap) >> 4;
827 static int predictor_zero(
833 sezi = fmult(state_ptr->
b[0] >> 2, state_ptr->
dq[0]);
834 for (i = 1; i < 6; i++)
835 sezi += fmult(state_ptr->
b[i] >> 2, state_ptr->
dq[i]);
839 static int predictor_pole(
842 return (fmult(state_ptr->
a[1] >> 2, state_ptr->
sr[1]) +
843 fmult(state_ptr->
a[0] >> 2, state_ptr->
sr[0]));
846 static int step_size(
853 if (state_ptr->
ap >= 256)
854 return (state_ptr->
yu);
856 y = state_ptr->
yl >> 6;
857 dif = state_ptr->
yu - y;
858 al = state_ptr->
ap >> 2;
860 y += (dif * al) >> 6;
862 y += (dif * al + 0x3F) >> 6;
870 static short _dqlntab[16];
871 static short _witab[16];
872 static short _fitab[16];
873 static short qtab_721[7];
880 unsigned decode(linear_t buffer,
void *from,
unsigned lsamples);
881 unsigned encode(linear_t buffer,
void *dest,
unsigned lsamples);
889 } g723_4(
"adpcm", Audio::g721ADPCM);
894 static short _dqlntab[8];
895 static short _witab[8];
896 static short _fitab[8];
897 static short qtab_723_24[3];
904 unsigned decode(linear_t buffer,
void *from,
unsigned lsamples);
905 unsigned encode(linear_t buffer,
void *dest,
unsigned lsamples);
913 } g723_3(
"g.723", Audio::g723_3bit);
918 static short _dqlntab[32];
919 static short _witab[32];
920 static short _fitab[32];
921 static short qtab_723_40[15];
928 unsigned decode(linear_t buffer,
void *from,
unsigned lsamples);
929 unsigned encode(linear_t buffer,
void *dest,
unsigned lsamples);
937 } g723_5(
"g.723", Audio::g723_5bit);
942 static short _dqlntab[4];
943 static short _witab[4];
944 static short _fitab[4];
945 static short qtab_723_16[1];
953 unsigned decode(linear_t buffer,
void *from,
unsigned lsamples);
954 unsigned encode(linear_t buffer,
void *dest,
unsigned lsamples);
956 unsigned char encoder(
short sl,
state_t *state);
961 }
g723_2(
"g.723", Audio::g723_2bit);
963 short g723_2Codec::_dqlntab[4] = { 116, 365, 365, 116};
964 short g723_2Codec::_witab[4] = {-704, 14048, 14048, -704};
965 short g723_2Codec::_fitab[4] = {0, 0xE00, 0xE00, 0};
966 short g723_2Codec::qtab_723_16[1] = {261};
968 short g723_3Codec::_dqlntab[8] = {-2048, 135, 273, 373, 373, 273, 135, -2048};
969 short g723_3Codec::_witab[8] = {-128, 960, 4384, 18624, 18624, 4384, 960, -128};
970 short g723_3Codec::_fitab[8] = {0, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0};
971 short g723_3Codec::qtab_723_24[3] = {8, 218, 331};
973 short g723_5Codec::_dqlntab[32] = {-2048, -66, 28, 104, 169, 224, 274, 318,
974 358, 395, 429, 459, 488, 514, 539, 566,
975 566, 539, 514, 488, 459, 429, 395, 358,
976 318, 274, 224, 169, 104, 28, -66, -2048};
978 short g723_5Codec::_witab[32] = {448, 448, 768, 1248, 1280, 1312, 1856, 3200,
979 4512, 5728, 7008, 8960, 11456, 14080, 16928, 22272,
980 22272, 16928, 14080, 11456, 8960, 7008, 5728, 4512,
981 3200, 1856, 1312, 1280, 1248, 768, 448, 448};
983 short g723_5Codec::_fitab[32] = {0, 0, 0, 0, 0, 0x200, 0x200, 0x200,
984 0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
985 0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
986 0x200, 0x200, 0x200, 0, 0, 0, 0, 0};
988 short g723_5Codec::qtab_723_40[15] = {-122, -16, 68, 139, 198, 250, 298, 339,
989 378, 413, 445, 475, 502, 528, 553};
992 short g721Codec::_dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
993 425, 373, 323, 273, 213, 135, 4, -2048};
994 short g721Codec::_witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
995 1122, 355, 198, 112, 64, 41, 18, -12};
996 short g721Codec::_fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
997 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
998 short g721Codec::qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
1005 info.framecount = 8;
1006 info.bitrate = 24000;
1007 info.encoding = g723_3bit;
1008 info.annotation = (
char *)
"g.723/3";
1010 memset(&encode_state, 0,
sizeof(encode_state));
1011 memset(&decode_state, 0,
sizeof(decode_state));
1012 encode_state.
yl = decode_state.
yl = 34816;
1013 encode_state.
yu = decode_state.
yu = 544;
1014 encode_state.
sr[0] = encode_state.
sr[1] = decode_state.
sr[0] = decode_state.
sr[1] = 32;
1016 for(pos = 0; pos < 6; ++pos)
1017 encode_state.
dq[pos] = decode_state.
dq[pos] = 32;
1023 info.framecount = 8;
1024 info.bitrate = 24000;
1026 info.annotation = (
char *)
"g.723/3";
1033 unsigned char g723_3Codec::encoder(
short sl,
state_t *state_ptr)
1035 short sezi, se, sez, sei;
1036 short d, sr, y, dqsez, dq, i;
1040 sezi = predictor_zero(state_ptr);
1042 sei = sezi + predictor_pole(state_ptr);
1048 y = step_size(state_ptr);
1049 i = quantize(d, y, qtab_723_24, 3);
1050 dq = reconstruct(i & 4, _dqlntab[i], y);
1052 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;
1053 dqsez = sr + sez - se;
1055 update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1056 return (
unsigned char)(i);
1059 short g723_3Codec::coder(
state_t *state_ptr,
int i)
1061 short sezi, sei, sez, se;
1062 short y, sr, dq, dqsez;
1065 sezi = predictor_zero(state_ptr);
1067 sei = sezi + predictor_pole(state_ptr);
1070 y = step_size(state_ptr);
1071 dq = reconstruct(i & 0x04, _dqlntab[i], y);
1073 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq);
1075 dqsez = sr - se + sez;
1077 update(3, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1082 unsigned g723_3Codec::encode(linear_t buffer,
void *coded,
unsigned lsamples)
1084 unsigned count = (lsamples / 8);
1085 encoded_t dest = (encoded_t)coded;
1086 unsigned i, data, byte, bits;
1091 for(i = 0; i < 8; ++i)
1094 data |= (byte << bits);
1097 *(dest++) = (data & 0xff);
1103 return (lsamples / 8) * 8;
1106 unsigned g723_3Codec::decode(linear_t buffer,
void *from,
unsigned lsamples)
1108 encoded_t src = (encoded_t)from;
1109 unsigned count = (lsamples / 8) * 8;
1110 unsigned char byte, nib;
1111 unsigned bits = 0, data = 0;
1116 data |= (byte << bits);
1124 return (lsamples / 8) * 8;
1127 AudioCodec *g723_3Codec::getByInfo(info_t& info)
1132 AudioCodec *g723_3Codec::getByFormat(
const char *format)
1144 info.framecount = 4;
1145 info.bitrate = 16000;
1146 info.encoding = g723_3bit;
1147 info.annotation = (
char *)
"g.723/2";
1149 memset(&encode_state, 0,
sizeof(encode_state));
1150 memset(&decode_state, 0,
sizeof(decode_state));
1151 encode_state.
yl = decode_state.
yl = 34816;
1152 encode_state.
yu = decode_state.
yu = 544;
1153 encode_state.
sr[0] = encode_state.
sr[1] = decode_state.
sr[0] = decode_state.
sr[1] = 32;
1155 for(pos = 0; pos < 6; ++pos)
1156 encode_state.
dq[pos] = decode_state.
dq[pos] = 32;
1162 info.framecount = 4;
1163 info.bitrate = 16000;
1165 info.annotation = (
char *)
"g.723/2";
1173 short sezi, se, sez, sei;
1174 short d, sr, y, dqsez, dq, i;
1178 sezi = predictor_zero(state_ptr);
1180 sei = sezi + predictor_pole(state_ptr);
1186 y = step_size(state_ptr);
1187 i = quantize(d, y, qtab_723_16, 1);
1193 if ((d & 0x8000) == 0)
1196 dq = reconstruct(i & 2, _dqlntab[i], y);
1198 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;
1199 dqsez = sr + sez - se;
1201 update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1204 return (
unsigned char)(i);
1209 short sezi, sei, sez, se;
1210 short y, sr, dq, dqsez;
1214 sezi = predictor_zero(state_ptr);
1216 sei = sezi + predictor_pole(state_ptr);
1219 y = step_size(state_ptr);
1220 dq = reconstruct(i & 0x02, _dqlntab[i], y);
1222 sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq);
1224 dqsez = sr - se + sez;
1226 update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1234 unsigned count = (lsamples / 4);
1235 encoded_t dest = (encoded_t)coded;
1236 unsigned i, data, byte, bits;
1241 for(i = 0; i < 4; ++i)
1243 byte =
encoder(*(buffer++), &encode_state);
1244 data |= (byte << bits);
1247 *(dest++) = (data & 0xff);
1253 return (lsamples / 4) * 4;
1258 encoded_t src = (encoded_t)from;
1259 unsigned count = (lsamples / 4) * 4;
1260 unsigned char byte, nib;
1261 unsigned bits = 0, data = 0;
1266 data |= (byte << bits);
1272 *(buffer++) =
coder(&decode_state, nib);
1274 return (lsamples / 4) * 4;
1292 info.framecount = 8;
1293 info.bitrate = 40000;
1294 info.encoding = g723_5bit;
1295 info.annotation = (
char *)
"g.723/5";
1297 memset(&encode_state, 0,
sizeof(encode_state));
1298 memset(&decode_state, 0,
sizeof(decode_state));
1299 encode_state.
yl = decode_state.
yl = 34816;
1300 encode_state.
yu = decode_state.
yu = 544;
1301 encode_state.
sr[0] = encode_state.
sr[1] = decode_state.
sr[0] = decode_state.
sr[1] = 32;
1303 for(pos = 0; pos < 6; ++pos)
1304 encode_state.
dq[pos] = decode_state.
dq[pos] = 32;
1310 info.framecount = 8;
1311 info.bitrate = 40000;
1313 info.annotation = (
char *)
"g.723/5";
1319 unsigned char g723_5Codec::encoder(
short sl,
state_t *state_ptr)
1321 short sei, sezi, se, sez;
1330 sezi = predictor_zero(state_ptr);
1332 sei = sezi + predictor_pole(state_ptr);
1338 y = step_size(state_ptr);
1339 i = quantize(d, y, qtab_723_40, 15);
1341 dq = reconstruct(i & 0x10, _dqlntab[i], y);
1343 sr = (dq < 0) ? se - (dq & 0x7FFF) : se + dq;
1344 dqsez = sr + sez - se;
1346 update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1348 return (
unsigned char)(i);
1351 short g723_5Codec::coder(
state_t *state_ptr,
int i)
1353 short sezi, sei, sez, se;
1360 sezi = predictor_zero(state_ptr);
1362 sei = sezi + predictor_pole(state_ptr);
1365 y = step_size(state_ptr);
1366 dq = reconstruct(i & 0x10, _dqlntab[i], y);
1368 sr = (dq < 0) ? (se - (dq & 0x7FFF)) : (se + dq);
1370 dqsez = sr - se + sez;
1372 update(5, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
1376 unsigned g723_5Codec::encode(linear_t buffer,
void *coded,
unsigned lsamples)
1378 unsigned count = (lsamples / 8);
1379 encoded_t dest = (encoded_t)coded;
1380 unsigned i, data, byte, bits;
1385 for(i = 0; i < 8; ++i)
1388 data |= (byte << bits);
1391 *(dest++) = (data & 0xff);
1397 return (lsamples / 8) * 8;
1400 unsigned g723_5Codec::decode(linear_t buffer,
void *from,
unsigned lsamples)
1402 encoded_t src = (encoded_t)from;
1403 unsigned count = (lsamples / 8) * 8;
1404 unsigned char byte, nib;
1405 unsigned bits = 0, data = 0;
1410 data |= (byte << bits);
1418 return (lsamples / 8) * 8;
1421 AudioCodec *g723_5Codec::getByInfo(info_t& info)
1426 AudioCodec *g723_5Codec::getByFormat(
const char *format)
1436 info.framecount = 2;
1438 info.bitrate = 32000;
1439 info.annotation = (
char *)
"g.721";
1440 info.encoding = g721ADPCM;
1442 memset(&encode_state, 0,
sizeof(encode_state));
1443 memset(&decode_state, 0,
sizeof(decode_state));
1444 encode_state.
yl = decode_state.
yl = 34816;
1445 encode_state.
yu = decode_state.
yu = 544;
1446 encode_state.
sr[0] = encode_state.
sr[1] = decode_state.
sr[0] = decode_state.
sr[1] = 32;
1448 for(pos = 0; pos < 6; ++pos)
1449 encode_state.
dq[pos] = decode_state.
dq[pos] = 32;
1455 info.framecount = 2;
1457 info.bitrate = 32000;
1458 info.annotation = (
char *)
"g.721";
1464 unsigned char g721Codec::encoder(
short sl,
state_t *
state)
1466 short sezi, se, sez;
1467 short d, sr, y, dqsez, dq, i;
1471 sezi = predictor_zero(state);
1473 se = (sezi + predictor_pole(state)) >> 1;
1477 y = step_size(state);
1478 i = quantize(d, y, qtab_721, 7);
1479 dq = reconstruct(i & 8, _dqlntab[i], y);
1480 sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;
1482 dqsez = sr + sez - se;
1484 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state);
1486 return (
unsigned char)(i);
1489 short g721Codec::coder(
state_t *state,
int i)
1491 short sezi, sei, sez, se;
1492 short y, sr, dq, dqsez;
1494 sezi = predictor_zero(state);
1496 sei = sezi + predictor_pole(state);
1498 y = step_size(state);
1499 dq = reconstruct(i & 0x08, _dqlntab[i], y);
1500 sr = (dq < 0) ? (se - (dq & 0x3fff)) : se + dq;
1501 dqsez = sr - se + sez;
1502 update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state);
1506 unsigned g721Codec::encode(linear_t buffer,
void *coded,
unsigned lsamples)
1508 unsigned count = (lsamples / 2);
1509 unsigned char byte = 0;
1510 encoded_t dest = (encoded_t)coded;
1511 unsigned data, bits, i;
1516 for(i = 0; i < 2; ++i)
1519 data |= (byte << bits);
1522 *(dest++) = (data & 0xff);
1525 return (lsamples / 2) * 2;
1528 unsigned g721Codec::decode(linear_t buffer,
void *from,
unsigned lsamples)
1530 encoded_t src = (encoded_t)from;
1531 unsigned count = lsamples / 2;
1540 return (lsamples / 2) * 2;
1543 AudioCodec *g721Codec::getByInfo(info_t& info)
1548 AudioCodec *g721Codec::getByFormat(
const char *format)
1553 static int oki_index[8] = {-1, -1, -1, -1, 2, 4, 6, 8};
1555 static int oki_steps[49] = {
1556 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
1557 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
1558 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
1559 1060, 1166, 1282, 1411, 1552
1565 typedef struct state {
1575 unsigned decode(linear_t buffer,
void *from,
unsigned lsamples);
1576 unsigned encode(linear_t buffer,
void *dest,
unsigned lsamples);
1577 unsigned char encode_sample(
state_t *state,
short sample);
1578 short decode_sample(
state_t *state,
unsigned char code);
1581 okiCodec(
const char *
id, encoding_t e);
1584 } voxcodec(
"vox", Audio::voxADPCM),
okicodec(
"oki", Audio::okiADPCM);
1589 info.framecount = 2;
1592 if(encoding == voxADPCM) {
1594 info.bitrate = 24000;
1595 info.annotation = (
char *)
"vox";
1599 info.bitrate = 24000;
1600 info.annotation = (
char *)
"oki";
1611 info.framecount = 2;
1613 if(encoding == voxADPCM) {
1615 info.bitrate = 24000;
1616 info.annotation = (
char *)
"vox";
1620 info.bitrate = 24000;
1621 info.annotation = (
char *)
"oki";
1631 unsigned char okiCodec::encode_sample(
state_t *state,
short sample)
1633 unsigned char code = 0;
1636 step = oki_steps[state->
ssindex];
1637 diff = sample - state->
last;
1647 if(diff >= step/2) {
1654 decode_sample(state, code);
1658 short okiCodec::decode_sample(
state_t *state,
unsigned char code)
1660 short diff, step, sample;
1662 step = oki_steps[state->
ssindex];
1672 sample = state->
last + diff;
1675 else if(sample < -2047)
1677 state->
last = sample;
1678 state->
ssindex += oki_index[code & 0x07];
1686 unsigned okiCodec::encode(linear_t buffer,
void *coded,
unsigned lsamples)
1688 unsigned count = (lsamples / 2) * 2;
1690 unsigned char byte = 0;
1691 encoded_t dest = (encoded_t)coded;
1695 byte |= encode_sample(&
encode_state, *(buffer++) / 16 );
1699 byte = encode_sample(&
encode_state, *(buffer++) / 16 ) << 4 ;
1701 return (lsamples / 2) * 2;
1704 unsigned okiCodec::decode(linear_t buffer,
void *from,
unsigned lsamples)
1706 encoded_t src = (encoded_t)from;
1707 unsigned count = lsamples / 2;
1711 byte = ((*src >> 4) & 0x0f);
1712 *(buffer++) = (decode_sample(&
decode_state, byte) * 16);
1713 byte = (*src & 0x0f);
1714 *(buffer++) = (decode_sample(&
decode_state, byte) * 16);
1717 return (lsamples / 2) * 2;
1720 AudioCodec *okiCodec::getByInfo(info_t& info)
1725 AudioCodec *okiCodec::getByFormat(
const char *format)
1730 #if defined(HAVE_GSM_H) || defined(HAVE_GSM_GSM_H)
1732 static class __LOCAL GSMCodec :
private AudioCodec
1735 gsm encoder, decoder;
1739 unsigned encode(linear_t data,
void *dest,
unsigned samples);
1740 unsigned decode(linear_t data,
void *source,
unsigned samples);
1743 GSMCodec(
const char *
id, encoding_t e);
1746 } gsm_codec(
"gsm", Audio::gsmVoice);
1748 GSMCodec::GSMCodec()
1750 encoder = gsm_create();
1751 decoder = gsm_create();
1752 info.framesize = 33;
1753 info.framecount = 160;
1755 info.bitrate = 13200;
1756 info.annotation = (
char *)
"gsm";
1757 info.encoding = gsmVoice;
1760 GSMCodec::GSMCodec(
const char *
id, encoding_t e) :
AudioCodec(id, e)
1762 encoder = gsm_create();
1763 decoder = gsm_create();
1764 info.framesize = 33;
1765 info.framecount = 160;
1767 info.bitrate = 13200;
1768 info.annotation = (
char *)
"gsm";
1771 GSMCodec::~GSMCodec()
1773 gsm_destroy(encoder);
1774 gsm_destroy(decoder);
1777 AudioCodec *GSMCodec::getByInfo(info_t& info)
1782 AudioCodec *GSMCodec::getByFormat(
const char *format)
1787 unsigned GSMCodec::encode(linear_t from,
void *dest,
unsigned samples)
1789 unsigned count = samples / 160;
1790 unsigned result = count * 33;
1791 gsm_byte *encoded = (gsm_byte *)dest;
1797 gsm_encode(encoder, from, encoded);
1804 unsigned GSMCodec::decode(linear_t dest,
void *from,
unsigned samples)
1806 unsigned count = samples / 160;
1807 unsigned result = count * 33;
1808 gsm_byte *encoded = (gsm_byte *)from;
1813 gsm_decode(decoder, encoded, dest);
1822 #ifdef HAVE_SPEEX_SPEEX_H
1824 static class __LOCAL SpeexCommon:
public AudioCodec
1827 const SpeexMode *spx_mode;
1828 SpeexBits enc_bits, dec_bits;
1829 unsigned int spx_clock, spx_channel;
1830 void *encoder, *decoder;
1834 SpeexCommon(encoding_t enc,
const char *name);
1838 unsigned encode(linear_t buffer,
void *dest,
unsigned lsamples);
1839 unsigned decode(linear_t buffer,
void *source,
unsigned lsamples);
1843 } speex_codec(Audio::speexVoice,
"speex");
1845 class __LOCAL SpeexAudio:
public SpeexCommon
1851 class __LOCAL SpeexVoice:
public SpeexCommon
1857 SpeexCommon::SpeexCommon(encoding_t enc,
const char *
id) :
1860 info.framesize = 20;
1861 info.framecount = 160;
1863 info.bitrate = 24000;
1864 info.annotation = (
char *)
"speex/8000";
1871 spx_mode = &speex_nb_mode;
1874 info.annotation = (
char *)
"speex/16000";
1875 info.framesize = 40;
1878 spx_mode = &speex_wb_mode;
1883 encoder = decoder = NULL;
1886 SpeexCommon::SpeexCommon() :
1891 SpeexCommon::~SpeexCommon()
1894 speex_bits_destroy(&dec_bits);
1895 speex_decoder_destroy(decoder);
1898 speex_bits_destroy(&enc_bits);
1899 speex_encoder_destroy(encoder);
1901 decoder = encoder = NULL;
1904 AudioCodec *SpeexCommon::getByFormat(
const char *format)
1906 if(!strnicmp(format,
"speex/16", 8))
1911 AudioCodec *SpeexCommon::getByInfo(info_t& info)
1913 switch(info.encoding) {
1921 unsigned SpeexCommon::decode(linear_t buffer,
void *src,
unsigned lsamples)
1923 unsigned count = lsamples / info.framecount;
1924 unsigned result = 0;
1925 char *encoded = (
char *)src;
1931 speex_bits_read_from(&dec_bits, encoded, info.framesize);
1932 if(speex_decode_int(decoder, &dec_bits, buffer))
1934 result += info.framesize;
1939 unsigned SpeexCommon::encode(linear_t buffer,
void *dest,
unsigned lsamples)
1941 unsigned count = lsamples / info.framecount;
1942 unsigned result = 0;
1943 char *encoded = (
char *)dest;
1949 speex_bits_reset(&enc_bits);
1950 speex_encoder_ctl(encoder, SPEEX_SET_SAMPLING_RATE, &spx_clock);
1951 speex_encode_int(encoder, buffer, &enc_bits);
1952 int nb = speex_bits_write(&enc_bits, encoded, info.framesize);
1960 SpeexAudio::SpeexAudio() :
1963 info.encoding = speexVoice;
1964 info.framesize = 40;
1965 info.framecount = 160;
1967 info.bitrate = 48000;
1968 info.annotation = (
char *)
"SPEEX/16000";
1971 spx_mode = &speex_wb_mode;
1972 speex_bits_init(&dec_bits);
1973 decoder = speex_decoder_init(spx_mode);
1974 speex_bits_init(&enc_bits);
1975 encoder = speex_encoder_init(spx_mode);
1976 speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &spx_frame);
1977 info.framecount = spx_frame;
1981 SpeexVoice::SpeexVoice() :
1984 info.encoding = speexVoice;
1985 info.framesize = 20;
1986 info.framecount = 160;
1988 info.bitrate = 24000;
1989 info.annotation = (
char *)
"SPEEX/8000";
1992 spx_mode = &speex_nb_mode;
1993 speex_bits_init(&dec_bits);
1994 decoder = speex_decoder_init(spx_mode);
1995 speex_bits_init(&enc_bits);
1996 encoder = speex_encoder_init(spx_mode);
1997 speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &spx_frame);
1998 info.framecount = spx_frame;
short coder(state_t *state, int nib)
g723_3Codec(const char *id, encoding_t e)
g721Codec(const char *id, encoding_t e)
AudioCodec * getByFormat(const char *format)
#define BAYONNE_NAMESPACE
g723_2Codec AudioCodec g723_2("g.723", Audio::g723_2bit)
okiCodec(const char *id, encoding_t e)
okiCodec AudioCodec okicodec("oki", Audio::okiADPCM)
g723_5Codec(const char *id, encoding_t e)
g723_2Codec(const char *id, encoding_t e)
GNU Bayonne library namespace.
unsigned encode(linear_t buffer, void *dest, unsigned lsamples)
unsigned char encoder(short sl, state_t *state)
AudioCodec * getByInfo(info_t &info)
unsigned decode(linear_t buffer, void *from, unsigned lsamples)