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 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>
28 #include "gstrtph263ppay.h"
30 /* elementfactory information */
31 static const GstElementDetails gst_rtp_h263ppay_details =
32 GST_ELEMENT_DETAILS ("RTP packet parser",
33 "Codec/Payloader/Network",
34 "Payload-encodes H263+ video in RTP packets (RFC 2429)",
35 "Wim Taymans <wim@fluendo.com>");
37 static GstStaticPadTemplate gst_rtp_h263p_pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
41 GST_STATIC_CAPS ("video/x-h263")
44 static GstStaticPadTemplate gst_rtp_h263p_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "media = (string) \"video\", "
50 "payload = (int) [ 96, 127 ], "
51 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"")
54 static void gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass);
55 static void gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass);
56 static void gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay);
57 static void gst_rtp_h263p_pay_finalize (GObject * object);
59 static gboolean gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload,
61 static GstFlowReturn gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload *
62 payload, GstBuffer * buffer);
64 static GstBaseRTPPayloadClass *parent_class = NULL;
67 gst_rtp_h263p_pay_get_type (void)
69 static GType rtph263ppay_type = 0;
71 if (!rtph263ppay_type) {
72 static const GTypeInfo rtph263ppay_info = {
73 sizeof (GstRtpH263PPayClass),
74 (GBaseInitFunc) gst_rtp_h263p_pay_base_init,
76 (GClassInitFunc) gst_rtp_h263p_pay_class_init,
79 sizeof (GstRtpH263PPay),
81 (GInstanceInitFunc) gst_rtp_h263p_pay_init,
85 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpH263PPay",
86 &rtph263ppay_info, 0);
88 return rtph263ppay_type;
92 gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass)
94 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
96 gst_element_class_add_pad_template (element_class,
97 gst_static_pad_template_get (&gst_rtp_h263p_pay_src_template));
98 gst_element_class_add_pad_template (element_class,
99 gst_static_pad_template_get (&gst_rtp_h263p_pay_sink_template));
101 gst_element_class_set_details (element_class, &gst_rtp_h263ppay_details);
105 gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass)
107 GObjectClass *gobject_class;
108 GstElementClass *gstelement_class;
109 GstBaseRTPPayloadClass *gstbasertppayload_class;
111 gobject_class = (GObjectClass *) klass;
112 gstelement_class = (GstElementClass *) klass;
113 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
115 parent_class = g_type_class_peek_parent (klass);
117 gobject_class->finalize = gst_rtp_h263p_pay_finalize;
119 gstbasertppayload_class->set_caps = gst_rtp_h263p_pay_setcaps;
120 gstbasertppayload_class->handle_buffer = gst_rtp_h263p_pay_handle_buffer;
124 gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay)
126 rtph263ppay->adapter = gst_adapter_new ();
130 gst_rtp_h263p_pay_finalize (GObject * object)
132 GstRtpH263PPay *rtph263ppay;
134 rtph263ppay = GST_RTP_H263P_PAY (object);
136 g_object_unref (rtph263ppay->adapter);
137 rtph263ppay->adapter = NULL;
139 G_OBJECT_CLASS (parent_class)->finalize (object);
143 gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
145 gst_basertppayload_set_options (payload, "video", TRUE, "H263-1998", 90000);
146 gst_basertppayload_set_outcaps (payload, NULL);
153 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
160 avail = gst_adapter_available (rtph263ppay->adapter);
173 /* FIXME, do better mtu packing, header len etc should be
174 * included in this calculation. */
175 towrite = MIN (avail, GST_BASE_RTP_PAYLOAD_MTU (rtph263ppay));
176 /* for fragmented frames we need 2 bytes header, for other
177 * frames we must reuse the first 2 bytes of the data as the
179 header_len = (fragmented ? 2 : 0);
180 payload_len = header_len + towrite;
182 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
183 /* last fragment gets the marker bit set */
184 gst_rtp_buffer_set_marker (outbuf, avail > towrite ? 0 : 1);
186 payload = gst_rtp_buffer_get_payload (outbuf);
188 data = (guint8 *) gst_adapter_peek (rtph263ppay->adapter, towrite);
189 memcpy (&payload[header_len], data, towrite);
192 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
193 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
194 * | RR |P|V| PLEN |PEBIT|
195 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
197 payload[0] = fragmented ? 0x00 : 0x04;
200 GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_ts;
201 gst_adapter_flush (rtph263ppay->adapter, towrite);
203 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263ppay), outbuf);
213 gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload * payload,
216 GstRtpH263PPay *rtph263ppay;
220 rtph263ppay = GST_RTP_H263P_PAY (payload);
222 size = GST_BUFFER_SIZE (buffer);
223 rtph263ppay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
225 /* we always encode and flush a full picture */
226 gst_adapter_push (rtph263ppay->adapter, buffer);
227 ret = gst_rtp_h263p_pay_flush (rtph263ppay);
233 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
235 return gst_element_register (plugin, "rtph263ppay",
236 GST_RANK_NONE, GST_TYPE_RTP_H263P_PAY);