⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codec_g72x.c

📁 G.729 and G.723.1 codecs x86 (and x86_64) Linux and FreeBSD source code for Asterisk open source PBX
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <pthread.h>#include <fcntl.h>#include <stdlib.h>#include <unistd.h>#include <netinet/in.h>#include <string.h>#include <stdio.h>#include <time.h>#if G72X_ASTERISK#include <asterisk.h>#include <asterisk/lock.h>#include <asterisk/translate.h>#include <asterisk/module.h>#include <asterisk/logger.h>#include <asterisk/channel.h>#include <asterisk/utils.h>#include <asterisk/options.h>#include <asterisk/cli.h>#if G72X_ASTERISK < 14#error not supported yet#endif#elif G72X_CALLWEAVER#error not supported yet#include "confdefs.h"#include <callweaver/lock.h>#include <callweaver/translate.h>#include <callweaver/module.h>#include <callweaver/logger.h>#include <callweaver/channel.h>#include <callweaver/utils.h>#include <callweaver/options.h>#include <callweaver/cli.h>#define ast_frame          opbx_frame#define ast_log            opbx_log#define ast_translator     opbx_translator#define ast_mutex_lock     opbx_mutex_lock#define ast_verbose        opbx_verbose#define ast_trans_frameout        opbx_trans_frameout#define ast_register_translator   opbx_register_translator#define ast_unregister_translator opbx_unregister_translator#define AST_FRAME_VOICE     OPBX_FRAME_VOICE#define AST_FORMAT_SLINEAR  OPBX_FORMAT_SLINEAR#define AST_FORMAT_G729A    OPBX_FORMAT_G729A#define AST_FORMAT_G723_1   OPBX_FORMAT_G723_1#define AST_FRIENDLY_OFFSET OPBX_FRIENDLY_OFFSET#else#error either G72X_ASTERISK or G72X_CALLWEAVER must be defined#endif#include <ippcore.h>#include <ipps.h>#if G72X_9#define G72X_CODEC "g729"#if G72X_9_FP#include "g729fpapi.h"#define apiG729Encoder_InitBuff apiG729FPEncoder_InitBuff#define apiG729Decoder_InitBuff apiG729FPDecoder_InitBuff#define apiG729Encoder_Init     apiG729FPEncoder_Init#define apiG729Decoder_Init     apiG729FPDecoder_Init#define apiG729Encode           apiG729FPEncode#define apiG729Decode           apiG729FPDecode#define apiG729Encoder_Alloc    apiG729FPEncoder_Alloc#define apiG729Decoder_Alloc    apiG729FPDecoder_Alloc#define apiG729Codec_ScratchMemoryAlloc apiG729FPCodec_ScratchMemoryAlloc#else#include "g729api.h"#endif#include "slin_g72x_ex.h"#include "g729_slin_ex.h"#define SLIN_FRAME_LEN  160#define G729_FRAME_LEN  10#define G729_SAMPLES    80 /* 10ms at 8000 hz, 160 bytes signed linear */#define BUFFER_SAMPLES  8000#define G72X_FRAME_LEN  G729_FRAME_LEN#define G72X_SAMPLES    G729_SAMPLES#define G72X_AST_FORMAT AST_FORMAT_G729A#elif G72X_3#define G72X_CODEC "g723"#include "g723api.h"#include "slin_g72x_ex.h"#include "g723_slin_ex.h"#define SLIN_FRAME_LEN  480#define G723_FRAME_LEN  24 /* maximum frame length */#define G723_SAMPLES    240 /* 30ms at 8000 hz, 480 bytes signed linear */#define BUFFER_SAMPLES  8000#define G72X_FRAME_LEN  G723_FRAME_LEN#define G72X_SAMPLES    G723_SAMPLES#define G72X_AST_FORMAT AST_FORMAT_G723_1#define G723_RATE_63 0 /* G723_Rate63 in owng723.h */#define G723_RATE_53 1 /* G723_Rate53 */#define G723_DEFAULT_SEND_RATE G723_RATE_63static int g723_sendrate = G723_DEFAULT_SEND_RATE;#else#error either G72X_9 or G72X_3 must be defined#endif#define AST_MODULE "codec_" G72X_CODEC#define G72X_DESC G72X_CODEC " Coder/Decoder, based on IPP"struct g72x_coder_pvt {    void *coder;    void *scratch_mem;    int16_t buf[BUFFER_SAMPLES]; /* 1 second */};static int encoder_size;static int decoder_size;static int coder_size_scratch;/* debug array to collect information about incoming frame sizes *//* the code is not aiming at correctness so there are no locking and no atomic operations */static int *frame_sizes = NULL;#define DEBUG_MAX_FRAME_SIZE 2000#define DEBUG_FRAME_SIZE_INC \    do { \    if (frame_sizes != NULL) { \        if (f->datalen >= DEBUG_MAX_FRAME_SIZE) \            ++frame_sizes[DEBUG_MAX_FRAME_SIZE]; \        else \            ++frame_sizes[f->datalen]; \        } \    } while (0)static int lintog72x_new(struct ast_trans_pvt *pvt){    struct g72x_coder_pvt *state = pvt->pvt;#ifndef IPPCORE_NO_SSE    ippSetFlushToZero(1, NULL); /* is FZM flag per-thread or not? does it matter at all? */#endif    state->coder = ippsMalloc_8u(encoder_size);    state->scratch_mem = ippsMalloc_8u(coder_size_scratch);#if G72X_9    apiG729Encoder_InitBuff(state->coder, state->scratch_mem);    apiG729Encoder_Init(state->coder, G729A_CODEC, G729Encode_VAD_Disabled);#else    apiG723Encoder_InitBuff(state->coder, state->scratch_mem);    apiG723Encoder_Init(state->coder, G723Encode_DefaultMode);#endif    return 0;}static int g72xtolin_new(struct ast_trans_pvt *pvt){    struct g72x_coder_pvt *state = pvt->pvt;#ifndef IPPCORE_NO_SSE    ippSetFlushToZero(1, NULL);#endif    state->coder = ippsMalloc_8u(decoder_size);    state->scratch_mem = ippsMalloc_8u(coder_size_scratch);#if G72X_9    apiG729Decoder_InitBuff(state->coder, state->scratch_mem);    apiG729Decoder_Init(state->coder, G729A_CODEC);#else    apiG723Decoder_InitBuff(state->coder, state->scratch_mem);    apiG723Decoder_Init(state->coder, G723Decode_DefaultMode);#endif    return 0;}static struct ast_frame *lintog72x_sample(void){    static struct ast_frame f;    f.frametype = AST_FRAME_VOICE;    f.subclass = AST_FORMAT_SLINEAR;    f.datalen = sizeof(slin_g72x_ex);    f.samples = sizeof(slin_g72x_ex)/2;    f.mallocd = 0;    f.offset = 0;    f.src = __PRETTY_FUNCTION__;    f.data = slin_g72x_ex;    return &f;}static struct ast_frame *g72xtolin_sample(void){    static struct ast_frame f;    f.frametype = AST_FRAME_VOICE;    f.subclass = G72X_AST_FORMAT;    f.datalen = sizeof(g72x_slin_ex);    f.samples = G72X_SAMPLES;    f.mallocd = 0;    f.offset = 0;    f.src = __PRETTY_FUNCTION__;    f.data = g72x_slin_ex;    return &f;}/* XXX taken from IPP sample, why it is 160/480 bytes in size? should be the size of compressed frame? http://softwarecommunity.intel.com/isn/Community/en-US/forums/thread/30244903.aspx */static int16_t lost_frame[G72X_SAMPLES] = { 0 };/* static unsigned char lost_frame[G72X_FRAME_LEN] = { 0 }; */#if G72X_9static int g729_frame_type(int datalen){    switch (datalen) {        case 0: return -1;  /* erased */     /* case 0: return 0; maybe it should be 0 - untransmitted silence? */        case 2: return 1;  /* SID */        case 8: return 2;  /* 729d */        case 10: return 3; /* 729, 729a */        case 15: return 4; /* 729e */    }    return 0;}static int g72xtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f){    struct g72x_coder_pvt *state = pvt->pvt;    int16_t *dst = (int16_t *)pvt->outbuf;    int framesize;    int x;    DEBUG_FRAME_SIZE_INC;    if (f->datalen == 0) {  /* Native PLC interpolation */        if (option_verbose > 2)            ast_verbose(VERBOSE_PREFIX_3 "G.729 PLC\n");        if (pvt->samples + G729_SAMPLES > BUFFER_SAMPLES) {            ast_log(LOG_WARNING, "Out of buffer space\n");            return -1;        }        apiG729Decode(state->coder, (unsigned char *)lost_frame, g729_frame_type(0), dst + pvt->samples);        pvt->samples += G729_SAMPLES;        pvt->datalen += 2 * G729_SAMPLES; /* 2 bytes/sample */        return 0;    }    for(x = 0; x < f->datalen; x += framesize) {        if (pvt->samples + G729_SAMPLES > BUFFER_SAMPLES) { /* XXX how the hell this BUFFER_SAMPLES check is supposed to catch memory overruns? use buf_size */            ast_log(LOG_WARNING, "Out of buffer space\n");            return -1;        }        if(f->datalen - x < 8)            framesize = 2;  /* SID */        else            framesize = 10; /* regular 729a frame */        apiG729Decode(state->coder, (unsigned char *)f->data + x, g729_frame_type(framesize), dst + pvt->samples);        pvt->samples += G729_SAMPLES;        pvt->datalen += 2*G729_SAMPLES;    }    return 0;}#else /* G72X_3 */static int g723_frame_length(int frametype){    switch(frametype) {        case 0: return 24; /* 6.3kbps */        case 1: return 20; /* 5.3kbps */        case 2: return 4;  /* SID */    }    return 1; /* XXX untransmitted */}static int g72xtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f){    struct g72x_coder_pvt *state = pvt->pvt;    int16_t *dst = (int16_t *)pvt->outbuf;    int badframe;    int frametype;    int framesize;    int x;    DEBUG_FRAME_SIZE_INC;    if (f->datalen == 0) {  /* Native PLC interpolation */        if (option_verbose > 2)            ast_verbose(VERBOSE_PREFIX_3 "G.723.1 PLC\n");        if (pvt->samples + G723_SAMPLES > BUFFER_SAMPLES) {            ast_log(LOG_WARNING, "Out of buffer space\n");            return -1;        }        badframe = 1; /* the frame is lost */        apiG723Decode(state->coder, (void *)lost_frame, badframe, dst + pvt->samples);        pvt->samples += G723_SAMPLES;        pvt->datalen += 2 * G723_SAMPLES; /* 2 bytes/sample */        return 0;    }    badframe = 0;    for(x = 0; x < f->datalen; x += framesize) {        if (pvt->samples + G723_SAMPLES > BUFFER_SAMPLES) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -