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 "gstrtpelements.h"
30 #include "gstrtpvrawpay.h"
31 #include "gstrtputils.h"
35 PROP_CHUNKS_PER_FRAME = 1
38 #define DEFAULT_CHUNKS_PER_FRAME 10
40 GST_DEBUG_CATEGORY_STATIC (rtpvrawpay_debug);
41 #define GST_CAT_DEFAULT (rtpvrawpay_debug)
43 static GstStaticPadTemplate gst_rtp_vraw_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_STATIC_CAPS ("video/x-raw, "
48 "format = (string) { RGB, RGBA, BGR, BGRA, AYUV, UYVY, I420, Y41B, UYVP }, "
49 "width = (int) [ 1, 32767 ], " "height = (int) [ 1, 32767 ]; ")
52 static GstStaticPadTemplate gst_rtp_vraw_pay_src_template =
53 GST_STATIC_PAD_TEMPLATE ("src",
56 GST_STATIC_CAPS ("application/x-rtp, "
57 "media = (string) \"video\", "
58 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
59 "clock-rate = (int) 90000, "
60 "encoding-name = (string) \"RAW\","
61 "sampling = (string) { \"RGB\", \"RGBA\", \"BGR\", \"BGRA\", "
62 "\"YCbCr-4:4:4\", \"YCbCr-4:2:2\", \"YCbCr-4:2:0\", "
64 /* we cannot express these as strings
65 * "width = (string) [1 32767],"
66 * "height = (string) [1 32767],"
68 "depth = (string) { \"8\", \"10\", \"12\", \"16\" },"
69 "colorimetry = (string) { \"BT601-5\", \"BT709-2\", \"SMPTE240M\" }"
73 * chroma-position = (string)
79 static gboolean gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload,
81 static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload *
82 payload, GstBuffer * buffer);
83 static void gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
84 GValue * value, GParamSpec * pspec);
85 static void gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
86 const GValue * value, GParamSpec * pspec);
88 G_DEFINE_TYPE (GstRtpVRawPay, gst_rtp_vraw_pay, GST_TYPE_RTP_BASE_PAYLOAD);
89 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpvrawpay, "rtpvrawpay",
90 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY, rtp_element_init (plugin));
93 gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
95 GstRTPBasePayloadClass *gstrtpbasepayload_class;
96 GstElementClass *gstelement_class;
97 GObjectClass *gobject_class;
99 gobject_class = (GObjectClass *) klass;
100 gstelement_class = (GstElementClass *) klass;
101 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
103 gobject_class->set_property = gst_rtp_vraw_pay_set_property;
104 gobject_class->get_property = gst_rtp_vraw_pay_get_property;
106 g_object_class_install_property (gobject_class,
107 PROP_CHUNKS_PER_FRAME,
108 g_param_spec_int ("chunks-per-frame", "Chunks per Frame",
109 "Split and send out each frame in multiple chunks to reduce overhead",
110 1, G_MAXINT, DEFAULT_CHUNKS_PER_FRAME,
111 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
114 gstrtpbasepayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
115 gstrtpbasepayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
117 gst_element_class_add_static_pad_template (gstelement_class,
118 &gst_rtp_vraw_pay_src_template);
119 gst_element_class_add_static_pad_template (gstelement_class,
120 &gst_rtp_vraw_pay_sink_template);
122 gst_element_class_set_static_metadata (gstelement_class,
123 "RTP Raw Video payloader", "Codec/Payloader/Network/RTP",
124 "Payload raw video as RTP packets (RFC 4175)",
125 "Wim Taymans <wim.taymans@gmail.com>");
127 GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
128 "Raw video RTP Payloader");
132 gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay)
134 rtpvrawpay->chunks_per_frame = DEFAULT_CHUNKS_PER_FRAME;
138 gst_rtp_vraw_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
140 GstRtpVRawPay *rtpvrawpay;
142 gint pgroup, xinc, yinc;
143 const gchar *depthstr, *samplingstr, *colorimetrystr;
147 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
149 if (!gst_video_info_from_caps (&info, caps))
152 rtpvrawpay->vinfo = info;
154 if (gst_video_colorimetry_matches (&info.colorimetry,
155 GST_VIDEO_COLORIMETRY_BT601)) {
156 colorimetrystr = "BT601-5";
157 } else if (gst_video_colorimetry_matches (&info.colorimetry,
158 GST_VIDEO_COLORIMETRY_BT709)) {
159 colorimetrystr = "BT709-2";
160 } else if (gst_video_colorimetry_matches (&info.colorimetry,
161 GST_VIDEO_COLORIMETRY_SMPTE240M)) {
162 colorimetrystr = "SMPTE240M";
164 colorimetrystr = "SMPTE240M";
169 /* these values are the only thing we can do */
172 switch (GST_VIDEO_INFO_FORMAT (&info)) {
173 case GST_VIDEO_FORMAT_RGBA:
174 samplingstr = "RGBA";
177 case GST_VIDEO_FORMAT_BGRA:
178 samplingstr = "BGRA";
181 case GST_VIDEO_FORMAT_RGB:
185 case GST_VIDEO_FORMAT_BGR:
189 case GST_VIDEO_FORMAT_AYUV:
190 samplingstr = "YCbCr-4:4:4";
193 case GST_VIDEO_FORMAT_UYVY:
194 samplingstr = "YCbCr-4:2:2";
198 case GST_VIDEO_FORMAT_Y41B:
199 samplingstr = "YCbCr-4:1:1";
203 case GST_VIDEO_FORMAT_I420:
204 samplingstr = "YCbCr-4:2:0";
208 case GST_VIDEO_FORMAT_UYVP:
209 samplingstr = "YCbCr-4:2:2";
219 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
223 rtpvrawpay->pgroup = pgroup;
224 rtpvrawpay->xinc = xinc;
225 rtpvrawpay->yinc = yinc;
227 GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %s",
228 GST_VIDEO_INFO_WIDTH (&info), GST_VIDEO_INFO_HEIGHT (&info), samplingstr);
229 GST_DEBUG_OBJECT (payload, "xinc %d, yinc %d, pgroup %d", xinc, yinc, pgroup);
231 wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
232 hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
234 gst_rtp_base_payload_set_options (payload, "video", TRUE, "RAW", 90000);
235 if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
236 res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
237 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
238 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
239 colorimetrystr, "interlace", G_TYPE_STRING, "true", NULL);
241 res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
242 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
243 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
244 colorimetrystr, NULL);
254 GST_ERROR_OBJECT (payload, "could not parse caps");
259 GST_ERROR_OBJECT (payload, "unknown caps format");
265 gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
267 GstRtpVRawPay *rtpvrawpay;
268 GstFlowReturn ret = GST_FLOW_OK;
269 gfloat packets_per_packline;
270 guint pgroups_per_packet;
271 guint packlines_per_list, buffers_per_list;
272 guint lines_delay; /* after how many packed lines we push out a buffer list */
273 guint last_line; /* last pack line number we pushed out a buffer list */
275 guint8 *p0, *yp, *up, *vp;
276 guint ystride, uvstride;
282 GstVideoFormat format;
285 gboolean use_buffer_lists;
286 GstBufferList *list = NULL;
287 GstRTPBuffer rtp = { NULL, };
290 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
292 if (!gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ)) {
293 gst_buffer_unref (buffer);
294 return GST_FLOW_ERROR;
297 discont = GST_BUFFER_IS_DISCONT (buffer);
299 GST_LOG_OBJECT (rtpvrawpay, "new frame of %" G_GSIZE_FORMAT " bytes",
300 gst_buffer_get_size (buffer));
302 /* get pointer and strides of the planes */
303 p0 = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
304 yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
305 up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
306 vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
308 ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
309 uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
311 mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
313 /* amount of bytes for one pixel */
314 pgroup = rtpvrawpay->pgroup;
315 width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
316 height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
318 interlaced = GST_VIDEO_INFO_IS_INTERLACED (&rtpvrawpay->vinfo);
320 format = GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo);
322 yinc = rtpvrawpay->yinc;
323 xinc = rtpvrawpay->xinc;
325 /* after how many packed lines we push out a buffer list */
326 lines_delay = GST_ROUND_UP_4 (height / rtpvrawpay->chunks_per_frame);
328 /* calculate how many buffers we expect to store in a single buffer list */
329 pgroups_per_packet = (mtu - (12 + 14)) / pgroup;
330 packets_per_packline = width / (xinc * pgroups_per_packet * 1.0);
331 packlines_per_list = height / (yinc * rtpvrawpay->chunks_per_frame);
332 buffers_per_list = packlines_per_list * packets_per_packline;
333 buffers_per_list = GST_ROUND_UP_8 (buffers_per_list);
335 use_buffer_lists = buffers_per_list > 1 &&
336 (rtpvrawpay->chunks_per_frame < (height / yinc));
338 fields = 1 + interlaced;
340 /* start with line 0, offset 0 */
341 for (field = 0; field < fields; field++) {
346 if (use_buffer_lists)
347 list = gst_buffer_list_new_sized (buffers_per_list);
349 /* write all lines */
350 while (line < height) {
351 guint left, pack_line;
353 guint8 *outdata, *headers;
354 gboolean next_line, complete = FALSE;
355 guint length, cont, pixels;
357 /* get the max allowed payload length size, we try to fill the complete MTU */
358 left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
359 out = gst_rtp_base_payload_allocate_output_buffer (payload, left, 0, 0);
362 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT);
363 /* Only the first outputted buffer has the DISCONT flag */
368 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer);
370 GST_BUFFER_PTS (out) = GST_BUFFER_PTS (buffer) +
371 GST_BUFFER_DURATION (buffer) / 2;
374 gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
375 outdata = gst_rtp_buffer_get_payload (&rtp);
377 GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
382 * 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
383 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
384 * | Extended Sequence Number | Length |
385 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
386 * |F| Line No |C| Offset |
387 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
388 * | Length |F| Line No |
389 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
391 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ .
393 * . Two (partial) lines of video data .
395 * +---------------------------------------------------------------+
398 /* need 2 bytes for the extended sequence number */
403 /* the headers start here */
406 /* make sure we can fit at least *one* header and pixel */
407 if (!(left > (6 + pgroup))) {
408 gst_rtp_buffer_unmap (&rtp);
409 gst_buffer_unref (out);
413 /* while we can fit at least one header and one pixel */
414 while (left > (6 + pgroup)) {
415 /* we need a 6 bytes header */
418 /* get how may bytes we need for the remaining pixels */
419 pixels = width - offset;
420 length = (pixels * pgroup) / xinc;
422 if (left >= length) {
423 /* pixels and header fit completely, we will write them and skip to the
427 /* line does not fit completely, see how many pixels fit */
428 pixels = (left / pgroup) * xinc;
429 length = (pixels * pgroup) / xinc;
432 GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
437 *outdata++ = (length >> 8) & 0xff;
438 *outdata++ = length & 0xff;
441 *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
442 *outdata++ = line & 0xff;
445 /* go to next line we do this here to make the check below easier */
449 /* calculate continuation marker */
450 cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
452 /* write offset and continuation marker */
453 *outdata++ = ((offset >> 8) & 0x7f) | cont;
454 *outdata++ = offset & 0xff;
459 GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
462 GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
468 GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
469 (guint) (outdata - headers));
471 /* second pass, read headers and write the data */
475 /* read length and cont */
476 length = (headers[0] << 8) | headers[1];
477 lin = ((headers[2] & 0x7f) << 8) | headers[3];
478 offs = ((headers[4] & 0x7f) << 8) | headers[5];
479 cont = headers[4] & 0x80;
480 pixels = length / pgroup;
483 GST_LOG_OBJECT (payload,
484 "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
488 case GST_VIDEO_FORMAT_RGB:
489 case GST_VIDEO_FORMAT_RGBA:
490 case GST_VIDEO_FORMAT_BGR:
491 case GST_VIDEO_FORMAT_BGRA:
492 case GST_VIDEO_FORMAT_UYVY:
493 case GST_VIDEO_FORMAT_UYVP:
495 memcpy (outdata, p0 + (lin * ystride) + (offs * pgroup), length);
498 case GST_VIDEO_FORMAT_AYUV:
503 datap = p0 + (lin * ystride) + (offs * 4);
505 for (i = 0; i < pixels; i++) {
506 *outdata++ = datap[2];
507 *outdata++ = datap[1];
508 *outdata++ = datap[3];
513 case GST_VIDEO_FORMAT_I420:
517 guint8 *yd1p, *yd2p, *udp, *vdp;
519 yd1p = yp + (lin * ystride) + (offs);
520 yd2p = yd1p + ystride;
521 uvoff = (lin / yinc * uvstride) + (offs / xinc);
525 for (i = 0; i < pixels; i++) {
526 *outdata++ = *yd1p++;
527 *outdata++ = *yd1p++;
528 *outdata++ = *yd2p++;
529 *outdata++ = *yd2p++;
535 case GST_VIDEO_FORMAT_Y41B:
539 guint8 *ydp, *udp, *vdp;
541 ydp = yp + (lin * ystride) + offs;
542 uvoff = (lin / yinc * uvstride) + (offs / xinc);
546 for (i = 0; i < pixels; i++) {
557 gst_rtp_buffer_unmap (&rtp);
558 gst_buffer_unref (out);
559 goto unknown_sampling;
566 if (line >= height) {
567 GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
568 gst_rtp_buffer_set_marker (&rtp, TRUE);
569 GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_MARKER);
572 gst_rtp_buffer_unmap (&rtp);
574 GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
575 gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
578 gst_rtp_copy_video_meta (rtpvrawpay, out, buffer);
580 /* Now either push out the buffer directly */
581 if (!use_buffer_lists) {
582 ret = gst_rtp_base_payload_push (payload, out);
586 /* or add the buffer to buffer list ... */
587 gst_buffer_list_add (list, out);
589 /* .. and check if we need to push out the list */
590 pack_line = (line - field) / fields;
591 if (complete || (pack_line > last_line && pack_line % lines_delay == 0)) {
592 GST_LOG_OBJECT (rtpvrawpay, "pushing list of %u buffers up to pack "
593 "line %u", gst_buffer_list_length (list), pack_line);
594 ret = gst_rtp_base_payload_push_list (payload, list);
597 list = gst_buffer_list_new_sized (buffers_per_list);
598 last_line = pack_line;
604 gst_video_frame_unmap (&frame);
605 gst_buffer_unref (buffer);
612 GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
613 (NULL), ("unimplemented sampling"));
614 gst_video_frame_unmap (&frame);
615 gst_buffer_unref (buffer);
616 return GST_FLOW_NOT_SUPPORTED;
620 GST_ELEMENT_ERROR (payload, RESOURCE, NO_SPACE_LEFT,
621 (NULL), ("not enough space to send at least one pixel"));
622 gst_video_frame_unmap (&frame);
623 gst_buffer_unref (buffer);
624 return GST_FLOW_NOT_SUPPORTED;
629 gst_rtp_vraw_pay_set_property (GObject * object, guint prop_id,
630 const GValue * value, GParamSpec * pspec)
632 GstRtpVRawPay *rtpvrawpay;
634 rtpvrawpay = GST_RTP_VRAW_PAY (object);
637 case PROP_CHUNKS_PER_FRAME:
638 rtpvrawpay->chunks_per_frame = g_value_get_int (value);
641 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
647 gst_rtp_vraw_pay_get_property (GObject * object, guint prop_id,
648 GValue * value, GParamSpec * pspec)
650 GstRtpVRawPay *rtpvrawpay;
652 rtpvrawpay = GST_RTP_VRAW_PAY (object);
655 case PROP_CHUNKS_PER_FRAME:
656 g_value_set_int (value, rtpvrawpay->chunks_per_frame);
659 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);