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

📄 bitstream.c

📁 MPEG2/MPEG4编解码参考程序(实现了MPEG4的部分功能)
💻 C
📖 第 1 页 / 共 3 页
字号:
  oldCurrentBit = stream->currentBit;  result = BsGetBitChar(stream,data,numBit);  stream->currentBit = oldCurrentBit;  if (result)    CommonWarning("BsGetBitAheadChar: error reading bit stream");  return result;}/* BsGetBitAheadShort() *//* Read bits from bit stream (short). *//* (Current position in bit stream is NOT advanced !!!) */int BsGetBitAheadShort (  BsBitStream *stream,		/* in: bit stream */  unsigned short *data,		/* out: bits read */				/*      (may be NULL if numBit==0) */  int numBit)			/* in: num bits to read */				/*     [0..16] */				/* returns: */				/*  0=OK  1=error */{  long oldCurrentBit;  int result;  if (BSdebugLevel >= 3)    printf("BsGetBitAheadShort: %s  id=%ld  numBit=%d\n",	   (stream->file!=NULL)?"file":"buffer",stream->streamId,numBit);  oldCurrentBit = stream->currentBit;  result = BsGetBitShort(stream,data,numBit);  stream->currentBit = oldCurrentBit;  if (result)    CommonWarning("BsGetBitAheadShort: error reading bit stream");  return result;}/* BsGetBitAheadInt() *//* Read bits from bit stream (int). *//* (Current position in bit stream is NOT advanced !!!) */int BsGetBitAheadInt (  BsBitStream *stream,		/* in: bit stream */  unsigned int *data,		/* out: bits read */				/*      (may be NULL if numBit==0) */  int numBit)			/* in: num bits to read */				/*     [0..16] */				/* returns: */				/*  0=OK  1=error */{  long oldCurrentBit;  int result;  if (BSdebugLevel >= 3)    printf("BsGetBitAheadInt: %s  id=%ld  numBit=%d\n",	   (stream->file!=NULL)?"file":"buffer",stream->streamId,numBit);  oldCurrentBit = stream->currentBit;  result = BsGetBitInt(stream,data,numBit);  stream->currentBit = oldCurrentBit;  if (result)    CommonWarning("BsGetBitAheadInt: error reading bit stream");  return result;}long BsGetBufferFullness (BsBitBuffer *buffer){  return (buffer->numBit);}/* BsGetBuffer() *//* Read bits from bit stream to buffer. *//* (Current position in bit stream is advanced !!!) *//* (Buffer is cleared first - previous data in buffer is lost !!!) */int BsGetBuffer (  BsBitStream *stream,		/* in: bit stream */  BsBitBuffer *buffer,		/* out: buffer read */				/*      (may be NULL if numBit==0) */  long numBit)			/* in: num bits to read */				/* returns: */				/*  0=OK  1=error */{  long i,numByte,numRemain;  unsigned long data;  if (BSdebugLevel >= 2) {    printf("BsGetBuffer: %s  id=%ld  numBit=%ld  ",	   (stream->file!=NULL)?"file":"buffer",	   stream->streamId,numBit);      if (buffer != NULL)	printf("bufSize=%ld  bufAddr=0x%lx  ",	       buffer->size,(unsigned long)buffer);      else	printf("(bufAddr=(NULL)  ");    printf("curBit=%ld\n",stream->currentBit);  }  if (stream->write != 0)    CommonExit(1,"BsGetBuffer: stream not in read mode");  if (numBit == 0)    return 0;  if (stream->buffer[0] == buffer)    CommonExit(1,"BsGetBuffer: can not get buffer from itself");  if (numBit < 0 || numBit > buffer->size)    CommonExit(1,"BsGetBuffer: number of bits out of range (%ld)",numBit);  BsClearBuffer(buffer);  numByte = bit2byte(numBit)-1;  for (i=0; i<numByte; i++) {    if (BsGetBit(stream,&data,BYTE_NUMBIT)) {      if ( ! BSaacEOF || BSdebugLevel > 0  )        CommonWarning("BsGetBuffer: error reading bit stream");      buffer->numBit = i*BYTE_NUMBIT;      return 1;    }    buffer->data[i] = data;  }  numRemain = numBit-numByte*BYTE_NUMBIT;  if (BsGetBit(stream,&data,numRemain)) {    if ( ! BSaacEOF || BSdebugLevel > 0  )      CommonWarning("BsGetBuffer: error reading bit stream");    buffer->numBit = numByte*BYTE_NUMBIT;    return 1;  }  buffer->data[i] = data<<(BYTE_NUMBIT-numRemain);  buffer->numBit = numBit;  return 0;}/* BsGetBufferAppend() *//* Append bits from bit stream to buffer. *//* (Current position in bit stream is advanced !!!) */int BsGetBufferAppend (  BsBitStream *stream,		/* in: bit stream */  BsBitBuffer *buffer,		/* out: buffer read */				/*      (may be NULL if numBit==0) */  int append,			/* in: if != 0: append bits to buffer */				/*              (don't clear buffer) */  long numBit)			/* in: num bits to read */				/* returns: */				/*  0=OK  1=error */{  long i,numByte,last_byte,numRemain;  unsigned long data;  int tmp,shift_cnt,eof;  if (BSdebugLevel >= 2) {    printf("BsGetBufferAppend: %s  id=%ld  numBit=%ld  ",	   (stream->file!=NULL)?"file":"buffer",	   stream->streamId,numBit);    if (buffer != NULL)      printf("bufSize=%ld  bufAddr=0x%lx  ",	     buffer->size,(unsigned long)buffer);    else      printf("(bufAddr=(NULL)  ");    printf("curBit=%ld\n",stream->currentBit);  }  if (stream->write != 0)    CommonExit(1,"BsGetBufferAppend: stream not in read mode");  if (stream->buffer[0] == buffer)    CommonExit(1,"BsGetBufferAppend: cannot get buffer from itself");  if (numBit < 0)    CommonExit(1,"BsGetBufferAppend: number of bits out of range (%ld)",	       numBit);#if 1 	/* AI 990528 */      /* check whether number of bits to be read exceeds the end of bitstream */  eof = BsEof(stream, numBit);/*  if (BsEof(stream, numBit-1)) { */  if (eof) {    numBit = BYTE_NUMBIT*stream->numByte - stream->currentBit;    if (BSdebugLevel >= 2) {      printf("*** numBits(modified)=%ld\n", numBit);    }  }#endif  if (append) {    /* append to buffer (don't clear buffer) */    if ((numBit+buffer->numBit) > buffer->size)      CommonExit(1,"BsGetBufferAppend: number of bits out of range (%ld)",		 numBit);    /* fill up the last possible incomplete byte */    tmp = 8 - buffer->numBit%8;    if (tmp == 8)      tmp = 0;    if (tmp <= numBit) {      shift_cnt = 0;    }    else {      shift_cnt = tmp - numBit;      tmp = numBit;    }    if (tmp) {      if (BsGetBit(stream,&data,tmp)) {	CommonWarning("BsGetBufferAppend: error reading bit stream");	return 1;      }      data <<= shift_cnt;      numBit -= tmp;      last_byte = buffer->numBit/8;      data |= buffer->data[last_byte];      buffer->numBit += tmp;      buffer->data[last_byte] = data;      last_byte++;    }    else      last_byte = buffer->numBit/8;  }  else { /* if (append) */    /* clear buffer */    if (numBit > buffer->size)      CommonExit(1,"BsGetBufferAppend: number of bits out of range (%ld)",                 numBit);    BsClearBuffer(buffer);    last_byte = 0;  }  if (numBit > 0) {    numByte = bit2byte(numBit)-1;    for (i=last_byte; i<last_byte+numByte; i++) {      if ((tmp = BsGetBit(stream,&data,BYTE_NUMBIT))) {	buffer->numBit += (i-last_byte)*BYTE_NUMBIT;        if (tmp==-1)          return -1;	/* end of file */	CommonWarning("BsGetBufferAppend: error reading bit stream");	return 1;      }      buffer->data[i] = data;    }    numRemain = numBit-numByte*BYTE_NUMBIT;    if (BsGetBit(stream,&data,numRemain)) {      CommonWarning("BsGetBufferAppend: error reading bit stream");      buffer->numBit += numByte*BYTE_NUMBIT;      return 1;    }    buffer->data[i] = data<<(BYTE_NUMBIT-numRemain);    buffer->numBit += numBit;  }#if 1	/* AI 990528 */  if ( eof ) {    /* just reached to the end of bitstream */    if (stream->currentBit == BYTE_NUMBIT*stream->numByte) {      if (BSdebugLevel >= 2) {	printf("*** just reached the end of bitstream\n");      }      return -1;	/* end of file */    }  }#endif/*  } */  return 0;}/* BsGetBufferAhead() *//* Read bits ahead of current position from bit stream to buffer. *//* (Current position in bit stream is NOT advanced !!!) *//* (Buffer is cleared first - previous data in buffer is lost !!!) */int BsGetBufferAhead (  BsBitStream *stream,		/* in: bit stream */  BsBitBuffer *buffer,		/* out: buffer read */				/*      (may be NULL if numBit==0) */  long numBit)			/* in: num bits to read */				/* returns: */				/*  0=OK  1=error */{  long oldCurrentBit;  int result;  if (BSdebugLevel >= 2)    printf("BsGetBufferAhead: %s  id=%ld  numBit=%ld\n",	   (stream->file!=NULL)?"file":"buffer",stream->streamId,numBit);  if (numBit > stream->buffer[0]->size)    CommonExit(1,"BsGetBufferAhead: "	       "number of bits to look ahead too high (%ld)",numBit);  oldCurrentBit = stream->currentBit;  result = BsGetBuffer(stream,buffer,numBit);  stream->currentBit = oldCurrentBit;  if (result)    if ( ! BSaacEOF || BSdebugLevel > 0  )      CommonWarning("BsGetBufferAhead: error reading bit stream");  return result;}/* BsGetSkip() *//* Skip bits in bit stream (read). *//* (Current position in bit stream is advanced !!!) */int BsGetSkip (  BsBitStream *stream,		/* in: bit stream */  long numBit)			/* in: num bits to skip */				/* returns: */				/*  0=OK  1=error */{  if (BSdebugLevel >= 2) {    printf("BsGetSkip: %s  id=%ld  numBit=%ld  ",	   (stream->file!=NULL)?"file":"buffer",	   stream->streamId,numBit);    printf("curBit=%ld\n",stream->currentBit);  }  if (stream->write != 0)    CommonExit(1,"BsGetSkip: stream not in read mode");  if (numBit < 0)    CommonExit(1,"BsGetSkip: number of bits out of range (%ld)",numBit);  if (numBit == 0)    return 0;  if (BsReadAhead(stream,numBit) || BsCheckRead(stream,numBit)) {    CommonWarning("BsGetSkip: error reading bit stream");    return 1;  }  stream->currentBit += numBit;  return 0;}/* BsPutBit() *//* Write bits to bit stream. */int BsPutBit (  BsBitStream *stream,		/* in: bit stream */  unsigned long data,		/* in: bits to write */  int numBit)			/* in: num bits to write */				/*     [0..32] */				/* returns: */				/*  0=OK  1=error */{  int num,maxNum,curNum;  unsigned long bits;  if (BSdebugLevel > 3)    printf("BsPutBit: %s  id=%ld  numBit=%d  data=0x%lx,%ld  curBit=%ld\n",	   (stream->file!=NULL)?"file":"buffer",	   stream->streamId,numBit,data,data,stream->currentBit);  if (stream->write != 1)    CommonExit(1,"BsPutBit: stream not in write mode");  if (numBit<0 || numBit>LONG_NUMBIT)    CommonExit(1,"BsPutBit: number of bits out of range (%d)",numBit);  if (numBit < 32 && data > ((unsigned long)1<<numBit)-1)    CommonExit(1,"BsPutBit: data requires more than %d bits (0x%lx)",	       numBit,data);  if (numBit == 0)    return 0;  /* write bits in packets according to buffer byte boundaries */  num = 0;  maxNum = BYTE_NUMBIT - stream->currentBit % BYTE_NUMBIT;  while (num < numBit) {    curNum = min(numBit-num,maxNum);    bits = data>>(numBit-num-curNum);    if (BsWriteByte(stream,bits,curNum)) {      CommonWarning("BsPutBit: error writing bit stream");      return 1;    }    num += curNum;    maxNum = BYTE_NUMBIT;  }  return 0;}/* BsPutBuffer() *//* Write bits from buffer to bit stream. */int BsPutBuffer (  BsBitStream *stream,		/* in: bit stream */  BsBitBuffer *buffer)		/* in: buffer to write */				/* returns: */				/*  0=OK  1=error */{  long i,numByte,numRemain;  if (buffer->numBit == 0)    return 0;  if (BSdebugLevel >= 2)    printf("BsPutBuffer: %s  id=%ld  numBit=%ld  bufAddr=0x%lx  curBit=%ld\n",	   (stream->file!=NULL)?"file":"buffer",	   stream->streamId,buffer->numBit,(unsigned long)buffer,	   stream->currentBit);  if (stream->write != 1)    CommonExit(1,"BsPutBuffer: stream not in write mode");  if (stream->buffer[0] == buffer)    CommonExit(1,"BsPutBuffer: can not put buffer into itself");  numByte = bit2byte(buffer->numBit)-1;  for (i=0; i<numByte; i++)    if (BsPutBit(stream,buffer->data[i],BYTE_NUMBIT)) {      CommonWarning("BsPutBuffer: error writing bit stream");      return 1;    }  numRemain = buffer->numBit-numByte*BYTE_NUMBIT;  if (BsPutBit(stream,buffer->data[i]>>(BYTE_NUMBIT-numRemain),numRemain)) {    CommonWarning("BsPutBuffer: error reading bit stream");    return 1;  }  return 0;}/* BsAllocBuffer() *//* Allocate bit buffer. */BsBitBuffer *BsAllocBuffer (  long numBit)			/* in: buffer size in bits */				/* returns: */				/*  bit buffer (handle) */{  BsBitBuffer *buffer;  if (BSdebugLevel >= 2)    printf("BsAllocBuffer: size=%ld\n",numBit);  if ((buffer=(BsBitBuffer*)malloc(sizeof(BsBitBuffer))) == NULL)    CommonExit(1,"BsAllocBuffer: memory allocation error (buffer)");  if ((buffer->data=(unsigned char*)malloc(bit2byte(numBit)*sizeof(char)))      == NULL)    CommonExit(1,"BsAllocBuffer: memory allocation error (data)");  buffer->numBit = 0;  buffer->size = numBit;  if (BSdebugLevel >= 2)    printf("BsAllocBuffer: bufAddr=0x%lx\n",(unsigned long)buffer);  return buffer;}/* BsFreeBuffer() *//* Free bit buffer. */void BsFreeBuffer (  BsBitBuffer *buffer)		/* in: bit buffer */{  if (BSdebugLevel >= 2)    printf("BsFreeBuffer: size=%ld  bufAddr=0x%lx\n",	   buffer->size,(unsigned long)buffer);  free(buffer->data);  free(buffer);}/* BsBufferNumBit() *//* Get number of bits in buffer. */long BsBufferNumBit (  BsBitBuffer *buffer)		/* in: bit buffer */				/* returns: */				/*  number of bits in buffer */{  if (BSdebugLevel >= 2)    printf("BsBufferNumBit: numBit=%ld  size=%ld  bufAddr=0x%lx\n",	   buffer->numBit,buffer->size,(unsigned long)buffer);  return buffer->numBit ;}/* BsClearBuffer() *//* Clear bit buffer (set numBit to 0). */void BsClearBuffer (  BsBitBuffer *buffer)		/* in: bit buffer */{  if (BSdebugLevel >= 2)    printf("BsClearBuffer: size=%ld  bufAddr=0x%lx\n",	   buffer->size,(unsigned long)buffer);  if (buffer->numBit != 0 ) {    #if 0 /* removed due to BUG! in mp4dec.c */    CommonWarning("BsClearBuffer: Buffer not empty!");    #endif /* removed due to BUG! in mp4dec.c */  }  buffer->numBit = 0;}/* end of bitstream.c */

⌨️ 快捷键说明

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