📄 codec_g729.c
字号:
/* * Asterisk -- A telephony toolkit for Linux. * * Translate between signed linear and G.729 * * Portions Copyright (C) 1999, Mark Spencer * * Mark Spencer <markster@linux-support.net> * * This program is free software, distributed under the terms of * the GNU General Public License */#include <fcntl.h>#include <stdlib.h>#include <unistd.h>#include <netinet/in.h>#include <string.h>#include <stdio.h>#include "asterisk.h"ASTERISK_FILE_VERSION(__FILE__, "$Revision: 1.3 $")#include <sys/types.h>#include "asterisk/translate.h"#include "asterisk/module.h"#include "asterisk/logger.h"#include "asterisk/channel.h"#if defined(ANNEX_ALL)# include "g729cp/g729cp.h"#elif defined(ANNEX_A) && defined(ANNEX_B)# include "g729abc/g729ab.h"#elif defined(ANNEX_B)# include "g729bc/g729bc.h"#else# error Define the combination of ANNEX_A, ANNEX_B or ANNEX_ALL#endif/* Sample frame data */#include "slin_g729_ex.h"#include "g729_slin_ex.h"AST_MUTEX_DEFINE_STATIC(localuser_lock);static int localusecnt=0;#if defined(ANNEX_ALL)static char *tdesc = "Annex B (floating point) G.729/PCM16 Codec Translator";#elif defined(ANNEX_A) && defined(ANNEX_B)static char *tdesc = "Annex A/B (floating point) G.729/PCM16 Codec Translator";#elif defined(ANNEX_B)static char *tdesc = "Annex B (floating point) G.729/PCM16 Codec Translator";#endifstruct g729_encoder_pvt{ struct cod_state cod; struct ast_frame f; /* Space to build offset */ char offset[AST_FRIENDLY_OFFSET]; /* Buffer for our outgoing frame */ char outbuf[8000]; /* Enough to store a full second */ short buf[8000]; int tail;};struct g729_decoder_pvt{ struct dec_state dec; struct ast_frame f; /* Space to build offset */ char offset[AST_FRIENDLY_OFFSET]; /* Enough to store a full second */ short buf[8000]; int tail;};static struct ast_translator_pvt *g729tolin_new(void){ struct g729_decoder_pvt *tmp; tmp = malloc(sizeof(struct g729_decoder_pvt)); if (tmp) { g729_init_decoder(&tmp->dec); tmp->tail = 0; localusecnt++; ast_update_use_count(); } return (struct ast_translator_pvt *)tmp;}static struct ast_translator_pvt *lintog729_new(void){ struct g729_encoder_pvt *tmp; tmp = malloc(sizeof(struct g729_encoder_pvt)); if (tmp) {#if defined(ANNEX_B) || defined(ANNEX_ALL) g729_init_coder(&tmp->cod, 0); /* for now we cannot enable VAD */#else g729_init_coder(&tmp->cod);#endif localusecnt++; ast_update_use_count(); tmp->tail = 0; } return (struct ast_translator_pvt *)tmp;}static struct ast_frame *g729tolin_sample(void){ static struct ast_frame f; f.frametype = AST_FRAME_VOICE; f.subclass = AST_FORMAT_G729A; f.datalen = sizeof(g729_slin_ex); /* All frames are 10 bytes and ms long */ f.samples = sizeof(g729_slin_ex) / G729_VOICE_FRAME_SIZE * 80; f.mallocd = 0; f.offset = 0; f.src = __PRETTY_FUNCTION__; f.data = g729_slin_ex; return &f;}static struct ast_frame *lintog729_sample(void){ static struct ast_frame f; f.frametype = AST_FRAME_VOICE; f.subclass = AST_FORMAT_SLINEAR; f.datalen = sizeof(slin_g729_ex); /* Assume 8000 Hz */ f.samples = sizeof(slin_g729_ex) / sizeof(short); f.mallocd = 0; f.offset = 0; f.src = __PRETTY_FUNCTION__; f.data = slin_g729_ex; return &f;}static int g729tolin_framein(struct ast_translator_pvt *pvt, struct ast_frame *f){ struct g729_decoder_pvt *tmp = (struct g729_decoder_pvt *)pvt; int len; int chunk; int left; for (len = 0, left = f->datalen; left > 0; left -= chunk, len += chunk) { /* Assuming there's space left, decode into the current buffer at the tail location */ /* buffer cam consist of 0 or more Voice frame + 0 or 1 SID frame */ if (left >= G729_VOICE_FRAME_SIZE) chunk = G729_VOICE_FRAME_SIZE; else if (left == G729_SID_FRAME_SIZE) chunk = G729_SID_FRAME_SIZE; else { ast_log(LOG_WARNING, "Invalid data (%d bytes at the end)\n", left); return -1; } if (tmp->tail + L_FRAME < sizeof(tmp->buf) / 2) { g729_decoder(&tmp->dec, tmp->buf + tmp->tail, f->data + len, chunk); tmp->tail += L_FRAME; } else { ast_log(LOG_WARNING, "Out of buffer space\n"); return -1; } } return 0;}static struct ast_frame *g729tolin_frameout(struct ast_translator_pvt *pvt){ struct g729_decoder_pvt *tmp = (struct g729_decoder_pvt *)pvt; if (!tmp->tail) return NULL; /* Signed linear is no particular frame size, so just send whatever we have in the buffer in one lump sum */ tmp->f.frametype = AST_FRAME_VOICE; tmp->f.subclass = AST_FORMAT_SLINEAR; tmp->f.datalen = tmp->tail * 2; /* Assume 8000 Hz */ tmp->f.samples = tmp->tail; tmp->f.mallocd = 0; tmp->f.offset = AST_FRIENDLY_OFFSET; tmp->f.src = __PRETTY_FUNCTION__; tmp->f.data = tmp->buf; /* Reset tail pointer */ tmp->tail = 0;#if 0 /* Save the frames */ { static int fd2 = -1; if (fd2 == -1) { fd2 = open("g729.example", O_WRONLY | O_CREAT | O_TRUNC, 0644); } write(fd2, tmp->f.data, tmp->f.datalen); } #endif return &tmp->f; }static int lintog729_framein(struct ast_translator_pvt *pvt, struct ast_frame *f){ /* Just add the frames to our stream */ /* XXX We should look at how old the rest of our stream is, and if it is too old, then we should overwrite it entirely, otherwise we can get artifacts of earlier talk that do not belong */ struct g729_encoder_pvt *tmp = (struct g729_encoder_pvt *)pvt; if (tmp->tail + f->datalen/2 < sizeof(tmp->buf) / 2) { memcpy(&tmp->buf[tmp->tail], f->data, f->datalen); tmp->tail += f->datalen/2; } else { ast_log(LOG_WARNING, "Out of buffer space\n"); return -1; } return 0;}static struct ast_frame *lintog729_frameout(struct ast_translator_pvt *pvt){ struct g729_encoder_pvt *tmp = (struct g729_encoder_pvt *)pvt; int cnt; int cbret; /* We can't work on anything less than a frame in size */ if (tmp->tail < L_FRAME) return NULL; tmp->f.frametype = AST_FRAME_VOICE; tmp->f.subclass = AST_FORMAT_G729A; tmp->f.offset = AST_FRIENDLY_OFFSET; tmp->f.src = __PRETTY_FUNCTION__; tmp->f.samples = 0; tmp->f.mallocd = 0; for (cnt = 0; tmp->tail >= L_FRAME; cnt += cbret) { /* Encode a frame of data */ if (cnt + G729_VOICE_FRAME_SIZE > sizeof(tmp->outbuf)) { ast_log(LOG_WARNING, "Out of buffer space\n"); return NULL; } g729_coder(&tmp->cod, tmp->buf, tmp->outbuf + cnt, &cbret); /* Assume 8000 Hz */ tmp->f.samples += L_FRAME; tmp->tail -= L_FRAME; /* Move the data at the end of the buffer to the front */ if (tmp->tail) memmove(tmp->buf, tmp->buf + L_FRAME, tmp->tail * 2); } tmp->f.datalen = cnt; tmp->f.data = tmp->outbuf;#if 0 /* Save to a g729 sample output file... */ { static int fd = -1; int delay = htonl(30); short size; if (fd < 0) fd = open("trans.g729", O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) ast_log(LOG_WARNING, "Unable to create demo\n"); write(fd, &delay, 4); size = htons(tmp->f.datalen); write(fd, &size, 2); write(fd, tmp->f.data, tmp->f.datalen); }#endif return &tmp->f; }static void g729_destroy(struct ast_translator_pvt *pvt){ free(pvt); localusecnt--; ast_update_use_count();}static struct ast_translator g729tolin = { "g729tolin", AST_FORMAT_G729A, AST_FORMAT_SLINEAR, g729tolin_new, g729tolin_framein, g729tolin_frameout, g729_destroy, g729tolin_sample };static struct ast_translator lintog729 = { "lintog729", AST_FORMAT_SLINEAR, AST_FORMAT_G729A, lintog729_new, lintog729_framein, lintog729_frameout, g729_destroy, lintog729_sample };int unload_module(void){ int res; ast_mutex_lock(&localuser_lock); res = ast_unregister_translator(&lintog729); if (!res) res = ast_unregister_translator(&g729tolin); if (localusecnt) res = -1; ast_mutex_unlock(&localuser_lock); return res;}int load_module(void){ int res; res=ast_register_translator(&g729tolin); if (!res) res=ast_register_translator(&lintog729); else ast_unregister_translator(&g729tolin); return res;}char *description(void){ return tdesc;}int usecount(void){ int res; STANDARD_USECOUNT(res); return res;}char *key(void){ return ASTERISK_GPL_KEY;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -