gst: Set alignment at the correct place of GstAllocationParams
[platform/upstream/gstreamer.git] / gst-libs / gst / video / gstvideoencoder.c
index 933bb9b..5b96569 100644 (file)
 #include "gstvideoutils.h"
 
 #include <gst/video/gstvideometa.h>
+#include <gst/video/gstvideopool.h>
 
 #include <string.h>
 
@@ -141,6 +142,7 @@ struct _GstVideoEncoderPrivate
   /* FIXME : (and introduce a context ?) */
   gboolean drained;
   gboolean at_eos;
+  gboolean do_caps;
 
   gint64 min_latency;
   gint64 max_latency;
@@ -152,7 +154,7 @@ struct _GstVideoEncoderPrivate
 
   GList *force_key_unit;        /* List of pending forced keyunits */
 
-  guint64 system_frame_number;
+  guint32 system_frame_number;
 
   GList *frames;                /* Protected with OBJECT_LOCK */
   GstVideoCodecState *input_state;
@@ -161,6 +163,9 @@ struct _GstVideoEncoderPrivate
 
   gint64 bytes;
   gint64 time;
+
+  GstAllocator *allocator;
+  GstAllocationParams params;
 };
 
 typedef struct _ForcedKeyUnitEvent ForcedKeyUnitEvent;
@@ -215,12 +220,15 @@ static gboolean gst_video_encoder_sink_query (GstPad * pad, GstObject * parent,
 static gboolean gst_video_encoder_src_query (GstPad * pad, GstObject * parent,
     GstQuery * query);
 static GstVideoCodecFrame *gst_video_encoder_new_frame (GstVideoEncoder *
-    encoder, GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
+    encoder, GstBuffer * buf, GstClockTime pts, GstClockTime dts,
+    GstClockTime duration);
 
 static gboolean gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
     GstEvent * event);
 static gboolean gst_video_encoder_src_event_default (GstVideoEncoder * encoder,
     GstEvent * event);
+static gboolean gst_video_encoder_decide_allocation_default (GstVideoEncoder *
+    encoder, GstQuery * query);
 static gboolean gst_video_encoder_propose_allocation_default (GstVideoEncoder *
     encoder, GstQuery * query);
 
@@ -283,6 +291,7 @@ gst_video_encoder_class_init (GstVideoEncoderClass * klass)
   klass->sink_event = gst_video_encoder_sink_event_default;
   klass->src_event = gst_video_encoder_src_event_default;
   klass->propose_allocation = gst_video_encoder_propose_allocation_default;
+  klass->decide_allocation = gst_video_encoder_decide_allocation_default;
 }
 
 static void
@@ -440,8 +449,6 @@ exit:
  * @headers: (transfer full) (element-type GstBuffer): a list of #GstBuffer containing the codec header
  *
  * Set the codec headers to be sent downstream whenever requested.
- *
- * Since: 0.10.36
  */
 void
 gst_video_encoder_set_headers (GstVideoEncoder * video_encoder, GList * headers)
@@ -611,14 +618,13 @@ parse_fail:
  * gst_video_encoder_proxy_getcaps:
  * @enc: a #GstVideoEncoder
  * @caps: initial caps
+ * @filter: filter caps
  *
  * Returns caps that express @caps (or sink template caps if @caps == NULL)
  * restricted to resolution/format/... combinations supported by downstream
  * elements (e.g. muxers).
  *
  * Returns: a #GstCaps owned by caller
- *
- * Since: 0.10.36
  */
 GstCaps *
 gst_video_encoder_proxy_getcaps (GstVideoEncoder * encoder, GstCaps * caps,
@@ -708,10 +714,90 @@ gst_video_encoder_sink_getcaps (GstVideoEncoder * encoder, GstCaps * filter)
 }
 
 static gboolean
+gst_video_encoder_decide_allocation_default (GstVideoEncoder * encoder,
+    GstQuery * query)
+{
+  GstAllocator *allocator = NULL;
+  GstAllocationParams params;
+  gboolean update_allocator;
+
+  /* we got configuration from our peer or the decide_allocation method,
+   * parse them */
+  if (gst_query_get_n_allocation_params (query) > 0) {
+    /* try the allocator */
+    gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
+    update_allocator = TRUE;
+  } else {
+    allocator = NULL;
+    gst_allocation_params_init (&params);
+    update_allocator = FALSE;
+  }
+
+  if (update_allocator)
+    gst_query_set_nth_allocation_param (query, 0, allocator, &params);
+  else
+    gst_query_add_allocation_param (query, allocator, &params);
+  if (allocator)
+    gst_object_unref (allocator);
+
+  return TRUE;
+}
+
+static gboolean
 gst_video_encoder_propose_allocation_default (GstVideoEncoder * encoder,
     GstQuery * query)
 {
+  GstCaps *caps;
+  GstVideoInfo info;
+  GstBufferPool *pool;
+  guint size;
+
+  gst_query_parse_allocation (query, &caps, NULL);
+
+  if (caps == NULL)
+    return FALSE;
+
+  if (!gst_video_info_from_caps (&info, caps))
+    return FALSE;
+
+  size = GST_VIDEO_INFO_SIZE (&info);
+
+  if (gst_query_get_n_allocation_pools (query) == 0) {
+    GstStructure *structure;
+    GstAllocator *allocator = NULL;
+    GstAllocationParams params = { 0, 15, 0, 0 };
+
+    if (gst_query_get_n_allocation_params (query) > 0)
+      gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
+    else
+      gst_query_add_allocation_param (query, allocator, &params);
+
+    pool = gst_video_buffer_pool_new ();
+
+    structure = gst_buffer_pool_get_config (pool);
+    gst_buffer_pool_config_set_params (structure, caps, size, 0, 0);
+    gst_buffer_pool_config_set_allocator (structure, allocator, &params);
+
+    if (allocator)
+      gst_object_unref (allocator);
+
+    if (!gst_buffer_pool_set_config (pool, structure))
+      goto config_failed;
+
+    gst_query_add_allocation_pool (query, pool, size, 0, 0);
+    gst_object_unref (pool);
+    gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
+  }
+
   return TRUE;
+
+  /* ERRORS */
+config_failed:
+  {
+    GST_ERROR_OBJECT (encoder, "failed to set config");
+    gst_object_unref (pool);
+    return FALSE;
+  }
 }
 
 static gboolean
@@ -764,6 +850,11 @@ gst_video_encoder_finalize (GObject * object)
   }
   g_rec_mutex_clear (&encoder->stream_lock);
 
+  if (encoder->priv->allocator) {
+    gst_object_unref (encoder->priv->allocator);
+    encoder->priv->allocator = NULL;
+  }
+
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
@@ -813,7 +904,8 @@ gst_video_encoder_sink_event_default (GstVideoEncoder * encoder,
       GstCaps *caps;
 
       gst_event_parse_caps (event, &caps);
-      ret = gst_video_encoder_setcaps (encoder, caps);
+      ret = TRUE;
+      encoder->priv->do_caps = TRUE;
       gst_event_unref (event);
       event = NULL;
       break;
@@ -1066,7 +1158,7 @@ error:
 
 static GstVideoCodecFrame *
 gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
-    GstClockTime timestamp, GstClockTime duration)
+    GstClockTime pts, GstClockTime dts, GstClockTime duration)
 {
   GstVideoEncoderPrivate *priv = encoder->priv;
   GstVideoCodecFrame *frame;
@@ -1086,7 +1178,8 @@ gst_video_encoder_new_frame (GstVideoEncoder * encoder, GstBuffer * buf,
   frame->events = priv->current_frame_events;
   priv->current_frame_events = NULL;
   frame->input_buffer = buf;
-  frame->pts = timestamp;
+  frame->pts = pts;
+  frame->dts = dts;
   frame->duration = duration;
 
   return frame;
@@ -1100,8 +1193,9 @@ gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
   GstVideoEncoderPrivate *priv;
   GstVideoEncoderClass *klass;
   GstVideoCodecFrame *frame;
+  GstClockTime pts, dts, duration;
   GstFlowReturn ret = GST_FLOW_OK;
-  guint64 start, stop = GST_CLOCK_TIME_NONE, cstart, cstop;
+  guint64 start, stop, cstart, cstop;
 
   encoder = GST_VIDEO_ENCODER (parent);
   priv = encoder->priv;
@@ -1109,22 +1203,40 @@ gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
 
   g_return_val_if_fail (klass->handle_frame != NULL, GST_FLOW_ERROR);
 
+  if (G_UNLIKELY (encoder->priv->do_caps)) {
+    GstCaps *caps = gst_pad_get_current_caps (encoder->sinkpad);
+    if (!caps)
+      goto not_negotiated;
+    if (!gst_video_encoder_setcaps (encoder, caps)) {
+      gst_caps_unref (caps);
+      goto not_negotiated;
+    }
+    encoder->priv->do_caps = FALSE;
+  }
+
   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
 
-  start = GST_BUFFER_TIMESTAMP (buf);
-  if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DURATION (buf)))
-    stop = start + GST_BUFFER_DURATION (buf);
+  pts = GST_BUFFER_PTS (buf);
+  dts = GST_BUFFER_DTS (buf);
+  duration = GST_BUFFER_DURATION (buf);
 
   GST_LOG_OBJECT (encoder,
-      "received buffer of size %d with ts %" GST_TIME_FORMAT
-      ", duration %" GST_TIME_FORMAT, gst_buffer_get_size (buf),
-      GST_TIME_ARGS (start), GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
+      "received buffer of size %" G_GSIZE_FORMAT " with PTS %" GST_TIME_FORMAT
+      ", PTS %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
+      gst_buffer_get_size (buf), GST_TIME_ARGS (pts), GST_TIME_ARGS (dts),
+      GST_TIME_ARGS (duration));
 
   if (priv->at_eos) {
     ret = GST_FLOW_EOS;
     goto done;
   }
 
+  start = pts;
+  if (GST_CLOCK_TIME_IS_VALID (duration))
+    stop = start + duration;
+  else
+    stop = GST_CLOCK_TIME_NONE;
+
   /* Drop buffers outside of segment */
   if (!gst_segment_clip (&encoder->output_segment,
           GST_FORMAT_TIME, start, stop, &cstart, &cstop)) {
@@ -1133,7 +1245,8 @@ gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
     goto done;
   }
 
-  frame = gst_video_encoder_new_frame (encoder, buf, cstart, cstop - cstart);
+  frame =
+      gst_video_encoder_new_frame (encoder, buf, cstart, dts, cstop - cstart);
 
   GST_OBJECT_LOCK (encoder);
   if (priv->force_key_unit) {
@@ -1143,7 +1256,7 @@ gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
 
     running_time =
         gst_segment_to_running_time (&encoder->output_segment, GST_FORMAT_TIME,
-        GST_BUFFER_TIMESTAMP (buf));
+        cstart);
 
     for (l = priv->force_key_unit; l; l = l->next) {
       ForcedKeyUnitEvent *tmp = l->data;
@@ -1177,6 +1290,7 @@ gst_video_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
   }
   GST_OBJECT_UNLOCK (encoder);
 
+  gst_video_codec_frame_ref (frame);
   priv->frames = g_list_append (priv->frames, frame);
 
   /* new data, more finish needed */
@@ -1191,6 +1305,15 @@ done:
   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
 
   return ret;
+
+  /* ERRORS */
+not_negotiated:
+  {
+    GST_ELEMENT_ERROR (encoder, CORE, NEGOTIATION, (NULL),
+        ("encoder not initialized"));
+    gst_buffer_unref (buf);
+    return GST_FLOW_NOT_NEGOTIATED;
+  }
 }
 
 static GstStateChangeReturn
@@ -1268,12 +1391,24 @@ close_failed:
   }
 }
 
-static gboolean
-gst_video_encoder_set_src_caps (GstVideoEncoder * encoder)
+/**
+ * gst_video_encoder_negotiate:
+ * @encoder: a #GstVideoEncoder
+ *
+ * Negotiate with downstream elements to currently configured #GstVideoCodecState.
+ *
+ * Returns: #TRUE if the negotiation succeeded, else #FALSE.
+ */
+gboolean
+gst_video_encoder_negotiate (GstVideoEncoder * encoder)
 {
+  GstVideoEncoderClass *klass = GST_VIDEO_ENCODER_GET_CLASS (encoder);
+  GstAllocator *allocator;
+  GstAllocationParams params;
   gboolean ret;
   GstVideoCodecState *state = encoder->priv->output_state;
   GstVideoInfo *info = &state->info;
+  GstQuery *query = NULL;
 
   g_return_val_if_fail (state->caps != NULL, FALSE);
 
@@ -1301,8 +1436,122 @@ gst_video_encoder_set_src_caps (GstVideoEncoder * encoder)
   }
 
   ret = gst_pad_set_caps (encoder->srcpad, state->caps);
+  if (!ret)
+    goto done;
+
+  query = gst_query_new_allocation (state->caps, TRUE);
+  if (!gst_pad_peer_query (encoder->srcpad, query)) {
+    GST_DEBUG_OBJECT (encoder, "didn't get downstream ALLOCATION hints");
+  }
+
+  g_assert (klass->decide_allocation != NULL);
+  ret = klass->decide_allocation (encoder, query);
+
+  GST_DEBUG_OBJECT (encoder, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, ret,
+      query);
+
+  if (!ret)
+    goto no_decide_allocation;
+
+  /* we got configuration from our peer or the decide_allocation method,
+   * parse them */
+  if (gst_query_get_n_allocation_params (query) > 0) {
+    gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
+  } else {
+    allocator = NULL;
+    gst_allocation_params_init (&params);
+  }
+
+  if (encoder->priv->allocator)
+    gst_object_unref (encoder->priv->allocator);
+  encoder->priv->allocator = allocator;
+  encoder->priv->params = params;
+
+done:
+  if (query)
+    gst_query_unref (query);
 
   return ret;
+
+  /* Errors */
+no_decide_allocation:
+  {
+    GST_WARNING_OBJECT (encoder, "Subclass failed to decide allocation");
+    goto done;
+  }
+}
+
+/**
+ * gst_video_encoder_allocate_output_buffer:
+ * @encoder: a #GstVideoEncoder
+ * @size: size of the buffer
+ *
+ * Helper function that allocates a buffer to hold an encoded video frame
+ * for @encoder's current #GstVideoCodecState.
+ *
+ * Returns: (transfer full): allocated buffer
+ */
+GstBuffer *
+gst_video_encoder_allocate_output_buffer (GstVideoEncoder * encoder, gsize size)
+{
+  GstBuffer *buffer;
+
+  g_return_val_if_fail (size > 0, NULL);
+
+  GST_DEBUG ("alloc src buffer");
+
+  GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
+  if (G_UNLIKELY (encoder->priv->output_state_changed
+          || (encoder->priv->output_state
+              && gst_pad_check_reconfigure (encoder->srcpad))))
+    gst_video_encoder_negotiate (encoder);
+
+  buffer =
+      gst_buffer_new_allocate (encoder->priv->allocator, size,
+      &encoder->priv->params);
+
+  GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
+
+  return buffer;
+}
+
+/**
+ * gst_video_encoder_allocate_output_frame:
+ * @encoder: a #GstVideoEncoder
+ * @frame: a #GstVideoCodecFrame
+ * @size: size of the buffer
+ *
+ * Helper function that allocates a buffer to hold an encoded video frame for @encoder's
+ * current #GstVideoCodecState.  Subclass should already have configured video
+ * state and set src pad caps.
+ *
+ * The buffer allocated here is owned by the frame and you should only
+ * keep references to the frame, not the buffer.
+ *
+ * Returns: %GST_FLOW_OK if an output buffer could be allocated
+ */
+GstFlowReturn
+gst_video_encoder_allocate_output_frame (GstVideoEncoder *
+    encoder, GstVideoCodecFrame * frame, gsize size)
+{
+  g_return_val_if_fail (frame->output_buffer == NULL, GST_FLOW_ERROR);
+  g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
+
+  GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
+  if (G_UNLIKELY (encoder->priv->output_state_changed
+          || (encoder->priv->output_state
+              && gst_pad_check_reconfigure (encoder->srcpad))))
+    gst_video_encoder_negotiate (encoder);
+
+  GST_LOG_OBJECT (encoder, "alloc buffer size %" G_GSIZE_FORMAT, size);
+
+  frame->output_buffer =
+      gst_buffer_new_allocate (encoder->priv->allocator, size,
+      &encoder->priv->params);
+
+  GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
+
+  return frame->output_buffer ? GST_FLOW_OK : GST_FLOW_ERROR;
 }
 
 /**
@@ -1316,9 +1565,11 @@ gst_video_encoder_set_src_caps (GstVideoEncoder * encoder)
  * It is subsequently pushed downstream or provided to @pre_push.
  * In any case, the frame is considered finished and released.
  *
- * Returns: a #GstFlowReturn resulting from sending data downstream
+ * After calling this function the output buffer of the frame is to be
+ * considered read-only. This function will also change the metadata
+ * of the buffer.
  *
- * Since: 0.10.36
+ * Returns: a #GstFlowReturn resulting from sending data downstream
  */
 GstFlowReturn
 gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
@@ -1338,8 +1589,10 @@ gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
 
   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
 
-  if (G_UNLIKELY (priv->output_state_changed))
-    gst_video_encoder_set_src_caps (encoder);
+  if (G_UNLIKELY (priv->output_state_changed || (priv->output_state
+              && gst_pad_check_reconfigure (encoder->srcpad))))
+    gst_video_encoder_negotiate (encoder);
+
 
   if (G_UNLIKELY (priv->output_state == NULL))
     goto no_output_state;
@@ -1438,7 +1691,8 @@ gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
   frame->distance_from_sync = priv->distance_from_sync;
   priv->distance_from_sync++;
 
-  GST_BUFFER_TIMESTAMP (frame->output_buffer) = frame->pts;
+  GST_BUFFER_PTS (frame->output_buffer) = frame->pts;
+  GST_BUFFER_DTS (frame->output_buffer) = frame->dts;
   GST_BUFFER_DURATION (frame->output_buffer) = frame->duration;
 
   /* update rate estimate */
@@ -1488,15 +1742,24 @@ gst_video_encoder_finish_frame (GstVideoEncoder * encoder,
   if (encoder_class->pre_push)
     ret = encoder_class->pre_push (encoder, frame);
 
+  /* A reference always needs to be owned by the frame on the buffer.
+   * For that reason, we use a complete sub-buffer (zero-cost) to push
+   * downstream.
+   * The original buffer will be free-ed only when downstream AND the
+   * current implementation are done with the frame. */
   if (ret == GST_FLOW_OK)
-    ret = gst_pad_push (encoder->srcpad, frame->output_buffer);
-
-  frame->output_buffer = NULL;
+    ret = gst_pad_push (encoder->srcpad, gst_buffer_ref (frame->output_buffer));
 
 done:
   /* handed out */
-  priv->frames = g_list_remove (priv->frames, frame);
 
+  /* unref once from the list */
+  l = g_list_find (priv->frames, frame);
+  if (l) {
+    gst_video_codec_frame_unref (frame);
+    priv->frames = g_list_delete_link (priv->frames, l);
+  }
+  /* unref because this function takes ownership */
   gst_video_codec_frame_unref (frame);
 
   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
@@ -1519,8 +1782,6 @@ no_output_state:
  * Get the current #GstVideoCodecState
  *
  * Returns: (transfer full): #GstVideoCodecState describing format of video data.
- *
- * Since: 0.10.36
  */
 GstVideoCodecState *
 gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
@@ -1560,8 +1821,6 @@ gst_video_encoder_get_output_state (GstVideoEncoder * encoder)
  * from the next call to #gst_video_encoder_finish_frame().
  *
  * Returns: (transfer full): the newly configured output state.
- *
- * Since: 0.10.36
  */
 GstVideoCodecState *
 gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
@@ -1592,8 +1851,6 @@ gst_video_encoder_set_output_state (GstVideoEncoder * encoder, GstCaps * caps,
  * @max_latency: maximum latency
  *
  * Informs baseclass of encoding latency.
- *
- * Since: 0.10.36
  */
 void
 gst_video_encoder_set_latency (GstVideoEncoder * encoder,
@@ -1614,12 +1871,13 @@ gst_video_encoder_set_latency (GstVideoEncoder * encoder,
 /**
  * gst_video_encoder_get_latency:
  * @encoder: a #GstVideoEncoder
- * @min_latency: (out) (allow-none): the configured minimum latency
- * @max_latency: (out) (allow-none): the configured maximum latency
- *
- * Returns the configured encoding latency.
+ * @min_latency: (out) (allow-none): address of variable in which to store the
+ *     configured minimum latency, or %NULL
+ * @max_latency: (out) (allow-none): address of variable in which to store the
+ *     configured maximum latency, or %NULL
  *
- * Since: 0.10.36
+ * Query the configured encoding latency. Results will be returned via
+ * @min_latency and @max_latency.
  */
 void
 gst_video_encoder_get_latency (GstVideoEncoder * encoder,
@@ -1639,22 +1897,19 @@ gst_video_encoder_get_latency (GstVideoEncoder * encoder,
  *
  * Get the oldest unfinished pending #GstVideoCodecFrame
  *
- * Returns: oldest unfinished pending #GstVideoCodecFrame
- *
- * Since: 0.10.36
+ * Returns: (transfer full): oldest unfinished pending #GstVideoCodecFrame
  */
 GstVideoCodecFrame *
 gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
 {
-  GList *g;
+  GstVideoCodecFrame *frame = NULL;
 
   GST_VIDEO_ENCODER_STREAM_LOCK (encoder);
-  g = encoder->priv->frames;
+  if (encoder->priv->frames)
+    frame = gst_video_codec_frame_ref (encoder->priv->frames->data);
   GST_VIDEO_ENCODER_STREAM_UNLOCK (encoder);
 
-  if (g == NULL)
-    return NULL;
-  return (GstVideoCodecFrame *) (g->data);
+  return (GstVideoCodecFrame *) frame;
 }
 
 /**
@@ -1664,9 +1919,7 @@ gst_video_encoder_get_oldest_frame (GstVideoEncoder * encoder)
  *
  * Get a pending unfinished #GstVideoCodecFrame
  * 
- * Returns: (transfer none): pending unfinished #GstVideoCodecFrame identified by @frame_number.
- *
- * Since: 0.10.36
+ * Returns: (transfer full): pending unfinished #GstVideoCodecFrame identified by @frame_number.
  */
 GstVideoCodecFrame *
 gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
@@ -1681,7 +1934,7 @@ gst_video_encoder_get_frame (GstVideoEncoder * encoder, int frame_number)
     GstVideoCodecFrame *tmp = g->data;
 
     if (tmp->system_frame_number == frame_number) {
-      frame = tmp;
+      frame = gst_video_codec_frame_ref (tmp);
       break;
     }
   }