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