gst/rtp/: Fix caps with payload numbers.
[platform/upstream/gstreamer.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;
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
288     if (rtpmp4gdepay->sizelength > 0) {
289       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
290        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
291        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
292        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
293        *
294        * The lenght is 2 bytes and contains the length of the following
295        * AU-headers in bits.
296        */
297       AU_headers_len = (payload[0] << 8) | payload[1];
298
299       /* skip header */
300       payload += 2;
301       payload_len -= 2;
302
303       /* FIXME, use bits */
304       AU_size = ((payload[0] << 8) | payload[1]) >> 3;
305       AU_index = payload[1] & 0x7;
306
307       GST_DEBUG_OBJECT (rtpmp4gdepay, "len, %d, size %d, index %d",
308           AU_headers_len, AU_size, AU_index);
309
310       /* skip special headers */
311       payload += (AU_headers_len + 7) / 8;
312       payload_len = AU_size;
313     }
314
315     timestamp = gst_rtp_buffer_get_timestamp (buf);
316
317     outbuf = gst_buffer_new_and_alloc (payload_len);
318     memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
319
320     gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
321
322     /* if this was the last packet of the VOP, create and push a buffer */
323     if (gst_rtp_buffer_get_marker (buf)) {
324       guint avail;
325
326       avail = gst_adapter_available (rtpmp4gdepay->adapter);
327
328       outbuf = gst_buffer_new ();
329       GST_BUFFER_SIZE (outbuf) = avail;
330       GST_BUFFER_MALLOCDATA (outbuf) =
331           gst_adapter_take (rtpmp4gdepay->adapter, avail);
332       GST_BUFFER_DATA (outbuf) = GST_BUFFER_MALLOCDATA (outbuf);
333       gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
334       GST_BUFFER_TIMESTAMP (outbuf) = gst_util_uint64_scale_int
335           (timestamp, GST_SECOND, depayload->clock_rate);
336
337       GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
338           GST_BUFFER_SIZE (outbuf));
339
340       return outbuf;
341     } else {
342       return NULL;
343     }
344   }
345   return NULL;
346
347   /* ERRORS */
348 bad_packet:
349   {
350     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
351         ("Packet did not validate."), (NULL));
352     return NULL;
353   }
354 }
355
356 static void
357 gst_rtp_mp4g_depay_set_property (GObject * object, guint prop_id,
358     const GValue * value, GParamSpec * pspec)
359 {
360   GstRtpMP4GDepay *rtpmp4gdepay;
361
362   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
363
364   switch (prop_id) {
365     default:
366       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
367       break;
368   }
369 }
370
371 static void
372 gst_rtp_mp4g_depay_get_property (GObject * object, guint prop_id,
373     GValue * value, GParamSpec * pspec)
374 {
375   GstRtpMP4GDepay *rtpmp4gdepay;
376
377   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
378
379   switch (prop_id) {
380     default:
381       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382       break;
383   }
384 }
385
386 static GstStateChangeReturn
387 gst_rtp_mp4g_depay_change_state (GstElement * element,
388     GstStateChange transition)
389 {
390   GstRtpMP4GDepay *rtpmp4gdepay;
391   GstStateChangeReturn ret;
392
393   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
394
395   switch (transition) {
396     case GST_STATE_CHANGE_READY_TO_PAUSED:
397       gst_adapter_clear (rtpmp4gdepay->adapter);
398       break;
399     default:
400       break;
401   }
402
403   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
404
405   switch (transition) {
406     default:
407       break;
408   }
409   return ret;
410 }
411
412 gboolean
413 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
414 {
415   return gst_element_register (plugin, "rtpmp4gdepay",
416       GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);
417 }