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