2012-01-16 13:20:46 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Smartjog S.A.S, Baptiste Coudurier <baptiste.coudurier@gmail.com>
|
|
|
|
* Copyright (c) 2011-2012 Smartjog S.A.S, Clément Bœsch <clement.boesch@smartjog.com>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Timecode helpers
|
2012-02-03 02:56:27 +03:00
|
|
|
* @see https://en.wikipedia.org/wiki/SMPTE_time_code
|
|
|
|
* @see http://www.dropframetimecode.org
|
2012-01-16 13:20:46 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "timecode.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "error.h"
|
|
|
|
|
2012-05-27 15:21:39 +03:00
|
|
|
int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
|
|
|
|
{
|
2020-09-05 22:45:52 +02:00
|
|
|
/* only works for multiples of NTSC 29.97 */
|
2012-05-27 15:21:39 +03:00
|
|
|
int drop_frames = 0;
|
2013-01-22 22:47:58 +03:00
|
|
|
int d, m, frames_per_10mins;
|
2012-05-27 15:21:39 +03:00
|
|
|
|
2020-09-05 22:45:52 +02:00
|
|
|
if (fps && fps % 30 == 0) {
|
|
|
|
drop_frames = fps / 30 * 2;
|
|
|
|
frames_per_10mins = fps / 30 * 17982;
|
2013-01-22 22:47:58 +03:00
|
|
|
} else
|
2012-05-27 15:21:39 +03:00
|
|
|
return framenum;
|
|
|
|
|
2013-01-22 22:47:58 +03:00
|
|
|
d = framenum / frames_per_10mins;
|
|
|
|
m = framenum % frames_per_10mins;
|
|
|
|
|
|
|
|
return framenum + 9 * drop_frames * d + drop_frames * ((m - drop_frames) / (frames_per_10mins / 10));
|
2012-05-27 15:21:39 +03:00
|
|
|
}
|
2012-01-16 13:20:46 +03:00
|
|
|
|
|
|
|
uint32_t av_timecode_get_smpte_from_framenum(const AVTimecode *tc, int framenum)
|
|
|
|
{
|
|
|
|
unsigned fps = tc->fps;
|
|
|
|
int drop = !!(tc->flags & AV_TIMECODE_FLAG_DROPFRAME);
|
|
|
|
int hh, mm, ss, ff;
|
|
|
|
|
|
|
|
framenum += tc->start;
|
|
|
|
if (drop)
|
2012-05-27 15:21:39 +03:00
|
|
|
framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
|
2012-01-16 13:20:46 +03:00
|
|
|
ff = framenum % fps;
|
|
|
|
ss = framenum / fps % 60;
|
|
|
|
mm = framenum / (fps*60) % 60;
|
|
|
|
hh = framenum / (fps*3600) % 24;
|
2020-07-20 21:38:21 +02:00
|
|
|
return av_timecode_get_smpte(tc->rate, drop, hh, mm, ss, ff);
|
2012-01-16 13:20:46 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 07:59:34 +02:00
|
|
|
uint32_t av_timecode_get_smpte(AVRational rate, int drop, int hh, int mm, int ss, int ff)
|
|
|
|
{
|
|
|
|
uint32_t tc = 0;
|
|
|
|
|
|
|
|
/* For SMPTE 12-M timecodes, frame count is a special case if > 30 FPS.
|
|
|
|
See SMPTE ST 12-1:2014 Sec 12.1 for more info. */
|
|
|
|
if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
|
|
|
|
if (ff % 2 == 1) {
|
|
|
|
if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
|
|
|
|
tc |= (1 << 7);
|
|
|
|
else
|
|
|
|
tc |= (1 << 23);
|
|
|
|
}
|
2020-07-20 22:33:20 +02:00
|
|
|
ff /= 2;
|
2020-06-24 07:59:34 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 17:46:42 +02:00
|
|
|
hh = hh % 24;
|
|
|
|
mm = av_clip(mm, 0, 59);
|
|
|
|
ss = av_clip(ss, 0, 59);
|
|
|
|
ff = ff % 40;
|
|
|
|
|
2020-06-24 07:59:34 +02:00
|
|
|
tc |= drop << 30;
|
2020-07-20 22:33:20 +02:00
|
|
|
tc |= (ff / 10) << 28;
|
|
|
|
tc |= (ff % 10) << 24;
|
2020-06-24 07:59:34 +02:00
|
|
|
tc |= (ss / 10) << 20;
|
|
|
|
tc |= (ss % 10) << 16;
|
|
|
|
tc |= (mm / 10) << 12;
|
|
|
|
tc |= (mm % 10) << 8;
|
|
|
|
tc |= (hh / 10) << 4;
|
2020-07-20 22:33:20 +02:00
|
|
|
tc |= (hh % 10);
|
2020-06-24 07:59:34 +02:00
|
|
|
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:20:46 +03:00
|
|
|
char *av_timecode_make_string(const AVTimecode *tc, char *buf, int framenum)
|
|
|
|
{
|
|
|
|
int fps = tc->fps;
|
|
|
|
int drop = tc->flags & AV_TIMECODE_FLAG_DROPFRAME;
|
|
|
|
int hh, mm, ss, ff, neg = 0;
|
|
|
|
|
|
|
|
framenum += tc->start;
|
|
|
|
if (drop)
|
2012-05-27 15:21:39 +03:00
|
|
|
framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
|
2012-01-16 13:20:46 +03:00
|
|
|
if (framenum < 0) {
|
|
|
|
framenum = -framenum;
|
|
|
|
neg = tc->flags & AV_TIMECODE_FLAG_ALLOWNEGATIVE;
|
|
|
|
}
|
|
|
|
ff = framenum % fps;
|
|
|
|
ss = framenum / fps % 60;
|
|
|
|
mm = framenum / (fps*60) % 60;
|
|
|
|
hh = framenum / (fps*3600);
|
|
|
|
if (tc->flags & AV_TIMECODE_FLAG_24HOURSMAX)
|
|
|
|
hh = hh % 24;
|
|
|
|
snprintf(buf, AV_TIMECODE_STR_SIZE, "%s%02d:%02d:%02d%c%02d",
|
|
|
|
neg ? "-" : "",
|
|
|
|
hh, mm, ss, drop ? ';' : ':', ff);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned bcd2uint(uint8_t bcd)
|
|
|
|
{
|
|
|
|
unsigned low = bcd & 0xf;
|
|
|
|
unsigned high = bcd >> 4;
|
|
|
|
if (low > 9 || high > 9)
|
|
|
|
return 0;
|
|
|
|
return low + 10*high;
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:09:32 +02:00
|
|
|
char *av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
|
2012-01-16 13:20:46 +03:00
|
|
|
{
|
|
|
|
unsigned hh = bcd2uint(tcsmpte & 0x3f); // 6-bit hours
|
|
|
|
unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f); // 7-bit minutes
|
|
|
|
unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f); // 7-bit seconds
|
|
|
|
unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f); // 6-bit frames
|
|
|
|
unsigned drop = tcsmpte & 1<<30 && !prevent_df; // 1-bit drop if not arbitrary bit
|
2020-07-24 15:09:32 +02:00
|
|
|
|
|
|
|
if (av_cmp_q(rate, (AVRational) {30, 1}) == 1) {
|
|
|
|
ff <<= 1;
|
|
|
|
if (!skip_field) {
|
|
|
|
if (av_cmp_q(rate, (AVRational) {50, 1}) == 0)
|
|
|
|
ff += !!(tcsmpte & 1 << 7);
|
|
|
|
else
|
|
|
|
ff += !!(tcsmpte & 1 << 23);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:20:46 +03:00
|
|
|
snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
|
|
|
|
hh, mm, ss, drop ? ';' : ':', ff);
|
|
|
|
return buf;
|
2020-07-24 15:09:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
|
|
|
|
{
|
|
|
|
return av_timecode_make_smpte_tc_string2(buf, (AVRational){30, 1}, tcsmpte, prevent_df, 1);
|
2012-01-16 13:20:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
|
|
|
|
{
|
2017-03-27 21:31:46 +02:00
|
|
|
snprintf(buf, AV_TIMECODE_STR_SIZE,
|
|
|
|
"%02"PRIu32":%02"PRIu32":%02"PRIu32"%c%02"PRIu32,
|
2012-01-16 13:20:46 +03:00
|
|
|
tc25bit>>19 & 0x1f, // 5-bit hours
|
|
|
|
tc25bit>>13 & 0x3f, // 6-bit minutes
|
|
|
|
tc25bit>>6 & 0x3f, // 6-bit seconds
|
|
|
|
tc25bit & 1<<24 ? ';' : ':', // 1-bit drop flag
|
|
|
|
tc25bit & 0x3f); // 6-bit frames
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2012-08-03 16:40:15 +03:00
|
|
|
static int check_fps(int fps)
|
|
|
|
{
|
|
|
|
int i;
|
2015-10-26 15:52:40 +02:00
|
|
|
static const int supported_fps[] = {
|
|
|
|
24, 25, 30, 48, 50, 60, 100, 120, 150,
|
|
|
|
};
|
2012-08-03 16:40:15 +03:00
|
|
|
|
|
|
|
for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
|
|
|
|
if (fps == supported_fps[i])
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:20:46 +03:00
|
|
|
static int check_timecode(void *log_ctx, AVTimecode *tc)
|
|
|
|
{
|
2015-12-03 04:14:11 +02:00
|
|
|
if ((int)tc->fps <= 0) {
|
2017-11-05 17:59:22 +02:00
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Valid timecode frame rate must be specified. Minimum value is 1\n");
|
2012-01-16 13:20:46 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2020-09-05 22:45:52 +02:00
|
|
|
if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps % 30 != 0) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with multiples of 30000/1001 FPS\n");
|
2012-01-16 13:20:46 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2012-08-03 16:40:15 +03:00
|
|
|
if (check_fps(tc->fps) < 0) {
|
2015-10-26 15:44:43 +02:00
|
|
|
av_log(log_ctx, AV_LOG_WARNING, "Using non-standard frame rate %d/%d\n",
|
2012-07-31 15:32:12 +03:00
|
|
|
tc->rate.num, tc->rate.den);
|
2012-01-16 13:20:46 +03:00
|
|
|
}
|
2012-08-03 16:40:15 +03:00
|
|
|
return 0;
|
2012-01-16 13:20:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fps_from_frame_rate(AVRational rate)
|
|
|
|
{
|
|
|
|
if (!rate.den || !rate.num)
|
|
|
|
return -1;
|
|
|
|
return (rate.num + rate.den/2) / rate.den;
|
|
|
|
}
|
|
|
|
|
2012-08-03 16:40:15 +03:00
|
|
|
int av_timecode_check_frame_rate(AVRational rate)
|
|
|
|
{
|
|
|
|
return check_fps(fps_from_frame_rate(rate));
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:20:46 +03:00
|
|
|
int av_timecode_init(AVTimecode *tc, AVRational rate, int flags, int frame_start, void *log_ctx)
|
|
|
|
{
|
|
|
|
memset(tc, 0, sizeof(*tc));
|
|
|
|
tc->start = frame_start;
|
|
|
|
tc->flags = flags;
|
|
|
|
tc->rate = rate;
|
|
|
|
tc->fps = fps_from_frame_rate(rate);
|
|
|
|
return check_timecode(log_ctx, tc);
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_timecode_init_from_string(AVTimecode *tc, AVRational rate, const char *str, void *log_ctx)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
int hh, mm, ss, ff, ret;
|
|
|
|
|
|
|
|
if (sscanf(str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) {
|
|
|
|
av_log(log_ctx, AV_LOG_ERROR, "Unable to parse timecode, "
|
|
|
|
"syntax: hh:mm:ss[:;.]ff\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(tc, 0, sizeof(*tc));
|
|
|
|
tc->flags = c != ':' ? AV_TIMECODE_FLAG_DROPFRAME : 0; // drop if ';', '.', ...
|
|
|
|
tc->rate = rate;
|
|
|
|
tc->fps = fps_from_frame_rate(rate);
|
|
|
|
|
|
|
|
ret = check_timecode(log_ctx, tc);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
tc->start = (hh*3600 + mm*60 + ss) * tc->fps + ff;
|
|
|
|
if (tc->flags & AV_TIMECODE_FLAG_DROPFRAME) { /* adjust frame number */
|
|
|
|
int tmins = 60*hh + mm;
|
2020-09-05 22:45:52 +02:00
|
|
|
tc->start -= (tc->fps / 30 * 2) * (tmins - tmins/10);
|
2012-01-16 13:20:46 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|