You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

992 lines
39 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
​
7 years ago
7 years ago
​
​
​
2 years ago
​
2 years ago
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
2 years ago
​
7 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
2 years ago
7 years ago
7 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
7 years ago
​
7 years ago
​
​
7 years ago
7 years ago
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
7 years ago
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
7 years ago
2 years ago
​
7 years ago
2 years ago
4 years ago
7 years ago
7 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
  1. #include "videoAVLIBSource.h"
  2. #include "libavutil/error.h"
  3. #define LOG( X ) m_pLog->Log( "LibAV_SRC", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG2w34[256];\
  7. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "LIBAV_SRC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "LIBAV_SRC", this, _LOG_ERROR, X );
  12. #endif
  13. static int interrupt_cb( void *ctx ){
  14. basicLogCollection::getInstance()->Log( "LibAV_CB", NULL, "INTERUPT!" );
  15. // do something
  16. return 0;
  17. }
  18. static const AVIOInterruptCB int_cb = { interrupt_cb, NULL };
  19. //https://ffmpeg.org/doxygen/3.4/ffplay_8c_source.html#l02732
  20. videoAVLIBSource::videoAVLIBSource( void *opaque ): basicMediaSource(), basicThread() {
  21. m_pLog = basicLogCollection::getInstance();
  22. m_bStreamingSource = true;
  23. m_bSizeIsAutomatic = false;
  24. m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = -1;
  25. m_bSrcType = AVLIB_SOURCE_NOTDEF;
  26. if( opaque ) {
  27. m_pOpaque = opaque;
  28. } else {
  29. m_pOpaque = (void *)this;
  30. };
  31. if( !initLibAV() ) return;
  32. m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
  33. if (m_pVideoState == NULL){
  34. LOG_ERROR("VideoState Memory Allocation Error");
  35. return;
  36. };
  37. m_eMediaStatus = m_pVideoState->m_bStatus = MS_UNDEFINED;
  38. m_pVideoState->m_iVideoStreamID = -1;
  39. #ifndef _NOAUDIO_
  40. m_pVideoState->m_iAudioStreamID = -1;
  41. #endif
  42. m_pVideoState->m_bStatus = MS_INIT;
  43. };
  44. videoAVLIBSource::videoAVLIBSource( char *Source, void *opaque ) : basicMediaSource(), basicThread() {
  45. m_pLog = basicLogCollection::getInstance();
  46. m_bStreamingSource = true;
  47. m_bSizeIsAutomatic = false;
  48. m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = -1;
  49. m_bSrcType = AVLIB_SOURCE_NOTDEF;
  50. if( opaque ) {
  51. m_pOpaque = opaque;
  52. } else {
  53. m_pOpaque = (void *)this;
  54. };
  55. if( !initLibAV() ) return;
  56. m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
  57. if (m_pVideoState == NULL){
  58. LOG_ERROR("VideoState Memory Allocation Error");
  59. return;
  60. };
  61. m_eMediaStatus = m_pVideoState->m_bStatus = MS_UNDEFINED;
  62. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  63. if (m_pVideoState->m_pFormatCtx == NULL){
  64. LOG_ERROR("Video Format Context Memory Allocation Error");
  65. return;
  66. };
  67. m_pVideoState->m_iVideoStreamID = -1;
  68. #ifndef _NOAUDIO_
  69. m_pVideoState->m_iAudioStreamID = -1;
  70. #endif
  71. m_pVideoState->m_bStatus = MS_INIT;
  72. _open( Source );
  73. };
  74. videoAVLIBSource::~videoAVLIBSource(){
  75. LOG("Destructor");
  76. if ( m_pVideoState != NULL ){
  77. _close();
  78. av_free(m_pVideoState);
  79. m_pVideoState = NULL;
  80. };
  81. };
  82. bool videoAVLIBSource::setOutputSize( int Width, int Height ){
  83. if( m_pVideoState ){
  84. if( m_pVideoState->m_bStatus == MS_INIT ) {
  85. if( Width && Height ) {
  86. m_bSizeIsAutomatic = false;
  87. m_nOutWidth = Width;
  88. m_nOutHeight = Height;
  89. return true;
  90. };
  91. };
  92. };
  93. return false;
  94. };
  95. bool videoAVLIBSource::setProportionalByWidth(int Width){
  96. if( m_pVideoState ){
  97. if( m_pVideoState->m_bStatus == MS_INIT ) {
  98. if( Width > 0 ) {
  99. m_bSizeIsAutomatic = true;
  100. m_nOutWidth = Width;
  101. return true;
  102. };
  103. };
  104. };
  105. return false;
  106. };
  107. bool videoAVLIBSource::setProportionalByHeight(int Height){
  108. if( m_pVideoState ){
  109. if( m_pVideoState->m_bStatus == MS_INIT ) {
  110. if( Height > 0 ) {
  111. m_bSizeIsAutomatic = true;
  112. m_nOutHeight = Height;
  113. return true;
  114. };
  115. };
  116. };
  117. return false;
  118. };
  119. bool videoAVLIBSource::setFrameAspectRatio( FrameAspectRatio AspectRatio ){
  120. return true;
  121. };
  122. bool videoAVLIBSource::_close(void){
  123. LOG("CLOSE");
  124. //set Close Status - to prevent continue
  125. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSED;
  126. if ( m_pVideoState == NULL) return true;
  127. m_pMediaDestination = NULL;//??
  128. //_stop(); //stops the Thread
  129. basicThread::waitThreadEnd( true );
  130. if ( m_pVideoState->m_pFormatOpts ) {
  131. av_dict_free( &m_pVideoState->m_pFormatOpts );
  132. m_pVideoState->m_pFormatOpts = NULL;
  133. };
  134. if ( m_pVideoState->m_pFormatCtx ) {
  135. avformat_close_input( &m_pVideoState->m_pFormatCtx );
  136. m_pVideoState->m_iVideoStreamID = -1;
  137. };
  138. if( m_pVideoState->m_bStatus > MS_INIT ){
  139. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  140. #ifndef _NOAUDIO_
  141. if (m_pVideoState->m_pAudioCtx != NULL){
  142. avcodec_close(m_pVideoState->m_pAudioCtx);
  143. avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  144. };
  145. #endif
  146. if (m_pVideoState->m_pCodecCtx != NULL){
  147. avcodec_close(m_pVideoState->m_pCodecCtx);
  148. m_pVideoState->m_iVideoStreamID = -1;
  149. };
  150. #else
  151. #ifndef _NOAUDIO_
  152. if (m_pVideoState->m_pAudioCtx != NULL){
  153. avcodec_close(m_pVideoState->m_pAudioCtx);
  154. avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  155. };
  156. #endif
  157. if (m_pVideoState->m_pCodecCtx != NULL){
  158. avcodec_close(m_pVideoState->m_pCodecCtx);
  159. avcodec_free_context(&m_pVideoState->m_pCodecCtx);
  160. };
  161. #endif
  162. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  163. };
  164. sws_freeContext( m_pVideoState->m_pSwsCtx );
  165. return true;
  166. };
  167. bool videoAVLIBSource::_continue( void ){
  168. if( m_pVideoState->m_bStatus == MS_STOPED ){
  169. return basicThread::init();
  170. };
  171. return false;
  172. };
  173. bool videoAVLIBSource::_stop(void){
  174. LOG("STOP");
  175. return basicThread::uninit();
  176. };
  177. //https://www.youtube.com/watch?v=Lu3LZmhC_DY
  178. bool videoAVLIBSource::_restart( void ){
  179. if (m_pVideoState == NULL) return false;
  180. if (m_pVideoState->m_bStatus < MS_OPENED) return false;
  181. if (m_pVideoState->m_pFormatCtx == NULL){
  182. LOG_ERROR("Video Format Context Memory Allocation Error");
  183. return false;
  184. };
  185. // Stop Thread
  186. basicThread::uninit();
  187. int RetVal;
  188. switch( m_bSrcType ){
  189. case AVLIB_SOURCE_DDSHOW:{
  190. AVInputFormat *iformat = av_find_input_format("dshow");
  191. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  192. char tB[1024];
  193. //av_free( iformat );
  194. char tBuffer[1024];
  195. av_strerror( RetVal, tBuffer, 1024 );
  196. sprintf(tB, "VideoSource Couldn't open input device[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  197. LOG_ERROR(tB);
  198. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  199. return false;
  200. };
  201. break;
  202. };
  203. case AVLIB_SOURCE_V4L2:
  204. case AVLIB_SOURCE_IPSTREAM:{
  205. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  206. //av_dict_free(&options);
  207. char tB[1024];
  208. char tBuffer[1024];
  209. av_strerror( RetVal, tBuffer, 1024 );
  210. sprintf( tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  211. LOG_ERROR( tB );
  212. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  213. return false;
  214. };
  215. break;
  216. };
  217. };
  218. #ifdef DEBUG
  219. LOG("DataStream Opened");
  220. #endif
  221. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  222. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  223. char tB[1024];
  224. sprintf( tB, "Couldn't find stream information: %s", m_pVideoState->m_pFileName );
  225. LOG_ERROR( tB );
  226. return false;
  227. };
  228. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  229. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  230. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  231. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  232. LOG( "VIDEO stream detected." );
  233. if( m_pVideoState->m_iVideoStreamID <0 ){
  234. LOG( "VIDEO stream assigned." );
  235. m_pVideoState->m_iVideoStreamID=i;
  236. };
  237. };
  238. # ifndef _NOAUDIO_
  239. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  240. LOG( "AUDIO stream detected." );
  241. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  242. LOG( "AUDIO stream assigned." );
  243. m_pVideoState->m_iAudioStreamID=i;
  244. }
  245. };
  246. # endif
  247. };
  248. if( m_pVideoState->m_iVideoStreamID == -1 ){
  249. LOG_ERROR( "Didn't find a video stream.");
  250. return false;
  251. };
  252. # ifndef _NOAUDIO_
  253. if( m_pVideoState->m_iAudioStreamID == -1 ){
  254. LOG_ERROR("Didn't find a audio stream.");
  255. return false;
  256. };
  257. # endif
  258. // Stream found
  259. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  260. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  261. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  262. #else
  263. # ifdef DEBUG_EXTRA
  264. LOG("YABABA-2!");
  265. # endif
  266. /* find the video stream information */
  267. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  268. if (m_pVideoState->m_iVideoStreamID == -1){
  269. LOG_ERROR("Didn't find a video stream.");
  270. return false;
  271. };
  272. # ifndef _NOAUDIO_
  273. if (m_pVideoState->m_iAudioStreamID == -1){
  274. LOG_ERROR("Didn't find a audio stream.");
  275. return false;
  276. };
  277. # endif
  278. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  279. LOG_ERROR("Can not create decoder context!");
  280. return false;
  281. };
  282. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  283. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  284. LOG_ERROR("Can not set Context parameters!");
  285. return false;
  286. };
  287. #endif
  288. if( m_pVideoState->m_pVideoCodec == NULL ){
  289. char tB[1024];
  290. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName);
  291. sprintf( tB, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName );
  292. LOG_ERROR( tB );
  293. return false;
  294. };
  295. /*
  296. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  297. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  298. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  299. */
  300. m_pVideoState->m_pCodecCtx->thread_count = 4;
  301. //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  302. //if (is_live)
  303. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  304. //else
  305. //m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  306. #ifdef DEBUG_EXTRA
  307. LOG("Before Open2");
  308. #endif
  309. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  310. char tB[1024];
  311. sprintf( tB, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName );
  312. LOG_ERROR( tB );
  313. return false;
  314. };
  315. /* we do not send complete frames */
  316. //if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  317. #ifndef _NOAUDIO_
  318. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  319. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  320. if( !m_pVideoState->m_pAudioCodec ){
  321. LOG_ERROR( "Unsupported Audio codec!" );
  322. return false;
  323. };
  324. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  325. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  326. LOG_ERROR( "Could not open Audio codec." );
  327. return false;
  328. };
  329. #endif
  330. return basicThread::init();
  331. };
  332. bool videoAVLIBSource::_open( char *URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, int s_width, int s_height ){
  333. if (URI == NULL) return false;
  334. if (m_pVideoState == NULL) return false;
  335. if (m_bThreadStatus > TH_PRE_INIT ) return false;
  336. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  337. if (m_pVideoState->m_pFormatCtx == NULL){
  338. LOG_ERROR("Video Format Context Memory Allocation Error");
  339. return false;
  340. };
  341. if (m_pVideoState->m_bStatus == MS_INIT) {
  342. char Buffer[1024] = {0};
  343. char tChar[128] = {0};
  344. strncpy( m_pVideoState->m_pFileName, URI, MAX_PATH_LEN );
  345. CSTR tMD5 = MD5String(m_pVideoState->m_pFileName );
  346. if ( tMD5 ) {
  347. sprintf( m_pCookie, "%s", tMD5 );
  348. free( tMD5 );
  349. } else sprintf( m_pCookie, "some-random-id" );
  350. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENING;
  351. if( strncmp( m_pVideoState->m_pFileName, "video=", 6 ) == 0 ){
  352. m_bSrcType = AVLIB_SOURCE_DDSHOW;
  353. #ifdef DEBUG
  354. LOG("MS-Video Device");
  355. #endif
  356. //AVDictionary *options = NULL;
  357. if( width != -1 && height != -1 ){
  358. m_bSizeIsAutomatic = false;
  359. char tChar[10];
  360. sprintf(tChar, "%dx%d", width, height);
  361. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  362. } else {
  363. //m_bSizeIsAutomatic = true;
  364. };
  365. //av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuyv422", 0 );
  366. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_codec", "mjpeg", 0 );
  367. if( inputPin != -1 ){
  368. sprintf(tChar, "%d", inputPin);
  369. av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", tChar, 0);
  370. };
  371. if( framerate != -1 ){
  372. sprintf(tChar, "%f", framerate);
  373. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  374. };
  375. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtbufsize", "500000k", 0);
  376. if( ShowDeviceMenu ){
  377. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_device_dialog", "true", 0);
  378. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_crossbar_connection_dialog", "true", 0);
  379. //sprintf(Buffer, "data/%s.settings", m_pCookie);
  380. //av_dict_set(&options, "video_device_save", Buffer, 0);
  381. #ifndef _NOAUDIO_
  382. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0);
  383. sprintf(Buffer, "%s-a.settings", URI);
  384. av_dict_set(&m_pVideoState->m_pFormatOpts, "audio_device_save", Buffer, 0);
  385. #endif
  386. } else {
  387. sprintf(Buffer, "data/%s.settings", m_pCookie );
  388. //if (!fileExist(Buffer)) av_dict_set(&m_pVideoState->m_pFormatOpts, "video_device_load", Buffer, 0);
  389. };
  390. //av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", "1", 0);
  391. //av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0);
  392. //av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_crossbar_connection_dialog", "true", 0);
  393. //AVInputFormat * is Internal Const in CoDec Lib
  394. AVInputFormat *iformat = av_find_input_format("dshow");
  395. //av_dict_set(&options, "pixel_format", "bgr24", 0);
  396. av_dict_set(&m_pVideoState->m_pFormatOpts, "timeout", "5000", 0);
  397. //AVDeviceInfoList *device_list;
  398. //m_pVideoState->m_pFormatCtx->iformat = iformat;
  399. //avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
  400. //avdevice_free_list_devices(&device_list);
  401. if( avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts) != 0 ){
  402. av_dict_free( &m_pVideoState->m_pFormatOpts );
  403. m_pVideoState->m_pFormatOpts = NULL;
  404. sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI);
  405. LOG_ERROR(Buffer);
  406. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  407. return false;
  408. };
  409. /*AVDictionaryEntry *e;
  410. if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  411. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  412. LOG_ERROR(Buffer);
  413. };*/
  414. //if (iformat!=NULL)
  415. //if (iformat->priv_class != NULL){
  416. // const AVOption *cOption = NULL;
  417. // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
  418. // serializeOption(cOption);
  419. //};
  420. //av_dict_free(&options);
  421. //AVOption *mpOption = NULL;
  422. } else {
  423. if( strncmp( URI, "/dev/video", 10 ) == 0 ) {
  424. m_bSrcType = AVLIB_SOURCE_V4L2;
  425. } else {
  426. m_bSrcType = AVLIB_SOURCE_IPSTREAM;
  427. };
  428. //AVDictionary *options = NULL;
  429. //av_dict_set(&m_pVideoState->m_pFormatOpts, "movflags", "faststart", 0);
  430. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ) {
  431. #ifdef DEBUG
  432. LOG("IP Stream Source");
  433. #endif
  434. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtsp_transport", "tcp", 0);
  435. av_dict_set(&m_pVideoState->m_pFormatOpts, "stimeout", "5000000", 0);
  436. av_dict_set(&m_pVideoState->m_pFormatOpts, "buffer_size", "4096000", 0);
  437. /*
  438. av_dict_set_int(&opts, "rw_timeout", 5000000, 0);
  439. av_dict_set_int(&opts, "tcp_nodelay", 1, 0);
  440. av_dict_set_int(&opts, "stimeout", 10000000, 0);
  441. av_dict_set(&opts, "user_agent", "Mozilla/5.0", 0);
  442. av_dict_set(&opts, "rtsp_transport", "tcp", 0);
  443. av_dict_set(&opts, "rtsp_flags", "prefer_tcp", 0);
  444. av_dict_set_int(&opts, "buffer_size", BUFSIZE, 0);
  445. pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;
  446. pFormatCtx->flags |= AVFMT_FLAG_DISCARD_CORRUPT;
  447. pFormatCtx->flags |= AVFMT_FLAG_NOBUFFER;
  448. */
  449. } else {
  450. #ifdef DEBUG
  451. LOG("Linux Video Device");
  452. #endif
  453. if( width != -1 && height != -1 ){
  454. m_bSizeIsAutomatic = false;
  455. sprintf(tChar, "%dx%d", width, height);
  456. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  457. switch ( inputPin ) {
  458. case 0: LOG("PIX YUYV"); av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuv420p", 0); break;
  459. case 1: LOG("PIX MJPG"); av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "mjpeg", 0); break;
  460. default: av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "rgb24", 0); break;
  461. };
  462. } else {
  463. //m_bSizeIsAutomatic = true;
  464. };
  465. av_dict_set(&m_pVideoState->m_pFormatOpts, "fflags", "nobuffer", 0);
  466. };
  467. av_dict_set(&m_pVideoState->m_pFormatOpts, "max_delay", "100", 0);
  468. //av_dict_set(&options, "analyzeduration", "1000000", 0);
  469. if( framerate != -1 ){
  470. sprintf(tChar, "%0.2f", framerate);
  471. //sprintf(tChar, "120" );
  472. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  473. //av_dict_set(&options, "frame_rate", tChar, 0);
  474. };
  475. //av_dict_set(&options, "timeout", "8000", 0);
  476. int RetVal = 0;
  477. //LOG("Before Open1");
  478. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  479. av_dict_free( &m_pVideoState->m_pFormatOpts);
  480. m_pVideoState->m_pFormatOpts = NULL;
  481. char tBuffer[1024];
  482. av_strerror( RetVal, tBuffer, 1024 );
  483. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  484. LOG_ERROR(Buffer);
  485. return false;
  486. };
  487. //LOG("After Open1");
  488. AVDictionaryEntry *e;
  489. if( e = av_dict_get( m_pVideoState->m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  490. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  491. LOG_ERROR(Buffer);
  492. };
  493. // e is pointer to entry!
  494. //av_dict_free( &e );
  495. };
  496. #ifdef DEBUG
  497. LOG("DataStream Opened");
  498. #endif
  499. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  500. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  501. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  502. LOG_ERROR(Buffer);
  503. return false;
  504. };
  505. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  506. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  507. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  508. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  509. LOG( "VIDEO stream detected." );
  510. if( m_pVideoState->m_iVideoStreamID <0 ){
  511. LOG( "VIDEO stream assigned." );
  512. m_pVideoState->m_iVideoStreamID=i;
  513. };
  514. };
  515. # ifndef _NOAUDIO_
  516. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  517. LOG( "AUDIO stream detected." );
  518. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  519. LOG( "AUDIO stream assigned." );
  520. m_pVideoState->m_iAudioStreamID=i;
  521. }
  522. };
  523. # endif
  524. };
  525. if( m_pVideoState->m_iVideoStreamID == -1 ){
  526. LOG_ERROR( "Didn't find a video stream.");
  527. return false;
  528. };
  529. // Video Stream found
  530. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  531. // Static pointer
  532. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  533. # ifndef _NOAUDIO_
  534. if( m_pVideoState->m_iAudioStreamID == -1 ){
  535. LOG_ERROR("Didn't find a audio stream.");
  536. return false;
  537. };
  538. //ToDo: Bind Audio Codec
  539. # endif
  540. #else
  541. # ifdef DEBUG_EXTRA
  542. LOG("YABABA-2!");
  543. # endif
  544. /* find the video stream information */
  545. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  546. if (m_pVideoState->m_iVideoStreamID == -1){
  547. LOG_ERROR("Didn't find a video stream.");
  548. return false;
  549. };
  550. # ifndef _NOAUDIO_
  551. if (m_pVideoState->m_iAudioStreamID == -1){
  552. LOG_ERROR("Didn't find a audio stream.");
  553. return false;
  554. };
  555. # endif
  556. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  557. LOG_ERROR("Can not create decoder context!");
  558. return false;
  559. };
  560. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  561. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  562. LOG_ERROR("Can not set Context parameters!");
  563. return false;
  564. };
  565. #endif
  566. if( m_pVideoState->m_pVideoCodec == NULL ){
  567. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  568. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  569. LOG_ERROR( Buffer );
  570. return false;
  571. };
  572. /*
  573. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  574. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  575. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  576. */
  577. m_pVideoState->m_pCodecCtx->thread_count = 4;
  578. //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  579. //if (is_live)
  580. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  581. //else
  582. //m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  583. #ifdef DEBUG_EXTRA
  584. LOG("Before Open2");
  585. #endif
  586. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  587. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  588. LOG_ERROR( Buffer );
  589. return false;
  590. };
  591. /* we do not send complete frames */
  592. if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  593. #ifndef _NOAUDIO_
  594. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  595. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  596. if( !m_pVideoState->m_pAudioCodec ){
  597. LOG_ERROR( "Unsupported Audio codec!" );
  598. return false;
  599. };
  600. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  601. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  602. LOG_ERROR( "Could not open Audio codec." );
  603. return false;
  604. };
  605. #endif
  606. #ifdef DEBUG_EXTRA
  607. char tB[256];
  608. sprintf( tB, "Setting Frame [%dx%d]", s_width, s_height );
  609. LOG( tB );
  610. sprintf( tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height );
  611. LOG( tB );
  612. #endif
  613. if( ( s_width > 0 ) && ( s_height > 0 ) ){
  614. m_nOutWidth = s_width;
  615. m_nOutHeight = s_height;
  616. } else {
  617. if( m_bSizeIsAutomatic ){
  618. #ifdef DEBUG_EXTRA
  619. LOG("Size is Automatic");
  620. #endif
  621. if(( m_nOutWidth == -1 )&&( m_nOutHeight == -1 )){
  622. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  623. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  624. } else {
  625. // Proportional
  626. if( m_nOutHeight == -1 ){
  627. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  628. m_nOutHeight = (int)(((float)m_nOutWidth ) * k );
  629. } else {
  630. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  631. m_nOutWidth = (int)(((float)m_nOutHeight ) * k );
  632. };
  633. };
  634. } else {
  635. #ifdef DEBUG_EXTRA
  636. LOG("Size is Manual");
  637. #endif
  638. if( ( width > 0 ) && ( height > 0 ) ){
  639. m_nOutWidth = width;
  640. m_nOutHeight = height;
  641. } else {
  642. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  643. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  644. };
  645. };
  646. };
  647. #ifdef DEBUG_EXTRA
  648. sprintf( tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight );
  649. LOG( tB );
  650. sprintf(tB,"swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width,
  651. m_pVideoState->m_pCodecCtx->height,
  652. m_pVideoState->m_pCodecCtx->pix_fmt,
  653. m_nOutWidth,
  654. m_nOutHeight,
  655. AV_PIX_FMT_BGR24,
  656. SWS_BICUBIC );
  657. LOG( tB );
  658. #endif
  659. m_pVideoState->m_pSwsCtx = sws_getContext(
  660. m_pVideoState->m_pCodecCtx->width,
  661. m_pVideoState->m_pCodecCtx->height,
  662. m_pVideoState->m_pCodecCtx->pix_fmt,
  663. m_nOutWidth,
  664. m_nOutHeight,
  665. AV_PIX_FMT_BGR24,
  666. SWS_BICUBIC, //SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  667. NULL,
  668. NULL,
  669. NULL
  670. );
  671. if ( m_pVideoState->m_pSwsCtx == NULL ){
  672. LOG_ERROR("Error Allocating SwsCtx!");
  673. return false;
  674. };
  675. #ifdef DEBUG
  676. LOG("Allocation Done->Start Thread!");
  677. #endif
  678. return init();
  679. };
  680. return false;
  681. };
  682. void videoAVLIBSource::m_fThreadProc( void ){
  683. AVPacket * pPacket = NULL;
  684. cv::Mat mRGB( m_nOutHeight, m_nOutWidth, CV_8UC3, cv::Scalar( 255, 0, 255 ) );
  685. AVFrame * pFrame = NULL;
  686. AVFrame * pFrameBGR = NULL;
  687. uint8_t * pBGRbuffer = NULL;
  688. int dec_ret;
  689. char tB[256];
  690. /** @todo Valgrind is reporting several important memory leaks in this method.
  691. *
  692. * The first one has to do with @p av_frame_alloc():
  693. *
  694. * ~~~~
  695. ==00:00:04:04.219 26021== 2,074,168 (536 direct, 2,073,632 indirect) bytes in 1 blocks are definitely lost in loss record 3,970 of 3,972
  696. ==00:00:04:04.219 26021== at 0x483AEB8: memalign (vg_replace_malloc.c:906)
  697. ==00:00:04:04.219 26021== by 0x483AFCE: posix_memalign (vg_replace_malloc.c:1070)
  698. ==00:00:04:04.219 26021== by 0x66E2244: av_malloc (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
  699. ==00:00:04:04.219 26021== by 0x66E25FD: av_mallocz (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
  700. ==00:00:04:04.219 26021== by 0x66BF6BF: av_frame_alloc (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
  701. ==00:00:04:04.219 26021== by 0x248B0E: videoAVLIBSource::m_fThreadProc() (videoAVLIBSource.cpp:802)
  702. ==00:00:04:04.219 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62)
  703. ==00:00:04:04.219 26021== by 0x49B7EA6: start_thread (pthread_create.c:477)
  704. ==00:00:04:04.219 26021== by 0x7874ADE: clone (clone.S:95)
  705. * ~~~~
  706. *
  707. * The next one has to do with @p av_image_alloc():
  708. *
  709. * ~~~~
  710. ==00:00:04:04.219 26021== 2,073,632 bytes in 1 blocks are indirectly lost in loss record 3,969 of 3,972
  711. ==00:00:04:04.219 26021== at 0x483AEB8: memalign (vg_replace_malloc.c:906)
  712. ==00:00:04:04.219 26021== by 0x483AFCE: posix_memalign (vg_replace_malloc.c:1070)
  713. ==00:00:04:04.219 26021== by 0x66E2244: av_malloc (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
  714. ==00:00:04:04.219 26021== by 0x66DCEB5: av_image_alloc (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
  715. ==00:00:04:04.219 26021== by 0x248BCB: videoAVLIBSource::m_fThreadProc() (videoAVLIBSource.cpp:807)
  716. ==00:00:04:04.219 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62)
  717. ==00:00:04:04.219 26021== by 0x49B7EA6: start_thread (pthread_create.c:477)
  718. ==00:00:04:04.219 26021== by 0x7874ADE: clone (clone.S:95)
  719. * ~~~~
  720. */
  721. m_bThreadStatus = TH_WORKING;
  722. AVPacket Packet;
  723. av_init_packet( &Packet ); // Optional in newer API
  724. Packet.data = NULL;
  725. Packet.size = 0;
  726. pFrame = av_frame_alloc();
  727. if ( pFrame == NULL ) {
  728. m_bThreadStatus = TH_ENDED;
  729. LOG_ERROR("Error allocating SRC AVFrame!");
  730. } else {
  731. pFrameBGR = av_frame_alloc();
  732. if ( pFrameBGR == NULL ) {
  733. m_bThreadStatus = TH_ENDED;
  734. LOG_ERROR("Error allocating BGR AVFrame!");
  735. } else {
  736. dec_ret = av_image_alloc( pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN );
  737. sprintf( tB, "Allocated: %d bytes to Frame", dec_ret );
  738. LOG( tB );
  739. if ( dec_ret < 0 ) {
  740. m_bThreadStatus = TH_ENDED;
  741. LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  742. } else {
  743. //pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  744. //if ( pBGRbuffer == NULL ) {
  745. // m_bThreadStatus = TH_ENDED;
  746. // LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  747. //} else {
  748. // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN );
  749. //
  750. // pFrameBGR->width = m_nWidth;
  751. // pFrameBGR->height = m_nHeight;
  752. // m_pVideoState->m_bStatus = MS_ALOCATED;
  753. #ifdef DEBUG
  754. LOG("Video Reader Thread Started.");
  755. #endif
  756. };
  757. };
  758. };
  759. //cv::setBreakOnError( true );
  760. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  761. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  762. SLEEP(20);
  763. #ifdef DEBUG_EXTRA
  764. LOG("Waiting Destination");
  765. #endif
  766. };
  767. m_eMediaStatus = m_pVideoState->m_bStatus = MS_PRESENT;
  768. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ){
  769. av_read_play( m_pVideoState->m_pFormatCtx );
  770. };
  771. #ifdef DEBUG
  772. LOG("Presentation Started.");
  773. #endif
  774. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  775. if ( m_pMediaDestination != NULL ){
  776. dec_ret = av_read_frame( m_pVideoState->m_pFormatCtx, &Packet );
  777. if( dec_ret >= 0 ){
  778. if( Packet.stream_index == m_pVideoState->m_iVideoStreamID ){
  779. m_pVideoState->m_bStatus = MS_PRESENT;
  780. dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, &Packet );
  781. if( dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */ ){
  782. LOG_ERROR( "Error sending video package to decoder" );
  783. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  784. m_bThreadStatus = TH_ENDED;
  785. } else {
  786. while( dec_ret >= 0 && m_bThreadStatus != TH_ENDED ) {
  787. dec_ret = avcodec_receive_frame( m_pVideoState->m_pCodecCtx, pFrame );
  788. if( dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF ){
  789. break;//Skip to next data
  790. } else if( dec_ret < 0 ){
  791. LOG_ERROR("Error decoding video frame");
  792. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  793. m_bThreadStatus = TH_ENDED;
  794. break;
  795. };
  796. sws_scale( m_pVideoState->m_pSwsCtx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize );
  797. try{
  798. if ( m_pMediaDestination ) {
  799. memcpy( mRGB.data, pFrameBGR->data[0], pFrameBGR->linesize[0] * m_nOutHeight );
  800. if (!m_pMediaDestination->pushFrame(mRGB, m_pOpaque)) {
  801. m_pMediaDestination = NULL;
  802. LOG_ERROR("Error Consume Frame by Media Destination!");
  803. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  804. m_bThreadStatus = TH_ENDED;
  805. };
  806. };
  807. } catch (cv::Exception & e) {
  808. LOG_ERROR( e.msg.c_str() );
  809. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  810. m_pMediaDestination = NULL;
  811. m_bThreadStatus = TH_ENDED;
  812. break;
  813. };
  814. };
  815. };
  816. };
  817. av_packet_unref( &Packet );
  818. } else if( dec_ret != AVERROR(EAGAIN) && dec_ret != AVERROR_EOF ){
  819. LOG_ERROR( "Error reading video package" );
  820. char tBuffer[1024];
  821. av_strerror( dec_ret, tBuffer, 1024 );
  822. LOG_ERROR( tBuffer );
  823. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  824. m_bThreadStatus = TH_ENDED;
  825. } else {
  826. if( dec_ret == AVERROR_EOF ){
  827. avformat_close_input( &m_pVideoState->m_pFormatCtx );
  828. if( ( dec_ret = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  829. char tBuffer[1024];
  830. av_strerror( dec_ret, tBuffer, 1024 );
  831. sprintf( tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, dec_ret, m_pVideoState->m_pFileName );
  832. LOG_ERROR( tB );
  833. m_pVideoState->m_bStatus = MS_CLOSING;
  834. m_bThreadStatus = TH_ENDED;
  835. };
  836. };
  837. };
  838. SLEEP( 5 );
  839. } else SLEEP( 5 );
  840. };
  841. #ifdef DEBUG
  842. LOG("Video Source Ended!");
  843. #endif
  844. if ( pFrame != NULL ) {
  845. av_frame_free( &pFrame );
  846. };
  847. if ( pFrameBGR ) {
  848. if( pFrameBGR->data[0] ) av_freep(&pFrameBGR->data[0]);
  849. av_frame_free( &pFrameBGR );
  850. };
  851. if (pBGRbuffer != NULL) {
  852. av_free( pBGRbuffer );
  853. };
  854. m_bThreadStatus = TH_ENDED;
  855. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  856. #ifdef DEBUG
  857. LOG("Video Structures Cleared!");
  858. #endif
  859. };
  860. /*
  861. 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.
  862. int save_frame_as_jpeg( AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) {
  863. AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
  864. if (!jpegCodec) {
  865. return -1;
  866. }
  867. AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
  868. if (!jpegContext) {
  869. return -1;
  870. }
  871. jpegContext->pix_fmt = pCodecCtx->pix_fmt;
  872. jpegContext->height = pFrame->height;
  873. jpegContext->width = pFrame->width;
  874. if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
  875. return -1;
  876. }
  877. FILE *JPEGFile;
  878. char JPEGFName[256];
  879. AVPacket packet = {.data = NULL, .size = 0};
  880. av_init_packet(&packet);
  881. int gotFrame;
  882. if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) {
  883. return -1;
  884. }
  885. sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo);
  886. JPEGFile = fopen(JPEGFName, "wb");
  887. fwrite(packet.data, 1, packet.size, JPEGFile);
  888. fclose(JPEGFile);
  889. av_free_packet(&packet);
  890. avcodec_close(jpegContext);
  891. return 0;
  892. }
  893. */
  894. MediaStateStatus videoAVLIBSource::getStatus( void ){
  895. if( m_pVideoState ) return m_pVideoState->m_bStatus;
  896. return MS_UNDEFINED;
  897. };
  898. bool videoAVLIBSource::isFinished( void ){
  899. if( m_pVideoState ) if( m_pVideoState->m_bStatus < MS_CLOSING ) return false;
  900. return true;
  901. };
  902. #undef LOG
  903. #undef LOG_ERROR