From 86feb437df4169b87258a02e8b1f79df63c34700 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Tue, 5 Dec 2017 02:27:37 +0000 Subject: [PATCH] Fix libav problems and correct playing timer --- app/VideoHandle.cpp | 2 +- lib/PlaYUVerStream.cpp | 2 +- lib/StreamHandlerLibav.cpp | 108 +++++++++++++++++++++---------------- 3 files changed, 65 insertions(+), 47 deletions(-) diff --git a/app/VideoHandle.cpp b/app/VideoHandle.cpp index 55ef8b9..8a75a31 100644 --- a/app/VideoHandle.cpp +++ b/app/VideoHandle.cpp @@ -47,7 +47,7 @@ VideoHandle::VideoHandle( QWidget* parent, PlaYUVerSubWindowHandle* windowManage m_bIsPlaying = false; m_pcPlayingTimer = new QTimer; #if( QT_VERSION_PLAYUVER == 5 ) - m_pcPlayingTimer->setTimerType( Qt::VeryCoarseTimer ); + m_pcPlayingTimer->setTimerType( Qt::PreciseTimer ); #endif connect( m_pcPlayingTimer, SIGNAL( timeout() ), this, SLOT( playEvent() ) ); m_acPlayingSubWindows.clear(); diff --git a/lib/PlaYUVerStream.cpp b/lib/PlaYUVerStream.cpp index 63be184..efb3f43 100644 --- a/lib/PlaYUVerStream.cpp +++ b/lib/PlaYUVerStream.cpp @@ -592,7 +592,7 @@ Bool PlaYUVerStream::seekInput( UInt64 new_frame_num ) if( !m_pcHandler->seek( m_iCurrFrameNum ) ) { - throw PlaYUVerFailure( "PlaYUVerStream", "Cannot write into the file" ); + throw PlaYUVerFailure( "PlaYUVerStream", "Cannot seek file into desired position" ); } m_frameBuffer->setIndex( 0 ); diff --git a/lib/StreamHandlerLibav.cpp b/lib/StreamHandlerLibav.cpp index 25e15cc..9144d89 100644 --- a/lib/StreamHandlerLibav.cpp +++ b/lib/StreamHandlerLibav.cpp @@ -26,7 +26,9 @@ #include "LibMemory.h" #include "PlaYUVerFrame.h" #include "PlaYUVerFramePixelFormats.h" + #include +#include #if( ( LIBAVCODEC_VERSION_MAJOR >= 57 ) && ( LIBAVCODEC_VERSION_MINOR >= 37 ) ) #define FF_SEND_RECEIVE_API @@ -70,35 +72,34 @@ Bool StreamHandlerLibav::openHandler( String strFilename, Bool bInput ) m_cFrame = NULL; m_bHasStream = false; - AVDictionary* format_opts = NULL; + // AVDictionary* format_opts = NULL; + // if( m_uiWidth > 0 && m_uiHeight > 0 ) + // { + // Char aux_string[10]; + // sprintf( aux_string, "%dx%d", m_uiWidth, m_uiHeight ); + // av_dict_set( &format_opts, "video_size", aux_string, 0 ); + // } + // Int ffmpeg_pel_format = g_PlaYUVerPixFmtDescriptorsList[m_iPixelFormat].ffmpegPelFormat; + // if( ffmpeg_pel_format >= 0 ) + // { + // av_dict_set( &format_opts, "pix_fmt", av_get_pix_fmt_name( AVPixelFormat( ffmpeg_pel_format ) ), 0 ); + // } - // if( m_uiWidth > 0 && m_uiHeight > 0 ) - // { - // Char aux_string[10]; - // sprintf( aux_string, "%dx%d", m_uiWidth, m_uiHeight ); - // av_dict_set( &format_opts, "video_size", aux_string, 0 ); - // } - // Int ffmpeg_pel_format = g_PlaYUVerPixFmtDescriptorsList[m_iPixelFormat].ffmpegPelFormat; - // if( ffmpeg_pel_format >= 0 ) - // { - // av_dict_set( &format_opts, "pix_fmt", av_get_pix_fmt_name( AVPixelFormat( ffmpeg_pel_format ) ), 0 ); - // } // Register all components of FFmpeg av_register_all(); - m_cFmtCtx = avformat_alloc_context(); - /* open input file, and allocate format context */ - if( avformat_open_input( &m_cFmtCtx, filename, NULL, &format_opts ) < 0 ) + if( avformat_open_input( &m_cFmtCtx, filename, NULL, NULL ) < 0 ) { - // qDebug( ) << " Could not open source file %s !!!" << filename << endl; + std::cout << " Could not open source file %s !!!" << filename << std::endl; + return false; } /* retrieve stream information */ if( avformat_find_stream_info( m_cFmtCtx, NULL ) < 0 ) { - // qDebug( ) << " Could not find stream information !!!" << endl; + std::cout << " Could not find stream information !!!" << std::endl; return false; } @@ -113,29 +114,40 @@ Bool StreamHandlerLibav::openHandler( String strFilename, Bool bInput ) m_cStream = m_cFmtCtx->streams[m_iStreamIdx]; /* find decoder for the stream */ - - AVCodecParameters* codec_param = m_cStream->codecpar; - AVCodec* dec = avcodec_find_decoder( codec_param->codec_id ); - + AVCodec* dec = avcodec_find_decoder( m_cStream->codecpar->codec_id ); if( !dec ) { - printf( "Failed to find %s codec\n", av_get_media_type_string( AVMEDIA_TYPE_VIDEO ) ); + std::cout << "Failed to find video coded" << std::endl; return false; } + /* Allocate a codec context for the decoder */ m_cCodedCtx = avcodec_alloc_context3( dec ); - - if( avcodec_open2( m_cCodedCtx, dec, &format_opts ) < 0 ) + if( !m_cCodedCtx ) { - printf( "Failed to open %s codec\n", av_get_media_type_string( AVMEDIA_TYPE_VIDEO ) ); + std::cout << "Failed to allocate the video codec context" << std::endl; return false; } - m_uiFrameBufferSize = av_image_get_buffer_size( AVPixelFormat( codec_param->format ), codec_param->width, codec_param->height, 1 ); + /* Copy codec parameters from input stream to output codec context */ + if( avcodec_parameters_to_context( m_cCodedCtx, m_cStream->codecpar ) < 0 ) + { + std::cout << "Failed to copy video codec parameters to decoder context" << std::endl; + return false; + } - Int pix_fmt = codec_param->format; - m_uiWidth = codec_param->width; - m_uiHeight = codec_param->height; + if( avcodec_open2( m_cCodedCtx, dec, NULL ) < 0 ) + { + std::cout << "Failed to open video coded" << std::endl; + return false; + } + + m_uiFrameBufferSize = + av_image_get_buffer_size( AVPixelFormat( m_cStream->codecpar->format ), m_cStream->codecpar->width, m_cStream->codecpar->height, 1 ); + + Int pix_fmt = m_cStream->codecpar->format; + m_uiWidth = m_cStream->codecpar->width; + m_uiHeight = m_cStream->codecpar->height; /** * Auxiliar conversation to re-use similar pixfmt @@ -164,7 +176,6 @@ Bool StreamHandlerLibav::openHandler( String strFilename, Bool bInput ) m_strFormatName = uppercase( strFilename.substr( strFilename.find_last_of( "." ) + 1 ) ); const char* name = avcodec_get_name( m_cCodedCtx->codec_id ); - // sprintf( m_acCodecName, "%s", name ); m_strCodecName = name; Double fr = 30; @@ -201,8 +212,7 @@ Bool StreamHandlerLibav::openHandler( String strFilename, Bool bInput ) av_dump_format( m_cFmtCtx, 0, filename, 0 ); if( !m_cStream ) { - // qDebug( ) << " Could not find audio or video stream in the input, - // aborting !!!" << endl; + std::cout << " Could not find audio or video stream in the input, aborting !!!" << std::endl; closeHandler(); return false; } @@ -277,22 +287,18 @@ Bool StreamHandlerLibav::read( PlaYUVerFrame* pcFrame ) Bool bGotFrame = false; Bool bErrors = false; Bool bReadPkt = false; - Int iRet; + Int iRet = 0; while( !bGotFrame && !bErrors ) { - iRet = avcodec_receive_frame( m_cCodedCtx, m_cFrame ); - if( iRet > 0 ) + if( ( iRet = avcodec_receive_frame( m_cCodedCtx, m_cFrame ) ) < 0 ) { - bGotFrame = true; - break; + if( iRet == AVERROR( EAGAIN ) || iRet == AVERROR_EOF ) + bReadPkt = true; + else + return false; } - else if( iRet == AVERROR( EAGAIN ) || iRet == AVERROR_EOF ) - bReadPkt = true; - else if( iRet < 0 ) - return false; - - if( iRet > 0 ) + else { bGotFrame = true; break; @@ -306,7 +312,7 @@ Bool StreamHandlerLibav::read( PlaYUVerFrame* pcFrame ) if( m_cPacket.stream_index == m_iStreamIdx ) // iRet = avcodec_send_packet( m_cCodedCtx, &m_cPacket ); if( ( iRet = avcodec_send_packet( m_cCodedCtx, &m_cPacket ) ) < 0 ) - return iRet; + return false; } } @@ -330,7 +336,19 @@ Bool StreamHandlerLibav::write( PlaYUVerFrame* pcFrame ) Bool StreamHandlerLibav::seek( UInt64 iFrameNum ) { - av_seek_frame( m_cFmtCtx, m_iStreamIdx, iFrameNum, AVSEEK_FLAG_FRAME ); + if( m_uiCurrFrameFileIdx == iFrameNum ) + return true; + + Int flags = AVSEEK_FLAG_ANY | AVSEEK_FLAG_FRAME; + if( iFrameNum < m_uiCurrFrameFileIdx ) + { + flags |= AVSEEK_FLAG_BACKWARD; + } + Int iRet = av_seek_frame( m_cFmtCtx, m_iStreamIdx, iFrameNum, flags ); + if( iRet < 0 ) + { + return false; + } m_uiCurrFrameFileIdx = iFrameNum; return true; }