📄 celp_encoder.c
字号:
/*====================================================================*//* MPEG-4 Audio (ISO/IEC 14496-3) Copyright Header *//*====================================================================*//*This software module was originally developed by Rakesh Taori and AndyGerrits (Philips Research Laboratories, Eindhoven, The Netherlands) inthe course of development of the MPEG-4 Audio (ISO/IEC 14496-3). Thissoftware module is an implementation of a part of one or more MPEG-4Audio (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/IEC14496-3) free license to this software module or modifications thereoffor use in hardware or software products claiming conformance to theMPEG-4 Audio (ISO/IEC 14496-3). Those intending to use this softwaremodule in hardware or software products are advised that its use mayinfringe existing patents. The original developer of this softwaremodule and his/her company, the subsequent editors and theircompanies, and ISO/IEC have no liability for use of this softwaremodule or modifications thereof in an implementation. Copyright is notreleased for non MPEG-4 Audio (ISO/IEC 14496-3) conforming products.CN1 retains full right to use the code for his/her own purpose, assignor donate the code to a third party and to inhibit third parties fromusing the code for non MPEG-4 Audio (ISO/IEC 14496-3) conformingproducts. This copyright notice must be included in all copies orderivative works. Copyright 1996.*//*====================================================================*//*======================================================================*//* *//* SOURCE_FILE: CELP_ENCODER.C *//* *//*======================================================================*/ /*======================================================================*//* I N C L U D E S *//*======================================================================*/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <assert.h>#include "buffersHandle.h" /* handler, defines, enums */#include "lpc_common.h" /* Common LPC core Defined Constants */#include "bitstream.h" /* bit stream module */#include "phi_cons.h" /* Philips Defined Constants */#include "phi_priv.h" /* for PHI private data storage */#include "phi_prep.h" /* Prototypes for Preprocessing Modules */#include "phi_lpc.h" /* Prototypes for LPC Modules */#include "phi_axit.h" /* Prototypes for Excitation Analysis Module */#include "celp_bitstream_mux.h" /* Prototypes for Bitstream Mux */#include "phi_apre.h" /* Prototypes for Excitation Analysis Module */#include "phi_fbit.h" /* Frame bit allocation table */#include "celp_encoder.h" /* Prototypes for Coder Routines */#include "pan_celp_const.h"/* Constants for PAN Routines */#include "celp_proto_enc.h"/* Prototypes for CELP Routines */#include "nec_abs_const.h" /* Constants for NEC Routines */#include "nec_abs_proto.h" /* Prototypes for NEC Routines */#include "phi_nec_lpc.h" /* Prototypes for NEC Routines */int CELPencDebugLevel = 0; /* HP 971120 *//*======================================================================*//* L O C A L F U N C T I O N D E C L A R A T I O N *//*======================================================================*/void nec_lpf_down( float xin[], float xout[], int len );/*======================================================================*//* L O C A L D A T A D E C L A R A T I O N *//*======================================================================*//*======================================================================*//* WideBand Data Declaration *//*======================================================================*/static float *PHI_sp_pp_buf;static float PHI_af;static long PHI_BUFFER;static float *Downsampled_signal;static float **SquaredHammingWindow; /* For Haming Window */static float **HammingWindow; /* For Haming Window */static float *PHI_GammaArrayBE; /* For Bandwidth Expansion */static float *PAN_GammaArrayBE; /* For Bandwidth Expansion */static float PHI_prev_x = (float)0.0; /* Previous x value */static float PHI_prev_y = (float)0.0; /* previous y value */static float NEC_prev_x = (float)0.0; /* Previous x value */static float NEC_prev_y = (float)0.0; /* previous y value *//*======================================================================*//* NarrowBand Data declaration *//*======================================================================*/static float *bws_sp_buffer; /* input signal buffer */static float *prev_Qlsp_coefficients; /* previous quantized LSP coeff. */static float *buf_Qlsp_coefficients_bws; /* current quantized LSP coeff. */static float *prev_Qlsp_coefficients_bws;/* previous quantized LSP coeff. */static float gamma_num; /* fixed or variable? */static float gamma_den; /* fixed or variable? */static long num_indices, buffer_size;static long num_enhstages;static long buffer_size_bws;static long frame_size_nb, frame_size_bws;static long n_subframes_bws;static long sbfrm_size_bws;static long lpc_order_bws;static long n_lpc_analysis_bws;static long num_lpc_indices_bws;static long *window_sizes_bws;static long *window_offsets_bws;/* ---------------------------------------------------------------------*/ /*Frame Related Information *//* ---------------------------------------------------------------------*/static unsigned long frame_ctr = 0; /* Frame Counter */static unsigned long lpc_sent_frames=0;/* Frames where LPc is sent *//*======================================================================*//* Type definitions *//*======================================================================*/typedef struct{ PHI_PRIV_TYPE *PHI_Priv; /* add private data pointers here for other coding varieties */} INST_CONTEXT_LPC_ENC_TYPE;/*======================================================================*//* Function definition: celp_coder *//*======================================================================*/void celp_coder(float **InputSignal, /* In: Multichannel Speech */BsBitStream *bitStream, /* Out: Bitstream */long sampling_frequency, /* In: Sampling Frequency */long bit_rate, /* In: Bit rate */long ExcitationMode, /* In: Excitation Mode */long SampleRateMode,long QuantizationMode, /* In: Type of Quantization */long FineRateControl, /* In: Fine Rate Control switch */long LosslessCodingMode, /* In: Lossless Coding Mode */ long RPE_configuration, /* In: RPE_configuration */long Wideband_VQ, /* In: Wideband VQ mode */long MPE_Configuration, /* In: Narrowband configuration */long NumEnhLayers, /* In: Number of Enhancement Layers */long BandwidthScalabilityMode, /* In: bandwidth switch */long BWS_Configuration, /* In: BWS_configuration */long PreProcessingSW, /* In: PreProcessingSW */long frame_size, /* In: Frame size */long n_subframes, /* In: Number of subframes */long sbfrm_size, /* In: Subframe size */long lpc_order, /* In: Order of LPC */long num_lpc_indices, /* In: Number of LPC indices */long num_shape_cbks, /* In: Number of Shape Codebooks */long num_gain_cbks, /* In: Number of Gain Codebooks */long n_lpc_analysis, /* In: Number of LPCs per frame */long window_offsets[], /* In: Offset for LPC-frame v.window*/long window_sizes[], /* In: LPC Analysis Window size */long max_n_lag_candidates, /* In: Maximum search candidates */float min_pitch_frequency, /* IN: Min Pitch Frequency */float max_pitch_frequency, /* IN: Max Pitch Frequency */long org_frame_bit_allocation[], /* In: Frame BIt alolocation */void *InstanceContext /* In/Out: instance context */){ /*==================================================================*/ /* L O C A L D A T A D E F I N I T I O N S */ /*==================================================================*/ float af_next =(float)0.0; /* First-order fit lpc parameter */ float *sp_buf_ptr = InputSignal[0]; /* Speech Buffer */ float *int_ap; /* Interpolated LPC coeffs */ float *ap; /* LPC Parameters */ float *ds_ap; long *ds_window_offsets; long *ds_window_sizes; long *shape_indices; /* Lags for Codebooks */ long *gain_indices; /* Gains for Codebooks */ long *codes; /* LPC codes */ long interpolation_flag = 0; /* Interpolation flag */ long LPC_Present = 1; long signal_mode = 0; /* Signal mode */ long sbfrm_ctr = 0; /* Subframe counter */ int k; float *Downsampled_frame; long rms_index; float *int_ap_bws; /* Interpolated LPC coeffs */ float *ap_bws; /* LPC Parameters */ long *shape_indices_bws; /* Lags for Codebooks */ long *gain_indices_bws; /* Gains for Codebooks */ long *codes_bws; /* LPC codes */ long *bws_nb_acb_index; /* ACB codes */ long dummy = 0; PHI_PRIV_TYPE *PHI_Priv; /* -----------------------------------------------------------------*/ /* Set up pointers to private data */ /* -----------------------------------------------------------------*/ PHI_Priv = ((INST_CONTEXT_LPC_ENC_TYPE *)InstanceContext)->PHI_Priv; if ( BandwidthScalabilityMode == OFF) { /* -----------------------------------------------------------------*/ /* Segmentation for wideband speech */ /* -----------------------------------------------------------------*/ for ( k = 0; k < (int)(PHI_BUFFER-frame_size); k++) { PHI_sp_pp_buf[k] = PHI_sp_pp_buf[k + (int)(frame_size)]; } /* -----------------------------------------------------------------*/ /* Preprocessing (In our case it is just a DC notch) */ /* -----------------------------------------------------------------*/ if (PreProcessingSW == ON) { celp_preprocessing(sp_buf_ptr, &PHI_sp_pp_buf[PHI_BUFFER-frame_size], &PHI_prev_x, &PHI_prev_y, frame_size, sampling_frequency); } else { for ( k = 0; k < frame_size; k++) PHI_sp_pp_buf[PHI_BUFFER-frame_size+k] = sp_buf_ptr[k]; } } else { for ( k = 0; k < (int)(PHI_BUFFER-frame_size_nb); k++) { PHI_sp_pp_buf[k] = PHI_sp_pp_buf[k + (int)(frame_size_nb)]; } for ( k = 0; k < (int)(buffer_size_bws-frame_size_bws); k++) { bws_sp_buffer[k] = bws_sp_buffer[k + (int)(frame_size_bws)]; } if (PreProcessingSW == ON) { celp_preprocessing(sp_buf_ptr, &bws_sp_buffer[buffer_size_bws-frame_size_bws], &PHI_prev_x, &PHI_prev_y, frame_size_bws, sampling_frequency); } else { for ( k = 0; k < frame_size_bws; k++) bws_sp_buffer[buffer_size_bws-frame_size_bws+k] = sp_buf_ptr[k]; } nec_lpf_down( &bws_sp_buffer[buffer_size_bws-frame_size_bws], &PHI_sp_pp_buf[buffer_size-frame_size_nb], frame_size_bws ); } /* -----------------------------------------------------------------*/ /* Segmentation for downsampled speech */ /* -----------------------------------------------------------------*/ if ((ExcitationMode == RegularPulseExc) && (SampleRateMode == fs16kHz) && (QuantizationMode == VectorQuantizer) && (Wideband_VQ == Scalable_VQ)) { for ( k = 0; k < (int)(PHI_BUFFER-frame_size)/2; k++) { Downsampled_signal[k] = Downsampled_signal[k + (int)(frame_size/2)]; } /* -----------------------------------------------------------------*/ /* Allocate memory for downsampling frame */ /* -----------------------------------------------------------------*/ if ( (( Downsampled_frame = (float *)malloc((unsigned int)(frame_size/2) * sizeof(float))) == NULL ) ) { fprintf(stderr, "MALLOC FAILURE in abs_coder \n"); exit(1); } /* -----------------------------------------------------------------*/ /* Segmentation for downsampled speech */ /* -----------------------------------------------------------------*/ nec_lpf_down(sp_buf_ptr, Downsampled_frame, (int)frame_size); /* -----------------------------------------------------------------*/ /* Preprocessing (In our case it is just a DC notch) */ /* -----------------------------------------------------------------*/ celp_preprocessing(Downsampled_frame, &Downsampled_signal[(PHI_BUFFER-frame_size)/2], &NEC_prev_x, &NEC_prev_y, frame_size/2, sampling_frequency/2); /* -----------------------------------------------------------------*/ /* Free allocated downsampled frame */ /* -----------------------------------------------------------------*/ free(Downsampled_frame); } if (ExcitationMode == RegularPulseExc) { /* -----------------------------------------------------------------*/ /* To synchronise the decoded output with the original signal */ /* This helps to make measurements such as SNR */ /* -----------------------------------------------------------------*/ if (frame_ctr < 2) { frame_ctr++; return; } } if (ExcitationMode == MultiPulseExc) { if (bitStream == NULL) return; } /* -----------------------------------------------------------------*/ /* Create Arrays for frame processing */ /* -----------------------------------------------------------------*/ if ( (( int_ap = (float *)malloc((unsigned int)(n_subframes * lpc_order) * sizeof(float))) == NULL )|| (( ap = (float *)malloc((unsigned int)(n_lpc_analysis * lpc_order) * sizeof(float))) == NULL )|| (( shape_indices = (long *)malloc((unsigned int)((num_enhstages+1)*num_shape_cbks * n_subframes) * sizeof(long))) == NULL )|| (( gain_indices = (long *)malloc((unsigned int)((num_enhstages+1)*num_gain_cbks * n_subframes) * sizeof(long))) == NULL )|| (( ds_ap = (float *)malloc((unsigned int)(n_lpc_analysis * lpc_order) * sizeof(float))) == NULL )|| (( ds_window_offsets = (long *)malloc((unsigned int)(n_lpc_analysis) * sizeof(long))) == NULL )|| (( ds_window_sizes = (long *)malloc((unsigned int)(n_lpc_analysis) * sizeof(long))) == NULL )|| (( codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL ) ) { fprintf(stderr, "MALLOC FAILURE in abs_coder \n"); exit(1); } if ( BandwidthScalabilityMode == ON) { if ( (( int_ap_bws = (float *)malloc((unsigned int)(n_subframes_bws * lpc_order_bws) * sizeof(float))) == NULL )|| (( ap_bws = (float *)malloc((unsigned int)(n_lpc_analysis_bws * lpc_order_bws) * sizeof(float))) == NULL )|| (( shape_indices_bws = (long *)malloc((unsigned int)(num_shape_cbks * n_subframes_bws) * sizeof(long))) == NULL )|| (( gain_indices_bws = (long *)malloc((unsigned int)(num_gain_cbks * n_subframes_bws) * sizeof(long))) == NULL )|| (( codes_bws = (long *)malloc((unsigned int)num_lpc_indices_bws * sizeof(long))) == NULL ) || (( bws_nb_acb_index = (long *)malloc((unsigned int)n_subframes_bws * sizeof(long))) == NULL ) ) { fprintf(stderr, "MALLOC FAILURE in abs_coder \n"); exit(1); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -