Moved around things

This commit is contained in:
Damiano Galassi committed 2015-09-28 13:46:06 +02:00
1 parent 3c6ed5896c
commit 10b77b2587
2 files changed
+60 -42

No files matched your search

+2 -1
View File
@@ -63,6 +63,7 @@ struct AudioFileIO
Float64 sampleRate; Float64 sampleRate;
UInt32 inputChannelsCount; UInt32 inputChannelsCount;
UInt32 outputChannelCount; UInt32 outputChannelCount;
NSUInteger downmixType; NSUInteger downmixType;
NSUInteger layout; NSUInteger layout;
hb_chan_map_t *ichanmap; hb_chan_map_t *ichanmap;
@@ -76,7 +77,7 @@ struct AudioFileIO
struct AudioFileIO encoderData; struct AudioFileIO encoderData;
} }
- (instancetype)initWithTrack:(MP42AudioTrack *)track andMixdownType:(NSString *)mixdownType error:(NSError **)outError; - (instancetype)initWithTrack:(MP42AudioTrack *)track andMixdownType:(NSString *)mixdownType error:(NSError **)error;
- (void)addSample:(MP42SampleBuffer *)sample; - (void)addSample:(MP42SampleBuffer *)sample;
- (nullable MP42SampleBuffer *)copyEncodedSample; - (nullable MP42SampleBuffer *)copyEncodedSample;
+58 -41
View File
@@ -23,6 +23,8 @@
@implementation MP42AudioConverter @implementation MP42AudioConverter
#pragma mark - Encoder
OSStatus EncoderDataProc(AudioConverterRef inAudioConverter, OSStatus EncoderDataProc(AudioConverterRef inAudioConverter,
UInt32* ioNumberDataPackets, UInt32* ioNumberDataPackets,
AudioBufferList* ioData, AudioBufferList* ioData,
@@ -33,7 +35,8 @@ OSStatus EncoderDataProc(AudioConverterRef inAudioConverter,
// figure out how much to read // figure out how much to read
if (*ioNumberDataPackets > afio->numPacketsPerRead) { if (*ioNumberDataPackets > afio->numPacketsPerRead) {
*ioNumberDataPackets = afio->numPacketsPerRead;} *ioNumberDataPackets = afio->numPacketsPerRead;
}
// read from the fifo // read from the fifo
UInt32 outNumBytes; UInt32 outNumBytes;
@@ -73,7 +76,7 @@ OSStatus EncoderDataProc(AudioConverterRef inAudioConverter,
return err; return err;
} }
- (void)EncoderThreadMainRoutine:(id)sender - (void)encoderThreadMainRoutine
{ {
@autoreleasepool { @autoreleasepool {
encoderDone = NO; encoderDone = NO;
@@ -258,6 +261,7 @@ OSStatus EncoderDataProc(AudioConverterRef inAudioConverter,
return; return;
} }
#pragma mark - Decoder
// decoder input data proc callback // decoder input data proc callback
OSStatus DecoderDataProc(AudioConverterRef inAudioConverter, OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
@@ -312,9 +316,8 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
return err; return err;
} }
- (void)DecoderThreadMainRoutine:(id)sender - (void)decoderThreadMainRoutine {
{ @autoreleasepool {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
OSStatus err; OSStatus err;
// set up buffers and data proc info struct // set up buffers and data proc info struct
@@ -335,7 +338,7 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
UInt32 numOutputPackets = theOutputBufSize / outputSizePerPacket; UInt32 numOutputPackets = theOutputBufSize / outputSizePerPacket;
// Launch the encoder thread // Launch the encoder thread
encoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(EncoderThreadMainRoutine:) object:self]; encoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(encoderThreadMainRoutine) object:nil];
[encoderThread setName:@"AAC Encoder"]; [encoderThread setName:@"AAC Encoder"];
[encoderThread start]; [encoderThread start];
@@ -432,22 +435,30 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
AudioConverterDispose(decoderData.converter); AudioConverterDispose(decoderData.converter);
[pool drain]; }
return; return;
} }
- (instancetype)initWithTrack:(MP42AudioTrack *)track andMixdownType:(NSString *)mixdownType error:(NSError **)outError #pragma mark - Init
{
if ((self = [super init])) { - (instancetype)initWithTrack:(MP42AudioTrack *)track andMixdownType:(NSString *)mixdownType error:(NSError **)error {
self = [super init];
if (self) {
OSStatus err; OSStatus err;
// Load the decoders // Load the decoders
[[MP42ComponentLoader sharedLoader] loadBundledComponents]; [[MP42ComponentLoader sharedLoader] loadBundledComponents];
// Init the FIFO
_outputSamplesBuffer = [[MP42Fifo alloc] init];
_inputSamplesBuffer = [[MP42Fifo alloc] init];
// Set the right mixdown to use // Set the right mixdown to use
sampleRate = [track.muxer_helper->importer timescaleForTrack:track]; sampleRate = [track.muxer_helper->importer timescaleForTrack:track];
inputChannelsCount = [track sourceChannels]; inputChannelsCount = track.sourceChannels;
outputChannelCount = [track channels]; outputChannelCount = track.channels;
if (inputChannelsCount == 0 || outputChannelCount == 0 || sampleRate == 0) { if (inputChannelsCount == 0 || outputChannelCount == 0 || sampleRate == 0) {
[self release]; [self release];
@@ -475,7 +486,9 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
outputChannelCount = 2; outputChannelCount = 2;
} }
if (([track.sourceFormat isEqualToString:@"True HD"] || [track.sourceFormat isEqualToString:MP42AudioFormatAC3]) && inputChannelsCount == 6 ) { if (([track.sourceFormat isEqualToString:MP42AudioFormatTrueHD] ||
[track.sourceFormat isEqualToString:MP42AudioFormatAC3]) && inputChannelsCount == 6 ) {
ichanmap = &hb_smpte_chan_map; ichanmap = &hb_smpte_chan_map;
layout = HB_INPUT_CH_LAYOUT_3F2R | HB_INPUT_CH_LAYOUT_HAS_LFE; layout = HB_INPUT_CH_LAYOUT_3F2R | HB_INPUT_CH_LAYOUT_HAS_LFE;
} }
@@ -483,9 +496,6 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
ichanmap = &hb_qt_chan_map; ichanmap = &hb_qt_chan_map;
} }
_outputSamplesBuffer = [[MP42Fifo alloc] init];
_inputSamplesBuffer = [[MP42Fifo alloc] init];
// Decoder initialization // Decoder initialization
CFDataRef magicCookie = NULL; CFDataRef magicCookie = NULL;
NSData *srcMagicCookie = [track.muxer_helper->importer magicCookieForTrack:track]; NSData *srcMagicCookie = [track.muxer_helper->importer magicCookieForTrack:track];
@@ -500,7 +510,7 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
inputFormat.mFormatID = kAudioFormatMPEG4AAC; inputFormat.mFormatID = kAudioFormatMPEG4AAC;
size_t cookieSize; size_t cookieSize;
uint8_t * cookie = CreateEsdsFromSetupData((uint8_t *)[srcMagicCookie bytes], [srcMagicCookie length], &cookieSize, 1, true, false); uint8_t *cookie = CreateEsdsFromSetupData((uint8_t *)srcMagicCookie.bytes, srcMagicCookie.length, &cookieSize, 1, true, false);
magicCookie = (CFDataRef) [[NSData dataWithBytes:cookie length:cookieSize] retain]; magicCookie = (CFDataRef) [[NSData dataWithBytes:cookie length:cookieSize] retain];
} }
else if ([track.sourceFormat isEqualToString:MP42AudioFormatALAC]) { else if ([track.sourceFormat isEqualToString:MP42AudioFormatALAC]) {
@@ -511,17 +521,20 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
else if ([track.sourceFormat isEqualToString:MP42AudioFormatVorbis]) { else if ([track.sourceFormat isEqualToString:MP42AudioFormatVorbis]) {
inputFormat.mFormatID = 'XiVs'; inputFormat.mFormatID = 'XiVs';
magicCookie = createDescExt_XiphVorbis([srcMagicCookie length], [srcMagicCookie bytes]); magicCookie = createDescExt_XiphVorbis(srcMagicCookie.length, srcMagicCookie.bytes);
} }
else if ([track.sourceFormat isEqualToString:MP42AudioFormatFLAC]) { else if ([track.sourceFormat isEqualToString:MP42AudioFormatFLAC]) {
inputFormat.mFormatID = 'XiFL'; inputFormat.mFormatID = 'XiFL';
magicCookie = createDescExt_XiphFLAC([srcMagicCookie length], [srcMagicCookie bytes]); magicCookie = createDescExt_XiphFLAC(srcMagicCookie.length, srcMagicCookie.bytes);
} }
else if ([track.sourceFormat isEqualToString:MP42AudioFormatAC3]) { else if ([track.sourceFormat isEqualToString:MP42AudioFormatAC3]) {
inputFormat.mFormatID = kAudioFormatAC3; inputFormat.mFormatID = kAudioFormatAC3;
inputFormat.mFramesPerPacket = 1536; inputFormat.mFramesPerPacket = 1536;
} }
else if ([track.sourceFormat isEqualToString:MP42AudioFormatEAC3]) {
// TODO
}
else if ([track.sourceFormat isEqualToString:MP42AudioFormatDTS]) { else if ([track.sourceFormat isEqualToString:MP42AudioFormatDTS]) {
inputFormat.mFormatID = 'DTS '; inputFormat.mFormatID = 'DTS ';
} }
@@ -529,7 +542,7 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
inputFormat.mFormatID = kAudioFormatMPEGLayer3; inputFormat.mFormatID = kAudioFormatMPEGLayer3;
inputFormat.mFramesPerPacket = 1152; inputFormat.mFramesPerPacket = 1152;
} }
else if ([track.sourceFormat isEqualToString:@"True HD"]) { else if ([track.sourceFormat isEqualToString:MP42AudioFormatTrueHD]) {
inputFormat.mFormatID = 'trhd'; inputFormat.mFormatID = 'trhd';
} }
else if ([track.sourceFormat isEqualToString:MP42AudioFormatPCM]) { else if ([track.sourceFormat isEqualToString:MP42AudioFormatPCM]) {
@@ -541,6 +554,15 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
inputFormat.mFormatID = kAudioFormatLinearPCM; inputFormat.mFormatID = kAudioFormatLinearPCM;
} }
} }
else {
if (error) {
*error = MP42Error(@"Audio Converter Error.",
@"Unsupported input format.",
130);
}
[self release];
return nil;
}
} }
bzero(&outputFormat, sizeof(AudioStreamBasicDescription)); bzero(&outputFormat, sizeof(AudioStreamBasicDescription));
@@ -553,11 +575,11 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
outputFormat.mChannelsPerFrame = track.sourceChannels; outputFormat.mChannelsPerFrame = track.sourceChannels;
outputFormat.mBitsPerChannel = 32; outputFormat.mBitsPerChannel = 32;
// initialize the decoder // Initialize the decoder.
err = AudioConverterNew( &inputFormat, &outputFormat, &decoderData.converter ); err = AudioConverterNew( &inputFormat, &outputFormat, &decoderData.converter );
if (err != noErr) { if (err != noErr) {
if (outError) { if (error) {
*outError = MP42Error(@"Audio Converter Error.", *error = MP42Error(@"Audio Converter Error.",
@"The Audio Converter can not be initialized", @"The Audio Converter can not be initialized",
130); 130);
} }
@@ -572,8 +594,9 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
if (magicCookie) { if (magicCookie) {
err = AudioConverterSetProperty(decoderData.converter, kAudioConverterDecompressionMagicCookie, err = AudioConverterSetProperty(decoderData.converter, kAudioConverterDecompressionMagicCookie,
CFDataGetLength(magicCookie) , CFDataGetBytePtr(magicCookie) ); CFDataGetLength(magicCookie) , CFDataGetBytePtr(magicCookie) );
if( err != noErr) if( err != noErr) {
NSLog(@"Boom Magic Cookie %ld",(long)err); NSLog(@"Boom Magic Cookie %ld",(long)err);
}
CFRelease(magicCookie); CFRelease(magicCookie);
} }
@@ -593,8 +616,9 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
bzero( newLayout, sizeof( AudioChannelLayout ) ); bzero( newLayout, sizeof( AudioChannelLayout ) );
newLayout->mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_5_1_D; newLayout->mChannelLayoutTag = kAudioChannelLayoutTag_MPEG_5_1_D;
err = AudioConverterSetProperty(decoderData.converter, kAudioConverterInputChannelLayout, sizeof(AudioChannelLayout), newLayout); err = AudioConverterSetProperty(decoderData.converter, kAudioConverterInputChannelLayout, sizeof(AudioChannelLayout), newLayout);
if(err) if (err) {
NSLog(@"Unable to set the new channel layout %ld",(long)err); NSLog(@"Unable to set the new channel layout %ld",(long)err);
}
free(newLayout); free(newLayout);
} }
free(layout); free(layout);
@@ -626,32 +650,28 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
decoderData.outputFormat = outputFormat; decoderData.outputFormat = outputFormat;
// Launch the decoder thread. // Launch the decoder thread.
decoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(DecoderThreadMainRoutine:) object:self]; decoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(decoderThreadMainRoutine) object:nil];
[decoderThread setName:@"Audio Decoder"]; [decoderThread setName:[NSString stringWithFormat:@"%@ Decoder", track.sourceFormat]];
[decoderThread start]; [decoderThread start];
} }
return self; return self;
} }
- (void)addSample:(MP42SampleBuffer *)sample - (void)addSample:(MP42SampleBuffer *)sample {
{
[_inputSamplesBuffer enqueue:sample]; [_inputSamplesBuffer enqueue:sample];
} }
- (MP42SampleBuffer *)copyEncodedSample - (MP42SampleBuffer *)copyEncodedSample {
{
return [_outputSamplesBuffer deque]; return [_outputSamplesBuffer deque];
} }
- (void)setInputDone - (void)setInputDone {
{
decoderData.fileReaderDone = YES; decoderData.fileReaderDone = YES;
encoderData.fileReaderDone = YES; encoderData.fileReaderDone = YES;
} }
- (void)cancel - (void)cancel {
{
OSAtomicIncrement32(&_cancelled); OSAtomicIncrement32(&_cancelled);
decoderData.fileReaderDone = YES; decoderData.fileReaderDone = YES;
@@ -665,18 +685,15 @@ OSStatus DecoderDataProc(AudioConverterRef inAudioConverter,
} }
} }
- (BOOL)encoderDone - (BOOL)encoderDone {
{
return encoderDone && [_outputSamplesBuffer isEmpty]; return encoderDone && [_outputSamplesBuffer isEmpty];
} }
- (NSData *)magicCookie - (NSData *)magicCookie {
{
return [[outputMagicCookie retain] autorelease]; return [[outputMagicCookie retain] autorelease];
} }
- (void)dealloc - (void)dealloc {
{
sfifo_close(&fifo); sfifo_close(&fifo);
free(decoderData.srcBuffer); free(decoderData.srcBuffer);