www.pudn.com > mediator15src.zip > getbits.c


/* 
 * getbits.c 
 * Copyright (C) 2001-2002 Arno Hornberger  
 * 
 * This file is part of MPEG Mediator, a free MPEG stream converter. 
 * 
 * MPEG Mediator 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. 
 * 
 * MPEG Mediator 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 "mpg123.h" 
 
static int mpg123_bitsleft; 
static unsigned int mpg123_buffer; 
static unsigned char *mpg123_bufferstart; 
 
void init_getbits(unsigned char *framedata) 
{ 
  mpg123_bufferstart = framedata; 
  mpg123_buffer = 0; 
  mpg123_bitsleft = 0; 
  getbits(0); 
} 
 
unsigned int getbits(int num_bits) 
{ 
  unsigned int result; 
   
  result = mpg123_buffer >> (32 - num_bits); 
  mpg123_buffer <<= num_bits; 
  mpg123_bitsleft -= num_bits; 
  while (mpg123_bitsleft <= 24) { 
    mpg123_buffer |= *(mpg123_bufferstart++) << (24 - mpg123_bitsleft); 
    mpg123_bitsleft += 8; 
  } 
 
  return result; 
}