From patchwork Wed Jan 1 00:58:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 17094 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a05:6214:1865:0:0:0:0 with SMTP id eh5csp219310qvb; Tue, 31 Dec 2019 17:01:20 -0800 (PST) X-Google-Smtp-Source: APXvYqxwgK1n4HjFk4bF+D8YKkoib+PE8WALm6UTfdABN6HxuPtK316vlBA+yQImVPA4VXSEKZJw X-Received: by 2002:a17:907:20a8:: with SMTP id pw8mr78050016ejb.248.1577840479949; Tue, 31 Dec 2019 17:01:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1577840479; cv=none; d=google.com; s=arc-20160816; b=XTarLPr8M0SYpOQQInDhKuQWS2xLUY0lCMsa+XjHSnaSpm7pnEhbaAdB3DGixwG45w AAegjMZ40yw/rHbliG1S9clDq3Fm4/1HLyGY23nGfp6rEU6/2DeYyq9JQtRTXfWrqZqC j3GUWJ96SAp+DCaMvvkB7/K3WSKqtIwpwBRVIawCyZYQT8P2cqEJzCSOrkHTz2RLFGGl 1DxVlWyU0NZg9eSXocGcpDgu00O048mfkHjAjLs5s9MZecGm14ZMOTIMu2ah5okO4Xiq RGegS1IlVjd+c7USdmiaqII5rXNRQT3Ba6OUrPSPUg5LyWbyvSxnjbwRZ4wGOXwTqQzt jMmg== 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=XJkKNiAaOUXS0cagc0vw2vjaX+ZJfOrTG1eNBrbcCVc=; b=j7m2lM+xoND3d0VM8AAhFeePC9D0D7H3yqMTt/7I254a2gY18if4jUCvB+Ak45aJl1 unzKa1n336ozKOCvwy3QtmtLTfCh+rHdzD/hnMPy+TTmr1zwbTNs4O006DSkFH3KeyL6 Z7fd1DPZdndk/mMTCW2DxSN+pptTaUUyFsjkdxLT6awOY86O7TJSP82Xcv1JsjP4Cz6E EEe9e9P2wqodAuKNenjlaNlQ8WS7kyIWnF5yEOWiN3dIog3b2Q9JEB9YYqSQTBxj8FRn imYQCtpxx89e5gMEn3oRAtl4ffvTAovsyG41qYy6XMaZfX0pQBZl0sNdxcbsfJAImJh+ HO1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=YlUsfhMt; 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 z16si32567767edq.340.2019.12.31.17.01.19; Tue, 31 Dec 2019 17:01:19 -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=YlUsfhMt; 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 AD3A668AE3E; Wed, 1 Jan 2020 02:59:23 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9C17568AD93 for ; Wed, 1 Jan 2020 02:59:09 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id t2so36224818wrr.1 for ; Tue, 31 Dec 2019 16:59:09 -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=Jfplpd5gdlyL5xQxsrkxKor0RSuvOvGDevBT6u1RRd4=; b=YlUsfhMtCPbdx0zm2vMudjla+McVPuBXzGCNljKO9gVmI59vtlLVbty0C74IuHVeWz 3315W2q/JjjZFsro2UNb37w7f2dzRF4dOCYPGhZ5APp5qdgRwpy13IbcmlRXDrbABSzc PkT88Q0sWKKy6trXNMk9DyB+kdTIPQcpb5peo+pLskC+mngcHcDxT6zEQ3epnuuqN3XF 9/MluuBGXlUmquk0ujC7/mOnO15yJEAXMk15B581rJtwuJEKbjg+fzUwfvYDDnDuUeEd ate7m2LitvmdouVPGbliAtmecLfVzpbsZRwRA3Azf9MXoY0/PoMgtuALTIW18CWZg/G9 8/pw== 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=Jfplpd5gdlyL5xQxsrkxKor0RSuvOvGDevBT6u1RRd4=; b=lBPTBvghdjwSVuoe7eT/g5ST5sh5KOK7+sqeRVsWct4yPaMJzzDg4kt89bLSjpIFeo srXilCD6UyainzMCSFZbqq44BquNJUs/m7RtaSON3kkE2MSiRpNX7Ha2UINpFDyv032L e7jFEJZAc9KhlnW1k5AvMMVCl3yaRzakY1KM6fMEJvPuNTcPWUMDAEGHIkoAY008HPww lga98YToVVo8nZumvAJ2kj3CvE8lMyvnSxHgJGYNwHKn5kg42h3+mN5a2nCKdBVj0OdQ 10KPK2a4wOgV6rJPgXaoT330LzX6MoinNHnd1PUOAd3w5PCV5gAlLDVHpqIIbLUQNXVA 5+ow== X-Gm-Message-State: APjAAAWa/+lShv5lOxryZ8AxH3hdAa3ldAx4OrP6uoqZ3yMxrxUywF1F SPGi6XGlylsA2/yrbbwe12DY1luf X-Received: by 2002:adf:e74f:: with SMTP id c15mr77641386wrn.274.1577840349007; Tue, 31 Dec 2019 16:59:09 -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.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Dec 2019 16:59:08 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 1 Jan 2020 01:58:27 +0100 Message-Id: <20200101005837.11356-11-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 10/20] avformat/matroskaenc: Avoid unnecessary seek 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: BpYSsYuam9tH Content-Length: 5524 When writing the SeekHead (a form of index) at the end of the muxing process, mkv_write_seekhead() would first seek to the position where the SeekHead ought to be written, then write it there and seek back to the original position afterwards. Which means: To the end of the file. Afterwards, a seek to the beginning of the file is performed to update further values. This of course means that the second seek in mkv_write_seekhead() was unnecessary. This has been changed: A new parameter was added to mkv_write_seekhead() containing the destination for the second seek, effectively eliminating the seek to the end of the file after writing the SeekHead. Signed-off-by: Andreas Rheinhardt --- libavformat/matroskaenc.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index c7948fb643..8040c8311c 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -437,28 +437,26 @@ static void mkv_add_seekhead_entry(MatroskaMuxContext *mkv, uint32_t elementid, /** * Write the SeekHead to the file at the location reserved for it - * and seek back to the initial position. When error_on_seek_failure + * and seek to destpos afterwards. When error_on_seek_failure * is not set, failure to seek to the position designated for the - * SeekHead is not considered an error; failure to seek back afterwards - * is always an error. + * SeekHead is not considered an error; failure to seek to destpos + * afterwards is always an error. * * @return 0 on success, < 0 on error. */ static int mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv, - int error_on_seek_failure) + int error_on_seek_failure, int64_t destpos) { AVIOContext *dyn_cp; mkv_seekhead *seekhead = &mkv->seekhead; ebml_master seekentry; - int64_t currentpos, remaining, ret64; + int64_t remaining, ret64; int i, ret; - currentpos = avio_tell(pb); - if ((ret64 = avio_seek(pb, seekhead->filepos, SEEK_SET)) < 0) { if (error_on_seek_failure) return ret64; - goto seek_back; + goto seek; } ret = start_ebml_master_crc32(pb, &dyn_cp, mkv, MATROSKA_ID_SEEKHEAD); @@ -482,8 +480,8 @@ static int mkv_write_seekhead(AVIOContext *pb, MatroskaMuxContext *mkv, remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb); put_ebml_void(pb, remaining); -seek_back: - if ((ret64 = avio_seek(pb, currentpos, SEEK_SET)) < 0) +seek: + if ((ret64 = avio_seek(pb, destpos, SEEK_SET)) < 0) return ret64; return 0; @@ -1929,7 +1927,7 @@ static int mkv_write_header(AVFormatContext *s) return ret; if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) { - ret = mkv_write_seekhead(pb, mkv, 0); + ret = mkv_write_seekhead(pb, mkv, 0, avio_tell(pb)); if (ret < 0) return ret; } @@ -2533,16 +2531,16 @@ static int mkv_write_trailer(AVFormatContext *s) mkv_add_seekhead_entry(mkv, MATROSKA_ID_CUES, cuespos); } - ret = mkv_write_seekhead(pb, mkv, 1); + currentpos = avio_tell(pb); + + ret = mkv_write_seekhead(pb, mkv, 1, mkv->info_pos); if (ret < 0) return ret; // update the duration av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration); - currentpos = avio_tell(pb); avio_seek(mkv->info_bc, mkv->duration_offset, SEEK_SET); put_ebml_float(mkv->info_bc, MATROSKA_ID_DURATION, mkv->duration); - avio_seek(pb, mkv->info_pos, SEEK_SET); end_ebml_master_crc32(pb, &mkv->info_bc, mkv); // write tracks master