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 GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
38 static GstStateChangeReturn gst_rtp_vp8_depay_change_state (GstElement *
39 element, GstStateChange transition);
40 static gboolean gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay,
43 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
45 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("video/x-vp8"));
51 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("application/x-rtp, "
56 "clock-rate = (int) 90000,"
57 "media = (string) \"video\","
58 "encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
61 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
63 self->adapter = gst_adapter_new ();
64 self->started = FALSE;
68 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
70 GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
71 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
72 GstRTPBaseDepayloadClass *depay_class =
73 (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);
76 gst_element_class_add_static_pad_template (element_class,
77 &gst_rtp_vp8_depay_sink_template);
78 gst_element_class_add_static_pad_template (element_class,
79 &gst_rtp_vp8_depay_src_template);
81 gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
82 "Codec/Depayloader/Network/RTP",
83 "Extracts VP8 video from RTP packets)",
84 "Sjoerd Simons <sjoerd@luon.net>");
86 object_class->dispose = gst_rtp_vp8_depay_dispose;
88 element_class->change_state = gst_rtp_vp8_depay_change_state;
90 depay_class->process_rtp_packet = gst_rtp_vp8_depay_process;
91 depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
93 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
94 "VP8 Video RTP Depayloader");
98 gst_rtp_vp8_depay_dispose (GObject * object)
100 GstRtpVP8Depay *self = GST_RTP_VP8_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_vp8_depay_parent_class)->dispose)
109 G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
113 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
115 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
121 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
122 GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
123 gst_adapter_clear (self->adapter);
124 self->started = FALSE;
127 size = gst_rtp_buffer_get_payload_len (rtp);
129 /* At least one header and one vp8 byte */
130 if (G_UNLIKELY (size < 2))
133 data = gst_rtp_buffer_get_payload (rtp);
135 if (G_UNLIKELY (!self->started)) {
136 /* Check if this is the start of a VP8 frame, otherwise bail */
137 /* S=1 and PartID= 0 */
138 if ((data[0] & 0x17) != 0x10)
141 self->started = TRUE;
145 /* Check X optional header */
146 if ((data[0] & 0x80) != 0) {
148 /* Check I optional header */
149 if ((data[1] & 0x80) != 0) {
150 if (G_UNLIKELY (size < 3))
153 /* Check for 16 bits PictureID */
154 if ((data[2] & 0x80) != 0)
157 /* Check L optional header */
158 if ((data[1] & 0x40) != 0)
160 /* Check T or K optional headers */
161 if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
164 GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
166 if (G_UNLIKELY (hdrsize >= size))
169 payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
170 gst_adapter_push (self->adapter, payload);
172 /* Marker indicates that it was the last rtp packet for this frame */
173 if (gst_rtp_buffer_get_marker (rtp)) {
177 if (gst_adapter_available (self->adapter) < 10)
179 gst_adapter_copy (self->adapter, &header, 0, 10);
181 out = gst_adapter_take_buffer (self->adapter,
182 gst_adapter_available (self->adapter));
184 self->started = FALSE;
187 out = gst_buffer_make_writable (out);
188 /* Filter away all metas that are not sensible to copy */
189 gst_rtp_drop_meta (GST_ELEMENT_CAST (self), out,
190 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
191 if ((header[0] & 0x01)) {
192 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
194 if (!self->caps_sent) {
195 gst_buffer_unref (out);
197 GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
198 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
199 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
203 guint profile, width, height;
205 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
207 profile = (header[0] & 0x0e) >> 1;
208 width = GST_READ_UINT16_LE (header + 6) & 0x3fff;
209 height = GST_READ_UINT16_LE (header + 8) & 0x3fff;
211 if (G_UNLIKELY (self->last_width != width ||
212 self->last_height != height || self->last_profile != profile)) {
213 gchar profile_str[3];
216 snprintf (profile_str, 3, "%u", profile);
217 srccaps = gst_caps_new_simple ("video/x-vp8",
218 "framerate", GST_TYPE_FRACTION, 0, 1,
219 "height", G_TYPE_INT, height,
220 "width", G_TYPE_INT, width,
221 "profile", G_TYPE_STRING, profile_str, NULL);
223 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
224 gst_caps_unref (srccaps);
226 self->caps_sent = TRUE;
227 self->last_width = width;
228 self->last_height = height;
229 self->last_profile = profile;
240 GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
241 gst_adapter_clear (self->adapter);
242 self->started = FALSE;
247 static GstStateChangeReturn
248 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
250 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
252 switch (transition) {
253 case GST_STATE_CHANGE_READY_TO_PAUSED:
254 self->last_profile = -1;
255 self->last_height = -1;
256 self->last_width = -1;
257 self->caps_sent = FALSE;
264 GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
269 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
271 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
273 switch (GST_EVENT_TYPE (event)) {
274 case GST_EVENT_FLUSH_STOP:
275 self->last_profile = -1;
276 self->last_height = -1;
277 self->last_width = -1;
284 GST_RTP_BASE_DEPAYLOAD_CLASS
285 (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
289 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
291 return gst_element_register (plugin, "rtpvp8depay",
292 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);