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.

250 lines
9.0 KiB

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