2 * Copyright (C) <2008> Wim Taymans <wim.taymans@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpvrawdepay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpvrawdepay_debug);
31 #define GST_CAT_DEFAULT (rtpvrawdepay_debug)
33 static GstStaticPadTemplate gst_rtp_vraw_depay_src_template =
34 GST_STATIC_PAD_TEMPLATE ("src",
37 GST_STATIC_CAPS ("video/x-raw-rgb; video/x-raw-yuv")
40 static GstStaticPadTemplate gst_rtp_vraw_depay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("application/x-rtp, "
45 "media = (string) \"video\", "
46 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
47 "clock-rate = (int) 90000, " "encoding-name = (string) \"RAW\"")
50 GST_BOILERPLATE (GstRtpVRawDepay, gst_rtp_vraw_depay, GstBaseRTPDepayload,
51 GST_TYPE_BASE_RTP_DEPAYLOAD);
53 static gboolean gst_rtp_vraw_depay_setcaps (GstBaseRTPDepayload * depayload,
55 static GstBuffer *gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload,
58 static GstStateChangeReturn gst_rtp_vraw_depay_change_state (GstElement *
59 element, GstStateChange transition);
61 static gboolean gst_rtp_vraw_depay_handle_event (GstBaseRTPDepayload * filter,
65 gst_rtp_vraw_depay_base_init (gpointer klass)
67 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
69 gst_element_class_add_static_pad_template (element_class,
70 &gst_rtp_vraw_depay_src_template);
71 gst_element_class_add_static_pad_template (element_class,
72 &gst_rtp_vraw_depay_sink_template);
74 gst_element_class_set_details_simple (element_class,
75 "RTP Raw Video depayloader", "Codec/Depayloader/Network/RTP",
76 "Extracts raw video from RTP packets (RFC 4175)",
77 "Wim Taymans <wim.taymans@gmail.com>");
81 gst_rtp_vraw_depay_class_init (GstRtpVRawDepayClass * klass)
83 GstElementClass *gstelement_class;
84 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
86 gstelement_class = (GstElementClass *) klass;
87 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
89 gstelement_class->change_state = gst_rtp_vraw_depay_change_state;
91 gstbasertpdepayload_class->set_caps = gst_rtp_vraw_depay_setcaps;
92 gstbasertpdepayload_class->process = gst_rtp_vraw_depay_process;
93 gstbasertpdepayload_class->handle_event = gst_rtp_vraw_depay_handle_event;
95 GST_DEBUG_CATEGORY_INIT (rtpvrawdepay_debug, "rtpvrawdepay", 0,
96 "raw video RTP Depayloader");
100 gst_rtp_vraw_depay_init (GstRtpVRawDepay * rtpvrawdepay,
101 GstRtpVRawDepayClass * klass)
103 /* needed because of GST_BOILERPLATE */
107 gst_rtp_vraw_depay_reset (GstRtpVRawDepay * rtpvrawdepay)
109 if (rtpvrawdepay->outbuf) {
110 gst_buffer_unref (rtpvrawdepay->outbuf);
111 rtpvrawdepay->outbuf = NULL;
113 rtpvrawdepay->timestamp = -1;
117 gst_rtp_vraw_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
119 GstStructure *structure;
120 GstRtpVRawDepay *rtpvrawdepay;
122 const gchar *str, *type;
123 gint format, width, height, pgroup, xinc, yinc;
124 guint ystride, uvstride, yp, up, vp, outsize;
128 gint rmask = 0, gmask = 0, bmask = 0, amask = 0, bpp = 0, depth = 0;
130 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
132 structure = gst_caps_get_structure (caps, 0);
134 yp = up = vp = uvstride = 0;
137 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
138 clock_rate = 90000; /* default */
139 depayload->clock_rate = clock_rate;
141 if (!(str = gst_structure_get_string (structure, "width")))
145 if (!(str = gst_structure_get_string (structure, "height")))
149 /* optional interlace value but we don't handle interlaced
151 if (gst_structure_get_string (structure, "interlace"))
154 if (!(str = gst_structure_get_string (structure, "sampling")))
157 if (!strcmp (str, "RGB")) {
158 format = GST_VIDEO_FORMAT_RGB;
160 ystride = GST_ROUND_UP_4 (width * 3);
161 outsize = ystride * height;
162 type = "video/x-raw-rgb";
168 } else if (!strcmp (str, "RGBA")) {
169 format = GST_VIDEO_FORMAT_RGBA;
172 outsize = ystride * height;
173 type = "video/x-raw-rgb";
180 } else if (!strcmp (str, "BGR")) {
181 format = GST_VIDEO_FORMAT_BGR;
183 ystride = GST_ROUND_UP_4 (width * 3);
184 outsize = ystride * height;
185 type = "video/x-raw-rgb";
191 } else if (!strcmp (str, "BGRA")) {
192 format = GST_VIDEO_FORMAT_BGRA;
195 outsize = ystride * height;
196 type = "video/x-raw-rgb";
203 } else if (!strcmp (str, "YCbCr-4:4:4")) {
204 format = GST_VIDEO_FORMAT_AYUV;
207 outsize = ystride * height;
208 type = "video/x-raw-yuv";
209 fourcc = GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
210 } else if (!strcmp (str, "YCbCr-4:2:2")) {
211 format = GST_VIDEO_FORMAT_UYVY;
213 ystride = GST_ROUND_UP_2 (width) * 2;
214 outsize = ystride * height;
215 type = "video/x-raw-yuv";
216 fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
218 } else if (!strcmp (str, "YCbCr-4:2:0")) {
219 format = GST_VIDEO_FORMAT_I420;
221 ystride = GST_ROUND_UP_4 (width);
222 uvstride = GST_ROUND_UP_8 (width) / 2;
223 up = ystride * GST_ROUND_UP_2 (height);
224 vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
225 outsize = vp + uvstride * GST_ROUND_UP_2 (height) / 2;
226 type = "video/x-raw-yuv";
227 fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
229 } else if (!strcmp (str, "YCbCr-4:1:1")) {
230 format = GST_VIDEO_FORMAT_Y41B;
232 ystride = GST_ROUND_UP_4 (width);
233 uvstride = GST_ROUND_UP_8 (width) / 4;
234 up = ystride * height;
235 vp = up + uvstride * height;
236 outsize = vp + uvstride * height;
237 type = "video/x-raw-yuv";
238 fourcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
243 rtpvrawdepay->width = width;
244 rtpvrawdepay->height = height;
245 rtpvrawdepay->format = format;
246 rtpvrawdepay->yp = yp;
247 rtpvrawdepay->up = up;
248 rtpvrawdepay->vp = vp;
249 rtpvrawdepay->pgroup = pgroup;
250 rtpvrawdepay->xinc = xinc;
251 rtpvrawdepay->yinc = yinc;
252 rtpvrawdepay->ystride = ystride;
253 rtpvrawdepay->uvstride = uvstride;
254 rtpvrawdepay->outsize = outsize;
256 srccaps = gst_caps_new_simple (type,
257 "width", G_TYPE_INT, width,
258 "height", G_TYPE_INT, height,
259 "format", GST_TYPE_FOURCC, fourcc,
260 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
262 if (!strcmp (type, "video/x-raw-rgb")) {
263 gst_caps_set_simple (srccaps,
264 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
265 "red_mask", G_TYPE_INT, rmask,
266 "green_mask", G_TYPE_INT, gmask,
267 "blue_mask", G_TYPE_INT, bmask,
268 "bpp", G_TYPE_INT, bpp, "depth", G_TYPE_INT, depth, NULL);
271 gst_caps_set_simple (srccaps, "alpha_mask", G_TYPE_INT, amask, NULL);
275 res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
276 gst_caps_unref (srccaps);
278 GST_DEBUG_OBJECT (depayload, "width %d, height %d, format %d", width, height,
280 GST_DEBUG_OBJECT (depayload, "yp %d, up %d, vp %d", yp, up, vp);
281 GST_DEBUG_OBJECT (depayload, "xinc %d, yinc %d", xinc, yinc);
282 GST_DEBUG_OBJECT (depayload, "pgroup %d, ystride %d, uvstride %d", pgroup,
284 GST_DEBUG_OBJECT (depayload, "outsize %u", outsize);
291 GST_ERROR_OBJECT (depayload, "no width specified");
296 GST_ERROR_OBJECT (depayload, "no height specified");
301 GST_ERROR_OBJECT (depayload, "interlaced formats not supported yet");
306 GST_ERROR_OBJECT (depayload, "no sampling specified");
311 GST_ERROR_OBJECT (depayload, "unknown sampling format '%s'", str);
317 gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
319 GstRtpVRawDepay *rtpvrawdepay;
320 guint8 *payload, *data, *yp, *up, *vp, *headers;
322 guint cont, ystride, uvstride, pgroup, payload_len;
323 gint width, height, xinc, yinc;
325 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
327 timestamp = gst_rtp_buffer_get_timestamp (buf);
329 if (timestamp != rtpvrawdepay->timestamp || rtpvrawdepay->outbuf == NULL) {
333 GST_LOG_OBJECT (depayload, "new frame with timestamp %u", timestamp);
334 /* new timestamp, flush old buffer and create new output buffer */
335 if (rtpvrawdepay->outbuf) {
336 gst_base_rtp_depayload_push_ts (depayload, rtpvrawdepay->timestamp,
337 rtpvrawdepay->outbuf);
338 rtpvrawdepay->outbuf = NULL;
341 ret = gst_pad_alloc_buffer (depayload->srcpad, -1, rtpvrawdepay->outsize,
342 GST_PAD_CAPS (depayload->srcpad), &outbuf);
343 if (ret != GST_FLOW_OK)
346 /* clear timestamp from alloc... */
347 GST_BUFFER_TIMESTAMP (outbuf) = -1;
349 rtpvrawdepay->outbuf = outbuf;
350 rtpvrawdepay->timestamp = timestamp;
353 data = GST_BUFFER_DATA (rtpvrawdepay->outbuf);
355 /* get pointer and strides of the planes */
356 yp = data + rtpvrawdepay->yp;
357 up = data + rtpvrawdepay->up;
358 vp = data + rtpvrawdepay->vp;
360 ystride = rtpvrawdepay->ystride;
361 uvstride = rtpvrawdepay->uvstride;
362 pgroup = rtpvrawdepay->pgroup;
363 width = rtpvrawdepay->width;
364 height = rtpvrawdepay->height;
365 xinc = rtpvrawdepay->xinc;
366 yinc = rtpvrawdepay->yinc;
368 payload = gst_rtp_buffer_get_payload (buf);
369 payload_len = gst_rtp_buffer_get_payload_len (buf);
374 /* skip extended seqnum */
378 /* remember header position */
381 /* find data start */
386 cont = payload[4] & 0x80;
393 guint length, line, offs, plen;
396 /* stop when we run out of data */
397 if (payload_len == 0)
400 /* read length and cont. This should work because we iterated the headers
402 length = (headers[0] << 8) | headers[1];
403 line = ((headers[2] & 0x7f) << 8) | headers[3];
404 offs = ((headers[4] & 0x7f) << 8) | headers[5];
405 cont = headers[4] & 0x80;
408 /* length must be a multiple of pgroup */
409 if (length % pgroup != 0)
412 if (length > payload_len)
413 length = payload_len;
416 if (line > (height - yinc)) {
417 GST_WARNING_OBJECT (depayload, "skipping line %d: out of range", line);
420 if (offs > (width - xinc)) {
421 GST_WARNING_OBJECT (depayload, "skipping offset %d: out of range", offs);
425 /* calculate the maximim amount of bytes we can use per line */
426 if (offs + ((length / pgroup) * xinc) > width) {
427 plen = ((width - offs) * pgroup) / xinc;
428 GST_WARNING_OBJECT (depayload, "clipping length %d, offset %d, plen %d",
433 GST_LOG_OBJECT (depayload,
434 "writing length %u/%u, line %u, offset %u, remaining %u", plen, length,
435 line, offs, payload_len);
437 switch (rtpvrawdepay->format) {
438 case GST_VIDEO_FORMAT_RGB:
439 case GST_VIDEO_FORMAT_RGBA:
440 case GST_VIDEO_FORMAT_BGR:
441 case GST_VIDEO_FORMAT_BGRA:
442 case GST_VIDEO_FORMAT_UYVY:
443 /* samples are packed just like gstreamer packs them */
445 datap = yp + (line * ystride) + (offs * pgroup);
447 memcpy (datap, payload, plen);
449 case GST_VIDEO_FORMAT_AYUV:
454 datap = yp + (line * ystride) + (offs * 4);
457 /* samples are packed in order Cb-Y-Cr for both interlaced and
458 * progressive frames */
459 for (i = 0; i < plen; i += pgroup) {
468 case GST_VIDEO_FORMAT_I420:
472 guint8 *yd1p, *yd2p, *udp, *vdp, *p;
474 yd1p = yp + (line * ystride) + (offs);
475 yd2p = yd1p + ystride;
476 uvoff = (line / yinc * uvstride) + (offs / xinc);
482 /* line 0/1: Y00-Y01-Y10-Y11-Cb00-Cr00 Y02-Y03-Y12-Y13-Cb01-Cr01 ... */
483 for (i = 0; i < plen; i += pgroup) {
494 case GST_VIDEO_FORMAT_Y41B:
498 guint8 *ydp, *udp, *vdp, *p;
500 ydp = yp + (line * ystride) + (offs);
501 uvoff = (line / yinc * uvstride) + (offs / xinc);
507 /* Samples are packed in order Cb0-Y0-Y1-Cr0-Y2-Y3 for both interlaced
508 * and progressive scan lines */
509 for (i = 0; i < plen; i += pgroup) {
521 goto unknown_sampling;
529 payload_len -= length;
532 if (gst_rtp_buffer_get_marker (buf)) {
533 GST_LOG_OBJECT (depayload, "marker, flushing frame");
534 if (rtpvrawdepay->outbuf) {
535 gst_base_rtp_depayload_push_ts (depayload, timestamp,
536 rtpvrawdepay->outbuf);
537 rtpvrawdepay->outbuf = NULL;
539 rtpvrawdepay->timestamp = -1;
546 GST_ELEMENT_ERROR (depayload, STREAM, FORMAT,
547 (NULL), ("unimplemented sampling"));
552 GST_WARNING_OBJECT (depayload, "failed to alloc output buffer");
557 GST_WARNING_OBJECT (depayload, "length not multiple of pgroup");
562 GST_WARNING_OBJECT (depayload, "short packet");
568 gst_rtp_vraw_depay_handle_event (GstBaseRTPDepayload * filter, GstEvent * event)
571 GstRtpVRawDepay *rtpvrawdepay;
573 rtpvrawdepay = GST_RTP_VRAW_DEPAY (filter);
575 switch (GST_EVENT_TYPE (event)) {
576 case GST_EVENT_FLUSH_STOP:
577 gst_rtp_vraw_depay_reset (rtpvrawdepay);
584 GST_BASE_RTP_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
589 static GstStateChangeReturn
590 gst_rtp_vraw_depay_change_state (GstElement * element,
591 GstStateChange transition)
593 GstRtpVRawDepay *rtpvrawdepay;
594 GstStateChangeReturn ret;
596 rtpvrawdepay = GST_RTP_VRAW_DEPAY (element);
598 switch (transition) {
599 case GST_STATE_CHANGE_NULL_TO_READY:
601 case GST_STATE_CHANGE_READY_TO_PAUSED:
602 gst_rtp_vraw_depay_reset (rtpvrawdepay);
608 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
610 switch (transition) {
611 case GST_STATE_CHANGE_PAUSED_TO_READY:
612 gst_rtp_vraw_depay_reset (rtpvrawdepay);
614 case GST_STATE_CHANGE_READY_TO_NULL:
623 gst_rtp_vraw_depay_plugin_init (GstPlugin * plugin)
625 return gst_element_register (plugin, "rtpvrawdepay",
626 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_DEPAY);