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.

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