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


/* 
* stat.cxx 
* 
* implementation for data statics. 
* 
* 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/stat.h" 
#include  
#include  
 
 
#ifdef  __cplusplus 
extern "C" { 
#endif 
 
#ifndef WIN32 
#define __int64 long long int 
#endif 
 
double computeSNRY(H263VencStatus *encoder) 
{ 
	double dRtn; 
	int i, j, k; 
	int lx = encoder->mv_outside_frame ? encoder->pels + 32 : encoder->pels; 
	int iTotal = encoder->pels * encoder->lines; 
	unsigned char *p1 = encoder->frame_buf[encoder->zero_index].pLum; 
	unsigned char *p2 = encoder->frameToEncode.pLum;	 
	// Attention!! The size of the following 2 variables depends on compiler 
	__int64 iDiff; 
	__int64 iSquare; 
 
	if (encoder->PTYPE == B_IMG) 
	{ 
		p1 = encoder->BPicture[0].pLum; 
		p2 = encoder->BPicture[encoder->B_count].pLum; 
		lx = encoder->pels; 
	} 
 
	iSquare = 0; 
	 
	for (j = 0, k = 0; j < encoder->lines; j++, k+=lx) 
	{ 
		for (i = 0; i < encoder->pels; i++) 
		{ 
			iDiff = *p2++ - p1[k+i]; 
			iSquare += iDiff * iDiff; 
		} 
	} 
	dRtn = (double)iSquare/(double)iTotal; 
	if(dRtn != 0) 
	{ 
		dRtn = (double)(255 * 255)/dRtn; 
		dRtn = 10 * (double)log10(dRtn); 
	} 
	else 
		dRtn = (float)99.99; 
	encoder->frmsnr.snrY  = dRtn; 
	return dRtn; 
} 
 
double computeSNRCB(H263VencStatus *encoder) 
{ 
	double dRtn; 
	int i, j, k; 
	int iTotal = encoder->lines * encoder->pels / 4; 
	int lx = encoder->mv_outside_frame ? encoder->pels/2 + 16 : encoder->pels/2; 
	unsigned char *p1 = encoder->frame_buf[encoder->zero_index].pCb; 
	unsigned char *p2 = encoder->frameToEncode.pCb;	 
	// Attention!! The size of the following 2 variables depends on compiler 
	__int64 iDiff; 
	__int64 iSquare; 
 
	if (encoder->PTYPE == B_IMG) 
	{ 
		p1 = encoder->BPicture[0].pCb; 
		p2 = encoder->BPicture[encoder->B_count].pCb; 
		lx = encoder->pels/2; 
	} 
	iSquare = 0; 
	 
	for(j = 0, k = 0; j < encoder->lines/2; j++, k += lx) 
	{ 
		for (i = 0; i < encoder->pels/2; i ++) 
		{ 
			iDiff = *p2++ - p1[k+i]; 
			iSquare+=iDiff * iDiff; 
		} 
	} 
	dRtn = (double)iSquare/(double)iTotal; 
	if(dRtn != 0) 
	{ 
		dRtn = (double)(255 * 255)/dRtn; 
		dRtn = 10 * (double)log10(dRtn); 
	} 
	else 
		dRtn = (float)99.99; 
	encoder->frmsnr.snrCB = dRtn; 
 
	return dRtn; 
} 
double computeSNRCR(H263VencStatus *encoder) 
{ 
	double dRtn; 
	int i, j, k; 
	int iTotal = encoder->lines * encoder->pels  / 4; 
	int lx = encoder->mv_outside_frame ? encoder->pels/2 + 16 : encoder->pels/2; 
	unsigned char *p1 = encoder->frame_buf[encoder->zero_index].pCr; 
	unsigned char *p2 = encoder->frameToEncode.pCr;	 
	// Attention!! The size of the following 2 variables depends on compiler 
	__int64 iDiff; 
	__int64 iSquare; 
 
	if (encoder->PTYPE == B_IMG) 
	{ 
		p1 = encoder->BPicture[0].pCr; 
		p2 = encoder->BPicture[encoder->B_count].pCr; 
		lx = encoder->pels / 2; 
	} 
	iSquare = 0; 
	 
	for(j = 0, k = 0; j < encoder->lines/2; j++, k += lx) 
	{ 
		for (i = 0; i < encoder->pels/2; i ++) 
		{ 
			iDiff = *p2++ - p1[k+i]; 
			iSquare+=iDiff * iDiff; 
		} 
	} 
	dRtn = (double)iSquare/(double)iTotal; 
	if(dRtn != 0) 
	{ 
		dRtn = (double)(255 * 255)/dRtn; 
		dRtn = 10 * (double)log10(dRtn); 
	} 
	else 
		dRtn = (float)99.99; 
	encoder->frmsnr.snrCR = dRtn; 
 
	return dRtn; 
} 
 
#ifdef  __cplusplus 
} 
#endif