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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
28 #include "gstrtpelements.h"
29 #include "gstrtpsv3vdepay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY (rtpsv3vdepay_debug);
33 #define GST_CAT_DEFAULT rtpsv3vdepay_debug
35 static GstStaticPadTemplate gst_rtp_sv3v_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
39 GST_STATIC_CAPS ("video/x-svq, " "svqversion = (int) 3")
42 static GstStaticPadTemplate gst_rtp_sv3v_depay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"video\", "
48 "clock-rate = (int) 90000, "
49 "encoding-name = (string) { \"X-SV3V-ES\", \"X-SORENSON-VIDEO\" , \"X-SORENSONVIDEO\" , \"X-SorensonVideo\" }")
52 #define gst_rtp_sv3v_depay_parent_class parent_class
53 G_DEFINE_TYPE_WITH_CODE (GstRtpSV3VDepay, gst_rtp_sv3v_depay,
54 GST_TYPE_RTP_BASE_DEPAYLOAD, GST_DEBUG_CATEGORY_INIT (rtpsv3vdepay_debug,
55 "rtpsv3vdepay", 0, "RTP SV3V depayloader"));
56 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpsv3vdepay, "rtpsv3vdepay",
57 GST_RANK_SECONDARY, GST_TYPE_RTP_SV3V_DEPAY, rtp_element_init (plugin));
59 static void gst_rtp_sv3v_depay_finalize (GObject * object);
61 static GstStateChangeReturn gst_rtp_sv3v_depay_change_state (GstElement *
62 element, GstStateChange transition);
64 static GstBuffer *gst_rtp_sv3v_depay_process (GstRTPBaseDepayload * depayload,
66 gboolean gst_rtp_sv3v_depay_setcaps (GstRTPBaseDepayload * filter,
70 gst_rtp_sv3v_depay_class_init (GstRtpSV3VDepayClass * klass)
72 GObjectClass *gobject_class;
73 GstElementClass *gstelement_class;
74 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
76 gobject_class = (GObjectClass *) klass;
77 gstelement_class = (GstElementClass *) klass;
78 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
80 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_sv3v_depay_process;
81 gstrtpbasedepayload_class->set_caps = gst_rtp_sv3v_depay_setcaps;
83 gobject_class->finalize = gst_rtp_sv3v_depay_finalize;
85 gstelement_class->change_state = gst_rtp_sv3v_depay_change_state;
87 gst_element_class_add_static_pad_template (gstelement_class,
88 &gst_rtp_sv3v_depay_src_template);
89 gst_element_class_add_static_pad_template (gstelement_class,
90 &gst_rtp_sv3v_depay_sink_template);
92 gst_element_class_set_static_metadata (gstelement_class,
93 "RTP SVQ3 depayloader", "Codec/Depayloader/Network/RTP",
94 "Extracts SVQ3 video from RTP packets (no RFC)",
95 "Wim Taymans <wim.taymans@gmail.com>");
99 gst_rtp_sv3v_depay_init (GstRtpSV3VDepay * rtpsv3vdepay)
101 rtpsv3vdepay->adapter = gst_adapter_new ();
105 gst_rtp_sv3v_depay_finalize (GObject * object)
107 GstRtpSV3VDepay *rtpsv3vdepay;
109 rtpsv3vdepay = GST_RTP_SV3V_DEPAY (object);
111 g_object_unref (rtpsv3vdepay->adapter);
112 rtpsv3vdepay->adapter = NULL;
114 G_OBJECT_CLASS (parent_class)->finalize (object);
117 /* only on the sink */
119 gst_rtp_sv3v_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
121 GstStructure *structure = gst_caps_get_structure (caps, 0);
124 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
125 clock_rate = 90000; /* default */
126 filter->clock_rate = clock_rate;
128 /* will set caps later */
134 gst_rtp_sv3v_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
136 GstRtpSV3VDepay *rtpsv3vdepay;
154 GstBuffer *outbuf = NULL;
157 rtpsv3vdepay = GST_RTP_SV3V_DEPAY (depayload);
160 /* flush on sequence number gaps */
161 seq = gst_rtp_buffer_get_seq (rtp);
163 GST_DEBUG ("timestamp %" GST_TIME_FORMAT ", sequence number:%d",
164 GST_TIME_ARGS (GST_BUFFER_PTS (rtp->buffer)), seq);
166 if (seq != rtpsv3vdepay->nextseq) {
167 GST_DEBUG ("Sequence discontinuity, clearing adapter");
168 gst_adapter_clear (rtpsv3vdepay->adapter);
170 rtpsv3vdepay->nextseq = seq + 1;
172 payload_len = gst_rtp_buffer_get_payload_len (rtp);
176 payload = gst_rtp_buffer_get_payload (rtp);
178 M = gst_rtp_buffer_get_marker (rtp);
180 /* This is all a guess:
182 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
183 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
184 * |0|C|S|E|0|0|0|0|0|0|0|0|0|0|0|0|
185 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
187 * C: config, packet contains config info
188 * S: start, packet contains start of frame
189 * E: end, packet contains end of frame
191 /* this seems to indicate a packet with a config string sent before each
193 C = (payload[0] & 0x40) == 0x40;
195 /* redundant with the RTP marker bit */
196 S = (payload[0] & 0x20) == 0x20;
197 E = (payload[0] & 0x10) == 0x10;
199 GST_DEBUG ("M:%d, C:%d, S:%d, E:%d", M, C, S, E);
201 GST_MEMDUMP ("incoming buffer", payload, payload_len);
203 if (G_UNLIKELY (C)) {
205 GstBuffer *codec_data;
209 GST_DEBUG ("Configuration packet");
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_has_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD
215 GST_DEBUG ("Already configured, skipping config parsing");
219 res = payload[2] >> 5;
221 /* width and height, according to http://wiki.multimedia.cx/index.php?title=Sorenson_Video_1#Stream_Format_And_Header */
222 if (G_LIKELY (res < 7)) {
223 rtpsv3vdepay->width = resolutions[res].width;
224 rtpsv3vdepay->height = resolutions[res].height;
226 /* extended width/height, they're contained in the following 24bit */
227 rtpsv3vdepay->width = ((payload[2] & 0x1f) << 7) | (payload[3] >> 1);
228 rtpsv3vdepay->height =
229 (payload[3] & 0x1) << 11 | payload[4] << 3 | (payload[5] >> 5);
232 /* CodecData needs to be 'SEQH' + len (32bit) + data according to
233 * ffmpeg's libavcodec/svq3.c:svq3_decode_init */
234 codec_data = gst_buffer_new_and_alloc (payload_len + 6);
235 gst_buffer_map (codec_data, &cmap, GST_MAP_WRITE);
236 memcpy (cmap.data, "SEQH", 4);
237 GST_WRITE_UINT32_LE (cmap.data + 4, payload_len - 2);
238 memcpy (cmap.data + 8, payload + 2, payload_len - 2);
239 GST_MEMDUMP ("codec_data", cmap.data, gst_buffer_get_size (codec_data));
240 gst_buffer_unmap (codec_data, &cmap);
242 caps = gst_caps_new_simple ("video/x-svq",
243 "svqversion", G_TYPE_INT, 3,
244 "width", G_TYPE_INT, rtpsv3vdepay->width,
245 "height", G_TYPE_INT, rtpsv3vdepay->height,
246 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
247 gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), caps);
248 gst_caps_unref (caps);
250 GST_DEBUG ("Depayloader now configured");
252 rtpsv3vdepay->configured = TRUE;
257 if (G_LIKELY (rtpsv3vdepay->configured)) {
260 GST_DEBUG ("Storing incoming payload");
261 /* store data in adapter, stip off 2 bytes header */
262 tmpbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 2, -1);
263 gst_adapter_push (rtpsv3vdepay->adapter, tmpbuf);
265 if (G_UNLIKELY (M)) {
266 /* frame is completed: push contents of adapter */
269 avail = gst_adapter_available (rtpsv3vdepay->adapter);
270 GST_DEBUG ("Returning completed output buffer [%d bytes]", avail);
271 outbuf = gst_adapter_take_buffer (rtpsv3vdepay->adapter, avail);
272 gst_rtp_drop_non_video_meta (rtpsv3vdepay, outbuf);
282 GST_ELEMENT_WARNING (rtpsv3vdepay, STREAM, DECODE,
283 (NULL), ("Packet was too short"));
288 static GstStateChangeReturn
289 gst_rtp_sv3v_depay_change_state (GstElement * element,
290 GstStateChange transition)
292 GstRtpSV3VDepay *rtpsv3vdepay;
293 GstStateChangeReturn ret;
295 rtpsv3vdepay = GST_RTP_SV3V_DEPAY (element);
297 switch (transition) {
298 case GST_STATE_CHANGE_NULL_TO_READY:
300 case GST_STATE_CHANGE_READY_TO_PAUSED:
301 gst_adapter_clear (rtpsv3vdepay->adapter);
307 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
309 switch (transition) {
310 case GST_STATE_CHANGE_READY_TO_NULL: