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.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpvrawpay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpvrawpay_debug);
31 #define GST_CAT_DEFAULT (rtpvrawpay_debug)
33 /* elementfactory information */
34 static const GstElementDetails gst_rtp_vrawpay_details =
35 GST_ELEMENT_DETAILS ("RTP Raw Video payloader",
36 "Codec/Payloader/Network",
37 "Payload raw video as RTP packets (RFC 4175)",
38 "Wim Taymans <wim.taymans@gmail.com>");
40 static GstStaticPadTemplate gst_rtp_vraw_pay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("video/x-raw-rgb, "
47 "endianness = (int) BIG_ENDIAN, "
48 "red_mask = (int) 0xFF000000, "
49 "green_mask = (int) 0x00FF0000, "
50 "blue_mask = (int) 0x0000FF00, "
51 "width = (int) [ 1, 32767 ], "
52 "height = (int) [ 1, 32767 ]; "
56 "endianness = (int) BIG_ENDIAN, "
57 "red_mask = (int) 0xFF000000, "
58 "green_mask = (int) 0x00FF0000, "
59 "blue_mask = (int) 0x0000FF00, "
60 "alpha_mask = (int) 0x000000FF, "
61 "width = (int) [ 1, 32767 ], "
62 "height = (int) [ 1, 32767 ]; "
66 "endianness = (int) BIG_ENDIAN, "
67 "red_mask = (int) 0x0000FF00, "
68 "green_mask = (int) 0x00FF0000, "
69 "blue_mask = (int) 0xFF000000, "
70 "width = (int) [ 1, 32767 ], "
71 "height = (int) [ 1, 32767 ]; "
75 "endianness = (int) BIG_ENDIAN, "
76 "red_mask = (int) 0x0000FF00, "
77 "green_mask = (int) 0x00FF0000, "
78 "blue_mask = (int) 0xFF000000, "
79 "alpha_mask = (int) 0x000000FF, "
80 "width = (int) [ 1, 32767 ], "
81 "height = (int) [ 1, 32767 ]; "
83 "format = (fourcc) { AYUV, UYVY, I420, Y41B }, "
84 "width = (int) [ 1, 32767 ], " "height = (int) [ 1, 32767 ]; ")
87 static GstStaticPadTemplate gst_rtp_vraw_pay_src_template =
88 GST_STATIC_PAD_TEMPLATE ("src",
91 GST_STATIC_CAPS ("application/x-rtp, "
92 "media = (string) \"video\", "
93 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
94 "clock-rate = (int) 90000, "
95 "encoding-name = (string) \"RAW\","
96 "sampling = (string) { \"RGB\", \"RGBA\", \"BGR\", \"BGRA\", "
97 "\"YCbCr-4:4:4\", \"YCbCr-4:2:2\", \"YCbCr-4:2:0\", "
99 /* we cannot express these as strings
100 * "width = (string) [1 32767],"
101 * "height = (string) [1 32767],"
103 "depth = (string) { \"8\", \"10\", \"12\", \"16\" },"
104 "colorimetry = (string) { \"BT601-5\", \"BT709-2\", \"SMPTE240M\" }"
108 * chroma-position = (string)
114 static void gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass);
115 static void gst_rtp_vraw_pay_base_init (GstRtpVRawPayClass * klass);
116 static void gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay);
118 static gboolean gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload,
120 static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload *
121 payload, GstBuffer * buffer);
123 static GstBaseRTPPayloadClass *parent_class = NULL;
126 gst_rtp_vraw_pay_get_type (void)
128 static GType rtpvrawpay_type = 0;
130 if (!rtpvrawpay_type) {
131 static const GTypeInfo rtpvrawpay_info = {
132 sizeof (GstRtpVRawPayClass),
133 (GBaseInitFunc) gst_rtp_vraw_pay_base_init,
135 (GClassInitFunc) gst_rtp_vraw_pay_class_init,
138 sizeof (GstRtpVRawPay),
140 (GInstanceInitFunc) gst_rtp_vraw_pay_init,
144 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpVRawPay",
145 &rtpvrawpay_info, 0);
147 return rtpvrawpay_type;
151 gst_rtp_vraw_pay_base_init (GstRtpVRawPayClass * klass)
153 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
155 gst_element_class_add_pad_template (element_class,
156 gst_static_pad_template_get (&gst_rtp_vraw_pay_src_template));
157 gst_element_class_add_pad_template (element_class,
158 gst_static_pad_template_get (&gst_rtp_vraw_pay_sink_template));
160 gst_element_class_set_details (element_class, &gst_rtp_vrawpay_details);
164 gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
166 GstBaseRTPPayloadClass *gstbasertppayload_class;
168 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
170 parent_class = g_type_class_peek_parent (klass);
172 gstbasertppayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
173 gstbasertppayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
175 GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
176 "Raw video RTP Payloader");
180 gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay)
185 gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
187 GstRtpVRawPay *rtpvrawpay;
193 gint pgroup, ystride, uvstride = 0, xinc, yinc;
194 GstVideoFormat sampling;
195 const gchar *depthstr, *samplingstr, *colorimetrystr;
199 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
201 s = gst_caps_get_structure (caps, 0);
203 /* start parsing the format */
204 name = gst_structure_get_name (s);
206 /* these values are the only thing we can do */
208 colorimetrystr = "SMPTE240M";
210 /* parse common width/height */
211 res = gst_structure_get_int (s, "width", &width);
212 res &= gst_structure_get_int (s, "height", &height);
214 goto missing_dimension;
216 /* fail on interlaced video for now */
217 if (!gst_structure_get_boolean (s, "interlaced", &interlaced))
226 if (!strcmp (name, "video/x-raw-rgb")) {
230 has_alpha = gst_structure_get_int (s, "alpha_mask", &amask);
232 if (!gst_structure_get_int (s, "red_mask", &rmask))
238 if (rmask == 0xFF000000) {
239 sampling = GST_VIDEO_FORMAT_RGBA;
240 samplingstr = "RGBA";
242 sampling = GST_VIDEO_FORMAT_BGRA;
243 samplingstr = "BGRA";
247 ystride = GST_ROUND_UP_4 (width * 3);
248 if (rmask == 0xFF000000) {
249 sampling = GST_VIDEO_FORMAT_RGB;
252 sampling = GST_VIDEO_FORMAT_BGR;
256 } else if (!strcmp (name, "video/x-raw-yuv")) {
259 if (!gst_structure_get_fourcc (s, "format", &fourcc))
262 GST_LOG_OBJECT (payload, "have fourcc %" GST_FOURCC_FORMAT,
263 GST_FOURCC_ARGS (fourcc));
266 case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
267 sampling = GST_VIDEO_FORMAT_AYUV;
268 samplingstr = "YCbCr-4:4:4";
272 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
273 sampling = GST_VIDEO_FORMAT_UYVY;
274 samplingstr = "YCbCr-4:2:2";
277 ystride = GST_ROUND_UP_2 (width) * 2;
279 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
280 sampling = GST_VIDEO_FORMAT_Y41B;
281 samplingstr = "YCbCr-4:1:1";
284 ystride = GST_ROUND_UP_4 (width);
285 uvstride = GST_ROUND_UP_8 (width) / 4;
286 up = ystride * height;
287 vp = up + uvstride * height;
289 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
290 sampling = GST_VIDEO_FORMAT_I420;
291 samplingstr = "YCbCr-4:2:0";
294 ystride = GST_ROUND_UP_4 (width);
295 uvstride = GST_ROUND_UP_8 (width) / 2;
296 up = ystride * GST_ROUND_UP_2 (height);
297 vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
305 rtpvrawpay->width = width;
306 rtpvrawpay->height = height;
307 rtpvrawpay->sampling = sampling;
308 rtpvrawpay->pgroup = pgroup;
309 rtpvrawpay->xinc = xinc;
310 rtpvrawpay->yinc = yinc;
314 rtpvrawpay->ystride = ystride;
315 rtpvrawpay->uvstride = uvstride;
317 GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %d", width, height,
319 GST_DEBUG_OBJECT (payload, "yp %d, up %d, vp %d", yp, up, vp);
320 GST_DEBUG_OBJECT (payload, "pgroup %d, ystride %d, uvstride %d", pgroup,
323 wstr = g_strdup_printf ("%d", rtpvrawpay->width);
324 hstr = g_strdup_printf ("%d", rtpvrawpay->height);
326 gst_basertppayload_set_options (payload, "video", TRUE, "RAW", 90000);
327 res = gst_basertppayload_set_outcaps (payload, "sampling", G_TYPE_STRING,
328 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
329 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
330 colorimetrystr, NULL);
339 GST_ERROR_OBJECT (payload, "unknown red mask specified");
344 GST_ERROR_OBJECT (payload, "unknown caps format");
349 GST_ERROR_OBJECT (payload, "invalid or missing fourcc");
354 GST_ERROR_OBJECT (payload, "interlaced video not supported yet");
359 GST_ERROR_OBJECT (payload, "missing width or height property");
365 gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
367 GstRtpVRawPay *rtpvrawpay;
368 GstFlowReturn ret = GST_FLOW_OK;
370 guint8 *data, *yp, *up, *vp;
371 guint ystride, uvstride;
376 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
378 data = GST_BUFFER_DATA (buffer);
379 size = GST_BUFFER_SIZE (buffer);
381 GST_LOG_OBJECT (rtpvrawpay, "new frame of %u bytes", size);
383 /* get pointer and strides of the planes */
384 yp = data + rtpvrawpay->yp;
385 up = data + rtpvrawpay->up;
386 vp = data + rtpvrawpay->vp;
388 ystride = rtpvrawpay->ystride;
389 uvstride = rtpvrawpay->uvstride;
391 mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
393 /* amount of bytes for one pixel */
394 pgroup = rtpvrawpay->pgroup;
395 width = rtpvrawpay->width;
396 height = rtpvrawpay->height;
398 /* start with line 0, offset 0 */
402 /* write all lines */
403 while (line < height) {
406 guint8 *outdata, *headers;
408 guint length, cont, pixels, fieldid;
410 /* get the max allowed payload length size, we try to fill the complete MTU */
411 left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
412 out = gst_rtp_buffer_new_allocate (left, 0, 0);
414 GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer);
416 outdata = gst_rtp_buffer_get_payload (out);
418 GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
423 * 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
424 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
425 * | Extended Sequence Number | Length |
426 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
427 * |F| Line No |C| Offset |
428 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
429 * | Length |F| Line No |
430 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
432 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ .
434 * . Two (partial) lines of video data .
436 * +---------------------------------------------------------------+
439 /* need 2 bytes for the extended sequence number */
444 /* the headers start here */
447 /* while we can fit at least one header and one pixel */
448 while (left > (6 + pgroup)) {
449 /* we need a 6 bytes header */
452 /* get how may bytes we need for the remaining pixels */
453 pixels = width - offset;
454 length = (pixels * pgroup) / rtpvrawpay->xinc;
456 if (left >= length) {
457 /* pixels and header fit completely, we will write them and skip to the
461 /* line does not fit completely, see how many pixels fit */
462 pixels = (left / pgroup) * rtpvrawpay->xinc;
463 length = (pixels * pgroup) / rtpvrawpay->xinc;
466 GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
471 *outdata++ = (length >> 8) & 0xff;
472 *outdata++ = length & 0xff;
474 /* always 0 for now */
478 *outdata++ = ((line >> 8) & 0x7f) | fieldid;
479 *outdata++ = line & 0xff;
482 /* go to next line we do this here to make the check below easier */
483 line += rtpvrawpay->yinc;
486 /* calculate continuation marker */
487 cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
489 /* write offset and continuation marker */
490 *outdata++ = ((offset >> 8) & 0x7f) | cont;
491 *outdata++ = offset & 0xff;
496 GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
499 GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
505 GST_LOG_OBJECT (rtpvrawpay, "consumed %d bytes",
506 (gint) (outdata - headers));
508 /* second pass, read headers and write the data */
512 /* read length and cont */
513 length = (headers[0] << 8) | headers[1];
514 lin = ((headers[2] & 0x7f) << 8) | headers[3];
515 offs = ((headers[4] & 0x7f) << 8) | headers[5];
516 cont = headers[4] & 0x80;
517 pixels = length / pgroup;
520 GST_LOG_OBJECT (payload, "writing length %u, line %u, offset %u, cont %d",
521 length, lin, offs, cont);
523 switch (rtpvrawpay->sampling) {
524 case GST_VIDEO_FORMAT_RGB:
525 case GST_VIDEO_FORMAT_RGBA:
526 case GST_VIDEO_FORMAT_BGR:
527 case GST_VIDEO_FORMAT_BGRA:
528 case GST_VIDEO_FORMAT_UYVY:
529 offs /= rtpvrawpay->xinc;
530 memcpy (outdata, yp + (lin * ystride) + (offs * pgroup), length);
533 case GST_VIDEO_FORMAT_AYUV:
538 datap = yp + (lin * ystride) + (offs * 4);
540 for (i = 0; i < pixels; i++) {
541 *outdata++ = datap[2];
542 *outdata++ = datap[1];
543 *outdata++ = datap[3];
548 case GST_VIDEO_FORMAT_I420:
552 guint8 *yd1p, *yd2p, *udp, *vdp;
554 yd1p = yp + (lin * ystride) + (offs);
555 yd2p = yd1p + ystride;
557 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
561 for (i = 0; i < pixels; i++) {
562 *outdata++ = *yd1p++;
563 *outdata++ = *yd1p++;
564 *outdata++ = *yd2p++;
565 *outdata++ = *yd2p++;
571 case GST_VIDEO_FORMAT_Y41B:
575 guint8 *ydp, *udp, *vdp;
577 ydp = yp + (lin * ystride) + offs;
579 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
583 for (i = 0; i < pixels; i++) {
594 gst_buffer_unref (out);
595 goto unknown_sampling;
602 if (line >= height) {
603 GST_LOG_OBJECT (rtpvrawpay, "frame complete, set marker");
604 gst_rtp_buffer_set_marker (out, TRUE);
607 GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
608 GST_BUFFER_SIZE (out) -= left;
612 ret = gst_basertppayload_push (payload, out);
614 gst_buffer_unref (buffer);
621 GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
622 (NULL), ("unimplemented sampling"));
623 gst_buffer_unref (buffer);
624 return GST_FLOW_NOT_SUPPORTED;
629 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
631 return gst_element_register (plugin, "rtpvrawpay",
632 GST_RANK_NONE, GST_TYPE_RTP_VRAW_PAY);