Files
MP42Foundation/MP42/MP42MatroskaTagMapping.h
T
Scott GarrettandClaude Code 1040f47542 Add Matroska tagging and track-removal support
- MP42Ebml: EBML vint/ID primitives, flat Segment scanner, block track
  number decoding, and element builders shared by all Matroska writers.
- MP42MkvImporter / MP42FileImporter: tagsOnly mode that mirrors the
  file's own track flags instead of the MP4 conversion heuristics.
- MP42MatroskaTagMapping: read/write SimpleTag maps shared by importer
  and writer, including community names for MP4-only movie fields.
- MP42MkvTagWriter: mkvpropedit-style in-place rewrite of Tags, cover
  art, and per-track name/language/enabled/forced fields with a
  size+mtime staleness guard; an untouched save is a byte-for-byte
  no-op.
- MP42MkvRemuxer: stream-copy rebuild that drops the blocks of removed
  tracks (TrackNumbers keep their gaps; block bytes are never
  rewritten), regenerates SeekHead and Cues, carries metadata edits in
  the same pass, and writes via temp file + atomic rename.
- MP42MkvTagWriter+Internal.h exposes the tag writer's scanning and
  payload builders so the remuxer reuses them unchanged.

Verified with the external matrix harness: 241 assertions green across
layout fixtures (ffmpeg/mkvmerge/streamed), removal scenarios, cue
parity, and idempotency.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-25 19:48:09 -05:00

139 lines
6.6 KiB
Objective-C

//
// MP42MatroskaTagMapping.h
// MP42Foundation
//
// Shared Matroska element identifiers and SimpleTag <-> MP42 metadata key
// mapping, used by both the importer (read path) and the tag writer (write
// path) so the two cannot drift apart.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
// EBML / Matroska element identifiers.
//
// These are NOT the values printed on the older matroska.org documentation
// pages, which are wrong for several elements (Tag, Targets, TargetTypeValue,
// Attachments among them). The values below were verified empirically by
// hex-dumping files written by ffmpeg and mkvtoolnix v104, and cross-checked
// against the constants the bundled MatroskaParser.c demuxer parses with.
// Treat MatroskaParser.c as the authority if you ever need to re-verify.
enum {
MP42MkvElementEBMLHeader = 0x1A45DFA3,
MP42MkvElementSegment = 0x18538067,
MP42MkvElementVoid = 0xEC,
MP42MkvElementSeekHead = 0x114D9B74,
MP42MkvElementSeek = 0x4DBB,
MP42MkvElementSeekID = 0x53AB,
MP42MkvElementSeekPosition = 0x53AC,
MP42MkvElementSegmentInfo = 0x1549A966,
MP42MkvElementTracks = 0x1654AE6B,
MP42MkvElementTrackEntry = 0xAE,
MP42MkvElementTrackNumber = 0xD7, // unsigned integer, referenced by block headers
MP42MkvElementTrackUID = 0x73C5, // unsigned integer, tags/chapters/cue refs
MP42MkvElementTrackName = 0x536E, // UTF-8 string
// The Matroska element ids are a known trap: the practical ids (used by
// libebml/mkvtoolnix and ffmpeg, and what every reader expects) are
// 0x22B59C for the ISO-639-2/B language and 0x22B59D for the BCP-47
// LanguageIETF. The EBML spec's later-introduced 0x447A carries the same
// ISO value; it is accepted on read but never written. (Modern mkvtoolnix
// files may contain ONLY the IETF element — mkvmerge derives the ISO code
// from it — so the ISO element can be absent.)
MP42MkvElementTrackLanguage = 0x22B59C, // ISO-639-2/B, 3 ASCII chars
MP42MkvElementTrackLanguageSpec = 0x447A, // newer spec id, same ISO value
MP42MkvElementTrackLanguageIETF = 0x22B59D, // BCP-47 string, e.g. "en-US"
MP42MkvElementTrackFlagEnabled = 0xB9, // unsigned integer, default 1
MP42MkvElementTrackFlagForced = 0x55AA, // unsigned integer, default 0
MP42MkvElementChapters = 0x1043A770,
MP42MkvElementCluster = 0x1F43B675,
MP42MkvElementClusterTimeCode = 0xE7, // unsigned integer, cluster child
MP42MkvElementSimpleBlock = 0xA3, // cluster child; payload starts with the track-number vint
MP42MkvElementBlockGroup = 0xA0, // cluster child; the track comes from its Block child
MP42MkvElementBlock = 0xA1, // BlockGroup child (inside SimpleBlock it is 0xA3)
MP42MkvElementAttachments = 0x1941A469,
MP42MkvElementAttachedFile = 0x61A7,
MP42MkvElementFileDescription = 0x467E,
MP42MkvElementFileName = 0x466E,
MP42MkvElementFileMimeType = 0x4660,
MP42MkvElementFileData = 0x465C,
MP42MkvElementFileUID = 0x46AE,
MP42MkvElementImageType = 0x3E5B,
MP42MkvElementCues = 0x1C53BB6B,
MP42MkvElementCuePoint = 0xBB,
MP42MkvElementCueTime = 0xB3,
MP42MkvElementCueTrackPositions = 0xB7,
MP42MkvElementCueTrack = 0xF7,
MP42MkvElementCueClusterPosition = 0xB1,
MP42MkvElementCueRelativePosition= 0xB0,
// Tagging elements (MatroskaTag spec). Note Tag and Targets are easy to
// get wrong: Tag is 0x7373 and Targets is 0x63C0.
MP42MkvElementTags = 0x1254C367,
MP42MkvElementTag = 0x7373,
MP42MkvElementTargets = 0x63C0,
MP42MkvElementTargetTypeValue = 0x68CA, // unsigned integer
MP42MkvElementTargetType = 0x63CA, // UTF-8 string, e.g. "MOVIE"
MP42MkvElementTagTrackUID = 0x63C5,
MP42MkvElementTagChapterUID = 0x63C4,
MP42MkvElementTagAttachmentUID = 0x63C6,
MP42MkvElementTagEditionUID = 0x63C9,
MP42MkvElementSimpleTag = 0x67C8,
MP42MkvElementTagName = 0x45A3,
MP42MkvElementTagLanguage = 0x447A, // ISO-639-2/B, 3 ASCII chars
MP42MkvElementTagLanguageIETF = 0x447B, // BCP-47, written by recent mkvtoolnix
MP42MkvElementTagDefault = 0x4484, // unsigned integer, default 1
MP42MkvElementTagString = 0x4487,
MP42MkvElementTagBinary = 0x4485,
};
// TargetTypeValue levels. MP42MkvTargetGlobal (0) is a synthetic level for
// tag groups without a Targets element; the importer treats them like the
// default (movie/album) group.
enum {
MP42MkvTargetGlobal = 0,
MP42MkvTargetShot = 10,
MP42MkvTargetScene = 20,
MP42MkvTargetChapter = 30,
MP42MkvTargetPart = 40,
MP42MkvTargetMovie = 50, // also EPISODE / ALBUM
MP42MkvTargetSeason = 60,
MP42MkvTargetCollection = 70,
};
// Read direction: SimpleTag name -> MP42 metadata key, for a target level.
// Levels other than collection/season (including global and part) use the
// default map, matching the importer's historical behavior.
NSDictionary<NSString *, NSString *> *MP42MkvSimpleTagMapForTargetLevel(NSInteger level);
// Whether the importer models tag groups at this level at all. Groups at
// chapter/scene/shot levels are skipped by the importer and must be carried
// through untouched by the writer.
BOOL MP42MkvTargetLevelIsModeled(NSInteger level);
// A canonical (ordered) write pair: one MP42 metadata key and the SimpleTag
// name it is serialized as at a given level. The writer iterates these in
// order so output is stable across saves.
@interface MP42MkvTagPair : NSObject
@property (nonatomic, readonly) NSString *simpleTagName;
@property (nonatomic, readonly) NSString *metadataKey;
- (instancetype)initWithSimpleTagName:(NSString *)name metadataKey:(NSString *)key;
@end
// Write direction: ordered pairs for a target level. Derived from the read
// map so both directions always agree.
NSArray<MP42MkvTagPair *> *MP42MkvWritePairsForTargetLevel(NSInteger level);
// Whether MP42MkvTagWriter can save an MP42 metadata key to a Matroska file:
// either it has a SimpleTag name at some modeled target level, or it is
// regenerated (TrackNumber -> PART_NUMBER/TOTAL_PARTS), or it is stored as a
// cover attachment (CoverArt). Keys answering NO here are silently dropped on
// save — the UI uses this to flag such fields to the user.
BOOL MP42MkvMetadataKeyIsPersisted(NSString *metadataKey);
NS_ASSUME_NONNULL_END