From patchwork Tue Jul 5 20:26:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 36683 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp3728273pzh; Tue, 5 Jul 2022 13:28:53 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uUZ+k+hp3pabXEXZrqcmYB78kSj3fDnwf8XYIikehbKiQmisXFKCg/FQ7ddJP5u+6CBix+ X-Received: by 2002:a05:6402:35c5:b0:437:140d:5cb3 with SMTP id z5-20020a05640235c500b00437140d5cb3mr48498571edc.72.1657052933082; Tue, 05 Jul 2022 13:28:53 -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 g8-20020a170906868800b007262d20ea70si19728190ejx.85.2022.07.05.13.28.52; Tue, 05 Jul 2022 13:28:53 -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=sOQkeoAe; 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 25DE768B9DB; Tue, 5 Jul 2022 23:28:47 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2087.outbound.protection.outlook.com [40.92.89.87]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7B8B468B9A6 for ; Tue, 5 Jul 2022 23:28:40 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JzbNUwKdPhEmO37rfWM1DI4HhsgDupQR94mo2cbKR+Wufwpl/bb3pUZQsz8+TMX75AB/85gEeqM/ofczp3RAPkiaepsSr2o+pIWaW+aIHqskObFBSEvjP5X5fQZSUPluBuKDlFZruv7imdM/grFnGHwbcTFFpFzVtRahuKLcfifutRW6AIv9qV8bLpHNPhIlpNS91VBqrVj9jIl8K7rV5gRovwCHaqyaJtAiQbrDkJSuT1sfi82jQS1ACXd+b91rv66wHCJqz+eoMZXOyxk7nRNl5lMe3dLy7SHe5FTPCjTC+MkgxaziP2Dg2kf7AgHwalgCxx60Os1WvcZuA4/7DQ== 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=fgxaH80Jd/KD68pCjGjCans5W+Xvvb8RduDJ9yf9M4I=; b=JwGgzAo9JIEWqXwNZEON4saCCy2b98KKNltQRfW9PFlb7cX22cdCG/RyfXZdNDXap+v1cZgvrpClFUTvziIV+Gv+ZVGWfcKVqpYWOVJbqVmV/DVV0YXyq/fZ2c9iv9cPKr5bhtET/8bvdJliOTbKWH43c+alfVxIWRTL3pL/xq095CVjBEM+wo9br5prT0Ff3wGNgbu89jCqVgvQQnb6/uLRaVZd9B6cx/qho9e6OgK7p6uOyUK/BsLjxmYJwHyr5oDiBdF/dBvPWzDcyk0BOG//jTD8ceRZHVC5iyqo+ZmwYjMjs13I91scou5FT+VoFKK5fWDzw0dXqPRPqZYFxQ== 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=fgxaH80Jd/KD68pCjGjCans5W+Xvvb8RduDJ9yf9M4I=; b=sOQkeoAeWFlCxbeZgspNPNXMJMKstFI2jhF5HXIuAsMNSRG+94UO73PGe4St0rwmhMLU0aonoH49L/rxRFGJ6rTts1Y/0LDo/YXGIASvLGELbZmTxMQ4MNEAYKho4OJ6ZZcIyZOM84pItAmtAWEhOqtGIIFGGE3nnFQA2gh6VfJNu7BG4l0t8BvUa1QrvUNNfNLS4JpaDNqZpfKIKMZZuu788JiLZ4970bIPYQpIj0vp7wwht2mAk/QFnxIPeR3+7rbUs43kU3f83lzWJR+Wgr805GnKgBbPxZoacKOZkU/c6FbQYkqy2wiumNEehU2LICPShA0ItT1Ftiuo+gbuDg== Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) by PA4PR01MB9452.eurprd01.prod.exchangelabs.com (2603:10a6:102:2af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.21; Tue, 5 Jul 2022 20:28:37 +0000 Received: from DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::60b9:9f29:40cc:f01c]) by DB6PR0101MB2214.eurprd01.prod.exchangelabs.com ([fe80::60b9:9f29:40cc:f01c%10]) with mapi id 15.20.5395.021; Tue, 5 Jul 2022 20:28:37 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 5 Jul 2022 22:26:47 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [rEyTW2fXdVmOcs2Ef2w3zbTmu4Ubbloh] X-ClientProxiedBy: AS9PR06CA0419.eurprd06.prod.outlook.com (2603:10a6:20b:461::15) To DB6PR0101MB2214.eurprd01.prod.exchangelabs.com (2603:10a6:4:42::27) X-Microsoft-Original-Message-ID: <20220705202650.797218-4-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e45e0706-5c18-4039-f800-08da5ec4f08c X-MS-Exchange-SLBlob-MailProps: S/btQ8cKWiSJyDCj7LTlCpA5m0H/gq0AZo5KQBbAMzGcLOLG0XAhSuc96n1qMZlSTmwcL2yl65nkM8VlufGpLknA9BsK3e9hiYtu+reADvlY5saqpVeVjHJ7rp2ENkBtd0k3KuEo0XKfGV/ZZsKbFtea444NFEStRhbuABbjRjIY6KATt0431609fspjhN8Zfo4Yt9oLIuwz82PeIQA7JEkelN6cTHGUn8G6GG5ajx4hClm6rEp3loO8VpCrCbAfDRbQrROOCZS1kyUoBHrbJfzD0A7zwMGI029tuQjx5FKz+DWjWpLsvIenNZUGY+wPDWaYyw2D/AqjDoBwD9Yc/z9y4vwJO8OjgwbsEfw+CaMFXgNb42pSajcXsGp8ODnIC1HG8ePCfkcRVKapVvlMMJFwlW1mldy33X8g9fYEQ352wGuLOx8JwC4hGrpirtMPUrxW77PVMWzahmbzFtFwxss7fqpsJuM0IVl81QF1x2jzS6g1F0Mdw3xfilIEzpPStye0+hJ1E0xaUSvUrh1k67ZHFXBJQPEf4THuDqi7ux/8JTx4TWdyukyLIpFEmg2D2Q6iD1BUd0S2DIAI7YATLKDTyy86DbDf/Ub3ogVCqj/rjzKOBRqS7i+0dM73qMK9t8KSnkzulJjk/gNZ1g41FLm4eqMSbLPSQbMXGzou2wZetPowXCQmZ/CdNNani8n89kQeofUCRftPjg7oM92lgAyshkZ7pA3aGi5lrenKP91UBqVrCZjvBl+HAT5AmpFiUI3BfaByOOE= X-MS-TrafficTypeDiagnostic: PA4PR01MB9452:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kIklfLX2CH+JqgHHCZtRTbQJTjIAzdo8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ssFZbzc9R6tPyfRB8kLCFPwF73vYwEL14wcqeumCy37t1Fl1KM6EzLY7XHkgnFCGTkJoh1445i94qrRFQInUTNNSm5GQZrPbkJ1ae7lqW01GOjAfiEp0VP8dqSmAs49dp/JmpzvGZwaG1XzRryGQOz6zZXaLXFDqmdoFRf89aRUCJYqaxCQsCvrjjENOIHAL6uIA8eqwiFHxoJ0iJjX5+lDFLosDOPbKzRwBWiVhORkcubLJno4hjvQz3Nfq2m8ZFBM3lTMO5Vj5D3pLy88YgJaOaB19bYWiUrdNo0J+wYxR4ZTx7TH6X/5wmy9VI64G/XuWFi2QhneZgHl9EqkQv47uMF8cj8on03xKwK6aoP1AAEqc0xUImwV21edruShg+PgsnYkMe4pLnJ+TP6sVkkx1UQNhGwi1C9shvETpGeW/0oB2+zZaSz4FwAZZgx9d9hkwnP2PyUeSAKv1aTdR+3pwColM4iXJLgE9+TUiaCPXLpUC8xSiSlCITwvLR4OAX7/8zls3xsj5LpvVCS0oZcmW1n5qlsvNl5MFeTDtKYPgkT8CkBeSt4uEJOJOgMKddS55jnueTwY6ZuJf1hySlleTPpJq/fC2P2BTVowvJi/0A3jtVcezgAKpv+k2MrpzkNs8mA2r09yFW95uGdlFaXVk0mISGURiCF0GwdM7Xk6pIri6AeL9J3YvS0dZzq2/zvKgEhifWJYgaOzmNcPrk9T4iTRDTnHNE/UnHifoCFWGGetn5UfRnzc6x3bxm3A9RstTFzJU7dJEJj5/OAiilnoTeA1PUcuyhwB3XvaY71Tnle1SCoZ18tYOmBRnPzd6TVc1Xkn4TM5UoXDYHfD4jU63S2wnYRPeg34wsDPfw27zanNl1zgeuk3HEBF+Tj++nu4Vb5S/ISqBI8yTf6ljbq6tg8N9OSM= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e45e0706-5c18-4039-f800-08da5ec4f08c X-MS-Exchange-CrossTenant-AuthSource: DB6PR0101MB2214.eurprd01.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Jul 2022 20:28:37.1336 (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: PA4PR01MB9452 Subject: [FFmpeg-devel] [PATCH 5/8] avformat/matroskaenc: Use av_fast_realloc_array for index entries 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: Qv9vMIGIgMms Currently, the Matroska muxer reallocates its array of index entries each time another entry is added. This is bad performance-wise, especially on Windows where reallocations are slow. This is solved by switching to av_fast_realloc_array() which ensures that actual reallocations will happen only seldomly. For an (admittedly extreme) example which consists of looping a video consisting of a single keyframe of size 4KB 540000 times this improved the time for writing a frame from 23524201 decicycles (516466 runs, 7822 skips) to 225240 decicycles (522122 runs, 2166 skips) on Windows. (Writing CRC-32 elements was disabled for these tests.) Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 1256bdfe36..7c7de612de 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -168,7 +168,8 @@ typedef struct mkv_cuepoint { typedef struct mkv_cues { mkv_cuepoint *entries; - int num_entries; + size_t num_entries; + size_t allocated_entries; } mkv_cues; struct MatroskaMuxContext; @@ -257,6 +258,10 @@ typedef struct MatroskaMuxContext { /** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */ #define MAX_CUETRACKPOS_SIZE 40 +/** Minimal size of CueTrack, CueClusterPosition and CueRelativePosition, + * and 1 + 1 bytes for the overhead of CueTrackPositions itself. */ +#define MIN_CUETRACKPOS_SIZE (1 + 1 + 3 * (1 + 1 + 1)) + /** 2 + 1 Simpletag header, 2 + 1 + 8 Name "DURATION", 23B for TagString */ #define DURATION_SIMPLETAG_SIZE (2 + 1 + (2 + 1 + 8) + 23) @@ -914,16 +919,20 @@ static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int64_t ts, int64_t cluster_pos, int64_t relative_pos, int64_t duration) { mkv_cues *cues = &mkv->cues; - mkv_cuepoint *entries = cues->entries; - unsigned idx = cues->num_entries; + mkv_cuepoint *entries; + size_t idx = cues->num_entries; + int ret; if (ts < 0) return 0; - entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint)); - if (!entries) - return AVERROR(ENOMEM); - cues->entries = entries; + ret = av_fast_realloc_array(&cues->entries, &cues->allocated_entries, + cues->num_entries + 1, + MAX_SUPPORTED_EBML_LENGTH / MIN_CUETRACKPOS_SIZE, + sizeof(*cues->entries)); + if (ret < 0) + return ret; + entries = cues->entries; /* Make sure the cues entries are sorted by pts. */ while (idx > 0 && entries[idx - 1].pts > ts)