From e42154278f286b102f0eb47ddf15921c97ded223 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Mon, 31 May 2021 12:09:29 +0200 Subject: [PATCH] Add experimental av1 support. --- MP42/MP42AVFImporter.m | 3 +++ MP42/MP42Mp4Importer.m | 8 ++++++++ MP42/MP42Muxer.m | 24 ++++++++++++++++++++++++ MP42/MP42PrivateUtilities.m | 3 +++ MP42/MP42Utilities.m | 3 ++- MP42/MP42VideoTrack.m | 3 ++- 6 files changed, 42 insertions(+), 2 deletions(-) diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index 03455af..e98a7de 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -812,6 +812,9 @@ MP42_OBJC_DIRECT_MEMBERS else if (code == kCMVideoCodecType_HEVC || code == 'hev1') { magicCookie = CFDictionaryGetValue(atoms, @"hvcC"); } + else if (code == 'av01') { + magicCookie = CFDictionaryGetValue(atoms, @"av1C"); + } else if (code == kCMVideoCodecType_MPEG4Video) { magicCookie = CFDictionaryGetValue(atoms, @"esds"); } diff --git a/MP42/MP42Mp4Importer.m b/MP42/MP42Mp4Importer.m index 9bc9eaf..f0fce95 100644 --- a/MP42/MP42Mp4Importer.m +++ b/MP42/MP42Mp4Importer.m @@ -223,6 +223,14 @@ typedef struct MP4DemuxHelper { free(ppValue); return magicCookie; } + else if (!strcmp(media_data_name, "av01")) { + uint8_t *ppValue; + uint32_t pValueSize; + MP4GetTrackBytesProperty(_fileHandle, srcTrackId, "mdia.minf.stbl.stsd.av01.av1C.av1Config", &ppValue, &pValueSize); + magicCookie = [NSData dataWithBytes:ppValue length:pValueSize]; + free(ppValue); + return magicCookie; + } else if (!strcmp(media_data_name, "mp4v")) { uint8_t *ppConfig; uint32_t pConfigSize; MP4GetTrackESConfiguration(_fileHandle, srcTrackId, &ppConfig, &pConfigSize); diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index 35f0718..a627175 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -247,6 +247,30 @@ MP42_OBJC_DIRECT_MEMBERS } } + // AV1 video track + else if ([track isMemberOfClass:[MP42VideoTrack class]] && (format == kMP42VideoCodecType_AV1)) { + + if (magicCookie.length && magicCookie.length < UINT32_MAX) { + + dstTrackId = MP4AddAV1VideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, + ((MP42VideoTrack *)track).width, ((MP42VideoTrack *)track).height); + + if (dstTrackId) { + MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.av01.av1C.av1Config", magicCookie.bytes, (uint32_t)magicCookie.length); + [importer setActiveTrack:track]; + } + else { + [unsupportedTracks addObject:track]; + continue; + } + } + else { + [unsupportedTracks addObject:track]; + continue; + } + } + + // MPEG-4 Visual video track else if ([track isMemberOfClass:[MP42VideoTrack class]] && format == kMP42VideoCodecType_MPEG4Video) { MP4SetVideoProfileLevel(_fileHandle, MPEG4_SP_L3); diff --git a/MP42/MP42PrivateUtilities.m b/MP42/MP42PrivateUtilities.m index d7a6d6e..3a868cb 100644 --- a/MP42/MP42PrivateUtilities.m +++ b/MP42/MP42PrivateUtilities.m @@ -228,6 +228,9 @@ FourCharCode getTrackMediaSubType(MP4FileHandle fileHandle, MP4TrackId Id) else if (!strcmp(dataName, "hev1")) { return kMP42VideoCodecType_HEVC_PSinBitstream; } + else if (!strcmp(dataName, "av01")) { + return kMP42VideoCodecType_AV1; + } else if (!strcmp(dataName, "mp4a")) { uint8_t audiotype = MP4GetTrackEsdsObjectTypeId(fileHandle, Id); if (audiotype == MP4_MPEG4_AUDIO_TYPE) diff --git a/MP42/MP42Utilities.m b/MP42/MP42Utilities.m index fd8b72f..93620e1 100644 --- a/MP42/MP42Utilities.m +++ b/MP42/MP42Utilities.m @@ -39,7 +39,8 @@ MP42Duration TimeFromString(NSString *time_string, int32_t timeScale) BOOL isTrackMuxable(FourCharCode format) { - FourCharCode supportedFormats[] = {kMP42VideoCodecType_HEVC, + FourCharCode supportedFormats[] = {kMP42VideoCodecType_AV1, + kMP42VideoCodecType_HEVC, kMP42VideoCodecType_HEVC_PSinBitstream, kMP42VideoCodecType_H264, kMP42VideoCodecType_MPEG4Video, diff --git a/MP42/MP42VideoTrack.m b/MP42/MP42VideoTrack.m index 62819d7..8f72064 100644 --- a/MP42/MP42VideoTrack.m +++ b/MP42/MP42VideoTrack.m @@ -203,7 +203,8 @@ static uint32_t convertToFixedPoint(CGFloat value) { free(val); if ((self.format == kMP42VideoCodecType_H264 || self.format == kMP42VideoCodecType_MPEG4Video - || self.format == kMP42VideoCodecType_HEVC || self.format == kMP42VideoCodecType_HEVC_PSinBitstream)) { + || self.format == kMP42VideoCodecType_HEVC || self.format == kMP42VideoCodecType_HEVC_PSinBitstream + || self.format == kMP42VideoCodecType_AV1)) { if (self.updatedProperty[@"colr"] || self.muxed == NO) { if (_colorPrimaries > 0 && _transferCharacteristics > 0 && _matrixCoefficients > 0) {