Write the sgpd/sbgp only when importing tracks, do not write a guessed delay on save on every mp4.

Fixed another crash in avfoundation edit lists code.
This commit is contained in:
Damiano Galassi committed 2017-09-03 11:41:28 +02:00
1 parent adb8735bf4
commit 9f7089b831
8 files changed
+67 -50

No files matched your search

+5 -4
View File
@@ -899,6 +899,11 @@
return result; return result;
} }
- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track;
{
return YES;
}
- (void)demux { - (void)demux {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@@ -1142,15 +1147,12 @@
free(sizeArrayOut); free(sizeArrayOut);
} }
else { else {
if (attachments && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_EmptyMedia)) {
CMTime currentDuration = CMTimeConvertScale(duration, timescale, kCMTimeRoundingMethod_Default); CMTime currentDuration = CMTimeConvertScale(duration, timescale, kCMTimeRoundingMethod_Default);
CMTime currentDecodeTimestamp = CMTimeConvertScale(decodeTimeStamp, timescale, kCMTimeRoundingMethod_Default);
CMTime currentTimeStamp = CMTimeConvertScale(presentationTimeStamp, timescale, kCMTimeRoundingMethod_Default); CMTime currentTimeStamp = CMTimeConvertScale(presentationTimeStamp, timescale, kCMTimeRoundingMethod_Default);
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default); CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default);
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
sample->duration = currentDuration.value; sample->duration = currentDuration.value;
sample->offset = -currentDecodeTimestamp.value + presentationTimeStamp.value;
sample->presentationTimestamp = currentTimeStamp.value; sample->presentationTimestamp = currentTimeStamp.value;
sample->presentationOutputTimestamp = currentOutputTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
sample->timescale = timescale; sample->timescale = timescale;
@@ -1162,7 +1164,6 @@
[demuxHelper->editsConstructor addSample:sample]; [demuxHelper->editsConstructor addSample:sample];
} }
}
CFRelease(sampleBuffer); CFRelease(sampleBuffer);
self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100); self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100);
-15
View File
@@ -54,21 +54,6 @@
_channels = c.channels; _channels = c.channels;
free(pAacConfig); free(pAacConfig);
} }
if (!MP4HaveTrackAtom(fileHandle, self.trackId, "mdia.minf.stbl.sgpd")) {
BOOL addDelay = YES;
uint32_t edits = MP4GetTrackNumberOfEdits(fileHandle, self.trackId);
if (edits == 1) {
MP4Timestamp startTime = MP4GetTrackEditMediaStart(fileHandle, self.trackId, 1);
if (startTime != 0) {
addDelay = NO;
}
}
if (addDelay) {
// Guess a audio encoder delay
self.startOffset = -2112.f / self.timescale * 1000;
}
}
} }
else if ((audioType == MP4_PCM16_LITTLE_ENDIAN_AUDIO_TYPE) || else if ((audioType == MP4_PCM16_LITTLE_ENDIAN_AUDIO_TYPE) ||
(audioType == MP4_PCM16_BIG_ENDIAN_AUDIO_TYPE)) { (audioType == MP4_PCM16_BIG_ENDIAN_AUDIO_TYPE)) {
+31 -31
View File
@@ -58,10 +58,16 @@
CFBooleanRef emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia); CFBooleanRef emptyMedia = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_EmptyMedia);
if (emptyMedia && CFBooleanGetValue(emptyMedia) == 1) { if (emptyMedia && CFBooleanGetValue(emptyMedia) == 1) {
[self flush]; [self flush];
CMTime editStart = CMTimeMake(sample->presentationOutputTimestamp, _timescale);
[self startEditListAtTime:editStart];
_emptyEditOpen = YES;
} }
} }
if (sample->size) {
[_priorityQueue insert:sample]; [_priorityQueue insert:sample];
}
if ([_priorityQueue isFull]) { if ([_priorityQueue isFull]) {
MP42SampleBuffer *extractedSample = [_priorityQueue extract]; MP42SampleBuffer *extractedSample = [_priorityQueue extract];
@@ -69,7 +75,6 @@
} }
} }
- (void)flush - (void)flush
{ {
while (!_priorityQueue.isEmpty) { while (!_priorityQueue.isEmpty) {
@@ -80,6 +85,8 @@
if (_editOpen == YES && _emptyEditOpen == NO) { 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];
_currentMediaTime = 0;
} }
} }
@@ -90,47 +97,38 @@
- (void)analyzeSample:(MP42SampleBuffer *)sample { - (void)analyzeSample:(MP42SampleBuffer *)sample {
#ifdef AVF_DEBUG
NSLog(@"T: %llu, D: %lld, P: %lld, PO: %lld O: %lld", _currentMediaTime, sample->decodeTimestamp, sample->presentationTimestamp, sample->presentationOutputTimestamp, sample->offset);
#endif
if (_timescale == 0) { if (_timescale == 0) {
_timescale = sample->timescale; _timescale = sample->timescale;
// Re-align things if the first sample pts is not 0
if (sample->presentationTimestamp != 0) {
_currentMediaTime += sample->presentationTimestamp;
}
} }
CFDictionaryRef trimStart = NULL, trimEnd = NULL, emptyMedia = NULL; if (_currentMediaTime == 0) {
// Re-align things if the first sample pts is not 0
_currentMediaTime = sample->presentationTimestamp;
}
#ifdef AVF_DEBUG
NSLog(@"T: %llu, D: %lld, P: %lld, PO: %lld O: %lld", _currentMediaTime, sample->decodeTimestamp, sample->presentationTimestamp, sample->presentationOutputTimestamp, sample->offset);
NSLog(@"%d", sample->flags);
#endif
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);
} }
// Check if we need to add an empty edit list. BOOL shouldCloseEmptyEdit = (trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == 0)) && _emptyEditOpen == YES && _editOpen == YES;
if (emptyMedia) {
if (_editOpen == YES) { if (shouldCloseEmptyEdit) {
[self endEditListAtTime:CMTimeMake(_currentMediaTime, _timescale) empty:NO]; CMTime editEnd = CMTimeMake(sample->presentationOutputTimestamp, _timescale);
[self endEditListAtTime:editEnd empty:YES];
_emptyEditOpen = NO;
} }
CMTime editStart = CMTimeMake(sample->presentationOutputTimestamp, _timescale); BOOL shouldStartNewEdit = trimStart || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == 0 && _editOpen == NO && _emptyEditOpen == NO);
[self startEditListAtTime:editStart];
_emptyEditOpen = YES;
}
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 (_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];
} }
@@ -142,7 +140,6 @@
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default); trimStartTime = CMTimeConvertScale(trimStartTime, _timescale, kCMTimeRoundingMethod_Default);
editStart.value += trimStartTime.value; editStart.value += trimStartTime.value;
trimmedDuration -= trimStartTime.value;
} }
[self startEditListAtTime:editStart]; [self startEditListAtTime:editStart];
@@ -150,16 +147,19 @@
_currentMediaTime += sample->duration; _currentMediaTime += sample->duration;
BOOL shouldEndEdit = trimEnd || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) == YES && _editOpen == YES); BOOL shouldEndEdit = (trimEnd || ((sample->flags & MP42SampleBufferFlagDoNotDisplay) != 0)) && _editOpen == YES && _emptyEditOpen == NO;
if (shouldEndEdit) { if (shouldEndEdit) {
CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale); CMTime editEnd = CMTimeMake(_currentMediaTime, _timescale);
if ((sample->flags & MP42SampleBufferFlagDoNotDisplay) != 0) {
editEnd.value -= sample->duration;
}
if (trimEnd) { if (trimEnd) {
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd); CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default); 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];
+2
View File
@@ -50,6 +50,8 @@ 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)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track;
- (BOOL)supportsPreciseTimestamps; - (BOOL)supportsPreciseTimestamps;
@end @end
+5
View File
@@ -208,6 +208,11 @@ static NSArray<NSString *> *_supportedFileFormats;
userInfo:nil]; userInfo:nil];
} }
- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track;
{
return NO;
}
- (BOOL)supportsPreciseTimestamps - (BOOL)supportsPreciseTimestamps
{ {
return NO; return NO;
+17
View File
@@ -784,6 +784,11 @@ static NSString * TrackNameToString(TrackInfo *track)
return result; return result;
} }
- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track;
{
return YES;
}
// Methods to extract all the samples from the active tracks at the same time // Methods to extract all the samples from the active tracks at the same time
- (void)demux - (void)demux
{ {
@@ -1206,6 +1211,18 @@ static NSString * TrackNameToString(TrackInfo *track)
editDuration, editDuration,
0); 0);
} }
else {
MP4Duration editDuration = MP4ConvertFromTrackDuration(fileHandle, trackId,
MP4GetTrackDuration(fileHandle, trackId),
MP4GetTimeScale(fileHandle));
MP4AddTrackEdit(fileHandle,
trackId,
MP4_INVALID_EDIT_ID,
0,
editDuration,
0);
}
} }
return YES; return YES;
+5
View File
@@ -273,6 +273,11 @@
return nil; return nil;
} }
- (BOOL)audioTrackUsesExplicitEncoderDelay:(MP42Track *)track;
{
return MP4HaveTrackAtom(_fileHandle, track.sourceId, "mdia.minf.stbl.sgpd");
}
- (void)demux - (void)demux
{ {
@autoreleasepool { @autoreleasepool {
+2
View File
@@ -268,6 +268,8 @@
magicCookie.length); magicCookie.length);
} }
MP4SetTrackWantsRoll(_fileHandle, dstTrackId, [helper->importer audioTrackUsesExplicitEncoderDelay:track]);
[helper->importer setActiveTrack:track]; [helper->importer setActiveTrack:track];
} }