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 #define DEFAULT_FRAGMENTATION_MODE GST_FRAGMENTATION_MODE_NORMAL
35 PROP_FRAGMENTATION_MODE
38 #define GST_TYPE_FRAGMENTATION_MODE (gst_fragmentation_mode_get_type())
40 gst_fragmentation_mode_get_type (void)
42 static GType fragmentation_mode_type = 0;
43 static const GFlagsValue fragmentation_mode[] = {
44 {GST_FRAGMENTATION_MODE_NORMAL, "Normal", "normal"},
45 {GST_FRAGMENTATION_MODE_SYNC, "Fragment at sync points", "sync"},
49 if (!fragmentation_mode_type) {
50 fragmentation_mode_type =
51 g_flags_register_static ("GstFragmentationMode", fragmentation_mode);
53 return fragmentation_mode_type;
57 GST_DEBUG_CATEGORY_STATIC (rtph263ppay_debug);
58 #define GST_CAT_DEFAULT rtph263ppay_debug
60 /* elementfactory information */
61 static const GstElementDetails gst_rtp_h263ppay_details =
62 GST_ELEMENT_DETAILS ("RTP packet payloader",
63 "Codec/Payloader/Network",
64 "Payload-encodes H263+ video in RTP packets (RFC 2429)",
65 "Wim Taymans <wim@fluendo.com>");
67 static GstStaticPadTemplate gst_rtp_h263p_pay_sink_template =
68 GST_STATIC_PAD_TEMPLATE ("sink",
71 GST_STATIC_CAPS ("video/x-h263, "
72 "variant = (string) \"itu\", " "h263version = (string) \"h263p\"")
75 static GstStaticPadTemplate gst_rtp_h263p_pay_src_template =
76 GST_STATIC_PAD_TEMPLATE ("src",
79 GST_STATIC_CAPS ("application/x-rtp, "
80 "media = (string) \"video\", "
81 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
82 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"")
85 static void gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass);
86 static void gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass);
87 static void gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay);
88 static void gst_rtp_h263p_pay_finalize (GObject * object);
90 static void gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
93 static void gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 static gboolean gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload,
98 static GstFlowReturn gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload *
99 payload, GstBuffer * buffer);
101 static GstBaseRTPPayloadClass *parent_class = NULL;
104 gst_rtp_h263p_pay_get_type (void)
106 static GType rtph263ppay_type = 0;
108 if (!rtph263ppay_type) {
109 static const GTypeInfo rtph263ppay_info = {
110 sizeof (GstRtpH263PPayClass),
111 (GBaseInitFunc) gst_rtp_h263p_pay_base_init,
113 (GClassInitFunc) gst_rtp_h263p_pay_class_init,
116 sizeof (GstRtpH263PPay),
118 (GInstanceInitFunc) gst_rtp_h263p_pay_init,
122 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpH263PPay",
123 &rtph263ppay_info, 0);
125 return rtph263ppay_type;
129 gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass)
131 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
133 gst_element_class_add_pad_template (element_class,
134 gst_static_pad_template_get (&gst_rtp_h263p_pay_src_template));
135 gst_element_class_add_pad_template (element_class,
136 gst_static_pad_template_get (&gst_rtp_h263p_pay_sink_template));
138 gst_element_class_set_details (element_class, &gst_rtp_h263ppay_details);
142 gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass)
144 GObjectClass *gobject_class;
145 GstElementClass *gstelement_class;
146 GstBaseRTPPayloadClass *gstbasertppayload_class;
148 gobject_class = (GObjectClass *) klass;
149 gstelement_class = (GstElementClass *) klass;
150 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
152 parent_class = g_type_class_peek_parent (klass);
154 gobject_class->finalize = gst_rtp_h263p_pay_finalize;
155 gobject_class->set_property = gst_rtp_h263p_pay_set_property;
156 gobject_class->get_property = gst_rtp_h263p_pay_get_property;
158 gstbasertppayload_class->set_caps = gst_rtp_h263p_pay_setcaps;
159 gstbasertppayload_class->handle_buffer = gst_rtp_h263p_pay_handle_buffer;
161 g_object_class_install_property (G_OBJECT_CLASS (klass),
162 PROP_FRAGMENTATION_MODE, g_param_spec_enum ("fragmentation-mode",
163 "Fragmentation Mode",
164 "Packet Fragmentation Mode", GST_TYPE_FRAGMENTATION_MODE,
165 DEFAULT_FRAGMENTATION_MODE, G_PARAM_READWRITE));
167 GST_DEBUG_CATEGORY_INIT (rtph263ppay_debug, "rtph263ppay",
168 0, "rtph263ppay (RFC 2429)");
173 gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay)
175 rtph263ppay->adapter = gst_adapter_new ();
177 rtph263ppay->fragmentation_mode = DEFAULT_FRAGMENTATION_MODE;
181 gst_rtp_h263p_pay_finalize (GObject * object)
183 GstRtpH263PPay *rtph263ppay;
185 rtph263ppay = GST_RTP_H263P_PAY (object);
187 g_object_unref (rtph263ppay->adapter);
188 rtph263ppay->adapter = NULL;
190 G_OBJECT_CLASS (parent_class)->finalize (object);
194 gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
196 gst_basertppayload_set_options (payload, "video", TRUE, "H263-1998", 90000);
197 gst_basertppayload_set_outcaps (payload, NULL);
203 gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
204 const GValue * value, GParamSpec * pspec)
206 GstRtpH263PPay *rtph263ppay;
208 rtph263ppay = GST_RTP_H263P_PAY (object);
211 case PROP_FRAGMENTATION_MODE:
212 rtph263ppay->fragmentation_mode = g_value_get_enum (value);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
222 GValue * value, GParamSpec * pspec)
224 GstRtpH263PPay *rtph263ppay;
226 rtph263ppay = GST_RTP_H263P_PAY (object);
229 case PROP_FRAGMENTATION_MODE:
230 g_value_set_enum (value, rtph263ppay->fragmentation_mode);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
248 avail = gst_adapter_available (rtph263ppay->adapter);
253 /* This algorithm assumes the H263+ encoder sends complete frames in each
255 /* With Fragmentation Mode at GST_FRAGMENTATION_MODE_NORMAL:
256 * This algorithm implements the Follow-on packets method for packetization.
257 * This assumes low packet loss network.
258 * With Fragmentation Mode at GST_FRAGMENTATION_MODE_SYNC:
259 * This algorithm separates large frames at synchronisation points (Segments)
260 * (See RFC 2429 section 6). It would be interesting to have a property such as network
261 * quality to select between both packetization methods */
262 /* TODO Add VRC supprt (See RFC 2429 section 4.2) */
271 gboolean found_gob = FALSE;
273 if (rtph263ppay->fragmentation_mode == GST_FRAGMENTATION_MODE_SYNC) {
274 /* start after 1st gop possible */
275 guint parsed_len = 3;
276 const guint8 *parse_data = NULL;
278 parse_data = gst_adapter_peek (rtph263ppay->adapter, avail);
280 /* Check if we have a gob or eos , eossbs */
281 if (avail >= 3 && *parse_data == 0 && *(parse_data + 1) == 0
282 && *(parse_data + 2) >= 0x80) {
283 GST_DEBUG_OBJECT (rtph263ppay, " Found GOB header");
286 /* Find next and cut the packet accordingly */
287 while (parsed_len + 2 < avail) {
288 if (parse_data[parsed_len] == 0 && parse_data[parsed_len + 1] == 0
289 && parse_data[parsed_len + 2] >= 0x80) {
290 next_gop = parsed_len;
291 GST_DEBUG_OBJECT (rtph263ppay, " Next GOB Detected at : %d",
299 /* for picture start frames (non-fragmented), we need to remove the first
300 * two 0x00 bytes and set P=1 */
301 header_len = (fragmented && !found_gob) ? 2 : 0;
303 towrite = MIN (avail, gst_rtp_buffer_calc_payload_len
304 (GST_BASE_RTP_PAYLOAD_MTU (rtph263ppay) - header_len, 0, 0));
307 towrite = MIN (next_gop, towrite);
309 payload_len = header_len + towrite;
311 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
312 /* last fragment gets the marker bit set */
313 gst_rtp_buffer_set_marker (outbuf, avail > towrite ? 0 : 1);
315 payload = gst_rtp_buffer_get_payload (outbuf);
317 data = (guint8 *) gst_adapter_peek (rtph263ppay->adapter, towrite);
318 memcpy (&payload[header_len], data, towrite);
321 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
322 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
323 * | RR |P|V| PLEN |PEBIT|
324 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
326 /* if fragmented or gop header , write p bit =1 */
327 payload[0] = (fragmented && !found_gob) ? 0x00 : 0x04;
330 GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_ts;
331 gst_adapter_flush (rtph263ppay->adapter, towrite);
333 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263ppay), outbuf);
343 gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload * payload,
346 GstRtpH263PPay *rtph263ppay;
350 rtph263ppay = GST_RTP_H263P_PAY (payload);
352 size = GST_BUFFER_SIZE (buffer);
353 rtph263ppay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
355 /* we always encode and flush a full picture */
356 gst_adapter_push (rtph263ppay->adapter, buffer);
357 ret = gst_rtp_h263p_pay_flush (rtph263ppay);
363 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
365 return gst_element_register (plugin, "rtph263ppay",
366 GST_RANK_NONE, GST_TYPE_RTP_H263P_PAY);