Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:53 2009

## 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 - 2005, Digium, Inc.
    00005  *
    00006  * Mark Spencer <markster@digium.com>
    00007  *
    00008  * Based on frompcm.c and topcm.c from the Emiliano MIPL browser/
    00009  * interpreter.  See http://www.bsdtelephony.com.mx
    00010  *
    00011  * See http://www.asterisk.org for more information about
    00012  * the Asterisk project. Please do not directly contact
    00013  * any of the maintainers of this project for assistance;
    00014  * the project provides a web site, mailing lists and IRC
    00015  * channels for your use.
    00016  *
    00017  * This program is free software, distributed under the terms of
    00018  * the GNU General Public License Version 2. See the LICENSE file
    00019  * at the top of the source tree.
    00020  */
    00021
    00022
    00023 /*! \file
    00024  *
    00025  * \brief codec_g726.c - translate between signed linear and ITU G.726-32kbps
    00026  *
    00027  * \ingroup codecs
    00028  */
    00029
    00030 #include <fcntl.h>
    00031 #include <netinet/in.h>
    00032 #include <stdio.h>
    00033 #include <stdlib.h>
    00034 #include <string.h>
    00035 #include <unistd.h>
    00036
    00037 #include "asterisk.h"
    00038
    00039 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    00040
    00041 #include "asterisk/lock.h"
    00042 #include "asterisk/logger.h"
    00043 #include "asterisk/module.h"
    00044 #include "asterisk/config.h"
    00045 #include "asterisk/options.h"
    00046 #include "asterisk/translate.h"
    00047 #include "asterisk/channel.h"
    00048
    00049 #define WANT_ASM
    00050 #include "log2comp.h"
    00051
    00052 /* define NOT_BLI to use a faster but not bit-level identical version */
    00053 /* #define NOT_BLI */
    00054
    00055 #if defined(NOT_BLI)
    00056 #  if defined(_MSC_VER)
    00057 typedef __int64 sint64;
    00058 #  elif defined(__GNUC__)
    00059 typedef long long sint64;
    00060 #  else
    00061 #     error 64-bit integer type is not defined for your compiler/platform
    00062 #  endif
    00063 #endif
    00064
    00065 #define BUFFER_SIZE   8096 /* size for the translation buffers */
    00066 #define BUF_SHIFT 5
    00067
    00068 AST_MUTEX_DEFINE_STATIC(localuser_lock);
    00069 static int localusecnt = 0;
    00070
    00071 static char *tdesc = "ITU G.726-32kbps G726 Transcoder";
    00072
    00073 static int useplc = 0;
    00074
    00075 /* Sample frame data */
    00076
    00077 #include "slin_g726_ex.h"
    00078 #include "g726_slin_ex.h"
    00079
    00080 /*
    00081  * The following is the definition of the state structure
    00082  * used by the G.721/G.723 encoder and decoder to preserve their internal
    00083  * state between successive calls.  The meanings of the majority
    00084  * of the state structure fields are explained in detail in the
    00085  * CCITT Recommendation G.721.  The field names are essentially indentical
    00086  * to variable names in the bit level description of the coding algorithm
    00087  * included in this Recommendation.
    00088  */
    00089 struct g726_state {
    00090    long yl; /* Locked or steady state step size multiplier. */
    00091    int yu;     /* Unlocked or non-steady state step size multiplier. */
    00092    int dms; /* Short term energy estimate. */
    00093    int dml; /* Long term energy estimate. */
    00094    int ap;     /* Linear weighting coefficient of 'yl' and 'yu'. */
    00095
    00096    int a[2];   /* Coefficients of pole portion of prediction filter.
    00097              * stored as fixed-point 1==2^14 */
    00098    int b[6];   /* Coefficients of zero portion of prediction filter.
    00099              * stored as fixed-point 1==2^14 */
    00100    int pk[2];  /* Signs of previous two samples of a partially
    00101           * reconstructed signal.
    00102           */
    00103    int dq[6];  /* Previous 6 samples of the quantized difference signal
    00104              * stored as fixed point 1==2^12,
    00105              * or in internal floating point format */
    00106    int sr[2];  /* Previous 2 samples of the quantized difference signal
    00107              * stored as fixed point 1==2^12,
    00108              * or in internal floating point format */
    00109    int td;  /* delayed tone detect, new in 1988 version */
    00110 };
    00111
    00112
    00113
    00114 static int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
    00115 /*
    00116  * Maps G.721 code word to reconstructed scale factor normalized log
    00117  * magnitude values.
    00118  */
    00119 static int _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
    00120             425, 373, 323, 273, 213, 135, 4, -2048};
    00121
    00122 /* Maps G.721 code word to log of scale factor multiplier. */
    00123 static int _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
    00124             1122, 355, 198, 112, 64, 41, 18, -12};
    00125 /*
    00126  * Maps G.721 code words to a set of values whose long and short
    00127  * term averages are computed and then compared to give an indication
    00128  * how stationary (steady state) the signal is.
    00129  */
    00130 static int _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
    00131             0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
    00132
    00133 /* Deprecated
    00134 static int power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
    00135          0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
    00136 */
    00137
    00138 /*
    00139  * g72x_init_state()
    00140  *
    00141  * This routine initializes and/or resets the g726_state structure
    00142  * pointed to by 'state_ptr'.
    00143  * All the initial state values are specified in the CCITT G.721 document.
    00144  */
    00145 static void g726_init_state(struct g726_state *state_ptr)
    00146 {
    00147    int      cnta;
    00148
    00149    state_ptr->yl = 34816;
    00150    state_ptr->yu = 544;
    00151    state_ptr->dms = 0;
    00152    state_ptr->dml = 0;
    00153    state_ptr->ap = 0;
    00154    for (cnta = 0; cnta < 2; cnta++)
    00155    {
    00156       state_ptr->a[cnta] = 0;
    00157       state_ptr->pk[cnta] = 0;
    00158 #ifdef NOT_BLI
    00159       state_ptr->sr[cnta] = 1;
    00160 #else
    00161       state_ptr->sr[cnta] = 32;
    00162 #endif
    00163    }
    00164    for (cnta = 0; cnta < 6; cnta++)
    00165    {
    00166       state_ptr->b[cnta] = 0;
    00167 #ifdef NOT_BLI
    00168       state_ptr->dq[cnta] = 1;
    00169 #else
    00170       state_ptr->dq[cnta] = 32;
    00171 #endif
    00172    }
    00173    state_ptr->td = 0;
    00174 }
    00175
    00176 /*
    00177  * quan()
    00178  *
    00179  * quantizes the input val against the table of integers.
    00180  * It returns i if table[i - 1] <= val < table[i].
    00181  *
    00182  * Using linear search for simple coding.
    00183  */
    00184 static int quan(int val, int *table, int size)
    00185 {
    00186    int      i;
    00187
    00188    for (i = 0; i < size && val >= *table; ++i, ++table)
    00189       ;
    00190    return (i);
    00191 }
    00192
    00193 #ifdef NOT_BLI /* faster non-identical version */
    00194
    00195 /*
    00196  * predictor_zero()
    00197  *
    00198  * computes the estimated signal from 6-zero predictor.
    00199  *
    00200  */
    00201 static int predictor_zero(struct g726_state *state_ptr)
    00202 {  /* divide by 2 is necessary here to handle negative numbers correctly */
    00203    int i;
    00204    sint64 sezi;
    00205    for (sezi = 0, i = 0; i < 6; i++)         /* ACCUM */
    00206       sezi += (sint64)state_ptr->b[i] * state_ptr->dq[i];
    00207    return (int)(sezi >> 13) / 2 /* 2^14 */;
    00208 }
    00209
    00210 /*
    00211  * predictor_pole()
    00212  *
    00213  * computes the estimated signal from 2-pole predictor.
    00214  *
    00215  */
    00216 static int predictor_pole(struct g726_state *state_ptr)
    00217 {  /* divide by 2 is necessary here to handle negative numbers correctly */
    00218    return (int)(((sint64)state_ptr->a[1] * state_ptr->sr[1] +
    00219                  (sint64)state_ptr->a[0] * state_ptr->sr[0]) >> 13) / 2 /* 2^14 */;
    00220 }
    00221
    00222 #else /* NOT_BLI - identical version */
    00223 /*
    00224  * fmult()
    00225  *
    00226  * returns the integer product of the fixed-point number "an" (1==2^12) and
    00227  * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
    00228  */
    00229 static int fmult(int an, int srn)
    00230 {
    00231    int      anmag, anexp, anmant;
    00232    int      wanexp, wanmant;
    00233    int      retval;
    00234
    00235    anmag = (an > 0) ? an : ((-an) & 0x1FFF);
    00236    anexp = ilog2(anmag) - 5;
    00237    anmant = (anmag == 0) ? 32 :
    00238        (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
    00239    wanexp = anexp + ((srn >> 6) & 0xF) - 13;
    00240
    00241    wanmant = (anmant * (srn & 077) + 0x30) >> 4;
    00242    retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
    00243        (wanmant >> -wanexp);
    00244
    00245    return (((an ^ srn) < 0) ? -retval : retval);
    00246 }
    00247
    00248 static int predictor_zero(struct g726_state *state_ptr)
    00249 {
    00250    int      i;
    00251    int      sezi;
    00252    for (sezi = 0, i = 0; i < 6; i++)         /* ACCUM */
    00253       sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
    00254    return sezi;
    00255 }
    00256
    00257 static int predictor_pole(struct g726_state *state_ptr)
    00258 {
    00259    return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
    00260          fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
    00261 }
    00262
    00263 #endif /* NOT_BLI */
    00264
    00265 /*
    00266  * step_size()
    00267  *
    00268  * computes the quantization step size of the adaptive quantizer.
    00269  *
    00270  */
    00271 static int step_size(struct g726_state *state_ptr)
    00272 {
    00273    int      y;
    00274    int      dif;
    00275    int      al;
    00276
    00277    if (state_ptr->ap >= 256)
    00278       return (state_ptr->yu);
    00279    else {
    00280       y = state_ptr->yl >> 6;
    00281       dif = state_ptr->yu - y;
    00282       al = state_ptr->ap >> 2;
    00283       if (dif > 0)
    00284          y += (dif * al) >> 6;
    00285       else if (dif < 0)
    00286          y += (dif * al + 0x3F) >> 6;
    00287       return (y);
    00288    }
    00289 }
    00290
    00291 /*
    00292  * quantize()
    00293  *
    00294  * Given a raw sample, 'd', of the difference signal and a
    00295  * quantization step size scale factor, 'y', this routine returns the
    00296  * ADPCM codeword to which that sample gets quantized.  The step
    00297  * size scale factor division operation is done in the log base 2 domain
    00298  * as a subtraction.
    00299  */
    00300 static int quantize(
    00301    int      d, /* Raw difference signal sample */
    00302    int      y, /* Step size multiplier */
    00303    int      *table,  /* quantization table */
    00304    int      size) /* table size of integers */
    00305 {
    00306    int      dqm;  /* Magnitude of 'd' */
    00307    int      exp;  /* Integer part of base 2 log of 'd' */
    00308    int      mant; /* Fractional part of base 2 log */
    00309    int      dl;      /* Log of magnitude of 'd' */
    00310    int      dln;  /* Step size scale factor normalized log */
    00311    int      i;
    00312
    00313    /*
    00314     * LOG
    00315     *
    00316     * Compute base 2 log of 'd', and store in 'dl'.
    00317     */
    00318    dqm = abs(d);
    00319    exp = ilog2(dqm);
    00320    if (exp < 0)
    00321       exp = 0;
    00322    mant = ((dqm << 7) >> exp) & 0x7F;  /* Fractional portion. */
    00323    dl = (exp << 7) | mant;
    00324
    00325    /*
    00326     * SUBTB
    00327     *
    00328     * "Divide" by step size multiplier.
    00329     */
    00330    dln = dl - (y >> 2);
    00331
    00332    /*
    00333     * QUAN
    00334     *
    00335     * Obtain codword i for 'd'.
    00336     */
    00337    i = quan(dln, table, size);
    00338    if (d < 0)        /* take 1's complement of i */
    00339       return ((size << 1) + 1 - i);
    00340    else if (i == 0)     /* take 1's complement of 0 */
    00341       return ((size << 1) + 1); /* new in 1988 */
    00342    else
    00343       return (i);
    00344 }
    00345
    00346 /*
    00347  * reconstruct()
    00348  *
    00349  * Returns reconstructed difference signal 'dq' obtained from
    00350  * codeword 'i' and quantization step size scale factor 'y'.
    00351  * Multiplication is performed in log base 2 domain as addition.
    00352  */
    00353 static int reconstruct(
    00354    int      sign, /* 0 for non-negative value */
    00355    int      dqln, /* G.72x codeword */
    00356    int      y) /* Step size multiplier */
    00357 {
    00358    int      dql;  /* Log of 'dq' magnitude */
    00359    int      dex;  /* Integer part of log */
    00360    int      dqt;
    00361    int      dq;   /* Reconstructed difference signal sample */
    00362
    00363    dql = dqln + (y >> 2);  /* ADDA */
    00364
    00365    if (dql < 0) {
    00366 #ifdef NOT_BLI
    00367       return (sign) ? -1 : 1;
    00368 #else
    00369       return (sign) ? -0x8000 : 0;
    00370 #endif
    00371    } else {    /* ANTILOG */
    00372       dex = (dql >> 7) & 15;
    00373       dqt = 128 + (dql & 127);
    00374 #ifdef NOT_BLI
    00375       dq = ((dqt << 19) >> (14 - dex));
    00376       return (sign) ? -dq : dq;
    00377 #else
    00378       dq = (dqt << 7) >> (14 - dex);
    00379       return (sign) ? (dq - 0x8000) : dq;
    00380 #endif
    00381    }
    00382 }
    00383
    00384 /*
    00385  * update()
    00386  *
    00387  * updates the state variables for each output code
    00388  */
    00389 static void update(
    00390    int      code_size,  /* distinguish 723_40 with others */
    00391    int      y,    /* quantizer step size */
    00392    int      wi,      /* scale factor multiplier */
    00393    int      fi,      /* for long/short term energies */
    00394    int      dq,      /* quantized prediction difference */
    00395    int      sr,      /* reconstructed signal */
    00396    int      dqsez,      /* difference from 2-pole predictor */
    00397    struct g726_state *state_ptr) /* coder state pointer */
    00398 {
    00399    int      cnt;
    00400    int      mag;     /* Adaptive predictor, FLOAT A */
    00401 #ifndef NOT_BLI
    00402    int      exp;
    00403 #endif
    00404    int      a2p=0;      /* LIMC */
    00405    int      a1ul;    /* UPA1 */
    00406    int      pks1;    /* UPA2 */
    00407    int      fa1;
    00408    int      tr;         /* tone/transition detector */
    00409    int      ylint, thr2, dqthr;
    00410    int      ylfrac, thr1;
    00411    int      pk0;
    00412
    00413    pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
    00414
    00415 #ifdef NOT_BLI
    00416    mag = abs(dq / 0x1000); /* prediction difference magnitude */
    00417 #else
    00418    mag = dq & 0x7FFF;      /* prediction difference magnitude */
    00419 #endif
    00420    /* TRANS */
    00421    ylint = state_ptr->yl >> 15;  /* exponent part of yl */
    00422    ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
    00423    thr1 = (32 + ylfrac) << ylint;      /* threshold */
    00424    thr2 = (ylint > 9) ? 31 << 10 : thr1;  /* limit thr2 to 31 << 10 */
    00425    dqthr = (thr2 + (thr2 >> 1)) >> 1;  /* dqthr = 0.75 * thr2 */
    00426    if (state_ptr->td == 0)    /* signal supposed voice */
    00427       tr = 0;
    00428    else if (mag <= dqthr)     /* supposed data, but small mag */
    00429       tr = 0;        /* treated as voice */
    00430    else           /* signal is data (modem) */
    00431       tr = 1;
    00432
    00433    /*
    00434     * Quantizer scale factor adaptation.
    00435     */
    00436
    00437    /* FUNCTW & FILTD & DELAY */
    00438    /* update non-steady state step size multiplier */
    00439    state_ptr->yu = y + ((wi - y) >> 5);
    00440
    00441    /* LIMB */
    00442    if (state_ptr->yu < 544)   /* 544 <= yu <= 5120 */
    00443       state_ptr->yu = 544;
    00444    else if (state_ptr->yu > 5120)
    00445       state_ptr->yu = 5120;
    00446
    00447    /* FILTE & DELAY */
    00448    /* update steady state step size multiplier */
    00449    state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
    00450
    00451    /*
    00452     * Adaptive predictor coefficients.
    00453     */
    00454    if (tr == 1) {       /* reset a's and b's for modem signal */
    00455       state_ptr->a[0] = 0;
    00456       state_ptr->a[1] = 0;
    00457       state_ptr->b[0] = 0;
    00458       state_ptr->b[1] = 0;
    00459       state_ptr->b[2] = 0;
    00460       state_ptr->b[3] = 0;
    00461       state_ptr->b[4] = 0;
    00462       state_ptr->b[5] = 0;
    00463    } else {       /* update a's and b's */
    00464       pks1 = pk0 ^ state_ptr->pk[0];      /* UPA2 */
    00465
    00466       /* update predictor pole a[1] */
    00467       a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
    00468       if (dqsez != 0) {
    00469          fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
    00470          if (fa1 < -8191)  /* a2p = function of fa1 */
    00471             a2p -= 0x100;
    00472          else if (fa1 > 8191)
    00473             a2p += 0xFF;
    00474          else
    00475             a2p += fa1 >> 5;
    00476
    00477          if (pk0 ^ state_ptr->pk[1])
    00478             /* LIMC */
    00479             if (a2p <= -12160)
    00480                a2p = -12288;
    00481             else if (a2p >= 12416)
    00482                a2p = 12288;
    00483             else
    00484                a2p -= 0x80;
    00485          else if (a2p <= -12416)
    00486             a2p = -12288;
    00487          else if (a2p >= 12160)
    00488             a2p = 12288;
    00489          else
    00490             a2p += 0x80;
    00491       }
    00492
    00493       /* TRIGB & DELAY */
    00494       state_ptr->a[1] = a2p;
    00495
    00496       /* UPA1 */
    00497       /* update predictor pole a[0] */
    00498       state_ptr->a[0] -= state_ptr->a[0] >> 8;
    00499       if (dqsez != 0) {
    00500          if (pks1 == 0)
    00501             state_ptr->a[0] += 192;
    00502          else
    00503             state_ptr->a[0] -= 192;
    00504       }
    00505       /* LIMD */
    00506       a1ul = 15360 - a2p;
    00507       if (state_ptr->a[0] < -a1ul)
    00508          state_ptr->a[0] = -a1ul;
    00509       else if (state_ptr->a[0] > a1ul)
    00510          state_ptr->a[0] = a1ul;
    00511
    00512       /* UPB : update predictor zeros b[6] */
    00513       for (cnt = 0; cnt < 6; cnt++) {
    00514          if (code_size == 5)     /* for 40Kbps G.723 */
    00515             state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
    00516          else        /* for G.721 and 24Kbps G.723 */
    00517             state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
    00518          if (mag)
    00519          {  /* XOR */
    00520             if ((dq ^ state_ptr->dq[cnt]) >= 0)
    00521                state_ptr->b[cnt] += 128;
    00522             else
    00523                state_ptr->b[cnt] -= 128;
    00524          }
    00525       }
    00526    }
    00527
    00528    for (cnt = 5; cnt > 0; cnt--)
    00529       state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
    00530 #ifdef NOT_BLI
    00531    state_ptr->dq[0] = dq;
    00532 #else
    00533    /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
    00534    if (mag == 0) {
    00535       state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0x20 - 0x400;
    00536    } else {
    00537       exp = ilog2(mag) + 1;
    00538       state_ptr->dq[0] = (dq >= 0) ?
    00539           (exp << 6) + ((mag << 6) >> exp) :
    00540           (exp << 6) + ((mag << 6) >> exp) - 0x400;
    00541    }
    00542 #endif
    00543
    00544    state_ptr->sr[1] = state_ptr->sr[0];
    00545 #ifdef NOT_BLI
    00546    state_ptr->sr[0] = sr;
    00547 #else
    00548    /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
    00549    if (sr == 0) {
    00550       state_ptr->sr[0] = 0x20;
    00551    } else if (sr > 0) {
    00552       exp = ilog2(sr) + 1;
    00553       state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
    00554    } else if (sr > -0x8000) {
    00555       mag = -sr;
    00556       exp = ilog2(mag) + 1;
    00557       state_ptr->sr[0] =  (exp << 6) + ((mag << 6) >> exp) - 0x400;
    00558    } else
    00559       state_ptr->sr[0] = 0x20 - 0x400;
    00560 #endif
    00561
    00562    /* DELAY A */
    00563    state_ptr->pk[1] = state_ptr->pk[0];
    00564    state_ptr->pk[0] = pk0;
    00565
    00566    /* TONE */
    00567    if (tr == 1)      /* this sample has been treated as data */
    00568       state_ptr->td = 0;   /* next one will be treated as voice */
    00569    else if (a2p < -11776)  /* small sample-to-sample correlation */
    00570       state_ptr->td = 1;   /* signal may be data */
    00571    else           /* signal is voice */
    00572       state_ptr->td = 0;
    00573
    00574    /*
    00575     * Adaptation speed control.
    00576     */
    00577    state_ptr->dms += (fi - state_ptr->dms) >> 5;      /* FILTA */
    00578    state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7);   /* FILTB */
    00579
    00580    if (tr == 1)
    00581       state_ptr->ap = 256;
    00582    else if (y < 1536)               /* SUBTC */
    00583       state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
    00584    else if (state_ptr->td == 1)
    00585       state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
    00586    else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
    00587        (state_ptr->dml >> 3))
    00588       state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
    00589    else
    00590       state_ptr->ap += (-state_ptr->ap) >> 4;
    00591 }
    00592
    00593 /*
    00594  * g726_decode()
    00595  *
    00596  * Description:
    00597  *
    00598  * Decodes a 4-bit code of G.726-32 encoded data of i and
    00599  * returns the resulting linear PCM, A-law or u-law value.
    00600  * return -1 for unknown out_coding value.
    00601  */
    00602 static int g726_decode(int i, struct g726_state *state_ptr)
    00603 {
    00604    int      sezi, sez, se; /* ACCUM */
    00605    int      y;       /* MIX */
    00606    int      sr;         /* ADDB */
    00607    int      dq;
    00608    int      dqsez;
    00609
    00610    i &= 0x0f;        /* mask to get proper bits */
    00611 #ifdef NOT_BLI
    00612    sezi = predictor_zero(state_ptr);
    00613    sez = sezi;
    00614    se = sezi + predictor_pole(state_ptr); /* estimated signal */
    00615 #else
    00616    sezi = predictor_zero(state_ptr);
    00617    sez = sezi >> 1;
    00618    se = (sezi + predictor_pole(state_ptr)) >> 1;   /* estimated signal */
    00619 #endif
    00620
    00621    y = step_size(state_ptr);  /* dynamic quantizer step size */
    00622
    00623    dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized diff. */
    00624
    00625 #ifdef NOT_BLI
    00626    sr = se + dq;           /* reconst. signal */
    00627    dqsez = dq + sez;       /* pole prediction diff. */
    00628 #else
    00629    sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;   /* reconst. signal */
    00630    dqsez = sr - se + sez;     /* pole prediction diff. */
    00631 #endif
    00632
    00633    update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
    00634
    00635 #ifdef NOT_BLI
    00636    return (sr >> 10);   /* sr was 26-bit dynamic range */
    00637 #else
    00638    return (sr << 2); /* sr was 14-bit dynamic range */
    00639 #endif
    00640 }
    00641
    00642 /*
    00643  * g726_encode()
    00644  *
    00645  * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
    00646  * the resulting code. -1 is returned for unknown input coding value.
    00647  */
    00648 static int g726_encode(int sl, struct g726_state *state_ptr)
    00649 {
    00650    int      sezi, se, sez;    /* ACCUM */
    00651    int      d;       /* SUBTA */
    00652    int      sr;         /* ADDB */
    00653    int      y;       /* MIX */
    00654    int      dqsez;         /* ADDC */
    00655    int      dq, i;
    00656
    00657 #ifdef NOT_BLI
    00658    sl <<= 10;        /* 26-bit dynamic range */
    00659
    00660    sezi = predictor_zero(state_ptr);
    00661    sez = sezi;
    00662    se = sezi + predictor_pole(state_ptr); /* estimated signal */
    00663 #else
    00664    sl >>= 2;         /* 14-bit dynamic range */
    00665
    00666    sezi = predictor_zero(state_ptr);
    00667    sez = sezi >> 1;
    00668    se = (sezi + predictor_pole(state_ptr)) >> 1;   /* estimated signal */
    00669 #endif
    00670
    00671    d = sl - se;            /* estimation difference */
    00672
    00673    /* quantize the prediction difference */
    00674    y = step_size(state_ptr);     /* quantizer step size */
    00675 #ifdef NOT_BLI
    00676    d /= 0x1000;
    00677 #endif
    00678    i = quantize(d, y, qtab_721, 7); /* i = G726 code */
    00679
    00680    dq = reconstruct(i & 8, _dqlntab[i], y);  /* quantized est diff */
    00681
    00682 #ifdef NOT_BLI
    00683    sr = se + dq;           /* reconst. signal */
    00684    dqsez = dq + sez;       /* pole prediction diff. */
    00685 #else
    00686    sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq;   /* reconst. signal */
    00687    dqsez = sr - se + sez;        /* pole prediction diff. */
    00688 #endif
    00689
    00690    update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
    00691
    00692    return (i);
    00693 }
    00694
    00695 /*
    00696  * Private workspace for translating signed linear signals to G726.
    00697  */
    00698
    00699 struct g726_encoder_pvt
    00700 {
    00701   struct ast_frame f;
    00702   char offset[AST_FRIENDLY_OFFSET];   /* Space to build offset */
    00703   unsigned char outbuf[BUFFER_SIZE];  /* Encoded G726, two nibbles to a word */
    00704   unsigned char next_flag;
    00705   struct g726_state g726;
    00706   int tail;
    00707 };
    00708
    00709 /*
    00710  * Private workspace for translating G726 signals to signed linear.
    00711  */
    00712
    00713 struct g726_decoder_pvt
    00714 {
    00715   struct ast_frame f;
    00716   char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
    00717   short outbuf[BUFFER_SIZE];  /* Decoded signed linear values */
    00718   struct g726_state g726;
    00719   int tail;
    00720   plc_state_t plc;
    00721 };
    00722
    00723 /*
    00724  * G726ToLin_New
    00725  *  Create a new instance of g726_decoder_pvt.
    00726  *
    00727  * Results:
    00728  *  Returns a pointer to the new instance.
    00729  *
    00730  * Side effects:
    00731  *  None.
    00732  */
    00733
    00734 static struct ast_translator_pvt *
    00735 g726tolin_new (void)
    00736 {
    00737   struct g726_decoder_pvt *tmp;
    00738   tmp = malloc (sizeof (struct g726_decoder_pvt));
    00739   if (tmp)
    00740     {
    00741      memset(tmp, 0, sizeof(*tmp));
    00742       tmp->tail = 0;
    00743       plc_init(&tmp->plc);
    00744       localusecnt++;
    00745      g726_init_state(&tmp->g726);
    00746       ast_update_use_count ();
    00747     }
    00748   return (struct ast_translator_pvt *) tmp;
    00749 }
    00750
    00751 /*
    00752  * LinToG726_New
    00753  *  Create a new instance of g726_encoder_pvt.
    00754  *
    00755  * Results:
    00756  *  Returns a pointer to the new instance.
    00757  *
    00758  * Side effects:
    00759  *  None.
    00760  */
    00761
    00762 static struct ast_translator_pvt *
    00763 lintog726_new (void)
    00764 {
    00765   struct g726_encoder_pvt *tmp;
    00766   tmp = malloc (sizeof (struct g726_encoder_pvt));
    00767   if (tmp)
    00768     {
    00769      memset(tmp, 0, sizeof(*tmp));
    00770       localusecnt++;
    00771       tmp->tail = 0;
    00772      g726_init_state(&tmp->g726);
    00773       ast_update_use_count ();
    00774     }
    00775   return (struct ast_translator_pvt *) tmp;
    00776 }
    00777
    00778 /*
    00779  * G726ToLin_FrameIn
    00780  *  Fill an input buffer with packed 4-bit G726 values if there is room
    00781  *  left.
    00782  *
    00783  * Results:
    00784  *  Foo
    00785  *
    00786  * Side effects:
    00787  *  tmp->tail is the number of packed values in the buffer.
    00788  */
    00789
    00790 static int
    00791 g726tolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
    00792 {
    00793   struct g726_decoder_pvt *tmp = (struct g726_decoder_pvt *) pvt;
    00794   unsigned char *b;
    00795   int x;
    00796
    00797   if(f->datalen == 0) { /* perform PLC with nominal framesize of 20ms/160 samples */
    00798         if((tmp->tail + 160) > BUFFER_SIZE) {
    00799             ast_log(LOG_WARNING, "Out of buffer space\n");
    00800             return -1;
    00801         }
    00802         if(useplc) {
    00803        plc_fillin(&tmp->plc, tmp->outbuf+tmp->tail, 160);
    00804        tmp->tail += 160;
    00805    }
    00806         return 0;
    00807   }
    00808
    00809   b = f->data;
    00810   for (x=0;x<f->datalen;x++) {
    00811    if (tmp->tail >= BUFFER_SIZE) {
    00812       ast_log(LOG_WARNING, "Out of buffer space!\n");
    00813       return -1;
    00814    }
    00815    tmp->outbuf[tmp->tail++] = g726_decode((b[x] >> 4) & 0xf, &tmp->g726);
    00816    if (tmp->tail >= BUFFER_SIZE) {
    00817       ast_log(LOG_WARNING, "Out of buffer space!\n");
    00818       return -1;
    00819    }
    00820    tmp->outbuf[tmp->tail++] = g726_decode(b[x] & 0x0f, &tmp->g726);
    00821   }
    00822
    00823   if(useplc) plc_rx(&tmp->plc, tmp->outbuf+tmp->tail-f->datalen*2, f->datalen*2);
    00824
    00825   return 0;
    00826 }
    00827
    00828 /*
    00829  * G726ToLin_FrameOut
    00830  *  Convert 4-bit G726 encoded signals to 16-bit signed linear.
    00831  *
    00832  * Results:
    00833  *  Converted signals are placed in tmp->f.data, tmp->f.datalen
    00834  *  and tmp->f.samples are calculated.
    00835  *
    00836  * Side effects:
    00837  *  None.
    00838  */
    00839
    00840 static struct ast_frame *
    00841 g726tolin_frameout (struct ast_translator_pvt *pvt)
    00842 {
    00843   struct g726_decoder_pvt *tmp = (struct g726_decoder_pvt *) pvt;
    00844
    00845   if (!tmp->tail)
    00846     return NULL;
    00847
    00848   tmp->f.frametype = AST_FRAME_VOICE;
    00849   tmp->f.subclass = AST_FORMAT_SLINEAR;
    00850   tmp->f.datalen = tmp->tail * 2;
    00851   tmp->f.samples = tmp->tail;
    00852   tmp->f.mallocd = 0;
    00853   tmp->f.offset = AST_FRIENDLY_OFFSET;
    00854   tmp->f.src = __PRETTY_FUNCTION__;
    00855   tmp->f.data = tmp->outbuf;
    00856   tmp->tail = 0;
    00857   return &tmp->f;
    00858 }
    00859
    00860 /*
    00861  * LinToG726_FrameIn
    00862  *  Fill an input buffer with 16-bit signed linear PCM values.
    00863  *
    00864  * Results:
    00865  *  None.
    00866  *
    00867  * Side effects:
    00868  *  tmp->tail is number of signal values in the input buffer.
    00869  */
    00870
    00871 static int
    00872 lintog726_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
    00873 {
    00874   struct g726_encoder_pvt *tmp = (struct g726_encoder_pvt *) pvt;
    00875   short *s = f->data;
    00876   int samples = f->datalen / 2;
    00877   int x;
    00878   for (x=0;x<samples;x++) {
    00879    if (tmp->next_flag & 0x80) {
    00880       if (tmp->tail >= BUFFER_SIZE) {
    00881          ast_log(LOG_WARNING, "Out of buffer space\n");
    00882          return -1;
    00883       }
    00884       tmp->outbuf[tmp->tail++] = ((tmp->next_flag & 0xf)<< 4) | g726_encode(s[x], &tmp->g726);
    00885       tmp->next_flag = 0;
    00886    } else {
    00887       tmp->next_flag = 0x80 | g726_encode(s[x], &tmp->g726);
    00888    }
    00889   }
    00890   return 0;
    00891 }
    00892
    00893 /*
    00894  * LinToG726_FrameOut
    00895  *  Convert a buffer of raw 16-bit signed linear PCM to a buffer
    00896  *  of 4-bit G726 packed two to a byte (Big Endian).
    00897  *
    00898  * Results:
    00899  *  Foo
    00900  *
    00901  * Side effects:
    00902  *  Leftover inbuf data gets packed, tail gets updated.
    00903  */
    00904
    00905 static struct ast_frame *
    00906 lintog726_frameout (struct ast_translator_pvt *pvt)
    00907 {
    00908   struct g726_encoder_pvt *tmp = (struct g726_encoder_pvt *) pvt;
    00909
    00910   if (!tmp->tail)
    00911    return NULL;
    00912   tmp->f.frametype = AST_FRAME_VOICE;
    00913   tmp->f.subclass = AST_FORMAT_G726;
    00914   tmp->f.samples = tmp->tail * 2;
    00915   tmp->f.mallocd = 0;
    00916   tmp->f.offset = AST_FRIENDLY_OFFSET;
    00917   tmp->f.src = __PRETTY_FUNCTION__;
    00918   tmp->f.data = tmp->outbuf;
    00919   tmp->f.datalen = tmp->tail;
    00920
    00921   tmp->tail = 0;
    00922   return &tmp->f;
    00923 }
    00924
    00925
    00926 /*
    00927  * G726ToLin_Sample
    00928  */
    00929
    00930 static struct ast_frame *
    00931 g726tolin_sample (void)
    00932 {
    00933   static struct ast_frame f;
    00934   f.frametype = AST_FRAME_VOICE;
    00935   f.subclass = AST_FORMAT_G726;
    00936   f.datalen = sizeof (g726_slin_ex);
    00937   f.samples = sizeof(g726_slin_ex) * 2;
    00938   f.mallocd = 0;
    00939   f.offset = 0;
    00940   f.src = __PRETTY_FUNCTION__;
    00941   f.data = g726_slin_ex;
    00942   return &f;
    00943 }
    00944
    00945 /*
    00946  * LinToG726_Sample
    00947  */
    00948
    00949 static struct ast_frame *
    00950 lintog726_sample (void)
    00951 {
    00952   static struct ast_frame f;
    00953   f.frametype = AST_FRAME_VOICE;
    00954   f.subclass = AST_FORMAT_SLINEAR;
    00955   f.datalen = sizeof (slin_g726_ex);
    00956   /* Assume 8000 Hz */
    00957   f.samples = sizeof (slin_g726_ex) / 2;
    00958   f.mallocd = 0;
    00959   f.offset = 0;
    00960   f.src = __PRETTY_FUNCTION__;
    00961   f.data = slin_g726_ex;
    00962   return &f;
    00963 }
    00964
    00965 /*
    00966  * G726_Destroy
    00967  *  Destroys a private workspace.
    00968  *
    00969  * Results:
    00970  *  It's gone!
    00971  *
    00972  * Side effects:
    00973  *  None.
    00974  */
    00975
    00976 static void
    00977 g726_destroy (struct ast_translator_pvt *pvt)
    00978 {
    00979   free (pvt);
    00980   localusecnt--;
    00981   ast_update_use_count ();
    00982 }
    00983
    00984 /*
    00985  * The complete translator for G726ToLin.
    00986  */
    00987
    00988 static struct ast_translator g726tolin = {
    00989   "g726tolin",
    00990   AST_FORMAT_G726,
    00991   AST_FORMAT_SLINEAR,
    00992   g726tolin_new,
    00993   g726tolin_framein,
    00994   g726tolin_frameout,
    00995   g726_destroy,
    00996   /* NULL */
    00997   g726tolin_sample
    00998 };
    00999
    01000 /*
    01001  * The complete translator for LinToG726.
    01002  */
    01003
    01004 static struct ast_translator lintog726 = {
    01005   "lintog726",
    01006   AST_FORMAT_SLINEAR,
    01007   AST_FORMAT_G726,
    01008   lintog726_new,
    01009   lintog726_framein,
    01010   lintog726_frameout,
    01011   g726_destroy,
    01012   /* NULL */
    01013   lintog726_sample
    01014 };
    01015
    01016 static void
    01017 parse_config(void)
    01018 {
    01019   struct ast_config *cfg;
    01020   struct ast_variable *var;
    01021   if ((cfg = ast_config_load("codecs.conf"))) {
    01022     if ((var = ast_variable_browse(cfg, "plc"))) {
    01023       while (var) {
    01024        if (!strcasecmp(var->name, "genericplc")) {
    01025          useplc = ast_true(var->value) ? 1 : 0;
    01026          if (option_verbose > 2)
    01027            ast_verbose(VERBOSE_PREFIX_3 "codec_g726: %susing generic PLC\n", useplc ? "" : "not ");
    01028        }
    01029        var = var->next;
    01030       }
    01031     }
    01032     ast_config_destroy(cfg);
    01033   }
    01034 }
    01035
    01036 int
    01037 reload(void)
    01038 {
    01039   parse_config();
    01040   return 0;
    01041 }
    01042
    01043 int
    01044 unload_module (void)
    01045 {
    01046   int res;
    01047   ast_mutex_lock (&localuser_lock);
    01048   res = ast_unregister_translator (&lintog726);
    01049   if (!res)
    01050     res = ast_unregister_translator (&g726tolin);
    01051   if (localusecnt)
    01052     res = -1;
    01053   ast_mutex_unlock (&localuser_lock);
    01054   return res;
    01055 }
    01056
    01057 int
    01058 load_module (void)
    01059 {
    01060   int res;
    01061   parse_config();
    01062   res = ast_register_translator (&g726tolin);
    01063   if (!res)
    01064     res = ast_register_translator (&lintog726);
    01065   else
    01066     ast_unregister_translator (&g726tolin);
    01067   return res;
    01068 }
    01069
    01070 /*
    01071  * Return a description of this module.
    01072  */
    01073
    01074 char *
    01075 description (void)
    01076 {
    01077   return tdesc;
    01078 }
    01079
    01080 int
    01081 usecount (void)
    01082 {
    01083   int res;
    01084   STANDARD_USECOUNT (res);
    01085   return res;
    01086 }
    01087
    01088 char *
    01089 key ()
    01090 {
    01091   return ASTERISK_GPL_KEY;
    01092 }