Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / tests / check / elements / videocrop.c
1 /* GStreamer unit test for the videocrop element
2  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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 #ifdef HAVE_VALGRIND
25 # include <valgrind/valgrind.h>
26 #endif
27
28 #include <unistd.h>
29
30 #include <gst/check/gstcheck.h>
31 #include <gst/video/video.h>
32 #include <gst/base/gstbasetransform.h>
33
34 /* return a list of caps where we only need to set
35  * width and height to get fixed caps */
36 static GList *
37 video_crop_get_test_caps (GstElement * videocrop)
38 {
39   const GstCaps *allowed_caps;
40   GstPad *srcpad;
41   GList *list = NULL;
42   guint i;
43
44   srcpad = gst_element_get_static_pad (videocrop, "src");
45   fail_unless (srcpad != NULL);
46   allowed_caps = gst_pad_get_pad_template_caps (srcpad);
47   fail_unless (allowed_caps != NULL);
48
49   for (i = 0; i < gst_caps_get_size (allowed_caps); ++i) {
50     GstStructure *new_structure;
51     GstCaps *single_caps;
52
53     single_caps = gst_caps_new_empty ();
54     new_structure =
55         gst_structure_copy (gst_caps_get_structure (allowed_caps, i));
56     gst_structure_set (new_structure, "framerate", GST_TYPE_FRACTION,
57         1, 1, NULL);
58     gst_structure_remove_field (new_structure, "width");
59     gst_structure_remove_field (new_structure, "height");
60     gst_caps_append_structure (single_caps, new_structure);
61
62     /* should be fixed without width/height */
63     fail_unless (gst_caps_is_fixed (single_caps));
64
65     list = g_list_prepend (list, single_caps);
66   }
67
68   gst_object_unref (srcpad);
69
70   return list;
71 }
72
73 GST_START_TEST (test_unit_sizes)
74 {
75   GstBaseTransformClass *csp_klass, *vcrop_klass;
76   GstElement *videocrop, *csp;
77   GList *caps_list, *l;
78
79   videocrop = gst_element_factory_make ("videocrop", "videocrop");
80   fail_unless (videocrop != NULL, "Failed to create videocrop element");
81   vcrop_klass = GST_BASE_TRANSFORM_GET_CLASS (videocrop);
82
83   csp = gst_element_factory_make ("ffmpegcolorspace", "csp");
84   fail_unless (csp != NULL, "Failed to create ffmpegcolorspace element");
85   csp_klass = GST_BASE_TRANSFORM_GET_CLASS (csp);
86
87   caps_list = video_crop_get_test_caps (videocrop);
88
89   for (l = caps_list; l != NULL; l = l->next) {
90     const struct
91     {
92       gint width, height;
93     } sizes_to_try[] = {
94       {
95       160, 120}, {
96       161, 120}, {
97       160, 121}, {
98       161, 121}, {
99       159, 120}, {
100       160, 119}, {
101       159, 119}, {
102       159, 121}
103     };
104     GstStructure *s;
105     GstCaps *caps;
106     gint i;
107
108     caps = gst_caps_copy (GST_CAPS (l->data));
109     s = gst_caps_get_structure (caps, 0);
110     fail_unless (s != NULL);
111
112     for (i = 0; i < G_N_ELEMENTS (sizes_to_try); ++i) {
113       gchar *caps_str;
114       gsize csp_size = 0;
115       gsize vc_size = 0;
116
117       gst_structure_set (s, "width", G_TYPE_INT, sizes_to_try[i].width,
118           "height", G_TYPE_INT, sizes_to_try[i].height, NULL);
119
120       caps_str = gst_caps_to_string (caps);
121       GST_INFO ("Testing unit size for %s", caps_str);
122
123       /* skip if ffmpegcolorspace doesn't support these caps
124        * (only works with gst-plugins-base 0.10.9.1 or later) */
125       if (!csp_klass->get_unit_size ((GstBaseTransform *) csp, caps, &csp_size)) {
126         GST_INFO ("ffmpegcolorspace does not support format %s", caps_str);
127         g_free (caps_str);
128         continue;
129       }
130
131       fail_unless (vcrop_klass->get_unit_size ((GstBaseTransform *) videocrop,
132               caps, &vc_size));
133
134       fail_unless (vc_size == csp_size,
135           "videocrop and ffmpegcolorspace return different unit sizes for "
136           "caps %s: vc_size=%d, csp_size=%d", caps_str, vc_size, csp_size);
137
138       g_free (caps_str);
139     }
140
141     gst_caps_unref (caps);
142   }
143
144   g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
145   g_list_free (caps_list);
146
147   gst_object_unref (csp);
148   gst_object_unref (videocrop);
149 }
150
151 GST_END_TEST;
152
153 typedef struct
154 {
155   GstElement *pipeline;
156   GstElement *src;
157   GstElement *filter;
158   GstElement *crop;
159   GstElement *sink;
160   GstBuffer *last_buf;
161   GstCaps *last_caps;
162 } GstVideoCropTestContext;
163
164 static void
165 handoff_cb (GstElement * sink, GstBuffer * buf, GstPad * pad,
166     GstVideoCropTestContext * ctx)
167 {
168   GstCaps *caps;
169
170   gst_buffer_replace (&ctx->last_buf, buf);
171   caps = gst_pad_get_current_caps (pad);
172   gst_caps_replace (&ctx->last_caps, caps);
173   gst_caps_unref (caps);
174 }
175
176 static void
177 videocrop_test_cropping_init_context (GstVideoCropTestContext * ctx)
178 {
179   fail_unless (ctx != NULL);
180
181   ctx->pipeline = gst_pipeline_new ("pipeline");
182   fail_unless (ctx->pipeline != NULL);
183   ctx->src = gst_element_factory_make ("videotestsrc", "src");
184   fail_unless (ctx->src != NULL, "Failed to create videotestsrc element");
185   ctx->filter = gst_element_factory_make ("capsfilter", "filter");
186   fail_unless (ctx->filter != NULL, "Failed to create capsfilter element");
187   ctx->crop = gst_element_factory_make ("videocrop", "crop");
188   fail_unless (ctx->crop != NULL, "Failed to create videocrop element");
189   ctx->sink = gst_element_factory_make ("fakesink", "sink");
190   fail_unless (ctx->sink != NULL, "Failed to create fakesink element");
191
192   gst_bin_add_many (GST_BIN (ctx->pipeline), ctx->src, ctx->filter,
193       ctx->crop, ctx->sink, NULL);
194   gst_element_link_many (ctx->src, ctx->filter, ctx->crop, ctx->sink, NULL);
195
196   /* set pattern to 'red' - for our purposes it doesn't matter anyway */
197   g_object_set (ctx->src, "pattern", 4, NULL);
198
199   g_object_set (ctx->sink, "signal-handoffs", TRUE, NULL);
200   g_signal_connect (ctx->sink, "preroll-handoff", G_CALLBACK (handoff_cb), ctx);
201
202   ctx->last_buf = NULL;
203   ctx->last_caps = NULL;
204
205   GST_LOG ("context inited");
206 }
207
208 static void
209 videocrop_test_cropping_deinit_context (GstVideoCropTestContext * ctx)
210 {
211   GST_LOG ("deiniting context");
212
213   gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
214   gst_object_unref (ctx->pipeline);
215   gst_buffer_replace (&ctx->last_buf, NULL);
216   memset (ctx, 0x00, sizeof (GstVideoCropTestContext));
217 }
218
219 typedef void (*GstVideoCropTestBufferFunc) (GstBuffer * buffer, GstCaps * caps);
220
221 static void
222 videocrop_test_cropping (GstVideoCropTestContext * ctx, GstCaps * in_caps,
223     gint left, gint right, gint top, gint bottom,
224     GstVideoCropTestBufferFunc func)
225 {
226   GST_LOG ("lrtb = %03u %03u %03u %03u, caps = %" GST_PTR_FORMAT, left, right,
227       top, bottom, in_caps);
228
229   g_object_set (ctx->filter, "caps", in_caps, NULL);
230
231   g_object_set (ctx->crop, "left", left, "right", right, "top", top,
232       "bottom", bottom, NULL);
233
234   /* this will fail if videotestsrc doesn't support our format; we need
235    * videotestsrc from -base CVS 0.10.9.1 with RGBA and AYUV support */
236   fail_unless (gst_element_set_state (ctx->pipeline,
237           GST_STATE_PAUSED) != GST_STATE_CHANGE_FAILURE);
238   fail_unless (gst_element_get_state (ctx->pipeline, NULL, NULL,
239           -1) == GST_STATE_CHANGE_SUCCESS);
240
241   if (func != NULL) {
242     func (ctx->last_buf, ctx->last_caps);
243   }
244
245   gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
246 }
247
248 static void
249 check_1x1_buffer (GstBuffer * buf, GstCaps * caps)
250 {
251   GstVideoInfo info;
252   GstVideoFrame frame;
253   /* the exact values we check for come from videotestsrc */
254   static const guint yuv_values[] = { 81, 90, 240, 0 };
255   static const guint rgb_values[] = { 0xff, 0, 0, 0 };
256   const guint *values;
257   guint i;
258
259   fail_unless (buf != NULL);
260   fail_unless (caps != NULL);
261
262   fail_unless (gst_video_info_from_caps (&info, caps));
263   fail_unless (gst_video_frame_map (&frame, &info, buf, GST_MAP_READ));
264
265   if (GST_VIDEO_INFO_IS_YUV (&info))
266     values = yuv_values;
267   else
268     values = rgb_values;
269
270   for (i = 0; i < GST_VIDEO_FRAME_N_COMPONENTS (&frame); i++) {
271     guint8 *data = GST_VIDEO_FRAME_COMP_DATA (&frame, i);
272
273     fail_unless_equals_int (data[0], values[i]);
274   }
275
276   /*
277      fail_unless_equals_int ((pixel & rmask) >> rshift, 0xff);
278      fail_unless_equals_int ((pixel & gmask) >> gshift, 0x00);
279      fail_unless_equals_int ((pixel & bmask) >> bshift, 0x00);
280    */
281 }
282
283 GST_START_TEST (test_crop_to_1x1)
284 {
285   GstVideoCropTestContext ctx;
286   GList *caps_list, *node;
287
288   videocrop_test_cropping_init_context (&ctx);
289
290   caps_list = video_crop_get_test_caps (ctx.crop);
291
292   for (node = caps_list; node != NULL; node = node->next) {
293     GstStructure *s;
294     GstCaps *caps;
295
296     caps = gst_caps_copy (GST_CAPS (node->data));
297     s = gst_caps_get_structure (caps, 0);
298     fail_unless (s != NULL);
299
300     if (g_strcmp0 (gst_structure_get_name (s), "video/x-raw-gray") == 0) {
301       /* videotestsrc does not support this format */
302       gst_caps_unref (caps);
303       continue;
304     }
305
306     GST_INFO ("testing format: %" GST_PTR_FORMAT, caps);
307
308     gst_structure_set (s, "width", G_TYPE_INT, 160,
309         "height", G_TYPE_INT, 160, NULL);
310
311     videocrop_test_cropping (&ctx, caps, 159, 0, 159, 0, check_1x1_buffer);
312     /* commented out because they don't really add anything useful check-wise:
313        videocrop_test_cropping (&ctx, caps, 0, 159, 0, 159, check_1x1_buffer);
314        videocrop_test_cropping (&ctx, caps, 159, 0, 0, 159, check_1x1_buffer);
315        videocrop_test_cropping (&ctx, caps, 0, 159, 159, 0, check_1x1_buffer);
316      */
317     gst_caps_unref (caps);
318   }
319   g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
320   g_list_free (caps_list);
321
322   videocrop_test_cropping_deinit_context (&ctx);
323 }
324
325 GST_END_TEST;
326
327 GST_START_TEST (test_cropping)
328 {
329   GstVideoCropTestContext ctx;
330   struct
331   {
332     gint width, height;
333   } sizes_to_try[] = {
334     {
335     160, 160}, {
336     161, 160}, {
337     160, 161}, {
338     161, 161}, {
339     159, 160}, {
340     160, 159}, {
341     159, 159}, {
342     159, 161}
343   };
344   GList *caps_list, *node;
345   gint i;
346
347   videocrop_test_cropping_init_context (&ctx);
348
349   caps_list = video_crop_get_test_caps (ctx.crop);
350   node = g_list_nth (caps_list, __i__);
351
352   if (node != NULL) {
353     GstStructure *s;
354     GstCaps *caps;
355
356     caps = gst_caps_copy (GST_CAPS (node->data));
357     s = gst_caps_get_structure (caps, 0);
358     fail_unless (s != NULL);
359
360     /* videotestsrc does not support this format, so ignore it */
361     if (!g_str_equal (gst_structure_get_name (s), "video/x-raw-gray")) {
362       GST_INFO ("testing format: %" GST_PTR_FORMAT, caps);
363
364       for (i = 0; i < G_N_ELEMENTS (sizes_to_try); ++i) {
365         GstCaps *in_caps;
366
367         GST_INFO (" - %d x %d", sizes_to_try[i].width, sizes_to_try[i].height);
368
369         gst_structure_set (s, "width", G_TYPE_INT, sizes_to_try[i].width,
370             "height", G_TYPE_INT, sizes_to_try[i].height, NULL);
371         in_caps = gst_caps_copy (caps);
372
373         videocrop_test_cropping (&ctx, in_caps, 0, 0, 0, 0, NULL);
374         videocrop_test_cropping (&ctx, in_caps, 1, 0, 0, 0, NULL);
375         videocrop_test_cropping (&ctx, in_caps, 0, 1, 0, 0, NULL);
376         videocrop_test_cropping (&ctx, in_caps, 0, 0, 1, 0, NULL);
377         videocrop_test_cropping (&ctx, in_caps, 0, 0, 0, 1, NULL);
378         videocrop_test_cropping (&ctx, in_caps, 63, 0, 0, 0, NULL);
379         videocrop_test_cropping (&ctx, in_caps, 0, 63, 0, 0, NULL);
380         videocrop_test_cropping (&ctx, in_caps, 0, 0, 63, 0, NULL);
381         videocrop_test_cropping (&ctx, in_caps, 0, 0, 0, 63, NULL);
382         videocrop_test_cropping (&ctx, in_caps, 63, 0, 0, 1, NULL);
383         videocrop_test_cropping (&ctx, in_caps, 0, 63, 1, 0, NULL);
384         videocrop_test_cropping (&ctx, in_caps, 0, 1, 63, 0, NULL);
385         videocrop_test_cropping (&ctx, in_caps, 1, 0, 0, 63, NULL);
386         videocrop_test_cropping (&ctx, in_caps, 0, 0, 0, 0, NULL);
387         videocrop_test_cropping (&ctx, in_caps, 32, 0, 0, 128, NULL);
388         videocrop_test_cropping (&ctx, in_caps, 0, 32, 128, 0, NULL);
389         videocrop_test_cropping (&ctx, in_caps, 0, 128, 32, 0, NULL);
390         videocrop_test_cropping (&ctx, in_caps, 128, 0, 0, 32, NULL);
391         videocrop_test_cropping (&ctx, in_caps, 1, 1, 1, 1, NULL);
392         videocrop_test_cropping (&ctx, in_caps, 63, 63, 63, 63, NULL);
393         videocrop_test_cropping (&ctx, in_caps, 64, 64, 64, 64, NULL);
394
395         gst_caps_unref (in_caps);
396       }
397     }
398
399     gst_caps_unref (caps);
400   } else {
401     GST_INFO ("no caps #%d", __i__);
402   }
403   g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
404   g_list_free (caps_list);
405
406   videocrop_test_cropping_deinit_context (&ctx);
407 }
408
409 GST_END_TEST;
410
411
412 static GstPadProbeReturn
413 buffer_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
414 {
415   GstBuffer **p_buf = data;
416   GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
417
418   gst_buffer_replace (p_buf, buf);
419
420   return GST_PAD_PROBE_OK;      /* keep data */
421 }
422
423 GST_START_TEST (test_passthrough)
424 {
425   GstStateChangeReturn state_ret;
426   GstVideoCropTestContext ctx;
427   GstPad *srcpad;
428   GstBuffer *gen_buf = NULL;    /* buffer generated by videotestsrc */
429
430   videocrop_test_cropping_init_context (&ctx);
431
432   g_object_set (ctx.src, "num-buffers", 1, NULL);
433
434   srcpad = gst_element_get_static_pad (ctx.src, "src");
435   fail_unless (srcpad != NULL);
436   gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BUFFER, buffer_probe_cb,
437       &gen_buf, NULL);
438   gst_object_unref (srcpad);
439
440   g_object_set (ctx.crop, "left", 0, "right", 0, "top", 0, "bottom", 0, NULL);
441
442   state_ret = gst_element_set_state (ctx.pipeline, GST_STATE_PAUSED);
443   fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
444       "couldn't set pipeline to PAUSED state");
445
446   state_ret = gst_element_get_state (ctx.pipeline, NULL, NULL, -1);
447   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
448       "pipeline failed to go to PAUSED state");
449
450   fail_unless (gen_buf != NULL);
451   fail_unless (ctx.last_buf != NULL);
452
453   /* pass through should do nothing */
454   fail_unless (gen_buf == ctx.last_buf);
455
456   videocrop_test_cropping_deinit_context (&ctx);
457
458   fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT_VALUE (gen_buf), 1);
459   gst_buffer_unref (gen_buf);
460 }
461
462 GST_END_TEST;
463
464 static gint
465 notgst_value_list_get_nth_int (const GValue * list_val, guint n)
466 {
467   const GValue *v;
468
469   fail_unless (GST_VALUE_HOLDS_LIST (list_val));
470   fail_unless (n < gst_value_list_get_size (list_val));
471
472   v = gst_value_list_get_value (list_val, n);
473   fail_unless (G_VALUE_HOLDS_INT (v));
474   return g_value_get_int (v);
475 }
476
477 GST_START_TEST (test_caps_transform)
478 {
479   GstVideoCropTestContext ctx;
480   GstBaseTransformClass *klass;
481   GstBaseTransform *crop;
482   const GValue *w_val;
483   const GValue *h_val;
484   GstCaps *caps, *adj_caps;
485
486   videocrop_test_cropping_init_context (&ctx);
487
488   crop = GST_BASE_TRANSFORM (ctx.crop);
489   klass = GST_BASE_TRANSFORM_GET_CLASS (ctx.crop);
490   fail_unless (klass != NULL);
491
492   caps = gst_caps_new_simple ("video/x-raw-yuv",
493       "format", G_TYPE_STRING, "I420",
494       "framerate", GST_TYPE_FRACTION, 1, 1,
495       "width", G_TYPE_INT, 200, "height", G_TYPE_INT, 100, NULL);
496
497   /* by default, it should be no cropping and hence passthrough */
498   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
499   fail_unless (adj_caps != NULL);
500   fail_unless (gst_caps_is_equal (adj_caps, caps));
501   gst_caps_unref (adj_caps);
502
503   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
504   fail_unless (adj_caps != NULL);
505   fail_unless (gst_caps_is_equal (adj_caps, caps));
506   gst_caps_unref (adj_caps);
507
508   /* make sure that's still true after changing properties back and forth */
509   g_object_set (ctx.crop, "left", 1, "right", 3, "top", 5, "bottom", 7, NULL);
510   g_object_set (ctx.crop, "left", 0, "right", 0, "top", 0, "bottom", 0, NULL);
511
512   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
513   fail_unless (adj_caps != NULL);
514   fail_unless (gst_caps_is_equal (adj_caps, caps));
515   gst_caps_unref (adj_caps);
516
517   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
518   fail_unless (adj_caps != NULL);
519   fail_unless (gst_caps_is_equal (adj_caps, caps));
520   gst_caps_unref (adj_caps);
521
522   /* now check adjustments made ... */
523   g_object_set (ctx.crop, "left", 1, "right", 3, "top", 5, "bottom", 7, NULL);
524
525   /* ========= (1) fixed value ============================================= */
526
527   /* sink => source, source must be bigger if we crop stuff off */
528   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
529   fail_unless (adj_caps != NULL);
530   fail_unless (gst_caps_get_size (adj_caps) == 1);
531   w_val =
532       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
533   fail_unless (w_val != NULL);
534   fail_unless (G_VALUE_HOLDS_INT (w_val));
535   fail_unless_equals_int (g_value_get_int (w_val), 200 + (1 + 3));
536   h_val =
537       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
538   fail_unless (h_val != NULL);
539   fail_unless (G_VALUE_HOLDS_INT (h_val));
540   fail_unless_equals_int (g_value_get_int (h_val), 100 + (5 + 7));
541   gst_caps_unref (adj_caps);
542
543   /* source => sink becomes smaller */
544   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
545   fail_unless (adj_caps != NULL);
546   fail_unless (gst_caps_get_size (adj_caps) == 1);
547   w_val =
548       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
549   fail_unless (w_val != NULL);
550   fail_unless (G_VALUE_HOLDS_INT (w_val));
551   fail_unless_equals_int (g_value_get_int (w_val), 200 - (1 + 3));
552   h_val =
553       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
554   fail_unless (h_val != NULL);
555   fail_unless (G_VALUE_HOLDS_INT (h_val));
556   fail_unless_equals_int (g_value_get_int (h_val), 100 - (5 + 7));
557   gst_caps_unref (adj_caps);
558
559   /* ========= (2) range (simple adjustment) =============================== */
560
561   gst_structure_set (gst_caps_get_structure (caps, 0),
562       "width", GST_TYPE_INT_RANGE, 1000, 2000,
563       "height", GST_TYPE_INT_RANGE, 3000, 4000, NULL);
564
565   /* sink => source, source must be bigger if we crop stuff off */
566   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
567   fail_unless (adj_caps != NULL);
568   fail_unless (gst_caps_get_size (adj_caps) == 1);
569   w_val =
570       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
571   fail_unless (w_val != NULL);
572   fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
573   fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1000 + (1 + 3));
574   fail_unless_equals_int (gst_value_get_int_range_max (w_val), 2000 + (1 + 3));
575   h_val =
576       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
577   fail_unless (h_val != NULL);
578   fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
579   fail_unless_equals_int (gst_value_get_int_range_min (h_val), 3000 + (5 + 7));
580   fail_unless_equals_int (gst_value_get_int_range_max (h_val), 4000 + (5 + 7));
581   gst_caps_unref (adj_caps);
582
583   /* source => sink becomes smaller */
584   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
585   fail_unless (adj_caps != NULL);
586   fail_unless (gst_caps_get_size (adj_caps) == 1);
587   w_val =
588       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
589   fail_unless (w_val != NULL);
590   fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
591   fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1000 - (1 + 3));
592   fail_unless_equals_int (gst_value_get_int_range_max (w_val), 2000 - (1 + 3));
593   h_val =
594       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
595   fail_unless (h_val != NULL);
596   fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
597   fail_unless_equals_int (gst_value_get_int_range_min (h_val), 3000 - (5 + 7));
598   fail_unless_equals_int (gst_value_get_int_range_max (h_val), 4000 - (5 + 7));
599   gst_caps_unref (adj_caps);
600
601   /* ========= (3) range (adjustment at boundary) ========================== */
602
603   gst_structure_set (gst_caps_get_structure (caps, 0),
604       "width", GST_TYPE_INT_RANGE, 2, G_MAXINT,
605       "height", GST_TYPE_INT_RANGE, 2, G_MAXINT, NULL);
606
607   /* sink => source, source must be bigger if we crop stuff off */
608   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
609   fail_unless (adj_caps != NULL);
610   fail_unless (gst_caps_get_size (adj_caps) == 1);
611   w_val =
612       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
613   fail_unless (w_val != NULL);
614   fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
615   fail_unless_equals_int (gst_value_get_int_range_min (w_val), 2 + (1 + 3));
616   fail_unless_equals_int (gst_value_get_int_range_max (w_val), G_MAXINT);
617   h_val =
618       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
619   fail_unless (h_val != NULL);
620   fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
621   fail_unless_equals_int (gst_value_get_int_range_min (h_val), 2 + (5 + 7));
622   fail_unless_equals_int (gst_value_get_int_range_max (h_val), G_MAXINT);
623   gst_caps_unref (adj_caps);
624
625   /* source => sink becomes smaller */
626   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
627   fail_unless (adj_caps != NULL);
628   fail_unless (gst_caps_get_size (adj_caps) == 1);
629   w_val =
630       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
631   fail_unless (w_val != NULL);
632   fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
633   fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1);
634   fail_unless_equals_int (gst_value_get_int_range_max (w_val),
635       G_MAXINT - (1 + 3));
636   h_val =
637       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
638   fail_unless (h_val != NULL);
639   fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
640   fail_unless_equals_int (gst_value_get_int_range_min (h_val), 1);
641   fail_unless_equals_int (gst_value_get_int_range_max (h_val),
642       G_MAXINT - (5 + 7));
643   gst_caps_unref (adj_caps);
644
645   /* ========= (4) list of values ========================================== */
646
647   {
648     GValue list = { 0, };
649     GValue ival = { 0, };
650
651     g_value_init (&ival, G_TYPE_INT);
652     g_value_init (&list, GST_TYPE_LIST);
653     g_value_set_int (&ival, 2);
654     gst_value_list_append_value (&list, &ival);
655     g_value_set_int (&ival, G_MAXINT);
656     gst_value_list_append_value (&list, &ival);
657     gst_structure_set_value (gst_caps_get_structure (caps, 0), "width", &list);
658     g_value_unset (&list);
659     g_value_unset (&ival);
660
661     g_value_init (&ival, G_TYPE_INT);
662     g_value_init (&list, GST_TYPE_LIST);
663     g_value_set_int (&ival, 5);
664     gst_value_list_append_value (&list, &ival);
665     g_value_set_int (&ival, 1000);
666     gst_value_list_append_value (&list, &ival);
667     gst_structure_set_value (gst_caps_get_structure (caps, 0), "height", &list);
668     g_value_unset (&list);
669     g_value_unset (&ival);
670   }
671
672   /* sink => source, source must be bigger if we crop stuff off */
673   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
674   fail_unless (adj_caps != NULL);
675   fail_unless (gst_caps_get_size (adj_caps) == 1);
676   w_val =
677       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
678   fail_unless (w_val != NULL);
679   fail_unless (GST_VALUE_HOLDS_LIST (w_val));
680   fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 0),
681       2 + (1 + 3));
682   fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 1), G_MAXINT);
683   h_val =
684       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
685   fail_unless (h_val != NULL);
686   fail_unless (GST_VALUE_HOLDS_LIST (h_val));
687   fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 0),
688       5 + (5 + 7));
689   fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 1),
690       1000 + (5 + 7));
691   gst_caps_unref (adj_caps);
692
693   /* source => sink becomes smaller */
694   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
695   fail_unless (adj_caps != NULL);
696   fail_unless (gst_caps_get_size (adj_caps) == 1);
697   w_val =
698       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
699   fail_unless (w_val != NULL);
700   fail_unless (GST_VALUE_HOLDS_LIST (w_val));
701   fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 0), 1);
702   fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 1),
703       G_MAXINT - (1 + 3));
704   h_val =
705       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
706   fail_unless (h_val != NULL);
707   fail_unless (GST_VALUE_HOLDS_LIST (h_val));
708   fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 0), 1);
709   fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 1),
710       1000 - (5 + 7));
711   gst_caps_unref (adj_caps);
712
713   gst_caps_unref (caps);
714   videocrop_test_cropping_deinit_context (&ctx);
715 }
716
717 GST_END_TEST;
718
719 static Suite *
720 videocrop_suite (void)
721 {
722   Suite *s = suite_create ("videocrop");
723   TCase *tc_chain = tcase_create ("general");
724
725 #ifdef HAVE_VALGRIND
726   if (RUNNING_ON_VALGRIND) {
727     /* our tests take quite a long time, so increase
728      * timeout (~25 minutes on my 1.6GHz AMD K7) */
729     tcase_set_timeout (tc_chain, 30 * 60);
730   } else
731 #endif
732   {
733     /* increase timeout, these tests take a long time (60 secs here) */
734     tcase_set_timeout (tc_chain, 2 * 60);
735   }
736
737   suite_add_tcase (s, tc_chain);
738   tcase_add_test (tc_chain, test_crop_to_1x1);
739   tcase_add_test (tc_chain, test_caps_transform);
740   tcase_add_test (tc_chain, test_passthrough);
741   tcase_add_test (tc_chain, test_unit_sizes);
742   tcase_add_loop_test (tc_chain, test_cropping, 0, 25);
743
744   return s;
745 }
746
747 int
748 main (int argc, char **argv)
749 {
750   int nf;
751
752   Suite *s = videocrop_suite ();
753   SRunner *sr = srunner_create (s);
754
755 #ifdef HAVE_VALGRIND
756   if (RUNNING_ON_VALGRIND) {
757     /* otherwise valgrind errors out when liboil probes CPU extensions
758      * in oil_init() during which it causes SIGILLs etc. to be fired */
759     g_setenv ("OIL_CPU_FLAGS", "0", 0);
760   }
761 #endif
762
763   gst_check_init (&argc, &argv);
764
765   srunner_run_all (sr, CK_NORMAL);
766   nf = srunner_ntests_failed (sr);
767   srunner_free (sr);
768
769   return nf;
770 }