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);
62 gst_rtp_vraw_depay_base_init (gpointer klass)
64 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
66 gst_element_class_add_pad_template (element_class,
67 gst_static_pad_template_get (&gst_rtp_vraw_depay_src_template));
68 gst_element_class_add_pad_template (element_class,
69 gst_static_pad_template_get (&gst_rtp_vraw_depay_sink_template));
71 gst_element_class_set_details_simple (element_class,
72 "RTP Raw Video depayloader", "Codec/Depayloader/Network",
73 "Extracts raw video from RTP packets (RFC 4175)",
74 "Wim Taymans <wim.taymans@gmail.com>");
78 gst_rtp_vraw_depay_class_init (GstRtpVRawDepayClass * klass)
80 GstElementClass *gstelement_class;
81 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
83 gstelement_class = (GstElementClass *) klass;
84 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
86 gstelement_class->change_state = gst_rtp_vraw_depay_change_state;
88 gstbasertpdepayload_class->set_caps = gst_rtp_vraw_depay_setcaps;
89 gstbasertpdepayload_class->process = gst_rtp_vraw_depay_process;
91 GST_DEBUG_CATEGORY_INIT (rtpvrawdepay_debug, "rtpvrawdepay", 0,
92 "raw video RTP Depayloader");
96 gst_rtp_vraw_depay_init (GstRtpVRawDepay * rtpvrawdepay,
97 GstRtpVRawDepayClass * klass)
99 /* needed because of GST_BOILERPLATE */
103 gst_rtp_vraw_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
105 GstStructure *structure;
106 GstRtpVRawDepay *rtpvrawdepay;
108 const gchar *str, *type;
109 gint format, width, height, pgroup, xinc, yinc;
110 guint ystride, uvstride, yp, up, vp, outsize;
115 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
117 structure = gst_caps_get_structure (caps, 0);
119 yp = up = vp = uvstride = 0;
122 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
123 clock_rate = 90000; /* default */
124 depayload->clock_rate = clock_rate;
126 if (!(str = gst_structure_get_string (structure, "width")))
130 if (!(str = gst_structure_get_string (structure, "height")))
134 /* optional interlace value but we don't handle interlaced
136 if (gst_structure_get_string (structure, "interlace"))
139 if (!(str = gst_structure_get_string (structure, "sampling")))
142 if (!strcmp (str, "RGB")) {
143 format = GST_VIDEO_FORMAT_RGB;
145 ystride = GST_ROUND_UP_4 (width * 3);
146 outsize = ystride * height;
147 type = "video/x-raw-rgb";
148 } else if (!strcmp (str, "RGBA")) {
149 format = GST_VIDEO_FORMAT_RGBA;
152 outsize = ystride * height;
153 type = "video/x-raw-rgb";
154 } else if (!strcmp (str, "BGR")) {
155 format = GST_VIDEO_FORMAT_BGR;
157 ystride = GST_ROUND_UP_4 (width * 3);
158 outsize = ystride * height;
159 type = "video/x-raw-rgb";
160 } else if (!strcmp (str, "BGRA")) {
161 format = GST_VIDEO_FORMAT_BGRA;
164 outsize = ystride * height;
165 type = "video/x-raw-rgb";
166 } else if (!strcmp (str, "YCbCr-4:4:4")) {
167 format = GST_VIDEO_FORMAT_AYUV;
170 outsize = ystride * height;
171 type = "video/x-raw-yuv";
172 fourcc = GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
173 } else if (!strcmp (str, "YCbCr-4:2:2")) {
174 format = GST_VIDEO_FORMAT_UYVY;
176 ystride = GST_ROUND_UP_2 (width) * 2;
177 outsize = ystride * height;
178 type = "video/x-raw-yuv";
179 fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
181 } else if (!strcmp (str, "YCbCr-4:2:0")) {
182 format = GST_VIDEO_FORMAT_I420;
184 ystride = GST_ROUND_UP_4 (width);
185 uvstride = GST_ROUND_UP_8 (width) / 2;
186 up = ystride * GST_ROUND_UP_2 (height);
187 vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
188 outsize = vp + uvstride * GST_ROUND_UP_2 (height) / 2;
189 type = "video/x-raw-yuv";
190 fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
192 } else if (!strcmp (str, "YCbCr-4:1:1")) {
193 format = GST_VIDEO_FORMAT_Y41B;
195 ystride = GST_ROUND_UP_4 (width);
196 uvstride = GST_ROUND_UP_8 (width) / 4;
197 up = ystride * height;
198 vp = up + uvstride * height;
199 outsize = vp + uvstride * height;
200 type = "video/x-raw-yuv";
201 fourcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
206 rtpvrawdepay->width = width;
207 rtpvrawdepay->height = height;
208 rtpvrawdepay->format = format;
209 rtpvrawdepay->yp = yp;
210 rtpvrawdepay->up = up;
211 rtpvrawdepay->vp = vp;
212 rtpvrawdepay->pgroup = pgroup;
213 rtpvrawdepay->xinc = xinc;
214 rtpvrawdepay->yinc = yinc;
215 rtpvrawdepay->ystride = ystride;
216 rtpvrawdepay->uvstride = uvstride;
217 rtpvrawdepay->outsize = outsize;
219 srccaps = gst_caps_new_simple (type,
220 "width", G_TYPE_INT, width,
221 "height", G_TYPE_INT, height,
222 "format", GST_TYPE_FOURCC, fourcc,
223 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
225 res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
226 gst_caps_unref (srccaps);
228 GST_DEBUG_OBJECT (depayload, "width %d, height %d, format %d", width, height,
230 GST_DEBUG_OBJECT (depayload, "yp %d, up %d, vp %d", yp, up, vp);
231 GST_DEBUG_OBJECT (depayload, "pgroup %d, ystride %d, uvstride %d", pgroup,
233 GST_DEBUG_OBJECT (depayload, "outsize %u", outsize);
240 GST_ERROR_OBJECT (depayload, "no width specified");
245 GST_ERROR_OBJECT (depayload, "no height specified");
250 GST_ERROR_OBJECT (depayload, "interlaced formats not supported yet");
255 GST_ERROR_OBJECT (depayload, "no sampling specified");
260 GST_ERROR_OBJECT (depayload, "unknown sampling format '%s'", str);
266 gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
268 GstRtpVRawDepay *rtpvrawdepay;
269 guint8 *payload, *data, *dataend, *yp, *up, *vp, *headers;
271 guint cont, ystride, uvstride, pgroup, payload_len, size;
272 gint width, height, xinc, yinc;
274 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
276 timestamp = gst_rtp_buffer_get_timestamp (buf);
278 if (timestamp != rtpvrawdepay->timestamp || rtpvrawdepay->outbuf == NULL) {
282 GST_LOG_OBJECT (depayload, "new frame with timestamp %u", timestamp);
283 /* new timestamp, flush old buffer and create new output buffer */
284 if (rtpvrawdepay->outbuf) {
285 gst_base_rtp_depayload_push_ts (depayload, rtpvrawdepay->timestamp,
286 rtpvrawdepay->outbuf);
287 rtpvrawdepay->outbuf = NULL;
290 ret = gst_pad_alloc_buffer (depayload->srcpad, -1, rtpvrawdepay->outsize,
291 GST_PAD_CAPS (depayload->srcpad), &outbuf);
292 if (ret != GST_FLOW_OK)
295 /* clear timestamp from alloc... */
296 GST_BUFFER_TIMESTAMP (outbuf) = -1;
298 rtpvrawdepay->outbuf = outbuf;
299 rtpvrawdepay->timestamp = timestamp;
302 data = GST_BUFFER_DATA (rtpvrawdepay->outbuf);
303 size = GST_BUFFER_SIZE (rtpvrawdepay->outbuf);
304 dataend = data + size;
306 /* get pointer and strides of the planes */
307 yp = data + rtpvrawdepay->yp;
308 up = data + rtpvrawdepay->up;
309 vp = data + rtpvrawdepay->vp;
311 ystride = rtpvrawdepay->ystride;
312 uvstride = rtpvrawdepay->uvstride;
313 pgroup = rtpvrawdepay->pgroup;
314 width = rtpvrawdepay->width;
315 height = rtpvrawdepay->height;
316 xinc = rtpvrawdepay->xinc;
317 yinc = rtpvrawdepay->yinc;
319 payload = gst_rtp_buffer_get_payload (buf);
320 payload_len = gst_rtp_buffer_get_payload_len (buf);
325 /* skip extended seqnum */
329 /* remember header position */
332 /* find data start */
337 cont = payload[4] & 0x80;
344 guint length, line, offs, plen;
347 /* stop when we run out of data */
348 if (payload_len == 0)
351 /* read length and cont. This should work because we iterated the headers
353 length = (headers[0] << 8) | headers[1];
354 line = ((headers[2] & 0x7f) << 8) | headers[3];
355 offs = ((headers[4] & 0x7f) << 8) | headers[5];
356 cont = headers[4] & 0x80;
359 /* length must be a multiple of pgroup */
360 if (length % pgroup != 0)
363 if (length > payload_len)
364 length = payload_len;
367 if (line > (height - yinc)) {
368 GST_WARNING_OBJECT (depayload, "skipping line %d: out of range", line);
371 if (offs > (width - xinc)) {
372 GST_WARNING_OBJECT (depayload, "skipping offset %d: out of range", offs);
376 /* calculate the maximim amount of bytes we can use per line */
377 if (offs + ((length / pgroup) * xinc) > (width - xinc)) {
378 plen = ((width - offs) * pgroup) / xinc;
379 GST_WARNING_OBJECT (depayload, "clipping length %d, offset %d", length,
384 GST_LOG_OBJECT (depayload,
385 "writing length %u/%u, line %u, offset %u, remaining %u", plen, length,
386 line, offs, payload_len);
388 switch (rtpvrawdepay->format) {
389 case GST_VIDEO_FORMAT_RGB:
390 case GST_VIDEO_FORMAT_RGBA:
391 case GST_VIDEO_FORMAT_BGR:
392 case GST_VIDEO_FORMAT_BGRA:
393 case GST_VIDEO_FORMAT_UYVY:
394 /* samples are packed just like gstreamer packs them */
396 datap = yp + (line * ystride) + (offs * pgroup);
398 memcpy (datap, payload, plen);
400 case GST_VIDEO_FORMAT_AYUV:
405 datap = yp + (line * ystride) + (offs * 4);
408 /* samples are packed in order Cb-Y-Cr for both interlaced and
409 * progressive frames */
410 for (i = 0; i < plen; i += pgroup) {
419 case GST_VIDEO_FORMAT_I420:
423 guint8 *yd1p, *yd2p, *udp, *vdp, *p;
425 yd1p = yp + (line * ystride) + (offs);
426 yd2p = yd1p + ystride;
427 uvoff = (line / yinc * uvstride) + (offs / xinc);
433 /* line 0/1: Y00-Y01-Y10-Y11-Cb00-Cr00 Y02-Y03-Y12-Y13-Cb01-Cr01 ... */
434 for (i = 0; i < plen; i += pgroup) {
445 case GST_VIDEO_FORMAT_Y41B:
449 guint8 *ydp, *udp, *vdp, *p;
451 ydp = yp + (line * ystride) + (offs);
452 uvoff = (line / yinc * uvstride) + (offs / xinc);
458 /* Samples are packed in order Cb0-Y0-Y1-Cr0-Y2-Y3 for both interlaced
459 * and progressive scan lines */
460 for (i = 0; i < plen; i += pgroup) {
472 goto unknown_sampling;
480 payload_len -= length;
483 if (gst_rtp_buffer_get_marker (buf)) {
484 GST_LOG_OBJECT (depayload, "marker, flushing frame");
485 if (rtpvrawdepay->outbuf) {
486 gst_base_rtp_depayload_push_ts (depayload, timestamp,
487 rtpvrawdepay->outbuf);
488 rtpvrawdepay->outbuf = NULL;
490 rtpvrawdepay->timestamp = -1;
497 GST_ELEMENT_ERROR (depayload, STREAM, FORMAT,
498 (NULL), ("unimplemented sampling"));
503 GST_WARNING_OBJECT (depayload, "failed to alloc output buffer");
508 GST_WARNING_OBJECT (depayload, "length not multiple of pgroup");
513 GST_WARNING_OBJECT (depayload, "short packet");
518 static GstStateChangeReturn
519 gst_rtp_vraw_depay_change_state (GstElement * element,
520 GstStateChange transition)
522 GstRtpVRawDepay *rtpvrawdepay;
523 GstStateChangeReturn ret;
525 rtpvrawdepay = GST_RTP_VRAW_DEPAY (element);
527 switch (transition) {
528 case GST_STATE_CHANGE_NULL_TO_READY:
530 case GST_STATE_CHANGE_READY_TO_PAUSED:
531 rtpvrawdepay->timestamp = -1;
537 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
539 switch (transition) {
540 case GST_STATE_CHANGE_PAUSED_TO_READY:
541 if (rtpvrawdepay->outbuf) {
542 gst_buffer_unref (rtpvrawdepay->outbuf);
543 rtpvrawdepay->outbuf = NULL;
546 case GST_STATE_CHANGE_READY_TO_NULL:
555 gst_rtp_vraw_depay_plugin_init (GstPlugin * plugin)
557 return gst_element_register (plugin, "rtpvrawdepay",
558 GST_RANK_MARGINAL, GST_TYPE_RTP_VRAW_DEPAY);