rtpvorbisdepay: remove dead code
[platform/upstream/gst-plugins-good.git] / 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 "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
62 gst_rtp_vp9_depay_init (GstRtpVP9Depay * self)
63 {
64   self->adapter = gst_adapter_new ();
65   self->started = FALSE;
66 }
67
68 static void
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
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 *
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   /* flexible-mode not implemented */
169   g_assert (!f_bit);
170
171   /* Check L optional header layer indices */
172   if (l_bit) {
173     hdrsize++;
174     /* Check TL0PICIDX temporal layer zero index (non-flexible mode) */
175     if (!f_bit)
176       hdrsize++;
177   }
178
179   /* Check V optional Scalability Structure */
180   if (v_bit) {
181     guint n_s, y_bit, g_bit;
182     guint8 *ss = &data[hdrsize];
183     guint sssize = 1;
184
185     if (G_UNLIKELY (size < hdrsize + sssize + 1))
186       goto too_small;
187
188     n_s = (ss[0] & 0xe0) >> 5;
189     y_bit = (ss[0] & 0x10) != 0;
190     g_bit = (ss[0] & 0x08) != 0;
191
192     GST_TRACE_OBJECT (self, "SS header: N_S=%u, Y=%u, G=%u", n_s, y_bit, g_bit);
193
194     sssize += y_bit ? (n_s + 1) * 4 : 0;
195     if (G_UNLIKELY (size < hdrsize + sssize + 1))
196       goto too_small;
197
198     if (y_bit) {
199       guint i;
200       for (i = 0; i <= n_s; i++) {
201         /* For now, simply use the last layer specified for width and height */
202         self->ss_width = ss[1 + i * 4] * 256 + ss[2 + i * 4];
203         self->ss_height = ss[3 + i * 4] * 256 + ss[4 + i * 4];
204         GST_TRACE_OBJECT (self, "N_S[%d]: WIDTH=%u, HEIGHT=%u", i,
205             self->ss_width, self->ss_height);
206       }
207     }
208
209     if (g_bit) {
210       guint i, j;
211       guint n_g = ss[sssize];
212       sssize++;
213       if (G_UNLIKELY (size < hdrsize + sssize + 1))
214         goto too_small;
215       for (i = 0; i < n_g; i++) {
216         guint t = (ss[sssize] & 0xe0) >> 5;
217         guint u = (ss[sssize] & 0x10) >> 4;
218         guint r = (ss[sssize] & 0x0c) >> 2;
219         GST_TRACE_OBJECT (self, "N_G[%u]: 0x%02x -> T=%u, U=%u, R=%u", i,
220             ss[sssize], t, u, r);
221         for (j = 0; j < r; j++)
222           GST_TRACE_OBJECT (self, "  R[%u]: P_DIFF=%u", j, ss[sssize + 1 + j]);
223         sssize += 1 + r;
224         if (G_UNLIKELY (size < hdrsize + sssize + 1))
225           goto too_small;
226       }
227     }
228     hdrsize += sssize;
229   }
230
231   GST_DEBUG_OBJECT (depay, "hdrsize %u, size %u", hdrsize, size);
232
233   if (G_UNLIKELY (hdrsize >= size))
234     goto too_small;
235
236   payload = gst_rtp_buffer_get_payload_subbuffer (rtp, hdrsize, -1);
237   {
238     GstMapInfo map;
239     gst_buffer_map (payload, &map, GST_MAP_READ);
240     GST_MEMDUMP_OBJECT (self, "vp9 payload", map.data, 16);
241     gst_buffer_unmap (payload, &map);
242   }
243   gst_adapter_push (self->adapter, payload);
244
245   /* Marker indicates that it was the last rtp packet for this frame */
246   if (gst_rtp_buffer_get_marker (rtp)) {
247     GstBuffer *out;
248     gboolean key_frame_first_layer = !p_bit && spatial_layer == 0;
249
250
251     if (gst_adapter_available (self->adapter) < 10)
252       goto too_small;
253
254     out = gst_adapter_take_buffer (self->adapter,
255         gst_adapter_available (self->adapter));
256
257     self->started = FALSE;
258
259     /* mark keyframes */
260     out = gst_buffer_make_writable (out);
261     /* Filter away all metas that are not sensible to copy */
262     gst_rtp_drop_meta (GST_ELEMENT_CAST (self), out,
263         g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
264     if (!key_frame_first_layer) {
265       GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
266
267       if (!self->caps_sent) {
268         gst_buffer_unref (out);
269         out = NULL;
270         GST_INFO_OBJECT (self, "Dropping inter-frame before intra-frame");
271         gst_pad_push_event (GST_RTP_BASE_DEPAYLOAD_SINKPAD (depay),
272             gst_video_event_new_upstream_force_key_unit (GST_CLOCK_TIME_NONE,
273                 TRUE, 0));
274       }
275     } else {
276       GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
277
278       if (self->last_width != self->ss_width ||
279           self->last_height != self->ss_height) {
280         GstCaps *srccaps;
281
282         /* Width and height are optional in the RTP header. Consider to parse
283          * the frame header in addition if missing from RTP header */
284         if (self->ss_width != 0 && self->ss_height != 0) {
285           srccaps = gst_caps_new_simple ("video/x-vp9",
286               "framerate", GST_TYPE_FRACTION, 0, 1,
287               "width", G_TYPE_INT, self->ss_width,
288               "height", G_TYPE_INT, self->ss_height, NULL);
289         } else {
290           srccaps = gst_caps_new_simple ("video/x-vp9",
291               "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
292         }
293
294         gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), srccaps);
295         gst_caps_unref (srccaps);
296
297         self->caps_sent = TRUE;
298         self->last_width = self->ss_width;
299         self->last_height = self->ss_height;
300         self->ss_width = 0;
301         self->ss_height = 0;
302       }
303     }
304
305     return out;
306   }
307
308 done:
309   return NULL;
310
311 too_small:
312   GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
313   gst_adapter_clear (self->adapter);
314   self->started = FALSE;
315
316   goto done;
317 }
318
319 static GstStateChangeReturn
320 gst_rtp_vp9_depay_change_state (GstElement * element, GstStateChange transition)
321 {
322   GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (element);
323
324   switch (transition) {
325     case GST_STATE_CHANGE_READY_TO_PAUSED:
326       self->last_width = -1;
327       self->last_height = -1;
328       self->caps_sent = FALSE;
329       break;
330     default:
331       break;
332   }
333
334   return
335       GST_ELEMENT_CLASS (gst_rtp_vp9_depay_parent_class)->change_state (element,
336       transition);
337 }
338
339 static gboolean
340 gst_rtp_vp9_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
341 {
342   GstRtpVP9Depay *self = GST_RTP_VP9_DEPAY (depay);
343
344   switch (GST_EVENT_TYPE (event)) {
345     case GST_EVENT_FLUSH_STOP:
346       self->last_width = -1;
347       self->last_height = -1;
348       break;
349     default:
350       break;
351   }
352
353   return
354       GST_RTP_BASE_DEPAYLOAD_CLASS
355       (gst_rtp_vp9_depay_parent_class)->handle_event (depay, event);
356 }
357
358 gboolean
359 gst_rtp_vp9_depay_plugin_init (GstPlugin * plugin)
360 {
361   return gst_element_register (plugin, "rtpvp9depay",
362       GST_RANK_MARGINAL, GST_TYPE_RTP_VP9_DEPAY);
363 }