Thu Oct 11 06:33:45 2012

Asterisk developer's documentation


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: 328259 $")
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    .newpvt = gsm_new,
00173    .framein = gsmtolin_framein,
00174    .destroy = gsm_destroy_stuff,
00175    .sample = gsm_sample,
00176    .buffer_samples = BUFFER_SAMPLES,
00177    .buf_size = BUFFER_SAMPLES * 2,
00178    .desc_size = sizeof (struct gsm_translator_pvt ),
00179 };
00180 
00181 static struct ast_translator lintogsm = {
00182    .name = "lintogsm", 
00183    .newpvt = gsm_new,
00184    .framein = lintogsm_framein,
00185    .frameout = lintogsm_frameout,
00186    .destroy = gsm_destroy_stuff,
00187    .sample = slin8_sample,
00188    .desc_size = sizeof (struct gsm_translator_pvt ),
00189    .buf_size = (BUFFER_SAMPLES * GSM_FRAME_LEN + GSM_SAMPLES - 1)/GSM_SAMPLES,
00190 };
00191 
00192 /*! \brief standard module glue */
00193 static int reload(void)
00194 {
00195    return AST_MODULE_LOAD_SUCCESS;
00196 }
00197 
00198 static int unload_module(void)
00199 {
00200    int res;
00201 
00202    res = ast_unregister_translator(&lintogsm);
00203    if (!res)
00204       res = ast_unregister_translator(&gsmtolin);
00205 
00206    return res;
00207 }
00208 
00209 static int load_module(void)
00210 {
00211    int res;
00212 
00213    ast_format_set(&gsmtolin.src_format, AST_FORMAT_GSM, 0);
00214    ast_format_set(&gsmtolin.dst_format, AST_FORMAT_SLINEAR, 0);
00215 
00216    ast_format_set(&lintogsm.src_format, AST_FORMAT_SLINEAR, 0);
00217    ast_format_set(&lintogsm.dst_format, AST_FORMAT_GSM, 0);
00218 
00219    res = ast_register_translator(&gsmtolin);
00220    if (!res) 
00221       res=ast_register_translator(&lintogsm);
00222    else
00223       ast_unregister_translator(&gsmtolin);
00224    if (res) 
00225       return AST_MODULE_LOAD_FAILURE;
00226    return AST_MODULE_LOAD_SUCCESS;
00227 }
00228 
00229 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GSM Coder/Decoder",
00230       .load = load_module,
00231       .unload = unload_module,
00232       .reload = reload,
00233           );

Generated on Thu Oct 11 06:33:45 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6