From ff57f6913456ec1991e55517cf1f239e80eeddef Mon Sep 17 00:00:00 2001 From: Edward Hervey Date: Mon, 15 Dec 2014 14:10:17 +0100 Subject: [PATCH] video: Fix non-default usage of gst_video_sink_center_rect Make sure we take into account non-0 x/y destination rectangles --- gst-libs/gst/video/gstvideosink.c | 16 ++++----- tests/check/libs/video.c | 60 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/gst-libs/gst/video/gstvideosink.c b/gst-libs/gst/video/gstvideosink.c index 47045933ad..26b7df21c3 100644 --- a/gst-libs/gst/video/gstvideosink.c +++ b/gst-libs/gst/video/gstvideosink.c @@ -87,8 +87,8 @@ gst_video_sink_center_rect (GstVideoRectangle src, GstVideoRectangle dst, if (!scaling) { result->w = MIN (src.w, dst.w); result->h = MIN (src.h, dst.h); - result->x = (dst.w - result->w) / 2; - result->y = (dst.h - result->h) / 2; + result->x = dst.x + (dst.w - result->w) / 2; + result->y = dst.y + (dst.h - result->h) / 2; } else { gdouble src_ratio, dst_ratio; @@ -98,16 +98,16 @@ gst_video_sink_center_rect (GstVideoRectangle src, GstVideoRectangle dst, if (src_ratio > dst_ratio) { result->w = dst.w; result->h = dst.w / src_ratio; - result->x = 0; - result->y = (dst.h - result->h) / 2; + result->x = dst.x; + result->y = dst.y + (dst.h - result->h) / 2; } else if (src_ratio < dst_ratio) { result->w = dst.h * src_ratio; result->h = dst.h; - result->x = (dst.w - result->w) / 2; - result->y = 0; + result->x = dst.x + (dst.w - result->w) / 2; + result->y = dst.y; } else { - result->x = 0; - result->y = 0; + result->x = dst.x; + result->y = dst.y; result->w = dst.w; result->h = dst.h; } diff --git a/tests/check/libs/video.c b/tests/check/libs/video.c index 8c699dc88c..4ffa1c637c 100644 --- a/tests/check/libs/video.c +++ b/tests/check/libs/video.c @@ -2277,6 +2277,65 @@ GST_START_TEST (test_video_transfer) GST_END_TEST; +GST_START_TEST (test_video_center_rect) +{ + GstVideoRectangle src, dest, result, expected; + +#define NEW_RECT(x,y,w,h) ((GstVideoRectangle) {x,y,w,h}) +#define CHECK_RECT(res, exp) \ + fail_unless_equals_int(exp.x, res.x);\ + fail_unless_equals_int(exp.y, res.y);\ + fail_unless_equals_int(exp.w, res.w);\ + fail_unless_equals_int(exp.h, res.h); + + /* 1:1 Aspect Ratio */ + src = NEW_RECT (0, 0, 100, 100); + dest = NEW_RECT (0, 0, 100, 100); + expected = NEW_RECT (0, 0, 100, 100); + gst_video_sink_center_rect (src, dest, &result, TRUE); + CHECK_RECT (result, expected); + + src = NEW_RECT (0, 0, 100, 100); + dest = NEW_RECT (0, 0, 50, 50); + expected = NEW_RECT (0, 0, 50, 50); + gst_video_sink_center_rect (src, dest, &result, TRUE); + CHECK_RECT (result, expected); + + src = NEW_RECT (0, 0, 100, 100); + dest = NEW_RECT (50, 50, 100, 100); + expected = NEW_RECT (50, 50, 100, 100); + gst_video_sink_center_rect (src, dest, &result, TRUE); + CHECK_RECT (result, expected); + + /* Aspect ratio scaling (tall) */ + src = NEW_RECT (0, 0, 50, 100); + dest = NEW_RECT (0, 0, 50, 50); + expected = NEW_RECT (12, 0, 25, 50); + gst_video_sink_center_rect (src, dest, &result, TRUE); + CHECK_RECT (result, expected); + + src = NEW_RECT (0, 0, 50, 100); + dest = NEW_RECT (50, 50, 50, 50); + expected = NEW_RECT (62, 50, 25, 50); + gst_video_sink_center_rect (src, dest, &result, TRUE); + CHECK_RECT (result, expected); + + /* Aspect ratio scaling (wide) */ + src = NEW_RECT (0, 0, 100, 50); + dest = NEW_RECT (0, 0, 50, 50); + expected = NEW_RECT (0, 12, 50, 25); + gst_video_sink_center_rect (src, dest, &result, TRUE); + CHECK_RECT (result, expected); + + src = NEW_RECT (0, 0, 100, 50); + dest = NEW_RECT (50, 50, 50, 50); + expected = NEW_RECT (50, 62, 50, 25); + gst_video_sink_center_rect (src, dest, &result, TRUE); + CHECK_RECT (result, expected); +} + +GST_END_TEST; + void test_overlay_blend_rect (gint x, gint y, gint width, gint height, GstVideoFrame * video_frame); void test_overlay_blend_rect_verify (gint x, gint y, gint width, @@ -2470,6 +2529,7 @@ video_suite (void) tcase_add_test (tc_chain, test_video_size_convert); tcase_add_test (tc_chain, test_video_transfer); tcase_add_test (tc_chain, test_overlay_blend); + tcase_add_test (tc_chain, test_video_center_rect); return s; }