Wed Oct 28 11:52:12 2009

Asterisk developer's documentation


codec_lpc10.c File Reference

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

#include "asterisk.h"
#include "asterisk/translate.h"
#include "asterisk/config.h"
#include "asterisk/module.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

static void __reg_module (void)
static void __unreg_module (void)
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 int parse_config (int reload)
static int reload (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "LPC10 2.4kbps Coder/Decoder" , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, }
static struct ast_module_infoast_module_info = &__mod_info
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 52 of file codec_lpc10.c.

#define LPC10_BYTES_IN_COMPRESSED_FRAME   (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8

Definition at line 50 of file codec_lpc10.c.

Referenced by lintolpc10_frameout(), and lpc10tolin_framein().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 311 of file codec_lpc10.c.

static void __unreg_module ( void   )  [static]

Definition at line 311 of file codec_lpc10.c.

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

Definition at line 123 of file codec_lpc10.c.

Referenced by lintolpc10_frameout().

00124 {
00125    unsigned char mask=0x80;
00126    int x;
00127    *c = 0;
00128    for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
00129       if (bits[x])
00130          *c |= mask;
00131       mask = mask >> 1;
00132       if ((x % 8)==7) {
00133          c++;
00134          *c = 0;
00135          mask = 0x80;
00136       }
00137    }
00138 }

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

Definition at line 109 of file codec_lpc10.c.

Referenced by lpc10tolin_framein().

00110 {
00111    int x;
00112    for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
00113       if (*c & (0x80 >> (x & 7)))
00114          bits[x] = 1;
00115       else
00116          bits[x] = 0;
00117       if ((x & 7) == 7)
00118          c++;
00119    }
00120 }

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

Definition at line 173 of file codec_lpc10.c.

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

00174 {
00175    struct lpc10_coder_pvt *tmp = pvt->pvt;
00176 
00177    /* Just add the frames to our stream */
00178    if (pvt->samples + f->samples > BUFFER_SAMPLES) {
00179       ast_log(LOG_WARNING, "Out of buffer space\n");
00180       return -1;
00181    }
00182    memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
00183    pvt->samples += f->samples;
00184    return 0;
00185 }

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

Definition at line 187 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, ast_trans_pvt::samples, and ast_trans_pvt::uc.

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

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

Definition at line 78 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::ptr, ast_frame::samples, slin_lpc10_ex, ast_frame::src, and ast_frame::subclass.

00079 {
00080    static struct ast_frame f;
00081    f.frametype = AST_FRAME_VOICE;
00082    f.subclass = AST_FORMAT_SLINEAR;
00083    f.datalen = sizeof(slin_lpc10_ex);
00084    /* Assume 8000 Hz */
00085    f.samples = LPC10_SAMPLES_PER_FRAME;
00086    f.mallocd = 0;
00087    f.offset = 0;
00088    f.src = __PRETTY_FUNCTION__;
00089    f.data.ptr = slin_lpc10_ex;
00090    return &f;
00091 }

static int load_module ( void   )  [static]

Definition at line 291 of file codec_lpc10.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_register_translator, ast_unregister_translator(), and parse_config().

00292 {
00293    int res;
00294 
00295    if (parse_config(0))
00296       return AST_MODULE_LOAD_DECLINE;
00297    res = ast_register_translator(&lpc10tolin);
00298    if (!res) 
00299       res = ast_register_translator(&lintolpc10);
00300    else
00301       ast_unregister_translator(&lpc10tolin);
00302    if (res)
00303       return AST_MODULE_LOAD_FAILURE;
00304    return AST_MODULE_LOAD_SUCCESS;
00305 }

static int lpc10_dec_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 71 of file codec_lpc10.c.

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

00072 {
00073    struct lpc10_coder_pvt *tmp = pvt->pvt;
00074 
00075    return (tmp->lpc10.dec = create_lpc10_decoder_state()) ? 0 : -1;
00076 }

static void lpc10_destroy ( struct ast_trans_pvt arg  )  [static]

Definition at line 218 of file codec_lpc10.c.

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

00219 {
00220    struct lpc10_coder_pvt *pvt = arg->pvt;
00221    /* Enc and DEC are both just allocated, so they can be freed */
00222    ast_free(pvt->lpc10.enc);
00223 }

static int lpc10_enc_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 64 of file codec_lpc10.c.

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

00065 {
00066    struct lpc10_coder_pvt *tmp = pvt->pvt;
00067 
00068    return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
00069 }

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

Definition at line 140 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(), ast_trans_pvt::i16, len(), LOG_WARNING, lpc10_coder_pvt::lpc10, LPC10_BYTES_IN_COMPRESSED_FRAME, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

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

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

Definition at line 93 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::ptr, ast_frame::samples, ast_frame::src, and ast_frame::subclass.

00094 {
00095    static struct ast_frame f;
00096    f.frametype = AST_FRAME_VOICE;
00097    f.subclass = AST_FORMAT_LPC10;
00098    f.datalen = sizeof(lpc10_slin_ex);
00099    /* All frames are 22 ms long (maybe a little more -- why did he choose
00100       LPC10_SAMPLES_PER_FRAME sample frames anyway?? */
00101    f.samples = LPC10_SAMPLES_PER_FRAME;
00102    f.mallocd = 0;
00103    f.offset = 0;
00104    f.src = __PRETTY_FUNCTION__;
00105    f.data.ptr = lpc10_slin_ex;
00106    return &f;
00107 }

static int parse_config ( int  reload  )  [static]

Definition at line 253 of file codec_lpc10.c.

References ast_config_destroy(), ast_config_load, ast_true(), ast_variable_browse(), ast_verb, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEUNCHANGED, ast_variable::name, ast_variable::next, ast_translator::useplc, ast_variable::value, and var.

00254 {
00255    struct ast_variable *var;
00256    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00257    struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
00258    if (cfg == NULL)
00259       return 0;
00260    if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00261       return 0;
00262    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00263       if (!strcasecmp(var->name, "genericplc")) {
00264          lpc10tolin.useplc = ast_true(var->value) ? 1 : 0;
00265          ast_verb(3, "codec_lpc10: %susing generic PLC\n",
00266                lpc10tolin.useplc ? "" : "not ");
00267       }
00268    }
00269    ast_config_destroy(cfg);
00270    return 0;
00271 }

static int reload ( void   )  [static]

Definition at line 273 of file codec_lpc10.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and parse_config().

00274 {
00275    if (parse_config(1))
00276       return AST_MODULE_LOAD_DECLINE;
00277    return AST_MODULE_LOAD_SUCCESS;
00278 }

static int unload_module ( void   )  [static]

Definition at line 281 of file codec_lpc10.c.

References ast_unregister_translator().

00282 {
00283    int res;
00284 
00285    res = ast_unregister_translator(&lintolpc10);
00286    res |= ast_unregister_translator(&lpc10tolin);
00287 
00288    return res;
00289 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "LPC10 2.4kbps Coder/Decoder" , .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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, } [static]

Definition at line 311 of file codec_lpc10.c.

Definition at line 311 of file codec_lpc10.c.

struct ast_translator lintolpc10 [static]

Definition at line 239 of file codec_lpc10.c.

struct ast_translator lpc10tolin [static]

Definition at line 225 of file codec_lpc10.c.


Generated on Wed Oct 28 11:52:13 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6