Wed Oct 28 11:51:02 2009

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

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