Asterisk - The Open Source Telephony Project  18.5.0
codec_ulaw.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 <[email protected]>
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_ulaw.c - translate between signed linear and ulaw
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/config.h"
34 #include "asterisk/translate.h"
35 #include "asterisk/ulaw.h"
36 #include "asterisk/utils.h"
37 
38 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
39 
40 /* Sample frame data */
41 #include "asterisk/slin.h"
42 #include "ex_ulaw.h"
43 
44 /*! \brief convert and store samples in outbuf */
45 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
46 {
47  int i = f->samples;
48  unsigned char *src = f->data.ptr;
49  int16_t *dst = pvt->outbuf.i16 + pvt->samples;
50 
51  pvt->samples += i;
52  pvt->datalen += i * 2; /* 2 bytes/sample */
53 
54  /* convert and copy in outbuf */
55  while (i--)
56  *dst++ = AST_MULAW(*src++);
57 
58  return 0;
59 }
60 
61 /*! \brief convert and store samples in outbuf */
62 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
63 {
64  int i = f->samples;
65  char *dst = pvt->outbuf.c + pvt->samples;
66  int16_t *src = f->data.ptr;
67 
68  pvt->samples += i;
69  pvt->datalen += i; /* 1 byte/sample */
70 
71  while (i--)
72  *dst++ = AST_LIN2MU(*src++);
73 
74  return 0;
75 }
76 
77 /*!
78  * \brief The complete translator for ulawToLin.
79  */
80 
81 static struct ast_translator ulawtolin = {
82  .name = "ulawtolin",
83  .src_codec = {
84  .name = "ulaw",
85  .type = AST_MEDIA_TYPE_AUDIO,
86  .sample_rate = 8000,
87  },
88  .dst_codec = {
89  .name = "slin",
90  .type = AST_MEDIA_TYPE_AUDIO,
91  .sample_rate = 8000,
92  },
93  .format = "slin",
94  .framein = ulawtolin_framein,
95  .sample = ulaw_sample,
96  .buffer_samples = BUFFER_SAMPLES,
97  .buf_size = BUFFER_SAMPLES * 2,
98 };
99 
100 static struct ast_translator testlawtolin = {
101  .name = "testlawtolin",
102  .src_codec = {
103  .name = "testlaw",
104  .type = AST_MEDIA_TYPE_AUDIO,
105  .sample_rate = 8000,
106  },
107  .dst_codec = {
108  .name = "slin",
109  .type = AST_MEDIA_TYPE_AUDIO,
110  .sample_rate = 8000,
111  },
112  .format = "slin",
113  .framein = ulawtolin_framein,
114  .sample = ulaw_sample,
115  .buffer_samples = BUFFER_SAMPLES,
116  .buf_size = BUFFER_SAMPLES * 2,
117 };
118 
119 /*!
120  * \brief The complete translator for LinToulaw.
121  */
122 
123 static struct ast_translator lintoulaw = {
124  .name = "lintoulaw",
125  .src_codec = {
126  .name = "slin",
127  .type = AST_MEDIA_TYPE_AUDIO,
128  .sample_rate = 8000,
129  },
130  .dst_codec = {
131  .name = "ulaw",
132  .type = AST_MEDIA_TYPE_AUDIO,
133  .sample_rate = 8000,
134  },
135  .format = "ulaw",
136  .framein = lintoulaw_framein,
137  .sample = slin8_sample,
138  .buf_size = BUFFER_SAMPLES,
139  .buffer_samples = BUFFER_SAMPLES,
140 };
141 
142 static struct ast_translator lintotestlaw = {
143  .name = "lintotestlaw",
144  .src_codec = {
145  .name = "slin",
146  .type = AST_MEDIA_TYPE_AUDIO,
147  .sample_rate = 8000,
148  },
149  .dst_codec = {
150  .name = "testlaw",
151  .type = AST_MEDIA_TYPE_AUDIO,
152  .sample_rate = 8000,
153  },
154  .format = "testlaw",
155  .framein = lintoulaw_framein,
156  .sample = slin8_sample,
157  .buf_size = BUFFER_SAMPLES,
158  .buffer_samples = BUFFER_SAMPLES,
159 };
160 
161 static int unload_module(void)
162 {
163  int res;
164 
165  res = ast_unregister_translator(&lintoulaw);
166  res |= ast_unregister_translator(&ulawtolin);
167  res |= ast_unregister_translator(&testlawtolin);
168  res |= ast_unregister_translator(&lintotestlaw);
169 
170  return res;
171 }
172 
173 static int load_module(void)
174 {
175  int res;
176 
177  res = ast_register_translator(&ulawtolin);
178  res |= ast_register_translator(&lintoulaw);
179  res |= ast_register_translator(&lintotestlaw);
180  res |= ast_register_translator(&testlawtolin);
181 
182  if (res) {
183  unload_module();
185  }
186 
188 }
189 
190 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
191  .support_level = AST_MODULE_SUPPORT_CORE,
192  .load = load_module,
193  .unload = unload_module,
194 );
int datalen
actual space used in outbuf
Definition: translate.h:218
Asterisk main include file. File version handling, generic pbx functions.
Descriptor of a translator.
Definition: translate.h:137
short int16_t
Definition: db.h:59
Support for translation of data formats. translate.c.
static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert and store samples in outbuf
Definition: codec_ulaw.c:45
8-bit data
static struct ast_frame * slin8_sample(void)
Definition: slin.h:64
static struct ast_translator ulawtolin
The complete translator for ulawToLin.
Definition: codec_ulaw.c:81
int16_t * i16
Definition: translate.h:223
Utility functions.
Configuration File Parser.
static struct ast_translator testlawtolin
Definition: codec_ulaw.c:100
static int unload_module(void)
Definition: codec_ulaw.c:161
u-Law to Signed linear conversion
static struct ast_translator lintotestlaw
Definition: codec_ulaw.c:142
#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:1333
static int load_module(void)
Definition: codec_ulaw.c:173
union ast_trans_pvt::@327 outbuf
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
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static struct ast_frame * ulaw_sample(void)
Definition: ex_ulaw.h:26
static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert and store samples in outbuf
Definition: codec_ulaw.c:62
Data structure associated with a single frame of data.
#define BUFFER_SAMPLES
Definition: codec_ulaw.c:38
static struct ast_translator lintoulaw
The complete translator for LinToulaw.
Definition: codec_ulaw.c:123
union ast_frame::@263 data
#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