rtpvorbisdepay: remove dead code
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4vpay.c
1 /* GStreamer
2  * Copyright (C) <2005> 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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
28
29 #include "gstrtpmp4vpay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpmp4vpay_debug);
33 #define GST_CAT_DEFAULT (rtpmp4vpay_debug)
34
35 static GstStaticPadTemplate gst_rtp_mp4v_pay_sink_template =
36     GST_STATIC_PAD_TEMPLATE ("sink",
37     GST_PAD_SINK,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("video/mpeg,"
40         "mpegversion=(int) 4, systemstream=(boolean)false;" "video/x-divx")
41     );
42
43 static GstStaticPadTemplate gst_rtp_mp4v_pay_src_template =
44 GST_STATIC_PAD_TEMPLATE ("src",
45     GST_PAD_SRC,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("application/x-rtp, "
48         "media = (string) \"video\", "
49         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
50         "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
51         /* two string params
52          *
53          "profile-level-id = (string) [1,MAX]"
54          "config = (string) [1,MAX]"
55          */
56     )
57     );
58
59 #define DEFAULT_CONFIG_INTERVAL 0
60
61 enum
62 {
63   PROP_0,
64   PROP_CONFIG_INTERVAL
65 };
66
67
68 static void gst_rtp_mp4v_pay_finalize (GObject * object);
69
70 static void gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
71     const GValue * value, GParamSpec * pspec);
72 static void gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
73     GValue * value, GParamSpec * pspec);
74
75 static gboolean gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload,
76     GstCaps * caps);
77 static GstFlowReturn gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload *
78     payload, GstBuffer * buffer);
79 static gboolean gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay,
80     GstEvent * event);
81
82 #define gst_rtp_mp4v_pay_parent_class parent_class
83 G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD)
84
85      static void gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass)
86 {
87   GObjectClass *gobject_class;
88   GstElementClass *gstelement_class;
89   GstRTPBasePayloadClass *gstrtpbasepayload_class;
90
91   gobject_class = (GObjectClass *) klass;
92   gstelement_class = (GstElementClass *) klass;
93   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
94
95   gobject_class->set_property = gst_rtp_mp4v_pay_set_property;
96   gobject_class->get_property = gst_rtp_mp4v_pay_get_property;
97
98   gst_element_class_add_static_pad_template (gstelement_class,
99       &gst_rtp_mp4v_pay_src_template);
100   gst_element_class_add_static_pad_template (gstelement_class,
101       &gst_rtp_mp4v_pay_sink_template);
102
103   gst_element_class_set_static_metadata (gstelement_class,
104       "RTP MPEG4 Video payloader", "Codec/Payloader/Network/RTP",
105       "Payload MPEG-4 video as RTP packets (RFC 3016)",
106       "Wim Taymans <wim.taymans@gmail.com>");
107
108   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONFIG_INTERVAL,
109       g_param_spec_uint ("config-interval", "Config Send Interval",
110           "Send Config Insertion Interval in seconds (configuration headers "
111           "will be multiplexed in the data stream when detected.) (0 = disabled)",
112           0, 3600, DEFAULT_CONFIG_INTERVAL,
113           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
114       );
115
116   gobject_class->finalize = gst_rtp_mp4v_pay_finalize;
117
118   gstrtpbasepayload_class->set_caps = gst_rtp_mp4v_pay_setcaps;
119   gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4v_pay_handle_buffer;
120   gstrtpbasepayload_class->sink_event = gst_rtp_mp4v_pay_sink_event;
121
122   GST_DEBUG_CATEGORY_INIT (rtpmp4vpay_debug, "rtpmp4vpay", 0,
123       "MP4 video RTP Payloader");
124 }
125
126 static void
127 gst_rtp_mp4v_pay_init (GstRtpMP4VPay * rtpmp4vpay)
128 {
129   rtpmp4vpay->adapter = gst_adapter_new ();
130   rtpmp4vpay->rate = 90000;
131   rtpmp4vpay->profile = 1;
132   rtpmp4vpay->need_config = TRUE;
133   rtpmp4vpay->config_interval = DEFAULT_CONFIG_INTERVAL;
134   rtpmp4vpay->last_config = -1;
135
136   rtpmp4vpay->config = NULL;
137 }
138
139 static void
140 gst_rtp_mp4v_pay_finalize (GObject * object)
141 {
142   GstRtpMP4VPay *rtpmp4vpay;
143
144   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
145
146   if (rtpmp4vpay->config) {
147     gst_buffer_unref (rtpmp4vpay->config);
148     rtpmp4vpay->config = NULL;
149   }
150   g_object_unref (rtpmp4vpay->adapter);
151   rtpmp4vpay->adapter = NULL;
152
153   G_OBJECT_CLASS (parent_class)->finalize (object);
154 }
155
156 static gboolean
157 gst_rtp_mp4v_pay_new_caps (GstRtpMP4VPay * rtpmp4vpay)
158 {
159   gchar *profile, *config;
160   GValue v = { 0 };
161   gboolean res;
162
163   profile = g_strdup_printf ("%d", rtpmp4vpay->profile);
164   g_value_init (&v, GST_TYPE_BUFFER);
165   gst_value_set_buffer (&v, rtpmp4vpay->config);
166   config = gst_value_serialize (&v);
167
168   res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4vpay),
169       "profile-level-id", G_TYPE_STRING, profile,
170       "config", G_TYPE_STRING, config, NULL);
171
172   g_value_unset (&v);
173
174   g_free (profile);
175   g_free (config);
176
177   return res;
178 }
179
180 static gboolean
181 gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
182 {
183   GstRtpMP4VPay *rtpmp4vpay;
184   GstStructure *structure;
185   const GValue *codec_data;
186   gboolean res;
187
188   rtpmp4vpay = GST_RTP_MP4V_PAY (payload);
189
190   gst_rtp_base_payload_set_options (payload, "video", TRUE, "MP4V-ES",
191       rtpmp4vpay->rate);
192
193   res = TRUE;
194
195   structure = gst_caps_get_structure (caps, 0);
196   codec_data = gst_structure_get_value (structure, "codec_data");
197   if (codec_data) {
198     GST_LOG_OBJECT (rtpmp4vpay, "got codec_data");
199     if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
200       GstBuffer *buffer;
201
202       buffer = gst_value_get_buffer (codec_data);
203
204       if (gst_buffer_get_size (buffer) < 5)
205         goto done;
206
207       gst_buffer_extract (buffer, 4, &rtpmp4vpay->profile, 1);
208       GST_LOG_OBJECT (rtpmp4vpay, "configuring codec_data, profile %d",
209           rtpmp4vpay->profile);
210
211       if (rtpmp4vpay->config)
212         gst_buffer_unref (rtpmp4vpay->config);
213       rtpmp4vpay->config = gst_buffer_copy (buffer);
214       res = gst_rtp_mp4v_pay_new_caps (rtpmp4vpay);
215     }
216   }
217
218 done:
219   return res;
220 }
221
222 static void
223 gst_rtp_mp4v_pay_empty (GstRtpMP4VPay * rtpmp4vpay)
224 {
225   gst_adapter_clear (rtpmp4vpay->adapter);
226 }
227
228 #define RTP_HEADER_LEN 12
229
230 static GstFlowReturn
231 gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
232 {
233   guint avail, mtu;
234   GstBuffer *outbuf;
235   GstBuffer *outbuf_data = NULL;
236   GstFlowReturn ret;
237   GstBufferList *list = NULL;
238
239   /* the data available in the adapter is either smaller
240    * than the MTU or bigger. In the case it is smaller, the complete
241    * adapter contents can be put in one packet. In the case the
242    * adapter has more than one MTU, we need to split the MP4V data
243    * over multiple packets. */
244   avail = gst_adapter_available (rtpmp4vpay->adapter);
245
246   if (rtpmp4vpay->config == NULL && rtpmp4vpay->need_config) {
247     /* when we don't have a config yet, flush things out */
248     gst_adapter_flush (rtpmp4vpay->adapter, avail);
249     avail = 0;
250   }
251
252   if (!avail)
253     return GST_FLOW_OK;
254
255   mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp4vpay);
256
257   /* Use buffer lists. Each frame will be put into a list
258    * of buffers and the whole list will be pushed downstream
259    * at once */
260   list = gst_buffer_list_new_sized ((avail / (mtu - RTP_HEADER_LEN)) + 1);
261
262   while (avail > 0) {
263     guint towrite;
264     guint payload_len;
265     guint packet_len;
266     GstRTPBuffer rtp = { NULL };
267
268     /* this will be the total lenght of the packet */
269     packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
270
271     /* fill one MTU or all available bytes */
272     towrite = MIN (packet_len, mtu);
273
274     /* this is the payload length */
275     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
276
277     /* create buffer without payload. The payload will be put
278      * in next buffer instead. Both buffers will be merged */
279     outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
280
281     /* Take buffer with the payload from the adapter */
282     outbuf_data = gst_adapter_take_buffer_fast (rtpmp4vpay->adapter,
283         payload_len);
284
285     avail -= payload_len;
286
287     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
288     gst_rtp_buffer_set_marker (&rtp, avail == 0);
289     gst_rtp_buffer_unmap (&rtp);
290     gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpmp4vpay), outbuf, outbuf_data,
291         g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
292     outbuf = gst_buffer_append (outbuf, outbuf_data);
293
294     GST_BUFFER_PTS (outbuf) = rtpmp4vpay->first_timestamp;
295
296     /* add to list */
297     gst_buffer_list_insert (list, -1, outbuf);
298   }
299
300   /* push the whole buffer list at once */
301   ret =
302       gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmp4vpay), list);
303
304   return ret;
305 }
306
307 #define VOS_STARTCODE                   0x000001B0
308 #define VOS_ENDCODE                     0x000001B1
309 #define USER_DATA_STARTCODE             0x000001B2
310 #define GOP_STARTCODE                   0x000001B3
311 #define VISUAL_OBJECT_STARTCODE         0x000001B5
312 #define VOP_STARTCODE                   0x000001B6
313
314 static gboolean
315 gst_rtp_mp4v_pay_depay_data (GstRtpMP4VPay * enc, guint8 * data, guint size,
316     gint * strip, gboolean * vopi)
317 {
318   guint32 code;
319   gboolean result;
320   *vopi = FALSE;
321
322   *strip = 0;
323
324   if (size < 5)
325     return FALSE;
326
327   code = GST_READ_UINT32_BE (data);
328   GST_DEBUG_OBJECT (enc, "start code 0x%08x", code);
329
330   switch (code) {
331     case VOS_STARTCODE:
332     case 0x00000101:
333     {
334       gint i;
335       guint8 profile;
336       gboolean newprofile = FALSE;
337       gboolean equal;
338
339       if (code == VOS_STARTCODE) {
340         /* profile_and_level_indication */
341         profile = data[4];
342
343         GST_DEBUG_OBJECT (enc, "VOS profile 0x%08x", profile);
344
345         if (profile != enc->profile) {
346           newprofile = TRUE;
347           enc->profile = profile;
348         }
349       }
350
351       /* up to the next GOP_STARTCODE or VOP_STARTCODE is
352        * the config information */
353       code = 0xffffffff;
354       for (i = 5; i < size - 4; i++) {
355         code = (code << 8) | data[i];
356         if (code == GOP_STARTCODE || code == VOP_STARTCODE)
357           break;
358       }
359       i -= 3;
360       /* see if config changed */
361       equal = FALSE;
362       if (enc->config) {
363         if (gst_buffer_get_size (enc->config) == i) {
364           equal = gst_buffer_memcmp (enc->config, 0, data, i) == 0;
365         }
366       }
367       /* if config string changed or new profile, make new caps */
368       if (!equal || newprofile) {
369         if (enc->config)
370           gst_buffer_unref (enc->config);
371         enc->config = gst_buffer_new_and_alloc (i);
372
373         gst_buffer_fill (enc->config, 0, data, i);
374
375         gst_rtp_mp4v_pay_new_caps (enc);
376       }
377       *strip = i;
378       /* we need to flush out the current packet. */
379       result = TRUE;
380       break;
381     }
382     case VOP_STARTCODE:
383       GST_DEBUG_OBJECT (enc, "VOP");
384       /* VOP startcode, we don't have to flush the packet */
385       result = FALSE;
386       /* vop-coding-type == I-frame */
387       if (size > 4 && (data[4] >> 6 == 0)) {
388         GST_DEBUG_OBJECT (enc, "VOP-I");
389         *vopi = TRUE;
390       }
391       break;
392     case GOP_STARTCODE:
393       GST_DEBUG_OBJECT (enc, "GOP");
394       *vopi = TRUE;
395       result = TRUE;
396       break;
397     case 0x00000100:
398       enc->need_config = FALSE;
399       result = TRUE;
400       break;
401     default:
402       if (code >= 0x20 && code <= 0x2f) {
403         GST_DEBUG_OBJECT (enc, "short header");
404         result = FALSE;
405       } else {
406         GST_DEBUG_OBJECT (enc, "other startcode");
407         /* all other startcodes need a flush */
408         result = TRUE;
409       }
410       break;
411   }
412   return result;
413 }
414
415 /* we expect buffers starting on startcodes. 
416  */
417 static GstFlowReturn
418 gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
419     GstBuffer * buffer)
420 {
421   GstRtpMP4VPay *rtpmp4vpay;
422   GstFlowReturn ret;
423   guint avail;
424   guint packet_len;
425   GstMapInfo map;
426   gsize size;
427   gboolean flush;
428   gint strip;
429   GstClockTime timestamp, duration;
430   gboolean vopi;
431   gboolean send_config;
432
433   ret = GST_FLOW_OK;
434   send_config = FALSE;
435
436   rtpmp4vpay = GST_RTP_MP4V_PAY (basepayload);
437
438   gst_buffer_map (buffer, &map, GST_MAP_READ);
439   size = map.size;
440   timestamp = GST_BUFFER_PTS (buffer);
441   duration = GST_BUFFER_DURATION (buffer);
442   avail = gst_adapter_available (rtpmp4vpay->adapter);
443
444   if (duration == -1)
445     duration = 0;
446
447   /* empty buffer, take timestamp */
448   if (avail == 0) {
449     rtpmp4vpay->first_timestamp = timestamp;
450     rtpmp4vpay->duration = 0;
451   }
452
453   /* depay incomming data and see if we need to start a new RTP
454    * packet */
455   flush =
456       gst_rtp_mp4v_pay_depay_data (rtpmp4vpay, map.data, size, &strip, &vopi);
457   gst_buffer_unmap (buffer, &map);
458
459   if (strip) {
460     /* strip off config if requested */
461     if (!(rtpmp4vpay->config_interval > 0)) {
462       GstBuffer *subbuf;
463
464       GST_LOG_OBJECT (rtpmp4vpay, "stripping config at %d, size %d", strip,
465           (gint) size - strip);
466
467       /* strip off header */
468       subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, strip,
469           size - strip);
470       GST_BUFFER_PTS (subbuf) = timestamp;
471       gst_buffer_unref (buffer);
472       buffer = subbuf;
473
474       size = gst_buffer_get_size (buffer);
475     } else {
476       GST_LOG_OBJECT (rtpmp4vpay, "found config in stream");
477       rtpmp4vpay->last_config = timestamp;
478     }
479   }
480
481   /* there is a config request, see if we need to insert it */
482   if (vopi && (rtpmp4vpay->config_interval > 0) && rtpmp4vpay->config) {
483     if (rtpmp4vpay->last_config != -1) {
484       guint64 diff;
485
486       GST_LOG_OBJECT (rtpmp4vpay,
487           "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
488           GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpmp4vpay->last_config));
489
490       /* calculate diff between last config in milliseconds */
491       if (timestamp > rtpmp4vpay->last_config) {
492         diff = timestamp - rtpmp4vpay->last_config;
493       } else {
494         diff = 0;
495       }
496
497       GST_DEBUG_OBJECT (rtpmp4vpay,
498           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
499
500       /* bigger than interval, queue config */
501       /* FIXME should convert timestamps to running time */
502       if (GST_TIME_AS_SECONDS (diff) >= rtpmp4vpay->config_interval) {
503         GST_DEBUG_OBJECT (rtpmp4vpay, "time to send config");
504         send_config = TRUE;
505       }
506     } else {
507       /* no known previous config time, send now */
508       GST_DEBUG_OBJECT (rtpmp4vpay, "no previous config time, send now");
509       send_config = TRUE;
510     }
511
512     if (send_config) {
513       /* we need to send config now first */
514       GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
515
516       /* insert header */
517       buffer = gst_buffer_append (gst_buffer_ref (rtpmp4vpay->config), buffer);
518
519       GST_BUFFER_PTS (buffer) = timestamp;
520       size = gst_buffer_get_size (buffer);
521
522       if (timestamp != -1) {
523         rtpmp4vpay->last_config = timestamp;
524       }
525     }
526   }
527
528   /* if we need to flush, do so now */
529   if (flush) {
530     ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
531     rtpmp4vpay->first_timestamp = timestamp;
532     rtpmp4vpay->duration = 0;
533     avail = 0;
534   }
535
536   /* get packet length of data and see if we exceeded MTU. */
537   packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
538
539   if (gst_rtp_base_payload_is_filled (basepayload,
540           packet_len, rtpmp4vpay->duration + duration)) {
541     ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
542     rtpmp4vpay->first_timestamp = timestamp;
543     rtpmp4vpay->duration = 0;
544   }
545
546   /* push new data */
547   gst_adapter_push (rtpmp4vpay->adapter, buffer);
548
549   rtpmp4vpay->duration += duration;
550
551   return ret;
552 }
553
554 static gboolean
555 gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay, GstEvent * event)
556 {
557   GstRtpMP4VPay *rtpmp4vpay;
558
559   rtpmp4vpay = GST_RTP_MP4V_PAY (pay);
560
561   GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
562
563   switch (GST_EVENT_TYPE (event)) {
564     case GST_EVENT_SEGMENT:
565     case GST_EVENT_EOS:
566       /* This flush call makes sure that the last buffer is always pushed
567        * to the base payloader */
568       gst_rtp_mp4v_pay_flush (rtpmp4vpay);
569       break;
570     case GST_EVENT_FLUSH_STOP:
571       gst_rtp_mp4v_pay_empty (rtpmp4vpay);
572       break;
573     default:
574       break;
575   }
576
577   /* let parent handle event too */
578   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (pay, event);
579 }
580
581 static void
582 gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
583     const GValue * value, GParamSpec * pspec)
584 {
585   GstRtpMP4VPay *rtpmp4vpay;
586
587   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
588
589   switch (prop_id) {
590     case PROP_CONFIG_INTERVAL:
591       rtpmp4vpay->config_interval = g_value_get_uint (value);
592       break;
593     default:
594       break;
595   }
596 }
597
598 static void
599 gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
600     GValue * value, GParamSpec * pspec)
601 {
602   GstRtpMP4VPay *rtpmp4vpay;
603
604   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
605
606   switch (prop_id) {
607     case PROP_CONFIG_INTERVAL:
608       g_value_set_uint (value, rtpmp4vpay->config_interval);
609       break;
610     default:
611       break;
612   }
613 }
614
615 gboolean
616 gst_rtp_mp4v_pay_plugin_init (GstPlugin * plugin)
617 {
618   return gst_element_register (plugin, "rtpmp4vpay",
619       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4V_PAY);
620 }