From 83f7b29e133ba35e9014c8955bc3ba655ff4702d Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Wed, 8 Dec 2021 11:56:28 +0100 Subject: [PATCH] Add support for hvcE and avcE boxes. --- MP42/MP42AVFImporter.m | 10 ++++++++++ MP42/MP42MkvImporter.m | 41 ++++++++++++++++++++++++++--------------- MP42/MP42Muxer.m | 16 ++++++++++++++++ MP42/MP42VideoTrack.h | 3 ++- MP42/MP42VideoTrack.m | 23 ++++++++++++++++++++++- 5 files changed, 76 insertions(+), 17 deletions(-) diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index 6195635..773a587 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -331,6 +331,16 @@ MP42_OBJC_DIRECT_MEMBERS videoTrack.dolbyVision = dv; } + + CFDataRef dvELConfiguration = CFDictionaryGetValue(atoms, @"hvcE"); + + if (!dvELConfiguration) { + dvELConfiguration = CFDictionaryGetValue(atoms, @"avcE"); + } + + if (dvELConfiguration) { + videoTrack.dolbyVisionELConfiguration = (__bridge NSData *)(dvELConfiguration); + } } } diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index a634885..33dbc4a 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -197,27 +197,38 @@ MP42_OBJC_DIRECT_MEMBERS for (unsigned int xi = 0; xi < bamCount; xi++) { BlockAdditionMapping *blockAddition = &bams[xi]; - if (blockAddition->TrackId == i && - (blockAddition->Value == 0x64766343 || blockAddition->Value == 0x664767643 || blockAddition->Value == 0x64767643) && - blockAddition->Length >= 24) { - uint8_t *data = malloc(blockAddition->Length); - if (readData(_ioStream, blockAddition->Position, &data, blockAddition->Length)) { - MP42DolbyVisionMetadata dv; + if (blockAddition->TrackId == i) { + if ((blockAddition->Value == 'dvcC' || + blockAddition->Value == 0x664767643 || + blockAddition->Value == 'dvvC') && + blockAddition->Length >= 24) { + uint8_t *buffer = malloc(blockAddition->Length); - dv.versionMajor = data[0]; - dv.versionMinor = data[1]; + if (readData(_ioStream, blockAddition->Position, &buffer, blockAddition->Length)) { + MP42DolbyVisionMetadata dv; - dv.profile = (data[2] & 0xfe) >> 1; - dv.level = ((data[2] & 0x1) << 7) + ((data[3] & 0xf8) >> 3); + dv.versionMajor = buffer[0]; + dv.versionMinor = buffer[1]; - dv.rpuPresentFlag = (data[3] & 0x4) >> 2; - dv.elPresentFlag = (data[3] & 0x2) >> 1; - dv.blPresentFlag = data[3] & 0x1; + dv.profile = (buffer[2] & 0xfe) >> 1; + dv.level = ((buffer[2] & 0x1) << 7) + ((buffer[3] & 0xf8) >> 3); - dv.blSignalCompatibilityId = (data[4] & 0xf0) >> 4; + dv.rpuPresentFlag = (buffer[3] & 0x4) >> 2; + dv.elPresentFlag = (buffer[3] & 0x2) >> 1; + dv.blPresentFlag = buffer[3] & 0x1; - videoTrack.dolbyVision = dv; + dv.blSignalCompatibilityId = (buffer[4] & 0xf0) >> 4; + + videoTrack.dolbyVision = dv; + free(buffer); + } + } + else if (blockAddition->Value == 'hvcE' || blockAddition->Value == 'avcE') { + uint8_t *buffer = malloc(blockAddition->Length); + if (readData(_ioStream, blockAddition->Position, &buffer, blockAddition->Length)) { + videoTrack.dolbyVisionELConfiguration = [NSData dataWithBytesNoCopy:buffer length:blockAddition->Length]; + } } } } diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index 65fcf2d..fd30611 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -241,6 +241,11 @@ MP42_OBJC_DIRECT_MEMBERS videoTrack.dolbyVision.blPresentFlag, videoTrack.dolbyVision.blSignalCompatibilityId); } + if (videoTrack.dolbyVisionELConfiguration) { + MP4SetDolbyVisionELConfiguration(_fileHandle, dstTrackId, + videoTrack.dolbyVisionELConfiguration.bytes, + (uint32_t)videoTrack.dolbyVisionELConfiguration.length); + } [importer setActiveTrack:track]; } else { @@ -259,6 +264,12 @@ MP42_OBJC_DIRECT_MEMBERS (format == kMP42VideoCodecType_DolbyVisionHEVC || format == kMP42VideoCodecType_DolbyVisionHEVC_PSinBitstream)) { MP42VideoTrack *videoTrack = (MP42VideoTrack *)track; + uint8_t *hvcCAtom = (uint8_t *)magicCookie.bytes; + + if ([_options[MP42ForceHvc1] boolValue] && magicCookie.length < UINT32_MAX) { + force_HEVC_completeness(hvcCAtom, (uint32_t)magicCookie.length); + } + bool completeness = 0; if (magicCookie.length && magicCookie.length < UINT32_MAX && !analyze_HEVC(magicCookie.bytes, (uint32_t)magicCookie.length, &completeness)) { @@ -276,6 +287,11 @@ MP42_OBJC_DIRECT_MEMBERS completeness); if (dstTrackId) { + if (videoTrack.dolbyVisionELConfiguration) { + MP4SetDolbyVisionELConfiguration(_fileHandle, dstTrackId, + videoTrack.dolbyVisionELConfiguration.bytes, + (uint32_t)videoTrack.dolbyVisionELConfiguration.length); + } [importer setActiveTrack:track]; } else { diff --git a/MP42/MP42VideoTrack.h b/MP42/MP42VideoTrack.h index 60fcb95..88fcc66 100644 --- a/MP42/MP42VideoTrack.h +++ b/MP42/MP42VideoTrack.h @@ -27,12 +27,13 @@ @property(nonatomic, readwrite) uint16_t matrixCoefficients; @property(nonatomic, readwrite) uint16_t colorRange; -// HDR Mastering Metadata +// HDR10 Mastering Metadata @property(nonatomic, readwrite) MP42MasteringDisplayMetadata mastering; @property(nonatomic, readwrite) MP42ContentLightMetadata coll; // Dolby Vision @property(nonatomic, readwrite) MP42DolbyVisionMetadata dolbyVision; +@property(nonatomic, readwrite) NSData *dolbyVisionELConfiguration; // Pixel Aspect Ratio @property(nonatomic, readwrite) uint64_t hSpacing; diff --git a/MP42/MP42VideoTrack.m b/MP42/MP42VideoTrack.m index f383c17..8700d0a 100644 --- a/MP42/MP42VideoTrack.m +++ b/MP42/MP42VideoTrack.m @@ -188,6 +188,24 @@ MP42_OBJC_DIRECT_MEMBERS _dolbyVision.blPresentFlag = blPresentFlag; _dolbyVision.blSignalCompatibilityId = blSignalCompatibilityId; } + + if (MP4HaveTrackAtom(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.hvcE")) { + uint8_t *ppValue; + uint32_t pValueSize = 0; + MP4GetTrackBytesProperty(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.hvcE.HEVCConfig", &ppValue, &pValueSize); + if (pValueSize) { + _dolbyVisionELConfiguration = [NSData dataWithBytesNoCopy:ppValue length:pValueSize]; + } + } + else if (MP4HaveTrackAtom(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.avcE")) { + uint8_t *ppValue; + uint32_t pValueSize = 0; + MP4GetTrackBytesProperty(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.avcE.AVCConfig", &ppValue, &pValueSize); + if (pValueSize) { + _dolbyVisionELConfiguration = [NSData dataWithBytesNoCopy:ppValue length:pValueSize]; + } + } + if (MP4HaveTrackAtom(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.clap")) { MP4GetTrackIntegerProperty(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.clap.cleanApertureWidthN", &_cleanApertureWidthN); MP4GetTrackIntegerProperty(fileHandle, self.trackId, "mdia.minf.stbl.stsd.*.clap.cleanApertureWidthD", &_cleanApertureWidthD); @@ -484,7 +502,7 @@ static uint32_t convertToFixedPoint(CGFloat value) { if (![summary containsString:@"Dolby Vision"]) { [summary appendString:@" Dolby Vision"]; } - [summary appendFormat:@" %d.%d", _dolbyVision.profile, _dolbyVision.level]; + [summary appendFormat:@" %d.%d", _dolbyVision.profile, _dolbyVision.blSignalCompatibilityId]; } return summary; } @@ -511,6 +529,7 @@ static uint32_t convertToFixedPoint(CGFloat value) { copy->_mastering = _mastering; copy->_coll = _coll; copy->_dolbyVision = _dolbyVision; + copy->_dolbyVisionELConfiguration = _dolbyVisionELConfiguration; copy->_hSpacing = _hSpacing; copy->_vSpacing = _vSpacing; @@ -594,6 +613,7 @@ static uint32_t convertToFixedPoint(CGFloat value) { [coder encodeInt32:_dolbyVision.elPresentFlag forKey:@"DVelPresentFlag"]; [coder encodeInt32:_dolbyVision.blPresentFlag forKey:@"DVblPresentFlag"]; [coder encodeInt32:_dolbyVision.blSignalCompatibilityId forKey:@"DVblSignalCompatibilityId"]; + [coder encodeObject:_dolbyVisionELConfiguration forKey:@"DVELConfiguration"]; [coder encodeDouble:_transform.a forKey:@"transformA"]; [coder encodeDouble:_transform.b forKey:@"transformB"]; @@ -659,6 +679,7 @@ static uint32_t convertToFixedPoint(CGFloat value) { _dolbyVision.elPresentFlag = [decoder decodeInt32ForKey:@"DVelPresentFlag"]; _dolbyVision.blPresentFlag = [decoder decodeInt32ForKey:@"DVblPresentFlag"]; _dolbyVision.blSignalCompatibilityId = [decoder decodeInt32ForKey:@"DVblSignalCompatibilityId"]; + _dolbyVisionELConfiguration = [decoder decodeObjectOfClass:[NSData class] forKey:@"DVELConfiguration"]; _hSpacing = [decoder decodeInt64ForKey:@"hSpacing"]; _vSpacing = [decoder decodeInt64ForKey:@"vSpacing"];