Thu Apr 28 2011 17:15:21

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: 267507 $")
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 /*!
00090  * \brief The complete translator for LinToulaw.
00091  */
00092 
00093 static struct ast_translator lintoulaw = {
00094    .name = "lintoulaw",
00095    .srcfmt = AST_FORMAT_SLINEAR,
00096    .dstfmt = AST_FORMAT_ULAW,
00097    .framein = lintoulaw_framein,
00098    .sample = slin8_sample,
00099    .buf_size = BUFFER_SAMPLES,
00100    .buffer_samples = BUFFER_SAMPLES,
00101 };
00102 
00103 static int reload(void)
00104 {
00105    return AST_MODULE_LOAD_SUCCESS;
00106 }
00107 
00108 static int unload_module(void)
00109 {
00110    int res;
00111 
00112    res = ast_unregister_translator(&lintoulaw);
00113    res |= ast_unregister_translator(&ulawtolin);
00114 
00115    return res;
00116 }
00117 
00118 static int load_module(void)
00119 {
00120    int res;
00121 
00122    res = ast_register_translator(&ulawtolin);
00123    if (!res)
00124       res = ast_register_translator(&lintoulaw);
00125    else
00126       ast_unregister_translator(&ulawtolin);
00127    if (res)
00128       return AST_MODULE_LOAD_FAILURE;
00129    return AST_MODULE_LOAD_SUCCESS;
00130 }
00131 
00132 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00133       .load = load_module,
00134       .unload = unload_module,
00135       .reload = reload,
00136           );