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

📄 main.c

📁 基于H.263的图像压缩编解码的C源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/************************************************************************
 *
 *  main.c, main module of tmn (TMN encoder).
 *  tmn is an H.263 encoder somewhat based on the Test Model Near-term
 *  (TMN5) in the ITU-T LBC Experts Group.
 *
 ************************************************************************/

#include"sim.h"

FILE *streamfile;

void main(int argc, char *argv[])
{
  PictImage *prev_image = NULL;
  PictImage *curr_image = NULL;
  PictImage *curr_recon = NULL;
  PictImage *prev_recon = NULL;

  /* PB-frame specific */
  PictImage *B_recon = NULL;
  PictImage *B_image = NULL;

  Pict *pic = (Pict *)malloc(sizeof(Pict));
  unsigned char *image;
  FILE *cleared;

  int i, check_image_sum;
  float mean_frame_rate, ref_frame_rate, frame_rate, seconds;
  int first_loop_finished=0;
  int total_frames_passed, PPFlag = 0, targetrate;
  int frames,bframes,pframes,wcopies,icopies, write_repeated,pdist=0,bdist=0;
  int start, end, frame_no, writediff;  
  int first_frameskip, chosen_frameskip, orig_frameskip, frameskip;
  int QP,QPI;
  Times *times = (Times *)malloc(sizeof(Times));
  Bits *bits = (Bits *)malloc(sizeof(Bits));
  Bits *total_bits = (Bits *)malloc(sizeof(Bits));
  Bits *intra_bits = (Bits *)malloc(sizeof(Bits));
  Results *res = (Results *)malloc(sizeof(Results));
  Results *total_res = (Results *)malloc(sizeof(Results));
  Results *b_res = (Results *)malloc(sizeof(Results));
  char *seqfilename = (char *)malloc(sizeof(char)*100);
  char *streamname = (char *)malloc(sizeof(char)*100);
  char *outputfile =  (char *)malloc(sizeof(char)*100);
  char *diff_filename=DEF_DIFFILENAME;
  char *tracefile =  (char *)malloc(sizeof(char)*100);
  char *file_save_in = (char *)malloc(sizeof(char)*100);

#ifndef OFFLINE_RATE_CONTROL
  float DelayBetweenFramesInSeconds;
  int CommBacklog;
#else
  PictImage *stored_image = NULL;
  int start_rate_control;
#endif

  extern int arith_used;

  fprintf (stdout,"\nH.263 coder version 2.0, Copyright (C) 1995, 1996 ,1997, 1998.\n");

  headerlength = DEF_HEADERLENGTH;

#ifndef FASTIDCT
  init_idctref();
#endif

  /* Default variable values */
  time_flag = DEF_TIME_MODE;
  advanced = DEF_ADV_MODE;
  syntax_arith_coding = DEF_SAC_MODE;
  pic->unrestricted_mv_mode = DEF_UMV_MODE;
  mv_outside_frame = DEF_UMV_MODE || DEF_ADV_MODE;
  long_vectors = DEF_UMV_MODE;
  pb_frames = DEF_PBF_MODE;

  QP = DEF_INTER_QUANT;
  QPI = DEF_INTRA_QUANT; 
  pic->BQUANT = DEF_BQUANT; 
  pic->source_format = DEF_CODING_FORMAT;

  ref_frame_rate = (float)DEF_REF_FRAME_RATE;
  chosen_frameskip = DEF_FRAMESKIP + 1;
  orig_frameskip = DEF_ORIG_SKIP + 1;
#ifdef OFFLINE_RATE_CONTROL
  start_rate_control = DEF_START_RATE_CONTROL;
#else
  pic->target_frame_rate = (float)DEF_TARGET_FRAME_RATE;
#endif

  seqfilename[0] = '\0';
  strcpy(streamname, DEF_STREAMNAME);
  strcpy(outputfile, DEF_OUTFILENAME);

  writediff = DEF_WRITE_DIFF;
  trace = DEF_WRITE_TRACE;
  write_repeated = DEF_WRITE_REPEATED;
  pic->seek_dist = DEF_SEEK_DIST;
  pic->use_gobsync = DEF_INSERT_SYNC;
  start = DEF_START_FRAME;
  end = DEF_STOP_FRAME;

  targetrate = 0; 
  /* default is variable bit rate (fixed quantizer) will be used */

  frames = 0;
  pframes = 0;
  bframes = 0;
  total_frames_passed = 0;
  pic->PB = 0;
  wcopies = icopies = 1;

  pic->TR = 0;
  pic->QP_mean = (float)0.0;


  global_file_save_flag = 0;

  /* Process arguments */
  for (i = 1; i < argc; i++) {
    if (*(argv[i]) == '-') {
      switch(*(++argv[i])) {
      case 'a':
        start = atoi(argv[++i]);
        break;
      case 'b':
        end = atoi(argv[++i]);
        break;
      case 'S':
        chosen_frameskip = atoi(argv[++i]) + 1;
        break;
      case 'O':
        orig_frameskip = atoi(argv[++i]) + 1;
        break;
      case 's':
        pic->seek_dist = atoi(argv[++i]);
        break;
      case 'o':
        strcpy(outputfile, argv[++i]);
        break;
      case 'e':
        headerlength = atoi(argv[++i]);
        break;
      case 'm':
        write_repeated = ON;
        break;
      case 'i':
        strcpy(seqfilename, argv[++i]);
        break;
      case 'q':
        QP = atoi(argv[++i]);
        if (QP > 31 || QP < 0) {
          fprintf(stderr,"QP out of range - clipping it\n");
          QP = mmin(31,mmax(0,QP));
        }
        break;
      case 'I':
        QPI = atoi(argv[++i]);
        if (QPI > 31 || QPI < 0) {
          fprintf(stderr,"QP out of range - clipping it\n");
          QPI = mmin(31,mmax(0,QPI));
        }
        break;
      case 'w':
        writediff = ON;
        break;
      case 'B':
        strcpy(streamname, argv[++i]);
        break;
	  case 'p':
		global_file_save_flag = 1;
        strcpy(file_save_in, argv[++i]);
		break;
      case 'h':
        Help();
        exit(0);
        break;
      case 'H':
        AdvancedHelp();
        exit(0);
        break;
      case 't':
        trace = 1;
        break;
      case 'g':
        pic->use_gobsync = atoi(argv[++i]);;
        break;
	  case 'T':
        time_flag = 1;
		break;
      case 'D':
        /* note that the Unrestricted Motion Vector mode turns on 
           both long_vectors and mv_outside_frame */
        pic->unrestricted_mv_mode = ON;
        mv_outside_frame = ON;
        long_vectors = ON;
        break;
      case 'E':
        syntax_arith_coding = ON;
        break;
      case 'F':
        /* note that the Advanced Prediction mode turns on both 
           advanced (8x8 vectors and OBMC) and mv_outside_frame */
        /* the Extended Motion Vector mode is included in the
           Unrestricted Motion Vector mode, which of course can be
           use together with the Advanced Prediction mode */
        advanced = ON;
        mv_outside_frame = ON;
        break;
      case 'G':
        pb_frames = ON;
        break;
      case 'Q':
        pic->BQUANT = atoi(argv[++i]);
        break;
      case 'r':
        targetrate = atoi(argv[++i]);
        break;
#ifdef OFFLINE_RATE_CONTROL
      case 'R':
        start_rate_control = atoi(argv[++i]);
        break;
#else
      case 'R':
        pic->target_frame_rate = (float)atof(argv[++i]);
        break;
#endif
      case 'Z':
        ref_frame_rate = (float)atoi(argv[++i]);
        break;
      case 'x':
	pic->source_format =  atoi(argv[++i]);
	break;
      default:
        fprintf(stderr,"Illegal option: %c\n",*argv[i]);
        Help();
        exit(-1);
        break;
      }
    }
  }
  
  switch (pic->source_format) {
  case (SF_SQCIF):
    fprintf(stdout, "Encoding format: SQCIF (128x96)\n");
    pels  = 128;
    lines =  96;
    break;
  case (SF_QCIF):
    fprintf(stdout, "Encoding format: QCIF (176x144)\n");
    pels  = 176;
    lines = 144;
    break;
  case (SF_CIF1):
    fprintf(stdout, "Encoding format: CIF (352x288)\n");
    pels  = 352;
    lines = 288;
    break;
  case (SF_CIF2):
	fprintf(stdout, "Encoding format: CIF (352x288)\n");
	pels  = 352;
	lines = 240;
	break;
  case (SF_4CIF):
    fprintf(stdout, "Encoding format: 4CIF (704x576)\n");
    pels  = 704;
    lines = 576;
    break;
  case (SF_16CIF):
    fprintf(stdout, "Encoding format: 16CIF (1408x1152)\n");
    pels  = 1408;
    lines = 1152;
    break;
  default:
    fprintf(stderr,"Illegal coding format\n");
    exit(-1);
  }
  cpels = pels/2;

  if (seqfilename[0] == '\0') {
    fprintf(stderr,"Required input parameter \'-i <filename>\' missing\n");
    Help();
    exit(-1);
  }

  /* if global_file_save_flag == 1, then open outfile as save */
  if(global_file_save_flag == 1){
	  message_in = fopen(file_save_in,"wt");
	  if(message_in == NULL){
		  fprintf(stderr, "PSNR file write error !\n");
		  exit(-1);
	  }
	  fprintf(message_in,"Frame_num,Y_PSNR,Cb_PSNR,Cr_PSNR,Total_bits,Frame_header_bits,Vectors\n");
  }
#ifndef OFFLINE_RATE_CONTROL
  /* rate control variables */
  pic->bit_rate = targetrate;
  pic->src_frame_rate = (int)(ref_frame_rate / orig_frameskip);
  DelayBetweenFramesInSeconds = (float) 1.0/(float)pic->src_frame_rate;
  InitializeRateControl();
#endif

  if (QP == 0 || QPI == 0) {
    fprintf(stderr,"Warning:");
    fprintf(stderr,"QP is zero. Bitstream will not be correctly decodable\n");
  }

  if (ref_frame_rate != 25.0 && ref_frame_rate != 30.0) {
    fprintf(stderr,"Warning: Reference frame rate should be 25 or 30 fps\n");
  }

  frame_rate =  ref_frame_rate / (float)(orig_frameskip * chosen_frameskip);
#ifdef OFFLINE_RATE_CONTROL
  fprintf(stdout,"Encoding frame rate  : %.2f\n", frame_rate);
#else
  if (pic->bit_rate == 0)
    fprintf(stdout,"Encoding frame rate  : %.2f\n", frame_rate);
  else
    fprintf(stdout,"Encoding frame rate  : variable\n");
#endif
  fprintf(stdout,"Reference frame rate : %.2f\n", ref_frame_rate);
  fprintf(stdout,"Orig. seq. frame rate: %.2f\n\n", 
          ref_frame_rate / (float)orig_frameskip);

  /* Open stream for writing */
  streamfile = fopen (streamname, "wb");
  if (streamname == NULL) {
    fprintf(stderr,"Unable to open streamfile\n");
    exit(-1);
  }	

  /* Initialize bitcounters */
  initbits ();
  
  if (trace) {
    strcpy(tracefile, "trace.intra");
    /* Open trace-file for writing */
    if ((tf = fopen(tracefile,"w")) == NULL) {
      fprintf(stderr,"Unable to open tracefile (intra)\n");
      exit(-1);
    }
  }
  /* Clear output files */
  if ((cleared = fopen(outputfile,"wb")) == NULL) {

⌨️ 快捷键说明

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