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.

624 lines
22 KiB

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