www.pudn.com > henclib263.rar > bitstream.cxx


/* 
* bitstream.cxx 
* 
* for the functions which deal with bitstream operation. 
* 
* Copyright (c) 2002-2004 Li Chun-lin(li_chunlin@263.net) 
* 
* This program is free software; you can redistribute it and/or 
* modify it under the terms of the GNU General Public License 
* as published by the Free Software Foundation; either version 2 
* of the License, or (at your option) any later version. 
*  
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
* GNU General Public License for more details. 
 
* You should have received a copy of the GNU General Public License 
* along with this program; if not, write to the Free Software 
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
*/ 
#include "../include/HEnc.h" 
 
#ifdef  __cplusplus 
extern "C" { 
#endif 
 
extern putstrm putstrmctrl; 
 
 
/*! 
* putbits:       function of sending bits to the stream buffer 
* note:          How does the function work is decided by the structure of putstrm. 
*                the structure provide a buffer to store coded bits and a method 
*                to process those bits when the buffer is full. 
* optimization:  no optimization 
*/ 
void putbits(int val, int n) 
{ 
  int i; 
  unsigned int mask; 
 
 
  mask = 1 << (n-1); //!< selects first (leftmost) bit  
 
  for (i=0; i>= 1; //!< select next bit  
    putstrmctrl.pcnt--; 
 
    if (putstrmctrl.pcnt==0) //< 8 bit buffer full  
	{ 
      putstrmctrl.pout[putstrmctrl.ptr] = putstrmctrl.bitout; 
//	  fputc (putstrmctrl.bitout, putstrmctrl.bitstrmfptr); 
      putstrmctrl.pcnt = 8; 
	  putstrmctrl.ptr++; 
	  if (putstrmctrl.ptr >= putstrmctrl.buff_size) 
	  { 
		  (putstrmctrl.WriteData)(putstrmctrl.pout, putstrmctrl.ptr, putstrmctrl.timestamp, 
			                    putstrmctrl.pic_type, putstrmctrl.context); 
		  putstrmctrl.ptr = 0; 
	  } 
 
    } 
  } 
} 
 
/* 
*  alignbits: called when one frame is completely encoded, to make the last bit of the  
*             stream the last bit of a byte 
*/ 
 
int alignbits(void) 
{ 
  int ret_value; 
   
  if (putstrmctrl.pcnt!=8) 
  { 
    ret_value = putstrmctrl.pcnt;	 
    putbits (0, ret_value); 
    return ret_value; 
  } 
  else 
    return 0; 
} 
 
/* 
*  clear_buff: called when one frame is completely encoded, to process the data in the stream 
*              buffer through a callback function, no matter the buffer is full or not.  
*/ 
void clear_buff(void) 
{ 
  (putstrmctrl.WriteData)(putstrmctrl.pout, putstrmctrl.ptr, putstrmctrl.timestamp, 
			              putstrmctrl.pic_type, putstrmctrl.context); 
} 
 
#ifdef  __cplusplus 
} 
#endif