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 "gstrtph263pdepay.h"
29 #include "gstrtputils.h"
30
31 GST_DEBUG_CATEGORY_STATIC (rtph263pdepay_debug);
32 #define GST_CAT_DEFAULT (rtph263pdepay_debug)
33
34 static GstStaticPadTemplate gst_rtp_h263p_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
36 GST_PAD_SRC,
37 GST_PAD_ALWAYS,
38 GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
39 );
40
41 static GstStaticPadTemplate gst_rtp_h263p_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) [1, MAX], "
48 "encoding-name = (string) \"H263-1998\"; "
49 /* optional params */
50 /* NOTE all optional SDP params must be strings in the caps */
51 /*
52 "sqcif = (string) [1, 32], "
53 "qcif = (string) [1, 32], "
54 "cif = (string) [1, 32], "
55 "cif4 = (string) [1, 32], "
56 "cif16 = (string) [1, 32], "
57 "custom = (string) ANY, "
58 "f = (string) {0, 1},"
59 "i = (string) {0, 1},"
60 "j = (string) {0, 1},"
61 "t = (string) {0, 1},"
62 "k = (string) {1, 2, 3, 4},"
63 "n = (string) {1, 2, 3, 4},"
64 "p = (string) ANY,"
65 "par = (string) ANY, "
66 "cpcf = (string) ANY, "
67 "bpp = (string) [0, 65536], "
68 "hrd = (string) {0, 1}; "
69 */
70 "application/x-rtp, "
71 "media = (string) \"video\", "
72 "clock-rate = (int) [1, MAX], "
73 "encoding-name = (string) \"H263-2000\" "
74 /* optional params */
75 /* NOTE all optional SDP params must be strings in the caps */
76 /*
77 "profile = (string) [0, 10], "
78 "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
79 "interlace = (string) {0, 1};"
80 */
81 )
82 );
83
84 #define gst_rtp_h263p_depay_parent_class parent_class
85 G_DEFINE_TYPE (GstRtpH263PDepay, gst_rtp_h263p_depay,
86 GST_TYPE_RTP_BASE_DEPAYLOAD);
87
88 static void gst_rtp_h263p_depay_finalize (GObject * object);
89
90 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
91 element, GstStateChange transition);
92
93 static GstBuffer *gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
94 GstRTPBuffer * rtp);
95 gboolean gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter,
96 GstCaps * caps);
97
98 static void
gst_rtp_h263p_depay_class_init(GstRtpH263PDepayClass * klass)99 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
100 {
101 GObjectClass *gobject_class;
102 GstElementClass *gstelement_class;
103 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
104
105 gobject_class = (GObjectClass *) klass;
106 gstelement_class = (GstElementClass *) klass;
107 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
108
109 gobject_class->finalize = gst_rtp_h263p_depay_finalize;
110
111 gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
112
113 gst_element_class_add_static_pad_template (gstelement_class,
114 &gst_rtp_h263p_depay_src_template);
115 gst_element_class_add_static_pad_template (gstelement_class,
116 &gst_rtp_h263p_depay_sink_template);
117
118 gst_element_class_set_static_metadata (gstelement_class,
119 "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
120 "Extracts H263/+/++ video from RTP packets (RFC 4629)",
121 "Wim Taymans <wim.taymans@gmail.com>");
122
123 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h263p_depay_process;
124 gstrtpbasedepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
125
126 GST_DEBUG_CATEGORY_INIT (rtph263pdepay_debug, "rtph263pdepay", 0,
127 "H263+ Video RTP Depayloader");
128 }
129
130 static void
gst_rtp_h263p_depay_init(GstRtpH263PDepay * rtph263pdepay)131 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay)
132 {
133 rtph263pdepay->adapter = gst_adapter_new ();
134 }
135
136 static void
gst_rtp_h263p_depay_finalize(GObject * object)137 gst_rtp_h263p_depay_finalize (GObject * object)
138 {
139 GstRtpH263PDepay *rtph263pdepay;
140
141 rtph263pdepay = GST_RTP_H263P_DEPAY (object);
142
143 g_object_unref (rtph263pdepay->adapter);
144 rtph263pdepay->adapter = NULL;
145
146 G_OBJECT_CLASS (parent_class)->finalize (object);
147 }
148
149 gboolean
gst_rtp_h263p_depay_setcaps(GstRTPBaseDepayload * filter,GstCaps * caps)150 gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
151 {
152 GstCaps *srccaps = NULL;
153 GstStructure *structure = gst_caps_get_structure (caps, 0);
154 gint clock_rate;
155 const gchar *encoding_name = NULL;
156 gboolean res;
157
158 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
159 clock_rate = 90000; /* default */
160 filter->clock_rate = clock_rate;
161
162 encoding_name = gst_structure_get_string (structure, "encoding-name");
163 if (encoding_name == NULL)
164 goto no_encoding_name;
165
166 if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
167 /* always h263++ */
168 srccaps = gst_caps_new_simple ("video/x-h263",
169 "variant", G_TYPE_STRING, "itu",
170 "h263version", G_TYPE_STRING, "h263pp", NULL);
171 } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
172 /* this can be H263 or H263+ depending on defined appendixes in the optional
173 * SDP params */
174 const gchar *F, *I, *J, *T, *K, *N, *P;
175 gboolean is_h263p = FALSE;
176
177 F = gst_structure_get_string (structure, "f");
178 if (F)
179 if (g_ascii_strcasecmp (F, "1") == 0)
180 is_h263p = TRUE;
181 I = gst_structure_get_string (structure, "i");
182 if (I)
183 if (g_ascii_strcasecmp (I, "1") == 0)
184 is_h263p = TRUE;
185 J = gst_structure_get_string (structure, "j");
186 if (J)
187 if (g_ascii_strcasecmp (J, "1") == 0)
188 is_h263p = TRUE;
189 T = gst_structure_get_string (structure, "t");
190 if (T)
191 if (g_ascii_strcasecmp (T, "1") == 0)
192 is_h263p = TRUE;
193 K = gst_structure_get_string (structure, "k");
194 if (K)
195 is_h263p = TRUE;
196 N = gst_structure_get_string (structure, "n");
197 if (N)
198 is_h263p = TRUE;
199 P = gst_structure_get_string (structure, "p");
200 if (P)
201 is_h263p = TRUE;
202
203 if (is_h263p) {
204 srccaps = gst_caps_new_simple ("video/x-h263",
205 "variant", G_TYPE_STRING, "itu",
206 "h263version", G_TYPE_STRING, "h263p", NULL);
207 } else {
208 srccaps = gst_caps_new_simple ("video/x-h263",
209 "variant", G_TYPE_STRING, "itu",
210 "h263version", G_TYPE_STRING, "h263", NULL);
211 }
212 }
213 if (!srccaps)
214 goto no_caps;
215
216 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
217 gst_caps_unref (srccaps);
218
219 return res;
220
221 /* ERRORS */
222 no_encoding_name:
223 {
224 GST_ERROR_OBJECT (filter, "no encoding-name");
225 return FALSE;
226 }
227 no_caps:
228 {
229 GST_ERROR_OBJECT (filter, "invalid encoding-name");
230 return FALSE;
231 }
232 }
233
234 static GstBuffer *
gst_rtp_h263p_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)235 gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
236 GstRTPBuffer * rtp)
237 {
238 GstRtpH263PDepay *rtph263pdepay;
239 GstBuffer *outbuf;
240 gint payload_len;
241 guint8 *payload;
242 gboolean P, V, M;
243 guint header_len;
244 guint8 PLEN, PEBIT;
245
246 rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
247
248 /* flush remaining data on discont */
249 if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
250 GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
251 gst_adapter_clear (rtph263pdepay->adapter);
252 rtph263pdepay->wait_start = TRUE;
253 }
254
255 payload_len = gst_rtp_buffer_get_payload_len (rtp);
256 header_len = 2;
257
258 if (payload_len < header_len)
259 goto too_small;
260
261 payload = gst_rtp_buffer_get_payload (rtp);
262
263 M = gst_rtp_buffer_get_marker (rtp);
264
265 /* 0 1
266 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
267 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
268 * | RR |P|V| PLEN |PEBIT|
269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270 */
271 P = (payload[0] & 0x04) == 0x04;
272 V = (payload[0] & 0x02) == 0x02;
273 PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
274 PEBIT = payload[1] & 0x7;
275
276 GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
277 PEBIT);
278
279 if (V) {
280 header_len++;
281 }
282 if (PLEN) {
283 header_len += PLEN;
284 }
285
286 if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
287 goto too_small;
288
289 if (P) {
290 rtph263pdepay->wait_start = FALSE;
291 header_len -= 2;
292 }
293
294 if (rtph263pdepay->wait_start)
295 goto waiting_start;
296
297 if (payload_len < header_len)
298 goto too_small;
299
300 /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
301 /* FIXME actually use the RTP picture header when it is lost in the network */
302 /* for now strip off header */
303 payload_len -= header_len;
304
305 if (M) {
306 /* frame is completed: append to previous, push it out */
307 guint len, padlen;
308 guint avail;
309 GstBuffer *padbuf;
310
311 GST_LOG_OBJECT (depayload, "Frame complete");
312
313 outbuf =
314 gst_rtp_buffer_get_payload_subbuffer (rtp, header_len, payload_len);
315 if (P)
316 gst_buffer_memset (outbuf, 0, 0, 2);
317 gst_adapter_push (rtph263pdepay->adapter, outbuf);
318 outbuf = NULL;
319
320 avail = gst_adapter_available (rtph263pdepay->adapter);
321 len = avail + payload_len;
322 padlen = (len % 4) + 4;
323
324 if (avail == 0)
325 goto empty_frame;
326
327 outbuf = gst_adapter_take_buffer (rtph263pdepay->adapter, avail);
328 if (padlen) {
329 padbuf = gst_buffer_new_and_alloc (padlen);
330 gst_buffer_memset (padbuf, 0, 0, padlen);
331 outbuf = gst_buffer_append (outbuf, padbuf);
332 }
333
334 gst_rtp_drop_non_video_meta (rtph263pdepay, outbuf);
335
336 return outbuf;
337 } else {
338 /* frame not completed: store in adapter */
339 GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
340
341 outbuf =
342 gst_rtp_buffer_get_payload_subbuffer (rtp, header_len, payload_len);
343 if (P)
344 gst_buffer_memset (outbuf, 0, 0, 2);
345 gst_adapter_push (rtph263pdepay->adapter, outbuf);
346 }
347 return NULL;
348
349 too_small:
350 {
351 GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
352 ("Packet payload was too small"), (NULL));
353 return NULL;
354 }
355 waiting_start:
356 {
357 GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
358 return NULL;
359 }
360 empty_frame:
361 {
362 GST_WARNING_OBJECT (rtph263pdepay, "Depayloaded frame is empty, dropping");
363 return NULL;
364 }
365 }
366
367 static GstStateChangeReturn
gst_rtp_h263p_depay_change_state(GstElement * element,GstStateChange transition)368 gst_rtp_h263p_depay_change_state (GstElement * element,
369 GstStateChange transition)
370 {
371 GstRtpH263PDepay *rtph263pdepay;
372 GstStateChangeReturn ret;
373
374 rtph263pdepay = GST_RTP_H263P_DEPAY (element);
375
376 switch (transition) {
377 case GST_STATE_CHANGE_NULL_TO_READY:
378 break;
379 case GST_STATE_CHANGE_READY_TO_PAUSED:
380 gst_adapter_clear (rtph263pdepay->adapter);
381 rtph263pdepay->wait_start = TRUE;
382 break;
383 default:
384 break;
385 }
386
387 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
388
389 switch (transition) {
390 case GST_STATE_CHANGE_READY_TO_NULL:
391 break;
392 default:
393 break;
394 }
395 return ret;
396 }
397
398 gboolean
gst_rtp_h263p_depay_plugin_init(GstPlugin * plugin)399 gst_rtp_h263p_depay_plugin_init (GstPlugin * plugin)
400 {
401 return gst_element_register (plugin, "rtph263pdepay",
402 GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_DEPAY);
403 }
404