move sample/timecode conversion to libtimecode.
[ardour.git] / libs / timecode / src / time.cc
1 /*
2   Copyright (C) 2006-2010 Paul Davis
3         
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.
8   
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.
13   
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.
17 */
18
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))
21
22 #include <math.h>
23 #include <stdio.h>
24
25 #include "timecode/time.h"
26
27 namespace Timecode {
28
29 float Time::default_rate = 30.0;
30
31
32 /** Increment @a timecode by exactly one frame (keep subframes value).
33  * Realtime safe.
34  * @return true if seconds wrap.
35  */
36 Wrap
37 increment (Time& timecode, uint32_t subframes_per_frame)
38 {
39         Wrap wrap = NONE;
40
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;
46                         return SECONDS;
47                 }
48     
49                 timecode.negative = false;
50                 wrap = decrement (timecode, subframes_per_frame);
51                 if (!Timecode_IS_ZERO (timecode)) {
52                         timecode.negative = true;
53                 }
54                 return wrap;
55         }
56
57         switch ((int)ceil (timecode.rate)) {
58         case 24:
59                 if (timecode.frames == 23) {
60                         timecode.frames = 0;
61                         wrap = SECONDS;
62                 }
63                 break;
64         case 25:
65                 if (timecode.frames == 24) {
66                         timecode.frames = 0;
67                         wrap = SECONDS;
68                 }
69                 break;
70         case 30:
71                 if (timecode.drop) {
72                         if (timecode.frames == 29) {
73                                 if (((timecode.minutes + 1) % 10) && (timecode.seconds == 59)) {
74                                         timecode.frames = 2;
75                                 }
76                                 else {
77                                         timecode.frames = 0;
78                                 }
79                                 wrap = SECONDS;
80                         }
81                 } else {
82
83                         if (timecode.frames == 29) {
84                                 timecode.frames = 0;
85                                 wrap = SECONDS;
86                         }
87                 }
88                 break;
89         case 60:
90                 if (timecode.frames == 59) {
91                         timecode.frames = 0;
92                         wrap = SECONDS;
93                 }
94                 break;
95         }
96   
97         if (wrap == SECONDS) {
98                 if (timecode.seconds == 59) {
99                         timecode.seconds = 0;
100                         wrap = MINUTES;
101                         if (timecode.minutes == 59) {
102                                 timecode.minutes = 0;
103                                 wrap = HOURS;
104                                 timecode.hours++;
105                         } else {
106                                 timecode.minutes++;
107                         }
108                 } else {
109                         timecode.seconds++;
110                 }
111         } else {
112                 timecode.frames++;
113         }
114   
115         return wrap;
116 }
117
118
119 /** Decrement @a timecode by exactly one frame (keep subframes value)
120  * Realtime safe.
121  * @return true if seconds wrap. */
122 Wrap
123 decrement (Time& timecode, uint32_t subframes_per_frame)
124 {
125         Wrap wrap = NONE;
126   
127         if (timecode.negative || Timecode_IS_ZERO (timecode)) {
128                 timecode.negative = false;
129                 wrap = increment (timecode, subframes_per_frame);
130                 timecode.negative = true;
131                 return wrap;
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;
136                 return SECONDS;
137         }
138   
139         switch ((int)ceil (timecode.rate)) {
140         case 24:
141                 if (timecode.frames == 0) {
142                         timecode.frames = 23;
143                         wrap = SECONDS;
144                 }
145                 break;
146         case 25:
147                 if (timecode.frames == 0) {
148                         timecode.frames = 24;
149                         wrap = SECONDS;
150                 }
151                 break;
152         case 30:
153                 if (timecode.drop) {
154                         if ((timecode.minutes % 10) && (timecode.seconds == 0)) {
155                                 if (timecode.frames <= 2) {
156                                         timecode.frames = 29;
157                                         wrap = SECONDS;
158                                 }
159                         } else if (timecode.frames == 0) {
160                                 timecode.frames = 29;
161                                 wrap = SECONDS;
162                         }
163                         
164                 } else {
165                         if (timecode.frames == 0) {
166                                 timecode.frames = 29;
167                                 wrap = SECONDS;
168                         }
169                 }
170                 break;
171         case 60:
172                 if (timecode.frames == 0) {
173                         timecode.frames = 59;
174                         wrap = SECONDS;
175                 }
176                 break;
177         }
178   
179         if (wrap == SECONDS) {
180                 if (timecode.seconds == 0) {
181                         timecode.seconds = 59;
182                         wrap = MINUTES;
183                         if (timecode.minutes == 0) {
184                                 timecode.minutes = 59;
185                                 wrap = HOURS;
186                                 timecode.hours--;
187                         }
188                         else {
189                                 timecode.minutes--;
190                         }
191                 } else {
192                         timecode.seconds--;
193                 }
194         } else {
195                 timecode.frames--;
196         }
197   
198         if (Timecode_IS_ZERO (timecode)) {
199                 timecode.negative = false;
200         }
201   
202         return wrap;
203 }
204
205
206 /** Go to lowest absolute subframe value in this frame (set to 0 :-)) */
207 void
208 frames_floor (Time& timecode)
209 {
210         timecode.subframes = 0;
211         if (Timecode_IS_ZERO (timecode)) {
212                 timecode.negative = false;
213         }
214 }
215
216
217 /** Increment @a timecode by one subframe */
218 Wrap
219 increment_subframes (Time& timecode, uint32_t subframes_per_frame)
220 {
221         Wrap wrap = NONE;
222   
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;
228                 }
229                 return wrap;
230         }
231   
232         timecode.subframes++;
233         if (timecode.subframes >= subframes_per_frame) {
234                 timecode.subframes = 0;
235                 increment (timecode, subframes_per_frame);
236                 return FRAMES;
237         }
238         return NONE;
239 }
240
241
242 /** Decrement @a timecode by one subframe */
243 Wrap
244 decrement_subframes (Time& timecode, uint32_t subframes_per_frame)
245 {
246         Wrap wrap = NONE;
247   
248         if (timecode.negative) {
249                 timecode.negative = false;
250                 wrap = increment_subframes (timecode, subframes_per_frame);
251                 timecode.negative = true;
252                 return wrap;
253         }
254   
255         if (timecode.subframes <= 0) {
256                 timecode.subframes = 0;
257                 if (Timecode_IS_ZERO (timecode)) {
258                         timecode.negative = true;
259                         timecode.subframes = 1;
260                         return FRAMES;
261                 } else {
262                         decrement (timecode, subframes_per_frame);
263                         timecode.subframes = 79;
264                         return FRAMES;
265                 }
266         } else {
267                 timecode.subframes--;
268                 if (Timecode_IS_ZERO (timecode)) {
269                         timecode.negative = false;
270                 }
271                 return NONE;
272         }
273 }
274
275
276 /** Go to next whole second (frames == 0 or frames == 2) */
277 Wrap
278 increment_seconds (Time& timecode, uint32_t subframes_per_frame)
279 {
280         Wrap wrap = NONE;
281   
282         // Clear subframes
283         frames_floor (timecode);
284   
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;
292                 }
293         } else {
294                 // Go to highest possible frame in this second
295                 switch ((int)ceil (timecode.rate)) {
296                 case 24:
297                         timecode.frames = 23;
298                         break;
299                 case 25:
300                         timecode.frames = 24;
301                         break;
302                 case 30:
303                         timecode.frames = 29;
304                         break;
305                 case 60:
306                         timecode.frames = 59;
307                         break;
308                 }
309     
310                 // Increment by one frame
311                 wrap = increment (timecode, subframes_per_frame);
312         }
313   
314         return wrap;
315 }
316
317
318 /** Go to lowest (absolute) frame value in this second
319  * Doesn't care about positive/negative */
320 void
321 seconds_floor (Time& timecode)
322 {
323         // Clear subframes
324         frames_floor (timecode);
325   
326         // Go to lowest possible frame in this second
327         switch ((int)ceil (timecode.rate)) {
328         case 24:
329         case 25:
330         case 30:
331         case 60:
332                 if (!(timecode.drop)) {
333                         timecode.frames = 0;
334                 } else {
335                         if ((timecode.minutes % 10) && (timecode.seconds == 0)) {
336                                 timecode.frames = 2;
337                         } else {
338                                 timecode.frames = 0;
339                         }
340                 }
341                 break;
342         }
343   
344         if (Timecode_IS_ZERO (timecode)) {
345                 timecode.negative = false;
346         }
347 }
348
349
350 /** Go to next whole minute (seconds == 0, frames == 0 or frames == 2) */
351 Wrap
352 increment_minutes (Time& timecode, uint32_t subframes_per_frame)
353 {
354         Wrap wrap = NONE;
355   
356         // Clear subframes
357         frames_floor (timecode);
358   
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);
364         } else {
365                 // Go to highest possible second
366                 timecode.seconds = 59;
367                 // Wrap minute by incrementing second
368                 wrap = increment_seconds (timecode, subframes_per_frame);
369         }
370   
371         return wrap;
372 }
373
374
375 /** Go to lowest absolute value in this minute */
376 void
377 minutes_floor (Time& timecode)
378 {
379         // Go to lowest possible second
380         timecode.seconds = 0;
381         // Go to lowest possible frame
382         seconds_floor (timecode);
383
384         if (Timecode_IS_ZERO (timecode)) {
385                 timecode.negative = false;
386         }
387 }
388
389
390 /** Go to next whole hour (minute = 0, second = 0, frame = 0) */
391 Wrap
392 increment_hours (Time& timecode, uint32_t subframes_per_frame)
393 {
394         Wrap wrap = NONE;
395   
396         // Clear subframes
397         frames_floor (timecode);
398   
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);
404         } else {
405                 timecode.minutes = 59;
406                 wrap = increment_minutes (timecode, subframes_per_frame);
407         }
408   
409         return wrap;
410 }
411
412
413 /** Go to lowest absolute value in this hour */
414 void
415 hours_floor(Time& timecode)
416 {
417         timecode.minutes   = 0;
418         timecode.seconds   = 0;
419         timecode.frames    = 0;
420         timecode.subframes = 0;
421   
422         if (Timecode_IS_ZERO (timecode)) {
423                 timecode.negative = false;
424         }
425 }
426
427 float
428 timecode_to_frames_per_second(TimecodeFormat t)
429 {
430         switch (t) {
431                 case timecode_23976:
432                         return (24000.0/1001.0); //23.976;
433
434                         break;
435                 case timecode_24:
436                         return 24;
437
438                         break;
439                 case timecode_24976:
440                         return (25000.0/1001.0); //24.976;
441
442                         break;
443                 case timecode_25:
444                         return 25;
445
446                         break;
447                 case timecode_2997:
448                         return 29.97;
449
450                         break;
451                 case timecode_2997drop:
452                         return (30000.0/1001.0); //29.97;
453
454                         break;
455                 case timecode_30:
456                         return 30;
457
458                         break;
459                 case timecode_30drop:
460                         return 30;
461
462                         break;
463                 case timecode_5994:
464                         return (60000.0/1001.0); //59.94;
465
466                         break;
467                 case timecode_60:
468                         return 60;
469
470                         break;
471                 default:
472                         //std::cerr << "Editor received unexpected timecode type" << std::endl;
473                         break;
474         }
475         return 30.0;
476 }
477
478 bool
479 timecode_has_drop_frames(TimecodeFormat t)
480 {
481         switch (t) {
482                 case timecode_23976:
483                         return false;
484
485                         break;
486                 case timecode_24:
487                         return false;
488
489                         break;
490                 case timecode_24976:
491                         return false;
492
493                         break;
494                 case timecode_25:
495                         return false;
496
497                         break;
498                 case timecode_2997:
499                         return false;
500
501                         break;
502                 case timecode_2997drop:
503                         return true;
504
505                         break;
506                 case timecode_30:
507                         return false;
508
509                         break;
510                 case timecode_30drop:
511                         return true;
512
513                         break;
514                 case timecode_5994:
515                         return false;
516
517                         break;
518                 case timecode_60:
519                         return false;
520
521                         break;
522                 default:
523                         //error << "Editor received unexpected timecode type" << endmsg;
524                         break;
525         }
526
527         return false;
528 }
529
530 std::string
531 timecode_format_name (TimecodeFormat const t)
532 {
533         switch (t) {
534                 case timecode_23976:
535                         return "23.98";
536
537                         break;
538                 case timecode_24:
539                         return "24";
540
541                         break;
542                 case timecode_24976:
543                         return "24.98";
544
545                         break;
546                 case timecode_25:
547                         return "25";
548
549                         break;
550                 case timecode_2997:
551                         return "29.97";
552
553                         break;
554                 case timecode_2997drop:
555                         return "29.97 drop";
556
557                         break;
558                 case timecode_30:
559                         return "30";
560
561                         break;
562                 case timecode_30drop:
563                         return "30 drop";
564
565                         break;
566                 case timecode_5994:
567                         return "59.94";
568
569                         break;
570                 case timecode_60:
571                         return "60";
572
573                         break;
574                 default:
575                         break;
576         }
577
578         return "??";
579 }
580
581 std::string timecode_format_time (Timecode::Time& TC)
582 {
583         char buf[32];
584         if (TC.negative) {
585                 snprintf (buf, sizeof (buf), "-%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
586         } else {
587                 snprintf (buf, sizeof (buf), " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32, TC.hours, TC.minutes, TC.seconds, TC.frames);
588         }
589         return std::string(buf);
590 }
591
592 std::string timecode_format_sampletime (
593                 int64_t sample,
594                 double sample_frame_rate,
595                 double timecode_frames_per_second, bool timecode_drop_frames)
596 {
597         Time t;
598         sample_to_timecode(
599                         sample, t, false, false,
600                         timecode_frames_per_second, timecode_drop_frames,
601                         sample_frame_rate,
602                         80, false, 0);
603         return timecode_format_time(t);
604 }
605
606 void
607 timecode_to_sample(
608                 Timecode::Time& timecode, int64_t& sample,
609                 bool use_offset, bool use_subframes,
610     /* Note - framerate info is taken from Timecode::Time& */
611                 double sample_frame_rate /**< may include pull up/down */,
612                 int32_t subframes_per_frame,
613     /* optional offset  - can be improved: function pointer to lazily query this*/
614                 bool offset_is_negative, int64_t offset_samples
615                 )
616 {
617         const double frames_per_timecode_frame = (double) sample_frame_rate / (double) timecode.rate;
618
619         if (timecode.drop) {
620                 // The drop frame format was created to better approximate the 30000/1001 = 29.97002997002997....
621                 // framerate of NTSC color TV. The used frame rate of drop frame is 29.97, which drifts by about
622                 // 0.108 frame per hour, or about 1.3 frames per 12 hours. This is not perfect, but a lot better
623                 // than using 30 non drop, which will drift with about 1.8 frame per minute.
624                 // Using 29.97, drop frame real time can be accurate only every 10th minute (10 minutes of 29.97 fps
625                 // is exactly 17982 frames). One minute is 1798.2 frames, but we count 30 frames per second
626                 // (30 * 60 = 1800). This means that at the first minute boundary (at the end of 0:0:59:29) we
627                 // are 1.8 frames too late relative to real time. By dropping 2 frames (jumping to 0:1:0:2) we are
628                 // approx. 0.2 frames too early. This adds up with 0.2 too early for each minute until we are 1.8
629                 // frames too early at 0:9:0:2 (9 * 0.2 = 1.8). The 10th minute brings us 1.8 frames later again
630                 // (at end of 0:9:59:29), which sums up to 0 (we are back to zero at 0:10:0:0 :-).
631                 //
632                 // In table form:
633                 //
634                 // Timecode value    frames offset   subframes offset   seconds (rounded)  44100 sample (rounded)
635                 //  0:00:00:00        0.0             0                     0.000                0 (accurate)
636                 //  0:00:59:29        1.8           144                    60.027          2647177
637                 //  0:01:00:02       -0.2           -16                    60.060          2648648
638                 //  0:01:59:29        1.6           128                   120.020          5292883
639                 //  0:02:00:02       -0.4           -32                   120.053          5294354
640                 //  0:02:59:29        1.4           112                   180.013          7938588
641                 //  0:03:00:02       -0.6           -48                   180.047          7940060
642                 //  0:03:59:29        1.2            96                   240.007         10584294
643                 //  0:04:00:02       -0.8           -64                   240.040         10585766
644                 //  0:04:59:29        1.0            80                   300.000         13230000
645                 //  0:05:00:02       -1.0           -80                   300.033         13231471
646                 //  0:05:59:29        0.8            64                   359.993         15875706
647                 //  0:06:00:02       -1.2           -96                   360.027         15877177
648                 //  0:06:59:29        0.6            48                   419.987         18521411
649                 //  0:07:00:02       -1.4          -112                   420.020         18522883
650                 //  0:07:59:29        0.4            32                   478.980         21167117
651                 //  0:08:00:02       -1.6          -128                   480.013         21168589
652                 //  0:08:59:29        0.2            16                   539.973         23812823
653                 //  0:09:00:02       -1.8          -144                   540.007         23814294
654                 //  0:09:59:29        0.0+            0+                  599.967         26458529
655                 //  0:10:00:00        0.0             0                   600.000         26460000 (accurate)
656                 //
657                 //  Per Sigmond <per@sigmond.no>
658
659                 // Samples inside time dividable by 10 minutes (real time accurate)
660                 int64_t base_samples = (int64_t) (((timecode.hours * 107892) + ((timecode.minutes / 10) * 17982)) * frames_per_timecode_frame);
661
662                 // Samples inside time exceeding the nearest 10 minutes (always offset, see above)
663                 int32_t exceeding_df_minutes = timecode.minutes % 10;
664                 int32_t exceeding_df_seconds = (exceeding_df_minutes * 60) + timecode.seconds;
665                 int32_t exceeding_df_frames = (30 * exceeding_df_seconds) + timecode.frames - (2 * exceeding_df_minutes);
666                 int64_t exceeding_samples = (int64_t) rint(exceeding_df_frames * frames_per_timecode_frame);
667                 sample = base_samples + exceeding_samples;
668         } else {
669                 /*
670                    Non drop is easy.. just note the use of
671                    rint(timecode.rate) * frames_per_timecode_frame
672                    (frames per Timecode second), which is larger than
673                    frame_rate() in the non-integer Timecode rate case.
674                 */
675
676                 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));
677         }
678
679         if (use_subframes) {
680                 sample += (int64_t) (((double)timecode.subframes * frames_per_timecode_frame) / subframes_per_frame);
681         }
682
683         if (use_offset) {
684                 if (offset_is_negative) {
685                         if (sample >= offset_samples) {
686                                 sample -= offset_samples;
687                         } else {
688                                 /* Prevent song-time from becoming negative */
689                                 sample = 0;
690                         }
691                 } else {
692                         if (timecode.negative) {
693                                 if (sample <= offset_samples) {
694                                         sample = offset_samples - sample;
695                                 } else {
696                                         sample = 0;
697                                 }
698                         } else {
699                                 sample += offset_samples;
700                         }
701                 }
702         }
703 }
704
705
706 void
707 sample_to_timecode (
708                 int64_t sample, Timecode::Time& timecode,
709                 bool use_offset, bool use_subframes,
710     /* framerate info */
711                 double timecode_frames_per_second,
712                 bool   timecode_drop_frames,
713                 double sample_frame_rate/**< can include pull up/down */,
714                 int32_t subframes_per_frame,
715     /* optional offset  - can be improved: function pointer to lazily query this*/
716                 bool offset_is_negative, int64_t offset_samples
717                 )
718 {
719         const double frames_per_timecode_frame = (double) sample_frame_rate / (double) timecode_frames_per_second;
720         int32_t frames_per_hour;
721
722         if (timecode_drop_frames) {
723           frames_per_hour = (int32_t)(107892 * frames_per_timecode_frame);
724         } else {
725           frames_per_hour = (int32_t)(3600 * rint(timecode_frames_per_second) * frames_per_timecode_frame);
726         }
727
728   /* do the work */
729         int64_t offset_sample;
730
731         if (!use_offset) {
732                 offset_sample = sample;
733                 timecode.negative = false;
734         } else {
735                 if (offset_is_negative) {
736                         offset_sample = sample + offset_samples;
737                         timecode.negative = false;
738                 } else {
739                         if (sample < offset_samples) {
740                                 offset_sample = (offset_samples - sample);
741                                 timecode.negative = true;
742                         } else {
743                                 offset_sample =  sample - offset_samples;
744                                 timecode.negative = false;
745                         }
746                 }
747         }
748
749         double timecode_frames_left_exact;
750         double timecode_frames_fraction;
751         uint64_t timecode_frames_left;
752
753         // Extract whole hours. Do this to prevent rounding errors with
754         // high sample numbers in the calculations that follow.
755         timecode.hours = offset_sample / frames_per_hour;
756         offset_sample = offset_sample % frames_per_hour;
757
758         // Calculate exact number of (exceeding) timecode frames and fractional frames
759         timecode_frames_left_exact = (double) offset_sample / frames_per_timecode_frame;
760         timecode_frames_fraction = timecode_frames_left_exact - floor( timecode_frames_left_exact );
761         timecode.subframes = (int32_t) floor(timecode_frames_fraction * subframes_per_frame);
762
763         // XXX Not sure if this is necessary anymore...
764         if (timecode.subframes == subframes_per_frame) {
765                 // This can happen with 24 fps (and 29.97 fps ?)
766                 timecode_frames_left_exact = ceil( timecode_frames_left_exact );
767                 timecode.subframes = 0;
768         }
769
770         // Extract hour-exceeding frames for minute, second and frame calculations
771         timecode_frames_left = (uint64_t) floor (timecode_frames_left_exact);
772
773         if (timecode_drop_frames) {
774                 // See int32_t explanation in timecode_to_sample()...
775
776                 // Number of 10 minute chunks
777                 timecode.minutes = (timecode_frames_left / 17982) * 10; // exactly 17982 frames in 10 minutes
778                 // frames exceeding the nearest 10 minute barrier
779                 int32_t exceeding_df_frames = timecode_frames_left % 17982;
780
781                 // Find minutes exceeding the nearest 10 minute barrier
782                 if (exceeding_df_frames >= 1800) { // nothing to do if we are inside the first minute (0-1799)
783                         exceeding_df_frames -= 1800; // take away first minute (different number of frames than the others)
784                         int32_t extra_minutes_minus_1 = exceeding_df_frames / 1798; // how many minutes after the first one
785                         exceeding_df_frames -= extra_minutes_minus_1 * 1798; // take away the (extra) minutes just found
786                         timecode.minutes += extra_minutes_minus_1 + 1; // update with exceeding minutes
787                 }
788
789                 // Adjust frame numbering for dropped frames (frame 0 and 1 skipped at start of every minute except every 10th)
790                 if (timecode.minutes % 10) {
791                         // Every minute except every 10th
792                         if (exceeding_df_frames < 28) {
793                                 // First second, frames 0 and 1 are skipped
794                                 timecode.seconds = 0;
795                                 timecode.frames = exceeding_df_frames + 2;
796                         } else {
797                                 // All other seconds, all 30 frames are counted
798                                 exceeding_df_frames -= 28;
799                                 timecode.seconds = (exceeding_df_frames / 30) + 1;
800                                 timecode.frames = exceeding_df_frames % 30;
801                         }
802                 } else {
803                         // Every 10th minute, all 30 frames counted in all seconds
804                         timecode.seconds = exceeding_df_frames / 30;
805                         timecode.frames = exceeding_df_frames % 30;
806                 }
807         } else {
808                 // Non drop is easy
809                 timecode.minutes = timecode_frames_left / ((int32_t) rint (timecode_frames_per_second) * 60);
810                 timecode_frames_left = timecode_frames_left % ((int32_t) rint (timecode_frames_per_second) * 60);
811                 timecode.seconds = timecode_frames_left / (int32_t) rint(timecode_frames_per_second);
812                 timecode.frames = timecode_frames_left % (int32_t) rint(timecode_frames_per_second);
813         }
814
815         if (!use_subframes) {
816                 timecode.subframes = 0;
817         }
818         /* set frame rate and drop frame */
819         timecode.rate = timecode_frames_per_second;
820         timecode.drop = timecode_drop_frames;
821 }
822
823 } // namespace Timecode
824
825 std::ostream& 
826 operator<<(std::ostream& ostr, const Timecode::Time& t) 
827 {
828         return t.print (ostr);
829 }