Files
eden/src/video_core/host1x/codecs/codec.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3.3 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2020-10-26 23:07:36 -04:00
#include "common/assert.h"
#include "common/settings.h"
2022-01-30 10:31:13 +01:00
#include "video_core/host1x/codecs/codec.h"
#include "video_core/host1x/codecs/h264.h"
#include "video_core/host1x/codecs/vp8.h"
#include "video_core/host1x/codecs/vp9.h"
2022-01-30 22:26:01 +01:00
#include "video_core/host1x/host1x.h"
2020-10-26 23:07:36 -04:00
#include "video_core/memory_manager.h"
namespace Tegra {
2020-11-25 17:10:44 -05:00
2022-01-30 22:26:01 +01:00
Codec::Codec(Host1x::Host1x& host1x_, const Host1x::NvdecCommon::NvdecRegisters& regs)
: host1x(host1x_), state{regs}, h264_decoder(std::make_unique<Decoder::H264>(host1x)),
vp8_decoder(std::make_unique<Decoder::VP8>(host1x)),
vp9_decoder(std::make_unique<Decoder::VP9>(host1x)) {}
2020-10-26 23:07:36 -04:00
Codec::~Codec() = default;
void Codec::Initialize() {
initialized = decode_api.Initialize(current_codec);
}
2022-01-30 10:31:13 +01:00
void Codec::SetTargetCodec(Host1x::NvdecCommon::VideoCodec codec) {
2020-11-23 13:25:01 -05:00
if (current_codec != codec) {
current_codec = codec;
LOG_INFO(Service_NVDRV, "NVDEC video codec initialized to {}", GetCurrentCodecName());
2020-11-23 13:25:01 -05:00
}
2020-10-26 23:07:36 -04:00
}
void Codec::Decode() {
const bool is_first_frame = !initialized;
if (is_first_frame) {
Initialize();
2020-10-26 23:07:36 -04:00
}
if (!initialized) {
return;
}
// Assemble bitstream.
bool vp9_hidden_frame = false;
size_t configuration_size = 0;
const auto packet_data = [&]() {
2021-11-12 19:28:21 -05:00
switch (current_codec) {
2022-01-30 10:31:13 +01:00
case Tegra::Host1x::NvdecCommon::VideoCodec::H264:
return h264_decoder->ComposeFrame(state, &configuration_size, is_first_frame);
2022-01-30 10:31:13 +01:00
case Tegra::Host1x::NvdecCommon::VideoCodec::VP8:
return vp8_decoder->ComposeFrame(state);
2022-01-30 10:31:13 +01:00
case Tegra::Host1x::NvdecCommon::VideoCodec::VP9:
vp9_decoder->ComposeFrame(state);
2021-11-12 19:28:21 -05:00
vp9_hidden_frame = vp9_decoder->WasFrameHidden();
return vp9_decoder->GetFrameBytes();
default:
ASSERT(false);
2023-06-24 21:58:23 -04:00
return std::span<const u8>{};
2021-11-12 19:28:21 -05:00
}
}();
// Send assembled bitstream to decoder.
if (!decode_api.SendPacket(packet_data, configuration_size)) {
return;
}
// Only receive/store visible frames.
if (vp9_hidden_frame) {
return;
}
// Receive output frames from decoder.
decode_api.ReceiveFrames(frames);
while (frames.size() > 10) {
LOG_DEBUG(HW_GPU, "ReceiveFrames overflow, dropped frame");
frames.pop();
2020-10-26 23:07:36 -04:00
}
}
std::unique_ptr<FFmpeg::Frame> Codec::GetCurrentFrame() {
2020-11-25 17:10:44 -05:00
// Sometimes VIC will request more frames than have been decoded.
// in this case, return a blank frame and don't overwrite previous data.
if (frames.empty()) {
return {};
2020-11-25 17:10:44 -05:00
}
auto frame = std::move(frames.front());
frames.pop();
2020-11-26 00:18:26 -05:00
return frame;
2020-10-26 23:07:36 -04:00
}
2022-01-30 10:31:13 +01:00
Host1x::NvdecCommon::VideoCodec Codec::GetCurrentCodec() const {
2020-10-26 23:07:36 -04:00
return current_codec;
}
std::string_view Codec::GetCurrentCodecName() const {
switch (current_codec) {
2022-01-30 10:31:13 +01:00
case Host1x::NvdecCommon::VideoCodec::None:
return "None";
2022-01-30 10:31:13 +01:00
case Host1x::NvdecCommon::VideoCodec::H264:
return "H264";
2022-01-30 10:31:13 +01:00
case Host1x::NvdecCommon::VideoCodec::VP8:
return "VP8";
2022-01-30 10:31:13 +01:00
case Host1x::NvdecCommon::VideoCodec::H265:
return "H265";
2022-01-30 10:31:13 +01:00
case Host1x::NvdecCommon::VideoCodec::VP9:
return "VP9";
default:
return "Unknown";
}
}
2020-10-26 23:07:36 -04:00
} // namespace Tegra