From d4e0c1c642aacd42b91d6c549e8c1a5936af9b3d Mon Sep 17 00:00:00 2001 From: Finn Voorhees Date: Mon, 10 Mar 2025 22:25:40 +0000 Subject: [PATCH] Update progress on main thread --- .swiftformat | 4 +++- .../Upscaling/CoreImage/UpscalingFilter.swift | 6 ++--- .../Upscaling/UpscalingExportSession.swift | 22 +++++++++++-------- Sources/fx-upscale/FXUpscale.swift | 14 +++++------- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/.swiftformat b/.swiftformat index 879e079..654a195 100644 --- a/.swiftformat +++ b/.swiftformat @@ -1,3 +1,4 @@ +--swiftversion 5.9 --enable blankLineAfterImports --enable blankLinesBetweenImports --enable blockComments @@ -14,7 +15,8 @@ --ifdef no-indent --funcattributes same-line --typeattributes same-line ---varattributes same-line +--computedvarattrs same-line +--storedvarattrs same-line --ranges no-space --header strip --selfrequired log,debug,info,notice,warning,trace,error,critical,fault diff --git a/Sources/Upscaling/CoreImage/UpscalingFilter.swift b/Sources/Upscaling/CoreImage/UpscalingFilter.swift index 1df5f64..cdc7486 100644 --- a/Sources/Upscaling/CoreImage/UpscalingFilter.swift +++ b/Sources/Upscaling/CoreImage/UpscalingFilter.swift @@ -9,9 +9,6 @@ import MetalFX public class UpscalingFilter: CIFilter { // MARK: Public - public var inputImage: CIImage? - public var outputSize: CGSize? - override public var outputImage: CIImage? { #if canImport(MetalFX) guard let device, let inputImage, let outputSize else { return nil } @@ -49,6 +46,9 @@ public class UpscalingFilter: CIFilter { #endif } + public var inputImage: CIImage? + public var outputSize: CGSize? + // MARK: Private private let device = MTLCreateSystemDefaultDevice() diff --git a/Sources/Upscaling/UpscalingExportSession.swift b/Sources/Upscaling/UpscalingExportSession.swift index 8bb0f5b..36b6e74 100644 --- a/Sources/Upscaling/UpscalingExportSession.swift +++ b/Sources/Upscaling/UpscalingExportSession.swift @@ -51,13 +51,11 @@ public class UpscalingExportSession { throw Error.outputURLAlreadyExists } - let outputFileType: AVFileType = { - switch outputURL.pathExtension.lowercased() { - case "mov": return .mov - case "m4v": return .m4v - default: return .mp4 - } - }() + let outputFileType: AVFileType = switch outputURL.pathExtension.lowercased() { + case "mov": .mov + case "m4v": .m4v + default: .mp4 + } let assetWriter = try AVAssetWriter(outputURL: outputURL, fileType: outputFileType) assetWriter.metadata = try await asset.load(.metadata) @@ -383,7 +381,10 @@ public class UpscalingExportSession { while assetWriterInput.isReadyForMoreMediaData { if let nextSampleBuffer = assetReaderOutput.copyNextSampleBuffer() { if nextSampleBuffer.presentationTimeStamp.isNumeric { - progress.completedUnitCount = Int64(nextSampleBuffer.presentationTimeStamp.seconds) + let timestamp = Int64(nextSampleBuffer.presentationTimeStamp.seconds) + DispatchQueue.main.async { + progress.completedUnitCount = timestamp + } } if let imageBuffer = nextSampleBuffer.imageBuffer { let upscaledImageBuffer = upscaler.upscale( @@ -433,7 +434,10 @@ public class UpscalingExportSession { while assetWriterInput.isReadyForMoreMediaData { if let nextSampleBuffer = assetReaderOutput.copyNextSampleBuffer() { if nextSampleBuffer.presentationTimeStamp.isNumeric { - progress.completedUnitCount = Int64(nextSampleBuffer.presentationTimeStamp.seconds) + let timestamp = Int64(nextSampleBuffer.presentationTimeStamp.seconds) + DispatchQueue.main.async { + progress.completedUnitCount = timestamp + } } if let taggedBuffers = nextSampleBuffer.taggedBuffers { let leftEyeBuffer = taggedBuffers.first(where: { diff --git a/Sources/fx-upscale/FXUpscale.swift b/Sources/fx-upscale/FXUpscale.swift index 4ec394b..d516a4f 100644 --- a/Sources/fx-upscale/FXUpscale.swift +++ b/Sources/fx-upscale/FXUpscale.swift @@ -52,18 +52,16 @@ import Upscaling } let outputSize = CGSize(width: width, height: height) - let outputCodec: AVVideoCodecType? = { - switch codec.lowercased() { - case "prores": return .proRes422 - case "h264": return .h264 - default: return .hevc - } - }() + let outputCodec: AVVideoCodecType? = switch codec.lowercased() { + case "prores": .proRes422 + case "h264": .h264 + default: .hevc + } // Through anecdotal tests anything beyond 14.5K fails to encode for anything other than ProRes let convertToProRes = (outputSize.width * outputSize.height) > (14500 * 8156) - if (convertToProRes) { + if convertToProRes { CommandLine.info("Forced ProRes conversion due to output size being larger than 14.5K (will fail otherwise)") }