Better edit lists support for the AVFoundation importer.

This commit is contained in:
Damiano Galassi committed 2015-11-04 23:37:50 +01:00
1 parent 98689e43a9
commit 3784e788d8
4 files changed
+44 -26

No files matched your search

+23 -10
View File
@@ -43,7 +43,7 @@
@implementation MP42AVFImporter @implementation MP42AVFImporter
+ (NSArray<NSString *> *)supportedFileFormats { + (NSArray<NSString *> *)supportedFileFormats {
return @[@"mov", @"m2ts", @"ts", @"mts", @"ac3", @"eac3"]; return @[@"mov", @"m2ts", @"ts", @"mts", @"ac3", @"eac3", @"ec3"];
} }
- (NSString *)formatForTrack:(AVAssetTrack *)track { - (NSString *)formatForTrack:(AVAssetTrack *)track {
@@ -95,6 +95,9 @@
case 'ms \0': case 'ms \0':
result = MP42AudioFormatAC3; result = MP42AudioFormatAC3;
break; break;
case kAudioFormatEnhancedAC3:
result = MP42AudioFormatEAC3;
break;
case kAudioFormatMPEGLayer1: case kAudioFormatMPEGLayer1:
case kAudioFormatMPEGLayer2: case kAudioFormatMPEGLayer2:
case kAudioFormatMPEGLayer3: case kAudioFormatMPEGLayer3:
@@ -651,7 +654,7 @@
else if (code == kAudioFormatAppleLossless) { else if (code == kAudioFormatAppleLossless) {
if (cookieSizeOut > 48) { if (cookieSizeOut > 48) {
// Remove unneeded parts of the cookie, as describred in ALACMagicCookieDescription.txt // Remove unneeded parts of the cookie, as described in ALACMagicCookieDescription.txt
magicCookie += 24; magicCookie += 24;
cookieSizeOut = cookieSizeOut - 24 - 8; cookieSizeOut = cookieSizeOut - 24 - 8;
} }
@@ -661,8 +664,10 @@
} }
else if (code == kAudioFormatEnhancedAC3) { else if (code == kAudioFormatEnhancedAC3) {
magicCookie += 2; // dec3 atom
cookieSizeOut = cookieSizeOut - 2; // remove the atom header
magicCookie += 8;
cookieSizeOut = cookieSizeOut - 8;
return [NSData dataWithBytes:magicCookie length:cookieSizeOut]; return [NSData dataWithBytes:magicCookie length:cookieSizeOut];
} }
@@ -793,12 +798,17 @@
CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, sampleData); CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, sampleData);
// 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 = YES;
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 = NO;
}
if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) {
doNotDisplay = YES;
}
} }
} }
@@ -824,6 +834,7 @@
sample->isSync = sync; sample->isSync = sync;
sample->trackId = track.sourceId; sample->trackId = track.sourceId;
sample->attachments = (void *)attachments; sample->attachments = (void *)attachments;
sample->doNotDisplay = doNotDisplay;
[demuxHelper->editsConstructor addSample:sample]; [demuxHelper->editsConstructor addSample:sample];
[self enqueue:sample]; [self enqueue:sample];
@@ -886,7 +897,7 @@
sampleTimingInfo = timingArrayOut[0]; sampleTimingInfo = timingArrayOut[0];
decodeTimeStamp = sampleTimingInfo.decodeTimeStamp; decodeTimeStamp = sampleTimingInfo.decodeTimeStamp;
decodeTimeStamp.value = decodeTimeStamp.value + ( sampleTimingInfo.duration.value * i); decodeTimeStamp.value = decodeTimeStamp.value + ( sampleTimingInfo.duration.value * i);
presentationTimeStamp = sampleTimingInfo.presentationTimeStamp; presentationTimeStamp = sampleTimingInfo.presentationTimeStamp;
presentationTimeStamp.value = presentationTimeStamp.value + ( sampleTimingInfo.duration.value * i); presentationTimeStamp.value = presentationTimeStamp.value + ( sampleTimingInfo.duration.value * i);
} else { } else {
@@ -905,8 +916,9 @@
sampleSize = sizeArrayOut[i]; sampleSize = sizeArrayOut[i];
} }
if (!sampleSize) if (!sampleSize) {
continue; continue;
}
void *sampleData = malloc(sampleSize); void *sampleData = malloc(sampleSize);
@@ -934,7 +946,8 @@
sample->data = sampleData; sample->data = sampleData;
sample->size = sampleSize; sample->size = sampleSize;
sample->duration = sampleTimingInfo.duration.value; sample->duration = sampleTimingInfo.duration.value;
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; // FIXME
//sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->timestamp = sampleTimingInfo.presentationTimeStamp.value; sample->timestamp = sampleTimingInfo.presentationTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value;
sample->timescale = sampleTimingInfo.duration.timescale; sample->timescale = sampleTimingInfo.duration.timescale;
+3 -2
View File
@@ -13,12 +13,13 @@
#import "MP42Sample.h" #import "MP42Sample.h"
/** /**
* Analyzes the sample buffers of a track and tries to recreate an array of edits lists. * Analyzes the sample buffers of a track and tries to recreate an array of edits lists
* by analyzing the doNotDisplay and trimAtStart/End flags
* TO-DO: doesn't work in all cases yet. * TO-DO: doesn't work in all cases yet.
*/ */
@interface MP42EditListsReconstructor : NSObject { @interface MP42EditListsReconstructor : NSObject {
@private @private
MP42Heap *_queue; MP42Heap *_priorityQueue;
int64_t _currentTime; int64_t _currentTime;
int64_t _delta; int64_t _delta;
+17 -14
View File
@@ -16,7 +16,7 @@
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
if (self) { if (self) {
_queue = [[MP42Heap alloc] initWithCapacity:32 andComparator:^NSComparisonResult(id obj1, id obj2) { _priorityQueue = [[MP42Heap alloc] initWithCapacity:32 andComparator:^NSComparisonResult(id obj1, id obj2) {
return ((MP42SampleBuffer *)obj2)->presentationTimestamp - ((MP42SampleBuffer *)obj1)->presentationTimestamp; return ((MP42SampleBuffer *)obj2)->presentationTimestamp - ((MP42SampleBuffer *)obj1)->presentationTimestamp;
}]; }];
_count = 1; _count = 1;
@@ -26,15 +26,13 @@
- (void)addSample:(MP42SampleBuffer *)sample { - (void)addSample:(MP42SampleBuffer *)sample {
[sample retain]; [sample retain];
[_queue insert:sample]; [_priorityQueue insert:sample];
if ([_queue isFull]) { if ([_priorityQueue isFull]) {
MP42SampleBuffer *extractedSample = [_queue 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)];
_currentTime += extractedSample->presentationTimestamp - extractedSample->timestamp;
} }
[self analyzeSample:extractedSample]; [self analyzeSample:extractedSample];
@@ -43,8 +41,8 @@
} }
- (void)done { - (void)done {
while (![_queue isEmpty]) { while (![_priorityQueue isEmpty]) {
MP42SampleBuffer *extractedSample = [_queue 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)];
@@ -71,9 +69,11 @@
if (sample->attachments) { if (sample->attachments) {
// Check if we have to trim the start or end of a sample // Check if we have to trim the start or end of a sample
// If so it means we need to start/end an edit // If so it means we need to start/end an edit
if ((trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart))) { if ((trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) ||
if ([self isEditListOpen]) (sample->doNotDisplay == NO && [self isEditListOpen] == NO)) {
if ([self isEditListOpen]) {
[self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO]; [self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO];
}
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
@@ -89,7 +89,8 @@
_delta = sample->presentationTimestamp - sample->timestamp; _delta = sample->presentationTimestamp - sample->timestamp;
if (sample->attachments) { if (sample->attachments) {
if ((trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd))) { if ((trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) ||
(sample->doNotDisplay == YES && [self isEditListOpen] == YES)) {
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); CMTime editEnd = CMTimeMake(_currentTime - trimEndTime.value, _timescale);
@@ -119,14 +120,16 @@
* 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++;
@@ -142,7 +145,7 @@
} }
- (void)dealloc { - (void)dealloc {
[_queue release]; [_priorityQueue release];
[super dealloc]; [super dealloc];
} }
+1
View File
@@ -30,6 +30,7 @@ typedef NS_OPTIONS(NSUInteger, MP42SampleBufferFlag) {
BOOL isSync; BOOL isSync;
BOOL isForced; BOOL isForced;
BOOL doNotDisplay;
void *attachments; void *attachments;
uint16_t flags; uint16_t flags;