2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.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
21 #include <gst/rtp/gstrtpbuffer.h>
22 #include "gstrtph263pdec.h"
24 /* elementfactory information */
25 static GstElementDetails gst_rtp_h263pdec_details = {
27 "Codec/Parser/Network",
28 "Extracts H263+ video from RTP packets (RFC 2429)",
29 "Wim Taymans <wim@fluendo.com>"
32 /* RtpH263PDec signals and args */
45 static GstStaticPadTemplate gst_rtph263pdec_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("video/x-h263")
52 static GstStaticPadTemplate gst_rtph263pdec_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS ("application/x-rtp, "
57 "media = (string) \"video\", "
58 "payload = (int) [ 96, 255 ], "
59 "clock_rate = (int) 90000, " "encoding_name = (string) \"H263-1998\"")
63 static void gst_rtph263pdec_class_init (GstRtpH263PDecClass * klass);
64 static void gst_rtph263pdec_base_init (GstRtpH263PDecClass * klass);
65 static void gst_rtph263pdec_init (GstRtpH263PDec * rtph263pdec);
66 static void gst_rtph263pdec_finalize (GObject * object);
68 static GstFlowReturn gst_rtph263pdec_chain (GstPad * pad, GstBuffer * buffer);
70 static void gst_rtph263pdec_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_rtph263pdec_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
75 static GstStateChangeReturn gst_rtph263pdec_change_state (GstElement *
76 element, GstStateChange transition);
78 static GstElementClass *parent_class = NULL;
81 gst_rtph263pdec_get_type (void)
83 static GType rtph263pdec_type = 0;
85 if (!rtph263pdec_type) {
86 static const GTypeInfo rtph263pdec_info = {
87 sizeof (GstRtpH263PDecClass),
88 (GBaseInitFunc) gst_rtph263pdec_base_init,
90 (GClassInitFunc) gst_rtph263pdec_class_init,
93 sizeof (GstRtpH263PDec),
95 (GInstanceInitFunc) gst_rtph263pdec_init,
99 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpH263PDec",
100 &rtph263pdec_info, 0);
102 return rtph263pdec_type;
106 gst_rtph263pdec_base_init (GstRtpH263PDecClass * klass)
108 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&gst_rtph263pdec_src_template));
112 gst_element_class_add_pad_template (element_class,
113 gst_static_pad_template_get (&gst_rtph263pdec_sink_template));
115 gst_element_class_set_details (element_class, &gst_rtp_h263pdec_details);
119 gst_rtph263pdec_class_init (GstRtpH263PDecClass * klass)
121 GObjectClass *gobject_class;
122 GstElementClass *gstelement_class;
124 gobject_class = (GObjectClass *) klass;
125 gstelement_class = (GstElementClass *) klass;
127 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
129 gobject_class->finalize = gst_rtph263pdec_finalize;
131 gobject_class->set_property = gst_rtph263pdec_set_property;
132 gobject_class->get_property = gst_rtph263pdec_get_property;
134 gstelement_class->change_state = gst_rtph263pdec_change_state;
138 gst_rtph263pdec_init (GstRtpH263PDec * rtph263pdec)
140 rtph263pdec->srcpad =
141 gst_pad_new_from_template (gst_static_pad_template_get
142 (&gst_rtph263pdec_src_template), "src");
143 gst_element_add_pad (GST_ELEMENT (rtph263pdec), rtph263pdec->srcpad);
145 rtph263pdec->sinkpad =
146 gst_pad_new_from_template (gst_static_pad_template_get
147 (&gst_rtph263pdec_sink_template), "sink");
148 gst_pad_set_chain_function (rtph263pdec->sinkpad, gst_rtph263pdec_chain);
149 gst_element_add_pad (GST_ELEMENT (rtph263pdec), rtph263pdec->sinkpad);
151 rtph263pdec->adapter = gst_adapter_new ();
155 gst_rtph263pdec_finalize (GObject * object)
157 GstRtpH263PDec *rtph263pdec;
159 rtph263pdec = GST_RTP_H263P_DEC (object);
161 g_object_unref (rtph263pdec->adapter);
162 rtph263pdec->adapter = NULL;
164 G_OBJECT_CLASS (parent_class)->finalize (object);
168 gst_rtph263pdec_chain (GstPad * pad, GstBuffer * buf)
170 GstRtpH263PDec *rtph263pdec;
174 /* GstRTPPayload pt; */
176 rtph263pdec = GST_RTP_H263P_DEC (GST_OBJECT_PARENT (pad));
178 if (!gst_rtpbuffer_validate (buf))
182 if ((pt = gst_rtpbuffer_get_payload_type (buf)) != 0)
194 payload_len = gst_rtpbuffer_get_payload_len (buf);
195 payload = gst_rtpbuffer_get_payload (buf);
199 M = gst_rtpbuffer_get_marker (buf);
200 P = (payload[0] & 0x04) == 0x04;
201 V = (payload[0] & 0x02) == 0x02;
202 PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
213 payload[header_len] = 0;
214 payload[header_len + 1] = 0;
217 /* strip off header */
218 payload += header_len;
219 payload_len -= header_len;
221 timestamp = gst_rtpbuffer_get_timestamp (buf);
224 /* frame is completed: append to previous, push it out */
228 avail = gst_adapter_available (rtph263pdec->adapter);
230 outbuf = gst_buffer_new_and_alloc (avail + payload_len);
232 /* prepend previous data */
234 data = (guint8 *) gst_adapter_peek (rtph263pdec->adapter, avail);
235 memcpy (GST_BUFFER_DATA (outbuf), data, avail);
236 gst_adapter_flush (rtph263pdec->adapter, avail);
238 memcpy (GST_BUFFER_DATA (outbuf) + avail, payload, payload_len);
240 GST_BUFFER_TIMESTAMP (outbuf) = timestamp * GST_SECOND / 90000;
241 gst_buffer_set_caps (outbuf,
242 (GstCaps *) gst_pad_get_pad_template_caps (rtph263pdec->srcpad));
244 ret = gst_pad_push (rtph263pdec->srcpad, outbuf);
246 /* frame not completed: store in adapter */
247 outbuf = gst_buffer_new_and_alloc (payload_len);
249 memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
251 gst_adapter_push (rtph263pdec->adapter, outbuf);
256 gst_buffer_unref (buf);
263 GST_DEBUG ("Packet does not validate");
264 gst_buffer_unref (buf);
265 return GST_FLOW_ERROR;
270 GST_DEBUG ("Unexpected payload type %u", pt);
272 gst_buffer_unref (buf);
273 return GST_FLOW_ERROR;
279 gst_rtph263pdec_set_property (GObject * object, guint prop_id,
280 const GValue * value, GParamSpec * pspec)
282 GstRtpH263PDec *rtph263pdec;
284 rtph263pdec = GST_RTP_H263P_DEC (object);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 gst_rtph263pdec_get_property (GObject * object, guint prop_id, GValue * value,
297 GstRtpH263PDec *rtph263pdec;
299 rtph263pdec = GST_RTP_H263P_DEC (object);
303 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 static GstStateChangeReturn
309 gst_rtph263pdec_change_state (GstElement * element, GstStateChange transition)
311 GstRtpH263PDec *rtph263pdec;
312 GstStateChangeReturn ret;
314 rtph263pdec = GST_RTP_H263P_DEC (element);
316 switch (transition) {
317 case GST_STATE_CHANGE_NULL_TO_READY:
319 case GST_STATE_CHANGE_READY_TO_PAUSED:
320 gst_adapter_clear (rtph263pdec->adapter);
326 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
328 switch (transition) {
329 case GST_STATE_CHANGE_READY_TO_NULL:
338 gst_rtph263pdec_plugin_init (GstPlugin * plugin)
340 return gst_element_register (plugin, "rtph263pdec",
341 GST_RANK_NONE, GST_TYPE_RTP_H263P_DEC);