Asterisk - The Open Source Telephony Project GIT-master-57e2743
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions | Variables
codec_ilbc.c File Reference

Translate between signed linear and Internet Low Bitrate Codec. More...

#include "asterisk.h"
#include "asterisk/codec.h"
#include "asterisk/format.h"
#include "asterisk/frame.h"
#include "asterisk/linkedlists.h"
#include "asterisk/logger.h"
#include "asterisk/module.h"
#include "asterisk/translate.h"
#include "ilbc/iLBC_encode.h"
#include "ilbc/iLBC_decode.h"
#include "asterisk/ilbc.h"
#include "asterisk/slin.h"
#include "ex_ilbc.h"
Include dependency graph for codec_ilbc.c:

Go to the source code of this file.

Data Structures

struct  ilbc_coder_pvt
 

Macros

#define BUF_TYPE   uc
 
#define BUFFER_SAMPLES   8000
 
#define USE_ILBC_ENHANCER   0
 

Typedefs

typedef float ilbc_block
 
typedef unsigned char ilbc_bytes
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_module * AST_MODULE_SELF_SYM (void)
 
static int ilbctolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 decode a frame and store in outbuf
 
static int ilbctolin_new (struct ast_trans_pvt *pvt)
 
static int lintoilbc_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 store a frame into a temporary buffer, for later decoding
 
static struct ast_frame * lintoilbc_frameout (struct ast_trans_pvt *pvt)
 encode the temporary buffer and generate a frame
 
static int lintoilbc_new (struct ast_trans_pvt *pvt)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "iLBC Coder/Decoder" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_info * ast_module_info = &__mod_info
 
static struct ast_translator ilbctolin
 
static struct ast_translator lintoilbc
 

Detailed Description

Translate between signed linear and Internet Low Bitrate Codec.

Definition in file codec_ilbc.c.

Macro Definition Documentation

◆ BUF_TYPE

#define BUF_TYPE   uc

Definition at line 75 of file codec_ilbc.c.

◆ BUFFER_SAMPLES

#define BUFFER_SAMPLES   8000

Definition at line 81 of file codec_ilbc.c.

◆ USE_ILBC_ENHANCER

#define USE_ILBC_ENHANCER   0

Definition at line 80 of file codec_ilbc.c.

Typedef Documentation

◆ ilbc_block

typedef float ilbc_block

Definition at line 74 of file codec_ilbc.c.

◆ ilbc_bytes

typedef unsigned char ilbc_bytes

Definition at line 73 of file codec_ilbc.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 298 of file codec_ilbc.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 298 of file codec_ilbc.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 298 of file codec_ilbc.c.

◆ ilbctolin_framein()

static int ilbctolin_framein ( struct ast_trans_pvt *  pvt,
struct ast_frame *  f 
)
static

decode a frame and store in outbuf

Definition at line 116 of file codec_ilbc.c.

117{
118 struct ilbc_coder_pvt *tmp = pvt->pvt;
120 const unsigned int mode = attr ? attr->mode : 30;
121 const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
122 const unsigned int samples_per_frame = mode * sample_rate / 1000;
123 const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
124
125 int plc_mode = 1; /* 1 = normal data, 0 = plc */
126 /* Assuming there's space left, decode into the current buffer at
127 the tail location. Read in as many frames as there are */
128 int x,i;
129 int datalen = f->datalen;
130 int16_t *dst = pvt->outbuf.i16;
131 ilbc_block tmpf[samples_per_frame];
132
133 if (!f->data.ptr && datalen) {
134 ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set");
135 f->datalen = 0;
136 datalen = 0;
137 }
138
139 if (datalen == 0) { /* native PLC, set fake datalen and clear plc_mode */
140 datalen = octets_per_frame;
141 f->samples = samples_per_frame;
142 plc_mode = 0; /* do native plc */
143 pvt->samples += samples_per_frame;
144 }
145
146 if (datalen % octets_per_frame) {
147 ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of %u bytes long from %s (%d)?\n", octets_per_frame, f->src, datalen);
148 return -1;
149 }
150
151 if (!tmp->inited) {
153 tmp->inited = 1;
154 }
155
156 for (x = 0; x < datalen; x += octets_per_frame) {
157 if (pvt->samples + samples_per_frame > BUFFER_SAMPLES) {
158 ast_log(LOG_WARNING, "Out of buffer space\n");
159 return -1;
160 }
161 iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
162 for (i = 0; i < samples_per_frame; i++)
163 dst[pvt->samples + i] = tmpf[i];
164 pvt->samples += samples_per_frame;
165 pvt->datalen += samples_per_frame * 2;
166 }
167 return 0;
168}
#define ast_log
Definition astobj2.c:42
#define USE_ILBC_ENHANCER
Definition codec_ilbc.c:80
#define BUFFER_SAMPLES
Definition codec_ilbc.c:81
float ilbc_block
Definition codec_ilbc.c:74
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition format.c:125
short initDecode(iLBC_Dec_Inst_t *iLBCdec_inst, int mode, int use_enhancer)
Definition iLBC_decode.c:33
void iLBC_decode(float *decblock, unsigned char *bytes, iLBC_Dec_Inst_t *iLBCdec_inst, int mode)
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
#define NULL
Definition resample.c:96
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition codec.h:52
struct ast_format * format
struct ast_frame_subclass subclass
union ast_frame::@237 data
union ast_trans_pvt::@306 outbuf
struct ast_translator * t
Definition translate.h:214
int datalen
actual space used in outbuf
Definition translate.h:218
int16_t * i16
Definition translate.h:223
struct ast_codec dst_codec
Definition translate.h:140
unsigned int mode
Definition ilbc.h:5
iLBC_Dec_Inst_t dec
Definition codec_ilbc.c:89
int16_t inited
Definition codec_ilbc.c:92

References ast_debug, ast_format_get_attribute_data(), ast_log, BUFFER_SAMPLES, ast_frame::data, ast_frame::datalen, ast_trans_pvt::datalen, ilbc_coder_pvt::dec, ast_translator::dst_codec, ast_frame_subclass::format, ast_trans_pvt::i16, iLBC_decode(), initDecode(), ilbc_coder_pvt::inited, LOG_WARNING, ilbc_attr::mode, NULL, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, ast_codec::sample_rate, ast_frame::samples, ast_trans_pvt::samples, ast_frame::src, ast_frame::subclass, ast_trans_pvt::t, and USE_ILBC_ENHANCER.

◆ ilbctolin_new()

static int ilbctolin_new ( struct ast_trans_pvt *  pvt)
static

Definition at line 106 of file codec_ilbc.c.

107{
108 struct ilbc_coder_pvt *tmp = pvt->pvt;
109
110 tmp->inited = 0; /* we do not know the iLBC mode, yet */
111
112 return 0;
113}

References ilbc_coder_pvt::inited, and ast_trans_pvt::pvt.

◆ lintoilbc_framein()

static int lintoilbc_framein ( struct ast_trans_pvt *  pvt,
struct ast_frame *  f 
)
static

store a frame into a temporary buffer, for later decoding

Definition at line 171 of file codec_ilbc.c.

172{
173 struct ilbc_coder_pvt *tmp = pvt->pvt;
174
175 /* Just add the frames to our stream */
176 /* XXX We should look at how old the rest of our stream is, and if it
177 is too old, then we should overwrite it entirely, otherwise we can
178 get artifacts of earlier talk that do not belong */
179 memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
180 pvt->samples += f->samples;
181 return 0;
182}
int16_t buf[BUFFER_SAMPLES]
Definition codec_ilbc.c:91

References ilbc_coder_pvt::buf, ast_frame::data, ast_frame::datalen, ast_frame::ptr, ast_trans_pvt::pvt, ast_frame::samples, and ast_trans_pvt::samples.

◆ lintoilbc_frameout()

static struct ast_frame * lintoilbc_frameout ( struct ast_trans_pvt *  pvt)
static

encode the temporary buffer and generate a frame

Definition at line 185 of file codec_ilbc.c.

186{
187 struct ilbc_coder_pvt *tmp = pvt->pvt;
188 struct ast_frame *result = NULL;
189 struct ast_frame *last = NULL;
190 int samples = 0; /* output samples */
191
193 const unsigned int mode = attr ? attr->mode : 30;
194 const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
195 const unsigned int samples_per_frame = mode * sample_rate / 1000;
196 const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
197
198 while (pvt->samples >= samples_per_frame) {
199 struct ast_frame *current;
200 ilbc_block tmpf[samples_per_frame];
201 int i;
202
203 /* Encode a frame of data */
204 for (i = 0; i < samples_per_frame; i++)
205 tmpf[i] = tmp->buf[samples + i];
206 iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc);
207
208 samples += samples_per_frame;
209 pvt->samples -= samples_per_frame;
210
211 current = ast_trans_frameout(pvt, octets_per_frame, samples_per_frame);
212 if (!current) {
213 continue;
214 } else if (last) {
216 } else {
217 result = current;
218 }
219 last = current;
220 }
221
222 /* Move the data at the end of the buffer to the front */
223 if (samples) {
224 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
225 }
226
227 return result;
228}
struct sla_ringing_trunk * last
Definition app_sla.c:338
static PGresult * result
Definition cel_pgsql.c:84
size_t current
unsigned char ilbc_bytes
Definition codec_ilbc.c:73
void iLBC_encode(unsigned char *bytes, float *block, iLBC_Enc_Inst_t *iLBCenc_inst)
Definition iLBC_encode.c:89
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Data structure associated with a single frame of data.
struct ast_frame f
Definition translate.h:215
iLBC_Enc_Inst_t enc
Definition codec_ilbc.c:88
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition translate.c:496

References ast_format_get_attribute_data(), AST_LIST_NEXT, ast_trans_frameout(), ilbc_coder_pvt::buf, current, ast_translator::dst_codec, ilbc_coder_pvt::enc, ast_trans_pvt::f, ast_frame_subclass::format, iLBC_encode(), last, ilbc_attr::mode, NULL, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, result, ast_codec::sample_rate, ast_frame::samples, ast_trans_pvt::samples, ast_frame::subclass, and ast_trans_pvt::t.

◆ lintoilbc_new()

static int lintoilbc_new ( struct ast_trans_pvt *  pvt)
static

Definition at line 95 of file codec_ilbc.c.

96{
97 struct ilbc_coder_pvt *tmp = pvt->pvt;
99 const unsigned int mode = attr ? attr->mode : 30;
100
101 initEncode(&tmp->enc, mode);
102
103 return 0;
104}
short initEncode(iLBC_Enc_Inst_t *iLBCenc_inst, int mode)
Definition iLBC_encode.c:35
struct ast_format * explicit_dst
Definition translate.h:237

References ast_format_get_attribute_data(), ilbc_coder_pvt::enc, ast_trans_pvt::explicit_dst, initEncode(), ilbc_attr::mode, NULL, and ast_trans_pvt::pvt.

◆ load_module()

static int load_module ( void  )
static

Definition at line 283 of file codec_ilbc.c.

284{
285 int res;
286
289
290 if (res) {
293 }
294
296}
static struct ast_translator ilbctolin
Definition codec_ilbc.c:230
static struct ast_translator lintoilbc
Definition codec_ilbc.c:251
static int unload_module(void)
Definition codec_ilbc.c:273
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
#define ast_register_translator(t)
See __ast_register_translator()
Definition translate.h:258

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_register_translator, ilbctolin, lintoilbc, and unload_module().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 273 of file codec_ilbc.c.

274{
275 int res;
276
279
280 return res;
281}
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given translator.
Definition translate.c:1624

References ast_unregister_translator(), ilbctolin, and lintoilbc.

Referenced by load_module().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "iLBC Coder/Decoder" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 298 of file codec_ilbc.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 298 of file codec_ilbc.c.

◆ ilbctolin

struct ast_translator ilbctolin
static

Definition at line 230 of file codec_ilbc.c.

230 {
231 .name = "ilbctolin",
232 .src_codec = {
233 .name = "ilbc",
234 .type = AST_MEDIA_TYPE_AUDIO,
235 .sample_rate = 8000,
236 },
237 .dst_codec = {
238 .name = "slin",
239 .type = AST_MEDIA_TYPE_AUDIO,
240 .sample_rate = 8000,
241 },
242 .format = "slin",
243 .newpvt = ilbctolin_new,
244 .framein = ilbctolin_framein,
245 .sample = ilbc_sample,
246 .desc_size = sizeof(struct ilbc_coder_pvt),
247 .buf_size = BUFFER_SAMPLES * 2,
248 .native_plc = 1,
249};
@ AST_MEDIA_TYPE_AUDIO
Definition codec.h:32
static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
decode a frame and store in outbuf
Definition codec_ilbc.c:116
static int ilbctolin_new(struct ast_trans_pvt *pvt)
Definition codec_ilbc.c:106
static struct ast_frame * ilbc_sample(void)
Definition ex_ilbc.h:21

Referenced by load_module(), and unload_module().

◆ lintoilbc

struct ast_translator lintoilbc
static

Definition at line 251 of file codec_ilbc.c.

251 {
252 .name = "lintoilbc",
253 .src_codec = {
254 .name = "slin",
255 .type = AST_MEDIA_TYPE_AUDIO,
256 .sample_rate = 8000,
257 },
258 .dst_codec = {
259 .name = "ilbc",
260 .type = AST_MEDIA_TYPE_AUDIO,
261 .sample_rate = 8000,
262 },
263 .format = "ilbc",
264 .newpvt = lintoilbc_new,
265 .framein = lintoilbc_framein,
266 .frameout = lintoilbc_frameout,
267 .sample = slin8_sample,
268 .desc_size = sizeof(struct ilbc_coder_pvt),
269 /* frame len (38 bytes), frame size (160 samples), ceil (+ 160 - 1) */
270 .buf_size = (BUFFER_SAMPLES * 38 + 160 - 1) / 160,
271};
static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
store a frame into a temporary buffer, for later decoding
Definition codec_ilbc.c:171
static struct ast_frame * lintoilbc_frameout(struct ast_trans_pvt *pvt)
encode the temporary buffer and generate a frame
Definition codec_ilbc.c:185
static int lintoilbc_new(struct ast_trans_pvt *pvt)
Definition codec_ilbc.c:95
static struct ast_frame * slin8_sample(void)
Definition slin.h:64

Referenced by load_module(), and unload_module().