fixes: #514889
[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, 16], " *//* max 16 bits, should be enough... */
71         /* "indexlength = (string) [1, 8], " */
72         /* "indexdeltalength = (string) [1, 8], " */
73         /* "ctsdeltalength = (string) [1, 64], " */
74         /* "dtsdeltalength = (string) [1, 64], " */
75         /* "randomaccessindication = (string) {0, 1}, " */
76         /* "streamstateindication = (string) [0, 64], " */
77         /* "auxiliarydatasizelength = (string) [0, 64]" */ )
78     );
79
80 GST_BOILERPLATE (GstRtpMP4GDepay, gst_rtp_mp4g_depay, GstBaseRTPDepayload,
81     GST_TYPE_BASE_RTP_DEPAYLOAD);
82
83 static void gst_rtp_mp4g_depay_finalize (GObject * object);
84
85 static gboolean gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload,
86     GstCaps * caps);
87 static GstBuffer *gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload,
88     GstBuffer * buf);
89
90 static GstStateChangeReturn gst_rtp_mp4g_depay_change_state (GstElement *
91     element, GstStateChange transition);
92
93
94 static void
95 gst_rtp_mp4g_depay_base_init (gpointer klass)
96 {
97   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
98
99   gst_element_class_add_pad_template (element_class,
100       gst_static_pad_template_get (&gst_rtp_mp4g_depay_src_template));
101   gst_element_class_add_pad_template (element_class,
102       gst_static_pad_template_get (&gst_rtp_mp4g_depay_sink_template));
103
104   gst_element_class_set_details (element_class, &gst_rtp_mp4gdepay_details);
105 }
106
107 static void
108 gst_rtp_mp4g_depay_class_init (GstRtpMP4GDepayClass * klass)
109 {
110   GObjectClass *gobject_class;
111   GstElementClass *gstelement_class;
112   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
113
114   gobject_class = (GObjectClass *) klass;
115   gstelement_class = (GstElementClass *) klass;
116   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
117
118   parent_class = g_type_class_peek_parent (klass);
119
120   gobject_class->finalize = gst_rtp_mp4g_depay_finalize;
121
122   gstelement_class->change_state = gst_rtp_mp4g_depay_change_state;
123
124   gstbasertpdepayload_class->process = gst_rtp_mp4g_depay_process;
125   gstbasertpdepayload_class->set_caps = gst_rtp_mp4g_depay_setcaps;
126
127   GST_DEBUG_CATEGORY_INIT (rtpmp4gdepay_debug, "rtpmp4gdepay", 0,
128       "MP4-generic RTP Depayloader");
129 }
130
131 static void
132 gst_rtp_mp4g_depay_init (GstRtpMP4GDepay * rtpmp4gdepay,
133     GstRtpMP4GDepayClass * klass)
134 {
135   rtpmp4gdepay->adapter = gst_adapter_new ();
136 }
137
138 static void
139 gst_rtp_mp4g_depay_finalize (GObject * object)
140 {
141   GstRtpMP4GDepay *rtpmp4gdepay;
142
143   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (object);
144
145   g_object_unref (rtpmp4gdepay->adapter);
146   rtpmp4gdepay->adapter = NULL;
147
148   G_OBJECT_CLASS (parent_class)->finalize (object);
149 }
150
151 static gint
152 gst_rtp_mp4g_depay_parse_int (GstStructure * structure, const gchar * field,
153     gint def)
154 {
155   const gchar *str;
156   gint res;
157
158   if ((str = gst_structure_get_string (structure, field)))
159     return atoi (str);
160
161   if (gst_structure_get_int (structure, field, &res))
162     return res;
163
164   return def;
165 }
166
167 static gboolean
168 gst_rtp_mp4g_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
169 {
170
171   GstStructure *structure;
172   GstRtpMP4GDepay *rtpmp4gdepay;
173   GstCaps *srccaps = NULL;
174   const gchar *str;
175   gint clock_rate = 90000;      /* default */
176   gint someint;
177
178   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
179
180   structure = gst_caps_get_structure (caps, 0);
181
182   gst_structure_get_int (structure, "clock-rate", &clock_rate);
183   depayload->clock_rate = clock_rate;
184
185   if ((str = gst_structure_get_string (structure, "media"))) {
186     if (strcmp (str, "audio") == 0) {
187       srccaps = gst_caps_new_simple ("audio/mpeg",
188           "mpegversion", G_TYPE_INT, 4, NULL);
189     } else if (strcmp (str, "video") == 0) {
190       srccaps = gst_caps_new_simple ("video/mpeg",
191           "mpegversion", G_TYPE_INT, 4,
192           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
193     }
194   }
195   if (srccaps == NULL)
196     goto unknown_media;
197
198   /* these values are optional and have a default value of 0 (no header) */
199   rtpmp4gdepay->sizelength =
200       gst_rtp_mp4g_depay_parse_int (structure, "sizelength", 0);
201   rtpmp4gdepay->indexlength =
202       gst_rtp_mp4g_depay_parse_int (structure, "indexlength", 0);
203   rtpmp4gdepay->indexdeltalength =
204       gst_rtp_mp4g_depay_parse_int (structure, "indexdeltalength", 0);
205   rtpmp4gdepay->ctsdeltalength =
206       gst_rtp_mp4g_depay_parse_int (structure, "ctsdeltalength", 0);
207   rtpmp4gdepay->dtsdeltalength =
208       gst_rtp_mp4g_depay_parse_int (structure, "dtsdeltalength", 0);
209   someint =
210       gst_rtp_mp4g_depay_parse_int (structure, "randomaccessindication", 0);
211   rtpmp4gdepay->randomaccessindication = someint > 0 ? 1 : 0;
212   rtpmp4gdepay->streamstateindication =
213       gst_rtp_mp4g_depay_parse_int (structure, "streamstateindication", 0);
214   rtpmp4gdepay->auxiliarydatasizelength =
215       gst_rtp_mp4g_depay_parse_int (structure, "auxiliarydatasizelength", 0);
216
217   /* get config string */
218   if ((str = gst_structure_get_string (structure, "config"))) {
219     GValue v = { 0 };
220
221     g_value_init (&v, GST_TYPE_BUFFER);
222     if (gst_value_deserialize (&v, str)) {
223       GstBuffer *buffer;
224
225       buffer = gst_value_get_buffer (&v);
226       gst_caps_set_simple (srccaps,
227           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
228       g_value_unset (&v);
229     } else {
230       g_warning ("cannot convert config to buffer");
231     }
232   }
233
234   gst_pad_set_caps (depayload->srcpad, srccaps);
235   gst_caps_unref (srccaps);
236
237   return TRUE;
238
239   /* ERRORS */
240 unknown_media:
241   {
242     GST_DEBUG_OBJECT (rtpmp4gdepay, "Unknown media type");
243     return FALSE;
244   }
245 }
246
247 static GstBuffer *
248 gst_rtp_mp4g_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
249 {
250   GstRtpMP4GDepay *rtpmp4gdepay;
251   GstBuffer *outbuf;
252
253   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (depayload);
254
255   if (!gst_rtp_buffer_validate (buf))
256     goto bad_packet;
257
258   /* flush remaining data on discont */
259   if (GST_BUFFER_IS_DISCONT (buf)) {
260     gst_adapter_clear (rtpmp4gdepay->adapter);
261   }
262
263   {
264     gint payload_len, payload_header;
265     guint8 *payload;
266     guint32 timestamp;
267     guint AU_headers_len;
268     guint AU_size, AU_index;
269     gboolean M;
270
271     payload_len = gst_rtp_buffer_get_payload_len (buf);
272     payload = gst_rtp_buffer_get_payload (buf);
273     payload_header = 0;
274
275     timestamp = gst_rtp_buffer_get_timestamp (buf);
276     M = gst_rtp_buffer_get_marker (buf);
277
278     if (rtpmp4gdepay->sizelength > 0) {
279       gint num_AU_headers, AU_headers_bytes, i;
280
281       /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
282        * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
283        * |                 |   (1)   |   (2)   |      |   (n) * | bits  |
284        * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
285        *
286        * The length is 2 bytes and contains the length of the following
287        * AU-headers in bits.
288        */
289       AU_headers_len = (payload[0] << 8) | payload[1];
290       AU_headers_bytes = (AU_headers_len + 7) / 8;
291       num_AU_headers = AU_headers_len / 16;
292
293       GST_DEBUG_OBJECT (rtpmp4gdepay, "AU headers len %d, bytes %d, num %d",
294           AU_headers_len, AU_headers_bytes, num_AU_headers);
295
296       /* skip header */
297       payload += 2;
298       /* skip special headers */
299       payload_header = 2 + AU_headers_bytes;
300
301       for (i = 0; i < num_AU_headers; i++) {
302         /* FIXME, use bits */
303         AU_size = ((payload[0] << 8) | payload[1]) >> 3;
304         AU_index = payload[1] & 0x7;
305         payload += 2;
306
307         GST_DEBUG_OBJECT (rtpmp4gdepay, "len, %d, size %d, index %d",
308             AU_headers_len, AU_size, AU_index);
309
310         /* collect stuff in the adapter, strip header from payload and push in
311          * the adapter */
312         outbuf =
313             gst_rtp_buffer_get_payload_subbuffer (buf, payload_header, AU_size);
314         gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
315
316         if (M) {
317           guint avail;
318
319           /* packet is complete, flush */
320           avail = gst_adapter_available (rtpmp4gdepay->adapter);
321
322           outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
323           gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
324
325           GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
326               GST_BUFFER_SIZE (outbuf));
327
328           /* only apply the timestamp for the first buffer */
329           if (i == 0)
330             gst_base_rtp_depayload_push_ts (depayload, timestamp, outbuf);
331           else
332             gst_base_rtp_depayload_push (depayload, outbuf);
333         }
334         payload_header += AU_size;
335       }
336     } else {
337       /* push complete buffer in adapter */
338       outbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 0, payload_len);
339       gst_adapter_push (rtpmp4gdepay->adapter, outbuf);
340
341       /* if this was the last packet of the VOP, create and push a buffer */
342       if (M) {
343         guint avail;
344
345         avail = gst_adapter_available (rtpmp4gdepay->adapter);
346
347         outbuf = gst_adapter_take_buffer (rtpmp4gdepay->adapter, avail);
348         gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
349
350         GST_DEBUG ("gst_rtp_mp4g_depay_chain: pushing buffer of size %d",
351             GST_BUFFER_SIZE (outbuf));
352
353         return outbuf;
354       }
355     }
356   }
357   return NULL;
358
359   /* ERRORS */
360 bad_packet:
361   {
362     GST_ELEMENT_WARNING (rtpmp4gdepay, STREAM, DECODE,
363         ("Packet did not validate."), (NULL));
364     return NULL;
365   }
366 }
367
368 static GstStateChangeReturn
369 gst_rtp_mp4g_depay_change_state (GstElement * element,
370     GstStateChange transition)
371 {
372   GstRtpMP4GDepay *rtpmp4gdepay;
373   GstStateChangeReturn ret;
374
375   rtpmp4gdepay = GST_RTP_MP4G_DEPAY (element);
376
377   switch (transition) {
378     case GST_STATE_CHANGE_READY_TO_PAUSED:
379       gst_adapter_clear (rtpmp4gdepay->adapter);
380       break;
381     default:
382       break;
383   }
384
385   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
386
387   switch (transition) {
388     default:
389       break;
390   }
391   return ret;
392 }
393
394 gboolean
395 gst_rtp_mp4g_depay_plugin_init (GstPlugin * plugin)
396 {
397   return gst_element_register (plugin, "rtpmp4gdepay",
398       GST_RANK_MARGINAL, GST_TYPE_RTP_MP4G_DEPAY);
399 }