Initial support for empty edit lists.

This commit is contained in:
Damiano Galassi committed 2015-11-05 23:17:59 +01:00
1 parent 126fd4de6d
commit e42c2af234
4 files changed
+58 -46

No files matched your search

+7 -3
View File
@@ -785,9 +785,13 @@
AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput; AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput;
while (!_cancelled) { while (!_cancelled) {
CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer]; CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
if (sampleBuffer) { if (sampleBuffer) {
CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer); CMItemCount samplesNum = CMSampleBufferGetNumSamples(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);
@@ -816,7 +820,6 @@
} }
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_Default); 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
@@ -832,7 +835,7 @@
sample->duration = duration.value; sample->duration = duration.value;
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value;
sample->timestamp = currentOutputTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
sample->timescale = duration.timescale; sample->timescale = duration.timescale;
sample->isSync = sync; sample->isSync = sync;
sample->trackId = track.sourceId; sample->trackId = track.sourceId;
@@ -964,11 +967,12 @@
sample->duration = sampleTimingInfo.duration.value; sample->duration = sampleTimingInfo.duration.value;
// FIXME // FIXME
//sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; //sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->timestamp = sampleTimingInfo.presentationTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value;
sample->presentationOutputTimestamp = presentationTimeStamp.value;
sample->timescale = sampleTimingInfo.duration.timescale; sample->timescale = sampleTimingInfo.duration.timescale;
sample->isSync = sync; sample->isSync = sync;
sample->trackId = track.sourceId; sample->trackId = track.sourceId;
sample->doNotDisplay = doNotDisplay;
if (attachmentsSent == NO) { if (attachmentsSent == NO) {
sample->attachments = (void *)attachments; sample->attachments = (void *)attachments;
+8 -8
View File
@@ -21,15 +21,15 @@
@private @private
MP42Heap *_priorityQueue; MP42Heap *_priorityQueue;
uint64_t _currentTime; uint64_t _currentTime;
int64_t _delta; CMTimeScale _timescale;
CMTimeScale _timescale;
int64_t _count;
CMTimeRange *_edits; int64_t _delta;
uint64_t _editsCount;
uint64_t _editsSize; CMTimeRange *_edits;
BOOL _editOpen; uint64_t _editsCount;
uint64_t _editsSize;
BOOL _editOpen;
} }
- (void)addSample:(MP42SampleBuffer *)sample; - (void)addSample:(MP42SampleBuffer *)sample;
+41 -34
View File
@@ -16,23 +16,31 @@
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
if (self) { if (self) {
_priorityQueue = [[MP42Heap alloc] initWithCapacity:32 andComparator:^NSComparisonResult(id obj1, id obj2) { _priorityQueue = [[MP42Heap alloc] initWithCapacity:32 andComparator:^NSComparisonResult(MP42SampleBuffer * obj1, MP42SampleBuffer * obj2) {
return ((MP42SampleBuffer *)obj2)->presentationTimestamp - ((MP42SampleBuffer *)obj1)->presentationTimestamp; return obj2->presentationTimestamp - obj1->presentationTimestamp;
}]; }];
_count = 1;
} }
return self; return self;
} }
- (void)addSample:(MP42SampleBuffer *)sample { - (void)addSample:(MP42SampleBuffer *)sample {
[sample retain]; [sample retain];
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) {
[self flush];
}
}
[_priorityQueue insert:sample]; [_priorityQueue insert:sample];
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->offset != 0) { if (sample->presentationTimestamp != 0) {
_currentTime += sample->offset - sample->timestamp; _currentTime += sample->presentationTimestamp;
} }
} }
@@ -43,49 +51,53 @@
} }
} }
- (void)done { - (void)flush
while (![_priorityQueue isEmpty]) { {
while (!_priorityQueue.isEmpty) {
MP42SampleBuffer *extractedSample = [_priorityQueue extract]; MP42SampleBuffer *extractedSample = [_priorityQueue extract];
if (_timescale == 0) {
_timescale = extractedSample->timescale;
[self startEditListAtTime:CMTimeMake(extractedSample->presentationTimestamp - extractedSample->timestamp, _timescale)];
}
[self analyzeSample:extractedSample]; [self analyzeSample:extractedSample];
[extractedSample release]; [extractedSample release];
} }
if (_editOpen) { if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO]; [self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
} }
} }
- (void)analyzeSample:(MP42SampleBuffer *)sample { - (void)done {
if (sample->attachments) { [self flush];
#ifdef AVF_DEBUG }
NSLog(@"Attachments found: %@", sample->attachments);
#endif
}
CFDictionaryRef trimStart = NULL, trimEnd = NULL, emptyMedia = NULL; - (void)analyzeSample:(MP42SampleBuffer *)sample {
CFDictionaryRef trimStart = NULL, trimEnd = 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);
} }
if (emptyMedia) {
#ifdef AVF_DEBUG #ifdef AVF_DEBUG
NSLog(@"Empty edit"); NSLog(@"T: %llu, P: %llu, PO: %llu", _currentTime, sample->presentationTimestamp, sample->presentationOutputTimestamp);
#endif #endif
if (sample->presentationOutputTimestamp > sample->presentationTimestamp + _delta) {
_delta = sample->presentationOutputTimestamp - sample->presentationTimestamp;
if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentTime, _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];
} }
BOOL shouldStartNewEdit = trimStart || (sample->doNotDisplay == NO && _editOpen == NO); BOOL shouldStartNewEdit = trimStart || (sample->doNotDisplay == NO && _editOpen == NO);
if (shouldStartNewEdit) { if (shouldStartNewEdit) {
// Close the current edit list // Close the current edit list
if (_editOpen) { if (_editOpen == YES) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO]; [self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
} }
@@ -116,16 +128,14 @@
[self endEditListAtTime:editEnd empty:NO]; [self endEditListAtTime:editEnd empty:NO];
} }
#ifdef AVF_DEBUG
NSLog(@"%llu, T: %llu, C: %llu, O: %llu", _count++, _currentTime, sample->timestamp, sample->offset);
#endif
} }
/** /**
* Starts a new edit * Starts a new edit
*/ */
- (void)startEditListAtTime:(CMTime)time { - (void)startEditListAtTime:(CMTime)time {
NSAssert(!_editOpen, @"Trying to open an edit list when one is already open.");
if (_editsSize <= _editsCount) { if (_editsSize <= _editsCount) {
_editsSize += 20; _editsSize += 20;
_edits = (CMTimeRange *) realloc(_edits, sizeof(CMTimeRange) * _editsSize); _edits = (CMTimeRange *) realloc(_edits, sizeof(CMTimeRange) * _editsSize);
@@ -135,12 +145,10 @@
} }
/** /**
* Closes a opened edit * Closes a open edit
*/ */
- (void)endEditListAtTime:(CMTime)time empty:(BOOL)type { - (void)endEditListAtTime:(CMTime)time empty:(BOOL)type {
if (!_editOpen) { NSAssert(_editOpen, @"Trying to close an edit list when there isn't a open one");
return;
}
time.value -= _edits[_editsCount].start.value; time.value -= _edits[_editsCount].start.value;
_edits[_editsCount].duration = time; _edits[_editsCount].duration = time;
@@ -160,5 +168,4 @@
[super dealloc]; [super dealloc];
} }
@end @end
+2 -1
View File
@@ -23,7 +23,8 @@ typedef NS_OPTIONS(NSUInteger, MP42SampleBufferFlag) {
uint64_t duration; uint64_t duration;
int64_t offset; int64_t offset;
uint64_t presentationTimestamp; int64_t presentationTimestamp;
int64_t presentationOutputTimestamp;
uint64_t timestamp; uint64_t timestamp;
uint32_t trackId; uint32_t trackId;