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.

1189 lines
34 KiB

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