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.

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