Asterisk - The Open Source Telephony Project  18.5.0
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_moduleAST_MODULE_SELF_SYM (void)
 
static int ilbctolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 decode a frame and store in outbuf More...
 
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 More...
 
static struct ast_framelintoilbc_frameout (struct ast_trans_pvt *pvt)
 encode the temporary buffer and generate a frame More...
 
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 = "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_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 53 of file codec_ilbc.c.

◆ BUFFER_SAMPLES

#define BUFFER_SAMPLES   8000

Definition at line 59 of file codec_ilbc.c.

Referenced by ilbctolin_framein().

◆ USE_ILBC_ENHANCER

#define USE_ILBC_ENHANCER   0

Definition at line 58 of file codec_ilbc.c.

Referenced by ilbctolin_framein().

Typedef Documentation

◆ ilbc_block

typedef float ilbc_block

Definition at line 52 of file codec_ilbc.c.

◆ ilbc_bytes

typedef unsigned char ilbc_bytes

Definition at line 51 of file codec_ilbc.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 276 of file codec_ilbc.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 276 of file codec_ilbc.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 276 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 94 of file codec_ilbc.c.

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, tmp(), and USE_ILBC_ENHANCER.

95 {
96  struct ilbc_coder_pvt *tmp = pvt->pvt;
98  const unsigned int mode = attr ? attr->mode : 30;
99  const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
100  const unsigned int samples_per_frame = mode * sample_rate / 1000;
101  const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
102 
103  int plc_mode = 1; /* 1 = normal data, 0 = plc */
104  /* Assuming there's space left, decode into the current buffer at
105  the tail location. Read in as many frames as there are */
106  int x,i;
107  int datalen = f->datalen;
108  int16_t *dst = pvt->outbuf.i16;
109  ilbc_block tmpf[samples_per_frame];
110 
111  if (!f->data.ptr && datalen) {
112  ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set");
113  f->datalen = 0;
114  datalen = 0;
115  }
116 
117  if (datalen == 0) { /* native PLC, set fake datalen and clear plc_mode */
118  datalen = octets_per_frame;
119  f->samples = samples_per_frame;
120  plc_mode = 0; /* do native plc */
121  pvt->samples += samples_per_frame;
122  }
123 
124  if (datalen % octets_per_frame) {
125  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);
126  return -1;
127  }
128 
129  if (!tmp->inited) {
130  initDecode(&tmp->dec, mode, USE_ILBC_ENHANCER);
131  tmp->inited = 1;
132  }
133 
134  for (x = 0; x < datalen; x += octets_per_frame) {
135  if (pvt->samples + samples_per_frame > BUFFER_SAMPLES) {
136  ast_log(LOG_WARNING, "Out of buffer space\n");
137  return -1;
138  }
139  iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
140  for (i = 0; i < samples_per_frame; i++)
141  dst[pvt->samples + i] = tmpf[i];
142  pvt->samples += samples_per_frame;
143  pvt->datalen += samples_per_frame * 2;
144  }
145  return 0;
146 }
int datalen
actual space used in outbuf
Definition: translate.h:218
Definition: ilbc.h:4
short int16_t
Definition: db.h:59
float ilbc_block
Definition: codec_ilbc.c:52
void iLBC_decode(float *decblock, unsigned char *bytes, iLBC_Dec_Inst_t *iLBCdec_inst, int mode)
Definition: iLBC_decode.c:326
#define LOG_WARNING
Definition: logger.h:274
int16_t inited
Definition: codec_ilbc.c:70
static int tmp()
Definition: bt_open.c:389
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
struct ast_frame_subclass subclass
int16_t * i16
Definition: translate.h:223
short initDecode(iLBC_Dec_Inst_t *iLBCdec_inst, int mode, int use_enhancer)
Definition: iLBC_decode.c:33
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
const char * src
union ast_trans_pvt::@327 outbuf
struct ast_codec dst_codec
Definition: translate.h:140
iLBC_Dec_Inst_t dec
Definition: codec_ilbc.c:67
unsigned int mode
Definition: ilbc.h:5
#define BUFFER_SAMPLES
Definition: codec_ilbc.c:59
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition: codec.h:52
#define USE_ILBC_ENHANCER
Definition: codec_ilbc.c:58
struct ast_translator * t
Definition: translate.h:214
union ast_frame::@263 data
struct ast_format * format

◆ ilbctolin_new()

static int ilbctolin_new ( struct ast_trans_pvt pvt)
static

Definition at line 84 of file codec_ilbc.c.

References ilbc_coder_pvt::inited, ast_trans_pvt::pvt, and tmp().

85 {
86  struct ilbc_coder_pvt *tmp = pvt->pvt;
87 
88  tmp->inited = 0; /* we do not know the iLBC mode, yet */
89 
90  return 0;
91 }
int16_t inited
Definition: codec_ilbc.c:70
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219

◆ 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 149 of file codec_ilbc.c.

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

150 {
151  struct ilbc_coder_pvt *tmp = pvt->pvt;
152 
153  /* Just add the frames to our stream */
154  /* XXX We should look at how old the rest of our stream is, and if it
155  is too old, then we should overwrite it entirely, otherwise we can
156  get artifacts of earlier talk that do not belong */
157  memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
158  pvt->samples += f->samples;
159  return 0;
160 }
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219
int16_t buf[BUFFER_SAMPLES]
Definition: codec_ilbc.c:69
union ast_frame::@263 data

◆ 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 163 of file codec_ilbc.c.

References ast_format_get_attribute_data(), AST_LIST_NEXT, ast_trans_frameout(), ilbc_coder_pvt::buf, 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, ast_trans_pvt::t, and tmp().

164 {
165  struct ilbc_coder_pvt *tmp = pvt->pvt;
166  struct ast_frame *result = NULL;
167  struct ast_frame *last = NULL;
168  int samples = 0; /* output samples */
169 
171  const unsigned int mode = attr ? attr->mode : 30;
172  const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
173  const unsigned int samples_per_frame = mode * sample_rate / 1000;
174  const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
175 
176  while (pvt->samples >= samples_per_frame) {
177  struct ast_frame *current;
178  ilbc_block tmpf[samples_per_frame];
179  int i;
180 
181  /* Encode a frame of data */
182  for (i = 0; i < samples_per_frame; i++)
183  tmpf[i] = tmp->buf[samples + i];
184  iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc);
185 
186  samples += samples_per_frame;
187  pvt->samples -= samples_per_frame;
188 
189  current = ast_trans_frameout(pvt, octets_per_frame, samples_per_frame);
190  if (!current) {
191  continue;
192  } else if (last) {
193  AST_LIST_NEXT(last, frame_list) = current;
194  } else {
195  result = current;
196  }
197  last = current;
198  }
199 
200  /* Move the data at the end of the buffer to the front */
201  if (samples) {
202  memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
203  }
204 
205  return result;
206 }
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition: translate.c:438
iLBC_Enc_Inst_t enc
Definition: codec_ilbc.c:66
Definition: ilbc.h:4
float ilbc_block
Definition: codec_ilbc.c:52
struct ast_frame f
Definition: translate.h:215
static int tmp()
Definition: bt_open.c:389
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:438
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
void iLBC_encode(unsigned char *bytes, float *block, iLBC_Enc_Inst_t *iLBCenc_inst)
Definition: iLBC_encode.c:89
struct ast_frame_subclass subclass
struct sla_ringing_trunk * last
Definition: app_meetme.c:1092
unsigned char ilbc_bytes
Definition: codec_ilbc.c:51
int16_t buf[BUFFER_SAMPLES]
Definition: codec_ilbc.c:69
union ast_trans_pvt::@327 outbuf
struct ast_codec dst_codec
Definition: translate.h:140
unsigned int mode
Definition: ilbc.h:5
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition: codec.h:52
static PGresult * result
Definition: cel_pgsql.c:88
Data structure associated with a single frame of data.
struct ast_translator * t
Definition: translate.h:214
struct ast_format * format

◆ lintoilbc_new()

static int lintoilbc_new ( struct ast_trans_pvt pvt)
static

Definition at line 73 of file codec_ilbc.c.

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

74 {
75  struct ilbc_coder_pvt *tmp = pvt->pvt;
77  const unsigned int mode = attr ? attr->mode : 30;
78 
79  initEncode(&tmp->enc, mode);
80 
81  return 0;
82 }
iLBC_Enc_Inst_t enc
Definition: codec_ilbc.c:66
Definition: ilbc.h:4
static int tmp()
Definition: bt_open.c:389
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition: format.c:125
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
struct ast_format * explicit_dst
Definition: translate.h:237
short initEncode(iLBC_Enc_Inst_t *iLBCenc_inst, int mode)
Definition: iLBC_encode.c:35
unsigned int mode
Definition: ilbc.h:5

◆ load_module()

static int load_module ( void  )
static

Definition at line 261 of file codec_ilbc.c.

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

262 {
263  int res;
264 
267 
268  if (res) {
269  unload_module();
271  }
272 
274 }
static struct ast_translator ilbctolin
Definition: codec_ilbc.c:208
static struct ast_translator lintoilbc
Definition: codec_ilbc.c:229
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:257
static int unload_module(void)
Definition: codec_ilbc.c:251
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 251 of file codec_ilbc.c.

References ast_unregister_translator().

Referenced by load_module().

252 {
253  int res;
254 
257 
258  return res;
259 }
static struct ast_translator ilbctolin
Definition: codec_ilbc.c:208
static struct ast_translator lintoilbc
Definition: codec_ilbc.c:229
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:1333

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "iLBC Coder/Decoder" , .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 276 of file codec_ilbc.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 276 of file codec_ilbc.c.

◆ ilbctolin

struct ast_translator ilbctolin
static

Definition at line 208 of file codec_ilbc.c.

◆ lintoilbc

struct ast_translator lintoilbc
static

Definition at line 229 of file codec_ilbc.c.