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.
24 * Important note for NTSC-users:
26 * Because the author uses PAL video, and he does not have proper DV
27 * documentation (the DV format specification is not freely available),
28 * this code may very well contain PAL-specific assumptions.
35 #include "gstrtpdvdepay.h"
37 GST_DEBUG_CATEGORY (rtpdvdepay_debug);
38 #define GST_CAT_DEFAULT (rtpdvdepay_debug)
39 /* Filter signals and args */
51 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
54 GST_STATIC_CAPS ("video/x-dv")
57 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
60 GST_STATIC_CAPS ("application/x-rtp, "
61 "media = (string) { \"video\", \"audio\" },"
62 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
63 "encoding-name = (string) \"DV\", "
64 "clock-rate = (int) 90000,"
65 "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
66 "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
67 "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
68 "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
69 /* optional parameters can't go in the template
70 * "audio = (string) { \"bundled\", \"none\" }"
75 static GstStateChangeReturn
76 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition);
78 static GstBuffer *gst_rtp_dv_depay_process (GstBaseRTPDepayload * base,
80 static gboolean gst_rtp_dv_depay_setcaps (GstBaseRTPDepayload * depayload,
83 GST_BOILERPLATE (GstRTPDVDepay, gst_rtp_dv_depay, GstBaseRTPDepayload,
84 GST_TYPE_BASE_RTP_DEPAYLOAD)
86 static void gst_rtp_dv_depay_base_init (gpointer g_class)
88 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
90 gst_element_class_add_static_pad_template (element_class, &src_factory);
91 gst_element_class_add_static_pad_template (element_class, &sink_factory);
93 gst_element_class_set_details_simple (element_class, "RTP DV Depayloader",
94 "Codec/Depayloader/Network/RTP",
95 "Depayloads DV from RTP packets (RFC 3189)",
96 "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
99 /* initialize the plugin's class */
101 gst_rtp_dv_depay_class_init (GstRTPDVDepayClass * klass)
103 GstElementClass *gstelement_class = (GstElementClass *) klass;
104 GstBaseRTPDepayloadClass *gstbasertpdepayload_class =
105 (GstBaseRTPDepayloadClass *) klass;
107 gstelement_class->change_state =
108 GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_change_state);
110 gstbasertpdepayload_class->process =
111 GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_process);
112 gstbasertpdepayload_class->set_caps =
113 GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_setcaps);
115 GST_DEBUG_CATEGORY_INIT (rtpdvdepay_debug, "rtpdvdepay", 0,
116 "DV RTP Depayloader");
119 /* initialize the new element
120 * instantiate pads and add them to element
122 * initialize structure
125 gst_rtp_dv_depay_init (GstRTPDVDepay * filter, GstRTPDVDepayClass * klass)
130 parse_encode (GstRTPDVDepay * rtpdvdepay, const gchar * encode)
132 rtpdvdepay->width = 720;
133 if (!strcmp (encode, "314M-25/525-60")) {
134 rtpdvdepay->frame_size = 240000;
135 rtpdvdepay->height = 480;
136 rtpdvdepay->rate_num = 30000;
137 rtpdvdepay->rate_denom = 1001;
138 } else if (!strcmp (encode, "SD-VCR/525-60")) {
139 rtpdvdepay->frame_size = 120000;
140 rtpdvdepay->height = 480;
141 rtpdvdepay->rate_num = 30000;
142 rtpdvdepay->rate_denom = 1001;
143 } else if (!strcmp (encode, "314M-50/625-50")) {
144 rtpdvdepay->frame_size = 288000;
145 rtpdvdepay->height = 576;
146 rtpdvdepay->rate_num = 25;
147 rtpdvdepay->rate_denom = 1;
148 } else if (!strcmp (encode, "SD-VCR/625-50")) {
149 rtpdvdepay->frame_size = 144000;
150 rtpdvdepay->height = 576;
151 rtpdvdepay->rate_num = 25;
152 rtpdvdepay->rate_denom = 1;
153 } else if (!strcmp (encode, "314M-25/625-50")) {
154 rtpdvdepay->frame_size = 144000;
155 rtpdvdepay->height = 576;
156 rtpdvdepay->rate_num = 25;
157 rtpdvdepay->rate_denom = 1;
159 rtpdvdepay->frame_size = -1;
161 return rtpdvdepay->frame_size != -1;
165 gst_rtp_dv_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
167 GstStructure *structure;
168 GstRTPDVDepay *rtpdvdepay;
171 gboolean systemstream, ret;
172 const gchar *encode, *media;
174 rtpdvdepay = GST_RTP_DV_DEPAY (depayload);
176 structure = gst_caps_get_structure (caps, 0);
178 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
179 clock_rate = 90000; /* default */
180 depayload->clock_rate = clock_rate;
182 /* we really need the encode property to figure out the frame size, it's also
183 * required by the spec */
184 if (!(encode = gst_structure_get_string (structure, "encode")))
187 /* figure out the size of one frame */
188 if (!parse_encode (rtpdvdepay, encode))
191 /* check the media, this tells us that the stream has video or not */
192 if (!(media = gst_structure_get_string (structure, "media")))
195 systemstream = FALSE;
197 if (!strcmp (media, "audio")) {
198 /* we need a demuxer for audio only */
200 } else if (!strcmp (media, "video")) {
203 /* check the optional audio field, if it's present and set to bundled, we
204 * are dealing with a system stream. */
205 if ((audio = gst_structure_get_string (structure, "audio"))) {
206 if (!strcmp (audio, "bundled"))
211 /* allocate accumulator */
212 rtpdvdepay->acc = gst_buffer_new_and_alloc (rtpdvdepay->frame_size);
214 /* Initialize the new accumulator frame.
215 * If the previous frame exists, copy that into the accumulator frame.
216 * This way, missing packets in the stream won't show up badly. */
217 memset (GST_BUFFER_DATA (rtpdvdepay->acc), 0, rtpdvdepay->frame_size);
219 srccaps = gst_caps_new_simple ("video/x-dv",
220 "systemstream", G_TYPE_BOOLEAN, systemstream,
221 "width", G_TYPE_INT, rtpdvdepay->width,
222 "height", G_TYPE_INT, rtpdvdepay->height,
223 "framerate", GST_TYPE_FRACTION, rtpdvdepay->rate_num,
224 rtpdvdepay->rate_denom, NULL);
225 ret = gst_pad_set_caps (depayload->srcpad, srccaps);
226 gst_caps_unref (srccaps);
233 GST_ERROR_OBJECT (rtpdvdepay, "required encode property not found in caps");
238 GST_ERROR_OBJECT (rtpdvdepay, "unknown encode property %s found", encode);
243 GST_ERROR_OBJECT (rtpdvdepay, "required media property not found in caps");
248 /* A DV frame consists of a bunch of 80-byte DIF blocks.
249 * Each DIF block contains a 3-byte header telling where in the DV frame the
250 * DIF block should go. We use this information to calculate its position.
253 calculate_difblock_location (guint8 * block)
255 gint block_type, dif_sequence, dif_block;
258 block_type = block[0] >> 5;
259 dif_sequence = block[1] >> 4;
260 dif_block = block[2];
262 location = dif_sequence * 150;
264 switch (block_type) {
265 case 0: /* Header block, no offset */
267 case 1: /* Subcode block */
268 location += (1 + dif_block);
270 case 2: /* VAUX block */
271 location += (3 + dif_block);
273 case 3: /* Audio block */
274 location += (6 + dif_block * 16);
276 case 4: /* Video block */
277 location += (7 + (dif_block / 15) + dif_block);
279 default: /* Something bogus */
280 GST_DEBUG ("UNKNOWN BLOCK");
287 /* Process one RTP packet. Accumulate RTP payload in the proper place in a DV
288 * frame, and return that frame if we detect a new frame, or NULL otherwise.
289 * We assume a DV frame is 144000 bytes. That should accomodate PAL as well as
293 gst_rtp_dv_depay_process (GstBaseRTPDepayload * base, GstBuffer * in)
295 GstBuffer *out = NULL;
298 guint payload_len, location;
299 GstRTPDVDepay *dvdepay = GST_RTP_DV_DEPAY (base);
302 marker = gst_rtp_buffer_get_marker (in);
304 /* Check if the received packet contains (the start of) a new frame, we do
305 * this by checking the RTP timestamp. */
306 rtp_ts = gst_rtp_buffer_get_timestamp (in);
308 /* we cannot copy the packet yet if the marker is set, we will do that below
309 * after taking out the data */
310 if (dvdepay->prev_ts != -1 && rtp_ts != dvdepay->prev_ts && !marker) {
311 /* the timestamp changed */
312 GST_DEBUG_OBJECT (dvdepay, "new frame with ts %u, old ts %u", rtp_ts,
315 /* return copy of accumulator. */
316 out = gst_buffer_copy (dvdepay->acc);
319 /* Extract the payload */
320 payload_len = gst_rtp_buffer_get_payload_len (in);
321 payload = gst_rtp_buffer_get_payload (in);
323 /* copy all DIF chunks in their place. */
324 while (payload_len >= 80) {
327 /* Calculate where in the frame the payload should go */
328 location = calculate_difblock_location (payload);
331 /* part of a header, set the flag to mark that we have the header. */
332 dvdepay->header_mask |= (1 << location);
333 GST_LOG_OBJECT (dvdepay, "got header at location %d, now %02x", location,
334 dvdepay->header_mask);
336 GST_LOG_OBJECT (dvdepay, "got block at location %d", location);
339 /* get the byte offset of the dif block */
340 offset = location * 80;
342 /* And copy it in, provided the location is sane. */
343 if (offset >= 0 && offset <= dvdepay->frame_size - 80)
344 memcpy (GST_BUFFER_DATA (dvdepay->acc) + offset, payload, 80);
351 GST_DEBUG_OBJECT (dvdepay, "marker bit complete frame %u", rtp_ts);
352 /* only copy the frame when we have a complete header */
353 if (dvdepay->header_mask == 0x3f) {
354 /* The marker marks the end of a frame that we need to push. The next frame
355 * will change the timestamp but we won't copy the accumulator again because
356 * we set the prev_ts to -1. */
357 out = gst_buffer_copy (dvdepay->acc);
359 GST_WARNING_OBJECT (dvdepay, "waiting for frame headers %02x",
360 dvdepay->header_mask);
362 dvdepay->prev_ts = -1;
364 /* save last timestamp */
365 dvdepay->prev_ts = rtp_ts;
371 gst_rtp_dv_depay_reset (GstRTPDVDepay * depay)
374 gst_buffer_unref (depay->acc);
378 depay->header_mask = 0;
381 static GstStateChangeReturn
382 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition)
384 GstStateChangeReturn ret;
385 GstRTPDVDepay *depay = GST_RTP_DV_DEPAY (element);
387 switch (transition) {
388 case GST_STATE_CHANGE_READY_TO_PAUSED:
389 gst_rtp_dv_depay_reset (depay);
395 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
396 (element, transition), GST_STATE_CHANGE_FAILURE);
398 switch (transition) {
399 case GST_STATE_CHANGE_PAUSED_TO_READY:
400 gst_rtp_dv_depay_reset (depay);
409 gst_rtp_dv_depay_plugin_init (GstPlugin * plugin)
411 return gst_element_register (plugin, "rtpdvdepay",
412 GST_RANK_SECONDARY, GST_TYPE_RTP_DV_DEPAY);