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

📄 common.c

📁 MPEG 2的音频编码软件。喜欢多媒体的开发人员可以看看。
💻 C
📖 第 1 页 / 共 5 页
字号:
			case 0x31:
			case 0x32:
			case 0x33:
			case 0x34:
			case 0x35:
			case 0x36:
			case 0x37:
			case 0x38:
			case 0x39:
			case 0x41:
			case 0x42:
			case 0x43:
			case 0x44:
			case 0x45:
			case 0x46:
			index++;
			break;
			default: break;
		}
	   }

           if (val[0] <= 0x39) bs->buf[i] = (val[0] - 0x30) << 4;
                 else  bs->buf[i] = (val[0] - 0x37) << 4;
           if (val[1] <= 0x39) bs->buf[i--] |= (val[1] - 0x30);
                 else  bs->buf[i--] |= (val[1] - 0x37);
	   index = 0;
      }

      if (!n) {
         bs->eob= i+1;
      }

    }
}

static char *he = "0123456789ABCDEF";

/* empty the buffer to the output device when the buffer becomes full */
void empty_buffer(Bit_stream_struc *bs, int minimum)
                        /* bit stream structure */
                        /* end of the buffer to empty */
{
   register int i;

#if BS_FORMAT == BINARY
   for (i=bs->buf_size-1;i>=minimum;i--)
      fwrite(&bs->buf[i], sizeof(unsigned char), 1, bs->pt);
#else
   for (i=bs->buf_size-1;i>=minimum;i--) {
       char val[2];
       val[0] = he[((bs->buf[i] >> 4) & 0x0F)];
       val[1] = he[(bs->buf[i] & 0x0F)];
       fwrite(val, sizeof(char), 2, bs->pt);
   }
#endif

   for (i=minimum-1; i>=0; i--)
       bs->buf[bs->buf_size - minimum + i] = bs->buf[i];

   bs->buf_byte_idx = bs->buf_size -1 - minimum;
   bs->buf_bit_idx = 8;
}

/* open the device to write the bit stream into it */
void open_bit_stream_w(Bit_stream_struc *bs, char *bs_filenam, int size)
                        /* bit stream structure */
                        /* name of the bit stream file */
                        /* size of the buffer */
{
   if ((bs->pt = fopen(bs_filenam, "w+")) == NULL) {
      printf("Could not create \"%s\".\n", bs_filenam);
      exit(0);
   }
   alloc_buffer(bs, size);
   bs->buf_byte_idx = size-1;
   bs->buf_bit_idx=8;
   bs->totbit=0;
   bs->mode = WRITE_MODE;
   bs->eob = FALSE;
   bs->eobs = FALSE;
}

/* open the device to read the bit stream from it */
void open_bit_stream_r(Bit_stream_struc *bs, char *bs_filenam, int size)
                        /* bit stream structure */
                        /* name of the bit stream file */
                        /* size of the buffer */
{
   register unsigned long n;
   register int i=0;
   register unsigned char flag = 1;
   unsigned char val;

   if ((bs->pt = fopen(bs_filenam, "rb")) == NULL) {
      printf("Could not find \"%s\".\n", bs_filenam);
      exit(0);
   }

   do {
     n = fread(&val, sizeof(unsigned char), 1, bs->pt);
     switch (val) {
      case 0x30:
      case 0x31:
      case 0x32:
      case 0x33:
      case 0x34:
      case 0x35:
      case 0x36:
      case 0x37:
      case 0x38:
      case 0x39:
      case 0x41:
      case 0x42:
      case 0x43:
      case 0x44:
      case 0x45:
      case 0x46:
      case 0xa:  /* \n */
          break;

      default: /* detection of an binary character */
          flag--;
          i = 300;
          break;
     }

   } while (flag & n);

   if (flag) {
      if (verbosity >= 2) printf ("the bit stream file %s is an ASCII file\n", bs_filenam);
      bs->format = ASCII;
   }
   else {
      bs->format = BINARY;
      if (verbosity >= 2) printf ("the bit stream file %s is a BINARY file\n", bs_filenam);
   }

   fclose(bs->pt);

   if ((bs->pt = fopen(bs_filenam, "rb")) == NULL) {
      printf("Could not find \"%s\".\n", bs_filenam);
      exit(0);
   }

   alloc_buffer(bs, size);
   bs->buf_byte_idx=0;
   bs->buf_bit_idx=0;
   bs->totbit=0;
   bs->mode = READ_MODE;
   bs->eob = FALSE;
   bs->eobs = FALSE;
}

/* close the device containing the bit stream after a read process */
void close_bit_stream_r (Bit_stream_struc *bs)
{
    fclose (bs->pt);
    desalloc_buffer (bs);
}

/*close the device containing the bit stream after a write process*/
void close_bit_stream_w (Bit_stream_struc *bs)
{
    empty_buffer (bs, bs->buf_byte_idx + 1);
    fclose (bs->pt);
    desalloc_buffer (bs);
}

/* open and initialize the buffer; */
void alloc_buffer (Bit_stream_struc *bs, int size)
{
    bs->buf = (unsigned char *) mem_alloc (size * sizeof (unsigned char), "buffer");
    bs->buf_size = size;
}

/* empty and close the buffer */
void desalloc_buffer (Bit_stream_struc *bs)
{
   free (bs->buf);
}

int putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff};
int mask[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};

/*read 1 bit from the bit stream */
unsigned int get1bit(Bit_stream_struc *bs)
                        /* bit stream structure */
{
   unsigned int bit;
   register int i;

   bs->totbit++;

   if (!bs->buf_bit_idx) {
        bs->buf_bit_idx = 8;
        bs->buf_byte_idx--;
        if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
             if (bs->eob)
                bs->eobs = TRUE;
             else {
                for (i=bs->buf_byte_idx; i>=0;i--)
                  bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
                refill_buffer(bs);
                bs->buf_byte_idx = bs->buf_size-1;
             }
        }
   }
   bit = bs->buf[bs->buf_byte_idx]&mask[bs->buf_bit_idx-1];
   bit = bit >> (bs->buf_bit_idx-1);
   bs->buf_bit_idx--;
#ifdef	PrintBitDebug
   printf ("pos: %5d getbits: %2d code: %4x val: %5d\n",
		bs->totbit-1,  1, bit, bit);
   fflush (stdout);
#endif
   return(bit);
}

/*write 1 bit from the bit stream */
void put1bit(Bit_stream_struc *bs, int bit)
                        /* bit stream structure */
                        /* bit to write into the buffer */
{
   register int i;

#ifdef	PrintBitDebug
   printf ("pos: %5d putbits: %2d code: %4x val: %5d\n",
		bs->totbit,  1, bit, bit);
   fflush (stdout);
#endif

   bs->totbit++;

   bs->buf[bs->buf_byte_idx] |= (bit&0x1) << (bs->buf_bit_idx-1);
   bs->buf_bit_idx--;
   if (!bs->buf_bit_idx) {
       bs->buf_bit_idx = 8;
       bs->buf_byte_idx--;
       if (bs->buf_byte_idx < 0)
          empty_buffer(bs, MINIMUM);
       bs->buf[bs->buf_byte_idx] = 0;
   }
}

/*read N bit from the bit stream */
unsigned long getbits(Bit_stream_struc *bs, int N)
                        /* bit stream structure */
                        /* number of bits to read from the bit stream */
{
 unsigned long val=0;
 register int i;
 register int j = N;
 register int k, tmp;

 if (N > MAX_LENGTH)
    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);

 bs->totbit += N;
 while (j > 0) {
   if (!bs->buf_bit_idx) {
        bs->buf_bit_idx = 8;
        bs->buf_byte_idx--;
        if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) {
             if (bs->eob)
                bs->eobs = TRUE;
             else {
                for (i=bs->buf_byte_idx; i>=0;i--)
                   bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i];
                refill_buffer(bs);
                bs->buf_byte_idx = bs->buf_size-1;
             }
        }
   }
   k = MIN (j, bs->buf_bit_idx);
   tmp = bs->buf[bs->buf_byte_idx]&putmask[bs->buf_bit_idx];
   tmp = tmp >> (bs->buf_bit_idx-k);
   val |= tmp << (j-k);
   bs->buf_bit_idx -= k;
   j -= k;
 }
#ifdef	PrintBitDebug
 printf ("pos: %5d getbits: %2d code: %4x val: %5d\n", 
		 bs->totbit - N,  N, val, val);
 fflush (stdout);
#endif
 return(val);
}

/*write N bits into the bit stream */
void putbits(Bit_stream_struc *bs, unsigned int val, int N)
                        /* bit stream structure */
                        /* val to write into the buffer */
                        /* number of bits of val */
{
 register int i;
 register int j = N;
 register int k, tmp;

 if (N > MAX_LENGTH)
    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH);

#ifdef	PrintBitDebug
 printf ("pos: %5d putbits: %2d code: %4x val: %5d\n", 
		 bs->totbit,  N, val, val);
 fflush (stdout);
#endif

 bs->totbit += N;
 while (j > 0) {
   k = MIN(j, bs->buf_bit_idx);
   tmp = val >> (j-k);
   bs->buf[bs->buf_byte_idx] |= (tmp&putmask[k]) << (bs->buf_bit_idx-k);
   bs->buf_bit_idx -= k;
   if (!bs->buf_bit_idx) {
       bs->buf_bit_idx = 8;
       bs->buf_byte_idx--;
       if (bs->buf_byte_idx < 0)
          empty_buffer(bs, MINIMUM);
       bs->buf[bs->buf_byte_idx] = 0;
   }
   j -= k;
 }
}

/*return the current bit stream length (in bits)*/
unsigned long sstell(Bit_stream_struc *bs)
                        /* bit stream structure */
{
  return(bs->totbit);
}

/*return the status of the bit stream*/
/* returns 1 if end of bit stream was reached */
/* returns 0 if end of bit stream was not reached */
int end_bs(Bit_stream_struc *bs)
                        /* bit stream structure */
{
  return(bs->eobs);
}

/*****************************************************************************
*
*  End of bit_stream.c package
*
*****************************************************************************/

int transmission_channel (frame_params *fr_ps, int sbgr, int m)
{
    int config = fr_ps->config;
    int tca = fr_ps->header->tc_alloc[sbgr];
#ifdef Augmentation_7ch
    int aug_tca = fr_ps->header->aug_tc_alloc[sbgr];
#endif

    /* 960627 FdB TCA table dependent on configuration */
    if (config == 320)
#ifdef Augmentation_7ch
	if (m >= 5)
	    return (transmission_channel7[aug_tca][m-5]);		/* 5/2 */
	else
#endif
	    return (transmission_channel5[tca][m]);		/* 3/2 */
    else if (config == 310)
	return (transmission_channel4a[tca][m]);	/* 3/1 */
    else if (config == 220)
	return (transmission_channel4b[tca][m]);	/* 2/2 */
    else if (config == 300 || config == 302 || config == 210)
	return (transmission_channel3[tca][m]);		/* 3/0 (+2/0) and 2/1 */
    else
	return (m);
}

int dyn_ch (frame_params *fr_ps, int sbgr, int m)
{
    int config = fr_ps->config;
    int dynx = fr_ps->header->dyn_cross[sbgr];
    int dynx2stereo = fr_ps->header->dyn_second_stereo[sbgr];
#ifdef Augmentation_7ch
    int aug_dynx = fr_ps->header->aug_dyn_cross[sbgr];
#endif
    /* 960627 FdB Dyn_ch table dependent on configuration */
    if (config == 320)
#ifdef Augmentation_7ch
	if (m >= 5)
	    return (dyn_ch5[aug_dynx][m-5]);
	else
#endif
	    return (dyn_ch4[dynx][m-2]);
    else if (config == 310 || config == 220)
	return (dyn_ch3[dynx][m-2]);
    else if (config == 300 || config == 302 || config == 210)
    {
	if (config == 302 && dynx2stereo && m == 4)
	    return (0);
	else
	    return (dyn_ch1[dynx][m-2]);
    }
    else if (config == 202 && dynx2stereo && m == 3)
	return (0);
    else if (config == 102 && dynx2stereo && m == 2)
	return (0);
    else

⌨️ 快捷键说明

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