mirror of
https://git.eden-emu.dev/eden-emu/eden
synced 2026-07-11 02:16:39 +02:00
372bdfccbc
This is the first step into the conversion on full GPU video decoding for Android; currently due to the VIC structure, I couldn't set it on surface mode to prevent the latency when sending video decoded (which is processed by GPU now), because currently we convert YUV420 into the Nvidia's format each frame constantly on CPU, aside the requirements from other extensions to work + VIC rewrite, which is a work currently not planned to happen on this PR; the actual configuration for video decoding is ByteBuffer, using GPU to decode NVDEC data (all codecs supported, h264, vp8 and vp9) and send it to CPU for display purposes, which is more faster than relying purely on CPU for any drawing task, saving devices resources/ heating, the downside on this will be the slight latency when a new frame is displayed, which is gonna be a black frame for less than a second, nothing major to harm the experience rather than actually trying our best to take advantage on hardware accelerated. Aside this, I also added a bunch of minor Vulkan fixes to grant drivers less thinkering when receiving spir-v instructions, meaning this has new bans for extensions on QCOM (following reported issues on other projects working around Adreno driver behavior), this more than providing performance aims to enhance the stability on the driver, performance it's gonna likely to be hit based on the UBO's (StorageBufferAccess) operations and SSBO (uniformStorageBufferAccess), there was an already existing path for the emulation which forces to wide them into 32bit packed operations. I also included some smaller changes/ bugs + VUID's fixes from earlier changes on my work. Special Thanks: -> Mr. Smoly Gidolard (@gidoly) Sources: 1.- https://github.com/microsoft/DirectXShaderCompiler/issues/2842 2.- https://github.com/mlc-ai/web-llm/issues/836 3.- https://github.com/ggml-org/llama.cpp/issues/5186 4.- https://github.com/encounter/aurora/pull/202 Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/4191
249 lines
5.4 KiB
C++
249 lines
5.4 KiB
C++
// SPDX-FileCopyrightText: Copyright 2026 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
|
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <vector>
|
|
#include <queue>
|
|
|
|
#include "common/common_funcs.h"
|
|
#include "common/common_types.h"
|
|
#include "video_core/host1x/nvdec_common.h"
|
|
|
|
extern "C" {
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wconversion"
|
|
#endif
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
#include <libavcodec/codec.h>
|
|
#include <libavutil/opt.h>
|
|
#include <libavutil/pixdesc.h>
|
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
#pragma GCC diagnostic pop
|
|
#endif
|
|
}
|
|
|
|
namespace Tegra {
|
|
class MemoryManager;
|
|
}
|
|
|
|
namespace FFmpeg {
|
|
|
|
class Packet;
|
|
class Frame;
|
|
class Decoder;
|
|
class HardwareContext;
|
|
class DecoderContext;
|
|
class DeinterlaceFilter;
|
|
|
|
// Wraps an AVPacket, a container for compressed bitstream data.
|
|
class Packet {
|
|
public:
|
|
YUZU_NON_COPYABLE(Packet);
|
|
YUZU_NON_MOVEABLE(Packet);
|
|
|
|
explicit Packet(std::span<const u8> data);
|
|
~Packet();
|
|
|
|
AVPacket* GetPacket() const {
|
|
return m_packet;
|
|
}
|
|
|
|
private:
|
|
AVPacket* m_packet{};
|
|
};
|
|
|
|
// Wraps an AVFrame, a container for audio and video stream data.
|
|
class Frame {
|
|
public:
|
|
YUZU_NON_COPYABLE(Frame);
|
|
YUZU_NON_MOVEABLE(Frame);
|
|
|
|
explicit Frame();
|
|
~Frame();
|
|
|
|
int GetWidth() const {
|
|
return m_frame->width;
|
|
}
|
|
|
|
int GetHeight() const {
|
|
return m_frame->height;
|
|
}
|
|
|
|
AVPixelFormat GetPixelFormat() const {
|
|
return static_cast<AVPixelFormat>(m_frame->format);
|
|
}
|
|
|
|
int GetStride(int plane) const {
|
|
return m_frame->linesize[plane];
|
|
}
|
|
|
|
int* GetStrides() const {
|
|
return m_frame->linesize;
|
|
}
|
|
|
|
u8* GetData(int plane) const {
|
|
return m_frame->data[plane];
|
|
}
|
|
|
|
const u8* GetPlane(int plane) const {
|
|
return m_frame->data[plane];
|
|
}
|
|
|
|
u8** GetPlanes() const {
|
|
return m_frame->data;
|
|
}
|
|
|
|
void SetFormat(int format) {
|
|
m_frame->format = format;
|
|
}
|
|
|
|
bool IsInterlaced() const {
|
|
#if defined(FF_API_INTERLACED_FRAME) || LIBAVUTIL_VERSION_MAJOR >= 59
|
|
return m_frame->flags & AV_FRAME_FLAG_INTERLACED;
|
|
#else
|
|
return m_frame->interlaced_frame;
|
|
#endif
|
|
}
|
|
|
|
bool IsHardwareDecoded() const {
|
|
return m_frame->hw_frames_ctx != nullptr;
|
|
}
|
|
|
|
AVFrame* GetFrame() const {
|
|
return m_frame;
|
|
}
|
|
|
|
private:
|
|
AVFrame* m_frame{};
|
|
};
|
|
|
|
// Wraps an AVCodec, a type containing information about a codec.
|
|
class Decoder {
|
|
public:
|
|
YUZU_NON_COPYABLE(Decoder);
|
|
YUZU_NON_MOVEABLE(Decoder);
|
|
|
|
explicit Decoder(Tegra::Host1x::NvdecCommon::VideoCodec codec);
|
|
~Decoder() = default;
|
|
|
|
bool SupportsDecodingOnDevice(AVPixelFormat* out_pix_fmt, AVHWDeviceType type) const;
|
|
|
|
const AVCodec* GetCodec() const {
|
|
return m_codec;
|
|
}
|
|
|
|
private:
|
|
const AVCodec* m_codec{};
|
|
};
|
|
|
|
// Wraps AVBufferRef for an accelerated decoder.
|
|
class HardwareContext {
|
|
public:
|
|
YUZU_NON_COPYABLE(HardwareContext);
|
|
YUZU_NON_MOVEABLE(HardwareContext);
|
|
|
|
static std::vector<AVHWDeviceType> GetSupportedDeviceTypes();
|
|
|
|
explicit HardwareContext() = default;
|
|
~HardwareContext();
|
|
|
|
bool InitializeForDecoder(DecoderContext& decoder_context, const Decoder& decoder);
|
|
|
|
AVBufferRef* GetBufferRef() const {
|
|
return m_gpu_decoder;
|
|
}
|
|
|
|
private:
|
|
bool InitializeWithType(AVHWDeviceType type);
|
|
|
|
AVBufferRef* m_gpu_decoder{};
|
|
};
|
|
|
|
// Wraps an AVCodecContext.
|
|
class DecoderContext {
|
|
public:
|
|
YUZU_NON_COPYABLE(DecoderContext);
|
|
YUZU_NON_MOVEABLE(DecoderContext);
|
|
|
|
explicit DecoderContext(const Decoder& decoder);
|
|
~DecoderContext();
|
|
|
|
void InitializeHardwareDecoder(const HardwareContext& context, AVPixelFormat hw_pix_fmt);
|
|
bool OpenContext(const Decoder& decoder, std::span<const u8> extradata = {});
|
|
bool SendPacket(const Packet& packet);
|
|
std::shared_ptr<Frame> ReceiveFrame();
|
|
|
|
AVCodecContext* GetCodecContext() const {
|
|
return m_codec_context;
|
|
}
|
|
|
|
bool UsingDecodeOrder() const {
|
|
return m_decode_order;
|
|
}
|
|
|
|
private:
|
|
const Decoder& m_decoder;
|
|
AVCodecContext* m_codec_context{};
|
|
std::shared_ptr<Frame> m_final_frame{};
|
|
bool m_decode_order{};
|
|
};
|
|
|
|
struct FrameOffsets {
|
|
bool interlaced{};
|
|
bool hidden{};
|
|
u64 luma{};
|
|
u64 luma_bottom{};
|
|
};
|
|
|
|
struct FrameDimensions {
|
|
s32 width{};
|
|
s32 height{};
|
|
};
|
|
|
|
class DecodeApi {
|
|
public:
|
|
YUZU_NON_COPYABLE(DecodeApi);
|
|
YUZU_NON_MOVEABLE(DecodeApi);
|
|
|
|
DecodeApi() = default;
|
|
~DecodeApi() = default;
|
|
|
|
bool Initialize(Tegra::Host1x::NvdecCommon::VideoCodec codec);
|
|
void Reset();
|
|
|
|
bool UsingDecodeOrder() const {
|
|
return m_decoder_context->UsingDecodeOrder();
|
|
}
|
|
|
|
bool SendPacket(std::span<const u8> packet_data, const FrameOffsets& offsets,
|
|
std::optional<FrameDimensions> dimensions = std::nullopt);
|
|
|
|
struct DecodedFrame {
|
|
std::shared_ptr<Frame> frame;
|
|
FrameOffsets offsets;
|
|
};
|
|
std::optional<DecodedFrame> ReceiveFrame();
|
|
|
|
private:
|
|
std::optional<FFmpeg::Decoder> m_decoder;
|
|
std::optional<FFmpeg::DecoderContext> m_decoder_context;
|
|
std::optional<FFmpeg::HardwareContext> m_hardware_context;
|
|
bool m_opened{};
|
|
bool m_defer_android_mediacodec_open{};
|
|
bool m_needs_h264_extradata{};
|
|
s64 m_next_pts{};
|
|
std::queue<FrameOffsets> m_pending_offsets;
|
|
};
|
|
|
|
} // namespace FFmpeg
|