Mon Mar 31 07:40:06 2008

Asterisk developer's documentation


codec_ilbc.c File Reference

Translate between signed linear and Internet Low Bitrate Codec. 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/module.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/utils.h"
#include "ilbc/iLBC_encode.h"
#include "ilbc/iLBC_decode.h"
#include "slin_ilbc_ex.h"
#include "ilbc_slin_ex.h"

Include dependency graph for codec_ilbc.c:

Go to the source code of this file.

Data Structures

struct  ilbc_coder_pvt

Defines

#define BUFFER_SAMPLES   8000
#define ILBC_FRAME_LEN   50
#define ILBC_MS   30
#define ILBC_SAMPLES   240
#define USE_ILBC_ENHANCER   1

Functions

 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"iLBC Coder/Decoder")
static int ilbctolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 decode a frame and store in outbuf
static int ilbctolin_new (struct ast_trans_pvt *pvt)
static struct ast_frameilbctolin_sample (void)
static int lintoilbc_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 store a frame into a temporary buffer, for later decoding
static struct ast_framelintoilbc_frameout (struct ast_trans_pvt *pvt)
 encode the temporary buffer and generate a frame
static int lintoilbc_new (struct ast_trans_pvt *pvt)
static struct ast_framelintoilbc_sample (void)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_translator ilbctolin
static struct ast_translator lintoilbc


Detailed Description

Translate between signed linear and Internet Low Bitrate Codec.

Definition in file codec_ilbc.c.


Define Documentation

#define BUFFER_SAMPLES   8000

Definition at line 59 of file codec_ilbc.c.

#define ILBC_FRAME_LEN   50

Definition at line 57 of file codec_ilbc.c.

Referenced by ilbctolin_framein(), and lintoilbc_frameout().

#define ILBC_MS   30

Definition at line 54 of file codec_ilbc.c.

Referenced by ilbctolin_new(), and lintoilbc_new().

#define ILBC_SAMPLES   240

Definition at line 58 of file codec_ilbc.c.

Referenced by ilbc_read(), ilbc_seek(), ilbc_tell(), ilbctolin_framein(), ilbctolin_sample(), and lintoilbc_frameout().

#define USE_ILBC_ENHANCER   1

Definition at line 53 of file codec_ilbc.c.

Referenced by ilbctolin_new().


Function Documentation

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"iLBC Coder/Decoder"   
)

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

decode a frame and store in outbuf

Definition at line 116 of file codec_ilbc.c.

References ast_log(), BUFFER_SAMPLES, ast_trans_pvt::datalen, ilbc_coder_pvt::dec, f, ILBC_FRAME_LEN, ILBC_SAMPLES, LOG_WARNING, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

00117 {
00118    struct ilbc_coder_pvt *tmp = pvt->pvt;
00119    int plc_mode = 1; /* 1 = normal data, 0 = plc */
00120    /* Assuming there's space left, decode into the current buffer at
00121       the tail location.  Read in as many frames as there are */
00122    int x,i;
00123    int16_t *dst = (int16_t *)pvt->outbuf;
00124    float tmpf[ILBC_SAMPLES];
00125 
00126    if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
00127       f->datalen = ILBC_FRAME_LEN;
00128       f->samples = ILBC_SAMPLES;
00129       plc_mode = 0;  /* do native plc */
00130       pvt->samples += ILBC_SAMPLES;
00131    }
00132 
00133    if (f->datalen % ILBC_FRAME_LEN) {
00134       ast_log(LOG_WARNING, "Huh?  An ilbc frame that isn't a multiple of 50 bytes long from %s (%d)?\n", f->src, f->datalen);
00135       return -1;
00136    }
00137    
00138    for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
00139       if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {   
00140          ast_log(LOG_WARNING, "Out of buffer space\n");
00141          return -1;
00142       }     
00143       iLBC_decode(tmpf, plc_mode ? f->data + x : NULL, &tmp->dec, plc_mode);
00144       for ( i=0; i < ILBC_SAMPLES; i++)
00145          dst[pvt->samples + i] = tmpf[i];
00146       pvt->samples += ILBC_SAMPLES;
00147       pvt->datalen += 2*ILBC_SAMPLES;
00148    }
00149    return 0;
00150 }

static int ilbctolin_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 77 of file codec_ilbc.c.

References ilbc_coder_pvt::dec, ILBC_MS, ast_trans_pvt::pvt, and USE_ILBC_ENHANCER.

00078 {
00079    struct ilbc_coder_pvt *tmp = pvt->pvt;
00080 
00081    initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
00082 
00083    return 0;
00084 }

static struct ast_frame* ilbctolin_sample ( void   )  [static]

Definition at line 100 of file codec_ilbc.c.

References AST_FORMAT_ILBC, AST_FRAME_VOICE, f, ILBC_SAMPLES, and ilbc_slin_ex.

00101 {
00102    static struct ast_frame f;
00103    f.frametype = AST_FRAME_VOICE;
00104    f.subclass = AST_FORMAT_ILBC;
00105    f.datalen = sizeof(ilbc_slin_ex);
00106    /* All frames are 30 ms long */
00107    f.samples = ILBC_SAMPLES;
00108    f.mallocd = 0;
00109    f.offset = 0;
00110    f.src = __PRETTY_FUNCTION__;
00111    f.data = ilbc_slin_ex;
00112    return &f;
00113 }

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

store a frame into a temporary buffer, for later decoding

Definition at line 153 of file codec_ilbc.c.

References ilbc_coder_pvt::buf, f, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

00154 {
00155    struct ilbc_coder_pvt *tmp = pvt->pvt;
00156 
00157    /* Just add the frames to our stream */
00158    /* XXX We should look at how old the rest of our stream is, and if it
00159       is too old, then we should overwrite it entirely, otherwise we can
00160       get artifacts of earlier talk that do not belong */
00161    memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
00162    pvt->samples += f->samples;
00163    return 0;
00164 }

static struct ast_frame* lintoilbc_frameout ( struct ast_trans_pvt pvt  )  [static]

encode the temporary buffer and generate a frame

Definition at line 167 of file codec_ilbc.c.

References ast_trans_frameout(), ilbc_coder_pvt::buf, ilbc_coder_pvt::enc, ILBC_FRAME_LEN, ILBC_SAMPLES, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

00168 {
00169    struct ilbc_coder_pvt *tmp = pvt->pvt;
00170    int datalen = 0;
00171    int samples = 0;
00172 
00173    /* We can't work on anything less than a frame in size */
00174    if (pvt->samples < ILBC_SAMPLES)
00175       return NULL;
00176    while (pvt->samples >= ILBC_SAMPLES) {
00177       float tmpf[ILBC_SAMPLES];
00178       int i;
00179 
00180       /* Encode a frame of data */
00181       for (i = 0 ; i < ILBC_SAMPLES ; i++)
00182          tmpf[i] = tmp->buf[samples + i];
00183       iLBC_encode((unsigned char *) pvt->outbuf + datalen, tmpf, &tmp->enc);
00184 
00185       datalen += ILBC_FRAME_LEN;
00186       samples += ILBC_SAMPLES;
00187       pvt->samples -= ILBC_SAMPLES;
00188    }
00189 
00190    /* Move the data at the end of the buffer to the front */
00191    if (pvt->samples)
00192       memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
00193 
00194    return ast_trans_frameout(pvt, datalen, samples);
00195 }

static int lintoilbc_new ( struct ast_trans_pvt pvt  )  [static]

Definition at line 68 of file codec_ilbc.c.

References ilbc_coder_pvt::enc, ILBC_MS, and ast_trans_pvt::pvt.

00069 {
00070    struct ilbc_coder_pvt *tmp = pvt->pvt;
00071 
00072    initEncode(&tmp->enc, ILBC_MS);
00073 
00074    return 0;
00075 }

static struct ast_frame* lintoilbc_sample ( void   )  [static]

Definition at line 86 of file codec_ilbc.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, f, and slin_ilbc_ex.

00087 {
00088    static struct ast_frame f;
00089    f.frametype = AST_FRAME_VOICE;
00090    f.subclass = AST_FORMAT_SLINEAR;
00091    f.datalen = sizeof(slin_ilbc_ex);
00092    f.samples = sizeof(slin_ilbc_ex)/2;
00093    f.mallocd = 0;
00094    f.offset = 0;
00095    f.src = __PRETTY_FUNCTION__;
00096    f.data = slin_ilbc_ex;
00097    return &f;
00098 }

static int load_module ( void   )  [static]

Definition at line 231 of file codec_ilbc.c.

References ast_register_translator, ast_unregister_translator(), ilbctolin, and lintoilbc.

00232 {
00233    int res;
00234 
00235    res = ast_register_translator(&ilbctolin);
00236    if (!res) 
00237       res=ast_register_translator(&lintoilbc);
00238    else
00239       ast_unregister_translator(&ilbctolin);
00240 
00241    return res;
00242 }

static int unload_module ( void   )  [static]

Definition at line 221 of file codec_ilbc.c.

References ast_unregister_translator(), ilbctolin, and lintoilbc.

00222 {
00223    int res;
00224 
00225    res = ast_unregister_translator(&lintoilbc);
00226    res |= ast_unregister_translator(&ilbctolin);
00227 
00228    return res;
00229 }


Variable Documentation

struct ast_translator ilbctolin [static]

Definition at line 197 of file codec_ilbc.c.

Referenced by load_module(), and unload_module().

struct ast_translator lintoilbc [static]

Definition at line 209 of file codec_ilbc.c.

Referenced by load_module(), and unload_module().


Generated on Mon Mar 31 07:40:06 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1