Mon Mar 19 11:30:26 2012

Asterisk developer's documentation


codec_g726.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * Kevin P. Fleming <kpfleming@digium.com>
00008  *
00009  * Based on frompcm.c and topcm.c from the Emiliano MIPL browser/
00010  * interpreter.  See http://www.bsdtelephony.com.mx
00011  *
00012  * See http://www.asterisk.org for more information about
00013  * the Asterisk project. Please do not directly contact
00014  * any of the maintainers of this project for assistance;
00015  * the project provides a web site, mailing lists and IRC
00016  * channels for your use.
00017  *
00018  * This program is free software, distributed under the terms of
00019  * the GNU General Public License Version 2. See the LICENSE file
00020  * at the top of the source tree.
00021  */
00022 
00023 /*! \file
00024  *
00025  * \brief codec_g726.c - translate between signed linear and ITU G.726-32kbps (both RFC3551 and AAL2 codeword packing)
00026  *
00027  * \ingroup codecs
00028  */
00029 
00030 /*** MODULEINFO
00031    <support_level>core</support_level>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00037 
00038 #include "asterisk/lock.h"
00039 #include "asterisk/linkedlists.h"
00040 #include "asterisk/module.h"
00041 #include "asterisk/config.h"
00042 #include "asterisk/translate.h"
00043 #include "asterisk/utils.h"
00044 
00045 #define WANT_ASM
00046 #include "log2comp.h"
00047 
00048 /* define NOT_BLI to use a faster but not bit-level identical version */
00049 /* #define NOT_BLI */
00050 
00051 #if defined(NOT_BLI)
00052 #  if defined(_MSC_VER)
00053 typedef __int64 sint64;
00054 #  elif defined(__GNUC__)
00055 typedef long long sint64;
00056 #  else
00057 #     error 64-bit integer type is not defined for your compiler/platform
00058 #  endif
00059 #endif
00060 
00061 #define BUFFER_SAMPLES   8096 /* size for the translation buffers */
00062 #define BUF_SHIFT 5
00063 
00064 /* Sample frame data */
00065 #include "asterisk/slin.h"
00066 #include "ex_g726.h"
00067 
00068 /*
00069  * The following is the definition of the state structure
00070  * used by the G.726 encoder and decoder to preserve their internal
00071  * state between successive calls.  The meanings of the majority
00072  * of the state structure fields are explained in detail in the
00073  * CCITT Recommendation G.721.  The field names are essentially identical
00074  * to variable names in the bit level description of the coding algorithm
00075  * included in this Recommendation.
00076  */
00077 struct g726_state {
00078    long yl; /* Locked or steady state step size multiplier. */
00079    int yu;     /* Unlocked or non-steady state step size multiplier. */
00080    int dms; /* Short term energy estimate. */
00081    int dml; /* Long term energy estimate. */
00082    int ap;     /* Linear weighting coefficient of 'yl' and 'yu'. */
00083    int a[2];   /* Coefficients of pole portion of prediction filter.
00084           * stored as fixed-point 1==2^14 */
00085    int b[6];   /* Coefficients of zero portion of prediction filter.
00086           * stored as fixed-point 1==2^14 */
00087    int pk[2];  /* Signs of previous two samples of a partially
00088           * reconstructed signal. */
00089    int dq[6];     /* Previous 6 samples of the quantized difference signal
00090           * stored as fixed point 1==2^12,
00091           * or in internal floating point format */
00092    int sr[2];  /* Previous 2 samples of the quantized difference signal
00093           * stored as fixed point 1==2^12,
00094           * or in internal floating point format */
00095    int td;     /* delayed tone detect, new in 1988 version */
00096 };
00097 
00098 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
00099 /*
00100  * Maps G.721 code word to reconstructed scale factor normalized log
00101  * magnitude values.
00102  */
00103 static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
00104             425, 373, 323, 273, 213, 135, 4, -2048};
00105 
00106 /* Maps G.721 code word to log of scale factor multiplier. */
00107 static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
00108             1122, 355, 198, 112, 64, 41, 18, -12};
00109 /*
00110  * Maps G.721 code words to a set of values whose long and short
00111  * term averages are computed and then compared to give an indication
00112  * how stationary (steady state) the signal is.
00113  */
00114 static int _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
00115             0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
00116 
00117 
00118 /*
00119  * g72x_init_state()
00120  *
00121  * This routine initializes and/or resets the g726_state structure
00122  * pointed to by 'state_ptr'.
00123  * All the initial state values are specified in the CCITT G.721 document.
00124  */
00125 static void g726_init_state(struct g726_state *state_ptr)
00126 {
00127    int      cnta;
00128 
00129    state_ptr->yl = 34816;
00130    state_ptr->yu = 544;
00131    state_ptr->dms = 0;
00132    state_ptr->dml = 0;
00133    state_ptr->ap = 0;
00134    for (cnta = 0; cnta < 2; cnta++) {
00135       state_ptr->a[cnta] = 0;
00136       state_ptr->pk[cnta] = 0;
00137 #ifdef NOT_BLI
00138       state_ptr->sr[cnta] = 1;
00139 #else
00140       state_ptr->sr[cnta] = 32;
00141 #endif
00142    }
00143    for (cnta = 0; cnta < 6; cnta++) {
00144       state_ptr->b[cnta] = 0;
00145 #ifdef NOT_BLI
00146       state_ptr->dq[cnta] = 1;
00147 #else
00148       state_ptr->dq[cnta] = 32;
00149 #endif
00150    }
00151    state_ptr->td = 0;
00152 }
00153 
00154 /*
00155  * quan()
00156  *
00157  * quantizes the input val against the table of integers.
00158  * It returns i if table[i - 1] <= val < table[i].
00159  *
00160  * Using linear search for simple coding.
00161  */
00162 static int quan(int val, int *table, int size)
00163 {
00164    int      i;
00165 
00166    for (i = 0; i < size && val >= *table; ++i, ++table)
00167       ;
00168    return (i);
00169 }
00170 
00171 #ifdef NOT_BLI /* faster non-identical version */
00172 
00173 /*
00174  * predictor_zero()
00175  *
00176  * computes the estimated signal from 6-zero predictor.
00177  *
00178  */
00179 static int predictor_zero(struct g726_state *state_ptr)
00180 {  /* divide by 2 is necessary here to handle negative numbers correctly */
00181    int i;
00182    sint64 sezi;
00183    for (sezi = 0, i = 0; i < 6; i++)         /* ACCUM */
00184       sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
00185    return (int)(sezi >> 13) / 2 /* 2^14 */;
00186 }
00187 
00188 /*
00189  * predictor_pole()
00190  *
00191  * computes the estimated signal from 2-pole predictor.
00192  *
00193  */
00194 static int predictor_pole(struct g726_state *state_ptr)
00195 {  /* divide by 2 is necessary here to handle negative numbers correctly */
00196    return (int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
00197                  (sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 /* 2^14 */;
00198 }
00199 
00200 #else /* NOT_BLI - identical version */
00201 /*
00202  * fmult()
00203  *
00204  * returns the integer product of the fixed-point number "an" (1==2^12) and
00205  * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
00206  */
00207 static int fmult(int an, int srn)
00208 {
00209    int      anmag, anexp, anmant;
00210    int      wanexp, wanmant;
00211    int      retval;
00212 
00213    anmag = (an > 0) ? an : ((-an) & 0x1FFF);
00214    anexp = ilog2(anmag) - 5;
00215    anmant = (anmag == 0) ? 32 :
00216        (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
00217    wanexp = anexp + ((srn >> 6) & 0xF) - 13;
00218 
00219    wanmant = (anmant * (srn & 077) + 0x30) >> 4;
00220    retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
00221        (wanmant >> -wanexp);
00222 
00223    return (((an ^ srn) < 0) ? -retval : retval);
00224 }
00225 
00226 static int predictor_zero(struct g726_state *state_ptr)
00227 {
00228    int      i;
00229    int      sezi;
00230    for (sezi = 0, i = 0; i < 6; i++)         /* ACCUM */
00231       sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
00232    return sezi;
00233 }
00234 
00235 static int predictor_pole(struct g726_state *state_ptr)
00236 {
00237    return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
00238          fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
00239 }
00240 
00241 #endif /* NOT_BLI */
00242 
00243 /*
00244  * step_size()
00245  *
00246  * computes the quantization step size of the adaptive quantizer.
00247  *
00248  */
00249 static int step_size(struct g726_state *state_ptr)
00250 {
00251    int      y;
00252    int      dif;
00253    int      al;
00254 
00255    if (state_ptr->ap >= 256)
00256       return (state_ptr->yu);
00257    else {
00258       y = state_ptr->yl >> 6;
00259       dif = state_ptr->yu - y;
00260       al = state_ptr->ap >> 2;
00261       if (dif > 0)
00262          y += (dif * al) >> 6;
00263       else if (dif < 0)
00264          y += (dif * al + 0x3F) >> 6;
00265       return (y);
00266    }
00267 }
00268 
00269 /*
00270  * quantize()
00271  *
00272  * Given a raw sample, 'd', of the difference signal and a
00273  * quantization step size scale factor, 'y', this routine returns the
00274  * ADPCM codeword to which that sample gets quantized.  The step
00275  * size scale factor division operation is done in the log base 2 domain
00276  * as a subtraction.
00277  */
00278 static int quantize(
00279    int      d, /* Raw difference signal sample */
00280    int      y, /* Step size multiplier */
00281    int      *table,  /* quantization table */
00282    int      size) /* table size of integers */
00283 {
00284    int      dqm;  /* Magnitude of 'd' */
00285    int      exp;  /* Integer part of base 2 log of 'd' */
00286    int      mant; /* Fractional part of base 2 log */
00287    int      dl;      /* Log of magnitude of 'd' */
00288    int      dln;  /* Step size scale factor normalized log */
00289    int      i;
00290 
00291    /*
00292     * LOG
00293     *
00294     * Compute base 2 log of 'd', and store in 'dl'.
00295     */
00296    dqm = abs(d);
00297    exp = ilog2(dqm);
00298    if (exp < 0)
00299       exp = 0;
00300    mant = ((dqm << 7) >> exp) & 0x7F;  /* Fractional portion. */
00301    dl = (exp << 7) | mant;
00302 
00303    /*
00304     * SUBTB
00305     *
00306     * "Divide" by step size multiplier.
00307     */
00308    dln = dl - (y >> 2);
00309 
00310    /*
00311     * QUAN
00312     *
00313     * Obtain codword i for 'd'.
00314     */
00315    i = quan(dln, table, size);
00316    if (d < 0)        /* take 1's complement of i */
00317       return ((size << 1) + 1 - i);
00318    else if (i == 0)     /* take 1's complement of 0 */
00319       return ((size << 1) + 1); /* new in 1988 */
00320    else
00321       return (i);
00322 }
00323 
00324 /*
00325  * reconstruct()
00326  *
00327  * Returns reconstructed difference signal 'dq' obtained from
00328  * codeword 'i' and quantization step size scale factor 'y'.
00329  * Multiplication is performed in log base 2 domain as addition.
00330  */
00331 static int reconstruct(
00332    int      sign, /* 0 for non-negative value */
00333    int      dqln, /* G.72x codeword */
00334    int      y) /* Step size multiplier */
00335 {
00336    int      dql;  /* Log of 'dq' magnitude */
00337    int      dex;  /* Integer part of log */
00338    int      dqt;
00339    int      dq;   /* Reconstructed difference signal sample */
00340 
00341    dql = dqln + (y >> 2);  /* ADDA */
00342 
00343    if (dql < 0) {
00344 #ifdef NOT_BLI
00345       return (sign) ? -1 : 1;
00346 #else
00347       return (sign) ? -0x8000 : 0;
00348 #endif
00349    } else {    /* ANTILOG */
00350       dex = (dql >> 7) & 15;
00351       dqt = 128 + (dql & 127);
00352 #ifdef NOT_BLI
00353       dq = ((dqt << 19) >> (14 - dex));
00354       return (sign) ? -dq : dq;
00355 #else
00356       dq = (dqt << 7) >> (14 - dex);
00357       return (sign) ? (dq - 0x8000) : dq;
00358 #endif
00359    }
00360 }
00361 
00362 /*
00363  * update()
00364  *
00365  * updates the state variables for each output code
00366  */
00367 static void update(
00368    int      code_size,  /* distinguish 723_40 with others */
00369    int      y,    /* quantizer step size */
00370    int      wi,      /* scale factor multiplier */
00371    int      fi,      /* for long/short term energies */
00372    int      dq,      /* quantized prediction difference */
00373    int      sr,      /* reconstructed signal */
00374    int      dqsez,      /* difference from 2-pole predictor */
00375    struct g726_state *state_ptr) /* coder state pointer */
00376 {
00377    int      cnt;
00378    int      mag;     /* Adaptive predictor, FLOAT A */
00379 #ifndef NOT_BLI
00380    int      exp;
00381 #endif
00382    int      a2p=0;      /* LIMC */
00383    int      a1ul;    /* UPA1 */
00384    int      pks1;    /* UPA2 */
00385    int      fa1;
00386    int      tr;         /* tone/transition detector */
00387    int      ylint, thr2, dqthr;
00388    int      ylfrac, thr1;
00389    int      pk0;
00390 
00391    pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
00392 
00393 #ifdef NOT_BLI
00394    mag = abs(dq / 0x1000); /* prediction difference magnitude */
00395 #else
00396    mag = dq & 0x7FFF;      /* prediction difference magnitude */
00397 #endif
00398    /* TRANS */
00399    ylint = state_ptr->yl >> 15;  /* exponent part of yl */
00400    ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
00401    thr1 = (32 + ylfrac) << ylint;      /* threshold */
00402    thr2 = (ylint > 9) ? 31 << 10 : thr1;  /* limit thr2 to 31 << 10 */
00403    dqthr = (thr2 + (thr2 >> 1)) >> 1;  /* dqthr = 0.75 * thr2 */
00404    if (state_ptr->td == 0)    /* signal supposed voice */
00405       tr = 0;
00406    else if (mag <= dqthr)     /* supposed data, but small mag */
00407       tr = 0;        /* treated as voice */
00408    else           /* signal is data (modem) */
00409       tr = 1;
00410 
00411    /*
00412     * Quantizer scale factor adaptation.
00413     */
00414 
00415    /* FUNCTW & FILTD & DELAY */
00416    /* update non-steady state step size multiplier */
00417    state_ptr->yu = y + ((wi - y) >> 5);
00418 
00419    /* LIMB */
00420    if (state_ptr->yu < 544)   /* 544 <= yu <= 5120 */
00421       state_ptr->yu = 544;
00422    else if (state_ptr->yu > 5120)
00423       state_ptr->yu = 5120;
00424 
00425    /* FILTE & DELAY */
00426    /* update steady state step size multiplier */
00427    state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
00428 
00429    /*
00430     * Adaptive predictor coefficients.
00431     */
00432    if (tr == 1) {       /* reset a's and b's for modem signal */
00433       state_ptr->a[0] = 0;
00434       state_ptr->a[1] = 0;
00435       state_ptr->b[0] = 0;
00436       state_ptr->b[1] = 0;
00437       state_ptr->b[2] = 0;
00438       state_ptr->b[3] = 0;
00439       state_ptr->b[4] = 0;
00440       state_ptr->b[5] = 0;
00441    } else {       /* update a's and b's */
00442       pks1 = pk0 ^ state_ptr->pk[0];      /* UPA2 */
00443 
00444       /* update predictor pole a[1] */
00445       a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
00446       if (dqsez != 0) {
00447          fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
00448          if (fa1 < -8191)  /* a2p = function of fa1 */
00449             a2p -= 0x100;
00450          else if (fa1 > 8191)
00451             a2p += 0xFF;
00452          else
00453             a2p += fa1 >> 5;
00454 
00455          if (pk0 ^ state_ptr->pk[1])
00456             /* LIMC */
00457             if (a2p <= -12160)
00458                a2p = -12288;
00459             else if (a2p >= 12416)
00460                a2p = 12288;
00461             else
00462                a2p -= 0x80;
00463          else if (a2p <= -12416)
00464             a2p = -12288;
00465          else if (a2p >= 12160)
00466             a2p = 12288;
00467          else
00468             a2p += 0x80;
00469       }
00470 
00471       /* TRIGB & DELAY */
00472       state_ptr->a[1] = a2p;
00473 
00474       /* UPA1 */
00475       /* update predictor pole a[0] */
00476       state_ptr->a[0] -= state_ptr->a[0] >> 8;
00477       if (dqsez != 0) {
00478          if (pks1 == 0)
00479             state_ptr->a[0] += 192;
00480          else
00481             state_ptr->a[0] -= 192;
00482       }
00483       /* LIMD */
00484       a1ul = 15360 - a2p;
00485       if (state_ptr->a[0] < -a1ul)
00486          state_ptr->a[0] = -a1ul;
00487       else if (state_ptr->a[0] > a1ul)
00488          state_ptr->a[0] = a1ul;
00489 
00490       /* UPB : update predictor zeros b[6] */
00491       for (cnt = 0; cnt < 6; cnt++) {
00492          if (code_size == 5)     /* for 40Kbps G.723 */
00493             state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
00494          else        /* for G.721 and 24Kbps G.723 */
00495             state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
00496          if (mag)
00497          {  /* XOR */
00498             if ((dq ^ state_ptr->dq[cnt]) >= 0)
00499                state_ptr->b[cnt] += 128;
00500             else
00501                state_ptr->b[cnt] -= 128;
00502          }
00503       }
00504    }
00505 
00506    for (cnt = 5; cnt > 0; cnt--)
00507       state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
00508 #ifdef NOT_BLI
00509    state_ptr->dq[0] = dq;
00510 #else
00511    /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
00512    if (mag == 0) {
00513       state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
00514    } else {
00515       exp = ilog2(mag) + 1;
00516       state_ptr->dq[0] = (dq >= 0) ?
00517           (exp << 6) + ((mag << 6) >> exp) :
00518           (exp << 6) + ((mag << 6) >> exp) - 0x400;
00519    }
00520 #endif
00521 
00522    state_ptr->sr[1] = state_ptr->sr[0];
00523 #ifdef NOT_BLI
00524    state_ptr->sr[0] = sr;
00525 #else
00526    /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
00527    if (sr == 0) {
00528       state_ptr->sr[0] = 0x20;
00529    } else if (sr > 0) {
00530       exp = ilog2(sr) + 1;
00531       state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
00532    } else if (sr > -0x8000) {
00533       mag = -sr;
00534       exp = ilog2(mag) + 1;
00535       state_ptr->sr[0] =  (exp << 6) + ((mag << 6) >> exp) - 0x400;
00536    } else
00537       state_ptr->sr[0] = 0x20 - 0x400;
00538 #endif
00539 
00540    /* DELAY A */
00541    state_ptr->pk[1] = state_ptr->pk[0];
00542    state_ptr->pk[0] = pk0;
00543 
00544    /* TONE */
00545    if (tr == 1)      /* this sample has been treated as data */
00546       state_ptr->td = 0;   /* next one will be treated as voice */
00547    else if (a2p < -11776)  /* small sample-to-sample correlation */
00548       state_ptr->td = 1;   /* signal may be data */
00549    else           /* signal is voice */
00550       state_ptr->td = 0;
00551 
00552    /*
00553     * Adaptation speed control.
00554     */
00555    state_ptr->dms += (fi - state_ptr->dms) >> 5;      /* FILTA */
00556    state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);   /* FILTB */
00557 
00558    if (tr == 1)
00559       state_ptr->ap = 256;
00560    else if (y < 1536)               /* SUBTC */
00561       state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
00562    else if (state_ptr->td == 1)
00563       state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
00564    else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
00565        (state_ptr->dml >> 3))
00566       state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
00567    else
00568       state_ptr->ap += (-state_ptr->ap) >> 4;
00569 }
00570 
00571 /*
00572  * g726_decode()
00573  *
00574  * Description:
00575  *
00576  * Decodes a 4-bit code of G.726-32 encoded data of i and
00577  * returns the resulting linear PCM, A-law or u-law value.
00578  * return -1 for unknown out_coding value.
00579  */
00580 static int g726_decode(int i, struct g726_state *state_ptr)
00581 {
00582    int      sezi, sez, se; /* ACCUM */
00583    int      y;       /* MIX */
00584    int      sr;         /* ADDB */
00585    int      dq;
00586    int      dqsez;
00587 
00588    i &= 0x0f;        /* mask to get proper bits */
00589 #ifdef NOT_BLI
00590    sezi = predictor_zero(state_ptr);
00591    sez = sezi;
00592    se = sezi + predictor_pole(state_ptr); /* estimated signal */
00593 #else
00594    sezi = predictor_zero(state_ptr);
00595    sez = sezi >> 1;
00596    se = (sezi + predictor_pole(state_ptr)) >> 1;   /* estimated signal */
00597 #endif
00598 
00599    y = step_size(state_ptr);  /* dynamic quantizer step size */
00600 
00601    dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized diff. */
00602 
00603 #ifdef NOT_BLI
00604    sr = se + dq;           /* reconst. signal */
00605    dqsez = dq + sez;       /* pole prediction diff. */
00606 #else
00607    sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;   /* reconst. signal */
00608    dqsez = sr - se + sez;     /* pole prediction diff. */
00609 #endif
00610 
00611    update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
00612 
00613 #ifdef NOT_BLI
00614    return (sr >> 10);   /* sr was 26-bit dynamic range */
00615 #else
00616    return (sr << 2); /* sr was 14-bit dynamic range */
00617 #endif
00618 }
00619 
00620 /*
00621  * g726_encode()
00622  *
00623  * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
00624  * the resulting code. -1 is returned for unknown input coding value.
00625  */
00626 static int g726_encode(int sl, struct g726_state *state_ptr)
00627 {
00628    int      sezi, se, sez;    /* ACCUM */
00629    int      d;       /* SUBTA */
00630    int      sr;         /* ADDB */
00631    int      y;       /* MIX */
00632    int      dqsez;         /* ADDC */
00633    int      dq, i;
00634 
00635 #ifdef NOT_BLI
00636    sl <<= 10;        /* 26-bit dynamic range */
00637 
00638    sezi = predictor_zero(state_ptr);
00639    sez = sezi;
00640    se = sezi + predictor_pole(state_ptr); /* estimated signal */
00641 #else
00642    sl >>= 2;         /* 14-bit dynamic range */
00643 
00644    sezi = predictor_zero(state_ptr);
00645    sez = sezi >> 1;
00646    se = (sezi + predictor_pole(state_ptr)) >> 1;   /* estimated signal */
00647 #endif
00648 
00649    d = sl - se;            /* estimation difference */
00650 
00651    /* quantize the prediction difference */
00652    y = step_size(state_ptr);     /* quantizer step size */
00653 #ifdef NOT_BLI
00654    d /= 0x1000;
00655 #endif
00656    i = quantize(d, y, qtab_721, 7); /* i = G726 code */
00657 
00658    dq = reconstruct(i & 8, _dqlntab[i], y);  /* quantized est diff */
00659 
00660 #ifdef NOT_BLI
00661    sr = se + dq;           /* reconst. signal */
00662    dqsez = dq + sez;       /* pole prediction diff. */
00663 #else
00664    sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;   /* reconst. signal */
00665    dqsez = sr - se + sez;        /* pole prediction diff. */
00666 #endif
00667 
00668    update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
00669 
00670    return (i);
00671 }
00672 
00673 /*
00674  * Private workspace for translating signed linear signals to G726.
00675  * Don't bother to define two distinct structs.
00676  */
00677 
00678 struct g726_coder_pvt {
00679    /* buffer any odd byte in input - 0x80 + (value & 0xf) if present */
00680    unsigned char next_flag;
00681    struct g726_state g726;
00682 };
00683 
00684 /*! \brief init a new instance of g726_coder_pvt. */
00685 static int lintog726_new(struct ast_trans_pvt *pvt)
00686 {
00687    struct g726_coder_pvt *tmp = pvt->pvt;
00688 
00689    g726_init_state(&tmp->g726);
00690 
00691    return 0;
00692 }
00693 
00694 /*! \brief decode packed 4-bit G726 values (AAL2 packing) and store in buffer. */
00695 static int g726aal2tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
00696 {
00697    struct g726_coder_pvt *tmp = pvt->pvt;
00698    unsigned char *src = f->data.ptr;
00699    int16_t *dst = pvt->outbuf.i16 + pvt->samples;
00700    unsigned int i;
00701 
00702    for (i = 0; i < f->datalen; i++) {
00703       *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
00704       *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
00705    }
00706 
00707    pvt->samples += f->samples;
00708    pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
00709 
00710    return 0;
00711 }
00712 
00713 /*! \brief compress and store data (4-bit G726 samples, AAL2 packing) in outbuf */
00714 static int lintog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00715 {
00716    struct g726_coder_pvt *tmp = pvt->pvt;
00717    int16_t *src = f->data.ptr;
00718    unsigned int i;
00719 
00720    for (i = 0; i < f->samples; i++) {
00721       unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
00722 
00723       if (tmp->next_flag & 0x80) {  /* merge with leftover sample */
00724          pvt->outbuf.c[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
00725          pvt->samples += 2;   /* 2 samples per byte */
00726          tmp->next_flag = 0;
00727       } else {
00728          tmp->next_flag = 0x80 | d;
00729       }
00730    }
00731 
00732    return 0;
00733 }
00734 
00735 /*! \brief decode packed 4-bit G726 values (RFC3551 packing) and store in buffer. */
00736 static int g726tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
00737 {
00738    struct g726_coder_pvt *tmp = pvt->pvt;
00739    unsigned char *src = f->data.ptr;
00740    int16_t *dst = pvt->outbuf.i16 + pvt->samples;
00741    unsigned int i;
00742 
00743    for (i = 0; i < f->datalen; i++) {
00744       *dst++ = g726_decode(src[i] & 0x0f, &tmp->g726);
00745       *dst++ = g726_decode((src[i] >> 4) & 0xf, &tmp->g726);
00746    }
00747 
00748    pvt->samples += f->samples;
00749    pvt->datalen += 2 * f->samples; /* 2 bytes/sample */
00750 
00751    return 0;
00752 }
00753 
00754 /*! \brief compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf */
00755 static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00756 {
00757    struct g726_coder_pvt *tmp = pvt->pvt;
00758    int16_t *src = f->data.ptr;
00759    unsigned int i;
00760 
00761    for (i = 0; i < f->samples; i++) {
00762       unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
00763 
00764       if (tmp->next_flag & 0x80) {  /* merge with leftover sample */
00765          pvt->outbuf.c[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
00766          pvt->samples += 2;   /* 2 samples per byte */
00767          tmp->next_flag = 0;
00768       } else {
00769          tmp->next_flag = 0x80 | d;
00770       }
00771    }
00772 
00773    return 0;
00774 }
00775 
00776 static struct ast_translator g726tolin = {
00777    .name = "g726tolin",
00778    .srcfmt = AST_FORMAT_G726,
00779    .dstfmt = AST_FORMAT_SLINEAR,
00780    .newpvt = lintog726_new,   /* same for both directions */
00781    .framein = g726tolin_framein,
00782    .sample = g726_sample,
00783    .desc_size = sizeof(struct g726_coder_pvt),
00784    .buffer_samples = BUFFER_SAMPLES,
00785    .buf_size = BUFFER_SAMPLES * 2,
00786 };
00787 
00788 static struct ast_translator lintog726 = {
00789    .name = "lintog726",
00790    .srcfmt = AST_FORMAT_SLINEAR,
00791    .dstfmt = AST_FORMAT_G726,
00792    .newpvt = lintog726_new,   /* same for both directions */
00793    .framein = lintog726_framein,
00794    .sample = slin8_sample,
00795    .desc_size = sizeof(struct g726_coder_pvt),
00796    .buffer_samples = BUFFER_SAMPLES,
00797    .buf_size = BUFFER_SAMPLES/2,
00798 };
00799 
00800 static struct ast_translator g726aal2tolin = {
00801    .name = "g726aal2tolin",
00802    .srcfmt = AST_FORMAT_G726_AAL2,
00803    .dstfmt = AST_FORMAT_SLINEAR,
00804    .newpvt = lintog726_new,   /* same for both directions */
00805    .framein = g726aal2tolin_framein,
00806    .sample = g726_sample,
00807    .desc_size = sizeof(struct g726_coder_pvt),
00808    .buffer_samples = BUFFER_SAMPLES,
00809    .buf_size = BUFFER_SAMPLES * 2,
00810 };
00811 
00812 static struct ast_translator lintog726aal2 = {
00813    .name = "lintog726aal2",
00814    .srcfmt = AST_FORMAT_SLINEAR,
00815    .dstfmt = AST_FORMAT_G726_AAL2,
00816    .newpvt = lintog726_new,   /* same for both directions */
00817    .framein = lintog726aal2_framein,
00818    .sample = slin8_sample,
00819    .desc_size = sizeof(struct g726_coder_pvt),
00820    .buffer_samples = BUFFER_SAMPLES,
00821    .buf_size = BUFFER_SAMPLES / 2,
00822 };
00823 
00824 static int reload(void)
00825 {
00826    return AST_MODULE_LOAD_SUCCESS;
00827 }
00828 
00829 static int unload_module(void)
00830 {
00831    int res = 0;
00832 
00833    res |= ast_unregister_translator(&g726tolin);
00834    res |= ast_unregister_translator(&lintog726);
00835 
00836    res |= ast_unregister_translator(&g726aal2tolin);
00837    res |= ast_unregister_translator(&lintog726aal2);
00838 
00839    return res;
00840 }
00841 
00842 static int load_module(void)
00843 {
00844    int res = 0;
00845 
00846    res |= ast_register_translator(&g726tolin);
00847    res |= ast_register_translator(&lintog726);
00848 
00849    res |= ast_register_translator(&g726aal2tolin);
00850    res |= ast_register_translator(&lintog726aal2);
00851 
00852    if (res) {
00853       unload_module();
00854       return AST_MODULE_LOAD_FAILURE;
00855    }  
00856 
00857    return AST_MODULE_LOAD_SUCCESS;
00858 }
00859 
00860 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.726-32kbps G726 Transcoder",
00861       .load = load_module,
00862       .unload = unload_module,
00863       .reload = reload,
00864           );

Generated on Mon Mar 19 11:30:26 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7