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