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>
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 
25 #include "gstrtpvp8depay.h"
26 #include "gstrtputils.h"
27 
28 #include <gst/video/video.h>
29 
30 #include <stdio.h>
31 
32 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
33 #define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug
34 
35 static void gst_rtp_vp8_depay_dispose (GObject * object);
36 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
37     GstRTPBuffer * rtp);
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,
41     GstEvent * event);
42 
43 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
44 
45 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("video/x-vp8"));
50 
51 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
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\" }"));
59 
60 static void
gst_rtp_vp8_depay_init(GstRtpVP8Depay * self)61 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
62 {
63   self->adapter = gst_adapter_new ();
64   self->started = FALSE;
65 }
66 
67 static void
gst_rtp_vp8_depay_class_init(GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)68 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
69 {
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);
74 
75 
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);
80 
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>");
85 
86   object_class->dispose = gst_rtp_vp8_depay_dispose;
87 
88   element_class->change_state = gst_rtp_vp8_depay_change_state;
89 
90   depay_class->process_rtp_packet = gst_rtp_vp8_depay_process;
91   depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
92 
93   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
94       "VP8 Video RTP Depayloader");
95 }
96 
97 static void
gst_rtp_vp8_depay_dispose(GObject * object)98 gst_rtp_vp8_depay_dispose (GObject * object)
99 {
100   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
101 
102   if (self->adapter != NULL)
103     g_object_unref (self->adapter);
104   self->adapter = NULL;
105 
106   /* release any references held by the object here */
107 
108   if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
109     G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
110 }
111 
112 static GstBuffer *
gst_rtp_vp8_depay_process(GstRTPBaseDepayload * depay,GstRTPBuffer * rtp)113 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
114 {
115   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
116   GstBuffer *payload;
117   guint8 *data;
118   guint hdrsize;
119   guint size;
120 
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;
125   }
126 
127   size = gst_rtp_buffer_get_payload_len (rtp);
128 
129   /* At least one header and one vp8 byte */
130   if (G_UNLIKELY (size < 2))
131     goto too_small;
132 
133   data = gst_rtp_buffer_get_payload (rtp);
134 
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)
139       goto done;
140 
141     self->started = TRUE;
142   }
143 
144   hdrsize = 1;
145   /* Check X optional header */
146   if ((data[0] & 0x80) != 0) {
147     hdrsize++;
148     /* Check I optional header */
149     if ((data[1] & 0x80) != 0) {
150       if (G_UNLIKELY (size < 3))
151         goto too_small;
152       hdrsize++;
153       /* Check for 16 bits PictureID */
154       if ((data[2] & 0x80) != 0)
155         hdrsize++;
156     }
157     /* Check L optional header */
158     if ((data[1] & 0x40) != 0)
159       hdrsize++;
160     /* Check T or K optional headers */
161     if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
162       hdrsize++;
163   }
164   GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
165 
166   if (G_UNLIKELY (hdrsize >= size))
167     goto too_small;
168 
169   payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
170   gst_adapter_push (self->adapter, payload);
171 
172   /* Marker indicates that it was the last rtp packet for this frame */
173   if (gst_rtp_buffer_get_marker (rtp)) {
174     GstBuffer *out;
175     guint8 header[10];
176 
177     if (gst_adapter_available (self->adapter) < 10)
178       goto too_small;
179     gst_adapter_copy (self->adapter, &header, 0, 10);
180 
181     out = gst_adapter_take_buffer (self->adapter,
182         gst_adapter_available (self->adapter));
183 
184     self->started = FALSE;
185 
186     /* mark keyframes */
187     out = gst_buffer_make_writable (out);
188     /* Filter away all metas that are not sensible to copy */
189     gst_rtp_drop_non_video_meta (self, out);
190     if ((header[0] & 0x01)) {
191       GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
192 
193       if (!self->caps_sent) {
194         gst_buffer_unref (out);
195         out = NULL;
196         GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
197         gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
198             gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
199                 TRUE, 0));
200       }
201     } else {
202       guint profile, width, height;
203 
204       GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
205 
206       profile = (header[0] & 0x0e) >> 1;
207       width = GST_READ_UINT16_LE (header + 6) & 0x3fff;
208       height = GST_READ_UINT16_LE (header + 8) & 0x3fff;
209 
210       if (G_UNLIKELY (self->last_width != width ||
211               self->last_height != height || self->last_profile != profile)) {
212         gchar profile_str[3];
213         GstCaps *srccaps;
214 
215         snprintf (profile_str, 3, "%u", profile);
216         srccaps = gst_caps_new_simple ("video/x-vp8",
217             "framerate", GST_TYPE_FRACTION, 0, 1,
218             "height", G_TYPE_INT, height,
219             "width", G_TYPE_INT, width,
220             "profile", G_TYPE_STRING, profile_str, NULL);
221 
222         gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
223         gst_caps_unref (srccaps);
224 
225         self->caps_sent = TRUE;
226         self->last_width = width;
227         self->last_height = height;
228         self->last_profile = profile;
229       }
230     }
231 
232     return out;
233   }
234 
235 done:
236   return NULL;
237 
238 too_small:
239   GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
240   gst_adapter_clear (self->adapter);
241   self->started = FALSE;
242 
243   goto done;
244 }
245 
246 static GstStateChangeReturn
gst_rtp_vp8_depay_change_state(GstElement * element,GstStateChange transition)247 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
248 {
249   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
250 
251   switch (transition) {
252     case GST_STATE_CHANGE_READY_TO_PAUSED:
253       self->last_profile = -1;
254       self->last_height = -1;
255       self->last_width = -1;
256       self->caps_sent = FALSE;
257       break;
258     default:
259       break;
260   }
261 
262   return
263       GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
264       transition);
265 }
266 
267 static gboolean
gst_rtp_vp8_depay_handle_event(GstRTPBaseDepayload * depay,GstEvent * event)268 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
269 {
270   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
271 
272   switch (GST_EVENT_TYPE (event)) {
273     case GST_EVENT_FLUSH_STOP:
274       self->last_profile = -1;
275       self->last_height = -1;
276       self->last_width = -1;
277       break;
278     default:
279       break;
280   }
281 
282   return
283       GST_RTP_BASE_DEPAYLOAD_CLASS
284       (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
285 }
286 
287 gboolean
gst_rtp_vp8_depay_plugin_init(GstPlugin * plugin)288 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
289 {
290   return gst_element_register (plugin, "rtpvp8depay",
291       GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);
292 }
293