• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
28 #include "gstrtpelements.h"
29 #include "gstrtph263pdepay.h"
30 #include "gstrtputils.h"
31 
32 GST_DEBUG_CATEGORY_STATIC (rtph263pdepay_debug);
33 #define GST_CAT_DEFAULT (rtph263pdepay_debug)
34 
35 static GstStaticPadTemplate gst_rtp_h263p_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("video/x-h263, " "variant = (string) \"itu\" ")
40     );
41 
42 static GstStaticPadTemplate gst_rtp_h263p_depay_sink_template =
43     GST_STATIC_PAD_TEMPLATE ("sink",
44     GST_PAD_SINK,
45     GST_PAD_ALWAYS,
46     GST_STATIC_CAPS ("application/x-rtp, "
47         "media = (string) \"video\", "
48         "clock-rate = (int) [1, MAX], "
49         "encoding-name = (string) \"H263-1998\"; "
50         /* optional params */
51         /* NOTE all optional SDP params must be strings in the caps */
52         /*
53            "sqcif = (string) [1, 32], "
54            "qcif = (string) [1, 32], "
55            "cif = (string) [1, 32], "
56            "cif4 = (string) [1, 32], "
57            "cif16 = (string) [1, 32], "
58            "custom = (string) ANY, "
59            "f = (string) {0, 1},"
60            "i = (string) {0, 1},"
61            "j = (string) {0, 1},"
62            "t = (string) {0, 1},"
63            "k = (string) {1, 2, 3, 4},"
64            "n = (string) {1, 2, 3, 4},"
65            "p = (string) ANY,"
66            "par = (string) ANY, "
67            "cpcf = (string) ANY, "
68            "bpp = (string) [0, 65536], "
69            "hrd = (string) {0, 1}; "
70          */
71         "application/x-rtp, "
72         "media = (string) \"video\", "
73         "clock-rate = (int) [1, MAX], "
74         "encoding-name = (string) \"H263-2000\" "
75         /* optional params */
76         /* NOTE all optional SDP params must be strings in the caps */
77         /*
78            "profile = (string) [0, 10], "
79            "level = (string) {10, 20, 30, 40, 45, 50, 60, 70}, "
80            "interlace = (string) {0, 1};"
81          */
82     )
83     );
84 
85 #define gst_rtp_h263p_depay_parent_class parent_class
86 G_DEFINE_TYPE (GstRtpH263PDepay, gst_rtp_h263p_depay,
87     GST_TYPE_RTP_BASE_DEPAYLOAD);
88 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtph263pdepay, "rtph263pdepay",
89     GST_RANK_SECONDARY, GST_TYPE_RTP_H263P_DEPAY, rtp_element_init (plugin));
90 
91 static void gst_rtp_h263p_depay_finalize (GObject * object);
92 
93 static GstStateChangeReturn gst_rtp_h263p_depay_change_state (GstElement *
94     element, GstStateChange transition);
95 
96 static GstBuffer *gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
97     GstRTPBuffer * rtp);
98 gboolean gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter,
99     GstCaps * caps);
100 
101 static void
gst_rtp_h263p_depay_class_init(GstRtpH263PDepayClass * klass)102 gst_rtp_h263p_depay_class_init (GstRtpH263PDepayClass * klass)
103 {
104   GObjectClass *gobject_class;
105   GstElementClass *gstelement_class;
106   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
107 
108   gobject_class = (GObjectClass *) klass;
109   gstelement_class = (GstElementClass *) klass;
110   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
111 
112   gobject_class->finalize = gst_rtp_h263p_depay_finalize;
113 
114   gstelement_class->change_state = gst_rtp_h263p_depay_change_state;
115 
116   gst_element_class_add_static_pad_template (gstelement_class,
117       &gst_rtp_h263p_depay_src_template);
118   gst_element_class_add_static_pad_template (gstelement_class,
119       &gst_rtp_h263p_depay_sink_template);
120 
121   gst_element_class_set_static_metadata (gstelement_class,
122       "RTP H263 depayloader", "Codec/Depayloader/Network/RTP",
123       "Extracts H263/+/++ video from RTP packets (RFC 4629)",
124       "Wim Taymans <wim.taymans@gmail.com>");
125 
126   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_h263p_depay_process;
127   gstrtpbasedepayload_class->set_caps = gst_rtp_h263p_depay_setcaps;
128 
129   GST_DEBUG_CATEGORY_INIT (rtph263pdepay_debug, "rtph263pdepay", 0,
130       "H263+ Video RTP Depayloader");
131 }
132 
133 static void
gst_rtp_h263p_depay_init(GstRtpH263PDepay * rtph263pdepay)134 gst_rtp_h263p_depay_init (GstRtpH263PDepay * rtph263pdepay)
135 {
136   rtph263pdepay->adapter = gst_adapter_new ();
137 }
138 
139 static void
gst_rtp_h263p_depay_finalize(GObject * object)140 gst_rtp_h263p_depay_finalize (GObject * object)
141 {
142   GstRtpH263PDepay *rtph263pdepay;
143 
144   rtph263pdepay = GST_RTP_H263P_DEPAY (object);
145 
146   g_object_unref (rtph263pdepay->adapter);
147   rtph263pdepay->adapter = NULL;
148 
149   G_OBJECT_CLASS (parent_class)->finalize (object);
150 }
151 
152 gboolean
gst_rtp_h263p_depay_setcaps(GstRTPBaseDepayload * filter,GstCaps * caps)153 gst_rtp_h263p_depay_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps)
154 {
155   GstCaps *srccaps = NULL;
156   GstStructure *structure = gst_caps_get_structure (caps, 0);
157   gint clock_rate;
158   const gchar *encoding_name = NULL;
159   gboolean res;
160 
161   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
162     clock_rate = 90000;         /* default */
163   filter->clock_rate = clock_rate;
164 
165   encoding_name = gst_structure_get_string (structure, "encoding-name");
166   if (encoding_name == NULL)
167     goto no_encoding_name;
168 
169   if (g_ascii_strcasecmp (encoding_name, "H263-2000") == 0) {
170     /* always h263++ */
171     srccaps = gst_caps_new_simple ("video/x-h263",
172         "variant", G_TYPE_STRING, "itu",
173         "h263version", G_TYPE_STRING, "h263pp", NULL);
174   } else if (g_ascii_strcasecmp (encoding_name, "H263-1998") == 0) {
175     /* this can be H263 or H263+ depending on defined appendixes in the optional
176      * SDP params */
177     const gchar *F, *I, *J, *T, *K, *N, *P;
178     gboolean is_h263p = FALSE;
179 
180     F = gst_structure_get_string (structure, "f");
181     if (F)
182       if (g_ascii_strcasecmp (F, "1") == 0)
183         is_h263p = TRUE;
184     I = gst_structure_get_string (structure, "i");
185     if (I)
186       if (g_ascii_strcasecmp (I, "1") == 0)
187         is_h263p = TRUE;
188     J = gst_structure_get_string (structure, "j");
189     if (J)
190       if (g_ascii_strcasecmp (J, "1") == 0)
191         is_h263p = TRUE;
192     T = gst_structure_get_string (structure, "t");
193     if (T)
194       if (g_ascii_strcasecmp (T, "1") == 0)
195         is_h263p = TRUE;
196     K = gst_structure_get_string (structure, "k");
197     if (K)
198       is_h263p = TRUE;
199     N = gst_structure_get_string (structure, "n");
200     if (N)
201       is_h263p = TRUE;
202     P = gst_structure_get_string (structure, "p");
203     if (P)
204       is_h263p = TRUE;
205 
206     if (is_h263p) {
207       srccaps = gst_caps_new_simple ("video/x-h263",
208           "variant", G_TYPE_STRING, "itu",
209           "h263version", G_TYPE_STRING, "h263p", NULL);
210     } else {
211       srccaps = gst_caps_new_simple ("video/x-h263",
212           "variant", G_TYPE_STRING, "itu",
213           "h263version", G_TYPE_STRING, "h263", NULL);
214     }
215   }
216   if (!srccaps)
217     goto no_caps;
218 
219   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (filter), srccaps);
220   gst_caps_unref (srccaps);
221 
222   return res;
223 
224   /* ERRORS */
225 no_encoding_name:
226   {
227     GST_ERROR_OBJECT (filter, "no encoding-name");
228     return FALSE;
229   }
230 no_caps:
231   {
232     GST_ERROR_OBJECT (filter, "invalid encoding-name");
233     return FALSE;
234   }
235 }
236 
237 static void
gst_rtp_h263p_depay_decorate_output_buffer(GstRtpH263PDepay * rtph263pdepay,GstBuffer * outbuf)238 gst_rtp_h263p_depay_decorate_output_buffer (GstRtpH263PDepay * rtph263pdepay,
239     GstBuffer * outbuf)
240 {
241   gboolean is_intra = FALSE;
242   GstBitReader bits;
243   guint8 pic_hdr[16];
244   gsize pic_hdr_len = 0;
245   guint32 psc, ptype, mpptype;
246   guint8 ufep;
247 
248   pic_hdr_len = gst_buffer_extract (outbuf, 0, pic_hdr, sizeof (pic_hdr));
249 
250   GST_MEMDUMP_OBJECT (rtph263pdepay, "pic_hdr", pic_hdr, pic_hdr_len);
251 
252 #if 0
253   if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) >= GST_LEVEL_MEMDUMP) {
254     gchar bit_str[1 + sizeof (pic_hdr) * 8] = { 0, };
255     guint8 b;
256 
257     gst_bit_reader_init (&bits, pic_hdr, pic_hdr_len);
258     while ((gst_bit_reader_get_bits_uint8 (&bits, &b, 1))) {
259       g_strlcat (bit_str, b ? "1" : "0", sizeof (bit_str));
260     }
261     GST_TRACE_OBJECT (rtph263pdepay, "pic_hdr bits: %s", bit_str);
262   }
263 #endif
264 
265   gst_bit_reader_init (&bits, pic_hdr, pic_hdr_len);
266 
267   /* PSC - Picture Start Code: 22 bits: 0000 0000 0000 0000 10 0000 */
268   if (!gst_bit_reader_get_bits_uint32 (&bits, &psc, 22) || psc != 0x20) {
269     GST_WARNING_OBJECT (rtph263pdepay, "No picture start code");
270     return;
271   }
272 
273   /* TR - Temporal Reference: 8 bits */
274   if (!gst_bit_reader_skip (&bits, 8)) {
275     GST_WARNING_OBJECT (rtph263pdepay, "Short picture header: no TR");
276     return;
277   }
278 
279   /* PTYPE (first 8 bits) */
280   if (!gst_bit_reader_get_bits_uint32 (&bits, &ptype, 8) || (ptype >> 6) != 2) {
281     GST_WARNING_OBJECT (rtph263pdepay, "Short picture header: no PTYPE");
282     return;
283   }
284 
285   /* PTYPE: check for extended PTYPE (bits 6-8 = 111) */
286   if ((ptype & 7) != 7) {
287     /* No extended PTYPE, read remaining 5 bits */
288     if (!gst_bit_reader_get_bits_uint32 (&bits, &ptype, 5)) {
289       GST_WARNING_OBJECT (rtph263pdepay, "Short picture header: no PTYPE");
290       return;
291     }
292     is_intra = (ptype & 0x10) == 0;
293     goto done;
294   }
295 
296   /* UFEP - Update Full Extended PTYPE */
297   ufep = 0;
298   if (!gst_bit_reader_get_bits_uint8 (&bits, &ufep, 3) || ufep > 1) {
299     GST_WARNING_OBJECT (rtph263pdepay, "Short picture header: no PLUSPTYPE, %d",
300         ufep);
301     return;
302   }
303 
304   /* Skip optional part of PLUSPTYPE (OPPTYPE) */
305   if (ufep == 1 && !gst_bit_reader_skip (&bits, 18)) {
306     GST_WARNING_OBJECT (rtph263pdepay, "Short picture header: no OPPTYPE");
307     return;
308   }
309 
310   /* Mandatory part of PLUSPTYPE (MPPTYPE) */
311   if (!gst_bit_reader_get_bits_uint32 (&bits, &mpptype, 9)
312       || (mpptype & 7) != 1) {
313     GST_WARNING_OBJECT (rtph263pdepay, "Short picture header: no MPPTYPE");
314     return;
315   }
316 
317   is_intra = (mpptype >> 6) == 0;
318 
319 done:
320 
321   if (is_intra) {
322     GST_LOG_OBJECT (rtph263pdepay, "I-frame");
323     GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
324   } else {
325     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
326   }
327 }
328 
329 static GstBuffer *
gst_rtp_h263p_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)330 gst_rtp_h263p_depay_process (GstRTPBaseDepayload * depayload,
331     GstRTPBuffer * rtp)
332 {
333   GstRtpH263PDepay *rtph263pdepay;
334   GstBuffer *outbuf;
335   gint payload_len;
336   guint8 *payload;
337   gboolean P, V, M;
338   guint header_len;
339   guint8 PLEN, PEBIT;
340 
341   rtph263pdepay = GST_RTP_H263P_DEPAY (depayload);
342 
343   /* flush remaining data on discont */
344   if (GST_BUFFER_IS_DISCONT (rtp->buffer)) {
345     GST_LOG_OBJECT (depayload, "DISCONT, flushing adapter");
346     gst_adapter_clear (rtph263pdepay->adapter);
347     rtph263pdepay->wait_start = TRUE;
348   }
349 
350   payload_len = gst_rtp_buffer_get_payload_len (rtp);
351   header_len = 2;
352 
353   if (payload_len < header_len)
354     goto too_small;
355 
356   payload = gst_rtp_buffer_get_payload (rtp);
357 
358   M = gst_rtp_buffer_get_marker (rtp);
359 
360   /*  0                   1
361    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
362    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
363    * |   RR    |P|V|   PLEN    |PEBIT|
364    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
365    */
366   P = (payload[0] & 0x04) == 0x04;
367   V = (payload[0] & 0x02) == 0x02;
368   PLEN = ((payload[0] & 0x1) << 5) | (payload[1] >> 3);
369   PEBIT = payload[1] & 0x7;
370 
371   GST_LOG_OBJECT (depayload, "P %d, V %d, PLEN %d, PEBIT %d", P, V, PLEN,
372       PEBIT);
373 
374   if (V) {
375     header_len++;
376   }
377   if (PLEN) {
378     header_len += PLEN;
379   }
380 
381   if ((!P && payload_len < header_len) || (P && payload_len < header_len - 2))
382     goto too_small;
383 
384   if (P) {
385     rtph263pdepay->wait_start = FALSE;
386     header_len -= 2;
387   }
388 
389   if (rtph263pdepay->wait_start)
390     goto waiting_start;
391 
392   if (payload_len < header_len)
393     goto too_small;
394 
395   /* FIXME do not ignore the VRC header (See RFC 2429 section 4.2) */
396   /* FIXME actually use the RTP picture header when it is lost in the network */
397   /* for now strip off header */
398   payload_len -= header_len;
399 
400   if (M) {
401     /* frame is completed: append to previous, push it out */
402     guint len, padlen;
403     guint avail;
404     GstBuffer *padbuf;
405 
406     GST_LOG_OBJECT (depayload, "Frame complete");
407 
408     outbuf =
409         gst_rtp_buffer_get_payload_subbuffer (rtp, header_len, payload_len);
410     if (P)
411       gst_buffer_memset (outbuf, 0, 0, 2);
412     gst_adapter_push (rtph263pdepay->adapter, outbuf);
413     outbuf = NULL;
414 
415     avail = gst_adapter_available (rtph263pdepay->adapter);
416     len = avail + payload_len;
417     padlen = (len % 4) + 4;
418 
419     if (avail == 0)
420       goto empty_frame;
421 
422     outbuf = gst_adapter_take_buffer (rtph263pdepay->adapter, avail);
423     if (padlen) {
424       padbuf = gst_buffer_new_and_alloc (padlen);
425       gst_buffer_memset (padbuf, 0, 0, padlen);
426       outbuf = gst_buffer_append (outbuf, padbuf);
427     }
428 
429     gst_rtp_drop_non_video_meta (rtph263pdepay, outbuf);
430 
431     gst_rtp_h263p_depay_decorate_output_buffer (rtph263pdepay, outbuf);
432 
433     return outbuf;
434   } else {
435     /* frame not completed: store in adapter */
436     GST_LOG_OBJECT (depayload, "Frame incomplete, storing %d", payload_len);
437 
438     outbuf =
439         gst_rtp_buffer_get_payload_subbuffer (rtp, header_len, payload_len);
440     if (P)
441       gst_buffer_memset (outbuf, 0, 0, 2);
442     gst_adapter_push (rtph263pdepay->adapter, outbuf);
443   }
444   return NULL;
445 
446 too_small:
447   {
448     GST_ELEMENT_WARNING (rtph263pdepay, STREAM, DECODE,
449         ("Packet payload was too small"), (NULL));
450     return NULL;
451   }
452 waiting_start:
453   {
454     GST_DEBUG_OBJECT (rtph263pdepay, "waiting for picture start");
455     return NULL;
456   }
457 empty_frame:
458   {
459     GST_WARNING_OBJECT (rtph263pdepay, "Depayloaded frame is empty, dropping");
460     return NULL;
461   }
462 }
463 
464 static GstStateChangeReturn
gst_rtp_h263p_depay_change_state(GstElement * element,GstStateChange transition)465 gst_rtp_h263p_depay_change_state (GstElement * element,
466     GstStateChange transition)
467 {
468   GstRtpH263PDepay *rtph263pdepay;
469   GstStateChangeReturn ret;
470 
471   rtph263pdepay = GST_RTP_H263P_DEPAY (element);
472 
473   switch (transition) {
474     case GST_STATE_CHANGE_NULL_TO_READY:
475       break;
476     case GST_STATE_CHANGE_READY_TO_PAUSED:
477       gst_adapter_clear (rtph263pdepay->adapter);
478       rtph263pdepay->wait_start = TRUE;
479       break;
480     default:
481       break;
482   }
483 
484   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
485 
486   switch (transition) {
487     case GST_STATE_CHANGE_READY_TO_NULL:
488       break;
489     default:
490       break;
491   }
492   return ret;
493 }
494