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.

1169 lines
33 KiB

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