Asterisk - The Open Source Telephony Project  18.5.0
codec_alaw.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_alaw.c - translate between signed linear and alaw
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/alaw.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_alaw.h"
43 
44 /*! \brief decode frame into lin and fill output buffer. */
45 static int alawtolin_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  while (i--)
55  *dst++ = AST_ALAW(*src++);
56 
57  return 0;
58 }
59 
60 /*! \brief convert and store input samples in output buffer */
61 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
62 {
63  int i = f->samples;
64  char *dst = pvt->outbuf.c + pvt->samples;
65  int16_t *src = f->data.ptr;
66 
67  pvt->samples += i;
68  pvt->datalen += i; /* 1 byte/sample */
69 
70  while (i--)
71  *dst++ = AST_LIN2A(*src++);
72 
73  return 0;
74 }
75 
76 static struct ast_translator alawtolin = {
77  .name = "alawtolin",
78  .src_codec = {
79  .name = "alaw",
80  .type = AST_MEDIA_TYPE_AUDIO,
81  .sample_rate = 8000,
82  },
83  .dst_codec = {
84  .name = "slin",
85  .type = AST_MEDIA_TYPE_AUDIO,
86  .sample_rate = 8000,
87  },
88  .format = "slin",
89  .framein = alawtolin_framein,
90  .sample = alaw_sample,
91  .buffer_samples = BUFFER_SAMPLES,
92  .buf_size = BUFFER_SAMPLES * 2,
93 };
94 
95 static struct ast_translator lintoalaw = {
96  .name = "lintoalaw",
97  .src_codec = {
98  .name = "slin",
99  .type = AST_MEDIA_TYPE_AUDIO,
100  .sample_rate = 8000,
101  },
102  .dst_codec = {
103  .name = "alaw",
104  .type = AST_MEDIA_TYPE_AUDIO,
105  .sample_rate = 8000,
106  },
107  .format = "alaw",
108  .framein = lintoalaw_framein,
109  .sample = slin8_sample,
110  .buffer_samples = BUFFER_SAMPLES,
111  .buf_size = BUFFER_SAMPLES,
112 };
113 
114 static int unload_module(void)
115 {
116  int res;
117 
118  res = ast_unregister_translator(&lintoalaw);
119  res |= ast_unregister_translator(&alawtolin);
120 
121  return res;
122 }
123 
124 static int load_module(void)
125 {
126  int res;
127 
128  res = ast_register_translator(&alawtolin);
129  res |= ast_register_translator(&lintoalaw);
130 
131  if (res) {
132  unload_module();
134  }
135 
137 }
138 
140  .support_level = AST_MODULE_SUPPORT_CORE,
141  .load = load_module,
142  .unload = unload_module,
143 );
A-Law to Signed linear conversion.
int datalen
actual space used in outbuf
Definition: translate.h:218
8-bit data
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.
#define AST_ALAW(a)
Definition: alaw.h:84
static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
decode frame into lin and fill output buffer.
Definition: codec_alaw.c:45
static int load_module(void)
Definition: codec_alaw.c:124
#define BUFFER_SAMPLES
Definition: codec_alaw.c:38
static struct ast_frame * slin8_sample(void)
Definition: slin.h:64
int16_t * i16
Definition: translate.h:223
Utility functions.
Configuration File Parser.
#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 struct ast_translator alawtolin
Definition: codec_alaw.c:76
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 int unload_module(void)
Definition: codec_alaw.c:114
static struct ast_translator lintoalaw
Definition: codec_alaw.c:95
Data structure associated with a single frame of data.
static struct ast_frame * alaw_sample(void)
Definition: ex_alaw.h:26
union ast_frame::@263 data
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert and store input samples in output buffer
Definition: codec_alaw.c:61
char name[80]
Definition: translate.h:138
#define AST_LIN2A(a)
Definition: alaw.h:50