summaryrefslogtreecommitdiffstats
path: root/macosx/HBRange.m
blob: 83df24b66603a3e84a888d3c693b55868ad69fc7 (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
/*  HBRange.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 "HBRange.h"
#import "HBTitle.h"
#import "NSCodingMacro.h"

@implementation HBRange

#pragma mark - NSCoding

- (instancetype)initWithTitle:(HBTitle *)title
{
    self = [super init];
    if (self)
    {
        _title = title;

        _chapterStart = 0;
        _chapterStop = (int)title.chapters.count - 1;

        _secondsStart = 0;
        _secondsStop = title.duration;

        _frameStart = 0;
        _frameStop = title.frames;
    }
    return self;
}

- (NSString *)duration
{
    if (self.type == HBRangeTypeChapters)
    {
        hb_title_t *title = self.title.hb_title;
        hb_chapter_t *chapter;
        int64_t duration = 0;

        for (int i = self.chapterStart; i <= self.chapterStop; i++ )
        {
            chapter = (hb_chapter_t *) hb_list_item(title->list_chapter, i);
            duration += chapter->duration;
        }

        duration /= 90000; // pts -> seconds
        return [NSString stringWithFormat: @"%02lld:%02lld:%02lld", duration / 3600, ( duration / 60 ) % 60,  duration % 60];
    }
    else if (self.type == HBRangeTypeSeconds)
    {
        int duration = self.secondsStop - self.secondsStart;
        return [NSString stringWithFormat:@"%02d:%02d:%02d", duration / 3600, (duration / 60) % 60, duration % 60];

    }
    else if (self.type == HBRangeTypeFrames)
    {
        hb_title_t *title = self.title.hb_title;
        int duration = (self.frameStop - self.frameStart) / (title->vrate.num / title->vrate.den);
        return [NSString stringWithFormat: @"%02d:%02d:%02d", duration / 3600, ( duration / 60 ) % 60, duration % 60];
    }

    return @"00:00:00";
}

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

    if ([key isEqualToString:@"duration"])
    {
        retval = [NSSet setWithObjects:@"type", @"chapterStart", @"chapterStop", @"frameStart", @"frameStop",
                  @"secondsStart", @"secondsStop",nil];
    }

    return retval;
}

#pragma mark - NSCopying

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

    if (copy)
    {
        copy->_type = _type;
        copy->_chapterStart = _chapterStart;
        copy->_chapterStop = _chapterStop;
        copy->_secondsStart = _secondsStart;
        copy->_secondsStop = _secondsStop;
        copy->_frameStart = _frameStart;
        copy->_frameStop = _frameStop;
    }

    return copy;
}

#pragma mark - NSCoding

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

    encodeInt(_type);

    encodeInt(_chapterStart);
    encodeInt(_chapterStop);

    encodeInt(_secondsStart);
    encodeInt(_secondsStop);

    encodeInt(_frameStart);
    encodeInt(_frameStop);
}

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

    decodeInt(_type);

    decodeInt(_chapterStart);
    decodeInt(_chapterStop);

    decodeInt(_secondsStart);
    decodeInt(_secondsStop);

    decodeInt(_frameStart);
    decodeInt(_frameStop);
    
    return self;
}

@end