diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index ec86c3d..a7ac0a5 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -43,7 +43,7 @@ @implementation MP42AVFImporter + (NSArray *)supportedFileFormats { - return @[@"mov", @"m2ts", @"ts", @"mts", @"ac3", @"eac3"]; + return @[@"mov", @"m2ts", @"ts", @"mts", @"ac3", @"eac3", @"ec3"]; } - (NSString *)formatForTrack:(AVAssetTrack *)track { @@ -95,6 +95,9 @@ case 'ms \0': result = MP42AudioFormatAC3; break; + case kAudioFormatEnhancedAC3: + result = MP42AudioFormatEAC3; + break; case kAudioFormatMPEGLayer1: case kAudioFormatMPEGLayer2: case kAudioFormatMPEGLayer3: @@ -651,7 +654,7 @@ else if (code == kAudioFormatAppleLossless) { 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; cookieSizeOut = cookieSizeOut - 24 - 8; } @@ -661,8 +664,10 @@ } else if (code == kAudioFormatEnhancedAC3) { - magicCookie += 2; - cookieSizeOut = cookieSizeOut - 2; + // dec3 atom + // remove the atom header + magicCookie += 8; + cookieSizeOut = cookieSizeOut - 8; return [NSData dataWithBytes:magicCookie length:cookieSizeOut]; } @@ -793,12 +798,17 @@ CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, sampleData); // Read sample attachment, sync to mark the frame as sync - BOOL sync = 1; + BOOL sync = YES; + BOOL doNotDisplay = NO; CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO); if (attachmentsArray) { for (NSDictionary *dict in (NSArray *)attachmentsArray) { - if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) - sync = 0; + if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) { + sync = NO; + } + if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) { + doNotDisplay = YES; + } } } @@ -824,6 +834,7 @@ sample->isSync = sync; sample->trackId = track.sourceId; sample->attachments = (void *)attachments; + sample->doNotDisplay = doNotDisplay; [demuxHelper->editsConstructor addSample:sample]; [self enqueue:sample]; @@ -886,7 +897,7 @@ sampleTimingInfo = timingArrayOut[0]; decodeTimeStamp = sampleTimingInfo.decodeTimeStamp; decodeTimeStamp.value = decodeTimeStamp.value + ( sampleTimingInfo.duration.value * i); - + presentationTimeStamp = sampleTimingInfo.presentationTimeStamp; presentationTimeStamp.value = presentationTimeStamp.value + ( sampleTimingInfo.duration.value * i); } else { @@ -905,8 +916,9 @@ sampleSize = sizeArrayOut[i]; } - if (!sampleSize) + if (!sampleSize) { continue; + } void *sampleData = malloc(sampleSize); @@ -934,7 +946,8 @@ sample->data = sampleData; sample->size = sampleSize; sample->duration = sampleTimingInfo.duration.value; - sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; + // FIXME + //sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; sample->timestamp = sampleTimingInfo.presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value; sample->timescale = sampleTimingInfo.duration.timescale; diff --git a/MP42/MP42EditListsReconstructor.h b/MP42/MP42EditListsReconstructor.h index b9c0039..8e1152f 100644 --- a/MP42/MP42EditListsReconstructor.h +++ b/MP42/MP42EditListsReconstructor.h @@ -13,12 +13,13 @@ #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. */ @interface MP42EditListsReconstructor : NSObject { @private - MP42Heap *_queue; + MP42Heap *_priorityQueue; int64_t _currentTime; int64_t _delta; diff --git a/MP42/MP42EditListsReconstructor.m b/MP42/MP42EditListsReconstructor.m index afb1b75..283eef9 100644 --- a/MP42/MP42EditListsReconstructor.m +++ b/MP42/MP42EditListsReconstructor.m @@ -16,7 +16,7 @@ - (instancetype)init { self = [super init]; 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; }]; _count = 1; @@ -26,15 +26,13 @@ - (void)addSample:(MP42SampleBuffer *)sample { [sample retain]; - [_queue insert:sample]; + [_priorityQueue insert:sample]; - if ([_queue isFull]) { - MP42SampleBuffer *extractedSample = [_queue extract]; + if ([_priorityQueue isFull]) { + MP42SampleBuffer *extractedSample = [_priorityQueue extract]; if (_timescale == 0) { _timescale = extractedSample->timescale; - [self startEditListAtTime:CMTimeMake(extractedSample->presentationTimestamp - extractedSample->timestamp, _timescale)]; - _currentTime += extractedSample->presentationTimestamp - extractedSample->timestamp; } [self analyzeSample:extractedSample]; @@ -43,8 +41,8 @@ } - (void)done { - while (![_queue isEmpty]) { - MP42SampleBuffer *extractedSample = [_queue extract]; + while (![_priorityQueue isEmpty]) { + MP42SampleBuffer *extractedSample = [_priorityQueue extract]; if (_timescale == 0) { _timescale = extractedSample->timescale; [self startEditListAtTime:CMTimeMake(extractedSample->presentationTimestamp - extractedSample->timestamp, _timescale)]; @@ -71,9 +69,11 @@ if (sample->attachments) { // 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 ((trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart))) { - if ([self isEditListOpen]) + if ((trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) || + (sample->doNotDisplay == NO && [self isEditListOpen] == NO)) { + if ([self isEditListOpen]) { [self endEditListAtTime:CMTimeMake(_currentTime, _timescale) empty:NO]; + } CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart); @@ -89,7 +89,8 @@ _delta = sample->presentationTimestamp - sample->timestamp; 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); trimEndTime = CMTimeConvertScale(trimEndTime, _timescale, kCMTimeRoundingMethod_Default); CMTime editEnd = CMTimeMake(_currentTime - trimEndTime.value, _timescale); @@ -119,14 +120,16 @@ * Closes a opened edit */ - (void)endEditListAtTime:(CMTime)time empty:(BOOL)type { - if (!editOpen) + if (!editOpen) { return; + } time.value -= edits[editsCount].start.value; edits[editsCount].duration = time; - if (type) + if (type) { edits[editsCount].start.value = -1; + } if (edits[editsCount].duration.value > 0) { editsCount++; @@ -142,7 +145,7 @@ } - (void)dealloc { - [_queue release]; + [_priorityQueue release]; [super dealloc]; } diff --git a/MP42/MP42Sample.h b/MP42/MP42Sample.h index d64b433..56b7e8a 100644 --- a/MP42/MP42Sample.h +++ b/MP42/MP42Sample.h @@ -30,6 +30,7 @@ typedef NS_OPTIONS(NSUInteger, MP42SampleBufferFlag) { BOOL isSync; BOOL isForced; + BOOL doNotDisplay; void *attachments; uint16_t flags;