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.

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