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