upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpsv3vdepay.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 <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpsv3vdepay.h"
28
29 GST_DEBUG_CATEGORY (rtpsv3vdepay_debug);
30 #define GST_CAT_DEFAULT rtpsv3vdepay_debug
31
32 static GstStaticPadTemplate gst_rtp_sv3v_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("video/x-svq, " "svqversion = (int) 3")
37     );
38
39 static GstStaticPadTemplate gst_rtp_sv3v_depay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("application/x-rtp, "
44         "media = (string) \"video\", "
45         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
46         "clock-rate = (int) 90000, "
47         "encoding-name = (string) { \"X-SV3V-ES\", \"X-SORENSON-VIDEO\" , \"X-SORENSONVIDEO\" , \"X-SorensonVideo\" }")
48     );
49
50 GST_BOILERPLATE (GstRtpSV3VDepay, gst_rtp_sv3v_depay, GstBaseRTPDepayload,
51     GST_TYPE_BASE_RTP_DEPAYLOAD);
52
53 static void gst_rtp_sv3v_depay_finalize (GObject * object);
54
55 static GstStateChangeReturn gst_rtp_sv3v_depay_change_state (GstElement *
56     element, GstStateChange transition);
57
58 static GstBuffer *gst_rtp_sv3v_depay_process (GstBaseRTPDepayload * depayload,
59     GstBuffer * buf);
60 gboolean gst_rtp_sv3v_depay_setcaps (GstBaseRTPDepayload * filter,
61     GstCaps * caps);
62
63 static void
64 gst_rtp_sv3v_depay_base_init (gpointer klass)
65 {
66   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
67
68   gst_element_class_add_pad_template (element_class,
69       gst_static_pad_template_get (&gst_rtp_sv3v_depay_src_template));
70   gst_element_class_add_pad_template (element_class,
71       gst_static_pad_template_get (&gst_rtp_sv3v_depay_sink_template));
72
73
74   gst_element_class_set_details_simple (element_class, "RTP SVQ3 depayloader",
75       "Codec/Depayloader/Network/RTP",
76       "Extracts SVQ3 video from RTP packets (no RFC)",
77       "Wim Taymans <wim.taymans@gmail.com>");
78 }
79
80 static void
81 gst_rtp_sv3v_depay_class_init (GstRtpSV3VDepayClass * klass)
82 {
83   GObjectClass *gobject_class;
84   GstElementClass *gstelement_class;
85   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
86
87   gobject_class = (GObjectClass *) klass;
88   gstelement_class = (GstElementClass *) klass;
89   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
90
91   gstbasertpdepayload_class->process = gst_rtp_sv3v_depay_process;
92   gstbasertpdepayload_class->set_caps = gst_rtp_sv3v_depay_setcaps;
93
94   gobject_class->finalize = gst_rtp_sv3v_depay_finalize;
95
96   gstelement_class->change_state = gst_rtp_sv3v_depay_change_state;
97 }
98
99 static void
100 gst_rtp_sv3v_depay_init (GstRtpSV3VDepay * rtpsv3vdepay,
101     GstRtpSV3VDepayClass * klass)
102 {
103   rtpsv3vdepay->adapter = gst_adapter_new ();
104 }
105
106 static void
107 gst_rtp_sv3v_depay_finalize (GObject * object)
108 {
109   GstRtpSV3VDepay *rtpsv3vdepay;
110
111   rtpsv3vdepay = GST_RTP_SV3V_DEPAY (object);
112
113   g_object_unref (rtpsv3vdepay->adapter);
114   rtpsv3vdepay->adapter = NULL;
115
116   G_OBJECT_CLASS (parent_class)->finalize (object);
117 }
118
119 // only on the sink
120 gboolean
121 gst_rtp_sv3v_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
122 {
123   GstStructure *structure = gst_caps_get_structure (caps, 0);
124   gint clock_rate;
125
126   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
127     clock_rate = 90000;         // default
128   filter->clock_rate = clock_rate;
129
130   /* will set caps later */
131
132   return TRUE;
133 }
134
135 static GstBuffer *
136 gst_rtp_sv3v_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
137 {
138   GstRtpSV3VDepay *rtpsv3vdepay;
139   static struct
140   {
141     guint width, height;
142   } resolutions[7] = {
143     {
144     160, 128}, {
145     128, 96}, {
146     176, 144}, {
147     352, 288}, {
148     704, 576}, {
149     240, 180}, {
150     320, 240}
151   };
152   gint payload_len;
153   guint8 *payload;
154   gboolean M;
155   gboolean C, S, E;
156   GstBuffer *outbuf = NULL;
157   guint16 seq;
158
159   rtpsv3vdepay = GST_RTP_SV3V_DEPAY (depayload);
160
161   /* flush on sequence number gaps */
162   seq = gst_rtp_buffer_get_seq (buf);
163
164   GST_DEBUG ("timestamp %" GST_TIME_FORMAT ", sequence number:%d",
165       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), seq);
166
167   if (seq != rtpsv3vdepay->nextseq) {
168     GST_DEBUG ("Sequence discontinuity, clearing adapter");
169     gst_adapter_clear (rtpsv3vdepay->adapter);
170   }
171   rtpsv3vdepay->nextseq = seq + 1;
172
173   payload_len = gst_rtp_buffer_get_payload_len (buf);
174   if (payload_len < 3)
175     goto bad_packet;
176
177   payload = gst_rtp_buffer_get_payload (buf);
178
179   M = gst_rtp_buffer_get_marker (buf);
180
181   /* This is all a guess:
182    *                      1 1 1 1 1 1
183    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 
184    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
185    * |0|C|S|E|0|0|0|0|0|0|0|0|0|0|0|0|
186    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
187    *
188    * C: config, packet contains config info
189    * S: start, packet contains start of frame
190    * E: end, packet contains end of frame
191    */
192   /* this seems to indicate a packet with a config string sent before each
193    * keyframe */
194   C = (payload[0] & 0x40) == 0x40;
195
196   /* redundant with the RTP marker bit */
197   S = (payload[0] & 0x20) == 0x20;
198   E = (payload[0] & 0x10) == 0x10;
199
200   GST_DEBUG ("M:%d, C:%d, S:%d, E:%d", M, C, S, E);
201
202   GST_MEMDUMP ("incoming buffer", payload, payload_len);
203
204   if (G_UNLIKELY (C)) {
205     GstCaps *caps;
206     GstBuffer *codec_data;
207     guint8 res;
208
209     GST_DEBUG ("Configuration packet");
210
211     /* if we already have caps, we don't need to do anything. FIXME, check if
212      * something changed. */
213     if (G_UNLIKELY (GST_PAD_CAPS (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload)))) {
214       GST_DEBUG ("Already configured, skipping config parsing");
215       goto beach;
216     }
217
218     res = payload[2] >> 5;
219
220     /* width and height, according to http://wiki.multimedia.cx/index.php?title=Sorenson_Video_1#Stream_Format_And_Header */
221     if (G_LIKELY (res < 7)) {
222       rtpsv3vdepay->width = resolutions[res].width;
223       rtpsv3vdepay->height = resolutions[res].height;
224     } else {
225       /* extended width/height, they're contained in the following 24bit */
226       rtpsv3vdepay->width = ((payload[2] & 0x1f) << 7) | (payload[3] >> 1);
227       rtpsv3vdepay->height =
228           (payload[3] & 0x1) << 11 | payload[4] << 3 | (payload[5] >> 5);
229     }
230
231     /* CodecData needs to be 'SEQH' + len (32bit) + data according to
232      * ffmpeg's libavcodec/svq3.c:svq3_decode_init */
233     codec_data = gst_buffer_new_and_alloc (payload_len + 6);
234     memcpy (GST_BUFFER_DATA (codec_data), "SEQH", 4);
235     GST_WRITE_UINT32_LE (GST_BUFFER_DATA (codec_data) + 4, payload_len - 2);
236     memcpy (GST_BUFFER_DATA (codec_data) + 8, payload + 2, payload_len - 2);
237
238     GST_MEMDUMP ("codec_data", GST_BUFFER_DATA (codec_data),
239         GST_BUFFER_SIZE (codec_data));
240
241     caps = gst_caps_new_simple ("video/x-svq",
242         "svqversion", G_TYPE_INT, 3,
243         "width", G_TYPE_INT, rtpsv3vdepay->width,
244         "height", G_TYPE_INT, rtpsv3vdepay->height,
245         "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
246     gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (depayload), caps);
247     gst_caps_unref (caps);
248
249     GST_DEBUG ("Depayloader now configured");
250
251     rtpsv3vdepay->configured = TRUE;
252
253     goto beach;
254   }
255
256   if (G_LIKELY (rtpsv3vdepay->configured)) {
257     GstBuffer *tmpbuf;
258
259     GST_DEBUG ("Storing incoming payload");
260     /* store data in adapter, stip off 2 bytes header */
261     tmpbuf = gst_rtp_buffer_get_payload_subbuffer (buf, 2, -1);
262     gst_adapter_push (rtpsv3vdepay->adapter, tmpbuf);
263
264     if (G_UNLIKELY (M)) {
265       /* frame is completed: push contents of adapter */
266       guint avail;
267
268       avail = gst_adapter_available (rtpsv3vdepay->adapter);
269       GST_DEBUG ("Returning completed output buffer [%d bytes]", avail);
270       outbuf = gst_adapter_take_buffer (rtpsv3vdepay->adapter, avail);
271     }
272   }
273
274 beach:
275   return outbuf;
276
277   /* ERRORS */
278 bad_packet:
279   {
280     GST_ELEMENT_WARNING (rtpsv3vdepay, STREAM, DECODE,
281         (NULL), ("Packet was too short"));
282     return NULL;
283   }
284 }
285
286 static GstStateChangeReturn
287 gst_rtp_sv3v_depay_change_state (GstElement * element,
288     GstStateChange transition)
289 {
290   GstRtpSV3VDepay *rtpsv3vdepay;
291   GstStateChangeReturn ret;
292
293   rtpsv3vdepay = GST_RTP_SV3V_DEPAY (element);
294
295   switch (transition) {
296     case GST_STATE_CHANGE_NULL_TO_READY:
297       break;
298     case GST_STATE_CHANGE_READY_TO_PAUSED:
299       gst_adapter_clear (rtpsv3vdepay->adapter);
300       break;
301     default:
302       break;
303   }
304
305   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
306
307   switch (transition) {
308     case GST_STATE_CHANGE_READY_TO_NULL:
309       break;
310     default:
311       break;
312   }
313   return ret;
314 }
315
316 gboolean
317 gst_rtp_sv3v_depay_plugin_init (GstPlugin * plugin)
318 {
319   GST_DEBUG_CATEGORY_INIT (rtpsv3vdepay_debug, "rtpsv3vdepay", 0,
320       "RTP SV3V depayloader");
321
322   return gst_element_register (plugin, "rtpsv3vdepay",
323       GST_RANK_SECONDARY, GST_TYPE_RTP_SV3V_DEPAY);
324 }