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, "
38 "format = (string) { RGB, RGBA, BGR, BGRA, AYUYV, UYVY, I420, Y41B, UYVP, I420, Y42B, Y444 }, "
39 "width = (int) [ 1, 32767 ], " "height = (int) [ 1, 32767 ]; ")
42 static GstStaticPadTemplate gst_rtp_vraw_pay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"video\", "
48 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
49 "clock-rate = (int) 90000, "
50 "encoding-name = (string) \"RAW\","
51 "sampling = (string) { \"RGB\", \"RGBA\", \"BGR\", \"BGRA\", "
52 "\"YCbCr-4:4:4\", \"YCbCr-4:2:2\", \"YCbCr-4:2:0\", "
54 /* we cannot express these as strings
55 * "width = (string) [1 32767],"
56 * "height = (string) [1 32767],"
58 "depth = (string) { \"8\", \"10\", \"12\", \"16\" },"
59 "colorimetry = (string) { \"BT601-5\", \"BT709-2\", \"SMPTE240M\" }"
63 * chroma-position = (string)
69 static gboolean gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload,
71 static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload *
72 payload, GstBuffer * buffer);
74 G_DEFINE_TYPE (GstRtpVRawPay, gst_rtp_vraw_pay, GST_TYPE_BASE_RTP_PAYLOAD)
76 static void gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
78 GstBaseRTPPayloadClass *gstbasertppayload_class;
79 GstElementClass *gstelement_class;
81 gstelement_class = (GstElementClass *) klass;
82 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
84 gstbasertppayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
85 gstbasertppayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
87 gst_element_class_add_pad_template (gstelement_class,
88 gst_static_pad_template_get (&gst_rtp_vraw_pay_src_template));
89 gst_element_class_add_pad_template (gstelement_class,
90 gst_static_pad_template_get (&gst_rtp_vraw_pay_sink_template));
92 gst_element_class_set_details_simple (gstelement_class,
93 "RTP Raw Video payloader", "Codec/Payloader/Network/RTP",
94 "Payload raw video as RTP packets (RFC 4175)",
95 "Wim Taymans <wim.taymans@gmail.com>");
97 GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
98 "Raw video RTP Payloader");
102 gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay)
107 gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
109 GstRtpVRawPay *rtpvrawpay;
111 gint pgroup, xinc, yinc;
112 const gchar *depthstr, *samplingstr, *colorimetrystr;
117 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
119 if (!gst_video_info_from_caps (&info, caps))
122 rtpvrawpay->vinfo = info;
124 colorimetrystr = "SMPTE240M";
125 if (info.color_matrix) {
126 if (g_str_equal (info.color_matrix, "sdtv")) {
127 /* BT.601 implies a bit more than just color-matrix */
128 colorimetrystr = "BT601-5";
129 } else if (g_str_equal (info.color_matrix, "hdtv")) {
130 colorimetrystr = "BT709-2";
136 /* these values are the only thing we can do */
140 switch (GST_VIDEO_INFO_FORMAT (&info)) {
141 case GST_VIDEO_FORMAT_RGBA:
142 samplingstr = "RGBA";
145 case GST_VIDEO_FORMAT_BGRA:
146 samplingstr = "BGRA";
149 case GST_VIDEO_FORMAT_RGB:
152 case GST_VIDEO_FORMAT_BGR:
156 case GST_VIDEO_FORMAT_AYUV:
157 samplingstr = "YCbCr-4:4:4";
160 case GST_VIDEO_FORMAT_UYVY:
161 samplingstr = "YCbCr-4:2:2";
165 case GST_VIDEO_FORMAT_Y41B:
166 samplingstr = "YCbCr-4:1:1";
170 case GST_VIDEO_FORMAT_I420:
171 samplingstr = "YCbCr-4:2:0";
175 case GST_VIDEO_FORMAT_UYVP:
176 samplingstr = "YCbCr-4:2:2";
187 if (info.flags & GST_VIDEO_FLAG_INTERLACED) {
191 rtpvrawpay->pgroup = pgroup;
192 rtpvrawpay->xinc = xinc;
193 rtpvrawpay->yinc = yinc;
194 rtpvrawpay->depth = depth;
196 GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %s",
197 GST_VIDEO_INFO_WIDTH (&info), GST_VIDEO_INFO_HEIGHT (&info), samplingstr);
198 GST_DEBUG_OBJECT (payload, "xinc %d, yinc %d, pgroup %d", xinc, yinc, pgroup);
200 wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
201 hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
203 gst_basertppayload_set_options (payload, "video", TRUE, "RAW", 90000);
204 if (info.flags & GST_VIDEO_FLAG_INTERLACED) {
205 res = gst_basertppayload_set_outcaps (payload, "sampling", G_TYPE_STRING,
206 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
207 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
208 colorimetrystr, "interlace", G_TYPE_STRING, "true", NULL);
210 res = gst_basertppayload_set_outcaps (payload, "sampling", G_TYPE_STRING,
211 samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
212 wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
213 colorimetrystr, NULL);
223 GST_ERROR_OBJECT (payload, "could not parse caps");
228 GST_ERROR_OBJECT (payload, "unknown caps format");
234 gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
236 GstRtpVRawPay *rtpvrawpay;
237 GstFlowReturn ret = GST_FLOW_OK;
239 guint8 *yp, *up, *vp;
240 guint ystride, uvstride;
249 rtpvrawpay = GST_RTP_VRAW_PAY (payload);
251 gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ);
253 GST_LOG_OBJECT (rtpvrawpay, "new frame of %u bytes",
254 gst_buffer_get_size (buffer));
256 /* get pointer and strides of the planes */
257 yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
258 up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
259 vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
261 ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
262 uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
264 mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
266 /* amount of bytes for one pixel */
267 pgroup = rtpvrawpay->pgroup;
268 width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
269 height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
271 interlaced = !!(rtpvrawpay->vinfo.flags & GST_VIDEO_FLAG_INTERLACED);
273 /* start with line 0, offset 0 */
274 for (field = 0; field < 1 + interlaced; field++) {
278 /* write all lines */
279 while (line < height) {
282 guint8 *outdata, *headers;
284 guint length, cont, pixels;
286 /* get the max allowed payload length size, we try to fill the complete MTU */
287 left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
288 out = gst_rtp_buffer_new_allocate (left, 0, 0);
291 GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer);
293 GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer) +
294 GST_BUFFER_DURATION (buffer) / 2;
297 gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
298 outdata = gst_rtp_buffer_get_payload (&rtp);
300 GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
305 * 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
306 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
307 * | Extended Sequence Number | Length |
308 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
309 * |F| Line No |C| Offset |
310 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
311 * | Length |F| Line No |
312 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
314 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ .
316 * . Two (partial) lines of video data .
318 * +---------------------------------------------------------------+
321 /* need 2 bytes for the extended sequence number */
326 /* the headers start here */
329 /* while we can fit at least one header and one pixel */
330 while (left > (6 + pgroup)) {
331 /* we need a 6 bytes header */
334 /* get how may bytes we need for the remaining pixels */
335 pixels = width - offset;
336 length = (pixels * pgroup) / rtpvrawpay->xinc;
338 if (left >= length) {
339 /* pixels and header fit completely, we will write them and skip to the
343 /* line does not fit completely, see how many pixels fit */
344 pixels = (left / pgroup) * rtpvrawpay->xinc;
345 length = (pixels * pgroup) / rtpvrawpay->xinc;
348 GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
353 *outdata++ = (length >> 8) & 0xff;
354 *outdata++ = length & 0xff;
357 *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
358 *outdata++ = line & 0xff;
361 /* go to next line we do this here to make the check below easier */
362 line += rtpvrawpay->yinc;
365 /* calculate continuation marker */
366 cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
368 /* write offset and continuation marker */
369 *outdata++ = ((offset >> 8) & 0x7f) | cont;
370 *outdata++ = offset & 0xff;
375 GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
378 GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
384 GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
385 (guint) (outdata - headers));
387 /* second pass, read headers and write the data */
391 /* read length and cont */
392 length = (headers[0] << 8) | headers[1];
393 lin = ((headers[2] & 0x7f) << 8) | headers[3];
394 offs = ((headers[4] & 0x7f) << 8) | headers[5];
395 cont = headers[4] & 0x80;
396 pixels = length / pgroup;
399 GST_LOG_OBJECT (payload,
400 "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
403 switch (GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo)) {
404 case GST_VIDEO_FORMAT_RGB:
405 case GST_VIDEO_FORMAT_RGBA:
406 case GST_VIDEO_FORMAT_BGR:
407 case GST_VIDEO_FORMAT_BGRA:
408 case GST_VIDEO_FORMAT_UYVY:
409 offs /= rtpvrawpay->xinc;
410 memcpy (outdata, yp + (lin * ystride) + (offs * pgroup), length);
413 case GST_VIDEO_FORMAT_AYUV:
418 datap = yp + (lin * ystride) + (offs * 4);
420 for (i = 0; i < pixels; i++) {
421 *outdata++ = datap[2];
422 *outdata++ = datap[1];
423 *outdata++ = datap[3];
428 case GST_VIDEO_FORMAT_I420:
432 guint8 *yd1p, *yd2p, *udp, *vdp;
434 yd1p = yp + (lin * ystride) + (offs);
435 yd2p = yd1p + ystride;
437 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
441 for (i = 0; i < pixels; i++) {
442 *outdata++ = *yd1p++;
443 *outdata++ = *yd1p++;
444 *outdata++ = *yd2p++;
445 *outdata++ = *yd2p++;
451 case GST_VIDEO_FORMAT_Y41B:
455 guint8 *ydp, *udp, *vdp;
457 ydp = yp + (lin * ystride) + offs;
459 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
463 for (i = 0; i < pixels; i++) {
474 gst_rtp_buffer_unmap (&rtp);
475 gst_buffer_unref (out);
476 goto unknown_sampling;
483 if (line >= height) {
484 GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
485 gst_rtp_buffer_set_marker (&rtp, TRUE);
487 gst_rtp_buffer_unmap (&rtp);
489 GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
490 gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
494 ret = gst_basertppayload_push (payload, out);
499 gst_video_frame_unmap (&frame);
500 gst_buffer_unref (buffer);
507 GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
508 (NULL), ("unimplemented sampling"));
509 gst_video_frame_unmap (&frame);
510 gst_buffer_unref (buffer);
511 return GST_FLOW_NOT_SUPPORTED;
516 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
518 return gst_element_register (plugin, "rtpvrawpay",
519 GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);