📄 g727.c
字号:
/* 19.May.2000 v1.02============================================================================= U U GGG SSSS TTTTT U U G S T U U G GG SSSS T U U G G S T UUU GG SSS T ======================================== ITU-T - USER'S GROUP ON SOFTWARE TOOLS ======================================== ============================================================= COPYRIGHT NOTE: This source code, and all of its derivations, is subject to the "ITU-T General Public License". Please have it read in the distribution disk, or in the ITU-T Recommendation G.191 on "SOFTWARE TOOLS FOR SPEECH AND AUDIO CODING STANDARDS". =============================================================MODULE: G727.C, G.727 embedded ADPCM algorithm operating with 5-, 4-, 3-, and 2-bits per sampleORIGINAL BY: Nikolai L. Birioukov Maxim A. Lagerev Ukrainian Research Institut of Telecommunications P.O. 252650 13, Solomenskaya St. Kiev, Ukraine Phone : +380 44 276 51 01 FAX : +380 44 226 26 46 EMail : INFO@UNIIS,KIEV,UAFUNCTIONS: G727_reset ...... G727 reset function; G727_encode ..... G727 encoder function; G727_decode ..... G727 decoder function;HISTORY: 01.Apr.1995 0.98 Version of the G727 module in C++ code 10.Jul.1996 0.99 C code version released to UGST <info@uniis.kiev.ua> 10.Mar.1997 1.00 Conversion to UGST format <simao.campos@comsat.com> 04.Aug.1997 1.01 Eliminated compilation warning about unused variables as per revision from <Morgan.Lindqvist@era-t.ericsson.se> 19.May.2000 v1.02 Corrected self-documentation of functions. <simao>=============================================================================*/#include <assert.h>#include "g727.h"/* Local function prototypes */Int16 g727_get_d ARGS((Int8 s, Int16 se, short law));Int16 g727_expand ARGS((Int8 sp, short law));Int16 g727_subta ARGS((Int16 sl, Int16 se));Int8 g727_get_in ARGS((Int16 d, Int16 y, short rate));void g727_log ARGS((Int16 *dl, Int8 *ds, Int16 d));Int8 g727_quan ARGS((Int16 dln, Int8 ds, short rate));Int16 g727_subtb ARGS((Int16 dl, Int16 y));Int8 g727_get_ic ARGS((Int8 in, short enh));Int16 g727_get_dq ARGS((Int16 y, Int8 in, short rate));Int16 g727_antilog ARGS((Int16 dql, Int8 dqs));Int16 g727_adda ARGS((Int16 dqln, Int16 y));void g727_reconst ARGS((Int16 *dqln, Int8 *dqs, Int8 in, short rate));void g727_qsfa_reset ARGS((g727_qsfa_state *st));Int16 g727_get_y ARGS((g727_qsfa_state *st, Int8 al));Int32 g727_get_yl ARGS((g727_qsfa_state *st));void g727_qsfa_transit ARGS((g727_qsfa_state *st, Int8 in, Int16 y, short rate));Int16 g727_filtd ARGS((Int16 wi, Int16 y));Int32 g727_filte ARGS((g727_qsfa_state *st, Int16 yup));Int16 g727_functw ARGS((Int8 in, short rate));Int16 g727_limb ARGS((Int16 yut));Int16 g727_mix ARGS((g727_qsfa_state *st, Int8 al));void g727_asc_reset ARGS((g727_asc_state *st));Int8 g727_get_al ARGS((g727_asc_state *st));void g727_asc_transit ARGS((g727_asc_state *st, Int8 in, Int16 y, Int8 tr, Int8 tdp, short rate));Int16 g727_filta ARGS((g727_asc_state *st, Int8 fi));Int16 g727_filtb ARGS((g727_asc_state *st, Int8 fi));Int16 g727_filtc ARGS((g727_asc_state *st, Int8 ax));Int8 g727_functf ARGS((Int8 in, short rate));Int8 g727_lima ARGS((g727_asc_state *st));Int8 g727_subtc ARGS((Int16 dmsp, Int16 dmlp, Int8 tdp, Int16 y));Int16 g727_triga ARGS((Int8 tr, Int16 app));void g727_aprsc_reset ARGS((g727_aprsc_state *st));Int16 g727_get_sr ARGS((Int16 dq, Int16 se));void g727_get_se_sez ARGS((g727_aprsc_state *st, Int16 *se, Int16 *sez));Int16 g727_get_a2p ARGS((g727_aprsc_state *st, Int16 dq, Int16 sez, Int8 *pk0, Int8 *sigpk));void g727_aprsc_transit ARGS((g727_aprsc_state *st, Int16 dq, Int8 tr, Int16 sr, Int16 a2p, Int8 pk0, Int8 sigpk));void g727_accum ARGS((Int16 *se, Int16 *sez, Int16 wa1, Int16 wa2, Int16 wb1, Int16 wb2, Int16 wb3, Int16 wb4, Int16 wb5, Int16 wb6));Int16 g727_addb ARGS((Int16 dq, Int16 se));void g727_addc ARGS((Int8 *pk0, Int8 *sigpk, Int16 dq, Int16 sez));Int8 g727_makexp ARGS((Int16 mag));Int16 g727_floata ARGS((Int16 dq));Int16 g727_floatb ARGS((Int16 sr));Int16 g727_fmult ARGS((Int16 an, Int16 srn));Int16 g727_limc ARGS((Int16 a2t));Int16 g727_limd ARGS((Int16 a1t, Int16 a2p));Int16 g727_aprsc_trigb ARGS((Int8 tr, Int16 anp));Int16 g727_upa1 ARGS((g727_aprsc_state *st, Int8 pk0, Int8 sigpk));Int16 g727_upa2 ARGS((g727_aprsc_state *st, Int8 pk0, Int8 sigpk));Int16 g727_upb ARGS((Int8 un, Int16 bn, Int16 dq));Int8 g727_xor ARGS((Int16 dqn, Int16 dq));Int16 g727_get_srff ARGS((Int16 dqff, Int16 se));void g727_ttd_reset ARGS((g727_ttd_state *st));Int8 g727_get_tr ARGS((g727_ttd_state *st, Int32 yl, Int16 dq));Int8 g727_get_tdp ARGS((Int16 a2p));void g727_ttd_transit ARGS((g727_ttd_state *st, Int8 tr, Int8 tdp));Int8 g727_tone ARGS((Int16 a2p));Int8 g727_trans ARGS((g727_ttd_state *st, Int32 yl, Int16 dq));Int8 g727_ttd_trigb ARGS((Int8 tr, Int8 tdp));Int8 g727_get_sd ARGS((Int16 sr, Int16 se, Int8 in, Int16 y, short law, short rate));Int8 g727_compress ARGS((Int16 sr, short law));Int8 g727_sync ARGS((Int8 in, Int8 sp, Int16 dlnx, short law, Int8 ds, short rate));/*---------- Constant tables ----------*/static Int16 g727_dqlnTable40[32] = { 3832, 4035, 34, 97, 145, 184, 217, 246, 273, 298, 321, 344, 367, 391, 419, 456, 456, 419, 391, 367, 344, 321, 298, 273, 246, 217, 184, 145, 97, 34, 4035, 3832};static Int16 g727_dqlnTable32[16] = { 3961, 68, 165, 232, 285, 332, 377, 428, 428, 377, 332, 285, 232, 165, 68, 3961};static Int16 g727_dqlnTable24[8] = { 4085, 199, 307, 395, 395, 307, 199, 4085};static Int16 g727_dqlnTable16[4] = { 116, 365, 365, 116};static Int16 g727_wiTable32[8] = { 4084, 4, 27, 50, 98, 184, 340, 1108};static Int16 g727_wiTable24[4] = { 4092, 30, 137, 582};static Int16 g727_wiTable16[2] = { 4074, 439};static Int8 g727_fiTable32[8] = { 0, 0, 0, 1, 1, 1, 3, 7};static Int8 g727_fiTable24[4] = { 0, 1, 2, 7};static Int8 g727_fiTable16[2] = { 0, 7};/*---------- G.727 ADPCM Encoder & Decoder ----------*//* ---------------------------------------------------------------------------- void g727_reset (g727_state *st); Description: ~~~~~~~~~~~~ Reset ITU-T G.727 embedded ADPCM encoder or decoder state variable. The state variable is defined in g727.h. Return value: ~~~~~~~~~~~~~ None. Prototype: in file g727.h ~~~~~~~~~~ History: ~~~~~~~~ 10.Jul.96 0.99 C code version released to UGST <info@uniis.kiev.ua> 10.Mar.97 1.00 Conversion to UGST format <simao.campos@comsat.com> ----------------------------------------------------------------------------*/voidg727_reset(g727_state *st){ g727_qsfa_reset(&st->qsfa); g727_asc_reset(&st->asc); g727_aprsc_reset(&st->aprsc); g727_ttd_reset(&st->ttd);}/* ........................ End of G727_reset() ........................ *//* ---------------------------------------------------------------------------- void g727_encode_block (short *src, short *dst, short n, ~~~~~~~~~~~~~~~~~~~~~~ short law, short cbits, short ebits, g727_state *st); Description: ~~~~~~~~~~~~ Simulation of the ITU-T G.727 embedded ADPCM encoder. Takes the A or mu law input array of shorts `src' (16 bit, right- justified, without sign extension) of length `n', and saves the encoded samples in the array of shorts `dst', with the same number of samples and right-justified. The ADPCM samples will have cbits core bits, and ebits enhancement bits. The state variables are saved in the structure `g727_state'. The law is A if `law'=='1', and mu law if `law'=='0'. Parameters: ~~~~~~~~~~~ src .......... A- or u-law 16-bit right justified sample to encode dst .......... 16-bit right justified ADPCM-encoded sample with cbits core bits and ebits enhancement bits n ............ Number of samples to encode. law .......... encoding law (character '1'=A-law, character '0'=u-law). cbits ........ number of core bits ebits ........ number of enhancement bits g727_state ... G.727 state variable structure Return value: ~~~~~~~~~~~~~ None. Prototype: in file g727.h ~~~~~~~~~~ History: ~~~~~~~~ 10.Jul.96 0.99 C code version released to UGST <info@uniis.kiev.ua> 10.Mar.97 1.00 Conversion to UGST format <simao.campos@comsat.com> ----------------------------------------------------------------------------*/voidg727_encode_block(short *src, short *dst, short n, short law, short cbits, short ebits, g727_state *st){ short *eptr = src + n; /* Fix for compatibility with g726_encode() definitions */ if (law == '1') law = 1; else if (law == '0') law = 0; /* Encode all samples in buffer */ while (src < eptr) *dst++ = g727_encode_sample(*src++, law, cbits, ebits, st);}/* ..................... End of G727_encode_block() ..................... *//* ---------------------------------------------------------------------------- void g727_decode_block (short *src, short *dst, short n, ~~~~~~~~~~~~~~~~~~~~~~ short law, short cbits, short ebits, g727_state *st); Description: ~~~~~~~~~~~~ Simulation of the ITU-T G.727 embedded ADPCM decoder. Takes the ADPCM input array of shorts `src' (16 bit, right- justified, without sign extension) of length `n', and saves the decoded samples (A or mu law) in the array of shorts `dst', with the same number of samples and right-justified. The ADPCM samples must have cbits core bits, and ebits enhancement bits. The state variables are saved in the structure `g727_state'. The law is A if `law'=='1', and mu law if `law'=='0'. Parameters: ~~~~~~~~~~~ src .......... 16-bit right justified ADPCM-encoded sample with cbits core bits and ebits enhancement bits dst .......... A- or u-law 16-bit right justified sample to encode n ............ Number of samples to encode. law .......... encoding law (character '1'=A-law, character '0'=u-law). cbits ........ number of core bits ebits ........ number of enhancement bits g727_state ... G.727 state variable structure Return value: ~~~~~~~~~~~~~ None. Prototype: in file g727.h ~~~~~~~~~~ History: ~~~~~~~~ 10.Jul.96 0.99 C code version released to UGST <info@uniis.kiev.ua> 10.Mar.97 1.00 Conversion to UGST format <simao.campos@comsat.com> ----------------------------------------------------------------------------*/voidg727_decode_block(short *src, short *dst, short n, short law, short cbits, short ebits, g727_state *st){ short *eptr = src + n; /* Fix for compatibility with g726_decode() definitions */ if (law == '1') law = 1; else if (law == '0') law = 0; /* Decode all samples in buffer */ while (src < eptr) *dst++ = g727_decode_sample(*src++, law, cbits, ebits, st);}/* ...................... End of G727_decode_block() ..................... *//* ---------------------------------------------------------------------------- short g727_encode_sample (short code, short law, short cbits, ~~~~~~~~~~~~~~~~~~~~~~~~ short ebits, g727_state *st); Description: ~~~~~~~~~~~~ Simulation of the ITU-T G.727 embedded ADPCM encoder. Takes one A or mu law (16 bit, right- justified, without sign extension) sample in 'code' and returns the ADPCM-encoded sample using the law and number of core and enhancement bits specified by the user. Samples are returned right-justified. The state variables are saved in the structure `g727_state'. The law is A if `law'=='1', and mu law if `law'=='0'. Parameters: ~~~~~~~~~~~ code ......... A- or u-law 16-bit right justified sample to encode law .......... encoding law (character '1'=A-law, character '0'=u-law). cbits ........ number of core bits ebits ........ number of enhancement bits g727_state ... G.727 state variable structure Return value: ~~~~~~~~~~~~~ 16-bit right justified ADPCM-encoded sample with cbits core bits and ebits enhancement bits Prototype: in file g727.h ~~~~~~~~~~ History: ~~~~~~~~ 10.Jul.96 0.99 C code version released to UGST <info@uniis.kiev.ua> 10.Mar.97 1.00 Conversion to UGST format <simao.campos@comsat.com> ----------------------------------------------------------------------------*/short g727_encode_sample(short code, short law, short cbits, short ebits, g727_state *st){ Int16 se; Int16 sez; Int8 al; Int16 y; Int32 yl; Int16 d; Int8 in; Int8 ic; Int16 dq; Int16 sr; Int8 pk0; Int8 sigpk; Int16 a2p; Int8 tr; Int8 tdp; assert(2 <= cbits && cbits <= 4 && ebits >= 0 && cbits + ebits <= 5); g727_get_se_sez(&st->aprsc, &se, &sez); al = g727_get_al(&st->asc); y = g727_get_y(&st->qsfa, al); yl = g727_get_yl(&st->qsfa); d = g727_get_d((Int8)code, se, law); in = g727_get_in(d, y, cbits + ebits); ic = g727_get_ic(in, ebits); dq = g727_get_dq(y, ic, cbits); sr = g727_get_sr(dq, se); a2p = g727_get_a2p(&st->aprsc, dq, sez, &pk0, &sigpk); tr = g727_get_tr(&st->ttd, yl, dq); tdp = g727_get_tdp(a2p); g727_qsfa_transit(&st->qsfa, ic, y, cbits); g727_asc_transit(&st->asc, ic, y, tr, tdp, cbits); g727_aprsc_transit(&st->aprsc, dq, tr, sr, a2p, pk0, sigpk); g727_ttd_transit(&st->ttd, tr, tdp); return in;}/* ..................... End of G727_encode_sample() ..................... *//* ---------------------------------------------------------------------------- void g727_decode_sample (short code, short law, short cbits, ~~~~~~~~~~~~~~~~~~~~~~~ short ebits, g727_state *st); Description: ~~~~~~~~~~~~ Simulation of the ITU-T G.727 embedded ADPCM decoder. Takes the ADPCM short sample in 'code' (16 bit, right- justified, without sign extension), and returns the decoded sample (A or mu law), right-justified. The state variables are saved in the structure `g727_state'. The law is A if `law'=='1', and mu law if `law'=='0'. Parameters: ~~~~~~~~~~~ code ......... 16-bit right justified ADPCM-encoded sample with cbits core bits and ebits enhancement bits law .......... encoding law (character '1'=A-law, character '0'=u-law). cbits ........ number of core bits ebits ........ number of enhancement bits g727_state ... G.727 state variable structure Return value: ~~~~~~~~~~~~~ 16-bit right justified decoded A- or u-law sample Prototype: in file g727.h ~~~~~~~~~~ History: ~~~~~~~~ 10.Jul.96 0.99 C code version released to UGST <info@uniis.kiev.ua> 10.Mar.97 1.00 Conversion to UGST format <simao.campos@comsat.com> ----------------------------------------------------------------------------*/shortg727_decode_sample(short code, short law, short cbits, short ebits, g727_state *st){ Int8 ic; Int8 al; Int16 y; Int32 yl; short rt; Int16 dqfb; Int16 dqff; Int16 se; Int16 sez; Int16 srfb; Int8 pk0; Int8 sigpk; Int16 a2p; Int16 srff; Int8 tr; Int8 tdp; Int8 sd; assert(2 <= cbits && cbits <= 4 && ebits >= 0 && cbits + ebits <= 5); ic = g727_get_ic((Int8)code, ebits); al = g727_get_al(&st->asc); y = g727_get_y(&st->qsfa, al); yl = g727_get_yl(&st->qsfa); rt = cbits + ebits; dqfb = g727_get_dq(y, ic, cbits); dqff = g727_get_dq(y, (Int8)code, rt); g727_get_se_sez(&st->aprsc, &se, &sez); srfb = g727_get_sr(dqfb, se); a2p = g727_get_a2p(&st->aprsc, dqfb, sez, &pk0, &sigpk); srff = g727_get_srff(dqff, se); tr = g727_get_tr(&st->ttd, yl, dqfb); tdp = g727_get_tdp(a2p); sd = g727_get_sd(srff, se, (Int8)code, y, law, rt); g727_qsfa_transit(&st->qsfa, ic, y, cbits);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -