Blender V5.3
timecode.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstdio>
12
13#include "BLI_math_base_c.hh"
14#include "BLI_string.hh"
15
16#include "BLI_timecode.hh" /* own include */
17
18#include "DNA_userdef_types.h" /* for eTimecodeStyles only */
19
20#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
21
22namespace blender {
23
25 const size_t maxncpy,
26 const int brevity_level,
27 const float time_seconds,
28 const double fps,
29 const short timecode_style)
30{
31 int hours = 0, minutes = 0, seconds = 0, frames = 0;
32 float time = time_seconds;
33 char neg[2] = {'\0'};
34 size_t rlen;
35
36 /* Get frames. */
37 if (time < 0) {
38 /* Correction for negative frames. */
39 neg[0] = '-';
40 time = -time;
41 }
42
43 if (time >= 3600.0f) {
44 /* hours */
45 /* XXX should we only display a single digit for hours since clips are
46 * VERY UNLIKELY to be more than 1-2 hours max? However, that would
47 * go against conventions...
48 */
49 hours = int(time) / 3600;
50 time = fmodf(time, 3600);
51 }
52
53 if (time >= 60.0f) {
54 /* minutes */
55 minutes = int(time) / 60;
56 time = fmodf(time, 60);
57 }
58
59 if (brevity_level <= 0) {
60 /* seconds + frames
61 * Frames are derived from 'fraction' of second. We need to perform some additional rounding
62 * to cope with 'half' frames, etc., which should be fine in most cases
63 */
64 seconds = int(time);
65 frames = round_fl_to_int(float((double(time) - double(seconds)) * fps));
66 }
67 else {
68 /* seconds (with pixel offset rounding) */
69 seconds = round_fl_to_int(time);
70 }
71
72 switch (timecode_style) {
74 /* - In general, minutes and seconds should be shown, as most clips will be
75 * within this length. Hours will only be included if relevant.
76 * - Only show frames when zoomed in enough for them to be relevant
77 * (using separator of '+' for frames).
78 * When showing frames, use slightly different display
79 * to avoid confusion with `mm:ss` format.
80 */
81 if (brevity_level <= 0) {
82 /* include "frames" in display */
83 if (hours) {
84 rlen = BLI_snprintf_rlen(
85 str, maxncpy, "%s%02d:%02d:%02d+%02d", neg, hours, minutes, seconds, frames);
86 }
87 else if (minutes) {
88 rlen = BLI_snprintf_rlen(
89 str, maxncpy, "%s%02d:%02d+%02d", neg, minutes, seconds, frames);
90 }
91 else {
92 rlen = BLI_snprintf_rlen(str, maxncpy, "%s00:%02d+%02d", neg, seconds, frames);
93 }
94 }
95 else {
96 /* don't include 'frames' in display */
97 if (hours) {
98 rlen = BLI_snprintf_rlen(str, maxncpy, "%s%02d:%02d:%02d", neg, hours, minutes, seconds);
99 }
100 else {
101 rlen = BLI_snprintf_rlen(str, maxncpy, "%s%02d:%02d", neg, minutes, seconds);
102 }
103 }
104 break;
105 }
107 /* reduced SMPTE format that always shows minutes, seconds, frames.
108 * Hours only shown as needed. */
109 if (hours) {
110 rlen = BLI_snprintf_rlen(
111 str, maxncpy, "%s%02d:%02d:%02d:%02d", neg, hours, minutes, seconds, frames);
112 }
113 else {
114 rlen = BLI_snprintf_rlen(str, maxncpy, "%s%02d:%02d:%02d", neg, minutes, seconds, frames);
115 }
116 break;
117 }
119 /* reduced SMPTE. Instead of frames, milliseconds are shown */
120
121 /* precision of decimal part */
122 const int ms_dp = (brevity_level <= 0) ? (1 - brevity_level) : 1;
123
124 /* to get 2 digit whole-number part for seconds display
125 * (i.e. 3 is for 2 digits + radix, on top of full length) */
126 const int s_pad = ms_dp + 3;
127
128 if (hours) {
129 rlen = BLI_snprintf_rlen(
130 str, maxncpy, "%s%02d:%02d:%0*.*f", neg, hours, minutes, s_pad, ms_dp, time);
131 }
132 else {
133 rlen = BLI_snprintf_rlen(str, maxncpy, "%s%02d:%0*.*f", neg, minutes, s_pad, ms_dp, time);
134 }
135 break;
136 }
138 /* SubRip, like SMPTE milliseconds but seconds and milliseconds
139 * are separated by a comma, not a dot... */
140
141 /* precision of decimal part */
142 const int ms_dp = (brevity_level <= 0) ? (1 - brevity_level) : 1;
143 const int ms = round_fl_to_int((time - float(seconds)) * 1000.0f);
144
145 rlen = BLI_snprintf_rlen(
146 str, maxncpy, "%s%02d:%02d:%02d,%0*d", neg, hours, minutes, seconds, ms_dp, ms);
147 break;
148 }
150 /* only show the original seconds display */
151 /* round to whole numbers if brevity_level is >= 1 (i.e. scale is coarse) */
152 if (brevity_level <= 0) {
153 rlen = BLI_snprintf_rlen(str, maxncpy, "%.*f", 1 - brevity_level, time_seconds);
154 }
155 else {
156 rlen = BLI_snprintf_rlen(str, maxncpy, "%d", round_fl_to_int(time_seconds));
157 }
158 break;
159 }
161 default: {
162 /* full SMPTE format */
163 rlen = BLI_snprintf_rlen(
164 str, maxncpy, "%s%02d:%02d:%02d:%02d", neg, hours, minutes, seconds, frames);
165 break;
166 }
167 }
168
169 return rlen;
170}
171
173 const size_t maxncpy,
174 const double time_seconds)
175{
176 size_t rlen;
177
178 /* format 00:00:00.00 (hr:min:sec) string has to be 12 long */
179 const int hr = int(time_seconds) / (60 * 60);
180 const int min = (int(time_seconds) / 60) % 60;
181 const int sec = int(time_seconds) % 60;
182 const int hun = int(fmod(time_seconds, 1.0) * 100);
183
184 if (hr) {
185 rlen = BLI_snprintf_rlen(str, maxncpy, "%.2d:%.2d:%.2d.%.2d", hr, min, sec, hun);
186 }
187 else {
188 rlen = BLI_snprintf_rlen(str, maxncpy, "%.2d:%.2d.%.2d", min, sec, hun);
189 }
190
191 return rlen;
192}
193
195 const size_t maxncpy,
196 const int brevity_level,
197 const float time_seconds)
198{
199 size_t rlen;
200
201 /* round to whole numbers if brevity_level is >= 1 (i.e. scale is coarse) */
202 if (brevity_level <= 0) {
203 rlen = BLI_snprintf_rlen(str, maxncpy, "%.*f", 1 - brevity_level, time_seconds);
204 }
205 else {
206 rlen = BLI_snprintf_rlen(str, maxncpy, "%d", round_fl_to_int(time_seconds));
207 }
208
209 return rlen;
210}
211
212} // namespace blender
#define fmodf(x, y)
#define str(s)
ccl_device_inline float2 fmod(const float2 a, const float b)
BMesh Iterators.
MINLINE int round_fl_to_int(float a)
size_t BLI_snprintf_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
size_t BLI_timecode_string_from_time_seconds(char *str, size_t maxncpy, int brevity_level, float time_seconds) ATTR_NONNULL()
Definition timecode.cc:194
size_t BLI_timecode_string_from_time(char *str, size_t maxncpy, int brevity_level, float time_seconds, double fps, short timecode_style) ATTR_NONNULL()
Definition timecode.cc:24
size_t BLI_timecode_string_from_time_simple(char *str, size_t maxncpy, double time_seconds) ATTR_NONNULL()
Definition timecode.cc:172
@ USER_TIMECODE_SECONDS_ONLY
@ USER_TIMECODE_MILLISECONDS
#define min(a, b)
Definition sort.cc:38