From patchwork Thu Dec 26 10:53:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16973 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 1DE00449EFF for ; Thu, 26 Dec 2019 12:54:27 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 06BAB68AB80; Thu, 26 Dec 2019 12:54:27 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 97E5C6881B7 for ; Thu, 26 Dec 2019 12:54:25 +0200 (EET) Received: by mail-wr1-f47.google.com with SMTP id d16so23342386wre.10 for ; Thu, 26 Dec 2019 02:54:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=STrHXrDURsCgYdJsTGsFTNHIhC2wIaZZwjDfyv5nCn4=; b=isYa2emI9AiZ++FY2yNffRP6KVJaOjbj4m6nWo5BoFGFh4WHvu/qcnOYrWBdMBwugK 9Q5jRfR0tslAKPf/bAlsA2/axsxVCczQTBO6M08GdpYj6rhLuU25SpwrJ7VyERznR8/3 C52E5MQrdJPTvHsNRjJ71YGlYLXwihUzxKlcLvXo3roj+jeqRpmY2uw9Qg+39qyOcDpx xg3Zmtqo96zwH7+F0ohDX7E8iUmFq2xojzZngkjCsxirZMhIrVE7+J6MbheI7JiW/ddE qb0ru7/5lLvT1NHjV3Ys0+V5h1GWup6xyYgm5LxneNgqICJMHQq8vTb0qVubPVR0Zye0 7yIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=STrHXrDURsCgYdJsTGsFTNHIhC2wIaZZwjDfyv5nCn4=; b=p/NN8dAZmxBbqBUItvl7BwmlzZ7tFikvZ+ZJNqsIZs/YDqYKdOtkhzieL+19ZyyrLh S3+NeQHBYe9Ka1eNhHVXSgIYD1oiasVxff3vT/tjVB2j2Zk9eLo4FkLjX//+QlWoBOMn bug8q8HQj4BDNl2DhB1vok/Pf/OmVV2BsgDabVkvGikDMkaZ2TITycv5V7oCfa95BoOR K5A4S/TWVijpfT0YsV4NBbXqDDYj959poeTaSCrcHXT8q3jbXfzGkYQAigmrRe/JtWiD i8pYreif3+wEPoc5PsYrAP+IXpdpChupa+P63LaVmFcY3PjEXHACsVAHYv8fdmG7mXmu LxNg== X-Gm-Message-State: APjAAAV9nnshCGpdzISB4E6TzuOBWcbvxa0Jw9xVPcKrF7x510EWIfYg bPfGYagsiCzb/n640sOTcgLrLVr2 X-Google-Smtp-Source: APXvYqwLzP1uK7GJrt8wimiEpLhWUsZMTiWFgBlV0uACSBQZcl+1GwAeglyDdNIUpCR6BpBuEKVORQ== X-Received: by 2002:adf:e70d:: with SMTP id c13mr44876484wrm.248.1577357664592; Thu, 26 Dec 2019 02:54:24 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc10203.dynamic.kabel-deutschland.de. [188.193.2.3]) by smtp.gmail.com with ESMTPSA id c68sm7997618wme.13.2019.12.26.02.54.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Dec 2019 02:54:23 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Thu, 26 Dec 2019 11:53:40 +0100 Message-Id: <20191226105342.11175-15-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191226105342.11175-1-andreas.rheinhardt@gmail.com> References: <20191226105342.11175-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 15/17] avformat/matroskaenc: Use av_fast_realloc_array for index entries 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 --- The guy from ticket #8109 will probably be quite happy about this. libavformat/matroskaenc.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 469b604de6..72d19a9f5a 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -88,7 +88,8 @@ typedef struct mkv_cuepoint { typedef struct mkv_cues { int64_t segment_offset; mkv_cuepoint *entries; - int num_entries; + unsigned num_entries; + unsigned allocated_entries; } mkv_cues; typedef struct mkv_track { @@ -171,7 +172,7 @@ typedef struct MatroskaMuxContext { /** per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 3 8-byte uint max * and one 1-byte uint for the track number (this assumes MAX_TRACKS to be <= 255) */ -#define MAX_CUETRACKPOS_SIZE 35 +#define MAX_CUETRACKPOS_SIZE 35LL /** per-cuepoint - 1 1-byte EBML ID, 1 1-byte EBML size, 8-byte uint max */ #define MAX_CUEPOINT_CONTENT_SIZE(num_tracks) 10 + MAX_CUETRACKPOS_SIZE * num_tracks @@ -540,7 +541,8 @@ static int mkv_add_cuepoint(mkv_cues *cues, int stream, int tracknum, int64_t ts if (ts < 0) return 0; - entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint)); + entries = av_fast_realloc_array(entries, &cues->allocated_entries, + cues->num_entries + 1, sizeof(mkv_cuepoint)); if (!entries) return AVERROR(ENOMEM); cues->entries = entries; @@ -560,21 +562,21 @@ static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tra MatroskaMuxContext *mkv = s->priv_data; AVIOContext *dyn_cp, *pb = s->pb; int64_t currentpos; - int i, j, ret; + int ret; currentpos = avio_tell(pb); ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_CUES); if (ret < 0) return ret; - for (i = 0; i < cues->num_entries; i++) { + for (unsigned i = 0; i < cues->num_entries; i++) { ebml_master cuepoint, track_positions; mkv_cuepoint *entry = &cues->entries[i]; uint64_t pts = entry->pts; - int ctp_nb = 0; + unsigned ctp_nb = 0, j; // Calculate the number of entries, so we know the element size - for (j = 0; j < num_tracks; j++) + for (int j = 0; j < num_tracks; j++) tracks[j].has_cue = 0; for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) { int idx = entry[j].stream_idx; @@ -591,7 +593,7 @@ static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tra // put all the entries from different tracks that have the exact same // timestamp into the same CuePoint - for (j = 0; j < num_tracks; j++) + for (int j = 0; j < num_tracks; j++) tracks[j].has_cue = 0; for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) { int idx = entry[j].stream_idx;