Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpvrawpay.c
1 /* GStreamer
2  * Copyright (C) <2008> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpvrawpay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpvrawpay_debug);
31 #define GST_CAT_DEFAULT (rtpvrawpay_debug)
32
33 static GstStaticPadTemplate gst_rtp_vraw_pay_sink_template =
34     GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
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 ]; ")
40     );
41
42 static GstStaticPadTemplate gst_rtp_vraw_pay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
44     GST_PAD_SRC,
45     GST_PAD_ALWAYS,
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\", "
53         "\"YCbCr-4:1:1\" },"
54         /* we cannot express these as strings 
55          * "width = (string) [1 32767],"
56          * "height = (string) [1 32767],"
57          */
58         "depth = (string) { \"8\", \"10\", \"12\", \"16\" },"
59         "colorimetry = (string) { \"BT601-5\", \"BT709-2\", \"SMPTE240M\" }"
60         /* optional 
61          * interlace = 
62          * top-field-first = 
63          * chroma-position = (string) 
64          * gamma = (float)
65          */
66     )
67     );
68
69 static gboolean gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload,
70     GstCaps * caps);
71 static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload *
72     payload, GstBuffer * buffer);
73
74 G_DEFINE_TYPE (GstRtpVRawPay, gst_rtp_vraw_pay, GST_TYPE_BASE_RTP_PAYLOAD)
75
76      static void gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
77 {
78   GstBaseRTPPayloadClass *gstbasertppayload_class;
79   GstElementClass *gstelement_class;
80
81   gstelement_class = (GstElementClass *) klass;
82   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
83
84   gstbasertppayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
85   gstbasertppayload_class->handle_buffer = gst_rtp_vraw_pay_handle_buffer;
86
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));
91
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>");
96
97   GST_DEBUG_CATEGORY_INIT (rtpvrawpay_debug, "rtpvrawpay", 0,
98       "Raw video RTP Payloader");
99 }
100
101 static void
102 gst_rtp_vraw_pay_init (GstRtpVRawPay * rtpvrawpay)
103 {
104 }
105
106 static gboolean
107 gst_rtp_vraw_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
108 {
109   GstRtpVRawPay *rtpvrawpay;
110   gboolean res;
111   gint pgroup, xinc, yinc;
112   const gchar *depthstr, *samplingstr, *colorimetrystr;
113   gchar *wstr, *hstr;
114   gint depth;
115   GstVideoInfo info;
116
117   rtpvrawpay = GST_RTP_VRAW_PAY (payload);
118
119   if (!gst_video_info_from_caps (&info, caps))
120     goto invalid_caps;
121
122   rtpvrawpay->vinfo = info;
123
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";
131     }
132   }
133
134   xinc = yinc = 1;
135
136   /* these values are the only thing we can do */
137   depthstr = "8";
138   depth = 8;
139
140   switch (GST_VIDEO_INFO_FORMAT (&info)) {
141     case GST_VIDEO_FORMAT_RGBA:
142       samplingstr = "RGBA";
143       pgroup = 4;
144       break;
145     case GST_VIDEO_FORMAT_BGRA:
146       samplingstr = "BGRA";
147       pgroup = 4;
148       break;
149     case GST_VIDEO_FORMAT_RGB:
150       samplingstr = "RGB";
151       pgroup = 3;
152     case GST_VIDEO_FORMAT_BGR:
153       samplingstr = "BGR";
154       pgroup = 3;
155       break;
156     case GST_VIDEO_FORMAT_AYUV:
157       samplingstr = "YCbCr-4:4:4";
158       pgroup = 3;
159       break;
160     case GST_VIDEO_FORMAT_UYVY:
161       samplingstr = "YCbCr-4:2:2";
162       pgroup = 4;
163       xinc = 2;
164       break;
165     case GST_VIDEO_FORMAT_Y41B:
166       samplingstr = "YCbCr-4:1:1";
167       pgroup = 6;
168       xinc = 4;
169       break;
170     case GST_VIDEO_FORMAT_I420:
171       samplingstr = "YCbCr-4:2:0";
172       pgroup = 6;
173       xinc = yinc = 2;
174       break;
175     case GST_VIDEO_FORMAT_UYVP:
176       samplingstr = "YCbCr-4:2:2";
177       pgroup = 4;
178       xinc = 2;
179       depth = 10;
180       depthstr = "10";
181       break;
182     default:
183       goto unknown_format;
184       break;
185   }
186
187   if (info.flags & GST_VIDEO_FLAG_INTERLACED) {
188     yinc *= 2;
189   }
190
191   rtpvrawpay->pgroup = pgroup;
192   rtpvrawpay->xinc = xinc;
193   rtpvrawpay->yinc = yinc;
194   rtpvrawpay->depth = depth;
195
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);
199
200   wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
201   hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
202
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);
209   } else {
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);
214   }
215   g_free (wstr);
216   g_free (hstr);
217
218   return res;
219
220   /* ERRORS */
221 invalid_caps:
222   {
223     GST_ERROR_OBJECT (payload, "could not parse caps");
224     return FALSE;
225   }
226 unknown_format:
227   {
228     GST_ERROR_OBJECT (payload, "unknown caps format");
229     return FALSE;
230   }
231 }
232
233 static GstFlowReturn
234 gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
235 {
236   GstRtpVRawPay *rtpvrawpay;
237   GstFlowReturn ret = GST_FLOW_OK;
238   guint line, offset;
239   guint8 *yp, *up, *vp;
240   guint ystride, uvstride;
241   guint pgroup;
242   guint mtu;
243   guint width, height;
244   gint field;
245   GstVideoFrame frame;
246   gint interlaced;
247   GstRTPBuffer rtp;
248
249   rtpvrawpay = GST_RTP_VRAW_PAY (payload);
250
251   gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ);
252
253   GST_LOG_OBJECT (rtpvrawpay, "new frame of %u bytes",
254       gst_buffer_get_size (buffer));
255
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);
260
261   ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
262   uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
263
264   mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
265
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);
270
271   interlaced = !!(rtpvrawpay->vinfo.flags & GST_VIDEO_FLAG_INTERLACED);
272
273   /* start with line 0, offset 0 */
274   for (field = 0; field < 1 + interlaced; field++) {
275     line = field;
276     offset = 0;
277
278     /* write all lines */
279     while (line < height) {
280       guint left;
281       GstBuffer *out;
282       guint8 *outdata, *headers;
283       gboolean next_line;
284       guint length, cont, pixels;
285
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);
289
290       if (field == 0) {
291         GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer);
292       } else {
293         GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer) +
294             GST_BUFFER_DURATION (buffer) / 2;
295       }
296
297       gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
298       outdata = gst_rtp_buffer_get_payload (&rtp);
299
300       GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
301           mtu);
302
303       /*
304        *   0                   1                   2                   3
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        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
313        *  |C|           Offset            |                               .
314        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               .
315        *  .                                                               .
316        *  .                 Two (partial) lines of video data             .
317        *  .                                                               .
318        *  +---------------------------------------------------------------+
319        */
320
321       /* need 2 bytes for the extended sequence number */
322       *outdata++ = 0;
323       *outdata++ = 0;
324       left -= 2;
325
326       /* the headers start here */
327       headers = outdata;
328
329       /* while we can fit at least one header and one pixel */
330       while (left > (6 + pgroup)) {
331         /* we need a 6 bytes header */
332         left -= 6;
333
334         /* get how may bytes we need for the remaining pixels */
335         pixels = width - offset;
336         length = (pixels * pgroup) / rtpvrawpay->xinc;
337
338         if (left >= length) {
339           /* pixels and header fit completely, we will write them and skip to the
340            * next line. */
341           next_line = TRUE;
342         } else {
343           /* line does not fit completely, see how many pixels fit */
344           pixels = (left / pgroup) * rtpvrawpay->xinc;
345           length = (pixels * pgroup) / rtpvrawpay->xinc;
346           next_line = FALSE;
347         }
348         GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
349             pixels);
350         left -= length;
351
352         /* write length */
353         *outdata++ = (length >> 8) & 0xff;
354         *outdata++ = length & 0xff;
355
356         /* write line no */
357         *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
358         *outdata++ = line & 0xff;
359
360         if (next_line) {
361           /* go to next line we do this here to make the check below easier */
362           line += rtpvrawpay->yinc;
363         }
364
365         /* calculate continuation marker */
366         cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
367
368         /* write offset and continuation marker */
369         *outdata++ = ((offset >> 8) & 0x7f) | cont;
370         *outdata++ = offset & 0xff;
371
372         if (next_line) {
373           /* reset offset */
374           offset = 0;
375           GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
376         } else {
377           offset += pixels;
378           GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
379         }
380
381         if (!cont)
382           break;
383       }
384       GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
385           (guint) (outdata - headers));
386
387       /* second pass, read headers and write the data */
388       while (TRUE) {
389         guint offs, lin;
390
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;
397         headers += 6;
398
399         GST_LOG_OBJECT (payload,
400             "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
401             cont);
402
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);
411             outdata += length;
412             break;
413           case GST_VIDEO_FORMAT_AYUV:
414           {
415             gint i;
416             guint8 *datap;
417
418             datap = yp + (lin * ystride) + (offs * 4);
419
420             for (i = 0; i < pixels; i++) {
421               *outdata++ = datap[2];
422               *outdata++ = datap[1];
423               *outdata++ = datap[3];
424               datap += 4;
425             }
426             break;
427           }
428           case GST_VIDEO_FORMAT_I420:
429           {
430             gint i;
431             guint uvoff;
432             guint8 *yd1p, *yd2p, *udp, *vdp;
433
434             yd1p = yp + (lin * ystride) + (offs);
435             yd2p = yd1p + ystride;
436             uvoff =
437                 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
438             udp = up + uvoff;
439             vdp = vp + uvoff;
440
441             for (i = 0; i < pixels; i++) {
442               *outdata++ = *yd1p++;
443               *outdata++ = *yd1p++;
444               *outdata++ = *yd2p++;
445               *outdata++ = *yd2p++;
446               *outdata++ = *udp++;
447               *outdata++ = *vdp++;
448             }
449             break;
450           }
451           case GST_VIDEO_FORMAT_Y41B:
452           {
453             gint i;
454             guint uvoff;
455             guint8 *ydp, *udp, *vdp;
456
457             ydp = yp + (lin * ystride) + offs;
458             uvoff =
459                 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
460             udp = up + uvoff;
461             vdp = vp + uvoff;
462
463             for (i = 0; i < pixels; i++) {
464               *outdata++ = *udp++;
465               *outdata++ = *ydp++;
466               *outdata++ = *ydp++;
467               *outdata++ = *vdp++;
468               *outdata++ = *ydp++;
469               *outdata++ = *ydp++;
470             }
471             break;
472           }
473           default:
474             gst_rtp_buffer_unmap (&rtp);
475             gst_buffer_unref (out);
476             goto unknown_sampling;
477         }
478
479         if (!cont)
480           break;
481       }
482
483       if (line >= height) {
484         GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
485         gst_rtp_buffer_set_marker (&rtp, TRUE);
486       }
487       gst_rtp_buffer_unmap (&rtp);
488       if (left > 0) {
489         GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
490         gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
491       }
492
493       /* push buffer */
494       ret = gst_basertppayload_push (payload, out);
495     }
496
497   }
498
499   gst_video_frame_unmap (&frame);
500   gst_buffer_unref (buffer);
501
502   return ret;
503
504   /* ERRORS */
505 unknown_sampling:
506   {
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;
512   }
513 }
514
515 gboolean
516 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
517 {
518   return gst_element_register (plugin, "rtpvrawpay",
519       GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);
520 }