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