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.

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