Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpvrawdepay.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 <gst/rtp/gstrtpbuffer.h>
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include "gstrtpvrawdepay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpvrawdepay_debug);
31 #define GST_CAT_DEFAULT (rtpvrawdepay_debug)
32
33 static GstStaticPadTemplate gst_rtp_vraw_depay_src_template =
34     GST_STATIC_PAD_TEMPLATE ("src",
35     GST_PAD_SRC,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("video/x-raw-rgb; video/x-raw-yuv")
38     );
39
40 static GstStaticPadTemplate gst_rtp_vraw_depay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("application/x-rtp, "
45         "media = (string) \"video\", "
46         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
47         "clock-rate = (int) 90000, " "encoding-name = (string) \"RAW\"")
48     );
49
50 GST_BOILERPLATE (GstRtpVRawDepay, gst_rtp_vraw_depay, GstBaseRTPDepayload,
51     GST_TYPE_BASE_RTP_DEPAYLOAD);
52
53 static gboolean gst_rtp_vraw_depay_setcaps (GstBaseRTPDepayload * depayload,
54     GstCaps * caps);
55 static GstBuffer *gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload,
56     GstBuffer * buf);
57
58 static GstStateChangeReturn gst_rtp_vraw_depay_change_state (GstElement *
59     element, GstStateChange transition);
60
61 static gboolean gst_rtp_vraw_depay_handle_event (GstBaseRTPDepayload * filter,
62     GstEvent * event);
63
64 static void
65 gst_rtp_vraw_depay_base_init (gpointer klass)
66 {
67   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
68
69   gst_element_class_add_static_pad_template (element_class,
70       &gst_rtp_vraw_depay_src_template);
71   gst_element_class_add_static_pad_template (element_class,
72       &gst_rtp_vraw_depay_sink_template);
73
74   gst_element_class_set_details_simple (element_class,
75       "RTP Raw Video depayloader", "Codec/Depayloader/Network/RTP",
76       "Extracts raw video from RTP packets (RFC 4175)",
77       "Wim Taymans <wim.taymans@gmail.com>");
78 }
79
80 static void
81 gst_rtp_vraw_depay_class_init (GstRtpVRawDepayClass * klass)
82 {
83   GstElementClass *gstelement_class;
84   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
85
86   gstelement_class = (GstElementClass *) klass;
87   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
88
89   gstelement_class->change_state = gst_rtp_vraw_depay_change_state;
90
91   gstbasertpdepayload_class->set_caps = gst_rtp_vraw_depay_setcaps;
92   gstbasertpdepayload_class->process = gst_rtp_vraw_depay_process;
93   gstbasertpdepayload_class->handle_event = gst_rtp_vraw_depay_handle_event;
94
95   GST_DEBUG_CATEGORY_INIT (rtpvrawdepay_debug, "rtpvrawdepay", 0,
96       "raw video RTP Depayloader");
97 }
98
99 static void
100 gst_rtp_vraw_depay_init (GstRtpVRawDepay * rtpvrawdepay,
101     GstRtpVRawDepayClass * klass)
102 {
103   /* needed because of GST_BOILERPLATE */
104 }
105
106 static void
107 gst_rtp_vraw_depay_reset (GstRtpVRawDepay * rtpvrawdepay)
108 {
109   if (rtpvrawdepay->outbuf) {
110     gst_buffer_unref (rtpvrawdepay->outbuf);
111     rtpvrawdepay->outbuf = NULL;
112   }
113   rtpvrawdepay->timestamp = -1;
114 }
115
116 static gboolean
117 gst_rtp_vraw_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
118 {
119   GstStructure *structure;
120   GstRtpVRawDepay *rtpvrawdepay;
121   gint clock_rate;
122   const gchar *str, *type;
123   gint format, width, height, pgroup, xinc, yinc;
124   guint ystride, uvstride, yp, up, vp, outsize;
125   GstCaps *srccaps;
126   guint32 fourcc = 0;
127   gboolean res;
128   gint rmask = 0, gmask = 0, bmask = 0, amask = 0, bpp = 0, depth = 0;
129
130   rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
131
132   structure = gst_caps_get_structure (caps, 0);
133
134   yp = up = vp = uvstride = 0;
135   xinc = yinc = 1;
136
137   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
138     clock_rate = 90000;         /* default */
139   depayload->clock_rate = clock_rate;
140
141   if (!(str = gst_structure_get_string (structure, "width")))
142     goto no_width;
143   width = atoi (str);
144
145   if (!(str = gst_structure_get_string (structure, "height")))
146     goto no_height;
147   height = atoi (str);
148
149   /* optional interlace value but we don't handle interlaced
150    * formats yet */
151   if (gst_structure_get_string (structure, "interlace"))
152     goto interlaced;
153
154   if (!(str = gst_structure_get_string (structure, "sampling")))
155     goto no_sampling;
156
157   if (!strcmp (str, "RGB")) {
158     format = GST_VIDEO_FORMAT_RGB;
159     pgroup = 3;
160     ystride = GST_ROUND_UP_4 (width * 3);
161     outsize = ystride * height;
162     type = "video/x-raw-rgb";
163     rmask = 0x00ff0000;
164     gmask = 0x0000ff00;
165     bmask = 0x000000ff;
166     depth = 24;
167     bpp = 24;
168   } else if (!strcmp (str, "RGBA")) {
169     format = GST_VIDEO_FORMAT_RGBA;
170     pgroup = 4;
171     ystride = width * 4;
172     outsize = ystride * height;
173     type = "video/x-raw-rgb";
174     rmask = 0xff000000;
175     gmask = 0x00ff0000;
176     bmask = 0x0000ff00;
177     amask = 0x000000ff;
178     depth = 32;
179     bpp = 32;
180   } else if (!strcmp (str, "BGR")) {
181     format = GST_VIDEO_FORMAT_BGR;
182     pgroup = 3;
183     ystride = GST_ROUND_UP_4 (width * 3);
184     outsize = ystride * height;
185     type = "video/x-raw-rgb";
186     rmask = 0x000000ff;
187     gmask = 0x0000ff00;
188     bmask = 0x00ff0000;
189     depth = 24;
190     bpp = 24;
191   } else if (!strcmp (str, "BGRA")) {
192     format = GST_VIDEO_FORMAT_BGRA;
193     pgroup = 4;
194     ystride = width * 4;
195     outsize = ystride * height;
196     type = "video/x-raw-rgb";
197     rmask = 0x0000ff00;
198     gmask = 0x00ff0000;
199     bmask = 0xff000000;
200     amask = 0x000000ff;
201     depth = 32;
202     bpp = 32;
203   } else if (!strcmp (str, "YCbCr-4:4:4")) {
204     format = GST_VIDEO_FORMAT_AYUV;
205     pgroup = 3;
206     ystride = width * 4;
207     outsize = ystride * height;
208     type = "video/x-raw-yuv";
209     fourcc = GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
210   } else if (!strcmp (str, "YCbCr-4:2:2")) {
211     format = GST_VIDEO_FORMAT_UYVY;
212     pgroup = 4;
213     ystride = GST_ROUND_UP_2 (width) * 2;
214     outsize = ystride * height;
215     type = "video/x-raw-yuv";
216     fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
217     xinc = 2;
218   } else if (!strcmp (str, "YCbCr-4:2:0")) {
219     format = GST_VIDEO_FORMAT_I420;
220     pgroup = 6;
221     ystride = GST_ROUND_UP_4 (width);
222     uvstride = GST_ROUND_UP_8 (width) / 2;
223     up = ystride * GST_ROUND_UP_2 (height);
224     vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
225     outsize = vp + uvstride * GST_ROUND_UP_2 (height) / 2;
226     type = "video/x-raw-yuv";
227     fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
228     xinc = yinc = 2;
229   } else if (!strcmp (str, "YCbCr-4:1:1")) {
230     format = GST_VIDEO_FORMAT_Y41B;
231     pgroup = 6;
232     ystride = GST_ROUND_UP_4 (width);
233     uvstride = GST_ROUND_UP_8 (width) / 4;
234     up = ystride * height;
235     vp = up + uvstride * height;
236     outsize = vp + uvstride * height;
237     type = "video/x-raw-yuv";
238     fourcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
239     xinc = 4;
240   } else
241     goto unknown_format;
242
243   rtpvrawdepay->width = width;
244   rtpvrawdepay->height = height;
245   rtpvrawdepay->format = format;
246   rtpvrawdepay->yp = yp;
247   rtpvrawdepay->up = up;
248   rtpvrawdepay->vp = vp;
249   rtpvrawdepay->pgroup = pgroup;
250   rtpvrawdepay->xinc = xinc;
251   rtpvrawdepay->yinc = yinc;
252   rtpvrawdepay->ystride = ystride;
253   rtpvrawdepay->uvstride = uvstride;
254   rtpvrawdepay->outsize = outsize;
255
256   srccaps = gst_caps_new_simple (type,
257       "width", G_TYPE_INT, width,
258       "height", G_TYPE_INT, height,
259       "format", GST_TYPE_FOURCC, fourcc,
260       "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
261
262   if (!strcmp (type, "video/x-raw-rgb")) {
263     gst_caps_set_simple (srccaps,
264         "endianness", G_TYPE_INT, G_BIG_ENDIAN,
265         "red_mask", G_TYPE_INT, rmask,
266         "green_mask", G_TYPE_INT, gmask,
267         "blue_mask", G_TYPE_INT, bmask,
268         "bpp", G_TYPE_INT, bpp, "depth", G_TYPE_INT, depth, NULL);
269
270     if (amask > 0) {
271       gst_caps_set_simple (srccaps, "alpha_mask", G_TYPE_INT, amask, NULL);
272     }
273   }
274
275   res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
276   gst_caps_unref (srccaps);
277
278   GST_DEBUG_OBJECT (depayload, "width %d, height %d, format %d", width, height,
279       format);
280   GST_DEBUG_OBJECT (depayload, "yp %d, up %d, vp %d", yp, up, vp);
281   GST_DEBUG_OBJECT (depayload, "xinc %d, yinc %d", xinc, yinc);
282   GST_DEBUG_OBJECT (depayload, "pgroup %d, ystride %d, uvstride %d", pgroup,
283       ystride, uvstride);
284   GST_DEBUG_OBJECT (depayload, "outsize %u", outsize);
285
286   return res;
287
288   /* ERRORS */
289 no_width:
290   {
291     GST_ERROR_OBJECT (depayload, "no width specified");
292     return FALSE;
293   }
294 no_height:
295   {
296     GST_ERROR_OBJECT (depayload, "no height specified");
297     return FALSE;
298   }
299 interlaced:
300   {
301     GST_ERROR_OBJECT (depayload, "interlaced formats not supported yet");
302     return FALSE;
303   }
304 no_sampling:
305   {
306     GST_ERROR_OBJECT (depayload, "no sampling specified");
307     return FALSE;
308   }
309 unknown_format:
310   {
311     GST_ERROR_OBJECT (depayload, "unknown sampling format '%s'", str);
312     return FALSE;
313   }
314 }
315
316 static GstBuffer *
317 gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
318 {
319   GstRtpVRawDepay *rtpvrawdepay;
320   guint8 *payload, *data, *yp, *up, *vp, *headers;
321   guint32 timestamp;
322   guint cont, ystride, uvstride, pgroup, payload_len;
323   gint width, height, xinc, yinc;
324
325   rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
326
327   timestamp = gst_rtp_buffer_get_timestamp (buf);
328
329   if (timestamp != rtpvrawdepay->timestamp || rtpvrawdepay->outbuf == NULL) {
330     GstBuffer *outbuf;
331     GstFlowReturn ret;
332
333     GST_LOG_OBJECT (depayload, "new frame with timestamp %u", timestamp);
334     /* new timestamp, flush old buffer and create new output buffer */
335     if (rtpvrawdepay->outbuf) {
336       gst_base_rtp_depayload_push_ts (depayload, rtpvrawdepay->timestamp,
337           rtpvrawdepay->outbuf);
338       rtpvrawdepay->outbuf = NULL;
339     }
340
341     ret = gst_pad_alloc_buffer (depayload->srcpad, -1, rtpvrawdepay->outsize,
342         GST_PAD_CAPS (depayload->srcpad), &outbuf);
343     if (ret != GST_FLOW_OK)
344       goto alloc_failed;
345
346     /* clear timestamp from alloc... */
347     GST_BUFFER_TIMESTAMP (outbuf) = -1;
348
349     rtpvrawdepay->outbuf = outbuf;
350     rtpvrawdepay->timestamp = timestamp;
351   }
352
353   data = GST_BUFFER_DATA (rtpvrawdepay->outbuf);
354
355   /* get pointer and strides of the planes */
356   yp = data + rtpvrawdepay->yp;
357   up = data + rtpvrawdepay->up;
358   vp = data + rtpvrawdepay->vp;
359
360   ystride = rtpvrawdepay->ystride;
361   uvstride = rtpvrawdepay->uvstride;
362   pgroup = rtpvrawdepay->pgroup;
363   width = rtpvrawdepay->width;
364   height = rtpvrawdepay->height;
365   xinc = rtpvrawdepay->xinc;
366   yinc = rtpvrawdepay->yinc;
367
368   payload = gst_rtp_buffer_get_payload (buf);
369   payload_len = gst_rtp_buffer_get_payload_len (buf);
370
371   if (payload_len < 3)
372     goto short_packet;
373
374   /* skip extended seqnum */
375   payload += 2;
376   payload_len -= 2;
377
378   /* remember header position */
379   headers = payload;
380
381   /* find data start */
382   do {
383     if (payload_len < 6)
384       goto short_packet;
385
386     cont = payload[4] & 0x80;
387
388     payload += 6;
389     payload_len -= 6;
390   } while (cont);
391
392   while (TRUE) {
393     guint length, line, offs, plen;
394     guint8 *datap;
395
396     /* stop when we run out of data */
397     if (payload_len == 0)
398       break;
399
400     /* read length and cont. This should work because we iterated the headers
401      * above. */
402     length = (headers[0] << 8) | headers[1];
403     line = ((headers[2] & 0x7f) << 8) | headers[3];
404     offs = ((headers[4] & 0x7f) << 8) | headers[5];
405     cont = headers[4] & 0x80;
406     headers += 6;
407
408     /* length must be a multiple of pgroup */
409     if (length % pgroup != 0)
410       goto wrong_length;
411
412     if (length > payload_len)
413       length = payload_len;
414
415     /* sanity check */
416     if (line > (height - yinc)) {
417       GST_WARNING_OBJECT (depayload, "skipping line %d: out of range", line);
418       goto next;
419     }
420     if (offs > (width - xinc)) {
421       GST_WARNING_OBJECT (depayload, "skipping offset %d: out of range", offs);
422       goto next;
423     }
424
425     /* calculate the maximim amount of bytes we can use per line */
426     if (offs + ((length / pgroup) * xinc) > width) {
427       plen = ((width - offs) * pgroup) / xinc;
428       GST_WARNING_OBJECT (depayload, "clipping length %d, offset %d, plen %d",
429           length, offs, plen);
430     } else
431       plen = length;
432
433     GST_LOG_OBJECT (depayload,
434         "writing length %u/%u, line %u, offset %u, remaining %u", plen, length,
435         line, offs, payload_len);
436
437     switch (rtpvrawdepay->format) {
438       case GST_VIDEO_FORMAT_RGB:
439       case GST_VIDEO_FORMAT_RGBA:
440       case GST_VIDEO_FORMAT_BGR:
441       case GST_VIDEO_FORMAT_BGRA:
442       case GST_VIDEO_FORMAT_UYVY:
443         /* samples are packed just like gstreamer packs them */
444         offs /= xinc;
445         datap = yp + (line * ystride) + (offs * pgroup);
446
447         memcpy (datap, payload, plen);
448         break;
449       case GST_VIDEO_FORMAT_AYUV:
450       {
451         gint i;
452         guint8 *p;
453
454         datap = yp + (line * ystride) + (offs * 4);
455         p = payload;
456
457         /* samples are packed in order Cb-Y-Cr for both interlaced and
458          * progressive frames */
459         for (i = 0; i < plen; i += pgroup) {
460           *datap++ = 0;
461           *datap++ = p[1];
462           *datap++ = p[0];
463           *datap++ = p[2];
464           p += pgroup;
465         }
466         break;
467       }
468       case GST_VIDEO_FORMAT_I420:
469       {
470         gint i;
471         guint uvoff;
472         guint8 *yd1p, *yd2p, *udp, *vdp, *p;
473
474         yd1p = yp + (line * ystride) + (offs);
475         yd2p = yd1p + ystride;
476         uvoff = (line / yinc * uvstride) + (offs / xinc);
477
478         udp = up + uvoff;
479         vdp = vp + uvoff;
480         p = payload;
481
482         /* line 0/1: Y00-Y01-Y10-Y11-Cb00-Cr00 Y02-Y03-Y12-Y13-Cb01-Cr01 ...  */
483         for (i = 0; i < plen; i += pgroup) {
484           *yd1p++ = p[0];
485           *yd1p++ = p[1];
486           *yd2p++ = p[2];
487           *yd2p++ = p[3];
488           *udp++ = p[4];
489           *vdp++ = p[5];
490           p += pgroup;
491         }
492         break;
493       }
494       case GST_VIDEO_FORMAT_Y41B:
495       {
496         gint i;
497         guint uvoff;
498         guint8 *ydp, *udp, *vdp, *p;
499
500         ydp = yp + (line * ystride) + (offs);
501         uvoff = (line / yinc * uvstride) + (offs / xinc);
502
503         udp = up + uvoff;
504         vdp = vp + uvoff;
505         p = payload;
506
507         /* Samples are packed in order Cb0-Y0-Y1-Cr0-Y2-Y3 for both interlaced
508          * and progressive scan lines */
509         for (i = 0; i < plen; i += pgroup) {
510           *udp++ = p[0];
511           *ydp++ = p[1];
512           *ydp++ = p[2];
513           *vdp++ = p[3];
514           *ydp++ = p[4];
515           *ydp++ = p[5];
516           p += pgroup;
517         }
518         break;
519       }
520       default:
521         goto unknown_sampling;
522     }
523
524   next:
525     if (!cont)
526       break;
527
528     payload += length;
529     payload_len -= length;
530   }
531
532   if (gst_rtp_buffer_get_marker (buf)) {
533     GST_LOG_OBJECT (depayload, "marker, flushing frame");
534     if (rtpvrawdepay->outbuf) {
535       gst_base_rtp_depayload_push_ts (depayload, timestamp,
536           rtpvrawdepay->outbuf);
537       rtpvrawdepay->outbuf = NULL;
538     }
539     rtpvrawdepay->timestamp = -1;
540   }
541   return NULL;
542
543   /* ERRORS */
544 unknown_sampling:
545   {
546     GST_ELEMENT_ERROR (depayload, STREAM, FORMAT,
547         (NULL), ("unimplemented sampling"));
548     return NULL;
549   }
550 alloc_failed:
551   {
552     GST_WARNING_OBJECT (depayload, "failed to alloc output buffer");
553     return NULL;
554   }
555 wrong_length:
556   {
557     GST_WARNING_OBJECT (depayload, "length not multiple of pgroup");
558     return NULL;
559   }
560 short_packet:
561   {
562     GST_WARNING_OBJECT (depayload, "short packet");
563     return NULL;
564   }
565 }
566
567 static gboolean
568 gst_rtp_vraw_depay_handle_event (GstBaseRTPDepayload * filter, GstEvent * event)
569 {
570   gboolean ret;
571   GstRtpVRawDepay *rtpvrawdepay;
572
573   rtpvrawdepay = GST_RTP_VRAW_DEPAY (filter);
574
575   switch (GST_EVENT_TYPE (event)) {
576     case GST_EVENT_FLUSH_STOP:
577       gst_rtp_vraw_depay_reset (rtpvrawdepay);
578       break;
579     default:
580       break;
581   }
582
583   ret =
584       GST_BASE_RTP_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
585
586   return ret;
587 }
588
589 static GstStateChangeReturn
590 gst_rtp_vraw_depay_change_state (GstElement * element,
591     GstStateChange transition)
592 {
593   GstRtpVRawDepay *rtpvrawdepay;
594   GstStateChangeReturn ret;
595
596   rtpvrawdepay = GST_RTP_VRAW_DEPAY (element);
597
598   switch (transition) {
599     case GST_STATE_CHANGE_NULL_TO_READY:
600       break;
601     case GST_STATE_CHANGE_READY_TO_PAUSED:
602       gst_rtp_vraw_depay_reset (rtpvrawdepay);
603       break;
604     default:
605       break;
606   }
607
608   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
609
610   switch (transition) {
611     case GST_STATE_CHANGE_PAUSED_TO_READY:
612       gst_rtp_vraw_depay_reset (rtpvrawdepay);
613       break;
614     case GST_STATE_CHANGE_READY_TO_NULL:
615       break;
616     default:
617       break;
618   }
619   return ret;
620 }
621
622 gboolean
623 gst_rtp_vraw_depay_plugin_init (GstPlugin * plugin)
624 {
625   return gst_element_register (plugin, "rtpvrawdepay",
626       GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_DEPAY);
627 }