Wed Apr 6 11:29:44 2011

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 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 267492 $")
00029 
00030 #include "asterisk/module.h"
00031 #include "asterisk/config.h"
00032 #include "asterisk/translate.h"
00033 #include "asterisk/ulaw.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_ulaw.h"
00041 
00042 /*! \brief convert and store samples in outbuf */
00043 static int ulawtolin_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    /* convert and copy in outbuf */
00053    while (i--)
00054       *dst++ = AST_MULAW(*src++);
00055 
00056    return 0;
00057 }
00058 
00059 /*! \brief convert and store samples in outbuf */
00060 static int lintoulaw_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_LIN2MU(*src++);
00071 
00072    return 0;
00073 }
00074 
00075 /*!
00076  * \brief The complete translator for ulawToLin.
00077  */
00078 
00079 static struct ast_translator ulawtolin = {
00080    .name = "ulawtolin",
00081    .srcfmt = AST_FORMAT_ULAW,
00082    .dstfmt = AST_FORMAT_SLINEAR,
00083    .framein = ulawtolin_framein,
00084    .sample = ulaw_sample,
00085    .buffer_samples = BUFFER_SAMPLES,
00086    .buf_size = BUFFER_SAMPLES * 2,
00087 };
00088 
00089 static struct ast_translator testlawtolin = {
00090    .name = "testlawtolin",
00091    .srcfmt = AST_FORMAT_TESTLAW,
00092    .dstfmt = AST_FORMAT_SLINEAR,
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    .srcfmt = AST_FORMAT_SLINEAR,
00106    .dstfmt = AST_FORMAT_ULAW,
00107    .framein = lintoulaw_framein,
00108    .sample = slin8_sample,
00109    .buf_size = BUFFER_SAMPLES,
00110    .buffer_samples = BUFFER_SAMPLES,
00111 };
00112 
00113 static struct ast_translator lintotestlaw = {
00114    .name = "lintotestlaw",
00115    .srcfmt = AST_FORMAT_SLINEAR,
00116    .dstfmt = AST_FORMAT_TESTLAW,
00117    .framein = lintoulaw_framein,
00118    .sample = slin8_sample,
00119    .buf_size = BUFFER_SAMPLES,
00120    .buffer_samples = BUFFER_SAMPLES,
00121 };
00122 
00123 static int reload(void)
00124 {
00125    return AST_MODULE_LOAD_SUCCESS;
00126 }
00127 
00128 static int unload_module(void)
00129 {
00130    int res;
00131 
00132    res = ast_unregister_translator(&lintoulaw);
00133    res |= ast_unregister_translator(&ulawtolin);
00134    res |= ast_unregister_translator(&testlawtolin);
00135    res |= ast_unregister_translator(&lintotestlaw);
00136 
00137    return res;
00138 }
00139 
00140 static int load_module(void)
00141 {
00142    int res;
00143 
00144    res = ast_register_translator(&ulawtolin);
00145    if (!res) {
00146       res = ast_register_translator(&lintoulaw);
00147       res |= ast_register_translator(&lintotestlaw);
00148       res |= ast_register_translator(&testlawtolin);
00149    } else
00150       ast_unregister_translator(&ulawtolin);
00151    if (res)
00152       return AST_MODULE_LOAD_FAILURE;
00153    return AST_MODULE_LOAD_SUCCESS;
00154 }
00155 
00156 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00157       .load = load_module,
00158       .unload = unload_module,
00159       .reload = reload,
00160           );

Generated on Wed Apr 6 11:29:44 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7