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.

600 lines
21 KiB

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];
  148. sprintf(m_pCookie, "%s", MD5String(URI));
  149. m_pVideoState->m_bStatus = MS_OPENING;
  150. if( strncmp( URI, "video=", 6 ) == 0 ){
  151. // MS-Video Device
  152. AVDictionary *options = NULL;
  153. if( width != -1 && height != -1 ){
  154. m_bSizeIsAutomatic = false;
  155. char tChar[10];
  156. sprintf(tChar, "%dx%d", width, height);
  157. av_dict_set(&options, "video_size", tChar, 0);
  158. } else {
  159. m_bSizeIsAutomatic = true;
  160. };
  161. //av_dict_set(&options, "pixel_format", "yuyv422", 0 );
  162. av_dict_set(&options, "video_codec", "mjpeg", 0 );
  163. if( inputPin != -1 ){
  164. char tChar[4];
  165. sprintf(tChar, "%d", inputPin);
  166. av_dict_set(&options, "crossbar_video_input_pin_number", tChar, 0);
  167. };
  168. if( framerate != -1 ){
  169. char tChar[4];
  170. sprintf(tChar, "%f", framerate);
  171. av_dict_set(&options, "framerate", tChar, 0);
  172. };
  173. av_dict_set(&options, "rtbufsize", "500000k", 0);
  174. if( ShowDeviceMenu ){
  175. av_dict_set(&options, "show_video_device_dialog", "true", 0);
  176. av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
  177. //sprintf(Buffer, "data/%s.settings", m_pCookie);
  178. //av_dict_set(&options, "video_device_save", Buffer, 0);
  179. #ifndef _NOAUDIO_
  180. av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
  181. sprintf(Buffer, "%s-a.settings", URI);
  182. av_dict_set(&options, "audio_device_save", Buffer, 0);
  183. #endif
  184. } else {
  185. sprintf(Buffer, "data/%s.settings", m_pCookie );
  186. //if (!fileExist(Buffer)) av_dict_set(&options, "video_device_load", Buffer, 0);
  187. };
  188. //av_dict_set(&options, "crossbar_video_input_pin_number", "1", 0);
  189. //av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
  190. //av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
  191. AVInputFormat *iformat = av_find_input_format("dshow");
  192. //av_dict_set(&options, "pixel_format", "bgr24", 0);
  193. av_dict_set(&options, "timeout", "5000", 0);
  194. //AVDeviceInfoList *device_list;
  195. //m_pVideoState->m_pFormatCtx->iformat = iformat;
  196. //avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
  197. //avdevice_free_list_devices(&device_list);
  198. if( avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, iformat, &options) != 0 ){
  199. av_dict_free(&options);
  200. av_free( iformat );
  201. sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI);
  202. LOG_ERROR(Buffer);
  203. m_pVideoState->m_bStatus = MS_INIT;
  204. return false;
  205. };
  206. /*AVDictionaryEntry *e;
  207. if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  208. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  209. LOG_ERROR(Buffer);
  210. };*/
  211. //if (iformat!=NULL)
  212. //if (iformat->priv_class != NULL){
  213. // const AVOption *cOption = NULL;
  214. // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
  215. // serializeOption(cOption);
  216. //};
  217. av_dict_free(&options);
  218. AVOption *mpOption = NULL;
  219. } else {
  220. // Linux Video Device Or IP Stream Source
  221. AVDictionary *options = NULL;
  222. if( width != -1 && height != -1 ){
  223. m_bSizeIsAutomatic = false;
  224. char tChar[32];
  225. sprintf(tChar, "%dx%d", width, height);
  226. av_dict_set(&options, "video_size", tChar, 0);
  227. } else {
  228. m_bSizeIsAutomatic = true;
  229. };
  230. //av_dict_set(&options, "rtsp_transport", "tcp", 0);
  231. av_dict_set(&options, "max_delay", "300000", 0);
  232. //av_dict_set(&options, "stimeout", "1000000", 0);
  233. av_dict_set(&options, "fflags", "nobuffer", 0);
  234. //av_dict_set(&options, "analyzeduration", "1000000", 0);
  235. if( framerate != -1 ){
  236. char tChar[10];
  237. sprintf(tChar, "%0.2f", framerate);
  238. //sprintf(tChar, "120" );
  239. av_dict_set(&options, "framerate", tChar, 0);
  240. //av_dict_set(&options, "frame_rate", tChar, 0);
  241. };
  242. av_dict_set(&options, "pixel_format", "mjpeg", 0);
  243. //av_dict_set(&options, "timeout", "8000", 0);
  244. int RetVal = 0;
  245. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, NULL, &options) ) != 0 ){
  246. av_dict_free(&options);
  247. char tBuffer[1024];
  248. av_strerror( RetVal, tBuffer, 1024 );
  249. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, URI);
  250. LOG_ERROR(Buffer);
  251. return false;
  252. };
  253. AVDictionaryEntry *e;
  254. if( e = av_dict_get( options, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  255. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  256. LOG_ERROR(Buffer);
  257. };
  258. av_dict_free(&options);
  259. };
  260. //DataStream Opened !
  261. m_pVideoState->m_bStatus = MS_OPENED;
  262. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  263. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  264. LOG_ERROR(Buffer);
  265. return false;
  266. };
  267. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  268. LOG("YABABA-1!");
  269. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  270. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  271. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  272. LOG( "VIDEO stream detected." );
  273. if( m_pVideoState->m_iVideoStreamID <0 ){
  274. LOG( "VIDEO stream assigned." );
  275. m_pVideoState->m_iVideoStreamID=i;
  276. };
  277. };
  278. # ifndef _NOAUDIO_
  279. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  280. LOG( "AUDIO stream detected." );
  281. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  282. LOG( "AUDIO stream assigned." );
  283. m_pVideoState->m_iAudioStreamID=i;
  284. }
  285. };
  286. # endif
  287. };
  288. if( m_pVideoState->m_iVideoStreamID == -1 ){
  289. LOG_ERROR( "Didn't find a video stream.");
  290. return false;
  291. };
  292. # ifndef _NOAUDIO_
  293. if( m_pVideoState->m_iAudioStreamID == -1 ){
  294. LOG_ERROR("Didn't find a audio stream.");
  295. return false;
  296. };
  297. # endif
  298. // Stream found
  299. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  300. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  301. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  302. #else
  303. LOG("YABABA-2!");
  304. /* find the video stream information */
  305. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  306. if (m_pVideoState->m_iVideoStreamID == -1){
  307. LOG_ERROR("Didn't find a video stream.");
  308. return false;
  309. };
  310. # ifndef _NOAUDIO_
  311. if (m_pVideoState->m_iAudioStreamID == -1){
  312. LOG_ERROR("Didn't find a audio stream.");
  313. return false;
  314. };
  315. # endif
  316. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  317. LOG_ERROR("Can not create decoder context!");
  318. return false;
  319. };
  320. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  321. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  322. LOG_ERROR("Can not set Context parameters!");
  323. return false;
  324. };
  325. #endif
  326. if( m_pVideoState->m_pVideoCodec == NULL ){
  327. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  328. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  329. LOG_ERROR( Buffer );
  330. return false;
  331. };
  332. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  333. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  334. LOG_ERROR( Buffer );
  335. return false;
  336. };
  337. #ifndef _NOAUDIO_
  338. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  339. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  340. if( !m_pVideoState->m_pAudioCodec ){
  341. LOG_ERROR( "Unsupported Audio codec!" );
  342. return false;
  343. };
  344. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  345. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  346. LOG_ERROR( "Could not open Audio codec." );
  347. return false;
  348. };
  349. #endif
  350. m_pFrame = av_frame_alloc();
  351. #ifdef PROCESS_YUV420P
  352. m_pFrameYUV = av_frame_alloc();
  353. #endif
  354. #ifdef PROCESS_BGR24
  355. m_pFrameBGR = av_frame_alloc();
  356. #endif
  357. if( scaled ){
  358. m_nWidth = s_width;
  359. m_nHeight = s_height;
  360. } else {
  361. if (m_bSizeIsAutomatic){
  362. m_nWidth = m_pVideoState->m_pCodecCtx->width;
  363. m_nHeight = m_pVideoState->m_pCodecCtx->height;
  364. } else {
  365. m_nWidth = width;
  366. m_nHeight = height;
  367. };
  368. };
  369. #ifdef PROCESS_YUV420P
  370. m_pOut_buffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_YUV420P, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  371. avpicture_fill((AVPicture *) m_pFrameYUV, m_pOut_buffer, AV_PIX_FMT_YUV420P, m_nWidth, m_nHeight );
  372. #endif
  373. #ifdef PROCESS_BGR24
  374. m_pOut_buffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  375. avpicture_fill( (AVPicture *) m_pFrameBGR, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight );
  376. //av_image_fill_arrays(m_pFrameBGR->data, m_pFrameBGR->linesize, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, 1);
  377. //m_pMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) );
  378. //m_pOMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) );
  379. #endif
  380. m_pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) );
  381. // Printout FF
  382. //av_dump_format(m_pFormatCtx,0,filepath,0);
  383. #ifdef PROCESS_YUV420P
  384. 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 );
  385. #endif
  386. #ifdef PROCESS_BGR24
  387. img_convert_ctx = sws_getContext(
  388. m_pVideoState->m_pCodecCtx->width,
  389. m_pVideoState->m_pCodecCtx->height,
  390. m_pVideoState->m_pCodecCtx->pix_fmt,
  391. m_nWidth,
  392. m_nHeight,
  393. AV_PIX_FMT_BGR24,
  394. SWS_BICUBIC,
  395. NULL,
  396. NULL,
  397. NULL
  398. );
  399. //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);
  400. #endif
  401. m_pVideoState->m_bStatus = MS_ALOCATED; // All seems to be good -> allocating mem
  402. LOG("YABABA!!!!");
  403. if( m_bThreadStatus < TH_WORKING ) init();
  404. LOG("YABABA232!");
  405. return true;
  406. };
  407. return false;
  408. };
  409. #if !(( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 ))
  410. // The flush packet is a non-NULL packet with size 0 and data NULL
  411. int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt){
  412. int ret;
  413. *got_frame = 0;
  414. if (pkt){
  415. ret = avcodec_send_packet(avctx, pkt);
  416. // In particular, we don't expect AVERROR(EAGAIN), because we read all
  417. // decoded frames with avcodec_receive_frame() until done.
  418. if (ret < 0) return ret == AVERROR_EOF ? 0 : ret;
  419. };
  420. ret = avcodec_receive_frame(avctx, frame);
  421. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return ret;
  422. if (ret >= 0) *got_frame = 1;
  423. return 0;
  424. };
  425. #endif
  426. void videoAVLIBSource::m_fThreadProc( void ){
  427. int RetVal;
  428. int bGotPicture = 0;
  429. m_bThreadStatus = TH_WORKING;
  430. LOG("Reader Thread Started.");
  431. //cv::setBreakOnError( true );
  432. m_pVideoState->m_bStatus = MS_STOPED;
  433. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  434. SLEEP(20);
  435. };
  436. LOG("Presentation Started.");
  437. //m_pVideoState->m_bStatus = MS_PRESENT;
  438. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  439. if ( m_pMediaDestination != NULL ){
  440. if( av_read_frame( m_pVideoState->m_pFormatCtx, m_pPacket ) >= 0 ){
  441. if( m_pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
  442. # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  443. RetVal = avcodec_decode_video2(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket);
  444. # else
  445. RetVal = decode(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket);
  446. #endif
  447. if( RetVal < 0 ){
  448. LOG_ERROR( "MediaSource::DecoderProc - Decode Error." );
  449. //m_bThreadStatus = TH_ENDED;
  450. //m_pVideoState->m_bStatus = MS_STOPED;
  451. continue;
  452. };
  453. if( m_bThreadStatus == TH_ENDED ){
  454. m_pVideoState->m_bStatus = MS_STOPED;
  455. continue;
  456. };
  457. if (bGotPicture){
  458. #ifdef PROCESS_YUV420P
  459. 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 );
  460. 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] );
  461. #endif
  462. #ifdef PROCESS_BGR24
  463. 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);
  464. try{
  465. # ifdef USE_QVideoFrame
  466. QVideoFrame *tFrame;
  467. tFrame = new QVideoFrame(m_pFrameBGR->linesize[0] * m_nHeight, QSize(m_nHeight, m_nWidth), m_pFrameBGR->linesize[0], QVideoFrame::Format_BGR24);
  468. memcpy(tFrame->bits(), m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight);
  469. if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame(tFrame, me );
  470. # else
  471. cv::Mat *mRGB;
  472. mRGB = new cv::Mat(m_nHeight, m_nWidth, CV_8UC3);
  473. memcpy(mRGB->data, m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight);
  474. if (m_pMediaDestination != NULL)
  475. m_pMediaDestination->pushFrame( mRGB, this );
  476. delete(mRGB);
  477. # endif // !USE_QVideoFrame
  478. } catch (cv::Exception & e) {
  479. LOG_ERROR(e.msg.c_str()); // output exception message
  480. };
  481. #endif
  482. //if(!m_bStreamingSource) SLEEP( m_pFrame->pkt_duration / 1000 );
  483. };
  484. };
  485. # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  486. av_free_packet( m_pPacket );
  487. # else
  488. av_packet_unref(m_pPacket);
  489. # endif
  490. } else {
  491. // Read Tale of data
  492. while(1){
  493. # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  494. RetVal = avcodec_decode_video2( m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket );
  495. # else
  496. RetVal = decode(m_pVideoState->m_pCodecCtx, m_pFrame, &bGotPicture, m_pPacket);
  497. # endif
  498. if( RetVal < 0 ) break;
  499. if( !bGotPicture ) break;
  500. if( m_bThreadStatus == TH_ENDED ){
  501. m_pVideoState->m_bStatus = MS_STOPED;
  502. break;
  503. };
  504. #ifdef PROCESS_YUV420P
  505. 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);
  506. 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] );
  507. #endif
  508. #ifdef PROCESS_BGR24
  509. 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);
  510. try{
  511. cv::Mat *mRGB;
  512. mRGB = new cv::Mat(m_nHeight, m_nWidth, CV_8UC3);
  513. memcpy(mRGB->data, m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight);
  514. if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame( mRGB, this );
  515. delete(mRGB);
  516. } catch (cv::Exception & e) {
  517. LOG_ERROR(e.msg.c_str()); // output exception message
  518. };
  519. #endif
  520. };
  521. LOG("Video Stream Ended");
  522. # if ( LIBAVCODEC_VERSION_MAJOR < 58 ) && ( LIBAVCODEC_VERSION_MINOR < 50 )
  523. av_free_packet(m_pPacket);
  524. # else
  525. av_packet_unref(m_pPacket);
  526. # endif
  527. m_pVideoState->m_bStatus = MS_CLOSED;
  528. m_bThreadStatus = TH_ENDED;
  529. };
  530. }else{
  531. m_pVideoState->m_bStatus = MS_CLOSED;
  532. #ifdef __linux__
  533. usleep( 20000 );
  534. #else
  535. Sleep(20);
  536. #endif
  537. };
  538. };
  539. m_pVideoState->m_bStatus = MS_CLOSED;
  540. m_bThreadStatus = TH_ENDED;
  541. LOG("Thread Ended.");
  542. };
  543. #undef LOG
  544. #undef LOG_ERROR