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.

629 lines
22 KiB

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