diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index f4be5f3..3c8f78d 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -26,9 +26,6 @@ @interface AVFDemuxHelper : NSObject { @public -#ifdef SB_AVF_DEBUG - int64_t currentTime; -#endif int64_t minDisplayOffset; AVAssetReaderOutput *assetReaderOutput; @@ -55,6 +52,10 @@ @"ac3", @"eac3", @"ec3", @"webvtt", @"vtt", @"caf", @"aif", @"aiff", @"aifc", @"wav", @"flac"]; } +- (BOOL)supportsPreciseTimestamps { + return YES; +} + - (FourCharCode)formatForTrack:(AVAssetTrack *)track { FourCharCode result = 0; CMFormatDescriptionRef formatDescription = (CMFormatDescriptionRef)track.formatDescriptions.firstObject; @@ -925,7 +926,7 @@ track.muxer_helper->demuxer_context = [[AVFDemuxHelper alloc] init]; demuxHelper = track.muxer_helper->demuxer_context; demuxHelper->assetReaderOutput = assetReaderOutput; - demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] initWithMediaFormat:track.format]; + demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init]; totalDataLength += track.dataLength; } @@ -944,57 +945,57 @@ while (!self.isCancelled) { CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer]; + CMTimeScale timescale = [self timescaleForTrack:track]; if (sampleBuffer) { CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer); + + CMTime duration = CMSampleBufferGetDuration(sampleBuffer); + CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer); + CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer); - if (samplesNum == 1) { - // We have only a sample - CMTime duration = CMSampleBufferGetDuration(sampleBuffer); - CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer); - CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); + CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default); - CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer); - size_t sampleSize = CMBlockBufferGetDataLength(buffer); - void *sampleData = malloc(sampleSize); - CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, sampleData); - - // Read sample attachment, sync to mark the frame as sync - BOOL sync = YES; - BOOL doNotDisplay = NO; - CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO); - if (attachmentsArray) { - for (NSDictionary *dict in (NSArray *)attachmentsArray) { - if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) { - sync = NO; - } - if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) { - doNotDisplay = YES; - } + // Read sample attachment, to mark the frame as sync + BOOL sync = 1; + BOOL doNotDisplay = NO; + CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO); + if (attachmentsArray) { + for (NSDictionary *dict in (NSArray *)attachmentsArray) { + if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) { + sync = 0; + } + if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) { + doNotDisplay = YES; } } + } - CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_Default); - CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(NULL, sampleBuffer, kCMAttachmentMode_ShouldPropagate); + CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(NULL, sampleBuffer, kCMAttachmentMode_ShouldPropagate); -#ifdef SB_AVF_DEBUG - NSLog(@"Delta: %lld", presentationTimeStamp.value - currentOutputTimeStamp.value); - NSLog(@"C: %lld, P: %lld, PO: %lld", demuxHelper->currentTime, presentationTimeStamp.value, currentOutputTimeStamp.value); - NSLog(@"Dur: %lld, D: %lld, P: %lld, PO: %lld", duration.value, decodeTimeStamp.value, presentationTimeStamp.value, presentationOutputTimeStamp.value); -#endif + // Get CMBlockBufferRef to extract the actual data later + CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer); + size_t bufferSize = CMBlockBufferGetDataLength(buffer); + + // We have only a sample + // or the format is PCM, if so send only a single buffer to improve performance + if (samplesNum == 1 || (samplesNum > 1 && track.format == kMP42AudioCodecType_LinearPCM)) { + + void *sampleData = malloc(bufferSize); + CMBlockBufferCopyDataBytes(buffer, 0, bufferSize, sampleData); // Enqueues the new sample MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; sample->data = sampleData; - sample->size = sampleSize; + sample->size = bufferSize; sample->duration = duration.value; sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; sample->decodeTimestamp = decodeTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value; - sample->timescale = duration.timescale; + sample->timescale = timescale; sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; sample->trackId = track.sourceId; @@ -1008,17 +1009,15 @@ [self enqueue:sample]; [sample release]; -#ifdef SB_AVF_DEBUG - demuxHelper->currentTime += duration.value; -#endif - currentDataLength += sampleSize; - } else if (samplesNum > 1) { - // The CMSampleBufferRef contains more than one sample + currentDataLength += bufferSize; + } + // The CMSampleBufferRef contains more than one sample + else if (samplesNum > 1) { if (!CMSampleBufferDataIsReady(sampleBuffer)) { CMSampleBufferMakeDataReady(sampleBuffer); } - // A CMSampleBufferRef can contains an unknown number of samples, check how many needs to be divided to separated MP42SampleBuffers + // A CMSampleBufferRef can contains an multiple samples, check how many needs to be divided to separated MP42SampleBuffers // First get the array with the timings for each sample CMItemCount timingArrayEntries = 0; CMItemCount timingArrayEntriesNeededOut = 0; @@ -1057,176 +1056,112 @@ continue; } - CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(NULL, sampleBuffer, kCMAttachmentMode_ShouldPropagate); + for (uint64_t i = 0, pos = 0; i < samplesNum; i++) { + CMSampleTimingInfo sampleTimingInfo; + CMTime decodeTimeStamp; + CMTime presentationTimeStamp; - // Get CMBlockBufferRef to extract the actual data later - CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer); - size_t bufferSize = CMBlockBufferGetDataLength(buffer); + size_t sampleSize; + + // If the size of sample timing array is equal to 1, it means every sample has got the same timing + if (timingArrayEntries == 1) { + sampleTimingInfo = timingArrayOut[0]; + decodeTimeStamp = sampleTimingInfo.decodeTimeStamp; + decodeTimeStamp.value = decodeTimeStamp.value + ( sampleTimingInfo.duration.value * i); + + presentationTimeStamp = sampleTimingInfo.presentationTimeStamp; + presentationTimeStamp.value = presentationTimeStamp.value + ( sampleTimingInfo.duration.value * i); + } else { + sampleTimingInfo = timingArrayOut[i]; + decodeTimeStamp = sampleTimingInfo.decodeTimeStamp; + presentationTimeStamp = sampleTimingInfo.presentationTimeStamp; + } + + // If the size of sample size array is equal to 1, it means every sample has got the same size + if (sizeArrayEntries == 1) { + sampleSize = sizeArrayOut[0]; + } else { + sampleSize = sizeArrayOut[i]; + } + + if (!sampleSize) { + continue; + } - // Don't split the buffer if the format is PCM - if (track.format == kMP42AudioCodecType_LinearPCM) { - size_t sampleSize = bufferSize; void *sampleData = malloc(sampleSize); - CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, sampleData); - - CMTime duration = CMSampleBufferGetDuration(sampleBuffer); - CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer); - CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); - CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer); - CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_Default); - - // Read sample attachment, sync to mark the frame as sync - BOOL sync = 1; - BOOL doNotDisplay = NO; - CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO); - if (attachmentsArray) { - for (NSDictionary *dict in (NSArray *)attachmentsArray) { - if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) { - sync = 0; - } - if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) { - doNotDisplay = YES; - } - } + if (pos < bufferSize) { + CMBlockBufferCopyDataBytes(buffer, pos, sampleSize, sampleData); + pos += sampleSize; } // Enqueues the new sample MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; + + if (attachments && i == 0 && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) { + CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments); + CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtEnd); + sample->attachments = (void *)copy; + CFDictionaryRef trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart); + CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); + trimStartTime = CMTimeConvertScale(trimStartTime, sampleTimingInfo.duration.value, kCMTimeRoundingMethod_Default); + currentOutputTimeStamp.value -= trimStartTime.value; + + } + sample->data = sampleData; sample->size = sampleSize; - sample->duration = duration.value; + sample->duration = sampleTimingInfo.duration.value; sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value; - sample->timescale = duration.timescale; + sample->timescale = timescale; sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; sample->trackId = track.sourceId; - sample->attachments = (void *)attachments; + if (attachments && i == (samplesNum - 1) && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) { + CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments); + CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtStart); + sample->attachments = (void *)copy; + CFDictionaryRef trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd); + CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd); + trimEndTime = CMTimeConvertScale(trimEndTime, sampleTimingInfo.duration.value, kCMTimeRoundingMethod_Default); + currentOutputTimeStamp.value -= trimEndTime.value; + } [demuxHelper->editsConstructor addSample:sample]; [self enqueue:sample]; [sample release]; -#ifdef SB_AVF_DEBUG - demuxHelper->currentTime += duration.value; -#endif + currentOutputTimeStamp.value = currentOutputTimeStamp.value + sampleTimingInfo.duration.value; currentDataLength += sampleSize; } - else { - int pos = 0; - CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timingArrayOut[0].duration.timescale, kCMTimeRoundingMethod_Default); - - for (int i = 0; i < samplesNum; i++) { - CMSampleTimingInfo sampleTimingInfo; - __unused CMTime decodeTimeStamp; - CMTime presentationTimeStamp; - - size_t sampleSize; - - // If the size of sample timing array is equal to 1, it means every sample has got the same timing - if (timingArrayEntries == 1) { - sampleTimingInfo = timingArrayOut[0]; - decodeTimeStamp = sampleTimingInfo.decodeTimeStamp; - decodeTimeStamp.value = decodeTimeStamp.value + ( sampleTimingInfo.duration.value * i); - - presentationTimeStamp = sampleTimingInfo.presentationTimeStamp; - presentationTimeStamp.value = presentationTimeStamp.value + ( sampleTimingInfo.duration.value * i); - } else { - sampleTimingInfo = timingArrayOut[i]; - decodeTimeStamp = sampleTimingInfo.decodeTimeStamp; - presentationTimeStamp = sampleTimingInfo.presentationTimeStamp; - } - - // If the size of sample size array is equal to 1, it means every sample has got the same size - if (sizeArrayEntries == 1) { - sampleSize = sizeArrayOut[0]; - } else { - sampleSize = sizeArrayOut[i]; - } - - if (!sampleSize) { - continue; - } - - void *sampleData = malloc(sampleSize); - - if (pos < bufferSize) { - CMBlockBufferCopyDataBytes(buffer, pos, sampleSize, sampleData); - pos += sampleSize; - } - - // Read sample attachment, sync to mark the frame as sync - BOOL sync = 1; - BOOL doNotDisplay = NO; - CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO); - if (attachmentsArray) { - for (NSDictionary *dict in (NSArray *)attachmentsArray) { - if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) { - sync = 0; - } - if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) { - doNotDisplay = YES; - } - } - } - -#ifdef SB_AVF_DEBUG - NSLog(@"D: %lld, P: %lld, PO: %lld", decodeTimeStamp.value, presentationTimeStamp.value, presentationOutputTimeStamp.value); -#endif - - // Enqueues the new sample - MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; - - if (attachments && i == 0 && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) { - CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments); - CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtEnd); - sample->attachments = (void *)copy; - CFDictionaryRef trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart); - CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); - trimStartTime = CMTimeConvertScale(trimStartTime, sampleTimingInfo.duration.value, kCMTimeRoundingMethod_Default); - currentOutputTimeStamp.value -= trimStartTime.value; - - } - - sample->data = sampleData; - sample->size = sampleSize; - sample->duration = sampleTimingInfo.duration.value; - sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; - sample->presentationTimestamp = presentationTimeStamp.value; - sample->presentationOutputTimestamp = currentOutputTimeStamp.value; - sample->timescale = sampleTimingInfo.duration.timescale; - sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; - sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; - sample->trackId = track.sourceId; - - if (attachments && i == (samplesNum - 1) && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) { - CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments); - CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtStart); - sample->attachments = (void *)copy; - CFDictionaryRef trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd); - CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd); - trimEndTime = CMTimeConvertScale(trimEndTime, sampleTimingInfo.duration.value, kCMTimeRoundingMethod_Default); - currentOutputTimeStamp.value -= trimEndTime.value; - } - - [demuxHelper->editsConstructor addSample:sample]; - [self enqueue:sample]; - [sample release]; - -#ifdef SB_AVF_DEBUG - demuxHelper->currentTime += sampleTimingInfo.duration.value; -#endif - currentOutputTimeStamp.value = currentOutputTimeStamp.value + sampleTimingInfo.duration.value; - currentDataLength += sampleSize; - } - } free(timingArrayOut); 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); + + 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; + + [demuxHelper->editsConstructor addSample:sample]; + } } CFRelease(sampleBuffer); @@ -1271,13 +1206,13 @@ for (uint64_t i = 0; i < helper->editsConstructor.editsCount; i++) { CMTimeRange timeRange = helper->editsConstructor.edits[i]; CMTime duration = CMTimeConvertScale(timeRange.duration, timescale, kCMTimeRoundingMethod_Default); - - trackDuration += duration.value; + MP4Timestamp startTime = timeRange.start.value == -1 ? -1 : timeRange.start.value - helper->minDisplayOffset; MP4AddTrackEdit(fileHandle, trackId, MP4_INVALID_EDIT_ID, - timeRange.start.value - helper->minDisplayOffset, + startTime, duration.value, 0); + trackDuration += duration.value; } if (trackDuration) { diff --git a/MP42/MP42EditListsReconstructor.h b/MP42/MP42EditListsReconstructor.h index 0636e5e..f78e515 100644 --- a/MP42/MP42EditListsReconstructor.h +++ b/MP42/MP42EditListsReconstructor.h @@ -18,8 +18,6 @@ */ @interface MP42EditListsReconstructor : NSObject -- (instancetype)initWithMediaFormat:(FourCharCode)format; - - (void)addSample:(MP42SampleBuffer *)sample; - (void)done; diff --git a/MP42/MP42EditListsReconstructor.m b/MP42/MP42EditListsReconstructor.m index fb128e8..1a8d4b1 100644 --- a/MP42/MP42EditListsReconstructor.m +++ b/MP42/MP42EditListsReconstructor.m @@ -10,37 +10,32 @@ #import "MP42MediaFormat.h" #import "MP42Heap.h" -@implementation MP42EditListsReconstructor { +@implementation MP42EditListsReconstructor +{ @private MP42Heap *_priorityQueue; uint64_t _currentMediaTime; CMTimeScale _timescale; - int64_t _delta; - - uint64_t _previousPresentationOutputTimeStamp; - uint64_t _emptyEditPresentationOutputTimestamp; - CMTimeRange *_edits; uint64_t _editsCount; uint64_t _editsSize; BOOL _editOpen; + BOOL _emptyEditOpen; } -- (instancetype)init { - self = [self initWithMediaFormat:0]; - return self; -} - -- (instancetype)initWithMediaFormat:(FourCharCode)format { +- (instancetype)init +{ self = [super init]; + if (self) { _priorityQueue = [[MP42Heap alloc] initWithCapacity:32 comparator:^NSComparisonResult(MP42SampleBuffer * obj1, MP42SampleBuffer * obj2) { return obj2->presentationTimestamp - obj1->presentationTimestamp; }]; } + return self; } @@ -49,14 +44,21 @@ free(_edits); } -- (void)addSample:(MP42SampleBuffer *)sample { - +- (void)addSample:(MP42SampleBuffer *)sample +{ if (sample->attachments) { + // Flush the current queue, because pts time is going to be reset CFBooleanRef resetDecoderBeforeDecoding = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding); if (resetDecoderBeforeDecoding && CFBooleanGetValue(resetDecoderBeforeDecoding) == 1) { [self flush]; } + + // Flush the current queue, because an empty edit is coming + CFBooleanRef emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia); + if (emptyMedia && CFBooleanGetValue(emptyMedia) == 1) { + [self flush]; + } } [_priorityQueue insert:sample]; @@ -67,6 +69,7 @@ } } + - (void)flush { while (!_priorityQueue.isEmpty) { @@ -74,17 +77,14 @@ [self analyzeSample:extractedSample]; } - if (_editOpen == YES) { + if (_editOpen == YES && _emptyEditOpen == NO) { CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale); [self endEditListAtTime:editEnd empty:NO]; } - -#ifdef AVF_DEBUG - NSLog(@"Flush Done"); -#endif } -- (void)done { +- (void)done +{ [self flush]; } @@ -102,35 +102,22 @@ } } - CFDictionaryRef trimStart = NULL, trimEnd = NULL; + CFDictionaryRef trimStart = NULL, trimEnd = NULL, emptyMedia = 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 (sample->presentationOutputTimestamp > sample->presentationTimestamp + _delta) { - _delta = sample->presentationOutputTimestamp - sample->presentationTimestamp; - + if (emptyMedia) { if (_editOpen == YES) { [self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO]; } - if ([self isLastEditEmpty]) { - // If there is already an empty edit list as the last list, append the duration to it - uint64_t editDuration = sample->presentationOutputTimestamp - _emptyEditPresentationOutputTimestamp; - [self expandLastEmptyEdit: CMTimeMake(editDuration, _timescale)]; - } - else { - // Add an empty edit list - uint64_t editDuration = sample->presentationOutputTimestamp - _previousPresentationOutputTimeStamp; - - CMTime editStart = CMTimeMake(_currentMediaTime, _timescale); - [self startEditListAtTime:editStart]; - CMTime editEnd = CMTimeMake(_currentMediaTime + editDuration, _timescale); - [self endEditListAtTime:editEnd empty:YES]; - _emptyEditPresentationOutputTimestamp = sample->presentationOutputTimestamp + sample->duration; - } + CMTime editStart = CMTimeMake(sample->presentationOutputTimestamp, _timescale); + [self startEditListAtTime:editStart]; + _emptyEditOpen = YES; } BOOL shouldStartNewEdit = trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == NO && _editOpen == NO); @@ -139,6 +126,11 @@ 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]; } @@ -172,23 +164,6 @@ [self endEditListAtTime:editEnd empty:NO]; } - - _previousPresentationOutputTimeStamp = sample->presentationOutputTimestamp + trimmedDuration; -} - -/* - * Check if the last edit is an empty edit - */ -- (BOOL)isLastEditEmpty { - if (_editsCount == 0) { return NO; } - return _edits[_editsCount - 1].start.value == -1; -} - -- (void)expandLastEmptyEdit:(CMTime)time { - _edits[_editsCount - 1].duration.value += time.value; -#ifdef AVF_DEBUG - NSLog(@"Expanded empty edit"); -#endif } /** @@ -203,10 +178,6 @@ } _edits[_editsCount] = CMTimeRangeMake(time, kCMTimeInvalid); _editOpen = YES; - -#ifdef AVF_DEBUG - NSLog(@"Started an edit"); -#endif } /** @@ -220,14 +191,8 @@ if (type) { _edits[_editsCount].start.value = -1; -#ifdef AVF_DEBUG - NSLog(@"Closed empty edit"); -#endif } else { -#ifdef AVF_DEBUG - NSLog(@"Closed edit"); -#endif } if (_edits[_editsCount].duration.value > 0) { diff --git a/MP42/MP42FileImporter+Private.h b/MP42/MP42FileImporter+Private.h index c94f490..7bb21e1 100644 --- a/MP42/MP42FileImporter+Private.h +++ b/MP42/MP42FileImporter+Private.h @@ -50,6 +50,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable NSData *)magicCookieForTrack:(MP42Track *)track; - (AudioStreamBasicDescription)audioDescriptionForTrack:(MP42Track *)track; - (BOOL)cleanUp:(MP42FileHandle)fileHandle; +- (BOOL)supportsPreciseTimestamps; @end diff --git a/MP42/MP42FileImporter.m b/MP42/MP42FileImporter.m index 2a81b5a..0d04bb5 100644 --- a/MP42/MP42FileImporter.m +++ b/MP42/MP42FileImporter.m @@ -208,6 +208,11 @@ static NSArray *_supportedFileFormats; userInfo:nil]; } +- (BOOL)supportsPreciseTimestamps +{ + return NO; +} + - (BOOL)cleanUp:(MP4FileHandle)fileHandle { return YES; diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index f847a74..1075f9f 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -198,6 +198,10 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui return @[@"mkv", @"mka", @"mks"]; } +- (BOOL)supportsPreciseTimestamps { + return YES; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { if ((self = [super initWithURL:fileURL])) {