Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Dec 4, 2023
1 parent e8eb3b7 commit f572bc1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Sources/KSPlayer/AVPlayer/KSOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ open class KSOptions {
return availableHDRModes.dynamicRange
}
}
#else
return destinationDynamicRange ?? cotentRange
#endif
return cotentRange
}
Expand Down
36 changes: 19 additions & 17 deletions Sources/KSPlayer/MEPlayer/Resample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 12 additions & 19 deletions Sources/KSPlayer/Metal/PixelBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -86,7 +85,7 @@ extension CVPixelBuffer: PixelBufferProtocol {
}
}

var colorPrimaries: CFString? {
public var colorPrimaries: CFString? {
get {
CVBufferGetAttachment(self, kCVImageBufferColorPrimariesKey, nil)?.takeUnretainedValue() as? NSString
}
Expand All @@ -97,7 +96,7 @@ extension CVPixelBuffer: PixelBufferProtocol {
}
}

var transferFunction: CFString? {
public var transferFunction: CFString? {
get {
CVBufferGetAttachment(self, kCVImageBufferTransferFunctionKey, nil)?.takeUnretainedValue() as? NSString
}
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit f572bc1

Please sign in to comment.