- 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>
134 lines
5.7 KiB
Objective-C
134 lines
5.7 KiB
Objective-C
//
|
|
// MP42MkvTagWriter+Internal.h
|
|
// MP42Foundation
|
|
//
|
|
// Internal (not framework-exported) declarations shared between
|
|
// MP42MkvTagWriter and MP42MkvRemuxer. The scanned file model and the
|
|
// payload builders live in the tag writer's implementation; the remuxer
|
|
// rebuilds a whole file from the same structures and builders so the two
|
|
// writers can never drift on what a "tagged save" means. Do not import
|
|
// this header from outside the framework, and never add it to the public
|
|
// headers build phase.
|
|
//
|
|
|
|
#import "MP42MkvTagWriter.h"
|
|
|
|
#import "MP42MatroskaTagMapping.h"
|
|
|
|
@class MP42Metadata;
|
|
@class MP42Track;
|
|
|
|
typedef NS_ENUM(NSInteger, MP42MkvKidKind) {
|
|
MP42MkvKidKeep = 0,
|
|
MP42MkvKidTags,
|
|
MP42MkvKidAttachments,
|
|
MP42MkvKidTracks,
|
|
MP42MkvKidSeekHead,
|
|
MP42MkvKidCues,
|
|
MP42MkvKidCluster,
|
|
};
|
|
|
|
// A direct child of the Segment element plus its rewrite plan. All "rel"
|
|
// quantities are byte offsets relative to the segment *data* start, which is
|
|
// the same origin SeekHead/Cue positions use; that keeps position math
|
|
// independent of any width change to the Segment header itself.
|
|
@interface MP42MkvKid : NSObject
|
|
@property (nonatomic, strong) MP42EbmlElement *el;
|
|
@property (nonatomic) MP42MkvKidKind kind;
|
|
@property (nonatomic, strong, nullable) NSData *payload; // parsed kinds only
|
|
@property (nonatomic, strong, nullable) NSData *pendingPayload; // set when the payload is rewritten
|
|
@property (nonatomic) BOOL removed; // element disappears
|
|
@property (nonatomic) BOOL materializeUnknown; // cluster header: unknown size -> known
|
|
@property (nonatomic) uint64_t relStart; // el.start - segment data start
|
|
@property (nonatomic) uint64_t newStart; // plan-domain, relative to data start
|
|
- (uint64_t)oldTotal;
|
|
@end
|
|
|
|
// Snapshot of the file structure the rewrite operates on.
|
|
@interface MP42MkvFile : NSObject
|
|
@property (nonatomic, strong) MP42EbmlElement *segment;
|
|
@property (nonatomic, strong) NSArray<MP42MkvKid *> *kids;
|
|
@property (nonatomic, strong, nullable) MP42MkvKid *tags;
|
|
@property (nonatomic, strong, nullable) MP42MkvKid *attachments;
|
|
@property (nonatomic, strong, nullable) MP42MkvKid *tracks;
|
|
@property (nonatomic, strong, nullable) MP42MkvKid *firstCluster;
|
|
@property (nonatomic, strong) NSArray<MP42MkvKid *> *seekHeads;
|
|
@property (nonatomic, strong) NSArray<MP42MkvKid *> *cues;
|
|
@property (nonatomic) BOOL segmentSizeUnknown;
|
|
@property (nonatomic) uint64_t segDataStart; // absolute
|
|
@property (nonatomic) uint64_t segDataLen; // old data length (to EOF when unknown)
|
|
@end
|
|
|
|
// Per-field rewrite plan inside one TrackEntry (see editTrackEntry:…).
|
|
typedef NS_ENUM(NSInteger, MP42MkvFieldAction) {
|
|
MP42MkvFieldKeep = 0, // leave the existing element untouched
|
|
MP42MkvFieldDrop, // remove every element with this id
|
|
MP42MkvFieldSet, // replace the first occurrence (append if absent)
|
|
};
|
|
|
|
@interface MP42MkvFieldEdit : NSObject
|
|
@property (nonatomic) MP42MkvFieldAction action;
|
|
@property (nonatomic, strong, nullable) NSData *payload; // Set only
|
|
@property (nonatomic) uint32_t appendId; // id used when the file has no such element
|
|
@end
|
|
|
|
// Maps an old SeekPosition (segment-data-relative) to its new value; sets
|
|
// *drop for entries pointing into a removed element.
|
|
typedef uint64_t (^MP42MkvPosMapper)(uint64_t relPos, BOOL *drop);
|
|
|
|
@interface MP42MkvTagWriter ()
|
|
|
|
// The members MP42MkvRemuxer drives. Semantics and nullability are as
|
|
// documented at each implementation in MP42MkvTagWriter.m.
|
|
|
|
- (BOOL)checkStalenessWithError:(NSError * _Nullable *)outError;
|
|
|
|
- (nullable MP42MkvFile *)scanFile:(MP42EbmlScanner *)scanner error:(NSError * _Nullable *)outError;
|
|
|
|
- (nullable NSData *)buildTagsPayloadForMetadata:(MP42Metadata *)metadata
|
|
file:(MP42MkvFile *)file
|
|
error:(NSError * _Nullable *)outError;
|
|
|
|
- (BOOL)buildAttachmentsPayloadForMetadata:(MP42Metadata *)metadata
|
|
file:(MP42MkvFile *)file
|
|
payload:(NSData * _Nullable *)outPayload
|
|
changed:(BOOL *)outChanged
|
|
shouldExist:(BOOL *)outShouldExist
|
|
error:(NSError * _Nullable *)outError;
|
|
|
|
- (BOOL)buildTracksPayloadForTracks:(NSArray<MP42Track *> *)modelTracks
|
|
file:(MP42MkvFile *)file
|
|
payload:(NSData * _Nullable *)outPayload
|
|
changed:(BOOL *)outChanged
|
|
error:(NSError * _Nullable *)outError;
|
|
|
|
- (nullable NSData *)editTrackEntry:(MP42EbmlElement *)entry
|
|
buffer:(NSData *)buffer
|
|
regionEnd:(uint64_t)regionEnd
|
|
track:(MP42Track *)track
|
|
error:(NSError * _Nullable *)outError;
|
|
|
|
// Low-level output helpers (they consult -cancel and report progress on the
|
|
// writer instance they are called on — the remuxer drives a helper writer
|
|
// and mirrors its own state onto it).
|
|
|
|
- (BOOL)writeData:(NSData *)data
|
|
fd:(int)fd
|
|
offset:(uint64_t)offset
|
|
error:(NSError * _Nullable *)outError;
|
|
|
|
- (BOOL)copyFromScanner:(MP42EbmlScanner *)scanner
|
|
source:(uint64_t)sourceAbs
|
|
length:(uint64_t)length
|
|
toFD:(int)outFd
|
|
outOffset:(uint64_t *)outOffset
|
|
totalEstimate:(uint64_t)totalEstimate
|
|
error:(NSError * _Nullable *)outError;
|
|
|
|
- (BOOL)appendData:(NSData *)data
|
|
toFD:(int)fd
|
|
outOffset:(uint64_t *)outOffset
|
|
error:(NSError * _Nullable *)outError;
|
|
|
|
@end
|