Some more tweaks to the edit lists constructor.

This commit is contained in:
Damiano Galassi committed 2015-11-05 15:42:53 +01:00
1 parent caa54060f2
commit 126fd4de6d
3 files changed
+82 -57

No files matched your search

+17 -2
View File
@@ -759,8 +759,9 @@
for (MP42Track *track in self.inputTracks) { for (MP42Track *track in self.inputTracks) {
AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[_localAsset trackWithTrackID:track.sourceId] AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[_localAsset trackWithTrackID:track.sourceId]
outputSettings:nil]; outputSettings:nil];
if (![assetReader canAddOutput: assetReaderOutput]) if (![assetReader canAddOutput: assetReaderOutput]) {
NSLog(@"Unable to add the output to assetReader!"); NSLog(@"Unable to add the output to assetReader!");
}
[assetReader addOutput:assetReaderOutput]; [assetReader addOutput:assetReaderOutput];
@@ -885,6 +886,9 @@
continue; continue;
} }
BOOL attachmentsSent = NO;
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(NULL, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
// Get CMBlockBufferRef to extract the actual data later // Get CMBlockBufferRef to extract the actual data later
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer); CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
size_t bufferSize = CMBlockBufferGetDataLength(buffer); size_t bufferSize = CMBlockBufferGetDataLength(buffer);
@@ -935,11 +939,17 @@
// 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;
BOOL doNotDisplay = NO;
CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO); CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO);
if (attachmentsArray) { if (attachmentsArray) {
for (NSDictionary *dict in (NSArray *)attachmentsArray) { for (NSDictionary *dict in (NSArray *)attachmentsArray) {
if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) {
sync = 0; sync = 0;
}
if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) {
doNotDisplay = YES;
}
} }
} }
@@ -960,6 +970,11 @@
sample->isSync = sync; sample->isSync = sync;
sample->trackId = track.sourceId; sample->trackId = track.sourceId;
if (attachmentsSent == NO) {
sample->attachments = (void *)attachments;
attachmentsSent = YES;
}
[demuxHelper->editsConstructor addSample:sample]; [demuxHelper->editsConstructor addSample:sample];
[self enqueue:sample]; [self enqueue:sample];
[sample release]; [sample release];
+5 -5
View File
@@ -21,15 +21,15 @@
@private @private
MP42Heap *_priorityQueue; MP42Heap *_priorityQueue;
int64_t _currentTime; uint64_t _currentTime;
int64_t _delta; int64_t _delta;
CMTimeScale _timescale; CMTimeScale _timescale;
int64_t _count; int64_t _count;
CMTimeRange *edits; CMTimeRange *_edits;
uint64_t editsCount; uint64_t _editsCount;
uint64_t editsSize; uint64_t _editsSize;
BOOL editOpen; BOOL _editOpen;
} }
- (void)addSample:(MP42SampleBuffer *)sample; - (void)addSample:(MP42SampleBuffer *)sample;
+60 -50
View File
@@ -10,8 +10,8 @@
@implementation MP42EditListsReconstructor @implementation MP42EditListsReconstructor
@synthesize edits; @synthesize edits = _edits;
@synthesize editsCount; @synthesize editsCount = _editsCount;
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
@@ -28,14 +28,16 @@
[sample retain]; [sample retain];
[_priorityQueue insert:sample]; [_priorityQueue insert:sample];
if (_timescale == 0) {
_timescale = sample->timescale;
// Re-align things if the first sample pts is not 0
if (sample->offset != 0) {
_currentTime += sample->offset - sample->timestamp;
}
}
if ([_priorityQueue isFull]) { if ([_priorityQueue isFull]) {
MP42SampleBuffer *extractedSample = [_priorityQueue extract]; MP42SampleBuffer *extractedSample = [_priorityQueue extract];
if (_timescale == 0) {
_timescale = extractedSample->timescale;
_currentTime += extractedSample->offset;
}
[self analyzeSample:extractedSample]; [self analyzeSample:extractedSample];
[extractedSample release]; [extractedSample release];
} }
@@ -44,6 +46,7 @@
- (void)done { - (void)done {
while (![_priorityQueue isEmpty]) { while (![_priorityQueue isEmpty]) {
MP42SampleBuffer *extractedSample = [_priorityQueue extract]; MP42SampleBuffer *extractedSample = [_priorityQueue extract];
if (_timescale == 0) { if (_timescale == 0) {
_timescale = extractedSample->timescale; _timescale = extractedSample->timescale;
[self startEditListAtTime:CMTimeMake(extractedSample->presentationTimestamp - extractedSample->timestamp, _timescale)]; [self startEditListAtTime:CMTimeMake(extractedSample->presentationTimestamp - extractedSample->timestamp, _timescale)];
@@ -53,7 +56,7 @@
[extractedSample release]; [extractedSample release];
} }
if ([self isEditListOpen]) { if (_editOpen) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO]; [self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
} }
} }
@@ -65,43 +68,57 @@
#endif #endif
} }
CFDictionaryRef trimStart = NULL, trimEnd = NULL; CFDictionaryRef trimStart = NULL, trimEnd = NULL, emptyMedia = NULL;
if (sample->attachments) { if (sample->attachments) {
// Check if we have to trim the start or end of a sample trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart);
// If so it means we need to start/end an edit trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd);
if ((trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) || emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia);
(sample->doNotDisplay == NO && [self isEditListOpen] == NO)) { }
if ([self isEditListOpen]) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
}
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); if (emptyMedia) {
#ifdef AVF_DEBUG
NSLog(@"Empty edit");
#endif
}
trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default); BOOL shouldStartNewEdit = trimStart || (sample->doNotDisplay == NO && _editOpen == NO);
CMTime editStart = CMTimeMake(_currentTime, _timescale);
editStart.value += trimStartTime.value;
[self startEditListAtTime:editStart]; if (shouldStartNewEdit) {
// Close the current edit list
if (_editOpen) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
} }
// Calculate the new edit start
CMTime editStart = CMTimeMake(_currentTime, _timescale);
if (trimStart) {
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default);
editStart.value += trimStartTime.value;
}
[self startEditListAtTime:editStart];
} }
_currentTime += sample->duration; _currentTime += sample->duration;
_delta = sample->presentationTimestamp - sample->timestamp;
if (sample->attachments) { BOOL shouldEndEdit = trimEnd || (sample->doNotDisplay == YES && _editOpen == YES);
if ((trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) ||
(sample->doNotDisplay == YES && [self isEditListOpen] == YES)) { if (shouldEndEdit) {
CMTime editEnd = CMTimeMake(_currentTime, _timescale);
if (trimEnd) {
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd); CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default); trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default);
CMTime editEnd = CMTimeMake(_currentTime - trimEndTime.value, _timescale); editEnd.value -= trimEndTime.value;
[self endEditListAtTime:editEnd empty:NO];
} }
[self endEditListAtTime:editEnd empty:NO];
} }
#ifdef AVF_DEBUG #ifdef AVF_DEBUG
NSLog(@"%llu, %llu, %llu, %llu", _count++, _delta, _currentTime, sample->timestamp); NSLog(@"%llu, T: %llu, C: %llu, O: %llu", _count++, _currentTime, sample->timestamp, sample->offset);
#endif #endif
} }
@@ -109,40 +126,33 @@
* Starts a new edit * Starts a new edit
*/ */
- (void)startEditListAtTime:(CMTime)time { - (void)startEditListAtTime:(CMTime)time {
if (editsSize <= editsCount) { if (_editsSize <= _editsCount) {
editsSize += 20; _editsSize += 20;
edits = (CMTimeRange *) realloc(edits, sizeof(CMTimeRange) * editsSize); _edits = (CMTimeRange *) realloc(_edits, sizeof(CMTimeRange) * _editsSize);
} }
edits[editsCount] = CMTimeRangeMake(time, kCMTimeInvalid); _edits[_editsCount] = CMTimeRangeMake(time, kCMTimeInvalid);
editOpen = YES; _editOpen = YES;
} }
/** /**
* Closes a opened edit * Closes a opened edit
*/ */
- (void)endEditListAtTime:(CMTime)time empty:(BOOL)type { - (void)endEditListAtTime:(CMTime)time empty:(BOOL)type {
if (!editOpen) { if (!_editOpen) {
return; return;
} }
time.value -= edits[editsCount].start.value; time.value -= _edits[_editsCount].start.value;
edits[editsCount].duration = time; _edits[_editsCount].duration = time;
if (type) { if (type) {
edits[editsCount].start.value = -1; _edits[_editsCount].start.value = -1;
} }
if (edits[editsCount].duration.value > 0) { if (_edits[_editsCount].duration.value > 0) {
editsCount++; _editsCount++;
} }
editOpen = NO; _editOpen = NO;
}
/**
* Returns if there is an open edit list
*/
- (BOOL)isEditListOpen {
return editOpen;
} }
- (void)dealloc { - (void)dealloc {