1 /* GStreamer
2  * Copyright (C) <2006> 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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /*
21  * based on http://developer.apple.com/quicktime/icefloe/dispatch026.html
22  */
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26 
27 #include <gst/rtp/gstrtpbuffer.h>
28 
29 #include <string.h>
30 #include "gstrtpxqtdepay.h"
31 
32 #define MAKE_TLV(a,b)  (((a)<<8)|(b))
33 
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')
50 
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))
59 
60 #define FOURCC_avc1     GST_MAKE_FOURCC('a','v','c','1')
61 #define FOURCC_avc3     GST_MAKE_FOURCC('a','v','c','3')
62 #define FOURCC_avcC     GST_MAKE_FOURCC('a','v','c','C')
63 
64 GST_DEBUG_CATEGORY_STATIC (rtpxqtdepay_debug);
65 #define GST_CAT_DEFAULT (rtpxqtdepay_debug)
66 
67 /* RtpXQTDepay signals and args */
68 enum
69 {
70   /* FILL ME */
71   LAST_SIGNAL
72 };
73 
74 enum
75 {
76   PROP_0,
77 };
78 
79 static GstStaticPadTemplate gst_rtp_xqt_depay_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
81     GST_PAD_SRC,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS_ANY);
84 
85 static GstStaticPadTemplate gst_rtp_xqt_depay_sink_template =
86 GST_STATIC_PAD_TEMPLATE ("sink",
87     GST_PAD_SINK,
88     GST_PAD_ALWAYS,
89     GST_STATIC_CAPS ("application/x-rtp, "
90         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
91         "media = (string) { \"audio\", \"video\" }, clock-rate = (int) [1, MAX], "
92         "encoding-name = (string) { \"X-QT\", \"X-QUICKTIME\" }")
93     );
94 
95 #define gst_rtp_xqt_depay_parent_class parent_class
96 G_DEFINE_TYPE (GstRtpXQTDepay, gst_rtp_xqt_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
97 
98 static void gst_rtp_xqt_depay_finalize (GObject * object);
99 
100 static gboolean gst_rtp_xqt_depay_setcaps (GstRTPBaseDepayload * depayload,
101     GstCaps * caps);
102 static GstBuffer *gst_rtp_xqt_depay_process (GstRTPBaseDepayload * depayload,
103     GstBuffer * buf);
104 
105 static GstStateChangeReturn gst_rtp_xqt_depay_change_state (GstElement *
106     element, GstStateChange transition);
107 
108 
109 static void
gst_rtp_xqt_depay_class_init(GstRtpXQTDepayClass * klass)110 gst_rtp_xqt_depay_class_init (GstRtpXQTDepayClass * klass)
111 {
112   GObjectClass *gobject_class;
113   GstElementClass *gstelement_class;
114   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
115 
116   gobject_class = (GObjectClass *) klass;
117   gstelement_class = (GstElementClass *) klass;
118   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
119 
120   parent_class = g_type_class_peek_parent (klass);
121 
122   gobject_class->finalize = gst_rtp_xqt_depay_finalize;
123 
124   gstelement_class->change_state = gst_rtp_xqt_depay_change_state;
125 
126   gstrtpbasedepayload_class->set_caps = gst_rtp_xqt_depay_setcaps;
127   gstrtpbasedepayload_class->process = gst_rtp_xqt_depay_process;
128 
129   GST_DEBUG_CATEGORY_INIT (rtpxqtdepay_debug, "rtpxqtdepay", 0,
130       "QT Media RTP Depayloader");
131 
132   gst_element_class_add_static_pad_template (gstelement_class,
133       &gst_rtp_xqt_depay_src_template);
134   gst_element_class_add_static_pad_template (gstelement_class,
135       &gst_rtp_xqt_depay_sink_template);
136 
137   gst_element_class_set_static_metadata (gstelement_class,
138       "RTP packet depayloader", "Codec/Depayloader/Network",
139       "Extracts Quicktime audio/video from RTP packets",
140       "Wim Taymans <wim@fluendo.com>");
141 }
142 
143 static void
gst_rtp_xqt_depay_init(GstRtpXQTDepay * rtpxqtdepay)144 gst_rtp_xqt_depay_init (GstRtpXQTDepay * rtpxqtdepay)
145 {
146   rtpxqtdepay->adapter = gst_adapter_new ();
147 }
148 
149 static void
gst_rtp_xqt_depay_finalize(GObject * object)150 gst_rtp_xqt_depay_finalize (GObject * object)
151 {
152   GstRtpXQTDepay *rtpxqtdepay;
153 
154   rtpxqtdepay = GST_RTP_XQT_DEPAY (object);
155 
156   g_object_unref (rtpxqtdepay->adapter);
157   rtpxqtdepay->adapter = NULL;
158 
159   G_OBJECT_CLASS (parent_class)->finalize (object);
160 }
161 
162 static gboolean
gst_rtp_quicktime_parse_sd(GstRtpXQTDepay * rtpxqtdepay,guint8 * data,guint data_len)163 gst_rtp_quicktime_parse_sd (GstRtpXQTDepay * rtpxqtdepay, guint8 * data,
164     guint data_len)
165 {
166   gint len;
167   guint32 fourcc;
168 
169   if (data_len < 8)
170     goto too_short;
171 
172   len = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
173   if (len > data_len)
174     goto too_short;
175 
176   fourcc = QT_FOURCC (data + 4);
177 
178   GST_DEBUG_OBJECT (rtpxqtdepay, "parsing %" GST_FOURCC_FORMAT,
179       GST_FOURCC_ARGS (fourcc));
180 
181   switch (fourcc) {
182     case FOURCC_avc1:
183     case FOURCC_avc3:
184     {
185       guint32 chlen;
186 
187       if (len < 0x56)
188         goto too_short;
189       len -= 0x56;
190       data += 0x56;
191 
192       /* find avcC */
193       while (len >= 8) {
194         chlen = QT_UINT32 (data);
195         fourcc = QT_FOURCC (data + 4);
196         if (fourcc == FOURCC_avcC) {
197           GstBuffer *buf;
198           gint size;
199           GstCaps *caps;
200 
201           GST_DEBUG_OBJECT (rtpxqtdepay, "found avcC codec_data in sd, %u",
202               chlen);
203 
204           /* parse, if found */
205           if (chlen < len)
206             size = chlen - 8;
207           else
208             size = len - 8;
209 
210           buf = gst_buffer_new_and_alloc (size);
211           gst_buffer_fill (buf, 0, data + 8, size);
212           caps = gst_caps_new_simple ("video/x-h264",
213               "codec_data", GST_TYPE_BUFFER, buf, NULL);
214           gst_buffer_unref (buf);
215           gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD (rtpxqtdepay)->srcpad, caps);
216           gst_caps_unref (caps);
217           break;
218         }
219         len -= chlen;
220         data += chlen;
221       }
222       break;
223     }
224     default:
225       break;
226   }
227   return TRUE;
228 
229   /* ERRORS */
230 too_short:
231   {
232     return FALSE;
233   }
234 }
235 
236 static gboolean
gst_rtp_xqt_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)237 gst_rtp_xqt_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
238 {
239   GstStructure *structure;
240   gint clock_rate = 90000;      /* default */
241 
242   structure = gst_caps_get_structure (caps, 0);
243 
244   gst_structure_get_int (structure, "clock-rate", &clock_rate);
245   depayload->clock_rate = clock_rate;
246 
247   return TRUE;
248 }
249 
250 static GstBuffer *
gst_rtp_xqt_depay_process(GstRTPBaseDepayload * depayload,GstBuffer * buf)251 gst_rtp_xqt_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
252 {
253   GstRtpXQTDepay *rtpxqtdepay;
254   GstBuffer *outbuf = NULL;
255   gboolean m;
256   GstRTPBuffer rtp = { NULL };
257 
258   rtpxqtdepay = GST_RTP_XQT_DEPAY (depayload);
259 
260   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
261 
262   if (GST_BUFFER_IS_DISCONT (buf)) {
263     /* discont, clear adapter and try to find a new packet start */
264     gst_adapter_clear (rtpxqtdepay->adapter);
265     rtpxqtdepay->need_resync = TRUE;
266     GST_DEBUG_OBJECT (rtpxqtdepay, "we need resync");
267   }
268 
269   m = gst_rtp_buffer_get_marker (&rtp);
270   GST_LOG_OBJECT (rtpxqtdepay, "marker: %d", m);
271 
272   {
273     gint payload_len;
274     guint avail;
275     guint8 *payload;
276     guint8 ver, pck;
277     gboolean s, q, l, d;
278 
279     payload_len = gst_rtp_buffer_get_payload_len (&rtp);
280     payload = gst_rtp_buffer_get_payload (&rtp);
281 
282     /*                      1                   2                   3
283      *  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
284      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
285      * | VER   |PCK|S|Q|L| RES         |D| QuickTime Payload ID        |
286      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
287      */
288     if (payload_len <= 4)
289       goto wrong_length;
290 
291     ver = (payload[0] & 0xf0) >> 4;
292     if (ver > 1)
293       goto wrong_version;
294 
295     pck = (payload[0] & 0x0c) >> 2;
296     if (pck == 0)
297       goto pck_reserved;
298 
299     s = (payload[0] & 0x02) != 0;       /* contains sync sample */
300     q = (payload[0] & 0x01) != 0;       /* has payload description */
301     l = (payload[1] & 0x80) != 0;       /* has packet specific information description */
302     d = (payload[2] & 0x80) != 0;       /* don't cache info for payload id */
303     /* id used for caching info */
304     rtpxqtdepay->current_id = ((payload[2] & 0x7f) << 8) | payload[3];
305 
306     GST_LOG_OBJECT (rtpxqtdepay,
307         "VER: %d, PCK: %d, S: %d, Q: %d, L: %d, D: %d, ID: %d", ver, pck, s, q,
308         l, d, rtpxqtdepay->current_id);
309 
310     if (rtpxqtdepay->need_resync) {
311       /* we need to find the boundary of a new packet after a DISCONT */
312       if (pck != 3 || q) {
313         /* non-fragmented packet or payload description present, packet starts
314          * here. */
315         rtpxqtdepay->need_resync = FALSE;
316       } else {
317         /* fragmented packet without description */
318         if (m) {
319           /* marker bit set, next packet is start of new one */
320           rtpxqtdepay->need_resync = FALSE;
321         }
322         goto need_resync;
323       }
324     }
325 
326     payload += 4;
327     payload_len -= 4;
328 
329     if (q) {
330       gboolean k, f, a, z;
331       guint pdlen, pdpadded;
332       gint padding;
333       /* media_type only used for printing */
334       guint32 G_GNUC_UNUSED media_type;
335       guint32 timescale;
336 
337       /*                      1                   2                   3
338        *  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
339        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
340        * |K|F|A|Z| RES                   | QuickTime Payload Desc Length |
341        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
342        * . QuickTime Payload Desc Data ... .
343        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
344        */
345       if (payload_len <= 4)
346         goto wrong_length;
347 
348       k = (payload[0] & 0x80) != 0;     /* keyframe */
349       f = (payload[0] & 0x40) != 0;     /* sparse */
350       a = (payload[0] & 0x20) != 0;     /* start of payload */
351       z = (payload[0] & 0x10) != 0;     /* end of payload */
352       pdlen = (payload[2] << 8) | payload[3];
353 
354       if (pdlen < 12)
355         goto wrong_length;
356 
357       /* calc padding */
358       pdpadded = pdlen + 3;
359       pdpadded -= pdpadded % 4;
360       if (payload_len < pdpadded)
361         goto wrong_length;
362 
363       padding = pdpadded - pdlen;
364       GST_LOG_OBJECT (rtpxqtdepay,
365           "K: %d, F: %d, A: %d, Z: %d, len: %d, padding %d", k, f, a, z, pdlen,
366           padding);
367 
368       payload += 4;
369       payload_len -= 4;
370       /*                      1                   2                   3
371        *  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
372        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
373        * | QuickTime Media Type                                          |
374        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
375        * | Timescale                                                     |
376        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377        * . QuickTime TLVs ... .
378        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379        */
380       media_type =
381           (payload[0] << 24) | (payload[1] << 16) | (payload[2] << 8) |
382           payload[3];
383       timescale =
384           (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) |
385           payload[7];
386 
387       GST_LOG_OBJECT (rtpxqtdepay, "media_type: %c%c%c%c, timescale %u",
388           payload[0], payload[1], payload[2], payload[3], timescale);
389 
390       payload += 8;
391       payload_len -= 8;
392       pdlen -= 12;
393 
394       /* parse TLV (type-length-value triplets */
395       while (pdlen > 3) {
396         guint16 tlv_len, tlv_type;
397 
398         /*                      1                   2                   3
399          *  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
400          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
401          * | QuickTime TLV Length          | QuickTime TLV Type            |
402          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
403          * . QuickTime TLV Value ... .
404          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
405          */
406         tlv_len = (payload[0] << 8) | payload[1];
407         tlv_type = (payload[2] << 8) | payload[3];
408         pdlen -= 4;
409         if (tlv_len > pdlen)
410           goto wrong_length;
411 
412         GST_LOG_OBJECT (rtpxqtdepay, "TLV '%c%c', len %d", payload[2],
413             payload[3], tlv_len);
414 
415         payload += 4;
416         payload_len -= 4;
417 
418         switch (tlv_type) {
419           case TLV_sd:
420             /* Session description */
421             if (!gst_rtp_quicktime_parse_sd (rtpxqtdepay, payload, tlv_len))
422               goto unknown_format;
423             rtpxqtdepay->have_sd = TRUE;
424             break;
425           case TLV_qt:
426           case TLV_ti:
427           case TLV_ly:
428           case TLV_vo:
429           case TLV_mx:
430           case TLV_tr:
431           case TLV_tw:
432           case TLV_th:
433           case TLV_la:
434           case TLV_rt:
435           case TLV_gm:
436           case TLV_oc:
437           case TLV_cr:
438           case TLV_du:
439           case TLV_po:
440           default:
441             break;
442         }
443 
444         pdlen -= tlv_len;
445         payload += tlv_len;
446         payload_len -= tlv_len;
447       }
448       payload += padding;
449       payload_len -= padding;
450     }
451 
452     if (l) {
453       guint ssilen, ssipadded;
454       gint padding;
455 
456       /*                      1                   2                   3
457        *  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
458        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
459        * | RES                           | Sample-Specific Info Length   |
460        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
461        * . QuickTime TLVs ...
462        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
463        */
464       if (payload_len <= 4)
465         goto wrong_length;
466 
467       ssilen = (payload[2] << 8) | payload[3];
468       if (ssilen < 4)
469         goto wrong_length;
470 
471       /* calc padding */
472       ssipadded = ssilen + 3;
473       ssipadded -= ssipadded % 4;
474       if (payload_len < ssipadded)
475         goto wrong_length;
476 
477       padding = ssipadded - ssilen;
478       GST_LOG_OBJECT (rtpxqtdepay, "len: %d, padding %d", ssilen, padding);
479 
480       payload += 4;
481       payload_len -= 4;
482       ssilen -= 4;
483 
484       /* parse TLV (type-length-value triplets */
485       while (ssilen > 3) {
486         guint16 tlv_len, tlv_type;
487 
488         /*                      1                   2                   3
489          *  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
490          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491          * | QuickTime TLV Length          | QuickTime TLV Type            |
492          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493          * . QuickTime TLV Value ... .
494          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
495          */
496         tlv_len = (payload[0] << 8) | payload[1];
497         tlv_type = (payload[2] << 8) | payload[3];
498         ssilen -= 4;
499         if (tlv_len > ssilen)
500           goto wrong_length;
501 
502         GST_LOG_OBJECT (rtpxqtdepay, "TLV '%c%c', len %d", payload[2],
503             payload[3], tlv_len);
504 
505         payload += 4;
506         payload_len -= 4;
507 
508         switch (tlv_type) {
509           case TLV_sd:
510           case TLV_qt:
511           case TLV_ti:
512           case TLV_ly:
513           case TLV_vo:
514           case TLV_mx:
515           case TLV_tr:
516           case TLV_tw:
517           case TLV_th:
518           case TLV_la:
519           case TLV_rt:
520           case TLV_gm:
521           case TLV_oc:
522           case TLV_cr:
523           case TLV_du:
524           case TLV_po:
525           default:
526             break;
527         }
528 
529         ssilen -= tlv_len;
530         payload += tlv_len;
531         payload_len -= tlv_len;
532       }
533       payload += padding;
534       payload_len -= padding;
535     }
536 
537     rtpxqtdepay->previous_id = rtpxqtdepay->current_id;
538 
539     switch (pck) {
540       case 1:
541       {
542         /* multiple samples per packet. */
543         outbuf = gst_buffer_new_and_alloc (payload_len);
544         gst_buffer_fill (outbuf, 0, payload, payload_len);
545 
546         goto done;
547       }
548       case 2:
549       {
550         guint slen;
551 
552         /* multiple samples per packet.
553          *                      1                   2                   3
554          *  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
555          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
556          * |S| Reserved                    | Sample Length                 |
557          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
558          * | Sample Timestamp                                              |
559          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
560          * . Sample Data ...                                               .
561          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
562          * |S| Reserved                    | Sample Length                 |
563          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
564          * | Sample Timestamp                                              |
565          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
566          * . Sample Data ...                                               .
567          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
568          * . ......                                                        .
569          * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
570          */
571         while (payload_len > 8) {
572           s = (payload[0] & 0x80) != 0; /* contains sync sample */
573           slen = (payload[2] << 8) | payload[3];
574           /* timestamp =
575            *    (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) |
576            *    payload[7];
577            */
578 
579           payload += 8;
580           payload_len -= 8;
581 
582           if (slen > payload_len)
583             slen = payload_len;
584 
585           outbuf = gst_buffer_new_and_alloc (slen);
586           gst_buffer_fill (outbuf, 0, payload, slen);
587           if (!s)
588             GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
589 
590           gst_rtp_base_depayload_push (depayload, outbuf);
591 
592           /* aligned on 32 bit boundary */
593           slen = GST_ROUND_UP_4 (slen);
594 
595           payload += slen;
596           payload_len -= slen;
597         }
598         break;
599       }
600       case 3:
601       {
602         /* one sample per packet, use adapter to combine based on marker bit. */
603         outbuf = gst_buffer_new_and_alloc (payload_len);
604         gst_buffer_fill (outbuf, 0, payload, payload_len);
605 
606         gst_adapter_push (rtpxqtdepay->adapter, outbuf);
607         outbuf = NULL;
608 
609         if (!m)
610           goto done;
611 
612         avail = gst_adapter_available (rtpxqtdepay->adapter);
613         outbuf = gst_adapter_take_buffer (rtpxqtdepay->adapter, avail);
614 
615         GST_DEBUG_OBJECT (rtpxqtdepay,
616             "gst_rtp_xqt_depay_chain: pushing buffer of size %u", avail);
617 
618         goto done;
619       }
620     }
621   }
622 
623 done:
624   gst_rtp_buffer_unmap (&rtp);
625   return outbuf;
626 
627 need_resync:
628   {
629     GST_DEBUG_OBJECT (rtpxqtdepay, "waiting for marker");
630     goto done;
631   }
632 wrong_version:
633   {
634     GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
635         ("Unknown payload version."), (NULL));
636     goto done;
637   }
638 pck_reserved:
639   {
640     GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
641         ("PCK reserved 0."), (NULL));
642     goto done;
643   }
644 wrong_length:
645   {
646     GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
647         ("Wrong payload length."), (NULL));
648     goto done;
649   }
650 unknown_format:
651   {
652     GST_ELEMENT_WARNING (rtpxqtdepay, STREAM, DECODE,
653         ("Unknown payload format."), (NULL));
654     goto done;
655   }
656 }
657 
658 static GstStateChangeReturn
gst_rtp_xqt_depay_change_state(GstElement * element,GstStateChange transition)659 gst_rtp_xqt_depay_change_state (GstElement * element, GstStateChange transition)
660 {
661   GstRtpXQTDepay *rtpxqtdepay;
662   GstStateChangeReturn ret;
663 
664   rtpxqtdepay = GST_RTP_XQT_DEPAY (element);
665 
666   switch (transition) {
667     case GST_STATE_CHANGE_READY_TO_PAUSED:
668       gst_adapter_clear (rtpxqtdepay->adapter);
669       rtpxqtdepay->previous_id = -1;
670       rtpxqtdepay->current_id = -1;
671       rtpxqtdepay->need_resync = TRUE;
672       rtpxqtdepay->have_sd = FALSE;
673       break;
674     default:
675       break;
676   }
677 
678   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
679 
680   switch (transition) {
681     case GST_STATE_CHANGE_PAUSED_TO_READY:
682       gst_adapter_clear (rtpxqtdepay->adapter);
683     default:
684       break;
685   }
686   return ret;
687 }
688