Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mac Catalyst Support #116

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
664 changes: 646 additions & 18 deletions SGPlayer.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1100"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B0198738231AAB3900EC1C47"
BuildableName = "SGPlayer.framework"
BlueprintName = "SGPlayer Catalyst"
ReferencedContainer = "container:SGPlayer.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "B0198738231AAB3900EC1C47"
BuildableName = "SGPlayer.framework"
BlueprintName = "SGPlayer Catalyst"
ReferencedContainer = "container:SGPlayer.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
159 changes: 159 additions & 0 deletions SGPlayer/Classes/Catalyst.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
//

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my fork, I migrated to simd_float4x4 found in simd/simd.h. Granted, I had to re-write some of the functions used, but it should be able to use vector instructions on any architecture supported by LLVM.

// Catalyst.h
// SGPlayer
//
// Created by Steven Troughton-Smith on 31/08/2019.
// Copyright © 2019 single. All rights reserved.
//

#ifndef Catalyst_h
#define Catalyst_h

#define GLK_INLINE static __inline__

#if defined(__STRICT_ANSI__)
struct _GLKMatrix4
{
float m[16];
} __attribute__((aligned(16)));
typedef struct _GLKMatrix4 GLKMatrix4;
#else
union _GLKMatrix4
{
struct
{
float m00, m01, m02, m03;
float m10, m11, m12, m13;
float m20, m21, m22, m23;
float m30, m31, m32, m33;
};
float m[16];
} __attribute__((aligned(16)));
typedef union _GLKMatrix4 GLKMatrix4;
#endif

GLK_INLINE GLKMatrix4 GLKMatrix4Multiply(GLKMatrix4 matrixLeft, GLKMatrix4 matrixRight)
{
#if defined(GLK_SSE3_INTRINSICS)

const __m128 l0 = _mm_load_ps(&matrixLeft.m[0]);
const __m128 l1 = _mm_load_ps(&matrixLeft.m[4]);
const __m128 l2 = _mm_load_ps(&matrixLeft.m[8]);
const __m128 l3 = _mm_load_ps(&matrixLeft.m[12]);

const __m128 r0 = _mm_load_ps(&matrixRight.m[0]);
const __m128 r1 = _mm_load_ps(&matrixRight.m[4]);
const __m128 r2 = _mm_load_ps(&matrixRight.m[8]);
const __m128 r3 = _mm_load_ps(&matrixRight.m[12]);

const __m128 m0 = l0 * _mm_shuffle_ps(r0, r0, _MM_SHUFFLE(0, 0, 0, 0))
+ l1 * _mm_shuffle_ps(r0, r0, _MM_SHUFFLE(1, 1, 1, 1))
+ l2 * _mm_shuffle_ps(r0, r0, _MM_SHUFFLE(2, 2, 2, 2))
+ l3 * _mm_shuffle_ps(r0, r0, _MM_SHUFFLE(3, 3, 3, 3));

const __m128 m1 = l0 * _mm_shuffle_ps(r1, r1, _MM_SHUFFLE(0, 0, 0, 0))
+ l1 * _mm_shuffle_ps(r1, r1, _MM_SHUFFLE(1, 1, 1, 1))
+ l2 * _mm_shuffle_ps(r1, r1, _MM_SHUFFLE(2, 2, 2, 2))
+ l3 * _mm_shuffle_ps(r1, r1, _MM_SHUFFLE(3, 3, 3, 3));

const __m128 m2 = l0 * _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(0, 0, 0, 0))
+ l1 * _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(1, 1, 1, 1))
+ l2 * _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(2, 2, 2, 2))
+ l3 * _mm_shuffle_ps(r2, r2, _MM_SHUFFLE(3, 3, 3, 3));

const __m128 m3 = l0 * _mm_shuffle_ps(r3, r3, _MM_SHUFFLE(0, 0, 0, 0))
+ l1 * _mm_shuffle_ps(r3, r3, _MM_SHUFFLE(1, 1, 1, 1))
+ l2 * _mm_shuffle_ps(r3, r3, _MM_SHUFFLE(2, 2, 2, 2))
+ l3 * _mm_shuffle_ps(r3, r3, _MM_SHUFFLE(3, 3, 3, 3));

GLKMatrix4 m;
_mm_store_ps(&m.m[0], m0);
_mm_store_ps(&m.m[4], m1);
_mm_store_ps(&m.m[8], m2);
_mm_store_ps(&m.m[12], m3);
return m;

#else
GLKMatrix4 m;

m.m[0] = matrixLeft.m[0] * matrixRight.m[0] + matrixLeft.m[4] * matrixRight.m[1] + matrixLeft.m[8] * matrixRight.m[2] + matrixLeft.m[12] * matrixRight.m[3];
m.m[4] = matrixLeft.m[0] * matrixRight.m[4] + matrixLeft.m[4] * matrixRight.m[5] + matrixLeft.m[8] * matrixRight.m[6] + matrixLeft.m[12] * matrixRight.m[7];
m.m[8] = matrixLeft.m[0] * matrixRight.m[8] + matrixLeft.m[4] * matrixRight.m[9] + matrixLeft.m[8] * matrixRight.m[10] + matrixLeft.m[12] * matrixRight.m[11];
m.m[12] = matrixLeft.m[0] * matrixRight.m[12] + matrixLeft.m[4] * matrixRight.m[13] + matrixLeft.m[8] * matrixRight.m[14] + matrixLeft.m[12] * matrixRight.m[15];

m.m[1] = matrixLeft.m[1] * matrixRight.m[0] + matrixLeft.m[5] * matrixRight.m[1] + matrixLeft.m[9] * matrixRight.m[2] + matrixLeft.m[13] * matrixRight.m[3];
m.m[5] = matrixLeft.m[1] * matrixRight.m[4] + matrixLeft.m[5] * matrixRight.m[5] + matrixLeft.m[9] * matrixRight.m[6] + matrixLeft.m[13] * matrixRight.m[7];
m.m[9] = matrixLeft.m[1] * matrixRight.m[8] + matrixLeft.m[5] * matrixRight.m[9] + matrixLeft.m[9] * matrixRight.m[10] + matrixLeft.m[13] * matrixRight.m[11];
m.m[13] = matrixLeft.m[1] * matrixRight.m[12] + matrixLeft.m[5] * matrixRight.m[13] + matrixLeft.m[9] * matrixRight.m[14] + matrixLeft.m[13] * matrixRight.m[15];

m.m[2] = matrixLeft.m[2] * matrixRight.m[0] + matrixLeft.m[6] * matrixRight.m[1] + matrixLeft.m[10] * matrixRight.m[2] + matrixLeft.m[14] * matrixRight.m[3];
m.m[6] = matrixLeft.m[2] * matrixRight.m[4] + matrixLeft.m[6] * matrixRight.m[5] + matrixLeft.m[10] * matrixRight.m[6] + matrixLeft.m[14] * matrixRight.m[7];
m.m[10] = matrixLeft.m[2] * matrixRight.m[8] + matrixLeft.m[6] * matrixRight.m[9] + matrixLeft.m[10] * matrixRight.m[10] + matrixLeft.m[14] * matrixRight.m[11];
m.m[14] = matrixLeft.m[2] * matrixRight.m[12] + matrixLeft.m[6] * matrixRight.m[13] + matrixLeft.m[10] * matrixRight.m[14] + matrixLeft.m[14] * matrixRight.m[15];

m.m[3] = matrixLeft.m[3] * matrixRight.m[0] + matrixLeft.m[7] * matrixRight.m[1] + matrixLeft.m[11] * matrixRight.m[2] + matrixLeft.m[15] * matrixRight.m[3];
m.m[7] = matrixLeft.m[3] * matrixRight.m[4] + matrixLeft.m[7] * matrixRight.m[5] + matrixLeft.m[11] * matrixRight.m[6] + matrixLeft.m[15] * matrixRight.m[7];
m.m[11] = matrixLeft.m[3] * matrixRight.m[8] + matrixLeft.m[7] * matrixRight.m[9] + matrixLeft.m[11] * matrixRight.m[10] + matrixLeft.m[15] * matrixRight.m[11];
m.m[15] = matrixLeft.m[3] * matrixRight.m[12] + matrixLeft.m[7] * matrixRight.m[13] + matrixLeft.m[11] * matrixRight.m[14] + matrixLeft.m[15] * matrixRight.m[15];

return m;
#endif
}


extern const GLKMatrix4 GLKMatrix4Identity;

GLK_INLINE GLKMatrix4 GLKMatrix4MakeZRotation(float radians)
{
float cos = cosf(radians);
float sin = sinf(radians);

GLKMatrix4 m = { cos, sin, 0.0f, 0.0f,
-sin, cos, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };

return m;
}

GLK_INLINE GLKMatrix4 GLKMatrix4RotateZ(GLKMatrix4 matrix, float radians);
GLK_INLINE GLKMatrix4 GLKMatrix4RotateZ(GLKMatrix4 matrix, float radians)
{
GLKMatrix4 rm = GLKMatrix4MakeZRotation(radians);
return GLKMatrix4Multiply(matrix, rm);
}


GLK_INLINE GLKMatrix4 GLKMatrix4Transpose(GLKMatrix4 matrix)
{
GLKMatrix4 m = { matrix.m[0], matrix.m[4], matrix.m[8], matrix.m[12],
matrix.m[1], matrix.m[5], matrix.m[9], matrix.m[13],
matrix.m[2], matrix.m[6], matrix.m[10], matrix.m[14],
matrix.m[3], matrix.m[7], matrix.m[11], matrix.m[15] };
return m;
}


GLK_INLINE GLKMatrix4 GLKMatrix4MakeYRotation(float radians)
{
float cos = cosf(radians);
float sin = sinf(radians);

GLKMatrix4 m = { cos, 0.0f, -sin, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
sin, 0.0f, cos, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };

return m;
}

GLK_INLINE GLKMatrix4 GLKMatrix4RotateY(GLKMatrix4 matrix, float radians)
{
GLKMatrix4 rm = GLKMatrix4MakeYRotation(radians);
return GLKMatrix4Multiply(matrix, rm);
}

GLK_INLINE float GLKMathDegreesToRadians(float degrees) { return degrees * (M_PI / 180); };


#endif /* Catalyst_h */
4 changes: 2 additions & 2 deletions SGPlayer/Classes/Core/SGAudio/SGAudioPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ + (AudioComponentDescription)mixerACD
{
AudioComponentDescription acd;
acd.componentType = kAudioUnitType_Mixer;
#if SGPLATFORM_TARGET_OS_MAC
acd.componentSubType = kAudioUnitSubType_StereoMixer;
#if SGPLATFORM_TARGET_OS_MAC || TARGET_OS_MACCATALYST
acd.componentSubType = kAudioUnitSubType_SpatialMixer;
#elif SGPLATFORM_TARGET_OS_IPHONE_OR_TV
acd.componentSubType = kAudioUnitSubType_MultiChannelMixer;
#endif
Expand Down
4 changes: 2 additions & 2 deletions SGPlayer/Classes/Core/SGFFmpeg/SGFFmpeg.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

static void SGFFmpegLogCallback(void * context, int level, const char * format, va_list args)
{
// NSString * message = [[NSString alloc] initWithFormat:[NSString stringWithUTF8String:format] arguments:args];
// NSLog(@"SGFFLog : %@", message);
NSString * message = [[NSString alloc] initWithFormat:[NSString stringWithUTF8String:format] arguments:args];
NSLog(@"> SGFFLog : %@", message);
}

void SGFFmpegSetupIfNeeded(void)
Expand Down
4 changes: 4 additions & 0 deletions SGPlayer/Classes/Core/SGMetal/SGMetalProjection.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
//

#import <Metal/Metal.h>
#if TARGET_OS_MACCATALYST
#import "Catalyst.h"
#else
#import <GLKit/GLKit.h>
#endif

@interface SGMetalProjection : NSObject

Expand Down
8 changes: 6 additions & 2 deletions SGPlayer/Classes/Core/SGMetal/SGMetalRenderPipelinePool.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
#import "SGMetalBGRARenderPipeline.h"

#import "SGPLFTargets.h"
#if TARGET_OS_MACCATALYST
#import "SGMetalShader_macOS.h"
#else
#if SGPLATFORM_TARGET_OS_IPHONE
#import "SGMetalShader_iOS.h"
#elif SGPLATFORM_TARGET_OS_TV
#import "SGMetalShader_tvOS.h"
#elif SGPLATFORM_TARGET_OS_MAC
#import "SGMetalShader_macOS.h"
#endif
#endif

@interface SGMetalRenderPipelinePool ()

Expand All @@ -36,8 +40,8 @@ - (instancetype)initWithDevice:(id<MTLDevice>)device
{
if (self = [super init]) {
self.device = device;
self.library = [device newLibraryWithData:dispatch_data_create(metallib, sizeof(metallib), dispatch_get_global_queue(0, 0), ^{}) error:NULL];
}
self.library = [device newLibraryWithData:dispatch_data_create(metallib, sizeof(metallib), dispatch_get_global_queue(0, 0), ^{}) error:NULL];
}
return self;
}

Expand Down
23 changes: 19 additions & 4 deletions SGPlayer/Classes/Core/SGMetal/SGMetalYUVRenderPipeline.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,26 @@ @implementation SGMetalYUVRenderPipeline
- (instancetype)initWithDevice:(id<MTLDevice>)device library:(id<MTLLibrary>)library
{
if (self = [super initWithDevice:device library:library]) {

NSError *error = nil;

self.descriptor = [[MTLRenderPipelineDescriptor alloc] init];
self.descriptor.vertexFunction = [self.library newFunctionWithName:@"vertexShader"];
self.descriptor.fragmentFunction = [self.library newFunctionWithName:@"fragmentShaderYUV"];
self.descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
self.state = [self.device newRenderPipelineStateWithDescriptor:self.descriptor error:nil];
self.descriptor.vertexFunction = [self.library newFunctionWithName:@"vertexShader" constantValues:[MTLFunctionConstantValues new] error:&error];

if (error)
NSLog(@"ERROR: %@", error.localizedDescription);

self.descriptor.fragmentFunction = [self.library newFunctionWithName:@"fragmentShaderYUV" constantValues:[MTLFunctionConstantValues new] error:&error];

if (error)
NSLog(@"ERROR: %@", error.localizedDescription);

self.descriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
self.state = [self.device newRenderPipelineStateWithDescriptor:self.descriptor error:&error];

if (error)
NSLog(@"ERROR: %@", error.localizedDescription);

}
return self;
}
Expand Down
2 changes: 1 addition & 1 deletion SGPlayer/Classes/Core/SGOption/SGDecoderOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ - (id)copyWithZone:(NSZone *)zone
- (instancetype)init
{
if (self = [super init]) {
self->_options = nil;
self->_options = @{@"sn":[NSNull null]};
self->_threadsAuto = YES;
self->_refcountedFrames = YES;
self->_hardwareDecodeH264 = YES;
Expand Down
8 changes: 6 additions & 2 deletions SGPlayer/Classes/Core/SGOption/SGDemuxerOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ - (id)copyWithZone:(NSZone *)zone
- (instancetype)init
{
if (self = [super init]) {
self->_options = @{@"reconnect" : @(1),
self->_options = @{@"timeout" : @(20 * 1000 * 1000),
@"reconnect" : @(1),
@"user-agent" : @"SGPlayer",
@"timeout" : @(20 * 1000 * 1000)};
// @"headers": @"Connection: Keep-Alive\r\n",
// @"sn" : @"",
// @"vf" : @"scale=w=1920:h=1080:force_original_aspect_ratio=decrease"
};
}
return self;
}
Expand Down
2 changes: 1 addition & 1 deletion SGPlayer/Classes/Core/SGPlatform/SGPLFTargets.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#import <TargetConditionals.h>

#define SGPLATFORM_TARGET_OS_MAC TARGET_OS_OSX
#define SGPLATFORM_TARGET_OS_IPHONE TARGET_OS_IOS
#define SGPLATFORM_TARGET_OS_IPHONE TARGET_OS_IOS||TARGET_OS_MACCATALYST
#define SGPLATFORM_TARGET_OS_TV TARGET_OS_TV

#define SGPLATFORM_TARGET_OS_MAC_OR_IPHONE (SGPLATFORM_TARGET_OS_MAC || SGPLATFORM_TARGET_OS_IPHONE)
Expand Down
7 changes: 6 additions & 1 deletion SGPlayer/Classes/Core/SGVR/SGMotionSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
//

#import <Foundation/Foundation.h>

#if TARGET_OS_MACCATALYST
#import "Catalyst.h"
#else
#import <GLKit/GLKit.h>
#endif


@interface SGMotionSensor : NSObject

@property (nonatomic, readonly) BOOL ready;
@property (nonatomic, readonly) GLKMatrix4 matrix;

- (void)start;
- (void)stop;

Expand Down
Loading