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.

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