Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
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 "asterisk/linkedlists.h"
#include "lpc10/lpc10.h"
#include "asterisk/slin.h"
#include "ex_lpc10.h"
Include dependency graph for codec_lpc10.c:

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)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, }
 
static const 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

◆ BUFFER_SAMPLES

#define BUFFER_SAMPLES   8000

Definition at line 55 of file codec_lpc10.c.

Referenced by lintolpc10_framein(), and lpc10tolin_framein().

◆ LPC10_BYTES_IN_COMPRESSED_FRAME

#define LPC10_BYTES_IN_COMPRESSED_FRAME   (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8

Definition at line 53 of file codec_lpc10.c.

Referenced by lintolpc10_frameout(), and lpc10tolin_framein().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 285 of file codec_lpc10.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 285 of file codec_lpc10.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 285 of file codec_lpc10.c.

◆ build_bits()

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

Definition at line 95 of file codec_lpc10.c.

References LPC10_BITS_IN_COMPRESSED_FRAME.

Referenced by lintolpc10_frameout().

96 {
97  unsigned char mask=0x80;
98  int x;
99  *c = 0;
100  for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
101  if (bits[x])
102  *c |= mask;
103  mask = mask >> 1;
104  if ((x % 8)==7) {
105  c++;
106  *c = 0;
107  mask = 0x80;
108  }
109  }
110 }
#define LPC10_BITS_IN_COMPRESSED_FRAME
Definition: lpc10.h:37
static struct test_val c

◆ extract_bits()

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

Definition at line 81 of file codec_lpc10.c.

References LPC10_BITS_IN_COMPRESSED_FRAME.

Referenced by lpc10tolin_framein().

82 {
83  int x;
84  for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
85  if (*c & (0x80 >> (x & 7)))
86  bits[x] = 1;
87  else
88  bits[x] = 0;
89  if ((x & 7) == 7)
90  c++;
91  }
92 }
#define LPC10_BITS_IN_COMPRESSED_FRAME
Definition: lpc10.h:37
static struct test_val c

◆ lintolpc10_framein()

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

Definition at line 145 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_frame::samples, ast_trans_pvt::samples, and tmp().

146 {
147  struct lpc10_coder_pvt *tmp = pvt->pvt;
148 
149  /* Just add the frames to our stream */
150  if (pvt->samples + f->samples > BUFFER_SAMPLES) {
151  ast_log(LOG_WARNING, "Out of buffer space\n");
152  return -1;
153  }
154  memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
155  pvt->samples += f->samples;
156  return 0;
157 }
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219
short buf[BUFFER_SAMPLES]
Definition: codec_lpc10.c:63
#define ast_log
Definition: astobj2.c:42
#define BUFFER_SAMPLES
Definition: codec_lpc10.c:55
union ast_frame::@263 data

◆ lintolpc10_frameout()

static struct ast_frame* lintolpc10_frameout ( struct ast_trans_pvt pvt)
static

Definition at line 159 of file codec_lpc10.c.

References AST_LIST_NEXT, ast_trans_frameout(), lpc10_coder_pvt::buf, build_bits(), lpc10_coder_pvt::enc, last, lpc10_coder_pvt::longer, lpc10_coder_pvt::lpc10, LPC10_BITS_IN_COMPRESSED_FRAME, LPC10_BYTES_IN_COMPRESSED_FRAME, lpc10_encode(), LPC10_SAMPLES_PER_FRAME, NULL, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, result, ast_frame::samples, ast_trans_pvt::samples, tmp(), and ast_trans_pvt::uc.

160 {
161  struct lpc10_coder_pvt *tmp = pvt->pvt;
162  struct ast_frame *result = NULL;
163  struct ast_frame *last = NULL;
164  int samples = 0; /* output samples */
165 
166  while (pvt->samples >= LPC10_SAMPLES_PER_FRAME) {
167  struct ast_frame *current;
168  float tmpbuf[LPC10_SAMPLES_PER_FRAME];
169  INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX what ??? */
170  int x;
171 
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, bits);
177 
178  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  current = ast_trans_frameout(pvt, LPC10_BYTES_IN_COMPRESSED_FRAME, LPC10_SAMPLES_PER_FRAME);
185  if (!current) {
186  continue;
187  } else if (last) {
188  AST_LIST_NEXT(last, frame_list) = current;
189  } else {
190  result = current;
191  }
192  last = current;
193  }
194 
195  /* Move the data at the end of the buffer to the front */
196  if (samples) {
197  memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
198  }
199 
200  return result;
201 }
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition: translate.c:438
#define LPC10_BYTES_IN_COMPRESSED_FRAME
Definition: codec_lpc10.c:53
int lpc10_encode(real *speech, INT32 *bits, struct lpc10_encoder_state *st)
Definition: lpcenc.c:108
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
#define LPC10_BITS_IN_COMPRESSED_FRAME
Definition: lpc10.h:37
unsigned char * uc
Definition: translate.h:222
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
#define LPC10_SAMPLES_PER_FRAME
Definition: lpc10.h:36
union lpc10_coder_pvt::@183 lpc10
short buf[BUFFER_SAMPLES]
Definition: codec_lpc10.c:63
struct sla_ringing_trunk * last
Definition: app_meetme.c:1092
struct lpc10_encoder_state * enc
Definition: codec_lpc10.c:59
union ast_trans_pvt::@327 outbuf
static void build_bits(unsigned char *c, INT32 *bits)
Definition: codec_lpc10.c:95
static PGresult * result
Definition: cel_pgsql.c:88
Data structure associated with a single frame of data.

◆ load_module()

static int load_module ( void  )
static

Definition at line 266 of file codec_lpc10.c.

References AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ast_register_translator, ASTERISK_GPL_KEY, and unload_module().

267 {
268  int res;
269 
272 
273  if (res) {
274  unload_module();
276  }
277 
279 }
static struct ast_translator lpc10tolin
Definition: codec_lpc10.c:211
static struct ast_translator lintolpc10
Definition: codec_lpc10.c:233
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:257
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static int unload_module(void)
Definition: codec_lpc10.c:256

◆ lpc10_dec_new()

static int lpc10_dec_new ( struct ast_trans_pvt pvt)
static

Definition at line 74 of file codec_lpc10.c.

References create_lpc10_decoder_state(), lpc10_coder_pvt::dec, lpc10_coder_pvt::lpc10, ast_trans_pvt::pvt, and tmp().

75 {
76  struct lpc10_coder_pvt *tmp = pvt->pvt;
77 
78  return (tmp->lpc10.dec = create_lpc10_decoder_state()) ? 0 : -1;
79 }
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219
union lpc10_coder_pvt::@183 lpc10
struct lpc10_decoder_state * create_lpc10_decoder_state(void)
Definition: lpcini.c:369
struct lpc10_decoder_state * dec
Definition: codec_lpc10.c:60

◆ lpc10_destroy()

static void lpc10_destroy ( struct ast_trans_pvt arg)
static

Definition at line 204 of file codec_lpc10.c.

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

205 {
206  struct lpc10_coder_pvt *pvt = arg->pvt;
207  /* Enc and DEC are both just allocated, so they can be freed */
208  ast_free(pvt->lpc10.enc);
209 }
void * pvt
Definition: translate.h:219
union lpc10_coder_pvt::@183 lpc10
struct lpc10_encoder_state * enc
Definition: codec_lpc10.c:59
#define ast_free(a)
Definition: astmm.h:182

◆ lpc10_enc_new()

static int lpc10_enc_new ( struct ast_trans_pvt pvt)
static

Definition at line 67 of file codec_lpc10.c.

References create_lpc10_encoder_state(), lpc10_coder_pvt::enc, lpc10_coder_pvt::lpc10, ast_trans_pvt::pvt, and tmp().

68 {
69  struct lpc10_coder_pvt *tmp = pvt->pvt;
70 
71  return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
72 }
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219
union lpc10_coder_pvt::@183 lpc10
struct lpc10_encoder_state * enc
Definition: codec_lpc10.c:59
struct lpc10_encoder_state * create_lpc10_encoder_state(void)
Definition: lpcini.c:258

◆ lpc10tolin_framein()

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

Definition at line 112 of file codec_lpc10.c.

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

113 {
114  struct lpc10_coder_pvt *tmp = pvt->pvt;
115  int16_t *dst = pvt->outbuf.i16;
116  int len = 0;
117 
118  while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {
119  int x;
120  float tmpbuf[LPC10_SAMPLES_PER_FRAME];
121  INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX see note */
123  ast_log(LOG_WARNING, "Out of buffer space\n");
124  return -1;
125  }
126  extract_bits(bits, f->data.ptr + len);
127  if (lpc10_decode(bits, tmpbuf, tmp->lpc10.dec)) {
128  ast_log(LOG_WARNING, "Invalid lpc10 data\n");
129  return -1;
130  }
131  for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++) {
132  /* Convert to a short between -1.0 and 1.0 */
133  dst[pvt->samples + x] = (int16_t)(32768.0 * tmpbuf[x]);
134  }
135 
139  }
140  if (len != f->datalen)
141  printf("Decoded %d, expected %d\n", len, f->datalen);
142  return 0;
143 }
int datalen
actual space used in outbuf
Definition: translate.h:218
#define LPC10_BYTES_IN_COMPRESSED_FRAME
Definition: codec_lpc10.c:53
short int16_t
Definition: db.h:59
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
#define LPC10_BITS_IN_COMPRESSED_FRAME
Definition: lpc10.h:37
void * pvt
Definition: translate.h:219
#define LPC10_SAMPLES_PER_FRAME
Definition: lpc10.h:36
union lpc10_coder_pvt::@183 lpc10
int16_t * i16
Definition: translate.h:223
#define ast_log
Definition: astobj2.c:42
union ast_trans_pvt::@327 outbuf
int lpc10_decode(INT32 *bits, real *speech, struct lpc10_decoder_state *st)
Definition: lpcdec.c:113
static void extract_bits(INT32 *bits, unsigned char *c)
Definition: codec_lpc10.c:81
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define BUFFER_SAMPLES
Definition: codec_lpc10.c:55
union ast_frame::@263 data
struct lpc10_decoder_state * dec
Definition: codec_lpc10.c:60

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 256 of file codec_lpc10.c.

References ast_unregister_translator().

Referenced by load_module().

257 {
258  int res;
259 
262 
263  return res;
264 }
static struct ast_translator lpc10tolin
Definition: codec_lpc10.c:211
static struct ast_translator lintolpc10
Definition: codec_lpc10.c:233
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_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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, }
static

Definition at line 285 of file codec_lpc10.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 285 of file codec_lpc10.c.

◆ lintolpc10

struct ast_translator lintolpc10
static

Definition at line 233 of file codec_lpc10.c.

◆ lpc10tolin

struct ast_translator lpc10tolin
static

Definition at line 211 of file codec_lpc10.c.