diff --git a/Demo/SwiftUI/Shared/MovieModel.swift b/Demo/SwiftUI/Shared/MovieModel.swift index 5e4763235..b912fe45a 100644 --- a/Demo/SwiftUI/Shared/MovieModel.swift +++ b/Demo/SwiftUI/Shared/MovieModel.swift @@ -34,27 +34,6 @@ class MEOptions: KSOptions { hardwareDecode = false asynchronousDecompression = false } - #if os(tvOS) || os(xrOS) - runInMainqueue { [weak self] in - guard let self else { - return - } - if let displayManager = UIApplication.shared.windows.first?.avDisplayManager, - displayManager.isDisplayCriteriaMatchingEnabled, - !displayManager.isDisplayModeSwitchInProgress - { - let refreshRate = assetTrack.nominalFrameRate - if KSOptions.displayCriteriaFormatDescriptionEnabled, let formatDescription = assetTrack.formatDescription, #available(tvOS 17.0, *) { - displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, formatDescription: formatDescription) - } else { - if let dynamicRange = assetTrack.dynamicRange { - let videoDynamicRange = self.availableDynamicRange(dynamicRange) ?? dynamicRange - displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, videoDynamicRange: videoDynamicRange.rawValue) - } - } - } - } - #endif } } diff --git a/Sources/KSPlayer/AVPlayer/KSOptions.swift b/Sources/KSPlayer/AVPlayer/KSOptions.swift index 41334d89a..80594ea6b 100644 --- a/Sources/KSPlayer/AVPlayer/KSOptions.swift +++ b/Sources/KSPlayer/AVPlayer/KSOptions.swift @@ -299,29 +299,7 @@ open class KSOptions { /** 在创建解码器之前可以对KSOptions和assetTrack做一些处理。例如判断fieldOrder为tt或bb的话,那就自动加videofilters */ - open func process(assetTrack: some MediaPlayerTrack) { - if assetTrack.mediaType == .video { - #if os(tvOS) || os(xrOS) - runInMainqueue { - if let displayManager = UIApplication.shared.windows.first?.avDisplayManager, - displayManager.isDisplayCriteriaMatchingEnabled, - !displayManager.isDisplayModeSwitchInProgress - { - let refreshRate = assetTrack.nominalFrameRate - if KSOptions.displayCriteriaFormatDescriptionEnabled, let formatDescription = assetTrack.formatDescription, #available(tvOS 17.0, *) { - displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, formatDescription: formatDescription) - } else { - // if let dynamicRange = assetTrack.dynamicRange { - // let videoDynamicRange = availableDynamicRange(dynamicRange) ?? dynamicRange - // displayManager.preferredDisplayCriteria = AVDisplayCriteria(refreshRate: refreshRate, videoDynamicRange: videoDynamicRange.rawValue) - // } - } - } - } - - #endif - } - } + open func process(assetTrack _: some MediaPlayerTrack) {} open func updateVideo(refreshRate: Float, formatDescription: CMFormatDescription?) { #if os(tvOS) || os(xrOS) diff --git a/Sources/KSPlayer/AVPlayer/MediaPlayerProtocol.swift b/Sources/KSPlayer/AVPlayer/MediaPlayerProtocol.swift index 0a0ed68ee..8e7910415 100644 --- a/Sources/KSPlayer/AVPlayer/MediaPlayerProtocol.swift +++ b/Sources/KSPlayer/AVPlayer/MediaPlayerProtocol.swift @@ -227,7 +227,7 @@ public extension CMFormatDescription { let cotentRange: DynamicRange if codecType.string == "dvhe" || codecType == kCMVideoCodecType_DolbyVisionHEVC { cotentRange = .dolbyVision - } else if transferFunction == kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ as String { /// HDR + } else if codecType.bitDepth == 10 || transferFunction == kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ as String { /// HDR cotentRange = .hdr10 } else if transferFunction == kCVImageBufferTransferFunction_ITU_R_2100_HLG as String { /// HLG cotentRange = .hlg diff --git a/Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift b/Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift index 6010c4555..69fbd4ac7 100644 --- a/Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift +++ b/Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift @@ -221,12 +221,12 @@ extension AVChromaLocation { } extension AVPixelFormat { - func bitDepth() -> Int32 { + var bitDepth: Int32 { let descriptor = av_pix_fmt_desc_get(self) return descriptor?.pointee.comp.0.depth ?? 8 } - func planeCount() -> UInt8 { + var planeCount: UInt8 { if let desc = av_pix_fmt_desc_get(self) { switch desc.pointee.nb_components { case 3: diff --git a/Sources/KSPlayer/MEPlayer/AVFoundationExtension.swift b/Sources/KSPlayer/MEPlayer/AVFoundationExtension.swift index 5aa7b09d5..1dd64b634 100644 --- a/Sources/KSPlayer/MEPlayer/AVFoundationExtension.swift +++ b/Sources/KSPlayer/MEPlayer/AVFoundationExtension.swift @@ -11,7 +11,7 @@ import FFmpegKit import Libavutil extension OSType { - func planeCount() -> UInt8 { + var planeCount: UInt8 { switch self { case kCVPixelFormatType_48RGB, @@ -43,6 +43,15 @@ extension OSType { default: return 2 } } + + var bitDepth: Int32 { + switch self { + case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange, kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange, kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange, kCVPixelFormatType_420YpCbCr10BiPlanarFullRange, kCVPixelFormatType_422YpCbCr10BiPlanarFullRange, kCVPixelFormatType_444YpCbCr10BiPlanarFullRange: + return 10 + default: + return 8 + } + } } extension CVPixelBufferPool { diff --git a/Sources/KSPlayer/MEPlayer/FFmpegAssetTrack.swift b/Sources/KSPlayer/MEPlayer/FFmpegAssetTrack.swift index b74c6e531..47dd3ed45 100644 --- a/Sources/KSPlayer/MEPlayer/FFmpegAssetTrack.swift +++ b/Sources/KSPlayer/MEPlayer/FFmpegAssetTrack.swift @@ -100,7 +100,6 @@ public class FFmpegAssetTrack: MediaPlayerTrack { init?(codecpar: AVCodecParameters) { self.codecpar = codecpar - let format = AVPixelFormat(rawValue: codecpar.format) bitRate = codecpar.bit_rate // codec_tag byte order is LSB first CMFormatDescription.MediaSubType(rawValue: codecpar.codec_tag.bigEndian) let codecType = codecpar.codec_id.mediaSubType @@ -169,16 +168,20 @@ public class FFmpegAssetTrack: MediaPlayerTrack { } isConvertNALSize = false } + let format = AVPixelFormat(rawValue: codecpar.format) + let fullRange = codecpar.color_range == AVCOL_RANGE_JPEG let dic: NSMutableDictionary = [ kCVImageBufferChromaLocationBottomFieldKey: kCVImageBufferChromaLocation_Left, kCVImageBufferChromaLocationTopFieldKey: kCVImageBufferChromaLocation_Left, - kCMFormatDescriptionExtension_Depth: format.bitDepth() * Int32(format.planeCount()), - kCMFormatDescriptionExtension_FullRangeVideo: codecpar.color_range == AVCOL_RANGE_JPEG, + kCMFormatDescriptionExtension_Depth: format.bitDepth * Int32(format.planeCount), + kCMFormatDescriptionExtension_FullRangeVideo: fullRange, codecType.rawValue == kCMVideoCodecType_HEVC ? "EnableHardwareAcceleratedVideoDecoder" : "RequireHardwareAcceleratedVideoDecoder": true, ] + // kCMFormatDescriptionExtension_BitsPerComponent if let atomsData { dic[kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms] = [codecType.rawValue.avc: atomsData] } + dic[kCVPixelBufferPixelFormatTypeKey] = format.osType(fullRange: fullRange) dic[kCVImageBufferPixelAspectRatioKey] = sar.aspectRatio dic[kCVImageBufferColorPrimariesKey] = codecpar.color_primaries.colorPrimaries as String? dic[kCVImageBufferTransferFunctionKey] = codecpar.color_trc.transferFunction as String? @@ -227,3 +230,10 @@ public class FFmpegAssetTrack: MediaPlayerTrack { } } } + +extension FFmpegAssetTrack { + var pixelFormatType: OSType? { + let format = AVPixelFormat(codecpar.format) + return format.osType(fullRange: fullRangeVideo) + } +} diff --git a/Sources/KSPlayer/MEPlayer/Resample.swift b/Sources/KSPlayer/MEPlayer/Resample.swift index 6fb9afb17..2458607bf 100644 --- a/Sources/KSPlayer/MEPlayer/Resample.swift +++ b/Sources/KSPlayer/MEPlayer/Resample.swift @@ -64,7 +64,7 @@ class VideoSwresample: Swresample { self.height = height self.width = width let pixelFormatType: OSType - if let osType = format.osType(), osType.planeCount() == format.planeCount(), format.bitDepth() <= 8 { + if let osType = format.osType(), osType.planeCount == format.planeCount, format.bitDepth <= 8 { pixelFormatType = osType sws_freeContext(imgConvertCtx) imgConvertCtx = nil @@ -123,7 +123,7 @@ class VideoSwresample: Swresample { } _ = sws_scale(imgConvertCtx, data.map { UnsafePointer($0) }, linesize, 0, height, contents, bytesPerRow) } else { - let planeCount = format.planeCount() + let planeCount = format.planeCount for i in 0 ..< bufferPlaneCount { let height = pbuf.heightOfPlane(at: i) let size = Int(linesize[i]) diff --git a/Sources/KSPlayer/MEPlayer/VideoToolboxDecode.swift b/Sources/KSPlayer/MEPlayer/VideoToolboxDecode.swift index b8d7b40f5..83c773d60 100644 --- a/Sources/KSPlayer/MEPlayer/VideoToolboxDecode.swift +++ b/Sources/KSPlayer/MEPlayer/VideoToolboxDecode.swift @@ -106,8 +106,7 @@ class DecompressionSession { fileprivate var assetTrack: FFmpegAssetTrack init?(assetTrack: FFmpegAssetTrack, options: KSOptions) { self.assetTrack = assetTrack - let format = AVPixelFormat(assetTrack.codecpar.format) - guard let pixelFormatType = format.osType(fullRange: assetTrack.fullRangeVideo), let formatDescription = assetTrack.formatDescription else { + guard let pixelFormatType = assetTrack.pixelFormatType, let formatDescription = assetTrack.formatDescription else { return nil } self.formatDescription = formatDescription diff --git a/Sources/KSPlayer/Metal/BufferProtocol.swift b/Sources/KSPlayer/Metal/BufferProtocol.swift index c24006519..99549b363 100644 --- a/Sources/KSPlayer/Metal/BufferProtocol.swift +++ b/Sources/KSPlayer/Metal/BufferProtocol.swift @@ -98,12 +98,7 @@ public extension CVPixelBuffer { } var bitDepth: Int32 { - switch CVPixelBufferGetPixelFormatType(self) { - case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange, kCVPixelFormatType_422YpCbCr10BiPlanarVideoRange, kCVPixelFormatType_444YpCbCr10BiPlanarVideoRange, kCVPixelFormatType_420YpCbCr10BiPlanarFullRange, kCVPixelFormatType_422YpCbCr10BiPlanarFullRange, kCVPixelFormatType_444YpCbCr10BiPlanarFullRange: - return 10 - default: - return 8 - } + CVPixelBufferGetPixelFormatType(self).bitDepth } func cgImage() -> CGImage? {