From patchwork Thu Jan 7 00:43:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Niedermayer X-Patchwork-Id: 24817 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9685244A217 for ; Thu, 7 Jan 2021 02:44:43 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6E6CE689A81; Thu, 7 Jan 2021 02:44:43 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from vie01a-dmta-pe02-1.mx.upcmail.net (vie01a-dmta-pe02-1.mx.upcmail.net [62.179.121.157]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1E86F6880B4 for ; Thu, 7 Jan 2021 02:44:36 +0200 (EET) Received: from [172.31.216.235] (helo=vie01a-pemc-psmtp-pe12.mail.upcmail.net) by vie01a-dmta-pe02.mx.upcmail.net with esmtp (Exim 4.92) (envelope-from ) id 1kxJPh-0009di-0K for ffmpeg-devel@ffmpeg.org; Thu, 07 Jan 2021 01:44:29 +0100 Received: from localhost ([213.47.68.29]) by vie01a-pemc-psmtp-pe12.mail.upcmail.net with ESMTP id xJOikLJtYO4rAxJOikGaPc; Thu, 07 Jan 2021 01:43:28 +0100 X-Env-Mailfrom: michael@niedermayer.cc X-Env-Rcptto: ffmpeg-devel@ffmpeg.org X-SourceIP: 213.47.68.29 X-CNFS-Analysis: v=2.3 cv=RNDN4Lq+ c=1 sm=1 tr=0 a=2hcxjKEKjp0CzLx6oWAm4g==:117 a=2hcxjKEKjp0CzLx6oWAm4g==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=GEAsPZ9sns4A:10 a=ZZnuYtJkoWoA:10 a=nZOtpAppAAAA:20 a=BYsqDdkqvhxgFblcNXUA:9 a=1fhp2MxaeJtTNGEnv6mo:22 a=Z5ABNNGmrOfJ6cZ5bIyy:22 a=UDnyf2zBuKT2w-IlGP_r:22 From: Michael Niedermayer To: FFmpeg development discussions and patches Date: Thu, 7 Jan 2021 01:43:27 +0100 Message-Id: <20210107004328.27244-1-michael@niedermayer.cc> X-Mailer: git-send-email 2.17.1 X-CMAE-Envelope: MS4wfIWnm5wxl5wePw0L/bV8M7ZHI5MFztzEOpU+PugiE544MZxkxKsdVDoiWcPcNtM3v2Dk28A8vgu8sOix5Je9B5M5sVkJMbxC5QcZvTElNNOWNr5Z63Qy vf8UHSn2avXDJboUNOw/c+Y6XVb3Z4R+mNMclwiOdScI1stft+5syWn1 Subject: [FFmpeg-devel] [PATCH 1/2] avformat: Change avpriv_new_chapter() from O(n) to (1) in the common case X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 --- libavformat/internal.h | 5 +++++ libavformat/utils.c | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/libavformat/internal.h b/libavformat/internal.h index 49e82bfbca..f45b1cd6b4 100644 --- a/libavformat/internal.h +++ b/libavformat/internal.h @@ -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 { diff --git a/libavformat/utils.c b/libavformat/utils.c index 3ba4ae4123..a43d227128 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -4612,9 +4612,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));