From 243ba371b39b47736dd9b8f570a268e25ff6fa54 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 30 Jan 2021 16:33:58 -0600 Subject: [PATCH] Initialize a DRM hardware context for hwaccel usage --- app/streaming/video/ffmpeg-renderers/drm.cpp | 31 ++++++++++++++++++-- app/streaming/video/ffmpeg-renderers/drm.h | 1 + 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/streaming/video/ffmpeg-renderers/drm.cpp b/app/streaming/video/ffmpeg-renderers/drm.cpp index ca234ff4..ded1112d 100644 --- a/app/streaming/video/ffmpeg-renderers/drm.cpp +++ b/app/streaming/video/ffmpeg-renderers/drm.cpp @@ -15,7 +15,8 @@ extern "C" { #include DrmRenderer::DrmRenderer() - : m_DrmFd(-1), + : m_HwContext(nullptr), + m_DrmFd(-1), m_CrtcId(0), m_PlaneId(0), m_CurrentFbId(0) @@ -31,11 +32,15 @@ DrmRenderer::~DrmRenderer() if (m_DrmFd != -1) { close(m_DrmFd); } + + if (m_HwContext != nullptr) { + av_buffer_unref(&m_HwContext); + } } -bool DrmRenderer::prepareDecoderContext(AVCodecContext*, AVDictionary**) +bool DrmRenderer::prepareDecoderContext(AVCodecContext* context, AVDictionary**) { - /* Nothing to do */ + context->hw_device_ctx = av_buffer_ref(m_HwContext); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Using DRM renderer"); @@ -185,6 +190,26 @@ bool DrmRenderer::initialize(PDECODER_PARAMETERS) drmModeFreePlaneResources(planeRes); + m_HwContext = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM); + if (m_HwContext == nullptr) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "av_hwdevice_ctx_alloc(DRM) failed"); + return false; + } + + AVHWDeviceContext* deviceContext = (AVHWDeviceContext*)m_HwContext->data; + AVDRMDeviceContext* drmDeviceContext = (AVDRMDeviceContext*)deviceContext->hwctx; + + drmDeviceContext->fd = m_DrmFd; + + int err = av_hwdevice_ctx_init(m_HwContext); + if (err < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "av_hwdevice_ctx_init(DRM) failed: %d", + err); + return false; + } + return true; } diff --git a/app/streaming/video/ffmpeg-renderers/drm.h b/app/streaming/video/ffmpeg-renderers/drm.h index 7d88e223..5dbd91dd 100644 --- a/app/streaming/video/ffmpeg-renderers/drm.h +++ b/app/streaming/video/ffmpeg-renderers/drm.h @@ -16,6 +16,7 @@ public: virtual int getRendererAttributes() override; private: + AVBufferRef* m_HwContext; int m_DrmFd; uint32_t m_CrtcId; uint32_t m_PlaneId;