From 9c327eb0a377608f925014a030ff3a0a42e2a161 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Wed, 2 Apr 2014 03:28:07 +0100 Subject: [PATCH] Fix input stream buffer behaviour in order to detect end of sequence and add support to lock multiple sequence display --- source/app/InputStream.cpp | 36 +++++--- source/app/InputStream.h | 15 ++-- source/app/SubWindowHandle.cpp | 19 ++--- source/app/SubWindowHandle.h | 3 +- source/app/plaYUVerApp.cpp | 151 ++++++++++++++++++--------------- 5 files changed, 123 insertions(+), 101 deletions(-) diff --git a/source/app/InputStream.cpp b/source/app/InputStream.cpp index 394eed7..20cf1a7 100755 --- a/source/app/InputStream.cpp +++ b/source/app/InputStream.cpp @@ -157,7 +157,7 @@ Bool InputStream::open( QString filename, UInt width, UInt height, Int input_for } } m_uiFrameBufferIndex = 0; - m_pcCurrFrame = m_pcNextFrame = m_ppcFrameBuffer[m_uiFrameBufferIndex]; + m_pcCurrFrame = m_ppcFrameBuffer[m_uiFrameBufferIndex]; UInt64 frame_bytes_input = m_pcCurrFrame->getBytesPerFrame(); @@ -188,11 +188,7 @@ Bool InputStream::open( QString filename, UInt width, UInt height, Int input_for m_cStreamInformationString.append( "] " ); m_cStreamInformationString.append( QFileInfo( m_cFilename ).fileName() ); - m_iCurrFrameNum = -1; - readNextFrame(); - setNextFrame(); - if( m_uiTotalFrameNum > 1 ) - readNextFrame(); + seekInput( 0 ); m_cTimer.start(); m_uiAveragePlayInterval = 0; @@ -298,9 +294,9 @@ Void InputStream::readNextFrame() if( m_iCurrFrameNum + 1 >= ( Int )m_uiTotalFrameNum ) { - m_iErrorStatus = END_OF_SEQ; - m_iCurrFrameNum = 0; - seekInput( m_iCurrFrameNum ); + m_iErrorStatus = LAST_FRAME; + m_pcNextFrame = NULL; + return; } #ifdef USE_FFMPEG @@ -337,10 +333,19 @@ Bool InputStream::writeFrame( const QString& filename ) Void InputStream::setNextFrame() { - m_pcCurrFrame = m_ppcFrameBuffer[m_uiFrameBufferIndex]; - m_pcNextFrame = m_ppcFrameBuffer[!m_uiFrameBufferIndex]; - m_uiFrameBufferIndex = !m_uiFrameBufferIndex; - m_iCurrFrameNum++; + if( m_pcNextFrame ) + { + m_pcCurrFrame = m_pcNextFrame; + m_iCurrFrameNum++; + m_pcNextFrame = m_ppcFrameBuffer[!m_uiFrameBufferIndex]; + m_uiFrameBufferIndex = !m_uiFrameBufferIndex; + } + else + { + m_iErrorStatus = END_OF_SEQ; + m_iCurrFrameNum = 0; + seekInput( m_iCurrFrameNum ); + } } PlaYUVerFrame* InputStream::getCurrFrame( PlaYUVerFrame *pyuv_image ) @@ -382,10 +387,13 @@ Void InputStream::seekInput( UInt64 new_frame_num ) UInt64 nbytes_seek = frame_bytes_input * new_frame_num; fseek( m_pFile, nbytes_seek, SEEK_SET ); } + m_uiFrameBufferIndex = 0; + m_pcCurrFrame = m_pcNextFrame = m_ppcFrameBuffer[m_uiFrameBufferIndex]; m_iCurrFrameNum = new_frame_num - 1; readNextFrame(); setNextFrame(); - readNextFrame(); + if( m_uiTotalFrameNum > 1 ) + readNextFrame(); } Bool InputStream::checkErrors( Int error_type ) diff --git a/source/app/InputStream.h b/source/app/InputStream.h index 34158f1..0dc1636 100755 --- a/source/app/InputStream.h +++ b/source/app/InputStream.h @@ -48,13 +48,6 @@ namespace plaYUVer { -enum InputStream_Errors -{ - NO_STREAM_ERROR = 0, - READING = 1, - END_OF_SEQ = 2, -}; - class InputStream { private: @@ -107,6 +100,14 @@ public: YUVFormat = 0, // Use color space. }; + enum InputStreamErrors + { + NO_STREAM_ERROR = 0, + READING, + LAST_FRAME, + END_OF_SEQ, + }; + Bool open( QString filename, UInt width, UInt height, Int input_format, UInt frame_rate ); Void close(); diff --git a/source/app/SubWindowHandle.cpp b/source/app/SubWindowHandle.cpp index 439b103..e9ca957 100644 --- a/source/app/SubWindowHandle.cpp +++ b/source/app/SubWindowHandle.cpp @@ -175,26 +175,25 @@ Bool SubWindowHandle::save() return true; } -bool SubWindowHandle::playEvent() +Int SubWindowHandle::playEvent() { - bool iRet = true; if( m_bIsPlaying ) { m_pCurrStream->setNextFrame(); refreshFrame(); + if( m_pCurrStream->checkErrors( InputStream::END_OF_SEQ ) ) + { + return -2; + } m_pCurrStream->readNextFrame(); - if( m_pCurrStream->checkErrors( READING ) ) + if( m_pCurrStream->checkErrors( InputStream::READING ) ) { QMessageBox::warning( this, tr( "plaYUVer" ), tr( "Cannot read %1." ).arg( m_cCurrFileName ) ); - return false; + return -3; } - else if( m_pCurrStream->checkErrors( END_OF_SEQ ) ) - { - iRet = false; - } - return iRet; + return 0; } - return false; + return -1; } Void SubWindowHandle::seekAbsoluteEvent( UInt new_frame_num ) diff --git a/source/app/SubWindowHandle.h b/source/app/SubWindowHandle.h index e13b3fa..cbde86e 100644 --- a/source/app/SubWindowHandle.h +++ b/source/app/SubWindowHandle.h @@ -67,7 +67,9 @@ public: Bool loadFile( const QString &fileName ); Bool save(); + Int playEvent(); Void stopEvent(); + Void seekAbsoluteEvent( UInt new_frame_num ); Void seekRelativeEvent( Bool bIsFoward ); @@ -143,7 +145,6 @@ protected: void closeEvent( QCloseEvent *event ); public Q_SLOTS: - bool playEvent(); void adjustScrollBar( double factor ); }; diff --git a/source/app/plaYUVerApp.cpp b/source/app/plaYUVerApp.cpp index 3bec299..676ce13 100644 --- a/source/app/plaYUVerApp.cpp +++ b/source/app/plaYUVerApp.cpp @@ -218,12 +218,11 @@ void plaYUVerApp::updateProperties() void plaYUVerApp::play() { - Bool bFind = false; - - if( actionVideoLock->isChecked() ) + if( m_pcCurrentSubWindow ) { - if( m_pcCurrentSubWindow ) + if( actionVideoLock->isChecked() && m_acPlayingSubWindows.size() ) { + Bool bFind = false; for( Int i = 0; i < m_acPlayingSubWindows.size(); i++ ) { if( m_acPlayingSubWindows.at( i ) == m_pcCurrentSubWindow ) @@ -235,26 +234,28 @@ void plaYUVerApp::play() if( !bFind ) { m_acPlayingSubWindows.append( m_pcCurrentSubWindow ); - m_pcCurrentSubWindow->seekAbsoluteEvent( m_pcFrameSlider->value() ); - m_pcFrameSlider->setMaximum( qMin( m_pcFrameSlider->value(), ( Int )m_pcCurrentSubWindow->getInputStream()->getFrameNum() - 1 ) ); + m_pcCurrentSubWindow->seekAbsoluteEvent( m_acPlayingSubWindows.at( 0 )->getInputStream()->getCurrFrameNum() ); + m_pcFrameSlider->setMaximum( qMin( m_pcFrameSlider->maximum(), ( Int )m_pcCurrentSubWindow->getInputStream()->getFrameNum() - 1 ) ); } } - } - else - { - playingTimer->stop(); - m_uiAveragePlayInterval = 0; - m_bIsPlaying = false; - } - if( !m_bIsPlaying ) - { - UInt frameRate = m_pcCurrentSubWindow->getInputStream()->getFrameRate(); - UInt timeInterval = ( UInt )( 1000.0 / frameRate + 0.5 ); - qDebug( ) << "Desired frame rate: " - << QString::number( 1000 / timeInterval ) - << " fps"; - playingTimer->start( timeInterval ); - m_cTimer.start(); + else + { + playingTimer->stop(); + m_acPlayingSubWindows.clear(); + m_acPlayingSubWindows.append( m_pcCurrentSubWindow ); + m_uiAveragePlayInterval = 0; + m_bIsPlaying = false; + } + if( !m_bIsPlaying ) + { + UInt frameRate = m_acPlayingSubWindows.at( 0 )->getInputStream()->getFrameRate(); + UInt timeInterval = ( UInt )( 1000.0 / frameRate + 0.5 ); + qDebug( ) << "Desired frame rate: " + << QString::number( 1000 / timeInterval ) + << " fps"; + playingTimer->start( timeInterval ); + m_cTimer.start(); + } } } @@ -265,77 +266,90 @@ void plaYUVerApp::pause() void plaYUVerApp::stop() { - if( actionVideoLock->isChecked() ) + if( m_acPlayingSubWindows.size() ) { + for( Int i = 0; i < m_acPlayingSubWindows.size(); i++ ) + { + m_acPlayingSubWindows.at( i )->stopEvent(); + } m_acPlayingSubWindows.clear(); + playingTimer->stop(); + m_pcFrameSlider->setValue( m_pcCurrentSubWindow->getInputStream()->getCurrFrameNum() ); + + if( m_uiAveragePlayInterval ) + qDebug( ) << "Real display time: " + << QString::number( 1000 / m_uiAveragePlayInterval ) + << " fps"; + m_uiAveragePlayInterval = 0; } else { - if( m_pcCurrentSubWindow ) - { - m_pcCurrentSubWindow->stopEvent(); - m_pcFrameSlider->setValue( m_pcCurrentSubWindow->getInputStream()->getCurrFrameNum() ); - playingTimer->stop(); - if( m_uiAveragePlayInterval ) - qDebug( ) << "Real display time: " - << QString::number( 1000 / m_uiAveragePlayInterval ) - << " fps"; - m_uiAveragePlayInterval = 0; - } + m_pcCurrentSubWindow->stopEvent(); + playingTimer->stop(); + m_pcFrameSlider->setValue( m_pcCurrentSubWindow->getInputStream()->getCurrFrameNum() ); } - } void plaYUVerApp::playEvent() { - //m_cTimer.restart(); - if( actionVideoLock->isChecked() ) + UInt time = m_cTimer.elapsed(); + m_cTimer.restart(); + for( Int i = 0; i < m_acPlayingSubWindows.size(); i++ ) { - for( Int i = 0; i < m_acPlayingSubWindows.size(); i++ ) + switch( m_acPlayingSubWindows.at( i )->playEvent() ) { - if( !m_acPlayingSubWindows.at( i )->playEvent() ) + case -1: + stop(); + m_pcFrameSlider->setValue( m_pcCurrentSubWindow ? m_pcCurrentSubWindow->getInputStream()->getCurrFrameNum() : 0 ); + break; + case -2: + if( !actionVideoLoop->isChecked() ) { - if( !actionVideoLoop->isChecked() ) - { - stop(); - } + stop(); + m_pcFrameSlider->setValue( m_pcCurrentSubWindow ? m_pcCurrentSubWindow->getInputStream()->getCurrFrameNum() : 0 ); + return; } - } - m_pcFrameSlider->setValue( m_acPlayingSubWindows.at( 0 )->getInputStream()->getCurrFrameNum() ); - } - else - { - if( m_pcCurrentSubWindow ) - { - UInt time = m_cTimer.elapsed(); - m_cTimer.restart(); - m_uiAveragePlayInterval = ( m_uiAveragePlayInterval + time ) / 2; - - if( !m_pcCurrentSubWindow->playEvent() ) - { - if( !actionVideoLoop->isChecked() ) - { - stop(); - } - } - m_pcFrameSlider->setValue( m_pcCurrentSubWindow->getInputStream()->getCurrFrameNum() ); + break; + case -3: + m_acPlayingSubWindows.at( i )->close(); + break; + default: + break; } } + m_pcFrameSlider->setValue( m_acPlayingSubWindows.at( 0 )->getInputStream()->getCurrFrameNum() ); + m_uiAveragePlayInterval = ( m_uiAveragePlayInterval + time ) / 2; } void plaYUVerApp::seekEvent( int direction ) { - if( activeSubWindow() ) + if( m_acPlayingSubWindows.size() ) { - activeSubWindow()->seekRelativeEvent( direction > 0 ? true : false ); + for( Int i = 0; i < m_acPlayingSubWindows.size(); i++ ) + { + m_acPlayingSubWindows.at( i )->seekRelativeEvent( direction > 0 ? true : false ); + } + } + else + { + if( m_pcCurrentSubWindow ) + m_pcCurrentSubWindow->seekRelativeEvent( direction > 0 ? true : false ); } } void plaYUVerApp::seekSliderEvent( int new_frame_num ) { - if( activeSubWindow() ) + if( m_acPlayingSubWindows.size() ) { - activeSubWindow()->seekAbsoluteEvent( ( UInt )new_frame_num ); + for( Int i = 0; i < m_acPlayingSubWindows.size(); i++ ) + { + m_acPlayingSubWindows.at( i )->seekAbsoluteEvent( ( UInt )new_frame_num ); + } + } + else + { + if( m_pcCurrentSubWindow ) + m_pcCurrentSubWindow->seekAbsoluteEvent( ( UInt )new_frame_num ); } } @@ -348,7 +362,6 @@ void plaYUVerApp::lockButtonEvent() } } - // ----------------------- Zoom Functions ----------------------- void plaYUVerApp::normalSize() @@ -385,11 +398,11 @@ void plaYUVerApp::chageSubWindowSelection() { if( activeSubWindow() ) { - playingTimer->stop(); if( m_pcCurrentSubWindow ) m_pcCurrentSubWindow->disableModule(); - if( !actionVideoLock->isChecked() ) + if( m_acPlayingSubWindows.size() < 2 ) { + playingTimer->stop(); m_pcFrameSlider->setMaximum( activeSubWindow()->getInputStream()->getFrameNum() - 1 ); m_pcFrameSlider->setValue( activeSubWindow()->getInputStream()->getCurrFrameNum() ); }