Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


codec_a_mu.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_a_mu.c - translate between alaw and ulaw directly
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/translate.h"
36 #include "asterisk/alaw.h"
37 #include "asterisk/ulaw.h"
38 #include "asterisk/utils.h"
39 
40 #define BUFFER_SAMPLES 8000 /* size for the translation buffers */
41 
42 static unsigned char mu2a[256];
43 static unsigned char a2mu[256];
44 
45 /* Sample frame data */
46 #include "ex_ulaw.h"
47 #include "ex_alaw.h"
48 
49 /*! \brief convert frame data and store into the buffer */
50 static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
51 {
52  int x = f->samples;
53  unsigned char *src = f->data.ptr;
54  unsigned char *dst = pvt->outbuf.uc + pvt->samples;
55 
56  pvt->samples += x;
57  pvt->datalen += x;
58 
59  while (x--)
60  *dst++ = a2mu[*src++];
61 
62  return 0;
63 }
64 
65 /*! \brief convert frame data and store into the buffer */
66 static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
67 {
68  int x = f->samples;
69  unsigned char *src = f->data.ptr;
70  unsigned char *dst = pvt->outbuf.uc + pvt->samples;
71 
72  pvt->samples += x;
73  pvt->datalen += x;
74 
75  while (x--)
76  *dst++ = mu2a[*src++];
77 
78  return 0;
79 }
80 
81 static struct ast_translator alawtoulaw = {
82  .name = "alawtoulaw",
83  .srcfmt = AST_FORMAT_ALAW,
84  .dstfmt = AST_FORMAT_ULAW,
85  .framein = alawtoulaw_framein,
86  .sample = alaw_sample,
87  .buffer_samples = BUFFER_SAMPLES,
88  .buf_size = BUFFER_SAMPLES,
89 };
90 
91 static struct ast_translator ulawtoalaw = {
92  .name = "ulawtoalaw",
93  .srcfmt = AST_FORMAT_ULAW,
94  .dstfmt = AST_FORMAT_ALAW,
95  .framein = ulawtoalaw_framein,
96  .sample = ulaw_sample,
97  .buffer_samples = BUFFER_SAMPLES,
98  .buf_size = BUFFER_SAMPLES,
99 };
100 
101 /*! \brief standard module glue */
102 
103 static int unload_module(void)
104 {
105  int res;
106 
107  res = ast_unregister_translator(&ulawtoalaw);
108  res |= ast_unregister_translator(&alawtoulaw);
109 
110  return res;
111 }
112 
113 static int load_module(void)
114 {
115  int res;
116  int x;
117 
118  for (x=0;x<256;x++) {
119  mu2a[x] = AST_LIN2A(AST_MULAW(x));
120  a2mu[x] = AST_LIN2MU(AST_ALAW(x));
121  }
122  res = ast_register_translator(&alawtoulaw);
123  if (!res)
124  res = ast_register_translator(&ulawtoalaw);
125  else
126  ast_unregister_translator(&alawtoulaw);
127  if (res)
130 }
131 
132 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");
A-Law to Signed linear conversion.
int datalen
actual space used in outbuf
Definition: translate.h:140
static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert frame data and store into the buffer
Definition: codec_a_mu.c:66
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
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
unsigned char * uc
Definition: translate.h:144
const char name[80]
Definition: translate.h:72
8-bit data
Utility functions.
static int unload_module(void)
standard module glue
Definition: codec_a_mu.c:103
u-Law to Signed linear conversion
#define AST_FORMAT_ALAW
Definition: frame.h:248
#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 struct ast_translator alawtoulaw
Definition: codec_a_mu.c:81
#define BUFFER_SAMPLES
Definition: codec_a_mu.c:40
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
static struct ast_format f[]
Definition: format_g726.c:181
static struct ast_frame * ulaw_sample(void)
Definition: ex_ulaw.h:23
static int load_module(void)
Definition: codec_a_mu.c:113
static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert frame data and store into the buffer
Definition: codec_a_mu.c:50
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
static struct ast_translator ulawtoalaw
Definition: codec_a_mu.c:91
static unsigned char a2mu[256]
Definition: codec_a_mu.c:43
static unsigned char mu2a[256]
Definition: codec_a_mu.c:42
#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
#define AST_LIN2A(a)
Definition: alaw.h:50