]> git.sesse.net Git - ffmpeg/commitdiff
avformat: Change avpriv_new_chapter() from O(n) to (1) in the common case
authorMichael Niedermayer <michael@niedermayer.cc>
Wed, 6 Jan 2021 22:24:49 +0000 (23:24 +0100)
committerMichael Niedermayer <michael@niedermayer.cc>
Thu, 28 Jan 2021 20:08:35 +0000 (21:08 +0100)
Fixes: timeout (slow -> 300ms)
Fixes: 28876/clusterfuzz-testcase-minimized-ffmpeg_dem_MOV_fuzzer-5664824587583488
Found-by: continuous fuzzing process https://github.com/google/oss-fuzz/tree/master/projects/ffmpeg
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
libavformat/internal.h
libavformat/utils.c

index 49e82bfbca1176942e40470ac4c53bb1c8081577..f45b1cd6b4678180d5cf53354f91ab86585fb09a 100644 (file)
@@ -142,6 +142,11 @@ struct AVFormatInternal {
      * Prefer the codec framerate for avg_frame_rate computation.
      */
     int prefer_codec_framerate;
+
+    /**
+     * Set if chapter ids are strictly monotonic.
+     */
+    int chapter_ids_monotonic;
 };
 
 struct AVStreamInternal {
index 1ec71691e5e6f7dafbe81defce227fa6faaa0947..9dab4fc96f7a8f15f5cb8e254e82a3979c412ed2 100644 (file)
@@ -4614,9 +4614,14 @@ AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base,
         return NULL;
     }
 
-    for (i = 0; i < s->nb_chapters; i++)
-        if (s->chapters[i]->id == id)
-            chapter = s->chapters[i];
+    if (!s->nb_chapters) {
+        s->internal->chapter_ids_monotonic = 1;
+    } else if (!s->internal->chapter_ids_monotonic || s->chapters[s->nb_chapters-1]->id >= id) {
+        s->internal->chapter_ids_monotonic = 0;
+        for (i = 0; i < s->nb_chapters; i++)
+            if (s->chapters[i]->id == id)
+                chapter = s->chapters[i];
+    }
 
     if (!chapter) {
         chapter = av_mallocz(sizeof(AVChapter));