diff --git a/MP42/MP42AACImporter.mm b/MP42/MP42AACImporter.mm index d01ef21..ecf96b7 100644 --- a/MP42/MP42AACImporter.mm +++ b/MP42/MP42AACImporter.mm @@ -858,7 +858,7 @@ static bool GetFirstHeader(FILE* inFile) int64_t currentSize = 0; - while (LoadNextAacFrame(inFile, sampleBuffer, &sampleSize, true) && !_cancelled) { + while (LoadNextAacFrame(inFile, sampleBuffer, &sampleSize, true) && !self.isCancelled) { MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; void * sampleDataBuffer = malloc(sampleSize); @@ -878,7 +878,7 @@ static bool GetFirstHeader(FILE* inFile) sampleSize = sizeof(sampleBuffer); currentSize += sampleSize; - _progress = (currentSize / (CGFloat) size) * 100; + self.progress = (currentSize / (CGFloat) size) * 100; } [self setDone]; diff --git a/MP42/MP42AC3Importer.m b/MP42/MP42AC3Importer.m index e4719fa..d69cada 100644 --- a/MP42/MP42AC3Importer.m +++ b/MP42/MP42AC3Importer.m @@ -369,7 +369,7 @@ static bool GetFirstHeader(FILE* inFile) int64_t currentSize = 0; - while (LoadNextAc3Frame(inFile, &pBuf, &pBufSize, false) && !_cancelled) { + while (LoadNextAc3Frame(inFile, &pBuf, &pBufSize, false) && !self.isCancelled) { MP42SampleBuffer *sample = [[MP42SampleBuffer alloc] init]; void *sampleDataBuffer = malloc(pBufSize); @@ -387,7 +387,7 @@ static bool GetFirstHeader(FILE* inFile) sampleId++; currentSize += pBufSize; - _progress = (currentSize / (CGFloat) size) * 100; + self.progress = (currentSize / (CGFloat) size) * 100; } free(pBuf); diff --git a/MP42/MP42AVFImporter.m b/MP42/MP42AVFImporter.m index 225cd8e..b27c928 100644 --- a/MP42/MP42AVFImporter.m +++ b/MP42/MP42AVFImporter.m @@ -937,7 +937,7 @@ demuxHelper = track.muxer_helper->demuxer_context; AVAssetReaderOutput *assetReaderOutput = demuxHelper->assetReaderOutput; - while (!_cancelled) { + while (!self.isCancelled) { CMSampleBufferRef sampleBuffer = [assetReaderOutput copyNextSampleBuffer]; @@ -1205,7 +1205,7 @@ } CFRelease(sampleBuffer); - _progress = (((CGFloat) currentDataLength / totalDataLength ) * 100); + self.progress = (((CGFloat) currentDataLength / totalDataLength ) * 100); } else { break; diff --git a/MP42/MP42CCImporter.m b/MP42/MP42CCImporter.m index 434834b..8a71171 100644 --- a/MP42/MP42CCImporter.m +++ b/MP42/MP42CCImporter.m @@ -159,7 +159,7 @@ static int ParseByte(const char *string, UInt8 *byte, Boolean hex) } for (MP42TextSample *ccSample in sampleArray) { - if (_cancelled) { + if (self.isCancelled) { break; } @@ -242,7 +242,7 @@ static int ParseByte(const char *string, UInt8 *byte, Boolean hex) [sample release]; i++; - _progress = ((CGFloat)i / sampleArray.count) * 100; + self.progress = ((CGFloat)i / sampleArray.count) * 100; } [sampleArray release]; diff --git a/MP42/MP42Fifo.m b/MP42/MP42Fifo.m index 9ec310a..9c312fd 100644 --- a/MP42/MP42Fifo.m +++ b/MP42/MP42Fifo.m @@ -15,13 +15,15 @@ int32_t _head; int32_t _tail; - int32_t _count; int32_t _size; + _Atomic int32_t _count; - int32_t _cancelled; + _Atomic int32_t _cancelled; dispatch_semaphore_t _full; dispatch_semaphore_t _empty; + + dispatch_queue_t _queue; } - (instancetype)init { @@ -36,7 +38,7 @@ _array = (id *) malloc(sizeof(id) * _size); _full = dispatch_semaphore_create(_size - 1); _empty = dispatch_semaphore_create(0); - + _queue = dispatch_queue_create("org.subler.FifoQueue", DISPATCH_QUEUE_SERIAL); } return self; } @@ -48,26 +50,32 @@ dispatch_semaphore_wait(_full, DISPATCH_TIME_FOREVER); - _array[_tail++] = item; + dispatch_sync(_queue, ^{ + _array[_tail++] = item; + }); if (_tail == _size) { _tail = 0; } - OSAtomicIncrement32Barrier(&_count); + _count++; dispatch_semaphore_signal(_empty); } - (nullable id)dequeue NS_RETURNS_RETAINED { if (!_count) return nil; - id item = _array[_head++]; + __block id item = nil; + + dispatch_sync(_queue, ^{ + item = _array[_head++]; + }); if (_head == _size) { _head = 0; } - OSAtomicDecrement32Barrier(&_count); + _count--; dispatch_semaphore_signal(_full); return item; @@ -104,7 +112,7 @@ } - (void)cancel { - OSAtomicIncrement32(&_cancelled); + _cancelled = 1; [self drain]; } @@ -114,6 +122,7 @@ free(_array); dispatch_release(_full); dispatch_release(_empty); + dispatch_release(_queue); [super dealloc]; } diff --git a/MP42/MP42File.m b/MP42/MP42File.m index 77215cf..943871a 100644 --- a/MP42/MP42File.m +++ b/MP42/MP42File.m @@ -559,7 +559,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { - (BOOL)optimize { __block BOOL noErr = NO; - __block int32_t done = 0; + __block _Atomic int32_t done = 0; @autoreleasepool { NSError *error = nil; @@ -572,7 +572,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { dispatch_async(dispatch_get_global_queue(0, 0), ^{ noErr = MP4Optimize(self.URL.fileSystemRepresentation, tempURL.fileSystemRepresentation); - OSAtomicIncrement32Barrier(&done); + done = 1; dispatch_semaphore_signal(sem); }); @@ -649,7 +649,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { __block BOOL noErr = YES; if (![self.URL isEqualTo:url]) { - __block int32_t done = 0; + __block _Atomic int32_t done = 0; dispatch_semaphore_t sem = dispatch_semaphore_create(0); NSFileManager *fileManager = [[NSFileManager alloc] init]; @@ -659,7 +659,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { noErr = [fileManager copyItemAtURL:self.URL toURL:url error:outError]; if (!noErr && *outError) { } - OSAtomicIncrement32Barrier(&done); + done = 1; dispatch_semaphore_signal(sem); }); @@ -917,7 +917,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) { NSRectFill(rect); if (chapterT.image.image) - [[chapterT image].image drawInRect:rect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; + [[chapterT image].image drawInRect:rect fromRect:NSZeroRect operation:NSCompositingOperationCopy fraction:1.0]; [NSGraphicsContext restoreGraphicsState]; diff --git a/MP42/MP42FileImporter+Private.h b/MP42/MP42FileImporter+Private.h index a35dbb2..c94f490 100644 --- a/MP42/MP42FileImporter+Private.h +++ b/MP42/MP42FileImporter+Private.h @@ -36,7 +36,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)enqueue:(MP42SampleBuffer *)sample; -- (double)progress; +@property (nonatomic, readwrite) double progress; +@property (nonatomic, readonly, getter=isCancelled) BOOL cancelled; + - (void)setDone; @end diff --git a/MP42/MP42FileImporter.h b/MP42/MP42FileImporter.h index 4c206a9..21d73aa 100644 --- a/MP42/MP42FileImporter.h +++ b/MP42/MP42FileImporter.h @@ -13,20 +13,16 @@ NS_ASSUME_NONNULL_BEGIN @class MP42Metadata; @class MP42Track; -@interface MP42FileImporter : NSObject { -@protected - double _progress; - int32_t _cancelled; -} +@interface MP42FileImporter : NSObject + (NSArray *)supportedFileFormats; + (BOOL)canInitWithFileType:(NSString *)fileType; - (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)error; -@property(nonatomic, readonly) NSURL *fileURL; -@property(nonatomic, readonly) MP42Metadata *metadata; -@property(nonatomic, readonly) NSArray *tracks; +@property (nonatomic, readonly) NSURL *fileURL; +@property (nonatomic, readonly) MP42Metadata *metadata; +@property (nonatomic, readonly) NSArray *tracks; @end diff --git a/MP42/MP42FileImporter.m b/MP42/MP42FileImporter.m index 4702859..2a81b5a 100644 --- a/MP42/MP42FileImporter.m +++ b/MP42/MP42FileImporter.m @@ -44,6 +44,9 @@ static NSArray *_supportedFileFormats; NSThread *_demuxerThread; dispatch_semaphore_t _doneSem; + + _Atomic double _progress; + _Atomic BOOL _cancelled; } + (void)initialize { @@ -253,7 +256,7 @@ static NSArray *_supportedFileFormats; - (void)cancelReading { - OSAtomicIncrement32(&_cancelled); + _cancelled = YES; // wait until the demuxer thread exits dispatch_semaphore_wait(_doneSem, DISPATCH_TIME_FOREVER); @@ -296,11 +299,21 @@ static NSArray *_supportedFileFormats; dispatch_semaphore_signal(_doneSem); } +- (void)setProgress:(double)progress +{ + _progress = progress; +} + - (double)progress { return _progress; } +- (BOOL)isCancelled +{ + return _cancelled; +} + - (MP42Track *)inputTrackWithTrackID:(MP4TrackId)trackId { for (MP42Track *track in _inputTracks) { diff --git a/MP42/MP42H264Importer.mm b/MP42/MP42H264Importer.mm index f64ea33..f8747f4 100644 --- a/MP42/MP42H264Importer.mm +++ b/MP42/MP42H264Importer.mm @@ -1485,7 +1485,7 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height memset(&h264_dec, 0, sizeof(h264_dec)); DpbInit(&h264_dpb); - while ( (LoadNal(&nal) != false) && !_cancelled) { + while ( (LoadNal(&nal) != false) && !self.isCancelled) { uint32_t header_size; header_size = nal.buffer[2] == 1 ? 3 : 4; bool boundary = h264_detect_boundary(nal.buffer, @@ -1511,7 +1511,7 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height [sample release]; currentSize += nal_buffer_size; - _progress = (currentSize / (CGFloat) _size) * 100; + self.progress = (currentSize / (CGFloat) _size) * 100; sampleId++; DpbAdd( &h264_dpb, poc, slice_is_idr ); @@ -1598,7 +1598,7 @@ NSData* H264Info(const char *filePath, uint32_t *pic_width, uint32_t *pic_height [sample release]; currentSize += nal_buffer_size; - _progress = (currentSize / (CGFloat) _size) * 100; + self.progress = (currentSize / (CGFloat) _size) * 100; DpbAdd(&h264_dpb, h264_dec.pic_order_cnt, slice_is_idr); } diff --git a/MP42/MP42MkvImporter.m b/MP42/MP42MkvImporter.m index 1c6813a..823e2af 100644 --- a/MP42/MP42MkvImporter.m +++ b/MP42/MP42MkvImporter.m @@ -800,8 +800,8 @@ static NSString * TrackNameToString(TrackInfo *track) mkv_SetTrackMask(_matroskaFile, TrackMask); - while (!mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) && !_cancelled) { - _progress = (StartTime / _fileDuration / 10000); + while (!mkv_ReadFrame(_matroskaFile, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) && !self.isCancelled) { + self.progress = (StartTime / _fileDuration / 10000); muxer_helper *helper = NULL; MP42Track *track = nil; @@ -842,7 +842,8 @@ static NSString * TrackNameToString(TrackInfo *track) #ifdef VARIABLE_AUDIO_RATE if (demuxHelper->previousSample) { - uint64_t sampleDuration = (sample->decodeTimestamp * (double)mkv_TruncFloat(trackInfo->AV.Audio.SamplingFreq) / 1000000000.f) - demuxHelper->currentTime; + 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. @@ -1116,7 +1117,7 @@ static NSString * TrackNameToString(TrackInfo *track) [ss setFinished:YES]; - while (![ss isEmpty] && !_cancelled) { + while (![ss isEmpty] && !self.isCancelled) { SBSubLine *sl = [ss getSerializedPacket]; if ([sl->line isEqualToString:@"\n"]) { diff --git a/MP42/MP42Mp4Importer.m b/MP42/MP42Mp4Importer.m index bfef430..a1890c9 100644 --- a/MP42/MP42Mp4Importer.m +++ b/MP42/MP42Mp4Importer.m @@ -299,7 +299,7 @@ MP4Duration timescale = MP4GetTimeScale(_fileHandle); while (tracksDone != tracksNumber) { - if (_cancelled) { + if (self.isCancelled) { break; } @@ -307,7 +307,7 @@ muxer_helper *helper = track.muxer_helper; demuxHelper = helper->demuxer_context; - if (_cancelled) { + if (self.isCancelled) { break; } @@ -354,7 +354,7 @@ } } - _progress = ((CGFloat)currentTime * timescale / totalDuration) * 100; + self.progress = ((CGFloat)currentTime * timescale / totalDuration) * 100; currentTime += 3; } diff --git a/MP42/MP42PreviewGenerator.m b/MP42/MP42PreviewGenerator.m index 0202709..f9e0d88 100644 --- a/MP42/MP42PreviewGenerator.m +++ b/MP42/MP42PreviewGenerator.m @@ -35,15 +35,30 @@ generator.requestedTimeToleranceAfter = kCMTimeZero; CGFloat offset; + MP42Duration assetDuration = CMTimeGetSeconds(asset.duration) * 1000; for (NSInteger idx = 0; idx < chapters.count; idx++) { MP42TextSample *chapter = chapters[idx]; - MP42Duration nextChapterTimestamp = (idx < chapters.count - 1) ? [chapters[idx+1] timestamp] : CMTimeGetSeconds(asset.duration) * 1000; - if (position <= 0.0) offset = MINIMUM_OFFSET; - else if (position >= 1.0) offset = ((nextChapterTimestamp - [chapter timestamp]) * position) - MINIMUM_OFFSET; - else offset = (nextChapterTimestamp - [chapter timestamp]) * position; - CMTime time = CMTimeMake([chapter timestamp] + MAX(MINIMUM_OFFSET, offset), 1000); + + if (chapter.timestamp > assetDuration) { + break; + } + + MP42Duration nextChapterTimestamp = (idx < chapters.count - 1) ? chapters[idx+1].timestamp : assetDuration; + + if (position <= 0.0) { + offset = MINIMUM_OFFSET; + } + else if (position >= 1.0) { + offset = ((nextChapterTimestamp - chapter.timestamp) * position) - MINIMUM_OFFSET; + } + else { + offset = (nextChapterTimestamp - chapter.timestamp) * position; + } + + CMTime time = CMTimeMake(chapter.timestamp + MAX(MINIMUM_OFFSET, offset), 1000); + CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:NULL]; if (imgRef) { NSSize size = NSMakeSize(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); diff --git a/MP42/MP42SrtImporter.m b/MP42/MP42SrtImporter.m index 888797b..68fa133 100644 --- a/MP42/MP42SrtImporter.m +++ b/MP42/MP42SrtImporter.m @@ -155,7 +155,7 @@ trackSize.width = track.trackWidth; trackSize.height = track.trackHeight; - while (![_ss isEmpty] && !_cancelled) { + while (!_ss.isEmpty && !self.isCancelled) { SBSubLine *sl = [_ss getSerializedPacket]; if ([sl->line isEqualToString:@"\n"]) { @@ -171,7 +171,7 @@ } } - _progress = 100.0; + self.progress = 100.0; [self setDone]; } diff --git a/MP42/MP42SubUtilities.h b/MP42/MP42SubUtilities.h index 33b1530..4924352 100644 --- a/MP42/MP42SubUtilities.h +++ b/MP42/MP42SubUtilities.h @@ -27,7 +27,7 @@ @interface SBSubSerializer : NSObject { // input lines, sorted by 1. beginning time 2. original insertion order - NSMutableArray *lines; + NSMutableArray *lines; BOOL finished; BOOL position_information, forced; diff --git a/MP42/MP42VobSubImporter.m b/MP42/MP42VobSubImporter.m index c624ff2..9d11e46 100644 --- a/MP42/MP42VobSubImporter.m +++ b/MP42/MP42VobSubImporter.m @@ -314,9 +314,9 @@ static NSArray * LoadVobSubSubtitles(NSURL *theDirectory, NSStr SBVobSubSample *firstSample = nil; uint32_t lastTime = 0; - int i, sampleCount = [vobTrack->samples count]; + int sampleCount = vobTrack->samples.count; - for (i = 0; i < sampleCount && !_cancelled; i++) { + for (int i = 0; i < sampleCount && !self.isCancelled; i++) { SBVobSubSample *currentSample = [vobTrack->samples objectAtIndex:i]; int offset = currentSample->fileOffset; int nextOffset; @@ -392,7 +392,7 @@ static NSArray * LoadVobSubSubtitles(NSURL *theDirectory, NSStr lastTime = endTime; - _progress = ((i / (CGFloat) sampleCount ) * 100 / tracksNumber) + (tracksDone / (CGFloat) tracksNumber * 100); + self.progress = ((i / (CGFloat) sampleCount ) * 100 / tracksNumber) + (tracksDone / (CGFloat) tracksNumber * 100); } tracksDone++; } diff --git a/MP42/muxer/MatroskaParser.c b/MP42/muxer/MatroskaParser.c index 3dc51ab..6ed135c 100644 --- a/MP42/muxer/MatroskaParser.c +++ b/MP42/muxer/MatroskaParser.c @@ -776,7 +776,7 @@ static ulonglong readUInt(MatroskaFile *mf,unsigned int len) { static inline longlong readSInt(MatroskaFile *mf,unsigned int len) { longlong v = readUInt(mf,(unsigned)len); int s = 64 - (len<<3); - return (v << s) >> s; + return ((ulonglong)v << s) >> s; } static MKFLOAT readFloat(MatroskaFile *mf,unsigned int len) {