Mon Mar 31 07:38:01 2008

Asterisk developer's documentation


codec_alaw.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_alaw.c - translate between signed linear and alaw
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/alaw.h"
00052 #include "asterisk/utils.h"
00053 
00054 #define BUFFER_SAMPLES   8096 /* size for the translation buffers */
00055 
00056 /* Sample frame data (Mu data is okay) */
00057 
00058 #include "slin_ulaw_ex.h"
00059 #include "ulaw_slin_ex.h"
00060 
00061 /*! \brief decode frame into lin and fill output buffer. */
00062 static int alawtolin_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    while (i--)
00072       *dst++ = alaw_to_linear(*src++);
00073 
00074    return 0;
00075 }
00076 
00077 /*! \brief convert and store input samples in output buffer */
00078 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00079 {
00080    int i = f->samples;
00081    char *dst = pvt->outbuf + pvt->samples;
00082    int16_t *src = f->data;
00083 
00084    pvt->samples += i;
00085    pvt->datalen += i;   /* 1 byte/sample */
00086 
00087    while (i--) 
00088       *dst++ = linear_to_alaw(*src++);
00089 
00090    return 0;
00091 }
00092 
00093 /*! \brief alawToLin_Sample */
00094 static struct ast_frame *alawtolin_sample(void)
00095 {
00096    static struct ast_frame f;
00097    f.frametype = AST_FRAME_VOICE;
00098    f.subclass = AST_FORMAT_ALAW;
00099    f.datalen = sizeof(ulaw_slin_ex);
00100    f.samples = sizeof(ulaw_slin_ex);
00101    f.mallocd = 0;
00102    f.offset = 0;
00103    f.src = __PRETTY_FUNCTION__;
00104    f.data = ulaw_slin_ex;
00105    return &f;
00106 }
00107 
00108 /*! \brief LinToalaw_Sample */
00109 static struct ast_frame *lintoalaw_sample(void)
00110 {
00111    static struct ast_frame f;
00112    f.frametype = AST_FRAME_VOICE;
00113    f.subclass = AST_FORMAT_SLINEAR;
00114    f.datalen = sizeof(slin_ulaw_ex);
00115    f.samples = sizeof(slin_ulaw_ex) / 2;
00116    f.mallocd = 0;
00117    f.offset = 0;
00118    f.src = __PRETTY_FUNCTION__;
00119    f.data = slin_ulaw_ex;
00120    return &f;
00121 }
00122 
00123 static struct ast_translator alawtolin = {
00124    .name = "alawtolin",
00125    .srcfmt = AST_FORMAT_ALAW,
00126    .dstfmt = AST_FORMAT_SLINEAR,
00127    .framein = alawtolin_framein,
00128    .sample = alawtolin_sample,
00129    .buffer_samples = BUFFER_SAMPLES,
00130    .buf_size = BUFFER_SAMPLES * 2,
00131    .plc_samples = 160,
00132 };
00133 
00134 static struct ast_translator lintoalaw = {
00135    "lintoalaw",
00136    .srcfmt = AST_FORMAT_SLINEAR,
00137    .dstfmt = AST_FORMAT_ALAW,
00138    .framein = lintoalaw_framein,
00139    .sample = lintoalaw_sample,
00140    .buffer_samples = BUFFER_SAMPLES,
00141    .buf_size = BUFFER_SAMPLES,
00142 };
00143 
00144 static void parse_config(void)
00145 {
00146    struct ast_variable *var;
00147    struct ast_config *cfg = ast_config_load("codecs.conf");
00148    if (!cfg)
00149       return;
00150    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00151       if (!strcasecmp(var->name, "genericplc")) {
00152          alawtolin.useplc = ast_true(var->value) ? 1 : 0;
00153          if (option_verbose > 2)
00154             ast_verbose(VERBOSE_PREFIX_3 "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
00155       }
00156    }
00157    ast_config_destroy(cfg);
00158 }
00159 
00160 /*! \brief standard module stuff */
00161 
00162 static int reload(void)
00163 {
00164    parse_config();
00165    return 0;
00166 }
00167 
00168 static int unload_module(void)
00169 {
00170    int res;
00171 
00172    res = ast_unregister_translator(&lintoalaw);
00173    res |= ast_unregister_translator(&alawtolin);
00174 
00175    return res;
00176 }
00177 
00178 static int load_module(void)
00179 {
00180    int res;
00181 
00182    parse_config();
00183    res = ast_register_translator(&alawtolin);
00184    if (!res)
00185       res = ast_register_translator(&lintoalaw);
00186    else
00187       ast_unregister_translator(&alawtolin);
00188 
00189    return res;
00190 }
00191 
00192 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
00193       .load = load_module,
00194       .unload = unload_module,
00195       .reload = reload,
00196           );

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