gst/rtp/: Use more efficient adapter and rtpbuffer methods when possible.
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4gdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <gst/rtp/gstrtpbuffer.h>
25
26 #include <string.h>
27 #include "gstrtpmp4gdepay.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
30 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
31
32 /* elementfactory information */
33 static const GstElementDetails gst_rtp_mp4gdepay_details =
34 GST_ELEMENT_DETAILS ("RTP packet depayloader",
35     "Codec/Depayloader/Network",
36     "Extracts MPEG4 elementary streams from RTP packets (RFC 3640)",
37     "Wim Taymans <wim@fluendo.com>");
38
39 /* RtpMP4GDepay signals and args */
40 enum
41 {
42   /* FILL ME */
43   LAST_SIGNAL
44 };
45
46 enum
47 {
48   ARG_0,
49 };
50
51 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
52     GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("video/mpeg,"
56         "mpegversion=(int) 4,"
57         "systemstream=(boolean)false;" "audio/mpeg," "mpegversion=(int) 4")
58     );
59
60 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("application/x-rtp, "
65         "media = (string) { \"video\", \"audio\", \"application\" }, "
66         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
67         "clock-rate = (int) [1, MAX ], "
68         "encoding-name = (string) \"MPEG4-GENERIC\", "
69         /* required string params */
70         "streamtype = (string) { \"4\", \"5\" }, "      /* 4 = video, 5 = audio */
71         /* "profile-level-id = (string) [1,MAX], " */
72         /* "config = (string) [1,MAX]" */
73         "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\" } "
74         /* Optional general parameters */
75         /* "objecttype = (string) [1,MAX], " */
76         /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
77         /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
78         /* "maxdisplacement = (string) [1,MAX], " */
79         /* "de-interleavebuffersize = (string) [1,MAX], " */
80         /* Optional configuration parameters */
81         /* "sizelength = (string) [1, 16], " *//* max 16 bits, should be enough... */
82         /* "indexlength = (string) [1, 8], " */
83         /* "indexdeltalength = (string) [1, 8], " */
84         /* "ctsdeltalength = (string) [1, 64], " */
85         /* "dtsdeltalength = (string) [1, 64], " */
86         /* "randomaccessindication = (string) {0, 1}, " */
87         /* "streamstateindication = (string) [0, 64], " */
88         /* "auxiliarydatasizelength = (string) [0, 64]" */ )
89     );
90
91 GST_BOILERPLATE (GstRtpMP4GDepay, gst_rtp_mp4g_depay, GstBaseRTPDepayload,
92     GST_TYPE_BASE_RTP_DEPAYLOAD);
93
94 static void gst_rtp_mp4g_depay_finalize (GObject * object);
95
96 static gboolean gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload,
97     GstCaps * caps);
98 static GstBuffer *gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload,
99     GstBuffer * buf);
100
101 static void gst_rtp_mp4g_depay_set_property (GObject * object, guint prop_id,
102     const GValue * value, GParamSpec * pspec);
103 static void gst_rtp_mp4g_depay_get_property (GObject * object, guint prop_id,
104     GValue * value, GParamSpec * pspec);
105
106 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
107     element, GstStateChange transition);
108
109
110 static void
111 gst_rtp_mp4g_depay_base_init (gpointer klass)
112 {
113   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
114
115   gst_element_class_add_pad_template (element_class,
116       gst_static_pad_template_get (&gst_rtp_mp4g_depay_src_template));
117   gst_element_class_add_pad_template (element_class,
118       gst_static_pad_template_get (&gst_rtp_mp4g_depay_sink_template));
119
120   gst_element_class_set_details (element_class, &gst_rtp_mp4gdepay_details);
121 }
122
123 static void
124 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
125 {
126   GObjectClass *gobject_class;
127   GstElementClass *gstelement_class;
128   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
129
130   gobject_class = (GObjectClass *) klass;
131   gstelement_class = (GstElementClass *) klass;
132   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
133
134   parent_class = g_type_class_peek_parent (klass);
135
136   gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
137   gobject_class->set_property = gst_rtp_mp4g_depay_set_property;
138   gobject_class->get_property = gst_rtp_mp4g_depay_get_property;
139
140   gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
141
142   gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
143   gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
144
145   GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
146       "MP4-generic RTP Depayloader");
147 }
148
149 static void
150 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay,
151     GstRtpMP4GDepayClass * klass)
152 {
153   rtpmp4gdepay->adapter = gst_adapter_new ();
154 }
155
156 static void
157 gst_rtp_mp4g_depay_finalize (GObject * object)
158 {
159   GstRtpMP4GDepay *rtpmp4gdepay;
160
161   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
162
163   g_object_unref (rtpmp4gdepay->adapter);
164   rtpmp4gdepay->adapter = NULL;
165
166   G_OBJECT_CLASS (parent_class)->finalize (object);
167 }
168
169 static gint
170 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
171     gint def)
172 {
173   const gchar *str;
174   gint res;
175
176   if ((str = gst_structure_get_string (structure, field)))
177     return atoi (str);
178
179   if (gst_structure_get_int (structure, field, &res))
180     return res;
181
182   return def;
183 }
184
185 static gboolean
186 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
187 {
188
189   GstStructure *structure;
190   GstRtpMP4GDepay *rtpmp4gdepay;
191   GstCaps *srccaps = NULL;
192   const gchar *str;
193   gint clock_rate = 90000;      /* default */
194   gint someint;
195
196   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
197
198   structure = gst_caps_get_structure (caps, 0);
199
200   gst_structure_get_int (structure, "clock-rate", &clock_rate);
201   depayload->clock_rate = clock_rate;
202
203   if ((str = gst_structure_get_string (structure, "media"))) {
204     if (strcmp (str, "audio") == 0) {
205       srccaps = gst_caps_new_simple ("audio/mpeg",
206           "mpegversion", G_TYPE_INT, 4, NULL);
207     } else if (strcmp (str, "video") == 0) {
208       srccaps = gst_caps_new_simple ("video/mpeg",
209           "mpegversion", G_TYPE_INT, 4,
210           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
211     }
212   }
213   if (srccaps == NULL)
214     goto unknown_media;
215
216   /* these values are optional and have a default value of 0 (no header) */
217   rtpmp4gdepay->sizelength =
218       gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
219   rtpmp4gdepay->indexlength =
220       gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
221   rtpmp4gdepay->indexdeltalength =
222       gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
223   rtpmp4gdepay->ctsdeltalength =
224       gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
225   rtpmp4gdepay->dtsdeltalength =
226       gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
227   someint =
228       gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
229   rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
230   rtpmp4gdepay->streamstateindication =
231       gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
232   rtpmp4gdepay->auxiliarydatasizelength =
233       gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
234
235   /* get config string */
236   if ((str = gst_structure_get_string (structure, "config"))) {
237     GValue v = { 0 };
238
239     g_value_init (&v, GST_TYPE_BUFFER);
240     if (gst_value_deserialize (&v, str)) {
241       GstBuffer *buffer;
242
243       buffer = gst_value_get_buffer (&v);
244       gst_buffer_ref (buffer);
245       g_value_unset (&v);
246
247       gst_caps_set_simple (srccaps,
248           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
249     } else {
250       g_warning ("cannot convert config to buffer");
251     }
252   }
253
254   gst_pad_set_caps (depayload->srcpad, srccaps);
255   gst_caps_unref (srccaps);
256
257   return TRUE;
258
259   /* ERRORS */
260 unknown_media:
261   {
262     GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
263     return FALSE;
264   }
265 }
266
267 static GstBuffer *
268 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
269 {
270   GstRtpMP4GDepay *rtpmp4gdepay;
271   GstBuffer *outbuf;
272
273   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
274
275   if (!gst_rtp_buffer_validate (buf))
276     goto bad_packet;
277
278   {
279     gint payload_len, payload_header;
280     guint8 *payload;
281     guint32 timestamp;
282     guint AU_headers_len;
283     guint AU_size, AU_index;
284
285     payload_len = gst_rtp_buffer_get_payload_len (buf);
286     payload = gst_rtp_buffer_get_payload (buf);
287     payload_header = 0;
288
289     if (rtpmp4gdepay->sizelength > 0) {
290       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
291        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
292        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
293        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
294        *
295        * The lenght is 2 bytes and contains the length of the following
296        * AU-headers in bits.
297        */
298       AU_headers_len = (payload[0] << 8) | payload[1];
299
300       /* skip header */
301       payload += 2;
302       payload_header += 2;
303       payload_len -= 2;
304
305       /* FIXME, use bits */
306       AU_size = ((payload[0] << 8) | payload[1]) >> 3;
307       AU_index = payload[1] & 0x7;
308
309       GST_DEBUG_OBJECT (rtpmp4gdepay, "len, %d, size %d, index %d",
310           AU_headers_len, AU_size, AU_index);
311
312       /* skip special headers */
313       payload += (AU_headers_len + 7) / 8;
314       payload_header += (AU_headers_len + 7) / 8;
315       payload_len = AU_size;
316     }
317
318     timestamp = gst_rtp_buffer_get_timestamp (buf);
319
320     /* strip header from payload and push in the adapter */
321     outbuf =
322         gst_rtp_buffer_get_payload_subbuffer (buf, payload_header, payload_len);
323     gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
324
325     /* if this was the last packet of the VOP, create and push a buffer */
326     if (gst_rtp_buffer_get_marker (buf)) {
327       guint avail;
328
329       avail = gst_adapter_available (rtpmp4gdepay->adapter);
330
331       outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
332       gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
333       GST_BUFFER_TIMESTAMP (outbuf) = gst_util_uint64_scale_int
334           (timestamp, GST_SECOND, depayload->clock_rate);
335
336       GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
337           GST_BUFFER_SIZE (outbuf));
338
339       return outbuf;
340     } else {
341       return NULL;
342     }
343   }
344   return NULL;
345
346   /* ERRORS */
347 bad_packet:
348   {
349     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
350         ("Packet did not validate."), (NULL));
351     return NULL;
352   }
353 }
354
355 static void
356 gst_rtp_mp4g_depay_set_property (GObject * object, guint prop_id,
357     const GValue * value, GParamSpec * pspec)
358 {
359   GstRtpMP4GDepay *rtpmp4gdepay;
360
361   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
362
363   switch (prop_id) {
364     default:
365       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366       break;
367   }
368 }
369
370 static void
371 gst_rtp_mp4g_depay_get_property (GObject * object, guint prop_id,
372     GValue * value, GParamSpec * pspec)
373 {
374   GstRtpMP4GDepay *rtpmp4gdepay;
375
376   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
377
378   switch (prop_id) {
379     default:
380       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381       break;
382   }
383 }
384
385 static GstStateChangeReturn
386 gst_rtp_mp4g_depay_change_state (GstElement * element,
387     GstStateChange transition)
388 {
389   GstRtpMP4GDepay *rtpmp4gdepay;
390   GstStateChangeReturn ret;
391
392   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
393
394   switch (transition) {
395     case GST_STATE_CHANGE_READY_TO_PAUSED:
396       gst_adapter_clear (rtpmp4gdepay->adapter);
397       break;
398     default:
399       break;
400   }
401
402   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
403
404   switch (transition) {
405     default:
406       break;
407   }
408   return ret;
409 }
410
411 gboolean
412 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
413 {
414   return gst_element_register (plugin, "rtpmp4gdepay",
415       GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);
416 }