cannam@154: /*********************************************************************** cannam@154: Copyright (c) 2006-2011, Skype Limited. All rights reserved. cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: - Redistributions of source code must retain the above copyright notice, cannam@154: this list of conditions and the following disclaimer. cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: - Neither the name of Internet Society, IETF or IETF Trust, nor the cannam@154: names of specific contributors, may be used to endorse or promote cannam@154: products derived from this software without specific prior written cannam@154: permission. cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" cannam@154: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE cannam@154: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE cannam@154: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE cannam@154: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR cannam@154: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF cannam@154: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS cannam@154: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN cannam@154: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) cannam@154: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE cannam@154: POSSIBILITY OF SUCH DAMAGE. cannam@154: ***********************************************************************/ cannam@154: cannam@154: #ifndef SILK_MAIN_FLP_H cannam@154: #define SILK_MAIN_FLP_H cannam@154: cannam@154: #include "SigProc_FLP.h" cannam@154: #include "SigProc_FIX.h" cannam@154: #include "structs_FLP.h" cannam@154: #include "main.h" cannam@154: #include "define.h" cannam@154: #include "debug.h" cannam@154: #include "entenc.h" cannam@154: cannam@154: #ifdef __cplusplus cannam@154: extern "C" cannam@154: { cannam@154: #endif cannam@154: cannam@154: #define silk_encoder_state_Fxx silk_encoder_state_FLP cannam@154: #define silk_encode_do_VAD_Fxx silk_encode_do_VAD_FLP cannam@154: #define silk_encode_frame_Fxx silk_encode_frame_FLP cannam@154: cannam@154: /*********************/ cannam@154: /* Encoder Functions */ cannam@154: /*********************/ cannam@154: cannam@154: /* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */ cannam@154: void silk_HP_variable_cutoff( cannam@154: silk_encoder_state_Fxx state_Fxx[] /* I/O Encoder states */ cannam@154: ); cannam@154: cannam@154: /* Encoder main function */ cannam@154: void silk_encode_do_VAD_FLP( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: opus_int activity /* I Decision of Opus voice activity detector */ cannam@154: ); cannam@154: cannam@154: /* Encoder main function */ cannam@154: opus_int silk_encode_frame_FLP( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: opus_int32 *pnBytesOut, /* O Number of payload bytes; */ cannam@154: ec_enc *psRangeEnc, /* I/O compressor data structure */ cannam@154: opus_int condCoding, /* I The type of conditional coding to use */ cannam@154: opus_int maxBits, /* I If > 0: maximum number of output bits */ cannam@154: opus_int useCBR /* I Flag to force constant-bitrate operation */ cannam@154: ); cannam@154: cannam@154: /* Initializes the Silk encoder state */ cannam@154: opus_int silk_init_encoder( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: int arch /* I Run-tim architecture */ cannam@154: ); cannam@154: cannam@154: /* Control the Silk encoder */ cannam@154: opus_int silk_control_encoder( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Pointer to Silk encoder state FLP */ cannam@154: silk_EncControlStruct *encControl, /* I Control structure */ cannam@154: const opus_int allow_bw_switch, /* I Flag to allow switching audio bandwidth */ cannam@154: const opus_int channelNb, /* I Channel number */ cannam@154: const opus_int force_fs_kHz cannam@154: ); cannam@154: cannam@154: /**************************/ cannam@154: /* Noise shaping analysis */ cannam@154: /**************************/ cannam@154: /* Compute noise shaping coefficients and initial gain values */ cannam@154: void silk_noise_shape_analysis_FLP( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ cannam@154: const silk_float *pitch_res, /* I LPC residual from pitch analysis */ cannam@154: const silk_float *x /* I Input signal [frame_length + la_shape] */ cannam@154: ); cannam@154: cannam@154: /* Autocorrelations for a warped frequency axis */ cannam@154: void silk_warped_autocorrelation_FLP( cannam@154: silk_float *corr, /* O Result [order + 1] */ cannam@154: const silk_float *input, /* I Input data to correlate */ cannam@154: const silk_float warping, /* I Warping coefficient */ cannam@154: const opus_int length, /* I Length of input */ cannam@154: const opus_int order /* I Correlation order (even) */ cannam@154: ); cannam@154: cannam@154: /* Calculation of LTP state scaling */ cannam@154: void silk_LTP_scale_ctrl_FLP( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ cannam@154: opus_int condCoding /* I The type of conditional coding to use */ cannam@154: ); cannam@154: cannam@154: /**********************************************/ cannam@154: /* Prediction Analysis */ cannam@154: /**********************************************/ cannam@154: /* Find pitch lags */ cannam@154: void silk_find_pitch_lags_FLP( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ cannam@154: silk_float res[], /* O Residual */ cannam@154: const silk_float x[], /* I Speech signal */ cannam@154: int arch /* I Run-time architecture */ cannam@154: ); cannam@154: cannam@154: /* Find LPC and LTP coefficients */ cannam@154: void silk_find_pred_coefs_FLP( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ cannam@154: const silk_float res_pitch[], /* I Residual from pitch analysis */ cannam@154: const silk_float x[], /* I Speech signal */ cannam@154: opus_int condCoding /* I The type of conditional coding to use */ cannam@154: ); cannam@154: cannam@154: /* LPC analysis */ cannam@154: void silk_find_LPC_FLP( cannam@154: silk_encoder_state *psEncC, /* I/O Encoder state */ cannam@154: opus_int16 NLSF_Q15[], /* O NLSFs */ cannam@154: const silk_float x[], /* I Input signal */ cannam@154: const silk_float minInvGain /* I Prediction gain from LTP (dB) */ cannam@154: ); cannam@154: cannam@154: /* LTP analysis */ cannam@154: void silk_find_LTP_FLP( cannam@154: silk_float XX[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */ cannam@154: silk_float xX[ MAX_NB_SUBFR * LTP_ORDER ], /* O Weight for LTP quantization */ cannam@154: const silk_float r_ptr[], /* I LPC residual */ cannam@154: const opus_int lag[ MAX_NB_SUBFR ], /* I LTP lags */ cannam@154: const opus_int subfr_length, /* I Subframe length */ cannam@154: const opus_int nb_subfr /* I number of subframes */ cannam@154: ); cannam@154: cannam@154: void silk_LTP_analysis_filter_FLP( cannam@154: silk_float *LTP_res, /* O LTP res MAX_NB_SUBFR*(pre_lgth+subfr_lngth) */ cannam@154: const silk_float *x, /* I Input signal, with preceding samples */ cannam@154: const silk_float B[ LTP_ORDER * MAX_NB_SUBFR ], /* I LTP coefficients for each subframe */ cannam@154: const opus_int pitchL[ MAX_NB_SUBFR ], /* I Pitch lags */ cannam@154: const silk_float invGains[ MAX_NB_SUBFR ], /* I Inverse quantization gains */ cannam@154: const opus_int subfr_length, /* I Length of each subframe */ cannam@154: const opus_int nb_subfr, /* I number of subframes */ cannam@154: const opus_int pre_length /* I Preceding samples for each subframe */ cannam@154: ); cannam@154: cannam@154: /* Calculates residual energies of input subframes where all subframes have LPC_order */ cannam@154: /* of preceding samples */ cannam@154: void silk_residual_energy_FLP( cannam@154: silk_float nrgs[ MAX_NB_SUBFR ], /* O Residual energy per subframe */ cannam@154: const silk_float x[], /* I Input signal */ cannam@154: silk_float a[ 2 ][ MAX_LPC_ORDER ], /* I AR coefs for each frame half */ cannam@154: const silk_float gains[], /* I Quantization gains */ cannam@154: const opus_int subfr_length, /* I Subframe length */ cannam@154: const opus_int nb_subfr, /* I number of subframes */ cannam@154: const opus_int LPC_order /* I LPC order */ cannam@154: ); cannam@154: cannam@154: /* 16th order LPC analysis filter */ cannam@154: void silk_LPC_analysis_filter_FLP( cannam@154: silk_float r_LPC[], /* O LPC residual signal */ cannam@154: const silk_float PredCoef[], /* I LPC coefficients */ cannam@154: const silk_float s[], /* I Input signal */ cannam@154: const opus_int length, /* I Length of input signal */ cannam@154: const opus_int Order /* I LPC order */ cannam@154: ); cannam@154: cannam@154: /* LTP tap quantizer */ cannam@154: void silk_quant_LTP_gains_FLP( cannam@154: silk_float B[ MAX_NB_SUBFR * LTP_ORDER ], /* O Quantized LTP gains */ cannam@154: opus_int8 cbk_index[ MAX_NB_SUBFR ], /* O Codebook index */ cannam@154: opus_int8 *periodicity_index, /* O Periodicity index */ cannam@154: opus_int32 *sum_log_gain_Q7, /* I/O Cumulative max prediction gain */ cannam@154: silk_float *pred_gain_dB, /* O LTP prediction gain */ cannam@154: const silk_float XX[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* I Correlation matrix */ cannam@154: const silk_float xX[ MAX_NB_SUBFR * LTP_ORDER ], /* I Correlation vector */ cannam@154: const opus_int subfr_len, /* I Number of samples per subframe */ cannam@154: const opus_int nb_subfr, /* I Number of subframes */ cannam@154: int arch /* I Run-time architecture */ cannam@154: ); cannam@154: cannam@154: /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */ cannam@154: silk_float silk_residual_energy_covar_FLP( /* O Weighted residual energy */ cannam@154: const silk_float *c, /* I Filter coefficients */ cannam@154: silk_float *wXX, /* I/O Weighted correlation matrix, reg. out */ cannam@154: const silk_float *wXx, /* I Weighted correlation vector */ cannam@154: const silk_float wxx, /* I Weighted correlation value */ cannam@154: const opus_int D /* I Dimension */ cannam@154: ); cannam@154: cannam@154: /* Processing of gains */ cannam@154: void silk_process_gains_FLP( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ cannam@154: opus_int condCoding /* I The type of conditional coding to use */ cannam@154: ); cannam@154: cannam@154: /******************/ cannam@154: /* Linear Algebra */ cannam@154: /******************/ cannam@154: /* Calculates correlation matrix X'*X */ cannam@154: void silk_corrMatrix_FLP( cannam@154: const silk_float *x, /* I x vector [ L+order-1 ] used to create X */ cannam@154: const opus_int L, /* I Length of vectors */ cannam@154: const opus_int Order, /* I Max lag for correlation */ cannam@154: silk_float *XX /* O X'*X correlation matrix [order x order] */ cannam@154: ); cannam@154: cannam@154: /* Calculates correlation vector X'*t */ cannam@154: void silk_corrVector_FLP( cannam@154: const silk_float *x, /* I x vector [L+order-1] used to create X */ cannam@154: const silk_float *t, /* I Target vector [L] */ cannam@154: const opus_int L, /* I Length of vecors */ cannam@154: const opus_int Order, /* I Max lag for correlation */ cannam@154: silk_float *Xt /* O X'*t correlation vector [order] */ cannam@154: ); cannam@154: cannam@154: /* Apply sine window to signal vector. */ cannam@154: /* Window types: */ cannam@154: /* 1 -> sine window from 0 to pi/2 */ cannam@154: /* 2 -> sine window from pi/2 to pi */ cannam@154: void silk_apply_sine_window_FLP( cannam@154: silk_float px_win[], /* O Pointer to windowed signal */ cannam@154: const silk_float px[], /* I Pointer to input signal */ cannam@154: const opus_int win_type, /* I Selects a window type */ cannam@154: const opus_int length /* I Window length, multiple of 4 */ cannam@154: ); cannam@154: cannam@154: /* Wrapper functions. Call flp / fix code */ cannam@154: cannam@154: /* Convert AR filter coefficients to NLSF parameters */ cannam@154: void silk_A2NLSF_FLP( cannam@154: opus_int16 *NLSF_Q15, /* O NLSF vector [ LPC_order ] */ cannam@154: const silk_float *pAR, /* I LPC coefficients [ LPC_order ] */ cannam@154: const opus_int LPC_order /* I LPC order */ cannam@154: ); cannam@154: cannam@154: /* Convert NLSF parameters to AR prediction filter coefficients */ cannam@154: void silk_NLSF2A_FLP( cannam@154: silk_float *pAR, /* O LPC coefficients [ LPC_order ] */ cannam@154: const opus_int16 *NLSF_Q15, /* I NLSF vector [ LPC_order ] */ cannam@154: const opus_int LPC_order, /* I LPC order */ cannam@154: int arch /* I Run-time architecture */ cannam@154: ); cannam@154: cannam@154: /* Limit, stabilize, and quantize NLSFs */ cannam@154: void silk_process_NLSFs_FLP( cannam@154: silk_encoder_state *psEncC, /* I/O Encoder state */ cannam@154: silk_float PredCoef[ 2 ][ MAX_LPC_ORDER ], /* O Prediction coefficients */ cannam@154: opus_int16 NLSF_Q15[ MAX_LPC_ORDER ], /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */ cannam@154: const opus_int16 prev_NLSF_Q15[ MAX_LPC_ORDER ] /* I Previous Normalized LSFs (0 - (2^15-1)) */ cannam@154: ); cannam@154: cannam@154: /* Floating-point Silk NSQ wrapper */ cannam@154: void silk_NSQ_wrapper_FLP( cannam@154: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ cannam@154: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ cannam@154: SideInfoIndices *psIndices, /* I/O Quantization indices */ cannam@154: silk_nsq_state *psNSQ, /* I/O Noise Shaping Quantzation state */ cannam@154: opus_int8 pulses[], /* O Quantized pulse signal */ cannam@154: const silk_float x[] /* I Prefiltered input signal */ cannam@154: ); cannam@154: cannam@154: #ifdef __cplusplus cannam@154: } cannam@154: #endif cannam@154: cannam@154: #endif