From eab041866b7c766aa38258aecef8a00c03612459 Mon Sep 17 00:00:00 2001 From: ameerj Date: Wed, 25 Nov 2020 17:10:44 -0500 Subject: Queue decoded frames, cleanup decoders --- src/video_core/command_classes/codecs/codec.cpp | 30 ++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/video_core/command_classes/codecs/codec.cpp') diff --git a/src/video_core/command_classes/codecs/codec.cpp b/src/video_core/command_classes/codecs/codec.cpp index 1adf3cd13..1a19341c8 100644 --- a/src/video_core/command_classes/codecs/codec.cpp +++ b/src/video_core/command_classes/codecs/codec.cpp @@ -18,6 +18,11 @@ extern "C" { namespace Tegra { +void av_frame_deleter(AVFrame* ptr) { + av_frame_unref(ptr); + av_free(ptr); +} + Codec::Codec(GPU& gpu_) : gpu(gpu_), h264_decoder(std::make_unique(gpu)), vp9_decoder(std::make_unique(gpu)) {} @@ -27,7 +32,9 @@ Codec::~Codec() { return; } // Free libav memory + AVFrame* av_frame{nullptr}; avcodec_send_packet(av_codec_ctx, nullptr); + av_frame = av_frame_alloc(); avcodec_receive_frame(av_codec_ctx, av_frame); avcodec_flush_buffers(av_codec_ctx); @@ -60,7 +67,7 @@ void Codec::Decode() { } av_codec_ctx = avcodec_alloc_context3(av_codec); - av_frame = av_frame_alloc(); + av_codec_ctx->refcounted_frames = 1; av_opt_set(av_codec_ctx->priv_data, "tune", "zerolatency", 0); // TODO(ameerj): libavcodec gpu hw acceleration @@ -68,8 +75,6 @@ void Codec::Decode() { const auto av_error = avcodec_open2(av_codec_ctx, av_codec, nullptr); if (av_error < 0) { LOG_ERROR(Service_NVDRV, "avcodec_open2() Failed."); - av_frame_unref(av_frame); - av_free(av_frame); avcodec_close(av_codec_ctx); return; } @@ -96,16 +101,21 @@ void Codec::Decode() { if (!vp9_hidden_frame) { // Only receive/store visible frames - avcodec_receive_frame(av_codec_ctx, av_frame); + AVFramePtr frame = AVFramePtr{av_frame_alloc(), av_frame_deleter}; + avcodec_receive_frame(av_codec_ctx, frame.get()); + av_frames.push(std::move(frame)); } } -AVFrame* Codec::GetCurrentFrame() { - return av_frame; -} - -const AVFrame* Codec::GetCurrentFrame() const { - return av_frame; +AVFramePtr Codec::GetCurrentFrame() { + // Sometimes VIC will request more frames than have been decoded. + // in this case, return a nullptr and don't overwrite previous frame data + if (av_frames.size() > 0) { + AVFramePtr frame = std::move(av_frames.front()); + av_frames.pop(); + return frame; + } + return AVFramePtr{nullptr, av_frame_deleter}; } NvdecCommon::VideoCodec Codec::GetCurrentCodec() const { -- cgit v1.2.3 From c9e3abe2060760d71c83a1574559b6e479e637d2 Mon Sep 17 00:00:00 2001 From: ameerj Date: Thu, 26 Nov 2020 00:18:26 -0500 Subject: Address PR feedback remove some redundant moves, make deleter match naming guidelines. Co-Authored-By: LC <712067+lioncash@users.noreply.github.com> --- src/video_core/command_classes/codecs/codec.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/video_core/command_classes/codecs/codec.cpp') diff --git a/src/video_core/command_classes/codecs/codec.cpp b/src/video_core/command_classes/codecs/codec.cpp index 1a19341c8..412e1e41c 100644 --- a/src/video_core/command_classes/codecs/codec.cpp +++ b/src/video_core/command_classes/codecs/codec.cpp @@ -18,7 +18,7 @@ extern "C" { namespace Tegra { -void av_frame_deleter(AVFrame* ptr) { +void AVFrameDeleter(AVFrame* ptr) { av_frame_unref(ptr); av_free(ptr); } @@ -101,7 +101,7 @@ void Codec::Decode() { if (!vp9_hidden_frame) { // Only receive/store visible frames - AVFramePtr frame = AVFramePtr{av_frame_alloc(), av_frame_deleter}; + AVFramePtr frame = AVFramePtr{av_frame_alloc(), AVFrameDeleter}; avcodec_receive_frame(av_codec_ctx, frame.get()); av_frames.push(std::move(frame)); } @@ -110,12 +110,13 @@ void Codec::Decode() { AVFramePtr Codec::GetCurrentFrame() { // Sometimes VIC will request more frames than have been decoded. // in this case, return a nullptr and don't overwrite previous frame data - if (av_frames.size() > 0) { - AVFramePtr frame = std::move(av_frames.front()); - av_frames.pop(); - return frame; + if (av_frames.empty()) { + return AVFramePtr{nullptr, AVFrameDeleter}; } - return AVFramePtr{nullptr, av_frame_deleter}; + + AVFramePtr frame = std::move(av_frames.front()); + av_frames.pop(); + return frame; } NvdecCommon::VideoCodec Codec::GetCurrentCodec() const { -- cgit v1.2.3 From 979b60273889f070737d1fe3037991245180ca67 Mon Sep 17 00:00:00 2001 From: ameerj Date: Thu, 26 Nov 2020 14:04:06 -0500 Subject: Limit queue size to 10 frames Workaround for ZLA, which seems to decode and queue twice as many frames as it displays. --- src/video_core/command_classes/codecs/codec.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/video_core/command_classes/codecs/codec.cpp') diff --git a/src/video_core/command_classes/codecs/codec.cpp b/src/video_core/command_classes/codecs/codec.cpp index 412e1e41c..9a88f64e4 100644 --- a/src/video_core/command_classes/codecs/codec.cpp +++ b/src/video_core/command_classes/codecs/codec.cpp @@ -104,6 +104,10 @@ void Codec::Decode() { AVFramePtr frame = AVFramePtr{av_frame_alloc(), AVFrameDeleter}; avcodec_receive_frame(av_codec_ctx, frame.get()); av_frames.push(std::move(frame)); + // Limit queue to 10 frames. Workaround for ZLA decode and queue spam + if (av_frames.size() > 10) { + av_frames.pop(); + } } } -- cgit v1.2.3