MP42MkvRemuxer gains replacementChapters: when set, the Chapters element is rebuilt from the model track instead of copied verbatim (chapterCount 0 removes the element and its SeekHead entry). Chapter timecodes are written as absolute nanoseconds — libmatroska/mkvtoolnix/ffmpeg all treat them as ns regardless of TimecodeScale, confirmed by hex-dumping a mkvmerge-written file and reading it back with mkvinfo and ffprobe. ChapterAtom is 0xB6 (0xB0 is PixelWidth), and mandatory EditionUID/ ChapterUID are derived deterministically from the content because ffmpeg silently skips atoms without a ChapterUID. Starts are sorted ascending with ties clamped to previous+1ms (mkvmerge rejects non-ascending), empty titles get the importer's "Chapter N" fallback, and the last atom's end rides the track duration only when known. An existing Info Duration is recomputed as the greatest written block start+1ms: the Info payload is rewritten with a fixed 8-byte double slot patched after the clusters stream past, converted to TimecodeScale units. Added blocks measure start+1 like source blocks so a second no-op remux stays byte-identical (including BlockDuration broke idempotence). Files without a source Duration keep having none. The importer's chapter /1e6 needed no change (Start is already ns); a comment now records that so it doesn't get "fixed" to scale-based math. Co-Authored-By: Claude Code <noreply@anthropic.com>
168 lines
8.6 KiB
Objective-C
168 lines
8.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
|
|
MP42MkvElementTrackType = 0x83, // unsigned integer: 1 video, 2 audio, 17 subtitle
|
|
MP42MkvElementFlagLacing = 0x9C, // unsigned integer, default 1 (mkvmerge writes 0 for subtitles)
|
|
MP42MkvElementCodecID = 0x86, // ASCII string, e.g. "S_TEXT/UTF8"
|
|
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)
|
|
MP42MkvElementBlockDuration = 0x9B, // unsigned integer, BlockGroup child: display duration
|
|
// in TrackTimecodeScale units (default scale = 1 ms)
|
|
|
|
// Info children the remuxer recomputes; both are in TimecodeScale units
|
|
// (Duration is the only EBML *float* the remuxer touches — 8-byte double).
|
|
MP42MkvElementTimecodeScale = 0x2AD7B2, // unsigned integer, default 1000000 (1 ms in ns)
|
|
MP42MkvElementDuration = 0x4489, // float, TimecodeScale units
|
|
|
|
// Chapters children (MP42MkvElementChapters). DisplayUnitID (0x76) and
|
|
// ChapterLanguage (0x437C, default "eng") are omitted on write.
|
|
MP42MkvElementEditionEntry = 0x45B9,
|
|
MP42MkvElementEditionUID = 0x45BC, // unsigned integer, mandatory (1 edition 1 UID)
|
|
MP42MkvElementChapterUID = 0x73C4, // unsigned integer, mandatory — ffmpeg
|
|
// silently skips atoms without it
|
|
// ChapterAtom is 0xB6, NOT 0xB0 — 0xB0 is PixelWidth (TrackEntry scope;
|
|
// hex-dumping mkvmerge-written Chapters confirms 0xB6, and the vendored
|
|
// MatroskaParser.c has always parsed 0xB6 here).
|
|
MP42MkvElementChapterAtom = 0xB6,
|
|
// Chapter timecodes are absolute NANOSECONDS — unlike almost every
|
|
// other Matroska time, they do NOT follow TimecodeScale (libmatroska
|
|
// and ffmpeg both write/read them as ns; confirmed against a
|
|
// mkvmerge-written file).
|
|
MP42MkvElementChapterTimeStart = 0x91, // unsigned integer, ns
|
|
MP42MkvElementChapterTimeEnd = 0x92, // unsigned integer, ns; omit when unknown
|
|
MP42MkvElementChapterDisplay = 0x80,
|
|
MP42MkvElementChapterString = 0x85, // UTF-8 string
|
|
|
|
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
|