QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstH26xParameterSetGate.cc
Go to the documentation of this file.
1// The H.264/H.265 part of GStreamer::SourceFactory, declared in GstSourceFactory.h: the setup of the depayloaders and
2// parsers, and the gate that holds a depayloader's output until its parameter sets have arrived.
3
4#include <gst/gst.h>
5
6#include "GStreamerHelpers.h"
7#include "GstScoped.h"
8#include "GstSourceFactory.h"
9
11
12namespace {
13
14// NAL unit types (ITU-T H.264 and H.265, table 7-1). Slices are the types up to the last one named here.
15constexpr guint kH264NalSliceFirst = 1;
16constexpr guint kH264NalSliceIdr = 5;
17constexpr guint kH264NalSps = 7;
18constexpr guint kH264NalPps = 8;
19constexpr guint kH265NalSliceLast = 31;
20constexpr guint kH265NalVps = 32;
21constexpr guint kH265NalSps = 33;
22constexpr guint kH265NalPps = 34;
23
24bool isH26xRtpDepayloader(const char* factoryName)
25{
26 return (g_strcmp0(factoryName, "rtph264depay") == 0) || (g_strcmp0(factoryName, "rtph265depay") == 0);
27}
28
29// Joining a stream mid-GOP, slices before the next parameter sets can't be decoded. GStreamer 1.28's h264parse warns
30// for every such slice and, when converting byte-stream to avc for the decoder, fails the pipeline with "No caps set"
31// if it ever pushes an access unit before it has both SPS and PPS, e.g. an SPS whose PPS a jitter buffer dropped.
32// The depayloader's DELTA_UNIT flag isn't enough either: a sliced encoder's IDR can be joined after its SPS/PPS.
33struct NalSummary
34{
35 bool vps = false;
36 bool sps = false;
37 bool pps = false;
38 bool slice = false;
39};
40
41NalSummary summarizeNals(GstBuffer* buffer, bool h265)
42{
43 NalSummary summary;
44 GstMapInfo map;
45 if (!gst_buffer_map(buffer, &map, GST_MAP_READ)) {
46 return summary;
47 }
48 const guint8* const data = map.data;
49 for (gsize i = 0; (i + 3) < map.size; ++i) {
50 if ((data[i] != 0) || (data[i + 1] != 0) || (data[i + 2] != 1)) {
51 continue;
52 }
53 const guint8 header = data[i + 3];
54 if (h265) {
55 const guint nalType = (header >> 1) & 0x3FU;
56 summary.vps |= (nalType == kH265NalVps);
57 summary.sps |= (nalType == kH265NalSps);
58 summary.pps |= (nalType == kH265NalPps);
59 summary.slice |= (nalType <= kH265NalSliceLast);
60 } else {
61 const guint nalType = header & 0x1FU;
62 summary.sps |= (nalType == kH264NalSps);
63 summary.pps |= (nalType == kH264NalPps);
64 summary.slice |= ((nalType >= kH264NalSliceFirst) && (nalType <= kH264NalSliceIdr));
65 }
66 i += 2;
67 }
68 gst_buffer_unmap(buffer, &map);
69 return summary;
70}
71
72bool outputsByteStream(GstPad* pad)
73{
74 const GstCapsPtr caps = adoptCaps(gst_pad_get_current_caps(pad));
75 const GstStructure* structure =
76 (caps && !gst_caps_is_empty(caps.get())) ? gst_caps_get_structure(caps.get(), 0) : nullptr;
77 return structure && (g_strcmp0(gst_structure_get_string(structure, "stream-format"), "byte-stream") == 0);
78}
79
81struct ParameterSetGate
82{
83 explicit ParameterSetGate(bool isH265)
84 : h265(isH265)
85 {}
86
87 ~ParameterSetGate()
88 {
89 gst_clear_buffer(&vps);
90 gst_clear_buffer(&sps);
91 gst_clear_buffer(&pps);
92 }
93
94 ParameterSetGate(const ParameterSetGate&) = delete;
95 ParameterSetGate& operator=(const ParameterSetGate&) = delete;
96
97 const bool h265;
98 GstBuffer* vps = nullptr;
99 GstBuffer* sps = nullptr;
100 GstBuffer* pps = nullptr;
101};
102
104GstBuffer* prependMemories(GstBuffer* buffer, GstBuffer* prefix)
105{
106 buffer = gst_buffer_make_writable(buffer);
107 for (guint i = gst_buffer_n_memory(prefix); i > 0; --i) {
108 gst_buffer_prepend_memory(buffer, gst_buffer_get_memory(prefix, i - 1));
109 }
110 return buffer;
111}
112
113GstPadProbeReturn holdUntilParameterSets(GstPad* pad, GstPadProbeInfo* info, gpointer data)
114{
115 GstBuffer* buffer = GST_PAD_PROBE_INFO_BUFFER(info);
116 if (!buffer) {
117 return GST_PAD_PROBE_OK;
118 }
119 // Length-prefixed avc/hvc1 output carries its parameter sets in the caps' codec_data.
120 if (!outputsByteStream(pad)) {
121 return GST_PAD_PROBE_REMOVE;
122 }
123
124 auto* gate = static_cast<ParameterSetGate*>(data);
125 const NalSummary nals = summarizeNals(buffer, gate->h265);
126 if (!nals.slice) {
127 if (nals.vps) {
128 gst_buffer_replace(&gate->vps, buffer);
129 }
130 if (nals.sps) {
131 gst_buffer_replace(&gate->sps, buffer);
132 }
133 if (nals.pps) {
134 gst_buffer_replace(&gate->pps, buffer);
135 }
136 return GST_PAD_PROBE_DROP;
137 }
138
139 const bool complete = (nals.sps || gate->sps) && (nals.pps || gate->pps) && (!gate->h265 || nals.vps || gate->vps);
140 if (!complete) {
141 return GST_PAD_PROBE_DROP;
142 }
143
144 // Prepend, in VPS/SPS/PPS order, the stored sets this buffer lacks; one buffer may hold several of them.
145 GstBuffer* const stored[] = {(gate->h265 && !nals.vps) ? gate->vps : nullptr, nals.sps ? nullptr : gate->sps,
146 nals.pps ? nullptr : gate->pps};
147 for (int i = 2; i >= 0; --i) {
148 const bool duplicate = (i > 0 && stored[i] == stored[i - 1]) || (i > 1 && stored[i] == stored[i - 2]);
149 if (stored[i] && !duplicate) {
150 buffer = prependMemories(buffer, stored[i]);
151 }
152 }
153 GST_PAD_PROBE_INFO_DATA(info) = buffer;
154 return GST_PAD_PROBE_REMOVE;
155}
156
157} // namespace
158
160{
161 const char* factoryName = factoryNameOf(GST_OBJECT_CAST(element));
162 if ((g_strcmp0(factoryName, "h264parse") == 0) || (g_strcmp0(factoryName, "h265parse") == 0)) {
163 // Recording can begin long after RTSP setup. Repeat codec parameter sets at every IDR so
164 // the late-opened recording branch can construct MP4/MOV codec_data without relying on
165 // the camera to resend its startup-only VPS/SPS/PPS or SPS/PPS.
166 g_object_set(element, "config-interval", -1, nullptr);
167 }
168}
169
170void configureH26xRtpDepayloader(GstElement* element, bool requestKeyframes)
171{
172 const char* factoryName = factoryNameOf(GST_OBJECT_CAST(element));
173 if (!isH26xRtpDepayloader(factoryName)) {
174 return;
175 }
176 // After packet loss, ask the camera for a keyframe rather than wait out the GOP: rtph264depay can since GStreamer
177 // 1.20, rtph265depay since 1.26.
178 if (requestKeyframes && g_object_class_find_property(G_OBJECT_GET_CLASS(element), "request-keyframe")) {
179 g_object_set(element, "request-keyframe", TRUE, nullptr);
180 }
181 if (const GstPadPtr srcPad = adoptPad(gst_element_get_static_pad(element, "src"))) {
182 gateOnParameterSets(srcPad.get(), g_strcmp0(factoryName, "rtph265depay") == 0);
183 }
184}
185
186void gateOnParameterSets(GstPad* depayloaderSrc, bool h265)
187{
188 (void) gst_pad_add_probe(depayloaderSrc, GST_PAD_PROBE_TYPE_BUFFER, holdUntilParameterSets,
189 new ParameterSetGate(h265),
190 [](gpointer gate) { delete static_cast<ParameterSetGate*>(gate); });
191}
192
193} // namespace GStreamer::SourceFactory
struct _GstElement GstElement
The RTSP source GStreamer::SourceFactory::create() builds, with QGC's transport, latency and retransm...
void gateOnParameterSets(GstPad *depayloaderSrc, bool h265)
void configureH26xParser(GstElement *element)
void configureH26xRtpDepayloader(GstElement *element, bool requestKeyframes)
std::unique_ptr< GstCaps, GstCapsDeleter > GstCapsPtr
Definition GstScoped.h:66
const char * factoryNameOf(GstObject *object)
The name of the factory that made object ("rtph264depay"); null when it is no element or has no facto...
GstCapsPtr adoptCaps(GstCaps *caps) noexcept
Definition GstScoped.h:109
std::unique_ptr< GstPad, GstObjectDeleter > GstPadPtr
Definition GstScoped.h:61
GstPadPtr adoptPad(GstPad *pad) noexcept
Definition GstScoped.h:84