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.

596 lines
21 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "videoAVLIBSource.h"
  2. #include "libavutil/error.h"
  3. #define LOG( X ) m_pLog->Log( "LibAV_SRC", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "LibAV_SRC", this, _LOG_ERROR, X )
  5. videoAVLIBSource::videoAVLIBSource(): basicMediaSource() {
  6. m_bSizeIsAutomatic = true;
  7. m_nWidth = 640;
  8. m_nHeight = 360;
  9. if (!initLibAV()) return;
  10. m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
  11. if( m_pVideoState == NULL ){
  12. LOG_ERROR("VideoState Memory Allocation Error");
  13. return;
  14. };
  15. m_pVideoState->m_bStatus = MS_UNDEFINED;
  16. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  17. if( m_pVideoState->m_pFormatCtx == NULL ){
  18. LOG_ERROR("VideoFormat Context Memory Allocation Error");
  19. return;
  20. };
  21. m_pFrame = NULL;
  22. m_pFrameBGR = NULL;
  23. img_convert_ctx = NULL;
  24. m_pVideoState->m_iVideoStreamID = -1;
  25. #ifndef _NOAUDIO_
  26. m_pVideoState->m_iAudioStreamID = -1;
  27. #endif
  28. m_pVideoState->m_bStatus = MS_INIT;
  29. };
  30. videoAVLIBSource::videoAVLIBSource(char *Source) : basicMediaSource(){
  31. m_bStreamingSource = true;
  32. m_bSizeIsAutomatic = true;
  33. m_nWidth = 640;
  34. m_nHeight = 360;
  35. if( !initLibAV() ) return;
  36. m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
  37. if (m_pVideoState == NULL){
  38. LOG_ERROR("VideoState Memory Allocation Error");
  39. return;
  40. };
  41. m_pVideoState->m_bStatus = MS_UNDEFINED;
  42. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  43. if (m_pVideoState->m_pFormatCtx == NULL){
  44. LOG_ERROR("VideoCodec Context Memory Allocation Error");
  45. return;
  46. };
  47. m_pVideoState->m_pFormatCtx->video_codec_id = AV_CODEC_ID_MJPEG;
  48. m_pVideoState->m_iVideoStreamID = -1;
  49. #ifndef _NOAUDIO_
  50. m_pVideoState->m_iAudioStreamID = -1;
  51. #endif
  52. m_pVideoState->m_bStatus = MS_INIT;
  53. _open( Source );
  54. };
  55. videoAVLIBSource::~videoAVLIBSource(){
  56. if (m_pVideoState != NULL){
  57. if( m_pVideoState->m_bStatus > MS_INIT ){
  58. m_bThreadStatus = TH_ENDED;
  59. while (m_pVideoState->m_bStatus == MS_PRESENT)
  60. #ifdef __linux__
  61. usleep(1000);
  62. #else
  63. Sleep(1);
  64. #endif
  65. #ifndef _NOAUDIO_
  66. if( m_pVideoState->m_pAudioCtx!=NULL ){
  67. avcodec_close( m_pVideoState->m_pAudioCtx );
  68. //avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  69. };
  70. #endif
  71. if( m_pVideoState->m_pCodecCtx != NULL ){
  72. avcodec_close(m_pVideoState->m_pCodecCtx);
  73. //avcodec_free_context(&m_pVideoState->m_pCodecCtx);
  74. };
  75. if( m_pVideoState->m_pFormatCtx != NULL ){
  76. avformat_close_input(&m_pVideoState->m_pFormatCtx);
  77. //avformat_free_context(m_pVideoState->m_pFormatCtx);
  78. };
  79. av_free(m_pVideoState);
  80. if( m_pFrame!= NULL ) av_frame_free(&m_pFrame);
  81. #ifdef PROCESS_YUV420P
  82. av_frame_free( &m_pFrameYUV );
  83. #endif
  84. #ifdef PROCESS_BGR24
  85. if (m_pFrameBGR != NULL) av_frame_free(&m_pFrameBGR);
  86. #endif
  87. //av_free( m_pOut_buffer );
  88. if( img_convert_ctx != NULL ) sws_freeContext(img_convert_ctx);
  89. };
  90. };
  91. };
  92. bool videoAVLIBSource::setSize(int Width, int Height){
  93. m_bSizeIsAutomatic = false;
  94. return true;
  95. };
  96. bool videoAVLIBSource::setProportionalByWidth(int Width){
  97. m_bSizeIsAutomatic = false;
  98. return true;
  99. };
  100. bool videoAVLIBSource::setProportionalByHeight(int Height){
  101. m_bSizeIsAutomatic = false;
  102. return true;
  103. };
  104. bool videoAVLIBSource::setFrameAspectRatio(FrameAspectRatio AspectRatio){
  105. return true;
  106. };
  107. bool videoAVLIBSource::_close(void){
  108. if (m_pVideoState == NULL) return true;
  109. if (m_pVideoState->m_bStatus < MS_OPENED) return true;
  110. m_pVideoState->m_bStatus = MS_CLOSING;
  111. while( m_pVideoState->m_bStatus != MS_CLOSED ) SLEEP(2);
  112. if (m_pVideoState->m_bStatus > MS_ALOCATED){
  113. av_frame_free(&m_pFrame);
  114. #ifdef PROCESS_YUV420P
  115. av_frame_free(&m_pFrameYUV);
  116. #endif
  117. #ifdef PROCESS_BGR24
  118. av_frame_free(&m_pFrameBGR);
  119. #endif
  120. sws_freeContext(img_convert_ctx);
  121. #ifndef _NOAUDIO_
  122. if (m_pVideoState->m_pAudioCtx != NULL){
  123. avcodec_close(m_pVideoState->m_pAudioCtx);
  124. //avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  125. };
  126. #endif
  127. if (m_pVideoState->m_pCodecCtx != NULL){
  128. avcodec_close(m_pVideoState->m_pCodecCtx);
  129. //avcodec_free_context(&m_pVideoState->m_pCodecCtx);
  130. avformat_close_input(&m_pVideoState->m_pFormatCtx);
  131. //av_free(m_pVideoState->m_pVideoCodec);
  132. m_pVideoState->m_iVideoStreamID = -1;
  133. };
  134. };
  135. m_pVideoState->m_bStatus = MS_INIT;
  136. return true;
  137. };
  138. bool videoAVLIBSource::_stop(void){
  139. if ( _close() ) basicThread::waitThreadEnd();
  140. else return false;
  141. return true;
  142. };
  143. //https://www.youtube.com/watch?v=Lu3LZmhC_DY
  144. bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, bool scaled, int s_width, int s_height ){
  145. if (m_pVideoState == NULL) return false;
  146. if (m_pVideoState->m_bStatus == MS_INIT){
  147. char Buffer[256] = {0};
  148. char tChar[128] = {0};
  149. sprintf(m_pCookie, "%s", MD5String(URI));
  150. m_pVideoState->m_bStatus = MS_OPENING;
  151. if( strncmp( URI, "video=", 6 ) == 0 ){
  152. // MS-Video Device
  153. AVDictionary *options = NULL;
  154. if( width != -1 && height != -1 ){
  155. m_bSizeIsAutomatic = false;
  156. char tChar[10];
  157. sprintf(tChar, "%dx%d", width, height);
  158. av_dict_set(&options, "video_size", tChar, 0);
  159. } else {
  160. m_bSizeIsAutomatic = true;
  161. };
  162. //av_dict_set(&options, "pixel_format", "yuyv422", 0 );
  163. av_dict_set(&options, "video_codec", "mjpeg", 0 );
  164. if( inputPin != -1 ){
  165. sprintf(tChar, "%d", inputPin);
  166. av_dict_set(&options, "crossbar_video_input_pin_number", tChar, 0);
  167. };
  168. if( framerate != -1 ){
  169. sprintf(tChar, "%f", framerate);
  170. av_dict_set(&options, "framerate", tChar, 0);
  171. };
  172. av_dict_set(&options, "rtbufsize", "500000k", 0);
  173. if( ShowDeviceMenu ){
  174. av_dict_set(&options, "show_video_device_dialog", "true", 0);
  175. av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
  176. //sprintf(Buffer, "data/%s.settings", m_pCookie);
  177. //av_dict_set(&options, "video_device_save", Buffer, 0);
  178. #ifndef _NOAUDIO_
  179. av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
  180. sprintf(Buffer, "%s-a.settings", URI);
  181. av_dict_set(&options, "audio_device_save", Buffer, 0);
  182. #endif
  183. } else {
  184. sprintf(Buffer, "data/%s.settings", m_pCookie );
  185. //if (!fileExist(Buffer)) av_dict_set(&options, "video_device_load", Buffer, 0);
  186. };
  187. //av_dict_set(&options, "crossbar_video_input_pin_number", "1", 0);
  188. //av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
  189. //av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
  190. AVInputFormat *iformat = av_find_input_format("dshow");
  191. //av_dict_set(&options, "pixel_format", "bgr24", 0);
  192. av_dict_set(&options, "timeout", "5000", 0);
  193. //AVDeviceInfoList *device_list;
  194. //m_pVideoState->m_pFormatCtx->iformat = iformat;
  195. //avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
  196. //avdevice_free_list_devices(&device_list);
  197. if( avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, iformat, &options) != 0 ){
  198. av_dict_free(&options);
  199. av_free( iformat );
  200. sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI);
  201. LOG_ERROR(Buffer);
  202. m_pVideoState->m_bStatus = MS_INIT;
  203. return false;
  204. };
  205. /*AVDictionaryEntry *e;
  206. if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  207. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  208. LOG_ERROR(Buffer);
  209. };*/
  210. //if (iformat!=NULL)
  211. //if (iformat->priv_class != NULL){
  212. // const AVOption *cOption = NULL;
  213. // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
  214. // serializeOption(cOption);
  215. //};
  216. av_dict_free(&options);
  217. AVOption *mpOption = NULL;
  218. } else {
  219. // Linux Video Device Or IP Stream Source
  220. AVDictionary *options = NULL;
  221. if( width != -1 && height != -1 ){
  222. m_bSizeIsAutomatic = false;
  223. sprintf(tChar, "%dx%d", width, height);
  224. av_dict_set(&options, "video_size", tChar, 0);
  225. } else {
  226. m_bSizeIsAutomatic = true;
  227. };
  228. //av_dict_set(&options, "rtsp_transport", "tcp", 0);
  229. av_dict_set(&options, "max_delay", "300000", 0);
  230. //av_dict_set(&options, "stimeout", "1000000", 0);
  231. av_dict_set(&options, "fflags", "nobuffer", 0);
  232. //av_dict_set(&options, "analyzeduration", "1000000", 0);
  233. if( framerate != -1 ){
  234. sprintf(tChar, "%0.2f", framerate);
  235. //sprintf(tChar, "120" );
  236. av_dict_set(&options, "framerate", tChar, 0);
  237. //av_dict_set(&options, "frame_rate", tChar, 0);
  238. };
  239. //av_dict_set(&options, "pixel_format", "mjpeg", 0);
  240. //av_dict_set(&options, "timeout", "8000", 0);
  241. int RetVal = 0;
  242. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, NULL, &options) ) != 0 ){
  243. av_dict_free(&options);
  244. char tBuffer[1024];
  245. av_strerror( RetVal, tBuffer, 1024 );
  246. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, URI);
  247. LOG_ERROR(Buffer);
  248. return false;
  249. };
  250. AVDictionaryEntry *e;
  251. if( e = av_dict_get( options, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  252. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  253. LOG_ERROR(Buffer);
  254. };
  255. av_dict_free(&options);
  256. };
  257. //DataStream Opened !
  258. m_pVideoState->m_bStatus = MS_OPENED;
  259. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  260. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  261. LOG_ERROR(Buffer);
  262. return false;
  263. };
  264. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  265. //LOG("YABABA-1!");
  266. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  267. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  268. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  269. LOG( "VIDEO stream detected." );
  270. if( m_pVideoState->m_iVideoStreamID <0 ){
  271. LOG( "VIDEO stream assigned." );
  272. m_pVideoState->m_iVideoStreamID=i;
  273. };
  274. };
  275. # ifndef _NOAUDIO_
  276. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  277. LOG( "AUDIO stream detected." );
  278. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  279. LOG( "AUDIO stream assigned." );
  280. m_pVideoState->m_iAudioStreamID=i;
  281. }
  282. };
  283. # endif
  284. };
  285. if( m_pVideoState->m_iVideoStreamID == -1 ){
  286. LOG_ERROR( "Didn't find a video stream.");
  287. return false;
  288. };
  289. # ifndef _NOAUDIO_
  290. if( m_pVideoState->m_iAudioStreamID == -1 ){
  291. LOG_ERROR("Didn't find a audio stream.");
  292. return false;
  293. };
  294. # endif
  295. // Stream found
  296. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  297. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  298. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  299. #else
  300. //LOG("YABABA-2!");
  301. /* find the video stream information */
  302. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  303. if (m_pVideoState->m_iVideoStreamID == -1){
  304. LOG_ERROR("Didn't find a video stream.");
  305. return false;
  306. };
  307. # ifndef _NOAUDIO_
  308. if (m_pVideoState->m_iAudioStreamID == -1){
  309. LOG_ERROR("Didn't find a audio stream.");
  310. return false;
  311. };
  312. # endif
  313. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  314. LOG_ERROR("Can not create decoder context!");
  315. return false;
  316. };
  317. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  318. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  319. LOG_ERROR("Can not set Context parameters!");
  320. return false;
  321. };
  322. #endif
  323. if( m_pVideoState->m_pVideoCodec == NULL ){
  324. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  325. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  326. LOG_ERROR( Buffer );
  327. return false;
  328. };
  329. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  330. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  331. LOG_ERROR( Buffer );
  332. return false;
  333. };
  334. #ifndef _NOAUDIO_
  335. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  336. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  337. if( !m_pVideoState->m_pAudioCodec ){
  338. LOG_ERROR( "Unsupported Audio codec!" );
  339. return false;
  340. };
  341. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  342. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  343. LOG_ERROR( "Could not open Audio codec." );
  344. return false;
  345. };
  346. #endif
  347. m_pFrame = av_frame_alloc();
  348. #ifdef PROCESS_YUV420P
  349. m_pFrameYUV = av_frame_alloc();
  350. #endif
  351. #ifdef PROCESS_BGR24
  352. m_pFrameBGR = av_frame_alloc();
  353. #endif
  354. if( scaled ){
  355. m_nWidth = s_width;
  356. m_nHeight = s_height;
  357. } else {
  358. if (m_bSizeIsAutomatic){
  359. m_nWidth = m_pVideoState->m_pCodecCtx->width;
  360. m_nHeight = m_pVideoState->m_pCodecCtx->height;
  361. } else {
  362. m_nWidth = width;
  363. m_nHeight = height;
  364. };
  365. };
  366. #ifdef PROCESS_YUV420P
  367. m_pOut_buffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_YUV420P, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  368. avpicture_fill((AVPicture *) m_pFrameYUV, m_pOut_buffer, AV_PIX_FMT_YUV420P, m_nWidth, m_nHeight );
  369. #endif
  370. #ifdef PROCESS_BGR24
  371. m_pOut_buffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  372. avpicture_fill( (AVPicture *) m_pFrameBGR, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight );
  373. //av_image_fill_arrays(m_pFrameBGR->data, m_pFrameBGR->linesize, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, 1);
  374. //m_pMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) );
  375. //m_pOMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) );
  376. #endif
  377. m_pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) );
  378. // Printout FF
  379. //av_dump_format(m_pFormatCtx,0,filepath,0);
  380. #ifdef PROCESS_YUV420P
  381. img_convert_ctx = sws_getContext( m_pCodecCtx->width, m_pCodecCtx->height, m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL );
  382. #endif
  383. #ifdef PROCESS_BGR24
  384. img_convert_ctx = sws_getContext(
  385. m_pVideoState->m_pCodecCtx->width,
  386. m_pVideoState->m_pCodecCtx->height,
  387. m_pVideoState->m_pCodecCtx->pix_fmt,
  388. m_nWidth,
  389. m_nHeight,
  390. AV_PIX_FMT_BGR24,
  391. SWS_BICUBIC,
  392. NULL,
  393. NULL,
  394. NULL
  395. );
  396. //img_convert_ctx = sws_getContext(m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height, m_pVideoState->m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
  397. #endif
  398. m_pVideoState->m_bStatus = MS_ALOCATED; // All seems to be good -> allocating mem
  399. //LOG("YABABA!!!!");
  400. if( m_bThreadStatus < TH_WORKING ) init();
  401. //LOG("YABABA232!");
  402. return true;
  403. };
  404. return false;
  405. };
  406. #if !(( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 ))
  407. // The flush packet is a non-NULL packet with size 0 and data NULL
  408. int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt){
  409. int ret;
  410. *got_frame = 0;
  411. if (pkt){
  412. ret = avcodec_send_packet(avctx, pkt);
  413. // In particular, we don't expect AVERROR(EAGAIN), because we read all
  414. // decoded frames with avcodec_receive_frame() until done.
  415. if (ret < 0) return ret == AVERROR_EOF ? 0 : ret;
  416. };
  417. ret = avcodec_receive_frame(avctx, frame);
  418. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return ret;
  419. if (ret >= 0) *got_frame = 1;
  420. return 0;
  421. };
  422. #endif
  423. void videoAVLIBSource::m_fThreadProc( void ){
  424. int RetVal;
  425. int bGotPicture = 0;
  426. m_bThreadStatus = TH_WORKING;
  427. LOG("Reader Thread Started.");
  428. //cv::setBreakOnError( true );
  429. m_pVideoState->m_bStatus = MS_STOPED;
  430. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  431. SLEEP(20);
  432. };
  433. LOG("Presentation Started.");
  434. //m_pVideoState->m_bStatus = MS_PRESENT;
  435. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  436. if ( m_pMediaDestination != NULL ){
  437. if( av_read_frame( m_pVideoState->m_pFormatCtx, m_pPacket ) >= 0 ){
  438. if( m_pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
  439. # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  440. RetVal = avcodec_decode_video2(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket);
  441. # else
  442. RetVal = decode(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket);
  443. #endif
  444. if( RetVal < 0 ){
  445. LOG_ERROR( "MediaSource::DecoderProc - Decode Error." );
  446. m_bThreadStatus = TH_ENDED;
  447. m_pVideoState->m_bStatus = MS_STOPED;
  448. continue;
  449. };
  450. if( m_bThreadStatus == TH_ENDED ){
  451. m_pVideoState->m_bStatus = MS_STOPED;
  452. continue;
  453. };
  454. if (bGotPicture){
  455. #ifdef PROCESS_YUV420P
  456. sws_scale( img_convert_ctx, (const uint8_t* const*) m_pFrame->data, m_pFrame->linesize, 0, m_pCodecCtx->height, m_pFrameYUV->data, m_pFrameYUV->linesize );
  457. SDL_UpdateYUVTexture( m_pTexture, &m_oRect, m_pFrameYUV->data[0], m_pFrameYUV->linesize[0], m_pFrameYUV->data[1], m_pFrameYUV->linesize[1], m_pFrameYUV->data[2], m_pFrameYUV->linesize[2] );
  458. #endif
  459. #ifdef PROCESS_BGR24
  460. sws_scale(img_convert_ctx, (const uint8_t* const*)m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize);
  461. try{
  462. # ifdef USE_QVideoFrame
  463. QVideoFrame *tFrame;
  464. tFrame = new QVideoFrame(m_pFrameBGR->linesize[0] * m_nHeight, QSize(m_nHeight, m_nWidth), m_pFrameBGR->linesize[0], QVideoFrame::Format_BGR24);
  465. memcpy(tFrame->bits(), m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight);
  466. if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame(tFrame, me );
  467. # else
  468. cv::Mat *mRGB;
  469. mRGB = new cv::Mat(m_nHeight, m_nWidth, CV_8UC3);
  470. memcpy(mRGB->data, m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight);
  471. if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame(mRGB, this);
  472. delete(mRGB);
  473. # endif // !USE_QVideoFrame
  474. } catch (cv::Exception & e) {
  475. LOG_ERROR(e.msg.c_str()); // output exception message
  476. };
  477. #endif
  478. //if(!m_bStreamingSource) SLEEP( m_pFrame->pkt_duration / 1000 );
  479. };
  480. };
  481. # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  482. av_free_packet( m_pPacket );
  483. # else
  484. av_packet_unref(m_pPacket);
  485. # endif
  486. } else {
  487. // Read Tale of data
  488. while(1){
  489. # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  490. RetVal = avcodec_decode_video2( m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket );
  491. # else
  492. RetVal = decode(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket);
  493. # endif
  494. if( RetVal < 0 ) break;
  495. if( !bGotPicture ) break;
  496. if( m_bThreadStatus == TH_ENDED ){
  497. m_pVideoState->m_bStatus = MS_STOPED;
  498. break;
  499. };
  500. #ifdef PROCESS_YUV420P
  501. sws_scale(me->img_convert_ctx, (const uint8_t* const*)me->m_pFrame->data, me->m_pFrame->linesize, 0, me->m_pCodecCtx->height, me->m_pFrameYUV->data, me->m_pFrameYUV->linesize);
  502. SDL_UpdateYUVTexture( m_pTexture, &m_oRect, m_pFrameYUV->data[0], m_pFrameYUV->linesize[0], m_pFrameYUV->data[1], m_pFrameYUV->linesize[1], m_pFrameYUV->data[2], m_pFrameYUV->linesize[2] );
  503. #endif
  504. #ifdef PROCESS_BGR24
  505. sws_scale(img_convert_ctx, (const uint8_t* const*)m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize);
  506. try{
  507. cv::Mat *mRGB;
  508. mRGB = new cv::Mat(m_nHeight, m_nWidth, CV_8UC3);
  509. memcpy(mRGB->data, m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight);
  510. if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame( mRGB, this );
  511. delete(mRGB);
  512. } catch (cv::Exception & e) {
  513. LOG_ERROR(e.msg.c_str()); // output exception message
  514. };
  515. #endif
  516. };
  517. LOG("Video Stream Ended");
  518. # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  519. av_free_packet(m_pPacket);
  520. # else
  521. av_packet_unref(m_pPacket);
  522. # endif
  523. m_pVideoState->m_bStatus = MS_CLOSED;
  524. m_bThreadStatus = TH_ENDED;
  525. };
  526. }else{
  527. m_pVideoState->m_bStatus = MS_CLOSED;
  528. #ifdef __linux__
  529. usleep( 20000 );
  530. #else
  531. Sleep(20);
  532. #endif
  533. };
  534. };
  535. m_pVideoState->m_bStatus = MS_CLOSED;
  536. m_bThreadStatus = TH_ENDED;
  537. LOG("Thread Ended.");
  538. };
  539. #undef LOG
  540. #undef LOG_ERROR