summaryrefslogtreecommitdiffstats
path: root/macosx/HBVideoController.m
blob: fa0bb192e2770b4687b82f059b95dcbeffd84cc0 (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
/*  HBVideoController.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 "HBVideoController.h"
#import "HBAdvancedController.h"
#import "HBVideo+UIAdditions.h"

#include "hb.h"

static void *HBVideoControllerContext = &HBVideoControllerContext;

@interface HBVideoController () {
    // Framerate Radio Button Framerate Controls
    IBOutlet NSButtonCell *fFramerateVfrPfrCell;

    // Video Encoder
    IBOutlet NSSlider *fVidQualitySlider;

    // Encoder options views
    IBOutlet NSView *fPresetView;
    IBOutlet NSView *fSimplePresetView;

    IBOutlet NSTextField *fEncoderOptionsLabel;

    // x264/x265 Presets Box
    IBOutlet NSButton       *fX264UseAdvancedOptionsCheck;
    IBOutlet NSBox          *fDividerLine;
    IBOutlet NSBox          *fPresetsBox;
    IBOutlet NSSlider       *fPresetsSlider;

    // Text Field to show the expanded opts from unparse()
    IBOutlet NSTextField *fDisplayX264PresetsUnparseTextField;
}

@property (nonatomic, retain, readwrite) HBAdvancedController *advancedController;

@property (nonatomic, readwrite) BOOL presetViewEnabled;

@property (nonatomic, readwrite) NSColor *labelColor;

@end

@implementation HBVideoController

@synthesize enabled = _enabled;

- (instancetype)initWithAdvancedController:(HBAdvancedController *)advancedController
{
    self = [self init];
    if (self)
    {
        _advancedController = [advancedController retain];
    }
    return self;
}

- (instancetype)init
{
    self = [super initWithNibName:@"Video" bundle:nil];
    if (self)
    {
        // Observe the advanced tab pref shown/hided state.
        [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self
                                                                  forKeyPath:@"values.HBShowAdvancedTab"
                                                                     options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial
                                                                     context:HBVideoControllerContext];

        // Observe the x264 slider granularity, to update the slider when the pref is changed.
        [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self
                                                                  forKeyPath:@"values.x264CqSliderFractional"
                                                                     options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial
                                                                     context:HBVideoControllerContext];

        // Observer a bunch of HBVideo properties to update the UI.
        [self addObserver:self forKeyPath:@"video.encoder" options:NSKeyValueObservingOptionInitial context:HBVideoControllerContext];
        [self addObserver:self forKeyPath:@"video.frameRate" options:NSKeyValueObservingOptionInitial context:HBVideoControllerContext];
        [self addObserver:self forKeyPath:@"video.unparseOptions" options:NSKeyValueObservingOptionInitial context:HBVideoControllerContext];
        [self addObserver:self forKeyPath:@"video.advancedOptions" options:NSKeyValueObservingOptionInitial context:HBVideoControllerContext];
    }

    return self;
}

- (void)setEnabled:(BOOL)flag
{
    _enabled = flag;

    if (_enabled)
    {
        self.labelColor = [NSColor controlTextColor];
    }
    else
    {
        self.labelColor = [NSColor disabledControlTextColor];
    }

    [self enableEncoderOptionsWidgets:flag];
}

#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == HBVideoControllerContext)
    {
        if ([keyPath isEqualToString:@"video.encoder"])
        {
            [self switchPresetView];
            [self setupQualitySlider];
        }
        else if ([keyPath isEqualToString:@"video.frameRate"])
        {
            // Hide and set the PFR Checkbox to OFF if we are set to Same as Source
            // Depending on whether or not Same as source is selected modify the title for
            // fFramerateVfrPfrCell
            if (self.video.frameRate == 0) // We are Same as Source
            {
                [fFramerateVfrPfrCell setTitle:@"Variable Framerate"];
            }
            else
            {
                [fFramerateVfrPfrCell setTitle:@"Peak Framerate (VFR)"];
            }
        }
        else if ([keyPath isEqualToString:@"video.unparseOptions"])
        {
            if (self.video.encoder == HB_VCODEC_X264)
            {
                fDisplayX264PresetsUnparseTextField.stringValue = [NSString stringWithFormat:@"x264 Unparse: %@", self.video.unparseOptions];
            }
            else
            {
                fDisplayX264PresetsUnparseTextField.stringValue = @"";
            }
        }
        else if ([keyPath isEqualToString:@"video.advancedOptions"])
        {
            if (self.video.advancedOptions)
            {
                // Do not enable the advanced panel it isn't visible.
                if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HBShowAdvancedTab"])
                {
                    self.advancedController.videoSettings = self.video.advancedOptions ? self.video : nil;
                }
                else
                {
                    self.video.advancedOptions = NO;
                }
            }
            // enable/disable, populate and update the various widgets
            [self enableEncoderOptionsWidgets:self.enabled];

        } else if ([keyPath isEqualToString:@"values.HBShowAdvancedTab"])
        {
            [self toggleAdvancedOptionsCheckBoxForEncoder:self.video.encoder];
        }
        else if ([keyPath isEqualToString:@"values.x264CqSliderFractional"])
        {
            [self setupQualitySlider];
        }
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark - Interface setup

/*
 * Use this method to setup the quality slider for cq/rf values depending on
 * the video encoder selected.
 */
- (void)setupQualitySlider
{
    int direction;
    float minValue, maxValue, granularity;
    hb_video_quality_get_limits(self.video.encoder,
                                &minValue, &maxValue, &granularity, &direction);
    if (granularity < 1.0f)
    {
         // Encoders that allow fractional CQ values often have a low granularity
         // which makes the slider hard to use, so use a value from preferences.
        granularity = [[NSUserDefaults standardUserDefaults]
                       floatForKey:@"x264CqSliderFractional"];
    }
    [fVidQualitySlider setNumberOfTickMarks:((maxValue - minValue) *
                                             (1.0f / granularity)) + 1];

    // Replace the slider transformer with a new one,
    // configured with the new max/min/direction values.
    [fVidQualitySlider unbind:@"value"];
    HBQualityTransformer *transformer = [[[HBQualityTransformer alloc] initWithReversedDirection:direction min:minValue max:maxValue] autorelease];
    [fVidQualitySlider bind:@"value" toObject:self withKeyPath:@"self.video.quality" options:@{NSValueTransformerBindingOption: transformer}];
}

#pragma mark - Video x264/x265 Presets

/**
 *  Shows/hides the right preset view for the current video encoder.
 */
- (void)switchPresetView
{
    self.advancedController.hidden = YES;

    if (self.video.encoder == HB_VCODEC_X264 || self.video.encoder == HB_VCODEC_X265)
    {
        [self toggleAdvancedOptionsCheckBoxForEncoder:self.video.encoder];

        fPresetsBox.contentView = fPresetView;
        [self setupPresetsSlider];

        if (self.video.encoder == HB_VCODEC_X264)
        {
            self.advancedController.hidden = NO;
        }
    }
    else if (self.video.encoder & HB_VCODEC_FFMPEG_MASK)
    {
        fPresetsBox.contentView = fSimplePresetView;
    }
    else
    {
        fPresetsBox.contentView = nil;
    }
}

/**
 *  Enables/disables the advanced panel and the preset panel.
 */
- (void)enableEncoderOptionsWidgets:(BOOL)enable
{
    // check whether the x264 preset system and the advanced panel should be enabled
    BOOL enable_x264_controls  = (enable && !self.video.advancedOptions);
    BOOL enable_advanced_panel = (enable && self.video.advancedOptions);

    // enable/disable the checkbox and advanced panel
    self.presetViewEnabled = enable_x264_controls;
    self.advancedController.enabled = enable_advanced_panel;
}

/**
 *  Shows/Hides the advanced options checkbox
 *
 *  @param encoder the current encoder
 */
- (void)toggleAdvancedOptionsCheckBoxForEncoder:(int)encoder
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HBShowAdvancedTab"] && (encoder == HB_VCODEC_X264))
    {
        fX264UseAdvancedOptionsCheck.hidden = NO;
        fDividerLine.hidden = YES;
        fEncoderOptionsLabel.stringValue = NSLocalizedString(@"Encoder Options:", @"");
    }
    else
    {
        fX264UseAdvancedOptionsCheck.hidden =YES;
        fDividerLine.hidden = NO;
        fEncoderOptionsLabel.stringValue = NSLocalizedString(@"Encoder Options", @"");
        self.video.advancedOptions = NO;
    }
}

/**
 *  Setup the presets slider with the right
 *  number of ticks.
 */
- (void)setupPresetsSlider
{
    // Bind the slider value to a custom value transformer,
    // done here because it can't be done in IB.
    [fPresetsSlider unbind:@"value"];
    HBPresetsTransformer *transformer = [[[HBPresetsTransformer alloc] initWithEncoder:self.video.encoder] autorelease];
    [fPresetsSlider bind:@"value" toObject:self withKeyPath:@"self.video.preset" options:@{NSValueTransformerBindingOption: transformer}];

    // setup the preset slider
    [fPresetsSlider setMaxValue:self.video.presets.count - 1];
    [fPresetsSlider setNumberOfTickMarks:self.video.presets.count];
}


@end