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

📄 ldecod.c

📁 在ccs下运行的代码。通过编译 可以运行
💻 C
📖 第 1 页 / 共 2 页
字号:


//#include "contributors.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
//#include <sys/timeb.h>
/*
#if defined WIN32
  #include <io.h>
#else
  #include <unistd.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
*/

#include <assert.h>

#include "global.h"
//#include "rtp.h"
#include "memalloc.h"
#include "mbuffer.h"
//#include "leaky_bucket.h"
#include "fmo.h"
#include "annexb.h"
#include "output.h"
//#include "cabac.h"

#include "erc_api.h"

#define JM          "9 (FRExt)"
#define VERSION     "9.5"
#define EXT_VERSION "(FRExt)"

//#define LOGFILE     "log.dec"
//#define DATADECFILE "dataDec.txt"
#define TRACEFILE   "trace_dec.txt"

extern objectBuffer_t *erc_object_list;
extern ercVariables_t *erc_errorVar;
extern ColocatedParams *Co_located;
FILE *p_out2;
 int   **cofAC8x8_intra; 
 int  ****cofAC8x8_iintra;
pic_parameter_set_rbsp_t *active_pps=NULL;
 seq_parameter_set_rbsp_t *active_sps=NULL;

// global picture format dependend buffers, mem allocation in decod.c ******************
 int  **refFrArr;                                //!< Array for reference frames of each block

 imgpel **imgY_ref;                                //!< reference frame find snr
 imgpel ***imgUV_ref;

 int  ReMapRef[20];
// B pictures
  int  Bframe_ctr;
 int  frame_no;

int  g_nFrame;

// For MB level frame/field coding
int  TopFieldForSkip_Y[16][16];
 int  TopFieldForSkip_UV[2][16][16];

 int  InvLevelScale4x4Luma_Intra[6][4][4];
int  InvLevelScale4x4Chroma_Intra[2][6][4][4];

int  InvLevelScale4x4Luma_Inter[6][4][4];
 int  InvLevelScale4x4Chroma_Inter[2][6][4][4];

 int  InvLevelScale8x8Luma_Intra[6][8][8];

int  InvLevelScale8x8Luma_Inter[6][8][8];

int  *qmatrix[8];
 char errortext[ET_SIZE]; //!< buffer for error message for exit with error()
int mprRGB[3][16][16];
 int rec_res[3][16][16];
 int  (*nal_startcode_follows) ();

// I have started to move the inp and img structures into global variables.
// They are declared in the following lines.  Since inp is defined in conio.h
// and cannot be overridden globally, it is defined here as input
//
// Everywhere, input-> and img-> can now be used either globally or with
// the local override through the formal parameter mechanism

extern FILE* bits;
extern StorablePicture* dec_picture;

struct inp_par    *input;       //!< input parameters from input configuration file
//struct snr_par    *snr;         //!< statistics
struct img_par    *img;         //!< image parameters

int global_init_done = 0;




//void Configure(int ac, char *av[])
void Configure()
{
  int CLcount;
//  char *config_filename=NULL;
  CLcount = 1;

  
  strcpy(input->infile,"test.264");      //! set default bitstream name
  strcpy(input->outfile,"test_dec.yuv"); //! set default output file name
 // strcpy(input->reffile,"test_rec.yuv"); //! set default reference file name
  input->FileFormat = PAR_OF_ANNEXB;
  
  input->FileFormat = PAR_OF_RTP;
  //input->ref_offset=0;
 // input->poc_scale=1;




#if TRACE
  if ((p_trace=fopen(TRACEFILE,"w"))==0)             // append new statistic at the end
  {
    snprintf(errortext, ET_SIZE, "Error open file %s!",TRACEFILE);
    error(errortext,500);
  }
#endif

  if((p_out2=fopen(input->outfile,"wb"))==NULL)
  //if ((p_out=open(input->outfile, OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
  {
    snprintf(errortext, ET_SIZE, "Error open file %s ",input->outfile);
    error(errortext,500);
  }

/*if ((p_out2=fopen(input->outfile,"wb"))==NULL)
  {
    snprintf(errortext, ET_SIZE, "Error open file %s ",input->outfile);
    error(errortext,500);
  }
  */

  fprintf(stdout,"----------------------------- JM %s %s -----------------------------\n", VERSION, EXT_VERSION);
//  fprintf(stdout," Decoder config file                    : %s \n",config_filename);
  fprintf(stdout,"--------------------------------------------------------------------------\n");
  fprintf(stdout," Input H.264 bitstream                  : %s \n",input->infile);
  fprintf(stdout," Output decoded YUV                     : %s \n",input->outfile);
  //fprintf(stdout," Output status file                     : %s \n",LOGFILE);
//  if ((p_ref=open(input->reffile,OPENFLAGS_READ))==-1)
//  {
 //   fprintf(stdout," Input reference file                   : %s does not exist \n",input->reffile);
 //   fprintf(stdout,"                                          SNR values are not available\n");
 // }
 // else
   // fprintf(stdout," Input reference file                   : %s \n",input->reffile);

  fprintf(stdout,"--------------------------------------------------------------------------\n");

  fprintf(stdout,"POC must = frame# or field# for SNRs to be correct\n");
  fprintf(stdout,"--------------------------------------------------------------------------\n");
  fprintf(stdout,"  Frame       POC   Pic#   QP  Y:U:V  Time(ms)\n");
  fprintf(stdout,"--------------------------------------------------------------------------\n");
  
}

/*!
 ***********************************************************************
 * \brief
 *    main function for TML decoder
 ***********************************************************************
 */
//int main(int argc, char **argv)
int main()
{
  // allocate memory for the structures
  if ((input =  (struct inp_par *)calloc(1, sizeof(struct inp_par)))==NULL) no_mem_exit("main: input");//zsj 051206分配内存单元
  //if ((snr   =  (struct snr_par *)calloc(1, sizeof(struct snr_par)))==NULL) no_mem_exit("main: snr");
  if ((img   =  (struct img_par *)calloc(1, sizeof(struct img_par)))==NULL) no_mem_exit("main: img");


 // Configure (argc, argv);//zsj 051206配置input结构
  Configure ();
  
  init_old_slice();//zsj 051206初始化 old_slice *    set defaults for old_slice   NAL unit of a picture
/*
  switch (input->FileFormat)//zsj 051206根据输入码流文件的格式确定是BitstreamFile  or RTPFile  
  //这里我们设置为  PAR_OF_ANNEXB
  {
  case 0:
    OpenBitstreamFile (input->infile);//zsj 051206打开比特流文件
    break;
  case 1:
    OpenRTPFile (input->infile);
    break;
  default:
    printf ("Unsupported file format %d, exit\n", input->FileFormat);
  }*/
    //OpenBitstreamFile (input->infile);//zsj 051206打开比特流文件
  bits=fopen(input->infile, "rb");
  // Allocate Slice data struct
  malloc_slice(input,img);//zsj 051206 分配struct img_par *img

  init(img);//zsj 051206   初始化struct img_par *img  中的 img->oldFrameSizeInMbs = -1;

  dec_picture = NULL;//zsj 051206  解码图像为空

  dpb.init_done = 0;//zsj 051206    decoderpicturebuffer
  g_nFrame = 0;//???zsj 051206 

  init_out_buffer();//zsj 051206  Initialize output buffer for direct output

//  img->idr_psnr_number=input->ref_offset;
//  img->psnr_number=0;

  img->number=0;//frame number zsj 051206 
  img->type = I_SLICE;//第一帧为I帧 zsj 051206 
  img->dec_ref_pic_marking_buffer = NULL;//DecRefPicMarking_t 解码参考帧的管理   可参考peter lee的blog
  //H.264解码器中参考图像的管理 zsj 051206 

  // B pictures
//  Bframe_ctr=snr->frame_ctr=0;///zsj 051206   b-counter

  // time for total decoding session
//  tot_time = 0;
  //while (decode_one_frame(img, input, snr) != EOS)//zsj 051206   解码过程
    while (decode_one_frame(img, input) != EOS)//zsj 051206   解码过程
    ;

//  report(input, img, snr);//zsj 051206  打印输出信息
	report(input, img);//zsj 051206  打印输出信息
  free_slice(input,img);//释放input img内存
  FmoFinit();//   Free memory allocated by FMO functions
  free_global_buffers();//Free allocated memory of frame size related global buffers

  flush_dpb();//All stored picture are output. Should be called to empty the buffer



  CloseBitstreamFile();//   Closes the bit stream file

  //close(p_out);
 fclose(p_out2);
 // if (p_ref!=-1)
    //close(p_ref);
#if TRACE
  fclose(p_trace);
#endif

  ercClose(erc_errorVar);

  free_dpb();
  uninit_out_buffer();

  free_colocated(Co_located);
  free (input);
//  free (snr);
  free (img);
  
  //while( !kbhit() ); 
  return 0;
}


/*!
 ***********************************************************************
 * \brief
 *    Initilize some arrays
 ***********************************************************************
 */
void init(struct img_par *img)  //!< image parameters
{
  img->oldFrameSizeInMbs = -1;

  imgY_ref  = NULL;
  imgUV_ref = NULL;
}

/*!
 ***********************************************************************
 * \brief
 *    Initilize FREXT variables
 ***********************************************************************
 */
void init_frext(struct img_par *img)  //!< image parameters
{
  //pel bitdepth init
  img->bitdepth_luma_qp_scale   = 6*(img->bitdepth_luma   - 8);
  if(img->bitdepth_luma > img->bitdepth_chroma || active_sps->chroma_format_idc == YUV400)
    img->pic_unit_bitsize_on_disk = (img->bitdepth_luma > 8)? 16:8;
  else
    img->pic_unit_bitsize_on_disk = (img->bitdepth_chroma > 8)? 16:8;
  img->dc_pred_value = 1<<(img->bitdepth_luma - 1);
  img->max_imgpel_value = (1<<img->bitdepth_luma) - 1;

  if (active_sps->chroma_format_idc != YUV400)
  {
    //for chrominance part
    img->bitdepth_chroma_qp_scale = 6*(img->bitdepth_chroma - 8);
    img->max_imgpel_value_uv      = (1<<img->bitdepth_chroma) - 1;
    img->num_blk8x8_uv = (1<<active_sps->chroma_format_idc)&(~(0x1));
    img->num_cdc_coeff = img->num_blk8x8_uv<<1;
    img->mb_cr_size_x  = (active_sps->chroma_format_idc==YUV420 || active_sps->chroma_format_idc==YUV422)? 8:16;
    img->mb_cr_size_y  = (active_sps->chroma_format_idc==YUV444 || active_sps->chroma_format_idc==YUV422)? 16:8;

    // Residue Color Transform
    if(img->residue_transform_flag)
      img->bitdepth_chroma_qp_scale += 6;
  }
  else
  {
    img->bitdepth_chroma_qp_scale = 0;
    img->max_imgpel_value_uv      = 0;
    img->num_blk8x8_uv = 0;
    img->num_cdc_coeff = 0;
    img->mb_cr_size_x  = 0;
    img->mb_cr_size_y  = 0;
  }

}


/*!
 ************************************************************************
 * \brief
 *    Read input from configuration file
 *
 * \par Input:
 *    Name of configuration filename
 *
 * \par Output
 *    none
 ************************************************************************
 */
//void init_conf(struct inp_par *inp, char *config_filename)
//void init_conf(struct inp_par *inp)
//{
  //FILE *fd;
  //int NAL_mode;

  // read the decoder configuration file
/*
  if((fd=fopen(config_filename,"r")) == NULL)
  {
    snprintf(errortext, ET_SIZE, "Error: Control file %s not found\n",config_filename);
    error(errortext, 300);
  }

  fscanf(fd,"%s",inp->infile);                // H.264 compressed input bitstream
  fscanf(fd,"%*[^\n]");

  fscanf(fd,"%s",inp->outfile);               // RAW (YUV/RGB) output file
  fscanf(fd,"%*[^\n]");

  fscanf(fd,"%s",inp->reffile);               // reference file
  fscanf(fd,"%*[^\n]");

  fscanf(fd,"%d",&(inp->write_uv));           // write UV in YUV 4:0:0 mode
  fscanf(fd,"%*[^\n]");
  
  fscanf(fd,"%d",&(NAL_mode));                // NAL mode
  fscanf(fd,"%*[^\n]");

  switch(NAL_mode)
  {
  case 0:
    inp->FileFormat = PAR_OF_ANNEXB;
    break;
  case 1:
    inp->FileFormat = PAR_OF_RTP;
    break;
  default:
    snprintf(errortext, ET_SIZE, "NAL mode %i is not supported", NAL_mode);
    error(errortext,400);
  }

  fscanf(fd,"%d,",&inp->ref_offset);   // offset used for SNR computation
  fscanf(fd,"%*[^\n]");

  fscanf(fd,"%d,",&inp->poc_scale);   // offset used for SNR computation
  fscanf(fd,"%*[^\n]");


  if (inp->poc_scale < 1 || inp->poc_scale > 10)
  {
    snprintf(errortext, ET_SIZE, "Poc Scale is %d. It has to be within range 1 to 10",inp->poc_scale);
    error(errortext,1);
  }

  inp->write_uv=1;


  fclose (fd);

}
*/

/*!
 ************************************************************************
 * \brief
 *    Reports the gathered information to appropriate outputs
 *
 * \par Input:
 *    struct inp_par *inp,
 *    struct img_par *img,
 *    struct snr_par *stat
 *

⌨️ 快捷键说明

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