|
|
#include "video_in_LIBAV.h"
static bool _FFD_REGISTER_ALL_FLAG = false;
void debugout(const char* szFormat, ...){ char szBuff[1024]; va_list arg; va_start(arg, szFormat); snprintf(szBuff, sizeof(szBuff), szFormat, arg); va_end(arg); printf( szBuff );};
bool _LIBAV_VideoInput::initLBAV( void ){ m_bSizeIsAutomatic = true; //m_sLastError = (char *) malloc( 1024 ); // For some text;
if( !_FFD_REGISTER_ALL_FLAG ){ _FFD_REGISTER_ALL_FLAG = true; //Init FFMPEG
av_register_all(); avdevice_register_all(); avformat_network_init(); }; m_pVideoState = ( VideoState* ) av_mallocz( sizeof( VideoState ) ); if( m_pVideoState == NULL ){ //SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "_VideoInputFFD State Memory Allocation Error" );
//exit( ERR_CRASH_MEMORY_NOT_ALLOCATED );
return false; }; m_pVideoState->m_pFormatCtx = avformat_alloc_context(); if( m_pVideoState->m_pFormatCtx == NULL ){ //SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "_VideoInputFFD Context Memory Allocation Error" );
//exit( ERR_CRASH_MEMORY_NOT_ALLOCATED );
return false; }; m_pVideoState->m_iVideoStreamID = -1;#ifndef _NOAUDIO_
m_pVideoState->m_iAudioStreamID = -1;#endif
m_pVideoState->m_bStatus = 0; return true;};
_LIBAV_VideoInput::_LIBAV_VideoInput(){ m_bStatus = VI_CREATED; if( initLBAV() ) m_bStatus = VI_INIT_OK;};
_LIBAV_VideoInput::_LIBAV_VideoInput( char *Source ){ m_bStatus = VI_CREATED; if( initLBAV() ) m_bStatus = VI_INIT_OK; openSource( Source );};
_LIBAV_VideoInput::~_LIBAV_VideoInput(){ if( m_pVideoState->m_bStatus == 1 ){ // free all mems
av_frame_free( &m_pFrame );#ifdef PROCESS_YUV420P
av_frame_free( &m_pFrameYUV );#endif
#ifdef PROCESS_BGR24
av_frame_free( &m_pFrameBGR );#endif
av_free( m_pOut_buffer ); sws_freeContext( img_convert_ctx ); }; if( m_pVideoState!= NULL ){#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_pVideoCtx!=NULL ){ avcodec_close( m_pVideoState->m_pVideoCtx ); avcodec_free_context( &m_pVideoState->m_pVideoCtx ); }; if( m_pVideoState->m_pFormatCtx!=NULL ){ avformat_close_input( &m_pVideoState->m_pFormatCtx ); avformat_free_context( m_pVideoState->m_pFormatCtx ); }; av_free( m_pVideoState ); }; //free( m_sLastError );
};
bool _LIBAV_VideoInput::openSource( char *URI ){ // If there is no pened streams
if( m_pVideoState->m_bStatus==0 ){ if( avformat_open_input( &m_pVideoState->m_pFormatCtx, URI, NULL, NULL ) != 0 ){ //SDL_Log( "_VideoInputFFD Couldn't open input stream %s", URI );
return false; }; if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){ //SDL_Log( "Couldn't find stream information.");
return false; }; for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){ if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO && m_pVideoState->m_iVideoStreamID <0 ){ m_pVideoState->m_iVideoStreamID=i; };#ifndef _NOAUDIO_
if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && m_pVideoState->m_iAudioStreamID < 0) { m_pVideoState->m_iAudioStreamID=i; };#endif
}; if( m_pVideoState->m_iVideoStreamID == -1 ){ //SDL_Log( "Didn't find a video stream.");
return false; };#ifndef _NOAUDIO_
if( m_pVideoState->m_iAudioStreamID == -1 ){ //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Didn't find a audio stream.");
return false; };#endif
// Stream found
m_pVideoState->m_pVideoCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec; m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pVideoCtx->codec_id ); if( m_pVideoState->m_pVideoCodec == NULL ){ //SDL_Log( "Codec [%d] not found.", m_pVideoState->m_pVideoCtx->codec_id );
return false; }; if( avcodec_open2( m_pVideoState->m_pVideoCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){ //SDL_Log( "Could not open Video codec[%d].", m_pVideoState->m_pVideoCtx->codec_id );
return false; };#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_pAudioCodec ){ //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "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 );
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
if( m_bSizeIsAutomatic ){ m_nWidth = m_pVideoState->m_pVideoCtx->width; m_nHeight = m_pVideoState->m_pVideoCtx->height; };#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 );#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 ); 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
img_convert_ctx = sws_getContext( m_pCodecCtx->width, m_pCodecCtx->height, m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);#endif
#ifdef PROCESS_BGR24
img_convert_ctx = sws_getContext( m_pVideoState->m_pVideoCtx->width, m_pVideoState->m_pVideoCtx->height, m_pVideoState->m_pVideoCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);#endif
m_pVideoState->m_bStatus = 1; // All seems to be good -> allocating mem
};};
void _LIBAV_VideoInput::m_fInputProc( void ){ int RetVal; int bGotPicture = 0; m_bStatus = VI_WORKING_OK;
while( m_bStatus == VI_WORKING_OK ){
if( av_read_frame( m_pVideoState->m_pFormatCtx, m_pPacket ) >= 0 ){
if( m_pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
RetVal = avcodec_decode_video2( m_pVideoState->m_pVideoCtx, m_pFrame, &bGotPicture, m_pPacket );
if( RetVal < 0 ){ //SDL_LogError( SDL_LOG_CATEGORY_VIDEO,"_VideoInputFFD::VideoProc - Decode Error." );
m_bStatus = VI_STOPED; };
if( bGotPicture ){#ifdef PROCESS_YUV420P
sws_scale( me->img_convert_ctx, (const uint8_t* const*) me->m_pFrame->data, me->m_pFrame->linesize, 0, me->m_pCodecCtx->height, me->m_pFrameYUV->data, me->m_pFrameYUV->linesize);#endif
#ifdef PROCESS_BGR24
sws_scale( img_convert_ctx, (const uint8_t* const*) m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pVideoCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize );#endif
#ifdef PROCESS_YUV420P
SDL_UpdateYUVTexture( me->m_pTexture, &me->m_oRect, me->m_pFrameYUV->data[0], me->m_pFrameYUV->linesize[0], me->m_pFrameYUV->data[1], me->m_pFrameYUV->linesize[1], me->m_pFrameYUV->data[2], me->m_pFrameYUV->linesize[2]);#endif
#ifdef PROCESS_BGR24
sws_scale( img_convert_ctx, (const uint8_t* const*)m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pVideoCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize); //memcpy( m_pOMat.data, m_pMat.data, (m_nWidth)*(m_nHeight)*sizeof(uint8_t)*3 );
cv::Mat *mRGB; mRGB = new cv::Mat( m_nHeight, m_nWidth, CV_8UC3 ); memcpy( mRGB->data, m_pFrameBGR->data[0], (m_nWidth)*(m_nHeight)*sizeof(uint8_t)*3 ); pushFrame( mRGB );#endif
//usleep( 2000 );
}; }; av_free_packet( m_pPacket ); } else { while(1){ RetVal = avcodec_decode_video2( m_pVideoState->m_pVideoCtx, m_pFrame, &bGotPicture, m_pPacket ); if( RetVal ) break; if(!bGotPicture) break;#ifdef PROCESS_YUV420P
sws_scale(me->img_convert_ctx, (const uint8_t* const*)me->m_pFrame->data, me->m_pFrame->linesize, 0, me->m_pCodecCtx->height, me->m_pFrameYUV->data, me->m_pFrameYUV->linesize); //SDL---------------------------
#endif
#ifdef PROCESS_BGR24
sws_scale(img_convert_ctx, (const uint8_t* const*)m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pVideoCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize);
#endif
//usleep( 2000 );
}; m_pVideoState->m_bStatus = 3;#if OUTPUT_YUV420P
fclose(fp_yuv);#endif
}; }; m_bStatus = VI_STOPED; pthread_exit( NULL );};
|