From patchwork Mon Jun 24 21:16:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 13691 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 C7935447B1B for ; Tue, 25 Jun 2019 00:24:32 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 92148689D13; Tue, 25 Jun 2019 00:24:32 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 12CB0680987 for ; Tue, 25 Jun 2019 00:24:26 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id a15so705363wmj.5 for ; Mon, 24 Jun 2019 14:24:26 -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=dV/YwqcaKLwV46ZrgONPQ+8mbw2dW6XEJiTGjIPQjRk=; b=MMnO8iK1UwEwwQX56KqXr/1Z1L3nVxOn8cTlKirwU74EHYKVeskWominD0BvTxahqp nWGMDJOwG0UYFJnUDdfKybDoxqw+K8JlAILxE4Fbz6tiumMMCpBGfkAh0A8ewT8ol+qZ dPAucaV8Y6DsXPbjbYrP7XTYUZspIOzXTeqajqkf9Kuy/0wCyMC27gsWhfnwzksnC2Ou vgaB0rRZ53kKwI3y8mi1oXuzhrtYqFMfhdkW9PHd/586kluE+LY+olJipi99pcS4GhGD h3rOQRqZuDrqc5PKDnNblrRokGWMEMgGQC47dxFtXqGWS9WuBBZbX0B3jkcbqpkBVSuE 14kQ== 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=dV/YwqcaKLwV46ZrgONPQ+8mbw2dW6XEJiTGjIPQjRk=; b=MT9gkXPFRJ1qnyKwSPCLJQ6VVaQsdPryLnPTXekxAveX5/2Hp/5ZAfu3okfckFOWBA DRQK+VqtzuwTlVSOzkokrPmAUQjaSQe79rF7PGhIrIoWjzAdaQgaO1OODYnhWkEsP3HL GQDay4kXPnmL5IhF/hZfdlD+/re7Npd7SjM7X8M24yWaMYhtwsEBMPLy6EAGTKMuOswr BoQXzCIyQgVTpA1MessktngKyeojzB98YqCGHOhgMvD3KvcEzZKGxqe+0fP8ApN8n1Sa Yj3uZDsGsuEfRUzcB/qdgYFFLEUIlh5UVG/GGcz7x8TEIQJEgdrl9Yvum5x6QP2nmoUK Zm0w== X-Gm-Message-State: APjAAAUhqjN0KtumCQXWH/UAwYZKC8Ch0uA1CMCGnfzrWfoy86/Jqbq5 Uons+I2geisAHinMVZjt2kvLhJDN X-Google-Smtp-Source: APXvYqxOIRTklU4OTD8VksFKxTh5+B8IhPklaxN2jAZxkQ0r5GJrvjawkb6Bq+/jCyZ6qdLIeiy2Og== X-Received: by 2002:a1c:18a:: with SMTP id 132mr17422014wmb.101.1561411050409; Mon, 24 Jun 2019 14:17:30 -0700 (PDT) Received: from localhost.localdomain (ipbcc063db.dynamic.kabel-deutschland.de. [188.192.99.219]) by smtp.gmail.com with ESMTPSA id t1sm15229092wra.74.2019.06.24.14.17.29 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 24 Jun 2019 14:17:29 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Mon, 24 Jun 2019 23:16:18 +0200 Message-Id: <20190624211618.39134-1-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190622080906.GM3118@michaelspb> References: <20190622080906.GM3118@michaelspb> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] libavformat/mux: Fix audio_preload 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" Commit 31f9032b added the audio_preload feature; its goal is to interleave audio earlier than the rest. Unfortunately, it has never ever worked, because the check for whether a packet should be interleaved before or after another packet was completely wrong: When audio_preload vanishes, interleave_compare_dts returns 1 if the new packet should be interleaved earlier than the packet it is compared with and that is what the rest of the code expects. But the codepath used when audio_preload is set does the opposite. Also fixes potential undefined behaviour (namely signed integer overflow). Signed-off-by: Andreas Rheinhardt --- avformat.h contains the following note about audio_preload: "not all formats support this and unpredictable things may happen if it is used when not supported." Has this been added because of unpredictable results caused by the buggy check? This option seems to work fine as long as audio_preload is not in the range of max_interleave_duration. libavformat/mux.c | 22 ++++++++++++++-------- libavformat/version.h | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/libavformat/mux.c b/libavformat/mux.c index 83fe1de78f..5e1ecd8485 100644 --- a/libavformat/mux.c +++ b/libavformat/mux.c @@ -1001,15 +1001,21 @@ static int interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVStream *st2 = s->streams[next->stream_index]; int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts, st->time_base); - if (s->audio_preload && ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) != (st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) { - int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO); - int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - s->audio_preload*(st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO); - if (ts == ts2) { - ts= ( pkt ->dts* st->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)* st->time_base.den)*st2->time_base.den - -( next->dts*st2->time_base.num*AV_TIME_BASE - s->audio_preload*(int64_t)(st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)*st2->time_base.den)* st->time_base.den; - ts2=0; + if (s->audio_preload) { + int preload = st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO; + int preload2 = st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO; + if (preload != preload2) { + preload *= s->audio_preload; + preload2 *= s->audio_preload; + int64_t ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - preload; + int64_t ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - preload2; + if (ts == ts2) { + ts = ((uint64_t)pkt ->dts*st ->time_base.num*AV_TIME_BASE - (uint64_t)preload *st ->time_base.den)*st2->time_base.den + - ((uint64_t)next->dts*st2->time_base.num*AV_TIME_BASE - (uint64_t)preload2*st2->time_base.den)*st ->time_base.den; + ts2 = 0; + } + comp = (ts2 > ts) - (ts2 < ts); } - comp= (ts>ts2) - (ts