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.

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