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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtph263pdepay.h"
29 static GstStaticPadTemplate gst_rtp_h263p_depay_src_template =
30 GST_STATIC_PAD_TEMPLATE ("src",
33 GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
36 static GstStaticPadTemplate gst_rtp_h263p_depay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_STATIC_CAPS ("application/x-rtp, "
41 "media = (string) \"video\", "
42 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
43 "clock-rate = (int) [1, MAX], "
44 "encoding-name = (string) \"H263-1998\"; "
46 /* NOTE all optional SDP params must be strings in the caps */
48 "sqcif = (string) [1, 32], "
49 "qcif = (string) [1, 32], "
50 "cif = (string) [1, 32], "
51 "cif4 = (string) [1, 32], "
52 "cif16 = (string) [1, 32], "
53 "custom = (string) ANY, "
54 "f = (string) {0, 1},"
55 "i = (string) {0, 1},"
56 "j = (string) {0, 1},"
57 "t = (string) {0, 1},"
58 "k = (string) {1, 2, 3, 4},"
59 "n = (string) {1, 2, 3, 4},"
61 "par = (string) ANY, "
62 "cpcf = (string) ANY, "
63 "bpp = (string) [0, 65536], "
64 "hrd = (string) {0, 1}; "
67 "media = (string) \"video\", "
68 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
69 "clock-rate = (int) [1, MAX], "
70 "encoding-name = (string) \"H263-2000\" "
72 /* NOTE all optional SDP params must be strings in the caps */
74 "profile = (string) [0, 10], "
75 "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
76 "interlace = (string) {0, 1};"
81 GST_BOILERPLATE (GstRtpH263PDepay, gst_rtp_h263p_depay, GstBaseRTPDepayload,
82 GST_TYPE_BASE_RTP_DEPAYLOAD);
84 static void gst_rtp_h263p_depay_finalize (GObject * object);
86 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
87 element, GstStateChange transition);
89 static GstBuffer *gst_rtp_h263p_depay_process (GstBaseRTPDepayload * depayload,
91 gboolean gst_rtp_h263p_depay_setcaps (GstBaseRTPDepayload * filter,
95 gst_rtp_h263p_depay_base_init (gpointer klass)
97 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
99 gst_element_class_add_static_pad_template (element_class,
100 &gst_rtp_h263p_depay_src_template);
101 gst_element_class_add_static_pad_template (element_class,
102 &gst_rtp_h263p_depay_sink_template);
105 gst_element_class_set_details_simple (element_class, "RTP H263 depayloader",
106 "Codec/Depayloader/Network/RTP",
107 "Extracts H263/+/++ video from RTP packets (RFC 4629)",
108 "Wim Taymans <wim.taymans@gmail.com>");
112 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
114 GObjectClass *gobject_class;
115 GstElementClass *gstelement_class;
116 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
118 gobject_class = (GObjectClass *) klass;
119 gstelement_class = (GstElementClass *) klass;
120 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
122 gstbasertpdepayload_class->process = gst_rtp_h263p_depay_process;
123 gstbasertpdepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
125 gobject_class->finalize = gst_rtp_h263p_depay_finalize;
127 gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
131 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay,
132 GstRtpH263PDepayClass * klass)
134 rtph263pdepay->adapter = gst_adapter_new ();
138 gst_rtp_h263p_depay_finalize (GObject * object)
140 GstRtpH263PDepay *rtph263pdepay;
142 rtph263pdepay = GST_RTP_H263P_DEPAY (object);
144 g_object_unref (rtph263pdepay->adapter);
145 rtph263pdepay->adapter = NULL;
147 G_OBJECT_CLASS (parent_class)->finalize (object);
151 gst_rtp_h263p_depay_setcaps (GstBaseRTPDepayload * filter, GstCaps * caps)
153 GstCaps *srccaps = NULL;
154 GstStructure *structure = gst_caps_get_structure (caps, 0);
156 const gchar *encoding_name = NULL;
159 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
160 clock_rate = 90000; /* default */
161 filter->clock_rate = clock_rate;
163 encoding_name = gst_structure_get_string (structure, "encoding-name");
164 if (encoding_name == NULL)
165 goto no_encoding_name;
167 if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
169 srccaps = gst_caps_new_simple ("video/x-h263",
170 "variant", G_TYPE_STRING, "itu",
171 "h263version", G_TYPE_STRING, "h263pp", NULL);
172 } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
173 /* this can be H263 or H263+ depending on defined appendixes in the optional
175 const gchar *F, *I, *J, *T, *K, *N, *P;
176 gboolean is_h263p = FALSE;
178 F = gst_structure_get_string (structure, "f");
180 if (g_ascii_strcasecmp (F, "1") == 0)
182 I = gst_structure_get_string (structure, "i");
184 if (g_ascii_strcasecmp (I, "1") == 0)
186 J = gst_structure_get_string (structure, "j");
188 if (g_ascii_strcasecmp (J, "1") == 0)
190 T = gst_structure_get_string (structure, "t");
192 if (g_ascii_strcasecmp (T, "1") == 0)
194 K = gst_structure_get_string (structure, "k");
197 N = gst_structure_get_string (structure, "n");
200 P = gst_structure_get_string (structure, "p");
205 srccaps = gst_caps_new_simple ("video/x-h263",
206 "variant", G_TYPE_STRING, "itu",
207 "h263version", G_TYPE_STRING, "h263p", NULL);
209 srccaps = gst_caps_new_simple ("video/x-h263",
210 "variant", G_TYPE_STRING, "itu",
211 "h263version", G_TYPE_STRING, "h263", NULL);
217 res = gst_pad_set_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (filter), srccaps);
218 gst_caps_unref (srccaps);
225 GST_ERROR_OBJECT (filter, "no encoding-name");
230 GST_ERROR_OBJECT (filter, "invalid encoding-name");
236 gst_rtp_h263p_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
238 GstRtpH263PDepay *rtph263pdepay;
241 rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
243 /* flush remaining data on discont */
244 if (GST_BUFFER_IS_DISCONT (buf)) {
245 GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
246 gst_adapter_clear (rtph263pdepay->adapter);
247 rtph263pdepay->wait_start = TRUE;
257 payload_len = gst_rtp_buffer_get_payload_len (buf);
258 payload = gst_rtp_buffer_get_payload (buf);
262 if (payload_len < header_len)
265 M = gst_rtp_buffer_get_marker (buf);
268 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
269 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270 * | RR |P|V| PLEN |PEBIT|
271 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
273 P = (payload[0] & 0x04) == 0x04;
274 V = (payload[0] & 0x02) == 0x02;
275 PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
276 PEBIT = payload[1] & 0x7;
278 GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
288 if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
292 /* FIXME, have to make the packet writable hear. Better to reset these
293 * bytes when we copy the packet below */
294 rtph263pdepay->wait_start = FALSE;
296 payload[header_len] = 0;
297 payload[header_len + 1] = 0;
300 if (rtph263pdepay->wait_start)
303 if (payload_len < header_len)
306 /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
307 /* FIXME actually use the RTP picture header when it is lost in the network */
308 /* for now strip off header */
309 payload += header_len;
310 payload_len -= header_len;
313 /* frame is completed: append to previous, push it out */
317 GST_LOG_OBJECT (depayload, "Frame complete");
319 avail = gst_adapter_available (rtph263pdepay->adapter);
321 len = avail + payload_len;
322 padlen = (len % 4) + 4;
323 outbuf = gst_buffer_new_and_alloc (len + padlen);
324 memset (GST_BUFFER_DATA (outbuf) + len, 0, padlen);
325 GST_BUFFER_SIZE (outbuf) = len;
327 /* prepend previous data */
329 gst_adapter_copy (rtph263pdepay->adapter, GST_BUFFER_DATA (outbuf), 0,
331 gst_adapter_flush (rtph263pdepay->adapter, avail);
333 memcpy (GST_BUFFER_DATA (outbuf) + avail, payload, payload_len);
338 /* frame not completed: store in adapter */
339 outbuf = gst_buffer_new_and_alloc (payload_len);
341 GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
343 memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
345 gst_adapter_push (rtph263pdepay->adapter, outbuf);
352 GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
353 ("Packet payload was too small"), (NULL));
358 GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
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);