From patchwork Sat May 2 17:16:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 19436 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 180A344BB45 for ; Sat, 2 May 2020 20:17:54 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id F21CA68C87D; Sat, 2 May 2020 20:17:53 +0300 (EEST) 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 741DE68BB49 for ; Sat, 2 May 2020 20:17:47 +0300 (EEST) Received: by mail-wr1-f67.google.com with SMTP id l18so4945430wrn.6 for ; Sat, 02 May 2020 10:17:47 -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=m2afAJXouO4SO1qFdxhXh3XS8DKewaJnmMx5V/+WaxI=; b=GoVyjiMdI0et75a4nozRjr/ngnBx88urwH4/Xyr1V3gbFBgKErdtYjpOctaLSs752p k9Mmcf83ZuUXgffbOC3SojccJ25Zv6CBGZpV3TdDaszVdpc+TJX42k9FLiB3r1vt6YDG fJVhwg5xaFugryb2ORqphINm0D2IXhayMmu2GZZj7qBpFh1iE52xcbO1lUKzfRXv4t7P zxEgWhFPJSxv2S8HYBozyFq3WwI6+FGUh4Ns+Ma5PHrACbvmTPMENCW5UsVOraMvMTtq I2F/6555J6l1D//RQhD21jAnjjHSM5CTh/69qOltubTHLQfo91ebe0DWpDXKk8UYmknU Qjww== 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=m2afAJXouO4SO1qFdxhXh3XS8DKewaJnmMx5V/+WaxI=; b=Vz5aokftm7U1++autKXMxvzrSpoVTrAbBTAS4ll6oITtrxeXZS7vQWoVld9HLCNvUW pE2zZ67tPNbNP6UI4f7gz5lF2pJrj7CzmK1pd3ilUtkEjYSqyMDgAv0Qvldf95xKC17n QrUqsGiCSzF8IidAdBLwn3tMlrMgb6cbV/gPTn2pI30WV2Zv2xWt/aPgL4PIUxjU7Q48 D19w3mxkbG90dnaOTBeS5rJr90DKXDnpmOomBuvGBqc5qYhMeohqUoyGI+16jS29V9bs P2QeIK7MTKacTFQYScegXQkeuO2CFOKksEVZ1zOHfmzXCq8wrlxl+UH3cjWuj96fMq0V UjVA== X-Gm-Message-State: AGi0PuZY76kCKRkCKUkbeUOw5H2i/YSpJXwR5UI6W0aKdrA8CDFtyO2L 4tSvDVv0tqIX1iYh+k/TIMdDbbiC X-Google-Smtp-Source: APiQypIVU28n32ztJ67q8miScE2CUhwOmyXaM326VFsmr1ZiljBxtvDyRcktzIFFeEjl6Tr0L8jJAA== X-Received: by 2002:a5d:45cf:: with SMTP id b15mr9776311wrs.78.1588439866425; Sat, 02 May 2020 10:17:46 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id u74sm5241524wmu.13.2020.05.02.10.17.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 May 2020 10:17:45 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sat, 2 May 2020 19:16:54 +0200 Message-Id: <20200502171700.28991-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200429222156.29129-1-andreas.rheinhardt@gmail.com> References: <20200429222156.29129-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 07/13] avformat/matroskaenc: Don't segfault when seekability changes 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" If the Matroska muxer's AVIOContext was unseekable when writing the header, but is seekable when writing the trailer, the code for writing the trailer presumes that a dynamic buffer exists and tries to update its content in order to overwrite data that has already been preliminarily written when writing the header, yet said buffer doesn't exist as it has been written finally and not preliminarily when writing the header (because of the unseekability it was presumed that one won't be able to update the data anyway). This commit adds a check for this and also for a similar situation involving updating extradata with new data from packet side-data. Signed-off-by: Andreas Rheinhardt --- It seems that the whole code was based around the presumption that the seekability does not change mid-stream. E.g. before add68dcca there were the way level 1 elements were written depended upon the seekability and the output would be corrupt if it changed between opening and closing. Given the lack of tickets about this it seems that this assumption holds true. Therefore this patch uses the existence of the Tracks buffer as proxy for seekability. libavformat/matroskaenc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 99b549ecc4..1a142403fa 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -2186,7 +2186,7 @@ static int mkv_check_new_extra_data(AVFormatContext *s, const AVPacket *pkt) switch (par->codec_id) { case AV_CODEC_ID_AAC: - if (side_data_size && (s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) { + if (side_data_size && mkv->track.bc) { int filler, output_sample_rate = 0; ret = get_aac_sample_rates(s, side_data, side_data_size, &track->sample_rate, &output_sample_rate); @@ -2213,7 +2213,7 @@ static int mkv_check_new_extra_data(AVFormatContext *s, const AVPacket *pkt) } break; case AV_CODEC_ID_FLAC: - if (side_data_size && (s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live) { + if (side_data_size && mkv->track.bc) { uint8_t *old_extradata = par->extradata; if (side_data_size != par->extradata_size) { av_log(s, AV_LOG_ERROR, "Invalid FLAC STREAMINFO metadata for output stream %d\n", @@ -2229,8 +2229,7 @@ static int mkv_check_new_extra_data(AVFormatContext *s, const AVPacket *pkt) // FIXME: Remove the following once libaom starts propagating extradata during init() // See https://bugs.chromium.org/p/aomedia/issues/detail?id=2012 case AV_CODEC_ID_AV1: - if (side_data_size && (s->pb->seekable & AVIO_SEEKABLE_NORMAL) && !mkv->is_live && - !par->extradata_size) { + if (side_data_size && mkv->track.bc && !par->extradata_size) { AVIOContext *dyn_cp; uint8_t *codecpriv; int codecpriv_size; @@ -2541,6 +2540,7 @@ static int mkv_write_trailer(AVFormatContext *s) if (ret < 0) return ret; + if (mkv->info.bc) { // update the duration av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration); avio_seek(mkv->info.bc, mkv->duration_offset, SEEK_SET); @@ -2549,6 +2549,7 @@ static int mkv_write_trailer(AVFormatContext *s) MATROSKA_ID_INFO, 0, 0, 0); if (ret < 0) return ret; + } if (mkv->track.bc) { // write Tracks master