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.

710 lines
25 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
​
​
​
​
​
​
​
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. videoAVLIBSource::videoAVLIBSource( void *opaque ): basicMediaSource(), basicThread() {
  14. m_pLog = basicLogCollection::getInstance();
  15. m_bStreamingSource = true;
  16. m_bSizeIsAutomatic = false;
  17. m_nWidth = 640;
  18. m_nHeight = 524;
  19. m_bSrcType = AVLIB_SOURCE_NOTDEF;
  20. if( opaque ) {
  21. m_pOpaque = opaque;
  22. } else {
  23. m_pOpaque = (void *)this;
  24. };
  25. if( !initLibAV() ) return;
  26. m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
  27. if (m_pVideoState == NULL){
  28. LOG_ERROR("VideoState Memory Allocation Error");
  29. return;
  30. };
  31. m_pVideoState->m_bStatus = MS_UNDEFINED;
  32. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  33. if (m_pVideoState->m_pFormatCtx == NULL){
  34. LOG_ERROR("VideoCodec Context Memory Allocation Error");
  35. return;
  36. };
  37. m_pVideoState->m_iVideoStreamID = -1;
  38. #ifndef _NOAUDIO_
  39. m_pVideoState->m_iAudioStreamID = -1;
  40. #endif
  41. m_pVideoState->m_bStatus = MS_INIT;
  42. };
  43. videoAVLIBSource::videoAVLIBSource( char *Source, void *opaque ) : basicMediaSource(), basicThread() {
  44. m_pLog = basicLogCollection::getInstance();
  45. m_bStreamingSource = true;
  46. m_bSizeIsAutomatic = false;
  47. m_nWidth = 640;
  48. m_nHeight = 524;
  49. m_bSrcType = AVLIB_SOURCE_NOTDEF;
  50. if( opaque ) {
  51. m_pOpaque = opaque;
  52. } else {
  53. m_pOpaque = (void *)this;
  54. };
  55. if( !initLibAV() ) return;
  56. m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
  57. if (m_pVideoState == NULL){
  58. LOG_ERROR("VideoState Memory Allocation Error");
  59. return;
  60. };
  61. m_pVideoState->m_bStatus = MS_UNDEFINED;
  62. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  63. if (m_pVideoState->m_pFormatCtx == NULL){
  64. LOG_ERROR("VideoCodec Context Memory Allocation Error");
  65. return;
  66. };
  67. m_pVideoState->m_iVideoStreamID = -1;
  68. #ifndef _NOAUDIO_
  69. m_pVideoState->m_iAudioStreamID = -1;
  70. #endif
  71. m_pVideoState->m_bStatus = MS_INIT;
  72. _open( Source );
  73. };
  74. videoAVLIBSource::~videoAVLIBSource(){
  75. if (m_pVideoState != NULL){
  76. if( m_pVideoState->m_bStatus > MS_INIT ){
  77. m_bThreadStatus = TH_ENDED;
  78. while (m_pVideoState->m_bStatus == MS_PRESENT) SLEEP( 10 );
  79. #ifndef _NOAUDIO_
  80. if( m_pVideoState->m_pAudioCtx!=NULL ){
  81. avcodec_close( m_pVideoState->m_pAudioCtx );
  82. //avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  83. };
  84. #endif
  85. if( m_pVideoState->m_pCodecCtx != NULL ){
  86. avcodec_close(m_pVideoState->m_pCodecCtx);
  87. //avcodec_free_context(&m_pVideoState->m_pCodecCtx);
  88. };
  89. if( m_pVideoState->m_pFormatCtx != NULL ){
  90. avformat_close_input(&m_pVideoState->m_pFormatCtx);
  91. //avformat_free_context(m_pVideoState->m_pFormatCtx);
  92. };
  93. if( m_pVideoState->m_pSwsCtx != NULL ){
  94. sws_freeContext( m_pVideoState->m_pSwsCtx );
  95. };
  96. av_free(m_pVideoState);
  97. };
  98. };
  99. };
  100. bool videoAVLIBSource::setSize( int Width, int Height ){
  101. if( m_pVideoState ){
  102. if( m_pVideoState->m_bStatus == MS_INIT ) {
  103. if( Width && Height ) {
  104. m_bSizeIsAutomatic = false;
  105. m_nWidth = Width;
  106. m_nHeight = Height;
  107. return true;
  108. };
  109. };
  110. };
  111. return false;
  112. };
  113. bool videoAVLIBSource::setProportionalByWidth(int Width){
  114. if( m_pVideoState ){
  115. if( m_pVideoState->m_bStatus == MS_INIT ) {
  116. if( Width > 0 ) {
  117. m_bSizeIsAutomatic = true;
  118. m_nWidth = Width;
  119. return true;
  120. };
  121. };
  122. };
  123. return false;
  124. };
  125. bool videoAVLIBSource::setProportionalByHeight(int Height){
  126. if( m_pVideoState ){
  127. if( m_pVideoState->m_bStatus == MS_INIT ) {
  128. if( Height > 0 ) {
  129. m_bSizeIsAutomatic = true;
  130. m_nHeight = Height;
  131. return true;
  132. };
  133. };
  134. };
  135. return false;
  136. };
  137. bool videoAVLIBSource::setFrameAspectRatio( FrameAspectRatio AspectRatio ){
  138. return true;
  139. };
  140. bool videoAVLIBSource::_close(void){
  141. if (m_pVideoState == NULL) return true;
  142. if (m_pVideoState->m_bStatus < MS_OPENED) return true;
  143. if (m_pVideoState->m_bStatus == MS_CLOSED) return true;
  144. _stop();
  145. if (m_pVideoState->m_bStatus > MS_ALOCATED){
  146. #ifndef _NOAUDIO_
  147. if (m_pVideoState->m_pAudioCtx != NULL){
  148. avcodec_close(m_pVideoState->m_pAudioCtx);
  149. //avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  150. };
  151. #endif
  152. if (m_pVideoState->m_pCodecCtx != NULL){
  153. avcodec_close(m_pVideoState->m_pCodecCtx);
  154. //avcodec_free_context(&m_pVideoState->m_pCodecCtx);
  155. avformat_close_input(&m_pVideoState->m_pFormatCtx);
  156. //av_free(m_pVideoState->m_pVideoCodec);
  157. m_pVideoState->m_iVideoStreamID = -1;
  158. };
  159. };
  160. m_pVideoState->m_bStatus = MS_INIT;
  161. return true;
  162. };
  163. bool videoAVLIBSource::_stop(void){
  164. basicThread::waitThreadEnd( true );
  165. return true;
  166. };
  167. //https://www.youtube.com/watch?v=Lu3LZmhC_DY
  168. bool videoAVLIBSource::_open(char *URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, bool scaled, int s_width, int s_height ){
  169. if (m_pVideoState == NULL) return false;
  170. if (m_bThreadStatus > TH_PRE_INIT ) return false;
  171. if (m_pVideoState->m_bStatus == MS_INIT) {
  172. char Buffer[1024] = {0};
  173. char tChar[128] = {0};
  174. sprintf( m_pCookie, "%s", MD5String(URI) );
  175. m_pVideoState->m_bStatus = MS_OPENING;
  176. if( strncmp( URI, "video=", 6 ) == 0 ){
  177. m_bSrcType = AVLIB_SOURCE_DDSHOW;
  178. #ifdef DEBUG
  179. LOG("MS-Video Device");
  180. #endif
  181. AVDictionary *options = NULL;
  182. if( width != -1 && height != -1 ){
  183. m_bSizeIsAutomatic = false;
  184. char tChar[10];
  185. sprintf(tChar, "%dx%d", width, height);
  186. av_dict_set(&options, "video_size", tChar, 0);
  187. } else {
  188. m_bSizeIsAutomatic = true;
  189. };
  190. //av_dict_set(&options, "pixel_format", "yuyv422", 0 );
  191. av_dict_set(&options, "video_codec", "mjpeg", 0 );
  192. if( inputPin != -1 ){
  193. sprintf(tChar, "%d", inputPin);
  194. av_dict_set(&options, "crossbar_video_input_pin_number", tChar, 0);
  195. };
  196. if( framerate != -1 ){
  197. sprintf(tChar, "%f", framerate);
  198. av_dict_set(&options, "framerate", tChar, 0);
  199. };
  200. av_dict_set(&options, "rtbufsize", "500000k", 0);
  201. if( ShowDeviceMenu ){
  202. av_dict_set(&options, "show_video_device_dialog", "true", 0);
  203. av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
  204. //sprintf(Buffer, "data/%s.settings", m_pCookie);
  205. //av_dict_set(&options, "video_device_save", Buffer, 0);
  206. #ifndef _NOAUDIO_
  207. av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
  208. sprintf(Buffer, "%s-a.settings", URI);
  209. av_dict_set(&options, "audio_device_save", Buffer, 0);
  210. #endif
  211. } else {
  212. sprintf(Buffer, "data/%s.settings", m_pCookie );
  213. //if (!fileExist(Buffer)) av_dict_set(&options, "video_device_load", Buffer, 0);
  214. };
  215. //av_dict_set(&options, "crossbar_video_input_pin_number", "1", 0);
  216. //av_dict_set(&options, "show_audio_crossbar_connection_dialog", "true", 0);
  217. //av_dict_set(&options, "show_video_crossbar_connection_dialog", "true", 0);
  218. AVInputFormat *iformat = av_find_input_format("dshow");
  219. //av_dict_set(&options, "pixel_format", "bgr24", 0);
  220. av_dict_set(&options, "timeout", "5000", 0);
  221. //AVDeviceInfoList *device_list;
  222. //m_pVideoState->m_pFormatCtx->iformat = iformat;
  223. //avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
  224. //avdevice_free_list_devices(&device_list);
  225. if( avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, iformat, &options) != 0 ){
  226. av_dict_free(&options);
  227. //av_free( iformat );
  228. sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI);
  229. LOG_ERROR(Buffer);
  230. m_pVideoState->m_bStatus = MS_INIT;
  231. return false;
  232. };
  233. /*AVDictionaryEntry *e;
  234. if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  235. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  236. LOG_ERROR(Buffer);
  237. };*/
  238. //if (iformat!=NULL)
  239. //if (iformat->priv_class != NULL){
  240. // const AVOption *cOption = NULL;
  241. // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
  242. // serializeOption(cOption);
  243. //};
  244. av_dict_free(&options);
  245. AVOption *mpOption = NULL;
  246. } else {
  247. if( strncmp( URI, "/dev/video", 10 ) == 0 ) {
  248. m_bSrcType = AVLIB_SOURCE_V4L2;
  249. } else {
  250. m_bSrcType = AVLIB_SOURCE_IPSTREAM;
  251. };
  252. //AVDictionary *options = NULL;
  253. //av_dict_set(&m_pVideoState->m_pFormatOpts, "movflags", "faststart", 0);
  254. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ) {
  255. #ifdef DEBUG
  256. LOG("IP Stream Source");
  257. #endif
  258. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtsp_transport", "tcp", 0);
  259. av_dict_set(&m_pVideoState->m_pFormatOpts, "stimeout", "5000000", 0);
  260. } else {
  261. #ifdef DEBUG
  262. LOG("Linux Video Device");
  263. #endif
  264. if( width != -1 && height != -1 ){
  265. m_bSizeIsAutomatic = false;
  266. sprintf(tChar, "%dx%d", width, height);
  267. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  268. } else {
  269. m_bSizeIsAutomatic = true;
  270. };
  271. };
  272. av_dict_set(&m_pVideoState->m_pFormatOpts, "buffer_size", "1024000", 0);
  273. av_dict_set(&m_pVideoState->m_pFormatOpts, "max_delay", "100", 0);
  274. av_dict_set(&m_pVideoState->m_pFormatOpts, "fflags", "nobuffer", 0);
  275. //av_dict_set(&options, "analyzeduration", "1000000", 0);
  276. if( framerate != -1 ){
  277. sprintf(tChar, "%0.2f", framerate);
  278. //sprintf(tChar, "120" );
  279. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  280. //av_dict_set(&options, "frame_rate", tChar, 0);
  281. };
  282. //av_dict_set(&options, "pixel_format", "mjpeg", 0);
  283. //av_dict_set(&options, "timeout", "8000", 0);
  284. int RetVal = 0;
  285. strncpy( m_pVideoState->m_pFileName, URI, MAX_PATH_LEN );
  286. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  287. //av_dict_free(&options);
  288. char tBuffer[1024];
  289. av_strerror( RetVal, tBuffer, 1024 );
  290. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  291. LOG_ERROR(Buffer);
  292. return false;
  293. };
  294. AVDictionaryEntry *e;
  295. if( e = av_dict_get( m_pVideoState->m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  296. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  297. LOG_ERROR(Buffer);
  298. };
  299. //av_dict_free( &e );
  300. };
  301. //DataStream Opened !
  302. m_pVideoState->m_bStatus = MS_OPENED;
  303. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  304. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  305. LOG_ERROR(Buffer);
  306. return false;
  307. };
  308. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  309. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  310. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  311. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  312. LOG( "VIDEO stream detected." );
  313. if( m_pVideoState->m_iVideoStreamID <0 ){
  314. LOG( "VIDEO stream assigned." );
  315. m_pVideoState->m_iVideoStreamID=i;
  316. };
  317. };
  318. # ifndef _NOAUDIO_
  319. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  320. LOG( "AUDIO stream detected." );
  321. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  322. LOG( "AUDIO stream assigned." );
  323. m_pVideoState->m_iAudioStreamID=i;
  324. }
  325. };
  326. # endif
  327. };
  328. if( m_pVideoState->m_iVideoStreamID == -1 ){
  329. LOG_ERROR( "Didn't find a video stream.");
  330. return false;
  331. };
  332. # ifndef _NOAUDIO_
  333. if( m_pVideoState->m_iAudioStreamID == -1 ){
  334. LOG_ERROR("Didn't find a audio stream.");
  335. return false;
  336. };
  337. # endif
  338. // Stream found
  339. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  340. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  341. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  342. #else
  343. //LOG("YABABA-2!");
  344. /* find the video stream information */
  345. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  346. if (m_pVideoState->m_iVideoStreamID == -1){
  347. LOG_ERROR("Didn't find a video stream.");
  348. return false;
  349. };
  350. # ifndef _NOAUDIO_
  351. if (m_pVideoState->m_iAudioStreamID == -1){
  352. LOG_ERROR("Didn't find a audio stream.");
  353. return false;
  354. };
  355. # endif
  356. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  357. LOG_ERROR("Can not create decoder context!");
  358. return false;
  359. };
  360. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  361. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  362. LOG_ERROR("Can not set Context parameters!");
  363. return false;
  364. };
  365. #endif
  366. if( m_pVideoState->m_pVideoCodec == NULL ){
  367. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  368. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  369. LOG_ERROR( Buffer );
  370. return false;
  371. };
  372. /*
  373. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  374. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  375. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  376. */
  377. //m_pVideoState->m_pCodecCtx->thread_count = 2;
  378. //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  379. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  380. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  381. LOG_ERROR( Buffer );
  382. return false;
  383. };
  384. /* we do not send complete frames */
  385. if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  386. #ifndef _NOAUDIO_
  387. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  388. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  389. if( !m_pVideoState->m_pAudioCodec ){
  390. LOG_ERROR( "Unsupported Audio codec!" );
  391. return false;
  392. };
  393. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  394. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  395. LOG_ERROR( "Could not open Audio codec." );
  396. return false;
  397. };
  398. #endif
  399. #ifdef DEBUG
  400. char tB[256];
  401. sprintf( tB, "Setting Frame [%dx%d]", m_nWidth, m_nHeight );
  402. LOG( tB );
  403. sprintf( tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height );
  404. LOG( tB );
  405. #endif
  406. if( scaled ){
  407. #ifdef DEBUG
  408. LOG("Size is Scaled");
  409. #endif
  410. m_nWidth = s_width;
  411. m_nHeight = s_height;
  412. } else {
  413. if( m_bSizeIsAutomatic ){
  414. #ifdef DEBUG
  415. LOG("Size is Automatic");
  416. #endif
  417. if(( m_nWidth == -1 )&&( m_nHeight == -1 )){
  418. m_nWidth = m_pVideoState->m_pCodecCtx->width;
  419. m_nHeight = m_pVideoState->m_pCodecCtx->height;
  420. } else {
  421. // Proportional
  422. if( m_nHeight == -1 ){
  423. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  424. m_nHeight = (int)(((float)m_nWidth ) * k );
  425. } else {
  426. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  427. m_nWidth = (int)(((float)m_nHeight ) * k );
  428. };
  429. };
  430. } else {
  431. #ifdef DEBUG
  432. LOG("Size is Manual");
  433. #endif
  434. if( width > 0 ){
  435. m_nWidth = width;
  436. };
  437. if( height > 0 ){
  438. m_nHeight = height;
  439. };
  440. };
  441. };
  442. #ifdef DEBUG
  443. sprintf( tB, "Using Size [%dx%d]", m_nWidth, m_nHeight );
  444. LOG( tB );
  445. sprintf(tB,"swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width,
  446. m_pVideoState->m_pCodecCtx->height,
  447. m_pVideoState->m_pCodecCtx->pix_fmt,
  448. m_nWidth,
  449. m_nHeight,
  450. AV_PIX_FMT_BGR24,
  451. SWS_BILINEAR );
  452. LOG( tB );
  453. #endif
  454. m_pVideoState->m_pSwsCtx = sws_getContext(
  455. m_pVideoState->m_pCodecCtx->width,
  456. m_pVideoState->m_pCodecCtx->height,
  457. m_pVideoState->m_pCodecCtx->pix_fmt,
  458. m_nWidth,
  459. m_nHeight,
  460. AV_PIX_FMT_BGR24,
  461. SWS_FAST_BILINEAR | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  462. NULL,
  463. NULL,
  464. NULL
  465. );
  466. if ( m_pVideoState->m_pSwsCtx == NULL ){
  467. LOG_ERROR("Error Allocating SwsCtx!");
  468. return false;
  469. };
  470. #ifdef DEBUG
  471. LOG("Allocation Done->Start Thread!");
  472. #endif
  473. return init();
  474. };
  475. return false;
  476. };
  477. void videoAVLIBSource::m_fThreadProc( void ){
  478. AVPacket * pPacket = NULL;
  479. cv::Mat mRGB( m_nHeight, m_nWidth, CV_8UC3 );
  480. AVFrame * pFrame = NULL;
  481. AVFrame * pFrameBGR = NULL;
  482. uint8_t * pBGRbuffer = NULL;
  483. int dec_ret;
  484. char tB[256];
  485. pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) );
  486. av_init_packet( pPacket );
  487. if ( pPacket == NULL ) {
  488. m_bThreadStatus = TH_ENDED;
  489. LOG_ERROR("Error allocating AVPacket!");
  490. } else {
  491. pFrame = av_frame_alloc();
  492. if ( pFrame == NULL ) {
  493. m_bThreadStatus = TH_ENDED;
  494. LOG_ERROR("Error allocating SRC AVFrame!");
  495. } else {
  496. pFrameBGR = av_frame_alloc();
  497. if ( pFrameBGR == NULL ) {
  498. m_bThreadStatus = TH_ENDED;
  499. LOG_ERROR("Error allocating BGR AVFrame!");
  500. } else {
  501. pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  502. if ( pBGRbuffer == NULL ) {
  503. m_bThreadStatus = TH_ENDED;
  504. LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  505. } else {
  506. av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN );
  507. pFrameBGR->width = m_nWidth;
  508. pFrameBGR->height = m_nHeight;
  509. m_pVideoState->m_bStatus = MS_ALOCATED;
  510. m_bThreadStatus = TH_WORKING;
  511. #ifdef DEBUG_EXTRA
  512. LOG("Video Reader Thread Started.");
  513. #endif
  514. };
  515. };
  516. };
  517. };
  518. //cv::setBreakOnError( true );
  519. m_pVideoState->m_bStatus = MS_STOPED;
  520. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  521. SLEEP(20);
  522. #ifdef DEBUG
  523. LOG("Waiting Destination");
  524. #endif
  525. };
  526. m_pVideoState->m_bStatus = MS_PRESENT;
  527. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ){
  528. //av_read_play( m_pVideoState->m_pFormatCtx );
  529. };
  530. #ifdef DEBUG
  531. LOG("Presentation Started.");
  532. #endif
  533. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  534. if ( m_pMediaDestination != NULL ){
  535. dec_ret = av_read_frame( m_pVideoState->m_pFormatCtx, pPacket );
  536. if( dec_ret >= 0 ){
  537. if( pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
  538. m_pVideoState->m_bStatus = MS_PRESENT;
  539. dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, pPacket );
  540. if( dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */ ){
  541. LOG_ERROR( "Error sending video package to decoder" );
  542. m_pVideoState->m_bStatus = MS_CLOSING;
  543. m_bThreadStatus = TH_ENDED;
  544. } else {
  545. while( dec_ret >= 0 && m_bThreadStatus != TH_ENDED ) {
  546. dec_ret = avcodec_receive_frame( m_pVideoState->m_pCodecCtx, pFrame );
  547. if( dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF ){
  548. break;//Skip to next data
  549. } else if( dec_ret < 0 ){
  550. LOG_ERROR("Error decoding video frame");
  551. m_pVideoState->m_bStatus = MS_CLOSING;
  552. m_bThreadStatus = TH_ENDED;
  553. break;
  554. };
  555. sws_scale( m_pVideoState->m_pSwsCtx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize );
  556. try{
  557. memcpy( mRGB.data, pFrameBGR->data[0], pFrameBGR->linesize[0] * m_nHeight );
  558. m_pMediaDestination->pushFrame( mRGB, m_pOpaque );
  559. } catch (cv::Exception & e) {
  560. LOG_ERROR( e.msg.c_str() );
  561. };
  562. };
  563. };
  564. };
  565. } else if( dec_ret != AVERROR(EAGAIN) && dec_ret != AVERROR_EOF ){
  566. LOG_ERROR( "Error reading video package" );
  567. char tBuffer[1024];
  568. av_strerror( dec_ret, tBuffer, 1024 );
  569. LOG_ERROR( tBuffer );
  570. m_pVideoState->m_bStatus = MS_CLOSING;
  571. m_bThreadStatus = TH_ENDED;
  572. } else {
  573. if( dec_ret == AVERROR_EOF ){
  574. avformat_close_input( &m_pVideoState->m_pFormatCtx );
  575. if( ( dec_ret = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  576. char tBuffer[1024];
  577. av_strerror( dec_ret, tBuffer, 1024 );
  578. sprintf( tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, dec_ret, m_pVideoState->m_pFileName );
  579. LOG_ERROR( tB );
  580. m_pVideoState->m_bStatus = MS_CLOSING;
  581. m_bThreadStatus = TH_ENDED;
  582. };
  583. };
  584. //m_pVideoState->m_bStatus = MS_STOPED;
  585. SLEEP( 5 );
  586. };
  587. av_packet_unref( pPacket );
  588. } else SLEEP( 20 );
  589. };
  590. if ( pFrame != NULL ) {
  591. av_frame_free( &pFrame );
  592. };
  593. if ( pPacket != NULL ) {
  594. av_free( pPacket );
  595. };
  596. if( pBGRbuffer != NULL ) {
  597. av_free( pBGRbuffer );
  598. };
  599. m_pVideoState->m_bStatus = MS_CLOSED;
  600. m_bThreadStatus = TH_ENDED;
  601. #ifdef DEBUG
  602. LOG("Video Source Ended!");
  603. #endif
  604. };
  605. /*
  606. 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.
  607. int save_frame_as_jpeg( AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) {
  608. AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
  609. if (!jpegCodec) {
  610. return -1;
  611. }
  612. AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
  613. if (!jpegContext) {
  614. return -1;
  615. }
  616. jpegContext->pix_fmt = pCodecCtx->pix_fmt;
  617. jpegContext->height = pFrame->height;
  618. jpegContext->width = pFrame->width;
  619. if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
  620. return -1;
  621. }
  622. FILE *JPEGFile;
  623. char JPEGFName[256];
  624. AVPacket packet = {.data = NULL, .size = 0};
  625. av_init_packet(&packet);
  626. int gotFrame;
  627. if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) {
  628. return -1;
  629. }
  630. sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo);
  631. JPEGFile = fopen(JPEGFName, "wb");
  632. fwrite(packet.data, 1, packet.size, JPEGFile);
  633. fclose(JPEGFile);
  634. av_free_packet(&packet);
  635. avcodec_close(jpegContext);
  636. return 0;
  637. }
  638. */
  639. MediaStateStatus videoAVLIBSource::getStatus( void ){
  640. if( m_pVideoState ) return m_pVideoState->m_bStatus;
  641. return MS_UNDEFINED;
  642. };
  643. bool videoAVLIBSource::isFinished( void ){
  644. if( m_pVideoState ) if( m_pVideoState->m_bStatus < MS_CLOSING ) return false;
  645. return true;
  646. };
  647. #undef LOG
  648. #undef LOG_ERROR