From 1a35aef644e89cfe77bf2d6d5967e463e79c53f9 Mon Sep 17 00:00:00 2001 From: crueter Date: Fri, 18 Jul 2025 20:02:13 +0200 Subject: [PATCH 1/7] [android] Better error handling for username collision/validity (#76) also removed the generate button from sw keyboard Signed-off-by: crueter Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/76 --- CMakeModules/DownloadExternals.cmake | 2 +- .../yuzu/yuzu_emu/dialogs/NetPlayDialog.kt | 35 ++++++------ .../settings/model/view/SettingsItem.kt | 9 +-- .../settings/ui/SettingsDialogFragment.kt | 7 +-- .../yuzu_emu/network/NetDataValidators.kt | 56 +++++++++++++++++++ .../src/main/res/layout/dialog_edit_text.xml | 1 + .../app/src/main/res/values/strings.xml | 11 ++-- 7 files changed, 87 insertions(+), 34 deletions(-) create mode 100644 src/android/app/src/main/java/org/yuzu/yuzu_emu/network/NetDataValidators.kt diff --git a/CMakeModules/DownloadExternals.cmake b/CMakeModules/DownloadExternals.cmake index e65827290d..3e9bc26d69 100644 --- a/CMakeModules/DownloadExternals.cmake +++ b/CMakeModules/DownloadExternals.cmake @@ -133,7 +133,7 @@ function(download_qt_configuration prefix_out target host type arch arch_path ba set(install_args ${install_args} install-tool --outputdir ${base_path} ${host} desktop ${target}) else() set(prefix "${base_path}/${target}/${arch_path}") - set(install_args ${install_args} install-qt --outputdir ${base_path} ${host} ${type} ${target} ${arch} -m qtbase) + set(install_args ${install_args} install-qt --outputdir ${base_path} ${host} ${type} ${target} ${arch} -m qt_base) if (YUZU_USE_QT_MULTIMEDIA) set(install_args ${install_args} qtmultimedia) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/dialogs/NetPlayDialog.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/dialogs/NetPlayDialog.kt index ede2cfafa4..cd3e9a4474 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/dialogs/NetPlayDialog.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/dialogs/NetPlayDialog.kt @@ -34,6 +34,7 @@ import org.yuzu.yuzu_emu.databinding.ItemBanListBinding import org.yuzu.yuzu_emu.databinding.ItemButtonNetplayBinding import org.yuzu.yuzu_emu.databinding.ItemTextNetplayBinding import org.yuzu.yuzu_emu.features.settings.model.StringSetting +import org.yuzu.yuzu_emu.network.NetDataValidators import org.yuzu.yuzu_emu.network.NetPlayManager import org.yuzu.yuzu_emu.utils.CompatUtils import org.yuzu.yuzu_emu.utils.GameHelper @@ -102,8 +103,16 @@ class NetPlayDialog(context: Context) : BottomSheetDialog(context) { dismiss() } btnLobbyBrowser.setOnClickListener { - LobbyBrowser(context).show() - dismiss() + if (!NetDataValidators.username()) { + Toast.makeText( + context, + R.string.multiplayer_nickname_invalid, + Toast.LENGTH_LONG + ).show() + } else { + LobbyBrowser(context).show() + dismiss() + } } } } @@ -368,7 +377,7 @@ class NetPlayDialog(context: Context) : BottomSheetDialog(context) { ) ) { override fun validate(s: String): Boolean { - return s.length in 3..20 + return NetDataValidators.roomName(s) } } @@ -378,7 +387,7 @@ class NetPlayDialog(context: Context) : BottomSheetDialog(context) { context.getString(R.string.multiplayer_required) ) { override fun validate(s: String): Boolean { - return s.isNotEmpty() + return NetDataValidators.notEmpty(s) } } @@ -388,12 +397,7 @@ class NetPlayDialog(context: Context) : BottomSheetDialog(context) { context.getString(R.string.multiplayer_token_required) ) { override fun validate(s: String): Boolean { - if (s != context.getString(R.string.multiplayer_public_visibility)) { - return true; - } - - val token = StringSetting.WEB_TOKEN.getString() - return token.matches(Regex("[a-z]{48}")) + return NetDataValidators.roomVisibility(s, context) } } @@ -403,12 +407,7 @@ class NetPlayDialog(context: Context) : BottomSheetDialog(context) { context.getString(R.string.multiplayer_ip_error) ) { override fun validate(s: String): Boolean { - return try { - InetAddress.getByName(s) - s.length >= 7 - } catch (_: Exception) { - false - } + return NetDataValidators.ipAddress(s) } } @@ -418,7 +417,7 @@ class NetPlayDialog(context: Context) : BottomSheetDialog(context) { context.getString(R.string.multiplayer_username_error) ) { override fun validate(s: String): Boolean { - return s.length in 4..20 + return NetDataValidators.username(s) } } @@ -428,7 +427,7 @@ class NetPlayDialog(context: Context) : BottomSheetDialog(context) { context.getString(R.string.multiplayer_port_error) ) { override fun validate(s: String): Boolean { - return s.toIntOrNull() in 1..65535 + return NetDataValidators.port(s) } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SettingsItem.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SettingsItem.kt index ab35a9180c..a269cab254 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SettingsItem.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SettingsItem.kt @@ -20,6 +20,7 @@ import org.yuzu.yuzu_emu.features.settings.model.IntSetting import org.yuzu.yuzu_emu.features.settings.model.LongSetting import org.yuzu.yuzu_emu.features.settings.model.ShortSetting import org.yuzu.yuzu_emu.features.settings.model.StringSetting +import org.yuzu.yuzu_emu.network.NetDataValidators import org.yuzu.yuzu_emu.utils.GpuDriverHelper import org.yuzu.yuzu_emu.utils.NativeConfig @@ -300,9 +301,7 @@ abstract class SettingsItem( val chars = "abcdefghijklmnopqrstuvwxyz" (1..48).map { chars.random() }.joinToString("") }, - validator = { s -> - s?.matches(Regex("[a-z]{48}")) == true - }, + validator = NetDataValidators::token, errorId = R.string.multiplayer_token_error ) ) @@ -312,9 +311,7 @@ abstract class SettingsItem( StringSetting.WEB_USERNAME, titleId = R.string.web_username, descriptionId = R.string.web_username_description, - validator = { s -> - s?.length in 4..20 - }, + validator = NetDataValidators::username, errorId = R.string.multiplayer_username_error ) ) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsDialogFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsDialogFragment.kt index e50ebe50f4..aa17d05e34 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsDialogFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/SettingsDialogFragment.kt @@ -154,8 +154,6 @@ class SettingsDialogFragment : DialogFragment(), DialogInterface.OnClickListener stringInputBinding.generate.setOnClickListener { stringInputBinding.editText.setText(onGenerate()) } - } else { - stringInputBinding.generate.isVisible = false } val validator = item.validator @@ -179,8 +177,9 @@ class SettingsDialogFragment : DialogFragment(), DialogInterface.OnClickListener } override fun afterTextChanged(s: Editable?) { - stringInputBinding.editText.error = - if (validator(s.toString())) null else requireContext().getString(item.errorId) + val isValid = validator(s.toString()) + stringInputBinding.editTextLayout.isErrorEnabled = !isValid + stringInputBinding.editTextLayout.error = if (isValid) null else requireContext().getString(item.errorId) } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/network/NetDataValidators.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/network/NetDataValidators.kt new file mode 100644 index 0000000000..b3edf35d8e --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/network/NetDataValidators.kt @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +package org.yuzu.yuzu_emu.network + +import android.content.Context +import org.yuzu.yuzu_emu.R +import org.yuzu.yuzu_emu.features.settings.model.StringSetting +import java.net.InetAddress + +object NetDataValidators { + fun roomName(s: String): Boolean { + return s.length in 3..20 + } + + fun notEmpty(s: String): Boolean { + return s.isNotEmpty() + } + + fun token(s: String?): Boolean { + return s?.matches(Regex("[a-z]{48}")) == true + } + + fun token(): Boolean { + return token(StringSetting.WEB_TOKEN.getString()) + } + + fun roomVisibility(s: String, context: Context): Boolean { + if (s != context.getString(R.string.multiplayer_public_visibility)) { + return true; + } + + return token() + } + + fun ipAddress(s: String): Boolean { + return try { + InetAddress.getByName(s) + s.length >= 7 + } catch (_: Exception) { + false + } + } + + fun username(s: String?): Boolean { + return s?.matches(Regex("^[ a-zA-Z0-9._-]{4,20}$")) == true + } + + fun username(): Boolean { + return username(StringSetting.WEB_USERNAME.getString()) + } + + fun port(s: String): Boolean { + return s.toIntOrNull() in 1..65535 + } +} \ No newline at end of file diff --git a/src/android/app/src/main/res/layout/dialog_edit_text.xml b/src/android/app/src/main/res/layout/dialog_edit_text.xml index 3612f46eae..bc7864a395 100644 --- a/src/android/app/src/main/res/layout/dialog_edit_text.xml +++ b/src/android/app/src/main/res/layout/dialog_edit_text.xml @@ -27,6 +27,7 @@ android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="@string/generate" + android:visibility="gone" app:layout_constraintEnd_toEndOf="@+id/edit_text_layout" app:layout_constraintTop_toBottomOf="@+id/edit_text_layout" /> diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml index e611e66c1f..3814105ea0 100644 --- a/src/android/app/src/main/res/values/strings.xml +++ b/src/android/app/src/main/res/values/strings.xml @@ -148,18 +148,18 @@ Failed to join room Name is too short Invalid address - Invalid port! + Invalid port Exit Room Network error Lost connection - Name collision + Username already taken MAC Address collision Console ID collision Wrong version Wrong password Could not connect Room is full - Host banned + You are banned from this room Permission denied No such user Already in room @@ -221,7 +221,8 @@ Required Web Token required, go to Advanced Settings -> System -> Network Invalid IP format - Must be between 4–20 characters + Must be between 4–20 characters, and contain alphanumeric characters, periods, dashes, underscores, and spaces only + Username invalid, ensure it is set properly in System -> Network Must be 48 characters, and lowercase a-z only Must be between 1 and 65535 Cancel @@ -467,7 +468,7 @@ Web Token Web token used for creating public lobbies. It is a 48-character string containing only lowercase a-z. Web Username - Username to be shown in multiplayer lobbies. It must be 4–20 characters. + Username to be shown in multiplayer lobbies. It must be 4–20 characters, containing only alphanumeric characters, dashes, periods, underscores, and spaces. Network From ba00b39af688ca229af8ba5b23206a208de977f3 Mon Sep 17 00:00:00 2001 From: MaranBr Date: Mon, 14 Jul 2025 20:57:24 -0400 Subject: [PATCH 2/7] Update the preferred GPU decoder list for each platform --- src/video_core/host1x/ffmpeg/ffmpeg.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/video_core/host1x/ffmpeg/ffmpeg.cpp b/src/video_core/host1x/ffmpeg/ffmpeg.cpp index d6eff2bdd7..be9f09e3c9 100644 --- a/src/video_core/host1x/ffmpeg/ffmpeg.cpp +++ b/src/video_core/host1x/ffmpeg/ffmpeg.cpp @@ -26,14 +26,16 @@ namespace { constexpr AVPixelFormat PreferredGpuFormat = AV_PIX_FMT_NV12; constexpr AVPixelFormat PreferredCpuFormat = AV_PIX_FMT_YUV420P; constexpr std::array PreferredGpuDecoders = { -#ifdef _WIN32 +#if defined (_WIN32) AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_D3D11VA, AV_HWDEVICE_TYPE_DXVA2, #elif defined(__FreeBSD__) AV_HWDEVICE_TYPE_VDPAU, #elif defined(__unix__) + AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_VAAPI, + AV_HWDEVICE_TYPE_VDPAU, #endif AV_HWDEVICE_TYPE_VULKAN, }; From 7f89e3353e64cc6936a940b8c464952e099bf9e8 Mon Sep 17 00:00:00 2001 From: MaranBr Date: Tue, 15 Jul 2025 08:19:04 -0400 Subject: [PATCH 3/7] Clean up the code --- src/video_core/host1x/ffmpeg/ffmpeg.cpp | 5 ++++- src/video_core/host1x/ffmpeg/ffmpeg.h | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/video_core/host1x/ffmpeg/ffmpeg.cpp b/src/video_core/host1x/ffmpeg/ffmpeg.cpp index be9f09e3c9..4af03b8c27 100644 --- a/src/video_core/host1x/ffmpeg/ffmpeg.cpp +++ b/src/video_core/host1x/ffmpeg/ffmpeg.cpp @@ -105,6 +105,7 @@ bool Decoder::SupportsDecodingOnDevice(AVPixelFormat* out_pix_fmt, AVHWDeviceTyp LOG_DEBUG(HW_GPU, "{} decoder does not support device type {}", m_codec->name, av_hwdevice_get_type_name(type)); break; } + if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX && config->device_type == type) { LOG_INFO(HW_GPU, "Using {} GPU decoder", av_hwdevice_get_type_name(type)); *out_pix_fmt = config->pix_fmt; @@ -218,10 +219,12 @@ bool DecoderContext::OpenContext(const Decoder& decoder) { bool DecoderContext::SendPacket(const Packet& packet) { m_temp_frame = std::make_shared(); + if (const int ret = avcodec_send_packet(m_codec_context, packet.GetPacket()); ret < 0 && ret != AVERROR_EOF) { LOG_ERROR(HW_GPU, "avcodec_send_packet error: {}", AVError(ret)); return false; } + return true; } @@ -240,7 +243,7 @@ std::shared_ptr DecoderContext::ReceiveFrame() { } const auto desc = av_pix_fmt_desc_get(intermediate_frame->GetPixelFormat()); - if (m_codec_context->hw_device_ctx && (desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) { + if (m_codec_context->hw_device_ctx && desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) { 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)); diff --git a/src/video_core/host1x/ffmpeg/ffmpeg.h b/src/video_core/host1x/ffmpeg/ffmpeg.h index 6200fa691d..0864fd0cbe 100644 --- a/src/video_core/host1x/ffmpeg/ffmpeg.h +++ b/src/video_core/host1x/ffmpeg/ffmpeg.h @@ -26,7 +26,6 @@ extern "C" { #include #include -// Works quite fine, and omits the hacky ffmpeg building for now... #if defined(__FreeBSD__) #include #else From 4feb09b9db1759c144c167a6f0ed3fd68de5cd68 Mon Sep 17 00:00:00 2001 From: MaranBr Date: Wed, 16 Jul 2025 17:14:27 -0400 Subject: [PATCH 4/7] Fix hardware decoding for VP8 video codec --- src/video_core/host1x/ffmpeg/ffmpeg.cpp | 68 +++++++++++++++---------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/src/video_core/host1x/ffmpeg/ffmpeg.cpp b/src/video_core/host1x/ffmpeg/ffmpeg.cpp index 4af03b8c27..8cdaabda2a 100644 --- a/src/video_core/host1x/ffmpeg/ffmpeg.cpp +++ b/src/video_core/host1x/ffmpeg/ffmpeg.cpp @@ -23,8 +23,6 @@ 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, @@ -41,17 +39,34 @@ constexpr std::array PreferredGpuDecoders = { }; AVPixelFormat GetGpuFormat(AVCodecContext* codec_context, const AVPixelFormat* pix_fmts) { - for (const AVPixelFormat* p = pix_fmts; *p != AV_PIX_FMT_NONE; ++p) { - if (*p == codec_context->pix_fmt) { - return codec_context->pix_fmt; - } - } + 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) { + if (config->device_type == AV_HWDEVICE_TYPE_CUDA) { + return AV_PIX_FMT_CUDA; + } else if (config->device_type == AV_HWDEVICE_TYPE_VAAPI) { + return AV_PIX_FMT_VAAPI; + } + } + } + + 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; + } + } + } LOG_INFO(HW_GPU, "Could not find compatible GPU AV format, falling back to CPU"); av_buffer_unref(&codec_context->hw_device_ctx); - codec_context->pix_fmt = PreferredCpuFormat; - return codec_context->pix_fmt; + return AV_PIX_FMT_YUV420P; } std::string AVError(int errnum) { @@ -60,7 +75,7 @@ std::string AVError(int errnum) { return errbuf; } -} // namespace +} Packet::Packet(std::span data) { m_packet = av_packet_alloc(); @@ -83,16 +98,16 @@ Frame::~Frame() { Decoder::Decoder(Tegra::Host1x::NvdecCommon::VideoCodec codec) { const AVCodecID av_codec = [&] { switch (codec) { - case Tegra::Host1x::NvdecCommon::VideoCodec::H264: - return AV_CODEC_ID_H264; - case Tegra::Host1x::NvdecCommon::VideoCodec::VP8: - return AV_CODEC_ID_VP8; - case Tegra::Host1x::NvdecCommon::VideoCodec::VP9: - return AV_CODEC_ID_VP9; - default: - UNIMPLEMENTED_MSG("Unknown codec {}", codec); - return AV_CODEC_ID_NONE; - } + case Tegra::Host1x::NvdecCommon::VideoCodec::H264: + return AV_CODEC_ID_H264; + case Tegra::Host1x::NvdecCommon::VideoCodec::VP8: + return AV_CODEC_ID_VP8; + case Tegra::Host1x::NvdecCommon::VideoCodec::VP9: + return AV_CODEC_ID_VP9; + default: + UNIMPLEMENTED_MSG("Unknown codec {}", codec); + return AV_CODEC_ID_NONE; + } }(); m_codec = avcodec_find_decoder(av_codec); @@ -218,17 +233,17 @@ bool DecoderContext::OpenContext(const Decoder& decoder) { } bool DecoderContext::SendPacket(const Packet& packet) { - m_temp_frame = std::make_shared(); - if (const int ret = avcodec_send_packet(m_codec_context, packet.GetPacket()); ret < 0 && ret != AVERROR_EOF) { LOG_ERROR(HW_GPU, "avcodec_send_packet error: {}", AVError(ret)); return false; } - + return true; } std::shared_ptr DecoderContext::ReceiveFrame() { + m_temp_frame = std::make_shared(); + auto ReceiveImpl = [&](AVFrame* frame) -> bool { if (const int ret = avcodec_receive_frame(m_codec_context, frame); ret < 0 && ret != AVERROR_EOF) { LOG_ERROR(HW_GPU, "avcodec_receive_frame error: {}", AVError(ret)); @@ -242,9 +257,8 @@ std::shared_ptr DecoderContext::ReceiveFrame() { return {}; } - const auto desc = av_pix_fmt_desc_get(intermediate_frame->GetPixelFormat()); - if (m_codec_context->hw_device_ctx && desc && desc->flags & AV_PIX_FMT_FLAG_HWACCEL) { - m_temp_frame->SetFormat(PreferredGpuFormat); + if (m_codec_context->hw_device_ctx) { + m_temp_frame->SetFormat(AV_PIX_FMT_NV12); 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 {}; @@ -292,4 +306,4 @@ std::shared_ptr DecodeApi::ReceiveFrame() { return m_decoder_context->ReceiveFrame(); } -} // namespace FFmpeg +} From 8870538499586e02e3cffd4ade73060be9495c78 Mon Sep 17 00:00:00 2001 From: MaranBr Date: Thu, 17 Jul 2025 09:48:04 -0400 Subject: [PATCH 5/7] Add some comments and organize some things --- src/video_core/host1x/ffmpeg/ffmpeg.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/video_core/host1x/ffmpeg/ffmpeg.cpp b/src/video_core/host1x/ffmpeg/ffmpeg.cpp index 8cdaabda2a..3735906c54 100644 --- a/src/video_core/host1x/ffmpeg/ffmpeg.cpp +++ b/src/video_core/host1x/ffmpeg/ffmpeg.cpp @@ -39,6 +39,7 @@ constexpr std::array PreferredGpuDecoders = { }; AVPixelFormat GetGpuFormat(AVCodecContext* codec_context, const AVPixelFormat* pix_fmts) { + // Check if there is a specific pixel format supported by the GPU decoder. for (int i = 0;; i++) { const AVCodecHWConfig* config = avcodec_get_hw_config(codec_context->codec, i); if (!config) { @@ -54,6 +55,7 @@ AVPixelFormat GetGpuFormat(AVCodecContext* codec_context, const AVPixelFormat* p } } + // Check if there is another 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) { @@ -63,7 +65,8 @@ AVPixelFormat GetGpuFormat(AVCodecContext* codec_context, const AVPixelFormat* p } } - LOG_INFO(HW_GPU, "Could not find compatible GPU AV format, falling back to CPU"); + // Fallback to CPU decoder. + LOG_INFO(HW_GPU, "Could not find compatible GPU pixel format, falling back to CPU"); av_buffer_unref(&codec_context->hw_device_ctx); return AV_PIX_FMT_YUV420P; @@ -242,8 +245,6 @@ bool DecoderContext::SendPacket(const Packet& packet) { } std::shared_ptr DecoderContext::ReceiveFrame() { - m_temp_frame = std::make_shared(); - auto ReceiveImpl = [&](AVFrame* frame) -> bool { if (const int ret = avcodec_receive_frame(m_codec_context, frame); ret < 0 && ret != AVERROR_EOF) { LOG_ERROR(HW_GPU, "avcodec_receive_frame error: {}", AVError(ret)); @@ -257,6 +258,7 @@ std::shared_ptr DecoderContext::ReceiveFrame() { return {}; } + m_temp_frame = std::make_shared(); if (m_codec_context->hw_device_ctx) { m_temp_frame->SetFormat(AV_PIX_FMT_NV12); if (int ret = av_hwframe_transfer_data(m_temp_frame->GetFrame(), intermediate_frame->GetFrame(), 0); ret < 0) { From b1a71a0715fed61cebe4f599ad06d58d7e363ab7 Mon Sep 17 00:00:00 2001 From: MaranBr Date: Thu, 17 Jul 2025 23:06:14 -0400 Subject: [PATCH 6/7] Simplify the code --- src/video_core/host1x/ffmpeg/ffmpeg.cpp | 36 +++++++++++-------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/video_core/host1x/ffmpeg/ffmpeg.cpp b/src/video_core/host1x/ffmpeg/ffmpeg.cpp index 3735906c54..40f9bca170 100644 --- a/src/video_core/host1x/ffmpeg/ffmpeg.cpp +++ b/src/video_core/host1x/ffmpeg/ffmpeg.cpp @@ -1,6 +1,3 @@ -// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project -// SPDX-License-Identifier: GPL-3.0-or-later - // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later @@ -39,23 +36,7 @@ constexpr std::array PreferredGpuDecoders = { }; AVPixelFormat GetGpuFormat(AVCodecContext* codec_context, const AVPixelFormat* pix_fmts) { - // Check if there is a specific pixel format supported by the GPU decoder. - 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) { - if (config->device_type == AV_HWDEVICE_TYPE_CUDA) { - return AV_PIX_FMT_CUDA; - } else if (config->device_type == AV_HWDEVICE_TYPE_VAAPI) { - return AV_PIX_FMT_VAAPI; - } - } - } - - // Check if there is another pixel format supported by the GPU decoder. + // 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) { @@ -65,11 +46,23 @@ AVPixelFormat GetGpuFormat(AVCodecContext* codec_context, const AVPixelFormat* p } } + // Another check to confirm if there is a pixel format supported by specific GPU decoders. + 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; + } + } + // Fallback to CPU decoder. LOG_INFO(HW_GPU, "Could not find compatible GPU pixel format, falling back to CPU"); av_buffer_unref(&codec_context->hw_device_ctx); - return AV_PIX_FMT_YUV420P; + return codec_context->pix_fmt; } std::string AVError(int errnum) { @@ -266,6 +259,7 @@ std::shared_ptr DecoderContext::ReceiveFrame() { return {}; } } else { + m_temp_frame->SetFormat(AV_PIX_FMT_YUV420P); m_temp_frame = std::move(intermediate_frame); } From cf90ae644eea3c3692b5b9c97adda964338d6bed Mon Sep 17 00:00:00 2001 From: MaranBr Date: Thu, 17 Jul 2025 23:09:21 -0400 Subject: [PATCH 7/7] Fix headers --- src/video_core/host1x/ffmpeg/ffmpeg.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/video_core/host1x/ffmpeg/ffmpeg.cpp b/src/video_core/host1x/ffmpeg/ffmpeg.cpp index 40f9bca170..3170f53884 100644 --- a/src/video_core/host1x/ffmpeg/ffmpeg.cpp +++ b/src/video_core/host1x/ffmpeg/ffmpeg.cpp @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later