Wed Jan 8 2020 09:50:10

Asterisk developer's documentation


codec_adpcm.c File Reference

codec_adpcm.c - translate between signed linear and Dialogic ADPCM More...

#include "asterisk.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/translate.h"
#include "asterisk/utils.h"
#include "asterisk/slin.h"
#include "ex_adpcm.h"

Go to the source code of this file.

Data Structures

struct  adpcm_decoder_pvt
 Workspace for translating ADPCM signals to signed linear. More...
 
struct  adpcm_encoder_pvt
 Workspace for translating signed linear signals to ADPCM. More...
 
struct  adpcm_state
 

Macros

#define BUFFER_SAMPLES   8096 /* size for the translation buffers */
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int adpcm (short csig, struct adpcm_state *state)
 
static int adpcmtolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 decode 4-bit adpcm frame data and store in output buffer More...
 
static short decode (int encoded, struct adpcm_state *state)
 
static int lintoadpcm_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 fill input buffer with 16-bit signed linear PCM values. More...
 
static struct ast_framelintoadpcm_frameout (struct ast_trans_pvt *pvt)
 convert inbuf and store into frame More...
 
static int load_module (void)
 
static int reload (void)
 standard module glue More...
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Adaptive Differential PCM 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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .reload = reload, }
 
static struct ast_translator adpcmtolin
 
static struct ast_module_infoast_module_info = &__mod_info
 
static int indsft [8] = { -1, -1, -1, -1, 2, 4, 6, 8 }
 
static struct ast_translator lintoadpcm
 
static int stpsz [49]
 

Detailed Description

codec_adpcm.c - translate between signed linear and Dialogic ADPCM

Definition in file codec_adpcm.c.

Macro Definition Documentation

#define BUFFER_SAMPLES   8096 /* size for the translation buffers */

Definition at line 48 of file codec_adpcm.c.

Function Documentation

static void __reg_module ( void  )
static

Definition at line 348 of file codec_adpcm.c.

static void __unreg_module ( void  )
static

Definition at line 348 of file codec_adpcm.c.

static int adpcm ( short  csig,
struct adpcm_state state 
)
inlinestatic

Definition at line 166 of file codec_adpcm.c.

References decode(), adpcm_state::signal, and adpcm_state::ssindex.

Referenced by lintoadpcm_frameout().

167 {
168  int diff;
169  int step;
170  int encoded;
171 
172  /*
173  * Clip csig if too large or too small
174  */
175  csig >>= 4;
176 
177  step = stpsz[state->ssindex];
178  diff = csig - state->signal;
179 
180 #ifdef NOT_BLI
181  if (diff < 0) {
182  encoded = (-diff << 2) / step;
183  if (encoded > 7)
184  encoded = 7;
185  encoded |= 0x08;
186  } else {
187  encoded = (diff << 2) / step;
188  if (encoded > 7)
189  encoded = 7;
190  }
191 #else /* BLI code */
192  if (diff < 0) {
193  encoded = 8;
194  diff = -diff;
195  } else
196  encoded = 0;
197  if (diff >= step) {
198  encoded |= 4;
199  diff -= step;
200  }
201  step >>= 1;
202  if (diff >= step) {
203  encoded |= 2;
204  diff -= step;
205  }
206  step >>= 1;
207  if (diff >= step)
208  encoded |= 1;
209 #endif /* NOT_BLI */
210 
211  /* feedback to state */
212  decode(encoded, state);
213 
214  return encoded;
215 }
static int stpsz[49]
Definition: codec_adpcm.c:64
static short decode(int encoded, struct adpcm_state *state)
Definition: codec_adpcm.c:93
static int adpcmtolin_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

decode 4-bit adpcm frame data and store in output buffer

Definition at line 231 of file codec_adpcm.c.

References ast_frame::data, ast_trans_pvt::datalen, ast_frame::datalen, decode(), ast_trans_pvt::i16, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, ast_trans_pvt::samples, ast_frame::samples, and adpcm_decoder_pvt::state.

232 {
233  struct adpcm_decoder_pvt *tmp = pvt->pvt;
234  int x = f->datalen;
235  unsigned char *src = f->data.ptr;
236  int16_t *dst = pvt->outbuf.i16 + pvt->samples;
237 
238  while (x--) {
239  *dst++ = decode((*src >> 4) & 0xf, &tmp->state);
240  *dst++ = decode(*src++ & 0x0f, &tmp->state);
241  }
242  pvt->samples += f->samples;
243  pvt->datalen += 2*f->samples;
244  return 0;
245 }
int datalen
actual space used in outbuf
Definition: translate.h:140
union ast_trans_pvt::@213 outbuf
Workspace for translating ADPCM signals to signed linear.
Definition: codec_adpcm.c:226
void * ptr
Definition: frame.h:160
void * pvt
Definition: translate.h:141
int16_t * i16
Definition: translate.h:145
int datalen
Definition: frame.h:148
union ast_frame::@172 data
struct adpcm_state state
Definition: codec_adpcm.c:227
int samples
Definition: frame.h:150
static short decode(int encoded, struct adpcm_state *state)
Definition: codec_adpcm.c:93
static short decode ( int  encoded,
struct adpcm_state state 
)
inlinestatic

Definition at line 93 of file codec_adpcm.c.

References adpcm_state::next_flag, adpcm_state::signal, adpcm_state::ssindex, and adpcm_state::zero_count.

Referenced by adpcm(), and adpcmtolin_framein().

94 {
95  int diff;
96  int step;
97  int sign;
98 
99  step = stpsz[state->ssindex];
100 
101  sign = encoded & 0x08;
102  encoded &= 0x07;
103 #ifdef NOT_BLI
104  diff = (((encoded << 1) + 1) * step) >> 3;
105 #else /* BLI code */
106  diff = step >> 3;
107  if (encoded & 4)
108  diff += step;
109  if (encoded & 2)
110  diff += step >> 1;
111  if (encoded & 1)
112  diff += step >> 2;
113  if ((encoded >> 1) & step & 0x1)
114  diff++;
115 #endif
116  if (sign)
117  diff = -diff;
118 
119  if (state->next_flag & 0x1)
120  state->signal -= 8;
121  else if (state->next_flag & 0x2)
122  state->signal += 8;
123 
124  state->signal += diff;
125 
126  if (state->signal > 2047)
127  state->signal = 2047;
128  else if (state->signal < -2047)
129  state->signal = -2047;
130 
131  state->next_flag = 0;
132 
133 #ifdef AUTO_RETURN
134  if (encoded)
135  state->zero_count = 0;
136  else if (++(state->zero_count) == 24) {
137  state->zero_count = 0;
138  if (state->signal > 0)
139  state->next_flag = 0x1;
140  else if (state->signal < 0)
141  state->next_flag = 0x2;
142  }
143 #endif
144 
145  state->ssindex += indsft[encoded];
146  if (state->ssindex < 0)
147  state->ssindex = 0;
148  else if (state->ssindex > 48)
149  state->ssindex = 48;
150 
151  return state->signal << 4;
152 }
static int indsft[8]
Definition: codec_adpcm.c:58
int zero_count
Definition: codec_adpcm.c:78
static int stpsz[49]
Definition: codec_adpcm.c:64
static int lintoadpcm_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

fill input buffer with 16-bit signed linear PCM values.

Definition at line 248 of file codec_adpcm.c.

References ast_frame::data, ast_frame::datalen, adpcm_encoder_pvt::inbuf, ast_frame::ptr, ast_trans_pvt::pvt, ast_trans_pvt::samples, and ast_frame::samples.

249 {
250  struct adpcm_encoder_pvt *tmp = pvt->pvt;
251 
252  memcpy(&tmp->inbuf[pvt->samples], f->data.ptr, f->datalen);
253  pvt->samples += f->samples;
254  return 0;
255 }
void * ptr
Definition: frame.h:160
void * pvt
Definition: translate.h:141
int datalen
Definition: frame.h:148
Workspace for translating signed linear signals to ADPCM.
Definition: codec_adpcm.c:220
int16_t inbuf[BUFFER_SAMPLES]
Definition: codec_adpcm.c:222
union ast_frame::@172 data
int samples
Definition: frame.h:150
static struct ast_frame* lintoadpcm_frameout ( struct ast_trans_pvt pvt)
static

convert inbuf and store into frame

Definition at line 258 of file codec_adpcm.c.

References adpcm(), ast_trans_frameout(), ast_trans_pvt::c, f, adpcm_encoder_pvt::inbuf, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, ast_trans_pvt::samples, ast_frame::samples, and adpcm_encoder_pvt::state.

259 {
260  struct adpcm_encoder_pvt *tmp = pvt->pvt;
261  struct ast_frame *f;
262  int i;
263  int samples = pvt->samples; /* save original number */
264 
265  if (samples < 2)
266  return NULL;
267 
268  pvt->samples &= ~1; /* atomic size is 2 samples */
269 
270  for (i = 0; i < pvt->samples; i += 2) {
271  pvt->outbuf.c[i/2] =
272  (adpcm(tmp->inbuf[i ], &tmp->state) << 4) |
273  (adpcm(tmp->inbuf[i+1], &tmp->state) );
274  };
275 
276  f = ast_trans_frameout(pvt, pvt->samples/2, 0);
277 
278  /*
279  * If there is a left over sample, move it to the beginning
280  * of the input buffer.
281  */
282 
283  if (samples & 1) { /* move the leftover sample at beginning */
284  tmp->inbuf[0] = tmp->inbuf[samples - 1];
285  pvt->samples = 1;
286  }
287  return f;
288 }
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition: translate.c:235
union ast_trans_pvt::@213 outbuf
static int adpcm(short csig, struct adpcm_state *state)
Definition: codec_adpcm.c:166
void * pvt
Definition: translate.h:141
static struct ast_format f[]
Definition: format_g726.c:181
Data structure associated with a single frame of data.
Definition: frame.h:142
Workspace for translating signed linear signals to ADPCM.
Definition: codec_adpcm.c:220
int16_t inbuf[BUFFER_SAMPLES]
Definition: codec_adpcm.c:222
struct adpcm_state state
Definition: codec_adpcm.c:221
int samples
Definition: frame.h:150
static int load_module ( void  )
static

Definition at line 330 of file codec_adpcm.c.

References AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_register_translator, and ast_unregister_translator().

331 {
332  int res;
333 
335  if (!res)
337  else
339  if (res)
342 }
static struct ast_translator lintoadpcm
Definition: codec_adpcm.c:302
#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_translator adpcmtolin
Definition: codec_adpcm.c:291
static int reload ( void  )
static

standard module glue

Definition at line 315 of file codec_adpcm.c.

References AST_MODULE_LOAD_SUCCESS.

316 {
318 }
static int unload_module ( void  )
static

Definition at line 320 of file codec_adpcm.c.

References ast_unregister_translator().

321 {
322  int res;
323 
326 
327  return res;
328 }
static struct ast_translator lintoadpcm
Definition: codec_adpcm.c:302
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:942
static struct ast_translator adpcmtolin
Definition: codec_adpcm.c:291

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Adaptive Differential PCM 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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .reload = reload, }
static

Definition at line 348 of file codec_adpcm.c.

struct ast_translator adpcmtolin
static

Definition at line 291 of file codec_adpcm.c.

Definition at line 348 of file codec_adpcm.c.

int indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 }
static

Definition at line 58 of file codec_adpcm.c.

struct ast_translator lintoadpcm
static

Definition at line 302 of file codec_adpcm.c.

int stpsz[49]
static

Definition at line 64 of file codec_adpcm.c.