Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpvp9depay.c
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 "gstrtpelements.h"
27 #include "gstrtpvp9depay.h"
28 #include "gstrtputils.h"
29
30 #include <gst/video/video.h>
31
32 #include <stdio.h>
33
34 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp9_depay_debug);
35 #define GST_CAT_DEFAULT gst_rtp_vp9_depay_debug
36
37 static void gst_rtp_vp9_depay_dispose (GObject * object);
38 static GstBuffer *gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depayload,
39     GstRTPBuffer * rtp);
40 static GstStateChangeReturn gst_rtp_vp9_depay_change_state (GstElement *
41     element, GstStateChange transition);
42 static gboolean gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay,
43     GstEvent * event);
44 static gboolean gst_rtp_vp9_depay_packet_lost (GstRTPBaseDepayload * depay,
45     GstEvent * event);
46
47 G_DEFINE_TYPE (GstRtpVP9Depay, gst_rtp_vp9_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
48 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpvp9depay, "rtpvp9depay",
49     GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_DEPAY, rtp_element_init (plugin));
50
51 static GstStaticPadTemplate gst_rtp_vp9_depay_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("video/x-vp9"));
56
57 static GstStaticPadTemplate gst_rtp_vp9_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) { \"VP9\", \"VP9-DRAFT-IETF-01\" }"));
65
66 #define PICTURE_ID_NONE (UINT_MAX)
67 #define IS_PICTURE_ID_15BITS(pid) (((guint)(pid) & 0x8000) != 0)
68
69 static void
70 gst_rtp_vp9_depay_init (GstRtpVP9Depay * self)
71 {
72   self->adapter = gst_adapter_new ();
73   self->started = FALSE;
74   self->inter_picture = FALSE;
75 }
76
77 static void
78 gst_rtp_vp9_depay_class_init (GstRtpVP9DepayClass * gst_rtp_vp9_depay_class)
79 {
80   GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp9_depay_class);
81   GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp9_depay_class);
82   GstRTPBaseDepayloadClass *depay_class =
83       (GstRTPBaseDepayloadClass *) (gst_rtp_vp9_depay_class);
84
85
86   gst_element_class_add_static_pad_template (element_class,
87       &gst_rtp_vp9_depay_sink_template);
88   gst_element_class_add_static_pad_template (element_class,
89       &gst_rtp_vp9_depay_src_template);
90
91   gst_element_class_set_static_metadata (element_class, "RTP VP9 depayloader",
92       "Codec/Depayloader/Network/RTP",
93       "Extracts VP9 video from RTP packets)", "Stian Selnes <stian@pexip.com>");
94
95   object_class->dispose = gst_rtp_vp9_depay_dispose;
96
97   element_class->change_state = gst_rtp_vp9_depay_change_state;
98
99   depay_class->process_rtp_packet = gst_rtp_vp9_depay_process;
100   depay_class->handle_event = gst_rtp_vp9_depay_handle_event;
101   depay_class->packet_lost = gst_rtp_vp9_depay_packet_lost;
102
103   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp9_depay_debug, "rtpvp9depay", 0,
104       "VP9 Video RTP Depayloader");
105 }
106
107 static void
108 gst_rtp_vp9_depay_dispose (GObject * object)
109 {
110   GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (object);
111
112   if (self->adapter != NULL)
113     g_object_unref (self->adapter);
114   self->adapter = NULL;
115
116   /* release any references held by the object here */
117
118   if (G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose)
119     G_OBJECT_CLASS (gst_rtp_vp9_depay_parent_class)->dispose (object);
120 }
121
122 static gint
123 picture_id_compare (guint16 id0, guint16 id1)
124 {
125   guint shift = 16 - (IS_PICTURE_ID_15BITS (id1) ? 15 : 7);
126   id0 = id0 << shift;
127   id1 = id1 << shift;
128   return ((gint16) (id1 - id0)) >> shift;
129 }
130
131 static void
132 send_last_lost_event (GstRtpVP9Depay * self)
133 {
134   if (self->last_lost_event) {
135     GST_DEBUG_OBJECT (self,
136         "Sending the last stopped lost event: %" GST_PTR_FORMAT,
137         self->last_lost_event);
138     GST_RTP_BASE_DEPAYLOAD_CLASS (gst_rtp_vp9_depay_parent_class)
139         ->packet_lost (GST_RTP_BASE_DEPAYLOAD_CAST (self),
140         self->last_lost_event);
141     gst_event_replace (&self->last_lost_event, NULL);
142   }
143 }
144
145 static void
146 send_last_lost_event_if_needed (GstRtpVP9Depay * self, guint new_picture_id)
147 {
148   if (self->last_picture_id == PICTURE_ID_NONE ||
149       self->last_picture_id == new_picture_id)
150     return;
151
152   if (self->last_lost_event) {
153     gboolean send_lost_event = FALSE;
154     if (new_picture_id == PICTURE_ID_NONE) {
155       GST_DEBUG_OBJECT (self, "Dropping the last stopped lost event "
156           "(picture id does not exist): %" GST_PTR_FORMAT,
157           self->last_lost_event);
158     } else if (IS_PICTURE_ID_15BITS (self->last_picture_id) &&
159         !IS_PICTURE_ID_15BITS (new_picture_id)) {
160       GST_DEBUG_OBJECT (self, "Dropping the last stopped lost event "
161           "(picture id has less bits than before): %" GST_PTR_FORMAT,
162           self->last_lost_event);
163     } else if (picture_id_compare (self->last_picture_id, new_picture_id) != 1) {
164       GstStructure *s = gst_event_writable_structure (self->last_lost_event);
165
166       GST_DEBUG_OBJECT (self, "Sending the last stopped lost event "
167           "(gap in picture id %u %u): %" GST_PTR_FORMAT,
168           self->last_picture_id, new_picture_id, self->last_lost_event);
169       send_lost_event = TRUE;
170       /* Prevent rtpbasedepayload from dropping the event now
171        * that we have made sure the lost packet was not FEC */
172       gst_structure_remove_field (s, "might-have-been-fec");
173     }
174     if (send_lost_event)
175       GST_RTP_BASE_DEPAYLOAD_CLASS (gst_rtp_vp9_depay_parent_class)
176           ->packet_lost (GST_RTP_BASE_DEPAYLOAD_CAST (self),
177           self->last_lost_event);
178
179     gst_event_replace (&self->last_lost_event, NULL);
180   }
181 }
182
183 static GstBuffer *
184 gst_rtp_vp9_depay_process (GstRTPBaseDepayload * depay, GstRTPBuffer * rtp)
185 {
186   GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
187   GstBuffer *payload;
188   guint8 *data;
189   guint hdrsize = 1;
190   guint size;
191   gint spatial_layer = 0;
192   guint picture_id = PICTURE_ID_NONE;
193   gboolean i_bit, p_bit, l_bit, f_bit, b_bit, e_bit, v_bit, d_bit = 0;
194   gboolean is_start_of_picture;
195
196   if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (rtp->buffer))) {
197     GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
198     gst_adapter_clear (self->adapter);
199     self->started = FALSE;
200   }
201
202   size = gst_rtp_buffer_get_payload_len (rtp);
203
204   /* Mandatory with at least one header and one vp9 byte */
205   if (G_UNLIKELY (size < hdrsize + 1))
206     goto too_small;
207
208   data = gst_rtp_buffer_get_payload (rtp);
209   i_bit = (data[0] & 0x80) != 0;
210   p_bit = (data[0] & 0x40) != 0;
211   l_bit = (data[0] & 0x20) != 0;
212   f_bit = (data[0] & 0x10) != 0;
213   b_bit = (data[0] & 0x08) != 0;
214   e_bit = (data[0] & 0x04) != 0;
215   v_bit = (data[0] & 0x02) != 0;
216
217   GST_TRACE_OBJECT (self, "IPLFBEV : %d%d%d%d%d%d%d", i_bit, p_bit, l_bit,
218       f_bit, b_bit, e_bit, v_bit);
219
220   /* Check I optional header Picture ID */
221   if (i_bit) {
222     hdrsize++;
223     if (G_UNLIKELY (size < hdrsize + 1))
224       goto too_small;
225     picture_id = data[1];
226     /* Check M for 15 bits PictureID */
227     if ((data[1] & 0x80) != 0) {
228       hdrsize++;
229       if (G_UNLIKELY (size < hdrsize + 1))
230         goto too_small;
231       picture_id = (picture_id << 8) | data[2];
232     }
233   }
234
235   /* Check L optional header layer indices */
236   if (l_bit) {
237     spatial_layer = (data[hdrsize] >> 1) & 0x07;
238     d_bit = (data[hdrsize] >> 0) & 0x01;
239     GST_TRACE_OBJECT (self, "TID=%d, U=%d, SID=%d, D=%d",
240         (data[hdrsize] >> 5) & 0x07, (data[hdrsize] >> 4) & 0x01,
241         (data[hdrsize] >> 1) & 0x07, (data[hdrsize] >> 0) & 0x01);
242
243     if (spatial_layer == 0 && d_bit != 0) {
244       /* Invalid according to draft-ietf-payload-vp9-06, but firefox 61 and
245        * chrome 66 sends enchanment layers with SID=0, so let's not drop the
246        * packet. */
247       GST_LOG_OBJECT (self, "Invalid inter-layer dependency for base layer");
248     }
249
250     hdrsize++;
251     /* Check TL0PICIDX temporal layer zero index (non-flexible mode) */
252     if (!f_bit)
253       hdrsize++;
254   }
255
256   if (p_bit && f_bit) {
257     gint i;
258
259     /* At least one P_DIFF|N, up to three times */
260     for (i = 0; i < 3; i++) {
261       guint p_diff, n_bit;
262
263       if (G_UNLIKELY (size < hdrsize + 1))
264         goto too_small;
265
266       p_diff = data[hdrsize] >> 1;
267       n_bit = data[hdrsize] & 0x1;
268       GST_TRACE_OBJECT (self, "P_DIFF[%d]=%d", i, p_diff);
269       hdrsize++;
270
271       if (!n_bit)
272         break;
273     }
274   }
275
276   /* Check V optional Scalability Structure */
277   if (v_bit) {
278     guint n_s, y_bit, g_bit;
279     guint8 *ss = &data[hdrsize];
280     guint sssize = 1;
281
282     if (G_UNLIKELY (size < hdrsize + sssize + 1))
283       goto too_small;
284
285     n_s = (ss[0] & 0xe0) >> 5;
286     y_bit = (ss[0] & 0x10) != 0;
287     g_bit = (ss[0] & 0x08) != 0;
288
289     GST_TRACE_OBJECT (self, "SS header: N_S=%u, Y=%u, G=%u", n_s, y_bit, g_bit);
290
291     sssize += y_bit ? (n_s + 1) * 4 : 0;
292     if (G_UNLIKELY (size < hdrsize + sssize + 1))
293       goto too_small;
294
295     if (y_bit) {
296       guint i;
297       for (i = 0; i <= n_s; i++) {
298         /* For now, simply use the last layer specified for width and height */
299         self->ss_width = ss[1 + i * 4] * 256 + ss[2 + i * 4];
300         self->ss_height = ss[3 + i * 4] * 256 + ss[4 + i * 4];
301         GST_TRACE_OBJECT (self, "N_S[%d]: WIDTH=%u, HEIGHT=%u", i,
302             self->ss_width, self->ss_height);
303       }
304     }
305
306     if (g_bit) {
307       guint i, j;
308       guint n_g = ss[sssize];
309       sssize++;
310       if (G_UNLIKELY (size < hdrsize + sssize + 1))
311         goto too_small;
312       for (i = 0; i < n_g; i++) {
313         guint t = (ss[sssize] & 0xe0) >> 5;
314         guint u = (ss[sssize] & 0x10) >> 4;
315         guint r = (ss[sssize] & 0x0c) >> 2;
316         GST_TRACE_OBJECT (self, "N_G[%u]: 0x%02x -> T=%u, U=%u, R=%u", i,
317             ss[sssize], t, u, r);
318         for (j = 0; j < r; j++)
319           GST_TRACE_OBJECT (self, "  R[%u]: P_DIFF=%u", j, ss[sssize + 1 + j]);
320         sssize += 1 + r;
321         if (G_UNLIKELY (size < hdrsize + sssize + 1))
322           goto too_small;
323       }
324     }
325     hdrsize += sssize;
326   }
327
328   GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u, picture id 0x%x",
329       hdrsize, size, picture_id);
330
331   if (G_UNLIKELY (hdrsize >= size))
332     goto too_small;
333
334   is_start_of_picture = b_bit && (!l_bit || !d_bit);
335   /* If this is a start frame AND we are already processing a frame, we need to flush and wait for next start frame */
336   if (is_start_of_picture) {
337     if (G_UNLIKELY (self->started)) {
338       GST_DEBUG_OBJECT (depay, "Incomplete frame, flushing adapter");
339       gst_adapter_clear (self->adapter);
340       self->started = FALSE;
341     }
342   }
343
344   if (G_UNLIKELY (!self->started)) {
345     /* Check if this is the start of a VP9 layer frame, otherwise bail */
346     if (!b_bit) {
347       GST_DEBUG_OBJECT (depay,
348           "The layer is missing the first packets, ignoring the packet");
349       if (self->stop_lost_events) {
350         send_last_lost_event (self);
351         self->stop_lost_events = FALSE;
352       }
353       goto done;
354     }
355
356     GST_DEBUG_OBJECT (depay, "Found the start of the layer");
357     if (self->stop_lost_events) {
358       send_last_lost_event_if_needed (self, picture_id);
359       self->stop_lost_events = FALSE;
360     }
361     self->started = TRUE;
362     self->inter_picture = FALSE;
363   }
364
365   payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
366   if (GST_LEVEL_MEMDUMP <= gst_debug_category_get_threshold (GST_CAT_DEFAULT)) {
367     GstMapInfo map;
368     gst_buffer_map (payload, &map, GST_MAP_READ);
369     GST_MEMDUMP_OBJECT (self, "vp9 payload", map.data, 16);
370     gst_buffer_unmap (payload, &map);
371   }
372   gst_adapter_push (self->adapter, payload);
373   self->last_picture_id = picture_id;
374   self->inter_picture |= p_bit;
375
376   /* Marker indicates that it was the last rtp packet for this picture. Note
377    * that if spatial scalability is used, e_bit will be set for the last
378    * packet of a frame while the marker bit is not set until the last packet
379    * of the picture. */
380   if (gst_rtp_buffer_get_marker (rtp)) {
381     GstBuffer *out;
382
383     GST_DEBUG_OBJECT (depay,
384         "Found the end of the frame (%" G_GSIZE_FORMAT " bytes)",
385         gst_adapter_available (self->adapter));
386
387     if (gst_adapter_available (self->adapter) < 10)
388       goto too_small;
389
390     out = gst_adapter_take_buffer (self->adapter,
391         gst_adapter_available (self->adapter));
392
393     self->started = FALSE;
394
395     /* mark keyframes */
396     out = gst_buffer_make_writable (out);
397     /* Filter away all metas that are not sensible to copy */
398     gst_rtp_drop_non_video_meta (self, out);
399     if (self->inter_picture) {
400       GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
401
402       if (!self->caps_sent) {
403         gst_buffer_unref (out);
404         out = NULL;
405         GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
406         gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
407             gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
408                 TRUE, 0));
409       }
410     } else {
411       GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
412
413       if (self->last_width != self->ss_width ||
414           self->last_height != self->ss_height) {
415         GstCaps *srccaps;
416
417         /* Width and height are optional in the RTP header. Consider to parse
418          * the frame header in addition if missing from RTP header */
419         if (self->ss_width != 0 && self->ss_height != 0) {
420           srccaps = gst_caps_new_simple ("video/x-vp9",
421               "framerate", GST_TYPE_FRACTION, 0, 1,
422               "width", G_TYPE_INT, self->ss_width,
423               "height", G_TYPE_INT, self->ss_height, NULL);
424         } else {
425           srccaps = gst_caps_new_simple ("video/x-vp9",
426               "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
427         }
428
429         gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
430         gst_caps_unref (srccaps);
431
432         self->caps_sent = TRUE;
433         self->last_width = self->ss_width;
434         self->last_height = self->ss_height;
435         self->ss_width = 0;
436         self->ss_height = 0;
437       }
438     }
439
440     if (picture_id != PICTURE_ID_NONE)
441       self->stop_lost_events = TRUE;
442     return out;
443   }
444
445 done:
446   return NULL;
447
448 too_small:
449   GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
450   gst_adapter_clear (self->adapter);
451   self->started = FALSE;
452   goto done;
453 }
454
455 static GstStateChangeReturn
456 gst_rtp_vp9_depay_change_state (GstElement * element, GstStateChange transition)
457 {
458   GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (element);
459
460   switch (transition) {
461     case GST_STATE_CHANGE_READY_TO_PAUSED:
462       self->last_width = -1;
463       self->last_height = -1;
464       self->caps_sent = FALSE;
465       self->last_picture_id = PICTURE_ID_NONE;
466       gst_event_replace (&self->last_lost_event, NULL);
467       self->stop_lost_events = FALSE;
468       break;
469     default:
470       break;
471   }
472
473   return
474       GST_ELEMENT_CLASS (gst_rtp_vp9_depay_parent_class)->change_state (element,
475       transition);
476 }
477
478 static gboolean
479 gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
480 {
481   GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
482
483   switch (GST_EVENT_TYPE (event)) {
484     case GST_EVENT_FLUSH_STOP:
485       self->last_width = -1;
486       self->last_height = -1;
487       self->last_picture_id = PICTURE_ID_NONE;
488       gst_event_replace (&self->last_lost_event, NULL);
489       self->stop_lost_events = FALSE;
490       break;
491     default:
492       break;
493   }
494
495   return
496       GST_RTP_BASE_DEPAYLOAD_CLASS
497       (gst_rtp_vp9_depay_parent_class)->handle_event (depay, event);
498 }
499
500 static gboolean
501 gst_rtp_vp9_depay_packet_lost (GstRTPBaseDepayload * depay, GstEvent * event)
502 {
503   GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
504   const GstStructure *s;
505   gboolean might_have_been_fec;
506
507   s = gst_event_get_structure (event);
508
509   if (self->stop_lost_events) {
510     if (gst_structure_get_boolean (s, "might-have-been-fec",
511             &might_have_been_fec)
512         && might_have_been_fec) {
513       GST_DEBUG_OBJECT (depay, "Stopping lost event %" GST_PTR_FORMAT, event);
514       gst_event_replace (&self->last_lost_event, event);
515       return TRUE;
516     }
517   } else if (self->last_picture_id != PICTURE_ID_NONE) {
518     GstStructure *s = gst_event_writable_structure (self->last_lost_event);
519
520     /* We are currently processing a picture, let's make sure the
521      * base depayloader doesn't drop this lost event */
522     gst_structure_remove_field (s, "might-have-been-fec");
523   }
524
525   return
526       GST_RTP_BASE_DEPAYLOAD_CLASS
527       (gst_rtp_vp9_depay_parent_class)->packet_lost (depay, event);
528 }