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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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 "clock-rate = (int) [1, MAX], "
43 "encoding-name = (string) \"H263-1998\"; "
45 /* NOTE all optional SDP params must be strings in the caps */
47 "sqcif = (string) [1, 32], "
48 "qcif = (string) [1, 32], "
49 "cif = (string) [1, 32], "
50 "cif4 = (string) [1, 32], "
51 "cif16 = (string) [1, 32], "
52 "custom = (string) ANY, "
53 "f = (string) {0, 1},"
54 "i = (string) {0, 1},"
55 "j = (string) {0, 1},"
56 "t = (string) {0, 1},"
57 "k = (string) {1, 2, 3, 4},"
58 "n = (string) {1, 2, 3, 4},"
60 "par = (string) ANY, "
61 "cpcf = (string) ANY, "
62 "bpp = (string) [0, 65536], "
63 "hrd = (string) {0, 1}; "
66 "media = (string) \"video\", "
67 "clock-rate = (int) [1, MAX], "
68 "encoding-name = (string) \"H263-2000\" "
70 /* NOTE all optional SDP params must be strings in the caps */
72 "profile = (string) [0, 10], "
73 "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
74 "interlace = (string) {0, 1};"
79 #define gst_rtp_h263p_depay_parent_class parent_class
80 G_DEFINE_TYPE (GstRtpH263PDepay, gst_rtp_h263p_depay,
81 GST_TYPE_RTP_BASE_DEPAYLOAD);
83 static void gst_rtp_h263p_depay_finalize (GObject * object);
85 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
86 element, GstStateChange transition);
88 static GstBuffer *gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
90 gboolean gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter,
94 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
98 GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
100 gobject_class = (GObjectClass *) klass;
101 gstelement_class = (GstElementClass *) klass;
102 gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
104 gobject_class->finalize = gst_rtp_h263p_depay_finalize;
106 gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
108 gst_element_class_add_pad_template (gstelement_class,
109 gst_static_pad_template_get (&gst_rtp_h263p_depay_src_template));
110 gst_element_class_add_pad_template (gstelement_class,
111 gst_static_pad_template_get (&gst_rtp_h263p_depay_sink_template));
113 gst_element_class_set_static_metadata (gstelement_class,
114 "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
115 "Extracts H263/+/++ video from RTP packets (RFC 4629)",
116 "Wim Taymans <wim.taymans@gmail.com>");
118 gstrtpbasedepayload_class->process = gst_rtp_h263p_depay_process;
119 gstrtpbasedepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
123 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay)
125 rtph263pdepay->adapter = gst_adapter_new ();
129 gst_rtp_h263p_depay_finalize (GObject * object)
131 GstRtpH263PDepay *rtph263pdepay;
133 rtph263pdepay = GST_RTP_H263P_DEPAY (object);
135 g_object_unref (rtph263pdepay->adapter);
136 rtph263pdepay->adapter = NULL;
138 G_OBJECT_CLASS (parent_class)->finalize (object);
142 gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
144 GstCaps *srccaps = NULL;
145 GstStructure *structure = gst_caps_get_structure (caps, 0);
147 const gchar *encoding_name = NULL;
150 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
151 clock_rate = 90000; /* default */
152 filter->clock_rate = clock_rate;
154 encoding_name = gst_structure_get_string (structure, "encoding-name");
155 if (encoding_name == NULL)
156 goto no_encoding_name;
158 if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
160 srccaps = gst_caps_new_simple ("video/x-h263",
161 "variant", G_TYPE_STRING, "itu",
162 "h263version", G_TYPE_STRING, "h263pp", NULL);
163 } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
164 /* this can be H263 or H263+ depending on defined appendixes in the optional
166 const gchar *F, *I, *J, *T, *K, *N, *P;
167 gboolean is_h263p = FALSE;
169 F = gst_structure_get_string (structure, "f");
171 if (g_ascii_strcasecmp (F, "1") == 0)
173 I = gst_structure_get_string (structure, "i");
175 if (g_ascii_strcasecmp (I, "1") == 0)
177 J = gst_structure_get_string (structure, "j");
179 if (g_ascii_strcasecmp (J, "1") == 0)
181 T = gst_structure_get_string (structure, "t");
183 if (g_ascii_strcasecmp (T, "1") == 0)
185 K = gst_structure_get_string (structure, "k");
188 N = gst_structure_get_string (structure, "n");
191 P = gst_structure_get_string (structure, "p");
196 srccaps = gst_caps_new_simple ("video/x-h263",
197 "variant", G_TYPE_STRING, "itu",
198 "h263version", G_TYPE_STRING, "h263p", NULL);
200 srccaps = gst_caps_new_simple ("video/x-h263",
201 "variant", G_TYPE_STRING, "itu",
202 "h263version", G_TYPE_STRING, "h263", NULL);
208 res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
209 gst_caps_unref (srccaps);
216 GST_ERROR_OBJECT (filter, "no encoding-name");
221 GST_ERROR_OBJECT (filter, "invalid encoding-name");
227 gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
229 GstRtpH263PDepay *rtph263pdepay;
236 GstRTPBuffer rtp = { NULL };
238 rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
240 /* flush remaining data on discont */
241 if (GST_BUFFER_IS_DISCONT (buf)) {
242 GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
243 gst_adapter_clear (rtph263pdepay->adapter);
244 rtph263pdepay->wait_start = TRUE;
247 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
249 payload_len = gst_rtp_buffer_get_payload_len (&rtp);
252 if (payload_len < header_len)
255 payload = gst_rtp_buffer_get_payload (&rtp);
257 M = gst_rtp_buffer_get_marker (&rtp);
260 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
261 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
262 * | RR |P|V| PLEN |PEBIT|
263 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
265 P = (payload[0] & 0x04) == 0x04;
266 V = (payload[0] & 0x02) == 0x02;
267 PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
268 PEBIT = payload[1] & 0x7;
270 GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
280 if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
284 /* FIXME, have to make the packet writable hear. Better to reset these
285 * bytes when we copy the packet below */
286 rtph263pdepay->wait_start = FALSE;
288 payload[header_len] = 0;
289 payload[header_len + 1] = 0;
292 if (rtph263pdepay->wait_start)
295 if (payload_len < header_len)
298 /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
299 /* FIXME actually use the RTP picture header when it is lost in the network */
300 /* for now strip off header */
301 payload += header_len;
302 payload_len -= header_len;
305 /* frame is completed: append to previous, push it out */
310 GST_LOG_OBJECT (depayload, "Frame complete");
313 gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len);
314 gst_adapter_push (rtph263pdepay->adapter, outbuf);
317 avail = gst_adapter_available (rtph263pdepay->adapter);
318 len = avail + payload_len;
319 padlen = (len % 4) + 4;
321 outbuf = gst_adapter_take_buffer (rtph263pdepay->adapter, avail);
323 padbuf = gst_buffer_new_and_alloc (padlen);
324 gst_buffer_memset (padbuf, 0, 0, padlen);
325 outbuf = gst_buffer_append (outbuf, padbuf);
327 gst_rtp_buffer_unmap (&rtp);
332 /* frame not completed: store in adapter */
333 GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
336 gst_rtp_buffer_get_payload_subbuffer (&rtp, header_len, payload_len);
337 gst_adapter_push (rtph263pdepay->adapter, outbuf);
338 gst_rtp_buffer_unmap (&rtp);
344 GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
345 ("Packet payload was too small"), (NULL));
346 gst_rtp_buffer_unmap (&rtp);
351 GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
352 gst_rtp_buffer_unmap (&rtp);
357 static GstStateChangeReturn
358 gst_rtp_h263p_depay_change_state (GstElement * element,
359 GstStateChange transition)
361 GstRtpH263PDepay *rtph263pdepay;
362 GstStateChangeReturn ret;
364 rtph263pdepay = GST_RTP_H263P_DEPAY (element);
366 switch (transition) {
367 case GST_STATE_CHANGE_NULL_TO_READY:
369 case GST_STATE_CHANGE_READY_TO_PAUSED:
370 gst_adapter_clear (rtph263pdepay->adapter);
371 rtph263pdepay->wait_start = TRUE;
377 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
379 switch (transition) {
380 case GST_STATE_CHANGE_READY_TO_NULL:
389 gst_rtp_h263p_depay_plugin_init (GstPlugin * plugin)
391 return gst_element_register (plugin, "rtph263pdepay",
392 GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_DEPAY);