gst/rtp/: Fix caps with payload numbers.
[platform/upstream/gstreamer.git] / gst / rtp / gstrtph263ppay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.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
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtph263ppay.h"
29
30 /* elementfactory information */
31 static const GstElementDetails gst_rtp_h263ppay_details =
32 GST_ELEMENT_DETAILS ("RTP packet payloader",
33     "Codec/Payloader/Network",
34     "Payload-encodes H263+ video in RTP packets (RFC 2429)",
35     "Wim Taymans <wim@fluendo.com>");
36
37 static GstStaticPadTemplate gst_rtp_h263p_pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
39     GST_PAD_SINK,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("video/x-h263, "
42         "variant = (string) \"itu\", " "h263version = (string) \"h263p\"")
43     );
44
45 static GstStaticPadTemplate gst_rtp_h263p_pay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp, "
50         "media = (string) \"video\", "
51         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"")
53     );
54
55 static void gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass);
56 static void gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass);
57 static void gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay);
58 static void gst_rtp_h263p_pay_finalize (GObject * object);
59
60 static gboolean gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload,
61     GstCaps * caps);
62 static GstFlowReturn gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload *
63     payload, GstBuffer * buffer);
64
65 static GstBaseRTPPayloadClass *parent_class = NULL;
66
67 static GType
68 gst_rtp_h263p_pay_get_type (void)
69 {
70   static GType rtph263ppay_type = 0;
71
72   if (!rtph263ppay_type) {
73     static const GTypeInfo rtph263ppay_info = {
74       sizeof (GstRtpH263PPayClass),
75       (GBaseInitFunc) gst_rtp_h263p_pay_base_init,
76       NULL,
77       (GClassInitFunc) gst_rtp_h263p_pay_class_init,
78       NULL,
79       NULL,
80       sizeof (GstRtpH263PPay),
81       0,
82       (GInstanceInitFunc) gst_rtp_h263p_pay_init,
83     };
84
85     rtph263ppay_type =
86         g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpH263PPay",
87         &rtph263ppay_info, 0);
88   }
89   return rtph263ppay_type;
90 }
91
92 static void
93 gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass)
94 {
95   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
96
97   gst_element_class_add_pad_template (element_class,
98       gst_static_pad_template_get (&gst_rtp_h263p_pay_src_template));
99   gst_element_class_add_pad_template (element_class,
100       gst_static_pad_template_get (&gst_rtp_h263p_pay_sink_template));
101
102   gst_element_class_set_details (element_class, &gst_rtp_h263ppay_details);
103 }
104
105 static void
106 gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass)
107 {
108   GObjectClass *gobject_class;
109   GstElementClass *gstelement_class;
110   GstBaseRTPPayloadClass *gstbasertppayload_class;
111
112   gobject_class = (GObjectClass *) klass;
113   gstelement_class = (GstElementClass *) klass;
114   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
115
116   parent_class = g_type_class_peek_parent (klass);
117
118   gobject_class->finalize = gst_rtp_h263p_pay_finalize;
119
120   gstbasertppayload_class->set_caps = gst_rtp_h263p_pay_setcaps;
121   gstbasertppayload_class->handle_buffer = gst_rtp_h263p_pay_handle_buffer;
122 }
123
124 static void
125 gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay)
126 {
127   rtph263ppay->adapter = gst_adapter_new ();
128 }
129
130 static void
131 gst_rtp_h263p_pay_finalize (GObject * object)
132 {
133   GstRtpH263PPay *rtph263ppay;
134
135   rtph263ppay = GST_RTP_H263P_PAY (object);
136
137   g_object_unref (rtph263ppay->adapter);
138   rtph263ppay->adapter = NULL;
139
140   G_OBJECT_CLASS (parent_class)->finalize (object);
141 }
142
143 static gboolean
144 gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
145 {
146   gst_basertppayload_set_options (payload, "video", TRUE, "H263-1998", 90000);
147   gst_basertppayload_set_outcaps (payload, NULL);
148
149   return TRUE;
150 }
151
152
153 static GstFlowReturn
154 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
155 {
156   guint avail;
157   GstBuffer *outbuf;
158   GstFlowReturn ret;
159   gboolean fragmented;
160
161   avail = gst_adapter_available (rtph263ppay->adapter);
162   if (avail == 0)
163     return GST_FLOW_OK;
164
165   fragmented = FALSE;
166
167   /* This algorithm assumes the H263+ encoder sends complete frames in each
168    * buffer */
169   /* This algorithm implements the Follow-on packets method for packetization.
170    * This assumes low packet loss network. A more resilient method would be to
171    * separate large frames at synchronisation points (Segments) (See RFC 2429
172    * section 6). It would be interesting to have a property such as network
173    * quality to select between both packetization methods */
174   /* TODO Add VRC supprt (See RFC 2429 section 4.2) */
175   while (avail > 0) {
176     guint towrite;
177     guint8 *payload;
178     guint8 *data;
179     guint payload_len;
180     gint header_len;
181
182     /* for picture start frames (non-fragmented), we need to remove the first
183      * two 0x00 bytes and set P=1 */
184     header_len = (fragmented ? 2 : 0);
185
186     towrite = MIN (avail, gst_rtp_buffer_calc_payload_len
187         (GST_BASE_RTP_PAYLOAD_MTU (rtph263ppay) - header_len, 0, 0));
188
189     payload_len = header_len + towrite;
190
191     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
192     /* last fragment gets the marker bit set */
193     gst_rtp_buffer_set_marker (outbuf, avail > towrite ? 0 : 1);
194
195     payload = gst_rtp_buffer_get_payload (outbuf);
196
197     data = (guint8 *) gst_adapter_peek (rtph263ppay->adapter, towrite);
198     memcpy (&payload[header_len], data, towrite);
199
200     /*  0                   1
201      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
202      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203      * |   RR    |P|V|   PLEN    |PEBIT|
204      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205      */
206     payload[0] = fragmented ? 0x00 : 0x04;
207     payload[1] = 0;
208
209     GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_ts;
210     gst_adapter_flush (rtph263ppay->adapter, towrite);
211
212     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263ppay), outbuf);
213
214     avail -= towrite;
215     fragmented = TRUE;
216   }
217
218   return ret;
219 }
220
221 static GstFlowReturn
222 gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload * payload,
223     GstBuffer * buffer)
224 {
225   GstRtpH263PPay *rtph263ppay;
226   GstFlowReturn ret;
227   guint size;
228
229   rtph263ppay = GST_RTP_H263P_PAY (payload);
230
231   size = GST_BUFFER_SIZE (buffer);
232   rtph263ppay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
233
234   /* we always encode and flush a full picture */
235   gst_adapter_push (rtph263ppay->adapter, buffer);
236   ret = gst_rtp_h263p_pay_flush (rtph263ppay);
237
238   return ret;
239 }
240
241 gboolean
242 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
243 {
244   return gst_element_register (plugin, "rtph263ppay",
245       GST_RANK_NONE, GST_TYPE_RTP_H263P_PAY);
246 }