Mon Sep 20 2010 00:22:30

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: 150729 $")
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 */
00039 #include "asterisk/slin.h"
00040 #include "ex_alaw.h"
00041 
00042 /*! \brief decode frame into lin and fill output buffer. */
00043 static int alawtolin_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    while (i--)
00053       *dst++ = AST_ALAW(*src++);
00054 
00055    return 0;
00056 }
00057 
00058 /*! \brief convert and store input samples in output buffer */
00059 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00060 {
00061    int i = f->samples;
00062    char *dst = pvt->outbuf.c + pvt->samples;
00063    int16_t *src = f->data.ptr;
00064 
00065    pvt->samples += i;
00066    pvt->datalen += i;   /* 1 byte/sample */
00067 
00068    while (i--) 
00069       *dst++ = AST_LIN2A(*src++);
00070 
00071    return 0;
00072 }
00073 
00074 static struct ast_translator alawtolin = {
00075    .name = "alawtolin",
00076    .srcfmt = AST_FORMAT_ALAW,
00077    .dstfmt = AST_FORMAT_SLINEAR,
00078    .framein = alawtolin_framein,
00079    .sample = alaw_sample,
00080    .buffer_samples = BUFFER_SAMPLES,
00081    .buf_size = BUFFER_SAMPLES * 2,
00082    .plc_samples = 160,
00083 };
00084 
00085 static struct ast_translator lintoalaw = {
00086    "lintoalaw",
00087    .srcfmt = AST_FORMAT_SLINEAR,
00088    .dstfmt = AST_FORMAT_ALAW,
00089    .framein = lintoalaw_framein,
00090    .sample = slin8_sample,
00091    .buffer_samples = BUFFER_SAMPLES,
00092    .buf_size = BUFFER_SAMPLES,
00093 };
00094 
00095 static int parse_config(int reload)
00096 {
00097    struct ast_variable *var;
00098    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
00099    struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
00100    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
00101       return 0;
00102    for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
00103       if (!strcasecmp(var->name, "genericplc")) {
00104          alawtolin.useplc = ast_true(var->value) ? 1 : 0;
00105          ast_verb(3, "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
00106       }
00107    }
00108    ast_config_destroy(cfg);
00109    return 0;
00110 }
00111 
00112 /*! \brief standard module stuff */
00113 
00114 static int reload(void)
00115 {
00116    if (parse_config(1))
00117       return AST_MODULE_LOAD_DECLINE;
00118    return AST_MODULE_LOAD_SUCCESS;
00119 }
00120 
00121 static int unload_module(void)
00122 {
00123    int res;
00124 
00125    res = ast_unregister_translator(&lintoalaw);
00126    res |= ast_unregister_translator(&alawtolin);
00127 
00128    return res;
00129 }
00130 
00131 static int load_module(void)
00132 {
00133    int res;
00134 
00135    if (parse_config(0))
00136       return AST_MODULE_LOAD_DECLINE;
00137    res = ast_register_translator(&alawtolin);
00138    if (!res)
00139       res = ast_register_translator(&lintoalaw);
00140    else
00141       ast_unregister_translator(&alawtolin);
00142    if (res)
00143       return AST_MODULE_LOAD_FAILURE;
00144    return AST_MODULE_LOAD_SUCCESS;
00145 }
00146 
00147 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
00148       .load = load_module,
00149       .unload = unload_module,
00150       .reload = reload,
00151           );