Fri Jul 24 00:41:39 2009

Asterisk developer's documentation


codec_g726.c File Reference

codec_g726.c - translate between signed linear and ITU G.726-32kbps (both RFC3551 and AAL2 codeword packing) More...

#include "asterisk.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/translate.h"
#include "asterisk/utils.h"
#include "log2comp.h"
#include "slin_g726_ex.h"
#include "g726_slin_ex.h"

Go to the source code of this file.

Data Structures

struct  g726_coder_pvt
struct  g726_state

Defines

#define BUF_SHIFT   5
#define BUFFER_SAMPLES   8096
#define WANT_ASM

Functions

static void __reg_module (void)
static void __unreg_module (void)
static int fmult (int an, int srn)
static int g726_decode (int i, struct g726_state *state_ptr)
static int g726_encode (int sl, struct g726_state *state_ptr)
static void g726_init_state (struct g726_state *state_ptr)
static int g726aal2tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 decode packed 4-bit G726 values (AAL2 packing) and store in buffer.
static int g726tog726aal2_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 convert G726-32 RFC3551 packed data into AAL2 packed data (or vice-versa)
static int g726tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 decode packed 4-bit G726 values (RFC3551 packing) and store in buffer.
static struct ast_frameg726tolin_sample (void)
static int lintog726_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf
static int lintog726_new (struct ast_trans_pvt *pvt)
 init a new instance of g726_coder_pvt.
static struct ast_framelintog726_sample (void)
static int lintog726aal2_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 compress and store data (4-bit G726 samples, AAL2 packing) in outbuf
static int load_module (void)
static int parse_config (int reload)
static int predictor_pole (struct g726_state *state_ptr)
static int predictor_zero (struct g726_state *state_ptr)
static int quan (int val, int *table, int size)
static int quantize (int d, int y, int *table, int size)
static int reconstruct (int sign, int dqln, int y)
static int reload (void)
static int step_size (struct g726_state *state_ptr)
static int unload_module (void)
static void update (int code_size, int y, int wi, int fi, int dq, int sr, int dqsez, struct g726_state *state_ptr)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "ITU G.726-32kbps G726 Transcoder" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, .reload = reload, }
static int _dqlntab [16]
static int _fitab [16]
static int _witab [16]
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_translator g726aal2tog726
static struct ast_translator g726aal2tolin
static struct ast_translator g726tog726aal2
static struct ast_translator g726tolin
static struct ast_translator lintog726
static struct ast_translator lintog726aal2
static int qtab_721 [7] = {-124, 80, 178, 246, 300, 349, 400}


Detailed Description

codec_g726.c - translate between signed linear and ITU G.726-32kbps (both RFC3551 and AAL2 codeword packing)

Definition in file codec_g726.c.


Define Documentation

#define BUF_SHIFT   5

Definition at line 58 of file codec_g726.c.

#define BUFFER_SAMPLES   8096

Definition at line 57 of file codec_g726.c.

#define WANT_ASM

Definition at line 41 of file codec_g726.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 960 of file codec_g726.c.

static void __unreg_module ( void   )  [static]

Definition at line 960 of file codec_g726.c.

static int fmult ( int  an,
int  srn 
) [static]

Definition at line 204 of file codec_g726.c.

References ilog2().

Referenced by predictor_pole(), and predictor_zero().

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 }

static int g726_decode ( int  i,
struct g726_state state_ptr 
) [static]

Definition at line 577 of file codec_g726.c.

References predictor_pole(), predictor_zero(), reconstruct(), step_size(), and update().

Referenced by g726aal2tolin_framein(), and g726tolin_framein().

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 }

static int g726_encode ( int  sl,
struct g726_state state_ptr 
) [static]

Definition at line 623 of file codec_g726.c.

References predictor_pole(), predictor_zero(), quantize(), reconstruct(), step_size(), and update().

Referenced by lintog726_framein(), and lintog726aal2_framein().

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 }

static void g726_init_state ( struct g726_state state_ptr  )  [static]

Definition at line 122 of file codec_g726.c.

References g726_state::a, g726_state::ap, g726_state::b, g726_state::dml, g726_state::dms, g726_state::dq, g726_state::pk, g726_state::sr, g726_state::td, g726_state::yl, and g726_state::yu.

Referenced by lintog726_new().

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 }

static int g726aal2tolin_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
) [static]

decode packed 4-bit G726 values (AAL2 packing) and store in buffer.

Definition at line 692 of file codec_g726.c.

References ast_trans_pvt::datalen, f, g726_coder_pvt::g726, g726_decode(), ast_trans_pvt::i16, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

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 }

static int g726tog726aal2_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
) [static]

convert G726-32 RFC3551 packed data into AAL2 packed data (or vice-versa)

Definition at line 774 of file codec_g726.c.

References ast_trans_pvt::datalen, f, ast_trans_pvt::outbuf, ast_trans_pvt::samples, and ast_trans_pvt::uc.

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 }

static int g726tolin_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
) [static]

decode packed 4-bit G726 values (RFC3551 packing) and store in buffer.

Definition at line 733 of file codec_g726.c.

References ast_trans_pvt::datalen, f, g726_coder_pvt::g726, g726_decode(), ast_trans_pvt::i16, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

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 }

static struct ast_frame* g726tolin_sample ( void   )  [static]

Definition at line 789 of file codec_g726.c.

References AST_FORMAT_G726, AST_FRAME_VOICE, f, g726_slin_ex, and ast_frame::samples.

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 }

static int lintog726_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
) [static]

compress and store data (4-bit G726 samples, RFC3551 packing) in outbuf

Definition at line 752 of file codec_g726.c.

References ast_trans_pvt::c, ast_trans_pvt::datalen, f, g726_coder_pvt::g726, g726_encode(), g726_coder_pvt::next_flag, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

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 }

static int lintog726_new ( struct ast_trans_pvt pvt  )  [static]

init a new instance of g726_coder_pvt.

Definition at line 682 of file codec_g726.c.

References g726_coder_pvt::g726, g726_init_state(), and 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 }

static struct ast_frame* lintog726_sample ( void   )  [static]

Definition at line 803 of file codec_g726.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, f, ast_frame::samples, and slin_g726_ex.

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 }

static int lintog726aal2_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
) [static]

compress and store data (4-bit G726 samples, AAL2 packing) in outbuf

Definition at line 711 of file codec_g726.c.

References ast_trans_pvt::c, ast_trans_pvt::datalen, f, g726_coder_pvt::g726, g726_encode(), g726_coder_pvt::next_flag, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

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 }

static int load_module ( void   )  [static]

Definition at line 931 of file codec_g726.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_register_translator, g726aal2tog726, g726aal2tolin, g726tog726aal2, g726tolin, lintog726, lintog726aal2, parse_config(), and unload_module().

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 }

static int parse_config ( int  reload  )  [static]

Definition at line 887 of file codec_g726.c.

References ast_config_load, ast_true(), ast_variable_browse(), ast_verb, CONFIG_FLAG_FILEUNCHANGED, config_flags, CONFIG_STATUS_FILEUNCHANGED, g726tolin, ast_translator::useplc, and var.

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 }

static int predictor_pole ( struct g726_state state_ptr  )  [static]

Definition at line 232 of file codec_g726.c.

References g726_state::a, fmult(), and g726_state::sr.

Referenced by g726_decode(), and g726_encode().

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 }

static int predictor_zero ( struct g726_state state_ptr  )  [static]

Definition at line 223 of file codec_g726.c.

References g726_state::b, g726_state::dq, and fmult().

Referenced by g726_decode(), and g726_encode().

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 }

static int quan ( int  val,
int *  table,
int  size 
) [static]

Definition at line 159 of file codec_g726.c.

Referenced by quantize().

00160 {
00161    int      i;
00162 
00163    for (i = 0; i < size && val >= *table; ++i, ++table)
00164       ;
00165    return (i);
00166 }

static int quantize ( int  d,
int  y,
int *  table,
int  size 
) [static]

Definition at line 275 of file codec_g726.c.

References ilog2(), and quan().

Referenced by g726_encode().

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 }

static int reconstruct ( int  sign,
int  dqln,
int  y 
) [static]

Definition at line 328 of file codec_g726.c.

Referenced by bridge_p2p_rtp_write(), g726_decode(), and g726_encode().

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 }

static int reload ( void   )  [static]

Definition at line 908 of file codec_g726.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and parse_config().

00909 {
00910    if (parse_config(1))
00911       return AST_MODULE_LOAD_DECLINE;
00912    return AST_MODULE_LOAD_SUCCESS;
00913 }

static int step_size ( struct g726_state state_ptr  )  [static]

Definition at line 246 of file codec_g726.c.

References g726_state::ap, g726_state::yl, and g726_state::yu.

Referenced by g726_decode(), and g726_encode().

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 }

static int unload_module ( void   )  [static]

Definition at line 915 of file codec_g726.c.

References ast_unregister_translator(), g726aal2tog726, g726aal2tolin, g726tog726aal2, g726tolin, lintog726, and lintog726aal2.

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 }

static void update ( int  code_size,
int  y,
int  wi,
int  fi,
int  dq,
int  sr,
int  dqsez,
struct g726_state state_ptr 
) [static]

Definition at line 364 of file codec_g726.c.

References g726_state::a, g726_state::ap, g726_state::b, g726_state::dml, g726_state::dms, g726_state::dq, ilog2(), g726_state::pk, g726_state::sr, g726_state::td, g726_state::yl, and g726_state::yu.

Referenced by g726_decode(), and g726_encode().

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 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "ITU G.726-32kbps G726 Transcoder" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "a9c98e5d177805051735cb5b0b16b0a0" , .load = load_module, .unload = unload_module, .reload = reload, } [static]

Definition at line 960 of file codec_g726.c.

int _dqlntab[16] [static]

Initial value:

 {-2048, 4, 135, 213, 273, 323, 373, 425,
            425, 373, 323, 273, 213, 135, 4, -2048}

Definition at line 100 of file codec_g726.c.

int _fitab[16] [static]

Initial value:

 {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
            0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0}

Definition at line 111 of file codec_g726.c.

int _witab[16] [static]

Initial value:

 {-12, 18, 41, 64, 112, 198, 355, 1122,
            1122, 355, 198, 112, 64, 41, 18, -12}

Definition at line 104 of file codec_g726.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 960 of file codec_g726.c.

struct ast_translator g726aal2tog726 [static]

Definition at line 877 of file codec_g726.c.

Referenced by load_module(), and unload_module().

struct ast_translator g726aal2tolin [static]

Definition at line 842 of file codec_g726.c.

Referenced by load_module(), and unload_module().

struct ast_translator g726tog726aal2 [static]

Definition at line 867 of file codec_g726.c.

Referenced by load_module(), and unload_module().

struct ast_translator g726tolin [static]

Definition at line 817 of file codec_g726.c.

Referenced by load_module(), parse_config(), and unload_module().

struct ast_translator lintog726 [static]

Definition at line 830 of file codec_g726.c.

Referenced by load_module(), and unload_module().

struct ast_translator lintog726aal2 [static]

Definition at line 855 of file codec_g726.c.

Referenced by load_module(), and unload_module().

int qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400} [static]

Definition at line 95 of file codec_g726.c.


Generated on Fri Jul 24 00:41:39 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7