codec_g726.c

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

Generated on Thu Apr 16 06:27:30 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6