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.

641 lines
23 KiB

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