Fix edit lists conversion from AVFoundation that failed with empty edits after a normal edit.

This commit is contained in:
Damiano Galassi committed 2017-09-01 22:13:37 +02:00
1 parent 3a7eb08c8b
commit 4d3b21a3ed
2 files changed
+98 -38

No files matched your search

+30 -20
View File
@@ -948,13 +948,13 @@
if (sampleBuffer) { if (sampleBuffer) {
CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer); CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer);
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
if (samplesNum == 1) { if (samplesNum == 1) {
// We have only a sample // We have only a sample
CMTime duration = CMSampleBufferGetDuration(sampleBuffer); CMTime duration = CMSampleBufferGetDuration(sampleBuffer);
CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer); CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer); CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
size_t sampleSize = CMBlockBufferGetDataLength(buffer); size_t sampleSize = CMBlockBufferGetDataLength(buffer);
@@ -976,7 +976,7 @@
} }
} }
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_QuickTime); 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 #ifdef SB_AVF_DEBUG
@@ -991,6 +991,7 @@
sample->size = sampleSize; sample->size = sampleSize;
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->presentationTimestamp = presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value;
sample->presentationOutputTimestamp = currentOutputTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
sample->timescale = duration.timescale; sample->timescale = duration.timescale;
@@ -1073,7 +1074,7 @@
CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer); CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer); CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_QuickTime); CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_Default);
// Read sample attachment, sync to mark the frame as sync // Read sample attachment, sync to mark the frame as sync
BOOL sync = 1; BOOL sync = 1;
@@ -1116,11 +1117,12 @@
} }
else { else {
int pos = 0; int pos = 0;
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timingArrayOut[0].duration.timescale, kCMTimeRoundingMethod_Default);
for (int i = 0; i < samplesNum; i++) { for (int i = 0; i < samplesNum; i++) {
CMSampleTimingInfo sampleTimingInfo; CMSampleTimingInfo sampleTimingInfo;
__unused CMTime decodeTimeStamp; __unused CMTime decodeTimeStamp;
CMTime presentationTimeStamp; CMTime presentationTimeStamp;
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
size_t sampleSize; size_t sampleSize;
@@ -1138,9 +1140,6 @@
presentationTimeStamp = sampleTimingInfo.presentationTimeStamp; presentationTimeStamp = sampleTimingInfo.presentationTimeStamp;
} }
presentationOutputTimeStamp.value = presentationOutputTimeStamp.value + (sampleTimingInfo.duration.value * i /
((double) sampleTimingInfo.duration.timescale / presentationOutputTimeStamp.timescale));
// If the size of sample size array is equal to 1, it means every sample has got the same size // If the size of sample size array is equal to 1, it means every sample has got the same size
if (sizeArrayEntries == 1) { if (sizeArrayEntries == 1) {
sampleSize = sizeArrayOut[0]; sampleSize = sizeArrayOut[0];
@@ -1180,27 +1179,37 @@
// Enqueues the new sample // Enqueues the new sample
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
sample->data = sampleData;
sample->size = sampleSize;
sample->duration = sampleTimingInfo.duration.value;
// FIXME
//sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value;
sample->presentationOutputTimestamp = presentationTimeStamp.value;
sample->timescale = sampleTimingInfo.duration.timescale;
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
sample->trackId = track.sourceId;
if (attachments && i == 0 && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) { if (attachments && i == 0 && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) {
CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments); CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments);
CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtEnd); CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtEnd);
sample->attachments = (void *)copy; 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;
} }
else if (attachments && i == (samplesNum - 1) && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) {
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); CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments);
CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtStart); CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtStart);
sample->attachments = (void *)copy; 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]; [demuxHelper->editsConstructor addSample:sample];
@@ -1210,6 +1219,7 @@
#ifdef SB_AVF_DEBUG #ifdef SB_AVF_DEBUG
demuxHelper->currentTime += sampleTimingInfo.duration.value; demuxHelper->currentTime += sampleTimingInfo.duration.value;
#endif #endif
currentOutputTimeStamp.value = currentOutputTimeStamp.value + sampleTimingInfo.duration.value;
currentDataLength += sampleSize; currentDataLength += sampleSize;
} }
} }
@@ -1260,7 +1270,7 @@
// Add back the new constructed edit lists. // Add back the new constructed edit lists.
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_QuickTime); CMTime duration = CMTimeConvertScale(timeRange.duration, timescale, kCMTimeRoundingMethod_Default);
trackDuration += duration.value; trackDuration += duration.value;
+68 -18
View File
@@ -14,11 +14,14 @@
@private @private
MP42Heap<MP42SampleBuffer *> *_priorityQueue; MP42Heap<MP42SampleBuffer *> *_priorityQueue;
uint64_t _currentTime; uint64_t _currentMediaTime;
CMTimeScale _timescale; CMTimeScale _timescale;
int64_t _delta; 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;
@@ -51,7 +54,7 @@
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 && _priorityQueue.count) { if (resetDecoderBeforeDecoding && CFBooleanGetValue(resetDecoderBeforeDecoding) == 1) {
[self flush]; [self flush];
} }
} }
@@ -72,9 +75,13 @@
} }
if (_editOpen == YES) { if (_editOpen == YES) {
CMTime editEnd = CMTimeMake(_currentTime, _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 {
@@ -84,14 +91,14 @@
- (void)analyzeSample:(MP42SampleBuffer *)sample { - (void)analyzeSample:(MP42SampleBuffer *)sample {
#ifdef AVF_DEBUG #ifdef AVF_DEBUG
NSLog(@"T: %llu, P: %lld, PO: %lld O: %lld", _currentTime, sample->presentationTimestamp, sample->presentationOutputTimestamp, sample->offset); NSLog(@"T: %llu, D: %lld, P: %lld, PO: %lld O: %lld", _currentMediaTime, sample->decodeTimestamp, sample->presentationTimestamp, sample->presentationOutputTimestamp, sample->offset);
#endif #endif
if (_timescale == 0) { if (_timescale == 0) {
_timescale = sample->timescale; _timescale = sample->timescale;
// Re-align things if the first sample pts is not 0 // Re-align things if the first sample pts is not 0
if (sample->presentationTimestamp != 0) { if (sample->presentationTimestamp != 0) {
_currentTime += sample->presentationTimestamp; _currentMediaTime += sample->presentationTimestamp;
} }
} }
@@ -106,51 +113,82 @@
_delta = sample->presentationOutputTimestamp - sample->presentationTimestamp; _delta = sample->presentationOutputTimestamp - sample->presentationTimestamp;
if (_editOpen == YES) { if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO]; [self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO];
} }
// Add an empty edit list if ([self isLastEditEmpty]) {
CMTime editStart = CMTimeMake(_currentTime, _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;
CMTime editEnd = CMTimeMake(_currentTime + _delta, _timescale); [self expandLastEmptyEdit: CMTimeMake(editDuration, _timescale)];
[self endEditListAtTime:editEnd empty:YES]; }
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);
uint64_t trimmedDuration = sample->duration;
if (shouldStartNewEdit) { if (shouldStartNewEdit) {
// Close the current edit list // Close the current edit list
if (_editOpen == YES) { if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO]; [self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO];
} }
// Calculate the new edit start // Calculate the new edit start
CMTime editStart = CMTimeMake(_currentTime, _timescale); CMTime editStart = CMTimeMake(_currentMediaTime, _timescale);
if (trimStart) { if (trimStart) {
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_QuickTime); trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default);
editStart.value += trimStartTime.value; editStart.value += trimStartTime.value;
trimmedDuration -= trimStartTime.value;
} }
[self startEditListAtTime:editStart]; [self startEditListAtTime:editStart];
} }
_currentTime += sample->duration; _currentMediaTime += sample->duration;
BOOL shouldEndEdit = trimEnd || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == YES && _editOpen == YES); BOOL shouldEndEdit = trimEnd || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == YES && _editOpen == YES);
if (shouldEndEdit) { if (shouldEndEdit) {
CMTime editEnd = CMTimeMake(_currentTime, _timescale); CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale);
if (trimEnd) { if (trimEnd) {
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd); CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_QuickTime); trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default);
editEnd.value -= trimEndTime.value; editEnd.value -= trimEndTime.value;
trimmedDuration -= trimEndTime.value;
} }
[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
} }
/** /**
@@ -165,6 +203,10 @@
} }
_edits[_editsCount] = CMTimeRangeMake(time, kCMTimeInvalid); _edits[_editsCount] = CMTimeRangeMake(time, kCMTimeInvalid);
_editOpen = YES; _editOpen = YES;
#ifdef AVF_DEBUG
NSLog(@"Started an edit");
#endif
} }
/** /**
@@ -178,6 +220,14 @@
if (type) { if (type) {
_edits[_editsCount].start.value = -1; _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) { if (_edits[_editsCount].duration.value > 0) {