Asterisk - The Open Source Telephony Project GIT-master-0bf3178
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 = AST_BUILDOPT_SUM, .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.nosp@m.raud.nosp@m.@comp.nosp@m.user.nosp@m.ve.co.nosp@m.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.

◆ CODEC2_SAMPLES

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

Definition at line 48 of file codec_codec2.c.

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.

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

References ast_trans_pvt::pvt, and tmp().

◆ codec2_new()

static int codec2_new ( struct ast_trans_pvt pvt)
static

Definition at line 60 of file codec_codec2.c.

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}
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR

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

◆ 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.

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}
#define CODEC2_SAMPLES
Definition: codec_codec2.c:48
#define CODEC2_FRAME_LEN
Definition: codec_codec2.c:49
short int16_t
Definition: db.h:59
union ast_frame::@226 data
int datalen
actual space used in outbuf
Definition: translate.h:218
union ast_trans_pvt::@287 outbuf
int16_t * i16
Definition: translate.h:223

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

◆ 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.

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}

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

◆ 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.

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
119 pvt->samples -= CODEC2_SAMPLES;
120
122
123 if (!current) {
124 continue;
125 } else if (last) {
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 sla_ringing_trunk * last
Definition: app_sla.c:332
static PGresult * result
Definition: cel_pgsql.c:84
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
size_t current
Definition: main/cli.c:113
#define NULL
Definition: resample.c:96
Data structure associated with a single frame of data.
unsigned char * uc
Definition: translate.h:222
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition: translate.c:439

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

◆ load_module()

static int load_module ( void  )
static

Definition at line 206 of file codec_codec2.c.

207{
208 int res = 0;
209
213
214 if (res) {
217 }
218
220}
static struct ast_translator codec2tolin
Definition: codec_codec2.c:150
static struct ast_translator lintocodec2
Definition: codec_codec2.c:172
static int unload_module(void)
Definition: codec_codec2.c:195
struct ast_format * ast_format_codec2
Built-in cached Codec 2 format.
Definition: format_cache.c:226
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
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:3429
#define ast_register_translator(t)
See __ast_register_translator()
Definition: translate.h:258

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

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 195 of file codec_codec2.c.

196{
197 int res = 0;
198
202
203 return res;
204}
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:3445
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given translator.
Definition: translate.c:1350

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

Referenced by load_module().

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 = AST_BUILDOPT_SUM, .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.

Referenced by load_module(), and unload_module().

◆ lintocodec2

struct ast_translator lintocodec2
static

Definition at line 172 of file codec_codec2.c.

Referenced by load_module(), and unload_module().