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.

463 lines
20 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaMediaEncoder.h"
  2. #include "servaKernel.h"
  3. #define LOG( X ) m_pLog->Log( "MEN", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG234[256];\
  7. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "MEN", this, _LOG_ERROR, _tBfrErrMSG234 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "MEN", this, _LOG_ERROR, X );
  12. #endif
  13. servaMediaEncoder::servaMediaEncoder( uint64_t Ref ):basicThread(){
  14. m_pLog = basicLogCollection::getInstance();
  15. m_pMaster = servaKernel::getInstance();
  16. m_nRefID = Ref;
  17. m_nInFileSize = 0;
  18. m_nOutFileSize = 0;
  19. m_bStatus = ENCODER_NONE;
  20. DBC_SERVA_MEDIA_ENCODER_SETTINGS *tData = NULL;//m_pDBC->getMediaEncoderSettings();
  21. if( tData ){
  22. memcpy( &m_sEncoderSettings, tData, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ) );
  23. } else {
  24. memset( &m_sEncoderSettings, 0, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ));
  25. m_sEncoderSettings.CRF = 20;
  26. strncpy( m_sEncoderSettings.preset, "medium", 15 );
  27. strncpy( m_sEncoderSettings.tune, "fastdecode", 15 );
  28. strncpy( m_sEncoderSettings.profile, "high422", 15 );
  29. m_sEncoderSettings.TH_MODE = FF_THREAD_FRAME;
  30. m_sEncoderSettings.bitrate = 400000;
  31. strncpy( m_sEncoderSettings.CONTAINER, "mp4", 4 );
  32. strncpy( m_sEncoderSettings.CODEC, "h264", 4 );
  33. }
  34. memset( &m_sDecoderVS, 0, sizeof(LIBAV_VS));
  35. memset( &m_sEncoderVS, 0, sizeof(LIBAV_VS));
  36. };
  37. servaMediaEncoder::~servaMediaEncoder(){
  38. if ( m_sEncoderVS.m_pFormatCtx ) {
  39. /* Free the streams. */
  40. for (int i = 0; i < m_sEncoderVS.m_pFormatCtx->nb_streams; i++) {
  41. av_freep(&m_sEncoderVS.m_pFormatCtx->streams[i]->codec);
  42. av_freep(&m_sEncoderVS.m_pFormatCtx->streams[i]);
  43. };
  44. av_free( m_sEncoderVS.m_pFormatCtx );
  45. };
  46. if (m_sEncoderVS.m_pCodecCtx) {
  47. avcodec_close( m_sEncoderVS.m_pCodecCtx );
  48. avcodec_free_context( &m_sEncoderVS.m_pCodecCtx );
  49. };
  50. if (m_sEncoderVS.m_pFormatOpts) av_free( m_sEncoderVS.m_pFormatOpts );
  51. };
  52. #include <sys/stat.h>
  53. bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int height, CSTR custom ){
  54. char Buffer[1024] = {0};
  55. int RetVal;
  56. if ( !initLibAV() ) return false;
  57. strncpy( m_sDecoderVS.m_pFileName, src, MAX_PATH_LEN);
  58. strncpy( m_sEncoderVS.m_pFileName, dst, MAX_PATH_LEN);
  59. struct stat st;
  60. stat( m_sDecoderVS.m_pFileName, &st );
  61. m_nInFileSize = st.st_size;
  62. //LOG("Before Open1");
  63. if( ( RetVal = avformat_open_input( &m_sDecoderVS.m_pFormatCtx, m_sDecoderVS.m_pFileName, NULL, &m_sDecoderVS.m_pFormatOpts) ) != 0 ){
  64. //av_dict_free(&options);
  65. char tBuffer[1024];
  66. av_strerror( RetVal, tBuffer, 1024 );
  67. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_sDecoderVS.m_pFileName );
  68. LOG_ERROR(Buffer);
  69. return false;
  70. };
  71. //LOG("After Open1");
  72. AVDictionaryEntry *e;
  73. if( e = av_dict_get( m_sDecoderVS.m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  74. sprintf( Buffer, "Option %s not recognized by the demuxer", e->key );
  75. LOG_ERROR(Buffer);
  76. };
  77. #ifdef DEBUG
  78. LOG("DataStream Opened");
  79. #endif
  80. m_sDecoderVS.m_bStatus = MS_OPENED;
  81. if( avformat_find_stream_info( m_sDecoderVS.m_pFormatCtx, NULL ) < 0 ){
  82. sprintf(Buffer, "Couldn't find stream information: %s", m_sDecoderVS.m_pFileName );
  83. LOG_ERROR( Buffer );
  84. return false;
  85. };
  86. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  87. for( unsigned int i=0; i < m_sDecoderVS.m_pFormatCtx->nb_streams; i++ ){
  88. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  89. if( m_sDecoderVS.m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  90. LOG( "VIDEO stream detected." );
  91. if( m_sDecoderVS.m_iVideoStreamID <0 ){
  92. LOG( "VIDEO stream assigned." );
  93. m_pVideoState->m_iVideoStreamID=i;
  94. };
  95. };
  96. };
  97. if( m_sDecoderVS.m_iVideoStreamID == -1 ){
  98. LOG_ERROR( "Didn't find a video stream.");
  99. return false;
  100. };
  101. // Stream found
  102. m_sDecoderVS.m_pCodecCtx = m_sDecoderVS.m_pFormatCtx->streams[m_sDecoderVS.m_iVideoStreamID]->codec;
  103. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  104. m_sDecoderVS.m_pVideoCodec = avcodec_find_decoder( m_sDecoderVS.m_pCodecCtx->codec_id );
  105. #else
  106. # ifdef DEBUG_EXTRA
  107. LOG("YABABA-2!");
  108. # endif
  109. /* find the video stream information */
  110. m_sDecoderVS.m_iVideoStreamID = av_find_best_stream( m_sDecoderVS.m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_sDecoderVS.m_pVideoCodec, 0);
  111. if (m_sDecoderVS.m_iVideoStreamID == -1){
  112. LOG_ERROR("Didn't find a video stream.");
  113. return false;
  114. };
  115. if (!(m_sDecoderVS.m_pCodecCtx = avcodec_alloc_context3(m_sDecoderVS.m_pVideoCodec))){
  116. LOG_ERROR("Can not create decoder context!");
  117. return false;
  118. };
  119. m_sDecoderVS.m_pVideoStr = m_sDecoderVS.m_pFormatCtx->streams[m_sDecoderVS.m_iVideoStreamID];
  120. if ( avcodec_parameters_to_context( m_sDecoderVS.m_pCodecCtx, m_sDecoderVS.m_pVideoStr->codecpar ) < 0 ){
  121. LOG_ERROR("Can not set Context parameters!");
  122. return false;
  123. };
  124. #endif
  125. if( m_sDecoderVS.m_pVideoCodec == NULL ){
  126. sprintf(Buffer, "Codec [%d] not found for %s", m_sDecoderVS.m_pCodecCtx->codec_id, m_sDecoderVS.m_pFileName );
  127. LOG_ERROR( Buffer );
  128. return false;
  129. };
  130. /*
  131. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  132. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  133. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  134. */
  135. //m_sDecoderVS.m_pCodecCtx->thread_count = 4;
  136. m_sDecoderVS.m_pCodecCtx->thread_type = m_sEncoderSettings.TH_MODE;//FF_THREAD_FRAME;
  137. //if (is_live)
  138. //m_sDecoderVS.m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  139. //else
  140. //m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  141. #ifdef DEBUG_EXTRA
  142. LOG("Before Open2");
  143. #endif
  144. if( avcodec_open2( m_sDecoderVS.m_pCodecCtx, m_sDecoderVS.m_pVideoCodec, NULL ) < 0 ){
  145. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_sDecoderVS.m_pCodecCtx->codec_id, m_sDecoderVS.m_pFileName );
  146. LOG_ERROR( Buffer );
  147. return false;
  148. };
  149. /* we do not send complete frames */
  150. if( m_sDecoderVS.m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_sDecoderVS.m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  151. #ifdef DEBUG
  152. char tB[256];
  153. sprintf( tB, "Setting Frame [%dx%d]", width, height );
  154. LOG( tB );
  155. sprintf( tB, "Source Frame [%dx%d]", m_sDecoderVS.m_pCodecCtx->width, m_sDecoderVS.m_pCodecCtx->height );
  156. LOG( tB );
  157. #endif
  158. /*
  159. if( ( width > 0 ) && ( height > 0 ) ){
  160. m_nOutWidth = width;
  161. m_nOutHeight = height;
  162. } else {
  163. if( m_bSizeIsAutomatic ){
  164. #ifdef DEBUG_EXTRA
  165. LOG("Size is Automatic");
  166. #endif
  167. if(( m_nOutWidth == -1 )&&( m_nOutHeight == -1 )){
  168. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  169. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  170. } else {
  171. // Proportional
  172. if( m_nOutHeight == -1 ){
  173. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  174. m_nOutHeight = (int)(((float)m_nOutWidth ) * k );
  175. } else {
  176. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  177. m_nOutWidth = (int)(((float)m_nOutHeight ) * k );
  178. };
  179. };
  180. } else {
  181. #ifdef DEBUG_EXTRA
  182. LOG("Size is Manual");
  183. #endif
  184. if( ( width > 0 ) && ( height > 0 ) ){
  185. m_nOutWidth = width;
  186. m_nOutHeight = height;
  187. } else {
  188. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  189. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  190. };
  191. };
  192. };
  193. #ifdef DEBUG_EXTRA
  194. sprintf( tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight );
  195. LOG( tB );
  196. sprintf(tB,"swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width,
  197. m_pVideoState->m_pCodecCtx->height,
  198. m_pVideoState->m_pCodecCtx->pix_fmt,
  199. m_nOutWidth,
  200. m_nOutHeight,
  201. AV_PIX_FMT_BGR24,
  202. SWS_BICUBIC );
  203. LOG( tB );
  204. #endif
  205. m_pVideoState->m_pSwsCtx = sws_getContext(
  206. m_pVideoState->m_pCodecCtx->width,
  207. m_pVideoState->m_pCodecCtx->height,
  208. m_pVideoState->m_pCodecCtx->pix_fmt,
  209. m_nOutWidth,
  210. m_nOutHeight,
  211. AV_PIX_FMT_BGR24,
  212. SWS_BICUBIC, //SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  213. NULL,
  214. NULL,
  215. NULL
  216. );
  217. if ( m_pVideoState->m_pSwsCtx == NULL ){
  218. LOG_ERROR("Error Allocating SwsCtx!");
  219. return false;
  220. };
  221. */
  222. m_bStatus = DECODER_INIT;
  223. /* Encoder */
  224. m_sEncoderVS.m_pFormatCtx = NULL;
  225. m_sEncoderVS.m_pFormatOpts = NULL;
  226. int ret = avformat_alloc_output_context2( &m_sEncoderVS.m_pFormatCtx, NULL, NULL, m_sEncoderVS.m_pFileName );
  227. if (ret < 0) {
  228. LOG_ERROR("VideoFormat Context Memory Allocation Error");
  229. return false;
  230. };
  231. LOG(m_sEncoderVS.m_pFileName);
  232. m_sEncoderVS.m_pFormatCtx->oformat = av_guess_format( NULL, m_sEncoderVS.m_pFileName, NULL);
  233. if (m_sEncoderVS.m_pFormatCtx->oformat == NULL){
  234. LOG_ERROR("VideoFormat Format Not Guessed!");
  235. return false;
  236. } else {
  237. LOG("Video Container Format Guessed");
  238. };
  239. //In Case mp4
  240. if ( strcasestr( m_sEncoderSettings.CONTAINER, "mp4" ) ) {
  241. if (av_dict_set(&m_sEncoderVS.m_pFormatOpts, "brand", "mp42", 0) < 0) {
  242. LOG_ERROR( "av_dict_set brand!" );
  243. };
  244. };
  245. m_sEncoderVS.m_pVideoStr = avformat_new_stream( m_sEncoderVS.m_pFormatCtx, m_sEncoderVS.m_pVideoCodec );
  246. if( m_sEncoderVS.m_pVideoStr == NULL ){
  247. LOG_ERROR("VideoStream Not Created!");
  248. }else{
  249. LOG("VideoStream is Created");
  250. };
  251. //m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder(av_guess_codec(m_sEncoderVS.m_pFormatCtx->oformat, NULL, m_sEncoderVS.m_pFileName, NULL, AVMEDIA_TYPE_VIDEO));
  252. if ( strcasestr( m_sEncoderSettings.CODEC, "h264" ) ) {
  253. m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder( AV_CODEC_ID_H264 );
  254. } else {
  255. m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder( AV_CODEC_ID_H265 );
  256. };
  257. if( m_sEncoderVS.m_pVideoCodec == NULL ){
  258. LOG_ERROR("VideoCodec Not Found!");
  259. return false;
  260. } else {
  261. LOG("Video Codec Found");
  262. };
  263. m_sEncoderVS.m_pCodecCtx = avcodec_alloc_context3( m_sEncoderVS.m_pVideoCodec );
  264. if (m_sEncoderVS.m_pCodecCtx == NULL){
  265. LOG_ERROR("VideoCodec Context Memory Allocation Error");
  266. return false;
  267. } else {
  268. LOG("Codec Context3 Allocated");
  269. };
  270. m_sEncoderVS.m_pCodecCtx->width = m_sDecoderVS.m_pCodecCtx->width;
  271. m_sEncoderVS.m_pCodecCtx->height = m_sDecoderVS.m_pCodecCtx->height;
  272. //const AVRational dst_fps = { fps, 1 };
  273. m_sEncoderVS.m_pVideoStr->time_base = av_make_q( 1, 10000 );// .num = 1;//av_inv_q(dst_fps);
  274. //m_sEncoderVS.m_pVideoStr->time_base.den = fps;
  275. m_sEncoderVS.m_pVideoStr->r_frame_rate = av_make_q( fps, 1);//.num = fps;
  276. //m_sEncoderVS.m_pVideoStr->r_frame_rate.den = 1;
  277. m_sEncoderVS.m_pVideoStr->avg_frame_rate = av_make_q( fps, 1); //.num = fps;
  278. //m_sEncoderVS.m_pVideoStr->avg_frame_rate.den = 1;
  279. m_sEncoderVS.m_pCodecCtx->time_base = av_make_q( 1, 10000 );//.num = 1;
  280. //m_sEncoderVS.m_pCodecCtx->time_base.den = fps;
  281. m_sEncoderVS.m_pCodecCtx->gop_size = fps << 1; // emit one intra frame every fps*2 frames
  282. m_sEncoderVS.m_pCodecCtx->bit_rate = m_sEncoderSettings.bitrate; //400000;
  283. m_sEncoderVS.m_pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
  284. //if (codec_id == AV_CODEC_ID_H264)
  285. //av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "slow", 0);
  286. av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", m_sEncoderSettings.preset, 0 );
  287. av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "profile", m_sEncoderSettings.profile, 0 );
  288. //m_pCodecCtx->max_b_frames = 1;
  289. //m_pCodecCtx->level = 31;
  290. /// P-frames, generated by referencing data from prev and future frames.
  291. /// [Compression up, CPU usage up]
  292. /// [use 3/gop]
  293. m_sEncoderVS.m_pCodecCtx->max_b_frames = 5;
  294. /// Can be used by a P-frame(predictive, partial frame) to help define a future frame in a compressed video.
  295. /// [use 3�5 ref per P]
  296. m_sEncoderVS.m_pCodecCtx->refs = 3;
  297. /// Compression efficiency (slower -> better quality + higher cpu%)
  298. /// [ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow]
  299. /// Set this option to "ultrafast" is critical for realtime encoding
  300. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "ultrafast", 0);
  301. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "fast", 0 );
  302. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "slow", 0 );
  303. /// Compression rate (lower -> higher compression) compress to lower size, makes decoded image more noisy
  304. /// Range: [0; 51], sane range: [18; 26]. I used 35 as good compression/quality compromise. This option also critical for realtime encoding
  305. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "crf", "20", 0);
  306. char tBuf[16]; sprintf( tBuf, "%d", m_sEncoderSettings.CRF );
  307. av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "crf", tBuf, 0 );
  308. /// Change settings based upon the specifics of input
  309. /// [psnr, ssim, grain, zerolatency, fastdecode, animation]
  310. /// This option is most critical for realtime encoding, because it removes delay between 1th input frame and 1th output packet.
  311. //av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "tune", "zerolatency", 0);
  312. av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "tune", m_sEncoderSettings.tune, 0);
  313. /// Disable b-pyramid. CLI options for this is "-b-pyramid 0"
  314. /// Because Quicktime (ie. iOS) doesn't support this option
  315. av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "b-pyramid", "0", 0 );
  316. if( avcodec_is_open( m_sEncoderVS.m_pCodecCtx ) == 0 ){
  317. LOG("output codec context is closed, opening");
  318. int ret = avcodec_open2( m_sEncoderVS.m_pCodecCtx, m_sEncoderVS.m_pVideoCodec, NULL );
  319. if( ret < 0 ){
  320. LOG_ERROR("Error opening2 encoder");
  321. char buffer[30];
  322. sprintf(buffer, "Error code: %i", ret );
  323. LOG_ERROR(buffer);
  324. return false;
  325. };
  326. } else {
  327. LOG("output codec context is opened");
  328. };
  329. m_sEncoderVS.m_pVideoStr->codec = m_sEncoderVS.m_pCodecCtx; //---->see next page
  330. if (avio_open(&m_sEncoderVS.m_pFormatCtx->pb, m_sEncoderVS.m_pFileName, AVIO_FLAG_WRITE ) < 0){
  331. LOG_ERROR("Could not open output file.");
  332. return false;
  333. };
  334. if (avformat_write_header(m_sEncoderVS.m_pFormatCtx, &m_sEncoderVS.m_pFormatOpts ) < 0){
  335. LOG_ERROR("Error occurred when writing header to output file.");
  336. return false;
  337. };
  338. m_sEncoderVS.m_iVideoStreamID = -1;
  339. #ifdef DEBUG
  340. LOG("Allocation Done");
  341. #endif
  342. m_bStatus = ENCODER_INIT;
  343. return init();
  344. };
  345. void servaMediaEncoder::m_fThreadProc( void ){
  346. AVPacket read_packet;
  347. AVPacket encoded_packet;
  348. uint32_t current_frame_num = 0;
  349. uint32_t frame_count = 0;
  350. uint8_t operc = 0;
  351. uint64_t pack_duration = m_sEncoderVS.m_pCodecCtx->time_base.den / m_sEncoderVS.m_pCodecCtx->time_base.num / m_sEncoderVS.m_pVideoStr->avg_frame_rate.num * m_sEncoderVS.m_pVideoStr->avg_frame_rate.den;
  352. int got_frame;
  353. int test;
  354. m_bStatus = THREAD_INIT;
  355. // Allocate video frame
  356. AVFrame *decoded_frame = av_frame_alloc();
  357. m_bThreadStatus = TH_WORKING;
  358. m_bStatus = ENCODER_ENCODING;
  359. m_nReadSize = 0;
  360. while( av_read_frame( m_sDecoderVS.m_pFormatCtx, &read_packet ) >= 0 && m_bThreadStatus < TH_ENDED ){
  361. m_nReadSize += read_packet.size;
  362. // Is this a packet from the video stream?
  363. if( read_packet.stream_index == m_sDecoderVS.m_iVideoStreamID ){
  364. // Decode video frame
  365. if( avcodec_decode_video2( m_sDecoderVS.m_pCodecCtx, decoded_frame, &got_frame, &read_packet ) < 0 ){
  366. m_bThreadStatus = TH_ENDED;
  367. continue;
  368. };
  369. // Did we get a video frame?
  370. if( got_frame ){
  371. av_init_packet( &encoded_packet );
  372. encoded_packet.data = NULL;
  373. encoded_packet.size = 0;
  374. decoded_frame->pts = (int64_t)current_frame_num * pack_duration;
  375. uint8_t perc = ( 100 * m_nReadSize ) / m_nInFileSize;
  376. if ( operc!= perc ) {
  377. operc == perc;
  378. m_pMaster->updateEncodeStatus(m_nRefID, MEDIA_ENCODE_PROGRESS, perc);
  379. };
  380. if( avcodec_encode_video2( m_sEncoderVS.m_pCodecCtx, &encoded_packet, decoded_frame, &got_frame) < 0 ){
  381. m_bThreadStatus = TH_ENDED;
  382. continue;
  383. };
  384. if( got_frame ){
  385. //if( m_sEncoderVS.m_pCodecCtx->coded_frame->pts != AV_NOPTS_VALUE ) encoded_packet.pts = av_rescale_q( m_sEncoderVS.m_pCodecCtx->coded_frame->pts, m_sEncoderVS.m_pCodecCtx->time_base, m_sEncoderVS.m_pVideoStr->time_base );
  386. if( m_sEncoderVS.m_pCodecCtx->coded_frame->key_frame ) encoded_packet.flags |= AV_PKT_FLAG_KEY;
  387. encoded_packet.stream_index = m_sEncoderVS.m_pVideoStr->index;
  388. //encoded_packet.dts = av_rescale_q( m_sEncoderVS.m_pCodecCtx->coded_frame->pts, m_sEncoderVS.m_pCodecCtx->time_base, m_sEncoderVS.m_pVideoStr->time_base );
  389. encoded_packet.duration = pack_duration;
  390. if( av_interleaved_write_frame( m_sEncoderVS.m_pFormatCtx, &encoded_packet ) < 0 ){
  391. m_bThreadStatus = TH_ENDED;
  392. continue;
  393. } else {
  394. m_nOutFileSize += encoded_packet.size;
  395. current_frame_num++;
  396. };
  397. };
  398. frame_count++;
  399. av_free_packet( &encoded_packet );
  400. };
  401. };
  402. // Free the packet that was allocated by av_read_frame
  403. av_free_packet( &read_packet );
  404. };
  405. // Close the video file
  406. avformat_close_input( &m_sDecoderVS.m_pFormatCtx );
  407. // Free the YUV frame
  408. av_frame_free( &decoded_frame );
  409. av_write_trailer( m_sEncoderVS.m_pFormatCtx );
  410. avio_close( m_sEncoderVS.m_pFormatCtx->pb );
  411. m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_ENCODE_PROGRESS, 100 );
  412. m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_READY, 100 );
  413. unlink( m_sDecoderVS.m_pFileName );
  414. m_bStatus = ENCODER_FINISHED;
  415. };
  416. uint8_t servaMediaEncoder::getStatus(){
  417. return m_bStatus;
  418. };
  419. #undef LOG
  420. #undef LOG_ERROR