Fix PCM reading performance in MP42AVFoundation importer.
This commit is contained in:
1 parent
c9b59d96f0
commit
c1e174996b
2 files changed
+111
-57
No files matched your search
+3
-2
@@ -3,7 +3,8 @@
|
|||||||
// MP42Foundation
|
// MP42Foundation
|
||||||
//
|
//
|
||||||
// Created by Damiano Galassi on 23/07/2016.
|
// Created by Damiano Galassi on 23/07/2016.
|
||||||
// Copyright © 2016 Damiano Galassi. All rights reserved.
|
//
|
||||||
|
// Useful utilites from FFmpeg source code.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "FFmpegUtils.h"
|
#include "FFmpegUtils.h"
|
||||||
@@ -114,7 +115,7 @@ static const struct {
|
|||||||
static const struct {
|
static const struct {
|
||||||
AudioFormatFlags mFormatFlags;
|
AudioFormatFlags mFormatFlags;
|
||||||
UInt32 mBitsPerChannel;
|
UInt32 mBitsPerChannel;
|
||||||
enum AVCodecID codecID;
|
enum AVCodecID codecID;
|
||||||
} kAudioPCMCodecMap[] =
|
} kAudioPCMCodecMap[] =
|
||||||
{
|
{
|
||||||
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked, 16, AV_CODEC_ID_PCM_S16LE },
|
{ kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked, 16, AV_CODEC_ID_PCM_S16LE },
|
||||||
|
|||||||
+108
-55
@@ -184,6 +184,7 @@
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
audioTrack.channels = 1;
|
audioTrack.channels = 1;
|
||||||
|
audioTrack.channelLayoutTag = kAudioChannelLayoutTag_Mono;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newTrack = audioTrack;
|
newTrack = audioTrack;
|
||||||
@@ -898,49 +899,18 @@
|
|||||||
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
|
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
|
||||||
size_t bufferSize = CMBlockBufferGetDataLength(buffer);
|
size_t bufferSize = CMBlockBufferGetDataLength(buffer);
|
||||||
|
|
||||||
int pos = 0;
|
// Don't split the buffer if the format is PCM
|
||||||
for (int i = 0; i < samplesNum; i++) {
|
if (track.format == kMP42AudioCodecType_LinearPCM) {
|
||||||
CMSampleTimingInfo sampleTimingInfo;
|
size_t sampleSize = bufferSize;
|
||||||
__unused CMTime decodeTimeStamp;
|
|
||||||
CMTime presentationTimeStamp;
|
|
||||||
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
|
|
||||||
|
|
||||||
size_t sampleSize;
|
|
||||||
|
|
||||||
// If the size of sample timing array is equal to 1, it means every sample has got the same timing
|
|
||||||
if (timingArrayEntries == 1) {
|
|
||||||
sampleTimingInfo = timingArrayOut[0];
|
|
||||||
decodeTimeStamp = sampleTimingInfo.decodeTimeStamp;
|
|
||||||
decodeTimeStamp.value = decodeTimeStamp.value + ( sampleTimingInfo.duration.value * i);
|
|
||||||
|
|
||||||
presentationTimeStamp = sampleTimingInfo.presentationTimeStamp;
|
|
||||||
presentationTimeStamp.value = presentationTimeStamp.value + ( sampleTimingInfo.duration.value * i);
|
|
||||||
} else {
|
|
||||||
sampleTimingInfo = timingArrayOut[i];
|
|
||||||
decodeTimeStamp = sampleTimingInfo.decodeTimeStamp;
|
|
||||||
presentationTimeStamp = sampleTimingInfo.presentationTimeStamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
presentationOutputTimeStamp.value = presentationOutputTimeStamp.value + (sampleTimingInfo.duration.value * i /
|
|
||||||
((double) sampleTimingInfo.duration.timescale / presentationOutputTimeStamp.timescale));
|
|
||||||
|
|
||||||
// If the size of sample size array is equal to 1, it means every sample has got the same size
|
|
||||||
if (sizeArrayEntries == 1) {
|
|
||||||
sampleSize = sizeArrayOut[0];
|
|
||||||
} else {
|
|
||||||
sampleSize = sizeArrayOut[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!sampleSize) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *sampleData = malloc(sampleSize);
|
void *sampleData = malloc(sampleSize);
|
||||||
|
|
||||||
if (pos < bufferSize) {
|
CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, sampleData);
|
||||||
CMBlockBufferCopyDataBytes(buffer, pos, sampleSize, sampleData);
|
|
||||||
pos += sampleSize;
|
CMTime duration = CMSampleBufferGetDuration(sampleBuffer);
|
||||||
}
|
CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);
|
||||||
|
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
|
||||||
|
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
|
||||||
|
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, duration.timescale, kCMTimeRoundingMethod_QuickTime);
|
||||||
|
|
||||||
// Read sample attachment, sync to mark the frame as sync
|
// Read sample attachment, sync to mark the frame as sync
|
||||||
BOOL sync = 1;
|
BOOL sync = 1;
|
||||||
@@ -957,36 +927,119 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SB_AVF_DEBUG
|
|
||||||
NSLog(@"D: %lld, P: %lld, PO: %lld", decodeTimeStamp.value, presentationTimeStamp.value, presentationOutputTimeStamp.value);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Enqueues the new sample
|
// Enqueues the new sample
|
||||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||||
sample->data = sampleData;
|
sample->data = sampleData;
|
||||||
sample->size = sampleSize;
|
sample->size = sampleSize;
|
||||||
sample->duration = sampleTimingInfo.duration.value;
|
sample->duration = duration.value;
|
||||||
// FIXME
|
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
||||||
//sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
|
||||||
sample->presentationTimestamp = presentationTimeStamp.value;
|
sample->presentationTimestamp = presentationTimeStamp.value;
|
||||||
sample->presentationOutputTimestamp = presentationTimeStamp.value;
|
sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
|
||||||
sample->timescale = sampleTimingInfo.duration.timescale;
|
sample->timescale = duration.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 = track.sourceId;
|
||||||
|
|
||||||
if (attachmentsSent == NO) {
|
sample->attachments = (void *)attachments;
|
||||||
sample->attachments = (void *)attachments;
|
|
||||||
attachmentsSent = YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
[demuxHelper->editsConstructor addSample:sample];
|
[demuxHelper->editsConstructor addSample:sample];
|
||||||
[self enqueue:sample];
|
[self enqueue:sample];
|
||||||
[sample release];
|
[sample release];
|
||||||
|
|
||||||
demuxHelper->currentTime += sampleTimingInfo.duration.value;
|
demuxHelper->currentTime += duration.value;
|
||||||
currentDataLength += sampleSize;
|
currentDataLength += sampleSize;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
int pos = 0;
|
||||||
|
for (int i = 0; i < samplesNum; i++) {
|
||||||
|
CMSampleTimingInfo sampleTimingInfo;
|
||||||
|
__unused CMTime decodeTimeStamp;
|
||||||
|
CMTime presentationTimeStamp;
|
||||||
|
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
|
||||||
|
|
||||||
|
size_t sampleSize;
|
||||||
|
|
||||||
|
// If the size of sample timing array is equal to 1, it means every sample has got the same timing
|
||||||
|
if (timingArrayEntries == 1) {
|
||||||
|
sampleTimingInfo = timingArrayOut[0];
|
||||||
|
decodeTimeStamp = sampleTimingInfo.decodeTimeStamp;
|
||||||
|
decodeTimeStamp.value = decodeTimeStamp.value + ( sampleTimingInfo.duration.value * i);
|
||||||
|
|
||||||
|
presentationTimeStamp = sampleTimingInfo.presentationTimeStamp;
|
||||||
|
presentationTimeStamp.value = presentationTimeStamp.value + ( sampleTimingInfo.duration.value * i);
|
||||||
|
} else {
|
||||||
|
sampleTimingInfo = timingArrayOut[i];
|
||||||
|
decodeTimeStamp = sampleTimingInfo.decodeTimeStamp;
|
||||||
|
presentationTimeStamp = sampleTimingInfo.presentationTimeStamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
presentationOutputTimeStamp.value = presentationOutputTimeStamp.value + (sampleTimingInfo.duration.value * i /
|
||||||
|
((double) sampleTimingInfo.duration.timescale / presentationOutputTimeStamp.timescale));
|
||||||
|
|
||||||
|
// If the size of sample size array is equal to 1, it means every sample has got the same size
|
||||||
|
if (sizeArrayEntries == 1) {
|
||||||
|
sampleSize = sizeArrayOut[0];
|
||||||
|
} else {
|
||||||
|
sampleSize = sizeArrayOut[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sampleSize) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *sampleData = malloc(sampleSize);
|
||||||
|
|
||||||
|
if (pos < bufferSize) {
|
||||||
|
CMBlockBufferCopyDataBytes(buffer, pos, sampleSize, sampleData);
|
||||||
|
pos += sampleSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read sample attachment, sync to mark the frame as sync
|
||||||
|
BOOL sync = 1;
|
||||||
|
BOOL doNotDisplay = NO;
|
||||||
|
CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO);
|
||||||
|
if (attachmentsArray) {
|
||||||
|
for (NSDictionary *dict in (NSArray *)attachmentsArray) {
|
||||||
|
if ([dict valueForKey:(NSString *)kCMSampleAttachmentKey_NotSync]) {
|
||||||
|
sync = 0;
|
||||||
|
}
|
||||||
|
if ([dict valueForKey:(NSString*)kCMSampleAttachmentKey_DoNotDisplay]) {
|
||||||
|
doNotDisplay = YES;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef SB_AVF_DEBUG
|
||||||
|
NSLog(@"D: %lld, P: %lld, PO: %lld", decodeTimeStamp.value, presentationTimeStamp.value, presentationOutputTimeStamp.value);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Enqueues the new sample
|
||||||
|
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||||
|
sample->data = sampleData;
|
||||||
|
sample->size = sampleSize;
|
||||||
|
sample->duration = sampleTimingInfo.duration.value;
|
||||||
|
// FIXME
|
||||||
|
//sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
||||||
|
sample->presentationTimestamp = presentationTimeStamp.value;
|
||||||
|
sample->presentationOutputTimestamp = presentationTimeStamp.value;
|
||||||
|
sample->timescale = sampleTimingInfo.duration.timescale;
|
||||||
|
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||||
|
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||||
|
sample->trackId = track.sourceId;
|
||||||
|
|
||||||
|
if (attachmentsSent == NO) {
|
||||||
|
sample->attachments = (void *)attachments;
|
||||||
|
attachmentsSent = YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
[demuxHelper->editsConstructor addSample:sample];
|
||||||
|
[self enqueue:sample];
|
||||||
|
[sample release];
|
||||||
|
|
||||||
|
demuxHelper->currentTime += sampleTimingInfo.duration.value;
|
||||||
|
currentDataLength += sampleSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
free(timingArrayOut);
|
free(timingArrayOut);
|
||||||
free(sizeArrayOut);
|
free(sizeArrayOut);
|
||||||
|
|||||||
Reference in new issue
Block a user