phi_lpc.c
资源名称:NETVIDEO.rar [点击查看]
上传用户:sun1608
上传日期:2007-02-02
资源大小:6116k
文件大小:130k
源码类别:
流媒体/Mpeg4/MP4
开发平台:
Visual C++
- /*====================================================================*/
- /* MPEG-4 Audio (ISO/IEC 14496-3) Copyright Header */
- /*====================================================================*/
- /*
- This software module was originally developed by Rakesh Taori and Andy
- Gerrits (Philips Research Laboratories, Eindhoven, The Netherlands) in
- the course of development of the MPEG-4 Audio (ISO/IEC 14496-3). This
- software module is an implementation of a part of one or more MPEG-4
- Audio (ISO/IEC 14496-3) tools as specified by the MPEG-4 Audio
- (ISO/IEC 14496-3). ISO/IEC gives users of the MPEG-4 Audio (ISO/IEC
- 14496-3) free license to this software module or modifications thereof
- for use in hardware or software products claiming conformance to the
- MPEG-4 Audio (ISO/IEC 14496-3). Those intending to use this software
- module in hardware or software products are advised that its use may
- infringe existing patents. The original developer of this software
- module and his/her company, the subsequent editors and their
- companies, and ISO/IEC have no liability for use of this software
- module or modifications thereof in an implementation. Copyright is not
- released for non MPEG-4 Audio (ISO/IEC 14496-3) conforming products.
- CN1 retains full right to use the code for his/her own purpose, assign
- or donate the code to a third party and to inhibit third parties from
- using the code for non MPEG-4 Audio (ISO/IEC 14496-3) conforming
- products. This copyright notice must be included in all copies or
- derivative works. Copyright 1996.
- */
- /*====================================================================*/
- /*======================================================================*/
- /* */
- /* SOURCE_FILE: PHI_LPC.C */
- /* PACKAGE: WDBxx */
- /* COMPONENT: Linear Prediction Subroutines */
- /* */
- /*======================================================================*/
- /*======================================================================*/
- /* I N C L U D E S */
- /*======================================================================*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <malloc.h>
- #include <float.h>
- #include <assert.h>
- #include "buffersHandle.h" /* handler, defines, enums */
- #include "bitstream.h"
- #include "phi_cons.h"
- #include "phi_priv.h"
- #include "phi_lpc.h"
- #include "phi_lpcq.h"
- #include "phi_lsfr.h"
- #include "nec_abs_proto.h"
- #include "pan_celp_const.h"
- #include "pan_celp_proto.h"
- #include "att_proto.h"
- #include "nec_abs_const.h"
- #include "nec_lspnw20.tbl"
- #define NEC_PAI 3.141592
- #define NEC_LSPPRDCT_ORDER 4
- #define NEC_NUM_LSPSPLIT1 2
- #define NEC_NUM_LSPSPLIT2 4
- #define NEC_QLSP_CAND 2
- #define NEC_LSP_MINWIDTH_FRQ16 0.028
- #define NEC_MAX_LSPVQ_ORDER 20
- #define DEBUG_YM /* flag for halting LPC analysis : '98/11/27 */
- /*======================================================================*/
- /* L O C A L F U N C T I O N S P R O T O T Y P E S */
- /*======================================================================*/
- /*======================================================================*/
- /* Modified Panasonic/NEC functions */
- /*======================================================================*/
- static void mod_nec_lsp_sort( float x[], long order , PHI_PRIV_TYPE *PHI_Priv);
- /*======================================================================*/
- static void mod_nec_psvq( float *, float *, float *,
- long, long, float *, long *, long );
- /*======================================================================*/
- static void mod_nb_abs_lsp_quantizer (
- float current_lsp[], /* in: current LSP to be quantized */
- float previous_Qlsp[], /* In: previous Quantised LSP */
- float current_Qlsp[], /* out: quantized LSP */
- long lpc_indices[], /* out: LPC code indices */
- long lpc_order, /* in: order of LPC */
- long num_lpc_indices, /* in: number of LPC indices */
- long n_lpc_analysis /* in: number of LP analysis per frame */
- );
- /*======================================================================*/
- static void mod_nec_bws_lsp_quantizer(
- float lsp[], /* input */
- float qlsp8[], /* input */
- float qlsp[], /* output */
- long indices[], /* output */
- long lpc_order, /* configuration input */
- long lpc_order_8, /* configuration input */
- long num_lpc_indices, /* configuration input */
- float blsp[NEC_LSPPRDCT_ORDER][NEC_MAX_LSPVQ_ORDER],
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- );
- /*======================================================================*/
- static void mod_nb_abs_lsp_decode(
- unsigned long lpc_indices[], /* in: LPC code indices */
- float prev_Qlsp[], /* in: previous LSP vector */
- float current_Qlsp[], /* out: quantized LSP vector */
- long lpc_order, /* in: order of LPC */
- long num_lpc_indices /* in: number of LPC indices */
- );
- /*======================================================================*/
- static void mod_nec_bws_lsp_decoder(
- unsigned long indices[], /* input */
- float qlsp8[], /* input */
- float qlsp[], /* output */
- long lpc_order, /* configuration input */
- long lpc_order_8, /* configuration input */
- long num_lpc_indices, /* configuration input */
- float blsp[NEC_LSPPRDCT_ORDER][NEC_MAX_LSPVQ_ORDER],
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- );
- /*======================================================================*/
- static void mod_wb_celp_lsp_quantizer (
- float current_lsp[], /* in: current LSP to be quantized */
- float previous_Qlsp[], /* In: previous Quantised LSP */
- float current_Qlsp[], /* out: quantized LSP */
- long lpc_indices[], /* out: LPC code indices */
- long lpc_order, /* in: order of LPC */
- long num_lpc_indices, /* in: number of LPC indices */
- long n_lpc_analysis /* in: number of LP analysis per frame */
- );
- /*======================================================================*/
- static void mod_wb_celp_lsp_decode(
- unsigned long lpc_indices[], /* in: LPC code indices */
- float prev_Qlsp[], /* in: previous LSP vector */
- float current_Qlsp[], /* out: quantized LSP vector */
- long lpc_order, /* in: order of LPC */
- long num_lpc_indices /* in: number of LPC indices */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_Apar2Rfc */
- /*======================================================================*/
- static void PHI_Apar2Rfc
- (
- int P, /* In: LPC Order */
- float ap[], /* In: Polynomial coefficients [0..P-1] */
- float rfc[] /* Out: Reflection coefficients [0..P-1] */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_Rfc2Apar */
- /*======================================================================*/
- static void PHI_Rfc2Apar
- (
- int P, /* In: LPC Order */
- float rq[], /* In: Reflection coefficients [0..P-1] */
- float aq[] /* Out: a-parameters [0..P-1] */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_CalcAcf */
- /*======================================================================*/
- static void PHI_CalcAcf
- (
- double sp_in[], /* In: Input segment [0..N-1] */
- double acf[], /* Out: Autocorrelation coeffs [0..P] */
- int N, /* In: Number of samples in the segment */
- int P /* In: LPC Order */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_LevinsonDurbin */
- /*======================================================================*/
- static int PHI_LevinsonDurbin /* Retun Value: 1 if unstable filter */
- (
- double acf[], /* In: Autocorrelation coeffs [0..P] */
- double apar[], /* Out: Polynomial coeffciients [0..P-1] */
- double rfc[], /* Out: Reflection coefficients [0..P-1] */
- int P, /* In: LPC Order */
- double * E /* Out: Residual energy at the last stage */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_Rfc2Lar */
- /*======================================================================*/
- static void PHI_Rfc2Lar
- (
- int P, /* In: LPC Order */
- float rfc[], /* In: Array of reflection coeffs[0..P-1] */
- float lar[] /* Out: Array of log area ratios[0..P-1] */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_Lar2Rfc */
- /*======================================================================*/
- static int PHI_Lar2Rfc
- (
- int P, /* In: LPC Order */
- float lar[], /* In: Array of log area ratios[0..P-1] */
- float rfc[] /* Out: Array of reflection coeffs[0..P-1] */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_PackLpcCodes */
- /*======================================================================*/
- static void PHI_PackLpcCodes
- (
- const long lpc_order, /*In: Order of LPC */
- const long rfc_code[], /*In: Array of Refl. Coeffs[0..order-1] */
- const long pack_array_size, /*In: Array size after packing */
- long packed_code[] /*Out: Packed Array[0..pack_array-size-1] */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_UnpackLpcCodes */
- /*======================================================================*/
- static void PHI_UnpackLpcCodes
- (
- const long lpc_order, /*In: Order of LPC */
- long rfc_code[], /*Out: Array of Refl. Coeffs[0..order-1] */
- const long pack_array_size, /*In: Array size after packing */
- const long packed_code[] /*In: Packed Array[0..pack_array-size-1] */
- );
- /*======================================================================*/
- /* Function Prototype: PHI_lpc_analysis */
- /*======================================================================*/
- void PHI_lpc_analysis
- (
- float PP_InputSignal[], /* In: Input Signal */
- float lpc_coefficients[], /* Out: LPC Coefficients[0..lpc_order-1]*/
- float *first_order_lpc_par, /* Out: a_parameter for 1st-order fit */
- long frame_size, /* In: Number of samples in frame */
- float HamWin[], /* In: Hamming Window */
- long window_offset, /* In: offset for window w.r.t curr. fr*/
- long window_size, /* In: LPC Analysis-Window Size */
- float gamma_be[], /* In: Bandwidth expansion coeffs. */
- long lpc_order /* In: Order of LPC */
- );
- /*======================================================================*/
- /* Function Definition:PHI_InitLpcAnalysisEncoder */
- /*======================================================================*/
- void PHI_InitLpcAnalysisEncoder
- (
- long win_size[], /* In: LPC Analysis-Window Size */
- long n_lpc_analysis, /* In: Numberof LPC Analysis Frame */
- long order, /* In: Order of LPC */
- long order_8, /* In: Order of LPC */
- float gamma_be, /* In: Bandwidth Expansion Coefficient */
- long bit_rate, /* In: Bit Rate */
- long sampling_frequency, /* In: Sampling Frequency */
- long SampleRateMode, /* In: SampleRateMode */
- long frame_size, /* In: Frame Size */
- long num_lpc_indices, /* In: Number of LPC indices */
- long n_subframes, /* In: Number of subframes */
- long num_shape_cbks, /* In: Number of Shape Codebooks */
- long num_gain_cbks, /* In: Number of Gain Codebooks */
- long frame_bit_allocation[], /* In: Frame bit allocation */
- long num_indices,
- long QuantizationMode,
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- )
- {
- int x;
- int i,j;
- /* -----------------------------------------------------------------*/
- /* Create Arrays for Hamming Window and gamma array */
- /* -----------------------------------------------------------------*/
- if
- (
- (( PHI_Priv->PHI_mem_i = (float *)malloc((unsigned int)order * sizeof(float))) == NULL ) ||
- (( PHI_Priv->PHI_current_lar = (float *)malloc((unsigned int)order * sizeof(float))) == NULL )||
- (( PHI_Priv->PHI_prev_lar = (float *)malloc((unsigned int)order * sizeof(float))) == NULL )||
- (( PHI_Priv->PHI_prev_indices = (long *)malloc((unsigned int)order * sizeof(long))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine InitLpcAnalysis n");
- exit(1);
- }
- /* -----------------------------------------------------------------*/
- /* Create Array for Quantiser BLSP */
- /* -----------------------------------------------------------------*/
- for ( i = 0; i < NEC_LSPPRDCT_ORDER; i++ )
- {
- for ( j = 0; j < (int)order; j++ )
- {
- if ( j >= (int)(order/2) )
- PHI_Priv->blsp_previous[i][j]=(float)NEC_PAI/(float)(order+1)*(j+1);
- else
- PHI_Priv->blsp_previous[i][j]=(float)0.0;
- }
- }
- /* -----------------------------------------------------------------*/
- /* Create Array for Quantiser (narrowband) */
- /* -----------------------------------------------------------------*/
- if((PHI_Priv->previous_q_lsf_8=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->previous_q_lsf_8+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- if((PHI_Priv->current_q_lsf_8=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->current_q_lsf_8+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- if((PHI_Priv->next_q_lsf_8=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->next_q_lsf_8+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- if((PHI_Priv->previous_uq_lsf_8=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->previous_uq_lsf_8+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- if((PHI_Priv->current_uq_lsf_8=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->current_uq_lsf_8+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- if((PHI_Priv->next_uq_lsf_8=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->next_uq_lsf_8+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- if((PHI_Priv->previous_q_lsf_int_8=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->previous_q_lsf_int_8+i) = ((i+1)/(float)((order_8)+1)) * PAN_PI;
- /* -----------------------------------------------------------------*/
- /* Create Array for Quantiser (wideband) */
- /* -----------------------------------------------------------------*/
- if((PHI_Priv->previous_q_lsf_int_16=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->previous_q_lsf_int_16+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- if((PHI_Priv->previous_q_lsf_16=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->previous_q_lsf_16+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- if((PHI_Priv->current_q_lsf_16=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->current_q_lsf_16+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- if((PHI_Priv->next_q_lsf_16=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->next_q_lsf_16+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- if((PHI_Priv->previous_uq_lsf_16=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->previous_uq_lsf_16+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- if((PHI_Priv->current_uq_lsf_16=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->current_uq_lsf_16+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- if((PHI_Priv->next_uq_lsf_16=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->next_uq_lsf_16+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- /* -----------------------------------------------------------------*/
- /* Initialise arrays */
- /* -----------------------------------------------------------------*/
- for(x=0; x < (int)order; x++)
- {
- PHI_Priv->PHI_mem_i[x] = PHI_Priv->PHI_prev_lar[x] = PHI_Priv->PHI_current_lar[x] = (float)0.0;
- PHI_Priv->PHI_prev_indices[x] = (long)(21 - PHI_tbl_rfc_range[0][x]);
- }
- /* -----------------------------------------------------------------*/
- /* Initialise variables for dynamic threshold */
- /* -----------------------------------------------------------------*/
- {
- long bits_frame;
- float per_lpc;
- long k;
- /* ------------------------------------------------------------*/
- /* Determine length of LPC frames and no LPC frames */
- /* ------------------------------------------------------------*/
- PHI_Priv->PHI_MAX_BITS = 0;
- for (k = 0; k < (2 + num_lpc_indices + n_subframes*(num_shape_cbks+num_gain_cbks)); k++)
- {
- PHI_Priv->PHI_MAX_BITS += frame_bit_allocation[k];
- }
- PHI_Priv->PHI_MIN_BITS = PHI_Priv->PHI_MAX_BITS;
- for (k = 0; k < num_lpc_indices; k++)
- {
- PHI_Priv->PHI_MIN_BITS -= frame_bit_allocation[k+2];
- }
- PHI_Priv->PHI_FR = ((float)frame_size / (float)sampling_frequency);
- /*-------------------------------------------------------------*/
- /* How many bits per frame are needed to achieve fixed bit rate*/
- /* equal to what user has input */
- /*-------------------------------------------------------------*/
- bits_frame = (long)((float)bit_rate * PHI_Priv->PHI_FR + 0.5F);
- /* ------------------------------------------------------------*/
- /* Store desired bit rate */
- /* ------------------------------------------------------------*/
- PHI_Priv->PHI_desired_bit_rate = (long) ((float) bits_frame / PHI_Priv->PHI_FR + 0.5F);
- PHI_Priv->PHI_actual_bits = 0;
- /*-------------------------------------------------------------*/
- /* Needed %-lpc to get the desired bit rate */
- /*-------------------------------------------------------------*/
- per_lpc = ((float)(bits_frame-PHI_Priv->PHI_MIN_BITS))/((float)(PHI_Priv->PHI_MAX_BITS-PHI_Priv->PHI_MIN_BITS));
- /*-------------------------------------------------------------*/
- /* Choose initial value for distance threshold */
- /*-------------------------------------------------------------*/
- if (per_lpc < 0.51F)
- {
- PHI_Priv->PHI_dyn_lpc_thresh = 1.0F;
- }
- else
- if (per_lpc < 0.6F)
- {
- PHI_Priv->PHI_dyn_lpc_thresh = 0.25F;
- }
- else
- if (per_lpc < 0.75F)
- {
- PHI_Priv->PHI_dyn_lpc_thresh = 0.15F;
- }
- else
- if (per_lpc < 0.85F)
- {
- PHI_Priv->PHI_dyn_lpc_thresh = 0.1F;
- }
- else
- if (per_lpc < 0.97F)
- {
- PHI_Priv->PHI_dyn_lpc_thresh = 0.05F;
- }
- else
- {
- PHI_Priv->PHI_dyn_lpc_thresh = 0.0F;
- }
- /*-------------------------------------------------------------*/
- /* Compute number of frames per second */
- /*-------------------------------------------------------------*/
- PHI_Priv->PHI_FRAMES = (long)((float)sampling_frequency/(float)frame_size + 0.5F);
- /*-------------------------------------------------------------*/
- /* Determine the stop threshold */
- /*-------------------------------------------------------------*/
- PHI_Priv->PHI_stop_threshold = (float)0.35;
- }
- }
- /*======================================================================*/
- /* Function Definition:PAN_InitLpcAnalysisEncoder */
- /* Nov. 07 96 - Added for narrowband coder */
- /*======================================================================*/
- void PAN_InitLpcAnalysisEncoder
- (
- long win_size[], /* In: LPC Analysis-Window Size */
- long n_lpc_analysis, /* In: Numberof LPC Analysis Frame */
- long order, /* In: Order of LPC */
- float gamma_be, /* In: Bandwidth Expansion Coefficient */
- long bit_rate, /* In: Bit Rate */
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- )
- {
- /* -----------------------------------------------------------------*/
- /* Create Arrays for Hamming Window and gamma array */
- /* -----------------------------------------------------------------*/
- if
- (
- (( PHI_Priv->PHI_mem_i = (float *)malloc((unsigned int)order * sizeof(float))) == NULL)
- )
- {
- printf("MALLOC FAILURE in Routine InitLpcAnalysis n");
- exit(1);
- }
- }
- /*======================================================================*/
- /* Function Definition:PHI_InitLpcAnalysisDecoder */
- /*======================================================================*/
- void PHI_InitLpcAnalysisDecoder
- (
- long order, /* In: Order of LPC */
- long order_8, /* In: Order of LPC */
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- )
- {
- int x;
- int i,j ;
- /* -----------------------------------------------------------------*/
- /* Create Arrays for lars in the decoder */
- /* -----------------------------------------------------------------*/
- if
- (
- (( PHI_Priv->PHI_mem_s = (float *)malloc((unsigned int)order * sizeof(float))) == NULL ) ||
- (( PHI_Priv->PHI_dec_current_lar = (float *)malloc((unsigned int)order * sizeof(float))) == NULL )||
- (( PHI_Priv->PHI_dec_prev_lar = (float *)malloc((unsigned int)order * sizeof(float))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine InitLpcAnalysis n");
- exit(1);
- }
- /* -----------------------------------------------------------------*/
- /* Create Array for Quantiser BLSP */
- /* -----------------------------------------------------------------*/
- for ( i = 0; i < NEC_LSPPRDCT_ORDER; i++ )
- {
- for ( j = 0; j < (int)order; j++ )
- {
- if ( j >= (int)(order/2) )
- PHI_Priv->blsp_dec[i][j]=(float)NEC_PAI/(float)(order+1)*(j+1);
- else
- PHI_Priv->blsp_dec[i][j]=(float)0.0;
- }
- }
- /* -----------------------------------------------------------------*/
- /* Create Array for Quantiser (narrowband) */
- /* -----------------------------------------------------------------*/
- if((PHI_Priv->previous_q_lsf_8_dec=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->previous_q_lsf_8_dec+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- if((PHI_Priv->current_q_lsf_8_dec=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->current_q_lsf_8_dec+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- if((PHI_Priv->next_q_lsf_8_dec=(float *)calloc(order_8, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order_8;i++)
- *(PHI_Priv->next_q_lsf_8_dec+i) = ((i+1)/(float)((order_8)+1))*PAN_PI;
- /* -----------------------------------------------------------------*/
- /* Create Array for Quantiser (wideband) */
- /* -----------------------------------------------------------------*/
- if((PHI_Priv->previous_q_lsf_16_dec=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->previous_q_lsf_16_dec+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- if((PHI_Priv->current_q_lsf_16_dec=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->current_q_lsf_16_dec+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- if((PHI_Priv->next_q_lsf_16_dec=(float *)calloc(order, sizeof(float)))==NULL) {
- printf("n memory allocation error in initialization_encodern");
- exit(1);
- }
- for(i=0;i<order;i++)
- *(PHI_Priv->next_q_lsf_16_dec+i) = ((i+1)/(float)((order)+1)) * PAN_PI;
- /* -----------------------------------------------------------------*/
- /* Initialise arrays */
- /* -----------------------------------------------------------------*/
- for(x=0; x < (int)order; x++)
- {
- PHI_Priv->PHI_mem_s[x] = PHI_Priv->PHI_dec_prev_lar[x] = PHI_Priv->PHI_dec_current_lar[x] = (float)0.0;
- }
- }
- /*======================================================================*/
- /* Function Definition: PHI_FreeLpcAnalysisEncoder */
- /*======================================================================*/
- void PHI_FreeLpcAnalysisEncoder
- (
- long n_lpc_analysis,
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- )
- {
- /* -----------------------------------------------------------------*/
- /*FREE the arrays that were malloc'd */
- /* -----------------------------------------------------------------*/
- FREE(PHI_Priv->next_uq_lsf_16);
- FREE(PHI_Priv->current_uq_lsf_16);
- FREE(PHI_Priv->previous_uq_lsf_16);
- FREE(PHI_Priv->next_q_lsf_16);
- FREE(PHI_Priv->current_q_lsf_16);
- FREE(PHI_Priv->previous_q_lsf_16);
- FREE(PHI_Priv->previous_q_lsf_int_16);
- FREE(PHI_Priv->previous_q_lsf_int_8);
- FREE(PHI_Priv->next_uq_lsf_8);
- FREE(PHI_Priv->current_uq_lsf_8);
- FREE(PHI_Priv->previous_uq_lsf_8);
- FREE(PHI_Priv->next_q_lsf_8);
- FREE(PHI_Priv->current_q_lsf_8);
- FREE(PHI_Priv->previous_q_lsf_8);
- FREE(PHI_Priv->PHI_mem_i);
- FREE(PHI_Priv->PHI_prev_lar);
- FREE(PHI_Priv->PHI_prev_indices);
- FREE(PHI_Priv->PHI_current_lar);
- }
- /*======================================================================*/
- /* Function Definition: PAN_FreeLpcAnalysisEncoder */
- /* Nov. 07 96 - Added for narrowband coder */
- /*======================================================================*/
- void PAN_FreeLpcAnalysisEncoder
- (
- long n_lpc_analysis,
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- )
- {
- /* -----------------------------------------------------------------*/
- /*FREE the arrays that were malloc'd */
- /* -----------------------------------------------------------------*/
- FREE(PHI_Priv->PHI_mem_i);
- }
- /*======================================================================*/
- /* Function Definition: PHI_FreeLpcAnalysisDecoder */
- /*======================================================================*/
- void PHI_FreeLpcAnalysisDecoder
- (
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- )
- {
- /* -----------------------------------------------------------------*/
- /*FREE the arrays that were malloc'd */
- /* -----------------------------------------------------------------*/
- FREE(PHI_Priv->next_q_lsf_16_dec);
- FREE(PHI_Priv->current_q_lsf_16_dec);
- FREE(PHI_Priv->previous_q_lsf_16_dec);
- FREE(PHI_Priv->next_q_lsf_8_dec);
- FREE(PHI_Priv->current_q_lsf_8_dec);
- FREE(PHI_Priv->previous_q_lsf_8_dec);
- FREE(PHI_Priv->PHI_mem_s);
- FREE(PHI_Priv->PHI_dec_prev_lar);
- FREE(PHI_Priv->PHI_dec_current_lar);
- }
- /*======================================================================*/
- /* Function Definition:celp_lpc_analysis */
- /*======================================================================*/
- void celp_lpc_analysis
- (
- float PP_InputSignal[], /* In: Input Signal */
- float lpc_coefficients[], /* Out: LPC Coefficients[0..lpc_order-1]*/
- float *first_order_lpc_par, /* Out: a_parameter for 1st-order fit */
- long frame_size, /* In: Number of samples in frame */
- long window_offsets[], /* In: offset for window w.r.t curr. fr*/
- long window_sizes[], /* In: LPC Analysis-Window Size */
- float *windows[], /* In: Array of LPC Analysis windows */
- float gamma_be[], /* In: Bandwidth expansion coefficients*/
- long lpc_order, /* In: Order of LPC */
- long n_lpc_analysis /* In: Number of LP analysis/frame */
- )
- {
- int i;
- for(i = 0; i < (int)n_lpc_analysis; i++)
- {
- PHI_lpc_analysis(PP_InputSignal,lpc_coefficients+lpc_order*(long)i,
- first_order_lpc_par,
- frame_size, windows[i], window_offsets[i], window_sizes[i],
- gamma_be,
- lpc_order);
- }
- }
- /*======================================================================*/
- /* Function Definition:PHI_lpc_analysis */
- /*======================================================================*/
- void PHI_lpc_analysis
- (
- float PP_InputSignal[], /* In: Input Signal */
- float lpc_coefficients[], /* Out: LPC Coefficients[0..lpc_order-1]*/
- float *first_order_lpc_par, /* Out: a_parameter for 1st-order fit */
- long frame_size, /* In: Number of samples in frame */
- float HamWin[], /* In: Hamming Window */
- long window_offset, /* In: offset for window w.r.t curr. fr*/
- long window_size, /* In: LPC Analysis-Window Size */
- float gamma_be[], /* In: Bandwidth expansion coeffs. */
- long lpc_order /* In: Order of LPC */
- )
- {
- /*------------------------------------------------------------------*/
- /* Volatile Variables */
- /* -----------------------------------------------------------------*/
- double *acf; /* For Autocorrelation Function */
- double *reflection_coeffs; /* For Reflection Coefficients */
- double *LpcAnalysisBlock; /* For Windowed Input Signal */
- double *apars; /* a-parameters of double precision */
- int k;
- /*------------------------------------------------------------------*/
- /* Allocate space for lpc, acf, a-parameters and rcf */
- /*------------------------------------------------------------------*/
- if
- (
- (( reflection_coeffs = (double *)malloc((unsigned int)lpc_order * sizeof(double))) == NULL ) ||
- (( acf = (double *)malloc((unsigned int)(lpc_order + 1) * sizeof(double))) == NULL ) ||
- (( apars = (double *)malloc((unsigned int)(lpc_order) * sizeof(double))) == NULL ) ||
- (( LpcAnalysisBlock = (double *)malloc((unsigned int)window_size * sizeof(double))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine abs_lpc_analysis n");
- exit(1);
- }
- /*------------------------------------------------------------------*/
- /* Windowing of the input signal */
- /*------------------------------------------------------------------*/
- for(k = 0; k < (int)window_size; k++)
- {
- LpcAnalysisBlock[k] = (double)PP_InputSignal[k + (int)window_offset] * (double)HamWin[k];
- }
- /*------------------------------------------------------------------*/
- /* Compute Autocorrelation */
- /*------------------------------------------------------------------*/
- PHI_CalcAcf(LpcAnalysisBlock, acf, (int)window_size, (int)lpc_order);
- /*------------------------------------------------------------------*/
- /* Levinson Recursion */
- /*------------------------------------------------------------------*/
- {
- double Energy = 0.0;
- PHI_LevinsonDurbin(acf, apars, reflection_coeffs,(int)lpc_order,&Energy);
- }
- /*------------------------------------------------------------------*/
- /* First-Order LPC Fit */
- /*------------------------------------------------------------------*/
- *first_order_lpc_par = (float)reflection_coeffs[0];
- /*------------------------------------------------------------------*/
- /* Bandwidth Expansion */
- /*------------------------------------------------------------------*/
- for(k = 0; k < (int)lpc_order; k++)
- {
- lpc_coefficients[k] = (float)apars[k] * gamma_be[k];
- }
- /*------------------------------------------------------------------*/
- /*FREE the arrays that were malloced */
- /*------------------------------------------------------------------*/
- FREE(LpcAnalysisBlock);
- FREE(reflection_coeffs);
- FREE(acf);
- FREE(apars);
- }
- /*======================================================================*/
- /* Function Definition:VQ_celp_lpc_decode */
- /*======================================================================*/
- void VQ_celp_lpc_decode
- (
- unsigned long lpc_indices[], /* In: Received Packed LPC Codes */
- float int_Qlpc_coefficients[], /* Out: Qaunt/interpolated a-pars */
- long lpc_order, /* In: Order of LPC */
- long num_lpc_indices, /* In: Number of packes LPC codes */
- long n_subframes, /* In: Number of subframes */
- unsigned long interpolation_flag, /* In: Was interpolation done? */
- long Wideband_VQ, /* In: Wideband VQ switch */
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- )
- {
- if (lpc_order == ORDER_LPC_8)
- { /* 8 khz */
- float *int_lsf;
- int s,k;
- float *tmp_lpc_coefficients;
- /*------------------------------------------------------------------*/
- /* Allocate space for current_rfc and current lar */
- /*------------------------------------------------------------------*/
- if
- (
- (( int_lsf = (float *)malloc((unsigned int)lpc_order * sizeof(float))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine abs_lpc_quantizer n");
- exit(1);
- }
- if((tmp_lpc_coefficients=(float *)calloc(lpc_order+1, sizeof(float)))==NULL) {
- printf("n Memory allocation error in abs_lpc_quantizern");
- exit(4);
- }
- if (interpolation_flag)
- {
- /*----------------------------------------------------------*/
- /* Compute LSP coeffs of the next frame */
- /*----------------------------------------------------------*/
- mod_nb_abs_lsp_decode(lpc_indices, PHI_Priv->previous_q_lsf_8_dec,
- PHI_Priv->next_q_lsf_8_dec, lpc_order, 5);
- /*------------------------------------------------------------------*/
- /* Interpolate LSP coeffs to obtain coeficients of cuurent frame */
- /*------------------------------------------------------------------*/
- for(k = 0; k < (int)lpc_order; k++)
- {
- PHI_Priv->current_q_lsf_8_dec[k] =
- (float)0.5 * (PHI_Priv->previous_q_lsf_8_dec[k] + PHI_Priv->next_q_lsf_8_dec[k]);
- }
- }
- else
- {
- if (PHI_Priv->PHI_dec_prev_interpolation_flag == 0)
- {
- /*--------------------------------------------------------------*/
- /* Compute LSP coeffs of the current frame */
- /*--------------------------------------------------------------*/
- mod_nb_abs_lsp_decode(lpc_indices, PHI_Priv->previous_q_lsf_8_dec,
- PHI_Priv->current_q_lsf_8_dec,
- lpc_order, 5);
- }
- else
- {
- /* LSPs received in the previous frame: do nothing */
- ;
- }
- }
- /*------------------------------------------------------------------*/
- /* Interpolation Procedure on LSPs */
- /*------------------------------------------------------------------*/
- for(s = 0; s < (int)n_subframes; s++)
- {
- for(k = 0; k < (int)lpc_order; k++)
- {
- register float tmp;
- tmp = (((float)((int)n_subframes - s - 1) * PHI_Priv->previous_q_lsf_8_dec[k])
- + ((float)(1 + s) * PHI_Priv->current_q_lsf_8_dec[k]))/(float)n_subframes;
- int_lsf[k] = tmp;
- }
- /*--------------------------------------------------------------*/
- /*Compute corresponding a-parameters */
- /*--------------------------------------------------------------*/
- lsf2pc(tmp_lpc_coefficients, int_lsf, lpc_order);
- for( k = 0; k < lpc_order; k++)
- {
- int_Qlpc_coefficients[s*(int)lpc_order + k] = -tmp_lpc_coefficients[k+1];
- }
- }
- /* -----------------------------------------------------------------*/
- /* Save memory */
- /* -----------------------------------------------------------------*/
- for(k=0; k < (int)lpc_order; k++)
- {
- PHI_Priv->previous_q_lsf_8_dec[k] = PHI_Priv->current_q_lsf_8_dec[k];
- PHI_Priv->current_q_lsf_8_dec[k] = PHI_Priv->next_q_lsf_8_dec[k];
- }
- PHI_Priv->PHI_dec_prev_interpolation_flag = interpolation_flag;
- /* -----------------------------------------------------------------*/
- /*FREE the malloc'd arrays */
- /* -----------------------------------------------------------------*/
- free(int_lsf);
- free(tmp_lpc_coefficients);
- }
- else
- { /* 16 khz */
- float *int_lsf;
- int s,k;
- float *tmp_lpc_coefficients;
- if (Wideband_VQ == Scalable_VQ)
- {
- /* 16 khz Scalable VQ */
- /*------------------------------------------------------------------*/
- /* Allocate space for current_rfc and current lar */
- /*------------------------------------------------------------------*/
- if
- (
- (( int_lsf = (float *)malloc((unsigned int)lpc_order * sizeof(float))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine abs_lpc_quantizer n");
- exit(1);
- }
- if((tmp_lpc_coefficients=(float *)calloc(lpc_order+1, sizeof(float)))==NULL) {
- printf("n Memory allocation error in abs_lpc_quantizern");
- exit(4);
- }
- if (interpolation_flag)
- {
- /*----------------------------------------------------------*/
- /* Compute LSP coeffs of the next frame */
- /*----------------------------------------------------------*/
- mod_nb_abs_lsp_decode(lpc_indices, PHI_Priv->previous_q_lsf_8_dec,
- PHI_Priv->next_q_lsf_8_dec,
- lpc_order/2, 5);
- mod_nec_bws_lsp_decoder( lpc_indices + 5, PHI_Priv->next_q_lsf_8_dec,
- PHI_Priv->next_q_lsf_16_dec,
- lpc_order, lpc_order/2, 6, PHI_Priv->blsp_dec, PHI_Priv);
- /*------------------------------------------------------------------*/
- /* Interpolate LSP coeffs to obtain coeficients of cuurent frame */
- /*------------------------------------------------------------------*/
- for(k = 0; k < (int)lpc_order; k++)
- {
- PHI_Priv->current_q_lsf_16_dec[k] =
- (float)0.5 * (PHI_Priv->previous_q_lsf_16_dec[k] + PHI_Priv->next_q_lsf_16_dec[k]);
- }
- }
- else
- {
- if (PHI_Priv->PHI_dec_prev_interpolation_flag == 0)
- {
- /*--------------------------------------------------------------*/
- /* Compute LSP coeffs of the current frame */
- /*--------------------------------------------------------------*/
- mod_nb_abs_lsp_decode(lpc_indices, PHI_Priv->previous_q_lsf_8_dec,
- PHI_Priv->current_q_lsf_8_dec,
- lpc_order/2, 5);
- mod_nec_bws_lsp_decoder( lpc_indices + 5, PHI_Priv->current_q_lsf_8_dec,
- PHI_Priv->current_q_lsf_16_dec,
- lpc_order, lpc_order/2, 6, PHI_Priv->blsp_dec, PHI_Priv);
- }
- else
- {
- /* LSPs received in the previous frame: do nothing */
- ;
- }
- }
- /*------------------------------------------------------------------*/
- /* Interpolation Procedure on LSPs */
- /*------------------------------------------------------------------*/
- for(s = 0; s < (int)n_subframes; s++)
- {
- for(k = 0; k < (int)lpc_order; k++)
- {
- register float tmp;
- tmp = (((float)((int)n_subframes - s - 1) * PHI_Priv->previous_q_lsf_16_dec[k])
- + ((float)(1 + s) * PHI_Priv->current_q_lsf_16_dec[k]))/(float)n_subframes;
- int_lsf[k] = tmp;
- }
- /*--------------------------------------------------------------*/
- /*Compute corresponding a-parameters */
- /*--------------------------------------------------------------*/
- lsf2pc(tmp_lpc_coefficients, int_lsf, lpc_order);
- for( k = 0; k < lpc_order; k++)
- {
- int_Qlpc_coefficients[s*(int)lpc_order + k] = -tmp_lpc_coefficients[k+1];
- }
- }
- /* -----------------------------------------------------------------*/
- /* Save memory */
- /* -----------------------------------------------------------------*/
- for(k=0; k < (int)lpc_order/2; k++)
- {
- PHI_Priv->previous_q_lsf_8_dec[k] = PHI_Priv->current_q_lsf_8_dec[k];
- PHI_Priv->current_q_lsf_8_dec[k] = PHI_Priv->next_q_lsf_8_dec[k];
- }
- for(k=0; k < (int)lpc_order; k++)
- {
- PHI_Priv->previous_q_lsf_16_dec[k] = PHI_Priv->current_q_lsf_16_dec[k];
- PHI_Priv->current_q_lsf_16_dec[k] = PHI_Priv->next_q_lsf_16_dec[k];
- }
- PHI_Priv->PHI_dec_prev_interpolation_flag = interpolation_flag;
- /* -----------------------------------------------------------------*/
- /*FREE the malloc'd arrays */
- /* -----------------------------------------------------------------*/
- FREE(int_lsf);
- FREE(tmp_lpc_coefficients);
- }
- else
- { /* 16 khz Optimized VQ */
- float *int_lsf;
- int s,k;
- float *tmp_lpc_coefficients;
- /*------------------------------------------------------------------*/
- /* Allocate space for current_rfc and current lar */
- /*------------------------------------------------------------------*/
- if
- (
- (( int_lsf = (float *)malloc((unsigned int)lpc_order * sizeof(float))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine abs_lpc_quantizer n");
- exit(1);
- }
- if((tmp_lpc_coefficients=(float *)calloc(lpc_order+1, sizeof(float)))==NULL) {
- printf("n Memory allocation error in abs_lpc_quantizern");
- exit(4);
- }
- if (interpolation_flag)
- {
- /*----------------------------------------------------------*/
- /* Compute LSP coeffs of the next frame */
- /*----------------------------------------------------------*/
- mod_wb_celp_lsp_decode(lpc_indices, PHI_Priv->previous_q_lsf_16_dec,
- PHI_Priv->next_q_lsf_16_dec, lpc_order,
- num_lpc_indices);
- /*------------------------------------------------------------------*/
- /* Interpolate LSP coeffs to obtain coeficients of cuurent frame */
- /*------------------------------------------------------------------*/
- for(k = 0; k < (int)lpc_order; k++)
- {
- PHI_Priv->current_q_lsf_16_dec[k] =
- (float)0.5 * (PHI_Priv->previous_q_lsf_16_dec[k] + PHI_Priv->next_q_lsf_16_dec[k]);
- }
- }
- else
- {
- if (PHI_Priv->PHI_dec_prev_interpolation_flag == 0)
- {
- /*--------------------------------------------------------------*/
- /* Compute LSP coeffs of the current frame */
- /*--------------------------------------------------------------*/
- mod_wb_celp_lsp_decode(lpc_indices,
- PHI_Priv->previous_q_lsf_16_dec,
- PHI_Priv->current_q_lsf_16_dec,
- lpc_order, num_lpc_indices);
- }
- else
- {
- /* LSPs received in the previous frame: do nothing */
- ;
- }
- }
- /*------------------------------------------------------------------*/
- /* Interpolation Procedure on LSPs */
- /*------------------------------------------------------------------*/
- for(s = 0; s < (int)n_subframes; s++)
- {
- for(k = 0; k < (int)lpc_order; k++)
- {
- register float tmp;
- tmp = (((float)((int)n_subframes - s - 1) * PHI_Priv->previous_q_lsf_16_dec[k])
- + ((float)(1 + s) * PHI_Priv->current_q_lsf_16_dec[k]))/(float)n_subframes;
- int_lsf[k] = tmp;
- }
- /*--------------------------------------------------------------*/
- /*Compute corresponding a-parameters */
- /*--------------------------------------------------------------*/
- lsf2pc(tmp_lpc_coefficients, int_lsf, lpc_order);
- for( k = 0; k < lpc_order; k++)
- {
- int_Qlpc_coefficients[s*(int)lpc_order + k] = -tmp_lpc_coefficients[k+1];
- }
- }
- /* -----------------------------------------------------------------*/
- /* Save memory */
- /* -----------------------------------------------------------------*/
- for(k=0; k < (int)lpc_order; k++)
- {
- PHI_Priv->previous_q_lsf_16_dec[k] = PHI_Priv->current_q_lsf_16_dec[k];
- PHI_Priv->current_q_lsf_16_dec[k] = PHI_Priv->next_q_lsf_16_dec[k];
- }
- PHI_Priv->PHI_dec_prev_interpolation_flag = interpolation_flag;
- /* -----------------------------------------------------------------*/
- /*FREE the malloc'd arrays */
- /* -----------------------------------------------------------------*/
- FREE (int_lsf);
- FREE (tmp_lpc_coefficients);
- }
- }
- }
- /*======================================================================*/
- /* Function Definition:celp_lpc_quantizer using VQ */
- /*======================================================================*/
- void VQ_celp_lpc_quantizer
- (
- float lpc_coefficients[], /* In: Current unquantised a-pars */
- float lpc_coefficients_8[], /* In: Current unquantised a-pars(8 kHz)*/
- float int_Qlpc_coefficients[], /* Out: Qaunt/interpolated a-pars */
- long lpc_indices[], /* Out: Codes thar are transmitted */
- long lpc_order, /* In: Order of LPC */
- long num_lpc_indices, /* In: Number of packes LPC codes */
- long n_lpc_analysis, /* In: Number of LPC/frame */
- long n_subframes, /* In: Number of subframes */
- long *interpolation_flag, /* Out: Interpolation Flag */
- long *send_lpc_flag, /* Out: Send LPC flag */
- long Wideband_VQ,
- PHI_PRIV_TYPE *PHI_Priv /* In/Out: PHI private data (instance context) */
- )
- {
- if (lpc_order == ORDER_LPC_8)
- { /* fs = 8 khz */
- float *int_lsf;
- long *next_codes;
- long *current_codes;
- float *tmp_lpc_coefficients;
- int i,s,k;
- /*------------------------------------------------------------------*/
- /* Allocate space for current_rfc and current lar */
- /*------------------------------------------------------------------*/
- if
- (
- (( int_lsf = (float *)malloc((unsigned int)lpc_order * sizeof(float))) == NULL ) ||
- (( next_codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL ) ||
- (( current_codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine abs_lpc_quantizer n");
- exit(1);
- }
- if((tmp_lpc_coefficients=(float *)calloc(lpc_order+1, sizeof(float)))==NULL) {
- printf("n Memory allocation error in abs_lpc_quantizern");
- exit(4);
- }
- /*------------------------------------------------------------------*/
- /* Calculate Narrowband LSPs */
- /*------------------------------------------------------------------*/
- tmp_lpc_coefficients[0] = 1.;
- for(i=0;i<lpc_order;i++)
- tmp_lpc_coefficients[i+1] =
- -lpc_coefficients[i];
- pc2lsf(PHI_Priv->next_uq_lsf_8, tmp_lpc_coefficients, lpc_order);
- /*------------------------------------------------------------------*/
- /* Narrowband Quantization Procedure */
- /*------------------------------------------------------------------*/
- mod_nb_abs_lsp_quantizer(PHI_Priv->next_uq_lsf_8, PHI_Priv->previous_q_lsf_8, PHI_Priv->next_q_lsf_8,
- next_codes, lpc_order, 5,
- n_lpc_analysis);
- /*------------------------------------------------------------------*/
- /* Narrowband Quantization Procedure */
- /*------------------------------------------------------------------*/
- mod_nb_abs_lsp_quantizer(PHI_Priv->current_uq_lsf_8, PHI_Priv->previous_q_lsf_8, PHI_Priv->current_q_lsf_8,
- current_codes, lpc_order, 5,
- n_lpc_analysis);
- /*------------------------------------------------------------------*/
- /* Determine dynamic threshold */
- /*------------------------------------------------------------------*/
- if (PHI_Priv->PHI_frames_sent == PHI_Priv->PHI_FRAMES)
- {
- long actual_bit_rate = (long)(((float)PHI_Priv->PHI_actual_bits)/(((float)PHI_Priv->PHI_frames_sent)*PHI_Priv->PHI_FR));
- float diff = (float)fabs((double)(PHI_Priv->PHI_desired_bit_rate - actual_bit_rate));
- float per_diff = diff/(float)PHI_Priv->PHI_desired_bit_rate;
- if (per_diff > (float)0.005)
- {
- long coeff = (long)(per_diff * (float)10.0+(float)0.5) + 1;
- if (actual_bit_rate > PHI_Priv->PHI_desired_bit_rate)
- PHI_Priv->PHI_dyn_lpc_thresh += (float)0.01 * (float)coeff;
- if (actual_bit_rate < PHI_Priv->PHI_desired_bit_rate)
- PHI_Priv->PHI_dyn_lpc_thresh -= (float)0.01 * (float)coeff;
- if (PHI_Priv->PHI_dyn_lpc_thresh < (float)0.0)
- PHI_Priv->PHI_dyn_lpc_thresh = (float)0.0;
- if (PHI_Priv->PHI_dyn_lpc_thresh > PHI_Priv->PHI_stop_threshold)
- PHI_Priv->PHI_dyn_lpc_thresh = PHI_Priv->PHI_stop_threshold;
- }
- PHI_Priv->PHI_frames_sent = 0;
- PHI_Priv->PHI_actual_bits = 0;
- }
- /*------------------------------------------------------------------*/
- /* Check if you really need to transmit an LPC set */
- /*------------------------------------------------------------------*/
- if (PHI_Priv->PHI_prev_int_flag)
- {
- *interpolation_flag = 0;
- if (!PHI_Priv->PHI_prev_lpc_flag)
- {
- *send_lpc_flag = 1;
- }
- else
- {
- *send_lpc_flag = 0;
- }
- }
- else
- {
- float d;
- float lpc_thresh = PHI_Priv->PHI_dyn_lpc_thresh;
- for(d = (float)0.0, k = 0; k < (int)lpc_order; k++)
- {
- double temp;
- temp = (double)(((float)0.5 * (PHI_Priv->previous_q_lsf_8[k] + PHI_Priv->next_q_lsf_8[k])) - PHI_Priv->current_q_lsf_8[k]);
- d += (float)fabs(temp);
- }
- d /= 2.0F;
- if (d < lpc_thresh)
- {
- *interpolation_flag = 1;
- *send_lpc_flag = 1;
- /*--------------------------------------------------------------*/
- /* Perform Interpolation */
- /*--------------------------------------------------------------*/
- for(k = 0; k < (int)lpc_order; k++)
- {
- PHI_Priv->current_q_lsf_8[k] = (float)0.5 * (PHI_Priv->previous_q_lsf_8[k] + PHI_Priv->next_q_lsf_8[k]);
- }
- }
- else
- {
- *interpolation_flag = 0;
- *send_lpc_flag = 1;
- }
- }
- /*------------------------------------------------------------------*/
- /* Make sure to copy the right indices for the bit stream */
- /*------------------------------------------------------------------*/
- if (*send_lpc_flag)
- {
- if (*interpolation_flag)
- {
- /* INTERPOLATION: use next indices (= indices of next frame) */
- for (k=0; k < (int)num_lpc_indices; k++)
- {
- lpc_indices[k] = next_codes[k];
- }
- }
- else
- {
- /* NO INTERPOLATION: use current indices (= indices of current frame) */
- for (k=0; k < (int)num_lpc_indices; k++)
- {
- lpc_indices[k] = current_codes[k];
- }
- }
- }
- else
- {
- int k;
- for (k=0; k < (int)num_lpc_indices; k++)
- {
- lpc_indices[k] = 0;
- }
- }
- PHI_Priv->PHI_prev_lpc_flag = *send_lpc_flag;
- PHI_Priv->PHI_prev_int_flag = *interpolation_flag;
- /*------------------------------------------------------------------*/
- /* Update the number of frames and bits that are output */
- /*------------------------------------------------------------------*/
- PHI_Priv->PHI_frames_sent += 1;
- if (*send_lpc_flag)
- {
- PHI_Priv->PHI_actual_bits += PHI_Priv->PHI_MAX_BITS;
- }
- else
- {
- PHI_Priv->PHI_actual_bits += PHI_Priv->PHI_MIN_BITS;
- }
- /*------------------------------------------------------------------*/
- /* Interpolation Procedure on LSPs */
- /*------------------------------------------------------------------*/
- for(s = 0; s < (int)n_subframes; s++)
- {
- for(k = 0; k < (int)lpc_order; k++)
- {
- register float tmp;
- tmp = (((float)((int)n_subframes - s - 1) * PHI_Priv->previous_q_lsf_int_8[k])
- + ((float)(1 + s) * PHI_Priv->current_q_lsf_8[k]))/(float)n_subframes;
- int_lsf[k] = tmp;
- }
- /*--------------------------------------------------------------*/
- /*Compute corresponding a-parameters */
- /*--------------------------------------------------------------*/
- lsf2pc(tmp_lpc_coefficients, int_lsf, lpc_order);
- for( k = 0; k < lpc_order; k++)
- {
- int_Qlpc_coefficients[s*(int)lpc_order + k] = -tmp_lpc_coefficients[k+1];
- }
- }
- /* -----------------------------------------------------------------*/
- /* Save memory */
- /* -----------------------------------------------------------------*/
- for(k=0; k < (int)lpc_order; k++)
- {
- PHI_Priv->previous_uq_lsf_8[k] = PHI_Priv->current_uq_lsf_8[k];
- PHI_Priv->current_uq_lsf_8[k] = PHI_Priv->next_uq_lsf_8[k];
- PHI_Priv->previous_q_lsf_int_8[k] = PHI_Priv->current_q_lsf_8[k];
- }
- if (*interpolation_flag)
- {
- ;
- }
- else
- {
- for(k=0; k < (int)lpc_order; k++)
- {
- PHI_Priv->previous_q_lsf_8[k] = PHI_Priv->current_q_lsf_8[k];
- PHI_Priv->current_q_lsf_8[k] = PHI_Priv->next_q_lsf_8[k];
- }
- }
- /* -----------------------------------------------------------------*/
- /*FREE the malloc'd arrays */
- /* -----------------------------------------------------------------*/
- FREE(int_lsf);
- FREE(next_codes);
- FREE(current_codes);
- FREE(tmp_lpc_coefficients);
- }
- else
- {
- if (Wideband_VQ==Scalable_VQ)
- { /* 16 khz Scalable VQ */
- float *int_lsf;
- long *next_codes;
- long *current_codes;
- float *tmp_lpc_coefficients;
- int i, j, s,k;
- float blsp_next[NEC_LSPPRDCT_ORDER][NEC_MAX_LSPVQ_ORDER];
- float blsp_current[NEC_LSPPRDCT_ORDER][NEC_MAX_LSPVQ_ORDER];
- /*------------------------------------------------------------------*/
- /* Allocate space for current_rfc and current lar */
- /*------------------------------------------------------------------*/
- if
- (
- (( int_lsf = (float *)malloc((unsigned int)lpc_order * sizeof(float))) == NULL ) ||
- (( next_codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL ) ||
- (( current_codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine abs_lpc_quantizer n");
- exit(1);
- }
- if((tmp_lpc_coefficients=(float *)calloc(lpc_order+1, sizeof(float)))==NULL) {
- printf("n Memory allocation error in abs_lpc_quantizern");
- exit(4);
- }
- /*------------------------------------------------------------------*/
- /* Copy memory for Wideband Quantizer */
- /*------------------------------------------------------------------*/
- for (i=0; i < NEC_LSPPRDCT_ORDER; i++)
- {
- for(j=0; j < NEC_MAX_LSPVQ_ORDER; j++)
- {
- blsp_next[i][j] = PHI_Priv->blsp_previous[i][j];
- blsp_current[i][j] = PHI_Priv->blsp_previous[i][j];
- }
- }
- /*------------------------------------------------------------------*/
- /* Calculate Narrowband LSPs */
- /*------------------------------------------------------------------*/
- tmp_lpc_coefficients[0] = 1.;
- for(i=0;i<lpc_order/2;i++)
- tmp_lpc_coefficients[i+1] =
- -lpc_coefficients_8[i];
- pc2lsf(PHI_Priv->next_uq_lsf_8, tmp_lpc_coefficients, lpc_order/2);
- /*------------------------------------------------------------------*/
- /* Narrowband Quantization Procedure */
- /*------------------------------------------------------------------*/
- mod_nb_abs_lsp_quantizer(PHI_Priv->next_uq_lsf_8, PHI_Priv->previous_q_lsf_8, PHI_Priv->next_q_lsf_8,
- next_codes, lpc_order/2, 5,
- n_lpc_analysis);
- /*------------------------------------------------------------------*/
- /* Calculate Wideband LSPs */
- /*------------------------------------------------------------------*/
- tmp_lpc_coefficients[0] = 1.;
- for(i=0;i<lpc_order;i++)
- tmp_lpc_coefficients[i+1] =
- -lpc_coefficients[i];
- pc2lsf(PHI_Priv->next_uq_lsf_16, tmp_lpc_coefficients, lpc_order);
- /*------------------------------------------------------------------*/
- /* Wideband Quantization Procedure */
- /*------------------------------------------------------------------*/
- mod_nec_bws_lsp_quantizer(PHI_Priv->next_uq_lsf_16, PHI_Priv->next_q_lsf_8,
- PHI_Priv->next_q_lsf_16, next_codes + 5,
- lpc_order, lpc_order/2, num_lpc_indices, blsp_next, PHI_Priv);
- /*------------------------------------------------------------------*/
- /* Narrowband Quantization Procedure */
- /*------------------------------------------------------------------*/
- mod_nb_abs_lsp_quantizer(PHI_Priv->current_uq_lsf_8, PHI_Priv->previous_q_lsf_8, PHI_Priv->current_q_lsf_8,
- current_codes, lpc_order/2, 5,
- n_lpc_analysis);
- /*------------------------------------------------------------------*/
- /* Wideband Quantization Procedure */
- /*------------------------------------------------------------------*/
- mod_nec_bws_lsp_quantizer(PHI_Priv->current_uq_lsf_16, PHI_Priv->current_q_lsf_8,
- PHI_Priv->current_q_lsf_16, current_codes + 5,
- lpc_order, lpc_order/2, num_lpc_indices, blsp_current, PHI_Priv);
- /*------------------------------------------------------------------*/
- /* Determine dynamic threshold */
- /*------------------------------------------------------------------*/
- if (PHI_Priv->PHI_frames_sent == PHI_Priv->PHI_FRAMES)
- {
- long actual_bit_rate = (long)(((float)PHI_Priv->PHI_actual_bits)/(((float)PHI_Priv->PHI_frames_sent)*PHI_Priv->PHI_FR));
- float diff = (float)fabs((double)(PHI_Priv->PHI_desired_bit_rate - actual_bit_rate));
- float per_diff = diff/(float)PHI_Priv->PHI_desired_bit_rate;
- if (per_diff > (float)0.005)
- {
- long coeff = (long)(per_diff * (float)10.0+(float)0.5) + 1;
- if (actual_bit_rate > PHI_Priv->PHI_desired_bit_rate)
- PHI_Priv->PHI_dyn_lpc_thresh += (float)0.01 * (float)coeff;
- if (actual_bit_rate < PHI_Priv->PHI_desired_bit_rate)
- PHI_Priv->PHI_dyn_lpc_thresh -= (float)0.01 * (float)coeff;
- if (PHI_Priv->PHI_dyn_lpc_thresh < (float)0.0)
- PHI_Priv->PHI_dyn_lpc_thresh = (float)0.0;
- if (PHI_Priv->PHI_dyn_lpc_thresh > PHI_Priv->PHI_stop_threshold)
- PHI_Priv->PHI_dyn_lpc_thresh = PHI_Priv->PHI_stop_threshold;
- }
- PHI_Priv->PHI_frames_sent = 0;
- PHI_Priv->PHI_actual_bits = 0;
- }
- /*------------------------------------------------------------------*/
- /* Check if you really need to transmit an LPC set */
- /*------------------------------------------------------------------*/
- if (PHI_Priv->PHI_prev_int_flag)
- {
- *interpolation_flag = 0;
- if (!PHI_Priv->PHI_prev_lpc_flag)
- {
- *send_lpc_flag = 1;
- }
- else
- {
- *send_lpc_flag = 0;
- }
- }
- else
- {
- float d;
- float lpc_thresh = PHI_Priv->PHI_dyn_lpc_thresh;
- for(d = (float)0.0, k = 0; k < (int)lpc_order; k++)
- {
- double temp;
- temp = (double)(((float)0.5 * (PHI_Priv->previous_q_lsf_16[k] + PHI_Priv->next_q_lsf_16[k])) - PHI_Priv->current_q_lsf_16[k]);
- d += (float)fabs(temp);
- }
- d /= 2.0F;
- if (d < lpc_thresh)
- {
- *interpolation_flag = 1;
- *send_lpc_flag = 1;
- /*--------------------------------------------------------------*/
- /* Perform Interpolation */
- /*--------------------------------------------------------------*/
- for(k = 0; k < (int)lpc_order; k++)
- {
- PHI_Priv->current_q_lsf_16[k] = (float)0.5 * (PHI_Priv->previous_q_lsf_16[k] + PHI_Priv->next_q_lsf_16[k]);
- }
- }
- else
- {
- *interpolation_flag = 0;
- *send_lpc_flag = 1;
- }
- }
- /*------------------------------------------------------------------*/
- /* Make sure to copy the right indices for the bit stream */
- /*------------------------------------------------------------------*/
- if (*send_lpc_flag)
- {
- if (*interpolation_flag)
- {
- /* INTERPOLATION: use next indices (= indices of next frame) */
- for (k=0; k < (int)num_lpc_indices; k++)
- {
- lpc_indices[k] = next_codes[k];
- }
- }
- else
- {
- /* NO INTERPOLATION: use current indices (= indices of current frame) */
- for (k=0; k < (int)num_lpc_indices; k++)
- {
- lpc_indices[k] = current_codes[k];
- }
- }
- }
- else
- {
- int k;
- for (k=0; k < (int)num_lpc_indices; k++)
- {
- lpc_indices[k] = 0;
- }
- }
- PHI_Priv->PHI_prev_lpc_flag = *send_lpc_flag;
- PHI_Priv->PHI_prev_int_flag = *interpolation_flag;
- /*------------------------------------------------------------------*/
- /* Update the number of frames and bits that are output */
- /*------------------------------------------------------------------*/
- PHI_Priv->PHI_frames_sent += 1;
- if (*send_lpc_flag)
- {
- PHI_Priv->PHI_actual_bits += PHI_Priv->PHI_MAX_BITS;
- }
- else
- {
- PHI_Priv->PHI_actual_bits += PHI_Priv->PHI_MIN_BITS;
- }
- /*------------------------------------------------------------------*/
- /* Interpolation Procedure on LSPs */
- /*------------------------------------------------------------------*/
- for(s = 0; s < (int)n_subframes; s++)
- {
- for(k = 0; k < (int)lpc_order; k++)
- {
- register float tmp;
- tmp = (((float)((int)n_subframes - s - 1) * PHI_Priv->previous_q_lsf_int_16[k])
- + ((float)(1 + s) * PHI_Priv->current_q_lsf_16[k]))/(float)n_subframes;
- int_lsf[k] = tmp;
- }
- /*--------------------------------------------------------------*/
- /*Compute corresponding a-parameters */
- /*--------------------------------------------------------------*/
- lsf2pc(tmp_lpc_coefficients, int_lsf, lpc_order);
- for( k = 0; k < lpc_order; k++)
- {
- int_Qlpc_coefficients[s*(int)lpc_order + k] = -tmp_lpc_coefficients[k+1];
- }
- }
- /* -----------------------------------------------------------------*/
- /* Save memory */
- /* -----------------------------------------------------------------*/
- for(k=0; k < (int)lpc_order/2; k++)
- {
- PHI_Priv->previous_uq_lsf_8[k] = PHI_Priv->current_uq_lsf_8[k];
- PHI_Priv->current_uq_lsf_8[k] = PHI_Priv->next_uq_lsf_8[k];
- }
- for(k=0; k < (int)lpc_order; k++)
- {
- PHI_Priv->previous_uq_lsf_16[k] = PHI_Priv->current_uq_lsf_16[k];
- PHI_Priv->current_uq_lsf_16[k] = PHI_Priv->next_uq_lsf_16[k];
- }
- for(k=0; k < (int)lpc_order; k++)
- {
- PHI_Priv->previous_q_lsf_int_16[k] = PHI_Priv->current_q_lsf_16[k];
- }
- if (*interpolation_flag)
- {
- ;
- }
- else
- {
- for(k=0; k < (int)lpc_order/2; k++)
- {
- PHI_Priv->previous_q_lsf_8[k] = PHI_Priv->current_q_lsf_8[k];
- PHI_Priv->current_q_lsf_8[k] = PHI_Priv->next_q_lsf_8[k];
- }
- for(k=0; k < (int)lpc_order; k++)
- {
- PHI_Priv->previous_q_lsf_16[k] = PHI_Priv->current_q_lsf_16[k];
- PHI_Priv->current_q_lsf_16[k] = PHI_Priv->next_q_lsf_16[k];
- }
- for (i=0; i < NEC_LSPPRDCT_ORDER; i++)
- {
- for(j=0; j < NEC_MAX_LSPVQ_ORDER; j++)
- {
- PHI_Priv->blsp_previous[i][j] = blsp_current[i][j];
- }
- }
- }
- /* -----------------------------------------------------------------*/
- /*FREE the malloc'd arrays */
- /* -----------------------------------------------------------------*/
- free(int_lsf);
- free(next_codes);
- free(current_codes);
- free(tmp_lpc_coefficients);
- }
- else
- { /* 16 khz Optimized VQ */
- float *int_lsf;
- long *next_codes;
- long *current_codes;
- float *tmp_lpc_coefficients;
- int i, s,k;
- /*------------------------------------------------------------------*/
- /* Allocate space for current_rfc and current lar */
- /*------------------------------------------------------------------*/
- if
- (
- (( int_lsf = (float *)malloc((unsigned int)lpc_order * sizeof(float))) == NULL ) ||
- (( next_codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL ) ||
- (( current_codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL )
- )
- {
- printf("MALLOC FAILURE in Routine abs_lpc_quantizer n");
- exit(1);
- }
- if((tmp_lpc_coefficients=(float *)calloc(lpc_order+1, sizeof(float)))==NULL) {
- printf("n Memory allocation error in abs_lpc_quantizern");
- exit(4);
- }