e71baf8165e99169df2d5f6ba491bad247c78f3d
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpvp8depay.c
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 void gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
37     GValue * value, GParamSpec * pspec);
38 static void gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
39     const GValue * value, GParamSpec * pspec);
40 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
41     GstRTPBuffer * rtp);
42 static GstStateChangeReturn gst_rtp_vp8_depay_change_state (GstElement *
43     element, GstStateChange transition);
44 static gboolean gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay,
45     GstEvent * event);
46
47 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
48
49 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("video/x-vp8"));
54
55 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp, "
60         "clock-rate = (int) 90000,"
61         "media = (string) \"video\","
62         "encoding-name = (string) { \"VP8\", \"VP8-DRAFT-IETF-01\" }"));
63
64 #define DEFAULT_WAIT_FOR_KEYFRAME FALSE
65
66 enum
67 {
68   PROP_0,
69   PROP_WAIT_FOR_KEYFRAME
70 };
71
72 static void
73 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
74 {
75   self->adapter = gst_adapter_new ();
76   self->started = FALSE;
77   self->wait_for_keyframe = DEFAULT_WAIT_FOR_KEYFRAME;
78 }
79
80 static void
81 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
82 {
83   GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
84   GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
85   GstRTPBaseDepayloadClass *depay_class =
86       (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);
87
88   gst_element_class_add_static_pad_template (element_class,
89       &gst_rtp_vp8_depay_sink_template);
90   gst_element_class_add_static_pad_template (element_class,
91       &gst_rtp_vp8_depay_src_template);
92
93   gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
94       "Codec/Depayloader/Network/RTP",
95       "Extracts VP8 video from RTP packets)",
96       "Sjoerd Simons <sjoerd@luon.net>");
97
98   object_class->dispose = gst_rtp_vp8_depay_dispose;
99   object_class->set_property = gst_rtp_vp8_depay_set_property;
100   object_class->get_property = gst_rtp_vp8_depay_get_property;
101
102   g_object_class_install_property (object_class, PROP_WAIT_FOR_KEYFRAME,
103       g_param_spec_boolean ("wait-for-keyframe", "Wait for Keyframe",
104           "Wait for the next keyframe after packet loss",
105           DEFAULT_WAIT_FOR_KEYFRAME,
106           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107
108   element_class->change_state = gst_rtp_vp8_depay_change_state;
109
110   depay_class->process_rtp_packet = gst_rtp_vp8_depay_process;
111   depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
112
113   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
114       "VP8 Video RTP Depayloader");
115 }
116
117 static void
118 gst_rtp_vp8_depay_dispose (GObject * object)
119 {
120   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
121
122   if (self->adapter != NULL)
123     g_object_unref (self->adapter);
124   self->adapter = NULL;
125
126   /* release any references held by the object here */
127
128   if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
129     G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
130 }
131
132 static void
133 gst_rtp_vp8_depay_set_property (GObject * object, guint prop_id,
134     const GValue * value, GParamSpec * pspec)
135 {
136   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
137
138   switch (prop_id) {
139     case PROP_WAIT_FOR_KEYFRAME:
140       self->wait_for_keyframe = g_value_get_boolean (value);
141       break;
142     default:
143       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
144       break;
145   }
146 }
147
148 static void
149 gst_rtp_vp8_depay_get_property (GObject * object, guint prop_id,
150     GValue * value, GParamSpec * pspec)
151 {
152   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
153
154   switch (prop_id) {
155     case PROP_WAIT_FOR_KEYFRAME:
156       g_value_set_boolean (value, self->wait_for_keyframe);
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161   }
162 }
163
164 static GstBuffer *
165 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
166 {
167   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
168   GstBuffer *payload;
169   guint8 *data;
170   guint hdrsize;
171   guint size;
172
173   if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
174     GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
175     gst_adapter_clear (self->adapter);
176     self->started = FALSE;
177
178     if (self->wait_for_keyframe)
179       self->waiting_for_keyframe = TRUE;
180   }
181
182   size = gst_rtp_buffer_get_payload_len (rtp);
183
184   /* At least one header and one vp8 byte */
185   if (G_UNLIKELY (size < 2))
186     goto too_small;
187
188   data = gst_rtp_buffer_get_payload (rtp);
189
190   if (G_UNLIKELY (!self->started)) {
191     /* Check if this is the start of a VP8 frame, otherwise bail */
192     /* S=1 and PartID= 0 */
193     if ((data[0] & 0x17) != 0x10)
194       goto done;
195
196     self->started = TRUE;
197   }
198
199   hdrsize = 1;
200   /* Check X optional header */
201   if ((data[0] & 0x80) != 0) {
202     hdrsize++;
203     /* Check I optional header */
204     if ((data[1] & 0x80) != 0) {
205       if (G_UNLIKELY (size < 3))
206         goto too_small;
207       hdrsize++;
208       /* Check for 16 bits PictureID */
209       if ((data[2] & 0x80) != 0)
210         hdrsize++;
211     }
212     /* Check L optional header */
213     if ((data[1] & 0x40) != 0)
214       hdrsize++;
215     /* Check T or K optional headers */
216     if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
217       hdrsize++;
218   }
219   GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
220
221   if (G_UNLIKELY (hdrsize >= size))
222     goto too_small;
223
224   payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
225   gst_adapter_push (self->adapter, payload);
226
227   /* Marker indicates that it was the last rtp packet for this frame */
228   if (gst_rtp_buffer_get_marker (rtp)) {
229     GstBuffer *out;
230     guint8 header[10];
231
232     if (gst_adapter_available (self->adapter) < 10)
233       goto too_small;
234     gst_adapter_copy (self->adapter, &header, 0, 10);
235
236     out = gst_adapter_take_buffer (self->adapter,
237         gst_adapter_available (self->adapter));
238
239     self->started = FALSE;
240
241     /* mark keyframes */
242     out = gst_buffer_make_writable (out);
243     /* Filter away all metas that are not sensible to copy */
244     gst_rtp_drop_non_video_meta (self, out);
245     if ((header[0] & 0x01)) {
246       GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
247
248       if (self->waiting_for_keyframe) {
249         gst_buffer_unref (out);
250         out = NULL;
251         GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
252         gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
253             gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
254                 TRUE, 0));
255       }
256     } else {
257       guint profile, width, height;
258
259       GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
260
261       profile = (header[0] & 0x0e) >> 1;
262       width = GST_READ_UINT16_LE (header + 6) & 0x3fff;
263       height = GST_READ_UINT16_LE (header + 8) & 0x3fff;
264
265       if (G_UNLIKELY (self->last_width != width ||
266               self->last_height != height || self->last_profile != profile)) {
267         gchar profile_str[3];
268         GstCaps *srccaps;
269
270         snprintf (profile_str, 3, "%u", profile);
271         srccaps = gst_caps_new_simple ("video/x-vp8",
272             "framerate", GST_TYPE_FRACTION, 0, 1,
273             "height", G_TYPE_INT, height,
274             "width", G_TYPE_INT, width,
275             "profile", G_TYPE_STRING, profile_str, NULL);
276
277         gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
278         gst_caps_unref (srccaps);
279
280         self->last_width = width;
281         self->last_height = height;
282         self->last_profile = profile;
283       }
284       self->waiting_for_keyframe = FALSE;
285     }
286
287     return out;
288   }
289
290 done:
291   return NULL;
292
293 too_small:
294   GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
295   gst_adapter_clear (self->adapter);
296   self->started = FALSE;
297
298   goto done;
299 }
300
301 static GstStateChangeReturn
302 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
303 {
304   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
305
306   switch (transition) {
307     case GST_STATE_CHANGE_READY_TO_PAUSED:
308       self->last_profile = -1;
309       self->last_height = -1;
310       self->last_width = -1;
311       self->waiting_for_keyframe = TRUE;
312       break;
313     default:
314       break;
315   }
316
317   return
318       GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
319       transition);
320 }
321
322 static gboolean
323 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
324 {
325   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
326
327   switch (GST_EVENT_TYPE (event)) {
328     case GST_EVENT_FLUSH_STOP:
329       self->last_profile = -1;
330       self->last_height = -1;
331       self->last_width = -1;
332       break;
333     default:
334       break;
335   }
336
337   return
338       GST_RTP_BASE_DEPAYLOAD_CLASS
339       (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
340 }
341
342 gboolean
343 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
344 {
345   return gst_element_register (plugin, "rtpvp8depay",
346       GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);
347 }