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"
27 #include <gst/video/video.h>
31 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
32 #define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug
34 static void gst_rtp_vp8_depay_dispose (GObject * object);
35 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
37 static GstStateChangeReturn gst_rtp_vp8_depay_change_state (GstElement *
38 element, GstStateChange transition);
39 static gboolean gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay,
42 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
44 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_STATIC_CAPS ("video/x-vp8"));
50 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
54 GST_STATIC_CAPS ("application/x-rtp, "
55 "clock-rate = (int) 90000,"
56 "media = (string) \"video\","
57 "encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
60 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
62 self->adapter = gst_adapter_new ();
63 self->started = FALSE;
67 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
69 GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
70 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
71 GstRTPBaseDepayloadClass *depay_class =
72 (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);
75 gst_element_class_add_pad_template (element_class,
76 gst_static_pad_template_get (&gst_rtp_vp8_depay_sink_template));
77 gst_element_class_add_pad_template (element_class,
78 gst_static_pad_template_get (&gst_rtp_vp8_depay_src_template));
80 gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
81 "Codec/Depayloader/Network/RTP",
82 "Extracts VP8 video from RTP packets)",
83 "Sjoerd Simons <sjoerd@luon.net>");
85 object_class->dispose = gst_rtp_vp8_depay_dispose;
87 element_class->change_state = gst_rtp_vp8_depay_change_state;
89 depay_class->process = gst_rtp_vp8_depay_process;
90 depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
92 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
93 "VP8 Video RTP Depayloader");
97 gst_rtp_vp8_depay_dispose (GObject * object)
99 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
101 if (self->adapter != NULL)
102 g_object_unref (self->adapter);
103 self->adapter = NULL;
105 /* release any references held by the object here */
107 if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
108 G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
112 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstBuffer * buf)
114 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
119 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
121 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (buf))) {
122 GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
123 gst_adapter_clear (self->adapter);
124 self->started = FALSE;
127 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuffer);
128 size = gst_rtp_buffer_get_payload_len (&rtpbuffer);
130 /* At least one header and one vp8 byte */
131 if (G_UNLIKELY (size < 2))
134 data = gst_rtp_buffer_get_payload (&rtpbuffer);
136 if (G_UNLIKELY (!self->started)) {
137 /* Check if this is the start of a VP8 frame, otherwise bail */
138 /* S=1 and PartID= 0 */
139 if ((data[0] & 0x1F) != 0x10)
142 self->started = TRUE;
146 /* Check X optional header */
147 if ((data[0] & 0x80) != 0) {
149 /* Check I optional header */
150 if ((data[1] & 0x80) != 0) {
151 if (G_UNLIKELY (size < 3))
154 /* Check for 16 bits PictureID */
155 if ((data[2] & 0x80) != 0)
158 /* Check L optional header */
159 if ((data[1] & 0x40) != 0)
161 /* Check T or K optional headers */
162 if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
165 GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
167 if (G_UNLIKELY (hdrsize >= size))
170 payload = gst_rtp_buffer_get_payload_subbuffer (&rtpbuffer, hdrsize, -1);
171 gst_adapter_push (self->adapter, payload);
173 /* Marker indicates that it was the last rtp packet for this frame */
174 if (gst_rtp_buffer_get_marker (&rtpbuffer)) {
178 gst_adapter_copy (self->adapter, &flag0, 0, 1);
180 out = gst_adapter_take_buffer (self->adapter,
181 gst_adapter_available (self->adapter));
183 self->started = FALSE;
184 gst_rtp_buffer_unmap (&rtpbuffer);
187 out = gst_buffer_make_writable (out);
188 if ((flag0 & 0x01)) {
189 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
191 if (!self->caps_sent) {
192 gst_buffer_unref (out);
194 GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
195 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
196 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
202 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
204 if (gst_buffer_map (out, &info, GST_MAP_READ)) {
205 guint profile, width, height;
207 profile = (flag0 & 0x0e) >> 1;
208 width = GST_READ_UINT16_LE (info.data + 6) & 0x3fff;
209 height = GST_READ_UINT16_LE (info.data + 8) & 0x3fff;
210 gst_buffer_unmap (out, &info);
212 if (G_UNLIKELY (self->last_width != width ||
213 self->last_height != height || self->last_profile != profile)) {
214 gchar profile_str[3];
217 snprintf (profile_str, 3, "%u", profile);
218 srccaps = gst_caps_new_simple ("video/x-vp8",
219 "framerate", GST_TYPE_FRACTION, 0, 1,
220 "height", G_TYPE_INT, height,
221 "width", G_TYPE_INT, width,
222 "profile", G_TYPE_STRING, profile_str, NULL);
224 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
225 gst_caps_unref (srccaps);
227 self->caps_sent = TRUE;
228 self->last_width = width;
229 self->last_height = height;
230 self->last_profile = profile;
239 gst_rtp_buffer_unmap (&rtpbuffer);
243 GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
244 gst_adapter_clear (self->adapter);
245 self->started = FALSE;
250 static GstStateChangeReturn
251 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
253 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
255 switch (transition) {
256 case GST_STATE_CHANGE_READY_TO_PAUSED:
257 self->last_profile = -1;
258 self->last_height = -1;
259 self->last_width = -1;
260 self->caps_sent = FALSE;
267 GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
272 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
274 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
276 switch (GST_EVENT_TYPE (event)) {
277 case GST_EVENT_FLUSH_STOP:
278 self->last_profile = -1;
279 self->last_height = -1;
280 self->last_width = -1;
287 GST_RTP_BASE_DEPAYLOAD_CLASS
288 (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
292 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
294 return gst_element_register (plugin, "rtpvp8depay",
295 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);