From patchwork Fri May 21 00:29:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Fabian 'Xaymar' Dirks X-Patchwork-Id: 27851 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:b214:0:0:0:0:0 with SMTP id b20csp13921iof; Thu, 20 May 2021 17:30:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGjem+pGUKO2YZEPcu2gIDkkfSYsR5zGVocP2LkHXpj1cPk9qUZBbsSebYNdP3HOm1m0Vx X-Received: by 2002:a17:906:ecf9:: with SMTP id qt25mr7288711ejb.55.1621557019761; Thu, 20 May 2021 17:30:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621557019; cv=none; d=google.com; s=arc-20160816; b=dNbPT/dGb7FmxISdJK/IqgkNw2UkjB0hxClUY1ELdLrB9OcZt22GU/ty+4mub+CcdA TGTmeZgjti3UghLwAjuNfC3OOEXJNsf+68+3k8vEMbiMhIqRKFMvOOK/r934pSd6gK5V cTX14RJULIfUS0BOryRSF+BYWX5tW2arBdGHXRtxgq2rppWfjVds70DcnsKRRs5h+h8Q gRHvSQGgsnWWmcYV1n4NGJWwIcApGk0W2eSvoOJNvD400Ag4/xgDM6LyUemyagq8XCR2 eiFkRKhODzKUgeFf4JN8oEs4cgvAyYBJ8m0pRT7zr3UxupYwW+/aWl0cPp1V8Edh1Vky wDkw== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=N54dcW7UgPbtcKzpOVAstnJgBe9uXCEK8Pd3aGT5wzg=; b=RHcp8cJ4lNzH8mf+Tic6qEc+lDr4HmhLDcMD4u9bhFo6uFNprqXGATB7NHR5O2UyZF WFthv81VOoExxTdJFqx4VuFIFvi19yUhsLUoLenPFh3N0xG5tvzTdsL5aVmnHNXzgR9i ewosddxjyKpuZpnhb5/b1DByVp56Lpt2OAewsL1y0ub1U2JD8U8scVHn0xaSpFH+vRTK IhXObtVfBy+KdRghO4dViCVxJho7hyGuHU1RIcuyR/PgevWMdQJSRJTHBrx2zMjzLRwT opcIVQPDNQhwh6vNKHTKaBqatPwhJHrY+p4AezDGdHoWjyVHEbTvvJlSDXAis3R52OKn YtaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@xaymar.com header.s=mail header.b=JXaE1ukd; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=xaymar.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id du20si4759938ejc.634.2021.05.20.17.30.18; Thu, 20 May 2021 17:30:19 -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=@xaymar.com header.s=mail header.b=JXaE1ukd; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=xaymar.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B16D56882CB; Fri, 21 May 2021 03:30:14 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.xaymar.com (mail.xaymar.com [88.99.2.119]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 98848680735 for ; Fri, 21 May 2021 03:30:08 +0300 (EEST) Received-SPF: Temperror (mailfrom) identity=mailfrom; client-ip=90.187.35.181; helo=localhost.localdomain; envelope-from=michael.dirks@xaymar.com; receiver= DKIM-Signature: v=1; a=rsa-sha256; c=simple/relaxed; d=xaymar.com; s=mail; t=1621557007; bh=AkSdjKlt2FgfMjv/br/TkIacYsClJmX9NEnUdUweC6k=; h=From:To:Cc:Subject:Date:From; b=JXaE1ukdpflb1+fb2q5Bt6l/7/kZ6sYmdlcD68J3d6bWwWsunbuMZeLlR/8pJu0vV Ykn3c0VHv4nSW1YKeAnh8RKftiP3yLAVyj1bBvFBdaYLkEZl3oua0wew03xhprJ+wT zOLScqZunrW4tsweWbmWHqu2oT2VxbUMzJw46hx4Xubp0p6Zxn+kdvt3oeqBYaRJda 9k9c25gyoSI1S4/MUpF6dHEpyJJTmpXTStfVjsVFPmZGciJ0MdPPS16ENOiz0xE51E MAvWcS1H6+9LW0i8VMU8fr7tHxNaUPyLusp58C+SE8suIT5kR7o4mnwVC6vutCaN4Z 4f6/5zBQzpYNw== Received: from localhost.localdomain (business-90-187-35-181.pool2.vodafone-ip.de [90.187.35.181]) by mail.xaymar.com (Postfix) with ESMTPSA id D72F3E0080; Fri, 21 May 2021 02:30:07 +0200 (CEST) From: michael.dirks@xaymar.com To: ffmpeg-devel@ffmpeg.org Date: Fri, 21 May 2021 02:29:54 +0200 Message-Id: <20210521002954.1684-1-michael.dirks@xaymar.com> X-Mailer: git-send-email 2.31.1.windows.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/matroskaenc: Allow changing the time stamp precision via option X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Michael Fabian 'Xaymar' Dirks Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: C/AKbWyMzEul From: Michael Fabian 'Xaymar' Dirks Adds "timestamp_precision" to the available options for Matroska muxing. The option enables users and developers to change the precision of the time stamps in the Matroska container up to 1 nanosecond, which can aid with the proper detection of constant and variable rate content. Work-around fix for: 259, 6406, 7927, 8909 and 9124. Signed-off-by: Michael Fabian 'Xaymar' Dirks --- doc/muxers.texi | 8 ++++++++ libavformat/matroskaenc.c | 33 ++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index e1c6ad0829..8655be94ff 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -1583,6 +1583,14 @@ bitmap is stored bottom-up. Note that this option does not flip the bitmap which has to be done manually beforehand, e.g. by using the vflip filter. Default is @var{false} and indicates bitmap is stored top down. +@item timestamp_precision +Sets the timestamp precision up to 1 nanosecond for Matroska/WebM, which can +improve detection of constant rate content in demuxers. Note that some poorly +implemented demuxers may require a timestamp precision of 1 millisecond, so +increasing it past that point may result in playback issues. Higher precision +also reduces the maximum possible timestamp significantly. +Default is @var{1/1000} (1 millisecond). + @end table @anchor{md5} diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index 186a25d920..1b911a648c 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -158,6 +158,8 @@ typedef struct MatroskaMuxContext { int default_mode; uint32_t segment_uid[4]; + + AVRational time_base; } MatroskaMuxContext; /** 2 bytes * 7 for EBML IDs, 7 1-byte EBML lengths, 6 1-byte uint, @@ -1814,6 +1816,7 @@ static int mkv_write_header(AVFormatContext *s) const AVDictionaryEntry *tag; int ret, i, version = 2; int64_t creation_time; + int64_t time_base = 1; if (mkv->mode != MODE_WEBM || av_dict_get(s->metadata, "stereo_mode", NULL, 0) || @@ -1850,7 +1853,10 @@ static int mkv_write_header(AVFormatContext *s) return ret; pb = mkv->info.bc; - put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000); + time_base = av_rescale_q(time_base, mkv->time_base, (AVRational){1, 1000000000}); + av_log(s, AV_LOG_DEBUG, "TimestampScale is: %" PRId64 " ns\n", time_base); + put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, time_base); + if ((tag = av_dict_get(s->metadata, "title", NULL, 0))) put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value); if (!(s->flags & AVFMT_FLAG_BITEXACT)) { @@ -1883,11 +1889,11 @@ static int mkv_write_header(AVFormatContext *s) int64_t metadata_duration = get_metadata_duration(s); if (s->duration > 0) { - int64_t scaledDuration = av_rescale(s->duration, 1000, AV_TIME_BASE); + int64_t scaledDuration = av_rescale_q(s->duration, AV_TIME_BASE_Q, mkv->time_base); put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration); av_log(s, AV_LOG_DEBUG, "Write early duration from recording time = %" PRIu64 "\n", scaledDuration); } else if (metadata_duration > 0) { - int64_t scaledDuration = av_rescale(metadata_duration, 1000, AV_TIME_BASE); + int64_t scaledDuration = av_rescale_q(metadata_duration, AV_TIME_BASE_Q, mkv->time_base); put_ebml_float(pb, MATROSKA_ID_DURATION, scaledDuration); av_log(s, AV_LOG_DEBUG, "Write early duration from metadata = %" PRIu64 "\n", scaledDuration); } else if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) { @@ -1948,12 +1954,12 @@ static int mkv_write_header(AVFormatContext *s) // after 4k and on a keyframe if (IS_SEEKABLE(pb, mkv)) { if (mkv->cluster_time_limit < 0) - mkv->cluster_time_limit = 5000; + mkv->cluster_time_limit = av_rescale_q(5000, (AVRational){1, 1000}, mkv->time_base); if (mkv->cluster_size_limit < 0) mkv->cluster_size_limit = 5 * 1024 * 1024; } else { if (mkv->cluster_time_limit < 0) - mkv->cluster_time_limit = 1000; + mkv->cluster_time_limit = av_rescale_q(1000, (AVRational){1, 1000}, mkv->time_base); if (mkv->cluster_size_limit < 0) mkv->cluster_size_limit = 32 * 1024; } @@ -2713,6 +2719,14 @@ static int mkv_init(struct AVFormatContext *s) } else mkv->mode = MODE_MATROSKAv2; + // WebM requires a timestamp precision of 1ms. + if (mkv->mode == MODE_WEBM) { + if (av_cmp_q(mkv->time_base, (AVRational){1, 1000}) != 0) { + av_log(s, AV_LOG_ERROR, "WebM requires 1ms timestamp precision\n"); + return AVERROR(EINVAL); + } + } + mkv->cur_audio_pkt = av_packet_alloc(); if (!mkv->cur_audio_pkt) return AVERROR(ENOMEM); @@ -2738,8 +2752,8 @@ static int mkv_init(struct AVFormatContext *s) track->uid = mkv_get_uid(mkv->tracks, i, &c); } - // ms precision is the de-facto standard timescale for mkv files - avpriv_set_pts_info(st, 64, 1, 1000); + // Use user-defined timescale. + avpriv_set_pts_info(st, 64, mkv->time_base.num, mkv->time_base.den); if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) { if (mkv->mode == MODE_WEBM) { @@ -2759,6 +2773,10 @@ static int mkv_init(struct AVFormatContext *s) track->track_num_size = ebml_num_size(track->track_num); } + // Scale the configured cluster_time_limit. + if (mkv->cluster_time_limit >= 0) + mkv->cluster_time_limit = av_rescale_q(mkv->cluster_time_limit, (AVRational){1, 1000}, mkv->time_base); + if (mkv->is_dash && nb_tracks != 1) return AVERROR(EINVAL); @@ -2826,6 +2844,7 @@ static const AVOption options[] = { { "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" }, + { "timestamp_precision", "Timestamp precision to use for the entire container", OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0.001 }, 0.000000001, INT_MAX, FLAGS}, { NULL }, };