Improve AVFoundation demuxing speed.
This commit is contained in:
3 files changed
+56
-26
No files matched your search
+51
-22
@@ -26,6 +26,12 @@
|
||||
|
||||
@interface AVFDemuxHelper : NSObject {
|
||||
@public
|
||||
MP42TrackId sourceID;
|
||||
CMTimeScale timescale;
|
||||
CMTimeValue currentTime;
|
||||
|
||||
uint32_t done;
|
||||
|
||||
AVAssetReaderOutput *assetReaderOutput;
|
||||
MP42EditListsReconstructor *editsConstructor;
|
||||
}
|
||||
@@ -920,14 +926,19 @@
|
||||
for (MP42Track *track in self.inputTracks) {
|
||||
AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[_localAsset trackWithTrackID:track.sourceId]
|
||||
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!");
|
||||
}
|
||||
|
||||
[assetReader addOutput:assetReaderOutput];
|
||||
|
||||
track.muxer_helper->demuxer_context = [[AVFDemuxHelper alloc] init];
|
||||
demuxHelper = track.muxer_helper->demuxer_context;
|
||||
demuxHelper->sourceID = track.sourceId;
|
||||
demuxHelper->timescale = [self timescaleForTrack:track];
|
||||
demuxHelper->assetReaderOutput = assetReaderOutput;
|
||||
demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init];
|
||||
|
||||
@@ -939,16 +950,29 @@
|
||||
|
||||
if (!success) {
|
||||
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) {
|
||||
demuxHelper = track.muxer_helper->demuxer_context;
|
||||
AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput;
|
||||
|
||||
while (!self.isCancelled) {
|
||||
|
||||
while (demuxHelper->currentTime < demuxHelper->timescale * currentTime && !demuxHelper->done) {
|
||||
CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
|
||||
CMTimeScale timescale = [self timescaleForTrack:track];
|
||||
|
||||
if (sampleBuffer) {
|
||||
|
||||
@@ -959,7 +983,7 @@
|
||||
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(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
|
||||
BOOL sync = 1;
|
||||
@@ -998,10 +1022,10 @@
|
||||
sample->decodeTimestamp = decodeTimeStamp.value;
|
||||
sample->presentationTimestamp = presentationTimeStamp.value;
|
||||
sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
|
||||
sample->timescale = timescale;
|
||||
sample->timescale = demuxHelper->timescale;
|
||||
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||
sample->trackId = track.sourceId;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
sample->attachments = (void *)attachments;
|
||||
|
||||
[demuxHelper->editsConstructor addSample:sample];
|
||||
@@ -1009,6 +1033,7 @@
|
||||
[sample release];
|
||||
|
||||
currentDataLength += bufferSize;
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
}
|
||||
// The CMSampleBufferRef contains more than one sample
|
||||
else if (samplesNum > 1) {
|
||||
@@ -1111,13 +1136,13 @@
|
||||
sample->data = sampleData;
|
||||
sample->size = sampleSize;
|
||||
sample->duration = sampleTimingInfo.duration.value;
|
||||
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
||||
//sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
||||
sample->presentationTimestamp = presentationTimeStamp.value;
|
||||
sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
|
||||
sample->timescale = timescale;
|
||||
sample->timescale = demuxHelper->timescale;
|
||||
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||
sample->trackId = track.sourceId;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
|
||||
if (attachments && i == (samplesNum - 1) && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) {
|
||||
CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments);
|
||||
@@ -1137,37 +1162,41 @@
|
||||
currentDataLength += sampleSize;
|
||||
}
|
||||
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
|
||||
free(timingArrayOut);
|
||||
free(sizeArrayOut);
|
||||
}
|
||||
else {
|
||||
CMTime currentDuration = CMTimeConvertScale(duration, timescale, kCMTimeRoundingMethod_Default);
|
||||
CMTime currentTimeStamp = CMTimeConvertScale(presentationTimeStamp, timescale, kCMTimeRoundingMethod_Default);
|
||||
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, timescale, kCMTimeRoundingMethod_Default);
|
||||
CMTime currentDuration = CMTimeConvertScale(duration, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
|
||||
CMTime currentTimeStamp = CMTimeConvertScale(presentationTimeStamp, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
|
||||
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
|
||||
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->duration = currentDuration.value;
|
||||
sample->presentationTimestamp = currentTimeStamp.value;
|
||||
sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
|
||||
sample->timescale = timescale;
|
||||
sample->timescale = demuxHelper->timescale;
|
||||
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||
sample->trackId = track.sourceId;
|
||||
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
sample->attachments = (void *)attachments;
|
||||
|
||||
[demuxHelper->editsConstructor addSample:sample];
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
}
|
||||
CFRelease(sampleBuffer);
|
||||
|
||||
self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100);
|
||||
|
||||
} else {
|
||||
break;
|
||||
demuxHelper->done = YES;
|
||||
[demuxHelper->editsConstructor done];
|
||||
tracksDone += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[demuxHelper->editsConstructor done];
|
||||
self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100);
|
||||
currentTime += 2;
|
||||
}
|
||||
|
||||
[assetReader release];
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
@interface MP4DemuxHelper : NSObject {
|
||||
@public
|
||||
MP4TrackId sourceID;
|
||||
|
||||
MP4SampleId currentSampleId;
|
||||
uint64_t totalSampleNumber;
|
||||
MP4Timestamp currentTime;
|
||||
@@ -294,6 +296,7 @@
|
||||
for (MP42Track *track in inputTracks) {
|
||||
track.muxer_helper->demuxer_context = [[MP4DemuxHelper alloc] init];
|
||||
demuxHelper = track.muxer_helper->demuxer_context;
|
||||
demuxHelper->sourceID = track.sourceId;
|
||||
demuxHelper->totalSampleNumber = MP4GetTrackNumberOfSamples(_fileHandle, track.sourceId);
|
||||
demuxHelper->timeScale = MP4GetTrackTimeScale(_fileHandle, track.sourceId);
|
||||
demuxHelper->done = 0;
|
||||
@@ -317,7 +320,6 @@
|
||||
}
|
||||
|
||||
while (demuxHelper->currentTime < demuxHelper->timeScale * currentTime && !demuxHelper->done) {
|
||||
MP4TrackId srcTrackId = [track sourceId];
|
||||
uint8_t *pBytes = NULL;
|
||||
uint32_t numBytes = 0;
|
||||
MP4Duration duration;
|
||||
@@ -333,7 +335,7 @@
|
||||
}
|
||||
|
||||
if (!MP4ReadSample(_fileHandle,
|
||||
srcTrackId,
|
||||
demuxHelper->sourceID,
|
||||
demuxHelper->currentSampleId,
|
||||
&pBytes, &numBytes,
|
||||
&pStartTime, &duration, &renderingOffset,
|
||||
@@ -350,7 +352,7 @@
|
||||
sample->offset = renderingOffset;
|
||||
sample->decodeTimestamp = pStartTime;
|
||||
sample->flags |= isSyncSample ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->trackId = track.sourceId;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
|
||||
[self enqueue:sample];
|
||||
[sample release];
|
||||
|
||||
@@ -531,7 +531,6 @@
|
||||
if (sampleBuffer->flags & MP42SampleBufferFlagEndOfFile) {
|
||||
track.muxer_helper->done = 1;
|
||||
}
|
||||
|
||||
else if (!MP4WriteSample(_fileHandle, trackId,
|
||||
sampleBuffer->data, sampleBuffer->size,
|
||||
sampleBuffer->duration, sampleBuffer->offset,
|
||||
|
||||
Reference in new issue
Block a user