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.

954 lines
37 KiB

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