Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


codec_resample.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Digium, Inc.
5  *
6  * Russell Bryant <russell@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 /*!
20  * \file
21  *
22  * \brief Resample slinear audio
23  *
24  * \note To install libresample, check it out of the following repository:
25  * <code>$ svn co http://svn.digium.com/svn/thirdparty/libresample/trunk</code>
26  *
27  * \ingroup codecs
28  */
29 
30 /*** MODULEINFO
31  <depend>resample</depend>
32  <support_level>core</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
38 
39 /* These are for SHRT_MAX and FLT_MAX -- { */
40 #if defined(__Darwin__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
41 #include <float.h>
42 #else
43 #include <values.h>
44 #endif
45 #include <limits.h>
46 /* } */
47 
48 #include <libresample.h>
49 
50 #include "asterisk/module.h"
51 #include "asterisk/translate.h"
52 
53 #include "asterisk/slin.h"
54 
55 #define RESAMPLER_QUALITY 1
56 
57 #define OUTBUF_SIZE 8096
58 
60  void *resampler;
62 };
63 
65  void *resampler;
67 };
68 
69 static int slin16_to_slin8_new(struct ast_trans_pvt *pvt)
70 {
71  struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
72 
73  resamp_pvt->resample_factor = 8000.0 / 16000.0;
74 
75  if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, resamp_pvt->resample_factor, resamp_pvt->resample_factor)))
76  return -1;
77 
78  return 0;
79 }
80 
81 static int slin8_to_slin16_new(struct ast_trans_pvt *pvt)
82 {
83  struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
84 
85  resamp_pvt->resample_factor = 16000.0 / 8000.0;
86 
87  if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, resamp_pvt->resample_factor, resamp_pvt->resample_factor)))
88  return -1;
89 
90  return 0;
91 }
92 
93 static void slin16_to_slin8_destroy(struct ast_trans_pvt *pvt)
94 {
95  struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
96 
97  if (resamp_pvt->resampler)
98  resample_close(resamp_pvt->resampler);
99 }
100 
101 static void slin8_to_slin16_destroy(struct ast_trans_pvt *pvt)
102 {
103  struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
104 
105  if (resamp_pvt->resampler)
106  resample_close(resamp_pvt->resampler);
107 }
108 
109 static int resample_frame(struct ast_trans_pvt *pvt,
110  void *resampler, float resample_factor, struct ast_frame *f)
111 {
112  int total_in_buf_used = 0;
113  int total_out_buf_used = 0;
114  int16_t *in_buf = (int16_t *) f->data.ptr;
115  int16_t *out_buf = pvt->outbuf.i16 + pvt->samples;
116  float in_buf_f[f->samples];
117  float out_buf_f[2048];
118  int res = 0;
119  int i;
120 
121  for (i = 0; i < f->samples; i++)
122  in_buf_f[i] = in_buf[i] * (FLT_MAX / SHRT_MAX);
123 
124  while (total_in_buf_used < f->samples) {
125  int in_buf_used, out_buf_used;
126 
127  out_buf_used = resample_process(resampler, resample_factor,
128  &in_buf_f[total_in_buf_used], f->samples - total_in_buf_used,
129  0, &in_buf_used,
130  &out_buf_f[total_out_buf_used], ARRAY_LEN(out_buf_f) - total_out_buf_used);
131 
132  if (out_buf_used < 0)
133  break;
134 
135  total_out_buf_used += out_buf_used;
136  total_in_buf_used += in_buf_used;
137 
138  if (total_out_buf_used == ARRAY_LEN(out_buf_f)) {
139  ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n");
140  res = -1;
141  break;
142  }
143  }
144 
145  for (i = 0; i < total_out_buf_used; i++)
146  out_buf[i] = out_buf_f[i] * (SHRT_MAX / FLT_MAX);
147 
148  pvt->samples += total_out_buf_used;
149  pvt->datalen += (total_out_buf_used * sizeof(int16_t));
150 
151  return res;
152 }
153 
154 static int slin16_to_slin8_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
155 {
156  struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
157  void *resampler = resamp_pvt->resampler;
158  float resample_factor = resamp_pvt->resample_factor;
159 
160  return resample_frame(pvt, resampler, resample_factor, f);
161 }
162 
163 static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
164 {
165  struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
166  void *resampler = resamp_pvt->resampler;
167  float resample_factor = resamp_pvt->resample_factor;
168 
169  return resample_frame(pvt, resampler, resample_factor, f);
170 }
171 
173  .name = "slin16_to_slin8",
174  .srcfmt = AST_FORMAT_SLINEAR16,
175  .dstfmt = AST_FORMAT_SLINEAR,
176  .newpvt = slin16_to_slin8_new,
177  .destroy = slin16_to_slin8_destroy,
178  .framein = slin16_to_slin8_framein,
179  .sample = slin16_sample,
180  .desc_size = sizeof(struct slin16_to_slin8_pvt),
181  .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
182  .buf_size = OUTBUF_SIZE,
183 };
184 
186  .name = "slin8_to_slin16",
187  .srcfmt = AST_FORMAT_SLINEAR,
188  .dstfmt = AST_FORMAT_SLINEAR16,
189  .newpvt = slin8_to_slin16_new,
190  .destroy = slin8_to_slin16_destroy,
191  .framein = slin8_to_slin16_framein,
192  .sample = slin8_sample,
193  .desc_size = sizeof(struct slin8_to_slin16_pvt),
194  .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
195  .buf_size = OUTBUF_SIZE,
196 };
197 
198 static int unload_module(void)
199 {
200  int res = 0;
201 
202  res |= ast_unregister_translator(&slin16_to_slin8);
203  res |= ast_unregister_translator(&slin8_to_slin16);
204 
205  return res;
206 }
207 
208 static int load_module(void)
209 {
210  int res = 0;
211 
212  res |= ast_register_translator(&slin16_to_slin8);
213  res |= ast_register_translator(&slin8_to_slin16);
214 
216 }
217 
218 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");
int datalen
actual space used in outbuf
Definition: translate.h:140
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
static int slin8_to_slin16_new(struct ast_trans_pvt *pvt)
Asterisk main include file. File version handling, generic pbx functions.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
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
const char name[80]
Definition: translate.h:72
for(;;)
Definition: ast_expr2.c:2460
static void slin8_to_slin16_destroy(struct ast_trans_pvt *pvt)
static struct ast_frame * slin8_sample(void)
Definition: slin.h:61
void * pvt
Definition: translate.h:141
static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
static int load_module(void)
int16_t * i16
Definition: translate.h:145
static struct ast_frame * slin16_sample(void)
Definition: slin.h:77
#define RESAMPLER_QUALITY
static int slin16_to_slin8_new(struct ast_trans_pvt *pvt)
#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
#define AST_FORMAT_SLINEAR16
Definition: frame.h:272
static struct ast_translator slin8_to_slin16
#define LOG_ERROR
Definition: logger.h:155
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:135
static int slin16_to_slin8_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
#define OUTBUF_SIZE
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static struct ast_translator slin16_to_slin8
static int resample_frame(struct ast_trans_pvt *pvt, void *resampler, float resample_factor, struct ast_frame *f)
static struct ast_format f[]
Definition: format_g726.c:181
static void slin16_to_slin8_destroy(struct ast_trans_pvt *pvt)
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
static int unload_module(void)
Data structure associated with a single frame of data.
Definition: frame.h:142
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
#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