Fix some more sync issues from avfoundation edit lists.

This commit is contained in:
Damiano Galassi committed 2017-09-02 17:06:20 +02:00
1 parent ef3de22096
commit adb8735bf4
6 files changed
+170 -262

No files matched your search

+129 -194
View File
@@ -26,9 +26,6 @@
@interface AVFDemuxHelper : NSObject { @interface AVFDemuxHelper : NSObject {
@public @public
#ifdef SB_AVF_DEBUG
int64_t currentTime;
#endif
int64_t minDisplayOffset; int64_t minDisplayOffset;
AVAssetReaderOutput *assetReaderOutput; AVAssetReaderOutput *assetReaderOutput;
@@ -55,6 +52,10 @@
@"ac3", @"eac3", @"ec3", @"webvtt", @"vtt", @"caf", @"aif", @"aiff", @"aifc", @"wav", @"flac"]; @"ac3", @"eac3", @"ec3", @"webvtt", @"vtt", @"caf", @"aif", @"aiff", @"aifc", @"wav", @"flac"];
} }
- (BOOL)supportsPreciseTimestamps {
return YES;
}
- (FourCharCode)formatForTrack:(AVAssetTrack *)track { - (FourCharCode)formatForTrack:(AVAssetTrack *)track {
FourCharCode result = 0; FourCharCode result = 0;
CMFormatDescriptionRef formatDescription = (CMFormatDescriptionRef)track.formatDescriptions.firstObject; CMFormatDescriptionRef formatDescription = (CMFormatDescriptionRef)track.formatDescriptions.firstObject;
@@ -925,7 +926,7 @@
track.muxer_helper->demuxer_context = [[AVFDemuxHelper alloc] init]; track.muxer_helper->demuxer_context = [[AVFDemuxHelper alloc] init];
demuxHelper = track.muxer_helper->demuxer_context; demuxHelper = track.muxer_helper->demuxer_context;
demuxHelper->assetReaderOutput = assetReaderOutput; demuxHelper->assetReaderOutput = assetReaderOutput;
demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] initWithMediaFormat:track.format]; demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init];
totalDataLength += track.dataLength; totalDataLength += track.dataLength;
} }
@@ -944,57 +945,57 @@
while (!self.isCancelled) { while (!self.isCancelled) {
CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer]; CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
CMTimeScale timescale = [self timescaleForTrack:track];
if (sampleBuffer) { if (sampleBuffer) {
CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer); CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer);
CMTime duration = CMSampleBufferGetDuration(sampleBuffer);
CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer); CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
if (samplesNum == 1) { CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default);
// We have only a sample
CMTime duration = CMSampleBufferGetDuration(sampleBuffer);
CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer); // Read sample attachment, to mark the frame as sync
size_t sampleSize = CMBlockBufferGetDataLength(buffer); BOOL sync = 1;
void *sampleData = malloc(sampleSize); BOOL doNotDisplay = NO;
CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, sampleData); CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO);
if (attachmentsArray) {
// Read sample attachment, sync to mark the frame as sync for (NSDictionary *dict in (NSArray *)attachmentsArray) {
BOOL sync = YES; if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) {
BOOL doNotDisplay = NO; sync = 0;
CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO); }
if (attachmentsArray) { if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) {
for (NSDictionary *dict in (NSArray *)attachmentsArray) { doNotDisplay = YES;
if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) {
sync = NO;
}
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 // Get CMBlockBufferRef to extract the actual data later
NSLog(@"Delta: %lld", presentationTimeStamp.value - currentOutputTimeStamp.value); CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
NSLog(@"C: %lld, P: %lld, PO: %lld", demuxHelper->currentTime, presentationTimeStamp.value, currentOutputTimeStamp.value); size_t bufferSize = CMBlockBufferGetDataLength(buffer);
NSLog(@"Dur: %lld, D: %lld, P: %lld, PO: %lld", duration.value, decodeTimeStamp.value, presentationTimeStamp.value, presentationOutputTimeStamp.value);
#endif // 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 // Enqueues the new sample
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
sample->data = sampleData; sample->data = sampleData;
sample->size = sampleSize; sample->size = bufferSize;
sample->duration = duration.value; sample->duration = duration.value;
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->decodeTimestamp = decodeTimeStamp.value; sample->decodeTimestamp = decodeTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value;
sample->presentationOutputTimestamp = currentOutputTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
sample->timescale = duration.timescale; sample->timescale = timescale;
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
sample->trackId = track.sourceId; sample->trackId = track.sourceId;
@@ -1008,17 +1009,15 @@
[self enqueue:sample]; [self enqueue:sample];
[sample release]; [sample release];
#ifdef SB_AVF_DEBUG currentDataLength += bufferSize;
demuxHelper->currentTime += duration.value; }
#endif // The CMSampleBufferRef contains more than one sample
currentDataLength += sampleSize; else if (samplesNum > 1) {
} else if (samplesNum > 1) {
// The CMSampleBufferRef contains more than one sample
if (!CMSampleBufferDataIsReady(sampleBuffer)) { if (!CMSampleBufferDataIsReady(sampleBuffer)) {
CMSampleBufferMakeDataReady(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 // First get the array with the timings for each sample
CMItemCount timingArrayEntries = 0; CMItemCount timingArrayEntries = 0;
CMItemCount timingArrayEntriesNeededOut = 0; CMItemCount timingArrayEntriesNeededOut = 0;
@@ -1057,49 +1056,104 @@
continue; 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 size_t sampleSize;
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
size_t bufferSize = CMBlockBufferGetDataLength(buffer); // 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); void *sampleData = malloc(sampleSize);
CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, sampleData); if (pos < bufferSize) {
CMBlockBufferCopyDataBytes(buffer, pos, sampleSize, sampleData);
CMTime duration = CMSampleBufferGetDuration(sampleBuffer); pos += sampleSize;
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;
}
}
} }
// Enqueues the new sample // Enqueues the new sample
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; 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->data = sampleData;
sample->size = sampleSize; sample->size = sampleSize;
sample->duration = duration.value; sample->duration = sampleTimingInfo.duration.value;
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value;
sample->presentationOutputTimestamp = currentOutputTimeStamp.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;
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];
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 |= sync ? MP42SampleBufferFlagIsSync : 0;
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
sample->trackId = track.sourceId; sample->trackId = track.sourceId;
@@ -1107,126 +1161,7 @@
sample->attachments = (void *)attachments; sample->attachments = (void *)attachments;
[demuxHelper->editsConstructor addSample:sample]; [demuxHelper->editsConstructor addSample:sample];
[self enqueue:sample];
[sample release];
#ifdef SB_AVF_DEBUG
demuxHelper->currentTime += duration.value;
#endif
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);
} }
CFRelease(sampleBuffer); CFRelease(sampleBuffer);
@@ -1271,13 +1206,13 @@
for (uint64_t i = 0; i < helper->editsConstructor.editsCount; i++) { for (uint64_t i = 0; i < helper->editsConstructor.editsCount; i++) {
CMTimeRange timeRange = helper->editsConstructor.edits[i]; CMTimeRange timeRange = helper->editsConstructor.edits[i];
CMTime duration = CMTimeConvertScale(timeRange.duration, timescale, kCMTimeRoundingMethod_Default); CMTime duration = CMTimeConvertScale(timeRange.duration, timescale, kCMTimeRoundingMethod_Default);
MP4Timestamp startTime = timeRange.start.value == -1 ? -1 : timeRange.start.value - helper->minDisplayOffset;
trackDuration += duration.value;
MP4AddTrackEdit(fileHandle, trackId, MP4_INVALID_EDIT_ID, MP4AddTrackEdit(fileHandle, trackId, MP4_INVALID_EDIT_ID,
timeRange.start.value - helper->minDisplayOffset, startTime,
duration.value, 0); duration.value, 0);
trackDuration += duration.value;
} }
if (trackDuration) { if (trackDuration) {
-2
View File
@@ -18,8 +18,6 @@
*/ */
@interface MP42EditListsReconstructor : NSObject @interface MP42EditListsReconstructor : NSObject
- (instancetype)initWithMediaFormat:(FourCharCode)format;
- (void)addSample:(MP42SampleBuffer *)sample; - (void)addSample:(MP42SampleBuffer *)sample;
- (void)done; - (void)done;
+31 -66
View File
@@ -10,37 +10,32 @@
#import "MP42MediaFormat.h" #import "MP42MediaFormat.h"
#import "MP42Heap.h" #import "MP42Heap.h"
@implementation MP42EditListsReconstructor { @implementation MP42EditListsReconstructor
{
@private @private
MP42Heap<MP42SampleBuffer *> *_priorityQueue; MP42Heap<MP42SampleBuffer *> *_priorityQueue;
uint64_t _currentMediaTime; uint64_t _currentMediaTime;
CMTimeScale _timescale; CMTimeScale _timescale;
int64_t _delta;
uint64_t _previousPresentationOutputTimeStamp;
uint64_t _emptyEditPresentationOutputTimestamp;
CMTimeRange *_edits; CMTimeRange *_edits;
uint64_t _editsCount; uint64_t _editsCount;
uint64_t _editsSize; uint64_t _editsSize;
BOOL _editOpen; BOOL _editOpen;
BOOL _emptyEditOpen;
} }
- (instancetype)init { - (instancetype)init
self = [self initWithMediaFormat:0]; {
return self;
}
- (instancetype)initWithMediaFormat:(FourCharCode)format {
self = [super init]; self = [super init];
if (self) { if (self) {
_priorityQueue = [[MP42Heap alloc] initWithCapacity:32 comparator:^NSComparisonResult(MP42SampleBuffer * obj1, MP42SampleBuffer * obj2) { _priorityQueue = [[MP42Heap alloc] initWithCapacity:32 comparator:^NSComparisonResult(MP42SampleBuffer * obj1, MP42SampleBuffer * obj2) {
return obj2->presentationTimestamp - obj1->presentationTimestamp; return obj2->presentationTimestamp - obj1->presentationTimestamp;
}]; }];
} }
return self; return self;
} }
@@ -49,14 +44,21 @@
free(_edits); free(_edits);
} }
- (void)addSample:(MP42SampleBuffer *)sample { - (void)addSample:(MP42SampleBuffer *)sample
{
if (sample->attachments) { if (sample->attachments) {
// Flush the current queue, because pts time is going to be reset // Flush the current queue, because pts time is going to be reset
CFBooleanRef resetDecoderBeforeDecoding = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding); CFBooleanRef resetDecoderBeforeDecoding = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_ResetDecoderBeforeDecoding);
if (resetDecoderBeforeDecoding && CFBooleanGetValue(resetDecoderBeforeDecoding) == 1) { if (resetDecoderBeforeDecoding && CFBooleanGetValue(resetDecoderBeforeDecoding) == 1) {
[self flush]; [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]; [_priorityQueue insert:sample];
@@ -67,6 +69,7 @@
} }
} }
- (void)flush - (void)flush
{ {
while (!_priorityQueue.isEmpty) { while (!_priorityQueue.isEmpty) {
@@ -74,17 +77,14 @@
[self analyzeSample:extractedSample]; [self analyzeSample:extractedSample];
} }
if (_editOpen == YES) { if (_editOpen == YES && _emptyEditOpen == NO) {
CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale); CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale);
[self endEditListAtTime:editEnd empty:NO]; [self endEditListAtTime:editEnd empty:NO];
} }
#ifdef AVF_DEBUG
NSLog(@"Flush Done");
#endif
} }
- (void)done { - (void)done
{
[self flush]; [self flush];
} }
@@ -102,35 +102,22 @@
} }
} }
CFDictionaryRef trimStart = NULL, trimEnd = NULL; CFDictionaryRef trimStart = NULL, trimEnd = NULL, emptyMedia = NULL;
if (sample->attachments) { if (sample->attachments) {
trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart); trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart);
trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd); trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd);
emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia);
} }
// Check if we need to add an empty edit list. // Check if we need to add an empty edit list.
if (sample->presentationOutputTimestamp > sample->presentationTimestamp + _delta) { if (emptyMedia) {
_delta = sample->presentationOutputTimestamp - sample->presentationTimestamp;
if (_editOpen == YES) { if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO]; [self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO];
} }
if ([self isLastEditEmpty]) { CMTime editStart = CMTimeMake(sample->presentationOutputTimestamp, _timescale);
// If there is already an empty edit list as the last list, append the duration to it [self startEditListAtTime:editStart];
uint64_t editDuration = sample->presentationOutputTimestamp - _emptyEditPresentationOutputTimestamp; _emptyEditOpen = YES;
[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;
}
} }
BOOL shouldStartNewEdit = trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == NO && _editOpen == NO); BOOL shouldStartNewEdit = trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == NO && _editOpen == NO);
@@ -139,6 +126,11 @@
if (shouldStartNewEdit) { if (shouldStartNewEdit) {
// Close the current edit list // Close the current edit list
if (_emptyEditOpen == YES) {
_emptyEditOpen = NO;
CMTime editEnd = CMTimeMake(sample->presentationOutputTimestamp, _timescale);
[self endEditListAtTime:editEnd empty:YES];
}
if (_editOpen == YES) { if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO]; [self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO];
} }
@@ -172,23 +164,6 @@
[self endEditListAtTime:editEnd empty:NO]; [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); _edits[_editsCount] = CMTimeRangeMake(time, kCMTimeInvalid);
_editOpen = YES; _editOpen = YES;
#ifdef AVF_DEBUG
NSLog(@"Started an edit");
#endif
} }
/** /**
@@ -220,14 +191,8 @@
if (type) { if (type) {
_edits[_editsCount].start.value = -1; _edits[_editsCount].start.value = -1;
#ifdef AVF_DEBUG
NSLog(@"Closed empty edit");
#endif
} }
else { else {
#ifdef AVF_DEBUG
NSLog(@"Closed edit");
#endif
} }
if (_edits[_editsCount].duration.value > 0) { if (_edits[_editsCount].duration.value > 0) {
+1
View File
@@ -50,6 +50,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable NSData *)magicCookieForTrack:(MP42Track *)track; - (nullable NSData *)magicCookieForTrack:(MP42Track *)track;
- (AudioStreamBasicDescription)audioDescriptionForTrack:(MP42Track *)track; - (AudioStreamBasicDescription)audioDescriptionForTrack:(MP42Track *)track;
- (BOOL)cleanUp:(MP42FileHandle)fileHandle; - (BOOL)cleanUp:(MP42FileHandle)fileHandle;
- (BOOL)supportsPreciseTimestamps;
@end @end
+5
View File
@@ -208,6 +208,11 @@ static NSArray<NSString *> *_supportedFileFormats;
userInfo:nil]; userInfo:nil];
} }
- (BOOL)supportsPreciseTimestamps
{
return NO;
}
- (BOOL)cleanUp:(MP4FileHandle)fileHandle - (BOOL)cleanUp:(MP4FileHandle)fileHandle
{ {
return YES; return YES;
+4
View File
@@ -198,6 +198,10 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui
return @[@"mkv", @"mka", @"mks"]; return @[@"mkv", @"mka", @"mks"];
} }
- (BOOL)supportsPreciseTimestamps {
return YES;
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
{ {
if ((self = [super initWithURL:fileURL])) { if ((self = [super initWithURL:fileURL])) {