⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 celp_decoder.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 4 页
字号:
/*====================================================================*/
/*         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:    CELP_DECODER.C                                  */
/*                                                                      */
/*======================================================================*/
    
/*======================================================================*/
/*      I N C L U D E S                                                 */
/*======================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "buffersHandle.h"       /* handler, defines, enums */

#include "bitstream.h"     /* bit stream module                         */
#include "phi_cons.h"           /* Philips Defined Constants            */
#include "phi_priv.h"		/* for PHI private data storage */
#include "bitstream.h"
#include "lpc_common.h"         /* Common LPC core Defined Constants    */
#include "phi_gxit.h"           /* Excitation Generation Prototypes     */
#include "phi_lpc.h"            /* Prototypes of LPC Subroutines        */
#include "phi_post.h"           /* Post Processing Module Prototype     */
#include "phi_fbit.h"           /* Frame bit allocation table           */
#include "nec_abs_proto.h"      /* Prototypes for NEC Routines          */
#include "pan_celp_const.h"     /* Constants for PAN Routines           */
#include "celp_proto_dec.h"     /* Prototypes for CELP Routines         */
#include "celp_decoder.h"       /* Prototypes for CELP Decoder Routines */
#include "celp_bitstream_demux.h"/* Prototypes for CELP Decoder Routines*/
#include "nec_abs_const.h"      /* Constants for NEC Routines           */

int CELPdecDebugLevel = 0;	/* HP 971120 */

/* ---------------------------------------------------------------------*/ 
/* Frame counter                                                        */
/* ---------------------------------------------------------------------*/
static unsigned long frame_ctr = 0;   /* Frame Counter                   */

static long postfilter = 0;           /* Postfilter switch               */
static float *prev_Qlsp_coefficients; /* previous quantized LSP coeff.   */

static long num_enhstages;
static long dec_enhstages;
static long dec_bwsmode;
static long num_indices;

/*======================================================================*/
/*    NarrowBand Data declaration                                       */
/*======================================================================*/
static float *buf_Qlsp_coefficients_bws; /* current quantized LSP coeff. */
static float *prev_Qlsp_coefficients_bws;/* previous quantized LSP coeff. */

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 num_lpc_indices_bws;
static long mp_pos_bits, mp_sgn_bits;
static long enh_pos_bits, enh_sgn_bits;
static long bws_pos_bits, bws_sgn_bits;

/*======================================================================*/
/* Type definitions                                                     */
/*======================================================================*/

typedef	struct
{
  PHI_PRIV_TYPE	*PHI_Priv;
  /* add private data pointers here for other coding varieties */
} INST_CONTEXT_LPC_DEC_TYPE;


/*======================================================================*/
/* Function definition: abs_coder                                       */
/*======================================================================*/
void celp_decoder
(
BsBitStream *bitStream,           /* In: Bitstream                     */
float     **OutputSignal,          /* Out: Multichannel Output          */
long        ExcitationMode,	   /* In: Excitation Mode	        */
long        SampleRateMode,	   /* In: SampleRate Mode		*/
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        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        *org_frame_bit_allocation, /* In: bit num. for each index     */
void        *InstanceContext	   /* In: pointer to instance context */
)
{
    /*==================================================================*/
    /*      L O C A L   D A T A   D E F I N I T I O N S                 */
    /*==================================================================*/
    float *int_ap;                                    /*Interpolated ap */
    unsigned long *shape_indices;                     /* Shape indices  */
    unsigned long *gain_indices;                      /* Gain indices   */
    unsigned long *indices;                           /* LPC codes*/
    long sbfrm_ctr = 0;
    long dum = 0;

    float *int_ap_bws;                               /*Interpolated ap */
    unsigned long *shape_indices_bws;                /* Shape indices  */
    unsigned long *gain_indices_bws;                 /* Gain indices   */
    unsigned long *indices_bws;                      /* LPC codes*/
    long *bws_nb_acb_index;                          /* ACB codes      */

    unsigned long interpolation_flag = 0;           /* Interpolation Flag*/
    unsigned long LPC_Present = 1;
    unsigned long signal_mode;
    unsigned long rms_index;

    PHI_PRIV_TYPE *PHI_Priv;	

    /* -----------------------------------------------------------------*/
    /* Set up pointers to private data                                  */
    /* -----------------------------------------------------------------*/
    PHI_Priv = ((INST_CONTEXT_LPC_DEC_TYPE *)InstanceContext)->PHI_Priv;
	
    /* -----------------------------------------------------------------*/
    /* Create Arrays for frame processing                               */
    /* -----------------------------------------------------------------*/
    if
    (
    (( int_ap = (float *)malloc((unsigned int)(n_subframes * lpc_order) * sizeof(float))) == NULL )||
    (( shape_indices = (unsigned long *)malloc((unsigned int)((num_enhstages+1)*num_shape_cbks * n_subframes) * sizeof(unsigned long))) == NULL )||
    (( gain_indices = (unsigned long *)malloc((unsigned int)((num_enhstages+1)*num_gain_cbks * n_subframes) * sizeof(unsigned long))) == NULL )||
    (( indices = (unsigned long *)malloc((unsigned int)num_lpc_indices * sizeof(unsigned long))) == NULL )
    )
    {
        fprintf(stderr, "MALLOC FAILURE in abs_decoder  \n");
        exit(1);
    }

    if (BandwidthScalabilityMode == ON) {

      if
	(
	 (( int_ap_bws = (float *)malloc((unsigned int)(n_subframes_bws * lpc_order_bws) * sizeof(float))) == NULL )||
	 (( shape_indices_bws = (unsigned long *)malloc((unsigned int)(num_shape_cbks * n_subframes_bws) * sizeof(unsigned long))) == NULL )||
	 (( gain_indices_bws = (unsigned long *)malloc((unsigned int)(num_gain_cbks * n_subframes_bws) * sizeof(unsigned long))) == NULL )||
	 (( indices_bws = (unsigned long *)malloc((unsigned int)num_lpc_indices_bws * sizeof(unsigned long))) == NULL ) ||
	 (( bws_nb_acb_index = (long *)malloc((unsigned int)n_subframes_bws * sizeof(long))) == NULL )
	 )
	  {
	    fprintf(stderr, "MALLOC FAILURE in abs_decoder  \n");
	    exit(1);
	  }
    }

    /* CelpBaseFrame() */
    /* Celp_LPC() */
    /*==================================================================*/
    /* CELP Lpc demux                                                   */
    /*==================================================================*/
   
    if (FineRateControl == ON)
    {
	/* ---------------------------------------------------------*/
	/* Step I: Read interpolation_flag and LPC_present flag     */
	/* ---------------------------------------------------------*/
	BsGetBit(bitStream, &interpolation_flag, 1);
	BsGetBit(bitStream, &LPC_Present, 1);

	/* ---------------------------------------------------------*/
	/* Step II: If LPC is present                               */
	/* ---------------------------------------------------------*/
	if (LPC_Present == YES)
	{
	    if (SampleRateMode == fs8kHz)
	    {
		Read_NarrowBand_LSP(bitStream, indices);
	    } 
	    else
	    {
		Read_Wideband_LSP(bitStream, indices);
	    }
	}
    }
    else
    {
	if (SampleRateMode == fs8kHz)
	{
	    Read_NarrowBand_LSP(bitStream, indices);
	} 
	else
	{
	    Read_Wideband_LSP(bitStream, indices);
	}
    }

    /*==================================================================*/
    /* CELP Excitation decoding                                         */
    /*==================================================================*/
    if ( ExcitationMode == RegularPulseExc )
    {  /* RPE_Frame() */
	long subframe;

	/*--------------------------------------------------------------*/
	/* Regular Pulse Excitation                                     */ 
	/*--------------------------------------------------------------*/
	for(subframe = 0; subframe < n_subframes; subframe++)
	{
	    /* ---------------------------------------------------------*/
	    /* Read the Adaptive Codebook Lag                           */
	    /* ---------------------------------------------------------*/
	    BsGetBit(bitStream, &shape_indices[subframe * num_shape_cbks], 8);

	    /* ---------------------------------------------------------*/
	    /*Read the Fixed Codebook Index (function of bit-rate)      */
	    /* ---------------------------------------------------------*/

	    switch (RPE_configuration)
	    {
		case     0   :   BsGetBit(bitStream, &shape_indices[subframe * num_shape_cbks + 1], 11);
		                 break;
		case     1   :   BsGetBit(bitStream, &shape_indices[subframe * num_shape_cbks + 1], 11);
		                 break;
		case     2   :   BsGetBit(bitStream, &shape_indices[subframe * num_shape_cbks + 1], 12);
		                 break;
		case     3   :   BsGetBit(bitStream, &shape_indices[subframe * num_shape_cbks + 1], 12);
		                 break;
	    }

	    /* ---------------------------------------------------------*/
	    /*Read the Adaptive Codebook Gain                           */
	    /* ---------------------------------------------------------*/
	    BsGetBit(bitStream, &gain_indices[subframe * num_gain_cbks], 6);
			
	    /* ---------------------------------------------------------*/
	    /*Read the Fixed Codebook Gain (function of subframe)       */
	    /*Later subframes are encoded differentially w.r.t previous */
	    /* ---------------------------------------------------------*/
	    if (subframe == 0)
	    {
		BsGetBit(bitStream, &gain_indices[subframe * num_gain_cbks + 1], 5);
	    }
	    else
	    {
		BsGetBit(bitStream, &gain_indices[subframe * num_gain_cbks + 1], 3);
	    }
			
	}
    }
    
    if (ExcitationMode == MultiPulseExc) { /* MPE_frame() */
      /*--------------------------------------------------------------*/
      /* Multi-Pulse Excitation                                       */ 
      /*--------------------------------------------------------------*/
      long i;

      BsGetBit(bitStream, &signal_mode, NEC_BIT_MODE);
      BsGetBit(bitStream, &rms_index, NEC_BIT_RMS);
      if (SampleRateMode == fs8kHz) {
	for ( i = 0; i < n_subframes; i++ ) {
	  BsGetBit(bitStream,
		 &shape_indices[i*num_shape_cbks+0], NEC_BIT_ACB);
	  BsGetBit(bitStream,
		 &shape_indices[i*num_shape_cbks+1], mp_pos_bits);
	  BsGetBit(bitStream,
		 &shape_indices[i*num_shape_cbks+2], mp_sgn_bits);
	  BsGetBit(bitStream,
		 &gain_indices[i*num_gain_cbks+0], NEC_BIT_GAIN);
	}
      } else {
	for ( i = 0; i < n_subframes; i++ ) {
	  BsGetBit(bitStream,
		 &shape_indices[i*num_shape_cbks+0], NEC_ACB_BIT_WB);
	  BsGetBit(bitStream,
		 &shape_indices[i*num_shape_cbks+1], mp_pos_bits);
	  BsGetBit(bitStream,
		 &shape_indices[i*num_shape_cbks+2], mp_sgn_bits);
	  BsGetBit(bitStream,
		 &gain_indices[i*num_gain_cbks+0], NEC_BIT_GAIN_WB);
	}
      }
    }
    /* end of CelpBaseFrame() */

    if (ExcitationMode == MultiPulseExc) {
      long i, j;

      /* CelpBRSenhFrame() */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -