Fix edit lists conversion from AVFoundation that failed with empty edits after a normal edit.
This commit is contained in:
2 files changed
+98
-38
No files matched your search
@@ -14,11 +14,14 @@
|
||||
@private
|
||||
MP42Heap<MP42SampleBuffer *> *_priorityQueue;
|
||||
|
||||
uint64_t _currentTime;
|
||||
CMTimeScale _timescale;
|
||||
uint64_t _currentMediaTime;
|
||||
CMTimeScale _timescale;
|
||||
|
||||
int64_t _delta;
|
||||
|
||||
uint64_t _previousPresentationOutputTimeStamp;
|
||||
uint64_t _emptyEditPresentationOutputTimestamp;
|
||||
|
||||
CMTimeRange *_edits;
|
||||
uint64_t _editsCount;
|
||||
uint64_t _editsSize;
|
||||
@@ -51,7 +54,7 @@
|
||||
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 && _priorityQueue.count) {
|
||||
if (resetDecoderBeforeDecoding && CFBooleanGetValue(resetDecoderBeforeDecoding) == 1) {
|
||||
[self flush];
|
||||
}
|
||||
}
|
||||
@@ -72,9 +75,13 @@
|
||||
}
|
||||
|
||||
if (_editOpen == YES) {
|
||||
CMTime editEnd = CMTimeMake(_currentTime, _timescale);
|
||||
CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale);
|
||||
[self endEditListAtTime:editEnd empty:NO];
|
||||
}
|
||||
|
||||
#ifdef AVF_DEBUG
|
||||
NSLog(@"Flush Done");
|
||||
#endif
|
||||
}
|
||||
|
||||
- (void)done {
|
||||
@@ -84,14 +91,14 @@
|
||||
- (void)analyzeSample:(MP42SampleBuffer *)sample {
|
||||
|
||||
#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
|
||||
|
||||
if (_timescale == 0) {
|
||||
_timescale = sample->timescale;
|
||||
// Re-align things if the first sample pts is not 0
|
||||
if (sample->presentationTimestamp != 0) {
|
||||
_currentTime += sample->presentationTimestamp;
|
||||
_currentMediaTime += sample->presentationTimestamp;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,51 +113,82 @@
|
||||
_delta = sample->presentationOutputTimestamp - sample->presentationTimestamp;
|
||||
|
||||
if (_editOpen == YES) {
|
||||
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
|
||||
[self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO];
|
||||
}
|
||||
|
||||
// Add an empty edit list
|
||||
CMTime editStart = CMTimeMake(_currentTime, _timescale);
|
||||
[self startEditListAtTime:editStart];
|
||||
CMTime editEnd = CMTimeMake(_currentTime + _delta, _timescale);
|
||||
[self endEditListAtTime:editEnd empty:YES];
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL shouldStartNewEdit = trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == NO && _editOpen == NO);
|
||||
|
||||
uint64_t trimmedDuration = sample->duration;
|
||||
|
||||
if (shouldStartNewEdit) {
|
||||
// Close the current edit list
|
||||
if (_editOpen == YES) {
|
||||
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
|
||||
[self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO];
|
||||
}
|
||||
|
||||
// Calculate the new edit start
|
||||
CMTime editStart = CMTimeMake(_currentTime, _timescale);
|
||||
CMTime editStart = CMTimeMake(_currentMediaTime, _timescale);
|
||||
|
||||
if (trimStart) {
|
||||
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
|
||||
trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_QuickTime);
|
||||
trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default);
|
||||
editStart.value += trimStartTime.value;
|
||||
trimmedDuration -= trimStartTime.value;
|
||||
}
|
||||
|
||||
[self startEditListAtTime:editStart];
|
||||
}
|
||||
|
||||
_currentTime += sample->duration;
|
||||
_currentMediaTime += sample->duration;
|
||||
|
||||
BOOL shouldEndEdit = trimEnd || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == YES && _editOpen == YES);
|
||||
|
||||
if (shouldEndEdit) {
|
||||
CMTime editEnd = CMTimeMake(_currentTime, _timescale);
|
||||
CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale);
|
||||
|
||||
if (trimEnd) {
|
||||
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
|
||||
trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_QuickTime);
|
||||
trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default);
|
||||
editEnd.value -= trimEndTime.value;
|
||||
trimmedDuration -= trimEndTime.value;
|
||||
}
|
||||
|
||||
[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);
|
||||
_editOpen = YES;
|
||||
|
||||
#ifdef AVF_DEBUG
|
||||
NSLog(@"Started an edit");
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,6 +220,14 @@
|
||||
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user