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