From patchwork Wed Jan 1 00:58:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17092 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a05:6214:1865:0:0:0:0 with SMTP id eh5csp218643qvb; Tue, 31 Dec 2019 17:00:53 -0800 (PST) X-Google-Smtp-Source: APXvYqyibpgg04Qs8/4fZMsikFGeotxvUiTk87HYK0IRdL50j6HlOX1cBdRD/55I4ho9xWsITXtO X-Received: by 2002:aa7:d6d1:: with SMTP id x17mr43422891edr.57.1577840453060; Tue, 31 Dec 2019 17:00:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577840453; cv=none; d=google.com; s=arc-20160816; b=xPzS8Qq5ksGHfT4TkQS5VWtZUVqpqt8iaTNhFuvvO9ZqbhnZeyl+jIHgBRZbX8j3jV DhG1kG11arcYKYkb6eLpUJ5vwqkQcz/YHF5VaWn/c0TRc7RhQRsHSzCnoupnTO8YBz+U TJzV0zNLFW54B9t6CTuVvpTLsT2RpRvPBSBAiNxPlCJedkJwI4TAj3Rp1lcyjV9mAMEa cTz2Ms51yQZRLR4GRvlGiZ4msDvXhnx5qawNz9wBH7+Jl8/MdnyZp4gZR0aU93IL7D9E fEgtWRWm6aVKbrHL1bkaUKsCcUeiuHSO09bN5RnDq0qo7eY3Q2S+r1rvJIgFqgP9xgY3 EYKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=ge0C/663UADFnq8yUJb01j6FnyZCpQgabWlxKyl5V2A=; b=X0ko0wpRhEiV74mC96iLzqHxaZHN+AzI82nkazJQ2cP0jQbeSuP3QZBDSCALl9Q7a7 4nSsgn4CVCt4ySU56yLP+Nr6YmW2/8y4mrgUxHxa/ih2gHJsOZ8WcyReN+JkFSyMrpEk GccNSyVb8HoeC0d4wj3IU++lV10W7bK3lJzuPx1hiaM5o/jbzpmQApV5/FSwIdVnW0T8 HcTHEgjeOozTIBomha4or8U6S3NDSCwzhH+bdWg8cCGS85L5BWUxvzMlPoDnXi3I+JJU GyLcHdpwCQRiuC5YZVAJEDI01AW0LlLaiYmDCj0qgD9kpABZqJpuLxPqhl3oVGWp08HV LctQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=QE9QxobN; 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=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z13si32820557eja.299.2019.12.31.17.00.52; Tue, 31 Dec 2019 17:00:53 -0800 (PST) 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=@gmail.com header.s=20161025 header.b=QE9QxobN; 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=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C60F068AD91; Wed, 1 Jan 2020 02:59:20 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f67.google.com (mail-wr1-f67.google.com [209.85.221.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 25C3268AD85 for ; Wed, 1 Jan 2020 02:59:13 +0200 (EET) Received: by mail-wr1-f67.google.com with SMTP id g17so36197914wro.2 for ; Tue, 31 Dec 2019 16:59:13 -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=Fq5a8yG/pcTS7VKDvhIl4Y/IlGp96/3wOmxjFnJs9NU=; b=QE9QxobNh7OvY0knB9TDkmnXGqWamvXph9VVxc5m5tW8srM0p7uxRjvrmpqBT/KHax NcOkbRFOgwejqoBH5NynNW3uJzA2fgHza17gxgegK9RzwKvxrryOnNoka8opfP8JXSrD qTdoV/jnOlWjBBeT2aipEkOTzl7XzN+t575DEaWVUMd5u90EhPB6fWK3uL5lA/b/6/8N fPfflreXE6Ug9wZv9DlKirzbg8r+z09G3oit/EPifC06APkr/7M6B/3u0Ak00iGa/HDL wdPIp3NeeNfQlWN3i0vC+v9i/sXdVjkQZaByL/OXVNVzw14gDmqvgWgtjVQdt7deMpBV qVaQ== 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=Fq5a8yG/pcTS7VKDvhIl4Y/IlGp96/3wOmxjFnJs9NU=; b=LRaYolfqLdPiTKhGf4Rbs8C9tZLvswIiBYHMZx9Zr/A9bhVLjymvCiMWgSUowcUWk1 FyWtYd5aAACrOi4BrGDDpotthjbRw+p7tBbljoQiWWaWtLxiJtQb/D5HJR7fzgUkavqy Rxzr97Vj9QIon67WRIA8J/sBIwqKa6dnCwwWwpfaUGbg1NhanpRKqRSZ2GRZiJW/s4Sj +BGTUoxCo5SMqNjYNYOX/ZapN6+aKk+fOGlyjqbU/HV4+tQ2DyEAZHpZLVQYQ7bBrUgF qCw2x4eCWscIfsGOqt5pX80EKgqFQ4d8+xPDSsRjOGiGX2pH3swVINeinmqpHzaxK7eY TKjA== X-Gm-Message-State: APjAAAWV2Grl7BYgMMUp5VWWA5W1bQ2KtxaGt/wnuBa1aceDsfxVB6ta f/1pQjvZmh0d0BjlDEkPR84eF3n9 X-Received: by 2002:a5d:4386:: with SMTP id i6mr74101799wrq.63.1577840352237; Tue, 31 Dec 2019 16:59:12 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08bbf.dynamic.kabel-deutschland.de. [188.192.139.191]) by smtp.gmail.com with ESMTPSA id w17sm52431655wrt.89.2019.12.31.16.59.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Dec 2019 16:59:11 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 1 Jan 2020 01:58:31 +0100 Message-Id: <20200101005837.11356-15-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200101005837.11356-1-andreas.rheinhardt@gmail.com> References: <20200101005837.11356-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 14/20] avformat/matroskadec: Simplify writing Cues 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" X-TUID: XeUf3TS18jiy Content-Length: 8884 When the Matroska muxer writes the Cues (the index), it groups index entries with the same timestamp into the same CuePoint to save space. But given Matroska's variable-length length fields, it either needs to have an upper bound of the final size of the CuePoint before writing it or the CuePoint has to be assembled in a different buffer, so that after having assembled the CuePoint (when the real size is known), the CuePoint's header can be written and its data copied after it. The first of these approaches is the currently used one. This entails finding out the number of entries in a CuePoint before starting the CuePoint and therefore means that the list is read at least twice. Furthermore, a worst-case upper-bound for the length of a single entry was used, so that sometimes bytes are wasted on length fields. This commit switches to the second approach. This is no longer more expensive than the current approach if one only resets the dynamic buffer used to write the CuePoint's content instead of opening a new buffer for every CuePoint: Writing the trailer of a file with 540.000 CuePoints improved actually from 219054414 decicycles to 2164379394 decicycles (based upon 50 iterations). Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 74 +++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 2c4d3c338f..6f0760fa69 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -170,12 +170,8 @@ typedef struct MatroskaMuxContext { * offset, 4 bytes for target EBML ID */ #define MAX_SEEKENTRY_SIZE 21 -/** 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 - -/** 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 +/** 4 * (1-byte EBML ID, 1-byte EBML size, 8-byte uint max) */ +#define MAX_CUETRACKPOS_SIZE 40 /** Seek preroll value for opus */ #define OPUS_SEEK_PREROLL 80000000 @@ -509,58 +505,54 @@ static int mkv_add_cuepoint(MatroskaMuxContext *mkv, int stream, int tracknum, i static int64_t mkv_write_cues(AVFormatContext *s, mkv_cues *cues, mkv_track *tracks, int num_tracks) { MatroskaMuxContext *mkv = s->priv_data; - AVIOContext *dyn_cp, *pb = s->pb; + AVIOContext *dyn_cp, *pb = s->pb, *cuepoint; 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++) { - ebml_master cuepoint, track_positions; - mkv_cuepoint *entry = &cues->entries[i]; - uint64_t pts = entry->pts; - int ctp_nb = 0; - - // Calculate the number of entries, so we know the element size - for (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; + ret = avio_open_dyn_buf(&cuepoint); + if (ret < 0) { + ffio_free_dyn_buf(&dyn_cp); + return ret; + } - av_assert0(idx >= 0 && idx < num_tracks); - if (tracks[idx].has_cue && s->streams[idx]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) - continue; - tracks[idx].has_cue = 1; - ctp_nb ++; - } + for (mkv_cuepoint *entry = cues->entries, *end = entry + cues->num_entries; + entry < end;) { + uint64_t pts = entry->pts; + uint8_t *buf; + int size; - cuepoint = start_ebml_master(dyn_cp, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_CONTENT_SIZE(ctp_nb)); - put_ebml_uint(dyn_cp, MATROSKA_ID_CUETIME, pts); + put_ebml_uint(cuepoint, MATROSKA_ID_CUETIME, pts); // 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; + do { + ebml_master track_positions; + int idx = entry->stream_idx; + av_assert0(idx >= 0 && idx < num_tracks); if (tracks[idx].has_cue && s->streams[idx]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) continue; tracks[idx].has_cue = 1; - track_positions = start_ebml_master(dyn_cp, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE); - put_ebml_uint(dyn_cp, MATROSKA_ID_CUETRACK , entry[j].tracknum ); - put_ebml_uint(dyn_cp, MATROSKA_ID_CUECLUSTERPOSITION , entry[j].cluster_pos); - put_ebml_uint(dyn_cp, MATROSKA_ID_CUERELATIVEPOSITION, entry[j].relative_pos); - if (entry[j].duration != -1) - put_ebml_uint(dyn_cp, MATROSKA_ID_CUEDURATION , entry[j].duration); - end_ebml_master(dyn_cp, track_positions); - } - i += j - 1; - end_ebml_master(dyn_cp, cuepoint); - } + track_positions = start_ebml_master(cuepoint, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE); + put_ebml_uint(cuepoint, MATROSKA_ID_CUETRACK , entry->tracknum ); + put_ebml_uint(cuepoint, MATROSKA_ID_CUECLUSTERPOSITION , entry->cluster_pos); + put_ebml_uint(cuepoint, MATROSKA_ID_CUERELATIVEPOSITION, entry->relative_pos); + if (entry->duration != -1) + put_ebml_uint(cuepoint, MATROSKA_ID_CUEDURATION , entry->duration); + end_ebml_master(cuepoint, track_positions); + } while (++entry < end && entry->pts == pts); + size = avio_get_dyn_buf(cuepoint, &buf); + put_ebml_binary(dyn_cp, MATROSKA_ID_POINTENTRY, buf, size); + ffio_reset_dyn_buf(cuepoint); + } + ffio_free_dyn_buf(&cuepoint); end_ebml_master_crc32(pb, &dyn_cp, mkv); return currentpos;