Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


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 <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_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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
33 
34 #include "asterisk/module.h"
35 #include "asterisk/config.h"
36 #include "asterisk/translate.h"
37 #include "asterisk/ulaw.h"
38 #include "asterisk/utils.h"
39 
40 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
41 
42 /* Sample frame data */
43 #include "asterisk/slin.h"
44 #include "ex_ulaw.h"
45 
46 /*! \brief convert and store samples in outbuf */
47 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
48 {
49  int i = f->samples;
50  unsigned char *src = f->data.ptr;
51  int16_t *dst = pvt->outbuf.i16 + pvt->samples;
52 
53  pvt->samples += i;
54  pvt->datalen += i * 2; /* 2 bytes/sample */
55 
56  /* convert and copy in outbuf */
57  while (i--)
58  *dst++ = AST_MULAW(*src++);
59 
60  return 0;
61 }
62 
63 /*! \brief convert and store samples in outbuf */
64 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
65 {
66  int i = f->samples;
67  char *dst = pvt->outbuf.c + pvt->samples;
68  int16_t *src = f->data.ptr;
69 
70  pvt->samples += i;
71  pvt->datalen += i; /* 1 byte/sample */
72 
73  while (i--)
74  *dst++ = AST_LIN2MU(*src++);
75 
76  return 0;
77 }
78 
79 /*!
80  * \brief The complete translator for ulawToLin.
81  */
82 
83 static struct ast_translator ulawtolin = {
84  .name = "ulawtolin",
85  .srcfmt = AST_FORMAT_ULAW,
86  .dstfmt = AST_FORMAT_SLINEAR,
87  .framein = ulawtolin_framein,
88  .sample = ulaw_sample,
89  .buffer_samples = BUFFER_SAMPLES,
90  .buf_size = BUFFER_SAMPLES * 2,
91 };
92 
93 static struct ast_translator testlawtolin = {
94  .name = "testlawtolin",
95  .srcfmt = AST_FORMAT_TESTLAW,
96  .dstfmt = AST_FORMAT_SLINEAR,
97  .framein = ulawtolin_framein,
98  .sample = ulaw_sample,
99  .buffer_samples = BUFFER_SAMPLES,
100  .buf_size = BUFFER_SAMPLES * 2,
101 };
102 
103 /*!
104  * \brief The complete translator for LinToulaw.
105  */
106 
107 static struct ast_translator lintoulaw = {
108  .name = "lintoulaw",
109  .srcfmt = AST_FORMAT_SLINEAR,
110  .dstfmt = AST_FORMAT_ULAW,
111  .framein = lintoulaw_framein,
112  .sample = slin8_sample,
113  .buf_size = BUFFER_SAMPLES,
114  .buffer_samples = BUFFER_SAMPLES,
115 };
116 
117 static struct ast_translator lintotestlaw = {
118  .name = "lintotestlaw",
119  .srcfmt = AST_FORMAT_SLINEAR,
120  .dstfmt = AST_FORMAT_TESTLAW,
121  .framein = lintoulaw_framein,
122  .sample = slin8_sample,
123  .buf_size = BUFFER_SAMPLES,
124  .buffer_samples = BUFFER_SAMPLES,
125 };
126 
127 static int reload(void)
128 {
130 }
131 
132 static int unload_module(void)
133 {
134  int res;
135 
136  res = ast_unregister_translator(&lintoulaw);
137  res |= ast_unregister_translator(&ulawtolin);
138  res |= ast_unregister_translator(&testlawtolin);
139  res |= ast_unregister_translator(&lintotestlaw);
140 
141  return res;
142 }
143 
144 static int load_module(void)
145 {
146  int res;
147 
148  res = ast_register_translator(&ulawtolin);
149  if (!res) {
150  res = ast_register_translator(&lintoulaw);
151  res |= ast_register_translator(&lintotestlaw);
152  res |= ast_register_translator(&testlawtolin);
153  } else
154  ast_unregister_translator(&ulawtolin);
155  if (res)
158 }
159 
160 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
161  .load = load_module,
162  .unload = unload_module,
163  .reload = reload,
164  );
int datalen
actual space used in outbuf
Definition: translate.h:140
Asterisk main include file. File version handling, generic pbx functions.
union ast_trans_pvt::@213 outbuf
Descriptor of a translator.
Definition: translate.h:71
Support for translation of data formats. translate.c.
void * ptr
Definition: frame.h:160
static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert and store samples in outbuf
Definition: codec_ulaw.c:47
Configuration File Parser.
const char name[80]
Definition: translate.h:72
static int reload(void)
Definition: codec_ulaw.c:127
8-bit data
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
static struct ast_frame * slin8_sample(void)
Definition: slin.h:61
static struct ast_translator ulawtolin
The complete translator for ulawToLin.
Definition: codec_ulaw.c:83
int16_t * i16
Definition: translate.h:145
Utility functions.
static struct ast_translator testlawtolin
Definition: codec_ulaw.c:93
static int unload_module(void)
Definition: codec_ulaw.c:132
u-Law to Signed linear conversion
static struct ast_translator lintotestlaw
Definition: codec_ulaw.c:117
#define AST_MULAW(a)
Definition: ulaw.h:85
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:170
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:942
static int load_module(void)
Definition: codec_ulaw.c:144
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
#define AST_FORMAT_ULAW
Definition: frame.h:246
#define AST_FORMAT_TESTLAW
Definition: frame.h:303
static struct ast_format f[]
Definition: format_g726.c:181
static struct ast_frame * ulaw_sample(void)
Definition: ex_ulaw.h:23
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert and store samples in outbuf
Definition: codec_ulaw.c:64
Data structure associated with a single frame of data.
Definition: frame.h:142
#define BUFFER_SAMPLES
Definition: codec_ulaw.c:40
static struct ast_translator lintoulaw
The complete translator for LinToulaw.
Definition: codec_ulaw.c:107
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
union ast_frame::@172 data
#define AST_LIN2MU(a)
Definition: ulaw.h:49
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150