[FFmpeg-devel] [PATCH 1/3] timecode: better input checks in init function.

Clément Bœsch ubitux at gmail.com
Tue Dec 6 11:37:37 CET 2011


From: Clément Bœsch <clement.boesch at smartjog.com>

---
 libavcodec/timecode.c |   37 ++++++++++++++++++++++++++++++-------
 1 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/libavcodec/timecode.c b/libavcodec/timecode.c
index 7f3b1d5..4148f32 100644
--- a/libavcodec/timecode.c
+++ b/libavcodec/timecode.c
@@ -55,9 +55,32 @@ uint32_t ff_framenum_to_smtpe_timecode(unsigned frame, int fps, int drop)
            (  (frame / (fps * 3600) % 24)) % 10;          // units of hours
 }
 
+static int ff_check_timecode_rate(void *avcl, AVRational rate, int drop)
+{
+    int fps = (rate.num + rate.den/2) / rate.den;
+
+    if (!rate.num || !rate.den) {
+        av_log(avcl, AV_LOG_ERROR, "Timecode frame rate must be specified\n");
+        return -1;
+    }
+    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 +89,17 @@ int ff_init_smtpe_timecode(void *avcl, struct ff_timecode *tc)
         return -1;
     }
 
+    tc->drop  = c != ':'; // drop if ';', '.', ...
+
+    ret = ff_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;
-- 
1.7.7.3



More information about the ffmpeg-devel mailing list