Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
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.
 
static int lintocodec2_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 store samples into working buffer for later decode
 
static struct ast_framelintocodec2_frameout (struct ast_trans_pvt *pvt)
 encode and produce a frame
 
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 = ASTERISK_GPL_KEY , .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}
struct CODEC2 * state

References ast_trans_pvt::pvt, and codec2_translator_pvt::state.

◆ 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 codec2_translator_pvt::state.

◆ 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
#define CODEC2_FRAME_LEN
union ast_frame::@239 data
union ast_trans_pvt::@308 outbuf
int datalen
actual space used in outbuf
Definition translate.h:218
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 codec2_translator_pvt::state.

◆ 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}
int16_t buf[BUFFER_SAMPLES]

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

◆ 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:338
static PGresult * result
Definition cel_pgsql.c:84
size_t current
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
#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_translator_pvt::buf, CODEC2_FRAME_LEN, CODEC2_SAMPLES, current, last, NULL, ast_trans_pvt::outbuf, ast_trans_pvt::pvt, result, ast_frame::samples, ast_trans_pvt::samples, codec2_translator_pvt::state, 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
static struct ast_translator lintocodec2
static int unload_module(void)
struct ast_format * ast_format_codec2
Built-in cached Codec 2 format.
@ 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...
#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...
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 = ASTERISK_GPL_KEY , .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.

150 {
151 .name = "codec2tolin",
152 .src_codec = {
153 .name = "codec2",
154 .type = AST_MEDIA_TYPE_AUDIO,
155 .sample_rate = 8000,
156 },
157 .dst_codec = {
158 .name = "slin",
159 .type = AST_MEDIA_TYPE_AUDIO,
160 .sample_rate = 8000,
161 },
162 .format = "slin",
163 .newpvt = codec2_new,
164 .framein = codec2tolin_framein,
165 .destroy = codec2_destroy_stuff,
166 .sample = codec2_sample,
167 .desc_size = sizeof(struct codec2_translator_pvt),
168 .buffer_samples = BUFFER_SAMPLES,
169 .buf_size = BUFFER_SAMPLES * 2,
170};
@ AST_MEDIA_TYPE_AUDIO
Definition codec.h:32
#define BUFFER_SAMPLES
static int codec2_new(struct ast_trans_pvt *pvt)
static void codec2_destroy_stuff(struct ast_trans_pvt *pvt)
static int codec2tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
decode and store in outbuf.
static struct ast_frame * codec2_sample(void)
Definition ex_codec2.h:17

Referenced by load_module(), and unload_module().

◆ lintocodec2

struct ast_translator lintocodec2
static

Definition at line 172 of file codec_codec2.c.

172 {
173 .name = "lintocodec2",
174 .src_codec = {
175 .name = "slin",
176 .type = AST_MEDIA_TYPE_AUDIO,
177 .sample_rate = 8000,
178 },
179 .dst_codec = {
180 .name = "codec2",
181 .type = AST_MEDIA_TYPE_AUDIO,
182 .sample_rate = 8000,
183 },
184 .format = "codec2",
185 .newpvt = codec2_new,
186 .framein = lintocodec2_framein,
187 .frameout = lintocodec2_frameout,
188 .destroy = codec2_destroy_stuff,
189 .sample = slin8_sample,
190 .desc_size = sizeof(struct codec2_translator_pvt),
191 .buffer_samples = BUFFER_SAMPLES,
193};
static struct ast_frame * lintocodec2_frameout(struct ast_trans_pvt *pvt)
encode and produce a frame
static int lintocodec2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
store samples into working buffer for later decode
static struct ast_frame * slin8_sample(void)
Definition slin.h:64

Referenced by load_module(), and unload_module().