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.

1176 lines
33 KiB

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