From 27093c19a4578456cebe2523092c468d23614431 Mon Sep 17 00:00:00 2001 From: Thijs Vermeir Date: Sat, 24 Jan 2009 14:48:00 +0100 Subject: 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 +++++++++++++++++++++++++------------ 1 file changed, 180 insertions(+), 85 deletions(-) (limited to 'gst/videocrop') diff --git a/gst/videocrop/gstaspectratiocrop.c b/gst/videocrop/gstaspectratiocrop.c index 2cdd51ca..28ca397b 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: -- cgit