Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


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 <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_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 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/alaw.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_alaw.h"
45 
46 /*! \brief decode frame into lin and fill output buffer. */
47 static int alawtolin_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  while (i--)
57  *dst++ = AST_ALAW(*src++);
58 
59  return 0;
60 }
61 
62 /*! \brief convert and store input samples in output buffer */
63 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
64 {
65  int i = f->samples;
66  char *dst = pvt->outbuf.c + pvt->samples;
67  int16_t *src = f->data.ptr;
68 
69  pvt->samples += i;
70  pvt->datalen += i; /* 1 byte/sample */
71 
72  while (i--)
73  *dst++ = AST_LIN2A(*src++);
74 
75  return 0;
76 }
77 
78 static struct ast_translator alawtolin = {
79  .name = "alawtolin",
80  .srcfmt = AST_FORMAT_ALAW,
81  .dstfmt = AST_FORMAT_SLINEAR,
82  .framein = alawtolin_framein,
83  .sample = alaw_sample,
84  .buffer_samples = BUFFER_SAMPLES,
85  .buf_size = BUFFER_SAMPLES * 2,
86 };
87 
88 static struct ast_translator lintoalaw = {
89  "lintoalaw",
91  .dstfmt = AST_FORMAT_ALAW,
92  .framein = lintoalaw_framein,
93  .sample = slin8_sample,
94  .buffer_samples = BUFFER_SAMPLES,
95  .buf_size = BUFFER_SAMPLES,
96 };
97 
98 /*! \brief standard module stuff */
99 
100 static int reload(void)
101 {
103 }
104 
105 static int unload_module(void)
106 {
107  int res;
108 
109  res = ast_unregister_translator(&lintoalaw);
110  res |= ast_unregister_translator(&alawtolin);
111 
112  return res;
113 }
114 
115 static int load_module(void)
116 {
117  int res;
118 
119  res = ast_register_translator(&alawtolin);
120  if (!res)
121  res = ast_register_translator(&lintoalaw);
122  else
123  ast_unregister_translator(&alawtolin);
124  if (res)
127 }
128 
130  .load = load_module,
131  .unload = unload_module,
132  .reload = reload,
133  );
A-Law to Signed linear conversion.
int datalen
actual space used in outbuf
Definition: translate.h:140
8-bit data
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
#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:47
static int load_module(void)
Definition: codec_alaw.c:115
#define BUFFER_SAMPLES
Definition: codec_alaw.c:40
Configuration File Parser.
static int reload(void)
standard module stuff
Definition: codec_alaw.c:100
const char name[80]
Definition: translate.h:72
#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
int16_t * i16
Definition: translate.h:145
Utility functions.
#define AST_FORMAT_ALAW
Definition: frame.h:248
#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 struct ast_translator alawtolin
Definition: codec_alaw.c:78
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
static struct ast_format f[]
Definition: format_g726.c:181
static int unload_module(void)
Definition: codec_alaw.c:105
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
static struct ast_translator lintoalaw
Definition: codec_alaw.c:88
Data structure associated with a single frame of data.
Definition: frame.h:142
static struct ast_frame * alaw_sample(void)
Definition: ex_alaw.h:23
format_t srcfmt
Definition: translate.h:73
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
union ast_frame::@172 data
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:63
#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
#define AST_LIN2A(a)
Definition: alaw.h:50