Sat Aug 6 00:39:28 2011

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

Generated on Sat Aug 6 00:39:28 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7