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