codec_lpc10.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * The lpc10 code is from a library used by nautilus, modified to be a bit
00009  * nicer to the compiler.
00010  * See http://www.arl.wustl.edu/~jaf/ 
00011  *
00012  * See http://www.asterisk.org for more information about
00013  * the Asterisk project. Please do not directly contact
00014  * any of the maintainers of this project for assistance;
00015  * the project provides a web site, mailing lists and IRC
00016  * channels for your use.
00017  *
00018  * This program is free software, distributed under the terms of
00019  * the GNU General Public License Version 2. See the LICENSE file
00020  * at the top of the source tree.
00021  */
00022 
00023 /*! \file
00024  *
00025  * \brief Translate between signed linear and LPC10 (Linear Predictor Code)
00026  *
00027  * \ingroup codecs
00028  */
00029 
00030 /*** MODULEINFO
00031    <support_level>core</support_level>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00037 
00038 #include "asterisk/translate.h"
00039 #include "asterisk/config.h"
00040 #include "asterisk/module.h"
00041 #include "asterisk/utils.h"
00042 
00043 #include "lpc10/lpc10.h"
00044 
00045 /* Sample frame data */
00046 #include "asterisk/slin.h"
00047 #include "ex_lpc10.h"
00048 
00049 /* We use a very strange format here...  I have no idea why...  The frames are 180
00050    samples long, which isn't even an even number of milliseconds...  Not only that
00051    but we hvae to waste two bits of each frame to keep them ending on a byte boundary
00052    because the frames are 54 bits long */
00053 
00054 #define LPC10_BYTES_IN_COMPRESSED_FRAME (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8
00055 
00056 #define  BUFFER_SAMPLES 8000
00057 
00058 struct lpc10_coder_pvt {
00059    union {
00060       struct lpc10_encoder_state *enc;
00061       struct lpc10_decoder_state *dec;
00062    } lpc10;
00063    /* Enough to store a full second */
00064    short buf[BUFFER_SAMPLES];
00065    int longer;
00066 };
00067 
00068 static int lpc10_enc_new(struct 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 }
00074 
00075 static int lpc10_dec_new(struct 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 }
00081 
00082 static void extract_bits(INT32 *bits, unsigned char *c)
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 }
00094 
00095 /* XXX note lpc10_encode() produces one bit per word in bits[] */
00096 static void build_bits(unsigned char *c, INT32 *bits)
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 }
00112 
00113 static int lpc10tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
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 }
00145 
00146 static int lintolpc10_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
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 }
00159 
00160 static struct ast_frame *lintolpc10_frameout(struct ast_trans_pvt *pvt)
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 }
00189 
00190 
00191 static void lpc10_destroy(struct ast_trans_pvt *arg)
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 }
00197 
00198 static struct ast_translator lpc10tolin = {
00199    .name = "lpc10tolin",
00200    .src_codec = {
00201       .name = "lpc10",
00202       .type = AST_MEDIA_TYPE_AUDIO,
00203       .sample_rate = 8000,
00204    },
00205    .dst_codec = {
00206       .name = "slin",
00207       .type = AST_MEDIA_TYPE_AUDIO,
00208       .sample_rate = 8000,
00209    },
00210    .format = "slin",
00211    .newpvt = lpc10_dec_new,
00212    .framein = lpc10tolin_framein,
00213    .destroy = lpc10_destroy,
00214    .sample = lpc10_sample,
00215    .desc_size = sizeof(struct lpc10_coder_pvt),
00216    .buffer_samples = BUFFER_SAMPLES,
00217    .buf_size = BUFFER_SAMPLES * 2,
00218 };
00219 
00220 static struct ast_translator lintolpc10 = {
00221    .name = "lintolpc10",
00222    .src_codec = {
00223       .name = "slin",
00224       .type = AST_MEDIA_TYPE_AUDIO,
00225       .sample_rate = 8000,
00226    },
00227    .dst_codec = {
00228       .name = "lpc10",
00229       .type = AST_MEDIA_TYPE_AUDIO,
00230       .sample_rate = 8000,
00231    },
00232    .format = "lpc10",
00233    .newpvt = lpc10_enc_new,
00234    .framein = lintolpc10_framein,
00235    .frameout = lintolpc10_frameout,
00236    .destroy = lpc10_destroy,
00237    .sample = slin8_sample,
00238    .desc_size = sizeof(struct lpc10_coder_pvt),
00239    .buffer_samples = BUFFER_SAMPLES,
00240    .buf_size = LPC10_BYTES_IN_COMPRESSED_FRAME * (1 + BUFFER_SAMPLES / LPC10_SAMPLES_PER_FRAME),
00241 };
00242 
00243 static int unload_module(void)
00244 {
00245    int res;
00246 
00247    res = ast_unregister_translator(&lintolpc10);
00248    res |= ast_unregister_translator(&lpc10tolin);
00249 
00250    return res;
00251 }
00252 
00253 static int load_module(void)
00254 {
00255    int res;
00256 
00257    res = ast_register_translator(&lpc10tolin);
00258    res |= ast_register_translator(&lintolpc10);
00259 
00260    if (res) {
00261       unload_module();
00262       return AST_MODULE_LOAD_FAILURE;
00263    }
00264 
00265    return AST_MODULE_LOAD_SUCCESS;
00266 }
00267 
00268 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "LPC10 2.4kbps Coder/Decoder",
00269       .support_level = AST_MODULE_SUPPORT_CORE,
00270       .load = load_module,
00271       .unload = unload_module,
00272           );

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