Mon Sep 20 2010 00:22:31

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: 150729 $")
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 #include "asterisk/slin.h"
00040 #include "ex_ulaw.h"
00041 
00042 /*! \brief convert and store samples in outbuf */
00043 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00044 {
00045    int i = f->samples;
00046    unsigned char *src = f->data.ptr;
00047    int16_t *dst = pvt->outbuf.i16 + pvt->samples;
00048 
00049    pvt->samples += i;
00050    pvt->datalen += i * 2;  /* 2 bytes/sample */
00051 
00052    /* convert and copy in outbuf */
00053    while (i--)
00054       *dst++ = AST_MULAW(*src++);
00055 
00056    return 0;
00057 }
00058 
00059 /*! \brief convert and store samples in outbuf */
00060 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00061 {
00062    int i = f->samples;
00063    char *dst = pvt->outbuf.c + pvt->samples;
00064    int16_t *src = f->data.ptr;
00065 
00066    pvt->samples += i;
00067    pvt->datalen += i;   /* 1 byte/sample */
00068 
00069    while (i--)
00070       *dst++ = AST_LIN2MU(*src++);
00071 
00072    return 0;
00073 }
00074 
00075 /*!
00076  * \brief The complete translator for ulawToLin.
00077  */
00078 
00079 static struct ast_translator ulawtolin = {
00080    .name = "ulawtolin",
00081    .srcfmt = AST_FORMAT_ULAW,
00082    .dstfmt = AST_FORMAT_SLINEAR,
00083    .framein = ulawtolin_framein,
00084    .sample = ulaw_sample,
00085    .buffer_samples = BUFFER_SAMPLES,
00086    .buf_size = BUFFER_SAMPLES * 2,
00087    .plc_samples = 160,
00088 };
00089 
00090 /*!
00091  * \brief The complete translator for LinToulaw.
00092  */
00093 
00094 static struct ast_translator lintoulaw = {
00095    .name = "lintoulaw",
00096    .srcfmt = AST_FORMAT_SLINEAR,
00097    .dstfmt = AST_FORMAT_ULAW,
00098    .framein = lintoulaw_framein,
00099    .sample = slin8_sample,
00100    .buf_size = BUFFER_SAMPLES,
00101    .buffer_samples = BUFFER_SAMPLES,
00102 };
00103 
00104 static int parse_config(int reload)
00105 {
00106    struct ast_variable *var;
00107    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00108    struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
00109    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
00110       return 0;
00111    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00112       if (!strcasecmp(var->name, "genericplc")) {
00113          ulawtolin.useplc = ast_true(var->value) ? 1 : 0;
00114          ast_verb(3, "codec_ulaw: %susing generic PLC\n", ulawtolin.useplc ? "" : "not ");
00115       }
00116    }
00117    ast_config_destroy(cfg);
00118    return 0;
00119 }
00120 
00121 static int reload(void)
00122 {
00123    if (parse_config(1))
00124       return AST_MODULE_LOAD_DECLINE;
00125    return AST_MODULE_LOAD_SUCCESS;
00126 }
00127 
00128 static int unload_module(void)
00129 {
00130    int res;
00131 
00132    res = ast_unregister_translator(&lintoulaw);
00133    res |= ast_unregister_translator(&ulawtolin);
00134 
00135    return res;
00136 }
00137 
00138 static int load_module(void)
00139 {
00140    int res;
00141 
00142    if (parse_config(0))
00143       return AST_MODULE_LOAD_DECLINE;
00144    res = ast_register_translator(&ulawtolin);
00145    if (!res)
00146       res = ast_register_translator(&lintoulaw);
00147    else
00148       ast_unregister_translator(&ulawtolin);
00149    if (res)
00150       return AST_MODULE_LOAD_FAILURE;
00151    return AST_MODULE_LOAD_SUCCESS;
00152 }
00153 
00154 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00155       .load = load_module,
00156       .unload = unload_module,
00157       .reload = reload,
00158           );