Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


codec_g722.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2008, Digium, Inc.
5  *
6  * Matthew Fredrickson <creslin@digium.com>
7  * Russell Bryant <russell@digium.com>
8  *
9  * Special thanks to Steve Underwood for the implementation
10  * and for doing the 8khz<->g.722 direct translation code.
11  *
12  * See http://www.asterisk.org for more information about
13  * the Asterisk project. Please do not directly contact
14  * any of the maintainers of this project for assistance;
15  * the project provides a web site, mailing lists and IRC
16  * channels for your use.
17  *
18  * This program is free software, distributed under the terms of
19  * the GNU General Public License Version 2. See the LICENSE file
20  * at the top of the source tree.
21  */
22 
23 /*! \file
24  *
25  * \brief codec_g722.c - translate between signed linear and ITU G.722-64kbps
26  *
27  * \author Matthew Fredrickson <creslin@digium.com>
28  * \author Russell Bryant <russell@digium.com>
29  *
30  * \arg http://soft-switch.org/downloads/non-gpl-bits.tgz
31  * \arg http://lists.digium.com/pipermail/asterisk-dev/2006-September/022866.html
32  *
33  * \ingroup codecs
34  */
35 
36 /*** MODULEINFO
37  <support_level>core</support_level>
38  ***/
39 
40 #include "asterisk.h"
41 
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
43 
44 #include "asterisk/linkedlists.h"
45 #include "asterisk/module.h"
46 #include "asterisk/config.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/utils.h"
49 
50 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
51 #define BUF_SHIFT 5
52 
53 #include "g722/g722.h"
54 
55 /* Sample frame data */
56 #include "asterisk/slin.h"
57 #include "ex_g722.h"
58 
60  g722_encode_state_t g722;
61 };
62 
64  g722_decode_state_t g722;
65 };
66 
67 /*! \brief init a new instance of g722_encoder_pvt. */
68 static int lintog722_new(struct ast_trans_pvt *pvt)
69 {
70  struct g722_encoder_pvt *tmp = pvt->pvt;
71 
72  g722_encode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
73 
74  return 0;
75 }
76 
77 static int lin16tog722_new(struct ast_trans_pvt *pvt)
78 {
79  struct g722_encoder_pvt *tmp = pvt->pvt;
80 
81  g722_encode_init(&tmp->g722, 64000, 0);
82 
83  return 0;
84 }
85 
86 /*! \brief init a new instance of g722_encoder_pvt. */
87 static int g722tolin_new(struct ast_trans_pvt *pvt)
88 {
89  struct g722_decoder_pvt *tmp = pvt->pvt;
90 
91  g722_decode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
92 
93  return 0;
94 }
95 
96 static int g722tolin16_new(struct ast_trans_pvt *pvt)
97 {
98  struct g722_decoder_pvt *tmp = pvt->pvt;
99 
100  g722_decode_init(&tmp->g722, 64000, 0);
101 
102  return 0;
103 }
104 
105 static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
106 {
107  struct g722_decoder_pvt *tmp = pvt->pvt;
108  int out_samples;
109  int in_samples;
110 
111  /* g722_decode expects the samples to be in the invalid samples / 2 format */
112  in_samples = f->samples / 2;
113 
114  out_samples = g722_decode(&tmp->g722, &pvt->outbuf.i16[pvt->samples * sizeof(int16_t)],
115  (uint8_t *) f->data.ptr, in_samples);
116 
117  pvt->samples += out_samples;
118 
119  pvt->datalen += (out_samples * sizeof(int16_t));
120 
121  return 0;
122 }
123 
124 static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
125 {
126  struct g722_encoder_pvt *tmp = pvt->pvt;
127  int outlen;
128 
129  outlen = g722_encode(&tmp->g722, (&pvt->outbuf.ui8[pvt->datalen]),
130  (int16_t *) f->data.ptr, f->samples);
131 
132  pvt->samples += outlen * 2;
133 
134  pvt->datalen += outlen;
135 
136  return 0;
137 }
138 
139 static struct ast_translator g722tolin = {
140  .name = "g722tolin",
141  .srcfmt = AST_FORMAT_G722,
142  .dstfmt = AST_FORMAT_SLINEAR,
143  .newpvt = g722tolin_new, /* same for both directions */
144  .framein = g722tolin_framein,
145  .sample = g722_sample,
146  .desc_size = sizeof(struct g722_decoder_pvt),
147  .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
148  .buf_size = BUFFER_SAMPLES,
149 };
150 
151 static struct ast_translator lintog722 = {
152  .name = "lintog722",
153  .srcfmt = AST_FORMAT_SLINEAR,
154  .dstfmt = AST_FORMAT_G722,
155  .newpvt = lintog722_new, /* same for both directions */
156  .framein = lintog722_framein,
157  .sample = slin8_sample,
158  .desc_size = sizeof(struct g722_encoder_pvt),
159  .buffer_samples = BUFFER_SAMPLES * 2,
160  .buf_size = BUFFER_SAMPLES,
161 };
162 
163 static struct ast_translator g722tolin16 = {
164  .name = "g722tolin16",
165  .srcfmt = AST_FORMAT_G722,
166  .dstfmt = AST_FORMAT_SLINEAR16,
167  .newpvt = g722tolin16_new, /* same for both directions */
168  .framein = g722tolin_framein,
169  .sample = g722_sample,
170  .desc_size = sizeof(struct g722_decoder_pvt),
171  .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
172  .buf_size = BUFFER_SAMPLES,
173 };
174 
175 static struct ast_translator lin16tog722 = {
176  .name = "lin16tog722",
177  .srcfmt = AST_FORMAT_SLINEAR16,
178  .dstfmt = AST_FORMAT_G722,
179  .newpvt = lin16tog722_new, /* same for both directions */
180  .framein = lintog722_framein,
181  .sample = slin16_sample,
182  .desc_size = sizeof(struct g722_encoder_pvt),
183  .buffer_samples = BUFFER_SAMPLES * 2,
184  .buf_size = BUFFER_SAMPLES,
185 };
186 
187 static int reload(void)
188 {
190 }
191 
192 static int unload_module(void)
193 {
194  int res = 0;
195 
196  res |= ast_unregister_translator(&g722tolin);
197  res |= ast_unregister_translator(&lintog722);
198  res |= ast_unregister_translator(&g722tolin16);
199  res |= ast_unregister_translator(&lin16tog722);
200 
201  return res;
202 }
203 
204 static int load_module(void)
205 {
206  int res = 0;
207 
208  res |= ast_register_translator(&g722tolin);
209  res |= ast_register_translator(&lintog722);
210  res |= ast_register_translator(&g722tolin16);
211  res |= ast_register_translator(&lin16tog722);
212 
213  if (res) {
214  unload_module();
216  }
217 
219 }
220 
221 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
222  .load = load_module,
223  .unload = unload_module,
224  .reload = reload,
225  );
static int unload_module(void)
Definition: codec_g722.c:192
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 load_module(void)
Definition: codec_g722.c:204
Configuration File Parser.
const char name[80]
Definition: translate.h:72
g722_decode_state_t g722
Definition: codec_g722.c:64
#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
void * pvt
Definition: translate.h:141
static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
Definition: codec_g722.c:105
static struct ast_translator lintog722
Definition: codec_g722.c:151
int16_t * i16
Definition: translate.h:145
static struct ast_frame * slin16_sample(void)
Definition: slin.h:77
Utility functions.
static int reload(void)
Definition: codec_g722.c:187
static struct ast_translator g722tolin
Definition: codec_g722.c:139
static int lintog722_new(struct ast_trans_pvt *pvt)
init a new instance of g722_encoder_pvt.
Definition: codec_g722.c:68
#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_frame * g722_sample(void)
Definition: ex_g722.h:33
uint8_t * ui8
Definition: translate.h:146
#define AST_FORMAT_SLINEAR16
Definition: frame.h:272
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
static struct ast_translator g722tolin16
Definition: codec_g722.c:163
#define BUFFER_SAMPLES
Definition: codec_g722.c:50
static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
Definition: codec_g722.c:124
static struct ast_format f[]
Definition: format_g726.c:181
8-bit data
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
Data structure associated with a single frame of data.
Definition: frame.h:142
#define AST_FORMAT_G722
Definition: frame.h:266
g722_encode_state_t g722
Definition: codec_g722.c:60
static int lin16tog722_new(struct ast_trans_pvt *pvt)
Definition: codec_g722.c:77
static int g722tolin_new(struct ast_trans_pvt *pvt)
init a new instance of g722_encoder_pvt.
Definition: codec_g722.c:87
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
static int g722tolin16_new(struct ast_trans_pvt *pvt)
Definition: codec_g722.c:96
#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
static struct ast_translator lin16tog722
Definition: codec_g722.c:175