00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 40722 $")
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/translate.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/alaw.h"
00043 #include "asterisk/ulaw.h"
00044 #include "asterisk/utils.h"
00045
00046 #define BUFFER_SAMPLES 8000
00047
00048 static unsigned char mu2a[256];
00049 static unsigned char a2mu[256];
00050
00051
00052
00053 #include "ulaw_slin_ex.h"
00054
00055
00056 static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00057 {
00058 int x = f->samples;
00059 unsigned char *src = f->data;
00060 unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
00061
00062 pvt->samples += x;
00063 pvt->datalen += x;
00064
00065 while (x--)
00066 *dst++ = a2mu[*src++];
00067
00068 return 0;
00069 }
00070
00071
00072 static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00073 {
00074 int x = f->samples;
00075 unsigned char *src = f->data;
00076 unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
00077
00078 pvt->samples += x;
00079 pvt->datalen += x;
00080
00081 while (x--)
00082 *dst++ = mu2a[*src++];
00083
00084 return 0;
00085 }
00086
00087
00088
00089
00090 static struct ast_frame *alawtoulaw_sample(void)
00091 {
00092 static struct ast_frame f;
00093 f.frametype = AST_FRAME_VOICE;
00094 f.subclass = AST_FORMAT_ALAW;
00095 f.datalen = sizeof(ulaw_slin_ex);
00096 f.samples = sizeof(ulaw_slin_ex);
00097 f.mallocd = 0;
00098 f.offset = 0;
00099 f.src = __PRETTY_FUNCTION__;
00100 f.data = ulaw_slin_ex;
00101 return &f;
00102 }
00103
00104 static struct ast_frame *ulawtoalaw_sample(void)
00105 {
00106 static struct ast_frame f;
00107 f.frametype = AST_FRAME_VOICE;
00108 f.subclass = AST_FORMAT_ULAW;
00109 f.datalen = sizeof(ulaw_slin_ex);
00110 f.samples = sizeof(ulaw_slin_ex);
00111 f.mallocd = 0;
00112 f.offset = 0;
00113 f.src = __PRETTY_FUNCTION__;
00114 f.data = ulaw_slin_ex;
00115 return &f;
00116 }
00117
00118 static struct ast_translator alawtoulaw = {
00119 .name = "alawtoulaw",
00120 .srcfmt = AST_FORMAT_ALAW,
00121 .dstfmt = AST_FORMAT_ULAW,
00122 .framein = alawtoulaw_framein,
00123 .sample = alawtoulaw_sample,
00124 .buffer_samples = BUFFER_SAMPLES,
00125 .buf_size = BUFFER_SAMPLES,
00126 };
00127
00128 static struct ast_translator ulawtoalaw = {
00129 .name = "ulawtoalaw",
00130 .srcfmt = AST_FORMAT_ULAW,
00131 .dstfmt = AST_FORMAT_ALAW,
00132 .framein = ulawtoalaw_framein,
00133 .sample = ulawtoalaw_sample,
00134 .buffer_samples = BUFFER_SAMPLES,
00135 .buf_size = BUFFER_SAMPLES,
00136 };
00137
00138
00139
00140 static int unload_module(void)
00141 {
00142 int res;
00143
00144 res = ast_unregister_translator(&ulawtoalaw);
00145 res |= ast_unregister_translator(&alawtoulaw);
00146
00147 return res;
00148 }
00149
00150 static int load_module(void)
00151 {
00152 int res;
00153 int x;
00154
00155 for (x=0;x<256;x++) {
00156 mu2a[x] = AST_LIN2A(AST_MULAW(x));
00157 a2mu[x] = AST_LIN2MU(AST_ALAW(x));
00158 }
00159 res = ast_register_translator(&alawtoulaw);
00160 if (!res)
00161 res = ast_register_translator(&ulawtoalaw);
00162 else
00163 ast_unregister_translator(&alawtoulaw);
00164
00165 return res;
00166 }
00167
00168 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");