From 00d6ffd9f56e6ce636f7d61bc1ab2e9ff1227889 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim-Philipp=20M=C3=BCller?= Date: Fri, 25 Nov 2011 16:46:09 +0000 Subject: [PATCH] tests: add basic unit test for video overlay composition and rectangles --- tests/check/libs/video.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/tests/check/libs/video.c b/tests/check/libs/video.c index 47327d4..bd9674a 100644 --- a/tests/check/libs/video.c +++ b/tests/check/libs/video.c @@ -2,7 +2,7 @@ * * Copyright (C) <2003> David A. Schleef * Copyright (C) <2006> Jan Schmidt - * Copyright (C) <2008> Tim-Philipp Müller + * Copyright (C) <2008,2011> Tim-Philipp Müller * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -29,6 +29,7 @@ #include #include +#include #include /* These are from the current/old videotestsrc; we check our new public API @@ -774,6 +775,130 @@ GST_START_TEST (test_video_size_from_caps) GST_END_TEST; +#undef ASSERT_CRITICAL +#define ASSERT_CRITICAL(code) while(0){} /* nothing */ + +GST_START_TEST (test_overlay_composition) +{ + GstVideoOverlayComposition *comp1, *comp2; + GstVideoOverlayRectangle *rect1, *rect2; + GstBuffer *pix1, *pix2, *buf; + guint w, h, stride; + gint x, y; + + pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50); + memset (GST_BUFFER_DATA (pix1), 0, GST_BUFFER_SIZE (pix1)); + + rect1 = gst_video_overlay_rectangle_new_argb (pix1, 200, 50, 200 * 4, + 600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE); + + gst_buffer_unref (pix1); + pix1 = NULL; + + comp1 = gst_video_overlay_composition_new (rect1); + fail_unless (gst_video_overlay_composition_n_rectangles (comp1) == 1); + fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 0) == rect1); + fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 1) == NULL); + + /* composition took own ref, so refcount is 2 now, so this should fail */ + ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 50, + 600, 300, 50)); + + /* drop our ref, so refcount is 1 (we know it will continue to be valid) */ + gst_video_overlay_rectangle_unref (rect1); + gst_video_overlay_rectangle_set_render_rectangle (rect1, 50, 600, 300, 50); + + comp2 = gst_video_overlay_composition_new (rect1); + fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1); + fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect1); + fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL); + + /* now refcount is 2 again because comp2 has also taken a ref, so must fail */ + ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 0, + 0, 1, 1)); + + /* this should make a copy of the rectangles so drop the original + * second ref on rect1 */ + comp2 = gst_video_overlay_composition_make_writable (comp2); + gst_video_overlay_rectangle_set_render_rectangle (rect1, 51, 601, 301, 51); + + rect2 = gst_video_overlay_composition_get_rectangle (comp2, 0); + fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1); + fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect2); + fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL); + fail_unless (rect1 != rect2); + + gst_video_overlay_composition_add_rectangle (comp1, rect2); + gst_video_overlay_composition_ref (comp1); + ASSERT_CRITICAL (gst_video_overlay_composition_add_rectangle (comp1, rect2)); + gst_video_overlay_composition_unref (comp1); + + /* make sure the copy really worked */ + gst_video_overlay_rectangle_get_render_rectangle (rect1, &x, &y, &w, &h); + fail_unless_equals_int (x, 51); + fail_unless_equals_int (y, 601); + fail_unless_equals_int (w, 301); + fail_unless_equals_int (h, 51); + + /* get scaled pixbuf and touch last byte */ + pix1 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride, + GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE); + fail_unless_equals_int (*(GST_BUFFER_DATA (pix1) + (h * stride + w - 1)), 0); + + gst_video_overlay_rectangle_get_render_rectangle (rect2, &x, &y, &w, &h); + fail_unless_equals_int (x, 50); + fail_unless_equals_int (y, 600); + fail_unless_equals_int (w, 300); + fail_unless_equals_int (h, 50); + + /* get scaled pixbuf and touch last byte */ + pix2 = gst_video_overlay_rectangle_get_pixels_argb (rect2, &stride, + GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE); + fail_unless_equals_int (*(GST_BUFFER_DATA (pix1) + (h * stride + w - 1)), 0); + + /* get scaled pixbuf again, should be the same buffer as before (caching) */ + pix1 = gst_video_overlay_rectangle_get_pixels_argb (rect2, &stride, + GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE); + fail_unless (pix1 == pix2); + + /* now compare the original unscaled ones */ + pix1 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h, + &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE); + pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect2, &w, &h, + &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE); + + /* the original pixel buffers should be identical */ + fail_unless (pix1 == pix2); + fail_unless_equals_int (w, 200); + fail_unless_equals_int (h, 50); + + /* touch last byte */ + fail_unless_equals_int (*(GST_BUFFER_DATA (pix1) + (h * stride + w - 1)), 0); + + /* test attaching and retrieving of compositions to/from buffers */ + buf = gst_buffer_new (); + fail_unless (gst_video_buffer_get_overlay_composition (buf) == NULL); + + gst_buffer_ref (buf); + ASSERT_CRITICAL (gst_video_buffer_set_overlay_composition (buf, comp1)); + gst_buffer_unref (buf); + gst_video_buffer_set_overlay_composition (buf, comp1); + fail_unless (gst_video_buffer_get_overlay_composition (buf) == comp1); + gst_video_buffer_set_overlay_composition (buf, comp2); + fail_unless (gst_video_buffer_get_overlay_composition (buf) == comp2); + gst_video_buffer_set_overlay_composition (buf, NULL); + fail_unless (gst_video_buffer_get_overlay_composition (buf) == NULL); + + /* make sure the buffer cleans up its composition ref when unreffed */ + gst_video_buffer_set_overlay_composition (buf, comp2); + gst_buffer_unref (buf); + + gst_video_overlay_composition_unref (comp2); + gst_video_overlay_composition_unref (comp1); +} + +GST_END_TEST; + static Suite * video_suite (void) { @@ -790,6 +915,7 @@ video_suite (void) tcase_add_test (tc_chain, test_convert_frame); tcase_add_test (tc_chain, test_convert_frame_async); tcase_add_test (tc_chain, test_video_size_from_caps); + tcase_add_test (tc_chain, test_overlay_composition); return s; } -- 2.7.4