codec_a_mu.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_a_mu.c - translate between alaw and ulaw directly
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: 419044 $")
00033 
00034 #include "asterisk/module.h"
00035 #include "asterisk/translate.h"
00036 #include "asterisk/alaw.h"
00037 #include "asterisk/ulaw.h"
00038 #include "asterisk/utils.h"
00039 
00040 #define BUFFER_SAMPLES   8000 /* size for the translation buffers */
00041 
00042 static unsigned char mu2a[256];
00043 static unsigned char a2mu[256];
00044 
00045 /* Sample frame data */
00046 #include "ex_ulaw.h"
00047 #include "ex_alaw.h"
00048 
00049 /*! \brief convert frame data and store into the buffer */
00050 static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00051 {
00052    int x = f->samples;
00053    unsigned char *src = f->data.ptr;
00054    unsigned char *dst = pvt->outbuf.uc + pvt->samples;
00055 
00056    pvt->samples += x;
00057    pvt->datalen += x;
00058 
00059    while (x--)
00060       *dst++ = a2mu[*src++];
00061 
00062    return 0;
00063 }
00064 
00065 /*! \brief convert frame data and store into the buffer */
00066 static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00067 {
00068    int x = f->samples;
00069    unsigned char *src = f->data.ptr;
00070    unsigned char *dst = pvt->outbuf.uc + pvt->samples;
00071 
00072    pvt->samples += x;
00073    pvt->datalen += x;
00074 
00075    while (x--)
00076       *dst++ = mu2a[*src++];
00077 
00078    return 0;
00079 }
00080 
00081 static struct ast_translator alawtoulaw = {
00082    .name = "alawtoulaw",
00083    .src_codec = {
00084       .name = "alaw",
00085       .type = AST_MEDIA_TYPE_AUDIO,
00086       .sample_rate = 8000,
00087    },
00088    .dst_codec = {
00089       .name = "ulaw",
00090       .type = AST_MEDIA_TYPE_AUDIO,
00091       .sample_rate = 8000,
00092    },
00093    .format = "ulaw",
00094    .framein = alawtoulaw_framein,
00095    .sample = alaw_sample,
00096    .buffer_samples = BUFFER_SAMPLES,
00097    .buf_size = BUFFER_SAMPLES,
00098 };
00099 
00100 static struct ast_translator ulawtoalaw = {
00101    .name = "ulawtoalaw",
00102    .src_codec = {
00103       .name = "ulaw",
00104       .type = AST_MEDIA_TYPE_AUDIO,
00105       .sample_rate = 8000,
00106    },
00107    .dst_codec = {
00108       .name = "alaw",
00109       .type = AST_MEDIA_TYPE_AUDIO,
00110       .sample_rate = 8000,
00111    },
00112    .format = "alaw",
00113    .framein = ulawtoalaw_framein,
00114    .sample = ulaw_sample,
00115    .buffer_samples = BUFFER_SAMPLES,
00116    .buf_size = BUFFER_SAMPLES,
00117 };
00118 
00119 /*! \brief standard module glue */
00120 
00121 static int unload_module(void)
00122 {
00123    int res;
00124 
00125    res = ast_unregister_translator(&ulawtoalaw);
00126    res |= ast_unregister_translator(&alawtoulaw);
00127 
00128    return res;
00129 }
00130 
00131 static int load_module(void)
00132 {
00133    int res;
00134    int x;
00135 
00136    for (x=0;x<256;x++) {
00137       mu2a[x] = AST_LIN2A(AST_MULAW(x));
00138       a2mu[x] = AST_LIN2MU(AST_ALAW(x));
00139    }
00140 
00141    res = ast_register_translator(&alawtoulaw);
00142    res |= ast_register_translator(&ulawtoalaw);
00143 
00144    if (res) {
00145       unload_module();
00146       return AST_MODULE_LOAD_FAILURE;
00147    }
00148 
00149    return AST_MODULE_LOAD_SUCCESS;
00150 }
00151 
00152 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");

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