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 "gstrtph263pdepay.h"
29 #include "gstrtputils.h"
31 GST_DEBUG_CATEGORY_STATIC (rtph263pdepay_debug);
32 #define GST_CAT_DEFAULT (rtph263pdepay_debug)
34 static GstStaticPadTemplate gst_rtp_h263p_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
38 GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
41 static GstStaticPadTemplate gst_rtp_h263p_depay_sink_template =
42 GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("application/x-rtp, "
46 "media = (string) \"video\", "
47 "clock-rate = (int) [1, MAX], "
48 "encoding-name = (string) \"H263-1998\"; "
50 /* NOTE all optional SDP params must be strings in the caps */
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},"
65 "par = (string) ANY, "
66 "cpcf = (string) ANY, "
67 "bpp = (string) [0, 65536], "
68 "hrd = (string) {0, 1}; "
71 "media = (string) \"video\", "
72 "clock-rate = (int) [1, MAX], "
73 "encoding-name = (string) \"H263-2000\" "
75 /* NOTE all optional SDP params must be strings in the caps */
77 "profile = (string) [0, 10], "
78 "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
79 "interlace = (string) {0, 1};"
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);
88 static void gst_rtp_h263p_depay_finalize (GObject * object);
90 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
91 element, GstStateChange transition);
93 static GstBuffer *gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
95 gboolean gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter,
99 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
101 GObjectClass *gobject_class;
102 GstElementClass *gstelement_class;
103 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
105 gobject_class = (GObjectClass *) klass;
106 gstelement_class = (GstElementClass *) klass;
107 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
109 gobject_class->finalize = gst_rtp_h263p_depay_finalize;
111 gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
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);
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>");
123 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h263p_depay_process;
124 gstrtpbasedepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
126 GST_DEBUG_CATEGORY_INIT (rtph263pdepay_debug, "rtph263pdepay", 0,
127 "H263+ Video RTP Depayloader");
131 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay)
133 rtph263pdepay->adapter = gst_adapter_new ();
137 gst_rtp_h263p_depay_finalize (GObject * object)
139 GstRtpH263PDepay *rtph263pdepay;
141 rtph263pdepay = GST_RTP_H263P_DEPAY (object);
143 g_object_unref (rtph263pdepay->adapter);
144 rtph263pdepay->adapter = NULL;
146 G_OBJECT_CLASS (parent_class)->finalize (object);
150 gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
152 GstCaps *srccaps = NULL;
153 GstStructure *structure = gst_caps_get_structure (caps, 0);
155 const gchar *encoding_name = NULL;
158 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
159 clock_rate = 90000; /* default */
160 filter->clock_rate = clock_rate;
162 encoding_name = gst_structure_get_string (structure, "encoding-name");
163 if (encoding_name == NULL)
164 goto no_encoding_name;
166 if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
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
174 const gchar *F, *I, *J, *T, *K, *N, *P;
175 gboolean is_h263p = FALSE;
177 F = gst_structure_get_string (structure, "f");
179 if (g_ascii_strcasecmp (F, "1") == 0)
181 I = gst_structure_get_string (structure, "i");
183 if (g_ascii_strcasecmp (I, "1") == 0)
185 J = gst_structure_get_string (structure, "j");
187 if (g_ascii_strcasecmp (J, "1") == 0)
189 T = gst_structure_get_string (structure, "t");
191 if (g_ascii_strcasecmp (T, "1") == 0)
193 K = gst_structure_get_string (structure, "k");
196 N = gst_structure_get_string (structure, "n");
199 P = gst_structure_get_string (structure, "p");
204 srccaps = gst_caps_new_simple ("video/x-h263",
205 "variant", G_TYPE_STRING, "itu",
206 "h263version", G_TYPE_STRING, "h263p", NULL);
208 srccaps = gst_caps_new_simple ("video/x-h263",
209 "variant", G_TYPE_STRING, "itu",
210 "h263version", G_TYPE_STRING, "h263", NULL);
216 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
217 gst_caps_unref (srccaps);
224 GST_ERROR_OBJECT (filter, "no encoding-name");
229 GST_ERROR_OBJECT (filter, "invalid encoding-name");
235 gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
238 GstRtpH263PDepay *rtph263pdepay;
246 rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
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;
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);
334 gst_rtp_drop_meta (GST_ELEMENT_CAST (rtph263pdepay), outbuf,
335 g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
339 /* frame not completed: store in adapter */
340 GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
343 gst_rtp_buffer_get_payload_subbuffer (rtp, header_len, payload_len);
344 gst_adapter_push (rtph263pdepay->adapter, outbuf);
350 GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
351 ("Packet payload was too small"), (NULL));
356 GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
361 static GstStateChangeReturn
362 gst_rtp_h263p_depay_change_state (GstElement * element,
363 GstStateChange transition)
365 GstRtpH263PDepay *rtph263pdepay;
366 GstStateChangeReturn ret;
368 rtph263pdepay = GST_RTP_H263P_DEPAY (element);
370 switch (transition) {
371 case GST_STATE_CHANGE_NULL_TO_READY:
373 case GST_STATE_CHANGE_READY_TO_PAUSED:
374 gst_adapter_clear (rtph263pdepay->adapter);
375 rtph263pdepay->wait_start = TRUE;
381 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
383 switch (transition) {
384 case GST_STATE_CHANGE_READY_TO_NULL:
393 gst_rtp_h263p_depay_plugin_init (GstPlugin * plugin)
395 return gst_element_register (plugin, "rtph263pdepay",
396 GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_DEPAY);