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 "gstrtph263pdepay.h"
29 GST_DEBUG_CATEGORY_STATIC (rtph263pdepay_debug);
30 #define GST_CAT_DEFAULT (rtph263pdepay_debug)
32 static GstStaticPadTemplate gst_rtp_h263p_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
36 GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
39 static GstStaticPadTemplate gst_rtp_h263p_depay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_STATIC_CAPS ("application/x-rtp, "
44 "media = (string) \"video\", "
45 "clock-rate = (int) [1, MAX], "
46 "encoding-name = (string) \"H263-1998\"; "
48 /* NOTE all optional SDP params must be strings in the caps */
50 "sqcif = (string) [1, 32], "
51 "qcif = (string) [1, 32], "
52 "cif = (string) [1, 32], "
53 "cif4 = (string) [1, 32], "
54 "cif16 = (string) [1, 32], "
55 "custom = (string) ANY, "
56 "f = (string) {0, 1},"
57 "i = (string) {0, 1},"
58 "j = (string) {0, 1},"
59 "t = (string) {0, 1},"
60 "k = (string) {1, 2, 3, 4},"
61 "n = (string) {1, 2, 3, 4},"
63 "par = (string) ANY, "
64 "cpcf = (string) ANY, "
65 "bpp = (string) [0, 65536], "
66 "hrd = (string) {0, 1}; "
69 "media = (string) \"video\", "
70 "clock-rate = (int) [1, MAX], "
71 "encoding-name = (string) \"H263-2000\" "
73 /* NOTE all optional SDP params must be strings in the caps */
75 "profile = (string) [0, 10], "
76 "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
77 "interlace = (string) {0, 1};"
82 #define gst_rtp_h263p_depay_parent_class parent_class
83 G_DEFINE_TYPE (GstRtpH263PDepay, gst_rtp_h263p_depay,
84 GST_TYPE_RTP_BASE_DEPAYLOAD);
86 static void gst_rtp_h263p_depay_finalize (GObject * object);
88 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
89 element, GstStateChange transition);
91 static GstBuffer *gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
93 gboolean gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter,
97 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
99 GObjectClass *gobject_class;
100 GstElementClass *gstelement_class;
101 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
103 gobject_class = (GObjectClass *) klass;
104 gstelement_class = (GstElementClass *) klass;
105 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
107 gobject_class->finalize = gst_rtp_h263p_depay_finalize;
109 gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
111 gst_element_class_add_pad_template (gstelement_class,
112 gst_static_pad_template_get (&gst_rtp_h263p_depay_src_template));
113 gst_element_class_add_pad_template (gstelement_class,
114 gst_static_pad_template_get (&gst_rtp_h263p_depay_sink_template));
116 gst_element_class_set_static_metadata (gstelement_class,
117 "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
118 "Extracts H263/+/++ video from RTP packets (RFC 4629)",
119 "Wim Taymans <wim.taymans@gmail.com>");
121 gstrtpbasedepayload_class->process = gst_rtp_h263p_depay_process;
122 gstrtpbasedepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
124 GST_DEBUG_CATEGORY_INIT (rtph263pdepay_debug, "rtph263pdepay", 0,
125 "H263+ Video RTP Depayloader");
129 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay)
131 rtph263pdepay->adapter = gst_adapter_new ();
135 gst_rtp_h263p_depay_finalize (GObject * object)
137 GstRtpH263PDepay *rtph263pdepay;
139 rtph263pdepay = GST_RTP_H263P_DEPAY (object);
141 g_object_unref (rtph263pdepay->adapter);
142 rtph263pdepay->adapter = NULL;
144 G_OBJECT_CLASS (parent_class)->finalize (object);
148 gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
150 GstCaps *srccaps = NULL;
151 GstStructure *structure = gst_caps_get_structure (caps, 0);
153 const gchar *encoding_name = NULL;
156 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
157 clock_rate = 90000; /* default */
158 filter->clock_rate = clock_rate;
160 encoding_name = gst_structure_get_string (structure, "encoding-name");
161 if (encoding_name == NULL)
162 goto no_encoding_name;
164 if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
166 srccaps = gst_caps_new_simple ("video/x-h263",
167 "variant", G_TYPE_STRING, "itu",
168 "h263version", G_TYPE_STRING, "h263pp", NULL);
169 } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
170 /* this can be H263 or H263+ depending on defined appendixes in the optional
172 const gchar *F, *I, *J, *T, *K, *N, *P;
173 gboolean is_h263p = FALSE;
175 F = gst_structure_get_string (structure, "f");
177 if (g_ascii_strcasecmp (F, "1") == 0)
179 I = gst_structure_get_string (structure, "i");
181 if (g_ascii_strcasecmp (I, "1") == 0)
183 J = gst_structure_get_string (structure, "j");
185 if (g_ascii_strcasecmp (J, "1") == 0)
187 T = gst_structure_get_string (structure, "t");
189 if (g_ascii_strcasecmp (T, "1") == 0)
191 K = gst_structure_get_string (structure, "k");
194 N = gst_structure_get_string (structure, "n");
197 P = gst_structure_get_string (structure, "p");
202 srccaps = gst_caps_new_simple ("video/x-h263",
203 "variant", G_TYPE_STRING, "itu",
204 "h263version", G_TYPE_STRING, "h263p", NULL);
206 srccaps = gst_caps_new_simple ("video/x-h263",
207 "variant", G_TYPE_STRING, "itu",
208 "h263version", G_TYPE_STRING, "h263", NULL);
214 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
215 gst_caps_unref (srccaps);
222 GST_ERROR_OBJECT (filter, "no encoding-name");
227 GST_ERROR_OBJECT (filter, "invalid encoding-name");
233 gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
235 GstRtpH263PDepay *rtph263pdepay;
242 GstRTPBuffer rtp = { NULL };
244 rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
246 /* flush remaining data on discont */
247 if (GST_BUFFER_IS_DISCONT (buf)) {
248 GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
249 gst_adapter_clear (rtph263pdepay->adapter);
250 rtph263pdepay->wait_start = TRUE;
253 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
255 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
258 if (payload_len < header_len)
261 payload = gst_rtp_buffer_get_payload (&rtp);
263 M = gst_rtp_buffer_get_marker (&rtp);
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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;
276 GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
286 if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
290 /* FIXME, have to make the packet writable hear. Better to reset these
291 * bytes when we copy the packet below */
292 rtph263pdepay->wait_start = FALSE;
294 payload[header_len] = 0;
295 payload[header_len + 1] = 0;
298 if (rtph263pdepay->wait_start)
301 if (payload_len < header_len)
304 /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
305 /* FIXME actually use the RTP picture header when it is lost in the network */
306 /* for now strip off header */
307 payload += header_len;
308 payload_len -= header_len;
311 /* frame is completed: append to previous, push it out */
316 GST_LOG_OBJECT (depayload, "Frame complete");
319 gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len);
320 gst_adapter_push (rtph263pdepay->adapter, outbuf);
323 avail = gst_adapter_available (rtph263pdepay->adapter);
324 len = avail + payload_len;
325 padlen = (len % 4) + 4;
327 outbuf = gst_adapter_take_buffer (rtph263pdepay->adapter, avail);
329 padbuf = gst_buffer_new_and_alloc (padlen);
330 gst_buffer_memset (padbuf, 0, 0, padlen);
331 outbuf = gst_buffer_append (outbuf, padbuf);
333 gst_rtp_buffer_unmap (&rtp);
338 /* frame not completed: store in adapter */
339 GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
342 gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len);
343 gst_adapter_push (rtph263pdepay->adapter, outbuf);
344 gst_rtp_buffer_unmap (&rtp);
350 GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
351 ("Packet payload was too small"), (NULL));
352 gst_rtp_buffer_unmap (&rtp);
357 GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
358 gst_rtp_buffer_unmap (&rtp);
363 static GstStateChangeReturn
364 gst_rtp_h263p_depay_change_state (GstElement * element,
365 GstStateChange transition)
367 GstRtpH263PDepay *rtph263pdepay;
368 GstStateChangeReturn ret;
370 rtph263pdepay = GST_RTP_H263P_DEPAY (element);
372 switch (transition) {
373 case GST_STATE_CHANGE_NULL_TO_READY:
375 case GST_STATE_CHANGE_READY_TO_PAUSED:
376 gst_adapter_clear (rtph263pdepay->adapter);
377 rtph263pdepay->wait_start = TRUE;
383 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
385 switch (transition) {
386 case GST_STATE_CHANGE_READY_TO_NULL:
395 gst_rtp_h263p_depay_plugin_init (GstPlugin * plugin)
397 return gst_element_register (plugin, "rtph263pdepay",
398 GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_DEPAY);