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