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

Generated on Fri Jun 19 12:09:42 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7