gst/rtp/gstrtpmp4gdepay.c: Change some of the ranges in the caps, mostly for the...
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpmp4gdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> 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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpmp4gdepay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4gdepay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4gdepay_debug)
32
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>");
39
40 static GstStaticPadTemplate gst_rtp_mp4g_depay_src_template =
41     GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("video/mpeg,"
45         "mpegversion=(int) 4,"
46         "systemstream=(boolean)false;" "audio/mpeg," "mpegversion=(int) 4")
47     );
48
49 static GstStaticPadTemplate gst_rtp_mp4g_depay_sink_template =
50 GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
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, 32], " */
71         /* "indexlength = (string) [1, 32], " */
72         /* "indexdeltalength = (string) [1, 32], " */
73         /* "ctsdeltalength = (string) [1, 32], " */
74         /* "dtsdeltalength = (string) [1, 32], " */
75         /* "randomaccessindication = (string) {0, 1}, " */
76         /* "streamstateindication = (string) [0, 32], " */
77         /* "auxiliarydatasizelength = (string) [0, 32]" */ )
78     );
79
80 /* simple bitstream parser */
81 typedef struct
82 {
83   const guint8 *data;
84   const guint8 *end;
85   gint head;                    /* bitpos in the cache of next bit */
86   guint64 cache;                /* cached bytes */
87 } GstBsParse;
88
89 static void
90 gst_bs_parse_init (GstBsParse * bs, const guint8 * data, guint size)
91 {
92   bs->data = data;
93   bs->end = data + size;
94   bs->head = 0;
95   bs->cache = 0xffffffff;
96 }
97
98 static guint32
99 gst_bs_parse_read (GstBsParse * bs, guint n)
100 {
101   guint32 res = 0;
102   gint shift;
103
104   if (n == 0)
105     return res;
106
107   /* fill up the cache if we need to */
108   while (bs->head < n) {
109     if (bs->data >= bs->end) {
110       /* we're at the end, can't produce more than head number of bits */
111       n = bs->head;
112       break;
113     }
114     /* shift bytes in cache, moving the head bits of the cache left */
115     bs->cache = (bs->cache << 8) | *bs->data++;
116     bs->head += 8;
117   }
118
119   /* bring the required bits down and truncate */
120   if ((shift = bs->head - n) > 0)
121     res = bs->cache >> shift;
122   else
123     res = bs->cache;
124
125   /* mask out required bits */
126   if (n < 32)
127     res &= (1 << n) - 1;
128
129   bs->head = shift;
130
131   return res;
132 }
133
134
135 GST_BOILERPLATE (GstRtpMP4GDepay, gst_rtp_mp4g_depay, GstBaseRTPDepayload,
136     GST_TYPE_BASE_RTP_DEPAYLOAD);
137
138 static void gst_rtp_mp4g_depay_finalize (GObject * object);
139
140 static gboolean gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload,
141     GstCaps * caps);
142 static GstBuffer *gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload,
143     GstBuffer * buf);
144
145 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
146     element, GstStateChange transition);
147
148
149 static void
150 gst_rtp_mp4g_depay_base_init (gpointer klass)
151 {
152   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
153
154   gst_element_class_add_pad_template (element_class,
155       gst_static_pad_template_get (&gst_rtp_mp4g_depay_src_template));
156   gst_element_class_add_pad_template (element_class,
157       gst_static_pad_template_get (&gst_rtp_mp4g_depay_sink_template));
158
159   gst_element_class_set_details (element_class, &gst_rtp_mp4gdepay_details);
160 }
161
162 static void
163 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
164 {
165   GObjectClass *gobject_class;
166   GstElementClass *gstelement_class;
167   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
168
169   gobject_class = (GObjectClass *) klass;
170   gstelement_class = (GstElementClass *) klass;
171   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
172
173   parent_class = g_type_class_peek_parent (klass);
174
175   gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
176
177   gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
178
179   gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
180   gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
181
182   GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
183       "MP4-generic RTP Depayloader");
184 }
185
186 static void
187 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay,
188     GstRtpMP4GDepayClass * klass)
189 {
190   rtpmp4gdepay->adapter = gst_adapter_new ();
191 }
192
193 static void
194 gst_rtp_mp4g_depay_finalize (GObject * object)
195 {
196   GstRtpMP4GDepay *rtpmp4gdepay;
197
198   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
199
200   g_object_unref (rtpmp4gdepay->adapter);
201   rtpmp4gdepay->adapter = NULL;
202
203   G_OBJECT_CLASS (parent_class)->finalize (object);
204 }
205
206 static gint
207 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
208     gint def)
209 {
210   const gchar *str;
211   gint res;
212
213   if ((str = gst_structure_get_string (structure, field)))
214     return atoi (str);
215
216   if (gst_structure_get_int (structure, field, &res))
217     return res;
218
219   return def;
220 }
221
222 static gboolean
223 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
224 {
225
226   GstStructure *structure;
227   GstRtpMP4GDepay *rtpmp4gdepay;
228   GstCaps *srccaps = NULL;
229   const gchar *str;
230   gint clock_rate = 90000;      /* default */
231   gint someint;
232
233   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
234
235   structure = gst_caps_get_structure (caps, 0);
236
237   gst_structure_get_int (structure, "clock-rate", &clock_rate);
238   depayload->clock_rate = clock_rate;
239
240   if ((str = gst_structure_get_string (structure, "media"))) {
241     if (strcmp (str, "audio") == 0) {
242       srccaps = gst_caps_new_simple ("audio/mpeg",
243           "mpegversion", G_TYPE_INT, 4, NULL);
244     } else if (strcmp (str, "video") == 0) {
245       srccaps = gst_caps_new_simple ("video/mpeg",
246           "mpegversion", G_TYPE_INT, 4,
247           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
248     }
249   }
250   if (srccaps == NULL)
251     goto unknown_media;
252
253   /* these values are optional and have a default value of 0 (no header) */
254   rtpmp4gdepay->sizelength =
255       gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
256   rtpmp4gdepay->indexlength =
257       gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
258   rtpmp4gdepay->indexdeltalength =
259       gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
260   rtpmp4gdepay->ctsdeltalength =
261       gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
262   rtpmp4gdepay->dtsdeltalength =
263       gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
264   someint =
265       gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
266   rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
267   rtpmp4gdepay->streamstateindication =
268       gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
269   rtpmp4gdepay->auxiliarydatasizelength =
270       gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
271
272   /* get config string */
273   if ((str = gst_structure_get_string (structure, "config"))) {
274     GValue v = { 0 };
275
276     g_value_init (&v, GST_TYPE_BUFFER);
277     if (gst_value_deserialize (&v, str)) {
278       GstBuffer *buffer;
279
280       buffer = gst_value_get_buffer (&v);
281       gst_caps_set_simple (srccaps,
282           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
283       g_value_unset (&v);
284     } else {
285       g_warning ("cannot convert config to buffer");
286     }
287   }
288
289   gst_pad_set_caps (depayload->srcpad, srccaps);
290   gst_caps_unref (srccaps);
291
292   return TRUE;
293
294   /* ERRORS */
295 unknown_media:
296   {
297     GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
298     return FALSE;
299   }
300 }
301
302 static GstBuffer *
303 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
304 {
305   GstRtpMP4GDepay *rtpmp4gdepay;
306   GstBuffer *outbuf;
307
308   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
309
310   if (!gst_rtp_buffer_validate (buf))
311     goto bad_packet;
312
313   /* flush remaining data on discont */
314   if (GST_BUFFER_IS_DISCONT (buf)) {
315     gst_adapter_clear (rtpmp4gdepay->adapter);
316   }
317
318   {
319     gint payload_len, payload_AU;
320     guint8 *payload;
321     guint32 timestamp;
322     guint AU_headers_len;
323     guint AU_size, AU_index, payload_AU_size;
324     gboolean M;
325
326     payload_len = gst_rtp_buffer_get_payload_len (buf);
327     payload = gst_rtp_buffer_get_payload (buf);
328
329     timestamp = gst_rtp_buffer_get_timestamp (buf);
330     M = gst_rtp_buffer_get_marker (buf);
331
332     if (rtpmp4gdepay->sizelength > 0) {
333       gint num_AU_headers, AU_headers_bytes, i;
334       GstBsParse bs;
335
336       if (payload_len < 2)
337         goto short_payload;
338
339       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
340        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
341        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
342        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
343        *
344        * The length is 2 bytes and contains the length of the following
345        * AU-headers in bits.
346        */
347       AU_headers_len = (payload[0] << 8) | payload[1];
348       AU_headers_bytes = (AU_headers_len + 7) / 8;
349       num_AU_headers = AU_headers_len / 16;
350
351       GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
352           AU_headers_len, AU_headers_bytes, num_AU_headers);
353
354       /* skip header */
355       payload += 2;
356       payload_len -= 2;
357
358       if (payload_len < AU_headers_bytes)
359         goto short_payload;
360
361       /* skip special headers, point to first payload AU */
362       payload_AU = 2 + AU_headers_bytes;
363       payload_AU_size = payload_len - AU_headers_bytes;
364
365       /* point the bitstream parser to the first AU header bit */
366       gst_bs_parse_init (&bs, payload, payload_len);
367
368       for (i = 0; i < num_AU_headers && payload_AU_size > 0; i++) {
369         /* parse AU header
370          *  +---------------------------------------+
371          *  |     AU-size                           |
372          *  +---------------------------------------+
373          *  |     AU-Index / AU-Index-delta         |
374          *  +---------------------------------------+
375          *  |     CTS-flag                          |
376          *  +---------------------------------------+
377          *  |     CTS-delta                         |
378          *  +---------------------------------------+
379          *  |     DTS-flag                          |
380          *  +---------------------------------------+
381          *  |     DTS-delta                         |
382          *  +---------------------------------------+
383          *  |     RAP-flag                          |
384          *  +---------------------------------------+
385          *  |     Stream-state                      |
386          *  +---------------------------------------+
387          */
388         AU_size = gst_bs_parse_read (&bs, rtpmp4gdepay->sizelength);
389         if (i == 0)
390           AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexlength);
391         else
392           AU_index = gst_bs_parse_read (&bs, rtpmp4gdepay->indexdeltalength);
393         if (rtpmp4gdepay->ctsdeltalength > 0) {
394           if (gst_bs_parse_read (&bs, 1))
395             gst_bs_parse_read (&bs, rtpmp4gdepay->ctsdeltalength);
396         }
397         if (rtpmp4gdepay->dtsdeltalength > 0) {
398           if (gst_bs_parse_read (&bs, 1))
399             gst_bs_parse_read (&bs, rtpmp4gdepay->dtsdeltalength);
400         }
401         if (rtpmp4gdepay->randomaccessindication)
402           gst_bs_parse_read (&bs, 1);
403         if (rtpmp4gdepay->streamstateindication > 0)
404           gst_bs_parse_read (&bs, rtpmp4gdepay->streamstateindication);
405
406         GST_DEBUG_OBJECT (rtpmp4gdepay, "size %d, index %d", AU_size, AU_index);
407
408         /* fragmented pakets have the AU_size set to the size of the
409          * unfragmented AU. */
410         if (AU_size > payload_AU_size)
411           AU_size = payload_AU_size;
412
413         /* collect stuff in the adapter, strip header from payload and push in
414          * the adapter */
415         outbuf =
416             gst_rtp_buffer_get_payload_subbuffer (buf, payload_AU, AU_size);
417         gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
418
419         if (M) {
420           guint avail;
421
422           /* packet is complete, flush */
423           avail = gst_adapter_available (rtpmp4gdepay->adapter);
424
425           outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
426           gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
427
428           GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
429               GST_BUFFER_SIZE (outbuf));
430
431           /* only apply the timestamp for the first buffer */
432           if (i == 0)
433             gst_base_rtp_depayload_push_ts (depayload, timestamp, outbuf);
434           else
435             gst_base_rtp_depayload_push (depayload, outbuf);
436         }
437         payload_AU += AU_size;
438         payload_AU_size -= AU_size;
439       }
440     } else {
441       /* push complete buffer in adapter */
442       outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 0, payload_len);
443       gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
444
445       /* if this was the last packet of the VOP, create and push a buffer */
446       if (M) {
447         guint avail;
448
449         avail = gst_adapter_available (rtpmp4gdepay->adapter);
450
451         outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
452         gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
453
454         GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
455             GST_BUFFER_SIZE (outbuf));
456
457         return outbuf;
458       }
459     }
460   }
461   return NULL;
462
463   /* ERRORS */
464 bad_packet:
465   {
466     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
467         ("Packet did not validate."), (NULL));
468     return NULL;
469   }
470 short_payload:
471   {
472     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
473         ("Packet payload was too short."), (NULL));
474     return NULL;
475   }
476 }
477
478 static GstStateChangeReturn
479 gst_rtp_mp4g_depay_change_state (GstElement * element,
480     GstStateChange transition)
481 {
482   GstRtpMP4GDepay *rtpmp4gdepay;
483   GstStateChangeReturn ret;
484
485   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
486
487   switch (transition) {
488     case GST_STATE_CHANGE_READY_TO_PAUSED:
489       gst_adapter_clear (rtpmp4gdepay->adapter);
490       break;
491     default:
492       break;
493   }
494
495   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
496
497   switch (transition) {
498     default:
499       break;
500   }
501   return ret;
502 }
503
504 gboolean
505 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
506 {
507   return gst_element_register (plugin, "rtpmp4gdepay",
508       GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);
509 }