From fc6b9d8a4c8d139d5b96134dbb9961f28bcca04e Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Fri, 1 Sep 2017 07:36:57 +0200 Subject: [PATCH] Use NSTimeInterval to store a track start offset, rename alternate_group to alternateGroup. --- MP42/MP42File.m | 2 +- MP42/MP42MkvImporter.m | 15 +++++++---- MP42/MP42PrivateUtilities.h | 4 +-- MP42/MP42PrivateUtilities.m | 53 ++++++++++++++++++++----------------- MP42/MP42SrtImporter.m | 2 +- MP42/MP42Track.h | 4 +-- MP42/MP42Track.m | 31 +++++++++++----------- MP42/MP42VobSubImporter.m | 2 +- 8 files changed, 61 insertions(+), 52 deletions(-) diff --git a/MP42/MP42File.m b/MP42/MP42File.m index 601d29b..7357113 100644 --- a/MP42/MP42File.m +++ b/MP42/MP42File.m @@ -532,7 +532,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { } for (MP42Track *track in tracks) { - track.alternate_group = groupID; + track.alternateGroup = groupID; if (track.enabled && !enabled) { enabled = YES; diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index 40be7a9..f847a74 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -285,7 +285,7 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui MP42AudioTrack *audioTrack = [[MP42AudioTrack alloc] init]; audioTrack.channels = mkvTrack->AV.Audio.Channels; audioTrack.channelLayoutTag = getDefaultChannelLayout(mkvTrack->AV.Audio.Channels); - audioTrack.alternate_group = 1; + audioTrack.alternateGroup = 1; for (MP42Track *track in self.tracks) { if ([track isMemberOfClass:[MP42AudioTrack class]]) { @@ -299,7 +299,7 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui // Text else if (mkvTrack->Type == TT_SUB) { newTrack = [[MP42SubtitleTrack alloc] init]; - newTrack.alternate_group = 2; + newTrack.alternateGroup = 2; for (MP42Track *subtitleTrack in self.tracks) { if ([subtitleTrack isMemberOfClass:[MP42SubtitleTrack class]]) { @@ -588,7 +588,7 @@ static NSString * TrackNameToString(TrackInfo *track) return nil; } -- (uint64_t)matroskaTrackStartTime:(TrackInfo *)track Id:(MP4TrackId)Id +- (double)matroskaTrackStartTime:(TrackInfo *)track Id:(MP4TrackId)Id { uint64_t StartTime, EndTime, FilePos; uint32_t Track, FrameSize, FrameFlags; @@ -602,8 +602,13 @@ static NSString * TrackNameToString(TrackInfo *track) mkv_Seek(_matroskaFile, 0, 0); TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, Id); + int64_t codecDelay = trackInfo->CodecDelay; + if (codecDelay == 0 && trackInfo->Type == TT_AUDIO && !strcmp(trackInfo->CodecID, "A_AAC")) { + NSUInteger sampleRate = mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq); + codecDelay = 2112 * SCALE_FACTOR * 1000 / sampleRate; + } - return (StartTime - trackInfo->CodecDelay) / SCALE_FACTOR; + return (((double)StartTime) - codecDelay) / SCALE_FACTOR; } - (NSUInteger)timescaleForTrack:(MP42Track *)track @@ -630,7 +635,7 @@ static NSString * TrackNameToString(TrackInfo *track) - (NSSize)sizeForTrack:(MP42Track *)track { - return NSMakeSize([(MP42VideoTrack*)track width], [(MP42VideoTrack*) track height]); + return NSMakeSize([(MP42VideoTrack *)track width], [(MP42VideoTrack *) track height]); } - (NSData *)magicCookieForTrack:(MP42Track *)track diff --git a/MP42/MP42PrivateUtilities.h b/MP42/MP42PrivateUtilities.h index 725e5c5..48f07fe 100644 --- a/MP42/MP42PrivateUtilities.h +++ b/MP42/MP42PrivateUtilities.h @@ -56,8 +56,8 @@ ComponentResult ReadESDSDescExt(void* descExt, UInt8 **buffer, int *size, int ve MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId); -int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id); -void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, int64_t offset); +double getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id); +void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, double offset); int copyTrackEditLists (MP4FileHandle fileHandle, MP4TrackId srcTrackId, MP4TrackId dstTrackId); NSError * MP42Error(NSString *description, NSString* recoverySuggestion, NSInteger code); diff --git a/MP42/MP42PrivateUtilities.m b/MP42/MP42PrivateUtilities.m index c02d209..bebe4e1 100644 --- a/MP42/MP42PrivateUtilities.m +++ b/MP42/MP42PrivateUtilities.m @@ -10,11 +10,13 @@ #import "MP42PrivateUtilities.h" #import #import +#import #include #include #import "MP42Languages.h" #import "MP42MediaFormat.h" + #include "intreadwrite.h" #include "avcodec.h" @@ -495,9 +497,9 @@ uint8_t *CreateEsdsFromSetupData(uint8_t *codecPrivate, size_t vosLen, size_t *e #pragma mark - -int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id) +NSTimeInterval getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id) { - int64_t offset = 0; + NSTimeInterval offset = 0; uint32_t i = 1, trackEditCount = MP4GetTrackNumberOfEdits(fileHandle, Id); while (i <= trackEditCount) { @@ -509,11 +511,11 @@ int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id) MP4GetTrackIntegerProperty(fileHandle, Id, "edts.elst.version", &editListVersion); if (editListVersion == 0 && editMediaTime == ((uint32_t)-1)) - offset += MP4ConvertFromMovieDuration(fileHandle, editDuration, MP4_MILLISECONDS_TIME_SCALE); + offset += MP4ConvertFromMovieDuration(fileHandle, editDuration, MP4_NANOSECONDS_TIME_SCALE); else if (editListVersion == 1 && editMediaTime == ((uint64_t)-1)) - offset += MP4ConvertFromMovieDuration(fileHandle, editDuration, MP4_MILLISECONDS_TIME_SCALE); + offset += MP4ConvertFromMovieDuration(fileHandle, editDuration, MP4_NANOSECONDS_TIME_SCALE); else if (i == 1) - offset -= MP4ConvertFromTrackDuration(fileHandle, Id, editMediaTime, MP4_MILLISECONDS_TIME_SCALE); + offset -= MP4ConvertFromTrackDuration(fileHandle, Id, editMediaTime, MP4_NANOSECONDS_TIME_SCALE); //NSLog(@"Track %d, Media Time = %lld, Segment Duration: %qu Media Rate:%d", Id, editMediaTime, editDuration, editMediaRate); i++; @@ -521,7 +523,7 @@ int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id) //NSLog(@"Track %d offset: %d ms", Id, offset); - return offset; + return offset / 1000000; } MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId) @@ -544,41 +546,42 @@ MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId) return duration; } -void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, int64_t offset) +void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, NSTimeInterval offset) { uint32_t trackEditsCount = MP4GetTrackNumberOfEdits(fileHandle, Id); - if (offset > 0) - offset = MP4ConvertToTrackDuration(fileHandle, Id, offset, MP4_MILLISECONDS_TIME_SCALE); - else - offset = -(MP4ConvertToTrackDuration(fileHandle, Id, -offset, MP4_MILLISECONDS_TIME_SCALE)); - // If there is no existing edit list, just add some new ones at the start and do the usual stuff. - if (offset && !trackEditsCount) { + CMTime time = CMTimeMake(offset * 1000000, MP4_NANOSECONDS_TIME_SCALE); + CMTime trackTime = CMTimeConvertScale(time, MP4GetTrackTimeScale(fileHandle, Id), kCMTimeRoundingMethod_Default); + + int64_t offset_i = trackTime.value; + + // If there isn't an existing edit list, just add some new ones at the start and do the usual stuff. + if (offset_i && !trackEditsCount) { MP4Duration editDuration = MP4ConvertFromTrackDuration(fileHandle, Id, MP4GetTrackDuration(fileHandle, Id), MP4GetTimeScale(fileHandle)); - if (offset > 0) { + if (offset_i > 0) { MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, Id, - offset, + offset_i, MP4GetTimeScale(fileHandle)); MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, -1, delayDuration, 0); MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, 0, editDuration, 0); } - else if (offset < 0) { + else if (offset_i < 0) { MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, Id, - -offset, + -offset_i, MP4GetTimeScale(fileHandle)); - MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, -offset, editDuration - delayDuration, 0); + MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, -offset_i, editDuration - delayDuration, 0); } } // If the mp4 contains already some edits list, try to reuse them else if (trackEditsCount) { - if (offset >= 0) { + if (offset_i >= 0) { // Remove all the empty edit lists while (MP4GetTrackNumberOfEdits(fileHandle, Id)) { uint64_t editListVersion = 0; @@ -608,13 +611,13 @@ void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, int64_t offset MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, Id, - offset, + offset_i, MP4GetTimeScale(fileHandle)); - if (offset != 0) + if (offset_i != 0) MP4AddTrackEdit(fileHandle, Id, 1, -1, delayDuration, 0); } - else if (offset < 0) { + else if (offset_i < 0) { // First remove all the empty edit lists while (MP4GetTrackNumberOfEdits(fileHandle, Id)) { uint64_t editListVersion = 0; @@ -640,17 +643,17 @@ void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, int64_t offset MP4GetTimeScale(fileHandle)); MP4Duration newOffsetDuration = MP4ConvertFromTrackDuration(fileHandle, Id, - -offset, + -offset_i, MP4GetTimeScale(fileHandle)); MP4SetTrackEditDuration(fileHandle, Id, 1, oldEditDuration + oldEditMediaStart - newOffsetDuration); - MP4SetTrackEditMediaStart(fileHandle, Id, 1, -offset); + MP4SetTrackEditMediaStart(fileHandle, Id, 1, -offset_i); } // Else create a new one. else { MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, Id, - -offset, + -offset_i, MP4GetTimeScale(fileHandle)); MP4AddTrackEdit(fileHandle, Id, 1, -1, delayDuration, 0); diff --git a/MP42/MP42SrtImporter.m b/MP42/MP42SrtImporter.m index ff21690..3088dc0 100644 --- a/MP42/MP42SrtImporter.m +++ b/MP42/MP42SrtImporter.m @@ -38,7 +38,7 @@ newTrack.format = kMP42SubtitleCodecType_3GText; newTrack.URL = self.fileURL; - newTrack.alternate_group = 2; + newTrack.alternateGroup = 2; newTrack.language = getFilenameLanguage((CFStringRef)self.fileURL.path); // Check if a 10.10 only class is available, NSLinguisticTagger crashes on 10.9 diff --git a/MP42/MP42Track.h b/MP42/MP42Track.h index 23a7dab..689cf5a 100644 --- a/MP42/MP42Track.h +++ b/MP42/MP42Track.h @@ -40,8 +40,8 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readwrite, copy) NSSet *mediaCharacteristicTags; -@property(nonatomic, readwrite) uint64_t alternate_group; -@property(nonatomic, readwrite) int64_t startOffset; +@property(nonatomic, readwrite) uint64_t alternateGroup; +@property(nonatomic, readwrite) NSTimeInterval startOffset; @property(nonatomic, readwrite, copy) MP42ConversionSettings *conversionSettings; diff --git a/MP42/MP42Track.m b/MP42/MP42Track.m index c698143..b1b51d6 100644 --- a/MP42/MP42Track.m +++ b/MP42/MP42Track.m @@ -22,8 +22,9 @@ NSString *_language; BOOL _enabled; - uint64_t _alternate_group; - int64_t _startOffset; + uint64_t _alternateGroup; + + NSTimeInterval _startOffset; } @property(nonatomic, readwrite) MP42TrackId trackId; @@ -104,7 +105,7 @@ _enabled = NO; } - MP4GetTrackIntegerProperty(fileHandle, _trackId, "tkhd.alternate_group", &_alternate_group); + MP4GetTrackIntegerProperty(fileHandle, _trackId, "tkhd.alternate_group", &_alternateGroup); // Media characteristic tags NSMutableSet *mediaCharacteristicTags = [[NSMutableSet alloc] init]; @@ -165,7 +166,7 @@ copy->_name = [_name copy]; copy->_language = [_language copy]; copy->_enabled = _enabled; - copy->_alternate_group = _alternate_group; + copy->_alternateGroup = _alternateGroup; copy->_startOffset = _startOffset; copy->_dataLength = _dataLength; @@ -212,7 +213,7 @@ } if (_updatedProperty[@"alternate_group"] || !_muxed) { - MP4SetTrackIntegerProperty(fileHandle, _trackId, "tkhd.alternate_group", _alternate_group); + MP4SetTrackIntegerProperty(fileHandle, _trackId, "tkhd.alternate_group", _alternateGroup); } if (_updatedProperty[@"start_offset"]) { @@ -312,22 +313,22 @@ return _enabled; } -- (uint64_t)alternate_group { - return _alternate_group; +- (uint64_t)alternateGroup { + return _alternateGroup; } -- (void)setAlternate_group:(uint64_t)newGroup +- (void)setAlternateGroup:(uint64_t)newGroup { - _alternate_group = newGroup; + _alternateGroup = newGroup; self.edited = YES; _updatedProperty[@"alternate_group"] = @YES; } -- (int64_t)startOffset { +- (NSTimeInterval)startOffset { return _startOffset; } -- (void)setStartOffset:(int64_t)newOffset +- (void)setStartOffset:(NSTimeInterval)newOffset { _startOffset = newOffset; self.edited = YES; @@ -390,8 +391,8 @@ [coder encodeBool:_enabled forKey:@"enabled"]; - [coder encodeInt64:_alternate_group forKey:@"alternate_group"]; - [coder encodeInt64:_startOffset forKey:@"startOffset"]; + [coder encodeInt64:_alternateGroup forKey:@"alternate_group"]; + [coder encodeDouble:_startOffset forKey:@"startOffset"]; [coder encodeBool:_edited forKey:@"isEdited"]; [coder encodeBool:_muxed forKey:@"muxed"]; @@ -437,8 +438,8 @@ _enabled = [decoder decodeBoolForKey:@"enabled"]; - _alternate_group = [decoder decodeInt64ForKey:@"alternate_group"]; - _startOffset = [decoder decodeInt64ForKey:@"startOffset"]; + _alternateGroup = [decoder decodeInt64ForKey:@"alternate_group"]; + _startOffset = [decoder decodeDoubleForKey:@"startOffset"]; _edited = [decoder decodeBoolForKey:@"isEdited"]; _muxed = [decoder decodeBoolForKey:@"muxed"]; diff --git a/MP42/MP42VobSubImporter.m b/MP42/MP42VobSubImporter.m index 30f721b..bd3e2bf 100644 --- a/MP42/MP42VobSubImporter.m +++ b/MP42/MP42VobSubImporter.m @@ -241,7 +241,7 @@ static NSArray * LoadVobSubSubtitles(NSURL *theDirectory, NSStr newTrack.format = kMP42SubtitleCodecType_VobSub; newTrack.URL = self.fileURL; - newTrack.alternate_group = 2; + newTrack.alternateGroup = 2; newTrack.trackId = count++; newTrack.language = [MP42Languages.defaultManager extendedTagForISO_639_1:track->language]; newTrack.duration = track->duration;