Bug 2483137

Summary: ffmpeg destroys time timestamps (PTS, DTS) of MKV file with H.264 video with B-frames
Product: [Fedora] Fedora Reporter: Takashi Yano <takashi.yano>
Component: ffmpegAssignee: Multimedia SIG <multimedia-sig>
Status: CLOSED ERRATA QA Contact:
Severity: medium Docs Contact:
Priority: unspecified    
Version: 45CC: asn, development.group, dominik, multimedia-sig, ngompa13
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
URL: https://cygwin.com/pipermail/cygwin/2026-May/259717.html
Whiteboard:
Fixed In Version: ffmpeg-9.0.2-3.fc46 ffmpeg-9.0.2-3.fc45 ffmpeg-8.1.3-2.fc44 Doc Type: ---
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2026-10-31 00:48:21 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Takashi Yano 2026-05-29 01:23:56 UTC
When you manipulate H.264 mkv video files having B-Frame, the timestamps (PTS, DTS) will be broken with the messages:
$ ffmpeg -i input.mkv -c copy output.mkv
[...]
[vost#0:0/copy @ 0x55b39a6ed200] Invalid DTS: 1376 PTS: 1293, replacing by guess
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 1336, current: 1293; changing to 1336. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 2127, current: 2085; changing to 2127. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 2294, current: 2252; changing to 2294. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 2503, current: 2419; changing to 2503. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 2503, current: 2461; changing to 2503. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 2711, current: 2628; changing to 2711. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 2711, current: 2669; changing to 2711. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 3045, current: 3003; changing to 3045. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 3212, current: 3128; changing to 3212. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 3212, current: 3170; changing to 3212. This may result in incorrect timestamps in the output file.
[vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 3420, current: 3337; changing to 3420. This may result in incorrect timestamps in the output file.

The cause is that ffmpeg-free package disables "h264" codec. Without "h264" codec, libavformat/demux.c fails to set AVContext has_b_frames correctly. 

Reproducible: Always

Steps to Reproduce:
1. Prepare H.264 mkv video file having B-frame.
2. Run ffmpeg -i input.mkv -c copy output.mkv
3. Run ffprobe -select_streams v -show_packets output.mkv |grep pts= |head -20

Actual Results:
The timestamp (PTS, DTS) of the output.mkv is broken.
pts=0
pts=250
pts=125
pts=125
pts=125
pts=167
pts=209
pts=375
pts=334
pts=334
pts=542
pts=459
pts=459
pts=501
pts=709
pts=626
pts=626
pts=667
pts=876
pts=792


Expected Results:
No warning messages are shown, and output.mkv is kept with valid timestamps.
pts=0
pts=250
pts=125
pts=42
pts=83
pts=167
pts=209
pts=375
pts=334
pts=292
pts=542
pts=459
pts=417
pts=501
pts=709
pts=626
pts=584
pts=667
pts=876
pts=792


Additional Information:
The following patch fixes the issue.

--- origsrc/ffmpeg-8.1.1/libavformat/demux.c	2026-05-26 16:39:49.600047200 +0900
+++ src/ffmpeg-8.1.1/libavformat/demux.c	2026-05-26 20:11:11.079674900 +0900
@@ -1022,6 +1022,13 @@ static void compute_pkt_fields(AVFormatC
     if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
         pkt->dts = AV_NOPTS_VALUE;
 
+#if !CONFIG_H264_DECODER
+    /* Workaround for incorrect H.264 DTS value */
+    if (sti->avctx->codec_id == AV_CODEC_ID_H264
+        && !sti->avctx->has_b_frames)
+        sti->avctx->has_b_frames = MAX_REORDER_DELAY;
+#endif
+
     if (pc && pc->pict_type == AV_PICTURE_TYPE_B
         && !sti->avctx->has_b_frames)
         //FIXME Set low_delay = 0 when has_b_frames = 1

Comment 1 Takashi Yano 2026-05-29 01:30:08 UTC
The broken .mkv file can be recovered using:
https://github.com/tyan0/fix_ts

Comment 2 Dominik 'Rathann' Mierzejewski 2026-06-05 12:14:09 UTC
(In reply to Takashi Yano from comment #0)
> When you manipulate H.264 mkv video files having B-Frame, the timestamps
> (PTS, DTS) will be broken with the messages:
> $ ffmpeg -i input.mkv -c copy output.mkv
> [...]
> [vost#0:0/copy @ 0x55b39a6ed200] Invalid DTS: 1376 PTS: 1293, replacing by
> guess
> [vost#0:0/copy @ 0x55b39a6ed200] Non-monotonic DTS; previous: 1336, current:
> 1293; changing to 1336. This may result in incorrect timestamps in the
> output file.

Confirmed, I can reproduce this if libavcodec-freeworld is not installed.

> The cause is that ffmpeg-free package disables "h264" codec. Without "h264"
> codec, libavformat/demux.c fails to set AVContext has_b_frames correctly. 

Confirmed.

> Additional Information:
> The following patch fixes the issue.

Have you submitted this patch upstream?

Comment 3 Dominik 'Rathann' Mierzejewski 2026-06-05 12:31:33 UTC
Reported upstream: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/23365 .

Comment 4 Takashi Yano 2026-08-24 10:58:10 UTC
I noticed that the same issue occurs also for HEVC.
The following patch fixes the both issues.

--- origsrc/ffmpeg-8.1.1/libavformat/demux.c	2026-05-26 16:39:49.600047200 +0900
+++ src/ffmpeg-8.1.1/libavformat/demux.c	2026-05-26 20:11:11.079674900 +0900
@@ -1022,6 +1022,14 @@ static void compute_pkt_fields(AVFormatC
     if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
         pkt->dts = AV_NOPTS_VALUE;
 
+#if !CONFIG_H264_DECODER || !CONFIG_HEVC_DECODER
+    /* Workaround for incorrect H.264/HEVC DTS value */
+    if ((sti->avctx->codec_id == AV_CODEC_ID_H264
+         || sti->avctx->codec_id == AV_CODEC_ID_HEVC)
+        && !sti->avctx->has_b_frames)
+        sti->avctx->has_b_frames = MAX_REORDER_DELAY;
+#endif
+
     if (pc && pc->pict_type == AV_PICTURE_TYPE_B
         && !sti->avctx->has_b_frames)
         //FIXME Set low_delay = 0 when has_b_frames = 1

Comment 5 Takashi Yano 2026-08-24 11:51:25 UTC
The following patch might be better.

--- origsrc/ffmpeg-9.0.1/libavformat/demux.c	2026-08-24 20:35:51.368562300 +0900
+++ src/ffmpeg-9.0.1/libavformat/demux.c	2026-08-24 20:42:40.164433200 +0900
@@ -993,6 +993,9 @@ static void compute_pkt_fields(AVFormatC
     int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
                        st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
                        st->codecpar->codec_id != AV_CODEC_ID_VVC;
+#if !CONFIG_H264_DECODER || !CONFIG_HEVC_DECODER
+    int need_dts_b_frame_fix = 0;
+#endif
 
     if (s->flags & AVFMT_FLAG_NOFILLIN)
         return;
@@ -1022,6 +1025,20 @@ static void compute_pkt_fields(AVFormatC
     if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
         pkt->dts = AV_NOPTS_VALUE;
 
+#if !CONFIG_H264_DECODER
+    if (sti->avctx->codec_id == AV_CODEC_ID_H264)
+        need_dts_b_frame_fix = 1;
+#endif
+#if !CONFIG_HEVC_DECODER
+    if (sti->avctx->codec_id == AV_CODEC_ID_HEVC)
+        need_dts_b_frame_fix = 1;
+#endif
+#if !CONFIG_H264_DECODER || !CONFIG_HEVC_DECODER
+    /* Workaround for incorrect H.264/HEVC DTS value */
+    if (need_dts_b_frame_fix && !sti->avctx->has_b_frames)
+        sti->avctx->has_b_frames = MAX_REORDER_DELAY;
+#endif
+
     if (pc && pc->pict_type == AV_PICTURE_TYPE_B
         && !sti->avctx->has_b_frames)
         //FIXME Set low_delay = 0 when has_b_frames = 1

Comment 6 Takashi Yano 2026-09-02 03:36:27 UTC
Claude has proposed an improvement with additional fixes for the issue as follows.


Fix H.264/HEVC DTS when the decoders are disabled.

With the H.264 and HEVC decoders disabled nothing sets
AVCodecContext.has_b_frames, so libavformat cannot turn the PTS the
container carries into DTS.  For Matroska, which stores no DTS at all,
the result is a stream copy with duplicated PTS - see
https://bugzilla.redhat.com/show_bug.cgi?id=2483137 .

Two parts:

1. The H.264 and HEVC parsers report the reorder depth from the SPS.
   Everything needed is already there: they parse the SPS, including out
   of avcC/hvcC extradata so length-prefixed input works, and h264_ps.o
   is linked whenever the parser is enabled.  The value is only ever
   raised, so a decoder that determined a larger depth still wins.  For
   HEVC the parser has to be allowed to run first, so drop the exception
   that skipped parsing for HEVC in matroskadec.c.

2. libavformat falls back to MAX_REORDER_DELAY only while the depth is
   genuinely unknown.  16 is the ceiling both specs allow, so it can
   never be too small, but it makes every DTS up to 16 frames early.
   Once a parser has read an SPS, has_b_frames == 0 means the stream
   really does not reorder and must be left alone.

Measured on this build (H.264 file from the bug report, 24000/1001,
35510 frames, real reorder depth 2; a 240 frame HEVC Matroska file; and
a 120 frame H.264 file encoded with -bf 0):

                     has_b_frames        packets without DTS
  H.264   before     16                  50
  H.264   after       2 (correct)        50
  HEVC    before     16                  16 of 240
  HEVC    after       2 (correct)         2 of 240
  -bf 0   before     16                  50
  -bf 0   after       0 (correct)        50

has_b_frames now matches the reorder depth derived independently from the
bitstream POC.  For the -bf 0 file DTS is now equal to PTS on every
packet instead of running a whole 667 ms ahead.  DTS stays monotonic with
dts <= pts in every case and the packet count never changes.  Letting the
HEVC parser run costs no measurable time on a 29 MB / 1439 frame file.

--- origsrc/ffmpeg-9.0.1/libavformat/demux.c	2026-09-02 03:00:00.000000000 +0900
+++ src/ffmpeg-9.0.1/libavformat/demux.c	2026-09-02 03:00:00.000000000 +0900
@@ -762,6 +762,13 @@
     if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
     if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
         return 1;
+#if !CONFIG_H264_DECODER
+    /* Without the decoder nb_decoded_frames stays at zero, so none of the
+     * guesses below can ever come true.  has_b_frames is whatever the
+     * parser reported (or the MAX_REORDER_DELAY fallback) and will not
+     * change any more, so there is nothing left to wait for. */
+    return 1;
+#endif
     av_assert0(sti->avctx->codec_id == AV_CODEC_ID_H264 || (sti->avctx->codec_id == AV_CODEC_ID_NONE && !avcodec_is_open(sti->avctx)));
 #if CONFIG_H264_DECODER
     if (sti->avctx->has_b_frames && avcodec_is_open(sti->avctx) &&
@@ -993,6 +1000,9 @@
     int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
                        st->codecpar->codec_id != AV_CODEC_ID_HEVC &&
                        st->codecpar->codec_id != AV_CODEC_ID_VVC;
+#if !CONFIG_H264_DECODER || !CONFIG_HEVC_DECODER
+    int need_dts_b_frame_fix = 0;
+#endif
 
     if (s->flags & AVFMT_FLAG_NOFILLIN)
         return;
@@ -1022,6 +1032,29 @@
     if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
         pkt->dts = AV_NOPTS_VALUE;
 
+#if !CONFIG_H264_DECODER
+    if (sti->avctx->codec_id == AV_CODEC_ID_H264)
+        need_dts_b_frame_fix = 1;
+#endif
+#if !CONFIG_HEVC_DECODER
+    if (sti->avctx->codec_id == AV_CODEC_ID_HEVC)
+        need_dts_b_frame_fix = 1;
+#endif
+#if !CONFIG_H264_DECODER || !CONFIG_HEVC_DECODER
+    /* Workaround for incorrect H.264/HEVC DTS value.
+     *
+     * Only apply it while the reorder depth is genuinely unknown.  The H.264
+     * and HEVC parsers report the depth taken from the SPS, so once a parser
+     * has read one, has_b_frames == 0 means the stream really does not
+     * reorder and forcing MAX_REORDER_DELAY would make every DTS that much
+     * too early.  pc is NULL exactly when no parser is in use (see the
+     * !need_parsing || !parser branch in read_frame_internal()), and
+     * pc->coded_width is still 0 until a parser has parsed an SPS. */
+    if (need_dts_b_frame_fix && !sti->avctx->has_b_frames &&
+        !(pc && pc->coded_width))
+        sti->avctx->has_b_frames = MAX_REORDER_DELAY;
+#endif
+
     if (pc && pc->pict_type == AV_PICTURE_TYPE_B
         && !sti->avctx->has_b_frames)
         //FIXME Set low_delay = 0 when has_b_frames = 1
--- origsrc/ffmpeg-9.0.1/libavcodec/h264_parser.c	2026-09-02 03:00:00.000000000 +0900
+++ src/ffmpeg-9.0.1/libavcodec/h264_parser.c	2026-09-02 03:00:00.000000000 +0900
@@ -27,6 +27,8 @@
 
 #define UNCHECKED_BITSTREAM_READER 1
 
+#include "config_components.h"
+
 #include <stdint.h>
 
 #include "libavutil/attributes.h"
@@ -383,6 +385,15 @@
             p->ps.sps = p->ps.pps->sps;
             sps       = p->ps.sps;
 
+#if !CONFIG_H264_DECODER
+            /* Report the reorder depth the SPS asks for.  Only the decoder
+             * sets has_b_frames, so without it libavformat has nothing to
+             * compute DTS from and has to assume MAX_REORDER_DELAY, which
+             * makes every DTS unnecessarily early. */
+            if (avctx->has_b_frames < sps->num_reorder_frames)
+                avctx->has_b_frames = sps->num_reorder_frames;
+#endif
+
             // heuristic to detect non marked keyframes
             if (p->ps.sps->ref_frame_count <= 1 && p->ps.pps->ref_count[0] <= 1 && s->pict_type == AV_PICTURE_TYPE_I)
                 s->key_frame = 1;
--- origsrc/ffmpeg-9.0.1/libavcodec/hevc/parser.c	2026-09-02 03:00:00.000000000 +0900
+++ src/ffmpeg-9.0.1/libavcodec/hevc/parser.c	2026-09-02 03:00:00.000000000 +0900
@@ -20,6 +20,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "config_components.h"
+
 #include "libavutil/common.h"
 #include "libavutil/mem.h"
 
@@ -95,6 +97,14 @@
     avctx->profile  = sps->ptl.general_ptl.profile_idc;
     avctx->level    = sps->ptl.general_ptl.level_idc;
 
+#if !CONFIG_HEVC_DECODER
+    /* The same value the decoder would set.  Reporting it here means
+     * libavformat gets the real reorder depth even without the decoder,
+     * instead of having to assume MAX_REORDER_DELAY. */
+    if (avctx->has_b_frames < sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics)
+        avctx->has_b_frames = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics;
+#endif
+
     if (sps->vps->vps_timing_info_present_flag) {
         num = sps->vps->vps_num_units_in_tick;
         den = sps->vps->vps_time_scale;
--- origsrc/ffmpeg-9.0.1/libavformat/matroskadec.c	2026-09-02 03:00:00.000000000 +0900
+++ src/ffmpeg-9.0.1/libavformat/matroskadec.c	2026-09-02 03:00:00.000000000 +0900
@@ -3042,7 +3042,9 @@
         bytestream_put_le32(&cropping, track->video.pixel_cropl);
         bytestream_put_le32(&cropping, track->video.pixel_cropr);
     }
-    if (par->codec_id != AV_CODEC_ID_HEVC)
+    /* HEVC needs parsing too when its decoder is not available: the
+     * parser is then the only thing that can report the reorder depth. */
+    if (!CONFIG_HEVC_DECODER || par->codec_id != AV_CODEC_ID_HEVC)
         sti->need_parsing = AVSTREAM_PARSE_HEADERS;
 
     if (track->default_duration) {

Comment 7 Fedora Update System 2026-10-30 23:18:12 UTC
FEDORA-2026-ca1ddaf1d9 (ffmpeg-9.0.2-3.fc46) has been submitted as an update to Fedora 46.
https://bodhi.fedoraproject.org/updates/FEDORA-2026-ca1ddaf1d9

Comment 8 Fedora Update System 2026-10-30 23:34:18 UTC
FEDORA-2026-3d4f4334d7 (ffmpeg-9.0.2-3.fc45) has been submitted as an update to Fedora 45.
https://bodhi.fedoraproject.org/updates/FEDORA-2026-3d4f4334d7

Comment 9 Fedora Update System 2026-10-30 23:35:18 UTC
FEDORA-2026-ddf8b57870 (ffmpeg-8.1.3-2.fc44) has been submitted as an update to Fedora 44.
https://bodhi.fedoraproject.org/updates/FEDORA-2026-ddf8b57870

Comment 10 Fedora Update System 2026-10-31 00:48:21 UTC
FEDORA-2026-ca1ddaf1d9 (ffmpeg-9.0.2-3.fc46) has been pushed to the Fedora 46 stable repository.
If problem still persists, please make note of it in this bug report.

Comment 11 Fedora Update System 2026-10-31 03:04:25 UTC
FEDORA-2026-ddf8b57870 has been pushed to the Fedora 44 testing repository.
Soon you'll be able to install the update with the following command:
`sudo dnf upgrade --enablerepo=updates-testing --refresh --advisory=FEDORA-2026-ddf8b57870`
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2026-ddf8b57870

See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.

Comment 12 Fedora Update System 2026-10-31 05:14:48 UTC
FEDORA-2026-3d4f4334d7 has been pushed to the Fedora 45 testing repository.
Soon you'll be able to install the update with the following command:
`sudo dnf upgrade --enablerepo=updates-testing --refresh --advisory=FEDORA-2026-3d4f4334d7`
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2026-3d4f4334d7

See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.

Comment 13 Fedora Update System 2026-10-31 18:54:16 UTC
FEDORA-2026-3d4f4334d7 (ffmpeg-9.0.2-3.fc45) has been pushed to the Fedora 45 stable repository.
If problem still persists, please make note of it in this bug report.

Comment 14 Fedora Update System 2026-11-03 01:39:02 UTC
FEDORA-2026-ddf8b57870 (ffmpeg-8.1.3-2.fc44) has been pushed to the Fedora 44 stable repository.
If problem still persists, please make note of it in this bug report.