| 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 "timecode_internal.h" | ||
| 33 | #include "log.h" | ||
| 34 | #include "error.h" | ||
| 35 | |||
| 36 | 3866 | int av_timecode_adjust_ntsc_framenum2(int framenum, int fps) | |
| 37 | { | ||
| 38 | /* only works for multiples of NTSC 29.97 */ | ||
| 39 | 3866 | int drop_frames = 0; | |
| 40 | int d, m, frames_per_10mins; | ||
| 41 | |||
| 42 |
2/4✓ Branch 0 taken 3866 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3866 times.
✗ Branch 3 not taken.
|
3866 | if (fps && fps % 30 == 0) { |
| 43 | 3866 | drop_frames = fps / 30 * 2; | |
| 44 | 3866 | frames_per_10mins = fps / 30 * 17982; | |
| 45 | } else | ||
| 46 | ✗ | return framenum; | |
| 47 | |||
| 48 | 3866 | d = framenum / frames_per_10mins; | |
| 49 | 3866 | m = framenum % frames_per_10mins; | |
| 50 | |||
| 51 | 3866 | return framenum + 9U * drop_frames * d + drop_frames * ((m - drop_frames) / (frames_per_10mins / 10)); | |
| 52 | } | ||
| 53 | |||
| 54 | 270142 | uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum) | |
| 55 | { | ||
| 56 | 270142 | unsigned fps = tc->fps; | |
| 57 | 270142 | int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME); | |
| 58 | int hh, mm, ss, ff; | ||
| 59 | |||
| 60 | 270142 | framenum += tc->start; | |
| 61 |
2/2✓ Branch 0 taken 3515 times.
✓ Branch 1 taken 266627 times.
|
270142 | if (drop) |
| 62 | 3515 | framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps); | |
| 63 | 270142 | ff = framenum % fps; | |
| 64 | 270142 | ss = framenum / fps % 60; | |
| 65 | 270142 | mm = framenum / (fps*60LL) % 60; | |
| 66 | 270142 | hh = framenum / (fps*3600LL) % 24; | |
| 67 | 270142 | return av_timecode_get_smpte(tc->rate, drop, hh, mm, ss, ff); | |
| 68 | } | ||
| 69 | |||
| 70 | 271119 | uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff) | |
| 71 | { | ||
| 72 | 271119 | uint32_t tc = 0; | |
| 73 | |||
| 74 | /* For SMPTE 12-M timecodes, frame count is a special case if > 30 FPS. | ||
| 75 | See SMPTE ST 12-1:2014 Sec 12.1 for more info. */ | ||
| 76 |
2/2✓ Branch 1 taken 57628 times.
✓ Branch 2 taken 213491 times.
|
271119 | if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) { |
| 77 |
2/2✓ Branch 0 taken 28802 times.
✓ Branch 1 taken 28826 times.
|
57628 | if (ff % 2 == 1) { |
| 78 |
2/2✓ Branch 1 taken 28800 times.
✓ Branch 2 taken 2 times.
|
28802 | if (av_cmp_q(rate, (AVRational) {50, 1}) == 0) |
| 79 | 28800 | tc |= (1 << 7); | |
| 80 | else | ||
| 81 | 2 | tc |= (1 << 23); | |
| 82 | } | ||
| 83 | 57628 | ff /= 2; | |
| 84 | } | ||
| 85 | |||
| 86 | 271119 | hh = hh % 24; | |
| 87 | 271119 | mm = av_clip(mm, 0, 59); | |
| 88 | 271119 | ss = av_clip(ss, 0, 59); | |
| 89 | 271119 | ff = ff % 40; | |
| 90 | |||
| 91 | 271119 | tc |= drop << 30; | |
| 92 | 271119 | tc |= (ff / 10) << 28; | |
| 93 | 271119 | tc |= (ff % 10) << 24; | |
| 94 | 271119 | tc |= (ss / 10) << 20; | |
| 95 | 271119 | tc |= (ss % 10) << 16; | |
| 96 | 271119 | tc |= (mm / 10) << 12; | |
| 97 | 271119 | tc |= (mm % 10) << 8; | |
| 98 | 271119 | tc |= (hh / 10) << 4; | |
| 99 | 271119 | tc |= (hh % 10); | |
| 100 | |||
| 101 | 271119 | return tc; | |
| 102 | } | ||
| 103 | |||
| 104 | 456 | char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum_arg) | |
| 105 | { | ||
| 106 | 456 | int fps = tc->fps; | |
| 107 | 456 | int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME; | |
| 108 | 456 | int hh, mm, ss, ff, ff_len, neg = 0; | |
| 109 | 456 | int64_t framenum = framenum_arg; | |
| 110 | |||
| 111 | 456 | framenum += tc->start; | |
| 112 |
2/2✓ Branch 0 taken 342 times.
✓ Branch 1 taken 114 times.
|
456 | if (drop) |
| 113 | 342 | framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps); | |
| 114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
|
456 | if (framenum < 0) { |
| 115 | ✗ | framenum = -framenum; | |
| 116 | ✗ | neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE; | |
| 117 | } | ||
| 118 | 456 | ff = framenum % fps; | |
| 119 | 456 | ss = framenum / fps % 60; | |
| 120 | 456 | mm = framenum / (fps*60LL) % 60; | |
| 121 | 456 | hh = framenum / (fps*3600LL); | |
| 122 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 455 times.
|
456 | if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX) |
| 123 | 1 | hh = hh % 24; | |
| 124 |
4/8✓ Branch 0 taken 456 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 456 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 456 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 456 times.
✗ Branch 7 not taken.
|
456 | ff_len = fps > 10000 ? 5 : fps > 1000 ? 4 : fps > 100 ? 3 : fps > 10 ? 2 : 1; |
| 125 |
3/4✓ Branch 0 taken 342 times.
✓ Branch 1 taken 114 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 456 times.
|
456 | snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%0*d", |
| 126 | neg ? "-" : "", | ||
| 127 | hh, mm, ss, drop ? ';' : ':', ff_len, ff); | ||
| 128 | 456 | return buf; | |
| 129 | } | ||
| 130 | |||
| 131 | 1027 | char *av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field) | |
| 132 | { | ||
| 133 | unsigned hh, mm, ss, ff, drop; | ||
| 134 | 1027 | ff_timecode_set_smpte(&drop, &hh, &mm, &ss, &ff, rate, tcsmpte, prevent_df, skip_field); | |
| 135 | |||
| 136 | 1027 | snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u", | |
| 137 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1025 times.
|
1027 | hh, mm, ss, drop ? ';' : ':', ff); |
| 138 | 1027 | return buf; | |
| 139 | |||
| 140 | } | ||
| 141 | |||
| 142 | ✗ | char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df) | |
| 143 | { | ||
| 144 | ✗ | return av_timecode_make_smpte_tc_string2(buf, (AVRational){30, 1}, tcsmpte, prevent_df, 1); | |
| 145 | } | ||
| 146 | |||
| 147 | 379 | char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit) | |
| 148 | { | ||
| 149 | 379 | snprintf(buf, AV_TIMECODE_STR_SIZE, | |
| 150 | "%02"PRIu32":%02"PRIu32":%02"PRIu32"%c%02"PRIu32, | ||
| 151 | 379 | tc25bit>>19 & 0x1f, // 5-bit hours | |
| 152 | 379 | tc25bit>>13 & 0x3f, // 6-bit minutes | |
| 153 | 379 | tc25bit>>6 & 0x3f, // 6-bit seconds | |
| 154 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 330 times.
|
379 | tc25bit & 1<<24 ? ';' : ':', // 1-bit drop flag |
| 155 | tc25bit & 0x3f); // 6-bit frames | ||
| 156 | 379 | return buf; | |
| 157 | } | ||
| 158 | |||
| 159 | 400 | static int check_fps(int fps) | |
| 160 | { | ||
| 161 | int i; | ||
| 162 | static const int supported_fps[] = { | ||
| 163 | 24, 25, 30, 48, 50, 60, 100, 120, 150, | ||
| 164 | }; | ||
| 165 | |||
| 166 |
1/2✓ Branch 0 taken 1076 times.
✗ Branch 1 not taken.
|
1076 | for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++) |
| 167 |
2/2✓ Branch 0 taken 400 times.
✓ Branch 1 taken 676 times.
|
1076 | if (fps == supported_fps[i]) |
| 168 | 400 | return 0; | |
| 169 | ✗ | return -1; | |
| 170 | } | ||
| 171 | |||
| 172 | 400 | static int check_timecode(void *log_ctx, AVTimecode *tc) | |
| 173 | { | ||
| 174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
|
400 | if ((int)tc->fps <= 0) { |
| 175 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Valid timecode frame rate must be specified. Minimum value is 1\n"); | |
| 176 | ✗ | return AVERROR(EINVAL); | |
| 177 | } | ||
| 178 |
3/4✓ Branch 0 taken 235 times.
✓ Branch 1 taken 165 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 235 times.
|
400 | if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps % 30 != 0) { |
| 179 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with multiples of 30000/1001 FPS\n"); | |
| 180 | ✗ | return AVERROR(EINVAL); | |
| 181 | } | ||
| 182 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
|
400 | if (check_fps(tc->fps) < 0) { |
| 183 | ✗ | av_log(log_ctx, AV_LOG_WARNING, "Using non-standard frame rate %d/%d\n", | |
| 184 | tc->rate.num, tc->rate.den); | ||
| 185 | } | ||
| 186 | 400 | return 0; | |
| 187 | } | ||
| 188 | |||
| 189 | 400 | static int fps_from_frame_rate(AVRational rate) | |
| 190 | { | ||
| 191 |
2/4✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 400 times.
|
400 | if (!rate.den || !rate.num) |
| 192 | ✗ | return -1; | |
| 193 | 400 | return (rate.num + rate.den/2LL) / rate.den; | |
| 194 | } | ||
| 195 | |||
| 196 | ✗ | int av_timecode_check_frame_rate(AVRational rate) | |
| 197 | { | ||
| 198 | ✗ | return check_fps(fps_from_frame_rate(rate)); | |
| 199 | } | ||
| 200 | |||
| 201 | 167 | int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx) | |
| 202 | { | ||
| 203 | 167 | memset(tc, 0, sizeof(*tc)); | |
| 204 | 167 | tc->start = frame_start; | |
| 205 | 167 | tc->flags = flags; | |
| 206 | 167 | tc->rate = rate; | |
| 207 | 167 | tc->fps = fps_from_frame_rate(rate); | |
| 208 | 167 | return check_timecode(log_ctx, tc); | |
| 209 | } | ||
| 210 | |||
| 211 | 233 | int av_timecode_init_from_components(AVTimecode *tc, AVRational rate, int flags, int hh, int mm, int ss, int ff, void *log_ctx) | |
| 212 | { | ||
| 213 | int ret; | ||
| 214 | |||
| 215 | 233 | memset(tc, 0, sizeof(*tc)); | |
| 216 | 233 | tc->flags = flags; | |
| 217 | 233 | tc->rate = rate; | |
| 218 | 233 | tc->fps = fps_from_frame_rate(rate); | |
| 219 | |||
| 220 | 233 | ret = check_timecode(log_ctx, tc); | |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 233 times.
|
233 | if (ret < 0) |
| 222 | ✗ | return ret; | |
| 223 | |||
| 224 | 233 | tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff; | |
| 225 |
2/2✓ Branch 0 taken 214 times.
✓ Branch 1 taken 19 times.
|
233 | if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */ |
| 226 | 214 | int tmins = 60*hh + mm; | |
| 227 | 214 | tc->start -= (tc->fps / 30 * 2) * (tmins - tmins/10); | |
| 228 | } | ||
| 229 | 233 | return 0; | |
| 230 | } | ||
| 231 | |||
| 232 | 28 | int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx) | |
| 233 | { | ||
| 234 | char c; | ||
| 235 | int hh, mm, ss, ff, flags; | ||
| 236 | |||
| 237 |
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) { |
| 238 | ✗ | av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, " | |
| 239 | "syntax: hh:mm:ss[:;.]ff\n"); | ||
| 240 | ✗ | return AVERROR_INVALIDDATA; | |
| 241 | } | ||
| 242 | 28 | flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ... | |
| 243 | |||
| 244 | 28 | return av_timecode_init_from_components(tc, rate, flags, hh, mm, ss, ff, log_ctx); | |
| 245 | } | ||
| 246 |