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