summaryrefslogtreecommitdiff
path: root/src/dcp_time.cc
blob: 025a48590ed51aafcc4ff02c8670e0997a3e4724 (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
/*
    Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>

    This file is part of libdcp.

    libdcp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libdcp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations
    including the two.

    You must obey the GNU General Public License in all respects
    for all of the code used other than OpenSSL.  If you modify
    file(s) with this exception, you may extend this exception to your
    version of the file(s), but you are not obligated to do so.  If you
    do not wish to do so, delete this exception statement from your
    version.  If you delete this exception statement from all source
    files in the program, then also delete it here.
*/

/** @file  src/dcp_time.cc
 *  @brief Time class.
 */

#include "raw_convert.h"
#include "dcp_time.h"
#include "exceptions.h"
#include "compose.hpp"
#include "dcp_assert.h"
#include <boost/algorithm/string.hpp>
#include <boost/optional.hpp>
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;
using namespace boost;
using namespace dcp;

Time::Time (int frame, double frames_per_second, int tcr_)
{
	set (double (frame) / frames_per_second, tcr_);
}

/** Construct a Time from a number of seconds and a timecode rate.
 *
 *  @param seconds A number of seconds.
 *  @param tcr_ Timecode rate.
 */
Time::Time (double seconds, int tcr_)
{
	set (seconds, tcr_);
}

/** Construct a Time with specified timecode rate and using the supplied
 *  number of seconds.
 *
 *  @param seconds A number of seconds.
 *  @param tcr_ Timecode rate to use.
 */
void
Time::set (double seconds, int tcr_)
{
	s = floor (seconds);
	tcr = tcr_;

	e = int (round ((seconds - s) * tcr));

	if (s >= 60) {
		m = s / 60;
		s -= m * 60;
	} else {
		m = 0;
	}

	if (m >= 60) {
		h = m / 60;
		m -= h * 60;
	} else {
		h = 0;
	}
}

/** @param time String of the form
 *     HH:MM:SS:EE                          for SMPTE
 *     HH:MM:SS:E[E[E]] or HH:MM:SS.s[s[s]] for Interop
 *  where HH are hours, MM minutes, SS seconds, EE editable units and
 *  sss millseconds.
 *
 *  @param tcr_ Timecode rate if this is a SMPTE time, otherwise empty for an Interop time.
 */
Time::Time (string time, optional<int> tcr_)
{
	vector<string> b;
	split (b, time, is_any_of (":"));

	if (b.size() < 3 || b[0].empty() || b[1].empty() || b[0].length() > 2 || b[1].length() > 2) {
		boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time)));
	}

	if (!tcr_) {
		/* Interop */
		if (b.size() == 3) {
			/* HH:MM:SS.s[s[s]] */
			vector<string> bs;
			split (bs, b[2], is_any_of ("."));
			if (bs.size() != 2) {
				boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time)));
			}

			h = raw_convert<int> (b[0]);
			m = raw_convert<int> (b[1]);
			if (bs[0].empty() || bs[0].length() > 2) {
				boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[0])));
			}
			s = raw_convert<int> (bs[0]);
			if (bs[1].empty() || bs[1].length() > 3) {
				boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, bs[1])));
			}
			e = raw_convert<int> (bs[1]);
			tcr = 1000;
		} else if (b.size() == 4) {
			/* HH:MM:SS:EE[E] */
			h = raw_convert<int> (b[0]);
			m = raw_convert<int> (b[1]);
			if (b[2].empty() || b[2].length() > 2) {
				boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2])));
			}
			s = raw_convert<int> (b[2]);
			if (b[3].empty() || b[3].length() > 3) {
				boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3])));
			}
			e = raw_convert<int> (b[3]);
			tcr = 250;
		} else {
			boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1", time)));
		}

	} else {
		/* SMPTE: HH:MM:SS:EE */
		split (b, time, is_any_of (":"));
		if (b.size() != 4) {
			boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; does not have 4 parts", time)));
		}

		h = raw_convert<int> (b[0]);
		m = raw_convert<int> (b[1]);
		if (b[2].empty() || b[2].length() > 2) {
			boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[2])));
		}
		s = raw_convert<int> (b[2]);
		if (b[3].empty() || b[3].length() > 2) {
			boost::throw_exception (ReadError (String::compose ("unrecognised time specification %1; %2 has bad length", time, b[3])));
		}
		e = raw_convert<int> (b[3]);
		tcr = tcr_.get();
	}
}

bool
dcp::operator== (Time const & a, Time const & b)
{
	return (a.h == b.h && a.m == b.m && a.s == b.s && (a.e * b.tcr) == (b.e * a.tcr));
}

bool
dcp::operator!= (Time const & a, Time const & b)
{
	return !(a == b);
}

bool
dcp::operator<= (Time const & a, Time const & b)
{
	return a < b || a == b;
}

bool
dcp::operator>= (Time const & a, Time const & b)
{
	return a > b || a == b;
}

bool
dcp::operator< (Time const & a, Time const & b)
{
	if (a.h != b.h) {
		return a.h < b.h;
	}

	if (a.m != b.m) {
		return a.m < b.m;
	}

	if (a.s != b.s) {
		return a.s < b.s;
	}

	return (a.e * b.tcr) < (b.e * a.tcr);
}

bool
dcp::operator> (Time const & a, Time const & b)
{
	if (a.h != b.h) {
		return a.h > b.h;
	}

	if (a.m != b.m) {
		return a.m > b.m;
	}

	if (a.s != b.s) {
		return a.s > b.s;
	}

	return (a.e * b.tcr) > (b.e * a.tcr);
}

ostream &
dcp::operator<< (ostream& s, Time const & t)
{
	s << t.h << ":" << t.m << ":" << t.s << "." << t.e;
	return s;
}

dcp::Time
dcp::operator+ (Time a, Time b)
{
	Time r;

	/* Make sure we have a common tcr */
	if (a.tcr != b.tcr) {
		a.e *= b.tcr;
		b.e *= a.tcr;
		r.tcr = a.tcr * b.tcr;
	} else {
		r.tcr = a.tcr;
	}

	r.e = a.e + b.e;
	if (r.e >= r.tcr) {
		r.e -= r.tcr;
		r.s++;
	}

	r.s += a.s + b.s;
	if (r.s >= 60) {
		r.s -= 60;
		r.m++;
	}

	r.m += a.m + b.m;
	if (r.m >= 60) {
		r.m -= 60;
		r.h++;
	}

	r.h += a.h + b.h;

	return r;
}

dcp::Time
dcp::operator- (Time a, Time b)
{
	Time r;

	/* Make sure we have a common tcr */
	if (a.tcr != b.tcr) {
		a.e *= b.tcr;
		b.e *= a.tcr;
		r.tcr = a.tcr * b.tcr;
	} else {
		r.tcr = a.tcr;
	}

	r.e = a.e - b.e;
	if (r.e < 0) {
		r.e += r.tcr;
		r.s--;
	}

	r.s += (a.s - b.s);
	if (r.s < 0) {
		r.s += 60;
		r.m--;
	}

	r.m += (a.m - b.m);
	if (r.m < 0) {
		r.m += 60;
		r.h--;
	}

	r.h += (a.h - b.h);

	return r;
}

float
dcp::operator/ (Time a, Time const & b)
{
	int64_t const at = a.h * 3600 + a.m * 60 + a.s * float (a.e) / a.tcr;
	int64_t const bt = b.h * 3600 + b.m * 60 + b.s * float (b.e) / b.tcr;
	return float (at) / bt;
}

/** @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) */
string
Time::as_string (Standard standard) const
{
	char buffer[64];

	if (standard == SMPTE) {
		snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%02d", h, m, s, e);
	} else {
		snprintf (buffer, sizeof(buffer), "%02d:%02d:%02d:%03d", h, m, s, e);
	}

	return buffer;
}

/** @param tcr_ Timecode rate with which the return value should be counted.
 *  @return the total number of editable units that this time consists of at the specified timecode rate, rounded up
 *  to the nearest editable unit. For example, as_editable_units (24) returns the total time in frames at 24fps.
 */
int64_t
Time::as_editable_units (int tcr_) const
{
	return ceil (int64_t(e) * double (tcr_) / tcr) + int64_t(s) * tcr_ + int64_t(m) * 60 * tcr_ + int64_t(h) * 60 * 60 * tcr_;
}

/** @return the total number of seconds that this time consists of */
double
Time::as_seconds () const
{
	return h * 3600 + m * 60 + s + double(e) / tcr;
}

/** @param tcr_ New timecode rate.
 *  @return A new Time which is this time at the spcified new timecode rate.
 */
Time
Time::rebase (int tcr_) const
{
	long int e_ = lrintf (float (e) * tcr_ / tcr);
	int s_ = s;
	if (e_ >= tcr_) {
		e_ -= tcr_;
		++s_;
	}
	int m_ = m;
	if (s_ >= 60) {
		s_ -= 60;
		++m_;
	}
	int h_ = h;
	if (m_ >= 60) {
		m_ -= 60;
		++h_;
	}

	return Time (h_, m_, s_, e_, tcr_);
}