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

📄 spdemo.c

📁 Reference Implementation of G.711 standard and other voice codecs
💻 C
📖 第 1 页 / 共 2 页
字号:
}#undef P/* .................. End of display_usage() ....................... */ char *format_str(i)int i;{  switch(i)  {  case byte:    return "byte";    break;  case g192:    return "g192";    break;  case compact:    return "bit";    break;  }  return "";}/* ------------------------------------------------------------------------ *//*                              Main Program                                *//* ------------------------------------------------------------------------ */int main(argc, argv)int argc;char *argv[];{  long            N = 256, N1 = 1, N2 = 0, smpno, size, i;  long            fr_len = 0;           /* Frame length in number of bits */  long            bitno, cur_blk, resolution = 16;  char            bs_type = HAS_HEADER; /* Type for bitstream */  char            bs_format = g192;     /* Format for bitstream */  long            Nin, Nout;      /* Frame length for input and input files */  short          *par_buf;	        /* parallel-aligned samples' buffer */  short          *bit_stm;	        /* bit-stream data */  char            inpfil[127], outfil[127];  FILE           *inpfilptr, *outfilptr;  int             inp, out;  long            start_byte;  char            sync=1, oper[8];  static char     just[8] = "right";  short           inp_type, out_type;  /*   char            format_str[4][5] = {{"byte"}, {"g192"}, {"bit"}, {""}}; */#ifdef VMS  char            mrs[15];	/* for correct mrs in VMS environment */#endif  long		 (*serialize_f)();    /* pointer to serialization routine */  long           (*parallelize_f)();  /* pointer to parallelization routine */  char quiet=0;  /* ......... GET PARAMETERS ......... */  /* Check options */  if (argc < 2)    display_usage(0);  else  {    while (argc > 1 && argv[1][0] == '-')      if (strcmp (argv[1], "-start") == 0)      {	/* Define starting sample for s/p operation */	N1 = atol (argv[2]);	/* Move arg{c,v} over the option to the next argument */	argc -= 2;	argv += 2;      }      else if (strcmp (argv[1], "-frame") == 0)      {	/* Define input & output encoded speech bitstream format */	fr_len = atoi(argv[2]);	/* Move arg{c,v} over the option to the next argument */	argc -= 2;	argv += 2;      }      else if (strcmp (argv[1], "-n") == 0)      {	/* Define number of frames to process */	N2 = atoi(argv[2]);	/* Move arg{c,v} over the option to the next argument */	argc -= 2;	argv += 2;      }      else if (strcmp(argv[1],"-r")==0 || strcmp(argv[1],"-res")==0)      {	/* Define resolution */	resolution = atol(argv[2]);	/* Move arg{c,v} over the option to the next argument */	argc -= 2;	argv += 2;      }      else if (strcmp(argv[1], "-left") == 0 ||                strcmp(argv[1], "-LEFT") == 0)      {	/* Data is left-justified */	strcpy(just, "left");		/* Update arg[vc] */	argv++;	argc--;      }      else if (strcmp(argv[1], "-right") == 0 ||                strcmp(argv[1], "-RIGHT") == 0)      {	/* Data is right-justified */	strcpy(just, "right");		/* Update arg[vc] */	argv++;	argc--;      }      else if (strcmp(argv[1], "-sync") == 0)      {	/* Use sync header */	sync=1;		/* Update arg[vc] */	argv++;	argc--;      }      else if (strcmp(argv[1], "-nosync") == 0)      {	/* Don't use sync header */	sync=0;		/* Update arg[vc] */	argv++;	argc--;      }      else if (strcmp(argv[1], "-q") == 0)      {	/* quiet operation - don't print progress flag */	quiet=1;		/* Update arg[vc] */	argv++;	argc--;      }      else if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-?") == 0)      {	/* Display help */	display_usage(0);      }      else if (strstr(argv[1], "-help"))      {	/* Display help */	display_usage(1);      }      else if (strcmp(argv[1], "-") == 0)      {	/* End of option parsing */	break;      }      else      {	fprintf(stderr, "ERROR! Invalid option \"%s\" in command line\n\n",		argv[1]);	display_usage(0);      }  }  /* Read parameters for processing */  GET_PAR_S(1,  "_Operation (ps,sp): .......... ", oper);  GET_PAR_S(2,  "_File to be converted: ....... ", inpfil);  GET_PAR_S(3,  "_Output File: ................ ", outfil);  FIND_PAR_L(4, "_Block Length: ............... ", N, N);  FIND_PAR_L(5, "_Start Block: ................ ", N1, N1);  FIND_PAR_L(6, "_No. of Blocks: .............. ", N2, N2);  FIND_PAR_L(7, "_Resolution of input data: ... ", resolution, resolution);  FIND_PAR_I(8, "_Sync header? (1=Yes,0=No) ... ", sync, sync);  FIND_PAR_S(9, "_Left of right adjusted? ..... ", just, just); /* Classification of the conversion desired */  inp_type = is_serial(oper[0])? IS_SERIAL : IS_PARALLEL;  out_type = is_serial(oper[1])? IS_SERIAL : IS_PARALLEL;  /* Verify if valid input/output combination */  /* If input is parallel, output must be serial */  /* If input is serial, output must be parallel */  if (inp_type == out_type)    HARAKIRI(" Invalid input/output combination of option [sp|ps]!\n", 7); /* Verify if sync is valid; if not, change to 1 */  if (sync!=1 && sync!=0)    sync = 1; /* Definition of the routines in function of the justification */  if (strcmp(just, "right")==0 || strcmp(just, "RIGHT")==0)  {     serialize_f = serialize_right_justified;     parallelize_f = parallelize_right_justified;  }  else  {     serialize_f = serialize_left_justified;     parallelize_f = parallelize_left_justified;  }  /* ***** FILE OPERATIONS ***** */#ifdef VMS  sprintf(mrs, "mrs=%d", 512);#endif  /* Open input file */  if ((inpfilptr = fopen(inpfil, RB)) == NULL)    KILL(inpfil, 2);  inp = fileno(inpfilptr);  /* Open (create) output file */  if ((outfilptr = fopen(outfil, WB)) == NULL)    KILL(outfil, 3);  out = fileno(outfilptr);  /* Define 1st byte to process */  start_byte = N * (--N1) * sizeof(short);    /* Move file's pointer to 1st desired block */  if (fseek(inpfilptr, start_byte, 0) < 0l)    KILL(inpfil, 4);  /* ***** DEFINE FRAME SIZES AND FILE TYPES ***** */  /* Get input and output frame sizes */  if (inp_type==IS_SERIAL && out_type==IS_PARALLEL)  { /* SP mode: */    /* Find the actual frame size for the serial bit stream from the       serial bitstream. If this is a headerless bitstream, assumes that       the frame size is based on the provided block size. */    /* Find the number of words per frame (payload only!),        check whether a sync header was found, and also returns the type        of bitstream (g192, byte, compact) found */    i = check_sync(inpfilptr, inpfil, &bs_type, &fr_len, &bs_format);    /* If bitstream is not g192, abort */    if (bs_format != g192)      HARAKIRI("Bitstream needs to be in G.192 format. Aborted.\n", 7);    /* If sync info in file differs from the users' provided       input, warns and changes to the value found in file */    if (sync != i)    {      fprintf (stderr, "*** Switching sync from %d to %ld ***\n",	       sync, i);      sync = i;    }    /* If input BS is headerless, any frame size will do; use default */    if (fr_len==0)      fr_len = N * resolution;    /* Set Nin to length of payload + header */    Nin = fr_len + (sync ? OVERHEAD : 0);    /* Increase Ni by size of header, if present */    /* Output frame size is the # of bits per frame divided       by the resolution */    Nout = Nin/resolution;    /* Print info */    fprintf(stderr, "# Input has %ld bits/frame %s sync header\n", 	    Nin, sync?"with":"without");    fprintf(stderr, "# Output data has %ld %ld-bit parallel samples/frame\n",	    Nout, resolution);  }  else  { /* PS mode: Input is parallel and output is serial */    /* Input frame size is as provided by the user */    Nin = N;     /* Output frame size defined by Nin and the resolution */    fr_len = N*resolution; /* Payload only */    Nout = fr_len  + (sync ? OVERHEAD : 0); /* Payload + header */    /* Print info */    fprintf(stderr, "# Input data has %ld %ld-bit parallel samples/frame\n",	    Nin, resolution);    fprintf(stderr, "# Output has %ld bits/frame %s sync header\n", 	    Nout, sync?"with":"without");  }  /* The following is a hook for future extensions */  switch(bs_format)  {  case g192:    size = sizeof(short);    break;  case byte:    size = sizeof(char);    break;  case compact:    size = sizeof(char);    break;  }  /* ***** ALLOCATION OF BUFFERS ***** */  /* Allocate memory for input and output buffers */  if (inp_type==IS_SERIAL && out_type==IS_PARALLEL)  {    if ((bit_stm = (short *) calloc(Nin, size)) == NULL)          HARAKIRI("Can't allocate memory for input buffer\n", 10);    if ((par_buf = (short *) calloc(Nout, sizeof(short))) == NULL)          HARAKIRI("Can't allocate memory for output buffer\n", 11);  }  else if (inp_type==IS_PARALLEL && out_type==IS_SERIAL)   {    if ((par_buf = (short *) calloc(Nin, sizeof(short))) == NULL)          HARAKIRI("Can't allocate memory for input buffer\n", 10);    if ((bit_stm = (short *) calloc(Nout, size)) == NULL)          HARAKIRI("Can't allocate memory for output buffer\n", 11);  }  /* Check if is to process the whole file */  if (N2 == 0)  {    struct stat     st;    stat(inpfil, &st);    N2 = ceil((st.st_size - start_byte) / (double)(Nin * size));  }  /* ***** PRINT PRELIMINARY INFORMATION ***** */  fprintf(stderr, "# Operation: %s\n", oper);  fprintf(stderr, "# Resolution: %ld\n", resolution);  fprintf(stderr, "# Bitstream format: %s\n", format_str((int)bs_format));  fprintf(stderr, "# Header: %s\n", sync? "present" : "none");  fprintf(stderr, "# Payload/total frame size: %ld / %ld\n",	  fr_len, fr_len + (sync ? OVERHEAD : 0) );  /* ***** CARRY OUT CONVERSION ***** */    smpno=bitno=0;  if (inp_type==IS_SERIAL && out_type==IS_PARALLEL)  {    /* Performs the serial-to-parallel conversion */    for (cur_blk = 0; cur_blk < N2; cur_blk++)      {        if (!quiet)	  fprintf(stderr, "\rProcessing block %ld\t", cur_blk+1);	if ((bitno = fread(bit_stm, size, Nin, inpfilptr)) < 0)	  KILL(inpfil, 5);	i += parallelize_f  	     (bit_stm,    /* input buffer pointer */	      par_buf,    /* output buffer pointer */	      bitno,      /* number of bits (not samples) per frame */	      resolution, /* number of bits per sample */	      sync);      /* whether sync header is present or not */	/* Check if error was returned */	if (i<0)	{	  fprintf(stderr, "Returned error %ld in S/P function; aborting\n", i);	  exit(10);	}	if (i==0)	  fprintf(stderr, "Detected erased frame at frame no. %ld; skipping\n",		 cur_blk);	else	  smpno += i;	if ((bitno = fwrite(par_buf, sizeof(short), Nout, outfilptr)) < 0)	  KILL(outfil, 6);      }      fprintf(stderr, "# Total %ld samples parallelized\n", smpno);  }  else if (inp_type==IS_PARALLEL && out_type==IS_SERIAL)  {    /* Performs the parallel-to-serial conversion */    for (cur_blk = 0; cur_blk < N2; cur_blk++)      {        if (!quiet)	  fprintf(stderr, "\rProcessing block %ld\t", cur_blk+1);	if ((smpno = fread(par_buf, sizeof(short), Nin, inpfilptr)) < 0)	  KILL(inpfil, 5);	bitno += serialize_f                  (par_buf,     /* input buffer pointer */                   bit_stm,     /* output buffer pointer */                   N,           /* number of samples (not bits!) per frame */                   resolution,  /* number of bits per sample */                   sync);       /* whether sync header is present or not */	if ((smpno = fwrite(bit_stm, size, Nout, outfilptr)) < 0)	  KILL(outfil, 6);      }      fprintf(stderr, "# Total %ld bits produced\n", bitno);  }  /* ***** EXITING ***** */  /* Release memory */  free(bit_stm);  free(par_buf);   /* And close files! */  fclose(outfilptr);  fclose(inpfilptr);    /* Return OK when not VMS */#ifndef VMS  return(0);#endif}/* ....................... End of main() program ....................... */ 

⌨️ 快捷键说明

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