Skip to content

Commit

Permalink
fix #615
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Nov 6, 2023
1 parent 4e28a73 commit 4032a8e
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 61 deletions.
21 changes: 0 additions & 21 deletions Demo/SwiftUI/Shared/MovieModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
24 changes: 1 addition & 23 deletions Sources/KSPlayer/AVPlayer/KSOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/KSPlayer/AVPlayer/MediaPlayerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion Sources/KSPlayer/MEPlayer/AVFoundationExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import FFmpegKit
import Libavutil

extension OSType {
func planeCount() -> UInt8 {
var planeCount: UInt8 {
switch self {
case
kCVPixelFormatType_48RGB,
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions Sources/KSPlayer/MEPlayer/FFmpegAssetTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -227,3 +230,10 @@ public class FFmpegAssetTrack: MediaPlayerTrack {
}
}
}

extension FFmpegAssetTrack {
var pixelFormatType: OSType? {
let format = AVPixelFormat(codecpar.format)
return format.osType(fullRange: fullRangeVideo)
}
}
4 changes: 2 additions & 2 deletions Sources/KSPlayer/MEPlayer/Resample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down
3 changes: 1 addition & 2 deletions Sources/KSPlayer/MEPlayer/VideoToolboxDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions Sources/KSPlayer/Metal/BufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down

0 comments on commit 4032a8e

Please sign in to comment.