Asterisk - The Open Source Telephony Project  21.4.1
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 #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 /*!
101  * \brief The complete translator for LinToulaw.
102  */
103 
104 static struct ast_translator lintoulaw = {
105  .name = "lintoulaw",
106  .src_codec = {
107  .name = "slin",
108  .type = AST_MEDIA_TYPE_AUDIO,
109  .sample_rate = 8000,
110  },
111  .dst_codec = {
112  .name = "ulaw",
113  .type = AST_MEDIA_TYPE_AUDIO,
114  .sample_rate = 8000,
115  },
116  .format = "ulaw",
117  .framein = lintoulaw_framein,
118  .sample = slin8_sample,
119  .buf_size = BUFFER_SAMPLES,
120  .buffer_samples = BUFFER_SAMPLES,
121 };
122 
123 static int unload_module(void)
124 {
125  int res;
126 
127  res = ast_unregister_translator(&lintoulaw);
128  res |= ast_unregister_translator(&ulawtolin);
129 
130  return res;
131 }
132 
133 static int load_module(void)
134 {
135  int res;
136 
137  res = ast_register_translator(&ulawtolin);
138  res |= ast_register_translator(&lintoulaw);
139 
140  if (res) {
141  unload_module();
143  }
144 
146 }
147 
148 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
149  .support_level = AST_MODULE_SUPPORT_CORE,
150  .load = load_module,
151  .unload = unload_module,
152 );
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
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_translator ulawtolin
The complete translator for ulawToLin.
Definition: codec_ulaw.c:81
Utility functions.
Configuration File Parser.
u-Law to Signed linear conversion
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:258
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given translator.
Definition: translate.c:1350
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:213
union ast_frame::@224 data
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
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.
static struct ast_translator lintoulaw
The complete translator for LinToulaw.
Definition: codec_ulaw.c:104
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
char name[80]
Definition: translate.h:138