Fix handling of timing in SMPTE subtitles.
[libdcp.git] / src / dcp_time.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/dcp_time.cc
21  *  @brief Time class.
22  */
23
24 #include "raw_convert.h"
25 #include "dcp_time.h"
26 #include "exceptions.h"
27 #include <boost/algorithm/string.hpp>
28 #include <iostream>
29 #include <vector>
30 #include <cmath>
31
32 using namespace std;
33 using namespace boost;
34 using namespace dcp;
35
36 Time::Time (int frame, int frames_per_second, int tcr_)
37         : h (0)
38         , m (0)
39         , s (0)
40         , e (0)
41         , tcr (tcr_)
42 {
43         set (double (frame) / frames_per_second, tcr);
44 }
45
46 void
47 Time::set (double seconds, int tcr_)
48 {
49         s = floor (seconds);
50         tcr = tcr_;
51         
52         e = int (round ((seconds - s) * tcr));
53
54         if (s >= 60) {
55                 m = s / 60;
56                 s -= m * 60;
57         }
58
59         if (m >= 60) {
60                 h = m / 60;
61                 m -= h * 60;
62         }
63 }
64
65 Time::Time (string time, int tcr_)
66         : tcr (tcr_)
67 {
68         vector<string> b;
69         split (b, time, is_any_of (":"));
70         if (b.size() != 4) {
71                 boost::throw_exception (DCPReadError ("unrecognised time specification"));
72         }
73         
74         h = raw_convert<int> (b[0]);
75         m = raw_convert<int> (b[1]);
76         s = raw_convert<int> (b[2]);
77         e = raw_convert<int> (b[3]);
78 }
79
80 bool
81 dcp::operator== (Time const & a, Time const & b)
82 {
83         return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
84 }
85
86 bool
87 dcp::operator!= (Time const & a, Time const & b)
88 {
89         return !(a == b);
90 }
91
92 bool
93 dcp::operator<= (Time const & a, Time const & b)
94 {
95         return a < b || a == b;
96 }
97
98 bool
99 dcp::operator>= (Time const & a, Time const & b)
100 {
101         return a > b || a == b;
102 }
103
104 bool
105 dcp::operator< (Time const & a, Time const & b)
106 {
107         if (a.h != b.h) {
108                 return a.h < b.h;
109         }
110
111         if (a.m != b.m) {
112                 return a.m < b.m;
113         }
114
115         if (a.s != b.s) {
116                 return a.s < b.s;
117         }
118
119         if ((a.e * b.tcr) != (b.e * a.tcr)) {
120                 return (a.e * b.tcr) < (b.e * a.tcr);
121         }
122
123         return true;
124 }
125
126 bool
127 dcp::operator> (Time const & a, Time const & b)
128 {
129         if (a.h != b.h) {
130                 return a.h > b.h;
131         }
132
133         if (a.m != b.m) {
134                 return a.m > b.m;
135         }
136
137         if (a.s != b.s) {
138                 return a.s > b.s;
139         }
140
141         if ((a.e * b.tcr) != (b.e * a.tcr)) {
142                 return (a.e * b.tcr) > (b.e * a.tcr);
143         }
144
145         return true;
146 }
147
148 ostream &
149 dcp::operator<< (ostream& s, Time const & t)
150 {
151         s << t.h << ":" << t.m << ":" << t.s << "." << t.e;
152         return s;
153 }
154
155 dcp::Time
156 dcp::operator+ (Time a, Time b)
157 {
158         Time r;
159
160         /* Make sure we have a common tcr */
161         if (a.tcr != b.tcr) {
162                 a.e *= b.tcr;
163                 b.e *= a.tcr;
164                 r.tcr = a.tcr * b.tcr;
165         } else {
166                 r.tcr = a.tcr;
167         }
168
169         r.e = a.e + b.e;
170         if (r.e >= r.tcr) {
171                 r.e -= r.tcr;
172                 r.s++;
173         }
174
175         r.s += a.s + b.s;
176         if (r.s >= 60) {
177                 r.s -= 60;
178                 r.m++;
179         }
180
181         r.m += a.m + b.m;
182         if (r.m >= 60) {
183                 r.m -= 60;
184                 r.h++;
185         }
186
187         r.h += a.h + b.h;
188
189         return r;
190 }
191
192 dcp::Time
193 dcp::operator- (Time a, Time b)
194 {
195         Time r;
196
197         /* Make sure we have a common tcr */
198         if (a.tcr != b.tcr) {
199                 a.e *= b.tcr;
200                 b.e *= a.tcr;
201                 r.tcr = a.tcr * b.tcr;
202         } else {
203                 r.tcr = a.tcr;
204         }
205         
206         r.e = a.e - b.e;
207         if (r.e < 0) {
208                 r.e += r.tcr;
209                 r.s--;
210         }
211
212         r.s += (a.s - b.s);
213         if (r.s < 0) {
214                 r.s += 60;
215                 r.m--;
216         }
217
218         r.m += (a.m - b.m);
219         if (r.m < 0) {
220                 r.m += 60;
221                 r.h--;
222         }
223
224         r.h += (a.h - b.h);
225
226         return r;
227 }
228
229 float
230 dcp::operator/ (Time a, Time const & b)
231 {
232         int64_t const at = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr;
233         int64_t const bt = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr;
234         return float (at) / bt;
235 }
236
237 /** @return A string of the form h:m:s:e */
238 string
239 Time::to_string () const
240 {
241         stringstream str;
242         str << h << ":" << m << ":" << s << ":" << e;
243         return str.str ();
244 }
245
246 int64_t
247 Time::to_editable_units (int tcr_) const
248 {
249         return (int64_t(e) * float (tcr_ / tcr)) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
250 }