From 1a908808976c386f244e520a8a6f22127b98976f Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Tue, 14 May 2019 15:09:12 +0200 Subject: [PATCH] Add a workaround for some mov aac tracks. --- MP42/MP42AVFImporter.m | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index ee3160d..0a359fa 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -789,6 +789,38 @@ const void *cookieBuffer = CMAudioFormatDescriptionGetMagicCookie(formatDescription, &cookieSizeOut); // Returns proper Atmos dec3 atom (in macOS 10.14.2), if proper E-AC3 stream, not AC-3-embedded! if (cookieBuffer == NULL || cookieSizeOut == 0) { + + if (code == kAudioFormatMPEG4AAC) { + + // Try to find the ESDS manually + CFDictionaryRef extensions = CMFormatDescriptionGetExtensions(formatDescription); + if (extensions != NULL) { + CFDataRef verbatimSampleDescription = CFDictionaryGetValue(extensions, kCMFormatDescriptionExtension_VerbatimSampleDescription); + + if (verbatimSampleDescription != NULL) { + CFIndex length = CFDataGetLength(verbatimSampleDescription); + if (length >= 103) { + UInt8 *cookieBuffer = malloc(sizeof(UInt8) * length - 60); + CFRange range = CFRangeMake(60, length - 60); + CFDataGetBytes(verbatimSampleDescription, range, cookieBuffer); + + UInt8 *buffer; + int size; + ReadESDSDescExt((void *)cookieBuffer, &buffer, &size, 1); + + NSData *magicCookie = nil; + if (size) { + magicCookie = [NSData dataWithBytes:buffer length:size]; + free(buffer); + } + free(cookieBuffer); + + return magicCookie; + } + } + } + } + return nil; }