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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
29 #include "gstrtpvrawpay.h"
30 #include "gstrtputils.h"
34 PROP_CHUNKS_PER_FRAME = 1
37 #define DEFAULT_CHUNKS_PER_FRAME 10
39 GST_DEBUG_CATEGORY_STATIC (rtpvrawpay_debug);
40 #define GST_CAT_DEFAULT (rtpvrawpay_debug)
42 static GstStaticPadTemplate gst_rtp_vraw_pay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("video/x-raw, "
47 "format = (string) { RGB, RGBA, BGR, BGRA, AYUV, UYVY, I420, Y41B, UYVP }, "
48 "width = (int) [ 1, 32767 ], " "height = (int) [ 1, 32767 ]; ")
51 static GstStaticPadTemplate gst_rtp_vraw_pay_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("application/x-rtp, "
56 "media = (string) \"video\", "
57 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
58 "clock-rate = (int) 90000, "
59 "encoding-name = (string) \"RAW\","
60 "sampling = (string) { \"RGB\", \"RGBA\", \"BGR\", \"BGRA\", "
61 "\"YCbCr-4:4:4\", \"YCbCr-4:2:2\", \"YCbCr-4:2:0\", "
63 /* we cannot express these as strings
64 * "width = (string) [1 32767],"
65 * "height = (string) [1 32767],"
67 "depth = (string) { \"8\", \"10\", \"12\", \"16\" },"
68 "colorimetry = (string) { \"BT601-5\", \"BT709-2\", \"SMPTE240M\" }"
72 * chroma-position = (string)
78 static gboolean gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload,
80 static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload *
81 payload, GstBuffer * buffer);
82 static void gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
84 static void gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
87 G_DEFINE_TYPE (GstRtpVRawPay, gst_rtp_vraw_pay, GST_TYPE_RTP_BASE_PAYLOAD)
89 static void gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
91 GstRTPBasePayloadClass *gstrtpbasepayload_class;
92 GstElementClass *gstelement_class;
93 GObjectClass *gobject_class;
95 gobject_class = (GObjectClass *) klass;
96 gstelement_class = (GstElementClass *) klass;
97 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
99 gobject_class->set_property = gst_rtp_vraw_pay_set_property;
100 gobject_class->get_property = gst_rtp_vraw_pay_get_property;
102 g_object_class_install_property (gobject_class,
103 PROP_CHUNKS_PER_FRAME,
104 g_param_spec_int ("chunks-per-frame", "Chunks per Frame",
105 "Split and send out each frame in multiple chunks to reduce overhead",
106 1, G_MAXINT, DEFAULT_CHUNKS_PER_FRAME,
107 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
110 gstrtpbasepayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
111 gstrtpbasepayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
113 gst_element_class_add_static_pad_template (gstelement_class,
114 &gst_rtp_vraw_pay_src_template);
115 gst_element_class_add_static_pad_template (gstelement_class,
116 &gst_rtp_vraw_pay_sink_template);
118 gst_element_class_set_static_metadata (gstelement_class,
119 "RTP Raw Video payloader", "Codec/Payloader/Network/RTP",
120 "Payload raw video as RTP packets (RFC 4175)",
121 "Wim Taymans <wim.taymans@gmail.com>");
123 GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
124 "Raw video RTP Payloader");
128 gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay)
130 rtpvrawpay->chunks_per_frame = DEFAULT_CHUNKS_PER_FRAME;
134 gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
136 GstRtpVRawPay *rtpvrawpay;
138 gint pgroup, xinc, yinc;
139 const gchar *depthstr, *samplingstr, *colorimetrystr;
143 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
145 if (!gst_video_info_from_caps (&info, caps))
148 rtpvrawpay->vinfo = info;
150 if (gst_video_colorimetry_matches (&info.colorimetry,
151 GST_VIDEO_COLORIMETRY_BT601)) {
152 colorimetrystr = "BT601-5";
153 } else if (gst_video_colorimetry_matches (&info.colorimetry,
154 GST_VIDEO_COLORIMETRY_BT709)) {
155 colorimetrystr = "BT709-2";
156 } else if (gst_video_colorimetry_matches (&info.colorimetry,
157 GST_VIDEO_COLORIMETRY_SMPTE240M)) {
158 colorimetrystr = "SMPTE240M";
160 colorimetrystr = "SMPTE240M";
165 /* these values are the only thing we can do */
168 switch (GST_VIDEO_INFO_FORMAT (&info)) {
169 case GST_VIDEO_FORMAT_RGBA:
170 samplingstr = "RGBA";
173 case GST_VIDEO_FORMAT_BGRA:
174 samplingstr = "BGRA";
177 case GST_VIDEO_FORMAT_RGB:
181 case GST_VIDEO_FORMAT_BGR:
185 case GST_VIDEO_FORMAT_AYUV:
186 samplingstr = "YCbCr-4:4:4";
189 case GST_VIDEO_FORMAT_UYVY:
190 samplingstr = "YCbCr-4:2:2";
194 case GST_VIDEO_FORMAT_Y41B:
195 samplingstr = "YCbCr-4:1:1";
199 case GST_VIDEO_FORMAT_I420:
200 samplingstr = "YCbCr-4:2:0";
204 case GST_VIDEO_FORMAT_UYVP:
205 samplingstr = "YCbCr-4:2:2";
215 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
219 rtpvrawpay->pgroup = pgroup;
220 rtpvrawpay->xinc = xinc;
221 rtpvrawpay->yinc = yinc;
223 GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %s",
224 GST_VIDEO_INFO_WIDTH (&info), GST_VIDEO_INFO_HEIGHT (&info), samplingstr);
225 GST_DEBUG_OBJECT (payload, "xinc %d, yinc %d, pgroup %d", xinc, yinc, pgroup);
227 wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
228 hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
230 gst_rtp_base_payload_set_options (payload, "video", TRUE, "RAW", 90000);
231 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
232 res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
233 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
234 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
235 colorimetrystr, "interlace", G_TYPE_STRING, "true", NULL);
237 res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
238 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
239 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
240 colorimetrystr, NULL);
250 GST_ERROR_OBJECT (payload, "could not parse caps");
255 GST_ERROR_OBJECT (payload, "unknown caps format");
261 gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
263 GstRtpVRawPay *rtpvrawpay;
264 GstFlowReturn ret = GST_FLOW_OK;
265 gfloat packets_per_packline;
266 guint pgroups_per_packet;
267 guint packlines_per_list, buffers_per_list;
268 guint lines_delay; /* after how many packed lines we push out a buffer list */
269 guint last_line; /* last pack line number we pushed out a buffer list */
271 guint8 *p0, *yp, *up, *vp;
272 guint ystride, uvstride;
278 GstVideoFormat format;
281 gboolean use_buffer_lists;
282 GstBufferList *list = NULL;
283 GstRTPBuffer rtp = { NULL, };
285 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
287 if (!gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ)) {
288 gst_buffer_unref (buffer);
289 return GST_FLOW_ERROR;
292 GST_LOG_OBJECT (rtpvrawpay, "new frame of %" G_GSIZE_FORMAT " bytes",
293 gst_buffer_get_size (buffer));
295 /* get pointer and strides of the planes */
296 p0 = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
297 yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
298 up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
299 vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
301 ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
302 uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
304 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
306 /* amount of bytes for one pixel */
307 pgroup = rtpvrawpay->pgroup;
308 width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
309 height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
311 interlaced = GST_VIDEO_INFO_IS_INTERLACED (&rtpvrawpay->vinfo);
313 format = GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo);
315 yinc = rtpvrawpay->yinc;
316 xinc = rtpvrawpay->xinc;
318 /* after how many packed lines we push out a buffer list */
319 lines_delay = GST_ROUND_UP_4 (height / rtpvrawpay->chunks_per_frame);
321 /* calculate how many buffers we expect to store in a single buffer list */
322 pgroups_per_packet = (mtu - (12 + 14)) / pgroup;
323 packets_per_packline = width / (xinc * pgroups_per_packet * 1.0);
324 packlines_per_list = height / (yinc * rtpvrawpay->chunks_per_frame);
325 buffers_per_list = packlines_per_list * packets_per_packline;
326 buffers_per_list = GST_ROUND_UP_8 (buffers_per_list);
328 use_buffer_lists = (rtpvrawpay->chunks_per_frame < (height / yinc));
330 fields = 1 + interlaced;
332 /* start with line 0, offset 0 */
333 for (field = 0; field < fields; field++) {
338 if (use_buffer_lists)
339 list = gst_buffer_list_new_sized (buffers_per_list);
341 /* write all lines */
342 while (line < height) {
343 guint left, pack_line;
345 guint8 *outdata, *headers;
346 gboolean next_line, complete = FALSE;
347 guint length, cont, pixels;
349 /* get the max allowed payload length size, we try to fill the complete MTU */
350 left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
351 out = gst_rtp_buffer_new_allocate (left, 0, 0);
354 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer);
356 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer) +
357 GST_BUFFER_DURATION (buffer) / 2;
360 gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
361 outdata = gst_rtp_buffer_get_payload (&rtp);
363 GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
368 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
369 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
370 * | Extended Sequence Number | Length |
371 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
372 * |F| Line No |C| Offset |
373 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374 * | Length |F| Line No |
375 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ .
379 * . Two (partial) lines of video data .
381 * +---------------------------------------------------------------+
384 /* need 2 bytes for the extended sequence number */
389 /* the headers start here */
392 /* make sure we can fit at least *one* header and pixel */
393 if (!(left > (6 + pgroup))) {
394 gst_rtp_buffer_unmap (&rtp);
395 gst_buffer_unref (out);
399 /* while we can fit at least one header and one pixel */
400 while (left > (6 + pgroup)) {
401 /* we need a 6 bytes header */
404 /* get how may bytes we need for the remaining pixels */
405 pixels = width - offset;
406 length = (pixels * pgroup) / xinc;
408 if (left >= length) {
409 /* pixels and header fit completely, we will write them and skip to the
413 /* line does not fit completely, see how many pixels fit */
414 pixels = (left / pgroup) * xinc;
415 length = (pixels * pgroup) / xinc;
418 GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
423 *outdata++ = (length >> 8) & 0xff;
424 *outdata++ = length & 0xff;
427 *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
428 *outdata++ = line & 0xff;
431 /* go to next line we do this here to make the check below easier */
435 /* calculate continuation marker */
436 cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
438 /* write offset and continuation marker */
439 *outdata++ = ((offset >> 8) & 0x7f) | cont;
440 *outdata++ = offset & 0xff;
445 GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
448 GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
454 GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
455 (guint) (outdata - headers));
457 /* second pass, read headers and write the data */
461 /* read length and cont */
462 length = (headers[0] << 8) | headers[1];
463 lin = ((headers[2] & 0x7f) << 8) | headers[3];
464 offs = ((headers[4] & 0x7f) << 8) | headers[5];
465 cont = headers[4] & 0x80;
466 pixels = length / pgroup;
469 GST_LOG_OBJECT (payload,
470 "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
474 case GST_VIDEO_FORMAT_RGB:
475 case GST_VIDEO_FORMAT_RGBA:
476 case GST_VIDEO_FORMAT_BGR:
477 case GST_VIDEO_FORMAT_BGRA:
478 case GST_VIDEO_FORMAT_UYVY:
479 case GST_VIDEO_FORMAT_UYVP:
481 memcpy (outdata, p0 + (lin * ystride) + (offs * pgroup), length);
484 case GST_VIDEO_FORMAT_AYUV:
489 datap = p0 + (lin * ystride) + (offs * 4);
491 for (i = 0; i < pixels; i++) {
492 *outdata++ = datap[2];
493 *outdata++ = datap[1];
494 *outdata++ = datap[3];
499 case GST_VIDEO_FORMAT_I420:
503 guint8 *yd1p, *yd2p, *udp, *vdp;
505 yd1p = yp + (lin * ystride) + (offs);
506 yd2p = yd1p + ystride;
507 uvoff = (lin / yinc * uvstride) + (offs / xinc);
511 for (i = 0; i < pixels; i++) {
512 *outdata++ = *yd1p++;
513 *outdata++ = *yd1p++;
514 *outdata++ = *yd2p++;
515 *outdata++ = *yd2p++;
521 case GST_VIDEO_FORMAT_Y41B:
525 guint8 *ydp, *udp, *vdp;
527 ydp = yp + (lin * ystride) + offs;
528 uvoff = (lin / yinc * uvstride) + (offs / xinc);
532 for (i = 0; i < pixels; i++) {
543 gst_rtp_buffer_unmap (&rtp);
544 gst_buffer_unref (out);
545 goto unknown_sampling;
552 if (line >= height) {
553 GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
554 gst_rtp_buffer_set_marker (&rtp, TRUE);
557 gst_rtp_buffer_unmap (&rtp);
559 GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
560 gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
563 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpvrawpay), out, buffer,
564 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
567 /* Now either push out the buffer directly */
568 if (!use_buffer_lists) {
569 ret = gst_rtp_base_payload_push (payload, out);
573 /* or add the buffer to buffer list ... */
574 gst_buffer_list_add (list, out);
576 /* .. and check if we need to push out the list */
577 pack_line = (line - field) / fields;
578 if (complete || (pack_line > last_line && pack_line % lines_delay == 0)) {
579 GST_LOG_OBJECT (rtpvrawpay, "pushing list of %u buffers up to pack "
580 "line %u", gst_buffer_list_length (list), pack_line);
581 ret = gst_rtp_base_payload_push_list (payload, list);
584 list = gst_buffer_list_new_sized (buffers_per_list);
585 last_line = pack_line;
591 gst_video_frame_unmap (&frame);
592 gst_buffer_unref (buffer);
599 GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
600 (NULL), ("unimplemented sampling"));
601 gst_video_frame_unmap (&frame);
602 gst_buffer_unref (buffer);
603 return GST_FLOW_NOT_SUPPORTED;
607 GST_ELEMENT_ERROR (payload, RESOURCE, NO_SPACE_LEFT,
608 (NULL), ("not enough space to send at least one pixel"));
609 gst_video_frame_unmap (&frame);
610 gst_buffer_unref (buffer);
611 return GST_FLOW_NOT_SUPPORTED;
616 gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
617 const GValue * value, GParamSpec * pspec)
619 GstRtpVRawPay *rtpvrawpay;
621 rtpvrawpay = GST_RTP_VRAW_PAY (object);
624 case PROP_CHUNKS_PER_FRAME:
625 rtpvrawpay->chunks_per_frame = g_value_get_int (value);
628 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
634 gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
635 GValue * value, GParamSpec * pspec)
637 GstRtpVRawPay *rtpvrawpay;
639 rtpvrawpay = GST_RTP_VRAW_PAY (object);
642 case PROP_CHUNKS_PER_FRAME:
643 g_value_set_int (value, rtpvrawpay->chunks_per_frame);
646 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
652 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
654 return gst_element_register (plugin, "rtpvrawpay",
655 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);