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
34 #define DEFAULT_ACCESS_UNIT FALSE
45 /* 3 zero bytes syncword */
46 static const guint8 sync_bytes[] = { 0, 0, 0, 1 };
48 static GstStaticPadTemplate gst_rtp_h264_depay_src_template =
49 GST_STATIC_PAD_TEMPLATE ("src",
52 GST_STATIC_CAPS ("video/x-h264")
55 static GstStaticPadTemplate gst_rtp_h264_depay_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("application/x-rtp, "
60 "media = (string) \"video\", "
61 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
62 "clock-rate = (int) 90000, " "encoding-name = (string) \"H264\"")
63 /** optional parameters **/
64 /* "profile-level-id = (string) ANY, " */
65 /* "max-mbps = (string) ANY, " */
66 /* "max-fs = (string) ANY, " */
67 /* "max-cpb = (string) ANY, " */
68 /* "max-dpb = (string) ANY, " */
69 /* "max-br = (string) ANY, " */
70 /* "redundant-pic-cap = (string) { \"0\", \"1\" }, " */
71 /* "sprop-parameter-sets = (string) ANY, " */
72 /* "parameter-add = (string) { \"0\", \"1\" }, " */
73 /* "packetization-mode = (string) { \"0\", \"1\", \"2\" }, " */
74 /* "sprop-interleaving-depth = (string) ANY, " */
75 /* "sprop-deint-buf-req = (string) ANY, " */
76 /* "deint-buf-cap = (string) ANY, " */
77 /* "sprop-init-buf-time = (string) ANY, " */
78 /* "sprop-max-don-diff = (string) ANY, " */
79 /* "max-rcmd-nalu-size = (string) ANY " */
82 GST_BOILERPLATE (GstRtpH264Depay, gst_rtp_h264_depay, GstBaseRTPDepayload,
83 GST_TYPE_BASE_RTP_DEPAYLOAD);
85 static void gst_rtp_h264_depay_finalize (GObject * object);
86 static void gst_rtp_h264_depay_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_rtp_h264_depay_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
91 static GstStateChangeReturn gst_rtp_h264_depay_change_state (GstElement *
92 element, GstStateChange transition);
94 static GstBuffer *gst_rtp_h264_depay_process (GstBaseRTPDepayload * depayload,
96 static gboolean gst_rtp_h264_depay_setcaps (GstBaseRTPDepayload * filter,
100 gst_rtp_h264_depay_base_init (gpointer klass)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
104 gst_element_class_add_pad_template (element_class,
105 gst_static_pad_template_get (&gst_rtp_h264_depay_src_template));
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get (&gst_rtp_h264_depay_sink_template));
109 gst_element_class_set_details_simple (element_class, "RTP H264 depayloader",
110 "Codec/Depayloader/Network/RTP",
111 "Extracts H264 video from RTP packets (RFC 3984)",
112 "Wim Taymans <wim.taymans@gmail.com>");
116 gst_rtp_h264_depay_class_init (GstRtpH264DepayClass * klass)
118 GObjectClass *gobject_class;
119 GstElementClass *gstelement_class;
120 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
122 gobject_class = (GObjectClass *) klass;
123 gstelement_class = (GstElementClass *) klass;
124 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
126 gobject_class->finalize = gst_rtp_h264_depay_finalize;
128 gobject_class->set_property = gst_rtp_h264_depay_set_property;
129 gobject_class->get_property = gst_rtp_h264_depay_get_property;
131 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BYTE_STREAM,
132 g_param_spec_boolean ("byte-stream", "Byte Stream",
133 "Generate byte stream format of NALU (deprecated; use caps)",
134 DEFAULT_BYTE_STREAM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
135 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ACCESS_UNIT,
136 g_param_spec_boolean ("access-unit", "Access Unit",
137 "Merge NALU into AU (picture) (deprecated; use caps)",
138 DEFAULT_ACCESS_UNIT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
140 gstelement_class->change_state = gst_rtp_h264_depay_change_state;
142 gstbasertpdepayload_class->process = gst_rtp_h264_depay_process;
143 gstbasertpdepayload_class->set_caps = gst_rtp_h264_depay_setcaps;
145 GST_DEBUG_CATEGORY_INIT (rtph264depay_debug, "rtph264depay", 0,
146 "H264 Video RTP Depayloader");
150 gst_rtp_h264_depay_init (GstRtpH264Depay * rtph264depay,
151 GstRtpH264DepayClass * klass)
153 rtph264depay->adapter = gst_adapter_new ();
154 rtph264depay->picture_adapter = gst_adapter_new ();
155 rtph264depay->byte_stream = DEFAULT_BYTE_STREAM;
156 rtph264depay->merge = DEFAULT_ACCESS_UNIT;
160 gst_rtp_h264_depay_finalize (GObject * object)
162 GstRtpH264Depay *rtph264depay;
164 rtph264depay = GST_RTP_H264_DEPAY (object);
166 if (rtph264depay->codec_data)
167 gst_buffer_unref (rtph264depay->codec_data);
169 g_object_unref (rtph264depay->adapter);
170 g_object_unref (rtph264depay->picture_adapter);
172 G_OBJECT_CLASS (parent_class)->finalize (object);
176 gst_rtp_h264_depay_set_property (GObject * object, guint prop_id,
177 const GValue * value, GParamSpec * pspec)
179 GstRtpH264Depay *rtph264depay;
181 rtph264depay = GST_RTP_H264_DEPAY (object);
184 case PROP_BYTE_STREAM:
185 rtph264depay->byte_stream = g_value_get_boolean (value);
187 case PROP_ACCESS_UNIT:
188 rtph264depay->merge = g_value_get_boolean (value);
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197 gst_rtp_h264_depay_get_property (GObject * object, guint prop_id,
198 GValue * value, GParamSpec * pspec)
200 GstRtpH264Depay *rtph264depay;
202 rtph264depay = GST_RTP_H264_DEPAY (object);
205 case PROP_BYTE_STREAM:
206 g_value_set_boolean (value, rtph264depay->byte_stream);
208 case PROP_ACCESS_UNIT:
209 g_value_set_boolean (value, rtph264depay->merge);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 gst_rtp_h264_depay_negotiate (GstRtpH264Depay * rtph264depay)
221 gint byte_stream = -1;
225 gst_pad_get_allowed_caps (GST_BASE_RTP_DEPAYLOAD_SRCPAD (rtph264depay));
227 GST_DEBUG_OBJECT (rtph264depay, "allowed caps: %" GST_PTR_FORMAT, caps);
230 if (gst_caps_get_size (caps) > 0) {
231 GstStructure *s = gst_caps_get_structure (caps, 0);
232 const gchar *str = NULL;
234 if ((str = gst_structure_get_string (s, "stream-format"))) {
235 if (strcmp (str, "avc") == 0) {
237 } else if (strcmp (str, "byte-stream") == 0) {
240 GST_DEBUG_OBJECT (rtph264depay, "unknown stream-format: %s", str);
244 if ((str = gst_structure_get_string (s, "alignment"))) {
245 if (strcmp (str, "au") == 0) {
247 } else if (strcmp (str, "nal") == 0) {
250 GST_DEBUG_OBJECT (rtph264depay, "unknown alignment: %s", str);
254 gst_caps_unref (caps);
257 if (byte_stream >= 0) {
258 GST_DEBUG_OBJECT (rtph264depay, "downstream requires byte-stream %d",
260 if (rtph264depay->byte_stream != byte_stream) {
261 GST_WARNING_OBJECT (rtph264depay,
262 "overriding property setting based on caps");
263 rtph264depay->byte_stream = byte_stream;
267 GST_DEBUG_OBJECT (rtph264depay, "downstream requires merge %d", merge);
268 if (rtph264depay->merge != merge) {
269 GST_WARNING_OBJECT (rtph264depay,
270 "overriding property setting based on caps");
271 rtph264depay->merge = merge;
277 gst_rtp_h264_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
281 GstStructure *structure = gst_caps_get_structure (caps, 0);
282 GstRtpH264Depay *rtph264depay;
283 const gchar *ps, *profile;
284 GstBuffer *codec_data;
288 rtph264depay = GST_RTP_H264_DEPAY (depayload);
290 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
292 depayload->clock_rate = clock_rate;
294 srccaps = gst_caps_new_simple ("video/x-h264", NULL);
296 /* Base64 encoded, comma separated config NALs */
297 ps = gst_structure_get_string (structure, "sprop-parameter-sets");
298 /* hex: AVCProfileIndication:8 | profile_compat:8 | AVCLevelIndication:8 */
299 profile = gst_structure_get_string (structure, "profile-level-id");
301 /* negotiate with downstream w.r.t. output format and alignment */
302 gst_rtp_h264_depay_negotiate (rtph264depay);
304 if (rtph264depay->byte_stream && ps != NULL) {
305 /* for bytestream we only need the parameter sets but we don't error out
306 * when they are not there, we assume they are in the stream. */
311 params = g_strsplit (ps, ",", 0);
313 /* count total number of bytes in base64. Also include the sync bytes in
314 * front of the params. */
316 for (i = 0; params[i]; i++) {
317 len += strlen (params[i]);
318 len += sizeof (sync_bytes);
320 /* we seriously overshoot the length, but it's fine. */
321 codec_data = gst_buffer_new_and_alloc (len);
322 b64 = GST_BUFFER_DATA (codec_data);
324 for (i = 0; params[i]; i++) {
328 GST_DEBUG_OBJECT (depayload, "decoding param %d (%s)", i, params[i]);
329 memcpy (b64, sync_bytes, sizeof (sync_bytes));
330 b64 += sizeof (sync_bytes);
332 g_base64_decode_step (params[i], strlen (params[i]), b64, &state,
334 GST_DEBUG_OBJECT (depayload, "decoded %d bytes", len);
335 total += len + sizeof (sync_bytes);
338 GST_BUFFER_SIZE (codec_data) = total;
341 /* keep the codec_data, we need to send it as the first buffer. We cannot
342 * push it in the adapter because the adapter might be flushed on discont.
344 if (rtph264depay->codec_data)
345 gst_buffer_unref (rtph264depay->codec_data);
346 rtph264depay->codec_data = codec_data;
347 } else if (!rtph264depay->byte_stream) {
350 guint len, num_sps, num_pps;
355 goto incomplete_caps;
357 params = g_strsplit (ps, ",", 0);
358 len = g_strv_length (params);
360 GST_DEBUG_OBJECT (depayload, "we have %d params", len);
362 sps = g_new0 (guint8 *, len + 1);
363 pps = g_new0 (guint8 *, len + 1);
364 num_sps = num_pps = 0;
366 /* start with 7 bytes header */
368 for (i = 0; params[i]; i++) {
374 nal_len = strlen (params[i]);
375 nalp = g_malloc (nal_len + 2);
378 g_base64_decode_step (params[i], nal_len, nalp + 2, &state, &save);
379 nalp[0] = (nal_len >> 8) & 0xff;
380 nalp[1] = nal_len & 0xff;
383 /* copy to the right list */
384 if ((nalp[2] & 0x1f) == 7) {
385 GST_DEBUG_OBJECT (depayload, "adding param %d as SPS %d", i, num_sps);
386 sps[num_sps++] = nalp;
388 GST_DEBUG_OBJECT (depayload, "adding param %d as PPS %d", i, num_pps);
389 pps[num_pps++] = nalp;
394 if (num_sps == 0 || (GST_READ_UINT16_BE (sps[0]) < 3) || num_pps == 0) {
395 g_strfreev ((gchar **) pps);
396 g_strfreev ((gchar **) sps);
397 goto incomplete_caps;
400 codec_data = gst_buffer_new_and_alloc (len);
401 data = GST_BUFFER_DATA (codec_data);
403 /* 8 bits version == 1 */
408 /* hex: AVCProfileIndication:8 | profile_compat:8 | AVCLevelIndication:8 */
409 sscanf (profile, "%6x", &profile_id);
410 *data++ = (profile_id >> 16) & 0xff;
411 *data++ = (profile_id >> 8) & 0xff;
412 *data++ = profile_id & 0xff;
414 /* extract from SPS */
419 /* 6 bits reserved | 2 bits lengthSizeMinusOn */
421 /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
422 *data++ = 0xe0 | (num_sps & 0x1f);
425 for (i = 0; sps[i]; i++) {
426 len = ((sps[i][0] << 8) | sps[i][1]) + 2;
427 GST_DEBUG_OBJECT (depayload, "copy SPS %d of length %d", i, len);
428 memcpy (data, sps[i], len);
433 /* 8 bits numOfPictureParameterSets */
436 for (i = 0; pps[i]; i++) {
437 len = ((pps[i][0] << 8) | pps[i][1]) + 2;
438 GST_DEBUG_OBJECT (depayload, "copy PPS %d of length %d", i, len);
439 memcpy (data, pps[i], len);
444 GST_BUFFER_SIZE (codec_data) = data - GST_BUFFER_DATA (codec_data);
446 gst_caps_set_simple (srccaps,
447 "codec_data", GST_TYPE_BUFFER, codec_data, NULL);
448 gst_buffer_unref (codec_data);
451 gst_caps_set_simple (srccaps, "stream-format", G_TYPE_STRING,
452 rtph264depay->byte_stream ? "byte-stream" : "avc",
453 "alignment", G_TYPE_STRING, rtph264depay->merge ? "au" : "nal", NULL);
455 res = gst_pad_set_caps (depayload->srcpad, srccaps);
456 gst_caps_unref (srccaps);
463 GST_DEBUG_OBJECT (depayload, "we have incomplete caps");
464 gst_caps_unref (srccaps);
469 /* SPS/PPS/IDR considered key, all others DELTA;
470 * so downstream waiting for keyframe can pick up at SPS/PPS/IDR */
471 #define NAL_TYPE_IS_KEY(nt) (((nt) == 5) || ((nt) == 7) || ((nt) == 8))
474 gst_rtp_h264_depay_handle_nal (GstRtpH264Depay * rtph264depay, GstBuffer * nal,
475 GstClockTime in_timestamp)
477 GstBaseRTPDepayload *depayload = GST_BASE_RTP_DEPAYLOAD (rtph264depay);
481 GstBuffer *outbuf = NULL;
482 GstClockTime out_timestamp;
483 gboolean keyframe, out_keyframe;
485 size = GST_BUFFER_SIZE (nal);
486 if (G_UNLIKELY (size < 5))
489 data = GST_BUFFER_DATA (nal);
491 nal_type = data[4] & 0x1f;
492 GST_DEBUG_OBJECT (rtph264depay, "handle NAL type %d", nal_type);
494 keyframe = NAL_TYPE_IS_KEY (nal_type);
496 out_keyframe = keyframe;
497 out_timestamp = in_timestamp;
499 if (rtph264depay->merge) {
500 gboolean start = FALSE, complete = FALSE;
502 /* consider a coded slices (IDR or not) to start a picture,
503 * (so ending the previous one) if first_mb_in_slice == 0
504 * (non-0 is part of previous one) */
505 /* NOTE this is not entirely according to Access Unit specs in 7.4.1.2.4,
506 * but in practice it works in sane cases, needs not much parsing,
507 * and also works with broken frame_num in NAL (where spec-wise would fail) */
508 if (nal_type == 1 || nal_type == 2 || nal_type == 5) {
509 /* we have a picture start */
511 if (data[5] & 0x80) {
512 /* first_mb_in_slice == 0 completes a picture */
515 } else if (nal_type >= 6 && nal_type <= 9) {
516 /* SEI, SPS, PPS, AU terminate picture */
519 GST_DEBUG_OBJECT (depayload, "start %d, complete %d", start, complete);
521 if (complete && rtph264depay->picture_start) {
524 /* we had a picture in the adapter and we completed it */
525 GST_DEBUG_OBJECT (depayload, "taking completed AU");
526 outsize = gst_adapter_available (rtph264depay->picture_adapter);
527 outbuf = gst_adapter_take_buffer (rtph264depay->picture_adapter, outsize);
529 out_timestamp = rtph264depay->last_ts;
530 out_keyframe = rtph264depay->last_keyframe;
532 rtph264depay->last_keyframe = FALSE;
533 rtph264depay->picture_start = FALSE;
537 GST_DEBUG_OBJECT (depayload, "adding NAL to picture adapter");
538 gst_adapter_push (rtph264depay->picture_adapter, nal);
539 rtph264depay->last_ts = in_timestamp;
540 rtph264depay->last_keyframe |= keyframe;
541 rtph264depay->picture_start |= start;
543 /* no merge, output is input nal */
544 GST_DEBUG_OBJECT (depayload, "using NAL as output");
549 /* prepend codec_data */
550 if (rtph264depay->codec_data) {
551 GST_DEBUG_OBJECT (depayload, "prepending codec_data");
552 outbuf = gst_buffer_join (rtph264depay->codec_data, outbuf);
553 rtph264depay->codec_data = NULL;
556 outbuf = gst_buffer_make_metadata_writable (outbuf);
558 GST_BUFFER_TIMESTAMP (outbuf) = out_timestamp;
561 GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
563 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
565 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
567 gst_base_rtp_depayload_push (depayload, outbuf);
575 GST_WARNING_OBJECT (depayload, "dropping short NAL");
576 gst_buffer_unref (nal);
582 gst_rtp_h264_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
584 GstRtpH264Depay *rtph264depay;
586 guint8 nal_unit_type;
588 rtph264depay = GST_RTP_H264_DEPAY (depayload);
590 /* flush remaining data on discont */
591 if (GST_BUFFER_IS_DISCONT (buf)) {
592 gst_adapter_clear (rtph264depay->adapter);
593 rtph264depay->wait_start = TRUE;
602 guint outsize, nalu_size;
603 GstClockTime timestamp;
605 timestamp = GST_BUFFER_TIMESTAMP (buf);
607 payload_len = gst_rtp_buffer_get_payload_len (buf);
608 payload = gst_rtp_buffer_get_payload (buf);
610 GST_DEBUG_OBJECT (rtph264depay, "receiving %d bytes", payload_len);
612 if (payload_len == 0)
623 nal_ref_idc = (payload[0] & 0x60) >> 5;
624 nal_unit_type = payload[0] & 0x1f;
626 /* at least one byte header with type */
629 GST_DEBUG_OBJECT (rtph264depay, "NRI %d, Type %d", nal_ref_idc,
632 switch (nal_unit_type) {
639 /* STAP-B Single-time aggregation packet 5.7.1 */
640 /* 2 byte extra header for DON */
646 payload += header_len;
647 payload_len -= header_len;
649 rtph264depay->wait_start = FALSE;
652 /* STAP-A Single-time aggregation packet 5.7.1 */
653 while (payload_len > 2) {
655 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
656 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
658 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
660 nalu_size = (payload[0] << 8) | payload[1];
662 if (nalu_size > payload_len)
663 nalu_size = payload_len;
665 outsize = nalu_size + sizeof (sync_bytes);
666 outbuf = gst_buffer_new_and_alloc (outsize);
667 outdata = GST_BUFFER_DATA (outbuf);
668 if (rtph264depay->byte_stream) {
669 memcpy (outdata, sync_bytes, sizeof (sync_bytes));
671 outdata[0] = outdata[1] = 0;
672 outdata[2] = payload[0];
673 outdata[3] = payload[1];
676 /* strip NALU size */
680 outdata += sizeof (sync_bytes);
681 memcpy (outdata, payload, nalu_size);
683 gst_adapter_push (rtph264depay->adapter, outbuf);
685 payload += nalu_size;
686 payload_len -= nalu_size;
689 outsize = gst_adapter_available (rtph264depay->adapter);
690 outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
692 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp);
696 /* MTAP16 Multi-time aggregation packet 5.7.2 */
698 /* fallthrough, not implemented */
700 /* MTAP24 Multi-time aggregation packet 5.7.2 */
702 goto not_implemented;
707 /* FU-A Fragmentation unit 5.8 */
708 /* FU-B Fragmentation unit 5.8 */
717 * R is reserved and always 0
719 S = (payload[1] & 0x80) == 0x80;
720 E = (payload[1] & 0x40) == 0x40;
722 GST_DEBUG_OBJECT (rtph264depay, "S %d, E %d", S, E);
724 if (rtph264depay->wait_start && !S)
728 /* NAL unit starts here */
731 rtph264depay->wait_start = FALSE;
733 /* reconstruct NAL header */
734 nal_header = (payload[0] & 0xe0) | (payload[1] & 0x1f);
736 /* strip type header, keep FU header, we'll reuse it to reconstruct
741 nalu_size = payload_len;
742 outsize = nalu_size + sizeof (sync_bytes);
743 outbuf = gst_buffer_new_and_alloc (outsize);
744 outdata = GST_BUFFER_DATA (outbuf);
745 outdata += sizeof (sync_bytes);
746 memcpy (outdata, payload, nalu_size);
747 outdata[0] = nal_header;
749 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
751 /* and assemble in the adapter */
752 gst_adapter_push (rtph264depay->adapter, outbuf);
754 /* strip off FU indicator and FU header bytes */
758 outsize = payload_len;
759 outbuf = gst_buffer_new_and_alloc (outsize);
760 outdata = GST_BUFFER_DATA (outbuf);
761 memcpy (outdata, payload, outsize);
763 GST_DEBUG_OBJECT (rtph264depay, "queueing %d bytes", outsize);
765 /* and assemble in the adapter */
766 gst_adapter_push (rtph264depay->adapter, outbuf);
769 /* if NAL unit ends, flush the adapter */
771 GST_DEBUG_OBJECT (rtph264depay, "output %d bytes", outsize);
773 outsize = gst_adapter_available (rtph264depay->adapter);
774 outbuf = gst_adapter_take_buffer (rtph264depay->adapter, outsize);
775 outdata = GST_BUFFER_DATA (outbuf);
777 if (rtph264depay->byte_stream) {
778 memcpy (outdata, sync_bytes, sizeof (sync_bytes));
781 outdata[0] = (outsize >> 24);
782 outdata[1] = (outsize >> 16);
783 outdata[2] = (outsize >> 8);
784 outdata[3] = (outsize);
786 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp);
792 rtph264depay->wait_start = FALSE;
794 /* 1-23 NAL unit Single NAL unit packet per H.264 5.6 */
795 /* the entire payload is the output buffer */
796 nalu_size = payload_len;
797 outsize = nalu_size + sizeof (sync_bytes);
798 outbuf = gst_buffer_new_and_alloc (outsize);
799 outdata = GST_BUFFER_DATA (outbuf);
800 if (rtph264depay->byte_stream) {
801 memcpy (outdata, sync_bytes, sizeof (sync_bytes));
803 outdata[0] = outdata[1] = 0;
804 outdata[2] = nalu_size >> 8;
805 outdata[3] = nalu_size & 0xff;
807 outdata += sizeof (sync_bytes);
808 memcpy (outdata, payload, nalu_size);
810 gst_rtp_h264_depay_handle_nal (rtph264depay, outbuf, timestamp);
821 GST_ELEMENT_WARNING (rtph264depay, STREAM, DECODE,
822 (NULL), ("Undefined packet type"));
827 GST_DEBUG_OBJECT (rtph264depay, "waiting for start");
832 GST_ELEMENT_ERROR (rtph264depay, STREAM, FORMAT,
833 (NULL), ("NAL unit type %d not supported yet", nal_unit_type));
838 static GstStateChangeReturn
839 gst_rtp_h264_depay_change_state (GstElement * element,
840 GstStateChange transition)
842 GstRtpH264Depay *rtph264depay;
843 GstStateChangeReturn ret;
845 rtph264depay = GST_RTP_H264_DEPAY (element);
847 switch (transition) {
848 case GST_STATE_CHANGE_NULL_TO_READY:
850 case GST_STATE_CHANGE_READY_TO_PAUSED:
851 gst_adapter_clear (rtph264depay->adapter);
852 rtph264depay->wait_start = TRUE;
853 gst_adapter_clear (rtph264depay->picture_adapter);
854 rtph264depay->picture_start = FALSE;
855 rtph264depay->last_keyframe = FALSE;
861 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
863 switch (transition) {
864 case GST_STATE_CHANGE_READY_TO_NULL:
873 gst_rtp_h264_depay_plugin_init (GstPlugin * plugin)
875 return gst_element_register (plugin, "rtph264depay",
876 GST_RANK_SECONDARY, GST_TYPE_RTP_H264_DEPAY);