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.

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