GstVideoFrame: Add g_auto() support
authorXavier Claessens <xavier.claessens@collabora.com>
Fri, 13 May 2022 16:57:06 +0000 (12:57 -0400)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Mon, 20 Jun 2022 16:17:50 +0000 (16:17 +0000)
Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2412>

subprojects/gst-plugins-base/gst-libs/gst/video/video-frame.c
subprojects/gst-plugins-base/gst-libs/gst/video/video-frame.h
subprojects/gst-plugins-base/tests/check/libs/video.c

index a86c70a..95adf36 100644 (file)
@@ -266,6 +266,12 @@ gst_video_frame_unmap (GstVideoFrame * frame)
   meta = frame->meta;
   flags = frame->map[0].flags;
 
+  /* Allow to unmap even if not mapped, to work nicely with
+   * g_auto (GstVideoFrame) frame = GST_VIDEO_FRAME_INIT;
+   * This is also more consistent with gst_buffer_unmap() */
+  if (G_UNLIKELY (buffer == NULL))
+    return;
+
   if (meta) {
     for (i = 0; i < frame->info.finfo->n_planes; i++) {
       gst_video_meta_unmap (meta, i, &frame->map[i]);
index f3925c4..6f02630 100644 (file)
@@ -93,6 +93,16 @@ struct _GstVideoFrame {
   gpointer _gst_reserved[GST_PADDING];
 };
 
+/**
+ * GST_VIDEO_FRAME_INIT:
+ *
+ * Initializer for #GstVideoFrame
+ *
+ * Since: 1.22
+ */
+#define GST_VIDEO_FRAME_INIT { { NULL, }, }
+
+
 GST_VIDEO_API
 gboolean    gst_video_frame_map           (GstVideoFrame *frame, const GstVideoInfo *info,
                                            GstBuffer *buffer, GstMapFlags flags);
@@ -249,6 +259,8 @@ typedef enum {
   /* 8 more flags possible afterwards */
 } GstVideoFrameMapFlags;
 
+G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(GstVideoFrame, gst_video_frame_unmap)
+
 G_END_DECLS
 
 #endif /* __GST_VIDEO_FRAME_H__ */
index 42674f4..f17322d 100644 (file)
@@ -4042,6 +4042,34 @@ GST_START_TEST (test_video_extrapolate_stride)
 
 GST_END_TEST;
 
+GST_START_TEST (test_auto_video_frame_unmap)
+{
+#ifdef g_auto
+  g_autoptr (GstBuffer) buf = NULL;
+  GstVideoInfo info;
+
+  fail_unless (gst_video_info_set_format (&info, GST_VIDEO_FORMAT_ENCODED, 10,
+          10));
+  buf = gst_buffer_new_and_alloc (info.size);
+
+  {
+    // unmap should be no-op
+    g_auto (GstVideoFrame) frame = GST_VIDEO_FRAME_INIT;
+    fail_unless (frame.buffer == NULL);
+  }
+
+  {
+    g_auto (GstVideoFrame) frame = GST_VIDEO_FRAME_INIT;
+    gst_video_frame_map (&frame, &info, buf, GST_MAP_READ);
+    fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT (buf), 2);
+  }
+
+  fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT (buf), 1);
+
+#endif
+}
+
+GST_END_TEST;
 
 static Suite *
 video_suite (void)
@@ -4098,6 +4126,7 @@ video_suite (void)
   tcase_add_test (tc_chain, test_video_flags);
   tcase_add_test (tc_chain, test_video_make_raw_caps);
   tcase_add_test (tc_chain, test_video_extrapolate_stride);
+  tcase_add_test (tc_chain, test_auto_video_frame_unmap);
 
   return s;
 }