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 gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ);
289 GST_LOG_OBJECT (rtpvrawpay, "new frame of %" G_GSIZE_FORMAT " bytes",
290 gst_buffer_get_size (buffer));
292 /* get pointer and strides of the planes */
293 p0 = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
294 yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
295 up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
296 vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
298 ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
299 uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
301 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
303 /* amount of bytes for one pixel */
304 pgroup = rtpvrawpay->pgroup;
305 width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
306 height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
308 interlaced = GST_VIDEO_INFO_IS_INTERLACED (&rtpvrawpay->vinfo);
310 format = GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo);
312 yinc = rtpvrawpay->yinc;
313 xinc = rtpvrawpay->xinc;
315 /* after how many packed lines we push out a buffer list */
316 lines_delay = GST_ROUND_UP_4 (height / rtpvrawpay->chunks_per_frame);
318 /* calculate how many buffers we expect to store in a single buffer list */
319 pgroups_per_packet = (mtu - (12 + 14)) / pgroup;
320 packets_per_packline = width / (xinc * pgroups_per_packet * 1.0);
321 packlines_per_list = height / (yinc * rtpvrawpay->chunks_per_frame);
322 buffers_per_list = packlines_per_list * packets_per_packline;
323 buffers_per_list = GST_ROUND_UP_8 (buffers_per_list);
325 use_buffer_lists = (rtpvrawpay->chunks_per_frame < (height / yinc));
327 fields = 1 + interlaced;
329 /* start with line 0, offset 0 */
330 for (field = 0; field < fields; field++) {
335 if (use_buffer_lists)
336 list = gst_buffer_list_new_sized (buffers_per_list);
338 /* write all lines */
339 while (line < height) {
340 guint left, pack_line;
342 guint8 *outdata, *headers;
343 gboolean next_line, complete = FALSE;
344 guint length, cont, pixels;
346 /* get the max allowed payload length size, we try to fill the complete MTU */
347 left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
348 out = gst_rtp_buffer_new_allocate (left, 0, 0);
351 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer);
353 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer) +
354 GST_BUFFER_DURATION (buffer) / 2;
357 gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
358 outdata = gst_rtp_buffer_get_payload (&rtp);
360 GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
365 * 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
366 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
367 * | Extended Sequence Number | Length |
368 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
369 * |F| Line No |C| Offset |
370 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371 * | Length |F| Line No |
372 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
374 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ .
376 * . Two (partial) lines of video data .
378 * +---------------------------------------------------------------+
381 /* need 2 bytes for the extended sequence number */
386 /* the headers start here */
389 /* make sure we can fit at least *one* header and pixel */
390 if (!(left > (6 + pgroup))) {
391 gst_rtp_buffer_unmap (&rtp);
392 gst_buffer_unref (out);
396 /* while we can fit at least one header and one pixel */
397 while (left > (6 + pgroup)) {
398 /* we need a 6 bytes header */
401 /* get how may bytes we need for the remaining pixels */
402 pixels = width - offset;
403 length = (pixels * pgroup) / xinc;
405 if (left >= length) {
406 /* pixels and header fit completely, we will write them and skip to the
410 /* line does not fit completely, see how many pixels fit */
411 pixels = (left / pgroup) * xinc;
412 length = (pixels * pgroup) / xinc;
415 GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
420 *outdata++ = (length >> 8) & 0xff;
421 *outdata++ = length & 0xff;
424 *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
425 *outdata++ = line & 0xff;
428 /* go to next line we do this here to make the check below easier */
432 /* calculate continuation marker */
433 cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
435 /* write offset and continuation marker */
436 *outdata++ = ((offset >> 8) & 0x7f) | cont;
437 *outdata++ = offset & 0xff;
442 GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
445 GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
451 GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
452 (guint) (outdata - headers));
454 /* second pass, read headers and write the data */
458 /* read length and cont */
459 length = (headers[0] << 8) | headers[1];
460 lin = ((headers[2] & 0x7f) << 8) | headers[3];
461 offs = ((headers[4] & 0x7f) << 8) | headers[5];
462 cont = headers[4] & 0x80;
463 pixels = length / pgroup;
466 GST_LOG_OBJECT (payload,
467 "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
471 case GST_VIDEO_FORMAT_RGB:
472 case GST_VIDEO_FORMAT_RGBA:
473 case GST_VIDEO_FORMAT_BGR:
474 case GST_VIDEO_FORMAT_BGRA:
475 case GST_VIDEO_FORMAT_UYVY:
476 case GST_VIDEO_FORMAT_UYVP:
478 memcpy (outdata, p0 + (lin * ystride) + (offs * pgroup), length);
481 case GST_VIDEO_FORMAT_AYUV:
486 datap = p0 + (lin * ystride) + (offs * 4);
488 for (i = 0; i < pixels; i++) {
489 *outdata++ = datap[2];
490 *outdata++ = datap[1];
491 *outdata++ = datap[3];
496 case GST_VIDEO_FORMAT_I420:
500 guint8 *yd1p, *yd2p, *udp, *vdp;
502 yd1p = yp + (lin * ystride) + (offs);
503 yd2p = yd1p + ystride;
504 uvoff = (lin / yinc * uvstride) + (offs / xinc);
508 for (i = 0; i < pixels; i++) {
509 *outdata++ = *yd1p++;
510 *outdata++ = *yd1p++;
511 *outdata++ = *yd2p++;
512 *outdata++ = *yd2p++;
518 case GST_VIDEO_FORMAT_Y41B:
522 guint8 *ydp, *udp, *vdp;
524 ydp = yp + (lin * ystride) + offs;
525 uvoff = (lin / yinc * uvstride) + (offs / xinc);
529 for (i = 0; i < pixels; i++) {
540 gst_rtp_buffer_unmap (&rtp);
541 gst_buffer_unref (out);
542 goto unknown_sampling;
549 if (line >= height) {
550 GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
551 gst_rtp_buffer_set_marker (&rtp, TRUE);
554 gst_rtp_buffer_unmap (&rtp);
556 GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
557 gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
560 gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpvrawpay), out, buffer,
561 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
564 /* Now either push out the buffer directly */
565 if (!use_buffer_lists) {
566 ret = gst_rtp_base_payload_push (payload, out);
570 /* or add the buffer to buffer list ... */
571 gst_buffer_list_add (list, out);
573 /* .. and check if we need to push out the list */
574 pack_line = (line - field) / fields;
575 if (complete || (pack_line > last_line && pack_line % lines_delay == 0)) {
576 GST_LOG_OBJECT (rtpvrawpay, "pushing list of %u buffers up to pack "
577 "line %u", gst_buffer_list_length (list), pack_line);
578 ret = gst_rtp_base_payload_push_list (payload, list);
581 list = gst_buffer_list_new_sized (buffers_per_list);
582 last_line = pack_line;
588 gst_video_frame_unmap (&frame);
589 gst_buffer_unref (buffer);
596 GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
597 (NULL), ("unimplemented sampling"));
598 gst_video_frame_unmap (&frame);
599 gst_buffer_unref (buffer);
600 return GST_FLOW_NOT_SUPPORTED;
604 GST_ELEMENT_ERROR (payload, RESOURCE, NO_SPACE_LEFT,
605 (NULL), ("not enough space to send at least one pixel"));
606 gst_video_frame_unmap (&frame);
607 gst_buffer_unref (buffer);
608 return GST_FLOW_NOT_SUPPORTED;
613 gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
614 const GValue * value, GParamSpec * pspec)
616 GstRtpVRawPay *rtpvrawpay;
618 rtpvrawpay = GST_RTP_VRAW_PAY (object);
621 case PROP_CHUNKS_PER_FRAME:
622 rtpvrawpay->chunks_per_frame = g_value_get_int (value);
625 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
631 gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
632 GValue * value, GParamSpec * pspec)
634 GstRtpVRawPay *rtpvrawpay;
636 rtpvrawpay = GST_RTP_VRAW_PAY (object);
639 case PROP_CHUNKS_PER_FRAME:
640 g_value_set_int (value, rtpvrawpay->chunks_per_frame);
643 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
651 return gst_element_register (plugin, "rtpvrawpay",
652 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);