2018-07-16 01:11:57 +00:00
|
|
|
// Nasty hack to avoid conflict between AVFoundation and
|
|
|
|
// libavutil both defining AVMediaType
|
|
|
|
#define AVMediaType AVMediaType_FFmpeg
|
|
|
|
#include "vt.h"
|
2018-08-16 05:02:15 +00:00
|
|
|
#include "pacer/pacer.h"
|
2018-07-16 01:11:57 +00:00
|
|
|
#undef AVMediaType
|
|
|
|
|
|
|
|
#include <SDL_syswm.h>
|
|
|
|
#include <Limelight.h>
|
2024-02-07 08:40:35 +00:00
|
|
|
#include "streaming/session.h"
|
|
|
|
#include "streaming/streamutils.h"
|
|
|
|
#include "path.h"
|
2018-07-16 01:11:57 +00:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#import <VideoToolbox/VideoToolbox.h>
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
2019-02-14 04:55:52 +00:00
|
|
|
#import <dispatch/dispatch.h>
|
2019-11-06 02:36:43 +00:00
|
|
|
#import <Metal/Metal.h>
|
2024-02-07 08:40:35 +00:00
|
|
|
#import <MetalKit/MetalKit.h>
|
|
|
|
|
|
|
|
struct CscParams
|
|
|
|
{
|
|
|
|
vector_float3 matrix[3];
|
|
|
|
vector_float3 offsets;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const CscParams k_CscParams_Bt601Lim = {
|
|
|
|
// CSC Matrix
|
|
|
|
{
|
|
|
|
{ 1.1644f, 0.0f, 1.5960f },
|
|
|
|
{ 1.1644f, -0.3917f, -0.8129f },
|
|
|
|
{ 1.1644f, 2.0172f, 0.0f }
|
|
|
|
},
|
|
|
|
|
|
|
|
// Offsets
|
|
|
|
{ 16.0f / 255.0f, 128.0f / 255.0f, 128.0f / 255.0f },
|
|
|
|
};
|
|
|
|
static const CscParams k_CscParams_Bt601Full = {
|
|
|
|
// CSC Matrix
|
|
|
|
{
|
|
|
|
{ 1.0f, 0.0f, 1.4020f },
|
|
|
|
{ 1.0f, -0.3441f, -0.7141f },
|
|
|
|
{ 1.0f, 1.7720f, 0.0f },
|
|
|
|
},
|
|
|
|
|
|
|
|
// Offsets
|
|
|
|
{ 0.0f, 128.0f / 255.0f, 128.0f / 255.0f },
|
|
|
|
};
|
|
|
|
static const CscParams k_CscParams_Bt709Lim = {
|
|
|
|
// CSC Matrix
|
|
|
|
{
|
|
|
|
{ 1.1644f, 0.0f, 1.7927f },
|
|
|
|
{ 1.1644f, -0.2132f, -0.5329f },
|
|
|
|
{ 1.1644f, 2.1124f, 0.0f },
|
|
|
|
},
|
|
|
|
|
|
|
|
// Offsets
|
|
|
|
{ 16.0f / 255.0f, 128.0f / 255.0f, 128.0f / 255.0f },
|
|
|
|
};
|
|
|
|
static const CscParams k_CscParams_Bt709Full = {
|
|
|
|
// CSC Matrix
|
|
|
|
{
|
|
|
|
{ 1.0f, 0.0f, 1.5748f },
|
|
|
|
{ 1.0f, -0.1873f, -0.4681f },
|
|
|
|
{ 1.0f, 1.8556f, 0.0f },
|
|
|
|
},
|
|
|
|
|
|
|
|
// Offsets
|
|
|
|
{ 0.0f, 128.0f / 255.0f, 128.0f / 255.0f },
|
|
|
|
};
|
|
|
|
static const CscParams k_CscParams_Bt2020Lim = {
|
|
|
|
// CSC Matrix
|
|
|
|
{
|
|
|
|
{ 1.1644f, 0.0f, 1.6781f },
|
|
|
|
{ 1.1644f, -0.1874f, -0.6505f },
|
|
|
|
{ 1.1644f, 2.1418f, 0.0f },
|
|
|
|
},
|
|
|
|
|
|
|
|
// Offsets
|
|
|
|
{ 16.0f / 255.0f, 128.0f / 255.0f, 128.0f / 255.0f },
|
|
|
|
};
|
|
|
|
static const CscParams k_CscParams_Bt2020Full = {
|
|
|
|
// CSC Matrix
|
|
|
|
{
|
|
|
|
{ 1.0f, 0.0f, 1.4746f },
|
|
|
|
{ 1.0f, -0.1646f, -0.5714f },
|
|
|
|
{ 1.0f, 1.8814f, 0.0f },
|
|
|
|
},
|
|
|
|
|
|
|
|
// Offsets
|
|
|
|
{ 0.0f, 128.0f / 255.0f, 128.0f / 255.0f },
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Vertex
|
|
|
|
{
|
|
|
|
vector_float4 position;
|
|
|
|
vector_float2 texCoord;
|
|
|
|
};
|
2018-07-18 07:10:22 +00:00
|
|
|
|
2018-08-16 06:57:03 +00:00
|
|
|
class VTRenderer : public IFFmpegRenderer
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
VTRenderer()
|
2024-02-07 08:40:35 +00:00
|
|
|
: m_Window(nullptr),
|
|
|
|
m_HwContext(nullptr),
|
|
|
|
m_MetalLayer(nullptr),
|
|
|
|
m_TextureCache(nullptr),
|
|
|
|
m_CscParamsBuffer(nullptr),
|
|
|
|
m_VideoVertexBuffer(nullptr),
|
2024-02-11 06:11:23 +00:00
|
|
|
m_OverlayTextures{},
|
|
|
|
m_OverlayLock(0),
|
|
|
|
m_VideoPipelineState(nullptr),
|
|
|
|
m_OverlayPipelineState(nullptr),
|
2024-02-07 08:40:35 +00:00
|
|
|
m_ShaderLibrary(nullptr),
|
|
|
|
m_CommandQueue(nullptr),
|
2024-02-11 04:49:13 +00:00
|
|
|
m_NextDrawable(nullptr),
|
2024-02-07 09:26:09 +00:00
|
|
|
m_MetalView(nullptr),
|
2022-09-24 17:28:23 +00:00
|
|
|
m_LastColorSpace(-1),
|
2024-02-07 08:40:35 +00:00
|
|
|
m_LastFullRange(false),
|
2024-02-09 00:05:47 +00:00
|
|
|
m_LastFrameWidth(-1),
|
|
|
|
m_LastFrameHeight(-1),
|
|
|
|
m_LastDrawableWidth(-1),
|
|
|
|
m_LastDrawableHeight(-1),
|
2024-02-11 04:49:13 +00:00
|
|
|
m_PresentationMutex(SDL_CreateMutex()),
|
|
|
|
m_PresentationCond(SDL_CreateCond()),
|
|
|
|
m_PendingPresentationCount(0)
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:56:53 +00:00
|
|
|
virtual ~VTRenderer() override
|
2023-09-12 01:49:35 +00:00
|
|
|
{ @autoreleasepool {
|
2024-02-11 04:49:13 +00:00
|
|
|
if (m_PresentationCond != nullptr) {
|
|
|
|
SDL_DestroyCond(m_PresentationCond);
|
2019-05-12 01:33:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
if (m_PresentationMutex != nullptr) {
|
|
|
|
SDL_DestroyMutex(m_PresentationMutex);
|
2019-05-19 16:52:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
if (m_HwContext != nullptr) {
|
|
|
|
av_buffer_unref(&m_HwContext);
|
|
|
|
}
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
if (m_CscParamsBuffer != nullptr) {
|
|
|
|
[m_CscParamsBuffer release];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_VideoVertexBuffer != nullptr) {
|
|
|
|
[m_VideoVertexBuffer release];
|
|
|
|
}
|
|
|
|
|
2024-02-11 06:11:23 +00:00
|
|
|
if (m_VideoPipelineState != nullptr) {
|
|
|
|
[m_VideoPipelineState release];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < Overlay::OverlayMax; i++) {
|
|
|
|
if (m_OverlayTextures[i] != nullptr) {
|
|
|
|
[m_OverlayTextures[i] release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_OverlayPipelineState != nullptr) {
|
|
|
|
[m_OverlayPipelineState release];
|
2024-02-07 08:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ShaderLibrary != nullptr) {
|
|
|
|
[m_ShaderLibrary release];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_CommandQueue != nullptr) {
|
|
|
|
[m_CommandQueue release];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_TextureCache != nullptr) {
|
|
|
|
CFRelease(m_TextureCache);
|
2023-09-12 01:49:35 +00:00
|
|
|
}
|
2023-09-12 02:07:54 +00:00
|
|
|
|
2024-02-07 09:26:09 +00:00
|
|
|
if (m_MetalView != nullptr) {
|
|
|
|
SDL_Metal_DestroyView(m_MetalView);
|
|
|
|
}
|
2023-09-12 01:49:35 +00:00
|
|
|
}}
|
2018-07-18 07:10:22 +00:00
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
void discardNextDrawable()
|
|
|
|
{ @autoreleasepool {
|
|
|
|
if (!m_NextDrawable) {
|
|
|
|
return;
|
2019-05-12 01:33:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
[m_NextDrawable release];
|
|
|
|
m_NextDrawable = nullptr;
|
|
|
|
}}
|
2019-05-12 01:33:12 +00:00
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
virtual void waitToRender() override
|
|
|
|
{ @autoreleasepool {
|
|
|
|
if (!m_NextDrawable) {
|
|
|
|
// Wait for the next available drawable before latching the frame to render
|
|
|
|
m_NextDrawable = [[m_MetalLayer nextDrawable] retain];
|
|
|
|
if (m_NextDrawable == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-21 16:09:16 +00:00
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
// Pace ourselves by waiting if too many frames are pending presentation
|
|
|
|
SDL_LockMutex(m_PresentationMutex);
|
|
|
|
if (m_PendingPresentationCount > 2) {
|
|
|
|
if (SDL_CondWaitTimeout(m_PresentationCond, m_PresentationMutex, 100) == SDL_MUTEX_TIMEDOUT) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Presentation wait timed out after 100 ms");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SDL_UnlockMutex(m_PresentationMutex);
|
2019-05-12 01:33:12 +00:00
|
|
|
}
|
2024-02-11 04:49:13 +00:00
|
|
|
}}
|
2019-05-12 01:33:12 +00:00
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
virtual void cleanupRenderContext() override
|
2022-04-08 03:08:43 +00:00
|
|
|
{
|
2024-02-11 04:49:13 +00:00
|
|
|
// Free any unused drawable
|
|
|
|
discardNextDrawable();
|
2022-04-08 03:08:43 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
bool updateVideoRegionSizeForFrame(AVFrame* frame)
|
2023-02-03 01:16:18 +00:00
|
|
|
{
|
2024-02-07 08:40:35 +00:00
|
|
|
int drawableWidth, drawableHeight;
|
|
|
|
SDL_Metal_GetDrawableSize(m_Window, &drawableWidth, &drawableHeight);
|
2023-02-03 01:16:18 +00:00
|
|
|
|
2024-02-09 00:05:47 +00:00
|
|
|
// Check if anything has changed since the last vertex buffer upload
|
|
|
|
if (m_VideoVertexBuffer &&
|
|
|
|
frame->width == m_LastFrameWidth && frame->height == m_LastFrameHeight &&
|
|
|
|
drawableWidth == m_LastDrawableWidth && drawableHeight == m_LastDrawableHeight) {
|
|
|
|
// Nothing to do
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
// Determine the correct scaled size for the video region
|
|
|
|
SDL_Rect src, dst;
|
|
|
|
src.x = src.y = 0;
|
|
|
|
src.w = frame->width;
|
|
|
|
src.h = frame->height;
|
|
|
|
dst.x = dst.y = 0;
|
|
|
|
dst.w = drawableWidth;
|
|
|
|
dst.h = drawableHeight;
|
|
|
|
StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
|
2023-02-03 01:16:18 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
// Convert screen space to normalized device coordinates
|
|
|
|
SDL_FRect renderRect;
|
|
|
|
StreamUtils::screenSpaceToNormalizedDeviceCoords(&dst, &renderRect, drawableWidth, drawableHeight);
|
2018-07-18 07:10:22 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
Vertex verts[] =
|
|
|
|
{
|
|
|
|
{ { renderRect.x, renderRect.y, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
|
|
|
{ { renderRect.x, renderRect.y+renderRect.h, 0.0f, 1.0f }, { 0.0f, 0} },
|
|
|
|
{ { renderRect.x+renderRect.w, renderRect.y, 0.0f, 1.0f }, { 1.0f, 1.0f} },
|
|
|
|
{ { renderRect.x+renderRect.w, renderRect.y+renderRect.h, 0.0f, 1.0f }, { 1.0f, 0} },
|
|
|
|
};
|
2018-08-17 06:09:40 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
[m_VideoVertexBuffer release];
|
2024-02-09 00:06:46 +00:00
|
|
|
auto bufferOptions = MTLCPUCacheModeWriteCombined | MTLResourceStorageModeManaged;
|
|
|
|
m_VideoVertexBuffer = [m_MetalLayer.device newBufferWithBytes:verts length:sizeof(verts) options:bufferOptions];
|
2024-02-07 08:40:35 +00:00
|
|
|
if (!m_VideoVertexBuffer) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to create video vertex buffer");
|
|
|
|
return false;
|
2018-08-16 06:57:03 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 00:05:47 +00:00
|
|
|
m_LastFrameWidth = frame->width;
|
|
|
|
m_LastFrameHeight = frame->height;
|
|
|
|
m_LastDrawableWidth = drawableWidth;
|
|
|
|
m_LastDrawableHeight = drawableHeight;
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-03-11 05:21:09 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
bool updateColorSpaceForFrame(AVFrame* frame)
|
|
|
|
{
|
2022-09-24 17:28:23 +00:00
|
|
|
int colorspace = getFrameColorspace(frame);
|
2024-02-07 08:40:35 +00:00
|
|
|
bool fullRange = isFrameFullRange(frame);
|
|
|
|
if (colorspace != m_LastColorSpace || fullRange != m_LastFullRange) {
|
|
|
|
CGColorSpaceRef newColorSpace;
|
|
|
|
void* paramBuffer;
|
2021-11-19 06:28:15 +00:00
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
// Free any unpresented drawable since we're changing pixel formats
|
|
|
|
discardNextDrawable();
|
|
|
|
|
2022-09-24 17:28:23 +00:00
|
|
|
switch (colorspace) {
|
|
|
|
case COLORSPACE_REC_709:
|
2024-02-07 08:40:35 +00:00
|
|
|
m_MetalLayer.colorspace = newColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_709);
|
|
|
|
m_MetalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
|
|
|
|
paramBuffer = (void*)(fullRange ? &k_CscParams_Bt709Full : &k_CscParams_Bt709Lim);
|
2022-01-22 05:09:43 +00:00
|
|
|
break;
|
2022-09-24 17:28:23 +00:00
|
|
|
case COLORSPACE_REC_2020:
|
2024-02-07 08:40:35 +00:00
|
|
|
// https://developer.apple.com/documentation/metal/hdr_content/using_color_spaces_to_display_hdr_content
|
2023-10-15 20:30:41 +00:00
|
|
|
if (frame->color_trc == AVCOL_TRC_SMPTE2084) {
|
|
|
|
if (@available(macOS 11.0, *)) {
|
2024-02-07 08:40:35 +00:00
|
|
|
m_MetalLayer.colorspace = newColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2100_PQ);
|
2023-10-15 20:30:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-02-07 08:40:35 +00:00
|
|
|
m_MetalLayer.colorspace = newColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
|
2023-10-15 20:30:41 +00:00
|
|
|
}
|
2024-02-07 08:40:35 +00:00
|
|
|
m_MetalLayer.pixelFormat = MTLPixelFormatBGR10A2Unorm;
|
2023-10-15 20:30:41 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-02-07 08:40:35 +00:00
|
|
|
m_MetalLayer.colorspace = newColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
|
|
|
|
m_MetalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
|
2023-10-15 20:30:41 +00:00
|
|
|
}
|
2024-02-07 08:40:35 +00:00
|
|
|
paramBuffer = (void*)(fullRange ? &k_CscParams_Bt2020Full : &k_CscParams_Bt2020Lim);
|
2022-01-22 05:09:43 +00:00
|
|
|
break;
|
2024-02-07 08:40:35 +00:00
|
|
|
default:
|
2022-09-24 17:28:23 +00:00
|
|
|
case COLORSPACE_REC_601:
|
2024-02-07 08:40:35 +00:00
|
|
|
m_MetalLayer.colorspace = newColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
|
|
m_MetalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm;
|
|
|
|
paramBuffer = (void*)(fullRange ? &k_CscParams_Bt601Full : &k_CscParams_Bt601Lim);
|
2022-01-22 05:09:43 +00:00
|
|
|
break;
|
2020-03-11 05:21:09 +00:00
|
|
|
}
|
2022-01-22 05:09:43 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
// The CAMetalLayer retains the CGColorSpace
|
|
|
|
CGColorSpaceRelease(newColorSpace);
|
|
|
|
|
|
|
|
// Create the new colorspace parameter buffer for our fragment shader
|
|
|
|
[m_CscParamsBuffer release];
|
2024-02-09 00:06:46 +00:00
|
|
|
auto bufferOptions = MTLCPUCacheModeWriteCombined | MTLResourceStorageModeManaged;
|
|
|
|
m_CscParamsBuffer = [m_MetalLayer.device newBufferWithBytes:paramBuffer length:sizeof(CscParams) options:bufferOptions];
|
2024-02-07 08:40:35 +00:00
|
|
|
if (!m_CscParamsBuffer) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to create CSC parameters buffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MTLRenderPipelineDescriptor *pipelineDesc = [[MTLRenderPipelineDescriptor new] autorelease];
|
|
|
|
pipelineDesc.vertexFunction = [[m_ShaderLibrary newFunctionWithName:@"vs_draw"] autorelease];
|
|
|
|
pipelineDesc.fragmentFunction = [[m_ShaderLibrary newFunctionWithName:@"ps_draw_biplanar"] autorelease];
|
|
|
|
pipelineDesc.colorAttachments[0].pixelFormat = m_MetalLayer.pixelFormat;
|
2024-02-11 06:11:23 +00:00
|
|
|
[m_VideoPipelineState release];
|
|
|
|
m_VideoPipelineState = [m_MetalLayer.device newRenderPipelineStateWithDescriptor:pipelineDesc error:nullptr];
|
|
|
|
if (!m_VideoPipelineState) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to create video pipeline state");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipelineDesc = [[MTLRenderPipelineDescriptor new] autorelease];
|
|
|
|
pipelineDesc.vertexFunction = [[m_ShaderLibrary newFunctionWithName:@"vs_draw"] autorelease];
|
|
|
|
pipelineDesc.fragmentFunction = [[m_ShaderLibrary newFunctionWithName:@"ps_draw_rgb"] autorelease];
|
|
|
|
pipelineDesc.colorAttachments[0].pixelFormat = m_MetalLayer.pixelFormat;
|
|
|
|
pipelineDesc.colorAttachments[0].blendingEnabled = YES;
|
|
|
|
pipelineDesc.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
|
|
|
|
pipelineDesc.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
|
|
|
|
pipelineDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
|
|
|
|
pipelineDesc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
|
|
|
|
pipelineDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
|
|
|
|
pipelineDesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
|
|
|
|
[m_OverlayPipelineState release];
|
|
|
|
m_OverlayPipelineState = [m_MetalLayer.device newRenderPipelineStateWithDescriptor:pipelineDesc error:nullptr];
|
|
|
|
if (!m_VideoPipelineState) {
|
2024-02-07 08:40:35 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
2024-02-11 06:11:23 +00:00
|
|
|
"Failed to create overlay pipeline state");
|
2024-02-07 08:40:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-24 17:28:23 +00:00
|
|
|
m_LastColorSpace = colorspace;
|
2024-02-07 08:40:35 +00:00
|
|
|
m_LastFullRange = fullRange;
|
2020-03-11 05:21:09 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Caller frees frame after we return
|
|
|
|
virtual void renderFrame(AVFrame* frame) override
|
|
|
|
{ @autoreleasepool {
|
|
|
|
CVPixelBufferRef pixBuf = reinterpret_cast<CVPixelBufferRef>(frame->data[3]);
|
|
|
|
|
|
|
|
if (m_MetalLayer.preferredDevice != nullptr && m_MetalLayer.preferredDevice != m_MetalLayer.device) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Resetting renderer after preferred device changed");
|
|
|
|
|
|
|
|
// Trigger the main thread to recreate the decoder
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = SDL_RENDER_DEVICE_RESET;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
return;
|
2020-03-11 05:21:09 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
// Handle changes to the frame's colorspace from last time we rendered
|
|
|
|
if (!updateColorSpaceForFrame(frame)) {
|
|
|
|
// Trigger the main thread to recreate the decoder
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = SDL_RENDER_DEVICE_RESET;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
return;
|
2023-02-03 01:16:18 +00:00
|
|
|
}
|
2024-02-07 08:40:35 +00:00
|
|
|
|
|
|
|
// Handle changes to the video size or drawable size
|
|
|
|
if (!updateVideoRegionSizeForFrame(frame)) {
|
|
|
|
// Trigger the main thread to recreate the decoder
|
|
|
|
SDL_Event event;
|
|
|
|
event.type = SDL_RENDER_DEVICE_RESET;
|
|
|
|
SDL_PushEvent(&event);
|
|
|
|
return;
|
2023-02-03 01:16:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
// Don't proceed with rendering if we don't have a drawable
|
|
|
|
if (m_NextDrawable == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2024-02-07 08:40:35 +00:00
|
|
|
|
|
|
|
// Create Metal textures for the planes of the CVPixelBuffer
|
|
|
|
std::array<CVMetalTextureRef, 2> textures;
|
|
|
|
for (size_t i = 0; i < textures.size(); i++) {
|
|
|
|
MTLPixelFormat fmt;
|
|
|
|
|
|
|
|
switch (CVPixelBufferGetPixelFormatType(pixBuf)) {
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
|
|
|
|
fmt = (i == 0) ? MTLPixelFormatR8Unorm : MTLPixelFormatRG8Unorm;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange:
|
|
|
|
case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange:
|
|
|
|
fmt = (i == 0) ? MTLPixelFormatR16Unorm : MTLPixelFormatRG16Unorm;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-07-18 07:10:22 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
2024-02-07 08:40:35 +00:00
|
|
|
"Unknown pixel format: %x",
|
|
|
|
CVPixelBufferGetPixelFormatType(pixBuf));
|
2018-07-18 07:10:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
CVReturn err = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, m_TextureCache, pixBuf, nullptr, fmt,
|
|
|
|
CVPixelBufferGetWidthOfPlane(pixBuf, i),
|
|
|
|
CVPixelBufferGetHeightOfPlane(pixBuf, i),
|
|
|
|
i,
|
|
|
|
&textures[i]);
|
|
|
|
if (err != kCVReturnSuccess) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"CVMetalTextureCacheCreateTextureFromImage() failed: %d",
|
|
|
|
err);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-18 07:10:22 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
// Prepare a render pass to render into the next drawable
|
|
|
|
auto renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];
|
2024-02-11 04:49:13 +00:00
|
|
|
renderPassDescriptor.colorAttachments[0].texture = m_NextDrawable.texture;
|
2024-02-07 08:40:35 +00:00
|
|
|
renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
|
|
|
|
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 0.0);
|
|
|
|
renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
|
|
|
|
auto commandBuffer = [m_CommandQueue commandBuffer];
|
|
|
|
auto renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
|
2024-02-11 06:11:23 +00:00
|
|
|
|
|
|
|
// Bind textures and buffers then draw the video region
|
|
|
|
[renderEncoder setRenderPipelineState:m_VideoPipelineState];
|
2024-02-07 08:40:35 +00:00
|
|
|
for (size_t i = 0; i < textures.size(); i++) {
|
|
|
|
[renderEncoder setFragmentTexture:CVMetalTextureGetTexture(textures[i]) atIndex:i];
|
|
|
|
}
|
|
|
|
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer>) {
|
2024-02-11 04:49:13 +00:00
|
|
|
// Free textures after completion of rendering per CVMetalTextureCache requirements
|
|
|
|
for (const CVMetalTextureRef &tex : textures) {
|
|
|
|
CFRelease(tex);
|
|
|
|
}
|
2024-02-07 08:40:35 +00:00
|
|
|
}];
|
|
|
|
[renderEncoder setFragmentBuffer:m_CscParamsBuffer offset:0 atIndex:0];
|
|
|
|
[renderEncoder setVertexBuffer:m_VideoVertexBuffer offset:0 atIndex:0];
|
|
|
|
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
|
2024-02-11 06:11:23 +00:00
|
|
|
|
|
|
|
// Now draw any overlays that are enabled
|
|
|
|
for (int i = 0; i < Overlay::OverlayMax; i++) {
|
|
|
|
id<MTLTexture> overlayTexture = nullptr;
|
|
|
|
|
|
|
|
// Try to acquire a reference on the overlay texture
|
|
|
|
SDL_AtomicLock(&m_OverlayLock);
|
|
|
|
overlayTexture = [m_OverlayTextures[i] retain];
|
|
|
|
SDL_AtomicUnlock(&m_OverlayLock);
|
|
|
|
|
|
|
|
if (overlayTexture) {
|
|
|
|
SDL_FRect renderRect = {};
|
|
|
|
if (i == Overlay::OverlayStatusUpdate) {
|
|
|
|
// Bottom Left
|
|
|
|
renderRect.x = 0;
|
|
|
|
renderRect.y = 0;
|
|
|
|
}
|
|
|
|
else if (i == Overlay::OverlayDebug) {
|
|
|
|
// Top left
|
|
|
|
renderRect.x = 0;
|
|
|
|
renderRect.y = m_LastDrawableHeight - overlayTexture.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderRect.w = overlayTexture.width;
|
|
|
|
renderRect.h = overlayTexture.height;
|
|
|
|
|
|
|
|
// Convert screen space to normalized device coordinates
|
|
|
|
StreamUtils::screenSpaceToNormalizedDeviceCoords(&renderRect, m_LastDrawableWidth, m_LastDrawableHeight);
|
|
|
|
|
|
|
|
Vertex verts[] =
|
|
|
|
{
|
|
|
|
{ { renderRect.x, renderRect.y, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
|
|
|
{ { renderRect.x, renderRect.y+renderRect.h, 0.0f, 1.0f }, { 0.0f, 0} },
|
|
|
|
{ { renderRect.x+renderRect.w, renderRect.y, 0.0f, 1.0f }, { 1.0f, 1.0f} },
|
|
|
|
{ { renderRect.x+renderRect.w, renderRect.y+renderRect.h, 0.0f, 1.0f }, { 1.0f, 0} },
|
|
|
|
};
|
|
|
|
|
|
|
|
[renderEncoder setRenderPipelineState:m_OverlayPipelineState];
|
|
|
|
[renderEncoder setFragmentTexture:overlayTexture atIndex:0];
|
|
|
|
[renderEncoder setVertexBytes:verts length:sizeof(verts) atIndex:0];
|
|
|
|
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:SDL_arraysize(verts)];
|
|
|
|
|
|
|
|
[overlayTexture release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
[renderEncoder endEncoding];
|
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
// Queue a completion callback on the drawable to pace our rendering
|
|
|
|
SDL_LockMutex(m_PresentationMutex);
|
|
|
|
m_PendingPresentationCount++;
|
|
|
|
SDL_UnlockMutex(m_PresentationMutex);
|
|
|
|
[m_NextDrawable addPresentedHandler:^(id<MTLDrawable>) {
|
|
|
|
SDL_LockMutex(m_PresentationMutex);
|
|
|
|
m_PendingPresentationCount--;
|
|
|
|
SDL_CondSignal(m_PresentationCond);
|
|
|
|
SDL_UnlockMutex(m_PresentationMutex);
|
|
|
|
}];
|
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
// Flip to the newly rendered buffer
|
2024-02-11 04:49:13 +00:00
|
|
|
[commandBuffer presentDrawable:m_NextDrawable];
|
2024-02-07 08:40:35 +00:00
|
|
|
[commandBuffer commit];
|
2024-02-11 04:49:13 +00:00
|
|
|
|
2024-02-11 06:15:32 +00:00
|
|
|
// Wait for the command buffer to complete and free our CVMetalTextureCache references
|
|
|
|
[commandBuffer waitUntilCompleted];
|
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
[m_NextDrawable release];
|
|
|
|
m_NextDrawable = nullptr;
|
2023-09-12 01:49:35 +00:00
|
|
|
}}
|
2018-07-16 01:11:57 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
bool checkDecoderCapabilities(PDECODER_PARAMETERS params) {
|
2019-04-12 05:27:20 +00:00
|
|
|
if (params->videoFormat & VIDEO_FORMAT_MASK_H264) {
|
2022-01-07 05:05:18 +00:00
|
|
|
if (!VTIsHardwareDecodeSupported(kCMVideoCodecType_H264)) {
|
2018-07-16 01:11:57 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-07 05:05:18 +00:00
|
|
|
"No HW accelerated H.264 decode via VT");
|
|
|
|
return false;
|
2018-07-16 01:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 05:27:20 +00:00
|
|
|
else if (params->videoFormat & VIDEO_FORMAT_MASK_H265) {
|
2022-01-07 05:05:18 +00:00
|
|
|
if (!VTIsHardwareDecodeSupported(kCMVideoCodecType_HEVC)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HW accelerated HEVC decode via VT");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HEVC Main10 requires more extensive checks because there's no
|
|
|
|
// simple API to check for Main10 hardware decoding, and if we don't
|
|
|
|
// have it, we'll silently get software decoding with horrible performance.
|
|
|
|
if (params->videoFormat == VIDEO_FORMAT_H265_MAIN10) {
|
|
|
|
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
|
|
|
|
if (device == nullptr) {
|
2018-07-16 01:11:57 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-07 05:05:18 +00:00
|
|
|
"Unable to get default Metal device");
|
2018-07-16 01:11:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 02:36:43 +00:00
|
|
|
|
2022-01-07 05:05:18 +00:00
|
|
|
// Exclude all GPUs earlier than macOSGPUFamily2
|
|
|
|
// https://developer.apple.com/documentation/metal/mtlfeatureset/mtlfeatureset_macos_gpufamily2_v1
|
|
|
|
if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily2_v1]) {
|
|
|
|
if ([device.name containsString:@"Intel"]) {
|
|
|
|
// 500-series Intel GPUs are Skylake and don't support Main10 hardware decoding
|
|
|
|
if ([device.name containsString:@" 5"]) {
|
2019-11-06 02:36:43 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2022-01-07 05:05:18 +00:00
|
|
|
"No HEVC Main10 support on Skylake iGPU");
|
|
|
|
[device release];
|
2019-11-06 02:36:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 05:05:18 +00:00
|
|
|
else if ([device.name containsString:@"AMD"]) {
|
|
|
|
// FirePro D, M200, and M300 series GPUs don't support Main10 hardware decoding
|
|
|
|
if ([device.name containsString:@"FirePro D"] ||
|
|
|
|
[device.name containsString:@" M2"] ||
|
|
|
|
[device.name containsString:@" M3"]) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HEVC Main10 support on AMD GPUs until Polaris");
|
|
|
|
[device release];
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 02:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-12 03:00:34 +00:00
|
|
|
else {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HEVC Main10 support on macOS GPUFamily1 GPUs");
|
|
|
|
[device release];
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-07 05:05:18 +00:00
|
|
|
|
|
|
|
[device release];
|
2018-07-16 01:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-16 20:38:58 +00:00
|
|
|
else if (params->videoFormat & VIDEO_FORMAT_MASK_AV1) {
|
|
|
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 130000
|
|
|
|
if (!VTIsHardwareDecodeSupported(kCMVideoCodecType_AV1)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HW accelerated AV1 decode via VT");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 10-bit is part of the Main profile for AV1, so it will always
|
|
|
|
// be present on hardware that supports 8-bit.
|
|
|
|
#else
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"AV1 requires building with Xcode 14 or later");
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2018-07-16 01:11:57 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool initialize(PDECODER_PARAMETERS params) override
|
|
|
|
{ @autoreleasepool {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
m_Window = params->window;
|
|
|
|
|
|
|
|
if (!checkDecoderCapabilities(params)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-09 12:44:11 +00:00
|
|
|
err = av_hwdevice_ctx_create(&m_HwContext,
|
|
|
|
AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
if (err < 0) {
|
2018-07-16 01:11:57 +00:00
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
2023-09-09 12:44:11 +00:00
|
|
|
"av_hwdevice_ctx_create() failed for VT decoder: %d",
|
|
|
|
err);
|
2018-07-16 01:11:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-07 09:26:09 +00:00
|
|
|
m_MetalView = SDL_Metal_CreateView(m_Window);
|
|
|
|
if (!m_MetalView) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_Metal_CreateView() failed: %s",
|
|
|
|
SDL_GetError());
|
2024-02-07 08:40:35 +00:00
|
|
|
return false;
|
2023-09-09 12:44:11 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 09:26:09 +00:00
|
|
|
m_MetalLayer = (CAMetalLayer*)SDL_Metal_GetLayer(m_MetalView);
|
2024-02-07 08:40:35 +00:00
|
|
|
|
|
|
|
// Choose a device
|
|
|
|
m_MetalLayer.device = m_MetalLayer.preferredDevice;
|
|
|
|
if (!m_MetalLayer.device) {
|
|
|
|
m_MetalLayer.device = [MTLCreateSystemDefaultDevice() autorelease];
|
|
|
|
if (!m_MetalLayer.device) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No Metal device found!");
|
2023-09-09 12:44:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-07 08:40:35 +00:00
|
|
|
}
|
2023-09-09 12:44:11 +00:00
|
|
|
|
2024-02-09 00:19:01 +00:00
|
|
|
// Allow EDR content if we're streaming in a 10-bit format
|
2024-02-07 08:40:35 +00:00
|
|
|
m_MetalLayer.wantsExtendedDynamicRangeContent = !!(params->videoFormat & VIDEO_FORMAT_MASK_10BIT);
|
2024-02-09 00:19:01 +00:00
|
|
|
|
|
|
|
// Ideally, we don't actually want triple buffering due to increased
|
2024-02-11 04:49:13 +00:00
|
|
|
// display latency, since our render time is very short. However, we
|
|
|
|
// *need* 3 drawables in order to hit the offloaded "direct" display
|
|
|
|
// path for our Metal layer.
|
2024-02-09 00:19:01 +00:00
|
|
|
//
|
|
|
|
// If we only use 2 drawables, we'll be stuck in the composited path
|
|
|
|
// (particularly for windowed mode) and our latency will actually be
|
|
|
|
// higher than opting for triple buffering.
|
|
|
|
m_MetalLayer.maximumDrawableCount = 3;
|
|
|
|
|
|
|
|
// Allow tearing if V-Sync is off (also requires direct display path)
|
2024-02-07 08:40:35 +00:00
|
|
|
m_MetalLayer.displaySyncEnabled = params->enableVsync;
|
2018-09-23 01:34:15 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
// Create the Metal texture cache for our CVPixelBuffers
|
|
|
|
CFStringRef keys[1] = { kCVMetalTextureUsage };
|
|
|
|
NSUInteger values[1] = { MTLTextureUsageShaderRead };
|
|
|
|
auto cacheAttributes = CFDictionaryCreate(kCFAllocatorDefault, (const void**)keys, (const void**)values, 1, nullptr, nullptr);
|
|
|
|
err = CVMetalTextureCacheCreate(kCFAllocatorDefault, cacheAttributes, m_MetalLayer.device, nullptr, &m_TextureCache);
|
|
|
|
CFRelease(cacheAttributes);
|
2018-07-16 01:11:57 +00:00
|
|
|
|
2024-02-07 08:40:35 +00:00
|
|
|
if (err != kCVReturnSuccess) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"CVMetalTextureCacheCreate() failed: %d",
|
|
|
|
err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile our shaders
|
|
|
|
QString shaderSource = QString::fromUtf8(Path::readDataFile("vt_renderer.metal"));
|
|
|
|
m_ShaderLibrary = [m_MetalLayer.device newLibraryWithSource:shaderSource.toNSString() options:nullptr error:nullptr];
|
|
|
|
if (!m_ShaderLibrary) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Failed to compile shaders");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a command queue for submission
|
|
|
|
m_CommandQueue = [m_MetalLayer.device newCommandQueue];
|
2018-07-16 01:11:57 +00:00
|
|
|
return true;
|
2023-09-12 01:49:35 +00:00
|
|
|
}}
|
2018-07-16 01:11:57 +00:00
|
|
|
|
2024-02-11 06:11:23 +00:00
|
|
|
virtual void notifyOverlayUpdated(Overlay::OverlayType type) override
|
2023-09-12 01:49:35 +00:00
|
|
|
{ @autoreleasepool {
|
2024-02-11 06:11:23 +00:00
|
|
|
SDL_Surface* newSurface = Session::get()->getOverlayManager().getUpdatedOverlaySurface(type);
|
|
|
|
bool overlayEnabled = Session::get()->getOverlayManager().isOverlayEnabled(type);
|
|
|
|
if (newSurface == nullptr && overlayEnabled) {
|
|
|
|
// The overlay is enabled and there is no new surface. Leave the old texture alone.
|
|
|
|
return;
|
2019-02-14 04:55:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-11 06:11:23 +00:00
|
|
|
SDL_AtomicLock(&m_OverlayLock);
|
|
|
|
auto oldTexture = m_OverlayTextures[type];
|
|
|
|
m_OverlayTextures[type] = nullptr;
|
|
|
|
SDL_AtomicUnlock(&m_OverlayLock);
|
2024-02-09 00:05:47 +00:00
|
|
|
|
2024-02-11 06:11:23 +00:00
|
|
|
[oldTexture release];
|
2019-02-14 04:55:52 +00:00
|
|
|
|
2024-02-11 06:11:23 +00:00
|
|
|
// If the overlay is disabled, we're done
|
|
|
|
if (!overlayEnabled) {
|
|
|
|
SDL_FreeSurface(newSurface);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-19 03:56:53 +00:00
|
|
|
|
2024-02-11 06:11:23 +00:00
|
|
|
// Create a texture to hold our pixel data
|
|
|
|
SDL_assert(!SDL_MUSTLOCK(newSurface));
|
|
|
|
SDL_assert(newSurface->format->format == SDL_PIXELFORMAT_ARGB8888);
|
|
|
|
auto texDesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
|
|
|
|
width:newSurface->w
|
|
|
|
height:newSurface->h
|
|
|
|
mipmapped:NO];
|
|
|
|
texDesc.cpuCacheMode = MTLCPUCacheModeWriteCombined;
|
|
|
|
texDesc.storageMode = MTLStorageModeManaged;
|
|
|
|
texDesc.usage = MTLTextureUsageShaderRead;
|
|
|
|
auto newTexture = [m_MetalLayer.device newTextureWithDescriptor:texDesc];
|
|
|
|
|
|
|
|
// Load the pixel data into the new texture
|
|
|
|
[newTexture replaceRegion:MTLRegionMake2D(0, 0, newSurface->w, newSurface->h)
|
|
|
|
mipmapLevel:0
|
|
|
|
withBytes:newSurface->pixels
|
|
|
|
bytesPerRow:newSurface->pitch];
|
|
|
|
|
|
|
|
// The surface is no longer required
|
|
|
|
SDL_FreeSurface(newSurface);
|
|
|
|
newSurface = nullptr;
|
|
|
|
|
|
|
|
SDL_AtomicLock(&m_OverlayLock);
|
|
|
|
m_OverlayTextures[type] = newTexture;
|
|
|
|
SDL_AtomicUnlock(&m_OverlayLock);
|
|
|
|
}}
|
2019-02-14 04:55:52 +00:00
|
|
|
|
2020-02-09 01:47:26 +00:00
|
|
|
virtual bool prepareDecoderContext(AVCodecContext* context, AVDictionary**) override
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
context->hw_device_ctx = av_buffer_ref(m_HwContext);
|
2018-07-28 23:06:26 +00:00
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
2024-02-07 08:40:35 +00:00
|
|
|
"Using VideoToolbox Metal renderer");
|
2018-07-28 23:06:26 +00:00
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-19 07:59:04 +00:00
|
|
|
virtual bool needsTestFrame() override
|
|
|
|
{
|
2019-02-16 03:10:21 +00:00
|
|
|
// We used to trust VT to tell us whether decode will work, but
|
|
|
|
// there are cases where it can lie because the hardware technically
|
|
|
|
// can decode the format but VT is unserviceable for some other reason.
|
|
|
|
// Decoding the test frame will tell us for sure whether it will work.
|
|
|
|
return true;
|
2018-08-19 07:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 06:19:56 +00:00
|
|
|
int getDecoderColorspace() override
|
2020-03-11 05:21:09 +00:00
|
|
|
{
|
2020-03-11 06:21:00 +00:00
|
|
|
// macOS seems to handle Rec 601 best
|
|
|
|
return COLORSPACE_REC_601;
|
2020-03-11 05:21:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 05:25:59 +00:00
|
|
|
int getDecoderCapabilities() override
|
|
|
|
{
|
2023-08-13 21:41:48 +00:00
|
|
|
return CAPABILITY_REFERENCE_FRAME_INVALIDATION_HEVC |
|
|
|
|
CAPABILITY_REFERENCE_FRAME_INVALIDATION_AV1;
|
2022-10-05 05:25:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 06:59:04 +00:00
|
|
|
int getRendererAttributes() override
|
|
|
|
{
|
|
|
|
// AVSampleBufferDisplayLayer supports HDR output
|
|
|
|
return RENDERER_ATTRIBUTE_HDR_SUPPORT;
|
|
|
|
}
|
|
|
|
|
2024-02-09 00:05:47 +00:00
|
|
|
bool notifyWindowChanged(PWINDOW_STATE_CHANGE_INFO info) override
|
|
|
|
{
|
|
|
|
auto unhandledStateFlags = info->stateChangeFlags;
|
|
|
|
|
|
|
|
// We can always handle size changes
|
|
|
|
unhandledStateFlags &= ~WINDOW_STATE_CHANGE_SIZE;
|
|
|
|
|
2024-02-11 04:49:13 +00:00
|
|
|
// We can handle monitor changes
|
|
|
|
unhandledStateFlags &= ~WINDOW_STATE_CHANGE_DISPLAY;
|
2024-02-09 00:05:47 +00:00
|
|
|
|
|
|
|
// If nothing is left, we handled everything
|
|
|
|
return unhandledStateFlags == 0;
|
|
|
|
}
|
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
private:
|
2024-02-07 08:40:35 +00:00
|
|
|
SDL_Window* m_Window;
|
2018-07-16 01:11:57 +00:00
|
|
|
AVBufferRef* m_HwContext;
|
2024-02-07 08:40:35 +00:00
|
|
|
CAMetalLayer* m_MetalLayer;
|
|
|
|
CVMetalTextureCacheRef m_TextureCache;
|
|
|
|
id<MTLBuffer> m_CscParamsBuffer;
|
|
|
|
id<MTLBuffer> m_VideoVertexBuffer;
|
2024-02-11 06:11:23 +00:00
|
|
|
id<MTLTexture> m_OverlayTextures[Overlay::OverlayMax];
|
|
|
|
SDL_SpinLock m_OverlayLock;
|
|
|
|
id<MTLRenderPipelineState> m_VideoPipelineState;
|
|
|
|
id<MTLRenderPipelineState> m_OverlayPipelineState;
|
2024-02-07 08:40:35 +00:00
|
|
|
id<MTLLibrary> m_ShaderLibrary;
|
|
|
|
id<MTLCommandQueue> m_CommandQueue;
|
2024-02-11 04:49:13 +00:00
|
|
|
id<CAMetalDrawable> m_NextDrawable;
|
2024-02-07 09:26:09 +00:00
|
|
|
SDL_MetalView m_MetalView;
|
2022-09-24 17:28:23 +00:00
|
|
|
int m_LastColorSpace;
|
2024-02-07 08:40:35 +00:00
|
|
|
bool m_LastFullRange;
|
2024-02-09 00:05:47 +00:00
|
|
|
int m_LastFrameWidth;
|
|
|
|
int m_LastFrameHeight;
|
|
|
|
int m_LastDrawableWidth;
|
|
|
|
int m_LastDrawableHeight;
|
2024-02-11 04:49:13 +00:00
|
|
|
SDL_mutex* m_PresentationMutex;
|
|
|
|
SDL_cond* m_PresentationCond;
|
|
|
|
int m_PendingPresentationCount;
|
2018-07-16 01:11:57 +00:00
|
|
|
};
|
|
|
|
|
2018-07-18 03:00:16 +00:00
|
|
|
IFFmpegRenderer* VTRendererFactory::createRenderer() {
|
2018-07-16 01:11:57 +00:00
|
|
|
return new VTRenderer();
|
|
|
|
}
|