From 506b145480f9de8a6e8b99e59aba02ed2dacbfad Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Fri, 18 Sep 2015 09:10:41 +0200 Subject: [PATCH] Remove the big if structure in MP42FileImporter init. --- MP42/MP42AACImporter.mm | 4 ++ MP42/MP42AC3Importer.m | 3 + MP42/MP42AVFImporter.m | 45 ++++++++++---- MP42/MP42CCImporter.m | 4 ++ MP42/MP42FileImporter.m | 104 ++++++++++++------------------- MP42/MP42H264Importer.mm | 4 ++ MP42/MP42MkvImporter.m | 128 +++++++++++++++++++++++++------------- MP42/MP42Mp4Importer.m | 4 ++ MP42/MP42QTImporter.m | 4 ++ MP42/MP42SrtImporter.m | 4 ++ MP42/MP42VobSubImporter.m | 4 ++ 11 files changed, 192 insertions(+), 116 deletions(-) diff --git a/MP42/MP42AACImporter.mm b/MP42/MP42AACImporter.mm index e7b385a..5e6aae4 100644 --- a/MP42/MP42AACImporter.mm +++ b/MP42/MP42AACImporter.mm @@ -710,6 +710,10 @@ static bool GetFirstHeader(FILE* inFile) return true; } ++ (NSArray *)supportedFileFormats { + return @[@"aac"]; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { if ((self = [super initWithURL:fileURL])) { diff --git a/MP42/MP42AC3Importer.m b/MP42/MP42AC3Importer.m index efc946f..b62f1ae 100644 --- a/MP42/MP42AC3Importer.m +++ b/MP42/MP42AC3Importer.m @@ -255,6 +255,9 @@ static bool GetFirstHeader(FILE* inFile) return true; } ++ (NSArray *)supportedFileFormats { + return @[@"ac3"]; +} - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index f42b18c..0772b0d 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -42,6 +42,10 @@ @implementation MP42AVFImporter ++ (NSArray *)supportedFileFormats { + return @[@"mov", @"m2ts", @"ts", @"mts", @"eac3"]; +} + - (NSString *)formatForTrack:(AVAssetTrack *)track { NSString *result = @""; CMFormatDescriptionRef formatDescription = (CMFormatDescriptionRef)track.formatDescriptions.firstObject; @@ -606,38 +610,46 @@ } - (NSData *)magicCookieForTrack:(MP42Track *)track { - AVAssetTrack *assetTrack = [_localAsset trackWithTrackID:[track sourceId]]; - CMFormatDescriptionRef formatDescription = NULL; - NSArray *formatDescriptions = assetTrack.formatDescriptions; - if ([formatDescriptions count] > 0) - formatDescription = (CMFormatDescriptionRef)[formatDescriptions objectAtIndex:0]; + AVAssetTrack *assetTrack = [_localAsset trackWithTrackID:track.sourceId]; + CMFormatDescriptionRef formatDescription = (CMFormatDescriptionRef)assetTrack.formatDescriptions.firstObject; if (formatDescription) { + FourCharCode code = CMFormatDescriptionGetMediaSubType(formatDescription); - if ([[assetTrack mediaType] isEqualToString:AVMediaTypeVideo]) { + + if ([assetTrack.mediaType isEqualToString:AVMediaTypeVideo]) { + CFDictionaryRef extentions = CMFormatDescriptionGetExtensions(formatDescription); CFDictionaryRef atoms = CFDictionaryGetValue(extentions, kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms); CFDataRef magicCookie = NULL; - if (code == kCMVideoCodecType_H264) + if (code == kCMVideoCodecType_H264) { magicCookie = CFDictionaryGetValue(atoms, @"avcC"); - else if (code == kCMVideoCodecType_MPEG4Video) + } + else if (code == kCMVideoCodecType_MPEG4Video) { magicCookie = CFDictionaryGetValue(atoms, @"esds"); + } return (NSData *)magicCookie; - } else if ([[assetTrack mediaType] isEqualToString:AVMediaTypeAudio]) { + + } else if ([assetTrack.mediaType isEqualToString:AVMediaTypeAudio]) { + size_t cookieSizeOut; const void *magicCookie = CMAudioFormatDescriptionGetMagicCookie(formatDescription, &cookieSizeOut); if (code == kAudioFormatMPEG4AAC || code == kAudioFormatMPEG4AAC_HE || code == kAudioFormatMPEG4AAC_HE_V2) { + // Extract DecoderSpecific info UInt8 *buffer; int size; ReadESDSDescExt((void*)magicCookie, &buffer, &size, 0); return [NSData dataWithBytes:buffer length:size]; - } else if (code == kAudioFormatAppleLossless) { + + } + else if (code == kAudioFormatAppleLossless) { + if (cookieSizeOut > 48) { // Remove unneeded parts of the cookie, as describred in ALACMagicCookieDescription.txt magicCookie += 24; @@ -645,7 +657,18 @@ } return [NSData dataWithBytes:magicCookie length:cookieSizeOut]; - } else if (code == kAudioFormatAC3) { + + } + + else if (code == kAudioFormatEnhancedAC3) { + magicCookie += 2; + cookieSizeOut = cookieSizeOut - 2; + + return [NSData dataWithBytes:magicCookie length:cookieSizeOut]; + } + + else if (code == kAudioFormatAC3) { + OSStatus err = noErr; size_t channelLayoutSize = 0; const AudioStreamBasicDescription *asbd = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription); diff --git a/MP42/MP42CCImporter.m b/MP42/MP42CCImporter.m index 37d49e2..172e460 100644 --- a/MP42/MP42CCImporter.m +++ b/MP42/MP42CCImporter.m @@ -18,6 +18,10 @@ @implementation MP42CCImporter ++ (NSArray *)supportedFileFormats { + return @[@"scc"]; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { if ((self = [super initWithURL:fileURL])) { diff --git a/MP42/MP42FileImporter.m b/MP42/MP42FileImporter.m index 8998b7a..edb5794 100644 --- a/MP42/MP42FileImporter.m +++ b/MP42/MP42FileImporter.m @@ -15,32 +15,51 @@ #import "MP42AACImporter.h" #import "MP42H264Importer.h" #import "MP42VobSubImporter.h" -#import "MP42Track.h" -#import "MP42Fifo.h" - +#import "MP42AVFImporter.h" #if !__LP64__ #import "MP42QTImporter.h" #endif -#import "MP42AVFImporter.h" +#import "MP42Track.h" +#import "MP42Fifo.h" #import "MP42AudioConverter.h" - -#import "mp4v2.h" -#import "MP42PrivateUtilities.h" #import "MP42Track+Muxer.h" +static NSArray *_fileImporters; +static NSArray *_supportedFileFormats; + @implementation MP42FileImporter ++ (void)initialize { + _fileImporters = [@[[MP42MkvImporter class], + [MP42Mp4Importer class], + [MP42SrtImporter class], + [MP42CCImporter class], + [MP42AC3Importer class], + [MP42H264Importer class], + [MP42VobSubImporter class], +#if !__LP64__ + [MP42QTImporter class], +#endif + [MP42AVFImporter class]] retain]; + + NSMutableArray *formats = [[NSMutableArray alloc] init]; + + for (Class c in _fileImporters) { + [formats addObjectsFromArray:[c supportedFileFormats]]; + } + + _supportedFileFormats = [formats copy]; + [formats release]; +} + + (NSArray *)supportedFileFormats { - return @[@"scc", @"smi", @"txt", @"m4v", @"mp4", - @"m4a", @"m4a", @"mov", @"ts", @"mts", - @"m2ts", @"mkv", @"mka", @"mks", @"h264", - @"264", @"idx", @"aac", @"ac3", @"eac3", @"srt"]; + return _supportedFileFormats; } + (BOOL)canInitWithFileType:(NSString *)fileType { - return [[MP42FileImporter supportedFileFormats] containsObject:fileType.lowercaseString]; + return [[self supportedFileFormats] containsObject:fileType.lowercaseString]; } - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)error; @@ -48,57 +67,16 @@ [self release]; self = nil; - NSString *pathExtension = fileURL.pathExtension; - - if ([pathExtension caseInsensitiveCompare: @"mkv"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"mka"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"mks"] == NSOrderedSame) { - self = [MP42MkvImporter alloc]; - } - else if ([pathExtension caseInsensitiveCompare: @"mp4"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"m4v"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"m4a"] == NSOrderedSame) { - self = [MP42Mp4Importer alloc]; - } - else if ([pathExtension caseInsensitiveCompare: @"srt"] == NSOrderedSame) { - self = [MP42SrtImporter alloc]; - } - else if ([pathExtension caseInsensitiveCompare: @"scc"] == NSOrderedSame) { - self = [MP42CCImporter alloc]; - } - else if ([pathExtension caseInsensitiveCompare: @"ac3"] == NSOrderedSame) { - self = [MP42AC3Importer alloc]; - } - else if ([pathExtension caseInsensitiveCompare: @"aac"] == NSOrderedSame) { - self = [MP42AACImporter alloc]; - } - else if ([pathExtension caseInsensitiveCompare: @"264"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"h264"] == NSOrderedSame) { - self = [MP42H264Importer alloc]; - } - else if ([pathExtension caseInsensitiveCompare: @"idx"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"idx"] == NSOrderedSame) { - self = [MP42VobSubImporter alloc]; - } -#if !__LP64__ - /*else if ([pathExtension caseInsensitiveCompare: @"mov"] == NSOrderedSame) { - self = [MP42QTImporter alloc]; - }*/ -#endif - else if ([pathExtension caseInsensitiveCompare: @"m2ts"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"ts"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"mts"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"mov"] == NSOrderedSame || - [pathExtension caseInsensitiveCompare: @"eac3"] == NSOrderedSame) { - self = [MP42AVFImporter alloc]; - } - - if (self) { - self = [self initWithURL:fileURL error:error]; - - if (self) { - for (MP42Track *track in _tracksArray) - track.muxer_helper->importer = self; + // Initialize the right file importer subclass + for (Class c in _fileImporters) { + if ([c canInitWithFileType:fileURL.pathExtension]) { + self = [[c alloc] initWithURL:fileURL error:error]; + if (self) { + for (MP42Track *track in _tracksArray) { + track.muxer_helper->importer = self; + } + } + return self; } } diff --git a/MP42/MP42H264Importer.mm b/MP42/MP42H264Importer.mm index 3bac7e7..b470a6a 100644 --- a/MP42/MP42H264Importer.mm +++ b/MP42/MP42H264Importer.mm @@ -1354,6 +1354,10 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height @implementation MP42H264Importer ++ (NSArray *)supportedFileFormats { + return @[@"264", @"h264"]; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { if ((self = [super initWithURL:fileURL])) { diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index 524648b..68d0f5e 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -141,6 +141,10 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t @implementation MP42MkvImporter ++ (NSArray *)supportedFileFormats { + return @[@"mkv", @"mka", @"mks"]; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { if ((self = [super initWithURL:fileURL])) { @@ -479,10 +483,7 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t - (NSData *)magicCookieForTrack:(MP42Track *)track { - if (!_matroskaFile) - return nil; - - TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, [track sourceId]); + TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, track.sourceId); if ((!strcmp(trackInfo->CodecID, "A_AAC/MPEG4/LC") || !strcmp(trackInfo->CodecID, "A_AAC/MPEG2/LC")) && !trackInfo->CodecPrivateSize) { @@ -493,8 +494,9 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t [magicCookie appendBytes:aac length:2]; return [magicCookie autorelease]; - } else if (!strcmp(trackInfo->CodecID, "A_AC3")) { - mkv_SetTrackMask(_matroskaFile, ~(1 << [track sourceId])); + } + else if (!strcmp(trackInfo->CodecID, "A_AC3") || !strcmp(trackInfo->CodecID, "A_EAC3")) { + mkv_SetTrackMask(_matroskaFile, ~(1 << track.sourceId)); uint64_t StartTime, EndTime, FilePos; uint32_t rt, FrameSize, FrameFlags; @@ -502,47 +504,92 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t // read first header to create track int firstFrame = mkv_ReadFrame(_matroskaFile, 0, &rt, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags); - if (firstFrame != 0) + + if (firstFrame != 0) { return nil; + } if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) { - // parse AC3 header - // collect all the necessary meta information - uint64_t fscod, frmsizecod, bsid, bsmod, acmod, lfeon; - uint32_t lfe_offset = 4; + NSMutableData *magicCookie = nil; - fscod = (*(frame+4) >> 6) & 0x3; - frmsizecod = (*(frame+4) & 0x3f) >> 1; - bsid = (*(frame+5) >> 3) & 0x1f; - bsmod = (*(frame+5) & 0xf); - acmod = (*(frame+6) >> 5) & 0x7; - if (acmod == 2) - lfe_offset -= 2; - else { - if ((acmod & 1) && acmod != 1) - lfe_offset -= 2; - if (acmod & 4) + if (!strcmp(trackInfo->CodecID, "A_AC3")) { + // parse AC3 header + // collect all the necessary meta information + uint64_t fscod, frmsizecod, bsid, bsmod, acmod, lfeon; + uint32_t lfe_offset = 4; + + fscod = (*(frame+4) >> 6) & 0x3; + frmsizecod = (*(frame+4) & 0x3f) >> 1; + bsid = (*(frame+5) >> 3) & 0x1f; + bsmod = (*(frame+5) & 0xf); + acmod = (*(frame+6) >> 5) & 0x7; + if (acmod == 2) { lfe_offset -= 2; + } + else { + if ((acmod & 1) && acmod != 1) { + lfe_offset -= 2; + } + if (acmod & 4) { + lfe_offset -= 2; + } + } + lfeon = (*(frame+6) >> lfe_offset) & 0x1; + + + magicCookie = [[NSMutableData alloc] init]; + [magicCookie appendBytes:&fscod length:sizeof(uint64_t)]; + [magicCookie appendBytes:&bsid length:sizeof(uint64_t)]; + [magicCookie appendBytes:&bsmod length:sizeof(uint64_t)]; + [magicCookie appendBytes:&acmod length:sizeof(uint64_t)]; + [magicCookie appendBytes:&lfeon length:sizeof(uint64_t)]; + [magicCookie appendBytes:&frmsizecod length:sizeof(uint64_t)]; + } + else if (!strcmp(trackInfo->CodecID, "A_EAC3")) { + // parse EAC3 header + + uint64_t strmtyp, substreamid; + uint64_t frmsiz, fscod, bsid, bsmod, acmod, lfeon; + + strmtyp = (*(frame+2) >> 6) & 0x3; + substreamid = (*(frame+2) >> 3) & 0x7; + + frmsiz = (*(frame+2) & 0x7) << 8; + frmsiz += *(frame+3); + + fscod = (*(frame+4) >> 6) & 0x3; + + if (fscod == 0x3) { + + } + else { + + } + + acmod = (*(frame+4) & 0xe) >> 1; + lfeon = (*(frame+4) & 0x1); + bsid = (*(frame+5) >> 3) & 0x1f; + bsmod = 0; + + if (acmod == 0x0) { /* if 1+1 mode (dual mono, so some items need a second value) */ + + } + + if (strmtyp == 0x1) { /* if dependent stream */ + + } } - lfeon = (*(frame+6) >> lfe_offset) & 0x1; mkv_Seek(_matroskaFile, 0, 0); - - NSMutableData *ac3Info = [[NSMutableData alloc] init]; - [ac3Info appendBytes:&fscod length:sizeof(uint64_t)]; - [ac3Info appendBytes:&bsid length:sizeof(uint64_t)]; - [ac3Info appendBytes:&bsmod length:sizeof(uint64_t)]; - [ac3Info appendBytes:&acmod length:sizeof(uint64_t)]; - [ac3Info appendBytes:&lfeon length:sizeof(uint64_t)]; - [ac3Info appendBytes:&frmsizecod length:sizeof(uint64_t)]; - free(frame); - return [ac3Info autorelease]; + return [magicCookie autorelease]; } - else + else { return nil; - } else if (!strcmp(trackInfo->CodecID, "S_VOBSUB")) { + } + } + else if (!strcmp(trackInfo->CodecID, "S_VOBSUB")) { char *string = (char *) trackInfo->CodecPrivate; char *palette = strnstr(string, "palette:", trackInfo->CodecPrivateSize); @@ -558,14 +605,11 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t return [NSData dataWithBytes:colorPalette length:sizeof(UInt32)*16]; } - NSData *magicCookie = [NSData dataWithBytes:trackInfo->CodecPrivate length:trackInfo->CodecPrivateSize]; + if (trackInfo->CodecPrivate && trackInfo->CodecPrivateSize) { + return [NSData dataWithBytes:trackInfo->CodecPrivate length:trackInfo->CodecPrivateSize]; + } - if (magicCookie) { - return magicCookie; - } - else { - return nil; - } + return nil; } // Methods to extract all the samples from the active tracks at the same time diff --git a/MP42/MP42Mp4Importer.m b/MP42/MP42Mp4Importer.m index 350461f..afd5872 100644 --- a/MP42/MP42Mp4Importer.m +++ b/MP42/MP42Mp4Importer.m @@ -32,6 +32,10 @@ @implementation MP42Mp4Importer ++ (NSArray *)supportedFileFormats { + return @[@"mp4", @"m4v", @"m4a", @"m4r"]; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError; { if ((self = [super initWithURL:fileURL])) { diff --git a/MP42/MP42QTImporter.m b/MP42/MP42QTImporter.m index 513648e..e36c7d2 100644 --- a/MP42/MP42QTImporter.m +++ b/MP42/MP42QTImporter.m @@ -37,6 +37,10 @@ @implementation MP42QTImporter ++ (NSArray *)supportedFileFormats { + return @[@"mov"]; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { if ((self = [super initWithURL:fileURL])) { diff --git a/MP42/MP42SrtImporter.m b/MP42/MP42SrtImporter.m index 8b5373b..5baf671 100644 --- a/MP42/MP42SrtImporter.m +++ b/MP42/MP42SrtImporter.m @@ -19,6 +19,10 @@ @implementation MP42SrtImporter ++ (NSArray *)supportedFileFormats { + return @[@"srt"]; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { if ((self = [super initWithURL:fileURL])) { diff --git a/MP42/MP42VobSubImporter.m b/MP42/MP42VobSubImporter.m index 591271c..a384b14 100644 --- a/MP42/MP42VobSubImporter.m +++ b/MP42/MP42VobSubImporter.m @@ -221,6 +221,10 @@ static NSArray * LoadVobSubSubtitles(NSURL *theDirectory, NSStr @implementation MP42VobSubImporter ++ (NSArray *)supportedFileFormats { + return @[@"idx"]; +} + - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError { if ((self = [super initWithURL:fileURL])) {