rtpvrawpay: Add missing break
[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 GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_depay_debug);
28 #define GST_CAT_DEFAULT gst_rtp_vp8_depay_debug
29
30 static void gst_rtp_vp8_depay_dispose (GObject * object);
31 static GstBuffer *gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depayload,
32     GstBuffer * buf);
33 static gboolean gst_rtp_vp8_depay_set_caps (GstRTPBaseDepayload * depayload,
34     GstCaps * caps);
35
36 G_DEFINE_TYPE (GstRtpVP8Depay, gst_rtp_vp8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
37
38 static GstStaticPadTemplate gst_rtp_vp8_depay_src_template =
39 GST_STATIC_PAD_TEMPLATE ("src",
40     GST_PAD_SRC,
41     GST_PAD_ALWAYS,
42     GST_STATIC_CAPS ("video/x-vp8"));
43
44 static GstStaticPadTemplate gst_rtp_vp8_depay_sink_template =
45 GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "clock-rate = (int) 90000,"
50         "media = (string) \"video\","
51         "encoding-name = (string) \"VP8-DRAFT-IETF-01\""));
52
53 static void
54 gst_rtp_vp8_depay_init (GstRtpVP8Depay * self)
55 {
56   self->adapter = gst_adapter_new ();
57   self->started = FALSE;
58 }
59
60 static void
61 gst_rtp_vp8_depay_class_init (GstRtpVP8DepayClass * gst_rtp_vp8_depay_class)
62 {
63   GObjectClass *object_class = G_OBJECT_CLASS (gst_rtp_vp8_depay_class);
64   GstElementClass *element_class = GST_ELEMENT_CLASS (gst_rtp_vp8_depay_class);
65   GstRTPBaseDepayloadClass *depay_class =
66       (GstRTPBaseDepayloadClass *) (gst_rtp_vp8_depay_class);
67
68
69   gst_element_class_add_pad_template (element_class,
70       gst_static_pad_template_get (&gst_rtp_vp8_depay_sink_template));
71   gst_element_class_add_pad_template (element_class,
72       gst_static_pad_template_get (&gst_rtp_vp8_depay_src_template));
73
74   gst_element_class_set_static_metadata (element_class, "RTP VP8 depayloader",
75       "Codec/Depayloader/Network/RTP",
76       "Extracts VP8 video from RTP packets)",
77       "Sjoerd Simons <sjoerd@luon.net>");
78
79   object_class->dispose = gst_rtp_vp8_depay_dispose;
80
81   depay_class->process = gst_rtp_vp8_depay_process;
82   depay_class->set_caps = gst_rtp_vp8_depay_set_caps;
83
84   GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_depay_debug, "rtpvp8depay", 0,
85       "VP8 Video RTP Depayloader");
86 }
87
88 static void
89 gst_rtp_vp8_depay_dispose (GObject * object)
90 {
91   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (object);
92
93   if (self->adapter != NULL)
94     g_object_unref (self->adapter);
95   self->adapter = NULL;
96
97   /* release any references held by the object here */
98
99   if (G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose)
100     G_OBJECT_CLASS (gst_rtp_vp8_depay_parent_class)->dispose (object);
101 }
102
103 static GstBuffer *
104 gst_rtp_vp8_depay_process (GstRTPBaseDepayload * depay, GstBuffer * buf)
105 {
106   GstRtpVP8Depay *self = GST_RTP_VP8_DEPAY (depay);
107   GstBuffer *payload;
108   guint8 *data;
109   guint offset;
110   guint size;
111   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
112
113   if (G_UNLIKELY (GST_BUFFER_IS_DISCONT (buf))) {
114     GST_LOG_OBJECT (self, "Discontinuity, flushing adapter");
115     gst_adapter_clear (self->adapter);
116     self->started = FALSE;
117   }
118
119   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuffer);
120   size = gst_rtp_buffer_get_payload_len (&rtpbuffer);
121
122   /* At least one header and one vp8 byte */
123   if (G_UNLIKELY (size < 2))
124     goto too_small;
125
126   data = gst_rtp_buffer_get_payload (&rtpbuffer);
127
128   if (G_UNLIKELY (!self->started)) {
129     /* Check if this is the start of a VP8 frame, otherwise bail */
130     /* S=1 and PartID= 0 */
131     if ((data[0] & 0x1F) != 0x10)
132       goto done;
133
134     self->started = TRUE;
135   }
136
137   offset = 1;
138   /* Check X optional header */
139   if ((data[0] & 0x80) != 0) {
140     offset++;
141     /* Check I optional header */
142     if ((data[1] & 0x80) != 0) {
143       offset++;
144       if (G_UNLIKELY (offset + 2 >= size))
145         goto too_small;
146       /* Check for 16 bits PictureID */
147       if ((data[2] & 0x80) != 0)
148         offset++;
149     }
150     /* Check L optional header */
151     if ((data[1] & 0x40) != 0)
152       offset++;
153     /* Check T or K optional headers */
154     if ((data[1] & 0x20) != 0 || (data[1] & 0x10) != 0)
155       offset++;
156   }
157
158   if (G_UNLIKELY (offset >= size))
159     goto too_small;
160
161   payload = gst_rtp_buffer_get_payload_subbuffer (&rtpbuffer, offset, -1);
162   gst_adapter_push (self->adapter, payload);
163
164   /* Marker indicates that it was the last rtp packet for this frame */
165   if (gst_rtp_buffer_get_marker (&rtpbuffer)) {
166     GstBuffer *out;
167     guint8 flag0;
168
169     gst_adapter_copy (self->adapter, &flag0, 0, 1);
170
171     out = gst_adapter_take_buffer (self->adapter,
172         gst_adapter_available (self->adapter));
173
174     self->started = FALSE;
175     gst_rtp_buffer_unmap (&rtpbuffer);
176
177     /* mark keyframes */
178     out = gst_buffer_make_writable (out);
179     if ((flag0 & 0x01))
180       GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
181     else
182       GST_BUFFER_FLAG_UNSET (out, GST_BUFFER_FLAG_DELTA_UNIT);
183
184     return out;
185   }
186
187 done:
188   gst_rtp_buffer_unmap (&rtpbuffer);
189   return NULL;
190
191 too_small:
192   GST_LOG_OBJECT (self, "Invalid rtp packet (too small), ignoring");
193   gst_adapter_clear (self->adapter);
194   self->started = FALSE;
195
196   goto done;
197 }
198
199 static gboolean
200 gst_rtp_vp8_depay_set_caps (GstRTPBaseDepayload * depayload, GstCaps * caps)
201 {
202   GstCaps *srccaps = gst_caps_new_simple ("video/x-vp8",
203       "framerate", GST_TYPE_FRACTION, 0, 1,
204       NULL);
205
206   gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
207   gst_caps_unref (srccaps);
208
209   return TRUE;
210 }
211
212 gboolean
213 gst_rtp_vp8_depay_plugin_init (GstPlugin * plugin)
214 {
215   return gst_element_register (plugin, "rtpvp8depay",
216       GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_DEPAY);
217 }