From caa54060f2fbfd7a2c60457c7536e6431b00752d Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Thu, 5 Nov 2015 08:16:38 +0100 Subject: [PATCH] Fix offsets when recreating the edit lists from avfoundation. --- MP42/MP42AVFImporter.m | 24 ++++++++++++++++++++++-- MP42/MP42EditListsReconstructor.m | 1 + 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index a7ac0a5..e36812c 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -25,6 +25,8 @@ @interface AVFDemuxHelper : NSObject { @public int64_t currentTime; + int64_t minDisplayOffset; + AVAssetReaderOutput *assetReaderOutput; MP42EditListsReconstructor *editsConstructor; } @@ -836,6 +838,10 @@ sample->attachments = (void *)attachments; sample->doNotDisplay = doNotDisplay; + if (sample->offset < demuxHelper->minDisplayOffset) { + demuxHelper->minDisplayOffset = sample->offset; + } + [demuxHelper->editsConstructor addSample:sample]; [self enqueue:sample]; [sample release]; @@ -988,23 +994,37 @@ for (MP42Track *track in self.outputsTracks) { MP4Duration trackDuration = 0; + MP4TrackId trackId = track.trackId; + MP42Track *inputTrack = [self inputTrackWithTrackID:track.sourceId]; AVFDemuxHelper *helper = inputTrack.muxer_helper->demuxer_context; + // Make sure the sample offsets are all positive. + if (helper->minDisplayOffset != 0) { + MP4SampleId samplesCount = MP4GetTrackNumberOfSamples(fileHandle, trackId); + for (unsigned int i = 0; i < samplesCount; i++) { + MP4SetSampleRenderingOffset(fileHandle, + trackId, + 1 + i, + MP4GetSampleRenderingOffset(fileHandle, trackId, 1 + i) - helper->minDisplayOffset); + } + } + + // Add back the new constructed edit lists. for (uint64_t i = 0; i < helper->editsConstructor.editsCount; i++) { CMTimeRange timeRange = helper->editsConstructor.edits[i]; CMTime duration = CMTimeConvertScale(timeRange.duration, timescale, kCMTimeRoundingMethod_Default); trackDuration += duration.value; - MP4AddTrackEdit(fileHandle, track.trackId, MP4_INVALID_EDIT_ID, timeRange.start.value, + MP4AddTrackEdit(fileHandle, trackId, MP4_INVALID_EDIT_ID, timeRange.start.value - helper->minDisplayOffset, duration.value, 0); } if (trackDuration) { - MP4SetTrackIntegerProperty(fileHandle, track.trackId, "tkhd.duration", trackDuration); + MP4SetTrackIntegerProperty(fileHandle, trackId, "tkhd.duration", trackDuration); } } diff --git a/MP42/MP42EditListsReconstructor.m b/MP42/MP42EditListsReconstructor.m index 283eef9..74e50b6 100644 --- a/MP42/MP42EditListsReconstructor.m +++ b/MP42/MP42EditListsReconstructor.m @@ -33,6 +33,7 @@ if (_timescale == 0) { _timescale = extractedSample->timescale; + _currentTime += extractedSample->offset; } [self analyzeSample:extractedSample];