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    <use>ilbc</use>
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419044 $")
00036 
00037 #include "asterisk/translate.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/utils.h"
00040 
00041 #ifdef ILBC_WEBRTC
00042 #include <ilbc.h>
00043 typedef WebRtc_UWord16 ilbc_bytes;
00044 typedef WebRtc_Word16  ilbc_block;
00045 #define BUF_TYPE i16
00046 #else
00047 #include "ilbc/iLBC_encode.h"
00048 #include "ilbc/iLBC_decode.h"
00049 typedef unsigned char ilbc_bytes;
00050 typedef float         ilbc_block;
00051 #define BUF_TYPE uc
00052 #endif
00053 
00054 #define USE_ILBC_ENHANCER  0
00055 #define ILBC_MS         30
00056 /* #define ILBC_MS         20 */
00057 
00058 #define  ILBC_FRAME_LEN 50 /* apparently... */
00059 #define  ILBC_SAMPLES   240   /* 30ms at 8000 hz */
00060 #define  BUFFER_SAMPLES 8000
00061 
00062 /* Sample frame data */
00063 #include "asterisk/slin.h"
00064 #include "ex_ilbc.h"
00065 
00066 struct ilbc_coder_pvt {
00067    iLBC_Enc_Inst_t enc;
00068    iLBC_Dec_Inst_t dec;
00069    /* Enough to store a full second */
00070    int16_t buf[BUFFER_SAMPLES];
00071 };
00072 
00073 static int lintoilbc_new(struct ast_trans_pvt *pvt)
00074 {
00075    struct ilbc_coder_pvt *tmp = pvt->pvt;
00076 
00077    initEncode(&tmp->enc, ILBC_MS);
00078 
00079    return 0;
00080 }
00081 
00082 static int ilbctolin_new(struct ast_trans_pvt *pvt)
00083 {
00084    struct ilbc_coder_pvt *tmp = pvt->pvt;
00085 
00086    initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
00087 
00088    return 0;
00089 }
00090 
00091 /*! \brief decode a frame and store in outbuf */
00092 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00093 {
00094    struct ilbc_coder_pvt *tmp = pvt->pvt;
00095    int plc_mode = 1; /* 1 = normal data, 0 = plc */
00096    /* Assuming there's space left, decode into the current buffer at
00097       the tail location.  Read in as many frames as there are */
00098    int x,i;
00099    int datalen = f->datalen;
00100    int16_t *dst = pvt->outbuf.i16;
00101    ilbc_block tmpf[ILBC_SAMPLES];
00102 
00103    if (!f->data.ptr && datalen) {
00104       ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set");
00105       f->datalen = 0;
00106       datalen = 0;
00107    }
00108 
00109    if (datalen == 0) { /* native PLC, set fake datalen and clear plc_mode */
00110       datalen = ILBC_FRAME_LEN;
00111       f->samples = ILBC_SAMPLES;
00112       plc_mode = 0;  /* do native plc */
00113       pvt->samples += ILBC_SAMPLES;
00114    }
00115 
00116    if (datalen % ILBC_FRAME_LEN) {
00117       ast_log(LOG_WARNING, "Huh?  An ilbc frame that isn't a multiple of 50 bytes long from %s (%d)?\n", f->src, datalen);
00118       return -1;
00119    }
00120 
00121    for (x=0; x < datalen ; x += ILBC_FRAME_LEN) {
00122       if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
00123          ast_log(LOG_WARNING, "Out of buffer space\n");
00124          return -1;
00125       }
00126       iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
00127       for ( i=0; i < ILBC_SAMPLES; i++)
00128          dst[pvt->samples + i] = tmpf[i];
00129       pvt->samples += ILBC_SAMPLES;
00130       pvt->datalen += 2*ILBC_SAMPLES;
00131    }
00132    return 0;
00133 }
00134 
00135 /*! \brief store a frame into a temporary buffer, for later decoding */
00136 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00137 {
00138    struct ilbc_coder_pvt *tmp = pvt->pvt;
00139 
00140    /* Just add the frames to our stream */
00141    /* XXX We should look at how old the rest of our stream is, and if it
00142       is too old, then we should overwrite it entirely, otherwise we can
00143       get artifacts of earlier talk that do not belong */
00144    memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
00145    pvt->samples += f->samples;
00146    return 0;
00147 }
00148 
00149 /*! \brief encode the temporary buffer and generate a frame */
00150 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
00151 {
00152    struct ilbc_coder_pvt *tmp = pvt->pvt;
00153    int datalen = 0;
00154    int samples = 0;
00155 
00156    /* We can't work on anything less than a frame in size */
00157    if (pvt->samples < ILBC_SAMPLES)
00158       return NULL;
00159    while (pvt->samples >= ILBC_SAMPLES) {
00160       ilbc_block tmpf[ILBC_SAMPLES];
00161       int i;
00162 
00163       /* Encode a frame of data */
00164       for (i = 0 ; i < ILBC_SAMPLES ; i++)
00165          tmpf[i] = tmp->buf[samples + i];
00166       iLBC_encode( (ilbc_bytes*)pvt->outbuf.BUF_TYPE + datalen, tmpf, &tmp->enc);
00167 
00168       datalen += ILBC_FRAME_LEN;
00169       samples += ILBC_SAMPLES;
00170       pvt->samples -= ILBC_SAMPLES;
00171    }
00172 
00173    /* Move the data at the end of the buffer to the front */
00174    if (pvt->samples)
00175       memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
00176 
00177    return ast_trans_frameout(pvt, datalen, samples);
00178 }
00179 
00180 static struct ast_translator ilbctolin = {
00181    .name = "ilbctolin",
00182    .src_codec = {
00183       .name = "ilbc",
00184       .type = AST_MEDIA_TYPE_AUDIO,
00185       .sample_rate = 8000,
00186    },
00187    .dst_codec = {
00188       .name = "slin",
00189       .type = AST_MEDIA_TYPE_AUDIO,
00190       .sample_rate = 8000,
00191    },
00192    .format = "slin",
00193    .newpvt = ilbctolin_new,
00194    .framein = ilbctolin_framein,
00195    .sample = ilbc_sample,
00196    .desc_size = sizeof(struct ilbc_coder_pvt),
00197    .buf_size = BUFFER_SAMPLES * 2,
00198    .native_plc = 1,
00199 };
00200 
00201 static struct ast_translator lintoilbc = {
00202    .name = "lintoilbc",
00203    .src_codec = {
00204       .name = "slin",
00205       .type = AST_MEDIA_TYPE_AUDIO,
00206       .sample_rate = 8000,
00207    },
00208    .dst_codec = {
00209       .name = "ilbc",
00210       .type = AST_MEDIA_TYPE_AUDIO,
00211       .sample_rate = 8000,
00212    },
00213    .format = "ilbc",
00214    .newpvt = lintoilbc_new,
00215    .framein = lintoilbc_framein,
00216    .frameout = lintoilbc_frameout,
00217    .sample = slin8_sample,
00218    .desc_size = sizeof(struct ilbc_coder_pvt),
00219    .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
00220 };
00221 
00222 static int unload_module(void)
00223 {
00224    int res;
00225 
00226    res = ast_unregister_translator(&lintoilbc);
00227    res |= ast_unregister_translator(&ilbctolin);
00228 
00229    return res;
00230 }
00231 
00232 static int load_module(void)
00233 {
00234    int res;
00235 
00236    res = ast_register_translator(&ilbctolin);
00237    res |= ast_register_translator(&lintoilbc);
00238 
00239    if (res) {
00240       unload_module();
00241       return AST_MODULE_LOAD_FAILURE;
00242    }
00243 
00244    return AST_MODULE_LOAD_SUCCESS;
00245 }
00246 
00247 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");

Generated on Thu Apr 16 06:27:30 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6