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")
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 #define gst_rtp_vraw_depay_parent_class parent_class
51 G_DEFINE_TYPE (GstRtpVRawDepay, gst_rtp_vraw_depay,
52 GST_TYPE_RTP_BASE_DEPAYLOAD);
54 static gboolean gst_rtp_vraw_depay_setcaps (GstRTPBaseDepayload * depayload,
56 static GstBuffer *gst_rtp_vraw_depay_process (GstRTPBaseDepayload * depayload,
59 static GstStateChangeReturn gst_rtp_vraw_depay_change_state (GstElement *
60 element, GstStateChange transition);
62 static gboolean gst_rtp_vraw_depay_handle_event (GstRTPBaseDepayload * filter,
66 gst_rtp_vraw_depay_class_init (GstRtpVRawDepayClass * klass)
68 GstElementClass *gstelement_class;
69 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
71 gstelement_class = (GstElementClass *) klass;
72 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
74 gstelement_class->change_state = gst_rtp_vraw_depay_change_state;
76 gstrtpbasedepayload_class->set_caps = gst_rtp_vraw_depay_setcaps;
77 gstrtpbasedepayload_class->process = gst_rtp_vraw_depay_process;
78 gstrtpbasedepayload_class->handle_event = gst_rtp_vraw_depay_handle_event;
80 gst_element_class_add_pad_template (gstelement_class,
81 gst_static_pad_template_get (&gst_rtp_vraw_depay_src_template));
82 gst_element_class_add_pad_template (gstelement_class,
83 gst_static_pad_template_get (&gst_rtp_vraw_depay_sink_template));
85 gst_element_class_set_details_simple (gstelement_class,
86 "RTP Raw Video depayloader", "Codec/Depayloader/Network/RTP",
87 "Extracts raw video from RTP packets (RFC 4175)",
88 "Wim Taymans <wim.taymans@gmail.com>");
90 GST_DEBUG_CATEGORY_INIT (rtpvrawdepay_debug, "rtpvrawdepay", 0,
91 "raw video RTP Depayloader");
95 gst_rtp_vraw_depay_init (GstRtpVRawDepay * rtpvrawdepay)
97 /* needed because of GST_BOILERPLATE */
101 gst_rtp_vraw_depay_reset (GstRtpVRawDepay * rtpvrawdepay)
103 if (rtpvrawdepay->outbuf) {
104 gst_buffer_unref (rtpvrawdepay->outbuf);
105 rtpvrawdepay->outbuf = NULL;
107 rtpvrawdepay->timestamp = -1;
108 if (rtpvrawdepay->pool) {
109 gst_buffer_pool_set_active (rtpvrawdepay->pool, FALSE);
110 gst_object_unref (rtpvrawdepay->pool);
111 rtpvrawdepay->pool = NULL;
116 gst_rtp_vraw_depay_negotiate_pool (GstRtpVRawDepay * depay, GstCaps * caps,
120 GstBufferPool *pool = NULL;
121 guint size, min, max, prefix, padding, alignment;
122 GstStructure *config;
124 /* find a pool for the negotiated caps now */
125 query = gst_query_new_allocation (caps, TRUE);
127 if (gst_pad_peer_query (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depay), query)) {
128 GST_DEBUG_OBJECT (depay, "got downstream ALLOCATION hints");
129 /* we got configuration from our peer, parse them */
130 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
131 &padding, &alignment, &pool);
133 GST_DEBUG_OBJECT (depay, "didn't get downstream ALLOCATION hints");
142 /* we did not get a pool, make one ourselves then */
143 pool = gst_buffer_pool_new ();
147 gst_object_unref (depay->pool);
150 config = gst_buffer_pool_get_config (pool);
151 gst_buffer_pool_config_set (config, caps, size, min, max, prefix, padding,
153 /* just set the metadata, if the pool can support it we will transparently use
154 * it through the video info API. We could also see if the pool support this
155 * metadata and only activate it then. */
156 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
158 gst_buffer_pool_set_config (pool, config);
160 gst_buffer_pool_set_active (pool, TRUE);
162 gst_query_unref (query);
168 gst_rtp_vraw_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
170 GstStructure *structure;
171 GstRtpVRawDepay *rtpvrawdepay;
174 gint format, width, height, pgroup, xinc, yinc;
179 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
181 structure = gst_caps_get_structure (caps, 0);
185 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
186 clock_rate = 90000; /* default */
187 depayload->clock_rate = clock_rate;
189 if (!(str = gst_structure_get_string (structure, "width")))
193 if (!(str = gst_structure_get_string (structure, "height")))
197 /* optional interlace value but we don't handle interlaced
199 if (gst_structure_get_string (structure, "interlace"))
202 if (!(str = gst_structure_get_string (structure, "sampling")))
205 if (!strcmp (str, "RGB")) {
206 format = GST_VIDEO_FORMAT_RGB;
208 } else if (!strcmp (str, "RGBA")) {
209 format = GST_VIDEO_FORMAT_RGBA;
211 } else if (!strcmp (str, "BGR")) {
212 format = GST_VIDEO_FORMAT_BGR;
214 } else if (!strcmp (str, "BGRA")) {
215 format = GST_VIDEO_FORMAT_BGRA;
217 } else if (!strcmp (str, "YCbCr-4:4:4")) {
218 format = GST_VIDEO_FORMAT_AYUV;
220 } else if (!strcmp (str, "YCbCr-4:2:2")) {
221 format = GST_VIDEO_FORMAT_UYVY;
224 } else if (!strcmp (str, "YCbCr-4:2:0")) {
225 format = GST_VIDEO_FORMAT_I420;
228 } else if (!strcmp (str, "YCbCr-4:1:1")) {
229 format = GST_VIDEO_FORMAT_Y41B;
235 gst_video_info_init (&rtpvrawdepay->vinfo);
236 gst_video_info_set_format (&rtpvrawdepay->vinfo, format, width, height);
237 GST_VIDEO_INFO_FPS_N (&rtpvrawdepay->vinfo) = 0;
238 GST_VIDEO_INFO_FPS_D (&rtpvrawdepay->vinfo) = 1;
240 rtpvrawdepay->pgroup = pgroup;
241 rtpvrawdepay->xinc = xinc;
242 rtpvrawdepay->yinc = yinc;
244 srccaps = gst_video_info_to_caps (&rtpvrawdepay->vinfo);
245 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
246 gst_caps_unref (srccaps);
248 GST_DEBUG_OBJECT (depayload, "width %d, height %d, format %d", width, height,
250 GST_DEBUG_OBJECT (depayload, "xinc %d, yinc %d, pgroup %d",
253 /* negotiate a bufferpool */
254 if ((ret = gst_rtp_vraw_depay_negotiate_pool (rtpvrawdepay, caps,
255 &rtpvrawdepay->vinfo)) != GST_FLOW_OK)
263 GST_ERROR_OBJECT (depayload, "no width specified");
268 GST_ERROR_OBJECT (depayload, "no height specified");
273 GST_ERROR_OBJECT (depayload, "interlaced formats not supported yet");
278 GST_ERROR_OBJECT (depayload, "no sampling specified");
283 GST_ERROR_OBJECT (depayload, "unknown sampling format '%s'", str);
288 GST_DEBUG_OBJECT (depayload, "no bufferpool");
294 gst_rtp_vraw_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
296 GstRtpVRawDepay *rtpvrawdepay;
297 guint8 *payload, *yp, *up, *vp, *headers;
299 guint cont, ystride, uvstride, pgroup, payload_len;
300 gint width, height, xinc, yinc;
301 GstRTPBuffer rtp = { NULL };
304 rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
306 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
308 timestamp = gst_rtp_buffer_get_timestamp (&rtp);
310 if (timestamp != rtpvrawdepay->timestamp || rtpvrawdepay->outbuf == NULL) {
314 GST_LOG_OBJECT (depayload, "new frame with timestamp %u", timestamp);
315 /* new timestamp, flush old buffer and create new output buffer */
316 if (rtpvrawdepay->outbuf) {
317 gst_rtp_base_depayload_push (depayload, rtpvrawdepay->outbuf);
318 rtpvrawdepay->outbuf = NULL;
321 if (gst_pad_check_reconfigure (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload))) {
325 gst_pad_get_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
326 gst_rtp_vraw_depay_negotiate_pool (rtpvrawdepay, caps,
327 &rtpvrawdepay->vinfo);
328 gst_caps_unref (caps);
331 ret = gst_buffer_pool_acquire_buffer (rtpvrawdepay->pool, &outbuf, NULL);
332 if (G_UNLIKELY (ret != GST_FLOW_OK))
335 /* clear timestamp from alloc... */
336 GST_BUFFER_TIMESTAMP (outbuf) = -1;
338 rtpvrawdepay->outbuf = outbuf;
339 rtpvrawdepay->timestamp = timestamp;
342 if (!gst_video_frame_map (&frame, &rtpvrawdepay->vinfo, rtpvrawdepay->outbuf,
346 /* get pointer and strides of the planes */
347 yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
348 up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
349 vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
351 ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
352 uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
354 pgroup = rtpvrawdepay->pgroup;
355 width = GST_VIDEO_INFO_WIDTH (&rtpvrawdepay->vinfo);
356 height = GST_VIDEO_INFO_HEIGHT (&rtpvrawdepay->vinfo);
357 xinc = rtpvrawdepay->xinc;
358 yinc = rtpvrawdepay->yinc;
360 payload = gst_rtp_buffer_get_payload (&rtp);
361 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
366 /* skip extended seqnum */
370 /* remember header position */
373 /* find data start */
378 cont = payload[4] & 0x80;
385 guint length, line, offs, plen;
388 /* stop when we run out of data */
389 if (payload_len == 0)
392 /* read length and cont. This should work because we iterated the headers
394 length = (headers[0] << 8) | headers[1];
395 line = ((headers[2] & 0x7f) << 8) | headers[3];
396 offs = ((headers[4] & 0x7f) << 8) | headers[5];
397 cont = headers[4] & 0x80;
400 /* length must be a multiple of pgroup */
401 if (length % pgroup != 0)
404 if (length > payload_len)
405 length = payload_len;
408 if (line > (height - yinc)) {
409 GST_WARNING_OBJECT (depayload, "skipping line %d: out of range", line);
412 if (offs > (width - xinc)) {
413 GST_WARNING_OBJECT (depayload, "skipping offset %d: out of range", offs);
417 /* calculate the maximim amount of bytes we can use per line */
418 if (offs + ((length / pgroup) * xinc) > width) {
419 plen = ((width - offs) * pgroup) / xinc;
420 GST_WARNING_OBJECT (depayload, "clipping length %d, offset %d, plen %d",
425 GST_LOG_OBJECT (depayload,
426 "writing length %u/%u, line %u, offset %u, remaining %u", plen, length,
427 line, offs, payload_len);
429 switch (GST_VIDEO_INFO_FORMAT (&rtpvrawdepay->vinfo)) {
430 case GST_VIDEO_FORMAT_RGB:
431 case GST_VIDEO_FORMAT_RGBA:
432 case GST_VIDEO_FORMAT_BGR:
433 case GST_VIDEO_FORMAT_BGRA:
434 case GST_VIDEO_FORMAT_UYVY:
435 /* samples are packed just like gstreamer packs them */
437 datap = yp + (line * ystride) + (offs * pgroup);
439 memcpy (datap, payload, plen);
441 case GST_VIDEO_FORMAT_AYUV:
446 datap = yp + (line * ystride) + (offs * 4);
449 /* samples are packed in order Cb-Y-Cr for both interlaced and
450 * progressive frames */
451 for (i = 0; i < plen; i += pgroup) {
460 case GST_VIDEO_FORMAT_I420:
464 guint8 *yd1p, *yd2p, *udp, *vdp, *p;
466 yd1p = yp + (line * ystride) + (offs);
467 yd2p = yd1p + ystride;
468 uvoff = (line / yinc * uvstride) + (offs / xinc);
474 /* line 0/1: Y00-Y01-Y10-Y11-Cb00-Cr00 Y02-Y03-Y12-Y13-Cb01-Cr01 ... */
475 for (i = 0; i < plen; i += pgroup) {
486 case GST_VIDEO_FORMAT_Y41B:
490 guint8 *ydp, *udp, *vdp, *p;
492 ydp = yp + (line * ystride) + (offs);
493 uvoff = (line / yinc * uvstride) + (offs / xinc);
499 /* Samples are packed in order Cb0-Y0-Y1-Cr0-Y2-Y3 for both interlaced
500 * and progressive scan lines */
501 for (i = 0; i < plen; i += pgroup) {
513 goto unknown_sampling;
521 payload_len -= length;
524 gst_video_frame_unmap (&frame);
525 gst_rtp_buffer_unmap (&rtp);
527 if (gst_rtp_buffer_get_marker (&rtp)) {
528 GST_LOG_OBJECT (depayload, "marker, flushing frame");
529 if (rtpvrawdepay->outbuf) {
530 gst_rtp_base_depayload_push (depayload, rtpvrawdepay->outbuf);
531 rtpvrawdepay->outbuf = NULL;
533 rtpvrawdepay->timestamp = -1;
540 GST_ELEMENT_ERROR (depayload, STREAM, FORMAT,
541 (NULL), ("unimplemented sampling"));
542 gst_video_frame_unmap (&frame);
543 gst_rtp_buffer_unmap (&rtp);
548 GST_WARNING_OBJECT (depayload, "failed to alloc output buffer");
549 gst_rtp_buffer_unmap (&rtp);
554 GST_ERROR_OBJECT (depayload, "could not map video frame");
555 gst_rtp_buffer_unmap (&rtp);
560 GST_WARNING_OBJECT (depayload, "length not multiple of pgroup");
561 gst_video_frame_unmap (&frame);
562 gst_rtp_buffer_unmap (&rtp);
567 GST_WARNING_OBJECT (depayload, "short packet");
568 gst_video_frame_unmap (&frame);
569 gst_rtp_buffer_unmap (&rtp);
575 gst_rtp_vraw_depay_handle_event (GstRTPBaseDepayload * filter, GstEvent * event)
578 GstRtpVRawDepay *rtpvrawdepay;
580 rtpvrawdepay = GST_RTP_VRAW_DEPAY (filter);
582 switch (GST_EVENT_TYPE (event)) {
583 case GST_EVENT_FLUSH_STOP:
584 gst_rtp_vraw_depay_reset (rtpvrawdepay);
591 GST_RTP_BASE_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
596 static GstStateChangeReturn
597 gst_rtp_vraw_depay_change_state (GstElement * element,
598 GstStateChange transition)
600 GstRtpVRawDepay *rtpvrawdepay;
601 GstStateChangeReturn ret;
603 rtpvrawdepay = GST_RTP_VRAW_DEPAY (element);
605 switch (transition) {
606 case GST_STATE_CHANGE_NULL_TO_READY:
608 case GST_STATE_CHANGE_READY_TO_PAUSED:
609 gst_rtp_vraw_depay_reset (rtpvrawdepay);
615 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
617 switch (transition) {
618 case GST_STATE_CHANGE_PAUSED_TO_READY:
619 gst_rtp_vraw_depay_reset (rtpvrawdepay);
621 case GST_STATE_CHANGE_READY_TO_NULL:
630 gst_rtp_vraw_depay_plugin_init (GstPlugin * plugin)
632 return gst_element_register (plugin, "rtpvrawdepay",
633 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_DEPAY);