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.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpmp4gdepay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
33 /* elementfactory information */
34 static const GstElementDetails gst_rtp_mp4gdepay_details =
35 GST_ELEMENT_DETAILS ("RTP packet depayloader",
36 "Codec/Depayloader/Network",
37 "Extracts MPEG4 elementary streams from RTP packets (RFC 3640)",
38 "Wim Taymans <wim@fluendo.com>");
40 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("video/mpeg,"
45 "mpegversion=(int) 4,"
46 "systemstream=(boolean)false;" "audio/mpeg," "mpegversion=(int) 4")
49 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
50 GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_STATIC_CAPS ("application/x-rtp, "
54 "media = (string) { \"video\", \"audio\", \"application\" }, "
55 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56 "clock-rate = (int) [1, MAX ], "
57 "encoding-name = (string) \"MPEG4-GENERIC\", "
58 /* required string params */
59 "streamtype = (string) { \"4\", \"5\" }, " /* 4 = video, 5 = audio */
60 /* "profile-level-id = (string) [1,MAX], " */
61 /* "config = (string) [1,MAX]" */
62 "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\" } "
63 /* Optional general parameters */
64 /* "objecttype = (string) [1,MAX], " */
65 /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
66 /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
67 /* "maxdisplacement = (string) [1,MAX], " */
68 /* "de-interleavebuffersize = (string) [1,MAX], " */
69 /* Optional configuration parameters */
70 /* "sizelength = (string) [1, 16], " *//* max 16 bits, should be enough... */
71 /* "indexlength = (string) [1, 8], " */
72 /* "indexdeltalength = (string) [1, 8], " */
73 /* "ctsdeltalength = (string) [1, 64], " */
74 /* "dtsdeltalength = (string) [1, 64], " */
75 /* "randomaccessindication = (string) {0, 1}, " */
76 /* "streamstateindication = (string) [0, 64], " */
77 /* "auxiliarydatasizelength = (string) [0, 64]" */ )
80 GST_BOILERPLATE (GstRtpMP4GDepay, gst_rtp_mp4g_depay, GstBaseRTPDepayload,
81 GST_TYPE_BASE_RTP_DEPAYLOAD);
83 static void gst_rtp_mp4g_depay_finalize (GObject * object);
85 static gboolean gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload,
87 static GstBuffer *gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload,
90 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
91 element, GstStateChange transition);
95 gst_rtp_mp4g_depay_base_init (gpointer klass)
97 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
99 gst_element_class_add_pad_template (element_class,
100 gst_static_pad_template_get (&gst_rtp_mp4g_depay_src_template));
101 gst_element_class_add_pad_template (element_class,
102 gst_static_pad_template_get (&gst_rtp_mp4g_depay_sink_template));
104 gst_element_class_set_details (element_class, &gst_rtp_mp4gdepay_details);
108 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
110 GObjectClass *gobject_class;
111 GstElementClass *gstelement_class;
112 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
114 gobject_class = (GObjectClass *) klass;
115 gstelement_class = (GstElementClass *) klass;
116 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
118 parent_class = g_type_class_peek_parent (klass);
120 gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
122 gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
124 gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
125 gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
127 GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
128 "MP4-generic RTP Depayloader");
132 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay,
133 GstRtpMP4GDepayClass * klass)
135 rtpmp4gdepay->adapter = gst_adapter_new ();
139 gst_rtp_mp4g_depay_finalize (GObject * object)
141 GstRtpMP4GDepay *rtpmp4gdepay;
143 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
145 g_object_unref (rtpmp4gdepay->adapter);
146 rtpmp4gdepay->adapter = NULL;
148 G_OBJECT_CLASS (parent_class)->finalize (object);
152 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
158 if ((str = gst_structure_get_string (structure, field)))
161 if (gst_structure_get_int (structure, field, &res))
168 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
171 GstStructure *structure;
172 GstRtpMP4GDepay *rtpmp4gdepay;
173 GstCaps *srccaps = NULL;
175 gint clock_rate = 90000; /* default */
178 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
180 structure = gst_caps_get_structure (caps, 0);
182 gst_structure_get_int (structure, "clock-rate", &clock_rate);
183 depayload->clock_rate = clock_rate;
185 if ((str = gst_structure_get_string (structure, "media"))) {
186 if (strcmp (str, "audio") == 0) {
187 srccaps = gst_caps_new_simple ("audio/mpeg",
188 "mpegversion", G_TYPE_INT, 4, NULL);
189 } else if (strcmp (str, "video") == 0) {
190 srccaps = gst_caps_new_simple ("video/mpeg",
191 "mpegversion", G_TYPE_INT, 4,
192 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
198 /* these values are optional and have a default value of 0 (no header) */
199 rtpmp4gdepay->sizelength =
200 gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
201 rtpmp4gdepay->indexlength =
202 gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
203 rtpmp4gdepay->indexdeltalength =
204 gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
205 rtpmp4gdepay->ctsdeltalength =
206 gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
207 rtpmp4gdepay->dtsdeltalength =
208 gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
210 gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
211 rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
212 rtpmp4gdepay->streamstateindication =
213 gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
214 rtpmp4gdepay->auxiliarydatasizelength =
215 gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
217 /* get config string */
218 if ((str = gst_structure_get_string (structure, "config"))) {
221 g_value_init (&v, GST_TYPE_BUFFER);
222 if (gst_value_deserialize (&v, str)) {
225 buffer = gst_value_get_buffer (&v);
226 gst_caps_set_simple (srccaps,
227 "codec_data", GST_TYPE_BUFFER, buffer, NULL);
230 g_warning ("cannot convert config to buffer");
234 gst_pad_set_caps (depayload->srcpad, srccaps);
235 gst_caps_unref (srccaps);
242 GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
248 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
250 GstRtpMP4GDepay *rtpmp4gdepay;
253 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
255 if (!gst_rtp_buffer_validate (buf))
258 /* flush remaining data on discont */
259 if (GST_BUFFER_IS_DISCONT (buf)) {
260 gst_adapter_clear (rtpmp4gdepay->adapter);
264 gint payload_len, payload_header;
267 guint AU_headers_len;
268 guint AU_size, AU_index;
271 payload_len = gst_rtp_buffer_get_payload_len (buf);
272 payload = gst_rtp_buffer_get_payload (buf);
275 timestamp = gst_rtp_buffer_get_timestamp (buf);
276 M = gst_rtp_buffer_get_marker (buf);
278 if (rtpmp4gdepay->sizelength > 0) {
279 gint num_AU_headers, AU_headers_bytes, i;
281 /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
282 * |AU-headers-length|AU-header|AU-header| |AU-header|padding|
283 * | | (1) | (2) | | (n) * | bits |
284 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
286 * The length is 2 bytes and contains the length of the following
287 * AU-headers in bits.
289 AU_headers_len = (payload[0] << 8) | payload[1];
290 AU_headers_bytes = (AU_headers_len + 7) / 8;
291 num_AU_headers = AU_headers_len / 16;
293 GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
294 AU_headers_len, AU_headers_bytes, num_AU_headers);
298 /* skip special headers */
299 payload_header = 2 + AU_headers_bytes;
301 for (i = 0; i < num_AU_headers; i++) {
302 /* FIXME, use bits */
303 AU_size = ((payload[0] << 8) | payload[1]) >> 3;
304 AU_index = payload[1] & 0x7;
307 GST_DEBUG_OBJECT (rtpmp4gdepay, "len, %d, size %d, index %d",
308 AU_headers_len, AU_size, AU_index);
310 /* collect stuff in the adapter, strip header from payload and push in
313 gst_rtp_buffer_get_payload_subbuffer (buf, payload_header, AU_size);
314 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
319 /* packet is complete, flush */
320 avail = gst_adapter_available (rtpmp4gdepay->adapter);
322 outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
323 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
325 GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
326 GST_BUFFER_SIZE (outbuf));
328 /* only apply the timestamp for the first buffer */
330 gst_base_rtp_depayload_push_ts (depayload, timestamp, outbuf);
332 gst_base_rtp_depayload_push (depayload, outbuf);
334 payload_header += AU_size;
337 /* push complete buffer in adapter */
338 outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 0, payload_len);
339 gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
341 /* if this was the last packet of the VOP, create and push a buffer */
345 avail = gst_adapter_available (rtpmp4gdepay->adapter);
347 outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
348 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
350 GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
351 GST_BUFFER_SIZE (outbuf));
362 GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
363 ("Packet did not validate."), (NULL));
368 static GstStateChangeReturn
369 gst_rtp_mp4g_depay_change_state (GstElement * element,
370 GstStateChange transition)
372 GstRtpMP4GDepay *rtpmp4gdepay;
373 GstStateChangeReturn ret;
375 rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
377 switch (transition) {
378 case GST_STATE_CHANGE_READY_TO_PAUSED:
379 gst_adapter_clear (rtpmp4gdepay->adapter);
385 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
387 switch (transition) {
395 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
397 return gst_element_register (plugin, "rtpmp4gdepay",
398 GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);