Asterisk - The Open Source Telephony Project  GIT-master-0190e70
codec_a_mu.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief codec_a_mu.c - translate between alaw and ulaw directly
22  *
23  * \ingroup codecs
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include "asterisk/module.h"
33 #include "asterisk/translate.h"
34 #include "asterisk/alaw.h"
35 #include "asterisk/ulaw.h"
36 #include "asterisk/utils.h"
37 
38 #define BUFFER_SAMPLES 8000 /* size for the translation buffers */
39 
40 static unsigned char mu2a[256];
41 static unsigned char a2mu[256];
42 
43 /* Sample frame data */
44 #include "ex_ulaw.h"
45 #include "ex_alaw.h"
46 
47 /*! \brief convert frame data and store into the buffer */
48 static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
49 {
50  int x = f->samples;
51  unsigned char *src = f->data.ptr;
52  unsigned char *dst = pvt->outbuf.uc + pvt->samples;
53 
54  pvt->samples += x;
55  pvt->datalen += x;
56 
57  while (x--)
58  *dst++ = a2mu[*src++];
59 
60  return 0;
61 }
62 
63 /*! \brief convert frame data and store into the buffer */
64 static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
65 {
66  int x = f->samples;
67  unsigned char *src = f->data.ptr;
68  unsigned char *dst = pvt->outbuf.uc + pvt->samples;
69 
70  pvt->samples += x;
71  pvt->datalen += x;
72 
73  while (x--)
74  *dst++ = mu2a[*src++];
75 
76  return 0;
77 }
78 
79 static struct ast_translator alawtoulaw = {
80  .name = "alawtoulaw",
81  .src_codec = {
82  .name = "alaw",
83  .type = AST_MEDIA_TYPE_AUDIO,
84  .sample_rate = 8000,
85  },
86  .dst_codec = {
87  .name = "ulaw",
88  .type = AST_MEDIA_TYPE_AUDIO,
89  .sample_rate = 8000,
90  },
91  .format = "ulaw",
92  .framein = alawtoulaw_framein,
93  .sample = alaw_sample,
94  .buffer_samples = BUFFER_SAMPLES,
95  .buf_size = BUFFER_SAMPLES,
96 };
97 
98 static struct ast_translator ulawtoalaw = {
99  .name = "ulawtoalaw",
100  .src_codec = {
101  .name = "ulaw",
102  .type = AST_MEDIA_TYPE_AUDIO,
103  .sample_rate = 8000,
104  },
105  .dst_codec = {
106  .name = "alaw",
107  .type = AST_MEDIA_TYPE_AUDIO,
108  .sample_rate = 8000,
109  },
110  .format = "alaw",
111  .framein = ulawtoalaw_framein,
112  .sample = ulaw_sample,
113  .buffer_samples = BUFFER_SAMPLES,
114  .buf_size = BUFFER_SAMPLES,
115 };
116 
117 /*! \brief standard module glue */
118 
119 static int unload_module(void)
120 {
121  int res;
122 
123  res = ast_unregister_translator(&ulawtoalaw);
124  res |= ast_unregister_translator(&alawtoulaw);
125 
126  return res;
127 }
128 
129 static int load_module(void)
130 {
131  int res;
132  int x;
133 
134  for (x=0;x<256;x++) {
135  mu2a[x] = AST_LIN2A(AST_MULAW(x));
136  a2mu[x] = AST_LIN2MU(AST_ALAW(x));
137  }
138 
139  res = ast_register_translator(&alawtoulaw);
140  res |= ast_register_translator(&ulawtoalaw);
141 
142  if (res) {
143  unload_module();
145  }
146 
148 }
149 
150 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");
A-Law to Signed linear conversion.
int datalen
actual space used in outbuf
Definition: translate.h:218
static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert frame data and store into the buffer
Definition: codec_a_mu.c:64
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
8-bit data
Asterisk main include file. File version handling, generic pbx functions.
Descriptor of a translator.
Definition: translate.h:137
union ast_trans_pvt::@321 outbuf
Support for translation of data formats. translate.c.
#define AST_ALAW(a)
Definition: alaw.h:84
union ast_frame::@257 data
unsigned char * uc
Definition: translate.h:222
8-bit data
Utility functions.
static int unload_module(void)
standard module glue
Definition: codec_a_mu.c:119
u-Law to Signed linear conversion
#define AST_MULAW(a)
Definition: ulaw.h:85
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:257
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:1327
static struct ast_translator alawtoulaw
Definition: codec_a_mu.c:79
#define BUFFER_SAMPLES
Definition: codec_a_mu.c:38
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:213
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct ast_frame * ulaw_sample(void)
Definition: ex_ulaw.h:26
static int load_module(void)
Definition: codec_a_mu.c:129
static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert frame data and store into the buffer
Definition: codec_a_mu.c:48
Data structure associated with a single frame of data.
static struct ast_frame * alaw_sample(void)
Definition: ex_alaw.h:26
static struct ast_translator ulawtoalaw
Definition: codec_a_mu.c:98
static unsigned char a2mu[256]
Definition: codec_a_mu.c:41
static unsigned char mu2a[256]
Definition: codec_a_mu.c:40
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define AST_LIN2MU(a)
Definition: ulaw.h:49
char name[80]
Definition: translate.h:138
#define AST_LIN2A(a)
Definition: alaw.h:50