2019-02-13 03:58:36 +00:00
|
|
|
#include "sdlvid.h"
|
2018-07-13 09:28:10 +00:00
|
|
|
|
2019-02-13 05:55:15 +00:00
|
|
|
#include "streaming/session.h"
|
2020-04-25 02:37:15 +00:00
|
|
|
#include "streaming/streamutils.h"
|
2019-02-13 05:55:15 +00:00
|
|
|
|
2018-07-27 02:26:22 +00:00
|
|
|
#include <Limelight.h>
|
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
SdlRenderer::SdlRenderer()
|
2022-01-02 04:15:43 +00:00
|
|
|
: m_VideoFormat(0),
|
|
|
|
m_Renderer(nullptr),
|
2019-04-10 04:26:47 +00:00
|
|
|
m_Texture(nullptr),
|
2022-01-02 19:41:36 +00:00
|
|
|
m_SwPixelFormat(AV_PIX_FMT_NONE),
|
|
|
|
m_MapFrame(false)
|
2018-07-13 09:28:10 +00:00
|
|
|
{
|
2019-03-17 22:08:21 +00:00
|
|
|
SDL_zero(m_OverlayTextures);
|
2021-12-14 03:41:00 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CUDA
|
|
|
|
m_CudaGLHelper = nullptr;
|
|
|
|
#endif
|
2018-07-13 09:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SdlRenderer::~SdlRenderer()
|
|
|
|
{
|
2021-12-14 03:41:00 +00:00
|
|
|
#ifdef HAVE_CUDA
|
|
|
|
if (m_CudaGLHelper != nullptr) {
|
|
|
|
delete m_CudaGLHelper;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-03-17 22:08:21 +00:00
|
|
|
for (int i = 0; i < Overlay::OverlayMax; i++) {
|
|
|
|
if (m_OverlayTextures[i] != nullptr) {
|
|
|
|
SDL_DestroyTexture(m_OverlayTextures[i]);
|
|
|
|
}
|
2019-02-13 05:55:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
if (m_Texture != nullptr) {
|
|
|
|
SDL_DestroyTexture(m_Texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Renderer != nullptr) {
|
|
|
|
SDL_DestroyRenderer(m_Renderer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 01:47:26 +00:00
|
|
|
bool SdlRenderer::prepareDecoderContext(AVCodecContext*, AVDictionary**)
|
2018-07-13 09:28:10 +00:00
|
|
|
{
|
|
|
|
/* Nothing to do */
|
2018-07-28 23:06:26 +00:00
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2019-04-10 04:46:14 +00:00
|
|
|
"Using SDL renderer");
|
2018-07-28 23:06:26 +00:00
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-10 04:46:14 +00:00
|
|
|
bool SdlRenderer::isRenderThreadSupported()
|
|
|
|
{
|
|
|
|
SDL_RendererInfo info;
|
|
|
|
SDL_GetRendererInfo(m_Renderer, &info);
|
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL renderer backend: %s",
|
|
|
|
info.name);
|
|
|
|
|
|
|
|
if (info.name != QString("direct3d")) {
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL renderer backend requires main thread rendering");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-26 23:02:29 +00:00
|
|
|
bool SdlRenderer::isPixelFormatSupported(int, AVPixelFormat pixelFormat)
|
|
|
|
{
|
|
|
|
// Remember to keep this in sync with SdlRenderer::renderFrame()!
|
|
|
|
switch (pixelFormat)
|
|
|
|
{
|
|
|
|
case AV_PIX_FMT_YUV420P:
|
|
|
|
case AV_PIX_FMT_NV12:
|
|
|
|
case AV_PIX_FMT_NV21:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 05:27:20 +00:00
|
|
|
bool SdlRenderer::initialize(PDECODER_PARAMETERS params)
|
2018-07-13 09:28:10 +00:00
|
|
|
{
|
2018-08-21 01:19:42 +00:00
|
|
|
Uint32 rendererFlags = SDL_RENDERER_ACCELERATED;
|
|
|
|
|
2022-01-02 04:15:43 +00:00
|
|
|
m_VideoFormat = params->videoFormat;
|
|
|
|
|
2019-11-06 01:29:21 +00:00
|
|
|
if (params->videoFormat == VIDEO_FORMAT_H265_MAIN10) {
|
|
|
|
// SDL doesn't support rendering YUV 10-bit textures yet
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-12 05:27:20 +00:00
|
|
|
if ((SDL_GetWindowFlags(params->window) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN) {
|
2018-08-21 01:19:42 +00:00
|
|
|
// In full-screen exclusive mode, we enable V-sync if requested. For other modes, Windows and Mac
|
|
|
|
// have compositors that make rendering tear-free. Linux compositor varies by distro and user
|
|
|
|
// configuration but doesn't seem feasible to detect here.
|
2019-04-12 05:27:20 +00:00
|
|
|
if (params->enableVsync) {
|
2018-08-21 01:19:42 +00:00
|
|
|
rendererFlags |= SDL_RENDERER_PRESENTVSYNC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 03:44:50 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// We render on a different thread than the main thread which is handling window
|
|
|
|
// messages. Without D3DCREATE_MULTITHREADED, this can cause a deadlock by blocking
|
|
|
|
// on a window message being processed while the main thread is blocked waiting for
|
|
|
|
// the render thread to finish.
|
|
|
|
SDL_SetHintWithPriority(SDL_HINT_RENDER_DIRECT3D_THREADSAFE, "1", SDL_HINT_OVERRIDE);
|
|
|
|
#endif
|
|
|
|
|
2019-04-12 05:27:20 +00:00
|
|
|
m_Renderer = SDL_CreateRenderer(params->window, -1, rendererFlags);
|
2018-07-13 09:28:10 +00:00
|
|
|
if (!m_Renderer) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_CreateRenderer() failed: %s",
|
|
|
|
SDL_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-13 01:41:36 +00:00
|
|
|
// SDL_CreateRenderer() can end up having to recreate our window (SDL_RecreateWindow())
|
|
|
|
// to ensure it's compatible with the renderer's OpenGL context. If that happens, we
|
|
|
|
// can get spurious SDL_WINDOWEVENT events that will cause us to (again) recreate our
|
|
|
|
// renderer. This can lead to an infinite to renderer recreation, so discard all
|
|
|
|
// SDL_WINDOWEVENT events after SDL_CreateRenderer().
|
2021-01-09 23:51:25 +00:00
|
|
|
Session* session = Session::get();
|
|
|
|
if (session != nullptr) {
|
|
|
|
// If we get here during a session, we need to synchronize with the event loop
|
|
|
|
// to ensure we don't drop any important events.
|
|
|
|
session->flushWindowEvents();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If we get here prior to the start of a session, just pump and flush ourselves.
|
|
|
|
SDL_PumpEvents();
|
|
|
|
SDL_FlushEvent(SDL_WINDOWEVENT);
|
|
|
|
}
|
2020-05-13 01:41:36 +00:00
|
|
|
|
2020-05-05 00:48:32 +00:00
|
|
|
// Calculate the video region size, scaling to fill the output size while
|
2020-04-25 02:37:15 +00:00
|
|
|
// preserving the aspect ratio of the video stream.
|
|
|
|
SDL_Rect src, dst;
|
|
|
|
src.x = src.y = 0;
|
|
|
|
src.w = params->width;
|
|
|
|
src.h = params->height;
|
|
|
|
dst.x = dst.y = 0;
|
2020-05-05 00:48:32 +00:00
|
|
|
SDL_GetRendererOutputSize(m_Renderer, &dst.w, &dst.h);
|
2020-04-25 02:37:15 +00:00
|
|
|
StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
|
|
|
|
|
|
|
|
// Ensure the viewport is set to the desired video region
|
|
|
|
SDL_RenderSetViewport(m_Renderer, &dst);
|
2018-07-16 09:07:32 +00:00
|
|
|
|
2018-07-19 05:07:43 +00:00
|
|
|
// Draw a black frame until the video stream starts rendering
|
|
|
|
SDL_SetRenderDrawColor(m_Renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
|
|
|
|
SDL_RenderClear(m_Renderer);
|
|
|
|
SDL_RenderPresent(m_Renderer);
|
|
|
|
|
2019-03-17 06:50:22 +00:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
// For some reason, using Direct3D9Ex breaks this with multi-monitor setups.
|
|
|
|
// When focus is lost, the window is minimized then immediately restored without
|
|
|
|
// input focus. This glitches out the renderer and a bunch of other stuff.
|
|
|
|
// Direct3D9Ex itself seems to have this minimize on focus loss behavior on its
|
|
|
|
// own, so just disable SDL's handling of the focus loss event.
|
2019-03-17 07:18:15 +00:00
|
|
|
SDL_SetHintWithPriority(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0", SDL_HINT_OVERRIDE);
|
2019-03-17 06:50:22 +00:00
|
|
|
#endif
|
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-17 22:08:21 +00:00
|
|
|
void SdlRenderer::renderOverlay(Overlay::OverlayType type)
|
|
|
|
{
|
|
|
|
if (Session::get()->getOverlayManager().isOverlayEnabled(type)) {
|
|
|
|
// If a new surface has been created for updated overlay data, convert it into a texture.
|
|
|
|
// NB: We have to do this conversion at render-time because we can only interact
|
|
|
|
// with the renderer on a single thread.
|
2020-12-13 21:51:02 +00:00
|
|
|
SDL_Surface* newSurface = Session::get()->getOverlayManager().getUpdatedOverlaySurface(type);
|
|
|
|
if (newSurface != nullptr) {
|
2019-03-17 22:08:21 +00:00
|
|
|
if (m_OverlayTextures[type] != nullptr) {
|
|
|
|
SDL_DestroyTexture(m_OverlayTextures[type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == Overlay::OverlayStatusUpdate) {
|
2019-03-24 04:14:21 +00:00
|
|
|
// Bottom Left
|
2020-05-08 01:56:53 +00:00
|
|
|
SDL_Rect viewportRect;
|
|
|
|
SDL_RenderGetViewport(m_Renderer, &viewportRect);
|
2019-03-24 04:14:21 +00:00
|
|
|
m_OverlayRects[type].x = 0;
|
2020-12-13 21:51:02 +00:00
|
|
|
m_OverlayRects[type].y = viewportRect.h - newSurface->h;
|
2019-03-17 22:08:21 +00:00
|
|
|
}
|
|
|
|
else if (type == Overlay::OverlayDebug) {
|
|
|
|
// Top left
|
|
|
|
m_OverlayRects[type].x = 0;
|
|
|
|
m_OverlayRects[type].y = 0;
|
|
|
|
}
|
|
|
|
|
2020-12-13 21:51:02 +00:00
|
|
|
m_OverlayRects[type].w = newSurface->w;
|
|
|
|
m_OverlayRects[type].h = newSurface->h;
|
2019-03-17 22:08:21 +00:00
|
|
|
|
2020-12-13 21:51:02 +00:00
|
|
|
m_OverlayTextures[type] = SDL_CreateTextureFromSurface(m_Renderer, newSurface);
|
|
|
|
SDL_FreeSurface(newSurface);
|
2019-03-17 22:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have an overlay texture, render it too
|
|
|
|
if (m_OverlayTextures[type] != nullptr) {
|
|
|
|
SDL_RenderCopy(m_Renderer, m_OverlayTextures[type], nullptr, &m_OverlayRects[type]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:41:36 +00:00
|
|
|
bool SdlRenderer::initializeReadBackFormat(AVBufferRef* hwFrameCtxRef, AVFrame* testFrame)
|
2022-01-02 04:15:43 +00:00
|
|
|
{
|
|
|
|
auto hwFrameCtx = (AVHWFramesContext*)hwFrameCtxRef->data;
|
|
|
|
int err;
|
|
|
|
enum AVPixelFormat *formats;
|
2022-01-02 19:41:36 +00:00
|
|
|
AVFrame* outputFrame;
|
|
|
|
|
|
|
|
// This function must only be called once per instance
|
|
|
|
SDL_assert(m_SwPixelFormat == AV_PIX_FMT_NONE);
|
|
|
|
SDL_assert(!m_MapFrame);
|
|
|
|
|
|
|
|
// Try direct mapping before resorting to copying the frame
|
|
|
|
outputFrame = av_frame_alloc();
|
|
|
|
if (outputFrame != nullptr) {
|
|
|
|
err = av_hwframe_map(outputFrame, testFrame, AV_HWFRAME_MAP_READ);
|
|
|
|
if (err == 0) {
|
|
|
|
if (isPixelFormatSupported(m_VideoFormat, (AVPixelFormat)outputFrame->format)) {
|
|
|
|
m_SwPixelFormat = (AVPixelFormat)outputFrame->format;
|
|
|
|
m_MapFrame = true;
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Skipping unsupported hwframe mapping format: %d",
|
|
|
|
outputFrame->format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-02 19:58:37 +00:00
|
|
|
"av_hwframe_map() is unsupported (error: %d)",
|
|
|
|
err);
|
|
|
|
SDL_assert(err == AVERROR(ENOSYS));
|
2022-01-02 19:41:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-02 04:15:43 +00:00
|
|
|
|
2022-01-02 19:41:36 +00:00
|
|
|
// Direct mapping didn't work, so let's see what transfer formats we have
|
2022-01-02 04:15:43 +00:00
|
|
|
err = av_hwframe_transfer_get_formats(hwFrameCtxRef, AV_HWFRAME_TRANSFER_DIRECTION_FROM, &formats, 0);
|
|
|
|
if (err < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"av_hwframe_transfer_get_formats() failed: %d",
|
|
|
|
err);
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: In this algorithm, we prefer to get a preferred hardware readback format
|
|
|
|
// and non-preferred rendering format rather than the other way around. This is
|
|
|
|
// why we loop through the readback format list in order, rather than searching
|
|
|
|
// for the format from getPreferredPixelFormat() in the list.
|
|
|
|
for (int i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
|
|
|
|
SDL_assert(m_VideoFormat != 0);
|
|
|
|
if (!isPixelFormatSupported(m_VideoFormat, formats[i])) {
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Skipping unsupported hwframe transfer format %d",
|
|
|
|
formats[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:41:36 +00:00
|
|
|
m_SwPixelFormat = formats[i];
|
2022-01-02 04:15:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&formats);
|
|
|
|
|
|
|
|
Exit:
|
2022-01-02 19:41:36 +00:00
|
|
|
av_frame_free(&outputFrame);
|
|
|
|
|
2022-01-02 04:15:43 +00:00
|
|
|
// If we didn't find any supported formats, try hwFrameCtx->sw_format.
|
2022-01-02 19:41:36 +00:00
|
|
|
if (m_SwPixelFormat == AV_PIX_FMT_NONE) {
|
2022-01-02 04:15:43 +00:00
|
|
|
if (isPixelFormatSupported(m_VideoFormat, hwFrameCtx->sw_format)) {
|
2022-01-02 19:41:36 +00:00
|
|
|
m_SwPixelFormat = hwFrameCtx->sw_format;
|
2022-01-02 04:15:43 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-02 19:58:37 +00:00
|
|
|
"Unable to find compatible hwframe transfer format (sw_format = %d)",
|
|
|
|
hwFrameCtx->sw_format);
|
2022-01-02 19:41:36 +00:00
|
|
|
return false;
|
2022-01-02 04:15:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-02 19:41:36 +00:00
|
|
|
"Selected hwframe->swframe format: %d (mapping: %s)",
|
|
|
|
m_SwPixelFormat,
|
|
|
|
m_MapFrame ? "yes" : "no");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFrame* SdlRenderer::getSwFrameFromHwFrame(AVFrame* hwFrame)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
SDL_assert(m_SwPixelFormat != AV_PIX_FMT_NONE);
|
|
|
|
|
|
|
|
AVFrame* swFrame = av_frame_alloc();
|
|
|
|
if (swFrame == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
swFrame->format = m_SwPixelFormat;
|
|
|
|
|
|
|
|
if (m_MapFrame) {
|
2022-01-02 19:58:37 +00:00
|
|
|
// We don't use AV_HWFRAME_MAP_DIRECT here because it can cause huge
|
|
|
|
// performance penalties on Intel hardware with VAAPI due to mappings
|
|
|
|
// being uncached memory.
|
2022-01-02 19:41:36 +00:00
|
|
|
err = av_hwframe_map(swFrame, hwFrame, AV_HWFRAME_MAP_READ);
|
|
|
|
if (err < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"av_hwframe_map() failed: %d",
|
|
|
|
err);
|
|
|
|
av_frame_free(&swFrame);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = av_hwframe_transfer_data(swFrame, hwFrame, 0);
|
|
|
|
if (err < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"av_hwframe_transfer_data() failed: %d",
|
|
|
|
err);
|
|
|
|
av_frame_free(&swFrame);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// av_hwframe_transfer_data() can nuke frame metadata,
|
|
|
|
// so anything other than width, height, and format must
|
|
|
|
// be set *after* calling av_hwframe_transfer_data().
|
|
|
|
swFrame->colorspace = hwFrame->colorspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
return swFrame;
|
2022-01-02 04:15:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 03:31:01 +00:00
|
|
|
void SdlRenderer::renderFrame(AVFrame* frame)
|
2018-07-13 09:28:10 +00:00
|
|
|
{
|
2019-04-12 05:27:20 +00:00
|
|
|
int err;
|
|
|
|
AVFrame* swFrame = nullptr;
|
|
|
|
|
2020-05-14 01:55:21 +00:00
|
|
|
if (frame == nullptr) {
|
|
|
|
// End of stream - nothing to do for us
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-07 00:22:39 +00:00
|
|
|
if (frame->hw_frames_ctx != nullptr && frame->format != AV_PIX_FMT_CUDA) {
|
2022-01-02 04:15:43 +00:00
|
|
|
#ifdef HAVE_CUDA
|
2021-12-14 03:41:00 +00:00
|
|
|
ReadbackRetry:
|
2022-01-02 04:15:43 +00:00
|
|
|
#endif
|
2019-04-12 05:27:20 +00:00
|
|
|
// If we are acting as the frontend for a hardware
|
|
|
|
// accelerated decoder, we'll need to read the frame
|
|
|
|
// back to render it.
|
|
|
|
|
2019-06-27 04:25:41 +00:00
|
|
|
// Find the native read-back format
|
|
|
|
if (m_SwPixelFormat == AV_PIX_FMT_NONE) {
|
2022-01-02 19:41:36 +00:00
|
|
|
initializeReadBackFormat(frame->hw_frames_ctx, frame);
|
2019-06-27 04:25:41 +00:00
|
|
|
|
2022-01-02 04:15:43 +00:00
|
|
|
// If we don't support any of the hw transfer formats, we should
|
|
|
|
// have failed inside testRenderFrame() and not made it here.
|
2019-06-27 04:25:41 +00:00
|
|
|
SDL_assert(m_SwPixelFormat != AV_PIX_FMT_NONE);
|
|
|
|
}
|
|
|
|
|
2022-01-02 19:41:36 +00:00
|
|
|
// Map or copy this hwframe to a swframe that we can work with
|
|
|
|
frame = swFrame = getSwFrameFromHwFrame(frame);
|
2019-04-12 05:27:20 +00:00
|
|
|
if (swFrame == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 05:16:06 +00:00
|
|
|
if (m_Texture == nullptr) {
|
|
|
|
Uint32 sdlFormat;
|
|
|
|
|
2020-01-26 23:02:29 +00:00
|
|
|
// Remember to keep this in sync with SdlRenderer::isPixelFormatSupported()!
|
2019-04-20 05:16:06 +00:00
|
|
|
switch (frame->format)
|
|
|
|
{
|
|
|
|
case AV_PIX_FMT_YUV420P:
|
|
|
|
sdlFormat = SDL_PIXELFORMAT_YV12;
|
|
|
|
break;
|
2021-12-07 00:22:39 +00:00
|
|
|
case AV_PIX_FMT_CUDA:
|
2019-04-20 05:16:06 +00:00
|
|
|
case AV_PIX_FMT_NV12:
|
|
|
|
sdlFormat = SDL_PIXELFORMAT_NV12;
|
|
|
|
break;
|
2019-05-03 05:51:28 +00:00
|
|
|
case AV_PIX_FMT_NV21:
|
|
|
|
sdlFormat = SDL_PIXELFORMAT_NV21;
|
|
|
|
break;
|
2019-04-20 05:16:06 +00:00
|
|
|
default:
|
|
|
|
SDL_assert(false);
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
2019-12-14 22:40:02 +00:00
|
|
|
switch (frame->colorspace)
|
|
|
|
{
|
|
|
|
case AVCOL_SPC_BT709:
|
|
|
|
SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_BT709);
|
|
|
|
break;
|
|
|
|
case AVCOL_SPC_BT470BG:
|
|
|
|
case AVCOL_SPC_SMPTE170M:
|
|
|
|
default:
|
|
|
|
SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_BT601);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-04-20 05:16:06 +00:00
|
|
|
m_Texture = SDL_CreateTexture(m_Renderer,
|
|
|
|
sdlFormat,
|
|
|
|
SDL_TEXTUREACCESS_STREAMING,
|
|
|
|
frame->width,
|
|
|
|
frame->height);
|
|
|
|
if (!m_Texture) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_CreateTexture() failed: %s",
|
|
|
|
SDL_GetError());
|
|
|
|
goto Exit;
|
|
|
|
}
|
2021-12-14 03:41:00 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CUDA
|
|
|
|
if (frame->format == AV_PIX_FMT_CUDA) {
|
|
|
|
SDL_assert(m_CudaGLHelper == nullptr);
|
|
|
|
m_CudaGLHelper = new CUDAGLInteropHelper(((AVHWFramesContext*)frame->hw_frames_ctx->data)->device_ctx);
|
|
|
|
|
|
|
|
SDL_GL_BindTexture(m_Texture, nullptr, nullptr);
|
|
|
|
if (!m_CudaGLHelper->registerBoundTextures()) {
|
|
|
|
// If we can't register textures, fall back to normal read-back rendering
|
|
|
|
delete m_CudaGLHelper;
|
|
|
|
m_CudaGLHelper = nullptr;
|
|
|
|
}
|
|
|
|
SDL_GL_UnbindTexture(m_Texture);
|
|
|
|
}
|
|
|
|
#endif
|
2019-04-20 05:16:06 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 00:22:39 +00:00
|
|
|
if (frame->format == AV_PIX_FMT_CUDA) {
|
|
|
|
#ifdef HAVE_CUDA
|
2021-12-14 03:41:00 +00:00
|
|
|
if (m_CudaGLHelper == nullptr || !m_CudaGLHelper->copyCudaFrameToTextures(frame)) {
|
|
|
|
goto ReadbackRetry;
|
|
|
|
}
|
2021-12-07 00:22:39 +00:00
|
|
|
#else
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Got CUDA frame, but not built with CUDA support!");
|
|
|
|
goto Exit;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (frame->format == AV_PIX_FMT_YUV420P) {
|
2019-04-20 05:16:06 +00:00
|
|
|
SDL_UpdateYUVTexture(m_Texture, nullptr,
|
|
|
|
frame->data[0],
|
|
|
|
frame->linesize[0],
|
|
|
|
frame->data[1],
|
|
|
|
frame->linesize[1],
|
|
|
|
frame->data[2],
|
|
|
|
frame->linesize[2]);
|
|
|
|
}
|
|
|
|
else {
|
2021-01-09 00:05:27 +00:00
|
|
|
#if SDL_VERSION_ATLEAST(2, 0, 15)
|
|
|
|
// SDL_UpdateNVTexture is not supported on all renderer backends,
|
|
|
|
// (notably not DX9), so we must have a fallback in case it's not
|
|
|
|
// supported and for earlier versions of SDL.
|
|
|
|
if (SDL_UpdateNVTexture(m_Texture,
|
|
|
|
nullptr,
|
|
|
|
frame->data[0],
|
|
|
|
frame->linesize[0],
|
|
|
|
frame->data[1],
|
|
|
|
frame->linesize[1]) != 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
char* pixels;
|
2022-01-02 20:35:14 +00:00
|
|
|
int texturePitch;
|
2021-01-09 00:05:27 +00:00
|
|
|
|
2022-01-02 20:35:14 +00:00
|
|
|
err = SDL_LockTexture(m_Texture, nullptr, (void**)&pixels, &texturePitch);
|
2021-01-09 00:05:27 +00:00
|
|
|
if (err < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_LockTexture() failed: %s",
|
|
|
|
SDL_GetError());
|
|
|
|
goto Exit;
|
|
|
|
}
|
2019-04-20 05:16:06 +00:00
|
|
|
|
2022-01-02 20:35:14 +00:00
|
|
|
// If the planar pitches match, we can use a single memcpy() to transfer
|
|
|
|
// the data. If not, we'll need to do separate memcpy() calls for each
|
|
|
|
// line to ensure the pitch doesn't get screwed up.
|
|
|
|
|
|
|
|
if (frame->linesize[0] == texturePitch) {
|
|
|
|
memcpy(pixels,
|
|
|
|
frame->data[0],
|
|
|
|
frame->linesize[0] * frame->height);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int pitch = SDL_min(frame->linesize[0], texturePitch);
|
|
|
|
for (int i = 0; i < frame->height; i++) {
|
|
|
|
memcpy(pixels + (texturePitch * i),
|
|
|
|
frame->data[0] + (frame->linesize[0] * i),
|
|
|
|
pitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame->linesize[1] == texturePitch) {
|
|
|
|
memcpy(pixels + (texturePitch * frame->height),
|
|
|
|
frame->data[1],
|
|
|
|
frame->linesize[1] * frame->height / 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int pitch = SDL_min(frame->linesize[1], texturePitch);
|
|
|
|
for (int i = 0; i < frame->height / 2; i++) {
|
|
|
|
memcpy(pixels + (texturePitch * (frame->height + i)),
|
|
|
|
frame->data[1] + (frame->linesize[1] * i),
|
|
|
|
pitch);
|
|
|
|
}
|
|
|
|
}
|
2019-04-20 05:16:06 +00:00
|
|
|
|
2021-01-09 00:05:27 +00:00
|
|
|
SDL_UnlockTexture(m_Texture);
|
|
|
|
}
|
2019-04-20 05:16:06 +00:00
|
|
|
}
|
2018-07-18 07:10:22 +00:00
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
SDL_RenderClear(m_Renderer);
|
2019-02-13 05:55:15 +00:00
|
|
|
|
|
|
|
// Draw the video content itself
|
2018-07-13 09:28:10 +00:00
|
|
|
SDL_RenderCopy(m_Renderer, m_Texture, nullptr, nullptr);
|
2019-02-13 05:55:15 +00:00
|
|
|
|
|
|
|
// Draw the overlays
|
2019-03-17 22:08:21 +00:00
|
|
|
for (int i = 0; i < Overlay::OverlayMax; i++) {
|
|
|
|
renderOverlay((Overlay::OverlayType)i);
|
2019-02-13 05:55:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 09:28:10 +00:00
|
|
|
SDL_RenderPresent(m_Renderer);
|
2019-04-12 05:27:20 +00:00
|
|
|
|
2019-04-20 05:16:06 +00:00
|
|
|
Exit:
|
2019-04-12 05:27:20 +00:00
|
|
|
if (swFrame != nullptr) {
|
|
|
|
av_frame_free(&swFrame);
|
|
|
|
}
|
2018-07-13 09:28:10 +00:00
|
|
|
}
|
2021-03-23 04:15:14 +00:00
|
|
|
|
|
|
|
bool SdlRenderer::testRenderFrame(AVFrame* frame)
|
|
|
|
{
|
|
|
|
// If we are acting as the frontend for a hardware
|
|
|
|
// accelerated decoder, we'll need to read the frame
|
|
|
|
// back to render it. Test that this can be done
|
|
|
|
// for the given frame successfully.
|
|
|
|
if (frame->hw_frames_ctx != nullptr) {
|
2022-01-02 19:41:36 +00:00
|
|
|
if (!initializeReadBackFormat(frame->hw_frames_ctx, frame)) {
|
2022-01-02 04:15:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-23 04:15:14 +00:00
|
|
|
|
2022-01-02 19:41:36 +00:00
|
|
|
AVFrame* swFrame = getSwFrameFromHwFrame(frame);
|
2021-03-23 04:15:14 +00:00
|
|
|
if (swFrame == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&swFrame);
|
|
|
|
}
|
2022-01-02 21:03:49 +00:00
|
|
|
else if (!isPixelFormatSupported(m_VideoFormat, (AVPixelFormat)frame->format)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Swframe pixel format unsupported: %d",
|
|
|
|
frame->format);
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-23 04:15:14 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|