From patchwork Wed Nov 6 02:49:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 16127 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 8413E447DB1 for ; Wed, 6 Nov 2019 04:50:57 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6670A68B0AD; Wed, 6 Nov 2019 04:50:57 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 630CC68B08F for ; Wed, 6 Nov 2019 04:50:52 +0200 (EET) Received: by mail-wm1-f68.google.com with SMTP id m17so1620728wmi.5 for ; Tue, 05 Nov 2019 18:50:52 -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=BUck6r2ecGEQCihGknLSc04W5Tn1m2kc2JDyg2LScHY=; b=kEdLNd2hGtAiUGul+wBmUH3njXclPmRRtLUB5J2LsDHBGmzNVGJUYQTuygBSfVpz1H FQV3Aa38G5FakgzHjFoazVZn7T/XCnpzhYchpkCXyIt0LkfeV1RRDMT7s57rEf9ItBOP aIt/iHszRNi9Xn09S6NctaW/e/GhZohcOlogablHjZwiZjJPU2akcVfGND+rvdtiETqI iTNIQkl8JhyEz9G9jWZKMYzB2rJUNCWKql5DsX8NJq8DnevVBdh6bPUcpXolW7n9N+rF 3HIPzGJrrKEj9g+hpyAwFKgPmGTXV+PK2fesntDp8kRCq6hhIHNl8Sgbwe+Z5BhkTZf+ +B8Q== 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=BUck6r2ecGEQCihGknLSc04W5Tn1m2kc2JDyg2LScHY=; b=Qgab4BOrTGz0QGSdU8lviyqbZBZFa4fey7v6tpKMZCYAxIc2vaxN1NimkHp+rfVoNM I3BEhngXDY0lvTGCtkcEouNr7h0Nxg9z3R+Ka1O2QnSd8M/2JMwA1bVoXOMkRdcgfwR0 R8q2/paCuCwrzBHyKD8F74yV4ssZFt0mL3xarDDvPumtgVBIVtW5UPfYt9GV4IUVDMSO RlWk3BZBKlNtUwPWNX2kG/VmaUdcxzxRmz3pgYqtwNk080/fwT2m1jI35bmvI7DO/pMJ TBRFQPY1qYycul4Ry/YU1Im4YUIoZhkKakFikeGitNf8txA7MNweRA6QZRRT8QTWkIDl eTuQ== X-Gm-Message-State: APjAAAV1dyy3UoZ5ElCkxpn6ApnxTxvDVLvx5KBV8y7CGsUFJzRST06k UO+5SrcEAH1R6pQzwdnsWEFT4yia X-Google-Smtp-Source: APXvYqxdq+D41T2HRA7Y7y7E8jSFsyckObc3vruBiDbonPqwUgqFBxJ5hanxMmbjUgVav5ttPfsG6A== X-Received: by 2002:a1c:2395:: with SMTP id j143mr306156wmj.128.1573008651723; Tue, 05 Nov 2019 18:50:51 -0800 (PST) Received: from sblaptop.fritz.box (ipbcc08937.dynamic.kabel-deutschland.de. [188.192.137.55]) by smtp.gmail.com with ESMTPSA id l13sm1281710wmh.12.2019.11.05.18.50.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Nov 2019 18:50:51 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Wed, 6 Nov 2019 03:49:14 +0100 Message-Id: <20191106024922.19228-15-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191106024922.19228-1-andreas.rheinhardt@gmail.com> References: <20191106024922.19228-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 15/23] avformat/matroskaenc: Don't use size of inexistent cluster 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" In order to determine whether the current cluster needs to be closed because of the limits on cluster-size and cluster-time, mkv_write_packet() would first get the size of the current cluster via applying avio_tell() on the dynamic buffer holding the current cluster. It did this without checking whether there is an open cluster right now, so that avio_tell() returned AVERROR(EINVAL) which worked as expected because the return value was put into a signed type (the call was unchecked). This is not good as it relied on avio_tell() (or actually, avio_seek()) to handle the case of an inexistent AVIOContext gracefully. Fixing this is easy: Only check if a cluster needs to be closed if a cluster is in fact open. Essentially the same happened with the tags in mkv_write_trailer(). It has been fixed, too. Signed-off-by: Andreas Rheinhardt --- I wonder whether we should rather check mkv->cluster_bc instead of mkv->cluster_pos != -1. I would have made a commit to do so if I understood this avio_write_marker stuff that seems be broken better (the check below should probably be mkv->cluster_pos != -1 instead of != 0). If opening a cluster fails, mkv->cluster_pos is still set; such problems would be automatically fixed by such a switch. libavformat/matroskaenc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 39ad5d1ccf..f84e50f94e 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -2354,6 +2354,7 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt) if (ret < 0) return ret; + if (mkv->cluster_pos != -1) { if (mkv->tracks[pkt->stream_index].write_dts) cluster_time = pkt->dts - mkv->cluster_pts; else @@ -2381,9 +2382,10 @@ static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt) start_new_cluster = 0; } - if (mkv->cluster_pos != -1 && start_new_cluster) { + if (start_new_cluster) { mkv_end_cluster(s); } + } if (!mkv->cluster_pos) avio_write_marker(s->pb, @@ -2498,7 +2500,7 @@ static int mkv_write_trailer(AVFormatContext *s) end_ebml_master_crc32(pb, &mkv->tracks_bc, mkv); // update stream durations - if (!mkv->is_live) { + if (mkv->tags_bc) { int i; int64_t curr = avio_tell(mkv->tags_bc); for (i = 0; i < s->nb_streams; ++i) { @@ -2522,8 +2524,6 @@ static int mkv_write_trailer(AVFormatContext *s) } } avio_seek(mkv->tags_bc, curr, SEEK_SET); - } - if (mkv->tags_bc && !mkv->is_live) { avio_seek(pb, mkv->tags_pos, SEEK_SET); end_ebml_master_crc32(pb, &mkv->tags_bc, mkv); }