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.

782 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 = 4;
  406. //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  407. //if (is_live)
  408. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  409. //else
  410. //m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  411. #ifdef DEBUG_EXTRA
  412. LOG("Before Open2");
  413. #endif
  414. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  415. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  416. LOG_ERROR( Buffer );
  417. return false;
  418. };
  419. /* we do not send complete frames */
  420. if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  421. #ifndef _NOAUDIO_
  422. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  423. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  424. if( !m_pVideoState->m_pAudioCodec ){
  425. LOG_ERROR( "Unsupported Audio codec!" );
  426. return false;
  427. };
  428. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  429. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  430. LOG_ERROR( "Could not open Audio codec." );
  431. return false;
  432. };
  433. #endif
  434. #ifdef DEBUG_EXTRA
  435. char tB[256];
  436. sprintf( tB, "Setting Frame [%dx%d]", m_nWidth, m_nHeight );
  437. LOG( tB );
  438. sprintf( tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height );
  439. LOG( tB );
  440. #endif
  441. if( ( s_width > 0 ) && ( s_height > 0 ) ){
  442. m_nOutWidth = s_width;
  443. m_nOutHeight = s_height;
  444. } else {
  445. if( m_bSizeIsAutomatic ){
  446. #ifdef DEBUG_EXTRA
  447. LOG("Size is Automatic");
  448. #endif
  449. if(( m_nOutWidth == -1 )&&( m_nOutHeight == -1 )){
  450. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  451. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  452. } else {
  453. // Proportional
  454. if( m_nOutHeight == -1 ){
  455. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  456. m_nOutHeight = (int)(((float)m_nOutWidth ) * k );
  457. } else {
  458. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  459. m_nOutWidth = (int)(((float)m_nOutHeight ) * k );
  460. };
  461. };
  462. } else {
  463. #ifdef DEBUG_EXTRA
  464. LOG("Size is Manual");
  465. #endif
  466. if( ( width > 0 ) && ( height > 0 ) ){
  467. m_nOutWidth = width;
  468. m_nOutHeight = height;
  469. } else {
  470. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  471. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  472. };
  473. };
  474. };
  475. #ifdef DEBUG_EXTRA
  476. sprintf( tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight );
  477. LOG( tB );
  478. sprintf(tB,"swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width,
  479. m_pVideoState->m_pCodecCtx->height,
  480. m_pVideoState->m_pCodecCtx->pix_fmt,
  481. m_nOutWidth,
  482. m_nOutHeight,
  483. AV_PIX_FMT_BGR24,
  484. SWS_BICUBIC );
  485. LOG( tB );
  486. #endif
  487. m_pVideoState->m_pSwsCtx = sws_getContext(
  488. m_pVideoState->m_pCodecCtx->width,
  489. m_pVideoState->m_pCodecCtx->height,
  490. m_pVideoState->m_pCodecCtx->pix_fmt,
  491. m_nOutWidth,
  492. m_nOutHeight,
  493. AV_PIX_FMT_BGR24,
  494. SWS_BICUBIC, //SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  495. NULL,
  496. NULL,
  497. NULL
  498. );
  499. if ( m_pVideoState->m_pSwsCtx == NULL ){
  500. LOG_ERROR("Error Allocating SwsCtx!");
  501. return false;
  502. };
  503. #ifdef DEBUG
  504. LOG("Allocation Done->Start Thread!");
  505. #endif
  506. return init();
  507. };
  508. return false;
  509. };
  510. void videoAVLIBSource::m_fThreadProc( void ){
  511. AVPacket * pPacket = NULL;
  512. cv::Mat mRGB( m_nOutHeight, m_nOutWidth, CV_8UC3, cv::Scalar( 255, 0, 255 ) );
  513. AVFrame * pFrame = NULL;
  514. AVFrame * pFrameBGR = NULL;
  515. uint8_t * pBGRbuffer = NULL;
  516. int dec_ret;
  517. char tB[256];
  518. m_bThreadStatus = TH_WORKING;
  519. pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) );
  520. if ( pPacket == NULL ) {
  521. m_bThreadStatus = TH_ENDED;
  522. LOG_ERROR("Error allocating AVPacket!");
  523. } else {
  524. av_init_packet( pPacket );
  525. pFrame = av_frame_alloc();
  526. if ( pFrame == NULL ) {
  527. m_bThreadStatus = TH_ENDED;
  528. LOG_ERROR("Error allocating SRC AVFrame!");
  529. } else {
  530. pFrameBGR = av_frame_alloc();
  531. if ( pFrameBGR == NULL ) {
  532. m_bThreadStatus = TH_ENDED;
  533. LOG_ERROR("Error allocating BGR AVFrame!");
  534. } else {
  535. dec_ret = av_image_alloc ( pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN );
  536. sprintf( tB, "Allocated: %d bytes to Frame", dec_ret );
  537. LOG( tB );
  538. if ( dec_ret < 0 ) {
  539. m_bThreadStatus = TH_ENDED;
  540. LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  541. } else {
  542. //pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  543. //if ( pBGRbuffer == NULL ) {
  544. // m_bThreadStatus = TH_ENDED;
  545. // LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  546. //} else {
  547. // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN );
  548. //
  549. // pFrameBGR->width = m_nWidth;
  550. // pFrameBGR->height = m_nHeight;
  551. // m_pVideoState->m_bStatus = MS_ALOCATED;
  552. #ifdef DEBUG
  553. LOG("Video Reader Thread Started.");
  554. #endif
  555. };
  556. };
  557. };
  558. };
  559. //cv::setBreakOnError( true );
  560. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  561. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  562. SLEEP(20);
  563. #ifdef DEBUG
  564. LOG("Waiting Destination");
  565. #endif
  566. };
  567. m_eMediaStatus = m_pVideoState->m_bStatus = MS_PRESENT;
  568. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ){
  569. //av_read_play( m_pVideoState->m_pFormatCtx );
  570. };
  571. #ifdef DEBUG
  572. LOG("Presentation Started.");
  573. #endif
  574. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  575. //LOG("1");
  576. if ( m_pMediaDestination != NULL ){
  577. //LOG("2");
  578. dec_ret = av_read_frame( m_pVideoState->m_pFormatCtx, pPacket );
  579. //LOG("3");
  580. if( dec_ret >= 0 ){
  581. //LOG("4");
  582. if( pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
  583. //LOG("5");
  584. m_pVideoState->m_bStatus = MS_PRESENT;
  585. dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, pPacket );
  586. //LOG("6");
  587. if( dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */ ){
  588. LOG_ERROR( "Error sending video package to decoder" );
  589. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  590. m_bThreadStatus = TH_ENDED;
  591. } else {
  592. //LOG("7");
  593. while( dec_ret >= 0 && m_bThreadStatus != TH_ENDED ) {
  594. //LOG("8");
  595. dec_ret = avcodec_receive_frame( m_pVideoState->m_pCodecCtx, pFrame );
  596. //LOG("9");
  597. if( dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF ){
  598. //LOG("S");
  599. break;//Skip to next data
  600. } else if( dec_ret < 0 ){
  601. LOG_ERROR("Error decoding video frame");
  602. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  603. m_bThreadStatus = TH_ENDED;
  604. break;
  605. };
  606. //LOG("10");
  607. sws_scale( m_pVideoState->m_pSwsCtx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize );
  608. //LOG("11");
  609. try{
  610. //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 );
  611. //LOG(tB);
  612. memcpy( mRGB.data, pFrameBGR->data[0], pFrameBGR->linesize[0] * m_nOutHeight );
  613. //LOG("13");
  614. if(!m_pMediaDestination->pushFrame( mRGB, m_pOpaque )){
  615. m_pMediaDestination = NULL;
  616. LOG_ERROR("Error Consume Frame by Media Destination!");
  617. };
  618. //LOG("14");
  619. } catch (cv::Exception & e) {
  620. LOG_ERROR( e.msg.c_str() );
  621. };
  622. };
  623. };
  624. };
  625. } else if( dec_ret != AVERROR(EAGAIN) && dec_ret != AVERROR_EOF ){
  626. LOG_ERROR( "Error reading video package" );
  627. char tBuffer[1024];
  628. av_strerror( dec_ret, tBuffer, 1024 );
  629. LOG_ERROR( tBuffer );
  630. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  631. m_bThreadStatus = TH_ENDED;
  632. } else {
  633. if( dec_ret == AVERROR_EOF ){
  634. avformat_close_input( &m_pVideoState->m_pFormatCtx );
  635. if( ( dec_ret = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  636. char tBuffer[1024];
  637. av_strerror( dec_ret, tBuffer, 1024 );
  638. sprintf( tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, dec_ret, m_pVideoState->m_pFileName );
  639. LOG_ERROR( tB );
  640. m_pVideoState->m_bStatus = MS_CLOSING;
  641. m_bThreadStatus = TH_ENDED;
  642. };
  643. };
  644. //m_pVideoState->m_bStatus = MS_STOPED;
  645. SLEEP( 5 );
  646. };
  647. av_packet_unref( pPacket );
  648. SLEEP( 10 );
  649. } else SLEEP( 10 );
  650. };
  651. #ifdef DEBUG
  652. LOG("Video Source Ended!");
  653. #endif
  654. if ( pFrame != NULL ) {
  655. av_frame_free( &pFrame );
  656. };
  657. if ( pPacket != NULL ) {
  658. av_free( pPacket );
  659. };
  660. if( pBGRbuffer != NULL ) {
  661. av_free( pBGRbuffer );
  662. };
  663. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSED;
  664. m_bThreadStatus = TH_ENDED;
  665. #ifdef DEBUG
  666. LOG("Video Structures Cleared!");
  667. #endif
  668. };
  669. /*
  670. 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.
  671. int save_frame_as_jpeg( AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) {
  672. AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
  673. if (!jpegCodec) {
  674. return -1;
  675. }
  676. AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
  677. if (!jpegContext) {
  678. return -1;
  679. }
  680. jpegContext->pix_fmt = pCodecCtx->pix_fmt;
  681. jpegContext->height = pFrame->height;
  682. jpegContext->width = pFrame->width;
  683. if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
  684. return -1;
  685. }
  686. FILE *JPEGFile;
  687. char JPEGFName[256];
  688. AVPacket packet = {.data = NULL, .size = 0};
  689. av_init_packet(&packet);
  690. int gotFrame;
  691. if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) {
  692. return -1;
  693. }
  694. sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo);
  695. JPEGFile = fopen(JPEGFName, "wb");
  696. fwrite(packet.data, 1, packet.size, JPEGFile);
  697. fclose(JPEGFile);
  698. av_free_packet(&packet);
  699. avcodec_close(jpegContext);
  700. return 0;
  701. }
  702. */
  703. MediaStateStatus videoAVLIBSource::getStatus( void ){
  704. if( m_pVideoState ) return m_pVideoState->m_bStatus;
  705. return MS_UNDEFINED;
  706. };
  707. bool videoAVLIBSource::isFinished( void ){
  708. if( m_pVideoState ) if( m_pVideoState->m_bStatus < MS_CLOSING ) return false;
  709. return true;
  710. };
  711. #undef LOG
  712. #undef LOG_ERROR