Sat Aug 6 00:39:28 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: 267539 $")
00029 
00030 #include <fcntl.h>
00031 #include <netinet/in.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036 
00037 #include "asterisk/lock.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/config.h"
00041 #include "asterisk/options.h"
00042 #include "asterisk/translate.h"
00043 #include "asterisk/channel.h"
00044 #include "asterisk/ulaw.h"
00045 #include "asterisk/utils.h"
00046 
00047 #define BUFFER_SAMPLES   8096 /* size for the translation buffers */
00048 
00049 /* Sample frame data */
00050 
00051 #include "slin_ulaw_ex.h"
00052 #include "ulaw_slin_ex.h"
00053 
00054 /*! \brief convert and store samples in outbuf */
00055 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00056 {
00057    int i = f->samples;
00058    unsigned char *src = f->data;
00059    int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
00060 
00061    pvt->samples += i;
00062    pvt->datalen += i * 2;  /* 2 bytes/sample */
00063 
00064    /* convert and copy in outbuf */
00065    while (i--)
00066       *dst++ = AST_MULAW(*src++);
00067 
00068    return 0;
00069 }
00070 
00071 /*! \brief convert and store samples in outbuf */
00072 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00073 {
00074    int i = f->samples;
00075    char *dst = pvt->outbuf + pvt->samples;
00076    int16_t *src = f->data;
00077 
00078    pvt->samples += i;
00079    pvt->datalen += i;   /* 1 byte/sample */
00080 
00081    while (i--)
00082       *dst++ = AST_LIN2MU(*src++);
00083 
00084    return 0;
00085 }
00086 
00087 /*!  * \brief ulawToLin_Sample */
00088 static struct ast_frame *ulawtolin_sample(void)
00089 {
00090    static struct ast_frame f;
00091    f.frametype = AST_FRAME_VOICE;
00092    f.subclass = AST_FORMAT_ULAW;
00093    f.datalen = sizeof(ulaw_slin_ex);
00094    f.samples = sizeof(ulaw_slin_ex);
00095    f.mallocd = 0;
00096    f.offset = 0;
00097    f.src = __PRETTY_FUNCTION__;
00098    f.data = ulaw_slin_ex;
00099    return &f;
00100 }
00101 
00102 /*!
00103  * \brief LinToulaw_Sample
00104  */
00105 
00106 static struct ast_frame *lintoulaw_sample(void)
00107 {
00108    static struct ast_frame f;
00109    f.frametype = AST_FRAME_VOICE;
00110    f.subclass = AST_FORMAT_SLINEAR;
00111    f.datalen = sizeof(slin_ulaw_ex);
00112    /* Assume 8000 Hz */
00113    f.samples = sizeof(slin_ulaw_ex) / 2;
00114    f.mallocd = 0;
00115    f.offset = 0;
00116    f.src = __PRETTY_FUNCTION__;
00117    f.data = slin_ulaw_ex;
00118    return &f;
00119 }
00120 
00121 /*!
00122  * \brief The complete translator for ulawToLin.
00123  */
00124 
00125 static struct ast_translator ulawtolin = {
00126    .name = "ulawtolin",
00127    .srcfmt = AST_FORMAT_ULAW,
00128    .dstfmt = AST_FORMAT_SLINEAR,
00129    .framein = ulawtolin_framein,
00130    .sample = ulawtolin_sample,
00131    .buffer_samples = BUFFER_SAMPLES,
00132    .buf_size = BUFFER_SAMPLES * 2,
00133 };
00134 
00135 /*!
00136  * \brief The complete translator for LinToulaw.
00137  */
00138 
00139 static struct ast_translator lintoulaw = {
00140    .name = "lintoulaw",
00141    .srcfmt = AST_FORMAT_SLINEAR,
00142    .dstfmt = AST_FORMAT_ULAW,
00143    .framein = lintoulaw_framein,
00144    .sample = lintoulaw_sample,
00145    .buf_size = BUFFER_SAMPLES,
00146    .buffer_samples = BUFFER_SAMPLES,
00147 };
00148 
00149 static int reload(void)
00150 {
00151    return 0;
00152 }
00153 
00154 static int unload_module(void)
00155 {
00156    int res;
00157 
00158    res = ast_unregister_translator(&lintoulaw);
00159    res |= ast_unregister_translator(&ulawtolin);
00160 
00161    return res;
00162 }
00163 
00164 static int load_module(void)
00165 {
00166    int res;
00167 
00168    res = ast_register_translator(&ulawtolin);
00169    if (!res)
00170       res = ast_register_translator(&lintoulaw);
00171    else
00172       ast_unregister_translator(&ulawtolin);
00173 
00174    return res;
00175 }
00176 
00177 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00178       .load = load_module,
00179       .unload = unload_module,
00180       .reload = reload,
00181           );

Generated on Sat Aug 6 00:39:28 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7