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.

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