Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / aspectratiocrop.c
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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_RGBx ";"                        \
30   GST_VIDEO_CAPS_xRGB ";"                        \
31   GST_VIDEO_CAPS_BGRx ";"                        \
32   GST_VIDEO_CAPS_xBGR ";"                        \
33   GST_VIDEO_CAPS_RGBA ";"                        \
34   GST_VIDEO_CAPS_ARGB ";"                        \
35   GST_VIDEO_CAPS_BGRA ";"                        \
36   GST_VIDEO_CAPS_ABGR ";"                        \
37   GST_VIDEO_CAPS_RGB ";"                         \
38   GST_VIDEO_CAPS_BGR ";"                         \
39   GST_VIDEO_CAPS_YUV ("AYUV") ";"                \
40   GST_VIDEO_CAPS_YUV ("YUY2") ";"                \
41   GST_VIDEO_CAPS_YUV ("YVYU") ";"                \
42   GST_VIDEO_CAPS_YUV ("UYVY") ";"                \
43   GST_VIDEO_CAPS_YUV ("Y800") ";"                \
44   GST_VIDEO_CAPS_YUV ("I420") ";"                \
45   GST_VIDEO_CAPS_YUV ("YV12") ";"                \
46   GST_VIDEO_CAPS_RGB_16 ";"                      \
47   GST_VIDEO_CAPS_RGB_15
48
49 static GstBuffer *
50 make_buffer_with_caps (const gchar * caps_string, int buffer_size)
51 {
52   GstCaps *caps;
53   GstBuffer *temp;
54
55   caps = gst_caps_from_string (caps_string);
56   temp = gst_buffer_new_and_alloc (buffer_size);
57   fail_if (caps == NULL);
58   fail_if (temp == NULL);
59   gst_buffer_set_caps (temp, caps);
60   gst_caps_unref (caps);
61
62   return temp;
63 }
64
65 static void
66 check_aspectratiocrop (const gchar * in_string, const gchar * out_string,
67     gint in_size, gint out_size, gint ar_n, gint ar_d)
68 {
69   GstElement *element;
70   GstPad *pad_peer;
71   GstPad *sink_pad = NULL;
72   GstPad *src_pad;
73   GstBuffer *new;
74   GstBuffer *buffer;
75   GstBuffer *buffer_out;
76   GstCaps *sink_caps;
77
78   buffer = make_buffer_with_caps (in_string, in_size);
79   buffer_out = make_buffer_with_caps (out_string, out_size);
80
81   /* check that there are no buffers waiting */
82   gst_check_drop_buffers ();
83
84   /* create the element */
85   element = gst_check_setup_element ("aspectratiocrop");
86
87   /* set the requested aspect ratio */
88   g_object_set (G_OBJECT (element), "aspect-ratio", ar_n, ar_d, NULL);
89
90   /* create the src pad */
91   src_pad = gst_pad_new (NULL, GST_PAD_SRC);
92   gst_pad_set_caps (src_pad, GST_BUFFER_CAPS (buffer));
93   pad_peer = gst_element_get_static_pad (element, "sink");
94   fail_if (pad_peer == NULL);
95   fail_unless (gst_pad_link (src_pad, pad_peer) == GST_PAD_LINK_OK,
96       "Could not link source and %s sink pads", GST_ELEMENT_NAME (element));
97   gst_object_unref (pad_peer);
98   gst_pad_set_active (src_pad, TRUE);
99
100   /* create the sink pad */
101   pad_peer = gst_element_get_static_pad (element, "src");
102   sink_caps = gst_caps_from_string (ASPECT_RATIO_CROP_CAPS);
103   sink_pad = gst_pad_new (NULL, GST_PAD_SINK);
104   GST_PAD_CAPS (sink_pad) = sink_caps;
105   fail_unless (gst_pad_link (pad_peer, sink_pad) == GST_PAD_LINK_OK,
106       "Could not link sink and %s source pads", GST_ELEMENT_NAME (element));
107   gst_object_unref (pad_peer);
108   gst_pad_set_chain_function (sink_pad, gst_check_chain_func);
109   gst_pad_set_active (sink_pad, TRUE);
110
111   /* configure the sink pad */
112   fail_unless (gst_element_set_state (element,
113           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
114       "could not set to playing");
115   fail_unless (gst_pad_push (src_pad, buffer) == GST_FLOW_OK,
116       "Failed to push buffer");
117   fail_unless (gst_element_set_state (element,
118           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
119
120   /* check the result */
121   fail_unless (g_list_length (buffers) == 1);
122   new = GST_BUFFER (buffers->data);
123   buffers = g_list_remove (buffers, new);
124   fail_unless (GST_BUFFER_SIZE (buffer_out) == GST_BUFFER_SIZE (new),
125       "size of the buffers are not the same");
126   gst_check_caps_equal (GST_BUFFER_CAPS (buffer_out), GST_BUFFER_CAPS (new));
127   gst_buffer_unref (new);
128   gst_buffer_unref (buffer_out);
129
130   /* teardown the element and pads */
131   gst_pad_set_active (src_pad, FALSE);
132   gst_check_teardown_src_pad (element);
133   gst_pad_set_active (sink_pad, FALSE);
134   gst_check_teardown_sink_pad (element);
135   gst_check_teardown_element (element);
136 }
137
138 GST_START_TEST (test_no_cropping)
139 {
140   check_aspectratiocrop
141       ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1",
142       "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1",
143       153600, 153600, 4, 3);
144   check_aspectratiocrop
145       ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)320, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3",
146       "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)320, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3",
147       204800, 204800, 4, 3);
148 }
149
150 GST_END_TEST;
151
152 GST_START_TEST (test_autocropping)
153 {
154   check_aspectratiocrop
155       ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3",
156       "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)240, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)4/3",
157       153600, 115200, 4, 3);
158
159   check_aspectratiocrop
160       ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/9",
161       "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)180, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/9",
162       153600, 86400, 4, 3);
163
164   check_aspectratiocrop
165       ("video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)240, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/15",
166       "video/x-raw-yuv, format=(fourcc)YUY2, width=(int)320, height=(int)192, framerate=(fraction)30/1, pixel-aspect-ratio=(fraction)16/15",
167       153600, 122880, 16, 9);
168
169 }
170
171 GST_END_TEST;
172
173 static Suite *
174 aspectratiocrop_suite (void)
175 {
176   Suite *s = suite_create ("aspectratiocrop");
177   TCase *tc_chain = tcase_create ("general");
178
179   suite_add_tcase (s, tc_chain);
180   tcase_add_test (tc_chain, test_no_cropping);
181   tcase_add_test (tc_chain, test_autocropping);
182
183   return s;
184 }
185
186 int
187 main (int argc, char **argv)
188 {
189   int nf;
190
191   Suite *s = aspectratiocrop_suite ();
192   SRunner *sr = srunner_create (s);
193
194   gst_check_init (&argc, &argv);
195
196   srunner_run_all (sr, CK_NORMAL);
197   nf = srunner_ntests_failed (sr);
198   srunner_free (sr);
199
200   return nf;
201 }