1 /* gstrtpvp9depay.c - Source for GstRtpVP9Depay
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 * Copyright (C) 2015 Stian Selnes <stian@pexip.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include "gstrtpvp9depay.h"
27 #include "gstrtputils.h"
28
29 #include <gst/video/video.h>
30
31 #include <stdio.h>
32
33 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_depay_debug);
34 #define GST_CAT_DEFAULT gst_rtp_vp9_depay_debug
35
36 static void gst_rtp_vp9_depay_dispose (GObject * object);
37 static GstBuffer *gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depayload,
38 GstRTPBuffer * rtp);
39 static GstStateChangeReturn gst_rtp_vp9_depay_change_state (GstElement *
40 element, GstStateChange transition);
41 static gboolean gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay,
42 GstEvent * event);
43
44 G_DEFINE_TYPE (GstRtpVP9Depay, gst_rtp_vp9_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
45
46 static GstStaticPadTemplate gst_rtp_vp9_depay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_PAD_SRC,
49 GST_PAD_ALWAYS,
50 GST_STATIC_CAPS ("video/x-vp9"));
51
52 static GstStaticPadTemplate gst_rtp_vp9_depay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
54 GST_PAD_SINK,
55 GST_PAD_ALWAYS,
56 GST_STATIC_CAPS ("application/x-rtp, "
57 "clock-rate = (int) 90000,"
58 "media = (string) \"video\","
59 "encoding-name = (string) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
60
61 static void
gst_rtp_vp9_depay_init(GstRtpVP9Depay * self)62 gst_rtp_vp9_depay_init (GstRtpVP9Depay * self)
63 {
64 self->adapter = gst_adapter_new ();
65 self->started = FALSE;
66 }
67
68 static void
gst_rtp_vp9_depay_class_init(GstRtpVP9DepayClass * gst_rtp_vp9_depay_class)69 gst_rtp_vp9_depay_class_init (GstRtpVP9DepayClass * gst_rtp_vp9_depay_class)
70 {
71 GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp9_depay_class);
72 GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_depay_class);
73 GstRTPBaseDepayloadClass *depay_class =
74 (GstRTPBaseDepayloadClass *) (gst_rtp_vp9_depay_class);
75
76
77 gst_element_class_add_static_pad_template (element_class,
78 &gst_rtp_vp9_depay_sink_template);
79 gst_element_class_add_static_pad_template (element_class,
80 &gst_rtp_vp9_depay_src_template);
81
82 gst_element_class_set_static_metadata (element_class, "RTP VP9 depayloader",
83 "Codec/Depayloader/Network/RTP",
84 "Extracts VP9 video from RTP packets)", "Stian Selnes <stian@pexip.com>");
85
86 object_class->dispose = gst_rtp_vp9_depay_dispose;
87
88 element_class->change_state = gst_rtp_vp9_depay_change_state;
89
90 depay_class->process_rtp_packet = gst_rtp_vp9_depay_process;
91 depay_class->handle_event = gst_rtp_vp9_depay_handle_event;
92
93 GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_depay_debug, "rtpvp9depay", 0,
94 "VP9 Video RTP Depayloader");
95 }
96
97 static void
gst_rtp_vp9_depay_dispose(GObject * object)98 gst_rtp_vp9_depay_dispose (GObject * object)
99 {
100 GstRtpVP9Depay *self = GST_RTP_VP9_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_vp9_depay_parent_class)->dispose)
109 G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose (object);
110 }
111
112 static GstBuffer *
gst_rtp_vp9_depay_process(GstRTPBaseDepayload * depay,GstRTPBuffer * rtp)113 gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
114 {
115 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
116 GstBuffer *payload;
117 guint8 *data;
118 guint hdrsize = 1;
119 guint size;
120 gint spatial_layer = 0;
121 gboolean i_bit, p_bit, l_bit, f_bit, b_bit, e_bit, v_bit;
122
123 if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
124 GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
125 gst_adapter_clear (self->adapter);
126 self->started = FALSE;
127 }
128
129 size = gst_rtp_buffer_get_payload_len (rtp);
130
131 /* Mandatory with at least one header and one vp9 byte */
132 if (G_UNLIKELY (size < hdrsize + 1))
133 goto too_small;
134
135 data = gst_rtp_buffer_get_payload (rtp);
136 i_bit = (data[0] & 0x80) != 0;
137 p_bit = (data[0] & 0x40) != 0;
138 l_bit = (data[0] & 0x20) != 0;
139 f_bit = (data[0] & 0x10) != 0;
140 b_bit = (data[0] & 0x08) != 0;
141 e_bit = (data[0] & 0x04) != 0;
142 v_bit = (data[0] & 0x02) != 0;
143
144 if (G_UNLIKELY (!self->started)) {
145 /* Check if this is the start of a VP9 layer frame, otherwise bail */
146 if (!b_bit)
147 goto done;
148
149 self->started = TRUE;
150 }
151
152 GST_TRACE_OBJECT (self, "IPLFBEV : %d%d%d%d%d%d%d", i_bit, p_bit, l_bit,
153 f_bit, b_bit, e_bit, v_bit);
154
155 /* Check I optional header Picture ID */
156 if (i_bit) {
157 hdrsize++;
158 if (G_UNLIKELY (size < hdrsize + 1))
159 goto too_small;
160 /* Check M for 15 bits PictureID */
161 if ((data[1] & 0x80) != 0) {
162 hdrsize++;
163 if (G_UNLIKELY (size < hdrsize + 1))
164 goto too_small;
165 }
166 }
167
168 /* Check L optional header layer indices */
169 if (l_bit) {
170 hdrsize++;
171 /* Check TL0PICIDX temporal layer zero index (non-flexible mode) */
172 if (!f_bit)
173 hdrsize++;
174 }
175
176 if (p_bit && f_bit) {
177 gint i;
178
179 /* At least one P_DIFF|N, up to three times */
180 for (i = 0; i < 3; i++) {
181 guint p_diff, n_bit;
182
183 if (G_UNLIKELY (size < hdrsize + 1))
184 goto too_small;
185
186 p_diff = data[hdrsize] >> 1;
187 n_bit = data[hdrsize] & 0x1;
188 GST_TRACE_OBJECT (self, "P_DIFF[%d]=%d", i, p_diff);
189 hdrsize++;
190
191 if (!n_bit)
192 break;
193 }
194 }
195
196 /* Check V optional Scalability Structure */
197 if (v_bit) {
198 guint n_s, y_bit, g_bit;
199 guint8 *ss = &data[hdrsize];
200 guint sssize = 1;
201
202 if (G_UNLIKELY (size < hdrsize + sssize + 1))
203 goto too_small;
204
205 n_s = (ss[0] & 0xe0) >> 5;
206 y_bit = (ss[0] & 0x10) != 0;
207 g_bit = (ss[0] & 0x08) != 0;
208
209 GST_TRACE_OBJECT (self, "SS header: N_S=%u, Y=%u, G=%u", n_s, y_bit, g_bit);
210
211 sssize += y_bit ? (n_s + 1) * 4 : 0;
212 if (G_UNLIKELY (size < hdrsize + sssize + 1))
213 goto too_small;
214
215 if (y_bit) {
216 guint i;
217 for (i = 0; i <= n_s; i++) {
218 /* For now, simply use the last layer specified for width and height */
219 self->ss_width = ss[1 + i * 4] * 256 + ss[2 + i * 4];
220 self->ss_height = ss[3 + i * 4] * 256 + ss[4 + i * 4];
221 GST_TRACE_OBJECT (self, "N_S[%d]: WIDTH=%u, HEIGHT=%u", i,
222 self->ss_width, self->ss_height);
223 }
224 }
225
226 if (g_bit) {
227 guint i, j;
228 guint n_g = ss[sssize];
229 sssize++;
230 if (G_UNLIKELY (size < hdrsize + sssize + 1))
231 goto too_small;
232 for (i = 0; i < n_g; i++) {
233 guint t = (ss[sssize] & 0xe0) >> 5;
234 guint u = (ss[sssize] & 0x10) >> 4;
235 guint r = (ss[sssize] & 0x0c) >> 2;
236 GST_TRACE_OBJECT (self, "N_G[%u]: 0x%02x -> T=%u, U=%u, R=%u", i,
237 ss[sssize], t, u, r);
238 for (j = 0; j < r; j++)
239 GST_TRACE_OBJECT (self, " R[%u]: P_DIFF=%u", j, ss[sssize + 1 + j]);
240 sssize += 1 + r;
241 if (G_UNLIKELY (size < hdrsize + sssize + 1))
242 goto too_small;
243 }
244 }
245 hdrsize += sssize;
246 }
247
248 GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
249
250 if (G_UNLIKELY (hdrsize >= size))
251 goto too_small;
252
253 payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
254 {
255 GstMapInfo map;
256 gst_buffer_map (payload, &map, GST_MAP_READ);
257 GST_MEMDUMP_OBJECT (self, "vp9 payload", map.data, 16);
258 gst_buffer_unmap (payload, &map);
259 }
260 gst_adapter_push (self->adapter, payload);
261
262 /* Marker indicates that it was the last rtp packet for this frame */
263 if (gst_rtp_buffer_get_marker (rtp)) {
264 GstBuffer *out;
265 gboolean key_frame_first_layer = !p_bit && spatial_layer == 0;
266
267 if (gst_adapter_available (self->adapter) < 10)
268 goto too_small;
269
270 out = gst_adapter_take_buffer (self->adapter,
271 gst_adapter_available (self->adapter));
272
273 self->started = FALSE;
274
275 /* mark keyframes */
276 out = gst_buffer_make_writable (out);
277 /* Filter away all metas that are not sensible to copy */
278 gst_rtp_drop_non_video_meta (self, out);
279 if (!key_frame_first_layer) {
280 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
281
282 if (!self->caps_sent) {
283 gst_buffer_unref (out);
284 out = NULL;
285 GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
286 gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
287 gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
288 TRUE, 0));
289 }
290 } else {
291 GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
292
293 if (self->last_width != self->ss_width ||
294 self->last_height != self->ss_height) {
295 GstCaps *srccaps;
296
297 /* Width and height are optional in the RTP header. Consider to parse
298 * the frame header in addition if missing from RTP header */
299 if (self->ss_width != 0 && self->ss_height != 0) {
300 srccaps = gst_caps_new_simple ("video/x-vp9",
301 "framerate", GST_TYPE_FRACTION, 0, 1,
302 "width", G_TYPE_INT, self->ss_width,
303 "height", G_TYPE_INT, self->ss_height, NULL);
304 } else {
305 srccaps = gst_caps_new_simple ("video/x-vp9",
306 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
307 }
308
309 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
310 gst_caps_unref (srccaps);
311
312 self->caps_sent = TRUE;
313 self->last_width = self->ss_width;
314 self->last_height = self->ss_height;
315 self->ss_width = 0;
316 self->ss_height = 0;
317 }
318 }
319
320 return out;
321 }
322
323 done:
324 return NULL;
325
326 too_small:
327 GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
328 gst_adapter_clear (self->adapter);
329 self->started = FALSE;
330
331 goto done;
332 }
333
334 static GstStateChangeReturn
gst_rtp_vp9_depay_change_state(GstElement * element,GstStateChange transition)335 gst_rtp_vp9_depay_change_state (GstElement * element, GstStateChange transition)
336 {
337 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (element);
338
339 switch (transition) {
340 case GST_STATE_CHANGE_READY_TO_PAUSED:
341 self->last_width = -1;
342 self->last_height = -1;
343 self->caps_sent = FALSE;
344 break;
345 default:
346 break;
347 }
348
349 return
350 GST_ELEMENT_CLASS (gst_rtp_vp9_depay_parent_class)->change_state (element,
351 transition);
352 }
353
354 static gboolean
gst_rtp_vp9_depay_handle_event(GstRTPBaseDepayload * depay,GstEvent * event)355 gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
356 {
357 GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
358
359 switch (GST_EVENT_TYPE (event)) {
360 case GST_EVENT_FLUSH_STOP:
361 self->last_width = -1;
362 self->last_height = -1;
363 break;
364 default:
365 break;
366 }
367
368 return
369 GST_RTP_BASE_DEPAYLOAD_CLASS
370 (gst_rtp_vp9_depay_parent_class)->handle_event (depay, event);
371 }
372
373 gboolean
gst_rtp_vp9_depay_plugin_init(GstPlugin * plugin)374 gst_rtp_vp9_depay_plugin_init (GstPlugin * plugin)
375 {
376 return gst_element_register (plugin, "rtpvp9depay",
377 GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_DEPAY);
378 }
379