summaryrefslogtreecommitdiffstats
path: root/macosx/HBAudioTrackPreset.m
blob: 9f91c3ab84c9014ef4956fe2fec790ec1c87d6f1 (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
//
//  HBAudioEncoder.m
//  HandBrake
//
//  Created by Damiano Galassi on 30/07/14.
//
//

#import "HBAudioTrackPreset.h"
#include "hb.h"

#define DEFAULT_SAMPLERATE 48000

static void *HBAudioEncoderContex = &HBAudioEncoderContex;

@implementation HBAudioTrackPreset

- (instancetype)init
{
    self = [super init];
    if (self) {
        // defaults settings
        _encoder = HB_ACODEC_CA_AAC;
        _sampleRate = 0;
        _bitRate = 160;
        _mixdown = HB_AMIXDOWN_DOLBYPLII;

        // add a serie of observers to keep the tracks properties in a valid state.
        [self addObserver:self forKeyPath:@"encoder" options:0 context:HBAudioEncoderContex];
        [self addObserver:self forKeyPath:@"mixdown" options:0 context:HBAudioEncoderContex];
        [self addObserver:self forKeyPath:@"sampleRate" options:0 context:HBAudioEncoderContex];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == HBAudioEncoderContex)
    {
        // Validate the settings
        if ([keyPath isEqualToString:@"encoder"])
        {
            [self validateMixdown];
            [self validateBitrate];
        }
        else if ([keyPath isEqualToString:@"mixdown"] || [keyPath isEqualToString:@"sampleRate"])
        {
            [self validateBitrate];
        }
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark -
#pragma mark Validation

/**
 *  Validates the mixdown property.
 */
- (void)validateMixdown
{
    if (!hb_mixdown_has_codec_support(self.mixdown, self.encoder))
    {
        self.mixdown = hb_mixdown_get_default(self.encoder, 0);
    }
}

- (void)validateBitrate
{
    int minBitRate = 0;
    int maxBitRate = 0;

    int sampleRate = self.sampleRate ? self.sampleRate : DEFAULT_SAMPLERATE;

    hb_audio_bitrate_get_limits(self.encoder, sampleRate, self.mixdown, &minBitRate, &maxBitRate);

    if (self.bitRate < minBitRate || self.bitRate > maxBitRate)
    {
        self.bitRate = maxBitRate;
    }
}

- (BOOL)mixdownEnabled
{
    BOOL retval = YES;

    if (self.mixdown == HB_AMIXDOWN_NONE)
    {
        // "None" mixdown (passthru)
        retval = NO;
    }

    return retval;
}

- (BOOL)bitrateEnabled
{
    BOOL retval = YES;

    int myCodecDefaultBitrate = hb_audio_bitrate_get_default(self.encoder, 0, 0);
    if (myCodecDefaultBitrate < 0)
    {
        retval = NO;
    }
    return retval;
}

- (BOOL)passThruDisabled
{
    BOOL retval = YES;

    if (self.encoder & HB_ACODEC_PASS_FLAG)
    {
        retval = NO;
    }

    return retval;
}

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

    if (nil != *ioValue)
    {
        if (0.0 < [*ioValue floatValue] && 1.0 > [*ioValue floatValue])
        {
            *ioValue = @(1.0);
        }
    }

    return retval;
}

// 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 *)outError
{
    BOOL retval = YES;

    if (nil != *ioValue)
    {
        if (0.0 < [*ioValue floatValue] && 1.0 > [*ioValue floatValue])
        {
            *ioValue = @(0.0);
        }
    }

    return retval;
}

- (void)dealloc
{
    // Remove the KVO observers before deallocing the instance.
    [self removeObserver:self forKeyPath:@"encoder"];
    [self removeObserver:self forKeyPath:@"mixdown"];
    [self removeObserver:self forKeyPath:@"sampleRate"];

    [super dealloc];
}

#pragma mark - Options

- (NSArray *)encoders
{
    NSMutableArray *encoders = [[NSMutableArray alloc] init];
    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))
    {
        [encoders addObject:@(audio_encoder->name)];
    }
    return [encoders autorelease];
}

- (NSArray *)mixdowns
{
    NSMutableArray *mixdowns = [[NSMutableArray alloc] init];
    for (const hb_mixdown_t *mixdown = hb_mixdown_get_next(NULL);
         mixdown != NULL;
         mixdown  = hb_mixdown_get_next(mixdown))
    {
        if (hb_mixdown_has_codec_support(mixdown->amixdown, self.encoder))
        {
            [mixdowns addObject:@(mixdown->name)];
        }
    }
    return [mixdowns autorelease];
}

- (NSArray *)samplerates
{
    NSMutableArray *samplerates = [[NSMutableArray alloc] init];
    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))
    {
        [samplerates addObject:@(audio_samplerate->name)];
    }
    return [samplerates autorelease];
}

- (NSArray *)bitrates
{
    int minBitRate = 0;
    int maxBitRate = 0;

    // If the samplerate is "Auto" pass a fake sampleRate to get the bitrates
    int sampleRate = self.sampleRate ? self.sampleRate : DEFAULT_SAMPLERATE;

    hb_audio_bitrate_get_limits(self.encoder, sampleRate, self.mixdown, &minBitRate, &maxBitRate);

    NSMutableArray *bitrates = [[NSMutableArray alloc] init];
    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))
    {
        if (audio_bitrate->rate >= minBitRate && audio_bitrate->rate <= maxBitRate)
        {
            [bitrates addObject:@(audio_bitrate->name)];
        }
    }
    return [bitrates autorelease];
}

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

    // Tell KVO to reaload the *enabled keyPaths
    // after a change to encoder.
    if ([key isEqualToString:@"bitrateEnabled"] ||
        [key isEqualToString:@"passThruDisabled"] ||
        [key isEqualToString:@"mixdownEnabled"])
    {
        retval = [NSSet setWithObjects:@"encoder", nil];
    }

    return retval;
}

@end

#pragma mark - Value Trasformers

@implementation HBEncoderTrasformer

+ (Class)transformedValueClass
{
    return [NSString class];
}

- (id)transformedValue:(id)value
{
    const char *name = hb_audio_encoder_get_name([value intValue]);
    if (name)
    {
        return @(name);
    }
    else
    {
        return nil;
    }
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)reverseTransformedValue:(id)value
{
    return @(hb_audio_encoder_get_from_name([value UTF8String]));
}

@end

@implementation HBMixdownTrasformer

+ (Class)transformedValueClass
{
    return [NSString class];
}

- (id)transformedValue:(id)value
{
    const char *name = hb_mixdown_get_name([value intValue]);
    if (name)
    {
        return @(name);
    }
    else
    {
        return nil;
    }
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)reverseTransformedValue:(id)value
{
    return @(hb_mixdown_get_from_name([value UTF8String]));
}

@end

@implementation HBSampleRateTrasformer

+ (Class)transformedValueClass
{
    return [NSString class];
}

- (id)transformedValue:(id)value
{
    const char *name = hb_audio_samplerate_get_name([value intValue]);
    if (name)
    {
        return @(name);
    }
    else
    {
        return nil;
    }
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)reverseTransformedValue:(id)value
{
    return @(hb_audio_samplerate_get_from_name([value UTF8String]));
}

@end

@implementation HBIntegerTrasformer

+ (Class)transformedValueClass
{
    return [NSString class];
}

- (id)transformedValue:(id)value
{
    return [value stringValue];
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)reverseTransformedValue:(id)value
{
    return @([value intValue]);
}

@end