From f33a58721839d76c9c37be8e1a22b2db3198d9d1 Mon Sep 17 00:00:00 2001 From: loki Date: Fri, 4 Jun 2021 21:35:00 +0200 Subject: [PATCH] Fix software encoder --- CMakeLists.txt | 1 - sunshine/video.cpp | 41 ++++++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c316e49..46793ca5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,7 +111,6 @@ else() set(GRAPHICS_LIBRARIES gbm dl - EGL va va-drm) diff --git a/sunshine/video.cpp b/sunshine/video.cpp index e9f57fb7..ec9c9c3e 100644 --- a/sunshine/video.cpp +++ b/sunshine/video.cpp @@ -99,8 +99,14 @@ public: this->frame = frame; // If it's a hwframe, allocate buffers for hardware - if(frame->hw_frames_ctx && av_hwframe_get_buffer(frame->hw_frames_ctx, frame, 0)) { - return -1; + if(frame->hw_frames_ctx) { + hw_frame.reset(frame); + + if(av_hwframe_get_buffer(frame->hw_frames_ctx, frame, 0)) return -1; + } + + if(!frame->hw_frames_ctx) { + sw_frame.reset(frame); } return 0; @@ -117,13 +123,15 @@ public: * When preserving aspect ratio, ensure that padding is black */ int prefill() { - auto width = sw_frame->width; - auto height = sw_frame->height; + auto frame = sw_frame ? sw_frame.get() : this->frame; + auto width = frame->width; + auto height = frame->height; + av_frame_get_buffer(frame, 0); sws_t sws { sws_getContext( width, height, AV_PIX_FMT_BGR0, - width, height, (AVPixelFormat)sw_frame->format, + width, height, (AVPixelFormat)frame->format, SWS_LANCZOS | SWS_ACCURATE_RND, nullptr, nullptr, nullptr) }; @@ -139,10 +147,10 @@ public: width, 0 }; - av_frame_make_writable(sw_frame.get()); + av_frame_make_writable(frame); auto data = img.begin(); - int ret = sws_scale(sws.get(), (std::uint8_t *const *)&data, linesizes, 0, height, sw_frame->data, sw_frame->linesize); + int ret = sws_scale(sws.get(), (std::uint8_t *const *)&data, linesizes, 0, height, frame->data, frame->linesize); if(ret <= 0) { BOOST_LOG(error) << "Couldn't convert image to required format and/or size"sv; @@ -153,20 +161,16 @@ public: } int init(int in_width, int in_height, AVFrame *frame, AVPixelFormat format) { - sw_frame.reset(av_frame_alloc()); - // If the device used is hardware, yet the image resides on main memory if(frame->hw_frames_ctx) { + sw_frame.reset(av_frame_alloc()); + sw_frame->width = frame->width; sw_frame->height = frame->height; sw_frame->format = format; - - av_frame_get_buffer(sw_frame.get(), 0); } else { - av_frame_get_buffer(frame, 0); - - av_frame_ref(sw_frame.get(), frame); + this->frame = frame; } if(prefill()) { @@ -195,11 +199,10 @@ public: return sws ? 0 : -1; } - ~swdevice_t() override { - if(frame) { - av_frame_unref(frame); - } - } + ~swdevice_t() override {} + + // Store ownsership when frame is hw_frame + frame_t hw_frame; frame_t sw_frame; sws_t sws;