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

📄 softbit.c

📁 Reference Implementation of G.711 standard and other voice codecs
💻 C
📖 第 1 页 / 共 2 页
字号:
  }  /* Convert byte-oriented data to word16-oriented data */  for (i=0; i<n; i++)  {    tmp = byte[i];    if (tmp == 0x20 || tmp == 0x21) /* It is a frame sync/erasure word */      patt[i] = 0x6B00 | tmp;    else      patt[i] = tmp;  }  /* Free memory and quit */  free(byte);  return(n);}/* ....................... End of read_byte() ....................... *//*   -------------------------------------------------------------------------  long save_g192 (short *patt, long n, FILE *F);  ~~~~~~~~~~~~~~  Save a G.192-compliant 16-bit serial bitstream error pattern.  Parameter:  ~~~~~~~~~~  patt .... headerless G.192 array with the softbits representing             the bit error/frame erasure pattern  n ....... number of softbits in the pattern  F ....... pointer to FILE where the pattern should be saved  Return value:   ~~~~~~~~~~~~~  Returns a long with the number of shorts saved to a file. On error,   returns -1.  Original author: <simao.campos@comsat.com>  ~~~~~~~~~~~~~~~~  History:  ~~~~~~~~  13.Aug.97  v.1.0  Created.  -------------------------------------------------------------------------*/long save_g192(patt, n, F)short *patt;long n;FILE *F;
{  /* Save words to file */  return(fwrite(patt, sizeof(short), n, F)<n ? -1l : n);}/* ....................... End of save_g192() ....................... *//*   -------------------------------------------------------------------------  long save_bit (short *patt, long n, FILE *F);  ~~~~~~~~~~~~~  Save a headerless G.192 error pattern as a bit-oriented file where the  LSb corresponds to the bit that occurs first in time.  Parameter:  ~~~~~~~~~~  patt .... headerless G.192 array with the softbits representing            the bit error/frame erasure pattern  n ....... number of softbits in the pattern  F ....... pointer to FILE where the pattern should be saved  Return value:   ~~~~~~~~~~~~~  Returns a long with the number of shorts saved to a file. On error,   returns -1.  Original author: <simao.campos@comsat.com>  ~~~~~~~~~~~~~~~~  History:  ~~~~~~~~  15.Aug.97  v.1.0  Created.  -------------------------------------------------------------------------*/#define IS_ONE(x)  ((x) && G192_ONE)long save_bit(patt, n, F)short *patt;long n;FILE *F;{  char *bits;  short one, *p = patt;  long i, j, k, nbytes;  char register tmp;  /* Skip function if no samples are to be read */  if (n==0)    return(0);  /* Calculate number of bytes necessary in the compact bitstream */  if (n%8)  {    fprintf(stderr, "The number of errors is not byte-aligned. \n");    fprintf(stderr, "Zero insertion will be used and need to be \n");    fprintf(stderr, "accounted for by the error-insertion program!\n");  }  nbytes= (long)(ceil(n/8.0));  /* Allocate memory */  if ((bits = (char *)calloc(nbytes, sizeof(char)))==NULL)    HARAKIRI ("Cannot allocate memory to save compact binary bitstream\n", 6);    /* Reset memory to zero */  memset(bits, 0, nbytes);  /* Scan to determine whether it is a bit error or a frame erasure array */  switch(*p)  {  case G192_ZERO:  case G192_ONE: /* Bit error */    one = G192_ONE;    break;  case G192_SYNC:  case G192_FER: /* Frame erasure */    one = G192_FER;    break;  }  /* Convert byte-oriented to compact bit oriented data */  for (i=j=0; j<nbytes; j++)  {    /* Get 1st bit ... */    tmp = (*p++==one) ? 1 : 0;     /* Compact all the other bits ... */    for (k=1; k<8 && i<n; k++, i++)    {      tmp += (unsigned char)( ( (*p++) == one ? 1 : 0) << k);    }     /* Save word as short */    bits[j] = tmp;  }  /* Save words to file */  i = fwrite(bits, sizeof(char), nbytes, F);  /* Free memory and quit */  free(bits);  return(n<i ? -1l : n);}/* ....................... End of save_bit() ....................... *//*   -------------------------------------------------------------------------  long save_byte (short *patt, long n, FILE *F);  ~~~~~~~~~~~~~~~  Save a G.192 error pattern as a byte-oriented serial bitstream  error pattern by writing to file only the lower byte of each 16-bit  word. The follwoing map is used:  0x007F -> 0x7F ('0' softbit)  0x0081 -> 0x81 ('1' softbit)  0x0021 -> 0x21 (Frame OK)  0x0020 -> 0x20 (Frame erasure)  NOTE: The user is responsible for having only these four values in        the input array. The code will NOT check for compliance.  Parameter:  ~~~~~~~~~~  patt .... char array with the softbits representing the bit            error/frame erasure pattern  n ....... number of softbits in the pattern  F ....... pointer to FILE where the pattern should be saved  Return value:   ~~~~~~~~~~~~~  Returns a long with the number of shorts saved to a file. On error,   returns -1.  Original author: <simao.campos@comsat.com>  ~~~~~~~~~~~~~~~~  History:  ~~~~~~~~  15.Aug.97  v.1.0  Created.  ------------------------------------------------------------------------- */long save_byte(patt, n, F)short *patt;long n;FILE *F;{  char *byte;  long i;  /* Skip function if no samples are to be read */  if (n==0)    return(0);  /* Allocate memory */  if ((byte = (char *)calloc(n, sizeof(char)))==NULL)    HARAKIRI ("Cannot allocate memory to save data as byte bitstream\n", 6);    /* Convert word16-oriented data to byte-oriented data */  /* NO compliance verification is performed, for performance reasons */  for (i=0; i<n; i++)    byte[i] = (unsigned char)(patt[i] & 0x00FF);  /* Save words to file */  i = fwrite(byte, sizeof(char), n, F);  /* Free memory and quit */  free(byte);  return(n<i ? -1l : n);}/* ....................... End of save_byte() ....................... *//*  ---------------------------------------------------------------------------  char *format_str (int fmt);  ~~~~~~~~~~~~~~~~~  Function to return a string with the description of the current  bitstream format (g192, byte, or bit).  Parameters:  fmt ... integer with the bitstream format  Returned value:  ~~~~~~~~~~~~~~~  Returns a pointer to the format string, or a NULL pointer  if fmt is invalid.   Original author: <simao.campos@comsat.com>  ~~~~~~~~~~~~~~~~  History:  ~~~~~~~~  21.Aug.97  v1.00 created  ---------------------------------------------------------------------------*/char *format_str(fmt)int fmt;{  switch(fmt)  {  case byte:    return "byte";    break;  case g192:    return "g192";    break;  case compact:    return "bit";    break;  }  return "";}/* ....................... End of format_str() ....................... */ /*  ---------------------------------------------------------------------------  char *type_str (int type);  ~~~~~~~~~~~~~~  Function to return a string with the description of the current  bitstream format (g192, byte, or bit).  Parameters:  type ... integer with the bitstream format  Returned value:  ~~~~~~~~~~~~~~~  Returns a pointer to the format string, or a NULL pointer  if type is invalid.   Original author: <simao.campos@comsat.com>  ~~~~~~~~~~~~~~~~  History:  ~~~~~~~~  21.Aug.97  v1.00 created  ---------------------------------------------------------------------------*/char *type_str(type)int type;{  switch(type)  {  case BER:    return "BER";    break;  case FER:    return "FER";    break;  }  return "";}/* ....................... End of type_str() ....................... */ /*  --------------------------------------------------------------------------  char check_eid_format (FILE *F, char *file, char *type);  ~~~~~~~~~~~~~~~~~~~~~  Function that checks the format (g192, byte, bit) in a given  bitstream, and tries to guess the type of data (bit stream or frame  erasure pattern)  Parameter:  ~~~~~~~~~~  F ...... FILE * structure to file to be checked  file ... name of file to be checked  type ... pointer to guessed data type (FER or BER) in file  Returned value:  ~~~~~~~~~~~~~~~  Returns the data format (g192, byte, bit) found in file.  Original author: <simao.campos@comsat.com>  ~~~~~~~~~~~~~~~~  History:  ~~~~~~~~  15.Aug.97  v.1.0  Created.
  01.Jun.05  v.1.1  Bug correction: switch is made on the "unsigned short" value
					(v.1.0: "unsigned" only). <Cyril Guillaum

⌨️ 快捷键说明

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