This source file includes following definitions.
- av_timecode_adjust_ntsc_framenum2
- av_timecode_get_smpte_from_framenum
- av_timecode_make_string
- bcd2uint
- av_timecode_make_smpte_tc_string
- av_timecode_make_mpeg_tc_string
- check_fps
- check_timecode
- fps_from_frame_rate
- av_timecode_check_frame_rate
- av_timecode_init
- av_timecode_init_from_string
#include <stdio.h>
#include "timecode.h"
#include "log.h"
#include "error.h"
int av_timecode_adjust_ntsc_framenum2(int framenum, int fps)
{
int drop_frames = 0;
int d, m, frames_per_10mins;
if (fps == 30) {
drop_frames = 2;
frames_per_10mins = 17982;
} else if (fps == 60) {
drop_frames = 4;
frames_per_10mins = 35964;
} else
return framenum;
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));
}
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)
framenum = av_timecode_adjust_ntsc_framenum2(framenum, tc->fps);
ff = framenum % fps;
ss = framenum / fps % 60;
mm = framenum / (fps*60) % 60;
hh = framenum / (fps*3600) % 24;
return 0 << 31 |
drop << 30 |
(ff / 10) << 28 |
(ff % 10) << 24 |
0 << 23 |
(ss / 10) << 20 |
(ss % 10) << 16 |
0 << 15 |
(mm / 10) << 12 |
(mm % 10) << 8 |
0 << 7 |
0 << 6 |
(hh / 10) << 4 |
(hh % 10);
}
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)
framenum = av_timecode_adjust_ntsc_framenum2(framenum, fps);
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;
}
char *av_timecode_make_smpte_tc_string(char *buf, uint32_t tcsmpte, int prevent_df)
{
unsigned hh = bcd2uint(tcsmpte & 0x3f);
unsigned mm = bcd2uint(tcsmpte>>8 & 0x7f);
unsigned ss = bcd2uint(tcsmpte>>16 & 0x7f);
unsigned ff = bcd2uint(tcsmpte>>24 & 0x3f);
unsigned drop = tcsmpte & 1<<30 && !prevent_df;
snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
hh, mm, ss, drop ? ';' : ':', ff);
return buf;
}
char *av_timecode_make_mpeg_tc_string(char *buf, uint32_t tc25bit)
{
snprintf(buf, AV_TIMECODE_STR_SIZE, "%02u:%02u:%02u%c%02u",
tc25bit>>19 & 0x1f,
tc25bit>>13 & 0x3f,
tc25bit>>6 & 0x3f,
tc25bit & 1<<24 ? ';' : ':',
tc25bit & 0x3f);
return buf;
}
static int check_fps(int fps)
{
int i;
static const int supported_fps[] = {24, 25, 30, 48, 50, 60};
for (i = 0; i < FF_ARRAY_ELEMS(supported_fps); i++)
if (fps == supported_fps[i])
return 0;
return -1;
}
static int check_timecode(void *log_ctx, AVTimecode *tc)
{
if (tc->fps <= 0) {
av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
return AVERROR(EINVAL);
}
if ((tc->flags & AV_TIMECODE_FLAG_DROPFRAME) && tc->fps != 30 && tc->fps != 60) {
av_log(log_ctx, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 or 60000/1001 FPS\n");
return AVERROR(EINVAL);
}
if (check_fps(tc->fps) < 0) {
av_log(log_ctx, AV_LOG_ERROR, "Timecode frame rate %d/%d not supported\n",
tc->rate.num, tc->rate.den);
return AVERROR_PATCHWELCOME;
}
return 0;
}
static int fps_from_frame_rate(AVRational rate)
{
if (!rate.den || !rate.num)
return -1;
return (rate.num + rate.den/2) / rate.den;
}
int av_timecode_check_frame_rate(AVRational rate)
{
return check_fps(fps_from_frame_rate(rate));
}
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;
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) {
int tmins = 60*hh + mm;
tc->start -= 2 * (tmins - tmins/10);
}
return 0;
}