Thu Apr 28 2011 17:15:21

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