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.

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