codec_gsm.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * The GSM code is from TOAST.  Copyright information for that package is available
00005  * in the GSM directory.
00006  *
00007  * Copyright (C) 1999 - 2005, Digium, Inc.
00008  *
00009  * Mark Spencer <markster@digium.com>
00010  *
00011  * See http://www.asterisk.org for more information about
00012  * the Asterisk project. Please do not directly contact
00013  * any of the maintainers of this project for assistance;
00014  * the project provides a web site, mailing lists and IRC
00015  * channels for your use.
00016  *
00017  * This program is free software, distributed under the terms of
00018  * the GNU General Public License Version 2. See the LICENSE file
00019  * at the top of the source tree.
00020  */
00021 
00022 /*! \file
00023  *
00024  * \brief Translate between signed linear and Global System for Mobile Communications (GSM)
00025  *
00026  * \ingroup codecs
00027  */
00028 
00029 /*** MODULEINFO
00030    <depend>gsm</depend>
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 #ifdef HAVE_GSM_HEADER
00044 #include "gsm.h"
00045 #elif defined(HAVE_GSM_GSM_HEADER)
00046 #include <gsm/gsm.h>
00047 #endif
00048 
00049 #include "../formats/msgsm.h"
00050 
00051 #define BUFFER_SAMPLES  8000
00052 #define GSM_SAMPLES  160
00053 #define  GSM_FRAME_LEN  33
00054 #define  MSGSM_FRAME_LEN   65
00055 
00056 /* Sample frame data */
00057 #include "asterisk/slin.h"
00058 #include "ex_gsm.h"
00059 
00060 struct gsm_translator_pvt {   /* both gsm2lin and lin2gsm */
00061    gsm gsm;
00062    int16_t buf[BUFFER_SAMPLES];  /* lin2gsm, temporary storage */
00063 };
00064 
00065 static int gsm_new(struct ast_trans_pvt *pvt)
00066 {
00067    struct gsm_translator_pvt *tmp = pvt->pvt;
00068    
00069    return (tmp->gsm = gsm_create()) ? 0 : -1;
00070 }
00071 
00072 /*! \brief decode and store in outbuf. */
00073 static int gsmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00074 {
00075    struct gsm_translator_pvt *tmp = pvt->pvt;
00076    int x;
00077    int16_t *dst = pvt->outbuf.i16;
00078    /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
00079    int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
00080       MSGSM_FRAME_LEN : GSM_FRAME_LEN;
00081 
00082    for (x=0; x < f->datalen; x += flen) {
00083       unsigned char data[2 * GSM_FRAME_LEN];
00084       unsigned char *src;
00085       int len;
00086       if (flen == MSGSM_FRAME_LEN) {
00087          len = 2*GSM_SAMPLES;
00088          src = data;
00089          /* Translate MSGSM format to Real GSM format before feeding in */
00090          /* XXX what's the point here! we should just work
00091           * on the full format.
00092           */
00093          conv65(f->data.ptr + x, data);
00094       } else {
00095          len = GSM_SAMPLES;
00096          src = f->data.ptr + x;
00097       }
00098       /* XXX maybe we don't need to check */
00099       if (pvt->samples + len > BUFFER_SAMPLES) {   
00100          ast_log(LOG_WARNING, "Out of buffer space\n");
00101          return -1;
00102       }
00103       if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) {
00104          ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
00105          return -1;
00106       }
00107       pvt->samples += GSM_SAMPLES;
00108       pvt->datalen += 2 * GSM_SAMPLES;
00109       if (flen == MSGSM_FRAME_LEN) {
00110          if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) {
00111             ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
00112             return -1;
00113          }
00114          pvt->samples += GSM_SAMPLES;
00115          pvt->datalen += 2 * GSM_SAMPLES;
00116       }
00117    }
00118    return 0;
00119 }
00120 
00121 /*! \brief store samples into working buffer for later decode */
00122 static int lintogsm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00123 {
00124    struct gsm_translator_pvt *tmp = pvt->pvt;
00125 
00126    /* XXX We should look at how old the rest of our stream is, and if it
00127       is too old, then we should overwrite it entirely, otherwise we can
00128       get artifacts of earlier talk that do not belong */
00129    if (pvt->samples + f->samples > BUFFER_SAMPLES) {
00130       ast_log(LOG_WARNING, "Out of buffer space\n");
00131       return -1;
00132    }
00133    memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
00134    pvt->samples += f->samples;
00135    return 0;
00136 }
00137 
00138 /*! \brief encode and produce a frame */
00139 static struct ast_frame *lintogsm_frameout(struct ast_trans_pvt *pvt)
00140 {
00141    struct gsm_translator_pvt *tmp = pvt->pvt;
00142    int datalen = 0;
00143    int samples = 0;
00144 
00145    /* We can't work on anything less than a frame in size */
00146    if (pvt->samples < GSM_SAMPLES)
00147       return NULL;
00148    while (pvt->samples >= GSM_SAMPLES) {
00149       /* Encode a frame of data */
00150       gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c + datalen);
00151       datalen += GSM_FRAME_LEN;
00152       samples += GSM_SAMPLES;
00153       pvt->samples -= GSM_SAMPLES;
00154    }
00155 
00156    /* Move the data at the end of the buffer to the front */
00157    if (pvt->samples)
00158       memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
00159 
00160    return ast_trans_frameout(pvt, datalen, samples);
00161 }
00162 
00163 static void gsm_destroy_stuff(struct ast_trans_pvt *pvt)
00164 {
00165    struct gsm_translator_pvt *tmp = pvt->pvt;
00166    if (tmp->gsm)
00167       gsm_destroy(tmp->gsm);
00168 }
00169 
00170 static struct ast_translator gsmtolin = {
00171    .name = "gsmtolin",
00172    .src_codec = {
00173       .name = "gsm",
00174       .type = AST_MEDIA_TYPE_AUDIO,
00175       .sample_rate = 8000,
00176    },
00177    .dst_codec = {
00178       .name = "slin",
00179       .type = AST_MEDIA_TYPE_AUDIO,
00180       .sample_rate = 8000,
00181    },
00182    .format = "slin",
00183    .newpvt = gsm_new,
00184    .framein = gsmtolin_framein,
00185    .destroy = gsm_destroy_stuff,
00186    .sample = gsm_sample,
00187    .buffer_samples = BUFFER_SAMPLES,
00188    .buf_size = BUFFER_SAMPLES * 2,
00189    .desc_size = sizeof (struct gsm_translator_pvt ),
00190 };
00191 
00192 static struct ast_translator lintogsm = {
00193    .name = "lintogsm",
00194    .src_codec = {
00195       .name = "slin",
00196       .type = AST_MEDIA_TYPE_AUDIO,
00197       .sample_rate = 8000,
00198    },
00199    .dst_codec = {
00200       .name = "gsm",
00201       .type = AST_MEDIA_TYPE_AUDIO,
00202       .sample_rate = 8000,
00203    },
00204    .format = "gsm",
00205    .newpvt = gsm_new,
00206    .framein = lintogsm_framein,
00207    .frameout = lintogsm_frameout,
00208    .destroy = gsm_destroy_stuff,
00209    .sample = slin8_sample,
00210    .desc_size = sizeof (struct gsm_translator_pvt ),
00211    .buf_size = (BUFFER_SAMPLES * GSM_FRAME_LEN + GSM_SAMPLES - 1)/GSM_SAMPLES,
00212 };
00213 
00214 static int unload_module(void)
00215 {
00216    int res;
00217 
00218    res = ast_unregister_translator(&lintogsm);
00219    res |= ast_unregister_translator(&gsmtolin);
00220 
00221    return res;
00222 }
00223 
00224 static int load_module(void)
00225 {
00226    int res;
00227 
00228    res = ast_register_translator(&gsmtolin);
00229    res |= ast_register_translator(&lintogsm);
00230 
00231    if (res) {
00232       unload_module();
00233       return AST_MODULE_LOAD_FAILURE;
00234    }
00235 
00236    return AST_MODULE_LOAD_SUCCESS;
00237 }
00238 
00239 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GSM Coder/Decoder",
00240       .support_level = AST_MODULE_SUPPORT_CORE,
00241       .load = load_module,
00242       .unload = unload_module,
00243           );

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