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.

728 lines
27 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
​
​
​
​
​
​
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
​
​
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 = -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. /*
  401. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  402. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  403. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  404. */
  405. m_pVideoState->m_pCodecCtx->thread_count = 2;
  406. m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  407. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  408. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  409. LOG_ERROR( Buffer );
  410. return false;
  411. };
  412. /* we do not send complete frames */
  413. if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  414. #ifndef _NOAUDIO_
  415. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  416. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  417. if( !m_pVideoState->m_pAudioCodec ){
  418. LOG_ERROR( "Unsupported Audio codec!" );
  419. return false;
  420. };
  421. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  422. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  423. LOG_ERROR( "Could not open Audio codec." );
  424. return false;
  425. };
  426. #endif
  427. m_pFrame = av_frame_alloc();
  428. #ifdef PROCESS_YUV420P
  429. m_pFrameYUV = av_frame_alloc();
  430. #endif
  431. #ifdef PROCESS_BGR24
  432. m_pFrameBGR = av_frame_alloc();
  433. #endif
  434. char tB[128];
  435. sprintf( tB, "Setting Frame [%dx%d]", m_nWidth, m_nHeight );
  436. LOG( tB );
  437. sprintf( tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height );
  438. LOG( tB );
  439. if( scaled ){
  440. LOG("Size is Scaled");
  441. m_nWidth = s_width;
  442. m_nHeight = s_height;
  443. } else {
  444. if( m_bSizeIsAutomatic ){
  445. LOG("Size is Automatic");
  446. if(( m_nWidth == -1 )&&( m_nHeight == -1 )){
  447. m_nWidth = m_pVideoState->m_pCodecCtx->width;
  448. m_nHeight = m_pVideoState->m_pCodecCtx->height;
  449. } else {
  450. // Proportional
  451. if( m_nHeight == -1 ){
  452. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  453. m_nHeight = (int)(((float)m_nWidth ) * k );
  454. } else {
  455. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  456. m_nWidth = (int)(((float)m_nHeight ) * k );
  457. };
  458. };
  459. } else {
  460. LOG("Size is Manual");
  461. if( width > 0 ){
  462. m_nWidth = width;
  463. };
  464. if( height > 0 ){
  465. m_nHeight = height;
  466. };
  467. };
  468. };
  469. sprintf( tB, "Actual Frame [%dx%d]", m_nWidth, m_nHeight );
  470. LOG( tB );
  471. #ifdef PROCESS_YUV420P
  472. m_pOut_buffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_YUV420P, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  473. //avpicture_fill((AVPicture *) m_pFrameYUV, m_pOut_buffer, AV_PIX_FMT_YUV420P, m_nWidth, m_nHeight );
  474. av_image_fill_arrays(m_pFrameYUV->data, m_pFrameYUV->linesize, m_pOut_buffer, AV_PIX_FMT_YUV420P, m_nWidth, m_nHeight, 1);
  475. m_pFrameYUV->width = m_nWidth;
  476. m_pFrameYUV->height = m_nHeight;
  477. #endif
  478. #ifdef PROCESS_BGR24
  479. m_pOut_buffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  480. avpicture_fill( (AVPicture *) m_pFrameBGR, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight );
  481. //av_image_fill_arrays(m_pFrameBGR->data, m_pFrameBGR->linesize, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, 1);
  482. //m_pMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) );
  483. //m_pOMat = cv::Mat( m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255) );
  484. #endif
  485. m_pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) );
  486. // Printout FF
  487. //av_dump_format(m_pFormatCtx,0,filepath,0);
  488. #ifdef PROCESS_YUV420P
  489. //Col Depth 12bpp
  490. 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 );
  491. //Col Depth 16bpp
  492. //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);
  493. #endif
  494. #ifdef PROCESS_BGR24
  495. img_convert_ctx = sws_getContext(
  496. m_pVideoState->m_pCodecCtx->width,
  497. m_pVideoState->m_pCodecCtx->height,
  498. m_pVideoState->m_pCodecCtx->pix_fmt,
  499. m_nWidth,
  500. m_nHeight,
  501. AV_PIX_FMT_BGR24,
  502. SWS_BICUBIC,
  503. NULL,
  504. NULL,
  505. NULL
  506. );
  507. //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);
  508. #endif
  509. m_pVideoState->m_bStatus = MS_ALOCATED; // All seems to be good -> allocating mem
  510. if( m_bThreadStatus < TH_WORKING ) return init();
  511. };
  512. return false;
  513. };
  514. #if !(( LIBAVCODEC_VERSION_MAJOR < 58 ) /* && ( LIBAVCODEC_VERSION_MINOR < 50 ) */ )
  515. // The flush packet is a non-NULL packet with size 0 and data NULL
  516. int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt){
  517. int ret;
  518. *got_frame = 0;
  519. if (pkt){
  520. ret = avcodec_send_packet(avctx, pkt);
  521. // In particular, we don't expect AVERROR(EAGAIN), because we read all
  522. // decoded frames with avcodec_receive_frame() until done.
  523. if (ret < 0) return ret == AVERROR_EOF ? 0 : ret;
  524. };
  525. ret = avcodec_receive_frame(avctx, frame);
  526. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return ret;
  527. if (ret >= 0) *got_frame = 1;
  528. return 0;
  529. };
  530. #endif
  531. void videoAVLIBSource::m_fThreadProc( void ){
  532. int RetVal;
  533. int bGotPicture = 0;
  534. #ifdef _OPENCV_
  535. cv::Mat mRGB(m_nHeight, m_nWidth, CV_8UC3);
  536. #endif
  537. m_bThreadStatus = TH_WORKING;
  538. #ifdef DEBUG_EXTRA
  539. LOG("Reader Thread Started.");
  540. #endif
  541. //cv::setBreakOnError( true );
  542. m_pVideoState->m_bStatus = MS_STOPED;
  543. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  544. SLEEP(20);
  545. };
  546. #ifdef DEBUG
  547. LOG("Presentation Started.");
  548. #endif
  549. //m_pVideoState->m_bStatus = MS_PRESENT;
  550. av_read_play( m_pVideoState->m_pFormatCtx );
  551. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  552. if ( m_pMediaDestination != NULL ){
  553. if( av_read_frame( m_pVideoState->m_pFormatCtx, m_pPacket ) >= 0 ){
  554. if( m_pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
  555. int dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, m_pPacket );
  556. if( dec_ret < 0 ){
  557. LOG_ERROR( "Error decoding video frame" );
  558. m_pVideoState->m_bStatus = MS_CLOSING;
  559. m_bThreadStatus = TH_ENDED;
  560. } else {
  561. while( dec_ret >= 0 && m_bThreadStatus != TH_ENDED ) {
  562. dec_ret = avcodec_receive_frame( m_pVideoState->m_pCodecCtx, m_pFrame );
  563. if( dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF ){
  564. continue;//Skip to next data
  565. } else if( dec_ret < 0 ){
  566. LOG_ERROR("Error in video decoding");
  567. m_pVideoState->m_bStatus = MS_STOPED;
  568. m_bThreadStatus = TH_ENDED;
  569. break;
  570. };
  571. #ifdef PROCESS_YUV420P
  572. 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 );
  573. //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]);
  574. m_pMediaDestination->pushFrame( m_pFrameYUV, m_pOpaque );
  575. #else // PROCESS_BGR24
  576. 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 );
  577. try{
  578. #ifdef USE_QVideoFrame
  579. QVideoFrame *tFrame;
  580. tFrame = new QVideoFrame(m_pFrameBGR->linesize[0] * m_nHeight, QSize(m_nHeight, m_nWidth), m_pFrameBGR->linesize[0], QVideoFrame::Format_BGR24);
  581. memcpy(tFrame->bits(), m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight);
  582. if (m_pMediaDestination != NULL) m_pMediaDestination->pushFrame(tFrame, m_pOpaque );
  583. #else
  584. #ifdef _OPENCV_
  585. memcpy( mRGB.data, m_pFrameBGR->data[0], m_pFrameBGR->linesize[0] * m_nHeight );
  586. #endif
  587. #ifdef _SDL_
  588. SDL_Surface* mRGB = SDL_CreateRGBSurfaceFrom( m_pFrameBGR->data[0], m_nHeight, m_nWidth, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask );
  589. #endif
  590. m_pMediaDestination->pushFrame( mRGB, m_pOpaque );
  591. #ifdef _SDL_
  592. SDL_FreeSurface(mRGB);
  593. #endif
  594. #ifdef _OPENCV_
  595. //delete(mRGB);
  596. #endif
  597. #endif // !USE_QVideoFrame
  598. } catch (cv::Exception & e) {
  599. LOG_ERROR(e.msg.c_str()); // output exception message
  600. };
  601. #endif
  602. //Unref Frame
  603. //av_freep( m_pFrame->data[0] );
  604. //av_frame_unref( m_pFrame );
  605. };
  606. };
  607. };
  608. } else {
  609. m_pVideoState->m_bStatus = MS_CLOSED;
  610. m_bThreadStatus = TH_ENDED;
  611. };
  612. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) //&& ( LIBAVCODEC_VERSION_MINOR < 50 )
  613. av_free_packet( m_pPacket );
  614. #else
  615. av_packet_unref( m_pPacket );
  616. #endif
  617. } else SLEEP( 20 );
  618. };
  619. m_pVideoState->m_bStatus = MS_CLOSED;
  620. m_bThreadStatus = TH_ENDED;
  621. };
  622. /*
  623. 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.
  624. int save_frame_as_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) {
  625. AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
  626. if (!jpegCodec) {
  627. return -1;
  628. }
  629. AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
  630. if (!jpegContext) {
  631. return -1;
  632. }
  633. jpegContext->pix_fmt = pCodecCtx->pix_fmt;
  634. jpegContext->height = pFrame->height;
  635. jpegContext->width = pFrame->width;
  636. if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
  637. return -1;
  638. }
  639. FILE *JPEGFile;
  640. char JPEGFName[256];
  641. AVPacket packet = {.data = NULL, .size = 0};
  642. av_init_packet(&packet);
  643. int gotFrame;
  644. if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) {
  645. return -1;
  646. }
  647. sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo);
  648. JPEGFile = fopen(JPEGFName, "wb");
  649. fwrite(packet.data, 1, packet.size, JPEGFile);
  650. fclose(JPEGFile);
  651. av_free_packet(&packet);
  652. avcodec_close(jpegContext);
  653. return 0;
  654. }
  655. */
  656. #undef TLOG
  657. #undef TLOG_ERROR
  658. #undef LOG
  659. #undef LOG_ERROR