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

📄 dec-patt.c

📁 Reference Implementation of G.711 standard and other voice codecs
💻 C
📖 第 1 页 / 共 2 页
字号:
      {	/* Save bitstream as a byte-oriented serial bitstream */	save_format = byte;	save_patt = save_byte;	/* Move arg{c,v} over the option to the next argument */	argc--;	argv++;      }      else if (strcmp (argv[1], "-compact") == 0 ||	       strcmp (argv[1], "-bit") == 0)      {	/* Save bitstream as a compact binary bitstream */	save_format = compact;	save_patt = save_bit;	/* Move arg{c,v} over the option to the next argument */	argc--;	argv++;      }      else if (strcmp (argv[1], "-q") == 0)      {	/* Set quiet mode */	quiet = 1;	/* Move arg{c,v} over the option to the next argument */	argc--;	argv++;      }      else      {	fprintf (stderr, "ERROR! Invalid option \"%s\" in command line\n\n",		 argv[1]);	display_usage ();      }  }  /* Get command line parameters */  GET_PAR_S (1, "_Master error pattern file ...............: ", master_ep);  GET_PAR_S (2, "_Decimated error pattern file ............: ", new_ep);  FIND_PAR_L (3, "_Decimation factor .......................: ",	      dec_fac, dec_fac);  /* Starting frame is from 0 to number_of_frames-1 */  start_frame--;  /* Open files */  if ((Fi= fopen (master_ep, RB)) == NULL)    HARAKIRI ("Could not open input bitstream file\n", 1);  if ((Fo= fopen (new_ep, WB)) == NULL)    HARAKIRI ("Could not create output file\n", 1);  /* *** CHECK CONSISTENCY *** */  /* Do preliminary inspection in the INPUT BITSTREAM FILE to check     its format (byte, bit, g192) */  i = check_eid_format(Fibs, ibs_file, &tmp_type);  /* Check whether the specified BS format matches with the one in the file */  if (i != bs_format)  {    /* The input bitstream format is not the same as specified */    fprintf (stderr, "*** Switching bitstream format from %s to %s ***\n",	     format_str((int)bs_format), format_str(i));    bs_format = i;  }  /* Check whether the BS has a sync header */  if (tmp_type == FER)  {    /* The input BS may have a G.192 synchronism header - verify */    if (bs_format == g192)    {      short tmp[2];      /* Get presumed first G.192 sync header */      fread(tmp, sizeof(short), 2, Fibs);      /* tmp[1] should have the frame length */      i = tmp[1];      /* advance file to the (presumed) next G.192 sync header */      fseek(Fibs, (long)(tmp[1])*sizeof(short), SEEK_CUR);      /* get (presumed) next G.192 sync header */      fread(tmp, sizeof(short), 2, Fibs);      /* Verify */      if (((tmp[0] & 0xFFF0) == 0x6B20) && (i == tmp[1]))      {	fr_len = i;	sync_header = 1;      }      else	sync_header = 0;    }    else if (bs_format == byte)    {      char tmp[2];      /* Get presumed first byte-wise G.192 sync header */      fread(tmp, sizeof(char), 2, Fibs);      /* tmp[1] should have the frame length */      i = tmp[1];      /* advance file to the (presumed) next byte-wise G.192 sync header */      fseek(Fibs, (long)tmp[1], SEEK_CUR);      /* get (presumed) next G.192 sync header */      fread(tmp, sizeof(char), 2, Fibs);      /* Verify */      if (((tmp[0] & 0xF0) == 0x20) && (i == tmp[1]))      {	fr_len = i;	sync_header = 1;      }      else	sync_header = 0;    }    else      sync_header = 0;    /* Rewind file */    fseek(Fibs, 0l, SEEK_SET);  }  /* If input BS is headerless, any frame size will do; using default */  if (fr_len == 0)    fr_len = blk;  /* Also, reset sync if BS is compact */  if (bs_format == compact && sync_header)  {    fprintf (stderr, "*** Disabling SYNC header for compact bitstream ***\n");    sync_header = 0;  }  /* *** FINAL INITIALIZATIONS *** */  /* Use the proper data I/O functions */  read_patt = ep_format==byte? read_byte              : (ep_format==g192? read_g192 :		 (ep_type==BER? read_bit_ber : read_bit_fer));  save_patt = ep_format==byte? save_byte              : (ep_format==g192? save_g192 : save_bit);  /* Define how many samples are read for each frame */  /* Bitstream may have sync headers, which are 2 samples-long */  bs_len = sync_header? fr_len + 2 : fr_len;  ep_len = fr_len;  /* Allocate memory for data buffers */  if ((master = (short *)calloc(fr_len, sizeof(short))) == NULL)    HARAKIRI("Can't allocate memory for master error pattern. Aborted.\n",6);  if ((new = (short *)calloc(fr_len, sizeof(short))) == NULL)    HARAKIRI("Can't allocate memory for new error pattern. Aborted.\n",6);  /* Initializes to the start of the payload in input bitstream */  payload = sync_header? bs + 2: bs;  /* *** START ACTUAL WORK *** */  switch(ep_type)  {  case FER:    k = 0;    while(1)    {      /* Read one frame from bitstream */      items = read_data (bs, bs_len, Fibs);      /* Stop when reaches end-of-file */      if (items==0)	break;      /* Aborts on error */      if (items < 0)	KILL(ibs_file, 7);      /* Check if read all expected samples; if not, take a special action */      if (items < bs_len)      {	if (sync_header)	{	  /* If the bitstream has sync header, this situation should	     not occur, since the length of the input bitstream file	     should be a multiple of the frame size! The file is	     either invalid otr corrupt. Execution is aborted at this	     point */	  HARAKIRI("File size not multiple of frame length. Aborted!\n", 9);	}	else if (feof(Fibs))	{	  /* EOF reached. Since the input bitstream is headerless,             this maybe a corrupt file, or the user simply specified             the wrong frame size. Warn the user and continue */	  fprintf(stderr, "*** File size not multiple of frame length ***\n");	  bs_len = fr_len = items;	}	else /* An unknown error happened! */	  KILL(ibs_file, 7);      }      /* Read a number of erasure flags from file */      while (k==0)      {	/* No EP flags in buffer; read a number of them */	k = read_patt (ep, ep_len, Fep);	/* No flags read - either error or EOF */	/* Go back to beginning of EP & fill up EP buffer */	if (k<=0)	{	  if (k < 0)	    KILL(ep_file, 7);	    /* Error: abort */	  fseek(Fep, 0l, SEEK_SET); /* EOF: Rewind */	}	/* Count how many times wrapped the EP file */	wraps++;      }      /* Update frame counters */      proc_master++;      /* Save original or erased frame, as appropriate */      if (ep[ep_len - k] == G192_FER)      {	items = save_data (erased_frame, bs_len, Fobs);	disturb_master ++;      }      else	items = save_data (bs, bs_len, Fobs);      /* Abort on error */      if (items < bs_len)	KILL(obs_file, 7);      /* Decrement counter of number of flags in EP buffer */      k--;    }    break;  case BER:    while(1)    {      /* Read one frame from bitstream */      items = read_data (bs, bs_len, Fibs);      /* Stop when reaches end-of-file */      if (items==0)	break;      /* Aborts on error */      if (items < 0)	KILL(ibs_file, 7);      /* Check if read all expected samples; if not, probably hit EOF */      if (items < bs_len)      {	if (sync_header)	{	  /* If the bitstream has sync header, this situation should	     not occur, since the length of the input bitstream file	     should be a multiple of the frame size! The file is	     either invalid otr corrupt. Execution is aborted at this	     point */	  HARAKIRI("File size not multiple of frame length in header. Aborted!\n", 9);	}	else if (feof(Fibs))	{	  /* EOF reached. Since the input bitstream is headerless,             this maybe a corrupt file, or the user simply specified             the wrong frame size. Warn the user and continue */	  fprintf(stderr, "*** File size not multiple of given frame length ***\n");	  bs_len = fr_len = items;	}	else /* An unknown error happened! */	  KILL(ibs_file, 7);      }      /* Read one error pattern frame from file */      items = read_patt (ep, ep_len, Fep);      /* Treat case when EP finishes before BS: */      /* Go back to beginning of EP & fill up EP buffer */      if (items < ep_len)      {	if (items < 0)	  KILL(ep_file, 7);	k = ep_len - items; /* Number of missing EP samples */	fseek(Fep, 0l, SEEK_SET); /* Rewind */	items = read_patt (&ep[items], k, Fep); /* Fill-up EP buffer */	/* Count how many times wrapped the EP file */	wraps++;       }      /* Convolve errors */      items = insert_errors (payload, ep, payload, fr_len);      /* Update BER counters */      disturb_master += items;      proc_master += fr_len;     /* Save disturb_master bitstream to file */      items = save_data (bs, bs_len, Fobs);      /* Abort on error */      if (items < bs_len)	KILL(obs_file, 7);    }    break;  }  /* ***  PRINT SUMMARY OF OPTIONS & RESULTS ON SCREEN *** */  /* Print summary */  fprintf (stderr, "# Pattern format %s....... : %s\n",	   ep_type == FER? "(Frame erasure) " : "(Bit error) ....", 	   format_str((int)ep_format));  fprintf (stderr, "# Frame size ............................: %ld\n",	   fr_len);  fprintf (stderr, "# Processed %s..................... : %.0f \n",	   ep_type==BER? "bits .." : "frames ", proc_master);  fprintf (stderr, "# Master EP - Distorted %s.......... : %.0f \n",	   ep_type==BER? "bits .." : "frames ", disturb_master);  fprintf (stderr, "# Master EP %s...........: %f %%\n",	   ep_type==BER? "Bit error rate ..." : "Frame erasure rate", 	   100.0 * disturb_master / proc_master);  fprintf (stderr, "# Output EP - Distorted %s.......... : %.0f \n",	   ep_type==BER? "bits .." : "frames ", disturb_new);  fprintf (stderr, "# Output EP %s...........: %f %%\n",	   ep_type==BER? "Bit error rate ..." : "Frame erasure rate", 	   100.0 * disturb_new / proc_new);  /* *** FINALIZATIONS *** */  /* Free memory allocated */  free(new);  free(master);  /* Close the output file and quit *** */  fclose (Fi);  fclose (Fo);#ifndef VMS			/* return value to OS if not VMS */  return 0;#endif}#endif /* Scheleton */

⌨️ 快捷键说明

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