@ -12,52 +12,34 @@
# define LOG_ERROR( X ) m_pLog->Log( "LIBAV_SRC", this, _LOG_ERROR, X );
# endif
# define TLOG( X ) basicLogCollection::getInstance()->Log( "LIBAV_SRC_T", NULL, X )
# ifdef DEBUG
# define TLOG_ERROR( X ) {\
char _tBfrErrMSG2w34 [ 256 ] ; \
sprintf ( _tBfrErrMSG2w34 , " In file %s[%d] in func:%s %s " , __FILE__ , __LINE__ , __func__ , X ) ; \
basicLogCollection : : getInstance ( ) - > Log ( " LIBAV_SRC_T " , NULL , _LOG_ERROR , _tBfrErrMSG2w34 ) ; \
}
# else
# define TLOG_ERROR( X ) basicLogCollection::getInstance()->Log( "LIBAV_SRC_T", NULL, _LOG_ERROR, X );
# endif
videoAVLIBSource : : videoAVLIBSource ( void * opaque ) : basicMediaSource ( ) , basicThread ( ) {
m_pLog = basicLogCollection : : getInstance ( ) ;
m_bSizeIsAutomatic = true ;
m_nWidth = - 1 ;
m_nHeight = - 1 ;
m_bStreamingSource = true ;
m_bSizeIsAutomatic = false ;
m_nWidth = 640 ;
m_nHeight = 524 ;
m_bSrcType = AVLIB_SOURCE_NOTDEF ;
if ( opaque ) {
m_pOpaque = opaque ;
} else {
m_pOpaque = ( void * ) this ;
} ;
if ( ! initLibAV ( ) ) return ;
if ( ! initLibAV ( ) ) return ;
m_pVideoState = ( VideoState * ) av_mallocz ( sizeof ( VideoState ) ) ;
if ( m_pVideoState = = NULL ) {
if ( m_pVideoState = = NULL ) {
LOG_ERROR ( " VideoState Memory Allocation Error " ) ;
return ;
} ;
m_pVideoState - > m_bStatus = MS_UNDEFINED ;
m_pVideoState - > m_pFormatCtx = avformat_alloc_context ( ) ;
if ( m_pVideoState - > m_pFormatCtx = = NULL ) {
LOG_ERROR ( " VideoFormat Context Memory Allocation Error " ) ;
if ( m_pVideoState - > m_pFormatCtx = = NULL ) {
LOG_ERROR ( " VideoCodec Context Memory Allocation Error " ) ;
return ;
} ;
m_pFrame = NULL ;
# ifdef PROCESS_YUV420P
m_pFrameYUV = NULL ;
# else
m_pFrameBGR = NULL ;
# endif
img_convert_ctx = NULL ;
m_pVideoState - > m_iVideoStreamID = - 1 ;
# ifndef _NOAUDIO_
m_pVideoState - > m_iAudioStreamID = - 1 ;
@ -65,12 +47,13 @@ videoAVLIBSource::videoAVLIBSource( void *opaque ): basicMediaSource(), basicThr
m_pVideoState - > m_bStatus = MS_INIT ;
} ;
videoAVLIBSource : : videoAVLIBSource ( char * Source , void * opaque ) : basicMediaSource ( ) {
videoAVLIBSource : : videoAVLIBSource ( char * Source , void * opaque ) : basicMediaSource ( ) , basicThread ( ) {
m_pLog = basicLogCollection : : getInstance ( ) ;
m_bStreamingSource = true ;
m_bSizeIsAutomatic = true ;
m_nWidth = - 1 ;
m_nHeight = - 1 ;
m_bSizeIsAutomatic = false ;
m_nWidth = 640 ;
m_nHeight = 524 ;
m_bSrcType = AVLIB_SOURCE_NOTDEF ;
if ( opaque ) {
m_pOpaque = opaque ;
} else {
@ -91,7 +74,6 @@ videoAVLIBSource::videoAVLIBSource(char *Source, void *opaque ) : basicMediaSour
LOG_ERROR ( " VideoCodec Context Memory Allocation Error " ) ;
return ;
} ;
m_pVideoState - > m_pFormatCtx - > video_codec_id = AV_CODEC_ID_MJPEG ;
m_pVideoState - > m_iVideoStreamID = - 1 ;
# ifndef _NOAUDIO_
m_pVideoState - > m_iAudioStreamID = - 1 ;
@ -121,18 +103,10 @@ videoAVLIBSource::~videoAVLIBSource(){
avformat_close_input ( & m_pVideoState - > m_pFormatCtx ) ;
//avformat_free_context(m_pVideoState->m_pFormatCtx);
} ;
if ( m_pVideoState - > m_pSwsCtx ! = NULL ) {
sws_freeContext ( m_pVideoState - > m_pSwsCtx ) ;
} ;
av_free ( m_pVideoState ) ;
if ( m_pFrame ! = NULL ) av_frame_free ( & m_pFrame ) ;
# ifdef PROCESS_YUV420P
av_frame_free ( & m_pFrameYUV ) ;
# endif
# ifdef PROCESS_BGR24
if ( m_pFrameBGR ! = NULL ) av_frame_free ( & m_pFrameBGR ) ;
# endif
//av_free( m_pOut_buffer );
if ( img_convert_ctx ! = NULL ) sws_freeContext ( img_convert_ctx ) ;
} ;
} ;
} ;
@ -187,20 +161,9 @@ bool videoAVLIBSource::_close(void){
if ( m_pVideoState - > m_bStatus < MS_OPENED ) return true ;
if ( m_pVideoState - > m_bStatus = = MS_CLOSED ) return true ;
m_pVideoState - > m_bStatus = MS_CLOSING ;
while ( m_pVideoState - > m_bStatus ! = MS_CLOSED ) SLEEP ( 10 ) ;
_stop ( ) ;
if ( m_pVideoState - > m_bStatus > MS_ALOCATED ) {
av_frame_free ( & m_pFrame ) ;
# ifdef PROCESS_YUV420P
av_frame_free ( & m_pFrameYUV ) ;
# else
av_frame_free ( & m_pFrameBGR ) ;
# endif
sws_freeContext ( img_convert_ctx ) ;
# ifndef _NOAUDIO_
if ( m_pVideoState - > m_pAudioCtx ! = NULL ) {
avcodec_close ( m_pVideoState - > m_pAudioCtx ) ;
@ -222,8 +185,7 @@ bool videoAVLIBSource::_close(void){
} ;
bool videoAVLIBSource : : _stop ( void ) {
if ( _close ( ) ) basicThread : : waitThreadEnd ( ) ;
else return false ;
basicThread : : waitThreadEnd ( true ) ;
return true ;
} ;
//https://www.youtube.com/watch?v=Lu3LZmhC_DY
@ -236,10 +198,9 @@ bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, in
char tChar [ 128 ] = { 0 } ;
sprintf ( m_pCookie , " %s " , MD5String ( URI ) ) ;
m_pVideoState - > m_bStatus = MS_OPENING ;
uint8_t bSrcType = AVLIB_SOURCE_NOTDEF ;
if ( strncmp ( URI , " video= " , 6 ) = = 0 ) {
bSrcType = AVLIB_SOURCE_DDSHOW ;
m_ bSrcType = AVLIB_SOURCE_DDSHOW ;
# ifdef DEBUG
LOG ( " MS-Video Device " ) ;
# endif
@ -315,18 +276,18 @@ bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, in
} else {
if ( strncmp ( URI , " /dev/video " , 10 ) = = 0 ) {
bSrcType = AVLIB_SOURCE_V4L2 ;
m_ bSrcType = AVLIB_SOURCE_V4L2 ;
} else {
bSrcType = AVLIB_SOURCE_IPSTREAM ;
m_ bSrcType = AVLIB_SOURCE_IPSTREAM ;
} ;
AVDictionary * options = NULL ;
//av_dict_set(&option s, "movflags", "faststart", 0);
if ( bSrcType = = AVLIB_SOURCE_IPSTREAM ) {
//AVDictionary *options = NULL;
//av_dict_set(&m_pVideoState->m_pFormatOpt s, "movflags", "faststart", 0);
if ( m_ bSrcType = = AVLIB_SOURCE_IPSTREAM ) {
# ifdef DEBUG
LOG ( " IP Stream Source " ) ;
# endif
av_dict_set ( & option s, " rtsp_transport " , " tcp " , 0 ) ;
av_dict_set ( & option s, " stimeout " , " 1 000000" , 0 ) ;
av_dict_set ( & m_pVideoState - > m_pFormatOpt s, " rtsp_transport " , " tcp " , 0 ) ;
av_dict_set ( & m_pVideoState - > m_pFormatOpt s, " stimeout " , " 5 000000" , 0 ) ;
} else {
# ifdef DEBUG
LOG ( " Linux Video Device " ) ;
@ -334,37 +295,39 @@ bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, in
if ( width ! = - 1 & & height ! = - 1 ) {
m_bSizeIsAutomatic = false ;
sprintf ( tChar , " %dx%d " , width , height ) ;
av_dict_set ( & option s, " video_size " , tChar , 0 ) ;
av_dict_set ( & m_pVideoState - > m_pFormatOpt s, " video_size " , tChar , 0 ) ;
} else {
m_bSizeIsAutomatic = true ;
} ;
} ;
av_dict_set ( & options , " max_delay " , " 300000 " , 0 ) ;
av_dict_set ( & options , " fflags " , " nobuffer " , 0 ) ;
av_dict_set ( & m_pVideoState - > m_pFormatOpts , " buffer_size " , " 1024000 " , 0 ) ;
av_dict_set ( & m_pVideoState - > m_pFormatOpts , " max_delay " , " 100 " , 0 ) ;
av_dict_set ( & m_pVideoState - > m_pFormatOpts , " fflags " , " nobuffer " , 0 ) ;
//av_dict_set(&options, "analyzeduration", "1000000", 0);
if ( framerate ! = - 1 ) {
sprintf ( tChar , " %0.2f " , framerate ) ;
//sprintf(tChar, "120" );
av_dict_set ( & option s, " framerate " , tChar , 0 ) ;
av_dict_set ( & m_pVideoState - > m_pFormatOpt s, " framerate " , tChar , 0 ) ;
//av_dict_set(&options, "frame_rate", tChar, 0);
} ;
//av_dict_set(&options, "pixel_format", "mjpeg", 0);
//av_dict_set(&options, "timeout", "8000", 0);
int RetVal = 0 ;
if ( ( RetVal = avformat_open_input ( & m_pVideoState - > m_pFormatCtx , URI , NULL , & options ) ) ! = 0 ) {
av_dict_free ( & options ) ;
strncpy ( m_pVideoState - > m_pFileName , URI , MAX_PATH_LEN ) ;
if ( ( RetVal = avformat_open_input ( & m_pVideoState - > m_pFormatCtx , m_pVideoState - > m_pFileName , NULL , & m_pVideoState - > m_pFormatOpts ) ) ! = 0 ) {
//av_dict_free(&options);
char tBuffer [ 1024 ] ;
av_strerror ( RetVal , tBuffer , 1024 ) ;
sprintf ( Buffer , " VideoSource Couldn't open input stream[%s](Code:%d): %s " , tBuffer , RetVal , URI ) ;
sprintf ( Buffer , " VideoSource Couldn't open input stream[%s](Code:%d): %s " , tBuffer , RetVal , m_pVideoState - > m_pFileName ) ;
LOG_ERROR ( Buffer ) ;
return false ;
} ;
AVDictionaryEntry * e ;
if ( e = av_dict_get ( option s, " " , NULL , AV_DICT_IGNORE_SUFFIX ) ) {
if ( e = av_dict_get ( m_pVideoState - > m_pFormatOpt s, " " , NULL , AV_DICT_IGNORE_SUFFIX ) ) {
sprintf ( Buffer , " Option %s not recognized by the demuxer " , e - > key ) ;
LOG_ERROR ( Buffer ) ;
} ;
av_dict_free ( & options ) ;
//av_dict_free( &e );
} ;
//DataStream Opened !
m_pVideoState - > m_bStatus = MS_OPENED ;
@ -453,8 +416,8 @@ bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, in
and threaded mode ( AVCodecContext : : thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE ) .
Thread pool will be managed by FFmpeg itself , although some answers say that it is also possible using application - provided pool .
*/
m_pVideoState - > m_pCodecCtx - > thread_count = 2 ;
m_pVideoState - > m_pCodecCtx - > thread_type = FF_THREAD_FRAME ;
//m_pVideoState->m_pCodecCtx->thread_count = 2;
//m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
if ( avcodec_open2 ( m_pVideoState - > m_pCodecCtx , m_pVideoState - > m_pVideoCodec , NULL ) < 0 ) {
sprintf ( Buffer , " Could not open Video codec[%d] for %s " , m_pVideoState - > m_pCodecCtx - > codec_id , URI ) ;
@ -478,25 +441,24 @@ bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, in
return false ;
} ;
# endif
m_pFrame = av_frame_alloc ( ) ;
# ifdef PROCESS_YUV420P
m_pFrameYUV = av_frame_alloc ( ) ;
# endif
# ifdef PROCESS_BGR24
m_pFrameBGR = av_frame_alloc ( ) ;
# endif
char tB [ 128 ] ;
# ifdef DEBUG
char tB [ 256 ] ;
sprintf ( tB , " Setting Frame [%dx%d] " , m_nWidth , m_nHeight ) ;
LOG ( tB ) ;
sprintf ( tB , " Source Frame [%dx%d] " , m_pVideoState - > m_pCodecCtx - > width , m_pVideoState - > m_pCodecCtx - > height ) ;
LOG ( tB ) ;
# endif
if ( scaled ) {
# ifdef DEBUG
LOG ( " Size is Scaled " ) ;
# endif
m_nWidth = s_width ;
m_nHeight = s_height ;
} else {
if ( m_bSizeIsAutomatic ) {
# ifdef DEBUG
LOG ( " Size is Automatic " ) ;
# endif
if ( ( m_nWidth = = - 1 ) & & ( m_nHeight = = - 1 ) ) {
m_nWidth = m_pVideoState - > m_pCodecCtx - > width ;
m_nHeight = m_pVideoState - > m_pCodecCtx - > height ;
@ -511,7 +473,9 @@ bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, in
} ;
} ;
} else {
# ifdef DEBUG
LOG ( " Size is Manual " ) ;
# endif
if ( width > 0 ) {
m_nWidth = width ;
} ;
@ -520,90 +484,87 @@ bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, in
} ;
} ;
} ;
sprintf ( tB , " Actual Frame [%dx%d] " , m_nWidth , m_nHeight ) ;
# ifdef DEBUG
sprintf ( tB , " Using Size [%dx%d] " , m_nWidth , m_nHeight ) ;
LOG ( tB ) ;
# ifdef PROCESS_YUV420P
m_pOut_buffer = ( uint8_t * ) av_malloc ( av_image_get_buffer_size ( AV_PIX_FMT_YUV420P , m_nWidth , m_nHeight , BUFFER_ALIGN ) ) ;
//avpicture_fill((AVPicture *) m_pFrameYUV, m_pOut_buffer, AV_PIX_FMT_YUV420P, m_nWidth, m_nHeight );
av_image_fill_arrays ( m_pFrameYUV - > data , m_pFrameYUV - > linesize , m_pOut_buffer , AV_PIX_FMT_YUV420P , m_nWidth , m_nHeight , BUFFER_ALIGN ) ;
m_pFrameYUV - > width = m_nWidth ;
m_pFrameYUV - > height = m_nHeight ;
# endif
# ifdef PROCESS_BGR24
m_pOut_buffer = ( uint8_t * ) av_malloc ( av_image_get_buffer_size ( AV_PIX_FMT_BGR24 , m_nWidth , m_nHeight , BUFFER_ALIGN ) ) ;
if ( m_pOut_buffer = = NULL ) return false ;
//avpicture_fill((AVPicture *)m_pFrameBGR, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight);
av_image_fill_arrays ( m_pFrameBGR - > data , m_pFrameBGR - > linesize , m_pOut_buffer , AV_PIX_FMT_BGR24 , m_nWidth , m_nHeight , BUFFER_ALIGN ) ;
m_pFrameBGR - > width = m_nWidth ;
m_pFrameBGR - > height = m_nHeight ;
//m_pMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) );
//m_pOMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) );
# endif
m_pPacket = ( AVPacket * ) av_malloc ( sizeof ( AVPacket ) ) ;
if ( m_pPacket = = NULL ) return false ;
// Printout FF
//av_dump_format(m_pFormatCtx,0,filepath,0);
# ifdef PROCESS_YUV420P
//Col Depth 12bpp
img_convert_ctx = sws_getContext ( m_pVideoState - > m_pCodecCtx - > width , m_pVideoState - > m_pCodecCtx - > height , m_pVideoState - > m_pCodecCtx - > pix_fmt , m_nWidth , m_nHeight , AV_PIX_FMT_YUV420P , SWS_BICUBIC , NULL , NULL , NULL ) ;
if ( img_convert_ctx = = NULL ) return false ;
//Col Depth 16bpp
//img_convert_ctx = sws_getContext(m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height, m_pVideoState->m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_YUYV422, SWS_BICUBIC, NULL, NULL, NULL);
# endif
# ifdef PROCESS_BGR24
img_convert_ctx = sws_getContext (
sprintf ( tB , " swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d} " , m_pVideoState - > m_pCodecCtx - > width ,
m_pVideoState - > m_pCodecCtx - > height ,
m_pVideoState - > m_pCodecCtx - > pix_fmt ,
m_nWidth ,
m_nHeight ,
AV_PIX_FMT_BGR24 ,
SWS_BILINEAR ) ;
LOG ( tB ) ;
# endif
m_pVideoState - > m_pSwsCtx = sws_getContext (
m_pVideoState - > m_pCodecCtx - > width ,
m_pVideoState - > m_pCodecCtx - > height ,
m_pVideoState - > m_pCodecCtx - > pix_fmt ,
m_nWidth ,
m_nHeight ,
AV_PIX_FMT_BGR24 ,
SWS_BICUBIC ,
SWS_FAST_BILINEAR | SWS_ACCURATE_RND , //SWS_BILINEAR,//SWS_BICUBIC,
NULL ,
NULL ,
NULL
) ;
if ( img_convert_ctx = = NULL ) return false ;
//img_convert_ctx = sws_getContext(m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height, m_pVideoState->m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
# endif
m_pVideoState - > m_bStatus = MS_ALOCATED ; // All seems to be good -> allocating mem
if ( m_pVideoState - > m_pSwsCtx = = NULL ) {
LOG_ERROR ( " Error Allocating SwsCtx! " ) ;
return false ;
} ;
# ifdef DEBUG
LOG ( " Allocation Done->Start Thread! " ) ;
# endif
return init ( ) ;
} ;
return false ;
} ;
# if !(( LIBAVCODEC_VERSION_MAJOR < 58 ) /* && ( LIBAVCODEC_VERSION_MINOR < 50 ) */ )
// The flush packet is a non-NULL packet with size 0 and data NULL
int decode ( AVCodecContext * avctx , AVFrame * frame , int * got_frame , AVPacket * pkt ) {
int ret ;
* got_frame = 0 ;
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 ) return ret = = AVERROR_EOF ? 0 : ret ;
} ;
ret = avcodec_receive_frame ( avctx , frame ) ;
if ( ret < 0 & & ret ! = AVERROR ( EAGAIN ) & & ret ! = AVERROR_EOF ) return ret ;
if ( ret > = 0 ) * got_frame = 1 ;
return 0 ;
} ;
# endif
void videoAVLIBSource : : m_fThreadProc ( void ) {
int RetVal ;
int bGotPicture = 0 ;
# ifdef _OPENCV_
cv : : Mat mRGB ( m_nHeight , m_nWidth , CV_8UC3 ) ;
# endif
m_bThreadStatus = TH_WORKING ;
AVPacket * pPacket = NULL ;
cv : : Mat mRGB ( m_nHeight , m_nWidth , CV_8UC3 ) ;
AVFrame * pFrame = NULL ;
AVFrame * pFrameBGR = NULL ;
uint8_t * pBGRbuffer = NULL ;
int dec_ret ;
char tB [ 256 ] ;
pPacket = ( AVPacket * ) av_malloc ( sizeof ( AVPacket ) ) ;
av_init_packet ( pPacket ) ;
if ( pPacket = = NULL ) {
m_bThreadStatus = TH_ENDED ;
LOG_ERROR ( " Error allocating AVPacket! " ) ;
} else {
pFrame = av_frame_alloc ( ) ;
if ( pFrame = = NULL ) {
m_bThreadStatus = TH_ENDED ;
LOG_ERROR ( " Error allocating SRC AVFrame! " ) ;
} else {
pFrameBGR = av_frame_alloc ( ) ;
if ( pFrameBGR = = NULL ) {
m_bThreadStatus = TH_ENDED ;
LOG_ERROR ( " Error allocating BGR AVFrame! " ) ;
} else {
pBGRbuffer = ( uint8_t * ) av_malloc ( av_image_get_buffer_size ( AV_PIX_FMT_BGR24 , m_nWidth , m_nHeight , BUFFER_ALIGN ) ) ;
if ( pBGRbuffer = = NULL ) {
m_bThreadStatus = TH_ENDED ;
LOG_ERROR ( " Error allocating BGR AVFrame Buffer! " ) ;
} else {
av_image_fill_arrays ( pFrameBGR - > data , pFrameBGR - > linesize , pBGRbuffer , AV_PIX_FMT_BGR24 , m_nWidth , m_nHeight , BUFFER_ALIGN ) ;
pFrameBGR - > width = m_nWidth ;
pFrameBGR - > height = m_nHeight ;
m_pVideoState - > m_bStatus = MS_ALOCATED ;
m_bThreadStatus = TH_WORKING ;
# ifdef DEBUG_EXTRA
LOG ( " Reader Thread Started. " ) ;
LOG ( " Video Reader Thread Started." ) ;
# endif
//cv::setBreakOnError( true );
} ;
} ;
} ;
} ;
//cv::setBreakOnError( true );
m_pVideoState - > m_bStatus = MS_STOPED ;
while ( m_pMediaDestination = = NULL & & m_bThreadStatus = = TH_WORKING ) {
SLEEP ( 20 ) ;
@ -611,90 +572,91 @@ void videoAVLIBSource::m_fThreadProc( void ){
LOG ( " Waiting Destination " ) ;
# endif
} ;
m_pVideoState - > m_bStatus = MS_PRESENT ;
if ( m_bSrcType = = AVLIB_SOURCE_IPSTREAM ) {
//av_read_play( m_pVideoState->m_pFormatCtx );
} ;
# ifdef DEBUG
LOG ( " Presentation Started. " ) ;
# endif
m_pVideoState - > m_bStatus = MS_PRESENT ;
av_read_play ( m_pVideoState - > m_pFormatCtx ) ;
while ( m_pVideoState - > m_bStatus < MS_CLOSING & & m_bThreadStatus ! = TH_ENDED ) {
if ( m_pMediaDestination ! = NULL ) {
if ( av_read_frame ( m_pVideoState - > m_pFormatCtx , m_pPacket ) > = 0 ) {
if ( m_pPacket - > stream_index = = m_pVideoState - > m_iVideoStreamID ) {
int dec_ret = avcodec_send_packet ( m_pVideoState - > m_pCodecCtx , m_pPacket ) ;
if ( dec_ret < 0 ) {
LOG_ERROR ( " Error decoding video frame " ) ;
dec_ret = av_read_frame ( m_pVideoState - > m_pFormatCtx , pPacket ) ;
if ( dec_ret > = 0 ) {
if ( pPacket - > stream_index = = m_pVideoState - > m_iVideoStreamID ) {
m_pVideoState - > m_bStatus = MS_PRESENT ;
dec_ret = avcodec_send_packet ( m_pVideoState - > m_pCodecCtx , pPacket ) ;
if ( dec_ret < 0 & & dec_ret ! = AVERROR ( EAGAIN ) /* && dec_ret != AVERROR_EOF */ ) {
LOG_ERROR ( " Error sending video package to decoder " ) ;
m_pVideoState - > m_bStatus = MS_CLOSING ;
m_bThreadStatus = TH_ENDED ;
} else {
while ( dec_ret > = 0 & & m_bThreadStatus ! = TH_ENDED ) {
dec_ret = avcodec_receive_frame ( m_pVideoState - > m_pCodecCtx , m_ pFrame ) ;
dec_ret = avcodec_receive_frame ( m_pVideoState - > m_pCodecCtx , pFrame ) ;
if ( dec_ret = = AVERROR ( EAGAIN ) | | dec_ret = = AVERROR_EOF ) {
continue ; //Skip to next data
break ; //Skip to next data
} else if ( dec_ret < 0 ) {
LOG_ERROR ( " Error in video decoding " ) ;
m_pVideoState - > m_bStatus = MS_STOPED ;
LOG_ERROR ( " Error decoding video frame " ) ;
m_pVideoState - > m_bStatus = MS_CLOSING ;
m_bThreadStatus = TH_ENDED ;
break ;
} ;
# ifdef PROCESS_YUV420P
sws_scale ( img_convert_ctx , ( const uint8_t * const * ) m_pFrame - > data , m_pFrame - > linesize , 0 , m_pVideoState - > m_pCodecCtx - > height , m_pFrameYUV - > data , m_pFrameYUV - > linesize ) ;
//SDL_UpdateYUVTexture(m_pTexture, &m_oRect, m_pFrameYUV->data[0], m_pFrameYUV->linesize[0], m_pFrameYUV->data[1], m_pFrameYUV->linesize[1], m_pFrameYUV->data[2], m_pFrameYUV->linesize[2]);
m_pMediaDestination - > pushFrame ( m_pFrameYUV , m_pOpaque ) ;
# else // PROCESS_BGR24
sws_scale ( img_convert_ctx , ( const uint8_t * const * ) m_pFrame - > data , m_pFrame - > linesize , 0 , m_pVideoState - > m_pCodecCtx - > height , m_pFrameBGR - > data , m_pFrameBGR - > linesize ) ;
sws_scale ( m_pVideoState - > m_pSwsCtx , ( const uint8_t * const * ) pFrame - > data , pFrame - > linesize , 0 , m_pVideoState - > m_pCodecCtx - > height , pFrameBGR - > data , pFrameBGR - > linesize ) ;
try {
# ifdef USE_QVideoFrame
QVideoFrame * tFrame ;
tFrame = new QVideoFrame ( m_pFrameBGR - > linesize [ 0 ] * m_nHeight , QSize ( m_nHeight , m_nWidth ) , m_pFrameBGR - > linesize [ 0 ] , QVideoFrame : : Format_BGR24 ) ;
memcpy ( tFrame - > bits ( ) , m_pFrameBGR - > data [ 0 ] , m_pFrameBGR - > linesize [ 0 ] * m_nHeight ) ;
if ( m_pMediaDestination ! = NULL ) m_pMediaDestination - > pushFrame ( tFrame , m_pOpaque ) ;
# else
# ifdef _OPENCV_
memcpy ( mRGB . data , m_pFrameBGR - > data [ 0 ] , m_pFrameBGR - > linesize [ 0 ] * m_nHeight ) ;
# endif
# ifdef _SDL_
SDL_Surface * mRGB = SDL_CreateRGBSurfaceFrom ( m_pFrameBGR - > data [ 0 ] , m_nHeight , m_nWidth , int depth , Uint32 Rmask , Uint32 Gmask , Uint32 Bmask , Uint32 Amask ) ;
# endif
memcpy ( mRGB . data , pFrameBGR - > data [ 0 ] , pFrameBGR - > linesize [ 0 ] * m_nHeight ) ;
m_pMediaDestination - > pushFrame ( mRGB , m_pOpaque ) ;
# ifdef _SDL_
SDL_FreeSurface ( mRGB ) ;
# endif
# ifdef _OPENCV_
//delete(mRGB);
# endif
# endif // !USE_QVideoFrame
} catch ( cv : : Exception & e ) {
LOG_ERROR ( e . msg . c_str ( ) ) ; // output exception message
LOG_ERROR ( e . msg . c_str ( ) ) ;
} ;
# endif
//Unref Frame
//av_freep( m_pFrame->data[0] );
//av_frame_unref( m_pFrame );
} ;
} ;
} ;
} else {
m_pVideoState - > m_bStatus = MS_CLOSED ;
} else if ( dec_ret ! = AVERROR ( EAGAIN ) & & dec_ret ! = AVERROR_EOF ) {
LOG_ERROR ( " Error reading video package " ) ;
char tBuffer [ 1024 ] ;
av_strerror ( dec_ret , tBuffer , 1024 ) ;
LOG_ERROR ( tBuffer ) ;
m_pVideoState - > m_bStatus = MS_CLOSING ;
m_bThreadStatus = TH_ENDED ;
} else {
if ( dec_ret = = AVERROR_EOF ) {
avformat_close_input ( & m_pVideoState - > m_pFormatCtx ) ;
if ( ( dec_ret = avformat_open_input ( & m_pVideoState - > m_pFormatCtx , m_pVideoState - > m_pFileName , NULL , & m_pVideoState - > m_pFormatOpts ) ) ! = 0 ) {
char tBuffer [ 1024 ] ;
av_strerror ( dec_ret , tBuffer , 1024 ) ;
sprintf ( tB , " VideoSource Couldn't open input stream[%s](Code:%d): %s " , tBuffer , dec_ret , m_pVideoState - > m_pFileName ) ;
LOG_ERROR ( tB ) ;
m_pVideoState - > m_bStatus = MS_CLOSING ;
m_bThreadStatus = TH_ENDED ;
} ;
} ;
//m_pVideoState->m_bStatus = MS_STOPED;
SLEEP ( 5 ) ;
} ;
# if ( LIBAVCODEC_VERSION_MAJOR < 58 ) //&& ( LIBAVCODEC_VERSION_MINOR < 50 )
av_free_packet ( m_pPacket ) ;
# else
av_packet_unref ( m_pPacket ) ;
# endif
av_packet_unref ( pPacket ) ;
} else SLEEP ( 20 ) ;
} ;
m_pVideoState - > m_bStatus = MS_CLOSED ;
if ( pFrame ! = NULL ) {
av_frame_free ( & pFrame ) ;
} ;
if ( pPacket ! = NULL ) {
av_free ( pPacket ) ;
} ;
if ( pBGRbuffer ! = NULL ) {
av_free ( pBGRbuffer ) ;
} ;
m_pVideoState - > m_bStatus = MS_CLOSED ;
m_bThreadStatus = TH_ENDED ;
# ifdef DEBUG
LOG ( " Video Source Ended! " ) ;
# endif
} ;
/*
JPEG2000 isn ' t jpeg . To encode JPEG images , use AV_CODEC_ID_MJPEG . MJPEG stands for " motion JPEG " , which is how a sequence of JPEG pictures making up a video stream is typically called .
int save_frame_as_jpeg ( AVCodecContext * pCodecCtx , AVFrame * pFrame , int FrameNo ) {
int save_frame_as_jpeg ( AVCodecContext * pCodecCtx , AVFrame * pFrame , int FrameNo ) {
AVCodec * jpegCodec = avcodec_find_encoder ( AV_CODEC_ID_JPEG2000 ) ;
if ( ! jpegCodec ) {
return - 1 ;
@ -733,21 +695,16 @@ int save_frame_as_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo)
}
*/
MediaStateStatus videoAVLIBSource : : getStatus ( void ) {
if ( m_pVideoState ) return m_pVideoState - > m_bStatus ;
return MS_UNDEFINED ;
} ;
bool videoAVLIBSource : : isFinished ( void ) {
if ( m_pVideoState ) {
char tT [ 64 ] ;
LOG ( " VideoState OK " ) ;
sprintf ( tT , " VS=%d ? 8 " , m_pVideoState - > m_bStatus ) ;
LOG ( tT ) ;
return ( m_pVideoState - > m_bStatus = = MS_CLOSED ) ;
} else {
LOG ( " No VideoState Object! " ) ;
} ;
if ( m_pVideoState ) if ( m_pVideoState - > m_bStatus < MS_CLOSING ) return false ;
return true ;
} ;
# undef TLOG
# undef TLOG_ERROR
# undef LOG
# undef LOG_ERROR