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))
25 #include "timecode/time.h"
29 double Time::default_rate = 30.0;
32 /** Increment @a timecode by exactly one frame (keep subframes value).
34 * @return true if seconds wrap.
37 increment (Time& timecode, uint32_t subframes_per_frame)
41 if (timecode.negative) {
42 if (Timecode_IS_AROUND_ZERO (timecode) && timecode.subframes) {
43 // We have a zero transition involving only subframes
44 timecode.subframes = subframes_per_frame - timecode.subframes;
45 timecode.negative = false;
49 timecode.negative = false;
50 wrap = decrement (timecode, subframes_per_frame);
51 if (!Timecode_IS_ZERO (timecode)) {
52 timecode.negative = true;
57 switch ((int)ceil (timecode.rate)) {
59 if (timecode.frames == 23) {
65 if (timecode.frames == 24) {
72 if (timecode.frames == 29) {
73 if (((timecode.minutes + 1) % 10) && (timecode.seconds == 59)) {
83 if (timecode.frames == 29) {
90 if (timecode.frames == 59) {
97 if (wrap == SECONDS) {
98 if (timecode.seconds == 59) {
101 if (timecode.minutes == 59) {
102 timecode.minutes = 0;
119 /** Decrement @a timecode by exactly one frame (keep subframes value)
121 * @return true if seconds wrap. */
123 decrement (Time& timecode, uint32_t subframes_per_frame)
127 if (timecode.negative || Timecode_IS_ZERO (timecode)) {
128 timecode.negative = false;
129 wrap = increment (timecode, subframes_per_frame);
130 timecode.negative = true;
132 } else if (Timecode_IS_AROUND_ZERO (timecode) && timecode.subframes) {
133 // We have a zero transition involving only subframes
134 timecode.subframes = subframes_per_frame - timecode.subframes;
135 timecode.negative = true;
139 switch ((int)ceil (timecode.rate)) {
141 if (timecode.frames == 0) {
142 timecode.frames = 23;
147 if (timecode.frames == 0) {
148 timecode.frames = 24;
154 if ((timecode.minutes % 10) && (timecode.seconds == 0)) {
155 if (timecode.frames <= 2) {
156 timecode.frames = 29;
159 } else if (timecode.frames == 0) {
160 timecode.frames = 29;
165 if (timecode.frames == 0) {
166 timecode.frames = 29;
172 if (timecode.frames == 0) {
173 timecode.frames = 59;
179 if (wrap == SECONDS) {
180 if (timecode.seconds == 0) {
181 timecode.seconds = 59;
183 if (timecode.minutes == 0) {
184 timecode.minutes = 59;
198 if (Timecode_IS_ZERO (timecode)) {
199 timecode.negative = false;
206 /** Go to lowest absolute subframe value in this frame (set to 0 :-)) */
208 frames_floor (Time& timecode)
210 timecode.subframes = 0;
211 if (Timecode_IS_ZERO (timecode)) {
212 timecode.negative = false;
217 /** Increment @a timecode by one subframe */
219 increment_subframes (Time& timecode, uint32_t subframes_per_frame)
223 if (timecode.negative) {
224 timecode.negative = false;
225 wrap = decrement_subframes (timecode, subframes_per_frame);
226 if (!Timecode_IS_ZERO (timecode)) {
227 timecode.negative = true;
232 timecode.subframes++;
233 if (timecode.subframes >= subframes_per_frame) {
234 timecode.subframes = 0;
235 increment (timecode, subframes_per_frame);
242 /** Decrement @a timecode by one subframe */
244 decrement_subframes (Time& timecode, uint32_t subframes_per_frame)
248 if (timecode.negative) {
249 timecode.negative = false;
250 wrap = increment_subframes (timecode, subframes_per_frame);
251 timecode.negative = true;
255 if (timecode.subframes <= 0) {
256 timecode.subframes = 0;
257 if (Timecode_IS_ZERO (timecode)) {
258 timecode.negative = true;
259 timecode.subframes = 1;
262 decrement (timecode, subframes_per_frame);
263 timecode.subframes = 79;
267 timecode.subframes--;
268 if (Timecode_IS_ZERO (timecode)) {
269 timecode.negative = false;
276 /** Go to next whole second (frames == 0 or frames == 2) */
278 increment_seconds (Time& timecode, uint32_t subframes_per_frame)
283 frames_floor (timecode);
285 if (timecode.negative) {
286 // Wrap second if on second boundary
287 wrap = increment (timecode, subframes_per_frame);
288 // Go to lowest absolute frame value
289 seconds_floor (timecode);
290 if (Timecode_IS_ZERO (timecode)) {
291 timecode.negative = false;
294 // Go to highest possible frame in this second
295 switch ((int)ceil (timecode.rate)) {
297 timecode.frames = 23;
300 timecode.frames = 24;
303 timecode.frames = 29;
306 timecode.frames = 59;
310 // Increment by one frame
311 wrap = increment (timecode, subframes_per_frame);
318 /** Go to lowest (absolute) frame value in this second
319 * Doesn't care about positive/negative */
321 seconds_floor (Time& timecode)
324 frames_floor (timecode);
326 // Go to lowest possible frame in this second
327 switch ((int)ceil (timecode.rate)) {
332 if (!(timecode.drop)) {
335 if ((timecode.minutes % 10) && (timecode.seconds == 0)) {
344 if (Timecode_IS_ZERO (timecode)) {
345 timecode.negative = false;
350 /** Go to next whole minute (seconds == 0, frames == 0 or frames == 2) */
352 increment_minutes (Time& timecode, uint32_t subframes_per_frame)
357 frames_floor (timecode);
359 if (timecode.negative) {
360 // Wrap if on minute boundary
361 wrap = increment_seconds (timecode, subframes_per_frame);
362 // Go to lowest possible value in this minute
363 minutes_floor (timecode);
365 // Go to highest possible second
366 timecode.seconds = 59;
367 // Wrap minute by incrementing second
368 wrap = increment_seconds (timecode, subframes_per_frame);
375 /** Go to lowest absolute value in this minute */
377 minutes_floor (Time& timecode)
379 // Go to lowest possible second
380 timecode.seconds = 0;
381 // Go to lowest possible frame
382 seconds_floor (timecode);
384 if (Timecode_IS_ZERO (timecode)) {
385 timecode.negative = false;
390 /** Go to next whole hour (minute = 0, second = 0, frame = 0) */
392 increment_hours (Time& timecode, uint32_t subframes_per_frame)
397 frames_floor (timecode);
399 if (timecode.negative) {
400 // Wrap if on hour boundary
401 wrap = increment_minutes (timecode, subframes_per_frame);
402 // Go to lowest possible value in this hour
403 hours_floor(timecode);
405 timecode.minutes = 59;
406 wrap = increment_minutes (timecode, subframes_per_frame);
413 /** Go to lowest absolute value in this hour */
415 hours_floor(Time& timecode)
417 timecode.minutes = 0;
418 timecode.seconds = 0;
420 timecode.subframes = 0;
422 if (Timecode_IS_ZERO (timecode)) {
423 timecode.negative = false;
428 timecode_to_frames_per_second(TimecodeFormat t)
432 return (24000.0/1001.0); //23.976;
440 return (25000.0/1001.0); //24.976;
448 return (30000.0/1001.0); //29.97;
451 case timecode_2997drop:
452 return (30000.0/1001.0); //29.97;
455 case timecode_2997000:
459 case timecode_2997000drop:
467 case timecode_30drop:
472 return (60000.0/1001.0); //59.94;
480 //std::cerr << "Editor received unexpected timecode type" << std::endl;
487 timecode_has_drop_frames(TimecodeFormat t)
510 case timecode_2997drop:
514 case timecode_2997000:
518 case timecode_2997000drop:
526 case timecode_30drop:
539 //error << "Editor received unexpected timecode type" << endmsg;
547 timecode_format_name (TimecodeFormat const t)
570 case timecode_2997drop:
574 case timecode_2997000:
578 case timecode_2997000drop:
579 return "29.97000 drop";
586 case timecode_30drop:
605 std::string timecode_format_time (Timecode::Time TC)
609 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 "%c%02" PRIu32,
610 TC.hours, TC.minutes, TC.seconds, TC.drop ? ';' : ':', TC.frames);
612 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 "%c%02" PRIu32,
613 TC.hours, TC.minutes, TC.seconds, TC.drop ? ';' : ':', TC.frames);
615 return std::string(buf);
618 std::string timecode_format_sampletime (
620 double sample_frame_rate,
621 double timecode_frames_per_second, bool timecode_drop_frames)
625 sample, t, false, false,
626 timecode_frames_per_second, timecode_drop_frames,
629 return timecode_format_time(t);
634 Timecode::Time& timecode, int64_t& sample,
635 bool use_offset, bool use_subframes,
636 /* Note - framerate info is taken from Timecode::Time& */
637 double sample_frame_rate /**< may include pull up/down */,
638 uint32_t subframes_per_frame,
639 /* optional offset - can be improved: function pointer to lazily query this*/
640 bool offset_is_negative, int64_t offset_samples
643 const double frames_per_timecode_frame = (double) sample_frame_rate / (double) timecode.rate;
646 // The drop frame format was created to better approximate the 30000/1001 = 29.97002997002997....
647 // framerate of NTSC color TV. The used frame rate of drop frame is 29.97, which drifts by about
648 // 0.108 frame per hour, or about 1.3 frames per 12 hours. This is not perfect, but a lot better
649 // than using 30 non drop, which will drift with about 1.8 frame per minute.
650 // Using 29.97, drop frame real time can be accurate only every 10th minute (10 minutes of 29.97 fps
651 // is exactly 17982 frames). One minute is 1798.2 frames, but we count 30 frames per second
652 // (30 * 60 = 1800). This means that at the first minute boundary (at the end of 0:0:59:29) we
653 // are 1.8 frames too late relative to real time. By dropping 2 frames (jumping to 0:1:0:2) we are
654 // approx. 0.2 frames too early. This adds up with 0.2 too early for each minute until we are 1.8
655 // frames too early at 0:9:0:2 (9 * 0.2 = 1.8). The 10th minute brings us 1.8 frames later again
656 // (at end of 0:9:59:29), which sums up to 0 (we are back to zero at 0:10:0:0 :-).
660 // Timecode value frames offset subframes offset seconds (rounded) 44100 sample (rounded)
661 // 0:00:00:00 0.0 0 0.000 0 (accurate)
662 // 0:00:59:29 1.8 144 60.027 2647177
663 // 0:01:00:02 -0.2 -16 60.060 2648648
664 // 0:01:59:29 1.6 128 120.020 5292883
665 // 0:02:00:02 -0.4 -32 120.053 5294354
666 // 0:02:59:29 1.4 112 180.013 7938588
667 // 0:03:00:02 -0.6 -48 180.047 7940060
668 // 0:03:59:29 1.2 96 240.007 10584294
669 // 0:04:00:02 -0.8 -64 240.040 10585766
670 // 0:04:59:29 1.0 80 300.000 13230000
671 // 0:05:00:02 -1.0 -80 300.033 13231471
672 // 0:05:59:29 0.8 64 359.993 15875706
673 // 0:06:00:02 -1.2 -96 360.027 15877177
674 // 0:06:59:29 0.6 48 419.987 18521411
675 // 0:07:00:02 -1.4 -112 420.020 18522883
676 // 0:07:59:29 0.4 32 478.980 21167117
677 // 0:08:00:02 -1.6 -128 480.013 21168589
678 // 0:08:59:29 0.2 16 539.973 23812823
679 // 0:09:00:02 -1.8 -144 540.007 23814294
680 // 0:09:59:29 0.0+ 0+ 599.967 26458529
681 // 0:10:00:00 0.0 0 600.000 26460000 (accurate)
683 // Per Sigmond <per@sigmond.no>
685 // Samples inside time dividable by 10 minutes (real time accurate)
686 int64_t base_samples = (int64_t) (((timecode.hours * 107892) + ((timecode.minutes / 10) * 17982)) * frames_per_timecode_frame);
688 // Samples inside time exceeding the nearest 10 minutes (always offset, see above)
689 int32_t exceeding_df_minutes = timecode.minutes % 10;
690 int32_t exceeding_df_seconds = (exceeding_df_minutes * 60) + timecode.seconds;
691 int32_t exceeding_df_frames = (30 * exceeding_df_seconds) + timecode.frames - (2 * exceeding_df_minutes);
692 int64_t exceeding_samples = (int64_t) rint(exceeding_df_frames * frames_per_timecode_frame);
693 sample = base_samples + exceeding_samples;
696 Non drop is easy.. just note the use of
697 rint(timecode.rate) * frames_per_timecode_frame
698 (frames per Timecode second), which is larger than
699 frame_rate() in the non-integer Timecode rate case.
702 sample = (int64_t)rint((((timecode.hours * 60 * 60) + (timecode.minutes * 60) + timecode.seconds) * (rint(timecode.rate) * frames_per_timecode_frame)) + (timecode.frames * frames_per_timecode_frame));
706 sample += (int64_t) (((double)timecode.subframes * frames_per_timecode_frame) / subframes_per_frame);
710 if (offset_is_negative) {
711 if (sample >= offset_samples) {
712 sample -= offset_samples;
714 /* Prevent song-time from becoming negative */
718 if (timecode.negative) {
719 if (sample <= offset_samples) {
720 sample = offset_samples - sample;
725 sample += offset_samples;
734 int64_t sample, Timecode::Time& timecode,
735 bool use_offset, bool use_subframes,
737 double timecode_frames_per_second,
738 bool timecode_drop_frames,
739 double sample_frame_rate/**< can include pull up/down */,
740 uint32_t subframes_per_frame,
741 /* optional offset - can be improved: function pointer to lazily query this*/
742 bool offset_is_negative, int64_t offset_samples
745 const double frames_per_timecode_frame = (double) sample_frame_rate / (double) timecode_frames_per_second;
746 int32_t frames_per_hour;
748 if (timecode_drop_frames) {
749 frames_per_hour = (int32_t)(107892 * frames_per_timecode_frame);
751 frames_per_hour = (int32_t)(3600 * rint(timecode_frames_per_second) * frames_per_timecode_frame);
755 int64_t offset_sample;
758 offset_sample = sample;
759 timecode.negative = false;
761 if (offset_is_negative) {
762 offset_sample = sample + offset_samples;
763 timecode.negative = false;
765 if (sample < offset_samples) {
766 offset_sample = (offset_samples - sample);
767 timecode.negative = true;
769 offset_sample = sample - offset_samples;
770 timecode.negative = false;
775 double timecode_frames_left_exact;
776 double timecode_frames_fraction;
777 uint64_t timecode_frames_left;
779 // Extract whole hours. Do this to prevent rounding errors with
780 // high sample numbers in the calculations that follow.
781 timecode.hours = offset_sample / frames_per_hour;
782 offset_sample = offset_sample % frames_per_hour;
784 // Calculate exact number of (exceeding) timecode frames and fractional frames
785 timecode_frames_left_exact = (double) offset_sample / frames_per_timecode_frame;
786 timecode_frames_fraction = timecode_frames_left_exact - floor( timecode_frames_left_exact );
787 timecode.subframes = (int32_t) floor(timecode_frames_fraction * subframes_per_frame);
789 // XXX Not sure if this is necessary anymore...
790 if (timecode.subframes == subframes_per_frame) {
791 // This can happen with 24 fps (and 29.97 fps ?)
792 timecode_frames_left_exact = ceil( timecode_frames_left_exact );
793 timecode.subframes = 0;
796 // Extract hour-exceeding frames for minute, second and frame calculations
797 timecode_frames_left = (uint64_t) floor (timecode_frames_left_exact);
799 if (timecode_drop_frames) {
800 // See int32_t explanation in timecode_to_sample()...
802 // Number of 10 minute chunks
803 timecode.minutes = (timecode_frames_left / 17982) * 10; // exactly 17982 frames in 10 minutes
804 // frames exceeding the nearest 10 minute barrier
805 int32_t exceeding_df_frames = timecode_frames_left % 17982;
807 // Find minutes exceeding the nearest 10 minute barrier
808 if (exceeding_df_frames >= 1800) { // nothing to do if we are inside the first minute (0-1799)
809 exceeding_df_frames -= 1800; // take away first minute (different number of frames than the others)
810 int32_t extra_minutes_minus_1 = exceeding_df_frames / 1798; // how many minutes after the first one
811 exceeding_df_frames -= extra_minutes_minus_1 * 1798; // take away the (extra) minutes just found
812 timecode.minutes += extra_minutes_minus_1 + 1; // update with exceeding minutes
815 // Adjust frame numbering for dropped frames (frame 0 and 1 skipped at start of every minute except every 10th)
816 if (timecode.minutes % 10) {
817 // Every minute except every 10th
818 if (exceeding_df_frames < 28) {
819 // First second, frames 0 and 1 are skipped
820 timecode.seconds = 0;
821 timecode.frames = exceeding_df_frames + 2;
823 // All other seconds, all 30 frames are counted
824 exceeding_df_frames -= 28;
825 timecode.seconds = (exceeding_df_frames / 30) + 1;
826 timecode.frames = exceeding_df_frames % 30;
829 // Every 10th minute, all 30 frames counted in all seconds
830 timecode.seconds = exceeding_df_frames / 30;
831 timecode.frames = exceeding_df_frames % 30;
835 timecode.minutes = timecode_frames_left / ((int32_t) rint (timecode_frames_per_second) * 60);
836 timecode_frames_left = timecode_frames_left % ((int32_t) rint (timecode_frames_per_second) * 60);
837 timecode.seconds = timecode_frames_left / (int32_t) rint(timecode_frames_per_second);
838 timecode.frames = timecode_frames_left % (int32_t) rint(timecode_frames_per_second);
841 if (!use_subframes) {
842 timecode.subframes = 0;
844 /* set frame rate and drop frame */
845 timecode.rate = timecode_frames_per_second;
846 timecode.drop = timecode_drop_frames;
849 } // namespace Timecode
852 operator<<(std::ostream& ostr, const Timecode::Time& t)
854 return t.print (ostr);