]> git.sesse.net Git - ffmpeg/blob - libavutil/timecode.c
Merge commit '0c03cc68386443f1e96ab6fb358220faf67cd5ff'
[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 #ifdef FF_API_OLD_TC_ADJUST_FRAMENUM
35 int av_timecode_adjust_ntsc_framenum(int framenum)
36 {
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);
42 }
43 #endif
44
45 int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
46 {
47     /* only works for NTSC 29.97 and 59.94 */
48     int drop_frames = 0;
49     int d = framenum / 17982;
50     int m = framenum % 17982;
51
52     if (fps == 30)
53         drop_frames = 2;
54     else if (fps == 60)
55         drop_frames = 4;
56     else
57         return framenum;
58
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);
61 }
62
63 uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
64 {
65     unsigned fps = tc->fps;
66     int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
67     int hh, mm, ss, ff;
68
69     framenum += tc->start;
70     if (drop)
71         framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
72     ff = framenum % 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)
87            0         <<  6 | // BGF1
88            (hh / 10) <<  4 | // tens  of hours
89            (hh % 10);        // units of hours
90 }
91
92 char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
93 {
94     int fps = tc->fps;
95     int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
96     int hh, mm, ss, ff, neg = 0;
97
98     framenum += tc->start;
99     if (drop)
100         framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
101     if (framenum < 0) {
102         framenum = -framenum;
103         neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
104     }
105     ff = framenum % fps;
106     ss = framenum / fps        % 60;
107     mm = framenum / (fps*60)   % 60;
108     hh = framenum / (fps*3600);
109     if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
110         hh = hh % 24;
111     snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
112              neg ? "-" : "",
113              hh, mm, ss, drop ? ';' : ':', ff);
114     return buf;
115 }
116
117 static unsigned bcd2uint(uint8_t bcd)
118 {
119    unsigned low  = bcd & 0xf;
120    unsigned high = bcd >> 4;
121    if (low > 9 || high > 9)
122        return 0;
123    return low + 10*high;
124 }
125
126 char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
127 {
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);
135     return buf;
136 }
137
138 char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
139 {
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
146     return buf;
147 }
148
149 static int check_fps(int fps)
150 {
151     int i;
152     static const int supported_fps[] = {24, 25, 30, 50, 60};
153
154     for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
155         if (fps == supported_fps[i])
156             return 0;
157     return -1;
158 }
159
160 static int check_timecode(void *log_ctx, AVTimecode *tc)
161 {
162     if (tc->fps <= 0) {
163         av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
164         return AVERROR(EINVAL);
165     }
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);
169     }
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;
174     }
175     return 0;
176 }
177
178 static int fps_from_frame_rate(AVRational rate)
179 {
180     if (!rate.den || !rate.num)
181         return -1;
182     return (rate.num + rate.den/2) / rate.den;
183 }
184
185 int av_timecode_check_frame_rate(AVRational rate)
186 {
187     return check_fps(fps_from_frame_rate(rate));
188 }
189
190 int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
191 {
192     memset(tc, 0, sizeof(*tc));
193     tc->start = frame_start;
194     tc->flags = flags;
195     tc->rate  = rate;
196     tc->fps   = fps_from_frame_rate(rate);
197     return check_timecode(log_ctx, tc);
198 }
199
200 int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
201 {
202     char c;
203     int hh, mm, ss, ff, ret;
204
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;
209     }
210
211     memset(tc, 0, sizeof(*tc));
212     tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
213     tc->rate  = rate;
214     tc->fps   = fps_from_frame_rate(rate);
215
216     ret = check_timecode(log_ctx, tc);
217     if (ret < 0)
218         return ret;
219
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);
224     }
225     return 0;
226 }