diff --git a/source/app/ModulesHandle.cpp b/source/app/ModulesHandle.cpp index c75502b..47c1ddd 100644 --- a/source/app/ModulesHandle.cpp +++ b/source/app/ModulesHandle.cpp @@ -251,15 +251,28 @@ Void ModulesHandle::openModuleIfStream( PlaYUVerModuleIf *pcCurrModuleIf ) UInt Width = 0, Height = 0, FrameRate = 30; Int InputFormat = -1; - QString supported = tr( "Supported Files" ); - QString formats = PlaYUVerStream::supportedWriteFormats(); - formats.prepend( " (" ); - formats.append( ")" ); - supported.append( formats ); // supported=="Supported Files (*.pbm *.jpg...)" + + QString supported = tr( "Supported Files (" ); + QStringList formatsList; + QStringList formatsExt = PlaYUVerStream::supportedWriteFormatsExt(); + QStringList formatsName = PlaYUVerStream::supportedWriteFormatsName(); + + for( Int i = 0; i < formatsName.size(); i++ ) + { + QString currFmt; + supported.append( " *." ); + supported.append( formatsExt[i] ); + currFmt.append( formatsName[i] ); + currFmt.append( " (*." ); + currFmt.append( formatsExt[i] ); + currFmt.append( ")" ); + formatsList << currFmt; + } + supported.append( " )" ); QStringList filter; filter << supported - << PlaYUVerStream::supportedWriteFormatsList() + << formatsList << tr( "All Files (*)" ); QString fileName = QFileDialog::getSaveFileName( m_pcParent, tr( "Open File" ), QString(), filter.join( ";;" ) ); diff --git a/source/app/SubWindowHandle.cpp b/source/app/SubWindowHandle.cpp index a866484..c54467e 100644 --- a/source/app/SubWindowHandle.cpp +++ b/source/app/SubWindowHandle.cpp @@ -130,7 +130,7 @@ Void SubWindowHandle::enableModule( PlaYUVerModuleIf* select_module ) disableModule(); } m_pcCurrentModule = select_module; - // refreshFrame(); + // refreshFrame(); } Void SubWindowHandle::disableModule() @@ -200,33 +200,12 @@ Void SubWindowHandle::refreshFrame() } } -Bool SubWindowHandle::save() +Bool SubWindowHandle::save( QString filename ) { - QString supported = tr( "Supported Files" ); - QString formats = PlaYUVerStream::supportedWriteFormats(); - formats.prepend( " (" ); - formats.append( ")" ); - supported.append( formats ); // supported=="Supported Files (*.pbm *.jpg...)" - QStringList filter; - filter << supported - << PlaYUVerStream::supportedWriteFormatsList() - << tr( "All Files (*)" ); - QString fileName = QFileDialog::getSaveFileName( this, tr( "Save As" ), m_cCurrFileName, filter.join( ";;" ) ); - if( fileName.isEmpty() ) - return false; - QApplication::setOverrideCursor( Qt::WaitCursor ); - m_pCurrStream->getCurrFrame()->FrametoRGB8(); - QImage qimg = QImage( m_pCurrStream->getCurrFrame()->getQImageBuffer(), m_pCurrStream->getCurrFrame()->getWidth(), m_pCurrStream->getCurrFrame()->getHeight(), - QImage::Format_RGB888 ); - if( qimg.save( fileName ) ) - { - QApplication::restoreOverrideCursor(); - QMessageBox::warning( this, tr( "plaYUVer" ), tr( "Cannot save file %1" ).arg( fileName ) ); - return false; - } + Bool iRet = m_pCurrStream->saveFrame( filename ); QApplication::restoreOverrideCursor(); - return true; + return iRet; } Void SubWindowHandle::play() @@ -414,9 +393,8 @@ QSize SubWindowHandle::sizeHint() const QSize isize; if( m_pcCurrFrame ) isize = QSize( m_pcCurrFrame->getWidth() + 50, m_pcCurrFrame->getHeight() + 50 ); - else - if( m_pCurrStream ) - isize = QSize( m_pCurrStream->getWidth() + 50, m_pCurrStream->getHeight() + 50 ); + else if( m_pCurrStream ) + isize = QSize( m_pCurrStream->getWidth() + 50, m_pCurrStream->getHeight() + 50 ); // If the SubWindowHandle needs more space that the avaiable, we'll give // to the subwindow a reasonable size preserving the image aspect ratio. diff --git a/source/app/SubWindowHandle.h b/source/app/SubWindowHandle.h index 16cb400..e58b392 100644 --- a/source/app/SubWindowHandle.h +++ b/source/app/SubWindowHandle.h @@ -68,7 +68,7 @@ public: Bool mayClose(); Bool loadFile( QString cFilename, Bool bForceDialog = false ); Void reloadFile(); - Bool save(); + Bool save( QString filename ); Void play(); Void pause(); diff --git a/source/app/plaYUVerApp.cpp b/source/app/plaYUVerApp.cpp index 7d925e2..a0e1627 100644 --- a/source/app/plaYUVerApp.cpp +++ b/source/app/plaYUVerApp.cpp @@ -188,15 +188,27 @@ Void plaYUVerApp::loadFile( QString fileName ) Void plaYUVerApp::open() { - QString supported = tr( "Supported Files" ); - QString formats = PlaYUVerStream::supportedReadFormats(); - formats.prepend( " (" ); - formats.append( ")" ); - supported.append( formats ); // supported=="Supported Files (*.pbm *.jpg...)" + QString supported = tr( "Supported Files (" ); + QStringList formatsList; + QStringList formatsExt = PlaYUVerStream::supportedReadFormatsExt(); + QStringList formatsName = PlaYUVerStream::supportedReadFormatsName(); + + for( Int i = 0; i < formatsName.size(); i++ ) + { + QString currFmt; + supported.append( " *." ); + supported.append( formatsExt[i] ); + currFmt.append( formatsName[i] ); + currFmt.append( " (*." ); + currFmt.append( formatsExt[i] ); + currFmt.append( ")" ); + formatsList << currFmt; + } + supported.append( " )" ); QStringList filter; filter << supported - << PlaYUVerStream::supportedReadFormatsList() + << formatsList << tr( "All Files (*)" ); QString fileName = QFileDialog::getOpenFileName( this, tr( "Open File" ), m_cLastOpenPath, filter.join( ";;" ) ); @@ -211,7 +223,43 @@ Void plaYUVerApp::open() Void plaYUVerApp::save() { if( m_pcCurrentSubWindow ) - m_pcCurrentSubWindow->save(); + { + QString supported = tr( "Supported Files (" ); + QStringList formatsList; + QStringList formatsExt = PlaYUVerStream::supportedSaveFormatsExt(); + QStringList formatsName = PlaYUVerStream::supportedSaveFormatsName(); + + for( Int i = 0; i < formatsName.size(); i++ ) + { + QString currFmt; + supported.append( " *." ); + supported.append( formatsExt[i] ); + currFmt.append( formatsName[i] ); + currFmt.append( " (*." ); + currFmt.append( formatsExt[i] ); + currFmt.append( ")" ); + formatsList << currFmt; + } + supported.append( " )" ); + + QStringList filter; + filter << supported + << formatsList + << tr( "All Files (*)" ); + + QString fileName = QFileDialog::getSaveFileName( this, tr( "Open File" ), QString(), filter.join( ";;" ) ); + + if( !fileName.isEmpty() ) + { + m_cLastOpenPath = QFileInfo( fileName ).path(); + if( !m_pcCurrentSubWindow->save( fileName ) ) + { + QApplication::restoreOverrideCursor(); + QMessageBox::warning( this, tr( "plaYUVer" ), tr( "Cannot save file %1" ).arg( fileName ) ); + return; + } + } + } } Void plaYUVerApp::format() diff --git a/source/lib/PlaYUVerStream.cpp b/source/lib/PlaYUVerStream.cpp index 5176e91..70e7f70 100644 --- a/source/lib/PlaYUVerStream.cpp +++ b/source/lib/PlaYUVerStream.cpp @@ -26,6 +26,7 @@ #include #include "LibMemory.h" #include "PlaYUVerStream.h" +#include #ifdef USE_FFMPEG #include "LibAvContextHandle.h" #endif @@ -47,7 +48,7 @@ PlaYUVerStream::PlaYUVerStream() m_iCurrFrameNum = -1; m_iPixelFormat = -1; m_uiFrameRate = 30; - m_iFileFormat = INVALID; + m_iFileFormat = INVALID_INPUT; m_pStreamBuffer = NULL; m_cFilename = QString( "" ); m_cStreamInformationString = QString( "" ); @@ -57,8 +58,9 @@ PlaYUVerStream::PlaYUVerStream() m_uiFrameBufferSize = 2; m_uiFrameBufferIndex = 0; m_uiAveragePlayInterval = 0; - - m_cLibAvContext = NULL; +#ifdef USE_FFMPEG + m_cLibAvContext = new LibAvContextHandle; +#endif } PlaYUVerStream::~PlaYUVerStream() @@ -66,65 +68,6 @@ PlaYUVerStream::~PlaYUVerStream() close(); } -QString PlaYUVerStream::supportedReadFormats() -{ - QString formats; - formats = "*.yuv " // Raw video -#ifdef USE_FFMPEG - "*.avi " // Audio video interleaved - "*.mp4 "// MP4 - "*.wmv "// Windows media video -#endif -; return formats; -} - -QStringList PlaYUVerStream::supportedReadFormatsList() -{ - QStringList formats; - formats << "Raw video (*.yuv)" -#ifdef USE_FFMPEG - << "Audio video interleaved (*.avi)" - << "MPEG4 (*.mp4)" - << "Windows media video (*.wmv)" -#endif - ; - return formats; -} - -QString PlaYUVerStream::supportedWriteFormats() -{ - QString formats; - formats = "*.yuv " // raw video - ; - return formats; -} - -QStringList PlaYUVerStream::supportedWriteFormatsList() -{ - QStringList formats; - formats << "Raw video (*.yuv)"; - return formats; -} - -QString PlaYUVerStream::supportedSaveFormats() -{ - QString formats; - formats = "*.jpg " // Joint Photographic Experts Group - "*.jpeg "// Joint Photographic Experts Group - "*.png "// Portable Network Graphics - ; - return formats; -} - -QStringList PlaYUVerStream::supportedSaveFormatsList() -{ - QStringList formats; - formats << "Windows Bitmap (*.bmp)" - << "Joint Photographic Experts Group (*.jpg *.jpeg)" - << "Portable Network Graphics (*.png)"; - return formats; -} - Bool PlaYUVerStream::open( QString filename, UInt width, UInt height, Int input_format, UInt frame_rate, Bool bInput ) { Bool avStatus = false; @@ -141,17 +84,38 @@ Bool PlaYUVerStream::open( QString filename, UInt width, UInt height, Int input_ m_uiHeight = height; m_uiFrameRate = frame_rate; - m_iFileFormat = YUVFormat; - m_iPixelFormat = input_format; -#ifdef USE_FFMPEG - if( m_bIsInput && QFileInfo( filename ).suffix().compare( QString( "yuv" ) ) ) + if( m_bIsInput ) { - avStatus = m_cLibAvContext->initAvFormat( m_cFilename.toLocal8Bit().data(), m_uiWidth, m_uiHeight, m_iPixelFormat, m_uiFrameRate, m_uiTotalFrameNum ); - m_cFormatName = QFileInfo( filename ).completeSuffix().toUpper(); - m_cCodedName = QString::fromUtf8( m_cLibAvContext->getCodecName() ).toUpper(); + m_iFileFormat = YUVINPUT; + + QStringList formatsExt = PlaYUVerStream::supportedReadFormatsExt(); + QString currExt = QFileInfo( filename ).suffix(); + if( formatsExt.contains( currExt ) ) + { + m_iFileFormat = formatsExt.indexOf( currExt ); + } + + m_iPixelFormat = input_format; + + #ifdef USE_FFMPEG + if( m_bIsInput /*&& m_iFileFormat != YUVINPUT */) + { + avStatus = m_cLibAvContext->initAvFormat( m_cFilename.toLocal8Bit().data(), m_uiWidth, m_uiHeight, m_iPixelFormat, m_uiFrameRate, m_uiTotalFrameNum ); + m_cFormatName = QFileInfo( filename ).completeSuffix().toUpper(); + m_cCodedName = QString::fromUtf8( m_cLibAvContext->getCodecName() ).toUpper(); + } + #endif + + m_uiFrameBufferSize = 2; } -#endif + else + { + m_iFileFormat = YUVOUTPUT; + m_uiFrameBufferSize = 1; + } + + if( m_uiWidth <= 0 || m_uiHeight <= 0 || m_iPixelFormat < 0 ) { @@ -159,14 +123,6 @@ Bool PlaYUVerStream::open( QString filename, UInt width, UInt height, Int input_ return m_bInit; } - if( m_bIsInput ) - { - m_uiFrameBufferSize = 2; - } - else - { - m_uiFrameBufferSize = 1; - } getMem1D( &m_ppcFrameBuffer, m_uiFrameBufferSize ); for( UInt i = 0; i < m_uiFrameBufferSize; i++ ) { @@ -301,7 +257,7 @@ Bool PlaYUVerStream::guessFormat( QString filename, UInt& rWidth, UInt& rHeight, resolutionString.remove( "_" ); QStringList resolutionArgs = resolutionString.split( "x" ); qDebug( ) << "Found resolution = " - << resolutionArgs; + << resolutionArgs; if( resolutionArgs.size() == 2 ) { rWidth = resolutionArgs.at( 0 ).toUInt(); @@ -454,9 +410,10 @@ Void PlaYUVerStream::writeFrame( PlaYUVerFrame *pcFrame ) return; } -Void PlaYUVerStream::saveFrame( const QString& filename ) +Bool PlaYUVerStream::saveFrame( const QString& filename ) { - return; + QImage qimg = QImage( m_pcCurrFrame->getQImageBuffer(), m_pcCurrFrame->getWidth(), m_pcCurrFrame->getHeight(), QImage::Format_RGB888 ); + return qimg.save( filename ); } Void PlaYUVerStream::setNextFrame() @@ -495,7 +452,7 @@ PlaYUVerFrame* PlaYUVerStream::getCurrFrame() Void PlaYUVerStream::seekInput( UInt64 new_frame_num ) { - if( !m_pFile || new_frame_num < 0 || new_frame_num >= m_uiTotalFrameNum || ( Int64 )new_frame_num == m_iCurrFrameNum ) + if( !m_bInit || new_frame_num < 0 || new_frame_num >= m_uiTotalFrameNum || ( Int64 )new_frame_num == m_iCurrFrameNum ) return; m_iCurrFrameNum = new_frame_num - 1; diff --git a/source/lib/PlaYUVerStream.h b/source/lib/PlaYUVerStream.h index 72d079d..f8c1dd9 100644 --- a/source/lib/PlaYUVerStream.h +++ b/source/lib/PlaYUVerStream.h @@ -77,19 +77,101 @@ public: PlaYUVerStream(); ~PlaYUVerStream(); - static QString supportedReadFormats(); - static QStringList supportedReadFormatsList(); - static QString supportedWriteFormats(); - static QStringList supportedWriteFormatsList(); - static QString supportedSaveFormats(); - static QStringList supportedSaveFormatsList(); - - enum PlaYUVerStreamFormats + enum PlaYUVerStreamInputFormats { - INVALID = -1, - YUVFormat = 0, // Use color space. + INVALID_INPUT = -1, + YUVINPUT = 0, +#ifdef USE_FFMPEG + AVIINPUT, + MP4INPUT, + WMVINPUT, + PGMINPUT, + PNGINPUT, + BMPINPUT, + JPEGINPUT, +#endif + TOTAL_INPUT_FORMATS }; + static QStringList supportedReadFormatsExt() + { + QStringList formatsExt; + formatsExt << "yuv" // Raw video +#ifdef USE_FFMPEG + << "avi" // Audio video interleaved + << "mp4"// MP4 + << "wmv"// Windows media video + << "pgm" + << "png" + << "bmp" + << "jpg" +#endif + ; + assert( formatsExt.size() == TOTAL_INPUT_FORMATS ); + return formatsExt; + } + + static QStringList supportedReadFormatsName() + { + QStringList formatsName; + formatsName << "Raw video" +#ifdef USE_FFMPEG + << "Audio video interleaved" + << "MPEG4" + << "Windows media video" + << "Portable Grayscale Map" + << "Portable Network Graphics" + << "Windows Bitmap" + << "Joint Photographic Experts Group" +#endif + ; + assert( formatsName.size() == TOTAL_INPUT_FORMATS ); + return formatsName; + } + + enum PlaYUVerStreamOutputFormats + { + INVALID_OUTPUT = -1, + YUVOUTPUT = 0, + TOTAL_OUTPUT_FORMATS + }; + + static QStringList supportedWriteFormatsExt() + { + QStringList formatsExt; + formatsExt << "yuv" // raw video + ; + assert( formatsExt.size() == TOTAL_OUTPUT_FORMATS ); + return formatsExt; + } + + static QStringList supportedWriteFormatsName() + { + QStringList formatsName; + formatsName << "Raw video"; + assert( formatsName.size() == TOTAL_OUTPUT_FORMATS ); + return formatsName; + } + + static QStringList supportedSaveFormatsExt() + { + QStringList formatsExt; + formatsExt << "bmp" + << "jpeg" + << "png" // portable network graphics + ; + return formatsExt; + } + + static QStringList supportedSaveFormatsName() + { + QStringList formatsName; + formatsName << "Windows Bitmap" + << "Joint Photographic Experts Group" + << "Portable Network Graphics"; + return formatsName; + } + enum PlaYUVerStreamErrors { NO_STREAM_ERROR = 0, @@ -111,7 +193,7 @@ public: Void writeFrame(); Void writeFrame( PlaYUVerFrame *pcFrame ); - Void saveFrame( const QString& filename ); + Bool saveFrame( const QString& filename ); Void setNextFrame(); PlaYUVerFrame* getCurrFrame(); diff --git a/source/lib/ffmpeg/LibAvContextHandle.cpp b/source/lib/ffmpeg/LibAvContextHandle.cpp index 436933d..efe8895 100644 --- a/source/lib/ffmpeg/LibAvContextHandle.cpp +++ b/source/lib/ffmpeg/LibAvContextHandle.cpp @@ -108,16 +108,35 @@ Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height sprintf( aux_string, "%dx%d", width, height ); av_dict_set( &format_opts, "video_size", aux_string, 0 ); } +// YUV420p = 0, +// YUV444p, +// YUV422p, +// YUYV422, +// GRAY, +// RGB8, switch( pixel_format ) { case PlaYUVerFrame::NO_FMT: break; + case PlaYUVerFrame::YUV420p: av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUV420P ), 0 ); break; + case PlaYUVerFrame::YUV444p: + av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUV444P ), 0 ); + break; + case PlaYUVerFrame::YUV422p: + av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUV422P ), 0 ); + break; + case PlaYUVerFrame::YUYV422: + av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_YUYV422 ), 0 ); + break; case PlaYUVerFrame::GRAY: av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_GRAY8 ), 0 ); break; + case PlaYUVerFrame::RGB8: + av_dict_set( &format_opts, "pixel_format", av_get_pix_fmt_name( AV_PIX_FMT_RGB24 ), 0 ); + break; default: break; } @@ -152,8 +171,8 @@ Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height video_dst_bufsize = ret; } - const char *codec_name = avcodec_get_name(video_dec_ctx->codec_id); - sprintf(m_acCodecName, "%s", codec_name); + const char *codec_name = avcodec_get_name( video_dec_ctx->codec_id ); + sprintf( m_acCodecName, "%s", codec_name ); Double fr = 30; if( video_stream->avg_frame_rate.den && video_stream->avg_frame_rate.num ) @@ -188,6 +207,9 @@ Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height case AV_PIX_FMT_YUV420P: pixel_format = PlaYUVerFrame::YUV420p; break; + case AV_PIX_FMT_YUV444P: + pixel_format = PlaYUVerFrame::YUV444p; + break; case AV_PIX_FMT_YUV422P: pixel_format = PlaYUVerFrame::YUV422p; break; @@ -213,7 +235,7 @@ Bool LibAvContextHandle::initAvFormat( char* filename, UInt& width, UInt& height closeAvFormat(); return false; } - + #if( LIBAVFORMAT_VERSION_MAJOR >= 55 ) frame = av_frame_alloc(); #else