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>
This commit is contained in:
1 parent
bfa477b799
commit
1040f47542
18 files changed
+4397
-86
No files matched your search
+133
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// MP42Ebml.h
|
||||
// MP42Foundation
|
||||
//
|
||||
// Minimal EBML (Extensible Binary Meta Language) primitives: variable-size
|
||||
// integer coding, flat element scanning over a file descriptor, and element
|
||||
// serialization helpers. Sufficient to read and rewrite the metadata-side
|
||||
// elements of Matroska files (Tags, Attachments, SeekHead, Cues, Void); it
|
||||
// deliberately knows nothing about track/cluster payload contents.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
FOUNDATION_EXPORT NSErrorDomain const MP42MkvErrorDomain;
|
||||
|
||||
typedef NS_ERROR_ENUM(MP42MkvErrorDomain, MP42MkvError) {
|
||||
MP42MkvErrorParse = 1, // malformed / truncated EBML structure
|
||||
MP42MkvErrorUnsupported = 2, // layout we refuse to rewrite (multiple segments, stray unknown sizes…)
|
||||
MP42MkvErrorStaleFile = 3, // file changed on disk since it was opened
|
||||
MP42MkvErrorWriteFailed = 4,
|
||||
MP42MkvErrorCancelled = 5,
|
||||
};
|
||||
|
||||
// Pass as the `length` argument of -scanChildrenFrom:length: to scan to the
|
||||
// end of the file.
|
||||
FOUNDATION_EXPORT const uint64_t MP42EbmlScanToEnd;
|
||||
|
||||
// One EBML element header + extent found by a flat scan.
|
||||
@interface MP42EbmlElement : NSObject
|
||||
|
||||
@property (nonatomic) uint32_t identifier; // raw ID, marker bit included
|
||||
@property (nonatomic) uint64_t start; // absolute offset of the first ID byte
|
||||
@property (nonatomic) uint64_t headerSize; // ID bytes + size vint bytes
|
||||
@property (nonatomic) uint64_t dataSize; // decoded payload length
|
||||
@property (nonatomic) BOOL sizeIsUnknown; // unknown-size vint: payload runs to parent end
|
||||
|
||||
- (uint64_t)dataStart; // start + headerSize
|
||||
- (uint64_t)end; // only meaningful when !sizeIsUnknown
|
||||
|
||||
// Total extent (header + payload) of a known-size element.
|
||||
- (uint64_t)totalSize;
|
||||
|
||||
@end
|
||||
|
||||
@interface MP42EbmlScanner : NSObject
|
||||
|
||||
@property (nonatomic, readonly) uint64_t fileSize;
|
||||
|
||||
// Opens the file at URL for reading. The scanner owns the descriptor; call
|
||||
// -close when done (also called from dealloc).
|
||||
+ (nullable instancetype)scannerWithURL:(NSURL *)URL error:(NSError * _Nullable *)outError;
|
||||
- (void)close;
|
||||
|
||||
- (nullable NSData *)readAtOffset:(uint64_t)offset
|
||||
length:(uint64_t)length
|
||||
error:(NSError * _Nullable *)outError;
|
||||
|
||||
// Flat scan of direct children covering [start, start+length) (or to EOF for
|
||||
// MP42EbmlScanToEnd). Elements are returned in file order; payloads are not
|
||||
// descended into. An element with an unknown-size vint consumes the rest of
|
||||
// the region and terminates the scan — callers must only tolerate that for
|
||||
// trailing elements (clusters in streamed files).
|
||||
//
|
||||
// Returns nil (and sets *outError) on malformed or truncated data. Runs of
|
||||
// 0x00 padding are treated as end-of-structure, not an error.
|
||||
- (nullable NSArray<MP42EbmlElement *> *)scanChildrenFrom:(uint64_t)start
|
||||
length:(uint64_t)length
|
||||
error:(NSError * _Nullable *)outError;
|
||||
|
||||
@end
|
||||
|
||||
//
|
||||
// EBML vint primitives and element builders (all class methods, no I/O).
|
||||
//
|
||||
@interface MP42Ebml : NSObject
|
||||
|
||||
+ (NSError *)errorWithCode:(MP42MkvError)code message:(NSString *)format, ... NS_FORMAT_FUNCTION(2, 3);
|
||||
|
||||
// Size vints: marker bit is stripped from the value. An all-ones value bits
|
||||
// pattern decodes as "unknown size".
|
||||
|
||||
// Returns the byte length of the size vint at buf (1...8), or 0 if the
|
||||
// leading byte is invalid or `available` is too small.
|
||||
+ (NSUInteger)sizeVintLengthAt:(const uint8_t *)buf available:(NSUInteger)available;
|
||||
// Decodes a size vint of the given length. Sets *unknown for all-ones values.
|
||||
+ (uint64_t)sizeVintValueAt:(const uint8_t *)buf length:(NSUInteger)length unknown:(nullable BOOL *)unknown;
|
||||
// Canonical minimal width for encoding a size value.
|
||||
+ (NSUInteger)encodedLengthForSizeValue:(uint64_t)value;
|
||||
|
||||
// ID vints: the marker bit is part of the ID value.
|
||||
+ (NSUInteger)idVintLengthAt:(const uint8_t *)buf available:(NSUInteger)available;
|
||||
+ (uint32_t)idValueAt:(const uint8_t *)buf length:(NSUInteger)length;
|
||||
|
||||
// Block-header track number: an "unmarked" vint whose marker bit only codes
|
||||
// the length. Returns the bytes consumed (0 when invalid/truncated) and the
|
||||
// decoded value in *outValue.
|
||||
+ (NSUInteger)blockTrackNumberAt:(const uint8_t *)buf available:(NSUInteger)available value:(nullable uint32_t *)outValue;
|
||||
|
||||
// Unsigned integer element payloads (SeekPosition, CueClusterPosition…).
|
||||
+ (uint64_t)unsignedIntValueAt:(const uint8_t *)buf length:(NSUInteger)length;
|
||||
|
||||
// Appends a size vint using the canonical minimal width.
|
||||
+ (void)appendSize:(uint64_t)value to:(NSMutableData *)data;
|
||||
// Appends a size vint at a fixed width (width must be >= canonical width).
|
||||
+ (void)appendSize:(uint64_t)value width:(NSUInteger)width to:(NSMutableData *)data;
|
||||
// Appends an unknown-size vint of the given width (1...8).
|
||||
+ (void)appendUnknownSizeOfWidth:(NSUInteger)width to:(NSMutableData *)data;
|
||||
|
||||
+ (void)appendId:(uint32_t)identifier to:(NSMutableData *)data;
|
||||
+ (void)appendUnsignedInt:(uint64_t)value width:(NSUInteger)width to:(NSMutableData *)data;
|
||||
|
||||
// Complete elements.
|
||||
+ (void)appendElement:(uint32_t)identifier payload:(nullable NSData *)payload to:(NSMutableData *)data;
|
||||
+ (void)appendStringElement:(uint32_t)identifier string:(NSString *)string to:(NSMutableData *)data;
|
||||
+ (void)appendVoidOfLength:(uint64_t)length to:(NSMutableData *)data;
|
||||
|
||||
// Flat scan of direct children inside an in-memory buffer, mirroring
|
||||
// -[MP42EbmlScanner scanChildrenFrom:length: semantics]. Returned elements
|
||||
// carry offsets relative to the buffer's own byte 0.
|
||||
+ (nullable NSArray<MP42EbmlElement *> *)scanBuffer:(NSData *)buffer
|
||||
from:(uint64_t)start
|
||||
length:(uint64_t)length
|
||||
error:(NSError * _Nullable *)outError;
|
||||
|
||||
// Where an element's payload ends within a region, tolerating the
|
||||
// truncated/unknown-size case: min(payload end or region end).
|
||||
+ (uint64_t)payloadEndForElement:(MP42EbmlElement *)element regionEnd:(uint64_t)regionEnd;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
Reference in new issue
Block a user