2 * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpsv3vdepay.h"
29 GST_DEBUG_CATEGORY (rtpsv3vdepay_debug);
30 #define GST_CAT_DEFAULT rtpsv3vdepay_debug
32 static GstStaticPadTemplate gst_rtp_sv3v_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
36 GST_STATIC_CAPS ("video/x-svq, " "svqversion = (int) 3")
39 static GstStaticPadTemplate gst_rtp_sv3v_depay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
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\" }")
50 #define gst_rtp_sv3v_depay_parent_class parent_class
51 G_DEFINE_TYPE (GstRtpSV3VDepay, gst_rtp_sv3v_depay,
52 GST_TYPE_RTP_BASE_DEPAYLOAD);
54 static void gst_rtp_sv3v_depay_finalize (GObject * object);
56 static GstStateChangeReturn gst_rtp_sv3v_depay_change_state (GstElement *
57 element, GstStateChange transition);
59 static GstBuffer *gst_rtp_sv3v_depay_process (GstRTPBaseDepayload * depayload,
61 gboolean gst_rtp_sv3v_depay_setcaps (GstRTPBaseDepayload * filter,
65 gst_rtp_sv3v_depay_class_init (GstRtpSV3VDepayClass * klass)
67 GObjectClass *gobject_class;
68 GstElementClass *gstelement_class;
69 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
71 gobject_class = (GObjectClass *) klass;
72 gstelement_class = (GstElementClass *) klass;
73 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
75 gstrtpbasedepayload_class->process = gst_rtp_sv3v_depay_process;
76 gstrtpbasedepayload_class->set_caps = gst_rtp_sv3v_depay_setcaps;
78 gobject_class->finalize = gst_rtp_sv3v_depay_finalize;
80 gstelement_class->change_state = gst_rtp_sv3v_depay_change_state;
82 gst_element_class_add_pad_template (gstelement_class,
83 gst_static_pad_template_get (&gst_rtp_sv3v_depay_src_template));
84 gst_element_class_add_pad_template (gstelement_class,
85 gst_static_pad_template_get (&gst_rtp_sv3v_depay_sink_template));
87 gst_element_class_set_details_simple (gstelement_class,
88 "RTP SVQ3 depayloader", "Codec/Depayloader/Network/RTP",
89 "Extracts SVQ3 video from RTP packets (no RFC)",
90 "Wim Taymans <wim.taymans@gmail.com>");
94 gst_rtp_sv3v_depay_init (GstRtpSV3VDepay * rtpsv3vdepay)
96 rtpsv3vdepay->adapter = gst_adapter_new ();
100 gst_rtp_sv3v_depay_finalize (GObject * object)
102 GstRtpSV3VDepay *rtpsv3vdepay;
104 rtpsv3vdepay = GST_RTP_SV3V_DEPAY (object);
106 g_object_unref (rtpsv3vdepay->adapter);
107 rtpsv3vdepay->adapter = NULL;
109 G_OBJECT_CLASS (parent_class)->finalize (object);
114 gst_rtp_sv3v_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
116 GstStructure *structure = gst_caps_get_structure (caps, 0);
119 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
120 clock_rate = 90000; // default
121 filter->clock_rate = clock_rate;
123 /* will set caps later */
129 gst_rtp_sv3v_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
131 GstRtpSV3VDepay *rtpsv3vdepay;
149 GstBuffer *outbuf = NULL;
151 GstRTPBuffer rtp = { NULL };
153 rtpsv3vdepay = GST_RTP_SV3V_DEPAY (depayload);
155 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
157 /* flush on sequence number gaps */
158 seq = gst_rtp_buffer_get_seq (&rtp);
160 GST_DEBUG ("timestamp %" GST_TIME_FORMAT ", sequence number:%d",
161 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), seq);
163 if (seq != rtpsv3vdepay->nextseq) {
164 GST_DEBUG ("Sequence discontinuity, clearing adapter");
165 gst_adapter_clear (rtpsv3vdepay->adapter);
167 rtpsv3vdepay->nextseq = seq + 1;
169 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
173 payload = gst_rtp_buffer_get_payload (&rtp);
175 M = gst_rtp_buffer_get_marker (&rtp);
177 /* This is all a guess:
179 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
180 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
181 * |0|C|S|E|0|0|0|0|0|0|0|0|0|0|0|0|
182 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
184 * C: config, packet contains config info
185 * S: start, packet contains start of frame
186 * E: end, packet contains end of frame
188 /* this seems to indicate a packet with a config string sent before each
190 C = (payload[0] & 0x40) == 0x40;
192 /* redundant with the RTP marker bit */
193 S = (payload[0] & 0x20) == 0x20;
194 E = (payload[0] & 0x10) == 0x10;
196 GST_DEBUG ("M:%d, C:%d, S:%d, E:%d", M, C, S, E);
198 GST_MEMDUMP ("incoming buffer", payload, payload_len);
200 if (G_UNLIKELY (C)) {
202 GstBuffer *codec_data;
206 GST_DEBUG ("Configuration packet");
208 /* if we already have caps, we don't need to do anything. FIXME, check if
209 * something changed. */
210 if (G_UNLIKELY (gst_pad_has_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD
212 GST_DEBUG ("Already configured, skipping config parsing");
216 res = payload[2] >> 5;
218 /* width and height, according to http://wiki.multimedia.cx/index.php?title=Sorenson_Video_1#Stream_Format_And_Header */
219 if (G_LIKELY (res < 7)) {
220 rtpsv3vdepay->width = resolutions[res].width;
221 rtpsv3vdepay->height = resolutions[res].height;
223 /* extended width/height, they're contained in the following 24bit */
224 rtpsv3vdepay->width = ((payload[2] & 0x1f) << 7) | (payload[3] >> 1);
225 rtpsv3vdepay->height =
226 (payload[3] & 0x1) << 11 | payload[4] << 3 | (payload[5] >> 5);
229 /* CodecData needs to be 'SEQH' + len (32bit) + data according to
230 * ffmpeg's libavcodec/svq3.c:svq3_decode_init */
231 codec_data = gst_buffer_new_and_alloc (payload_len + 6);
232 gst_buffer_map (codec_data, &cmap, GST_MAP_WRITE);
233 memcpy (cmap.data, "SEQH", 4);
234 GST_WRITE_UINT32_LE (cmap.data + 4, payload_len - 2);
235 memcpy (cmap.data + 8, payload + 2, payload_len - 2);
236 GST_MEMDUMP ("codec_data", cmap.data, gst_buffer_get_size (codec_data));
237 gst_buffer_unmap (codec_data, &cmap);
239 caps = gst_caps_new_simple ("video/x-svq",
240 "svqversion", G_TYPE_INT, 3,
241 "width", G_TYPE_INT, rtpsv3vdepay->width,
242 "height", G_TYPE_INT, rtpsv3vdepay->height,
243 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
244 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), caps);
245 gst_caps_unref (caps);
247 GST_DEBUG ("Depayloader now configured");
249 rtpsv3vdepay->configured = TRUE;
254 if (G_LIKELY (rtpsv3vdepay->configured)) {
257 GST_DEBUG ("Storing incoming payload");
258 /* store data in adapter, stip off 2 bytes header */
259 tmpbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 2, -1);
260 gst_adapter_push (rtpsv3vdepay->adapter, tmpbuf);
262 if (G_UNLIKELY (M)) {
263 /* frame is completed: push contents of adapter */
266 avail = gst_adapter_available (rtpsv3vdepay->adapter);
267 GST_DEBUG ("Returning completed output buffer [%d bytes]", avail);
268 outbuf = gst_adapter_take_buffer (rtpsv3vdepay->adapter, avail);
273 gst_rtp_buffer_unmap (&rtp);
279 GST_ELEMENT_WARNING (rtpsv3vdepay, STREAM, DECODE,
280 (NULL), ("Packet was too short"));
281 gst_rtp_buffer_unmap (&rtp);
286 static GstStateChangeReturn
287 gst_rtp_sv3v_depay_change_state (GstElement * element,
288 GstStateChange transition)
290 GstRtpSV3VDepay *rtpsv3vdepay;
291 GstStateChangeReturn ret;
293 rtpsv3vdepay = GST_RTP_SV3V_DEPAY (element);
295 switch (transition) {
296 case GST_STATE_CHANGE_NULL_TO_READY:
298 case GST_STATE_CHANGE_READY_TO_PAUSED:
299 gst_adapter_clear (rtpsv3vdepay->adapter);
305 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
307 switch (transition) {
308 case GST_STATE_CHANGE_READY_TO_NULL:
317 gst_rtp_sv3v_depay_plugin_init (GstPlugin * plugin)
319 GST_DEBUG_CATEGORY_INIT (rtpsv3vdepay_debug, "rtpsv3vdepay", 0,
320 "RTP SV3V depayloader");
322 return gst_element_register (plugin, "rtpsv3vdepay",
323 GST_RANK_SECONDARY, GST_TYPE_RTP_SV3V_DEPAY);