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