summaryrefslogtreecommitdiffstats
path: root/macosx/HBPicture.m
blob: 639535f3526ee1355b717b2dab9c84fa20c84fde (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
/*  HBPicture.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 "HBPicture.h"
#import "HBTitle.h"

#import "NSCodingMacro.h"

#include "hb.h"

NSString * const HBPictureChangedNotification = @"HBPictureChangedNotification";

@interface HBPicture ()

@property (nonatomic, readwrite, getter=isValidating) BOOL validating;
@property (nonatomic, readwrite, getter=areNotificationsEnabled) BOOL notificationsEnabled;

@property (nonatomic, readwrite) int keep;
@property (nonatomic, readwrite) BOOL darUpdated;

@end

@implementation HBPicture

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        // Set some values if we ever need a fake instance
        _width = 1280;
        _height = 720;
        _anamorphicMode = HB_ANAMORPHIC_NONE;
        _parWidth = 1;
        _parHeight = 1;
    }
    return self;
}

- (instancetype)initWithTitle:(HBTitle *)title
{
    self = [self init];
    if (self)
    {
        _title = title;
        _width = title.hb_title->geometry.width;
        _height = title.hb_title->geometry.height;

        [self validateSettings];

        _notificationsEnabled = YES;
    }
    return self;
}

- (void)postChangedNotification
{
    if (self.areNotificationsEnabled)
    {
        [[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:HBPictureChangedNotification
                                                                                              object:self
                                                                                            userInfo:nil]];
    }
}

- (void)setWidth:(int)width
{
    _width = width;

    if (!self.isValidating)
    {
        self.keep |= HB_KEEP_WIDTH;
        [self validateSettings];
    }
}

- (BOOL)validateWidth:(id *)ioValue error:(NSError *)outError
{
    BOOL retval = YES;

    if (nil != *ioValue)
    {
        int value = [*ioValue intValue];
        if (value >= self.maxWidth)
        {
            *ioValue = @(self.maxWidth);
        }
        else if (value <= 32)
        {
            *ioValue = @32;
        }
    }

    return retval;
}

- (void)setHeight:(int)height
{
    _height = height;
    if (!self.isValidating)
    {
        self.keep |= HB_KEEP_HEIGHT;
        [self validateSettings];
    }
}

- (BOOL)validateHeight:(id *)ioValue error:(NSError *)outError
{
    BOOL retval = YES;

    if (nil != *ioValue)
    {
        int value = [*ioValue intValue];
        if (value >= self.maxHeight)
        {
            *ioValue = @(self.maxHeight);
        }
        else if (value <= 32)
        {
            *ioValue = @32;
        }
    }

    return retval;
}

- (void)setDisplayWidth:(int)displayWidth
{
    _displayWidth = displayWidth;
    if (!self.isValidating)
    {
        self.darUpdated = YES;
        [self validateSettings];
    }
}

- (void)setParWidth:(int)parWidth
{
    _parWidth = parWidth;
    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

- (void)setParHeight:(int)parHeight
{
    _parHeight = parHeight;
    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

- (void)setCropTop:(int)cropTop
{
    _cropTop = cropTop;
    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

- (void)setCropBottom:(int)cropBottom
{
    _cropBottom = cropBottom;
    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

- (void)setCropLeft:(int)cropLeft
{
    _cropLeft = cropLeft;
    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

- (void)setCropRight:(int)cropRight
{
    _cropRight = cropRight;
    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

- (BOOL)validateCropTop:(id *)ioValue error:(NSError *)outError
{
    [self validateVCrop:ioValue];
    return YES;
}

- (BOOL)validateCropBottom:(id *)ioValue error:(NSError *)outError
{
    [self validateVCrop:ioValue];
    return YES;
}

- (BOOL)validateCropLeft:(id *)ioValue error:(NSError *)outError
{
    [self validateHCrop:ioValue];
    return YES;
}

- (BOOL)validateCropRight:(id *)ioValue error:(NSError *)outError
{
    [self validateHCrop:ioValue];
    return YES;
}

- (void)validateHCrop:(NSNumber **)ioValue
{
    if (nil != *ioValue)
    {
        int value = [*ioValue intValue];
        if (value >= self.maxHorizontalCrop)
        {
            *ioValue =  @(self.maxHorizontalCrop);
        }
        else if (value < 0)
        {
            *ioValue = @0;
        }
    }
}

- (void)validateVCrop:(NSNumber **)ioValue
{
    if (nil != *ioValue)
    {
        int value = [*ioValue intValue];
        if (value >= self.maxVerticalCrop)
        {
            *ioValue =  @(self.maxVerticalCrop);
        }
        else if (value < 0)
        {
            *ioValue = @0;
        }
    }
}

- (void)setAutocrop:(BOOL)autocrop
{
    _autocrop = autocrop;
    if (self.title && autocrop && !self.isValidating)
    {
        hb_title_t *title = self.title.hb_title;
        self.validating = YES;
        // Reset the crop values to those determined right after scan
        self.cropTop    = title->crop[0];
        self.cropBottom = title->crop[1];
        self.cropLeft   = title->crop[2];
        self.cropRight  = title->crop[3];
        self.validating = NO;
        [self validateSettings];
    }
}

- (void)setAnamorphicMode:(int)anamorphicMode
{
    _anamorphicMode = anamorphicMode;

    if (self.anamorphicMode == HB_ANAMORPHIC_STRICT ||
        self.anamorphicMode == HB_ANAMORPHIC_LOOSE)
    {
        self.keepDisplayAspect = YES;
    }

    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

- (void)setKeepDisplayAspect:(int)keepDisplayAspect
{
    _keepDisplayAspect = keepDisplayAspect;
    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

- (void)setModulus:(int)modulus
{
    _modulus = modulus;
    if (!self.isValidating)
    {
        [self validateSettings];
    }
}

#pragma mark - Max sizes

- (int)maxWidth
{
    if (self.title)
        return self.title.hb_title->geometry.width - self.cropRight - self.cropLeft;

    return 0;
}

- (int)maxHeight
{
    if (self.title)
        return self.title.hb_title->geometry.height - self.cropTop - self.cropBottom;

    return 0;
}

- (int)maxVerticalCrop
{
    if (self.title)
        return self.title.hb_title->geometry.height / 2 - 2;

    return 0;
}

- (int)maxHorizontalCrop
{
    if (self.title)
        return self.title.hb_title->geometry.width / 2 - 2;

    return 0;
}

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

    // Tell KVO to reload the editable state.
    if ([key isEqualToString:@"keepDisplayAspectEditable"] ||
        [key isEqualToString:@"heightEditable"] ||
        [key isEqualToString:@"widthEditable"] ||
        [key isEqualToString:@"customAnamorphicEnabled"])
    {
        retval = [NSSet setWithObjects:@"anamorphicMode", nil];
    }

    if ([key isEqualToString:@"maxWidth"] ||
        [key isEqualToString:@"maxHeight"])
    {
        retval = [NSSet setWithObjects:@"cropTop", @"cropBottom", @"cropLeft", @"cropRight", nil];
    }

    if ([key isEqualToString:@"info"] || [key isEqualToString:@"summary"])
    {
        retval = [NSSet setWithObjects:@"width", @"height",@"anamorphicMode", @"cropTop", @"cropBottom", @"cropLeft", @"cropRight", nil];
    }

    return retval;
}

- (void)setNilValueForKey:(NSString *)key
{
    [self setValue:@0 forKey:key];
}

#pragma mark - Picture Update Logic

/**
 *  Validates the settings through hb_set_anamorphic_size2,
 *  each setters calls this after setting its value.
 */
- (void)validateSettings
{
    self.validating = YES;

    hb_title_t *title = self.title.hb_title;

    self.keep |= self.keepDisplayAspect * HB_KEEP_DISPLAY_ASPECT;

    hb_geometry_t srcGeo, resultGeo;
    hb_geometry_settings_t uiGeo;

    srcGeo.width = title->geometry.width;
    srcGeo.height = title->geometry.height;
    srcGeo.par = title->geometry.par;

    uiGeo.mode = self.anamorphicMode;
    uiGeo.keep = self.keep;
    uiGeo.itu_par = 0;
    uiGeo.modulus = self.modulus;

    int crop[4] = {self.cropTop, self.cropBottom, self.cropLeft, self.cropRight};
    memcpy(uiGeo.crop, crop, sizeof(int[4]));
    uiGeo.geometry.width = self.width;
    uiGeo.geometry.height =  self.height;
    // Modulus added to maxWidth/maxHeight to allow a small amount of
    // upscaling to the next mod boundary.
    uiGeo.maxWidth = title->geometry.width - crop[2] - crop[3] + self.modulus - 1;
    uiGeo.maxHeight = title->geometry.height - crop[0] - crop[1] + self.modulus - 1;

    hb_rational_t par = {self.parWidth, self.parHeight};
    uiGeo.geometry.par = par;
    if (self.anamorphicMode == HB_ANAMORPHIC_CUSTOM && self.darUpdated)
    {
        uiGeo.geometry.par.num = self.displayWidth;
        uiGeo.geometry.par.den = uiGeo.geometry.width;
    }
    hb_set_anamorphic_size2(&srcGeo, &uiGeo, &resultGeo);

    int display_width;
    display_width = resultGeo.width * resultGeo.par.num / resultGeo.par.den;

    self.width = resultGeo.width;
    self.height = resultGeo.height;
    self.parWidth = resultGeo.par.num;
    self.parHeight = resultGeo.par.den;
    self.displayWidth = display_width;

    self.validating = NO;
    self.keep = 0;
    self.darUpdated = NO;

    [self postChangedNotification];
}

#pragma mark - NSCopying

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

    if (copy)
    {
        copy->_width = _width;
        copy->_height = _height;

        copy->_keepDisplayAspect = _keepDisplayAspect;
        copy->_anamorphicMode = _anamorphicMode;
        copy->_modulus = _modulus;

        copy->_displayWidth = _displayWidth;
        copy->_parWidth = _parWidth;
        copy->_parHeight = _parHeight;

        copy->_autocrop = _autocrop;
        copy->_cropTop = _cropTop;
        copy->_cropBottom = _cropBottom;
        copy->_cropLeft = _cropLeft;
        copy->_cropRight = _cropRight;

        copy->_notificationsEnabled = _notificationsEnabled;
    }

    return copy;
}

#pragma mark - NSCoding

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

    encodeInt(_width);
    encodeInt(_height);

    encodeInt(_keepDisplayAspect);
    encodeInt(_anamorphicMode);
    encodeInt(_modulus);

    encodeInt(_displayWidth);
    encodeInt(_parWidth);
    encodeInt(_parHeight);

    encodeBool(_autocrop);
    encodeInt(_cropTop);
    encodeInt(_cropBottom);
    encodeInt(_cropLeft);
    encodeInt(_cropRight);
}

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

    decodeInt(_width);
    decodeInt(_height);

    decodeInt(_keepDisplayAspect);
    decodeInt(_anamorphicMode);
    decodeInt(_modulus);

    decodeInt(_displayWidth);
    decodeInt(_parWidth);
    decodeInt(_parHeight);

    decodeBool(_autocrop);
    decodeInt(_cropTop);
    decodeInt(_cropBottom);
    decodeInt(_cropLeft);
    decodeInt(_cropRight);

    _notificationsEnabled = YES;
    
    return self;
}

#pragma mark - Presets/Queue

- (void)writeToPreset:(NSMutableDictionary *)preset
{
    preset[@"PictureKeepRatio"] = @(self.keepDisplayAspect);
    preset[@"PicturePAR"]       = @(self.anamorphicMode);
    preset[@"PictureModulus"]   = @(self.modulus);

    // Set crop settings
    preset[@"PictureAutoCrop"] = @(self.autocrop);

    preset[@"PictureTopCrop"]    = @(self.cropTop);
    preset[@"PictureBottomCrop"] = @(self.cropBottom);
    preset[@"PictureLeftCrop"]   = @(self.cropLeft);
    preset[@"PictureRightCrop"]  = @(self.cropRight);
}

- (void)applyPreset:(NSDictionary *)preset
{
    self.validating = YES;
    self.notificationsEnabled = NO;
    hb_title_t *title = self.title.hb_title;

    /* Note: objectForKey:@"UsesPictureSettings" refers to picture size, which encompasses:
     * height, width, keep ar, anamorphic and crop settings.
     * picture filters are handled separately below.
     */
    int maxWidth = title->geometry.width - self.cropLeft - self.cropRight;
    int maxHeight = title->geometry.height - self.cropTop - self.cropBottom;
    int jobMaxWidth = 0, jobMaxHeight = 0;

    /* Check to see if the objectForKey:@"UsesPictureSettings is greater than 0, as 0 means use picture sizing "None"
     * ( 2 is use max for source and 1 is use exact size when the preset was created ) and the
     * preset completely ignores any picture sizing values in the preset.
     */
    if ([preset[@"UsesPictureSettings"] intValue] > 0)
    {
        // If Cropping is set to custom, then recall all four crop values from
        // when the preset was created and apply them
        if ([preset[@"PictureAutoCrop"]  intValue] == 0)
        {
            self.autocrop = NO;

            // Here we use the custom crop values saved at the time the preset was saved
            self.cropTop    = [preset[@"PictureTopCrop"] intValue];
            self.cropBottom = [preset[@"PictureBottomCrop"] intValue];
            self.cropLeft   = [preset[@"PictureLeftCrop"] intValue];
            self.cropRight  = [preset[@"PictureRightCrop"] intValue];
        }
        else // if auto crop has been saved in preset, set to auto and use post scan auto crop
        {
            self.autocrop = YES;
            /* Here we use the auto crop values determined right after scan */
            self.cropTop    = title->crop[0];
            self.cropBottom = title->crop[1];
            self.cropLeft   = title->crop[2];
            self.cropRight  = title->crop[3];
        }

        // crop may have changed, reset maxWidth/maxHeight
        maxWidth = title->geometry.width - self.cropLeft - self.cropRight;
        maxHeight = title->geometry.height - self.cropTop - self.cropBottom;

        // Set modulus
        if (preset[@"PictureModulus"])
        {
            self.modulus = [preset[@"PictureModulus"]  intValue];
        }
        else
        {
            self.modulus = 16;
        }

        // Assume max picture settings initially.
        self.keepDisplayAspect = [preset[@"PictureKeepRatio"] intValue];
        self.anamorphicMode = [preset[@"PicturePAR"] intValue];
        self.width = title->geometry.width - self.cropLeft - self.cropRight;
        self.height = title->geometry.height - self.cropTop - self.cropBottom;

        // Check to see if the objectForKey:@"UsesPictureSettings" is 2,
        // which means "Use max. picture size for source"
        // If not 2 it must be 1 here which means "Use the picture
        // size specified in the preset"
        if ([preset[@"UsesPictureSettings"] intValue] != 2 &&
            [preset[@"UsesMaxPictureSettings"] intValue] != 1)
        {
             // if the preset specifies neither max. width nor height
             // (both are 0), use the max. picture size
             //
             // if the specified non-zero dimensions exceed those of the
             // source, also use the max. picture size (no upscaling)
            if ([preset[@"PictureWidth"] intValue] > 0)
            {
                jobMaxWidth  = [preset[@"PictureWidth"] intValue];
            }
            if ([preset[@"PictureHeight"]  intValue] > 0)
            {
                jobMaxHeight  = [preset[@"PictureHeight"] intValue];
            }
        }
    }
    // Modulus added to maxWidth/maxHeight to allow a small amount of
    // upscaling to the next mod boundary. This does not apply to
    // explicit limits set for device compatibility.  It only applies
    // when limiting to cropped title dimensions.
    maxWidth += self.modulus - 1;
    maxHeight += self.modulus - 1;
    if (jobMaxWidth == 0 || jobMaxWidth > maxWidth)
        jobMaxWidth = maxWidth;
    if (jobMaxHeight == 0 || jobMaxHeight > maxHeight)
        jobMaxHeight = maxHeight;

    hb_geometry_t srcGeo, resultGeo;
    hb_geometry_settings_t uiGeo;

    srcGeo.width = title->geometry.width;
    srcGeo.height = title->geometry.height;
    srcGeo.par = title->geometry.par;

    uiGeo.mode = self.anamorphicMode;
    uiGeo.keep = self.keepDisplayAspect * HB_KEEP_DISPLAY_ASPECT;
    uiGeo.itu_par = 0;
    uiGeo.modulus = self.modulus;
    int crop[4] = {self.cropTop, self.cropBottom, self.cropLeft, self.cropRight};
    memcpy(uiGeo.crop, crop, sizeof(int[4]));
    uiGeo.geometry.width = self.width;
    uiGeo.geometry.height =  self.height;

    hb_rational_t par = {self.parWidth, self.parHeight};
    uiGeo.geometry.par = par;

    uiGeo.maxWidth = jobMaxWidth;
    uiGeo.maxHeight = jobMaxHeight;
    hb_set_anamorphic_size2(&srcGeo, &uiGeo, &resultGeo);

    int display_width;
    display_width = resultGeo.width * resultGeo.par.num / resultGeo.par.den;

    self.width = resultGeo.width;
    self.height = resultGeo.height;
    self.parWidth = resultGeo.par.num;
    self.parHeight = resultGeo.par.den;
    self.displayWidth = display_width;

    self.validating = NO;
    self.notificationsEnabled = YES;
}

@end