]> git.sesse.net Git - ffmpeg/blob - libavutil/timecode.c
Merge commit '88bd7fdc821aaa0cbcf44cf075c62aaa42121e3f'
[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 = framenum / 17982;
39     int m = framenum % 17982;
40
41     if (fps == 30)
42         drop_frames = 2;
43     else if (fps == 60)
44         drop_frames = 4;
45     else
46         return framenum;
47
48     //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
49     return framenum + 9 * drop_frames * d + drop_frames * ((m - 2) / 1798);
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 0         << 31 | // color frame flag (0: unsync mode, 1: sync mode)
66            drop      << 30 | // drop  frame flag (0: non drop,    1: drop)
67            (ff / 10) << 28 | // tens  of frames
68            (ff % 10) << 24 | // units of frames
69            0         << 23 | // PC (NTSC) or BGF0 (PAL)
70            (ss / 10) << 20 | // tens  of seconds
71            (ss % 10) << 16 | // units of seconds
72            0         << 15 | // BGF0 (NTSC) or BGF2 (PAL)
73            (mm / 10) << 12 | // tens  of minutes
74            (mm % 10) <<  8 | // units of minutes
75            0         <<  7 | // BGF2 (NTSC) or PC (PAL)
76            0         <<  6 | // BGF1
77            (hh / 10) <<  4 | // tens  of hours
78            (hh % 10);        // units of hours
79 }
80
81 char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
82 {
83     int fps = tc->fps;
84     int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
85     int hh, mm, ss, ff, neg = 0;
86
87     framenum += tc->start;
88     if (drop)
89         framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
90     if (framenum < 0) {
91         framenum = -framenum;
92         neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
93     }
94     ff = framenum % fps;
95     ss = framenum / fps        % 60;
96     mm = framenum / (fps*60)   % 60;
97     hh = framenum / (fps*3600);
98     if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
99         hh = hh % 24;
100     snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
101              neg ? "-" : "",
102              hh, mm, ss, drop ? ';' : ':', ff);
103     return buf;
104 }
105
106 static unsigned bcd2uint(uint8_t bcd)
107 {
108    unsigned low  = bcd & 0xf;
109    unsigned high = bcd >> 4;
110    if (low > 9 || high > 9)
111        return 0;
112    return low + 10*high;
113 }
114
115 char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
116 {
117     unsigned hh   = bcd2uint(tcsmpte     & 0x3f);    // 6-bit hours
118     unsigned mm   = bcd2uint(tcsmpte>>8  & 0x7f);    // 7-bit minutes
119     unsigned ss   = bcd2uint(tcsmpte>>16 & 0x7f);    // 7-bit seconds
120     unsigned ff   = bcd2uint(tcsmpte>>24 & 0x3f);    // 6-bit frames
121     unsigned drop = tcsmpte & 1<<30 && !prevent_df;  // 1-bit drop if not arbitrary bit
122     snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
123              hh, mm, ss, drop ? ';' : ':', ff);
124     return buf;
125 }
126
127 char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
128 {
129     snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
130              tc25bit>>19 & 0x1f,              // 5-bit hours
131              tc25bit>>13 & 0x3f,              // 6-bit minutes
132              tc25bit>>6  & 0x3f,              // 6-bit seconds
133              tc25bit     & 1<<24 ? ';' : ':', // 1-bit drop flag
134              tc25bit     & 0x3f);             // 6-bit frames
135     return buf;
136 }
137
138 static int check_fps(int fps)
139 {
140     int i;
141     static const int supported_fps[] = {24, 25, 30, 50, 60};
142
143     for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
144         if (fps == supported_fps[i])
145             return 0;
146     return -1;
147 }
148
149 static int check_timecode(void *log_ctx, AVTimecode *tc)
150 {
151     if (tc->fps <= 0) {
152         av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
153         return AVERROR(EINVAL);
154     }
155     if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps != 30) {
156         av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n");
157         return AVERROR(EINVAL);
158     }
159     if (check_fps(tc->fps) < 0) {
160         av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate %d/%d not supported\n",
161                tc->rate.num, tc->rate.den);
162         return AVERROR_PATCHWELCOME;
163     }
164     return 0;
165 }
166
167 static int fps_from_frame_rate(AVRational rate)
168 {
169     if (!rate.den || !rate.num)
170         return -1;
171     return (rate.num + rate.den/2) / rate.den;
172 }
173
174 int av_timecode_check_frame_rate(AVRational rate)
175 {
176     return check_fps(fps_from_frame_rate(rate));
177 }
178
179 int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
180 {
181     memset(tc, 0, sizeof(*tc));
182     tc->start = frame_start;
183     tc->flags = flags;
184     tc->rate  = rate;
185     tc->fps   = fps_from_frame_rate(rate);
186     return check_timecode(log_ctx, tc);
187 }
188
189 int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
190 {
191     char c;
192     int hh, mm, ss, ff, ret;
193
194     if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
195         av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
196                                       "syntax: hh:mm:ss[:;.]ff\n");
197         return AVERROR_INVALIDDATA;
198     }
199
200     memset(tc, 0, sizeof(*tc));
201     tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
202     tc->rate  = rate;
203     tc->fps   = fps_from_frame_rate(rate);
204
205     ret = check_timecode(log_ctx, tc);
206     if (ret < 0)
207         return ret;
208
209     tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
210     if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */
211         int tmins = 60*hh + mm;
212         tc->start -= 2 * (tmins - tmins/10);
213     }
214     return 0;
215 }