summaryrefslogtreecommitdiffstats
path: root/macosx/HBAudioTrack.m
blob: 1c998cc4f3a051e20b29f526711bd40d255fb14a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*  HBAudioTrack.m $

 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HBAudioTrack.h"
#import "HBAudioController.h"
#import "HBJob.h"
#import "HBCodingUtilities.h"
#import "hb.h"

NSString *keyAudioTrackIndex = @"keyAudioTrackIndex";
NSString *keyAudioTrackName = @"keyAudioTrackName";
NSString *keyAudioInputBitrate = @"keyAudioInputBitrate";
NSString *keyAudioInputSampleRate = @"keyAudioInputSampleRate";
NSString *keyAudioInputCodec = @"keyAudioInputCodec";
NSString *keyAudioInputCodecParam = @"keyAudioInputCodecParam";
NSString *keyAudioInputChannelLayout = @"keyAudioInputChannelLayout";
NSString *keyAudioTrackLanguageIsoCode = @"keyAudioTrackLanguageIsoCode";

NSString *keyAudioCodecName = @"keyAudioCodecName";
NSString *keyAudioSupportedMuxers = @"keyAudioSupportedMuxers";
NSString *keyAudioSampleRateName = @"keyAudioSampleRateName";
NSString *keyAudioBitrateName = @"keyAudioBitrateName";
NSString *keyAudioMustMatchTrack = @"keyAudioMustMatchTrack";
NSString *keyAudioMixdownName = @"keyAudioMixdownName";

NSString *keyAudioCodec = @"codec";
NSString *keyAudioMixdown = @"mixdown";
NSString *keyAudioSamplerate = @"samplerate";
NSString *keyAudioBitrate = @"bitrate";

static NSMutableArray *masterCodecArray = nil;
static NSMutableArray *masterMixdownArray = nil;
static NSMutableArray *masterBitRateArray = nil;

@interface NSArray (HBAudioSupport)
- (NSDictionary *) dictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey;
- (NSDictionary *) lastDictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey;
@end
@implementation NSArray (HBAudioSupport)
- (NSDictionary *) dictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey reverse: (BOOL) reverse
{
    NSDictionary *retval = nil;
    NSEnumerator *enumerator = reverse ? [self reverseObjectEnumerator] : [self objectEnumerator];
    NSDictionary *dict;
    id aValue;

    while (nil != (dict = [enumerator nextObject]) && !retval)
    {
        if (nil != (aValue = dict[aKey]) && [aValue isEqual: anObject])
        {
            retval = dict;
        }
    }
    return retval;
}
- (NSDictionary *) dictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey
{
    return [self dictionaryWithObject: anObject matchingKey: aKey reverse: NO];
}
- (NSDictionary *) lastDictionaryWithObject: (id) anObject matchingKey: (NSString *) aKey
{
    return [self dictionaryWithObject: anObject matchingKey: aKey reverse: YES];
}

@end

@interface HBAudioTrack ()

@property (nonatomic, readwrite) NSArray *codecs;
@property (nonatomic, readwrite) NSArray *mixdowns;
@property (nonatomic, readwrite) NSArray *bitRates;

@end

@implementation HBAudioTrack

#pragma mark -
#pragma mark Object Setup

+ (void)initialize
{
    if ([HBAudioTrack class] == self)
    {
        masterCodecArray = [[NSMutableArray alloc] init]; // knowingly leaked
        for (const hb_encoder_t *audio_encoder = hb_audio_encoder_get_next(NULL);
             audio_encoder != NULL;
             audio_encoder  = hb_audio_encoder_get_next(audio_encoder))
        {
            id audioMustMatchTrack;
            if ((audio_encoder->codec &  HB_ACODEC_PASS_FLAG) &&
                (audio_encoder->codec != HB_ACODEC_AUTO_PASS))
            {
                audioMustMatchTrack = @(audio_encoder->codec &
                                                      ~HB_ACODEC_PASS_FLAG);
            }
            else
            {
                audioMustMatchTrack = @NO;
            }
            [masterCodecArray addObject:@{keyAudioCodecName:       @(audio_encoder->name),
                                          keyAudioCodec:           @(audio_encoder->codec),
                                          keyAudioSupportedMuxers: @(audio_encoder->muxers),
                                          keyAudioMustMatchTrack:    audioMustMatchTrack}];
        }

        masterMixdownArray = [[NSMutableArray alloc] init]; // knowingly leaked
        for (const hb_mixdown_t *mixdown = hb_mixdown_get_next(NULL);
             mixdown != NULL;
             mixdown  = hb_mixdown_get_next(mixdown))
        {
            [masterMixdownArray addObject:@{keyAudioMixdownName: @(mixdown->name),
                                            keyAudioMixdown:     @(mixdown->amixdown)}];
        }

        masterBitRateArray = [[NSMutableArray alloc] init]; // knowingly leaked
        for (const hb_rate_t *audio_bitrate = hb_audio_bitrate_get_next(NULL);
             audio_bitrate != NULL;
             audio_bitrate  = hb_audio_bitrate_get_next(audio_bitrate))
        {
            [masterBitRateArray addObject:@{keyAudioBitrateName: @(audio_bitrate->name),
                                            keyAudioBitrate:     @(audio_bitrate->rate)}];
        }
    }
}

// Ensure the list of codecs is accurate
// Update the current value of codec based on the revised list
- (void) updateCodecs
{
    NSMutableArray *permittedCodecs = [NSMutableArray array];
    NSUInteger count = [masterCodecArray count];
    NSDictionary *dict;

    // First get a list of the permitted codecs based on the internal rules
    if (nil != self.track && self.enabled)
    {
        BOOL goodToAdd;

        for (unsigned int i = 0; i < count; i++)
        {
            dict = masterCodecArray[i];

            // First make sure only codecs permitted by the container are here
            goodToAdd = !!([dict[keyAudioSupportedMuxers] intValue] & self.container);

            // 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
            if ([dict[keyAudioMustMatchTrack] boolValue])
            {
                if ([dict[keyAudioMustMatchTrack] intValue] != [self.track[keyAudioInputCodec] intValue])
                {
                    goodToAdd = NO;
                }
            }

            if (goodToAdd)
            {
                [permittedCodecs addObject: dict];
            }
        }
    }

    // Now make sure the permitted list and the actual ones matches
    [self setCodecs: permittedCodecs];

    // Ensure our codec is on the list of permitted codecs
    if (!self.codec || ![permittedCodecs containsObject: self.codec])
    {
        if (0 < [permittedCodecs count])
        {
            self.codec = permittedCodecs[0]; // This should be defaulting to Core Audio
        }
        else
        {
            self.codec = nil;
        }
    }
}

- (void)updateMixdowns:(BOOL)shouldSetDefault
{
    NSMutableArray *permittedMixdowns = [NSMutableArray array];
    NSDictionary *dict;
    int currentMixdown;

    unsigned long long channelLayout = [self.track[keyAudioInputChannelLayout] unsignedLongLongValue];
    NSUInteger count                 = [masterMixdownArray count];
    int codecCodec                   = [self.codec[keyAudioCodec] intValue];
    int theDefaultMixdown            = hb_mixdown_get_default(codecCodec, channelLayout);

    for (unsigned int i = 0; i < count; i++)
    {
        dict = masterMixdownArray[i];
        currentMixdown = [dict[keyAudioMixdown] intValue];

        if (hb_mixdown_is_supported(currentMixdown, codecCodec, channelLayout))
        {
            [permittedMixdowns addObject: dict];
        }
    }

    if (!self.enabled)
    {
        permittedMixdowns = nil;
    }

    // Now make sure the permitted list and the actual ones matches
    self.mixdowns = permittedMixdowns;

    // Select the proper one
    if (shouldSetDefault)
    {
        self.mixdown = [permittedMixdowns dictionaryWithObject: @(theDefaultMixdown)
                                                      matchingKey: keyAudioMixdown];
    }

    if (!self.mixdown || ![permittedMixdowns containsObject: self.mixdown])
    {
        self.mixdown = [permittedMixdowns lastObject];
    }
}

- (void)validateSamplerate
{
    int codec      = [self.codec[keyAudioCodec] intValue];
    int samplerate = [self.sampleRate[keyAudioSamplerate] intValue];

    if (codec & HB_ACODEC_PASS_FLAG)
    {
        [self setSampleRateFromName:@"Auto"];
    }
    else if (samplerate)
    {
        samplerate = hb_audio_samplerate_find_closest(samplerate, codec);
        [self setSampleRateFromName:@(hb_audio_samplerate_get_name(samplerate))];
    }
}

- (void)updateBitRates:(BOOL)shouldSetDefault
{
    NSMutableArray *permittedBitRates = [NSMutableArray array];
    NSDictionary *dict;
    int minBitRate;
    int maxBitRate;
    int currentBitRate;
    BOOL shouldAdd;

    NSUInteger count = [masterBitRateArray count];
    int trackInputBitRate = [self.track[keyAudioInputBitrate] intValue];
    int theSampleRate = [self.sampleRate[keyAudioSamplerate] intValue];

    if (0 == theSampleRate) // this means Auto
    {
        theSampleRate = [self.track[keyAudioInputSampleRate] intValue];
    }

    int ourCodec          = [self.codec[keyAudioCodec] intValue];
    int ourMixdown        = [self.mixdown[keyAudioMixdown] intValue];
    int theDefaultBitRate = hb_audio_bitrate_get_default(ourCodec, theSampleRate, ourMixdown);
    hb_audio_bitrate_get_limits(ourCodec, theSampleRate, ourMixdown, &minBitRate, &maxBitRate);

    BOOL codecIsPassthru = ([self.codec[keyAudioCodec] intValue] & HB_ACODEC_PASS_FLAG) ? YES : NO;
    BOOL codecIsLossless = (theDefaultBitRate == -1) ? YES : NO;

    if (codecIsPassthru)
    {
        NSDictionary *sourceBitRate = [masterBitRateArray dictionaryWithObject: @(trackInputBitRate)
                                                                   matchingKey: keyAudioBitrate];
        if (!sourceBitRate)
        {
            // the source bitrate isn't in the master array - create it
            sourceBitRate = @{keyAudioBitrateName: [NSString stringWithFormat: @"%d", trackInputBitRate],
                              keyAudioBitrate: @(trackInputBitRate)};
        }
        [permittedBitRates addObject: sourceBitRate];
    }
    else if (codecIsLossless)
    {
        NSDictionary *bitRateNotApplicable = @{keyAudioBitrateName: @"N/A",
                                               keyAudioBitrate: @-1};
        [permittedBitRates addObject: bitRateNotApplicable];
    }
    else
    {
        for (unsigned int i = 0; i < count; i++)
        {
            dict = masterBitRateArray[i];
            currentBitRate = [dict[keyAudioBitrate] intValue];

            // First ensure the bitrate falls within range of the codec
            shouldAdd = (currentBitRate >= minBitRate && currentBitRate <= maxBitRate);

            if (shouldAdd)
            {
                [permittedBitRates addObject: dict];
            }
        }
    }

    if (!self.enabled)
    {
        permittedBitRates = nil;
    }

    // Make sure we are updated with the permitted list
    self.bitRates = permittedBitRates;

    // Select the proper one
    if (shouldSetDefault)
    {
        [self setBitRateFromName: [NSString stringWithFormat:@"%d", theDefaultBitRate]];
    }

    if (!self.bitRate || ![permittedBitRates containsObject: self.bitRate])
    {
        self.bitRate = [permittedBitRates lastObject];
    }
}

#pragma mark -
#pragma mark Accessors

- (NSArray *)sampleRates
{
    NSMutableArray *samplerates = [[NSMutableArray alloc] init];

    /*
     * Note that for the Auto value we use 0 for the sample rate because our controller will give back the track's
     * input sample rate when it finds this 0 value as the selected sample rate.  We do this because the input
     * sample rate depends on the track, which means it depends on the title, so cannot be nicely set up here.
     */
    [samplerates addObject:@{keyAudioSampleRateName: @"Auto",
                             keyAudioSamplerate:     @0}];

    int codec = [self.codec[keyAudioCodec] intValue];
    for (const hb_rate_t *audio_samplerate = hb_audio_samplerate_get_next(NULL);
         audio_samplerate != NULL;
         audio_samplerate  = hb_audio_samplerate_get_next(audio_samplerate))
    {
        int rate = audio_samplerate->rate;
        if (rate == hb_audio_samplerate_find_closest(rate, codec))
        {
            [samplerates addObject:@{keyAudioSampleRateName: @(audio_samplerate->name),
                                     keyAudioSamplerate:     @(rate)}];
        }
    }
    return samplerates;
}

#pragma mark -
#pragma mark Setters

- (void)setContainer:(int)container
{
    if (container != _container)
    {
        [[self.undo prepareWithInvocationTarget:self] setContainer:_container];
    }
    _container = container;

    if (!(self.undo.isUndoing || self.undo.isRedoing))
    {
        [self updateCodecs];
    }
}

- (void)setTrack:(NSDictionary *)track
{
    if (track != _track)
    {
        [[self.undo prepareWithInvocationTarget:self] setTrack:_track];
    }
    NSDictionary *oldValue = _track;
    _track = track;
    if (nil != _track && !(self.undo.isUndoing || self.undo.isRedoing))
    {
        [self updateCodecs];
        [self updateMixdowns: YES];
        if (self.enabled)
        {
            self.sampleRate = self.sampleRates[0]; // default to Auto
        }
        if ([self.dataSource.noneTrack isEqual: oldValue])
        {
            [self.delegate switchingTrackFromNone: self];
        }
        if ([self.dataSource.noneTrack isEqual: self.track])
        {
            [self.delegate settingTrackToNone: self];
        }
    }
}

- (void)setCodec:(NSDictionary *)codec
{
    if (codec != _codec)
    {
        [[self.undo prepareWithInvocationTarget:self] setCodec:_codec];
    }
    _codec = codec;

    if (!(self.undo.isUndoing || self.undo.isRedoing))
    {
        [self validateSamplerate];
        [self updateMixdowns:YES];
        [self updateBitRates:YES];
    }
}

- (void)setMixdown:(NSDictionary *)mixdown
{
    if (mixdown != _mixdown)
    {
        [[self.undo prepareWithInvocationTarget:self] setMixdown:_mixdown];
    }
    _mixdown = mixdown;

    if (!(self.undo.isUndoing || self.undo.isRedoing))
    {
        [self updateBitRates:YES];
        [self.delegate mixdownChanged];
    }
}

- (void)setSampleRate:(NSDictionary *)sampleRate
{
    if (sampleRate != _sampleRate)
    {
        [[self.undo prepareWithInvocationTarget:self] setSampleRate:_sampleRate];
    }
    _sampleRate = sampleRate;

    if (!(self.undo.isUndoing || self.undo.isRedoing))
    {
        [self updateBitRates: NO];
    }
}

- (void)setBitRate:(NSDictionary *)bitRate
{
    if (bitRate != _bitRate)
    {
        [[self.undo prepareWithInvocationTarget:self] setBitRate:_bitRate];
    }
    _bitRate = bitRate;
}

- (void)setDrc:(double)drc
{
    if (drc != _drc)
    {
        [[self.undo prepareWithInvocationTarget:self] setDrc:_drc];
    }
    _drc = drc;
}

- (void)setGain:(double)gain
{
    if (gain != _gain)
    {
        [[self.undo prepareWithInvocationTarget:self] setGain:_gain];
    }
    _gain = gain;
}

#pragma mark -
#pragma mark Special Setters

- (void)setTrackFromIndex:(int)aValue
{
    self.track = [self.dataSource.masterTrackArray dictionaryWithObject: @(aValue)
                                                            matchingKey: keyAudioTrackIndex];
}

// This returns whether it is able to set the actual codec desired.
- (BOOL)setCodecFromName:(NSString *)aValue
{
    NSDictionary *dict = [self.codecs dictionaryWithObject: aValue matchingKey: keyAudioCodecName];
    if (nil != dict)
    {
        self.codec = dict;
    }
    return (nil != dict);
}

- (void)setMixdownFromName:(NSString *)aValue
{
    NSDictionary *dict = [self.mixdowns dictionaryWithObject: aValue matchingKey: keyAudioMixdownName];
    if (nil != dict)
    {
        self.mixdown = dict;
    }
}

- (void)setSampleRateFromName:(NSString *)aValue
{
    NSDictionary *dict = [self.sampleRates dictionaryWithObject: aValue matchingKey: keyAudioSampleRateName];
    if (nil != dict)
    {
        self.sampleRate = dict;
    }
}

- (void)setBitRateFromName:(NSString *)aValue
{
    NSDictionary *dict = [self.bitRates dictionaryWithObject: aValue matchingKey: keyAudioBitrateName];
    if (nil != dict)
    {
        self.bitRate = dict;
    }
}

- (void)setCodecs:(NSArray *)codecs
{
    if (codecs != _codecs)
    {
        [[self.undo prepareWithInvocationTarget:self] setCodecs:_codecs];
    }
    _codecs = codecs;
}

- (void)setMixdowns:(NSArray *)mixdowns
{
    if (mixdowns != _mixdowns)
    {
        [[self.undo prepareWithInvocationTarget:self] setMixdowns:_mixdowns];
    }
    _mixdowns = mixdowns;
}

- (void)setBitRates:(NSArray *)bitRates
{
    if (bitRates != _bitRates)
    {
        [[self.undo prepareWithInvocationTarget:self] setBitRates:_bitRates];
    }
    _bitRates = bitRates;
}

#pragma mark -
#pragma mark Validation

// Because we have indicated that the binding for the gain validates immediately we can implement the
// key value binding method to ensure the gain stays in our accepted range.
- (BOOL)validateGain:(id *)ioValue error:(NSError * __autoreleasing *)outError
{
    BOOL retval = YES;

    if (nil != *ioValue)
    {
        if ([*ioValue intValue] < -20)
        {
            *ioValue = @(-20);
        }
        else if ([*ioValue intValue] > 20)
        {
            *ioValue = @20;
        }
    }

    return retval;
}

#pragma mark - Bindings Support

- (NSArray *)masterTrackArray
{
    return self.dataSource.masterTrackArray;
}

- (BOOL)enabled
{
    return (nil != self.track) ? (![self.track isEqual: self.dataSource.noneTrack]) : NO;
}

- (BOOL)mixdownEnabled
{
    BOOL retval = self.enabled;

    if (retval)
    {
        int myMixdown = [self.mixdown[keyAudioMixdown] intValue];
        if (myMixdown == HB_AMIXDOWN_NONE)
        {
            // "None" mixdown (passthru)
            retval = NO;
        }
    }
    return retval;
}

- (BOOL)bitrateEnabled
{
    BOOL retval = self.enabled;

    if (retval)
    {
        int myCodecCodec          = [self.codec[keyAudioCodec] intValue];
        int myCodecDefaultBitrate = hb_audio_bitrate_get_default(myCodecCodec, 0, 0);
        if (myCodecDefaultBitrate < 0)
        {
            retval = NO;
        }
    }
    return retval;
}

- (BOOL)DRCEnabled
{
    BOOL retval = self.enabled;

    if (retval)
    {
        int myTrackParam = [self.track[keyAudioInputCodecParam] intValue];
        int myTrackCodec = [self.track[keyAudioInputCodec] intValue];
        int myCodecCodec = [self.codec[keyAudioCodec] intValue];
        if (!hb_audio_can_apply_drc(myTrackCodec, myTrackParam, myCodecCodec))
        {
            retval = NO;
        }
    }
    return retval;
}

- (BOOL)PassThruDisabled
{
    BOOL retval = self.enabled;

    if (retval)
    {
        int myCodecCodec = [self.codec[keyAudioCodec] intValue];
        if (myCodecCodec & HB_ACODEC_PASS_FLAG)
        {
            retval = NO;
        }
    }
    return retval;
}

- (void)setNilValueForKey:(NSString *)key
{
    if ([key isEqualToString:@"drc"] || [key isEqualToString:@"gain"])
    {
        [self setValue:@0 forKey:key];
    }
}

+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
    NSSet *retval = nil;

    if ([key isEqualToString:@"enabled"])
    {
        retval = [NSSet setWithObjects:@"track", nil];
    }
    else if ([key isEqualToString:@"PassThruDisabled"])
    {
        retval = [NSSet setWithObjects:@"track", @"codec", nil];
    }
    else if ([key isEqualToString:@"DRCEnabled"])
    {
        retval = [NSSet setWithObjects:@"track", @"codec", nil];
    }
    else if ([key isEqualToString:@"bitrateEnabled"])
    {
        retval = [NSSet setWithObjects:@"track", @"codec", nil];
    }
    else if ([key isEqualToString:@"mixdownEnabled"])
    {
        retval = [NSSet setWithObjects:@"track", @"mixdown", nil];
    }
    else
    {
        retval = [NSSet set];
    }

    return retval;
}

#pragma mark - NSCopying

- (instancetype)copyWithZone:(NSZone *)zone
{
    HBAudioTrack *copy = [[[self class] alloc] init];

    if (copy)
    {
        copy->_track = [_track copy];
        copy->_codec = [_codec copy];
        copy->_mixdown = [_mixdown copy];
        copy->_sampleRate = [_sampleRate copy];
        copy->_bitRate = [_bitRate copy];
        copy->_drc = _drc;
        copy->_gain = _gain;
        copy->_container = _container;

        copy->_codecs = [_codecs copy];
        copy->_mixdowns = [_mixdowns copy];
        copy->_bitRates = [_bitRates copy];
    }

    return copy;
}

#pragma mark - NSCoding

+ (BOOL)supportsSecureCoding
{
    return YES;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeInt:2 forKey:@"HBAudioTrackVersion"];

    encodeObject(_track);
    encodeObject(_codec);
    encodeObject(_mixdown);
    encodeObject(_sampleRate);
    encodeObject(_bitRate);
    encodeDouble(_drc);
    encodeDouble(_gain);
    encodeInt(_container);

    encodeObject(_codecs);
    encodeObject(_mixdowns);
    encodeObject(_bitRates);
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    self = [super init];

    decodeObject(_track, NSDictionary);
    decodeObject(_codec, NSDictionary);
    decodeObject(_mixdown, NSDictionary);
    decodeObject(_sampleRate, NSDictionary);
    decodeObject(_bitRate, NSDictionary);
    decodeDouble(_drc);
    decodeDouble(_gain);
    decodeInt(_container);

    decodeObject(_codecs, NSMutableArray);
    decodeObject(_mixdowns, NSMutableArray);
    decodeObject(_bitRates, NSArray);

    return self;
}

@end