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.

1205 lines
35 KiB

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