[host1x] Fix hardware detection and improve compatibility #85

Merged
crueter merged 15 commits from ffmpeg-fix into master 2025-07-22 07:31:37 +02:00
2 changed files with 145 additions and 150 deletions

View file

@ -23,6 +23,8 @@ namespace FFmpeg {
namespace {
constexpr AVPixelFormat PreferredGpuFormat = AV_PIX_FMT_NV12;
constexpr AVPixelFormat PreferredCpuFormat = AV_PIX_FMT_YUV420P;
constexpr std::array PreferredGpuDecoders = {
#if defined (_WIN32)
AV_HWDEVICE_TYPE_CUDA,
@ -39,32 +41,31 @@ constexpr std::array PreferredGpuDecoders = {
};
AVPixelFormat GetGpuFormat(AVCodecContext* codec_context, const AVPixelFormat* pix_fmts) {
// Check if there is a pixel format supported by the GPU decoder.
const auto desc = av_pix_fmt_desc_get(codec_context->pix_fmt);
if (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
for (const AVPixelFormat* p = pix_fmts; *p != AV_PIX_FMT_NONE; ++p) {
if (*p == codec_context->pix_fmt) {
return codec_context->pix_fmt;
}
}
}
// Another check to confirm if there is a pixel format supported by specific GPU decoders.
if (desc && !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
for (int i = 0;; i++) {
const AVCodecHWConfig* config = avcodec_get_hw_config(codec_context->codec, i);
if (!config) {
break;
}
if ((config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) && (config->device_type == AV_HWDEVICE_TYPE_CUDA || config->device_type == AV_HWDEVICE_TYPE_VAAPI)) {
return config->pix_fmt;
for (const auto type : PreferredGpuDecoders) {
if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX && config->device_type == type) {
codec_context->pix_fmt = config->pix_fmt;
}
}
}
}
// Fallback to CPU decoder.
LOG_INFO(HW_GPU, "Could not find compatible GPU pixel format, falling back to CPU");
for (const AVPixelFormat* p = pix_fmts; *p != AV_PIX_FMT_NONE; ++p) {
if (*p == codec_context->pix_fmt) {
return codec_context->pix_fmt;
}
}
LOG_INFO(HW_GPU, "Could not find supported GPU pixel format, falling back to CPU decoder");
av_buffer_unref(&codec_context->hw_device_ctx);
codec_context->pix_fmt = PreferredCpuFormat;
return codec_context->pix_fmt;
}
@ -225,7 +226,7 @@ bool DecoderContext::OpenContext(const Decoder& decoder) {
}
if (!m_codec_context->hw_device_ctx) {
LOG_INFO(HW_GPU, "Using FFmpeg software decoding");
LOG_INFO(HW_GPU, "Using FFmpeg CPU decoder");
}
return true;
@ -256,13 +257,12 @@ std::shared_ptr<Frame> DecoderContext::ReceiveFrame() {
m_temp_frame = std::make_shared<Frame>();
if (m_codec_context->hw_device_ctx) {
m_temp_frame->SetFormat(AV_PIX_FMT_NV12);
m_temp_frame->SetFormat(PreferredGpuFormat);
if (int ret = av_hwframe_transfer_data(m_temp_frame->GetFrame(), intermediate_frame->GetFrame(), 0); ret < 0) {
LOG_ERROR(HW_GPU, "av_hwframe_transfer_data error: {}", AVError(ret));
return {};
}
} else {
m_temp_frame->SetFormat(AV_PIX_FMT_YUV420P);
m_temp_frame = std::move(intermediate_frame);
}

View file

@ -23,15 +23,10 @@ extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavcodec/codec.h>
#include <libavutil/opt.h>
#include <libavutil/pixdesc.h>
#if defined(__FreeBSD__)
#include <libavcodec/codec.h>
#else
#include <libavcodec/codec_internal.h>
#endif
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif