diff --git a/MP42/MP42AudioConverter.h b/MP42/MP42AudioConverter.h index 66c811d..2a5b058 100644 --- a/MP42/MP42AudioConverter.h +++ b/MP42/MP42AudioConverter.h @@ -85,9 +85,6 @@ static struct AudioFileIO - (NSData *)magicCookie; - (void)cancel; -- (BOOL)encoderDone; - -- (void)setInputDone; NS_ASSUME_NONNULL_END diff --git a/MP42/MP42AudioConverter.m b/MP42/MP42AudioConverter.m index c3ca182..c92c9f7 100644 --- a/MP42/MP42AudioConverter.m +++ b/MP42/MP42AudioConverter.m @@ -255,6 +255,7 @@ OSStatus EncoderDataProc(AudioConverterRef inAudioConverter, AudioConverterDispose(converterEnc); + [self enqueueEndOfFileSample]; encoderDone = YES; } @@ -666,7 +667,12 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter, } - (void)addSample:(MP42SampleBuffer *)sample { - [_inputSamplesBuffer enqueue:sample]; + if (sample->flags & MP42SampleBufferFlagEndOfFile) { + [self setInputDone]; + } + else { + [_inputSamplesBuffer enqueue:sample]; + } } - (MP42SampleBuffer *)copyEncodedSample { @@ -678,6 +684,17 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter, encoderData.fileReaderDone = YES; } +/** + * Sends the EOF flag down the muxer chain. + */ +- (void)enqueueEndOfFileSample +{ + MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; + sample->flags |= MP42SampleBufferFlagEndOfFile; + [_outputSamplesBuffer enqueue:sample]; + [sample release]; +} + - (void)cancel { OSAtomicIncrement32(&_cancelled); @@ -692,10 +709,6 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter, } } -- (BOOL)encoderDone { - return encoderDone && [_outputSamplesBuffer isEmpty]; -} - - (NSData *)magicCookie { return [[outputMagicCookie retain] autorelease]; } diff --git a/MP42/MP42Fifo.h b/MP42/MP42Fifo.h index dd4da2e..5449d28 100644 --- a/MP42/MP42Fifo.h +++ b/MP42/MP42Fifo.h @@ -10,21 +10,7 @@ NS_ASSUME_NONNULL_BEGIN -@interface MP42Fifo<__covariant ObjectType> : NSObject { -@private - id *_array; - - int32_t _head; - int32_t _tail; - - int32_t _count; - int32_t _size; - - int32_t _cancelled; - - dispatch_semaphore_t _full; - dispatch_semaphore_t _empty; -} +@interface MP42Fifo<__covariant ObjectType> : NSObject - (instancetype)initWithCapacity:(NSUInteger)capacity NS_DESIGNATED_INITIALIZER; diff --git a/MP42/MP42Fifo.m b/MP42/MP42Fifo.m index 0bcb43e..d1a8729 100644 --- a/MP42/MP42Fifo.m +++ b/MP42/MP42Fifo.m @@ -8,7 +8,21 @@ #import "MP42Fifo.h" -@implementation MP42Fifo +@implementation MP42Fifo { +@private + id *_array; + + int32_t _head; + int32_t _tail; + + int32_t _count; + int32_t _size; + + int32_t _cancelled; + + dispatch_semaphore_t _full; + dispatch_semaphore_t _empty; +} - (instancetype)init { self = [self initWithCapacity:300]; diff --git a/MP42/MP42File.m b/MP42/MP42File.m index 61fd424..de8389c 100644 --- a/MP42/MP42File.m +++ b/MP42/MP42File.m @@ -578,6 +578,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { NSError *error = nil; NSURL *tempURL = [self tempURL]; NSFileManager *fileManager = [[NSFileManager alloc] init]; + dispatch_semaphore_t sem = dispatch_semaphore_create(0); if (tempURL) { unsigned long long originalFileSize = [[[fileManager attributesOfItemAtPath:self.URL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue]; @@ -585,6 +586,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { dispatch_async(dispatch_get_global_queue(0, 0), ^{ noErr = MP4Optimize(self.URL.path.fileSystemRepresentation, tempURL.path.fileSystemRepresentation); OSAtomicIncrement32Barrier(&done); + dispatch_semaphore_signal(sem); }); // Loop to check the progress @@ -594,6 +596,8 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { usleep(450000); } + dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); + // Additional check to see if we can open the optimized file if (noErr && [[[MP42File alloc] initWithURL:tempURL error:NULL] autorelease]) { // Replace the original file @@ -616,6 +620,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { } } + dispatch_release(sem); [fileManager release]; } diff --git a/MP42/MP42FileImporter+Private.h b/MP42/MP42FileImporter+Private.h index c1c2c86..c243f02 100644 --- a/MP42/MP42FileImporter+Private.h +++ b/MP42/MP42FileImporter+Private.h @@ -33,8 +33,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)enqueue:(MP42SampleBuffer *)sample; - (double)progress; - -- (BOOL)done; - (void)setDone; @end diff --git a/MP42/MP42FileImporter.h b/MP42/MP42FileImporter.h index 0173128..702e648 100644 --- a/MP42/MP42FileImporter.h +++ b/MP42/MP42FileImporter.h @@ -32,7 +32,6 @@ NS_ASSUME_NONNULL_BEGIN NSThread *_demuxerThread; - int32_t _done; dispatch_semaphore_t _doneSem; } diff --git a/MP42/MP42FileImporter.m b/MP42/MP42FileImporter.m index 419d677..9741781 100644 --- a/MP42/MP42FileImporter.m +++ b/MP42/MP42FileImporter.m @@ -231,7 +231,7 @@ static NSArray *_supportedFileFormats; _doneSem = dispatch_semaphore_create(0); - if (!_demuxerThread && !_done) { + if (!_demuxerThread) { _demuxerThread = [[NSThread alloc] initWithTarget:self selector:@selector(demux) object:nil]; _demuxerThread.name = self.description; @@ -274,14 +274,27 @@ static NSArray *_supportedFileFormats; } } -- (BOOL)done +/** + * Sends the EOF flag down the muxer chain. + */ +- (void)enqueueEndOfFileSamples { - return (_done > 0); + for (MP42Track *track in _outputsTracks) { + MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; + sample->flags |= MP42SampleBufferFlagEndOfFile; + + if (track.muxer_helper->converter) { + [track.muxer_helper->converter addSample:sample]; + } else { + [track.muxer_helper->fifo enqueue:sample]; + } + [sample release]; + } } - (void)setDone { - OSAtomicIncrement32(&_done); + [self enqueueEndOfFileSamples]; dispatch_semaphore_signal(_doneSem); } diff --git a/MP42/MP42Heap.h b/MP42/MP42Heap.h index 5e89ec0..b25e55a 100644 --- a/MP42/MP42Heap.h +++ b/MP42/MP42Heap.h @@ -14,14 +14,7 @@ NS_ASSUME_NONNULL_BEGIN * A simple heap/priority queue implementations with a static size. * It takes a NSComparator in input. */ -@interface MP42Heap : NSObject { -@private - id *_array; - uint64 _size; - uint64 _len; - - NSComparator _cmptr; -} +@interface MP42Heap : NSObject - (instancetype)initWithCapacity:(NSUInteger)numItems andComparator:(NSComparator)cmptr; diff --git a/MP42/MP42Heap.m b/MP42/MP42Heap.m index b713cce..7463651 100644 --- a/MP42/MP42Heap.m +++ b/MP42/MP42Heap.m @@ -26,7 +26,14 @@ uint64 right(uint64 i) { return i * 2 + 2; } -@implementation MP42Heap +@implementation MP42Heap { +@private + id *_array; + uint64 _size; + uint64 _len; + + NSComparator _cmptr; +} - (instancetype)initWithCapacity:(NSUInteger)numItems andComparator:(NSComparator)cmptr { self = [super init]; diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index 3e77231..f5873ce 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -179,6 +179,8 @@ //uint8_t *hvCCAtom = (uint8_t *)magicCookie.bytes; NSAssert(NO, @"H.265 muxing not implemented yet"); + + [helper->importer setActiveTrack:track]; } // MPEG-4 Visual video track @@ -254,8 +256,9 @@ else if ([track isMemberOfClass:[MP42AudioTrack class]] && [track.format isEqualToString:MP42AudioFormatALAC]) { dstTrackId = MP4AddALACAudioTrack(_fileHandle, timeScale); - if ([magicCookie length]) - MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.alac.alac.AppleLosslessMagicCookie", [magicCookie bytes], [magicCookie length]); + if (magicCookie.length) { + MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.alac.alac.AppleLosslessMagicCookie", magicCookie.bytes, magicCookie.length); + } [helper->importer setActiveTrack:track]; } @@ -277,12 +280,13 @@ NSInteger vPlacement = [(MP42SubtitleTrack*)track verticalPlacement]; - for (id workingTrack in _workingTracks) + for (id workingTrack in _workingTracks) { if ([workingTrack isMemberOfClass:[MP42VideoTrack class]]) { videoSize.width = [workingTrack trackWidth]; videoSize.height = [workingTrack trackHeight]; break; } + } if (!videoSize.width) { MP4TrackId videoTrack = findFirstVideoTrack(_fileHandle); @@ -301,8 +305,9 @@ else subSize.height = 0.15 * videoSize.height; } - else + else { subSize.height = videoSize.height; + } const uint8_t textColor[4] = { 255,255,255,255 }; dstTrackId = MP4AddSubtitleTrack(_fileHandle, timeScale, videoSize.width, subSize.height); @@ -359,27 +364,56 @@ [helper->importer setActiveTrack:track]; } + // VobSub bitmap track else if ([track isMemberOfClass:[MP42SubtitleTrack class]] && [track.format isEqualToString:MP42SubtitleFormatVobSub]) { - if ([magicCookie length] < sizeof(uint32_t) * 16) + if (magicCookie.length < sizeof(uint32_t) * 16) continue; dstTrackId = MP4AddSubpicTrack(_fileHandle, timeScale, 640, 480); - uint32_t *subPalette = (uint32_t*) [magicCookie bytes]; - int ii; - for ( ii = 0; ii < 16; ii++ ) + uint32_t *subPalette = (uint32_t *) magicCookie.bytes; + for (int ii = 0; ii < 16; ii++) { subPalette[ii] = rgb2yuv(subPalette[ii]); + } uint8_t palette[16][4]; - for ( ii = 0; ii < 16; ii++ ) { + for (int ii = 0; ii < 16; ii++ ) { palette[ii][0] = 0; palette[ii][1] = (subPalette[ii] >> 16) & 0xff; palette[ii][2] = (subPalette[ii] >> 8) & 0xff; palette[ii][3] = (subPalette[ii]) & 0xff; } MP4SetTrackESConfiguration(_fileHandle, dstTrackId, - (uint8_t*)palette, 16 * 4 ); + (uint8_t *)palette, 16 * 4 ); + + [helper->importer setActiveTrack:track]; + } + + // WebVTT + else if ([track isMemberOfClass:[MP42SubtitleTrack class]] && [track.format isEqualToString:MP42SubtitleFormatWebVTT]) { + NSSize videoSize = [helper->importer sizeForTrack:track]; + + for (id workingTrack in _workingTracks) + if ([workingTrack isMemberOfClass:[MP42VideoTrack class]]) { + videoSize.width = [workingTrack trackWidth]; + videoSize.height = [workingTrack trackHeight]; + break; + } + + if (!videoSize.width) { + MP4TrackId videoTrack = findFirstVideoTrack(_fileHandle); + if (videoTrack) { + videoSize.width = getFixedVideoWidth(_fileHandle, videoTrack); + videoSize.height = MP4GetTrackVideoHeight(_fileHandle, videoTrack); + } + else { + videoSize.width = 640; + videoSize.height = 480; + } + } + + dstTrackId = MP4AddWebVTTTrack(_fileHandle, timeScale, videoSize.width, videoSize.height, magicCookie.bytes, magicCookie.length); [helper->importer setActiveTrack:track]; } @@ -460,10 +494,16 @@ MP42SampleBuffer *sampleBuffer = nil; for (int i = 0; i < 100 && (sampleBuffer = [track copyNextSample]) != nil; i++) { - if (!MP4WriteSample(_fileHandle, track.trackId, - sampleBuffer->data, sampleBuffer->size, - sampleBuffer->duration, sampleBuffer->offset, - sampleBuffer->isSync)) { + + // Check if the track helper is done. + if (sampleBuffer->flags & MP42SampleBufferFlagEndOfFile) { + track.muxer_helper->done = 1; + } + + else if (!MP4WriteSample(_fileHandle, track.trackId, + sampleBuffer->data, sampleBuffer->size, + sampleBuffer->duration, sampleBuffer->offset, + sampleBuffer->isSync)) { _cancelled = YES; } diff --git a/MP42/MP42Track+Muxer.m b/MP42/MP42Track+Muxer.m index f6a0bf9..e012859 100644 --- a/MP42/MP42Track+Muxer.m +++ b/MP42/MP42Track+Muxer.m @@ -21,29 +21,14 @@ } - (MP42SampleBuffer *)copyNextSample { - MP42SampleBuffer *sample = nil; muxer_helper *helper = (muxer_helper *)_helper; if (helper->converter) { - if ([helper->importer done]) { - [helper->converter setInputDone]; - } - - if ([helper->converter encoderDone]) { - helper->done = YES; - } - - sample = [helper->converter copyEncodedSample]; + return [helper->converter copyEncodedSample]; } else { - if ([helper->fifo isEmpty] && [helper->importer done]) { - helper->done = YES; - } - - sample = [helper->fifo dequeue]; + return [helper->fifo dequeue]; } - - return sample; }