From patchwork Wed Jan 1 13:46:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17116 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 E8F07449BA5 for ; Wed, 1 Jan 2020 15:47:12 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C3F676881CD; Wed, 1 Jan 2020 15:47:12 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7A2A26880B2 for ; Wed, 1 Jan 2020 15:47:06 +0200 (EET) Received: by mail-wm1-f67.google.com with SMTP id q9so3552332wmj.5 for ; Wed, 01 Jan 2020 05:47:06 -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=kYjDxkQFQQ6bghDX5LgGtyTqaUPbb0Y1DpgHIAJoVzc=; b=h6SvcXhQdtzjyykCdMSGGsn4I/VwNf+4201CJzvCojJLxFbBS/JX9nQ3RhmCLkMf29 xOlWNuHFt4KkLaHaFvjr/yeZnFSOV8SdmwjpkSZo2ppPHmH7Rv6mXuax3jnlvjusjcOQ avZNykPooGv4ROv8jdTXVGN8qjz5/fUrOER9thknVyYExLUZUvkqcWj6AZUc3ApHsPHj lQGHPcvV4GDEpWNOboQ3YG4iIExbuqFmj0A/2bHF7+Zx7igCLglG2s0QtLamlBZlDwjo EFFslLx1TtCuWJ+qCFRyzID8t9CqaSxm4+1hedCLTJcpFnaDCi066etYXvN4kk5AVY5C fMSQ== 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=kYjDxkQFQQ6bghDX5LgGtyTqaUPbb0Y1DpgHIAJoVzc=; b=hNOIupzj5dHFO/HpWId9OEP0tGzpA4NLi3WnWOPCz5PGRwv+Tu23AuuZQOM8w72yif aGC4HgAg6hJtH6onfaYBWApnuC8+0BXqSnrAvSpHhLYCZVm8APgFFXWEDHH3fLZk2A+h BLOPLoMfrgw6cxi6o38PMkp884Ia5MK6crL/LoZbsSCpczrmoLClsYcOmQNMUR8klskX +mUngUCJ7W/TGiDbxj5FZcUWfmv4q9hV0p3q/SWB2wLS2uvpJ4bC2rf1lgdixid3gOeg MLX8d+Zz8m6XGdONu9MloRcIEBVVKi1tDaejO7XPgEGMOUzUVj0APvhuZTX8nz6Z9jv3 mnNw== X-Gm-Message-State: APjAAAU5FaZfwMztc9oJvl+zZlwcmTORA4ueJ5NXOEVqbSJAJQaD6SXo AH9ZbChYithQqUX24WdOn7tgcimD X-Google-Smtp-Source: APXvYqym6yD4eBzVmR+kjVQnYBJL2GVPi4cjXXHbo+TRMeMRPGZdKLordEQPk5g9k5S6Ulor2QTQSA== X-Received: by 2002:a05:600c:1003:: with SMTP id c3mr9485374wmc.47.1577886425753; Wed, 01 Jan 2020 05:47:05 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id z6sm54491593wrw.36.2020.01.01.05.47.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2020 05:47:05 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 1 Jan 2020 14:46:56 +0100 Message-Id: <20200101134657.12801-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200101132758.4452-1-andreas.rheinhardt@gmail.com> References: <20200101132758.4452-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 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 --- This patch will lead to a merge conflict with my recent Matroska muxer patchset [1] (in particular, [2]), but it is trivially fixable. I will update the other patchset as soon as one gets applied. [1]: https://ffmpeg.org/pipermail/ffmpeg-devel/2020-January/255139.html [2]: https://ffmpeg.org/pipermail/ffmpeg-devel/2020-January/255151.html libavformat/matroskaenc.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 469b604de6..2f5f8873db 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 @@ -535,15 +536,16 @@ static mkv_cues *mkv_start_cues(int64_t segment_offset) static int mkv_add_cuepoint(mkv_cues *cues, int stream, int tracknum, int64_t ts, int64_t cluster_pos, int64_t relative_pos, int64_t duration) { - mkv_cuepoint *entries = cues->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, UINT_MAX - 1, + sizeof(*cues->entries)); + if (ret < 0) + return ret; cues->entries[cues->num_entries].pts = ts; cues->entries[cues->num_entries].stream_idx = stream; @@ -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;