From c07bce8d411d88fc6cdccc9739aed14e0c72f123 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Mon, 31 Mar 2014 16:09:07 +0100 Subject: [PATCH] Improve code --- source/app/InputStream.cpp | 33 ++++++++++++++++++----------- source/app/InputStream.h | 3 +++ source/lib/PlaYUVerFrame.cpp | 40 ++++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/source/app/InputStream.cpp b/source/app/InputStream.cpp index 34dd57c..94533b1 100755 --- a/source/app/InputStream.cpp +++ b/source/app/InputStream.cpp @@ -58,6 +58,7 @@ InputStream::InputStream() m_pcNextFrame = NULL; m_ppcFrameBuffer = NULL; m_uiFrameBufferSize = 2; + m_uiAveragePlayInterval = 0; } InputStream::~InputStream() @@ -193,8 +194,10 @@ Bool InputStream::open( QString filename, UInt width, UInt height, Int input_for if( m_uiTotalFrameNum > 1 ) readNextFrame(); - m_bInit = true; + m_cTimer.start(); + m_uiAveragePlayInterval = 0; + m_bInit = true; return m_bInit; } @@ -220,6 +223,8 @@ Void InputStream::close() if( m_pInputBuffer ) freeMem1D( m_pInputBuffer ); + qDebug() << "Frame read time: " << QString::number( 1000 / (m_uiAveragePlayInterval+1) ) << " fps"; + m_bInit = false; } @@ -285,7 +290,7 @@ Bool InputStream::guessFormat( QString filename, UInt& rWidth, UInt& rHeight, In Void InputStream::readNextFrame() { UInt64 bytes_read = 0; - + m_cTimer.restart(); if( m_iStatus == 0 ) { return; @@ -303,20 +308,24 @@ Void InputStream::readNextFrame() { m_cLibAvContext.decodeAvFormat(); m_pcNextFrame->FrameFromBuffer( m_cLibAvContext.video_dst_data[0], m_iPixelFormat ); - return; } + else #endif - - UInt64 frame_bytes_input = m_pcNextFrame->getBytesPerFrame(); - bytes_read = fread( m_pInputBuffer, sizeof(Pel), frame_bytes_input, m_pFile ); - if( bytes_read != frame_bytes_input ) { - m_iErrorStatus = READING; - qDebug( ) << " Reading error !!!" - << endl; - return; + UInt64 frame_bytes_input = m_pcNextFrame->getBytesPerFrame(); + bytes_read = fread( m_pInputBuffer, sizeof(Pel), frame_bytes_input, m_pFile ); + if( bytes_read != frame_bytes_input ) + { + m_iErrorStatus = READING; + qDebug( ) << " Reading error !!!" + << endl; + return; + } + m_pcNextFrame->FrameFromBuffer( m_pInputBuffer, m_iPixelFormat ); } - m_pcNextFrame->FrameFromBuffer( m_pInputBuffer, m_iPixelFormat ); + Int time = m_cTimer.elapsed(); + m_uiAveragePlayInterval = ( m_uiAveragePlayInterval + time) / 2; + m_pcNextFrame->FrametoRGB8(); return; } diff --git a/source/app/InputStream.h b/source/app/InputStream.h index c068468..34158f1 100755 --- a/source/app/InputStream.h +++ b/source/app/InputStream.h @@ -71,6 +71,9 @@ private: QString m_cFormatName; QString m_cStreamInformationString; + QTime m_cTimer; + UInt m_uiAveragePlayInterval; + FILE* m_pFile; /**< The input file pointer >*/ Int m_iFileFormat; Int m_iPixelFormat; diff --git a/source/lib/PlaYUVerFrame.cpp b/source/lib/PlaYUVerFrame.cpp index f383e62..6c7e0b9 100755 --- a/source/lib/PlaYUVerFrame.cpp +++ b/source/lib/PlaYUVerFrame.cpp @@ -121,6 +121,28 @@ Void yuvToRgb( T iY, T iU, T iV, T &iR, T &iG, T &iB ) iB = 255; } +static inline Void yuvToRgb( Int iY, Int iU, Int iV, Int &iR, Int &iG, Int &iB ) +{ + iR = iY + 1402 * iV / 1000; + iG = iY - ( 101004 * iU + 209599 * iV ) / 293500; + iB = iY + 1772 * iU / 1000; + + if( iR < 0 ) + iR = 0; + if( iG < 0 ) + iG = 0; + if( iB < 0 ) + iB = 0; + + if( iR > 255 ) + iR = 255; + if( iG > 255 ) + iG = 255; + if( iB > 255 ) + iB = 255; +} + + template Void rgbToyuv( T iR, T iG, T iB, T &iY, T &iU, T &iV ) { @@ -191,9 +213,9 @@ Void PlaYUVerFrame::FrametoRGB8() for( UInt y = 0; y < m_uiHeight; y++ ) for( UInt x = 0; x < m_uiWidth; x++ ) { - *pcRGBPelInterlaced++ = m_pppcRGBPel[COLOR_R][y][x]; - *pcRGBPelInterlaced++ = m_pppcRGBPel[COLOR_G][y][x]; - *pcRGBPelInterlaced++ = m_pppcRGBPel[COLOR_B][y][x]; + *pcRGBPelInterlaced++ = *pRGBPelR++; + *pcRGBPelInterlaced++ = *pRGBPelG++; + *pcRGBPelInterlaced++ = *pRGBPelB++; } break; case YUV422p: @@ -218,9 +240,9 @@ Void PlaYUVerFrame::FrametoRGB8() for( UInt y = 0; y < m_uiHeight; y++ ) for( UInt x = 0; x < m_uiWidth; x++ ) { - *pcRGBPelInterlaced++ = m_pppcRGBPel[COLOR_R][y][x]; - *pcRGBPelInterlaced++ = m_pppcRGBPel[COLOR_G][y][x]; - *pcRGBPelInterlaced++ = m_pppcRGBPel[COLOR_B][y][x]; + *pcRGBPelInterlaced++ = *pRGBPelR++; + *pcRGBPelInterlaced++ = *pRGBPelG++; + *pcRGBPelInterlaced++ = *pRGBPelB++; } break; case YUV444p: @@ -261,9 +283,9 @@ Void PlaYUVerFrame::FrametoRGB8() for( UInt y = 0; y < m_uiHeight; y++ ) for( UInt x = 0; x < m_uiWidth; x++ ) { - *pcRGBPelInterlaced++ = m_pppcInputPel[COLOR_R][y][x]; - *pcRGBPelInterlaced++ = m_pppcInputPel[COLOR_G][y][x]; - *pcRGBPelInterlaced++ = m_pppcInputPel[COLOR_B][y][x]; + *pcRGBPelInterlaced++ = *pRGBPelR++; + *pcRGBPelInterlaced++ = *pRGBPelG++; + *pcRGBPelInterlaced++ = *pRGBPelB++; } break; default: