Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpmp4vdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> 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 "gstrtpmp4vdepay.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtpmp4vdepay_debug);
30 #define GST_CAT_DEFAULT (rtpmp4vdepay_debug)
31
32 static GstStaticPadTemplate gst_rtp_mp4v_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("video/mpeg,"
37         "mpegversion=(int) 4," "systemstream=(boolean)false")
38     );
39
40 static GstStaticPadTemplate gst_rtp_mp4v_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) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
48         /* All optional parameters
49          *
50          * "profile-level-id=[1,MAX]"
51          * "config=" 
52          */
53     )
54     );
55
56 GST_BOILERPLATE (GstRtpMP4VDepay, gst_rtp_mp4v_depay, GstBaseRTPDepayload,
57     GST_TYPE_BASE_RTP_DEPAYLOAD);
58
59 static void gst_rtp_mp4v_depay_finalize (GObject * object);
60
61 static gboolean gst_rtp_mp4v_depay_setcaps (GstBaseRTPDepayload * depayload,
62     GstCaps * caps);
63 static GstBuffer *gst_rtp_mp4v_depay_process (GstBaseRTPDepayload * depayload,
64     GstBuffer * buf);
65
66 static GstStateChangeReturn gst_rtp_mp4v_depay_change_state (GstElement *
67     element, GstStateChange transition);
68
69
70 static void
71 gst_rtp_mp4v_depay_base_init (gpointer klass)
72 {
73   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
74
75   gst_element_class_add_static_pad_template (element_class,
76       &gst_rtp_mp4v_depay_src_template);
77   gst_element_class_add_static_pad_template (element_class,
78       &gst_rtp_mp4v_depay_sink_template);
79
80   gst_element_class_set_details_simple (element_class,
81       "RTP MPEG4 video depayloader", "Codec/Depayloader/Network/RTP",
82       "Extracts MPEG4 video from RTP packets (RFC 3016)",
83       "Wim Taymans <wim.taymans@gmail.com>");
84 }
85
86 static void
87 gst_rtp_mp4v_depay_class_init (GstRtpMP4VDepayClass * klass)
88 {
89   GObjectClass *gobject_class;
90   GstElementClass *gstelement_class;
91   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
92
93   gobject_class = (GObjectClass *) klass;
94   gstelement_class = (GstElementClass *) klass;
95   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
96
97   gobject_class->finalize = gst_rtp_mp4v_depay_finalize;
98
99   gstelement_class->change_state = gst_rtp_mp4v_depay_change_state;
100
101   gstbasertpdepayload_class->process = gst_rtp_mp4v_depay_process;
102   gstbasertpdepayload_class->set_caps = gst_rtp_mp4v_depay_setcaps;
103
104   GST_DEBUG_CATEGORY_INIT (rtpmp4vdepay_debug, "rtpmp4vdepay", 0,
105       "MPEG4 video RTP Depayloader");
106 }
107
108 static void
109 gst_rtp_mp4v_depay_init (GstRtpMP4VDepay * rtpmp4vdepay,
110     GstRtpMP4VDepayClass * klass)
111 {
112   rtpmp4vdepay->adapter = gst_adapter_new ();
113 }
114
115 static void
116 gst_rtp_mp4v_depay_finalize (GObject * object)
117 {
118   GstRtpMP4VDepay *rtpmp4vdepay;
119
120   rtpmp4vdepay = GST_RTP_MP4V_DEPAY (object);
121
122   g_object_unref (rtpmp4vdepay->adapter);
123   rtpmp4vdepay->adapter = NULL;
124
125   G_OBJECT_CLASS (parent_class)->finalize (object);
126 }
127
128 static gboolean
129 gst_rtp_mp4v_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
130 {
131   GstStructure *structure;
132   GstCaps *srccaps;
133   const gchar *str;
134   gint clock_rate;
135   gboolean res;
136
137   structure = gst_caps_get_structure (caps, 0);
138
139   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
140     clock_rate = 90000;         /* default */
141   depayload->clock_rate = clock_rate;
142
143   srccaps = gst_caps_new_simple ("video/mpeg",
144       "mpegversion", G_TYPE_INT, 4,
145       "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
146
147   if ((str = gst_structure_get_string (structure, "config"))) {
148     GValue v = { 0 };
149
150     g_value_init (&v, GST_TYPE_BUFFER);
151     if (gst_value_deserialize (&v, str)) {
152       GstBuffer *buffer;
153
154       buffer = gst_value_get_buffer (&v);
155       gst_caps_set_simple (srccaps,
156           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
157       /* caps takes ref */
158       g_value_unset (&v);
159     } else {
160       g_warning ("cannot convert config to buffer");
161     }
162   }
163   res = gst_pad_set_caps (depayload->srcpad, srccaps);
164   gst_caps_unref (srccaps);
165
166   return res;
167 }
168
169 static GstBuffer *
170 gst_rtp_mp4v_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
171 {
172   GstRtpMP4VDepay *rtpmp4vdepay;
173   GstBuffer *outbuf;
174
175   rtpmp4vdepay = GST_RTP_MP4V_DEPAY (depayload);
176
177   /* flush remaining data on discont */
178   if (GST_BUFFER_IS_DISCONT (buf))
179     gst_adapter_clear (rtpmp4vdepay->adapter);
180
181   outbuf = gst_rtp_buffer_get_payload_buffer (buf);
182   gst_adapter_push (rtpmp4vdepay->adapter, outbuf);
183
184   /* if this was the last packet of the VOP, create and push a buffer */
185   if (gst_rtp_buffer_get_marker (buf)) {
186     guint avail;
187
188     avail = gst_adapter_available (rtpmp4vdepay->adapter);
189
190     outbuf = gst_adapter_take_buffer (rtpmp4vdepay->adapter, avail);
191
192     GST_DEBUG ("gst_rtp_mp4v_depay_chain: pushing buffer of size %d",
193         GST_BUFFER_SIZE (outbuf));
194
195     return outbuf;
196   }
197   return NULL;
198 }
199
200 static GstStateChangeReturn
201 gst_rtp_mp4v_depay_change_state (GstElement * element,
202     GstStateChange transition)
203 {
204   GstRtpMP4VDepay *rtpmp4vdepay;
205   GstStateChangeReturn ret;
206
207   rtpmp4vdepay = GST_RTP_MP4V_DEPAY (element);
208
209   switch (transition) {
210     case GST_STATE_CHANGE_READY_TO_PAUSED:
211       gst_adapter_clear (rtpmp4vdepay->adapter);
212       break;
213     default:
214       break;
215   }
216
217   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
218
219   switch (transition) {
220     default:
221       break;
222   }
223   return ret;
224 }
225
226 gboolean
227 gst_rtp_mp4v_depay_plugin_init (GstPlugin * plugin)
228 {
229   return gst_element_register (plugin, "rtpmp4vdepay",
230       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4V_DEPAY);
231 }