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