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