rtp: Fix unitialized compiler warnings on OS X build bot
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4gpay.c
1 /* GStreamer
2  * Copyright (C) <2006> Wim Taymans <wim.taymans@gmail.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
26 #include <gst/base/gstbitreader.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include "gstrtpmp4gpay.h"
30
31 GST_DEBUG_CATEGORY_STATIC (rtpmp4gpay_debug);
32 #define GST_CAT_DEFAULT (rtpmp4gpay_debug)
33
34 static GstStaticPadTemplate gst_rtp_mp4g_pay_sink_template =
35     GST_STATIC_PAD_TEMPLATE ("sink",
36     GST_PAD_SINK,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("video/mpeg,"
39         "mpegversion=(int) 4,"
40         "systemstream=(boolean)false;"
41         "audio/mpeg," "mpegversion=(int) 4, " "stream-format=(string) raw")
42     );
43
44 static GstStaticPadTemplate gst_rtp_mp4g_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "media = (string) { \"video\", \"audio\", \"application\" }, "
50         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51         "clock-rate = (int) [1, MAX ], "
52         "encoding-name = (string) \"MPEG4-GENERIC\", "
53         /* required string params */
54         "streamtype = (string) { \"4\", \"5\" }, "      /* 4 = video, 5 = audio */
55         /* "profile-level-id = (string) [1,MAX], " */
56         /* "config = (string) [1,MAX]" */
57         "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\" } "
58         /* Optional general parameters */
59         /* "objecttype = (string) [1,MAX], " */
60         /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
61         /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
62         /* "maxdisplacement = (string) [1,MAX], " */
63         /* "de-interleavebuffersize = (string) [1,MAX], " */
64         /* Optional configuration parameters */
65         /* "sizelength = (string) [1, 16], " *//* max 16 bits, should be enough... */
66         /* "indexlength = (string) [1, 8], " */
67         /* "indexdeltalength = (string) [1, 8], " */
68         /* "ctsdeltalength = (string) [1, 64], " */
69         /* "dtsdeltalength = (string) [1, 64], " */
70         /* "randomaccessindication = (string) {0, 1}, " */
71         /* "streamstateindication = (string) [0, 64], " */
72         /* "auxiliarydatasizelength = (string) [0, 64]" */ )
73     );
74
75
76 static void gst_rtp_mp4g_pay_finalize (GObject * object);
77
78 static GstStateChangeReturn gst_rtp_mp4g_pay_change_state (GstElement * element,
79     GstStateChange transition);
80
81 static gboolean gst_rtp_mp4g_pay_setcaps (GstBaseRTPPayload * payload,
82     GstCaps * caps);
83 static GstFlowReturn gst_rtp_mp4g_pay_handle_buffer (GstBaseRTPPayload *
84     payload, GstBuffer * buffer);
85
86 GST_BOILERPLATE (GstRtpMP4GPay, gst_rtp_mp4g_pay, GstBaseRTPPayload,
87     GST_TYPE_BASE_RTP_PAYLOAD)
88
89      static void gst_rtp_mp4g_pay_base_init (gpointer klass)
90 {
91   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
92
93   gst_element_class_add_pad_template (element_class,
94       gst_static_pad_template_get (&gst_rtp_mp4g_pay_src_template));
95   gst_element_class_add_pad_template (element_class,
96       gst_static_pad_template_get (&gst_rtp_mp4g_pay_sink_template));
97
98   gst_element_class_set_details_simple (element_class, "RTP MPEG4 ES payloader",
99       "Codec/Payloader/Network",
100       "Payload MPEG4 elementary streams as RTP packets (RFC 3640)",
101       "Wim Taymans <wim.taymans@gmail.com>");
102 }
103
104 static void
105 gst_rtp_mp4g_pay_class_init (GstRtpMP4GPayClass * klass)
106 {
107   GObjectClass *gobject_class;
108   GstElementClass *gstelement_class;
109   GstBaseRTPPayloadClass *gstbasertppayload_class;
110
111   gobject_class = (GObjectClass *) klass;
112   gstelement_class = (GstElementClass *) klass;
113   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
114
115   gobject_class->finalize = gst_rtp_mp4g_pay_finalize;
116
117   gstelement_class->change_state = gst_rtp_mp4g_pay_change_state;
118
119   gstbasertppayload_class->set_caps = gst_rtp_mp4g_pay_setcaps;
120   gstbasertppayload_class->handle_buffer = gst_rtp_mp4g_pay_handle_buffer;
121
122   GST_DEBUG_CATEGORY_INIT (rtpmp4gpay_debug, "rtpmp4gpay", 0,
123       "MP4-generic RTP Payloader");
124 }
125
126 static void
127 gst_rtp_mp4g_pay_init (GstRtpMP4GPay * rtpmp4gpay, GstRtpMP4GPayClass * klass)
128 {
129   rtpmp4gpay->adapter = gst_adapter_new ();
130 }
131
132 static void
133 gst_rtp_mp4g_pay_reset (GstRtpMP4GPay * rtpmp4gpay)
134 {
135   GST_DEBUG_OBJECT (rtpmp4gpay, "reset");
136
137   gst_adapter_clear (rtpmp4gpay->adapter);
138
139   g_free (rtpmp4gpay->params);
140   rtpmp4gpay->params = NULL;
141
142   if (rtpmp4gpay->config)
143     gst_buffer_unref (rtpmp4gpay->config);
144   rtpmp4gpay->config = NULL;
145
146   g_free (rtpmp4gpay->profile);
147   rtpmp4gpay->profile = NULL;
148
149   rtpmp4gpay->streamtype = NULL;
150   rtpmp4gpay->mode = NULL;
151
152   rtpmp4gpay->frame_len = 0;
153   rtpmp4gpay->offset = 0;
154 }
155
156 static void
157 gst_rtp_mp4g_pay_finalize (GObject * object)
158 {
159   GstRtpMP4GPay *rtpmp4gpay;
160
161   rtpmp4gpay = GST_RTP_MP4G_PAY (object);
162
163   gst_rtp_mp4g_pay_reset (rtpmp4gpay);
164
165   g_object_unref (rtpmp4gpay->adapter);
166   rtpmp4gpay->adapter = NULL;
167
168   G_OBJECT_CLASS (parent_class)->finalize (object);
169 }
170
171 static const unsigned int sampling_table[16] = {
172   96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
173   16000, 12000, 11025, 8000, 7350, 0, 0, 0
174 };
175
176 static gboolean
177 gst_rtp_mp4g_pay_parse_audio_config (GstRtpMP4GPay * rtpmp4gpay,
178     GstBuffer * buffer)
179 {
180   guint8 *data;
181   guint size;
182   guint8 objectType = 0;
183   guint8 samplingIdx = 0;
184   guint8 channelCfg = 0;
185   GstBitReader br;
186
187   data = GST_BUFFER_DATA (buffer);
188   size = GST_BUFFER_SIZE (buffer);
189
190   gst_bit_reader_init (&br, data, size);
191
192   /* any object type is fine, we need to copy it to the profile-level-id field. */
193   if (!gst_bit_reader_get_bits_uint8 (&br, &objectType, 5))
194     goto too_short;
195   if (objectType == 0)
196     goto invalid_object;
197
198   if (!gst_bit_reader_get_bits_uint8 (&br, &samplingIdx, 4))
199     goto too_short;
200   /* only fixed values for now */
201   if (samplingIdx > 12 && samplingIdx != 15)
202     goto wrong_freq;
203
204   if (!gst_bit_reader_get_bits_uint8 (&br, &channelCfg, 4))
205     goto too_short;
206   if (channelCfg > 7)
207     goto wrong_channels;
208
209   /* rtp rate depends on sampling rate of the audio */
210   if (samplingIdx == 15) {
211     guint32 rate = 0;
212
213     /* index of 15 means we get the rate in the next 24 bits */
214     if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
215       goto too_short;
216
217     rtpmp4gpay->rate = rate;
218   } else {
219     /* else use the rate from the table */
220     rtpmp4gpay->rate = sampling_table[samplingIdx];
221   }
222
223   rtpmp4gpay->frame_len = 1024;
224
225   switch (objectType) {
226     case 1:
227     case 2:
228     case 3:
229     case 4:
230     case 6:
231     case 7:
232     {
233       guint8 frameLenFlag = 0;
234
235       if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
236         if (frameLenFlag)
237           rtpmp4gpay->frame_len = 960;
238
239       break;
240     }
241     default:
242       break;
243   }
244
245   /* extra rtp params contain the number of channels */
246   g_free (rtpmp4gpay->params);
247   rtpmp4gpay->params = g_strdup_printf ("%d", channelCfg);
248   /* audio stream type */
249   rtpmp4gpay->streamtype = "5";
250   /* mode only high bitrate for now */
251   rtpmp4gpay->mode = "AAC-hbr";
252   /* profile */
253   g_free (rtpmp4gpay->profile);
254   rtpmp4gpay->profile = g_strdup_printf ("%d", objectType);
255
256   GST_DEBUG_OBJECT (rtpmp4gpay,
257       "objectType: %d, samplingIdx: %d (%d), channelCfg: %d, frame_len %d",
258       objectType, samplingIdx, rtpmp4gpay->rate, channelCfg,
259       rtpmp4gpay->frame_len);
260
261   return TRUE;
262
263   /* ERROR */
264 too_short:
265   {
266     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
267         (NULL), ("config string too short"));
268     return FALSE;
269   }
270 invalid_object:
271   {
272     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
273         (NULL), ("invalid object type"));
274     return FALSE;
275   }
276 wrong_freq:
277   {
278     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
279         (NULL), ("unsupported frequency index %d", samplingIdx));
280     return FALSE;
281   }
282 wrong_channels:
283   {
284     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
285         (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
286     return FALSE;
287   }
288 }
289
290 #define VOS_STARTCODE                   0x000001B0
291
292 static gboolean
293 gst_rtp_mp4g_pay_parse_video_config (GstRtpMP4GPay * rtpmp4gpay,
294     GstBuffer * buffer)
295 {
296   guint8 *data;
297   guint size;
298   guint32 code;
299
300   data = GST_BUFFER_DATA (buffer);
301   size = GST_BUFFER_SIZE (buffer);
302
303   if (size < 5)
304     goto too_short;
305
306   code = GST_READ_UINT32_BE (data);
307
308   g_free (rtpmp4gpay->profile);
309   if (code == VOS_STARTCODE) {
310     /* get profile */
311     rtpmp4gpay->profile = g_strdup_printf ("%d", (gint) data[4]);
312   } else {
313     GST_ELEMENT_WARNING (rtpmp4gpay, STREAM, FORMAT,
314         (NULL), ("profile not found in config string, assuming \'1\'"));
315     rtpmp4gpay->profile = g_strdup ("1");
316   }
317
318   /* fixed rate */
319   rtpmp4gpay->rate = 90000;
320   /* video stream type */
321   rtpmp4gpay->streamtype = "4";
322   /* no params for video */
323   rtpmp4gpay->params = NULL;
324   /* mode */
325   rtpmp4gpay->mode = "generic";
326
327   GST_LOG_OBJECT (rtpmp4gpay, "profile %s", rtpmp4gpay->profile);
328
329   return TRUE;
330
331   /* ERROR */
332 too_short:
333   {
334     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
335         (NULL), ("config string too short"));
336     return FALSE;
337   }
338 }
339
340 static gboolean
341 gst_rtp_mp4g_pay_new_caps (GstRtpMP4GPay * rtpmp4gpay)
342 {
343   gchar *config;
344   GValue v = { 0 };
345   gboolean res;
346
347 #define MP4GCAPS                                                \
348   "streamtype", G_TYPE_STRING, rtpmp4gpay->streamtype,          \
349   "profile-level-id", G_TYPE_STRING, rtpmp4gpay->profile,       \
350   "mode", G_TYPE_STRING, rtpmp4gpay->mode,                      \
351   "config", G_TYPE_STRING, config,                              \
352   "sizelength", G_TYPE_STRING, "13",                            \
353   "indexlength", G_TYPE_STRING, "3",                            \
354   "indexdeltalength", G_TYPE_STRING, "3",                       \
355   NULL
356
357   g_value_init (&v, GST_TYPE_BUFFER);
358   gst_value_set_buffer (&v, rtpmp4gpay->config);
359   config = gst_value_serialize (&v);
360
361   /* hmm, silly */
362   if (rtpmp4gpay->params) {
363     res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4gpay),
364         "encoding-params", G_TYPE_STRING, rtpmp4gpay->params, MP4GCAPS);
365   } else {
366     res = gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpmp4gpay),
367         MP4GCAPS);
368   }
369
370   g_value_unset (&v);
371   g_free (config);
372
373 #undef MP4GCAPS
374   return res;
375 }
376
377 static gboolean
378 gst_rtp_mp4g_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
379 {
380   GstRtpMP4GPay *rtpmp4gpay;
381   GstStructure *structure;
382   const GValue *codec_data;
383   const gchar *media_type = NULL;
384   gboolean res;
385
386   rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
387
388   structure = gst_caps_get_structure (caps, 0);
389
390   codec_data = gst_structure_get_value (structure, "codec_data");
391   if (codec_data) {
392     GST_LOG_OBJECT (rtpmp4gpay, "got codec_data");
393     if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
394       GstBuffer *buffer;
395       const gchar *name;
396
397       buffer = gst_value_get_buffer (codec_data);
398       GST_LOG_OBJECT (rtpmp4gpay, "configuring codec_data");
399
400       name = gst_structure_get_name (structure);
401
402       /* parse buffer */
403       if (!strcmp (name, "audio/mpeg")) {
404         res = gst_rtp_mp4g_pay_parse_audio_config (rtpmp4gpay, buffer);
405         media_type = "audio";
406       } else if (!strcmp (name, "video/mpeg")) {
407         res = gst_rtp_mp4g_pay_parse_video_config (rtpmp4gpay, buffer);
408         media_type = "video";
409       } else {
410         res = FALSE;
411       }
412       if (!res)
413         goto config_failed;
414
415       /* now we can configure the buffer */
416       if (rtpmp4gpay->config)
417         gst_buffer_unref (rtpmp4gpay->config);
418
419       rtpmp4gpay->config = gst_buffer_copy (buffer);
420     }
421   }
422   if (media_type == NULL)
423     goto config_failed;
424
425   gst_basertppayload_set_options (payload, media_type, TRUE, "MPEG4-GENERIC",
426       rtpmp4gpay->rate);
427
428   res = gst_rtp_mp4g_pay_new_caps (rtpmp4gpay);
429
430   return res;
431
432   /* ERRORS */
433 config_failed:
434   {
435     GST_DEBUG_OBJECT (rtpmp4gpay, "failed to parse config");
436     return FALSE;
437   }
438 }
439
440 static GstFlowReturn
441 gst_rtp_mp4g_pay_flush (GstRtpMP4GPay * rtpmp4gpay)
442 {
443   guint avail, total;
444   GstBuffer *outbuf;
445   GstFlowReturn ret;
446   gboolean fragmented;
447   guint mtu;
448
449   fragmented = FALSE;
450
451   /* the data available in the adapter is either smaller
452    * than the MTU or bigger. In the case it is smaller, the complete
453    * adapter contents can be put in one packet. In the case the
454    * adapter has more than one MTU, we need to fragment the MPEG data
455    * over multiple packets. */
456   total = avail = gst_adapter_available (rtpmp4gpay->adapter);
457
458   ret = GST_FLOW_OK;
459   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpmp4gpay);
460
461   while (avail > 0) {
462     guint towrite;
463     guint8 *payload;
464     guint payload_len;
465     guint packet_len;
466
467     /* this will be the total lenght of the packet */
468     packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
469
470     /* fill one MTU or all available bytes, we need 4 spare bytes for
471      * the AU header. */
472     towrite = MIN (packet_len, mtu - 4);
473
474     /* this is the payload length */
475     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
476
477     GST_DEBUG_OBJECT (rtpmp4gpay,
478         "avail %d, towrite %d, packet_len %d, payload_len %d", avail, towrite,
479         packet_len, payload_len);
480
481     /* create buffer to hold the payload, also make room for the 4 header bytes. */
482     outbuf = gst_rtp_buffer_new_allocate (payload_len + 4, 0, 0);
483
484     /* copy payload */
485     payload = gst_rtp_buffer_get_payload (outbuf);
486
487     /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
488      * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
489      * |                 |   (1)   |   (2)   |      |   (n)   | bits  |
490      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
491      */
492     /* AU-headers-length, we only have 1 AU-header */
493     payload[0] = 0x00;
494     payload[1] = 0x10;          /* we use 16 bits for the header */
495
496     /* +---------------------------------------+
497      * |     AU-size                           |
498      * +---------------------------------------+
499      * |     AU-Index / AU-Index-delta         |
500      * +---------------------------------------+
501      * |     CTS-flag                          |
502      * +---------------------------------------+
503      * |     CTS-delta                         |
504      * +---------------------------------------+
505      * |     DTS-flag                          |
506      * +---------------------------------------+
507      * |     DTS-delta                         |
508      * +---------------------------------------+
509      * |     RAP-flag                          |
510      * +---------------------------------------+
511      * |     Stream-state                      |
512      * +---------------------------------------+
513      */
514     /* The AU-header, no CTS, DTS, RAP, Stream-state 
515      *
516      * AU-size is always the total size of the AU, not the fragmented size 
517      */
518     payload[2] = (total & 0x1fe0) >> 5;
519     payload[3] = (total & 0x1f) << 3;   /* we use 13 bits for the size, 3 bits index */
520
521     /* copy stuff from adapter to payload */
522     gst_adapter_copy (rtpmp4gpay->adapter, &payload[4], 0, payload_len);
523     gst_adapter_flush (rtpmp4gpay->adapter, payload_len);
524
525     /* marker only if the packet is complete */
526     gst_rtp_buffer_set_marker (outbuf, avail <= payload_len);
527
528     GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4gpay->first_timestamp;
529     GST_BUFFER_DURATION (outbuf) = rtpmp4gpay->first_duration;
530
531     if (rtpmp4gpay->frame_len) {
532       GST_BUFFER_OFFSET (outbuf) = rtpmp4gpay->offset;
533       rtpmp4gpay->offset += rtpmp4gpay->frame_len;
534     }
535
536     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmp4gpay), outbuf);
537
538     avail -= payload_len;
539     fragmented = TRUE;
540   }
541
542   return ret;
543 }
544
545 /* we expect buffers as exactly one complete AU
546  */
547 static GstFlowReturn
548 gst_rtp_mp4g_pay_handle_buffer (GstBaseRTPPayload * basepayload,
549     GstBuffer * buffer)
550 {
551   GstRtpMP4GPay *rtpmp4gpay;
552
553   rtpmp4gpay = GST_RTP_MP4G_PAY (basepayload);
554
555   rtpmp4gpay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
556   rtpmp4gpay->first_duration = GST_BUFFER_DURATION (buffer);
557
558   /* we always encode and flush a full AU */
559   gst_adapter_push (rtpmp4gpay->adapter, buffer);
560
561   return gst_rtp_mp4g_pay_flush (rtpmp4gpay);
562 }
563
564 static GstStateChangeReturn
565 gst_rtp_mp4g_pay_change_state (GstElement * element, GstStateChange transition)
566 {
567   GstStateChangeReturn ret;
568   GstRtpMP4GPay *rtpmp4gpay;
569
570   rtpmp4gpay = GST_RTP_MP4G_PAY (element);
571
572   switch (transition) {
573     case GST_STATE_CHANGE_READY_TO_PAUSED:
574       gst_rtp_mp4g_pay_reset (rtpmp4gpay);
575       break;
576     default:
577       break;
578   }
579
580   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
581
582   return ret;
583 }
584
585 gboolean
586 gst_rtp_mp4g_pay_plugin_init (GstPlugin * plugin)
587 {
588   return gst_element_register (plugin, "rtpmp4gpay",
589       GST_RANK_NONE, GST_TYPE_RTP_MP4G_PAY);
590 }