From a1b7cd51b44d72b024294e95799d1edfe22ae17e Mon Sep 17 00:00:00 2001 From: CamilleLaVey Date: Tue, 7 Jul 2026 19:01:42 -0400 Subject: [PATCH] [android] Initial implementation for MediaCodec --- externals/ffmpeg/CMakeLists.txt | 5 +++++ src/common/android/id_cache.cpp | 4 ++++ src/common/android/id_cache.h | 1 + src/video_core/host1x/ffmpeg.cpp | 13 +++++++++++++ 4 files changed, 23 insertions(+) diff --git a/externals/ffmpeg/CMakeLists.txt b/externals/ffmpeg/CMakeLists.txt index 7908e0a619..0baff02c84 100644 --- a/externals/ffmpeg/CMakeLists.txt +++ b/externals/ffmpeg/CMakeLists.txt @@ -59,6 +59,11 @@ endif() if (PLATFORM_PS4 OR PLATFORM_MANAGARM) # Doesn't support VA-API, don't go thru the embarrassment of trying to enable it list(APPEND FFmpeg_HWACCEL_FLAGS --disable-vaapi) +elseif (ANDROID) + list(APPEND FFmpeg_HWACCEL_FLAGS + --enable-mediacodec + --enable-jni + ) elseif (UNIX AND NOT DEFINED FFmpeg_IS_CROSS_COMPILING AND NOT ANDROID) find_package(PkgConfig REQUIRED) pkg_check_modules(LIBVA libva) diff --git a/src/common/android/id_cache.cpp b/src/common/android/id_cache.cpp index 76af1e0fb2..a56dbc199b 100644 --- a/src/common/android/id_cache.cpp +++ b/src/common/android/id_cache.cpp @@ -123,6 +123,10 @@ namespace Common::Android { return owned.env; } + JavaVM *GetJavaVM() { + return s_java_vm; + } + jclass GetNativeLibraryClass() { return s_native_library_class; } diff --git a/src/common/android/id_cache.h b/src/common/android/id_cache.h index 6b48f471b7..2ae2cdc4cb 100644 --- a/src/common/android/id_cache.h +++ b/src/common/android/id_cache.h @@ -12,6 +12,7 @@ namespace Common::Android { JNIEnv* GetEnvForThread(); +JavaVM* GetJavaVM(); /** * Starts a new thread to run JNI. Intended to be used when you must run JNI from a fiber. diff --git a/src/video_core/host1x/ffmpeg.cpp b/src/video_core/host1x/ffmpeg.cpp index 88df6695d8..36364ba3c2 100644 --- a/src/video_core/host1x/ffmpeg.cpp +++ b/src/video_core/host1x/ffmpeg.cpp @@ -19,8 +19,16 @@ extern "C" { #endif #include + +#if defined(__ANDROID__) +#include +#endif } +#if defined(__ANDROID__) +#include "common/android/id_cache.h" +#endif + namespace FFmpeg { namespace { @@ -290,6 +298,11 @@ bool DecodeApi::Initialize(Tegra::Host1x::NvdecCommon::VideoCodec codec) { // Enable GPU decoding if requested. if (Settings::values.nvdec_emulation.GetValue() == Settings::NvdecEmulation::Gpu) { +#if defined(__ANDROID__) + static const int jni_vm_result = + av_jni_set_java_vm(Common::Android::GetJavaVM(), nullptr); + (void)jni_vm_result; +#endif m_hardware_context.emplace(); m_hardware_context->InitializeForDecoder(*m_decoder_context, *m_decoder); }