rtpvp8depay: When dropping intra packet, request keyframe
[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
27 #include <gst/video/video.h>
28
29 #include <stdio.h>
30
31 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
32 #define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug
33
34 static void gst_rtp_vp8_depay_dispose (GObject * object);
35 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
36     GstBuffer * buf);
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,
40     GstEvent * event);
41
42 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
43
44 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("video/x-vp8"));
49
50 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
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\" }"));
58
59 static void
60 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
61 {
62   self->adapter = gst_adapter_new ();
63   self->started = FALSE;
64 }
65
66 static void
67 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
68 {
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);
73
74
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));
79
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>");
84
85   object_class->dispose = gst_rtp_vp8_depay_dispose;
86
87   element_class->change_state = gst_rtp_vp8_depay_change_state;
88
89   depay_class->process = gst_rtp_vp8_depay_process;
90   depay_class->handle_event = gst_rtp_vp8_depay_handle_event;
91
92   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
93       "VP8 Video RTP Depayloader");
94 }
95
96 static void
97 gst_rtp_vp8_depay_dispose (GObject * object)
98 {
99   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
100
101   if (self->adapter != NULL)
102     g_object_unref (self->adapter);
103   self->adapter = NULL;
104
105   /* release any references held by the object here */
106
107   if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
108     G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
109 }
110
111 static GstBuffer *
112 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstBuffer * buf)
113 {
114   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
115   GstBuffer *payload;
116   guint8 *data;
117   guint hdrsize;
118   guint size;
119   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
120
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;
125   }
126
127   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuffer);
128   size = gst_rtp_buffer_get_payload_len (&rtpbuffer);
129
130   /* At least one header and one vp8 byte */
131   if (G_UNLIKELY (size < 2))
132     goto too_small;
133
134   data = gst_rtp_buffer_get_payload (&rtpbuffer);
135
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)
140       goto done;
141
142     self->started = TRUE;
143   }
144
145   hdrsize = 1;
146   /* Check X optional header */
147   if ((data[0] & 0x80) != 0) {
148     hdrsize++;
149     /* Check I optional header */
150     if ((data[1] & 0x80) != 0) {
151       if (G_UNLIKELY (size < 3))
152         goto too_small;
153       hdrsize++;
154       /* Check for 16 bits PictureID */
155       if ((data[2] & 0x80) != 0)
156         hdrsize++;
157     }
158     /* Check L optional header */
159     if ((data[1] & 0x40) != 0)
160       hdrsize++;
161     /* Check T or K optional headers */
162     if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
163       hdrsize++;
164   }
165   GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
166
167   if (G_UNLIKELY (hdrsize >= size))
168     goto too_small;
169
170   payload = gst_rtp_buffer_get_payload_subbuffer (&rtpbuffer, hdrsize, -1);
171   gst_adapter_push (self->adapter, payload);
172
173   /* Marker indicates that it was the last rtp packet for this frame */
174   if (gst_rtp_buffer_get_marker (&rtpbuffer)) {
175     GstBuffer *out;
176     guint8 flag0;
177
178     gst_adapter_copy (self->adapter, &flag0, 0, 1);
179
180     out = gst_adapter_take_buffer (self->adapter,
181         gst_adapter_available (self->adapter));
182
183     self->started = FALSE;
184     gst_rtp_buffer_unmap (&rtpbuffer);
185
186     /* mark keyframes */
187     out = gst_buffer_make_writable (out);
188     if ((flag0 & 0x01)) {
189       GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
190
191       if (!self->caps_sent) {
192         gst_buffer_unref (out);
193         out = NULL;
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,
197                 TRUE, 0));
198       }
199     } else {
200       GstMapInfo info;
201
202       GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
203
204       if (gst_buffer_map (out, &info, GST_MAP_READ)) {
205         guint profile, width, height;
206
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);
211
212         if (G_UNLIKELY (self->last_width != width ||
213                 self->last_height != height || self->last_profile != profile)) {
214           gchar profile_str[3];
215           GstCaps *srccaps;
216
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);
223
224           gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
225           gst_caps_unref (srccaps);
226
227           self->caps_sent = TRUE;
228           self->last_width = width;
229           self->last_height = height;
230           self->last_profile = profile;
231         }
232       }
233     }
234
235     return out;
236   }
237
238 done:
239   gst_rtp_buffer_unmap (&rtpbuffer);
240   return NULL;
241
242 too_small:
243   GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
244   gst_adapter_clear (self->adapter);
245   self->started = FALSE;
246
247   goto done;
248 }
249
250 static GstStateChangeReturn
251 gst_rtp_vp8_depay_change_state (GstElement * element, GstStateChange transition)
252 {
253   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (element);
254
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;
261       break;
262     default:
263       break;
264   }
265
266   return
267       GST_ELEMENT_CLASS (gst_rtp_vp8_depay_parent_class)->change_state (element,
268       transition);
269 }
270
271 static gboolean
272 gst_rtp_vp8_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
273 {
274   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
275
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;
281       break;
282     default:
283       break;
284   }
285
286   return
287       GST_RTP_BASE_DEPAYLOAD_CLASS
288       (gst_rtp_vp8_depay_parent_class)->handle_event (depay, event);
289 }
290
291 gboolean
292 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
293 {
294   return gst_element_register (plugin, "rtpvp8depay",
295       GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);
296 }