rtpvrawpay: micro-optimise variable access in inner loop
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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, AYUV, UYVY, I420, Y41B, UYVP }, "
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 (GstRTPBasePayload * payload,
70     GstCaps * caps);
71 static GstFlowReturn gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload *
72     payload, GstBuffer * buffer);
73
74 G_DEFINE_TYPE (GstRtpVRawPay, gst_rtp_vraw_pay, GST_TYPE_RTP_BASE_PAYLOAD)
75
76      static void gst_rtp_vraw_pay_class_init (GstRtpVRawPayClass * klass)
77 {
78   GstRTPBasePayloadClass *gstrtpbasepayload_class;
79   GstElementClass *gstelement_class;
80
81   gstelement_class = (GstElementClass *) klass;
82   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
83
84   gstrtpbasepayload_class->set_caps = gst_rtp_vraw_pay_setcaps;
85   gstrtpbasepayload_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_static_metadata (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 (GstRTPBasePayload * 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       break;
156     case GST_VIDEO_FORMAT_BGR:
157       samplingstr = "BGR";
158       pgroup = 3;
159       break;
160     case GST_VIDEO_FORMAT_AYUV:
161       samplingstr = "YCbCr-4:4:4";
162       pgroup = 3;
163       break;
164     case GST_VIDEO_FORMAT_UYVY:
165       samplingstr = "YCbCr-4:2:2";
166       pgroup = 4;
167       xinc = 2;
168       break;
169     case GST_VIDEO_FORMAT_Y41B:
170       samplingstr = "YCbCr-4:1:1";
171       pgroup = 6;
172       xinc = 4;
173       break;
174     case GST_VIDEO_FORMAT_I420:
175       samplingstr = "YCbCr-4:2:0";
176       pgroup = 6;
177       xinc = yinc = 2;
178       break;
179     case GST_VIDEO_FORMAT_UYVP:
180       samplingstr = "YCbCr-4:2:2";
181       pgroup = 5;
182       xinc = 2;
183       depth = 10;
184       depthstr = "10";
185       break;
186     default:
187       goto unknown_format;
188       break;
189   }
190
191   if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
192     yinc *= 2;
193   }
194
195   rtpvrawpay->pgroup = pgroup;
196   rtpvrawpay->xinc = xinc;
197   rtpvrawpay->yinc = yinc;
198   rtpvrawpay->depth = depth;
199
200   GST_DEBUG_OBJECT (payload, "width %d, height %d, sampling %s",
201       GST_VIDEO_INFO_WIDTH (&info), GST_VIDEO_INFO_HEIGHT (&info), samplingstr);
202   GST_DEBUG_OBJECT (payload, "xinc %d, yinc %d, pgroup %d", xinc, yinc, pgroup);
203
204   wstr = g_strdup_printf ("%d", GST_VIDEO_INFO_WIDTH (&info));
205   hstr = g_strdup_printf ("%d", GST_VIDEO_INFO_HEIGHT (&info));
206
207   gst_rtp_base_payload_set_options (payload, "video", TRUE, "RAW", 90000);
208   if (GST_VIDEO_INFO_IS_INTERLACED (&info)) {
209     res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
210         samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
211         wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
212         colorimetrystr, "interlace", G_TYPE_STRING, "true", NULL);
213   } else {
214     res = gst_rtp_base_payload_set_outcaps (payload, "sampling", G_TYPE_STRING,
215         samplingstr, "depth", G_TYPE_STRING, depthstr, "width", G_TYPE_STRING,
216         wstr, "height", G_TYPE_STRING, hstr, "colorimetry", G_TYPE_STRING,
217         colorimetrystr, NULL);
218   }
219   g_free (wstr);
220   g_free (hstr);
221
222   return res;
223
224   /* ERRORS */
225 invalid_caps:
226   {
227     GST_ERROR_OBJECT (payload, "could not parse caps");
228     return FALSE;
229   }
230 unknown_format:
231   {
232     GST_ERROR_OBJECT (payload, "unknown caps format");
233     return FALSE;
234   }
235 }
236
237 static GstFlowReturn
238 gst_rtp_vraw_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
239 {
240   GstRtpVRawPay *rtpvrawpay;
241   GstFlowReturn ret = GST_FLOW_OK;
242   guint lines_delay;            /* after how many packed lines we push out a buffer list */
243   guint last_line;              /* last pack line number we pushed out a buffer list     */
244   guint line, offset;
245   guint8 *p0, *yp, *up, *vp;
246   guint ystride, uvstride;
247   guint xinc, yinc;
248   guint pgroup;
249   guint mtu;
250   guint width, height;
251   gint field, fields;
252   GstVideoFormat format;
253   GstVideoFrame frame;
254   gint interlaced;
255   GstBufferList *list;
256   GstRTPBuffer rtp = { NULL, };
257
258   rtpvrawpay = GST_RTP_VRAW_PAY (payload);
259
260   gst_video_frame_map (&frame, &rtpvrawpay->vinfo, buffer, GST_MAP_READ);
261
262   GST_LOG_OBJECT (rtpvrawpay, "new frame of %" G_GSIZE_FORMAT " bytes",
263       gst_buffer_get_size (buffer));
264
265   /* get pointer and strides of the planes */
266   p0 = GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
267   yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
268   up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
269   vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
270
271   ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
272   uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
273
274   mtu = GST_RTP_BASE_PAYLOAD_MTU (payload);
275
276   /* amount of bytes for one pixel */
277   pgroup = rtpvrawpay->pgroup;
278   width = GST_VIDEO_INFO_WIDTH (&rtpvrawpay->vinfo);
279   height = GST_VIDEO_INFO_HEIGHT (&rtpvrawpay->vinfo);
280
281   interlaced = GST_VIDEO_INFO_IS_INTERLACED (&rtpvrawpay->vinfo);
282
283   format = GST_VIDEO_INFO_FORMAT (&rtpvrawpay->vinfo);
284
285   yinc = rtpvrawpay->yinc;
286   xinc = rtpvrawpay->xinc;
287
288   /* after how many packed lines we push out a buffer list */
289   lines_delay = GST_ROUND_UP_4 (height / 10);
290
291   fields = 1 + interlaced;
292
293   /* start with line 0, offset 0 */
294   for (field = 0; field < fields; field++) {
295     line = field;
296     offset = 0;
297     last_line = 0;
298
299     list = gst_buffer_list_new ();
300
301     /* write all lines */
302     while (line < height) {
303       guint left, pack_line;
304       GstBuffer *out;
305       guint8 *outdata, *headers;
306       gboolean next_line, complete = FALSE;
307       guint length, cont, pixels;
308
309       /* get the max allowed payload length size, we try to fill the complete MTU */
310       left = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
311       out = gst_rtp_buffer_new_allocate (left, 0, 0);
312
313       if (field == 0) {
314         GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer);
315       } else {
316         GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (buffer) +
317             GST_BUFFER_DURATION (buffer) / 2;
318       }
319
320       gst_rtp_buffer_map (out, GST_MAP_WRITE, &rtp);
321       outdata = gst_rtp_buffer_get_payload (&rtp);
322
323       GST_LOG_OBJECT (rtpvrawpay, "created buffer of size %u for MTU %u", left,
324           mtu);
325
326       /*
327        *   0                   1                   2                   3
328        *   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
329        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
330        *  |   Extended Sequence Number    |            Length             |
331        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
332        *  |F|          Line No            |C|           Offset            |
333        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
334        *  |            Length             |F|          Line No            |
335        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
336        *  |C|           Offset            |                               .
337        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               .
338        *  .                                                               .
339        *  .                 Two (partial) lines of video data             .
340        *  .                                                               .
341        *  +---------------------------------------------------------------+
342        */
343
344       /* need 2 bytes for the extended sequence number */
345       *outdata++ = 0;
346       *outdata++ = 0;
347       left -= 2;
348
349       /* the headers start here */
350       headers = outdata;
351
352       /* make sure we can fit at least *one* header and pixel */
353       if (!(left > (6 + pgroup))) {
354         gst_rtp_buffer_unmap (&rtp);
355         gst_buffer_unref (out);
356         goto too_small;
357       }
358
359       /* while we can fit at least one header and one pixel */
360       while (left > (6 + pgroup)) {
361         /* we need a 6 bytes header */
362         left -= 6;
363
364         /* get how may bytes we need for the remaining pixels */
365         pixels = width - offset;
366         length = (pixels * pgroup) / xinc;
367
368         if (left >= length) {
369           /* pixels and header fit completely, we will write them and skip to the
370            * next line. */
371           next_line = TRUE;
372         } else {
373           /* line does not fit completely, see how many pixels fit */
374           pixels = (left / pgroup) * xinc;
375           length = (pixels * pgroup) / xinc;
376           next_line = FALSE;
377         }
378         GST_LOG_OBJECT (rtpvrawpay, "filling %u bytes in %u pixels", length,
379             pixels);
380         left -= length;
381
382         /* write length */
383         *outdata++ = (length >> 8) & 0xff;
384         *outdata++ = length & 0xff;
385
386         /* write line no */
387         *outdata++ = ((line >> 8) & 0x7f) | ((field << 7) & 0x80);
388         *outdata++ = line & 0xff;
389
390         if (next_line) {
391           /* go to next line we do this here to make the check below easier */
392           line += yinc;
393         }
394
395         /* calculate continuation marker */
396         cont = (left > (6 + pgroup) && line < height) ? 0x80 : 0x00;
397
398         /* write offset and continuation marker */
399         *outdata++ = ((offset >> 8) & 0x7f) | cont;
400         *outdata++ = offset & 0xff;
401
402         if (next_line) {
403           /* reset offset */
404           offset = 0;
405           GST_LOG_OBJECT (rtpvrawpay, "go to next line %u", line);
406         } else {
407           offset += pixels;
408           GST_LOG_OBJECT (rtpvrawpay, "next offset %u", offset);
409         }
410
411         if (!cont)
412           break;
413       }
414       GST_LOG_OBJECT (rtpvrawpay, "consumed %u bytes",
415           (guint) (outdata - headers));
416
417       /* second pass, read headers and write the data */
418       while (TRUE) {
419         guint offs, lin;
420
421         /* read length and cont */
422         length = (headers[0] << 8) | headers[1];
423         lin = ((headers[2] & 0x7f) << 8) | headers[3];
424         offs = ((headers[4] & 0x7f) << 8) | headers[5];
425         cont = headers[4] & 0x80;
426         pixels = length / pgroup;
427         headers += 6;
428
429         GST_LOG_OBJECT (payload,
430             "writing length %u, line %u, offset %u, cont %d", length, lin, offs,
431             cont);
432
433         switch (format) {
434           case GST_VIDEO_FORMAT_RGB:
435           case GST_VIDEO_FORMAT_RGBA:
436           case GST_VIDEO_FORMAT_BGR:
437           case GST_VIDEO_FORMAT_BGRA:
438           case GST_VIDEO_FORMAT_UYVY:
439           case GST_VIDEO_FORMAT_UYVP:
440             offs /= xinc;
441             memcpy (outdata, p0 + (lin * ystride) + (offs * pgroup), length);
442             outdata += length;
443             break;
444           case GST_VIDEO_FORMAT_AYUV:
445           {
446             gint i;
447             guint8 *datap;
448
449             datap = p0 + (lin * ystride) + (offs * 4);
450
451             for (i = 0; i < pixels; i++) {
452               *outdata++ = datap[2];
453               *outdata++ = datap[1];
454               *outdata++ = datap[3];
455               datap += 4;
456             }
457             break;
458           }
459           case GST_VIDEO_FORMAT_I420:
460           {
461             gint i;
462             guint uvoff;
463             guint8 *yd1p, *yd2p, *udp, *vdp;
464
465             yd1p = yp + (lin * ystride) + (offs);
466             yd2p = yd1p + ystride;
467             uvoff = (lin / yinc * uvstride) + (offs / xinc);
468             udp = up + uvoff;
469             vdp = vp + uvoff;
470
471             for (i = 0; i < pixels; i++) {
472               *outdata++ = *yd1p++;
473               *outdata++ = *yd1p++;
474               *outdata++ = *yd2p++;
475               *outdata++ = *yd2p++;
476               *outdata++ = *udp++;
477               *outdata++ = *vdp++;
478             }
479             break;
480           }
481           case GST_VIDEO_FORMAT_Y41B:
482           {
483             gint i;
484             guint uvoff;
485             guint8 *ydp, *udp, *vdp;
486
487             ydp = yp + (lin * ystride) + offs;
488             uvoff = (lin / yinc * uvstride) + (offs / xinc);
489             udp = up + uvoff;
490             vdp = vp + uvoff;
491
492             for (i = 0; i < pixels; i++) {
493               *outdata++ = *udp++;
494               *outdata++ = *ydp++;
495               *outdata++ = *ydp++;
496               *outdata++ = *vdp++;
497               *outdata++ = *ydp++;
498               *outdata++ = *ydp++;
499             }
500             break;
501           }
502           default:
503             gst_rtp_buffer_unmap (&rtp);
504             gst_buffer_unref (out);
505             goto unknown_sampling;
506         }
507
508         if (!cont)
509           break;
510       }
511
512       if (line >= height) {
513         GST_LOG_OBJECT (rtpvrawpay, "field/frame complete, set marker");
514         gst_rtp_buffer_set_marker (&rtp, TRUE);
515         complete = TRUE;
516       }
517       gst_rtp_buffer_unmap (&rtp);
518       if (left > 0) {
519         GST_LOG_OBJECT (rtpvrawpay, "we have %u bytes left", left);
520         gst_buffer_resize (out, 0, gst_buffer_get_size (out) - left);
521       }
522
523       gst_buffer_list_add (list, out);
524
525       pack_line = (line - field) / fields;
526       if (complete || (pack_line > last_line && pack_line % lines_delay == 0)) {
527         /* push buffers */
528         GST_LOG_OBJECT (rtpvrawpay, "pushing list of %u buffers up to pack "
529             "line %u", gst_buffer_list_length (list), pack_line);
530         ret = gst_rtp_base_payload_push_list (payload, list);
531         list = NULL;
532         if (!complete)
533           list = gst_buffer_list_new ();
534         last_line = pack_line;
535       }
536     }
537
538   }
539
540   gst_video_frame_unmap (&frame);
541   gst_buffer_unref (buffer);
542
543   return ret;
544
545   /* ERRORS */
546 unknown_sampling:
547   {
548     GST_ELEMENT_ERROR (payload, STREAM, FORMAT,
549         (NULL), ("unimplemented sampling"));
550     gst_video_frame_unmap (&frame);
551     gst_buffer_unref (buffer);
552     return GST_FLOW_NOT_SUPPORTED;
553   }
554 too_small:
555   {
556     GST_ELEMENT_ERROR (payload, RESOURCE, NO_SPACE_LEFT,
557         (NULL), ("not enough space to send at least one pixel"));
558     gst_video_frame_unmap (&frame);
559     gst_buffer_unref (buffer);
560     return GST_FLOW_NOT_SUPPORTED;
561   }
562 }
563
564 gboolean
565 gst_rtp_vraw_pay_plugin_init (GstPlugin * plugin)
566 {
567   return gst_element_register (plugin, "rtpvrawpay",
568       GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_PAY);
569 }