Improve AVFoundation demuxing speed.

This commit is contained in:
Damiano Galassi committed 2017-09-14 13:21:34 +02:00
1 parent 876d1f2562
commit a707918ce0
3 files changed
+56 -26

No files matched your search

+51 -22
View File
@@ -26,6 +26,12 @@
@interface AVFDemuxHelper : NSObject { @interface AVFDemuxHelper : NSObject {
@public @public
MP42TrackId sourceID;
CMTimeScale timescale;
CMTimeValue currentTime;
uint32_t done;
AVAssetReaderOutput *assetReaderOutput; AVAssetReaderOutput *assetReaderOutput;
MP42EditListsReconstructor *editsConstructor; MP42EditListsReconstructor *editsConstructor;
} }
@@ -920,14 +926,19 @@
for (MP42Track *track in self.inputTracks) { for (MP42Track *track in self.inputTracks) {
AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[_localAsset trackWithTrackID:track.sourceId] AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[_localAsset trackWithTrackID:track.sourceId]
outputSettings:nil]; outputSettings:nil];
if (![assetReader canAddOutput:assetReaderOutput]) { assetReaderOutput.alwaysCopiesSampleData = NO;
if ([assetReader canAddOutput:assetReaderOutput]) {
[assetReader addOutput:assetReaderOutput];
}
else {
NSLog(@"Unable to add the output to assetReader!"); NSLog(@"Unable to add the output to assetReader!");
} }
[assetReader addOutput:assetReaderOutput];
track.muxer_helper->demuxer_context = [[AVFDemuxHelper alloc] init]; track.muxer_helper->demuxer_context = [[AVFDemuxHelper alloc] init];
demuxHelper = track.muxer_helper->demuxer_context; demuxHelper = track.muxer_helper->demuxer_context;
demuxHelper->sourceID = track.sourceId;
demuxHelper->timescale = [self timescaleForTrack:track];
demuxHelper->assetReaderOutput = assetReaderOutput; demuxHelper->assetReaderOutput = assetReaderOutput;
demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init]; demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init];
@@ -939,16 +950,29 @@
if (!success) { if (!success) {
localError = [assetReader error]; localError = [assetReader error];
[assetReader release];
[self setDone];
[pool release];
return;
}
CMTimeValue currentTime = 1;
NSInteger tracksNumber = self.inputTracks.count;
NSInteger tracksDone = 0;
while (tracksDone != tracksNumber) {
if (self.isCancelled) {
break;
} }
for (MP42Track *track in self.inputTracks) { for (MP42Track *track in self.inputTracks) {
demuxHelper = track.muxer_helper->demuxer_context; demuxHelper = track.muxer_helper->demuxer_context;
AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput; AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput;
while (!self.isCancelled) { while (demuxHelper->currentTime < demuxHelper->timescale * currentTime && !demuxHelper->done) {
CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer]; CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
CMTimeScale timescale = [self timescaleForTrack:track];
if (sampleBuffer) { if (sampleBuffer) {
@@ -959,7 +983,7 @@
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer); CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default); CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
// Read sample attachment, to mark the frame as sync // Read sample attachment, to mark the frame as sync
BOOL sync = 1; BOOL sync = 1;
@@ -998,10 +1022,10 @@
sample->decodeTimestamp = decodeTimeStamp.value; sample->decodeTimestamp = decodeTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value;
sample->presentationOutputTimestamp = currentOutputTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
sample->timescale = timescale; sample->timescale = demuxHelper->timescale;
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
sample->trackId = track.sourceId; sample->trackId = demuxHelper->sourceID;
sample->attachments = (void *)attachments; sample->attachments = (void *)attachments;
[demuxHelper->editsConstructor addSample:sample]; [demuxHelper->editsConstructor addSample:sample];
@@ -1009,6 +1033,7 @@
[sample release]; [sample release];
currentDataLength += bufferSize; currentDataLength += bufferSize;
demuxHelper->currentTime = currentOutputTimeStamp.value;
} }
// The CMSampleBufferRef contains more than one sample // The CMSampleBufferRef contains more than one sample
else if (samplesNum > 1) { else if (samplesNum > 1) {
@@ -1111,13 +1136,13 @@
sample->data = sampleData; sample->data = sampleData;
sample->size = sampleSize; sample->size = sampleSize;
sample->duration = sampleTimingInfo.duration.value; sample->duration = sampleTimingInfo.duration.value;
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value; //sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
sample->presentationTimestamp = presentationTimeStamp.value; sample->presentationTimestamp = presentationTimeStamp.value;
sample->presentationOutputTimestamp = currentOutputTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
sample->timescale = timescale; sample->timescale = demuxHelper->timescale;
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
sample->trackId = track.sourceId; sample->trackId = demuxHelper->sourceID;
if (attachments && i == (samplesNum - 1) && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) { if (attachments && i == (samplesNum - 1) && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) {
CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments); CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments);
@@ -1137,37 +1162,41 @@
currentDataLength += sampleSize; currentDataLength += sampleSize;
} }
demuxHelper->currentTime = currentOutputTimeStamp.value;
free(timingArrayOut); free(timingArrayOut);
free(sizeArrayOut); free(sizeArrayOut);
} }
else { else {
CMTime currentDuration = CMTimeConvertScale(duration, timescale, kCMTimeRoundingMethod_Default); CMTime currentDuration = CMTimeConvertScale(duration, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
CMTime currentTimeStamp = CMTimeConvertScale(presentationTimeStamp, timescale, kCMTimeRoundingMethod_Default); CMTime currentTimeStamp = CMTimeConvertScale(presentationTimeStamp, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default); CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
sample->duration = currentDuration.value; sample->duration = currentDuration.value;
sample->presentationTimestamp = currentTimeStamp.value; sample->presentationTimestamp = currentTimeStamp.value;
sample->presentationOutputTimestamp = currentOutputTimeStamp.value; sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
sample->timescale = timescale; sample->timescale = demuxHelper->timescale;
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0; sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0; sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
sample->trackId = track.sourceId; sample->trackId = demuxHelper->sourceID;
sample->attachments = (void *)attachments; sample->attachments = (void *)attachments;
[demuxHelper->editsConstructor addSample:sample]; [demuxHelper->editsConstructor addSample:sample];
demuxHelper->currentTime = currentOutputTimeStamp.value;
} }
CFRelease(sampleBuffer); CFRelease(sampleBuffer);
self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100);
} else { } else {
break; demuxHelper->done = YES;
[demuxHelper->editsConstructor done];
tracksDone += 1;
}
} }
} }
[demuxHelper->editsConstructor done]; self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100);
currentTime += 2;
} }
[assetReader release]; [assetReader release];
+5 -3
View File
@@ -20,6 +20,8 @@
@interface MP4DemuxHelper : NSObject { @interface MP4DemuxHelper : NSObject {
@public @public
MP4TrackId sourceID;
MP4SampleId currentSampleId; MP4SampleId currentSampleId;
uint64_t totalSampleNumber; uint64_t totalSampleNumber;
MP4Timestamp currentTime; MP4Timestamp currentTime;
@@ -294,6 +296,7 @@
for (MP42Track *track in inputTracks) { for (MP42Track *track in inputTracks) {
track.muxer_helper->demuxer_context = [[MP4DemuxHelper alloc] init]; track.muxer_helper->demuxer_context = [[MP4DemuxHelper alloc] init];
demuxHelper = track.muxer_helper->demuxer_context; demuxHelper = track.muxer_helper->demuxer_context;
demuxHelper->sourceID = track.sourceId;
demuxHelper->totalSampleNumber = MP4GetTrackNumberOfSamples(_fileHandle, track.sourceId); demuxHelper->totalSampleNumber = MP4GetTrackNumberOfSamples(_fileHandle, track.sourceId);
demuxHelper->timeScale = MP4GetTrackTimeScale(_fileHandle, track.sourceId); demuxHelper->timeScale = MP4GetTrackTimeScale(_fileHandle, track.sourceId);
demuxHelper->done = 0; demuxHelper->done = 0;
@@ -317,7 +320,6 @@
} }
while (demuxHelper->currentTime < demuxHelper->timeScale * currentTime && !demuxHelper->done) { while (demuxHelper->currentTime < demuxHelper->timeScale * currentTime && !demuxHelper->done) {
MP4TrackId srcTrackId = [track sourceId];
uint8_t *pBytes = NULL; uint8_t *pBytes = NULL;
uint32_t numBytes = 0; uint32_t numBytes = 0;
MP4Duration duration; MP4Duration duration;
@@ -333,7 +335,7 @@
} }
if (!MP4ReadSample(_fileHandle, if (!MP4ReadSample(_fileHandle,
srcTrackId, demuxHelper->sourceID,
demuxHelper->currentSampleId, demuxHelper->currentSampleId,
&pBytes, &numBytes, &pBytes, &numBytes,
&pStartTime, &duration, &renderingOffset, &pStartTime, &duration, &renderingOffset,
@@ -350,7 +352,7 @@
sample->offset = renderingOffset; sample->offset = renderingOffset;
sample->decodeTimestamp = pStartTime; sample->decodeTimestamp = pStartTime;
sample->flags |= isSyncSample ? MP42SampleBufferFlagIsSync : 0; sample->flags |= isSyncSample ? MP42SampleBufferFlagIsSync : 0;
sample->trackId = track.sourceId; sample->trackId = demuxHelper->sourceID;
[self enqueue:sample]; [self enqueue:sample];
[sample release]; [sample release];
-1
View File
@@ -531,7 +531,6 @@
if (sampleBuffer->flags & MP42SampleBufferFlagEndOfFile) { if (sampleBuffer->flags & MP42SampleBufferFlagEndOfFile) {
track.muxer_helper->done = 1; track.muxer_helper->done = 1;
} }
else if (!MP4WriteSample(_fileHandle, trackId, else if (!MP4WriteSample(_fileHandle, trackId,
sampleBuffer->data, sampleBuffer->size, sampleBuffer->data, sampleBuffer->size,
sampleBuffer->duration, sampleBuffer->offset, sampleBuffer->duration, sampleBuffer->offset,