www.pudn.com > av3dec_20050318.zip > sam_bitstream.c


/* 
*********************************************************************** 
* COPYRIGHT AND WARRANTY INFORMATION 
* 
* Copyright 2004,  Audio Video Coding Standard, Part III 
* 
* This software module was originally developed by 
* 
* JungHoe Kim (kjh94@samsung.com), Samsung AIT 
* 
* edited by 
* 
* Lei Miao (win.miaolei@samsung.com), Samsung AIT 
* 
* DISCLAIMER OF WARRANTY 
* 
* These software programs are available to the users without any 
* license fee or royalty on an "as is" basis. The AVS disclaims 
* any and all warranties, whether express, implied, or statutory, 
* including any implied warranties of merchantability or of fitness 
* for a particular purpose. In no event shall the contributors or  
* the AVS be liable for any incidental, punitive, or consequential 
* damages of any kind whatsoever arising from the use of this program. 
* 
* This disclaimer of warranty extends to the user of this program 
* and user's customers, employees, agents, transferees, successors, 
* and assigns. 
* 
* The AVS does not represent or warrant that the program furnished 
* hereunder are free of infringement of any third-party patents. 
* Commercial implementations of AVS, including shareware, may be 
* subject to royalty fees to patent holders. Information regarding 
* the AVS patent policy is available from the AVS Web site at 
* http://www.avs.org.cn 
* 
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY. 
************************************************************************ 
*/ 
 
#include  
#include  
#include  
#include  
 
#include "sam_cbc_dec.h" 
#include "funcs.h" 
 
#define  MINIMUM    3 
#define  MAX_LENGTH  3072		 
#define  ALIGNING  8 
 
static FILE		*bs_fp; 
static unsigned char bs_buf[4096];  /* bit stream buffer */ 
static int  bs_buf_size;            /* size of buffer (in number of bytes) */ 
static long  bs_totbit;             /* bit counter of bit stream */ 
static int  bs_buf_byte_idx;        /* pointer to top byte in buffer */ 
static int  bs_buf_bit_idx;         /* pointer to top bit of top byte in buffer */ 
static int  bs_eob;                 /* end of buffer index */ 
static int  bs_eobs;                /* end of bit stream flag */ 
static long	ReadBitsPerFrame; 
 
void	refill_buffer(); 
 
/* refill the buffer from the input device when the buffer becomes empty    */ 
void	refill_buffer() 
{ 
	register	int i=bs_buf_size-bs_buf_byte_idx; 
	register	int n=1; 
 
   bs_buf_byte_idx = 0; 
   if (!bs_eob) { 
         n = fread(&bs_buf[i], 1, bs_buf_size-i, bs_fp); 
	 if(n != (bs_buf_size-i)) 
		bs_eob = n+i; 
   } 
} 
 
/* open the device to read the bit stream from it */ 
void open_bitstream(char *bs_filenam) 
{ 
	bs_fp = fopen(bs_filenam, "rb"); 
	if (bs_fp == NULL) { 
		printf("Could not find input bs file %s.\n", bs_filenam); 
		exit(1); 
	} 
	 
  bs_buf_byte_idx=4095; 
  bs_buf_bit_idx=0; 
  bs_totbit=0; 
  ReadBitsPerFrame = 0; 
  bs_eob = FALSE; 
  bs_eobs = FALSE; 
  bs_buf_size = 4096; 
} 
 
/*close the device containing the bit stream after a read process*/ 
void close_bitstream() 
{ 
	fclose(bs_fp); 
} 
 
static int i_putmask[9]={0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff}; 
 
/*read N bit from the bit stream */ 
unsigned int getbits(int N) 
{ 
  unsigned long val=0; 
  register int i; 
  register int j = N; 
  register int k, tmp; 
 
  if(N <= 0) return 0; 
 
  if (N > MAX_LENGTH) 
    printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); 
 
  bs_totbit += N; 
  ReadBitsPerFrame += N; 
  while (j > 0) { 
    if (!bs_buf_bit_idx) { 
      bs_buf_bit_idx = 8; 
      bs_buf_byte_idx++; 
      if ((bs_buf_byte_idx >= (bs_buf_size - MINIMUM)) || (bs_buf_byte_idx == bs_eob)) { 
        if (bs_eob) { 
          bs_eobs = TRUE; 
          return 0; 
        } 
		else { 
          for (i=bs_buf_byte_idx; i> (bs_buf_bit_idx-k)) << (j-k); 
    bs_buf_bit_idx -= k; 
    j -= k; 
  } 
  return(val); 
} 
 
 
/*return the current bit stream length (in bits)*/ 
unsigned long i_sstell() 
{ 
  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() 
{ 
	ReadBitsPerFrame = 0; 
	if (bs_eob && bs_buf_byte_idx+1 >= bs_eob) 
		return TRUE; 
	else 
		return FALSE; 
}