gst/rtp/: Some cleanups in the h263p (de)payloaders.
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtph263ppay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.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 
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #  include "config.h"
17 #endif
18
19 #include <string.h>
20
21 #include <gst/rtp/gstrtpbuffer.h>
22
23 #include "gstrtph263penc.h"
24
25 /* elementfactory information */
26 static GstElementDetails gst_rtp_h263penc_details = {
27   "RTP packet parser",
28   "Codec/Parser/Network",
29   "Extracts H263+ video from RTP packets",
30   "Wim Taymans <wim@fluendo.com>"
31 };
32
33 /* RtpH263PEnc signals and args */
34 enum
35 {
36   /* FILL ME */
37   LAST_SIGNAL
38 };
39
40 #define DEFAULT_MTU     1024
41
42 enum
43 {
44   PROP_0,
45   PROP_MTU
46 };
47
48 static GstStaticPadTemplate gst_rtph263penc_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("video/x-h263")
53     );
54
55 static GstStaticPadTemplate gst_rtph263penc_src_template =
56 GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp")
60     );
61
62
63 static void gst_rtph263penc_class_init (GstRtpH263PEncClass * klass);
64 static void gst_rtph263penc_base_init (GstRtpH263PEncClass * klass);
65 static void gst_rtph263penc_init (GstRtpH263PEnc * rtph263penc);
66
67 static GstFlowReturn gst_rtph263penc_chain (GstPad * pad, GstBuffer * buffer);
68
69 static void gst_rtph263penc_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec);
71 static void gst_rtph263penc_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73
74 static GstElementStateReturn gst_rtph263penc_change_state (GstElement *
75     element);
76
77 static GstElementClass *parent_class = NULL;
78
79 static GType
80 gst_rtph263penc_get_type (void)
81 {
82   static GType rtph263penc_type = 0;
83
84   if (!rtph263penc_type) {
85     static const GTypeInfo rtph263penc_info = {
86       sizeof (GstRtpH263PEncClass),
87       (GBaseInitFunc) gst_rtph263penc_base_init,
88       NULL,
89       (GClassInitFunc) gst_rtph263penc_class_init,
90       NULL,
91       NULL,
92       sizeof (GstRtpH263PEnc),
93       0,
94       (GInstanceInitFunc) gst_rtph263penc_init,
95     };
96
97     rtph263penc_type =
98         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpH263PEnc",
99         &rtph263penc_info, 0);
100   }
101   return rtph263penc_type;
102 }
103
104 static void
105 gst_rtph263penc_base_init (GstRtpH263PEncClass * klass)
106 {
107   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
108
109   gst_element_class_add_pad_template (element_class,
110       gst_static_pad_template_get (&gst_rtph263penc_src_template));
111   gst_element_class_add_pad_template (element_class,
112       gst_static_pad_template_get (&gst_rtph263penc_sink_template));
113
114   gst_element_class_set_details (element_class, &gst_rtp_h263penc_details);
115 }
116
117 static void
118 gst_rtph263penc_class_init (GstRtpH263PEncClass * klass)
119 {
120   GObjectClass *gobject_class;
121   GstElementClass *gstelement_class;
122
123   gobject_class = (GObjectClass *) klass;
124   gstelement_class = (GstElementClass *) klass;
125
126   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
127
128   gobject_class->set_property = gst_rtph263penc_set_property;
129   gobject_class->get_property = gst_rtph263penc_get_property;
130
131   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MTU,
132       g_param_spec_uint ("mtu", "MTU",
133           "Maximum size of one packet",
134           28, G_MAXUINT, DEFAULT_MTU, G_PARAM_READWRITE));
135
136   gstelement_class->change_state = gst_rtph263penc_change_state;
137 }
138
139 static void
140 gst_rtph263penc_init (GstRtpH263PEnc * rtph263penc)
141 {
142   rtph263penc->srcpad =
143       gst_pad_new_from_template (gst_static_pad_template_get
144       (&gst_rtph263penc_src_template), "src");
145   gst_element_add_pad (GST_ELEMENT (rtph263penc), rtph263penc->srcpad);
146
147   rtph263penc->sinkpad =
148       gst_pad_new_from_template (gst_static_pad_template_get
149       (&gst_rtph263penc_sink_template), "sink");
150   gst_pad_set_chain_function (rtph263penc->sinkpad, gst_rtph263penc_chain);
151   gst_element_add_pad (GST_ELEMENT (rtph263penc), rtph263penc->sinkpad);
152
153   rtph263penc->adapter = gst_adapter_new ();
154   rtph263penc->mtu = DEFAULT_MTU;
155 }
156
157 static GstFlowReturn
158 gst_rtph263penc_flush (GstRtpH263PEnc * rtph263penc)
159 {
160   guint avail;
161   GstBuffer *outbuf;
162   GstFlowReturn ret;
163   gboolean fragmented;
164
165   avail = gst_adapter_available (rtph263penc->adapter);
166   if (avail == 0)
167     return GST_FLOW_OK;
168
169   fragmented = FALSE;
170
171   while (avail > 0) {
172     guint towrite;
173     guint8 *payload;
174     guint8 *data;
175     guint payload_len;
176     gint header_len;
177
178     /* FIXME, do better mtu packing, header len etc should be
179      * included in this calculation. */
180     towrite = MIN (avail, rtph263penc->mtu);
181     /* for fragmented frames we need 2 bytes header, for other
182      * frames we must reuse the first 2 bytes of the data as the
183      * header */
184     header_len = (fragmented ? 2 : 0);
185     payload_len = header_len + towrite;
186
187     outbuf = gst_rtpbuffer_new_allocate (payload_len, 0, 0);
188     gst_rtpbuffer_set_timestamp (outbuf,
189         rtph263penc->first_ts * 90000 / GST_SECOND);
190     /* last fragment gets the marker bit set */
191     gst_rtpbuffer_set_marker (outbuf, avail > towrite ? 0 : 1);
192
193     payload = gst_rtpbuffer_get_payload (outbuf);
194
195     data = (guint8 *) gst_adapter_peek (rtph263penc->adapter, towrite);
196     memcpy (&payload[header_len], data, towrite);
197
198     /*  0                   1
199      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
200      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
201      * |   RR    |P|V|   PLEN    |PEBIT|
202      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203      */
204     payload[0] = fragmented ? 0x00 : 0x04;
205     payload[1] = 0;
206
207     GST_BUFFER_TIMESTAMP (outbuf) = rtph263penc->first_ts;
208     gst_adapter_flush (rtph263penc->adapter, towrite);
209
210     ret = gst_pad_push (rtph263penc->srcpad, outbuf);
211
212     avail -= towrite;
213     fragmented = TRUE;
214   }
215
216   return ret;
217 }
218
219 static GstFlowReturn
220 gst_rtph263penc_chain (GstPad * pad, GstBuffer * buffer)
221 {
222   GstRtpH263PEnc *rtph263penc;
223   GstFlowReturn ret;
224   guint size;
225
226   rtph263penc = GST_RTP_H263P_ENC (GST_OBJECT_PARENT (pad));
227
228   size = GST_BUFFER_SIZE (buffer);
229   rtph263penc->first_ts = GST_BUFFER_TIMESTAMP (buffer);
230
231   /* we always encode and flush a full picture */
232   gst_adapter_push (rtph263penc->adapter, buffer);
233   ret = gst_rtph263penc_flush (rtph263penc);
234
235   return ret;
236 }
237
238 static void
239 gst_rtph263penc_set_property (GObject * object, guint prop_id,
240     const GValue * value, GParamSpec * pspec)
241 {
242   GstRtpH263PEnc *rtph263penc;
243
244   rtph263penc = GST_RTP_H263P_ENC (object);
245
246   switch (prop_id) {
247     case PROP_MTU:
248       rtph263penc->mtu = g_value_get_uint (value);
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253   }
254 }
255
256 static void
257 gst_rtph263penc_get_property (GObject * object, guint prop_id, GValue * value,
258     GParamSpec * pspec)
259 {
260   GstRtpH263PEnc *rtph263penc;
261
262   rtph263penc = GST_RTP_H263P_ENC (object);
263
264   switch (prop_id) {
265     case PROP_MTU:
266       g_value_set_uint (value, rtph263penc->mtu);
267       break;
268     default:
269       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270       break;
271   }
272 }
273
274 static GstElementStateReturn
275 gst_rtph263penc_change_state (GstElement * element)
276 {
277   GstRtpH263PEnc *rtph263penc;
278   gint transition;
279   GstElementStateReturn ret;
280
281   rtph263penc = GST_RTP_H263P_ENC (element);
282   transition = GST_STATE_TRANSITION (element);
283
284   switch (transition) {
285     case GST_STATE_NULL_TO_READY:
286       break;
287     default:
288       break;
289   }
290
291   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
292
293   switch (transition) {
294     case GST_STATE_READY_TO_NULL:
295       break;
296     default:
297       break;
298   }
299   return ret;
300 }
301
302 gboolean
303 gst_rtph263penc_plugin_init (GstPlugin * plugin)
304 {
305   return gst_element_register (plugin, "rtph263penc",
306       GST_RANK_NONE, GST_TYPE_RTP_H263P_ENC);
307 }