From 27093c19a4578456cebe2523092c468d23614431 Mon Sep 17 00:00:00 2001 From: Thijs Vermeir Date: Sat, 24 Jan 2009 14:48:00 +0100 Subject: [PATCH] Add unit test for aspectratiocrop Fixes bug #527951 Add unit test for aspectratiocrop and refactor this element. Added finalize function to cleanup leaking mutex. --- gst/videocrop/gstaspectratiocrop.c | 265 ++++++++++++++++++++++----------- tests/check/Makefile.am | 13 +- tests/check/elements/aspectratiocrop.c | 201 +++++++++++++++++++++++++ 3 files changed, 388 insertions(+), 91 deletions(-) create mode 100644 tests/check/elements/aspectratiocrop.c diff --git a/gst/videocrop/gstaspectratiocrop.c b/gst/videocrop/gstaspectratiocrop.c index 2cdd51c..28ca397 100644 --- a/gst/videocrop/gstaspectratiocrop.c +++ b/gst/videocrop/gstaspectratiocrop.c @@ -99,7 +99,12 @@ static void gst_aspect_ratio_crop_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); static void gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop, gint top, gint right, gint bottom, gint left); +static GstCaps *gst_aspect_ratio_crop_get_caps (GstPad * pad); static gboolean gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps); +static void gst_aspect_ratio_crop_finalize (GObject * object); +static void gst_aspect_ratio_transform_structure (GstAspectRatioCrop * + aspect_ratio_crop, GstStructure * structure, GstStructure ** new_structure, + gboolean set_videocrop); static void gst_aspect_ratio_crop_set_cropping (GstAspectRatioCrop * aspect_ratio_crop, @@ -140,28 +145,126 @@ gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps) GstPad *peer_pad; GstStructure *structure; gboolean ret; + + aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad)); + + g_mutex_lock (aspect_ratio_crop->crop_lock); + + structure = gst_caps_get_structure (caps, 0); + gst_aspect_ratio_transform_structure (aspect_ratio_crop, structure, NULL, + TRUE); + peer_pad = + gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop), + "sink"); + ret = gst_pad_set_caps (peer_pad, caps); + gst_object_unref (peer_pad); + gst_object_unref (aspect_ratio_crop); + g_mutex_unlock (aspect_ratio_crop->crop_lock); + return ret; +} + +static void +gst_aspect_ratio_crop_base_init (gpointer g_class) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + + gst_element_class_set_details (element_class, &aspect_ratio_crop_details); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); +} + +static void +gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass) +{ + GObjectClass *gobject_class; + + gobject_class = (GObjectClass *) klass; + + gobject_class->set_property = gst_aspect_ratio_crop_set_property; + gobject_class->get_property = gst_aspect_ratio_crop_get_property; + gobject_class->finalize = gst_aspect_ratio_crop_finalize; + + g_object_class_install_property (gobject_class, ARG_ASPECT_RATIO_CROP, + gst_param_spec_fraction ("aspect-ratio", "aspect-ratio", + "Target aspect-ratio of video", 0, 1, G_MAXINT, 1, 0, 1, + G_PARAM_READWRITE)); +} + +static void +gst_aspect_ratio_crop_finalize (GObject * object) +{ + GstAspectRatioCrop *aspect_ratio_crop; + + aspect_ratio_crop = GST_ASPECT_RATIO_CROP (object); + + if (aspect_ratio_crop->crop_lock) + g_mutex_free (aspect_ratio_crop->crop_lock); +} + +static void +gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop, + GstAspectRatioCropClass * klass) +{ + GstPad *link_pad; + GstPad *src_pad; + + GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0, + "aspectratiocrop"); + + aspect_ratio_crop->ar_num = 0; + aspect_ratio_crop->ar_denom = 1; + + aspect_ratio_crop->crop_lock = g_mutex_new (); + + /* add the transform element */ + aspect_ratio_crop->videocrop = gst_element_factory_make ("videocrop", NULL); + gst_bin_add (GST_BIN (aspect_ratio_crop), aspect_ratio_crop->videocrop); + + /* create ghost pad src */ + link_pad = + gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop), + "src"); + src_pad = gst_ghost_pad_new ("src", link_pad); + gst_pad_set_getcaps_function (src_pad, + GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_get_caps)); + gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), src_pad); + gst_object_unref (link_pad); + /* create ghost pad sink */ + link_pad = + gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop), + "sink"); + aspect_ratio_crop->sink = gst_ghost_pad_new ("sink", link_pad); + gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), + aspect_ratio_crop->sink); + gst_object_unref (link_pad); + gst_pad_set_setcaps_function (aspect_ratio_crop->sink, + GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_set_caps)); +} + +static void +gst_aspect_ratio_transform_structure (GstAspectRatioCrop * aspect_ratio_crop, + GstStructure * structure, GstStructure ** new_structure, + gboolean set_videocrop) +{ gdouble incoming_ar; gdouble requested_ar; gint width, height; gint cropvalue; gint par_d, par_n; - aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad)); - g_mutex_lock (aspect_ratio_crop->crop_lock); - /* Check if we need to change the aspect ratio */ if (aspect_ratio_crop->ar_num < 1) { GST_DEBUG_OBJECT (aspect_ratio_crop, "No cropping requested"); - gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0); - goto setcaps_on_peer; + goto beach; } /* get the information from the caps */ - structure = gst_caps_get_structure (caps, 0); - if (!gst_structure_get_int (structure, "width", &width)) - goto no_width; - if (!gst_structure_get_int (structure, "height", &height)) - goto no_height; + if (!gst_structure_get_int (structure, "width", &width) || + !gst_structure_get_int (structure, "height", &height)) + goto beach; if (!gst_structure_get_fraction (structure, "pixel-aspect-ratio", &par_n, &par_d)) { @@ -181,7 +284,7 @@ gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps) GST_DEBUG_OBJECT (aspect_ratio_crop, "Input video already has the correct aspect ratio (%.3f == %.3f)", incoming_ar, requested_ar); - gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0); + goto beach; } else if (requested_ar > incoming_ar) { /* fix aspect ratio with cropping on top and bottom */ cropvalue = @@ -193,8 +296,15 @@ gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps) } if (cropvalue >= (height / 2)) goto crop_failed; - gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0, - cropvalue, 0); + if (set_videocrop) { + gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0, + cropvalue, 0); + } + if (new_structure) { + *new_structure = gst_structure_copy (structure); + gst_structure_set (*new_structure, + "height", G_TYPE_INT, (int) (height - (cropvalue * 2)), NULL); + } } else { /* fix aspect ratio with cropping on left and right */ cropvalue = @@ -206,102 +316,87 @@ gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps) } if (cropvalue >= (width / 2)) goto crop_failed; - gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue, 0, - cropvalue); + if (set_videocrop) { + gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue, + 0, cropvalue); + } + if (new_structure) { + *new_structure = gst_structure_copy (structure); + gst_structure_set (*new_structure, + "width", G_TYPE_INT, (int) (width - (cropvalue * 2)), NULL); + } } -setcaps_on_peer: - GST_DEBUG_OBJECT (aspect_ratio_crop, - "setting the caps on the videocrop element"); - peer_pad = - gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop), - "sink"); - ret = gst_pad_set_caps (peer_pad, caps); - gst_object_unref (peer_pad); - gst_object_unref (aspect_ratio_crop); - g_mutex_unlock (aspect_ratio_crop->crop_lock); - return ret; + return; -no_width: - GST_INFO_OBJECT (aspect_ratio_crop, "no width found in the caps"); - goto beach; -no_height: - GST_INFO_OBJECT (aspect_ratio_crop, "no height found in the caps"); - goto beach; crop_failed: GST_WARNING_OBJECT (aspect_ratio_crop, "can't crop to aspect ratio requested"); goto beach; beach: - gst_object_unref (aspect_ratio_crop); - g_mutex_unlock (aspect_ratio_crop->crop_lock); - return FALSE; + if (set_videocrop) { + gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, 0, 0, 0); + } + + if (new_structure) { + *new_structure = gst_structure_copy (structure); + } } -static void -gst_aspect_ratio_crop_base_init (gpointer g_class) +static GstCaps * +gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop, + GstCaps * caps) { - GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + GstCaps *transform; + gint size, i; - gst_element_class_set_details (element_class, &aspect_ratio_crop_details); + transform = gst_caps_new_empty (); - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&sink_template)); - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&src_template)); -} + size = gst_caps_get_size (caps); -static void -gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass) -{ - GObjectClass *gobject_class; + for (i = 0; i < size; i++) { + GstStructure *s; + GstStructure *trans_s; - gobject_class = (GObjectClass *) klass; + s = gst_caps_get_structure (caps, i); - gobject_class->set_property = gst_aspect_ratio_crop_set_property; - gobject_class->get_property = gst_aspect_ratio_crop_get_property; + gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s, + FALSE); + gst_caps_append_structure (transform, trans_s); + } - g_object_class_install_property (gobject_class, ARG_ASPECT_RATIO_CROP, - gst_param_spec_fraction ("aspect-ratio", "aspect-ratio", - "Target aspect-ratio of video", 0, 1, G_MAXINT, 1, 0, 1, - G_PARAM_READWRITE)); + return transform; } -static void -gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop, - GstAspectRatioCropClass * klass) +static GstCaps * +gst_aspect_ratio_crop_get_caps (GstPad * pad) { - GstPad *link_pad; + GstPad *peer; + GstAspectRatioCrop *aspect_ratio_crop; + GstCaps *return_caps; - GST_DEBUG_CATEGORY_INIT (aspect_ratio_crop_debug, "aspectratiocrop", 0, - "aspectratiocrop"); + aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad)); - aspect_ratio_crop->ar_num = 0; - aspect_ratio_crop->ar_denom = 1; + g_mutex_lock (aspect_ratio_crop->crop_lock); - aspect_ratio_crop->crop_lock = g_mutex_new (); + peer = gst_pad_get_peer (aspect_ratio_crop->sink); + if (peer == NULL) { + return_caps = gst_static_pad_template_get_caps (&src_template); + gst_caps_ref (return_caps); + } else { + GstCaps *peer_caps; - /* add the transform element */ - aspect_ratio_crop->videocrop = gst_element_factory_make ("videocrop", NULL); - gst_bin_add (GST_BIN (aspect_ratio_crop), aspect_ratio_crop->videocrop); + peer_caps = gst_pad_get_caps (peer); + return_caps = + gst_aspect_ratio_crop_transform_caps (aspect_ratio_crop, peer_caps); + gst_caps_unref (peer_caps); + } - /* create ghost pad src */ - link_pad = - gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop), - "src"); - gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), gst_ghost_pad_new (NULL, - link_pad)); - gst_object_unref (link_pad); - /* create ghost pad sink */ - link_pad = - gst_element_get_static_pad (GST_ELEMENT (aspect_ratio_crop->videocrop), - "sink"); - aspect_ratio_crop->sink = gst_ghost_pad_new (NULL, link_pad); - gst_element_add_pad (GST_ELEMENT (aspect_ratio_crop), - aspect_ratio_crop->sink); - gst_object_unref (link_pad); - gst_pad_set_setcaps_function (aspect_ratio_crop->sink, - GST_DEBUG_FUNCPTR (gst_aspect_ratio_crop_set_caps)); + g_mutex_unlock (aspect_ratio_crop->crop_lock); + gst_object_unref (peer); + gst_object_unref (aspect_ratio_crop); + + return return_caps; } static void @@ -320,7 +415,7 @@ gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id, aspect_ratio_crop->ar_num = gst_value_get_fraction_numerator (value); aspect_ratio_crop->ar_denom = gst_value_get_fraction_denominator (value); - recheck = TRUE; + recheck = (GST_PAD_CAPS (aspect_ratio_crop->sink) != NULL); } break; default: diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 89a151c..2fd65b3 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -68,17 +68,18 @@ check_PROGRAMS = \ generic/states \ $(check_annodex) \ elements/alphacolor \ - elements/audiopanorama \ - elements/audioinvert \ + elements/aspectratiocrop \ + elements/audioamplify \ elements/audiochebband \ elements/audiocheblimit \ - elements/audioiirfilter \ - elements/audioamplify \ - elements/audioecho \ elements/audiodynamic \ + elements/audioecho \ + elements/audiofirfilter \ + elements/audioiirfilter \ + elements/audioinvert \ + elements/audiopanorama \ elements/audiowsincband \ elements/audiowsinclimit \ - elements/audiofirfilter \ elements/avimux \ elements/avisubtitle \ elements/deinterleave \ diff --git a/tests/check/elements/aspectratiocrop.c b/tests/check/elements/aspectratiocrop.c new file mode 100644 index 0000000..4f80552 --- /dev/null +++ b/tests/check/elements/aspectratiocrop.c @@ -0,0 +1,201 @@ +/* GStreamer unit test for the aspectratiocrop element + * Copyright (C) 2009 Thijs Vermeir + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include + +#define ASPECT_RATIO_CROP_CAPS \ + GST_VIDEO_CAPS_RGBx ";" \ + GST_VIDEO_CAPS_xRGB ";" \ + GST_VIDEO_CAPS_BGRx ";" \ + GST_VIDEO_CAPS_xBGR ";" \ + GST_VIDEO_CAPS_RGBA ";" \ + GST_VIDEO_CAPS_ARGB ";" \ + GST_VIDEO_CAPS_BGRA ";" \ + GST_VIDEO_CAPS_ABGR ";" \ + GST_VIDEO_CAPS_RGB ";" \ + GST_VIDEO_CAPS_BGR ";" \ + GST_VIDEO_CAPS_YUV ("AYUV") ";" \ + GST_VIDEO_CAPS_YUV ("YUY2") ";" \ + GST_VIDEO_CAPS_YUV ("YVYU") ";" \ + GST_VIDEO_CAPS_YUV ("UYVY") ";" \ + GST_VIDEO_CAPS_YUV ("Y800") ";" \ + GST_VIDEO_CAPS_YUV ("I420") ";" \ + GST_VIDEO_CAPS_YUV ("YV12") ";" \ + GST_VIDEO_CAPS_RGB_16 ";" \ + GST_VIDEO_CAPS_RGB_15 + +GstBuffer * +make_buffer_with_caps (const gchar * caps_string, int buffer_size) +{ + GstCaps *caps; + GstBuffer *temp; + + caps = gst_caps_from_string (caps_string); + temp = gst_buffer_new_and_alloc (buffer_size); + fail_if (caps == NULL); + fail_if (temp == NULL); + gst_buffer_set_caps (temp, caps); + gst_caps_unref (caps); + + return temp; +} + +void +check_aspectratiocrop (const gchar * in_string, const gchar * out_string, + gint in_size, gint out_size, gint ar_n, gint ar_d) +{ + GstElement *element; + GstPad *pad_peer; + GstPad *sink_pad = NULL; + GstPad *src_pad; + GstBuffer *new; + GstBuffer *buffer; + GstBuffer *buffer_out; + GstCaps *sink_caps; + + buffer = make_buffer_with_caps (in_string, in_size); + buffer_out = make_buffer_with_caps (out_string, out_size); + + /* check that there are no buffers waiting */ + gst_check_drop_buffers (); + + /* create the element */ + element = gst_check_setup_element ("aspectratiocrop"); + + /* set the requested aspect ratio */ + g_object_set (G_OBJECT (element), "aspect-ratio", ar_n, ar_d, NULL); + + /* create the src pad */ + src_pad = gst_pad_new (NULL, GST_PAD_SRC); + gst_pad_set_caps (src_pad, GST_BUFFER_CAPS (buffer)); + pad_peer = gst_element_get_static_pad (element, "sink"); + fail_if (pad_peer == NULL); + fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK, + "Could not link source and %s sink pads", GST_ELEMENT_NAME (element)); + gst_object_unref (pad_peer); + gst_pad_set_active (src_pad, TRUE); + + /* create the sink pad */ + pad_peer = gst_element_get_static_pad (element, "src"); + sink_caps = gst_caps_from_string (ASPECT_RATIO_CROP_CAPS); + sink_pad = gst_pad_new (NULL, GST_PAD_SINK); + GST_PAD_CAPS (sink_pad) = sink_caps; + fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK, + "Could not link sink and %s source pads", GST_ELEMENT_NAME (element)); + gst_object_unref (pad_peer); + gst_pad_set_chain_function (sink_pad, gst_check_chain_func); + gst_pad_set_active (sink_pad, TRUE); + + /* configure the sink pad */ + fail_unless (gst_element_set_state (element, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "could not set to playing"); + fail_unless (gst_pad_push (src_pad, buffer) == GST_FLOW_OK, + "Failed to push buffer"); + fail_unless (gst_element_set_state (element, + GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null"); + + /* check the result */ + fail_unless (g_list_length (buffers) == 1); + new = GST_BUFFER (buffers->data); + buffers = g_list_remove (buffers, new); + fail_unless (GST_BUFFER_SIZE (buffer_out) == GST_BUFFER_SIZE (new), + "size of the buffers are not the same"); + gst_check_caps_equal (GST_BUFFER_CAPS (buffer_out), GST_BUFFER_CAPS (new)); + gst_buffer_unref (new); + gst_buffer_unref (buffer_out); + + /* teardown the element and pads */ + gst_pad_set_active (src_pad, FALSE); + gst_check_teardown_src_pad (element); + gst_pad_set_active (sink_pad, FALSE); + gst_check_teardown_sink_pad (element); + gst_check_teardown_element (element); +} + +GST_START_TEST (test_no_cropping) +{ + check_aspectratiocrop + ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1", + "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1", + 153600, 153600, 4, 3); + check_aspectratiocrop + ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)320, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3", + "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)320, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3", + 204800, 204800, 4, 3); +} + +GST_END_TEST; + +GST_START_TEST (test_autocropping) +{ + check_aspectratiocrop + ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3", + "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)240, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3", + 153600, 115200, 4, 3); + + check_aspectratiocrop + ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/9", + "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)180, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/9", + 153600, 86400, 4, 3); + + check_aspectratiocrop + ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/15", + "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)192, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/15", + 153600, 122880, 16, 9); + +} + +GST_END_TEST; + +static Suite * +aspectratiocrop_suite (void) +{ + Suite *s = suite_create ("aspectratiocrop"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + tcase_add_test (tc_chain, test_no_cropping); + tcase_add_test (tc_chain, test_autocropping); + + return s; +} + +int +main (int argc, char **argv) +{ + int nf; + + Suite *s = aspectratiocrop_suite (); + SRunner *sr = srunner_create (s); + + gst_check_init (&argc, &argv); + + srunner_run_all (sr, CK_NORMAL); + nf = srunner_ntests_failed (sr); + srunner_free (sr); + + return nf; +} -- 2.7.4