www.pudn.com > mpeg4_DECORE.rar > GETBITS.H


/************************************************************************** 
 *                                                                        * 
 * This code has been developed by Eugene Kuznetsov. This software is an  * 
 * implementation of a part of one or more MPEG-4 Video tools as          * 
 * specified in ISO/IEC 14496-2 standard.  Those intending to use this    * 
 * software module in hardware or software products are advised that its  * 
 * use may infringe existing patents or copyrights, and any such use      * 
 * would be at such party's own risk.  The original developer of this     * 
 * software module and his/her company, and subsequent editors and their  * 
 * companies (including Project Mayo), will have no liability for use of  * 
 * this software or modifications or derivatives thereof.                 * 
 *                                                                        * 
 * Project Mayo gives users of the Codec a license to this software       * 
 * module or modifications thereof for use in hardware or software        * 
 * products claiming conformance to the MPEG-4 Video Standard as          * 
 * described in the Open DivX license.                                    * 
 *                                                                        * 
 * The complete Open DivX license can be found at                         * 
 * http://www.projectmayo.com/opendivx/license.php                        * 
 *                                                                        * 
 **************************************************************************/ 
/** 
*  Copyright (C) 2001 - Project Mayo 
 * 
 * Eugene Kuznetsov 
 * 
 * DivX Advanced Research Center  
* 
**/ 
 
#ifndef _DECORE_GETBITS_H 
#define _DECORE_GETBITS_H 
 
void initbits (unsigned char * stream, int length); 
void fillbfr (void); 
 
/***/ 
 
#if defined(LINUX) 
// 486+ specific instruction 
// anybody want to use decore on 386? 
#define _SWAP(a,b) b=*(int*)a; \ 
	__asm__ ( "bswapl %0\n" : "=g" (b) : "0" (b) ) 
#elif defined(WIN32) 
#define _SWAP(a,b) \ 
	b=*(int*)a; __asm mov eax,b __asm bswap eax __asm mov b, eax 
#else 
#define _SWAP(a,b) (b=((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3])) 
#endif 
 
#ifdef WIN32 
#include  
#endif 
 
/***/ 
 
#ifndef _DECORE 
 
static unsigned int msk[33] = 
{ 
  0x00000000, 0x00000001, 0x00000003, 0x00000007, 
  0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 
  0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 
  0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 
  0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff, 
  0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff, 
  0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff, 
  0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, 
  0xffffffff 
}; 
 
static void fillbfr () 
{ 
  int l; 
 
  ld->inbfr[0] = ld->inbfr[8]; 
  ld->inbfr[1] = ld->inbfr[9]; 
  ld->inbfr[2] = ld->inbfr[10]; 
  ld->inbfr[3] = ld->inbfr[11]; 
 
  if (ld->rdptr >= ld->rdbfr + 2048) 
  { 
    l = read (ld->infile, ld->rdbfr, 2048); 
    ld->rdptr = ld->rdbfr; 
    if (l < 2048) 
    { 
      if (l < 0) 
        l = 0; 
 
      while (l < 2048)          /* Add recognizable sequence end code */ 
      { 
        ld->rdbfr[l++] = 0; 
        ld->rdbfr[l++] = 0; 
        ld->rdbfr[l++] = (1 << 7) | (31 << 2); 
      } 
    } 
  } 
  for (l = 0; l < 8; l++) 
    ld->inbfr[l + 4] = ld->rdptr[l]; 
 
  ld->rdptr += 8; 
  ld->incnt += 64; 
} 
 
 
/* return next n bits (right adjusted) without advancing */ 
 
static unsigned int showbits (int n) 
{ 
  unsigned char *v; 
  unsigned int b; 
  int c; 
 
  if (ld->incnt < n) 
    fillbfr (); 
 
  v = ld->inbfr + ((96 - ld->incnt) >> 3); 
  b = (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | v[3]; 
  c = ((ld->incnt - 1) & 7) + 25; 
  return (b >> (c - n)) & msk[n]; 
} 
 
/* advance by n bits */ 
 
static void flushbits (int n) 
{ 
  ld->bitcnt += n; 
  ld->incnt -= n; 
 
  if (ld->incnt < 0) 
    fillbfr (); 
} 
 
#else 
 
static __inline unsigned int showbits (int n) 
{ 
	int rbit = 32 - ld->bitcnt; 
	unsigned int b; 
	_SWAP(ld->rdptr, b); 
//	return ((b & msk[rbit]) >> (rbit-n)); 
// Is it platform independent??? 
 
	return (b & (0xFFFFFFFFU >> (ld->bitcnt))) >> (rbit-n); 
} 
 
static __inline void flushbits (int n) 
{ 
	ld->bitcnt += n; 
	if (ld->bitcnt >= 8) { 
		ld->rdptr += ld->bitcnt / 8; 
		ld->bitcnt = ld->bitcnt % 8; 
	} 
} 
 
#endif // !_DECORE 
 
static __inline unsigned int getbits (int n) 
{ 
  unsigned int l; 
 
  l = showbits (n); 
  flushbits (n); 
 
  return l; 
} 
 
unsigned int getbits1 (void); 
 
#endif /* _DECORE_GETBITS_H */