Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
codec_codec2.c File Reference

Translate between signed linear and Codec 2. More...

#include "asterisk.h"
#include "asterisk/codec.h"
#include "asterisk/frame.h"
#include "asterisk/linkedlists.h"
#include "asterisk/logger.h"
#include "asterisk/module.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/translate.h"
#include <codec2/codec2.h>
#include "asterisk/slin.h"
#include "ex_codec2.h"
Include dependency graph for codec_codec2.c:

Go to the source code of this file.

Data Structures

struct  codec2_translator_pvt
 

Macros

#define BUFFER_SAMPLES   8000
 
#define CODEC2_FRAME_LEN   6 /* consider codec2_bits_per_frame(.) */
 
#define CODEC2_SAMPLES   160 /* consider codec2_samples_per_frame(.) */
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void codec2_destroy_stuff (struct ast_trans_pvt *pvt)
 
static int codec2_new (struct ast_trans_pvt *pvt)
 
static int codec2tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 decode and store in outbuf. More...
 
static int lintocodec2_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 store samples into working buffer for later decode More...
 
static struct ast_framelintocodec2_frameout (struct ast_trans_pvt *pvt)
 encode and produce a frame More...
 
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 = "Codec 2 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 codec2tolin
 
static struct ast_translator lintocodec2
 

Detailed Description

Translate between signed linear and Codec 2.

Author
Alexander Traud pabst[email protected]raud[email protected]@comp[email protected]user[email protected]ve.co[email protected]m
Note
http://www.rowetel.com/codec2.html

Definition in file codec_codec2.c.

Macro Definition Documentation

◆ BUFFER_SAMPLES

#define BUFFER_SAMPLES   8000

Definition at line 47 of file codec_codec2.c.

◆ CODEC2_FRAME_LEN

#define CODEC2_FRAME_LEN   6 /* consider codec2_bits_per_frame(.) */

Definition at line 49 of file codec_codec2.c.

Referenced by codec2tolin_framein(), and lintocodec2_frameout().

◆ CODEC2_SAMPLES

#define CODEC2_SAMPLES   160 /* consider codec2_samples_per_frame(.) */

Definition at line 48 of file codec_codec2.c.

Referenced by codec2_sample(), codec2tolin_framein(), and lintocodec2_frameout().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 222 of file codec_codec2.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 222 of file codec_codec2.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 222 of file codec_codec2.c.

◆ codec2_destroy_stuff()

static void codec2_destroy_stuff ( struct ast_trans_pvt pvt)
static

Definition at line 141 of file codec_codec2.c.

References ast_trans_pvt::pvt, codec2_translator_pvt::state, and tmp().

142 {
143  struct codec2_translator_pvt *tmp = pvt->pvt;
144 
145  if (tmp->state) {
146  codec2_destroy(tmp->state);
147  }
148 }
struct CODEC2 * state
Definition: codec_codec2.c:56
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219

◆ codec2_new()

static int codec2_new ( struct ast_trans_pvt pvt)
static

Definition at line 60 of file codec_codec2.c.

References ast_log, LOG_ERROR, ast_trans_pvt::pvt, codec2_translator_pvt::state, and tmp().

61 {
62  struct codec2_translator_pvt *tmp = pvt->pvt;
63 
64  tmp->state = codec2_create(CODEC2_MODE_2400);
65 
66  if (!tmp->state) {
67  ast_log(LOG_ERROR, "Error creating Codec 2 conversion\n");
68  return -1;
69  }
70 
71  return 0;
72 }
struct CODEC2 * state
Definition: codec_codec2.c:56
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285

◆ codec2tolin_framein()

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

decode and store in outbuf.

Definition at line 75 of file codec_codec2.c.

References CODEC2_FRAME_LEN, CODEC2_SAMPLES, ast_frame::data, ast_frame::datalen, ast_trans_pvt::datalen, ast_trans_pvt::i16, ast_trans_pvt::outbuf, ast_frame::ptr, ast_trans_pvt::pvt, ast_trans_pvt::samples, codec2_translator_pvt::state, and tmp().

76 {
77  struct codec2_translator_pvt *tmp = pvt->pvt;
78  int x;
79 
80  for (x = 0; x < f->datalen; x += CODEC2_FRAME_LEN) {
81  unsigned char *src = f->data.ptr + x;
82  int16_t *dst = pvt->outbuf.i16 + pvt->samples;
83 
84  codec2_decode(tmp->state, dst, src);
85 
86  pvt->samples += CODEC2_SAMPLES;
87  pvt->datalen += CODEC2_SAMPLES * 2;
88  }
89 
90  return 0;
91 }
int datalen
actual space used in outbuf
Definition: translate.h:218
struct CODEC2 * state
Definition: codec_codec2.c:56
short int16_t
Definition: db.h:59
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219
int16_t * i16
Definition: translate.h:223
union ast_trans_pvt::@327 outbuf
#define CODEC2_SAMPLES
Definition: codec_codec2.c:48
#define CODEC2_FRAME_LEN
Definition: codec_codec2.c:49
union ast_frame::@263 data

◆ lintocodec2_framein()

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

store samples into working buffer for later decode

Definition at line 94 of file codec_codec2.c.

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

95 {
96  struct codec2_translator_pvt *tmp = pvt->pvt;
97 
98  memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
99  pvt->samples += f->samples;
100 
101  return 0;
102 }
int16_t buf[BUFFER_SAMPLES]
Definition: codec_codec2.c:57
static int tmp()
Definition: bt_open.c:389
void * pvt
Definition: translate.h:219
union ast_frame::@263 data

◆ lintocodec2_frameout()

static struct ast_frame* lintocodec2_frameout ( struct ast_trans_pvt pvt)
static

encode and produce a frame

Definition at line 105 of file codec_codec2.c.

References AST_LIST_NEXT, ast_trans_frameout(), codec2_translator_pvt::buf, CODEC2_FRAME_LEN, CODEC2_SAMPLES, last, NULL, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, result, ast_frame::samples, ast_trans_pvt::samples, codec2_translator_pvt::state, tmp(), and ast_trans_pvt::uc.

106 {
107  struct codec2_translator_pvt *tmp = pvt->pvt;
108  struct ast_frame *result = NULL;
109  struct ast_frame *last = NULL;
110  int samples = 0; /* output samples */
111 
112  while (pvt->samples >= CODEC2_SAMPLES) {
113  struct ast_frame *current;
114 
115  /* Encode a frame of data */
116  codec2_encode(tmp->state, pvt->outbuf.uc, tmp->buf + samples);
117 
118  samples += CODEC2_SAMPLES;
119  pvt->samples -= CODEC2_SAMPLES;
120 
122 
123  if (!current) {
124  continue;
125  } else if (last) {
126  AST_LIST_NEXT(last, frame_list) = current;
127  } else {
128  result = current;
129  }
130  last = current;
131  }
132 
133  /* Move the data at the end of the buffer to the front */
134  if (samples) {
135  memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
136  }
137 
138  return result;
139 }
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition: translate.c:438
struct CODEC2 * state
Definition: codec_codec2.c:56
int16_t buf[BUFFER_SAMPLES]
Definition: codec_codec2.c:57
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
unsigned char * uc
Definition: translate.h:222
#define NULL
Definition: resample.c:96
void * pvt
Definition: translate.h:219
struct sla_ringing_trunk * last
Definition: app_meetme.c:1092
union ast_trans_pvt::@327 outbuf
#define CODEC2_SAMPLES
Definition: codec_codec2.c:48
#define CODEC2_FRAME_LEN
Definition: codec_codec2.c:49
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 206 of file codec_codec2.c.

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

207 {
208  int res = 0;
209 
213 
214  if (res) {
215  unload_module();
217  }
218 
220 }
static struct ast_translator codec2tolin
Definition: codec_codec2.c:150
static int unload_module(void)
Definition: codec_codec2.c:195
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:257
struct ast_format * ast_format_codec2
Built-in cached Codec 2 format.
Definition: format_cache.c:231
static struct ast_translator lintocodec2
Definition: codec_codec2.c:172
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
int ast_rtp_engine_load_format(struct ast_format *format)
Custom formats declared in codecs.conf at startup must be communicated to the rtp_engine so their mim...
Definition: rtp_engine.c:3229

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 195 of file codec_codec2.c.

References ast_format_codec2, ast_rtp_engine_unload_format(), and ast_unregister_translator().

Referenced by load_module().

196 {
197  int res = 0;
198 
202 
203  return res;
204 }
static struct ast_translator codec2tolin
Definition: codec_codec2.c:150
int ast_rtp_engine_unload_format(struct ast_format *format)
Formats requiring the use of a format attribute interface must have that interface registered in orde...
Definition: rtp_engine.c:3245
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
Definition: translate.c:1333
struct ast_format * ast_format_codec2
Built-in cached Codec 2 format.
Definition: format_cache.c:231
static struct ast_translator lintocodec2
Definition: codec_codec2.c:172

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Codec 2 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 222 of file codec_codec2.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 222 of file codec_codec2.c.

◆ codec2tolin

struct ast_translator codec2tolin
static

Definition at line 150 of file codec_codec2.c.

◆ lintocodec2

struct ast_translator lintocodec2
static

Definition at line 172 of file codec_codec2.c.