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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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 #include "gstrtpelements.h"
38 #include "gstrtputils.h"
40 GST_DEBUG_CATEGORY (rtpdvdepay_debug);
41 #define GST_CAT_DEFAULT (rtpdvdepay_debug)
42 /* Filter signals and args */
54 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS ("video/x-dv")
60 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
63 GST_STATIC_CAPS ("application/x-rtp, "
64 "media = (string) { \"video\", \"audio\" },"
65 "encoding-name = (string) \"DV\", "
66 "clock-rate = (int) 90000,"
67 "encode = (string) { \"SD-VCR/525-60\", \"SD-VCR/625-50\", \"HD-VCR/1125-60\","
68 "\"HD-VCR/1250-50\", \"SDL-VCR/525-60\", \"SDL-VCR/625-50\","
69 "\"306M/525-60\", \"306M/625-50\", \"314M-25/525-60\","
70 "\"314M-25/625-50\", \"314M-50/525-60\", \"314M-50/625-50\" }"
71 /* optional parameters can't go in the template
72 * "audio = (string) { \"bundled\", \"none\" }"
77 static GstStateChangeReturn
78 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition);
80 static GstBuffer *gst_rtp_dv_depay_process (GstRTPBaseDepayload * base,
82 static gboolean gst_rtp_dv_depay_setcaps (GstRTPBaseDepayload * depayload,
85 #define gst_rtp_dv_depay_parent_class parent_class
86 G_DEFINE_TYPE (GstRTPDVDepay, gst_rtp_dv_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
87 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpdvdepay, "rtpdvdepay",
88 GST_RANK_SECONDARY, GST_TYPE_RTP_DV_DEPAY, rtp_element_init (plugin));
91 gst_rtp_dv_depay_class_init (GstRTPDVDepayClass * klass)
93 GstElementClass *gstelement_class = (GstElementClass *) klass;
94 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class =
95 (GstRTPBaseDepayloadClass *) klass;
97 GST_DEBUG_CATEGORY_INIT (rtpdvdepay_debug, "rtpdvdepay", 0,
98 "DV RTP Depayloader");
100 gstelement_class->change_state =
101 GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_change_state);
103 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
104 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
106 gst_element_class_set_static_metadata (gstelement_class, "RTP DV Depayloader",
107 "Codec/Depayloader/Network/RTP",
108 "Depayloads DV from RTP packets (RFC 3189)",
109 "Marcel Moreaux <marcelm@spacelabs.nl>, Wim Taymans <wim.taymans@gmail.com>");
111 gstrtpbasedepayload_class->process_rtp_packet =
112 GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_process);
113 gstrtpbasedepayload_class->set_caps =
114 GST_DEBUG_FUNCPTR (gst_rtp_dv_depay_setcaps);
117 /* initialize the new element
118 * instantiate pads and add them to element
120 * initialize structure
123 gst_rtp_dv_depay_init (GstRTPDVDepay * filter)
128 parse_encode (GstRTPDVDepay * rtpdvdepay, const gchar * encode)
130 rtpdvdepay->width = 720;
131 if (!strcmp (encode, "314M-25/525-60")) {
132 rtpdvdepay->frame_size = 240000;
133 rtpdvdepay->height = 480;
134 rtpdvdepay->rate_num = 30000;
135 rtpdvdepay->rate_denom = 1001;
136 } else if (!strcmp (encode, "SD-VCR/525-60")) {
137 rtpdvdepay->frame_size = 120000;
138 rtpdvdepay->height = 480;
139 rtpdvdepay->rate_num = 30000;
140 rtpdvdepay->rate_denom = 1001;
141 } else if (!strcmp (encode, "314M-50/625-50")) {
142 rtpdvdepay->frame_size = 288000;
143 rtpdvdepay->height = 576;
144 rtpdvdepay->rate_num = 25;
145 rtpdvdepay->rate_denom = 1;
146 } else if (!strcmp (encode, "SD-VCR/625-50")) {
147 rtpdvdepay->frame_size = 144000;
148 rtpdvdepay->height = 576;
149 rtpdvdepay->rate_num = 25;
150 rtpdvdepay->rate_denom = 1;
151 } else if (!strcmp (encode, "314M-25/625-50")) {
152 rtpdvdepay->frame_size = 144000;
153 rtpdvdepay->height = 576;
154 rtpdvdepay->rate_num = 25;
155 rtpdvdepay->rate_denom = 1;
157 rtpdvdepay->frame_size = -1;
159 return rtpdvdepay->frame_size != -1;
163 gst_rtp_dv_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
165 GstStructure *structure;
166 GstRTPDVDepay *rtpdvdepay;
169 gboolean systemstream, ret;
170 const gchar *encode, *media;
172 rtpdvdepay = GST_RTP_DV_DEPAY (depayload);
174 structure = gst_caps_get_structure (caps, 0);
176 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
177 clock_rate = 90000; /* default */
178 depayload->clock_rate = clock_rate;
180 /* we really need the encode property to figure out the frame size, it's also
181 * required by the spec */
182 if (!(encode = gst_structure_get_string (structure, "encode")))
185 /* figure out the size of one frame */
186 if (!parse_encode (rtpdvdepay, encode))
189 /* check the media, this tells us that the stream has video or not */
190 if (!(media = gst_structure_get_string (structure, "media")))
193 systemstream = FALSE;
195 if (!strcmp (media, "audio")) {
196 /* we need a demuxer for audio only */
198 } else if (!strcmp (media, "video")) {
201 /* check the optional audio field, if it's present and set to bundled, we
202 * are dealing with a system stream. */
203 if ((audio = gst_structure_get_string (structure, "audio"))) {
204 if (!strcmp (audio, "bundled"))
209 /* allocate accumulator */
210 rtpdvdepay->acc = gst_buffer_new_and_alloc (rtpdvdepay->frame_size);
212 /* Initialize the new accumulator frame.
213 * If the previous frame exists, copy that into the accumulator frame.
214 * This way, missing packets in the stream won't show up badly. */
215 gst_buffer_memset (rtpdvdepay->acc, 0, 0, rtpdvdepay->frame_size);
217 srccaps = gst_caps_new_simple ("video/x-dv",
218 "systemstream", G_TYPE_BOOLEAN, systemstream,
219 "width", G_TYPE_INT, rtpdvdepay->width,
220 "height", G_TYPE_INT, rtpdvdepay->height,
221 "framerate", GST_TYPE_FRACTION, rtpdvdepay->rate_num,
222 rtpdvdepay->rate_denom, NULL);
223 ret = gst_pad_set_caps (depayload->srcpad, srccaps);
224 gst_caps_unref (srccaps);
231 GST_ERROR_OBJECT (rtpdvdepay, "required encode property not found in caps");
236 GST_ERROR_OBJECT (rtpdvdepay, "unknown encode property %s found", encode);
241 GST_ERROR_OBJECT (rtpdvdepay, "required media property not found in caps");
246 /* A DV frame consists of a bunch of 80-byte DIF blocks.
247 * Each DIF block contains a 3-byte header telling where in the DV frame the
248 * DIF block should go. We use this information to calculate its position.
251 calculate_difblock_location (guint8 * block)
253 gint block_type, dif_sequence, dif_block;
256 block_type = block[0] >> 5;
257 dif_sequence = block[1] >> 4;
258 dif_block = block[2];
260 location = dif_sequence * 150;
262 switch (block_type) {
263 case 0: /* Header block, no offset */
265 case 1: /* Subcode block */
266 location += (1 + dif_block);
268 case 2: /* VAUX block */
269 location += (3 + dif_block);
271 case 3: /* Audio block */
272 location += (6 + dif_block * 16);
274 case 4: /* Video block */
275 location += (7 + (dif_block / 15) + dif_block);
277 default: /* Something bogus */
278 GST_DEBUG ("UNKNOWN BLOCK");
286 foreach_metadata_drop (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
292 /* Process one RTP packet. Accumulate RTP payload in the proper place in a DV
293 * frame, and return that frame if we detect a new frame, or NULL otherwise.
294 * We assume a DV frame is 144000 bytes. That should accommodate PAL as well as
298 gst_rtp_dv_depay_process (GstRTPBaseDepayload * base, GstRTPBuffer * rtp)
300 GstBuffer *out = NULL;
303 guint payload_len, location;
304 GstRTPDVDepay *dvdepay = GST_RTP_DV_DEPAY (base);
308 marker = gst_rtp_buffer_get_marker (rtp);
310 /* Check if the received packet contains (the start of) a new frame, we do
311 * this by checking the RTP timestamp. */
312 rtp_ts = gst_rtp_buffer_get_timestamp (rtp);
314 /* we cannot copy the packet yet if the marker is set, we will do that below
315 * after taking out the data */
316 if (dvdepay->prev_ts != -1 && rtp_ts != dvdepay->prev_ts && !marker) {
317 /* the timestamp changed */
318 GST_DEBUG_OBJECT (dvdepay, "new frame with ts %u, old ts %u", rtp_ts,
321 /* return copy of accumulator. */
322 out = gst_buffer_copy (dvdepay->acc);
323 gst_buffer_foreach_meta (dvdepay->acc, foreach_metadata_drop, NULL);
326 /* Extract the payload */
327 payload_len = gst_rtp_buffer_get_payload_len (rtp);
328 payload = gst_rtp_buffer_get_payload (rtp);
330 /* copy all DIF chunks in their place. */
331 gst_buffer_map (dvdepay->acc, &map, GST_MAP_READWRITE);
332 while (payload_len >= 80) {
335 /* Calculate where in the frame the payload should go */
336 location = calculate_difblock_location (payload);
339 /* part of a header, set the flag to mark that we have the header. */
340 dvdepay->header_mask |= (1 << location);
341 GST_LOG_OBJECT (dvdepay, "got header at location %d, now %02x", location,
342 dvdepay->header_mask);
344 GST_LOG_OBJECT (dvdepay, "got block at location %d", location);
347 if (location != -1) {
348 /* get the byte offset of the dif block */
349 offset = location * 80;
351 /* And copy it in, provided the location is sane. */
352 if (offset <= dvdepay->frame_size - 80) {
353 memcpy (map.data + offset, payload, 80);
354 gst_rtp_copy_meta (GST_ELEMENT_CAST (dvdepay), dvdepay->acc,
362 gst_buffer_unmap (dvdepay->acc, &map);
365 GST_DEBUG_OBJECT (dvdepay, "marker bit complete frame %u", rtp_ts);
366 /* only copy the frame when we have a complete header */
367 if (dvdepay->header_mask == 0x3f) {
368 /* The marker marks the end of a frame that we need to push. The next frame
369 * will change the timestamp but we won't copy the accumulator again because
370 * we set the prev_ts to -1. */
371 out = gst_buffer_copy (dvdepay->acc);
372 gst_buffer_foreach_meta (dvdepay->acc, foreach_metadata_drop, NULL);
374 GST_WARNING_OBJECT (dvdepay, "waiting for frame headers %02x",
375 dvdepay->header_mask);
377 dvdepay->prev_ts = -1;
379 /* save last timestamp */
380 dvdepay->prev_ts = rtp_ts;
386 gst_rtp_dv_depay_reset (GstRTPDVDepay * depay)
389 gst_buffer_unref (depay->acc);
393 depay->header_mask = 0;
396 static GstStateChangeReturn
397 gst_rtp_dv_depay_change_state (GstElement * element, GstStateChange transition)
399 GstStateChangeReturn ret;
400 GstRTPDVDepay *depay = GST_RTP_DV_DEPAY (element);
402 switch (transition) {
403 case GST_STATE_CHANGE_READY_TO_PAUSED:
404 gst_rtp_dv_depay_reset (depay);
410 ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
411 (element, transition), GST_STATE_CHANGE_FAILURE);
413 switch (transition) {
414 case GST_STATE_CHANGE_PAUSED_TO_READY:
415 gst_rtp_dv_depay_reset (depay);