rtp: use boilerplate
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtph263ppay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtph263ppay.h"
29
30 #define DEFAULT_FRAGMENTATION_MODE   GST_FRAGMENTATION_MODE_NORMAL
31
32 enum
33 {
34   PROP_0,
35   PROP_FRAGMENTATION_MODE
36 };
37
38 #define GST_TYPE_FRAGMENTATION_MODE (gst_fragmentation_mode_get_type())
39 static GType
40 gst_fragmentation_mode_get_type (void)
41 {
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"},
46     {0, NULL, NULL},
47   };
48
49   if (!fragmentation_mode_type) {
50     fragmentation_mode_type =
51         g_enum_register_static ("GstFragmentationMode", fragmentation_mode);
52   }
53   return fragmentation_mode_type;
54 }
55
56
57 GST_DEBUG_CATEGORY_STATIC (rtph263ppay_debug);
58 #define GST_CAT_DEFAULT rtph263ppay_debug
59
60 /* elementfactory information */
61 static const GstElementDetails gst_rtp_h263ppay_details =
62 GST_ELEMENT_DETAILS ("RTP H263 payloader",
63     "Codec/Payloader/Network",
64     "Payload-encodes H263/+/++ video in RTP packets (RFC 4629)",
65     "Wim Taymans <wim.taymans@gmail.com>");
66
67 static GstStaticPadTemplate gst_rtp_h263p_pay_sink_template =
68 GST_STATIC_PAD_TEMPLATE ("sink",
69     GST_PAD_SINK,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
72     );
73
74 static GstStaticPadTemplate gst_rtp_h263p_pay_src_template =
75     GST_STATIC_PAD_TEMPLATE ("src",
76     GST_PAD_SRC,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS ("application/x-rtp, "
79         "media = (string) \"video\", "
80         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
81         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"; "
82         "application/x-rtp, "
83         "media = (string) \"video\", "
84         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
85         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-2000\"")
86     );
87
88 static void gst_rtp_h263p_pay_finalize (GObject * object);
89
90 static void gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec);
92 static void gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
93     GValue * value, GParamSpec * pspec);
94
95 static gboolean gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload,
96     GstCaps * caps);
97 static GstFlowReturn gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload *
98     payload, GstBuffer * buffer);
99
100 GST_BOILERPLATE (GstRtpH263PPay, gst_rtp_h263p_pay, GstBaseRTPPayload,
101     GST_TYPE_BASE_RTP_PAYLOAD);
102
103 static void
104 gst_rtp_h263p_pay_base_init (gpointer klass)
105 {
106   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
107
108   gst_element_class_add_pad_template (element_class,
109       gst_static_pad_template_get (&gst_rtp_h263p_pay_src_template));
110   gst_element_class_add_pad_template (element_class,
111       gst_static_pad_template_get (&gst_rtp_h263p_pay_sink_template));
112
113   gst_element_class_set_details (element_class, &gst_rtp_h263ppay_details);
114 }
115
116 static void
117 gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass)
118 {
119   GObjectClass *gobject_class;
120   GstBaseRTPPayloadClass *gstbasertppayload_class;
121
122   gobject_class = (GObjectClass *) klass;
123   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
124
125   gobject_class->finalize = gst_rtp_h263p_pay_finalize;
126   gobject_class->set_property = gst_rtp_h263p_pay_set_property;
127   gobject_class->get_property = gst_rtp_h263p_pay_get_property;
128
129   gstbasertppayload_class->set_caps = gst_rtp_h263p_pay_setcaps;
130   gstbasertppayload_class->handle_buffer = gst_rtp_h263p_pay_handle_buffer;
131
132   g_object_class_install_property (G_OBJECT_CLASS (klass),
133       PROP_FRAGMENTATION_MODE, g_param_spec_enum ("fragmentation-mode",
134           "Fragmentation Mode",
135           "Packet Fragmentation Mode", GST_TYPE_FRAGMENTATION_MODE,
136           DEFAULT_FRAGMENTATION_MODE, G_PARAM_READWRITE));
137
138   GST_DEBUG_CATEGORY_INIT (rtph263ppay_debug, "rtph263ppay",
139       0, "rtph263ppay (RFC 4629)");
140 }
141
142 static void
143 gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay,
144     GstRtpH263PPayClass * klass)
145 {
146   rtph263ppay->adapter = gst_adapter_new ();
147
148   rtph263ppay->fragmentation_mode = DEFAULT_FRAGMENTATION_MODE;
149 }
150
151 static void
152 gst_rtp_h263p_pay_finalize (GObject * object)
153 {
154   GstRtpH263PPay *rtph263ppay;
155
156   rtph263ppay = GST_RTP_H263P_PAY (object);
157
158   g_object_unref (rtph263ppay->adapter);
159   rtph263ppay->adapter = NULL;
160
161   G_OBJECT_CLASS (parent_class)->finalize (object);
162 }
163
164 static gboolean
165 gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
166 {
167   gboolean res;
168
169   gst_basertppayload_set_options (payload, "video", TRUE, "H263-1998", 90000);
170   res = gst_basertppayload_set_outcaps (payload, NULL);
171
172   return res;
173 }
174
175 static void
176 gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
177     const GValue * value, GParamSpec * pspec)
178 {
179   GstRtpH263PPay *rtph263ppay;
180
181   rtph263ppay = GST_RTP_H263P_PAY (object);
182
183   switch (prop_id) {
184     case PROP_FRAGMENTATION_MODE:
185       rtph263ppay->fragmentation_mode = g_value_get_enum (value);
186       break;
187     default:
188       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189       break;
190   }
191 }
192
193 static void
194 gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
195     GValue * value, GParamSpec * pspec)
196 {
197   GstRtpH263PPay *rtph263ppay;
198
199   rtph263ppay = GST_RTP_H263P_PAY (object);
200
201   switch (prop_id) {
202     case PROP_FRAGMENTATION_MODE:
203       g_value_set_enum (value, rtph263ppay->fragmentation_mode);
204       break;
205     default:
206       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207       break;
208   }
209 }
210
211 static GstFlowReturn
212 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
213 {
214   guint avail;
215   GstBuffer *outbuf;
216   GstFlowReturn ret;
217   gboolean fragmented;
218
219   avail = gst_adapter_available (rtph263ppay->adapter);
220   if (avail == 0)
221     return GST_FLOW_OK;
222
223   fragmented = FALSE;
224   /* This algorithm assumes the H263/+/++ encoder sends complete frames in each
225    * buffer */
226   /* With Fragmentation Mode at GST_FRAGMENTATION_MODE_NORMAL:
227    *  This algorithm implements the Follow-on packets method for packetization.
228    *  This assumes low packet loss network. 
229    * With Fragmentation Mode at GST_FRAGMENTATION_MODE_SYNC:
230    *  This algorithm separates large frames at synchronisation points (Segments)
231    *  (See RFC 4629 section 6). It would be interesting to have a property such as network
232    *  quality to select between both packetization methods */
233   /* TODO Add VRC supprt (See RFC 4629 section 5.2) */
234
235   while (avail > 0) {
236     guint towrite;
237     guint8 *payload;
238     guint8 *data;
239     guint payload_len;
240     gint header_len;
241     guint next_gop = 0;
242     gboolean found_gob = FALSE;
243
244     if (rtph263ppay->fragmentation_mode == GST_FRAGMENTATION_MODE_SYNC) {
245       /* start after 1st gop possible */
246       guint parsed_len = 3;
247       const guint8 *parse_data = NULL;
248
249       parse_data = gst_adapter_peek (rtph263ppay->adapter, avail);
250
251       /* Check if we have a gob or eos , eossbs */
252       /* FIXME EOS and EOSSBS packets should never contain any gobs and vice-versa */
253       if (avail >= 3 && *parse_data == 0 && *(parse_data + 1) == 0
254           && *(parse_data + 2) >= 0x80) {
255         GST_DEBUG_OBJECT (rtph263ppay, " Found GOB header");
256         found_gob = TRUE;
257       }
258       /* Find next and cut the packet accordingly */
259       /* TODO we should get as many gobs as possible until MTU is reached, this
260        * code seems to just get one GOB per packet */
261       while (parsed_len + 2 < avail) {
262         if (parse_data[parsed_len] == 0 && parse_data[parsed_len + 1] == 0
263             && parse_data[parsed_len + 2] >= 0x80) {
264           next_gop = parsed_len;
265           GST_DEBUG_OBJECT (rtph263ppay, " Next GOB Detected at :  %d",
266               next_gop);
267           break;
268         }
269         parsed_len++;
270       }
271     }
272
273     /* for picture start frames (non-fragmented), we need to remove the first
274      * two 0x00 bytes and set P=1 */
275     header_len = (fragmented && !found_gob) ? 2 : 0;
276
277     towrite = MIN (avail, gst_rtp_buffer_calc_payload_len
278         (GST_BASE_RTP_PAYLOAD_MTU (rtph263ppay) - header_len, 0, 0));
279
280     if (next_gop > 0)
281       towrite = MIN (next_gop, towrite);
282
283     payload_len = header_len + towrite;
284
285     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
286     /* last fragment gets the marker bit set */
287     gst_rtp_buffer_set_marker (outbuf, avail > towrite ? 0 : 1);
288
289     payload = gst_rtp_buffer_get_payload (outbuf);
290
291     data = (guint8 *) gst_adapter_peek (rtph263ppay->adapter, towrite);
292     memcpy (&payload[header_len], data, towrite);
293
294     /*  0                   1
295      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
296      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
297      * |   RR    |P|V|   PLEN    |PEBIT|
298      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
299      */
300     /* if fragmented or gop header , write p bit =1 */
301     payload[0] = (fragmented && !found_gob) ? 0x00 : 0x04;
302     payload[1] = 0;
303
304     GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_timestamp;
305     GST_BUFFER_DURATION (outbuf) = rtph263ppay->first_duration;
306
307     gst_adapter_flush (rtph263ppay->adapter, towrite);
308
309     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263ppay), outbuf);
310
311     avail -= towrite;
312     fragmented = TRUE;
313   }
314
315   return ret;
316 }
317
318 static GstFlowReturn
319 gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload * payload,
320     GstBuffer * buffer)
321 {
322   GstRtpH263PPay *rtph263ppay;
323   GstFlowReturn ret;
324
325   rtph263ppay = GST_RTP_H263P_PAY (payload);
326
327   rtph263ppay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
328   rtph263ppay->first_duration = GST_BUFFER_DURATION (buffer);
329
330   /* we always encode and flush a full picture */
331   gst_adapter_push (rtph263ppay->adapter, buffer);
332   ret = gst_rtp_h263p_pay_flush (rtph263ppay);
333
334   return ret;
335 }
336
337 gboolean
338 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
339 {
340   return gst_element_register (plugin, "rtph263ppay",
341       GST_RANK_NONE, GST_TYPE_RTP_H263P_PAY);
342 }