PR #26097 opened by superuser404
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26097
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26097.patch

The mpegts demuxer reads the DOVI video stream descriptor (0xb0) into 
`AV_PKT_DATA_DOVI_CONF`, but the muxer never writes it. A Dolby Vision HEVC 
stream copied into MPEG-TS keeps every RPU and loses its configuration record:

```
ffmpeg -i mov/dovi-p81.mp4 -c copy out.ts
ffprobe -show_entries stream_side_data_list out.ts   # no DOVI configuration 
record
```

A player that routes Dolby Vision from the record then plays a Profile 8.1 copy 
as HDR10, Profile 8.4 as HLG, and Profile 5 as HDR10 with IPT-PQ-c2 decoded as 
YCbCr. We hit this with live TV recordings, which are a stream copy from a 
broadcast TS into a TS file.

With this patch, a video stream whose record has `bl_present_flag` set gets the 
`DOVI` registration descriptor and the 0xb0 descriptor built from the record, 
in the layout `mpegts.c` already parses (version, profile, level, the three 
present flags, compatibility id, metadata compression, two reserved bits set).

Not covered: a dual-PID Profile 7 enhancement layer (`bl_present_flag` = 0). 
Its descriptor needs the base layer's PID, and the spec signals that PID with 
stream_type 0x06. Both could come from the Dolby Vision stream group the 
demuxer now creates, the way the LCEVC groups are handled in this muxer, but I 
kept that out of this patch. Such a stream is written as before.

Tests: `fate-mpegts-dovi-write-config5` and `-config8` remux the existing 
`mov/dovi-p5.mp4` and `mov/dovi-p81.mp4` into MPEG-TS and read the record back. 
Both fail without the muxer change. `fate-mpegts`, `fate-lavf`, 
`fate-matroska`, `fate-hevc`, `fate-mov` and `fate-source` pass (538 tests, 
macOS arm64).

The muxer change is based on a patch Zbigniew Szepczyński (MistyKuu) wrote for 
FFmpegBuild, https://github.com/superuser404notfound/FFmpegBuild/pull/11, where 
it was also checked end to end on Profile 8.1, 8.4 and 5 recordings.



From 309cd6afc22db66f4bf255f26808963f957d550b Mon Sep 17 00:00:00 2001
From: Vincent Herbst <[email protected]>
Date: Mon, 16 Nov 2026 11:03:29 +0100
Subject: [PATCH] avformat/mpegtsenc: write the Dolby Vision descriptors
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The demuxer reads the DOVI video stream descriptor into the stream's
AV_PKT_DATA_DOVI_CONF side data, but the muxer never wrote it. A Dolby
Vision HEVC stream copied into MPEG-TS therefore kept its RPU NAL units
and lost its configuration record, and a player that detects Dolby
Vision from the record treats a Profile 8.1 copy as HDR10, a Profile
8.4 copy as HLG and a Profile 5 copy as HDR10, decoding IPT-PQ-c2 as
YCbCr.

For a video stream whose record has bl_present_flag set, write the
"DOVI" registration descriptor and the DOVI video stream descriptor
from "Dolby Vision streams within the MPEG-2 transport stream format",
in the layout the demuxer parses. A stream without a base layer is
left as it was: its descriptor also carries the PID of the base layer,
and a dual-PID enhancement layer is signalled with stream_type 0x06,
neither of which this change covers. A stream without a record gets
nothing new.

Based on a patch by Zbigniew Szepczyński for FFmpegBuild.

Signed-off-by: Vincent Herbst <[email protected]>
---
 libavformat/mpegtsenc.c                  | 38 ++++++++++++++++++++++++
 tests/fate/mpegts.mak                    | 11 +++++++
 tests/ref/fate/mpegts-dovi-write-config5 | 32 ++++++++++++++++++++
 tests/ref/fate/mpegts-dovi-write-config8 | 32 ++++++++++++++++++++
 4 files changed, 113 insertions(+)
 create mode 100644 tests/ref/fate/mpegts-dovi-write-config5
 create mode 100644 tests/ref/fate/mpegts-dovi-write-config8

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 6682abfac4d..26ed684ceee 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -23,6 +23,7 @@
 #include "libavutil/bswap.h"
 #include "libavutil/crc.h"
 #include "libavutil/dict.h"
+#include "libavutil/dovi_meta.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/mem.h"
@@ -360,6 +361,42 @@ static void put_registration_descriptor(uint8_t **q_ptr, 
uint32_t tag)
     *q_ptr = q;
 }
 
+/* Dolby Vision streams within the MPEG-2 transport stream format: the 'DOVI'
+ * registration descriptor followed by the DOVI video stream descriptor, built
+ * from the stream's configuration record. A stream without a base layer would
+ * also need the PID of its base layer, which the record does not carry, so
+ * only a stream with a base layer gets them. */
+static void put_dovi_descriptor(uint8_t **q_ptr, const AVCodecParameters *par)
+{
+    const AVPacketSideData *sd = av_packet_side_data_get(par->coded_side_data,
+                                                         
par->nb_coded_side_data,
+                                                         
AV_PKT_DATA_DOVI_CONF);
+    const AVDOVIDecoderConfigurationRecord *dovi;
+    uint8_t *q;
+
+    if (!sd)
+        return;
+    dovi = (const AVDOVIDecoderConfigurationRecord *)sd->data;
+    if (!dovi->bl_present_flag)
+        return;
+
+    put_registration_descriptor(q_ptr, MKTAG('D', 'O', 'V', 'I'));
+    q = *q_ptr;
+    *q++ = DOVI_VIDEO_STREAM_DESCRIPTOR;
+    *q++ = 5;
+    *q++ = dovi->dv_version_major;
+    *q++ = dovi->dv_version_minor;
+    put16(&q, (dovi->dv_profile & 0x7f) << 9 |
+              (dovi->dv_level   & 0x3f) << 3 |
+              !!dovi->rpu_present_flag << 2 |
+              !!dovi->el_present_flag  << 1 |
+              1 /* bl_present_flag */);
+    *q++ = (dovi->dv_bl_signal_compatibility_id & 0x0f) << 4 |
+           (dovi->dv_md_compression & 0x03) << 2 |
+           0x03 /* reserved */;
+    *q_ptr = q;
+}
+
 static int get_dvb_stream_type(AVFormatContext *s, AVStream *st)
 {
     MpegTSWrite *ts = s->priv_data;
@@ -839,6 +876,7 @@ static int mpegts_write_pmt(AVFormatContext *s, 
MpegTSService *service)
                        stream_type == STREAM_TYPE_VIDEO_AVS3) {
                 put_registration_descriptor(&q, MKTAG('A', 'V', 'S', 'V'));
             }
+            put_dovi_descriptor(&q, st->codecpar);
             if (ts_st->nb_stream_groups) {
                 if (stream_type == STREAM_TYPE_VIDEO_LCEVC) {
                     LCEVCDecoderConfigurationRecord lvcc = { 0 };
diff --git a/tests/fate/mpegts.mak b/tests/fate/mpegts.mak
index d9136c25c7c..6b7bcf6bb8e 100644
--- a/tests/fate/mpegts.mak
+++ b/tests/fate/mpegts.mak
@@ -80,6 +80,17 @@ fate-mpegts-dvbsub-multi-service: CMD = \
   ffmpeg -i pipe: -map 0:s -c copy -disposition:s:0 hearing_impaired -bitexact 
-f mpegts - | \
   probe -count_packets -show_entries stream_disposition=hearing_impaired -of 
default=nw=1:nk=1 pipe:
 
+# The Dolby Vision configuration record must survive a stream copy into
+# MPEG-TS: the muxer writes the DOVI video stream descriptor and the demuxer
+# reads it back.
+FATE_MPEGTS_FFMPEG_FFPROBE-$(call REMUX, MPEGTS, MOV_DEMUXER HEVC_DECODER) += 
fate-mpegts-dovi-write-config5
+fate-mpegts-dovi-write-config5: CMD = transcode mov 
$(TARGET_SAMPLES)/mov/dovi-p5.mp4 mpegts \
+  "-map 0 -c copy" "-map 0 -c copy" "-show_entries stream_side_data_list"
+
+FATE_MPEGTS_FFMPEG_FFPROBE-$(call REMUX, MPEGTS, MOV_DEMUXER HEVC_DECODER) += 
fate-mpegts-dovi-write-config8
+fate-mpegts-dovi-write-config8: CMD = transcode mov 
$(TARGET_SAMPLES)/mov/dovi-p81.mp4 mpegts \
+  "-map 0 -c copy" "-map 0 -c copy" "-show_entries stream_side_data_list"
+
 FATE_SAMPLES_FFPROBE += $(FATE_MPEGTS_PROBE-yes)
 FATE_SAMPLES_FFMPEG_FFPROBE += $(FATE_MPEGTS_FFMPEG_FFPROBE-yes)
 
diff --git a/tests/ref/fate/mpegts-dovi-write-config5 
b/tests/ref/fate/mpegts-dovi-write-config5
new file mode 100644
index 00000000000..95987930a11
--- /dev/null
+++ b/tests/ref/fate/mpegts-dovi-write-config5
@@ -0,0 +1,32 @@
+c959d37405cea782de31c5f70a575582 
*tests/data/fate/mpegts-dovi-write-config5.mpegts
+6016 tests/data/fate/mpegts-dovi-write-config5.mpegts
+#extradata 0:       89, 0x6be10f3c
+#tb 0: 1/90000
+#media_type 0: video
+#codec_id 0: hevc
+#dimensions 0: 1920x1080
+#sar 0: 0/1
+0,      -7500,          0,     3750,      819, 0x98a662c1, S=1, MPEGTS Stream 
ID,        1, 0x00e000e0
+0,      -3750,      15000,     3750,      267, 0x6d0348cc, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,          0,       7500,     3750,      270, 0x55e44ac3, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,       3750,       3750,     3750,      271, 0x53d348a6, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,       7500,      11250,     3750,      270, 0x14cd4b83, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      11250,      30000,     3750,      268, 0x194a4871, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      15000,      22500,     3750,      272, 0x0d334a72, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      18750,      18750,     3750,      272, 0xdc114a2b, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      22500,      26250,     3750,      271, 0xbe3a4a24, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      26250,      33750,     3750,      268, 0x538148ac, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+[STREAM]
+[SIDE_DATA]
+side_data_type=DOVI configuration record
+dv_version_major=1
+dv_version_minor=0
+dv_profile=5
+dv_level=4
+rpu_present_flag=1
+el_present_flag=0
+bl_present_flag=1
+dv_bl_signal_compatibility_id=0
+dv_md_compression=none
+[/SIDE_DATA]
+[/STREAM]
diff --git a/tests/ref/fate/mpegts-dovi-write-config8 
b/tests/ref/fate/mpegts-dovi-write-config8
new file mode 100644
index 00000000000..927de0524c4
--- /dev/null
+++ b/tests/ref/fate/mpegts-dovi-write-config8
@@ -0,0 +1,32 @@
+11f419e1d6fb65a7d337080a9f50a58b 
*tests/data/fate/mpegts-dovi-write-config8.mpegts
+6016 tests/data/fate/mpegts-dovi-write-config8.mpegts
+#extradata 0:       92, 0x96370f9e
+#tb 0: 1/90000
+#media_type 0: video
+#codec_id 0: hevc
+#dimensions 0: 1920x1080
+#sar 0: 0/1
+0,      -7500,          0,     3750,      839, 0x80776590, S=1, MPEGTS Stream 
ID,        1, 0x00e000e0
+0,      -3750,      15000,     3750,      236, 0xacbd3cec, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,          0,       7500,     3750,      240, 0x41b33ec5, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,       3750,       3750,     3750,      240, 0xf5243d2f, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,       7500,      11250,     3750,      240, 0xea0d3f85, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      11250,      30000,     3750,      237, 0xec7b3d30, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      15000,      22500,     3750,      241, 0xaf433f43, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      18750,      18750,     3750,      241, 0x3e753ea2, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      22500,      26250,     3750,      240, 0xc98e3f64, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+0,      26250,      33750,     3750,      237, 0x1a7c3d65, F=0x0, S=1, MPEGTS 
Stream ID,        1, 0x00e000e0
+[STREAM]
+[SIDE_DATA]
+side_data_type=DOVI configuration record
+dv_version_major=1
+dv_version_minor=0
+dv_profile=8
+dv_level=4
+rpu_present_flag=1
+el_present_flag=0
+bl_present_flag=1
+dv_bl_signal_compatibility_id=1
+dv_md_compression=none
+[/SIDE_DATA]
+[/STREAM]
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to