diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index 3c8f78d..f7fdceb 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -899,6 +899,11 @@ return result; } +- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track; +{ + return YES; +} + - (void)demux { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @@ -967,7 +972,7 @@ if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) { sync = 0; } - if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) { + if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_DoNotDisplay]) { doNotDisplay = YES; } } @@ -1142,26 +1147,22 @@ free(sizeArrayOut); } else { - if (attachments && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_EmptyMedia)) { - CMTime currentDuration = CMTimeConvertScale(duration, timescale, kCMTimeRoundingMethod_Default); - CMTime currentDecodeTimestamp = CMTimeConvertScale(decodeTimeStamp, timescale, kCMTimeRoundingMethod_Default); - CMTime currentTimeStamp = CMTimeConvertScale(presentationTimeStamp, timescale, kCMTimeRoundingMethod_Default); - CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default); + CMTime currentDuration = CMTimeConvertScale(duration, timescale, kCMTimeRoundingMethod_Default); + CMTime currentTimeStamp = CMTimeConvertScale(presentationTimeStamp, timescale, kCMTimeRoundingMethod_Default); + CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default); + + MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; + sample->duration = currentDuration.value; + sample->presentationTimestamp = currentTimeStamp.value; + sample->presentationOutputTimestamp = currentOutputTimeStamp.value; + sample->timescale = timescale; + sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; + sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; + sample->trackId = track.sourceId; - MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; - sample->duration = currentDuration.value; - sample->offset = -currentDecodeTimestamp.value + presentationTimeStamp.value; - sample->presentationTimestamp = currentTimeStamp.value; - sample->presentationOutputTimestamp = currentOutputTimeStamp.value; - sample->timescale = timescale; - sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; - sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; - sample->trackId = track.sourceId; + sample->attachments = (void *)attachments; - sample->attachments = (void *)attachments; - - [demuxHelper->editsConstructor addSample:sample]; - } + [demuxHelper->editsConstructor addSample:sample]; } CFRelease(sampleBuffer); diff --git a/MP42/MP42AudioTrack.m b/MP42/MP42AudioTrack.m index fdad8ae..3d8b7b4 100644 --- a/MP42/MP42AudioTrack.m +++ b/MP42/MP42AudioTrack.m @@ -54,21 +54,6 @@ _channels = c.channels; free(pAacConfig); } - - if (!MP4HaveTrackAtom(fileHandle, self.trackId, "mdia.minf.stbl.sgpd")) { - BOOL addDelay = YES; - uint32_t edits = MP4GetTrackNumberOfEdits(fileHandle, self.trackId); - if (edits == 1) { - MP4Timestamp startTime = MP4GetTrackEditMediaStart(fileHandle, self.trackId, 1); - if (startTime != 0) { - addDelay = NO; - } - } - if (addDelay) { - // Guess a audio encoder delay - self.startOffset = -2112.f / self.timescale * 1000; - } - } } else if ((audioType == MP4_PCM16_LITTLE_ENDIAN_AUDIO_TYPE) || (audioType == MP4_PCM16_BIG_ENDIAN_AUDIO_TYPE)) { diff --git a/MP42/MP42EditListsReconstructor.m b/MP42/MP42EditListsReconstructor.m index 1a8d4b1..65214ed 100644 --- a/MP42/MP42EditListsReconstructor.m +++ b/MP42/MP42EditListsReconstructor.m @@ -58,10 +58,16 @@ CFBooleanRef emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia); if (emptyMedia && CFBooleanGetValue(emptyMedia) == 1) { [self flush]; + + CMTime editStart = CMTimeMake(sample->presentationOutputTimestamp, _timescale); + [self startEditListAtTime:editStart]; + _emptyEditOpen = YES; } } - [_priorityQueue insert:sample]; + if (sample->size) { + [_priorityQueue insert:sample]; + } if ([_priorityQueue isFull]) { MP42SampleBuffer *extractedSample = [_priorityQueue extract]; @@ -69,7 +75,6 @@ } } - - (void)flush { while (!_priorityQueue.isEmpty) { @@ -80,6 +85,8 @@ if (_editOpen == YES && _emptyEditOpen == NO) { CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale); [self endEditListAtTime:editEnd empty:NO]; + + _currentMediaTime = 0; } } @@ -90,47 +97,38 @@ - (void)analyzeSample:(MP42SampleBuffer *)sample { -#ifdef AVF_DEBUG - NSLog(@"T: %llu, D: %lld, P: %lld, PO: %lld O: %lld", _currentMediaTime, sample->decodeTimestamp, sample->presentationTimestamp, sample->presentationOutputTimestamp, sample->offset); -#endif - if (_timescale == 0) { _timescale = sample->timescale; - // Re-align things if the first sample pts is not 0 - if (sample->presentationTimestamp != 0) { - _currentMediaTime += sample->presentationTimestamp; - } } - CFDictionaryRef trimStart = NULL, trimEnd = NULL, emptyMedia = NULL; + if (_currentMediaTime == 0) { + // Re-align things if the first sample pts is not 0 + _currentMediaTime = sample->presentationTimestamp; + } + +#ifdef AVF_DEBUG + NSLog(@"T: %llu, D: %lld, P: %lld, PO: %lld O: %lld", _currentMediaTime, sample->decodeTimestamp, sample->presentationTimestamp, sample->presentationOutputTimestamp, sample->offset); + NSLog(@"%d", sample->flags); +#endif + + CFDictionaryRef trimStart = NULL, trimEnd = NULL; if (sample->attachments) { trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart); trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd); - emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia); } - // Check if we need to add an empty edit list. - if (emptyMedia) { - if (_editOpen == YES) { - [self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO]; - } + BOOL shouldCloseEmptyEdit = (trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == 0)) && _emptyEditOpen == YES && _editOpen == YES; - CMTime editStart = CMTimeMake(sample->presentationOutputTimestamp, _timescale); - [self startEditListAtTime:editStart]; - _emptyEditOpen = YES; + if (shouldCloseEmptyEdit) { + CMTime editEnd = CMTimeMake(sample->presentationOutputTimestamp, _timescale); + [self endEditListAtTime:editEnd empty:YES]; + _emptyEditOpen = NO; } - BOOL shouldStartNewEdit = trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == NO && _editOpen == NO); - - uint64_t trimmedDuration = sample->duration; + BOOL shouldStartNewEdit = trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == 0 && _editOpen == NO && _emptyEditOpen == NO); if (shouldStartNewEdit) { // Close the current edit list - if (_emptyEditOpen == YES) { - _emptyEditOpen = NO; - CMTime editEnd = CMTimeMake(sample->presentationOutputTimestamp, _timescale); - [self endEditListAtTime:editEnd empty:YES]; - } if (_editOpen == YES) { [self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO]; } @@ -142,7 +140,6 @@ CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default); editStart.value += trimStartTime.value; - trimmedDuration -= trimStartTime.value; } [self startEditListAtTime:editStart]; @@ -150,16 +147,19 @@ _currentMediaTime += sample->duration; - BOOL shouldEndEdit = trimEnd || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == YES && _editOpen == YES); + BOOL shouldEndEdit = (trimEnd || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) != 0)) && _editOpen == YES && _emptyEditOpen == NO; if (shouldEndEdit) { CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale); + if ((sample->flags & MP42SampleBufferFlagDoNotDisplay) != 0) { + editEnd.value -= sample->duration; + } + if (trimEnd) { CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd); trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default); editEnd.value -= trimEndTime.value; - trimmedDuration -= trimEndTime.value; } [self endEditListAtTime:editEnd empty:NO]; diff --git a/MP42/MP42FileImporter+Private.h b/MP42/MP42FileImporter+Private.h index 7bb21e1..85800cd 100644 --- a/MP42/MP42FileImporter+Private.h +++ b/MP42/MP42FileImporter+Private.h @@ -50,6 +50,8 @@ NS_ASSUME_NONNULL_BEGIN - (nullable NSData *)magicCookieForTrack:(MP42Track *)track; - (AudioStreamBasicDescription)audioDescriptionForTrack:(MP42Track *)track; - (BOOL)cleanUp:(MP42FileHandle)fileHandle; + +- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track; - (BOOL)supportsPreciseTimestamps; @end diff --git a/MP42/MP42FileImporter.m b/MP42/MP42FileImporter.m index 0d04bb5..3fff7cb 100644 --- a/MP42/MP42FileImporter.m +++ b/MP42/MP42FileImporter.m @@ -208,6 +208,11 @@ static NSArray *_supportedFileFormats; userInfo:nil]; } +- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track; +{ + return NO; +} + - (BOOL)supportsPreciseTimestamps { return NO; diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index 1075f9f..7aa620b 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -784,6 +784,11 @@ static NSString * TrackNameToString(TrackInfo *track) return result; } +- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track; +{ + return YES; +} + // Methods to extract all the samples from the active tracks at the same time - (void)demux { @@ -1206,6 +1211,18 @@ static NSString * TrackNameToString(TrackInfo *track) editDuration, 0); } + else { + MP4Duration editDuration = MP4ConvertFromTrackDuration(fileHandle, trackId, + MP4GetTrackDuration(fileHandle, trackId), + MP4GetTimeScale(fileHandle)); + + MP4AddTrackEdit(fileHandle, + trackId, + MP4_INVALID_EDIT_ID, + 0, + editDuration, + 0); + } } return YES; diff --git a/MP42/MP42Mp4Importer.m b/MP42/MP42Mp4Importer.m index a1890c9..8fedfcb 100644 --- a/MP42/MP42Mp4Importer.m +++ b/MP42/MP42Mp4Importer.m @@ -273,6 +273,11 @@ return nil; } +- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track; +{ + return MP4HaveTrackAtom(_fileHandle, track.sourceId, "mdia.minf.stbl.sgpd"); +} + - (void)demux { @autoreleasepool { diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index 5dcebc1..9379a0c 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -267,7 +267,9 @@ magicCookie.bytes, magicCookie.length); } - + + MP4SetTrackWantsRoll(_fileHandle, dstTrackId, [helper->importer audioTrackUsesExplicitEncoderDelay:track]); + [helper->importer setActiveTrack:track]; }