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.

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