From f572bc1c76e962fca159db36096036702f493341 Mon Sep 17 00:00:00 2001 From: kingslay Date: Mon, 4 Dec 2023 22:07:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/KSPlayer/AVPlayer/KSOptions.swift | 2 ++ Sources/KSPlayer/MEPlayer/Resample.swift | 36 ++++++++++--------- .../KSPlayer/Metal/PixelBufferProtocol.swift | 31 +++++++--------- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/Sources/KSPlayer/AVPlayer/KSOptions.swift b/Sources/KSPlayer/AVPlayer/KSOptions.swift index 75da74549..00a9aa317 100644 --- a/Sources/KSPlayer/AVPlayer/KSOptions.swift +++ b/Sources/KSPlayer/AVPlayer/KSOptions.swift @@ -402,6 +402,8 @@ open class KSOptions { return availableHDRModes.dynamicRange } } + #else + return destinationDynamicRange ?? cotentRange #endif return cotentRange } diff --git a/Sources/KSPlayer/MEPlayer/Resample.swift b/Sources/KSPlayer/MEPlayer/Resample.swift index a3fdcd6f0..a7cb0554a 100644 --- a/Sources/KSPlayer/MEPlayer/Resample.swift +++ b/Sources/KSPlayer/MEPlayer/Resample.swift @@ -86,22 +86,25 @@ class VideoSwresample: FrameChange { } else { frame.corePixelBuffer = transfer(frame: avframe.pointee) } -// if let sideData = avframe.pointee.side_data?.pointee?.pointee { -// if sideData.type == AV_FRAME_DATA_DOVI_RPU_BUFFER { -// let rpuBuff = sideData.data.withMemoryRebound(to: [UInt8].self, capacity: 1) { $0 } -// } else if sideData.type == AV_FRAME_DATA_DOVI_METADATA { // AVDOVIMetadata -// let doviMeta = sideData.data.withMemoryRebound(to: AVDOVIMetadata.self, capacity: 1) { $0 } -// let header = av_dovi_get_header(doviMeta) -// let mapping = av_dovi_get_mapping(doviMeta) -// let color = av_dovi_get_color(doviMeta) -// -// } else if sideData.type == AV_FRAME_DATA_DYNAMIC_HDR_PLUS { // AVDynamicHDRPlus -// let hdrPlus = sideData.data.withMemoryRebound(to: AVDynamicHDRPlus.self, capacity: 1) { $0 }.pointee -// -// } else if sideData.type == AV_FRAME_DATA_DYNAMIC_HDR_VIVID { // AVDynamicHDRVivid -// let hdrVivid = sideData.data.withMemoryRebound(to: AVDynamicHDRVivid.self, capacity: 1) { $0 }.pointee -// } -// } + for i in 0 ..< avframe.pointee.nb_side_data { + if let sideData = avframe.pointee.side_data[Int(i)]?.pointee { + if sideData.type == AV_FRAME_DATA_DOVI_RPU_BUFFER { + let rpuBuff = sideData.data.withMemoryRebound(to: [UInt8].self, capacity: 1) { $0 } + } else if sideData.type == AV_FRAME_DATA_DOVI_METADATA { // AVDOVIMetadata + let doviMeta = sideData.data.withMemoryRebound(to: AVDOVIMetadata.self, capacity: 1) { $0 } + let header = av_dovi_get_header(doviMeta) + let mapping = av_dovi_get_mapping(doviMeta) + let color = av_dovi_get_color(doviMeta) + frame.corePixelBuffer?.transferFunction = kCVImageBufferTransferFunction_ITU_R_2020 + } else if sideData.type == AV_FRAME_DATA_DYNAMIC_HDR_PLUS { // AVDynamicHDRPlus + let hdrPlus = sideData.data.withMemoryRebound(to: AVDynamicHDRPlus.self, capacity: 1) { $0 }.pointee + + } else if sideData.type == AV_FRAME_DATA_DYNAMIC_HDR_VIVID { // AVDynamicHDRVivid + let hdrVivid = sideData.data.withMemoryRebound(to: AVDynamicHDRVivid.self, capacity: 1) { $0 }.pointee + } + } + } + frame.corePixelBuffer?.colorspace = KSOptions.colorSpace(ycbcrMatrix: frame.corePixelBuffer?.yCbCrMatrix, transferFunction: frame.corePixelBuffer?.transferFunction) return frame } @@ -147,7 +150,6 @@ class VideoSwresample: FrameChange { if let chroma = frame.chroma_location.chroma { CVBufferSetAttachment(pbuf, kCVImageBufferChromaLocationTopFieldKey, chroma, .shouldPropagate) } - pbuf.colorspace = KSOptions.colorSpace(ycbcrMatrix: pbuf.yCbCrMatrix, transferFunction: pbuf.transferFunction) } return pbuf } diff --git a/Sources/KSPlayer/Metal/PixelBufferProtocol.swift b/Sources/KSPlayer/Metal/PixelBufferProtocol.swift index ea1e214a8..5d49424c4 100644 --- a/Sources/KSPlayer/Metal/PixelBufferProtocol.swift +++ b/Sources/KSPlayer/Metal/PixelBufferProtocol.swift @@ -16,17 +16,16 @@ import UIKit #endif public protocol PixelBufferProtocol: AnyObject { - var aspectRatio: CGSize { get set } - var planeCount: Int { get } var width: Int { get } var height: Int { get } var bitDepth: Int32 { get } var leftShift: UInt8 { get } -// var colorPrimaries: CFString? { get } -// var transferFunction: CFString? { get } - var yCbCrMatrix: CFString? { get } - var colorspace: CGColorSpace? { get } - var attachmentsDic: CFDictionary? { get } + var planeCount: Int { get } + var aspectRatio: CGSize { get set } + var yCbCrMatrix: CFString? { get set } + var colorPrimaries: CFString? { get set } + var transferFunction: CFString? { get set } + var colorspace: CGColorSpace? { get set } var cvPixelBuffer: CVPixelBuffer? { get } var isFullRangeVideo: Bool { get } func cgImage() -> CGImage? @@ -86,7 +85,7 @@ extension CVPixelBuffer: PixelBufferProtocol { } } - var colorPrimaries: CFString? { + public var colorPrimaries: CFString? { get { CVBufferGetAttachment(self, kCVImageBufferColorPrimariesKey, nil)?.takeUnretainedValue() as? NSString } @@ -97,7 +96,7 @@ extension CVPixelBuffer: PixelBufferProtocol { } } - var transferFunction: CFString? { + public var transferFunction: CFString? { get { CVBufferGetAttachment(self, kCVImageBufferTransferFunctionKey, nil)?.takeUnretainedValue() as? NSString } @@ -159,11 +158,10 @@ class PixelBuffer: PixelBufferProtocol { let leftShift: UInt8 let isFullRangeVideo: Bool var cvPixelBuffer: CVPixelBuffer? { nil } - var attachmentsDic: CFDictionary? - let colorPrimaries: CFString? - let transferFunction: CFString? - let yCbCrMatrix: CFString? - let colorspace: CGColorSpace? + var colorPrimaries: CFString? + var transferFunction: CFString? + var yCbCrMatrix: CFString? + var colorspace: CGColorSpace? private let format: AVPixelFormat private let formats: [MTLPixelFormat] private let widths: [Int] @@ -176,11 +174,6 @@ class PixelBuffer: PixelBufferProtocol { colorPrimaries = frame.color_primaries.colorPrimaries transferFunction = frame.color_trc.transferFunction colorspace = KSOptions.colorSpace(ycbcrMatrix: yCbCrMatrix, transferFunction: transferFunction) - var attachments = [CFString: CFString]() - attachments[kCVImageBufferColorPrimariesKey] = colorPrimaries - attachments[kCVImageBufferTransferFunctionKey] = transferFunction - attachments[kCVImageBufferYCbCrMatrixKey] = yCbCrMatrix - attachmentsDic = attachments as CFDictionary width = Int(frame.width) height = Int(frame.height) isFullRangeVideo = frame.color_range == AVCOL_RANGE_JPEG