diff options
Diffstat (limited to 'opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src')
66 files changed, 20399 insertions, 0 deletions
diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/a_refl.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/a_refl.cpp new file mode 100644 index 0000000..674094f --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/a_refl.cpp @@ -0,0 +1,283 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: a_refl.cpp + Functions: a_refl + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "a_refl.h" +#include "typedef.h" +#include "cnst.h" +#include "basic_op.h" + +/*---------------------------------------------------------------------------- +; MACROS [optional] +; [Define module specific macros here] +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES [optional] +; [Include all pre-processor statements here. Include conditional +; compile variables also.] +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; [List function prototypes here] +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; [Variable declaration - defined here and used outside this module] +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: AMREncode +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + a[] = pointer to directform coefficients of type Word16 + refl[] = pointer to reflection coefficients of type Word16 + + Outputs: + pOverflow = 1 if overflow exists in the math operations else zero. + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + File : a_refl.c + Purpose : Convert from direct form coefficients to + reflection coefficients + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + [1] a_refl.c , 3GPP TS 26.101 version 4.1.0 Release 4, June 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +void A_Refl( + Word16 a[], // i : Directform coefficients + Word16 refl[] // o : Reflection coefficients +) +{ + // local variables + Word16 i,j; + Word16 aState[M]; + Word16 bState[M]; + Word16 normShift; + Word16 normProd; + Word32 L_acc; + Word16 scale; + Word32 L_temp; + Word16 temp; + Word16 mult; + + // initialize states + for (i = 0; i < M; i++) + { + aState[i] = a[i]; + } + + // backward Levinson recursion + for (i = M-1; i >= 0; i--) + { + if (sub(abs_s(aState[i]), 4096) >= 0) + { + goto ExitRefl; + } + + refl[i] = shl(aState[i], 3); + + L_temp = L_mult(refl[i], refl[i]); + L_acc = L_sub(MAX_32, L_temp); + + normShift = norm_l(L_acc); + scale = sub(15, normShift); + + L_acc = L_shl(L_acc, normShift); + normProd = pv_round(L_acc); + + mult = div_s(16384, normProd); + + for (j = 0; j < i; j++) + { + L_acc = L_deposit_h(aState[j]); + L_acc = L_msu(L_acc, refl[i], aState[i-j-1]); + + temp = pv_round(L_acc); + L_temp = L_mult(mult, temp); + L_temp = L_shr_r(L_temp, scale); + + if (L_sub(L_abs(L_temp), 32767) > 0) + { + goto ExitRefl; + } + + bState[j] = extract_l(L_temp); + } + + for (j = 0; j < i; j++) + { + aState[j] = bState[j]; + } + } + return; + +ExitRefl: + for (i = 0; i < M; i++) + { + refl[i] = 0; + } +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void A_Refl( + Word16 a[], /* i : Directform coefficients */ + Word16 refl[], /* o : Reflection coefficients */ + Flag *pOverflow +) +{ + /* local variables */ + Word16 i; + Word16 j; + Word16 aState[M]; + Word16 bState[M]; + Word16 normShift; + Word16 normProd; + Word32 L_acc; + Word16 scale; + Word32 L_temp; + Word16 temp; + Word16 mult; + + /* initialize states */ + for (i = 0; i < M; i++) + { + aState[i] = a[i]; + } + + /* backward Levinson recursion */ + for (i = M - 1; i >= 0; i--) + { + if (abs_s(aState[i]) >= 4096) + { + for (i = 0; i < M; i++) + { + refl[i] = 0; + } + break; + } + + refl[i] = shl(aState[i], 3, pOverflow); + + L_temp = L_mult(refl[i], refl[i], pOverflow); + L_acc = L_sub(MAX_32, L_temp, pOverflow); + + normShift = norm_l(L_acc); + scale = 15 - normShift; + + L_acc = L_shl(L_acc, normShift, pOverflow); + normProd = pv_round(L_acc, pOverflow); + + mult = div_s(16384, normProd); + + for (j = 0; j < i; j++) + { + L_acc = ((Word32)aState[j] << 16); + L_acc = L_msu(L_acc, refl[i], aState[i-j-1], pOverflow); + + temp = pv_round(L_acc, pOverflow); + L_temp = L_mult(mult, temp, pOverflow); + L_temp = L_shr_r(L_temp, scale, pOverflow); + + + Word32 L_tmp_abs = L_temp - (L_temp < 0); + L_tmp_abs = L_tmp_abs ^(L_tmp_abs >> 31); + + if (L_tmp_abs > 32767) + { + for (i = 0; i < M; i++) + { + refl[i] = 0; + } + break; + } + + bState[j] = (Word16)(L_temp); + } + + for (j = 0; j < i; j++) + { + aState[j] = bState[j]; + } + } + return; +} + + + + + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/a_refl.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/a_refl.h new file mode 100644 index 0000000..d16e572 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/a_refl.h @@ -0,0 +1,115 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: a_refl.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : a_refl.h + Purpose : Convert from direct form coefficients to + reflection coefficients + +------------------------------------------------------------------------------ +*/ + +#ifndef A_REFL_H +#define A_REFL_H +#define a_refl_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + /* + * FUNCTION: A_Refl() + * PURPOSE: Convert from direct form coefficients to reflection coefficients + * DESCRIPTION: + * Directform coeffs in Q12 are converted to + * reflection coefficients Q15 + */ + void A_Refl( + Word16 a[], /* i : Directform coefficients */ + Word16 refl[], /* o : Reflection coefficients */ + Flag *pOverflow + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _A_REFL_H_ */ + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/agc.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/agc.cpp new file mode 100644 index 0000000..c06c24a --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/agc.cpp @@ -0,0 +1,954 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: agc.cpp + Funtions: energy_old + energy_new + agc_init + agc_reset + agc_exit + agc + agc2 + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + This set of modules scale the excitation level and output of the speech + signals. + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ + +#include "agc.h" +#include "cnst.h" +#include "inv_sqrt.h" +#include "basic_op.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: energy_old +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + in = input signal (Word16) + l_trm = input signal length (Word16) + pOverflow = address of overflow (Flag) + + Outputs: + pOverflow -> 1 if the energy computation saturates + + Returns: + s = return energy of signal (Word32) + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Returns the energy of the signal. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +static Word32 energy_old( // o : return energy of signal + Word16 in[], // i : input signal (length l_trm) + Word16 l_trm // i : signal length +) +{ + Word32 s; + Word16 i, temp; + + temp = shr (in[0], 2); + s = L_mult (temp, temp); + + for (i = 1; i < l_trm; i++) + { + temp = shr (in[i], 2); + s = L_mac (s, temp, temp); + } + + return s; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +static Word32 energy_old( /* o : return energy of signal */ + Word16 in[], /* i : input signal (length l_trm) */ + Word16 l_trm, /* i : signal length */ + Flag *pOverflow /* overflow: flag to indicate overflow */ +) + +{ + Word32 s = 0; + Word16 i; + Word16 temp; + + for (i = 0; i < l_trm; i++) + { + temp = in[i] >> 2; + s = L_mac(s, temp, temp, pOverflow); + } + + return(s); +} + +/*----------------------------------------------------------------------------*/ +/* +------------------------------------------------------------------------------ + FUNCTION NAME: energy_old__Wrapper +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + in = input signal (Word16) + l_trm = input signal length (Word16) + pOverflow = address of overflow (Flag) + Outputs: + pOverflow -> 1 if the energy computation saturates + + Returns: + s = return energy of signal (Word32) + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function provides external access to the static function energy_old. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + None + +------------------------------------------------------------------------------ + PSEUDO-CODE + + CALL energy_old ( in = in + l_trm = l_trm + pOverflow = pOverflow ) + MODIFYING(nothing) + RETURNING(energy_old_value = s) + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word32 energy_old_Wrapper(Word16 in[], Word16 l_trm, Flag *pOverflow) +{ + Word32 energy_old_value; + + /*---------------------------------------------------------------------------- + CALL energy_old ( in = in + l_trm = l_trm + pOverflow = pOverflow ) + + MODIFYING(nothing) + RETURNING(energy_old_value = s) + ----------------------------------------------------------------------------*/ + energy_old_value = energy_old(in, l_trm, pOverflow); + return(energy_old_value); +} +/*--------------------------------------------------------------------------*/ + +/* +----------------------------------------------------------------------------- + FUNCTION NAME: energy_new +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + in = input signal + l_trm = input signal length + pOverflow = address of overflow (Flag) + + Outputs: + pOverflow -> 1 if the energy computation saturates + + Returns: + s = return energy of signal + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Returns the energy of the signal. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +static Word32 energy_new( // o : return energy of signal + Word16 in[], // i : input signal (length l_trm) + Word16 l_trm ) // i : signal length + +{ + Word32 s; + Word16 i; + Flag ov_save; + + ov_save = Overflow; //save overflow flag in case energy_old + // must be called + s = L_mult(in[0], in[0]); + for (i = 1; i < l_trm; i++) + { + s = L_mac(s, in[i], in[i]); + } + + // check for overflow + if (L_sub (s, MAX_32) == 0L) + { + Overflow = ov_save; // restore overflow flag + s = energy_old (in, l_trm); // function result + } + else + { + s = L_shr(s, 4); + } + + return(s); +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +static Word32 energy_new( /* o : return energy of signal */ + Word16 in[], /* i : input signal (length l_trm) */ + Word16 l_trm, /* i : signal length */ + Flag *pOverflow /* i : overflow flag */ +) + +{ + Word32 s = 0; + Word16 i; + Flag ov_save; + + ov_save = *(pOverflow); /* save overflow flag in case energy_old */ + /* must be called */ + + + for (i = 0; i < l_trm; i++) + { + s = L_mac(s, in[i], in[i], pOverflow); + } + + /* check for overflow */ + if (s != MAX_32) + { + /* s is a sum of squares, so it won't be negative */ + s = s >> 4; + } + else + { + *(pOverflow) = ov_save; /* restore overflow flag */ + s = energy_old(in, l_trm, pOverflow); /* function result */ + } + + return (s); +} + +/*--------------------------------------------------------------------------*/ +/* +------------------------------------------------------------------------------ + FUNCTION NAME: energy_new__Wrapper +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + in = input signal (Word16) + l_trm = input signal length (Word16) + overflow = address of overflow (Flag) + + Outputs: + pOverflow -> 1 if the energy computation saturates + + Returns: + s = return energy of signal (Word32) + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function provides external access to the static function energy_new. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + None + +------------------------------------------------------------------------------ + PSEUDO-CODE + + CALL energy_new ( in = in + l_trm = l_trm + pOverflow = pOverflow ) + + MODIFYING(nothing) + + RETURNING(energy_new_value = s) + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word32 energy_new_Wrapper(Word16 in[], Word16 l_trm, Flag *pOverflow) +{ + Word32 energy_new_value; + + /*---------------------------------------------------------------------------- + CALL energy_new ( in = in + l_trm = l_trm + pOverflow = pOverflow ) + + MODIFYING(nothing) + RETURNING(energy_new_value = s) + + ----------------------------------------------------------------------------*/ + energy_new_value = energy_new(in, l_trm, pOverflow); + + return(energy_new_value); + +} + +/*--------------------------------------------------------------------------*/ + + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: agc_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a structure of type agcState + + Outputs: + Structure pointed to by state is initialized to zeros + + Returns: + Returns 0 if memory was successfully initialized, + otherwise returns -1. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Reset of agc (i.e. set state memory to 1.0). + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int agc_reset (agcState *state) +{ + if (state == (agcState *) NULL) + { + fprintf(stderr, "agc_reset: invalid parameter\n"); + return -1; + } + + state->past_gain = 4096; // initial value of past_gain = 1.0 + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 agc_reset(agcState *state) +{ + if (state == (agcState *) NULL) + { + /* fprintf(stderr, "agc_reset: invalid parameter\n"); */ + return(-1); + } + + state->past_gain = 4096; /* initial value of past_gain = 1.0 */ + + return(0); +} + +/*--------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: agc +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to agc state + sig_in = pointer to a buffer containing the postfilter input signal + sig_out = pointer to a buffer containing the postfilter output signal + agc_fac = AGC factor + l_trm = subframe size + pOverflow = pointer to the overflow flag + + Outputs: + st->past_gain = gain + buffer pointed to by sig_out contains the new postfilter output signal + pOverflow -> 1 if the agc computation saturates + + Returns: + return = 0 + + Global Variables Used: + none. + + Local Variables Needed: + none. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Scales the postfilter output on a subframe basis using: + + sig_out[n] = sig_out[n] * gain[n] + gain[n] = agc_fac * gain[n-1] + (1 - agc_fac) g_in/g_out + + where: gain[n] = gain at the nth sample given by + g_in/g_out = square root of the ratio of energy at + the input and output of the postfilter. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int agc ( + agcState *st, // i/o : agc state + Word16 *sig_in, // i : postfilter input signal (l_trm) + Word16 *sig_out, // i/o : postfilter output signal (l_trm) + Word16 agc_fac, // i : AGC factor + Word16 l_trm // i : subframe size +) +{ + Word16 i, exp; + Word16 gain_in, gain_out, g0, gain; + Word32 s; + + // calculate gain_out with exponent + s = energy_new(sig_out, l_trm); // function result + + if (s == 0) + { + st->past_gain = 0; + return 0; + } + exp = sub (norm_l (s), 1); + gain_out = pv_round (L_shl (s, exp)); + + // calculate gain_in with exponent + s = energy_new(sig_in, l_trm); // function result + + if (s == 0) + { + g0 = 0; + } + else + { + i = norm_l (s); + gain_in = pv_round (L_shl (s, i)); + exp = sub (exp, i); + + *---------------------------------------------------* + * g0 = (1-agc_fac) * sqrt(gain_in/gain_out); * + *---------------------------------------------------* + + s = L_deposit_l (div_s (gain_out, gain_in)); + s = L_shl (s, 7); // s = gain_out / gain_in + s = L_shr (s, exp); // add exponent + + s = Inv_sqrt (s); // function result + i = pv_round (L_shl (s, 9)); + + // g0 = i * (1-agc_fac) + g0 = mult (i, sub (32767, agc_fac)); + } + + // compute gain[n] = agc_fac * gain[n-1] + + (1-agc_fac) * sqrt(gain_in/gain_out) + // sig_out[n] = gain[n] * sig_out[n] + + gain = st->past_gain; + + for (i = 0; i < l_trm; i++) + { + gain = mult (gain, agc_fac); + gain = add (gain, g0); + sig_out[i] = extract_h (L_shl (L_mult (sig_out[i], gain), 3)); + } + + st->past_gain = gain; + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void agc( + agcState *st, /* i/o : agc state */ + Word16 *sig_in, /* i : postfilter input signal (l_trm) */ + Word16 *sig_out, /* i/o : postfilter output signal (l_trm) */ + Word16 agc_fac, /* i : AGC factor */ + Word16 l_trm, /* i : subframe size */ + Flag *pOverflow /* i : overflow Flag */ + +) + +{ + Word16 i; + Word16 exp; + Word16 gain_in; + Word16 gain_out; + Word16 g0; + Word16 gain; + Word32 s; + Word32 L_temp; + Word16 temp; + + Word16 *p_sig_out; + + /* calculate gain_out with exponent */ + s = energy_new(sig_out, l_trm, pOverflow); /* function result */ + + if (s == 0) + { + st->past_gain = 0; + return; + } + exp = norm_l(s) - 1; + + L_temp = L_shl(s, exp, pOverflow); + gain_out = pv_round(L_temp, pOverflow); + + /* calculate gain_in with exponent */ + s = energy_new(sig_in, l_trm, pOverflow); /* function result */ + + if (s == 0) + { + g0 = 0; + } + else + { + i = norm_l(s); + + /* L_temp = L_shl(s, i, pOverflow); */ + L_temp = s << i; + + gain_in = pv_round(L_temp, pOverflow); + + exp -= i; + + /*---------------------------------------------------* + * g0 = (1-agc_fac) * sqrt(gain_in/gain_out); * + *---------------------------------------------------*/ + + /* s = gain_out / gain_in */ + temp = div_s(gain_out, gain_in); + + /* s = L_deposit_l (temp); */ + s = (Word32) temp; + s = s << 7; + s = L_shr(s, exp, pOverflow); /* add exponent */ + + s = Inv_sqrt(s, pOverflow); /* function result */ + L_temp = s << 9; + + i = (Word16)((L_temp + (Word32) 0x00008000L) >> 16); + + /* g0 = i * (1-agc_fac) */ + temp = 32767 - agc_fac; + + g0 = (Word16)(((Word32) i * temp) >> 15); + + } + + /* compute gain[n] = agc_fac * gain[n-1] + + (1-agc_fac) * sqrt(gain_in/gain_out) */ + /* sig_out[n] = gain[n] * sig_out[n] */ + + gain = st->past_gain; + p_sig_out = sig_out; + + for (i = 0; i < l_trm; i++) + { + /* gain = mult (gain, agc_fac, pOverflow); */ + gain = (Word16)(((Word32) gain * agc_fac) >> 15); + + /* gain = add (gain, g0, pOverflow); */ + gain += g0; + + /* L_temp = L_mult (sig_out[i], gain, pOverflow); */ + L_temp = ((Word32)(*(p_sig_out)) * gain) << 1; + + *(p_sig_out++) = (Word16)(L_temp >> 13); + } + + st->past_gain = gain; + + return; +} + +/*--------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: agc2 +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + sig_in = pointer to a buffer containing the postfilter input signal + sig_out = pointer to a buffer containing the postfilter output signal + l_trm = subframe size + pOverflow = pointer to overflow flag + + Outputs: + sig_out points to a buffer containing the new scaled output signal. + pOverflow -> 1 if the agc computation saturates + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Scales the excitation on a subframe basis. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void agc2 ( + Word16 *sig_in, // i : postfilter input signal + Word16 *sig_out, // i/o : postfilter output signal + Word16 l_trm // i : subframe size +) +{ + Word16 i, exp; + Word16 gain_in, gain_out, g0; + Word32 s; + + // calculate gain_out with exponent + s = energy_new(sig_out, l_trm); // function result + + if (s == 0) + { + return; + } + exp = sub (norm_l (s), 1); + gain_out = pv_round (L_shl (s, exp)); + + // calculate gain_in with exponent + s = energy_new(sig_in, l_trm); // function result + + if (s == 0) + { + g0 = 0; + } + else + { + i = norm_l (s); + gain_in = pv_round (L_shl (s, i)); + exp = sub (exp, i); + + *---------------------------------------------------* + * g0 = sqrt(gain_in/gain_out); * + *---------------------------------------------------* + + s = L_deposit_l (div_s (gain_out, gain_in)); + s = L_shl (s, 7); // s = gain_out / gain_in + s = L_shr (s, exp); // add exponent + + s = Inv_sqrt (s); // function result + g0 = pv_round (L_shl (s, 9)); + } + + // sig_out(n) = gain(n) sig_out(n) + + for (i = 0; i < l_trm; i++) + { + sig_out[i] = extract_h (L_shl (L_mult (sig_out[i], g0), 3)); + } + + return; +} +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void agc2( + Word16 *sig_in, /* i : postfilter input signal */ + Word16 *sig_out, /* i/o : postfilter output signal */ + Word16 l_trm, /* i : subframe size */ + Flag *pOverflow /* i : overflow flag */ +) + +{ + Word16 i; + Word16 exp; + Word16 gain_in; + Word16 gain_out; + Word16 g0; + Word32 s; + Word32 L_temp; + Word16 temp; + + /* calculate gain_out with exponent */ + s = energy_new(sig_out, l_trm, pOverflow); /* function result */ + + if (s == 0) + { + return; + } + exp = norm_l(s) - 1; + L_temp = L_shl(s, exp, pOverflow); + gain_out = pv_round(L_temp, pOverflow); + + /* calculate gain_in with exponent */ + s = energy_new(sig_in, l_trm, pOverflow); /* function result */ + + if (s == 0) + { + g0 = 0; + } + else + { + i = norm_l(s); + L_temp = L_shl(s, i, pOverflow); + gain_in = pv_round(L_temp, pOverflow); + exp -= i; + + /*---------------------------------------------------* + * g0 = sqrt(gain_in/gain_out); * + *---------------------------------------------------*/ + + /* s = gain_out / gain_in */ + temp = div_s(gain_out, gain_in); + + /* s = L_deposit_l (temp); */ + s = (Word32)temp; + + if (s > (Word32) 0x00FFFFFFL) + { + s = MAX_32; + } + else if (s < (Word32) 0xFF000000L) + { + s = MIN_32; + } + else + { + s = s << 7; + } + s = L_shr(s, exp, pOverflow); /* add exponent */ + + s = Inv_sqrt(s, pOverflow); /* function result */ + + if (s > (Word32) 0x003FFFFFL) + { + L_temp = MAX_32; + } + else if (s < (Word32) 0xFFC00000L) + { + L_temp = MIN_32; + } + else + { + L_temp = s << 9; + } + g0 = pv_round(L_temp, pOverflow); + } + + /* sig_out(n) = gain(n) sig_out(n) */ + + for (i = l_trm - 1; i >= 0; i--) + { + L_temp = L_mult(sig_out[i], g0, pOverflow); + if (L_temp > (Word32) 0x0FFFFFFFL) + { + sig_out[i] = MAX_16; + } + else if (L_temp < (Word32) 0xF0000000L) + { + sig_out[i] = MIN_16; + } + else + { + sig_out[i] = (Word16)(L_temp >> 13); + } + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/agc.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/agc.h new file mode 100644 index 0000000..95cb10e --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/agc.h @@ -0,0 +1,144 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: agc.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : agc.h + Purpose : Scales the postfilter output on a subframe basis + : by automatic control of the subframe gain. + +------------------------------------------------------------------------------ +*/ + +#ifndef _AGC_H_ +#define _AGC_H_ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; [Define module specific macros here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; [Include all pre-processor statements here.] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; [Declare variables used in this module but defined elsewhere] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + Word16 past_gain; + } agcState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; [List function prototypes here] + ----------------------------------------------------------------------------*/ + /*---------------------------------------------------------------------------- + ; + ; Function : agc_reset + ; Purpose : Reset of agc (i.e. set state memory to 1.0) + ; Returns : 0 on success + ; + ----------------------------------------------------------------------------*/ + Word16 agc_reset(agcState *st); + + + /*---------------------------------------------------------------------------- + ; + ; Function : agc + ; Purpose : Scales the postfilter output on a subframe basis + ; Description : sig_out[n] = sig_out[n] * gain[n]; + ; where gain[n] is the gain at the nth sample given by + ; gain[n] = agc_fac * gain[n-1] + (1 - agc_fac) g_in/g_out + ; g_in/g_out is the square root of the ratio of energy at + ; the input and output of the postfilter. + ; + ----------------------------------------------------------------------------*/ + void agc( + agcState *st, /* i/o : agc state */ + Word16 *sig_in, /* i : postfilter input signal, (l_trm) */ + Word16 *sig_out, /* i/o : postfilter output signal, (l_trm) */ + Word16 agc_fac, /* i : AGC factor */ + Word16 l_trm, /* i : subframe size */ + Flag *pOverflow /* i : overflow flag */ + ); + + /*---------------------------------------------------------------------------- + ; + ; Function: agc2 + ; Purpose: Scales the excitation on a subframe basis + ; + ----------------------------------------------------------------------------*/ + void agc2( + Word16 *sig_in, /* i : postfilter input signal */ + Word16 *sig_out, /* i/o : postfilter output signal */ + Word16 l_trm, /* i : subframe size */ + Flag *pOverflow /* i : overflow flag */ + ); + +#ifdef __cplusplus +} +#endif + +#endif /* _AGC_H_ */ + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/amrdecode.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/amrdecode.cpp new file mode 100644 index 0000000..7a68676 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/amrdecode.cpp @@ -0,0 +1,469 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + Filename: amrdecode.cpp + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "amrdecode.h" +#include "cnst.h" +#include "typedef.h" +#include "frame.h" +#include "sp_dec.h" +#include "wmf_to_ets.h" +#include "if2_to_ets.h" +#include "frame_type_3gpp.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL STORE/BUFFER/POINTER DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: AMRDecode +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state_data = pointer to a structure (type void) + + frame_type = 3GPP frame type (enum Frame_Type_3GPP) + + speech_bits_ptr = pointer to the beginning of the raw encoded speech bits + for the current frame to be decoded (unsigned char) + + raw_pcm_buffer = pointer to the output pcm outputs array (Word16) + + input_format = input format used; valid values are AMR_WMF, AMR_IF2, + and AMR_ETS (Word16) + + Outputs: + raw_pcm_buffer contains the newly decoded linear PCM speech samples + state_data->prev_mode contains the new mode + + Returns: + byte_offset = address offset of the next frame to be processed or + error condition flag (-1) (int) + + Global Variables Used: + WmfDecBytesPerFrame = table containing the number of core AMR data bytes + used by each codec mode for WMF input format (const + int) + + If2DecBytesPerFrame = table containing the number of core AMR data bytes + used by each codec mode for IF2 input format (const + int) + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function is the top level entry function to the GSM AMR Decoder library. + + First, it checks the input format type (input_format) to determine the type + of de-formattting that needs to be done. If input_format is AMR_WMF, the input + data is in WMF (aka, non-IF2) format and the function wmf_to_ets will be + called to convert to the ETS format (1 bit/word, where 1 word = 16 bits), + and byte_offset will be updated according to the contents of WmfDecBytesPerFrame + table. + + If input_format is AMR_IF2, the input data is in IF2 format [1] and the + function if2_to_ets will be called to convert to the ETS format, and + byte_offset will be updated according to the contents of If2DecBytesPerFrame + table. + + The codec mode and receive frame type is initialized based on the incoming + frame_type. + + If input_format is AMR_ETS, the input data is in the ETS format. The receive + frame type is set to the value in the first location of the buffer pointed to + by speech_bits_ptr. Then, the encoded speech parameters in the buffer pointed + to by speech_bits is copied to dec_ets_input_bfr and the type will be changed + from unsigned char to Word16. Lastly, if the receive frame type is not + RX_NO_DATA, the mode is obtained from the buffer pointed to by + speech_bits_ptr, offset by MAX_SERIAL_SIZE+1, otherwise, the mode is set to + the previous mode (found the in state_data->prev_mode). + + If input_format is an unsupported format, byte_offset will be set to -1, to + indicate an error condition has occurred, and the function will exit. + + If there are no errors, GSMFrameDecode is called to decode a 20 ms frame. It + puts the decoded linear PCM samples in the buffer pointed to by + raw_pcm_buffer. Then, the prev_mode field of the structure pointed to by + state_data is updated to the current mode. + + This function returns the new byte_offset value to indicate the address + offset of the next speech frame to be decoded. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + [1] "AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0 + Release 4, June 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + Note: AMRSID_RXTYPE_BIT_OFFSET = 35 + AMRSID_RXMODE_BIT_OFFSET = 36 + NUM_AMRSID_RXMODE_BITS = 3 + + + // Set up Decoder state structure pointer + Speech_Decode_FrameState *decoder_state + = (Speech_Decode_FrameState *) state_data + + // Determine type of de-formatting + + // Decode WMF or IF2 frames + IF ((input_format == AMR_RX_WMF) | (input_format == AMR_RX_IF2)) + THEN + IF (input_format == AMR_RX_WMF) + THEN + // Convert incoming packetized raw WMF data to ETS format + CALL wmf_to_ets(frame_type = frame_type + input_ptr = speech_bits_ptr + output_ptr = &dec_ets_input_bfr) + MODIFYING(nothing) + RETURNING(nothing) + + // Address offset of the start of next frame + byte_offset = WmfDecBytesPerFrame[frame_type] + + ELSEIF (input_format == AMR_RX_IF2) + THEN + // Convert incoming packetized raw IF2 data to ETS format + CALL if2_to_ets(frame_type = frame_type + input_ptr = speech_bits_ptr + output_ptr = &dec_ets_input_bfr) + MODIFYING(nothing) + RETURNING(nothing) + + // Address offset of the start of next frame + byte_offset = If2DecBytesPerFrame[frame_type] + + ENDIF + + // Determine AMR codec mode and AMR RX frame type + IF (frame_type <= AMR_122) + THEN + mode = (enum Mode) frame_type; + rx_type = RX_SPEECH_GOOD; + + ELSEIF (frame_type == AMR_SID) + THEN + // Clear mode store prior to reading mode info from input buffer + mode = 0 + + FOR i = 0 TO NUM_AMRSID_RXMODE_BITS-1 + + mode |= (dec_ets_input_bfr[AMRSID_RXMODE_BIT_OFFSET+i] << i) + + ENDFOR + + IF (dec_ets_input_bfr[AMRSID_RXTYPE_BIT_OFFSET] == 0) + THEN + rx_type = RX_SID_FIRST + + ELSE + rx_type = RX_SID_UPDATE + + ENDIF + + ELSEIF ((frame_type > AMR_SID) && (frame_type < NO_DATA)) + THEN + // Use previous mode + mode = decoder_state->prev_mode + + // Unsupported SID frames + rx_type = RX_SPEECH_BAD; + + ELSE + // Use previous mode + mode = decoder_state->prev_mode + + // No data received + rx_type = RX_NO_DATA; + + ENDIF + + // Decode ETS frames + ELSEIF (input_format == AMR_RX_ETS) + THEN + // Change type of pointer to incoming raw ETS data + ets_word_ptr = (Word16 *) speech_bits_ptr + + // Get RX frame type + rx_type = (enum RXFrameType) *ets_word_ptr + ets_word_ptr = ets_word_ptr + 1 + + // Copy incoming raw ETS data to dec_ets_input_bfr + FOR i = 0 TO MAX_SERIAL_SIZE-1 + + dec_ets_input_bfr[i] = *ets_word_ptr + ets_word_ptr = ets_word_ptr + 1 + + ENDFOR + + // Get codec mode + IF (rx_type != RX_NO_DATA) + THEN + mode = (enum Mode) *ets_word_ptr + + ELSE + //Use previous mode if no received data + mode = decoder_state->prev_mode + + ENDIF + + // Set up byte_offset + byte_offset = 2*(MAX_SERIAL_SIZE+2) + + ELSE + // Invalid format, return error code + byte_offset = -1 + + ENDIF + + // Proceed with decoding frame, if there are no errors + IF (byte_offset != -1) + THEN + // Decode a 20 ms frame + CALL GSMFrameDecode( st = decoder_state + mode = mode + serial = dec_ets_input_bfr, + frame_type = rx_type, + synth = (Word16 *)raw_pcm_buffer); + MODIFYING (nothing) + RETURNING (Nothing) + + // Save mode for next frame + decoder_state->prev_mode = mode + + ENDIF + + RETURN (byte_offset) + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 AMRDecode( + void *state_data, + enum Frame_Type_3GPP frame_type, + UWord8 *speech_bits_ptr, + Word16 *raw_pcm_buffer, + bitstream_format input_format +) +{ + Word16 *ets_word_ptr; + enum Mode mode = (enum Mode)MR475; + int modeStore; + int tempInt; + enum RXFrameType rx_type = RX_NO_DATA; + Word16 dec_ets_input_bfr[MAX_SERIAL_SIZE]; + Word16 i; + Word16 byte_offset = -1; + + /* Type cast state_data to Speech_Decode_FrameState rather than passing + * that structure type to this function so the structure make up can't + * be viewed from higher level functions than this. + */ + Speech_Decode_FrameState *decoder_state + = (Speech_Decode_FrameState *) state_data; + + /* Determine type of de-formatting */ + /* WMF or IF2 frames */ + if ((input_format == MIME_IETF) | (input_format == IF2)) + { + if (input_format == MIME_IETF) + { + /* Convert incoming packetized raw WMF data to ETS format */ + wmf_to_ets(frame_type, speech_bits_ptr, dec_ets_input_bfr, &(decoder_state->decoder_amrState.common_amr_tbls)); + + /* Address offset of the start of next frame */ + byte_offset = WmfDecBytesPerFrame[frame_type]; + } + else /* else has to be input_format IF2 */ + { + /* Convert incoming packetized raw IF2 data to ETS format */ + if2_to_ets(frame_type, speech_bits_ptr, dec_ets_input_bfr, &(decoder_state->decoder_amrState.common_amr_tbls)); + + /* Address offset of the start of next frame */ + byte_offset = If2DecBytesPerFrame[frame_type]; + } + + /* At this point, input data is in ETS format */ + /* Determine AMR codec mode and AMR RX frame type */ + if (frame_type <= AMR_122) + { + mode = (enum Mode) frame_type; + rx_type = RX_SPEECH_GOOD; + } + else if (frame_type == AMR_SID) + { + /* Clear mode store prior to reading mode info from input buffer */ + modeStore = 0; + + for (i = 0; i < NUM_AMRSID_RXMODE_BITS; i++) + { + tempInt = dec_ets_input_bfr[AMRSID_RXMODE_BIT_OFFSET+i] << i; + modeStore |= tempInt; + } + mode = (enum Mode) modeStore; + + /* Get RX frame type */ + if (dec_ets_input_bfr[AMRSID_RXTYPE_BIT_OFFSET] == 0) + { + rx_type = RX_SID_FIRST; + } + else + { + rx_type = RX_SID_UPDATE; + } + } + else if (frame_type < AMR_NO_DATA) + { + /* Invalid frame_type, return error code */ + byte_offset = -1; /* !!! */ + } + else + { + mode = decoder_state->prev_mode; + + /* + * RX_NO_DATA, generate exponential decay from latest valid frame for the first 6 frames + * after that, create silent frames + */ + rx_type = RX_NO_DATA; + + } + + } + + /* ETS frames */ + else if (input_format == ETS) + { + /* Change type of pointer to incoming raw ETS data */ + ets_word_ptr = (Word16 *) speech_bits_ptr; + + /* Get RX frame type */ + rx_type = (enum RXFrameType) * ets_word_ptr; + ets_word_ptr++; + + /* Copy incoming raw ETS data to dec_ets_input_bfr */ + for (i = 0; i < MAX_SERIAL_SIZE; i++) + { + dec_ets_input_bfr[i] = *ets_word_ptr; + ets_word_ptr++; + } + + /* Get codec mode */ + if (rx_type != RX_NO_DATA) + { + /* Get mode from input bitstream */ + mode = (enum Mode) * ets_word_ptr; + } + else + { + /* Use previous mode if no received data */ + mode = decoder_state->prev_mode; + } + + /* Set up byte_offset */ + byte_offset = 2 * (MAX_SERIAL_SIZE + 2); + } + else + { + /* Invalid input format, return error code */ + byte_offset = -1; + } + + /* Proceed with decoding frame, if there are no errors */ + if (byte_offset != -1) + { + /* Decode a 20 ms frame */ + +#ifndef CONSOLE_DECODER_REF + /* Use PV version of sp_dec.c */ + GSMFrameDecode(decoder_state, mode, dec_ets_input_bfr, rx_type, + raw_pcm_buffer); + +#else + /* Use ETS version of sp_dec.c */ + Speech_Decode_Frame(decoder_state, mode, dec_ets_input_bfr, rx_type, + raw_pcm_buffer); + +#endif + + /* Save mode for next frame */ + decoder_state->prev_mode = mode; + } + + return (byte_offset); +} + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/amrdecode.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/amrdecode.h new file mode 100644 index 0000000..1715f0d --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/amrdecode.h @@ -0,0 +1,114 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: amrdecode.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the norm_s function. + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef ARMDECODE_H +#define ARMDECODE_H + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "mode.h" +#include "frame_type_3gpp.h" +#include "pvamrnbdecoder_api.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ +#define NUM_AMRSID_RXMODE_BITS 3 +#define AMRSID_RXMODE_BIT_OFFSET 36 +#define AMRSID_RXTYPE_BIT_OFFSET 35 + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + extern const Word16 WmfDecBytesPerFrame[]; + extern const Word16 If2DecBytesPerFrame[]; + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + Word16 AMRDecode( + void *state_data, + enum Frame_Type_3GPP frame_type, + UWord8 *speech_bits_ptr, + Word16 *raw_pcm_buffer, + bitstream_format input_format + ); + +#ifdef __cplusplus +} +#endif + +#endif /* _AMRDECODE_H_ */ diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/b_cn_cod.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/b_cn_cod.cpp new file mode 100644 index 0000000..f5317e6 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/b_cn_cod.cpp @@ -0,0 +1,456 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: b_cn_cod.cpp + Functions: pseudonoise + build_CN_code + build_CN_param + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + This module contains functions for comfort noise(CN) generation. + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "b_cn_cod.h" +#include "basic_op.h" +#include "cnst.h" + +/*---------------------------------------------------------------------------- +; MACROS +; [Define module specific macros here] +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; [Include all pre-processor statements here. Include conditional +; compile variables also.] +----------------------------------------------------------------------------*/ +#define NB_PULSE 10 /* number of random pulses in DTX operation */ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; [List function prototypes here] +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; [Variable declaration - defined here and used outside this module] +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: pseudonoise +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + pShift_reg = pointer to Old CN generator shift register state (Word32) + no_bits = Number of bits (Word16) + + Outputs: + pShift_reg -> Updated CN generator shift register state + + Returns: + noise_bits = Generated random integer value (Word16) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Generate a random integer value to use in comfort noise generation. The + algorithm uses polynomial x^31 + x^3 + 1. Length of the PN sequence + is 2^31 - 1 + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + b_cn_cod.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +Word16 pseudonoise ( + Word32 *shift_reg, // i/o : Old CN generator shift register state + Word16 no_bits // i : Number of bits +) +{ + Word16 noise_bits, Sn, i; + + noise_bits = 0; + for (i = 0; i < no_bits; i++) + { + // State n == 31 + if ((*shift_reg & 0x00000001L) != 0) + { + Sn = 1; + } + else + { + Sn = 0; + } + + // State n == 3 + if ((*shift_reg & 0x10000000L) != 0) + { + Sn = Sn ^ 1; + } + else + { + Sn = Sn ^ 0; + } + + noise_bits = shl (noise_bits, 1); + noise_bits = noise_bits | (extract_l (*shift_reg) & 1); + + *shift_reg = L_shr (*shift_reg, 1); + if (Sn & 1) + { + *shift_reg = *shift_reg | 0x40000000L; + } + } + return noise_bits; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ + +Word16 pseudonoise( + Word32 *pShift_reg, /* i/o : Old CN generator shift register state */ + Word16 no_bits /* i : Number of bits */ +) +{ + Word16 noise_bits; + Word16 Sn; + Word16 i; + Word16 temp; + + noise_bits = 0; + + for (i = 0; i < no_bits; i++) + { + /* State n == 31 */ + if ((*pShift_reg & 0x00000001L) != 0) + { + Sn = 1; + } + else + { + Sn = 0; + } + + /* State n == 3 */ + if ((*pShift_reg & 0x10000000L) != 0) + { + Sn ^= 1; + } + else + { + Sn ^= 0; + } + + noise_bits <<= 1; + + temp = (Word16)((*pShift_reg) & 1); + noise_bits |= temp; + + *pShift_reg >>= 1; + if (Sn & 1) + { + *pShift_reg |= 0x40000000L; + } + } + return noise_bits; +} + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: build_CN_code +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + pSeed = pointer to the Old CN generator shift register state (Word32) + cod[] = array to hold the generated CN fixed code vector (Word16) + pOverflow = pointer to overflow flag (Flag) + + Outputs: + cod[] = generated CN fixed code vector (Word16) + pSeed = Updated CN generator shift register state (Word16) + pOverflow -> 1 if overflow occured + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + +This function computes the comfort noise fixed codebook excitation. The gains +of the pulses are always +/-1. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + b_cn_cod.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void build_CN_code ( + Word32 *seed, // i/o : Old CN generator shift register state + Word16 cod[] // o : Generated CN fixed codebook vector +) +{ + Word16 i, j, k; + + for (i = 0; i < L_SUBFR; i++) + { + cod[i] = 0; + } + +// The reference ETSI code uses a global flag for Overflow. However in the +// actual implementation a pointer to the overflow flag is passed into the +// function so that it can be passed into the basic math functions L_mult() +// and add() + + for (k = 0; k < NB_PULSE; k++) + { + i = pseudonoise (seed, 2); // generate pulse position + i = shr (extract_l (L_mult (i, 10)), 1); + i = add (i, k); + + j = pseudonoise (seed, 1); // generate sign + + if (j > 0) + { + cod[i] = 4096; + } + else + { + cod[i] = -4096; + } + } + + return; +} +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ +void build_CN_code( + Word32 *pSeed, /* i/o : Old CN generator shift register state */ + Word16 cod[], /* o : Generated CN fixed codebook vector */ + Flag *pOverflow /* i/o : Overflow flag */ +) +{ + Word16 i, j, k; + Word16 temp; + + for (i = 0; i < L_SUBFR; i++) + { + cod[i] = 0; + } + + for (k = 0; k < NB_PULSE; k++) + { + i = pseudonoise(pSeed, 2); /* generate pulse position */ + + temp = (Word16)(L_mult(i, 10, pOverflow)); + i = temp >> 1; + i = add_16(i, k, pOverflow); + + j = pseudonoise(pSeed, 1); /* generate sign */ + + if (j > 0) + { + cod[i] = 4096; + } + else + { + cod[i] = -4096; + } + } + + return; +} + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: build_CN_param +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + pSeed = pointer to the Old CN generator shift register state (Word32) + n_param = Number of parameters to randomize (Word16) + param_size_table = table holding paameter sizes (Word16) + param[] = array to hold CN generated paramters (Word16) + pOverflow = pointer to overflow flag (Flag) + + Outputs: + param[] = CN generated parameters (Word16) + pSeed = Updated CN generator shift register state (Word16) + pOverflow -> 1 if overflow occured + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + +This function randomizes the speech parameters, so that they do not produce +tonal artifacts if used by ECU. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + b_cn_cod.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE +void build_CN_param ( + Word16 *seed, // i/o : Old CN generator shift register state + const Word16 n_param, // i : number of params + const Word16 param_size_table[],// i : size of params + Word16 parm[] // o : CN Generated params + ) +{ + Word16 i; + const Word16 *p; + +// The reference ETSI code uses a global flag for Overflow. However in the +// actual implementation a pointer to the overflow flag is passed into the +// function so that it can be passed into the basic math functions L_add() +// and L_mult() + + *seed = extract_l(L_add(L_shr(L_mult(*seed, 31821), 1), 13849L)); + + p = &window_200_40[*seed & 0x7F]; + for(i=0; i< n_param;i++){ + parm[i] = *p++ & ~(0xFFFF<<param_size_table[i]); + } +} + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ +void build_CN_param( + Word16 *pSeed, /* i/o : Old CN generator shift register state */ + const Word16 n_param, /* i : number of params */ + const Word16 param_size_table[],/* i : size of params */ + Word16 parm[], /* o : CN Generated params */ + const Word16* window_200_40_ptr, /* i : ptr to read-only table */ + Flag *pOverflow /* i/o : Overflow Flag */ +) + +{ + Word16 i; + const Word16 *pTemp; + Word32 L_temp; + Word16 temp; + + L_temp = L_mult(*pSeed, 31821, pOverflow); + L_temp >>= 1; + + *pSeed = (Word16)(L_add(L_temp, 13849L, pOverflow)); + + pTemp = &window_200_40_ptr[*pSeed & 0x7F]; + + for (i = 0; i < n_param; i++) + { + temp = ~(0xFFFF << param_size_table[i]); + parm[i] = *pTemp++ & temp; + } +} + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/b_cn_cod.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/b_cn_cod.h new file mode 100644 index 0000000..aec7c7c --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/b_cn_cod.h @@ -0,0 +1,147 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: b_cn_cod.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the comfort noise(CN) generator functions + +------------------------------------------------------------------------------ +*/ + +#ifndef B_CN_COD_H +#define B_CN_COD_H + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "basicop_malloc.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + + ; FUNCTION NAME: pseudonoise + ; + ; PURPOSE: Generate a random integer value to use in comfort noise + ; generation. The algorithm uses polynomial x^31 + x^3 + 1 + ; (length of PN sequence is 2^31 - 1). + ; + ----------------------------------------------------------------------------*/ + + Word16 pseudonoise( + Word32 *pShift_reg, /* i/o : Old CN generator shift register state */ + Word16 no_bits /* i : Number of bits */ + ); + + /*---------------------------------------------------------------------------- + + ; FUNCTION NAME: build_CN_code + ; + ; PURPOSE: Compute the comfort noise fixed codebook excitation. The + ; gains of the pulses are always +/-1. + ; + ----------------------------------------------------------------------------*/ + + void build_CN_code( + Word32 *pSeed, /* i/o : Old CN generator shift register state */ + Word16 cod[], /* o : Generated CN fixed codebook vector */ + Flag *pOverflow /* i/o : Overflow flag */ + ); + + /*---------------------------------------------------------------------------- + + ; FUNCTION NAME: build_CN_param + ; + ; PURPOSE: Randomize the speech parameters. So that they + ; do not produce tonal artifacts if used by ECU. + ; + ----------------------------------------------------------------------------*/ + + void build_CN_param( + Word16 *pSeed, /* i/o : Old CN generator shift register state */ + const Word16 n_param, /* i : number of params */ + const Word16 param_size_table[], /* i : size of params */ + Word16 parm[], /* o : CN Generated params */ + const Word16* window_200_40_ptr, /* i : ptr to read-only table */ + Flag *pOverflow /* i/o : Overflow Flag */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _B_CN_COD_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/bgnscd.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/bgnscd.cpp new file mode 100644 index 0000000..e14dbcd --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/bgnscd.cpp @@ -0,0 +1,555 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + Filename: bgnscd.cpp + Functions: + Bgn_scd_reset + Bgn_scd + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + Background noise source characteristic detector (SCD) + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "bgnscd.h" +#include "typedef.h" +#include "basic_op.h" +#include "cnst.h" +#include "gmed_n.h" +#include "sqrt_l.h" +#include "oscl_mem.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ +#define TRUE 1 +#define FALSE 0 + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Bgn_scd_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = points to memory of type Bgn_scdState. + + Outputs: + The memory of type Bgn_scdState pointed to by state is set to all + zeros. + + Returns: + Returns 0 if memory was successfully initialized, + otherwise returns -1. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Resets state memory. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + bgnscd.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +Word16 Bgn_scd_reset (Bgn_scdState *state) +{ + if (state == (Bgn_scdState *) NULL){ + fprintf(stderr, "Bgn_scd_reset: invalid parameter\n"); + return -1; + } + + // Static vectors to zero + Set_zero (state->frameEnergyHist, L_ENERGYHIST); + + // Initialize hangover handling + state->bgHangover = 0; + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 Bgn_scd_reset(Bgn_scdState *state) +{ + if (state == (Bgn_scdState *) NULL) + { + /* fprintf(stderr, "Bgn_scd_reset: invalid parameter\n"); */ + return(-1); + } + + /* Static vectors to zero */ + oscl_memset(state->frameEnergyHist, 0, L_ENERGYHIST*sizeof(Word16)); + + /* Initialize hangover handling */ + state->bgHangover = 0; + + return(0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Bgn_scd +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to state variables of type Bgn_scdState + ltpGainHist[] = LTP gain history (Word16) + speech[] = synthesis speech frame (Word16) + voicedHangover = pointer to # of frames after last voiced frame (Word16) + pOverflow = pointer to overflow indicator (Flag) + + Outputs: + st = function updates the state variables of type Bgn_scdState + pointed to by st. + voicedHangover = function updates the # of frames after last voiced + frame pointed to by voicedHangover. + pOverflow = 1 if the basic math function L_add() results in saturation. + else pOverflow is zero. + + Returns: + inbgNoise = flag if background noise is present (Word16) + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Characterize synthesis speech and detect background noise. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + bgnscd.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +Word16 Bgn_scd (Bgn_scdState *st, // i : State variables for bgn SCD + Word16 ltpGainHist[], // i : LTP gain history + Word16 speech[], // o : synthesis speech frame + Word16 *voicedHangover // o : # of frames after last + voiced frame + ) +{ + Word16 i; + Word16 prevVoiced, inbgNoise; + Word16 temp; + Word16 ltpLimit, frameEnergyMin; + Word16 currEnergy, noiseFloor, maxEnergy, maxEnergyLastPart; + Word32 s; + + // Update the inBackgroundNoise flag (valid for use in next frame if BFI) + // it now works as a energy detector floating on top + // not as good as a VAD. + + currEnergy = 0; + s = (Word32) 0; + + for (i = 0; i < L_FRAME; i++) + { + s = L_mac (s, speech[i], speech[i]); + } + + s = L_shl(s, 2); + + currEnergy = extract_h (s); + + frameEnergyMin = 32767; + + for (i = 0; i < L_ENERGYHIST; i++) + { + if (sub(st->frameEnergyHist[i], frameEnergyMin) < 0) + frameEnergyMin = st->frameEnergyHist[i]; + } + + noiseFloor = shl (frameEnergyMin, 4); // Frame Energy Margin of 16 + + maxEnergy = st->frameEnergyHist[0]; + for (i = 1; i < L_ENERGYHIST-4; i++) + { + if ( sub (maxEnergy, st->frameEnergyHist[i]) < 0) + { + maxEnergy = st->frameEnergyHist[i]; + } + } + + maxEnergyLastPart = st->frameEnergyHist[2*L_ENERGYHIST/3]; + for (i = 2*L_ENERGYHIST/3+1; i < L_ENERGYHIST; i++) + { + if ( sub (maxEnergyLastPart, st->frameEnergyHist[i] ) < 0) + { + maxEnergyLastPart = st->frameEnergyHist[i]; + } + } + + inbgNoise = 0; // false + + // Do not consider silence as noise + // Do not consider continuous high volume as noise + // Or if the current noise level is very low + // Mark as noise if under current noise limit + // OR if the maximum energy is below the upper limit + + if ( (sub(maxEnergy, LOWERNOISELIMIT) > 0) && + (sub(currEnergy, FRAMEENERGYLIMIT) < 0) && + (sub(currEnergy, LOWERNOISELIMIT) > 0) && + ( (sub(currEnergy, noiseFloor) < 0) || + (sub(maxEnergyLastPart, UPPERNOISELIMIT) < 0))) + { + if (sub(add(st->bgHangover, 1), 30) > 0) + { + st->bgHangover = 30; + } else + { + st->bgHangover = add(st->bgHangover, 1); + } + } + else + { + st->bgHangover = 0; + } + + // make final decision about frame state , act somewhat cautiosly + if (sub(st->bgHangover,1) > 0) + inbgNoise = 1; // true + + for (i = 0; i < L_ENERGYHIST-1; i++) + { + st->frameEnergyHist[i] = st->frameEnergyHist[i+1]; + } + st->frameEnergyHist[L_ENERGYHIST-1] = currEnergy; + + // prepare for voicing decision; tighten the threshold after some + time in noise + ltpLimit = 13926; // 0.85 Q14 + if (sub(st->bgHangover, 8) > 0) + { + ltpLimit = 15565; // 0.95 Q14 + } + if (sub(st->bgHangover, 15) > 0) + { + ltpLimit = 16383; // 1.00 Q14 + } + + // weak sort of voicing indication. + prevVoiced = 0; // false + + if (sub(gmed_n(<pGainHist[4], 5), ltpLimit) > 0) + { + prevVoiced = 1; // true + } + if (sub(st->bgHangover, 20) > 0) { + if (sub(gmed_n(ltpGainHist, 9), ltpLimit) > 0) + { + prevVoiced = 1; // true + } + else + { + prevVoiced = 0; // false + } + } + + if (prevVoiced) + { + *voicedHangover = 0; + } + else + { + temp = add(*voicedHangover, 1); + if (sub(temp, 10) > 0) + { + *voicedHangover = 10; + } + else + { + *voicedHangover = temp; + } + } + + return inbgNoise; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 Bgn_scd(Bgn_scdState *st, /* i : State variables for bgn SCD */ + Word16 ltpGainHist[], /* i : LTP gain history */ + Word16 speech[], /* o : synthesis speech frame */ + Word16 *voicedHangover,/* o : # of frames after last + voiced frame */ + Flag *pOverflow + ) +{ + Word16 i; + Word16 prevVoiced, inbgNoise; + Word16 temp; + Word16 ltpLimit, frameEnergyMin; + Word16 currEnergy, noiseFloor, maxEnergy, maxEnergyLastPart; + Word32 s, L_temp; + + + /* Update the inBackgroundNoise flag (valid for use in next frame if BFI) */ + /* it now works as a energy detector floating on top */ + /* not as good as a VAD. */ + + s = (Word32) 0; + + for (i = L_FRAME - 1; i >= 0; i--) + { + L_temp = ((Word32) speech[i]) * speech[i]; + if (L_temp != (Word32) 0x40000000L) + { + L_temp = L_temp << 1; + } + else + { + L_temp = MAX_32; + } + s = L_add(s, L_temp, pOverflow); + } + + /* s is a sum of squares, so don't need to check for neg overflow */ + if (s > (Word32)0x1fffffffL) + { + currEnergy = MAX_16; + } + else + { + currEnergy = (Word16)(s >> 14); + } + + frameEnergyMin = 32767; + for (i = L_ENERGYHIST - 1; i >= 0; i--) + { + if (st->frameEnergyHist[i] < frameEnergyMin) + { + frameEnergyMin = st->frameEnergyHist[i]; + } + } + + /* Frame Energy Margin of 16 */ + L_temp = (Word32)frameEnergyMin << 4; + if (L_temp != (Word32)((Word16) L_temp)) + { + if (L_temp > 0) + { + noiseFloor = MAX_16; + } + else + { + noiseFloor = MIN_16; + } + } + else + { + noiseFloor = (Word16)(L_temp); + } + + maxEnergy = st->frameEnergyHist[0]; + for (i = L_ENERGYHIST - 5; i >= 1; i--) + { + if (maxEnergy < st->frameEnergyHist[i]) + { + maxEnergy = st->frameEnergyHist[i]; + } + } + + maxEnergyLastPart = st->frameEnergyHist[2*L_ENERGYHIST/3]; + for (i = 2 * L_ENERGYHIST / 3 + 1; i < L_ENERGYHIST; i++) + { + if (maxEnergyLastPart < st->frameEnergyHist[i]) + { + maxEnergyLastPart = st->frameEnergyHist[i]; + } + } + + /* Do not consider silence as noise */ + /* Do not consider continuous high volume as noise */ + /* Or if the current noise level is very low */ + /* Mark as noise if under current noise limit */ + /* OR if the maximum energy is below the upper limit */ + + if ((maxEnergy > LOWERNOISELIMIT) && + (currEnergy < FRAMEENERGYLIMIT) && + (currEnergy > LOWERNOISELIMIT) && + ((currEnergy < noiseFloor) || + (maxEnergyLastPart < UPPERNOISELIMIT))) + { + if ((st->bgHangover + 1) > 30) + { + st->bgHangover = 30; + } + else + { + st->bgHangover += 1; + } + } + else + { + st->bgHangover = 0; + } + + /* make final decision about frame state , act somewhat cautiosly */ + + if (st->bgHangover > 1) + { + inbgNoise = TRUE; + } + else + { + inbgNoise = FALSE; + } + + for (i = 0; i < L_ENERGYHIST - 1; i++) + { + st->frameEnergyHist[i] = st->frameEnergyHist[i+1]; + } + st->frameEnergyHist[L_ENERGYHIST-1] = currEnergy; + + /* prepare for voicing decision; tighten the threshold after some + time in noise */ + + if (st->bgHangover > 15) + { + ltpLimit = 16383; /* 1.00 Q14 */ + } + else if (st->bgHangover > 8) + { + ltpLimit = 15565; /* 0.95 Q14 */ + } + else + { + ltpLimit = 13926; /* 0.85 Q14 */ + } + + /* weak sort of voicing indication. */ + prevVoiced = FALSE; + + if (gmed_n(<pGainHist[4], 5) > ltpLimit) + { + prevVoiced = TRUE; + } + + if (st->bgHangover > 20) + { + if (gmed_n(ltpGainHist, 9) > ltpLimit) + { + prevVoiced = TRUE; + } + else + { + prevVoiced = FALSE; + } + } + + + if (prevVoiced) + { + *voicedHangover = 0; + } + else + { + temp = *voicedHangover + 1; + + if (temp > 10) + { + *voicedHangover = 10; + } + else + { + *voicedHangover = temp; + } + } + + return(inbgNoise); +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/bgnscd.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/bgnscd.h new file mode 100644 index 0000000..f98eab9 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/bgnscd.h @@ -0,0 +1,150 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: bgnscd.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : bgnscd.h + Purpose : Background noise source charateristic detector (SCD) + +------------------------------------------------------------------------------ +*/ + +#ifndef _BGNSCD_H_ +#define _BGNSCD_H_ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "cnst.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; [Define module specific macros here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; [Include all pre-processor statements here.] + ----------------------------------------------------------------------------*/ +#define L_ENERGYHIST 60 +#define INV_L_FRAME 102 + + + /* 2*(160*x)^2 / 65536 where x is FLP values 150,5 and 50 */ +#define FRAMEENERGYLIMIT 17578 /* 150 */ +#define LOWERNOISELIMIT 20 /* 5 */ +#define UPPERNOISELIMIT 1953 /* 50 */ + + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; [Declare variables used in this module but defined elsewhere] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + /* history vector of past synthesis speech energy */ + Word16 frameEnergyHist[L_ENERGYHIST]; + + /* state flags */ + Word16 bgHangover; /* counter; number of frames after last speech frame */ + + } Bgn_scdState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; [List function prototypes here] + ----------------------------------------------------------------------------*/ + /* + * Function : Bgn_scd_init + * Purpose : Allocates initializes state memory + * Description : Stores pointer to filter status struct in *st. This + * pointer has to be passed to Bgn_scd in each call. + * Returns : 0 on success + */ + Word16 Bgn_scd_init(Bgn_scdState **st); + + /* + * Function : Bgn_scd_reset + * Purpose : Resets state memory + * Returns : 0 on success + */ + Word16 Bgn_scd_reset(Bgn_scdState *st); + + /* + * Function : Bgn_scd_exit + * Purpose : The memory used for state memory is freed + * Description : Stores NULL in *s + * Returns : void + */ + void Bgn_scd_exit(Bgn_scdState **st); + + /* + * Function : Bgn_scd + * Purpose : Charaterice synthesis speech and detect background noise + * Returns : background noise decision; 0 = bgn, 1 = no bgn + */ + Word16 Bgn_scd(Bgn_scdState *st, /* i : State variables for bgn SCD */ + Word16 ltpGainHist[], /* i : LTP gain history */ + Word16 speech[], /* o : synthesis speech frame */ + Word16 *voicedHangover,/* o : # of frames after last voiced frame */ + Flag *pOverflow + ); + + +#ifdef __cplusplus +} +#endif + +#endif /* _BGNSCD_H_ */ diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/c_g_aver.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/c_g_aver.cpp new file mode 100644 index 0000000..7a86760 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/c_g_aver.cpp @@ -0,0 +1,584 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: c_g_aver.cpp + Functions: + Cb_gain_average_reset + Cb_gain_average + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + This file contains functions that reset and perform + codebook gain calculations. + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "c_g_aver.h" +#include "typedef.h" +#include "mode.h" +#include "cnst.h" + +#include "basic_op.h" +#include "oscl_mem.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Cb_gain_average_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a structure of type Cb_gain_averageState + + Outputs: + Structure pointed to by state is initialized to zeros + + Returns: + Returns 0 if memory was successfully initialized, + otherwise returns -1. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Resets state memory + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + c_g_aver.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +Word16 Cb_gain_average_reset (Cb_gain_averageState *state) +{ + if (state == (Cb_gain_averageState *) NULL){ + fprintf(stderr, "Cb_gain_average_reset: invalid parameter\n"); + return -1; + } + + // Static vectors to zero + Set_zero (state->cbGainHistory, L_CBGAINHIST); + + // Initialize hangover handling + state->hangVar = 0; + state->hangCount= 0; + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 Cb_gain_average_reset(Cb_gain_averageState *state) +{ + if (state == (Cb_gain_averageState *) NULL) + { + /* fprint(stderr, "Cb_gain_average_reset: invalid parameter\n"); */ + return(-1); + } + + /* Static vectors to zero */ + oscl_memset(state->cbGainHistory, 0, L_CBGAINHIST*sizeof(Word16)); + + /* Initialize hangover handling */ + state->hangVar = 0; + state->hangCount = 0; + + return(0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Cb_gain_average +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to structure of type Cb_gain_averageState + mode = AMR mode (enum Mode) + gain_code = CB gain (Word16) + lsp = the LSP for the current frame (Word16) + lspAver = the average of LSP for 8 frames (Word16) + bfi = bad frame indication flag (Word16) + prev_bf = previous bad frame indication flag (Word16) + pdfi = potential degraded bad frame ind flag (Word16) + prev_pdf = prev pot. degraded bad frame ind flag (Word16) + inBackgroundNoise = background noise decision (Word16) + voicedHangover = # of frames after last voiced frame (Word16) + pOverflow = address of overflow (Flag) + + Returns: + cbGainMix = codebook gain (Word16) + + Outputs: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + The mix cb gains for MR475, MR515, MR59, MR67, MR102; gain_code other modes + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + c_g_aver.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +Word16 Cb_gain_average ( + Cb_gain_averageState *st, // i/o : State variables for CB gain avergeing + enum Mode mode, // i : AMR mode + Word16 gain_code, // i : CB gain Q1 + Word16 lsp[], // i : The LSP for the current frame Q15 + Word16 lspAver[], // i : The average of LSP for 8 frames Q15 + Word16 bfi, // i : bad frame indication flag + Word16 prev_bf, // i : previous bad frame indication flag + Word16 pdfi, // i : potential degraded bad frame ind flag + Word16 prev_pdf, // i : prev pot. degraded bad frame ind flag + Word16 inBackgroundNoise, // i : background noise decision + Word16 voicedHangover // i : # of frames after last voiced frame + ) +{ + //---------------------------------------------------------* + * Compute mixed cb gain, used to make cb gain more * + * smooth in background noise for modes 5.15, 5.9 and 6.7 * + * states that needs to be updated by all * + *--------------------------------------------------------- + Word16 i; + Word16 cbGainMix, diff, tmp_diff, bgMix, cbGainMean; + Word32 L_sum; + Word16 tmp[M], tmp1, tmp2, shift1, shift2, shift; + + // set correct cbGainMix for MR74, MR795, MR122 + cbGainMix = gain_code; + + *-------------------------------------------------------* + * Store list of CB gain needed in the CB gain * + * averaging * + *-------------------------------------------------------* + for (i = 0; i < (L_CBGAINHIST-1); i++) + { + st->cbGainHistory[i] = st->cbGainHistory[i+1]; + } + st->cbGainHistory[L_CBGAINHIST-1] = gain_code; + + // compute lsp difference + for (i = 0; i < M; i++) { + tmp1 = abs_s(sub(lspAver[i], lsp[i])); // Q15 + shift1 = sub(norm_s(tmp1), 1); // Qn + tmp1 = shl(tmp1, shift1); // Q15+Qn + shift2 = norm_s(lspAver[i]); // Qm + tmp2 = shl(lspAver[i], shift2); // Q15+Qm + tmp[i] = div_s(tmp1, tmp2); // Q15+(Q15+Qn)-(Q15+Qm) + shift = sub(add(2, shift1), shift2); + if (shift >= 0) + { + tmp[i] = shr(tmp[i], shift); // Q15+Qn-Qm-Qx=Q13 + } + else + { + tmp[i] = shl(tmp[i], negate(shift)); // Q15+Qn-Qm-Qx=Q13 + } + } + + diff = tmp[0]; + for (i = 1; i < M; i++) { + diff = add(diff, tmp[i]); // Q13 + } + + // Compute hangover + if (sub(diff, 5325) > 0) // 0.65 in Q11 + { + st->hangVar = add(st->hangVar, 1); + } + else + { + st->hangVar = 0; + } + + if (sub(st->hangVar, 10) > 0) + { + st->hangCount = 0; // Speech period, reset hangover variable + } + + // Compute mix constant (bgMix) + bgMix = 8192; // 1 in Q13 + if ((sub(mode, MR67) <= 0) || (sub(mode, MR102) == 0)) + // MR475, MR515, MR59, MR67, MR102 + { + // if errors and presumed noise make smoothing probability stronger + if (((((pdfi != 0) && (prev_pdf != 0)) || (bfi != 0) || (prev_bf != 0)) && + (sub(voicedHangover, 1) > 0) && (inBackgroundNoise != 0) && + ((sub(mode, MR475) == 0) || + (sub(mode, MR515) == 0) || + (sub(mode, MR59) == 0)) )) + { + // bgMix = min(0.25, max(0.0, diff-0.55)) / 0.25; + tmp_diff = sub(diff, 4506); // 0.55 in Q13 + + // max(0.0, diff-0.55) + if (tmp_diff > 0) + { + tmp1 = tmp_diff; + } + else + { + tmp1 = 0; + } + + // min(0.25, tmp1) + if (sub(2048, tmp1) < 0) + { + bgMix = 8192; + } + else + { + bgMix = shl(tmp1, 2); + } + } + else + { + // bgMix = min(0.25, max(0.0, diff-0.40)) / 0.25; + tmp_diff = sub(diff, 3277); // 0.4 in Q13 + + // max(0.0, diff-0.40) + if (tmp_diff > 0) + { + tmp1 = tmp_diff; + } + else + { + tmp1 = 0; + } + + // min(0.25, tmp1) + if (sub(2048, tmp1) < 0) + { + bgMix = 8192; + } + else + { + bgMix = shl(tmp1, 2); + } + } + + if ((sub(st->hangCount, 40) < 0) || (sub(diff, 5325) > 0)) // 0.65 in Q13 + { + bgMix = 8192; // disable mix if too short time since + } + + // Smoothen the cb gain trajectory + // smoothing depends on mix constant bgMix + L_sum = L_mult(6554, st->cbGainHistory[2]); // 0.2 in Q15; L_sum in Q17 + for (i = 3; i < L_CBGAINHIST; i++) + { + L_sum = L_mac(L_sum, 6554, st->cbGainHistory[i]); + } + cbGainMean = pv_round(L_sum); // Q1 + + // more smoothing in error and bg noise (NB no DFI used here) + if (((bfi != 0) || (prev_bf != 0)) && (inBackgroundNoise != 0) && + ((sub(mode, MR475) == 0) || + (sub(mode, MR515) == 0) || + (sub(mode, MR59) == 0)) ) + { + L_sum = L_mult(4681, st->cbGainHistory[0]); // 0.143 in Q15; L_sum in Q17 + for (i = 1; i < L_CBGAINHIST; i++) + { + L_sum = L_mac(L_sum, 4681, st->cbGainHistory[i]); + } + cbGainMean = pv_round(L_sum); // Q1 + } + + // cbGainMix = bgMix*cbGainMix + (1-bgMix)*cbGainMean; + L_sum = L_mult(bgMix, cbGainMix); // L_sum in Q15 + L_sum = L_mac(L_sum, 8192, cbGainMean); + L_sum = L_msu(L_sum, bgMix, cbGainMean); + cbGainMix = pv_round(L_shl(L_sum, 2)); // Q1 + } + + st->hangCount = add(st->hangCount, 1); + return cbGainMix; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 Cb_gain_average( + Cb_gain_averageState *st, /* i/o : State variables for CB gain averaging */ + enum Mode mode, /* i : AMR mode */ + Word16 gain_code, /* i : CB gain Q1 */ + Word16 lsp[], /* i : The LSP for the current frame Q15 */ + Word16 lspAver[], /* i : The average of LSP for 8 frames Q15 */ + Word16 bfi, /* i : bad frame indication flag */ + Word16 prev_bf, /* i : previous bad frame indication flag */ + Word16 pdfi, /* i : potential degraded bad frame ind flag */ + Word16 prev_pdf, /* i : prev pot. degraded bad frame ind flag */ + Word16 inBackgroundNoise, /* i : background noise decision */ + Word16 voicedHangover, /* i : # of frames after last voiced frame */ + Flag *pOverflow +) +{ + Word16 i; + Word16 cbGainMix; + Word16 diff; + Word16 tmp_diff; + Word16 bgMix; + Word16 cbGainMean; + Word32 L_sum; + Word16 tmp[M]; + Word16 tmp1; + Word16 tmp2; + Word16 shift1; + Word16 shift2; + Word16 shift; + + /*---------------------------------------------------------* + * Compute mixed cb gain, used to make cb gain more * + * smooth in background noise for modes 5.15, 5.9 and 6.7 * + * states that needs to be updated by all * + *---------------------------------------------------------*/ + + /* set correct cbGainMix for MR74, MR795, MR122 */ + cbGainMix = gain_code; + + /*-------------------------------------------------------* + * Store list of CB gain needed in the CB gain * + * averaging * + *-------------------------------------------------------*/ + for (i = 0; i < (L_CBGAINHIST - 1); i++) + { + st->cbGainHistory[i] = st->cbGainHistory[i+1]; + } + st->cbGainHistory[L_CBGAINHIST-1] = gain_code; + + diff = 0; + + /* compute lsp difference */ + for (i = 0; i < M; i++) + { + tmp1 = abs_s(sub(*(lspAver + i), *(lsp + i), pOverflow)); + /* Q15 */ + shift1 = norm_s(tmp1) - 1 ; /* Qn */ + tmp1 = shl(tmp1, shift1, pOverflow); /* Q15+Qn */ + shift2 = norm_s(*(lspAver + i)); /* Qm */ + tmp2 = shl(*(lspAver + i), shift2, pOverflow); /* Q15+Qm */ + tmp[i] = div_s(tmp1, tmp2); /* Q15+(Q15+Qn)-(Q15+Qm) */ + + shift = 2 + shift1 - shift2; + + if (shift >= 0) + { + *(tmp + i) = shr(*(tmp + i), shift, pOverflow); + /* Q15+Qn-Qm-Qx=Q13 */ + } + else + { + *(tmp + i) = shl(*(tmp + i), negate(shift), pOverflow); + /* Q15+Qn-Qm-Qx=Q13 */ + } + + diff = add_16(diff, *(tmp + i), pOverflow); /* Q13 */ + } + + /* Compute hangover */ + + if (diff > 5325) /* 0.65 in Q11 */ + { + st->hangVar += 1; + } + else + { + st->hangVar = 0; + } + + + if (st->hangVar > 10) + { + /* Speech period, reset hangover variable */ + st->hangCount = 0; + } + + /* Compute mix constant (bgMix) */ + bgMix = 8192; /* 1 in Q13 */ + + if ((mode <= MR67) || (mode == MR102)) + /* MR475, MR515, MR59, MR67, MR102 */ + { + /* if errors and presumed noise make smoothing probability stronger */ + + if (((((pdfi != 0) && (prev_pdf != 0)) || (bfi != 0) || + (prev_bf != 0)) + && (voicedHangover > 1) + && (inBackgroundNoise != 0) + && ((mode == MR475) || (mode == MR515) || + (mode == MR59)))) + { + /* bgMix = min(0.25, max(0.0, diff-0.55)) / 0.25; */ + tmp_diff = diff - 4506; /* 0.55 in Q13 */ + } + else + { + /* bgMix = min(0.25, max(0.0, diff-0.40)) / 0.25; */ + tmp_diff = diff - 3277; /* 0.4 in Q13 */ + } + + /* max(0.0, diff-0.55) or */ + /* max(0.0, diff-0.40) */ + if (tmp_diff > 0) + { + tmp1 = tmp_diff; + } + else + { + tmp1 = 0; + } + + /* min(0.25, tmp1) */ + if (2048 < tmp1) + { + bgMix = 8192; + } + else + { + bgMix = shl(tmp1, 2, pOverflow); + } + + if ((st->hangCount < 40) || (diff > 5325)) /* 0.65 in Q13 */ + { + /* disable mix if too short time since */ + bgMix = 8192; + } + + /* Smoothen the cb gain trajectory */ + /* smoothing depends on mix constant bgMix */ + L_sum = L_mult(6554, st->cbGainHistory[2], pOverflow); + /* 0.2 in Q15; L_sum in Q17 */ + + for (i = 3; i < L_CBGAINHIST; i++) + { + L_sum = L_mac(L_sum, 6554, st->cbGainHistory[i], pOverflow); + } + cbGainMean = pv_round(L_sum, pOverflow); /* Q1 */ + + /* more smoothing in error and bg noise (NB no DFI used here) */ + + if (((bfi != 0) || (prev_bf != 0)) && (inBackgroundNoise != 0) + && ((mode == MR475) || (mode == MR515) + || (mode == MR59))) + { + /* 0.143 in Q15; L_sum in Q17 */ + L_sum = L_mult(4681, st->cbGainHistory[0], pOverflow); + for (i = 1; i < L_CBGAINHIST; i++) + { + L_sum = + L_mac(L_sum, 4681, st->cbGainHistory[i], pOverflow); + } + cbGainMean = pv_round(L_sum, pOverflow); /* Q1 */ + } + + /* cbGainMix = bgMix*cbGainMix + (1-bgMix)*cbGainMean; */ + /* L_sum in Q15 */ + L_sum = L_mult(bgMix, cbGainMix, pOverflow); + L_sum = L_mac(L_sum, 8192, cbGainMean, pOverflow); + L_sum = L_msu(L_sum, bgMix, cbGainMean, pOverflow); + cbGainMix = pv_round(L_shl(L_sum, 2, pOverflow), pOverflow); /* Q1 */ + } + + st->hangCount += 1; + + return (cbGainMix); +} + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/c_g_aver.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/c_g_aver.h new file mode 100644 index 0000000..ac56383 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/c_g_aver.h @@ -0,0 +1,154 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: c_g_aver.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : c_g_aver.h + Purpose : Background noise source charateristic detector (SCD) + +------------------------------------------------------------------------------ +*/ + +#ifndef _C_G_AVER_H_ +#define _C_G_AVER_H_ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "mode.h" +#include "cnst.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; [Define module specific macros here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; [Include all pre-processor statements here.] + ----------------------------------------------------------------------------*/ +#define L_CBGAINHIST 7 + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; [Declare variables used in this module but defined elsewhere] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + /* history vector of past synthesis speech energy */ + Word16 cbGainHistory[L_CBGAINHIST]; + + /* state flags */ + Word16 hangVar; /* counter; */ + Word16 hangCount; /* counter; */ + + } Cb_gain_averageState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; [List function prototypes here] + ----------------------------------------------------------------------------*/ + /* + * Function : Cb_gain_average_init + * Purpose : Allocates initializes state memory + * Description : Stores pointer to filter status struct in *st. This + * pointer has to be passed to Cb_gain_average in each call. + * Returns : 0 on success + */ + Word16 Cb_gain_average_init(Cb_gain_averageState **st); + + /* + * Function : Cb_gain_average_reset + * Purpose : Resets state memory + * Returns : 0 on success + */ + Word16 Cb_gain_average_reset(Cb_gain_averageState *st); + + /* + * Function : Cb_gain_average_exit + * Purpose : The memory used for state memory is freed + * Description : Stores NULL in *s + * Returns : void + */ + void Cb_gain_average_exit(Cb_gain_averageState **st); + + /* + * Function : Cb_gain_average + * Purpose : Charaterice synthesis speech and detect background noise + * Returns : background noise decision; 0 = bgn, 1 = no bgn + */ + Word16 Cb_gain_average( + Cb_gain_averageState *st, /* i/o : State variables for CB gain avergeing */ + enum Mode mode, /* i : AMR mode */ + Word16 gain_code, /* i : CB gain Q1 */ + Word16 lsp[], /* i : The LSP for the current frame Q15 */ + Word16 lspAver[], /* i : The average of LSP for 8 frames Q15 */ + Word16 bfi, /* i : bad frame indication flag */ + Word16 prev_bf, /* i : previous bad frame indication flag */ + Word16 pdfi, /* i : potential degraded bad frame ind flag */ + Word16 prev_pdf, /* i : prev pot. degraded bad frame ind flag */ + Word16 inBackgroundNoise, /* i : background noise decision */ + Word16 voicedHangover, /* i : # of frames after last voiced frame */ + Flag *pOverflow + ); + + +#ifdef __cplusplus +} +#endif + +#endif /* _C_G_AVER_H_ */ + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d1035pf.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d1035pf.cpp new file mode 100644 index 0000000..d56b922 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d1035pf.cpp @@ -0,0 +1,231 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d1035pf.cpp + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "d1035pf.h" +#include "typedef.h" +#include "basic_op.h" +#include "cnst.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ +#define NB_PULSE 10 /* number of pulses */ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL STORE/BUFFER/POINTER DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: dec_10i40_35bits +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + index = buffer containing index of 10 pulses; each element is + represented by sign+position + cod = buffer of algebraic (fixed) codebook excitation + + Outputs: + cod buffer contains the new algebraic codebook excitation + + Returns: + None + + Global Variables Used: + dgray = gray decoding table + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function builds the innovative codevector from the received index of + algebraic codebook. See c1035pf.c for more details about the algebraic + codebook structure. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d1035pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void dec_10i40_35bits ( + Word16 index[], // (i) : index of 10 pulses (sign+position) + Word16 cod[] // (o) : algebraic (fixed) codebook excitation +) +{ + Word16 i, j, pos1, pos2, sign, tmp; + + for (i = 0; i < L_CODE; i++) + { + cod[i] = 0; + } + + // decode the positions and signs of pulses and build the codeword + + for (j = 0; j < NB_TRACK; j++) + { + // compute index i + + tmp = index[j]; + i = tmp & 7; + i = dgray[i]; + + i = extract_l (L_shr (L_mult (i, 5), 1)); + pos1 = add (i, j); // position of pulse "j" + + i = shr (tmp, 3) & 1; + if (i == 0) + { + sign = 4096; // +1.0 + } + else + { + sign = -4096; // -1.0 + } + + cod[pos1] = sign; + + // compute index i + + i = index[add (j, 5)] & 7; + i = dgray[i]; + i = extract_l (L_shr (L_mult (i, 5), 1)); + + pos2 = add (i, j); // position of pulse "j+5" + + if (sub (pos2, pos1) < 0) + { + sign = negate (sign); + } + cod[pos2] = add (cod[pos2], sign); + } + + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void dec_10i40_35bits( + Word16 index[], /* (i) : index of 10 pulses (sign+position) */ + Word16 cod[], /* (o) : algebraic (fixed) codebook excitation */ + const Word16* dgray_ptr /* i : ptr to read-only tbl */ +) +{ + register Word16 i, j, pos1, pos2; + Word16 sign, tmp; + + for (i = 0; i < L_CODE; i++) + { + *(cod + i) = 0; + } + + /* decode the positions and signs of pulses and build the codeword */ + + for (j = 0; j < NB_TRACK; j++) + { + /* compute index i */ + + tmp = *(index + j); + i = tmp & 7; + i = *(dgray_ptr + i); + + i = (Word16)(i * 5); + pos1 = i + j; /* position of pulse "j" */ + + i = (tmp >> 3) & 1; + + if (i == 0) + { + sign = 4096; /* +1.0 */ + } + else + { + sign = -4096; /* -1.0 */ + } + + *(cod + pos1) = sign; + + /* compute index i */ + + i = *(index + j + 5) & 7; + i = *(dgray_ptr + i); + i = (Word16)(i * 5); + + pos2 = i + j; /* position of pulse "j+5" */ + + + if (pos2 < pos1) + { + sign = negate(sign); + } + *(cod + pos2) += sign; + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d1035pf.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d1035pf.h new file mode 100644 index 0000000..ac15905 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d1035pf.h @@ -0,0 +1,106 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d1035pf.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains the prototype declaration for dec_10i40_35bits function. + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef D1035PF_H +#define D1035PF_H + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + void dec_10i40_35bits( + Word16 index[], /* (i) : index of 10 pulses (sign+position) */ + Word16 cod[], /* (o) : algebraic (fixed) codebook excitation */ + const Word16* dgray_ptr /* i : ptr to read-only tbl */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _D1035PF_H_ */ diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_11pf.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_11pf.cpp new file mode 100644 index 0000000..8a70032 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_11pf.cpp @@ -0,0 +1,192 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d2_11pf.cpp + +------------------------------------------------------------------------------ + MODULE DESCRIPTION +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "d2_11pf.h" +#include "typedef.h" +#include "cnst.h" + + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ +#define NB_PULSE 2 + + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: decode_2i40_11bits +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + sign -- Word16 -- signs of 2 pulses. + index -- Word16 -- Positions of the 2 pulses. + + Outputs: + cod[] -- array of type Word16 -- algebraic (fixed) codebook excitation + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d2_11pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void decode_2i40_11bits( + Word16 sign, /* i : signs of 2 pulses. */ + Word16 index, /* i : Positions of the 2 pulses. */ + Word16 cod[] /* o : algebraic (fixed) codebook excitation */ +) + +{ + Word16 i; + Word16 j; + + Word16 pos[NB_PULSE]; + + /* Decode the positions */ + + j = index & 0x1; + + index >>= 1; + + i = index & 0x7; + + pos[0] = i * 5 + j * 2 + 1; + + + + + index >>= 3; + + j = index & 0x3; + + index >>= 2; + + i = index & 0x7; + + if (j == 3) + { + pos[1] = i * 5 + 4; + } + else + { + pos[1] = i * 5 + j; + } + + + + + /* decode the signs and build the codeword */ + for (i = 0; i < L_SUBFR; i++) + { + cod[i] = 0; + } + + for (j = 0; j < NB_PULSE; j++) + { + i = sign & 1; + + /* This line is equivalent to... + * + * + * if (i == 1) + * { + * cod[pos[j]] = 8191; + * } + * if (i == 0) + * { + * cod[pos[j]] = -8192; + * } + */ + + cod[pos[j]] = i * 16383 - 8192; + + sign >>= 1; + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_11pf.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_11pf.h new file mode 100644 index 0000000..aaf2e08 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_11pf.h @@ -0,0 +1,93 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +******************************************************************************** +* +* GSM AMR-NB speech codec R98 Version 7.5.0 March 2, 2001 +* R99 Version 3.2.0 +* REL-4 Version 4.0.0 +* +******************************************************************************** +* +* File : d2_11pf.h +* Purpose : Algebraic codebook decoder +* +******************************************************************************** +*/ +#ifndef d2_11pf_h +#define d2_11pf_h "$Id $" + +/* +******************************************************************************** +* INCLUDE FILES +******************************************************************************** +*/ +#include "typedef.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* + ******************************************************************************** + * LOCAL VARIABLES AND TABLES + ******************************************************************************** + */ + + /* + ******************************************************************************** + * DEFINITION OF DATA TYPES + ******************************************************************************** + */ + + /* + ******************************************************************************** + * DECLARATION OF PROTOTYPES + ******************************************************************************** + */ + /************************************************************************* + * + * FUNCTION: decode_2i40_11bits (decod_ACELP()) + * + * PURPOSE: Algebraic codebook decoder for 2 pulses coded with 11 bits + * + *************************************************************************/ + + void decode_2i40_11bits( + Word16 sign, /* i : signs of 2 pulses. */ + Word16 index, /* i : Positions of the 2 pulses. */ + Word16 cod[] /* o : algebraic (fixed) codebook excitation */ + ); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_9pf.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_9pf.cpp new file mode 100644 index 0000000..fca7a8a --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_9pf.cpp @@ -0,0 +1,212 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d2_9pf.cpp + Functions: decode_2i40_9bits + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + + FUNCTION: decode_2i40_9bits (decod_ACELP()) + + PURPOSE: Algebraic codebook decoder. For details about the encoding see + c2_9pf.c +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "d2_9pf.h" +#include "typedef.h" +#include "basic_op.h" +#include "cnst.h" + + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. Include conditional + ; compile variables also. + ----------------------------------------------------------------------------*/ +#define NB_PULSE 2 + + + /*---------------------------------------------------------------------------- + ; LOCAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; LOCAL VARIABLE DEFINITIONS + ; Variable declaration - defined here and used outside this module + ----------------------------------------------------------------------------*/ + + /*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: decode_2i40_11bits +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + sign -- Word16 -- signs of 2 pulses. + index -- Word16 -- Positions of the 2 pulses. + + Outputs: + cod[] -- array of type Word16 -- algebraic (fixed) codebook excitation + pOverflow = pointer to overflow flag + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d2_9pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void decode_2i40_9bits( + Word16 subNr, /* i : subframe number */ + Word16 sign, /* i : signs of 2 pulses. */ + Word16 index, /* i : Positions of the 2 pulses. */ + const Word16* startPos_ptr, /* i: ptr to read only table */ + Word16 cod[], /* o : algebraic (fixed) codebook excitation */ + Flag *pOverflow /* o : Flag set when overflow occurs */ +) +{ + Word16 i; + Word16 j; + Word16 k; + + Word16 pos[NB_PULSE]; + + /* Decode the positions */ + /* table bit is the MSB */ + + j = (Word16)(index & 64); + + j >>= 3; + + i = index & 7; + + k = + shl( + subNr, + 1, + pOverflow); + + k += j; + + /* pos0 =i*5+startPos_ptr[j*8+subNr*2] */ + pos[0] = i * 5 + startPos_ptr[k++]; + + + index >>= 3; + + i = index & 7; + + /* pos1 =i*5+startPos_ptr[j*8+subNr*2 + 1] */ + pos[1] = i * 5 + startPos_ptr[k]; + + + /* decode the signs and build the codeword */ + + for (i = L_SUBFR - 1; i >= 0; i--) + { + cod[i] = 0; + } + + for (j = 0; j < NB_PULSE; j++) + { + i = sign & 0x1; + + /* This line is equivalent to... + * + * + * if (i == 1) + * { + * cod[pos[j]] = 8191; + * } + * if (i == 0) + * { + * cod[pos[j]] = -8192; + * } + */ + + cod[pos[j]] = i * 16383 - 8192; + + sign >>= 1; + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_9pf.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_9pf.h new file mode 100644 index 0000000..ef7c16b --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d2_9pf.h @@ -0,0 +1,111 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d2_9pf.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the d2_9pf.c + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef d2_9pf_h +#define d2_9pf_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + void decode_2i40_9bits( + Word16 subNr, /* i : subframe number */ + Word16 sign, /* i : signs of 2 pulses. */ + Word16 index, /* i : Positions of the 2 pulses. */ + const Word16* startPos_ptr, /* i: ptr to read only table */ + Word16 cod[], /* o : algebraic (fixed) codebook excitation */ + Flag * pOverflow /* o : Flag set when overflow occurs */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _d2_9PF_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d3_14pf.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d3_14pf.cpp new file mode 100644 index 0000000..193b7de --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d3_14pf.cpp @@ -0,0 +1,201 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d3_14pf.cpp + Functions: decode_3i40_14bits + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + + FUNCTION: decode_3i40_14bits (decod_ACELP()) + + PURPOSE: Algebraic codebook decoder. For details about the encoding see + c3_14pf.c +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "cnst.h" +#include "d3_14pf.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ +#define NB_PULSE 3 /* number of pulses */ + + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: decode_3i40_14bits +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + sign -- Word16 -- signs of 3 pulses. + index -- Word16 -- Positions of the 3 pulses. + + Outputs: + cod[] -- array of type Word16 -- algebraic (fixed) codebook excitation + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d2_9pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void decode_3i40_14bits( + Word16 sign, /* i : signs of 3 pulses. */ + Word16 index, /* i : Positions of the 3 pulses. */ + Word16 cod[] /* o : algebraic (fixed) codebook excitation */ +) +{ + Word16 i; + Word16 j; + + Word16 pos[NB_PULSE]; + + /* Decode the positions */ + + i = index & 0x7; + + pos[0] = i * 5; + + + + + + index >>= 3; + + j = index & 0x1; + + index >>= 1; + + i = index & 0x7; + + pos[1] = i * 5 + j * 2 + 1; + + + + + + index >>= 3; + + j = index & 0x1; + + index >>= 1; + + i = index & 0x7; + + pos[2] = i * 5 + j * 2 + 2; + + + /* decode the signs and build the codeword */ + + for (i = 0; i < L_SUBFR; i++) + { + cod[i] = 0; + } + + for (j = 0; j < NB_PULSE; j++) + { + i = sign & 1; + + /* This line is equivalent to... + * + * + * if (i == 1) + * { + * cod[pos[j]] = 8191; + * } + * if (i == 0) + * { + * cod[pos[j]] = -8192; + * } + */ + + cod[pos[j]] = i * 16383 - 8192; + + sign >>= 1; + + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d3_14pf.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d3_14pf.h new file mode 100644 index 0000000..e8a0de1 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d3_14pf.h @@ -0,0 +1,107 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d3_14pf.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the d3_14pf.c + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef d3_14pf_h +#define d3_14pf_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + void decode_3i40_14bits( + Word16 sign, /* i : signs of 3 pulses. */ + Word16 index, /* i : Positions of the 3 pulses. */ + Word16 cod[] /* o : algebraic (fixed) codebook excitation */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _d3_14PF_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d4_17pf.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d4_17pf.cpp new file mode 100644 index 0000000..62838bd --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d4_17pf.cpp @@ -0,0 +1,237 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d4_17pf.cpp + Functions: decode_4i40_17bits + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + + FUNCTION: decode_4i40_17bits (decod_ACELP()) + + PURPOSE: Algebraic codebook decoder. For details about the encoding see + c4_17pf.c +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "basic_op.h" +#include "cnst.h" +#include "d4_17pf.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. Include conditional + ; compile variables also. + ----------------------------------------------------------------------------*/ +#define NB_PULSE 4 /* number of pulses */ + + + /*---------------------------------------------------------------------------- + ; LOCAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; LOCAL VARIABLE DEFINITIONS + ; Variable declaration - defined here and used outside this module + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: decode_4i40_17bits +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + sign -- Word16 -- signs of 3 pulses. + index -- Word16 -- Positions of the 3 pulses. + + Outputs: + cod[] -- array of type Word16 -- algebraic (fixed) codebook excitation + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d4_17pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void decode_4i40_17bits( + Word16 sign, /* i : signs of 4 pulses. */ + Word16 index, /* i : Positions of the 4 pulses. */ + const Word16* dgray_ptr, /* i : Pointer to read-only table */ + Word16 cod[] /* o : algebraic (fixed) codebook excitation */ +) +{ + Word16 i; + Word16 j; + + Word16 pos[NB_PULSE]; + + /* Index is a 13-bit value. 3 bits each correspond to the + * positions 0-2, with 4 bits allocated for position 3. + * + * + * [][][][] [][][] [][][] [][][] + * | | | | + * | | | | + * pos3 pos2 pos1 pos0 + */ + + /* Decode the positions */ + + i = index & 0x7; + + i = dgray_ptr[i]; + + pos[0] = i * 5; /* pos0 =i*5 */ + + + index >>= 3; + + i = index & 0x7; + + i = dgray_ptr[i]; + + pos[1] = i * 5 + 1; /* pos1 =i*5+1 */ + + + + index >>= 3; + + i = index & 0x7; + + i = dgray_ptr[i]; + + pos[2] = i * 5 + 2; /* pos2 =i*5+2 */ + + + + + + index >>= 3; + + j = index & 0x1; + + index >>= 1; + + i = index & 0x7; + + i = dgray_ptr[i]; + + pos[3] = i * 5 + 3 + j; /* pos3 =i*5+3+j */ + + + /* decode the signs and build the codeword */ + + for (i = 0; i < L_SUBFR; i++) + { + cod[i] = 0; + } + + for (j = 0; j < NB_PULSE; j++) + { + i = sign & 0x1; + + /* This line is equivalent to... + * + * + * if (i == 1) + * { + * cod[pos[j]] = 8191; + * } + * if (i == 0) + * { + * cod[pos[j]] = -8192; + * } + */ + + cod[pos[j]] = i * 16383 - 8192; + + sign >>= 1; + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d4_17pf.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d4_17pf.h new file mode 100644 index 0000000..b69cfae --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d4_17pf.h @@ -0,0 +1,109 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d4_17pf.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the d4_17pf.c + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef d4_17pf_h +#define d4_17pf_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + void decode_4i40_17bits( + Word16 sign, /* i : signs of 4 pulses. */ + Word16 index, /* i : Positions of the 4 pulses. */ + const Word16* dgray_ptr, /* i : Pointer to read-only table */ + Word16 cod[] /* o : algebraic (fixed) codebook excitation */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _d4_17PF_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d8_31pf.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d8_31pf.cpp new file mode 100644 index 0000000..6195896 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d8_31pf.cpp @@ -0,0 +1,556 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d8_31pf.cpp + +------------------------------------------------------------------------------ + + MODULE DESCRIPTION +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "d8_31pf.h" +#include "typedef.h" +#include "basic_op.h" +#include "cnst.h" + + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ +#define NB_PULSE 8 /* number of pulses */ + +/* define values/representation for output codevector and sign */ +#define POS_CODE 8191 +#define NEG_CODE 8191 + + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: decompress10 +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + MSBs -- Word16 -- MSB part of the index + LSBs -- Word16 -- LSB part of the index + index1 -- Word16 -- index for first pos in pos_index[] + index2 -- Word16 -- index for second pos in pos_index[] + index3 -- Word16 -- index for third pos in pos_index[] + + Outputs: + pos_indx[] -- array of type Word16 -- position of 3 pulses (decompressed) + + pOverflow Flag set when overflow occurs, pointer of type Flag * + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +static void decompress10( + Word16 MSBs, /* i : MSB part of the index */ + Word16 LSBs, /* i : LSB part of the index */ + Word16 index1, /* i : index for first pos in pos_index[] */ + Word16 index2, /* i : index for second pos in pos_index[] */ + Word16 index3, /* i : index for third pos in pos_index[] */ + Word16 pos_indx[], /* o : position of 3 pulses (decompressed) */ + Flag *pOverflow) /* o : Flag set when overflow occurs */ +{ + Word16 ia; + Word16 ib; + Word16 ic; + Word32 tempWord32; + + /* + pos_indx[index1] = ((MSBs-25*(MSBs/25))%5)*2 + (LSBs-4*(LSBs/4))%2; + pos_indx[index2] = ((MSBs-25*(MSBs/25))/5)*2 + (LSBs-4*(LSBs/4))/2; + pos_indx[index3] = (MSBs/25)*2 + LSBs/4; + */ + + if (MSBs > 124) + { + MSBs = 124; + } + + ia = + mult( + MSBs, + 1311, + pOverflow); + + tempWord32 = + L_mult( + ia, + 25, + pOverflow); + + + ia = (Word16)(MSBs - (tempWord32 >> 1)); + ib = + mult( + ia, + 6554, + pOverflow); + + tempWord32 = + L_mult( + ib, + 5, + pOverflow); + + ib = ia - (Word16)(tempWord32 >> 1); + + ib = + shl( + ib, + 1, + pOverflow); + + + ic = LSBs - ((LSBs >> 2) << 2); + + + pos_indx[index1] = ib + (ic & 1); + + + ib = + mult( + ia, + 6554, + pOverflow); + + ib = + shl( + ib, + 1, + pOverflow); + + + pos_indx[index2] = ib + (ic >> 1); + + + ib = LSBs >> 2; + + ic = + mult( + MSBs, + 1311, + pOverflow); + + ic = + shl( + ic, + 1, + pOverflow); + + pos_indx[index3] = add_16(ib, ic, pOverflow); + + return; +} + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: decompress_code +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + indx[] -- array of type Word16 -- position and sign of + 8 pulses (compressed) + + Outputs: + sign_indx[] -- array of type Word16 -- signs of 4 pulses (signs only) + pos_indx[] -- array of type Word16 -- position index of 8 pulses + (position only) + pOverflow pointer to type Flag -- Flag set when overflow occurs + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + PURPOSE: decompression of the linear codewords to 4+three indeces + one bit from each pulse is made robust to errors by + minimizing the phase shift of a bit error. + 4 signs (one for each track) + i0,i4,i1 => one index (7+3) bits, 3 LSBs more robust + i2,i6,i5 => one index (7+3) bits, 3 LSBs more robust + i3,i7 => one index (5+2) bits, 2-3 LSbs more robust + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +static void decompress_code( + Word16 indx[], /* i : position and sign of 8 pulses (compressed) */ + Word16 sign_indx[], /* o : signs of 4 pulses (signs only) */ + Word16 pos_indx[], /* o : position index of 8 pulses (position only) */ + Flag *pOverflow /* o : Flag set when overflow occurs */ +) +{ + Word16 i; + Word16 ia; + Word16 ib; + Word16 MSBs; + Word16 LSBs; + Word16 MSBs0_24; + Word32 tempWord32; + + for (i = 0; i < NB_TRACK_MR102; i++) + { + sign_indx[i] = indx[i]; + } + + /* + First index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits + MSBs = indx[NB_TRACK]/8; + LSBs = indx[NB_TRACK]%8; + */ + MSBs = indx[NB_TRACK_MR102] >> 3; + + LSBs = indx[NB_TRACK_MR102] & 0x7; + + decompress10( + MSBs, + LSBs, + 0, + 4, + 1, + pos_indx, + pOverflow); + + /* + Second index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits + MSBs = indx[NB_TRACK+1]/8; + LSBs = indx[NB_TRACK+1]%8; + */ + MSBs = indx[NB_TRACK_MR102+1] >> 3; + + LSBs = indx[NB_TRACK_MR102+1] & 0x7; + + decompress10( + MSBs, + LSBs, + 2, + 6, + 5, + pos_indx, + pOverflow); + + /* + Third index: 10x10 -> 2x5x2x5-> 25x2x2 -> 5+1x2 bits + MSBs = indx[NB_TRACK+2]/4; + LSBs = indx[NB_TRACK+2]%4; + MSBs0_24 = (MSBs*25+12)/32; + if ((MSBs0_24/5)%2==1) + pos_indx[3] = (4-(MSBs0_24%5))*2 + LSBs%2; + else + pos_indx[3] = (MSBs0_24%5)*2 + LSBs%2; + pos_indx[7] = (MSBs0_24/5)*2 + LSBs/2; + */ + + MSBs = indx[NB_TRACK_MR102+2] >> 2; + + LSBs = indx[NB_TRACK_MR102+2] & 0x3; + + tempWord32 = + L_mult( + MSBs, + 25, + pOverflow); + + ia = + (Word16) + L_shr( + tempWord32, + 1, + pOverflow); + + ia += 12; + + MSBs0_24 = ia >> 5; + + + ia = + mult( + MSBs0_24, + 6554, + pOverflow); + + ia &= 1; + + + ib = + mult( + MSBs0_24, + 6554, + pOverflow); + + tempWord32 = + L_mult( + ib, + 5, + pOverflow); + + + ib = MSBs0_24 - (Word16)(tempWord32 >> 1); + + if (ia == 1) + { + ib = 4 - ib; + + } + + + ib = + shl( + ib, + 1, + pOverflow); + + ia = LSBs & 0x1; + + pos_indx[3] = + add_16( + ib, + ia, + pOverflow); + + ia = + mult( + MSBs0_24, + 6554, + pOverflow); + + ia = + shl( + ia, + 1, + pOverflow); + + pos_indx[7] = ia + (LSBs >> 1); + +} + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: dec_8i40_31bits +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + index array of type Word16 -- index of 8 pulses (sign+position) + + Outputs: + cod array of type Word16 -- algebraic (fixed) codebook excitation + pOverflow pointer to type Flag -- Flag set when overflow occurs + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + PURPOSE: Builds the innovative codevector from the received + index of algebraic codebook. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d8_31pf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void dec_8i40_31bits( + Word16 index[], /* i : index of 8 pulses (sign+position) */ + Word16 cod[], /* o : algebraic (fixed) codebook excitation */ + Flag *pOverflow /* o : Flag set when overflow occurs */ +) +{ + Word16 i; + Word16 j; + Word16 pos1; + Word16 pos2; + Word16 sign; + + Word16 linear_signs[NB_TRACK_MR102]; + Word16 linear_codewords[NB_PULSE]; + + for (i = 0; i < L_CODE; i++) + { + cod[i] = 0; + } + + decompress_code( + index, + linear_signs, + linear_codewords, + pOverflow); + + /* decode the positions and signs of pulses and build the codeword */ + for (j = 0; j < NB_TRACK_MR102; j++) /* NB_TRACK_MR102 = 4 */ + { + /* position of pulse "j" */ + + pos1 = (linear_codewords[j] << 2) + j; + + + if (linear_signs[j] == 0) + { + sign = POS_CODE; /* +1.0 */ + } + else + { + sign = -NEG_CODE; /* -1.0 */ + } + + if (pos1 < L_SUBFR) + { + cod[pos1] = sign; /* avoid buffer overflow */ + } + + /* compute index i */ + /* position of pulse "j+4" */ + + pos2 = (linear_codewords[j + 4] << 2) + j; + + + if (pos2 < pos1) + { + sign = negate(sign); + } + + if (pos2 < L_SUBFR) + { + cod[pos2] += sign; /* avoid buffer overflow */ + } + + + } /* for (j = 0; j < NB_TRACK_MR102; j++) */ + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d8_31pf.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d8_31pf.h new file mode 100644 index 0000000..d46e236 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d8_31pf.h @@ -0,0 +1,108 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d8_31pf.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the d8_31pf.c + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef d8_31pf_h +#define d8_31pf_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + void dec_8i40_31bits( + Word16 index[], /* i : index of 8 pulses (sign+position) */ + Word16 cod[], /* o : algebraic (fixed) codebook excitation */ + Flag * pOverflow /* o : Flag set when overflow occurs */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _d8_31PF_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_gain_c.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_gain_c.cpp new file mode 100644 index 0000000..9cc14c0 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_gain_c.cpp @@ -0,0 +1,214 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d_gain_c.cpp + Functions: d_gain_c + + ------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + pred_state = pointer to sturcture type gc_predState. MA predictor state + mode = AMR mode (MR795 or MR122) of type enum Mode + index = received quantization index of type Word16 + code[] = pointer to innovation codevector of type Word16 + pOverflow= pointer to value indicating existence of overflow (Flag) + + Outputs: + pred_state = pointer to sturcture type gc_predState. MA predictor state + gain_code = pointer to decoded innovation gain of type Word16 + pOverflow = 1 if there is an overflow else it is zero. + + Returns: + None. + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Function : d_gain_code + Purpose : Decode the fixed codebook gain using the received index. + +------------------------------------------------------------------------------ + REQUIREMENTS + + + +------------------------------------------------------------------------------ + REFERENCES + + d_gain_c.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "d_gain_c.h" +#include "typedef.h" +#include "mode.h" + +#include "oper_32b.h" +#include "cnst.h" +#include "log2.h" +#include "pow2.h" +#include "gc_pred.h" + +#include "basic_op.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. Include conditional + ; compile variables also. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; LOCAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; LOCAL STORE/BUFFER/POINTER DEFINITIONS + ; Variable declaration - defined here and used outside this module + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; EXTERNAL FUNCTION REFERENCES + ; Declare functions defined elsewhere and referenced in this module + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ +void d_gain_code( + gc_predState *pred_state, /* i/o : MA predictor state */ + enum Mode mode, /* i : AMR mode (MR795 or MR122) */ + Word16 index, /* i : received quantization index */ + Word16 code[], /* i : innovation codevector */ + const Word16* qua_gain_code_ptr, /* i : Pointer to read-only table */ + Word16 *gain_code, /* o : decoded innovation gain */ + Flag *pOverflow +) +{ + Word16 gcode0, exp, frac; + const Word16 *p; + Word16 qua_ener_MR122, qua_ener; + Word16 exp_inn_en; + Word16 frac_inn_en; + Word32 L_tmp; + Word16 tbl_tmp; + Word16 temp; + /*-------------- Decode codebook gain ---------------*/ + + /*-------------------------------------------------------------------* + * predict codebook gain * + * ~~~~~~~~~~~~~~~~~~~~~ * + * gc0 = Pow2(int(d)+frac(d)) * + * = 2^exp + 2^frac * + * * + *-------------------------------------------------------------------*/ + + gc_pred(pred_state, mode, code, &exp, &frac, + &exp_inn_en, &frac_inn_en, pOverflow); + + index &= 31; /* index < 32, to avoid buffer overflow */ + tbl_tmp = index + (index << 1); + + p = &qua_gain_code_ptr[tbl_tmp]; + + /* Different scalings between MR122 and the other modes */ + temp = sub((Word16)mode, (Word16)MR122, pOverflow); + if (temp == 0) + { + gcode0 = (Word16)(Pow2(exp, frac, pOverflow)); /* predicted gain */ + gcode0 = shl(gcode0, 4, pOverflow); + *gain_code = shl(mult(gcode0, *p++, pOverflow), 1, pOverflow); + } + else + { + gcode0 = (Word16)(Pow2(14, frac, pOverflow)); + L_tmp = L_mult(*p++, gcode0, pOverflow); + L_tmp = L_shr(L_tmp, sub(9, exp, pOverflow), pOverflow); + *gain_code = (Word16)(L_tmp >> 16); /* Q1 */ + } + + /*-------------------------------------------------------------------* + * update table of past quantized energies * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * + *-------------------------------------------------------------------*/ + qua_ener_MR122 = *p++; + qua_ener = *p++; + gc_pred_update(pred_state, qua_ener_MR122, qua_ener); + + return; +} + + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_gain_p.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_gain_p.cpp new file mode 100644 index 0000000..ac39950 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_gain_p.cpp @@ -0,0 +1,153 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d_gain_p.cpp + Functions: d_gain_p + + ------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + mode -- enumerated type -- AMR mode + index -- Word16 -- index of quantization + Outputs: + None + + Returns: + Word16 gain -- (Q14) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Function : d_gain_pitch + Purpose : Decodes the pitch gain using the received index. + output is in Q14 + +------------------------------------------------------------------------------ + REQUIREMENTS + + + +------------------------------------------------------------------------------ + REFERENCES + + d_gain_p.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "d_gain_p.h" +#include "typedef.h" +#include "mode.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. Include conditional + ; compile variables also. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; LOCAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; LOCAL STORE/BUFFER/POINTER DEFINITIONS + ; Variable declaration - defined here and used outside this module + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL FUNCTION REFERENCES + ; Declare functions defined elsewhere and referenced in this module + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ + +Word16 d_gain_pitch( /* return value: gain (Q14) */ + enum Mode mode, /* i : AMR mode */ + Word16 index, /* i : index of quantization */ + const Word16* qua_gain_pitch_ptr /* i : pointer to read-only tables */ +) +{ + Word16 gain; + + gain = qua_gain_pitch_ptr[index]; + + if (mode == MR122) + { + /* clear 2 LSBits */ + gain &= 0xFFFC; + } + + return gain; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf.cpp new file mode 100644 index 0000000..7ac56b4 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf.cpp @@ -0,0 +1,170 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d_plsf.cpp + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + common part (reset) of LSF decoder + module (rest in d_plsf_3.c and d_plsf_5.c) +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "basic_op.h" +#include "cnst.h" +#include "oscl_mem.h" +#include "d_plsf.h" +#include "q_plsf_5_tbl.h" + + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL FUNCTION REFERENCES +; Declare functions defined elsewhere and referenced in this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL VARIABLES REFERENCES +; Declare variables used in this module but defined elsewhere +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: D_plsf_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to structure of type D_plsf_reset + + Outputs: + fields of the structure pointed to by state is initialized to zero + + Returns: + return_value = 0, if reset was successful; -1, otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Resets state memory + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + d_plsf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int D_plsf_reset (D_plsfState *state) +{ + Word16 i; + + if (state == (D_plsfState *) NULL){ + // fprintf(stderr, "D_plsf_reset: invalid parameter\n"); + return -1; + } + + for (i = 0; i < M; i++){ + state->past_r_q[i] = 0; // Past quantized prediction error + } + + // Past dequantized lsfs + Copy(mean_lsf, &state->past_lsf_q[0], M); + + return 0; +} +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 D_plsf_reset(D_plsfState *state, const Word16* mean_lsf_5_ptr) +{ + Word16 i; + + if (state == (D_plsfState *) NULL) + { + /* fprintf(stderr, "D_plsf_reset: invalid parameter\n"); */ + return -1; + } + + for (i = 0; i < M; i++) + { + state->past_r_q[i] = 0; /* Past quantized prediction error */ + } + + /* Past dequantized lsfs */ + oscl_memmove((void *)&state->past_lsf_q[0], mean_lsf_5_ptr, M*sizeof(*mean_lsf_5_ptr)); + + return 0; + +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf_3.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf_3.cpp new file mode 100644 index 0000000..2d3a4ef --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf_3.cpp @@ -0,0 +1,468 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d_plsf_3.cpp + Functions: D_plsf_3 + + ------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st -- Pointer to type struct D_plsfState + mode -- enum Mode -- coder mode + bfi -- Word16 -- bad frame indicator (set to 1 if a bad frame is received) + indice -- Pointer to type Word16 -- quantization indices of + 3 submatrices, Q0 + + Outputs: + st -- Pointer to type struct D_plsfState + lsp1_q -- Pointer to type Word16 -- quantized 1st LSP vector Q15 + pOverflow -- Pointer to type Flag -- Flag set when overflow occurs + + Returns: + None. + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + PURPOSE: Decodes the LSP parameters using the received quantization + indices.1st order MA prediction and split by 3 vector + quantization (split-VQ) + +------------------------------------------------------------------------------ + REQUIREMENTS + + + +------------------------------------------------------------------------------ + REFERENCES + + d_plsf_3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "d_plsf.h" +#include "typedef.h" +#include "basic_op.h" +#include "lsp_lsf.h" +#include "reorder.h" +#include "oscl_mem.h" +#include "q_plsf_3_tbl.h" + + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ +#define ALPHA 29491 /* ALPHA -> 0.9 */ +#define ONE_ALPHA 3277 /* ONE_ALPHA-> (1.0-ALPHA) */ + + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL STORE/BUFFER/POINTER DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL FUNCTION REFERENCES +; Declare functions defined elsewhere and referenced in this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES +; Declare variables used in this module but defined elsewhere +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ + +void D_plsf_3( + D_plsfState *st, /* i/o: State struct */ + enum Mode mode, /* i : coder mode */ + Word16 bfi, /* i : bad frame indicator (set to 1 if a */ + /* bad frame is received) */ + Word16 * indice, /* i : quantization indices of 3 submatrices, Q0 */ + CommonAmrTbls* common_amr_tbls, /* i : structure containing ptrs to read-only tables */ + Word16 * lsp1_q, /* o : quantized 1st LSP vector, Q15 */ + Flag *pOverflow /* o : Flag set when overflow occurs */ +) +{ + Word16 i; + Word16 temp; + Word16 index; + + Word16 lsf1_r[M]; + Word16 lsf1_q[M]; + + const Word16* mean_lsf_3_ptr = common_amr_tbls->mean_lsf_3_ptr; + const Word16* pred_fac_3_ptr = common_amr_tbls->pred_fac_3_ptr; + const Word16* dico1_lsf_3_ptr = common_amr_tbls->dico1_lsf_3_ptr; + const Word16* dico2_lsf_3_ptr = common_amr_tbls->dico2_lsf_3_ptr; + const Word16* dico3_lsf_3_ptr = common_amr_tbls->dico3_lsf_3_ptr; + const Word16* mr515_3_lsf_ptr = common_amr_tbls->mr515_3_lsf_ptr; + const Word16* mr795_1_lsf_ptr = common_amr_tbls->mr795_1_lsf_ptr; + + if (bfi != 0) /* if bad frame */ + { + /* use the past LSFs slightly shifted towards their mean */ + + for (i = 0; i < M; i++) + { + /* lsfi_q[i] = ALPHA*past_lsf_q[i] + ONE_ALPHA*mean_lsf[i]; */ + temp = + mult( + st->past_lsf_q[i], + ALPHA, + pOverflow); + + index = + mult( + mean_lsf_3_ptr[i], + ONE_ALPHA, + pOverflow); + + lsf1_q[i] = + add_16( + index, + temp, + pOverflow); + } + + /* estimate past quantized residual to be used in next frame */ + if (mode != MRDTX) + { + for (i = 0; i < M; i++) + { + /* temp = mean_lsf[i] + past_r2_q[i] * PRED_FAC; */ + + temp = + mult( + st->past_r_q[i], + pred_fac_3_ptr[i], + pOverflow); + + temp = + add_16( + mean_lsf_3_ptr[i], + temp, + pOverflow); + + st->past_r_q[i] = + sub( + lsf1_q[i], + temp, + pOverflow); + } + + } /* if (mode == MRDTX) */ + else + { + for (i = 0; i < M; i++) + { + /* temp = mean_lsf[i] + past_r2_q[i]; */ + + temp = + add_16( + mean_lsf_3_ptr[i], + st->past_r_q[i], + pOverflow); + + st->past_r_q[i] = + sub( + lsf1_q[i], + temp, + pOverflow); + } + } + + } /* if (bfi != 0) */ + + else /* if good LSFs received */ + { + + Word16 index_limit_1 = 0; + Word16 index_limit_2 = (DICO2_SIZE - 1) * 3; + Word16 index_limit_3 = 0; + + const Word16 *p_cb1; + const Word16 *p_cb2; + const Word16 *p_cb3; + const Word16 *p_dico; + + + p_cb2 = dico2_lsf_3_ptr; /* size DICO2_SIZE*3 */ + + if ((mode == MR475) || (mode == MR515)) + { /* MR475, MR515 */ + p_cb1 = dico1_lsf_3_ptr; /* size DICO1_SIZE*3 */ + p_cb3 = mr515_3_lsf_ptr; /* size MR515_3_SIZE*4 */ + + index_limit_1 = (DICO1_SIZE - 1) * 3; + index_limit_3 = (MR515_3_SIZE - 1) * 4; + + } + else if (mode == MR795) + { /* MR795 */ + p_cb1 = mr795_1_lsf_ptr; /* size MR795_1_SIZE*3 */ + p_cb3 = dico3_lsf_3_ptr; /* size DICO3_SIZE*4 */ + + index_limit_1 = (MR795_1_SIZE - 1) * 3; + index_limit_3 = (DICO3_SIZE - 1) * 4; + + } + else + { /* MR59, MR67, MR74, MR102, MRDTX */ + p_cb1 = dico1_lsf_3_ptr; /* size DICO1_SIZE*3 */ + p_cb3 = dico3_lsf_3_ptr; /* size DICO3_SIZE*4 */ + + index_limit_1 = (DICO1_SIZE - 1) * 3; + index_limit_3 = (DICO3_SIZE - 1) * 4; + + } + + /* decode prediction residuals from 3 received indices */ + + index = *indice++; + + /* temp = 3*index; */ + temp = index + (index << 1); + + if (temp > index_limit_1) + { + temp = index_limit_1; /* avoid buffer overrun */ + } + + p_dico = &p_cb1[temp]; + + lsf1_r[0] = *p_dico++; + lsf1_r[1] = *p_dico++; + lsf1_r[2] = *p_dico++; + + index = *indice++; + + if (mode == MR475 || mode == MR515) + { /* MR475, MR515 only using every second entry */ + index <<= 1; + } + + /* temp = 3*index */ + temp = index + (index << 1); + + if (temp > index_limit_2) + { + temp = index_limit_2; /* avoid buffer overrun */ + } + + p_dico = &p_cb2[temp]; + + lsf1_r[3] = *p_dico++; + lsf1_r[4] = *p_dico++; + lsf1_r[5] = *p_dico++; + + index = *indice++; + + temp = index << 2; + + if (temp > index_limit_3) + { + temp = index_limit_3; /* avoid buffer overrun */ + } + + + p_dico = &p_cb3[temp]; + + lsf1_r[6] = *p_dico++; + lsf1_r[7] = *p_dico++; + lsf1_r[8] = *p_dico++; + lsf1_r[9] = *p_dico++; + + /* Compute quantized LSFs and update the past quantized residual */ + + if (mode != MRDTX) + { + for (i = 0; i < M; i++) + { + temp = + mult( + st->past_r_q[i], + pred_fac_3_ptr[i], + pOverflow); + + temp = + add_16( + mean_lsf_3_ptr[i], + temp, + pOverflow); + + lsf1_q[i] = + add_16( + lsf1_r[i], + temp, + pOverflow); + + st->past_r_q[i] = lsf1_r[i]; + } + } + else + { + for (i = 0; i < M; i++) + { + temp = + add_16( + mean_lsf_3_ptr[i], + st->past_r_q[i], + pOverflow); + + lsf1_q[i] = + add_16( + lsf1_r[i], + temp, + pOverflow); + + st->past_r_q[i] = lsf1_r[i]; + } + } + + } + + /* verification that LSFs has minimum distance of LSF_GAP Hz */ + + Reorder_lsf( + lsf1_q, + LSF_GAP, + M, + pOverflow); + + oscl_memmove( + (void *)st->past_lsf_q, + lsf1_q, + M*sizeof(*lsf1_q)); + + /* convert LSFs to the cosine domain */ + + Lsf_lsp( + lsf1_q, + lsp1_q, + M, + pOverflow); + + return; +} + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Init_D_plsf_3 +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type D_plsfState + index = Word16, past_rq_init[] index [0, 7] + + Outputs: + st = pointer to a structure of type D_plsfState + + Returns: + None + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function initializes the D_plsfState structure. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + d_plsf_3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ +void Init_D_plsf_3( + D_plsfState *st, /* i/o: State struct */ + Word16 index, /* i : past_rq_init[] index [0, 7] */ + const Word16* past_rq_init_ptr /* ptr to read-only table */) +{ + oscl_memmove( + (void *)st->past_r_q, + &past_rq_init_ptr[index * M], + M*sizeof(*past_rq_init_ptr)); +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf_5.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf_5.cpp new file mode 100644 index 0000000..8a4e763 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/d_plsf_5.cpp @@ -0,0 +1,459 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: d_plsf_5.cpp + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "d_plsf.h" +#include "typedef.h" +#include "basic_op.h" +#include "lsp_lsf.h" +#include "reorder.h" +#include "cnst.h" +#include "oscl_mem.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. Include conditional + ; compile variables also. + ----------------------------------------------------------------------------*/ + /* ALPHA -> 0.95 */ + /* ONE_ALPHA-> (1.0-ALPHA) */ +#define ALPHA 31128 +#define ONE_ALPHA 1639 + + /*---------------------------------------------------------------------------- + ; LOCAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; LOCAL STORE/BUFFER/POINTER DEFINITIONS + ; Variable declaration - defined here and used outside this module + ----------------------------------------------------------------------------*/ + + /*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: D_plsf_5 +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type D_plsfState + bfi = bad frame indicator; set to 1 if a bad frame is received (Word16) + indice = pointer to quantization indices of 5 submatrices (Word16) + lsp1_q = pointer to the quantized 1st LSP vector (Word16) + lsp2_q = pointer to the quantized 2nd LSP vector (Word16) + + Outputs: + lsp1_q points to the updated quantized 1st LSP vector + lsp2_q points to the updated quantized 2nd LSP vector + Flag *pOverflow -- Flag set when overflow occurs. + + Returns: + return_value = 0 (int) + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function decodes the 2 sets of LSP parameters in a frame using the + received quantization indices. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + d_plsf_5.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int D_plsf_5 ( + D_plsfState *st, // i/o: State variables + Word16 bfi, // i : bad frame indicator (set to 1 if a bad + frame is received) + Word16 *indice, // i : quantization indices of 5 submatrices, Q0 + Word16 *lsp1_q, // o : quantized 1st LSP vector (M), Q15 + Word16 *lsp2_q // o : quantized 2nd LSP vector (M), Q15 +) +{ + Word16 i; + const Word16 *p_dico; + Word16 temp, sign; + Word16 lsf1_r[M], lsf2_r[M]; + Word16 lsf1_q[M], lsf2_q[M]; + + if (bfi != 0) // if bad frame + { + // use the past LSFs slightly shifted towards their mean + + for (i = 0; i < M; i++) + { + // lsfi_q[i] = ALPHA*st->past_lsf_q[i] + ONE_ALPHA*mean_lsf[i]; + + lsf1_q[i] = add (mult (st->past_lsf_q[i], ALPHA), + mult (mean_lsf[i], ONE_ALPHA)); + + lsf2_q[i] = lsf1_q[i]; + } + + // estimate past quantized residual to be used in next frame + + for (i = 0; i < M; i++) + { + // temp = mean_lsf[i] + st->past_r_q[i] * LSP_PRED_FAC_MR122; + + temp = add (mean_lsf[i], mult (st->past_r_q[i], + LSP_PRED_FAC_MR122)); + + st->past_r_q[i] = sub (lsf2_q[i], temp); + } + } + else + // if good LSFs received + { + // decode prediction residuals from 5 received indices + + p_dico = &dico1_lsf[shl (indice[0], 2)]; + lsf1_r[0] = *p_dico++; + lsf1_r[1] = *p_dico++; + lsf2_r[0] = *p_dico++; + lsf2_r[1] = *p_dico++; + + p_dico = &dico2_lsf[shl (indice[1], 2)]; + lsf1_r[2] = *p_dico++; + lsf1_r[3] = *p_dico++; + lsf2_r[2] = *p_dico++; + lsf2_r[3] = *p_dico++; + + sign = indice[2] & 1; + i = shr (indice[2], 1); + p_dico = &dico3_lsf[shl (i, 2)]; + + if (sign == 0) + { + lsf1_r[4] = *p_dico++; + lsf1_r[5] = *p_dico++; + lsf2_r[4] = *p_dico++; + lsf2_r[5] = *p_dico++; + } + else + { + lsf1_r[4] = negate (*p_dico++); + lsf1_r[5] = negate (*p_dico++); + lsf2_r[4] = negate (*p_dico++); + lsf2_r[5] = negate (*p_dico++); + } + + p_dico = &dico4_lsf[shl (indice[3], 2)]; + lsf1_r[6] = *p_dico++; + lsf1_r[7] = *p_dico++; + lsf2_r[6] = *p_dico++; + lsf2_r[7] = *p_dico++; + + p_dico = &dico5_lsf[shl (indice[4], 2)]; + lsf1_r[8] = *p_dico++; + lsf1_r[9] = *p_dico++; + lsf2_r[8] = *p_dico++; + lsf2_r[9] = *p_dico++; + + // Compute quantized LSFs and update the past quantized residual + for (i = 0; i < M; i++) + { + temp = add (mean_lsf[i], mult (st->past_r_q[i], + LSP_PRED_FAC_MR122)); + lsf1_q[i] = add (lsf1_r[i], temp); + lsf2_q[i] = add (lsf2_r[i], temp); + st->past_r_q[i] = lsf2_r[i]; + } + } + + // verification that LSFs have minimum distance of LSF_GAP Hz + + Reorder_lsf (lsf1_q, LSF_GAP, M); + Reorder_lsf (lsf2_q, LSF_GAP, M); + + Copy (lsf2_q, st->past_lsf_q, M); + + // convert LSFs to the cosine domain + + Lsf_lsp (lsf1_q, lsp1_q, M); + Lsf_lsp (lsf2_q, lsp2_q, M); + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void D_plsf_5( + D_plsfState *st, /* i/o: State variables */ + Word16 bfi, /* i : bad frame indicator (set to 1 if a bad + frame is received) */ + Word16 *indice, /* i : quantization indices of 5 submatrices, Q0 */ + CommonAmrTbls* common_amr_tbls, /* i : structure containing ptrs to read-only tables */ + Word16 *lsp1_q, /* o : quantized 1st LSP vector (M), Q15 */ + Word16 *lsp2_q, /* o : quantized 2nd LSP vector (M), Q15 */ + Flag *pOverflow /* o : Flag set when overflow occurs */ +) +{ + register Word16 i; + Word16 temp; + Word16 sign; + + const Word16 *p_dico; + + Word16 lsf1_r[M]; + Word16 lsf2_r[M]; + Word16 lsf1_q[M]; + Word16 lsf2_q[M]; + + /* These tables are defined in q_plsf_5_tbl.c */ + const Word16* mean_lsf_5_ptr = common_amr_tbls->mean_lsf_5_ptr; + const Word16* dico1_lsf_5_ptr = common_amr_tbls->dico1_lsf_5_ptr; + const Word16* dico2_lsf_5_ptr = common_amr_tbls->dico2_lsf_5_ptr; + const Word16* dico3_lsf_5_ptr = common_amr_tbls->dico3_lsf_5_ptr; + const Word16* dico4_lsf_5_ptr = common_amr_tbls->dico4_lsf_5_ptr; + const Word16* dico5_lsf_5_ptr = common_amr_tbls->dico5_lsf_5_ptr; + + if (bfi != 0) /* if bad frame */ + { + /* use the past LSFs slightly shifted towards their mean */ + + for (i = 0; i < M; i++) + { + /* + * lsfi_q[i] = ALPHA*st->past_lsf_q[i] + + * ONE_ALPHA*mean_lsf[i]; + */ + + temp = (Word16)(((Word32) st->past_lsf_q[i] * ALPHA) >> 15); + + sign = (Word16)(((Word32) * (mean_lsf_5_ptr + i) * ONE_ALPHA) >> 15); + + *(lsf1_q + i) = add_16(sign, temp, pOverflow); + + *(lsf2_q + i) = *(lsf1_q + i); + + /* + * estimate past quantized residual to be used in + * next frame + */ + + /* + * temp = mean_lsf[i] + + * st->past_r_q[i] * LSP_PRED_FAC_MR122; + */ + + temp = (Word16)(((Word32) st->past_r_q[i] * LSP_PRED_FAC_MR122) >> 15); + + temp = add_16(*(mean_lsf_5_ptr + i), temp, pOverflow); + + st->past_r_q[i] = sub(*(lsf2_q + i), temp, pOverflow); + } + } + else + /* if good LSFs received */ + { + /* decode prediction residuals from 5 received indices */ + + temp = + shl( + *(indice), + 2, + pOverflow); + + p_dico = &dico1_lsf_5_ptr[temp]; + + *(lsf1_r + 0) = *p_dico++; + *(lsf1_r + 1) = *p_dico++; + *(lsf2_r + 0) = *p_dico++; + *(lsf2_r + 1) = *p_dico++; + + temp = shl(*(indice + 1), 2, pOverflow); + + p_dico = &dico2_lsf_5_ptr[temp]; + + *(lsf1_r + 2) = *p_dico++; + *(lsf1_r + 3) = *p_dico++; + *(lsf2_r + 2) = *p_dico++; + *(lsf2_r + 3) = *p_dico++; + + sign = *(indice + 2) & 1; + + if (*(indice + 2) < 0) + { + i = ~(~(*(indice + 2)) >> 1); + } + else + { + i = *(indice + 2) >> 1; + } + + temp = shl(i, 2, pOverflow); + + p_dico = &dico3_lsf_5_ptr[temp]; + + if (sign == 0) + { + *(lsf1_r + 4) = *p_dico++; + *(lsf1_r + 5) = *p_dico++; + *(lsf2_r + 4) = *p_dico++; + *(lsf2_r + 5) = *p_dico++; + } + else + { + *(lsf1_r + 4) = negate(*p_dico++); + *(lsf1_r + 5) = negate(*p_dico++); + *(lsf2_r + 4) = negate(*p_dico++); + *(lsf2_r + 5) = negate(*p_dico++); + } + + temp = shl(*(indice + 3), 2, pOverflow); + + p_dico = &dico4_lsf_5_ptr[temp]; + + *(lsf1_r + 6) = *p_dico++; + *(lsf1_r + 7) = *p_dico++; + *(lsf2_r + 6) = *p_dico++; + *(lsf2_r + 7) = *p_dico++; + + temp = shl(*(indice + 4), 2, pOverflow); + + p_dico = &dico5_lsf_5_ptr[temp]; + + *(lsf1_r + 8) = *p_dico++; + *(lsf1_r + 9) = *p_dico++; + *(lsf2_r + 8) = *p_dico++; + *(lsf2_r + 9) = *p_dico++; + + /* Compute quantized LSFs and update the past quantized + residual */ + for (i = 0; i < M; i++) + { + temp = + mult( + st->past_r_q[i], + LSP_PRED_FAC_MR122, + pOverflow); + + temp = + add_16( + *(mean_lsf_5_ptr + i), + temp, + pOverflow); + + *(lsf1_q + i) = + add_16( + *(lsf1_r + i), + temp, + pOverflow); + + *(lsf2_q + i) = + add_16( + *(lsf2_r + i), + temp, + pOverflow); + + st->past_r_q[i] = *(lsf2_r + i); + } + } + + /* verification that LSFs have minimum distance of LSF_GAP Hz */ + + Reorder_lsf( + lsf1_q, + LSF_GAP, + M, + pOverflow); + + Reorder_lsf( + lsf2_q, + LSF_GAP, + M, + pOverflow); + + oscl_memmove((void *)st->past_lsf_q, lsf2_q, M*sizeof(*lsf2_q)); + + /* convert LSFs to the cosine domain */ + + Lsf_lsp( + lsf1_q, + lsp1_q, + M, + pOverflow); + + Lsf_lsp( + lsf2_q, + lsp2_q, + M, + pOverflow); + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_amr.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_amr.cpp new file mode 100644 index 0000000..1e62c15 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_amr.cpp @@ -0,0 +1,2333 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + Filename: dec_amr.cpp + Funtions: Decoder_amr_init + Decoder_amr_reset + Decoder_amr + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + This file contains the function used to decode one speech frame using a given + codec mode. The functions used to initialize, reset, and exit are also + included in this file. + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "dec_amr.h" +#include "typedef.h" +#include "cnst.h" +#include "set_zero.h" +#include "syn_filt.h" +#include "d_plsf.h" +#include "agc.h" +#include "int_lpc.h" +#include "dec_gain.h" +#include "dec_lag3.h" +#include "dec_lag6.h" +#include "d2_9pf.h" +#include "d2_11pf.h" +#include "d3_14pf.h" +#include "d4_17pf.h" +#include "d8_31pf.h" +#include "d1035pf.h" +#include "pred_lt.h" +#include "d_gain_p.h" +#include "d_gain_c.h" +#include "dec_gain.h" +#include "ec_gains.h" +#include "ph_disp.h" +#include "c_g_aver.h" +#include "int_lsf.h" +#include "lsp_lsf.h" +#include "lsp_avg.h" +#include "bgnscd.h" +#include "ex_ctrl.h" +#include "sqrt_l.h" +#include "frame.h" +#include "b_cn_cod.h" +#include "basic_op.h" +#include "oscl_mem.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Decoder_amr_init +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a pointer to structures of type Decoder_amrState + + Outputs: + structure pointed to by the pointer which is pointed to by state is + initialized to each field's initial values + + state pointer points to the address of the memory allocated by + Decoder_amr_init function + + Returns: + return_value = 0, if the initialization was successful; -1, otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function allocates and initializes state memory used by the Decoder_amr + function. It stores the pointer to the filter status structure in state. This + pointer has to be passed to Decoder_amr in each call. The function returns + 0, if initialization was successful and -1, otherwise. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int Decoder_amr_init (Decoder_amrState **state) +{ + Decoder_amrState* s; + Word16 i; + + if (state == (Decoder_amrState **) NULL){ + fprintf(stderr, "Decoder_amr_init: invalid parameter\n"); + return -1; + } + *state = NULL; + + // allocate memory + if ((s= (Decoder_amrState *) malloc(sizeof(Decoder_amrState))) == NULL){ + fprintf(stderr, "Decoder_amr_init: can not malloc state structure\n"); + return -1; + } + + s->T0_lagBuff = 40; + s->inBackgroundNoise = 0; + s->voicedHangover = 0; + for (i = 0; i < 9; i++) + s->ltpGainHistory[i] = 0; + + s->lsfState = NULL; + s->ec_gain_p_st = NULL; + s->ec_gain_c_st = NULL; + s->pred_state = NULL; + s->ph_disp_st = NULL; + s->dtxDecoderState = NULL; + + if (D_plsf_init(&s->lsfState) || + ec_gain_pitch_init(&s->ec_gain_p_st) || + ec_gain_code_init(&s->ec_gain_c_st) || + gc_pred_init(&s->pred_state) || + Cb_gain_average_init(&s->Cb_gain_averState) || + lsp_avg_init(&s->lsp_avg_st) || + Bgn_scd_init(&s->background_state) || + ph_disp_init(&s->ph_disp_st) || + dtx_dec_init(&s->dtxDecoderState)) { + Decoder_amr_exit(&s); + return -1; + } + + Decoder_amr_reset(s, (enum Mode)0); + *state = s; + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 Decoder_amr_init(Decoder_amrState *s) +{ + Word16 i; + + if (s == (Decoder_amrState *) NULL) + { + /* fprint(stderr, "Decoder_amr_init: invalid parameter\n"); */ + return(-1); + } + + get_const_tbls(&s->common_amr_tbls); + + s->T0_lagBuff = 40; + s->inBackgroundNoise = 0; + s->voicedHangover = 0; + + /* Initialize overflow Flag */ + + s->overflow = 0; + + for (i = 0; i < LTP_GAIN_HISTORY_LEN; i++) + { + s->ltpGainHistory[i] = 0; + } + + D_plsf_reset(&s->lsfState, s->common_amr_tbls.mean_lsf_5_ptr); + ec_gain_pitch_reset(&s->ec_gain_p_st); + ec_gain_code_reset(&s->ec_gain_c_st); + Cb_gain_average_reset(&s->Cb_gain_averState); + lsp_avg_reset(&s->lsp_avg_st, s->common_amr_tbls.mean_lsf_5_ptr); + Bgn_scd_reset(&s->background_state); + ph_disp_reset(&s->ph_disp_st); + dtx_dec_reset(&s->dtxDecoderState); + gc_pred_reset(&s->pred_state); + + Decoder_amr_reset(s, MR475); + + return(0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Decoder_amr_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a structure of type Decoder_amrState + mode = codec mode (enum Mode) + + Outputs: + structure pointed to by state is initialized to its reset value + + Returns: + return_value = 0, if reset was successful; -1, otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function resets the state memory used by the Decoder_amr function. It + returns a 0, if reset was successful and -1, otherwise. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int Decoder_amr_reset (Decoder_amrState *state, enum Mode mode) +{ + Word16 i; + + if (state == (Decoder_amrState *) NULL){ + fprintf(stderr, "Decoder_amr_reset: invalid parameter\n"); + return -1; + } + + // Initialize static pointer + state->exc = state->old_exc + PIT_MAX + L_INTERPOL; + + // Static vectors to zero + Set_zero (state->old_exc, PIT_MAX + L_INTERPOL); + + if (mode != MRDTX) + Set_zero (state->mem_syn, M); + + // initialize pitch sharpening + state->sharp = SHARPMIN; + state->old_T0 = 40; + + // Initialize state->lsp_old [] + + if (mode != MRDTX) { + Copy(lsp_init_data, &state->lsp_old[0], M); + } + + // Initialize memories of bad frame handling + state->prev_bf = 0; + state->prev_pdf = 0; + state->state = 0; + + state->T0_lagBuff = 40; + state->inBackgroundNoise = 0; + state->voicedHangover = 0; + if (mode != MRDTX) { + for (i=0;i<9;i++) + state->excEnergyHist[i] = 0; + } + + for (i = 0; i < 9; i++) + state->ltpGainHistory[i] = 0; + + Cb_gain_average_reset(state->Cb_gain_averState); + if (mode != MRDTX) + lsp_avg_reset(state->lsp_avg_st); + D_plsf_reset(state->lsfState); + ec_gain_pitch_reset(state->ec_gain_p_st); + ec_gain_code_reset(state->ec_gain_c_st); + + if (mode != MRDTX) + gc_pred_reset(state->pred_state); + + Bgn_scd_reset(state->background_state); + state->nodataSeed = 21845; + ph_disp_reset(state->ph_disp_st); + if (mode != MRDTX) + dtx_dec_reset(state->dtxDecoderState); + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 Decoder_amr_reset(Decoder_amrState *state, enum Mode mode) +{ + Word16 i; + + if (state == (Decoder_amrState *) NULL) + { + /* fprint(stderr, "Decoder_amr_reset: invalid parameter\n"); */ + return(-1); + } + + /* Initialize static pointer */ + state->exc = state->old_exc + PIT_MAX + L_INTERPOL; + + /* Static vectors to zero */ + oscl_memset(state->old_exc, 0, sizeof(Word16)*(PIT_MAX + L_INTERPOL)); + + if (mode != MRDTX) + { + oscl_memset(state->mem_syn, 0, sizeof(Word16)*M); + } + /* initialize pitch sharpening */ + state->sharp = SHARPMIN; + state->old_T0 = 40; + + /* Initialize overflow Flag */ + + state->overflow = 0; + + /* Initialize state->lsp_old [] */ + + if (mode != MRDTX) + { + state->lsp_old[0] = 30000; + state->lsp_old[1] = 26000; + state->lsp_old[2] = 21000; + state->lsp_old[3] = 15000; + state->lsp_old[4] = 8000; + state->lsp_old[5] = 0; + state->lsp_old[6] = -8000; + state->lsp_old[7] = -15000; + state->lsp_old[8] = -21000; + state->lsp_old[9] = -26000; + } + + /* Initialize memories of bad frame handling */ + state->prev_bf = 0; + state->prev_pdf = 0; + state->state = 0; + + state->T0_lagBuff = 40; + state->inBackgroundNoise = 0; + state->voicedHangover = 0; + if (mode != MRDTX) + { + for (i = 0; i < EXC_ENERGY_HIST_LEN; i++) + { + state->excEnergyHist[i] = 0; + } + } + + for (i = 0; i < LTP_GAIN_HISTORY_LEN; i++) + { + state->ltpGainHistory[i] = 0; + } + + Cb_gain_average_reset(&(state->Cb_gain_averState)); + if (mode != MRDTX) + { + lsp_avg_reset(&(state->lsp_avg_st), state->common_amr_tbls.mean_lsf_5_ptr); + } + D_plsf_reset(&(state->lsfState), state->common_amr_tbls.mean_lsf_5_ptr); + ec_gain_pitch_reset(&(state->ec_gain_p_st)); + ec_gain_code_reset(&(state->ec_gain_c_st)); + + if (mode != MRDTX) + { + gc_pred_reset(&(state->pred_state)); + } + + Bgn_scd_reset(&(state->background_state)); + state->nodataSeed = 21845; + ph_disp_reset(&(state->ph_disp_st)); + if (mode != MRDTX) + { + dtx_dec_reset(&(state->dtxDecoderState)); + } + + return(0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Decoder_amr +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type Decoder_amrState + mode = codec mode (enum Mode) + parm = buffer of synthesis parameters (Word16) + frame_type = received frame type (enum RXFrameType) + synth = buffer containing synthetic speech (Word16) + A_t = buffer containing decoded LP filter in 4 subframes (Word16) + + Outputs: + structure pointed to by st contains the newly calculated decoder + parameters + synth buffer contains the decoded speech samples + A_t buffer contains the decoded LP filter parameters + + Returns: + return_value = 0 (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function performs the decoding of one speech frame for a given codec + mode. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int Decoder_amr ( + Decoder_amrState *st, // i/o : State variables + enum Mode mode, // i : AMR mode + Word16 parm[], // i : vector of synthesis parameters + (PRM_SIZE) + enum RXFrameType frame_type, // i : received frame type + Word16 synth[], // o : synthesis speech (L_FRAME) + Word16 A_t[] // o : decoded LP filter in 4 subframes + (AZ_SIZE) +) +{ + // LPC coefficients + + Word16 *Az; // Pointer on A_t + + // LSPs + + Word16 lsp_new[M]; + Word16 lsp_mid[M]; + + // LSFs + + Word16 prev_lsf[M]; + Word16 lsf_i[M]; + + // Algebraic codevector + + Word16 code[L_SUBFR]; + + // excitation + + Word16 excp[L_SUBFR]; + Word16 exc_enhanced[L_SUBFR]; + + // Scalars + + Word16 i, i_subfr; + Word16 T0, T0_frac, index, index_mr475 = 0; + Word16 gain_pit, gain_code, gain_code_mix, pit_sharp, pit_flag, pitch_fac; + Word16 t0_min, t0_max; + Word16 delta_frc_low, delta_frc_range; + Word16 tmp_shift; + Word16 temp; + Word32 L_temp; + Word16 flag4; + Word16 carefulFlag; + Word16 excEnergy; + Word16 subfrNr; + Word16 evenSubfr = 0; + + Word16 bfi = 0; // bad frame indication flag + Word16 pdfi = 0; // potential degraded bad frame flag + + enum DTXStateType newDTXState; // SPEECH , DTX, DTX_MUTE + + // find the new DTX state SPEECH OR DTX + newDTXState = rx_dtx_handler(st->dtxDecoderState, frame_type); + + // DTX actions + if (sub(newDTXState, SPEECH) != 0 ) + { + Decoder_amr_reset (st, MRDTX); + + dtx_dec(st->dtxDecoderState, + st->mem_syn, + st->lsfState, + st->pred_state, + st->Cb_gain_averState, + newDTXState, + mode, + parm, synth, A_t); + // update average lsp + + Lsf_lsp(st->lsfState->past_lsf_q, st->lsp_old, M); + lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q); + goto the_end; + } + + // SPEECH action state machine + if ((sub(frame_type, RX_SPEECH_BAD) == 0) || + (sub(frame_type, RX_NO_DATA) == 0) || + (sub(frame_type, RX_ONSET) == 0)) + { + bfi = 1; + if ((sub(frame_type, RX_NO_DATA) == 0) || + (sub(frame_type, RX_ONSET) == 0)) + { + build_CN_param(&st->nodataSeed, + prmno[mode], + bitno[mode], + parm); + } + } + else if (sub(frame_type, RX_SPEECH_DEGRADED) == 0) + { + pdfi = 1; + } + + if (bfi != 0) + { + st->state = add (st->state, 1); + } + else if (sub (st->state, 6) == 0) + + { + st->state = 5; + } + else + { + st->state = 0; + } + + if (sub (st->state, 6) > 0) + { + st->state = 6; + } + + // If this frame is the first speech frame after CNI period, + // set the BFH state machine to an appropriate state depending + // on whether there was DTX muting before start of speech or not + // If there was DTX muting, the first speech frame is muted. + // If there was no DTX muting, the first speech frame is not + // muted. The BFH state machine starts from state 5, however, to + // keep the audible noise resulting from a SID frame which is + // erroneously interpreted as a good speech frame as small as + // possible (the decoder output in this case is quickly muted) + + if (sub(st->dtxDecoderState->dtxGlobalState, DTX) == 0) + { + st->state = 5; + st->prev_bf = 0; + } + else if (sub(st->dtxDecoderState->dtxGlobalState, DTX_MUTE) == 0) + { + st->state = 5; + st->prev_bf = 1; + } + + // save old LSFs for CB gain smoothing + Copy (st->lsfState->past_lsf_q, prev_lsf, M); + + // decode LSF parameters and generate interpolated lpc coefficients + for the 4 subframes + if (sub (mode, MR122) != 0) + { + D_plsf_3(st->lsfState, mode, bfi, parm, lsp_new); + + // Advance synthesis parameters pointer + parm += 3; + + Int_lpc_1to3(st->lsp_old, lsp_new, A_t); + } + else + { + D_plsf_5 (st->lsfState, bfi, parm, lsp_mid, lsp_new); + + // Advance synthesis parameters pointer + parm += 5; + + Int_lpc_1and3 (st->lsp_old, lsp_mid, lsp_new, A_t); + } + + // update the LSPs for the next frame + for (i = 0; i < M; i++) + { + st->lsp_old[i] = lsp_new[i]; + } + + *------------------------------------------------------------------------* + * Loop for every subframe in the analysis frame * + *------------------------------------------------------------------------* + * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR * + * times * + * - decode the pitch delay * + * - decode algebraic code * + * - decode pitch and codebook gains * + * - find the excitation and compute synthesis speech * + *------------------------------------------------------------------------* + + // pointer to interpolated LPC parameters + Az = A_t; + + evenSubfr = 0; + subfrNr = -1; + for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) + { + subfrNr = add(subfrNr, 1); + evenSubfr = sub(1, evenSubfr); + + // flag for first and 3th subframe + pit_flag = i_subfr; + + if (sub (i_subfr, L_FRAME_BY2) == 0) + { + if (sub(mode, MR475) != 0 && sub(mode, MR515) != 0) + { + pit_flag = 0; + } + } + + // pitch index + index = *parm++; + + *-------------------------------------------------------* + * - decode pitch lag and find adaptive codebook vector. * + *-------------------------------------------------------* + + if (sub(mode, MR122) != 0) + { + // flag4 indicates encoding with 4 bit resolution; + // this is needed for mode MR475, MR515, MR59 and MR67 + + flag4 = 0; + if ((sub (mode, MR475) == 0) || + (sub (mode, MR515) == 0) || + (sub (mode, MR59) == 0) || + (sub (mode, MR67) == 0) ) { + flag4 = 1; + } + + *-------------------------------------------------------* + * - get ranges for the t0_min and t0_max * + * - only needed in delta decoding * + *-------------------------------------------------------* + + delta_frc_low = 5; + delta_frc_range = 9; + + if ( sub(mode, MR795) == 0 ) + { + delta_frc_low = 10; + delta_frc_range = 19; + } + + t0_min = sub(st->old_T0, delta_frc_low); + if (sub(t0_min, PIT_MIN) < 0) + { + t0_min = PIT_MIN; + } + t0_max = add(t0_min, delta_frc_range); + if (sub(t0_max, PIT_MAX) > 0) + { + t0_max = PIT_MAX; + t0_min = sub(t0_max, delta_frc_range); + } + + Dec_lag3 (index, t0_min, t0_max, pit_flag, st->old_T0, + &T0, &T0_frac, flag4); + + st->T0_lagBuff = T0; + + if (bfi != 0) + { + if (sub (st->old_T0, PIT_MAX) < 0) + { // Graceful pitch + st->old_T0 = add(st->old_T0, 1); // degradation + } + T0 = st->old_T0; + T0_frac = 0; + + if ( st->inBackgroundNoise != 0 && + sub(st->voicedHangover, 4) > 0 && + ((sub(mode, MR475) == 0 ) || + (sub(mode, MR515) == 0 ) || + (sub(mode, MR59) == 0) ) + ) + { + T0 = st->T0_lagBuff; + } + } + + Pred_lt_3or6 (st->exc, T0, T0_frac, L_SUBFR, 1); + } + else + { + Dec_lag6 (index, PIT_MIN_MR122, + PIT_MAX, pit_flag, &T0, &T0_frac); + + if ( bfi == 0 && (pit_flag == 0 || sub (index, 61) < 0)) + { + } + else + { + st->T0_lagBuff = T0; + T0 = st->old_T0; + T0_frac = 0; + } + + Pred_lt_3or6 (st->exc, T0, T0_frac, L_SUBFR, 0); + } + + *-------------------------------------------------------* + * - (MR122 only: Decode pitch gain.) * + * - Decode innovative codebook. * + * - set pitch sharpening factor * + *-------------------------------------------------------* + + if (sub (mode, MR475) == 0 || sub (mode, MR515) == 0) + { // MR475, MR515 + index = *parm++; // index of position + i = *parm++; // signs + + decode_2i40_9bits (subfrNr, i, index, code); + + pit_sharp = shl (st->sharp, 1); + } + else if (sub (mode, MR59) == 0) + { // MR59 + index = *parm++; // index of position + i = *parm++; // signs + + decode_2i40_11bits (i, index, code); + + pit_sharp = shl (st->sharp, 1); + } + else if (sub (mode, MR67) == 0) + { // MR67 + index = *parm++; // index of position + i = *parm++; // signs + + decode_3i40_14bits (i, index, code); + + pit_sharp = shl (st->sharp, 1); + } + else if (sub (mode, MR795) <= 0) + { // MR74, MR795 + index = *parm++; // index of position + i = *parm++; // signs + + decode_4i40_17bits (i, index, code); + + pit_sharp = shl (st->sharp, 1); + } + else if (sub (mode, MR102) == 0) + { // MR102 + dec_8i40_31bits (parm, code); + parm += 7; + + pit_sharp = shl (st->sharp, 1); + } + else + { // MR122 + index = *parm++; + if (bfi != 0) + { + ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit); + } + else + { + gain_pit = d_gain_pitch (mode, index); + } + ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf, + &gain_pit); + + dec_10i40_35bits (parm, code); + parm += 10; + + // pit_sharp = gain_pit; + // if (pit_sharp > 1.0) pit_sharp = 1.0; + + pit_sharp = shl (gain_pit, 1); + } + + *-------------------------------------------------------* + * - Add the pitch contribution to code[]. * + *-------------------------------------------------------* + for (i = T0; i < L_SUBFR; i++) + { + temp = mult (code[i - T0], pit_sharp); + code[i] = add (code[i], temp); + } + + *------------------------------------------------------------* + * - Decode codebook gain (MR122) or both pitch * + * gain and codebook gain (all others) * + * - Update pitch sharpening "sharp" with quantized gain_pit * + *------------------------------------------------------------* + + if (sub (mode, MR475) == 0) + { + // read and decode pitch and code gain + if (evenSubfr != 0) + { + index_mr475 = *parm++; // index of gain(s) + } + + if (bfi == 0) + { + Dec_gain(st->pred_state, mode, index_mr475, code, + evenSubfr, &gain_pit, &gain_code); + } + else + { + ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit); + ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state, + &gain_code); + } + ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf, + &gain_pit); + ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf, + &gain_code); + + pit_sharp = gain_pit; + if (sub (pit_sharp, SHARPMAX) > 0) + { + pit_sharp = SHARPMAX; + } + + } + else if ((sub (mode, MR74) <= 0) || + (sub (mode, MR102) == 0)) + { + // read and decode pitch and code gain + index = *parm++; // index of gain(s) + + if (bfi == 0) + { + Dec_gain(st->pred_state, mode, index, code, + evenSubfr, &gain_pit, &gain_code); + } + else + { + ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit); + ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state, + &gain_code); + } + ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf, + &gain_pit); + ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf, + &gain_code); + + pit_sharp = gain_pit; + if (sub (pit_sharp, SHARPMAX) > 0) + { + pit_sharp = SHARPMAX; + } + + if (sub (mode, MR102) == 0) + { + if (sub (st->old_T0, add(L_SUBFR, 5)) > 0) + { + pit_sharp = shr(pit_sharp, 2); + } + } + } + else + { + // read and decode pitch gain + index = *parm++; // index of gain(s) + + if (sub (mode, MR795) == 0) + { + // decode pitch gain + if (bfi != 0) + { + ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit); + } + else + { + gain_pit = d_gain_pitch (mode, index); + } + ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf, + &gain_pit); + + // read and decode code gain + index = *parm++; + if (bfi == 0) + { + d_gain_code (st->pred_state, mode, index, code, &gain_code); + } + else + { + ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state, + &gain_code); + } + ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf, + &gain_code); + + pit_sharp = gain_pit; + if (sub (pit_sharp, SHARPMAX) > 0) + { + pit_sharp = SHARPMAX; + } + } + else + { // MR122 + if (bfi == 0) + { + d_gain_code (st->pred_state, mode, index, code, &gain_code); + } + else + { + ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state, + &gain_code); + } + ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf, + &gain_code); + + pit_sharp = gain_pit; + } + } + + // store pitch sharpening for next subframe + // (for modes which use the previous pitch gain for + // pitch sharpening in the search phase) + // do not update sharpening in even subframes for MR475 + if (sub(mode, MR475) != 0 || evenSubfr == 0) + { + st->sharp = gain_pit; + if (sub (st->sharp, SHARPMAX) > 0) + { + st->sharp = SHARPMAX; + } + } + + pit_sharp = shl (pit_sharp, 1); + if (sub (pit_sharp, 16384) > 0) + { + for (i = 0; i < L_SUBFR; i++) + { + temp = mult (st->exc[i], pit_sharp); + L_temp = L_mult (temp, gain_pit); + if (sub(mode, MR122)==0) + { + L_temp = L_shr (L_temp, 1); + } + excp[i] = pv_round (L_temp); + } + } + + *-------------------------------------------------------* + * - Store list of LTP gains needed in the source * + * characteristic detector (SCD) * + *-------------------------------------------------------* + if ( bfi == 0 ) + { + for (i = 0; i < 8; i++) + { + st->ltpGainHistory[i] = st->ltpGainHistory[i+1]; + } + st->ltpGainHistory[8] = gain_pit; + } + + *-------------------------------------------------------* + * - Limit gain_pit if in background noise and BFI * + * for MR475, MR515, MR59 * + *-------------------------------------------------------* + + if ( (st->prev_bf != 0 || bfi != 0) && st->inBackgroundNoise != 0 && + ((sub(mode, MR475) == 0) || + (sub(mode, MR515) == 0) || + (sub(mode, MR59) == 0)) + ) + { + if ( sub (gain_pit, 12288) > 0) // if (gain_pit > 0.75) in Q14 + gain_pit = add( shr( sub(gain_pit, 12288), 1 ), 12288 ); + // gain_pit = (gain_pit-0.75)/2.0 + 0.75; + + if ( sub (gain_pit, 14745) > 0) // if (gain_pit > 0.90) in Q14 + { + gain_pit = 14745; + } + } + + *-------------------------------------------------------* + * Calculate CB mixed gain * + *-------------------------------------------------------* + Int_lsf(prev_lsf, st->lsfState->past_lsf_q, i_subfr, lsf_i); + gain_code_mix = Cb_gain_average( + st->Cb_gain_averState, mode, gain_code, + lsf_i, st->lsp_avg_st->lsp_meanSave, bfi, + st->prev_bf, pdfi, st->prev_pdf, + st->inBackgroundNoise, st->voicedHangover); + + // make sure that MR74, MR795, MR122 have original code_gain + if ((sub(mode, MR67) > 0) && (sub(mode, MR102) != 0) ) + // MR74, MR795, MR122 + { + gain_code_mix = gain_code; + } + + *-------------------------------------------------------* + * - Find the total excitation. * + * - Find synthesis speech corresponding to st->exc[]. * + *-------------------------------------------------------* + if (sub(mode, MR102) <= 0) // MR475, MR515, MR59, MR67, MR74, MR795, MR102 + { + pitch_fac = gain_pit; + tmp_shift = 1; + } + else // MR122 + { + pitch_fac = shr (gain_pit, 1); + tmp_shift = 2; + } + + // copy unscaled LTP excitation to exc_enhanced (used in phase + * dispersion below) and compute total excitation for LTP feedback + + for (i = 0; i < L_SUBFR; i++) + { + exc_enhanced[i] = st->exc[i]; + + // st->exc[i] = gain_pit*st->exc[i] + gain_code*code[i]; + L_temp = L_mult (st->exc[i], pitch_fac); + // 12.2: Q0 * Q13 + // 7.4: Q0 * Q14 + L_temp = L_mac (L_temp, code[i], gain_code); + // 12.2: Q12 * Q1 + // 7.4: Q13 * Q1 + L_temp = L_shl (L_temp, tmp_shift); // Q16 + st->exc[i] = pv_round (L_temp); + } + + *-------------------------------------------------------* + * - Adaptive phase dispersion * + *-------------------------------------------------------* + ph_disp_release(st->ph_disp_st); // free phase dispersion adaption + + if ( ((sub(mode, MR475) == 0) || + (sub(mode, MR515) == 0) || + (sub(mode, MR59) == 0)) && + sub(st->voicedHangover, 3) > 0 && + st->inBackgroundNoise != 0 && + bfi != 0 ) + { + ph_disp_lock(st->ph_disp_st); // Always Use full Phase Disp. + } // if error in bg noise + + // apply phase dispersion to innovation (if enabled) and + compute total excitation for synthesis part + ph_disp(st->ph_disp_st, mode, + exc_enhanced, gain_code_mix, gain_pit, code, + pitch_fac, tmp_shift); + + *-------------------------------------------------------* + * - The Excitation control module are active during BFI.* + * - Conceal drops in signal energy if in bg noise. * + *-------------------------------------------------------* + + L_temp = 0; + for (i = 0; i < L_SUBFR; i++) + { + L_temp = L_mac (L_temp, exc_enhanced[i], exc_enhanced[i] ); + } + + L_temp = L_shr (L_temp, 1); // excEnergy = sqrt(L_temp) in Q0 + L_temp = sqrt_l_exp(L_temp, &temp); // function result + L_temp = L_shr(L_temp, add( shr(temp, 1), 15)); + L_temp = L_shr(L_temp, 2); // To cope with 16-bit and + excEnergy = extract_l(L_temp); // scaling in ex_ctrl() + + if ( ((sub (mode, MR475) == 0) || + (sub (mode, MR515) == 0) || + (sub (mode, MR59) == 0)) && + sub(st->voicedHangover, 5) > 0 && + st->inBackgroundNoise != 0 && + sub(st->state, 4) < 0 && + ( (pdfi != 0 && st->prev_pdf != 0) || + bfi != 0 || + st->prev_bf != 0) ) + { + carefulFlag = 0; + if ( pdfi != 0 && bfi == 0 ) + { + carefulFlag = 1; + } + + Ex_ctrl(exc_enhanced, + excEnergy, + st->excEnergyHist, + st->voicedHangover, + st->prev_bf, + carefulFlag); + } + + if ( st->inBackgroundNoise != 0 && + ( bfi != 0 || st->prev_bf != 0 ) && + sub(st->state, 4) < 0 ) + { + ; // do nothing! + } + else + { + // Update energy history for all modes + for (i = 0; i < 8; i++) + { + st->excEnergyHist[i] = st->excEnergyHist[i+1]; + } + st->excEnergyHist[8] = excEnergy; + } + *-------------------------------------------------------* + * Excitation control module end. * + *-------------------------------------------------------* + + if (sub (pit_sharp, 16384) > 0) + { + for (i = 0; i < L_SUBFR; i++) + { + excp[i] = add (excp[i], exc_enhanced[i]); + } + agc2 (exc_enhanced, excp, L_SUBFR); + Overflow = 0; + Syn_filt (Az, excp, &synth[i_subfr], L_SUBFR, + st->mem_syn, 0); + } + else + { + Overflow = 0; + Syn_filt (Az, exc_enhanced, &synth[i_subfr], L_SUBFR, + st->mem_syn, 0); + } + + if (Overflow != 0) // Test for overflow + { + for (i = 0; i < PIT_MAX + L_INTERPOL + L_SUBFR; i++) + { + st->old_exc[i] = shr(st->old_exc[i], 2); + } + for (i = 0; i < L_SUBFR; i++) + { + exc_enhanced[i] = shr(exc_enhanced[i], 2); + } + Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->mem_syn, 1); + } + else + { + Copy(&synth[i_subfr+L_SUBFR-M], st->mem_syn, M); + } + + *--------------------------------------------------* + * Update signal for next frame. * + * -> shift to the left by L_SUBFR st->exc[] * + *--------------------------------------------------* + + Copy (&st->old_exc[L_SUBFR], &st->old_exc[0], PIT_MAX + L_INTERPOL); + + // interpolated LPC parameters for next subframe + Az += MP1; + + // store T0 for next subframe + st->old_T0 = T0; + } + + *-------------------------------------------------------* + * Call the Source Characteristic Detector which updates * + * st->inBackgroundNoise and st->voicedHangover. * + *-------------------------------------------------------* + + st->inBackgroundNoise = Bgn_scd(st->background_state, + &(st->ltpGainHistory[0]), + &(synth[0]), + &(st->voicedHangover) ); + + dtx_dec_activity_update(st->dtxDecoderState, + st->lsfState->past_lsf_q, + synth); + + // store bfi for next subframe + st->prev_bf = bfi; + st->prev_pdf = pdfi; + + *--------------------------------------------------* + * Calculate the LSF averages on the eight * + * previous frames * + *--------------------------------------------------* + + lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q); + +the_end: + st->dtxDecoderState->dtxGlobalState = newDTXState; + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void Decoder_amr( + Decoder_amrState *st, /* i/o : State variables */ + enum Mode mode, /* i : AMR mode */ + Word16 parm[], /* i : vector of synthesis parameters + (PRM_SIZE) */ + enum RXFrameType frame_type, /* i : received frame type */ + Word16 synth[], /* o : synthesis speech (L_FRAME) */ + Word16 A_t[] /* o : decoded LP filter in 4 subframes + (AZ_SIZE) */ +) +{ + /* LPC coefficients */ + + Word16 *Az; /* Pointer on A_t */ + + /* LSPs */ + + Word16 lsp_new[M]; + Word16 lsp_mid[M]; + + /* LSFs */ + + Word16 prev_lsf[M]; + Word16 lsf_i[M]; + + /* Algebraic codevector */ + + Word16 code[L_SUBFR]; + + /* excitation */ + + Word16 excp[L_SUBFR]; + Word16 exc_enhanced[L_SUBFR]; + + /* Scalars */ + + Word16 i; + Word16 i_subfr; + Word16 T0; + Word16 T0_frac; + Word16 index; + Word16 index_mr475 = 0; + Word16 gain_pit; + Word16 gain_code; + Word16 gain_code_mix; + Word16 pit_sharp; + Word16 pit_flag; + Word16 pitch_fac; + Word16 t0_min; + Word16 t0_max; + Word16 delta_frc_low; + Word16 delta_frc_range; + Word16 tmp_shift; + Word16 temp; + Word32 L_temp; + Word16 flag4; + Word16 carefulFlag; + Word16 excEnergy; + Word16 subfrNr; + Word16 evenSubfr = 0; + + Word16 bfi = 0; /* bad frame indication flag */ + Word16 pdfi = 0; /* potential degraded bad frame flag */ + + enum DTXStateType newDTXState; /* SPEECH , DTX, DTX_MUTE */ + Flag *pOverflow = &(st->overflow); /* Overflow flag */ + + + /* find the new DTX state SPEECH OR DTX */ + newDTXState = rx_dtx_handler(&(st->dtxDecoderState), frame_type, pOverflow); + + /* DTX actions */ + + if (newDTXState != SPEECH) + { + Decoder_amr_reset(st, MRDTX); + + dtx_dec(&(st->dtxDecoderState), + st->mem_syn, + &(st->lsfState), + &(st->pred_state), + &(st->Cb_gain_averState), + newDTXState, + mode, + parm, &(st->common_amr_tbls), synth, A_t, pOverflow); + + /* update average lsp */ + Lsf_lsp( + st->lsfState.past_lsf_q, + st->lsp_old, + M, + pOverflow); + + lsp_avg( + &(st->lsp_avg_st), + st->lsfState.past_lsf_q, + pOverflow); + + goto the_end; + } + + /* SPEECH action state machine */ + if ((frame_type == RX_SPEECH_BAD) || (frame_type == RX_NO_DATA) || + (frame_type == RX_ONSET)) + { + bfi = 1; + + if ((frame_type == RX_NO_DATA) || (frame_type == RX_ONSET)) + { + build_CN_param(&st->nodataSeed, + st->common_amr_tbls.prmno_ptr[mode], + st->common_amr_tbls.bitno_ptr[mode], + parm, + st->common_amr_tbls.window_200_40_ptr, + pOverflow); + } + } + else if (frame_type == RX_SPEECH_DEGRADED) + { + pdfi = 1; + } + + if (bfi != 0) + { + st->state += 1; + } + else if (st->state == 6) + + { + st->state = 5; + } + else + { + st->state = 0; + } + + + if (st->state > 6) + { + st->state = 6; + } + + /* If this frame is the first speech frame after CNI period, */ + /* set the BFH state machine to an appropriate state depending */ + /* on whether there was DTX muting before start of speech or not */ + /* If there was DTX muting, the first speech frame is muted. */ + /* If there was no DTX muting, the first speech frame is not */ + /* muted. The BFH state machine starts from state 5, however, to */ + /* keep the audible noise resulting from a SID frame which is */ + /* erroneously interpreted as a good speech frame as small as */ + /* possible (the decoder output in this case is quickly muted) */ + + if (st->dtxDecoderState.dtxGlobalState == DTX) + { + st->state = 5; + st->prev_bf = 0; + } + else if (st->dtxDecoderState.dtxGlobalState == DTX_MUTE) + { + st->state = 5; + st->prev_bf = 1; + } + + /* save old LSFs for CB gain smoothing */ + oscl_memmove((void *)prev_lsf, st->lsfState.past_lsf_q, M*sizeof(*st->lsfState.past_lsf_q)); + + /* decode LSF parameters and generate interpolated lpc coefficients + for the 4 subframes */ + + if (mode != MR122) + { + D_plsf_3( + &(st->lsfState), + mode, + bfi, + parm, + &st->common_amr_tbls, + lsp_new, + pOverflow); + + /* Advance synthesis parameters pointer */ + parm += 3; + + Int_lpc_1to3( + st->lsp_old, + lsp_new, + A_t, + pOverflow); + } + else + { + D_plsf_5( + &(st->lsfState), + bfi, + parm, + &(st->common_amr_tbls), + lsp_mid, + lsp_new, + pOverflow); + + /* Advance synthesis parameters pointer */ + parm += 5; + + Int_lpc_1and3( + st->lsp_old, + lsp_mid, + lsp_new, + A_t, + pOverflow); + } + + /* update the LSPs for the next frame */ + for (i = 0; i < M; i++) + { + st->lsp_old[i] = lsp_new[i]; + } + + /*------------------------------------------------------------------------* + * Loop for every subframe in the analysis frame * + *------------------------------------------------------------------------* + * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR * + * times * + * - decode the pitch delay * + * - decode algebraic code * + * - decode pitch and codebook gains * + * - find the excitation and compute synthesis speech * + *------------------------------------------------------------------------*/ + + /* pointer to interpolated LPC parameters */ + Az = A_t; + + evenSubfr = 0; + subfrNr = -1; + for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) + { + subfrNr += 1; + evenSubfr = 1 - evenSubfr; + + /* flag for first and 3th subframe */ + pit_flag = i_subfr; + + + if (i_subfr == L_FRAME_BY2) + { + if ((mode != MR475) && (mode != MR515)) + { + pit_flag = 0; + } + } + + /* pitch index */ + index = *parm++; + + /*-------------------------------------------------------* + * - decode pitch lag and find adaptive codebook vector. * + *-------------------------------------------------------*/ + + if (mode != MR122) + { + /* flag4 indicates encoding with 4 bit resolution; */ + /* this is needed for mode MR475, MR515, MR59 and MR67 */ + + flag4 = 0; + + if ((mode == MR475) || (mode == MR515) || (mode == MR59) || + (mode == MR67)) + { + flag4 = 1; + } + + /*-------------------------------------------------------* + * - get ranges for the t0_min and t0_max * + * - only needed in delta decoding * + *-------------------------------------------------------*/ + + delta_frc_low = 5; + delta_frc_range = 9; + + if (mode == MR795) + { + delta_frc_low = 10; + delta_frc_range = 19; + } + + t0_min = st->old_T0 - delta_frc_low; + + if (t0_min < PIT_MIN) + { + t0_min = PIT_MIN; + } + t0_max = t0_min + delta_frc_range; + + if (t0_max > PIT_MAX) + { + t0_max = PIT_MAX; + t0_min = t0_max - delta_frc_range; + } + + Dec_lag3(index, t0_min, t0_max, pit_flag, st->old_T0, + &T0, &T0_frac, flag4, pOverflow); + + st->T0_lagBuff = T0; + + if (bfi != 0) + { + if (st->old_T0 < PIT_MAX) + { /* Graceful pitch */ + st->old_T0 += 1; /* degradation */ + } + T0 = st->old_T0; + T0_frac = 0; + + if ((st->inBackgroundNoise != 0) && (st->voicedHangover > 4) && + ((mode == MR475) || (mode == MR515) || (mode == MR59))) + { + T0 = st->T0_lagBuff; + } + } + + Pred_lt_3or6(st->exc, T0, T0_frac, L_SUBFR, 1, pOverflow); + } + else + { + Dec_lag6(index, PIT_MIN_MR122, + PIT_MAX, pit_flag, &T0, &T0_frac, pOverflow); + + + if (!(bfi == 0 && (pit_flag == 0 || index < 61))) + { + st->T0_lagBuff = T0; + T0 = st->old_T0; + T0_frac = 0; + } + + Pred_lt_3or6(st->exc, T0, T0_frac, L_SUBFR, 0, pOverflow); + } + + /*-------------------------------------------------------* + * - (MR122 only: Decode pitch gain.) * + * - Decode innovative codebook. * + * - set pitch sharpening factor * + *-------------------------------------------------------*/ + if ((mode == MR475) || (mode == MR515)) + { /* MR475, MR515 */ + index = *parm++; /* index of position */ + i = *parm++; /* signs */ + + decode_2i40_9bits(subfrNr, i, index, st->common_amr_tbls.startPos_ptr, code, pOverflow); + + L_temp = (Word32)st->sharp << 1; + if (L_temp != (Word32)((Word16) L_temp)) + { + pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16; + } + else + { + pit_sharp = (Word16) L_temp; + } + } + else if (mode == MR59) + { /* MR59 */ + index = *parm++; /* index of position */ + i = *parm++; /* signs */ + + decode_2i40_11bits(i, index, code); + + L_temp = (Word32)st->sharp << 1; + if (L_temp != (Word32)((Word16) L_temp)) + { + pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16; + } + else + { + pit_sharp = (Word16) L_temp; + } + } + else if (mode == MR67) + { /* MR67 */ + index = *parm++; /* index of position */ + i = *parm++; /* signs */ + + decode_3i40_14bits(i, index, code); + + L_temp = (Word32)st->sharp << 1; + if (L_temp != (Word32)((Word16) L_temp)) + { + pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16; + } + else + { + pit_sharp = (Word16) L_temp; + } + } + else if (mode <= MR795) + { /* MR74, MR795 */ + index = *parm++; /* index of position */ + i = *parm++; /* signs */ + + decode_4i40_17bits(i, index, st->common_amr_tbls.dgray_ptr, code); + + L_temp = (Word32)st->sharp << 1; + if (L_temp != (Word32)((Word16) L_temp)) + { + pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16; + } + else + { + pit_sharp = (Word16) L_temp; + } + } + else if (mode == MR102) + { /* MR102 */ + dec_8i40_31bits(parm, code, pOverflow); + parm += 7; + + L_temp = (Word32)st->sharp << 1; + if (L_temp != (Word32)((Word16) L_temp)) + { + pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16; + } + else + { + pit_sharp = (Word16) L_temp; + } + } + else + { /* MR122 */ + index = *parm++; + + if (bfi != 0) + { + ec_gain_pitch( + &(st->ec_gain_p_st), + st->state, + &gain_pit, + pOverflow); + } + else + { + gain_pit = d_gain_pitch(mode, index, st->common_amr_tbls.qua_gain_pitch_ptr); + } + ec_gain_pitch_update( + &(st->ec_gain_p_st), + bfi, + st->prev_bf, + &gain_pit, + pOverflow); + + + dec_10i40_35bits(parm, code, st->common_amr_tbls.dgray_ptr); + parm += 10; + + /* pit_sharp = gain_pit; */ + /* if (pit_sharp > 1.0) pit_sharp = 1.0; */ + + L_temp = (Word32)gain_pit << 1; + if (L_temp != (Word32)((Word16) L_temp)) + { + pit_sharp = (gain_pit > 0) ? MAX_16 : MIN_16; + } + else + { + pit_sharp = (Word16) L_temp; + } + } + /*-------------------------------------------------------* + * - Add the pitch contribution to code[]. * + *-------------------------------------------------------*/ + for (i = T0; i < L_SUBFR; i++) + { + temp = mult(*(code + i - T0), pit_sharp, pOverflow); + *(code + i) = add_16(*(code + i), temp, pOverflow); + + } + + /*------------------------------------------------------------* + * - Decode codebook gain (MR122) or both pitch * + * gain and codebook gain (all others) * + * - Update pitch sharpening "sharp" with quantized gain_pit * + *------------------------------------------------------------*/ + if (mode == MR475) + { + /* read and decode pitch and code gain */ + + if (evenSubfr != 0) + { + index_mr475 = *parm++; /* index of gain(s) */ + } + + if (bfi == 0) + { + Dec_gain( + &(st->pred_state), + mode, + index_mr475, + code, + evenSubfr, + &gain_pit, + &gain_code, + &(st->common_amr_tbls), + pOverflow); + } + else + { + ec_gain_pitch( + &(st->ec_gain_p_st), + st->state, + &gain_pit, + pOverflow); + + ec_gain_code( + &(st->ec_gain_c_st), + &(st->pred_state), + st->state, + &gain_code, + pOverflow); + } + ec_gain_pitch_update( + &st->ec_gain_p_st, + bfi, + st->prev_bf, + &gain_pit, + pOverflow); + + ec_gain_code_update( + &st->ec_gain_c_st, + bfi, + st->prev_bf, + &gain_code, + pOverflow); + + pit_sharp = gain_pit; + + if (pit_sharp > SHARPMAX) + { + pit_sharp = SHARPMAX; + } + + } + else if ((mode <= MR74) || (mode == MR102)) + { + /* read and decode pitch and code gain */ + index = *parm++; /* index of gain(s) */ + + if (bfi == 0) + { + Dec_gain( + &(st->pred_state), + mode, + index, + code, + evenSubfr, + &gain_pit, + &gain_code, + &(st->common_amr_tbls), + pOverflow); + } + else + { + ec_gain_pitch( + &(st->ec_gain_p_st), + st->state, + &gain_pit, + pOverflow); + + ec_gain_code( + &(st->ec_gain_c_st), + &(st->pred_state), + st->state, + &gain_code, + pOverflow); + } + + ec_gain_pitch_update( + &(st->ec_gain_p_st), + bfi, + st->prev_bf, + &gain_pit, + pOverflow); + + ec_gain_code_update( + &(st->ec_gain_c_st), + bfi, + st->prev_bf, + &gain_code, + pOverflow); + + pit_sharp = gain_pit; + + if (pit_sharp > SHARPMAX) + { + pit_sharp = SHARPMAX; + } + + if (mode == MR102) + { + if (st->old_T0 > (L_SUBFR + 5)) + { + if (pit_sharp < 0) + { + pit_sharp = ~((~pit_sharp) >> 2); + } + else + { + pit_sharp = pit_sharp >> 2; + } + } + } + } + else + { + /* read and decode pitch gain */ + index = *parm++; /* index of gain(s) */ + + if (mode == MR795) + { + /* decode pitch gain */ + if (bfi != 0) + { + ec_gain_pitch( + &(st->ec_gain_p_st), + st->state, + &gain_pit, + pOverflow); + } + else + { + gain_pit = d_gain_pitch(mode, index, st->common_amr_tbls.qua_gain_pitch_ptr); + } + ec_gain_pitch_update( + &(st->ec_gain_p_st), + bfi, + st->prev_bf, + &gain_pit, + pOverflow); + + /* read and decode code gain */ + index = *parm++; + + if (bfi == 0) + { + d_gain_code( + &(st->pred_state), + mode, + index, + code, + st->common_amr_tbls.qua_gain_code_ptr, + &gain_code, + pOverflow); + } + else + { + ec_gain_code( + &(st->ec_gain_c_st), + &(st->pred_state), + st->state, + &gain_code, + pOverflow); + } + + ec_gain_code_update( + &(st->ec_gain_c_st), + bfi, + st->prev_bf, + &gain_code, + pOverflow); + + pit_sharp = gain_pit; + + if (pit_sharp > SHARPMAX) + { + pit_sharp = SHARPMAX; + } + } + else + { /* MR122 */ + + if (bfi == 0) + { + d_gain_code( + &(st->pred_state), + mode, + index, + code, + st->common_amr_tbls.qua_gain_code_ptr, + &gain_code, + pOverflow); + } + else + { + ec_gain_code( + &(st->ec_gain_c_st), + &(st->pred_state), + st->state, + &gain_code, + pOverflow); + } + + ec_gain_code_update( + &(st->ec_gain_c_st), + bfi, + st->prev_bf, + &gain_code, + pOverflow); + + pit_sharp = gain_pit; + } + } + + /* store pitch sharpening for next subframe */ + /* (for modes which use the previous pitch gain for */ + /* pitch sharpening in the search phase) */ + /* do not update sharpening in even subframes for MR475 */ + if ((mode != MR475) || (evenSubfr == 0)) + { + st->sharp = gain_pit; + + if (st->sharp > SHARPMAX) + { + st->sharp = SHARPMAX; + } + } + + pit_sharp = shl(pit_sharp, 1, pOverflow); + + if (pit_sharp > 16384) + { + for (i = 0; i < L_SUBFR; i++) + { + temp = mult(st->exc[i], pit_sharp, pOverflow); + L_temp = L_mult(temp, gain_pit, pOverflow); + + if (mode == MR122) + { + if (L_temp < 0) + { + L_temp = ~((~L_temp) >> 1); + } + else + { + L_temp = L_temp >> 1; + } + } + *(excp + i) = pv_round(L_temp, pOverflow); + } + } + + /*-------------------------------------------------------* + * - Store list of LTP gains needed in the source * + * characteristic detector (SCD) * + *-------------------------------------------------------*/ + + if (bfi == 0) + { + for (i = 0; i < 8; i++) + { + st->ltpGainHistory[i] = st->ltpGainHistory[i+1]; + } + st->ltpGainHistory[8] = gain_pit; + } + + /*-------------------------------------------------------* + * - Limit gain_pit if in background noise and BFI * + * for MR475, MR515, MR59 * + *-------------------------------------------------------*/ + + + if ((st->prev_bf != 0 || bfi != 0) && st->inBackgroundNoise != 0 && + ((mode == MR475) || (mode == MR515) || (mode == MR59))) + { + + if (gain_pit > 12288) /* if (gain_pit > 0.75) in Q14*/ + { + gain_pit = ((gain_pit - 12288) >> 1) + 12288; + /* gain_pit = (gain_pit-0.75)/2.0 + 0.75; */ + } + + if (gain_pit > 14745) /* if (gain_pit > 0.90) in Q14*/ + { + gain_pit = 14745; + } + } + + /*-------------------------------------------------------* + * Calculate CB mixed gain * + *-------------------------------------------------------*/ + Int_lsf( + prev_lsf, + st->lsfState.past_lsf_q, + i_subfr, + lsf_i, + pOverflow); + + gain_code_mix = + Cb_gain_average( + &(st->Cb_gain_averState), + mode, + gain_code, + lsf_i, + st->lsp_avg_st.lsp_meanSave, + bfi, + st->prev_bf, + pdfi, + st->prev_pdf, + st->inBackgroundNoise, + st->voicedHangover, + pOverflow); + + /* make sure that MR74, MR795, MR122 have original code_gain*/ + if ((mode > MR67) && (mode != MR102)) + /* MR74, MR795, MR122 */ + { + gain_code_mix = gain_code; + } + + /*-------------------------------------------------------* + * - Find the total excitation. * + * - Find synthesis speech corresponding to st->exc[]. * + *-------------------------------------------------------*/ + if (mode <= MR102) /* MR475, MR515, MR59, MR67, MR74, MR795, MR102*/ + { + pitch_fac = gain_pit; + tmp_shift = 1; + } + else /* MR122 */ + { + if (gain_pit < 0) + { + pitch_fac = ~((~gain_pit) >> 1); + } + else + { + pitch_fac = gain_pit >> 1; + } + tmp_shift = 2; + } + + /* copy unscaled LTP excitation to exc_enhanced (used in phase + * dispersion below) and compute total excitation for LTP feedback + */ + for (i = 0; i < L_SUBFR; i++) + { + exc_enhanced[i] = st->exc[i]; + + /* st->exc[i] = gain_pit*st->exc[i] + gain_code*code[i]; */ + L_temp = L_mult(st->exc[i], pitch_fac, pOverflow); + /* 12.2: Q0 * Q13 */ + /* 7.4: Q0 * Q14 */ + L_temp = L_mac(L_temp, code[i], gain_code, pOverflow); + /* 12.2: Q12 * Q1 */ + /* 7.4: Q13 * Q1 */ + L_temp = L_shl(L_temp, tmp_shift, pOverflow); /* Q16 */ + st->exc[i] = pv_round(L_temp, pOverflow); + } + + /*-------------------------------------------------------* + * - Adaptive phase dispersion * + *-------------------------------------------------------*/ + ph_disp_release(&(st->ph_disp_st)); /* free phase dispersion adaption */ + + + if (((mode == MR475) || (mode == MR515) || (mode == MR59)) && + (st->voicedHangover > 3) && (st->inBackgroundNoise != 0) && + (bfi != 0)) + { + ph_disp_lock(&(st->ph_disp_st)); /* Always Use full Phase Disp. */ + } /* if error in bg noise */ + + /* apply phase dispersion to innovation (if enabled) and + compute total excitation for synthesis part */ + ph_disp( + &(st->ph_disp_st), + mode, + exc_enhanced, + gain_code_mix, + gain_pit, + code, + pitch_fac, + tmp_shift, + &(st->common_amr_tbls), + pOverflow); + + /*-------------------------------------------------------* + * - The Excitation control module are active during BFI.* + * - Conceal drops in signal energy if in bg noise. * + *-------------------------------------------------------*/ + L_temp = 0; + for (i = 0; i < L_SUBFR; i++) + { + L_temp = L_mac(L_temp, *(exc_enhanced + i), *(exc_enhanced + i), pOverflow); + } + + /* excEnergy = sqrt(L_temp) in Q0 */ + if (L_temp < 0) + { + L_temp = ~((~L_temp) >> 1); + } + else + { + L_temp = L_temp >> 1; + } + + L_temp = sqrt_l_exp(L_temp, &temp, pOverflow); + /* To cope with 16-bit and scaling in ex_ctrl() */ + L_temp = L_shr(L_temp, (Word16)((temp >> 1) + 15), pOverflow); + if (L_temp < 0) + { + excEnergy = (Word16)(~((~L_temp) >> 2)); + } + else + { + excEnergy = (Word16)(L_temp >> 2); + } + + if (((mode == MR475) || (mode == MR515) || (mode == MR59)) && + (st->voicedHangover > 5) && (st->inBackgroundNoise != 0) && + (st->state < 4) && + ((pdfi != 0 && st->prev_pdf != 0) || bfi != 0 || st->prev_bf != 0)) + { + carefulFlag = 0; + + if (pdfi != 0 && bfi == 0) + { + carefulFlag = 1; + } + + Ex_ctrl(exc_enhanced, + excEnergy, + st->excEnergyHist, + st->voicedHangover, + st->prev_bf, + carefulFlag, pOverflow); + } + + if (!((st->inBackgroundNoise != 0) && (bfi != 0 || st->prev_bf != 0) && + (st->state < 4))) + { + /* Update energy history for all modes */ + for (i = 0; i < 8; i++) + { + st->excEnergyHist[i] = st->excEnergyHist[i+1]; + } + st->excEnergyHist[8] = excEnergy; + } + /*-------------------------------------------------------* + * Excitation control module end. * + *-------------------------------------------------------*/ + if (pit_sharp > 16384) + { + for (i = 0; i < L_SUBFR; i++) + { + *(excp + i) = add_16(*(excp + i), *(exc_enhanced + i), pOverflow); + + } + agc2(exc_enhanced, excp, L_SUBFR, pOverflow); + *pOverflow = 0; + Syn_filt(Az, excp, &synth[i_subfr], L_SUBFR, + st->mem_syn, 0); + } + else + { + *pOverflow = 0; + Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, + st->mem_syn, 0); + } + + if (*pOverflow != 0) /* Test for overflow */ + { + for (i = PIT_MAX + L_INTERPOL + L_SUBFR - 1; i >= 0; i--) + { + if (st->old_exc[i] < 0) + { + st->old_exc[i] = ~((~st->old_exc[i]) >> 2); + } + else + { + st->old_exc[i] = st->old_exc[i] >> 2; + } + + } + + for (i = L_SUBFR - 1; i >= 0; i--) + { + if (*(exc_enhanced + i) < 0) + { + *(exc_enhanced + i) = ~((~(*(exc_enhanced + i))) >> 2); + } + else + { + *(exc_enhanced + i) = *(exc_enhanced + i) >> 2; + } + } + Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->mem_syn, 1); + } + else + { + oscl_memmove((void *)st->mem_syn, &synth[i_subfr+L_SUBFR-M], M*sizeof(synth[0])); + } + + /*--------------------------------------------------* + * Update signal for next frame. * + * -> shift to the left by L_SUBFR st->exc[] * + *--------------------------------------------------*/ + + oscl_memmove((void *)&st->old_exc[0], &st->old_exc[L_SUBFR], (PIT_MAX + L_INTERPOL)*sizeof(st->old_exc[0])); + + /* interpolated LPC parameters for next subframe */ + Az += MP1; + + /* store T0 for next subframe */ + st->old_T0 = T0; + } + + /*-------------------------------------------------------* + * Call the Source Characteristic Detector which updates * + * st->inBackgroundNoise and st->voicedHangover. * + *-------------------------------------------------------*/ + + st->inBackgroundNoise = + Bgn_scd( + &(st->background_state), + &(st->ltpGainHistory[0]), + &(synth[0]), + &(st->voicedHangover), + pOverflow); + + dtx_dec_activity_update( + &(st->dtxDecoderState), + st->lsfState.past_lsf_q, + synth, + pOverflow); + + /* store bfi for next subframe */ + st->prev_bf = bfi; + st->prev_pdf = pdfi; + + /*--------------------------------------------------* + * Calculate the LSF averages on the eight * + * previous frames * + *--------------------------------------------------*/ + lsp_avg( + &(st->lsp_avg_st), + st->lsfState.past_lsf_q, + pOverflow); + +the_end: + st->dtxDecoderState.dtxGlobalState = newDTXState; + +// return(0); +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_amr.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_amr.h new file mode 100644 index 0000000..3066ff7 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_amr.h @@ -0,0 +1,202 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dec_amr.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : dec_amr.h + Purpose : Speech decoder routine. + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef DEC_AMR_H +#define DEC_AMR_H "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "cnst.h" +#include "mode.h" +#include "dtx_dec.h" +#include "d_plsf.h" +#include "gc_pred.h" +#include "ec_gains.h" +#include "ph_disp.h" +#include "c_g_aver.h" +#include "bgnscd.h" +#include "lsp_avg.h" +#include "frame.h" +#include "get_const_tbls.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ +#define EXC_ENERGY_HIST_LEN 9 +#define LTP_GAIN_HISTORY_LEN 9 + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + /* Excitation vector */ + Word16 old_exc[L_SUBFR + PIT_MAX + L_INTERPOL]; + Word16 *exc; + + /* Lsp (Line spectral pairs) */ + /* Word16 lsp[M]; */ /* Used by CN codec */ + Word16 lsp_old[M]; + + /* Filter's memory */ + Word16 mem_syn[M]; + + /* pitch sharpening */ + Word16 sharp; + Word16 old_T0; + + /* Memories for bad frame handling */ + Word16 prev_bf; + Word16 prev_pdf; + Word16 state; + Word16 excEnergyHist[EXC_ENERGY_HIST_LEN]; + + /* Variable holding received ltpLag, used in background noise and BFI */ + Word16 T0_lagBuff; + + /* Variables for the source characteristic detector (SCD) */ + Word16 inBackgroundNoise; + Word16 voicedHangover; + Word16 ltpGainHistory[LTP_GAIN_HISTORY_LEN]; + + Bgn_scdState background_state; + Word16 nodataSeed; + + Cb_gain_averageState Cb_gain_averState; + lsp_avgState lsp_avg_st; + + D_plsfState lsfState; + ec_gain_pitchState ec_gain_p_st; + ec_gain_codeState ec_gain_c_st; + gc_predState pred_state; + ph_dispState ph_disp_st; + dtx_decState dtxDecoderState; + Flag overflow; + CommonAmrTbls common_amr_tbls; + } Decoder_amrState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + /* + * Function : Decoder_amr_init + * Purpose : Allocates initializes state memory + * Description : Stores pointer to filter status struct in *st. This + * pointer has to be passed to Decoder_amr in each call. + * Returns : 0 on success + */ + Word16 Decoder_amr_init(Decoder_amrState *st); + + /* + * Function : Decoder_amr_reset + * Purpose : Resets state memory + * Returns : 0 on success + */ + Word16 Decoder_amr_reset(Decoder_amrState *st, enum Mode mode); + + /* + * Function : Decoder_amr_exit + * Purpose : The memory used for state memory is freed + * Description : Stores NULL in *s + * Returns : void + */ + void Decoder_amr_exit(Decoder_amrState **st); + + /* + * Function : Decoder_amr + * Purpose : Speech decoder routine. + * Returns : 0 + */ + void Decoder_amr( + Decoder_amrState *st, /* i/o : State variables */ + enum Mode mode, /* i : AMR mode */ + Word16 parm[], /* i : vector of synthesis parameters + (PRM_SIZE) */ + enum RXFrameType frame_type, /* i : received frame type */ + Word16 synth[], /* o : synthesis speech (L_FRAME) */ + Word16 A_t[] /* o : decoded LP filter in 4 subframes + (AZ_SIZE) */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* DEC_AMR_H_ */ + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_gain.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_gain.cpp new file mode 100644 index 0000000..7fd54c1 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_gain.cpp @@ -0,0 +1,265 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dec_gain.cpp + Functions: dec_gain + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ + +#include "dec_gain.h" +#include "typedef.h" +#include "mode.h" +#include "cnst.h" +#include "pow2.h" +#include "log2.h" +#include "gc_pred.h" +#include "basic_op.h" +#include "qua_gain_tbl.h" +#include "qgain475_tab.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: dec_gain +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + pred_state = pointer to MA predictor state of type gc_predState + index = AMR mode of type enum Mode + code[] = pointer to innovative vector of type Word16 + evenSubfr = Flag for even subframes of type Word16 + pOverflow = pointer to overflow flag + + + Outputs: + pred_state = pointer to MA predictor state of type gc_predState + gain_pit = pointer to pitch gain of type Word16 + gain_cod = pointer to code gain of type Word16 + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + File : dec_gain.c + Purpose : Decode the pitch and codebook gains + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + agc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + + + + + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + + +void Dec_gain( + gc_predState *pred_state, /* i/o: MA predictor state */ + enum Mode mode, /* i : AMR mode */ + Word16 index, /* i : index of quantization. */ + Word16 code[], /* i : Innovative vector. */ + Word16 evenSubfr, /* i : Flag for even subframes */ + Word16 * gain_pit, /* o : Pitch gain. */ + Word16 * gain_cod, /* o : Code gain. */ + CommonAmrTbls* common_amr_tbls, /* i : ptr to struct of tbls ptrs */ + Flag * pOverflow +) +{ + const Word16 *p; + Word16 frac; + Word16 gcode0; + Word16 exp; + Word16 qua_ener; + Word16 qua_ener_MR122; + Word16 g_code; + Word32 L_tmp; + Word16 temp1; + Word16 temp2; + + /* Read the quantized gains (table depends on mode) */ + index = shl(index, 2, pOverflow); + + if (mode == MR102 || mode == MR74 || mode == MR67) + { + p = &(common_amr_tbls->table_gain_highrates_ptr[index]); + + *gain_pit = *p++; + g_code = *p++; + qua_ener_MR122 = *p++; + qua_ener = *p; + } + else + { + if (mode == MR475) + { + index += (1 ^ evenSubfr) << 1; /* evenSubfr is 0 or 1 */ + + if (index > (MR475_VQ_SIZE*4 - 2)) + { + index = (MR475_VQ_SIZE * 4 - 2); /* avoid possible buffer overflow */ + } + + p = &table_gain_MR475[index]; + + *gain_pit = *p++; + g_code = *p++; + + /*---------------------------------------------------------* + * calculate predictor update values (not stored in 4.75 * + * quantizer table to save space): * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * + * * + * qua_ener = log2(g) * + * qua_ener_MR122 = 20*log10(g) * + *---------------------------------------------------------*/ + + /* Log2(x Q12) = log2(x) + 12 */ + temp1 = g_code; + Log2(temp1, &exp, &frac, pOverflow); + exp -= 12; + + temp1 = shr_r(frac, 5, pOverflow); + temp2 = shl(exp, 10, pOverflow); + qua_ener_MR122 = add_16(temp1, temp2, pOverflow); + + /* 24660 Q12 ~= 6.0206 = 20*log10(2) */ + L_tmp = Mpy_32_16(exp, frac, 24660, pOverflow); + L_tmp = L_shl(L_tmp, 13, pOverflow); + qua_ener = pv_round(L_tmp, pOverflow); + /* Q12 * Q0 = Q13 -> Q10 */ + } + else + { + p = &(common_amr_tbls->table_gain_lowrates_ptr[index]); + + *gain_pit = *p++; + g_code = *p++; + qua_ener_MR122 = *p++; + qua_ener = *p; + } + } + + /*-------------------------------------------------------------------* + * predict codebook gain * + * ~~~~~~~~~~~~~~~~~~~~~ * + * gc0 = Pow2(int(d)+frac(d)) * + * = 2^exp + 2^frac * + * * + * gcode0 (Q14) = 2^14*2^frac = gc0 * 2^(14-exp) * + *-------------------------------------------------------------------*/ + + gc_pred(pred_state, mode, code, &exp, &frac, NULL, NULL, pOverflow); + + gcode0 = (Word16) Pow2(14, frac, pOverflow); + + /*------------------------------------------------------------------* + * read quantized gains, update table of past quantized energies * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * + * st->past_qua_en(Q10) = 20 * Log10(g_fac) / constant * + * = Log2(g_fac) * + * = qua_ener * + * constant = 20*Log10(2) * + *------------------------------------------------------------------*/ + + L_tmp = L_mult(g_code, gcode0, pOverflow); + temp1 = 10 - exp; + L_tmp = L_shr(L_tmp, temp1, pOverflow); + *gain_cod = (Word16)(L_tmp >> 16); + + /* update table of past quantized energies */ + + gc_pred_update(pred_state, qua_ener_MR122, qua_ener); + + return; +} + + + + + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_gain.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_gain.h new file mode 100644 index 0000000..ceb6bba --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_gain.h @@ -0,0 +1,120 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dec_gain.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : dec_gain.h + Purpose : Decode the pitch and codebook gains + +------------------------------------------------------------------------------ +*/ + +#ifndef _DEC_GAIN_H_ +#define _DEC_GAIN_H_ +#define dec_gain_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "gc_pred.h" +#include "mode.h" +#include "get_const_tbls.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; [Define module specific macros here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; [Include all pre-processor statements here.] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; [Declare variables used in this module but defined elsewhere] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; [List function prototypes here] + ----------------------------------------------------------------------------*/ + + /* + * FUNCTION: Dec_gain() + * PURPOSE: Decode the pitch and codebook gains + */ + void Dec_gain( + gc_predState *pred_state, /* i/o: MA predictor state */ + enum Mode mode, /* i : AMR mode */ + Word16 index, /* i : index of quantization. */ + Word16 code[], /* i : Innovative vector. */ + Word16 evenSubfr, /* i : Flag for even subframes */ + Word16 * gain_pit, /* o : Pitch gain. */ + Word16 * gain_cod, /* o : Code gain. */ + CommonAmrTbls* common_amr_tbls, /* i : ptr to struct of tbls ptrs */ + Flag * pOverflow + ); + + + +#ifdef __cplusplus +} +#endif + +#endif /* _DEC_GAIN_H_ */ + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_input_format_tab.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_input_format_tab.cpp new file mode 100644 index 0000000..867df28 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_input_format_tab.cpp @@ -0,0 +1,190 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + Filename: dec_input_format_tab.cpp + +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + None + + Outputs: + None + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This file contains the tables of the number of data bytes per codec mode in + both WMF and IF2 input formats. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + [1] AMR Speech Codec Frame Structure, 3GPP TS 26.101 version 4.1.0 Release 4, + June 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "amrdecode.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. Include conditional + ; compile variables also. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; LOCAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; LOCAL STORE/BUFFER/POINTER DEFINITIONS + ; Variable declaration - defined here and used outside this module + ----------------------------------------------------------------------------*/ + /* Table containing the number of core AMR data bytes for */ + /* each codec mode for WMF input format(number excludes frame type byte) */ + const Word16 WmfDecBytesPerFrame[16] = + { + 12, /* 4.75 */ + 13, /* 5.15 */ + 15, /* 5.90 */ + 17, /* 6.70 */ + 19, /* 7.40 */ + 20, /* 7.95 */ + 26, /* 10.2 */ + 31, /* 12.2 */ + 5, /* GsmAmr comfort noise */ + 6, /* Gsm-Efr comfort noise */ + 5, /* IS-641 comfort noise */ + 5, /* Pdc-Efr comfort noise */ + 0, /* future use */ + 0, /* future use */ + 0, /* future use */ + 0 /* No transmission */ + }; + + /* Table containing the number of core AMR data bytes for */ + /* each codec mode for IF2 input format. */ + const Word16 If2DecBytesPerFrame[16] = + { + 13, /* 4.75 */ + 14, /* 5.15 */ + 16, /* 5.90 */ + 18, /* 6.70 */ + 19, /* 7.40 */ + 21, /* 7.95 */ + 26, /* 10.2 */ + 31, /* 12.2 */ + 6, /* GsmAmr comfort noise */ + 6, /* Gsm-Efr comfort noise */ + 6, /* IS-641 comfort noise */ + 6, /* Pdc-Efr comfort noise */ + 0, /* future use */ + 0, /* future use */ + 0, /* future use */ + 1 /* No transmission */ + }; + + /*---------------------------------------------------------------------------- + ; EXTERNAL FUNCTION REFERENCES + ; Declare functions defined elsewhere and referenced in this module + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + + /*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; Define all local variables +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; Function body here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; Return nothing or data or data pointer +----------------------------------------------------------------------------*/ + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag3.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag3.cpp new file mode 100644 index 0000000..07d58ad --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag3.cpp @@ -0,0 +1,284 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dec_lag3.cpp + Functions: Dec_lag3 + + ------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + index -- Word16 -- received pitch index + t0_min -- Word16 -- minimum of search range + t0_max -- Word16 -- maximum of search range + i_subfr -- Word16 -- subframe flag + T0_prev -- Word16 -- integer pitch delay of last subframe + used in 2nd and 4th subframes + flag4 -- Word16 -- flag for encoding with 4 bits + + Outputs: + + T0 -- Pointer to type Word16 -- integer part of pitch lag + T0_frac -- Pointer to type Word16 -- fractional part of pitch lag + pOverflow -- Pointer to type Flag -- Flag set when overflow occurs + + Returns: + None. + + Global Variables Used: + None + + Local Variables Needed: + None + + + ) +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + PURPOSE: Decoding of fractional pitch lag with 1/3 resolution. + Extract the integer and fraction parts of the pitch lag from + the received adaptive codebook index. + + See "Enc_lag3.c" for more details about the encoding procedure. + + The fractional lag in 1st and 3rd subframes is encoded with 8 bits + while that in 2nd and 4th subframes is relatively encoded with 4, 5 + and 6 bits depending on the mode. + +------------------------------------------------------------------------------ + REQUIREMENTS + + + +------------------------------------------------------------------------------ + REFERENCES + + dec_lag3.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "dec_lag3.h" +#include "typedef.h" +#include "basic_op.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL STORE/BUFFER/POINTER DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL FUNCTION REFERENCES +; Declare functions defined elsewhere and referenced in this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES +; Declare variables used in this module but defined elsewhere +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ + +void Dec_lag3(Word16 index, /* i : received pitch index */ + Word16 t0_min, /* i : minimum of search range */ + Word16 t0_max, /* i : maximum of search range */ + Word16 i_subfr, /* i : subframe flag */ + Word16 T0_prev, /* i : integer pitch delay of last subframe + used in 2nd and 4th subframes */ + Word16 * T0, /* o : integer part of pitch lag */ + Word16 * T0_frac, /* o : fractional part of pitch lag */ + Word16 flag4, /* i : flag for encoding with 4 bits */ + Flag *pOverflow /* o : Flag set when overflow occurs */ + ) +{ + Word16 i; + Word16 tmp_lag; + + if (i_subfr == 0) /* if 1st or 3rd subframe */ + { + + if (index < 197) + { + + tmp_lag = index + 2; + + tmp_lag = + mult( + tmp_lag, + 10923, + pOverflow); + + i = tmp_lag + 19; + + *T0 = i; + + /* i = 3 * (*T0) */ + + i <<= 1; + i += *T0; + + tmp_lag = index - i; + + *T0_frac = tmp_lag + 58; + } + else + { + *T0 = index - 112; + + *T0_frac = 0; + } + + } + else + { /* 2nd or 4th subframe */ + + if (flag4 == 0) + { + + /* 'normal' decoding: either with 5 or 6 bit resolution */ + + i = index + 2; + + i = ((Word32) i * 10923) >> 15; + + + i -= 1; + + *T0 = i + t0_min; + + /* i = 3* (*T0) */ + i = i + (i << 1); + + tmp_lag = index - 2; + + *T0_frac = tmp_lag - i; + } + else + { + + /* decoding with 4 bit resolution */ + + tmp_lag = T0_prev; + + i = + sub( + tmp_lag, + t0_min, + pOverflow); + + if (i > 5) + { + tmp_lag = t0_min + 5; + } + + i = t0_max - tmp_lag; + + if (i > 4) + { + tmp_lag = t0_max - 4; + } + + if (index < 4) + { + i = tmp_lag - 5; + + *T0 = i + index; + + *T0_frac = 0; + } + else + { + /* 4 >= index < 12 */ + if (index < 12) + { + i = index - 5; + i = ((Word32) i * 10923) >> 15; + + + i--; + + *T0 = i + tmp_lag; + + i = i + (i << 1); + + tmp_lag = index - 9; + + *T0_frac = tmp_lag - i; + } + else + { + i = index - 12; + + i = i + tmp_lag; + + *T0 = i + 1; + + *T0_frac = 0; + } + } + + } /* end if (decoding with 4 bit resolution) */ + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag3.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag3.h new file mode 100644 index 0000000..c90e459 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag3.h @@ -0,0 +1,115 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dec_lag3.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the dec_lag3.c + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef dec_lag3_h +#define dec_lag3_h "$Id $" + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + void Dec_lag3(Word16 index, /* i : received pitch index */ + Word16 T0_min, /* i : minimum of search range */ + Word16 T0_max, /* i : maximum of search range */ + Word16 i_subfr, /* i : subframe flag */ + Word16 T0_prev, /* i : integer pitch delay of last subframe + used in 2nd and 4th subframes */ + Word16 * T0, /* o : integer part of pitch lag */ + Word16 * T0_frac, /* o : fractional part of pitch lag */ + Word16 flag4, /* i : flag for encoding with 4 bits */ + Flag * pOverflow /* o : Flag set when overflow occurs */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _DEC_LAG_3_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag6.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag6.cpp new file mode 100644 index 0000000..8d580ad --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag6.cpp @@ -0,0 +1,229 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dec_lag6.cpp + Functions: Dec_lag6 + + ------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + index -- Word16 -- received pitch index + pit_min -- Word16 -- minimum pitch lag + pit_max -- Word16 -- maximum pitch lag + i_subfr -- Word16 -- subframe flag + T0 -- Pointer to type Word16 -- integer part of pitch lag + + Outputs: + + T0 -- Pointer to type Word16 -- integer part of pitch lag + T0_frac -- Pointer to type Word16 -- fractional part of pitch lag + pOverflow -- Pointer to type Flag -- Flag set when overflow occurs + + Returns: + None. + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + PURPOSE: Decoding of fractional pitch lag with 1/6 resolution. + Extract the integer and fraction parts of the pitch lag from + the received adaptive codebook index. + + See "Enc_lag6.c" for more details about the encoding procedure. + + The fractional lag in 1st and 3rd subframes is encoded with 9 bits + while that in 2nd and 4th subframes is relatively encoded with 6 bits. + Note that in relative encoding only 61 values are used. If the + decoder receives 61, 62, or 63 as the relative pitch index, it means + that a transmission error occurred. In this case, the pitch lag from + previous subframe (actually from previous frame) is used. + +------------------------------------------------------------------------------ + REQUIREMENTS + + + +------------------------------------------------------------------------------ + REFERENCES + + dec_lag6.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "dec_lag6.h" +#include "typedef.h" +#include "basic_op.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL STORE/BUFFER/POINTER DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL FUNCTION REFERENCES +; Declare functions defined elsewhere and referenced in this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES +; Declare variables used in this module but defined elsewhere +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ + +void Dec_lag6( + Word16 index, /* input : received pitch index */ + Word16 pit_min, /* input : minimum pitch lag */ + Word16 pit_max, /* input : maximum pitch lag */ + Word16 i_subfr, /* input : subframe flag */ + Word16 *T0, /* in/out: integer part of pitch lag */ + Word16 *T0_frac, /* output: fractional part of pitch lag */ + Flag *pOverflow /* o : Flag set when overflow occurs */ +) +{ + Word16 i; + Word16 T0_min; + Word16 T0_max; + Word16 k; + + if (i_subfr == 0) /* if 1st or 3rd subframe */ + { + if (index < 463) + { + /* T0 = (index+5)/6 + 17 */ + i = index + 5; + i = ((Word32) i * 5462) >> 15; + + + i += 17; + + *T0 = i; + + /* i = 3* (*T0) */ + + i <<= 1; + i += *T0; + + /* *T0_frac = index - T0*6 + 105 */ + + i <<= 1; + + i = index - i; + + *T0_frac = i + 105; + } + else + { + *T0 = index - 368; + + *T0_frac = 0; + } + } + else /* second or fourth subframe */ + { + /* find T0_min and T0_max for 2nd (or 4th) subframe */ + + T0_min = *T0 - 5; + + if (T0_min < pit_min) + { + T0_min = pit_min; + } + + T0_max = T0_min + 9; + + if (T0_max > pit_max) + { + T0_max = pit_max; + + T0_min = T0_max - 9; + } + + /* i = (index+5)/6 - 1 */ + i = index + 5; + + i = ((Word32) i * 5462) >> 15; + + + i -= 1; + + *T0 = i + T0_min; + + /* i = 3* (*T0) */ + + i = i + (i << 1); + + i <<= 1; + + k = index - 3; + + *T0_frac = k - i; + } +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag6.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag6.h new file mode 100644 index 0000000..5b0073d --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dec_lag6.h @@ -0,0 +1,113 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dec_lag6.h + + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the dec_lag6.c + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef dec_lag6_h +#define dec_lag6_h "$Id $" + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + void Dec_lag6( + Word16 index, /* input : received pitch index */ + Word16 pit_min, /* input : minimum pitch lag */ + Word16 pit_max, /* input : maximum pitch lag */ + Word16 i_subfr, /* input : subframe flag */ + Word16 *T0, /* in/out: integer part of pitch lag */ + Word16 *T0_frac, /* output: fractional part of pitch lag */ + Flag * pOverflow /* o : Flag set when overflow occurs */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _DEC_LAG_6_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/decoder_gsm_amr.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/decoder_gsm_amr.cpp new file mode 100644 index 0000000..90a9832 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/decoder_gsm_amr.cpp @@ -0,0 +1,262 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +#include "decoder_gsm_amr.h" +#include "sp_dec.h" +#include "amrdecode.h" +#include "pvamrnbdecoder_api.h" + +// Use default DLL entry point +#include "oscl_dll.h" +#include "oscl_error_codes.h" +#include "oscl_exception.h" +#include "oscl_mem.h" + +#define KCAI_CODEC_INIT_FAILURE -1 + +OSCL_DLL_ENTRY_POINT_DEFAULT() + +OSCL_EXPORT_REF CDecoder_AMR_NB *CDecoder_AMR_NB::NewL() +{ + CDecoder_AMR_NB *dec = new CDecoder_AMR_NB; + if (dec == NULL) + OSCL_LEAVE(OsclErrNoMemory); + else + dec->ConstructL(); + return dec; +} + +OSCL_EXPORT_REF void CDecoder_AMR_NB::ConstructL() +{ + iDecState = NULL; +} + + +/* +----------------------------------------------------------------------------- + + CDecoder_AMR_NB + + ~CDecoder_AMR_NB + + Empty decoder destructor. + + Parameters: none + + Return Values: none + +----------------------------------------------------------------------------- +*/ +OSCL_EXPORT_REF CDecoder_AMR_NB::~CDecoder_AMR_NB() +{ + if (iDecState) + oscl_free(iDecState); + iDecState = NULL; + + if (iInputBuf) + { + OSCL_ARRAY_DELETE(iInputBuf); + iInputBuf = NULL; + } + + if (iOutputBuf) + { + OSCL_ARRAY_DELETE(iOutputBuf); + iOutputBuf = NULL; + } +} + + +/* +----------------------------------------------------------------------------- + + CDecoder_AMR_NB + + StartL + + Start decoder object. Initialize codec status. + + Parameters: none + + Return Values: status + +----------------------------------------------------------------------------- +*/ +OSCL_EXPORT_REF int32 CDecoder_AMR_NB::StartL(tPVAmrDecoderExternal * pExt, + bool aAllocateInputBuffer, + bool aAllocateOutputBuffer) +{ + + if (aAllocateInputBuffer) + { + iInputBuf = OSCL_ARRAY_NEW(int16, MAX_NUM_PACKED_INPUT_BYTES); + if (iInputBuf == NULL) + { + return KCAI_CODEC_INIT_FAILURE; + } + } + else + { + iInputBuf = NULL; + } + pExt->pInputBuffer = (uint8 *)iInputBuf; + + if (aAllocateOutputBuffer) + { + iOutputBuf = OSCL_ARRAY_NEW(int16, L_FRAME); + + if (iOutputBuf == NULL) + { + return KCAI_CODEC_INIT_FAILURE; + } + } + else + { + iOutputBuf = NULL; + } + pExt->pOutputBuffer = iOutputBuf; + + pExt->samplingRate = 8000; + pExt->desiredChannels = 1; + + pExt->reset_flag = 0; + pExt->reset_flag_old = 1; + pExt->mode_old = 0; + + return GSMInitDecode(&iDecState, (int8*)"Decoder"); +} + + +/* +----------------------------------------------------------------------------- + + CDecoder_AMR_NB + + ExecuteL + + Execute decoder object. Read one encoded speech frame from the input + stream, decode it and write the decoded frame to output stream. + + Parameters: + + Return Values: status + + +----------------------------------------------------------------------------- +*/ + +OSCL_EXPORT_REF int32 CDecoder_AMR_NB::ExecuteL(tPVAmrDecoderExternal * pExt) +{ + + + if (pExt->input_format == WMF) + pExt->input_format = MIME_IETF; + + return AMRDecode(iDecState, + (enum Frame_Type_3GPP)pExt->mode, + (uint8*) pExt->pInputBuffer, + (int16*) pExt->pOutputBuffer, + pExt->input_format); + +} + +/* +----------------------------------------------------------------------------- + + CDecoder_AMR_NB + + StopL + + Stop decoder object. Flush out last frames, if necessary. + + Parameters: none + + Return Values: none + +----------------------------------------------------------------------------- +*/ +OSCL_EXPORT_REF void CDecoder_AMR_NB::StopL() +{ +} + +/* +----------------------------------------------------------------------------- + + CDecoder_AMR_NB + + ResetDecoderL + + Stop decoder object. Reset decoder. + + Parameters: none + + Return Values: status + +----------------------------------------------------------------------------- +*/ +OSCL_EXPORT_REF int32 CDecoder_AMR_NB::ResetDecoderL() +{ + return Speech_Decode_Frame_reset(iDecState); +} + + +/* +----------------------------------------------------------------------------- + + CDecoder_AMR_NB + + TerminateDecoderL + + Stop decoder object. close decoder. + + Parameters: none + + Return Values: none + +----------------------------------------------------------------------------- +*/ +OSCL_EXPORT_REF void CDecoder_AMR_NB::TerminateDecoderL() +{ + GSMDecodeFrameExit(&iDecState); + iDecState = NULL; + + if (iInputBuf) + { + OSCL_ARRAY_DELETE(iInputBuf); + iInputBuf = NULL; + } + + if (iOutputBuf) + { + OSCL_ARRAY_DELETE(iOutputBuf); + iOutputBuf = NULL; + } +} + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dtx_dec.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dtx_dec.cpp new file mode 100644 index 0000000..665a6ec --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dtx_dec.cpp @@ -0,0 +1,1895 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dtx_dec.cpp + Functions: + dtx_dec_reset + dtx_dec + dtx_dec_activity_update + rx_dtx_handler + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + These modules decode the comfort noise when in DTX. + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "dtx_dec.h" +#include "typedef.h" +#include "basic_op.h" +#include "set_zero.h" +#include "mode.h" +#include "log2.h" +#include "lsp_az.h" +#include "pow2.h" +#include "a_refl.h" +#include "b_cn_cod.h" +#include "syn_filt.h" +#include "lsp_lsf.h" +#include "reorder.h" +#include "oscl_mem.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ +#define PN_INITIAL_SEED 0x70816958L /* Pseudo noise generator seed value */ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/*************************************************** + * Scaling factors for the lsp variability operation * + ***************************************************/ +static const Word16 lsf_hist_mean_scale[M] = +{ + 20000, + 20000, + 20000, + 20000, + 20000, + 18000, + 16384, + 8192, + 0, + 0 +}; + +/************************************************* + * level adjustment for different modes Q11 * + *************************************************/ +static const Word16 dtx_log_en_adjust[9] = +{ + -1023, /* MR475 */ + -878, /* MR515 */ + -732, /* MR59 */ + -586, /* MR67 */ + -440, /* MR74 */ + -294, /* MR795 */ + -148, /* MR102 */ + 0, /* MR122 */ + 0, /* MRDTX */ +}; + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: dtx_dec_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type dtx_decState + + Outputs: + Structure pointed to by st is initialized to a set of initial values. + + Returns: + return_value = 0 if memory was successfully initialized, + otherwise returns -1 (int) + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Reset of state memory for dtx_dec. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int dtx_dec_reset (dtx_decState *st) +{ + int i; + + if (st == (dtx_decState *) NULL){ + fprintf(stderr, "dtx_dec_reset: invalid parameter\n"); + return -1; + } + + st->since_last_sid = 0; + st->true_sid_period_inv = (1 << 13); + + st->log_en = 3500; + st->old_log_en = 3500; + // low level noise for better performance in DTX handover cases + + st->L_pn_seed_rx = PN_INITIAL_SEED; + + // Initialize state->lsp [] and state->lsp_old [] + Copy(lsp_init_data, &st->lsp[0], M); + Copy(lsp_init_data, &st->lsp_old[0], M); + + st->lsf_hist_ptr = 0; + st->log_pg_mean = 0; + st->log_en_hist_ptr = 0; + + // initialize decoder lsf history + Copy(mean_lsf, &st->lsf_hist[0], M); + + for (i = 1; i < DTX_HIST_SIZE; i++) + { + Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M); + } + Set_zero(st->lsf_hist_mean, M*DTX_HIST_SIZE); + + // initialize decoder log frame energy + for (i = 0; i < DTX_HIST_SIZE; i++) + { + st->log_en_hist[i] = st->log_en; + } + + st->log_en_adjust = 0; + + st->dtxHangoverCount = DTX_HANG_CONST; + st->decAnaElapsedCount = 32767; + + st->sid_frame = 0; + st->valid_data = 0; + st->dtxHangoverAdded = 0; + + st->dtxGlobalState = DTX; + st->data_updated = 0; + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 dtx_dec_reset(dtx_decState *st) +{ + Word16 i; + + if (st == (dtx_decState *) NULL) + { + /* fprint(stderr, "dtx_dec_reset: invalid parameter\n"); */ + return(-1); + } + + st->since_last_sid = 0; + st->true_sid_period_inv = (1 << 13); + + st->log_en = 3500; + st->old_log_en = 3500; + /* low level noise for better performance in DTX handover cases*/ + + st->L_pn_seed_rx = PN_INITIAL_SEED; + + /* Initialize state->lsp [] */ + st->lsp[0] = 30000; + st->lsp[1] = 26000; + st->lsp[2] = 21000; + st->lsp[3] = 15000; + st->lsp[4] = 8000; + st->lsp[5] = 0; + st->lsp[6] = -8000; + st->lsp[7] = -15000; + st->lsp[8] = -21000; + st->lsp[9] = -26000; + + /* Initialize state->lsp_old [] */ + st->lsp_old[0] = 30000; + st->lsp_old[1] = 26000; + st->lsp_old[2] = 21000; + st->lsp_old[3] = 15000; + st->lsp_old[4] = 8000; + st->lsp_old[5] = 0; + st->lsp_old[6] = -8000; + st->lsp_old[7] = -15000; + st->lsp_old[8] = -21000; + st->lsp_old[9] = -26000; + + st->lsf_hist_ptr = 0; + st->log_pg_mean = 0; + st->log_en_hist_ptr = 0; + + /* initialize decoder lsf history */ + st->lsf_hist[0] = 1384; + st->lsf_hist[1] = 2077; + st->lsf_hist[2] = 3420; + st->lsf_hist[3] = 5108; + st->lsf_hist[4] = 6742; + st->lsf_hist[5] = 8122; + st->lsf_hist[6] = 9863; + st->lsf_hist[7] = 11092; + st->lsf_hist[8] = 12714; + st->lsf_hist[9] = 13701; + + for (i = 1; i < DTX_HIST_SIZE; i++) + { + oscl_memmove((void *)&st->lsf_hist[M*i], &st->lsf_hist[0], M*sizeof(st->lsf_hist[0])); + } + oscl_memset(st->lsf_hist_mean, 0, sizeof(Word16)*M*DTX_HIST_SIZE); + + /* initialize decoder log frame energy */ + for (i = 0; i < DTX_HIST_SIZE; i++) + { + st->log_en_hist[i] = st->log_en; + } + + st->log_en_adjust = 0; + + st->dtxHangoverCount = DTX_HANG_CONST; + st->decAnaElapsedCount = 32767; + + st->sid_frame = 0; + st->valid_data = 0; + st->dtxHangoverAdded = 0; + + st->dtxGlobalState = DTX; + st->data_updated = 0; + + return(0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: dtx_dec +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type dtx_decState + mem_syn = AMR decoder state + lsfState = decoder lsf states + predState = prediction states + averState = CB gain average states + new_state = new DTX state + mode = AMR mode + parm = Vector of synthesis parameters + + Outputs: + st points to an updated structure of type dtx_decState + mem_syn = AMR decoder state + lsfState = decoder lsf states + predState = prediction states + averState = CB gain average states + synth = synthesised speech + A_t = decoded LP filter in 4 subframes + + Returns: + return_value = 0 (int) + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Decode comfort noise when in DTX. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int dtx_dec( + dtx_decState *st, // i/o : State struct + Word16 mem_syn[], // i/o : AMR decoder state + D_plsfState* lsfState, // i/o : decoder lsf states + gc_predState* predState, // i/o : prediction states + Cb_gain_averageState* averState, // i/o : CB gain average states + enum DTXStateType new_state, // i : new DTX state + enum Mode mode, // i : AMR mode + Word16 parm[], // i : Vector of synthesis parameters + Word16 synth[], // o : synthesised speech + Word16 A_t[] // o : decoded LP filter in 4 subframes + ) +{ + Word16 log_en_index; + Word16 i, j; + Word16 int_fac; + Word32 L_log_en_int; + Word16 lsp_int[M]; + Word16 log_en_int_e; + Word16 log_en_int_m; + Word16 level; + Word16 acoeff[M + 1]; + Word16 refl[M]; + Word16 pred_err; + Word16 ex[L_SUBFR]; + Word16 ma_pred_init; + Word16 log_pg_e, log_pg_m; + Word16 log_pg; + Flag negative; + Word16 lsf_mean; + Word32 L_lsf_mean; + Word16 lsf_variab_index; + Word16 lsf_variab_factor; + Word16 lsf_int[M]; + Word16 lsf_int_variab[M]; + Word16 lsp_int_variab[M]; + Word16 acoeff_variab[M + 1]; + + Word16 lsf[M]; + Word32 L_lsf[M]; + Word16 ptr; + Word16 tmp_int_length; + + + // This function is called if synthesis state is not SPEECH + // the globally passed inputs to this function are + // st->sid_frame + // st->valid_data + // st->dtxHangoverAdded + // new_state (SPEECH, DTX, DTX_MUTE) + + if ((st->dtxHangoverAdded != 0) && + (st->sid_frame != 0)) + { + // sid_first after dtx hangover period + // or sid_upd after dtxhangover + + // set log_en_adjust to correct value + st->log_en_adjust = dtx_log_en_adjust[mode]; + + ptr = add(st->lsf_hist_ptr, M); + if (sub(ptr, 80) == 0) + { + ptr = 0; + } + Copy( &st->lsf_hist[st->lsf_hist_ptr],&st->lsf_hist[ptr],M); + + ptr = add(st->log_en_hist_ptr,1); + if (sub(ptr, DTX_HIST_SIZE) == 0) + { + ptr = 0; + } + st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; // Q11 + + // compute mean log energy and lsp + // from decoded signal (SID_FIRST) + st->log_en = 0; + for (i = 0; i < M; i++) + { + L_lsf[i] = 0; + } + + // average energy and lsp + for (i = 0; i < DTX_HIST_SIZE; i++) + { + st->log_en = add(st->log_en, + shr(st->log_en_hist[i],3)); + for (j = 0; j < M; j++) + { + L_lsf[j] = L_add(L_lsf[j], + L_deposit_l(st->lsf_hist[i * M + j])); + } + } + + for (j = 0; j < M; j++) + { + lsf[j] = extract_l(L_shr(L_lsf[j],3)); // divide by 8 + } + + Lsf_lsp(lsf, st->lsp, M); + + // make log_en speech coder mode independent + // added again later before synthesis + st->log_en = sub(st->log_en, st->log_en_adjust); + + // compute lsf variability vector + Copy(st->lsf_hist, st->lsf_hist_mean, 80); + + for (i = 0; i < M; i++) + { + L_lsf_mean = 0; + // compute mean lsf + for (j = 0; j < 8; j++) + { + L_lsf_mean = L_add(L_lsf_mean, + L_deposit_l(st->lsf_hist_mean[i+j*M])); + } + + lsf_mean = extract_l(L_shr(L_lsf_mean, 3)); + // subtract mean and limit to within reasonable limits + // moreover the upper lsf's are attenuated + for (j = 0; j < 8; j++) + { + // subtract mean + st->lsf_hist_mean[i+j*M] = + sub(st->lsf_hist_mean[i+j*M], lsf_mean); + + // attenuate deviation from mean, especially for upper lsf's + st->lsf_hist_mean[i+j*M] = + mult(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i]); + + // limit the deviation + if (st->lsf_hist_mean[i+j*M] < 0) + { + negative = 1; + } + else + { + negative = 0; + } + st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]); + + // apply soft limit + if (sub(st->lsf_hist_mean[i+j*M], 655) > 0) + { + st->lsf_hist_mean[i+j*M] = + add(655, shr(sub(st->lsf_hist_mean[i+j*M], 655), 2)); + } + + // apply hard limit + if (sub(st->lsf_hist_mean[i+j*M], 1310) > 0) + { + st->lsf_hist_mean[i+j*M] = 1310; + } + if (negative != 0) + { + st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M]; + } + + } + } + } + + if (st->sid_frame != 0 ) + { + // Set old SID parameters, always shift + // even if there is no new valid_data + Copy(st->lsp, st->lsp_old, M); + st->old_log_en = st->log_en; + + if (st->valid_data != 0 ) // new data available (no CRC) + { + // Compute interpolation factor, since the division only works + // for values of since_last_sid < 32 we have to limit the + // interpolation to 32 frames + tmp_int_length = st->since_last_sid; + st->since_last_sid = 0; + + if (sub(tmp_int_length, 32) > 0) + { + tmp_int_length = 32; + } + if (sub(tmp_int_length, 2) >= 0) + { + st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10)); + } + else + { + st->true_sid_period_inv = 1 << 14; // 0.5 it Q15 + } + + Init_D_plsf_3(lsfState, parm[0]); // temporay initialization + D_plsf_3(lsfState, MRDTX, 0, &parm[1], st->lsp); + Set_zero(lsfState->past_r_q, M); // reset for next speech frame + + log_en_index = parm[4]; + // Q11 and divide by 4 + st->log_en = shl(log_en_index, (11 - 2)); + + // Subtract 2.5 in Q11 + st->log_en = sub(st->log_en, (2560 * 2)); + + // Index 0 is reserved for silence + if (log_en_index == 0) + { + st->log_en = MIN_16; + } + + // no interpolation at startup after coder reset + // or when SID_UPD has been received right after SPEECH + if ((st->data_updated == 0) || + (sub(st->dtxGlobalState, SPEECH) == 0) + ) + { + Copy(st->lsp, st->lsp_old, M); + st->old_log_en = st->log_en; + } + } // endif valid_data + + // initialize gain predictor memory of other modes + ma_pred_init = sub(shr(st->log_en,1), 9000); + if (ma_pred_init > 0) + { + ma_pred_init = 0; + } + if (sub(ma_pred_init, -14436) < 0) + { + ma_pred_init = -14436; + } + + predState->past_qua_en[0] = ma_pred_init; + predState->past_qua_en[1] = ma_pred_init; + predState->past_qua_en[2] = ma_pred_init; + predState->past_qua_en[3] = ma_pred_init; + + // past_qua_en for other modes than MR122 + ma_pred_init = mult(5443, ma_pred_init); + // scale down by factor 20*log10(2) in Q15 + predState->past_qua_en_MR122[0] = ma_pred_init; + predState->past_qua_en_MR122[1] = ma_pred_init; + predState->past_qua_en_MR122[2] = ma_pred_init; + predState->past_qua_en_MR122[3] = ma_pred_init; + } // endif sid_frame + + // CN generation + // recompute level adjustment factor Q11 + // st->log_en_adjust = 0.9*st->log_en_adjust + + // 0.1*dtx_log_en_adjust[mode]); + st->log_en_adjust = add(mult(st->log_en_adjust, 29491), + shr(mult(shl(dtx_log_en_adjust[mode],5),3277),5)); + + // Interpolate SID info + int_fac = shl(add(1,st->since_last_sid), 10); // Q10 + int_fac = mult(int_fac, st->true_sid_period_inv); // Q10 * Q15 -> Q10 + + // Maximize to 1.0 in Q10 + if (sub(int_fac, 1024) > 0) + { + int_fac = 1024; + } + int_fac = shl(int_fac, 4); // Q10 -> Q14 + + L_log_en_int = L_mult(int_fac, st->log_en); // Q14 * Q11->Q26 + for(i = 0; i < M; i++) + { + lsp_int[i] = mult(int_fac, st->lsp[i]);// Q14 * Q15 -> Q14 + } + + int_fac = sub(16384, int_fac); // 1-k in Q14 + + // (Q14 * Q11 -> Q26) + Q26 -> Q26 + L_log_en_int = L_mac(L_log_en_int, int_fac, st->old_log_en); + for(i = 0; i < M; i++) + { + // Q14 + (Q14 * Q15 -> Q14) -> Q14 + lsp_int[i] = add(lsp_int[i], mult(int_fac, st->lsp_old[i])); + lsp_int[i] = shl(lsp_int[i], 1); // Q14 -> Q15 + } + + // compute the amount of lsf variability + lsf_variab_factor = sub(st->log_pg_mean,2457); // -0.6 in Q12 + // *0.3 Q12*Q15 -> Q12 + lsf_variab_factor = sub(4096, mult(lsf_variab_factor, 9830)); + + // limit to values between 0..1 in Q12 + if (sub(lsf_variab_factor, 4096) > 0) + { + lsf_variab_factor = 4096; + } + if (lsf_variab_factor < 0) + { + lsf_variab_factor = 0; + } + lsf_variab_factor = shl(lsf_variab_factor, 3); // -> Q15 + + // get index of vector to do variability with + lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3); + + // convert to lsf + Lsp_lsf(lsp_int, lsf_int, M); + + // apply lsf variability + Copy(lsf_int, lsf_int_variab, M); + for(i = 0; i < M; i++) + { + lsf_int_variab[i] = add(lsf_int_variab[i], + mult(lsf_variab_factor, + st->lsf_hist_mean[i+lsf_variab_index*M])); + } + + // make sure that LSP's are ordered + Reorder_lsf(lsf_int, LSF_GAP, M); + Reorder_lsf(lsf_int_variab, LSF_GAP, M); + + // copy lsf to speech decoders lsf state + Copy(lsf_int, lsfState->past_lsf_q, M); + + // convert to lsp + Lsf_lsp(lsf_int, lsp_int, M); + Lsf_lsp(lsf_int_variab, lsp_int_variab, M); + + // Compute acoeffs Q12 acoeff is used for level + // normalization and postfilter, acoeff_variab is + // used for synthesis filter + // by doing this we make sure that the level + // in high frequenncies does not jump up and down + + Lsp_Az(lsp_int, acoeff); + Lsp_Az(lsp_int_variab, acoeff_variab); + + // For use in postfilter + Copy(acoeff, &A_t[0], M + 1); + Copy(acoeff, &A_t[M + 1], M + 1); + Copy(acoeff, &A_t[2 * (M + 1)], M + 1); + Copy(acoeff, &A_t[3 * (M + 1)], M + 1); + + // Compute reflection coefficients Q15 + A_Refl(&acoeff[1], refl); + + // Compute prediction error in Q15 + pred_err = MAX_16; // 0.99997 in Q15 + for (i = 0; i < M; i++) + { + pred_err = mult(pred_err, sub(MAX_16, mult(refl[i], refl[i]))); + } + + // compute logarithm of prediction gain + Log2(L_deposit_l(pred_err), &log_pg_e, &log_pg_m); + + // convert exponent and mantissa to Word16 Q12 + log_pg = shl(sub(log_pg_e,15), 12); // Q12 + log_pg = shr(sub(0,add(log_pg, shr(log_pg_m, 15-12))), 1); + st->log_pg_mean = add(mult(29491,st->log_pg_mean), + mult(3277, log_pg)); + + // Compute interpolated log energy + L_log_en_int = L_shr(L_log_en_int, 10); // Q26 -> Q16 + + // Add 4 in Q16 + L_log_en_int = L_add(L_log_en_int, 4 * 65536L); + + // subtract prediction gain + L_log_en_int = L_sub(L_log_en_int, L_shl(L_deposit_l(log_pg), 4)); + + // adjust level to speech coder mode + L_log_en_int = L_add(L_log_en_int, + L_shl(L_deposit_l(st->log_en_adjust), 5)); + + log_en_int_e = extract_h(L_log_en_int); + log_en_int_m = extract_l(L_shr(L_sub(L_log_en_int, + L_deposit_h(log_en_int_e)), 1)); + level = extract_l(Pow2(log_en_int_e, log_en_int_m)); // Q4 + + for (i = 0; i < 4; i++) + { + // Compute innovation vector + build_CN_code(&st->L_pn_seed_rx, ex); + for (j = 0; j < L_SUBFR; j++) + { + ex[j] = mult(level, ex[j]); + } + // Synthesize + Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR, + mem_syn, 1); + + } // next i + + // reset codebook averaging variables + averState->hangVar = 20; + averState->hangCount = 0; + + if (sub(new_state, DTX_MUTE) == 0) + { + // mute comfort noise as it has been quite a long time since + * last SID update was performed + + tmp_int_length = st->since_last_sid; + if (sub(tmp_int_length, 32) > 0) + { + tmp_int_length = 32; + } + + // safety guard against division by zero + if(tmp_int_length <= 0) { + tmp_int_length = 8; + } + + st->true_sid_period_inv = div_s(1 << 10, shl(tmp_int_length, 10)); + + st->since_last_sid = 0; + Copy(st->lsp, st->lsp_old, M); + st->old_log_en = st->log_en; + // subtract 1/8 in Q11 i.e -6/8 dB + st->log_en = sub(st->log_en, 256); + } + + // reset interpolation length timer + // if data has been updated. + if ((st->sid_frame != 0) && + ((st->valid_data != 0) || + ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0))) + { + st->since_last_sid = 0; + st->data_updated = 1; + } + + return 0; +} + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void dtx_dec( + dtx_decState *st, /* i/o : State struct */ + Word16 mem_syn[], /* i/o : AMR decoder state */ + D_plsfState* lsfState, /* i/o : decoder lsf states */ + gc_predState* predState, /* i/o : prediction states */ + Cb_gain_averageState* averState, /* i/o : CB gain average states */ + enum DTXStateType new_state, /* i : new DTX state */ + enum Mode mode, /* i : AMR mode */ + Word16 parm[], /* i : Vector of synthesis parameters */ + CommonAmrTbls* common_amr_tbls, /* i : Ptr to struct of table ptrs */ + Word16 synth[], /* o : synthesised speech */ + Word16 A_t[], /* o : decoded LP filter in 4 subframes*/ + Flag *pOverflow +) +{ + Word16 log_en_index; + Word16 i; + Word16 j; + Word16 int_fac; + Word32 L_log_en_int; + Word16 lsp_int[M]; + Word16 log_en_int_e; + Word16 log_en_int_m; + Word16 level; + Word16 acoeff[M + 1]; + Word16 refl[M]; + Word16 pred_err; + Word16 ex[L_SUBFR]; + Word16 ma_pred_init; + Word16 log_pg_e; + Word16 log_pg_m; + Word16 log_pg; + Flag negative; + Word16 lsf_mean; + Word32 L_lsf_mean; + Word16 lsf_variab_index; + Word16 lsf_variab_factor; + Word16 lsf_int[M]; + Word16 lsf_int_variab[M]; + Word16 lsp_int_variab[M]; + Word16 acoeff_variab[M + 1]; + + Word16 lsf[M]; + Word32 L_lsf[M]; + Word16 ptr; + Word16 tmp_int_length; + + Word32 L_temp; + Word16 temp; + + /* This function is called if synthesis state is not SPEECH + * the globally passed inputs to this function are + * st->sid_frame + * st->valid_data + * st->dtxHangoverAdded + * new_state (SPEECH, DTX, DTX_MUTE) + */ + + if ((st->dtxHangoverAdded != 0) && + (st->sid_frame != 0)) + { + /* sid_first after dtx hangover period */ + /* or sid_upd after dtxhangover */ + + /* set log_en_adjust to correct value */ + st->log_en_adjust = dtx_log_en_adjust[mode]; + + ptr = st->lsf_hist_ptr + M; + + if (ptr == 80) + { + ptr = 0; + } + oscl_memmove((void *)&st->lsf_hist[ptr], &st->lsf_hist[st->lsf_hist_ptr], M*sizeof(*st->lsf_hist)); + + ptr = st->log_en_hist_ptr + 1; + + if (ptr == DTX_HIST_SIZE) + { + ptr = 0; + } + + st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; /* Q11 */ + + /* compute mean log energy and lsp * + * from decoded signal (SID_FIRST) */ + st->log_en = 0; + for (i = M - 1; i >= 0; i--) + { + L_lsf[i] = 0; + } + + /* average energy and lsp */ + for (i = DTX_HIST_SIZE - 1; i >= 0; i--) + { + if (st->log_en_hist[i] < 0) + { + temp = ~((~st->log_en_hist[i]) >> 3); + } + else + { + temp = st->log_en_hist[i] >> 3; + } + st->log_en = add_16(st->log_en, temp, pOverflow); + for (j = M - 1; j >= 0; j--) + { + L_lsf[j] = L_add(L_lsf[j], + (Word32)(st->lsf_hist[i * M + j]), pOverflow); + } + } + + for (j = M - 1; j >= 0; j--) + { + if (L_lsf[j] < 0) + { + lsf[j] = (Word16)(~((~L_lsf[j]) >> 3)); + } + else + { + lsf[j] = (Word16)(L_lsf[j] >> 3); + } + } + + Lsf_lsp(lsf, st->lsp, M, pOverflow); + + /* make log_en speech coder mode independent */ + /* added again later before synthesis */ + st->log_en = sub(st->log_en, st->log_en_adjust, pOverflow); + + /* compute lsf variability vector */ + oscl_memmove((void *)st->lsf_hist_mean, st->lsf_hist, 80*sizeof(*st->lsf_hist)); + + for (i = M - 1; i >= 0; i--) + { + L_lsf_mean = 0; + /* compute mean lsf */ + for (j = 8 - 1; j >= 0; j--) + { + L_lsf_mean = L_add(L_lsf_mean, + (Word32)(st->lsf_hist_mean[i+j*M]), pOverflow); + } + + if (L_lsf_mean < 0) + { + lsf_mean = (Word16)(~((~L_lsf_mean) >> 3)); + } + else + { + lsf_mean = (Word16)(L_lsf_mean >> 3); + } + /* subtract mean and limit to within reasonable limits * + * moreover the upper lsf's are attenuated */ + for (j = 8 - 1; j >= 0; j--) + { + /* subtract mean */ + st->lsf_hist_mean[i+j*M] = + sub(st->lsf_hist_mean[i+j*M], lsf_mean, pOverflow); + + /* attenuate deviation from mean, especially for upper lsf's */ + st->lsf_hist_mean[i+j*M] = + mult(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i], pOverflow); + + /* limit the deviation */ + if (st->lsf_hist_mean[i+j*M] < 0) + { + negative = 1; + } + else + { + negative = 0; + } + st->lsf_hist_mean[i+j*M] = abs_s(st->lsf_hist_mean[i+j*M]); + + /* apply soft limit */ + if (st->lsf_hist_mean[i+j*M] > 655) + { + st->lsf_hist_mean[i+j*M] = 655 + ((st->lsf_hist_mean[i+j*M] + - 655) >> 2); + } + + /* apply hard limit */ + if (st->lsf_hist_mean[i+j*M] > 1310) + { + st->lsf_hist_mean[i+j*M] = 1310; + } + + if (negative != 0) + { + st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M]; + } + } + } + } + + + if (st->sid_frame != 0) + { + /* Set old SID parameters, always shift */ + /* even if there is no new valid_data */ + oscl_memmove((void *)st->lsp_old, st->lsp, M*sizeof(*st->lsp)); + st->old_log_en = st->log_en; + + if (st->valid_data != 0) /* new data available (no CRC) */ + { + /* Compute interpolation factor, since the division only works * + * for values of since_last_sid < 32 we have to limit the * + * interpolation to 32 frames */ + tmp_int_length = st->since_last_sid; + st->since_last_sid = 0; + + if (tmp_int_length >= 32) + { + tmp_int_length = 32; + } + + L_temp = ((Word32) tmp_int_length) << 10; + if (L_temp != (Word32)((Word16) L_temp)) + { + *pOverflow = 1; + L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16); + } + temp = (Word16) L_temp; + + if (tmp_int_length >= 2) + { + st->true_sid_period_inv = div_s(1 << 10, temp); + } + else + { + st->true_sid_period_inv = 1 << 14; /* 0.5 it Q15 */ + } + + Init_D_plsf_3(lsfState, parm[0], common_amr_tbls->past_rq_init_ptr); + D_plsf_3(lsfState, MRDTX, 0, &parm[1], common_amr_tbls, st->lsp, pOverflow); + /* reset for next speech frame */ + oscl_memset((void *)lsfState->past_r_q, 0, M*sizeof(*lsfState->past_r_q)); + + log_en_index = parm[4]; + /* Q11 and divide by 4 */ + if ((log_en_index > 63) || (log_en_index < -64)) + { + st->log_en = (log_en_index > 0) ? MAX_16 : MIN_16; + } + else + { + st->log_en = (log_en_index) << (11 - 2); + } + + /* Subtract 2.5 in Q11 */ + st->log_en -= (2560 * 2); + + /* Index 0 is reserved for silence */ + if (log_en_index == 0) + { + st->log_en = MIN_16; + } + + /* no interpolation at startup after coder reset */ + /* or when SID_UPD has been received right after SPEECH */ + + if ((st->data_updated == 0) || + (st->dtxGlobalState == SPEECH)) + { + oscl_memmove((void *)st->lsp_old, st->lsp, M*sizeof(*st->lsp)); + st->old_log_en = st->log_en; + } + } /* endif valid_data */ + + /* initialize gain predictor memory of other modes */ + if (st->log_en < 0) + { + temp = ~((~st->log_en) >> 1); + } + else + { + temp = st->log_en >> 1; + } + ma_pred_init = temp - 9000; + + if (ma_pred_init > 0) + { + ma_pred_init = 0; + } + else if (ma_pred_init < -14436) + { + ma_pred_init = -14436; + } + + predState->past_qua_en[0] = ma_pred_init; + predState->past_qua_en[1] = ma_pred_init; + predState->past_qua_en[2] = ma_pred_init; + predState->past_qua_en[3] = ma_pred_init; + + /* past_qua_en for other modes than MR122 */ + ma_pred_init = ((Word32) ma_pred_init * 5443) >> 15; + + /* scale down by factor 20*log10(2) in Q15 */ + predState->past_qua_en_MR122[0] = ma_pred_init; + predState->past_qua_en_MR122[1] = ma_pred_init; + predState->past_qua_en_MR122[2] = ma_pred_init; + predState->past_qua_en_MR122[3] = ma_pred_init; + } /* endif sid_frame */ + + /* CN generation */ + /* recompute level adjustment factor Q11 * + * st->log_en_adjust = 0.9*st->log_en_adjust + * + * 0.1*dtx_log_en_adjust[mode]); */ + if (dtx_log_en_adjust[mode] > 1023) + { + temp = MAX_16; + } + else if (dtx_log_en_adjust[mode] < -1024) + { + temp = MIN_16; + } + else + { + temp = (((Word32) dtx_log_en_adjust[mode] << 5) * 3277) >> 15; + + + } + + if (temp < 0) + { + temp = ~((~temp) >> 5); + } + else + { + temp >>= 5; + } + st->log_en_adjust = add_16(((Word32)st->log_en_adjust * 29491) >> 15, temp, pOverflow); + + + /* Interpolate SID info */ + int_fac = shl((st->since_last_sid + 1), 10, pOverflow); /* Q10 */ + int_fac = mult(int_fac, st->true_sid_period_inv, pOverflow); /* Q10 * Q15 -> Q10 */ + + /* Maximize to 1.0 in Q10 */ + if (int_fac > 1024) + { + int_fac = 16384; + } + else if (int_fac < -2048) + { + int_fac = MIN_16; + } + else + { + int_fac <<= 4; /* Q10 -> Q14 */ + } + + L_log_en_int = L_mult(int_fac, st->log_en, pOverflow); /* Q14 * Q11->Q26 */ + for (i = M - 1; i >= 0; i--) + { + lsp_int[i] = mult(int_fac, st->lsp[i], pOverflow);/* Q14 * Q15 -> Q14 */ + } + + int_fac = 16384 - int_fac; /* 1-k in Q14 */ + + /* (Q14 * Q11 -> Q26) + Q26 -> Q26 */ + L_log_en_int = L_mac(L_log_en_int, int_fac, st->old_log_en, pOverflow); + for (i = M - 1; i >= 0; i--) + { + /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */ + lsp_int[i] = add_16(lsp_int[i], mult(int_fac, st->lsp_old[i], pOverflow), pOverflow); + + L_temp = ((Word32) lsp_int[i]) << 1; /* Q14 -> Q15 */ + if (L_temp != (Word32)((Word16) L_temp)) + { + *pOverflow = 1; + L_temp = (Word32)((lsp_int[i] > 0) ? MAX_16 : MIN_16); + } + lsp_int[i] = (Word16) L_temp; + } + + /* compute the amount of lsf variability */ + lsf_variab_factor = st->log_pg_mean - 2457; /* -0.6 in Q12 */ + /* *0.3 Q12*Q15 -> Q12 */ + lsf_variab_factor = 4096 - mult(lsf_variab_factor, 9830, pOverflow); + + /* limit to values between 0..1 in Q12 */ + if (lsf_variab_factor > 4095) + { + lsf_variab_factor = MAX_16; + } + else if (lsf_variab_factor < 0) + { + lsf_variab_factor = 0; + } + else + { + lsf_variab_factor <<= 3; /* -> Q15 */ + } + + /* get index of vector to do variability with */ + lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3); + + /* convert to lsf */ + Lsp_lsf(lsp_int, lsf_int, M, pOverflow); + + /* apply lsf variability */ + oscl_memmove((void *)lsf_int_variab, lsf_int, M*sizeof(*lsf_int)); + for (i = M - 1; i >= 0; i--) + { + lsf_int_variab[i] = add_16(lsf_int_variab[i], + mult(lsf_variab_factor, + st->lsf_hist_mean[i+lsf_variab_index*M], pOverflow) + , pOverflow); + } + + /* make sure that LSP's are ordered */ + Reorder_lsf(lsf_int, LSF_GAP, M, pOverflow); + Reorder_lsf(lsf_int_variab, LSF_GAP, M, pOverflow); + + /* copy lsf to speech decoders lsf state */ + oscl_memmove((void *)lsfState->past_lsf_q, lsf_int, M*sizeof(*lsf_int)); + + /* convert to lsp */ + Lsf_lsp(lsf_int, lsp_int, M, pOverflow); + Lsf_lsp(lsf_int_variab, lsp_int_variab, M, pOverflow); + + /* Compute acoeffs Q12 acoeff is used for level * + * normalization and postfilter, acoeff_variab is * + * used for synthesis filter * + * by doing this we make sure that the level * + * in high frequenncies does not jump up and down */ + + Lsp_Az(lsp_int, acoeff, pOverflow); + Lsp_Az(lsp_int_variab, acoeff_variab, pOverflow); + + /* For use in postfilter */ + oscl_memmove((void *)&A_t[0], acoeff, (M + 1)*sizeof(*acoeff)); + oscl_memmove((void *)&A_t[M + 1], acoeff, (M + 1)*sizeof(*acoeff)); + oscl_memmove((void *)&A_t[2 *(M + 1)], acoeff, (M + 1)*sizeof(*acoeff)); + oscl_memmove((void *)&A_t[3 *(M + 1)], acoeff, (M + 1)*sizeof(*acoeff)); + + /* Compute reflection coefficients Q15 */ + A_Refl(&acoeff[1], refl, pOverflow); + + /* Compute prediction error in Q15 */ + pred_err = MAX_16; /* 0.99997 in Q15 */ + for (i = 0; i < M; i++) + { + L_temp = (((Word32) refl[i]) * refl[i]) >> 15; + if (L_temp <= 0x00007fffL) + { + temp = MAX_16 - (Word16) L_temp; + } + else + { + *pOverflow = 1; + temp = 0; + } + pred_err = mult(pred_err, temp, pOverflow); + } + + /* compute logarithm of prediction gain */ + Log2((Word32)(pred_err), &log_pg_e, &log_pg_m, pOverflow); + + /* convert exponent and mantissa to Word16 Q12 */ + log_pg = shl((log_pg_e - 15), 12, pOverflow); /* Q12 */ + log_pg = shr(sub(0, add_16(log_pg, shr(log_pg_m, 15 - 12, pOverflow), + pOverflow), pOverflow), 1, pOverflow); + st->log_pg_mean = add_16(mult(29491, st->log_pg_mean, pOverflow), + mult(3277, log_pg, pOverflow), pOverflow); + + /* Compute interpolated log energy */ + L_log_en_int = L_shr(L_log_en_int, 10, pOverflow); /* Q26 -> Q16 */ + + /* Add 4 in Q16 */ + L_log_en_int = L_add(L_log_en_int, 4 * 65536L, pOverflow); + + /* subtract prediction gain */ + L_log_en_int = L_sub(L_log_en_int, L_shl((Word32)(log_pg), 4, pOverflow), pOverflow); + + /* adjust level to speech coder mode */ + L_log_en_int = L_add(L_log_en_int, + L_shl((Word32)(st->log_en_adjust), 5, pOverflow), pOverflow); + + log_en_int_e = (Word16)(L_log_en_int >> 16); + + log_en_int_m = (Word16)(L_shr(L_sub(L_log_en_int, + ((Word32)log_en_int_e << 16), pOverflow), 1, pOverflow)); + level = (Word16)(Pow2(log_en_int_e, log_en_int_m, pOverflow)); /* Q4 */ + + for (i = 0; i < 4; i++) + { + /* Compute innovation vector */ + build_CN_code(&st->L_pn_seed_rx, ex, pOverflow); + for (j = L_SUBFR - 1; j >= 0; j--) + { + ex[j] = mult(level, ex[j], pOverflow); + } + /* Synthesize */ + Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR, + mem_syn, 1); + + } /* next i */ + + /* reset codebook averaging variables */ + averState->hangVar = 20; + averState->hangCount = 0; + + if (new_state == DTX_MUTE) + { + /* mute comfort noise as it has been quite a long time since + * last SID update was performed */ + + tmp_int_length = st->since_last_sid; + + if (tmp_int_length > 32) + { + tmp_int_length = 32; + } + else if (tmp_int_length <= 0) + { + /* safety guard against division by zero */ + tmp_int_length = 8; + } + + L_temp = ((Word32) tmp_int_length) << 10; + if (L_temp != (Word32)((Word16) L_temp)) + { + *pOverflow = 1; + L_temp = (Word32)((tmp_int_length > 0) ? MAX_16 : MIN_16); + } + temp = (Word16) L_temp; + + st->true_sid_period_inv = div_s(1 << 10, temp); + + st->since_last_sid = 0; + oscl_memmove((void *)st->lsp_old, st->lsp, M*sizeof(*st->lsp)); + st->old_log_en = st->log_en; + /* subtract 1/8 in Q11 i.e -6/8 dB */ + st->log_en = st->log_en - 256; + } + + /* reset interpolation length timer + * if data has been updated. */ + if ((st->sid_frame != 0) && + ((st->valid_data != 0) || + ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0))) + { + st->since_last_sid = 0; + st->data_updated = 1; + } + + return; +} + + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: dtx_dec_activity_update +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type dtx_decState + lsf = + frame = + + Outputs: + st points to an updated structure of type dtx_decState + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function updates the DTX parameters. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void dtx_dec_activity_update(dtx_decState *st, + Word16 lsf[], + Word16 frame[]) +{ + Word16 i; + + Word32 L_frame_en; + Word16 log_en_e, log_en_m, log_en; + + // update lsp history + st->lsf_hist_ptr = add(st->lsf_hist_ptr,M); + if (sub(st->lsf_hist_ptr, 80) == 0) + { + st->lsf_hist_ptr = 0; + } + Copy(lsf, &st->lsf_hist[st->lsf_hist_ptr], M); + + // compute log energy based on frame energy + L_frame_en = 0; // Q0 + for (i=0; i < L_FRAME; i++) + { + L_frame_en = L_mac(L_frame_en, frame[i], frame[i]); + } + Log2(L_frame_en, &log_en_e, &log_en_m); + + // convert exponent and mantissa to Word16 Q10 + log_en = shl(log_en_e, 10); // Q10 + log_en = add(log_en, shr(log_en_m, 15-10)); + + // divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 + log_en = sub(log_en, 7497+1024); + + // insert into log energy buffer, no division by two as * + * log_en in decoder is Q11 + st->log_en_hist_ptr = add(st->log_en_hist_ptr, 1); + if (sub(st->log_en_hist_ptr, DTX_HIST_SIZE) == 0) + { + st->log_en_hist_ptr = 0; + } + st->log_en_hist[st->log_en_hist_ptr] = log_en; // Q11 +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void dtx_dec_activity_update(dtx_decState *st, + Word16 lsf[], + Word16 frame[], + Flag *pOverflow) +{ + Word16 i; + + Word32 L_frame_en; + Word32 L_temp; + Word16 log_en_e; + Word16 log_en_m = 0; + Word16 log_en = 0; + + /* update lsp history */ + st->lsf_hist_ptr += M; + + if (st->lsf_hist_ptr == 80) + { + st->lsf_hist_ptr = 0; + } + oscl_memmove((void *)&st->lsf_hist[st->lsf_hist_ptr], lsf, M*sizeof(*lsf)); + + /* compute log energy based on frame energy */ + L_frame_en = 0; /* Q0 */ + for (i = L_FRAME - 1; i >= 0; i--) + { + L_temp = ((Word32) frame[i]) * frame[i]; + if (L_temp != (Word32) 0x40000000L) + { + L_temp = L_temp << 1; + } + else + { + L_temp = MAX_32; + } + L_frame_en = L_add(L_frame_en, L_temp, pOverflow); + } + Log2(L_frame_en, &log_en_e, &log_en_m, pOverflow); + + /* convert exponent and mantissa to Word16 Q10 */ + L_temp = ((Word32) log_en_e) << 10; + + if (L_temp != (Word32)((Word16) L_temp)) + { + *pOverflow = 1; + L_temp = (Word32)((log_en_e > 0) ? MAX_16 : MIN_16); + } + log_en_e = (Word16) L_temp; + + if (log_en_m < 0) + { + log_en_m = ~((~log_en_m) >> 5); + } + else + { + log_en_m >>= 5; + } + log_en = log_en_e + log_en_m; + + /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */ + log_en -= 7497 + 1024; + + /* insert into log energy buffer, no division by two as * + * log_en in decoder is Q11 */ + st->log_en_hist_ptr += 1; + + if (st->log_en_hist_ptr == DTX_HIST_SIZE) + { + st->log_en_hist_ptr = 0; + } + st->log_en_hist[st->log_en_hist_ptr] = log_en; /* Q11 */ + + return; +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: rx_dtx_handler +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type dtx_decState + frame_type = RX frame type + + Returns: + newState = variable of type DTXStateType + + Outputs: + st points to an updated structure of type dtx_decState + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function determines the new state of the decoder based on the frame_type + and sets up the decoder parameters according to newState. + + Table of new SPD synthesis states + + | previous SPD_synthesis_state + Incoming | + frame_type | SPEECH | DTX | DTX_MUTE + --------------------------------------------------------------- + RX_SPEECH_GOOD , | | | + RX_SPEECH_PR_DEGRADED | SPEECH | SPEECH | SPEECH + ---------------------------------------------------------------- + RX_SPEECH_PR_BAD, | | | + RX_SPEECH_BAD, | SPEECH | DTX | DTX_MUTE + ---------------------------------------------------------------- + RX_SID_FIRST, | DTX | DTX/(DTX_MUTE)| DTX_MUTE + ---------------------------------------------------------------- + RX_SID_UPDATE, | DTX | DTX | DTX + ---------------------------------------------------------------- + RX_SID_BAD, | DTX | DTX/(DTX_MUTE)| DTX_MUTE + ---------------------------------------------------------------- + RX_NO_DATA | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE + |(class2 garb.)| | + ---------------------------------------------------------------- + RX_ONSET | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE + |(class2 garb.)| | + ---------------------------------------------------------------- + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + dtx_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.3.0, December 12, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +enum DTXStateType rx_dtx_handler( + dtx_decState *st, // i/o : State struct + enum RXFrameType frame_type // i : Frame type + ) +{ + enum DTXStateType newState; + enum DTXStateType encState; + + // DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) + if ((sub(frame_type, RX_SID_FIRST) == 0) || + (sub(frame_type, RX_SID_UPDATE) == 0) || + (sub(frame_type, RX_SID_BAD) == 0) || + (((sub(st->dtxGlobalState, DTX) == 0) || + (sub(st->dtxGlobalState, DTX_MUTE) == 0)) && + ((sub(frame_type, RX_NO_DATA) == 0) || + (sub(frame_type, RX_SPEECH_BAD) == 0) || + (sub(frame_type, RX_ONSET) == 0)))) + { + newState = DTX; + + // stay in mute for these input types + if ((sub(st->dtxGlobalState, DTX_MUTE) == 0) && + ((sub(frame_type, RX_SID_BAD) == 0) || + (sub(frame_type, RX_SID_FIRST) == 0) || + (sub(frame_type, RX_ONSET) == 0) || + (sub(frame_type, RX_NO_DATA) == 0))) + { + newState = DTX_MUTE; + } + + // evaluate if noise parameters are too old + // since_last_sid is reset when CN parameters have been updated + st->since_last_sid = add(st->since_last_sid, 1); + + // no update of sid parameters in DTX for a long while + // Due to the delayed update of st->since_last_sid counter + // SID_UPDATE frames need to be handled separately to avoid + // entering DTX_MUTE for late SID_UPDATE frames + if((sub(frame_type, RX_SID_UPDATE) != 0) && + (sub(st->since_last_sid, DTX_MAX_EMPTY_THRESH) > 0)) + { + newState = DTX_MUTE; + } + } + else + { + newState = SPEECH; + st->since_last_sid = 0; + } + + // reset the decAnaElapsed Counter when receiving CNI data the first + // time, to robustify counter missmatch after handover + // this might delay the bwd CNI analysis in the new decoder slightly. + + if ((st->data_updated == 0) && + (sub(frame_type, RX_SID_UPDATE) == 0)) + { + st->decAnaElapsedCount = 0; + } + + // update the SPE-SPD DTX hangover synchronization + // to know when SPE has added dtx hangover + st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1); + st->dtxHangoverAdded = 0; + + if ((sub(frame_type, RX_SID_FIRST) == 0) || + (sub(frame_type, RX_SID_UPDATE) == 0) || + (sub(frame_type, RX_SID_BAD) == 0) || + (sub(frame_type, RX_ONSET) == 0) || + (sub(frame_type, RX_NO_DATA) == 0)) + { + encState = DTX; + + // In frame errors simulations RX_NO_DATA may occasionally mean that + // a speech packet was probably sent by the encoder, + // the assumed _encoder_ state should be SPEECH in such cases. + if((sub(frame_type, RX_NO_DATA) == 0) && + (sub(newState, SPEECH) == 0)) + { + encState = SPEECH; + } + + // Note on RX_ONSET operation differing from RX_NO_DATA operation: + // If a RX_ONSET is received in the decoder (by "accident") + // it is still most likely that the encoder state + // for the "ONSET frame" was DTX. + + } + else + { + encState = SPEECH; + } + + if (sub(encState, SPEECH) == 0) + { + st->dtxHangoverCount = DTX_HANG_CONST; + } + else + { + if (sub(st->decAnaElapsedCount, DTX_ELAPSED_FRAMES_THRESH) > 0) + { + st->dtxHangoverAdded = 1; + st->decAnaElapsedCount = 0; + st->dtxHangoverCount = 0; + } + else if (st->dtxHangoverCount == 0) + { + st->decAnaElapsedCount = 0; + } + else + { + st->dtxHangoverCount = sub(st->dtxHangoverCount, 1); + } + } + + if (sub(newState, SPEECH) != 0) + { + // DTX or DTX_MUTE + // CN data is not in a first SID, first SIDs are marked as SID_BAD + // but will do backwards analysis if a hangover period has been added + // according to the state machine above + + st->sid_frame = 0; + st->valid_data = 0; + + if (sub(frame_type, RX_SID_FIRST) == 0) + { + st->sid_frame = 1; + } + else if (sub(frame_type, RX_SID_UPDATE) == 0) + { + st->sid_frame = 1; + st->valid_data = 1; + } + else if (sub(frame_type, RX_SID_BAD) == 0) + { + st->sid_frame = 1; + st->dtxHangoverAdded = 0; // use old data + } + } + + return newState; + // newState is used by both SPEECH AND DTX synthesis routines +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +enum DTXStateType rx_dtx_handler( + dtx_decState *st, /* i/o : State struct */ + enum RXFrameType frame_type,/* i : Frame type */ + Flag *pOverflow) +{ + enum DTXStateType newState; + enum DTXStateType encState; + + + /* DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) */ + + if ((frame_type == RX_SID_FIRST) || + (frame_type == RX_SID_UPDATE) || + (frame_type == RX_SID_BAD) || + (((st->dtxGlobalState == DTX) || (st->dtxGlobalState == DTX_MUTE)) && + ((frame_type == RX_NO_DATA) || (frame_type == RX_SPEECH_BAD) || + (frame_type == RX_ONSET)))) + { + newState = DTX; + + /* stay in mute for these input types */ + + if ((st->dtxGlobalState == DTX_MUTE) && + ((frame_type == RX_SID_BAD) || + (frame_type == RX_SID_FIRST) || + (frame_type == RX_ONSET) || + (frame_type == RX_NO_DATA))) + { + newState = DTX_MUTE; + } + + /* evaluate if noise parameters are too old */ + /* since_last_sid is reset when CN parameters have been updated */ + st->since_last_sid += 1; + + /* no update of sid parameters in DTX for a long while */ + /* Due to the delayed update of st->since_last_sid counter */ + /* SID_UPDATE frames need to be handled separately to avoid */ + /* entering DTX_MUTE for late SID_UPDATE frames */ + if ((frame_type != RX_SID_UPDATE) && + (st->since_last_sid > DTX_MAX_EMPTY_THRESH)) + { + newState = DTX_MUTE; + } + } + else + { + newState = SPEECH; + st->since_last_sid = 0; + } + + /* + reset the decAnaElapsed Counter when receiving CNI data the first + time, to robustify counter missmatch after handover + this might delay the bwd CNI analysis in the new decoder slightly. + */ + + if ((st->data_updated == 0) && + (frame_type == RX_SID_UPDATE)) + { + st->decAnaElapsedCount = 0; + } + + /* update the SPE-SPD DTX hangover synchronization */ + /* to know when SPE has added dtx hangover */ + st->decAnaElapsedCount = add_16(st->decAnaElapsedCount, 1, pOverflow); + st->dtxHangoverAdded = 0; + + if ((frame_type == RX_SID_FIRST) || + (frame_type == RX_SID_UPDATE) || + (frame_type == RX_SID_BAD) || + (frame_type == RX_ONSET) || + (frame_type == RX_NO_DATA)) + { + encState = DTX; + + /* + In frame errors simulations RX_NO_DATA may occasionally mean that + a speech packet was probably sent by the encoder, + the assumed _encoder_ state should be SPEECH in such cases. + */ + if ((frame_type == RX_NO_DATA) && + (newState == SPEECH)) + { + encState = SPEECH; + } + + /* + Note on RX_ONSET operation differing from RX_NO_DATA operation: + If a RX_ONSET is received in the decoder (by "accident") + it is still most likely that the encoder state + for the "ONSET frame" was DTX. + */ + } + else + { + encState = SPEECH; + } + + + if (encState == SPEECH) + { + st->dtxHangoverCount = DTX_HANG_CONST; + } + else + { + + if (st->decAnaElapsedCount > DTX_ELAPSED_FRAMES_THRESH) + { + st->dtxHangoverAdded = 1; + st->decAnaElapsedCount = 0; + st->dtxHangoverCount = 0; + } + else if (st->dtxHangoverCount == 0) + { + st->decAnaElapsedCount = 0; + } + else + { + st->dtxHangoverCount -= 1; + } + } + + if (newState != SPEECH) + { + /* DTX or DTX_MUTE + * CN data is not in a first SID, first SIDs are marked as SID_BAD + * but will do backwards analysis if a hangover period has been added + * according to the state machine above + */ + + st->sid_frame = 0; + st->valid_data = 0; + + if (frame_type == RX_SID_FIRST) + { + st->sid_frame = 1; + } + else if (frame_type == RX_SID_UPDATE) + { + st->sid_frame = 1; + st->valid_data = 1; + } + else if (frame_type == RX_SID_BAD) + { + st->sid_frame = 1; + st->dtxHangoverAdded = 0; /* use old data */ + } + } + + /* newState is used by both SPEECH AND DTX synthesis routines */ + return(newState); +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dtx_dec.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dtx_dec.h new file mode 100644 index 0000000..af462f7 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/dtx_dec.h @@ -0,0 +1,181 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: dtx_dec.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : dtx_dec.h + Purpose : Decode comfort noice when in DTX + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef DTX_DEC_H +#define DTX_DEC_H +#define dtx_dec_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "d_plsf.h" +#include "gc_pred.h" +#include "c_g_aver.h" +#include "frame.h" +#include "dtx_common_def.h" +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + enum DTXStateType {SPEECH = 0, DTX, DTX_MUTE}; + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + Word16 since_last_sid; + Word16 true_sid_period_inv; + Word16 log_en; + Word16 old_log_en; + Word32 L_pn_seed_rx; + Word16 lsp[M]; + Word16 lsp_old[M]; + + Word16 lsf_hist[M*DTX_HIST_SIZE]; + Word16 lsf_hist_ptr; + Word16 lsf_hist_mean[M*DTX_HIST_SIZE]; + Word16 log_pg_mean; + Word16 log_en_hist[DTX_HIST_SIZE]; + Word16 log_en_hist_ptr; + + Word16 log_en_adjust; + + Word16 dtxHangoverCount; + Word16 decAnaElapsedCount; + + Word16 sid_frame; + Word16 valid_data; + Word16 dtxHangoverAdded; + + enum DTXStateType dtxGlobalState; /* contains previous state */ + /* updated in main decoder */ + + Word16 data_updated; /* marker to know if CNI data is ever renewed */ + + } dtx_decState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + /* + * Function : dtx_dec_reset + * Purpose : Resets state memory + * Returns : 0 on success + */ + Word16 dtx_dec_reset(dtx_decState *st); + + /* + * Function : dtx_dec + * Purpose : + * Description : + */ + void dtx_dec( + dtx_decState *st, /* i/o : State struct */ + Word16 mem_syn[], /* i/o : AMR decoder state */ + D_plsfState* lsfState, /* i/o : decoder lsf states */ + gc_predState* predState, /* i/o : prediction states */ + Cb_gain_averageState* averState, /* i/o : CB gain average states */ + enum DTXStateType new_state, /* i : new DTX state */ + enum Mode mode, /* i : AMR mode */ + Word16 parm[], /* i : Vector of synthesis parameters */ + CommonAmrTbls* common_amr_tbls, /* i : Ptr to struct of table ptrs */ + Word16 synth[], /* o : synthesised speech */ + Word16 A_t[], /* o : decoded LP filter in 4 subframes*/ + Flag *pOverflow + ); + + void dtx_dec_activity_update(dtx_decState *st, + Word16 lsf[], + Word16 frame[], + Flag *pOverflow); + + /* + * Function : rx_dtx_handler + * Purpose : reads the frame type and checks history + * Description : to decide what kind of DTX/CNI action to perform + */ + enum DTXStateType rx_dtx_handler(dtx_decState *st, /* i/o : State struct */ + enum RXFrameType frame_type,/* i : Frame type */ + Flag *pOverflow); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* DEC_AMR_H_ */ diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ec_gains.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ec_gains.cpp new file mode 100644 index 0000000..f5d4a9d --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ec_gains.cpp @@ -0,0 +1,720 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + + Filename: ec_gains.cpp + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + These modules execute the code book gains for error concealment. This module + contains the init, reset, exit, and "main" functions in this process. + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "ec_gains.h" +#include "typedef.h" +#include "cnst.h" +#include "gmed_n.h" +#include "gc_pred.h" +#include "basic_op.h" + + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. Include conditional + ; compile variables also. + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; LOCAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; LOCAL VARIABLE DEFINITIONS + ; Variable declaration - defined here and used outside this module + ----------------------------------------------------------------------------*/ + + /*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ec_gain_code_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a pointer to a structure containing code state data of + stucture type ec_gain_codeState + + Outputs: + None. + + Returns: + None + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function resets the state data for the ec_gain module. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + None + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int ec_gain_code_reset (ec_gain_codeState *state) +{ + Word16 i; + + if (state == (ec_gain_codeState *) NULL){ + // fprintf(stderr, "ec_gain_code_reset: invalid parameter\n"); + return -1; + } + + for ( i = 0; i < 5; i++) + state->gbuf[i] = 1; + state->past_gain_code = 0; + state->prev_gc = 1; + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 ec_gain_code_reset(ec_gain_codeState *state) +{ + Word16 i; + + if (state == (ec_gain_codeState *) NULL) + { + /* fprintf(stderr, "ec_gain_code_reset: invalid parameter\n"); */ + return -1; + } + + for (i = 0; i < 5; i++) + state->gbuf[i] = 1; + state->past_gain_code = 0; + state->prev_gc = 1; + + return 0; +} + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ec_gain_code +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a pointer to a structure containing code state data of + stucture type ec_gain_codeState + pred_state = pointer to MA predictor state of type gc_predState + state = state of the state machine of type Word16 + gain_code = pointer to decoded innovation gain of type Word16 + pOverflow = pointer to overflow indicator of type Flag + + Outputs: + st = pointer to a pointer to a structure containing code state data of + stucture type ec_gain_codeState + pred_state = pointer to MA predictor state of type gc_predState + pOverflow = 1 if there is an overflow else it is zero. + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + +This function does error concealment using the codebook. Call this function +only in BFI (instead of normal gain decoding function). + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + ec_gain.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + static const Word16 cdown[7] = + { + 32767, 32112, 32112, 32112, + 32112, 32112, 22937 + }; + + Word16 tmp; + Word16 qua_ener_MR122; + Word16 qua_ener; + + // calculate median of last five gain values + tmp = gmed_n (st->gbuf,5); + + // new gain = minimum(median, past_gain) * cdown[state] + if (sub (tmp, st->past_gain_code) > 0) + { + tmp = st->past_gain_code; + } + tmp = mult (tmp, cdown[state]); + *gain_code = tmp; + + // update table of past quantized energies with average of + // current values + + gc_pred_average_limited(pred_state, &qua_ener_MR122, &qua_ener); + gc_pred_update(pred_state, qua_ener_MR122, qua_ener); +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ +void ec_gain_code( + ec_gain_codeState *st, /* i/o : State struct */ + gc_predState *pred_state, /* i/o : MA predictor state */ + Word16 state, /* i : state of the state machine */ + Word16 *gain_code, /* o : decoded innovation gain */ + Flag *pOverflow +) +{ + static const Word16 cdown[7] = + { + 32767, 32112, 32112, 32112, + 32112, 32112, 22937 + }; + + Word16 tmp; + Word16 qua_ener_MR122; + Word16 qua_ener; + + /* calculate median of last five gain values */ + tmp = gmed_n(st->gbuf, 5); + + /* new gain = minimum(median, past_gain) * cdown[state] */ + if (sub(tmp, st->past_gain_code, pOverflow) > 0) + { + tmp = st->past_gain_code; + } + tmp = mult(tmp, cdown[state], pOverflow); + *gain_code = tmp; + + /* update table of past quantized energies with average of + * current values + */ + gc_pred_average_limited(pred_state, &qua_ener_MR122, &qua_ener, pOverflow); + gc_pred_update(pred_state, qua_ener_MR122, qua_ener); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ec_gain_code_update +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a pointer to a structure containing code state data of + stucture type ec_gain_codeState + bfi = a flag that indicates if the frame is bad of type Word16 + prev_bf = a flag that indicates if the previous frame was bad of type Word16 + gain_code = pointer to decoded innovation gain of type Word16 + pOverflow = pointer to overflow indicator of type Flag + + Outputs: + st = pointer to a pointer to a structure containing code state data of + stucture type ec_gain_codeState + gain_code = pointer to decoded innovation gain of type Word16 + pOverflow = 1 if there is an overflow else it is zero. + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Purpose : update the codebook gain concealment state; + limit gain_code if the previous frame was bad + Call this function always after decoding (or concealing) + the gain + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + ec_gain.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + Word16 i; + + // limit gain_code by previous good gain if previous frame was bad + if (bfi == 0) + { + if (prev_bf != 0) + { + if (sub (*gain_code, st->prev_gc) > 0) + { + *gain_code = st->prev_gc; + } + } + st->prev_gc = *gain_code; + } + + // update EC states: previous gain, gain buffer + st->past_gain_code = *gain_code; + + for (i = 1; i < 5; i++) + { + st->gbuf[i - 1] = st->gbuf[i]; + } + st->gbuf[4] = *gain_code; + + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ +void ec_gain_code_update( + ec_gain_codeState *st, /* i/o : State struct */ + Word16 bfi, /* i : flag: frame is bad */ + Word16 prev_bf, /* i : flag: previous frame was bad */ + Word16 *gain_code, /* i/o : decoded innovation gain */ + Flag *pOverflow +) +{ + Word16 i; + + /* limit gain_code by previous good gain if previous frame was bad */ + if (bfi == 0) + { + if (prev_bf != 0) + { + if (sub(*gain_code, st->prev_gc, pOverflow) > 0) + { + *gain_code = st->prev_gc; + } + } + st->prev_gc = *gain_code; + } + + /* update EC states: previous gain, gain buffer */ + st->past_gain_code = *gain_code; + + for (i = 1; i < 5; i++) + { + st->gbuf[i - 1] = st->gbuf[i]; + } + st->gbuf[4] = *gain_code; + + return; +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ec_gain_pitch +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a pointer to a structure containing code + state data of stucture type ec_gain_pitchState + state = state of the state machine of type Word16 + pOverflow = pointer to overflow indicator of type Flag + + Outputs: + state = pointer to a pointer to a structure containing code + state data of stucture type ec_gain_pitchState + gain_pitch = pointer to pitch gain (Q14) of type Word16 + pOverflow = 1 if there is an overflow else it is zero. + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function conceals the error using code gain implementation in this + function. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + ec_gain.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + static const Word16 pdown[7] = + { + 32767, 32112, 32112, 26214, + 9830, 6553, 6553 + }; + + Word16 tmp; + + // calculate median of last five gains + tmp = gmed_n (st->pbuf, 5); + + // new gain = minimum(median, past_gain) * pdown[state] + if (sub (tmp, st->past_gain_pit) > 0) + { + tmp = st->past_gain_pit; + } + *gain_pitch = mult (tmp, pdown[state]); + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ +void ec_gain_pitch( + ec_gain_pitchState *st, /* i/o : state variables */ + Word16 state, /* i : state of the state machine */ + Word16 *gain_pitch, /* o : pitch gain (Q14) */ + Flag *pOverflow +) +{ + static const Word16 pdown[7] = + { + 32767, 32112, 32112, 26214, + 9830, 6553, 6553 + }; + + Word16 tmp; + + /* calculate median of last five gains */ + tmp = gmed_n(st->pbuf, 5); + + /* new gain = minimum(median, past_gain) * pdown[state] */ + if (sub(tmp, st->past_gain_pit, pOverflow) > 0) + { + tmp = st->past_gain_pit; + } + *gain_pitch = mult(tmp, pdown[state], pOverflow); +} + +/****************************************************************************/ +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ec_gain_pitch_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = state of the state machine of type Word16 + pOverflow = pointer to overflow indicator of type Flag + + Outputs: + state = pointer to a pointer to a structure containing code + state data of stucture type ec_gain_pitchState + pOverflow = 1 if there is an overflow else it is zero. + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Function: ec_gain_pitch_reset + Purpose: Resets state memory + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + ec_gain.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int ec_gain_pitch_reset (ec_gain_pitchState *state) +{ + Word16 i; + + if (state == (ec_gain_pitchState *) NULL){ + // fprintf(stderr, "ec_gain_pitch_reset: invalid parameter\n"); + return -1; + } + + for(i = 0; i < 5; i++) + state->pbuf[i] = 1640; + state->past_gain_pit = 0; + state->prev_gp = 16384; + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ +Word16 ec_gain_pitch_reset(ec_gain_pitchState *state) +{ + Word16 i; + + if (state == (ec_gain_pitchState *) NULL) + { + /* fprintf(stderr, "ec_gain_pitch_reset: invalid parameter\n"); */ + return -1; + } + + for (i = 0; i < 5; i++) + state->pbuf[i] = 1640; + state->past_gain_pit = 0; + state->prev_gp = 16384; + + return 0; +} + +/****************************************************************************/ + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ec_gain_pitch_update +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a pointer to a structure containing code + state data of stucture type ec_gain_pitchState + bfi = flag indicating the frame is bad of type Word16 + prev_bf = flag indicating the previous frame was bad of type Word16 + gain_pitch = pointer to pitch gain of type Word16 + pOverflow = pointer to overflow indicator of type Flag + + Outputs: + state = pointer to a pointer to a structure containing code + state data of stucture type ec_gain_pitchState + gain_pitch = pointer to pitch gain of type Word16 + pOverflow = 1 if there is an overflow else it is zero. + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Purpose : update the pitch gain concealment state; + limit gain_pitch if the previous frame was bad + Call this function always after decoding (or concealing) + the gain + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + ec_gain.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + Word16 i; + + if (bfi == 0) + { + if (prev_bf != 0) + { + if (sub (*gain_pitch, st->prev_gp) > 0) + { + *gain_pitch = st->prev_gp; + } + } + st->prev_gp = *gain_pitch; + } + + st->past_gain_pit = *gain_pitch; + + if (sub (st->past_gain_pit, 16384) > 0) // if (st->past_gain_pit > 1.0) + { + st->past_gain_pit = 16384; + } + for (i = 1; i < 5; i++) + { + st->pbuf[i - 1] = st->pbuf[i]; + } + st->pbuf[4] = st->past_gain_pit; + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ +void ec_gain_pitch_update( + ec_gain_pitchState *st, /* i/o : state variables */ + Word16 bfi, /* i : flag: frame is bad */ + Word16 prev_bf, /* i : flag: previous frame was bad */ + Word16 *gain_pitch, /* i/o : pitch gain */ + Flag *pOverflow +) +{ + Word16 i; + + if (bfi == 0) + { + if (prev_bf != 0) + { + if (sub(*gain_pitch, st->prev_gp, pOverflow) > 0) + { + *gain_pitch = st->prev_gp; + } + } + st->prev_gp = *gain_pitch; + } + + st->past_gain_pit = *gain_pitch; + + if (sub(st->past_gain_pit, 16384, pOverflow) > 0) + /* if (st->past_gain_pit > 1.0) */ + { + st->past_gain_pit = 16384; + } + for (i = 1; i < 5; i++) + { + st->pbuf[i - 1] = st->pbuf[i]; + } + st->pbuf[4] = st->past_gain_pit; +} + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ec_gains.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ec_gains.h new file mode 100644 index 0000000..6a16e0f --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ec_gains.h @@ -0,0 +1,196 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + Filename: ec_gains.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : ec_gains.c + Purpose: : Error concealment for pitch and codebook gains + +------------------------------------------------------------------------------ +*/ + +#ifndef _EC_GAINS_H_ +#define _EC_GAINS_H_ +#define ec_gains_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "gc_pred.h" + + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; [Define module specific macros here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; [Include all pre-processor statements here.] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; [Declare variables used in this module but defined elsewhere] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + Word16 pbuf[5]; + Word16 past_gain_pit; + Word16 prev_gp; + } ec_gain_pitchState; + + typedef struct + { + Word16 gbuf[5]; + Word16 past_gain_code; + Word16 prev_gc; + } ec_gain_codeState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; [List function prototypes here] + ----------------------------------------------------------------------------*/ + + /* + * Function : ec_gain_code_reset + * Purpose : Resets state memory + * + */ + Word16 ec_gain_code_reset( + ec_gain_codeState *state + ); + + + /* + * Function : ec_gain_code + * Purpose : conceal the codebook gain + * Call this function only in BFI (instead of normal gain + * decoding function) + */ + void ec_gain_code( + ec_gain_codeState *st, /* i/o : State struct */ + gc_predState *pred_state, /* i/o : MA predictor state */ + Word16 state, /* i : state of the state machine */ + Word16 *gain_code, /* o : decoded innovation gain */ + Flag *pOverflow + ); + + /* + * Function : ec_gain_code_update + * Purpose : update the codebook gain concealment state; + * limit gain_code if the previous frame was bad + * Call this function always after decoding (or concealing) + * the gain + */ + void ec_gain_code_update( + ec_gain_codeState *st, /* i/o : State struct */ + Word16 bfi, /* i : flag: frame is bad */ + Word16 prev_bf, /* i : flag: previous frame was bad */ + Word16 *gain_code, /* i/o : decoded innovation gain */ + Flag *pOverflow + ); + + + /* + * Function: ec_gain_pitch_reset + * Purpose: Resets state memory + */ + Word16 ec_gain_pitch_reset( + ec_gain_pitchState *state + ); + + /* + * Function : ec_gain_pitch_exit + * Purpose : The memory used for state memory is freed + */ + void ec_gain_pitch_exit( + ec_gain_pitchState **state + ); + + /* + * Function : ec_gain_pitch + * Purpose : conceal the pitch gain + * Call this function only in BFI (instead of normal gain + * decoding function) + */ + void ec_gain_pitch( + ec_gain_pitchState *st, /* i/o : state variables */ + Word16 state, /* i : state of the state machine */ + Word16 *gain_pitch, /* o : pitch gain (Q14) */ + Flag *pOverflow + ); + + /* + * Function : ec_gain_pitch_update + * Purpose : update the pitch gain concealment state; + * limit gain_pitch if the previous frame was bad + * Call this function always after decoding (or concealing) + * the gain + */ + void ec_gain_pitch_update( + ec_gain_pitchState *st, /* i/o : state variables */ + Word16 bfi, /* i : flag: frame is bad */ + Word16 prev_bf, /* i : flag: previous frame was bad */ + Word16 *gain_pitch, /* i/o : pitch gain */ + Flag *pOverflow + ); + + +#ifdef __cplusplus +} +#endif + +#endif /* _EC_GAINS_H_ */ + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ex_ctrl.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ex_ctrl.cpp new file mode 100644 index 0000000..9fae32a --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ex_ctrl.cpp @@ -0,0 +1,191 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: ex_ctrl.cpp + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "ex_ctrl.h" +#include "typedef.h" +#include "cnst.h" +#include "set_zero.h" +#include "gmed_n.h" +#include "sqrt_l.h" +#include "basic_op.h" +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ex_ctrl +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + excitation = pointer to current subframe excitation of type Word16 + excEnergy = Exc. Energy, sqrt(totEx*totEx) of type Word16 + exEnergyHist = pointer to history of subframe energies of type Word16 + voicedHangover = # of fr. after last voiced fr of type Word16 + carefulFlag = restrict dynamic in scaling of type Word16 + pOverflow = pointer to overflow indicator + + Outputs: + pOverflow = 1 if overflow exists in the math functions called by this function. + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Function : Ex_ctrl + Purpose : Charaterice synthesis speech and detect background noise + Returns : background noise decision; 0 = no bgn, 1 = bgn + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + ex_ctrl.c, 3GPP TS 26.101 version 4.1.0 Release 4, June 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ +Word16 Ex_ctrl(Word16 excitation[], /*i/o: Current subframe excitation */ + Word16 excEnergy, /* i : Exc. Energy, sqrt(totEx*totEx)*/ + Word16 exEnergyHist[], /* i : History of subframe energies */ + Word16 voicedHangover, /* i : # of fr. after last voiced fr.*/ + Word16 prevBFI, /* i : Set i previous BFI */ + Word16 carefulFlag, /* i : Restrict dymamic in scaling */ + Flag *pOverflow + ) +{ + Word16 i, exp; + Word16 testEnergy, scaleFactor, avgEnergy, prevEnergy; + Word32 t0; + + /* get target level */ + avgEnergy = gmed_n(exEnergyHist, 9); + + prevEnergy = (exEnergyHist[7] + exEnergyHist[8]) >> 1; + if (exEnergyHist[8] < prevEnergy) + { + prevEnergy = exEnergyHist[8]; + } + + /* upscaling to avoid too rapid energy rises for some cases */ + if ((excEnergy < avgEnergy) && (excEnergy > 5)) + { + testEnergy = shl(prevEnergy, 2, pOverflow); /* testEnergy = 4*prevEnergy; */ + + if ((voicedHangover < 7) || prevBFI != 0) + { + /* testEnergy = 3*prevEnergy */ + testEnergy = sub(testEnergy, prevEnergy, pOverflow); + } + + if (avgEnergy > testEnergy) + { + avgEnergy = testEnergy; + } + + /* scaleFactor=avgEnergy/excEnergy in Q0 (const 29 below)*/ + exp = norm_s(excEnergy); + excEnergy = shl(excEnergy, exp, pOverflow); + excEnergy = div_s((Word16) 16383, excEnergy); + t0 = L_mult(avgEnergy, excEnergy, pOverflow); + t0 = L_shr(t0, sub(20, exp, pOverflow), pOverflow); + /* const=30 for t0 in Q0, 20 for Q10 */ + if (t0 > 32767) + { + t0 = 32767; /* saturate */ + } + scaleFactor = (Word16)(t0); + + /* test if scaleFactor > 3.0 */ + if (carefulFlag != 0 && (scaleFactor > 3072)) + { + scaleFactor = 3072; + } + + /* scale the excitation by scaleFactor */ + for (i = 0; i < L_SUBFR; i++) + { + t0 = L_mult(scaleFactor, excitation[i], pOverflow); + t0 = L_shr(t0, 11, pOverflow); + excitation[i] = (Word16)(t0); + } + } + + return 0; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ex_ctrl.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ex_ctrl.h new file mode 100644 index 0000000..8f6472f --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ex_ctrl.h @@ -0,0 +1,122 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: ex_ctrl.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : ex_ctrl.h + Purpose : Excitation Control module in background noise + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef ex_ctrl_h +#define ex_ctrl_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "cnst.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ +#define L_ENERGYHIST 60 + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + /* + * Function : Ex_ctrl + * Purpose : Charaterice synthesis speech and detect background noise + * Returns : background noise decision; 0 = bgn, 1 = no bgn + */ + Word16 Ex_ctrl(Word16 excitation[], /*i/o: Current subframe excitation */ + Word16 excEnergy, /* i : Exc. Energy, sqrt(totEx*totEx)*/ + Word16 exEnergyHist[], /* i : History of subframe energies */ + Word16 voicedHangover, /* i : # of fr. after last voiced fr.*/ + Word16 prevBFI, /* i : Set i previous BFI */ + Word16 carefulFlag, /* i : Restrict dymamic in scaling */ + Flag *pOverflow + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _ex_ctrl_h_ */ + + + + + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/gsmamr_dec.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/gsmamr_dec.h new file mode 100644 index 0000000..4b43c9b --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/gsmamr_dec.h @@ -0,0 +1,150 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: gsmamr_dec.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This header contains all the necessary information needed to allow the gsm amr + decoder library to be used properly upon release. + +------------------------------------------------------------------------------ +*/ +#ifndef _GSMAMR_DEC_H_ +#define _GSMAMR_DEC_H_ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ + +#include "gsm_amr_typedefs.h" +#include "pvamrnbdecoder_api.h" +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; DEFINES + ----------------------------------------------------------------------------*/ + /* Number of 13-bit linear PCM samples per 20 ms frame */ + /* L_FRAME = (8 kHz) * (20 msec) = 160 samples */ +#define L_FRAME 160 + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + enum Frame_Type_3GPP + { + AMR_475 = 0, /* 4.75 kbps */ + AMR_515, /* 5.15 kbps */ + AMR_59, /* 5.9 kbps */ + AMR_67, /* 6.7 kbps */ + AMR_74, /* 7.4 kbps */ + AMR_795, /* 7.95 kbps */ + AMR_102, /* 10.2 kbps */ + AMR_122, /* 12.2 kbps */ + AMR_SID, /* GSM AMR DTX */ + GSM_EFR_SID, /* GSM EFR DTX */ + TDMA_EFR_SID, /* TDMA EFR DTX */ + PDC_EFR_SID, /* PDC EFR DTX */ + FOR_FUTURE_USE1, /* Unused 1 */ + FOR_FUTURE_USE2, /* Unused 2 */ + FOR_FUTURE_USE3, /* Unused 3 */ + AMR_NO_DATA + }; /* No data */ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ----------------------------------------------------------------------------*/ + /* + * This function allocates memory for filter structure and initializes state + * memory used by the GSM AMR decoder. This function returns zero. It will + * return negative one if there is an error. + */ + Word16 GSMInitDecode(void **state_data, + Word8 *id); + + /* + * AMRDecode steps into the part of the library that decodes the raw data + * speech bits for the decoding process. It returns the address offset of + * the next frame to be decoded. + */ + Word16 AMRDecode( + void *state_data, + enum Frame_Type_3GPP frame_type, + UWord8 *speech_bits_ptr, + Word16 *raw_pcm_buffer, + Word16 input_format + ); + + /* + * This function resets the state memory used by the GSM AMR decoder. This + * function returns zero. It will return negative one if there is an error. + */ + Word16 Speech_Decode_Frame_reset(void *state_data); + + /* + * This function frees up the memory used for the state memory of the + * GSM AMR decoder. + */ + void GSMDecodeFrameExit(void **state_data); + + +#ifdef __cplusplus +} +#endif + +#endif /* _GSMAMR_DEC_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/if2_to_ets.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/if2_to_ets.cpp new file mode 100644 index 0000000..6463bec --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/if2_to_ets.cpp @@ -0,0 +1,183 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + Filename: if2_to_ets.cpp + Funtions: if2_to_ets + +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "frame_type_3gpp.h" +#include "if2_to_ets.h" +#include "typedef.h" +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: if2_to_ets +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + frame_type_3gpp = decoder speech bit rate (enum Frame_Type_3GPP) + if2_input_ptr = pointer to input encoded speech bits in IF2 format (Word8) + ets_output_ptr = pointer to output encoded speech bits in ETS format (Word16) + + Outputs: + ets_output_ptr = pointer to encoded speech bits in the ETS format (Word16) + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function performs a transformation on the data buffers. It converts the + data format from IF2 to ETS. IF2 is the storage format where the frame type + is in the first four bits of the first byte. The upper four bits of that byte + contain the first four encoded speech bits for the frame. The following bytes + contain the rest of the encoded speech bits. The final byte has padded zeros + to make the frame byte aligned. ETS format has the encoded speech + bits each separate with only one bit stored in each word. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + +AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0 Release 4, June 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void if2_to_ets( + enum Frame_Type_3GPP frame_type_3gpp, + UWord8 *if2_input_ptr, + Word16 *ets_output_ptr, + CommonAmrTbls* common_amr_tbls) +{ + + Word16 i; + Word16 j; + Word16 x = 0; + const Word16* numCompressedBytes_ptr = common_amr_tbls->numCompressedBytes_ptr; + const Word16* numOfBits_ptr = common_amr_tbls->numOfBits_ptr; + const Word16* const* reorderBits_ptr = common_amr_tbls->reorderBits_ptr; + + /* + * The following section of code accesses bits in the IF2 method of + * bit ordering. Each bit is given its own location in the buffer pointed + * to by ets_output_ptr. The bits (for modes less than AMR_SID) are + * reordered using the tables in bitreorder.c before the data is stored + * into the buffer pointed to by ets_output_ptr. + */ + + if (frame_type_3gpp < AMR_SID) + { + for (j = 4; j < 8; j++) + { + ets_output_ptr[reorderBits_ptr[frame_type_3gpp][x++]] = + (if2_input_ptr[0] >> j) & 0x01; + } + for (i = 1; i < numCompressedBytes_ptr[frame_type_3gpp]; i++) + { + for (j = 0; j < 8; j++) + { + if (x >= numOfBits_ptr[frame_type_3gpp]) + { + break; + } + ets_output_ptr[reorderBits_ptr[frame_type_3gpp][x++]] = + (if2_input_ptr[i] >> j) & 0x01; + } + } + } + else + { + for (j = 4; j < 8; j++) + { + ets_output_ptr[x++] = + (if2_input_ptr[0] >> j) & 0x01; + } + for (i = 1; i < numCompressedBytes_ptr[frame_type_3gpp]; i++) + { + for (j = 0; j < 8; j++) + { + ets_output_ptr[x++] = + (if2_input_ptr[i] >> j) & 0x01; + } + } + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/if2_to_ets.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/if2_to_ets.h new file mode 100644 index 0000000..4994991 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/if2_to_ets.h @@ -0,0 +1,111 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: if2_to_ets.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the if2_to_ets function. + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef IF2_TO_ETS_H +#define IF2_TO_ETS_H + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "frame_type_3gpp.h" +#include "typedef.h" +#include "get_const_tbls.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + void if2_to_ets(enum Frame_Type_3GPP frame_type_3gpp, + UWord8 *if2_input_ptr, + Word16 *ets_output_ptr, + CommonAmrTbls* common_amr_tbls); + + + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/int_lsf.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/int_lsf.cpp new file mode 100644 index 0000000..e50eb6c --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/int_lsf.cpp @@ -0,0 +1,270 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: int_lsf.cpp + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "int_lsf.h" +#include "typedef.h" +#include "basic_op.h" +#include "cnst.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL STORE/BUFFER/POINTER DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Int_lsf +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + lsf_old = LSF vector at the 4th SF of past frame (Word16) + lsf_new = LSF vector at the 4th SF of present frame (Word16) + i_subfr = Current subframe (equal to 0,40,80 or 120) (Word16) + lsf_out = interpolated LSF parameters for current subframe (Word16) + + Outputs: + lsf_out = new interpolated LSF parameters for current subframe + pOverflow = pointer of type Flag * to overflow indicator. + + Returns: + None. + + Global Variables Used: + None. + + Local Variables Needed: + None. + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function interpolates the LSFs for selected subframe. + The 20 ms speech frame is divided into 4 subframes. The LSFs are + interpolated at the 1st, 2nd and 3rd subframe and only forwarded + at the 4th subframe. + + |------|------|------|------| + sf1 sf2 sf3 sf4 + F0 F1 + + sf1: 3/4 F0 + 1/4 F1 sf3: 1/4 F0 + 3/4 F1 + sf2: 1/2 F0 + 1/2 F1 sf4: F1 + +------------------------------------------------------------------------------ + REQUIREMENTS + + None. + +------------------------------------------------------------------------------ + REFERENCES + + int_lsf.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void Int_lsf( + Word16 lsf_old[], // i : LSF vector at the 4th SF of past frame + Word16 lsf_new[], // i : LSF vector at the 4th SF of present frame + Word16 i_subfr, // i : Pointer to current sf (equal to 0,40,80 or 120) + Word16 lsf_out[] // o : interpolated LSF parameters for current sf +) +{ + Word16 i; + + if ( i_subfr == 0 ) + { + for (i = 0; i < M; i++) { + lsf_out[i] = add(sub(lsf_old[i], shr(lsf_old[i], 2)), + shr(lsf_new[i], 2)); + } + } + else if ( sub(i_subfr, 40) == 0 ) + { + for (i = 0; i < M; i++) { + lsf_out[i] = add(shr(lsf_old[i],1), shr(lsf_new[i], 1) ); + } + } + else if ( sub(i_subfr, 80) == 0 ) + { + for (i = 0; i < M; i++) { + lsf_out[i] = add(shr(lsf_old[i], 2), + sub(lsf_new[i], shr(lsf_new[i], 2))); + } + } + else if ( sub(i_subfr, 120) == 0 ) + { + for (i = 0; i < M; i++) { + lsf_out[i] = lsf_new[i]; + } + } + + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void Int_lsf( + Word16 lsf_old[], /* i : LSF vector at the 4th SF of past frame */ + Word16 lsf_new[], /* i : LSF vector at the 4th SF of present frame */ + Word16 i_subfr, /* i : Current sf (equal to 0,40,80 or 120) */ + Word16 lsf_out[], /* o : interpolated LSF parameters for current sf */ + Flag *pOverflow /* o : flag set if overflow occurs */ +) +{ + register Word16 i; + register Word16 temp1; + register Word16 temp2; + + if (i_subfr == 0) + { + for (i = M - 1; i >= 0; i--) + { + if (*(lsf_old + i) < 0) + { + temp1 = ~(~(*(lsf_old + i)) >> 2); + } + else + { + temp1 = *(lsf_old + i) >> 2; + } + if (*(lsf_new + i) < 0) + { + temp2 = ~(~(*(lsf_new + i)) >> 2); + } + else + { + temp2 = *(lsf_new + i) >> 2; + } + *(lsf_out + i) = add_16((Word16)(*(lsf_old + i) - temp1), + (Word16)temp2, + pOverflow); + } + } + + else if (i_subfr == 40) + { + for (i = M - 1; i >= 0; i--) + { + if (*(lsf_old + i) < 0) + { + temp1 = ~(~(*(lsf_old + i)) >> 1); + } + else + { + temp1 = *(lsf_old + i) >> 1; + } + if (*(lsf_new + i) < 0) + { + temp2 = ~(~(*(lsf_new + i)) >> 1); + } + else + { + temp2 = *(lsf_new + i) >> 1; + } + *(lsf_out + i) = temp1 + temp2; + } + } + + else if (i_subfr == 80) + { + for (i = M - 1; i >= 0; i--) + { + if (*(lsf_old + i) < 0) + { + temp1 = ~(~(*(lsf_old + i)) >> 2); + } + else + { + temp1 = *(lsf_old + i) >> 2; + } + if (*(lsf_new + i) < 0) + { + temp2 = ~(~(*(lsf_new + i)) >> 2); + } + else + { + temp2 = *(lsf_new + i) >> 2; + } + *(lsf_out + i) = add_16((Word16)temp1, + (Word16)(*(lsf_new + i) - temp2), + pOverflow); + + } + } + + else if (i_subfr == 120) + { + for (i = M - 1; i >= 0; i--) + { + *(lsf_out + i) = *(lsf_new + i); + } + } + + return; +} + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/lsp_avg.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/lsp_avg.cpp new file mode 100644 index 0000000..2850d6e --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/lsp_avg.cpp @@ -0,0 +1,250 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: lsp_avg.cpp + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + LSP averaging and history +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "lsp_avg.h" +#include "basic_op.h" +#include "oper_32b.h" +#include "oscl_mem.h" +#include "q_plsf_5_tbl.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL FUNCTION REFERENCES +; Declare functions defined elsewhere and referenced in this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL VARIABLES REFERENCES +; Declare variables used in this module but defined elsewhere +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: lsp_avg_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to structure of type lsp_avgState + + Outputs: + fields of the structure pointed to by state are initialized. + + Returns: + return_value = 0, if reset was successful; -1, otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + +lsp_avg.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int lsp_avg_reset (lsp_avgState *st) +{ + if (st == (lsp_avgState *) NULL){ + // fprintf(stderr, "lsp_avg_reset: invalid parameter\n"); + return -1; + } + + Copy(mean_lsf, &st->lsp_meanSave[0], M); + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 lsp_avg_reset(lsp_avgState *st, const Word16* mean_lsf_5_ptr) +{ + if (st == (lsp_avgState *) NULL) + { + /* fprintf(stderr, "lsp_avg_reset: invalid parameter\n"); */ + return -1; + } + + oscl_memmove((void *)&st->lsp_meanSave[0], mean_lsf_5_ptr, M*sizeof(*mean_lsf_5_ptr)); + + return 0; +} + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: lsp_avg +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to structure of type lsp_avgState + lsp = pointer to Word16, which reflects the state of the state machine + + Outputs: + st = pointer to structure of type lsp_avgState + pOverflow = pointer to type Flag -- overflow indicator + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + +lsp_avg.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +void lsp_avg ( + lsp_avgState *st, // i/o : State struct Q15 + Word16 *lsp // i : state of the state machine Q15 +) +{ + Word16 i; + Word32 L_tmp; // Q31 + + for (i = 0; i < M; i++) { + + // mean = 0.84*mean + L_tmp = L_deposit_h(st->lsp_meanSave[i]); + L_tmp = L_msu(L_tmp, EXPCONST, st->lsp_meanSave[i]); + + // Add 0.16 of newest LSPs to mean + L_tmp = L_mac(L_tmp, EXPCONST, lsp[i]); + + // Save means + st->lsp_meanSave[i] = pv_round(L_tmp); // Q15 + } + + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void lsp_avg( + lsp_avgState *st, /* i/o : State struct Q15 */ + Word16 *lsp, /* i : state of the state machine Q15 */ + Flag *pOverflow /* o : Flag set when overflow occurs */ +) +{ + Word16 i; + Word32 L_tmp; /* Q31 */ + + for (i = 0; i < M; i++) + { + + /* mean = 0.84*mean */ + L_tmp = ((Word32)st->lsp_meanSave[i] << 16); + L_tmp = L_msu(L_tmp, EXPCONST, st->lsp_meanSave[i], pOverflow); + + /* Add 0.16 of newest LSPs to mean */ + L_tmp = L_mac(L_tmp, EXPCONST, lsp[i], pOverflow); + + /* Save means */ + st->lsp_meanSave[i] = pv_round(L_tmp, pOverflow); /* Q15 */ + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/lsp_avg.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/lsp_avg.h new file mode 100644 index 0000000..4b45b96 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/lsp_avg.h @@ -0,0 +1,120 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: lsp_avg.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the lsp_avg.c + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef lsp_avg_h +#define lsp_avg_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "cnst.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ +#define EXPCONST 5243 /* 0.16 in Q15 */ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + Word16 lsp_meanSave[M]; /* Averaged LSPs saved for efficiency */ + } lsp_avgState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + Word16 lsp_avg_reset( + lsp_avgState *state, + const Word16* mean_lsf_5_ptr + ); + + + void lsp_avg( + lsp_avgState *st, /* i/o : State struct Q15 */ + Word16 *lsp, /* i : LSP vector Q15 */ + Flag *pOverflow /* o : Flag set when overflow occurs */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _LSP_LSF_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ph_disp.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ph_disp.cpp new file mode 100644 index 0000000..30631f4 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ph_disp.cpp @@ -0,0 +1,796 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: ph_disp.cpp + Functions: + ph_disp_reset + ph_disp_lock + ph_disp_release + ph_disp + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + This file contains the function that performs adaptive phase dispersion of + the excitation signal. The phase dispersion initialization, reset, and + exit functions are included in this file, as well as, the phase dispersion + lock and release functions. + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "ph_disp.h" +#include "typedef.h" +#include "basic_op.h" +#include "cnst.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ph_disp_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a structure of type ph_dispState + + Outputs: + Structure pointed to by state is initialized to zeros + + Returns: + return_value = 0, if reset was successful; -1, otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function resets the variables used by the phase dispersion function. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + ph_disp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int ph_disp_reset (ph_dispState *state) +{ + Word16 i; + + if (state == (ph_dispState *) NULL){ + fprint(stderr, "ph_disp_reset: invalid parameter\n"); + return -1; + } + for (i=0; i<PHDGAINMEMSIZE; i++) + { + state->gainMem[i] = 0; + } + state->prevState = 0; + state->prevCbGain = 0; + state->lockFull = 0; + state->onset = 0; // assume no onset in start + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 ph_disp_reset(ph_dispState *state) +{ + register Word16 i; + + if (state == (ph_dispState *) NULL) + { + /* fprint(stderr, "ph_disp_reset: invalid parameter\n"); */ + return(-1); + } + for (i = 0; i < PHDGAINMEMSIZE; i++) + { + state->gainMem[i] = 0; + } + state->prevState = 0; + state->prevCbGain = 0; + state->lockFull = 0; + state->onset = 0; /* assume no onset in start */ + + return(0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ph_disp_lock +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a structure of type ph_dispState + + Outputs: + lockFull field of the structure pointed to by state is set to 1 + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function sets the lockFull flag to indicate a lock condition. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + ph_disp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void ph_disp_lock (ph_dispState *state) +{ + state->lockFull = 1; + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void ph_disp_lock(ph_dispState *state) +{ + state->lockFull = 1; + + return; +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ph_disp_release +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a structure of type ph_dispState + + Outputs: + lockFull field of the structure pointed to by state is set to 0 + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function clears the lockFull flag to indicate an unlocked state. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + ph_disp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void ph_disp_release (ph_dispState *state) +{ + state->lockFull = 0; + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void ph_disp_release(ph_dispState *state) +{ + state->lockFull = 0; + + return; +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: ph_disp +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a structure of type ph_dispState + mode = codec mode (enum Mode) + x = LTP excitation signal buffer (Word16) + cbGain = codebook gain (Word16) + ltpGain = LTP gain (Word16) + inno = innovation buffer (Word16) + pitch_fac = pitch factor used to scale the LTP excitation (Word16) + tmp_shift = shift factor applied to sum of scaled LTP excitation and + innovation before rounding (Word16) + pOverflow = pointer to overflow indicator (Flag) + + Outputs: + structure pointed to by state contains the updated gainMem array, + prevState, prevCbGain, and onset fields + x buffer contains the new excitation signal + inno buffer contains the new innovation signal + pOverflow -> 1 if there is overflow + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function performs adaptive phase dispersion, i.e., forming of total + excitation for the synthesis part of the decoder. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + ph_disp.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void ph_disp ( + ph_dispState *state, // i/o : State struct + enum Mode mode, // i : codec mode + Word16 x[], // i/o Q0 : in: LTP excitation signal + // out: total excitation signal + Word16 cbGain, // i Q1 : Codebook gain + Word16 ltpGain, // i Q14 : LTP gain + Word16 inno[], // i/o Q13 : Innovation vector (Q12 for 12.2) + Word16 pitch_fac, // i Q14 : pitch factor used to scale the + LTP excitation (Q13 for 12.2) + Word16 tmp_shift // i Q0 : shift factor applied to sum of + scaled LTP ex & innov. before + rounding +) +{ + Word16 i, i1; + Word16 tmp1; + Word32 L_temp; + Word16 impNr; // indicator for amount of disp./filter used + + Word16 inno_sav[L_SUBFR]; + Word16 ps_poss[L_SUBFR]; + Word16 j, nze, nPulse, ppos; + const Word16 *ph_imp; // Pointer to phase dispersion filter + + // Update LTP gain memory + for (i = PHDGAINMEMSIZE-1; i > 0; i--) + { + state->gainMem[i] = state->gainMem[i-1]; + } + state->gainMem[0] = ltpGain; + + // basic adaption of phase dispersion + if (sub(ltpGain, PHDTHR2LTP) < 0) { // if (ltpGain < 0.9) + if (sub(ltpGain, PHDTHR1LTP) > 0) + { // if (ltpGain > 0.6 + impNr = 1; // medium dispersion + } + else + { + impNr = 0; // maximum dispersion + } + } + else + { + impNr = 2; // no dispersion + } + + // onset indicator + // onset = (cbGain > onFact * cbGainMem[0]) + tmp1 = pv_round(L_shl(L_mult(state->prevCbGain, ONFACTPLUS1), 2)); + if (sub(cbGain, tmp1) > 0) + { + state->onset = ONLENGTH; + } + else + { + if (state->onset > 0) + { + state->onset = sub (state->onset, 1); + } + } + + // if not onset, check ltpGain buffer and use max phase dispersion if + half or more of the ltpGain-parameters say so + if (state->onset == 0) + { + // Check LTP gain memory and set filter accordingly + i1 = 0; + for (i = 0; i < PHDGAINMEMSIZE; i++) + { + if (sub(state->gainMem[i], PHDTHR1LTP) < 0) + { + i1 = add (i1, 1); + } + } + if (sub(i1, 2) > 0) + { + impNr = 0; + } + + } + // Restrict decrease in phase dispersion to one step if not onset + if ((sub(impNr, add(state->prevState, 1)) > 0) && (state->onset == 0)) + { + impNr = sub (impNr, 1); + } + // if onset, use one step less phase dispersion + if((sub(impNr, 2) < 0) && (state->onset > 0)) + { + impNr = add (impNr, 1); + } + + // disable for very low levels + if(sub(cbGain, 10) < 0) + { + impNr = 2; + } + + if(sub(state->lockFull, 1) == 0) + { + impNr = 0; + } + + // update static memory + state->prevState = impNr; + state->prevCbGain = cbGain; + + // do phase dispersion for all modes but 12.2 and 7.4; + // don't modify the innovation if impNr >=2 (= no phase disp) + if (sub(mode, MR122) != 0 && + sub(mode, MR102) != 0 && + sub(mode, MR74) != 0 && + sub(impNr, 2) < 0) + { + // track pulse positions, save innovation, + and initialize new innovation + nze = 0; + for (i = 0; i < L_SUBFR; i++) + { + if (inno[i] != 0) + { + ps_poss[nze] = i; + nze = add (nze, 1); + } + inno_sav[i] = inno[i]; + inno[i] = 0; + } + // Choose filter corresponding to codec mode and dispersion criterium + if (sub (mode, MR795) == 0) + { + if (impNr == 0) + { + ph_imp = ph_imp_low_MR795; + } + else + { + ph_imp = ph_imp_mid_MR795; + } + } + else + { + if (impNr == 0) + { + ph_imp = ph_imp_low; + } + else + { + ph_imp = ph_imp_mid; + } + } + + // Do phase dispersion of innovation + for (nPulse = 0; nPulse < nze; nPulse++) + { + ppos = ps_poss[nPulse]; + + // circular convolution with impulse response + j = 0; + for (i = ppos; i < L_SUBFR; i++) + { + // inno[i1] += inno_sav[ppos] * ph_imp[i1-ppos] + tmp1 = mult(inno_sav[ppos], ph_imp[j++]); + inno[i] = add(inno[i], tmp1); + } + + for (i = 0; i < ppos; i++) + { + // inno[i] += inno_sav[ppos] * ph_imp[L_SUBFR-ppos+i] + tmp1 = mult(inno_sav[ppos], ph_imp[j++]); + inno[i] = add(inno[i], tmp1); + } + } + } + + // compute total excitation for synthesis part of decoder + // (using modified innovation if phase dispersion is active) + for (i = 0; i < L_SUBFR; i++) + { + // x[i] = gain_pit*x[i] + cbGain*code[i]; + L_temp = L_mult ( x[i], pitch_fac); + // 12.2: Q0 * Q13 + // 7.4: Q0 * Q14 + L_temp = L_mac (L_temp, inno[i], cbGain); + // 12.2: Q12 * Q1 + // 7.4: Q13 * Q1 + L_temp = L_shl (L_temp, tmp_shift); // Q16 + x[i] = pv_round (L_temp); + } + + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void ph_disp( + ph_dispState *state, /* i/o : State struct */ + enum Mode mode, /* i : codec mode */ + Word16 x[], /* i/o Q0 : in: LTP excitation signal */ + /* out: total excitation signal */ + Word16 cbGain, /* i Q1 : Codebook gain */ + Word16 ltpGain, /* i Q14 : LTP gain */ + Word16 inno[], /* i/o Q13 : Innovation vector (Q12 for 12.2) */ + Word16 pitch_fac, /* i Q14 : pitch factor used to scale the + LTP excitation (Q13 for 12.2) */ + Word16 tmp_shift, /* i Q0 : shift factor applied to sum of + scaled LTP ex & innov. before + rounding */ + CommonAmrTbls* common_amr_tbls, /* i : ptr to struct of table ptrs */ + Flag *pOverflow /* i/o : oveflow indicator */ +) +{ + register Word16 i, i1; + register Word16 tmp1; + Word32 L_temp; + Word32 L_temp2; + Word16 impNr; /* indicator for amount of disp./filter used */ + + Word16 inno_sav[L_SUBFR]; + Word16 ps_poss[L_SUBFR]; + register Word16 nze, nPulse; + Word16 ppos; + const Word16 *ph_imp; /* Pointer to phase dispersion filter */ + + Word16 *p_inno; + Word16 *p_inno_sav; + Word16 *p_x; + const Word16 *p_ph_imp; + Word16 c_inno_sav; + + const Word16* ph_imp_low_MR795_ptr = common_amr_tbls->ph_imp_low_MR795_ptr; + const Word16* ph_imp_mid_MR795_ptr = common_amr_tbls->ph_imp_mid_MR795_ptr; + const Word16* ph_imp_low_ptr = common_amr_tbls->ph_imp_low_ptr; + const Word16* ph_imp_mid_ptr = common_amr_tbls->ph_imp_mid_ptr; + + /* Update LTP gain memory */ + /* Unrolled FOR loop below since PHDGAINMEMSIZE is assumed to stay */ + /* the same. */ + /* for (i = PHDGAINMEMSIZE-1; i > 0; i--) */ + /* { */ + /* state->gainMem[i] = state->gainMem[i-1]; */ + /* } */ + state->gainMem[4] = state->gainMem[3]; + state->gainMem[3] = state->gainMem[2]; + state->gainMem[2] = state->gainMem[1]; + state->gainMem[1] = state->gainMem[0]; + state->gainMem[0] = ltpGain; + + /* basic adaption of phase dispersion */ + + if (ltpGain < PHDTHR2LTP) /* if (ltpGain < 0.9) */ + { + if (ltpGain > PHDTHR1LTP) + { /* if (ltpGain > 0.6 */ + impNr = 1; /* medium dispersion */ + } + else + { + impNr = 0; /* maximum dispersion */ + } + } + else + { + impNr = 2; /* no dispersion */ + } + + /* onset indicator */ + /* onset = (cbGain > onFact * cbGainMem[0]) */ + + L_temp = ((Word32) state->prevCbGain * ONFACTPLUS1) << 1; + + /* (L_temp << 2) calculation with saturation check */ + if (L_temp > (Word32) 0X1fffffffL) + { + *pOverflow = 1; + L_temp = MAX_32; + } + else if (L_temp < (Word32) 0xe0000000L) + { + *pOverflow = 1; + L_temp = MIN_32; + } + else + { + L_temp <<= 2; + } + + tmp1 = pv_round(L_temp, pOverflow); + + if (cbGain > tmp1) + { + state->onset = ONLENGTH; + } + else + { + + if (state->onset > 0) + { + state->onset -= 1; + } + } + + /* if not onset, check ltpGain buffer and use max phase dispersion if + half or more of the ltpGain-parameters say so */ + if (state->onset == 0) + { + /* Check LTP gain memory and set filter accordingly */ + i1 = 0; + for (i = 0; i < PHDGAINMEMSIZE; i++) + { + if (state->gainMem[i] < PHDTHR1LTP) + { + i1 += 1; + } + } + + if (i1 > 2) + { + impNr = 0; + } + } + /* Restrict decrease in phase dispersion to one step if not onset */ + if ((impNr > ((state->prevState) + 1)) && (state->onset == 0)) + { + impNr -= 1; + } + + /* if onset, use one step less phase dispersion */ + if ((impNr < 2) && (state->onset > 0)) + { + impNr += 1; + } + + /* disable for very low levels */ + if (cbGain < 10) + { + impNr = 2; + } + + if (state->lockFull == 1) + { + impNr = 0; + } + + /* update static memory */ + state->prevState = impNr; + state->prevCbGain = cbGain; + + /* do phase dispersion for all modes but 12.2 and 7.4; + don't modify the innovation if impNr >=2 (= no phase disp) */ + if ((mode != MR122) && (mode != MR102) && (mode != MR74) && (impNr < 2)) + { + /* track pulse positions, save innovation, + and initialize new innovation */ + nze = 0; + p_inno = &inno[0]; + p_inno_sav = &inno_sav[0]; + + for (i = 0; i < L_SUBFR; i++) + { + if (*(p_inno) != 0) + { + ps_poss[nze] = i; + nze += 1; + } + *(p_inno_sav++) = *(p_inno); + *(p_inno++) = 0; + } + + /* Choose filter corresponding to codec mode and dispersion criterium */ + if (mode == MR795) + { + if (impNr == 0) + { + ph_imp = ph_imp_low_MR795_ptr; + } + else + { + ph_imp = ph_imp_mid_MR795_ptr; + } + } + else + { + if (impNr == 0) + { + ph_imp = ph_imp_low_ptr; + } + else + { + ph_imp = ph_imp_mid_ptr; + } + } + + /* Do phase dispersion of innovation */ + for (nPulse = 0; nPulse < nze; nPulse++) + { + ppos = ps_poss[nPulse]; + + /* circular convolution with impulse response */ + c_inno_sav = inno_sav[ppos]; + p_inno = &inno[ppos]; + p_ph_imp = ph_imp; + + for (i = ppos; i < L_SUBFR; i++) + { + /* inno[i1] += inno_sav[ppos] * ph_imp[i1-ppos] */ + L_temp = ((Word32) c_inno_sav * *(p_ph_imp++)) >> 15; + tmp1 = (Word16) L_temp; + *(p_inno) = add_16(*(p_inno), tmp1, pOverflow); + p_inno += 1; + } + + p_inno = &inno[0]; + + for (i = 0; i < ppos; i++) + { + /* inno[i] += inno_sav[ppos] * ph_imp[L_SUBFR-ppos+i] */ + L_temp = ((Word32) c_inno_sav * *(p_ph_imp++)) >> 15; + tmp1 = (Word16) L_temp; + *(p_inno) = add_16(*(p_inno), tmp1, pOverflow); + p_inno += 1; + } + } + } + + /* compute total excitation for synthesis part of decoder + (using modified innovation if phase dispersion is active) */ + p_inno = &inno[0]; + p_x = &x[0]; + + for (i = 0; i < L_SUBFR; i++) + { + /* x[i] = gain_pit*x[i] + cbGain*code[i]; */ + L_temp = L_mult(x[i], pitch_fac, pOverflow); + /* 12.2: Q0 * Q13 */ + /* 7.4: Q0 * Q14 */ + L_temp2 = ((Word32) * (p_inno++) * cbGain) << 1; + L_temp = L_add(L_temp, L_temp2, pOverflow); + /* 12.2: Q12 * Q1 */ + /* 7.4: Q13 * Q1 */ + L_temp = L_shl(L_temp, tmp_shift, pOverflow); /* Q16 */ + *(p_x++) = pv_round(L_temp, pOverflow); + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ph_disp.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ph_disp.h new file mode 100644 index 0000000..e48aea0 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/ph_disp.h @@ -0,0 +1,173 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: ph_disp.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the Phase dispersion of excitation signal ph_disp() function. + +------------------------------------------------------------------------------ +*/ + +#ifndef PH_DISP_H +#define PH_DISP_H "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "mode.h" +#include "get_const_tbls.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ +#define PHDGAINMEMSIZE 5 +#define PHDTHR1LTP 9830 /* 0.6 in Q14 */ +#define PHDTHR2LTP 14746 /* 0.9 in Q14 */ +#define ONFACTPLUS1 16384 /* 2.0 in Q13 */ +#define ONLENGTH 2 + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + Word16 gainMem[PHDGAINMEMSIZE]; + Word16 prevState; + Word16 prevCbGain; + Word16 lockFull; + Word16 onset; + } ph_dispState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; + ; Function: ph_disp_reset + ; Purpose: Initializes state memory + ; + ----------------------------------------------------------------------------*/ + Word16 ph_disp_reset(ph_dispState *state); + + /*---------------------------------------------------------------------------- + ; + ; Function: ph_disp_exit + ; Purpose: The memory used for state memory is freed + ; + ----------------------------------------------------------------------------*/ + void ph_disp_exit(ph_dispState **state); + + /*---------------------------------------------------------------------------- + ; + ; Function: ph_disp_lock + ; Purpose: mark phase dispersion as locked in state struct + ; + ----------------------------------------------------------------------------*/ + void ph_disp_lock(ph_dispState *state); + + /*---------------------------------------------------------------------------- + ; + ; Function: ph_disp_release + ; Purpose: mark phase dispersion as unlocked in state struct + ; + ----------------------------------------------------------------------------*/ + + void ph_disp_release(ph_dispState *state); + + /*---------------------------------------------------------------------------- + ; + ; Function: ph_disp + ; Purpose: perform phase dispersion according to the specified codec + ; mode and computes total excitation for synthesis part + ; if decoder + ; + ----------------------------------------------------------------------------*/ + + void ph_disp( + ph_dispState *state, /* i/o : State struct */ + enum Mode mode, /* i : codec mode */ + Word16 x[], /* i/o Q0 : in: LTP excitation signal */ + /* out: total excitation signal */ + Word16 cbGain, /* i Q1 : Codebook gain */ + Word16 ltpGain, /* i Q14 : LTP gain */ + Word16 inno[], /* i/o Q13 : Innovation vector (Q12 for 12.2) */ + Word16 pitch_fac, /* i Q14 : pitch factor used to scale the + LTP excitation (Q13 for 12.2) */ + Word16 tmp_shift, /* i Q0 : shift factor applied to sum of + scaled LTP ex & innov. before + rounding */ + CommonAmrTbls* common_amr_tbls, /* i : ptr to struct of table ptrs */ + Flag *pOverflow /* i/o : oveflow indicator */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* _PH_DISP_H_ */ + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/post_pro.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/post_pro.cpp new file mode 100644 index 0000000..7bf5021 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/post_pro.cpp @@ -0,0 +1,315 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: post_pro.cpp + Functions: + Post_Process_reset + Post_Process + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + This file contains the function that performs post-processing on the output + speech. Post-processing include filtering the output speech through a second + order high pass filter with cutoff frequency of 60 Hz, and up-scaling the + output speech by a factor of 2. In addition to the post-processing function + itself, a post-processing initialization function, post-processing reset + function, and post-processing exit function are also included in this file. + +------------------------------------------------------------------------------ +*/ + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "post_pro.h" +#include "typedef.h" +#include "basic_op.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* filter coefficients (fc = 60 Hz) */ +static const Word16 b[3] = {7699, -15398, 7699}; +static const Word16 a[3] = {8192, 15836, -7667}; + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Post_Process_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to a structure of type Post_ProcessState + + Outputs: + structure pointed to by state will have all its fields initialized + to zero + + Returns: + return_value = 0, if reset was successful; -1, otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function initializes state memory to zero. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + post_pro.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int Post_Process_reset (Post_ProcessState *state) +{ + if (state == (Post_ProcessState *) NULL){ + fprint(stderr, "Post_Process_reset: invalid parameter\n"); + return -1; + } + + state->y2_hi = 0; + state->y2_lo = 0; + state->y1_hi = 0; + state->y1_lo = 0; + state->x0 = 0; + state->x1 = 0; + + return 0; +} +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 Post_Process_reset(Post_ProcessState *state) +{ + if (state == (Post_ProcessState *) NULL) + { + /* fprint(stderr, "Post_Process_reset: invalid parameter\n"); */ + return(-1); + } + + state->y2_hi = 0; + state->y2_lo = 0; + state->y1_hi = 0; + state->y1_lo = 0; + state->x0 = 0; + state->x1 = 0; + + return(0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Post_Process +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type Post_ProcessState + signal = buffer containing the input signal (Word16) + lg = length of the input signal (Word16) + pOverflow = pointer to overflow indicator of type Flag + + Outputs: + structure pointed to by st contains new filter input and output values + signal buffer contains the HP filtered and up-scaled input signal + pOverflow points to 1 if overflow occurs in the math functions called + else it points to 0. + + Returns: + return_value = 0 (int) + + Global Variables Used: + a = buffer containing filter coefficients + b = buffer containing filter coefficients + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function performs post-processing on the output speech signal. First, + the output speech goes through a second order high pass filter with a + cutoff frequency of 60 Hz. Then, the filtered output speech is multiplied + by a factor of 2. The algorithm implemented follows the following difference + equation: + + y[i] = b[0]*x[i]*2 + b[1]*x[i-1]*2 + b[2]*x[i-2]*2 + a[1]*y[i-1] + a[2]*y[i-2]; + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + post_pro.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int Post_Process ( + Post_ProcessState *st, //i/o : post process state + Word16 signal[], //i/o : signal + Word16 lg //i : length of signal + ) +{ + Word16 i, x2; + Word32 L_tmp; + + for (i = 0; i < lg; i++) + { + x2 = st->x1; + st->x1 = st->x0; + st->x0 = signal[i]; + + // y[i] = b[0]*x[i]*2 + b[1]*x[i-1]*2 + b140[2]*x[i-2]/2 + // + a[1]*y[i-1] + a[2] * y[i-2]; + + L_tmp = Mpy_32_16 (st->y1_hi, st->y1_lo, a[1]); + L_tmp = L_add (L_tmp, Mpy_32_16 (st->y2_hi, st->y2_lo, a[2])); + L_tmp = L_mac (L_tmp, st->x0, b[0]); + L_tmp = L_mac (L_tmp, st->x1, b[1]); + L_tmp = L_mac (L_tmp, x2, b[2]); + L_tmp = L_shl (L_tmp, 2); + + //Multiplication by two of output speech with saturation. + signal[i] = pv_round(L_shl(L_tmp, 1)); + + st->y2_hi = st->y1_hi; + st->y2_lo = st->y1_lo; + L_Extract (L_tmp, &st->y1_hi, &st->y1_lo); + } + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void Post_Process( + Post_ProcessState *st, /* i/o : post process state */ + Word16 signal[], /* i/o : signal */ + Word16 lg, /* i : length of signal */ + Flag *pOverflow +) +{ + Word16 i, x2; + Word32 L_tmp; + + Word16 *p_signal; + Word16 c_a1 = a[1]; + Word16 c_a2 = a[2]; + Word16 c_b0 = b[0]; + Word16 c_b1 = b[1]; + Word16 c_b2 = b[2]; + + p_signal = &signal[0]; + + for (i = 0; i < lg; i++) + { + x2 = st->x1; + st->x1 = st->x0; + st->x0 = *(p_signal); + + /* y[i] = b[0]*x[i]*2 + b[1]*x[i-1]*2 + b140[2]*x[i-2]/2 */ + /* + a[1]*y[i-1] + a[2] * y[i-2]; */ + + L_tmp = ((Word32) st->y1_hi) * c_a1; + L_tmp += (((Word32) st->y1_lo) * c_a1) >> 15; + L_tmp += ((Word32) st->y2_hi) * c_a2; + L_tmp += (((Word32) st->y2_lo) * c_a2) >> 15; + L_tmp += ((Word32) st->x0) * c_b0; + L_tmp += ((Word32) st->x1) * c_b1; + L_tmp += ((Word32) x2) * c_b2; + L_tmp <<= 3; + + + /* Multiplication by two of output speech with saturation. */ + + *(p_signal++) = pv_round(L_shl(L_tmp, 1, pOverflow), pOverflow); + + st->y2_hi = st->y1_hi; + st->y2_lo = st->y1_lo; + + st->y1_hi = (Word16)(L_tmp >> 16); + st->y1_lo = (Word16)((L_tmp >> 1) - ((Word32) st->y1_hi << 15)); + + } + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/post_pro.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/post_pro.h new file mode 100644 index 0000000..820b79a --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/post_pro.h @@ -0,0 +1,129 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: post_pro.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : post_pro.h + Purpose : Postprocessing of output speech. + + - 2nd order high pass filtering with cut + off frequency at 60 Hz. + - Multiplication of output by two. +------------------------------------------------------------------------------ +*/ + +#ifndef _POST_PRO_H_ +#define _POST_PRO_H_ +#define post_pro_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; [Define module specific macros here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; [Include all pre-processor statements here.] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; [Declare variables used in this module but defined elsewhere] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + Word16 y2_hi; + Word16 y2_lo; + Word16 y1_hi; + Word16 y1_lo; + Word16 x0; + Word16 x1; + } Post_ProcessState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; [List function prototypes here] + ----------------------------------------------------------------------------*/ + + Word16 Post_Process_reset(Post_ProcessState *st); + /* reset of Post processing state (i.e. set state memory to zero) + returns 0 on success + */ + void Post_Process_exit(Post_ProcessState **st); + /* de-initialize Post processing state (i.e. free status struct) + stores NULL in *st + */ + + void Post_Process( + Post_ProcessState *st, /* i/o : post process state */ + Word16 signal[], /* i/o : signal */ + Word16 lg, /* i : lenght of signal */ + Flag *pOverflow + ); + +#ifdef __cplusplus +} +#endif + +#endif /* _POST_PRO_H_ */ + + + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/preemph.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/preemph.cpp new file mode 100644 index 0000000..5f8d0e3 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/preemph.cpp @@ -0,0 +1,222 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: preemph.cpp + Functions: + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + Purpose : Preemphasis filtering + Description : Filtering through 1 - g z^-1 + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "preemph.h" +#include "typedef.h" +#include "basic_op.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: preemphasis_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st -- double pointer to preemphasisState + + Outputs: + st -- double ponter to preemphasisState + + Returns: + -1 if an error occurs + 0 if OK + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Initializes state memory to zero +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + preemph.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 preemphasis_reset(preemphasisState *state) +{ + if (state == (preemphasisState *) NULL) + { + /* fprintf(stderr, "preemphasis_reset: invalid parameter\n"); */ + return -1; + } + + state->mem_pre = 0; + + return 0; +} + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: preemphasis +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st -- Pointer to preemphasisState -- preemphasis filter state + signal -- array of type Word16 -- input signal overwritten by the output + g -- Word16 -- preemphasis coefficient + L -- Word16 -- size of filtering + + Outputs: + st -- Pointer to preemphasisState -- preemphasis filter state + signal -- array of type Word16 -- input signal overwritten by the output + pOverflow -- pointer to type Flag -- overflow indicator + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Filtering through 1 - g z^-1 +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + preemph.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + + +void preemphasis( + preemphasisState *st, /* (i/o) : preemphasis filter state */ + Word16 *signal, /* (i/o) : input signal overwritten by the output */ + Word16 g, /* (i) : preemphasis coefficient */ + Word16 L, /* (i) : size of filtering */ + Flag *pOverflow /* (o) : overflow indicator */ +) +{ + Word16 *p1; + Word16 *p2; + Word16 temp; + Word16 temp2; + Word16 i; + + p1 = signal + L - 1; + p2 = p1 - 1; + temp = *p1; + + for (i = 0; i <= L - 2; i++) + { + temp2 = mult(g, *(p2--), pOverflow); + *p1 = sub(*p1, temp2, pOverflow); + + p1--; + } + + temp2 = mult(g, st->mem_pre, pOverflow); + + *p1 = sub(*p1, temp2, pOverflow); + + st->mem_pre = temp; + + return; +} + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/preemph.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/preemph.h new file mode 100644 index 0000000..0798731 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/preemph.h @@ -0,0 +1,124 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + Filename: preemph.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + This file contains all the constant definitions and prototype definitions + needed by the file, preemph.c + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; CONTINUE ONLY IF NOT ALREADY DEFINED +----------------------------------------------------------------------------*/ +#ifndef preemph_h +#define preemph_h "$Id $" + + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ + +#include "typedef.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; Define module specific macros here + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; Include all pre-processor statements here. + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; Declare variables used in this module but defined elsewhere + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + Word16 mem_pre; /* filter state */ + } preemphasisState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; Function Prototype declaration + ----------------------------------------------------------------------------*/ + + Word16 preemphasis_reset(preemphasisState *st); + /* reset of preemphasis filter (i.e. set state memory to zero) + returns 0 on success + */ + void preemphasis_exit(preemphasisState **st); + /* de-initialize preemphasis filter (i.e. free status struct) + stores NULL in *st + */ + + void preemphasis( + preemphasisState *st, /* (i/o): preemphasis filter state */ + Word16 *signal, /* (i/o): input signal overwritten by the output */ + Word16 g, /* (i) : preemphasis coefficient */ + Word16 L, /* (i) : size of filtering */ + Flag *pOverflow /* (o) : overflow indicator */ + ); + + /*---------------------------------------------------------------------------- + ; END + ----------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +#endif /* preemph_H_ */ + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pstfilt.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pstfilt.cpp new file mode 100644 index 0000000..479ded7 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pstfilt.cpp @@ -0,0 +1,506 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: pstfilt.cpp + Functions: + Post_Filter_reset + Post_Filter + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + This file contains the function that performs adaptive post-filtering on the + synthesized speech. It also contains the functions that initialize, reset, + and exit the post-filtering function. + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "pstfilt.h" +#include "typedef.h" +#include "mode.h" +#include "basicop_malloc.h" +#include "basic_op.h" +#include "weight_a.h" +#include "residu.h" +#include "syn_filt.h" +#include "preemph.h" +#include "cnst.h" +#include "oscl_mem.h" + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ +#define L_H 22 /* size of truncated impulse response of A(z/g1)/A(z/g2) */ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* Spectral expansion factors */ +static const Word16 gamma3_MR122[M] = +{ + 22938, 16057, 11240, 7868, 5508, + 3856, 2699, 1889, 1322, 925 +}; + +static const Word16 gamma3[M] = +{ + 18022, 9912, 5451, 2998, 1649, 907, 499, 274, 151, 83 +}; + +static const Word16 gamma4_MR122[M] = +{ + 24576, 18432, 13824, 10368, 7776, + 5832, 4374, 3281, 2461, 1846 +}; + +static const Word16 gamma4[M] = +{ + 22938, 16057, 11240, 7868, 5508, 3856, 2699, 1889, 1322, 925 +}; + +/*---------------------------------------------------------------------------- +; EXTERNAL FUNCTION REFERENCES +; Declare functions defined elsewhere and referenced in this module +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; EXTERNAL VARIABLES REFERENCES +; Declare variables used in this module but defined elsewhere +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Post_Filter_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to structure of type Post_FilterState + + Outputs: + fields of the structure pointed to by state is initialized to zero + + Returns: + return_value = 0, if reset was successful; -1, otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function initializes the state memory used by the Post_Filter function + to zero. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + pstfilt.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int Post_Filter_reset (Post_FilterState *state) +{ + if (state == (Post_FilterState *) NULL){ + fprintf(stderr, "Post_Filter_reset: invalid parameter\n"); + return -1; + } + + Set_zero (state->mem_syn_pst, M); + Set_zero (state->res2, L_SUBFR); + Set_zero (state->synth_buf, L_FRAME + M); + agc_reset(state->agc_state); + preemphasis_reset(state->preemph_state); + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 Post_Filter_reset(Post_FilterState *state) +{ + if (state == (Post_FilterState *) NULL) + { + /*fprintf(stderr, "Post_Filter_reset: invalid parameter\n"); */ + return(-1); + } + + oscl_memset(state->mem_syn_pst, 0, sizeof(Word16)*M); + oscl_memset(state->res2, 0, sizeof(Word16)*L_SUBFR); + oscl_memset(state->synth_buf, 0, sizeof(Word16)*(L_FRAME + M)); + agc_reset(&(state->agc_state)); + preemphasis_reset(&(state->preemph_state)); + + return(0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Post_Filter +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to a structure of type Post_FilterState + mode = AMR mode + syn = pointer to a buffer containing synthesized speech; upon + exiting this function, it will contain the post-filtered + synthesized speech + Az_4 = pointer to the interpolated LPC parameters for all subframes + pOverflow = pointer to overflow indicator of type Flag + + Outputs: + fields of the structure pointed to by st contains the updated field + values + syn buffer contains the post-filtered synthesized speech + pOverflow = 1 if overflow occurrs in the math functions called else + it is zero. + + Returns: + return_value = 0 (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function performs the post-filtering on the synthesized speech. The + post-filtering process is described as follows: + (1) inverse filtering of syn[] through A(z/0.7) to get res2[] + (2) tilt compensation filtering; 1 - MU*k*z^-1 + (3) synthesis filtering through 1/A(z/0.75) + (4) adaptive gain control + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + pstfilt.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int Post_Filter ( + Post_FilterState *st, // i/o : post filter states + enum Mode mode, // i : AMR mode + Word16 *syn, // i/o : synthesis speech (postfiltered is output) + Word16 *Az_4 // i : interpolated LPC parameters in all subfr. +) +{ + *-------------------------------------------------------------------* + * Declaration of parameters * + *-------------------------------------------------------------------* + + Word16 Ap3[MP1], Ap4[MP1]; // bandwidth expanded LP parameters + Word16 *Az; // pointer to Az_4: + // LPC parameters in each subframe + Word16 i_subfr; // index for beginning of subframe + Word16 h[L_H]; + + Word16 i; + Word16 temp1, temp2; + Word32 L_tmp; + Word16 *syn_work = &st->synth_buf[M]; + + + *-----------------------------------------------------* + * Post filtering * + *-----------------------------------------------------* + + Copy (syn, syn_work , L_FRAME); + + Az = Az_4; + + for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) + { + // Find weighted filter coefficients Ap3[] and ap[4] + + if (sub(mode, MR122) == 0 || sub(mode, MR102) == 0) + { + Weight_Ai (Az, gamma3_MR122, Ap3); + Weight_Ai (Az, gamma4_MR122, Ap4); + } + else + { + Weight_Ai (Az, gamma3, Ap3); + Weight_Ai (Az, gamma4, Ap4); + } + + // filtering of synthesis speech by A(z/0.7) to find res2[] + + Residu (Ap3, &syn_work[i_subfr], st->res2, L_SUBFR); + + // tilt compensation filter + + // impulse response of A(z/0.7)/A(z/0.75) + + Copy (Ap3, h, M + 1); + Set_zero (&h[M + 1], L_H - M - 1); + Syn_filt (Ap4, h, h, L_H, &h[M + 1], 0); + + // 1st correlation of h[] + + L_tmp = L_mult (h[0], h[0]); + for (i = 1; i < L_H; i++) + { + L_tmp = L_mac (L_tmp, h[i], h[i]); + } + temp1 = extract_h (L_tmp); + + L_tmp = L_mult (h[0], h[1]); + for (i = 1; i < L_H - 1; i++) + { + L_tmp = L_mac (L_tmp, h[i], h[i + 1]); + } + temp2 = extract_h (L_tmp); + + if (temp2 <= 0) + { + temp2 = 0; + } + else + { + temp2 = mult (temp2, MU); + temp2 = div_s (temp2, temp1); + } + + preemphasis (st->preemph_state, st->res2, temp2, L_SUBFR); + + // filtering through 1/A(z/0.75) + + Syn_filt (Ap4, st->res2, &syn[i_subfr], L_SUBFR, st->mem_syn_pst, 1); + + // scale output to input + + agc (st->agc_state, &syn_work[i_subfr], &syn[i_subfr], + AGC_FAC, L_SUBFR); + + Az += MP1; + } + + // update syn_work[] buffer + + Copy (&syn_work[L_FRAME - M], &syn_work[-M], M); + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void Post_Filter( + Post_FilterState *st, /* i/o : post filter states */ + enum Mode mode, /* i : AMR mode */ + Word16 *syn, /* i/o : synthesis speech (postfiltered is output) */ + Word16 *Az_4, /* i : interpolated LPC parameters in all subfr. */ + Flag *pOverflow +) +{ + Word16 Ap3[MP1]; + Word16 Ap4[MP1]; /* bandwidth expanded LP parameters */ + Word16 *Az; /* pointer to Az_4: */ + /* LPC parameters in each subframe */ + register Word16 i_subfr; /* index for beginning of subframe */ + Word16 h[L_H]; + + register Word16 i; + Word16 temp1; + Word16 temp2; + Word32 L_tmp; + Word32 L_tmp2; + Word16 *syn_work = &st->synth_buf[M]; + + + /*-----------------------------------------------------* + * Post filtering * + *-----------------------------------------------------*/ + + oscl_memmove((void *)syn_work , syn, L_FRAME*sizeof(*syn)); + + Az = Az_4; + + for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) + { + /* Find weighted filter coefficients Ap3[] and ap[4] */ + + if (mode == MR122 || mode == MR102) + { + Weight_Ai(Az, gamma3_MR122, Ap3); + Weight_Ai(Az, gamma4_MR122, Ap4); + } + else + { + Weight_Ai(Az, gamma3, Ap3); + Weight_Ai(Az, gamma4, Ap4); + } + + /* filtering of synthesis speech by A(z/0.7) to find res2[] */ + + Residu(Ap3, &syn_work[i_subfr], st->res2, L_SUBFR); + + /* tilt compensation filter */ + + /* impulse response of A(z/0.7)/A(z/0.75) */ + + oscl_memmove((void *)h, Ap3, (M + 1)*sizeof(*Ap3)); + oscl_memset(&h[M + 1], 0, sizeof(Word16)*(L_H - M - 1)); + Syn_filt(Ap4, h, h, L_H, &h[M + 1], 0); + + /* 1st correlation of h[] */ + + L_tmp = 0; + + for (i = L_H - 1; i >= 0; i--) + { + L_tmp2 = ((Word32) h[i]) * h[i]; + + if (L_tmp2 != (Word32) 0x40000000L) + { + L_tmp2 = L_tmp2 << 1; + } + else + { + *pOverflow = 1; + L_tmp2 = MAX_32; + break; + } + + L_tmp = L_add(L_tmp, L_tmp2, pOverflow); + } + temp1 = (Word16)(L_tmp >> 16); + + L_tmp = 0; + + for (i = L_H - 2; i >= 0; i--) + { + L_tmp2 = ((Word32) h[i]) * h[i + 1]; + + if (L_tmp2 != (Word32) 0x40000000L) + { + L_tmp2 = L_tmp2 << 1; + } + else + { + *pOverflow = 1; + L_tmp2 = MAX_32; + break; + } + + L_tmp = L_add(L_tmp, L_tmp2, pOverflow); + } + temp2 = (Word16)(L_tmp >> 16); + + if (temp2 <= 0) + { + temp2 = 0; + } + else + { + L_tmp = (((Word32) temp2) * MU) >> 15; + + /* Sign-extend product */ + if (L_tmp & (Word32) 0x00010000L) + { + L_tmp = L_tmp | (Word32) 0xffff0000L; + } + temp2 = (Word16) L_tmp; + + temp2 = div_s(temp2, temp1); + } + + preemphasis(&(st->preemph_state), st->res2, temp2, L_SUBFR, pOverflow); + + /* filtering through 1/A(z/0.75) */ + + Syn_filt(Ap4, st->res2, &syn[i_subfr], L_SUBFR, st->mem_syn_pst, 1); + + /* scale output to input */ + + agc(&(st->agc_state), &syn_work[i_subfr], &syn[i_subfr], + AGC_FAC, L_SUBFR, pOverflow); + + Az += MP1; + } + + /* update syn_work[] buffer */ + + oscl_memmove((void *)&syn_work[-M], &syn_work[L_FRAME - M], M*sizeof(*syn_work)); + + return; +} diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pstfilt.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pstfilt.h new file mode 100644 index 0000000..bdee89c --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pstfilt.h @@ -0,0 +1,132 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: pstfilt.h + +------------------------------------------------------------------------------ + INCLUDE DESCRIPTION + + File : pstfilt.h + Purpose : Performs adaptive postfiltering on the synthesis + : speech + +------------------------------------------------------------------------------ +*/ + +#ifndef _PSTFILT_H_ +#define _PSTFILT_H_ +#define pstfilt_h "$Id $" + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "typedef.h" +#include "mode.h" +#include "cnst.h" +#include "preemph.h" +#include "agc.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; [Define module specific macros here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; [Include all pre-processor statements here.] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; EXTERNAL VARIABLES REFERENCES + ; [Declare variables used in this module but defined elsewhere] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; SIMPLE TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; ENUMERATED TYPEDEF'S + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; STRUCTURES TYPEDEF'S + ----------------------------------------------------------------------------*/ + typedef struct + { + Word16 res2[L_SUBFR]; + Word16 mem_syn_pst[M]; + preemphasisState preemph_state; + agcState agc_state; + Word16 synth_buf[M + L_FRAME]; + } Post_FilterState; + + /*---------------------------------------------------------------------------- + ; GLOBAL FUNCTION DEFINITIONS + ; [List function prototypes here] + ----------------------------------------------------------------------------*/ + Word16 Post_Filter_reset(Post_FilterState *st); + /* reset post filter (i.e. set state memory to zero) + returns 0 on success + */ + + void Post_Filter( + Post_FilterState *st, /* i/o : post filter states */ + enum Mode mode, /* i : AMR mode */ + Word16 *syn, /* i/o : synthesis speech (postfiltered is output) */ + Word16 *Az_4, /* i : interpolated LPC parameters in all subfr. */ + Flag *pOverflow + ); + /* filters the signal syn using the parameters in Az_4 to calculate filter + coefficients. + The filter must be set up using Post_Filter_init prior to the first call + to Post_Filter. Post_FilterState is updated to mirror the current state + of the filter + + return 0 on success + */ + +#ifdef __cplusplus +} +#endif + +#endif /* _PSTFILT_H_ */ + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder.cpp new file mode 100644 index 0000000..95b0b47 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder.cpp @@ -0,0 +1,77 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ + +#include "PVGSMAMRDecoder.h" + + +///////////////////////////////////////////////////////////////////////////// +OSCL_EXPORT_REF CPVGSMAMRDecoder::CPVGSMAMRDecoder() +{ +} + + +///////////////////////////////////////////////////////////////////////////// +OSCL_EXPORT_REF CPVGSMAMRDecoder::~CPVGSMAMRDecoder() +{ + delete iDecState; + iDecState = NULL; +} + + +///////////////////////////////////////////////////////////////////////////// +OSCL_EXPORT_REF int32 CPVGSMAMRDecoder::InitDecoder(void) +{ + return GSMInitDecode(&iDecState, (int8*)"Decoder"); +} + + +///////////////////////////////////////////////////////////////////////////// +OSCL_EXPORT_REF int32 CPVGSMAMRDecoder::DecodeFrame(Frame_Type_3GPP aType, + uint8* aCompressedBlock, + uint8* aAudioBuffer, + int32 aFormat) +{ + return AMRDecode(iDecState, aType, aCompressedBlock, (Word16*)aAudioBuffer, (Word16) aFormat); +} + + +///////////////////////////////////////////////////////////////////////////// +OSCL_EXPORT_REF int32 CPVGSMAMRDecoder::ResetDecoder(void) +{ + return Speech_Decode_Frame_reset(iDecState); +} + + +///////////////////////////////////////////////////////////////////////////// +OSCL_EXPORT_REF void CPVGSMAMRDecoder::TerminateDecoder(void) +{ + GSMDecodeFrameExit(&iDecState); + iDecState = NULL; +} + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder.h new file mode 100644 index 0000000..bfdc07d --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder.h @@ -0,0 +1,57 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ + +#ifndef __PVGSMAMRDECODER_H +#define __PVGSMAMRDECODER_H + +#include "oscl_base.h" + +#include "gsmamr_dec.h" + +// PVGSMAMRDecoder +class CPVGSMAMRDecoder +{ + public: + OSCL_IMPORT_REF CPVGSMAMRDecoder(); + OSCL_IMPORT_REF ~CPVGSMAMRDecoder(); + + OSCL_IMPORT_REF int32 InitDecoder(void); + OSCL_IMPORT_REF int32 DecodeFrame(Frame_Type_3GPP aType, + uint8* aCompressedBlock, + uint8* aAudioBuffer, + int32 aFormat); + OSCL_IMPORT_REF int32 ResetDecoder(void); + OSCL_IMPORT_REF void TerminateDecoder(void); + + private: + void* iDecState; +}; + +#endif + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder_dpi.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder_dpi.h new file mode 100644 index 0000000..09c778e --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/pvgsmamrdecoder_dpi.h @@ -0,0 +1,59 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ + +#ifndef __PVGSMAMRDECODER_H +#define __PVGSMAMRDECODER_H + +#include "oscl_base.h" + +#include "gsmamr_dec.h" +#include "gsmamrdpidecoder.h" + +// PVGSMAMRDecoder +class CPVGSMAMRDecoder +{ + public: + OSCL_IMPORT_REF CPVGSMAMRDecoder(); + OSCL_IMPORT_REF ~CPVGSMAMRDecoder(); + + OSCL_IMPORT_REF int32 InitDecoder(void); + OSCL_IMPORT_REF int32 DecodeFrame(Frame_Type_3GPP aType, + uint8* aCompressedBlock, + uint8* aAudioBuffer, + int32 aFormat); + OSCL_IMPORT_REF int32 ResetDecoder(void); + OSCL_IMPORT_REF void TerminateDecoder(void); + + private: + void* iDecState; + CPvGsmAmrDPIDecoder *iDecoder; +}; + +#endif + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/qgain475_tab.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/qgain475_tab.cpp new file mode 100644 index 0000000..9aaafc2 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/qgain475_tab.cpp @@ -0,0 +1,405 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* + + + + Filename: qgain475_tab.cpp + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "qgain475_tab.h" + +/*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +extern "C" +{ +#endif + + /*---------------------------------------------------------------------------- + ; MACROS + ; [Define module specific macros here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; DEFINES + ; [Include all pre-processor statements here. Include conditional + ; compile variables also.] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; LOCAL FUNCTION DEFINITIONS + ; [List function prototypes here] + ----------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------- + ; LOCAL VARIABLE DEFINITIONS + ; [Variable declaration - defined here and used outside this module] + ----------------------------------------------------------------------------*/ + + /* The table contains the following data: + * + * g_pitch(0) (Q14) // for sub- + * g_fac(0) (Q12) // frame 0 and 2 + * g_pitch(1) (Q14) // for sub- + * g_fac(2) (Q12) // frame 1 and 3 + * + */ + const Word16 table_gain_MR475[MR475_VQ_SIZE*4] = + { + /*g_pit(0), g_fac(0), g_pit(1), g_fac(1) */ + 812, 128, 542, 140, + 2873, 1135, 2266, 3402, + 2067, 563, 12677, 647, + 4132, 1798, 5601, 5285, + 7689, 374, 3735, 441, + 10912, 2638, 11807, 2494, + 20490, 797, 5218, 675, + 6724, 8354, 5282, 1696, + 1488, 428, 5882, 452, + 5332, 4072, 3583, 1268, + 2469, 901, 15894, 1005, + 14982, 3271, 10331, 4858, + 3635, 2021, 2596, 835, + 12360, 4892, 12206, 1704, + 13432, 1604, 9118, 2341, + 3968, 1538, 5479, 9936, + 3795, 417, 1359, 414, + 3640, 1569, 7995, 3541, + 11405, 645, 8552, 635, + 4056, 1377, 16608, 6124, + 11420, 700, 2007, 607, + 12415, 1578, 11119, 4654, + 13680, 1708, 11990, 1229, + 7996, 7297, 13231, 5715, + 2428, 1159, 2073, 1941, + 6218, 6121, 3546, 1804, + 8925, 1802, 8679, 1580, + 13935, 3576, 13313, 6237, + 6142, 1130, 5994, 1734, + 14141, 4662, 11271, 3321, + 12226, 1551, 13931, 3015, + 5081, 10464, 9444, 6706, + 1689, 683, 1436, 1306, + 7212, 3933, 4082, 2713, + 7793, 704, 15070, 802, + 6299, 5212, 4337, 5357, + 6676, 541, 6062, 626, + 13651, 3700, 11498, 2408, + 16156, 716, 12177, 751, + 8065, 11489, 6314, 2256, + 4466, 496, 7293, 523, + 10213, 3833, 8394, 3037, + 8403, 966, 14228, 1880, + 8703, 5409, 16395, 4863, + 7420, 1979, 6089, 1230, + 9371, 4398, 14558, 3363, + 13559, 2873, 13163, 1465, + 5534, 1678, 13138, 14771, + 7338, 600, 1318, 548, + 4252, 3539, 10044, 2364, + 10587, 622, 13088, 669, + 14126, 3526, 5039, 9784, + 15338, 619, 3115, 590, + 16442, 3013, 15542, 4168, + 15537, 1611, 15405, 1228, + 16023, 9299, 7534, 4976, + 1990, 1213, 11447, 1157, + 12512, 5519, 9475, 2644, + 7716, 2034, 13280, 2239, + 16011, 5093, 8066, 6761, + 10083, 1413, 5002, 2347, + 12523, 5975, 15126, 2899, + 18264, 2289, 15827, 2527, + 16265, 10254, 14651, 11319, + 1797, 337, 3115, 397, + 3510, 2928, 4592, 2670, + 7519, 628, 11415, 656, + 5946, 2435, 6544, 7367, + 8238, 829, 4000, 863, + 10032, 2492, 16057, 3551, + 18204, 1054, 6103, 1454, + 5884, 7900, 18752, 3468, + 1864, 544, 9198, 683, + 11623, 4160, 4594, 1644, + 3158, 1157, 15953, 2560, + 12349, 3733, 17420, 5260, + 6106, 2004, 2917, 1742, + 16467, 5257, 16787, 1680, + 17205, 1759, 4773, 3231, + 7386, 6035, 14342, 10012, + 4035, 442, 4194, 458, + 9214, 2242, 7427, 4217, + 12860, 801, 11186, 825, + 12648, 2084, 12956, 6554, + 9505, 996, 6629, 985, + 10537, 2502, 15289, 5006, + 12602, 2055, 15484, 1653, + 16194, 6921, 14231, 5790, + 2626, 828, 5615, 1686, + 13663, 5778, 3668, 1554, + 11313, 2633, 9770, 1459, + 14003, 4733, 15897, 6291, + 6278, 1870, 7910, 2285, + 16978, 4571, 16576, 3849, + 15248, 2311, 16023, 3244, + 14459, 17808, 11847, 2763, + 1981, 1407, 1400, 876, + 4335, 3547, 4391, 4210, + 5405, 680, 17461, 781, + 6501, 5118, 8091, 7677, + 7355, 794, 8333, 1182, + 15041, 3160, 14928, 3039, + 20421, 880, 14545, 852, + 12337, 14708, 6904, 1920, + 4225, 933, 8218, 1087, + 10659, 4084, 10082, 4533, + 2735, 840, 20657, 1081, + 16711, 5966, 15873, 4578, + 10871, 2574, 3773, 1166, + 14519, 4044, 20699, 2627, + 15219, 2734, 15274, 2186, + 6257, 3226, 13125, 19480, + 7196, 930, 2462, 1618, + 4515, 3092, 13852, 4277, + 10460, 833, 17339, 810, + 16891, 2289, 15546, 8217, + 13603, 1684, 3197, 1834, + 15948, 2820, 15812, 5327, + 17006, 2438, 16788, 1326, + 15671, 8156, 11726, 8556, + 3762, 2053, 9563, 1317, + 13561, 6790, 12227, 1936, + 8180, 3550, 13287, 1778, + 16299, 6599, 16291, 7758, + 8521, 2551, 7225, 2645, + 18269, 7489, 16885, 2248, + 17882, 2884, 17265, 3328, + 9417, 20162, 11042, 8320, + 1286, 620, 1431, 583, + 5993, 2289, 3978, 3626, + 5144, 752, 13409, 830, + 5553, 2860, 11764, 5908, + 10737, 560, 5446, 564, + 13321, 3008, 11946, 3683, + 19887, 798, 9825, 728, + 13663, 8748, 7391, 3053, + 2515, 778, 6050, 833, + 6469, 5074, 8305, 2463, + 6141, 1865, 15308, 1262, + 14408, 4547, 13663, 4515, + 3137, 2983, 2479, 1259, + 15088, 4647, 15382, 2607, + 14492, 2392, 12462, 2537, + 7539, 2949, 12909, 12060, + 5468, 684, 3141, 722, + 5081, 1274, 12732, 4200, + 15302, 681, 7819, 592, + 6534, 2021, 16478, 8737, + 13364, 882, 5397, 899, + 14656, 2178, 14741, 4227, + 14270, 1298, 13929, 2029, + 15477, 7482, 15815, 4572, + 2521, 2013, 5062, 1804, + 5159, 6582, 7130, 3597, + 10920, 1611, 11729, 1708, + 16903, 3455, 16268, 6640, + 9306, 1007, 9369, 2106, + 19182, 5037, 12441, 4269, + 15919, 1332, 15357, 3512, + 11898, 14141, 16101, 6854, + 2010, 737, 3779, 861, + 11454, 2880, 3564, 3540, + 9057, 1241, 12391, 896, + 8546, 4629, 11561, 5776, + 8129, 589, 8218, 588, + 18728, 3755, 12973, 3149, + 15729, 758, 16634, 754, + 15222, 11138, 15871, 2208, + 4673, 610, 10218, 678, + 15257, 4146, 5729, 3327, + 8377, 1670, 19862, 2321, + 15450, 5511, 14054, 5481, + 5728, 2888, 7580, 1346, + 14384, 5325, 16236, 3950, + 15118, 3744, 15306, 1435, + 14597, 4070, 12301, 15696, + 7617, 1699, 2170, 884, + 4459, 4567, 18094, 3306, + 12742, 815, 14926, 907, + 15016, 4281, 15518, 8368, + 17994, 1087, 2358, 865, + 16281, 3787, 15679, 4596, + 16356, 1534, 16584, 2210, + 16833, 9697, 15929, 4513, + 3277, 1085, 9643, 2187, + 11973, 6068, 9199, 4462, + 8955, 1629, 10289, 3062, + 16481, 5155, 15466, 7066, + 13678, 2543, 5273, 2277, + 16746, 6213, 16655, 3408, + 20304, 3363, 18688, 1985, + 14172, 12867, 15154, 15703, + 4473, 1020, 1681, 886, + 4311, 4301, 8952, 3657, + 5893, 1147, 11647, 1452, + 15886, 2227, 4582, 6644, + 6929, 1205, 6220, 799, + 12415, 3409, 15968, 3877, + 19859, 2109, 9689, 2141, + 14742, 8830, 14480, 2599, + 1817, 1238, 7771, 813, + 19079, 4410, 5554, 2064, + 3687, 2844, 17435, 2256, + 16697, 4486, 16199, 5388, + 8028, 2763, 3405, 2119, + 17426, 5477, 13698, 2786, + 19879, 2720, 9098, 3880, + 18172, 4833, 17336, 12207, + 5116, 996, 4935, 988, + 9888, 3081, 6014, 5371, + 15881, 1667, 8405, 1183, + 15087, 2366, 19777, 7002, + 11963, 1562, 7279, 1128, + 16859, 1532, 15762, 5381, + 14708, 2065, 20105, 2155, + 17158, 8245, 17911, 6318, + 5467, 1504, 4100, 2574, + 17421, 6810, 5673, 2888, + 16636, 3382, 8975, 1831, + 20159, 4737, 19550, 7294, + 6658, 2781, 11472, 3321, + 19397, 5054, 18878, 4722, + 16439, 2373, 20430, 4386, + 11353, 26526, 11593, 3068, + 2866, 1566, 5108, 1070, + 9614, 4915, 4939, 3536, + 7541, 878, 20717, 851, + 6938, 4395, 16799, 7733, + 10137, 1019, 9845, 964, + 15494, 3955, 15459, 3430, + 18863, 982, 20120, 963, + 16876, 12887, 14334, 4200, + 6599, 1220, 9222, 814, + 16942, 5134, 5661, 4898, + 5488, 1798, 20258, 3962, + 17005, 6178, 17929, 5929, + 9365, 3420, 7474, 1971, + 19537, 5177, 19003, 3006, + 16454, 3788, 16070, 2367, + 8664, 2743, 9445, 26358, + 10856, 1287, 3555, 1009, + 5606, 3622, 19453, 5512, + 12453, 797, 20634, 911, + 15427, 3066, 17037, 10275, + 18883, 2633, 3913, 1268, + 19519, 3371, 18052, 5230, + 19291, 1678, 19508, 3172, + 18072, 10754, 16625, 6845, + 3134, 2298, 10869, 2437, + 15580, 6913, 12597, 3381, + 11116, 3297, 16762, 2424, + 18853, 6715, 17171, 9887, + 12743, 2605, 8937, 3140, + 19033, 7764, 18347, 3880, + 20475, 3682, 19602, 3380, + 13044, 19373, 10526, 23124 + }; + + + /*--------------------------------------------------------------------------*/ +#ifdef __cplusplus +} +#endif + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + None + + Outputs: + None + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + None + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + [1] qua_gain.tab, UMTS GSM AMR speech codec, + R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ + + + + + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/sp_dec.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/sp_dec.cpp new file mode 100644 index 0000000..f597a23 --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/sp_dec.cpp @@ -0,0 +1,774 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: sp_dec.cpp + Functions: GSMInitDecode + Speech_Decode_Frame_reset + GSMDecodeFrameExit + GSMFrameDecode + +------------------------------------------------------------------------------ + MODULE DESCRIPTION + + This file contains the functions that initialize, invoke, reset, and exit + the GSM AMR decoder. + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "sp_dec.h" +#include "typedef.h" +#include "cnst.h" +#include "dec_amr.h" +#include "pstfilt.h" +#include "mode.h" +#include "post_pro.h" +#include "oscl_mem.h" +#include "bitno_tab.h" + + +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Bin2int +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + no_of_bits = number of bits associated with value + bitstream = pointer to buffer where bits are read + + Outputs: + None + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Function : Bin2int + Purpose : Read "no_of_bits" bits from the array bitstream[] + and convert to integer. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + bits2prm.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +static Word16 Bin2int ( // Reconstructed parameter + Word16 no_of_bits, // input : number of bits associated with value + Word16 *bitstream // output: address where bits are written +) +{ + Word16 value, i, bit; + + value = 0; + for (i = 0; i < no_of_bits; i++) + { + value = shl (value, 1); + bit = *bitstream++; + if (sub (bit, BIT_1) == 0) + value = add (value, 1); + } + return (value); +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ +static Word16 Bin2int( /* Reconstructed parameter */ + Word16 no_of_bits, /* input : number of bits associated with value */ + Word16 *bitstream /* input: address where bits are read from */ +) +{ + Word16 value; + Word16 i; + Word16 single_bit; + + value = 0; + for (i = 0; i < no_of_bits; i++) + { + value <<= 1; + single_bit = *(bitstream++); + value |= single_bit; + } + return (value); +} + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: bits2prm +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + mode = AMR mode of type enum Mode + bits[] = pointer to serial bits of type Word16 + prm[] = pointer to analysis parameters of type Word16 + + Outputs: + None + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + Function : Bits2prm + Purpose : Retrieves the vector of encoder parameters from + the received serial bits in a frame. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + bits2prm.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +void Bits2prm ( + enum Mode mode, // i : AMR mode + Word16 bits[], // i : serial bits (size <= MAX_SERIAL_SIZE) + Word16 prm[] // o : analysis parameters (size <= MAX_PRM_SIZE) +) +{ + Word16 i; + + for (i = 0; i < prmno[mode]; i++) + { + prm[i] = Bin2int (bitno[mode][i], bits); + bits += bitno[mode][i]; + add(0,0); // account for above pointer update + } + + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; FUNCTION CODE +----------------------------------------------------------------------------*/ +void Bits2prm( + enum Mode mode, /* i : AMR mode */ + Word16 bits[], /* i : serial bits (size <= MAX_SERIAL_SIZE) */ + Word16 prm[], /* o : analysis parameters (size <= MAX_PRM_SIZE) */ + CommonAmrTbls* common_amr_tbls /* i : ptr to strcut of table ptrs */ +) +{ + Word16 i; + const Word16* prmno_ptr = common_amr_tbls->prmno_ptr; + const Word16* const* bitno_ptr = common_amr_tbls->bitno_ptr; + + + for (i = 0; i < prmno_ptr[mode]; i++) + { + prm[i] = Bin2int(bitno_ptr[mode][i], bits); + bits += bitno_ptr[mode][i]; + } + + return; +} + + + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: GSMInitDecode +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to an array of pointers to structures of type + Speech_Decode_FrameState + no_hp_post_MR122 = flag to turn off high-pass post filter for 12.2 kbps + mode (Flag) + id = pointer to an array whose contents are of type char + + Outputs: + decoder_amrState field of the structure pointed to by the pointer pointed + to by state is set to NULL + post_state field of the structure pointed to by the pointer pointed to + by state is set to NULL + postHP_state field of the structure pointed to by the pointer pointed to + by state is set to NULL + no_hp_post_MR122 field of the structure pointed to by the pointer pointed + to by state is set to the input no_hp_post_MR122 + + Returns: + return_value = set to zero, if initialization was successful; -1, + otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function allocates memory for filter structure and initializes state + memory used by the GSM AMR decoder. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + Note: Original function name of Speech_Decode_Frame_init was changed to + GSMInitDecode in the Code section. + +int Speech_Decode_Frame_init (Speech_Decode_FrameState **state, + char *id) +{ + Speech_Decode_FrameState* s; + + if (state == (Speech_Decode_FrameState **) NULL){ + fprintf(stderr, "Speech_Decode_Frame_init: invalid parameter\n"); + return -1; + } + *state = NULL; + + // allocate memory + if ((s= (Speech_Decode_FrameState *) + malloc(sizeof(Speech_Decode_FrameState))) == NULL) { + fprintf(stderr, "Speech_Decode_Frame_init: can not malloc state " + "structure\n"); + return -1; + } + s->decoder_amrState = NULL; + s->post_state = NULL; + s->postHP_state = NULL; + + if (Decoder_amr_init(&s->decoder_amrState) || + Post_Filter_init(&s->post_state) || + Post_Process_init(&s->postHP_state) ) { + Speech_Decode_Frame_exit(&s); + return -1; + } + + s->complexityCounter = getCounterId(id); + + Speech_Decode_Frame_reset(s); + *state = s; + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +Word16 GSMInitDecode(void **state_data, + Word8 * id) +{ + Speech_Decode_FrameState* s; + OSCL_UNUSED_ARG(id); + + if (state_data == NULL) + { + /* fprintf(stderr, "Speech_Decode_Frame_init: + invalid parameter\n"); */ + return (-1); + } + *state_data = NULL; + + /* allocate memory */ + if ((s = (Speech_Decode_FrameState *) + oscl_malloc(sizeof(Speech_Decode_FrameState))) == NULL) + { + /* fprintf(stderr, "Speech_Decode_Frame_init: can not malloc state " + "structure\n"); */ + return (-1); + } + + if (Decoder_amr_init(&s->decoder_amrState) + || Post_Process_reset(&s->postHP_state)) + { + Speech_Decode_FrameState *tmp = s; + /* + * dereferencing type-punned pointer avoid + * breaking strict-aliasing rules + */ + void** tempVoid = (void**) tmp; + GSMDecodeFrameExit(tempVoid); + return (-1); + } + + + Speech_Decode_Frame_reset(s); + *state_data = (void *)s; + + return (0); +} + + +/****************************************************************************/ + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: Speech_Decode_Frame_reset +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to structures of type Speech_Decode_FrameState + + Outputs: + None + + Returns: + return_value = set to zero if reset was successful; -1, otherwise (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function resets the state memory used by the GSM AMR decoder. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + +int Speech_Decode_Frame_reset (Speech_Decode_FrameState *state) +{ + if (state == (Speech_Decode_FrameState *) NULL){ + fprintf(stderr, "Speech_Decode_Frame_reset: invalid parameter\n"); + return -1; + } + + Decoder_amr_reset(state->decoder_amrState, (enum Mode)0); + Post_Filter_reset(state->post_state); + Post_Process_reset(state->postHP_state); + + state->prev_mode = (enum Mode)0; + + setCounter(state->complexityCounter); + Init_WMOPS_counter(); + setCounter(0); // set counter to global counter + + return 0; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ +Word16 Speech_Decode_Frame_reset(void *state_data) +{ + + Speech_Decode_FrameState *state = + (Speech_Decode_FrameState *) state_data; + + if (state_data == NULL) + { + /* fprintf(stderr, "Speech_Decode_Frame_reset: + invalid parameter\n"); */ + return (-1); + } + + Decoder_amr_reset(&(state->decoder_amrState), MR475); + Post_Filter_reset(&(state->post_state)); + Post_Process_reset(&(state->postHP_state)); + + state->prev_mode = MR475; + + return (0); +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: GSMDecodeFrameExit +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + state = pointer to an array of pointers to structures of type + Speech_Decode_FrameState + + Outputs: + state contents is set to NULL + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function frees up the memory used for the state memory of the GSM AMR + decoder. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + Note: The original function name of Speech_Decode_Frame_exit was changed to + GSMDecodeFrameExit in the Code section. + +void Speech_Decode_Frame_exit (Speech_Decode_FrameState **state) +{ + if (state == NULL || *state == NULL) + return; + + Decoder_amr_exit(&(*state)->decoder_amrState); + Post_Filter_exit(&(*state)->post_state); + Post_Process_exit(&(*state)->postHP_state); + + setCounter((*state)->complexityCounter); + WMOPS_output(0); + setCounter(0); // set counter to global counter + + // deallocate memory + free(*state); + *state = NULL; + + return; +} + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void GSMDecodeFrameExit(void **state_data) +{ + + Speech_Decode_FrameState **state = + (Speech_Decode_FrameState **) state_data; + + if (state == NULL || *state == NULL) + { + return; + } + + /* deallocate memory */ + oscl_free(*state); + *state = NULL; + + return; +} + +/****************************************************************************/ + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: GSMFrameDecode +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + st = pointer to structures of type Speech_Decode_FrameState + mode = GSM AMR codec mode (enum Mode) + serial = pointer to the serial bit stream buffer (unsigned char) + frame_type = GSM AMR receive frame type (enum RXFrameType) + synth = pointer to the output synthesis speech buffer (Word16) + + Outputs: + synth contents are truncated to 13 bits if NO13BIT is not defined, + otherwise, its contents are left at 16 bits + + Returns: + return_value = set to zero (int) + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function is the entry point to the GSM AMR decoder. The following + operations are performed on one received frame: First, the codec + parameters are parsed from the buffer pointed to by serial according to + frame_type. Then the AMR decoder is invoked via a call to Decoder_amr. Post + filtering of the decoded data is done via a call to Post_Filter function. + Lastly, the decoded data is post-processed via a call to Post_Process + function. If NO13BIT is not defined, the contents of the buffer pointed to + by synth is truncated to 13 bits. It remains unchanged otherwise. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + + sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + Note: The original function name of Speech_Decode_Frame_exit was changed to + GSMFrameDecode in the Code section. + +int Speech_Decode_Frame ( + Speech_Decode_FrameState *st, // io: post filter states + enum Mode mode, // i : AMR mode + Word16 *serial, // i : serial bit stream + enum RXFrameType frame_type, // i : Frame type + Word16 *synth // o : synthesis speech (postfiltered + // output) +) +{ + Word16 parm[MAX_PRM_SIZE + 1]; // Synthesis parameters + Word16 Az_dec[AZ_SIZE]; // Decoded Az for post-filter + // in 4 subframes + +#if !defined(NO13BIT) + Word16 i; +#endif + + setCounter(st->complexityCounter); + Reset_WMOPS_counter (); // reset WMOPS counter for the new frame + + // Serial to parameters + if ((frame_type == RX_SID_BAD) || + (frame_type == RX_SID_UPDATE)) { + // Override mode to MRDTX + Bits2prm (MRDTX, serial, parm); + } else { + Bits2prm (mode, serial, parm); + } + + // Synthesis + Decoder_amr(st->decoder_amrState, mode, parm, frame_type, + synth, Az_dec); + + Post_Filter(st->post_state, mode, synth, Az_dec); // Post-filter + + // post HP filter, and 15->16 bits + Post_Process(st->postHP_state, synth, L_FRAME); + +#if !defined(NO13BIT) + // Truncate to 13 bits + for (i = 0; i < L_FRAME; i++) + { + synth[i] = synth[i] & 0xfff8; + } +#endif + + setCounter(0); // set counter to global counter + + return 0; +} + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void GSMFrameDecode( + Speech_Decode_FrameState *st, /* io: post filter states */ + enum Mode mode, /* i : AMR mode */ + Word16 *serial, /* i : serial bit stream */ + enum RXFrameType frame_type, /* i : Frame type */ + Word16 *synth) /* o : synthesis speech (postfiltered */ +/* output) */ + +{ + Word16 parm[MAX_PRM_SIZE + 1]; /* Synthesis parameters */ + Word16 Az_dec[AZ_SIZE]; /* Decoded Az for post-filter */ + /* in 4 subframes */ + Flag *pOverflow = &(st->decoder_amrState.overflow); /* Overflow flag */ + +#if !defined(NO13BIT) + Word16 i; +#endif + + /* Serial to parameters */ + if ((frame_type == RX_SID_BAD) || + (frame_type == RX_SID_UPDATE)) + { + /* Override mode to MRDTX */ + Bits2prm(MRDTX, serial, parm, &st->decoder_amrState.common_amr_tbls); + } + else + { + Bits2prm(mode, serial, parm, &st->decoder_amrState.common_amr_tbls); + } + + /* Synthesis */ + Decoder_amr( + &(st->decoder_amrState), + mode, + parm, + frame_type, + synth, + Az_dec); + + /* Post-filter */ + Post_Filter( + &(st->post_state), + mode, + synth, + Az_dec, + pOverflow); + + /* post HP filter, and 15->16 bits */ + Post_Process( + &(st->postHP_state), + synth, + L_FRAME, + pOverflow); + +#if !defined(NO13BIT) + /* Truncate to 13 bits */ + for (i = 0; i < L_FRAME; i++) + { + synth[i] = synth[i] & 0xfff8; + } +#endif + + return; +} + + + diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/sp_dec.h b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/sp_dec.h new file mode 100644 index 0000000..3150feb --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/sp_dec.h @@ -0,0 +1,113 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +***************************************************************************** +* +* GSM AMR speech codec Version 2.0.0 February 8, 1999 +* +***************************************************************************** +* +* File : sp_dec.h +* Purpose : Decoding and post filtering of one speech frame. +* + + Description: Replaced "int" and/or "char" with OSCL defined types. + +***************************************************************************** +*/ +#ifndef sp_dec_h +#define sp_dec_h "$Id $" + +/* +***************************************************************************** +* INCLUDE FILES +***************************************************************************** +*/ +#include "typedef.h" +#include "cnst.h" +#include "dec_amr.h" +#include "pstfilt.h" +#include "post_pro.h" +#include "mode.h" + +/* +***************************************************************************** +* DEFINITION OF DATA TYPES +***************************************************************************** +*/ +typedef struct +{ + Decoder_amrState decoder_amrState; + Post_FilterState post_state; + Post_ProcessState postHP_state; + enum Mode prev_mode; +} Speech_Decode_FrameState; + +/* +***************************************************************************** +* DECLARATION OF PROTOTYPES +***************************************************************************** +*/ + +#if defined(__cplusplus) +extern "C" +{ +#endif + Word16 GSMInitDecode(void **state_data, + Word8 *id); + /* initialize one instance of the speech decoder + Stores pointer to filter status struct in *st. This pointer has to + be passed to Speech_Decode_Frame in each call. + returns 0 on success + */ + + Word16 Speech_Decode_Frame_reset(void *state_data); + /* reset speech decoder (i.e. set state memory to zero) + returns 0 on success + */ + + void GSMDecodeFrameExit(void **state_data); + /* de-initialize speech decoder (i.e. free status struct) + stores NULL in *s + */ + + void GSMFrameDecode( + Speech_Decode_FrameState *st, /* io: post filter states */ + enum Mode mode, /* i : AMR mode */ + Word16 *serial, /* i : serial bit stream */ + enum RXFrameType frame_type, /* i : Frame type */ + Word16 *synth /* o : synthesis speech (postfiltered */ + /* output) */ + ); + /* return 0 on success + */ +#if defined(__cplusplus) +} +#endif +#endif diff --git a/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/wmf_to_ets.cpp b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/wmf_to_ets.cpp new file mode 100644 index 0000000..c60c2cb --- /dev/null +++ b/opencore/codecs_v2/audio/gsm_amr/amr_nb/dec/src/wmf_to_ets.cpp @@ -0,0 +1,163 @@ +/* ------------------------------------------------------------------ + * Copyright (C) 1998-2009 PacketVideo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. + * See the License for the specific language governing permissions + * and limitations under the License. + * ------------------------------------------------------------------- + */ +/**************************************************************************************** +Portions of this file are derived from the following 3GPP standard: + + 3GPP TS 26.073 + ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec + Available from http://www.3gpp.org + +(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC) +Permission to distribute, modify and use this file under the standard license +terms listed above has been obtained from the copyright holder. +****************************************************************************************/ +/* +------------------------------------------------------------------------------ + + + + Filename: wmf_to_ets.cpp + Functions: wmf_to_ets + +------------------------------------------------------------------------------ +*/ + +/*---------------------------------------------------------------------------- +; INCLUDES +----------------------------------------------------------------------------*/ +#include "frame_type_3gpp.h" +#include "wmf_to_ets.h" +#include "typedef.h" +/*---------------------------------------------------------------------------- +; MACROS +; Define module specific macros here +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; DEFINES +; Include all pre-processor statements here. Include conditional +; compile variables also. +----------------------------------------------------------------------------*/ + + +/*---------------------------------------------------------------------------- +; LOCAL FUNCTION DEFINITIONS +; Function Prototype declaration +----------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------- +; LOCAL VARIABLE DEFINITIONS +; Variable declaration - defined here and used outside this module +----------------------------------------------------------------------------*/ + + + +/* +------------------------------------------------------------------------------ + FUNCTION NAME: wmf_to_ets +------------------------------------------------------------------------------ + INPUT AND OUTPUT DEFINITIONS + + Inputs: + frame_type_3gpp = decoder speech bit rate (enum Frame_Type_3GPP) + wmf_input_ptr = pointer to input encoded speech bits in WMF (non-IF2) format + (Word8) + ets_output_ptr = pointer to output encoded speech bits in ETS format (Word16) + + Outputs: + ets_output_ptr = pointer to encoded speech bits in the ETS format (Word16) + + Returns: + None + + Global Variables Used: + None + + Local Variables Needed: + None + +------------------------------------------------------------------------------ + FUNCTION DESCRIPTION + + This function performs a transformation on the data buffers. It converts the + data format from WMF (non-IF2) (Wireless Multi-media Forum) to ETS (European + Telecommunication Standard). WMF format has the encoded speech bits byte + aligned with MSB to LSB going left to right. ETS format has the encoded speech + bits each separate with only one bit stored in each word. + +------------------------------------------------------------------------------ + REQUIREMENTS + + None + +------------------------------------------------------------------------------ + REFERENCES + +AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0 Release 4, June 2001 + +------------------------------------------------------------------------------ + PSEUDO-CODE + + + +------------------------------------------------------------------------------ + CAUTION [optional] + [State any special notes, constraints or cautions for users of this function] + +------------------------------------------------------------------------------ +*/ + +void wmf_to_ets( + enum Frame_Type_3GPP frame_type_3gpp, + UWord8 *wmf_input_ptr, + Word16 *ets_output_ptr, + CommonAmrTbls* common_amr_tbls) +{ + + Word16 i; + const Word16* const* reorderBits_ptr = common_amr_tbls->reorderBits_ptr; + const Word16* numOfBits_ptr = common_amr_tbls->numOfBits_ptr; + + /* + * The following section of code accesses bits in the WMF method of + * bit ordering. Each bit is given its own location in the buffer pointed + * to by ets_output_ptr. If the frame_type_3gpp is less than MRDTX then + * the elements are reordered within the buffer pointed to by ets_output_ptr. + */ + + if (frame_type_3gpp < AMR_SID) + { + /* The table numOfBits[] can be found in bitreorder.c. */ + for (i = numOfBits_ptr[frame_type_3gpp] - 1; i >= 0; i--) + { + /* The table reorderBits[][] can be found in bitreorder.c. */ + ets_output_ptr[reorderBits_ptr[frame_type_3gpp][i]] = + (wmf_input_ptr[i>>3] >> ((~i) & 0x7)) & 0x01; + } + } + else + { + /* The table numOfBits[] can be found in bitreorder.c. */ + for (i = numOfBits_ptr[frame_type_3gpp] - 1; i >= 0; i--) + { + ets_output_ptr[i] = (wmf_input_ptr[i>>3] >> ((~i) & 0x7)) & 0x01; + } + } + + return; +} |