Sat Apr 12 07:12:45 2008

Asterisk developer's documentation


codec_lpc10.c File Reference

Translate between signed linear and LPC10 (Linear Predictor Code). More...

#include "asterisk.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include "asterisk/lock.h"
#include "asterisk/translate.h"
#include "asterisk/config.h"
#include "asterisk/options.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "lpc10/lpc10.h"
#include "slin_lpc10_ex.h"
#include "lpc10_slin_ex.h"

Include dependency graph for codec_lpc10.c:

Go to the source code of this file.

Data Structures

struct  lpc10_coder_pvt

Defines

#define BUFFER_SAMPLES   8000
#define LPC10_BYTES_IN_COMPRESSED_FRAME   (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8

Functions

 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,"LPC10 2.4kbps Coder/Decoder",.load=load_module,.unload=unload_module,.reload=reload,)
static void build_bits (unsigned char *c, INT32 *bits)
static void extract_bits (INT32 *bits, unsigned char *c)
static int lintolpc10_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
static struct ast_framelintolpc10_frameout (struct ast_trans_pvt *pvt)
static struct ast_framelintolpc10_sample (void)
static int load_module (void)
static int lpc10_dec_new (struct ast_trans_pvt *pvt)
static void lpc10_destroy (struct ast_trans_pvt *arg)
static int lpc10_enc_new (struct ast_trans_pvt *pvt)
static int lpc10tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
static struct ast_framelpc10tolin_sample (void)
static void parse_config (void)
static int reload (void)
static int unload_module (void)

Variables

static struct ast_translator lintolpc10
static struct ast_translator lpc10tolin


Detailed Description

Translate between signed linear and LPC10 (Linear Predictor Code).

Definition in file codec_lpc10.c.


Define Documentation

#define BUFFER_SAMPLES   8000

Definition at line 63 of file codec_lpc10.c.

#define LPC10_BYTES_IN_COMPRESSED_FRAME   (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8

Definition at line 61 of file codec_lpc10.c.

Referenced by lintolpc10_frameout(), and lpc10tolin_framein().


Function Documentation

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_DEFAULT  ,
"LPC10 2.4kbps Coder/Decoder"  ,
load = load_module,
unload = unload_module,
reload = reload 
)

static void build_bits ( unsigned char *  c,
INT32 *  bits 
) [static]

Definition at line 134 of file codec_lpc10.c.

Referenced by lintolpc10_frameout().

00135 {
00136    unsigned char mask=0x80;
00137    int x;
00138    *c = 0;
00139    for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
00140       if (bits[x])
00141          *c |= mask;
00142       mask = mask >> 1;
00143       if ((x % 8)==7) {
00144          c++;
00145          *c = 0;
00146          mask = 0x80;
00147       }
00148    }
00149 }

static void extract_bits ( INT32 *  bits,
unsigned char *  c 
) [static]

Definition at line 120 of file codec_lpc10.c.

Referenced by lpc10tolin_framein().

00121 {
00122    int x;
00123    for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
00124       if (*c & (0x80 >> (x & 7)))
00125          bits[x] = 1;
00126       else
00127          bits[x] = 0;
00128       if ((x & 7) == 7)
00129          c++;
00130    }
00131 }

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

Definition at line 184 of file codec_lpc10.c.

References ast_log(), lpc10_coder_pvt::buf, BUFFER_SAMPLES, ast_frame::data, ast_frame::datalen, LOG_WARNING, ast_trans_pvt::pvt, ast_frame::samples, and ast_trans_pvt::samples.

00185 {
00186    struct lpc10_coder_pvt *tmp = pvt->pvt;
00187 
00188    /* Just add the frames to our stream */
00189    if (pvt->samples + f->samples > BUFFER_SAMPLES) {
00190       ast_log(LOG_WARNING, "Out of buffer space\n");
00191       return -1;
00192    }
00193    memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
00194    pvt->samples += f->samples;
00195    return 0;
00196 }

static struct ast_frame* lintolpc10_frameout ( struct ast_trans_pvt pvt  )  [static, read]

Definition at line 198 of file codec_lpc10.c.

References ast_trans_frameout(), lpc10_coder_pvt::buf, build_bits(), lpc10_coder_pvt::enc, lpc10_coder_pvt::longer, lpc10_coder_pvt::lpc10, LPC10_BYTES_IN_COMPRESSED_FRAME, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

00199 {
00200    struct lpc10_coder_pvt *tmp = pvt->pvt;
00201    int x;
00202    int datalen = 0;  /* output frame */
00203    int samples = 0;  /* output samples */
00204    float tmpbuf[LPC10_SAMPLES_PER_FRAME];
00205    INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME];  /* XXX what ??? */
00206    /* We can't work on anything less than a frame in size */
00207    if (pvt->samples < LPC10_SAMPLES_PER_FRAME)
00208       return NULL;
00209    while (pvt->samples >=  LPC10_SAMPLES_PER_FRAME) {
00210       /* Encode a frame of data */
00211       for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++)
00212          tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0;
00213       lpc10_encode(tmpbuf, bits, tmp->lpc10.enc);
00214       build_bits((unsigned char *) pvt->outbuf + datalen, bits);
00215       datalen += LPC10_BYTES_IN_COMPRESSED_FRAME;
00216       samples += LPC10_SAMPLES_PER_FRAME;
00217       pvt->samples -= LPC10_SAMPLES_PER_FRAME;
00218       /* Use one of the two left over bits to record if this is a 22 or 23 ms frame...
00219          important for IAX use */
00220       tmp->longer = 1 - tmp->longer;
00221    }
00222    /* Move the data at the end of the buffer to the front */
00223    if (pvt->samples)
00224       memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
00225    return ast_trans_frameout(pvt, datalen, samples);
00226 }

static struct ast_frame* lintolpc10_sample ( void   )  [static, read]

Definition at line 89 of file codec_lpc10.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_frame::data, ast_frame::datalen, ast_frame::frametype, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, slin_lpc10_ex, ast_frame::src, and ast_frame::subclass.

00090 {
00091    static struct ast_frame f;
00092    f.frametype = AST_FRAME_VOICE;
00093    f.subclass = AST_FORMAT_SLINEAR;
00094    f.datalen = sizeof(slin_lpc10_ex);
00095    /* Assume 8000 Hz */
00096    f.samples = LPC10_SAMPLES_PER_FRAME;
00097    f.mallocd = 0;
00098    f.offset = 0;
00099    f.src = __PRETTY_FUNCTION__;
00100    f.data = slin_lpc10_ex;
00101    return &f;
00102 }

static int load_module ( void   )  [static]

Definition at line 299 of file codec_lpc10.c.

References ast_register_translator, ast_unregister_translator(), and parse_config().

00300 {
00301    int res;
00302 
00303    parse_config();
00304    res=ast_register_translator(&lpc10tolin);
00305    if (!res) 
00306       res=ast_register_translator(&lintolpc10);
00307    else
00308       ast_unregister_translator(&lpc10tolin);
00309 
00310    return res;
00311 }

static int lpc10_dec_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 82 of file codec_lpc10.c.

References lpc10_coder_pvt::dec, lpc10_coder_pvt::lpc10, and ast_trans_pvt::pvt.

00083 {
00084    struct lpc10_coder_pvt *tmp = pvt->pvt;
00085 
00086    return (tmp->lpc10.dec = create_lpc10_decoder_state()) ? 0 : -1;
00087 }

static void lpc10_destroy ( struct ast_trans_pvt arg  )  [static]

Definition at line 229 of file codec_lpc10.c.

References lpc10_coder_pvt::enc, free, lpc10_coder_pvt::lpc10, and ast_trans_pvt::pvt.

00230 {
00231    struct lpc10_coder_pvt *pvt = arg->pvt;
00232    /* Enc and DEC are both just allocated, so they can be freed */
00233    free(pvt->lpc10.enc);
00234 }

static int lpc10_enc_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 75 of file codec_lpc10.c.

References lpc10_coder_pvt::enc, lpc10_coder_pvt::lpc10, and ast_trans_pvt::pvt.

00076 {
00077    struct lpc10_coder_pvt *tmp = pvt->pvt;
00078 
00079    return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
00080 }

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

Definition at line 151 of file codec_lpc10.c.

References ast_log(), BUFFER_SAMPLES, ast_frame::data, ast_frame::datalen, ast_trans_pvt::datalen, lpc10_coder_pvt::dec, extract_bits(), len, LOG_WARNING, lpc10_coder_pvt::lpc10, LPC10_BYTES_IN_COMPRESSED_FRAME, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

00152 {
00153    struct lpc10_coder_pvt *tmp = pvt->pvt;
00154    int16_t *dst = (int16_t *)pvt->outbuf;
00155    int len = 0;
00156 
00157    while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {
00158       int x;
00159       float tmpbuf[LPC10_SAMPLES_PER_FRAME];
00160       INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX see note */
00161       if (pvt->samples + LPC10_SAMPLES_PER_FRAME > BUFFER_SAMPLES) {
00162          ast_log(LOG_WARNING, "Out of buffer space\n");
00163          return -1;
00164       }
00165       extract_bits(bits, f->data + len);
00166       if (lpc10_decode(bits, tmpbuf, tmp->lpc10.dec)) {
00167          ast_log(LOG_WARNING, "Invalid lpc10 data\n");
00168          return -1;
00169       }
00170       for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++) {
00171          /* Convert to a short between -1.0 and 1.0 */
00172          dst[pvt->samples + x] = (int16_t)(32768.0 * tmpbuf[x]);
00173       }
00174 
00175       pvt->samples += LPC10_SAMPLES_PER_FRAME;
00176       pvt->datalen += 2*LPC10_SAMPLES_PER_FRAME;
00177       len += LPC10_BYTES_IN_COMPRESSED_FRAME;
00178    }
00179    if (len != f->datalen) 
00180       printf("Decoded %d, expected %d\n", len, f->datalen);
00181    return 0;
00182 }

static struct ast_frame* lpc10tolin_sample ( void   )  [static, read]

Definition at line 104 of file codec_lpc10.c.

References AST_FORMAT_LPC10, AST_FRAME_VOICE, ast_frame::data, ast_frame::datalen, ast_frame::frametype, lpc10_slin_ex, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, ast_frame::src, and ast_frame::subclass.

00105 {
00106    static struct ast_frame f;
00107    f.frametype = AST_FRAME_VOICE;
00108    f.subclass = AST_FORMAT_LPC10;
00109    f.datalen = sizeof(lpc10_slin_ex);
00110    /* All frames are 22 ms long (maybe a little more -- why did he choose
00111       LPC10_SAMPLES_PER_FRAME sample frames anyway?? */
00112    f.samples = LPC10_SAMPLES_PER_FRAME;
00113    f.mallocd = 0;
00114    f.offset = 0;
00115    f.src = __PRETTY_FUNCTION__;
00116    f.data = lpc10_slin_ex;
00117    return &f;
00118 }

static void parse_config ( void   )  [static]

Definition at line 264 of file codec_lpc10.c.

References ast_config_destroy(), ast_config_load(), ast_true(), ast_variable_browse(), ast_verbose(), ast_variable::name, ast_variable::next, option_verbose, ast_translator::useplc, ast_variable::value, var, and VERBOSE_PREFIX_3.

00265 {
00266         struct ast_variable *var;
00267         struct ast_config *cfg = ast_config_load("codecs.conf");
00268    if (!cfg)
00269       return;
00270    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00271           if (!strcasecmp(var->name, "genericplc")) {
00272          lpc10tolin.useplc = ast_true(var->value) ? 1 : 0;
00273          if (option_verbose > 2)
00274                 ast_verbose(VERBOSE_PREFIX_3 "codec_lpc10: %susing generic PLC\n",
00275                lpc10tolin.useplc ? "" : "not ");
00276       }
00277         }
00278    ast_config_destroy(cfg);
00279 }

static int reload ( void   )  [static]

Definition at line 281 of file codec_lpc10.c.

References parse_config().

00282 {
00283         parse_config();
00284 
00285         return 0;
00286 }

static int unload_module ( void   )  [static]

Definition at line 289 of file codec_lpc10.c.

References ast_unregister_translator().

00290 {
00291    int res;
00292 
00293    res = ast_unregister_translator(&lintolpc10);
00294    res |= ast_unregister_translator(&lpc10tolin);
00295 
00296    return res;
00297 }


Variable Documentation

struct ast_translator lintolpc10 [static]

Definition at line 250 of file codec_lpc10.c.

struct ast_translator lpc10tolin [static]

Definition at line 236 of file codec_lpc10.c.


Generated on Sat Apr 12 07:12:45 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.5