Files
MP42Foundation/MP42/MP42MatroskaTagMapping.h
T
Scott GarrettandClaude Code 8ba3f25a2c Write added subtitle tracks during Matroska remux
MP42MkvRemuxer previously refused any track present in currentTracks
but not in sourceTracks; it now accepts added MP42SubtitleTracks whose
URL extension is srt/ssa/ass and muxes them into the rebuilt file as
S_TEXT/UTF8. The extension gate mirrors the app-side
DocumentWindowController.matroskaSubtitleExtensions list — the two
must stay in sync.

- Each added track gets an appended TrackEntry: TrackNumber = max of
  all entries + 1 (refuse past 0x3FFF, the 2-byte unmarked-vint
  limit), TrackType 17, FlagEnabled 1, FlagLacing 0, CodecID
  S_TEXT/UTF8, ISO 0x22B59C + IETF 0x22B59D language, Name only when
  it differs from the codec default.
- The model never holds subtitle text, so lines are re-parsed from
  track.URL at save time: SRT via LoadSRTFromURL + serializer drain,
  SSA/ASS via MP42SSAParser/MP42SSAConverter (styling stripped, same
  lossy path the tx3g muxer takes). Zero parsed lines refuses.
- Blocks are written as BlockGroup{Block(unmarked-vint track number,
  int16 BE relative timecode, UTF-8 text), BlockDuration(>=1 ms)}.
  The BlockGroup wrapper is mandatory: a bare Block as a direct
  Cluster child is silently ignored by libebml readers even though
  ffmpeg tolerates it.
- Lines are assigned to the last existing Cluster whose Timecode is
  <= the line's start time (inheriting missing cluster timecodes,
  negative relative timecodes are legal); lines whose relative
  timecode would exceed +32767 flush as synthesized Clusters right
  after that cluster. Original clusters are copied verbatim and
  never split.
- Added tracks join the existing per-track CueTrackPositions
  bookkeeping, and the drop-all-remaining guard still fires before
  additions, so drop-all-plus-add stays refused.

Adds TrackType/FlagLacing/CodecID/BlockDuration element ids to
MP42MatroskaTagMapping.h.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-26 11:53:17 -05:00

144 lines
7.1 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)
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