2 * Copyright (C) <2006> 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.
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtph264depay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtph264depay_debug);
31 #define GST_CAT_DEFAULT (rtph264depay_debug)
33 #define DEFAULT_BYTE_STREAM TRUE
43 /* 3 zero bytes syncword */
44 static const guint8 sync_bytes[] = { 0, 0, 0, 1 };
46 /* elementfactory information */
47 static const GstElementDetails gst_rtp_h264depay_details =
48 GST_ELEMENT_DETAILS ("RTP H264 depayloader",
49 "Codec/Depayloader/Network",
50 "Extracts H264 video from RTP packets (RFC 3984)",
51 "Wim Taymans <wim.taymans@gmail.com>");
53 static GstStaticPadTemplate gst_rtp_h264_depay_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
57 GST_STATIC_CAPS ("video/x-h264")
60 static GstStaticPadTemplate gst_rtp_h264_depay_sink_template =
61 GST_STATIC_PAD_TEMPLATE ("sink",
64 GST_STATIC_CAPS ("application/x-rtp, "
65 "media = (string) \"video\", "
66 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
67 "clock-rate = (int) 90000, " "encoding-name = (string) \"H264\"")
68 /** optional parameters **/
69 /* "profile-level-id = (string) ANY, " */
70 /* "max-mbps = (string) ANY, " */
71 /* "max-fs = (string) ANY, " */
72 /* "max-cpb = (string) ANY, " */
73 /* "max-dpb = (string) ANY, " */
74 /* "max-br = (string) ANY, " */
75 /* "redundant-pic-cap = (string) { \"0\", \"1\" }, " */
76 /* "sprop-parameter-sets = (string) ANY, " */
77 /* "parameter-add = (string) { \"0\", \"1\" }, " */
78 /* "packetization-mode = (string) { \"0\", \"1\", \"2\" }, " */
79 /* "sprop-interleaving-depth = (string) ANY, " */
80 /* "sprop-deint-buf-req = (string) ANY, " */
81 /* "deint-buf-cap = (string) ANY, " */
82 /* "sprop-init-buf-time = (string) ANY, " */
83 /* "sprop-max-don-diff = (string) ANY, " */
84 /* "max-rcmd-nalu-size = (string) ANY " */
87 GST_BOILERPLATE (GstRtpH264Depay, gst_rtp_h264_depay, GstBaseRTPDepayload,
88 GST_TYPE_BASE_RTP_DEPAYLOAD);
90 static void gst_rtp_h264_depay_finalize (GObject * object);
91 static void gst_rtp_h264_depay_set_property (GObject * object, guint prop_id,
92 const GValue * value, GParamSpec * pspec);
93 static void gst_rtp_h264_depay_get_property (GObject * object, guint prop_id,
94 GValue * value, GParamSpec * pspec);
96 static GstStateChangeReturn gst_rtp_h264_depay_change_state (GstElement *
97 element, GstStateChange transition);
99 static GstBuffer *gst_rtp_h264_depay_process (GstBaseRTPDepayload * depayload,
101 static gboolean gst_rtp_h264_depay_setcaps (GstBaseRTPDepayload * filter,
105 gst_rtp_h264_depay_base_init (gpointer klass)
107 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
109 gst_element_class_add_pad_template (element_class,
110 gst_static_pad_template_get (&gst_rtp_h264_depay_src_template));
111 gst_element_class_add_pad_template (element_class,
112 gst_static_pad_template_get (&gst_rtp_h264_depay_sink_template));
114 gst_element_class_set_details (element_class, &gst_rtp_h264depay_details);
118 gst_rtp_h264_depay_class_init (GstRtpH264DepayClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
122 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
124 gobject_class = (GObjectClass *) klass;
125 gstelement_class = (GstElementClass *) klass;
126 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
128 gobject_class->finalize = gst_rtp_h264_depay_finalize;
130 gobject_class->set_property = gst_rtp_h264_depay_set_property;
131 gobject_class->get_property = gst_rtp_h264_depay_get_property;
133 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTE_STREAM,
134 g_param_spec_boolean ("byte-stream", "Byte Stream",
135 "Generate byte stream format of NALU", DEFAULT_BYTE_STREAM,
136 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
138 gstelement_class->change_state = gst_rtp_h264_depay_change_state;
140 gstbasertpdepayload_class->process = gst_rtp_h264_depay_process;
141 gstbasertpdepayload_class->set_caps = gst_rtp_h264_depay_setcaps;
143 GST_DEBUG_CATEGORY_INIT (rtph264depay_debug, "rtph264depay", 0,
144 "H264 Video RTP Depayloader");
148 gst_rtp_h264_depay_init (GstRtpH264Depay * rtph264depay,
149 GstRtpH264DepayClass * klass)
151 rtph264depay->adapter = gst_adapter_new ();
152 rtph264depay->byte_stream = DEFAULT_BYTE_STREAM;
156 gst_rtp_h264_depay_finalize (GObject * object)
158 GstRtpH264Depay *rtph264depay;
160 rtph264depay = GST_RTP_H264_DEPAY (object);
162 if (rtph264depay->codec_data)
163 gst_buffer_unref (rtph264depay->codec_data);
165 g_object_unref (rtph264depay->adapter);
167 G_OBJECT_CLASS (parent_class)->finalize (object);
171 gst_rtp_h264_depay_set_property (GObject * object, guint prop_id,
172 const GValue * value, GParamSpec * pspec)
174 GstRtpH264Depay *rtph264depay;
176 rtph264depay = GST_RTP_H264_DEPAY (object);
179 case PROP_BYTE_STREAM:
180 rtph264depay->byte_stream = g_value_get_boolean (value);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 gst_rtp_h264_depay_get_property (GObject * object, guint prop_id,
190 GValue * value, GParamSpec * pspec)
192 GstRtpH264Depay *rtph264depay;
194 rtph264depay = GST_RTP_H264_DEPAY (object);
197 case PROP_BYTE_STREAM:
198 g_value_set_boolean (value, rtph264depay->byte_stream);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 gst_rtp_h264_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
211 GstStructure *structure = gst_caps_get_structure (caps, 0);
212 GstRtpH264Depay *rtph264depay;
213 const gchar *ps, *profile;
214 GstBuffer *codec_data;
218 rtph264depay = GST_RTP_H264_DEPAY (depayload);
220 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
222 depayload->clock_rate = clock_rate;
224 srccaps = gst_caps_new_simple ("video/x-h264", NULL);
226 /* Base64 encoded, comma separated config NALs */
227 ps = gst_structure_get_string (structure, "sprop-parameter-sets");
228 /* hex: AVCProfileIndication:8 | profile_compat:8 | AVCLevelIndication:8 */
229 profile = gst_structure_get_string (structure, "profile-level-id");
231 if (rtph264depay->byte_stream && ps != NULL) {
232 /* for bytestream we only need the parameter sets but we don't error out
233 * when they are not there, we assume they are in the stream. */
238 params = g_strsplit (ps, ",", 0);
240 /* count total number of bytes in base64. Also include the sync bytes in
241 * front of the params. */
243 for (i = 0; params[i]; i++) {
244 len += strlen (params[i]);
245 len += sizeof (sync_bytes);
247 /* we seriously overshoot the length, but it's fine. */
248 codec_data = gst_buffer_new_and_alloc (len);
249 b64 = GST_BUFFER_DATA (codec_data);
251 for (i = 0; params[i]; i++) {
255 GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
256 memcpy (b64, sync_bytes, sizeof (sync_bytes));
257 b64 += sizeof (sync_bytes);
259 g_base64_decode_step (params[i], strlen (params[i]), b64, &state,
261 GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
262 total += len + sizeof (sync_bytes);
265 GST_BUFFER_SIZE (codec_data) = total;
268 /* keep the codec_data, we need to send it as the first buffer. We cannot
269 * push it in the adapter because the adapter might be flushed on discont.
271 if (rtph264depay->codec_data)
272 gst_buffer_unref (rtph264depay->codec_data);
273 rtph264depay->codec_data = codec_data;
274 } else if (!rtph264depay->byte_stream) {
277 guint len, num_sps, num_pps;
282 if (ps == NULL || profile == NULL)
283 goto incomplete_caps;
285 params = g_strsplit (ps, ",", 0);
286 len = g_strv_length (params);
288 GST_DEBUG_OBJECT (depayload, "we have %d params", len);
290 sps = g_new0 (guint8 *, len + 1);
291 pps = g_new0 (guint8 *, len + 1);
292 num_sps = num_pps = 0;
294 /* start with 7 bytes header */
296 for (i = 0; params[i]; i++) {
302 nal_len = strlen (params[i]);
303 nalp = g_malloc (nal_len + 2);
306 g_base64_decode_step (params[i], nal_len, nalp + 2, &state, &save);
307 nalp[0] = (nal_len >> 8) & 0xff;
308 nalp[1] = nal_len & 0xff;
311 /* copy to the right list */
312 if ((nalp[2] & 0x1f) == 7) {
313 GST_DEBUG_OBJECT (depayload, "adding param %d as SPS %d", i, num_sps);
314 sps[num_sps++] = nalp;
316 GST_DEBUG_OBJECT (depayload, "adding param %d as PPS %d", i, num_pps);
317 pps[num_pps++] = nalp;
322 codec_data = gst_buffer_new_and_alloc (len);
323 data = GST_BUFFER_DATA (codec_data);
325 /* 8 bits version == 1 */
327 /* hex: AVCProfileIndication:8 | profile_compat:8 | AVCLevelIndication:8 */
328 sscanf (profile, "%6x", &profile_id);
329 *data++ = (profile_id >> 16) & 0xff;
330 *data++ = (profile_id >> 8) & 0xff;
331 *data++ = profile_id & 0xff;
332 /* 6 bits reserved | 2 bits lengthSizeMinusOn */
334 /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
335 *data++ = 0xe0 | (num_sps & 0x1f);
338 for (i = 0; sps[i]; i++) {
339 len = ((sps[i][0] << 8) | sps[i][1]) + 2;
340 GST_DEBUG_OBJECT (depayload, "copy SPS %d of length %d", i, len);
341 memcpy (data, sps[i], len);
346 /* 8 bits numOfPictureParameterSets */
349 for (i = 0; pps[i]; i++) {
350 len = ((pps[i][0] << 8) | pps[i][1]) + 2;
351 GST_DEBUG_OBJECT (depayload, "copy PPS %d of length %d", i, len);
352 memcpy (data, pps[i], len);
357 GST_BUFFER_SIZE (codec_data) = data - GST_BUFFER_DATA (codec_data);
359 gst_caps_set_simple (srccaps,
360 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
363 res = gst_pad_set_caps (depayload->srcpad, srccaps);
364 gst_caps_unref (srccaps);
371 GST_DEBUG_OBJECT (depayload, "we have incomplete caps");
372 gst_caps_unref (srccaps);
377 /* FIXME, non-bytestream handling is freaking out ffmpeg. Apparently we need to
378 * group all NAL units belonging to one frame together */
380 gst_rtp_h264_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
382 GstRtpH264Depay *rtph264depay;
384 guint8 nal_unit_type;
386 rtph264depay = GST_RTP_H264_DEPAY (depayload);
388 /* flush remaining data on discont */
389 if (GST_BUFFER_IS_DISCONT (buf)) {
390 gst_adapter_clear (rtph264depay->adapter);
391 rtph264depay->wait_start = TRUE;
400 guint outsize, nalu_size;
402 payload_len = gst_rtp_buffer_get_payload_len (buf);
403 payload = gst_rtp_buffer_get_payload (buf);
405 GST_DEBUG_OBJECT (rtph264depay, "receiving %d bytes", payload_len);
415 nal_ref_idc = (payload[0] & 0x60) >> 5;
416 nal_unit_type = payload[0] & 0x1f;
418 /* at least one byte header with type */
421 GST_DEBUG_OBJECT (rtph264depay, "NRI %d, Type %d", nal_ref_idc,
424 switch (nal_unit_type) {
431 /* STAP-B Single-time aggregation packet 5.7.1 */
432 /* 2 byte extra header for DON */
438 payload += header_len;
439 payload_len -= header_len;
441 rtph264depay->wait_start = FALSE;
443 /* prepend codec_data */
444 if (rtph264depay->codec_data) {
445 gst_adapter_push (rtph264depay->adapter, rtph264depay->codec_data);
446 rtph264depay->codec_data = NULL;
449 /* STAP-A Single-time aggregation packet 5.7.1 */
450 while (payload_len > 2) {
452 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
453 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
455 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
457 nalu_size = (payload[0] << 8) | payload[1];
459 if (nalu_size > payload_len)
460 nalu_size = payload_len;
462 outsize = nalu_size + sizeof (sync_bytes);
463 outbuf = gst_buffer_new_and_alloc (outsize);
464 outdata = GST_BUFFER_DATA (outbuf);
465 if (rtph264depay->byte_stream) {
466 memcpy (outdata, sync_bytes, sizeof (sync_bytes));
468 outdata[0] = outdata[1] = 0;
469 outdata[2] = payload[0];
470 outdata[3] = payload[1];
473 /* strip NALU size */
477 outdata += sizeof (sync_bytes);
478 memcpy (outdata, payload, nalu_size);
480 gst_adapter_push (rtph264depay->adapter, outbuf);
482 payload += nalu_size;
483 payload_len -= nalu_size;
486 outsize = gst_adapter_available (rtph264depay->adapter);
487 outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
489 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
494 /* MTAP16 Multi-time aggregation packet 5.7.2 */
496 /* fallthrough, not implemented */
498 /* MTAP24 Multi-time aggregation packet 5.7.2 */
500 goto not_implemented;
505 /* FU-A Fragmentation unit 5.8 */
506 /* FU-B Fragmentation unit 5.8 */
515 * R is reserved and always 0
517 S = (payload[1] & 0x80) == 0x80;
518 E = (payload[1] & 0x40) == 0x40;
520 GST_DEBUG_OBJECT (rtph264depay, "S %d, E %d", S, E);
522 if (rtph264depay->wait_start && !S)
526 /* NAL unit starts here */
529 rtph264depay->wait_start = FALSE;
531 /* reconstruct NAL header */
532 nal_header = (payload[0] & 0xe0) | (payload[1] & 0x1f);
534 /* strip type header, keep FU header, we'll reuse it to reconstruct
539 nalu_size = payload_len;
540 outsize = nalu_size + sizeof (sync_bytes);
541 outbuf = gst_buffer_new_and_alloc (outsize);
542 outdata = GST_BUFFER_DATA (outbuf);
543 outdata += sizeof (sync_bytes);
544 memcpy (outdata, payload, nalu_size);
545 outdata[0] = nal_header;
547 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
549 /* and assemble in the adapter */
550 gst_adapter_push (rtph264depay->adapter, outbuf);
552 /* strip off FU indicator and FU header bytes */
556 outsize = payload_len;
557 outbuf = gst_buffer_new_and_alloc (outsize);
558 outdata = GST_BUFFER_DATA (outbuf);
559 memcpy (outdata, payload, outsize);
561 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
563 /* and assemble in the adapter */
564 gst_adapter_push (rtph264depay->adapter, outbuf);
567 /* if NAL unit ends, flush the adapter */
569 GST_DEBUG_OBJECT (rtph264depay, "output %d bytes", outsize);
571 outsize = gst_adapter_available (rtph264depay->adapter);
572 outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
573 outdata = GST_BUFFER_DATA (outbuf);
575 if (rtph264depay->byte_stream) {
576 memcpy (outdata, sync_bytes, sizeof (sync_bytes));
579 outdata[0] = (outsize >> 24);
580 outdata[1] = (outsize >> 16);
581 outdata[2] = (outsize >> 8);
582 outdata[3] = (outsize);
584 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
586 /* push codec_data first */
587 if (rtph264depay->codec_data) {
588 gst_buffer_set_caps (rtph264depay->codec_data,
589 GST_PAD_CAPS (depayload->srcpad));
590 gst_base_rtp_depayload_push (depayload, rtph264depay->codec_data);
591 rtph264depay->codec_data = NULL;
599 rtph264depay->wait_start = FALSE;
601 /* 1-23 NAL unit Single NAL unit packet per H.264 5.6 */
602 /* the entire payload is the output buffer */
603 nalu_size = payload_len;
604 outsize = nalu_size + sizeof (sync_bytes);
605 outbuf = gst_buffer_new_and_alloc (outsize);
606 outdata = GST_BUFFER_DATA (outbuf);
607 if (rtph264depay->byte_stream) {
608 memcpy (outdata, sync_bytes, sizeof (sync_bytes));
610 outdata[0] = outdata[1] = 0;
611 outdata[2] = nalu_size >> 8;
612 outdata[3] = nalu_size & 0xff;
614 outdata += sizeof (sync_bytes);
615 memcpy (outdata, payload, nalu_size);
617 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
619 /* push codec_data first */
620 if (rtph264depay->codec_data) {
621 gst_buffer_set_caps (rtph264depay->codec_data,
622 GST_PAD_CAPS (depayload->srcpad));
623 gst_base_rtp_depayload_push (depayload, rtph264depay->codec_data);
624 rtph264depay->codec_data = NULL;
636 GST_ELEMENT_WARNING (rtph264depay, STREAM, DECODE,
637 (NULL), ("Undefined packet type"));
642 GST_DEBUG_OBJECT (rtph264depay, "waiting for start");
647 GST_ELEMENT_ERROR (rtph264depay, STREAM, FORMAT,
648 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
653 static GstStateChangeReturn
654 gst_rtp_h264_depay_change_state (GstElement * element,
655 GstStateChange transition)
657 GstRtpH264Depay *rtph264depay;
658 GstStateChangeReturn ret;
660 rtph264depay = GST_RTP_H264_DEPAY (element);
662 switch (transition) {
663 case GST_STATE_CHANGE_NULL_TO_READY:
665 case GST_STATE_CHANGE_READY_TO_PAUSED:
666 gst_adapter_clear (rtph264depay->adapter);
667 rtph264depay->wait_start = TRUE;
673 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
675 switch (transition) {
676 case GST_STATE_CHANGE_READY_TO_NULL:
685 gst_rtp_h264_depay_plugin_init (GstPlugin * plugin)
687 return gst_element_register (plugin, "rtph264depay",
688 GST_RANK_MARGINAL, GST_TYPE_RTP_H264_DEPAY);