upload tizen1.0 source
[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_pad_template (element_class,
70       gst_static_pad_template_get (&gst_rtp_vraw_depay_src_template));
71   gst_element_class_add_pad_template (element_class,
72       gst_static_pad_template_get (&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
129   rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
130
131   structure = gst_caps_get_structure (caps, 0);
132
133   yp = up = vp = uvstride = 0;
134   xinc = yinc = 1;
135
136   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
137     clock_rate = 90000;         /* default */
138   depayload->clock_rate = clock_rate;
139
140   if (!(str = gst_structure_get_string (structure, "width")))
141     goto no_width;
142   width = atoi (str);
143
144   if (!(str = gst_structure_get_string (structure, "height")))
145     goto no_height;
146   height = atoi (str);
147
148   /* optional interlace value but we don't handle interlaced
149    * formats yet */
150   if (gst_structure_get_string (structure, "interlace"))
151     goto interlaced;
152
153   if (!(str = gst_structure_get_string (structure, "sampling")))
154     goto no_sampling;
155
156   if (!strcmp (str, "RGB")) {
157     format = GST_VIDEO_FORMAT_RGB;
158     pgroup = 3;
159     ystride = GST_ROUND_UP_4 (width * 3);
160     outsize = ystride * height;
161     type = "video/x-raw-rgb";
162   } else if (!strcmp (str, "RGBA")) {
163     format = GST_VIDEO_FORMAT_RGBA;
164     pgroup = 4;
165     ystride = width * 4;
166     outsize = ystride * height;
167     type = "video/x-raw-rgb";
168   } else if (!strcmp (str, "BGR")) {
169     format = GST_VIDEO_FORMAT_BGR;
170     pgroup = 3;
171     ystride = GST_ROUND_UP_4 (width * 3);
172     outsize = ystride * height;
173     type = "video/x-raw-rgb";
174   } else if (!strcmp (str, "BGRA")) {
175     format = GST_VIDEO_FORMAT_BGRA;
176     pgroup = 4;
177     ystride = width * 4;
178     outsize = ystride * height;
179     type = "video/x-raw-rgb";
180   } else if (!strcmp (str, "YCbCr-4:4:4")) {
181     format = GST_VIDEO_FORMAT_AYUV;
182     pgroup = 3;
183     ystride = width * 4;
184     outsize = ystride * height;
185     type = "video/x-raw-yuv";
186     fourcc = GST_MAKE_FOURCC ('A', 'Y', 'U', 'V');
187   } else if (!strcmp (str, "YCbCr-4:2:2")) {
188     format = GST_VIDEO_FORMAT_UYVY;
189     pgroup = 4;
190     ystride = GST_ROUND_UP_2 (width) * 2;
191     outsize = ystride * height;
192     type = "video/x-raw-yuv";
193     fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
194     xinc = 2;
195   } else if (!strcmp (str, "YCbCr-4:2:0")) {
196     format = GST_VIDEO_FORMAT_I420;
197     pgroup = 6;
198     ystride = GST_ROUND_UP_4 (width);
199     uvstride = GST_ROUND_UP_8 (width) / 2;
200     up = ystride * GST_ROUND_UP_2 (height);
201     vp = up + uvstride * GST_ROUND_UP_2 (height) / 2;
202     outsize = vp + uvstride * GST_ROUND_UP_2 (height) / 2;
203     type = "video/x-raw-yuv";
204     fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
205     xinc = yinc = 2;
206   } else if (!strcmp (str, "YCbCr-4:1:1")) {
207     format = GST_VIDEO_FORMAT_Y41B;
208     pgroup = 6;
209     ystride = GST_ROUND_UP_4 (width);
210     uvstride = GST_ROUND_UP_8 (width) / 4;
211     up = ystride * height;
212     vp = up + uvstride * height;
213     outsize = vp + uvstride * height;
214     type = "video/x-raw-yuv";
215     fourcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
216     xinc = 4;
217   } else
218     goto unknown_format;
219
220   rtpvrawdepay->width = width;
221   rtpvrawdepay->height = height;
222   rtpvrawdepay->format = format;
223   rtpvrawdepay->yp = yp;
224   rtpvrawdepay->up = up;
225   rtpvrawdepay->vp = vp;
226   rtpvrawdepay->pgroup = pgroup;
227   rtpvrawdepay->xinc = xinc;
228   rtpvrawdepay->yinc = yinc;
229   rtpvrawdepay->ystride = ystride;
230   rtpvrawdepay->uvstride = uvstride;
231   rtpvrawdepay->outsize = outsize;
232
233   srccaps = gst_caps_new_simple (type,
234       "width", G_TYPE_INT, width,
235       "height", G_TYPE_INT, height,
236       "format", GST_TYPE_FOURCC, fourcc,
237       "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
238
239   res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), srccaps);
240   gst_caps_unref (srccaps);
241
242   GST_DEBUG_OBJECT (depayload, "width %d, height %d, format %d", width, height,
243       format);
244   GST_DEBUG_OBJECT (depayload, "yp %d, up %d, vp %d", yp, up, vp);
245   GST_DEBUG_OBJECT (depayload, "xinc %d, yinc %d", xinc, yinc);
246   GST_DEBUG_OBJECT (depayload, "pgroup %d, ystride %d, uvstride %d", pgroup,
247       ystride, uvstride);
248   GST_DEBUG_OBJECT (depayload, "outsize %u", outsize);
249
250   return res;
251
252   /* ERRORS */
253 no_width:
254   {
255     GST_ERROR_OBJECT (depayload, "no width specified");
256     return FALSE;
257   }
258 no_height:
259   {
260     GST_ERROR_OBJECT (depayload, "no height specified");
261     return FALSE;
262   }
263 interlaced:
264   {
265     GST_ERROR_OBJECT (depayload, "interlaced formats not supported yet");
266     return FALSE;
267   }
268 no_sampling:
269   {
270     GST_ERROR_OBJECT (depayload, "no sampling specified");
271     return FALSE;
272   }
273 unknown_format:
274   {
275     GST_ERROR_OBJECT (depayload, "unknown sampling format '%s'", str);
276     return FALSE;
277   }
278 }
279
280 static GstBuffer *
281 gst_rtp_vraw_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
282 {
283   GstRtpVRawDepay *rtpvrawdepay;
284   guint8 *payload, *data, *yp, *up, *vp, *headers;
285   guint32 timestamp;
286   guint cont, ystride, uvstride, pgroup, payload_len;
287   gint width, height, xinc, yinc;
288
289   rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
290
291   timestamp = gst_rtp_buffer_get_timestamp (buf);
292
293   if (timestamp != rtpvrawdepay->timestamp || rtpvrawdepay->outbuf == NULL) {
294     GstBuffer *outbuf;
295     GstFlowReturn ret;
296
297     GST_LOG_OBJECT (depayload, "new frame with timestamp %u", timestamp);
298     /* new timestamp, flush old buffer and create new output buffer */
299     if (rtpvrawdepay->outbuf) {
300       gst_base_rtp_depayload_push_ts (depayload, rtpvrawdepay->timestamp,
301           rtpvrawdepay->outbuf);
302       rtpvrawdepay->outbuf = NULL;
303     }
304
305     ret = gst_pad_alloc_buffer (depayload->srcpad, -1, rtpvrawdepay->outsize,
306         GST_PAD_CAPS (depayload->srcpad), &outbuf);
307     if (ret != GST_FLOW_OK)
308       goto alloc_failed;
309
310     /* clear timestamp from alloc... */
311     GST_BUFFER_TIMESTAMP (outbuf) = -1;
312
313     rtpvrawdepay->outbuf = outbuf;
314     rtpvrawdepay->timestamp = timestamp;
315   }
316
317   data = GST_BUFFER_DATA (rtpvrawdepay->outbuf);
318
319   /* get pointer and strides of the planes */
320   yp = data + rtpvrawdepay->yp;
321   up = data + rtpvrawdepay->up;
322   vp = data + rtpvrawdepay->vp;
323
324   ystride = rtpvrawdepay->ystride;
325   uvstride = rtpvrawdepay->uvstride;
326   pgroup = rtpvrawdepay->pgroup;
327   width = rtpvrawdepay->width;
328   height = rtpvrawdepay->height;
329   xinc = rtpvrawdepay->xinc;
330   yinc = rtpvrawdepay->yinc;
331
332   payload = gst_rtp_buffer_get_payload (buf);
333   payload_len = gst_rtp_buffer_get_payload_len (buf);
334
335   if (payload_len < 3)
336     goto short_packet;
337
338   /* skip extended seqnum */
339   payload += 2;
340   payload_len -= 2;
341
342   /* remember header position */
343   headers = payload;
344
345   /* find data start */
346   do {
347     if (payload_len < 6)
348       goto short_packet;
349
350     cont = payload[4] & 0x80;
351
352     payload += 6;
353     payload_len -= 6;
354   } while (cont);
355
356   while (TRUE) {
357     guint length, line, offs, plen;
358     guint8 *datap;
359
360     /* stop when we run out of data */
361     if (payload_len == 0)
362       break;
363
364     /* read length and cont. This should work because we iterated the headers
365      * above. */
366     length = (headers[0] << 8) | headers[1];
367     line = ((headers[2] & 0x7f) << 8) | headers[3];
368     offs = ((headers[4] & 0x7f) << 8) | headers[5];
369     cont = headers[4] & 0x80;
370     headers += 6;
371
372     /* length must be a multiple of pgroup */
373     if (length % pgroup != 0)
374       goto wrong_length;
375
376     if (length > payload_len)
377       length = payload_len;
378
379     /* sanity check */
380     if (line > (height - yinc)) {
381       GST_WARNING_OBJECT (depayload, "skipping line %d: out of range", line);
382       goto next;
383     }
384     if (offs > (width - xinc)) {
385       GST_WARNING_OBJECT (depayload, "skipping offset %d: out of range", offs);
386       goto next;
387     }
388
389     /* calculate the maximim amount of bytes we can use per line */
390     if (offs + ((length / pgroup) * xinc) > width) {
391       plen = ((width - offs) * pgroup) / xinc;
392       GST_WARNING_OBJECT (depayload, "clipping length %d, offset %d, plen %d",
393           length, offs, plen);
394     } else
395       plen = length;
396
397     GST_LOG_OBJECT (depayload,
398         "writing length %u/%u, line %u, offset %u, remaining %u", plen, length,
399         line, offs, payload_len);
400
401     switch (rtpvrawdepay->format) {
402       case GST_VIDEO_FORMAT_RGB:
403       case GST_VIDEO_FORMAT_RGBA:
404       case GST_VIDEO_FORMAT_BGR:
405       case GST_VIDEO_FORMAT_BGRA:
406       case GST_VIDEO_FORMAT_UYVY:
407         /* samples are packed just like gstreamer packs them */
408         offs /= xinc;
409         datap = yp + (line * ystride) + (offs * pgroup);
410
411         memcpy (datap, payload, plen);
412         break;
413       case GST_VIDEO_FORMAT_AYUV:
414       {
415         gint i;
416         guint8 *p;
417
418         datap = yp + (line * ystride) + (offs * 4);
419         p = payload;
420
421         /* samples are packed in order Cb-Y-Cr for both interlaced and
422          * progressive frames */
423         for (i = 0; i < plen; i += pgroup) {
424           *datap++ = 0;
425           *datap++ = p[1];
426           *datap++ = p[0];
427           *datap++ = p[2];
428           p += pgroup;
429         }
430         break;
431       }
432       case GST_VIDEO_FORMAT_I420:
433       {
434         gint i;
435         guint uvoff;
436         guint8 *yd1p, *yd2p, *udp, *vdp, *p;
437
438         yd1p = yp + (line * ystride) + (offs);
439         yd2p = yd1p + ystride;
440         uvoff = (line / yinc * uvstride) + (offs / xinc);
441
442         udp = up + uvoff;
443         vdp = vp + uvoff;
444         p = payload;
445
446         /* line 0/1: Y00-Y01-Y10-Y11-Cb00-Cr00 Y02-Y03-Y12-Y13-Cb01-Cr01 ...  */
447         for (i = 0; i < plen; i += pgroup) {
448           *yd1p++ = p[0];
449           *yd1p++ = p[1];
450           *yd2p++ = p[2];
451           *yd2p++ = p[3];
452           *udp++ = p[4];
453           *vdp++ = p[5];
454           p += pgroup;
455         }
456         break;
457       }
458       case GST_VIDEO_FORMAT_Y41B:
459       {
460         gint i;
461         guint uvoff;
462         guint8 *ydp, *udp, *vdp, *p;
463
464         ydp = yp + (line * ystride) + (offs);
465         uvoff = (line / yinc * uvstride) + (offs / xinc);
466
467         udp = up + uvoff;
468         vdp = vp + uvoff;
469         p = payload;
470
471         /* Samples are packed in order Cb0-Y0-Y1-Cr0-Y2-Y3 for both interlaced
472          * and progressive scan lines */
473         for (i = 0; i < plen; i += pgroup) {
474           *udp++ = p[0];
475           *ydp++ = p[1];
476           *ydp++ = p[2];
477           *vdp++ = p[3];
478           *ydp++ = p[4];
479           *ydp++ = p[5];
480           p += pgroup;
481         }
482         break;
483       }
484       default:
485         goto unknown_sampling;
486     }
487
488   next:
489     if (!cont)
490       break;
491
492     payload += length;
493     payload_len -= length;
494   }
495
496   if (gst_rtp_buffer_get_marker (buf)) {
497     GST_LOG_OBJECT (depayload, "marker, flushing frame");
498     if (rtpvrawdepay->outbuf) {
499       gst_base_rtp_depayload_push_ts (depayload, timestamp,
500           rtpvrawdepay->outbuf);
501       rtpvrawdepay->outbuf = NULL;
502     }
503     rtpvrawdepay->timestamp = -1;
504   }
505   return NULL;
506
507   /* ERRORS */
508 unknown_sampling:
509   {
510     GST_ELEMENT_ERROR (depayload, STREAM, FORMAT,
511         (NULL), ("unimplemented sampling"));
512     return NULL;
513   }
514 alloc_failed:
515   {
516     GST_WARNING_OBJECT (depayload, "failed to alloc output buffer");
517     return NULL;
518   }
519 wrong_length:
520   {
521     GST_WARNING_OBJECT (depayload, "length not multiple of pgroup");
522     return NULL;
523   }
524 short_packet:
525   {
526     GST_WARNING_OBJECT (depayload, "short packet");
527     return NULL;
528   }
529 }
530
531 static gboolean
532 gst_rtp_vraw_depay_handle_event (GstBaseRTPDepayload * filter, GstEvent * event)
533 {
534   gboolean ret;
535   GstRtpVRawDepay *rtpvrawdepay;
536
537   rtpvrawdepay = GST_RTP_VRAW_DEPAY (filter);
538
539   switch (GST_EVENT_TYPE (event)) {
540     case GST_EVENT_FLUSH_STOP:
541       gst_rtp_vraw_depay_reset (rtpvrawdepay);
542       break;
543     default:
544       break;
545   }
546
547   ret =
548       GST_BASE_RTP_DEPAYLOAD_CLASS (parent_class)->handle_event (filter, event);
549
550   return ret;
551 }
552
553 static GstStateChangeReturn
554 gst_rtp_vraw_depay_change_state (GstElement * element,
555     GstStateChange transition)
556 {
557   GstRtpVRawDepay *rtpvrawdepay;
558   GstStateChangeReturn ret;
559
560   rtpvrawdepay = GST_RTP_VRAW_DEPAY (element);
561
562   switch (transition) {
563     case GST_STATE_CHANGE_NULL_TO_READY:
564       break;
565     case GST_STATE_CHANGE_READY_TO_PAUSED:
566       gst_rtp_vraw_depay_reset (rtpvrawdepay);
567       break;
568     default:
569       break;
570   }
571
572   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
573
574   switch (transition) {
575     case GST_STATE_CHANGE_PAUSED_TO_READY:
576       gst_rtp_vraw_depay_reset (rtpvrawdepay);
577       break;
578     case GST_STATE_CHANGE_READY_TO_NULL:
579       break;
580     default:
581       break;
582   }
583   return ret;
584 }
585
586 gboolean
587 gst_rtp_vraw_depay_plugin_init (GstPlugin * plugin)
588 {
589   return gst_element_register (plugin, "rtpvrawdepay",
590       GST_RANK_SECONDARY, GST_TYPE_RTP_VRAW_DEPAY);
591 }