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 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
28 #define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug
30 static void gst_rtp_vp8_depay_dispose (GObject * object);
31 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
33 static gboolean gst_rtp_vp8_depay_set_caps (GstRTPBaseDepayload * depayload,
36 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
38 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
39 GST_STATIC_PAD_TEMPLATE ("src",
42 GST_STATIC_CAPS ("video/x-vp8"));
44 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "clock-rate = (int) 90000,"
50 "media = (string) \"video\","
51 "encoding-name = (string) \"VP8-DRAFT-IETF-01\""));
54 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
56 self->adapter = gst_adapter_new ();
57 self->started = FALSE;
61 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
63 GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
64 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
65 GstRTPBaseDepayloadClass *depay_class =
66 (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);
69 gst_element_class_add_pad_template (element_class,
70 gst_static_pad_template_get (&gst_rtp_vp8_depay_sink_template));
71 gst_element_class_add_pad_template (element_class,
72 gst_static_pad_template_get (&gst_rtp_vp8_depay_src_template));
74 gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
75 "Codec/Depayloader/Network/RTP",
76 "Extracts VP8 video from RTP packets)",
77 "Sjoerd Simons <sjoerd@luon.net>");
79 object_class->dispose = gst_rtp_vp8_depay_dispose;
81 depay_class->process = gst_rtp_vp8_depay_process;
82 depay_class->set_caps = gst_rtp_vp8_depay_set_caps;
84 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
85 "VP8 Video RTP Depayloader");
89 gst_rtp_vp8_depay_dispose (GObject * object)
91 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
93 if (self->adapter != NULL)
94 g_object_unref (self->adapter);
97 /* release any references held by the object here */
99 if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
100 G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
104 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstBuffer * buf)
106 GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
111 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
113 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (buf))) {
114 GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
115 gst_adapter_clear (self->adapter);
116 self->started = FALSE;
119 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuffer);
120 size = gst_rtp_buffer_get_payload_len (&rtpbuffer);
122 /* At least one header and one vp8 byte */
123 if (G_UNLIKELY (size < 2))
126 data = gst_rtp_buffer_get_payload (&rtpbuffer);
128 if (G_UNLIKELY (!self->started)) {
129 /* Check if this is the start of a VP8 frame, otherwise bail */
130 /* S=1 and PartID= 0 */
131 if ((data[0] & 0x1F) != 0x10)
134 self->started = TRUE;
138 /* Check X optional header */
139 if ((data[0] & 0x80) != 0) {
141 /* Check I optional header */
142 if ((data[1] & 0x80) != 0) {
144 if (G_UNLIKELY (offset + 2 >= size))
146 /* Check for 16 bits PictureID */
147 if ((data[2] & 0x80) != 0)
150 /* Check L optional header */
151 if ((data[1] & 0x40) != 0)
153 /* Check T or K optional headers */
154 if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
158 if (G_UNLIKELY (offset >= size))
161 payload = gst_rtp_buffer_get_payload_subbuffer (&rtpbuffer, offset, -1);
162 gst_adapter_push (self->adapter, payload);
164 /* Marker indicates that it was the last rtp packet for this frame */
165 if (gst_rtp_buffer_get_marker (&rtpbuffer)) {
169 gst_adapter_copy (self->adapter, &flag0, 0, 1);
171 out = gst_adapter_take_buffer (self->adapter,
172 gst_adapter_available (self->adapter));
174 self->started = FALSE;
175 gst_rtp_buffer_unmap (&rtpbuffer);
178 out = gst_buffer_make_writable (out);
180 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
182 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
188 gst_rtp_buffer_unmap (&rtpbuffer);
192 GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
193 gst_adapter_clear (self->adapter);
194 self->started = FALSE;
200 gst_rtp_vp8_depay_set_caps (GstRTPBaseDepayload * depayload, GstCaps * caps)
202 GstCaps *srccaps = gst_caps_new_simple ("video/x-vp8",
203 "framerate", GST_TYPE_FRACTION, 0, 1,
206 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
207 gst_caps_unref (srccaps);
213 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
215 return gst_element_register (plugin, "rtpvp8depay",
216 GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);