• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gstrtpelements.h"
29 #include "gstrtpsv3vdepay.h"
30 #include "gstrtputils.h"
31 
32 GST_DEBUG_CATEGORY (rtpsv3vdepay_debug);
33 #define GST_CAT_DEFAULT rtpsv3vdepay_debug
34 
35 static GstStaticPadTemplate gst_rtp_sv3v_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("video/x-svq, " "svqversion = (int) 3")
40     );
41 
42 static GstStaticPadTemplate gst_rtp_sv3v_depay_sink_template =
43 GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
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\" }")
50     );
51 
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));
58 
59 static void gst_rtp_sv3v_depay_finalize (GObject * object);
60 
61 static GstStateChangeReturn gst_rtp_sv3v_depay_change_state (GstElement *
62     element, GstStateChange transition);
63 
64 static GstBuffer *gst_rtp_sv3v_depay_process (GstRTPBaseDepayload * depayload,
65     GstRTPBuffer * rtp);
66 gboolean gst_rtp_sv3v_depay_setcaps (GstRTPBaseDepayload * filter,
67     GstCaps * caps);
68 
69 static void
gst_rtp_sv3v_depay_class_init(GstRtpSV3VDepayClass * klass)70 gst_rtp_sv3v_depay_class_init (GstRtpSV3VDepayClass * klass)
71 {
72   GObjectClass *gobject_class;
73   GstElementClass *gstelement_class;
74   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
75 
76   gobject_class = (GObjectClass *) klass;
77   gstelement_class = (GstElementClass *) klass;
78   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
79 
80   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_sv3v_depay_process;
81   gstrtpbasedepayload_class->set_caps = gst_rtp_sv3v_depay_setcaps;
82 
83   gobject_class->finalize = gst_rtp_sv3v_depay_finalize;
84 
85   gstelement_class->change_state = gst_rtp_sv3v_depay_change_state;
86 
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);
91 
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>");
96 }
97 
98 static void
gst_rtp_sv3v_depay_init(GstRtpSV3VDepay * rtpsv3vdepay)99 gst_rtp_sv3v_depay_init (GstRtpSV3VDepay * rtpsv3vdepay)
100 {
101   rtpsv3vdepay->adapter = gst_adapter_new ();
102 }
103 
104 static void
gst_rtp_sv3v_depay_finalize(GObject * object)105 gst_rtp_sv3v_depay_finalize (GObject * object)
106 {
107   GstRtpSV3VDepay *rtpsv3vdepay;
108 
109   rtpsv3vdepay = GST_RTP_SV3V_DEPAY (object);
110 
111   g_object_unref (rtpsv3vdepay->adapter);
112   rtpsv3vdepay->adapter = NULL;
113 
114   G_OBJECT_CLASS (parent_class)->finalize (object);
115 }
116 
117 /* only on the sink */
118 gboolean
gst_rtp_sv3v_depay_setcaps(GstRTPBaseDepayload * filter,GstCaps * caps)119 gst_rtp_sv3v_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
120 {
121   GstStructure *structure = gst_caps_get_structure (caps, 0);
122   gint clock_rate;
123 
124   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
125     clock_rate = 90000;         /* default */
126   filter->clock_rate = clock_rate;
127 
128   /* will set caps later */
129 
130   return TRUE;
131 }
132 
133 static GstBuffer *
gst_rtp_sv3v_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)134 gst_rtp_sv3v_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
135 {
136   GstRtpSV3VDepay *rtpsv3vdepay;
137   static struct
138   {
139     guint width, height;
140   } resolutions[7] = {
141     {
142     160, 128}, {
143     128, 96}, {
144     176, 144}, {
145     352, 288}, {
146     704, 576}, {
147     240, 180}, {
148     320, 240}
149   };
150   gint payload_len;
151   guint8 *payload;
152   gboolean M;
153   gboolean C, S, E;
154   GstBuffer *outbuf = NULL;
155   guint16 seq;
156 
157   rtpsv3vdepay = GST_RTP_SV3V_DEPAY (depayload);
158 
159 
160   /* flush on sequence number gaps */
161   seq = gst_rtp_buffer_get_seq (rtp);
162 
163   GST_DEBUG ("timestamp %" GST_TIME_FORMAT ", sequence number:%d",
164       GST_TIME_ARGS (GST_BUFFER_PTS (rtp->buffer)), seq);
165 
166   if (seq != rtpsv3vdepay->nextseq) {
167     GST_DEBUG ("Sequence discontinuity, clearing adapter");
168     gst_adapter_clear (rtpsv3vdepay->adapter);
169   }
170   rtpsv3vdepay->nextseq = seq + 1;
171 
172   payload_len = gst_rtp_buffer_get_payload_len (rtp);
173   if (payload_len < 3)
174     goto bad_packet;
175 
176   payload = gst_rtp_buffer_get_payload (rtp);
177 
178   M = gst_rtp_buffer_get_marker (rtp);
179 
180   /* This is all a guess:
181    *                      1 1 1 1 1 1
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    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
186    *
187    * C: config, packet contains config info
188    * S: start, packet contains start of frame
189    * E: end, packet contains end of frame
190    */
191   /* this seems to indicate a packet with a config string sent before each
192    * keyframe */
193   C = (payload[0] & 0x40) == 0x40;
194 
195   /* redundant with the RTP marker bit */
196   S = (payload[0] & 0x20) == 0x20;
197   E = (payload[0] & 0x10) == 0x10;
198 
199   GST_DEBUG ("M:%d, C:%d, S:%d, E:%d", M, C, S, E);
200 
201   GST_MEMDUMP ("incoming buffer", payload, payload_len);
202 
203   if (G_UNLIKELY (C)) {
204     GstCaps *caps;
205     GstBuffer *codec_data;
206     GstMapInfo cmap;
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_has_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD
214                 (depayload)))) {
215       GST_DEBUG ("Already configured, skipping config parsing");
216       goto beach;
217     }
218 
219     res = payload[2] >> 5;
220 
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;
225     } else {
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);
230     }
231 
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);
241 
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);
249 
250     GST_DEBUG ("Depayloader now configured");
251 
252     rtpsv3vdepay->configured = TRUE;
253 
254     goto beach;
255   }
256 
257   if (G_LIKELY (rtpsv3vdepay->configured)) {
258     GstBuffer *tmpbuf;
259 
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);
264 
265     if (G_UNLIKELY (M)) {
266       /* frame is completed: push contents of adapter */
267       guint avail;
268 
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);
273     }
274   }
275 
276 beach:
277   return outbuf;
278 
279   /* ERRORS */
280 bad_packet:
281   {
282     GST_ELEMENT_WARNING (rtpsv3vdepay, STREAM, DECODE,
283         (NULL), ("Packet was too short"));
284     return NULL;
285   }
286 }
287 
288 static GstStateChangeReturn
gst_rtp_sv3v_depay_change_state(GstElement * element,GstStateChange transition)289 gst_rtp_sv3v_depay_change_state (GstElement * element,
290     GstStateChange transition)
291 {
292   GstRtpSV3VDepay *rtpsv3vdepay;
293   GstStateChangeReturn ret;
294 
295   rtpsv3vdepay = GST_RTP_SV3V_DEPAY (element);
296 
297   switch (transition) {
298     case GST_STATE_CHANGE_NULL_TO_READY:
299       break;
300     case GST_STATE_CHANGE_READY_TO_PAUSED:
301       gst_adapter_clear (rtpsv3vdepay->adapter);
302       break;
303     default:
304       break;
305   }
306 
307   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
308 
309   switch (transition) {
310     case GST_STATE_CHANGE_READY_TO_NULL:
311       break;
312     default:
313       break;
314   }
315   return ret;
316 }
317