FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/timecode.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 118 137 86.1%
Functions: 13 15 86.7%
Branches: 50 70 71.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
3 * Copyright (c) 2011-2012 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Timecode helpers
25 * @see https://en.wikipedia.org/wiki/SMPTE_time_code
26 * @see http://www.dropframetimecode.org
27 */
28
29 #include <stdio.h>
30 #include "common.h"
31 #include "timecode.h"
32 #include "log.h"
33 #include "error.h"
34
35 3536 int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
36 {
37 /* only works for multiples of NTSC 29.97 */
38 3536 int drop_frames = 0;
39 int d, m, frames_per_10mins;
40
41
2/4
✓ Branch 0 taken 3536 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3536 times.
✗ Branch 3 not taken.
3536 if (fps && fps % 30 == 0) {
42 3536 drop_frames = fps / 30 * 2;
43 3536 frames_per_10mins = fps / 30 * 17982;
44 } else
45 return framenum;
46
47 3536 d = framenum / frames_per_10mins;
48 3536 m = framenum % frames_per_10mins;
49
50 3536 return framenum + 9U * drop_frames * d + drop_frames * ((m - drop_frames) / (frames_per_10mins / 10));
51 }
52
53 270142 uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
54 {
55 270142 unsigned fps = tc->fps;
56 270142 int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
57 int hh, mm, ss, ff;
58
59 270142 framenum += tc->start;
60
2/2
✓ Branch 0 taken 3515 times.
✓ Branch 1 taken 266627 times.
270142 if (drop)
61 3515 framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
62 270142 ff = framenum % fps;
63 270142 ss = framenum / fps % 60;
64 270142 mm = framenum / (fps*60) % 60;
65 270142 hh = framenum / (fps*3600) % 24;
66 270142 return av_timecode_get_smpte(tc->rate, drop, hh, mm, ss, ff);
67 }
68
69 271115 uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff)
70 {
71 271115 uint32_t tc = 0;
72
73 /* For SMPTE 12-M timecodes, frame count is a special case if > 30 FPS.
74 See SMPTE ST 12-1:2014 Sec 12.1 for more info. */
75
2/2
✓ Branch 1 taken 57625 times.
✓ Branch 2 taken 213490 times.
271115 if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
76
2/2
✓ Branch 0 taken 28802 times.
✓ Branch 1 taken 28823 times.
57625 if (ff % 2 == 1) {
77
2/2
✓ Branch 1 taken 28800 times.
✓ Branch 2 taken 2 times.
28802 if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
78 28800 tc |= (1 << 7);
79 else
80 2 tc |= (1 << 23);
81 }
82 57625 ff /= 2;
83 }
84
85 271115 hh = hh % 24;
86 271115 mm = av_clip(mm, 0, 59);
87 271115 ss = av_clip(ss, 0, 59);
88 271115 ff = ff % 40;
89
90 271115 tc |= drop << 30;
91 271115 tc |= (ff / 10) << 28;
92 271115 tc |= (ff % 10) << 24;
93 271115 tc |= (ss / 10) << 20;
94 271115 tc |= (ss % 10) << 16;
95 271115 tc |= (mm / 10) << 12;
96 271115 tc |= (mm % 10) << 8;
97 271115 tc |= (hh / 10) << 4;
98 271115 tc |= (hh % 10);
99
100 271115 return tc;
101 }
102
103 126 char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
104 {
105 126 int fps = tc->fps;
106 126 int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
107 126 int hh, mm, ss, ff, ff_len, neg = 0;
108
109 126 framenum += tc->start;
110
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 114 times.
126 if (drop)
111 12 framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (framenum < 0) {
113 framenum = -framenum;
114 neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
115 }
116 126 ff = framenum % fps;
117 126 ss = framenum / fps % 60;
118 126 mm = framenum / (fps*60LL) % 60;
119 126 hh = framenum / (fps*3600LL);
120
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 125 times.
126 if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
121 1 hh = hh % 24;
122
4/8
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 126 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 126 times.
✗ Branch 7 not taken.
126 ff_len = fps > 10000 ? 5 : fps > 1000 ? 4 : fps > 100 ? 3 : fps > 10 ? 2 : 1;
123
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 126 times.
126 snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%0*d",
124 neg ? "-" : "",
125 hh, mm, ss, drop ? ';' : ':', ff_len, ff);
126 126 return buf;
127 }
128
129 4080 static unsigned bcd2uint(uint8_t bcd)
130 {
131 4080 unsigned low = bcd & 0xf;
132 4080 unsigned high = bcd >> 4;
133
2/4
✓ Branch 0 taken 4080 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4080 times.
4080 if (low > 9 || high > 9)
134 return 0;
135 4080 return low + 10*high;
136 }
137
138 1020 char *av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
139 {
140 1020 unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
141 1020 unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
142 1020 unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
143 1020 unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
144
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1018 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
1020 unsigned drop = tcsmpte & 1<<30 && !prevent_df; // 1-bit drop if not arbitrary bit
145
146
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 979 times.
1020 if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
147 41 ff <<= 1;
148
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 4 times.
41 if (!skip_field) {
149
1/2
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
37 if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
150 37 ff += !!(tcsmpte & 1 << 7);
151 else
152 ff += !!(tcsmpte & 1 << 23);
153 }
154 }
155
156
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1018 times.
1020 snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
157 hh, mm, ss, drop ? ';' : ':', ff);
158 1020 return buf;
159
160 }
161
162 char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
163 {
164 return av_timecode_make_smpte_tc_string2(buf, (AVRational){30, 1}, tcsmpte, prevent_df, 1);
165 }
166
167 371 char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
168 {
169 371 snprintf(buf, AV_TIMECODE_STR_SIZE,
170 "%02"PRIu32":%02"PRIu32":%02"PRIu32"%c%02"PRIu32,
171 371 tc25bit>>19 & 0x1f, // 5-bit hours
172 371 tc25bit>>13 & 0x3f, // 6-bit minutes
173 371 tc25bit>>6 & 0x3f, // 6-bit seconds
174
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 322 times.
371 tc25bit & 1<<24 ? ';' : ':', // 1-bit drop flag
175 tc25bit & 0x3f); // 6-bit frames
176 371 return buf;
177 }
178
179 182 static int check_fps(int fps)
180 {
181 int i;
182 static const int supported_fps[] = {
183 24, 25, 30, 48, 50, 60, 100, 120, 150,
184 };
185
186
1/2
✓ Branch 0 taken 423 times.
✗ Branch 1 not taken.
423 for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
187
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 241 times.
423 if (fps == supported_fps[i])
188 182 return 0;
189 return -1;
190 }
191
192 182 static int check_timecode(void *log_ctx, AVTimecode *tc)
193 {
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
182 if ((int)tc->fps <= 0) {
195 av_log(log_ctx, AV_LOG_ERROR, "Valid timecode frame rate must be specified. Minimum value is 1\n");
196 return AVERROR(EINVAL);
197 }
198
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 157 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
182 if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps % 30 != 0) {
199 av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with multiples of 30000/1001 FPS\n");
200 return AVERROR(EINVAL);
201 }
202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 182 times.
182 if (check_fps(tc->fps) < 0) {
203 av_log(log_ctx, AV_LOG_WARNING, "Using non-standard frame rate %d/%d\n",
204 tc->rate.num, tc->rate.den);
205 }
206 182 return 0;
207 }
208
209 182 static int fps_from_frame_rate(AVRational rate)
210 {
211
2/4
✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 182 times.
182 if (!rate.den || !rate.num)
212 return -1;
213 182 return (rate.num + rate.den/2) / rate.den;
214 }
215
216 int av_timecode_check_frame_rate(AVRational rate)
217 {
218 return check_fps(fps_from_frame_rate(rate));
219 }
220
221 154 int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
222 {
223 154 memset(tc, 0, sizeof(*tc));
224 154 tc->start = frame_start;
225 154 tc->flags = flags;
226 154 tc->rate = rate;
227 154 tc->fps = fps_from_frame_rate(rate);
228 154 return check_timecode(log_ctx, tc);
229 }
230
231 28 int av_timecode_init_from_components(AVTimecode *tc, AVRational rate, int flags, int hh, int mm, int ss, int ff, void *log_ctx)
232 {
233 int ret;
234
235 28 memset(tc, 0, sizeof(*tc));
236 28 tc->flags = flags;
237 28 tc->rate = rate;
238 28 tc->fps = fps_from_frame_rate(rate);
239
240 28 ret = check_timecode(log_ctx, tc);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
242 return ret;
243
244 28 tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
245
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 15 times.
28 if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */
246 13 int tmins = 60*hh + mm;
247 13 tc->start -= (tc->fps / 30 * 2) * (tmins - tmins/10);
248 }
249 28 return 0;
250 }
251
252 28 int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
253 {
254 char c;
255 int hh, mm, ss, ff, flags;
256
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
258 av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
259 "syntax: hh:mm:ss[:;.]ff\n");
260 return AVERROR_INVALIDDATA;
261 }
262 28 flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
263
264 28 return av_timecode_init_from_components(tc, rate, flags, hh, mm, ss, ff, log_ctx);
265 }
266