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

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