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
gst_dv_pay_mode_get_type(void)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
gst_rtp_dv_pay_class_init(GstRTPDVPayClass * klass)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
137 static void
gst_rtp_dv_pay_init(GstRTPDVPay * rtpdvpay)138 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay)
139 {
140 }
141
142 static void
gst_dv_pay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)143 gst_dv_pay_set_property (GObject * object,
144 guint prop_id, const GValue * value, GParamSpec * pspec)
145 {
146 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
147
148 switch (prop_id) {
149 case PROP_MODE:
150 rtpdvpay->mode = g_value_get_enum (value);
151 break;
152 default:
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 break;
155 }
156 }
157
158 static void
gst_dv_pay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)159 gst_dv_pay_get_property (GObject * object,
160 guint prop_id, GValue * value, GParamSpec * pspec)
161 {
162 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
163
164 switch (prop_id) {
165 case PROP_MODE:
166 g_value_set_enum (value, rtpdvpay->mode);
167 break;
168 default:
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170 break;
171 }
172 }
173
174 static gboolean
gst_rtp_dv_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)175 gst_rtp_dv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
176 {
177 /* We don't do anything here, but we could check if it's a system stream and if
178 * it's not, default to sending the video only. We will negotiate downstream
179 * caps when we get to see the first frame. */
180
181 return TRUE;
182 }
183
184 static gboolean
gst_dv_pay_negotiate(GstRTPDVPay * rtpdvpay,guint8 * data,gsize size)185 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, gsize size)
186 {
187 const gchar *encode, *media;
188 gboolean audio_bundled, res;
189
190 if ((data[3] & 0x80) == 0) { /* DSF flag */
191 /* it's an NTSC format */
192 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
193 /* NTSC 50Mbps */
194 encode = "314M-25/525-60";
195 } else { /* 4:1:1 sampling */
196 /* NTSC 25Mbps */
197 encode = "SD-VCR/525-60";
198 }
199 } else {
200 /* it's a PAL format */
201 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
202 /* PAL 50Mbps */
203 encode = "314M-50/625-50";
204 } else if ((data[5] & 0x07) == 0) { /* APT flag */
205 /* PAL 25Mbps 4:2:0 */
206 encode = "SD-VCR/625-50";
207 } else
208 /* PAL 25Mbps 4:1:1 */
209 encode = "314M-25/625-50";
210 }
211
212 media = "video";
213 audio_bundled = FALSE;
214
215 switch (rtpdvpay->mode) {
216 case GST_DV_PAY_MODE_AUDIO:
217 media = "audio";
218 break;
219 case GST_DV_PAY_MODE_BUNDLED:
220 audio_bundled = TRUE;
221 break;
222 default:
223 break;
224 }
225 gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpdvpay), media,
226 TRUE, "DV", 90000);
227
228 if (audio_bundled) {
229 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
230 "encode", G_TYPE_STRING, encode,
231 "audio", G_TYPE_STRING, "bundled", NULL);
232 } else {
233 res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpdvpay),
234 "encode", G_TYPE_STRING, encode, NULL);
235 }
236 return res;
237 }
238
239 static gboolean
include_dif(GstRTPDVPay * rtpdvpay,guint8 * data)240 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
241 {
242 gint block_type;
243 gboolean res;
244
245 block_type = data[0] >> 5;
246
247 switch (block_type) {
248 case 0: /* Header block */
249 case 1: /* Subcode block */
250 case 2: /* VAUX block */
251 /* always include these blocks */
252 res = TRUE;
253 break;
254 case 3: /* Audio block */
255 /* never include audio if we are doing video only */
256 if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
257 res = FALSE;
258 else
259 res = TRUE;
260 break;
261 case 4: /* Video block */
262 /* never include video if we are doing audio only */
263 if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
264 res = FALSE;
265 else
266 res = TRUE;
267 break;
268 default: /* Something bogus, just ignore */
269 res = FALSE;
270 break;
271 }
272 return res;
273 }
274
275 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
276 */
277 static GstFlowReturn
gst_rtp_dv_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)278 gst_rtp_dv_pay_handle_buffer (GstRTPBasePayload * basepayload,
279 GstBuffer * buffer)
280 {
281 GstRTPDVPay *rtpdvpay;
282 guint max_payload_size;
283 GstBuffer *outbuf;
284 GstFlowReturn ret = GST_FLOW_OK;
285 gint hdrlen;
286 gsize size;
287 GstMapInfo map;
288 guint8 *data;
289 guint8 *dest;
290 guint filled;
291 GstRTPBuffer rtp = { NULL, };
292
293 rtpdvpay = GST_RTP_DV_PAY (basepayload);
294
295 hdrlen = gst_rtp_buffer_calc_header_len (0);
296 /* DV frames are made up from a bunch of DIF blocks. DIF blocks are 80 bytes
297 * each, and we should put an integral number of them in each RTP packet.
298 * Therefore, we round the available room down to the nearest multiple of 80.
299 *
300 * The available room is just the packet MTU, minus the RTP header length. */
301 max_payload_size = ((GST_RTP_BASE_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
302
303 /* The length of the buffer to transmit. */
304 if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
305 GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
306 (NULL), ("Failed to map buffer"));
307 gst_buffer_unref (buffer);
308 return GST_FLOW_ERROR;
309 }
310 data = map.data;
311 size = map.size;
312
313 GST_DEBUG_OBJECT (rtpdvpay,
314 "DV RTP payloader got buffer of %" G_GSIZE_FORMAT
315 " bytes, splitting in %u byte " "payload fragments, at time %"
316 GST_TIME_FORMAT, size, max_payload_size,
317 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
318
319 if (!rtpdvpay->negotiated) {
320 gst_dv_pay_negotiate (rtpdvpay, data, size);
321 /* if we have not yet scanned the stream for its type, do so now */
322 rtpdvpay->negotiated = TRUE;
323 }
324
325 outbuf = NULL;
326 dest = NULL;
327 filled = 0;
328
329 /* while we have a complete DIF chunks left */
330 while (size >= 80) {
331 /* Allocate a new buffer, set the timestamp */
332 if (outbuf == NULL) {
333 outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
334 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buffer);
335
336 if (!gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp)) {
337 gst_buffer_unref (outbuf);
338 GST_ELEMENT_ERROR (rtpdvpay, CORE, FAILED,
339 (NULL), ("Failed to map RTP buffer"));
340 ret = GST_FLOW_ERROR;
341 goto beach;
342 }
343 dest = gst_rtp_buffer_get_payload (&rtp);
344 filled = 0;
345 }
346
347 /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
348 if (include_dif (rtpdvpay, data)) {
349 /* copy data in packet */
350 memcpy (dest, data, 80);
351
352 dest += 80;
353 filled += 80;
354 }
355
356 /* go to next dif chunk */
357 size -= 80;
358 data += 80;
359
360 /* push out the buffer if the next one would exceed the max packet size or
361 * when we are pushing the last packet */
362 if (filled + 80 > max_payload_size || size < 80) {
363 if (size < 160) {
364 guint hlen;
365
366 /* set marker */
367 gst_rtp_buffer_set_marker (&rtp, TRUE);
368
369 /* shrink buffer to last packet */
370 hlen = gst_rtp_buffer_get_header_len (&rtp);
371 gst_rtp_buffer_set_packet_len (&rtp, hlen + filled);
372 }
373
374 /* Push out the created piece, and check for errors. */
375 gst_rtp_buffer_unmap (&rtp);
376 gst_rtp_copy_meta (GST_ELEMENT_CAST (basepayload), outbuf, buffer, 0);
377 ret = gst_rtp_base_payload_push (basepayload, outbuf);
378 if (ret != GST_FLOW_OK)
379 break;
380
381 outbuf = NULL;
382 }
383 }
384
385 beach:
386 gst_buffer_unmap (buffer, &map);
387 gst_buffer_unref (buffer);
388
389 return ret;
390 }
391
392 gboolean
gst_rtp_dv_pay_plugin_init(GstPlugin * plugin)393 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
394 {
395 return gst_element_register (plugin, "rtpdvpay",
396 GST_RANK_SECONDARY, GST_TYPE_RTP_DV_PAY);
397 }
398