@ -26,15 +26,15 @@ DATVideoRender::DATVideoRender(QWidget * parent):
@@ -26,15 +26,15 @@ DATVideoRender::DATVideoRender(QWidget * parent):
m_blnIsFFMPEGInitialized = false ;
m_blnIsOpen = false ;
m_objFormatCtx = NULL ;
m_objDecoderCtx = NULL ;
m_objSwsCtx = NULL ;
m_objFormatCtx = nullptr ;
m_objDecoderCtx = nullptr ;
m_objSwsCtx = nullptr ;
m_intVideoStreamIndex = - 1 ;
m_intCurrentRenderWidth = - 1 ;
m_intCurrentRenderHeight = - 1 ;
m_objFrame = NULL ;
m_objFrame = nullptr ;
m_intFrameCount = - 1 ;
}
@ -55,17 +55,15 @@ bool DATVideoRender::eventFilter(QObject *obj, QEvent *event)
@@ -55,17 +55,15 @@ bool DATVideoRender::eventFilter(QObject *obj, QEvent *event)
void DATVideoRender : : SetFullScreen ( bool blnFullScreen )
{
if ( m_blnIsFullScreen = = blnFullScreen )
{
if ( m_blnIsFullScreen = = blnFullScreen ) {
return ;
}
if ( blnFullScreen = = true )
if ( blnFullScreen = = true )
{
setWindowFlags ( Qt : : Window ) ;
setWindowState ( Qt : : WindowFullScreen ) ;
show ( ) ;
m_blnIsFullScreen = true ;
}
else
@ -73,7 +71,6 @@ void DATVideoRender::SetFullScreen(bool blnFullScreen)
@@ -73,7 +71,6 @@ void DATVideoRender::SetFullScreen(bool blnFullScreen)
setWindowFlags ( Qt : : Widget ) ;
setWindowState ( Qt : : WindowNoState ) ;
show ( ) ;
m_blnIsFullScreen = false ;
}
}
@ -90,18 +87,15 @@ static int64_t SeekFunction(void* opaque, int64_t offset, int whence)
@@ -90,18 +87,15 @@ static int64_t SeekFunction(void* opaque, int64_t offset, int whence)
{
QIODevice * objStream = reinterpret_cast < QIODevice * > ( opaque ) ;
if ( whence = = AVSEEK_SIZE )
{
if ( whence = = AVSEEK_SIZE ) {
return - 1 ;
}
if ( objStream - > isSequential ( ) )
{
if ( objStream - > isSequential ( ) ) {
return - 1 ;
}
if ( objStream - > seek ( offset ) = = false )
{
if ( objStream - > seek ( offset ) = = false ) {
return - 1 ;
}
@ -131,14 +125,12 @@ bool DATVideoRender::InitializeFFMPEG()
@@ -131,14 +125,12 @@ bool DATVideoRender::InitializeFFMPEG()
{
ResetMetaData ( ) ;
if ( m_blnIsFFMPEGInitialized )
{
if ( m_blnIsFFMPEGInitialized ) {
return false ;
}
avcodec_register_all ( ) ;
av_register_all ( ) ;
av_log_set_level ( AV_LOG_FATAL ) ;
//av_log_set_level(AV_LOG_ERROR);
@ -149,25 +141,25 @@ bool DATVideoRender::InitializeFFMPEG()
@@ -149,25 +141,25 @@ bool DATVideoRender::InitializeFFMPEG()
bool DATVideoRender : : PreprocessStream ( )
{
AVDictionary * objOpts = NULL ;
AVCodec * objCodec = NULL ;
AVDictionary * objOpts = nullptr ;
AVCodec * objCodec = nullptr ;
int intRet = - 1 ;
char * objBuffer = NULL ;
char * objBuffer = nullptr ;
//Identify stream
if ( avformat_find_stream_info ( m_objFormatCtx , NULL ) < 0 )
if ( avformat_find_stream_info ( m_objFormatCtx , nullptr ) < 0 )
{
avformat_close_input ( & m_objFormatCtx ) ;
m_objFormatCtx = NULL ;
m_objFormatCtx = nullptr ;
qDebug ( ) < < " DATVideoProcess::PreprocessStream cannot find stream info " ;
return false ;
}
//Find video stream
intRet = av_find_best_stream ( m_objFormatCtx , AVMEDIA_TYPE_VIDEO , - 1 , - 1 , NULL , 0 ) ;
intRet = av_find_best_stream ( m_objFormatCtx , AVMEDIA_TYPE_VIDEO , - 1 , - 1 , nullptr , 0 ) ;
if ( intRet < 0 )
{
@ -191,28 +183,24 @@ bool DATVideoRender::PreprocessStream()
@@ -191,28 +183,24 @@ bool DATVideoRender::PreprocessStream()
MetaData . PID = m_objFormatCtx - > streams [ m_intVideoStreamIndex ] - > id ;
MetaData . CodecID = m_objDecoderCtx - > codec_id ;
MetaData . OK_TransportStream = true ;
MetaData . Program = " " ;
MetaData . Stream = " " ;
if ( m_objFormatCtx - > programs )
{
objBuffer = NULL ;
objBuffer = nullptr ;
av_dict_get_string ( m_objFormatCtx - > programs [ m_intVideoStreamIndex ] - > metadata , & objBuffer , ' : ' , ' \n ' ) ;
if ( objBuffer ! = NULL )
{
if ( objBuffer ! = nullptr ) {
MetaData . Program = QString ( " %1 " ) . arg ( objBuffer ) ;
}
}
objBuffer = NULL ;
objBuffer = nullptr ;
av_dict_get_string ( m_objFormatCtx - > streams [ m_intVideoStreamIndex ] - > metadata , & objBuffer , ' : ' , ' \n ' ) ;
if ( objBuffer ! = NULL )
{
if ( objBuffer ! = nullptr ) {
MetaData . Stream = QString ( " %1 " ) . arg ( objBuffer ) ;
}
@ -220,10 +208,11 @@ bool DATVideoRender::PreprocessStream()
@@ -220,10 +208,11 @@ bool DATVideoRender::PreprocessStream()
//Decoder
objCodec = avcodec_find_decoder ( m_objDecoderCtx - > codec_id ) ;
if ( objCodec = = NULL )
if ( objCodec = = nullptr )
{
avformat_close_input ( & m_objFormatCtx ) ;
m_objFormatCtx = NULL ;
m_objFormatCtx = nullptr ;
qDebug ( ) < < " DATVideoProcess::PreprocessStream cannot find associated CODEC " ;
return false ;
@ -234,7 +223,7 @@ bool DATVideoRender::PreprocessStream()
@@ -234,7 +223,7 @@ bool DATVideoRender::PreprocessStream()
if ( avcodec_open2 ( m_objDecoderCtx , objCodec , & objOpts ) < 0 )
{
avformat_close_input ( & m_objFormatCtx ) ;
m_objFormatCtx = NULL ;
m_objFormatCtx = nullptr ;
qDebug ( ) < < " DATVideoProcess::PreprocessStream cannot open associated CODEC " ;
return false ;
@ -246,21 +235,18 @@ bool DATVideoRender::PreprocessStream()
@@ -246,21 +235,18 @@ bool DATVideoRender::PreprocessStream()
if ( ! m_objFrame )
{
avformat_close_input ( & m_objFormatCtx ) ;
m_objFormatCtx = NULL ;
m_objFormatCtx = nullptr ;
qDebug ( ) < < " DATVideoProcess::PreprocessStream cannot allocate frame " ;
return false ;
}
m_intFrameCount = 0 ;
MetaData . Width = m_objDecoderCtx - > width ;
MetaData . Height = m_objDecoderCtx - > height ;
MetaData . BitRate = m_objDecoderCtx - > bit_rate ;
MetaData . Channels = m_objDecoderCtx - > channels ;
MetaData . CodecDescription = QString ( " %1 " ) . arg ( objCodec - > long_name ) ;
MetaData . OK_VideoStream = true ;
emit onMetaDataChanged ( & MetaData ) ;
@ -271,111 +257,93 @@ bool DATVideoRender::PreprocessStream()
@@ -271,111 +257,93 @@ bool DATVideoRender::PreprocessStream()
bool DATVideoRender : : OpenStream ( DATVideostream * objDevice )
{
int intIOBufferSize = 32768 ;
unsigned char * ptrIOBuffer = NULL ;
AVIOContext * objIOCtx = NULL ;
unsigned char * ptrIOBuffer = nullptr ;
AVIOContext * objIOCtx = nullptr ;
if ( m_blnRunning )
{
if ( m_blnRunning ) {
return false ;
}
if ( objDevice = = NULL )
if ( objDevice = = nullptr )
{
qDebug ( ) < < " DATVideoProcess::OpenStream QIODevice is NULL " ;
qDebug ( ) < < " DATVideoProcess::OpenStream QIODevice is nullptr " ;
return false ;
}
if ( m_blnIsOpen )
if ( m_blnIsOpen )
{
qDebug ( ) < < " DATVideoProcess::OpenStream already open " ;
return false ;
}
if ( objDevice - > bytesAvailable ( ) < = 0 )
if ( objDevice - > bytesAvailable ( ) < = 0 )
{
qDebug ( ) < < " DATVideoProcess::OpenStream no data available " ;
MetaData . OK_Data = false ;
MetaData . OK_Data = false ;
emit onMetaDataChanged ( & MetaData ) ;
return false ;
}
//Only once execution
m_blnRunning = true ;
m_blnRunning = true ;
MetaData . OK_Data = true ;
MetaData . OK_Data = true ;
emit onMetaDataChanged ( & MetaData ) ;
InitializeFFMPEG ( ) ;
if ( ! m_blnIsFFMPEGInitialized )
if ( ! m_blnIsFFMPEGInitialized )
{
qDebug ( ) < < " DATVideoProcess::OpenStream FFMPEG not initialized " ;
m_blnRunning = false ;
m_blnRunning = false ;
return false ;
}
if ( ! objDevice - > open ( QIODevice : : ReadOnly ) )
if ( ! objDevice - > open ( QIODevice : : ReadOnly ) )
{
qDebug ( ) < < " DATVideoProcess::OpenStream cannot open QIODevice " ;
m_blnRunning = false ;
m_blnRunning = false ;
return false ;
}
//Connect QIODevice to FFMPEG Reader
m_objFormatCtx = avformat_alloc_context ( ) ;
if ( m_objFormatCtx = = NULL )
if ( m_objFormatCtx = = nullptr )
{
qDebug ( ) < < " DATVideoProcess::OpenStream cannot alloc format FFMPEG context " ;
m_blnRunning = false ;
m_blnRunning = false ;
return false ;
}
ptrIOBuffer = ( unsigned char * ) av_malloc ( intIOBufferSize + AV_INPUT_BUFFER_PADDING_SIZE ) ;
objIOCtx = avio_alloc_context ( ptrIOBuffer ,
intIOBufferSize ,
0 ,
reinterpret_cast < void * > ( objDevice ) ,
& ReadFunction ,
NULL ,
& SeekFunction ) ;
objIOCtx = avio_alloc_context ( ptrIOBuffer ,
intIOBufferSize ,
0 ,
reinterpret_cast < void * > ( objDevice ) ,
& ReadFunction ,
nullptr ,
& SeekFunction ) ;
m_objFormatCtx - > pb = objIOCtx ;
m_objFormatCtx - > flags | = AVFMT_FLAG_CUSTOM_IO ;
if ( avformat_open_input ( & m_objFormatCtx , NULL , NULL , NULL ) < 0 )
if ( avformat_open_input ( & m_objFormatCtx , nullptr , nullptr , nullptr ) < 0 )
{
qDebug ( ) < < " DATVideoProcess::OpenStream cannot open stream " ;
m_blnRunning = false ;
return false ;
}
if ( ! PreprocessStream ( ) )
if ( ! PreprocessStream ( ) )
{
m_blnRunning = false ;
return false ;
}
m_blnIsOpen = true ;
m_blnRunning = false ;
return true ;
@ -390,12 +358,10 @@ bool DATVideoRender::RenderStream()
@@ -390,12 +358,10 @@ bool DATVideoRender::RenderStream()
if ( ! m_blnIsOpen )
{
qDebug ( ) < < " DATVideoProcess::RenderStream Stream not open " ;
return false ;
}
if ( m_blnRunning )
{
if ( m_blnRunning ) {
return false ;
}
@ -407,7 +373,6 @@ bool DATVideoRender::RenderStream()
@@ -407,7 +373,6 @@ bool DATVideoRender::RenderStream()
if ( av_read_frame ( m_objFormatCtx , & objPacket ) < 0 )
{
qDebug ( ) < < " DATVideoProcess::RenderStream reading packet error " ;
m_blnRunning = false ;
return false ;
}
@ -418,34 +383,31 @@ bool DATVideoRender::RenderStream()
@@ -418,34 +383,31 @@ bool DATVideoRender::RenderStream()
memset ( m_objFrame , 0 , sizeof ( AVFrame ) ) ;
av_frame_unref ( m_objFrame ) ;
intGotFrame = 0 ;
intGotFrame = 0 ;
if ( new_decode ( m_objDecoderCtx , m_objFrame , & intGotFrame , & objPacket ) < 0 )
if ( new_decode ( m_objDecoderCtx , m_objFrame , & intGotFrame , & objPacket ) < 0 )
{
qDebug ( ) < < " DATVideoProcess::RenderStream decoding packet error " ;
m_blnRunning = false ;
return false ;
}
if ( intGotFrame )
if ( intGotFrame )
{
//Rendering and RGB Converter setup
blnNeedRenderingSetup = ( m_intFrameCount = = 0 ) ;
blnNeedRenderingSetup | = ( m_objSwsCtx = = NULL ) ;
blnNeedRenderingSetup | = ( m_objSwsCtx = = nullptr ) ;
if ( ( m_intCurrentRenderWidth ! = m_objFrame - > width ) | | ( m_intCurrentRenderHeight ! = m_objFrame - > height ) )
{
if ( ( m_intCurrentRenderWidth ! = m_objFrame - > width ) | | ( m_intCurrentRenderHeight ! = m_objFrame - > height ) ) {
blnNeedRenderingSetup = true ;
}
if ( blnNeedRenderingSetup )
if ( blnNeedRenderingSetup )
{
if ( m_objSwsCtx ! = NULL )
if ( m_objSwsCtx ! = nullptr )
{
sws_freeContext ( m_objSwsCtx ) ;
m_objSwsCtx = NULL ;
m_objSwsCtx = nullptr ;
}
//Convertisseur YUV -> RGB
@ -461,67 +423,57 @@ bool DATVideoRender::RenderStream()
@@ -461,67 +423,57 @@ bool DATVideoRender::RenderStream()
av_opt_set_int ( m_objSwsCtx , " sws_flag " , SWS_FAST_BILINEAR /* SWS_BICUBIC*/ , 0 ) ;
if ( sws_init_context ( m_objSwsCtx , NULL , NULL ) < 0 )
if ( sws_init_context ( m_objSwsCtx , nullptr , nullptr ) < 0 )
{
qDebug ( ) < < " DATVideoProcess::RenderStream cannont init video data converter " ;
m_objSwsCtx = NULL ;
m_objSwsCtx = nullptr ;
m_blnRunning = false ;
return false ;
}
if ( ( m_intCurrentRenderHeight > 0 ) & & ( m_intCurrentRenderWidth > 0 ) )
if ( ( m_intCurrentRenderHeight > 0 ) & & ( m_intCurrentRenderWidth > 0 ) )
{
//av_freep(&m_pbytDecodedData[0]);
//av_freep(&m_pintDecodedLineSize[0]);
}
if ( av_image_alloc ( m_pbytDecodedData , m_pintDecodedLineSize , m_objFrame - > width , m_objFrame - > height , AV_PIX_FMT_RGB24 , 1 ) < 0 )
if ( av_image_alloc ( m_pbytDecodedData , m_pintDecodedLineSize , m_objFrame - > width , m_objFrame - > height , AV_PIX_FMT_RGB24 , 1 ) < 0 )
{
qDebug ( ) < < " DATVideoProcess::RenderStream cannont init video image buffer " ;
sws_freeContext ( m_objSwsCtx ) ;
m_objSwsCtx = NULL ;
m_objSwsCtx = nullptr ;
m_blnRunning = false ;
return false ;
}
//Rendering device setup
resizeTVScreen ( m_objFrame - > width , m_objFrame - > height ) ;
update ( ) ;
resetImage ( ) ;
resizeTVScreen ( m_objFrame - > width , m_objFrame - > height ) ;
update ( ) ;
resetImage ( ) ;
m_intCurrentRenderWidth = m_objFrame - > width ;
m_intCurrentRenderHeight = m_objFrame - > height ;
m_intCurrentRenderWidth = m_objFrame - > width ;
m_intCurrentRenderHeight = m_objFrame - > height ;
MetaData . Width = m_objFrame - > width ;
MetaData . Height = m_objFrame - > height ;
MetaData . OK_Decoding = true ;
emit onMetaDataChanged ( & MetaData ) ;
MetaData . Width = m_objFrame - > width ;
MetaData . Height = m_objFrame - > height ;
MetaData . OK_Decoding = true ;
emit onMetaDataChanged ( & MetaData ) ;
}
//Frame rendering
if ( sws_scale ( m_objSwsCtx , m_objFrame - > data , m_objFrame - > linesize , 0 , m_objFrame - > height , m_pbytDecodedData , m_pintDecodedLineSize ) < 0 )
if ( sws_scale ( m_objSwsCtx , m_objFrame - > data , m_objFrame - > linesize , 0 , m_objFrame - > height , m_pbytDecodedData , m_pintDecodedLineSize ) < 0 )
{
qDebug ( ) < < " DATVideoProcess::RenderStream error converting video frame to RGB " ;
m_blnRunning = false ;
return false ;
}
renderImage ( m_pbytDecodedData [ 0 ] ) ;
av_frame_unref ( m_objFrame ) ;
m_intFrameCount + + ;
}
}
av_packet_unref ( & objPacket ) ;
@ -536,29 +488,25 @@ bool DATVideoRender::RenderStream()
@@ -536,29 +488,25 @@ bool DATVideoRender::RenderStream()
bool DATVideoRender : : CloseStream ( QIODevice * objDevice )
{
if ( m_blnRunning )
{
if ( m_blnRunning ) {
return false ;
}
if ( ! objDevice )
if ( ! objDevice )
{
qDebug ( ) < < " DATVideoProcess::CloseStream QIODevice is NULL " ;
qDebug ( ) < < " DATVideoProcess::CloseStream QIODevice is nullptr " ;
return false ;
}
if ( ! m_blnIsOpen )
if ( ! m_blnIsOpen )
{
qDebug ( ) < < " DATVideoProcess::CloseStream Stream not open " ;
return false ;
}
if ( ! m_objFormatCtx )
if ( ! m_objFormatCtx )
{
qDebug ( ) < < " DATVideoProcess::CloseStream FFMEG Context is not initialized " ;
return false ;
}
@ -567,32 +515,29 @@ bool DATVideoRender::CloseStream(QIODevice *objDevice)
@@ -567,32 +515,29 @@ bool DATVideoRender::CloseStream(QIODevice *objDevice)
// maybe done in the avcodec_close
// avformat_close_input(&m_objFormatCtx);
// m_objFormatCtx=NULL ;
// m_objFormatCtx=nullptr ;
if ( m_objDecoderCtx )
if ( m_objDecoderCtx )
{
avcodec_close ( m_objDecoderCtx ) ;
m_objDecoderCtx = NULL ;
m_objDecoderCtx = nullptr ;
}
if ( m_objFrame )
if ( m_objFrame )
{
av_frame_unref ( m_objFrame ) ;
av_frame_free ( & m_objFrame ) ;
}
if ( m_objSwsCtx ! = NULL )
if ( m_objSwsCtx ! = nullptr )
{
sws_freeContext ( m_objSwsCtx ) ;
m_objSwsCtx = NULL ;
m_objSwsCtx = nullptr ;
}
objDevice - > close ( ) ;
m_blnIsOpen = false ;
m_blnRunning = false ;
m_intCurrentRenderWidth = - 1 ;
m_intCurrentRenderHeight = - 1 ;
@ -612,19 +557,25 @@ int DATVideoRender::new_decode(AVCodecContext *avctx, AVFrame *frame, int *got_f
@@ -612,19 +557,25 @@ int DATVideoRender::new_decode(AVCodecContext *avctx, AVFrame *frame, int *got_f
* got_frame = 0 ;
if ( pkt ) {
if ( pkt )
{
ret = avcodec_send_packet ( avctx , pkt ) ;
// In particular, we don't expect AVERROR(EAGAIN), because we read all
// decoded frames with avcodec_receive_frame() until done.
if ( ret < 0 )
if ( ret < 0 ) {
return ret = = AVERROR_EOF ? 0 : ret ;
}
}
ret = avcodec_receive_frame ( avctx , frame ) ;
if ( ret < 0 & & ret ! = AVERROR ( EAGAIN ) & & ret ! = AVERROR_EOF )
if ( ret < 0 & & ret ! = AVERROR ( EAGAIN ) & & ret ! = AVERROR_EOF ) {
return ret ;
if ( ret > = 0 )
}
if ( ret > = 0 ) {
* got_frame = 1 ;
}
return 0 ;
}