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
+63 -45

No files matched your search

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