From patchwork Tue Apr 2 13:33:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Diego Felix de Souza via ffmpeg-devel X-Patchwork-Id: 12576 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 91365447FF3 for ; Tue, 2 Apr 2019 16:35:22 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7AC3D68ACF8; Tue, 2 Apr 2019 16:35:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 61D1A68ACC5 for ; Tue, 2 Apr 2019 16:35:20 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id j9so16707088wrn.6 for ; Tue, 02 Apr 2019 06:35:20 -0700 (PDT) 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=qQFDdUbXcbuSnxvT1l/xOuPchsKXSQ2cEiU0yy/q+2s=; b=XDU2ViLUx+hp3x+7KBYHXzhrvNlQnN+XGcn07RaC0Sp+sXNJA1Y2HtAK6L4il0DOaX 7yxPxD6CFbCvXaT1CycYrZO/kVILqaBMAOqq9NCMbnpMHqVgUHXunOfpPIfIS9aH2RrF vRLSYpc9W5+lXgsdctMYnek8zVjR7foqNxSM7Djia3/XF5JiNRGQXgvdupuuk3969v0H L3Wm4vtOj+59SR33bU59rbya8Y8gCibccaqa9o6iaOA/Qs1J8DoZbpJYLwGM0eHGiBuC 5Yl7odw8Og+S6DItUpDTo9eMHqUfcWr3KrWaCxDcU9sSuvH5b8HOrLV8OSwBYOwrayJB CHAg== X-Gm-Message-State: APjAAAUWTnDgqGqBYsNt9HejlEeJFeeyTh565lHYoIFVSa0HbWVQ4mOq NESj09SAjZRLd+ImKEU3VxmScspWv/c= X-Google-Smtp-Source: APXvYqw0WsQBEotcn6ZOcQVtMcXs1O1ckpMPnZWwsQ16xrPWqLoDUXvo/5uggclchXv78JIuWsz9gQ== X-Received: by 2002:a5d:6887:: with SMTP id h7mr619900wru.122.1554212119710; Tue, 02 Apr 2019 06:35:19 -0700 (PDT) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id t24sm15566886wmi.10.2019.04.02.06.35.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2019 06:35:19 -0700 (PDT) To: ffmpeg-devel@ffmpeg.org Date: Tue, 2 Apr 2019 15:33:00 +0200 Message-Id: <20190402133305.3328-11-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190402133305.3328-1-andreas.rheinhardt@googlemail.com> References: <20190402133305.3328-1-andreas.rheinhardt@googlemail.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: , X-Patchwork-Original-From: Andreas Rheinhardt via ffmpeg-devel From: Diego Felix de Souza via ffmpeg-devel 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 89bd8d5d44..5edbba89e8 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)