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

Update progress on main thread #23

Merged
merged 1 commit into from
Mar 10, 2025
Merged
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
4 changes: 3 additions & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--swiftversion 5.9
--enable blankLineAfterImports
--enable blankLinesBetweenImports
--enable blockComments
Expand All @@ -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
6 changes: 3 additions & 3 deletions Sources/Upscaling/CoreImage/UpscalingFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -49,6 +46,9 @@ public class UpscalingFilter: CIFilter {
#endif
}

public var inputImage: CIImage?
public var outputSize: CGSize?

// MARK: Private

private let device = MTLCreateSystemDefaultDevice()
Expand Down
22 changes: 13 additions & 9 deletions Sources/Upscaling/UpscalingExportSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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: {
Expand Down
14 changes: 6 additions & 8 deletions Sources/fx-upscale/FXUpscale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}

Expand Down