Wed Jan 8 2020 09:50:10

Asterisk developer's documentation


codec_lpc10.c File Reference

Translate between signed linear and LPC10 (Linear Predictor Code) More...

#include "asterisk.h"
#include "asterisk/translate.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "lpc10/lpc10.h"
#include "asterisk/slin.h"
#include "ex_lpc10.h"

Go to the source code of this file.

Data Structures

struct  lpc10_coder_pvt
 

Macros

#define BUFFER_SAMPLES   8000
 
#define LPC10_BYTES_IN_COMPRESSED_FRAME   (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static void build_bits (unsigned char *c, INT32 *bits)
 
static void extract_bits (INT32 *bits, unsigned char *c)
 
static int lintolpc10_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 
static struct ast_framelintolpc10_frameout (struct ast_trans_pvt *pvt)
 
static int load_module (void)
 
static int lpc10_dec_new (struct ast_trans_pvt *pvt)
 
static void lpc10_destroy (struct ast_trans_pvt *arg)
 
static int lpc10_enc_new (struct ast_trans_pvt *pvt)
 
static int lpc10tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 
static int reload (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "LPC10 2.4kbps 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_module_infoast_module_info = &__mod_info
 
static struct ast_translator lintolpc10
 
static struct ast_translator lpc10tolin
 

Detailed Description

Translate between signed linear and LPC10 (Linear Predictor Code)

Definition in file codec_lpc10.c.

Macro Definition Documentation

#define BUFFER_SAMPLES   8000

Definition at line 56 of file codec_lpc10.c.

Referenced by lintolpc10_framein(), and lpc10tolin_framein().

#define LPC10_BYTES_IN_COMPRESSED_FRAME   (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8

Definition at line 54 of file codec_lpc10.c.

Referenced by lintolpc10_frameout(), and lpc10tolin_framein().

Function Documentation

static void __reg_module ( void  )
static

Definition at line 259 of file codec_lpc10.c.

static void __unreg_module ( void  )
static

Definition at line 259 of file codec_lpc10.c.

static void build_bits ( unsigned char *  c,
INT32 *  bits 
)
static

Definition at line 96 of file codec_lpc10.c.

Referenced by lintolpc10_frameout().

97 {
98  unsigned char mask=0x80;
99  int x;
100  *c = 0;
101  for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
102  if (bits[x])
103  *c |= mask;
104  mask = mask >> 1;
105  if ((x % 8)==7) {
106  c++;
107  *c = 0;
108  mask = 0x80;
109  }
110  }
111 }
static void extract_bits ( INT32 *  bits,
unsigned char *  c 
)
static

Definition at line 82 of file codec_lpc10.c.

Referenced by lpc10tolin_framein().

83 {
84  int x;
85  for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
86  if (*c & (0x80 >> (x & 7)))
87  bits[x] = 1;
88  else
89  bits[x] = 0;
90  if ((x & 7) == 7)
91  c++;
92  }
93 }
static int lintolpc10_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

Definition at line 146 of file codec_lpc10.c.

References ast_log(), lpc10_coder_pvt::buf, BUFFER_SAMPLES, ast_frame::data, ast_frame::datalen, LOG_WARNING, ast_frame::ptr, ast_trans_pvt::pvt, ast_trans_pvt::samples, and ast_frame::samples.

147 {
148  struct lpc10_coder_pvt *tmp = pvt->pvt;
149 
150  /* Just add the frames to our stream */
151  if (pvt->samples + f->samples > BUFFER_SAMPLES) {
152  ast_log(LOG_WARNING, "Out of buffer space\n");
153  return -1;
154  }
155  memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
156  pvt->samples += f->samples;
157  return 0;
158 }
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
void * pvt
Definition: translate.h:141
short buf[BUFFER_SAMPLES]
Definition: codec_lpc10.c:64
int datalen
Definition: frame.h:148
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
#define BUFFER_SAMPLES
Definition: codec_lpc10.c:56
union ast_frame::@172 data
int samples
Definition: frame.h:150
static struct ast_frame* lintolpc10_frameout ( struct ast_trans_pvt pvt)
static

Definition at line 160 of file codec_lpc10.c.

References ast_trans_frameout(), lpc10_coder_pvt::buf, build_bits(), lpc10_coder_pvt::enc, lpc10_coder_pvt::longer, lpc10_coder_pvt::lpc10, LPC10_BYTES_IN_COMPRESSED_FRAME, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, ast_trans_pvt::samples, and ast_trans_pvt::uc.

161 {
162  struct lpc10_coder_pvt *tmp = pvt->pvt;
163  int x;
164  int datalen = 0; /* output frame */
165  int samples = 0; /* output samples */
166  float tmpbuf[LPC10_SAMPLES_PER_FRAME];
167  INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX what ??? */
168  /* We can't work on anything less than a frame in size */
169  if (pvt->samples < LPC10_SAMPLES_PER_FRAME)
170  return NULL;
171  while (pvt->samples >= LPC10_SAMPLES_PER_FRAME) {
172  /* Encode a frame of data */
173  for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++)
174  tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0;
175  lpc10_encode(tmpbuf, bits, tmp->lpc10.enc);
176  build_bits(pvt->outbuf.uc + datalen, bits);
178  samples += LPC10_SAMPLES_PER_FRAME;
179  pvt->samples -= LPC10_SAMPLES_PER_FRAME;
180  /* Use one of the two left over bits to record if this is a 22 or 23 ms frame...
181  important for IAX use */
182  tmp->longer = 1 - tmp->longer;
183  }
184  /* Move the data at the end of the buffer to the front */
185  if (pvt->samples)
186  memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
187  return ast_trans_frameout(pvt, datalen, samples);
188 }
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition: translate.c:235
#define LPC10_BYTES_IN_COMPRESSED_FRAME
Definition: codec_lpc10.c:54
union ast_trans_pvt::@213 outbuf
unsigned char * uc
Definition: translate.h:144
void * pvt
Definition: translate.h:141
short buf[BUFFER_SAMPLES]
Definition: codec_lpc10.c:64
struct lpc10_encoder_state * enc
Definition: codec_lpc10.c:60
union lpc10_coder_pvt::@128 lpc10
static void build_bits(unsigned char *c, INT32 *bits)
Definition: codec_lpc10.c:96
static int load_module ( void  )
static

Definition at line 241 of file codec_lpc10.c.

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

242 {
243  int res;
244 
246  if (!res)
248  else
250  if (res)
253 }
static struct ast_translator lpc10tolin
Definition: codec_lpc10.c:198
static struct ast_translator lintolpc10
Definition: codec_lpc10.c:211
#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 int lpc10_dec_new ( struct ast_trans_pvt pvt)
static

Definition at line 75 of file codec_lpc10.c.

References lpc10_coder_pvt::dec, lpc10_coder_pvt::lpc10, and ast_trans_pvt::pvt.

76 {
77  struct lpc10_coder_pvt *tmp = pvt->pvt;
78 
79  return (tmp->lpc10.dec = create_lpc10_decoder_state()) ? 0 : -1;
80 }
void * pvt
Definition: translate.h:141
union lpc10_coder_pvt::@128 lpc10
struct lpc10_decoder_state * dec
Definition: codec_lpc10.c:61
static void lpc10_destroy ( struct ast_trans_pvt arg)
static

Definition at line 191 of file codec_lpc10.c.

References ast_free, lpc10_coder_pvt::enc, lpc10_coder_pvt::lpc10, and ast_trans_pvt::pvt.

192 {
193  struct lpc10_coder_pvt *pvt = arg->pvt;
194  /* Enc and DEC are both just allocated, so they can be freed */
195  ast_free(pvt->lpc10.enc);
196 }
void * pvt
Definition: translate.h:141
struct lpc10_encoder_state * enc
Definition: codec_lpc10.c:60
union lpc10_coder_pvt::@128 lpc10
#define ast_free(a)
Definition: astmm.h:97
static int lpc10_enc_new ( struct ast_trans_pvt pvt)
static

Definition at line 68 of file codec_lpc10.c.

References lpc10_coder_pvt::enc, lpc10_coder_pvt::lpc10, and ast_trans_pvt::pvt.

69 {
70  struct lpc10_coder_pvt *tmp = pvt->pvt;
71 
72  return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
73 }
void * pvt
Definition: translate.h:141
struct lpc10_encoder_state * enc
Definition: codec_lpc10.c:60
union lpc10_coder_pvt::@128 lpc10
static int lpc10tolin_framein ( struct ast_trans_pvt pvt,
struct ast_frame f 
)
static

Definition at line 113 of file codec_lpc10.c.

References ast_log(), BUFFER_SAMPLES, ast_frame::data, ast_trans_pvt::datalen, ast_frame::datalen, lpc10_coder_pvt::dec, extract_bits(), ast_trans_pvt::i16, len(), LOG_WARNING, lpc10_coder_pvt::lpc10, LPC10_BYTES_IN_COMPRESSED_FRAME, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, and ast_trans_pvt::samples.

114 {
115  struct lpc10_coder_pvt *tmp = pvt->pvt;
116  int16_t *dst = pvt->outbuf.i16;
117  int len = 0;
118 
119  while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {
120  int x;
121  float tmpbuf[LPC10_SAMPLES_PER_FRAME];
122  INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX see note */
123  if (pvt->samples + LPC10_SAMPLES_PER_FRAME > BUFFER_SAMPLES) {
124  ast_log(LOG_WARNING, "Out of buffer space\n");
125  return -1;
126  }
127  extract_bits(bits, f->data.ptr + len);
128  if (lpc10_decode(bits, tmpbuf, tmp->lpc10.dec)) {
129  ast_log(LOG_WARNING, "Invalid lpc10 data\n");
130  return -1;
131  }
132  for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++) {
133  /* Convert to a short between -1.0 and 1.0 */
134  dst[pvt->samples + x] = (int16_t)(32768.0 * tmpbuf[x]);
135  }
136 
137  pvt->samples += LPC10_SAMPLES_PER_FRAME;
138  pvt->datalen += 2*LPC10_SAMPLES_PER_FRAME;
140  }
141  if (len != f->datalen)
142  printf("Decoded %d, expected %d\n", len, f->datalen);
143  return 0;
144 }
int datalen
actual space used in outbuf
Definition: translate.h:140
#define LPC10_BYTES_IN_COMPRESSED_FRAME
Definition: codec_lpc10.c:54
union ast_trans_pvt::@213 outbuf
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
void * pvt
Definition: translate.h:141
int16_t * i16
Definition: translate.h:145
int datalen
Definition: frame.h:148
static void extract_bits(INT32 *bits, unsigned char *c)
Definition: codec_lpc10.c:82
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
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
union lpc10_coder_pvt::@128 lpc10
#define BUFFER_SAMPLES
Definition: codec_lpc10.c:56
struct lpc10_decoder_state * dec
Definition: codec_lpc10.c:61
union ast_frame::@172 data
static int reload ( void  )
static

Definition at line 225 of file codec_lpc10.c.

References AST_MODULE_LOAD_SUCCESS.

226 {
228 }
static int unload_module ( void  )
static

Definition at line 231 of file codec_lpc10.c.

References ast_unregister_translator().

232 {
233  int res;
234 
237 
238  return res;
239 }
static struct ast_translator lpc10tolin
Definition: codec_lpc10.c:198
static struct ast_translator lintolpc10
Definition: codec_lpc10.c:211
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:942

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "LPC10 2.4kbps 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 259 of file codec_lpc10.c.

Definition at line 259 of file codec_lpc10.c.

struct ast_translator lintolpc10
static

Definition at line 211 of file codec_lpc10.c.

struct ast_translator lpc10tolin
static

Definition at line 198 of file codec_lpc10.c.