Mon Jun 27 16:50:53 2011

Asterisk developer's documentation


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

Generated on Mon Jun 27 16:50:53 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7