Improve compatibility with default builds of FFmpeg on all platforms

This commit is contained in:
MaranBr 2025-07-20 12:54:17 -04:00 committed by crueter
parent acbecd9f8e
commit e52c0a93a0
2 changed files with 7 additions and 10 deletions

View file

@ -23,6 +23,8 @@ namespace FFmpeg {
namespace { namespace {
constexpr AVPixelFormat PreferredGpuFormat = AV_PIX_FMT_NV12;
constexpr AVPixelFormat PreferredCpuFormat = AV_PIX_FMT_YUV420P;
constexpr std::array PreferredGpuDecoders = { constexpr std::array PreferredGpuDecoders = {
#if defined (_WIN32) #if defined (_WIN32)
AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_CUDA,
@ -50,7 +52,7 @@ AVPixelFormat GetGpuFormat(AVCodecContext* codec_context, const AVPixelFormat* p
} }
// Fallback to CPU decoder. // Fallback to CPU decoder.
LOG_INFO(HW_GPU, "Could not find compatible GPU pixel format, falling back to CPU"); LOG_INFO(HW_GPU, "Could not find supported GPU pixel format, falling back to CPU decoder");
av_buffer_unref(&codec_context->hw_device_ctx); av_buffer_unref(&codec_context->hw_device_ctx);
return codec_context->pix_fmt; return codec_context->pix_fmt;
@ -213,7 +215,7 @@ bool DecoderContext::OpenContext(const Decoder& decoder) {
} }
if (!m_codec_context->hw_device_ctx) { if (!m_codec_context->hw_device_ctx) {
LOG_INFO(HW_GPU, "Using FFmpeg software decoding"); LOG_INFO(HW_GPU, "Using FFmpeg CPU decoding");
} }
return true; return true;
@ -244,13 +246,13 @@ std::shared_ptr<Frame> DecoderContext::ReceiveFrame() {
m_temp_frame = std::make_shared<Frame>(); m_temp_frame = std::make_shared<Frame>();
if (m_codec_context->hw_device_ctx) { 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) { 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)); LOG_ERROR(HW_GPU, "av_hwframe_transfer_data error: {}", AVError(ret));
return {}; return {};
} }
} else { } else {
m_temp_frame->SetFormat(AV_PIX_FMT_YUV420P); m_temp_frame->SetFormat(PreferredCpuFormat);
m_temp_frame = std::move(intermediate_frame); m_temp_frame = std::move(intermediate_frame);
} }

View file

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