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