Use NSTimeInterval to store a track start offset, rename alternate_group to alternateGroup.

This commit is contained in:
Damiano Galassi committed 2017-09-01 07:36:57 +02:00
1 parent 02706f99d1
commit fc6b9d8a4c
8 files changed
+60 -51

No files matched your search

+1 -1
View File
@@ -532,7 +532,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
} }
for (MP42Track *track in tracks) { for (MP42Track *track in tracks) {
track.alternate_group = groupID; track.alternateGroup = groupID;
if (track.enabled && !enabled) { if (track.enabled && !enabled) {
enabled = YES; enabled = YES;
+9 -4
View File
@@ -285,7 +285,7 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui
MP42AudioTrack *audioTrack = [[MP42AudioTrack alloc] init]; MP42AudioTrack *audioTrack = [[MP42AudioTrack alloc] init];
audioTrack.channels = mkvTrack->AV.Audio.Channels; audioTrack.channels = mkvTrack->AV.Audio.Channels;
audioTrack.channelLayoutTag = getDefaultChannelLayout(mkvTrack->AV.Audio.Channels); audioTrack.channelLayoutTag = getDefaultChannelLayout(mkvTrack->AV.Audio.Channels);
audioTrack.alternate_group = 1; audioTrack.alternateGroup = 1;
for (MP42Track *track in self.tracks) { for (MP42Track *track in self.tracks) {
if ([track isMemberOfClass:[MP42AudioTrack class]]) { if ([track isMemberOfClass:[MP42AudioTrack class]]) {
@@ -299,7 +299,7 @@ static int readMkvPacket(struct StdIoStream *ioStream, TrackInfo *trackInfo, ui
// Text // Text
else if (mkvTrack->Type == TT_SUB) { else if (mkvTrack->Type == TT_SUB) {
newTrack = [[MP42SubtitleTrack alloc] init]; newTrack = [[MP42SubtitleTrack alloc] init];
newTrack.alternate_group = 2; newTrack.alternateGroup = 2;
for (MP42Track *subtitleTrack in self.tracks) { for (MP42Track *subtitleTrack in self.tracks) {
if ([subtitleTrack isMemberOfClass:[MP42SubtitleTrack class]]) { if ([subtitleTrack isMemberOfClass:[MP42SubtitleTrack class]]) {
@@ -588,7 +588,7 @@ static NSString * TrackNameToString(TrackInfo *track)
return nil; return nil;
} }
- (uint64_t)matroskaTrackStartTime:(TrackInfo *)track Id:(MP4TrackId)Id - (double)matroskaTrackStartTime:(TrackInfo *)track Id:(MP4TrackId)Id
{ {
uint64_t StartTime, EndTime, FilePos; uint64_t StartTime, EndTime, FilePos;
uint32_t Track, FrameSize, FrameFlags; uint32_t Track, FrameSize, FrameFlags;
@@ -602,8 +602,13 @@ static NSString * TrackNameToString(TrackInfo *track)
mkv_Seek(_matroskaFile, 0, 0); mkv_Seek(_matroskaFile, 0, 0);
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, Id); 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 - (NSUInteger)timescaleForTrack:(MP42Track *)track
+2 -2
View File
@@ -56,8 +56,8 @@ ComponentResult ReadESDSDescExt(void* descExt, UInt8 **buffer, int *size, int ve
MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId); MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId);
int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id); double getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id);
void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, int64_t offset); void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, double offset);
int copyTrackEditLists (MP4FileHandle fileHandle, MP4TrackId srcTrackId, MP4TrackId dstTrackId); int copyTrackEditLists (MP4FileHandle fileHandle, MP4TrackId srcTrackId, MP4TrackId dstTrackId);
NSError * MP42Error(NSString *description, NSString* recoverySuggestion, NSInteger code); NSError * MP42Error(NSString *description, NSString* recoverySuggestion, NSInteger code);
+28 -25
View File
@@ -10,11 +10,13 @@
#import "MP42PrivateUtilities.h" #import "MP42PrivateUtilities.h"
#import <string.h> #import <string.h>
#import <CoreAudio/CoreAudio.h> #import <CoreAudio/CoreAudio.h>
#import <CoreMedia/CMTime.h>
#include <zlib.h> #include <zlib.h>
#include <bzlib.h> #include <bzlib.h>
#import "MP42Languages.h" #import "MP42Languages.h"
#import "MP42MediaFormat.h" #import "MP42MediaFormat.h"
#include "intreadwrite.h" #include "intreadwrite.h"
#include "avcodec.h" #include "avcodec.h"
@@ -495,9 +497,9 @@ uint8_t *CreateEsdsFromSetupData(uint8_t *codecPrivate, size_t vosLen, size_t *e
#pragma mark - #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); uint32_t i = 1, trackEditCount = MP4GetTrackNumberOfEdits(fileHandle, Id);
while (i <= trackEditCount) { while (i <= trackEditCount) {
@@ -509,11 +511,11 @@ int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id)
MP4GetTrackIntegerProperty(fileHandle, Id, "edts.elst.version", &editListVersion); MP4GetTrackIntegerProperty(fileHandle, Id, "edts.elst.version", &editListVersion);
if (editListVersion == 0 && editMediaTime == ((uint32_t)-1)) 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)) 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) 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); //NSLog(@"Track %d, Media Time = %lld, Segment Duration: %qu Media Rate:%d", Id, editMediaTime, editDuration, editMediaRate);
i++; i++;
@@ -521,7 +523,7 @@ int64_t getTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id)
//NSLog(@"Track %d offset: %d ms", Id, offset); //NSLog(@"Track %d offset: %d ms", Id, offset);
return offset; return offset / 1000000;
} }
MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId) MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId)
@@ -544,41 +546,42 @@ MP4Duration getTrackDuration(MP4FileHandle fileHandle, MP4TrackId trackId)
return duration; 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); 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. CMTime time = CMTimeMake(offset * 1000000, MP4_NANOSECONDS_TIME_SCALE);
if (offset && !trackEditsCount) { 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, MP4Duration editDuration = MP4ConvertFromTrackDuration(fileHandle,
Id, Id,
MP4GetTrackDuration(fileHandle, Id), MP4GetTrackDuration(fileHandle, Id),
MP4GetTimeScale(fileHandle)); MP4GetTimeScale(fileHandle));
if (offset > 0) { if (offset_i > 0) {
MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle,
Id, Id,
offset, offset_i,
MP4GetTimeScale(fileHandle)); MP4GetTimeScale(fileHandle));
MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, -1, delayDuration, 0); MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, -1, delayDuration, 0);
MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, 0, editDuration, 0); MP4AddTrackEdit(fileHandle, Id, MP4_INVALID_EDIT_ID, 0, editDuration, 0);
} }
else if (offset < 0) { else if (offset_i < 0) {
MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle,
Id, Id,
-offset, -offset_i,
MP4GetTimeScale(fileHandle)); 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 // If the mp4 contains already some edits list, try to reuse them
else if (trackEditsCount) { else if (trackEditsCount) {
if (offset >= 0) { if (offset_i >= 0) {
// Remove all the empty edit lists // Remove all the empty edit lists
while (MP4GetTrackNumberOfEdits(fileHandle, Id)) { while (MP4GetTrackNumberOfEdits(fileHandle, Id)) {
uint64_t editListVersion = 0; uint64_t editListVersion = 0;
@@ -608,13 +611,13 @@ void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, int64_t offset
MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle,
Id, Id,
offset, offset_i,
MP4GetTimeScale(fileHandle)); MP4GetTimeScale(fileHandle));
if (offset != 0) if (offset_i != 0)
MP4AddTrackEdit(fileHandle, Id, 1, -1, delayDuration, 0); MP4AddTrackEdit(fileHandle, Id, 1, -1, delayDuration, 0);
} }
else if (offset < 0) { else if (offset_i < 0) {
// First remove all the empty edit lists // First remove all the empty edit lists
while (MP4GetTrackNumberOfEdits(fileHandle, Id)) { while (MP4GetTrackNumberOfEdits(fileHandle, Id)) {
uint64_t editListVersion = 0; uint64_t editListVersion = 0;
@@ -640,17 +643,17 @@ void setTrackStartOffset(MP4FileHandle fileHandle, MP4TrackId Id, int64_t offset
MP4GetTimeScale(fileHandle)); MP4GetTimeScale(fileHandle));
MP4Duration newOffsetDuration = MP4ConvertFromTrackDuration(fileHandle, MP4Duration newOffsetDuration = MP4ConvertFromTrackDuration(fileHandle,
Id, Id,
-offset, -offset_i,
MP4GetTimeScale(fileHandle)); MP4GetTimeScale(fileHandle));
MP4SetTrackEditDuration(fileHandle, Id, 1, oldEditDuration + oldEditMediaStart - newOffsetDuration); MP4SetTrackEditDuration(fileHandle, Id, 1, oldEditDuration + oldEditMediaStart - newOffsetDuration);
MP4SetTrackEditMediaStart(fileHandle, Id, 1, -offset); MP4SetTrackEditMediaStart(fileHandle, Id, 1, -offset_i);
} }
// Else create a new one. // Else create a new one.
else { else {
MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle, MP4Duration delayDuration = MP4ConvertFromTrackDuration(fileHandle,
Id, Id,
-offset, -offset_i,
MP4GetTimeScale(fileHandle)); MP4GetTimeScale(fileHandle));
MP4AddTrackEdit(fileHandle, Id, 1, -1, delayDuration, 0); MP4AddTrackEdit(fileHandle, Id, 1, -1, delayDuration, 0);
+1 -1
View File
@@ -38,7 +38,7 @@
newTrack.format = kMP42SubtitleCodecType_3GText; newTrack.format = kMP42SubtitleCodecType_3GText;
newTrack.URL = self.fileURL; newTrack.URL = self.fileURL;
newTrack.alternate_group = 2; newTrack.alternateGroup = 2;
newTrack.language = getFilenameLanguage((CFStringRef)self.fileURL.path); newTrack.language = getFilenameLanguage((CFStringRef)self.fileURL.path);
// Check if a 10.10 only class is available, NSLinguisticTagger crashes on 10.9 // Check if a 10.10 only class is available, NSLinguisticTagger crashes on 10.9
+2 -2
View File
@@ -40,8 +40,8 @@ NS_ASSUME_NONNULL_BEGIN
@property(nonatomic, readwrite, copy) NSSet<NSString *> *mediaCharacteristicTags; @property(nonatomic, readwrite, copy) NSSet<NSString *> *mediaCharacteristicTags;
@property(nonatomic, readwrite) uint64_t alternate_group; @property(nonatomic, readwrite) uint64_t alternateGroup;
@property(nonatomic, readwrite) int64_t startOffset; @property(nonatomic, readwrite) NSTimeInterval startOffset;
@property(nonatomic, readwrite, copy) MP42ConversionSettings *conversionSettings; @property(nonatomic, readwrite, copy) MP42ConversionSettings *conversionSettings;
+16 -15
View File
@@ -22,8 +22,9 @@
NSString *_language; NSString *_language;
BOOL _enabled; BOOL _enabled;
uint64_t _alternate_group; uint64_t _alternateGroup;
int64_t _startOffset;
NSTimeInterval _startOffset;
} }
@property(nonatomic, readwrite) MP42TrackId trackId; @property(nonatomic, readwrite) MP42TrackId trackId;
@@ -104,7 +105,7 @@
_enabled = NO; _enabled = NO;
} }
MP4GetTrackIntegerProperty(fileHandle, _trackId, "tkhd.alternate_group", &_alternate_group); MP4GetTrackIntegerProperty(fileHandle, _trackId, "tkhd.alternate_group", &_alternateGroup);
// Media characteristic tags // Media characteristic tags
NSMutableSet *mediaCharacteristicTags = [[NSMutableSet alloc] init]; NSMutableSet *mediaCharacteristicTags = [[NSMutableSet alloc] init];
@@ -165,7 +166,7 @@
copy->_name = [_name copy]; copy->_name = [_name copy];
copy->_language = [_language copy]; copy->_language = [_language copy];
copy->_enabled = _enabled; copy->_enabled = _enabled;
copy->_alternate_group = _alternate_group; copy->_alternateGroup = _alternateGroup;
copy->_startOffset = _startOffset; copy->_startOffset = _startOffset;
copy->_dataLength = _dataLength; copy->_dataLength = _dataLength;
@@ -212,7 +213,7 @@
} }
if (_updatedProperty[@"alternate_group"] || !_muxed) { if (_updatedProperty[@"alternate_group"] || !_muxed) {
MP4SetTrackIntegerProperty(fileHandle, _trackId, "tkhd.alternate_group", _alternate_group); MP4SetTrackIntegerProperty(fileHandle, _trackId, "tkhd.alternate_group", _alternateGroup);
} }
if (_updatedProperty[@"start_offset"]) { if (_updatedProperty[@"start_offset"]) {
@@ -312,22 +313,22 @@
return _enabled; return _enabled;
} }
- (uint64_t)alternate_group { - (uint64_t)alternateGroup {
return _alternate_group; return _alternateGroup;
} }
- (void)setAlternate_group:(uint64_t)newGroup - (void)setAlternateGroup:(uint64_t)newGroup
{ {
_alternate_group = newGroup; _alternateGroup = newGroup;
self.edited = YES; self.edited = YES;
_updatedProperty[@"alternate_group"] = @YES; _updatedProperty[@"alternate_group"] = @YES;
} }
- (int64_t)startOffset { - (NSTimeInterval)startOffset {
return _startOffset; return _startOffset;
} }
- (void)setStartOffset:(int64_t)newOffset - (void)setStartOffset:(NSTimeInterval)newOffset
{ {
_startOffset = newOffset; _startOffset = newOffset;
self.edited = YES; self.edited = YES;
@@ -390,8 +391,8 @@
[coder encodeBool:_enabled forKey:@"enabled"]; [coder encodeBool:_enabled forKey:@"enabled"];
[coder encodeInt64:_alternate_group forKey:@"alternate_group"]; [coder encodeInt64:_alternateGroup forKey:@"alternate_group"];
[coder encodeInt64:_startOffset forKey:@"startOffset"]; [coder encodeDouble:_startOffset forKey:@"startOffset"];
[coder encodeBool:_edited forKey:@"isEdited"]; [coder encodeBool:_edited forKey:@"isEdited"];
[coder encodeBool:_muxed forKey:@"muxed"]; [coder encodeBool:_muxed forKey:@"muxed"];
@@ -437,8 +438,8 @@
_enabled = [decoder decodeBoolForKey:@"enabled"]; _enabled = [decoder decodeBoolForKey:@"enabled"];
_alternate_group = [decoder decodeInt64ForKey:@"alternate_group"]; _alternateGroup = [decoder decodeInt64ForKey:@"alternate_group"];
_startOffset = [decoder decodeInt64ForKey:@"startOffset"]; _startOffset = [decoder decodeDoubleForKey:@"startOffset"];
_edited = [decoder decodeBoolForKey:@"isEdited"]; _edited = [decoder decodeBoolForKey:@"isEdited"];
_muxed = [decoder decodeBoolForKey:@"muxed"]; _muxed = [decoder decodeBoolForKey:@"muxed"];
+1 -1
View File
@@ -241,7 +241,7 @@ static NSArray<SBVobSubTrack *> * LoadVobSubSubtitles(NSURL *theDirectory, NSStr
newTrack.format = kMP42SubtitleCodecType_VobSub; newTrack.format = kMP42SubtitleCodecType_VobSub;
newTrack.URL = self.fileURL; newTrack.URL = self.fileURL;
newTrack.alternate_group = 2; newTrack.alternateGroup = 2;
newTrack.trackId = count++; newTrack.trackId = count++;
newTrack.language = [MP42Languages.defaultManager extendedTagForISO_639_1:track->language]; newTrack.language = [MP42Languages.defaultManager extendedTagForISO_639_1:track->language];
newTrack.duration = track->duration; newTrack.duration = track->duration;