diff options
author | Benny Prijono <bennylp@teluu.com> | 2008-03-17 14:07:53 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2008-03-17 14:07:53 +0000 |
commit | e14e1fddbedf8c03df686ecd7c7af0c0ca0f52c6 (patch) | |
tree | 8ae1ae5d648b62b7fe49cd738e1103e91ac5b310 /pjmedia/src | |
parent | 685aef52bcf830cb591798cd0814dd4a10605497 (diff) |
Ticket #507: committed and tested g722 patch on Windows
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1870 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjmedia/src')
-rw-r--r-- | pjmedia/src/pjmedia-codec/g722.c | 664 | ||||
-rw-r--r-- | pjmedia/src/pjmedia-codec/g722/g722_dec.c | 548 | ||||
-rw-r--r-- | pjmedia/src/pjmedia-codec/g722/g722_dec.h | 78 | ||||
-rw-r--r-- | pjmedia/src/pjmedia-codec/g722/g722_enc.c | 575 | ||||
-rw-r--r-- | pjmedia/src/pjmedia-codec/g722/g722_enc.h | 77 |
5 files changed, 1942 insertions, 0 deletions
diff --git a/pjmedia/src/pjmedia-codec/g722.c b/pjmedia/src/pjmedia-codec/g722.c new file mode 100644 index 00000000..ece73d87 --- /dev/null +++ b/pjmedia/src/pjmedia-codec/g722.c @@ -0,0 +1,664 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include <pjmedia-codec/g722.h> +#include <pjmedia/codec.h> +#include <pjmedia/errno.h> +#include <pjmedia/endpoint.h> +#include <pjmedia/plc.h> +#include <pjmedia/port.h> +#include <pjmedia/silencedet.h> +#include <pj/assert.h> +#include <pj/log.h> +#include <pj/pool.h> +#include <pj/string.h> +#include <pj/os.h> + +#if defined(PJMEDIA_HAS_G722_CODEC) && (PJMEDIA_HAS_G722_CODEC != 0) + +#include "g722/g722_enc.h" +#include "g722/g722_dec.h" + +#define THIS_FILE "g722.c" + +/* Defines */ +#define PTIME (20) +#define SAMPLES_PER_FRAME (16000 * PTIME /1000) +#define FRAME_LEN (160) +#define PLC_DISABLED 1 + +/* Tracing */ +#ifndef PJ_TRACE +# define PJ_TRACE 0 +#endif + +#if PJ_TRACE +# define TRACE_(expr) PJ_LOG(4,expr) +#else +# define TRACE_(expr) +#endif + + +/* Prototypes for G722 factory */ +static pj_status_t g722_test_alloc(pjmedia_codec_factory *factory, + const pjmedia_codec_info *id ); +static pj_status_t g722_default_attr(pjmedia_codec_factory *factory, + const pjmedia_codec_info *id, + pjmedia_codec_param *attr ); +static pj_status_t g722_enum_codecs(pjmedia_codec_factory *factory, + unsigned *count, + pjmedia_codec_info codecs[]); +static pj_status_t g722_alloc_codec(pjmedia_codec_factory *factory, + const pjmedia_codec_info *id, + pjmedia_codec **p_codec); +static pj_status_t g722_dealloc_codec(pjmedia_codec_factory *factory, + pjmedia_codec *codec ); + +/* Prototypes for G722 implementation. */ +static pj_status_t g722_codec_init(pjmedia_codec *codec, + pj_pool_t *pool ); +static pj_status_t g722_codec_open(pjmedia_codec *codec, + pjmedia_codec_param *attr ); +static pj_status_t g722_codec_close(pjmedia_codec *codec ); +static pj_status_t g722_codec_modify(pjmedia_codec *codec, + const pjmedia_codec_param *attr ); +static pj_status_t g722_codec_parse(pjmedia_codec *codec, + void *pkt, + pj_size_t pkt_size, + const pj_timestamp *ts, + unsigned *frame_cnt, + pjmedia_frame frames[]); +static pj_status_t g722_codec_encode(pjmedia_codec *codec, + const struct pjmedia_frame *input, + unsigned output_buf_len, + struct pjmedia_frame *output); +static pj_status_t g722_codec_decode(pjmedia_codec *codec, + const struct pjmedia_frame *input, + unsigned output_buf_len, + struct pjmedia_frame *output); +#if !PLC_DISABLED +static pj_status_t g722_codec_recover(pjmedia_codec *codec, + unsigned output_buf_len, + struct pjmedia_frame *output); +#endif + +/* Definition for G722 codec operations. */ +static pjmedia_codec_op g722_op = +{ + &g722_codec_init, + &g722_codec_open, + &g722_codec_close, + &g722_codec_modify, + &g722_codec_parse, + &g722_codec_encode, + &g722_codec_decode, +#if !PLC_DISABLED + &g722_codec_recover +#else + NULL +#endif +}; + +/* Definition for G722 codec factory operations. */ +static pjmedia_codec_factory_op g722_factory_op = +{ + &g722_test_alloc, + &g722_default_attr, + &g722_enum_codecs, + &g722_alloc_codec, + &g722_dealloc_codec +}; + +/* G722 factory */ +static struct g722_codec_factory +{ + pjmedia_codec_factory base; + pjmedia_endpt *endpt; + pj_pool_t *pool; + pj_mutex_t *mutex; + pjmedia_codec codec_list; +} g722_codec_factory; + + +/* G722 codec private data. */ +struct g722_data +{ + g722_enc_t encoder; + g722_dec_t decoder; + pj_bool_t plc_enabled; + pj_bool_t vad_enabled; + pjmedia_silence_det *vad; + pj_timestamp last_tx; +#if !PLC_DISABLED + pjmedia_plc *plc; +#endif +}; + + + +/* + * Initialize and register G722 codec factory to pjmedia endpoint. + */ +PJ_DEF(pj_status_t) pjmedia_codec_g722_init( pjmedia_endpt *endpt ) +{ + pjmedia_codec_mgr *codec_mgr; + pj_status_t status; + + if (g722_codec_factory.pool != NULL) + return PJ_SUCCESS; + + /* Create G722 codec factory. */ + g722_codec_factory.base.op = &g722_factory_op; + g722_codec_factory.base.factory_data = NULL; + g722_codec_factory.endpt = endpt; + + g722_codec_factory.pool = pjmedia_endpt_create_pool(endpt, "g722", 1000, + 1000); + if (!g722_codec_factory.pool) + return PJ_ENOMEM; + + pj_list_init(&g722_codec_factory.codec_list); + + /* Create mutex. */ + status = pj_mutex_create_simple(g722_codec_factory.pool, "g722", + &g722_codec_factory.mutex); + if (status != PJ_SUCCESS) + goto on_error; + + /* Get the codec manager. */ + codec_mgr = pjmedia_endpt_get_codec_mgr(endpt); + if (!codec_mgr) { + status = PJ_EINVALIDOP; + goto on_error; + } + + /* Register codec factory to endpoint. */ + status = pjmedia_codec_mgr_register_factory(codec_mgr, + &g722_codec_factory.base); + if (status != PJ_SUCCESS) + goto on_error; + + TRACE_((THIS_FILE, "G722 codec factory initialized")); + + /* Done. */ + return PJ_SUCCESS; + +on_error: + pj_pool_release(g722_codec_factory.pool); + g722_codec_factory.pool = NULL; + return status; +} + +/* + * Unregister G722 codec factory from pjmedia endpoint and deinitialize + * the G722 codec library. + */ +PJ_DEF(pj_status_t) pjmedia_codec_g722_deinit(void) +{ + pjmedia_codec_mgr *codec_mgr; + pj_status_t status; + + if (g722_codec_factory.pool == NULL) + return PJ_SUCCESS; + + /* Get the codec manager. */ + codec_mgr = pjmedia_endpt_get_codec_mgr(g722_codec_factory.endpt); + if (!codec_mgr) { + pj_pool_release(g722_codec_factory.pool); + g722_codec_factory.pool = NULL; + return PJ_EINVALIDOP; + } + + /* Unregister G722 codec factory. */ + status = pjmedia_codec_mgr_unregister_factory(codec_mgr, + &g722_codec_factory.base); + + /* Destroy mutex. */ + pj_mutex_destroy(g722_codec_factory.mutex); + + /* Destroy pool. */ + pj_pool_release(g722_codec_factory.pool); + g722_codec_factory.pool = NULL; + + TRACE_((THIS_FILE, "G722 codec factory shutdown")); + return status; +} + +/* + * Check if factory can allocate the specified codec. + */ +static pj_status_t g722_test_alloc(pjmedia_codec_factory *factory, + const pjmedia_codec_info *info ) +{ + PJ_UNUSED_ARG(factory); + + /* Check payload type. */ + if (info->pt != PJMEDIA_RTP_PT_G722) + return PJMEDIA_CODEC_EUNSUP; + + /* Ignore the rest, since it's static payload type. */ + + return PJ_SUCCESS; +} + +/* + * Generate default attribute. + */ +static pj_status_t g722_default_attr( pjmedia_codec_factory *factory, + const pjmedia_codec_info *id, + pjmedia_codec_param *attr ) +{ + PJ_UNUSED_ARG(factory); + PJ_UNUSED_ARG(id); + + pj_bzero(attr, sizeof(pjmedia_codec_param)); + attr->info.clock_rate = 16000; + attr->info.channel_cnt = 1; + attr->info.avg_bps = 64000; + attr->info.pcm_bits_per_sample = 16; + attr->info.frm_ptime = PTIME; + attr->info.pt = PJMEDIA_RTP_PT_G722; + + attr->setting.frm_per_pkt = 1; + attr->setting.vad = 1; + attr->setting.plc = 0; + + /* Default all other flag bits disabled. */ + + return PJ_SUCCESS; +} + +/* + * Enum codecs supported by this factory (i.e. only G722!). + */ +static pj_status_t g722_enum_codecs(pjmedia_codec_factory *factory, + unsigned *count, + pjmedia_codec_info codecs[]) +{ + PJ_UNUSED_ARG(factory); + PJ_ASSERT_RETURN(codecs && *count > 0, PJ_EINVAL); + + pj_bzero(&codecs[0], sizeof(pjmedia_codec_info)); + codecs[0].encoding_name = pj_str("G722"); + codecs[0].pt = PJMEDIA_RTP_PT_G722; + codecs[0].type = PJMEDIA_TYPE_AUDIO; + codecs[0].clock_rate = 16000; + codecs[0].channel_cnt = 1; + + *count = 1; + + return PJ_SUCCESS; +} + +/* + * Allocate a new G722 codec instance. + */ +static pj_status_t g722_alloc_codec(pjmedia_codec_factory *factory, + const pjmedia_codec_info *id, + pjmedia_codec **p_codec) +{ + pjmedia_codec *codec; + struct g722_data *g722_data; + + PJ_ASSERT_RETURN(factory && id && p_codec, PJ_EINVAL); + PJ_ASSERT_RETURN(factory == &g722_codec_factory.base, PJ_EINVAL); + + pj_mutex_lock(g722_codec_factory.mutex); + + /* Get free nodes, if any. */ + if (!pj_list_empty(&g722_codec_factory.codec_list)) { + codec = g722_codec_factory.codec_list.next; + pj_list_erase(codec); + } else { + pj_status_t status; + + codec = PJ_POOL_ZALLOC_T(g722_codec_factory.pool, pjmedia_codec); + PJ_ASSERT_RETURN(codec != NULL, PJ_ENOMEM); + codec->op = &g722_op; + codec->factory = factory; + + g722_data = PJ_POOL_ZALLOC_T(g722_codec_factory.pool, struct g722_data); + codec->codec_data = g722_data; + +#if !PLC_DISABLED + /* Create PLC */ + status = pjmedia_plc_create(g722_codec_factory.pool, 16000, + SAMPLES_PER_FRAME, 0, &g722_data->plc); + if (status != PJ_SUCCESS) { + pj_mutex_unlock(g722_codec_factory.mutex); + return status; + } +#endif + + /* Create silence detector */ + status = pjmedia_silence_det_create(g722_codec_factory.pool, + 16000, SAMPLES_PER_FRAME, + &g722_data->vad); + if (status != PJ_SUCCESS) { + pj_mutex_unlock(g722_codec_factory.mutex); + TRACE_((THIS_FILE, "Create silence detector failed (status = %d)", + status)); + return status; + } + } + + + pj_mutex_unlock(g722_codec_factory.mutex); + + *p_codec = codec; + return PJ_SUCCESS; +} + +/* + * Free codec. + */ +static pj_status_t g722_dealloc_codec(pjmedia_codec_factory *factory, + pjmedia_codec *codec ) +{ + struct g722_data *g722_data; + int i; + + PJ_ASSERT_RETURN(factory && codec, PJ_EINVAL); + PJ_ASSERT_RETURN(factory == &g722_codec_factory.base, PJ_EINVAL); + + g722_data = (struct g722_data*) codec->codec_data; + + /* Close codec, if it's not closed. */ + g722_codec_close(codec); + +#if !PLC_DISABLED + /* Clear left samples in the PLC, since codec+plc will be reused + * next time. + */ + for (i=0; i<2; ++i) { + pj_int16_t frame[SAMPLES_PER_FRAME]; + pjmedia_zero_samples(frame, PJ_ARRAY_SIZE(frame)); + pjmedia_plc_save(g722_data->plc, frame); + } +#else + PJ_UNUSED_ARG(i); +#endif + + /* Re-init silence_period */ + pj_set_timestamp32(&g722_data->last_tx, 0, 0); + + /* Put in the free list. */ + pj_mutex_lock(g722_codec_factory.mutex); + pj_list_push_front(&g722_codec_factory.codec_list, codec); + pj_mutex_unlock(g722_codec_factory.mutex); + + return PJ_SUCCESS; +} + +/* + * Init codec. + */ +static pj_status_t g722_codec_init(pjmedia_codec *codec, + pj_pool_t *pool ) +{ + PJ_UNUSED_ARG(codec); + PJ_UNUSED_ARG(pool); + return PJ_SUCCESS; +} + +/* + * Open codec. + */ +static pj_status_t g722_codec_open(pjmedia_codec *codec, + pjmedia_codec_param *attr ) +{ + struct g722_data *g722_data = (struct g722_data*) codec->codec_data; + pj_status_t status; + + PJ_ASSERT_RETURN(codec && attr, PJ_EINVAL); + PJ_ASSERT_RETURN(g722_data != NULL, PJ_EINVALIDOP); + + status = g722_enc_init(&g722_data->encoder); + if (status != PJ_SUCCESS) { + TRACE_((THIS_FILE, "g722_enc_init() failed, status=%d", status)); + pj_mutex_unlock(g722_codec_factory.mutex); + return PJMEDIA_CODEC_EFAILED; + } + + status = g722_dec_init(&g722_data->decoder); + if (status != PJ_SUCCESS) { + TRACE_((THIS_FILE, "g722_dec_init() failed, status=%d", status)); + pj_mutex_unlock(g722_codec_factory.mutex); + return PJMEDIA_CODEC_EFAILED; + } + + g722_data->vad_enabled = (attr->setting.vad != 0); + g722_data->plc_enabled = (attr->setting.plc != 0); + + TRACE_((THIS_FILE, "G722 codec opened: vad=%d, plc=%d", + g722_data->vad_enabled, g722_data->plc_enabled)); + return PJ_SUCCESS; +} + +/* + * Close codec. + */ +static pj_status_t g722_codec_close( pjmedia_codec *codec ) +{ + /* The codec, encoder, and decoder will be reused, so there's + * nothing to do here + */ + + PJ_UNUSED_ARG(codec); + + TRACE_((THIS_FILE, "G722 codec closed")); + return PJ_SUCCESS; +} + + +/* + * Modify codec settings. + */ +static pj_status_t g722_codec_modify(pjmedia_codec *codec, + const pjmedia_codec_param *attr ) +{ + struct g722_data *g722_data = (struct g722_data*) codec->codec_data; + + pj_assert(g722_data != NULL); + + g722_data->vad_enabled = (attr->setting.vad != 0); + g722_data->plc_enabled = (attr->setting.plc != 0); + + TRACE_((THIS_FILE, "G722 codec modified: vad=%d, plc=%d", + g722_data->vad_enabled, g722_data->plc_enabled)); + return PJ_SUCCESS; +} + + +/* + * Get frames in the packet. + */ +static pj_status_t g722_codec_parse(pjmedia_codec *codec, + void *pkt, + pj_size_t pkt_size, + const pj_timestamp *ts, + unsigned *frame_cnt, + pjmedia_frame frames[]) +{ + unsigned count = 0; + + PJ_UNUSED_ARG(codec); + + PJ_ASSERT_RETURN(frame_cnt, PJ_EINVAL); + + TRACE_((THIS_FILE, "G722 parse(): input len=%d", pkt_size)); + + while (pkt_size >= FRAME_LEN && count < *frame_cnt) { + frames[count].type = PJMEDIA_FRAME_TYPE_AUDIO; + frames[count].buf = pkt; + frames[count].size = FRAME_LEN; + frames[count].timestamp.u64 = ts->u64 + count * SAMPLES_PER_FRAME; + + pkt = ((char*)pkt) + FRAME_LEN; + pkt_size -= FRAME_LEN; + + ++count; + } + + TRACE_((THIS_FILE, "G722 parse(): got %d frames", count)); + + *frame_cnt = count; + return PJ_SUCCESS; +} + +/* + * Encode frame. + */ +static pj_status_t g722_codec_encode(pjmedia_codec *codec, + const struct pjmedia_frame *input, + unsigned output_buf_len, + struct pjmedia_frame *output) +{ + struct g722_data *g722_data = (struct g722_data*) codec->codec_data; + pj_status_t status; + + pj_assert(g722_data != NULL); + PJ_ASSERT_RETURN(input && output, PJ_EINVAL); + + if (output_buf_len < FRAME_LEN) + return PJMEDIA_CODEC_EFRMTOOSHORT; + + PJ_ASSERT_RETURN(input->size/2 == SAMPLES_PER_FRAME, + PJMEDIA_CODEC_EPCMFRMINLEN); + + /* Detect silence */ + if (g722_data->vad_enabled) { + pj_bool_t is_silence; + pj_int32_t silence_duration; + + silence_duration = pj_timestamp_diff32(&g722_data->last_tx, + &input->timestamp); + + is_silence = pjmedia_silence_det_detect(g722_data->vad, + (const pj_int16_t*) input->buf, + (input->size >> 1), + NULL); + if (is_silence && + PJMEDIA_CODEC_MAX_SILENCE_PERIOD != -1 && + silence_duration < PJMEDIA_CODEC_MAX_SILENCE_PERIOD) + { + output->type = PJMEDIA_FRAME_TYPE_NONE; + output->buf = NULL; + output->size = 0; + output->timestamp = input->timestamp; + return PJ_SUCCESS; + } else { + g722_data->last_tx = input->timestamp; + } + } + + /* Encode to temporary buffer */ + status = g722_enc_encode(&g722_data->encoder, (pj_int16_t*)input->buf, + SAMPLES_PER_FRAME, output->buf, &output->size); + if (status != PJ_SUCCESS) { + output->size = 0; + output->buf = NULL; + output->type = PJMEDIA_FRAME_TYPE_NONE; + TRACE_((THIS_FILE, "G722 encode() status: %d", status)); + return PJMEDIA_CODEC_EFAILED; + } + + output->type = PJMEDIA_FRAME_TYPE_AUDIO; + + TRACE_((THIS_FILE, "G722 encode(): size=%d", output->size)); + return PJ_SUCCESS; +} + +/* + * Decode frame. + */ +static pj_status_t g722_codec_decode(pjmedia_codec *codec, + const struct pjmedia_frame *input, + unsigned output_buf_len, + struct pjmedia_frame *output) +{ + struct g722_data *g722_data = (struct g722_data*) codec->codec_data; + pj_status_t status; + + pj_assert(g722_data != NULL); + PJ_ASSERT_RETURN(input && output, PJ_EINVAL); + + TRACE_((THIS_FILE, "G722 decode(): inbuf=%p, insize=%d, outbuf=%p," + "outsize=%d", + input->buf, input->size, output->buf, output_buf_len)); + + if (output_buf_len < SAMPLES_PER_FRAME * 2) { + TRACE_((THIS_FILE, "G722 decode() ERROR: PJMEDIA_CODEC_EPCMTOOSHORT")); + return PJMEDIA_CODEC_EPCMTOOSHORT; + } + + if (input->size != FRAME_LEN) { + TRACE_((THIS_FILE, "G722 decode() ERROR: PJMEDIA_CODEC_EFRMTOOSHORT")); + return PJMEDIA_CODEC_EFRMTOOSHORT; + } + + + /* Decode */ + output->size = SAMPLES_PER_FRAME; + status = g722_dec_decode(&g722_data->decoder, input->buf, input->size, + (pj_int16_t*)output->buf, &output->size); + if (status != PJ_SUCCESS) { + TRACE_((THIS_FILE, "G722 decode() status: %d", status)); + return PJMEDIA_CODEC_EFAILED; + } + + pj_assert(output->size == SAMPLES_PER_FRAME); + output->size = SAMPLES_PER_FRAME * 2; + output->type = PJMEDIA_FRAME_TYPE_AUDIO; + +#if !PLC_DISABLED + if (g722_data->plc_enabled) + pjmedia_plc_save(g722_data->plc, output->buf); +#endif + + TRACE_((THIS_FILE, "G722 decode done")); + return PJ_SUCCESS; +} + + +#if !PLC_DISABLED +/* + * Recover lost frame. + */ +static pj_status_t g722_codec_recover(pjmedia_codec *codec, + unsigned output_buf_len, + struct pjmedia_frame *output) +{ + struct g722_data *g722_data = codec->codec_data; + + PJ_ASSERT_RETURN(g722_data->plc_enabled, PJ_EINVALIDOP); + + PJ_ASSERT_RETURN(output_buf_len >= SAMPLES_PER_FRAME * 2, + PJMEDIA_CODEC_EPCMTOOSHORT); + + pjmedia_plc_generate(g722_data->plc, output->buf); + + output->size = SAMPLES_PER_FRAME * 2; + output->type = PJMEDIA_FRAME_TYPE_AUDIO; + + return PJ_SUCCESS; +} +#endif + +#endif // PJMEDIA_HAS_G722_CODEC + diff --git a/pjmedia/src/pjmedia-codec/g722/g722_dec.c b/pjmedia/src/pjmedia-codec/g722/g722_dec.c new file mode 100644 index 00000000..bb7e37d1 --- /dev/null +++ b/pjmedia/src/pjmedia-codec/g722/g722_dec.c @@ -0,0 +1,548 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* + * Based on implementation found in Carnegie Mellon Speech Group Software + * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright + * was claimed in the original source codes. + */ +#include <pjmedia/errno.h> +#include <pj/assert.h> +#include <pj/pool.h> + +#include "g722_dec.h" + +#if defined(PJMEDIA_HAS_G722_CODEC) && (PJMEDIA_HAS_G722_CODEC != 0) + +#define MODE 1 + +#define SATURATE(v, max, min) \ + if (v>max) v = max; \ + else if (v<min) v = min + +extern const int g722_qmf_coeff[24]; + +static const int qm4[16] = +{ + 0, -20456, -12896, -8968, + -6288, -4240, -2584, -1200, + 20456, 12896, 8968, 6288, + 4240, 2584, 1200, 0 +}; +static const int ilb[32] = { + 2048, 2093, 2139, 2186, 2233, 2282, 2332, + 2383, 2435, 2489, 2543, 2599, 2656, 2714, + 2774, 2834, 2896, 2960, 3025, 3091, 3158, + 3228, 3298, 3371, 3444, 3520, 3597, 3676, + 3756, 3838, 3922, 4008 +}; + + +static int block2l (int il, int detl) +{ + int dlt ; + int ril, wd2 ; + + /* INVQAL */ + ril = il >> 2 ; + wd2 = qm4[ril] ; + dlt = (detl * wd2) >> 15 ; + + return (dlt) ; +} + + +static int block3l (g722_dec_t *dec, int il) +{ + int detl ; + int ril, il4, wd, wd1, wd2, wd3, nbpl, depl ; + static const int wl[8] = { + -60, -30, 58, 172, 334, 538, 1198, 3042 + }; + static const int rl42[16] = { + 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0 + }; + + /* LOGSCL */ + ril = il >> 2 ; + il4 = rl42[ril] ; + wd = (dec->nbl * 32512) >> 15 ; + nbpl = wd + wl[il4] ; + + if (nbpl < 0) nbpl = 0 ; + if (nbpl > 18432) nbpl = 18432 ; + + /* SCALEL */ + wd1 = (nbpl >> 6) & 31 ; + wd2 = nbpl >> 11 ; + if ((8 - wd2) < 0) wd3 = ilb[wd1] << (wd2 - 8) ; + else wd3 = ilb[wd1] >> (8 - wd2) ; + depl = wd3 << 2 ; + + /* DELAYA */ + dec->nbl = nbpl ; + /* DELAYL */ + detl = depl ; + + return (detl) ; +} + + +static int block4l (g722_dec_t *dec, int dl) +{ + int sl = dec->slow ; + int i ; + int wd, wd1, wd2, wd3, wd4, wd5/*, wd6 */; + + dec->dlt[0] = dl; + + /* RECONS */ + dec->rlt[0] = sl + dec->dlt[0] ; + SATURATE(dec->rlt[0], 32767, -32768); + + /* PARREC */ + dec->plt[0] = dec->dlt[0] + dec->szl ; + SATURATE(dec->plt[0], 32767, -32768); + + /* UPPOL2 */ + dec->sgl[0] = dec->plt[0] >> 15 ; + dec->sgl[1] = dec->plt[1] >> 15 ; + dec->sgl[2] = dec->plt[2] >> 15 ; + + wd1 = dec->al[1] << 2; + SATURATE(wd1, 32767, -32768); + + if ( dec->sgl[0] == dec->sgl[1] ) wd2 = - wd1 ; + else wd2 = wd1 ; + if (wd2 > 32767) wd2 = 32767; + wd2 = wd2 >> 7 ; + + if ( dec->sgl[0] == dec->sgl[2] ) wd3 = 128 ; + else wd3 = - 128 ; + + wd4 = wd2 + wd3 ; + wd5 = (dec->al[2] * 32512) >> 15 ; + + dec->apl[2] = wd4 + wd5 ; + SATURATE(dec->apl[2], 12288, -12288); + + /* UPPOL1 */ + dec->sgl[0] = dec->plt[0] >> 15 ; + dec->sgl[1] = dec->plt[1] >> 15 ; + + if ( dec->sgl[0] == dec->sgl[1] ) wd1 = 192 ; + else wd1 = - 192 ; + + wd2 = (dec->al[1] * 32640) >> 15 ; + + dec->apl[1] = wd1 + wd2 ; + SATURATE(dec->apl[1], 32767, -32768); + + wd3 = (15360 - dec->apl[2]) ; + SATURATE(wd3, 32767, -32768); + if ( dec->apl[1] > wd3) dec->apl[1] = wd3 ; + if ( dec->apl[1] < -wd3) dec->apl[1] = -wd3 ; + + /* UPZERO */ + if ( dec->dlt[0] == 0 ) wd1 = 0 ; + else wd1 = 128 ; + + dec->sgl[0] = dec->dlt[0] >> 15 ; + + for ( i = 1; i < 7; i++ ) { + dec->sgl[i] = dec->dlt[i] >> 15 ; + if ( dec->sgl[i] == dec->sgl[0] ) wd2 = wd1 ; + else wd2 = - wd1 ; + wd3 = (dec->bl[i] * 32640) >> 15 ; + dec->bpl[i] = wd2 + wd3 ; + SATURATE(dec->bpl[i], 32767, -32768); + } + + /* DELAYA */ + for ( i = 6; i > 0; i-- ) { + dec->dlt[i] = dec->dlt[i-1] ; + dec->bl[i] = dec->bpl[i] ; + } + + for ( i = 2; i > 0; i-- ) { + dec->rlt[i] = dec->rlt[i-1] ; + dec->plt[i] = dec->plt[i-1] ; + dec->al[i] = dec->apl[i] ; + } + + /* FILTEP */ + wd1 = dec->rlt[1] << 1; + SATURATE(wd1, 32767, -32768); + wd1 = ( dec->al[1] * wd1 ) >> 15 ; + + wd2 = dec->rlt[2] << 1; + SATURATE(wd2, 32767, -32768); + wd2 = ( dec->al[2] * wd2 ) >> 15 ; + + dec->spl = wd1 + wd2 ; + SATURATE(dec->spl, 32767, -32768); + + /* FILTEZ */ + dec->szl = 0 ; + for (i=6; i>0; i--) { + wd = dec->dlt[i] << 1; + SATURATE(wd, 32767, -32768); + dec->szl += (dec->bl[i] * wd) >> 15 ; + SATURATE(dec->szl, 32767, -32768); + } + + /* PREDIC */ + sl = dec->spl + dec->szl ; + SATURATE(sl, 32767, -32768); + + return (sl) ; +} + +static int block5l (int ilr, int sl, int detl, int mode) +{ + int yl ; + int ril, dl, wd2 = 0; + static const int qm5[32] = { + -280, -280, -23352, -17560, + -14120, -11664, -9752, -8184, + -6864, -5712, -4696, -3784, + -2960, -2208, -1520, -880, + 23352, 17560, 14120, 11664, + 9752, 8184, 6864, 5712, + 4696, 3784, 2960, 2208, + 1520, 880, 280, -280 + }; + static const int qm6[64] = { + -136, -136, -136, -136, + -24808, -21904, -19008, -16704, + -14984, -13512, -12280, -11192, + -10232, -9360, -8576, -7856, + -7192, -6576, -6000, -5456, + -4944, -4464, -4008, -3576, + -3168, -2776, -2400, -2032, + -1688, -1360, -1040, -728, + 24808, 21904, 19008, 16704, + 14984, 13512, 12280, 11192, + 10232, 9360, 8576, 7856, + 7192, 6576, 6000, 5456, + 4944, 4464, 4008, 3576, + 3168, 2776, 2400, 2032, + 1688, 1360, 1040, 728, + 432, 136, -432, -136 + }; + + /* INVQBL */ + if (mode == 1) { + ril = ilr ; + wd2 = qm6[ril] ; + } + + if (mode == 2) { + ril = ilr >> 1 ; + wd2 = qm5[ril] ; + } + + if (mode == 3) { + ril = ilr >> 2 ; + wd2 = qm4[ril] ; + } + + dl = (detl * wd2 ) >> 15 ; + + /* RECONS */ + yl = sl + dl ; + SATURATE(yl, 32767, -32768); + + return (yl) ; +} + +static int block6l (int yl) +{ + int rl ; + + rl = yl ; + SATURATE(rl, 16383, -16384); + + return (rl) ; +} + +static int block2h (int ih, int deth) +{ + int dh ; + int wd2 ; + static const int qm2[4] = {-7408, -1616, 7408, 1616} ; + + /* INVQAH */ + wd2 = qm2[ih] ; + dh = (deth * wd2) >> 15 ; + + return (dh) ; +} + +static int block3h (g722_dec_t *dec, int ih) +{ + int deth ; + int ih2, wd, wd1, wd2, wd3, nbph, deph ; + static const int wh[3] = {0, -214, 798} ; + static const int rh2[4] = {2, 1, 2, 1} ; + + /* LOGSCH */ + ih2 = rh2[ih] ; + wd = (dec->nbh * 32512) >> 15 ; + nbph = wd + wh[ih2] ; + + if (nbph < 0) nbph = 0 ; + if (nbph > 22528) nbph = 22528 ; + + + /* SCALEH */ + wd1 = (nbph >> 6) & 31 ; + wd2 = nbph >> 11 ; + if ((10 - wd2) < 0) wd3 = ilb[wd1] << (wd2 - 10) ; + else wd3 = ilb[wd1] >> (10 - wd2) ; + deph = wd3 << 2 ; + + /* DELAYA */ + dec->nbh = nbph ; + + /* DELAYH */ + deth = deph ; + + return (deth) ; +} + +static int block4h (g722_dec_t *dec, int d) +{ + int sh = dec->shigh; + int i ; + int wd, wd1, wd2, wd3, wd4, wd5/*, wd6 */; + + dec->dh[0] = d; + + /* RECONS */ + dec->rh[0] = sh + dec->dh[0] ; + SATURATE(dec->rh[0], 32767, -32768); + + /* PARREC */ + dec->ph[0] = dec->dh[0] + dec->szh ; + SATURATE(dec->ph[0], 32767, -32768); + + /* UPPOL2 */ + dec->sgh[0] = dec->ph[0] >> 15 ; + dec->sgh[1] = dec->ph[1] >> 15 ; + dec->sgh[2] = dec->ph[2] >> 15 ; + + wd1 = dec->ah[1] << 2; + SATURATE(wd1, 32767, -32768); + + if ( dec->sgh[0] == dec->sgh[1] ) wd2 = - wd1 ; + else wd2 = wd1 ; + if (wd2 > 32767) wd2 = 32767; + + wd2 = wd2 >> 7 ; + + if ( dec->sgh[0] == dec->sgh[2] ) wd3 = 128 ; + else wd3 = - 128 ; + + wd4 = wd2 + wd3 ; + wd5 = (dec->ah[2] * 32512) >> 15 ; + + dec->aph[2] = wd4 + wd5 ; + SATURATE(dec->aph[2], 12288, -12288); + + /* UPPOL1 */ + dec->sgh[0] = dec->ph[0] >> 15 ; + dec->sgh[1] = dec->ph[1] >> 15 ; + + if ( dec->sgh[0] == dec->sgh[1] ) wd1 = 192 ; + else wd1 = - 192 ; + + wd2 = (dec->ah[1] * 32640) >> 15 ; + + dec->aph[1] = wd1 + wd2 ; + SATURATE(dec->aph[1], 32767, -32768); + //dec->aph[2]? + //if (aph[2] > 32767) aph[2] = 32767; + //if (aph[2] < -32768) aph[2] = -32768; + + wd3 = (15360 - dec->aph[2]) ; + SATURATE(wd3, 32767, -32768); + if ( dec->aph[1] > wd3) dec->aph[1] = wd3 ; + if ( dec->aph[1] < -wd3) dec->aph[1] = -wd3 ; + + /* UPZERO */ + if ( dec->dh[0] == 0 ) wd1 = 0 ; + if ( dec->dh[0] != 0 ) wd1 = 128 ; + + dec->sgh[0] = dec->dh[0] >> 15 ; + + for ( i = 1; i < 7; i++ ) { + dec->sgh[i] = dec->dh[i] >> 15 ; + if ( dec->sgh[i] == dec->sgh[0] ) wd2 = wd1 ; + else wd2 = - wd1 ; + wd3 = (dec->bh[i] * 32640) >> 15 ; + dec->bph[i] = wd2 + wd3 ; + } + + /* DELAYA */ + for ( i = 6; i > 0; i-- ) { + dec->dh[i] = dec->dh[i-1] ; + dec->bh[i] = dec->bph[i] ; + } + + for ( i = 2; i > 0; i-- ) { + dec->rh[i] = dec->rh[i-1] ; + dec->ph[i] = dec->ph[i-1] ; + dec->ah[i] = dec->aph[i] ; + } + + /* FILTEP */ + wd1 = dec->rh[1] << 1 ; + SATURATE(wd1, 32767, -32768); + wd1 = ( dec->ah[1] * wd1 ) >> 15 ; + + wd2 = dec->rh[2] << 1; + SATURATE(wd2, 32767, -32768); + wd2 = ( dec->ah[2] * wd2 ) >> 15 ; + + dec->sph = wd1 + wd2 ; + SATURATE(dec->sph, 32767, -32768); + + /* FILTEZ */ + dec->szh = 0 ; + for (i=6; i>0; i--) { + wd = dec->dh[i] << 1; + SATURATE(wd, 32767, -32768); + dec->szh += (dec->bh[i] * wd) >> 15 ; + SATURATE(dec->szh, 32767, -32768); + } + + /* PREDIC */ + sh = dec->sph + dec->szh ; + SATURATE(sh, 32767, -32768); + + return (sh) ; +} + +static int block5h (int dh, int sh) +{ + int rh ; + + rh = dh + sh; + SATURATE(rh, 16383, -16384); + + return (rh) ; +} + +void rx_qmf(g722_dec_t *dec, int rl, int rh, int *xout1, int *xout2) +{ + int i; + + pj_memmove(&dec->xd[1], dec->xd, 11*sizeof(dec->xd[0])); + pj_memmove(&dec->xs[1], dec->xs, 11*sizeof(dec->xs[0])); + + /* RECA */ + dec->xd[0] = rl - rh ; + if (dec->xd[0] > 16383) dec->xd[0] = 16383; + else if (dec->xd[0] < -16384) dec->xd[0] = -16384; + + /* RECB */ + dec->xs[0] = rl + rh ; + if (dec->xs[0] > 16383) dec->xs[0] = 16383; + else if (dec->xs[0] < -16384) dec->xs[0] = -16384; + + /* ACCUMC */ + *xout1 = 0; + for (i=0; i<12; ++i) *xout1 += dec->xd[i] * g722_qmf_coeff[2*i]; + *xout1 = *xout1 >> 12 ; + if (*xout1 > 16383) *xout1 = 16383 ; + else if (*xout1 < -16384) *xout1 = -16384 ; + + /* ACCUMD */ + *xout2 = 0; + for (i=0; i<12; ++i) *xout2 += dec->xs[i] * g722_qmf_coeff[2*i+1]; + *xout2 = *xout2 >> 12 ; + if (*xout2 > 16383) *xout2 = 16383 ; + else if (*xout2 < -16384) *xout2 = -16384 ; +} + + +PJ_DEF(pj_status_t) g722_dec_init(g722_dec_t *dec) +{ + PJ_ASSERT_RETURN(dec, PJ_EINVAL); + + pj_bzero(dec, sizeof(g722_dec_t)); + + dec->detlow = 32; + dec->dethigh = 8; + + return PJ_SUCCESS; +} + +PJ_DEF(pj_status_t) g722_dec_decode( g722_dec_t *dec, + void *in, + pj_size_t in_size, + pj_int16_t out[], + pj_size_t *nsamples) +{ + unsigned i; + int ilowr, ylow, rlow, dlowt; + int ihigh, rhigh, dhigh; + int pcm1, pcm2; + pj_uint8_t *in_ = (pj_uint8_t*) in; + + PJ_ASSERT_RETURN(dec && in && in_size && out && nsamples, PJ_EINVAL); + PJ_ASSERT_RETURN(*nsamples >= (in_size << 1), PJ_ETOOSMALL); + + for(i = 0; i < in_size; ++i) { + ilowr = in_[i] & 63; + ihigh = (in_[i] >> 6) & 3; + + /* low band decoder */ + ylow = block5l (ilowr, dec->slow, dec->detlow, MODE) ; + rlow = block6l (ylow) ; + dlowt = block2l (ilowr, dec->detlow) ; + dec->detlow = block3l (dec, ilowr) ; + dec->slow = block4l (dec, dlowt) ; + /* rlow <= output low band pcm */ + + /* high band decoder */ + dhigh = block2h (ihigh, dec->dethigh) ; + rhigh = block5h (dhigh, dec->shigh) ; + dec->dethigh = block3h (dec, ihigh) ; + dec->shigh = block4h (dec, dhigh) ; + /* rhigh <= output high band pcm */ + + rx_qmf(dec, rlow, rhigh, &pcm1, &pcm2); + out[i*2] = (pj_int16_t)pcm1; + out[i*2+1] = (pj_int16_t)pcm2; + } + + *nsamples = in_size << 1; + + return PJ_SUCCESS; +} + +PJ_DEF(pj_status_t) g722_dec_deinit(g722_dec_t *dec) +{ + pj_bzero(dec, sizeof(g722_dec_t)); + + return PJ_SUCCESS; +} + +#endif diff --git a/pjmedia/src/pjmedia-codec/g722/g722_dec.h b/pjmedia/src/pjmedia-codec/g722/g722_dec.h new file mode 100644 index 00000000..1494d297 --- /dev/null +++ b/pjmedia/src/pjmedia-codec/g722/g722_dec.h @@ -0,0 +1,78 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* + * Based on implementation found in Carnegie Mellon Speech Group Software + * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright + * was claimed in the original source codes. + */ +#ifndef __PJMEDIA_CODEC_G722_DEC_H__ +#define __PJMEDIA_CODEC_G722_DEC_H__ + +#include <pjmedia-codec/types.h> + +/* Decoder state */ +typedef struct g722_dec_t { + /* PCM low band */ + int slow; + int detlow; + int spl; + int szl; + int rlt [3]; + int al [3]; + int apl [3]; + int plt [3]; + int dlt [7]; + int bl [7]; + int bpl [7]; + int sgl [7]; + int nbl; + + /* PCM high band*/ + int shigh; + int dethigh; + int sph; + int szh; + int rh [3]; + int ah [3]; + int aph [3]; + int ph [3]; + int dh [7]; + int bh [7]; + int bph [7]; + int sgh [7]; + int nbh; + + /* QMF signal history */ + int xd[12]; + int xs[12]; +} g722_dec_t; + + +PJ_DECL(pj_status_t) g722_dec_init(g722_dec_t *dec); + +PJ_DECL(pj_status_t) g722_dec_decode(g722_dec_t *dec, + void *in, + pj_size_t in_size, + pj_int16_t out[], + pj_size_t *nsamples); + +PJ_DECL(pj_status_t) g722_dec_deinit(g722_dec_t *dec); + +#endif /* __PJMEDIA_CODEC_G722_DEC_H__ */ + diff --git a/pjmedia/src/pjmedia-codec/g722/g722_enc.c b/pjmedia/src/pjmedia-codec/g722/g722_enc.c new file mode 100644 index 00000000..e71aad1b --- /dev/null +++ b/pjmedia/src/pjmedia-codec/g722/g722_enc.c @@ -0,0 +1,575 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* + * Based on implementation found in Carnegie Mellon Speech Group Software + * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright + * was claimed in the original source codes. + */ +#include <pjmedia/errno.h> +#include <pj/assert.h> +#include <pj/pool.h> + +#include "g722_enc.h" + +#if defined(PJMEDIA_HAS_G722_CODEC) && (PJMEDIA_HAS_G722_CODEC != 0) + +#define SATURATE(v, max, min) \ + if (v>max) v = max; \ + else if (v<min) v = min + +/* QMF tap coefficients */ +const int g722_qmf_coeff[24] = { + 3, -11, -11, 53, 12, -156, + 32, 362, -210, -805, 951, 3876, + 3876, 951, -805, -210, 362, 32, + -156, 12, 53, -11, -11, 3 +}; + + +static int block1l (int xl, int sl, int detl) +{ + int il ; + + int i, el, sil, mil, wd, wd1, hdu ; + + static const int q6[32] = { + 0, 35, 72, 110, 150, 190, 233, 276, 323, + 370, 422, 473, 530, 587, 650, 714, 786, + 858, 940, 1023, 1121, 1219, 1339, 1458, + 1612, 1765, 1980, 2195, 2557, 2919, 0, 0 + }; + + static const int iln[32] = { + 0, 63, 62, 31, 30, 29, 28, 27, 26, 25, + 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, + 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 0 + }; + + static const int ilp[32] = { + 0, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, + 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, + 40, 39, 38, 37, 36, 35, 34, 33, 32, 0 + }; + + /* SUBTRA */ + + el = xl - sl ; + SATURATE(el, 32767, -32768); + + /* QUANTL */ + + sil = el >> 15 ; + if (sil == 0 ) wd = el ; + else wd = 32767 - el & 32767 ; + + mil = 1 ; + + for (i = 1; i < 30; i++) { + hdu = (q6[i] << 3) * detl; + wd1 = (hdu >> 15) ; + if (wd >= wd1) mil = (i + 1) ; + else break ; + } + + if (sil == -1 ) il = iln[mil] ; + else il = ilp[mil] ; + + return (il) ; +} + +static int block2l (int il, int detl) +{ + int dlt; + int ril, wd2 ; + static const int qm4[16] = { + 0, -20456, -12896, -8968, + -6288, -4240, -2584, -1200, + 20456, 12896, 8968, 6288, + 4240, 2584, 1200, 0 + }; + + /* INVQAL */ + ril = il >> 2 ; + wd2 = qm4[ril] ; + dlt = (detl * wd2) >> 15 ; + + return (dlt) ; +} + +static int block3l (g722_enc_t *enc, int il) +{ + int detl; + int ril, il4, wd, wd1, wd2, wd3, nbpl, depl ; + static int const wl[8] = { + -60, -30, 58, 172, 334, 538, 1198, 3042 + } ; + static int const rl42[16] = { + 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0 + }; + static const int ilb[32] = { + 2048, 2093, 2139, 2186, 2233, 2282, 2332, + 2383, 2435, 2489, 2543, 2599, 2656, 2714, + 2774, 2834, 2896, 2960, 3025, 3091, 3158, + 3228, 3298, 3371, 3444, 3520, 3597, 3676, + 3756, 3838, 3922, 4008 + }; + + /* LOGSCL */ + + ril = il >> 2 ; + il4 = rl42[ril] ; + + wd = (enc->nbl * 32512) >> 15 ; + nbpl = wd + wl[il4] ; + + if (nbpl < 0) nbpl = 0 ; + if (nbpl > 18432) nbpl = 18432 ; + + /* SCALEL */ + + wd1 = (nbpl >> 6) & 31 ; + wd2 = nbpl >> 11 ; + if ((8 - wd2) < 0) wd3 = ilb[wd1] << (wd2 - 8) ; + else wd3 = ilb[wd1] >> (8 - wd2) ; + depl = wd3 << 2 ; + + /* DELAYA */ + enc->nbl = nbpl ; + + /* DELAYL */ + detl = depl ; + +#ifdef DEBUG_VERBOSE + printf ("BLOCK3L il=%4d, ril=%4d, il4=%4d, nbl=%4d, wd=%4d, nbpl=%4d\n", + il, ril, il4, enc->nbl, wd, nbpl) ; + printf ("wd1=%4d, wd2=%4d, wd3=%4d, depl=%4d, detl=%4d\n", + wd1, wd2, wd3, depl, detl) ; +#endif + + return (detl) ; +} + +static int block4l (g722_enc_t *enc, int dl) +{ + int sl = enc->slow; + int i ; + int wd, wd1, wd2, wd3, wd4, wd5 /*, wd6 */; + + enc->dlt[0] = dl; + + /* RECONS */ + + enc->rlt[0] = sl + enc->dlt[0] ; + SATURATE(enc->rlt[0], 32767, -32768); + + /* PARREC */ + + enc->plt[0] = enc->dlt[0] + enc->szl ; + SATURATE(enc->plt[0], 32767, -32768); + + /* UPPOL2 */ + + enc->sgl[0] = enc->plt[0] >> 15 ; + enc->sgl[1] = enc->plt[1] >> 15 ; + enc->sgl[2] = enc->plt[2] >> 15 ; + + wd1 = enc->al[1] << 2; + SATURATE(wd1, 32767, -32768); + + if ( enc->sgl[0] == enc->sgl[1] ) wd2 = - wd1 ; + else wd2 = wd1 ; + if ( wd2 > 32767 ) wd2 = 32767; + + wd2 = wd2 >> 7 ; + + if ( enc->sgl[0] == enc->sgl[2] ) wd3 = 128 ; + else wd3 = - 128 ; + + wd4 = wd2 + wd3 ; + wd5 = (enc->al[2] * 32512) >> 15 ; + + enc->apl[2] = wd4 + wd5 ; + SATURATE(enc->apl[2], 12288, -12288); + + /* UPPOL1 */ + + enc->sgl[0] = enc->plt[0] >> 15 ; + enc->sgl[1] = enc->plt[1] >> 15 ; + + if ( enc->sgl[0] == enc->sgl[1] ) wd1 = 192 ; + else wd1 = - 192 ; + + wd2 = (enc->al[1] * 32640) >> 15 ; + + enc->apl[1] = wd1 + wd2 ; + SATURATE(enc->apl[1], 32767, -32768); + + wd3 = (15360 - enc->apl[2]) ; + SATURATE(wd3, 32767, -32768); + + if ( enc->apl[1] > wd3) enc->apl[1] = wd3 ; + if ( enc->apl[1] < -wd3) enc->apl[1] = -wd3 ; + + /* UPZERO */ + + if ( enc->dlt[0] == 0 ) wd1 = 0 ; + else wd1 = 128 ; + + enc->sgl[0] = enc->dlt[0] >> 15 ; + + for ( i = 1; i < 7; i++ ) { + enc->sgl[i] = enc->dlt[i] >> 15 ; + if ( enc->sgl[i] == enc->sgl[0] ) wd2 = wd1 ; + else wd2 = - wd1 ; + wd3 = (enc->bl[i] * 32640) >> 15 ; + enc->bpl[i] = wd2 + wd3 ; + SATURATE(enc->bpl[i], 32767, -32768); + } + + /* DELAYA */ + + for ( i = 6; i > 0; i-- ) { + enc->dlt[i] = enc->dlt[i-1] ; + enc->bl[i] = enc->bpl[i] ; + } + + for ( i = 2; i > 0; i-- ) { + enc->rlt[i] = enc->rlt[i-1] ; + enc->plt[i] = enc->plt[i-1] ; + enc->al[i] = enc->apl[i] ; + } + + /* FILTEP */ + + wd1 = enc->rlt[1] + enc->rlt[1]; + SATURATE(wd1, 32767, -32768); + wd1 = ( enc->al[1] * wd1 ) >> 15 ; + + wd2 = enc->rlt[2] + enc->rlt[2]; + SATURATE(wd2, 32767, -32768); + wd2 = ( enc->al[2] * wd2 ) >> 15 ; + + enc->spl = wd1 + wd2 ; + SATURATE(enc->spl, 32767, -32768); + + /* FILTEZ */ + + enc->szl = 0 ; + for (i=6; i>0; i--) { + wd = enc->dlt[i] + enc->dlt[i]; + SATURATE(wd, 32767, -32768); + enc->szl += (enc->bl[i] * wd) >> 15 ; + SATURATE(enc->szl, 32767, -32768); + } + + /* PREDIC */ + + sl = enc->spl + enc->szl ; + SATURATE(sl, 32767, -32768); + + return (sl) ; +} + +static int block1h (int xh, int sh, int deth) +{ + int ih ; + + int eh, sih, mih, wd, wd1, hdu ; + + static const int ihn[3] = { 0, 1, 0 } ; + static const int ihp[3] = { 0, 3, 2 } ; + + /* SUBTRA */ + + eh = xh - sh ; + SATURATE(eh, 32767, -32768); + + /* QUANTH */ + + sih = eh >> 15 ; + if (sih == 0 ) wd = eh ; + else wd = 32767 - eh & 32767 ; + + hdu = (564 << 3) * deth; + wd1 = (hdu >> 15) ; + if (wd >= wd1) mih = 2 ; + else mih = 1 ; + + if (sih == -1 ) ih = ihn[mih] ; + else ih = ihp[mih] ; + + return (ih) ; +} + +static int block2h (int ih, int deth) +{ + int dh ; + int wd2 ; + static const int qm2[4] = {-7408, -1616, 7408, 1616}; + + /* INVQAH */ + + wd2 = qm2[ih] ; + dh = (deth * wd2) >> 15 ; + + return (dh) ; +} + +static int block3h (g722_enc_t *enc, int ih) +{ + int deth ; + int ih2, wd, wd1, wd2, wd3, nbph, deph ; + static const int wh[3] = {0, -214, 798} ; + static const int rh2[4] = {2, 1, 2, 1} ; + static const int ilb[32] = { + 2048, 2093, 2139, 2186, 2233, 2282, 2332, + 2383, 2435, 2489, 2543, 2599, 2656, 2714, + 2774, 2834, 2896, 2960, 3025, 3091, 3158, + 3228, 3298, 3371, 3444, 3520, 3597, 3676, + 3756, 3838, 3922, 4008 + }; + + /* LOGSCH */ + + ih2 = rh2[ih] ; + wd = (enc->nbh * 32512) >> 15 ; + nbph = wd + wh[ih2] ; + + if (nbph < 0) nbph = 0 ; + if (nbph > 22528) nbph = 22528 ; + + /* SCALEH */ + + wd1 = (nbph >> 6) & 31 ; + wd2 = nbph >> 11 ; + if ((10-wd2) < 0) wd3 = ilb[wd1] << (wd2-10) ; + else wd3 = ilb[wd1] >> (10-wd2) ; + deph = wd3 << 2 ; + + /* DELAYA */ + enc->nbh = nbph ; + /* DELAYH */ + deth = deph ; + + return (deth) ; +} + +static int block4h (g722_enc_t *enc, int d) +{ + int sh = enc->shigh; + int i ; + int wd, wd1, wd2, wd3, wd4, wd5 /*, wd6 */; + + enc->dh[0] = d; + + /* RECONS */ + + enc->rh[0] = sh + enc->dh[0] ; + SATURATE(enc->rh[0], 32767, -32768); + + /* PARREC */ + + enc->ph[0] = enc->dh[0] + enc->szh ; + SATURATE(enc->ph[0], 32767, -32768); + + /* UPPOL2 */ + + enc->sgh[0] = enc->ph[0] >> 15 ; + enc->sgh[1] = enc->ph[1] >> 15 ; + enc->sgh[2] = enc->ph[2] >> 15 ; + + wd1 = enc->ah[1] << 2; + SATURATE(wd1, 32767, -32768); + + if ( enc->sgh[0] == enc->sgh[1] ) wd2 = - wd1 ; + else wd2 = wd1 ; + if ( wd2 > 32767 ) wd2 = 32767; + + wd2 = wd2 >> 7 ; + + if ( enc->sgh[0] == enc->sgh[2] ) wd3 = 128 ; + else wd3 = - 128 ; + + wd4 = wd2 + wd3 ; + wd5 = (enc->ah[2] * 32512) >> 15 ; + + enc->aph[2] = wd4 + wd5 ; + SATURATE(enc->aph[2], 12288, -12288); + + /* UPPOL1 */ + + enc->sgh[0] = enc->ph[0] >> 15 ; + enc->sgh[1] = enc->ph[1] >> 15 ; + + if ( enc->sgh[0] == enc->sgh[1] ) wd1 = 192 ; + else wd1 = - 192 ; + + wd2 = (enc->ah[1] * 32640) >> 15 ; + + enc->aph[1] = wd1 + wd2 ; + SATURATE(enc->aph[1], 32767, -32768); + + wd3 = (15360 - enc->aph[2]) ; + SATURATE(wd3, 32767, -32768); + + if ( enc->aph[1] > wd3) enc->aph[1] = wd3 ; + else if ( enc->aph[1] < -wd3) enc->aph[1] = -wd3 ; + + /* UPZERO */ + + if ( enc->dh[0] == 0 ) wd1 = 0 ; + else wd1 = 128 ; + + enc->sgh[0] = enc->dh[0] >> 15 ; + + for ( i = 1; i < 7; i++ ) { + enc->sgh[i] = enc->dh[i] >> 15 ; + if ( enc->sgh[i] == enc->sgh[0] ) wd2 = wd1 ; + else wd2 = - wd1 ; + wd3 = (enc->bh[i] * 32640) >> 15 ; + enc->bph[i] = wd2 + wd3 ; + SATURATE(enc->bph[i], 32767, -32768); + } + + /* DELAYA */ + for ( i = 6; i > 0; i-- ) { + enc->dh[i] = enc->dh[i-1] ; + enc->bh[i] = enc->bph[i] ; + } + + for ( i = 2; i > 0; i-- ) { + enc->rh[i] = enc->rh[i-1] ; + enc->ph[i] = enc->ph[i-1] ; + enc->ah[i] = enc->aph[i] ; + } + + /* FILTEP */ + + wd1 = enc->rh[1] + enc->rh[1]; + SATURATE(wd1, 32767, -32768); + wd1 = ( enc->ah[1] * wd1 ) >> 15 ; + + wd2 = enc->rh[2] + enc->rh[2]; + SATURATE(wd2, 32767, -32768); + wd2 = ( enc->ah[2] * wd2 ) >> 15 ; + + enc->sph = wd1 + wd2 ; + SATURATE(enc->sph, 32767, -32768); + + /* FILTEZ */ + + enc->szh = 0 ; + for (i=6; i>0; i--) { + wd = enc->dh[i] + enc->dh[i]; + SATURATE(wd, 32767, -32768); + enc->szh += (enc->bh[i] * wd) >> 15 ; + SATURATE(enc->szh, 32767, -32768); + } + + /* PREDIC */ + + sh = enc->sph + enc->szh ; + SATURATE(sh, 32767, -32768); + + return (sh) ; +} + +/* PROCESS PCM THROUGH THE QMF FILTER */ +static void tx_qmf(g722_enc_t *enc, int pcm1, int pcm2, int *lo, int *hi) +{ + int sumodd, sumeven; + int i; + + pj_memmove(&enc->x[2], enc->x, 22 * sizeof(enc->x[0])); + enc->x[1] = pcm1; + enc->x[0] = pcm2; + + sumodd = 0; + for (i=1; i<24; i+=2) sumodd += enc->x[i] * g722_qmf_coeff[i]; + + sumeven = 0; + for (i=0; i<24; i+=2) sumeven += enc->x[i] * g722_qmf_coeff[i]; + + *lo = (sumeven + sumodd) >> 13 ; + *hi = (sumeven - sumodd) >> 13 ; + + SATURATE(*lo, 16383, -16384); + SATURATE(*hi, 16383, -16383); +} + + +PJ_DEF(pj_status_t) g722_enc_init(g722_enc_t *enc) +{ + PJ_ASSERT_RETURN(enc, PJ_EINVAL); + + pj_bzero(enc, sizeof(g722_enc_t)); + + enc->detlow = 32; + enc->dethigh = 8; + + return PJ_SUCCESS; +} + +PJ_DEF(pj_status_t) g722_enc_encode( g722_enc_t *enc, + pj_int16_t in[], + pj_size_t nsamples, + void *out, + pj_size_t *out_size) +{ + unsigned i; + int xlow, ilow, dlowt; + int xhigh, ihigh, dhigh; + pj_uint8_t *out_ = (pj_uint8_t*) out; + + PJ_ASSERT_RETURN(enc && in && nsamples && out && out_size, PJ_EINVAL); + PJ_ASSERT_RETURN(nsamples % 2 == 0, PJ_EINVAL); + PJ_ASSERT_RETURN(*out_size >= (nsamples >> 1), PJ_ETOOSMALL); + + for(i = 0; i < nsamples; i += 2) { + tx_qmf(enc, in[i], in[i+1], &xlow, &xhigh); + + /* low band encoder */ + ilow = block1l (xlow, enc->slow, enc->detlow) ; + dlowt = block2l (ilow, enc->detlow) ; + enc->detlow = block3l (enc, ilow) ; + enc->slow = block4l (enc, dlowt) ; + + /* high band encoder */ + ihigh = block1h (xhigh, enc->shigh, enc->dethigh) ; + dhigh = block2h (ihigh, enc->dethigh) ; + enc->dethigh = block3h (enc, ihigh) ; + enc->shigh = block4h (enc, dhigh) ; + + /* bits mix low & high adpcm */ + out_[i/2] = (pj_uint8_t)((ihigh << 6) | ilow); + } + + *out_size = nsamples >> 1; + + return PJ_SUCCESS; +} + +PJ_DEF(pj_status_t) g722_enc_deinit(g722_enc_t *enc) +{ + pj_bzero(enc, sizeof(g722_enc_t)); + + return PJ_SUCCESS; +} + +#endif diff --git a/pjmedia/src/pjmedia-codec/g722/g722_enc.h b/pjmedia/src/pjmedia-codec/g722/g722_enc.h new file mode 100644 index 00000000..3bd235fc --- /dev/null +++ b/pjmedia/src/pjmedia-codec/g722/g722_enc.h @@ -0,0 +1,77 @@ +/* $Id$ */ +/* + * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* + * Based on implementation found in Carnegie Mellon Speech Group Software + * depository (ftp://ftp.cs.cmu.edu/project/fgdata/index.html). No copyright + * was claimed in the original source codes. + */ +#ifndef __PJMEDIA_CODEC_G722_ENC_H__ +#define __PJMEDIA_CODEC_G722_ENC_H__ + +#include <pjmedia-codec/types.h> + +/* Encoder state */ +typedef struct g722_enc_t { + /* PCM low band */ + int slow; + int detlow; + int spl; + int szl; + int rlt [3]; + int al [3]; + int apl [3]; + int plt [3]; + int dlt [7]; + int bl [7]; + int bpl [7]; + int sgl [7]; + int nbl; + + /* PCM high band*/ + int shigh; + int dethigh; + int sph; + int szh; + int rh [3]; + int ah [3]; + int aph [3]; + int ph [3]; + int dh [7]; + int bh [7]; + int bph [7]; + int sgh [7]; + int nbh; + + /* QMF signal history */ + int x[24]; +} g722_enc_t; + + +PJ_DECL(pj_status_t) g722_enc_init(g722_enc_t *enc); + +PJ_DECL(pj_status_t) g722_enc_encode(g722_enc_t *enc, + pj_int16_t in[], + pj_size_t nsamples, + void *out, + pj_size_t *out_size); + +PJ_DECL(pj_status_t) g722_enc_deinit(g722_enc_t *enc); + +#endif /* __PJMEDIA_CODEC_G722_ENC_H__ */ + |