diff --git a/MP42/MP42Track+Private.h b/MP42/MP42Track+Private.h index 275a48a..0e18479 100644 --- a/MP42/MP42Track+Private.h +++ b/MP42/MP42Track+Private.h @@ -26,6 +26,8 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readwrite) MP42CodecType format; @property(nonatomic, readwrite) MP42MediaType mediaType; +@property(nonatomic, readwrite) NSArray *sampleDescriptions; + @property(nonatomic, readwrite) uint32_t timescale; @property(nonatomic, readwrite) MP42Duration duration; @property(nonatomic, readwrite) uint32_t bitrate; diff --git a/MP42/MP42Track.h b/MP42/MP42Track.h index 66b1da8..334d59e 100644 --- a/MP42/MP42Track.h +++ b/MP42/MP42Track.h @@ -12,6 +12,7 @@ #import @class MP42SampleBuffer; +@class MP42SampleDescription; NS_ASSUME_NONNULL_BEGIN @@ -23,8 +24,11 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readonly) MP42TrackId trackId; @property(nonatomic, readonly, copy, nullable) NSURL *URL; -@property(nonatomic, readonly) MP42CodecType format; + +@property(nonatomic, readonly) NSArray *sampleDescriptions; + @property(nonatomic, readonly) MP42MediaType mediaType; +@property(nonatomic, readonly) MP42CodecType format; @property(nonatomic, readonly) uint32_t timescale; @property(nonatomic, readonly) MP42Duration duration; diff --git a/MP42/MP42Track.m b/MP42/MP42Track.m index c9a5920..53c4412 100644 --- a/MP42/MP42Track.m +++ b/MP42/MP42Track.m @@ -37,6 +37,8 @@ typedef struct muxer_helper { @property(nonatomic, readwrite) MP42CodecType format; @property(nonatomic, readwrite) MP42MediaType mediaType; +@property(nonatomic, readwrite) NSArray *sampleDescriptions; + @property(nonatomic, readwrite) uint32_t timescale; @property(nonatomic, readwrite) MP42Duration duration; @property(nonatomic, readwrite) uint64_t dataLength; @@ -144,11 +146,13 @@ typedef struct muxer_helper { return self; } -- (NSString *)description { +- (NSString *)description +{ return [NSString stringWithFormat:@"Track: %d, %@, %@, %llu kbit/s, %@", self.trackId, self.name, self.timeString, self.dataLength / self.duration * 8, localizedDisplayName(self.mediaType, self.format)]; } -- (void)dealloc { +- (void)dealloc +{ [self free_muxer_helper]; } diff --git a/MP42/MP42VideoTrack.m b/MP42/MP42VideoTrack.m index 4ce5838..9b6cfaf 100644 --- a/MP42/MP42VideoTrack.m +++ b/MP42/MP42VideoTrack.m @@ -10,6 +10,7 @@ #import "MP42Track+Private.h" #import "MP42MediaFormat.h" #import "MP42PrivateUtilities.h" +#import "MP42-Shared-Swift.h" #import MP42_OBJC_DIRECT_MEMBERS @@ -46,6 +47,16 @@ MP42_OBJC_DIRECT_MEMBERS _transform.ty = CFSwapInt32BigToHost(ptr32[7]) / 0x10000; free(val); + // Sample descriptions + uint32_t count = MP4GetTrackNumberOfSampleDescriptions(fileHandle, self.trackId); + + NSMutableArray *descriptions = [NSMutableArray array]; + for (uint32_t index = 0; index < count; index++) { + MP42VideoSampleDescription *description = [[MP42VideoSampleDescription alloc] initWithFileHandle:fileHandle trackId:self.trackId index:index]; + [descriptions addObject:description]; + } + self.sampleDescriptions = [descriptions copy]; + if (MP4HaveTrackAtom(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.pasp")) { MP4GetTrackIntegerProperty(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.pasp.hSpacing", &_hSpacing); MP4GetTrackIntegerProperty(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.pasp.vSpacing", &_vSpacing); diff --git a/MP42/SampleDescription.swift b/MP42/SampleDescription.swift new file mode 100644 index 0000000..2620f34 --- /dev/null +++ b/MP42/SampleDescription.swift @@ -0,0 +1,282 @@ +// +// SamplerDescription.swift +// MP42Foundation +// +// Created by Damiano Galassi on 12/12/21. +// Copyright © 2021 Damiano Galassi. All rights reserved. +// + +import Foundation +import mp4v2 + +@objc(MP42SampleDescription) public class SampleDescription: NSObject { + let format: MP42CodecType + + @objc public init(fileHandle: MP42FileHandle, trackId: MP42TrackId, index: UInt32) { + let type = String(cString: MP4GetTrackType(fileHandle, trackId)) + let mediaDataName = String(cString: MP4GetTrackMediaDataName(fileHandle, trackId, index)) + + switch (type, mediaDataName) { + case (_, "twos"): + format = kMP42AudioCodecType_LinearPCM + case (_, "mp4a"): + let typeId = Int32(MP4GetTrackEsdsObjectTypeId(fileHandle, trackId)) + switch typeId { + case MP4_MPEG4_AUDIO_TYPE: + format = kMP42AudioCodecType_MPEG4AAC + case MP4_MPEG2_AUDIO_TYPE, MP4_MPEG1_AUDIO_TYPE: + format = kMP42AudioCodecType_MPEGLayer3 + case 0xA9: + format = kMP42AudioCodecType_DTS + default: + format = kMP42MediaType_Unknown + } + case ("subp", "mp4s"): + format = kMP42SubtitleCodecType_VobSub + case (_, _): + format = FourCharCode(mediaDataName) + } + } +} + +struct Size { + var width: UInt16 + var height: UInt16 +} + +struct ColorInfo { + var colorPrimaries: UInt16 + var transferCharacteristics: UInt16 + var matrixCoefficients: UInt16 + var colorRange: UInt16 +} + +struct PixelAspectRatio { + var hSpacing: UInt64 + var vSpacing: UInt64 +} + +struct CleanAperture { + var widthN: UInt64 + var widthD: UInt64 + var heightN: UInt64 + var heightD: UInt64 + var horizOffN: UInt64 + var horizOffD: UInt64 + var vertOffN: UInt64 + var vertOffD: UInt64 +} + +struct H264Profile { + var origProfile: UInt8 + var origLevel: UInt8 + var newProfile: UInt8 + var newLevel: UInt8 +} + +@objc(MP42VideoSampleDescription) public class VideoSampleDescription: SampleDescription { + var size: Size + var color: ColorInfo? + var mastering: MP42MasteringDisplayMetadata? + var coll: MP42ContentLightMetadata? + var dolbyVision: MP42DolbyVisionMetadata? + var pasp: PixelAspectRatio + var cleanAperture: CleanAperture? + var profile: H264Profile? + + @objc public override init(fileHandle: MP42FileHandle, trackId: MP42TrackId, index: UInt32) { + let width = MP4GetTrackVideoWidth(fileHandle, trackId) + let height = MP4GetTrackVideoHeight(fileHandle, trackId) + + self.size = Size(width: width, height: height) + + // Color Information + +// if MP4HaveTrackAtom(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].colr") { +// var colorPrimaries: UInt64 = 0, transferCharacteristics: UInt64 = 0, matrixCoefficients: UInt64 = 0, colorRange: UInt = 0 +// +// var atomType = "nclc" +// //if (MP4GetTrackStringProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.*.colr.colorParameterType", &atomType)) +// +// self.color = ColorInfo(colorPrimaries: UInt16(colorPrimaries), transferCharacteristics: UInt16(transferCharacteristics), matrixCoefficients: UInt16(matrixCoefficients), colorRange: UInt16(colorRange)) +// } + + // Pixel aspect ratio + + var hSpacing: UInt64 = 1, vSpacing: UInt64 = 1 + + if MP4HaveTrackAtom(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].pasp") { + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].pasp.hSpacing", &hSpacing) + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].pasp.vSpacing", &vSpacing) + } + + self.pasp = PixelAspectRatio(hSpacing: hSpacing, vSpacing: vSpacing) + + // Content light level + + if MP4HaveTrackAtom(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clli") { + var maxCLL: UInt64 = 0, maxFALL: UInt64 = 0 + + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clli.maxContentLightLevel", &maxCLL) + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clli.maxPicAverageLightLevel", &maxFALL) + + self.coll = MP42ContentLightMetadata(MaxCLL: UInt32(maxCLL), MaxFALL: UInt32(maxFALL)) + } + + // Mastering display metadata + + if MP4HaveTrackAtom(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv") { + var displayPrimariesGX: UInt64 = 0, displayPrimariesGY: UInt64 = 0, displayPrimariesBX: UInt64 = 0, + displayPrimariesBY: UInt64 = 0, displayPrimariesRX: UInt64 = 0, displayPrimariesRY: UInt64 = 0, + whitePointX: UInt64 = 0, whitePointY: UInt64 = 0, + maxDisplayMasteringLuminance: UInt64 = 0, minDisplayMasteringLuminance: UInt64 = 0; + + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.displayPrimariesGX", &displayPrimariesGX); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.displayPrimariesGY", &displayPrimariesGY); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.displayPrimariesBX", &displayPrimariesBX); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.displayPrimariesBY", &displayPrimariesBY); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.displayPrimariesRX", &displayPrimariesRX); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.displayPrimariesRY", &displayPrimariesRY); + + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.whitePointX", &whitePointX); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.whitePointY", &whitePointY); + + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.maxDisplayMasteringLuminance", &maxDisplayMasteringLuminance); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].mdcv.minDisplayMasteringLuminance", &minDisplayMasteringLuminance); + + let chromaDen: Int32 = 50000, lumaDen: Int32 = 10000; + + self.mastering = MP42MasteringDisplayMetadata(display_primaries: ((make_rational(Int32(displayPrimariesRX), chromaDen), + make_rational(Int32(displayPrimariesRY), chromaDen)), + (make_rational(Int32(displayPrimariesGX), chromaDen), + make_rational(Int32(displayPrimariesGY), chromaDen)), + (make_rational(Int32(displayPrimariesBX), chromaDen), + make_rational(Int32(displayPrimariesBY), chromaDen))), + white_point: (make_rational(Int32(whitePointX), chromaDen), + make_rational(Int32(whitePointY), chromaDen)), + min_luminance: make_rational(Int32(minDisplayMasteringLuminance), lumaDen), + max_luminance: make_rational(Int32(maxDisplayMasteringLuminance), lumaDen), + has_primaries: 1, + has_luminance: 1) + } + + // Dolby Vision Configuration + + if MP4HaveTrackAtom(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvcC") { + var versionMajor: UInt64 = 0, versionMinor: UInt64 = 0, profile: UInt64 = 0, + level: UInt64 = 0, rpuPresentFlag: UInt64 = 0, elPresentFlag: UInt64 = 0, + blPresentFlag: UInt64 = 0, blSignalCompatibilityId: UInt64 = 0; + + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvvC.dv_version_major", &versionMajor); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvvC.dv_version_minor", &versionMinor); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvvC.dv_profile", &profile); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvvC.dv_level", &level); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvvC.rpu_present_flag", &rpuPresentFlag); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvvC.el_present_flag", &elPresentFlag); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvvC.bl_present_flag", &blPresentFlag); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].dvvC.dv_bl_signal_compatibility_id", &blSignalCompatibilityId); + + self.dolbyVision = MP42DolbyVisionMetadata(versionMajor: UInt8(versionMajor), versionMinor: UInt8(versionMinor), + profile: UInt8(profile), level: UInt8(level), + rpuPresentFlag: (rpuPresentFlag != 0), elPresentFlag: (elPresentFlag != 0), + blPresentFlag: (blPresentFlag != 0), blSignalCompatibilityId: UInt8(blSignalCompatibilityId)) + } + + // Clean aperture + if MP4HaveTrackAtom(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap") { + var widthN: UInt64 = 0, widthD: UInt64 = 0, + heightN: UInt64 = 0, heightD: UInt64 = 0, + horizOffN: UInt64 = 0, horizOffD: UInt64 = 0, + vertOffN: UInt64 = 0, vertOffD: UInt64 = 0; + + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap.cleanApertureWidthN", &widthN); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap.cleanApertureWidthD", &widthD); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap.cleanApertureHeightN", &heightN); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap.cleanApertureHeightD", &heightD); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap.horizOffN", &horizOffN); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap.horizOffD", &horizOffD); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap.vertOffN", &vertOffN); + MP4GetTrackIntegerProperty(fileHandle, trackId, "mdia.minf.stbl.stsd.[\(index)].clap.vertOffD", &vertOffD); + + self.cleanAperture = CleanAperture(widthN: widthN, widthD: widthD, heightN: heightN, heightD: heightD, + horizOffN: horizOffN, horizOffD: horizOffD, vertOffN: vertOffN, vertOffD: vertOffD) + + } + + super.init(fileHandle: fileHandle, trackId: trackId, index: index) + + if format == kMP42VideoCodecType_H264 { + var origProfile: UInt8 = 0, origLevel: UInt8 = 0 + MP4GetTrackH264ProfileLevel(fileHandle, trackId, + &origProfile, &origLevel) + self.profile = H264Profile(origProfile: origProfile,origLevel: origLevel, + newProfile: origProfile, newLevel: origLevel) + } + } +} + +class AudioSampleDescription: SampleDescription { + var channels: UInt32 + var channelLayoutTag: UInt32 + var extensionType: MP42AudioEmbeddedExtension + + override init(fileHandle: MP42FileHandle, trackId: MP42TrackId,index: UInt32) { + self.channels = 0 + self.channelLayoutTag = 0 + self.extensionType = kMP42AudioEmbeddedExtension_None + + super.init(fileHandle: fileHandle, trackId: trackId, index: index) + } + +} + + +/** + Set FourCharCode/OSType using a String. + + Examples: + let test: FourCharCode = "420v" + let test2 = FourCharCode("420f") + print(test.string, test2.string) +*/ +extension FourCharCode: ExpressibleByStringLiteral { + + public init(stringLiteral value: StringLiteralType) { + var code: FourCharCode = 0 + // Value has to consist of 4 printable ASCII characters, e.g. '420v'. + // Note: This implementation does not enforce printable range (32-126) + if value.count == 4 && value.utf8.count == 4 { + for byte in value.utf8 { + code = code << 8 + FourCharCode(byte) + } + } + else { + print("FourCharCode: Can't initialize with '\(value)', only printable ASCII allowed. Setting to '????'.") + code = 0x3F3F3F3F // = '????' + } + self = code + } + + public init(extendedGraphemeClusterLiteral value: String) { + self = FourCharCode(stringLiteral: value) + } + + public init(unicodeScalarLiteral value: String) { + self = FourCharCode(stringLiteral: value) + } + + public init(_ value: String) { + self = FourCharCode(stringLiteral: value) + } + + public var string: String? { + let cString: [CChar] = [ + CChar(self >> 24 & 0xFF), + CChar(self >> 16 & 0xFF), + CChar(self >> 8 & 0xFF), + CChar(self & 0xFF), + 0 + ] + return String(cString: cString) + } +} diff --git a/MP42Foundation.xcodeproj/project.pbxproj b/MP42Foundation.xcodeproj/project.pbxproj index 33c5645..38ab3a1 100644 --- a/MP42Foundation.xcodeproj/project.pbxproj +++ b/MP42Foundation.xcodeproj/project.pbxproj @@ -255,6 +255,8 @@ A9ED5B141F824BC100E0E4FA /* MP42SSAImporter.m in Sources */ = {isa = PBXBuildFile; fileRef = A9ED5B111F824BC000E0E4FA /* MP42SSAImporter.m */; }; A9ED5B151F824BC100E0E4FA /* MP42SSAImporter.h in Headers */ = {isa = PBXBuildFile; fileRef = A9ED5B121F824BC000E0E4FA /* MP42SSAImporter.h */; }; A9ED5B161F824BC100E0E4FA /* MP42SSAImporter.h in Headers */ = {isa = PBXBuildFile; fileRef = A9ED5B121F824BC000E0E4FA /* MP42SSAImporter.h */; }; + A9FD169F27661B690096BBA8 /* SampleDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9FD169E27661B690096BBA8 /* SampleDescription.swift */; }; + A9FD16A027661B690096BBA8 /* SampleDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9FD169E27661B690096BBA8 /* SampleDescription.swift */; }; FFE849961EBCAFDF000AA078 /* MP42AC3AudioEncoder.h in Headers */ = {isa = PBXBuildFile; fileRef = FFE849941EBCAFDF000AA078 /* MP42AC3AudioEncoder.h */; }; FFE849971EBCAFDF000AA078 /* MP42AC3AudioEncoder.m in Sources */ = {isa = PBXBuildFile; fileRef = FFE849951EBCAFDF000AA078 /* MP42AC3AudioEncoder.m */; }; /* End PBXBuildFile section */ @@ -441,6 +443,7 @@ A9ED5B0C1F824A9B00E0E4FA /* MP42SSAParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42SSAParser.m; sourceTree = ""; }; A9ED5B111F824BC000E0E4FA /* MP42SSAImporter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42SSAImporter.m; sourceTree = ""; }; A9ED5B121F824BC000E0E4FA /* MP42SSAImporter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42SSAImporter.h; sourceTree = ""; }; + A9FD169E27661B690096BBA8 /* SampleDescription.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleDescription.swift; sourceTree = ""; }; FFE849941EBCAFDF000AA078 /* MP42AC3AudioEncoder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42AC3AudioEncoder.h; sourceTree = ""; }; FFE849951EBCAFDF000AA078 /* MP42AC3AudioEncoder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42AC3AudioEncoder.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -573,6 +576,7 @@ 6661A2ED20890B4400B1A669 /* MP42Foundation.h */, A9B9C2301823923100416A4E /* MP42File.h */, A9B9C2311823923100416A4E /* MP42File.m */, + A9FD169E27661B690096BBA8 /* SampleDescription.swift */, A9B9C2501823923100416A4E /* MP42Track.h */, A9B9C2511823923100416A4E /* MP42Track.m */, A99FCBC31BAD65EF0058E27A /* MP42Track+Private.h */, @@ -984,6 +988,7 @@ A941A7B71DA7B08600FB2A7C /* MP42MetadataFormat.m in Sources */, A910B5DC18394EB20064028F /* MP42Languages.m in Sources */, A9059DCF1D870F8D000D1B9A /* MP42ConversionSettings.m in Sources */, + A9FD16A027661B690096BBA8 /* SampleDescription.swift in Sources */, A910B5E018394EB20064028F /* MP42PrivateUtilities.m in Sources */, A910B5E218394EB20064028F /* MP42Utilities.m in Sources */, A910B5E418394EB20064028F /* MP42SubUtilities.m in Sources */, @@ -1050,6 +1055,7 @@ A9DFBDA3195FF5BA00410060 /* MP42Heap.m in Sources */, A9059DCC1D870B9C000D1B9A /* MP42ConversionSettings.m in Sources */, A9B9C26B1823923200416A4E /* MP42AudioConverter.m in Sources */, + A9FD169F27661B690096BBA8 /* SampleDescription.swift in Sources */, A941C9601F82A9B900FC5E8D /* MP42SSAConverter.m in Sources */, A9B9C2711823923200416A4E /* MP42BitmapSubConverter.m in Sources */, A9B9C28E1823923200416A4E /* MP42Muxer.m in Sources */,