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.

1179 lines
34 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. AVInputFormat * iformat = av_find_input_format("dshow");
  258. if ((RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts)) != 0)
  259. {
  260. char tB[2048];
  261. // av_free( iformat );
  262. char tBuffer[128];
  263. av_strerror(RetVal, tBuffer, 128);
  264. sprintf(tB, "VideoSource Couldn't open input device[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName);
  265. LOG_ERROR(tB);
  266. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  267. return false;
  268. };
  269. break;
  270. };
  271. case AVLIB_SOURCE_V4L2:
  272. case AVLIB_SOURCE_IPSTREAM:
  273. {
  274. if ((RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts)) != 0)
  275. {
  276. // av_dict_free(&options);
  277. char tB[2048];
  278. char tBuffer[128];
  279. av_strerror(RetVal, tBuffer, 128);
  280. sprintf(tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName);
  281. LOG_ERROR(tB);
  282. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  283. return false;
  284. };
  285. break;
  286. };
  287. };
  288. #ifdef DEBUG
  289. LOG("DataStream Opened");
  290. #endif
  291. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  292. if (avformat_find_stream_info(m_pVideoState->m_pFormatCtx, NULL) < 0)
  293. {
  294. char tB[2048];
  295. sprintf(tB, "Couldn't find stream information: %s", m_pVideoState->m_pFileName);
  296. LOG_ERROR(tB);
  297. return false;
  298. };
  299. #if (LIBAVCODEC_VERSION_MAJOR < 58) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  300. for (unsigned int i = 0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++)
  301. {
  302. // if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  303. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  304. {
  305. LOG("VIDEO stream detected.");
  306. if (m_pVideoState->m_iVideoStreamID < 0)
  307. {
  308. LOG("VIDEO stream assigned.");
  309. m_pVideoState->m_iVideoStreamID = i;
  310. };
  311. };
  312. #ifndef _NOAUDIO_
  313. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  314. {
  315. LOG("AUDIO stream detected.");
  316. if (m_pVideoState->m_iAudioStreamID < 0)
  317. {
  318. LOG("AUDIO stream assigned.");
  319. m_pVideoState->m_iAudioStreamID = i;
  320. }
  321. };
  322. #endif
  323. };
  324. if (m_pVideoState->m_iVideoStreamID == -1)
  325. {
  326. LOG_ERROR("Didn't find a video stream.");
  327. return false;
  328. };
  329. #ifndef _NOAUDIO_
  330. if (m_pVideoState->m_iAudioStreamID == -1)
  331. {
  332. LOG_ERROR("Didn't find a audio stream.");
  333. return false;
  334. };
  335. #endif
  336. // Stream found
  337. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  338. // m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  339. m_pVideoState->m_pVideoCodec = avcodec_find_decoder(m_pVideoState->m_pCodecCtx->codec_id);
  340. #else
  341. #ifdef DEBUG_EXTRA
  342. LOG("YABABA-2!");
  343. #endif
  344. /* find the video stream information */
  345. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  346. if (m_pVideoState->m_iVideoStreamID == -1)
  347. {
  348. LOG_ERROR("Didn't find a video stream.");
  349. return false;
  350. };
  351. #ifndef _NOAUDIO_
  352. if (m_pVideoState->m_iAudioStreamID == -1)
  353. {
  354. LOG_ERROR("Didn't find a audio stream.");
  355. return false;
  356. };
  357. #endif
  358. if (! (m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec)))
  359. {
  360. LOG_ERROR("Can not create decoder context!");
  361. return false;
  362. };
  363. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  364. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0)
  365. {
  366. LOG_ERROR("Can not set Context parameters!");
  367. return false;
  368. };
  369. #endif
  370. if (m_pVideoState->m_pVideoCodec == NULL)
  371. {
  372. char tB[2048];
  373. // sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName);
  374. sprintf(tB, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName);
  375. LOG_ERROR(tB);
  376. return false;
  377. };
  378. /*
  379. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  380. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  381. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  382. */
  383. m_pVideoState->m_pCodecCtx->thread_count = 4;
  384. // m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  385. // if (is_live)
  386. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  387. // else
  388. // m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  389. #ifdef DEBUG_EXTRA
  390. LOG("Before Open2");
  391. #endif
  392. if (avcodec_open2(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL) < 0)
  393. {
  394. char tB[2048];
  395. sprintf(tB, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName);
  396. LOG_ERROR(tB);
  397. return false;
  398. };
  399. /* we do not send complete frames */
  400. // if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  401. #ifndef _NOAUDIO_
  402. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  403. m_pVideoState->m_pAudioCodec = avcodec_find_decoder(m_pVideoState->m_pAudioCtx->codec_id);
  404. if (! m_pVideoState->m_pAudioCodec)
  405. {
  406. LOG_ERROR("Unsupported Audio codec!");
  407. return false;
  408. };
  409. if (avcodec_open2(m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL) < 0)
  410. {
  411. // SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  412. LOG_ERROR("Could not open Audio codec.");
  413. return false;
  414. };
  415. #endif
  416. return basicThread::init();
  417. }
  418. bool videoAVLIBSource::_open(char * URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, int s_width, int s_height)
  419. {
  420. if (URI == NULL)
  421. {
  422. return false;
  423. }
  424. if (m_pVideoState == NULL)
  425. {
  426. return false;
  427. }
  428. if (m_bThreadStatus > TH_PRE_INIT)
  429. {
  430. return false;
  431. }
  432. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  433. if (m_pVideoState->m_pFormatCtx == NULL)
  434. {
  435. LOG_ERROR("Video Format Context Memory Allocation Error");
  436. return false;
  437. };
  438. if (m_pVideoState->m_bStatus == MS_INIT)
  439. {
  440. char Buffer[1248] = {0};
  441. char tChar[128] = {0};
  442. strncpy(m_pVideoState->m_pFileName, URI, MAX_PATH_LEN);
  443. CSTR tMD5 = MD5String(m_pVideoState->m_pFileName);
  444. if (tMD5)
  445. {
  446. sprintf(m_pCookie, "%s", tMD5);
  447. free(tMD5);
  448. }
  449. else
  450. {
  451. sprintf(m_pCookie, "some-random-id");
  452. }
  453. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENING;
  454. if (strncmp(m_pVideoState->m_pFileName, "video=", 6) == 0)
  455. {
  456. m_bSrcType = AVLIB_SOURCE_DDSHOW;
  457. #ifdef DEBUG
  458. LOG("MS-Video Device");
  459. #endif
  460. // AVDictionary *options = NULL;
  461. if (width != -1 && height != -1)
  462. {
  463. m_bSizeIsAutomatic = false;
  464. char tChar[10];
  465. sprintf(tChar, "%dx%d", width, height);
  466. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  467. }
  468. else
  469. {
  470. // m_bSizeIsAutomatic = true;
  471. };
  472. // av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuyv422", 0 );
  473. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_codec", "mjpeg", 0);
  474. if (inputPin != -1)
  475. {
  476. sprintf(tChar, "%d", inputPin);
  477. av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", tChar, 0);
  478. };
  479. if (framerate != -1)
  480. {
  481. sprintf(tChar, "%f", framerate);
  482. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  483. };
  484. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtbufsize", "500000k", 0);
  485. if (ShowDeviceMenu)
  486. {
  487. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_device_dialog", "true", 0);
  488. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_crossbar_connection_dialog", "true", 0);
  489. // sprintf(Buffer, "data/%s.settings", m_pCookie);
  490. // av_dict_set(&options, "video_device_save", Buffer, 0);
  491. #ifndef _NOAUDIO_
  492. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0);
  493. sprintf(Buffer, "%s-a.settings", URI);
  494. av_dict_set(&m_pVideoState->m_pFormatOpts, "audio_device_save", Buffer, 0);
  495. #endif
  496. }
  497. else
  498. {
  499. sprintf(Buffer, "data/%s.settings", m_pCookie);
  500. // if (!fileExist(Buffer)) av_dict_set(&m_pVideoState->m_pFormatOpts, "video_device_load", Buffer, 0);
  501. };
  502. // av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", "1", 0);
  503. // av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0);
  504. // av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_crossbar_connection_dialog", "true", 0);
  505. // AVInputFormat * is Internal Const in CoDec Lib
  506. AVInputFormat * iformat = av_find_input_format("dshow");
  507. // av_dict_set(&options, "pixel_format", "bgr24", 0);
  508. av_dict_set(&m_pVideoState->m_pFormatOpts, "timeout", "5000", 0);
  509. // AVDeviceInfoList *device_list;
  510. // m_pVideoState->m_pFormatCtx->iformat = iformat;
  511. // avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
  512. // avdevice_free_list_devices(&device_list);
  513. if (avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts) != 0)
  514. {
  515. av_dict_free(&m_pVideoState->m_pFormatOpts);
  516. m_pVideoState->m_pFormatOpts = NULL;
  517. sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI);
  518. LOG_ERROR(Buffer);
  519. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  520. return false;
  521. };
  522. /*AVDictionaryEntry *e;
  523. if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  524. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  525. LOG_ERROR(Buffer);
  526. };*/
  527. // if (iformat!=NULL)
  528. // if (iformat->priv_class != NULL){
  529. // const AVOption *cOption = NULL;
  530. // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
  531. // serializeOption(cOption);
  532. // };
  533. // av_dict_free(&options);
  534. // AVOption *mpOption = NULL;
  535. }
  536. else
  537. {
  538. if (strncmp(URI, "/dev/video", 10) == 0)
  539. {
  540. m_bSrcType = AVLIB_SOURCE_V4L2;
  541. }
  542. else
  543. {
  544. m_bSrcType = AVLIB_SOURCE_IPSTREAM;
  545. };
  546. // AVDictionary *options = NULL;
  547. // av_dict_set(&m_pVideoState->m_pFormatOpts, "movflags", "faststart", 0);
  548. if (m_bSrcType == AVLIB_SOURCE_IPSTREAM)
  549. {
  550. #ifdef DEBUG
  551. LOG("IP Stream Source");
  552. #endif
  553. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtsp_transport", "tcp", 0);
  554. av_dict_set(&m_pVideoState->m_pFormatOpts, "stimeout", "5000000", 0);
  555. av_dict_set(&m_pVideoState->m_pFormatOpts, "buffer_size", "4096000", 0);
  556. /*
  557. av_dict_set_int(&opts, "rw_timeout", 5000000, 0);
  558. av_dict_set_int(&opts, "tcp_nodelay", 1, 0);
  559. av_dict_set_int(&opts, "stimeout", 10000000, 0);
  560. av_dict_set(&opts, "user_agent", "Mozilla/5.0", 0);
  561. av_dict_set(&opts, "rtsp_transport", "tcp", 0);
  562. av_dict_set(&opts, "rtsp_flags", "prefer_tcp", 0);
  563. av_dict_set_int(&opts, "buffer_size", BUFSIZE, 0);
  564. pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;
  565. pFormatCtx->flags |= AVFMT_FLAG_DISCARD_CORRUPT;
  566. pFormatCtx->flags |= AVFMT_FLAG_NOBUFFER;
  567. */
  568. }
  569. else
  570. {
  571. #ifdef DEBUG
  572. LOG("Linux Video Device");
  573. #endif
  574. if (width != -1 && height != -1)
  575. {
  576. m_bSizeIsAutomatic = false;
  577. sprintf(tChar, "%dx%d", width, height);
  578. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  579. switch (inputPin)
  580. {
  581. case 0:
  582. LOG("PIX YUYV");
  583. av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuv420p", 0);
  584. break;
  585. case 1:
  586. LOG("PIX MJPG");
  587. av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "mjpeg", 0);
  588. break;
  589. default: av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "rgb24", 0); break;
  590. };
  591. }
  592. else
  593. {
  594. // m_bSizeIsAutomatic = true;
  595. };
  596. av_dict_set(&m_pVideoState->m_pFormatOpts, "fflags", "nobuffer", 0);
  597. };
  598. av_dict_set(&m_pVideoState->m_pFormatOpts, "max_delay", "100", 0);
  599. // av_dict_set(&options, "analyzeduration", "1000000", 0);
  600. if (framerate != -1)
  601. {
  602. sprintf(tChar, "%0.2f", framerate);
  603. // sprintf(tChar, "120" );
  604. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  605. // av_dict_set(&options, "frame_rate", tChar, 0);
  606. };
  607. // av_dict_set(&options, "timeout", "8000", 0);
  608. int RetVal = 0;
  609. // LOG("Before Open1");
  610. if ((RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts)) != 0)
  611. {
  612. av_dict_free(&m_pVideoState->m_pFormatOpts);
  613. m_pVideoState->m_pFormatOpts = NULL;
  614. char tBuffer[128];
  615. av_strerror(RetVal, tBuffer, 128);
  616. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName);
  617. LOG_ERROR(Buffer);
  618. return false;
  619. };
  620. // LOG("After Open1");
  621. AVDictionaryEntry * e;
  622. if ((e = av_dict_get(m_pVideoState->m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX)))
  623. {
  624. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  625. LOG_ERROR(Buffer);
  626. };
  627. // e is pointer to entry!
  628. // av_dict_free( &e );
  629. };
  630. #ifdef DEBUG
  631. LOG("DataStream Opened");
  632. #endif
  633. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  634. if (avformat_find_stream_info(m_pVideoState->m_pFormatCtx, NULL) < 0)
  635. {
  636. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  637. LOG_ERROR(Buffer);
  638. return false;
  639. };
  640. #if (LIBAVCODEC_VERSION_MAJOR < 58) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  641. for (unsigned int i = 0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++)
  642. {
  643. // if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  644. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  645. {
  646. LOG("VIDEO stream detected.");
  647. if (m_pVideoState->m_iVideoStreamID < 0)
  648. {
  649. LOG("VIDEO stream assigned.");
  650. m_pVideoState->m_iVideoStreamID = i;
  651. };
  652. };
  653. #ifndef _NOAUDIO_
  654. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  655. {
  656. LOG("AUDIO stream detected.");
  657. if (m_pVideoState->m_iAudioStreamID < 0)
  658. {
  659. LOG("AUDIO stream assigned.");
  660. m_pVideoState->m_iAudioStreamID = i;
  661. }
  662. };
  663. #endif
  664. };
  665. if (m_pVideoState->m_iVideoStreamID == -1)
  666. {
  667. LOG_ERROR("Didn't find a video stream.");
  668. return false;
  669. };
  670. // Video Stream found
  671. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  672. // Static pointer
  673. m_pVideoState->m_pVideoCodec = avcodec_find_decoder(m_pVideoState->m_pCodecCtx->codec_id);
  674. #ifndef _NOAUDIO_
  675. if (m_pVideoState->m_iAudioStreamID == -1)
  676. {
  677. LOG_ERROR("Didn't find a audio stream.");
  678. return false;
  679. };
  680. // ToDo: Bind Audio Codec
  681. #endif
  682. #else
  683. #ifdef DEBUG_EXTRA
  684. LOG("YABABA-2!");
  685. #endif
  686. /* find the video stream information */
  687. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  688. if (m_pVideoState->m_iVideoStreamID == -1)
  689. {
  690. LOG_ERROR("Didn't find a video stream.");
  691. return false;
  692. };
  693. #ifndef _NOAUDIO_
  694. if (m_pVideoState->m_iAudioStreamID == -1)
  695. {
  696. LOG_ERROR("Didn't find a audio stream.");
  697. return false;
  698. };
  699. #endif
  700. if (! (m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec)))
  701. {
  702. LOG_ERROR("Can not create decoder context!");
  703. return false;
  704. };
  705. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  706. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0)
  707. {
  708. LOG_ERROR("Can not set Context parameters!");
  709. return false;
  710. };
  711. #endif
  712. if (m_pVideoState->m_pVideoCodec == NULL)
  713. {
  714. // sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  715. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  716. LOG_ERROR(Buffer);
  717. return false;
  718. };
  719. /*
  720. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  721. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  722. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  723. */
  724. m_pVideoState->m_pCodecCtx->thread_count = 4;
  725. // m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  726. // if (is_live)
  727. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  728. // else
  729. // m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  730. #ifdef DEBUG_EXTRA
  731. LOG("Before Open2");
  732. #endif
  733. if (avcodec_open2(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL) < 0)
  734. {
  735. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  736. LOG_ERROR(Buffer);
  737. return false;
  738. };
  739. /* we do not send complete frames */
  740. if (m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED)
  741. {
  742. m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  743. }
  744. #ifndef _NOAUDIO_
  745. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  746. m_pVideoState->m_pAudioCodec = avcodec_find_decoder(m_pVideoState->m_pAudioCtx->codec_id);
  747. if (! m_pVideoState->m_pAudioCodec)
  748. {
  749. LOG_ERROR("Unsupported Audio codec!");
  750. return false;
  751. };
  752. if (avcodec_open2(m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL) < 0)
  753. {
  754. // SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  755. LOG_ERROR("Could not open Audio codec.");
  756. return false;
  757. };
  758. #endif
  759. #ifdef DEBUG_EXTRA
  760. char tB[256];
  761. sprintf(tB, "Setting Frame [%dx%d]", s_width, s_height);
  762. LOG(tB);
  763. sprintf(tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height);
  764. LOG(tB);
  765. #endif
  766. if ((s_width > 0) && (s_height > 0))
  767. {
  768. m_nOutWidth = s_width;
  769. m_nOutHeight = s_height;
  770. }
  771. else
  772. {
  773. if (m_bSizeIsAutomatic)
  774. {
  775. #ifdef DEBUG_EXTRA
  776. LOG("Size is Automatic");
  777. #endif
  778. if ((m_nOutWidth == -1) && (m_nOutHeight == -1))
  779. {
  780. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  781. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  782. }
  783. else
  784. {
  785. // Proportional
  786. if (m_nOutHeight == -1)
  787. {
  788. float k = ((float) m_pVideoState->m_pCodecCtx->height) / ((float) m_pVideoState->m_pCodecCtx->width);
  789. m_nOutHeight = (int) (((float) m_nOutWidth) * k);
  790. }
  791. else
  792. {
  793. float k = ((float) m_pVideoState->m_pCodecCtx->width) / ((float) m_pVideoState->m_pCodecCtx->height);
  794. m_nOutWidth = (int) (((float) m_nOutHeight) * k);
  795. };
  796. };
  797. }
  798. else
  799. {
  800. #ifdef DEBUG_EXTRA
  801. LOG("Size is Manual");
  802. #endif
  803. if ((width > 0) && (height > 0))
  804. {
  805. m_nOutWidth = width;
  806. m_nOutHeight = height;
  807. }
  808. else
  809. {
  810. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  811. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  812. };
  813. };
  814. };
  815. #ifdef DEBUG_EXTRA
  816. sprintf(tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight);
  817. LOG(tB);
  818. 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);
  819. LOG(tB);
  820. #endif
  821. 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,
  822. SWS_BICUBIC, // SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  823. NULL, NULL, NULL);
  824. if (m_pVideoState->m_pSwsCtx == NULL)
  825. {
  826. LOG_ERROR("Error Allocating SwsCtx!");
  827. return false;
  828. };
  829. #ifdef DEBUG
  830. LOG("Allocation Done->Start Thread!");
  831. #endif
  832. return init();
  833. };
  834. return false;
  835. }
  836. void videoAVLIBSource::m_fThreadProc(void)
  837. {
  838. //AVPacket * pPacket = NULL;
  839. cv::Mat mRGB(m_nOutHeight, m_nOutWidth, CV_8UC3, cv::Scalar(255, 0, 255));
  840. AVFrame * pFrame = NULL;
  841. AVFrame * pFrameBGR = NULL;
  842. uint8_t * pBGRbuffer = NULL;
  843. int dec_ret;
  844. char tB[2048];
  845. /** @todo Valgrind is reporting several important memory leaks in this method.
  846. *
  847. * The first one has to do with @p av_frame_alloc():
  848. * The next one has to do with @p av_image_alloc():
  849. */
  850. m_bThreadStatus = TH_WORKING;
  851. pthread_setname_np(pthread_self(), "AVLib Source");
  852. //AVPacket Packet;
  853. //av_init_packet(&Packet); // Optional in newer API
  854. AVPacket *Packet = av_packet_alloc();
  855. if (!Packet) {
  856. m_bThreadStatus = TH_ENDED;
  857. LOG_ERROR("Error allocating SRC AVFrame!");
  858. } else {
  859. Packet->data = NULL;
  860. Packet->size = 0;
  861. pFrame = av_frame_alloc();
  862. if (pFrame == NULL) {
  863. m_bThreadStatus = TH_ENDED;
  864. LOG_ERROR("Error allocating SRC AVFrame!");
  865. } else {
  866. pFrameBGR = av_frame_alloc();
  867. if (pFrameBGR == NULL) {
  868. m_bThreadStatus = TH_ENDED;
  869. LOG_ERROR("Error allocating BGR AVFrame!");
  870. } else {
  871. dec_ret = av_image_alloc(pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN);
  872. sprintf(tB, "Allocated: %d bytes to Frame", dec_ret);
  873. LOG(tB);
  874. if (dec_ret < 0) {
  875. m_bThreadStatus = TH_ENDED;
  876. LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  877. } else {
  878. // pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  879. // if ( pBGRbuffer == NULL ) {
  880. // m_bThreadStatus = TH_ENDED;
  881. // LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  882. // } else {
  883. // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN );
  884. //
  885. // pFrameBGR->width = m_nWidth;
  886. // pFrameBGR->height = m_nHeight;
  887. // m_pVideoState->m_bStatus = MS_ALOCATED;
  888. #ifdef DEBUG
  889. LOG("Video Reader Thread Started.");
  890. #endif
  891. };
  892. };
  893. };
  894. };// Packet allocation
  895. // cv::setBreakOnError( true );
  896. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  897. while (m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING)
  898. {
  899. SLEEP(20);
  900. #ifdef DEBUG_EXTRA
  901. LOG("Waiting Destination");
  902. #endif
  903. };
  904. m_eMediaStatus = m_pVideoState->m_bStatus = MS_PRESENT;
  905. if (m_bSrcType == AVLIB_SOURCE_IPSTREAM)
  906. {
  907. av_read_play(m_pVideoState->m_pFormatCtx);
  908. };
  909. #ifdef DEBUG
  910. LOG("Presentation Started.");
  911. #endif
  912. while (m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED)
  913. {
  914. if (m_pMediaDestination != NULL)
  915. {
  916. dec_ret = av_read_frame(m_pVideoState->m_pFormatCtx, Packet);
  917. if (dec_ret >= 0)
  918. {
  919. if (Packet->stream_index == m_pVideoState->m_iVideoStreamID)
  920. {
  921. m_pVideoState->m_bStatus = MS_PRESENT;
  922. dec_ret = avcodec_send_packet(m_pVideoState->m_pCodecCtx, Packet);
  923. if (dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */)
  924. {
  925. LOG_ERROR("Error sending video package to decoder");
  926. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  927. m_bThreadStatus = TH_ENDED;
  928. }
  929. else
  930. {
  931. while (dec_ret >= 0 && m_bThreadStatus != TH_ENDED)
  932. {
  933. dec_ret = avcodec_receive_frame(m_pVideoState->m_pCodecCtx, pFrame);
  934. if (dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF)
  935. {
  936. break; // Skip to next data
  937. }
  938. else if (dec_ret < 0)
  939. {
  940. LOG_ERROR("Error decoding video frame");
  941. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  942. m_bThreadStatus = TH_ENDED;
  943. break;
  944. };
  945. sws_scale(m_pVideoState->m_pSwsCtx, (const uint8_t * const *) pFrame->data, pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize);
  946. try
  947. {
  948. if (m_pMediaDestination)
  949. {
  950. memcpy(mRGB.data, pFrameBGR->data[0], pFrameBGR->linesize[0] * m_nOutHeight);
  951. if (! m_pMediaDestination->pushFrame(mRGB, m_pOpaque))
  952. {
  953. m_pMediaDestination = NULL;
  954. LOG_ERROR("Error Consume Frame by Media Destination!");
  955. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  956. m_bThreadStatus = TH_ENDED;
  957. };
  958. };
  959. }
  960. catch (cv::Exception & e)
  961. {
  962. LOG_ERROR(e.msg.c_str());
  963. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  964. m_pMediaDestination = NULL;
  965. m_bThreadStatus = TH_ENDED;
  966. break;
  967. };
  968. };
  969. };
  970. };
  971. av_packet_unref( Packet );
  972. }
  973. else if (dec_ret != AVERROR(EAGAIN) && dec_ret != AVERROR_EOF)
  974. {
  975. LOG_ERROR("Error reading video package");
  976. char tBuffer[128];
  977. av_strerror(dec_ret, tBuffer, 128);
  978. LOG_ERROR(tBuffer);
  979. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  980. m_bThreadStatus = TH_ENDED;
  981. }
  982. else
  983. {
  984. if (dec_ret == AVERROR_EOF)
  985. {
  986. avformat_close_input(&m_pVideoState->m_pFormatCtx);
  987. if ((dec_ret = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts)) != 0)
  988. {
  989. char tBuffer[128];
  990. av_strerror(dec_ret, tBuffer, 128);
  991. sprintf(tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, dec_ret, m_pVideoState->m_pFileName);
  992. LOG_ERROR(tB);
  993. m_pVideoState->m_bStatus = MS_CLOSING;
  994. m_bThreadStatus = TH_ENDED;
  995. };
  996. };
  997. };
  998. SLEEP(5);
  999. }
  1000. else
  1001. {
  1002. SLEEP(5);
  1003. }
  1004. };
  1005. #ifdef DEBUG
  1006. LOG("Video Source Ended!");
  1007. #endif
  1008. if ( Packet ) {
  1009. av_packet_free( &Packet );
  1010. };
  1011. if (pFrame != NULL)
  1012. {
  1013. av_frame_free(&pFrame);
  1014. };
  1015. if (pFrameBGR)
  1016. {
  1017. if (pFrameBGR->data[0])
  1018. {
  1019. av_freep(&pFrameBGR->data[0]);
  1020. }
  1021. av_frame_free(&pFrameBGR);
  1022. };
  1023. if (pBGRbuffer != NULL)
  1024. {
  1025. av_free(pBGRbuffer);
  1026. };
  1027. m_bThreadStatus = TH_ENDED;
  1028. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  1029. #ifdef DEBUG
  1030. LOG("Video Structures Cleared!");
  1031. #endif
  1032. }
  1033. /*
  1034. 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.
  1035. int save_frame_as_jpeg( AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) {
  1036. AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
  1037. if (!jpegCodec) {
  1038. return -1;
  1039. }
  1040. AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
  1041. if (!jpegContext) {
  1042. return -1;
  1043. }
  1044. jpegContext->pix_fmt = pCodecCtx->pix_fmt;
  1045. jpegContext->height = pFrame->height;
  1046. jpegContext->width = pFrame->width;
  1047. if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
  1048. return -1;
  1049. }
  1050. FILE *JPEGFile;
  1051. char JPEGFName[256];
  1052. AVPacket packet = {.data = NULL, .size = 0};
  1053. av_init_packet(&packet);
  1054. int gotFrame;
  1055. if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) {
  1056. return -1;
  1057. }
  1058. sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo);
  1059. JPEGFile = fopen(JPEGFName, "wb");
  1060. fwrite(packet.data, 1, packet.size, JPEGFile);
  1061. fclose(JPEGFile);
  1062. av_free_packet(&packet);
  1063. avcodec_close(jpegContext);
  1064. return 0;
  1065. }
  1066. */
  1067. MediaStateStatus videoAVLIBSource::getStatus(void)
  1068. {
  1069. if (m_pVideoState)
  1070. {
  1071. return m_pVideoState->m_bStatus;
  1072. }
  1073. return MS_UNDEFINED;
  1074. }
  1075. bool videoAVLIBSource::isFinished(void)
  1076. {
  1077. if (m_pVideoState)
  1078. {
  1079. if (m_pVideoState->m_bStatus < MS_CLOSING)
  1080. {
  1081. return false;
  1082. }
  1083. }
  1084. return true;
  1085. }
  1086. #undef LOG
  1087. #undef LOG_ERROR
  1088. //#undef DEBUG_EXTRA
  1089. #endif //! LRF_PROXY_ONLY