Mon Aug 31 12:30:33 2015

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 "asterisk/slin.h"
#include "ex_lpc10.h"

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 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 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 56 of file codec_lpc10.c.

#define LPC10_BYTES_IN_COMPRESSED_FRAME   (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8

Definition at line 54 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 96 of file codec_lpc10.c.

Referenced by lintolpc10_frameout().

00097 {
00098    unsigned char mask=0x80;
00099    int x;
00100    *c = 0;
00101    for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
00102       if (bits[x])
00103          *c |= mask;
00104       mask = mask >> 1;
00105       if ((x % 8)==7) {
00106          c++;
00107          *c = 0;
00108          mask = 0x80;
00109       }
00110    }
00111 }

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

Definition at line 82 of file codec_lpc10.c.

Referenced by lpc10tolin_framein().

00083 {
00084    int x;
00085    for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
00086       if (*c & (0x80 >> (x & 7)))
00087          bits[x] = 1;
00088       else
00089          bits[x] = 0;
00090       if ((x & 7) == 7)
00091          c++;
00092    }
00093 }

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

Definition at line 146 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.

00147 {
00148    struct lpc10_coder_pvt *tmp = pvt->pvt;
00149 
00150    /* Just add the frames to our stream */
00151    if (pvt->samples + f->samples > BUFFER_SAMPLES) {
00152       ast_log(LOG_WARNING, "Out of buffer space\n");
00153       return -1;
00154    }
00155    memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
00156    pvt->samples += f->samples;
00157    return 0;
00158 }

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

Definition at line 160 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.

00161 {
00162    struct lpc10_coder_pvt *tmp = pvt->pvt;
00163    int x;
00164    int datalen = 0;  /* output frame */
00165    int samples = 0;  /* output samples */
00166    float tmpbuf[LPC10_SAMPLES_PER_FRAME];
00167    INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME];  /* XXX what ??? */
00168    /* We can't work on anything less than a frame in size */
00169    if (pvt->samples < LPC10_SAMPLES_PER_FRAME)
00170       return NULL;
00171    while (pvt->samples >=  LPC10_SAMPLES_PER_FRAME) {
00172       /* Encode a frame of data */
00173       for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++)
00174          tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0;
00175       lpc10_encode(tmpbuf, bits, tmp->lpc10.enc);
00176       build_bits(pvt->outbuf.uc + datalen, bits);
00177       datalen += LPC10_BYTES_IN_COMPRESSED_FRAME;
00178       samples += LPC10_SAMPLES_PER_FRAME;
00179       pvt->samples -= LPC10_SAMPLES_PER_FRAME;
00180       /* Use one of the two left over bits to record if this is a 22 or 23 ms frame...
00181          important for IAX use */
00182       tmp->longer = 1 - tmp->longer;
00183    }
00184    /* Move the data at the end of the buffer to the front */
00185    if (pvt->samples)
00186       memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
00187    return ast_trans_frameout(pvt, datalen, samples);
00188 }

static int load_module ( void   )  [static]

Definition at line 241 of file codec_lpc10.c.

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

00242 {
00243    int res;
00244 
00245    res = ast_register_translator(&lpc10tolin);
00246    if (!res) 
00247       res = ast_register_translator(&lintolpc10);
00248    else
00249       ast_unregister_translator(&lpc10tolin);
00250    if (res)
00251       return AST_MODULE_LOAD_FAILURE;
00252    return AST_MODULE_LOAD_SUCCESS;
00253 }

static int lpc10_dec_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 75 of file codec_lpc10.c.

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

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

static void lpc10_destroy ( struct ast_trans_pvt arg  )  [static]

Definition at line 191 of file codec_lpc10.c.

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

00192 {
00193    struct lpc10_coder_pvt *pvt = arg->pvt;
00194    /* Enc and DEC are both just allocated, so they can be freed */
00195    ast_free(pvt->lpc10.enc);
00196 }

static int lpc10_enc_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 68 of file codec_lpc10.c.

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

00069 {
00070    struct lpc10_coder_pvt *tmp = pvt->pvt;
00071 
00072    return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
00073 }

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

Definition at line 113 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.

00114 {
00115    struct lpc10_coder_pvt *tmp = pvt->pvt;
00116    int16_t *dst = pvt->outbuf.i16;
00117    int len = 0;
00118 
00119    while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {
00120       int x;
00121       float tmpbuf[LPC10_SAMPLES_PER_FRAME];
00122       INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX see note */
00123       if (pvt->samples + LPC10_SAMPLES_PER_FRAME > BUFFER_SAMPLES) {
00124          ast_log(LOG_WARNING, "Out of buffer space\n");
00125          return -1;
00126       }
00127       extract_bits(bits, f->data.ptr + len);
00128       if (lpc10_decode(bits, tmpbuf, tmp->lpc10.dec)) {
00129          ast_log(LOG_WARNING, "Invalid lpc10 data\n");
00130          return -1;
00131       }
00132       for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++) {
00133          /* Convert to a short between -1.0 and 1.0 */
00134          dst[pvt->samples + x] = (int16_t)(32768.0 * tmpbuf[x]);
00135       }
00136 
00137       pvt->samples += LPC10_SAMPLES_PER_FRAME;
00138       pvt->datalen += 2*LPC10_SAMPLES_PER_FRAME;
00139       len += LPC10_BYTES_IN_COMPRESSED_FRAME;
00140    }
00141    if (len != f->datalen) 
00142       printf("Decoded %d, expected %d\n", len, f->datalen);
00143    return 0;
00144 }

static int reload ( void   )  [static]

Definition at line 225 of file codec_lpc10.c.

References AST_MODULE_LOAD_SUCCESS.

00226 {
00227    return AST_MODULE_LOAD_SUCCESS;
00228 }

static int unload_module ( void   )  [static]

Definition at line 231 of file codec_lpc10.c.

References ast_unregister_translator().

00232 {
00233    int res;
00234 
00235    res = ast_unregister_translator(&lintolpc10);
00236    res |= ast_unregister_translator(&lpc10tolin);
00237 
00238    return res;
00239 }


Variable Documentation

struct ast_translator lintolpc10 [static]

Definition at line 211 of file codec_lpc10.c.

struct ast_translator lpc10tolin [static]

Definition at line 198 of file codec_lpc10.c.


Generated on 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1