yading@11
|
1 /*
|
yading@11
|
2 * Copyright (c) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
|
yading@11
|
3 * Copyright (c) 2011-2012 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com>
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 /**
|
yading@11
|
23 * @file
|
yading@11
|
24 * Timecode helpers
|
yading@11
|
25 * @see https://en.wikipedia.org/wiki/SMPTE_time_code
|
yading@11
|
26 * @see http://www.dropframetimecode.org
|
yading@11
|
27 */
|
yading@11
|
28
|
yading@11
|
29 #include <stdio.h>
|
yading@11
|
30 #include "timecode.h"
|
yading@11
|
31 #include "log.h"
|
yading@11
|
32 #include "error.h"
|
yading@11
|
33
|
yading@11
|
34 int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
|
yading@11
|
35 {
|
yading@11
|
36 /* only works for NTSC 29.97 and 59.94 */
|
yading@11
|
37 int drop_frames = 0;
|
yading@11
|
38 int d, m, frames_per_10mins;
|
yading@11
|
39
|
yading@11
|
40 if (fps == 30) {
|
yading@11
|
41 drop_frames = 2;
|
yading@11
|
42 frames_per_10mins = 17982;
|
yading@11
|
43 } else if (fps == 60) {
|
yading@11
|
44 drop_frames = 4;
|
yading@11
|
45 frames_per_10mins = 35964;
|
yading@11
|
46 } else
|
yading@11
|
47 return framenum;
|
yading@11
|
48
|
yading@11
|
49 d = framenum / frames_per_10mins;
|
yading@11
|
50 m = framenum % frames_per_10mins;
|
yading@11
|
51
|
yading@11
|
52 return framenum + 9 * drop_frames * d + drop_frames * ((m - drop_frames) / (frames_per_10mins / 10));
|
yading@11
|
53 }
|
yading@11
|
54
|
yading@11
|
55 uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
|
yading@11
|
56 {
|
yading@11
|
57 unsigned fps = tc->fps;
|
yading@11
|
58 int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
|
yading@11
|
59 int hh, mm, ss, ff;
|
yading@11
|
60
|
yading@11
|
61 framenum += tc->start;
|
yading@11
|
62 if (drop)
|
yading@11
|
63 framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
|
yading@11
|
64 ff = framenum % fps;
|
yading@11
|
65 ss = framenum / fps % 60;
|
yading@11
|
66 mm = framenum / (fps*60) % 60;
|
yading@11
|
67 hh = framenum / (fps*3600) % 24;
|
yading@11
|
68 return 0 << 31 | // color frame flag (0: unsync mode, 1: sync mode)
|
yading@11
|
69 drop << 30 | // drop frame flag (0: non drop, 1: drop)
|
yading@11
|
70 (ff / 10) << 28 | // tens of frames
|
yading@11
|
71 (ff % 10) << 24 | // units of frames
|
yading@11
|
72 0 << 23 | // PC (NTSC) or BGF0 (PAL)
|
yading@11
|
73 (ss / 10) << 20 | // tens of seconds
|
yading@11
|
74 (ss % 10) << 16 | // units of seconds
|
yading@11
|
75 0 << 15 | // BGF0 (NTSC) or BGF2 (PAL)
|
yading@11
|
76 (mm / 10) << 12 | // tens of minutes
|
yading@11
|
77 (mm % 10) << 8 | // units of minutes
|
yading@11
|
78 0 << 7 | // BGF2 (NTSC) or PC (PAL)
|
yading@11
|
79 0 << 6 | // BGF1
|
yading@11
|
80 (hh / 10) << 4 | // tens of hours
|
yading@11
|
81 (hh % 10); // units of hours
|
yading@11
|
82 }
|
yading@11
|
83
|
yading@11
|
84 char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
|
yading@11
|
85 {
|
yading@11
|
86 int fps = tc->fps;
|
yading@11
|
87 int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
|
yading@11
|
88 int hh, mm, ss, ff, neg = 0;
|
yading@11
|
89
|
yading@11
|
90 framenum += tc->start;
|
yading@11
|
91 if (drop)
|
yading@11
|
92 framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
|
yading@11
|
93 if (framenum < 0) {
|
yading@11
|
94 framenum = -framenum;
|
yading@11
|
95 neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
|
yading@11
|
96 }
|
yading@11
|
97 ff = framenum % fps;
|
yading@11
|
98 ss = framenum / fps % 60;
|
yading@11
|
99 mm = framenum / (fps*60) % 60;
|
yading@11
|
100 hh = framenum / (fps*3600);
|
yading@11
|
101 if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
|
yading@11
|
102 hh = hh % 24;
|
yading@11
|
103 snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
|
yading@11
|
104 neg ? "-" : "",
|
yading@11
|
105 hh, mm, ss, drop ? ';' : ':', ff);
|
yading@11
|
106 return buf;
|
yading@11
|
107 }
|
yading@11
|
108
|
yading@11
|
109 static unsigned bcd2uint(uint8_t bcd)
|
yading@11
|
110 {
|
yading@11
|
111 unsigned low = bcd & 0xf;
|
yading@11
|
112 unsigned high = bcd >> 4;
|
yading@11
|
113 if (low > 9 || high > 9)
|
yading@11
|
114 return 0;
|
yading@11
|
115 return low + 10*high;
|
yading@11
|
116 }
|
yading@11
|
117
|
yading@11
|
118 char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
|
yading@11
|
119 {
|
yading@11
|
120 unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
|
yading@11
|
121 unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
|
yading@11
|
122 unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
|
yading@11
|
123 unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
|
yading@11
|
124 unsigned drop = tcsmpte & 1<<30 && !prevent_df; // 1-bit drop if not arbitrary bit
|
yading@11
|
125 snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
|
yading@11
|
126 hh, mm, ss, drop ? ';' : ':', ff);
|
yading@11
|
127 return buf;
|
yading@11
|
128 }
|
yading@11
|
129
|
yading@11
|
130 char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
|
yading@11
|
131 {
|
yading@11
|
132 snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
|
yading@11
|
133 tc25bit>>19 & 0x1f, // 5-bit hours
|
yading@11
|
134 tc25bit>>13 & 0x3f, // 6-bit minutes
|
yading@11
|
135 tc25bit>>6 & 0x3f, // 6-bit seconds
|
yading@11
|
136 tc25bit & 1<<24 ? ';' : ':', // 1-bit drop flag
|
yading@11
|
137 tc25bit & 0x3f); // 6-bit frames
|
yading@11
|
138 return buf;
|
yading@11
|
139 }
|
yading@11
|
140
|
yading@11
|
141 static int check_fps(int fps)
|
yading@11
|
142 {
|
yading@11
|
143 int i;
|
yading@11
|
144 static const int supported_fps[] = {24, 25, 30, 50, 60};
|
yading@11
|
145
|
yading@11
|
146 for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
|
yading@11
|
147 if (fps == supported_fps[i])
|
yading@11
|
148 return 0;
|
yading@11
|
149 return -1;
|
yading@11
|
150 }
|
yading@11
|
151
|
yading@11
|
152 static int check_timecode(void *log_ctx, AVTimecode *tc)
|
yading@11
|
153 {
|
yading@11
|
154 if (tc->fps <= 0) {
|
yading@11
|
155 av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
|
yading@11
|
156 return AVERROR(EINVAL);
|
yading@11
|
157 }
|
yading@11
|
158 if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps != 30 && tc->fps != 60) {
|
yading@11
|
159 av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 or 60000/1001 FPS\n");
|
yading@11
|
160 return AVERROR(EINVAL);
|
yading@11
|
161 }
|
yading@11
|
162 if (check_fps(tc->fps) < 0) {
|
yading@11
|
163 av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate %d/%d not supported\n",
|
yading@11
|
164 tc->rate.num, tc->rate.den);
|
yading@11
|
165 return AVERROR_PATCHWELCOME;
|
yading@11
|
166 }
|
yading@11
|
167 return 0;
|
yading@11
|
168 }
|
yading@11
|
169
|
yading@11
|
170 static int fps_from_frame_rate(AVRational rate)
|
yading@11
|
171 {
|
yading@11
|
172 if (!rate.den || !rate.num)
|
yading@11
|
173 return -1;
|
yading@11
|
174 return (rate.num + rate.den/2) / rate.den;
|
yading@11
|
175 }
|
yading@11
|
176
|
yading@11
|
177 int av_timecode_check_frame_rate(AVRational rate)
|
yading@11
|
178 {
|
yading@11
|
179 return check_fps(fps_from_frame_rate(rate));
|
yading@11
|
180 }
|
yading@11
|
181
|
yading@11
|
182 int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
|
yading@11
|
183 {
|
yading@11
|
184 memset(tc, 0, sizeof(*tc));
|
yading@11
|
185 tc->start = frame_start;
|
yading@11
|
186 tc->flags = flags;
|
yading@11
|
187 tc->rate = rate;
|
yading@11
|
188 tc->fps = fps_from_frame_rate(rate);
|
yading@11
|
189 return check_timecode(log_ctx, tc);
|
yading@11
|
190 }
|
yading@11
|
191
|
yading@11
|
192 int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
|
yading@11
|
193 {
|
yading@11
|
194 char c;
|
yading@11
|
195 int hh, mm, ss, ff, ret;
|
yading@11
|
196
|
yading@11
|
197 if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
|
yading@11
|
198 av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
|
yading@11
|
199 "syntax: hh:mm:ss[:;.]ff\n");
|
yading@11
|
200 return AVERROR_INVALIDDATA;
|
yading@11
|
201 }
|
yading@11
|
202
|
yading@11
|
203 memset(tc, 0, sizeof(*tc));
|
yading@11
|
204 tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
|
yading@11
|
205 tc->rate = rate;
|
yading@11
|
206 tc->fps = fps_from_frame_rate(rate);
|
yading@11
|
207
|
yading@11
|
208 ret = check_timecode(log_ctx, tc);
|
yading@11
|
209 if (ret < 0)
|
yading@11
|
210 return ret;
|
yading@11
|
211
|
yading@11
|
212 tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
|
yading@11
|
213 if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */
|
yading@11
|
214 int tmins = 60*hh + mm;
|
yading@11
|
215 tc->start -= 2 * (tmins - tmins/10);
|
yading@11
|
216 }
|
yading@11
|
217 return 0;
|
yading@11
|
218 }
|