From bda8ac4b37758b61d6182d9ed1a76317e1bdd1a7 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Wed, 4 Nov 2015 23:41:08 +0100 Subject: [PATCH] Partial eac3 passthru. --- MP42/MP42AudioConverter.m | 3 +- MP42/MP42AudioTrack.m | 8 ++- MP42/MP42Mp4Importer.m | 12 ++++- MP42/MP42Muxer.m | 4 +- MP42/MP42PrivateUtilities.h | 1 + MP42/MP42PrivateUtilities.m | 104 ++++++++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+), 6 deletions(-) diff --git a/MP42/MP42AudioConverter.m b/MP42/MP42AudioConverter.m index 6d60770..fb10043 100644 --- a/MP42/MP42AudioConverter.m +++ b/MP42/MP42AudioConverter.m @@ -539,7 +539,8 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter, inputFormat.mFramesPerPacket = 1536; } else if ([track.sourceFormat isEqualToString:MP42AudioFormatEAC3]) { - // TODO + inputFormat.mFormatID = kAudioFormatEnhancedAC3; + inputFormat.mFramesPerPacket = 1536; } else if ([track.sourceFormat isEqualToString:MP42AudioFormatDTS]) { inputFormat.mFormatID = 'DTS '; diff --git a/MP42/MP42AudioTrack.m b/MP42/MP42AudioTrack.m index 5835bf9..e04c084 100644 --- a/MP42/MP42AudioTrack.m +++ b/MP42/MP42AudioTrack.m @@ -69,8 +69,12 @@ extern u_int8_t MP4AV_AacConfigGetChannels(u_int8_t* pConfig); readAC3Config(acmod, lfeon, &_channels, &_channelLayoutTag); } - else if (MP4HaveTrackAtom(fileHandle, _trackId, "mdia.minf.stbl.stsd.ac-3.dec3")) { - // TODO + else if (MP4HaveTrackAtom(fileHandle, _trackId, "mdia.minf.stbl.stsd.ec-3.dec3")) { + uint8_t *ppValue; + uint32_t pValueSize; + MP4GetTrackBytesProperty(fileHandle, _trackId, "mdia.minf.stbl.stsd.ec-3.dec3.content", &ppValue, &pValueSize); + + readEAC3Config(ppValue, pValueSize, &_channels, &_channelLayoutTag); } else if (MP4HaveTrackAtom(fileHandle, _trackId, "mdia.minf.stbl.stsd.alac")) { uint64_t channels_count = 0; diff --git a/MP42/MP42Mp4Importer.m b/MP42/MP42Mp4Importer.m index afd5872..526b0c0 100644 --- a/MP42/MP42Mp4Importer.m +++ b/MP42/MP42Mp4Importer.m @@ -106,10 +106,18 @@ return [ac3Info autorelease]; } + else if (!strcmp(media_data_name, "ec-3")) { + if (MP4HaveTrackAtom(_fileHandle, srcTrackId, "mdia.minf.stbl.stsd.ec-3.dec3")) { + uint8_t *ppValue; + uint32_t pValueSize; + MP4GetTrackBytesProperty(_fileHandle, srcTrackId, "mdia.minf.stbl.stsd.ec-3.dec3.content", &ppValue, &pValueSize); + magicCookie = [NSData dataWithBytes:ppValue length:pValueSize]; + } + } else if (!strcmp(media_data_name, "alac")) { if (MP4HaveTrackAtom(_fileHandle, srcTrackId, "mdia.minf.stbl.stsd.alac.alac")) { - uint8_t* ppValue; - uint32_t pValueSize; + uint8_t *ppValue; + uint32_t pValueSize; MP4GetTrackBytesProperty(_fileHandle, srcTrackId, "mdia.minf.stbl.stsd.alac.alac.AppleLosslessMagicCookie", &ppValue, &pValueSize); magicCookie = [NSData dataWithBytes:ppValue length:pValueSize]; } diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index 929d29f..3594628 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -214,7 +214,7 @@ if ([magicCookie length] < sizeof(uint64_t) * 6) continue; - const uint64_t * ac3Info = (const uint64_t *)[magicCookie bytes]; + const uint64_t *ac3Info = (const uint64_t *)[magicCookie bytes]; dstTrackId = MP4AddAC3AudioTrack(_fileHandle, timeScale, @@ -230,7 +230,9 @@ // EAC-3 audio tack else if ([track isMemberOfClass:[MP42AudioTrack class]] && [track.format isEqualToString:MP42AudioFormatEAC3]) { + dstTrackId = MP4AddEAC3AudioTrack(_fileHandle, timeScale, magicCookie.bytes, magicCookie.length); + [helper->importer setActiveTrack:track]; } // ALAC audio track diff --git a/MP42/MP42PrivateUtilities.h b/MP42/MP42PrivateUtilities.h index a6561df..11ad32f 100644 --- a/MP42/MP42PrivateUtilities.h +++ b/MP42/MP42PrivateUtilities.h @@ -51,6 +51,7 @@ CFDataRef createDescExt_XiphVorbis(UInt32 codecPrivateSize, const void * codecPr CFDataRef createDescExt_XiphFLAC(UInt32 codecPrivateSize, const void * codecPrivate); int readAC3Config(uint64_t acmod, uint64_t lfeon, UInt32 *channelsCount, UInt32 *channelLayoutTag); +int readEAC3Config(const uint8_t *cookie, uint32_t cookieLen, UInt32 *channelsCount, UInt32 *channelLayoutTag); int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id); void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, int64_t offset); diff --git a/MP42/MP42PrivateUtilities.m b/MP42/MP42PrivateUtilities.m index 2879c27..8ca2b14 100644 --- a/MP42/MP42PrivateUtilities.m +++ b/MP42/MP42PrivateUtilities.m @@ -657,6 +657,110 @@ int readAC3Config(uint64_t acmod, uint64_t lfeon, UInt32 *channelsCount, UInt32 return 1; } +static const int eac3_layout_no_lfe[8] = { + kAudioChannelLayoutTag_Stereo, + kAudioChannelLayoutTag_Mono, + kAudioChannelLayoutTag_Stereo, + kAudioChannelLayoutTag_AC3_3_0, + kAudioChannelLayoutTag_DVD_2, + kAudioChannelLayoutTag_AC3_3_1, + kAudioChannelLayoutTag_DVD_3, + kAudioChannelLayoutTag_MPEG_5_0_C}; + +static const int eac3_layout_lfe[8] = { + kAudioChannelLayoutTag_DVD_4, + kAudioChannelLayoutTag_AC3_1_0_1, + kAudioChannelLayoutTag_DVD_4, + kAudioChannelLayoutTag_AC3_3_0_1, + kAudioChannelLayoutTag_AC3_2_1_1, + kAudioChannelLayoutTag_AC3_3_1_1, + kAudioChannelLayoutTag_DVD_18, + kAudioChannelLayoutTag_MPEG_5_1_C, +}; + +int readEAC3Config(const uint8_t *cookie, uint32_t cookieLen, UInt32 *channelsCount, UInt32 *channelLayoutTag) +{ + if (cookieLen < 5) { + return 0; + } + + uint32_t data_rate, num_ind_sub; + + data_rate = *cookie << 5; + data_rate += (*(cookie + 1) & 0xf8) >> 3; + + num_ind_sub = *(cookie + 1) & 0x7; + num_ind_sub += 1; + + // we support only one independent substream + for (int i = 0; i < 1; i++) + { + uint32_t fscod, bsid, asvc, bsmod, acmod, lfeon; + + fscod = (*(cookie + 2) & 0xc0) >> 6; + bsid = (*(cookie + 2) & 0x3e) >> 1; + + asvc = (*(cookie + 3) & 0x80) >> 7; + + bsmod = (*(cookie + 3) & 0x70) >> 4; + acmod = (*(cookie + 3) & 0xe) >> 1; + + lfeon = *(cookie + 3) & 0x1; + + uint32_t num_dep_sub = 0; + uint32_t chan_loc = 0; + num_dep_sub = (*(cookie + 4) & 0x1e) >> 1; + + if (num_dep_sub > 0 && cookieLen > 5) { + chan_loc = (*(cookie + 4) & 0x1) << 8; + chan_loc += *(cookie + 5); + } + + if (acmod == 7 && lfeon && chan_loc != 0) { + // TODO: complete the list + if (chan_loc & 0x1) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_7_1_B; + } + else if (chan_loc & 0x2) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_7_1_A; + } + else if (chan_loc & 0x3) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_6_1_A; + } + else if (chan_loc & 0x4) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_6_1_B; + } + else if (chan_loc & 0x5) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_7_1_C; + } + else if (chan_loc & 0x6) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_7_1_D; + } + else if (chan_loc & 0x7) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_7_1_E; + } + else if (chan_loc & 0x8) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_6_1_C; + } + else if (chan_loc & 0x9) { + *channelLayoutTag = kAudioChannelLayoutTag_EAC3_7_1_A; + } + } + else { + if (lfeon) { + *channelLayoutTag = eac3_layout_lfe[acmod]; + } + else { + *channelLayoutTag = eac3_layout_no_lfe[acmod]; + } + } + + *channelsCount = AudioChannelLayoutTag_GetNumberOfChannels(*channelLayoutTag); + } + + return 1; +} + int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id) { int64_t offset = 0;