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>
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 GEnumValue 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_enum_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 static GstStaticPadTemplate gst_rtp_h263p_pay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
67 static GstStaticPadTemplate gst_rtp_h263p_pay_src_template =
68 GST_STATIC_PAD_TEMPLATE ("src",
71 GST_STATIC_CAPS ("application/x-rtp, "
72 "media = (string) \"video\", "
73 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
74 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"; "
76 "media = (string) \"video\", "
77 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
78 "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-2000\"")
81 static void gst_rtp_h263p_pay_finalize (GObject * object);
83 static void gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
85 static void gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
88 static gboolean gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload,
90 static GstFlowReturn gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload *
91 payload, GstBuffer * buffer);
93 GST_BOILERPLATE (GstRtpH263PPay, gst_rtp_h263p_pay, GstBaseRTPPayload,
94 GST_TYPE_BASE_RTP_PAYLOAD);
97 gst_rtp_h263p_pay_base_init (gpointer klass)
99 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
101 gst_element_class_add_pad_template (element_class,
102 gst_static_pad_template_get (&gst_rtp_h263p_pay_src_template));
103 gst_element_class_add_pad_template (element_class,
104 gst_static_pad_template_get (&gst_rtp_h263p_pay_sink_template));
106 gst_element_class_set_details_simple (element_class, "RTP H263 payloader",
107 "Codec/Payloader/Network/RTP",
108 "Payload-encodes H263/+/++ video in RTP packets (RFC 4629)",
109 "Wim Taymans <wim.taymans@gmail.com>");
113 gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass)
115 GObjectClass *gobject_class;
116 GstBaseRTPPayloadClass *gstbasertppayload_class;
118 gobject_class = (GObjectClass *) klass;
119 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
121 gobject_class->finalize = gst_rtp_h263p_pay_finalize;
122 gobject_class->set_property = gst_rtp_h263p_pay_set_property;
123 gobject_class->get_property = gst_rtp_h263p_pay_get_property;
125 gstbasertppayload_class->set_caps = gst_rtp_h263p_pay_setcaps;
126 gstbasertppayload_class->handle_buffer = gst_rtp_h263p_pay_handle_buffer;
128 g_object_class_install_property (G_OBJECT_CLASS (klass),
129 PROP_FRAGMENTATION_MODE, g_param_spec_enum ("fragmentation-mode",
130 "Fragmentation Mode",
131 "Packet Fragmentation Mode", GST_TYPE_FRAGMENTATION_MODE,
132 DEFAULT_FRAGMENTATION_MODE,
133 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
135 GST_DEBUG_CATEGORY_INIT (rtph263ppay_debug, "rtph263ppay",
136 0, "rtph263ppay (RFC 4629)");
140 gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay,
141 GstRtpH263PPayClass * klass)
143 rtph263ppay->adapter = gst_adapter_new ();
145 rtph263ppay->fragmentation_mode = DEFAULT_FRAGMENTATION_MODE;
149 gst_rtp_h263p_pay_finalize (GObject * object)
151 GstRtpH263PPay *rtph263ppay;
153 rtph263ppay = GST_RTP_H263P_PAY (object);
155 g_object_unref (rtph263ppay->adapter);
156 rtph263ppay->adapter = NULL;
158 G_OBJECT_CLASS (parent_class)->finalize (object);
162 gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
166 gchar *encoding_name = NULL;
168 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
170 peercaps = gst_pad_peer_get_caps (GST_BASE_RTP_PAYLOAD_SRCPAD (payload));
172 GstCaps *intersect = gst_caps_intersect (peercaps,
173 gst_pad_get_pad_template_caps (GST_BASE_RTP_PAYLOAD_SRCPAD (payload)));
175 gst_caps_unref (peercaps);
176 if (!gst_caps_is_empty (intersect)) {
177 GstStructure *s = gst_caps_get_structure (intersect, 0);
178 encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
180 gst_caps_unref (intersect);
184 encoding_name = g_strdup ("H263-1998");
186 gst_basertppayload_set_options (payload, "video", TRUE,
187 (gchar *) encoding_name, 90000);
188 res = gst_basertppayload_set_outcaps (payload, NULL);
189 g_free (encoding_name);
195 gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
196 const GValue * value, GParamSpec * pspec)
198 GstRtpH263PPay *rtph263ppay;
200 rtph263ppay = GST_RTP_H263P_PAY (object);
203 case PROP_FRAGMENTATION_MODE:
204 rtph263ppay->fragmentation_mode = g_value_get_enum (value);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
214 GValue * value, GParamSpec * pspec)
216 GstRtpH263PPay *rtph263ppay;
218 rtph263ppay = GST_RTP_H263P_PAY (object);
221 case PROP_FRAGMENTATION_MODE:
222 g_value_set_enum (value, rtph263ppay->fragmentation_mode);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
238 avail = gst_adapter_available (rtph263ppay->adapter);
243 /* This algorithm assumes the H263/+/++ encoder sends complete frames in each
245 /* With Fragmentation Mode at GST_FRAGMENTATION_MODE_NORMAL:
246 * This algorithm implements the Follow-on packets method for packetization.
247 * This assumes low packet loss network.
248 * With Fragmentation Mode at GST_FRAGMENTATION_MODE_SYNC:
249 * This algorithm separates large frames at synchronisation points (Segments)
250 * (See RFC 4629 section 6). It would be interesting to have a property such as network
251 * quality to select between both packetization methods */
252 /* TODO Add VRC supprt (See RFC 4629 section 5.2) */
260 gboolean found_gob = FALSE;
262 if (rtph263ppay->fragmentation_mode == GST_FRAGMENTATION_MODE_SYNC) {
263 /* start after 1st gop possible */
264 guint parsed_len = 3;
265 const guint8 *parse_data = NULL;
267 parse_data = gst_adapter_peek (rtph263ppay->adapter, avail);
269 /* Check if we have a gob or eos , eossbs */
270 /* FIXME EOS and EOSSBS packets should never contain any gobs and vice-versa */
271 if (avail >= 3 && *parse_data == 0 && *(parse_data + 1) == 0
272 && *(parse_data + 2) >= 0x80) {
273 GST_DEBUG_OBJECT (rtph263ppay, " Found GOB header");
276 /* Find next and cut the packet accordingly */
277 /* TODO we should get as many gobs as possible until MTU is reached, this
278 * code seems to just get one GOB per packet */
279 while (parsed_len + 2 < avail) {
280 if (parse_data[parsed_len] == 0 && parse_data[parsed_len + 1] == 0
281 && parse_data[parsed_len + 2] >= 0x80) {
282 next_gop = parsed_len;
283 GST_DEBUG_OBJECT (rtph263ppay, " Next GOB Detected at : %d",
291 /* for picture start frames (non-fragmented), we need to remove the first
292 * two 0x00 bytes and set P=1 */
293 header_len = (fragmented && !found_gob) ? 2 : 0;
295 towrite = MIN (avail, gst_rtp_buffer_calc_payload_len
296 (GST_BASE_RTP_PAYLOAD_MTU (rtph263ppay) - header_len, 0, 0));
299 towrite = MIN (next_gop, towrite);
301 payload_len = header_len + towrite;
303 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
304 /* last fragment gets the marker bit set */
305 gst_rtp_buffer_set_marker (outbuf, avail > towrite ? 0 : 1);
307 payload = gst_rtp_buffer_get_payload (outbuf);
309 gst_adapter_copy (rtph263ppay->adapter, &payload[header_len], 0, towrite);
312 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
313 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
314 * | RR |P|V| PLEN |PEBIT|
315 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
317 /* if fragmented or gop header , write p bit =1 */
318 payload[0] = (fragmented && !found_gob) ? 0x00 : 0x04;
321 GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_timestamp;
322 GST_BUFFER_DURATION (outbuf) = rtph263ppay->first_duration;
324 gst_adapter_flush (rtph263ppay->adapter, towrite);
326 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263ppay), outbuf);
336 gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload * payload,
339 GstRtpH263PPay *rtph263ppay;
342 rtph263ppay = GST_RTP_H263P_PAY (payload);
344 rtph263ppay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
345 rtph263ppay->first_duration = GST_BUFFER_DURATION (buffer);
347 /* we always encode and flush a full picture */
348 gst_adapter_push (rtph263ppay->adapter, buffer);
349 ret = gst_rtp_h263p_pay_flush (rtph263ppay);
355 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
357 return gst_element_register (plugin, "rtph263ppay",
358 GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_PAY);