Port more classes to arc.

This commit is contained in:
Damiano Galassi committed 2017-09-29 18:10:59 +02:00
1 parent ea5fc13f87
commit 646585be6f
10 files changed
+626 -641

No files matched your search

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