1 /* gstrtpvp8depay.c - Source for GstRtpVP8Depay
2 * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
3 * Copyright (C) 2011 Collabora Ltd.
4 * Contact: Youness Alaoui <youness.alaoui@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "gstrtpvp8depay.h"
26 #include "gstrtputils.h"
28 #include <gst/video/video.h>
32 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
33 #define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug
35 static void gst_rtp_vp8_depay_dispose (GObject * object);
36 static void gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
37 GValue * value, GParamSpec * pspec);
38 static void gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
39 const GValue * value, GParamSpec * pspec);
40 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
42 static GstStateChangeReturn gst_rtp_vp8_depay_change_state (GstElement *
43 element, GstStateChange transition);
44 static gboolean gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay,
47 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
49 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("video/x-vp8"));
55 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("application/x-rtp, "
60 "clock-rate = (int) 90000,"
61 "media = (string) \"video\","
62 "encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
64 #define DEFAULT_WAIT_FOR_KEYFRAME FALSE
69 PROP_WAIT_FOR_KEYFRAME
73 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
75 self->adapter = gst_adapter_new ();
76 self->started = FALSE;
77 self->wait_for_keyframe = DEFAULT_WAIT_FOR_KEYFRAME;
81 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
83 GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
84 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
85 GstRTPBaseDepayloadClass *depay_class =
86 (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);
88 gst_element_class_add_static_pad_template (element_class,
89 &gst_rtp_vp8_depay_sink_template);
90 gst_element_class_add_static_pad_template (element_class,
91 &gst_rtp_vp8_depay_src_template);
93 gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
94 "Codec/Depayloader/Network/RTP",
95 "Extracts VP8 video from RTP packets)",
96 "Sjoerd Simons <sjoerd@luon.net>");
98 object_class->dispose = gst_rtp_vp8_depay_dispose;
99 object_class->set_property = gst_rtp_vp8_depay_set_property;
100 object_class->get_property = gst_rtp_vp8_depay_get_property;
102 g_object_class_install_property (object_class, PROP_WAIT_FOR_KEYFRAME,
103 g_param_spec_boolean ("wait-for-keyframe", "Wait for Keyframe",
104 "Wait for the next keyframe after packet loss",
105 DEFAULT_WAIT_FOR_KEYFRAME,
106 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
108 element_class->change_state = gst_rtp_vp8_depay_change_state;
110 depay_class->process_rtp_packet = gst_rtp_vp8_depay_process;
111 depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
113 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
114 "VP8 Video RTP Depayloader");
118 gst_rtp_vp8_depay_dispose (GObject * object)
120 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
122 if (self->adapter != NULL)
123 g_object_unref (self->adapter);
124 self->adapter = NULL;
126 /* release any references held by the object here */
128 if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
129 G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
133 gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
134 const GValue * value, GParamSpec * pspec)
136 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
139 case PROP_WAIT_FOR_KEYFRAME:
140 self->wait_for_keyframe = g_value_get_boolean (value);
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149 gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
150 GValue * value, GParamSpec * pspec)
152 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
155 case PROP_WAIT_FOR_KEYFRAME:
156 g_value_set_boolean (value, self->wait_for_keyframe);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
167 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
173 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
174 GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
175 gst_adapter_clear (self->adapter);
176 self->started = FALSE;
178 if (self->wait_for_keyframe)
179 self->waiting_for_keyframe = TRUE;
182 size = gst_rtp_buffer_get_payload_len (rtp);
184 /* At least one header and one vp8 byte */
185 if (G_UNLIKELY (size < 2))
188 data = gst_rtp_buffer_get_payload (rtp);
190 if (G_UNLIKELY (!self->started)) {
191 /* Check if this is the start of a VP8 frame, otherwise bail */
192 /* S=1 and PartID= 0 */
193 if ((data[0] & 0x17) != 0x10)
196 self->started = TRUE;
200 /* Check X optional header */
201 if ((data[0] & 0x80) != 0) {
203 /* Check I optional header */
204 if ((data[1] & 0x80) != 0) {
205 if (G_UNLIKELY (size < 3))
208 /* Check for 16 bits PictureID */
209 if ((data[2] & 0x80) != 0)
212 /* Check L optional header */
213 if ((data[1] & 0x40) != 0)
215 /* Check T or K optional headers */
216 if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
219 GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
221 if (G_UNLIKELY (hdrsize >= size))
224 payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
225 gst_adapter_push (self->adapter, payload);
227 /* Marker indicates that it was the last rtp packet for this frame */
228 if (gst_rtp_buffer_get_marker (rtp)) {
232 if (gst_adapter_available (self->adapter) < 10)
234 gst_adapter_copy (self->adapter, &header, 0, 10);
236 out = gst_adapter_take_buffer (self->adapter,
237 gst_adapter_available (self->adapter));
239 self->started = FALSE;
242 out = gst_buffer_make_writable (out);
243 /* Filter away all metas that are not sensible to copy */
244 gst_rtp_drop_non_video_meta (self, out);
245 if ((header[0] & 0x01)) {
246 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
248 if (self->waiting_for_keyframe) {
249 gst_buffer_unref (out);
251 GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
252 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
253 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
257 guint profile, width, height;
259 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
261 profile = (header[0] & 0x0e) >> 1;
262 width = GST_READ_UINT16_LE (header + 6) & 0x3fff;
263 height = GST_READ_UINT16_LE (header + 8) & 0x3fff;
265 if (G_UNLIKELY (self->last_width != width ||
266 self->last_height != height || self->last_profile != profile)) {
267 gchar profile_str[3];
270 snprintf (profile_str, 3, "%u", profile);
271 srccaps = gst_caps_new_simple ("video/x-vp8",
272 "framerate", GST_TYPE_FRACTION, 0, 1,
273 "height", G_TYPE_INT, height,
274 "width", G_TYPE_INT, width,
275 "profile", G_TYPE_STRING, profile_str, NULL);
277 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
278 gst_caps_unref (srccaps);
280 self->last_width = width;
281 self->last_height = height;
282 self->last_profile = profile;
284 self->waiting_for_keyframe = FALSE;
294 GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
295 gst_adapter_clear (self->adapter);
296 self->started = FALSE;
301 static GstStateChangeReturn
302 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
304 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
306 switch (transition) {
307 case GST_STATE_CHANGE_READY_TO_PAUSED:
308 self->last_profile = -1;
309 self->last_height = -1;
310 self->last_width = -1;
311 self->waiting_for_keyframe = TRUE;
318 GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
323 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
325 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
327 switch (GST_EVENT_TYPE (event)) {
328 case GST_EVENT_FLUSH_STOP:
329 self->last_profile = -1;
330 self->last_height = -1;
331 self->last_width = -1;
338 GST_RTP_BASE_DEPAYLOAD_CLASS
339 (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
343 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
345 return gst_element_register (plugin, "rtpvp8depay",
346 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);