Remove unused variables in _class_init
[platform/upstream/gstreamer.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_class_init (GstRtpH263PPayClass * klass);
89 static void gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass);
90 static void gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay);
91 static void gst_rtp_h263p_pay_finalize (GObject * object);
92
93 static void gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
94     const GValue * value, GParamSpec * pspec);
95 static void gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
96     GValue * value, GParamSpec * pspec);
97
98 static gboolean gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload,
99     GstCaps * caps);
100 static GstFlowReturn gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload *
101     payload, GstBuffer * buffer);
102
103 static GstBaseRTPPayloadClass *parent_class = NULL;
104
105 static GType
106 gst_rtp_h263p_pay_get_type (void)
107 {
108   static GType rtph263ppay_type = 0;
109
110   if (!rtph263ppay_type) {
111     static const GTypeInfo rtph263ppay_info = {
112       sizeof (GstRtpH263PPayClass),
113       (GBaseInitFunc) gst_rtp_h263p_pay_base_init,
114       NULL,
115       (GClassInitFunc) gst_rtp_h263p_pay_class_init,
116       NULL,
117       NULL,
118       sizeof (GstRtpH263PPay),
119       0,
120       (GInstanceInitFunc) gst_rtp_h263p_pay_init,
121     };
122
123     rtph263ppay_type =
124         g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpH263PPay",
125         &rtph263ppay_info, 0);
126   }
127   return rtph263ppay_type;
128 }
129
130 static void
131 gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass)
132 {
133   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
134
135   gst_element_class_add_pad_template (element_class,
136       gst_static_pad_template_get (&gst_rtp_h263p_pay_src_template));
137   gst_element_class_add_pad_template (element_class,
138       gst_static_pad_template_get (&gst_rtp_h263p_pay_sink_template));
139
140   gst_element_class_set_details (element_class, &gst_rtp_h263ppay_details);
141 }
142
143 static void
144 gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass)
145 {
146   GObjectClass *gobject_class;
147   GstBaseRTPPayloadClass *gstbasertppayload_class;
148
149   gobject_class = (GObjectClass *) klass;
150   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
151
152   parent_class = g_type_class_peek_parent (klass);
153
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;
157
158   gstbasertppayload_class->set_caps = gst_rtp_h263p_pay_setcaps;
159   gstbasertppayload_class->handle_buffer = gst_rtp_h263p_pay_handle_buffer;
160
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));
166
167   GST_DEBUG_CATEGORY_INIT (rtph263ppay_debug, "rtph263ppay",
168       0, "rtph263ppay (RFC 4629)");
169 }
170
171 static void
172 gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay)
173 {
174   rtph263ppay->adapter = gst_adapter_new ();
175
176   rtph263ppay->fragmentation_mode = DEFAULT_FRAGMENTATION_MODE;
177 }
178
179 static void
180 gst_rtp_h263p_pay_finalize (GObject * object)
181 {
182   GstRtpH263PPay *rtph263ppay;
183
184   rtph263ppay = GST_RTP_H263P_PAY (object);
185
186   g_object_unref (rtph263ppay->adapter);
187   rtph263ppay->adapter = NULL;
188
189   G_OBJECT_CLASS (parent_class)->finalize (object);
190 }
191
192 static gboolean
193 gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
194 {
195   gboolean res;
196
197   gst_basertppayload_set_options (payload, "video", TRUE, "H263-1998", 90000);
198   res = gst_basertppayload_set_outcaps (payload, NULL);
199
200   return res;
201 }
202
203 static void
204 gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
205     const GValue * value, GParamSpec * pspec)
206 {
207   GstRtpH263PPay *rtph263ppay;
208
209   rtph263ppay = GST_RTP_H263P_PAY (object);
210
211   switch (prop_id) {
212     case PROP_FRAGMENTATION_MODE:
213       rtph263ppay->fragmentation_mode = g_value_get_enum (value);
214       break;
215     default:
216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217       break;
218   }
219 }
220
221 static void
222 gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
223     GValue * value, GParamSpec * pspec)
224 {
225   GstRtpH263PPay *rtph263ppay;
226
227   rtph263ppay = GST_RTP_H263P_PAY (object);
228
229   switch (prop_id) {
230     case PROP_FRAGMENTATION_MODE:
231       g_value_set_enum (value, rtph263ppay->fragmentation_mode);
232       break;
233     default:
234       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235       break;
236   }
237 }
238
239 static GstFlowReturn
240 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
241 {
242   guint avail;
243   GstBuffer *outbuf;
244   GstFlowReturn ret;
245   gboolean fragmented;
246
247   avail = gst_adapter_available (rtph263ppay->adapter);
248   if (avail == 0)
249     return GST_FLOW_OK;
250
251   fragmented = FALSE;
252   /* This algorithm assumes the H263/+/++ encoder sends complete frames in each
253    * buffer */
254   /* With Fragmentation Mode at GST_FRAGMENTATION_MODE_NORMAL:
255    *  This algorithm implements the Follow-on packets method for packetization.
256    *  This assumes low packet loss network. 
257    * With Fragmentation Mode at GST_FRAGMENTATION_MODE_SYNC:
258    *  This algorithm separates large frames at synchronisation points (Segments)
259    *  (See RFC 4629 section 6). It would be interesting to have a property such as network
260    *  quality to select between both packetization methods */
261   /* TODO Add VRC supprt (See RFC 4629 section 5.2) */
262
263   while (avail > 0) {
264     guint towrite;
265     guint8 *payload;
266     guint8 *data;
267     guint payload_len;
268     gint header_len;
269     guint next_gop = 0;
270     gboolean found_gob = FALSE;
271
272     if (rtph263ppay->fragmentation_mode == GST_FRAGMENTATION_MODE_SYNC) {
273       /* start after 1st gop possible */
274       guint parsed_len = 3;
275       const guint8 *parse_data = NULL;
276
277       parse_data = gst_adapter_peek (rtph263ppay->adapter, avail);
278
279       /* Check if we have a gob or eos , eossbs */
280       /* FIXME EOS and EOSSBS packets should never contain any gobs and vice-versa */
281       if (avail >= 3 && *parse_data == 0 && *(parse_data + 1) == 0
282           && *(parse_data + 2) >= 0x80) {
283         GST_DEBUG_OBJECT (rtph263ppay, " Found GOB header");
284         found_gob = TRUE;
285       }
286       /* Find next and cut the packet accordingly */
287       /* TODO we should get as many gobs as possible until MTU is reached, this
288        * code seems to just get one GOB per packet */
289       while (parsed_len + 2 < avail) {
290         if (parse_data[parsed_len] == 0 && parse_data[parsed_len + 1] == 0
291             && parse_data[parsed_len + 2] >= 0x80) {
292           next_gop = parsed_len;
293           GST_DEBUG_OBJECT (rtph263ppay, " Next GOB Detected at :  %d",
294               next_gop);
295           break;
296         }
297         parsed_len++;
298       }
299     }
300
301     /* for picture start frames (non-fragmented), we need to remove the first
302      * two 0x00 bytes and set P=1 */
303     header_len = (fragmented && !found_gob) ? 2 : 0;
304
305     towrite = MIN (avail, gst_rtp_buffer_calc_payload_len
306         (GST_BASE_RTP_PAYLOAD_MTU (rtph263ppay) - header_len, 0, 0));
307
308     if (next_gop > 0)
309       towrite = MIN (next_gop, towrite);
310
311     payload_len = header_len + towrite;
312
313     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
314     /* last fragment gets the marker bit set */
315     gst_rtp_buffer_set_marker (outbuf, avail > towrite ? 0 : 1);
316
317     payload = gst_rtp_buffer_get_payload (outbuf);
318
319     data = (guint8 *) gst_adapter_peek (rtph263ppay->adapter, towrite);
320     memcpy (&payload[header_len], data, towrite);
321
322     /*  0                   1
323      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
324      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
325      * |   RR    |P|V|   PLEN    |PEBIT|
326      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
327      */
328     /* if fragmented or gop header , write p bit =1 */
329     payload[0] = (fragmented && !found_gob) ? 0x00 : 0x04;
330     payload[1] = 0;
331
332     GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_timestamp;
333     GST_BUFFER_DURATION (outbuf) = rtph263ppay->first_duration;
334
335     gst_adapter_flush (rtph263ppay->adapter, towrite);
336
337     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263ppay), outbuf);
338
339     avail -= towrite;
340     fragmented = TRUE;
341   }
342
343   return ret;
344 }
345
346 static GstFlowReturn
347 gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload * payload,
348     GstBuffer * buffer)
349 {
350   GstRtpH263PPay *rtph263ppay;
351   GstFlowReturn ret;
352   guint size;
353
354   rtph263ppay = GST_RTP_H263P_PAY (payload);
355
356   size = GST_BUFFER_SIZE (buffer);
357   rtph263ppay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
358   rtph263ppay->first_duration = GST_BUFFER_DURATION (buffer);
359
360   /* we always encode and flush a full picture */
361   gst_adapter_push (rtph263ppay->adapter, buffer);
362   ret = gst_rtp_h263p_pay_flush (rtph263ppay);
363
364   return ret;
365 }
366
367 gboolean
368 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
369 {
370   return gst_element_register (plugin, "rtph263ppay",
371       GST_RANK_NONE, GST_TYPE_RTP_H263P_PAY);
372 }