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>
5 * This file is part of FFmpeg.
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.
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.
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
25 * @see https://en.wikipedia.org/wiki/SMPTE_time_code
26 * @see http://www.dropframetimecode.org
34 #ifdef FF_API_OLD_TC_ADJUST_FRAMENUM
35 int av_timecode_adjust_ntsc_framenum(int framenum)
37 /* only works for NTSC 29.97 */
38 int d = framenum / 17982;
39 int m = framenum % 17982;
40 //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
41 return framenum + 18 * d + 2 * ((m - 2) / 1798);
45 int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
47 /* only works for NTSC 29.97 and 59.94 */
49 int d = framenum / 17982;
50 int m = framenum % 17982;
59 //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
60 return framenum + 9 * drop_frames * d + drop_frames * ((m - 2) / 1798);
63 uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
65 unsigned fps = tc->fps;
66 int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
69 framenum += tc->start;
71 framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
73 ss = framenum / fps % 60;
74 mm = framenum / (fps*60) % 60;
75 hh = framenum / (fps*3600) % 24;
76 return 0 << 31 | // color frame flag (0: unsync mode, 1: sync mode)
77 drop << 30 | // drop frame flag (0: non drop, 1: drop)
78 (ff / 10) << 28 | // tens of frames
79 (ff % 10) << 24 | // units of frames
80 0 << 23 | // PC (NTSC) or BGF0 (PAL)
81 (ss / 10) << 20 | // tens of seconds
82 (ss % 10) << 16 | // units of seconds
83 0 << 15 | // BGF0 (NTSC) or BGF2 (PAL)
84 (mm / 10) << 12 | // tens of minutes
85 (mm % 10) << 8 | // units of minutes
86 0 << 7 | // BGF2 (NTSC) or PC (PAL)
88 (hh / 10) << 4 | // tens of hours
89 (hh % 10); // units of hours
92 char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
95 int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
96 int hh, mm, ss, ff, neg = 0;
98 framenum += tc->start;
100 framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
102 framenum = -framenum;
103 neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
106 ss = framenum / fps % 60;
107 mm = framenum / (fps*60) % 60;
108 hh = framenum / (fps*3600);
109 if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
111 snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
113 hh, mm, ss, drop ? ';' : ':', ff);
117 static unsigned bcd2uint(uint8_t bcd)
119 unsigned low = bcd & 0xf;
120 unsigned high = bcd >> 4;
121 if (low > 9 || high > 9)
123 return low + 10*high;
126 char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
128 unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
129 unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
130 unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
131 unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
132 unsigned drop = tcsmpte & 1<<30 && !prevent_df; // 1-bit drop if not arbitrary bit
133 snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
134 hh, mm, ss, drop ? ';' : ':', ff);
138 char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
140 snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
141 tc25bit>>19 & 0x1f, // 5-bit hours
142 tc25bit>>13 & 0x3f, // 6-bit minutes
143 tc25bit>>6 & 0x3f, // 6-bit seconds
144 tc25bit & 1<<24 ? ';' : ':', // 1-bit drop flag
145 tc25bit & 0x3f); // 6-bit frames
149 static int check_fps(int fps)
152 static const int supported_fps[] = {24, 25, 30, 50, 60};
154 for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
155 if (fps == supported_fps[i])
160 static int check_timecode(void *log_ctx, AVTimecode *tc)
163 av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
164 return AVERROR(EINVAL);
166 if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps != 30) {
167 av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n");
168 return AVERROR(EINVAL);
170 if (check_fps(tc->fps) < 0) {
171 av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate %d/%d not supported\n",
172 tc->rate.num, tc->rate.den);
173 return AVERROR_PATCHWELCOME;
178 static int fps_from_frame_rate(AVRational rate)
180 if (!rate.den || !rate.num)
182 return (rate.num + rate.den/2) / rate.den;
185 int av_timecode_check_frame_rate(AVRational rate)
187 return check_fps(fps_from_frame_rate(rate));
190 int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
192 memset(tc, 0, sizeof(*tc));
193 tc->start = frame_start;
196 tc->fps = fps_from_frame_rate(rate);
197 return check_timecode(log_ctx, tc);
200 int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
203 int hh, mm, ss, ff, ret;
205 if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
206 av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
207 "syntax: hh:mm:ss[:;.]ff\n");
208 return AVERROR_INVALIDDATA;
211 memset(tc, 0, sizeof(*tc));
212 tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
214 tc->fps = fps_from_frame_rate(rate);
216 ret = check_timecode(log_ctx, tc);
220 tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
221 if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */
222 int tmins = 60*hh + mm;
223 tc->start -= 2 * (tmins - tmins/10);