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