Wed Oct 28 11:51:02 2009

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: 125386 $")
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 (Mu data is okay) */
00042 
00043 #include "ulaw_slin_ex.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 /*
00078  * alawToLin_Sample. Just random data, somehow...
00079  */
00080 static struct ast_frame *alawtoulaw_sample(void)
00081 {
00082    static struct ast_frame f;
00083    f.frametype = AST_FRAME_VOICE;
00084    f.subclass = AST_FORMAT_ALAW;
00085    f.datalen = sizeof(ulaw_slin_ex);
00086    f.samples = sizeof(ulaw_slin_ex);
00087    f.mallocd = 0;
00088    f.offset = 0;
00089    f.src = __PRETTY_FUNCTION__;
00090    f.data.ptr = ulaw_slin_ex; /* XXX what ? */
00091    return &f;
00092 }
00093 
00094 static struct ast_frame *ulawtoalaw_sample(void)
00095 {
00096    static struct ast_frame f;
00097    f.frametype = AST_FRAME_VOICE;
00098    f.subclass = AST_FORMAT_ULAW;
00099    f.datalen = sizeof(ulaw_slin_ex);
00100    f.samples = sizeof(ulaw_slin_ex);
00101    f.mallocd = 0;
00102    f.offset = 0;
00103    f.src = __PRETTY_FUNCTION__;
00104    f.data.ptr = ulaw_slin_ex;
00105    return &f;
00106 }
00107 
00108 static struct ast_translator alawtoulaw = {
00109    .name = "alawtoulaw",
00110    .srcfmt = AST_FORMAT_ALAW,
00111    .dstfmt = AST_FORMAT_ULAW,
00112    .framein = alawtoulaw_framein,
00113    .sample = alawtoulaw_sample,
00114    .buffer_samples = BUFFER_SAMPLES,
00115    .buf_size = BUFFER_SAMPLES,
00116 };
00117 
00118 static struct ast_translator ulawtoalaw = {
00119    .name = "ulawtoalaw",
00120    .srcfmt = AST_FORMAT_ULAW,
00121    .dstfmt = AST_FORMAT_ALAW,
00122    .framein = ulawtoalaw_framein,
00123    .sample = ulawtoalaw_sample,
00124    .buffer_samples = BUFFER_SAMPLES,
00125    .buf_size = BUFFER_SAMPLES,
00126 };
00127 
00128 /*! \brief standard module glue */
00129 
00130 static int unload_module(void)
00131 {
00132    int res;
00133 
00134    res = ast_unregister_translator(&ulawtoalaw);
00135    res |= ast_unregister_translator(&alawtoulaw);
00136 
00137    return res;
00138 }
00139 
00140 static int load_module(void)
00141 {
00142    int res;
00143    int x;
00144 
00145    for (x=0;x<256;x++) {
00146       mu2a[x] = AST_LIN2A(AST_MULAW(x));
00147       a2mu[x] = AST_LIN2MU(AST_ALAW(x));
00148    }
00149    res = ast_register_translator(&alawtoulaw);
00150    if (!res)
00151       res = ast_register_translator(&ulawtoalaw);
00152    else
00153       ast_unregister_translator(&alawtoulaw);
00154    if (res)
00155       return AST_MODULE_LOAD_FAILURE;
00156    return AST_MODULE_LOAD_SUCCESS;
00157 }
00158 
00159 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");

Generated on Wed Oct 28 11:51:02 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6