diff --git a/gst/videocrop/gstaspectratiocrop.c b/gst/videocrop/gstaspectratiocrop.c index 2cdd51ca68..28ca397b44 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,79 +145,14 @@ gst_aspect_ratio_crop_set_caps (GstPad * pad, GstCaps * caps) GstPad *peer_pad; GstStructure *structure; gboolean ret; - 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; - } - - /* 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_fraction (structure, "pixel-aspect-ratio", - &par_n, &par_d)) { - par_d = par_n = 1; - } - - incoming_ar = ((gdouble) (width * par_n)) / (height * par_d); - GST_LOG_OBJECT (aspect_ratio_crop, - "incoming caps width(%d), height(%d), par (%d/%d) : ar = %f", width, - height, par_n, par_d, incoming_ar); - - requested_ar = - (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom; - - /* check if the original aspect-ratio is the aspect-ratio that we want */ - if (requested_ar == incoming_ar) { - 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); - } else if (requested_ar > incoming_ar) { - /* fix aspect ratio with cropping on top and bottom */ - cropvalue = - ((((double) aspect_ratio_crop->ar_denom / - (double) (aspect_ratio_crop->ar_num)) * ((double) par_n / - (double) par_d) * width) - height) / 2; - if (cropvalue < 0) { - cropvalue *= -1; - } - if (cropvalue >= (height / 2)) - goto crop_failed; - gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, cropvalue, 0, - cropvalue, 0); - } else { - /* fix aspect ratio with cropping on left and right */ - cropvalue = - ((((double) aspect_ratio_crop->ar_num / - (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d / - (double) par_n) * height) - width) / 2; - if (cropvalue < 0) { - cropvalue *= -1; - } - if (cropvalue >= (width / 2)) - goto crop_failed; - gst_aspect_ratio_crop_set_cropping (aspect_ratio_crop, 0, cropvalue, 0, - cropvalue); - } - -setcaps_on_peer: - GST_DEBUG_OBJECT (aspect_ratio_crop, - "setting the caps on the videocrop element"); + 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"); @@ -221,21 +161,6 @@ setcaps_on_peer: gst_object_unref (aspect_ratio_crop); g_mutex_unlock (aspect_ratio_crop->crop_lock); return ret; - -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; } static void @@ -260,6 +185,7 @@ gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * 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", @@ -267,11 +193,23 @@ gst_aspect_ratio_crop_class_init (GstAspectRatioCropClass * klass) 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"); @@ -289,14 +227,16 @@ gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop, 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)); + 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 (NULL, link_pad); + 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); @@ -304,6 +244,161 @@ gst_aspect_ratio_crop_init (GstAspectRatioCrop * aspect_ratio_crop, 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; + + /* 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"); + goto beach; + } + + /* get the information from the caps */ + 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)) { + par_d = par_n = 1; + } + + incoming_ar = ((gdouble) (width * par_n)) / (height * par_d); + GST_LOG_OBJECT (aspect_ratio_crop, + "incoming caps width(%d), height(%d), par (%d/%d) : ar = %f", width, + height, par_n, par_d, incoming_ar); + + requested_ar = + (gdouble) aspect_ratio_crop->ar_num / aspect_ratio_crop->ar_denom; + + /* check if the original aspect-ratio is the aspect-ratio that we want */ + if (requested_ar == incoming_ar) { + GST_DEBUG_OBJECT (aspect_ratio_crop, + "Input video already has the correct aspect ratio (%.3f == %.3f)", + incoming_ar, requested_ar); + goto beach; + } else if (requested_ar > incoming_ar) { + /* fix aspect ratio with cropping on top and bottom */ + cropvalue = + ((((double) aspect_ratio_crop->ar_denom / + (double) (aspect_ratio_crop->ar_num)) * ((double) par_n / + (double) par_d) * width) - height) / 2; + if (cropvalue < 0) { + cropvalue *= -1; + } + if (cropvalue >= (height / 2)) + goto crop_failed; + 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 = + ((((double) aspect_ratio_crop->ar_num / + (double) (aspect_ratio_crop->ar_denom)) * ((double) par_d / + (double) par_n) * height) - width) / 2; + if (cropvalue < 0) { + cropvalue *= -1; + } + if (cropvalue >= (width / 2)) + goto crop_failed; + 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); + } + } + + return; + +crop_failed: + GST_WARNING_OBJECT (aspect_ratio_crop, + "can't crop to aspect ratio requested"); + goto beach; +beach: + 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 GstCaps * +gst_aspect_ratio_crop_transform_caps (GstAspectRatioCrop * aspect_ratio_crop, + GstCaps * caps) +{ + GstCaps *transform; + gint size, i; + + transform = gst_caps_new_empty (); + + size = gst_caps_get_size (caps); + + for (i = 0; i < size; i++) { + GstStructure *s; + GstStructure *trans_s; + + s = gst_caps_get_structure (caps, i); + + gst_aspect_ratio_transform_structure (aspect_ratio_crop, s, &trans_s, + FALSE); + gst_caps_append_structure (transform, trans_s); + } + + return transform; +} + +static GstCaps * +gst_aspect_ratio_crop_get_caps (GstPad * pad) +{ + GstPad *peer; + GstAspectRatioCrop *aspect_ratio_crop; + GstCaps *return_caps; + + aspect_ratio_crop = GST_ASPECT_RATIO_CROP (gst_pad_get_parent (pad)); + + g_mutex_lock (aspect_ratio_crop->crop_lock); + + 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; + + 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); + } + + g_mutex_unlock (aspect_ratio_crop->crop_lock); + gst_object_unref (peer); + gst_object_unref (aspect_ratio_crop); + + return return_caps; +} + static void gst_aspect_ratio_crop_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) @@ -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 89a151c4c3..2fd65b3606 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 0000000000..4f8055207f --- /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; +}