e21eaf2794aa8d37ef25c0705f2f547d8ceb5018
[libdcp.git] / src / dcp_time.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/dcp_time.cc
36  *  @brief Time class
37  */
38
39
40 #include "raw_convert.h"
41 #include "dcp_time.h"
42 #include "exceptions.h"
43 #include "compose.hpp"
44 #include "dcp_assert.h"
45 #include <boost/algorithm/string.hpp>
46 #include <boost/optional.hpp>
47 #include <iostream>
48 #include <vector>
49 #include <cmath>
50
51
52 using std::ostream;
53 using std::string;
54 using std::vector;
55 using boost::is_any_of;
56 using boost::optional;
57 using namespace dcp;
58
59
60 Time::Time (int frame, double frames_per_second, int tcr_)
61 {
62         set (double (frame) / frames_per_second, tcr_);
63 }
64
65
66 Time::Time (double seconds, int tcr_)
67 {
68         set (seconds, tcr_);
69 }
70
71
72 /** Construct a Time with specified timecode rate and using the supplied
73  *  number of seconds.
74  *
75  *  @param seconds A number of seconds.
76  *  @param tcr_ Timecode rate to use.
77  */
78 void
79 Time::set (double seconds, int tcr_)
80 {
81         s = floor (seconds);
82         tcr = tcr_;
83
84         e = int (round ((seconds - s) * tcr));
85
86         if (s >= 60) {
87                 m = s / 60;
88                 s -= m * 60;
89         } else {
90                 m = 0;
91         }
92
93         if (m >= 60) {
94                 h = m / 60;
95                 m -= h * 60;
96         } else {
97                 h = 0;
98         }
99 }
100
101
102 Time::Time (string time, optional<int> tcr_)
103 {
104         vector<string> b;
105         split (b, time, is_any_of (":"));
106
107         if (b.size() < 3 || b[0].empty() || b[1].empty() || b[0].length() > 2 || b[1].length() > 2) {
108                 boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time)));
109         }
110
111         if (!tcr_) {
112                 /* Interop */
113                 if (b.size() == 3) {
114                         /* HH:MM:SS.s[s[s]] */
115                         vector<string> bs;
116                         split (bs, b[2], is_any_of ("."));
117                         if (bs.size() != 2) {
118                                 boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time)));
119                         }
120
121                         h = raw_convert<int> (b[0]);
122                         m = raw_convert<int> (b[1]);
123                         if (bs[0].empty() || bs[0].length() > 2) {
124                                 boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[0])));
125                         }
126                         s = raw_convert<int> (bs[0]);
127                         if (bs[1].empty() || bs[1].length() > 3) {
128                                 boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[1])));
129                         }
130                         e = raw_convert<int> (bs[1]);
131                         tcr = 1000;
132                 } else if (b.size() == 4) {
133                         /* HH:MM:SS:EE[E] */
134                         h = raw_convert<int> (b[0]);
135                         m = raw_convert<int> (b[1]);
136                         if (b[2].empty() || b[2].length() > 2) {
137                                 boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2])));
138                         }
139                         s = raw_convert<int> (b[2]);
140                         if (b[3].empty() || b[3].length() > 3) {
141                                 boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3])));
142                         }
143                         e = raw_convert<int> (b[3]);
144                         tcr = 250;
145                 } else {
146                         boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time)));
147                 }
148
149         } else {
150                 /* SMPTE: HH:MM:SS:EE */
151                 split (b, time, is_any_of (":"));
152                 if (b.size() != 4) {
153                         boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; does not have 4 parts", time)));
154                 }
155
156                 h = raw_convert<int> (b[0]);
157                 m = raw_convert<int> (b[1]);
158                 if (b[2].empty() || b[2].length() > 2) {
159                         boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2])));
160                 }
161                 s = raw_convert<int> (b[2]);
162                 if (b[3].empty() || b[3].length() > 2) {
163                         boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3])));
164                 }
165                 e = raw_convert<int> (b[3]);
166                 tcr = tcr_.get();
167         }
168 }
169
170
171 bool
172 dcp::operator== (Time const & a, Time const & b)
173 {
174         return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
175 }
176
177
178 bool
179 dcp::operator!= (Time const & a, Time const & b)
180 {
181         return !(a == b);
182 }
183
184
185 bool
186 dcp::operator<= (Time const & a, Time const & b)
187 {
188         return a < b || a == b;
189 }
190
191
192 bool
193 dcp::operator>= (Time const & a, Time const & b)
194 {
195         return a > b || a == b;
196 }
197
198
199 bool
200 dcp::operator< (Time const & a, Time const & b)
201 {
202         if (a.h != b.h) {
203                 return a.h < b.h;
204         }
205
206         if (a.m != b.m) {
207                 return a.m < b.m;
208         }
209
210         if (a.s != b.s) {
211                 return a.s < b.s;
212         }
213
214         return (a.e * b.tcr) < (b.e * a.tcr);
215 }
216
217
218 bool
219 dcp::operator> (Time const & a, Time const & b)
220 {
221         if (a.h != b.h) {
222                 return a.h > b.h;
223         }
224
225         if (a.m != b.m) {
226                 return a.m > b.m;
227         }
228
229         if (a.s != b.s) {
230                 return a.s > b.s;
231         }
232
233         return (a.e * b.tcr) > (b.e * a.tcr);
234 }
235
236
237 ostream &
238 dcp::operator<< (ostream& s, Time const & t)
239 {
240         s << t.h << ":" << t.m << ":" << t.s << "." << t.e;
241         return s;
242 }
243
244
245 dcp::Time
246 dcp::operator+ (Time a, Time b)
247 {
248         Time r;
249
250         /* Make sure we have a common tcr */
251         if (a.tcr != b.tcr) {
252                 a.e *= b.tcr;
253                 b.e *= a.tcr;
254                 r.tcr = a.tcr * b.tcr;
255         } else {
256                 r.tcr = a.tcr;
257         }
258
259         r.e = a.e + b.e;
260         if (r.e >= r.tcr) {
261                 r.e -= r.tcr;
262                 r.s++;
263         }
264
265         r.s += a.s + b.s;
266         if (r.s >= 60) {
267                 r.s -= 60;
268                 r.m++;
269         }
270
271         r.m += a.m + b.m;
272         if (r.m >= 60) {
273                 r.m -= 60;
274                 r.h++;
275         }
276
277         r.h += a.h + b.h;
278
279         return r;
280 }
281
282
283 dcp::Time
284 dcp::operator- (Time a, Time b)
285 {
286         Time r;
287
288         /* Make sure we have a common tcr */
289         if (a.tcr != b.tcr) {
290                 a.e *= b.tcr;
291                 b.e *= a.tcr;
292                 r.tcr = a.tcr * b.tcr;
293         } else {
294                 r.tcr = a.tcr;
295         }
296
297         r.e = a.e - b.e;
298         if (r.e < 0) {
299                 r.e += r.tcr;
300                 r.s--;
301         }
302
303         r.s += (a.s - b.s);
304         if (r.s < 0) {
305                 r.s += 60;
306                 r.m--;
307         }
308
309         r.m += (a.m - b.m);
310         if (r.m < 0) {
311                 r.m += 60;
312                 r.h--;
313         }
314
315         r.h += (a.h - b.h);
316
317         return r;
318 }
319
320
321 float
322 dcp::operator/ (Time a, Time const & b)
323 {
324         int64_t const at = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr;
325         int64_t const bt = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr;
326         return float (at) / bt;
327 }
328
329
330 string
331 Time::as_string (Standard standard) const
332 {
333         char buffer[64];
334
335         if (standard == Standard::SMPTE) {
336                 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", h, m, s, e);
337         } else {
338                 snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%03d", h, m, s, e);
339         }
340
341         return buffer;
342 }
343
344
345 int64_t
346 Time::as_editable_units_floor (int tcr_) const
347 {
348         return floor(int64_t(e) * double(tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
349 }
350
351
352 int64_t
353 Time::as_editable_units_ceil (int tcr_) const
354 {
355         return ceil(int64_t(e) * double(tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
356 }
357
358
359 double
360 Time::as_seconds () const
361 {
362         return h * 3600 + m * 60 + s + double(e) / tcr;
363 }
364
365
366 Time
367 Time::rebase (int tcr_) const
368 {
369         long int e_ = lrintf (float (e) * tcr_ / tcr);
370         int s_ = s;
371         if (e_ >= tcr_) {
372                 e_ -= tcr_;
373                 ++s_;
374         }
375         int m_ = m;
376         if (s_ >= 60) {
377                 s_ -= 60;
378                 ++m_;
379         }
380         int h_ = h;
381         if (m_ >= 60) {
382                 m_ -= 60;
383                 ++h_;
384         }
385
386         return Time (h_, m_, s_, e_, tcr_);
387 }