// // MP42MatroskaTagMapping.m // MP42Foundation // #import "MP42MatroskaTagMapping.h" #import "MP42MetadataFormat.h" @implementation MP42MkvTagPair - (instancetype)initWithSimpleTagName:(NSString *)name metadataKey:(NSString *)key { self = [super init]; if (self) { _simpleTagName = [name copy]; _metadataKey = [key copy]; } return self; } @end // // Read maps. These were previously inline in MP42MkvImporter's // readMatroskaMetadata; they live here now because the tag writer serializes // its inverse and any drift between the two directions silently drops tags. // static NSDictionary *MP42MkvDefaultMap(void) { static NSDictionary *map; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ map = @{ @"TITLE": MP42MetadataKeyName, @"SUBTITLE": MP42MetadataKeyTrackSubTitle, @"ALBUM": MP42MetadataKeyAlbum, @"ARTIST": MP42MetadataKeyArtist, @"COMMENT": MP42MetadataKeyUserComment, @"GENRE": MP42MetadataKeyUserGenre, @"DATE_RELEASED": MP42MetadataKeyReleaseDate, @"BPM": MP42MetadataKeyBeatsPerMin, @"KEYWORDS": MP42MetadataKeyKeywords, @"THANKS_TO": MP42MetadataKeyThanks, @"COPYRIGHT": MP42MetadataKeyCopyright, @"DESCRIPTION": MP42MetadataKeyDescription, @"SYNOPSIS": MP42MetadataKeyLongDescription, @"DIRECTOR_OF_PHOTOGRAPHY": MP42MetadataKeyArtDirector, @"COMPOSER": MP42MetadataKeyComposer, @"ARRANGER": MP42MetadataKeyArranger, @"WRITTEN_BY": MP42MetadataKeyAuthor, @"LYRICS": MP42MetadataKeyLyrics, @"CONDUCTOR": MP42MetadataKeyConductor, @"PUBLISHER": MP42MetadataKeyPublisher, @"SOUND_ENGINEER": MP42MetadataKeySoundEngineer, @"LEAD_PERFORMER": MP42MetadataKeySoloist, @"PRODUCTION_STUDIO": MP42MetadataKeyStudio, @"DIRECTOR": MP42MetadataKeyDirector, @"PRODUCER": MP42MetadataKeyProducer, @"EXECUTIVE_PRODUCER": MP42MetadataKeyExecProducer, @"SCREENPLAY_BY": MP42MetadataKeyScreenwriters, @"CAST": MP42MetadataKeyCast, // Movie-tab fields with no Matroska-spec equivalent; these names // follow the community style (uppercase words, no separators) and // exist mainly so values survive a Subler save/reopen round-trip. @"RATING": MP42MetadataKeyRating, // parental advisory string @"CONTENTRATING": MP42MetadataKeyContentRating, // numeric rating code @"MEDIAKIND": MP42MetadataKeyMediaKind, @"HD": MP42MetadataKeyHDVideo, @"NETWORK": MP42MetadataKeyTVNetwork, @"EPISODEID": MP42MetadataKeyTVEpisodeID, @"ENCODER": MP42MetadataKeyEncodingTool, }; }); return map; } static NSDictionary *MP42MkvCollectionMap(void) { static NSDictionary *map; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ map = @{ @"TITLE": MP42MetadataKeyTVShow, @"COPYRIGHT": MP42MetadataKeyCopyright, @"DESCRIPTION": MP42MetadataKeySeriesDescription, @"SYNOPSIS": MP42MetadataKeySeriesDescription, }; }); return map; } static NSDictionary *MP42MkvSeasonMap(void) { static NSDictionary *map; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ map = @{ @"PART_NUMBER": MP42MetadataKeyTVSeason, @"DESCRIPTION": MP42MetadataKeySeriesDescription, @"SYNOPSIS": MP42MetadataKeySeriesDescription, @"DATE_RELEASED": MP42MetadataKeyReleaseDate, }; }); return map; } NSDictionary *MP42MkvSimpleTagMapForTargetLevel(NSInteger level) { switch (level) { case MP42MkvTargetCollection: return MP42MkvCollectionMap(); case MP42MkvTargetSeason: return MP42MkvSeasonMap(); default: return MP42MkvDefaultMap(); } } BOOL MP42MkvTargetLevelIsModeled(NSInteger level) { // Chapter/scene/shot level tags are skipped by the importer and must be // preserved verbatim by the writer. Everything else (global, part, movie, // season, collection) feeds the metadata model. return level != MP42MkvTargetChapter && level != MP42MkvTargetScene && level != MP42MkvTargetShot; } // Canonical SimpleTag name order per level, used to give the writer stable // output. Every name must exist in the read map for its level; names added // to a read map without being listed here are appended automatically (in // unspecified order) rather than silently dropped. static NSArray *MP42MkvWriteNameOrder(NSInteger level) { switch (level) { case MP42MkvTargetCollection: return @[ @"TITLE", @"SYNOPSIS", @"COPYRIGHT" ]; case MP42MkvTargetSeason: return @[ @"PART_NUMBER", @"SYNOPSIS", @"DATE_RELEASED" ]; default: return @[ @"TITLE", @"ALBUM", @"ARTIST", @"PART_NUMBER", @"TOTAL_PARTS", @"DATE_RELEASED", @"GENRE", @"DESCRIPTION", @"SYNOPSIS", @"COMMENT", @"COPYRIGHT", @"PRODUCTION_STUDIO", @"DIRECTOR", @"PRODUCER", @"EXECUTIVE_PRODUCER", @"SCREENPLAY_BY", @"WRITTEN_BY", @"COMPOSER", @"ARRANGER", @"LYRICS", @"CONDUCTOR", @"PUBLISHER", @"SOUND_ENGINEER", @"LEAD_PERFORMER", @"DIRECTOR_OF_PHOTOGRAPHY", @"CAST", @"RATING", @"CONTENTRATING", @"MEDIAKIND", @"HD", @"NETWORK", @"EPISODEID", @"KEYWORDS", @"THANKS_TO", @"BPM", @"ENCODER", @"SUBTITLE" ]; } } // All metadata keys MP42MkvTagWriter can round-trip, cached for the UI's // "can this field be saved?" hints. Kept here (next to the maps it derives // from) so the UI can never drift from what the writer actually writes. static NSSet *MP42MkvPersistedMetadataKeys(void) { static NSSet *keys; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSMutableSet *mutableKeys = [NSMutableSet setWithObjects: // Regenerated as PART_NUMBER / TOTAL_PARTS MP42MetadataKeyTrackNumber, // Fallback source for movie-level PART_NUMBER // when TrackNumber carries no index MP42MetadataKeyTVEpisodeNumber, // Regenerated as season-level PART_NUMBER // (also a season-map pair key, listed for clarity) MP42MetadataKeyTVSeason, // Stored as an AttachedFile, not a SimpleTag MP42MetadataKeyCoverArt, nil]; for (NSNumber *level in @[ @(MP42MkvTargetMovie), @(MP42MkvTargetSeason), @(MP42MkvTargetCollection) ]) { for (MP42MkvTagPair *pair in MP42MkvWritePairsForTargetLevel(level.integerValue)) { [mutableKeys addObject:pair.metadataKey]; } } keys = [mutableKeys copy]; }); return keys; } BOOL MP42MkvMetadataKeyIsPersisted(NSString *metadataKey) { return metadataKey.length > 0 && [MP42MkvPersistedMetadataKeys() containsObject:metadataKey]; } NSArray *MP42MkvWritePairsForTargetLevel(NSInteger level) { static NSMutableDictionary *> *cache; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ cache = [NSMutableDictionary dictionary]; }); NSNumber *levelKey = @(level); @synchronized (cache) { NSArray *pairs = cache[levelKey]; if (pairs) { return pairs; } NSDictionary *readMap = MP42MkvSimpleTagMapForTargetLevel(level); NSMutableArray *result = [NSMutableArray array]; NSMutableSet *usedKeys = [NSMutableSet set]; for (NSString *name in MP42MkvWriteNameOrder(level)) { NSString *key = readMap[name]; if (key && ![usedKeys containsObject:key]) { [usedKeys addObject:key]; [result addObject:[[MP42MkvTagPair alloc] initWithSimpleTagName:name metadataKey:key]]; } } // Names present in the read map but missing from the order list still // need to be written; append them so both directions stay in sync. for (NSString *name in readMap) { NSString *key = readMap[name]; if (![usedKeys containsObject:key]) { [usedKeys addObject:key]; [result addObject:[[MP42MkvTagPair alloc] initWithSimpleTagName:name metadataKey:key]]; } } pairs = [result copy]; cache[levelKey] = pairs; return pairs; } }