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>
28 #include "gstrtpvrawpay.h"
32 PROP_CHUNKS_PER_FRAME = 1
35 #define DEFAULT_CHUNKS_PER_FRAME 10
37 GST_DEBUG_CATEGORY_STATIC (rtpvrawpay_debug);
38 #define GST_CAT_DEFAULT (rtpvrawpay_debug)
40 static GstStaticPadTemplate gst_rtp_vraw_pay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("video/x-raw, "
45 "format = (string) { RGB, RGBA, BGR, BGRA, AYUV, UYVY, I420, Y41B, UYVP }, "
46 "width = (int) [ 1, 32767 ], " "height = (int) [ 1, 32767 ]; ")
49 static GstStaticPadTemplate gst_rtp_vraw_pay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("application/x-rtp, "
54 "media = (string) \"video\", "
55 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
56 "clock-rate = (int) 90000, "
57 "encoding-name = (string) \"RAW\","
58 "sampling = (string) { \"RGB\", \"RGBA\", \"BGR\", \"BGRA\", "
59 "\"YCbCr-4:4:4\", \"YCbCr-4:2:2\", \"YCbCr-4:2:0\", "
61 /* we cannot express these as strings
62 * "width = (string) [1 32767],"
63 * "height = (string) [1 32767],"
65 "depth = (string) { \"8\", \"10\", \"12\", \"16\" },"
66 "colorimetry = (string) { \"BT601-5\", \"BT709-2\", \"SMPTE240M\" }"
70 * chroma-position = (string)
76 static gboolean gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload,
78 static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload *
79 payload, GstBuffer * buffer);
80 static void gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
81 GValue * value, GParamSpec * pspec);
82 static void gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
85 G_DEFINE_TYPE (GstRtpVRawPay, gst_rtp_vraw_pay, GST_TYPE_RTP_BASE_PAYLOAD)
87 static void gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
89 GstRTPBasePayloadClass *gstrtpbasepayload_class;
90 GstElementClass *gstelement_class;
91 GObjectClass *gobject_class;
93 gobject_class = (GObjectClass *) klass;
94 gstelement_class = (GstElementClass *) klass;
95 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
97 gobject_class->set_property = gst_rtp_vraw_pay_set_property;
98 gobject_class->get_property = gst_rtp_vraw_pay_get_property;
100 g_object_class_install_property (gobject_class,
101 PROP_CHUNKS_PER_FRAME,
102 g_param_spec_int ("chunks-per-frame", "Chunks per Frame",
103 "Split and send out each frame in multiple chunks to reduce overhead",
104 1, G_MAXINT, DEFAULT_CHUNKS_PER_FRAME,
105 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
108 gstrtpbasepayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
109 gstrtpbasepayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
111 gst_element_class_add_pad_template (gstelement_class,
112 gst_static_pad_template_get (&gst_rtp_vraw_pay_src_template));
113 gst_element_class_add_pad_template (gstelement_class,
114 gst_static_pad_template_get (&gst_rtp_vraw_pay_sink_template));
116 gst_element_class_set_static_metadata (gstelement_class,
117 "RTP Raw Video payloader", "Codec/Payloader/Network/RTP",
118 "Payload raw video as RTP packets (RFC 4175)",
119 "Wim Taymans <wim.taymans@gmail.com>");
121 GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
122 "Raw video RTP Payloader");
126 gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay)
128 rtpvrawpay->chunks_per_frame = DEFAULT_CHUNKS_PER_FRAME;
132 gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
134 GstRtpVRawPay *rtpvrawpay;
136 gint pgroup, xinc, yinc;
137 const gchar *depthstr, *samplingstr, *colorimetrystr;
141 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
143 if (!gst_video_info_from_caps (&info, caps))
146 rtpvrawpay->vinfo = info;
148 if (gst_video_colorimetry_matches (&info.colorimetry,
149 GST_VIDEO_COLORIMETRY_BT601)) {
150 colorimetrystr = "BT601-5";
151 } else if (gst_video_colorimetry_matches (&info.colorimetry,
152 GST_VIDEO_COLORIMETRY_BT709)) {
153 colorimetrystr = "BT709-2";
154 } else if (gst_video_colorimetry_matches (&info.colorimetry,
155 GST_VIDEO_COLORIMETRY_SMPTE240M)) {
156 colorimetrystr = "SMPTE240M";
158 colorimetrystr = "SMPTE240M";
163 /* these values are the only thing we can do */
166 switch (GST_VIDEO_INFO_FORMAT (&info)) {
167 case GST_VIDEO_FORMAT_RGBA:
168 samplingstr = "RGBA";
171 case GST_VIDEO_FORMAT_BGRA:
172 samplingstr = "BGRA";
175 case GST_VIDEO_FORMAT_RGB:
179 case GST_VIDEO_FORMAT_BGR:
183 case GST_VIDEO_FORMAT_AYUV:
184 samplingstr = "YCbCr-4:4:4";
187 case GST_VIDEO_FORMAT_UYVY:
188 samplingstr = "YCbCr-4:2:2";
192 case GST_VIDEO_FORMAT_Y41B:
193 samplingstr = "YCbCr-4:1:1";
197 case GST_VIDEO_FORMAT_I420:
198 samplingstr = "YCbCr-4:2:0";
202 case GST_VIDEO_FORMAT_UYVP:
203 samplingstr = "YCbCr-4:2:2";
213 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
217 rtpvrawpay->pgroup = pgroup;
218 rtpvrawpay->xinc = xinc;
219 rtpvrawpay->yinc = yinc;
221 GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %s",
222 GST_VIDEO_INFO_WIDTH (&info), GST_VIDEO_INFO_HEIGHT (&info), samplingstr);
223 GST_DEBUG_OBJECT (payload, "xinc %d, yinc %d, pgroup %d", xinc, yinc, pgroup);
225 wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
226 hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
228 gst_rtp_base_payload_set_options (payload, "video", TRUE, "RAW", 90000);
229 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
230 res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
231 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
232 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
233 colorimetrystr, "interlace", G_TYPE_STRING, "true", NULL);
235 res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
236 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
237 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
238 colorimetrystr, NULL);
248 GST_ERROR_OBJECT (payload, "could not parse caps");
253 GST_ERROR_OBJECT (payload, "unknown caps format");
259 gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
261 GstRtpVRawPay *rtpvrawpay;
262 GstFlowReturn ret = GST_FLOW_OK;
263 gfloat packets_per_packline;
264 guint pgroups_per_packet;
265 guint packlines_per_list, buffers_per_list;
266 guint lines_delay; /* after how many packed lines we push out a buffer list */
267 guint last_line; /* last pack line number we pushed out a buffer list */
269 guint8 *p0, *yp, *up, *vp;
270 guint ystride, uvstride;
276 GstVideoFormat format;
279 gboolean use_buffer_lists;
280 GstBufferList *list = NULL;
281 GstRTPBuffer rtp = { NULL, };
283 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
285 gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ);
287 GST_LOG_OBJECT (rtpvrawpay, "new frame of %" G_GSIZE_FORMAT " bytes",
288 gst_buffer_get_size (buffer));
290 /* get pointer and strides of the planes */
291 p0 = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
292 yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
293 up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
294 vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
296 ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
297 uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
299 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
301 /* amount of bytes for one pixel */
302 pgroup = rtpvrawpay->pgroup;
303 width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
304 height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
306 interlaced = GST_VIDEO_INFO_IS_INTERLACED (&rtpvrawpay->vinfo);
308 format = GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo);
310 yinc = rtpvrawpay->yinc;
311 xinc = rtpvrawpay->xinc;
313 /* after how many packed lines we push out a buffer list */
314 lines_delay = GST_ROUND_UP_4 (height / rtpvrawpay->chunks_per_frame);
316 /* calculate how many buffers we expect to store in a single buffer list */
317 pgroups_per_packet = (mtu - (12 + 14)) / pgroup;
318 packets_per_packline = width / (xinc * pgroups_per_packet * 1.0);
319 packlines_per_list = height / (yinc * rtpvrawpay->chunks_per_frame);
320 buffers_per_list = packlines_per_list * packets_per_packline;
321 buffers_per_list = GST_ROUND_UP_8 (buffers_per_list);
323 use_buffer_lists = (rtpvrawpay->chunks_per_frame < (height / yinc));
325 fields = 1 + interlaced;
327 /* start with line 0, offset 0 */
328 for (field = 0; field < fields; field++) {
333 if (use_buffer_lists)
334 list = gst_buffer_list_new_sized (buffers_per_list);
336 /* write all lines */
337 while (line < height) {
338 guint left, pack_line;
340 guint8 *outdata, *headers;
341 gboolean next_line, complete = FALSE;
342 guint length, cont, pixels;
344 /* get the max allowed payload length size, we try to fill the complete MTU */
345 left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
346 out = gst_rtp_buffer_new_allocate (left, 0, 0);
349 GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer);
351 GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer) +
352 GST_BUFFER_DURATION (buffer) / 2;
355 gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
356 outdata = gst_rtp_buffer_get_payload (&rtp);
358 GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
363 * 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
364 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
365 * | Extended Sequence Number | Length |
366 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
367 * |F| Line No |C| Offset |
368 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
369 * | Length |F| Line No |
370 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
372 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ .
374 * . Two (partial) lines of video data .
376 * +---------------------------------------------------------------+
379 /* need 2 bytes for the extended sequence number */
384 /* the headers start here */
387 /* make sure we can fit at least *one* header and pixel */
388 if (!(left > (6 + pgroup))) {
389 gst_rtp_buffer_unmap (&rtp);
390 gst_buffer_unref (out);
394 /* while we can fit at least one header and one pixel */
395 while (left > (6 + pgroup)) {
396 /* we need a 6 bytes header */
399 /* get how may bytes we need for the remaining pixels */
400 pixels = width - offset;
401 length = (pixels * pgroup) / xinc;
403 if (left >= length) {
404 /* pixels and header fit completely, we will write them and skip to the
408 /* line does not fit completely, see how many pixels fit */
409 pixels = (left / pgroup) * xinc;
410 length = (pixels * pgroup) / xinc;
413 GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
418 *outdata++ = (length >> 8) & 0xff;
419 *outdata++ = length & 0xff;
422 *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
423 *outdata++ = line & 0xff;
426 /* go to next line we do this here to make the check below easier */
430 /* calculate continuation marker */
431 cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
433 /* write offset and continuation marker */
434 *outdata++ = ((offset >> 8) & 0x7f) | cont;
435 *outdata++ = offset & 0xff;
440 GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
443 GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
449 GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
450 (guint) (outdata - headers));
452 /* second pass, read headers and write the data */
456 /* read length and cont */
457 length = (headers[0] << 8) | headers[1];
458 lin = ((headers[2] & 0x7f) << 8) | headers[3];
459 offs = ((headers[4] & 0x7f) << 8) | headers[5];
460 cont = headers[4] & 0x80;
461 pixels = length / pgroup;
464 GST_LOG_OBJECT (payload,
465 "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
469 case GST_VIDEO_FORMAT_RGB:
470 case GST_VIDEO_FORMAT_RGBA:
471 case GST_VIDEO_FORMAT_BGR:
472 case GST_VIDEO_FORMAT_BGRA:
473 case GST_VIDEO_FORMAT_UYVY:
474 case GST_VIDEO_FORMAT_UYVP:
476 memcpy (outdata, p0 + (lin * ystride) + (offs * pgroup), length);
479 case GST_VIDEO_FORMAT_AYUV:
484 datap = p0 + (lin * ystride) + (offs * 4);
486 for (i = 0; i < pixels; i++) {
487 *outdata++ = datap[2];
488 *outdata++ = datap[1];
489 *outdata++ = datap[3];
494 case GST_VIDEO_FORMAT_I420:
498 guint8 *yd1p, *yd2p, *udp, *vdp;
500 yd1p = yp + (lin * ystride) + (offs);
501 yd2p = yd1p + ystride;
502 uvoff = (lin / yinc * uvstride) + (offs / xinc);
506 for (i = 0; i < pixels; i++) {
507 *outdata++ = *yd1p++;
508 *outdata++ = *yd1p++;
509 *outdata++ = *yd2p++;
510 *outdata++ = *yd2p++;
516 case GST_VIDEO_FORMAT_Y41B:
520 guint8 *ydp, *udp, *vdp;
522 ydp = yp + (lin * ystride) + offs;
523 uvoff = (lin / yinc * uvstride) + (offs / xinc);
527 for (i = 0; i < pixels; i++) {
538 gst_rtp_buffer_unmap (&rtp);
539 gst_buffer_unref (out);
540 goto unknown_sampling;
547 if (line >= height) {
548 GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
549 gst_rtp_buffer_set_marker (&rtp, TRUE);
552 gst_rtp_buffer_unmap (&rtp);
554 GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
555 gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
558 /* Now either push out the buffer directly */
559 if (!use_buffer_lists) {
560 ret = gst_rtp_base_payload_push (payload, out);
564 /* or add the buffer to buffer list ... */
565 gst_buffer_list_add (list, out);
567 /* .. and check if we need to push out the list */
568 pack_line = (line - field) / fields;
569 if (complete || (pack_line > last_line && pack_line % lines_delay == 0)) {
570 GST_LOG_OBJECT (rtpvrawpay, "pushing list of %u buffers up to pack "
571 "line %u", gst_buffer_list_length (list), pack_line);
572 ret = gst_rtp_base_payload_push_list (payload, list);
575 list = gst_buffer_list_new_sized (buffers_per_list);
576 last_line = pack_line;
582 gst_video_frame_unmap (&frame);
583 gst_buffer_unref (buffer);
590 GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
591 (NULL), ("unimplemented sampling"));
592 gst_video_frame_unmap (&frame);
593 gst_buffer_unref (buffer);
594 return GST_FLOW_NOT_SUPPORTED;
598 GST_ELEMENT_ERROR (payload, RESOURCE, NO_SPACE_LEFT,
599 (NULL), ("not enough space to send at least one pixel"));
600 gst_video_frame_unmap (&frame);
601 gst_buffer_unref (buffer);
602 return GST_FLOW_NOT_SUPPORTED;
607 gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
608 const GValue * value, GParamSpec * pspec)
610 GstRtpVRawPay *rtpvrawpay;
612 rtpvrawpay = GST_RTP_VRAW_PAY (object);
615 case PROP_CHUNKS_PER_FRAME:
616 rtpvrawpay->chunks_per_frame = g_value_get_int (value);
619 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
625 gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
626 GValue * value, GParamSpec * pspec)
628 GstRtpVRawPay *rtpvrawpay;
630 rtpvrawpay = GST_RTP_VRAW_PAY (object);
633 case PROP_CHUNKS_PER_FRAME:
634 g_value_set_int (value, rtpvrawpay->chunks_per_frame);
637 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
643 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
645 return gst_element_register (plugin, "rtpvrawpay",
646 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);