From patchwork Fri Apr 19 23:41:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 12829 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 557574494CF for ; Sat, 20 Apr 2019 02:50:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 410F468AAE8; Sat, 20 Apr 2019 02:50:40 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 104B368AAA6 for ; Sat, 20 Apr 2019 02:50:35 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id o25so7616132wmf.5 for ; Fri, 19 Apr 2019 16:50:35 -0700 (PDT) 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=JeZqgs08FXHM7Fqipthn9YEaCxrKLHm+uSJ1W/iayTI=; b=GjNUkqwiWM7b4QhTZ6ICyY3KCNVwZwet5Kzk0UkSUz5BCsQXgTWbzh+Dg6/VnS1DxO S1jmbRw5stqN2XvoW7Yuw4orBR9vkhPZqEKZ0+cigZd/vp0hfyjWsuuX7jqfxNQiASmn vZztvf3nPH2hANAj8bcwVHMSmuzA3+iHt5eyY4ysRXGtd7vpwUhk3/m6yVra4oewF1CL 8/83JyrqTJsegGuM6j/dKahtrB+ei8YupSXgIncBgRc5UGQJXdeQe1bRwXJmay5FNuFQ SmFlU1iaIn8Y4PdzFnlezucKz7MrQqp3Ma2ZFHu6junt2opBf1D+DDN+mWRCXIryOG67 /mlQ== 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=JeZqgs08FXHM7Fqipthn9YEaCxrKLHm+uSJ1W/iayTI=; b=cx6Qfw0r2/cqBaj1rTHjqJnz2ieM2XrlLSrQBZW5Oun8vgQCLsx0Yj7GIbMkApppUs 7rnmHjxvRIltJKgZkMVI2AhLuNYBSMymkOUoZrw0ZO0JEEMpbaO8aR36MgoooWnasBvv pozqoZiaHlyz0hTOKuWjYKROh19V9vmHYI7Y72o9bskVqYIDBqxc1lAD8GgMZh7Lmadg e/fODi5Cwq1qyEvCFPZJlm5QPCtKpZt8C59mjdfwC52+eF8LgNycB7zZSXolZ3z8ZIjq 2eme/gW1FMoJV/9dvm3M7Z/FHMrGeFeZbhGeqdzILMznrg3p9i2+GBQIoFaYQmsBTs8b BAQg== X-Gm-Message-State: APjAAAUc0tNVa2uKjo3+v0a4/RJV6/SDvE38/B95bA1LnYdFMuVp+oYk 3a7jctGBrZhLxMByj7LUkaPuxO6e X-Google-Smtp-Source: APXvYqzTI3ZRtC5fceWGLEVAXUyn7yzhpinGsmRbI2DGHi1eC0V/Nv12orePxU1IqEAqLNiKJEP1Gg== X-Received: by 2002:a1c:4e0a:: with SMTP id g10mr4244103wmh.128.1555717835262; Fri, 19 Apr 2019 16:50:35 -0700 (PDT) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.gmail.com with ESMTPSA id t14sm6002395wmi.16.2019.04.19.16.50.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Apr 2019 16:50:34 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Apr 2019 01:41:05 +0200 Message-Id: <20190419234110.32980-10-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190419234110.32980-1-andreas.rheinhardt@gmail.com> References: <20190402133305.3328-1-andreas.rheinhardt@googlemail.com> <20190419234110.32980-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/15] avformat/matroskaenc: Avoid seeking when writing level 1 elements 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" Up until now, the writing process for level 1 elements (those elements for which CRC-32 elements are written by default) was this in case the output was seekable: Write the EBML ID, write an "unkown length" EBML number of the desired length, then write the element into a dynamic buffer, then write the dynamic buffer (after possible calculation and writing of the CRC-element), then seek back to the size element and overwrite the unknown-size element with the real size. The seeking and overwriting part has been eliminated by not writing the size initially. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 0be33419eb..2875552469 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -58,8 +58,9 @@ #include "libavcodec/internal.h" typedef struct ebml_master { - int64_t pos; ///< absolute offset in the file where the master's elements start - int sizebytes; ///< how many bytes were reserved for the size + int64_t pos; ///< absolute offset in the containing AVIOContext where the size field starts + ///< for level 1 elements or else where the master's elements start + int sizebytes; ///< how many bytes were reserved/shall be used for the size } ebml_master; typedef struct mkv_seekhead_entry { @@ -316,6 +317,7 @@ static ebml_master start_ebml_master(AVIOContext *pb, uint32_t elementid, uint64_t expectedsize) { int bytes = expectedsize ? ebml_num_size(expectedsize) : 8; + put_ebml_id(pb, elementid); put_ebml_size_unknown(pb, bytes); return (ebml_master) {avio_tell(pb), bytes }; @@ -340,7 +342,10 @@ static int start_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, Matros return ret; if (pb->seekable & AVIO_SEEKABLE_NORMAL) { - *master = start_ebml_master(pb, elementid, expectedsize); + int bytes = expectedsize ? ebml_num_size(expectedsize) : 8; + + put_ebml_id(pb, elementid); + *master = (ebml_master) { avio_tell(pb), bytes }; if (mkv->write_crc) put_ebml_void(*dyn_cp, 6); /* Reserve space for CRC32 so position/size calculations using avio_tell() take it into account */ } else @@ -357,13 +362,13 @@ static void end_ebml_master_crc32(AVIOContext *pb, AVIOContext **dyn_cp, Matrosk if (pb->seekable & AVIO_SEEKABLE_NORMAL) { size = avio_close_dyn_buf(*dyn_cp, &buf); + put_ebml_num(pb, size, master.sizebytes); if (mkv->write_crc) { skip = 6; /* Skip reserved 6-byte long void element from the dynamic buffer. */ AV_WL32(crc, av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), UINT32_MAX, buf + skip, size - skip) ^ UINT32_MAX); put_ebml_binary(pb, EBML_ID_CRC32, crc, sizeof(crc)); } avio_write(pb, buf + skip, size - skip); - end_ebml_master(pb, master); } else { end_ebml_master(*dyn_cp, master); size = avio_close_dyn_buf(*dyn_cp, &buf); @@ -383,8 +388,8 @@ static void end_ebml_master_crc32_preliminary(AVIOContext *pb, AVIOContext **dyn uint8_t *buf; int size = avio_get_dyn_buf(*dyn_cp, &buf); + put_ebml_num(pb, size, master.sizebytes); avio_write(pb, buf, size); - end_ebml_master(pb, master); } static void put_xiph_size(AVIOContext *pb, int size)