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 #ifdef TIZEN_FEATURE_GST_UPSTREAM
37 static void gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
38 GValue * value, GParamSpec * pspec);
39 static void gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
40 const GValue * value, GParamSpec * pspec);
42 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
44 static GstStateChangeReturn gst_rtp_vp8_depay_change_state (GstElement *
45 element, GstStateChange transition);
46 static gboolean gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay,
49 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
51 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("video/x-vp8"));
57 static GstStaticPadTemplate gst_rtp_vp8_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) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
66 #ifdef TIZEN_FEATURE_GST_UPSTREAM
67 #define DEFAULT_WAIT_FOR_KEYFRAME FALSE
72 PROP_WAIT_FOR_KEYFRAME
77 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
79 self->adapter = gst_adapter_new ();
80 self->started = FALSE;
81 #ifdef TIZEN_FEATURE_GST_UPSTREAM
82 self->wait_for_keyframe = DEFAULT_WAIT_FOR_KEYFRAME;
87 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
89 GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
90 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
91 GstRTPBaseDepayloadClass *depay_class =
92 (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);
95 gst_element_class_add_static_pad_template (element_class,
96 &gst_rtp_vp8_depay_sink_template);
97 gst_element_class_add_static_pad_template (element_class,
98 &gst_rtp_vp8_depay_src_template);
100 gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
101 "Codec/Depayloader/Network/RTP",
102 "Extracts VP8 video from RTP packets)",
103 "Sjoerd Simons <sjoerd@luon.net>");
105 object_class->dispose = gst_rtp_vp8_depay_dispose;
106 #ifdef TIZEN_FEATURE_GST_UPSTREAM
107 object_class->set_property = gst_rtp_vp8_depay_set_property;
108 object_class->get_property = gst_rtp_vp8_depay_get_property;
110 g_object_class_install_property (object_class, PROP_WAIT_FOR_KEYFRAME,
111 g_param_spec_boolean ("wait-for-keyframe", "Wait for Keyframe",
112 "Wait for the next keyframe after packet loss",
113 DEFAULT_WAIT_FOR_KEYFRAME,
114 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116 element_class->change_state = gst_rtp_vp8_depay_change_state;
118 depay_class->process_rtp_packet = gst_rtp_vp8_depay_process;
119 depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
121 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
122 "VP8 Video RTP Depayloader");
126 gst_rtp_vp8_depay_dispose (GObject * object)
128 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
130 if (self->adapter != NULL)
131 g_object_unref (self->adapter);
132 self->adapter = NULL;
134 /* release any references held by the object here */
136 if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
137 G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
140 #ifdef TIZEN_FEATURE_GST_UPSTREAM
142 gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
143 const GValue * value, GParamSpec * pspec)
145 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
148 case PROP_WAIT_FOR_KEYFRAME:
149 self->wait_for_keyframe = g_value_get_boolean (value);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158 gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
159 GValue * value, GParamSpec * pspec)
161 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
164 case PROP_WAIT_FOR_KEYFRAME:
165 g_value_set_boolean (value, self->wait_for_keyframe);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
177 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
183 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
184 GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
185 gst_adapter_clear (self->adapter);
186 self->started = FALSE;
187 #ifdef TIZEN_FEATURE_GST_UPSTREAM
188 if (self->wait_for_keyframe)
189 self->waiting_for_keyframe = TRUE;
193 size = gst_rtp_buffer_get_payload_len (rtp);
195 /* At least one header and one vp8 byte */
196 if (G_UNLIKELY (size < 2))
199 data = gst_rtp_buffer_get_payload (rtp);
201 if (G_UNLIKELY (!self->started)) {
202 /* Check if this is the start of a VP8 frame, otherwise bail */
203 /* S=1 and PartID= 0 */
204 if ((data[0] & 0x17) != 0x10)
207 self->started = TRUE;
211 /* Check X optional header */
212 if ((data[0] & 0x80) != 0) {
214 /* Check I optional header */
215 if ((data[1] & 0x80) != 0) {
216 if (G_UNLIKELY (size < 3))
219 /* Check for 16 bits PictureID */
220 if ((data[2] & 0x80) != 0)
223 /* Check L optional header */
224 if ((data[1] & 0x40) != 0)
226 /* Check T or K optional headers */
227 if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
230 GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
232 if (G_UNLIKELY (hdrsize >= size))
235 payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
236 gst_adapter_push (self->adapter, payload);
238 /* Marker indicates that it was the last rtp packet for this frame */
239 if (gst_rtp_buffer_get_marker (rtp)) {
243 if (gst_adapter_available (self->adapter) < 10)
245 gst_adapter_copy (self->adapter, &header, 0, 10);
247 out = gst_adapter_take_buffer (self->adapter,
248 gst_adapter_available (self->adapter));
250 self->started = FALSE;
253 out = gst_buffer_make_writable (out);
254 /* Filter away all metas that are not sensible to copy */
255 gst_rtp_drop_non_video_meta (self, out);
256 if ((header[0] & 0x01)) {
257 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
259 #ifdef TIZEN_FEATURE_GST_UPSTREAM
260 if (self->waiting_for_keyframe) {
262 if (!self->caps_sent) {
264 gst_buffer_unref (out);
266 GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
267 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
268 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
272 guint profile, width, height;
274 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
276 profile = (header[0] & 0x0e) >> 1;
277 width = GST_READ_UINT16_LE (header + 6) & 0x3fff;
278 height = GST_READ_UINT16_LE (header + 8) & 0x3fff;
280 if (G_UNLIKELY (self->last_width != width ||
281 self->last_height != height || self->last_profile != profile)) {
282 gchar profile_str[3];
285 snprintf (profile_str, 3, "%u", profile);
286 srccaps = gst_caps_new_simple ("video/x-vp8",
287 "framerate", GST_TYPE_FRACTION, 0, 1,
288 "height", G_TYPE_INT, height,
289 "width", G_TYPE_INT, width,
290 "profile", G_TYPE_STRING, profile_str, NULL);
292 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
293 gst_caps_unref (srccaps);
294 #ifndef TIZEN_FEATURE_GST_UPSTREAM
295 self->caps_sent = TRUE;
297 self->last_width = width;
298 self->last_height = height;
299 self->last_profile = profile;
301 #ifdef TIZEN_FEATURE_GST_UPSTREAM
302 self->waiting_for_keyframe = FALSE;
313 GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
314 gst_adapter_clear (self->adapter);
315 self->started = FALSE;
320 static GstStateChangeReturn
321 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
323 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
325 switch (transition) {
326 case GST_STATE_CHANGE_READY_TO_PAUSED:
327 self->last_profile = -1;
328 self->last_height = -1;
329 self->last_width = -1;
330 #ifdef TIZEN_FEATURE_GST_UPSTREAM
331 self->waiting_for_keyframe = TRUE;
333 self->caps_sent = FALSE;
341 GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
346 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
348 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
350 switch (GST_EVENT_TYPE (event)) {
351 case GST_EVENT_FLUSH_STOP:
352 self->last_profile = -1;
353 self->last_height = -1;
354 self->last_width = -1;
361 GST_RTP_BASE_DEPAYLOAD_CLASS
362 (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
366 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
368 return gst_element_register (plugin, "rtpvp8depay",
369 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);