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

📄 vtcdec.cpp

📁 此源码是在VC平台下,实现MPEG4编解码的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  target_spatial_levels=MIN(mzte_codec.m_iSpatialLev,
			    target_spatial_levels);
  for (spa_lev=0; spa_lev<target_spatial_levels; spa_lev++)
  {
    /*----- AC: Set global spatial layer and SNR scalability level. -----*/
    /* Assumes all three color components have the same number of SNR 
       levels */
    mzte_codec.m_iCurSpatialLev = spa_lev;
    mzte_codec.m_SPlayer[0].SNR_scalability_levels = SNR_INFINITY;
    snr_scalability_levels = mzte_codec.m_SPlayer[0].SNR_scalability_levels;

    /* Update spatial level coeff info if changing spatial levels.
       Do this for all color components. */
    if (spa_lev != 0)
    {
      for (mzte_codec.m_iCurColor = 0; mzte_codec.m_iCurColor<mzte_codec.m_iColors;
	   mzte_codec.m_iCurColor++) 
      {
		setSpatialLevelAndDimensions(mzte_codec.m_iCurSpatialLev, 
					  mzte_codec.m_iCurColor);
// hjlee 0901
		if (mzte_codec.m_lastWvtDecompInSpaLayer[spa_lev-1][mzte_codec.m_iCurColor]
			>=0)		
		spatialLayerChangeUpdate(mzte_codec.m_iCurColor);
      }
    }
           
    if (!mzte_codec.m_bStartCodeEnable)
    {
      snr_scalability_levels = get_X_bits(5);  
      mzte_codec.m_SPlayer[0].SNR_scalability_levels = snr_scalability_levels;
    }

    /* Loop through SNR layers */      
    for (snr_lev=0; snr_lev<snr_scalability_levels; snr_lev++) 
    {
      if (spa_lev==target_spatial_levels-1 && snr_lev==target_snr_levels)
	break;

      /*----- AC: Set global SNR layer -----*/
      mzte_codec.m_iCurSNRLev = snr_lev;
             
      if (mzte_codec.m_bStartCodeEnable)
      {
		/*------- AC: Open and initialize bitstream file -------*/
		if (mzte_codec.m_iSingleBitFile==0)
		{
			sprintf(fname,mzte_codec.m_cBitFileAC,
			  mzte_codec.m_iCurSpatialLev,mzte_codec.m_iCurSNRLev);
		    if ((bitfile=fopen(fname,"rb"))==NULL)
			 errorHandler("Can't open file '%s' for reading.",fname);
	  
			/* initialize the buffer */
			init_bit_packing_fp(bitfile,1);
		}
		else
			init_bit_packing_fp(bitfile,0);
       	
		if (snr_lev==0) {
			 /*------- AC: Read header info to bitstream file -------*/
			 texture_spatial_layer_start_code = get_X_bits(32);
			 if (texture_spatial_layer_start_code != 
				  TEXTURE_SPATIAL_LAYER_START_CODE)
				 errorHandler("Wrong texture_spatial_layer_start_code3 %x.",
						 texture_spatial_layer_start_code);
	  
			 texture_spatial_layer_id = get_X_bits(5);
			 if(texture_spatial_layer_id !=spa_lev)
				errorHandler("Incorrect texture_spatial_layer_id");
			 snr_scalability_levels = get_X_bits(5);  
			 mzte_codec.m_SPlayer[0].SNR_scalability_levels = 
						snr_scalability_levels;
			 align_byte();  /* byte alignment before start code */
		}	
	
      }
      
      /*------- AC: Decode and inverse quantize all color components ------*/
      TextureSNRLayerMQ_decode(spa_lev, snr_lev, bitfile);   
      if (mzte_codec.m_bStartCodeEnable){
		align_byte();  /* byte alignment before start code */
		if (mzte_codec.m_iSingleBitFile==0)
		 fclose(bitfile);
      }
      
    } /* snr_lev */
    
  }  /* spa_lev */
 
  /*------- AC: Free Qlist structure -------*/
  ztqQListExit();
}

// hjlee 0901
Void CVTCDecoder::TextureObjectLayer_dec(
				   Int  target_spatial_levels,
				   Int  target_snr_levels, 
				   FILTER ***pwvtfilter)
{
  FILE *bitfile;
  Int  x,y,w,h,k;
  FILTER **wvtfilter;  // hjlee 0901
  UChar *inmask[3], *outmask[3];
  Int nLevels[3], ret;
  Int Width[3], Height[3];
  Int Nx[3], Ny[3];
  Int usemask;
  Int useInt=1;
  PICTURE *Image;
  Int col,l;


  /*-------------------------------------------------*/
  /*--------- DC (and overall header info) ----------*/
  /*-------------------------------------------------*/

  /*------- DC: Open and initialize bitstream file -------*/
  if ((bitfile=fopen(m_cInBitsFile,"rb"))==NULL)
    errorHandler("Can't open file '%s' for reading.",m_cInBitsFile);

  /* initialize variables */
  init_bit_packing_fp(bitfile,1);

  /*------- DC: Read header info from bitstream file -------*/
  header_Dec(pwvtfilter, &Image); // hjlee 0901
  wvtfilter = *pwvtfilter; // hjlee 0901

  /*--------------- CREATE DATA STRUCTURES -----------------*/
  noteDetail("Creating and initializing data structures....");
  mzte_codec.m_iColors = 3;
  mzte_codec.m_iBitDepth = 8;  
  usemask = mzte_codec.m_iAlphaChannel = 0;
  useInt =1;

  init_acm_maxf_dec();  // hjlee 0901 
  for (col=0; col<mzte_codec.m_iColors; col++) // hjlee 0901 BUG
	  for (l=0; l<mzte_codec.m_iWvtDecmpLev; l++) {
	    mzte_codec.m_SPlayer[col].SNRlayer.snr_image.wvtDecompNumBitPlanes[l] = 0;
//	    mzte_codec.m_SPlayer[col].SNRlayer.snr_image.wvtDecompResNumBitPlanes[l] = 0;
	  }


  
  for (col=0; col<mzte_codec.m_iColors; col++) {

    h = mzte_codec.m_iHeight >> (Int)(col>0);
    w  = mzte_codec.m_iWidth  >> (Int)(col>0);

		mzte_codec.m_SPlayer[col].coeffinfo = new COEFFINFO * [h];
		if (mzte_codec.m_SPlayer[col].coeffinfo == NULL)
			exit(fprintf(stderr,"Allocating memory for coefficient structure (I)."));
		mzte_codec.m_SPlayer[col].coeffinfo[0] = new COEFFINFO [h*w];
		if (mzte_codec.m_SPlayer[col].coeffinfo[0] == NULL)
			exit(fprintf(stderr,"Allocating memory for coefficient structure (II)."));
		for (int y = 1; y < h; ++y)
		  mzte_codec.m_SPlayer[col].coeffinfo[y] = 
			mzte_codec.m_SPlayer[col].coeffinfo[y-1]+w;
		
		for (y=0; y<h; y++)
			for (int x=0; x<w; x++) {
				mzte_codec.m_SPlayer[col].coeffinfo[y][x].skip =0;
				mzte_codec.m_SPlayer[col].coeffinfo[y][x].wvt_coeff = 0;
				mzte_codec.m_SPlayer[col].coeffinfo[y][x].rec_coeff = 0;
				mzte_codec.m_SPlayer[col].coeffinfo[y][x].quantized_value = 0;
				// mzte_codec.m_SPlayer[col].coeffinfo[y][x].qState = 0;
				mzte_codec.m_SPlayer[col].coeffinfo[y][x].type = 0;
				mzte_codec.m_SPlayer[col].coeffinfo[y][x].mask = 0;

			}


  }
  noteDetail("Completed creating and initializing data structures.");

  mzte_codec.m_iDCHeight  = mzte_codec.m_iHeight >> mzte_codec.m_iWvtDecmpLev;
  mzte_codec.m_iDCWidth   = mzte_codec.m_iWidth >> mzte_codec.m_iWvtDecmpLev;

 
  /* copy over the inmask[0] */

  Width[0] = mzte_codec.m_iWidth;
  Width[1] = Width[2] = (Width[0] >> 1);
  Height[0] = mzte_codec.m_iHeight;
  Height[1] = Height[2] = (Height[0] >> 1);
  nLevels[0] = mzte_codec.m_iWvtDecmpLev ;
  nLevels[1] = nLevels[2] = nLevels[0]-1;
  
  Nx[0] = Ny[0]=2;
  for(col=1;col<3;col++) Nx[col]=Ny[col]=1;
  
// #ifdef _DECODER_  // hjlee
  mzte_codec.m_Image = Image;
// #endif
  for (col=0; col<mzte_codec.m_iColors; col++) {
    mzte_codec.m_Image[col].height = mzte_codec.m_iHeight >> (Int)(col>0);
    mzte_codec.m_Image[col].width  = mzte_codec.m_iWidth >> (Int)(col>0);

    inmask[col] = mzte_codec.m_Image[col].mask; 
    outmask[col] = (UChar *)malloc(sizeof(UChar) *  Width[col]*Height[col]);

    ret = do_DWTMask(inmask[col], outmask[col], 
		     Width[col], Height[col], 
		     nLevels[col], &(wvtfilter[col==0?0:1])); 
    if (ret!= DWT_OK) 
      errorHandler("DWT Error Code %d\n", ret);
    
    for (k=0,y=0; y<Height[col]; y++)
      for (x=0; x<Width[col]; x++) 
	COEFF_MASK(x,y,col) = outmask[col][k++];
    free(outmask[col]);
    
  }


  if (target_spatial_levels<=0 || target_snr_levels<= 0)
    errorHandler("Neither target_spatial_levels nor target_snr_levels" \
		 "can be zero");

  /*------- DC: Decode and inverse quantize all color components -------*/
  textureLayerDC_Dec();


  /*------- DC: Close bitstream file -------*/

  /* hjlee 0901 */ 
  if (mzte_codec.m_bStartCodeEnable){
    align_byte();
    if(!mzte_codec.m_iSingleBitFile)
      fclose(bitfile);
  }
  

  /*-------------------------------------------------*/
  /*--------------------- AC ------------------------*/
  /*-------------------------------------------------*/

  
  /*------- AC: SINGLE-QUANT MODE -------*/
  if (mzte_codec.m_iQuantType == SINGLE_Q) 
    textureLayerSQ_Dec(bitfile);
  /*------- AC: MULTI-QUANT MODE -------*/
  else if (mzte_codec.m_iQuantType == MULTIPLE_Q)
// hjlee 0901
    textureLayerMQ_Dec(bitfile, target_spatial_levels, target_snr_levels,
		       wvtfilter);
	
	/*------- AC: BILEVEL-QUANT MODE -------*/
  else if (mzte_codec.m_iQuantType == BILEVEL_Q) {
    PEZW_target_spatial_levels=target_spatial_levels;
    PEZW_target_snr_levels=target_snr_levels;
    PEZW_target_bitrate=0;
	textureLayerBQ_Dec(bitfile);
  }

  for(col=0; col< mzte_codec.m_iColors; col++)
    free(Image[col].mask);
  free(Image);

  if (mzte_codec.m_iSingleBitFile==0){
    if(!mzte_codec.m_bStartCodeEnable)
      align_byte();
    fclose(bitfile);
  }
} 

Void CVTCDecoder::decode(Char *InBitsFile, Char *RecImageFile,
						 Int TargetSpaLev, Int TargetSNRLev)
{
  Int col;
    FILTER **wvtfilter; // hjlee 0901

  noteProgress("\n----- MPEG-4 Visual Texture Coding: Decoding -----\n");

  strcpy(m_cInBitsFile, InBitsFile);
  strcpy(m_cRecImageFile, RecImageFile);

  mzte_codec.m_iTargetSpatialLev = TargetSpaLev;
  mzte_codec.m_iTargetSNRLev = TargetSNRLev;

  mzte_codec.m_iScanOrder = 0;
  mzte_codec.m_iAcmMaxFreqChg =0;
  mzte_codec.m_iAcmOrder =0;


  mzte_codec.m_iColors=3;
//  init_acm_maxf_dec();  // hjlee 0901
  
  mzte_codec.m_iSingleBitFile = 1; /* hjlee 0511 */
  mzte_codec.m_cBitFile = NULL;
  mzte_codec.m_cBitFileAC = NULL;



  TextureObjectLayer_dec(mzte_codec.m_iTargetSpatialLev,
			 mzte_codec.m_iTargetSNRLev, &wvtfilter); 

  /* DISCRETE INVERSE WAVELET TRANSFORM */
  
  noteProgress("\nInverse Wavelet Transform....");
  perform_IDWT(wvtfilter, m_cRecImageFile); // hjlee 0901
  noteProgress("Completed inverse wavelet transform.");
  
  noteDetail("Freeing up decoding data structures....");
	/*----- free up coeff data structure -----*/
  for (col=0; col<mzte_codec.m_iColors; col++) {
		if (mzte_codec.m_SPlayer[col].coeffinfo[0] != NULL)
			delete (mzte_codec.m_SPlayer[col].coeffinfo[0]);
		mzte_codec.m_SPlayer[col].coeffinfo[0] = NULL;
		if (mzte_codec.m_SPlayer[col].coeffinfo)
			delete (mzte_codec.m_SPlayer[col].coeffinfo);
		mzte_codec.m_SPlayer[col].coeffinfo = NULL;
  }
  noteDetail("Completed freeing up decoding data structures.");

  noteProgress("\n----- Decoding Completed. -----\n");

}

⌨️ 快捷键说明

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