diff --git a/patches/4.2/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch b/patches/4.2/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch new file mode 100644 index 0000000000..afe09fbd03 --- /dev/null +++ b/patches/4.2/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch @@ -0,0 +1,142 @@ +From 3996cdc26aab55db5b4213bac039c8c353100ca5 Mon Sep 17 00:00:00 2001 +From: Michael Fabian 'Xaymar' Dirks +Date: Sat, 22 May 2021 23:13:13 +0200 +Subject: [PATCH] avformat/matroskaenc: Allow changing the time stamp precision + via option + +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. +--- + doc/muxers.texi | 9 +++++++++ + libavformat/matroskaenc.c | 33 ++++++++++++++++++++++++++------- + 2 files changed, 35 insertions(+), 7 deletions(-) + +diff --git a/doc/muxers.texi b/doc/muxers.texi +index b109297963..30fcd4df4a 100644 +--- a/doc/muxers.texi ++++ b/doc/muxers.texi +@@ -1287,6 +1287,15 @@ Both eyes laced in one Block, Left-eye view is first + @item block_rl + Both eyes laced in one Block, Right-eye view is first + @end table ++ ++@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 + + For example a 3D WebM clip can be created using the following command line: +diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c +index ad7b0bf2c6..832a008930 100644 +--- a/libavformat/matroskaenc.c ++++ b/libavformat/matroskaenc.c +@@ -161,6 +161,8 @@ typedef struct MatroskaMuxContext { + int64_t *stream_duration_offsets; + + int allow_raw_vfw; ++ ++ AVRational time_base; + } MatroskaMuxContext; + + /** 2 bytes * 7 for EBML IDs, 7 1-byte EBML lengths, 6 1-byte uint, +@@ -1863,6 +1865,7 @@ static int mkv_write_header(AVFormatContext *s) + AVDictionaryEntry *tag; + int ret, i, version = 2; + int64_t creation_time; ++ int64_t time_base = 1; + + if (!strcmp(s->oformat->name, "webm")) { + mkv->mode = MODE_WEBM; +@@ -1870,6 +1873,14 @@ static int mkv_write_header(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); ++ } ++ } ++ + if (mkv->mode != MODE_WEBM || + av_dict_get(s->metadata, "stereo_mode", NULL, 0) || + av_dict_get(s->metadata, "alpha_mode", NULL, 0)) +@@ -1917,7 +1928,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)) { +@@ -1959,11 +1973,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 { +@@ -2038,12 +2052,12 @@ static int mkv_write_header(AVFormatContext *s) + // after 4k and on a keyframe + if (pb->seekable & AVIO_SEEKABLE_NORMAL) { + 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; + } +@@ -2752,10 +2766,14 @@ static int mkv_init(struct AVFormatContext *s) + } + + for (i = 0; i < s->nb_streams; i++) { +- // ms precision is the de-facto standard timescale for mkv files +- avpriv_set_pts_info(s->streams[i], 64, 1, 1000); ++ // Use user-defined timescale. ++ avpriv_set_pts_info(st, 64, mkv->time_base.num, mkv->time_base.den); + } + ++ // 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); ++ + return 0; + } + +@@ -2815,6 +2833,7 @@ 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 }, ++ { "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 }, + }; + +-- +2.31.1.windows.1 + diff --git a/patches/4.3/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch b/patches/4.3/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch new file mode 100644 index 0000000000..4f27b719af --- /dev/null +++ b/patches/4.3/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch @@ -0,0 +1,148 @@ +From 5db9a7a8ff3131adc566b2378fd84f9a6c9597a2 Mon Sep 17 00:00:00 2001 +From: Michael Fabian 'Xaymar' Dirks +Date: Wed, 19 May 2021 01:39:54 +0200 +Subject: [PATCH] avformat/matroskaenc: Allow changing the time stamp precision + via option + +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 | 9 +++++++++ + libavformat/matroskaenc.c | 32 ++++++++++++++++++++++++++------ + 2 files changed, 35 insertions(+), 6 deletions(-) + +diff --git a/doc/muxers.texi b/doc/muxers.texi +index c598abbe66..6b64a9edeb 100644 +--- a/doc/muxers.texi ++++ b/doc/muxers.texi +@@ -1407,6 +1407,15 @@ disposition default exists, no subtitle track will be marked as default. + 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 ++ ++@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 eaed02bc92..f048b97a84 100644 +--- a/libavformat/matroskaenc.c ++++ b/libavformat/matroskaenc.c +@@ -157,6 +157,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, +@@ -1789,6 +1791,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) || +@@ -1826,6 +1829,10 @@ static int mkv_write_header(AVFormatContext *s) + 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)) { +@@ -1858,11 +1865,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) { +@@ -1925,12 +1932,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; + } +@@ -2685,6 +2692,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->tracks = av_mallocz_array(s->nb_streams, sizeof(*mkv->tracks)); + if (!mkv->tracks) + return AVERROR(ENOMEM); +@@ -2707,8 +2722,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) { +@@ -2728,6 +2743,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); + +@@ -2794,6 +2813,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 }, + }; + +-- +2.31.1.windows.1 + diff --git a/patches/4.4/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch b/patches/4.4/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch new file mode 100644 index 0000000000..7c8e0d6bee --- /dev/null +++ b/patches/4.4/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch @@ -0,0 +1,148 @@ +From 9b88662cdf8d38b7d747b1c0eec85662f8eab2ec Mon Sep 17 00:00:00 2001 +From: Michael Fabian 'Xaymar' Dirks +Date: Wed, 19 May 2021 01:39:54 +0200 +Subject: [PATCH] avformat/matroskaenc: Allow changing the time stamp precision + via option + +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 }, + }; + +-- +2.31.1.windows.1 + diff --git a/patches/master/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch b/patches/master/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch new file mode 100644 index 0000000000..7c8e0d6bee --- /dev/null +++ b/patches/master/0001-avformat-matroskaenc-Allow-changing-the-time-stamp-p.patch @@ -0,0 +1,148 @@ +From 9b88662cdf8d38b7d747b1c0eec85662f8eab2ec Mon Sep 17 00:00:00 2001 +From: Michael Fabian 'Xaymar' Dirks +Date: Wed, 19 May 2021 01:39:54 +0200 +Subject: [PATCH] avformat/matroskaenc: Allow changing the time stamp precision + via option + +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 }, + }; + +-- +2.31.1.windows.1 +