Wed Oct 28 11:51:02 2009

Asterisk developer's documentation


codec_ulaw.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  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief codec_ulaw.c - translate between signed linear and ulaw
00022  * 
00023  * \ingroup codecs
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 125386 $")
00029 
00030 #include "asterisk/module.h"
00031 #include "asterisk/config.h"
00032 #include "asterisk/translate.h"
00033 #include "asterisk/ulaw.h"
00034 #include "asterisk/utils.h"
00035 
00036 #define BUFFER_SAMPLES   8096 /* size for the translation buffers */
00037 
00038 /* Sample frame data */
00039 
00040 #include "slin_ulaw_ex.h"
00041 #include "ulaw_slin_ex.h"
00042 
00043 /*! \brief convert and store samples in outbuf */
00044 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00045 {
00046    int i = f->samples;
00047    unsigned char *src = f->data.ptr;
00048    int16_t *dst = pvt->outbuf.i16 + pvt->samples;
00049 
00050    pvt->samples += i;
00051    pvt->datalen += i * 2;  /* 2 bytes/sample */
00052 
00053    /* convert and copy in outbuf */
00054    while (i--)
00055       *dst++ = AST_MULAW(*src++);
00056 
00057    return 0;
00058 }
00059 
00060 /*! \brief convert and store samples in outbuf */
00061 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00062 {
00063    int i = f->samples;
00064    char *dst = pvt->outbuf.c + pvt->samples;
00065    int16_t *src = f->data.ptr;
00066 
00067    pvt->samples += i;
00068    pvt->datalen += i;   /* 1 byte/sample */
00069 
00070    while (i--)
00071       *dst++ = AST_LIN2MU(*src++);
00072 
00073    return 0;
00074 }
00075 
00076 /*!  * \brief ulawToLin_Sample */
00077 static struct ast_frame *ulawtolin_sample(void)
00078 {
00079    static struct ast_frame f;
00080    f.frametype = AST_FRAME_VOICE;
00081    f.subclass = AST_FORMAT_ULAW;
00082    f.datalen = sizeof(ulaw_slin_ex);
00083    f.samples = sizeof(ulaw_slin_ex);
00084    f.mallocd = 0;
00085    f.offset = 0;
00086    f.src = __PRETTY_FUNCTION__;
00087    f.data.ptr = ulaw_slin_ex;
00088    return &f;
00089 }
00090 
00091 /*!
00092  * \brief LinToulaw_Sample
00093  */
00094 
00095 static struct ast_frame *lintoulaw_sample(void)
00096 {
00097    static struct ast_frame f;
00098    f.frametype = AST_FRAME_VOICE;
00099    f.subclass = AST_FORMAT_SLINEAR;
00100    f.datalen = sizeof(slin_ulaw_ex);
00101    /* Assume 8000 Hz */
00102    f.samples = sizeof(slin_ulaw_ex) / 2;
00103    f.mallocd = 0;
00104    f.offset = 0;
00105    f.src = __PRETTY_FUNCTION__;
00106    f.data.ptr = slin_ulaw_ex;
00107    return &f;
00108 }
00109 
00110 /*!
00111  * \brief The complete translator for ulawToLin.
00112  */
00113 
00114 static struct ast_translator ulawtolin = {
00115    .name = "ulawtolin",
00116    .srcfmt = AST_FORMAT_ULAW,
00117    .dstfmt = AST_FORMAT_SLINEAR,
00118    .framein = ulawtolin_framein,
00119    .sample = ulawtolin_sample,
00120    .buffer_samples = BUFFER_SAMPLES,
00121    .buf_size = BUFFER_SAMPLES * 2,
00122    .plc_samples = 160,
00123 };
00124 
00125 /*!
00126  * \brief The complete translator for LinToulaw.
00127  */
00128 
00129 static struct ast_translator lintoulaw = {
00130    .name = "lintoulaw",
00131    .srcfmt = AST_FORMAT_SLINEAR,
00132    .dstfmt = AST_FORMAT_ULAW,
00133    .framein = lintoulaw_framein,
00134    .sample = lintoulaw_sample,
00135    .buf_size = BUFFER_SAMPLES,
00136    .buffer_samples = BUFFER_SAMPLES,
00137 };
00138 
00139 static int parse_config(int reload)
00140 {
00141    struct ast_variable *var;
00142    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00143    struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
00144    if (cfg == NULL)
00145       return 0;
00146    if (cfg == CONFIG_STATUS_FILEUNCHANGED)
00147       return 0;
00148    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00149       if (!strcasecmp(var->name, "genericplc")) {
00150          ulawtolin.useplc = ast_true(var->value) ? 1 : 0;
00151          ast_verb(3, "codec_ulaw: %susing generic PLC\n", ulawtolin.useplc ? "" : "not ");
00152       }
00153    }
00154    ast_config_destroy(cfg);
00155    return 0;
00156 }
00157 
00158 static int reload(void)
00159 {
00160    if (parse_config(1))
00161       return AST_MODULE_LOAD_DECLINE;
00162    return AST_MODULE_LOAD_SUCCESS;
00163 }
00164 
00165 static int unload_module(void)
00166 {
00167    int res;
00168 
00169    res = ast_unregister_translator(&lintoulaw);
00170    res |= ast_unregister_translator(&ulawtolin);
00171 
00172    return res;
00173 }
00174 
00175 static int load_module(void)
00176 {
00177    int res;
00178 
00179    if (parse_config(0))
00180       return AST_MODULE_LOAD_DECLINE;
00181    res = ast_register_translator(&ulawtolin);
00182    if (!res)
00183       res = ast_register_translator(&lintoulaw);
00184    else
00185       ast_unregister_translator(&ulawtolin);
00186    if (res)
00187       return AST_MODULE_LOAD_FAILURE;
00188    return AST_MODULE_LOAD_SUCCESS;
00189 }
00190 
00191 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00192       .load = load_module,
00193       .unload = unload_module,
00194       .reload = reload,
00195           );

Generated on Wed Oct 28 11:51:02 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6