|
|
|
@ -19,6 +19,20 @@ servaMediaEncoder::servaMediaEncoder( uint64_t Ref ):basicThread(){ |
|
|
|
m_nInFileSize = 0; |
|
|
|
m_nOutFileSize = 0; |
|
|
|
m_bStatus = ENCODER_NONE; |
|
|
|
DBC_SERVA_MEDIA_ENCODER_SETTINGS *tData = NULL;//m_pDBC->getMediaEncoderSettings();
|
|
|
|
if( tData ){ |
|
|
|
memcpy( &m_sEncoderSettings, tData, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ) ); |
|
|
|
} else { |
|
|
|
memset( &m_sEncoderSettings, 0, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS )); |
|
|
|
m_sEncoderSettings.CRF = 20; |
|
|
|
strncpy( m_sEncoderSettings.preset, "medium", 15 ); |
|
|
|
strncpy( m_sEncoderSettings.tune, "fastdecode", 15 ); |
|
|
|
strncpy( m_sEncoderSettings.profile, "high422", 15 ); |
|
|
|
m_sEncoderSettings.TH_MODE = FF_THREAD_FRAME; |
|
|
|
m_sEncoderSettings.bitrate = 400000; |
|
|
|
strncpy( m_sEncoderSettings.CONTAINER, "mp4", 4 ); |
|
|
|
strncpy( m_sEncoderSettings.CODEC, "h264", 4 ); |
|
|
|
} |
|
|
|
memset( &m_sDecoderVS, 0, sizeof(LIBAV_VS)); |
|
|
|
memset( &m_sEncoderVS, 0, sizeof(LIBAV_VS)); |
|
|
|
}; |
|
|
|
@ -133,7 +147,7 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int |
|
|
|
Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool. |
|
|
|
*/ |
|
|
|
//m_sDecoderVS.m_pCodecCtx->thread_count = 4;
|
|
|
|
m_sDecoderVS.m_pCodecCtx->thread_type = FF_THREAD_FRAME; |
|
|
|
m_sDecoderVS.m_pCodecCtx->thread_type = m_sEncoderSettings.TH_MODE;//FF_THREAD_FRAME;
|
|
|
|
//if (is_live)
|
|
|
|
//m_sDecoderVS.m_pCodecCtx->thread_count = FF_THREAD_SLICE;
|
|
|
|
//else
|
|
|
|
@ -242,9 +256,11 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int |
|
|
|
} else { |
|
|
|
LOG("Video Container Format Guessed"); |
|
|
|
}; |
|
|
|
|
|
|
|
if( av_dict_set( &m_sEncoderVS.m_pFormatOpts, "brand", "mp42", 0) < 0 ){ |
|
|
|
LOG_ERROR( "av_dict_set brand!" ); |
|
|
|
//In Case mp4
|
|
|
|
if ( strcasestr( m_sEncoderSettings.CONTAINER, "mp4" ) ) { |
|
|
|
if (av_dict_set(&m_sEncoderVS.m_pFormatOpts, "brand", "mp42", 0) < 0) { |
|
|
|
LOG_ERROR( "av_dict_set brand!" ); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
m_sEncoderVS.m_pVideoStr = avformat_new_stream( m_sEncoderVS.m_pFormatCtx, m_sEncoderVS.m_pVideoCodec ); |
|
|
|
@ -253,9 +269,14 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int |
|
|
|
LOG_ERROR("VideoStream Not Created!"); |
|
|
|
}else{ |
|
|
|
LOG("VideoStream is Created"); |
|
|
|
}; |
|
|
|
|
|
|
|
m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder( av_guess_codec( m_sEncoderVS.m_pFormatCtx->oformat, NULL, m_sEncoderVS.m_pFileName, NULL, AVMEDIA_TYPE_VIDEO ) ); |
|
|
|
}; |
|
|
|
|
|
|
|
//m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder(av_guess_codec(m_sEncoderVS.m_pFormatCtx->oformat, NULL, m_sEncoderVS.m_pFileName, NULL, AVMEDIA_TYPE_VIDEO));
|
|
|
|
if ( strcasestr( m_sEncoderSettings.CODEC, "h264" ) ) { |
|
|
|
m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder( AV_CODEC_ID_H264 ); |
|
|
|
} else { |
|
|
|
m_sEncoderVS.m_pVideoCodec = avcodec_find_encoder( AV_CODEC_ID_H265 ); |
|
|
|
}; |
|
|
|
|
|
|
|
if( m_sEncoderVS.m_pVideoCodec == NULL ){ |
|
|
|
LOG_ERROR("VideoCodec Not Found!"); |
|
|
|
@ -285,10 +306,13 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int |
|
|
|
m_sEncoderVS.m_pCodecCtx->time_base = av_make_q( 1, 10000 );//.num = 1;
|
|
|
|
//m_sEncoderVS.m_pCodecCtx->time_base.den = fps;
|
|
|
|
m_sEncoderVS.m_pCodecCtx->gop_size = fps << 1; // emit one intra frame every fps*2 frames
|
|
|
|
m_sEncoderVS.m_pCodecCtx->bit_rate = 400000; |
|
|
|
m_sEncoderVS.m_pCodecCtx->bit_rate = m_sEncoderSettings.bitrate; //400000;
|
|
|
|
m_sEncoderVS.m_pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; |
|
|
|
//if (codec_id == AV_CODEC_ID_H264)
|
|
|
|
av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "slow", 0); |
|
|
|
|
|
|
|
//av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", "slow", 0);
|
|
|
|
av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "preset", m_sEncoderSettings.preset, 0 ); |
|
|
|
av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "profile", m_sEncoderSettings.profile, 0 ); |
|
|
|
//m_pCodecCtx->max_b_frames = 1;
|
|
|
|
//m_pCodecCtx->level = 31;
|
|
|
|
|
|
|
|
@ -311,12 +335,15 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int |
|
|
|
|
|
|
|
/// Compression rate (lower -> higher compression) compress to lower size, makes decoded image more noisy
|
|
|
|
/// Range: [0; 51], sane range: [18; 26]. I used 35 as good compression/quality compromise. This option also critical for realtime encoding
|
|
|
|
av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "crf", "20", 0); |
|
|
|
//av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "crf", "20", 0);
|
|
|
|
char tBuf[16]; sprintf( tBuf, "%d", m_sEncoderSettings.CRF ); |
|
|
|
av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "crf", tBuf, 0 ); |
|
|
|
|
|
|
|
/// Change settings based upon the specifics of input
|
|
|
|
/// [psnr, ssim, grain, zerolatency, fastdecode, animation]
|
|
|
|
/// This option is most critical for realtime encoding, because it removes delay between 1th input frame and 1th output packet.
|
|
|
|
/// This option is most critical for realtime encoding, because it removes delay between 1th input frame and 1th output packet.
|
|
|
|
//av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "tune", "zerolatency", 0);
|
|
|
|
av_opt_set( m_sEncoderVS.m_pCodecCtx->priv_data, "tune", m_sEncoderSettings.tune, 0); |
|
|
|
|
|
|
|
/// Disable b-pyramid. CLI options for this is "-b-pyramid 0"
|
|
|
|
/// Because Quicktime (ie. iOS) doesn't support this option
|
|
|
|
|