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 /* elementfactory information */
34 static const GstElementDetails gst_rtp_vrawdepay_details =
35 GST_ELEMENT_DETAILS ("RTP Raw Video depayloader",
36 "Codec/Depayloader/Network",
37 "Extracts raw video from RTP packets (RFC 4175)",
38 "Wim Taymans <wim.taymans@gmail.com>");
40 static GstStaticPadTemplate gst_rtp_vraw_depay_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS ("video/x-raw-rgb; video/x-raw-yuv")
47 static GstStaticPadTemplate gst_rtp_vraw_depay_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
51 GST_STATIC_CAPS ("application/x-rtp, "
52 "media = (string) \"video\", "
53 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
54 "clock-rate = (int) 90000, " "encoding-name = (string) \"RAW\"")
57 GST_BOILERPLATE (GstRtpVRawDepay, gst_rtp_vraw_depay, GstBaseRTPDepayload,
58 GST_TYPE_BASE_RTP_DEPAYLOAD);
60 static gboolean gst_rtp_vraw_depay_setcaps (GstBaseRTPDepayload * depayload,
62 static GstBuffer *gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload,
65 static GstStateChangeReturn gst_rtp_vraw_depay_change_state (GstElement *
66 element, GstStateChange transition);
69 gst_rtp_vraw_depay_base_init (gpointer klass)
71 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
73 gst_element_class_add_pad_template (element_class,
74 gst_static_pad_template_get (&gst_rtp_vraw_depay_src_template));
75 gst_element_class_add_pad_template (element_class,
76 gst_static_pad_template_get (&gst_rtp_vraw_depay_sink_template));
78 gst_element_class_set_details (element_class, &gst_rtp_vrawdepay_details);
82 gst_rtp_vraw_depay_class_init (GstRtpVRawDepayClass * klass)
84 GstElementClass *gstelement_class;
85 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
87 gstelement_class = (GstElementClass *) klass;
88 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
90 parent_class = g_type_class_peek_parent (klass);
92 gstelement_class->change_state = gst_rtp_vraw_depay_change_state;
94 gstbasertpdepayload_class->set_caps = gst_rtp_vraw_depay_setcaps;
95 gstbasertpdepayload_class->process = gst_rtp_vraw_depay_process;
97 GST_DEBUG_CATEGORY_INIT (rtpvrawdepay_debug, "rtpvrawdepay", 0,
98 "raw video RTP Depayloader");
102 gst_rtp_vraw_depay_init (GstRtpVRawDepay * rtpvrawdepay,
103 GstRtpVRawDepayClass * klass)
105 /* needed because of GST_BOILERPLATE */
109 gst_rtp_vraw_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
111 GstStructure *structure;
112 GstRtpVRawDepay *rtpvrawdepay;
114 const gchar *str, *type;
115 gint format, width, height, pgroup, xinc, yinc;
116 guint ystride, uvstride, yp, up, vp, outsize;
121 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
123 structure = gst_caps_get_structure (caps, 0);
125 yp = up = vp = uvstride = 0;
128 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
129 clock_rate = 90000; /* default */
130 depayload->clock_rate = clock_rate;
132 if (!(str = gst_structure_get_string (structure, "width")))
136 if (!(str = gst_structure_get_string (structure, "height")))
140 /* optional interlace value but we don't handle interlaced
142 if ((str = gst_structure_get_string (structure, "interlace")))
145 if (!(str = gst_structure_get_string (structure, "sampling")))
148 if (!strcmp (str, "RGB")) {
149 format = GST_VIDEO_FORMAT_RGB;
151 ystride = GST_ROUND_UP_4 (width * 3);
152 outsize = ystride * height;
153 type = "video/x-raw-rgb";
154 } else if (!strcmp (str, "RGBA")) {
155 format = GST_VIDEO_FORMAT_RGBA;
158 outsize = ystride * height;
159 type = "video/x-raw-rgb";
160 } else if (!strcmp (str, "BGR")) {
161 format = GST_VIDEO_FORMAT_BGR;
163 ystride = GST_ROUND_UP_4 (width * 3);
164 outsize = ystride * height;
165 type = "video/x-raw-rgb";
166 } else if (!strcmp (str, "BGRA")) {
167 format = GST_VIDEO_FORMAT_BGRA;
170 outsize = ystride * height;
171 type = "video/x-raw-rgb";
172 } else if (!strcmp (str, "YCbCr-4:4:4")) {
173 format = GST_VIDEO_FORMAT_AYUV;
176 outsize = ystride * height;
177 type = "video/x-raw-yuv";
178 fourcc = GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
179 } else if (!strcmp (str, "YCbCr-4:2:2")) {
180 format = GST_VIDEO_FORMAT_UYVY;
182 ystride = GST_ROUND_UP_2 (width) * 2;
183 outsize = ystride * height;
184 type = "video/x-raw-yuv";
185 fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
187 } else if (!strcmp (str, "YCbCr-4:2:0")) {
188 format = GST_VIDEO_FORMAT_I420;
190 ystride = GST_ROUND_UP_4 (width);
191 uvstride = GST_ROUND_UP_8 (width) / 2;
192 up = ystride * GST_ROUND_UP_2 (height);
193 vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
194 outsize = vp + uvstride * GST_ROUND_UP_2 (height) / 2;
195 type = "video/x-raw-yuv";
196 fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
198 } else if (!strcmp (str, "YCbCr-4:1:1")) {
199 format = GST_VIDEO_FORMAT_Y41B;
201 ystride = GST_ROUND_UP_4 (width);
202 uvstride = GST_ROUND_UP_8 (width) / 4;
203 up = ystride * height;
204 vp = up + uvstride * height;
205 outsize = vp + uvstride * height;
206 type = "video/x-raw-yuv";
207 fourcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
212 rtpvrawdepay->width = width;
213 rtpvrawdepay->height = height;
214 rtpvrawdepay->format = format;
215 rtpvrawdepay->yp = yp;
216 rtpvrawdepay->up = up;
217 rtpvrawdepay->vp = vp;
218 rtpvrawdepay->pgroup = pgroup;
219 rtpvrawdepay->xinc = xinc;
220 rtpvrawdepay->yinc = yinc;
221 rtpvrawdepay->ystride = ystride;
222 rtpvrawdepay->uvstride = uvstride;
223 rtpvrawdepay->outsize = outsize;
225 srccaps = gst_caps_new_simple (type,
226 "width", G_TYPE_INT, width,
227 "height", G_TYPE_INT, height,
228 "format", GST_TYPE_FOURCC, fourcc,
229 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
231 res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
232 gst_caps_unref (srccaps);
234 GST_DEBUG_OBJECT (depayload, "width %d, height %d, format %d", width, height,
236 GST_DEBUG_OBJECT (depayload, "yp %d, up %d, vp %d", yp, up, vp);
237 GST_DEBUG_OBJECT (depayload, "pgroup %d, ystride %d, uvstride %d", pgroup,
239 GST_DEBUG_OBJECT (depayload, "outsize %u", outsize);
246 GST_ERROR_OBJECT (depayload, "no width specified");
251 GST_ERROR_OBJECT (depayload, "no height specified");
256 GST_ERROR_OBJECT (depayload, "interlaced formats not supported yet");
261 GST_ERROR_OBJECT (depayload, "no sampling specified");
266 GST_ERROR_OBJECT (depayload, "unknown sampling format '%s'", str);
272 gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
274 GstRtpVRawDepay *rtpvrawdepay;
276 guint8 *payload, *data, *yp, *up, *vp, *headers;
278 guint cont, ystride, uvstride, pgroup;
280 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
282 payload_len = gst_rtp_buffer_get_payload_len (buf);
284 timestamp = gst_rtp_buffer_get_timestamp (buf);
286 if (timestamp != rtpvrawdepay->timestamp || rtpvrawdepay->outbuf == NULL) {
290 GST_LOG_OBJECT (depayload, "new frame with timestamp %u", timestamp);
291 /* new timestamp, flush old buffer and create new output buffer */
292 if (rtpvrawdepay->outbuf) {
293 gst_base_rtp_depayload_push_ts (depayload, rtpvrawdepay->timestamp,
294 rtpvrawdepay->outbuf);
295 rtpvrawdepay->outbuf = NULL;
298 ret = gst_pad_alloc_buffer (depayload->srcpad, -1, rtpvrawdepay->outsize,
299 GST_PAD_CAPS (depayload->srcpad), &outbuf);
300 if (ret != GST_FLOW_OK)
303 /* clear timestamp from alloc... */
304 GST_BUFFER_TIMESTAMP (outbuf) = -1;
306 rtpvrawdepay->outbuf = outbuf;
307 rtpvrawdepay->timestamp = timestamp;
310 data = GST_BUFFER_DATA (rtpvrawdepay->outbuf);
312 /* get pointer and strides of the planes */
313 yp = data + rtpvrawdepay->yp;
314 up = data + rtpvrawdepay->up;
315 vp = data + rtpvrawdepay->vp;
317 ystride = rtpvrawdepay->ystride;
318 uvstride = rtpvrawdepay->uvstride;
319 pgroup = rtpvrawdepay->pgroup;
321 payload = gst_rtp_buffer_get_payload (buf);
323 /* skip extended seqnum */
327 /* remember header position */
330 /* find data start */
332 cont = payload[4] & 0x80;
337 guint length, line, offs;
340 /* read length and cont */
341 length = (headers[0] << 8) | headers[1];
342 line = ((headers[2] & 0x7f) << 8) | headers[3];
343 offs = ((headers[4] & 0x7f) << 8) | headers[5];
344 cont = headers[4] & 0x80;
348 if (line > (rtpvrawdepay->height - rtpvrawdepay->yinc))
350 if (offs > (rtpvrawdepay->width - rtpvrawdepay->xinc))
353 GST_LOG_OBJECT (depayload, "writing length %u, line %u, offset %u", length,
356 switch (rtpvrawdepay->format) {
357 case GST_VIDEO_FORMAT_RGB:
358 case GST_VIDEO_FORMAT_RGBA:
359 case GST_VIDEO_FORMAT_BGR:
360 case GST_VIDEO_FORMAT_BGRA:
361 case GST_VIDEO_FORMAT_UYVY:
362 /* samples are packed just like gstreamer packs them */
363 offs /= rtpvrawdepay->xinc;
364 datap = yp + (line * ystride) + (offs * pgroup);
365 memcpy (datap, payload, length);
368 case GST_VIDEO_FORMAT_AYUV:
372 datap = yp + (line * ystride) + (offs * 4);
374 /* samples are packed in order Cb-Y-Cr for both interlaced and
375 * progressive frames */
376 for (i = 0; i < length; i += pgroup) {
378 *datap++ = payload[1];
379 *datap++ = payload[0];
380 *datap++ = payload[2];
385 case GST_VIDEO_FORMAT_I420:
389 guint8 *yd1p, *yd2p, *udp, *vdp;
391 yd1p = yp + (line * ystride) + (offs);
392 yd2p = yd1p + ystride;
394 (line / rtpvrawdepay->yinc * uvstride) +
395 (offs / rtpvrawdepay->xinc);
399 /* line 0/1: Y00-Y01-Y10-Y11-Cb00-Cr00 Y02-Y03-Y12-Y13-Cb01-Cr01 ... */
400 for (i = 0; i < length; i += pgroup) {
401 *yd1p++ = payload[0];
402 *yd1p++ = payload[1];
403 *yd2p++ = payload[2];
404 *yd2p++ = payload[3];
411 case GST_VIDEO_FORMAT_Y41B:
415 guint8 *ydp, *udp, *vdp;
417 ydp = yp + (line * ystride) + (offs);
419 (line / rtpvrawdepay->yinc * uvstride) +
420 (offs / rtpvrawdepay->xinc);
424 /* Samples are packed in order Cb0-Y0-Y1-Cr0-Y2-Y3 for both interlaced
425 * and progressive scan lines */
426 for (i = 0; i < length; i += pgroup) {
438 goto unknown_sampling;
445 if (gst_rtp_buffer_get_marker (buf)) {
446 GST_LOG_OBJECT (depayload, "marker, flushing frame");
447 if (rtpvrawdepay->outbuf) {
448 gst_base_rtp_depayload_push_ts (depayload, timestamp,
449 rtpvrawdepay->outbuf);
450 rtpvrawdepay->outbuf = NULL;
452 rtpvrawdepay->timestamp = -1;
459 GST_ELEMENT_ERROR (depayload, STREAM, FORMAT,
460 (NULL), ("unimplemented sampling"));
465 GST_DEBUG_OBJECT (depayload, "failed to alloc output buffer");
470 static GstStateChangeReturn
471 gst_rtp_vraw_depay_change_state (GstElement * element,
472 GstStateChange transition)
474 GstRtpVRawDepay *rtpvrawdepay;
475 GstStateChangeReturn ret;
477 rtpvrawdepay = GST_RTP_VRAW_DEPAY (element);
479 switch (transition) {
480 case GST_STATE_CHANGE_NULL_TO_READY:
482 case GST_STATE_CHANGE_READY_TO_PAUSED:
483 rtpvrawdepay->timestamp = -1;
489 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
491 switch (transition) {
492 case GST_STATE_CHANGE_PAUSED_TO_READY:
493 if (rtpvrawdepay->outbuf) {
494 gst_buffer_unref (rtpvrawdepay->outbuf);
495 rtpvrawdepay->outbuf = NULL;
498 case GST_STATE_CHANGE_READY_TO_NULL:
507 gst_rtp_vraw_depay_plugin_init (GstPlugin * plugin)
509 return gst_element_register (plugin, "rtpvrawdepay",
510 GST_RANK_MARGINAL, GST_TYPE_RTP_VRAW_DEPAY);