Remove the big if structure in MP42FileImporter init.

This commit is contained in:
Damiano Galassi authored and Damiano Galassi committed 2015-09-18 09:10:41 +02:00
1 parent ba4aca5cfb
commit 506b145480
11 files changed
+192 -116

No files matched your search

+4
View File
@@ -710,6 +710,10 @@ static bool GetFirstHeader(FILE* inFile)
return true;
}
+ (NSArray<NSString *> *)supportedFileFormats {
return @[@"aac"];
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
{
if ((self = [super initWithURL:fileURL])) {
+3
View File
@@ -255,6 +255,9 @@ static bool GetFirstHeader(FILE* inFile)
return true;
}
+ (NSArray<NSString *> *)supportedFileFormats {
return @[@"ac3"];
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
{
+34 -11
View File
@@ -42,6 +42,10 @@
@implementation MP42AVFImporter
+ (NSArray<NSString *> *)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);
+4
View File
@@ -18,6 +18,10 @@
@implementation MP42CCImporter
+ (NSArray<NSString *> *)supportedFileFormats {
return @[@"scc"];
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
{
if ((self = [super initWithURL:fileURL])) {
+41 -63
View File
@@ -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<Class> *_fileImporters;
static NSArray<Class> *_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<NSString *> *)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;
}
}
+4
View File
@@ -1354,6 +1354,10 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
@implementation MP42H264Importer
+ (NSArray<NSString *> *)supportedFileFormats {
return @[@"264", @"h264"];
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
{
if ((self = [super initWithURL:fileURL])) {
+86 -42
View File
@@ -141,6 +141,10 @@ int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, uint64_t
@implementation MP42MkvImporter
+ (NSArray<NSString *> *)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
+4
View File
@@ -32,6 +32,10 @@
@implementation MP42Mp4Importer
+ (NSArray<NSString *> *)supportedFileFormats {
return @[@"mp4", @"m4v", @"m4a", @"m4r"];
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError;
{
if ((self = [super initWithURL:fileURL])) {
+4
View File
@@ -37,6 +37,10 @@
@implementation MP42QTImporter
+ (NSArray<NSString *> *)supportedFileFormats {
return @[@"mov"];
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
{
if ((self = [super initWithURL:fileURL])) {
+4
View File
@@ -19,6 +19,10 @@
@implementation MP42SrtImporter
+ (NSArray<NSString *> *)supportedFileFormats {
return @[@"srt"];
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
{
if ((self = [super initWithURL:fileURL])) {
+4
View File
@@ -221,6 +221,10 @@ static NSArray<SBVobSubTrack *> * LoadVobSubSubtitles(NSURL *theDirectory, NSStr
@implementation MP42VobSubImporter
+ (NSArray<NSString *> *)supportedFileFormats {
return @[@"idx"];
}
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
{
if ((self = [super initWithURL:fileURL])) {