Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
codec_resample.c File Reference

Resample slinear audio. More...

#include "asterisk.h"
#include "speex/speex_resampler.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "asterisk/slin.h"
Include dependency graph for codec_resample.c:

Go to the source code of this file.

Macros

#define OUTBUF_SAMPLES   11520
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static void resamp_destroy (struct ast_trans_pvt *pvt)
 
static int resamp_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 
static int resamp_new (struct ast_trans_pvt *pvt)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SLIN Resampling Codec" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_codec codec_list []
 
static int trans_size
 
static struct ast_translatortranslators
 

Detailed Description

Resample slinear audio.

Definition in file codec_resample.c.

Macro Definition Documentation

◆ OUTBUF_SAMPLES

#define OUTBUF_SAMPLES   11520

Definition at line 39 of file codec_resample.c.

Referenced by load_module(), and resamp_framein().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 189 of file codec_resample.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 189 of file codec_resample.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 189 of file codec_resample.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 149 of file codec_resample.c.

References ARRAY_LEN, ast_calloc, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_register_translator, ast_translator::buf_size, ast_translator::buffer_samples, ast_translator::desc_size, ast_translator::destroy, ast_translator::dst_codec, ast_translator::framein, name, ast_translator::newpvt, OUTBUF_SAMPLES, resamp_destroy(), resamp_framein(), resamp_new(), ast_codec::sample_rate, trans_size, and unload_module().

150 {
151  int res = 0;
152  int x, y, idx = 0;
153 
155  if (!(translators = ast_calloc(1, sizeof(struct ast_translator) * trans_size))) {
157  }
158 
159  for (x = 0; x < ARRAY_LEN(codec_list); x++) {
160  for (y = 0; y < ARRAY_LEN(codec_list); y++) {
161  if (x == y) {
162  continue;
163  }
164  translators[idx].newpvt = resamp_new;
165  translators[idx].destroy = resamp_destroy;
166  translators[idx].framein = resamp_framein;
167  translators[idx].desc_size = 0;
168  translators[idx].buffer_samples = OUTBUF_SAMPLES;
169  translators[idx].buf_size = (OUTBUF_SAMPLES * sizeof(int16_t));
170  memcpy(&translators[idx].src_codec, &codec_list[x], sizeof(struct ast_codec));
171  memcpy(&translators[idx].dst_codec, &codec_list[y], sizeof(struct ast_codec));
172  snprintf(translators[idx].name, sizeof(translators[idx].name), "slin %ukhz -> %ukhz",
173  translators[idx].src_codec.sample_rate, translators[idx].dst_codec.sample_rate);
174  res |= ast_register_translator(&translators[idx]);
175  idx++;
176  }
177 
178  }
179  /* in case ast_register_translator() failed, we call unload_module() and
180  ast_unregister_translator won't fail.*/
181  if (res) {
182  unload_module();
184  }
185 
187 }
static int trans_size
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
Descriptor of a translator.
Definition: translate.h:137
short int16_t
Definition: db.h:59
static void resamp_destroy(struct ast_trans_pvt *pvt)
static int resamp_new(struct ast_trans_pvt *pvt)
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:257
static int resamp_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
static struct ast_codec codec_list[]
the list of translators
Definition: codec_dahdi.c:281
#define OUTBUF_SAMPLES
static const char name[]
Definition: cdr_mysql.c:74
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static int unload_module(void)
Represents a media codec within Asterisk.
Definition: codec.h:42

◆ resamp_destroy()

static void resamp_destroy ( struct ast_trans_pvt pvt)
static

Definition at line 105 of file codec_resample.c.

References ast_trans_pvt::pvt, and speex_resampler_destroy().

Referenced by load_module().

106 {
107  SpeexResamplerState *resamp_pvt = pvt->pvt;
108 
109  speex_resampler_destroy(resamp_pvt);
110 }
void * pvt
Definition: translate.h:219
EXPORT void speex_resampler_destroy(SpeexResamplerState *st)
Definition: resample.c:849

◆ resamp_framein()

static int resamp_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

Definition at line 112 of file codec_resample.c.

References ast_frame::data, ast_frame::datalen, ast_trans_pvt::datalen, ast_trans_pvt::i16, ast_trans_pvt::outbuf, OUTBUF_SAMPLES, ast_frame::ptr, ast_trans_pvt::pvt, ast_trans_pvt::samples, and speex_resampler_process_int().

Referenced by load_module().

113 {
114  SpeexResamplerState *resamp_pvt = pvt->pvt;
115  unsigned int out_samples = OUTBUF_SAMPLES - pvt->samples;
116  unsigned int in_samples;
117 
118  if (!f->datalen) {
119  return -1;
120  }
121  in_samples = f->datalen / 2;
122 
123  speex_resampler_process_int(resamp_pvt,
124  0,
125  f->data.ptr,
126  &in_samples,
127  pvt->outbuf.i16 + pvt->samples,
128  &out_samples);
129 
130  pvt->samples += out_samples;
131  pvt->datalen += out_samples * 2;
132 
133  return 0;
134 }
int datalen
actual space used in outbuf
Definition: translate.h:218
void * pvt
Definition: translate.h:219
int16_t * i16
Definition: translate.h:223
union ast_trans_pvt::@327 outbuf
#define OUTBUF_SAMPLES
union ast_frame::@263 data
EXPORT int speex_resampler_process_int(SpeexResamplerState *st, spx_uint32_t channel_index, const spx_int16_t *in, spx_uint32_t *in_len, spx_int16_t *out, spx_uint32_t *out_len)
Definition: resample.c:906

◆ resamp_new()

static int resamp_new ( struct ast_trans_pvt pvt)
static

Definition at line 91 of file codec_resample.c.

References ao2_bump, ast_assert, ast_format_cache_get_slin_by_rate(), ast_translator::dst_codec, ast_trans_pvt::f, ast_frame_subclass::format, NULL, ast_trans_pvt::pvt, ast_codec::sample_rate, speex_resampler_init(), ast_translator::src_codec, ast_frame::subclass, and ast_trans_pvt::t.

Referenced by load_module().

92 {
93  int err;
94 
95  if (!(pvt->pvt = speex_resampler_init(1, pvt->t->src_codec.sample_rate, pvt->t->dst_codec.sample_rate, 5, &err))) {
96  return -1;
97  }
98 
99  ast_assert(pvt->f.subclass.format == NULL);
101 
102  return 0;
103 }
struct ast_frame f
Definition: translate.h:215
EXPORT SpeexResamplerState * speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err)
Create a new resampler with integer input and output rates.
Definition: resample.c:783
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
struct ast_frame_subclass subclass
#define ao2_bump(obj)
Definition: astobj2.h:491
struct ast_codec dst_codec
Definition: translate.h:140
struct ast_codec src_codec
Definition: translate.h:139
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition: codec.h:52
struct ast_translator * t
Definition: translate.h:214
struct ast_format * format
struct ast_format * ast_format_cache_get_slin_by_rate(unsigned int rate)
Retrieve the best signed linear format given a sample rate.
Definition: format_cache.c:520

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 136 of file codec_resample.c.

References ast_free, ast_unregister_translator(), and trans_size.

Referenced by load_module().

137 {
138  int res = 0;
139  int idx;
140 
141  for (idx = 0; idx < trans_size; idx++) {
143  }
145 
146  return res;
147 }
static int trans_size
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:1333
the list of translators
Definition: codec_dahdi.c:281
#define ast_free(a)
Definition: astmm.h:182

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SLIN Resampling Codec" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 189 of file codec_resample.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 189 of file codec_resample.c.

◆ codec_list

struct ast_codec codec_list[]
static

Definition at line 43 of file codec_resample.c.

Referenced by try_suggested_sip_codec().

◆ trans_size

int trans_size
static

Definition at line 42 of file codec_resample.c.

Referenced by load_module(), and unload_module().

◆ translators

struct ast_translator* translators
static

Definition at line 41 of file codec_resample.c.