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   if (gst_video_colorimetry_matches (&info.colorimetry,
125           GST_VIDEO_COLORIMETRY_BT601)) {
126     colorimetrystr = "BT601-5";
127   } else if (gst_video_colorimetry_matches (&info.colorimetry,
128           GST_VIDEO_COLORIMETRY_BT709)) {
129     colorimetrystr = "BT709-2";
130   } else if (gst_video_colorimetry_matches (&info.colorimetry,
131           GST_VIDEO_COLORIMETRY_SMPTE240M)) {
132     colorimetrystr = "SMPTE240M";
133   } else {
134     colorimetrystr = "SMPTE240M";
135   }
136
137   xinc = yinc = 1;
138
139   /* these values are the only thing we can do */
140   depthstr = "8";
141   depth = 8;
142
143   switch (GST_VIDEO_INFO_FORMAT (&info)) {
144     case GST_VIDEO_FORMAT_RGBA:
145       samplingstr = "RGBA";
146       pgroup = 4;
147       break;
148     case GST_VIDEO_FORMAT_BGRA:
149       samplingstr = "BGRA";
150       pgroup = 4;
151       break;
152     case GST_VIDEO_FORMAT_RGB:
153       samplingstr = "RGB";
154       pgroup = 3;
155     case GST_VIDEO_FORMAT_BGR:
156       samplingstr = "BGR";
157       pgroup = 3;
158       break;
159     case GST_VIDEO_FORMAT_AYUV:
160       samplingstr = "YCbCr-4:4:4";
161       pgroup = 3;
162       break;
163     case GST_VIDEO_FORMAT_UYVY:
164       samplingstr = "YCbCr-4:2:2";
165       pgroup = 4;
166       xinc = 2;
167       break;
168     case GST_VIDEO_FORMAT_Y41B:
169       samplingstr = "YCbCr-4:1:1";
170       pgroup = 6;
171       xinc = 4;
172       break;
173     case GST_VIDEO_FORMAT_I420:
174       samplingstr = "YCbCr-4:2:0";
175       pgroup = 6;
176       xinc = yinc = 2;
177       break;
178     case GST_VIDEO_FORMAT_UYVP:
179       samplingstr = "YCbCr-4:2:2";
180       pgroup = 4;
181       xinc = 2;
182       depth = 10;
183       depthstr = "10";
184       break;
185     default:
186       goto unknown_format;
187       break;
188   }
189
190   if (info.flags & GST_VIDEO_FLAG_INTERLACED) {
191     yinc *= 2;
192   }
193
194   rtpvrawpay->pgroup = pgroup;
195   rtpvrawpay->xinc = xinc;
196   rtpvrawpay->yinc = yinc;
197   rtpvrawpay->depth = depth;
198
199   GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %s",
200       GST_VIDEO_INFO_WIDTH (&info), GST_VIDEO_INFO_HEIGHT (&info), samplingstr);
201   GST_DEBUG_OBJECT (payload, "xinc %d, yinc %d, pgroup %d", xinc, yinc, pgroup);
202
203   wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
204   hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
205
206   gst_basertppayload_set_options (payload, "video", TRUE, "RAW", 90000);
207   if (info.flags & GST_VIDEO_FLAG_INTERLACED) {
208     res = gst_basertppayload_set_outcaps (payload, "sampling", G_TYPE_STRING,
209         samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
210         wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
211         colorimetrystr, "interlace", G_TYPE_STRING, "true", NULL);
212   } else {
213     res = gst_basertppayload_set_outcaps (payload, "sampling", G_TYPE_STRING,
214         samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
215         wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
216         colorimetrystr, NULL);
217   }
218   g_free (wstr);
219   g_free (hstr);
220
221   return res;
222
223   /* ERRORS */
224 invalid_caps:
225   {
226     GST_ERROR_OBJECT (payload, "could not parse caps");
227     return FALSE;
228   }
229 unknown_format:
230   {
231     GST_ERROR_OBJECT (payload, "unknown caps format");
232     return FALSE;
233   }
234 }
235
236 static GstFlowReturn
237 gst_rtp_vraw_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
238 {
239   GstRtpVRawPay *rtpvrawpay;
240   GstFlowReturn ret = GST_FLOW_OK;
241   guint line, offset;
242   guint8 *yp, *up, *vp;
243   guint ystride, uvstride;
244   guint pgroup;
245   guint mtu;
246   guint width, height;
247   gint field;
248   GstVideoFrame frame;
249   gint interlaced;
250   GstRTPBuffer rtp;
251
252   rtpvrawpay = GST_RTP_VRAW_PAY (payload);
253
254   gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ);
255
256   GST_LOG_OBJECT (rtpvrawpay, "new frame of %u bytes",
257       gst_buffer_get_size (buffer));
258
259   /* get pointer and strides of the planes */
260   yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
261   up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
262   vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
263
264   ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
265   uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
266
267   mtu = GST_BASE_RTP_PAYLOAD_MTU (payload);
268
269   /* amount of bytes for one pixel */
270   pgroup = rtpvrawpay->pgroup;
271   width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
272   height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
273
274   interlaced = ! !(rtpvrawpay->vinfo.flags & GST_VIDEO_FLAG_INTERLACED);
275
276   /* start with line 0, offset 0 */
277   for (field = 0; field < 1 + interlaced; field++) {
278     line = field;
279     offset = 0;
280
281     /* write all lines */
282     while (line < height) {
283       guint left;
284       GstBuffer *out;
285       guint8 *outdata, *headers;
286       gboolean next_line;
287       guint length, cont, pixels;
288
289       /* get the max allowed payload length size, we try to fill the complete MTU */
290       left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
291       out = gst_rtp_buffer_new_allocate (left, 0, 0);
292
293       if (field == 0) {
294         GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer);
295       } else {
296         GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer) +
297             GST_BUFFER_DURATION (buffer) / 2;
298       }
299
300       gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
301       outdata = gst_rtp_buffer_get_payload (&rtp);
302
303       GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
304           mtu);
305
306       /*
307        *   0                   1                   2                   3
308        *   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
309        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
310        *  |   Extended Sequence Number    |            Length             |
311        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
312        *  |F|          Line No            |C|           Offset            |
313        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
314        *  |            Length             |F|          Line No            |
315        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
316        *  |C|           Offset            |                               .
317        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               .
318        *  .                                                               .
319        *  .                 Two (partial) lines of video data             .
320        *  .                                                               .
321        *  +---------------------------------------------------------------+
322        */
323
324       /* need 2 bytes for the extended sequence number */
325       *outdata++ = 0;
326       *outdata++ = 0;
327       left -= 2;
328
329       /* the headers start here */
330       headers = outdata;
331
332       /* while we can fit at least one header and one pixel */
333       while (left > (6 + pgroup)) {
334         /* we need a 6 bytes header */
335         left -= 6;
336
337         /* get how may bytes we need for the remaining pixels */
338         pixels = width - offset;
339         length = (pixels * pgroup) / rtpvrawpay->xinc;
340
341         if (left >= length) {
342           /* pixels and header fit completely, we will write them and skip to the
343            * next line. */
344           next_line = TRUE;
345         } else {
346           /* line does not fit completely, see how many pixels fit */
347           pixels = (left / pgroup) * rtpvrawpay->xinc;
348           length = (pixels * pgroup) / rtpvrawpay->xinc;
349           next_line = FALSE;
350         }
351         GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
352             pixels);
353         left -= length;
354
355         /* write length */
356         *outdata++ = (length >> 8) & 0xff;
357         *outdata++ = length & 0xff;
358
359         /* write line no */
360         *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
361         *outdata++ = line & 0xff;
362
363         if (next_line) {
364           /* go to next line we do this here to make the check below easier */
365           line += rtpvrawpay->yinc;
366         }
367
368         /* calculate continuation marker */
369         cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
370
371         /* write offset and continuation marker */
372         *outdata++ = ((offset >> 8) & 0x7f) | cont;
373         *outdata++ = offset & 0xff;
374
375         if (next_line) {
376           /* reset offset */
377           offset = 0;
378           GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
379         } else {
380           offset += pixels;
381           GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
382         }
383
384         if (!cont)
385           break;
386       }
387       GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
388           (guint) (outdata - headers));
389
390       /* second pass, read headers and write the data */
391       while (TRUE) {
392         guint offs, lin;
393
394         /* read length and cont */
395         length = (headers[0] << 8) | headers[1];
396         lin = ((headers[2] & 0x7f) << 8) | headers[3];
397         offs = ((headers[4] & 0x7f) << 8) | headers[5];
398         cont = headers[4] & 0x80;
399         pixels = length / pgroup;
400         headers += 6;
401
402         GST_LOG_OBJECT (payload,
403             "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
404             cont);
405
406         switch (GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo)) {
407           case GST_VIDEO_FORMAT_RGB:
408           case GST_VIDEO_FORMAT_RGBA:
409           case GST_VIDEO_FORMAT_BGR:
410           case GST_VIDEO_FORMAT_BGRA:
411           case GST_VIDEO_FORMAT_UYVY:
412             offs /= rtpvrawpay->xinc;
413             memcpy (outdata, yp + (lin * ystride) + (offs * pgroup), length);
414             outdata += length;
415             break;
416           case GST_VIDEO_FORMAT_AYUV:
417           {
418             gint i;
419             guint8 *datap;
420
421             datap = yp + (lin * ystride) + (offs * 4);
422
423             for (i = 0; i < pixels; i++) {
424               *outdata++ = datap[2];
425               *outdata++ = datap[1];
426               *outdata++ = datap[3];
427               datap += 4;
428             }
429             break;
430           }
431           case GST_VIDEO_FORMAT_I420:
432           {
433             gint i;
434             guint uvoff;
435             guint8 *yd1p, *yd2p, *udp, *vdp;
436
437             yd1p = yp + (lin * ystride) + (offs);
438             yd2p = yd1p + ystride;
439             uvoff =
440                 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
441             udp = up + uvoff;
442             vdp = vp + uvoff;
443
444             for (i = 0; i < pixels; i++) {
445               *outdata++ = *yd1p++;
446               *outdata++ = *yd1p++;
447               *outdata++ = *yd2p++;
448               *outdata++ = *yd2p++;
449               *outdata++ = *udp++;
450               *outdata++ = *vdp++;
451             }
452             break;
453           }
454           case GST_VIDEO_FORMAT_Y41B:
455           {
456             gint i;
457             guint uvoff;
458             guint8 *ydp, *udp, *vdp;
459
460             ydp = yp + (lin * ystride) + offs;
461             uvoff =
462                 (lin / rtpvrawpay->yinc * uvstride) + (offs / rtpvrawpay->xinc);
463             udp = up + uvoff;
464             vdp = vp + uvoff;
465
466             for (i = 0; i < pixels; i++) {
467               *outdata++ = *udp++;
468               *outdata++ = *ydp++;
469               *outdata++ = *ydp++;
470               *outdata++ = *vdp++;
471               *outdata++ = *ydp++;
472               *outdata++ = *ydp++;
473             }
474             break;
475           }
476           default:
477             gst_rtp_buffer_unmap (&rtp);
478             gst_buffer_unref (out);
479             goto unknown_sampling;
480         }
481
482         if (!cont)
483           break;
484       }
485
486       if (line >= height) {
487         GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
488         gst_rtp_buffer_set_marker (&rtp, TRUE);
489       }
490       gst_rtp_buffer_unmap (&rtp);
491       if (left > 0) {
492         GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
493         gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
494       }
495
496       /* push buffer */
497       ret = gst_basertppayload_push (payload, out);
498     }
499
500   }
501
502   gst_video_frame_unmap (&frame);
503   gst_buffer_unref (buffer);
504
505   return ret;
506
507   /* ERRORS */
508 unknown_sampling:
509   {
510     GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
511         (NULL), ("unimplemented sampling"));
512     gst_video_frame_unmap (&frame);
513     gst_buffer_unref (buffer);
514     return GST_FLOW_NOT_SUPPORTED;
515   }
516 }
517
518 gboolean
519 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
520 {
521   return gst_element_register (plugin, "rtpvrawpay",
522       GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);
523 }