5 // Created on 2010-08-30.
9 #import "HBAudioController.h"
12 NSString *keyAudioCodecName = @"keyAudioCodecName";
13 NSString *keyAudioMP4 = @"keyAudioMP4";
14 NSString *keyAudioMKV = @"keyAudioMKV";
15 NSString *keyAudioSampleRateName = @"keyAudioSampleRateName";
16 NSString *keyAudioBitrateName = @"keyAudioBitrateName";
17 NSString *keyAudioMustMatchTrack = @"keyAudioMustMatchTrack";
18 NSString *keyAudioMixdownName = @"keyAudioMixdownName";
19 NSString *keyAudioMixdownLimitsToTrackBitRate = @"keyAudioMixdownLimitsToTrackBitRate";
20 NSString *keyAudioMixdownCanBeDefault = @"keyAudioMixdownCanBeDefault";
22 NSString *keyAudioCodec = @"codec";
23 NSString *keyAudioMixdown = @"mixdown";
24 NSString *keyAudioSamplerate = @"samplerate";
25 NSString *keyAudioBitrate = @"bitrate";
27 static NSMutableArray *masterCodecArray = nil;
28 static NSMutableArray *masterSampleRateArray = nil;
29 static NSMutableArray *masterBitRateArray = nil;
31 @interface NSArray (HBAudioSupport)
32 - (NSDictionary *) dictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey;
33 - (NSDictionary *) lastDictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey;
35 @implementation NSArray (HBAudioSupport)
36 - (NSDictionary *) dictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey reverse: (BOOL) reverse
39 NSDictionary *retval = nil;
40 NSEnumerator *enumerator = reverse ? [self reverseObjectEnumerator] : [self objectEnumerator];
44 while (nil != (dict = [enumerator nextObject]) && nil == retval) {
45 if (nil != (aValue = [dict objectForKey: aKey]) && YES == [aValue isEqual: anObject]) {
51 - (NSDictionary *) dictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey
52 { return [self dictionaryWithObject: anObject matchingKey: aKey reverse: NO]; }
53 - (NSDictionary *) lastDictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey
54 { return [self dictionaryWithObject: anObject matchingKey: aKey reverse: YES]; }
58 @implementation HBAudio
61 #pragma mark Object Setup
66 if ([HBAudio class] == self) {
68 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
71 masterCodecArray = [[NSMutableArray alloc] init]; // knowingly leaked
72 [masterCodecArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
73 NSLocalizedString(@"AAC (CoreAudio)", @"AAC (CoreAudio)"), keyAudioCodecName,
74 [NSNumber numberWithInt: HB_ACODEC_CA_AAC], keyAudioCodec,
75 [NSNumber numberWithBool: YES], keyAudioMP4,
76 [NSNumber numberWithBool: YES], keyAudioMKV,
77 [NSNumber numberWithBool: NO], keyAudioMustMatchTrack,
79 [masterCodecArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
80 NSLocalizedString(@"AAC (faac)", @"AAC (faac)"), keyAudioCodecName,
81 [NSNumber numberWithInt: HB_ACODEC_FAAC], keyAudioCodec,
82 [NSNumber numberWithBool: YES], keyAudioMP4,
83 [NSNumber numberWithBool: YES], keyAudioMKV,
84 [NSNumber numberWithBool: NO], keyAudioMustMatchTrack,
86 [masterCodecArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
87 NSLocalizedString(@"MP3 (lame)", @"MP3 (lame)"), keyAudioCodecName,
88 [NSNumber numberWithInt: HB_ACODEC_LAME], keyAudioCodec,
89 [NSNumber numberWithBool: YES], keyAudioMP4,
90 [NSNumber numberWithBool: YES], keyAudioMKV,
91 [NSNumber numberWithBool: NO], keyAudioMustMatchTrack,
93 [masterCodecArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
94 NSLocalizedString(@"AC3 Passthru", @"AC3 Passthru"), keyAudioCodecName,
95 [NSNumber numberWithInt: HB_ACODEC_AC3_PASS], keyAudioCodec,
96 [NSNumber numberWithBool: YES], keyAudioMP4,
97 [NSNumber numberWithBool: YES], keyAudioMKV,
98 [NSNumber numberWithInt: HB_ACODEC_AC3], keyAudioMustMatchTrack,
100 [masterCodecArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
101 NSLocalizedString(@"AC3", @"AC3"), keyAudioCodecName,
102 [NSNumber numberWithInt: HB_ACODEC_AC3], keyAudioCodec,
103 [NSNumber numberWithBool: YES], keyAudioMP4,
104 [NSNumber numberWithBool: YES], keyAudioMKV,
105 [NSNumber numberWithBool: NO], keyAudioMustMatchTrack,
107 [masterCodecArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
108 NSLocalizedString(@"DTS Passthru", @"DTS Passthru"), keyAudioCodecName,
109 [NSNumber numberWithInt: HB_ACODEC_DCA_PASS], keyAudioCodec,
110 [NSNumber numberWithBool: NO], keyAudioMP4,
111 [NSNumber numberWithBool: YES], keyAudioMKV,
112 [NSNumber numberWithInt: HB_ACODEC_DCA], keyAudioMustMatchTrack,
114 [masterCodecArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
115 NSLocalizedString(@"Vorbis (vorbis)", @"Vorbis (vorbis)"), keyAudioCodecName,
116 [NSNumber numberWithInt: HB_ACODEC_VORBIS], keyAudioCodec,
117 [NSNumber numberWithBool: NO], keyAudioMP4,
118 [NSNumber numberWithBool: YES], keyAudioMKV,
119 [NSNumber numberWithBool: NO], keyAudioMustMatchTrack,
122 // Note that for the Auto value we use 0 for the sample rate because our controller will give back the track's
123 // input sample rate when it finds this 0 value as the selected sample rate. We do this because the input
124 // sample rate depends on the track, which means it depends on the title, so cannot be nicely set up here.
125 masterSampleRateArray = [[NSMutableArray alloc] init]; // knowingly leaked
126 [masterSampleRateArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
127 NSLocalizedString(@"Auto", @"Auto"), keyAudioSampleRateName,
128 [NSNumber numberWithInt: 0], keyAudioSamplerate,
130 for (i = 0; i < hb_audio_rates_count; i++) {
131 [masterSampleRateArray addObject: [NSDictionary dictionaryWithObjectsAndKeys:
132 [NSString stringWithUTF8String: hb_audio_rates[i].string], keyAudioSampleRateName,
133 [NSNumber numberWithInt: hb_audio_rates[i].rate], keyAudioSamplerate,
137 masterBitRateArray = [[NSMutableArray alloc] init]; // knowingly leaked
138 for (i = 0; i < hb_audio_bitrates_count; i++) {
139 int rate = hb_audio_bitrates[i].rate;
140 dict = [NSDictionary dictionaryWithObjectsAndKeys:
141 [NSString stringWithUTF8String: hb_audio_bitrates[i].string], keyAudioBitrateName,
142 [NSNumber numberWithInt: rate], keyAudioBitrate,
144 [masterBitRateArray addObject: dict];
152 // Ensure the list of codecs is accurate
153 // Update the current value of codec based on the revised list
154 - (void) updateCodecs
157 NSMutableArray *permittedCodecs = [NSMutableArray array];
158 unsigned int count = [masterCodecArray count];
160 NSString *keyThatAllows = nil;
162 // Determine which key we use to see which codecs are permitted
163 switch ([videoContainerTag intValue]) {
165 keyThatAllows = keyAudioMP4;
168 keyThatAllows = keyAudioMKV;
171 keyThatAllows = @"error condition";
175 // First get a list of the permitted codecs based on the internal rules
176 if (nil != track && YES == [self enabled]) {
179 for (unsigned int i = 0; i < count; i++) {
180 dict = [masterCodecArray objectAtIndex: i];
182 // First make sure only codecs permitted by the container are here
183 goodToAdd = [[dict objectForKey: keyThatAllows] boolValue];
185 // Now we make sure if DTS or AC3 is not available in the track it is not put in the codec list, but in a general way
186 if (YES == [[dict objectForKey: keyAudioMustMatchTrack] boolValue]) {
187 if ([[dict objectForKey: keyAudioMustMatchTrack] intValue] != [[[self track] objectForKey: keyAudioInputCodec] intValue]) {
192 if (YES == goodToAdd) {
193 [permittedCodecs addObject: dict];
198 // Now make sure the permitted list and the actual ones matches
199 [self setCodecs: permittedCodecs];
201 // Ensure our codec is on the list of permitted codecs
202 if (nil == [self codec] || NO == [permittedCodecs containsObject: [self codec]]) {
203 if (0 < [permittedCodecs count]) {
204 [self setCodec: [permittedCodecs objectAtIndex: 0]]; // This should be defaulting to Core Audio
207 [self setCodec: nil];
214 // The rules here are provided as-is from the original -[Controller audioTrackPopUpChanged:mixdownToUse:] routine
215 // with the comments taken from there as well.
216 - (void) updateMixdowns
219 NSMutableArray *retval = [NSMutableArray array];
220 int trackCodec = [[track objectForKey: keyAudioInputCodec] intValue];
221 int codecCodec = [[codec objectForKey: keyAudioCodec] intValue];
223 if (HB_ACODEC_AC3 == trackCodec && HB_ACODEC_AC3_PASS == codecCodec) {
224 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
225 NSLocalizedString(@"AC3 Passthru", @"AC3 Passthru"), keyAudioMixdownName,
226 [NSNumber numberWithInt: HB_ACODEC_AC3_PASS], keyAudioMixdown,
227 [NSNumber numberWithBool: YES], keyAudioMixdownLimitsToTrackBitRate,
228 [NSNumber numberWithBool: YES], keyAudioMixdownCanBeDefault,
231 else if (HB_ACODEC_DCA == trackCodec && HB_ACODEC_DCA_PASS == codecCodec) {
232 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
233 NSLocalizedString(@"DTS Passthru", @"DTS Passthru"), keyAudioMixdownName,
234 [NSNumber numberWithInt: HB_ACODEC_DCA_PASS], keyAudioMixdown,
235 [NSNumber numberWithBool: YES], keyAudioMixdownLimitsToTrackBitRate,
236 [NSNumber numberWithBool: YES], keyAudioMixdownCanBeDefault,
240 int audioCodecsSupport6Ch = (trackCodec && HB_ACODEC_LAME != codecCodec);
241 int channelLayout = [[track objectForKey: keyAudioInputChannelLayout] intValue];
242 int layout = channelLayout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
244 /* add a mono option? */
245 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
246 [NSString stringWithUTF8String: hb_audio_mixdowns[0].human_readable_name], keyAudioMixdownName,
247 [NSNumber numberWithInt: hb_audio_mixdowns[0].amixdown], keyAudioMixdown,
248 [NSNumber numberWithBool: NO], keyAudioMixdownLimitsToTrackBitRate,
249 [NSNumber numberWithBool: YES], keyAudioMixdownCanBeDefault,
252 /* offer stereo if we have a stereo-or-better source */
253 if (layout >= HB_INPUT_CH_LAYOUT_STEREO) {
254 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
255 [NSString stringWithUTF8String: hb_audio_mixdowns[1].human_readable_name], keyAudioMixdownName,
256 [NSNumber numberWithInt: hb_audio_mixdowns[1].amixdown], keyAudioMixdown,
257 [NSNumber numberWithBool: NO], keyAudioMixdownLimitsToTrackBitRate,
258 [NSNumber numberWithBool: YES], keyAudioMixdownCanBeDefault,
262 /* do we want to add a dolby surround (DPL1) option? */
263 if (HB_INPUT_CH_LAYOUT_3F1R == layout || HB_INPUT_CH_LAYOUT_3F2R == layout || HB_INPUT_CH_LAYOUT_DOLBY == layout) {
264 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
265 [NSString stringWithUTF8String: hb_audio_mixdowns[2].human_readable_name], keyAudioMixdownName,
266 [NSNumber numberWithInt: hb_audio_mixdowns[2].amixdown], keyAudioMixdown,
267 [NSNumber numberWithBool: NO], keyAudioMixdownLimitsToTrackBitRate,
268 [NSNumber numberWithBool: YES], keyAudioMixdownCanBeDefault,
272 /* do we want to add a dolby pro logic 2 (DPL2) option? */
273 if (HB_INPUT_CH_LAYOUT_3F2R == layout) {
274 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
275 [NSString stringWithUTF8String: hb_audio_mixdowns[3].human_readable_name], keyAudioMixdownName,
276 [NSNumber numberWithInt: hb_audio_mixdowns[3].amixdown], keyAudioMixdown,
277 [NSNumber numberWithBool: NO], keyAudioMixdownLimitsToTrackBitRate,
278 [NSNumber numberWithBool: YES], keyAudioMixdownCanBeDefault,
282 /* do we want to add a 6-channel discrete option? */
283 if (1 == audioCodecsSupport6Ch && HB_INPUT_CH_LAYOUT_3F2R == layout && (channelLayout & HB_INPUT_CH_LAYOUT_HAS_LFE)) {
284 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
285 [NSString stringWithUTF8String: hb_audio_mixdowns[4].human_readable_name], keyAudioMixdownName,
286 [NSNumber numberWithInt: hb_audio_mixdowns[4].amixdown], keyAudioMixdown,
287 [NSNumber numberWithBool: NO], keyAudioMixdownLimitsToTrackBitRate,
288 [NSNumber numberWithBool: (HB_ACODEC_AC3 == codecCodec) ? YES : NO], keyAudioMixdownCanBeDefault,
292 // based on the fact that we are in an else section where the ifs before hand would have detected the following two
293 // situations, the following code will never add anything to the returned array. I am leaving this in place for
294 // historical reasons.
295 /* do we want to add an AC-3 passthrough option? */
296 if (HB_ACODEC_AC3 == trackCodec && HB_ACODEC_AC3_PASS == codecCodec) {
297 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
298 [NSString stringWithUTF8String: hb_audio_mixdowns[5].human_readable_name], keyAudioMixdownName,
299 [NSNumber numberWithInt: HB_ACODEC_AC3_PASS], keyAudioMixdown,
300 [NSNumber numberWithBool: YES], keyAudioMixdownLimitsToTrackBitRate,
301 [NSNumber numberWithBool: YES], keyAudioMixdownCanBeDefault,
305 /* do we want to add a DTS Passthru option ? HB_ACODEC_DCA*/
306 if (HB_ACODEC_DCA == trackCodec && HB_ACODEC_DCA_PASS == codecCodec) {
307 [retval addObject: [NSDictionary dictionaryWithObjectsAndKeys:
308 [NSString stringWithUTF8String: hb_audio_mixdowns[5].human_readable_name], keyAudioMixdownName,
309 [NSNumber numberWithInt: HB_ACODEC_DCA_PASS], keyAudioMixdown,
310 [NSNumber numberWithBool: YES], keyAudioMixdownLimitsToTrackBitRate,
311 [NSNumber numberWithBool: YES], keyAudioMixdownCanBeDefault,
316 // Now make sure the permitted list and the actual ones matches
317 [self setMixdowns: retval];
319 // Ensure our mixdown is on the list of permitted ones
320 if (YES == [[NSUserDefaults standardUserDefaults] boolForKey: @"CodecDefaultsMixdown"] ||
321 nil == [self mixdown] || NO == [retval containsObject: [self mixdown]]) {
322 [self setMixdown: [retval lastDictionaryWithObject: [NSNumber numberWithBool: YES] matchingKey: keyAudioMixdownCanBeDefault]];
328 - (void) updateBitRates
331 NSMutableArray *permittedBitRates = [NSMutableArray array];
335 count = [masterBitRateArray count];
338 NSString *defaultBitRate;
340 int trackInputBitRate = [[[self track] objectForKey: keyAudioInputBitrate] intValue];
341 BOOL limitsToTrackInputBitRate = [[[self mixdown] objectForKey: keyAudioMixdownLimitsToTrackBitRate] boolValue];
343 int theSampleRate = [[[self sampleRate] objectForKey: keyAudioSamplerate] intValue];
345 if (0 == theSampleRate) { // this means Auto
346 theSampleRate = [[[self track] objectForKey: keyAudioInputSampleRate] intValue];
349 int ourCodec = [[codec objectForKey: keyAudioCodec] intValue];
350 int ourMixdown = [[[self mixdown] objectForKey: keyAudioMixdown] intValue];
351 hb_get_audio_bitrate_limits(ourCodec, theSampleRate, ourMixdown, &minBitRate, &maxBitRate);
352 int theDefaultBitRate = hb_get_default_audio_bitrate(ourCodec, theSampleRate, ourMixdown);
353 defaultBitRate = [NSString stringWithFormat: @"%d", theDefaultBitRate];
355 for (unsigned int i = 0; i < count; i++) {
356 dict = [masterBitRateArray objectAtIndex: i];
357 currentBitRate = [[dict objectForKey: keyAudioBitrate] intValue];
359 // First ensure the bitrate falls within range of the codec
360 shouldAdd = (currentBitRate >= minBitRate && currentBitRate <= maxBitRate);
362 // Now make sure the mixdown is not limiting us to the track input bitrate
363 if (YES == shouldAdd && YES == limitsToTrackInputBitRate) {
364 if (currentBitRate != trackInputBitRate) {
369 if (YES == shouldAdd) {
370 [permittedBitRates addObject: dict];
374 // There is a situation where we have a mixdown requirement to match the track input bit rate,
375 // but it does not fall into the range the codec supports. Therefore, we force it here.
376 if (YES == limitsToTrackInputBitRate && 0 == [permittedBitRates count]) {
377 NSDictionary *missingBitRate = [masterBitRateArray dictionaryWithObject: [NSNumber numberWithInt: trackInputBitRate] matchingKey: keyAudioBitrate];
378 if (nil == missingBitRate) {
379 // We are in an even worse situation where the requested bit rate does not even exist in the underlying
380 // library of supported bitrates. Of course since this value is ignored we can freely make a bogus one
381 // for the UI just to make the user a little more aware.
382 missingBitRate = [NSDictionary dictionaryWithObjectsAndKeys:
383 [NSString stringWithFormat: @"%d", trackInputBitRate], keyAudioBitrateName,
384 [NSNumber numberWithInt: trackInputBitRate], keyAudioBitrate,
387 [permittedBitRates addObject: missingBitRate];
390 // Make sure we are updated with the permitted list
391 [self setBitRates: permittedBitRates];
393 // Select the proper one
394 [self setBitRateFromName: defaultBitRate];
396 if (nil == [self bitRate] || NO == [permittedBitRates containsObject: [self bitRate]]) {
397 [self setBitRate: [permittedBitRates lastObject]];
404 #pragma mark Accessors
409 @synthesize sampleRate;
412 @synthesize videoContainerTag;
413 @synthesize controller;
416 @synthesize mixdowns;
417 @synthesize bitRates;
419 - (void) setVideoContainerTag: (NSNumber *) aValue
422 if ((nil != aValue || nil != videoContainerTag) && NO == [aValue isEqual: videoContainerTag]) {
424 [videoContainerTag release];
425 videoContainerTag = aValue;
431 // We do some detection of the None track to do special things.
432 - (void) setTrack: (NSDictionary *) aValue
435 if ((nil != aValue || nil != track) && NO == [aValue isEqual: track]) {
436 BOOL switchingFromNone = [track isEqual: [controller noneTrack]];
437 BOOL switchingToNone = [aValue isEqual: [controller noneTrack]];
445 if (YES == [self enabled]) {
446 [self setSampleRate: [[self sampleRates] objectAtIndex: 0]]; // default to Auto
448 if (YES == switchingFromNone) {
449 [controller switchingTrackFromNone: self];
451 if (YES == switchingToNone) {
452 [controller settingTrackToNone: self];
459 - (void) setCodec: (NSDictionary *) aValue
462 if ((nil != aValue || nil != codec) && NO == [aValue isEqual: codec]) {
466 [self updateMixdowns];
467 [self updateBitRates];
472 - (void) setMixdown: (NSDictionary *) aValue
475 if ((nil != aValue || nil != mixdown) && NO == [aValue isEqual: mixdown]) {
479 [self updateBitRates];
480 [[NSNotificationCenter defaultCenter] postNotificationName: HBMixdownChangedNotification object: self];
485 - (NSArray *) tracks { return [controller masterTrackArray]; }
487 - (NSArray *) sampleRates { return masterSampleRateArray; }
492 [self setTrack: nil];
493 [self setCodec: nil];
494 [self setMixdown: nil];
495 [self setSampleRate: nil];
496 [self setBitRate: nil];
498 [self setVideoContainerTag: nil];
499 [self setCodecs: nil];
500 [self setMixdowns: nil];
501 [self setBitRates: nil];
507 #pragma mark Special Setters
509 - (void) setTrackFromIndex: (int) aValue
512 [self setTrack: [[self tracks] dictionaryWithObject: [NSNumber numberWithInt: aValue] matchingKey: keyAudioTrackIndex]];
516 // This returns whether it is able to set the actual codec desired.
517 - (BOOL) setCodecFromName: (NSString *) aValue
520 NSDictionary *dict = [[self codecs] dictionaryWithObject: aValue matchingKey: keyAudioCodecName];
523 [self setCodec: dict];
525 return (nil != dict);
528 - (void) setMixdownFromName: (NSString *) aValue
531 NSDictionary *dict = [[self mixdowns] dictionaryWithObject: aValue matchingKey: keyAudioMixdownName];
534 [self setMixdown: dict];
539 - (void) setSampleRateFromName: (NSString *) aValue
542 NSDictionary *dict = [[self sampleRates] dictionaryWithObject: aValue matchingKey: keyAudioSampleRateName];
545 [self setSampleRate: dict];
550 - (void) setBitRateFromName: (NSString *) aValue
553 NSDictionary *dict = [[self bitRates] dictionaryWithObject: aValue matchingKey: keyAudioBitrateName];
556 [self setBitRate: dict];
563 #pragma mark Validation
565 // Because we have indicated that the binding for the drc validates immediately we can implement the
566 // key value binding method to ensure the drc stays in our accepted range.
567 - (BOOL) validateDrc: (id *) ioValue error: (NSError *) outError
572 if (nil != *ioValue) {
573 if (0.0 < [*ioValue floatValue] && 1.0 > [*ioValue floatValue]) {
574 *ioValue = [NSNumber numberWithFloat: 1.0];
582 #pragma mark Bindings Support
587 return (nil != track) ? (NO == [track isEqual: [controller noneTrack]]) : NO;
590 - (BOOL) mixdownEnabled
593 BOOL retval = [self enabled];
596 int myMixdown = [[[self mixdown] objectForKey: keyAudioMixdown] intValue];
597 if (HB_ACODEC_AC3_PASS == myMixdown || HB_ACODEC_DCA_PASS == myMixdown) {
607 BOOL retval = [self enabled];
610 int myTrackCodec = [[[self track] objectForKey: keyAudioInputCodec] intValue];
611 if (HB_ACODEC_AC3 != myTrackCodec) {
618 + (NSSet *) keyPathsForValuesAffectingEnabled
621 return [NSSet setWithObjects: @"track", nil];
624 + (NSSet *) keyPathsForValuesAffectingMixdownEnabled
627 return [NSSet setWithObjects: @"track", @"mixdown", nil];
630 + (NSSet *) keyPathsForValuesAffectingAC3Enabled
633 return [NSSet setWithObjects: @"track", nil];