Skip to content

Commit

Permalink
fix #621
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Nov 7, 2023
1 parent c4897c8 commit ea924e7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 24 deletions.
16 changes: 4 additions & 12 deletions Demo/SwiftUI/Shared/MovieModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,17 @@ class MEOptions: KSOptions {
#else
static var isUseDisplayLayer = false
#endif
static var yadifMode = 1
override init() {
super.init()
formatContextOptions["reconnect_on_network_error"] = 1
audioLocale = Locale(identifier: "en-US")
}

override func process(assetTrack: some MediaPlayerTrack) {
if assetTrack.mediaType == .video {
if [FFmpegFieldOrder.bb, .bt, .tt, .tb].contains(assetTrack.fieldOrder) {
// todo 先不要用yadif_videotoolbox,不然会crash。这个后续在看下要怎么解决
// videoFilters.append("yadif_videotoolbox=mode=\(MEOptions.yadifMode):parity=-1:deint=1")
videoFilters.append("yadif=mode=\(MEOptions.yadifMode):parity=-1:deint=1")
hardwareDecode = false
asynchronousDecompression = false
}
}
super.process(assetTrack: assetTrack)
}

override func updateVideo(refreshRate: Float, formatDescription: CMFormatDescription?) {
override func updateVideo(refreshRate: Float, isDovi: Bool, formatDescription: CMFormatDescription?) {
#if os(tvOS) || os(xrOS)
guard let displayManager = UIApplication.shared.windows.first?.avDisplayManager,
displayManager.isDisplayCriteriaMatchingEnabled,
Expand All @@ -49,7 +40,8 @@ class MEOptions: KSOptions {
if KSOptions.displayCriteriaFormatDescriptionEnabled, #available(tvOS 17.0, *) {
displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, formatDescription: formatDescription)
} else {
displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, videoDynamicRange: formatDescription.dynamicRange.rawValue)
let dynamicRange = isDovi ? .dolbyVision : formatDescription.dynamicRange
displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, videoDynamicRange: dynamicRange.rawValue)
}
}
#endif
Expand Down
17 changes: 14 additions & 3 deletions Sources/KSPlayer/AVPlayer/KSOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,19 @@ open class KSOptions {
/**
在创建解码器之前可以对KSOptions和assetTrack做一些处理。例如判断fieldOrder为tt或bb的话,那就自动加videofilters
*/
open func process(assetTrack _: some MediaPlayerTrack) {}
open func process(assetTrack: some MediaPlayerTrack) {
if assetTrack.mediaType == .video {
if [FFmpegFieldOrder.bb, .bt, .tt, .tb].contains(assetTrack.fieldOrder) {
// todo 先不要用yadif_videotoolbox,不然会crash。这个后续在看下要怎么解决
// videoFilters.append("yadif_videotoolbox=mode=\(KSOptions.yadifMode):parity=-1:deint=1")
videoFilters.append("yadif=mode=\(KSOptions.yadifMode):parity=-1:deint=1")
hardwareDecode = false
asynchronousDecompression = false
}
}
}

open func updateVideo(refreshRate: Float, formatDescription: CMFormatDescription?) {
open func updateVideo(refreshRate: Float, isDovi: Bool, formatDescription: CMFormatDescription?) {
#if os(tvOS) || os(xrOS)
guard let displayManager = UIApplication.shared.windows.first?.avDisplayManager,
displayManager.isDisplayCriteriaMatchingEnabled,
Expand All @@ -313,7 +323,8 @@ open class KSOptions {
if KSOptions.displayCriteriaFormatDescriptionEnabled, #available(tvOS 17.0, *) {
displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, formatDescription: formatDescription)
} else {
// displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, videoDynamicRange: formatDescription.dynamicRange.rawValue)
let dynamicRange = isDovi ? .dolbyVision : formatDescription.dynamicRange
// displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, videoDynamicRange: dynamicRange.rawValue)
}
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Sources/KSPlayer/MEPlayer/FFmpegDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FFmpegDecode: DecodeProtocol {
codecContext?.pointee.time_base = assetTrack.timebase.rational
filter = MEFilter(timebase: assetTrack.timebase, isAudio: assetTrack.mediaType == .audio, nominalFrameRate: assetTrack.nominalFrameRate, options: options)
if assetTrack.mediaType == .video {
swresample = VideoSwresample(fps: assetTrack.nominalFrameRate)
swresample = VideoSwresample(fps: assetTrack.nominalFrameRate, isDovi: assetTrack.dovi != nil)
} else {
swresample = AudioSwresample(audioDescriptor: assetTrack.audioDescriptor!)
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/KSPlayer/MEPlayer/MetalPlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public final class MetalPlayView: UIView, VideoOutput {
displayView.displayLayer
}

private var isDovi: Bool = false
private var formatDescription: CMFormatDescription? {
didSet {
options.updateVideo(refreshRate: fps, formatDescription: formatDescription)
options.updateVideo(refreshRate: fps, isDovi: isDovi, formatDescription: formatDescription)
}
}

Expand All @@ -48,7 +49,7 @@ public final class MetalPlayView: UIView, VideoOutput {
displayLink.preferredFrameRateRange = CAFrameRateRange(minimum: Float(preferredFramesPerSecond), maximum: Float(preferredFramesPerSecond << 1))
}
#endif
options.updateVideo(refreshRate: fps, formatDescription: formatDescription)
options.updateVideo(refreshRate: fps, isDovi: isDovi, formatDescription: formatDescription)
}
}
}
Expand Down Expand Up @@ -177,6 +178,7 @@ extension MetalPlayView {
guard let pixelBuffer else {
return
}
isDovi = frame.isDovi
fps = frame.fps
let cmtime = frame.cmtime
let par = pixelBuffer.size
Expand Down
7 changes: 5 additions & 2 deletions Sources/KSPlayer/MEPlayer/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public extension KSOptions {
/// true: AVSampleBufferAudioRenderer false: AVAudioEngine
static var audioPlayerType: AudioOutput.Type = AudioEnginePlayer.self
static var videoPlayerType: (VideoOutput & UIView).Type = MetalPlayView.self
static var yadifMode = 1
static func colorSpace(ycbcrMatrix: CFString?, transferFunction: CFString?) -> CGColorSpace? {
switch ycbcrMatrix {
case kCVImageBufferYCbCrMatrix_ITU_R_709_2:
Expand Down Expand Up @@ -357,10 +358,12 @@ public final class VideoVTBFrame: MEFrame {
public var duration: Int64 = 0
public var position: Int64 = 0
public var size: Int32 = 0
let fps: Float
public let fps: Float
public let isDovi: Bool
var corePixelBuffer: CVPixelBuffer?
init(fps: Float) {
init(fps: Float, isDovi: Bool) {
self.fps = fps
self.isDovi = isDovi
}
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/KSPlayer/MEPlayer/Resample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class VideoSwresample: Swresample {
private var pool: CVPixelBufferPool?
private let dstFormat: AVPixelFormat?
private let fps: Float
init(dstFormat: AVPixelFormat? = nil, fps: Float = 60) {
private let isDovi: Bool
init(dstFormat: AVPixelFormat? = nil, fps: Float = 60, isDovi: Bool) {
self.dstFormat = dstFormat
self.fps = fps
self.isDovi = isDovi
}

func transfer(avframe: UnsafeMutablePointer<AVFrame>) throws -> MEFrame {
let frame = VideoVTBFrame(fps: fps)
let frame = VideoVTBFrame(fps: fps, isDovi: isDovi)
if avframe.pointee.format == AV_PIX_FMT_VIDEOTOOLBOX.rawValue {
frame.corePixelBuffer = unsafeBitCast(avframe.pointee.data.3, to: CVPixelBuffer.self)
} else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/KSPlayer/MEPlayer/SubtitleDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import AppKit
#endif
class SubtitleDecode: DecodeProtocol {
private var codecContext: UnsafeMutablePointer<AVCodecContext>?
private let scale = VideoSwresample(dstFormat: AV_PIX_FMT_ARGB)
private let scale = VideoSwresample(dstFormat: AV_PIX_FMT_ARGB, isDovi: false)
private var subtitle = AVSubtitle()
private var startTime = TimeInterval(0)
private var preSubtitleFrame: SubtitleFrame?
Expand Down
2 changes: 1 addition & 1 deletion Sources/KSPlayer/MEPlayer/VideoToolboxDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class VideoToolboxDecode: DecodeProtocol {
}
return
}
let frame = VideoVTBFrame(fps: session.assetTrack.nominalFrameRate)
let frame = VideoVTBFrame(fps: session.assetTrack.nominalFrameRate, isDovi: session.assetTrack.dovi != nil)
frame.corePixelBuffer = imageBuffer
frame.timebase = session.assetTrack.timebase
if packet.isKeyFrame, packetFlags & AV_PKT_FLAG_DISCARD != 0, self.lastPosition > 0 {
Expand Down

0 comments on commit ea924e7

Please sign in to comment.