Sat Apr 26 2014 22:01:35

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    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328259 $")
00033 
00034 #include "asterisk/module.h"
00035 #include "asterisk/config.h"
00036 #include "asterisk/translate.h"
00037 #include "asterisk/ulaw.h"
00038 #include "asterisk/utils.h"
00039 
00040 #define BUFFER_SAMPLES   8096 /* size for the translation buffers */
00041 
00042 /* Sample frame data */
00043 #include "asterisk/slin.h"
00044 #include "ex_ulaw.h"
00045 
00046 /*! \brief convert and store samples in outbuf */
00047 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00048 {
00049    int i = f->samples;
00050    unsigned char *src = f->data.ptr;
00051    int16_t *dst = pvt->outbuf.i16 + pvt->samples;
00052 
00053    pvt->samples += i;
00054    pvt->datalen += i * 2;  /* 2 bytes/sample */
00055 
00056    /* convert and copy in outbuf */
00057    while (i--)
00058       *dst++ = AST_MULAW(*src++);
00059 
00060    return 0;
00061 }
00062 
00063 /*! \brief convert and store samples in outbuf */
00064 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00065 {
00066    int i = f->samples;
00067    char *dst = pvt->outbuf.c + pvt->samples;
00068    int16_t *src = f->data.ptr;
00069 
00070    pvt->samples += i;
00071    pvt->datalen += i;   /* 1 byte/sample */
00072 
00073    while (i--)
00074       *dst++ = AST_LIN2MU(*src++);
00075 
00076    return 0;
00077 }
00078 
00079 /*!
00080  * \brief The complete translator for ulawToLin.
00081  */
00082 
00083 static struct ast_translator ulawtolin = {
00084    .name = "ulawtolin",
00085    .framein = ulawtolin_framein,
00086    .sample = ulaw_sample,
00087    .buffer_samples = BUFFER_SAMPLES,
00088    .buf_size = BUFFER_SAMPLES * 2,
00089 };
00090 
00091 static struct ast_translator testlawtolin = {
00092    .name = "testlawtolin",
00093    .framein = ulawtolin_framein,
00094    .sample = ulaw_sample,
00095    .buffer_samples = BUFFER_SAMPLES,
00096    .buf_size = BUFFER_SAMPLES * 2,
00097 };
00098 
00099 /*!
00100  * \brief The complete translator for LinToulaw.
00101  */
00102 
00103 static struct ast_translator lintoulaw = {
00104    .name = "lintoulaw",
00105    .framein = lintoulaw_framein,
00106    .sample = slin8_sample,
00107    .buf_size = BUFFER_SAMPLES,
00108    .buffer_samples = BUFFER_SAMPLES,
00109 };
00110 
00111 static struct ast_translator lintotestlaw = {
00112    .name = "lintotestlaw",
00113    .framein = lintoulaw_framein,
00114    .sample = slin8_sample,
00115    .buf_size = BUFFER_SAMPLES,
00116    .buffer_samples = BUFFER_SAMPLES,
00117 };
00118 
00119 static int reload(void)
00120 {
00121    return AST_MODULE_LOAD_SUCCESS;
00122 }
00123 
00124 static int unload_module(void)
00125 {
00126    int res;
00127 
00128    res = ast_unregister_translator(&lintoulaw);
00129    res |= ast_unregister_translator(&ulawtolin);
00130    res |= ast_unregister_translator(&testlawtolin);
00131    res |= ast_unregister_translator(&lintotestlaw);
00132 
00133    return res;
00134 }
00135 
00136 static int load_module(void)
00137 {
00138    int res;
00139 
00140    ast_format_set(&lintoulaw.src_format, AST_FORMAT_SLINEAR, 0);
00141    ast_format_set(&lintoulaw.dst_format, AST_FORMAT_ULAW, 0);
00142 
00143    ast_format_set(&lintotestlaw.src_format, AST_FORMAT_SLINEAR, 0);
00144    ast_format_set(&lintotestlaw.dst_format, AST_FORMAT_TESTLAW, 0);
00145 
00146    ast_format_set(&ulawtolin.src_format, AST_FORMAT_ULAW, 0);
00147    ast_format_set(&ulawtolin.dst_format, AST_FORMAT_SLINEAR, 0);
00148 
00149    ast_format_set(&testlawtolin.src_format, AST_FORMAT_TESTLAW, 0);
00150    ast_format_set(&testlawtolin.dst_format, AST_FORMAT_SLINEAR, 0);
00151 
00152    res = ast_register_translator(&ulawtolin);
00153    if (!res) {
00154       res = ast_register_translator(&lintoulaw);
00155       res |= ast_register_translator(&lintotestlaw);
00156       res |= ast_register_translator(&testlawtolin);
00157    } else
00158       ast_unregister_translator(&ulawtolin);
00159    if (res)
00160       return AST_MODULE_LOAD_FAILURE;
00161    return AST_MODULE_LOAD_SUCCESS;
00162 }
00163 
00164 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00165       .load = load_module,
00166       .unload = unload_module,
00167       .reload = reload,
00168           );