rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpdvpay.c
1 /* Farsight
2  * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
3  *           (C) 2008 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 <stdlib.h>
26 #include <string.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28
29 #include "gstrtpdvpay.h"
30 #include "gstrtputils.h"
31
32 GST_DEBUG_CATEGORY (rtpdvpay_debug);
33 #define GST_CAT_DEFAULT (rtpdvpay_debug)
34
35 #define DEFAULT_MODE GST_DV_PAY_MODE_VIDEO
36 enum
37 {
38   PROP_0,
39   PROP_MODE
40 };
41
42 /* takes both system and non-system streams */
43 static GstStaticPadTemplate gst_rtp_dv_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("video/x-dv")
48     );
49
50 static GstStaticPadTemplate gst_rtp_dv_pay_src_template =
51 GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-rtp, "
55         "media = (string) { \"video\", \"audio\" } ,"
56         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
57         "encoding-name = (string) \"DV\", "
58         "clock-rate = (int) 90000,"
59         "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
60         "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
61         "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
62         "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
63         /* optional parameters can't go in the template
64          * "audio = (string) { \"bundled\", \"none\" }"
65          */
66     )
67     );
68
69 static gboolean gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload,
70     GstCaps * caps);
71 static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * payload,
72     GstBuffer * buffer);
73
74 #define GST_TYPE_DV_PAY_MODE (gst_dv_pay_mode_get_type())
75 static GType
76 gst_dv_pay_mode_get_type (void)
77 {
78   static GType dv_pay_mode_type = 0;
79   static const GEnumValue dv_pay_modes[] = {
80     {GST_DV_PAY_MODE_VIDEO, "Video only", "video"},
81     {GST_DV_PAY_MODE_BUNDLED, "Video and Audio bundled", "bundled"},
82     {GST_DV_PAY_MODE_AUDIO, "Audio only", "audio"},
83     {0, NULL, NULL},
84   };
85
86   if (!dv_pay_mode_type) {
87     dv_pay_mode_type = g_enum_register_static ("GstDVPayMode", dv_pay_modes);
88   }
89   return dv_pay_mode_type;
90 }
91
92
93 static void gst_dv_pay_set_property (GObject * object,
94     guint prop_id, const GValue * value, GParamSpec * pspec);
95 static void gst_dv_pay_get_property (GObject * object,
96     guint prop_id, GValue * value, GParamSpec * pspec);
97
98 #define gst_rtp_dv_pay_parent_class parent_class
99 G_DEFINE_TYPE (GstRTPDVPay, gst_rtp_dv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
100
101 static void
102 gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
103 {
104   GObjectClass *gobject_class;
105   GstElementClass *gstelement_class;
106   GstRTPBasePayloadClass *gstrtpbasepayload_class;
107
108   GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
109
110   gobject_class = (GObjectClass *) klass;
111   gstelement_class = (GstElementClass *) klass;
112   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
113
114   gobject_class->set_property = gst_dv_pay_set_property;
115   gobject_class->get_property = gst_dv_pay_get_property;
116
117   g_object_class_install_property (gobject_class, PROP_MODE,
118       g_param_spec_enum ("mode", "Mode",
119           "The payload mode of payloading",
120           GST_TYPE_DV_PAY_MODE, DEFAULT_MODE,
121           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
122
123   gst_element_class_add_static_pad_template (gstelement_class,
124       &gst_rtp_dv_pay_sink_template);
125   gst_element_class_add_static_pad_template (gstelement_class,
126       &gst_rtp_dv_pay_src_template);
127
128   gst_element_class_set_static_metadata (gstelement_class, "RTP DV Payloader",
129       "Codec/Payloader/Network/RTP",
130       "Payloads DV into RTP packets (RFC 3189)",
131       "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
132
133   gstrtpbasepayload_class->set_caps = gst_rtp_dv_pay_setcaps;
134   gstrtpbasepayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
135
136 #ifndef TIZEN_FEATURE_GST_UPSTREAM_AVOID_BUILD_BREAK
137   gst_type_mark_as_plugin_api (GST_TYPE_DV_PAY_MODE, 0);
138 #endif
139 }
140
141 static void
142 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay)
143 {
144 }
145
146 static void
147 gst_dv_pay_set_property (GObject * object,
148     guint prop_id, const GValue * value, GParamSpec * pspec)
149 {
150   GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
151
152   switch (prop_id) {
153     case PROP_MODE:
154       rtpdvpay->mode = g_value_get_enum (value);
155       break;
156     default:
157       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158       break;
159   }
160 }
161
162 static void
163 gst_dv_pay_get_property (GObject * object,
164     guint prop_id, GValue * value, GParamSpec * pspec)
165 {
166   GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
167
168   switch (prop_id) {
169     case PROP_MODE:
170       g_value_set_enum (value, rtpdvpay->mode);
171       break;
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174       break;
175   }
176 }
177
178 static gboolean
179 gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
180 {
181   /* We don't do anything here, but we could check if it's a system stream and if
182    * it's not, default to sending the video only. We will negotiate downstream
183    * caps when we get to see the first frame. */
184
185   return TRUE;
186 }
187
188 static gboolean
189 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, gsize size)
190 {
191   const gchar *encode, *media;
192   gboolean audio_bundled, res;
193
194   if ((data[3] & 0x80) == 0) {  /* DSF flag */
195     /* it's an NTSC format */
196     if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
197       /* NTSC 50Mbps */
198       encode = "314M-25/525-60";
199     } else {                    /* 4:1:1 sampling */
200       /* NTSC 25Mbps */
201       encode = "SD-VCR/525-60";
202     }
203   } else {
204     /* it's a PAL format */
205     if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
206       /* PAL 50Mbps */
207       encode = "314M-50/625-50";
208     } else if ((data[5] & 0x07) == 0) { /* APT flag */
209       /* PAL 25Mbps 4:2:0 */
210       encode = "SD-VCR/625-50";
211     } else
212       /* PAL 25Mbps 4:1:1 */
213       encode = "314M-25/625-50";
214   }
215
216   media = "video";
217   audio_bundled = FALSE;
218
219   switch (rtpdvpay->mode) {
220     case GST_DV_PAY_MODE_AUDIO:
221       media = "audio";
222       break;
223     case GST_DV_PAY_MODE_BUNDLED:
224       audio_bundled = TRUE;
225       break;
226     default:
227       break;
228   }
229   gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpdvpay), media,
230       TRUE, "DV", 90000);
231
232   if (audio_bundled) {
233     res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
234         "encode", G_TYPE_STRING, encode,
235         "audio", G_TYPE_STRING, "bundled", NULL);
236   } else {
237     res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
238         "encode", G_TYPE_STRING, encode, NULL);
239   }
240   return res;
241 }
242
243 static gboolean
244 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
245 {
246   gint block_type;
247   gboolean res;
248
249   block_type = data[0] >> 5;
250
251   switch (block_type) {
252     case 0:                    /* Header block */
253     case 1:                    /* Subcode block */
254     case 2:                    /* VAUX block */
255       /* always include these blocks */
256       res = TRUE;
257       break;
258     case 3:                    /* Audio block */
259       /* never include audio if we are doing video only */
260       if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
261         res = FALSE;
262       else
263         res = TRUE;
264       break;
265     case 4:                    /* Video block */
266       /* never include video if we are doing audio only */
267       if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
268         res = FALSE;
269       else
270         res = TRUE;
271       break;
272     default:                   /* Something bogus, just ignore */
273       res = FALSE;
274       break;
275   }
276   return res;
277 }
278
279 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
280  */
281 static GstFlowReturn
282 gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * basepayload,
283     GstBuffer * buffer)
284 {
285   GstRTPDVPay *rtpdvpay;
286   guint max_payload_size;
287   GstBuffer *outbuf;
288   GstFlowReturn ret = GST_FLOW_OK;
289   gint hdrlen;
290   gsize size;
291   GstMapInfo map;
292   guint8 *data;
293   guint8 *dest;
294   guint filled;
295   GstRTPBuffer rtp = { NULL, };
296
297   rtpdvpay = GST_RTP_DV_PAY (basepayload);
298
299   hdrlen = gst_rtp_buffer_calc_header_len (0);
300   /* DV frames are made up from a bunch of DIF blocks. DIF blocks are 80 bytes
301    * each, and we should put an integral number of them in each RTP packet.
302    * Therefore, we round the available room down to the nearest multiple of 80.
303    *
304    * The available room is just the packet MTU, minus the RTP header length. */
305   max_payload_size = ((GST_RTP_BASE_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
306
307   /* The length of the buffer to transmit. */
308   if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
309     GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
310         (NULL), ("Failed to map buffer"));
311     gst_buffer_unref (buffer);
312     return GST_FLOW_ERROR;
313   }
314   data = map.data;
315   size = map.size;
316
317   GST_DEBUG_OBJECT (rtpdvpay,
318       "DV RTP payloader got buffer of %" G_GSIZE_FORMAT
319       " bytes, splitting in %u byte " "payload fragments, at time %"
320       GST_TIME_FORMAT, size, max_payload_size,
321       GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
322
323   if (!rtpdvpay->negotiated) {
324     gst_dv_pay_negotiate (rtpdvpay, data, size);
325     /* if we have not yet scanned the stream for its type, do so now */
326     rtpdvpay->negotiated = TRUE;
327   }
328
329   outbuf = NULL;
330   dest = NULL;
331   filled = 0;
332
333   /* while we have a complete DIF chunks left */
334   while (size >= 80) {
335     /* Allocate a new buffer, set the timestamp */
336     if (outbuf == NULL) {
337       outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
338       GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buffer);
339
340       if (!gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp)) {
341         gst_buffer_unref (outbuf);
342         GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
343             (NULL), ("Failed to map RTP buffer"));
344         ret = GST_FLOW_ERROR;
345         goto beach;
346       }
347       dest = gst_rtp_buffer_get_payload (&rtp);
348       filled = 0;
349     }
350
351     /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
352     if (include_dif (rtpdvpay, data)) {
353       /* copy data in packet */
354       memcpy (dest, data, 80);
355
356       dest += 80;
357       filled += 80;
358     }
359
360     /* go to next dif chunk */
361     size -= 80;
362     data += 80;
363
364     /* push out the buffer if the next one would exceed the max packet size or
365      * when we are pushing the last packet */
366     if (filled + 80 > max_payload_size || size < 80) {
367       if (size < 160) {
368         guint hlen;
369
370         /* set marker */
371         gst_rtp_buffer_set_marker (&rtp, TRUE);
372
373         /* shrink buffer to last packet */
374         hlen = gst_rtp_buffer_get_header_len (&rtp);
375         gst_rtp_buffer_set_packet_len (&rtp, hlen + filled);
376       }
377
378       /* Push out the created piece, and check for errors. */
379       gst_rtp_buffer_unmap (&rtp);
380       gst_rtp_copy_meta (GST_ELEMENT_CAST (basepayload), outbuf, buffer, 0);
381       ret = gst_rtp_base_payload_push (basepayload, outbuf);
382       if (ret != GST_FLOW_OK)
383         break;
384
385       outbuf = NULL;
386     }
387   }
388
389 beach:
390   gst_buffer_unmap (buffer, &map);
391   gst_buffer_unref (buffer);
392
393   return ret;
394 }
395
396 gboolean
397 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
398 {
399   return gst_element_register (plugin, "rtpdvpay",
400       GST_RANK_SECONDARY, GST_TYPE_RTP_DV_PAY);
401 }