|
|
#include "videoAVLIBSource.h"
#include "libavutil/error.h"
#define LOG( X ) m_pLog->Log( "LibAV_SRC", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG2w34[256];\ sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ m_pLog->Log( "LIBAV_SRC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ }#else
#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;
if( opaque ) { m_pOpaque = opaque; } else { m_pOpaque = (void *)this; }; if (!initLibAV()) return;
m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState)); 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"); 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;#endif
m_pVideoState->m_bStatus = MS_INIT;};
videoAVLIBSource::videoAVLIBSource(char *Source, void *opaque ) : basicMediaSource(){ m_pLog = basicLogCollection::getInstance(); m_bStreamingSource = true; m_bSizeIsAutomatic = true; m_nWidth = -1; m_nHeight = -1; if( opaque ) { m_pOpaque = opaque; } else { m_pOpaque = (void *)this; }; if( !initLibAV() ) return;
m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState)); 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("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;#endif
m_pVideoState->m_bStatus = MS_INIT; _open( Source );};
videoAVLIBSource::~videoAVLIBSource(){ if (m_pVideoState != NULL){ if( m_pVideoState->m_bStatus > MS_INIT ){ m_bThreadStatus = TH_ENDED; while (m_pVideoState->m_bStatus == MS_PRESENT) SLEEP( 10 );
#ifndef _NOAUDIO_
if( m_pVideoState->m_pAudioCtx!=NULL ){ avcodec_close( m_pVideoState->m_pAudioCtx ); //avcodec_free_context(&m_pVideoState->m_pAudioCtx);
};#endif
if( m_pVideoState->m_pCodecCtx != NULL ){ avcodec_close(m_pVideoState->m_pCodecCtx); //avcodec_free_context(&m_pVideoState->m_pCodecCtx);
}; if( m_pVideoState->m_pFormatCtx != NULL ){ avformat_close_input(&m_pVideoState->m_pFormatCtx); //avformat_free_context(m_pVideoState->m_pFormatCtx);
};
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); }; };};
bool videoAVLIBSource::setSize( int Width, int Height ){ if( m_pVideoState ){ if( m_pVideoState->m_bStatus == MS_INIT ) { if( Width && Height ) { m_bSizeIsAutomatic = false; m_nWidth = Width; m_nHeight = Height; return true; }; }; }; return false;};
bool videoAVLIBSource::setProportionalByWidth(int Width){ if( m_pVideoState ){ if( m_pVideoState->m_bStatus == MS_INIT ) { if( Width > 0 ) { m_bSizeIsAutomatic = true; m_nWidth = Width; return true; }; }; }; return false;};
bool videoAVLIBSource::setProportionalByHeight(int Height){ if( m_pVideoState ){ if( m_pVideoState->m_bStatus == MS_INIT ) { if( Height > 0 ) { m_bSizeIsAutomatic = true; m_nHeight = Height; return true; }; }; }; return false;};
bool videoAVLIBSource::setFrameAspectRatio( FrameAspectRatio AspectRatio ){ return true;};
bool videoAVLIBSource::_close(void){ if (m_pVideoState == NULL) return true; 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);
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); //avcodec_free_context(&m_pVideoState->m_pAudioCtx);
};#endif
if (m_pVideoState->m_pCodecCtx != NULL){ avcodec_close(m_pVideoState->m_pCodecCtx); //avcodec_free_context(&m_pVideoState->m_pCodecCtx);
avformat_close_input(&m_pVideoState->m_pFormatCtx); //av_free(m_pVideoState->m_pVideoCodec);
m_pVideoState->m_iVideoStreamID = -1; };
};
m_pVideoState->m_bStatus = MS_INIT; return true;};
bool videoAVLIBSource::_stop(void){ if ( _close() ) basicThread::waitThreadEnd(); else return false; return true;};//https://www.youtube.com/watch?v=Lu3LZmhC_DY
bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, bool scaled, int s_width, int s_height ){ if (m_pVideoState == NULL) return false; if (m_pVideoState->m_bStatus == MS_INIT){ char Buffer[1024] = {0}; 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;#ifdef DEBUG
LOG("MS-Video Device");#endif
AVDictionary *options = NULL; if( width != -1 && height != -1 ){ m_bSizeIsAutomatic = false; char tChar[10]; sprintf(tChar, "%dx%d", width, height); av_dict_set(&options, "video_size", tChar, 0); } else { m_bSizeIsAutomatic = true; }; //av_dict_set(&options, "pixel_format", "yuyv422", 0 );
av_dict_set(&options, "video_codec", "mjpeg", 0 ); if( inputPin != -1 ){ sprintf(tChar, "%d", inputPin); av_dict_set(&options, "crossbar_video_input_pin_number", tChar, 0); }; if( framerate != -1 ){ sprintf(tChar, "%f", framerate); av_dict_set(&options, "framerate", tChar, 0); }; av_dict_set(&options, "rtbufsize", "500000k", 0); if( ShowDeviceMenu ){ av_dict_set(&options, "show_video_device_dialog", "true", 0); av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0); //sprintf(Buffer, "data/%s.settings", m_pCookie);
//av_dict_set(&options, "video_device_save", Buffer, 0);
#ifndef _NOAUDIO_
av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0); sprintf(Buffer, "%s-a.settings", URI); av_dict_set(&options, "audio_device_save", Buffer, 0);#endif
} else { sprintf(Buffer, "data/%s.settings", m_pCookie ); //if (!fileExist(Buffer)) av_dict_set(&options, "video_device_load", Buffer, 0);
}; //av_dict_set(&options, "crossbar_video_input_pin_number", "1", 0);
//av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
//av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
AVInputFormat *iformat = av_find_input_format("dshow"); //av_dict_set(&options, "pixel_format", "bgr24", 0);
av_dict_set(&options, "timeout", "5000", 0); //AVDeviceInfoList *device_list;
//m_pVideoState->m_pFormatCtx->iformat = iformat;
//avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
//avdevice_free_list_devices(&device_list);
if( avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, iformat, &options) != 0 ){ av_dict_free(&options); //av_free( iformat );
sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI); LOG_ERROR(Buffer); m_pVideoState->m_bStatus = MS_INIT; return false; };
/*AVDictionaryEntry *e;
if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) { sprintf(Buffer, "Option %s not recognized by the demuxer", e->key); LOG_ERROR(Buffer); };*/ //if (iformat!=NULL)
//if (iformat->priv_class != NULL){
// const AVOption *cOption = NULL;
// while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
// serializeOption(cOption);
//};
av_dict_free(&options); AVOption *mpOption = NULL;
} else { if( strncmp( URI, "/dev/video", 10 ) == 0 ) { bSrcType = AVLIB_SOURCE_V4L2; } else { bSrcType = AVLIB_SOURCE_IPSTREAM; }; AVDictionary *options = NULL; //av_dict_set(&options, "movflags", "faststart", 0);
if( bSrcType == AVLIB_SOURCE_IPSTREAM ) {#ifdef DEBUG
LOG("IP Stream Source");#endif
av_dict_set(&options, "rtsp_transport", "tcp", 0); av_dict_set(&options, "stimeout", "1000000", 0); } else {#ifdef DEBUG
LOG("Linux Video Device");#endif
if( width != -1 && height != -1 ){ m_bSizeIsAutomatic = false; sprintf(tChar, "%dx%d", width, height); av_dict_set(&options, "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(&options, "analyzeduration", "1000000", 0);
if( framerate != -1 ){ sprintf(tChar, "%0.2f", framerate); //sprintf(tChar, "120" );
av_dict_set(&options, "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); char tBuffer[1024]; av_strerror( RetVal, tBuffer, 1024 ); sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, URI); LOG_ERROR(Buffer); return false; }; AVDictionaryEntry *e; if( e = av_dict_get( options, "", NULL, AV_DICT_IGNORE_SUFFIX )){ sprintf(Buffer, "Option %s not recognized by the demuxer", e->key); LOG_ERROR(Buffer); }; av_dict_free(&options); }; //DataStream Opened !
m_pVideoState->m_bStatus = MS_OPENED; if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){ sprintf(Buffer, "Couldn't find stream information: %s", URI); LOG_ERROR(Buffer); return false; };
#if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){ //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){ LOG( "VIDEO stream detected." ); if( m_pVideoState->m_iVideoStreamID <0 ){ LOG( "VIDEO stream assigned." ); m_pVideoState->m_iVideoStreamID=i; }; };# ifndef _NOAUDIO_
if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) { LOG( "AUDIO stream detected." ); if( m_pVideoState->m_iAudioStreamID < 0 ) { LOG( "AUDIO stream assigned." ); m_pVideoState->m_iAudioStreamID=i; } };# endif
};
if( m_pVideoState->m_iVideoStreamID == -1 ){ LOG_ERROR( "Didn't find a video stream."); return false; };
# ifndef _NOAUDIO_
if( m_pVideoState->m_iAudioStreamID == -1 ){ LOG_ERROR("Didn't find a audio stream."); return false; };# endif
// Stream found
m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec; //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );#else
//LOG("YABABA-2!");
/* find the video stream information */ m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
if (m_pVideoState->m_iVideoStreamID == -1){ LOG_ERROR("Didn't find a video stream."); return false; };
# ifndef _NOAUDIO_
if (m_pVideoState->m_iAudioStreamID == -1){ LOG_ERROR("Didn't find a audio stream."); return false; };# endif
if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){ LOG_ERROR("Can not create decoder context!"); return false; };
m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]; if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){ LOG_ERROR("Can not set Context parameters!"); return false; };
#endif
if( m_pVideoState->m_pVideoCodec == NULL ){ //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI); LOG_ERROR( Buffer ); return false; };
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 ); LOG_ERROR( Buffer ); return false; };
/* we do not send complete frames */ if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
#ifndef _NOAUDIO_
m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec; m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id ); if( !m_pVideoState->m_pAudioCodec ){ LOG_ERROR( "Unsupported Audio codec!" ); return false; }; if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){ //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
LOG_ERROR( "Could not open Audio codec." ); 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]; 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 ); if( scaled ){ LOG("Size is Scaled"); m_nWidth = s_width; m_nHeight = s_height; } else { if( m_bSizeIsAutomatic ){ LOG("Size is Automatic"); if(( m_nWidth == -1 )&&( m_nHeight == -1 )){ m_nWidth = m_pVideoState->m_pCodecCtx->width; m_nHeight = m_pVideoState->m_pCodecCtx->height; } else { // Proportional
if( m_nHeight == -1 ){ float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width); m_nHeight = (int)(((float)m_nWidth ) * k ); } else { float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height); m_nWidth = (int)(((float)m_nHeight ) * k ); }; }; } else { LOG("Size is Manual"); if( width > 0 ){ m_nWidth = width; }; if( height > 0 ){ m_nHeight = height; }; }; }; sprintf( tB, "Actual Frame [%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, 1); 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 )); 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, 1);
//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) ); // 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 ); //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( 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, NULL, NULL, NULL ); //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_bThreadStatus < TH_WORKING ) 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;#ifdef DEBUG_EXTRA
LOG("Reader Thread Started.");#endif
//cv::setBreakOnError( true );
m_pVideoState->m_bStatus = MS_STOPED; while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){ SLEEP(20); };#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" ); 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 ); if( dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF ){ continue;//Skip to next data
} else if( dec_ret < 0 ){ LOG_ERROR("Error in video decoding"); m_pVideoState->m_bStatus = MS_STOPED; 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 ); 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
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
}; #endif
//Unref Frame
//av_freep( m_pFrame->data[0] );
//av_frame_unref( m_pFrame );
}; }; }; } else { m_pVideoState->m_bStatus = MS_CLOSED; m_bThreadStatus = TH_ENDED; };#if ( LIBAVCODEC_VERSION_MAJOR < 58 ) //&& ( LIBAVCODEC_VERSION_MINOR < 50 )
av_free_packet( m_pPacket );#else
av_packet_unref( m_pPacket );#endif
} else SLEEP( 20 ); }; m_pVideoState->m_bStatus = MS_CLOSED; m_bThreadStatus = TH_ENDED;};
/*
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) { AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000); if (!jpegCodec) { return -1; } AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec); if (!jpegContext) { return -1; }
jpegContext->pix_fmt = pCodecCtx->pix_fmt; jpegContext->height = pFrame->height; jpegContext->width = pFrame->width;
if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) { return -1; } FILE *JPEGFile; char JPEGFName[256];
AVPacket packet = {.data = NULL, .size = 0}; av_init_packet(&packet); int gotFrame;
if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) { return -1; }
sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo); JPEGFile = fopen(JPEGFName, "wb"); fwrite(packet.data, 1, packet.size, JPEGFile); fclose(JPEGFile);
av_free_packet(&packet); avcodec_close(jpegContext); return 0;}*/
#undef TLOG
#undef TLOG_ERROR
#undef LOG
#undef LOG_ERROR
|