feat(encoder): added auto selection of the most suitable output pix_fmt

Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
k4yt3x
2024-11-10 00:00:00 +00:00
parent e477123e88
commit e393910f21
5 changed files with 85 additions and 12 deletions

View File

@@ -1,5 +1,10 @@
#include "avutils.h"
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/pixdesc.h>
}
#include <spdlog/spdlog.h>
int64_t get_video_frame_count(AVFormatContext *ifmt_ctx, int in_vstream_idx) {
@@ -48,3 +53,68 @@ int64_t get_video_frame_count(AVFormatContext *ifmt_ctx, int in_vstream_idx) {
// Estimate and return the total number of frames
return static_cast<int64_t>(duration_secs * fps);
}
enum AVPixelFormat
get_encoder_default_pix_fmt(const AVCodec *encoder, AVPixelFormat target_pix_fmt) {
int ret;
char errbuf[AV_ERROR_MAX_STRING_SIZE];
// Retrieve the list of supported pixel formats
const enum AVPixelFormat *supported_pix_fmts = nullptr;
ret = avcodec_get_supported_config(
nullptr, encoder, AV_CODEC_CONFIG_PIX_FORMAT, 0, (const void **)&supported_pix_fmts, nullptr
);
if (ret < 0) {
av_strerror(ret, errbuf, sizeof(errbuf));
spdlog::error("Failed to get supported pixel formats: {}", errbuf);
return AV_PIX_FMT_NONE;
}
if (supported_pix_fmts == nullptr) {
if (target_pix_fmt == AV_PIX_FMT_NONE) {
spdlog::warn("Encoder supports all pixel formats; defaulting to yuv420p");
return AV_PIX_FMT_YUV420P;
} else {
spdlog::warn("Encoder supports all pixel formats; defaulting to the decoder's format");
return target_pix_fmt;
}
}
// Determine if the target pixel format has an alpha channel
const AVPixFmtDescriptor *desc = nullptr;
int has_alpha = 0;
if (target_pix_fmt != AV_PIX_FMT_NONE) {
desc = av_pix_fmt_desc_get(target_pix_fmt);
has_alpha = desc ? (desc->nb_components % 2 == 0) : 0;
}
// Iterate over supported pixel formats to find the best match
enum AVPixelFormat best_pix_fmt = AV_PIX_FMT_NONE;
for (const enum AVPixelFormat *p = supported_pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
if (target_pix_fmt != AV_PIX_FMT_NONE) {
best_pix_fmt =
av_find_best_pix_fmt_of_2(best_pix_fmt, *p, target_pix_fmt, has_alpha, nullptr);
if (*p == target_pix_fmt) {
best_pix_fmt = target_pix_fmt;
break;
}
} else {
best_pix_fmt = *p;
break;
}
}
if (best_pix_fmt == AV_PIX_FMT_NONE) {
spdlog::error("No suitable pixel format found for encoder");
}
if (target_pix_fmt != AV_PIX_FMT_NONE && best_pix_fmt != target_pix_fmt) {
spdlog::warn(
"Incompatible pixel format '%s' for encoder '%s'; auto-selecting format '%s'",
av_get_pix_fmt_name(target_pix_fmt),
encoder->name,
av_get_pix_fmt_name(best_pix_fmt)
);
}
return best_pix_fmt;
}