Sun Oct 16 2011 08:41:38

Asterisk developer's documentation


codec_ilbc.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * The iLBC code is from The IETF code base and is copyright The Internet Society (2004)
00005  *
00006  * Copyright (C) 1999 - 2005, Digium, Inc.
00007  *
00008  * Mark Spencer <markster@digium.com>
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  */
00020 
00021 /*! \file
00022  *
00023  * \brief Translate between signed linear and Internet Low Bitrate Codec
00024  * 
00025  * \ingroup codecs
00026  */
00027 
00028 /*** MODULEINFO
00029    <defaultenabled>no</defaultenabled>
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00036 
00037 #include "asterisk/translate.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/utils.h"
00040 
00041 #include "ilbc/iLBC_encode.h"
00042 #include "ilbc/iLBC_decode.h"
00043 
00044 #define USE_ILBC_ENHANCER  0
00045 #define ILBC_MS         30
00046 /* #define ILBC_MS         20 */
00047 
00048 #define  ILBC_FRAME_LEN 50 /* apparently... */
00049 #define  ILBC_SAMPLES   240   /* 30ms at 8000 hz */
00050 #define  BUFFER_SAMPLES 8000
00051 
00052 /* Sample frame data */
00053 #include "asterisk/slin.h"
00054 #include "ex_ilbc.h"
00055 
00056 struct ilbc_coder_pvt {
00057    iLBC_Enc_Inst_t enc;
00058    iLBC_Dec_Inst_t dec;
00059    /* Enough to store a full second */
00060    int16_t buf[BUFFER_SAMPLES];
00061 };
00062 
00063 static int lintoilbc_new(struct ast_trans_pvt *pvt)
00064 {
00065    struct ilbc_coder_pvt *tmp = pvt->pvt;
00066 
00067    initEncode(&tmp->enc, ILBC_MS);
00068 
00069    return 0;
00070 }
00071 
00072 static int ilbctolin_new(struct ast_trans_pvt *pvt)
00073 {
00074    struct ilbc_coder_pvt *tmp = pvt->pvt;
00075 
00076    initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
00077 
00078    return 0;
00079 }
00080 
00081 /*! \brief decode a frame and store in outbuf */
00082 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00083 {
00084    struct ilbc_coder_pvt *tmp = pvt->pvt;
00085    int plc_mode = 1; /* 1 = normal data, 0 = plc */
00086    /* Assuming there's space left, decode into the current buffer at
00087       the tail location.  Read in as many frames as there are */
00088    int x,i;
00089    int16_t *dst = pvt->outbuf.i16;
00090    float tmpf[ILBC_SAMPLES];
00091 
00092    if (!f->data.ptr && f->datalen) {
00093       ast_log(LOG_DEBUG, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", f->datalen, f->src ? f->src : "no src set");
00094       f->datalen = 0;
00095    }
00096 
00097    if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
00098       f->datalen = ILBC_FRAME_LEN;
00099       f->samples = ILBC_SAMPLES;
00100       plc_mode = 0;  /* do native plc */
00101       pvt->samples += ILBC_SAMPLES;
00102    }
00103 
00104    if (f->datalen % ILBC_FRAME_LEN) {
00105       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);
00106       return -1;
00107    }
00108    
00109    for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
00110       if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {   
00111          ast_log(LOG_WARNING, "Out of buffer space\n");
00112          return -1;
00113       }     
00114       iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
00115       for ( i=0; i < ILBC_SAMPLES; i++)
00116          dst[pvt->samples + i] = tmpf[i];
00117       pvt->samples += ILBC_SAMPLES;
00118       pvt->datalen += 2*ILBC_SAMPLES;
00119    }
00120    return 0;
00121 }
00122 
00123 /*! \brief store a frame into a temporary buffer, for later decoding */
00124 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00125 {
00126    struct ilbc_coder_pvt *tmp = pvt->pvt;
00127 
00128    /* Just add the frames to our stream */
00129    /* XXX We should look at how old the rest of our stream is, and if it
00130       is too old, then we should overwrite it entirely, otherwise we can
00131       get artifacts of earlier talk that do not belong */
00132    memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
00133    pvt->samples += f->samples;
00134    return 0;
00135 }
00136 
00137 /*! \brief encode the temporary buffer and generate a frame */
00138 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
00139 {
00140    struct ilbc_coder_pvt *tmp = pvt->pvt;
00141    int datalen = 0;
00142    int samples = 0;
00143 
00144    /* We can't work on anything less than a frame in size */
00145    if (pvt->samples < ILBC_SAMPLES)
00146       return NULL;
00147    while (pvt->samples >= ILBC_SAMPLES) {
00148       float tmpf[ILBC_SAMPLES];
00149       int i;
00150 
00151       /* Encode a frame of data */
00152       for (i = 0 ; i < ILBC_SAMPLES ; i++)
00153          tmpf[i] = tmp->buf[samples + i];
00154       iLBC_encode( pvt->outbuf.uc + datalen, tmpf, &tmp->enc);
00155 
00156       datalen += ILBC_FRAME_LEN;
00157       samples += ILBC_SAMPLES;
00158       pvt->samples -= ILBC_SAMPLES;
00159    }
00160 
00161    /* Move the data at the end of the buffer to the front */
00162    if (pvt->samples)
00163       memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
00164 
00165    return ast_trans_frameout(pvt, datalen, samples);
00166 }
00167 
00168 static struct ast_translator ilbctolin = {
00169    .name = "ilbctolin", 
00170    .srcfmt = AST_FORMAT_ILBC,
00171    .dstfmt = AST_FORMAT_SLINEAR,
00172    .newpvt = ilbctolin_new,
00173    .framein = ilbctolin_framein,
00174    .sample = ilbc_sample,
00175    .desc_size = sizeof(struct ilbc_coder_pvt),
00176    .buf_size = BUFFER_SAMPLES * 2,
00177    .native_plc = 1,
00178 };
00179 
00180 static struct ast_translator lintoilbc = {
00181    .name = "lintoilbc", 
00182    .srcfmt = AST_FORMAT_SLINEAR,
00183    .dstfmt = AST_FORMAT_ILBC,
00184    .newpvt = lintoilbc_new,
00185    .framein = lintoilbc_framein,
00186    .frameout = lintoilbc_frameout,
00187    .sample = slin8_sample,
00188    .desc_size = sizeof(struct ilbc_coder_pvt),
00189    .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
00190 };
00191 
00192 static int unload_module(void)
00193 {
00194    int res;
00195 
00196    res = ast_unregister_translator(&lintoilbc);
00197    res |= ast_unregister_translator(&ilbctolin);
00198 
00199    return res;
00200 }
00201 
00202 static int load_module(void)
00203 {
00204    int res;
00205 
00206    res = ast_register_translator(&ilbctolin);
00207    if (!res) 
00208       res=ast_register_translator(&lintoilbc);
00209    else
00210       ast_unregister_translator(&ilbctolin);
00211    if (res)
00212       return AST_MODULE_LOAD_FAILURE;
00213    return AST_MODULE_LOAD_SUCCESS;
00214 }
00215 
00216 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");