Move files from gst-plugins-ugly into the "subprojects/gst-plugins-ugly/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-ugly / gst / asfdemux / gstrtpasfdepay.c
1 /* GStreamer RTP ASF depayloader
2  * Copyright (C) 2006 Tim-Philipp Müller  <tim centricular net>
3  *               2009 Wim Taymans  <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "gstasfelements.h"
26 #include "gstrtpasfdepay.h"
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 GST_DEBUG_CATEGORY_STATIC (rtpasfdepayload_debug);
33 #define GST_CAT_DEFAULT rtpasfdepayload_debug
34
35 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
36     GST_PAD_SRC,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("video/x-ms-asf")
39     );
40
41 /* Other parameters: config, maxps */
42 #define SINK_CAPS \
43   "application/x-rtp, "                                          \
44   "media = (string) { \"application\", \"video\", \"audio\" }, " \
45   "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "         \
46   "clock-rate = (int) [1, MAX ], "                               \
47   "encoding-name = (string) \"X-ASF-PF\""
48
49 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS (SINK_CAPS)
53     );
54
55 #define gst_rtp_asf_depay_parent_class parent_class
56 G_DEFINE_TYPE (GstRtpAsfDepay, gst_rtp_asf_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
57 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpasfdepay, "rtpasfdepay",
58     GST_RANK_MARGINAL, GST_TYPE_RTP_ASF_DEPAY, asf_element_init (plugin));
59
60 static void gst_rtp_asf_depay_finalize (GObject * object);
61
62 static GstStateChangeReturn gst_rtp_asf_depay_change_state (GstElement *
63     element, GstStateChange transition);
64
65 static gboolean gst_rtp_asf_depay_setcaps (GstRTPBaseDepayload * depay,
66     GstCaps * caps);
67 static GstBuffer *gst_rtp_asf_depay_process (GstRTPBaseDepayload * basedepay,
68     GstBuffer * buf);
69
70 static void
71 gst_rtp_asf_depay_class_init (GstRtpAsfDepayClass * klass)
72 {
73   GObjectClass *gobject_class;
74   GstElementClass *gstelement_class;
75   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
76
77   gobject_class = (GObjectClass *) klass;
78   gstelement_class = (GstElementClass *) klass;
79   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
80
81   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
82   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
83
84   gst_element_class_set_static_metadata (gstelement_class,
85       "RTP ASF packet depayloader", "Codec/Depayloader/Network",
86       "Extracts ASF streams from RTP",
87       "Tim-Philipp Müller <tim centricular net>, "
88       "Wim Taymans <wim.taymans@gmail.com>");
89
90   gobject_class->finalize = gst_rtp_asf_depay_finalize;
91
92   gstelement_class->change_state =
93       GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_change_state);
94
95   gstrtpbasedepayload_class->set_caps =
96       GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_setcaps);
97   gstrtpbasedepayload_class->process =
98       GST_DEBUG_FUNCPTR (gst_rtp_asf_depay_process);
99
100   GST_DEBUG_CATEGORY_INIT (rtpasfdepayload_debug, "rtpasfdepayload", 0,
101       "RTP asf depayloader element");
102 }
103
104 static void
105 gst_rtp_asf_depay_init (GstRtpAsfDepay * depay)
106 {
107   depay->adapter = gst_adapter_new ();
108 }
109
110 static void
111 gst_rtp_asf_depay_finalize (GObject * object)
112 {
113   GstRtpAsfDepay *depay;
114
115   depay = GST_RTP_ASF_DEPAY (object);
116
117   g_object_unref (depay->adapter);
118
119   G_OBJECT_CLASS (parent_class)->finalize (object);
120 }
121
122 static const guint8 asf_marker[16] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66,
123   0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c
124 };
125
126 static gboolean
127 gst_rtp_asf_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
128 {
129   GstRtpAsfDepay *depay;
130   GstStructure *s;
131   const gchar *config_str, *ps_string;
132   GstBuffer *buf;
133   GstCaps *src_caps;
134   guint8 *headers;
135   gsize headers_len;
136   gint clock_rate;
137
138   depay = GST_RTP_ASF_DEPAY (depayload);
139
140   s = gst_caps_get_structure (caps, 0);
141
142   if (!gst_structure_get_int (s, "clock-rate", &clock_rate) || clock_rate < 0)
143     clock_rate = 1000;
144   depayload->clock_rate = clock_rate;
145
146   /* config contains the asf headers in base64 coding */
147   config_str = gst_structure_get_string (s, "config");
148   if (config_str == NULL || *config_str == '\0')
149     goto no_config;
150
151   ps_string = gst_structure_get_string (s, "maxps");
152   if (ps_string == NULL || *ps_string == '\0')
153     goto no_packetsize;
154
155   if (depay->packet_size) {
156     /* header sent again following seek;
157      * discard to avoid confusing upstream */
158     if (depay->packet_size == atoi (ps_string)) {
159       goto duplicate_header;
160     } else {
161       /* since we should fiddle with downstream state to handle this */
162       goto refuse_renegotiation;
163     }
164   } else
165     depay->packet_size = atoi (ps_string);
166   if (depay->packet_size <= 16)
167     goto invalid_packetsize;
168
169   headers = (guint8 *) g_base64_decode (config_str, &headers_len);
170
171   if (headers == NULL || headers_len < 16
172       || memcmp (headers, asf_marker, 16) != 0)
173     goto invalid_headers;
174
175   src_caps = gst_caps_new_empty_simple ("video/x-ms-asf");
176   gst_pad_set_caps (depayload->srcpad, src_caps);
177   gst_caps_unref (src_caps);
178
179   buf = gst_buffer_new ();
180   gst_buffer_append_memory (buf,
181       gst_memory_new_wrapped (0, headers, headers_len, 0, headers_len, headers,
182           g_free));
183
184   gst_rtp_base_depayload_push (depayload, buf);
185
186   return TRUE;
187
188   /* ERRORS */
189 no_config:
190   {
191     GST_WARNING_OBJECT (depay, "caps without 'config' field with asf headers");
192     return FALSE;
193   }
194 no_packetsize:
195   {
196     GST_WARNING_OBJECT (depay, "caps without 'maxps' (packet size) field");
197     return FALSE;
198   }
199 invalid_packetsize:
200   {
201     GST_WARNING_OBJECT (depay, "packet size %u invalid", depay->packet_size);
202     return FALSE;
203   }
204 invalid_headers:
205   {
206     GST_WARNING_OBJECT (depay, "headers don't look like valid ASF headers");
207     g_free (headers);
208     return FALSE;
209   }
210 duplicate_header:
211   {
212     GST_DEBUG_OBJECT (depayload, "discarding duplicate header");
213     return TRUE;
214   }
215 refuse_renegotiation:
216   {
217     GST_WARNING_OBJECT (depayload, "cannot renegotiate to different header");
218     return FALSE;
219   }
220 }
221
222 static gint
223 field_size (guint8 field)
224 {
225   switch (field) {
226       /* DWORD - 32 bits */
227     case 3:
228       return 4;
229
230       /* WORD - 16 bits */
231     case 2:
232       return 2;
233
234       /* BYTE - 8 bits */
235     case 1:
236       return 1;
237
238       /* non-exitent */
239     case 0:
240     default:
241       return 0;
242   }
243 }
244
245 /* Set the padding field to te correct value as the spec
246  * says it should be se to 0 in the rtp packets
247  */
248 static GstBuffer *
249 gst_rtp_asf_depay_update_padding (GstRtpAsfDepay * depayload, GstBuffer * buf)
250 {
251   GstBuffer *result;
252   GstMapInfo map;
253   guint8 *data;
254   gint offset = 0;
255   guint8 aux;
256   guint8 seq_type;
257   guint8 pad_type;
258   guint8 pkt_type;
259   gsize plen, padding;
260
261   plen = gst_buffer_get_size (buf);
262   if (plen == depayload->packet_size)
263     return buf;
264
265   padding = depayload->packet_size - plen;
266
267   GST_LOG_OBJECT (depayload,
268       "padding buffer size %" G_GSIZE_FORMAT " to packet size %d", plen,
269       depayload->packet_size);
270
271   result = gst_buffer_new_and_alloc (depayload->packet_size);
272
273   gst_buffer_map (result, &map, GST_MAP_READ);
274   data = map.data;
275   memset (data + plen, 0, padding);
276
277   gst_buffer_extract (buf, 0, data, plen);
278   gst_buffer_unref (buf);
279
280   aux = data[offset++];
281   if (aux & 0x80) {
282     guint8 err_len = 0;
283     if (aux & 0x60) {
284       GST_WARNING_OBJECT (depayload, "Error correction length type should be "
285           "set to 0");
286       /* this packet doesn't follow the spec */
287       gst_buffer_unmap (result, &map);
288       return result;
289     }
290     err_len = aux & 0x0F;
291     offset += err_len;
292
293     aux = data[offset++];
294   }
295   seq_type = (aux >> 1) & 0x3;
296   pad_type = (aux >> 3) & 0x3;
297   pkt_type = (aux >> 5) & 0x3;
298
299   offset += 1;                  /* skip property flags */
300   offset += field_size (pkt_type);      /* skip packet length */
301   offset += field_size (seq_type);      /* skip sequence field */
302
303   /* write padding */
304   switch (pad_type) {
305       /* DWORD */
306     case 3:
307       GST_WRITE_UINT32_LE (&(data[offset]), padding);
308       break;
309
310       /* WORD */
311     case 2:
312       GST_WRITE_UINT16_LE (&(data[offset]), padding);
313       break;
314
315       /* BYTE */
316     case 1:
317       data[offset] = (guint8) padding;
318       break;
319
320       /* non-existent */
321     case 0:
322     default:
323       break;
324   }
325   gst_buffer_unmap (result, &map);
326
327   return result;
328 }
329
330 /* Docs: 'RTSP Protocol PDF' document from http://sdp.ppona.com/ (page 8) */
331
332 static GstBuffer *
333 gst_rtp_asf_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
334 {
335   GstRtpAsfDepay *depay;
336   const guint8 *payload;
337   GstBuffer *outbuf;
338   gboolean S, L, R, D, I;
339   guint payload_len, hdr_len, offset;
340   guint len_offs;
341   GstClockTime timestamp;
342   GstRTPBuffer rtpbuf = { NULL };
343
344   depay = GST_RTP_ASF_DEPAY (depayload);
345
346   /* flush remaining data on discont */
347   if (GST_BUFFER_IS_DISCONT (buf)) {
348     GST_LOG_OBJECT (depay, "got DISCONT");
349     gst_adapter_clear (depay->adapter);
350     depay->discont = TRUE;
351   }
352
353   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuf);
354   timestamp = GST_BUFFER_TIMESTAMP (buf);
355
356   payload_len = gst_rtp_buffer_get_payload_len (&rtpbuf);
357   payload = gst_rtp_buffer_get_payload (&rtpbuf);
358   offset = 0;
359
360   GST_LOG_OBJECT (depay, "got payload len of %u", payload_len);
361
362   do {
363     guint packet_len;
364
365     /* packet header is at least 4 bytes */
366     if (payload_len < 4)
367       goto too_small;
368
369     /*                      1                   2                   3
370      *  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 
371      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
372      * |S|L|R|D|I|RES  | Length/Offset                                 |
373      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374      * | Relative Timestamp (optional)                                 |
375      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
376      * | Duration (optional)                                           |
377      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
378      * | LocationId (optional)                                         |
379      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
380      *
381      * S: packet contains a keyframe.
382      * L: If 1, Length/Offset contains length, else contains the byte offset
383      *    of the fragment's first byte counted from the beginning of the
384      *    complete ASF data packet.
385      * R: relative timestamp present
386      * D: duration present
387      * I: locationid present
388      */
389
390     S = ((payload[0] & 0x80) != 0);
391     L = ((payload[0] & 0x40) != 0);
392     R = ((payload[0] & 0x20) != 0);
393     D = ((payload[0] & 0x10) != 0);
394     I = ((payload[0] & 0x08) != 0);
395
396     hdr_len = 4;
397
398     len_offs = (payload[1] << 16) | (payload[2] << 8) | payload[3];
399
400     if (R) {
401       GST_DEBUG ("Relative timestamp field present : %u",
402           GST_READ_UINT32_BE (payload + hdr_len));
403       hdr_len += 4;
404     }
405     if (D) {
406       GST_DEBUG ("Duration field present : %u",
407           GST_READ_UINT32_BE (payload + hdr_len));
408       hdr_len += 4;
409     }
410     if (I) {
411       GST_DEBUG ("LocationId field present : %u",
412           GST_READ_UINT32_BE (payload + hdr_len));
413       hdr_len += 4;
414     }
415
416     GST_LOG_OBJECT (depay, "S %d, L %d, R %d, D %d, I %d", S, L, R, D, I);
417     GST_LOG_OBJECT (depay, "payload_len:%d, hdr_len:%d, len_offs:%d",
418         payload_len, hdr_len, len_offs);
419
420     if (payload_len < hdr_len)
421       goto too_small;
422
423     /* skip headers */
424     payload_len -= hdr_len;
425     payload += hdr_len;
426     offset += hdr_len;
427
428     if (L) {
429       /* L bit set, len contains the length of the packet */
430       packet_len = len_offs;
431     } else {
432       /* else it contains an offset which we don't handle yet */
433       GST_LOG_OBJECT (depay, "We have a fragmented packet");
434       packet_len = payload_len;
435     }
436
437     if (packet_len > payload_len)
438       packet_len = payload_len;
439
440     GST_LOG_OBJECT (depay, "packet len %u, payload len %u, packet_size:%u",
441         packet_len, payload_len, depay->packet_size);
442
443     if (!L) {
444       guint available;
445       GstBuffer *sub;
446
447       /* Fragmented packet handling */
448       outbuf = NULL;
449
450       if (len_offs == (available = gst_adapter_available (depay->adapter))) {
451         /* fragment aligns with what we have, add it */
452         GST_LOG_OBJECT (depay, "collecting fragment");
453         sub =
454             gst_rtp_buffer_get_payload_subbuffer (&rtpbuf, offset, packet_len);
455         gst_adapter_push (depay->adapter, sub);
456         /* RTP marker bit M is set if this is last fragment */
457         if (gst_rtp_buffer_get_marker (&rtpbuf)) {
458           GST_LOG_OBJECT (depay, "last fragment, assembling packet");
459           outbuf =
460               gst_adapter_take_buffer (depay->adapter, available + packet_len);
461         }
462       } else {
463         if (available) {
464           GST_WARNING_OBJECT (depay, "Offset doesn't match previous data?!");
465           GST_DEBUG_OBJECT (depay, "clearing for re-sync");
466           gst_adapter_clear (depay->adapter);
467         } else
468           GST_DEBUG_OBJECT (depay, "waiting for start of packet");
469       }
470     } else {
471       GST_LOG_OBJECT (depay, "collecting packet");
472       outbuf =
473           gst_rtp_buffer_get_payload_subbuffer (&rtpbuf, offset, packet_len);
474     }
475
476     /* If we haven't completed a full ASF packet, return */
477     if (!outbuf)
478       return NULL;
479
480     outbuf = gst_rtp_asf_depay_update_padding (depay, outbuf);
481
482     if (!S)
483       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
484
485     if (depay->discont) {
486       GST_LOG_OBJECT (depay, "setting DISCONT");
487       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
488       depay->discont = FALSE;
489     }
490
491     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
492
493     gst_rtp_base_depayload_push (depayload, outbuf);
494
495     /* only apply the timestamp to the first buffer of this packet */
496     timestamp = -1;
497
498     /* skip packet data */
499     payload += packet_len;
500     offset += packet_len;
501     payload_len -= packet_len;
502   } while (payload_len > 0);
503
504   gst_rtp_buffer_unmap (&rtpbuf);
505
506   return NULL;
507
508 /* ERRORS */
509 too_small:
510   {
511     gst_rtp_buffer_unmap (&rtpbuf);
512     GST_WARNING_OBJECT (depayload, "Payload too small, expected at least 4 "
513         "bytes for header, but got only %d bytes", payload_len);
514     return NULL;
515   }
516 }
517
518 static GstStateChangeReturn
519 gst_rtp_asf_depay_change_state (GstElement * element, GstStateChange trans)
520 {
521   GstStateChangeReturn ret;
522   GstRtpAsfDepay *depay;
523
524   depay = GST_RTP_ASF_DEPAY (element);
525
526   switch (trans) {
527     case GST_STATE_CHANGE_READY_TO_PAUSED:
528       gst_adapter_clear (depay->adapter);
529       depay->discont = TRUE;
530       break;
531     default:
532       break;
533   }
534
535   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, trans);
536
537   switch (trans) {
538     case GST_STATE_CHANGE_PAUSED_TO_READY:
539       gst_adapter_clear (depay->adapter);
540       break;
541     default:
542       break;
543   }
544
545   return ret;
546 }