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.

289 lines
8.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "video_in_LIBAV.h"
  2. static bool _FFD_REGISTER_ALL_FLAG = false;
  3. void debugout(const char * szFormat, ...)
  4. {
  5. char szBuff[1024];
  6. va_list arg;
  7. va_start(arg, szFormat);
  8. snprintf(szBuff, sizeof(szBuff), szFormat, arg);
  9. va_end(arg);
  10. printf(szBuff);
  11. }
  12. bool _LIBAV_VideoInput::initLBAV(void)
  13. {
  14. m_bSizeIsAutomatic = true;
  15. // m_sLastError = (char *) malloc( 1024 ); // For some text;
  16. if (! _FFD_REGISTER_ALL_FLAG)
  17. {
  18. _FFD_REGISTER_ALL_FLAG = true;
  19. // Init FFMPEG
  20. av_register_all();
  21. avdevice_register_all();
  22. avformat_network_init();
  23. };
  24. m_pVideoState = (VideoState *) av_mallocz(sizeof(VideoState));
  25. if (m_pVideoState == NULL)
  26. {
  27. // SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "_VideoInputFFD State Memory Allocation Error" );
  28. // exit( ERR_CRASH_MEMORY_NOT_ALLOCATED );
  29. return false;
  30. };
  31. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  32. if (m_pVideoState->m_pFormatCtx == NULL)
  33. {
  34. // SDL_LogError( SDL_LOG_CATEGORY_VIDEO, "_VideoInputFFD Context Memory Allocation Error" );
  35. // exit( ERR_CRASH_MEMORY_NOT_ALLOCATED );
  36. return false;
  37. };
  38. m_pVideoState->m_iVideoStreamID = -1;
  39. #ifndef _NOAUDIO_
  40. m_pVideoState->m_iAudioStreamID = -1;
  41. #endif
  42. m_pVideoState->m_bStatus = 0;
  43. return true;
  44. }
  45. _LIBAV_VideoInput::_LIBAV_VideoInput()
  46. {
  47. m_bStatus = VI_CREATED;
  48. if (initLBAV())
  49. {
  50. m_bStatus = VI_INIT_OK;
  51. }
  52. }
  53. _LIBAV_VideoInput::_LIBAV_VideoInput(char * Source)
  54. {
  55. m_bStatus = VI_CREATED;
  56. if (initLBAV())
  57. {
  58. m_bStatus = VI_INIT_OK;
  59. }
  60. openSource(Source);
  61. }
  62. _LIBAV_VideoInput::~_LIBAV_VideoInput()
  63. {
  64. if (m_pVideoState->m_bStatus == 1)
  65. {
  66. // free all mems
  67. av_frame_free(&m_pFrame);
  68. #ifdef PROCESS_YUV420P
  69. av_frame_free(&m_pFrameYUV);
  70. #endif
  71. #ifdef PROCESS_BGR24
  72. av_frame_free(&m_pFrameBGR);
  73. #endif
  74. av_free(m_pOut_buffer);
  75. sws_freeContext(img_convert_ctx);
  76. };
  77. if (m_pVideoState != NULL)
  78. {
  79. #ifndef _NOAUDIO_
  80. if (m_pVideoState->m_pAudioCtx != NULL)
  81. {
  82. avcodec_close(m_pVideoState->m_pAudioCtx);
  83. avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  84. };
  85. #endif
  86. if (m_pVideoState->m_pVideoCtx != NULL)
  87. {
  88. avcodec_close(m_pVideoState->m_pVideoCtx);
  89. avcodec_free_context(&m_pVideoState->m_pVideoCtx);
  90. };
  91. if (m_pVideoState->m_pFormatCtx != NULL)
  92. {
  93. avformat_close_input(&m_pVideoState->m_pFormatCtx);
  94. avformat_free_context(m_pVideoState->m_pFormatCtx);
  95. };
  96. av_free(m_pVideoState);
  97. };
  98. // free( m_sLastError );
  99. }
  100. bool _LIBAV_VideoInput::openSource(char * URI)
  101. {
  102. // If there is no pened streams
  103. if (m_pVideoState->m_bStatus == 0)
  104. {
  105. if (avformat_open_input(&m_pVideoState->m_pFormatCtx, URI, NULL, NULL) != 0)
  106. {
  107. // SDL_Log( "_VideoInputFFD Couldn't open input stream %s", URI );
  108. return false;
  109. };
  110. if (avformat_find_stream_info(m_pVideoState->m_pFormatCtx, NULL) < 0)
  111. {
  112. // SDL_Log( "Couldn't find stream information.");
  113. return false;
  114. };
  115. for (unsigned int i = 0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++)
  116. {
  117. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO && m_pVideoState->m_iVideoStreamID < 0)
  118. {
  119. m_pVideoState->m_iVideoStreamID = i;
  120. };
  121. #ifndef _NOAUDIO_
  122. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO && m_pVideoState->m_iAudioStreamID < 0)
  123. {
  124. m_pVideoState->m_iAudioStreamID = i;
  125. };
  126. #endif
  127. };
  128. if (m_pVideoState->m_iVideoStreamID == -1)
  129. {
  130. // SDL_Log( "Didn't find a video stream.");
  131. return false;
  132. };
  133. #ifndef _NOAUDIO_
  134. if (m_pVideoState->m_iAudioStreamID == -1)
  135. {
  136. // SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Didn't find a audio stream.");
  137. return false;
  138. };
  139. #endif
  140. // Stream found
  141. m_pVideoState->m_pVideoCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  142. m_pVideoState->m_pVideoCodec = avcodec_find_decoder(m_pVideoState->m_pVideoCtx->codec_id);
  143. if (m_pVideoState->m_pVideoCodec == NULL)
  144. {
  145. // SDL_Log( "Codec [%d] not found.", m_pVideoState->m_pVideoCtx->codec_id );
  146. return false;
  147. };
  148. if (avcodec_open2(m_pVideoState->m_pVideoCtx, m_pVideoState->m_pVideoCodec, NULL) < 0)
  149. {
  150. // SDL_Log( "Could not open Video codec[%d].", m_pVideoState->m_pVideoCtx->codec_id );
  151. return false;
  152. };
  153. #ifndef _NOAUDIO_
  154. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  155. m_pVideoState->m_pAudioCodec = avcodec_find_decoder(m_pVideoState->m_pAudioCtx->codec_id);
  156. if (! m_pAudioCodec)
  157. {
  158. // SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Unsupported Audio codec!");
  159. return false;
  160. };
  161. if (avcodec_open2(m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL) < 0)
  162. {
  163. // SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  164. return false;
  165. };
  166. #endif
  167. m_pFrame = av_frame_alloc();
  168. #ifdef PROCESS_YUV420P
  169. m_pFrameYUV = av_frame_alloc();
  170. #endif
  171. #ifdef PROCESS_BGR24
  172. m_pFrameBGR = av_frame_alloc();
  173. #endif
  174. if (m_bSizeIsAutomatic)
  175. {
  176. m_nWidth = m_pVideoState->m_pVideoCtx->width;
  177. m_nHeight = m_pVideoState->m_pVideoCtx->height;
  178. };
  179. #ifdef PROCESS_YUV420P
  180. m_pOut_buffer = (uint8_t *) av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, m_nWidth, m_nHeight, BUFFER_ALIGN));
  181. avpicture_fill((AVPicture *) m_pFrameYUV, m_pOut_buffer, AV_PIX_FMT_YUV420P, m_nWidth, m_nHeight);
  182. #endif
  183. #ifdef PROCESS_BGR24
  184. m_pOut_buffer = (uint8_t *) av_malloc(av_image_get_buffer_size(AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN));
  185. avpicture_fill((AVPicture *) m_pFrameBGR, m_pOut_buffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight);
  186. m_pMat = cv::Mat(m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255));
  187. m_pOMat = cv::Mat(m_nHeight, m_nWidth, CV_8UC3, cv::Scalar(255));
  188. #endif
  189. m_pPacket = (AVPacket *) av_malloc(sizeof(AVPacket));
  190. // Printout FF
  191. // av_dump_format(m_pFormatCtx,0,filepath,0);
  192. #ifdef PROCESS_YUV420P
  193. img_convert_ctx = sws_getContext(m_pCodecCtx->width, m_pCodecCtx->height, m_pCodecCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
  194. #endif
  195. #ifdef PROCESS_BGR24
  196. img_convert_ctx = sws_getContext(m_pVideoState->m_pVideoCtx->width, m_pVideoState->m_pVideoCtx->height, m_pVideoState->m_pVideoCtx->pix_fmt, m_nWidth, m_nHeight, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
  197. #endif
  198. m_pVideoState->m_bStatus = 1; // All seems to be good -> allocating mem
  199. };
  200. }
  201. void _LIBAV_VideoInput::m_fInputProc(void)
  202. {
  203. int RetVal;
  204. int bGotPicture = 0;
  205. m_bStatus = VI_WORKING_OK;
  206. while (m_bStatus == VI_WORKING_OK)
  207. {
  208. if (av_read_frame(m_pVideoState->m_pFormatCtx, m_pPacket) >= 0)
  209. {
  210. if (m_pPacket->stream_index == m_pVideoState->m_iVideoStreamID)
  211. {
  212. RetVal = avcodec_decode_video2(m_pVideoState->m_pVideoCtx, m_pFrame, &bGotPicture, m_pPacket);
  213. if (RetVal < 0)
  214. {
  215. // SDL_LogError( SDL_LOG_CATEGORY_VIDEO,"_VideoInputFFD::VideoProc - Decode Error." );
  216. m_bStatus = VI_STOPED;
  217. };
  218. if (bGotPicture)
  219. {
  220. #ifdef PROCESS_YUV420P
  221. sws_scale(me->img_convert_ctx, (const uint8_t * const *) me->m_pFrame->data, me->m_pFrame->linesize, 0, me->m_pCodecCtx->height, me->m_pFrameYUV->data, me->m_pFrameYUV->linesize);
  222. #endif
  223. #ifdef PROCESS_BGR24
  224. sws_scale(img_convert_ctx, (const uint8_t * const *) m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pVideoCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize);
  225. #endif
  226. #ifdef PROCESS_YUV420P
  227. SDL_UpdateYUVTexture(me->m_pTexture, &me->m_oRect, me->m_pFrameYUV->data[0], me->m_pFrameYUV->linesize[0], me->m_pFrameYUV->data[1], me->m_pFrameYUV->linesize[1], me->m_pFrameYUV->data[2], me->m_pFrameYUV->linesize[2]);
  228. #endif
  229. #ifdef PROCESS_BGR24
  230. sws_scale(img_convert_ctx, (const uint8_t * const *) m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pVideoCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize);
  231. // memcpy( m_pOMat.data, m_pMat.data, (m_nWidth)*(m_nHeight)*sizeof(uint8_t)*3 );
  232. cv::Mat * mRGB;
  233. mRGB = new cv::Mat(m_nHeight, m_nWidth, CV_8UC3);
  234. memcpy(mRGB->data, m_pFrameBGR->data[0], (m_nWidth) * (m_nHeight) * sizeof(uint8_t) * 3);
  235. pushFrame(mRGB);
  236. #endif
  237. // usleep( 2000 );
  238. };
  239. };
  240. av_free_packet(m_pPacket);
  241. }
  242. else
  243. {
  244. while (1)
  245. {
  246. RetVal = avcodec_decode_video2(m_pVideoState->m_pVideoCtx, m_pFrame, &bGotPicture, m_pPacket);
  247. if (RetVal)
  248. {
  249. break;
  250. }
  251. if (! bGotPicture)
  252. {
  253. break;
  254. }
  255. #ifdef PROCESS_YUV420P
  256. sws_scale(me->img_convert_ctx, (const uint8_t * const *) me->m_pFrame->data, me->m_pFrame->linesize, 0, me->m_pCodecCtx->height, me->m_pFrameYUV->data, me->m_pFrameYUV->linesize);
  257. // SDL---------------------------
  258. #endif
  259. #ifdef PROCESS_BGR24
  260. sws_scale(img_convert_ctx, (const uint8_t * const *) m_pFrame->data, m_pFrame->linesize, 0, m_pVideoState->m_pVideoCtx->height, m_pFrameBGR->data, m_pFrameBGR->linesize);
  261. #endif
  262. // usleep( 2000 );
  263. };
  264. m_pVideoState->m_bStatus = 3;
  265. #if OUTPUT_YUV420P
  266. fclose(fp_yuv);
  267. #endif
  268. };
  269. };
  270. m_bStatus = VI_STOPED;
  271. pthread_exit(NULL);
  272. }