Update ffmpeg codec configures

This commit is contained in:
dijunkun
2023-11-08 15:54:08 +08:00
parent 99360f5da2
commit 27bfcb7621
5 changed files with 22 additions and 43 deletions

View File

@@ -47,7 +47,7 @@ FfmpegVideoDecoder::~FfmpegVideoDecoder() {
}
int FfmpegVideoDecoder::Init() {
// av_log_set_level(AV_LOG_DEBUG);
av_log_set_level(AV_LOG_ERROR);
codec_id_ = AV_CODEC_ID_H264;
codec_ = avcodec_find_decoder(codec_id_);
@@ -59,6 +59,8 @@ int FfmpegVideoDecoder::Init() {
if (!codec_ctx_) {
printf("Could not allocate video codec context\n");
return -1;
} else {
LOG_INFO("Use H264 decoder [{}]", codec_->name);
}
codec_ctx_->time_base.num = 1;
@@ -68,8 +70,8 @@ int FfmpegVideoDecoder::Init() {
codec_ctx_->time_base.den = 29;
codec_ctx_->width = 1280;
codec_ctx_->height = 720;
// codec_ctx_->pix_fmt = AV_PIX_FMT_NV12; // yuv420 default?
codec_ctx_->color_range = AVCOL_RANGE_JPEG;
codec_ctx_->pix_fmt = AV_PIX_FMT_YUV420P; // yuv420 default?
codec_ctx_->color_range = AVCOL_RANGE_MPEG;
if (avcodec_open2(codec_ctx_, codec_, NULL) < 0) {
printf("Could not open codec\n");

View File

@@ -59,19 +59,23 @@ FFmpegVideoEncoder::~FFmpegVideoEncoder() {
}
int FFmpegVideoEncoder::Init() {
// av_log_set_level(AV_LOG_VERBOSE);
av_log_set_level(AV_LOG_ERROR);
codec_ = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!codec_) {
LOG_ERROR("Failed to find H.264 encoder");
return -1;
} else {
if (0 == strcmp(codec_->name, "openh264")) {
#ifdef __linux__
if (0 != strcmp(codec_->name, "openh264")) {
use_openh264_ = true;
LOG_INFO("Use H264 encoder [OpenH264]");
}
#else
LOG_INFO("Use H264 encoder [{}]", codec_->name);
#endif
}
use_openh264_ = true;
// use_openh264_ = true;
codec_ctx_ = avcodec_alloc_context3(codec_);
if (!codec_ctx_) {
@@ -93,10 +97,14 @@ int FFmpegVideoEncoder::Init() {
codec_ctx_->gop_size = keyFrameInterval_;
codec_ctx_->keyint_min = keyFrameInterval_;
codec_ctx_->max_b_frames = 0;
codec_ctx_->bit_rate = maxBitrate_ * 2500;
codec_ctx_->bit_rate = maxBitrate_ * 2000;
codec_ctx_->qmin = 15;
codec_ctx_->qmax = 35;
codec_ctx_->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
codec_ctx_->flags2 |= AV_CODEC_FLAG2_LOCAL_HEADER;
av_opt_set_int(codec_ctx_->priv_data, "qp", 51, 0);
av_opt_set_int(codec_ctx_->priv_data, "crf", 23, 0);
// av_opt_set_int(codec_ctx_->priv_data, "qp", 51, 0);
// av_opt_set_int(codec_ctx_->priv_data, "crf", 23, 0);
if (!use_openh264_) {
av_opt_set(codec_ctx_->priv_data, "profile", "baseline", 0);