Port more classes to arc.
This commit is contained in:
10 files changed
+626
-641
No files matched your search
+242
-250
@@ -11,9 +11,7 @@
|
||||
|
||||
#import "MP42Languages.h"
|
||||
#import "MP42File.h"
|
||||
#import "MP42Image.h"
|
||||
|
||||
#import "mp4v2.h"
|
||||
#import "MP42PrivateUtilities.h"
|
||||
#import "MP42FormatUtilites.h"
|
||||
#import "MP42Track+Private.h"
|
||||
@@ -886,299 +884,293 @@
|
||||
|
||||
- (void)demux {
|
||||
@autoreleasepool {
|
||||
NSError *localError;
|
||||
BOOL success = YES;
|
||||
OSStatus err = noErr;
|
||||
CMTimeValue currentTime = 1;
|
||||
|
||||
CMTimeValue currentTime = 1;
|
||||
uint64_t currentDataLength = 0;
|
||||
uint64_t totalDataLength = 0;
|
||||
|
||||
uint64_t currentDataLength = 0;
|
||||
uint64_t totalDataLength = 0;
|
||||
NSInteger tracksDone = 0;
|
||||
NSInteger tracksNumber = self.inputTracks.count;
|
||||
|
||||
NSInteger tracksDone = 0;
|
||||
NSInteger tracksNumber = self.inputTracks.count;
|
||||
AVAssetReader *assetReader = [[AVAssetReader alloc] initWithAsset:_localAsset error:NULL];
|
||||
AVFDemuxHelper * helpers[tracksNumber];
|
||||
|
||||
AVAssetReader *assetReader = [[AVAssetReader alloc] initWithAsset:_localAsset error:&localError];
|
||||
AVFDemuxHelper * helpers[tracksNumber];
|
||||
if (assetReader != nil) {
|
||||
|
||||
success = (assetReader != nil);
|
||||
if (success) {
|
||||
NSUInteger index = 0;
|
||||
for (MP42Track *track in self.inputTracks) {
|
||||
AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[_localAsset trackWithTrackID:track.sourceId]
|
||||
outputSettings:nil];
|
||||
assetReaderOutput.alwaysCopiesSampleData = NO;
|
||||
for (NSUInteger index = 0; index < tracksNumber; index += 1) {
|
||||
MP42Track *track = self.inputTracks[index];
|
||||
AVAssetReaderOutput *assetReaderOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[_localAsset trackWithTrackID:track.sourceId]
|
||||
outputSettings:nil];
|
||||
assetReaderOutput.alwaysCopiesSampleData = NO;
|
||||
|
||||
if ([assetReader canAddOutput:assetReaderOutput]) {
|
||||
[assetReader addOutput:assetReaderOutput];
|
||||
if ([assetReader canAddOutput:assetReaderOutput]) {
|
||||
[assetReader addOutput:assetReaderOutput];
|
||||
}
|
||||
else {
|
||||
NSLog(@"Unable to add the output to assetReader!");
|
||||
}
|
||||
|
||||
AVFDemuxHelper *demuxHelper = [[AVFDemuxHelper alloc] init];
|
||||
demuxHelper->sourceID = track.sourceId;
|
||||
demuxHelper->timescale = [self timescaleForTrack:track];
|
||||
demuxHelper->format = track.format;
|
||||
demuxHelper->assetReaderOutput = assetReaderOutput;
|
||||
demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init];
|
||||
|
||||
track.demuxerHelper = demuxHelper;
|
||||
helpers[index] = demuxHelper;
|
||||
|
||||
totalDataLength += track.dataLength;
|
||||
}
|
||||
else {
|
||||
NSLog(@"Unable to add the output to assetReader!");
|
||||
}
|
||||
|
||||
AVFDemuxHelper *demuxHelper = [[AVFDemuxHelper alloc] init];
|
||||
demuxHelper->sourceID = track.sourceId;
|
||||
demuxHelper->timescale = [self timescaleForTrack:track];
|
||||
demuxHelper->format = track.format;
|
||||
demuxHelper->assetReaderOutput = assetReaderOutput;
|
||||
demuxHelper->editsConstructor = [[MP42EditListsReconstructor alloc] init];
|
||||
|
||||
track.demuxerHelper = demuxHelper;
|
||||
helpers[index] = demuxHelper;
|
||||
index += 1;
|
||||
|
||||
totalDataLength += track.dataLength;
|
||||
}
|
||||
}
|
||||
|
||||
success = [assetReader startReading];
|
||||
|
||||
if (!success) {
|
||||
localError = [assetReader error];
|
||||
[self setDone];
|
||||
return;
|
||||
}
|
||||
|
||||
while (tracksDone != tracksNumber) {
|
||||
if (self.isCancelled) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (NSUInteger index = 0; index < tracksNumber; index += 1) {
|
||||
AVFDemuxHelper *demuxHelper = helpers[index];
|
||||
AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput;
|
||||
if (![assetReader startReading]) {
|
||||
[self setDone];
|
||||
return;
|
||||
}
|
||||
|
||||
while (demuxHelper->currentTime < demuxHelper->timescale * currentTime && !demuxHelper->done) {
|
||||
CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
|
||||
while (tracksDone != tracksNumber) {
|
||||
if (self.isCancelled) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (sampleBuffer) {
|
||||
for (NSUInteger index = 0; index < tracksNumber; index += 1) {
|
||||
AVFDemuxHelper *demuxHelper = helpers[index];
|
||||
AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput;
|
||||
|
||||
CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer);
|
||||
while (demuxHelper->currentTime < demuxHelper->timescale * currentTime && !demuxHelper->done) {
|
||||
CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer];
|
||||
|
||||
CMTime duration = CMSampleBufferGetDuration(sampleBuffer);
|
||||
CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);
|
||||
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
|
||||
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
|
||||
if (sampleBuffer) {
|
||||
|
||||
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
|
||||
CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer);
|
||||
|
||||
// Read sample attachment, to mark the frame as sync
|
||||
BOOL sync = 1;
|
||||
BOOL doNotDisplay = NO;
|
||||
CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO);
|
||||
if (attachmentsArray) {
|
||||
for (NSDictionary *dict in (__bridge NSArray *)attachmentsArray) {
|
||||
if (dict[(NSString *)kCMSampleAttachmentKey_NotSync]) {
|
||||
sync = 0;
|
||||
}
|
||||
if (dict[(NSString *)kCMSampleAttachmentKey_DoNotDisplay]) {
|
||||
doNotDisplay = YES;
|
||||
CMTime duration = CMSampleBufferGetDuration(sampleBuffer);
|
||||
CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);
|
||||
CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
|
||||
CMTime presentationOutputTimeStamp = CMSampleBufferGetOutputPresentationTimeStamp(sampleBuffer);
|
||||
|
||||
CMTime currentOutputTimeStamp = CMTimeConvertScale(presentationOutputTimeStamp, demuxHelper->timescale, kCMTimeRoundingMethod_Default);
|
||||
|
||||
// Read sample attachment, to mark the frame as sync
|
||||
BOOL sync = 1;
|
||||
BOOL doNotDisplay = NO;
|
||||
CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, NO);
|
||||
if (attachmentsArray) {
|
||||
for (NSDictionary *dict in (__bridge NSArray *)attachmentsArray) {
|
||||
if (dict[(NSString *)kCMSampleAttachmentKey_NotSync]) {
|
||||
sync = 0;
|
||||
}
|
||||
if (dict[(NSString *)kCMSampleAttachmentKey_DoNotDisplay]) {
|
||||
doNotDisplay = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(NULL, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
|
||||
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(NULL, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
|
||||
|
||||
// Get CMBlockBufferRef to extract the actual data later
|
||||
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
|
||||
size_t bufferSize = CMBlockBufferGetDataLength(buffer);
|
||||
// Get CMBlockBufferRef to extract the actual data later
|
||||
CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);
|
||||
size_t bufferSize = CMBlockBufferGetDataLength(buffer);
|
||||
|
||||
// We have only a sample
|
||||
// or the format is PCM, if so send only a single buffer to improve performance
|
||||
if (samplesNum == 1 || (samplesNum > 1 && demuxHelper->format == kMP42AudioCodecType_LinearPCM)) {
|
||||
// We have only a sample
|
||||
// or the format is PCM, if so send only a single buffer to improve performance
|
||||
if (samplesNum == 1 || (samplesNum > 1 && demuxHelper->format == kMP42AudioCodecType_LinearPCM)) {
|
||||
|
||||
void *sampleData = malloc(bufferSize);
|
||||
CMBlockBufferCopyDataBytes(buffer, 0, bufferSize, sampleData);
|
||||
|
||||
// Enqueues the new sample
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->data = sampleData;
|
||||
sample->size = bufferSize;
|
||||
sample->duration = duration.value;
|
||||
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
||||
sample->decodeTimestamp = decodeTimeStamp.value;
|
||||
sample->presentationTimestamp = presentationTimeStamp.value;
|
||||
sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
|
||||
sample->timescale = demuxHelper->timescale;
|
||||
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
sample->attachments = (void *)attachments;
|
||||
|
||||
[demuxHelper->editsConstructor addSample:sample];
|
||||
[self enqueue:sample];
|
||||
|
||||
currentDataLength += bufferSize;
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
}
|
||||
// The CMSampleBufferRef contains more than one sample
|
||||
else if (samplesNum > 1) {
|
||||
if (!CMSampleBufferDataIsReady(sampleBuffer)) {
|
||||
CMSampleBufferMakeDataReady(sampleBuffer);
|
||||
}
|
||||
|
||||
// A CMSampleBufferRef can contains an multiple samples, check how many needs to be divided to separated MP42SampleBuffers
|
||||
// First get the array with the timings for each sample
|
||||
CMItemCount timingArrayEntries = 0;
|
||||
CMItemCount timingArrayEntriesNeededOut = 0;
|
||||
err = CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, timingArrayEntries, NULL, &timingArrayEntriesNeededOut);
|
||||
if (err) {
|
||||
CFRelease(sampleBuffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
CMSampleTimingInfo *timingArrayOut = malloc(sizeof(CMSampleTimingInfo) * timingArrayEntriesNeededOut);
|
||||
timingArrayEntries = timingArrayEntriesNeededOut;
|
||||
err = CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, timingArrayEntries, timingArrayOut, &timingArrayEntriesNeededOut);
|
||||
if (err) {
|
||||
free(timingArrayOut);
|
||||
CFRelease(sampleBuffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Then the array with the size of each sample
|
||||
CMItemCount sizeArrayEntries = 0;
|
||||
CMItemCount sizeArrayEntriesNeededOut = 0;
|
||||
err = CMSampleBufferGetSampleSizeArray(sampleBuffer, sizeArrayEntries, NULL, &sizeArrayEntriesNeededOut);
|
||||
if (err) {
|
||||
free(timingArrayOut);
|
||||
CFRelease(sampleBuffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t *sizeArrayOut = malloc(sizeof(size_t) * sizeArrayEntriesNeededOut);
|
||||
sizeArrayEntries = sizeArrayEntriesNeededOut;
|
||||
err = CMSampleBufferGetSampleSizeArray(sampleBuffer, sizeArrayEntries, sizeArrayOut, &sizeArrayEntriesNeededOut);
|
||||
if (err) {
|
||||
free(timingArrayOut);
|
||||
free(sizeArrayOut);
|
||||
CFRelease(sampleBuffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint64_t i = 0, pos = 0; i < samplesNum; i++) {
|
||||
CMSampleTimingInfo sampleTimingInfo;
|
||||
CMTime decodeTimeStamp;
|
||||
CMTime presentationTimeStamp;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
void *sampleData = malloc(bufferSize);
|
||||
CMBlockBufferCopyDataBytes(buffer, 0, bufferSize, sampleData);
|
||||
|
||||
// Enqueues the new sample
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
|
||||
if (attachments && i == 0 && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) {
|
||||
CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments);
|
||||
CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtEnd);
|
||||
sample->attachments = (void *)copy;
|
||||
CFDictionaryRef trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart);
|
||||
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
|
||||
trimStartTime = CMTimeConvertScale(trimStartTime, sampleTimingInfo.duration.value, kCMTimeRoundingMethod_Default);
|
||||
currentOutputTimeStamp.value -= trimStartTime.value;
|
||||
|
||||
}
|
||||
|
||||
sample->data = sampleData;
|
||||
sample->size = sampleSize;
|
||||
sample->duration = sampleTimingInfo.duration.value;
|
||||
//sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
||||
sample->size = bufferSize;
|
||||
sample->duration = duration.value;
|
||||
sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
||||
sample->decodeTimestamp = decodeTimeStamp.value;
|
||||
sample->presentationTimestamp = presentationTimeStamp.value;
|
||||
sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
|
||||
sample->timescale = demuxHelper->timescale;
|
||||
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
|
||||
if (attachments && i == (samplesNum - 1) && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) {
|
||||
CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments);
|
||||
CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtStart);
|
||||
sample->attachments = (void *)copy;
|
||||
CFDictionaryRef trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd);
|
||||
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
|
||||
trimEndTime = CMTimeConvertScale(trimEndTime, sampleTimingInfo.duration.value, kCMTimeRoundingMethod_Default);
|
||||
currentOutputTimeStamp.value -= trimEndTime.value;
|
||||
}
|
||||
sample->attachments = (void *)attachments;
|
||||
|
||||
[demuxHelper->editsConstructor addSample:sample];
|
||||
[self enqueue:sample];
|
||||
|
||||
currentOutputTimeStamp.value = currentOutputTimeStamp.value + sampleTimingInfo.duration.value;
|
||||
currentDataLength += sampleSize;
|
||||
currentDataLength += bufferSize;
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
}
|
||||
// The CMSampleBufferRef contains more than one sample
|
||||
else if (samplesNum > 1) {
|
||||
if (!CMSampleBufferDataIsReady(sampleBuffer)) {
|
||||
CMSampleBufferMakeDataReady(sampleBuffer);
|
||||
}
|
||||
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
// A CMSampleBufferRef can contains an multiple samples, check how many needs to be divided to separated MP42SampleBuffers
|
||||
// First get the array with the timings for each sample
|
||||
CMItemCount timingArrayEntries = 0;
|
||||
CMItemCount timingArrayEntriesNeededOut = 0;
|
||||
OSStatus err = noErr;
|
||||
|
||||
if (attachments) {
|
||||
CFRelease(attachments);
|
||||
err = CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, timingArrayEntries, NULL, &timingArrayEntriesNeededOut);
|
||||
if (err) {
|
||||
CFRelease(sampleBuffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
CMSampleTimingInfo *timingArrayOut = malloc(sizeof(CMSampleTimingInfo) * timingArrayEntriesNeededOut);
|
||||
timingArrayEntries = timingArrayEntriesNeededOut;
|
||||
err = CMSampleBufferGetSampleTimingInfoArray(sampleBuffer, timingArrayEntries, timingArrayOut, &timingArrayEntriesNeededOut);
|
||||
if (err) {
|
||||
free(timingArrayOut);
|
||||
CFRelease(sampleBuffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Then the array with the size of each sample
|
||||
CMItemCount sizeArrayEntries = 0;
|
||||
CMItemCount sizeArrayEntriesNeededOut = 0;
|
||||
err = CMSampleBufferGetSampleSizeArray(sampleBuffer, sizeArrayEntries, NULL, &sizeArrayEntriesNeededOut);
|
||||
if (err) {
|
||||
free(timingArrayOut);
|
||||
CFRelease(sampleBuffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t *sizeArrayOut = malloc(sizeof(size_t) * sizeArrayEntriesNeededOut);
|
||||
sizeArrayEntries = sizeArrayEntriesNeededOut;
|
||||
err = CMSampleBufferGetSampleSizeArray(sampleBuffer, sizeArrayEntries, sizeArrayOut, &sizeArrayEntriesNeededOut);
|
||||
if (err) {
|
||||
free(timingArrayOut);
|
||||
free(sizeArrayOut);
|
||||
CFRelease(sampleBuffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint64_t i = 0, pos = 0; i < samplesNum; i++) {
|
||||
CMSampleTimingInfo sampleTimingInfo;
|
||||
CMTime decodeTimeStamp;
|
||||
CMTime presentationTimeStamp;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Enqueues the new sample
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
|
||||
if (attachments && i == 0 && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart)) {
|
||||
CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments);
|
||||
CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtEnd);
|
||||
sample->attachments = (void *)copy;
|
||||
CFDictionaryRef trimStart = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtStart);
|
||||
CMTime trimStartTime = CMTimeMakeFromDictionary(trimStart);
|
||||
trimStartTime = CMTimeConvertScale(trimStartTime, sampleTimingInfo.duration.value, kCMTimeRoundingMethod_Default);
|
||||
currentOutputTimeStamp.value -= trimStartTime.value;
|
||||
|
||||
}
|
||||
|
||||
sample->data = sampleData;
|
||||
sample->size = sampleSize;
|
||||
sample->duration = sampleTimingInfo.duration.value;
|
||||
//sample->offset = -decodeTimeStamp.value + presentationTimeStamp.value;
|
||||
sample->presentationTimestamp = presentationTimeStamp.value;
|
||||
sample->presentationOutputTimestamp = currentOutputTimeStamp.value;
|
||||
sample->timescale = demuxHelper->timescale;
|
||||
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
|
||||
if (attachments && i == (samplesNum - 1) && CFDictionaryContainsKey(attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd)) {
|
||||
CFMutableDictionaryRef copy = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 2, attachments);
|
||||
CFDictionaryRemoveValue(copy, kCMSampleBufferAttachmentKey_TrimDurationAtStart);
|
||||
sample->attachments = (void *)copy;
|
||||
CFDictionaryRef trimEnd = CFDictionaryGetValue(sample->attachments, kCMSampleBufferAttachmentKey_TrimDurationAtEnd);
|
||||
CMTime trimEndTime = CMTimeMakeFromDictionary(trimEnd);
|
||||
trimEndTime = CMTimeConvertScale(trimEndTime, sampleTimingInfo.duration.value, kCMTimeRoundingMethod_Default);
|
||||
currentOutputTimeStamp.value -= trimEndTime.value;
|
||||
}
|
||||
|
||||
[demuxHelper->editsConstructor addSample:sample];
|
||||
[self enqueue:sample];
|
||||
|
||||
currentOutputTimeStamp.value = currentOutputTimeStamp.value + sampleTimingInfo.duration.value;
|
||||
currentDataLength += sampleSize;
|
||||
}
|
||||
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
|
||||
if (attachments) {
|
||||
CFRelease(attachments);
|
||||
}
|
||||
free(timingArrayOut);
|
||||
free(sizeArrayOut);
|
||||
}
|
||||
free(timingArrayOut);
|
||||
free(sizeArrayOut);
|
||||
else {
|
||||
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 = demuxHelper->timescale;
|
||||
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
sample->attachments = (void *)attachments;
|
||||
|
||||
[demuxHelper->editsConstructor addSample:sample];
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
}
|
||||
CFRelease(sampleBuffer);
|
||||
|
||||
} else {
|
||||
demuxHelper->done = YES;
|
||||
[demuxHelper->editsConstructor done];
|
||||
tracksDone += 1;
|
||||
}
|
||||
else {
|
||||
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 = demuxHelper->timescale;
|
||||
sample->flags |= sync ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->flags |= doNotDisplay ? MP42SampleBufferFlagDoNotDisplay : 0;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
sample->attachments = (void *)attachments;
|
||||
|
||||
[demuxHelper->editsConstructor addSample:sample];
|
||||
demuxHelper->currentTime = currentOutputTimeStamp.value;
|
||||
}
|
||||
CFRelease(sampleBuffer);
|
||||
|
||||
} else {
|
||||
demuxHelper->done = YES;
|
||||
[demuxHelper->editsConstructor done];
|
||||
tracksDone += 1;
|
||||
}
|
||||
}
|
||||
|
||||
self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100);
|
||||
currentTime += 1;
|
||||
}
|
||||
|
||||
self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100);
|
||||
currentTime += 1;
|
||||
[self setDone];
|
||||
}
|
||||
|
||||
[self setDone];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)cleanUp:(MP4FileHandle)fileHandle {
|
||||
|
||||
+1
-1
@@ -936,7 +936,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
|
||||
|
||||
for (MP42Track *track in self.itracks) {
|
||||
if (!track.muxed) {
|
||||
// Reopen the file importer is they are not already open
|
||||
// Reopen the file importers if they are not already open
|
||||
// this happens when the object was unarchived from a file.
|
||||
if (![track isMemberOfClass:[MP42ChapterTrack class]]) {
|
||||
if (!track.importer && track.URL) {
|
||||
|
||||
@@ -1267,7 +1267,6 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
|
||||
|
||||
inFile = fopen(filePath, "r");
|
||||
if (inFile == NULL) {
|
||||
[avcCData release];
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1278,7 +1277,6 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
|
||||
if (LoadNal(&nal) == false) {
|
||||
// fprintf(stderr, "%s: Could not find sequence header\n", ProgName);
|
||||
fclose(inFile);
|
||||
[avcCData release];
|
||||
return nil;
|
||||
}
|
||||
uint32_t header_size = nal.buffer[2] == 1 ? 3 : 4;
|
||||
@@ -1313,8 +1311,6 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
|
||||
[avcCData appendBytes:&iy length:sizeof(uint8_t)];
|
||||
[avcCData appendData:seqData];
|
||||
|
||||
[seqData release];
|
||||
|
||||
// skip the nal type byte
|
||||
if (h264_read_seq_info(nal.buffer, nal.buffer_on, &h264_dec) == -1)
|
||||
{
|
||||
@@ -1343,9 +1339,6 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
|
||||
|
||||
[avcCData appendBytes:&iy length:sizeof(uint8_t)];
|
||||
[avcCData appendData:pictData];
|
||||
|
||||
[pictData release];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1389,7 +1382,6 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
|
||||
[newTrack setDataLength:[[[[NSFileManager defaultManager] attributesOfItemAtPath:self.fileURL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]];
|
||||
|
||||
[self addTrack:newTrack];
|
||||
[newTrack release];
|
||||
}
|
||||
|
||||
return self;
|
||||
@@ -1508,7 +1500,6 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
|
||||
sample->trackId = trackId;
|
||||
|
||||
[self enqueue:sample];
|
||||
[sample release];
|
||||
|
||||
currentSize += nal_buffer_size;
|
||||
self.progress = (currentSize / (CGFloat) _size) * 100;
|
||||
@@ -1595,8 +1586,7 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
|
||||
sample->trackId = trackId;
|
||||
|
||||
[self enqueue:sample];
|
||||
[sample release];
|
||||
|
||||
|
||||
currentSize += nal_buffer_size;
|
||||
self.progress = (currentSize / (CGFloat) _size) * 100;
|
||||
|
||||
@@ -1644,10 +1634,7 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
[avcC release];
|
||||
fclose(inFile);
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
@end
|
||||
+308
-305
@@ -39,6 +39,9 @@
|
||||
|
||||
@interface MatroskaDemuxHelper : NSObject {
|
||||
@public
|
||||
MP42TrackId sourceID;
|
||||
TrackInfo *trackInfo;
|
||||
|
||||
NSMutableArray<MatroskaSample *> *queue;
|
||||
|
||||
uint64_t currentTime;
|
||||
@@ -606,7 +609,7 @@ static NSString * TrackNameToString(TrackInfo *track)
|
||||
|
||||
- (NSUInteger)timescaleForTrack:(MP42Track *)track
|
||||
{
|
||||
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, [track sourceId]);
|
||||
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, track.sourceId);
|
||||
if (trackInfo->Type == TT_VIDEO) {
|
||||
return 100000;
|
||||
} else if (trackInfo->Type == TT_AUDIO) {
|
||||
@@ -715,7 +718,7 @@ static NSString * TrackNameToString(TrackInfo *track)
|
||||
}
|
||||
|
||||
if (context) {
|
||||
magicCookie = (__bridge NSData *)createCookie_EAC3(context);
|
||||
magicCookie = (NSData *)CFBridgingRelease(createCookie_EAC3(context));
|
||||
free(context);
|
||||
}
|
||||
}
|
||||
@@ -779,372 +782,372 @@ static NSString * TrackNameToString(TrackInfo *track)
|
||||
return trackInfo->CodecDelay != 0;
|
||||
}
|
||||
|
||||
// Methods to extract all the samples from the active tracks at the same time
|
||||
- (void)demux
|
||||
{
|
||||
@autoreleasepool {
|
||||
uint64_t StartTime, EndTime, FilePos;
|
||||
uint32_t Track, FrameSize, FrameFlags;
|
||||
uint8_t *frame = NULL;
|
||||
|
||||
MatroskaDemuxHelper *demuxHelper = nil;
|
||||
MatroskaSample *frameSample = nil, *currentSample = nil;
|
||||
int64_t duration, next_duration;
|
||||
const unsigned int bufferSize = 20;
|
||||
unsigned long TrackMask = ~0;
|
||||
|
||||
const unsigned int bufferSize = 20;
|
||||
NSArray<MP42Track *> *tracks = self.tracks;
|
||||
NSArray<MP42Track *> *inputTracks = self.inputTracks;
|
||||
|
||||
/* mask other tracks because we don't need them */
|
||||
unsigned long TrackMask = ~0;
|
||||
NSInteger tracksNumber = tracks.count;
|
||||
MatroskaDemuxHelper * helpers[tracksNumber];
|
||||
|
||||
NSArray<MP42Track *> *inputTracks = self.inputTracks;
|
||||
for (NSUInteger index = 0; index < tracksNumber; index += 1) {
|
||||
MP42Track *track = tracks[index];
|
||||
if ([inputTracks containsObject:track]) {
|
||||
|
||||
for (MP42Track *track in inputTracks) {
|
||||
TrackMask &= ~(1l << track.sourceId);
|
||||
track.demuxerHelper = [[MatroskaDemuxHelper alloc] init];
|
||||
}
|
||||
MatroskaDemuxHelper *demuxHelper = [[MatroskaDemuxHelper alloc] init];
|
||||
demuxHelper->sourceID = track.sourceId;
|
||||
demuxHelper->trackInfo = mkv_GetTrackInfo(_matroskaFile, track.sourceId);
|
||||
|
||||
mkv_SetTrackMask(_matroskaFile, TrackMask);
|
||||
helpers[index] = demuxHelper;
|
||||
|
||||
while (!mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) && !self.isCancelled) {
|
||||
self.progress = (StartTime / _fileDuration / 10000);
|
||||
track.demuxerHelper = demuxHelper;
|
||||
|
||||
MP42Track *track = nil;
|
||||
|
||||
for (MP42Track *fTrack in inputTracks){
|
||||
if (fTrack.sourceId == Track) {
|
||||
demuxHelper = fTrack.demuxerHelper;
|
||||
track = fTrack;
|
||||
TrackMask &= ~(1l << track.sourceId);
|
||||
}
|
||||
}
|
||||
|
||||
if (!demuxHelper) {
|
||||
continue;
|
||||
}
|
||||
// mask other tracks because we don't need them
|
||||
mkv_SetTrackMask(_matroskaFile, TrackMask);
|
||||
|
||||
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, Track);
|
||||
uint64_t StartTime, EndTime, FilePos;
|
||||
uint32_t Track, FrameSize, FrameFlags;
|
||||
uint8_t *frame = NULL;
|
||||
MatroskaSample *frameSample = nil, *currentSample = nil;
|
||||
int64_t duration, next_duration;
|
||||
|
||||
if (StartTime < demuxHelper->startTime) {
|
||||
demuxHelper->startTime = StartTime;
|
||||
}
|
||||
while (!mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) && !self.isCancelled) {
|
||||
self.progress = (StartTime / _fileDuration / 10000);
|
||||
|
||||
if (trackInfo->Type == TT_AUDIO) {
|
||||
demuxHelper->samplesWritten++;
|
||||
MatroskaDemuxHelper *demuxHelper = helpers[Track];
|
||||
TrackInfo *trackInfo = demuxHelper->trackInfo;
|
||||
|
||||
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) {
|
||||
if (StartTime < demuxHelper->startTime) {
|
||||
demuxHelper->startTime = StartTime;
|
||||
}
|
||||
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->data = frame;
|
||||
sample->size = FrameSize;
|
||||
sample->duration = MP4_INVALID_DURATION;
|
||||
sample->offset = 0;
|
||||
sample->decodeTimestamp = StartTime;
|
||||
sample->flags |= MP42SampleBufferFlagIsSync;
|
||||
sample->trackId = track.sourceId;
|
||||
if (trackInfo->Type == TT_AUDIO) {
|
||||
demuxHelper->samplesWritten++;
|
||||
|
||||
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) {
|
||||
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->data = frame;
|
||||
sample->size = FrameSize;
|
||||
sample->duration = MP4_INVALID_DURATION;
|
||||
sample->offset = 0;
|
||||
sample->decodeTimestamp = StartTime;
|
||||
sample->flags |= MP42SampleBufferFlagIsSync;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
|
||||
#define VARIABLE_AUDIO_RATE 1
|
||||
|
||||
#ifdef VARIABLE_AUDIO_RATE
|
||||
if (demuxHelper->previousSample) {
|
||||
double convertedDuration = sample->decodeTimestamp * (double)mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000000000.f;
|
||||
uint64_t sampleDuration = convertedDuration - demuxHelper->currentTime;
|
||||
if (demuxHelper->previousSample) {
|
||||
double convertedDuration = sample->decodeTimestamp * (double)mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000000000.f;
|
||||
uint64_t sampleDuration = convertedDuration - demuxHelper->currentTime;
|
||||
|
||||
// MKV timestamps are a bit random, try to round them
|
||||
// to make the sample table in the mp4 smaller.
|
||||
// MKV timestamps are a bit random, try to round them
|
||||
// to make the sample table in the mp4 smaller.
|
||||
|
||||
// Round ac3
|
||||
if (sampleDuration < 550 && sampleDuration > 480) {
|
||||
sampleDuration = 512;
|
||||
}
|
||||
|
||||
// Round aac
|
||||
if (sampleDuration < 1060 && sampleDuration > 990) {
|
||||
sampleDuration = 1024;
|
||||
}
|
||||
|
||||
// Round ac3
|
||||
if (sampleDuration < 1576 && sampleDuration > 1500) {
|
||||
sampleDuration = 1536;
|
||||
}
|
||||
|
||||
demuxHelper->previousSample->duration = sampleDuration;
|
||||
[self enqueue:demuxHelper->previousSample];
|
||||
|
||||
demuxHelper->currentTime += sampleDuration;
|
||||
} else {
|
||||
demuxHelper->currentTime = sample->decodeTimestamp * (double)mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000000000.f;
|
||||
}
|
||||
|
||||
demuxHelper->previousSample = sample;
|
||||
#else
|
||||
[self enqueue:sample];
|
||||
[sample release];
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (trackInfo->Type == TT_SUB) {
|
||||
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) {
|
||||
if (strcmp(trackInfo->CodecID, "S_VOBSUB") && strcmp(trackInfo->CodecID, "S_HDMV/PGS")) {
|
||||
if (!demuxHelper->ss) {
|
||||
demuxHelper->ss = [[SBSubSerializer alloc] init];
|
||||
if (!strcmp(trackInfo->CodecID, "S_TEXT/ASS") || !strcmp(trackInfo->CodecID, "S_TEXT/SSA")) {
|
||||
[demuxHelper->ss setSSA:YES];
|
||||
}
|
||||
}
|
||||
|
||||
NSString *string = [[NSString alloc] initWithBytes:frame length:FrameSize encoding:NSUTF8StringEncoding];
|
||||
if (!strcmp(trackInfo->CodecID, "S_TEXT/ASS") || !strcmp(trackInfo->CodecID, "S_TEXT/SSA")) {
|
||||
string = StripSSALine(string);
|
||||
}
|
||||
|
||||
if ([string length]) {
|
||||
SBSubLine *sl = [[SBSubLine alloc] initWithLine:string start:StartTime / SCALE_FACTOR end:EndTime / SCALE_FACTOR];
|
||||
[demuxHelper->ss addLine:sl];
|
||||
}
|
||||
demuxHelper->samplesWritten++;
|
||||
free(frame);
|
||||
} else {
|
||||
MP42SampleBuffer *nextSample = [[MP42SampleBuffer alloc] init];
|
||||
|
||||
nextSample->duration = 0;
|
||||
nextSample->offset = 0;
|
||||
nextSample->decodeTimestamp = StartTime;
|
||||
nextSample->data = frame;
|
||||
nextSample->size = FrameSize;
|
||||
nextSample->flags |= MP42SampleBufferFlagIsSync;
|
||||
nextSample->trackId = track.sourceId;
|
||||
|
||||
// PGS are usually stored with just the start time, and blank samples to fill the gaps
|
||||
if (!strcmp(trackInfo->CodecID, "S_HDMV/PGS")) {
|
||||
if (!demuxHelper->previousSample) {
|
||||
demuxHelper->previousSample = [[MP42SampleBuffer alloc] init];
|
||||
demuxHelper->previousSample->duration = StartTime / SCALE_FACTOR;
|
||||
demuxHelper->previousSample->offset = 0;
|
||||
demuxHelper->previousSample->decodeTimestamp = StartTime;
|
||||
demuxHelper->previousSample->flags |= MP42SampleBufferFlagIsSync;
|
||||
demuxHelper->previousSample->trackId = track.sourceId;
|
||||
} else {
|
||||
if (nextSample->decodeTimestamp < demuxHelper->previousSample->decodeTimestamp) {
|
||||
// Out of order samples? swap the next with the previous
|
||||
MP42SampleBuffer *temp = nextSample;
|
||||
nextSample = demuxHelper->previousSample;
|
||||
demuxHelper->previousSample = temp;
|
||||
}
|
||||
|
||||
demuxHelper->previousSample->duration = (nextSample->decodeTimestamp - demuxHelper->previousSample->decodeTimestamp) / SCALE_FACTOR;
|
||||
// Round ac3
|
||||
if (sampleDuration < 550 && sampleDuration > 480) {
|
||||
sampleDuration = 512;
|
||||
}
|
||||
|
||||
// Round aac
|
||||
if (sampleDuration < 1060 && sampleDuration > 990) {
|
||||
sampleDuration = 1024;
|
||||
}
|
||||
|
||||
// Round ac3
|
||||
if (sampleDuration < 1576 && sampleDuration > 1500) {
|
||||
sampleDuration = 1536;
|
||||
}
|
||||
|
||||
demuxHelper->previousSample->duration = sampleDuration;
|
||||
[self enqueue:demuxHelper->previousSample];
|
||||
|
||||
demuxHelper->previousSample = nextSample;
|
||||
demuxHelper->currentTime += sampleDuration;
|
||||
} else {
|
||||
demuxHelper->currentTime = sample->decodeTimestamp * (double)mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000000000.f;
|
||||
}
|
||||
|
||||
demuxHelper->previousSample = sample;
|
||||
#else
|
||||
[self enqueue:sample];
|
||||
[sample release];
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (trackInfo->Type == TT_SUB) {
|
||||
if (readMkvPacket(_ioStream, trackInfo, FilePos, &frame, &FrameSize)) {
|
||||
if (strcmp(trackInfo->CodecID, "S_VOBSUB") && strcmp(trackInfo->CodecID, "S_HDMV/PGS")) {
|
||||
if (!demuxHelper->ss) {
|
||||
demuxHelper->ss = [[SBSubSerializer alloc] init];
|
||||
if (!strcmp(trackInfo->CodecID, "S_TEXT/ASS") || !strcmp(trackInfo->CodecID, "S_TEXT/SSA")) {
|
||||
[demuxHelper->ss setSSA:YES];
|
||||
}
|
||||
}
|
||||
|
||||
NSString *string = [[NSString alloc] initWithBytes:frame length:FrameSize encoding:NSUTF8StringEncoding];
|
||||
if (!strcmp(trackInfo->CodecID, "S_TEXT/ASS") || !strcmp(trackInfo->CodecID, "S_TEXT/SSA")) {
|
||||
string = StripSSALine(string);
|
||||
}
|
||||
|
||||
if ([string length]) {
|
||||
SBSubLine *sl = [[SBSubLine alloc] initWithLine:string start:StartTime / SCALE_FACTOR end:EndTime / SCALE_FACTOR];
|
||||
[demuxHelper->ss addLine:sl];
|
||||
}
|
||||
demuxHelper->samplesWritten++;
|
||||
} else if (!strcmp(trackInfo->CodecID, "S_VOBSUB")) {
|
||||
// VobSub seems to have an end duration, and no blank samples, so create a new one each time to fill the gaps
|
||||
if (StartTime > demuxHelper->currentTime) {
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->duration = (StartTime - demuxHelper->currentTime) / SCALE_FACTOR;
|
||||
sample->size = 2;
|
||||
sample->data = calloc(1, 2);
|
||||
sample->flags |= MP42SampleBufferFlagIsSync;
|
||||
sample->trackId = track.sourceId;
|
||||
|
||||
[self enqueue:sample];
|
||||
}
|
||||
|
||||
nextSample->duration = (EndTime - StartTime) / SCALE_FACTOR;
|
||||
|
||||
[self enqueue:nextSample];
|
||||
free(frame);
|
||||
} else {
|
||||
MP42SampleBuffer *nextSample = [[MP42SampleBuffer alloc] init];
|
||||
|
||||
demuxHelper->currentTime = EndTime;
|
||||
nextSample->duration = 0;
|
||||
nextSample->offset = 0;
|
||||
nextSample->decodeTimestamp = StartTime;
|
||||
nextSample->data = frame;
|
||||
nextSample->size = FrameSize;
|
||||
nextSample->flags |= MP42SampleBufferFlagIsSync;
|
||||
nextSample->trackId = demuxHelper->sourceID;
|
||||
|
||||
// PGS are usually stored with just the start time, and blank samples to fill the gaps
|
||||
if (!strcmp(trackInfo->CodecID, "S_HDMV/PGS")) {
|
||||
if (!demuxHelper->previousSample) {
|
||||
demuxHelper->previousSample = [[MP42SampleBuffer alloc] init];
|
||||
demuxHelper->previousSample->duration = StartTime / SCALE_FACTOR;
|
||||
demuxHelper->previousSample->offset = 0;
|
||||
demuxHelper->previousSample->decodeTimestamp = StartTime;
|
||||
demuxHelper->previousSample->flags |= MP42SampleBufferFlagIsSync;
|
||||
demuxHelper->previousSample->trackId = demuxHelper->sourceID;
|
||||
} else {
|
||||
if (nextSample->decodeTimestamp < demuxHelper->previousSample->decodeTimestamp) {
|
||||
// Out of order samples? swap the next with the previous
|
||||
MP42SampleBuffer *temp = nextSample;
|
||||
nextSample = demuxHelper->previousSample;
|
||||
demuxHelper->previousSample = temp;
|
||||
}
|
||||
|
||||
demuxHelper->previousSample->duration = (nextSample->decodeTimestamp - demuxHelper->previousSample->decodeTimestamp) / SCALE_FACTOR;
|
||||
}
|
||||
|
||||
[self enqueue:demuxHelper->previousSample];
|
||||
|
||||
demuxHelper->previousSample = nextSample;
|
||||
demuxHelper->samplesWritten++;
|
||||
} else if (!strcmp(trackInfo->CodecID, "S_VOBSUB")) {
|
||||
// VobSub seems to have an end duration, and no blank samples, so create a new one each time to fill the gaps
|
||||
if (StartTime > demuxHelper->currentTime) {
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->duration = (StartTime - demuxHelper->currentTime) / SCALE_FACTOR;
|
||||
sample->size = 2;
|
||||
sample->data = calloc(1, 2);
|
||||
sample->flags |= MP42SampleBufferFlagIsSync;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
|
||||
[self enqueue:sample];
|
||||
}
|
||||
|
||||
nextSample->duration = (EndTime - StartTime) / SCALE_FACTOR;
|
||||
|
||||
[self enqueue:nextSample];
|
||||
|
||||
demuxHelper->currentTime = EndTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (trackInfo->Type == TT_VIDEO) {
|
||||
else if (trackInfo->Type == TT_VIDEO) {
|
||||
|
||||
// read frames from file
|
||||
frameSample = [[MatroskaSample alloc] init];
|
||||
frameSample->startTime = StartTime;
|
||||
frameSample->endTime = EndTime;
|
||||
frameSample->filePos = FilePos;
|
||||
frameSample->frameSize = FrameSize;
|
||||
frameSample->frameFlags = FrameFlags;
|
||||
[demuxHelper->queue addObject:frameSample];
|
||||
// read frames from file
|
||||
frameSample = [[MatroskaSample alloc] init];
|
||||
frameSample->startTime = StartTime;
|
||||
frameSample->endTime = EndTime;
|
||||
frameSample->filePos = FilePos;
|
||||
frameSample->frameSize = FrameSize;
|
||||
frameSample->frameFlags = FrameFlags;
|
||||
[demuxHelper->queue addObject:frameSample];
|
||||
|
||||
if ([demuxHelper->queue count] < bufferSize) {
|
||||
continue;
|
||||
} else {
|
||||
currentSample = [demuxHelper->queue objectAtIndex:demuxHelper->buffer];
|
||||
|
||||
// matroska stores only the start and end time, so we need to recreate
|
||||
// the frame duration and the offset from the start time, the end time is useless
|
||||
// duration calculation
|
||||
duration = demuxHelper->queue.lastObject->startTime - currentSample->startTime;
|
||||
|
||||
for (MatroskaSample *sample in demuxHelper->queue) {
|
||||
if (sample != currentSample && (sample->startTime >= currentSample->startTime)) {
|
||||
if ((next_duration = (sample->startTime - currentSample->startTime)) < duration) {
|
||||
duration = next_duration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (duration == 0) {
|
||||
duration = 10000;
|
||||
}
|
||||
|
||||
// offset calculation
|
||||
int64_t offset = currentSample->startTime - demuxHelper->currentTime;
|
||||
|
||||
demuxHelper->currentTime += duration;
|
||||
|
||||
if (readMkvPacket(_ioStream, trackInfo, currentSample->filePos, &frame, ¤tSample->frameSize)) {
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->data = frame;
|
||||
sample->size = currentSample->frameSize;
|
||||
sample->duration = duration / 10000.0f;
|
||||
sample->offset = offset / 10000.0f;
|
||||
sample->decodeTimestamp = StartTime;
|
||||
sample->flags |= (currentSample->frameFlags & FRAME_KF) ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->trackId = track.sourceId;
|
||||
|
||||
demuxHelper->samplesWritten++;
|
||||
|
||||
// save the minimum offset, used later to keep all the offset values positive
|
||||
if (sample->offset < demuxHelper->minDisplayOffset) {
|
||||
demuxHelper->minDisplayOffset = sample->offset;
|
||||
}
|
||||
|
||||
if (demuxHelper->buffer >= bufferSize) {
|
||||
[demuxHelper->queue removeObjectAtIndex:0];
|
||||
}
|
||||
if (demuxHelper->buffer < bufferSize) {
|
||||
demuxHelper->buffer++;
|
||||
}
|
||||
|
||||
[self enqueue:sample];
|
||||
}
|
||||
else {
|
||||
if ([demuxHelper->queue count] < bufferSize) {
|
||||
continue;
|
||||
} else {
|
||||
currentSample = [demuxHelper->queue objectAtIndex:demuxHelper->buffer];
|
||||
|
||||
// matroska stores only the start and end time, so we need to recreate
|
||||
// the frame duration and the offset from the start time, the end time is useless
|
||||
// duration calculation
|
||||
duration = demuxHelper->queue.lastObject->startTime - currentSample->startTime;
|
||||
|
||||
for (MatroskaSample *sample in demuxHelper->queue) {
|
||||
if (sample != currentSample && (sample->startTime >= currentSample->startTime)) {
|
||||
if ((next_duration = (sample->startTime - currentSample->startTime)) < duration) {
|
||||
duration = next_duration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (duration == 0) {
|
||||
duration = 10000;
|
||||
}
|
||||
|
||||
// offset calculation
|
||||
int64_t offset = currentSample->startTime - demuxHelper->currentTime;
|
||||
|
||||
demuxHelper->currentTime += duration;
|
||||
|
||||
if (readMkvPacket(_ioStream, trackInfo, currentSample->filePos, &frame, ¤tSample->frameSize)) {
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->data = frame;
|
||||
sample->size = currentSample->frameSize;
|
||||
sample->duration = duration / 10000.0f;
|
||||
sample->offset = offset / 10000.0f;
|
||||
sample->decodeTimestamp = StartTime;
|
||||
sample->flags |= (currentSample->frameFlags & FRAME_KF) ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->trackId = demuxHelper->sourceID;
|
||||
|
||||
demuxHelper->samplesWritten++;
|
||||
|
||||
// save the minimum offset, used later to keep all the offset values positive
|
||||
if (sample->offset < demuxHelper->minDisplayOffset) {
|
||||
demuxHelper->minDisplayOffset = sample->offset;
|
||||
}
|
||||
|
||||
if (demuxHelper->buffer >= bufferSize) {
|
||||
[demuxHelper->queue removeObjectAtIndex:0];
|
||||
}
|
||||
if (demuxHelper->buffer < bufferSize) {
|
||||
demuxHelper->buffer++;
|
||||
}
|
||||
|
||||
[self enqueue:sample];
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (MP42Track *track in inputTracks) {
|
||||
demuxHelper = track.demuxerHelper;
|
||||
for (MP42Track *track in inputTracks) {
|
||||
MatroskaDemuxHelper *demuxHelper = track.demuxerHelper;
|
||||
|
||||
if (demuxHelper->queue) {
|
||||
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, [track sourceId]);
|
||||
if (demuxHelper->queue) {
|
||||
TrackInfo *trackInfo = mkv_GetTrackInfo(_matroskaFile, [track sourceId]);
|
||||
|
||||
while ([demuxHelper->queue count]) {
|
||||
if (demuxHelper->bufferFlush == 1) {
|
||||
// add a last sample to get the duration for the last frame
|
||||
MatroskaSample *lastSample = [demuxHelper->queue lastObject];
|
||||
for (MatroskaSample *sample in demuxHelper->queue) {
|
||||
if (sample->startTime > lastSample->startTime)
|
||||
lastSample = sample;
|
||||
while ([demuxHelper->queue count]) {
|
||||
if (demuxHelper->bufferFlush == 1) {
|
||||
// add a last sample to get the duration for the last frame
|
||||
MatroskaSample *lastSample = [demuxHelper->queue lastObject];
|
||||
for (MatroskaSample *sample in demuxHelper->queue) {
|
||||
if (sample->startTime > lastSample->startTime)
|
||||
lastSample = sample;
|
||||
}
|
||||
frameSample = [[MatroskaSample alloc] init];
|
||||
frameSample->startTime = lastSample->endTime;
|
||||
[demuxHelper->queue addObject:frameSample];
|
||||
}
|
||||
frameSample = [[MatroskaSample alloc] init];
|
||||
frameSample->startTime = lastSample->endTime;
|
||||
[demuxHelper->queue addObject:frameSample];
|
||||
}
|
||||
currentSample = [demuxHelper->queue objectAtIndex:demuxHelper->buffer];
|
||||
currentSample = [demuxHelper->queue objectAtIndex:demuxHelper->buffer];
|
||||
|
||||
// matroska stores only the start and end time, so we need to recreate
|
||||
// the frame duration and the offset from the start time, the end time is useless
|
||||
// duration calculation
|
||||
duration = demuxHelper->queue.lastObject->startTime - currentSample->startTime;
|
||||
// matroska stores only the start and end time, so we need to recreate
|
||||
// the frame duration and the offset from the start time, the end time is useless
|
||||
// duration calculation
|
||||
duration = demuxHelper->queue.lastObject->startTime - currentSample->startTime;
|
||||
|
||||
for (MatroskaSample *sample in demuxHelper->queue) {
|
||||
if (sample != currentSample && (sample->startTime >= currentSample->startTime)) {
|
||||
if ((next_duration = (sample->startTime - currentSample->startTime)) < duration) {
|
||||
duration = next_duration;
|
||||
for (MatroskaSample *sample in demuxHelper->queue) {
|
||||
if (sample != currentSample && (sample->startTime >= currentSample->startTime)) {
|
||||
if ((next_duration = (sample->startTime - currentSample->startTime)) < duration) {
|
||||
duration = next_duration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// offset calculation
|
||||
int64_t offset = currentSample->startTime - demuxHelper->currentTime;
|
||||
|
||||
demuxHelper->currentTime += duration;
|
||||
|
||||
if (readMkvPacket(_ioStream, trackInfo, currentSample->filePos, &frame, ¤tSample->frameSize)) {
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->data = frame;
|
||||
sample->size = currentSample->frameSize;
|
||||
sample->duration = duration / 10000.0f;
|
||||
sample->offset = offset / 10000.0f;
|
||||
sample->decodeTimestamp = StartTime;
|
||||
sample->flags |= (currentSample->frameFlags & FRAME_KF) ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->trackId = track.sourceId;
|
||||
|
||||
demuxHelper->samplesWritten++;
|
||||
|
||||
// save the minimum offset, used later to keep the all the offset values positive
|
||||
if (sample->offset < demuxHelper->minDisplayOffset) {
|
||||
demuxHelper->minDisplayOffset = sample->offset;
|
||||
}
|
||||
|
||||
if (demuxHelper->buffer >= bufferSize) {
|
||||
[demuxHelper->queue removeObjectAtIndex:0];
|
||||
}
|
||||
|
||||
[self enqueue:sample];
|
||||
|
||||
demuxHelper->bufferFlush++;
|
||||
if (demuxHelper->bufferFlush >= bufferSize - 1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// offset calculation
|
||||
int64_t offset = currentSample->startTime - demuxHelper->currentTime;
|
||||
if (demuxHelper->ss) {
|
||||
MP42SampleBuffer *sample = nil;
|
||||
MP4TrackId dstTrackId = track.sourceId;
|
||||
SBSubSerializer *ss = demuxHelper->ss;
|
||||
|
||||
demuxHelper->currentTime += duration;
|
||||
[ss setFinished:YES];
|
||||
|
||||
if (readMkvPacket(_ioStream, trackInfo, currentSample->filePos, &frame, ¤tSample->frameSize)) {
|
||||
MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init];
|
||||
sample->data = frame;
|
||||
sample->size = currentSample->frameSize;
|
||||
sample->duration = duration / 10000.0f;
|
||||
sample->offset = offset / 10000.0f;
|
||||
sample->decodeTimestamp = StartTime;
|
||||
sample->flags |= (currentSample->frameFlags & FRAME_KF) ? MP42SampleBufferFlagIsSync : 0;
|
||||
sample->trackId = track.sourceId;
|
||||
while (![ss isEmpty] && !self.isCancelled) {
|
||||
SBSubLine *sl = [ss getSerializedPacket];
|
||||
|
||||
demuxHelper->samplesWritten++;
|
||||
|
||||
// save the minimum offset, used later to keep the all the offset values positive
|
||||
if (sample->offset < demuxHelper->minDisplayOffset) {
|
||||
demuxHelper->minDisplayOffset = sample->offset;
|
||||
if ([sl->line isEqualToString:@"\n"]) {
|
||||
sample = copyEmptySubtitleSample(dstTrackId, sl->end_time - sl->begin_time, NO);
|
||||
}
|
||||
else {
|
||||
sample = copySubtitleSample(dstTrackId, sl->line, sl->end_time - sl->begin_time, NO, NO, YES, CGSizeMake(0, 0), 0);
|
||||
}
|
||||
|
||||
if (demuxHelper->buffer >= bufferSize) {
|
||||
[demuxHelper->queue removeObjectAtIndex:0];
|
||||
}
|
||||
|
||||
[self enqueue:sample];
|
||||
|
||||
demuxHelper->bufferFlush++;
|
||||
if (demuxHelper->bufferFlush >= bufferSize - 1) {
|
||||
if (!sample) {
|
||||
break;
|
||||
}
|
||||
|
||||
demuxHelper->currentTime += sample->duration;
|
||||
sample->decodeTimestamp = demuxHelper->currentTime;
|
||||
|
||||
[self enqueue:sample];
|
||||
|
||||
demuxHelper->samplesWritten++;
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (demuxHelper->previousSample) {
|
||||
if (track.mediaType == kMP42MediaType_Subtitle) {
|
||||
demuxHelper->previousSample->duration = 100;
|
||||
}
|
||||
|
||||
[self enqueue:demuxHelper->previousSample];
|
||||
demuxHelper->previousSample = nil;
|
||||
}
|
||||
}
|
||||
|
||||
if (demuxHelper->ss) {
|
||||
MP42SampleBuffer *sample = nil;
|
||||
MP4TrackId dstTrackId = track.sourceId;
|
||||
SBSubSerializer *ss = demuxHelper->ss;
|
||||
|
||||
[ss setFinished:YES];
|
||||
|
||||
while (![ss isEmpty] && !self.isCancelled) {
|
||||
SBSubLine *sl = [ss getSerializedPacket];
|
||||
|
||||
if ([sl->line isEqualToString:@"\n"]) {
|
||||
sample = copyEmptySubtitleSample(dstTrackId, sl->end_time - sl->begin_time, NO);
|
||||
}
|
||||
else {
|
||||
sample = copySubtitleSample(dstTrackId, sl->line, sl->end_time - sl->begin_time, NO, NO, YES, CGSizeMake(0, 0), 0);
|
||||
}
|
||||
|
||||
if (!sample) {
|
||||
break;
|
||||
}
|
||||
|
||||
demuxHelper->currentTime += sample->duration;
|
||||
sample->decodeTimestamp = demuxHelper->currentTime;
|
||||
|
||||
[self enqueue:sample];
|
||||
|
||||
demuxHelper->samplesWritten++;
|
||||
}
|
||||
}
|
||||
|
||||
if (demuxHelper->previousSample) {
|
||||
if (track.mediaType == kMP42MediaType_Subtitle) {
|
||||
demuxHelper->previousSample->duration = 100;
|
||||
}
|
||||
|
||||
[self enqueue:demuxHelper->previousSample];
|
||||
demuxHelper->previousSample = nil;
|
||||
}
|
||||
}
|
||||
|
||||
[self setDone];
|
||||
[self setDone];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -271,27 +271,26 @@ typedef struct MP4DemuxHelper {
|
||||
- (void)demux
|
||||
{
|
||||
@autoreleasepool {
|
||||
NSArray<MP42Track *> *inputTracks = self.inputTracks;
|
||||
|
||||
NSInteger tracksNumber = inputTracks.count;
|
||||
NSInteger tracksDone = 0;
|
||||
|
||||
if (!_fileHandle) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSArray<MP42Track *> *inputTracks = self.inputTracks;
|
||||
NSInteger tracksNumber = inputTracks.count;
|
||||
NSInteger tracksDone = 0;
|
||||
|
||||
MP4DemuxHelper * helpers[tracksNumber];
|
||||
|
||||
NSUInteger index = 0;
|
||||
for (MP42Track *track in inputTracks) {
|
||||
MP4DemuxHelper *demuxHelper = malloc(sizeof(MP4DemuxHelper));
|
||||
for (NSUInteger index = 0; index < tracksNumber; index += 1) {
|
||||
MP42Track *track = inputTracks[index];
|
||||
MP4DemuxHelper *demuxHelper = calloc(1, sizeof(MP4DemuxHelper));
|
||||
demuxHelper->sourceID = track.sourceId;
|
||||
demuxHelper->totalSampleNumber = MP4GetTrackNumberOfSamples(_fileHandle, track.sourceId);
|
||||
demuxHelper->timeScale = MP4GetTrackTimeScale(_fileHandle, track.sourceId);
|
||||
demuxHelper->done = 0;
|
||||
|
||||
helpers[index] = demuxHelper;
|
||||
index += 1;
|
||||
}
|
||||
|
||||
MP4Timestamp currentTime = 1;
|
||||
|
||||
+49
-57
@@ -19,7 +19,6 @@
|
||||
#import "mp4v2.h"
|
||||
#import "MP42FormatUtilites.h"
|
||||
#import "MP42PrivateUtilities.h"
|
||||
#import "MP42Track+Muxer.h"
|
||||
#import "MP42Track+Private.h"
|
||||
|
||||
@implementation MP42Muxer
|
||||
@@ -30,14 +29,14 @@
|
||||
id <MP42MuxerDelegate> _delegate;
|
||||
id <MP42Logging> _logger;
|
||||
|
||||
NSMutableArray<MP42Track *> *_workingTracks;
|
||||
NSMutableArray<MP42Track *> *_activeTracks;
|
||||
int32_t _cancelled;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_workingTracks = [[NSMutableArray alloc] init];
|
||||
_activeTracks = [[NSMutableArray alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -48,19 +47,12 @@
|
||||
NSParameterAssert(fileHandle);
|
||||
_fileHandle = fileHandle;
|
||||
_delegate = del;
|
||||
_logger = [logger retain];
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[_logger release];
|
||||
[_workingTracks release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (BOOL)canAddTrack:(MP42Track *)track
|
||||
{
|
||||
if (isTrackMuxable(track.targetFormat)) {
|
||||
@@ -76,7 +68,7 @@
|
||||
- (void)addTrack:(MP42Track *)track
|
||||
{
|
||||
if (![track isMemberOfClass:[MP42ChapterTrack class]]) {
|
||||
[_workingTracks addObject:track];
|
||||
[_activeTracks addObject:track];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,17 +76,17 @@
|
||||
{
|
||||
NSMutableArray<MP42Track *> *unsupportedTracks = [[NSMutableArray alloc] init];;
|
||||
|
||||
for (MP42Track *track in _workingTracks) {
|
||||
for (MP42Track *track in _activeTracks) {
|
||||
|
||||
MP42FileImporter *importer = track.importer;
|
||||
FourCharCode format = track.format;
|
||||
MP4TrackId dstTrackId = 0;
|
||||
NSData *magicCookie = nil;
|
||||
NSInteger timeScale = 0;
|
||||
muxer_helper *helper = track.muxer_helper;
|
||||
|
||||
if (helper) {
|
||||
magicCookie = [helper->importer magicCookieForTrack:track];
|
||||
timeScale = [helper->importer timescaleForTrack:track];
|
||||
if (importer) {
|
||||
magicCookie = [importer magicCookieForTrack:track];
|
||||
timeScale = [importer timescaleForTrack:track];
|
||||
}
|
||||
else {
|
||||
[unsupportedTracks addObject:track];
|
||||
@@ -115,7 +107,7 @@
|
||||
continue;
|
||||
}
|
||||
|
||||
helper->converter = audioConverter;
|
||||
track.converter = audioConverter;
|
||||
// The audio converter might downsample the audio,
|
||||
// so update the track timescale here.
|
||||
timeScale = audioConverter.sampleRate;
|
||||
@@ -134,7 +126,7 @@
|
||||
continue;
|
||||
}
|
||||
|
||||
helper->converter = subConverter;
|
||||
track.converter = subConverter;
|
||||
format = track.conversionSettings.format;
|
||||
} else if ([track isMemberOfClass:[MP42SubtitleTrack class]] && track.conversionSettings) {
|
||||
format = track.conversionSettings.format;
|
||||
@@ -148,7 +140,7 @@
|
||||
continue;
|
||||
}
|
||||
|
||||
NSSize size = [helper->importer sizeForTrack:track];
|
||||
NSSize size = [importer sizeForTrack:track];
|
||||
|
||||
uint8_t *avcCAtom = (uint8_t *)magicCookie.bytes;
|
||||
dstTrackId = MP4AddH264VideoTrack(_fileHandle, timeScale,
|
||||
@@ -193,7 +185,7 @@
|
||||
|
||||
MP4SetVideoProfileLevel(_fileHandle, 0x15);
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// H.265 video track
|
||||
@@ -209,7 +201,7 @@
|
||||
// Check whether we can use hvc1 or hev1 fourcc.
|
||||
bool completeness = 0;
|
||||
if (magicCookie.length && !analyze_HEVC(magicCookie.bytes, magicCookie.length, &completeness)) {
|
||||
NSSize size = [helper->importer sizeForTrack:track];
|
||||
NSSize size = [importer sizeForTrack:track];
|
||||
|
||||
dstTrackId = MP4AddH265VideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, size.width, size.height, completeness);
|
||||
|
||||
@@ -220,7 +212,7 @@
|
||||
else {
|
||||
MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.hev1.hvcC.content", magicCookie.bytes, magicCookie.length);
|
||||
}
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
else {
|
||||
[unsupportedTracks addObject:track];
|
||||
@@ -248,7 +240,7 @@
|
||||
magicCookie.length);
|
||||
}
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// Photo-JPEG video track
|
||||
@@ -257,7 +249,7 @@
|
||||
dstTrackId = MP4AddJpegVideoTrack(_fileHandle, timeScale,
|
||||
MP4_INVALID_DURATION, [(MP42VideoTrack*)track width], [(MP42VideoTrack*)track height]);
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// AAC audio track
|
||||
@@ -274,9 +266,9 @@
|
||||
magicCookie.length);
|
||||
}
|
||||
|
||||
MP4SetTrackWantsRoll(_fileHandle, dstTrackId, [helper->importer audioTrackUsesExplicitEncoderDelay:track]);
|
||||
MP4SetTrackWantsRoll(_fileHandle, dstTrackId, [importer audioTrackUsesExplicitEncoderDelay:track]);
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// AC-3 audio track
|
||||
@@ -305,14 +297,14 @@
|
||||
ac3Info[5]);
|
||||
}
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// EAC-3 audio tack
|
||||
else if ([track isMemberOfClass:[MP42AudioTrack class]] && format == kMP42AudioCodecType_EnhancedAC3) {
|
||||
dstTrackId = MP4AddEAC3AudioTrack(_fileHandle, timeScale, magicCookie.bytes, magicCookie.length);
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// ALAC audio track
|
||||
@@ -323,7 +315,7 @@
|
||||
MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.alac.alac.AppleLosslessMagicCookie", magicCookie.bytes, magicCookie.length);
|
||||
}
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// DTS audio track
|
||||
@@ -332,7 +324,7 @@
|
||||
timeScale,
|
||||
512, 0xA9);
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// 3GPP text track
|
||||
@@ -343,7 +335,7 @@
|
||||
MP42SubtitleTrack *subTrack = (MP42SubtitleTrack *)track;
|
||||
NSInteger vPlacement = subTrack.verticalPlacement;
|
||||
|
||||
for (id workingTrack in _workingTracks) {
|
||||
for (id workingTrack in _activeTracks) {
|
||||
if ([workingTrack isMemberOfClass:[MP42VideoTrack class]]) {
|
||||
videoSize.width = [workingTrack trackWidth];
|
||||
videoSize.height = [workingTrack trackHeight];
|
||||
@@ -403,7 +395,7 @@
|
||||
subTrack.trackWidth = videoSize.width;
|
||||
subTrack.trackHeight = subSize.height;
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// VobSub bitmap track
|
||||
@@ -430,14 +422,14 @@
|
||||
MP4SetTrackESConfiguration(_fileHandle, dstTrackId,
|
||||
(uint8_t *)palette, 16 * 4 );
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// WebVTT
|
||||
else if ([track isMemberOfClass:[MP42SubtitleTrack class]] && format ==kMP42SubtitleCodecType_WebVTT) {
|
||||
NSSize videoSize = [helper->importer sizeForTrack:track];
|
||||
NSSize videoSize = [importer sizeForTrack:track];
|
||||
|
||||
for (id workingTrack in _workingTracks)
|
||||
for (id workingTrack in _activeTracks)
|
||||
if ([workingTrack isMemberOfClass:[MP42VideoTrack class]]) {
|
||||
videoSize.width = [workingTrack trackWidth];
|
||||
videoSize.height = [workingTrack trackHeight];
|
||||
@@ -458,14 +450,14 @@
|
||||
|
||||
dstTrackId = MP4AddWebVTTTrack(_fileHandle, timeScale, videoSize.width, videoSize.height, magicCookie.bytes, magicCookie.length);
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
}
|
||||
|
||||
// Closed Caption text track
|
||||
else if ([track isMemberOfClass:[MP42ClosedCaptionTrack class]]) {
|
||||
NSSize videoSize = [helper->importer sizeForTrack:track];
|
||||
NSSize videoSize = [importer sizeForTrack:track];
|
||||
|
||||
for (id workingTrack in _workingTracks)
|
||||
for (id workingTrack in _activeTracks)
|
||||
if ([workingTrack isMemberOfClass:[MP42VideoTrack class]]) {
|
||||
videoSize.width = [workingTrack trackWidth];
|
||||
videoSize.height = [workingTrack trackHeight];
|
||||
@@ -486,7 +478,7 @@
|
||||
|
||||
dstTrackId = MP4AddCCTrack(_fileHandle, timeScale, videoSize.width, videoSize.height);
|
||||
|
||||
[helper->importer setActiveTrack:track];
|
||||
[importer setActiveTrack:track];
|
||||
} else {
|
||||
[unsupportedTracks addObject:track];
|
||||
continue;
|
||||
@@ -498,15 +490,14 @@
|
||||
}
|
||||
}
|
||||
|
||||
[_workingTracks removeObjectsInArray:unsupportedTracks];
|
||||
[unsupportedTracks release];
|
||||
[_activeTracks removeObjectsInArray:unsupportedTracks];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void)work
|
||||
{
|
||||
if (!_workingTracks.count) {
|
||||
if (!_activeTracks.count) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -519,7 +510,8 @@
|
||||
}
|
||||
|
||||
NSUInteger tracksImportersCount = trackImportersArray.count;
|
||||
NSUInteger tracksCount = _workingTracks.count;
|
||||
NSUInteger tracksCount = _activeTracks.count;
|
||||
NSMutableArray<MP42Track *> *tracks = [_activeTracks copy];
|
||||
|
||||
for (;;) {
|
||||
@autoreleasepool {
|
||||
@@ -527,15 +519,18 @@
|
||||
usleep(1000);
|
||||
|
||||
// Iterate the tracks array and mux the samples
|
||||
for (MP42Track *track in _workingTracks) {
|
||||
for (MP42Track *track in tracks) {
|
||||
MP42SampleBuffer *sampleBuffer = nil;
|
||||
MP42TrackId trackId = track.trackId;
|
||||
|
||||
for (int i = 0; i < 100 && (sampleBuffer = [track copyNextSample]) != nil; i++) {
|
||||
|
||||
// Check if the track helper is done.
|
||||
if (sampleBuffer->flags & MP42SampleBufferFlagEndOfFile) {
|
||||
track.muxer_helper->done = 1;
|
||||
// Tracks done, remove it from the loop
|
||||
NSMutableArray<MP42Track *> *copy = [tracks mutableCopy];
|
||||
[copy removeObject:track];
|
||||
tracks = copy;
|
||||
done += 1;
|
||||
}
|
||||
else if (!MP4WriteSample(_fileHandle, trackId,
|
||||
sampleBuffer->data, sampleBuffer->size,
|
||||
@@ -543,10 +538,7 @@
|
||||
sampleBuffer->flags & MP42SampleBufferFlagIsSync)) {
|
||||
_cancelled = YES;
|
||||
}
|
||||
|
||||
[sampleBuffer release];
|
||||
}
|
||||
done += (track.muxer_helper->done ? 1 : 0);
|
||||
}
|
||||
|
||||
if (_cancelled) {
|
||||
@@ -576,9 +568,9 @@
|
||||
}
|
||||
|
||||
// Write the converted audio track magic cookie
|
||||
for (MP42Track *track in _workingTracks) {
|
||||
if (track.muxer_helper->converter && track.conversionSettings && [track isMemberOfClass:[MP42AudioTrack class]]) {
|
||||
NSData *magicCookie = [track.muxer_helper->converter magicCookie];
|
||||
for (MP42Track *track in _activeTracks) {
|
||||
if (track.converter && track.conversionSettings && [track isMemberOfClass:[MP42AudioTrack class]]) {
|
||||
NSData *magicCookie = track.converter.magicCookie;
|
||||
|
||||
if (magicCookie) {
|
||||
if (track.conversionSettings.format == kAudioFormatMPEG4AAC) {
|
||||
@@ -622,12 +614,12 @@
|
||||
{
|
||||
NSMutableArray<MP42FileImporter *> *trackImportersArray = [NSMutableArray array];
|
||||
|
||||
for (MP42Track *track in _workingTracks) {
|
||||
if (![trackImportersArray containsObject:track.muxer_helper->importer]) {
|
||||
[trackImportersArray addObject:track.muxer_helper->importer];
|
||||
for (MP42Track *track in _activeTracks) {
|
||||
if (![trackImportersArray containsObject:track.importer]) {
|
||||
[trackImportersArray addObject:track.importer];
|
||||
}
|
||||
}
|
||||
return [[trackImportersArray copy] autorelease];
|
||||
return [trackImportersArray copy];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -21,16 +21,12 @@ typedef struct muxer_helper {
|
||||
// Output helpers
|
||||
id <MP42ConverterProtocol> converter;
|
||||
MP42Fifo<MP42SampleBuffer *> *fifo;
|
||||
|
||||
BOOL done;
|
||||
} muxer_helper;
|
||||
|
||||
@interface MP42Track (MP42TrackMuxerExtentions)
|
||||
|
||||
@property (nonatomic, readonly, nullable) muxer_helper *muxer_helper;
|
||||
|
||||
- (MP42SampleBuffer *)copyNextSample;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -34,6 +34,16 @@
|
||||
self.muxer_helper->importer = importer;
|
||||
}
|
||||
|
||||
- (id <MP42ConverterProtocol>)converter
|
||||
{
|
||||
return self.muxer_helper->converter;
|
||||
}
|
||||
|
||||
- (void)setConverter:(id <MP42ConverterProtocol>)converter
|
||||
{
|
||||
self.muxer_helper->converter = converter;
|
||||
}
|
||||
|
||||
- (void *)copy_muxer_helper
|
||||
{
|
||||
muxer_helper *copy = calloc(1, sizeof(muxer_helper));
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class MP42FileImporter;
|
||||
@class MP42SampleBuffer;
|
||||
@protocol MP42ConverterProtocol;
|
||||
|
||||
@interface MP42Track (Private)
|
||||
|
||||
@@ -34,8 +36,12 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
@property(nonatomic, readonly) NSMutableDictionary<NSString *, NSNumber *> *updatedProperty;
|
||||
|
||||
@property (nonatomic, readwrite) MP42FileImporter *importer;
|
||||
|
||||
@property (nonatomic, readwrite) id<MP42ConverterProtocol> converter;
|
||||
@property (nonatomic, readwrite) id demuxerHelper;
|
||||
|
||||
- (MP42SampleBuffer *)copyNextSample;
|
||||
|
||||
- (void *)copy_muxer_helper;
|
||||
- (void *)create_muxer_helper;
|
||||
|
||||
|
||||
@@ -164,7 +164,7 @@
|
||||
A9B9C27D1823923200416A4E /* MP42FileImporter.h in Headers */ = {isa = PBXBuildFile; fileRef = A9B9C2321823923100416A4E /* MP42FileImporter.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
A9B9C27E1823923200416A4E /* MP42FileImporter.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2331823923100416A4E /* MP42FileImporter.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
|
||||
A9B9C27F1823923200416A4E /* MP42H264Importer.h in Headers */ = {isa = PBXBuildFile; fileRef = A9B9C2341823923100416A4E /* MP42H264Importer.h */; };
|
||||
A9B9C2801823923200416A4E /* MP42H264Importer.mm in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2351823923100416A4E /* MP42H264Importer.mm */; settings = {COMPILER_FLAGS = "-w -Xanalyzer -analyzer-disable-all-checks -fno-objc-arc"; }; };
|
||||
A9B9C2801823923200416A4E /* MP42H264Importer.mm in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2351823923100416A4E /* MP42H264Importer.mm */; settings = {COMPILER_FLAGS = "-w -Xanalyzer -analyzer-disable-all-checks"; }; };
|
||||
A9B9C2811823923200416A4E /* MP42HtmlParser.h in Headers */ = {isa = PBXBuildFile; fileRef = A9B9C2361823923100416A4E /* MP42HtmlParser.h */; };
|
||||
A9B9C2821823923200416A4E /* MP42HtmlParser.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2371823923100416A4E /* MP42HtmlParser.m */; };
|
||||
A9B9C2831823923200416A4E /* MP42Image.h in Headers */ = {isa = PBXBuildFile; fileRef = A9B9C2381823923100416A4E /* MP42Image.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
@@ -178,7 +178,7 @@
|
||||
A9B9C28B1823923200416A4E /* MP42Mp4Importer.h in Headers */ = {isa = PBXBuildFile; fileRef = A9B9C2401823923100416A4E /* MP42Mp4Importer.h */; };
|
||||
A9B9C28C1823923200416A4E /* MP42Mp4Importer.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2411823923100416A4E /* MP42Mp4Importer.m */; };
|
||||
A9B9C28D1823923200416A4E /* MP42Muxer.h in Headers */ = {isa = PBXBuildFile; fileRef = A9B9C2421823923100416A4E /* MP42Muxer.h */; };
|
||||
A9B9C28E1823923200416A4E /* MP42Muxer.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2431823923100416A4E /* MP42Muxer.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
|
||||
A9B9C28E1823923200416A4E /* MP42Muxer.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2431823923100416A4E /* MP42Muxer.m */; };
|
||||
A9B9C28F1823923200416A4E /* MP42OCRWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = A9B9C2441823923100416A4E /* MP42OCRWrapper.h */; };
|
||||
A9B9C2901823923200416A4E /* MP42OCRWrapper.mm in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2451823923100416A4E /* MP42OCRWrapper.mm */; };
|
||||
A9B9C2941823923200416A4E /* MP42Sample.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B9C2491823923100416A4E /* MP42Sample.m */; };
|
||||
|
||||
Reference in new issue
Block a user