Mon Mar 31 07:38:01 2008

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 /*** MODULEINFO
00027   <depend>spandsp</depend>
00028 ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00033 
00034 #include <fcntl.h>
00035 #include <netinet/in.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 
00041 #include <spandsp/bit_operations.h>
00042 #include <spandsp/g711.h>
00043 
00044 #include "asterisk/lock.h"
00045 #include "asterisk/logger.h"
00046 #include "asterisk/module.h"
00047 #include "asterisk/config.h"
00048 #include "asterisk/options.h"
00049 #include "asterisk/translate.h"
00050 #include "asterisk/channel.h"
00051 #include "asterisk/ulaw.h"
00052 #include "asterisk/utils.h"
00053 
00054 #define BUFFER_SAMPLES   8096 /* size for the translation buffers */
00055 
00056 /* Sample frame data */
00057 
00058 #include "slin_ulaw_ex.h"
00059 #include "ulaw_slin_ex.h"
00060 
00061 /*! \brief convert and store samples in outbuf */
00062 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00063 {
00064    int i = f->samples;
00065    unsigned char *src = f->data;
00066    int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
00067 
00068    pvt->samples += i;
00069    pvt->datalen += i * 2;  /* 2 bytes/sample */
00070 
00071    /* convert and copy in outbuf */
00072    while (i--)
00073       *dst++ = ulaw_to_linear(*src++);
00074 
00075    return 0;
00076 }
00077 
00078 /*! \brief convert and store samples in outbuf */
00079 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00080 {
00081    int i = f->samples;
00082    char *dst = pvt->outbuf + pvt->samples;
00083    int16_t *src = f->data;
00084 
00085    pvt->samples += i;
00086    pvt->datalen += i;   /* 1 byte/sample */
00087 
00088    while (i--)
00089       *dst++ = linear_to_ulaw(*src++);
00090 
00091    return 0;
00092 }
00093 
00094 /*!  * \brief ulawToLin_Sample */
00095 static struct ast_frame *ulawtolin_sample(void)
00096 {
00097    static struct ast_frame f;
00098    f.frametype = AST_FRAME_VOICE;
00099    f.subclass = AST_FORMAT_ULAW;
00100    f.datalen = sizeof(ulaw_slin_ex);
00101    f.samples = sizeof(ulaw_slin_ex);
00102    f.mallocd = 0;
00103    f.offset = 0;
00104    f.src = __PRETTY_FUNCTION__;
00105    f.data = ulaw_slin_ex;
00106    return &f;
00107 }
00108 
00109 /*!
00110  * \brief LinToulaw_Sample
00111  */
00112 
00113 static struct ast_frame *lintoulaw_sample(void)
00114 {
00115    static struct ast_frame f;
00116    f.frametype = AST_FRAME_VOICE;
00117    f.subclass = AST_FORMAT_SLINEAR;
00118    f.datalen = sizeof(slin_ulaw_ex);
00119    /* Assume 8000 Hz */
00120    f.samples = sizeof(slin_ulaw_ex) / 2;
00121    f.mallocd = 0;
00122    f.offset = 0;
00123    f.src = __PRETTY_FUNCTION__;
00124    f.data = slin_ulaw_ex;
00125    return &f;
00126 }
00127 
00128 /*!
00129  * \brief The complete translator for ulawToLin.
00130  */
00131 
00132 static struct ast_translator ulawtolin = {
00133    .name = "ulawtolin",
00134    .srcfmt = AST_FORMAT_ULAW,
00135    .dstfmt = AST_FORMAT_SLINEAR,
00136    .framein = ulawtolin_framein,
00137    .sample = ulawtolin_sample,
00138    .buffer_samples = BUFFER_SAMPLES,
00139    .buf_size = BUFFER_SAMPLES * 2,
00140    .plc_samples = 160,
00141 };
00142 
00143 /*!
00144  * \brief The complete translator for LinToulaw.
00145  */
00146 
00147 static struct ast_translator lintoulaw = {
00148    .name = "lintoulaw",
00149    .srcfmt = AST_FORMAT_SLINEAR,
00150    .dstfmt = AST_FORMAT_ULAW,
00151    .framein = lintoulaw_framein,
00152    .sample = lintoulaw_sample,
00153    .buf_size = BUFFER_SAMPLES,
00154    .buffer_samples = BUFFER_SAMPLES,
00155 };
00156 
00157 static void parse_config(void)
00158 {
00159    struct ast_variable *var;
00160    struct ast_config *cfg = ast_config_load("codecs.conf");
00161    if (!cfg)
00162       return;
00163    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00164       if (!strcasecmp(var->name, "genericplc")) {
00165          ulawtolin.useplc = ast_true(var->value) ? 1 : 0;
00166          if (option_verbose > 2)
00167             ast_verbose(VERBOSE_PREFIX_3 "codec_ulaw: %susing generic PLC\n", ulawtolin.useplc ? "" : "not ");
00168       }
00169    }
00170    ast_config_destroy(cfg);
00171 }
00172 
00173 static int reload(void)
00174 {
00175    parse_config();
00176 
00177    return 0;
00178 }
00179 
00180 static int unload_module(void)
00181 {
00182    int res;
00183 
00184    res = ast_unregister_translator(&lintoulaw);
00185    res |= ast_unregister_translator(&ulawtolin);
00186 
00187    return res;
00188 }
00189 
00190 static int load_module(void)
00191 {
00192    int res;
00193 
00194    parse_config();
00195    res = ast_register_translator(&ulawtolin);
00196    if (!res)
00197       res = ast_register_translator(&lintoulaw);
00198    else
00199       ast_unregister_translator(&ulawtolin);
00200 
00201    return res;
00202 }
00203 
00204 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00205       .load = load_module,
00206       .unload = unload_module,
00207       .reload = reload,
00208           );

Generated on Mon Mar 31 07:38:01 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1