Skip to content

Commit

Permalink
PixelBuffer也返回formatDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Dec 7, 2023
1 parent 5d4f93b commit 5ea90f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Sources/KSPlayer/MEPlayer/FFmpegDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,16 @@ class FFmpegDecode: DecodeProtocol {
filter.filter(options: options, inputFrame: avframe) { avframe in
do {
var frame = try frameChange.change(avframe: avframe)
if let videoFrame = frame as? VideoVTBFrame, let pixelBuffer = videoFrame.corePixelBuffer as? PixelBuffer {
pixelBuffer.formatDescription = packet.assetTrack.formatDescription
}
frame.timebase = filter.timebase
// frame.timebase = Timebase(avframe.pointee.time_base)
frame.size = avframe.pointee.pkt_size
frame.duration = avframe.pointee.duration
if frame.duration == 0, avframe.pointee.sample_rate != 0, frame.timebase.num != 0 {
frame.duration = Int64(avframe.pointee.nb_samples) * Int64(frame.timebase.den) / (Int64(avframe.pointee.sample_rate) * Int64(frame.timebase.num))
}

var position = avframe.pointee.best_effort_timestamp
if position < 0 {
position = avframe.pointee.pts
Expand Down
10 changes: 2 additions & 8 deletions Sources/KSPlayer/MEPlayer/MetalPlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,13 @@ extension MetalPlayView {
}

private func checkFormatDescription(pixelBuffer: PixelBufferProtocol) {
guard let pixelBuffer = pixelBuffer.cvPixelBuffer else {
return
}
if formatDescription == nil || !CMVideoFormatDescriptionMatchesImageBuffer(formatDescription!, imageBuffer: pixelBuffer) {
if formatDescription == nil || !pixelBuffer.matche(formatDescription: formatDescription!) {
if formatDescription != nil {
displayView.removeFromSuperview()
displayView = AVSampleBufferDisplayView()
addSubview(displayView)
}
let err = CMVideoFormatDescriptionCreateForImageBuffer(allocator: nil, imageBuffer: pixelBuffer, formatDescriptionOut: &formatDescription)
if err != noErr {
KSLog("Error at CMVideoFormatDescriptionCreateForImageBuffer \(err)")
}
formatDescription = pixelBuffer.formatDescription
}
}

Expand Down
19 changes: 19 additions & 0 deletions Sources/KSPlayer/Metal/PixelBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public protocol PixelBufferProtocol: AnyObject {
var bitDepth: Int32 { get }
var leftShift: UInt8 { get }
var planeCount: Int { get }
var formatDescription: CMVideoFormatDescription? { get }
var aspectRatio: CGSize { get set }
var yCbCrMatrix: CFString? { get set }
var colorPrimaries: CFString? { get set }
Expand All @@ -32,6 +33,7 @@ public protocol PixelBufferProtocol: AnyObject {
func textures() -> [MTLTexture]
func widthOfPlane(at planeIndex: Int) -> Int
func heightOfPlane(at planeIndex: Int) -> Int
func matche(formatDescription: CMVideoFormatDescription) -> Bool
}

extension PixelBufferProtocol {
Expand Down Expand Up @@ -65,6 +67,14 @@ extension CVPixelBuffer: PixelBufferProtocol {
var isPlanar: Bool { CVPixelBufferIsPlanar(self) }

public var planeCount: Int { isPlanar ? CVPixelBufferGetPlaneCount(self) : 1 }
public var formatDescription: CMVideoFormatDescription? {
var formatDescription: CMVideoFormatDescription?
let err = CMVideoFormatDescriptionCreateForImageBuffer(allocator: nil, imageBuffer: self, formatDescriptionOut: &formatDescription)
if err != noErr {
KSLog("Error at CMVideoFormatDescriptionCreateForImageBuffer \(err)")
}
return formatDescription
}

public var isFullRangeVideo: Bool {
CVBufferGetAttachment(self, kCMFormatDescriptionExtension_FullRangeVideo, nil)?.takeUnretainedValue() as? Bool ?? false
Expand Down Expand Up @@ -147,6 +157,10 @@ extension CVPixelBuffer: PixelBufferProtocol {
public func textures() -> [MTLTexture] {
MetalRender.texture(pixelBuffer: self)
}

public func matche(formatDescription: CMVideoFormatDescription) -> Bool {
CMVideoFormatDescriptionMatchesImageBuffer(formatDescription, imageBuffer: self)
}
}

class PixelBuffer: PixelBufferProtocol {
Expand All @@ -162,6 +176,7 @@ class PixelBuffer: PixelBufferProtocol {
var transferFunction: CFString?
var yCbCrMatrix: CFString?
var colorspace: CGColorSpace?
var formatDescription: CMVideoFormatDescription? = nil
private let format: AVPixelFormat
private let formats: [MTLPixelFormat]
private let widths: [Int]
Expand Down Expand Up @@ -241,6 +256,10 @@ class PixelBuffer: PixelBufferProtocol {
}
return image
}

public func matche(formatDescription: CMVideoFormatDescription) -> Bool {
self.formatDescription == formatDescription
}
}

extension CGSize {
Expand Down

0 comments on commit 5ea90f6

Please sign in to comment.