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
​
​
​
​
​
​
​
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
​
​
  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_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_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_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_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", "1024000", 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. if( inputPin > 0 ) {
  288. switch ( inputPin ) {
  289. case 1: av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "YUYV", 0); break;
  290. case 2: av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "MJPEG", 0); break;
  291. };
  292. };
  293. } else {
  294. //m_bSizeIsAutomatic = true;
  295. };
  296. av_dict_set(&m_pVideoState->m_pFormatOpts, "fflags", "nobuffer", 0);
  297. };
  298. av_dict_set(&m_pVideoState->m_pFormatOpts, "max_delay", "100", 0);
  299. //av_dict_set(&options, "analyzeduration", "1000000", 0);
  300. if( framerate != -1 ){
  301. sprintf(tChar, "%0.2f", framerate);
  302. //sprintf(tChar, "120" );
  303. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  304. //av_dict_set(&options, "frame_rate", tChar, 0);
  305. };
  306. //av_dict_set(&options, "timeout", "8000", 0);
  307. int RetVal = 0;
  308. strncpy( m_pVideoState->m_pFileName, URI, MAX_PATH_LEN );
  309. //LOG("Before Open1");
  310. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  311. //av_dict_free(&options);
  312. char tBuffer[1024];
  313. av_strerror( RetVal, tBuffer, 1024 );
  314. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  315. LOG_ERROR(Buffer);
  316. return false;
  317. };
  318. //LOG("After Open1");
  319. AVDictionaryEntry *e;
  320. if( e = av_dict_get( m_pVideoState->m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  321. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  322. LOG_ERROR(Buffer);
  323. };
  324. //av_dict_free( &e );
  325. };
  326. #ifdef DEBUG
  327. LOG("DataStream Opened");
  328. #endif
  329. m_pVideoState->m_bStatus = MS_OPENED;
  330. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  331. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  332. LOG_ERROR(Buffer);
  333. return false;
  334. };
  335. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  336. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  337. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  338. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  339. LOG( "VIDEO stream detected." );
  340. if( m_pVideoState->m_iVideoStreamID <0 ){
  341. LOG( "VIDEO stream assigned." );
  342. m_pVideoState->m_iVideoStreamID=i;
  343. };
  344. };
  345. # ifndef _NOAUDIO_
  346. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  347. LOG( "AUDIO stream detected." );
  348. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  349. LOG( "AUDIO stream assigned." );
  350. m_pVideoState->m_iAudioStreamID=i;
  351. }
  352. };
  353. # endif
  354. };
  355. if( m_pVideoState->m_iVideoStreamID == -1 ){
  356. LOG_ERROR( "Didn't find a video stream.");
  357. return false;
  358. };
  359. # ifndef _NOAUDIO_
  360. if( m_pVideoState->m_iAudioStreamID == -1 ){
  361. LOG_ERROR("Didn't find a audio stream.");
  362. return false;
  363. };
  364. # endif
  365. // Stream found
  366. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  367. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  368. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  369. #else
  370. # ifdef DEBUG_EXTRA
  371. LOG("YABABA-2!");
  372. # endif
  373. /* find the video stream information */
  374. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  375. if (m_pVideoState->m_iVideoStreamID == -1){
  376. LOG_ERROR("Didn't find a video stream.");
  377. return false;
  378. };
  379. # ifndef _NOAUDIO_
  380. if (m_pVideoState->m_iAudioStreamID == -1){
  381. LOG_ERROR("Didn't find a audio stream.");
  382. return false;
  383. };
  384. # endif
  385. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  386. LOG_ERROR("Can not create decoder context!");
  387. return false;
  388. };
  389. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  390. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  391. LOG_ERROR("Can not set Context parameters!");
  392. return false;
  393. };
  394. #endif
  395. if( m_pVideoState->m_pVideoCodec == NULL ){
  396. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  397. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  398. LOG_ERROR( Buffer );
  399. return false;
  400. };
  401. /*
  402. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  403. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  404. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  405. */
  406. //m_pVideoState->m_pCodecCtx->thread_count = 2;
  407. //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  408. #ifdef DEBUG_EXTRA
  409. LOG("Before Open2");
  410. #endif
  411. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  412. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  413. LOG_ERROR( Buffer );
  414. return false;
  415. };
  416. /* we do not send complete frames */
  417. if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  418. #ifndef _NOAUDIO_
  419. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  420. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  421. if( !m_pVideoState->m_pAudioCodec ){
  422. LOG_ERROR( "Unsupported Audio codec!" );
  423. return false;
  424. };
  425. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  426. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  427. LOG_ERROR( "Could not open Audio codec." );
  428. return false;
  429. };
  430. #endif
  431. #ifdef DEBUG_EXTRA
  432. char tB[256];
  433. sprintf( tB, "Setting Frame [%dx%d]", m_nWidth, m_nHeight );
  434. LOG( tB );
  435. sprintf( tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height );
  436. LOG( tB );
  437. #endif
  438. if( ( s_width > 0 ) && ( s_height > 0 ) ){
  439. m_nOutWidth = s_width;
  440. m_nOutHeight = s_height;
  441. } else {
  442. if( m_bSizeIsAutomatic ){
  443. #ifdef DEBUG_EXTRA
  444. LOG("Size is Automatic");
  445. #endif
  446. if(( m_nOutWidth == -1 )&&( m_nOutHeight == -1 )){
  447. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  448. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  449. } else {
  450. // Proportional
  451. if( m_nOutHeight == -1 ){
  452. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  453. m_nOutHeight = (int)(((float)m_nOutWidth ) * k );
  454. } else {
  455. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  456. m_nOutWidth = (int)(((float)m_nOutHeight ) * k );
  457. };
  458. };
  459. } else {
  460. #ifdef DEBUG_EXTRA
  461. LOG("Size is Manual");
  462. #endif
  463. if( ( width > 0 ) && ( height > 0 ) ){
  464. m_nOutWidth = width;
  465. m_nOutHeight = height;
  466. } else {
  467. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  468. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  469. };
  470. };
  471. };
  472. #ifdef DEBUG_EXTRA
  473. sprintf( tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight );
  474. LOG( tB );
  475. sprintf(tB,"swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width,
  476. m_pVideoState->m_pCodecCtx->height,
  477. m_pVideoState->m_pCodecCtx->pix_fmt,
  478. m_nOutWidth,
  479. m_nOutHeight,
  480. AV_PIX_FMT_BGR24,
  481. SWS_BICUBIC );
  482. LOG( tB );
  483. #endif
  484. m_pVideoState->m_pSwsCtx = sws_getContext(
  485. m_pVideoState->m_pCodecCtx->width,
  486. m_pVideoState->m_pCodecCtx->height,
  487. m_pVideoState->m_pCodecCtx->pix_fmt,
  488. m_nOutWidth,
  489. m_nOutHeight,
  490. AV_PIX_FMT_BGR24,
  491. SWS_BICUBIC, //SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  492. NULL,
  493. NULL,
  494. NULL
  495. );
  496. if ( m_pVideoState->m_pSwsCtx == NULL ){
  497. LOG_ERROR("Error Allocating SwsCtx!");
  498. return false;
  499. };
  500. #ifdef DEBUG_EXTRA
  501. LOG("Allocation Done->Start Thread!");
  502. #endif
  503. return init();
  504. };
  505. return false;
  506. };
  507. void videoAVLIBSource::m_fThreadProc( void ){
  508. AVPacket * pPacket = NULL;
  509. cv::Mat mRGB( m_nOutHeight, m_nOutWidth, CV_8UC3, cv::Scalar( 255, 0, 255 ) );
  510. AVFrame * pFrame = NULL;
  511. AVFrame * pFrameBGR = NULL;
  512. uint8_t * pBGRbuffer = NULL;
  513. int dec_ret;
  514. char tB[256];
  515. m_bThreadStatus = TH_WORKING;
  516. pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) );
  517. if ( pPacket == NULL ) {
  518. m_bThreadStatus = TH_ENDED;
  519. LOG_ERROR("Error allocating AVPacket!");
  520. } else {
  521. av_init_packet( pPacket );
  522. pFrame = av_frame_alloc();
  523. if ( pFrame == NULL ) {
  524. m_bThreadStatus = TH_ENDED;
  525. LOG_ERROR("Error allocating SRC AVFrame!");
  526. } else {
  527. pFrameBGR = av_frame_alloc();
  528. if ( pFrameBGR == NULL ) {
  529. m_bThreadStatus = TH_ENDED;
  530. LOG_ERROR("Error allocating BGR AVFrame!");
  531. } else {
  532. dec_ret = av_image_alloc ( pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN );
  533. sprintf( tB, "Allocated: %d bytes to Frame", dec_ret );
  534. LOG( tB );
  535. if ( dec_ret < 0 ) {
  536. m_bThreadStatus = TH_ENDED;
  537. LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  538. } else {
  539. //pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  540. //if ( pBGRbuffer == NULL ) {
  541. // m_bThreadStatus = TH_ENDED;
  542. // LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  543. //} else {
  544. // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN );
  545. //
  546. // pFrameBGR->width = m_nWidth;
  547. // pFrameBGR->height = m_nHeight;
  548. // m_pVideoState->m_bStatus = MS_ALOCATED;
  549. #ifdef DEBUG_EXTRA
  550. LOG("Video Reader Thread Started.");
  551. #endif
  552. };
  553. };
  554. };
  555. };
  556. //cv::setBreakOnError( true );
  557. m_pVideoState->m_bStatus = MS_STOPED;
  558. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  559. SLEEP(20);
  560. #ifdef DEBUG_EXTRA
  561. LOG("Waiting Destination");
  562. #endif
  563. };
  564. m_pVideoState->m_bStatus = MS_PRESENT;
  565. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ){
  566. //av_read_play( m_pVideoState->m_pFormatCtx );
  567. };
  568. #ifdef DEBUG_EXTRA
  569. LOG("Presentation Started.");
  570. #endif
  571. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  572. //LOG("1");
  573. if ( m_pMediaDestination != NULL ){
  574. //LOG("2");
  575. dec_ret = av_read_frame( m_pVideoState->m_pFormatCtx, pPacket );
  576. //LOG("3");
  577. if( dec_ret >= 0 ){
  578. //LOG("4");
  579. if( pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
  580. //LOG("5");
  581. m_pVideoState->m_bStatus = MS_PRESENT;
  582. dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, pPacket );
  583. //LOG("6");
  584. if( dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */ ){
  585. LOG_ERROR( "Error sending video package to decoder" );
  586. m_pVideoState->m_bStatus = MS_CLOSING;
  587. m_bThreadStatus = TH_ENDED;
  588. } else {
  589. //LOG("7");
  590. while( dec_ret >= 0 && m_bThreadStatus != TH_ENDED ) {
  591. //LOG("8");
  592. dec_ret = avcodec_receive_frame( m_pVideoState->m_pCodecCtx, pFrame );
  593. //LOG("9");
  594. if( dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF ){
  595. //LOG("S");
  596. break;//Skip to next data
  597. } else if( dec_ret < 0 ){
  598. LOG_ERROR("Error decoding video frame");
  599. m_pVideoState->m_bStatus = MS_CLOSING;
  600. m_bThreadStatus = TH_ENDED;
  601. break;
  602. };
  603. //LOG("10");
  604. sws_scale( m_pVideoState->m_pSwsCtx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize );
  605. //LOG("11");
  606. try{
  607. //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 );
  608. //LOG(tB);
  609. memcpy( mRGB.data, pFrameBGR->data[0], pFrameBGR->linesize[0] * m_nOutHeight );
  610. //LOG("13");
  611. if(!m_pMediaDestination->pushFrame( mRGB, m_pOpaque )){
  612. m_pMediaDestination = NULL;
  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_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_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