From patchwork Tue Jul 5 20:26:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 36682 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp3728223pzh; Tue, 5 Jul 2022 13:28:44 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sUKwpHX7jOOYm0BCHBroxtvcOq6fKZhsPax7nuQBR9dmel2hUcFM3CU6/8c9OD/Ocjzdsa X-Received: by 2002:a05:6402:5193:b0:435:9a5f:50a8 with SMTP id q19-20020a056402519300b004359a5f50a8mr48649292edd.212.1657052924555; Tue, 05 Jul 2022 13:28:44 -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 nc27-20020a1709071c1b00b0072697437875si19283254ejc.704.2022.07.05.13.28.44; Tue, 05 Jul 2022 13:28:44 -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=suCdN6lq; 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 3F3A468B95D; Tue, 5 Jul 2022 23:28:42 +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 5A79B68B685 for ; Tue, 5 Jul 2022 23:28:35 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=J/6f591Podjjdmb/hm2hjBWzSpqjMJeubCKkxS8KeGhoUr9XFmomRO0XiBUK2v5Tbk1uF1OB+uSTMvGe7VkxXXPe5JHqdWpOUZN1bxJ2S9D8qUahNO+GBM8kdY80d2iFSCnwSis8eGYglzHhYAxGZ4TouFLj0bl/HB1Er7804UPVsmqyBdoH9MA5WG2d+7fEk5UrwDIf+oHEGew6kJmcmmBuwX59xd93eAmL826WqeiPLbv04M5SOJ/obAZ852FjzCJZdx8bqMTJsXQuzHPhvHbkjRA+8WbB0vCiWgBJNmhGH/JMTx2sYW3tgKx5IJ+2AXi8ztmAmNGu9PKJUSPa2A== 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=I1C7dAEJSRm/AltnHsgvLYrmz+/rFCMro02uJrkjbDQ=; b=OOumlA0hchfMWvWJ38VBbNZ9wLebCHaBy5vULcq4MW+ruiWc/n9SCnmGw6SPMxw2WGDj69NNs4QmPePKqQrlPvXMzIadad9G4PrKyipfEoC6+tZwnCKPLAvziLEqqgSWwIQI4/NKjjFxz60E9DKHK43Ygo4s//6pjPmWJLXLLfHuvCiunCJQ4uL+FeIUrNPEXr2fvBSHAzWhTySbhoOEcgtpDDURZJ627R7zLn0Dxq0uVmqDw+mIH81MNFuc7C9W42BzEM1Z6/Q8ZEhwmHvtthbe9qo0/Ek1yRKxS1OXugNBE+5QDQ6ibM8/QTMAP8OyE9I6ZbOpETMA31p8+5w1OA== 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=I1C7dAEJSRm/AltnHsgvLYrmz+/rFCMro02uJrkjbDQ=; b=suCdN6lqtQ9akGrWkw8uVOvo/vxS3AoxhBJXSIcv3xP8Cq7uP5QuiTsCV3fTizJBADTixmkUwjUYpfADNFKAPvm1U067j3s+mqPQ7o13+xx8vMKl/cvy4PU+/2XCdi2hmf+WoDH4I9WISKcuS2Vrn7NqZNTm/ODmkvty7roPYSnvlWdMyPWp81/thOXaYBbJ1ExBEO/2jMa2+GOAAxCg4LWZStt+4JRPOLbyC8P78M9kYf/SE2SjgBJu1dSQ1Hilb9/KbvwKdK+8SBm7faXWcXaVaYCFd3qXgpPBs53lztVQ3+hSgUHKPI/UJQtLtPjCJnD7xzU6U7Qh1MVHAfp3Kg== 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:33 +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:33 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 5 Jul 2022 22:26:46 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [s8cspEMQZegGmKCm1/oantxtg7jx66D0] 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-3-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 72cecf00-9573-493b-74e6-08da5ec4ee88 X-MS-Exchange-SLBlob-MailProps: S/btQ8cKWiSJyDCj7LTlCpA5m0H/gq0AOX6LJbwm7j8WYTbmQcg1uZ6OCGkPl2lR4i89QQtIFePeUxvcXaMnKpSjqW0Rk9VYTodOngF5zt/z7U8M0fSNX+MK5rcurvOwnC9rvhpERQMW2183BQrrx0Js6Sr6Xqx/Q9sfVI/ecWSxJ+g6wzfJ20BknVnH0UxEwQvfr96zV6OezKMcA2WzJd7DQBjTJGjr1AbgOc13lAkOMfSq/2lD4Dcn/iwWQ7H1arGjoxD/yAgGFdiyxLweg7n1ePV3/JpyQteHP96dTE/b67rIPkWlhPhjmt9ikobeqA+Gg7JsH9gu9GLBH7PZ/1CpiwAJzuPYpbv2y6NpRDIvRvtThv0+lIwHgcdoSDHON2RjpAW9QSOEe5Sljsd4PfF4Ipd+qSkFKdTEVecVFIoR7AkWdd2A4z+0dcl452MFqcGefPki2icAV7on6Doxd098gLcptoujGEB3aQXDNNjpTPAOwxVzVYmHOImZUFsNVQD34qzMrSxMYYDrKW0fmj13XU58NuJ4n3tZQ6Ai4Y81uLzVar5MeABORrIlPgvl5PdbqYOFf7mz/5ahdZpyBzXKTr0xab2OEY6A35JidVyKQQXD6IWNxToxnES+Jzksxg+/5xmBfpO0V3Y+cRxwcIZxN9qhbTWPL40yQy+oeUTgT10jQJVxVAnevELvkWHPeFc8q0B+k+02LDTkomMklvhk/uMhgPf3WoKp0cZUSh5JO2vkppZ586+lW7Ya5YLhsHwMjunPf98= X-MS-TrafficTypeDiagnostic: PA4PR01MB9452:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2J47M2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RDrwlZ48y5fxp9IOwlhHGoUpU/4ZI+2RuuT6OnZNgA9HN9z+uNcBT0O7AIG8/RJLdhXSTPcTp3e1xVyMC44Y3fngiIkZI6BtKL4dS1keI/tLMpWJeADJAd/6GI9cWajE/tRX4s0bYFl5Y7Gv202Y8k2C7acB5ctYgtRfpoy/8gXHPYbx/330X10sn7fzE7KNZfQJvdhPXymXqqHiS6yq47d2ozt42YOIdTJ4a+6Zwf8isstpokgUSnWTJtSI0o98+pRrwypWs9bAT0+q5vLuvID48CZfpV5qDvh4U4dyN94N9ne9vWZ57OFuvSpZEHQ0w1gxS3PYoPalgIfJR57Fc4EZc17lQ1RsesJ++7i43t++QRrB+e53w+gs8h2SmDl2WnG/Z/fsDW2a1yT3fi7rw7Md3uPdndwKZU2jwHgvQt+jCiJMurCpGcuX+5f/ija2Xro9zH2rzXdu4V7poJh4931Y8vQeQvWxmxsrgFOAXB2Eg/A5pMrBSlfBJJg0LzUmLZYT2iudyvZTbTLIgaekykB95ZiZFXI5d3BcfFDHgGwP5S+QCiLOmi/qcE13sByDDxARcV5IRcTNoOnWV8OrzN25hCQTjrMErhvkKvUjcsHtlmD4UEC6qu8RwvnXp14LdowBrmm1y8uvtDSq/JlkbiL07eNR2FFeARnDM2lRkzHyUAmRz7WjXDK7dU4ntktYp01hzvO1y2DSCBT6C1kT5cP1U7VUwkK/qTx/pzRcrK8t9SeGmJ9Y/asgpjPLlAxvE2kxZEIsF3rUENMqipLaQd9j437ca8sVzz+cBdp5ricjZeTxsxw02gtolWudd4Ddjlm74h29awYd8yx9Yt1XmZM2Dk++1vQvpR4njkd29KiZSGn8n9AWK3beNUf8hUyLM/oXUd26MNGMdpNv3ZYUpHdZgutg1ygCL/HDUyIBlCAnJM8tclI4c6Nccu29g= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 72cecf00-9573-493b-74e6-08da5ec4ee88 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:33.7651 (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 4/8] avformat/flvenc: Use array instead of linked list for index 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: erbQFDbN88eR Using a linked list had very much overhead (the pointer to the next entry increased the size of the index entry struct from 16 to 24 bytes, not to mention the overhead of having separate allocations), so it is better to (re)allocate a continuous array for the index. av_fast_realloc_array() is used for this purpose, in order not to reallocate the array for each entry. It's feature of allowing to restrict the number of elements of the array is put to good use here: Each entry will lead to 18 bytes being written and the array is contained in an element whose size field has a length of three bytes, so that more than (2^24 - 1) / 18 entries make no sense. Signed-off-by: Andreas Rheinhardt --- libavformat/flvenc.c | 60 +++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index b6135b25bf..426135aa15 100644 --- a/libavformat/flvenc.c +++ b/libavformat/flvenc.c @@ -34,6 +34,8 @@ #include "libavutil/opt.h" #include "libavcodec/put_bits.h" +/* Each FLVFileposition entry is written as two AMF double values. */ +#define FILEPOSITION_ENTRY_SIZE (2 * 9) static const AVCodecTag flv_video_codec_ids[] = { { AV_CODEC_ID_FLV1, FLV_CODECID_H263 }, @@ -73,7 +75,6 @@ typedef enum { typedef struct FLVFileposition { int64_t keyframe_position; double keyframe_timestamp; - struct FLVFileposition *next; } FLVFileposition; typedef struct FLVContext { @@ -107,9 +108,9 @@ typedef struct FLVContext { int acurframeindex; int64_t keyframes_info_offset; - int64_t filepositions_count; FLVFileposition *filepositions; - FLVFileposition *head_filepositions; + unsigned filepositions_count; + size_t filepositions_allocated; AVCodecParameters *audio_par; AVCodecParameters *video_par; @@ -548,27 +549,20 @@ static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, i static int flv_append_keyframe_info(AVFormatContext *s, FLVContext *flv, double ts, int64_t pos) { - FLVFileposition *position = av_malloc(sizeof(FLVFileposition)); - - if (!position) { - av_log(s, AV_LOG_WARNING, "no mem for add keyframe index!\n"); - return AVERROR(ENOMEM); - } - - position->keyframe_timestamp = ts; - position->keyframe_position = pos; - - if (!flv->filepositions_count) { - flv->filepositions = position; - flv->head_filepositions = flv->filepositions; - position->next = NULL; - } else { - flv->filepositions->next = position; - position->next = NULL; - flv->filepositions = flv->filepositions->next; + /* The filepositions array is part of a metadata element whose size field + * is three bytes long; so bound the number of filepositions in order not + * to allocate more than could ever be written. */ + int ret = av_fast_realloc_array(&flv->filepositions, + &flv->filepositions_allocated, + flv->filepositions_count + 1, + (((1 << 24) - 1) - 10) / FILEPOSITION_ENTRY_SIZE, + sizeof(*flv->filepositions)); + if (ret < 0) { + av_log(s, AV_LOG_WARNING, "Adding entry to keyframe index failed.\n"); + return ret; } - flv->filepositions_count++; + flv->filepositions[flv->filepositions_count++] = (FLVFileposition){ pos, ts }; return 0; } @@ -733,7 +727,7 @@ static int flv_write_trailer(AVFormatContext *s) int64_t cur_pos = avio_tell(s->pb); if (build_keyframes_idx) { - FLVFileposition *newflv_posinfo; + const FLVFileposition *flv_posinfo = flv->filepositions; avio_seek(pb, flv->videosize_offset, SEEK_SET); put_amf_double(pb, flv->videosize); @@ -758,15 +752,13 @@ static int flv_write_trailer(AVFormatContext *s) avio_seek(pb, flv->keyframes_info_offset, SEEK_SET); put_amf_string(pb, "filepositions"); put_amf_dword_array(pb, flv->filepositions_count); - for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) { - put_amf_double(pb, newflv_posinfo->keyframe_position + flv->keyframe_index_size); - } + for (unsigned i = 0; i < flv->filepositions_count; i++) + put_amf_double(pb, flv_posinfo[i].keyframe_position + flv->keyframe_index_size); put_amf_string(pb, "times"); put_amf_dword_array(pb, flv->filepositions_count); - for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) { - put_amf_double(pb, newflv_posinfo->keyframe_timestamp); - } + for (unsigned i = 0; i < flv->filepositions_count; i++) + put_amf_double(pb, flv_posinfo[i].keyframe_timestamp); put_amf_string(pb, ""); avio_w8(pb, AMF_END_OF_OBJECT); @@ -1037,15 +1029,9 @@ static int flv_check_bitstream(AVFormatContext *s, AVStream *st, static void flv_deinit(AVFormatContext *s) { FLVContext *flv = s->priv_data; - FLVFileposition *filepos = flv->head_filepositions; - while (filepos) { - FLVFileposition *next = filepos->next; - av_free(filepos); - filepos = next; - } - flv->filepositions = flv->head_filepositions = NULL; - flv->filepositions_count = 0; + flv->filepositions_allocated = flv->filepositions_count = 0; + av_freep(&flv->filepositions); } static const AVOption options[] = {