rtph263ppay: accept any h263 input unless downstream forces specific requirements
[platform/upstream/gst-plugins-good.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     return
252         gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SINKPAD (payload));
253
254   /* We basically need to differentiate two use-cases here: One where there's
255    * a capsfilter after the payloader with caps created from an SDP; in this
256    * case the filter caps are fixed and we want to signal to an encoder what
257    * we want it to produce. The second case is simply payloader ! depayloader
258    * where we are dealing with the depayloader's template caps. In this case
259    * we should accept any input compatible with our sink template caps. */
260   if (!gst_caps_is_fixed (peercaps))
261     return
262         gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SINKPAD (payload));
263
264   templ = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
265   intersect = gst_caps_intersect (peercaps, templ);
266   gst_caps_unref (peercaps);
267   gst_caps_unref (templ);
268
269   if (gst_caps_is_empty (intersect))
270     return intersect;
271
272   caps = gst_caps_new_empty ();
273   for (i = 0; i < gst_caps_get_size (intersect); i++) {
274     GstStructure *s = gst_caps_get_structure (intersect, i);
275     const gchar *encoding_name = gst_structure_get_string (s, "encoding-name");
276
277     if (!strcmp (encoding_name, "H263-2000")) {
278       const gchar *profile_str = gst_structure_get_string (s, "profile");
279       const gchar *level_str = gst_structure_get_string (s, "level");
280       int profile = 0;
281       int level = 0;
282
283       if (profile_str && level_str) {
284         gboolean i = FALSE, j = FALSE, l = FALSE, t = FALSE, f = FALSE,
285             v = FALSE;
286         GstStructure *new_s = gst_structure_new ("video/x-h263",
287             "variant", G_TYPE_STRING, "itu",
288             NULL);
289
290         profile = atoi (profile_str);
291         level = atoi (level_str);
292
293         /* These profiles are defined in the H.263 Annex X */
294         switch (profile) {
295           case 0:
296             /* The Baseline Profile (Profile 0) */
297             break;
298           case 1:
299             /* H.320 Coding Efficiency Version 2 Backward-Compatibility Profile
300              * (Profile 1)
301              * Baseline + Annexes I, J, L.4 and T
302              */
303             i = j = l = t = TRUE;
304             break;
305           case 2:
306             /* Version 1 Backward-Compatibility Profile (Profile 2)
307              * Baseline + Annex F
308              */
309             i = j = l = t = f = TRUE;
310             break;
311           case 3:
312             /* Version 2 Interactive and Streaming Wireless Profile
313              * Baseline + Annexes I, J, T
314              */
315             i = j = t = TRUE;
316             break;
317           case 4:
318             /* Version 3 Interactive and Streaming Wireless Profile (Profile 4)
319              * Baseline + Annexes I, J, T, V, W.6.3.8,
320              */
321             /* Missing W.6.3.8 */
322             i = j = t = v = TRUE;
323             break;
324           case 5:
325             /* Conversational High Compression Profile (Profile 5)
326              * Baseline + Annexes F, I, J, L.4, T, D, U
327              */
328             /* Missing D, U */
329             f = i = j = l = t = TRUE;
330             break;
331           case 6:
332             /* Conversational Internet Profile (Profile 6)
333              * Baseline + Annexes F, I, J, L.4, T, D, U and
334              * K with arbitratry slice ordering
335              */
336             /* Missing D, U, K with arbitratry slice ordering */
337             f = i = j = l = t = TRUE;
338             break;
339           case 7:
340             /* Conversational Interlace Profile (Profile 7)
341              * Baseline + Annexes F, I, J, L.4, T, D, U,  W.6.3.11
342              */
343             /* Missing D, U, W.6.3.11 */
344             f = i = j = l = t = TRUE;
345             break;
346           case 8:
347             /* High Latency Profile (Profile 8)
348              * Baseline + Annexes F, I, J, L.4, T, D, U, P.5, O.1.1 and
349              * K with arbitratry slice ordering
350              */
351             /* Missing D, U, P.5, O.1.1 */
352             f = i = j = l = t = TRUE;
353             break;
354         }
355
356
357         if (f || i || j || t || l || v) {
358           GValue list = { 0 };
359           GValue vstr = { 0 };
360
361           g_value_init (&list, GST_TYPE_LIST);
362           g_value_init (&vstr, G_TYPE_STRING);
363
364           g_value_set_static_string (&vstr, "h263");
365           gst_value_list_append_value (&list, &vstr);
366           g_value_set_static_string (&vstr, "h263p");
367           gst_value_list_append_value (&list, &vstr);
368
369           if (l || v) {
370             g_value_set_static_string (&vstr, "h263pp");
371             gst_value_list_append_value (&list, &vstr);
372           }
373           g_value_unset (&vstr);
374
375           gst_structure_set_value (new_s, "h263version", &list);
376           g_value_unset (&list);
377         } else {
378           gst_structure_set (new_s, "h263version", G_TYPE_STRING, "h263", NULL);
379         }
380
381
382         if (!f)
383           gst_structure_set (new_s, "annex-f", G_TYPE_BOOLEAN, FALSE, NULL);
384         if (!i)
385           gst_structure_set (new_s, "annex-i", G_TYPE_BOOLEAN, FALSE, NULL);
386         if (!j)
387           gst_structure_set (new_s, "annex-j", G_TYPE_BOOLEAN, FALSE, NULL);
388         if (!t)
389           gst_structure_set (new_s, "annex-t", G_TYPE_BOOLEAN, FALSE, NULL);
390         if (!l)
391           gst_structure_set (new_s, "annex-l", G_TYPE_BOOLEAN, FALSE, NULL);
392         if (!v)
393           gst_structure_set (new_s, "annex-v", G_TYPE_BOOLEAN, FALSE, NULL);
394
395
396         if (level <= 10 || level == 45) {
397           gst_structure_set (new_s,
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, 2002, NULL);
401           caps = gst_caps_merge_structure (caps, new_s);
402         } else if (level <= 20) {
403           GstStructure *s_copy = gst_structure_copy (new_s);
404
405           gst_structure_set (new_s,
406               "width", GST_TYPE_INT_RANGE, 1, 352,
407               "height", GST_TYPE_INT_RANGE, 1, 288,
408               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 30000, 2002, NULL);
409           caps = gst_caps_merge_structure (caps, new_s);
410
411           gst_structure_set (s_copy,
412               "width", GST_TYPE_INT_RANGE, 1, 176,
413               "height", GST_TYPE_INT_RANGE, 1, 144,
414               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 30000, 1001, NULL);
415           caps = gst_caps_merge_structure (caps, s_copy);
416         } else if (level <= 40) {
417
418           gst_structure_set (new_s,
419               "width", GST_TYPE_INT_RANGE, 1, 352,
420               "height", GST_TYPE_INT_RANGE, 1, 288,
421               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 30000, 1001, NULL);
422           caps = gst_caps_merge_structure (caps, new_s);
423         } else if (level <= 50) {
424           GstStructure *s_copy = gst_structure_copy (new_s);
425
426           gst_structure_set (new_s,
427               "width", GST_TYPE_INT_RANGE, 1, 352,
428               "height", GST_TYPE_INT_RANGE, 1, 288,
429               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 50, 1, NULL);
430           caps = gst_caps_merge_structure (caps, new_s);
431
432           gst_structure_set (s_copy,
433               "width", GST_TYPE_INT_RANGE, 1, 352,
434               "height", GST_TYPE_INT_RANGE, 1, 240,
435               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 60000, 1001, NULL);
436           caps = gst_caps_merge_structure (caps, s_copy);
437         } else if (level <= 60) {
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, 288,
443               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 50, 1, NULL);
444           caps = 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, 240,
449               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 60000, 1001, NULL);
450           caps = gst_caps_merge_structure (caps, s_copy);
451         } else if (level <= 70) {
452           GstStructure *s_copy = gst_structure_copy (new_s);
453
454           gst_structure_set (new_s,
455               "width", GST_TYPE_INT_RANGE, 1, 720,
456               "height", GST_TYPE_INT_RANGE, 1, 576,
457               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 50, 1, NULL);
458           caps = gst_caps_merge_structure (caps, new_s);
459
460           gst_structure_set (s_copy,
461               "width", GST_TYPE_INT_RANGE, 1, 720,
462               "height", GST_TYPE_INT_RANGE, 1, 480,
463               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 60000, 1001, NULL);
464           caps = gst_caps_merge_structure (caps, s_copy);
465         } else {
466           caps = gst_caps_merge_structure (caps, new_s);
467         }
468
469       } else {
470         GstStructure *new_s = gst_structure_new ("video/x-h263",
471             "variant", G_TYPE_STRING, "itu",
472             "h263version", G_TYPE_STRING, "h263",
473             NULL);
474
475         GST_DEBUG_OBJECT (rtph263ppay, "No profile or level specified"
476             " for H263-2000, defaulting to baseline H263");
477
478         caps = gst_caps_merge_structure (caps, new_s);
479       }
480     } else {
481       gboolean f = FALSE, i = FALSE, j = FALSE, t = FALSE;
482       /* FIXME: ffmpeg support the Appendix K too, how do we express it ?
483        *   guint k;
484        */
485       const gchar *str;
486       GstStructure *new_s = gst_structure_new ("video/x-h263",
487           "variant", G_TYPE_STRING, "itu",
488           NULL);
489       gboolean added = FALSE;
490
491       str = gst_structure_get_string (s, "f");
492       if (str && !strcmp (str, "1"))
493         f = TRUE;
494
495       str = gst_structure_get_string (s, "i");
496       if (str && !strcmp (str, "1"))
497         i = TRUE;
498
499       str = gst_structure_get_string (s, "j");
500       if (str && !strcmp (str, "1"))
501         j = TRUE;
502
503       str = gst_structure_get_string (s, "t");
504       if (str && !strcmp (str, "1"))
505         t = TRUE;
506
507       if (f || i || j || t) {
508         GValue list = { 0 };
509         GValue vstr = { 0 };
510
511         g_value_init (&list, GST_TYPE_LIST);
512         g_value_init (&vstr, G_TYPE_STRING);
513
514         g_value_set_static_string (&vstr, "h263");
515         gst_value_list_append_value (&list, &vstr);
516         g_value_set_static_string (&vstr, "h263p");
517         gst_value_list_append_value (&list, &vstr);
518         g_value_unset (&vstr);
519
520         gst_structure_set_value (new_s, "h263version", &list);
521         g_value_unset (&list);
522       } else {
523         gst_structure_set (new_s, "h263version", G_TYPE_STRING, "h263", NULL);
524       }
525
526       if (!f)
527         gst_structure_set (new_s, "annex-f", G_TYPE_BOOLEAN, FALSE, NULL);
528       if (!i)
529         gst_structure_set (new_s, "annex-i", G_TYPE_BOOLEAN, FALSE, NULL);
530       if (!j)
531         gst_structure_set (new_s, "annex-j", G_TYPE_BOOLEAN, FALSE, NULL);
532       if (!t)
533         gst_structure_set (new_s, "annex-t", G_TYPE_BOOLEAN, FALSE, NULL);
534
535
536       str = gst_structure_get_string (s, "custom");
537       if (str) {
538         unsigned int xmax, ymax, mpi;
539         if (sscanf (str, "%u,%u,%u", &xmax, &ymax, &mpi) == 3) {
540           if (xmax % 4 && ymax % 4 && mpi >= 1 && mpi <= 32) {
541             caps = caps_append (caps, new_s, xmax, ymax, mpi);
542             added = TRUE;
543           } else {
544             GST_WARNING_OBJECT (rtph263ppay, "Invalid custom framesize/MPI"
545                 " %u x %u at %u, ignoring", xmax, ymax, mpi);
546           }
547         } else {
548           GST_WARNING_OBJECT (rtph263ppay, "Invalid custom framesize/MPI: %s,"
549               " ignoring", str);
550         }
551       }
552
553       str = gst_structure_get_string (s, "16cif");
554       if (str) {
555         int mpi = atoi (str);
556         caps = caps_append (caps, new_s, 1408, 1152, mpi);
557         added = TRUE;
558       }
559
560       str = gst_structure_get_string (s, "4cif");
561       if (str) {
562         int mpi = atoi (str);
563         caps = caps_append (caps, new_s, 704, 576, mpi);
564         added = TRUE;
565       }
566
567       str = gst_structure_get_string (s, "cif");
568       if (str) {
569         int mpi = atoi (str);
570         caps = caps_append (caps, new_s, 352, 288, mpi);
571         added = TRUE;
572       }
573
574       str = gst_structure_get_string (s, "qcif");
575       if (str) {
576         int mpi = atoi (str);
577         caps = caps_append (caps, new_s, 176, 144, mpi);
578         added = TRUE;
579       }
580
581       str = gst_structure_get_string (s, "sqcif");
582       if (str) {
583         int mpi = atoi (str);
584         caps = caps_append (caps, new_s, 128, 96, mpi);
585         added = TRUE;
586       }
587
588       if (added)
589         gst_structure_free (new_s);
590       else
591         caps = gst_caps_merge_structure (caps, new_s);
592     }
593   }
594
595   gst_caps_unref (intersect);
596
597   return caps;
598 }
599
600
601 static void
602 gst_rtp_h263p_pay_set_property (GObject * object, guint prop_id,
603     const GValue * value, GParamSpec * pspec)
604 {
605   GstRtpH263PPay *rtph263ppay;
606
607   rtph263ppay = GST_RTP_H263P_PAY (object);
608
609   switch (prop_id) {
610     case PROP_FRAGMENTATION_MODE:
611       rtph263ppay->fragmentation_mode = g_value_get_enum (value);
612       break;
613     default:
614       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
615       break;
616   }
617 }
618
619 static void
620 gst_rtp_h263p_pay_get_property (GObject * object, guint prop_id,
621     GValue * value, GParamSpec * pspec)
622 {
623   GstRtpH263PPay *rtph263ppay;
624
625   rtph263ppay = GST_RTP_H263P_PAY (object);
626
627   switch (prop_id) {
628     case PROP_FRAGMENTATION_MODE:
629       g_value_set_enum (value, rtph263ppay->fragmentation_mode);
630       break;
631     default:
632       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
633       break;
634   }
635 }
636
637 static GstFlowReturn
638 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
639 {
640   guint avail;
641   GstBuffer *outbuf;
642   GstFlowReturn ret;
643   gboolean fragmented;
644
645   avail = gst_adapter_available (rtph263ppay->adapter);
646   if (avail == 0)
647     return GST_FLOW_OK;
648
649   fragmented = FALSE;
650   /* This algorithm assumes the H263/+/++ encoder sends complete frames in each
651    * buffer */
652   /* With Fragmentation Mode at GST_FRAGMENTATION_MODE_NORMAL:
653    *  This algorithm implements the Follow-on packets method for packetization.
654    *  This assumes low packet loss network. 
655    * With Fragmentation Mode at GST_FRAGMENTATION_MODE_SYNC:
656    *  This algorithm separates large frames at synchronisation points (Segments)
657    *  (See RFC 4629 section 6). It would be interesting to have a property such as network
658    *  quality to select between both packetization methods */
659   /* TODO Add VRC supprt (See RFC 4629 section 5.2) */
660
661   while (avail > 0) {
662     guint towrite;
663     guint8 *payload;
664     guint payload_len;
665     gint header_len;
666     guint next_gop = 0;
667     gboolean found_gob = FALSE;
668     GstRTPBuffer rtp = { NULL };
669
670     if (rtph263ppay->fragmentation_mode == GST_FRAGMENTATION_MODE_SYNC) {
671       /* start after 1st gop possible */
672       guint parsed_len = 3;
673       const guint8 *parse_data = NULL;
674
675       parse_data = gst_adapter_map (rtph263ppay->adapter, avail);
676
677       /* Check if we have a gob or eos , eossbs */
678       /* FIXME EOS and EOSSBS packets should never contain any gobs and vice-versa */
679       if (avail >= 3 && *parse_data == 0 && *(parse_data + 1) == 0
680           && *(parse_data + 2) >= 0x80) {
681         GST_DEBUG_OBJECT (rtph263ppay, " Found GOB header");
682         found_gob = TRUE;
683       }
684       /* Find next and cut the packet accordingly */
685       /* TODO we should get as many gobs as possible until MTU is reached, this
686        * code seems to just get one GOB per packet */
687       while (parsed_len + 2 < avail) {
688         if (parse_data[parsed_len] == 0 && parse_data[parsed_len + 1] == 0
689             && parse_data[parsed_len + 2] >= 0x80) {
690           next_gop = parsed_len;
691           GST_DEBUG_OBJECT (rtph263ppay, " Next GOB Detected at :  %d",
692               next_gop);
693           break;
694         }
695         parsed_len++;
696       }
697       gst_adapter_unmap (rtph263ppay->adapter);
698     }
699
700     /* for picture start frames (non-fragmented), we need to remove the first
701      * two 0x00 bytes and set P=1 */
702     header_len = (fragmented && !found_gob) ? 2 : 0;
703
704     towrite = MIN (avail, gst_rtp_buffer_calc_payload_len
705         (GST_RTP_BASE_PAYLOAD_MTU (rtph263ppay) - header_len, 0, 0));
706
707     if (next_gop > 0)
708       towrite = MIN (next_gop, towrite);
709
710     payload_len = header_len + towrite;
711
712     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
713
714     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
715     /* last fragment gets the marker bit set */
716     gst_rtp_buffer_set_marker (&rtp, avail > towrite ? 0 : 1);
717
718     payload = gst_rtp_buffer_get_payload (&rtp);
719
720     gst_adapter_copy (rtph263ppay->adapter, &payload[header_len], 0, towrite);
721
722     /*  0                   1
723      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
724      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
725      * |   RR    |P|V|   PLEN    |PEBIT|
726      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
727      */
728     /* if fragmented or gop header , write p bit =1 */
729     payload[0] = (fragmented && !found_gob) ? 0x00 : 0x04;
730     payload[1] = 0;
731
732     GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_timestamp;
733     GST_BUFFER_DURATION (outbuf) = rtph263ppay->first_duration;
734     gst_rtp_buffer_unmap (&rtp);
735
736     gst_adapter_flush (rtph263ppay->adapter, towrite);
737
738     ret =
739         gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtph263ppay), outbuf);
740
741     avail -= towrite;
742     fragmented = TRUE;
743   }
744
745   return ret;
746 }
747
748 static GstFlowReturn
749 gst_rtp_h263p_pay_handle_buffer (GstRTPBasePayload * payload,
750     GstBuffer * buffer)
751 {
752   GstRtpH263PPay *rtph263ppay;
753   GstFlowReturn ret;
754
755   rtph263ppay = GST_RTP_H263P_PAY (payload);
756
757   rtph263ppay->first_timestamp = GST_BUFFER_TIMESTAMP (buffer);
758   rtph263ppay->first_duration = GST_BUFFER_DURATION (buffer);
759
760   /* we always encode and flush a full picture */
761   gst_adapter_push (rtph263ppay->adapter, buffer);
762   ret = gst_rtp_h263p_pay_flush (rtph263ppay);
763
764   return ret;
765 }
766
767 gboolean
768 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
769 {
770   return gst_element_register (plugin, "rtph263ppay",
771       GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_PAY);
772 }