From 4e0d573c5b3e6271d642f5acba81ce9c9094ce03 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Mon, 9 Nov 2015 17:35:52 +0100 Subject: [PATCH] Remove the aac priming delay from the edit list media time. --- MP42/MP42AVFImporter.m | 9 +++--- MP42/MP42EditListsReconstructor.h | 13 ++++++-- MP42/MP42EditListsReconstructor.m | 53 ++++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index 1ae132f..3230a3b 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -768,7 +768,7 @@ track.muxer_helper->demuxer_context = [[AVFDemuxHelper alloc] init]; demuxHelper = track.muxer_helper->demuxer_context; demuxHelper->assetReaderOutput = assetReaderOutput; - demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init]; + demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] initWithMediaFormat:track.format]; totalDataLength += [track dataLength]; } @@ -819,7 +819,7 @@ } } - CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_Default); + CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_QuickTime); CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(NULL, sampleBuffer, kCMAttachmentMode_ShouldPropagate); #ifdef SB_AVF_DEBUG @@ -1033,11 +1033,12 @@ // Add back the new constructed edit lists. for (uint64_t i = 0; i < helper->editsConstructor.editsCount; i++) { CMTimeRange timeRange = helper->editsConstructor.edits[i]; - CMTime duration = CMTimeConvertScale(timeRange.duration, timescale, kCMTimeRoundingMethod_Default); + CMTime duration = CMTimeConvertScale(timeRange.duration, timescale, kCMTimeRoundingMethod_QuickTime); trackDuration += duration.value; - MP4AddTrackEdit(fileHandle, trackId, MP4_INVALID_EDIT_ID, timeRange.start.value - helper->minDisplayOffset, + MP4AddTrackEdit(fileHandle, trackId, MP4_INVALID_EDIT_ID, + timeRange.start.value - helper->minDisplayOffset, duration.value, 0); } diff --git a/MP42/MP42EditListsReconstructor.h b/MP42/MP42EditListsReconstructor.h index b7bd2a1..0cdfce5 100644 --- a/MP42/MP42EditListsReconstructor.h +++ b/MP42/MP42EditListsReconstructor.h @@ -26,12 +26,19 @@ int64_t _delta; - CMTimeRange *_edits; - uint64_t _editsCount; - uint64_t _editsSize; + CMTimeRange *_edits; + uint64_t _editsCount; + uint64_t _editsSize; + BOOL _editOpen; + + FourCharCode _format; + uint64_t _priming; + BOOL _primingUsed; } +- (instancetype)initWithMediaFormat:(NSString *)format; + - (void)addSample:(MP42SampleBuffer *)sample; - (void)done; diff --git a/MP42/MP42EditListsReconstructor.m b/MP42/MP42EditListsReconstructor.m index 169971b..24988e4 100644 --- a/MP42/MP42EditListsReconstructor.m +++ b/MP42/MP42EditListsReconstructor.m @@ -7,6 +7,7 @@ // #import "MP42EditListsReconstructor.h" +#import "MP42MediaFormat.h" @implementation MP42EditListsReconstructor @@ -14,11 +15,25 @@ @synthesize editsCount = _editsCount; - (instancetype)init { + self = [self initWithMediaFormat:0]; + return self; +} + +- (instancetype)initWithMediaFormat:(NSString *)format { self = [super init]; if (self) { _priorityQueue = [[MP42Heap alloc] initWithCapacity:32 andComparator:^NSComparisonResult(MP42SampleBuffer * obj1, MP42SampleBuffer * obj2) { return obj2->presentationTimestamp - obj1->presentationTimestamp; }]; + + + if ([format isEqualToString:MP42AudioFormatAAC]) { + _priming = 2112; + } + else if ([format isEqualToString:MP42AudioFormatHEAAC]) + { + _priming = 4224; + } } return self; } @@ -36,14 +51,6 @@ [_priorityQueue insert:sample]; - if (_timescale == 0) { - _timescale = sample->timescale; - // Re-align things if the first sample pts is not 0 - if (sample->presentationTimestamp != 0) { - _currentTime += sample->presentationTimestamp; - } - } - if ([_priorityQueue isFull]) { MP42SampleBuffer *extractedSample = [_priorityQueue extract]; [self analyzeSample:extractedSample]; @@ -60,7 +67,8 @@ } if (_editOpen == YES) { - [self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO]; + CMTime editEnd = CMTimeMake(_currentTime, _timescale); + [self endEditListAtTime:editEnd empty:NO]; } } @@ -69,16 +77,26 @@ } - (void)analyzeSample:(MP42SampleBuffer *)sample { + +#ifdef AVF_DEBUG + NSLog(@"T: %llu, P: %lld, PO: %lld O: %lld", _currentTime, 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) { + _currentTime += sample->presentationTimestamp; + } + } + CFDictionaryRef trimStart = NULL, trimEnd = NULL; if (sample->attachments) { trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart); trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd); } -#ifdef AVF_DEBUG - NSLog(@"T: %llu, P: %llu, PO: %llu", _currentTime, sample->presentationTimestamp, sample->presentationOutputTimestamp); -#endif - + // Check if we need to add an empty edit list. if (sample->presentationOutputTimestamp > sample->presentationTimestamp + _delta) { _delta = sample->presentationOutputTimestamp - sample->presentationTimestamp; @@ -106,10 +124,15 @@ if (trimStart) { CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); - trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default); + trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_QuickTime); editStart.value += trimStartTime.value; } + if (_priming && _primingUsed == NO) { + editStart.value -= _priming; + _primingUsed = YES; + } + [self startEditListAtTime:editStart]; } @@ -122,7 +145,7 @@ if (trimEnd) { CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd); - trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default); + trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_QuickTime); editEnd.value -= trimEndTime.value; }