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 "gstrtpelements.h"
27 #include "gstrtpvp9depay.h"
28 #include "gstrtputils.h"
30 #include <gst/video/video.h>
34 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_depay_debug);
35 #define GST_CAT_DEFAULT gst_rtp_vp9_depay_debug
37 static void gst_rtp_vp9_depay_dispose (GObject * object);
38 static GstBuffer *gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depayload,
40 static GstStateChangeReturn gst_rtp_vp9_depay_change_state (GstElement *
41 element, GstStateChange transition);
42 static gboolean gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay,
44 static gboolean gst_rtp_vp9_depay_packet_lost (GstRTPBaseDepayload * depay,
47 G_DEFINE_TYPE (GstRtpVP9Depay, gst_rtp_vp9_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
48 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpvp9depay, "rtpvp9depay",
49 GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_DEPAY, rtp_element_init (plugin));
51 static GstStaticPadTemplate gst_rtp_vp9_depay_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("video/x-vp9"));
57 static GstStaticPadTemplate gst_rtp_vp9_depay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("application/x-rtp, "
62 "clock-rate = (int) 90000,"
63 "media = (string) \"video\","
64 "encoding-name = (string) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
66 #define PICTURE_ID_NONE (UINT_MAX)
67 #define IS_PICTURE_ID_15BITS(pid) (((guint)(pid) & 0x8000) != 0)
70 gst_rtp_vp9_depay_init (GstRtpVP9Depay * self)
72 self->adapter = gst_adapter_new ();
73 self->started = FALSE;
74 self->inter_picture = FALSE;
78 gst_rtp_vp9_depay_class_init (GstRtpVP9DepayClass * gst_rtp_vp9_depay_class)
80 GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp9_depay_class);
81 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_depay_class);
82 GstRTPBaseDepayloadClass *depay_class =
83 (GstRTPBaseDepayloadClass *) (gst_rtp_vp9_depay_class);
86 gst_element_class_add_static_pad_template (element_class,
87 &gst_rtp_vp9_depay_sink_template);
88 gst_element_class_add_static_pad_template (element_class,
89 &gst_rtp_vp9_depay_src_template);
91 gst_element_class_set_static_metadata (element_class, "RTP VP9 depayloader",
92 "Codec/Depayloader/Network/RTP",
93 "Extracts VP9 video from RTP packets)", "Stian Selnes <stian@pexip.com>");
95 object_class->dispose = gst_rtp_vp9_depay_dispose;
97 element_class->change_state = gst_rtp_vp9_depay_change_state;
99 depay_class->process_rtp_packet = gst_rtp_vp9_depay_process;
100 depay_class->handle_event = gst_rtp_vp9_depay_handle_event;
101 depay_class->packet_lost = gst_rtp_vp9_depay_packet_lost;
103 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_depay_debug, "rtpvp9depay", 0,
104 "VP9 Video RTP Depayloader");
108 gst_rtp_vp9_depay_dispose (GObject * object)
110 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (object);
112 if (self->adapter != NULL)
113 g_object_unref (self->adapter);
114 self->adapter = NULL;
116 /* release any references held by the object here */
118 if (G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose)
119 G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose (object);
123 picture_id_compare (guint16 id0, guint16 id1)
125 guint shift = 16 - (IS_PICTURE_ID_15BITS (id1) ? 15 : 7);
128 return ((gint16) (id1 - id0)) >> shift;
132 send_last_lost_event (GstRtpVP9Depay * self)
134 if (self->last_lost_event) {
135 GST_DEBUG_OBJECT (self,
136 "Sending the last stopped lost event: %" GST_PTR_FORMAT,
137 self->last_lost_event);
138 GST_RTP_BASE_DEPAYLOAD_CLASS (gst_rtp_vp9_depay_parent_class)
139 ->packet_lost (GST_RTP_BASE_DEPAYLOAD_CAST (self),
140 self->last_lost_event);
141 gst_event_replace (&self->last_lost_event, NULL);
146 send_last_lost_event_if_needed (GstRtpVP9Depay * self, guint new_picture_id)
148 if (self->last_picture_id == PICTURE_ID_NONE ||
149 self->last_picture_id == new_picture_id)
152 if (self->last_lost_event) {
153 gboolean send_lost_event = FALSE;
154 if (new_picture_id == PICTURE_ID_NONE) {
155 GST_DEBUG_OBJECT (self, "Dropping the last stopped lost event "
156 "(picture id does not exist): %" GST_PTR_FORMAT,
157 self->last_lost_event);
158 } else if (IS_PICTURE_ID_15BITS (self->last_picture_id) &&
159 !IS_PICTURE_ID_15BITS (new_picture_id)) {
160 GST_DEBUG_OBJECT (self, "Dropping the last stopped lost event "
161 "(picture id has less bits than before): %" GST_PTR_FORMAT,
162 self->last_lost_event);
163 } else if (picture_id_compare (self->last_picture_id, new_picture_id) != 1) {
164 GstStructure *s = gst_event_writable_structure (self->last_lost_event);
166 GST_DEBUG_OBJECT (self, "Sending the last stopped lost event "
167 "(gap in picture id %u %u): %" GST_PTR_FORMAT,
168 self->last_picture_id, new_picture_id, self->last_lost_event);
169 send_lost_event = TRUE;
170 /* Prevent rtpbasedepayload from dropping the event now
171 * that we have made sure the lost packet was not FEC */
172 gst_structure_remove_field (s, "might-have-been-fec");
175 GST_RTP_BASE_DEPAYLOAD_CLASS (gst_rtp_vp9_depay_parent_class)
176 ->packet_lost (GST_RTP_BASE_DEPAYLOAD_CAST (self),
177 self->last_lost_event);
179 gst_event_replace (&self->last_lost_event, NULL);
184 gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
186 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
191 gint spatial_layer = 0;
192 guint picture_id = PICTURE_ID_NONE;
193 gboolean i_bit, p_bit, l_bit, f_bit, b_bit, e_bit, v_bit, d_bit = 0;
194 gboolean is_start_of_picture;
196 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
197 GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
198 gst_adapter_clear (self->adapter);
199 self->started = FALSE;
202 size = gst_rtp_buffer_get_payload_len (rtp);
204 /* Mandatory with at least one header and one vp9 byte */
205 if (G_UNLIKELY (size < hdrsize + 1))
208 data = gst_rtp_buffer_get_payload (rtp);
209 i_bit = (data[0] & 0x80) != 0;
210 p_bit = (data[0] & 0x40) != 0;
211 l_bit = (data[0] & 0x20) != 0;
212 f_bit = (data[0] & 0x10) != 0;
213 b_bit = (data[0] & 0x08) != 0;
214 e_bit = (data[0] & 0x04) != 0;
215 v_bit = (data[0] & 0x02) != 0;
217 GST_TRACE_OBJECT (self, "IPLFBEV : %d%d%d%d%d%d%d", i_bit, p_bit, l_bit,
218 f_bit, b_bit, e_bit, v_bit);
220 /* Check I optional header Picture ID */
223 if (G_UNLIKELY (size < hdrsize + 1))
225 picture_id = data[1];
226 /* Check M for 15 bits PictureID */
227 if ((data[1] & 0x80) != 0) {
229 if (G_UNLIKELY (size < hdrsize + 1))
231 picture_id = (picture_id << 8) | data[2];
235 /* Check L optional header layer indices */
237 spatial_layer = (data[hdrsize] >> 1) & 0x07;
238 d_bit = (data[hdrsize] >> 0) & 0x01;
239 GST_TRACE_OBJECT (self, "TID=%d, U=%d, SID=%d, D=%d",
240 (data[hdrsize] >> 5) & 0x07, (data[hdrsize] >> 4) & 0x01,
241 (data[hdrsize] >> 1) & 0x07, (data[hdrsize] >> 0) & 0x01);
243 if (spatial_layer == 0 && d_bit != 0) {
244 /* Invalid according to draft-ietf-payload-vp9-06, but firefox 61 and
245 * chrome 66 sends enchanment layers with SID=0, so let's not drop the
247 GST_LOG_OBJECT (self, "Invalid inter-layer dependency for base layer");
251 /* Check TL0PICIDX temporal layer zero index (non-flexible mode) */
256 if (p_bit && f_bit) {
259 /* At least one P_DIFF|N, up to three times */
260 for (i = 0; i < 3; i++) {
263 if (G_UNLIKELY (size < hdrsize + 1))
266 p_diff = data[hdrsize] >> 1;
267 n_bit = data[hdrsize] & 0x1;
268 GST_TRACE_OBJECT (self, "P_DIFF[%d]=%d", i, p_diff);
276 /* Check V optional Scalability Structure */
278 guint n_s, y_bit, g_bit;
279 guint8 *ss = &data[hdrsize];
282 if (G_UNLIKELY (size < hdrsize + sssize + 1))
285 n_s = (ss[0] & 0xe0) >> 5;
286 y_bit = (ss[0] & 0x10) != 0;
287 g_bit = (ss[0] & 0x08) != 0;
289 GST_TRACE_OBJECT (self, "SS header: N_S=%u, Y=%u, G=%u", n_s, y_bit, g_bit);
291 sssize += y_bit ? (n_s + 1) * 4 : 0;
292 if (G_UNLIKELY (size < hdrsize + sssize + 1))
297 for (i = 0; i <= n_s; i++) {
298 /* For now, simply use the last layer specified for width and height */
299 self->ss_width = ss[1 + i * 4] * 256 + ss[2 + i * 4];
300 self->ss_height = ss[3 + i * 4] * 256 + ss[4 + i * 4];
301 GST_TRACE_OBJECT (self, "N_S[%d]: WIDTH=%u, HEIGHT=%u", i,
302 self->ss_width, self->ss_height);
308 guint n_g = ss[sssize];
310 if (G_UNLIKELY (size < hdrsize + sssize + 1))
312 for (i = 0; i < n_g; i++) {
313 guint t = (ss[sssize] & 0xe0) >> 5;
314 guint u = (ss[sssize] & 0x10) >> 4;
315 guint r = (ss[sssize] & 0x0c) >> 2;
316 GST_TRACE_OBJECT (self, "N_G[%u]: 0x%02x -> T=%u, U=%u, R=%u", i,
317 ss[sssize], t, u, r);
318 for (j = 0; j < r; j++)
319 GST_TRACE_OBJECT (self, " R[%u]: P_DIFF=%u", j, ss[sssize + 1 + j]);
321 if (G_UNLIKELY (size < hdrsize + sssize + 1))
328 GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u, picture id 0x%x",
329 hdrsize, size, picture_id);
331 if (G_UNLIKELY (hdrsize >= size))
334 is_start_of_picture = b_bit && (!l_bit || !d_bit);
335 /* If this is a start frame AND we are already processing a frame, we need to flush and wait for next start frame */
336 if (is_start_of_picture) {
337 if (G_UNLIKELY (self->started)) {
338 GST_DEBUG_OBJECT (depay, "Incomplete frame, flushing adapter");
339 gst_adapter_clear (self->adapter);
340 self->started = FALSE;
344 if (G_UNLIKELY (!self->started)) {
345 /* Check if this is the start of a VP9 layer frame, otherwise bail */
347 GST_DEBUG_OBJECT (depay,
348 "The layer is missing the first packets, ignoring the packet");
349 if (self->stop_lost_events) {
350 send_last_lost_event (self);
351 self->stop_lost_events = FALSE;
356 GST_DEBUG_OBJECT (depay, "Found the start of the layer");
357 if (self->stop_lost_events) {
358 send_last_lost_event_if_needed (self, picture_id);
359 self->stop_lost_events = FALSE;
361 self->started = TRUE;
362 self->inter_picture = FALSE;
365 payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
366 if (GST_LEVEL_MEMDUMP <= gst_debug_category_get_threshold (GST_CAT_DEFAULT)) {
368 gst_buffer_map (payload, &map, GST_MAP_READ);
369 GST_MEMDUMP_OBJECT (self, "vp9 payload", map.data, 16);
370 gst_buffer_unmap (payload, &map);
372 gst_adapter_push (self->adapter, payload);
373 self->last_picture_id = picture_id;
374 self->inter_picture |= p_bit;
376 /* Marker indicates that it was the last rtp packet for this picture. Note
377 * that if spatial scalability is used, e_bit will be set for the last
378 * packet of a frame while the marker bit is not set until the last packet
380 if (gst_rtp_buffer_get_marker (rtp)) {
383 GST_DEBUG_OBJECT (depay,
384 "Found the end of the frame (%" G_GSIZE_FORMAT " bytes)",
385 gst_adapter_available (self->adapter));
387 if (gst_adapter_available (self->adapter) < 10)
390 out = gst_adapter_take_buffer (self->adapter,
391 gst_adapter_available (self->adapter));
393 self->started = FALSE;
396 out = gst_buffer_make_writable (out);
397 /* Filter away all metas that are not sensible to copy */
398 gst_rtp_drop_non_video_meta (self, out);
399 if (self->inter_picture) {
400 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
402 if (!self->caps_sent) {
403 gst_buffer_unref (out);
405 GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
406 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
407 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
411 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
413 if (self->last_width != self->ss_width ||
414 self->last_height != self->ss_height) {
417 /* Width and height are optional in the RTP header. Consider to parse
418 * the frame header in addition if missing from RTP header */
419 if (self->ss_width != 0 && self->ss_height != 0) {
420 srccaps = gst_caps_new_simple ("video/x-vp9",
421 "framerate", GST_TYPE_FRACTION, 0, 1,
422 "width", G_TYPE_INT, self->ss_width,
423 "height", G_TYPE_INT, self->ss_height, NULL);
425 srccaps = gst_caps_new_simple ("video/x-vp9",
426 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
429 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
430 gst_caps_unref (srccaps);
432 self->caps_sent = TRUE;
433 self->last_width = self->ss_width;
434 self->last_height = self->ss_height;
440 if (picture_id != PICTURE_ID_NONE)
441 self->stop_lost_events = TRUE;
449 GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
450 gst_adapter_clear (self->adapter);
451 self->started = FALSE;
455 static GstStateChangeReturn
456 gst_rtp_vp9_depay_change_state (GstElement * element, GstStateChange transition)
458 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (element);
460 switch (transition) {
461 case GST_STATE_CHANGE_READY_TO_PAUSED:
462 self->last_width = -1;
463 self->last_height = -1;
464 self->caps_sent = FALSE;
465 self->last_picture_id = PICTURE_ID_NONE;
466 gst_event_replace (&self->last_lost_event, NULL);
467 self->stop_lost_events = FALSE;
474 GST_ELEMENT_CLASS (gst_rtp_vp9_depay_parent_class)->change_state (element,
479 gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
481 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
483 switch (GST_EVENT_TYPE (event)) {
484 case GST_EVENT_FLUSH_STOP:
485 self->last_width = -1;
486 self->last_height = -1;
487 self->last_picture_id = PICTURE_ID_NONE;
488 gst_event_replace (&self->last_lost_event, NULL);
489 self->stop_lost_events = FALSE;
496 GST_RTP_BASE_DEPAYLOAD_CLASS
497 (gst_rtp_vp9_depay_parent_class)->handle_event (depay, event);
501 gst_rtp_vp9_depay_packet_lost (GstRTPBaseDepayload * depay, GstEvent * event)
503 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
504 const GstStructure *s;
505 gboolean might_have_been_fec;
507 s = gst_event_get_structure (event);
509 if (self->stop_lost_events) {
510 if (gst_structure_get_boolean (s, "might-have-been-fec",
511 &might_have_been_fec)
512 && might_have_been_fec) {
513 GST_DEBUG_OBJECT (depay, "Stopping lost event %" GST_PTR_FORMAT, event);
514 gst_event_replace (&self->last_lost_event, event);
517 } else if (self->last_picture_id != PICTURE_ID_NONE) {
518 GstStructure *s = gst_event_writable_structure (self->last_lost_event);
520 /* We are currently processing a picture, let's make sure the
521 * base depayloader doesn't drop this lost event */
522 gst_structure_remove_field (s, "might-have-been-fec");
526 GST_RTP_BASE_DEPAYLOAD_CLASS
527 (gst_rtp_vp9_depay_parent_class)->packet_lost (depay, event);