-
-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
195 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,88 @@ | ||
import CoreMedia | ||
import Foundation | ||
|
||
enum HEVCNALUnitType: UInt8 { | ||
case unspec = 0 | ||
case codedSliceTrailN = 0 | ||
case codedSliceTrailR = 1 | ||
case codedSliceTsaN = 2 | ||
case codedSliceTsaR = 3 | ||
case codedSliceStsaN = 4 | ||
case codedSliceStsaR = 5 | ||
case codedSliceRadlN = 6 | ||
case codedSliceRadlR = 7 | ||
case codedSliceRaslN = 8 | ||
case codedSliceRsslR = 9 | ||
/// 10...15 Reserved | ||
case vps = 32 | ||
case sps = 33 | ||
case pps = 34 | ||
case accessUnitDelimiter = 35 | ||
case unspec = 0xFF | ||
} | ||
|
||
struct HEVCNALUnit: NALUnit, Equatable { | ||
let type: HEVCNALUnitType | ||
let temporalIdPlusOne: UInt8 | ||
let payload: Data | ||
|
||
init(_ data: Data) { | ||
self.init(data, length: data.count) | ||
} | ||
|
||
init(_ data: Data, length: Int) { | ||
self.type = HEVCNALUnitType(rawValue: (data[0] & 0x7e) >> 1) ?? .unspec | ||
self.temporalIdPlusOne = data[1] & 0b00011111 | ||
self.payload = data.subdata(in: 2..<length) | ||
} | ||
|
||
var data: Data { | ||
var result = Data() | ||
result.append(type.rawValue << 1) | ||
result.append(temporalIdPlusOne) | ||
result.append(payload) | ||
return result | ||
} | ||
} | ||
|
||
extension [HEVCNALUnit] { | ||
func makeFormatDescription(_ nalUnitHeaderLength: Int32 = 4) -> CMFormatDescription? { | ||
guard | ||
let vps = first(where: { $0.type == .vps }), | ||
let sps = first(where: { $0.type == .sps }), | ||
let pps = first(where: { $0.type == .pps }) else { | ||
return nil | ||
} | ||
return vps.data.withUnsafeBytes { (vpsBuffer: UnsafeRawBufferPointer) -> CMFormatDescription? in | ||
guard let vpsBaseAddress = vpsBuffer.baseAddress else { | ||
return nil | ||
} | ||
return sps.data.withUnsafeBytes { (spsBuffer: UnsafeRawBufferPointer) -> CMFormatDescription? in | ||
guard let spsBaseAddress = spsBuffer.baseAddress else { | ||
return nil | ||
} | ||
return pps.data.withUnsafeBytes { (ppsBuffer: UnsafeRawBufferPointer) -> CMFormatDescription? in | ||
guard let ppsBaseAddress = ppsBuffer.baseAddress else { | ||
return nil | ||
} | ||
var formatDescriptionOut: CMFormatDescription? | ||
let pointers: [UnsafePointer<UInt8>] = [ | ||
vpsBaseAddress.assumingMemoryBound(to: UInt8.self), | ||
spsBaseAddress.assumingMemoryBound(to: UInt8.self), | ||
ppsBaseAddress.assumingMemoryBound(to: UInt8.self) | ||
] | ||
let sizes: [Int] = [vpsBuffer.count, spsBuffer.count, ppsBuffer.count] | ||
let status = CMVideoFormatDescriptionCreateFromHEVCParameterSets( | ||
allocator: kCFAllocatorDefault, | ||
parameterSetCount: pointers.count, | ||
parameterSetPointers: pointers, | ||
parameterSetSizes: sizes, | ||
nalUnitHeaderLength: nalUnitHeaderLength, | ||
extensions: nil, | ||
formatDescriptionOut: &formatDescriptionOut | ||
) | ||
return formatDescriptionOut | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import CoreMedia | ||
import Foundation | ||
|
||
protocol NALUnit { | ||
init(_ data: Data) | ||
} | ||
|
||
final class NALUnitReader { | ||
static let defaultNALUnitHeaderLength: Int32 = 4 | ||
var nalUnitHeaderLength: Int32 = NALUnitReader.defaultNALUnitHeaderLength | ||
|
||
func read<T: NALUnit>(_ data: inout Data, type: T.Type) -> [T] { | ||
var units: [T] = .init() | ||
var lastIndexOf = data.count - 1 | ||
for i in (2..<data.count).reversed() { | ||
guard data[i] == 1 && data[i - 1] == 0 && data[i - 2] == 0 else { | ||
continue | ||
} | ||
let startCodeLength = 0 <= i - 3 && data[i - 3] == 0 ? 4 : 3 | ||
units.append(T.init(data.subdata(in: (i + 1)..<lastIndexOf + 1))) | ||
lastIndexOf = i - startCodeLength | ||
} | ||
return units | ||
} | ||
|
||
func makeFormatDescription(_ data: inout Data, type: ESStreamType) -> CMFormatDescription? { | ||
switch type { | ||
case .h264: | ||
let units = read(&data, type: AVCNALUnit.self) | ||
return units.makeFormatDescription(nalUnitHeaderLength) | ||
case .h265: | ||
let units = read(&data, type: HEVCNALUnit.self) | ||
return units.makeFormatDescription(nalUnitHeaderLength) | ||
default: | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.