From 98aca4bbef6818bbc8209a801425c3a6f1ff41a9 Mon Sep 17 00:00:00 2001 From: Clément Bœsch Date: Tue, 6 Dec 2011 11:17:37 +0100 Subject: timecode: better input checks in init function. --- libavcodec/timecode.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'libavcodec/timecode.c') diff --git a/libavcodec/timecode.c b/libavcodec/timecode.c index 7f3b1d5ab8..8cd1bec941 100644 --- a/libavcodec/timecode.c +++ b/libavcodec/timecode.c @@ -55,9 +55,33 @@ uint32_t ff_framenum_to_smtpe_timecode(unsigned frame, int fps, int drop) ( (frame / (fps * 3600) % 24)) % 10; // units of hours } +static int check_timecode_rate(void *avcl, AVRational rate, int drop) +{ + int fps; + + if (!rate.num || !rate.den) { + av_log(avcl, AV_LOG_ERROR, "Timecode frame rate must be specified\n"); + return -1; + } + fps = (rate.num + rate.den/2) / rate.den; + if (drop && (rate.den != 1001 || fps != 30)) { + av_log(avcl, AV_LOG_ERROR, "Drop frame is only allowed with 30000/1001 FPS\n"); + return -2; + } + switch (fps) { + case 24: + case 25: + case 30: return 0; + + default: + av_log(avcl, AV_LOG_ERROR, "Timecode frame rate not supported\n"); + return -3; + } +} + int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc) { - int hh, mm, ss, ff, fps; + int hh, mm, ss, ff, fps, ret; char c; if (sscanf(tc->str, "%d:%d:%d%c%d", &hh, &mm, &ss, &c, &ff) != 5) { @@ -66,17 +90,17 @@ int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc) return -1; } + tc->drop = c != ':'; // drop if ';', '.', ... + + ret = check_timecode_rate(avcl, tc->rate, tc->drop); + if (ret < 0) + return ret; + fps = (tc->rate.num + tc->rate.den/2) / tc->rate.den; tc->start = (hh*3600 + mm*60 + ss) * fps + ff; - tc->drop = c != ':'; // drop if ';', '.', ... if (tc->drop) { /* adjust frame number */ int tmins = 60*hh + mm; - if (tc->rate.den != 1001 || fps != 30) { - av_log(avcl, AV_LOG_ERROR, "error: drop frame is only allowed with" - "30000/1001 FPS"); - return -2; - } tc->start -= 2 * (tmins - tmins/10); } return 0; -- cgit v1.2.3