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


/* 
* ratectrl.cxx 
* 
* implementation for Rate Control funtions. 
* 
* 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 "../include/HEnc.h" 
 
 
#ifdef  __cplusplus 
extern "C" { 
#endif 
 
 
int ratectrl(int target_bitrate, int frame_rate, int frame_bits[], int *Quant) 
{ 
	int i,j,k; 
	int totalframebits=0; 
	int leftbits=0; 
	int leftbitsneed=0; 
	int differ=0; 
	int frame_skip = 0; 
	int bpf=target_bitrate/frame_rate; 
 
	if (target_bitrate == 0) 
	{ 
		return 0; 
	} 
 
	printf("\nQI %d\n",*Quant); 
//  printf("\nframe_rate,%d\n",frame_rate);	 
	for(i=0;i0) 
			j=i; 
	} 
//	printf("\nthe last frame_bit:%d\n",j); 
	for(k=0;k<=j;k++) 
	{ 
		totalframebits+=frame_bits[k]; 
	} 
//	printf("\ntotalframebits,%d\n",totalframebits); 
 
 
	leftbits=target_bitrate-totalframebits;//客观上允许这么多比特,是上限 
//	printf("\nleftbits:%d\n",leftbits); 
	leftbitsneed=target_bitrate*(frame_rate-j-1)/frame_rate;// 
//	printf("\nleftbitsneed:%d\n",leftbitsneed); 
//	printf("\nbpf:%d\n",target_bitrate/frame_rate);	 
	differ=leftbitsneed-leftbits; 
//	printf("\ndiffer:%d\n",differ); 
	if(differ<=0) 
	{ 
		(*Quant)--; 
	} 
	else if(differ<=bpf) 
	{ 
		(*Quant)++; 
	} 
	else 
	{ 
		(*Quant)++; 
		frame_skip=differ/bpf; 
	} 
 
	*Quant = mmin(30,mmax(1, *Quant)); 
	 
	return frame_skip; 
 
} 
 
#ifdef  __cplusplus 
} 
#endif