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: 419592 $")
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    .src_codec = {
00086       .name = "ulaw",
00087       .type = AST_MEDIA_TYPE_AUDIO,
00088       .sample_rate = 8000,
00089    },
00090    .dst_codec = {
00091       .name = "slin",
00092       .type = AST_MEDIA_TYPE_AUDIO,
00093       .sample_rate = 8000,
00094    },
00095    .format = "slin",
00096    .framein = ulawtolin_framein,
00097    .sample = ulaw_sample,
00098    .buffer_samples = BUFFER_SAMPLES,
00099    .buf_size = BUFFER_SAMPLES * 2,
00100 };
00101 
00102 static struct ast_translator testlawtolin = {
00103    .name = "testlawtolin",
00104    .src_codec = {
00105       .name = "testlaw",
00106       .type = AST_MEDIA_TYPE_AUDIO,
00107       .sample_rate = 8000,
00108    },
00109    .dst_codec = {
00110       .name = "slin",
00111       .type = AST_MEDIA_TYPE_AUDIO,
00112       .sample_rate = 8000,
00113    },
00114    .format = "slin",
00115    .framein = ulawtolin_framein,
00116    .sample = ulaw_sample,
00117    .buffer_samples = BUFFER_SAMPLES,
00118    .buf_size = BUFFER_SAMPLES * 2,
00119 };
00120 
00121 /*!
00122  * \brief The complete translator for LinToulaw.
00123  */
00124 
00125 static struct ast_translator lintoulaw = {
00126    .name = "lintoulaw",
00127    .src_codec = {
00128       .name = "slin",
00129       .type = AST_MEDIA_TYPE_AUDIO,
00130       .sample_rate = 8000,
00131    },
00132    .dst_codec = {
00133       .name = "ulaw",
00134       .type = AST_MEDIA_TYPE_AUDIO,
00135       .sample_rate = 8000,
00136    },
00137    .format = "ulaw",
00138    .framein = lintoulaw_framein,
00139    .sample = slin8_sample,
00140    .buf_size = BUFFER_SAMPLES,
00141    .buffer_samples = BUFFER_SAMPLES,
00142 };
00143 
00144 static struct ast_translator lintotestlaw = {
00145    .name = "lintotestlaw",
00146    .src_codec = {
00147       .name = "slin",
00148       .type = AST_MEDIA_TYPE_AUDIO,
00149       .sample_rate = 8000,
00150    },
00151    .dst_codec = {
00152       .name = "testlaw",
00153       .type = AST_MEDIA_TYPE_AUDIO,
00154       .sample_rate = 8000,
00155    },
00156    .format = "testlaw",
00157    .framein = lintoulaw_framein,
00158    .sample = slin8_sample,
00159    .buf_size = BUFFER_SAMPLES,
00160    .buffer_samples = BUFFER_SAMPLES,
00161 };
00162 
00163 static int unload_module(void)
00164 {
00165    int res;
00166 
00167    res = ast_unregister_translator(&lintoulaw);
00168    res |= ast_unregister_translator(&ulawtolin);
00169    res |= ast_unregister_translator(&testlawtolin);
00170    res |= ast_unregister_translator(&lintotestlaw);
00171 
00172    return res;
00173 }
00174 
00175 static int load_module(void)
00176 {
00177    int res;
00178 
00179    res = ast_register_translator(&ulawtolin);
00180    res |= ast_register_translator(&lintoulaw);
00181    res |= ast_register_translator(&lintotestlaw);
00182    res |= ast_register_translator(&testlawtolin);
00183 
00184    if (res) {
00185       unload_module();
00186       return AST_MODULE_LOAD_FAILURE;
00187    }
00188 
00189    return AST_MODULE_LOAD_SUCCESS;
00190 }
00191 
00192 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
00193       .support_level = AST_MODULE_SUPPORT_CORE,
00194       .load = load_module,
00195       .unload = unload_module,
00196           );

Generated on Thu Apr 16 06:27:30 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6