diff --git a/MP42/MP42Metadata.m b/MP42/MP42Metadata.m index 76db7a3..3f9fd6f 100644 --- a/MP42/MP42Metadata.m +++ b/MP42/MP42Metadata.m @@ -13,145 +13,7 @@ #import "NSString+MP42Additions.h" #import "MP42Ratings.h" - -typedef struct genreType_t { - uint8_t index; - const char *short_name; - const char *english_name; -} genreType_t; - -static const genreType_t genreType_strings[] = { - {1, "blues", "Blues" }, - {2, "classicrock", "Classic Rock" }, - {3, "country", "Country" }, - {4, "dance", "Dance" }, - {5, "disco", "Disco" }, - {6, "funk", "Funk" }, - {7, "grunge", "Grunge" }, - {8, "hiphop", "Hop-Hop" }, - {9, "jazz", "Jazz" }, - {10, "metal", "Metal" }, - {11, "newage", "New Age" }, - {12, "oldies", "Oldies" }, - {13, "other", "Other" }, - {14, "pop", "Pop" }, - {15, "rand_b", "R&B" }, - {16, "rap", "Rap" }, - {17, "reggae", "Reggae" }, - {18, "rock", "Rock" }, - {19, "techno", "Techno" }, - {20, "industrial", "Industrial" }, - {21, "alternative", "Alternative" }, - {22, "ska", "Ska" }, - {23, "deathmetal", "Death Metal" }, - {24, "pranks", "Pranks" }, - {25, "soundtrack", "Soundtrack" }, - {26, "eurotechno", "Euro-Techno" }, - {27, "ambient", "Ambient" }, - {28, "triphop", "Trip-Hop" }, - {29, "vocal", "Vocal" }, - {30, "jazzfunk", "Jazz+Funk" }, - {31, "fusion", "Fusion" }, - {32, "trance", "Trance" }, - {33, "classical", "Classical" }, - {34, "instrumental", "Instrumental" }, - {35, "acid", "Acid" }, - {36, "house", "House" }, - {37, "game", "Game" }, - {38, "soundclip", "Sound Clip" }, - {39, "gospel", "Gospel" }, - {40, "noise", "Noise" }, - {41, "alternrock", "AlternRock" }, - {42, "bass", "Bass" }, - {43, "soul", "Soul" }, - {44, "punk", "Punk" }, - {45, "space", "Space" }, - {46, "meditative", "Meditative" }, - {47, "instrumentalpop", "Instrumental Pop" }, - {48, "instrumentalrock", "Instrumental Rock" }, - {49, "ethnic", "Ethnic" }, - {50, "gothic", "Gothic" }, - {51, "darkwave", "Darkwave" }, - {52, "technoindustrial", "Techno-Industrial" }, - {53, "electronic", "Electronic" }, - {54, "popfolk", "Pop-Folk" }, - {55, "eurodance", "Eurodance" }, - {56, "dream", "Dream" }, - {57, "southernrock", "Southern Rock" }, - {58, "comedy", "Comedy" }, - {59, "cult", "Cult" }, - {60, "gangsta", "Gangsta" }, - {61, "top40", "Top 40" }, - {62, "christianrap", "Christian Rap" }, - {63, "popfunk", "Pop/Funk" }, - {64, "jungle", "Jungle" }, - {65, "nativeamerican", "Native American" }, - {66, "cabaret", "Cabaret" }, - {67, "newwave", "New Wave" }, - {68, "psychedelic", "Psychedelic" }, - {69, "rave", "Rave" }, - {70, "showtunes", "Showtunes" }, - {71, "trailer", "Trailer" }, - {72, "lofi", "Lo-Fi" }, - {73, "tribal", "Tribal" }, - {74, "acidpunk", "Acid Punk" }, - {75, "acidjazz", "Acid Jazz" }, - {76, "polka", "Polka" }, - {77, "retro", "Retro" }, - {78, "musical", "Musical" }, - {79, "rockand_roll", "Rock & Roll" }, - - {80, "hardrock", "Hard Rock" }, - {81, "folk", "Folk" }, - {82, "folkrock", "Folk-Rock" }, - {83, "nationalfolk", "National Folk" }, - {84, "swing", "Swing" }, - {85, "fastfusion", "Fast Fusion" }, - {86, "bebob", "Bebob" }, - {87, "latin", "Latin" }, - {88, "revival", "Revival" }, - {89, "celtic", "Celtic" }, - {90, "bluegrass", "Bluegrass" }, - {91, "avantgarde", "Avantgarde" }, - {92, "gothicrock", "Gothic Rock" }, - {93, "progressiverock", "Progressive Rock" }, - {94, "psychedelicrock", "Psychedelic Rock" }, - {95, "symphonicrock", "SYMPHONIC_ROCK" }, - {96, "slowrock", "Slow Rock" }, - {97, "bigband", "Big Band" }, - {98, "chorus", "Chorus" }, - {99, "easylistening", "Easy Listening" }, - {100, "acoustic", "Acoustic" }, - {101, "humour", "Humor" }, - {102, "speech", "Speech" }, - {103, "chanson", "Chason" }, - {104, "opera", "Opera" }, - {105, "chambermusic", "Chamber Music" }, - {106, "sonata", "Sonata" }, - {107, "symphony", "Symphony" }, - {108, "bootybass", "Booty Bass" }, - {109, "primus", "Primus" }, - {110, "porngroove", "Porn Groove" }, - {111, "satire", "Satire" }, - {112, "slowjam", "Slow Jam" }, - {113, "club", "Club" }, - {114, "tango", "Tango" }, - {115, "samba", "Samba" }, - {116, "folklore", "Folklore" }, - {117, "ballad", "Ballad" }, - {118, "powerballad", "Power Ballad" }, - {119, "rhythmicsoul", "Rhythmic Soul" }, - {120, "freestyle", "Freestyle" }, - {121, "duet", "Duet" }, - {122, "punkrock", "Punk Rock" }, - {123, "drumsolo", "Drum Solo" }, - {124, "acapella", "A capella" }, - {125, "eurohouse", "Euro-House" }, - {126, "dancehall", "Dance Hall" }, - {255, "none", "none" }, - - {0, "undefined" } // must be last -}; +#import "MP42MetadataUtilities.h" @interface MP42Metadata () @@ -513,26 +375,6 @@ static const genreType_t genreType_strings[] = { return @""; } -- (NSString *)genreFromIndex:(NSInteger)index { - if ((index >= 0 && index < 127) || index == 255) { - genreType_t *genre = (genreType_t *) genreType_strings; - genre += index - 1; - return [NSString stringWithUTF8String:genre->english_name]; - } - else return nil; -} - -- (NSInteger)genreIndexFromString:(NSString *)genreString { - NSInteger genreIndex = 0; - genreType_t *genreList; - NSInteger k = 0; - for (genreList = (genreType_t *) genreType_strings; genreList->english_name; genreList++, k++ ) { - if ([genreString isEqualToString:[NSString stringWithUTF8String:genreList->english_name]]) - genreIndex = k + 1; - } - return genreIndex; -} - #pragma mark - Mutators - (void)mergeMetadata:(MP42Metadata *)metadata { @@ -659,7 +501,7 @@ static const genreType_t genreType_strings[] = { [self addMetadataItemWithUTF8String:tags->genre identifier:MP42MetadataKeyUserGenre]; } if (tags->genreType && !tags->genre) { - NSString *genre = [self genreFromIndex:*tags->genreType]; + NSString *genre = genreFromIndex(*tags->genreType); if (genre) { [self addMetadataItemWithString:genre identifier:MP42MetadataKeyUserGenre]; } @@ -962,7 +804,7 @@ static const genreType_t genreType_strings[] = { MP4TagsSetComposer (tags, self.itemsMap[MP42MetadataKeyComposer].stringValue.UTF8String); MP4TagsSetComments (tags, self.itemsMap[MP42MetadataKeyUserComment].stringValue.UTF8String); - uint16_t genreType = [self genreIndexFromString:self.itemsMap[MP42MetadataKeyUserGenre].stringValue]; + uint16_t genreType = genreIndexFromString(self.itemsMap[MP42MetadataKeyUserGenre].stringValue); if (genreType) { MP4TagsSetGenre(tags, NULL); MP4TagsSetGenreType(tags, &genreType); diff --git a/MP42/MP42MetadataItem.m b/MP42/MP42MetadataItem.m index 3d33258..24e2193 100644 --- a/MP42/MP42MetadataItem.m +++ b/MP42/MP42MetadataItem.m @@ -10,6 +10,7 @@ #import "MP42Metadata.h" #import "MP42Image.h" #import "NSString+MP42Additions.h" +#import "MP42MetadataUtilities.h" @implementation MP42MetadataItem @@ -50,8 +51,6 @@ static NSDictionary *_defaultTypes; } } -#import - - (instancetype)initWithIdentifier:(NSString *)identifier value:(id)value dataType:(MP42MetadataItemDataType)dataType @@ -147,6 +146,7 @@ static NSDictionary *_defaultTypes; break; case MP42MetadataItemDataTypeDate: NSAssert(NO, @"Unhandled conversion"); + _value = nil; break; case MP42MetadataItemDataTypeStringArray: _value = @[numberValue.stringValue]; @@ -156,6 +156,7 @@ static NSDictionary *_defaultTypes; break; default: NSAssert(NO, @"Unhandled conversion"); + _value = nil; } } @@ -164,19 +165,29 @@ static NSDictionary *_defaultTypes; NSData *dataValue = (NSData *)_value; switch (_dataType) { case MP42MetadataItemDataTypeString: - NSAssert(NO, @"Unhandled conversion"); + { + if (dataValue.length >= 2) { + uint8_t *bytes = (uint8_t *)dataValue.bytes; + int genre = ((bytes[0]) << 8) | ((bytes[1])); + _value = genreFromIndex(genre); + } break; + } case MP42MetadataItemDataTypeBool: NSAssert(NO, @"Unhandled conversion"); + _value = nil; break; case MP42MetadataItemDataTypeInteger: NSAssert(NO, @"Unhandled conversion"); + _value = nil; break; case MP42MetadataItemDataTypeDate: NSAssert(NO, @"Unhandled conversion"); + _value = nil; break; case MP42MetadataItemDataTypeStringArray: NSAssert(NO, @"Unhandled conversion"); + _value = nil; break; case MP42MetadataItemDataTypeIntegerArray: { @@ -190,6 +201,7 @@ static NSDictionary *_defaultTypes; } default: NSAssert(NO, @"Unhandled conversion"); + _value = nil; } } diff --git a/MP42/MP42MetadataUtilities.h b/MP42/MP42MetadataUtilities.h new file mode 100644 index 0000000..f901b43 --- /dev/null +++ b/MP42/MP42MetadataUtilities.h @@ -0,0 +1,17 @@ +// +// MP42MetadataUtilities.h +// MP42Foundation +// +// Created by Damiano Galassi on 12/01/17. +// Copyright © 2017 Damiano Galassi. All rights reserved. +// + +#ifndef MP42MetadataUtilities_h +#define MP42MetadataUtilities_h + +#import + +NSString * genreFromIndex(NSInteger index); +NSInteger genreIndexFromString(NSString *genreString); + +#endif /* MP42MetadataUtilities_h */ diff --git a/MP42/MP42MetadataUtilities.m b/MP42/MP42MetadataUtilities.m new file mode 100644 index 0000000..936146a --- /dev/null +++ b/MP42/MP42MetadataUtilities.m @@ -0,0 +1,173 @@ +// +// MP42MetadataUtilities.m +// MP42Foundation +// +// Created by Damiano Galassi on 12/01/17. +// Copyright © 2017 Damiano Galassi. All rights reserved. +// + +#import + +typedef struct genreType_t { + uint8_t index; + const char *short_name; + const char *english_name; +} genreType_t; + +static const genreType_t genreType_strings[] = { + {1, "blues", "Blues" }, + {2, "classicrock", "Classic Rock" }, + {3, "country", "Country" }, + {4, "dance", "Dance" }, + {5, "disco", "Disco" }, + {6, "funk", "Funk" }, + {7, "grunge", "Grunge" }, + {8, "hiphop", "Hop-Hop" }, + {9, "jazz", "Jazz" }, + {10, "metal", "Metal" }, + {11, "newage", "New Age" }, + {12, "oldies", "Oldies" }, + {13, "other", "Other" }, + {14, "pop", "Pop" }, + {15, "rand_b", "R&B" }, + {16, "rap", "Rap" }, + {17, "reggae", "Reggae" }, + {18, "rock", "Rock" }, + {19, "techno", "Techno" }, + {20, "industrial", "Industrial" }, + {21, "alternative", "Alternative" }, + {22, "ska", "Ska" }, + {23, "deathmetal", "Death Metal" }, + {24, "pranks", "Pranks" }, + {25, "soundtrack", "Soundtrack" }, + {26, "eurotechno", "Euro-Techno" }, + {27, "ambient", "Ambient" }, + {28, "triphop", "Trip-Hop" }, + {29, "vocal", "Vocal" }, + {30, "jazzfunk", "Jazz+Funk" }, + {31, "fusion", "Fusion" }, + {32, "trance", "Trance" }, + {33, "classical", "Classical" }, + {34, "instrumental", "Instrumental" }, + {35, "acid", "Acid" }, + {36, "house", "House" }, + {37, "game", "Game" }, + {38, "soundclip", "Sound Clip" }, + {39, "gospel", "Gospel" }, + {40, "noise", "Noise" }, + {41, "alternrock", "AlternRock" }, + {42, "bass", "Bass" }, + {43, "soul", "Soul" }, + {44, "punk", "Punk" }, + {45, "space", "Space" }, + {46, "meditative", "Meditative" }, + {47, "instrumentalpop", "Instrumental Pop" }, + {48, "instrumentalrock", "Instrumental Rock" }, + {49, "ethnic", "Ethnic" }, + {50, "gothic", "Gothic" }, + {51, "darkwave", "Darkwave" }, + {52, "technoindustrial", "Techno-Industrial" }, + {53, "electronic", "Electronic" }, + {54, "popfolk", "Pop-Folk" }, + {55, "eurodance", "Eurodance" }, + {56, "dream", "Dream" }, + {57, "southernrock", "Southern Rock" }, + {58, "comedy", "Comedy" }, + {59, "cult", "Cult" }, + {60, "gangsta", "Gangsta" }, + {61, "top40", "Top 40" }, + {62, "christianrap", "Christian Rap" }, + {63, "popfunk", "Pop/Funk" }, + {64, "jungle", "Jungle" }, + {65, "nativeamerican", "Native American" }, + {66, "cabaret", "Cabaret" }, + {67, "newwave", "New Wave" }, + {68, "psychedelic", "Psychedelic" }, + {69, "rave", "Rave" }, + {70, "showtunes", "Showtunes" }, + {71, "trailer", "Trailer" }, + {72, "lofi", "Lo-Fi" }, + {73, "tribal", "Tribal" }, + {74, "acidpunk", "Acid Punk" }, + {75, "acidjazz", "Acid Jazz" }, + {76, "polka", "Polka" }, + {77, "retro", "Retro" }, + {78, "musical", "Musical" }, + {79, "rockand_roll", "Rock & Roll" }, + + {80, "hardrock", "Hard Rock" }, + {81, "folk", "Folk" }, + {82, "folkrock", "Folk-Rock" }, + {83, "nationalfolk", "National Folk" }, + {84, "swing", "Swing" }, + {85, "fastfusion", "Fast Fusion" }, + {86, "bebob", "Bebob" }, + {87, "latin", "Latin" }, + {88, "revival", "Revival" }, + {89, "celtic", "Celtic" }, + {90, "bluegrass", "Bluegrass" }, + {91, "avantgarde", "Avantgarde" }, + {92, "gothicrock", "Gothic Rock" }, + {93, "progressiverock", "Progressive Rock" }, + {94, "psychedelicrock", "Psychedelic Rock" }, + {95, "symphonicrock", "SYMPHONIC_ROCK" }, + {96, "slowrock", "Slow Rock" }, + {97, "bigband", "Big Band" }, + {98, "chorus", "Chorus" }, + {99, "easylistening", "Easy Listening" }, + {100, "acoustic", "Acoustic" }, + {101, "humour", "Humor" }, + {102, "speech", "Speech" }, + {103, "chanson", "Chason" }, + {104, "opera", "Opera" }, + {105, "chambermusic", "Chamber Music" }, + {106, "sonata", "Sonata" }, + {107, "symphony", "Symphony" }, + {108, "bootybass", "Booty Bass" }, + {109, "primus", "Primus" }, + {110, "porngroove", "Porn Groove" }, + {111, "satire", "Satire" }, + {112, "slowjam", "Slow Jam" }, + {113, "club", "Club" }, + {114, "tango", "Tango" }, + {115, "samba", "Samba" }, + {116, "folklore", "Folklore" }, + {117, "ballad", "Ballad" }, + {118, "powerballad", "Power Ballad" }, + {119, "rhythmicsoul", "Rhythmic Soul" }, + {120, "freestyle", "Freestyle" }, + {121, "duet", "Duet" }, + {122, "punkrock", "Punk Rock" }, + {123, "drumsolo", "Drum Solo" }, + {124, "acapella", "A capella" }, + {125, "eurohouse", "Euro-House" }, + {126, "dancehall", "Dance Hall" }, + {255, "none", "none" }, + + {0, "undefined" } // must be last +}; + +NSString * genreFromIndex(NSInteger index) +{ + if ((index >= 0 && index < 127) || index == 255) { + genreType_t *genre = (genreType_t *) genreType_strings; + genre += index - 1; + return @(genre->english_name); + } + else { + return nil; + } +} + +NSInteger genreIndexFromString(NSString *genreString) +{ + NSInteger genreIndex = 0, k = 0; + + for (genreType_t *genreList = (genreType_t *) genreType_strings; genreList->english_name; genreList++, k++ ) { + if ([genreString isEqualToString:@(genreList->english_name)]) { + genreIndex = k + 1; + } + } + return genreIndex; +} + diff --git a/MP42Foundation.xcodeproj/project.pbxproj b/MP42Foundation.xcodeproj/project.pbxproj index 8e34b3a..88233ce 100644 --- a/MP42Foundation.xcodeproj/project.pbxproj +++ b/MP42Foundation.xcodeproj/project.pbxproj @@ -217,6 +217,8 @@ A9B9C2E3182397E700416A4E /* libuniversaldetector.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A9B9C2C11823944700416A4E /* libuniversaldetector.a */; }; A9B9C4931823986200416A4E /* Quartz.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A9B9C4921823986200416A4E /* Quartz.framework */; }; A9BA1AE91DBF7DBE00FD0B38 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = A9BA1AEB1DBF7DBE00FD0B38 /* Localizable.strings */; }; + A9D764311E278B8100ED2F78 /* MP42MetadataUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = A9D764301E278B8100ED2F78 /* MP42MetadataUtilities.m */; }; + A9D764321E278B8100ED2F78 /* MP42MetadataUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = A9D764301E278B8100ED2F78 /* MP42MetadataUtilities.m */; }; A9DFBDA1195FF5BA00410060 /* MP42Heap.h in Headers */ = {isa = PBXBuildFile; fileRef = A9DFBD9F195FF5BA00410060 /* MP42Heap.h */; }; A9DFBDA3195FF5BA00410060 /* MP42Heap.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DFBDA0195FF5BA00410060 /* MP42Heap.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; A9DFBDA4195FF5BA00410060 /* MP42Heap.m in Sources */ = {isa = PBXBuildFile; fileRef = A9DFBDA0195FF5BA00410060 /* MP42Heap.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; @@ -428,6 +430,8 @@ A9BA1ACD1DBF76FD00FD0B38 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/InfoPlist.strings; sourceTree = ""; }; A9BA1AEA1DBF7DBE00FD0B38 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/Localizable.strings; sourceTree = ""; }; A9D599BA1D4885E0001C22BD /* libswresample.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libswresample.a; path = contrib/ffmpeg/lib/libswresample.a; sourceTree = ""; }; + A9D764301E278B8100ED2F78 /* MP42MetadataUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42MetadataUtilities.m; sourceTree = ""; }; + A9D764351E278B9C00ED2F78 /* MP42MetadataUtilities.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MP42MetadataUtilities.h; sourceTree = ""; }; A9DFBD9F195FF5BA00410060 /* MP42Heap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42Heap.h; sourceTree = ""; }; A9DFBDA0195FF5BA00410060 /* MP42Heap.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MP42Heap.m; sourceTree = ""; }; A9DFBDC91960174500410060 /* MP42EditListsReconstructor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MP42EditListsReconstructor.h; sourceTree = ""; }; @@ -723,6 +727,8 @@ A91C3ACC1BF20CF1008BCF87 /* MP42FormatUtilites.mm */, A925AAAD18379AF800BE84D4 /* MP42Utilities.h */, A925AAAE18379AF800BE84D4 /* MP42Utilities.m */, + A9D764351E278B9C00ED2F78 /* MP42MetadataUtilities.h */, + A9D764301E278B8100ED2F78 /* MP42MetadataUtilities.m */, A9B9C24E1823923100416A4E /* MP42SubUtilities.h */, A9B9C24F1823923100416A4E /* MP42SubUtilities.m */, A9B9C2581823923200416A4E /* MP42XMLReader.h */, @@ -1041,6 +1047,7 @@ A910B60518394EB20064028F /* MP42VobSubImporter.m in Sources */, A91C3ACF1BF20CF1008BCF87 /* MP42FormatUtilites.mm in Sources */, A910B60718394EB20064028F /* MP42MkvImporter.m in Sources */, + A9D764321E278B8100ED2F78 /* MP42MetadataUtilities.m in Sources */, A910B60918394EB20064028F /* MP42Mp4Importer.m in Sources */, A910B60D18394EB20064028F /* MP42SrtImporter.m in Sources */, A9DFBDA5195FF5BA00410060 /* MP42Heap.m in Sources */, @@ -1099,6 +1106,7 @@ A9B9C2751823923200416A4E /* MP42ChapterTrack.m in Sources */, A9B9C2C51823957800416A4E /* MP42Languages.m in Sources */, A9B9C2841823923200416A4E /* MP42Image.m in Sources */, + A9D764311E278B8100ED2F78 /* MP42MetadataUtilities.m in Sources */, A9B9C29E1823923200416A4E /* MP42PrivateUtilities.m in Sources */, A97EA7C91D4B8A2D00257CEA /* FFmpegUtils.m in Sources */, A91C3ACE1BF20CF1008BCF87 /* MP42FormatUtilites.mm in Sources */,