FFmpeg  4.4
timecode.c
Go to the documentation of this file.
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 "timecode.h"
31 #include "log.h"
32 #include "error.h"
33 
34 int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
35 {
36  /* only works for multiples of NTSC 29.97 */
37  int drop_frames = 0;
38  int d, m, frames_per_10mins;
39 
40  if (fps && fps % 30 == 0) {
41  drop_frames = fps / 30 * 2;
42  frames_per_10mins = fps / 30 * 17982;
43  } else
44  return framenum;
45 
46  d = framenum / frames_per_10mins;
47  m = framenum % frames_per_10mins;
48 
49  return framenum + 9U * drop_frames * d + drop_frames * ((m - drop_frames) / (frames_per_10mins / 10));
50 }
51 
52 uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
53 {
54  unsigned fps = tc->fps;
55  int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
56  int hh, mm, ss, ff;
57 
58  framenum += tc->start;
59  if (drop)
60  framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
61  ff = framenum % fps;
62  ss = framenum / fps % 60;
63  mm = framenum / (fps*60) % 60;
64  hh = framenum / (fps*3600) % 24;
65  return av_timecode_get_smpte(tc->rate, drop, hh, mm, ss, ff);
66 }
67 
68 uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff)
69 {
70  uint32_t tc = 0;
71 
72  /* For SMPTE 12-M timecodes, frame count is a special case if > 30 FPS.
73  See SMPTE ST 12-1:2014 Sec 12.1 for more info. */
74  if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
75  if (ff % 2 == 1) {
76  if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
77  tc |= (1 << 7);
78  else
79  tc |= (1 << 23);
80  }
81  ff /= 2;
82  }
83 
84  hh = hh % 24;
85  mm = av_clip(mm, 0, 59);
86  ss = av_clip(ss, 0, 59);
87  ff = ff % 40;
88 
89  tc |= drop << 30;
90  tc |= (ff / 10) << 28;
91  tc |= (ff % 10) << 24;
92  tc |= (ss / 10) << 20;
93  tc |= (ss % 10) << 16;
94  tc |= (mm / 10) << 12;
95  tc |= (mm % 10) << 8;
96  tc |= (hh / 10) << 4;
97  tc |= (hh % 10);
98 
99  return tc;
100 }
101 
102 char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
103 {
104  int fps = tc->fps;
105  int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
106  int hh, mm, ss, ff, neg = 0;
107 
108  framenum += tc->start;
109  if (drop)
110  framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
111  if (framenum < 0) {
112  framenum = -framenum;
113  neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
114  }
115  ff = framenum % fps;
116  ss = framenum / fps % 60;
117  mm = framenum / (fps*60LL) % 60;
118  hh = framenum / (fps*3600LL);
119  if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
120  hh = hh % 24;
121  snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
122  neg ? "-" : "",
123  hh, mm, ss, drop ? ';' : ':', ff);
124  return buf;
125 }
126 
127 static unsigned bcd2uint(uint8_t bcd)
128 {
129  unsigned low = bcd & 0xf;
130  unsigned high = bcd >> 4;
131  if (low > 9 || high > 9)
132  return 0;
133  return low + 10*high;
134 }
135 
136 char *av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
137 {
138  unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
139  unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
140  unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
141  unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
142  unsigned drop = tcsmpte & 1<<30 && !prevent_df; // 1-bit drop if not arbitrary bit
143 
144  if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
145  ff <<= 1;
146  if (!skip_field) {
147  if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
148  ff += !!(tcsmpte & 1 << 7);
149  else
150  ff += !!(tcsmpte & 1 << 23);
151  }
152  }
153 
154  snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
155  hh, mm, ss, drop ? ';' : ':', ff);
156  return buf;
157 
158 }
159 
160 char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
161 {
162  return av_timecode_make_smpte_tc_string2(buf, (AVRational){30, 1}, tcsmpte, prevent_df, 1);
163 }
164 
165 char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
166 {
168  "%02"PRIu32":%02"PRIu32":%02"PRIu32"%c%02"PRIu32,
169  tc25bit>>19 & 0x1f, // 5-bit hours
170  tc25bit>>13 & 0x3f, // 6-bit minutes
171  tc25bit>>6 & 0x3f, // 6-bit seconds
172  tc25bit & 1<<24 ? ';' : ':', // 1-bit drop flag
173  tc25bit & 0x3f); // 6-bit frames
174  return buf;
175 }
176 
177 static int check_fps(int fps)
178 {
179  int i;
180  static const int supported_fps[] = {
181  24, 25, 30, 48, 50, 60, 100, 120, 150,
182  };
183 
184  for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
185  if (fps == supported_fps[i])
186  return 0;
187  return -1;
188 }
189 
190 static int check_timecode(void *log_ctx, AVTimecode *tc)
191 {
192  if ((int)tc->fps <= 0) {
193  av_log(log_ctx, AV_LOG_ERROR, "Valid timecode frame rate must be specified. Minimum value is 1\n");
194  return AVERROR(EINVAL);
195  }
196  if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps % 30 != 0) {
197  av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with multiples of 30000/1001 FPS\n");
198  return AVERROR(EINVAL);
199  }
200  if (check_fps(tc->fps) < 0) {
201  av_log(log_ctx, AV_LOG_WARNING, "Using non-standard frame rate %d/%d\n",
202  tc->rate.num, tc->rate.den);
203  }
204  return 0;
205 }
206 
208 {
209  if (!rate.den || !rate.num)
210  return -1;
211  return (rate.num + rate.den/2) / rate.den;
212 }
213 
215 {
216  return check_fps(fps_from_frame_rate(rate));
217 }
218 
219 int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
220 {
221  memset(tc, 0, sizeof(*tc));
222  tc->start = frame_start;
223  tc->flags = flags;
224  tc->rate = rate;
225  tc->fps = fps_from_frame_rate(rate);
226  return check_timecode(log_ctx, tc);
227 }
228 
229 int av_timecode_init_from_components(AVTimecode *tc, AVRational rate, int flags, int hh, int mm, int ss, int ff, void *log_ctx)
230 {
231  int ret;
232 
233  memset(tc, 0, sizeof(*tc));
234  tc->flags = flags;
235  tc->rate = rate;
236  tc->fps = fps_from_frame_rate(rate);
237 
238  ret = check_timecode(log_ctx, tc);
239  if (ret < 0)
240  return ret;
241 
242  tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
243  if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */
244  int tmins = 60*hh + mm;
245  tc->start -= (tc->fps / 30 * 2) * (tmins - tmins/10);
246  }
247  return 0;
248 }
249 
250 int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
251 {
252  char c;
253  int hh, mm, ss, ff, flags;
254 
255  if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
256  av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
257  "syntax: hh:mm:ss[:;.]ff\n");
258  return AVERROR_INVALIDDATA;
259  }
260  flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
261 
262  return av_timecode_init_from_components(tc, rate, flags, hh, mm, ss, ff, log_ctx);
263 }
uint8_t
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
#define av_clip
Definition: common.h:122
error code definitions
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
int i
Definition: input.c:407
static int frame_start(MpegEncContext *s)
#define tc
Definition: regdef.h:69
#define FF_ARRAY_ELEMS(a)
#define snprintf
Definition: snprintf.h:34
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
#define av_log(a,...)
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
Parse timecode representation (hh:mm:ss[:;.
Definition: timecode.c:250
static unsigned bcd2uint(uint8_t bcd)
Definition: timecode.c:127
char * av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
Get the timecode string from the SMPTE timecode format.
Definition: timecode.c:160
static int check_timecode(void *log_ctx, AVTimecode *tc)
Definition: timecode.c:190
uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff)
Convert sei info to SMPTE 12M binary representation.
Definition: timecode.c:68
char * av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
Get the timecode string from the 25-bit timecode format (MPEG GOP format).
Definition: timecode.c:165
char * av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
Load timecode string in buf.
Definition: timecode.c:102
char * av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
Get the timecode string from the SMPTE timecode format.
Definition: timecode.c:136
int av_timecode_init_from_components(AVTimecode *tc, AVRational rate, int flags, int hh, int mm, int ss, int ff, void *log_ctx)
Init a timecode struct from the passed timecode components.
Definition: timecode.c:229
static int check_fps(int fps)
Definition: timecode.c:177
int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
Init a timecode struct with the passed parameters.
Definition: timecode.c:219
static int fps_from_frame_rate(AVRational rate)
Definition: timecode.c:207
int av_timecode_check_frame_rate(AVRational rate)
Check if the timecode feature is available for the given frame rate.
Definition: timecode.c:214
uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
Convert frame number to SMPTE 12M binary representation.
Definition: timecode.c:52
int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
Adjust frame number for NTSC drop frame time code.
Definition: timecode.c:34
Timecode helpers header.
#define AV_TIMECODE_STR_SIZE
Definition: timecode.h:33
@ AV_TIMECODE_FLAG_ALLOWNEGATIVE
negative time values are allowed
Definition: timecode.h:38
@ AV_TIMECODE_FLAG_DROPFRAME
timecode is drop frame
Definition: timecode.h:36
@ AV_TIMECODE_FLAG_24HOURSMAX
timecode wraps after 24 hours
Definition: timecode.h:37
if(ret< 0)
Definition: vf_mcdeint.c:282
static double c[64]