2 * Copyright (C) <2006> 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.
21 * based on http://developer.apple.com/quicktime/icefloe/dispatch026.html
27 #include <gst/rtp/gstrtpbuffer.h>
30 #include "gstrtpxqtdepay.h"
32 #define MAKE_TLV(a,b) (((a)<<8)|(b))
34 #define TLV_sd MAKE_TLV ('s','d')
35 #define TLV_qt MAKE_TLV ('q','t')
36 #define TLV_ti MAKE_TLV ('t','i')
37 #define TLV_ly MAKE_TLV ('l','y')
38 #define TLV_vo MAKE_TLV ('v','o')
39 #define TLV_mx MAKE_TLV ('m','x')
40 #define TLV_tr MAKE_TLV ('t','r')
41 #define TLV_tw MAKE_TLV ('t','w')
42 #define TLV_th MAKE_TLV ('t','h')
43 #define TLV_la MAKE_TLV ('l','a')
44 #define TLV_rt MAKE_TLV ('r','t')
45 #define TLV_gm MAKE_TLV ('g','m')
46 #define TLV_oc MAKE_TLV ('o','c')
47 #define TLV_cr MAKE_TLV ('c','r')
48 #define TLV_du MAKE_TLV ('d','u')
49 #define TLV_po MAKE_TLV ('p','o')
51 #define QT_UINT32(a) (GST_READ_UINT32_BE(a))
52 #define QT_UINT24(a) (GST_READ_UINT32_BE(a) >> 8)
53 #define QT_UINT16(a) (GST_READ_UINT16_BE(a))
54 #define QT_UINT8(a) (GST_READ_UINT8(a))
55 #define QT_FP32(a) ((GST_READ_UINT32_BE(a))/65536.0)
56 #define QT_FP16(a) ((GST_READ_UINT16_BE(a))/256.0)
57 #define QT_FOURCC(a) (GST_READ_UINT32_LE(a))
58 #define QT_UINT64(a) ((((guint64)QT_UINT32(a))<<32)|QT_UINT32(((guint8 *)a)+4))
60 #define FOURCC_avc1 GST_MAKE_FOURCC('a','v','c','1')
61 #define FOURCC_avcC GST_MAKE_FOURCC('a','v','c','C')
63 GST_DEBUG_CATEGORY_STATIC (rtpxqtdepay_debug);
64 #define GST_CAT_DEFAULT (rtpxqtdepay_debug)
66 /* RtpXQTDepay signals and args */
78 static GstStaticPadTemplate gst_rtp_xqt_depay_src_template =
79 GST_STATIC_PAD_TEMPLATE ("src",
84 static GstStaticPadTemplate gst_rtp_xqt_depay_sink_template =
85 GST_STATIC_PAD_TEMPLATE ("sink",
88 GST_STATIC_CAPS ("application/x-rtp, "
89 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
90 "media = (string) { \"audio\", \"video\" }, clock-rate = (int) [1, MAX], "
91 "encoding-name = (string) { \"X-QT\", \"X-QUICKTIME\" }")
94 #define gst_rtp_xqt_depay_parent_class parent_class
95 G_DEFINE_TYPE (GstRtpXQTDepay, gst_rtp_xqt_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
97 static void gst_rtp_xqt_depay_finalize (GObject * object);
99 static gboolean gst_rtp_xqt_depay_setcaps (GstRTPBaseDepayload * depayload,
101 static GstBuffer *gst_rtp_xqt_depay_process (GstRTPBaseDepayload * depayload,
104 static GstStateChangeReturn gst_rtp_xqt_depay_change_state (GstElement *
105 element, GstStateChange transition);
109 gst_rtp_xqt_depay_class_init (GstRtpXQTDepayClass * klass)
111 GObjectClass *gobject_class;
112 GstElementClass *gstelement_class;
113 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
115 gobject_class = (GObjectClass *) klass;
116 gstelement_class = (GstElementClass *) klass;
117 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
119 parent_class = g_type_class_peek_parent (klass);
121 gobject_class->finalize = gst_rtp_xqt_depay_finalize;
123 gstelement_class->change_state = gst_rtp_xqt_depay_change_state;
125 gstrtpbasedepayload_class->set_caps = gst_rtp_xqt_depay_setcaps;
126 gstrtpbasedepayload_class->process = gst_rtp_xqt_depay_process;
128 GST_DEBUG_CATEGORY_INIT (rtpxqtdepay_debug, "rtpxqtdepay", 0,
129 "QT Media RTP Depayloader");
131 gst_element_class_add_pad_template (gstelement_class,
132 gst_static_pad_template_get (&gst_rtp_xqt_depay_src_template));
133 gst_element_class_add_pad_template (gstelement_class,
134 gst_static_pad_template_get (&gst_rtp_xqt_depay_sink_template));
136 gst_element_class_set_static_metadata (gstelement_class,
137 "RTP packet depayloader", "Codec/Depayloader/Network",
138 "Extracts Quicktime audio/video from RTP packets",
139 "Wim Taymans <wim@fluendo.com>");
143 gst_rtp_xqt_depay_init (GstRtpXQTDepay * rtpxqtdepay)
145 rtpxqtdepay->adapter = gst_adapter_new ();
149 gst_rtp_xqt_depay_finalize (GObject * object)
151 GstRtpXQTDepay *rtpxqtdepay;
153 rtpxqtdepay = GST_RTP_XQT_DEPAY (object);
155 g_object_unref (rtpxqtdepay->adapter);
156 rtpxqtdepay->adapter = NULL;
158 G_OBJECT_CLASS (parent_class)->finalize (object);
162 gst_rtp_quicktime_parse_sd (GstRtpXQTDepay * rtpxqtdepay, guint8 * data,
171 len = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
175 fourcc = QT_FOURCC (data + 4);
177 GST_DEBUG_OBJECT (rtpxqtdepay, "parsing %" GST_FOURCC_FORMAT,
178 GST_FOURCC_ARGS (fourcc));
192 chlen = QT_UINT32 (data);
193 fourcc = QT_FOURCC (data + 4);
194 if (fourcc == FOURCC_avcC) {
199 GST_DEBUG_OBJECT (rtpxqtdepay, "found avcC codec_data in sd, %u",
202 /* parse, if found */
208 buf = gst_buffer_new_and_alloc (size);
209 gst_buffer_fill (buf, 0, data + 8, size);
210 caps = gst_caps_new_simple ("video/x-h264",
211 "codec_data", GST_TYPE_BUFFER, buf, NULL);
212 gst_buffer_unref (buf);
213 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD (rtpxqtdepay)->srcpad, caps);
214 gst_caps_unref (caps);
235 gst_rtp_xqt_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
237 GstStructure *structure;
238 gint clock_rate = 90000; /* default */
240 structure = gst_caps_get_structure (caps, 0);
242 gst_structure_get_int (structure, "clock-rate", &clock_rate);
243 depayload->clock_rate = clock_rate;
249 gst_rtp_xqt_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
251 GstRtpXQTDepay *rtpxqtdepay;
252 GstBuffer *outbuf = NULL;
254 GstRTPBuffer rtp = { NULL };
256 rtpxqtdepay = GST_RTP_XQT_DEPAY (depayload);
258 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
260 if (!gst_rtp_buffer_validate (buf))
263 if (GST_BUFFER_IS_DISCONT (buf)) {
264 /* discont, clear adapter and try to find a new packet start */
265 gst_adapter_clear (rtpxqtdepay->adapter);
266 rtpxqtdepay->need_resync = TRUE;
267 GST_DEBUG_OBJECT (rtpxqtdepay, "we need resync");
270 m = gst_rtp_buffer_get_marker (&rtp);
271 GST_LOG_OBJECT (rtpxqtdepay, "marker: %d", m);
280 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
281 payload = gst_rtp_buffer_get_payload (&rtp);
284 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
285 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
286 * | VER |PCK|S|Q|L| RES |D| QuickTime Payload ID |
287 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
289 if (payload_len <= 4)
292 ver = (payload[0] & 0xf0) >> 4;
296 pck = (payload[0] & 0x0c) >> 2;
300 s = (payload[0] & 0x02) != 0; /* contains sync sample */
301 q = (payload[0] & 0x01) != 0; /* has payload description */
302 l = (payload[1] & 0x80) != 0; /* has packet specific information description */
303 d = (payload[2] & 0x80) != 0; /* don't cache info for payload id */
304 /* id used for caching info */
305 rtpxqtdepay->current_id = ((payload[2] & 0x7f) << 8) | payload[3];
307 GST_LOG_OBJECT (rtpxqtdepay,
308 "VER: %d, PCK: %d, S: %d, Q: %d, L: %d, D: %d, ID: %d", ver, pck, s, q,
309 l, d, rtpxqtdepay->current_id);
311 if (rtpxqtdepay->need_resync) {
312 /* we need to find the boundary of a new packet after a DISCONT */
314 /* non-fragmented packet or payload description present, packet starts
316 rtpxqtdepay->need_resync = FALSE;
318 /* fragmented packet without description */
320 /* marker bit set, next packet is start of new one */
321 rtpxqtdepay->need_resync = FALSE;
332 guint pdlen, pdpadded;
334 /* media_type only used for printing */
335 guint32 G_GNUC_UNUSED media_type;
339 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
340 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
341 * |K|F|A|Z| RES | QuickTime Payload Desc Length |
342 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
343 * . QuickTime Payload Desc Data ... .
344 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
346 if (payload_len <= 4)
349 k = (payload[0] & 0x80) != 0; /* keyframe */
350 f = (payload[0] & 0x40) != 0; /* sparse */
351 a = (payload[0] & 0x20) != 0; /* start of payload */
352 z = (payload[0] & 0x10) != 0; /* end of payload */
353 pdlen = (payload[2] << 8) | payload[3];
359 pdpadded = pdlen + 3;
360 pdpadded -= pdpadded % 4;
361 if (payload_len < pdpadded)
364 padding = pdpadded - pdlen;
365 GST_LOG_OBJECT (rtpxqtdepay,
366 "K: %d, F: %d, A: %d, Z: %d, len: %d, padding %d", k, f, a, z, pdlen,
372 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374 * | QuickTime Media Type |
375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
378 * . QuickTime TLVs ... .
379 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
382 (payload[0] << 24) | (payload[1] << 16) | (payload[2] << 8) |
385 (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) |
388 GST_LOG_OBJECT (rtpxqtdepay, "media_type: %c%c%c%c, timescale %u",
389 payload[0], payload[1], payload[2], payload[3], timescale);
395 /* parse TLV (type-length-value triplets */
397 guint16 tlv_len, tlv_type;
400 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
402 * | QuickTime TLV Length | QuickTime TLV Type |
403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
404 * . QuickTime TLV Value ... .
405 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
407 tlv_len = (payload[0] << 8) | payload[1];
408 tlv_type = (payload[2] << 8) | payload[3];
413 GST_LOG_OBJECT (rtpxqtdepay, "TLV '%c%c', len %d", payload[2],
414 payload[3], tlv_len);
421 /* Session description */
422 if (!gst_rtp_quicktime_parse_sd (rtpxqtdepay, payload, tlv_len))
424 rtpxqtdepay->have_sd = TRUE;
447 payload_len -= tlv_len;
450 payload_len -= padding;
454 guint ssilen, ssipadded;
458 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
459 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
460 * | RES | Sample-Specific Info Length |
461 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
462 * . QuickTime TLVs ...
463 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
465 if (payload_len <= 4)
468 ssilen = (payload[2] << 8) | payload[3];
473 ssipadded = ssilen + 3;
474 ssipadded -= ssipadded % 4;
475 if (payload_len < ssipadded)
478 padding = ssipadded - ssilen;
479 GST_LOG_OBJECT (rtpxqtdepay, "len: %d, padding %d", ssilen, padding);
485 /* parse TLV (type-length-value triplets */
487 guint16 tlv_len, tlv_type;
490 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
491 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
492 * | QuickTime TLV Length | QuickTime TLV Type |
493 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
494 * . QuickTime TLV Value ... .
495 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
497 tlv_len = (payload[0] << 8) | payload[1];
498 tlv_type = (payload[2] << 8) | payload[3];
500 if (tlv_len > ssilen)
503 GST_LOG_OBJECT (rtpxqtdepay, "TLV '%c%c', len %d", payload[2],
504 payload[3], tlv_len);
532 payload_len -= tlv_len;
535 payload_len -= padding;
538 rtpxqtdepay->previous_id = rtpxqtdepay->current_id;
543 /* multiple samples per packet. */
544 outbuf = gst_buffer_new_and_alloc (payload_len);
545 gst_buffer_fill (outbuf, 0, payload, payload_len);
553 /* multiple samples per packet.
555 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
556 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
557 * |S| Reserved | Sample Length |
558 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
559 * | Sample Timestamp |
560 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
561 * . Sample Data ... .
562 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
563 * |S| Reserved | Sample Length |
564 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
565 * | Sample Timestamp |
566 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
567 * . Sample Data ... .
568 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
572 while (payload_len > 8) {
573 s = (payload[0] & 0x80) != 0; /* contains sync sample */
574 slen = (payload[2] << 8) | payload[3];
576 * (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) |
583 if (slen > payload_len)
586 outbuf = gst_buffer_new_and_alloc (slen);
587 gst_buffer_fill (outbuf, 0, payload, slen);
589 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
591 gst_rtp_base_depayload_push (depayload, outbuf);
593 /* aligned on 32 bit boundary */
594 slen = GST_ROUND_UP_4 (slen);
603 /* one sample per packet, use adapter to combine based on marker bit. */
604 outbuf = gst_buffer_new_and_alloc (payload_len);
605 gst_buffer_fill (outbuf, 0, payload, payload_len);
607 gst_adapter_push (rtpxqtdepay->adapter, outbuf);
612 avail = gst_adapter_available (rtpxqtdepay->adapter);
613 outbuf = gst_adapter_take_buffer (rtpxqtdepay->adapter, avail);
615 GST_DEBUG_OBJECT (rtpxqtdepay,
616 "gst_rtp_xqt_depay_chain: pushing buffer of size %u", avail);
624 gst_rtp_buffer_unmap (&rtp);
629 GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
630 ("Packet did not validate."), (NULL));
635 GST_DEBUG_OBJECT (rtpxqtdepay, "waiting for marker");
640 GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
641 ("Unknown payload version."), (NULL));
646 GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
647 ("PCK reserved 0."), (NULL));
652 GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
653 ("Wrong payload length."), (NULL));
658 GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
659 ("Unknown payload format."), (NULL));
664 static GstStateChangeReturn
665 gst_rtp_xqt_depay_change_state (GstElement * element, GstStateChange transition)
667 GstRtpXQTDepay *rtpxqtdepay;
668 GstStateChangeReturn ret;
670 rtpxqtdepay = GST_RTP_XQT_DEPAY (element);
672 switch (transition) {
673 case GST_STATE_CHANGE_READY_TO_PAUSED:
674 gst_adapter_clear (rtpxqtdepay->adapter);
675 rtpxqtdepay->previous_id = -1;
676 rtpxqtdepay->current_id = -1;
677 rtpxqtdepay->need_resync = TRUE;
678 rtpxqtdepay->have_sd = FALSE;
684 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
686 switch (transition) {
687 case GST_STATE_CHANGE_PAUSED_TO_READY:
688 gst_adapter_clear (rtpxqtdepay->adapter);