Use a sample to indicate a reader EOF.
This commit is contained in:
1 parent
54437f9030
commit
6a2b4a1fad
12 files changed
+121
-71
No files matched your search
@@ -85,9 +85,6 @@ static struct AudioFileIO
|
|||||||
- (NSData *)magicCookie;
|
- (NSData *)magicCookie;
|
||||||
|
|
||||||
- (void)cancel;
|
- (void)cancel;
|
||||||
- (BOOL)encoderDone;
|
|
||||||
|
|
||||||
- (void)setInputDone;
|
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
|
||||||
|
|||||||
@@ -255,6 +255,7 @@ OSStatus EncoderDataProc(AudioConverterRef inAudioConverter,
|
|||||||
|
|
||||||
AudioConverterDispose(converterEnc);
|
AudioConverterDispose(converterEnc);
|
||||||
|
|
||||||
|
[self enqueueEndOfFileSample];
|
||||||
encoderDone = YES;
|
encoderDone = YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -666,7 +667,12 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)addSample:(MP42SampleBuffer *)sample {
|
- (void)addSample:(MP42SampleBuffer *)sample {
|
||||||
[_inputSamplesBuffer enqueue:sample];
|
if (sample->flags & MP42SampleBufferFlagEndOfFile) {
|
||||||
|
[self setInputDone];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
[_inputSamplesBuffer enqueue:sample];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (MP42SampleBuffer *)copyEncodedSample {
|
- (MP42SampleBuffer *)copyEncodedSample {
|
||||||
@@ -678,6 +684,17 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
|
|||||||
encoderData.fileReaderDone = YES;
|
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 {
|
- (void)cancel {
|
||||||
OSAtomicIncrement32(&_cancelled);
|
OSAtomicIncrement32(&_cancelled);
|
||||||
|
|
||||||
@@ -692,10 +709,6 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)encoderDone {
|
|
||||||
return encoderDone && [_outputSamplesBuffer isEmpty];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSData *)magicCookie {
|
- (NSData *)magicCookie {
|
||||||
return [[outputMagicCookie retain] autorelease];
|
return [[outputMagicCookie retain] autorelease];
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-15
@@ -10,21 +10,7 @@
|
|||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface MP42Fifo<__covariant ObjectType> : NSObject {
|
@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;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (instancetype)initWithCapacity:(NSUInteger)capacity NS_DESIGNATED_INITIALIZER;
|
- (instancetype)initWithCapacity:(NSUInteger)capacity NS_DESIGNATED_INITIALIZER;
|
||||||
|
|
||||||
|
|||||||
+15
-1
@@ -8,7 +8,21 @@
|
|||||||
|
|
||||||
#import "MP42Fifo.h"
|
#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 {
|
- (instancetype)init {
|
||||||
self = [self initWithCapacity:300];
|
self = [self initWithCapacity:300];
|
||||||
|
|||||||
@@ -578,6 +578,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
|
|||||||
NSError *error = nil;
|
NSError *error = nil;
|
||||||
NSURL *tempURL = [self tempURL];
|
NSURL *tempURL = [self tempURL];
|
||||||
NSFileManager *fileManager = [[NSFileManager alloc] init];
|
NSFileManager *fileManager = [[NSFileManager alloc] init];
|
||||||
|
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
|
||||||
|
|
||||||
if (tempURL) {
|
if (tempURL) {
|
||||||
unsigned long long originalFileSize = [[[fileManager attributesOfItemAtPath:self.URL.path error:nil] valueForKey:NSFileSize] unsignedLongLongValue];
|
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), ^{
|
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||||||
noErr = MP4Optimize(self.URL.path.fileSystemRepresentation, tempURL.path.fileSystemRepresentation);
|
noErr = MP4Optimize(self.URL.path.fileSystemRepresentation, tempURL.path.fileSystemRepresentation);
|
||||||
OSAtomicIncrement32Barrier(&done);
|
OSAtomicIncrement32Barrier(&done);
|
||||||
|
dispatch_semaphore_signal(sem);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Loop to check the progress
|
// Loop to check the progress
|
||||||
@@ -594,6 +596,8 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
|
|||||||
usleep(450000);
|
usleep(450000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
|
||||||
|
|
||||||
// Additional check to see if we can open the optimized file
|
// Additional check to see if we can open the optimized file
|
||||||
if (noErr && [[[MP42File alloc] initWithURL:tempURL error:NULL] autorelease]) {
|
if (noErr && [[[MP42File alloc] initWithURL:tempURL error:NULL] autorelease]) {
|
||||||
// Replace the original file
|
// Replace the original file
|
||||||
@@ -616,6 +620,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dispatch_release(sem);
|
||||||
[fileManager release];
|
[fileManager release];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,8 +33,6 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
- (void)enqueue:(MP42SampleBuffer *)sample;
|
- (void)enqueue:(MP42SampleBuffer *)sample;
|
||||||
|
|
||||||
- (double)progress;
|
- (double)progress;
|
||||||
|
|
||||||
- (BOOL)done;
|
|
||||||
- (void)setDone;
|
- (void)setDone;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
|
|
||||||
NSThread *_demuxerThread;
|
NSThread *_demuxerThread;
|
||||||
|
|
||||||
int32_t _done;
|
|
||||||
dispatch_semaphore_t _doneSem;
|
dispatch_semaphore_t _doneSem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+17
-4
@@ -231,7 +231,7 @@ static NSArray<NSString *> *_supportedFileFormats;
|
|||||||
|
|
||||||
_doneSem = dispatch_semaphore_create(0);
|
_doneSem = dispatch_semaphore_create(0);
|
||||||
|
|
||||||
if (!_demuxerThread && !_done) {
|
if (!_demuxerThread) {
|
||||||
_demuxerThread = [[NSThread alloc] initWithTarget:self selector:@selector(demux) object:nil];
|
_demuxerThread = [[NSThread alloc] initWithTarget:self selector:@selector(demux) object:nil];
|
||||||
_demuxerThread.name = self.description;
|
_demuxerThread.name = self.description;
|
||||||
|
|
||||||
@@ -274,14 +274,27 @@ static NSArray<NSString *> *_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
|
- (void)setDone
|
||||||
{
|
{
|
||||||
OSAtomicIncrement32(&_done);
|
[self enqueueEndOfFileSamples];
|
||||||
dispatch_semaphore_signal(_doneSem);
|
dispatch_semaphore_signal(_doneSem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-8
@@ -14,14 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|||||||
* A simple heap/priority queue implementations with a static size.
|
* A simple heap/priority queue implementations with a static size.
|
||||||
* It takes a NSComparator in input.
|
* It takes a NSComparator in input.
|
||||||
*/
|
*/
|
||||||
@interface MP42Heap<ObjectType> : NSObject {
|
@interface MP42Heap<ObjectType> : NSObject
|
||||||
@private
|
|
||||||
id *_array;
|
|
||||||
uint64 _size;
|
|
||||||
uint64 _len;
|
|
||||||
|
|
||||||
NSComparator _cmptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (instancetype)initWithCapacity:(NSUInteger)numItems andComparator:(NSComparator)cmptr;
|
- (instancetype)initWithCapacity:(NSUInteger)numItems andComparator:(NSComparator)cmptr;
|
||||||
|
|
||||||
|
|||||||
+8
-1
@@ -26,7 +26,14 @@ uint64 right(uint64 i) {
|
|||||||
return i * 2 + 2;
|
return i * 2 + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@implementation MP42Heap
|
@implementation MP42Heap {
|
||||||
|
@private
|
||||||
|
id *_array;
|
||||||
|
uint64 _size;
|
||||||
|
uint64 _len;
|
||||||
|
|
||||||
|
NSComparator _cmptr;
|
||||||
|
}
|
||||||
|
|
||||||
- (instancetype)initWithCapacity:(NSUInteger)numItems andComparator:(NSComparator)cmptr {
|
- (instancetype)initWithCapacity:(NSUInteger)numItems andComparator:(NSComparator)cmptr {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
|
|||||||
+54
-14
@@ -179,6 +179,8 @@
|
|||||||
//uint8_t *hvCCAtom = (uint8_t *)magicCookie.bytes;
|
//uint8_t *hvCCAtom = (uint8_t *)magicCookie.bytes;
|
||||||
|
|
||||||
NSAssert(NO, @"H.265 muxing not implemented yet");
|
NSAssert(NO, @"H.265 muxing not implemented yet");
|
||||||
|
|
||||||
|
[helper->importer setActiveTrack:track];
|
||||||
}
|
}
|
||||||
|
|
||||||
// MPEG-4 Visual video track
|
// MPEG-4 Visual video track
|
||||||
@@ -254,8 +256,9 @@
|
|||||||
else if ([track isMemberOfClass:[MP42AudioTrack class]] && [track.format isEqualToString:MP42AudioFormatALAC]) {
|
else if ([track isMemberOfClass:[MP42AudioTrack class]] && [track.format isEqualToString:MP42AudioFormatALAC]) {
|
||||||
dstTrackId = MP4AddALACAudioTrack(_fileHandle,
|
dstTrackId = MP4AddALACAudioTrack(_fileHandle,
|
||||||
timeScale);
|
timeScale);
|
||||||
if ([magicCookie length])
|
if (magicCookie.length) {
|
||||||
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];
|
[helper->importer setActiveTrack:track];
|
||||||
}
|
}
|
||||||
@@ -277,12 +280,13 @@
|
|||||||
|
|
||||||
NSInteger vPlacement = [(MP42SubtitleTrack*)track verticalPlacement];
|
NSInteger vPlacement = [(MP42SubtitleTrack*)track verticalPlacement];
|
||||||
|
|
||||||
for (id workingTrack in _workingTracks)
|
for (id workingTrack in _workingTracks) {
|
||||||
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];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!videoSize.width) {
|
if (!videoSize.width) {
|
||||||
MP4TrackId videoTrack = findFirstVideoTrack(_fileHandle);
|
MP4TrackId videoTrack = findFirstVideoTrack(_fileHandle);
|
||||||
@@ -301,8 +305,9 @@
|
|||||||
else
|
else
|
||||||
subSize.height = 0.15 * videoSize.height;
|
subSize.height = 0.15 * videoSize.height;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
subSize.height = videoSize.height;
|
subSize.height = videoSize.height;
|
||||||
|
}
|
||||||
|
|
||||||
const uint8_t textColor[4] = { 255,255,255,255 };
|
const uint8_t textColor[4] = { 255,255,255,255 };
|
||||||
dstTrackId = MP4AddSubtitleTrack(_fileHandle, timeScale, videoSize.width, subSize.height);
|
dstTrackId = MP4AddSubtitleTrack(_fileHandle, timeScale, videoSize.width, subSize.height);
|
||||||
@@ -359,27 +364,56 @@
|
|||||||
|
|
||||||
[helper->importer setActiveTrack:track];
|
[helper->importer setActiveTrack:track];
|
||||||
}
|
}
|
||||||
|
|
||||||
// VobSub bitmap track
|
// VobSub bitmap track
|
||||||
else if ([track isMemberOfClass:[MP42SubtitleTrack class]] && [track.format isEqualToString:MP42SubtitleFormatVobSub]) {
|
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;
|
continue;
|
||||||
|
|
||||||
dstTrackId = MP4AddSubpicTrack(_fileHandle, timeScale, 640, 480);
|
dstTrackId = MP4AddSubpicTrack(_fileHandle, timeScale, 640, 480);
|
||||||
|
|
||||||
uint32_t *subPalette = (uint32_t*) [magicCookie bytes];
|
uint32_t *subPalette = (uint32_t *) magicCookie.bytes;
|
||||||
int ii;
|
for (int ii = 0; ii < 16; ii++) {
|
||||||
for ( ii = 0; ii < 16; ii++ )
|
|
||||||
subPalette[ii] = rgb2yuv(subPalette[ii]);
|
subPalette[ii] = rgb2yuv(subPalette[ii]);
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t palette[16][4];
|
uint8_t palette[16][4];
|
||||||
for ( ii = 0; ii < 16; ii++ ) {
|
for (int ii = 0; ii < 16; ii++ ) {
|
||||||
palette[ii][0] = 0;
|
palette[ii][0] = 0;
|
||||||
palette[ii][1] = (subPalette[ii] >> 16) & 0xff;
|
palette[ii][1] = (subPalette[ii] >> 16) & 0xff;
|
||||||
palette[ii][2] = (subPalette[ii] >> 8) & 0xff;
|
palette[ii][2] = (subPalette[ii] >> 8) & 0xff;
|
||||||
palette[ii][3] = (subPalette[ii]) & 0xff;
|
palette[ii][3] = (subPalette[ii]) & 0xff;
|
||||||
}
|
}
|
||||||
MP4SetTrackESConfiguration(_fileHandle, dstTrackId,
|
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];
|
[helper->importer setActiveTrack:track];
|
||||||
}
|
}
|
||||||
@@ -460,10 +494,16 @@
|
|||||||
MP42SampleBuffer *sampleBuffer = nil;
|
MP42SampleBuffer *sampleBuffer = nil;
|
||||||
|
|
||||||
for (int i = 0; i < 100 && (sampleBuffer = [track copyNextSample]) != nil; i++) {
|
for (int i = 0; i < 100 && (sampleBuffer = [track copyNextSample]) != nil; i++) {
|
||||||
if (!MP4WriteSample(_fileHandle, track.trackId,
|
|
||||||
sampleBuffer->data, sampleBuffer->size,
|
// Check if the track helper is done.
|
||||||
sampleBuffer->duration, sampleBuffer->offset,
|
if (sampleBuffer->flags & MP42SampleBufferFlagEndOfFile) {
|
||||||
sampleBuffer->isSync)) {
|
track.muxer_helper->done = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (!MP4WriteSample(_fileHandle, track.trackId,
|
||||||
|
sampleBuffer->data, sampleBuffer->size,
|
||||||
|
sampleBuffer->duration, sampleBuffer->offset,
|
||||||
|
sampleBuffer->isSync)) {
|
||||||
_cancelled = YES;
|
_cancelled = YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-17
@@ -21,29 +21,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (MP42SampleBuffer *)copyNextSample {
|
- (MP42SampleBuffer *)copyNextSample {
|
||||||
MP42SampleBuffer *sample = nil;
|
|
||||||
muxer_helper *helper = (muxer_helper *)_helper;
|
muxer_helper *helper = (muxer_helper *)_helper;
|
||||||
|
|
||||||
if (helper->converter) {
|
if (helper->converter) {
|
||||||
if ([helper->importer done]) {
|
return [helper->converter copyEncodedSample];
|
||||||
[helper->converter setInputDone];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ([helper->converter encoderDone]) {
|
|
||||||
helper->done = YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
sample = [helper->converter copyEncodedSample];
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if ([helper->fifo isEmpty] && [helper->importer done]) {
|
return [helper->fifo dequeue];
|
||||||
helper->done = YES;
|
|
||||||
}
|
|
||||||
|
|
||||||
sample = [helper->fifo dequeue];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sample;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user