2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
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.
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.
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.
24 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpmp4gdepay.h"
29 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
30 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
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>");
39 /* RtpMP4GDepay signals and args */
51 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("video/mpeg,"
56 "mpegversion=(int) 4,"
57 "systemstream=(boolean)false;" "audio/mpeg," "mpegversion=(int) 4")
60 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
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]" */ )
91 GST_BOILERPLATE (GstRtpMP4GDepay, gst_rtp_mp4g_depay, GstBaseRTPDepayload,
92 GST_TYPE_BASE_RTP_DEPAYLOAD);
94 static void gst_rtp_mp4g_depay_finalize (GObject * object);
96 static gboolean gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload,
98 static GstBuffer *gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload,
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);
106 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
107 element, GstStateChange transition);
111 gst_rtp_mp4g_depay_base_init (gpointer klass)
113 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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));
120 gst_element_class_set_details (element_class, &gst_rtp_mp4gdepay_details);
124 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
126 GObjectClass *gobject_class;
127 GstElementClass *gstelement_class;
128 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
130 gobject_class = (GObjectClass *) klass;
131 gstelement_class = (GstElementClass *) klass;
132 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
134 parent_class = g_type_class_peek_parent (klass);
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;
140 gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
142 gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
143 gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
145 GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
146 "MP4-generic RTP Depayloader");
150 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay,
151 GstRtpMP4GDepayClass * klass)
153 rtpmp4gdepay->adapter = gst_adapter_new ();
157 gst_rtp_mp4g_depay_finalize (GObject * object)
159 GstRtpMP4GDepay *rtpmp4gdepay;
161 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
163 g_object_unref (rtpmp4gdepay->adapter);
164 rtpmp4gdepay->adapter = NULL;
166 G_OBJECT_CLASS (parent_class)->finalize (object);
170 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
176 if ((str = gst_structure_get_string (structure, field)))
179 if (gst_structure_get_int (structure, field, &res))
186 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
189 GstStructure *structure;
190 GstRtpMP4GDepay *rtpmp4gdepay;
191 GstCaps *srccaps = NULL;
193 gint clock_rate = 90000; /* default */
196 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
198 structure = gst_caps_get_structure (caps, 0);
200 gst_structure_get_int (structure, "clock-rate", &clock_rate);
201 depayload->clock_rate = clock_rate;
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);
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);
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);
235 /* get config string */
236 if ((str = gst_structure_get_string (structure, "config"))) {
239 g_value_init (&v, GST_TYPE_BUFFER);
240 if (gst_value_deserialize (&v, str)) {
243 buffer = gst_value_get_buffer (&v);
244 gst_buffer_ref (buffer);
247 gst_caps_set_simple (srccaps,
248 "codec_data", GST_TYPE_BUFFER, buffer, NULL);
250 g_warning ("cannot convert config to buffer");
254 gst_pad_set_caps (depayload->srcpad, srccaps);
255 gst_caps_unref (srccaps);
262 GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
268 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
270 GstRtpMP4GDepay *rtpmp4gdepay;
273 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
275 if (!gst_rtp_buffer_validate (buf))
279 gint payload_len, payload_header;
282 guint AU_headers_len;
283 guint AU_size, AU_index;
285 payload_len = gst_rtp_buffer_get_payload_len (buf);
286 payload = gst_rtp_buffer_get_payload (buf);
289 if (rtpmp4gdepay->sizelength > 0) {
290 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
291 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
292 * | | (1) | (2) | | (n) * | bits |
293 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
295 * The lenght is 2 bytes and contains the length of the following
296 * AU-headers in bits.
298 AU_headers_len = (payload[0] << 8) | payload[1];
305 /* FIXME, use bits */
306 AU_size = ((payload[0] << 8) | payload[1]) >> 3;
307 AU_index = payload[1] & 0x7;
309 GST_DEBUG_OBJECT (rtpmp4gdepay, "len, %d, size %d, index %d",
310 AU_headers_len, AU_size, AU_index);
312 /* skip special headers */
313 payload += (AU_headers_len + 7) / 8;
314 payload_header += (AU_headers_len + 7) / 8;
315 payload_len = AU_size;
318 timestamp = gst_rtp_buffer_get_timestamp (buf);
320 /* strip header from payload and push in the adapter */
322 gst_rtp_buffer_get_payload_subbuffer (buf, payload_header, payload_len);
323 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
325 /* if this was the last packet of the VOP, create and push a buffer */
326 if (gst_rtp_buffer_get_marker (buf)) {
329 avail = gst_adapter_available (rtpmp4gdepay->adapter);
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);
336 GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
337 GST_BUFFER_SIZE (outbuf));
349 GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
350 ("Packet did not validate."), (NULL));
356 gst_rtp_mp4g_depay_set_property (GObject * object, guint prop_id,
357 const GValue * value, GParamSpec * pspec)
359 GstRtpMP4GDepay *rtpmp4gdepay;
361 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
365 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371 gst_rtp_mp4g_depay_get_property (GObject * object, guint prop_id,
372 GValue * value, GParamSpec * pspec)
374 GstRtpMP4GDepay *rtpmp4gdepay;
376 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
385 static GstStateChangeReturn
386 gst_rtp_mp4g_depay_change_state (GstElement * element,
387 GstStateChange transition)
389 GstRtpMP4GDepay *rtpmp4gdepay;
390 GstStateChangeReturn ret;
392 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
394 switch (transition) {
395 case GST_STATE_CHANGE_READY_TO_PAUSED:
396 gst_adapter_clear (rtpmp4gdepay->adapter);
402 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
404 switch (transition) {
412 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
414 return gst_element_register (plugin, "rtpmp4gdepay",
415 GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);