2 Copyright (C) 2006-2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #define Timecode_IS_AROUND_ZERO(sm) (!(sm).frames && !(sm).seconds && !(sm).minutes && !(sm).hours)
20 #define Timecode_IS_ZERO(sm) (!(sm).frames && !(sm).seconds && !(sm).minutes && !(sm).hours && !(sm.subframes))
26 #include "temporal/time.h"
30 double Time::default_rate = 30.0;
33 /** Increment @a timecode by exactly one sample (keep subframes value).
35 * @return true if seconds wrap.
38 increment (Time& timecode, uint32_t subframes_per_frame)
42 if (timecode.negative) {
43 if (Timecode_IS_AROUND_ZERO (timecode) && timecode.subframes) {
44 // We have a zero transition involving only subframes
45 timecode.subframes = subframes_per_frame - timecode.subframes;
46 timecode.negative = false;
50 timecode.negative = false;
51 wrap = decrement (timecode, subframes_per_frame);
52 if (!Timecode_IS_ZERO (timecode)) {
53 timecode.negative = true;
58 switch ((int)ceil (timecode.rate)) {
60 if (timecode.frames == 23) {
66 if (timecode.frames == 24) {
73 if (timecode.frames == 29) {
74 if (((timecode.minutes + 1) % 10) && (timecode.seconds == 59)) {
84 if (timecode.frames == 29) {
91 if (timecode.frames == 59) {
98 if (wrap == SECONDS) {
99 if (timecode.seconds == 59) {
100 timecode.seconds = 0;
102 if (timecode.minutes == 59) {
103 timecode.minutes = 0;
120 /** Decrement @a timecode by exactly one sample (keep subframes value)
122 * @return true if seconds wrap. */
124 decrement (Time& timecode, uint32_t subframes_per_frame)
128 if (timecode.negative || Timecode_IS_ZERO (timecode)) {
129 timecode.negative = false;
130 wrap = increment (timecode, subframes_per_frame);
131 timecode.negative = true;
133 } else if (Timecode_IS_AROUND_ZERO (timecode) && timecode.subframes) {
134 // We have a zero transition involving only subframes
135 timecode.subframes = subframes_per_frame - timecode.subframes;
136 timecode.negative = true;
140 switch ((int)ceil (timecode.rate)) {
142 if (timecode.frames == 0) {
143 timecode.frames = 23;
148 if (timecode.frames == 0) {
149 timecode.frames = 24;
155 if ((timecode.minutes % 10) && (timecode.seconds == 0)) {
156 if (timecode.frames <= 2) {
157 timecode.frames = 29;
160 } else if (timecode.frames == 0) {
161 timecode.frames = 29;
166 if (timecode.frames == 0) {
167 timecode.frames = 29;
173 if (timecode.frames == 0) {
174 timecode.frames = 59;
180 if (wrap == SECONDS) {
181 if (timecode.seconds == 0) {
182 timecode.seconds = 59;
184 if (timecode.minutes == 0) {
185 timecode.minutes = 59;
199 if (Timecode_IS_ZERO (timecode)) {
200 timecode.negative = false;
207 /** Go to lowest absolute subframe value in this sample (set to 0 :-)) */
209 frames_floot (Time& timecode)
211 timecode.subframes = 0;
212 if (Timecode_IS_ZERO (timecode)) {
213 timecode.negative = false;
218 /** Increment @a timecode by one subframe */
220 increment_subframes (Time& timecode, uint32_t subframes_per_frame)
224 if (timecode.negative) {
225 timecode.negative = false;
226 wrap = decrement_subframes (timecode, subframes_per_frame);
227 if (!Timecode_IS_ZERO (timecode)) {
228 timecode.negative = true;
233 timecode.subframes++;
234 if (timecode.subframes >= subframes_per_frame) {
235 timecode.subframes = 0;
236 increment (timecode, subframes_per_frame);
243 /** Decrement @a timecode by one subframe */
245 decrement_subframes (Time& timecode, uint32_t subframes_per_frame)
249 if (timecode.negative) {
250 timecode.negative = false;
251 wrap = increment_subframes (timecode, subframes_per_frame);
252 timecode.negative = true;
256 if (timecode.subframes <= 0) {
257 timecode.subframes = 0;
258 if (Timecode_IS_ZERO (timecode)) {
259 timecode.negative = true;
260 timecode.subframes = 1;
263 decrement (timecode, subframes_per_frame);
264 timecode.subframes = 79;
268 timecode.subframes--;
269 if (Timecode_IS_ZERO (timecode)) {
270 timecode.negative = false;
277 /** Go to next whole second (frames == 0 or frames == 2) */
279 increment_seconds (Time& timecode, uint32_t subframes_per_frame)
284 frames_floot (timecode);
286 if (timecode.negative) {
287 // Wrap second if on second boundary
288 wrap = increment (timecode, subframes_per_frame);
289 // Go to lowest absolute frame value
290 seconds_floor (timecode);
291 if (Timecode_IS_ZERO (timecode)) {
292 timecode.negative = false;
295 // Go to highest possible frame in this second
296 switch ((int)ceil (timecode.rate)) {
298 timecode.frames = 23;
301 timecode.frames = 24;
304 timecode.frames = 29;
307 timecode.frames = 59;
311 // Increment by one frame
312 wrap = increment (timecode, subframes_per_frame);
319 /** Go to lowest (absolute) frame value in this second
320 * Doesn't care about positive/negative */
322 seconds_floor (Time& timecode)
325 frames_floot (timecode);
327 // Go to lowest possible frame in this second
328 switch ((int)ceil (timecode.rate)) {
333 if (!(timecode.drop)) {
336 if ((timecode.minutes % 10) && (timecode.seconds == 0)) {
345 if (Timecode_IS_ZERO (timecode)) {
346 timecode.negative = false;
351 /** Go to next whole minute (seconds == 0, frames == 0 or frames == 2) */
353 increment_minutes (Time& timecode, uint32_t subframes_per_frame)
358 frames_floot (timecode);
360 if (timecode.negative) {
361 // Wrap if on minute boundary
362 wrap = increment_seconds (timecode, subframes_per_frame);
363 // Go to lowest possible value in this minute
364 minutes_floor (timecode);
366 // Go to highest possible second
367 timecode.seconds = 59;
368 // Wrap minute by incrementing second
369 wrap = increment_seconds (timecode, subframes_per_frame);
376 /** Go to lowest absolute value in this minute */
378 minutes_floor (Time& timecode)
380 // Go to lowest possible second
381 timecode.seconds = 0;
382 // Go to lowest possible frame
383 seconds_floor (timecode);
385 if (Timecode_IS_ZERO (timecode)) {
386 timecode.negative = false;
391 /** Go to next whole hour (minute = 0, second = 0, frame = 0) */
393 increment_hours (Time& timecode, uint32_t subframes_per_frame)
398 frames_floot (timecode);
400 if (timecode.negative) {
401 // Wrap if on hour boundary
402 wrap = increment_minutes (timecode, subframes_per_frame);
403 // Go to lowest possible value in this hour
404 hours_floor(timecode);
406 timecode.minutes = 59;
407 wrap = increment_minutes (timecode, subframes_per_frame);
414 /** Go to lowest absolute value in this hour */
416 hours_floor(Time& timecode)
418 timecode.minutes = 0;
419 timecode.seconds = 0;
421 timecode.subframes = 0;
423 if (Timecode_IS_ZERO (timecode)) {
424 timecode.negative = false;
429 timecode_to_frames_per_second(TimecodeFormat t)
433 return (24000.0/1001.0); //23.976;
441 return (25000.0/1001.0); //24.976;
449 return (30000.0/1001.0); //29.97;
452 case timecode_2997drop:
453 return (30000.0/1001.0); //29.97;
456 case timecode_2997000:
460 case timecode_2997000drop:
468 case timecode_30drop:
473 return (60000.0/1001.0); //59.94;
481 //std::cerr << "Editor received unexpected timecode type" << std::endl;
488 timecode_has_drop_frames(TimecodeFormat t)
511 case timecode_2997drop:
515 case timecode_2997000:
519 case timecode_2997000drop:
527 case timecode_30drop:
540 //error << "Editor received unexpected timecode type" << endmsg;
548 timecode_format_name (TimecodeFormat const t)
567 case timecode_2997000:
572 case timecode_2997000drop:
573 case timecode_2997drop:
581 case timecode_30drop:
600 std::string timecode_format_time (Timecode::Time TC)
604 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 "%c%02" PRIu32,
605 TC.hours, TC.minutes, TC.seconds, TC.drop ? ';' : ':', TC.frames);
607 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 "%c%02" PRIu32,
608 TC.hours, TC.minutes, TC.seconds, TC.drop ? ';' : ':', TC.frames);
610 return std::string(buf);
613 std::string timecode_format_sampletime (
615 double sample_sample_rate,
616 double timecode_frames_per_second, bool timecode_drop_frames)
620 sample, t, false, false,
621 timecode_frames_per_second, timecode_drop_frames,
624 return timecode_format_time(t);
627 bool parse_timecode_format(std::string tc, Timecode::Time &TC) {
631 if (sscanf (tc.c_str(), "%[- ]%" PRId32 ":%" PRId32 ":%" PRId32 "%[:;]%" PRId32,
632 negative, &TC.hours, &TC.minutes, &TC.seconds, ignored, &TC.frames) != 6) {
633 TC.hours = TC.minutes = TC.seconds = TC.frames = 0;
637 if (negative[0]=='-') {
647 Timecode::Time& timecode, int64_t& sample,
648 bool use_offset, bool use_subframes,
649 /* Note - framerate info is taken from Timecode::Time& */
650 double sample_sample_rate /**< may include pull up/down */,
651 uint32_t subframes_per_frame,
652 /* optional offset - can be improved: function pointer to lazily query this*/
653 bool offset_is_negative, int64_t offset_samples
656 const double samples_per_timecode_frame = (double) sample_sample_rate / (double) timecode.rate;
659 // The drop frame format was created to better approximate the 30000/1001 = 29.97002997002997....
660 // framerate of NTSC color TV. The used frame rate of drop fra,e is 29.97, which drifts by about
661 // 0.108 frame per hour, or about 1.3 frames per 12 hours. This is not perfect, but a lot better
662 // than using 30 non drop, which will drift with about 1.8 frame per minute.
663 // Using 29.97, drop frame real time can be accurate only every 10th minute (10 minutes of 29.97 fps
664 // is exactly 17982 samples). One minute is 1798.2 samples, but we count 30 frames per second
665 // (30 * 60 = 1800). This means that at the first minute boundary (at the end of 0:0:59:29) we
666 // are 1.8 framess too late relative to real time. By dropping 2 frames (jumping to 0:1:0:2) we are
667 // approx. 0.2 frames too early. This adds up with 0.2 too early for each minute until we are 1.8
668 // samples too early at 0:9:0:2 (9 * 0.2 = 1.8). The 10th minute brings us 1.8 frames later again
669 // (at end of 0:9:59:29), which sums up to 0 (we are back to zero at 0:10:0:0 :-).
673 // Timecode value frames offset subframes offset seconds (rounded) 44100 sample (rounded)
674 // 0:00:00:00 0.0 0 0.000 0 (accurate)
675 // 0:00:59:29 1.8 144 60.027 2647177
676 // 0:01:00:02 -0.2 -16 60.060 2648648
677 // 0:01:59:29 1.6 128 120.020 5292883
678 // 0:02:00:02 -0.4 -32 120.053 5294354
679 // 0:02:59:29 1.4 112 180.013 7938588
680 // 0:03:00:02 -0.6 -48 180.047 7940060
681 // 0:03:59:29 1.2 96 240.007 10584294
682 // 0:04:00:02 -0.8 -64 240.040 10585766
683 // 0:04:59:29 1.0 80 300.000 13230000
684 // 0:05:00:02 -1.0 -80 300.033 13231471
685 // 0:05:59:29 0.8 64 359.993 15875706
686 // 0:06:00:02 -1.2 -96 360.027 15877177
687 // 0:06:59:29 0.6 48 419.987 18521411
688 // 0:07:00:02 -1.4 -112 420.020 18522883
689 // 0:07:59:29 0.4 32 478.980 21167117
690 // 0:08:00:02 -1.6 -128 480.013 21168589
691 // 0:08:59:29 0.2 16 539.973 23812823
692 // 0:09:00:02 -1.8 -144 540.007 23814294
693 // 0:09:59:29 0.0+ 0+ 599.967 26458529
694 // 0:10:00:00 0.0 0 600.000 26460000 (accurate)
696 // Per Sigmond <per@sigmond.no>
698 // This schma would compensate exactly for a frame-rate of 30 * 0.999. but the
699 // actual rate is 30000/1001 - which results in an offset of -3.6ms per hour or
700 // about -86ms over a 24-hour period. (SMPTE 12M-1999)
702 // Robin Gareus <robin@gareus.org>
704 const int64_t fps_i = ceil(timecode.rate);
705 int64_t totalMinutes = 60 * timecode.hours + timecode.minutes;
706 int64_t frameNumber = fps_i * 3600 * timecode.hours
707 + fps_i * 60 * timecode.minutes
708 + fps_i * timecode.seconds + timecode.frames
709 - 2 * (totalMinutes - totalMinutes / 10);
710 sample = frameNumber * sample_sample_rate / (double) timecode.rate;
713 Non drop is easy.. just note the use of
714 rint(timecode.rate) * samples_per_timecode_frame
715 (frames per Timecode second), which is larger than
716 sample_rate() in the non-integer Timecode rate case.
719 sample = (int64_t) rint(
721 ((timecode.hours * 60 * 60) + (timecode.minutes * 60) + timecode.seconds)
723 (rint(timecode.rate) * samples_per_timecode_frame)
725 + (timecode.frames * samples_per_timecode_frame)
730 sample += (int64_t) rint(((double)timecode.subframes * samples_per_timecode_frame) / (double)subframes_per_frame);
734 if (offset_is_negative) {
735 if (sample >= offset_samples) {
736 sample -= offset_samples;
738 /* Prevent song-time from becoming negative */
742 if (timecode.negative) {
743 if (sample <= offset_samples) {
744 sample = offset_samples - sample;
749 sample += offset_samples;
758 int64_t sample, Timecode::Time& timecode,
759 bool use_offset, bool use_subframes,
761 double timecode_frames_per_second,
762 bool timecode_drop_frames,
763 double sample_sample_rate/**< can include pull up/down */,
764 uint32_t subframes_per_frame,
765 /* optional offset - can be improved: function pointer to lazily query this*/
766 bool offset_is_negative, int64_t offset_samples
769 int64_t offset_sample;
772 timecode.negative = (sample < 0);
773 offset_sample = ::llabs(sample);
775 if (offset_is_negative) {
776 offset_sample = sample + offset_samples;
777 timecode.negative = false;
779 if (sample < offset_samples) {
780 offset_sample = (offset_samples - sample);
781 timecode.negative = true;
783 offset_sample = sample - offset_samples;
784 timecode.negative = false;
789 if (timecode_drop_frames) {
790 int64_t frameNumber = floor( (double)offset_sample * timecode_frames_per_second / sample_sample_rate);
792 /* there are 17982 samples in 10 min @ 29.97df */
793 const int64_t D = frameNumber / 17982;
794 const int64_t M = frameNumber % 17982;
796 timecode.subframes = rint(subframes_per_frame
797 * ((double)offset_sample * timecode_frames_per_second / sample_sample_rate - (double)frameNumber));
799 if (timecode.subframes == subframes_per_frame) {
800 timecode.subframes = 0;
804 frameNumber += 18*D + 2*((M - 2) / 1798);
806 timecode.frames = frameNumber % 30;
807 timecode.seconds = (frameNumber / 30) % 60;
808 timecode.minutes = ((frameNumber / 30) / 60) % 60;
809 timecode.hours = (((frameNumber / 30) / 60) / 60);
812 double timecode_frames_left_exact;
813 double timecode_frames_fraction;
814 int64_t timecode_frames_left;
815 const double samples_per_timecode_frame = sample_sample_rate / timecode_frames_per_second;
816 const int64_t frames_per_hour = (int64_t)(3600. * rint(timecode_frames_per_second) * samples_per_timecode_frame);
818 timecode.hours = offset_sample / frames_per_hour;
820 // Extract whole hours. Do this to prevent rounding errors with
821 // high sample numbers in the calculations that follow.
822 timecode_frames_left_exact = (double)(offset_sample % frames_per_hour) / samples_per_timecode_frame;
823 timecode_frames_fraction = timecode_frames_left_exact - floor( timecode_frames_left_exact );
825 timecode.subframes = (int32_t) rint(timecode_frames_fraction * subframes_per_frame);
826 timecode_frames_left = (int64_t) floor (timecode_frames_left_exact);
828 if (use_subframes && timecode.subframes == subframes_per_frame) {
829 timecode_frames_left++;
830 timecode.subframes = 0;
833 timecode.minutes = timecode_frames_left / ((int32_t) lrint (timecode_frames_per_second) * 60);
834 timecode_frames_left = timecode_frames_left % ((int32_t) lrint (timecode_frames_per_second) * 60);
835 timecode.seconds = timecode_frames_left / (int32_t) lrint(timecode_frames_per_second);
836 timecode.frames = timecode_frames_left % (int32_t) lrint(timecode_frames_per_second);
839 if (!use_subframes) {
840 timecode.subframes = 0;
842 /* set frame rate and drop sample */
843 timecode.rate = timecode_frames_per_second;
844 timecode.drop = timecode_drop_frames;
847 } // namespace Timecode
850 operator<<(std::ostream& ostr, const Timecode::Time& t)
852 return t.print (ostr);