Azat
Azat

Reputation: 590

Using Custom Video Compositor Class rotates the video

I'm building a subtitle generator. I have a subtitle layer which has all the implementations for the subtitles such as animations, styling, etc.

When I'm exporting the video, the video is rotated to 90 degrees to left. I've tried applying some transforms. It does work when I swap width and height on the render size and apply preferredTransform.

layerInstruction.setTransform(videoTrack.preferredTransform, at: .zero)

The composition code:

     let subtitleLayer = VideoSubtitleLayer()
        subtitleLayer.frame = CGRect(x: 0, y: 0, width: videoTrack.naturalSize.width, height: videoTrack.naturalSize.height)

        let videoComposition = AVMutableVideoComposition()
        videoComposition.frameDuration = CMTime(value: 1, timescale: 30)
        videoComposition.renderSize = CGSize(width: videoTrack.naturalSize.height, height: videoTrack.naturalSize.width) // swap width and height

        let defaultPosition = CGPoint(x: videoTrack.naturalSize.width * 0.5, y: videoTrack.naturalSize.height * 0.9)

        let instruction = VideoSubtitleInstruction(
            timeRange: CMTimeRange(start: .zero, duration: videoAsset.duration),
            subtitleLayer: subtitleLayer,
            savedSubtitlePosition: savedSubtitlePosition ?? defaultPosition
        )

        let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: compositionVideoTrack)

        layerInstruction.setTransform(videoTrack.preferredTransform, at: .zero)

        instruction.layerInstructions = [layerInstruction]
        videoComposition.instructions = [instruction]
        videoComposition.customVideoCompositorClass = SubtitleCompositor.self

This is working fine if I don't use my custom compositor class. If I start using it, the video starts to rotate 90 degrees to left again. videoComposition.customVideoCompositorClass = SubtitleCompositor.self

The subtitle compositor implementation looks like this:

import AVFoundation
import CoreImage

class SubtitleCompositor: NSObject, AVVideoCompositing {
    let subtitles: [Subtitle]

    override init() {
        self.subtitles = SubtitleStore.shared.subtitles
        super.init()
    }

    var requiredPixelBufferAttributesForRenderContext: [String : Any] = [
        kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB
    ]

    var sourcePixelBufferAttributes: [String : Any]? = [
        kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32ARGB
    ]

    private var renderContext: AVVideoCompositionRenderContext?

    func renderContextChanged(_ newRenderContext: AVVideoCompositionRenderContext) {
        renderContext = newRenderContext
    }

    func startRequest(_ request: AVAsynchronousVideoCompositionRequest) {
        guard let renderContext = renderContext else {
            print("Render context is nil.")
            request.finish(with: NSError(domain: "SubtitleCompositor", code: -1, userInfo: nil))
            return
        }

        guard let sourcePixelBuffer = request.sourceFrame(byTrackID: request.sourceTrackIDs[0].int32Value) else {
            print("Source pixel buffer is nil.")
            request.finish(with: NSError(domain: "SubtitleCompositor", code: -2, userInfo: nil))
            return
        }

        guard let destinationPixelBuffer = renderContext.newPixelBuffer() else {
            print("Failed to create destination pixel buffer.")
            request.finish(with: NSError(domain: "SubtitleCompositor", code: -3, userInfo: nil))
            return
        }

        // Copy the source pixel buffer to the destination pixel buffer
        copyPixelBuffer(sourcePixelBuffer, to: destinationPixelBuffer)

        guard let videoInstruction = request.videoCompositionInstruction as? VideoSubtitleInstruction,
              let subtitleLayer = videoInstruction.subtitleLayer else {
            print("Video instruction or subtitle layer is nil.")
            request.finish(withComposedVideoFrame: destinationPixelBuffer)
            return
        }

        DispatchQueue.main.sync {
            let currentTime = request.compositionTime.seconds

            if let subtitle = subtitles.first(where: { $0.startTime <= currentTime && $0.endTime > currentTime }) {
                subtitleLayer.updateSubtitle(subtitle: subtitle, time: CFTimeInterval(currentTime))
            } else {
                subtitleLayer.updateSubtitle(subtitle: nil, time: CFTimeInterval(currentTime))
            }

            if let position = videoInstruction.savedSubtitlePosition {
                subtitleLayer.setInitialPosition(position)
            }

            renderLayer(subtitleLayer, to: destinationPixelBuffer)
        }

        request.finish(withComposedVideoFrame: destinationPixelBuffer)
    }


    private func copyPixelBuffer(_ sourcePixelBuffer: CVPixelBuffer, to destinationPixelBuffer: CVPixelBuffer) {
        CVPixelBufferLockBaseAddress(sourcePixelBuffer, .readOnly)
        CVPixelBufferLockBaseAddress(destinationPixelBuffer, [])

        guard let sourceBaseAddress = CVPixelBufferGetBaseAddress(sourcePixelBuffer),
              let destinationBaseAddress = CVPixelBufferGetBaseAddress(destinationPixelBuffer) else {
            CVPixelBufferUnlockBaseAddress(sourcePixelBuffer, .readOnly)
            CVPixelBufferUnlockBaseAddress(destinationPixelBuffer, [])
            return
        }

        let sourceBytesPerRow = CVPixelBufferGetBytesPerRow(sourcePixelBuffer)
        let destinationBytesPerRow = CVPixelBufferGetBytesPerRow(destinationPixelBuffer)
        let height = CVPixelBufferGetHeight(sourcePixelBuffer)

        for row in 0..<height {
            memcpy(destinationBaseAddress + row * destinationBytesPerRow, sourceBaseAddress + row * sourceBytesPerRow, min(sourceBytesPerRow, destinationBytesPerRow))
        }

        CVPixelBufferUnlockBaseAddress(sourcePixelBuffer, .readOnly)
        CVPixelBufferUnlockBaseAddress(destinationPixelBuffer, [])
    }

    private func renderLayer(_ layer: CALayer, to pixelBuffer: CVPixelBuffer) {
        CVPixelBufferLockBaseAddress(pixelBuffer, [])

        guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
                                      width: CVPixelBufferGetWidth(pixelBuffer),
                                      height: CVPixelBufferGetHeight(pixelBuffer),
                                      bitsPerComponent: 8,
                                      bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
                                      space: CGColorSpaceCreateDeviceRGB(),
                                      bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue) else {
            CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
            return
        }


        // Flip the context vertically
        context.translateBy(x: 0, y: CGFloat(CVPixelBufferGetHeight(pixelBuffer)))
        context.scaleBy(x: 1.0, y: -1.0)


        // Render the layer to the context
        if layer.bounds.isEmpty || layer.sublayers?.isEmpty == true {
            print("Subtitle layer is empty or has no sublayers.")
        } else {
            layer.render(in: context)
        }

        CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
    }
}

I tried to rotate the sourcePixelBuffer to 90 degrees, It works fine but I don't think it is proper way to solve this problem. It also breaks the subtitle layer.

Upvotes: 0

Views: 91

Answers (1)

Matthew Harries
Matthew Harries

Reputation: 650

When using a custom compositor, you are responsible for applying the video track's preferred transform to the source frame that you receive in the request.

The size of the output frame returned from renderContext.newPixelBuffer() will match whatever you have set in AVVideoComposition.renderSize.

I suggest looking into using a CoreImage pipeline to apply your transform (and possibly subtitles) if you're not using a custom graphics pipeline like OpenGL or Metal.

https://developer.apple.com/documentation/coreimage/ciimage/1438203-imagebyapplyingtransform?language=objc

Upvotes: 0

Related Questions