00001 #include "YUVFrame.hpp" 00002 #include "YUVStream.hpp" 00003 #include "GOP.hpp" 00004 #include "MPGFrame.hpp" 00005 #include <math.h> 00006 00007 00008 YUVFrame::YUVFrame(long noS, unsigned int noP, 00009 long offset, GOP * pat, FrameType t):Frame(noS, noP, 00010 offset, pat, t) { 00011 00012 this->psnr = 0; 00013 } 00014 00015 00016 YUVFrame::~YUVFrame() { 00017 } 00018 00019 00020 void YUVFrame::setCompressedByteSize(uint siz) { 00021 compBSize = siz; 00022 } 00023 00024 00025 uint YUVFrame::getCompressedByteSize() { 00026 return compBSize; 00027 } 00028 00029 00030 MPGFrame* YUVFrame::getMPGFrame() { 00031 return mpgFrame; 00032 } 00033 00034 00035 void YUVFrame::setMPGFrame(MPGFrame * mpg) { 00036 mpgFrame = mpg; 00037 } 00038 00039 00040 double YUVFrame::computePSNRy(YUVFrame * other) { 00041 00042 if (!inMem()) { 00043 #if MYDEBUG >= 1 00044 cerr << "Can't compute PSNR! Frame isn't in memory!\n"; 00045 #endif 00046 return ERR_PSNR; 00047 } 00048 00049 double rmse, mse; 00050 mse = 0; 00051 rmse = 0; 00052 int k = 0; 00053 double temp = 0; 00054 unsigned char *otherData = (unsigned char *) other->getBuffer(); 00055 unsigned char *thisData = (unsigned char *) getBuffer(); 00056 YUVStream *thisParentStream = (YUVStream *) pattern->getStream(); 00057 long yOffset = thisParentStream->getBufOffsetY(); 00058 int width = thisParentStream->getWidth(); 00059 int height = thisParentStream->getHeight(); 00060 00061 #if MYDEBUG >=4 00062 cout << "yOffset: " << yOffset << nl; 00063 #endif 00064 00065 for (int i = 0; i < width; i++) { 00066 for (int j = 0; j < height; j++) { 00067 temp = (double) thisData[k + yOffset] - 00068 (double) otherData[k + yOffset]; 00069 mse += temp * temp; 00070 k++; 00071 } 00072 } 00073 00074 if (mse == 0) { 00075 // frames are equal return maxPSNR of stream 00076 YUVStream *ystr = (YUVStream *) pattern->getStream(); 00077 return ystr->getMaxPSNR(); 00078 } 00079 00080 mse = (double) mse / ((double) width * (double) height); 00081 rmse = sqrt(mse); 00082 return 20 * log10(255.0 / rmse); 00083 } 00084 00085 00086 double YUVFrame::computePSNR(YUVFrame * f) { 00087 if (!inMem()) { 00088 #if MYDEBUG >= 1 00089 cerr << "Can't compute PSNR! Frame isn't in memory!\n"; 00090 #endif 00091 return ERR_PSNR; 00092 } 00093 00094 this->psnr = this->computePSNRy(f); 00095 00096 return this->psnr; 00097 }