rtph265depay: assemble AUs into downstream-allocated memory
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtph265depay.c
index eb0c2b7..d5ff4ab 100644 (file)
@@ -124,14 +124,14 @@ gst_rtp_h265_depay_class_init (GstRtpH265DepayClass * klass)
 
   gobject_class->finalize = gst_rtp_h265_depay_finalize;
 
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&gst_rtp_h265_depay_src_template));
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&gst_rtp_h265_depay_sink_template));
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_h265_depay_src_template);
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_h265_depay_sink_template);
 
   gst_element_class_set_static_metadata (gstelement_class,
       "RTP H265 depayloader", "Codec/Depayloader/Network/RTP",
-      "Extracts H265 video from RTP packets (draft-ietf-payload-rtp-h265-03.txt)",
+      "Extracts H265 video from RTP packets (RFC 7798)",
       "Jurgen Slowack <jurgenslowack@gmail.com>");
   gstelement_class->change_state = gst_rtp_h265_depay_change_state;
 
@@ -146,7 +146,7 @@ gst_rtp_h265_depay_init (GstRtpH265Depay * rtph265depay)
   rtph265depay->adapter = gst_adapter_new ();
   rtph265depay->picture_adapter = gst_adapter_new ();
   rtph265depay->byte_stream = DEFAULT_BYTE_STREAM;
-  rtph265depay->stream_format = (gchar *) g_malloc (10);
+  rtph265depay->stream_format = NULL;
   rtph265depay->merge = DEFAULT_ACCESS_UNIT;
   rtph265depay->vps = g_ptr_array_new_with_free_func (
       (GDestroyNotify) gst_buffer_unref);
@@ -157,7 +157,7 @@ gst_rtp_h265_depay_init (GstRtpH265Depay * rtph265depay)
 }
 
 static void
-gst_rtp_h265_depay_reset (GstRtpH265Depay * rtph265depay)
+gst_rtp_h265_depay_reset (GstRtpH265Depay * rtph265depay, gboolean hard)
 {
   gst_adapter_clear (rtph265depay->adapter);
   rtph265depay->wait_start = TRUE;
@@ -170,6 +170,14 @@ gst_rtp_h265_depay_reset (GstRtpH265Depay * rtph265depay)
   g_ptr_array_set_size (rtph265depay->vps, 0);
   g_ptr_array_set_size (rtph265depay->sps, 0);
   g_ptr_array_set_size (rtph265depay->pps, 0);
+
+  if (hard) {
+    if (rtph265depay->allocator != NULL) {
+      gst_object_unref (rtph265depay->allocator);
+      rtph265depay->allocator = NULL;
+    }
+    gst_allocation_params_init (&rtph265depay->params);
+  }
 }
 
 static void
@@ -212,8 +220,8 @@ gst_rtp_h265_depay_negotiate (GstRtpH265Depay * rtph265depay)
       const gchar *str = NULL;
 
       if ((str = gst_structure_get_string (s, "stream-format"))) {
-
-        strcpy (rtph265depay->stream_format, str);
+        g_free (rtph265depay->stream_format);
+        rtph265depay->stream_format = g_strdup (str);
 
         if (strcmp (str, "hev1") == 0) {
           byte_stream = FALSE;
@@ -246,7 +254,8 @@ gst_rtp_h265_depay_negotiate (GstRtpH265Depay * rtph265depay)
   } else {
     GST_DEBUG_OBJECT (rtph265depay, "defaulting to byte-stream %d",
         DEFAULT_BYTE_STREAM);
-    strcpy (rtph265depay->stream_format, "byte-stream");
+    g_free (rtph265depay->stream_format);
+    rtph265depay->stream_format = g_strdup ("byte-stream");
     rtph265depay->byte_stream = DEFAULT_BYTE_STREAM;
   }
   if (merge != -1) {
@@ -291,13 +300,52 @@ parse_pps (GstMapInfo * map, guint32 * sps_id, guint32 * pps_id)
   return TRUE;
 }
 
+static gboolean
+gst_rtp_h265_depay_set_output_caps (GstRtpH265Depay * rtph265depay,
+    GstCaps * caps)
+{
+  GstAllocationParams params;
+  GstAllocator *allocator = NULL;
+  GstPad *srcpad;
+  gboolean res;
+
+  gst_allocation_params_init (&params);
+
+  srcpad = GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay);
+
+  res = gst_pad_set_caps (srcpad, caps);
+
+  if (res) {
+    GstQuery *query;
+
+    query = gst_query_new_allocation (caps, TRUE);
+    if (!gst_pad_peer_query (srcpad, query)) {
+      GST_DEBUG_OBJECT (rtph265depay, "downstream ALLOCATION query failed");
+    }
+
+    if (gst_query_get_n_allocation_params (query) > 0) {
+      gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
+    }
+
+    gst_query_unref (query);
+  }
+
+  if (rtph265depay->allocator)
+    gst_object_unref (rtph265depay->allocator);
+
+  rtph265depay->allocator = allocator;
+  rtph265depay->params = params;
+
+  return res;
+}
 
 static gboolean
 gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
 {
-  gboolean res;
+  gboolean res, update_caps;
   GstCaps *old_caps;
   GstCaps *srccaps;
+  GstPad *srcpad;
 
   if (!rtph265depay->byte_stream &&
       (!rtph265depay->new_codec_data ||
@@ -306,12 +354,10 @@ gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
     return TRUE;
 
   srccaps = gst_caps_new_simple ("video/x-h265",
-      "stream-format", G_TYPE_STRING,
-      rtph265depay->stream_format,
+      "stream-format", G_TYPE_STRING, rtph265depay->stream_format,
       "alignment", G_TYPE_STRING, rtph265depay->merge ? "au" : "nal", NULL);
 
   if (!rtph265depay->byte_stream) {
-
     GstBuffer *codec_data;
     gint i = 0;
     gint len;
@@ -354,7 +400,6 @@ gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
         num_sps, num_pps);
 
     codec_data = gst_buffer_new_and_alloc (len);
-    g_debug ("alloc_len: %u", len);
     gst_buffer_map (codec_data, &map, GST_MAP_READWRITE);
     data = map.data;
 
@@ -508,8 +553,9 @@ gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
     gst_buffer_unref (codec_data);
   }
 
-  old_caps =
-      gst_pad_get_current_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay));
+  srcpad = GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay);
+
+  old_caps = gst_pad_get_current_caps (srcpad);
   if (old_caps != NULL) {
 
     /* Only update the caps if they are not equal. For
@@ -518,12 +564,7 @@ gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
      * and gstrtph264depay
      */
     if (rtph265depay->byte_stream) {
-      if (!gst_caps_is_equal (srccaps, old_caps))
-        res =
-            gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
-            srccaps);
-      else
-        res = TRUE;
+      update_caps = !gst_caps_is_equal (srccaps, old_caps);
     } else {
       GstCaps *tmp_caps = gst_caps_copy (srccaps);
       GstStructure *old_s, *tmp_s;
@@ -534,19 +575,18 @@ gst_rtp_h265_set_src_caps (GstRtpH265Depay * rtph265depay)
         gst_structure_set_value (tmp_s, "codec_data",
             gst_structure_get_value (old_s, "codec_data"));
 
-      if (!gst_caps_is_equal (old_caps, tmp_caps))
-        res =
-            gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
-            srccaps);
-      else
-        res = TRUE;
-
+      update_caps = !gst_caps_is_equal (old_caps, tmp_caps);
       gst_caps_unref (tmp_caps);
     }
+    gst_caps_unref (old_caps);
   } else {
-    res =
-        gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (rtph265depay),
-        srccaps);
+    update_caps = TRUE;
+  }
+
+  if (update_caps) {
+    res = gst_rtp_h265_depay_set_output_caps (rtph265depay, srccaps);
+  } else {
+    res = TRUE;
   }
 
   gst_caps_unref (srccaps);
@@ -755,8 +795,10 @@ gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
   gint clock_rate;
   GstStructure *structure = gst_caps_get_structure (caps, 0);
   GstRtpH265Depay *rtph265depay;
-  const gchar *ps;
-  GstBuffer *codec_data;
+  const gchar *vps;
+  const gchar *sps;
+  const gchar *pps;
+  gchar *ps;
   GstMapInfo map;
   guint8 *ptr;
 
@@ -767,7 +809,14 @@ gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
   depayload->clock_rate = clock_rate;
 
   /* Base64 encoded, comma separated config NALs */
-  ps = gst_structure_get_string (structure, "sprop-parameter-sets");
+  vps = gst_structure_get_string (structure, "sprop-vps");
+  sps = gst_structure_get_string (structure, "sprop-sps");
+  pps = gst_structure_get_string (structure, "sprop-pps");
+  if (vps == NULL || sps == NULL || pps == NULL) {
+    ps = NULL;
+  } else {
+    ps = g_strdup_printf ("%s,%s,%s", vps, sps, pps);
+  }
 
   /* negotiate with downstream w.r.t. output format and alignment */
   gst_rtp_h265_depay_negotiate (rtph265depay);
@@ -776,6 +825,7 @@ gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
     /* for bytestream we only need the parameter sets but we don't error out
      * when they are not there, we assume they are in the stream. */
     gchar **params;
+    GstBuffer *codec_data;
     guint len, total;
     gint i;
 
@@ -838,6 +888,10 @@ gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
       gint state = 0;
 
       nal_len = strlen (params[i]);
+      if (nal_len == 0) {
+        GST_WARNING_OBJECT (depayload, "empty param '%s' (#%d)", params[i], i);
+        continue;
+      }
       nal = gst_buffer_new_and_alloc (nal_len);
       gst_buffer_map (nal, &nalmap, GST_MAP_READWRITE);
 
@@ -856,10 +910,14 @@ gst_rtp_h265_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
     }
     g_strfreev (params);
 
-    if (rtph265depay->sps->len == 0 || rtph265depay->pps->len == 0)
+    if (rtph265depay->vps->len == 0 || rtph265depay->sps->len == 0 ||
+        rtph265depay->pps->len == 0) {
       goto incomplete_caps;
+    }
   }
 
+  g_free (ps);
+
   return gst_rtp_h265_set_src_caps (rtph265depay);
 
   /* ERRORS */
@@ -867,21 +925,76 @@ incomplete_caps:
   {
     GST_DEBUG_OBJECT (depayload, "we have incomplete caps,"
         " doing setcaps later");
+    g_free (ps);
     return TRUE;
   }
 }
 
 static GstBuffer *
+gst_rtp_h265_depay_allocate_output_buffer (GstRtpH265Depay * depay, gsize size)
+{
+  GstBuffer *buffer = NULL;
+
+  g_return_val_if_fail (size > 0, NULL);
+
+  GST_LOG_OBJECT (depay, "want output buffer of %u bytes", (guint) size);
+
+  buffer = gst_buffer_new_allocate (depay->allocator, size, &depay->params);
+  if (buffer == NULL) {
+    GST_INFO_OBJECT (depay, "couldn't allocate output buffer");
+    buffer = gst_buffer_new_allocate (NULL, size, NULL);
+  }
+
+  return buffer;
+}
+
+static GstBuffer *
 gst_rtp_h265_complete_au (GstRtpH265Depay * rtph265depay,
     GstClockTime * out_timestamp, gboolean * out_keyframe)
 {
-  guint outsize;
+  GstBufferList *list;
+  GstMapInfo outmap;
   GstBuffer *outbuf;
+  guint outsize, offset = 0;
+  gint b, n_bufs, m, n_mem;
 
   /* we had a picture in the adapter and we completed it */
   GST_DEBUG_OBJECT (rtph265depay, "taking completed AU");
   outsize = gst_adapter_available (rtph265depay->picture_adapter);
-  outbuf = gst_adapter_take_buffer (rtph265depay->picture_adapter, outsize);
+
+  outbuf = gst_rtp_h265_depay_allocate_output_buffer (rtph265depay, outsize);
+
+  if (outbuf == NULL)
+    return NULL;
+
+  if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE))
+    return NULL;
+
+  list = gst_adapter_take_buffer_list (rtph265depay->picture_adapter, outsize);
+
+  n_bufs = gst_buffer_list_length (list);
+  for (b = 0; b < n_bufs; ++b) {
+    GstBuffer *buf = gst_buffer_list_get (list, b);
+
+    n_mem = gst_buffer_n_memory (buf);
+    for (m = 0; m < n_mem; ++m) {
+      GstMemory *mem = gst_buffer_peek_memory (buf, m);
+      gsize mem_size = gst_memory_get_sizes (mem, NULL, NULL);
+      GstMapInfo mem_map;
+
+      if (gst_memory_map (mem, &mem_map, GST_MAP_READ)) {
+        memcpy (outmap.data + offset, mem_map.data, mem_size);
+        gst_memory_unmap (mem, &mem_map);
+      } else {
+        memset (outmap.data + offset, 0, mem_size);
+      }
+      offset += mem_size;
+    }
+
+    gst_rtp_copy_video_meta (rtph265depay, outbuf, buf);
+  }
+  gst_buffer_list_unref (list);
+  gst_buffer_unmap (outbuf, &outmap);
 
   *out_timestamp = rtph265depay->last_ts;
   *out_keyframe = rtph265depay->last_keyframe;
@@ -914,9 +1027,17 @@ gst_rtp_h265_complete_au (GstRtpH265Depay * rtph265depay,
                                                                                                ||  ((nt) == GST_H265_NAL_SLICE_IDR_N_LP)\
                                                                                                ||  ((nt) == GST_H265_NAL_SLICE_CRA_NUT)                )
 
-#define NAL_TYPE_IS_KEY(nt) (NAL_TYPE_IS_PARAMETER_SET(nt) || NAL_TYPE_IS_CODED_SLICE_SEGMENT(nt))
+/* Intra random access point */
+#define NAL_TYPE_IS_IRAP(nt)   (((nt) == GST_H265_NAL_SLICE_BLA_W_LP)   \
+                             || ((nt) == GST_H265_NAL_SLICE_BLA_W_RADL) \
+                             || ((nt) == GST_H265_NAL_SLICE_BLA_N_LP)   \
+                             || ((nt) == GST_H265_NAL_SLICE_IDR_W_RADL) \
+                             || ((nt) == GST_H265_NAL_SLICE_IDR_N_LP)   \
+                             || ((nt) == GST_H265_NAL_SLICE_CRA_NUT))
 
-static GstBuffer *
+#define NAL_TYPE_IS_KEY(nt) (NAL_TYPE_IS_PARAMETER_SET(nt) || NAL_TYPE_IS_IRAP(nt))
+
+static void
 gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
     GstClockTime in_timestamp, gboolean marker)
 {
@@ -947,7 +1068,7 @@ gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
               4, gst_buffer_get_size (nal) - 4));
       gst_buffer_unmap (nal, &map);
       gst_buffer_unref (nal);
-      return NULL;
+      return;
     } else if (rtph265depay->sps->len == 0 || rtph265depay->pps->len == 0) {
       /* Down push down any buffer in non-bytestream mode if the SPS/PPS haven't
        * go through yet
@@ -958,7 +1079,7 @@ gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
                   "all-headers", G_TYPE_BOOLEAN, TRUE, NULL)));
       gst_buffer_unmap (nal, &map);
       gst_buffer_unref (nal);
-      return NULL;
+      return;
     }
 
     if (rtph265depay->new_codec_data &&
@@ -1013,17 +1134,14 @@ gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
     /* prepend codec_data */
     if (rtph265depay->codec_data) {
       GST_DEBUG_OBJECT (depayload, "prepending codec_data");
-      gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay),
-          rtph265depay->codec_data, outbuf,
-          g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
+      gst_rtp_copy_video_meta (rtph265depay, rtph265depay->codec_data, outbuf);
       outbuf = gst_buffer_append (rtph265depay->codec_data, outbuf);
       rtph265depay->codec_data = NULL;
       out_keyframe = TRUE;
     }
     outbuf = gst_buffer_make_writable (outbuf);
 
-    gst_rtp_drop_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
-        g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
+    gst_rtp_drop_non_video_meta (rtph265depay, outbuf);
 
     GST_BUFFER_PTS (outbuf) = out_timestamp;
 
@@ -1031,9 +1149,11 @@ gst_rtp_h265_depay_handle_nal (GstRtpH265Depay * rtph265depay, GstBuffer * nal,
       GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
     else
       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
+
+    gst_rtp_base_depayload_push (depayload, outbuf);
   }
 
-  return outbuf;
+  return;
 
   /* ERRORS */
 short_nal:
@@ -1041,13 +1161,12 @@ short_nal:
     GST_WARNING_OBJECT (depayload, "dropping short NAL");
     gst_buffer_unmap (nal, &map);
     gst_buffer_unref (nal);
-    return NULL;
+    return;
   }
 }
 
-static GstBuffer *
-gst_rtp_h265_push_fragmentation_unit (GstRtpH265Depay * rtph265depay,
-    gboolean send)
+static void
+gst_rtp_h265_finish_fragmentation_unit (GstRtpH265Depay * rtph265depay)
 {
   guint outsize;
   GstMapInfo map;
@@ -1068,21 +1187,17 @@ gst_rtp_h265_push_fragmentation_unit (GstRtpH265Depay * rtph265depay,
 
   rtph265depay->current_fu_type = 0;
 
-  outbuf = gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf,
+  gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf,
       rtph265depay->fu_timestamp, rtph265depay->fu_marker);
 
-  if (send && outbuf) {
-    gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtph265depay), outbuf);
-    outbuf = NULL;
-  }
-  return outbuf;
+  return;
 
 not_implemented:
   {
     GST_ERROR_OBJECT (rtph265depay,
         ("Only bytestream format is currently supported."));
     gst_buffer_unmap (outbuf, &map);
-    return NULL;
+    return;
   }
 }
 
@@ -1158,7 +1273,7 @@ gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
      * when the FU ended) and send out what we gathered thusfar */
     if (G_UNLIKELY (rtph265depay->current_fu_type != 0 &&
             nal_unit_type != rtph265depay->current_fu_type))
-      gst_rtp_h265_push_fragmentation_unit (rtph265depay, TRUE);
+      gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
 
     switch (nal_unit_type) {
       case 48:
@@ -1218,6 +1333,7 @@ gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
           if (rtph265depay->byte_stream) {
             memcpy (map.data, sync_bytes, sizeof (sync_bytes));
           } else {
+            gst_buffer_unmap (outbuf, &map);
             goto not_implemented;
           }
 
@@ -1228,26 +1344,14 @@ gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
           memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
           gst_buffer_unmap (outbuf, &map);
 
-          gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
-              rtp->buffer, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
+          gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
 
-          outbuf =
-              gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
+          gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
               marker);
-          if (outbuf)
-            gst_adapter_push (rtph265depay->adapter, outbuf);
 
           payload += nalu_size;
           payload_len -= nalu_size;
         }
-
-        outsize = gst_adapter_available (rtph265depay->adapter);
-        if (outsize > 0) {
-          outbuf = gst_adapter_take_buffer (rtph265depay->adapter, outsize);
-          outbuf =
-              gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
-              marker);
-        }
         break;
       }
       case 49:
@@ -1301,7 +1405,7 @@ gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
            * Assume that the remote payloader is buggy (doesn't set the end
            * bit) and send out what we've gathered thusfar */
           if (G_UNLIKELY (rtph265depay->current_fu_type != 0))
-            gst_rtp_h265_push_fragmentation_unit (rtph265depay, TRUE);
+            gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
 
           rtph265depay->current_fu_type = nal_unit_type;
           rtph265depay->fu_timestamp = timestamp;
@@ -1329,8 +1433,7 @@ gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
           map.data[sizeof (sync_bytes) + 1] = nal_header & 0xff;
           gst_buffer_unmap (outbuf, &map);
 
-          gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
-              rtp->buffer, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
+          gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
 
           GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
 
@@ -1349,8 +1452,7 @@ gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
           outbuf = gst_buffer_new_and_alloc (outsize);
           gst_buffer_fill (outbuf, 0, payload, outsize);
 
-          gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
-              rtp->buffer, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
+          gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
 
           GST_DEBUG_OBJECT (rtph265depay, "queueing %d bytes", outsize);
 
@@ -1363,7 +1465,7 @@ gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
 
         /* if NAL unit ends, flush the adapter */
         if (E) {
-          outbuf = gst_rtp_h265_push_fragmentation_unit (rtph265depay, FALSE);
+          gst_rtp_h265_finish_fragmentation_unit (rtph265depay);
           GST_DEBUG_OBJECT (rtph265depay, "End of Fragmentation Unit");
         }
         break;
@@ -1390,22 +1492,21 @@ gst_rtp_h265_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
         if (rtph265depay->byte_stream) {
           memcpy (map.data, sync_bytes, sizeof (sync_bytes));
         } else {
+          gst_buffer_unmap (outbuf, &map);
           goto not_implemented;
         }
         memcpy (map.data + sizeof (sync_bytes), payload, nalu_size);
         gst_buffer_unmap (outbuf, &map);
 
-        gst_rtp_copy_meta (GST_ELEMENT_CAST (rtph265depay), outbuf,
-            rtp->buffer, g_quark_from_static_string (GST_META_TAG_VIDEO_STR));
+        gst_rtp_copy_video_meta (rtph265depay, outbuf, rtp->buffer);
 
-        outbuf = gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp,
-            marker);
+        gst_rtp_h265_depay_handle_nal (rtph265depay, outbuf, timestamp, marker);
         break;
       }
     }
   }
 
-  return outbuf;
+  return NULL;
 
   /* ERRORS */
 empty_packet:
@@ -1443,7 +1544,7 @@ gst_rtp_h265_depay_handle_event (GstRTPBaseDepayload * depay, GstEvent * event)
 
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_FLUSH_STOP:
-      gst_rtp_h265_depay_reset (rtph265depay);
+      gst_rtp_h265_depay_reset (rtph265depay, FALSE);
       break;
     default:
       break;
@@ -1466,7 +1567,7 @@ gst_rtp_h265_depay_change_state (GstElement * element,
     case GST_STATE_CHANGE_NULL_TO_READY:
       break;
     case GST_STATE_CHANGE_READY_TO_PAUSED:
-      gst_rtp_h265_depay_reset (rtph265depay);
+      gst_rtp_h265_depay_reset (rtph265depay, TRUE);
       break;
     default:
       break;
@@ -1475,6 +1576,9 @@ gst_rtp_h265_depay_change_state (GstElement * element,
   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
 
   switch (transition) {
+    case GST_STATE_CHANGE_PAUSED_TO_READY:
+      gst_rtp_h265_depay_reset (rtph265depay, TRUE);
+      break;
     case GST_STATE_CHANGE_READY_TO_NULL:
       break;
     default: