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