From patchwork Sat Sep 24 23:20:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 38217 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp1371186pzh; Sat, 24 Sep 2022 16:21:06 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6h25HvzfFl3+et1H3MnoT+ByaGk8sWucx7dVJ9bYNqIs7MiwypRLaGxXaPFK1ur3Lvwr2e X-Received: by 2002:a17:907:6e9f:b0:780:b046:fb40 with SMTP id sh31-20020a1709076e9f00b00780b046fb40mr12106854ejc.256.1664061665553; Sat, 24 Sep 2022 16:21:05 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id gv26-20020a170906f11a00b0077ef3eeca17si10397124ejb.155.2022.09.24.16.21.04; Sat, 24 Sep 2022 16:21:05 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=s+hnSTcq; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4D71368B3B2; Sun, 25 Sep 2022 02:21:01 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-oln040092072108.outbound.protection.outlook.com [40.92.72.108]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4381E68B630 for ; Sun, 25 Sep 2022 02:20:54 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hmau1nauq0cL8nWa5uYDY70yLVibolSkYewXJJbZhrX2LRFxgMILS19xKTgovBUUHDwOYJs5Eg3r9sYXZbtNlVWf4mOqsRL8o437PnXZzoVvkdP5FPXkewMPQ716zTmsf6ywemO75CNKeTOGIKQVP6vcquhAwW2VX346OLpRiUosJ6cB/Rhht7jNaXxlixURdt7ehUn0bPXd3XMpQFz+YON8QbtATLJBNiQdQiN6SHfTPc8dBn/X+cG13JD9wxxJvOpqp8atfL8SAlNlqLoSMU8vBCIf/n/rRWBhUDcoNiBBnJiKbmIpK8boOMEwZMFCvq0FEZhNrpyY7jw66EBIJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=jVCwbi+l/Wnn5x64yCXgKJzKNr8ov4EJ0NLRe42+Zh0=; b=PY5g4tpM5wtXsnwx/f8qv2sXPAbK+Ff9sFiwqg3EMH7F0fhwesI2ttDaXUhzusWzMFRZ8qZgoc3ukW58fnrH3RdWDbob0M4RkgvYfBu3NV0Vg+r5kFy2gqzNUYW1X18t6zzWNg0hm9Ew0bR8pfWuawHEwftcVhqLVFt/cqdy+5FRV4mRQzIAf2YNcb3++Vo2+UVdDH071LSFRbjwfzWQnyvyvLDwA28mu3AOKevtLsMzvnt7w0ux2YGTb3fvR3G5V1SHcw1e3XL/EYbjco+aq13cyxOAlbi7jR18r2zH0lHIfUaH83N6lSTnU2weU3LUlwQO4EtA5aHX61doWAvB3w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=jVCwbi+l/Wnn5x64yCXgKJzKNr8ov4EJ0NLRe42+Zh0=; b=s+hnSTcqSLaWnY0jLqTMMc2VAzAXpKKHdfpOkUX+dPolisU4Z3TO4kZmz+wY5rmCG14W0jzJaDUtxlFsFwSu2d1Qjm9uCfIMaisOGCYOCDbuzGcKchD3vCLPtGftUKRZvan1tp9PCAGeOO2qKNhHbHZ1cQJkC65edoZGDhIRlW4xANMQTEqwKeYmOwGFlilPVrmSCGEJ+PrkqtwQE/AvLPbPjjoDFe4yg9pEDx8hS+vXmojnP8ybrXe1zT9koDjWqrdML5LCojl+J5VgM4qbVaO333szbSR7Jc/6xuOPnJ4iaQB+6Ws9ltwcv+wmKdKEIrRmsyshZp3wUT+vDmIkbw== Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) by DB4P250MB0757.EURP250.PROD.OUTLOOK.COM (2603:10a6:10:37a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.17; Sat, 24 Sep 2022 23:20:52 +0000 Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::85ac:1b92:90f:dc18]) by AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::85ac:1b92:90f:dc18%4]) with mapi id 15.20.5654.016; Sat, 24 Sep 2022 23:20:52 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 25 Sep 2022 01:20:51 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 X-TMN: [FfT9QEjGZQQnv+Aeye1fS/ifcPxVpQqmEFf+ZfzscRg=] X-ClientProxiedBy: ZR0P278CA0134.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:40::13) To AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) X-Microsoft-Original-Message-ID: <20220924232051.2233833-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8P250MB0744:EE_|DB4P250MB0757:EE_ X-MS-Office365-Filtering-Correlation-Id: 7f6855c9-1988-4c70-bdb4-08da9e836c39 X-MS-Exchange-SLBlob-MailProps: ZILSnhm0P3nx9aTg8IusoUid56WOextU7PsLI/gffqSywVK0h5JSaA012XDE/wrDLew4hmavaNXoaKuYXT+G0WIFwKhJmQ8YEN6K25bCNu8Uob20LVJ6KnD11IHFCLX06NuSVh4+yioNAk53NOgf2F0B45+Kq55uX2BHDB8WKMlp/VtUtv2J3tNUkUeo6/m1nmB7iAUL5EoOIcL1exQK/sqRUKPWNnSLGoBUhuPGvQZHfBa169veKWg/9GYam1P5SQEYoiPNaMTdw3mZ6x0sAozq4TifPJIA/Lg8qksNHDvWdiZdjqSa9NaDVKg8S40udgV+juytk9MNtktKaBaLslqi3BN5BslMzC7frCR/hUeEhmurbc+iBQQ8PaNceqksalTV8ZEiGsrQFSFVNRFjJ+JWXl/qe9nsndJB7U0Pl0ZFV8wRJJAz9fK8WlQojhz3hJjJGEVhmQG5tfkoi9G6DE8s7prJAhuwt778TkpHk0B/TxYPTGxAn1TrxvbI6JGLqUAjtP/zZmrMzlWYi02UMaq/b2f1kzdrk+5mxMobXRL9wdaSFPZGDSRJZWHkoL/fMFpJDzqp3N+BZk+m9jl+GReJ06Ed990nRK7J872HwpXx92/LTmZijVNZoFJZQxwO40W9SyQvRBC5STLFxsboFOysN7iVv8tyJeR08nx/OnqpE9fmw67k3/D7Hk8KTKKXL/v0srFOrHhfsm5j2AJqJk1sRT9lKOxsNgUS5DtSYgzdJ1UCDLfr7/5uw6jcdSCcd4G8jw09wzHXy/HbcWu2pxOw/3yYdIZ7 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XEtx6DY1aKxaxocy23+qB/pJ4ZZeV81UHFJgEdDV5TWe1TQa3k6FZAVLKW7lemraBrVYxJj7QWM6gkO9+Y3aUqtgoDf6ZatnXyqfaZWK7VmuLr99p3l3fqX1Az3zmU+Nh9Rsit1lmOp2gzkpdWOJqU061fEg0OzNhP6yqs3fsKyJwCbrfygny33njjdEdtug9jaJArZ+Mu9qXZc/e9N9EBRJfyko3cnvL4uaRcrIGhrlBRLOUqnBlMhg2jljZ2qMhEoLpfB9Zce53JPTtkY9Kb9xhPxGgpa7GJTzlI7P1iwc/AWBCfEJk/eTL/ZNUuNRIIxnfOgWaaQz6Gq7KgRYVZLQla91EqYrFBkEz/guhiJnIXwe1/upbGu8/3wT6twXDvhp4ll+6+tJBvWBR2NWt8BoGorZAf4pyYang/vu5bI/ArEi7jlp5ZKF7qbLuJNL2HdH/bL2jZArJ/tNro7o0ADJZgqzakXO7TzShSSN56gge9zIllRXmm93Lb4Br5tWRjpNulAKXN5XFd0cXyFKmBPNTW7rfioC8IFK46eVG/jla7TfncR7USX31yt4ePjo1yvQyJ45RV9KNISoFQ2SkJYgIcql8T4C5yThJhRZbguS0hXmsko+PtdG9+iAC2N7M97IUT8/Vasz195OUbaDpA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: t/tSaO20ZuEjussakyqTF6MFeYTRJEzj7gO33xBY3qLI6VJvP9pnc/B0IGrrJTqymz874+SYmc/g2hkBXmhM+tMayxWB/a21P3dSD5kc8MtdO0P6S0PVI8bCNhFgWvA6F77+sivFK3FmWiyvDjt580VpppOZ7mhEvn0rxY8FYx+6AMzwvPs0Puq+bj2OUWHZrUMcquLKllzMfQIOLocuFgtopzQmZFO56W9bnMmvTOOagc3w3MwNC/9zb+gfmiy7oKvVGhzUmbWY/ZBdN4i9GUMYCFLNmcevByADi9HPbmNNtTD6OCpYqSBbHHB8mUQMJLp5aZBmQG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-MS-Exchange-CrossTenant-AuthSource: AS8P250MB0744.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2022 23:20:52.2160 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB4P250MB0757 Subject: [FFmpeg-devel] [PATCH] avformat/mov: Speed up finding MOVFragmentIndexItem X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ik942fkQBGps The MOVFragmentIndexItems of a MOVFragmentIndex are ordered by increasing moof_offset; yet get_frag_stream_info_from_pkt() searched the array of index items linearly for the item with moof_offset <= pos < moof_offset of next index (if existing). This commit changes this: First, it is checked, whether the current item or the next item are the desired items; it this fails, a array is searched via binary search. Fixes ticket #9941. Signed-off-by: Andreas Rheinhardt --- The earlier code reused current in case pkt->pos is before the first moof_offset. I kept this behaviour, although I don't know whether this can actually happen. libavformat/mov.c | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/libavformat/mov.c b/libavformat/mov.c index 1f436e21d6..79331638b0 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -7192,26 +7192,40 @@ static int cenc_decrypt(MOVContext *c, MOVStreamContext *sc, AVEncryptionInfo *s static MOVFragmentStreamInfo *get_frag_stream_info_from_pkt(MOVFragmentIndex *frag_index, AVPacket *pkt, int id) { int current = frag_index->current; + int next = -1; if (!frag_index->nb_items) return NULL; // Check frag_index->current is the right one for pkt. It can out of sync. - if (current >= 0 && current < frag_index->nb_items) { - if (frag_index->item[current].moof_offset < pkt->pos && - (current + 1 == frag_index->nb_items || - frag_index->item[current + 1].moof_offset > pkt->pos)) - return get_frag_stream_info(frag_index, current, id); - } - - - for (int i = 0; i < frag_index->nb_items; i++) { - if (frag_index->item[i].moof_offset > pkt->pos) - break; - current = i; - } - frag_index->current = current; - return get_frag_stream_info(frag_index, current, id); + if (current >= 0 && current < frag_index->nb_items && + frag_index->item[current].moof_offset <= pkt->pos) { + if (current + 1U == frag_index->nb_items || + frag_index->item[current + 1].moof_offset > pkt->pos) { + next = current; + } else if (current + 2U == frag_index->nb_items || + frag_index->item[current + 2].moof_offset > pkt->pos) + next = current + 1; + } + + if (next < 0) { + if (pkt->pos < frag_index->item[0].moof_offset) { + /* Can this happen? Does this make sense? */ + next = current; + } else { + next = search_frag_moof_offset(frag_index, pkt->pos); + /* If there is no exact match for pkt->pos, then + * search_frag_moof_offset() returns the index of + * the first item with moof_offset > pkt->pos + * (or frag_index->nb_items if no such item exists). */ + if (next == frag_index->nb_items) + next = frag_index->nb_items - 1; + else if (frag_index->item[next].moof_offset > pkt->pos) + next--; + } + } + frag_index->current = next; + return get_frag_stream_info(frag_index, next, id); } static int cenc_filter(MOVContext *mov, AVStream* st, MOVStreamContext *sc, AVPacket *pkt, int current_index)