1 /* Common routines for G.721 and G.723 conversions.
2  *
3  * (c) SoX Contributors
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or (at
8  * your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  *
20  * This code is based on code from Sun, which came with the following
21  * copyright notice:
22  * -----------------------------------------------------------------------
23  * This source code is a product of Sun Microsystems, Inc. and is provided
24  * for unrestricted use.  Users may copy or modify this source code without
25  * charge.
26  *
27  * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
28  * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
30  *
31  * Sun source code is provided with no support and without any obligation on
32  * the part of Sun Microsystems, Inc. to assist in its use, correction,
33  * modification or enhancement.
34  *
35  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
36  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
37  * OR ANY PART THEREOF.
38  *
39  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
40  * or profits or other special, indirect and consequential damages, even if
41  * Sun has been advised of the possibility of such damages.
42  *
43  * Sun Microsystems, Inc.
44  * 2550 Garcia Avenue
45  * Mountain View, California  94043
46  * -----------------------------------------------------------------------
47  */
48 
49 #include "sox_i.h"
50 #include "g711.h"
51 #include "g72x.h"
52 
53 static const char LogTable256[] =
54 {
55         0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
56         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
57         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
58         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
59         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
60         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
61         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
62         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
63         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
64         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
65         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
66         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
67         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
68         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
69         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
70         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
71 };
72 
log2plus1(int val)73 static inline int log2plus1(int val)
74 {
75         /* From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup */
76         unsigned int v = (unsigned int)val; /* 32-bit word to find the log of */
77         unsigned r;     /* r will be lg(v) */
78         register unsigned int t, tt; /* temporaries */
79 
80         if ((tt = v >> 16))
81         {
82             r = (t = tt >> 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt];
83         }
84         else
85         {
86             r = (t = v >> 8) ? 8 + LogTable256[t] : LogTable256[v];
87         }
88 
89         return r + 1;
90 }
91 
92 /*
93  * quan()
94  *
95  * quantizes the input val against the table of size short integers.
96  * It returns i if table[i - 1] <= val < table[i].
97  *
98  * Using linear search for simple coding.
99  */
quan(int val,short const * table,int size)100 static int quan(int val, short const *table, int size)
101 {
102         int             i;
103 
104         for (i = 0; i < size; i++)
105                 if (val < *table++)
106                         break;
107         return (i);
108 }
109 
110 /*
111  * fmult()
112  *
113  * returns the integer product of the 14-bit integer "an" and
114  * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
115  */
fmult(int an,int srn)116 static int fmult(int an, int srn)
117 {
118         short           anmag, anexp, anmant;
119         short           wanexp, wanmant;
120         short           retval;
121 
122         anmag = (an > 0) ? an : ((-an) & 0x1FFF);
123         anexp = log2plus1(anmag) - 6;
124         anmant = (anmag == 0) ? 32 :
125             (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
126         wanexp = anexp + ((srn >> 6) & 0xF) - 13;
127 
128         wanmant = (anmant * (srn & 077) + 0x30) >> 4;
129         retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
130             (wanmant >> -wanexp);
131 
132         return (((an ^ srn) < 0) ? -retval : retval);
133 }
134 
135 /*
136  * g72x_init_state()
137  *
138  * This routine initializes and/or resets the g72x_state structure
139  * pointed to by 'state_ptr'.
140  * All the initial state values are specified in the CCITT G.721 document.
141  */
g72x_init_state(struct g72x_state * state_ptr)142 void g72x_init_state(struct g72x_state *state_ptr)
143 {
144         int             cnta;
145 
146         state_ptr->yl = 34816;
147         state_ptr->yu = 544;
148         state_ptr->dms = 0;
149         state_ptr->dml = 0;
150         state_ptr->ap = 0;
151         for (cnta = 0; cnta < 2; cnta++) {
152                 state_ptr->a[cnta] = 0;
153                 state_ptr->pk[cnta] = 0;
154                 state_ptr->sr[cnta] = 32;
155         }
156         for (cnta = 0; cnta < 6; cnta++) {
157                 state_ptr->b[cnta] = 0;
158                 state_ptr->dq[cnta] = 32;
159         }
160         state_ptr->td = 0;
161 }
162 
163 /*
164  * predictor_zero()
165  *
166  * computes the estimated signal from 6-zero predictor.
167  *
168  */
predictor_zero(struct g72x_state * state_ptr)169 int predictor_zero(struct g72x_state *state_ptr)
170 {
171         int             i;
172         int             sezi;
173 
174         sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
175         for (i = 1; i < 6; i++)                 /* ACCUM */
176                 sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
177         return (sezi);
178 }
179 /*
180  * predictor_pole()
181  *
182  * computes the estimated signal from 2-pole predictor.
183  *
184  */
predictor_pole(struct g72x_state * state_ptr)185 int predictor_pole(struct g72x_state *state_ptr)
186 {
187         return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
188             fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
189 }
190 /*
191  * step_size()
192  *
193  * computes the quantization step size of the adaptive quantizer.
194  *
195  */
step_size(struct g72x_state * state_ptr)196 int step_size(struct g72x_state *state_ptr)
197 {
198         int             y;
199         int             dif;
200         int             al;
201 
202         if (state_ptr->ap >= 256)
203                 return (state_ptr->yu);
204         else {
205                 y = state_ptr->yl >> 6;
206                 dif = state_ptr->yu - y;
207                 al = state_ptr->ap >> 2;
208                 if (dif > 0)
209                         y += (dif * al) >> 6;
210                 else if (dif < 0)
211                         y += (dif * al + 0x3F) >> 6;
212                 return (y);
213         }
214 }
215 
216 /*
217  * quantize()
218  *
219  * Given a raw sample, 'd', of the difference signal and a
220  * quantization step size scale factor, 'y', this routine returns the
221  * ADPCM codeword to which that sample gets quantized.  The step
222  * size scale factor division operation is done in the log base 2 domain
223  * as a subtraction.
224  */
quantize(int d,int y,short const * table,int size)225 int quantize(int d, int y, short const *table, int size)
226 {
227         short           dqm;    /* Magnitude of 'd' */
228         short           exp;    /* Integer part of base 2 log of 'd' */
229         short           mant;   /* Fractional part of base 2 log */
230         short           dl;     /* Log of magnitude of 'd' */
231         short           dln;    /* Step size scale factor normalized log */
232         int             i;
233 
234         /*
235          * LOG
236          *
237          * Compute base 2 log of 'd', and store in 'dl'.
238          */
239         dqm = abs(d);
240         exp = log2plus1(dqm >> 1);
241         mant = ((dqm << 7) >> exp) & 0x7F;      /* Fractional portion. */
242         dl = (exp << 7) + mant;
243 
244         /*
245          * SUBTB
246          *
247          * "Divide" by step size multiplier.
248          */
249         dln = dl - (y >> 2);
250 
251         /*
252          * QUAN
253          *
254          * Obtain codword i for 'd'.
255          */
256         i = quan(dln, table, size);
257         if (d < 0)                      /* take 1's complement of i */
258                 return ((size << 1) + 1 - i);
259         else if (i == 0)                /* take 1's complement of 0 */
260                 return ((size << 1) + 1); /* new in 1988 */
261         else
262                 return (i);
263 }
264 /*
265  * reconstruct()
266  *
267  * Returns reconstructed difference signal 'dq' obtained from
268  * codeword 'i' and quantization step size scale factor 'y'.
269  * Multiplication is performed in log base 2 domain as addition.
270  */
reconstruct(int sign,int dqln,int y)271 int reconstruct(int sign, int dqln, int y)
272 {
273         short           dql;    /* Log of 'dq' magnitude */
274         short           dex;    /* Integer part of log */
275         short           dqt;
276         short           dq;     /* Reconstructed difference signal sample */
277 
278         dql = dqln + (y >> 2);  /* ADDA */
279 
280         if (dql < 0) {
281                 return ((sign) ? -0x8000 : 0);
282         } else {                /* ANTILOG */
283                 dex = (dql >> 7) & 15;
284                 dqt = 128 + (dql & 127);
285                 dq = (dqt << 7) >> (14 - dex);
286                 return ((sign) ? (dq - 0x8000) : dq);
287         }
288 }
289 
290 
291 /*
292  * update()
293  *
294  * updates the state variables for each output code
295  */
update(int code_size,int y,int wi,int fi,int dq,int sr,int dqsez,struct g72x_state * state_ptr)296 void update(int code_size, int y, int wi, int fi, int dq, int sr,
297             int dqsez, struct g72x_state *state_ptr)
298 {
299         int             cnt;
300         short           mag, exp;       /* Adaptive predictor, FLOAT A */
301         short           a2p=0;          /* LIMC */
302         short           a1ul;           /* UPA1 */
303         short           pks1;           /* UPA2 */
304         short           fa1;
305         char            tr;             /* tone/transition detector */
306         short           ylint, thr2, dqthr;
307         short           ylfrac, thr1;
308         short           pk0;
309 
310         pk0 = (dqsez < 0) ? 1 : 0;      /* needed in updating predictor poles */
311 
312         mag = dq & 0x7FFF;              /* prediction difference magnitude */
313         /* TRANS */
314         ylint = state_ptr->yl >> 15;    /* exponent part of yl */
315         ylfrac = (state_ptr->yl >> 10) & 0x1F;  /* fractional part of yl */
316         thr1 = (32 + ylfrac) << ylint;          /* threshold */
317         thr2 = (ylint > 9) ? 31 << 10 : thr1;   /* limit thr2 to 31 << 10 */
318         dqthr = (thr2 + (thr2 >> 1)) >> 1;      /* dqthr = 0.75 * thr2 */
319         if (state_ptr->td == 0)         /* signal supposed voice */
320                 tr = 0;
321         else if (mag <= dqthr)          /* supposed data, but small mag */
322                 tr = 0;                 /* treated as voice */
323         else                            /* signal is data (modem) */
324                 tr = 1;
325 
326         /*
327          * Quantizer scale factor adaptation.
328          */
329 
330         /* FUNCTW & FILTD & DELAY */
331         /* update non-steady state step size multiplier */
332         state_ptr->yu = y + ((wi - y) >> 5);
333 
334         /* LIMB */
335         if (state_ptr->yu < 544)        /* 544 <= yu <= 5120 */
336                 state_ptr->yu = 544;
337         else if (state_ptr->yu > 5120)
338                 state_ptr->yu = 5120;
339 
340         /* FILTE & DELAY */
341         /* update steady state step size multiplier */
342         state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
343 
344         /*
345          * Adaptive predictor coefficients.
346          */
347         if (tr == 1) {                  /* reset a's and b's for modem signal */
348                 state_ptr->a[0] = 0;
349                 state_ptr->a[1] = 0;
350                 state_ptr->b[0] = 0;
351                 state_ptr->b[1] = 0;
352                 state_ptr->b[2] = 0;
353                 state_ptr->b[3] = 0;
354                 state_ptr->b[4] = 0;
355                 state_ptr->b[5] = 0;
356         } else {                        /* update a's and b's */
357                 pks1 = pk0 ^ state_ptr->pk[0];          /* UPA2 */
358 
359                 /* update predictor pole a[1] */
360                 a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
361                 if (dqsez != 0) {
362                         fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
363                         if (fa1 < -8191)        /* a2p = function of fa1 */
364                                 a2p -= 0x100;
365                         else if (fa1 > 8191)
366                                 a2p += 0xFF;
367                         else
368                                 a2p += fa1 >> 5;
369 
370                         if (pk0 ^ state_ptr->pk[1])
371                         {
372                                 /* LIMC */
373                                 if (a2p <= -12160)
374                                         a2p = -12288;
375                                 else if (a2p >= 12416)
376                                         a2p = 12288;
377                                 else
378                                         a2p -= 0x80;
379                         }
380                         else if (a2p <= -12416)
381                                 a2p = -12288;
382                         else if (a2p >= 12160)
383                                 a2p = 12288;
384                         else
385                                 a2p += 0x80;
386                 }
387 
388                 /* Possible bug: a2p not initialized if dqsez == 0) */
389                 /* TRIGB & DELAY */
390                 state_ptr->a[1] = a2p;
391 
392                 /* UPA1 */
393                 /* update predictor pole a[0] */
394                 state_ptr->a[0] -= state_ptr->a[0] >> 8;
395                 if (dqsez != 0)
396                 {
397                         if (pks1 == 0)
398                                 state_ptr->a[0] += 192;
399                         else
400                                 state_ptr->a[0] -= 192;
401                 }
402                 /* LIMD */
403                 a1ul = 15360 - a2p;
404                 if (state_ptr->a[0] < -a1ul)
405                         state_ptr->a[0] = -a1ul;
406                 else if (state_ptr->a[0] > a1ul)
407                         state_ptr->a[0] = a1ul;
408 
409                 /* UPB : update predictor zeros b[6] */
410                 for (cnt = 0; cnt < 6; cnt++) {
411                         if (code_size == 5)             /* for 40Kbps G.723 */
412                                 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
413                         else                    /* for G.721 and 24Kbps G.723 */
414                                 state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
415                         if (dq & 0x7FFF) {                      /* XOR */
416                                 if ((dq ^ state_ptr->dq[cnt]) >= 0)
417                                         state_ptr->b[cnt] += 128;
418                                 else
419                                         state_ptr->b[cnt] -= 128;
420                         }
421                 }
422         }
423 
424         for (cnt = 5; cnt > 0; cnt--)
425                 state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
426         /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
427         if (mag == 0) {
428                 state_ptr->dq[0] = (dq >= 0) ? 0x20 : (short)(unsigned short)0xFC20;
429         } else {
430                 exp = log2plus1(mag);
431                 state_ptr->dq[0] = (dq >= 0) ?
432                     (exp << 6) + ((mag << 6) >> exp) :
433                     (exp << 6) + ((mag << 6) >> exp) - 0x400;
434         }
435 
436         state_ptr->sr[1] = state_ptr->sr[0];
437         /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
438         if (sr == 0) {
439                 state_ptr->sr[0] = 0x20;
440         } else if (sr > 0) {
441                 exp = log2plus1(sr);
442                 state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
443         } else if (sr > -32768) {
444                 mag = -sr;
445                 exp = log2plus1(mag);
446                 state_ptr->sr[0] =  (exp << 6) + ((mag << 6) >> exp) - 0x400;
447         } else
448                 state_ptr->sr[0] = (short)(unsigned short)0xFC20;
449 
450         /* DELAY A */
451         state_ptr->pk[1] = state_ptr->pk[0];
452         state_ptr->pk[0] = pk0;
453 
454         /* TONE */
455         if (tr == 1)            /* this sample has been treated as data */
456                 state_ptr->td = 0;      /* next one will be treated as voice */
457         else if (a2p < -11776)  /* small sample-to-sample correlation */
458                 state_ptr->td = 1;      /* signal may be data */
459         else                            /* signal is voice */
460                 state_ptr->td = 0;
461 
462         /*
463          * Adaptation speed control.
464          */
465         state_ptr->dms += (fi - state_ptr->dms) >> 5;           /* FILTA */
466         state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);  /* FILTB */
467 
468         if (tr == 1)
469                 state_ptr->ap = 256;
470         else if (y < 1536)                                      /* SUBTC */
471                 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
472         else if (state_ptr->td == 1)
473                 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
474         else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
475             (state_ptr->dml >> 3))
476                 state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
477         else
478                 state_ptr->ap += (-state_ptr->ap) >> 4;
479 }
480 
481 /*
482  * tandem_adjust(sr, se, y, i, sign)
483  *
484  * At the end of ADPCM decoding, it simulates an encoder which may be receiving
485  * the output of this decoder as a tandem process. If the output of the
486  * simulated encoder differs from the input to this decoder, the decoder output
487  * is adjusted by one level of A-law or u-law codes.
488  *
489  * Input:
490  *      sr      decoder output linear PCM sample,
491  *      se      predictor estimate sample,
492  *      y       quantizer step size,
493  *      i       decoder input code,
494  *      sign    sign bit of code i
495  *
496  * Return:
497  *      adjusted A-law or u-law compressed sample.
498  */
tandem_adjust_alaw(int sr,int se,int y,int i,int sign,short const * qtab)499 int tandem_adjust_alaw(int sr, int se, int y, int i, int sign, short const *qtab)
500 {
501         unsigned char   sp;     /* A-law compressed 8-bit code */
502         short           dx;     /* prediction error */
503         char            id;     /* quantized prediction error */
504         int             sd;     /* adjusted A-law decoded sample value */
505         int             im;     /* biased magnitude of i */
506         int             imx;    /* biased magnitude of id */
507 
508         if (sr <= -32768)
509                 sr = -1;
510         sp = sox_13linear2alaw(((sr >> 1) << 3));/* short to A-law compression */
511         dx = (sox_alaw2linear16(sp) >> 2) - se;  /* 16-bit prediction error */
512         id = quantize(dx, y, qtab, sign - 1);
513 
514         if (id == i) {                  /* no adjustment on sp */
515                 return (sp);
516         } else {                        /* sp adjustment needed */
517                 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
518                 im = i ^ sign;          /* 2's complement to biased unsigned */
519                 imx = id ^ sign;
520 
521                 if (imx > im) {         /* sp adjusted to next lower value */
522                         if (sp & 0x80) {
523                                 sd = (sp == 0xD5) ? 0x55 :
524                                     ((sp ^ 0x55) - 1) ^ 0x55;
525                         } else {
526                                 sd = (sp == 0x2A) ? 0x2A :
527                                     ((sp ^ 0x55) + 1) ^ 0x55;
528                         }
529                 } else {                /* sp adjusted to next higher value */
530                         if (sp & 0x80)
531                                 sd = (sp == 0xAA) ? 0xAA :
532                                     ((sp ^ 0x55) + 1) ^ 0x55;
533                         else
534                                 sd = (sp == 0x55) ? 0xD5 :
535                                     ((sp ^ 0x55) - 1) ^ 0x55;
536                 }
537                 return (sd);
538         }
539 }
540 
tandem_adjust_ulaw(int sr,int se,int y,int i,int sign,short const * qtab)541 int tandem_adjust_ulaw(int sr, int se, int y, int i, int sign, short const *qtab)
542 {
543         unsigned char   sp;     /* u-law compressed 8-bit code */
544         short           dx;     /* prediction error */
545         char            id;     /* quantized prediction error */
546         int             sd;     /* adjusted u-law decoded sample value */
547         int             im;     /* biased magnitude of i */
548         int             imx;    /* biased magnitude of id */
549 
550         if (sr <= -32768)
551                 sr = 0;
552         sp = sox_14linear2ulaw((sr << 2));/* short to u-law compression */
553         dx = (sox_ulaw2linear16(sp) >> 2) - se;  /* 16-bit prediction error */
554         id = quantize(dx, y, qtab, sign - 1);
555         if (id == i) {
556                 return (sp);
557         } else {
558                 /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
559                 im = i ^ sign;          /* 2's complement to biased unsigned */
560                 imx = id ^ sign;
561                 if (imx > im) {         /* sp adjusted to next lower value */
562                         if (sp & 0x80)
563                                 sd = (sp == 0xFF) ? 0x7E : sp + 1;
564                         else
565                                 sd = (sp == 0) ? 0 : sp - 1;
566 
567                 } else {                /* sp adjusted to next higher value */
568                         if (sp & 0x80)
569                                 sd = (sp == 0x80) ? 0x80 : sp - 1;
570                         else
571                                 sd = (sp == 0x7F) ? 0xFE : sp + 1;
572                 }
573                 return (sd);
574         }
575 }
576