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>
|
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
#include <mach/mach_time.h>
|
2018-07-16 01:11:57 +00:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#import <VideoToolbox/VideoToolbox.h>
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
2018-07-18 07:10:22 +00:00
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
class VTRenderer : public IFFmpegRenderer, public IVsyncRenderer
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
VTRenderer()
|
|
|
|
: m_HwContext(nullptr),
|
|
|
|
m_DisplayLayer(nullptr),
|
|
|
|
m_FormatDesc(nullptr),
|
2018-07-18 07:10:22 +00:00
|
|
|
m_View(nullptr),
|
2018-08-16 05:02:15 +00:00
|
|
|
m_Pacer(this)
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~VTRenderer()
|
|
|
|
{
|
2018-08-15 02:13:17 +00:00
|
|
|
m_Pacer.drain();
|
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
if (m_HwContext != nullptr) {
|
|
|
|
av_buffer_unref(&m_HwContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_FormatDesc != nullptr) {
|
|
|
|
CFRelease(m_FormatDesc);
|
|
|
|
}
|
2018-07-18 07:10:22 +00:00
|
|
|
|
2018-07-21 02:37:54 +00:00
|
|
|
if (m_View != nullptr) {
|
|
|
|
[m_View removeFromSuperview];
|
|
|
|
}
|
2018-07-18 07:10:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
// Caller frees frame after we return
|
|
|
|
virtual void renderFrameAtVsync(AVFrame* frame) override
|
2018-07-18 07:10:22 +00:00
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
CVPixelBufferRef pixBuf = reinterpret_cast<CVPixelBufferRef>(frame->data[3]);
|
|
|
|
|
|
|
|
// If the format has changed or doesn't exist yet, construct it with the
|
|
|
|
// pixel buffer data
|
|
|
|
if (!m_FormatDesc || !CMVideoFormatDescriptionMatchesImageBuffer(m_FormatDesc, pixBuf)) {
|
|
|
|
if (m_FormatDesc != nullptr) {
|
|
|
|
CFRelease(m_FormatDesc);
|
|
|
|
}
|
|
|
|
status = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault,
|
|
|
|
pixBuf, &m_FormatDesc);
|
|
|
|
if (status != noErr) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"CMVideoFormatDescriptionCreateForImageBuffer() failed: %d",
|
|
|
|
status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue this sample for the next v-sync
|
|
|
|
CMSampleTimingInfo timingInfo = {
|
|
|
|
.duration = kCMTimeInvalid,
|
|
|
|
.decodeTimeStamp = kCMTimeInvalid,
|
2018-08-16 05:02:15 +00:00
|
|
|
.presentationTimeStamp = CMTimeMake(mach_absolute_time(), 1000 * 1000 * 1000)
|
2018-07-18 07:10:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CMSampleBufferRef sampleBuffer;
|
|
|
|
status = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault,
|
|
|
|
pixBuf,
|
|
|
|
m_FormatDesc,
|
|
|
|
&timingInfo,
|
|
|
|
&sampleBuffer);
|
|
|
|
if (status != noErr) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"CMSampleBufferCreateReadyWithImageBuffer() failed: %d",
|
|
|
|
status);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[m_DisplayLayer enqueueSampleBuffer:sampleBuffer];
|
|
|
|
|
|
|
|
CFRelease(sampleBuffer);
|
|
|
|
}
|
2018-07-16 01:11:57 +00:00
|
|
|
|
|
|
|
virtual bool initialize(SDL_Window* window,
|
|
|
|
int videoFormat,
|
|
|
|
int,
|
2018-08-16 03:41:19 +00:00
|
|
|
int,
|
2018-08-16 04:10:35 +00:00
|
|
|
int maxFps) override
|
2018-07-16 01:11:57 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2018-08-16 05:02:15 +00:00
|
|
|
if (!m_Pacer.initialize(window, maxFps)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-16 04:10:35 +00:00
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
if (videoFormat & VIDEO_FORMAT_MASK_H264) {
|
|
|
|
// Prior to 10.13, we'll just assume everything has
|
|
|
|
// H.264 support and fail open to allow VT decode.
|
|
|
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
|
|
|
|
if (__builtin_available(macOS 10.13, *)) {
|
|
|
|
if (!VTIsHardwareDecodeSupported(kCMVideoCodecType_H264)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HW accelerated H.264 decode via VT");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Assuming H.264 HW decode on < macOS 10.13");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (videoFormat & VIDEO_FORMAT_MASK_H265) {
|
|
|
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
|
|
|
|
if (__builtin_available(macOS 10.13, *)) {
|
|
|
|
if (!VTIsHardwareDecodeSupported(kCMVideoCodecType_HEVC)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HW accelerated HEVC decode via VT");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// Fail closed for HEVC if we're not on 10.13+
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"No HEVC support on < macOS 10.13");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SysWMinfo info;
|
|
|
|
|
|
|
|
SDL_VERSION(&info.version);
|
|
|
|
|
|
|
|
if (!SDL_GetWindowWMInfo(window, &info)) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_GetWindowWMInfo() failed: %s",
|
|
|
|
SDL_GetError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_assert(info.subsystem == SDL_SYSWM_COCOA);
|
|
|
|
|
|
|
|
// SDL adds its own content view to listen for events.
|
|
|
|
// We need to add a subview for our display layer.
|
|
|
|
NSView* contentView = info.info.cocoa.window.contentView;
|
|
|
|
m_View = [[NSView alloc] initWithFrame:contentView.bounds];
|
|
|
|
m_View.wantsLayer = YES;
|
|
|
|
[contentView addSubview: m_View];
|
|
|
|
|
|
|
|
setupDisplayLayer();
|
|
|
|
|
|
|
|
err = av_hwdevice_ctx_create(&m_HwContext,
|
|
|
|
AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
if (err < 0) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"av_hwdevice_ctx_create() failed for VT decoder: %d",
|
|
|
|
err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool prepareDecoderContext(AVCodecContext* context) override
|
|
|
|
{
|
|
|
|
context->hw_device_ctx = av_buffer_ref(m_HwContext);
|
2018-07-28 23:06:26 +00:00
|
|
|
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Using VideoToolbox accelerated renderer");
|
|
|
|
|
2018-07-16 01:11:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void renderFrame(AVFrame* frame) override
|
|
|
|
{
|
|
|
|
if (m_DisplayLayer.status == AVQueuedSampleBufferRenderingStatusFailed) {
|
|
|
|
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Resetting failed AVSampleBufferDisplay layer");
|
|
|
|
setupDisplayLayer();
|
|
|
|
}
|
|
|
|
|
2018-08-15 02:13:17 +00:00
|
|
|
m_Pacer.submitFrame(frame);
|
2018-07-16 01:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setupDisplayLayer()
|
|
|
|
{
|
|
|
|
CALayer* oldLayer = m_DisplayLayer;
|
|
|
|
|
|
|
|
m_DisplayLayer = [[AVSampleBufferDisplayLayer alloc] init];
|
|
|
|
m_DisplayLayer.bounds = m_View.bounds;
|
|
|
|
m_DisplayLayer.position = CGPointMake(CGRectGetMidX(m_View.bounds), CGRectGetMidY(m_View.bounds));
|
|
|
|
m_DisplayLayer.videoGravity = AVLayerVideoGravityResizeAspect;
|
|
|
|
|
|
|
|
CALayer* viewLayer = m_View.layer;
|
|
|
|
if (oldLayer != nil) {
|
|
|
|
[viewLayer replaceSublayer:oldLayer with:m_DisplayLayer];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
[viewLayer addSublayer:m_DisplayLayer];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AVBufferRef* m_HwContext;
|
|
|
|
AVSampleBufferDisplayLayer* m_DisplayLayer;
|
|
|
|
CMVideoFormatDescriptionRef m_FormatDesc;
|
|
|
|
NSView* m_View;
|
2018-08-15 02:13:17 +00:00
|
|
|
Pacer m_Pacer;
|
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();
|
|
|
|
}
|