1 /* gstrtpvp9depay.c - Source for GstRtpVP9Depay
2 * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
3 * Copyright (C) 2011 Collabora Ltd.
4 * Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
5 * Copyright (C) 2015 Stian Selnes <stian@pexip.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "gstrtpvp9depay.h"
27 #include "gstrtputils.h"
29 #include <gst/video/video.h>
33 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_depay_debug);
34 #define GST_CAT_DEFAULT gst_rtp_vp9_depay_debug
36 static void gst_rtp_vp9_depay_dispose (GObject * object);
37 static GstBuffer *gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depayload,
39 static GstStateChangeReturn gst_rtp_vp9_depay_change_state (GstElement *
40 element, GstStateChange transition);
41 static gboolean gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay,
44 G_DEFINE_TYPE (GstRtpVP9Depay, gst_rtp_vp9_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
46 static GstStaticPadTemplate gst_rtp_vp9_depay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
50 GST_STATIC_CAPS ("video/x-vp9"));
52 static GstStaticPadTemplate gst_rtp_vp9_depay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS ("application/x-rtp, "
57 "clock-rate = (int) 90000,"
58 "media = (string) \"video\","
59 "encoding-name = (string) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
62 gst_rtp_vp9_depay_init (GstRtpVP9Depay * self)
64 self->adapter = gst_adapter_new ();
65 self->started = FALSE;
69 gst_rtp_vp9_depay_class_init (GstRtpVP9DepayClass * gst_rtp_vp9_depay_class)
71 GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp9_depay_class);
72 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_depay_class);
73 GstRTPBaseDepayloadClass *depay_class =
74 (GstRTPBaseDepayloadClass *) (gst_rtp_vp9_depay_class);
77 gst_element_class_add_static_pad_template (element_class,
78 &gst_rtp_vp9_depay_sink_template);
79 gst_element_class_add_static_pad_template (element_class,
80 &gst_rtp_vp9_depay_src_template);
82 gst_element_class_set_static_metadata (element_class, "RTP VP9 depayloader",
83 "Codec/Depayloader/Network/RTP",
84 "Extracts VP9 video from RTP packets)", "Stian Selnes <stian@pexip.com>");
86 object_class->dispose = gst_rtp_vp9_depay_dispose;
88 element_class->change_state = gst_rtp_vp9_depay_change_state;
90 depay_class->process_rtp_packet = gst_rtp_vp9_depay_process;
91 depay_class->handle_event = gst_rtp_vp9_depay_handle_event;
93 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_depay_debug, "rtpvp9depay", 0,
94 "VP9 Video RTP Depayloader");
98 gst_rtp_vp9_depay_dispose (GObject * object)
100 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (object);
102 if (self->adapter != NULL)
103 g_object_unref (self->adapter);
104 self->adapter = NULL;
106 /* release any references held by the object here */
108 if (G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose)
109 G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose (object);
113 gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
115 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
120 gint spatial_layer = 0;
121 gboolean i_bit, p_bit, l_bit, f_bit, b_bit, e_bit, v_bit;
123 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
124 GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
125 gst_adapter_clear (self->adapter);
126 self->started = FALSE;
129 size = gst_rtp_buffer_get_payload_len (rtp);
131 /* Mandatory with at least one header and one vp9 byte */
132 if (G_UNLIKELY (size < hdrsize + 1))
135 data = gst_rtp_buffer_get_payload (rtp);
136 i_bit = (data[0] & 0x80) != 0;
137 p_bit = (data[0] & 0x40) != 0;
138 l_bit = (data[0] & 0x20) != 0;
139 f_bit = (data[0] & 0x10) != 0;
140 b_bit = (data[0] & 0x08) != 0;
141 e_bit = (data[0] & 0x04) != 0;
142 v_bit = (data[0] & 0x02) != 0;
144 if (G_UNLIKELY (!self->started)) {
145 /* Check if this is the start of a VP9 layer frame, otherwise bail */
149 self->started = TRUE;
152 GST_TRACE_OBJECT (self, "IPLFBEV : %d%d%d%d%d%d%d", i_bit, p_bit, l_bit,
153 f_bit, b_bit, e_bit, v_bit);
155 /* Check I optional header Picture ID */
158 if (G_UNLIKELY (size < hdrsize + 1))
160 /* Check M for 15 bits PictureID */
161 if ((data[1] & 0x80) != 0) {
163 if (G_UNLIKELY (size < hdrsize + 1))
168 /* flexible-mode not implemented */
171 /* Check L optional header layer indices */
174 /* Check TL0PICIDX temporal layer zero index (non-flexible mode) */
179 /* Check V optional Scalability Structure */
181 guint n_s, y_bit, g_bit;
182 guint8 *ss = &data[hdrsize];
185 if (G_UNLIKELY (size < hdrsize + sssize + 1))
188 n_s = (ss[0] & 0xe0) >> 5;
189 y_bit = (ss[0] & 0x10) != 0;
190 g_bit = (ss[0] & 0x08) != 0;
192 GST_TRACE_OBJECT (self, "SS header: N_S=%u, Y=%u, G=%u", n_s, y_bit, g_bit);
194 sssize += y_bit ? (n_s + 1) * 4 : 0;
195 if (G_UNLIKELY (size < hdrsize + sssize + 1))
200 for (i = 0; i <= n_s; i++) {
201 /* For now, simply use the last layer specified for width and height */
202 self->ss_width = ss[1 + i * 4] * 256 + ss[2 + i * 4];
203 self->ss_height = ss[3 + i * 4] * 256 + ss[4 + i * 4];
204 GST_TRACE_OBJECT (self, "N_S[%d]: WIDTH=%u, HEIGHT=%u", i,
205 self->ss_width, self->ss_height);
211 guint n_g = ss[sssize];
213 if (G_UNLIKELY (size < hdrsize + sssize + 1))
215 for (i = 0; i < n_g; i++) {
216 guint t = (ss[sssize] & 0xe0) >> 5;
217 guint u = (ss[sssize] & 0x10) >> 4;
218 guint r = (ss[sssize] & 0x0c) >> 2;
219 GST_TRACE_OBJECT (self, "N_G[%u]: 0x%02x -> T=%u, U=%u, R=%u", i,
220 ss[sssize], t, u, r);
221 for (j = 0; j < r; j++)
222 GST_TRACE_OBJECT (self, " R[%u]: P_DIFF=%u", j, ss[sssize + 1 + j]);
224 if (G_UNLIKELY (size < hdrsize + sssize + 1))
231 GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
233 if (G_UNLIKELY (hdrsize >= size))
236 payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
239 gst_buffer_map (payload, &map, GST_MAP_READ);
240 GST_MEMDUMP_OBJECT (self, "vp9 payload", map.data, 16);
241 gst_buffer_unmap (payload, &map);
243 gst_adapter_push (self->adapter, payload);
245 /* Marker indicates that it was the last rtp packet for this frame */
246 if (gst_rtp_buffer_get_marker (rtp)) {
248 gboolean key_frame_first_layer = !p_bit && spatial_layer == 0;
251 if (gst_adapter_available (self->adapter) < 10)
254 out = gst_adapter_take_buffer (self->adapter,
255 gst_adapter_available (self->adapter));
257 self->started = FALSE;
260 out = gst_buffer_make_writable (out);
261 /* Filter away all metas that are not sensible to copy */
262 gst_rtp_drop_meta (GST_ELEMENT_CAST (self), out,
263 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
264 if (!key_frame_first_layer) {
265 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
267 if (!self->caps_sent) {
268 gst_buffer_unref (out);
270 GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
271 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
272 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
276 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
278 if (self->last_width != self->ss_width ||
279 self->last_height != self->ss_height) {
282 /* Width and height are optional in the RTP header. Consider to parse
283 * the frame header in addition if missing from RTP header */
284 if (self->ss_width != 0 && self->ss_height != 0) {
285 srccaps = gst_caps_new_simple ("video/x-vp9",
286 "framerate", GST_TYPE_FRACTION, 0, 1,
287 "width", G_TYPE_INT, self->ss_width,
288 "height", G_TYPE_INT, self->ss_height, NULL);
290 srccaps = gst_caps_new_simple ("video/x-vp9",
291 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
294 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
295 gst_caps_unref (srccaps);
297 self->caps_sent = TRUE;
298 self->last_width = self->ss_width;
299 self->last_height = self->ss_height;
312 GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
313 gst_adapter_clear (self->adapter);
314 self->started = FALSE;
319 static GstStateChangeReturn
320 gst_rtp_vp9_depay_change_state (GstElement * element, GstStateChange transition)
322 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (element);
324 switch (transition) {
325 case GST_STATE_CHANGE_READY_TO_PAUSED:
326 self->last_width = -1;
327 self->last_height = -1;
328 self->caps_sent = FALSE;
335 GST_ELEMENT_CLASS (gst_rtp_vp9_depay_parent_class)->change_state (element,
340 gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
342 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
344 switch (GST_EVENT_TYPE (event)) {
345 case GST_EVENT_FLUSH_STOP:
346 self->last_width = -1;
347 self->last_height = -1;
354 GST_RTP_BASE_DEPAYLOAD_CLASS
355 (gst_rtp_vp9_depay_parent_class)->handle_event (depay, event);
359 gst_rtp_vp9_depay_plugin_init (GstPlugin * plugin)
361 return gst_element_register (plugin, "rtpvp9depay",
362 GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_DEPAY);