941107bb207a13cdd7c70629483ea9099e4466c7
[platform/upstream/gstreamer.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_RTP_BASE_DEPAYLOAD);
53
54 static gboolean gst_rtp_vraw_depay_setcaps (GstRTPBaseDepayload * depayload,
55     GstCaps * caps);
56 static GstBuffer *gst_rtp_vraw_depay_process (GstRTPBaseDepayload * 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 (GstRTPBaseDepayload * filter,
63     GstEvent * event);
64
65 static void
66 gst_rtp_vraw_depay_class_init (GstRtpVRawDepayClass * klass)
67 {
68   GstElementClass *gstelement_class;
69   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
70
71   gstelement_class = (GstElementClass *) klass;
72   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
73
74   gstelement_class->change_state = gst_rtp_vraw_depay_change_state;
75
76   gstrtpbasedepayload_class->set_caps = gst_rtp_vraw_depay_setcaps;
77   gstrtpbasedepayload_class->process = gst_rtp_vraw_depay_process;
78   gstrtpbasedepayload_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, padding, 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_RTP_BASE_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         &padding, &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     padding = 0;
138     alignment = 0;
139   }
140
141   if (pool == NULL) {
142     /* we did not get a pool, make one ourselves then */
143     pool = gst_buffer_pool_new ();
144   }
145
146   if (depay->pool)
147     gst_object_unref (depay->pool);
148   depay->pool = pool;
149
150   config = gst_buffer_pool_get_config (pool);
151   gst_buffer_pool_config_set (config, caps, size, min, max, prefix, padding,
152       alignment);
153   /* just set the metadata, if the pool can support it we will transparently use
154    * it through the video info API. We could also see if the pool support this
155    * metadata and only activate it then. */
156   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
157
158   gst_buffer_pool_set_config (pool, config);
159   /* and activate */
160   gst_buffer_pool_set_active (pool, TRUE);
161
162   gst_query_unref (query);
163
164   return GST_FLOW_OK;
165 }
166
167 static gboolean
168 gst_rtp_vraw_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
169 {
170   GstStructure *structure;
171   GstRtpVRawDepay *rtpvrawdepay;
172   gint clock_rate;
173   const gchar *str;
174   gint format, width, height, pgroup, xinc, yinc;
175   GstCaps *srccaps;
176   gboolean res;
177   GstFlowReturn ret;
178
179   rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
180
181   structure = gst_caps_get_structure (caps, 0);
182
183   xinc = yinc = 1;
184
185   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
186     clock_rate = 90000;         /* default */
187   depayload->clock_rate = clock_rate;
188
189   if (!(str = gst_structure_get_string (structure, "width")))
190     goto no_width;
191   width = atoi (str);
192
193   if (!(str = gst_structure_get_string (structure, "height")))
194     goto no_height;
195   height = atoi (str);
196
197   /* optional interlace value but we don't handle interlaced
198    * formats yet */
199   if (gst_structure_get_string (structure, "interlace"))
200     goto interlaced;
201
202   if (!(str = gst_structure_get_string (structure, "sampling")))
203     goto no_sampling;
204
205   if (!strcmp (str, "RGB")) {
206     format = GST_VIDEO_FORMAT_RGB;
207     pgroup = 3;
208   } else if (!strcmp (str, "RGBA")) {
209     format = GST_VIDEO_FORMAT_RGBA;
210     pgroup = 4;
211   } else if (!strcmp (str, "BGR")) {
212     format = GST_VIDEO_FORMAT_BGR;
213     pgroup = 3;
214   } else if (!strcmp (str, "BGRA")) {
215     format = GST_VIDEO_FORMAT_BGRA;
216     pgroup = 4;
217   } else if (!strcmp (str, "YCbCr-4:4:4")) {
218     format = GST_VIDEO_FORMAT_AYUV;
219     pgroup = 3;
220   } else if (!strcmp (str, "YCbCr-4:2:2")) {
221     format = GST_VIDEO_FORMAT_UYVY;
222     pgroup = 4;
223     xinc = 2;
224   } else if (!strcmp (str, "YCbCr-4:2:0")) {
225     format = GST_VIDEO_FORMAT_I420;
226     pgroup = 6;
227     xinc = yinc = 2;
228   } else if (!strcmp (str, "YCbCr-4:1:1")) {
229     format = GST_VIDEO_FORMAT_Y41B;
230     pgroup = 6;
231     xinc = 4;
232   } else
233     goto unknown_format;
234
235   gst_video_info_init (&rtpvrawdepay->vinfo);
236   gst_video_info_set_format (&rtpvrawdepay->vinfo, format, width, height);
237   GST_VIDEO_INFO_FPS_N (&rtpvrawdepay->vinfo) = 0;
238   GST_VIDEO_INFO_FPS_D (&rtpvrawdepay->vinfo) = 1;
239
240   rtpvrawdepay->pgroup = pgroup;
241   rtpvrawdepay->xinc = xinc;
242   rtpvrawdepay->yinc = yinc;
243
244   srccaps = gst_video_info_to_caps (&rtpvrawdepay->vinfo);
245   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
246   gst_caps_unref (srccaps);
247
248   GST_DEBUG_OBJECT (depayload, "width %d, height %d, format %d", width, height,
249       format);
250   GST_DEBUG_OBJECT (depayload, "xinc %d, yinc %d, pgroup %d",
251       xinc, yinc, pgroup);
252
253   /* negotiate a bufferpool */
254   if ((ret = gst_rtp_vraw_depay_negotiate_pool (rtpvrawdepay, caps,
255               &rtpvrawdepay->vinfo)) != GST_FLOW_OK)
256     goto no_bufferpool;
257
258   return res;
259
260   /* ERRORS */
261 no_width:
262   {
263     GST_ERROR_OBJECT (depayload, "no width specified");
264     return FALSE;
265   }
266 no_height:
267   {
268     GST_ERROR_OBJECT (depayload, "no height specified");
269     return FALSE;
270   }
271 interlaced:
272   {
273     GST_ERROR_OBJECT (depayload, "interlaced formats not supported yet");
274     return FALSE;
275   }
276 no_sampling:
277   {
278     GST_ERROR_OBJECT (depayload, "no sampling specified");
279     return FALSE;
280   }
281 unknown_format:
282   {
283     GST_ERROR_OBJECT (depayload, "unknown sampling format '%s'", str);
284     return FALSE;
285   }
286 no_bufferpool:
287   {
288     GST_DEBUG_OBJECT (depayload, "no bufferpool");
289     return FALSE;
290   }
291 }
292
293 static GstBuffer *
294 gst_rtp_vraw_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
295 {
296   GstRtpVRawDepay *rtpvrawdepay;
297   guint8 *payload, *yp, *up, *vp, *headers;
298   guint32 timestamp;
299   guint cont, ystride, uvstride, pgroup, payload_len;
300   gint width, height, xinc, yinc;
301   GstRTPBuffer rtp = { NULL };
302   GstVideoFrame frame;
303
304   rtpvrawdepay = GST_RTP_VRAW_DEPAY (depayload);
305
306   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
307
308   timestamp = gst_rtp_buffer_get_timestamp (&rtp);
309
310   if (timestamp != rtpvrawdepay->timestamp || rtpvrawdepay->outbuf == NULL) {
311     GstBuffer *outbuf;
312     GstFlowReturn ret;
313
314     GST_LOG_OBJECT (depayload, "new frame with timestamp %u", timestamp);
315     /* new timestamp, flush old buffer and create new output buffer */
316     if (rtpvrawdepay->outbuf) {
317       gst_rtp_base_depayload_push (depayload, rtpvrawdepay->outbuf);
318       rtpvrawdepay->outbuf = NULL;
319     }
320
321     if (gst_pad_check_reconfigure (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload))) {
322       GstCaps *caps;
323
324       caps =
325           gst_pad_get_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
326       gst_rtp_vraw_depay_negotiate_pool (rtpvrawdepay, caps,
327           &rtpvrawdepay->vinfo);
328       gst_caps_unref (caps);
329     }
330
331     ret = gst_buffer_pool_acquire_buffer (rtpvrawdepay->pool, &outbuf, NULL);
332     if (G_UNLIKELY (ret != GST_FLOW_OK))
333       goto alloc_failed;
334
335     /* clear timestamp from alloc... */
336     GST_BUFFER_TIMESTAMP (outbuf) = -1;
337
338     rtpvrawdepay->outbuf = outbuf;
339     rtpvrawdepay->timestamp = timestamp;
340   }
341
342   if (!gst_video_frame_map (&frame, &rtpvrawdepay->vinfo, rtpvrawdepay->outbuf,
343           GST_MAP_WRITE))
344     goto invalid_frame;
345
346   /* get pointer and strides of the planes */
347   yp = GST_VIDEO_FRAME_COMP_DATA (&frame, 0);
348   up = GST_VIDEO_FRAME_COMP_DATA (&frame, 1);
349   vp = GST_VIDEO_FRAME_COMP_DATA (&frame, 2);
350
351   ystride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0);
352   uvstride = GST_VIDEO_FRAME_COMP_STRIDE (&frame, 1);
353
354   pgroup = rtpvrawdepay->pgroup;
355   width = GST_VIDEO_INFO_WIDTH (&rtpvrawdepay->vinfo);
356   height = GST_VIDEO_INFO_HEIGHT (&rtpvrawdepay->vinfo);
357   xinc = rtpvrawdepay->xinc;
358   yinc = rtpvrawdepay->yinc;
359
360   payload = gst_rtp_buffer_get_payload (&rtp);
361   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
362
363   if (payload_len < 3)
364     goto short_packet;
365
366   /* skip extended seqnum */
367   payload += 2;
368   payload_len -= 2;
369
370   /* remember header position */
371   headers = payload;
372
373   /* find data start */
374   do {
375     if (payload_len < 6)
376       goto short_packet;
377
378     cont = payload[4] & 0x80;
379
380     payload += 6;
381     payload_len -= 6;
382   } while (cont);
383
384   while (TRUE) {
385     guint length, line, offs, plen;
386     guint8 *datap;
387
388     /* stop when we run out of data */
389     if (payload_len == 0)
390       break;
391
392     /* read length and cont. This should work because we iterated the headers
393      * above. */
394     length = (headers[0] << 8) | headers[1];
395     line = ((headers[2] & 0x7f) << 8) | headers[3];
396     offs = ((headers[4] & 0x7f) << 8) | headers[5];
397     cont = headers[4] & 0x80;
398     headers += 6;
399
400     /* length must be a multiple of pgroup */
401     if (length % pgroup != 0)
402       goto wrong_length;
403
404     if (length > payload_len)
405       length = payload_len;
406
407     /* sanity check */
408     if (line > (height - yinc)) {
409       GST_WARNING_OBJECT (depayload, "skipping line %d: out of range", line);
410       goto next;
411     }
412     if (offs > (width - xinc)) {
413       GST_WARNING_OBJECT (depayload, "skipping offset %d: out of range", offs);
414       goto next;
415     }
416
417     /* calculate the maximim amount of bytes we can use per line */
418     if (offs + ((length / pgroup) * xinc) > width) {
419       plen = ((width - offs) * pgroup) / xinc;
420       GST_WARNING_OBJECT (depayload, "clipping length %d, offset %d, plen %d",
421           length, offs, plen);
422     } else
423       plen = length;
424
425     GST_LOG_OBJECT (depayload,
426         "writing length %u/%u, line %u, offset %u, remaining %u", plen, length,
427         line, offs, payload_len);
428
429     switch (GST_VIDEO_INFO_FORMAT (&rtpvrawdepay->vinfo)) {
430       case GST_VIDEO_FORMAT_RGB:
431       case GST_VIDEO_FORMAT_RGBA:
432       case GST_VIDEO_FORMAT_BGR:
433       case GST_VIDEO_FORMAT_BGRA:
434       case GST_VIDEO_FORMAT_UYVY:
435         /* samples are packed just like gstreamer packs them */
436         offs /= xinc;
437         datap = yp + (line * ystride) + (offs * pgroup);
438
439         memcpy (datap, payload, plen);
440         break;
441       case GST_VIDEO_FORMAT_AYUV:
442       {
443         gint i;
444         guint8 *p;
445
446         datap = yp + (line * ystride) + (offs * 4);
447         p = payload;
448
449         /* samples are packed in order Cb-Y-Cr for both interlaced and
450          * progressive frames */
451         for (i = 0; i < plen; i += pgroup) {
452           *datap++ = 0;
453           *datap++ = p[1];
454           *datap++ = p[0];
455           *datap++ = p[2];
456           p += pgroup;
457         }
458         break;
459       }
460       case GST_VIDEO_FORMAT_I420:
461       {
462         gint i;
463         guint uvoff;
464         guint8 *yd1p, *yd2p, *udp, *vdp, *p;
465
466         yd1p = yp + (line * ystride) + (offs);
467         yd2p = yd1p + ystride;
468         uvoff = (line / yinc * uvstride) + (offs / xinc);
469
470         udp = up + uvoff;
471         vdp = vp + uvoff;
472         p = payload;
473
474         /* line 0/1: Y00-Y01-Y10-Y11-Cb00-Cr00 Y02-Y03-Y12-Y13-Cb01-Cr01 ...  */
475         for (i = 0; i < plen; i += pgroup) {
476           *yd1p++ = p[0];
477           *yd1p++ = p[1];
478           *yd2p++ = p[2];
479           *yd2p++ = p[3];
480           *udp++ = p[4];
481           *vdp++ = p[5];
482           p += pgroup;
483         }
484         break;
485       }
486       case GST_VIDEO_FORMAT_Y41B:
487       {
488         gint i;
489         guint uvoff;
490         guint8 *ydp, *udp, *vdp, *p;
491
492         ydp = yp + (line * ystride) + (offs);
493         uvoff = (line / yinc * uvstride) + (offs / xinc);
494
495         udp = up + uvoff;
496         vdp = vp + uvoff;
497         p = payload;
498
499         /* Samples are packed in order Cb0-Y0-Y1-Cr0-Y2-Y3 for both interlaced
500          * and progressive scan lines */
501         for (i = 0; i < plen; i += pgroup) {
502           *udp++ = p[0];
503           *ydp++ = p[1];
504           *ydp++ = p[2];
505           *vdp++ = p[3];
506           *ydp++ = p[4];
507           *ydp++ = p[5];
508           p += pgroup;
509         }
510         break;
511       }
512       default:
513         goto unknown_sampling;
514     }
515
516   next:
517     if (!cont)
518       break;
519
520     payload += length;
521     payload_len -= length;
522   }
523
524   gst_video_frame_unmap (&frame);
525   gst_rtp_buffer_unmap (&rtp);
526
527   if (gst_rtp_buffer_get_marker (&rtp)) {
528     GST_LOG_OBJECT (depayload, "marker, flushing frame");
529     if (rtpvrawdepay->outbuf) {
530       gst_rtp_base_depayload_push (depayload, 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 (GstRTPBaseDepayload * 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_RTP_BASE_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 }