2 * Copyright (C) 2006 Marcel Moreaux <marcelm@spacelabs.nl>
3 * (C) 2008 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
27 #include <gst/rtp/gstrtpbuffer.h>
29 #include "gstrtpdvpay.h"
31 GST_DEBUG_CATEGORY (rtpdvpay_debug);
32 #define GST_CAT_DEFAULT (rtpdvpay_debug)
34 /* Elementfactory information */
35 static GstElementDetails gst_rtp_dv_pay_details = {
37 "Codec/Payloader/Network",
38 "Payloads DV into RTP packets (RFC 3189)",
39 "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>"
42 #define DEFAULT_MODE GST_DV_PAY_MODE_VIDEO
49 /* takes both system and non-system streams */
50 static GstStaticPadTemplate gst_rtp_dv_pay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
54 GST_STATIC_CAPS ("video/x-dv")
57 static GstStaticPadTemplate gst_rtp_dv_pay_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
61 GST_STATIC_CAPS ("application/x-rtp, "
62 "media = (string) { \"video\", \"audio\" } ,"
63 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
64 "encoding-name = (string) \"DV\", "
65 "clock-rate = (int) 90000,"
66 "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
67 "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
68 "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
69 "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
70 /* optional parameters can't go in the template
71 * "audio = (string) { \"bundled\", \"none\" }"
76 static gboolean gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload,
78 static GstFlowReturn gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * payload,
81 #define GST_TYPE_DV_PAY_MODE (gst_dv_pay_mode_get_type())
83 gst_dv_pay_mode_get_type (void)
85 static GType dv_pay_mode_type = 0;
86 static const GEnumValue dv_pay_modes[] = {
87 {GST_DV_PAY_MODE_VIDEO, "Video only", "video"},
88 {GST_DV_PAY_MODE_BUNDLED, "Video and Audio bundled", "bundled"},
89 {GST_DV_PAY_MODE_AUDIO, "Audio only", "audio"},
93 if (!dv_pay_mode_type) {
94 dv_pay_mode_type = g_enum_register_static ("GstDVPayMode", dv_pay_modes);
96 return dv_pay_mode_type;
100 static void gst_dv_pay_set_property (GObject * object,
101 guint prop_id, const GValue * value, GParamSpec * pspec);
102 static void gst_dv_pay_get_property (GObject * object,
103 guint prop_id, GValue * value, GParamSpec * pspec);
105 GST_BOILERPLATE (GstRTPDVPay, gst_rtp_dv_pay, GstBaseRTPPayload,
106 GST_TYPE_BASE_RTP_PAYLOAD)
108 static void gst_rtp_dv_pay_base_init (gpointer g_class)
110 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
112 gst_element_class_add_pad_template (element_class,
113 gst_static_pad_template_get (&gst_rtp_dv_pay_sink_template));
114 gst_element_class_add_pad_template (element_class,
115 gst_static_pad_template_get (&gst_rtp_dv_pay_src_template));
116 gst_element_class_set_details (element_class, &gst_rtp_dv_pay_details);
120 gst_rtp_dv_pay_class_init (GstRTPDVPayClass * klass)
122 GObjectClass *gobject_class;
123 GstBaseRTPPayloadClass *gstbasertppayload_class;
125 gobject_class = (GObjectClass *) klass;
126 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
128 gobject_class->set_property = gst_dv_pay_set_property;
129 gobject_class->get_property = gst_dv_pay_get_property;
131 gstbasertppayload_class->set_caps = gst_rtp_dv_pay_setcaps;
132 gstbasertppayload_class->handle_buffer = gst_rtp_dv_pay_handle_buffer;
134 g_object_class_install_property (gobject_class, PROP_MODE,
135 g_param_spec_enum ("mode", "Mode",
136 "The payload mode of payloading",
137 GST_TYPE_DV_PAY_MODE, DEFAULT_MODE,
138 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
140 GST_DEBUG_CATEGORY_INIT (rtpdvpay_debug, "rtpdvpay", 0, "DV RTP Payloader");
144 gst_rtp_dv_pay_init (GstRTPDVPay * rtpdvpay, GstRTPDVPayClass * klass)
149 gst_dv_pay_set_property (GObject * object,
150 guint prop_id, const GValue * value, GParamSpec * pspec)
152 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
156 rtpdvpay->mode = g_value_get_enum (value);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165 gst_dv_pay_get_property (GObject * object,
166 guint prop_id, GValue * value, GParamSpec * pspec)
168 GstRTPDVPay *rtpdvpay = GST_RTP_DV_PAY (object);
172 g_value_set_enum (value, rtpdvpay->mode);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 gst_rtp_dv_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
183 /* We don't do anything here, but we could check if it's a system stream and if
184 * it's not, default to sending the video only. We will negotiate downstream
185 * caps when we get to see the first frame. */
191 gst_dv_pay_negotiate (GstRTPDVPay * rtpdvpay, guint8 * data, guint size)
193 gchar *encode, *media;
194 gboolean audio_bundled;
196 if ((data[3] & 0x80) == 0) { /* DSF flag */
197 /* it's an NTSC format */
198 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
200 encode = "314M-25/525-60";
201 } else { /* 4:1:1 sampling */
203 encode = "SD-VCR/525-60";
206 /* it's a PAL format */
207 if ((data[80 * 5 + 48 + 3] & 0x4) && (data[80 * 5 + 48] == 0x60)) { /* 4:2:2 sampling */
209 encode = "314M-50/625-50";
210 } else if ((data[5] & 0x07) == 0) { /* APT flag */
211 /* PAL 25Mbps 4:2:0 */
212 encode = "SD-VCR/625-50";
214 /* PAL 25Mbps 4:1:1 */
215 encode = "314M-25/625-50";
219 audio_bundled = FALSE;
221 switch (rtpdvpay->mode) {
222 case GST_DV_PAY_MODE_AUDIO:
225 case GST_DV_PAY_MODE_BUNDLED:
226 audio_bundled = TRUE;
231 gst_basertppayload_set_options (GST_BASE_RTP_PAYLOAD (rtpdvpay), media, TRUE,
235 gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
236 "encode", G_TYPE_STRING, encode,
237 "audio", G_TYPE_STRING, "bundled", NULL);
239 gst_basertppayload_set_outcaps (GST_BASE_RTP_PAYLOAD (rtpdvpay),
240 "encode", G_TYPE_STRING, encode, NULL);
246 include_dif (GstRTPDVPay * rtpdvpay, guint8 * data)
251 block_type = data[0] >> 5;
253 switch (block_type) {
254 case 0: /* Header block */
255 case 1: /* Subcode block */
256 case 2: /* VAUX block */
257 /* always include these blocks */
260 case 3: /* Audio block */
261 /* never include audio if we are doing video only */
262 if (rtpdvpay->mode == GST_DV_PAY_MODE_VIDEO)
267 case 4: /* Video block */
268 /* never include video if we are doing audio only */
269 if (rtpdvpay->mode == GST_DV_PAY_MODE_AUDIO)
274 default: /* Something bogus, just ignore */
281 /* Get a DV frame, chop it up in pieces, and push the pieces to the RTP layer.
284 gst_rtp_dv_pay_handle_buffer (GstBaseRTPPayload * basepayload,
287 GstRTPDVPay *rtpdvpay;
288 guint max_payload_size;
290 GstFlowReturn ret = GST_FLOW_OK;
297 rtpdvpay = GST_RTP_DV_PAY (basepayload);
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.
304 * The available room is just the packet MTU, minus the RTP header length. */
305 max_payload_size = ((GST_BASE_RTP_PAYLOAD_MTU (rtpdvpay) - hdrlen) / 80) * 80;
307 /* The length of the buffer to transmit. */
308 size = GST_BUFFER_SIZE (buffer);
309 data = GST_BUFFER_DATA (buffer);
311 GST_DEBUG_OBJECT (rtpdvpay,
312 "DV RTP payloader got buffer of %u bytes, splitting in %u byte "
313 "payload fragments, at time %" GST_TIME_FORMAT, size, max_payload_size,
314 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
316 if (!rtpdvpay->negotiated) {
317 gst_dv_pay_negotiate (rtpdvpay, data, size);
318 /* if we have not yet scanned the stream for its type, do so now */
319 rtpdvpay->negotiated = TRUE;
326 /* while we have a complete DIF chunks left */
328 /* Allocate a new buffer, set the timestamp */
329 if (outbuf == NULL) {
330 outbuf = gst_rtp_buffer_new_allocate (max_payload_size, 0, 0);
331 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buffer);
332 dest = gst_rtp_buffer_get_payload (outbuf);
336 /* inspect the DIF chunk, if we don't need to include it, skip to the next one. */
337 if (include_dif (rtpdvpay, data)) {
338 /* copy data in packet */
339 memcpy (dest, data, 80);
345 /* go to next dif chunk */
349 /* push out the buffer if the next one would exceed the max packet size or
350 * when we are pushing the last packet */
351 if (filled + 80 > max_payload_size || size < 80) {
356 gst_rtp_buffer_set_marker (outbuf, TRUE);
358 /* shrink buffer to last packet */
359 hlen = gst_rtp_buffer_get_header_len (outbuf);
360 gst_rtp_buffer_set_packet_len (outbuf, hlen + filled);
362 /* Push out the created piece, and check for errors. */
363 ret = gst_basertppayload_push (basepayload, outbuf);
364 if (ret != GST_FLOW_OK)
370 gst_buffer_unref (buffer);
376 gst_rtp_dv_pay_plugin_init (GstPlugin * plugin)
378 return gst_element_register (plugin, "rtpdvpay",
379 GST_RANK_NONE, GST_TYPE_RTP_DV_PAY);