Merge branch '1.0' of git.carlh.net:git/libdcp into 1.0
[libdcp.git] / src / dcp_time.cc
1 /*
2     Copyright (C) 2012-2016 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 /** @file  src/dcp_time.cc
35  *  @brief Time class.
36  */
37
38 #include "raw_convert.h"
39 #include "dcp_time.h"
40 #include "exceptions.h"
41 #include "compose.hpp"
42 #include <boost/algorithm/string.hpp>
43 #include <boost/optional.hpp>
44 #include <iostream>
45 #include <vector>
46 #include <cmath>
47
48 using namespace std;
49 using namespace boost;
50 using namespace dcp;
51
52 Time::Time (int frame, double frames_per_second, int tcr_)
53 {
54         set (double (frame) / frames_per_second, tcr_);
55 }
56
57 /** Construct a Time from a number of seconds and a timecode rate.
58  *
59  *  @param seconds A number of seconds.
60  *  @param tcr_ Timecode rate.
61  */
62 Time::Time (double seconds, int tcr_)
63 {
64         set (seconds, tcr_);
65 }
66
67 /** Construct a Time with specified timecode rate and using the supplied
68  *  number of seconds.
69  *
70  *  @param seconds A number of seconds.
71  *  @param tcr_ Timecode rate to use.
72  */
73 void
74 Time::set (double seconds, int tcr_)
75 {
76         s = floor (seconds);
77         tcr = tcr_;
78
79         e = int (round ((seconds - s) * tcr));
80
81         if (s >= 60) {
82                 m = s / 60;
83                 s -= m * 60;
84         } else {
85                 m = 0;
86         }
87
88         if (m >= 60) {
89                 h = m / 60;
90                 m -= h * 60;
91         } else {
92                 h = 0;
93         }
94 }
95
96 /** @param time String of the form
97  *     HH:MM:SS:EE                          for SMPTE
98  *     HH:MM:SS:E[E[E]] or HH:MM:SS.s[s[s]] for Interop
99  *  where HH are hours, MM minutes, SS seconds, EE editable units and
100  *  sss millseconds.
101  *
102  *  @param tcr_ Timecode rate if this is a SMPTE time, otherwise empty for an Interop time.
103  */
104 Time::Time (string time, optional<int> tcr_)
105 {
106         vector<string> b;
107         split (b, time, is_any_of (":"));
108
109         if (b.size() < 3 || b[0].empty() || b[1].empty() || b[0].length() > 2 || b[1].length() > 2) {
110                 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1", time)));
111         }
112
113         if (!tcr_) {
114                 /* Interop */
115                 if (b.size() == 3) {
116                         /* HH:MM:SS.s[s[s]] */
117                         vector<string> bs;
118                         split (bs, b[2], is_any_of ("."));
119                         if (bs.size() != 2) {
120                                 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1", time)));
121                         }
122
123                         h = raw_convert<int> (b[0]);
124                         m = raw_convert<int> (b[1]);
125                         if (bs[0].empty() || bs[0].length() > 2) {
126                                 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[0])));
127                         }
128                         s = raw_convert<int> (bs[0]);
129                         if (bs[1].empty() || bs[1].length() > 3) {
130                                 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[1])));
131                         }
132                         e = raw_convert<int> (bs[1]);
133                         tcr = 1000;
134                 } else if (b.size() == 4) {
135                         /* HH:MM:SS:EE[E] */
136                         h = raw_convert<int> (b[0]);
137                         m = raw_convert<int> (b[1]);
138                         if (b[2].empty() || b[2].length() > 2) {
139                                 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2])));
140                         }
141                         s = raw_convert<int> (b[2]);
142                         if (b[3].empty() || b[3].length() > 3) {
143                                 boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3])));
144                         }
145                         e = raw_convert<int> (b[3]);
146                         tcr = 250;
147                 } else {
148                         boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1", time)));
149                 }
150
151         } else {
152                 /* SMPTE: HH:MM:SS:EE */
153                 split (b, time, is_any_of (":"));
154                 if (b.size() != 4) {
155                         boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; does not have 4 parts", time)));
156                 }
157
158                 h = raw_convert<int> (b[0]);
159                 m = raw_convert<int> (b[1]);
160                 if (b[2].empty() || b[2].length() > 2) {
161                         boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2])));
162                 }
163                 s = raw_convert<int> (b[2]);
164                 if (b[3].empty() || b[3].length() > 2) {
165                         boost::throw_exception (DCPReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3])));
166                 }
167                 e = raw_convert<int> (b[3]);
168                 tcr = tcr_.get();
169         }
170 }
171
172 bool
173 dcp::operator== (Time const & a, Time const & b)
174 {
175         return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
176 }
177
178 bool
179 dcp::operator!= (Time const & a, Time const & b)
180 {
181         return !(a == b);
182 }
183
184 bool
185 dcp::operator<= (Time const & a, Time const & b)
186 {
187         return a < b || a == b;
188 }
189
190 bool
191 dcp::operator>= (Time const & a, Time const & b)
192 {
193         return a > b || a == b;
194 }
195
196 bool
197 dcp::operator< (Time const & a, Time const & b)
198 {
199         if (a.h != b.h) {
200                 return a.h < b.h;
201         }
202
203         if (a.m != b.m) {
204                 return a.m < b.m;
205         }
206
207         if (a.s != b.s) {
208                 return a.s < b.s;
209         }
210
211         if ((a.e * b.tcr) != (b.e * a.tcr)) {
212                 return (a.e * b.tcr) < (b.e * a.tcr);
213         }
214
215         return true;
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         if ((a.e * b.tcr) != (b.e * a.tcr)) {
234                 return (a.e * b.tcr) > (b.e * a.tcr);
235         }
236
237         return true;
238 }
239
240 ostream &
241 dcp::operator<< (ostream& s, Time const & t)
242 {
243         s << t.h << ":" << t.m << ":" << t.s << "." << t.e;
244         return s;
245 }
246
247 dcp::Time
248 dcp::operator+ (Time a, Time b)
249 {
250         Time r;
251
252         /* Make sure we have a common tcr */
253         if (a.tcr != b.tcr) {
254                 a.e *= b.tcr;
255                 b.e *= a.tcr;
256                 r.tcr = a.tcr * b.tcr;
257         } else {
258                 r.tcr = a.tcr;
259         }
260
261         r.e = a.e + b.e;
262         if (r.e >= r.tcr) {
263                 r.e -= r.tcr;
264                 r.s++;
265         }
266
267         r.s += a.s + b.s;
268         if (r.s >= 60) {
269                 r.s -= 60;
270                 r.m++;
271         }
272
273         r.m += a.m + b.m;
274         if (r.m >= 60) {
275                 r.m -= 60;
276                 r.h++;
277         }
278
279         r.h += a.h + b.h;
280
281         return r;
282 }
283
284 dcp::Time
285 dcp::operator- (Time a, Time b)
286 {
287         Time r;
288
289         /* Make sure we have a common tcr */
290         if (a.tcr != b.tcr) {
291                 a.e *= b.tcr;
292                 b.e *= a.tcr;
293                 r.tcr = a.tcr * b.tcr;
294         } else {
295                 r.tcr = a.tcr;
296         }
297
298         r.e = a.e - b.e;
299         if (r.e < 0) {
300                 r.e += r.tcr;
301                 r.s--;
302         }
303
304         r.s += (a.s - b.s);
305         if (r.s < 0) {
306                 r.s += 60;
307                 r.m--;
308         }
309
310         r.m += (a.m - b.m);
311         if (r.m < 0) {
312                 r.m += 60;
313                 r.h--;
314         }
315
316         r.h += (a.h - b.h);
317
318         return r;
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 /** @return A string of the form h:m:s:e padded as in 00:00:00:000 (for Interop) or 00:00:00:00 (for SMPTE) */
330 string
331 Time::as_string (Standard standard) const
332 {
333         stringstream str;
334         str << setw(2) << setfill('0') << h << ":"
335             << setw(2) << setfill('0') << m << ":"
336             << setw(2) << setfill('0') << s << ":";
337
338         if (standard == SMPTE) {
339                 str << setw(2) << setfill('0') << e;
340         } else {
341                 str << setw(3) << setfill('0') << e;
342         }
343
344         return str.str ();
345 }
346
347 /** @param tcr_ Timecode rate with which the return value should be counted.
348  *  @return the total number of editable units that this time consists of at the specified timecode rate, rounded up
349  *  to the nearest editable unit. For example, as_editable_units (24) returns the total time in frames at 24fps.
350  */
351 int64_t
352 Time::as_editable_units (int tcr_) const
353 {
354         return ceil (int64_t(e) * double (tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
355 }
356
357 /** @return the total number of seconds that this time consists of */
358 double
359 Time::as_seconds () const
360 {
361         return h * 3600 + m * 60 + s + double(e) / tcr;
362 }
363
364 /** @param tcr_ New timecode rate.
365  *  @return A new Time which is this time at the spcified new timecode rate.
366  */
367 Time
368 Time::rebase (int tcr_) const
369 {
370         return Time (h, m, s, floor (float (e) * tcr_ / tcr), tcr_);
371 }