various: fix pad template leaks
[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 #include <stdlib.h>
26 #include <stdio.h>
27
28 #include <gst/rtp/gstrtpbuffer.h>
29
30 #include "gstrtph263ppay.h"
31
32 #define DEFAULT_FRAGMENTATION_MODE   GST_FRAGMENTATION_MODE_NORMAL
33
34 enum
35 {
36   PROP_0,
37   PROP_FRAGMENTATION_MODE
38 };
39
40 #define GST_TYPE_FRAGMENTATION_MODE (gst_fragmentation_mode_get_type())
41 static GType
42 gst_fragmentation_mode_get_type (void)
43 {
44   static GType fragmentation_mode_type = 0;
45   static const GEnumValue fragmentation_mode[] = {
46     {GST_FRAGMENTATION_MODE_NORMAL, "Normal", "normal"},
47     {GST_FRAGMENTATION_MODE_SYNC, "Fragment at sync points", "sync"},
48     {0, NULL, NULL},
49   };
50
51   if (!fragmentation_mode_type) {
52     fragmentation_mode_type =
53         g_enum_register_static ("GstFragmentationMode", fragmentation_mode);
54   }
55   return fragmentation_mode_type;
56 }
57
58
59 GST_DEBUG_CATEGORY_STATIC (rtph263ppay_debug);
60 #define GST_CAT_DEFAULT rtph263ppay_debug
61
62 static GstStaticPadTemplate gst_rtp_h263p_pay_sink_template =
63 GST_STATIC_PAD_TEMPLATE ("sink",
64     GST_PAD_SINK,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\"")
67     );
68
69 /*
70  * We also return these in getcaps() as required by the SDP caps
71  *
72  * width = (int) [16, 4096]
73  * height = (int) [16, 4096]
74  * "annex-f = (boolean) {true, false},"
75  * "annex-i = (boolean) {true, false},"
76  * "annex-j = (boolean) {true, false},"
77  * "annex-l = (boolean) {true, false},"
78  * "annex-t = (boolean) {true, false},"
79  * "annex-v = (boolean) {true, false}")
80  */
81
82
83 static GstStaticPadTemplate gst_rtp_h263p_pay_src_template =
84     GST_STATIC_PAD_TEMPLATE ("src",
85     GST_PAD_SRC,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS ("application/x-rtp, "
88         "media = (string) \"video\", "
89         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
90         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"; "
91         "application/x-rtp, "
92         "media = (string) \"video\", "
93         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
94         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-2000\"")
95     );
96
97 static void gst_rtp_h263p_pay_finalize (GObject * object);
98
99 static void gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
100     const GValue * value, GParamSpec * pspec);
101 static void gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
102     GValue * value, GParamSpec * pspec);
103
104 static gboolean gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload,
105     GstCaps * caps);
106 static GstCaps *gst_rtp_h263p_pay_sink_getcaps (GstBaseRTPPayload * payload,
107     GstPad * pad);
108 static GstFlowReturn gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload *
109     payload, GstBuffer * buffer);
110
111 GST_BOILERPLATE (GstRtpH263PPay, gst_rtp_h263p_pay, GstBaseRTPPayload,
112     GST_TYPE_BASE_RTP_PAYLOAD);
113
114 static void
115 gst_rtp_h263p_pay_base_init (gpointer klass)
116 {
117   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
118
119   gst_element_class_add_static_pad_template (element_class,
120       &gst_rtp_h263p_pay_src_template);
121   gst_element_class_add_static_pad_template (element_class,
122       &gst_rtp_h263p_pay_sink_template);
123
124   gst_element_class_set_details_simple (element_class, "RTP H263 payloader",
125       "Codec/Payloader/Network/RTP",
126       "Payload-encodes H263/+/++ video in RTP packets (RFC 4629)",
127       "Wim Taymans <wim.taymans@gmail.com>");
128 }
129
130 static void
131 gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass)
132 {
133   GObjectClass *gobject_class;
134   GstBaseRTPPayloadClass *gstbasertppayload_class;
135
136   gobject_class = (GObjectClass *) klass;
137   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
138
139   gobject_class->finalize = gst_rtp_h263p_pay_finalize;
140   gobject_class->set_property = gst_rtp_h263p_pay_set_property;
141   gobject_class->get_property = gst_rtp_h263p_pay_get_property;
142
143   gstbasertppayload_class->set_caps = gst_rtp_h263p_pay_setcaps;
144   gstbasertppayload_class->get_caps = gst_rtp_h263p_pay_sink_getcaps;
145   gstbasertppayload_class->handle_buffer = gst_rtp_h263p_pay_handle_buffer;
146
147   g_object_class_install_property (G_OBJECT_CLASS (klass),
148       PROP_FRAGMENTATION_MODE, g_param_spec_enum ("fragmentation-mode",
149           "Fragmentation Mode",
150           "Packet Fragmentation Mode", GST_TYPE_FRAGMENTATION_MODE,
151           DEFAULT_FRAGMENTATION_MODE,
152           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153
154   GST_DEBUG_CATEGORY_INIT (rtph263ppay_debug, "rtph263ppay",
155       0, "rtph263ppay (RFC 4629)");
156 }
157
158 static void
159 gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay,
160     GstRtpH263PPayClass * klass)
161 {
162   rtph263ppay->adapter = gst_adapter_new ();
163
164   rtph263ppay->fragmentation_mode = DEFAULT_FRAGMENTATION_MODE;
165 }
166
167 static void
168 gst_rtp_h263p_pay_finalize (GObject * object)
169 {
170   GstRtpH263PPay *rtph263ppay;
171
172   rtph263ppay = GST_RTP_H263P_PAY (object);
173
174   g_object_unref (rtph263ppay->adapter);
175   rtph263ppay->adapter = NULL;
176
177   G_OBJECT_CLASS (parent_class)->finalize (object);
178 }
179
180 static gboolean
181 gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
182 {
183   gboolean res;
184   GstCaps *peercaps;
185   gchar *encoding_name = NULL;
186
187   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
188
189   peercaps = gst_pad_peer_get_caps (GST_BASE_RTP_PAYLOAD_SRCPAD (payload));
190   if (peercaps) {
191     GstCaps *intersect = gst_caps_intersect (peercaps,
192         gst_pad_get_pad_template_caps (GST_BASE_RTP_PAYLOAD_SRCPAD (payload)));
193
194     gst_caps_unref (peercaps);
195     if (!gst_caps_is_empty (intersect)) {
196       GstStructure *s = gst_caps_get_structure (intersect, 0);
197       encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
198     }
199     gst_caps_unref (intersect);
200   }
201
202   if (!encoding_name)
203     encoding_name = g_strdup ("H263-1998");
204
205   gst_basertppayload_set_options (payload, "video", TRUE,
206       (gchar *) encoding_name, 90000);
207   res = gst_basertppayload_set_outcaps (payload, NULL);
208   g_free (encoding_name);
209
210   return res;
211 }
212
213 static void
214 caps_append (GstCaps * caps, GstStructure * in_s, guint x, guint y, guint mpi)
215 {
216   GstStructure *s;
217
218   if (!in_s)
219     return;
220
221   if (mpi < 1 || mpi > 32)
222     return;
223
224   s = gst_structure_copy (in_s);
225
226   gst_structure_set (s,
227       "width", GST_TYPE_INT_RANGE, 1, x,
228       "height", GST_TYPE_INT_RANGE, 1, y,
229       "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 30000, 1001 * mpi, NULL);
230
231   gst_caps_merge_structure (caps, s);
232 }
233
234
235 static GstCaps *
236 gst_rtp_h263p_pay_sink_getcaps (GstBaseRTPPayload * payload, GstPad * pad)
237 {
238   GstRtpH263PPay *rtph263ppay;
239   GstCaps *caps = gst_caps_new_empty ();
240   GstCaps *peercaps = NULL;
241   GstCaps *intersect = NULL;
242   guint i;
243
244   rtph263ppay = GST_RTP_H263P_PAY (payload);
245
246   peercaps = gst_pad_peer_get_caps (GST_BASE_RTP_PAYLOAD_SRCPAD (payload));
247   if (!peercaps)
248     return
249         gst_caps_copy (gst_pad_get_pad_template_caps
250         (GST_BASE_RTP_PAYLOAD_SINKPAD (payload)));
251
252   intersect = gst_caps_intersect (peercaps,
253       gst_pad_get_pad_template_caps (GST_BASE_RTP_PAYLOAD_SRCPAD (payload)));
254   gst_caps_unref (peercaps);
255
256   if (gst_caps_is_empty (intersect))
257     return intersect;
258
259   for (i = 0; i < gst_caps_get_size (intersect); i++) {
260     GstStructure *s = gst_caps_get_structure (intersect, i);
261     const gchar *encoding_name = gst_structure_get_string (s, "encoding-name");
262
263     if (!strcmp (encoding_name, "H263-2000")) {
264       const gchar *profile_str = gst_structure_get_string (s, "profile");
265       const gchar *level_str = gst_structure_get_string (s, "level");
266       int profile = 0;
267       int level = 0;
268
269       if (profile_str && level_str) {
270         gboolean i = FALSE, j = FALSE, l = FALSE, t = FALSE, f = FALSE,
271             v = FALSE;
272         GstStructure *new_s = gst_structure_new ("video/x-h263",
273             "variant", G_TYPE_STRING, "itu",
274             NULL);
275
276         profile = atoi (profile_str);
277         level = atoi (level_str);
278
279         /* These profiles are defined in the H.263 Annex X */
280         switch (profile) {
281           case 0:
282             /* The Baseline Profile (Profile 0) */
283             break;
284           case 1:
285             /* H.320 Coding Efficiency Version 2 Backward-Compatibility Profile
286              * (Profile 1)
287              * Baseline + Annexes I, J, L.4 and T
288              */
289             i = j = l = t = TRUE;
290             break;
291           case 2:
292             /* Version 1 Backward-Compatibility Profile (Profile 2)
293              * Baseline + Annex F
294              */
295             i = j = l = t = f = TRUE;
296             break;
297           case 3:
298             /* Version 2 Interactive and Streaming Wireless Profile
299              * Baseline + Annexes I, J, T
300              */
301             i = j = t = TRUE;
302             break;
303           case 4:
304             /* Version 3 Interactive and Streaming Wireless Profile (Profile 4)
305              * Baseline + Annexes I, J, T, V, W.6.3.8,
306              */
307             /* Missing W.6.3.8 */
308             i = j = t = v = TRUE;
309             break;
310           case 5:
311             /* Conversational High Compression Profile (Profile 5)
312              * Baseline + Annexes F, I, J, L.4, T, D, U
313              */
314             /* Missing D, U */
315             f = i = j = l = t = TRUE;
316             break;
317           case 6:
318             /* Conversational Internet Profile (Profile 6)
319              * Baseline + Annexes F, I, J, L.4, T, D, U and
320              * K with arbitratry slice ordering
321              */
322             /* Missing D, U, K with arbitratry slice ordering */
323             f = i = j = l = t = TRUE;
324             break;
325           case 7:
326             /* Conversational Interlace Profile (Profile 7)
327              * Baseline + Annexes F, I, J, L.4, T, D, U,  W.6.3.11
328              */
329             /* Missing D, U, W.6.3.11 */
330             f = i = j = l = t = TRUE;
331             break;
332           case 8:
333             /* High Latency Profile (Profile 8)
334              * Baseline + Annexes F, I, J, L.4, T, D, U, P.5, O.1.1 and
335              * K with arbitratry slice ordering
336              */
337             /* Missing D, U, P.5, O.1.1 */
338             f = i = j = l = t = TRUE;
339             break;
340         }
341
342
343         if (f || i || j || t || l || v) {
344           GValue list = { 0 };
345           GValue vstr = { 0 };
346
347           g_value_init (&list, GST_TYPE_LIST);
348           g_value_init (&vstr, G_TYPE_STRING);
349
350           g_value_set_static_string (&vstr, "h263");
351           gst_value_list_append_value (&list, &vstr);
352           g_value_set_static_string (&vstr, "h263p");
353           gst_value_list_append_value (&list, &vstr);
354
355           if (l || v) {
356             g_value_set_static_string (&vstr, "h263pp");
357             gst_value_list_append_value (&list, &vstr);
358           }
359           g_value_unset (&vstr);
360
361           gst_structure_set_value (new_s, "h263version", &list);
362           g_value_unset (&list);
363         } else {
364           gst_structure_set (new_s, "h263version", G_TYPE_STRING, "h263", NULL);
365         }
366
367
368         if (!f)
369           gst_structure_set (new_s, "annex-f", G_TYPE_BOOLEAN, FALSE, NULL);
370         if (!i)
371           gst_structure_set (new_s, "annex-i", G_TYPE_BOOLEAN, FALSE, NULL);
372         if (!j)
373           gst_structure_set (new_s, "annex-j", G_TYPE_BOOLEAN, FALSE, NULL);
374         if (!t)
375           gst_structure_set (new_s, "annex-t", G_TYPE_BOOLEAN, FALSE, NULL);
376         if (!l)
377           gst_structure_set (new_s, "annex-l", G_TYPE_BOOLEAN, FALSE, NULL);
378         if (!v)
379           gst_structure_set (new_s, "annex-v", G_TYPE_BOOLEAN, FALSE, NULL);
380
381
382         if (level <= 10 || level == 45) {
383           gst_structure_set (new_s,
384               "width", GST_TYPE_INT_RANGE, 1, 176,
385               "height", GST_TYPE_INT_RANGE, 1, 144,
386               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 30000, 2002, NULL);
387           gst_caps_merge_structure (caps, new_s);
388         } else if (level <= 20) {
389           GstStructure *s_copy = gst_structure_copy (new_s);
390
391           gst_structure_set (new_s,
392               "width", GST_TYPE_INT_RANGE, 1, 352,
393               "height", GST_TYPE_INT_RANGE, 1, 288,
394               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 30000, 2002, NULL);
395           gst_caps_merge_structure (caps, new_s);
396
397           gst_structure_set (s_copy,
398               "width", GST_TYPE_INT_RANGE, 1, 176,
399               "height", GST_TYPE_INT_RANGE, 1, 144,
400               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 30000, 1001, NULL);
401           gst_caps_merge_structure (caps, s_copy);
402         } else if (level <= 40) {
403
404           gst_structure_set (new_s,
405               "width", GST_TYPE_INT_RANGE, 1, 352,
406               "height", GST_TYPE_INT_RANGE, 1, 288,
407               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 30000, 1001, NULL);
408           gst_caps_merge_structure (caps, new_s);
409         } else if (level <= 50) {
410           GstStructure *s_copy = gst_structure_copy (new_s);
411
412           gst_structure_set (new_s,
413               "width", GST_TYPE_INT_RANGE, 1, 352,
414               "height", GST_TYPE_INT_RANGE, 1, 288,
415               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 50, 1, NULL);
416           gst_caps_merge_structure (caps, new_s);
417
418           gst_structure_set (s_copy,
419               "width", GST_TYPE_INT_RANGE, 1, 352,
420               "height", GST_TYPE_INT_RANGE, 1, 240,
421               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 60000, 1001, NULL);
422           gst_caps_merge_structure (caps, s_copy);
423         } else if (level <= 60) {
424           GstStructure *s_copy = gst_structure_copy (new_s);
425
426           gst_structure_set (new_s,
427               "width", GST_TYPE_INT_RANGE, 1, 720,
428               "height", GST_TYPE_INT_RANGE, 1, 288,
429               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 50, 1, NULL);
430           gst_caps_merge_structure (caps, new_s);
431
432           gst_structure_set (s_copy,
433               "width", GST_TYPE_INT_RANGE, 1, 720,
434               "height", GST_TYPE_INT_RANGE, 1, 240,
435               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 60000, 1001, NULL);
436           gst_caps_merge_structure (caps, s_copy);
437         } else if (level <= 70) {
438           GstStructure *s_copy = gst_structure_copy (new_s);
439
440           gst_structure_set (new_s,
441               "width", GST_TYPE_INT_RANGE, 1, 720,
442               "height", GST_TYPE_INT_RANGE, 1, 576,
443               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 50, 1, NULL);
444           gst_caps_merge_structure (caps, new_s);
445
446           gst_structure_set (s_copy,
447               "width", GST_TYPE_INT_RANGE, 1, 720,
448               "height", GST_TYPE_INT_RANGE, 1, 480,
449               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 60000, 1001, NULL);
450           gst_caps_merge_structure (caps, s_copy);
451         } else {
452           gst_caps_merge_structure (caps, new_s);
453         }
454
455       } else {
456         GstStructure *new_s = gst_structure_new ("video/x-h263",
457             "variant", G_TYPE_STRING, "itu",
458             "h263version", G_TYPE_STRING, "h263",
459             NULL);
460
461         GST_DEBUG_OBJECT (rtph263ppay, "No profile or level specified"
462             " for H263-2000, defaulting to baseline H263");
463
464         gst_caps_merge_structure (caps, new_s);
465       }
466     } else {
467       gboolean f = FALSE, i = FALSE, j = FALSE, t = FALSE;
468       /* FIXME: ffmpeg support the Appendix K too, how do we express it ?
469        *   guint k;
470        */
471       const gchar *str;
472       GstStructure *new_s = gst_structure_new ("video/x-h263",
473           "variant", G_TYPE_STRING, "itu",
474           NULL);
475       gboolean added = FALSE;
476
477       str = gst_structure_get_string (s, "f");
478       if (str && !strcmp (str, "1"))
479         f = TRUE;
480
481       str = gst_structure_get_string (s, "i");
482       if (str && !strcmp (str, "1"))
483         i = TRUE;
484
485       str = gst_structure_get_string (s, "j");
486       if (str && !strcmp (str, "1"))
487         j = TRUE;
488
489       str = gst_structure_get_string (s, "t");
490       if (str && !strcmp (str, "1"))
491         t = TRUE;
492
493       if (f || i || j || t) {
494         GValue list = { 0 };
495         GValue vstr = { 0 };
496
497         g_value_init (&list, GST_TYPE_LIST);
498         g_value_init (&vstr, G_TYPE_STRING);
499
500         g_value_set_static_string (&vstr, "h263");
501         gst_value_list_append_value (&list, &vstr);
502         g_value_set_static_string (&vstr, "h263p");
503         gst_value_list_append_value (&list, &vstr);
504         g_value_unset (&vstr);
505
506         gst_structure_set_value (new_s, "h263version", &list);
507         g_value_unset (&list);
508       } else {
509         gst_structure_set (new_s, "h263version", G_TYPE_STRING, "h263", NULL);
510       }
511
512       if (!f)
513         gst_structure_set (new_s, "annex-f", G_TYPE_BOOLEAN, FALSE, NULL);
514       if (!i)
515         gst_structure_set (new_s, "annex-i", G_TYPE_BOOLEAN, FALSE, NULL);
516       if (!j)
517         gst_structure_set (new_s, "annex-j", G_TYPE_BOOLEAN, FALSE, NULL);
518       if (!t)
519         gst_structure_set (new_s, "annex-t", G_TYPE_BOOLEAN, FALSE, NULL);
520
521
522       str = gst_structure_get_string (s, "custom");
523       if (str) {
524         unsigned int xmax, ymax, mpi;
525         if (sscanf (str, "%u,%u,%u", &xmax, &ymax, &mpi) == 3) {
526           if (xmax % 4 && ymax % 4 && mpi >= 1 && mpi <= 32) {
527             caps_append (caps, new_s, xmax, ymax, mpi);
528             added = TRUE;
529           } else {
530             GST_WARNING_OBJECT (rtph263ppay, "Invalid custom framesize/MPI"
531                 " %u x %u at %u, ignoring", xmax, ymax, mpi);
532           }
533         } else {
534           GST_WARNING_OBJECT (rtph263ppay, "Invalid custom framesize/MPI: %s,"
535               " ignoring", str);
536         }
537       }
538
539       str = gst_structure_get_string (s, "16cif");
540       if (str) {
541         int mpi = atoi (str);
542         caps_append (caps, new_s, 1408, 1152, mpi);
543         added = TRUE;
544       }
545
546       str = gst_structure_get_string (s, "4cif");
547       if (str) {
548         int mpi = atoi (str);
549         caps_append (caps, new_s, 704, 576, mpi);
550         added = TRUE;
551       }
552
553       str = gst_structure_get_string (s, "cif");
554       if (str) {
555         int mpi = atoi (str);
556         caps_append (caps, new_s, 352, 288, mpi);
557         added = TRUE;
558       }
559
560       str = gst_structure_get_string (s, "qcif");
561       if (str) {
562         int mpi = atoi (str);
563         caps_append (caps, new_s, 176, 144, mpi);
564         added = TRUE;
565       }
566
567       str = gst_structure_get_string (s, "sqcif");
568       if (str) {
569         int mpi = atoi (str);
570         caps_append (caps, new_s, 128, 96, mpi);
571         added = TRUE;
572       }
573
574       if (added)
575         gst_structure_free (new_s);
576       else
577         gst_caps_merge_structure (caps, new_s);
578     }
579   }
580
581   gst_caps_unref (intersect);
582
583   return caps;
584 }
585
586
587 static void
588 gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
589     const GValue * value, GParamSpec * pspec)
590 {
591   GstRtpH263PPay *rtph263ppay;
592
593   rtph263ppay = GST_RTP_H263P_PAY (object);
594
595   switch (prop_id) {
596     case PROP_FRAGMENTATION_MODE:
597       rtph263ppay->fragmentation_mode = g_value_get_enum (value);
598       break;
599     default:
600       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
601       break;
602   }
603 }
604
605 static void
606 gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
607     GValue * value, GParamSpec * pspec)
608 {
609   GstRtpH263PPay *rtph263ppay;
610
611   rtph263ppay = GST_RTP_H263P_PAY (object);
612
613   switch (prop_id) {
614     case PROP_FRAGMENTATION_MODE:
615       g_value_set_enum (value, rtph263ppay->fragmentation_mode);
616       break;
617     default:
618       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619       break;
620   }
621 }
622
623 static GstFlowReturn
624 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
625 {
626   guint avail;
627   GstBuffer *outbuf;
628   GstFlowReturn ret;
629   gboolean fragmented;
630
631   avail = gst_adapter_available (rtph263ppay->adapter);
632   if (avail == 0)
633     return GST_FLOW_OK;
634
635   fragmented = FALSE;
636   /* This algorithm assumes the H263/+/++ encoder sends complete frames in each
637    * buffer */
638   /* With Fragmentation Mode at GST_FRAGMENTATION_MODE_NORMAL:
639    *  This algorithm implements the Follow-on packets method for packetization.
640    *  This assumes low packet loss network. 
641    * With Fragmentation Mode at GST_FRAGMENTATION_MODE_SYNC:
642    *  This algorithm separates large frames at synchronisation points (Segments)
643    *  (See RFC 4629 section 6). It would be interesting to have a property such as network
644    *  quality to select between both packetization methods */
645   /* TODO Add VRC supprt (See RFC 4629 section 5.2) */
646
647   while (avail > 0) {
648     guint towrite;
649     guint8 *payload;
650     guint payload_len;
651     gint header_len;
652     guint next_gop = 0;
653     gboolean found_gob = FALSE;
654
655     if (rtph263ppay->fragmentation_mode == GST_FRAGMENTATION_MODE_SYNC) {
656       /* start after 1st gop possible */
657       guint parsed_len = 3;
658       const guint8 *parse_data = NULL;
659
660       parse_data = gst_adapter_peek (rtph263ppay->adapter, avail);
661
662       /* Check if we have a gob or eos , eossbs */
663       /* FIXME EOS and EOSSBS packets should never contain any gobs and vice-versa */
664       if (avail >= 3 && *parse_data == 0 && *(parse_data + 1) == 0
665           && *(parse_data + 2) >= 0x80) {
666         GST_DEBUG_OBJECT (rtph263ppay, " Found GOB header");
667         found_gob = TRUE;
668       }
669       /* Find next and cut the packet accordingly */
670       /* TODO we should get as many gobs as possible until MTU is reached, this
671        * code seems to just get one GOB per packet */
672       while (parsed_len + 2 < avail) {
673         if (parse_data[parsed_len] == 0 && parse_data[parsed_len + 1] == 0
674             && parse_data[parsed_len + 2] >= 0x80) {
675           next_gop = parsed_len;
676           GST_DEBUG_OBJECT (rtph263ppay, " Next GOB Detected at :  %d",
677               next_gop);
678           break;
679         }
680         parsed_len++;
681       }
682     }
683
684     /* for picture start frames (non-fragmented), we need to remove the first
685      * two 0x00 bytes and set P=1 */
686     header_len = (fragmented && !found_gob) ? 2 : 0;
687
688     towrite = MIN (avail, gst_rtp_buffer_calc_payload_len
689         (GST_BASE_RTP_PAYLOAD_MTU (rtph263ppay) - header_len, 0, 0));
690
691     if (next_gop > 0)
692       towrite = MIN (next_gop, towrite);
693
694     payload_len = header_len + towrite;
695
696     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
697     /* last fragment gets the marker bit set */
698     gst_rtp_buffer_set_marker (outbuf, avail > towrite ? 0 : 1);
699
700     payload = gst_rtp_buffer_get_payload (outbuf);
701
702     gst_adapter_copy (rtph263ppay->adapter, &payload[header_len], 0, towrite);
703
704     /*  0                   1
705      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
706      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
707      * |   RR    |P|V|   PLEN    |PEBIT|
708      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
709      */
710     /* if fragmented or gop header , write p bit =1 */
711     payload[0] = (fragmented && !found_gob) ? 0x00 : 0x04;
712     payload[1] = 0;
713
714     GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_timestamp;
715     GST_BUFFER_DURATION (outbuf) = rtph263ppay->first_duration;
716
717     gst_adapter_flush (rtph263ppay->adapter, towrite);
718
719     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263ppay), outbuf);
720
721     avail -= towrite;
722     fragmented = TRUE;
723   }
724
725   return ret;
726 }
727
728 static GstFlowReturn
729 gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload * payload,
730     GstBuffer * buffer)
731 {
732   GstRtpH263PPay *rtph263ppay;
733   GstFlowReturn ret;
734
735   rtph263ppay = GST_RTP_H263P_PAY (payload);
736
737   rtph263ppay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
738   rtph263ppay->first_duration = GST_BUFFER_DURATION (buffer);
739
740   /* we always encode and flush a full picture */
741   gst_adapter_push (rtph263ppay->adapter, buffer);
742   ret = gst_rtp_h263p_pay_flush (rtph263ppay);
743
744   return ret;
745 }
746
747 gboolean
748 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
749 {
750   return gst_element_register (plugin, "rtph263ppay",
751       GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_PAY);
752 }