Optimize software video playback

This commit is contained in:
Ilya Laktyushin 2021-12-09 23:40:20 +04:00
parent c6be0f9b40
commit f4301ce632
3 changed files with 36 additions and 29 deletions

View File

@ -0,0 +1,9 @@
#ifndef FrameConverter_h
#define FrameConverter_h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
void fillDstPlane(uint8_t * _Nonnull dstPlane, uint8_t * _Nonnull srcPlane1, uint8_t * _Nonnull srcPlane2, size_t srcPlaneSize);
#endif /* FrameConverter_h */

View File

@ -0,0 +1,8 @@
#import <FFMpegBinding/FrameConverter.h>
void fillDstPlane(uint8_t * _Nonnull dstPlane, uint8_t * _Nonnull srcPlane1, uint8_t * _Nonnull srcPlane2, size_t srcPlaneSize) {
for (size_t i = 0; i < srcPlaneSize; i++){
dstPlane[2 * i] = srcPlane1[i];
dstPlane[2 * i + 1] = srcPlane2[i];
}
}

View File

@ -56,30 +56,17 @@ public final class FFMpegMediaVideoFrameDecoder: MediaTrackFrameDecoder {
private var delayedFrames: [MediaTrackFrame] = []
private var dstPlane: (UnsafeMutablePointer<UInt8>, Int)?
public init(codecContext: FFMpegAVCodecContext) {
self.codecContext = codecContext
self.videoFrame = FFMpegAVFrame()
/*var sourcePixelBufferOptions: [String: Any] = [:]
sourcePixelBufferOptions[kCVPixelBufferPixelFormatTypeKey as String] = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange as NSNumber
sourcePixelBufferOptions[kCVPixelBufferWidthKey as String] = codecContext.pointee.width as NSNumber
sourcePixelBufferOptions[kCVPixelBufferHeightKey as String] = codecContext.pointee.height as NSNumber
sourcePixelBufferOptions[kCVPixelBufferBytesPerRowAlignmentKey as String] = 128 as NSNumber
sourcePixelBufferOptions[kCVPixelBufferPlaneAlignmentKey as String] = 128 as NSNumber
let ioSurfaceProperties = NSMutableDictionary()
ioSurfaceProperties["IOSurfaceIsGlobal"] = true as NSNumber
sourcePixelBufferOptions[kCVPixelBufferIOSurfacePropertiesKey as String] = ioSurfaceProperties
var pixelBufferPoolOptions: [String: Any] = [:]
pixelBufferPoolOptions[kCVPixelBufferPoolMinimumBufferCountKey as String] = bufferCount as NSNumber
var pixelBufferPool: CVPixelBufferPool?
CVPixelBufferPoolCreate(kCFAllocatorDefault, pixelBufferPoolOptions as CFDictionary, sourcePixelBufferOptions as CFDictionary, &pixelBufferPool)
self.pixelBufferPool = pixelBufferPool*/
}
deinit {
if let (dstPlane, _) = self.dstPlane {
free(dstPlane)
}
}
func decodeInternal(frame: MediaTrackDecodableFrame) {
@ -298,15 +285,18 @@ public final class FFMpegMediaVideoFrameDecoder: MediaTrackFrameDecoder {
let srcPlaneSize = Int(frame.lineSize[1]) * Int(frame.height / 2)
let dstPlaneSize = srcPlaneSize * 2
let dstPlane = malloc(dstPlaneSize)!.assumingMemoryBound(to: UInt8.self)
defer {
free(dstPlane)
}
for i in 0 ..< srcPlaneSize {
dstPlane[2 * i] = frame.data[1]![i]
dstPlane[2 * i + 1] = frame.data[2]![i]
let dstPlane: UnsafeMutablePointer<UInt8>
if let (existingDstPlane, existingDstPlaneSize) = self.dstPlane, existingDstPlaneSize == dstPlaneSize {
dstPlane = existingDstPlane
} else {
if let (existingDstPlane, _) = self.dstPlane {
free(existingDstPlane)
}
dstPlane = malloc(dstPlaneSize)!.assumingMemoryBound(to: UInt8.self)
self.dstPlane = (dstPlane, dstPlaneSize)
}
fillDstPlane(dstPlane, frame.data[1]!, frame.data[2]!, srcPlaneSize)
let status = CVPixelBufferLockBaseAddress(pixelBuffer, [])
if status != kCVReturnSuccess {