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.

777 lines
29 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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::_open( char *URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, int s_width, int s_height ){
  175. if (m_pVideoState == NULL) return false;
  176. if (m_bThreadStatus > TH_PRE_INIT ) return false;
  177. if (m_pVideoState->m_bStatus == MS_INIT) {
  178. char Buffer[1024] = {0};
  179. char tChar[128] = {0};
  180. sprintf( m_pCookie, "%s", MD5String(URI) );
  181. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENING;
  182. if( strncmp( URI, "video=", 6 ) == 0 ){
  183. m_bSrcType = AVLIB_SOURCE_DDSHOW;
  184. #ifdef DEBUG
  185. LOG("MS-Video Device");
  186. #endif
  187. AVDictionary *options = NULL;
  188. if( width != -1 && height != -1 ){
  189. m_bSizeIsAutomatic = false;
  190. char tChar[10];
  191. sprintf(tChar, "%dx%d", width, height);
  192. av_dict_set(&options, "video_size", tChar, 0);
  193. } else {
  194. //m_bSizeIsAutomatic = true;
  195. };
  196. //av_dict_set(&options, "pixel_format", "yuyv422", 0 );
  197. av_dict_set(&options, "video_codec", "mjpeg", 0 );
  198. if( inputPin != -1 ){
  199. sprintf(tChar, "%d", inputPin);
  200. av_dict_set(&options, "crossbar_video_input_pin_number", tChar, 0);
  201. };
  202. if( framerate != -1 ){
  203. sprintf(tChar, "%f", framerate);
  204. av_dict_set(&options, "framerate", tChar, 0);
  205. };
  206. av_dict_set(&options, "rtbufsize", "500000k", 0);
  207. if( ShowDeviceMenu ){
  208. av_dict_set(&options, "show_video_device_dialog", "true", 0);
  209. av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
  210. //sprintf(Buffer, "data/%s.settings", m_pCookie);
  211. //av_dict_set(&options, "video_device_save", Buffer, 0);
  212. #ifndef _NOAUDIO_
  213. av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
  214. sprintf(Buffer, "%s-a.settings", URI);
  215. av_dict_set(&options, "audio_device_save", Buffer, 0);
  216. #endif
  217. } else {
  218. sprintf(Buffer, "data/%s.settings", m_pCookie );
  219. //if (!fileExist(Buffer)) av_dict_set(&options, "video_device_load", Buffer, 0);
  220. };
  221. //av_dict_set(&options, "crossbar_video_input_pin_number", "1", 0);
  222. //av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
  223. //av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
  224. AVInputFormat *iformat = av_find_input_format("dshow");
  225. //av_dict_set(&options, "pixel_format", "bgr24", 0);
  226. av_dict_set(&options, "timeout", "5000", 0);
  227. //AVDeviceInfoList *device_list;
  228. //m_pVideoState->m_pFormatCtx->iformat = iformat;
  229. //avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
  230. //avdevice_free_list_devices(&device_list);
  231. if( avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, iformat, &options) != 0 ){
  232. av_dict_free(&options);
  233. //av_free( iformat );
  234. sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI);
  235. LOG_ERROR(Buffer);
  236. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  237. return false;
  238. };
  239. /*AVDictionaryEntry *e;
  240. if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  241. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  242. LOG_ERROR(Buffer);
  243. };*/
  244. //if (iformat!=NULL)
  245. //if (iformat->priv_class != NULL){
  246. // const AVOption *cOption = NULL;
  247. // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
  248. // serializeOption(cOption);
  249. //};
  250. av_dict_free(&options);
  251. AVOption *mpOption = NULL;
  252. } else {
  253. if( strncmp( URI, "/dev/video", 10 ) == 0 ) {
  254. m_bSrcType = AVLIB_SOURCE_V4L2;
  255. } else {
  256. m_bSrcType = AVLIB_SOURCE_IPSTREAM;
  257. };
  258. //AVDictionary *options = NULL;
  259. //av_dict_set(&m_pVideoState->m_pFormatOpts, "movflags", "faststart", 0);
  260. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ) {
  261. #ifdef DEBUG
  262. LOG("IP Stream Source");
  263. #endif
  264. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtsp_transport", "tcp", 0);
  265. av_dict_set(&m_pVideoState->m_pFormatOpts, "stimeout", "5000000", 0);
  266. av_dict_set(&m_pVideoState->m_pFormatOpts, "buffer_size", "4096000", 0);
  267. /*
  268. av_dict_set_int(&opts, "rw_timeout", 5000000, 0);
  269. av_dict_set_int(&opts, "tcp_nodelay", 1, 0);
  270. av_dict_set_int(&opts, "stimeout", 10000000, 0);
  271. av_dict_set(&opts, "user_agent", "Mozilla/5.0", 0);
  272. av_dict_set(&opts, "rtsp_transport", "tcp", 0);
  273. av_dict_set(&opts, "rtsp_flags", "prefer_tcp", 0);
  274. av_dict_set_int(&opts, "buffer_size", BUFSIZE, 0);
  275. pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;
  276. pFormatCtx->flags |= AVFMT_FLAG_DISCARD_CORRUPT;
  277. pFormatCtx->flags |= AVFMT_FLAG_NOBUFFER;
  278. */
  279. } else {
  280. #ifdef DEBUG
  281. LOG("Linux Video Device");
  282. #endif
  283. if( width != -1 && height != -1 ){
  284. m_bSizeIsAutomatic = false;
  285. sprintf(tChar, "%dx%d", width, height);
  286. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  287. switch ( inputPin ) {
  288. case 0: LOG("PIX YUYV"); av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuv420p", 0); break;
  289. case 1: LOG("PIX MJPG"); av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "mjpeg", 0); break;
  290. default: av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "rgb24", 0); break;
  291. };
  292. } else {
  293. //m_bSizeIsAutomatic = true;
  294. };
  295. av_dict_set(&m_pVideoState->m_pFormatOpts, "fflags", "nobuffer", 0);
  296. };
  297. av_dict_set(&m_pVideoState->m_pFormatOpts, "max_delay", "100", 0);
  298. //av_dict_set(&options, "analyzeduration", "1000000", 0);
  299. if( framerate != -1 ){
  300. sprintf(tChar, "%0.2f", framerate);
  301. //sprintf(tChar, "120" );
  302. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  303. //av_dict_set(&options, "frame_rate", tChar, 0);
  304. };
  305. //av_dict_set(&options, "timeout", "8000", 0);
  306. int RetVal = 0;
  307. strncpy( m_pVideoState->m_pFileName, URI, MAX_PATH_LEN );
  308. //LOG("Before Open1");
  309. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  310. //av_dict_free(&options);
  311. char tBuffer[1024];
  312. av_strerror( RetVal, tBuffer, 1024 );
  313. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  314. LOG_ERROR(Buffer);
  315. return false;
  316. };
  317. //LOG("After Open1");
  318. AVDictionaryEntry *e;
  319. if( e = av_dict_get( m_pVideoState->m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  320. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  321. LOG_ERROR(Buffer);
  322. };
  323. //av_dict_free( &e );
  324. };
  325. #ifdef DEBUG
  326. LOG("DataStream Opened");
  327. #endif
  328. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  329. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  330. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  331. LOG_ERROR(Buffer);
  332. return false;
  333. };
  334. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  335. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  336. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  337. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  338. LOG( "VIDEO stream detected." );
  339. if( m_pVideoState->m_iVideoStreamID <0 ){
  340. LOG( "VIDEO stream assigned." );
  341. m_pVideoState->m_iVideoStreamID=i;
  342. };
  343. };
  344. # ifndef _NOAUDIO_
  345. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  346. LOG( "AUDIO stream detected." );
  347. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  348. LOG( "AUDIO stream assigned." );
  349. m_pVideoState->m_iAudioStreamID=i;
  350. }
  351. };
  352. # endif
  353. };
  354. if( m_pVideoState->m_iVideoStreamID == -1 ){
  355. LOG_ERROR( "Didn't find a video stream.");
  356. return false;
  357. };
  358. # ifndef _NOAUDIO_
  359. if( m_pVideoState->m_iAudioStreamID == -1 ){
  360. LOG_ERROR("Didn't find a audio stream.");
  361. return false;
  362. };
  363. # endif
  364. // Stream found
  365. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  366. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  367. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  368. #else
  369. # ifdef DEBUG_EXTRA
  370. LOG("YABABA-2!");
  371. # endif
  372. /* find the video stream information */
  373. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  374. if (m_pVideoState->m_iVideoStreamID == -1){
  375. LOG_ERROR("Didn't find a video stream.");
  376. return false;
  377. };
  378. # ifndef _NOAUDIO_
  379. if (m_pVideoState->m_iAudioStreamID == -1){
  380. LOG_ERROR("Didn't find a audio stream.");
  381. return false;
  382. };
  383. # endif
  384. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  385. LOG_ERROR("Can not create decoder context!");
  386. return false;
  387. };
  388. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  389. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  390. LOG_ERROR("Can not set Context parameters!");
  391. return false;
  392. };
  393. #endif
  394. if( m_pVideoState->m_pVideoCodec == NULL ){
  395. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  396. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  397. LOG_ERROR( Buffer );
  398. return false;
  399. };
  400. /*
  401. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  402. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  403. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  404. */
  405. //m_pVideoState->m_pCodecCtx->thread_count = 2;
  406. //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  407. #ifdef DEBUG_EXTRA
  408. LOG("Before Open2");
  409. #endif
  410. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  411. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  412. LOG_ERROR( Buffer );
  413. return false;
  414. };
  415. /* we do not send complete frames */
  416. if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  417. #ifndef _NOAUDIO_
  418. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  419. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  420. if( !m_pVideoState->m_pAudioCodec ){
  421. LOG_ERROR( "Unsupported Audio codec!" );
  422. return false;
  423. };
  424. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  425. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  426. LOG_ERROR( "Could not open Audio codec." );
  427. return false;
  428. };
  429. #endif
  430. #ifdef DEBUG_EXTRA
  431. char tB[256];
  432. sprintf( tB, "Setting Frame [%dx%d]", m_nWidth, m_nHeight );
  433. LOG( tB );
  434. sprintf( tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height );
  435. LOG( tB );
  436. #endif
  437. if( ( s_width > 0 ) && ( s_height > 0 ) ){
  438. m_nOutWidth = s_width;
  439. m_nOutHeight = s_height;
  440. } else {
  441. if( m_bSizeIsAutomatic ){
  442. #ifdef DEBUG_EXTRA
  443. LOG("Size is Automatic");
  444. #endif
  445. if(( m_nOutWidth == -1 )&&( m_nOutHeight == -1 )){
  446. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  447. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  448. } else {
  449. // Proportional
  450. if( m_nOutHeight == -1 ){
  451. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  452. m_nOutHeight = (int)(((float)m_nOutWidth ) * k );
  453. } else {
  454. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  455. m_nOutWidth = (int)(((float)m_nOutHeight ) * k );
  456. };
  457. };
  458. } else {
  459. #ifdef DEBUG_EXTRA
  460. LOG("Size is Manual");
  461. #endif
  462. if( ( width > 0 ) && ( height > 0 ) ){
  463. m_nOutWidth = width;
  464. m_nOutHeight = height;
  465. } else {
  466. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  467. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  468. };
  469. };
  470. };
  471. #ifdef DEBUG_EXTRA
  472. sprintf( tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight );
  473. LOG( tB );
  474. sprintf(tB,"swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width,
  475. m_pVideoState->m_pCodecCtx->height,
  476. m_pVideoState->m_pCodecCtx->pix_fmt,
  477. m_nOutWidth,
  478. m_nOutHeight,
  479. AV_PIX_FMT_BGR24,
  480. SWS_BICUBIC );
  481. LOG( tB );
  482. #endif
  483. m_pVideoState->m_pSwsCtx = sws_getContext(
  484. m_pVideoState->m_pCodecCtx->width,
  485. m_pVideoState->m_pCodecCtx->height,
  486. m_pVideoState->m_pCodecCtx->pix_fmt,
  487. m_nOutWidth,
  488. m_nOutHeight,
  489. AV_PIX_FMT_BGR24,
  490. SWS_BICUBIC, //SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  491. NULL,
  492. NULL,
  493. NULL
  494. );
  495. if ( m_pVideoState->m_pSwsCtx == NULL ){
  496. LOG_ERROR("Error Allocating SwsCtx!");
  497. return false;
  498. };
  499. #ifdef DEBUG
  500. LOG("Allocation Done->Start Thread!");
  501. #endif
  502. return init();
  503. };
  504. return false;
  505. };
  506. void videoAVLIBSource::m_fThreadProc( void ){
  507. AVPacket * pPacket = NULL;
  508. cv::Mat mRGB( m_nOutHeight, m_nOutWidth, CV_8UC3, cv::Scalar( 255, 0, 255 ) );
  509. AVFrame * pFrame = NULL;
  510. AVFrame * pFrameBGR = NULL;
  511. uint8_t * pBGRbuffer = NULL;
  512. int dec_ret;
  513. char tB[256];
  514. m_bThreadStatus = TH_WORKING;
  515. pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) );
  516. if ( pPacket == NULL ) {
  517. m_bThreadStatus = TH_ENDED;
  518. LOG_ERROR("Error allocating AVPacket!");
  519. } else {
  520. av_init_packet( pPacket );
  521. pFrame = av_frame_alloc();
  522. if ( pFrame == NULL ) {
  523. m_bThreadStatus = TH_ENDED;
  524. LOG_ERROR("Error allocating SRC AVFrame!");
  525. } else {
  526. pFrameBGR = av_frame_alloc();
  527. if ( pFrameBGR == NULL ) {
  528. m_bThreadStatus = TH_ENDED;
  529. LOG_ERROR("Error allocating BGR AVFrame!");
  530. } else {
  531. dec_ret = av_image_alloc ( pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN );
  532. sprintf( tB, "Allocated: %d bytes to Frame", dec_ret );
  533. LOG( tB );
  534. if ( dec_ret < 0 ) {
  535. m_bThreadStatus = TH_ENDED;
  536. LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  537. } else {
  538. //pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  539. //if ( pBGRbuffer == NULL ) {
  540. // m_bThreadStatus = TH_ENDED;
  541. // LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  542. //} else {
  543. // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN );
  544. //
  545. // pFrameBGR->width = m_nWidth;
  546. // pFrameBGR->height = m_nHeight;
  547. // m_pVideoState->m_bStatus = MS_ALOCATED;
  548. #ifdef DEBUG
  549. LOG("Video Reader Thread Started.");
  550. #endif
  551. };
  552. };
  553. };
  554. };
  555. //cv::setBreakOnError( true );
  556. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  557. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  558. SLEEP(20);
  559. #ifdef DEBUG
  560. LOG("Waiting Destination");
  561. #endif
  562. };
  563. m_eMediaStatus = m_pVideoState->m_bStatus = MS_PRESENT;
  564. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ){
  565. //av_read_play( m_pVideoState->m_pFormatCtx );
  566. };
  567. #ifdef DEBUG
  568. LOG("Presentation Started.");
  569. #endif
  570. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  571. //LOG("1");
  572. if ( m_pMediaDestination != NULL ){
  573. //LOG("2");
  574. dec_ret = av_read_frame( m_pVideoState->m_pFormatCtx, pPacket );
  575. //LOG("3");
  576. if( dec_ret >= 0 ){
  577. //LOG("4");
  578. if( pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
  579. //LOG("5");
  580. m_pVideoState->m_bStatus = MS_PRESENT;
  581. dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, pPacket );
  582. //LOG("6");
  583. if( dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */ ){
  584. LOG_ERROR( "Error sending video package to decoder" );
  585. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  586. m_bThreadStatus = TH_ENDED;
  587. } else {
  588. //LOG("7");
  589. while( dec_ret >= 0 && m_bThreadStatus != TH_ENDED ) {
  590. //LOG("8");
  591. dec_ret = avcodec_receive_frame( m_pVideoState->m_pCodecCtx, pFrame );
  592. //LOG("9");
  593. if( dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF ){
  594. //LOG("S");
  595. break;//Skip to next data
  596. } else if( dec_ret < 0 ){
  597. LOG_ERROR("Error decoding video frame");
  598. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  599. m_bThreadStatus = TH_ENDED;
  600. break;
  601. };
  602. //LOG("10");
  603. sws_scale( m_pVideoState->m_pSwsCtx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize );
  604. //LOG("11");
  605. try{
  606. //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 );
  607. //LOG(tB);
  608. memcpy( mRGB.data, pFrameBGR->data[0], pFrameBGR->linesize[0] * m_nOutHeight );
  609. //LOG("13");
  610. if(!m_pMediaDestination->pushFrame( mRGB, m_pOpaque )){
  611. m_pMediaDestination = NULL;
  612. LOG_ERROR("Error Consume Frame by Media Destination!");
  613. };
  614. //LOG("14");
  615. } catch (cv::Exception & e) {
  616. LOG_ERROR( e.msg.c_str() );
  617. };
  618. };
  619. };
  620. };
  621. } else if( dec_ret != AVERROR(EAGAIN) && dec_ret != AVERROR_EOF ){
  622. LOG_ERROR( "Error reading video package" );
  623. char tBuffer[1024];
  624. av_strerror( dec_ret, tBuffer, 1024 );
  625. LOG_ERROR( tBuffer );
  626. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  627. m_bThreadStatus = TH_ENDED;
  628. } else {
  629. if( dec_ret == AVERROR_EOF ){
  630. avformat_close_input( &m_pVideoState->m_pFormatCtx );
  631. if( ( dec_ret = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  632. char tBuffer[1024];
  633. av_strerror( dec_ret, tBuffer, 1024 );
  634. sprintf( tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, dec_ret, m_pVideoState->m_pFileName );
  635. LOG_ERROR( tB );
  636. m_pVideoState->m_bStatus = MS_CLOSING;
  637. m_bThreadStatus = TH_ENDED;
  638. };
  639. };
  640. //m_pVideoState->m_bStatus = MS_STOPED;
  641. SLEEP( 5 );
  642. };
  643. av_packet_unref( pPacket );
  644. } else SLEEP( 20 );
  645. };
  646. #ifdef DEBUG
  647. LOG("Video Source Ended!");
  648. #endif
  649. if ( pFrame != NULL ) {
  650. av_frame_free( &pFrame );
  651. };
  652. if ( pPacket != NULL ) {
  653. av_free( pPacket );
  654. };
  655. if( pBGRbuffer != NULL ) {
  656. av_free( pBGRbuffer );
  657. };
  658. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSED;
  659. m_bThreadStatus = TH_ENDED;
  660. #ifdef DEBUG
  661. LOG("Video Structures Cleared!");
  662. #endif
  663. };
  664. /*
  665. 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.
  666. int save_frame_as_jpeg( AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) {
  667. AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
  668. if (!jpegCodec) {
  669. return -1;
  670. }
  671. AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
  672. if (!jpegContext) {
  673. return -1;
  674. }
  675. jpegContext->pix_fmt = pCodecCtx->pix_fmt;
  676. jpegContext->height = pFrame->height;
  677. jpegContext->width = pFrame->width;
  678. if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
  679. return -1;
  680. }
  681. FILE *JPEGFile;
  682. char JPEGFName[256];
  683. AVPacket packet = {.data = NULL, .size = 0};
  684. av_init_packet(&packet);
  685. int gotFrame;
  686. if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) {
  687. return -1;
  688. }
  689. sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo);
  690. JPEGFile = fopen(JPEGFName, "wb");
  691. fwrite(packet.data, 1, packet.size, JPEGFile);
  692. fclose(JPEGFile);
  693. av_free_packet(&packet);
  694. avcodec_close(jpegContext);
  695. return 0;
  696. }
  697. */
  698. MediaStateStatus videoAVLIBSource::getStatus( void ){
  699. if( m_pVideoState ) return m_pVideoState->m_bStatus;
  700. return MS_UNDEFINED;
  701. };
  702. bool videoAVLIBSource::isFinished( void ){
  703. if( m_pVideoState ) if( m_pVideoState->m_bStatus < MS_CLOSING ) return false;
  704. return true;
  705. };
  706. #undef LOG
  707. #undef LOG_ERROR