|
|
@ -24,12 +24,12 @@ servaMediaEncoder::servaMediaEncoder( uint64_t Ref ):basicThread(){ |
|
|
memcpy( &m_sEncoderSettings, tData, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ) ); |
|
|
memcpy( &m_sEncoderSettings, tData, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS ) ); |
|
|
} else { |
|
|
} else { |
|
|
memset( &m_sEncoderSettings, 0, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS )); |
|
|
memset( &m_sEncoderSettings, 0, sizeof( DBC_SERVA_MEDIA_ENCODER_SETTINGS )); |
|
|
m_sEncoderSettings.CRF = 20; |
|
|
|
|
|
strncpy( m_sEncoderSettings.preset, "medium", 15 ); |
|
|
|
|
|
|
|
|
m_sEncoderSettings.CRF = 28;//20;
|
|
|
|
|
|
strncpy( m_sEncoderSettings.preset, "slow", 15 ); |
|
|
strncpy( m_sEncoderSettings.tune, "fastdecode", 15 ); |
|
|
strncpy( m_sEncoderSettings.tune, "fastdecode", 15 ); |
|
|
strncpy( m_sEncoderSettings.profile, "high422", 15 ); |
|
|
|
|
|
|
|
|
strncpy( m_sEncoderSettings.profile, "main", 15 ); |
|
|
m_sEncoderSettings.TH_MODE = FF_THREAD_FRAME; |
|
|
m_sEncoderSettings.TH_MODE = FF_THREAD_FRAME; |
|
|
m_sEncoderSettings.bitrate = 400000; |
|
|
|
|
|
|
|
|
m_sEncoderSettings.bitrate = 666; |
|
|
strncpy( m_sEncoderSettings.CONTAINER, "mp4", 4 ); |
|
|
strncpy( m_sEncoderSettings.CONTAINER, "mp4", 4 ); |
|
|
strncpy( m_sEncoderSettings.CODEC, "h264", 4 ); |
|
|
strncpy( m_sEncoderSettings.CODEC, "h264", 4 ); |
|
|
} |
|
|
} |
|
|
@ -53,6 +53,7 @@ servaMediaEncoder::~servaMediaEncoder(){ |
|
|
av_free( m_sEncoderVS.m_pFormatCtx ); |
|
|
av_free( m_sEncoderVS.m_pFormatCtx ); |
|
|
}; |
|
|
}; |
|
|
if (m_sEncoderVS.m_pFormatOpts) av_free( m_sEncoderVS.m_pFormatOpts ); |
|
|
if (m_sEncoderVS.m_pFormatOpts) av_free( m_sEncoderVS.m_pFormatOpts ); |
|
|
|
|
|
LOG("Destructor Exit"); |
|
|
}; |
|
|
}; |
|
|
#include <sys/stat.h>
|
|
|
#include <sys/stat.h>
|
|
|
bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int height, CSTR custom ){ |
|
|
bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int height, CSTR custom ){ |
|
|
@ -311,15 +312,15 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int |
|
|
m_sEncoderVS.m_pCodecCtx->height = m_sDecoderVS.m_pCodecCtx->height; |
|
|
m_sEncoderVS.m_pCodecCtx->height = m_sDecoderVS.m_pCodecCtx->height; |
|
|
//const AVRational dst_fps = { fps, 1 };
|
|
|
//const AVRational dst_fps = { fps, 1 };
|
|
|
|
|
|
|
|
|
m_sEncoderVS.m_pVideoStr->time_base = av_make_q( 1, 10000 );// .num = 1;//av_inv_q(dst_fps);
|
|
|
|
|
|
|
|
|
m_sEncoderVS.m_pVideoStr->time_base = av_make_q( 1, 150000 );// .num = 1;//av_inv_q(dst_fps);
|
|
|
//m_sEncoderVS.m_pVideoStr->time_base.den = fps;
|
|
|
//m_sEncoderVS.m_pVideoStr->time_base.den = fps;
|
|
|
m_sEncoderVS.m_pVideoStr->r_frame_rate = av_make_q( fps, 1);//.num = fps;
|
|
|
|
|
|
|
|
|
m_sEncoderVS.m_pVideoStr->r_frame_rate = av_make_q( fps, 1 );//.num = fps;
|
|
|
//m_sEncoderVS.m_pVideoStr->r_frame_rate.den = 1;
|
|
|
//m_sEncoderVS.m_pVideoStr->r_frame_rate.den = 1;
|
|
|
m_sEncoderVS.m_pVideoStr->avg_frame_rate = av_make_q( fps, 1); //.num = fps;
|
|
|
|
|
|
|
|
|
m_sEncoderVS.m_pVideoStr->avg_frame_rate = av_make_q( fps, 1 ); //.num = fps;
|
|
|
//m_sEncoderVS.m_pVideoStr->avg_frame_rate.den = 1;
|
|
|
//m_sEncoderVS.m_pVideoStr->avg_frame_rate.den = 1;
|
|
|
m_sEncoderVS.m_pCodecCtx->time_base = av_make_q( 1, 10000 );//.num = 1;
|
|
|
|
|
|
|
|
|
m_sEncoderVS.m_pCodecCtx->time_base = av_make_q( 1, 150000 );//.num = 1;
|
|
|
//m_sEncoderVS.m_pCodecCtx->time_base.den = fps;
|
|
|
//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->gop_size = fps << 1; // emit one intra frame every fps*2 frames
|
|
|
m_sEncoderVS.m_pCodecCtx->bit_rate = m_sEncoderSettings.bitrate; //400000;
|
|
|
m_sEncoderVS.m_pCodecCtx->bit_rate = m_sEncoderSettings.bitrate; //400000;
|
|
|
m_sEncoderVS.m_pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; |
|
|
m_sEncoderVS.m_pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; |
|
|
//if (codec_id == AV_CODEC_ID_H264)
|
|
|
//if (codec_id == AV_CODEC_ID_H264)
|
|
|
@ -327,13 +328,13 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int |
|
|
//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, "preset", m_sEncoderSettings.preset, 0 ); |
|
|
av_opt_set(m_sEncoderVS.m_pCodecCtx->priv_data, "profile", m_sEncoderSettings.profile, 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;
|
|
|
|
|
|
|
|
|
m_sEncoderVS.m_pCodecCtx->max_b_frames = m_sEncoderVS.m_pCodecCtx->gop_size * 3; |
|
|
|
|
|
m_sEncoderVS.m_pCodecCtx->level = 31; |
|
|
|
|
|
|
|
|
/// P-frames, generated by referencing data from prev and future frames.
|
|
|
/// P-frames, generated by referencing data from prev and future frames.
|
|
|
/// [Compression up, CPU usage up]
|
|
|
/// [Compression up, CPU usage up]
|
|
|
/// [use 3/gop]
|
|
|
/// [use 3/gop]
|
|
|
m_sEncoderVS.m_pCodecCtx->max_b_frames = 5; |
|
|
|
|
|
|
|
|
//m_sEncoderVS.m_pCodecCtx->max_b_frames = 5;
|
|
|
|
|
|
|
|
|
/// Can be used by a P-frame(predictive, partial frame) to help define a future frame in a compressed video.
|
|
|
/// Can be used by a P-frame(predictive, partial frame) to help define a future frame in a compressed video.
|
|
|
/// [use 3–5 ref per P]
|
|
|
/// [use 3–5 ref per P]
|
|
|
@ -400,20 +401,25 @@ bool servaMediaEncoder::create( CSTR src, CSTR dst, uint8_t fps, int width, int |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
void servaMediaEncoder::m_fThreadProc( void ){ |
|
|
void servaMediaEncoder::m_fThreadProc( void ){ |
|
|
AVPacket read_packet; |
|
|
|
|
|
AVPacket encoded_packet; |
|
|
|
|
|
uint32_t current_frame_num = 0; |
|
|
|
|
|
uint32_t frame_count = 0; |
|
|
|
|
|
uint8_t operc = 0; |
|
|
|
|
|
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; |
|
|
|
|
|
int got_frame; |
|
|
|
|
|
int test; |
|
|
|
|
|
|
|
|
AVPacket read_packet; |
|
|
|
|
|
AVPacket encoded_packet; |
|
|
|
|
|
uint64_t current_frame_num = 0; |
|
|
|
|
|
uint64_t frame_count = 0; |
|
|
|
|
|
uint8_t operc = 0; |
|
|
|
|
|
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 + 666; |
|
|
|
|
|
int got_frame; |
|
|
|
|
|
//int test;
|
|
|
m_bStatus = THREAD_INIT; |
|
|
m_bStatus = THREAD_INIT; |
|
|
// Allocate video frame
|
|
|
// Allocate video frame
|
|
|
AVFrame *decoded_frame = av_frame_alloc(); |
|
|
AVFrame *decoded_frame = av_frame_alloc(); |
|
|
m_bThreadStatus = TH_WORKING; |
|
|
m_bThreadStatus = TH_WORKING; |
|
|
m_bStatus = ENCODER_ENCODING; |
|
|
m_bStatus = ENCODER_ENCODING; |
|
|
m_nReadSize = 0; |
|
|
m_nReadSize = 0; |
|
|
|
|
|
char tB[128]; |
|
|
|
|
|
sprintf( tB, "Pack Duration: %" PRIu64, pack_duration ); |
|
|
|
|
|
LOG(tB); |
|
|
while( av_read_frame( m_sDecoderVS.m_pFormatCtx, &read_packet ) >= 0 && m_bThreadStatus < TH_ENDED ){ |
|
|
while( av_read_frame( m_sDecoderVS.m_pFormatCtx, &read_packet ) >= 0 && m_bThreadStatus < TH_ENDED ){ |
|
|
m_nReadSize += read_packet.size; |
|
|
m_nReadSize += read_packet.size; |
|
|
// Is this a packet from the video stream?
|
|
|
// Is this a packet from the video stream?
|
|
|
@ -428,7 +434,7 @@ void servaMediaEncoder::m_fThreadProc( void ){ |
|
|
av_init_packet( &encoded_packet ); |
|
|
av_init_packet( &encoded_packet ); |
|
|
encoded_packet.data = NULL; |
|
|
encoded_packet.data = NULL; |
|
|
encoded_packet.size = 0; |
|
|
encoded_packet.size = 0; |
|
|
decoded_frame->pts = (int64_t)current_frame_num * pack_duration; |
|
|
|
|
|
|
|
|
decoded_frame->pts = current_frame_num * pack_duration; |
|
|
|
|
|
|
|
|
uint8_t perc = ( 100 * m_nReadSize ) / m_nInFileSize; |
|
|
uint8_t perc = ( 100 * m_nReadSize ) / m_nInFileSize; |
|
|
if ( operc!= perc ) { |
|
|
if ( operc!= perc ) { |
|
|
@ -441,11 +447,14 @@ void servaMediaEncoder::m_fThreadProc( void ){ |
|
|
continue; |
|
|
continue; |
|
|
}; |
|
|
}; |
|
|
if( got_frame ){ |
|
|
if( got_frame ){ |
|
|
//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 );
|
|
|
|
|
|
|
|
|
|
|
|
if( m_sEncoderVS.m_pCodecCtx->coded_frame->key_frame ) encoded_packet.flags |= AV_PKT_FLAG_KEY; |
|
|
if( m_sEncoderVS.m_pCodecCtx->coded_frame->key_frame ) encoded_packet.flags |= AV_PKT_FLAG_KEY; |
|
|
encoded_packet.stream_index = m_sEncoderVS.m_pVideoStr->index; |
|
|
|
|
|
|
|
|
encoded_packet.stream_index = m_sEncoderVS.m_pVideoStr->index; |
|
|
|
|
|
//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 );
|
|
|
|
|
|
//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 );
|
|
|
//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 );
|
|
|
//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 );
|
|
|
encoded_packet.duration = pack_duration; |
|
|
|
|
|
|
|
|
//if( m_sEncoderVS.m_pCodecCtx->coded_frame->pts != AV_NOPTS_VALUE )
|
|
|
|
|
|
encoded_packet.duration = pack_duration; |
|
|
if( av_interleaved_write_frame( m_sEncoderVS.m_pFormatCtx, &encoded_packet ) < 0 ){ |
|
|
if( av_interleaved_write_frame( m_sEncoderVS.m_pFormatCtx, &encoded_packet ) < 0 ){ |
|
|
m_bThreadStatus = TH_ENDED; |
|
|
m_bThreadStatus = TH_ENDED; |
|
|
continue; |
|
|
continue; |
|
|
@ -470,7 +479,7 @@ void servaMediaEncoder::m_fThreadProc( void ){ |
|
|
m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_ENCODE_PROGRESS, 100 ); |
|
|
m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_ENCODE_PROGRESS, 100 ); |
|
|
m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_READY, 100 ); |
|
|
m_pMaster->updateEncodeStatus( m_nRefID, MEDIA_READY, 100 ); |
|
|
unlink( m_sDecoderVS.m_pFileName ); |
|
|
unlink( m_sDecoderVS.m_pFileName ); |
|
|
#ifdef DEBUG_EXTRA
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
LOG("Encoding Done!"); |
|
|
LOG("Encoding Done!"); |
|
|
#endif
|
|
|
#endif
|
|
|
m_bStatus = ENCODER_FINISHED; |
|
|
m_bStatus = ENCODER_FINISHED; |
|
|
|