1 /* GStreamer unit test for the aspectratiocrop element
2  * Copyright (C) 2009 Thijs Vermeir <thijsvermeir@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23 
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
26 #include <gst/check/gstcheck.h>
27 
28 #define ASPECT_RATIO_CROP_CAPS                      \
29   GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, " \
30       "RGBA, ARGB, BGRA, ABGR, RGB, BGR, AYUV, "    \
31       "YUY2, YVYU, UYVY, GRAY8, I420, YV12, RGB16, " \
32       "RGB15 }")
33 
34 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS (ASPECT_RATIO_CROP_CAPS)
38     );
39 
40 static void
check_aspectratiocrop(const gchar * in_string,const gchar * out_string,gint in_size,gint out_size,gint ar_n,gint ar_d)41 check_aspectratiocrop (const gchar * in_string, const gchar * out_string,
42     gint in_size, gint out_size, gint ar_n, gint ar_d)
43 {
44   GstElement *element;
45   GstPad *pad_peer;
46   GstPad *sink_pad = NULL;
47   GstPad *src_pad;
48   GstBuffer *new;
49   GstBuffer *buffer;
50   GstBuffer *buffer_out;
51   GstCaps *incaps;
52   GstCaps *outcaps;
53 
54   incaps = gst_caps_from_string (in_string);
55   buffer = gst_buffer_new_and_alloc (in_size);
56   outcaps = gst_caps_from_string (out_string);
57   buffer_out = gst_buffer_new_and_alloc (out_size);
58 
59   /* check that there are no buffers waiting */
60   gst_check_drop_buffers ();
61 
62   /* create the element */
63   element = gst_check_setup_element ("aspectratiocrop");
64 
65   /* set the requested aspect ratio */
66   g_object_set (G_OBJECT (element), "aspect-ratio", ar_n, ar_d, NULL);
67 
68   /* create the src pad */
69   src_pad = gst_pad_new (NULL, GST_PAD_SRC);
70   gst_pad_set_active (src_pad, TRUE);
71   GST_DEBUG ("setting caps %s %" GST_PTR_FORMAT, in_string, incaps);
72   gst_check_setup_events (src_pad, element, incaps, GST_FORMAT_TIME);
73 
74   pad_peer = gst_element_get_static_pad (element, "sink");
75   fail_if (pad_peer == NULL);
76   fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
77       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
78   gst_object_unref (pad_peer);
79 
80   /* create the sink pad */
81   pad_peer = gst_element_get_static_pad (element, "src");
82   sink_pad = gst_pad_new_from_static_template (&sinktemplate, "sink");
83   fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
84       "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
85   gst_object_unref (pad_peer);
86   gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
87   gst_pad_set_active (sink_pad, TRUE);
88 
89   /* configure the sink pad */
90   fail_unless (gst_element_set_state (element,
91           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
92       "could not set to playing");
93 
94   fail_unless (gst_pad_push (src_pad, buffer) == GST_FLOW_OK,
95       "Failed to push buffer");
96   fail_unless (gst_element_set_state (element,
97           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
98 
99   /* check the result */
100   fail_unless (g_list_length (buffers) == 1);
101   new = GST_BUFFER (buffers->data);
102   buffers = g_list_remove (buffers, new);
103   fail_unless (gst_buffer_get_size (buffer_out) == gst_buffer_get_size (new),
104       "size of the buffers are not the same");
105   {
106     GstCaps *sinkpad_caps;
107 
108     sinkpad_caps = gst_pad_get_current_caps (sink_pad);
109 
110     gst_check_caps_equal (sinkpad_caps, outcaps);
111 
112     gst_caps_unref (sinkpad_caps);
113   }
114   gst_buffer_unref (new);
115   gst_buffer_unref (buffer_out);
116   gst_caps_unref (outcaps);
117   gst_caps_unref (incaps);
118 
119   /* teardown the element and pads */
120   gst_pad_set_active (src_pad, FALSE);
121   gst_check_teardown_src_pad (element);
122   gst_pad_set_active (sink_pad, FALSE);
123   gst_check_teardown_sink_pad (element);
124   gst_check_teardown_element (element);
125 }
126 
GST_START_TEST(test_no_cropping)127 GST_START_TEST (test_no_cropping)
128 {
129   check_aspectratiocrop
130       ("video/x-raw, format=(string)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1",
131       "video/x-raw, format=(string)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1",
132       153600, 153600, 4, 3);
133   check_aspectratiocrop
134       ("video/x-raw, format=(string)YUY2, width=(int)320, height=(int)320, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3",
135       "video/x-raw, format=(string)YUY2, width=(int)320, height=(int)320, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3",
136       204800, 204800, 4, 3);
137 }
138 
139 GST_END_TEST;
140 
GST_START_TEST(test_autocropping)141 GST_START_TEST (test_autocropping)
142 {
143   check_aspectratiocrop
144       ("video/x-raw, format=(string)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3",
145       "video/x-raw, format=(string)YUY2, width=(int)240, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3",
146       153600, 115200, 4, 3);
147 
148   check_aspectratiocrop
149       ("video/x-raw, format=(string)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/9",
150       "video/x-raw, format=(string)YUY2, width=(int)180, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/9",
151       153600, 86400, 4, 3);
152 
153   check_aspectratiocrop
154       ("video/x-raw, format=(string)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/15",
155       "video/x-raw, format=(string)YUY2, width=(int)320, height=(int)192, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/15",
156       153600, 122880, 16, 9);
157 
158 }
159 
160 GST_END_TEST;
161 
162 static Suite *
aspectratiocrop_suite(void)163 aspectratiocrop_suite (void)
164 {
165   Suite *s = suite_create ("aspectratiocrop");
166   TCase *tc_chain = tcase_create ("general");
167 
168   suite_add_tcase (s, tc_chain);
169   tcase_add_test (tc_chain, test_no_cropping);
170   tcase_add_test (tc_chain, test_autocropping);
171 
172   return s;
173 }
174 
175 GST_CHECK_MAIN (aspectratiocrop);
176