From patchwork Sun Apr 5 15:59: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: 18683 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a0c:c987:0:0:0:0:0 with SMTP id b7csp662107qvk; Sun, 5 Apr 2020 09:03:07 -0700 (PDT) X-Google-Smtp-Source: APiQypIp8CgIYindADNsLjSHYigWuV3+4hs9ZOBXsdgeq13I8zl+3K6ikaPf6erWUgvHji/dfiV9 X-Received: by 2002:a05:6402:b17:: with SMTP id bm23mr15847218edb.165.1586102587822; Sun, 05 Apr 2020 09:03:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586102587; cv=none; d=google.com; s=arc-20160816; b=DeGsybfpJ4MDULh7LYtJ0I4dWSd/60Bn7m/iL3UqCNfIiJAfSbR+5rRWNbV0BMRa14 r7Bne4aMiiCscBSUG1ZJBsnUYlg77KtUu48+bULoRgphK719OFfgnYW2BTjxZGA+WXYf jVjfgPevTJ35arq8GYWI6ZEdDJdvnjjGNFrgDkIA1WX8FERTALSkMPZ4pGdntE1GVkcQ IMkgwfTRK8LSMVkbDhMsuZ+pZuaJsmq0ojDmbr5vQ/IhgZ6mkrc/vaAEi12NUt67kBzk nPmzHlfKM5QY7Lb3CHaryAc+cYzwZhs4SR+kfd4XY2KHpT6ZaHP+MTRMS1m3qw5d+qv8 SQvQ== 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=7IJSQpAW+KIA2LvQxQNv4mNgVWAiXE5QuoC4AbeOQM8=; b=E3eNMomQ8krbR3qMNWjfRBfUND4DvdYi9q4NmpEOI//532mQezGdBuX2spTqWVehy0 DKjWHojv3+UD4OTvk2BLThfZN0sziWrWJ9XiZJhf4HEPaxXqmkkUyyTWXkObq8AByJeb dtZ1N0pF/k4Z5tt7CKf8PrtPJe+cqgAXjpxrYeTGqhUhVYc7DVlFfqSDMGJ5gy2Ug5w0 94XLEBWT8Q83qiSvQMVlUlCkgcq1gPSpevJXEhwQtWr2UR7SIwvzLGQKR6+ScwOD68LH mxSAcQPI7lHcGs26N5QpAxe1ET8KVsyJTpA7+++twofhGC8MlpjYhqjvcZmTOy5NcAkr 91Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=GFicUn7J; 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 v15si10462773eda.214.2020.04.05.09.03.07; Sun, 05 Apr 2020 09:03:07 -0700 (PDT) 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=GFicUn7J; 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 C50DB68B520; Sun, 5 Apr 2020 19:00:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3317C68B4C7 for ; Sun, 5 Apr 2020 19:00:19 +0300 (EEST) Received: by mail-wr1-f45.google.com with SMTP id w10so14471765wrm.4 for ; Sun, 05 Apr 2020 09:00:19 -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=6ezyr8/l+bddLWaB8K3mHhZm8HSKMlqxwUOBD7E7Hic=; b=GFicUn7JL8h++gIe9CzkC17wYENZ0CpmlJJFDyQrWnyhoc+wGC86bgkYCmj7Fk/NOQ CoBGEDfvNd7xuDKONqw5MisuqaEQYw7ffkV5XmKsU6ZjtWDdifz59vfMpSFecB3+UAQ3 umwi4ZDKlL5+Wj1L76ctY96Cl00qz0z6RyagMWhTy5ceJ3MCvnYIguVdSnf/2xNzYSbb 8ysYTJ2kjvYDBEoeo14CC2sxh1r8pHoxaXTzOtEVJaq1mXTySru0ox6UvZj3N3HXKEwp 0BuszBB4PoOtRxgX/OTZ34FvXipy4xvzX1mpx1UqF4KlZYq1u7lXznQi7fSXl8eNZ1tA Jwyg== 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=6ezyr8/l+bddLWaB8K3mHhZm8HSKMlqxwUOBD7E7Hic=; b=Vovv2IXCHDwdfnk0/++BB0O6Ic85gNpBN7WjFqWoY9RW8pbuRameb7sguUAeYtLlQp iZN2fF7qE5cLsf+Wkd7g0g4P08Jxy2lfE6PO/e4p8iFi7B8FiflK7Aerz4yB3EgtEXfr RO5RRJPQbeWaENvTjZ2xgyitQ1dTcg7KgOGchzfcGTtubumWXxViGSLhca6S9TA018AD HMx+o6ueAgF6tAMMmVR1UMbhywVVqbvOk+e8KbBrx1fpFM+SZahQKtk1dyKRw/k91zKv AKeNA38QTIsR5a0e7ysWCqRqZhDV+jqRSrKXaKKiMw4EfWOPov8/Ai3JNIcrUNz+ddqf XsTw== X-Gm-Message-State: AGi0PuYRlZNNQEbDhWKyHJm329WPrSRr/X4liPJXNwPEA2wSYaQ64Ur6 UVyKXqQMvh2uLpC9AUNCWLPKI6Ek X-Received: by 2002:adf:9b96:: with SMTP id d22mr20778463wrc.249.1586102417658; Sun, 05 Apr 2020 09:00:17 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id v21sm20014567wmh.26.2020.04.05.09.00.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Apr 2020 09:00:16 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 5 Apr 2020 17:59:27 +0200 Message-Id: <20200405155928.9323-20-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200405155928.9323-1-andreas.rheinhardt@gmail.com> References: <20200405155928.9323-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 19/20] avformat/matroskaenc: Redo handling of FlagDefault 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: S5n6+Eyzt6ft Content-Length: 13374 Up until now, the Matroska muxer would mark a track as default if it had the disposition AV_DISPOSITION_DEFAULT or if there was no track with AV_DISPOSITION_DEFAULT set; in the latter case even more than one track of a kind (audio, video, subtitles) was marked as default which is not sensible. This commit changes the logic used to mark tracks as default. There are now three modes for this: a) In the "infer" mode the first track of every type (audio, video, subtitles) with default disposition set will be marked as default; if there is no such track (for a given type), then the first track of this type (if existing) will be marked as default. This behaviour is inspired by mkvmerge. It ensures that the default flags will be set in a sensible way even if the input comes from containers that lack the concept of default flags. This mode is the default mode. b) The "infer_no_subs" mode is similar to the "infer" mode; the difference is that if no subtitle track with default disposition exists, no subtitle track will be marked as default at all. c) The "passthrough" mode: Here the track will be marked as default if and only the corresponding input stream had disposition default. This fixes ticket #8173 (the passthrough mode is ideal for this) as well as ticket #8416 (the "infer_no_subs" mode leads to the desired output). Signed-off-by: Andreas Rheinhardt --- I did not apply this with the last round of patches as I am still hoping that someone would come up with better names. Furthermore, AVFormatContext.nb_streams is actually an unsigned, yet I am using an int to run through it (this is no problem now and probably won't be one ever, because the nb_streams is always in the range of int); I could rewrite this to only use unsigned. Should I do it? doc/muxers.texi | 19 +++++++ libavformat/matroskaenc.c | 56 ++++++++++++++++--- tests/ref/fate/matroska-flac-extradata-update | 4 +- 3 files changed, 69 insertions(+), 10 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 3be1c89416..4083e24205 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -1358,6 +1358,25 @@ A safe size for most use cases should be about 50kB per hour of video. Note that cues are only written if the output is seekable and this option will have no effect if it is not. +@item default_mode +This option controls how the FlagDefault of the output tracks will be set. +It influences which tracks players should play by default. The default mode +is @samp{infer}. +@table @samp +@item infer +In this mode, for each type of track (audio, video or subtitle), if there is +a track with disposition default of this type, then the first such track +(i.e. the one with the lowest index) will be marked as default; if no such +track exists, the first track of this type will be marked as default instead +(if existing). This ensures that the default flag is set in a sensible way even +if the input originated from containers that lack the concept of default tracks. +@item infer_no_subs +This mode is the same as infer except that if no subtitle track with +disposition default exists, no subtitle track will be marked as default. +@item passthrough +In this mode the FlagDefault is set if and only if the AV_DISPOSITION_DEFAULT +flag is set in the disposition of the corresponding stream. +@end table @end table @anchor{md5} diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 3cbaaf9574..084e7ca416 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -60,6 +60,12 @@ * Info, Tracks, Chapters, Attachments, Tags and Cues */ #define MAX_SEEKHEAD_ENTRIES 6 +enum { + DEFAULT_MODE_INFER, + DEFAULT_MODE_INFER_NO_SUBS, + DEFAULT_MODE_PASSTHROUGH, +}; + typedef struct ebml_master { int64_t pos; ///< absolute offset in the containing AVIOContext where the master's elements start int sizebytes; ///< how many bytes were reserved for the size @@ -149,6 +155,7 @@ typedef struct MatroskaMuxContext { int wrote_chapters; int allow_raw_vfw; + int default_mode; uint32_t segment_uid[4]; } MatroskaMuxContext; @@ -1088,7 +1095,7 @@ static int mkv_write_stereo_mode(AVFormatContext *s, AVIOContext *pb, static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, AVStream *st, mkv_track *track, AVIOContext *pb, - int default_stream_exists) + int is_default) { AVCodecParameters *par = st->codecpar; ebml_master subinfo, track_master; @@ -1126,8 +1133,8 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv, // The default value for TRACKFLAGDEFAULT is 1, so add element // if we need to clear it. - if (default_stream_exists && !(st->disposition & AV_DISPOSITION_DEFAULT)) - put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, !!(st->disposition & AV_DISPOSITION_DEFAULT)); + if (!is_default) + put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, 0); if (st->disposition & AV_DISPOSITION_FORCED) put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGFORCED, 1); @@ -1357,7 +1364,7 @@ static int mkv_write_tracks(AVFormatContext *s) { MatroskaMuxContext *mkv = s->priv_data; AVIOContext *pb = s->pb; - int i, ret, default_stream_exists = 0; + int i, ret, video_default_idx, audio_default_idx, subtitle_default_idx; if (mkv->nb_attachments == s->nb_streams) return 0; @@ -1368,14 +1375,43 @@ static int mkv_write_tracks(AVFormatContext *s) if (ret < 0) return ret; - for (i = 0; i < s->nb_streams; i++) { - AVStream *st = s->streams[i]; - default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT; + if (mkv->default_mode != DEFAULT_MODE_PASSTHROUGH) { + int video_idx, audio_idx, subtitle_idx; + + video_idx = video_default_idx = + audio_idx = audio_default_idx = + subtitle_idx = subtitle_default_idx = -1; + + for (i = s->nb_streams - 1; i >= 0; i--) { + AVStream *st = s->streams[i]; + + switch (st->codecpar->codec_type) { +#define CASE(type, variable) \ + case AVMEDIA_TYPE_ ## type: \ + variable ## _idx = i; \ + if (st->disposition & AV_DISPOSITION_DEFAULT) \ + variable ## _default_idx = i; \ + break; + CASE(VIDEO, video) + CASE(AUDIO, audio) + CASE(SUBTITLE, subtitle) +#undef CASE + } + } + + video_default_idx = FFMAX(video_default_idx, video_idx); + audio_default_idx = FFMAX(audio_default_idx, audio_idx); + if (mkv->default_mode != DEFAULT_MODE_INFER_NO_SUBS) + subtitle_default_idx = FFMAX(subtitle_default_idx, subtitle_idx); } for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; + int is_default = mkv->default_mode == DEFAULT_MODE_PASSTHROUGH ? + st->disposition & AV_DISPOSITION_DEFAULT : + i == video_default_idx || i == audio_default_idx || + i == subtitle_default_idx; ret = mkv_write_track(s, mkv, st, &mkv->tracks[i], - mkv->tracks_bc, default_stream_exists); + mkv->tracks_bc, is_default); if (ret < 0) return ret; } @@ -2724,6 +2760,10 @@ static const AVOption options[] = { { "live", "Write files assuming it is a live stream.", OFFSET(is_live), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "allow_raw_vfw", "allow RAW VFW mode", OFFSET(allow_raw_vfw), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, { "write_crc32", "write a CRC32 element inside every Level 1 element", OFFSET(write_crc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS }, + { "default_mode", "Controls how a track's FlagDefault is inferred", OFFSET(default_mode), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MODE_INFER }, DEFAULT_MODE_INFER, DEFAULT_MODE_PASSTHROUGH, FLAGS, "default_mode" }, + { "infer", "For each track type, mark the first track of disposition default as default; if none exists, mark the first track as default.", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_INFER }, 0, 0, FLAGS, "default_mode" }, + { "infer_no_subs", "For each track type, mark the first track of disposition default as default; for audio and video: if none exists, mark the first track as default.", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_INFER_NO_SUBS }, 0, 0, FLAGS, "default_mode" }, + { "passthrough", "Use the disposition flag as-is", 0, AV_OPT_TYPE_CONST, { .i64 = DEFAULT_MODE_PASSTHROUGH }, 0, 0, FLAGS, "default_mode" }, { NULL }, }; diff --git a/tests/ref/fate/matroska-flac-extradata-update b/tests/ref/fate/matroska-flac-extradata-update index 890bf1ac52..8b575903e6 100644 --- a/tests/ref/fate/matroska-flac-extradata-update +++ b/tests/ref/fate/matroska-flac-extradata-update @@ -1,5 +1,5 @@ -2b82ee67ace359edb464e289486b3ebd *tests/data/fate/matroska-flac-extradata-update.matroska -2008 tests/data/fate/matroska-flac-extradata-update.matroska +332bf4d9c92d24478d2a218e81223433 *tests/data/fate/matroska-flac-extradata-update.matroska +2011 tests/data/fate/matroska-flac-extradata-update.matroska #extradata 0: 34, 0x7acb09e7 #extradata 1: 34, 0x7acb09e7 #extradata 2: 34, 0x443402dd