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_pad_template (element_class,
70 gst_static_pad_template_get (&gst_rtp_vraw_depay_src_template));
71 gst_element_class_add_pad_template (element_class,
72 gst_static_pad_template_get (&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;
129 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
131 structure = gst_caps_get_structure (caps, 0);
133 yp = up = vp = uvstride = 0;
136 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
137 clock_rate = 90000; /* default */
138 depayload->clock_rate = clock_rate;
140 if (!(str = gst_structure_get_string (structure, "width")))
144 if (!(str = gst_structure_get_string (structure, "height")))
148 /* optional interlace value but we don't handle interlaced
150 if (gst_structure_get_string (structure, "interlace"))
153 if (!(str = gst_structure_get_string (structure, "sampling")))
156 if (!strcmp (str, "RGB")) {
157 format = GST_VIDEO_FORMAT_RGB;
159 ystride = GST_ROUND_UP_4 (width * 3);
160 outsize = ystride * height;
161 type = "video/x-raw-rgb";
162 } else if (!strcmp (str, "RGBA")) {
163 format = GST_VIDEO_FORMAT_RGBA;
166 outsize = ystride * height;
167 type = "video/x-raw-rgb";
168 } else if (!strcmp (str, "BGR")) {
169 format = GST_VIDEO_FORMAT_BGR;
171 ystride = GST_ROUND_UP_4 (width * 3);
172 outsize = ystride * height;
173 type = "video/x-raw-rgb";
174 } else if (!strcmp (str, "BGRA")) {
175 format = GST_VIDEO_FORMAT_BGRA;
178 outsize = ystride * height;
179 type = "video/x-raw-rgb";
180 } else if (!strcmp (str, "YCbCr-4:4:4")) {
181 format = GST_VIDEO_FORMAT_AYUV;
184 outsize = ystride * height;
185 type = "video/x-raw-yuv";
186 fourcc = GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
187 } else if (!strcmp (str, "YCbCr-4:2:2")) {
188 format = GST_VIDEO_FORMAT_UYVY;
190 ystride = GST_ROUND_UP_2 (width) * 2;
191 outsize = ystride * height;
192 type = "video/x-raw-yuv";
193 fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
195 } else if (!strcmp (str, "YCbCr-4:2:0")) {
196 format = GST_VIDEO_FORMAT_I420;
198 ystride = GST_ROUND_UP_4 (width);
199 uvstride = GST_ROUND_UP_8 (width) / 2;
200 up = ystride * GST_ROUND_UP_2 (height);
201 vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
202 outsize = vp + uvstride * GST_ROUND_UP_2 (height) / 2;
203 type = "video/x-raw-yuv";
204 fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
206 } else if (!strcmp (str, "YCbCr-4:1:1")) {
207 format = GST_VIDEO_FORMAT_Y41B;
209 ystride = GST_ROUND_UP_4 (width);
210 uvstride = GST_ROUND_UP_8 (width) / 4;
211 up = ystride * height;
212 vp = up + uvstride * height;
213 outsize = vp + uvstride * height;
214 type = "video/x-raw-yuv";
215 fourcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
220 rtpvrawdepay->width = width;
221 rtpvrawdepay->height = height;
222 rtpvrawdepay->format = format;
223 rtpvrawdepay->yp = yp;
224 rtpvrawdepay->up = up;
225 rtpvrawdepay->vp = vp;
226 rtpvrawdepay->pgroup = pgroup;
227 rtpvrawdepay->xinc = xinc;
228 rtpvrawdepay->yinc = yinc;
229 rtpvrawdepay->ystride = ystride;
230 rtpvrawdepay->uvstride = uvstride;
231 rtpvrawdepay->outsize = outsize;
233 srccaps = gst_caps_new_simple (type,
234 "width", G_TYPE_INT, width,
235 "height", G_TYPE_INT, height,
236 "format", GST_TYPE_FOURCC, fourcc,
237 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
239 res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
240 gst_caps_unref (srccaps);
242 GST_DEBUG_OBJECT (depayload, "width %d, height %d, format %d", width, height,
244 GST_DEBUG_OBJECT (depayload, "yp %d, up %d, vp %d", yp, up, vp);
245 GST_DEBUG_OBJECT (depayload, "xinc %d, yinc %d", xinc, yinc);
246 GST_DEBUG_OBJECT (depayload, "pgroup %d, ystride %d, uvstride %d", pgroup,
248 GST_DEBUG_OBJECT (depayload, "outsize %u", outsize);
255 GST_ERROR_OBJECT (depayload, "no width specified");
260 GST_ERROR_OBJECT (depayload, "no height specified");
265 GST_ERROR_OBJECT (depayload, "interlaced formats not supported yet");
270 GST_ERROR_OBJECT (depayload, "no sampling specified");
275 GST_ERROR_OBJECT (depayload, "unknown sampling format '%s'", str);
281 gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
283 GstRtpVRawDepay *rtpvrawdepay;
284 guint8 *payload, *data, *yp, *up, *vp, *headers;
286 guint cont, ystride, uvstride, pgroup, payload_len;
287 gint width, height, xinc, yinc;
289 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
291 timestamp = gst_rtp_buffer_get_timestamp (buf);
293 if (timestamp != rtpvrawdepay->timestamp || rtpvrawdepay->outbuf == NULL) {
297 GST_LOG_OBJECT (depayload, "new frame with timestamp %u", timestamp);
298 /* new timestamp, flush old buffer and create new output buffer */
299 if (rtpvrawdepay->outbuf) {
300 gst_base_rtp_depayload_push_ts (depayload, rtpvrawdepay->timestamp,
301 rtpvrawdepay->outbuf);
302 rtpvrawdepay->outbuf = NULL;
305 ret = gst_pad_alloc_buffer (depayload->srcpad, -1, rtpvrawdepay->outsize,
306 GST_PAD_CAPS (depayload->srcpad), &outbuf);
307 if (ret != GST_FLOW_OK)
310 /* clear timestamp from alloc... */
311 GST_BUFFER_TIMESTAMP (outbuf) = -1;
313 rtpvrawdepay->outbuf = outbuf;
314 rtpvrawdepay->timestamp = timestamp;
317 data = GST_BUFFER_DATA (rtpvrawdepay->outbuf);
319 /* get pointer and strides of the planes */
320 yp = data + rtpvrawdepay->yp;
321 up = data + rtpvrawdepay->up;
322 vp = data + rtpvrawdepay->vp;
324 ystride = rtpvrawdepay->ystride;
325 uvstride = rtpvrawdepay->uvstride;
326 pgroup = rtpvrawdepay->pgroup;
327 width = rtpvrawdepay->width;
328 height = rtpvrawdepay->height;
329 xinc = rtpvrawdepay->xinc;
330 yinc = rtpvrawdepay->yinc;
332 payload = gst_rtp_buffer_get_payload (buf);
333 payload_len = gst_rtp_buffer_get_payload_len (buf);
338 /* skip extended seqnum */
342 /* remember header position */
345 /* find data start */
350 cont = payload[4] & 0x80;
357 guint length, line, offs, plen;
360 /* stop when we run out of data */
361 if (payload_len == 0)
364 /* read length and cont. This should work because we iterated the headers
366 length = (headers[0] << 8) | headers[1];
367 line = ((headers[2] & 0x7f) << 8) | headers[3];
368 offs = ((headers[4] & 0x7f) << 8) | headers[5];
369 cont = headers[4] & 0x80;
372 /* length must be a multiple of pgroup */
373 if (length % pgroup != 0)
376 if (length > payload_len)
377 length = payload_len;
380 if (line > (height - yinc)) {
381 GST_WARNING_OBJECT (depayload, "skipping line %d: out of range", line);
384 if (offs > (width - xinc)) {
385 GST_WARNING_OBJECT (depayload, "skipping offset %d: out of range", offs);
389 /* calculate the maximim amount of bytes we can use per line */
390 if (offs + ((length / pgroup) * xinc) > width) {
391 plen = ((width - offs) * pgroup) / xinc;
392 GST_WARNING_OBJECT (depayload, "clipping length %d, offset %d, plen %d",
397 GST_LOG_OBJECT (depayload,
398 "writing length %u/%u, line %u, offset %u, remaining %u", plen, length,
399 line, offs, payload_len);
401 switch (rtpvrawdepay->format) {
402 case GST_VIDEO_FORMAT_RGB:
403 case GST_VIDEO_FORMAT_RGBA:
404 case GST_VIDEO_FORMAT_BGR:
405 case GST_VIDEO_FORMAT_BGRA:
406 case GST_VIDEO_FORMAT_UYVY:
407 /* samples are packed just like gstreamer packs them */
409 datap = yp + (line * ystride) + (offs * pgroup);
411 memcpy (datap, payload, plen);
413 case GST_VIDEO_FORMAT_AYUV:
418 datap = yp + (line * ystride) + (offs * 4);
421 /* samples are packed in order Cb-Y-Cr for both interlaced and
422 * progressive frames */
423 for (i = 0; i < plen; i += pgroup) {
432 case GST_VIDEO_FORMAT_I420:
436 guint8 *yd1p, *yd2p, *udp, *vdp, *p;
438 yd1p = yp + (line * ystride) + (offs);
439 yd2p = yd1p + ystride;
440 uvoff = (line / yinc * uvstride) + (offs / xinc);
446 /* line 0/1: Y00-Y01-Y10-Y11-Cb00-Cr00 Y02-Y03-Y12-Y13-Cb01-Cr01 ... */
447 for (i = 0; i < plen; i += pgroup) {
458 case GST_VIDEO_FORMAT_Y41B:
462 guint8 *ydp, *udp, *vdp, *p;
464 ydp = yp + (line * ystride) + (offs);
465 uvoff = (line / yinc * uvstride) + (offs / xinc);
471 /* Samples are packed in order Cb0-Y0-Y1-Cr0-Y2-Y3 for both interlaced
472 * and progressive scan lines */
473 for (i = 0; i < plen; i += pgroup) {
485 goto unknown_sampling;
493 payload_len -= length;
496 if (gst_rtp_buffer_get_marker (buf)) {
497 GST_LOG_OBJECT (depayload, "marker, flushing frame");
498 if (rtpvrawdepay->outbuf) {
499 gst_base_rtp_depayload_push_ts (depayload, timestamp,
500 rtpvrawdepay->outbuf);
501 rtpvrawdepay->outbuf = NULL;
503 rtpvrawdepay->timestamp = -1;
510 GST_ELEMENT_ERROR (depayload, STREAM, FORMAT,
511 (NULL), ("unimplemented sampling"));
516 GST_WARNING_OBJECT (depayload, "failed to alloc output buffer");
521 GST_WARNING_OBJECT (depayload, "length not multiple of pgroup");
526 GST_WARNING_OBJECT (depayload, "short packet");
532 gst_rtp_vraw_depay_handle_event (GstBaseRTPDepayload * filter, GstEvent * event)
535 GstRtpVRawDepay *rtpvrawdepay;
537 rtpvrawdepay = GST_RTP_VRAW_DEPAY (filter);
539 switch (GST_EVENT_TYPE (event)) {
540 case GST_EVENT_FLUSH_STOP:
541 gst_rtp_vraw_depay_reset (rtpvrawdepay);
548 GST_BASE_RTP_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
553 static GstStateChangeReturn
554 gst_rtp_vraw_depay_change_state (GstElement * element,
555 GstStateChange transition)
557 GstRtpVRawDepay *rtpvrawdepay;
558 GstStateChangeReturn ret;
560 rtpvrawdepay = GST_RTP_VRAW_DEPAY (element);
562 switch (transition) {
563 case GST_STATE_CHANGE_NULL_TO_READY:
565 case GST_STATE_CHANGE_READY_TO_PAUSED:
566 gst_rtp_vraw_depay_reset (rtpvrawdepay);
572 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
574 switch (transition) {
575 case GST_STATE_CHANGE_PAUSED_TO_READY:
576 gst_rtp_vraw_depay_reset (rtpvrawdepay);
578 case GST_STATE_CHANGE_READY_TO_NULL:
587 gst_rtp_vraw_depay_plugin_init (GstPlugin * plugin)
589 return gst_element_register (plugin, "rtpvrawdepay",
590 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_DEPAY);