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 static GstStaticPadTemplate gst_rtp_vraw_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_STATIC_CAPS ("video/x-raw-rgb, "
40 "endianness = (int) BIG_ENDIAN, "
41 "red_mask = (int) 0xFF000000, "
42 "green_mask = (int) 0x00FF0000, "
43 "blue_mask = (int) 0x0000FF00, "
44 "width = (int) [ 1, 32767 ], "
45 "height = (int) [ 1, 32767 ]; "
49 "endianness = (int) BIG_ENDIAN, "
50 "red_mask = (int) 0xFF000000, "
51 "green_mask = (int) 0x00FF0000, "
52 "blue_mask = (int) 0x0000FF00, "
53 "alpha_mask = (int) 0x000000FF, "
54 "width = (int) [ 1, 32767 ], "
55 "height = (int) [ 1, 32767 ]; "
59 "endianness = (int) BIG_ENDIAN, "
60 "red_mask = (int) 0x0000FF00, "
61 "green_mask = (int) 0x00FF0000, "
62 "blue_mask = (int) 0xFF000000, "
63 "width = (int) [ 1, 32767 ], "
64 "height = (int) [ 1, 32767 ]; "
68 "endianness = (int) BIG_ENDIAN, "
69 "red_mask = (int) 0x0000FF00, "
70 "green_mask = (int) 0x00FF0000, "
71 "blue_mask = (int) 0xFF000000, "
72 "alpha_mask = (int) 0x000000FF, "
73 "width = (int) [ 1, 32767 ], "
74 "height = (int) [ 1, 32767 ]; "
76 "format = (fourcc) { AYUV, UYVY, I420, Y41B }, "
77 "width = (int) [ 1, 32767 ], " "height = (int) [ 1, 32767 ]; ")
80 static GstStaticPadTemplate gst_rtp_vraw_pay_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
84 GST_STATIC_CAPS ("application/x-rtp, "
85 "media = (string) \"video\", "
86 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
87 "clock-rate = (int) 90000, "
88 "encoding-name = (string) \"RAW\","
89 "sampling = (string) { \"RGB\", \"RGBA\", \"BGR\", \"BGRA\", "
90 "\"YCbCr-4:4:4\", \"YCbCr-4:2:2\", \"YCbCr-4:2:0\", "
92 /* we cannot express these as strings
93 * "width = (string) [1 32767],"
94 * "height = (string) [1 32767],"
96 "depth = (string) { \"8\", \"10\", \"12\", \"16\" },"
97 "colorimetry = (string) { \"BT601-5\", \"BT709-2\", \"SMPTE240M\" }"
101 * chroma-position = (string)
107 static gboolean gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload,
109 static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload *
110 payload, GstBuffer * buffer);
112 GST_BOILERPLATE (GstRtpVRawPay, gst_rtp_vraw_pay, GstBaseRTPPayload,
113 GST_TYPE_BASE_RTP_PAYLOAD)
115 static void gst_rtp_vraw_pay_base_init (gpointer klass)
117 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
119 gst_element_class_add_pad_template (element_class,
120 gst_static_pad_template_get (&gst_rtp_vraw_pay_src_template));
121 gst_element_class_add_pad_template (element_class,
122 gst_static_pad_template_get (&gst_rtp_vraw_pay_sink_template));
124 gst_element_class_set_details_simple (element_class,
125 "RTP Raw Video payloader", "Codec/Payloader/Network",
126 "Payload raw video as RTP packets (RFC 4175)",
127 "Wim Taymans <wim.taymans@gmail.com>");
131 gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
133 GstBaseRTPPayloadClass *gstbasertppayload_class;
135 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
137 gstbasertppayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
138 gstbasertppayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
140 GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
141 "Raw video RTP Payloader");
145 gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay, GstRtpVRawPayClass * klass)
150 gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
152 GstRtpVRawPay *rtpvrawpay;
158 gint pgroup, ystride, uvstride = 0, xinc, yinc;
159 GstVideoFormat sampling;
160 const gchar *depthstr, *samplingstr, *colorimetrystr;
164 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
166 s = gst_caps_get_structure (caps, 0);
168 /* start parsing the format */
169 name = gst_structure_get_name (s);
171 /* these values are the only thing we can do */
173 colorimetrystr = "SMPTE240M";
175 /* parse common width/height */
176 res = gst_structure_get_int (s, "width", &width);
177 res &= gst_structure_get_int (s, "height", &height);
179 goto missing_dimension;
181 /* fail on interlaced video for now */
182 if (!gst_structure_get_boolean (s, "interlaced", &interlaced))
191 if (!strcmp (name, "video/x-raw-rgb")) {
195 has_alpha = gst_structure_get_int (s, "alpha_mask", &amask);
197 if (!gst_structure_get_int (s, "red_mask", &rmask))
203 if (rmask == 0xFF000000) {
204 sampling = GST_VIDEO_FORMAT_RGBA;
205 samplingstr = "RGBA";
207 sampling = GST_VIDEO_FORMAT_BGRA;
208 samplingstr = "BGRA";
212 ystride = GST_ROUND_UP_4 (width * 3);
213 if (rmask == 0xFF000000) {
214 sampling = GST_VIDEO_FORMAT_RGB;
217 sampling = GST_VIDEO_FORMAT_BGR;
221 } else if (!strcmp (name, "video/x-raw-yuv")) {
224 if (!gst_structure_get_fourcc (s, "format", &fourcc))
227 GST_LOG_OBJECT (payload, "have fourcc %" GST_FOURCC_FORMAT,
228 GST_FOURCC_ARGS (fourcc));
231 case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
232 sampling = GST_VIDEO_FORMAT_AYUV;
233 samplingstr = "YCbCr-4:4:4";
237 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
238 sampling = GST_VIDEO_FORMAT_UYVY;
239 samplingstr = "YCbCr-4:2:2";
242 ystride = GST_ROUND_UP_2 (width) * 2;
244 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
245 sampling = GST_VIDEO_FORMAT_Y41B;
246 samplingstr = "YCbCr-4:1:1";
249 ystride = GST_ROUND_UP_4 (width);
250 uvstride = GST_ROUND_UP_8 (width) / 4;
251 up = ystride * height;
252 vp = up + uvstride * height;
254 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
255 sampling = GST_VIDEO_FORMAT_I420;
256 samplingstr = "YCbCr-4:2:0";
259 ystride = GST_ROUND_UP_4 (width);
260 uvstride = GST_ROUND_UP_8 (width) / 2;
261 up = ystride * GST_ROUND_UP_2 (height);
262 vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
270 rtpvrawpay->width = width;
271 rtpvrawpay->height = height;
272 rtpvrawpay->sampling = sampling;
273 rtpvrawpay->pgroup = pgroup;
274 rtpvrawpay->xinc = xinc;
275 rtpvrawpay->yinc = yinc;
279 rtpvrawpay->ystride = ystride;
280 rtpvrawpay->uvstride = uvstride;
282 GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %d", width, height,
284 GST_DEBUG_OBJECT (payload, "yp %d, up %d, vp %d", yp, up, vp);
285 GST_DEBUG_OBJECT (payload, "pgroup %d, ystride %d, uvstride %d", pgroup,
288 wstr = g_strdup_printf ("%d", rtpvrawpay->width);
289 hstr = g_strdup_printf ("%d", rtpvrawpay->height);
291 gst_basertppayload_set_options (payload, "video", TRUE, "RAW", 90000);
292 res = gst_basertppayload_set_outcaps (payload, "sampling", G_TYPE_STRING,
293 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
294 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
295 colorimetrystr, NULL);
304 GST_ERROR_OBJECT (payload, "unknown red mask specified");
309 GST_ERROR_OBJECT (payload, "unknown caps format");
314 GST_ERROR_OBJECT (payload, "invalid or missing fourcc");
319 GST_ERROR_OBJECT (payload, "interlaced video not supported yet");
324 GST_ERROR_OBJECT (payload, "missing width or height property");
330 gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
332 GstRtpVRawPay *rtpvrawpay;
333 GstFlowReturn ret = GST_FLOW_OK;
335 guint8 *data, *yp, *up, *vp;
336 guint ystride, uvstride;
341 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
343 data = GST_BUFFER_DATA (buffer);
344 size = GST_BUFFER_SIZE (buffer);
346 GST_LOG_OBJECT (rtpvrawpay, "new frame of %u bytes", size);
348 /* get pointer and strides of the planes */
349 yp = data + rtpvrawpay->yp;
350 up = data + rtpvrawpay->up;
351 vp = data + rtpvrawpay->vp;
353 ystride = rtpvrawpay->ystride;
354 uvstride = rtpvrawpay->uvstride;
356 mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
358 /* amount of bytes for one pixel */
359 pgroup = rtpvrawpay->pgroup;
360 width = rtpvrawpay->width;
361 height = rtpvrawpay->height;
363 /* start with line 0, offset 0 */
367 /* write all lines */
368 while (line < height) {
371 guint8 *outdata, *headers;
373 guint length, cont, pixels, fieldid;
375 /* get the max allowed payload length size, we try to fill the complete MTU */
376 left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
377 out = gst_rtp_buffer_new_allocate (left, 0, 0);
379 GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer);
381 outdata = gst_rtp_buffer_get_payload (out);
383 GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
388 * 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
389 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
390 * | Extended Sequence Number | Length |
391 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
392 * |F| Line No |C| Offset |
393 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
394 * | Length |F| Line No |
395 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ .
399 * . Two (partial) lines of video data .
401 * +---------------------------------------------------------------+
404 /* need 2 bytes for the extended sequence number */
409 /* the headers start here */
412 /* while we can fit at least one header and one pixel */
413 while (left > (6 + pgroup)) {
414 /* we need a 6 bytes header */
417 /* get how may bytes we need for the remaining pixels */
418 pixels = width - offset;
419 length = (pixels * pgroup) / rtpvrawpay->xinc;
421 if (left >= length) {
422 /* pixels and header fit completely, we will write them and skip to the
426 /* line does not fit completely, see how many pixels fit */
427 pixels = (left / pgroup) * rtpvrawpay->xinc;
428 length = (pixels * pgroup) / rtpvrawpay->xinc;
431 GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
436 *outdata++ = (length >> 8) & 0xff;
437 *outdata++ = length & 0xff;
439 /* always 0 for now */
443 *outdata++ = ((line >> 8) & 0x7f) | fieldid;
444 *outdata++ = line & 0xff;
447 /* go to next line we do this here to make the check below easier */
448 line += rtpvrawpay->yinc;
451 /* calculate continuation marker */
452 cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
454 /* write offset and continuation marker */
455 *outdata++ = ((offset >> 8) & 0x7f) | cont;
456 *outdata++ = offset & 0xff;
461 GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
464 GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
470 GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
471 (guint) (outdata - headers));
473 /* second pass, read headers and write the data */
477 /* read length and cont */
478 length = (headers[0] << 8) | headers[1];
479 lin = ((headers[2] & 0x7f) << 8) | headers[3];
480 offs = ((headers[4] & 0x7f) << 8) | headers[5];
481 cont = headers[4] & 0x80;
482 pixels = length / pgroup;
485 GST_LOG_OBJECT (payload, "writing length %u, line %u, offset %u, cont %d",
486 length, lin, offs, cont);
488 switch (rtpvrawpay->sampling) {
489 case GST_VIDEO_FORMAT_RGB:
490 case GST_VIDEO_FORMAT_RGBA:
491 case GST_VIDEO_FORMAT_BGR:
492 case GST_VIDEO_FORMAT_BGRA:
493 case GST_VIDEO_FORMAT_UYVY:
494 offs /= rtpvrawpay->xinc;
495 memcpy (outdata, yp + (lin * ystride) + (offs * pgroup), length);
498 case GST_VIDEO_FORMAT_AYUV:
503 datap = yp + (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;
522 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
526 for (i = 0; i < pixels; i++) {
527 *outdata++ = *yd1p++;
528 *outdata++ = *yd1p++;
529 *outdata++ = *yd2p++;
530 *outdata++ = *yd2p++;
536 case GST_VIDEO_FORMAT_Y41B:
540 guint8 *ydp, *udp, *vdp;
542 ydp = yp + (lin * ystride) + offs;
544 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
548 for (i = 0; i < pixels; i++) {
559 gst_buffer_unref (out);
560 goto unknown_sampling;
567 if (line >= height) {
568 GST_LOG_OBJECT (rtpvrawpay, "frame complete, set marker");
569 gst_rtp_buffer_set_marker (out, TRUE);
572 GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
573 GST_BUFFER_SIZE (out) -= left;
577 ret = gst_basertppayload_push (payload, out);
579 gst_buffer_unref (buffer);
586 GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
587 (NULL), ("unimplemented sampling"));
588 gst_buffer_unref (buffer);
589 return GST_FLOW_NOT_SUPPORTED;
594 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
596 return gst_element_register (plugin, "rtpvrawpay",
597 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);