2 * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtph263pdepay.h"
29 static GstStaticPadTemplate gst_rtp_h263p_depay_src_template =
30 GST_STATIC_PAD_TEMPLATE ("src",
33 GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
36 static GstStaticPadTemplate gst_rtp_h263p_depay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_STATIC_CAPS ("application/x-rtp, "
41 "media = (string) \"video\", "
42 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
43 "clock-rate = (int) [1, MAX], "
44 "encoding-name = (string) \"H263-1998\"; "
46 /* NOTE all optional SDP params must be strings in the caps */
48 "sqcif = (string) [1, 32], "
49 "qcif = (string) [1, 32], "
50 "cif = (string) [1, 32], "
51 "cif4 = (string) [1, 32], "
52 "cif16 = (string) [1, 32], "
53 "custom = (string) ANY, "
54 "f = (string) {0, 1},"
55 "i = (string) {0, 1},"
56 "j = (string) {0, 1},"
57 "t = (string) {0, 1},"
58 "k = (string) {1, 2, 3, 4},"
59 "n = (string) {1, 2, 3, 4},"
61 "par = (string) ANY, "
62 "cpcf = (string) ANY, "
63 "bpp = (string) [0, 65536], "
64 "hrd = (string) {0, 1}; "
67 "media = (string) \"video\", "
68 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
69 "clock-rate = (int) [1, MAX], "
70 "encoding-name = (string) \"H263-2000\" "
72 /* NOTE all optional SDP params must be strings in the caps */
74 "profile = (string) [0, 10], "
75 "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
76 "interlace = (string) {0, 1};"
81 #define gst_rtp_h263p_depay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpH263PDepay, gst_rtp_h263p_depay,
83 GST_TYPE_RTP_BASE_DEPAYLOAD);
85 static void gst_rtp_h263p_depay_finalize (GObject * object);
87 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
88 element, GstStateChange transition);
90 static GstBuffer *gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
92 gboolean gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter,
96 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
98 GObjectClass *gobject_class;
99 GstElementClass *gstelement_class;
100 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
102 gobject_class = (GObjectClass *) klass;
103 gstelement_class = (GstElementClass *) klass;
104 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
106 gobject_class->finalize = gst_rtp_h263p_depay_finalize;
108 gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
110 gst_element_class_add_pad_template (gstelement_class,
111 gst_static_pad_template_get (&gst_rtp_h263p_depay_src_template));
112 gst_element_class_add_pad_template (gstelement_class,
113 gst_static_pad_template_get (&gst_rtp_h263p_depay_sink_template));
115 gst_element_class_set_static_metadata (gstelement_class,
116 "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
117 "Extracts H263/+/++ video from RTP packets (RFC 4629)",
118 "Wim Taymans <wim.taymans@gmail.com>");
120 gstrtpbasedepayload_class->process = gst_rtp_h263p_depay_process;
121 gstrtpbasedepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
125 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay)
127 rtph263pdepay->adapter = gst_adapter_new ();
131 gst_rtp_h263p_depay_finalize (GObject * object)
133 GstRtpH263PDepay *rtph263pdepay;
135 rtph263pdepay = GST_RTP_H263P_DEPAY (object);
137 g_object_unref (rtph263pdepay->adapter);
138 rtph263pdepay->adapter = NULL;
140 G_OBJECT_CLASS (parent_class)->finalize (object);
144 gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
146 GstCaps *srccaps = NULL;
147 GstStructure *structure = gst_caps_get_structure (caps, 0);
149 const gchar *encoding_name = NULL;
152 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
153 clock_rate = 90000; /* default */
154 filter->clock_rate = clock_rate;
156 encoding_name = gst_structure_get_string (structure, "encoding-name");
157 if (encoding_name == NULL)
158 goto no_encoding_name;
160 if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
162 srccaps = gst_caps_new_simple ("video/x-h263",
163 "variant", G_TYPE_STRING, "itu",
164 "h263version", G_TYPE_STRING, "h263pp", NULL);
165 } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
166 /* this can be H263 or H263+ depending on defined appendixes in the optional
168 const gchar *F, *I, *J, *T, *K, *N, *P;
169 gboolean is_h263p = FALSE;
171 F = gst_structure_get_string (structure, "f");
173 if (g_ascii_strcasecmp (F, "1") == 0)
175 I = gst_structure_get_string (structure, "i");
177 if (g_ascii_strcasecmp (I, "1") == 0)
179 J = gst_structure_get_string (structure, "j");
181 if (g_ascii_strcasecmp (J, "1") == 0)
183 T = gst_structure_get_string (structure, "t");
185 if (g_ascii_strcasecmp (T, "1") == 0)
187 K = gst_structure_get_string (structure, "k");
190 N = gst_structure_get_string (structure, "n");
193 P = gst_structure_get_string (structure, "p");
198 srccaps = gst_caps_new_simple ("video/x-h263",
199 "variant", G_TYPE_STRING, "itu",
200 "h263version", G_TYPE_STRING, "h263p", NULL);
202 srccaps = gst_caps_new_simple ("video/x-h263",
203 "variant", G_TYPE_STRING, "itu",
204 "h263version", G_TYPE_STRING, "h263", NULL);
210 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
211 gst_caps_unref (srccaps);
218 GST_ERROR_OBJECT (filter, "no encoding-name");
223 GST_ERROR_OBJECT (filter, "invalid encoding-name");
229 gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
231 GstRtpH263PDepay *rtph263pdepay;
238 GstRTPBuffer rtp = { NULL };
240 rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
242 /* flush remaining data on discont */
243 if (GST_BUFFER_IS_DISCONT (buf)) {
244 GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
245 gst_adapter_clear (rtph263pdepay->adapter);
246 rtph263pdepay->wait_start = TRUE;
249 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
251 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
254 if (payload_len < header_len)
257 payload = gst_rtp_buffer_get_payload (&rtp);
259 M = gst_rtp_buffer_get_marker (&rtp);
262 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
263 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
264 * | RR |P|V| PLEN |PEBIT|
265 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
267 P = (payload[0] & 0x04) == 0x04;
268 V = (payload[0] & 0x02) == 0x02;
269 PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
270 PEBIT = payload[1] & 0x7;
272 GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
282 if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
286 /* FIXME, have to make the packet writable hear. Better to reset these
287 * bytes when we copy the packet below */
288 rtph263pdepay->wait_start = FALSE;
290 payload[header_len] = 0;
291 payload[header_len + 1] = 0;
294 if (rtph263pdepay->wait_start)
297 if (payload_len < header_len)
300 /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
301 /* FIXME actually use the RTP picture header when it is lost in the network */
302 /* for now strip off header */
303 payload += header_len;
304 payload_len -= header_len;
307 /* frame is completed: append to previous, push it out */
312 GST_LOG_OBJECT (depayload, "Frame complete");
314 avail = gst_adapter_available (rtph263pdepay->adapter);
315 len = avail + payload_len;
316 padlen = (len % 4) + 4;
318 outbuf = gst_buffer_new_and_alloc (len + padlen);
320 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
321 memset (map.data + len, 0, padlen);
323 /* prepend previous data */
325 gst_adapter_copy (rtph263pdepay->adapter, map.data, 0, avail);
326 gst_adapter_flush (rtph263pdepay->adapter, avail);
328 memcpy (map.data + avail, payload, payload_len);
329 gst_buffer_unmap (outbuf, &map);
330 gst_rtp_buffer_unmap (&rtp);
335 /* frame not completed: store in adapter */
336 outbuf = gst_buffer_new_and_alloc (payload_len);
338 GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
339 gst_buffer_fill (outbuf, 0, payload, payload_len);
341 gst_adapter_push (rtph263pdepay->adapter, outbuf);
342 gst_rtp_buffer_unmap (&rtp);
348 GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
349 ("Packet payload was too small"), (NULL));
350 gst_rtp_buffer_unmap (&rtp);
355 GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
356 gst_rtp_buffer_unmap (&rtp);
361 static GstStateChangeReturn
362 gst_rtp_h263p_depay_change_state (GstElement * element,
363 GstStateChange transition)
365 GstRtpH263PDepay *rtph263pdepay;
366 GstStateChangeReturn ret;
368 rtph263pdepay = GST_RTP_H263P_DEPAY (element);
370 switch (transition) {
371 case GST_STATE_CHANGE_NULL_TO_READY:
373 case GST_STATE_CHANGE_READY_TO_PAUSED:
374 gst_adapter_clear (rtph263pdepay->adapter);
375 rtph263pdepay->wait_start = TRUE;
381 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
383 switch (transition) {
384 case GST_STATE_CHANGE_READY_TO_NULL:
393 gst_rtp_h263p_depay_plugin_init (GstPlugin * plugin)
395 return gst_element_register (plugin, "rtph263pdepay",
396 GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_DEPAY);