• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 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 #ifdef HAVE_VALGRIND
25 # include <valgrind/valgrind.h>
26 #endif
27 
28 #include <gst/check/gstcheck.h>
29 #include <gst/video/video.h>
30 #include <gst/base/gstbasetransform.h>
31 
32 /* return a list of caps where we only need to set
33  * width and height to get fixed caps */
34 static GList *
video_crop_get_test_caps(GstElement * videocrop)35 video_crop_get_test_caps (GstElement * videocrop)
36 {
37   GstCaps *templ, *allowed_caps;
38   GstPad *srcpad;
39   GList *list = NULL;
40   guint i;
41 
42   srcpad = gst_element_get_static_pad (videocrop, "src");
43   fail_unless (srcpad != NULL);
44   templ = gst_pad_get_pad_template_caps (srcpad);
45   fail_unless (templ != NULL);
46 
47   allowed_caps = gst_caps_normalize (templ);
48 
49   for (i = 0; i < gst_caps_get_size (allowed_caps); ++i) {
50     GstStructure *new_structure;
51     GstCaps *single_caps;
52     GstStructure *structure;
53 
54     /* featured caps don't describe format: skip them */
55     structure = gst_caps_get_structure (allowed_caps, i);
56     if (!gst_structure_has_field (structure, "format"))
57       continue;
58 
59     single_caps = gst_caps_new_empty ();
60     new_structure = gst_structure_copy (structure);
61     gst_structure_set (new_structure, "framerate", GST_TYPE_FRACTION,
62         1, 1, NULL);
63     gst_structure_remove_field (new_structure, "width");
64     gst_structure_remove_field (new_structure, "height");
65     gst_caps_append_structure (single_caps, new_structure);
66 
67     GST_DEBUG ("have caps %" GST_PTR_FORMAT, single_caps);
68     /* should be fixed without width/height */
69     fail_unless (gst_caps_is_fixed (single_caps));
70 
71     list = g_list_prepend (list, single_caps);
72   }
73 
74   gst_caps_unref (allowed_caps);
75   gst_object_unref (srcpad);
76 
77   return list;
78 }
79 
GST_START_TEST(test_unit_sizes)80 GST_START_TEST (test_unit_sizes)
81 {
82   GstBaseTransformClass *csp_klass, *vcrop_klass;
83   GstElement *videocrop, *csp;
84   GList *caps_list, *l;
85 
86   videocrop = gst_element_factory_make ("videocrop", "videocrop");
87   fail_unless (videocrop != NULL, "Failed to create videocrop element");
88   vcrop_klass = GST_BASE_TRANSFORM_GET_CLASS (videocrop);
89 
90   csp = gst_element_factory_make ("videoconvert", "csp");
91   fail_unless (csp != NULL, "Failed to create videoconvert element");
92   csp_klass = GST_BASE_TRANSFORM_GET_CLASS (csp);
93 
94   caps_list = video_crop_get_test_caps (videocrop);
95 
96   for (l = caps_list; l != NULL; l = l->next) {
97     const struct
98     {
99       gint width, height;
100     } sizes_to_try[] = {
101       {
102       160, 120}, {
103       161, 120}, {
104       160, 121}, {
105       161, 121}, {
106       159, 120}, {
107       160, 119}, {
108       159, 119}, {
109       159, 121}
110     };
111     GstStructure *s;
112     GstCaps *caps;
113     gint i;
114 
115     caps = gst_caps_copy (GST_CAPS (l->data));
116     s = gst_caps_get_structure (caps, 0);
117     fail_unless (s != NULL);
118 
119     for (i = 0; i < G_N_ELEMENTS (sizes_to_try); ++i) {
120       gchar *caps_str;
121       gsize csp_size = 0;
122       gsize vc_size = 0;
123 
124       gst_structure_set (s, "width", G_TYPE_INT, sizes_to_try[i].width,
125           "height", G_TYPE_INT, sizes_to_try[i].height, NULL);
126 
127       caps_str = gst_caps_to_string (caps);
128       GST_INFO ("Testing unit size for %s", caps_str);
129 
130       /* skip if videoconvert doesn't support these caps
131        * (only works with gst-plugins-base 0.10.9.1 or later) */
132       if (!csp_klass->get_unit_size ((GstBaseTransform *) csp, caps, &csp_size)) {
133         GST_INFO ("videoconvert does not support format %s", caps_str);
134         g_free (caps_str);
135         continue;
136       }
137 
138       fail_unless (vcrop_klass->get_unit_size ((GstBaseTransform *) videocrop,
139               caps, &vc_size));
140 
141       fail_unless (vc_size == csp_size,
142           "videocrop and videoconvert return different unit sizes for "
143           "caps %s: vc_size=%d, csp_size=%d", caps_str, vc_size, csp_size);
144 
145       g_free (caps_str);
146     }
147 
148     gst_caps_unref (caps);
149   }
150 
151   g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
152   g_list_free (caps_list);
153 
154   gst_object_unref (csp);
155   gst_object_unref (videocrop);
156 }
157 
158 GST_END_TEST;
159 
160 typedef struct
161 {
162   GstElement *pipeline;
163   GstElement *src;
164   GstElement *filter;
165   GstElement *crop;
166   GstElement *filter2;
167   GstElement *sink;
168   GstBuffer *last_buf;
169   GstCaps *last_caps;
170 } GstVideoCropTestContext;
171 
172 static void
handoff_cb(GstElement * sink,GstBuffer * buf,GstPad * pad,GstVideoCropTestContext * ctx)173 handoff_cb (GstElement * sink, GstBuffer * buf, GstPad * pad,
174     GstVideoCropTestContext * ctx)
175 {
176   GstCaps *caps;
177 
178   gst_buffer_replace (&ctx->last_buf, buf);
179   caps = gst_pad_get_current_caps (pad);
180   gst_caps_replace (&ctx->last_caps, caps);
181   gst_caps_unref (caps);
182 }
183 
184 static void
videocrop_test_cropping_init_context_full(GstVideoCropTestContext * ctx,gboolean featured)185 videocrop_test_cropping_init_context_full (GstVideoCropTestContext * ctx,
186     gboolean featured)
187 {
188   fail_unless (ctx != NULL);
189 
190   ctx->pipeline = gst_pipeline_new ("pipeline");
191   fail_unless (ctx->pipeline != NULL);
192 
193   if (featured)
194     ctx->src = gst_element_factory_make ("fakesrc", "src");
195   else
196     ctx->src = gst_element_factory_make ("videotestsrc", "src");
197   fail_unless (ctx->src != NULL, "Failed to create videotestsrc element");
198   ctx->filter = gst_element_factory_make ("capsfilter", "filter");
199   fail_unless (ctx->filter != NULL, "Failed to create capsfilter element");
200   ctx->crop = gst_element_factory_make ("videocrop", "crop");
201   fail_unless (ctx->crop != NULL, "Failed to create videocrop element");
202   ctx->filter2 = gst_element_factory_make ("capsfilter", "filter2");
203   fail_unless (ctx->filter2 != NULL,
204       "Failed to create second capsfilter element");
205   ctx->sink = gst_element_factory_make ("fakesink", "sink");
206   fail_unless (ctx->sink != NULL, "Failed to create fakesink element");
207 
208   gst_bin_add_many (GST_BIN (ctx->pipeline), ctx->src, ctx->filter,
209       ctx->crop, ctx->filter2, ctx->sink, NULL);
210   gst_element_link_many (ctx->src, ctx->filter, ctx->crop, ctx->filter2,
211       ctx->sink, NULL);
212 
213   if (featured) {
214     g_object_set (ctx->src, "format", GST_FORMAT_TIME, NULL);
215   } else {
216     /* set pattern to 'red' - for our purposes it doesn't matter anyway */
217     g_object_set (ctx->src, "pattern", 4, NULL);
218   }
219 
220   if (!featured) {
221     g_object_set (ctx->sink, "signal-handoffs", TRUE, NULL);
222     g_signal_connect (ctx->sink, "preroll-handoff", G_CALLBACK (handoff_cb),
223         ctx);
224   }
225 
226   ctx->last_buf = NULL;
227   ctx->last_caps = NULL;
228 
229   GST_LOG ("context inited");
230 }
231 
232 static void
videocrop_test_cropping_init_context(GstVideoCropTestContext * ctx)233 videocrop_test_cropping_init_context (GstVideoCropTestContext * ctx)
234 {
235   videocrop_test_cropping_init_context_full (ctx, FALSE);
236 }
237 
238 static void
videocrop_test_cropping_deinit_context(GstVideoCropTestContext * ctx)239 videocrop_test_cropping_deinit_context (GstVideoCropTestContext * ctx)
240 {
241   GST_LOG ("deiniting context");
242 
243   gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
244   gst_object_unref (ctx->pipeline);
245   gst_buffer_replace (&ctx->last_buf, NULL);
246   gst_caps_replace (&ctx->last_caps, NULL);
247   memset (ctx, 0x00, sizeof (GstVideoCropTestContext));
248 }
249 
250 typedef void (*GstVideoCropTestBufferFunc) (GstBuffer * buffer, GstCaps * caps);
251 
252 static void
videocrop_test_cropping(GstVideoCropTestContext * ctx,GstCaps * in_caps,GstCaps * out_caps,gint left,gint right,gint top,gint bottom,GstVideoCropTestBufferFunc func)253 videocrop_test_cropping (GstVideoCropTestContext * ctx, GstCaps * in_caps,
254     GstCaps * out_caps, gint left, gint right, gint top, gint bottom,
255     GstVideoCropTestBufferFunc func)
256 {
257   GST_LOG ("lrtb = %03u %03u %03u %03u, in_caps = %" GST_PTR_FORMAT
258       ", out_caps = %" GST_PTR_FORMAT, left, right, top, bottom, in_caps,
259       out_caps);
260 
261   g_object_set (ctx->filter, "caps", in_caps, NULL);
262   g_object_set (ctx->filter2, "caps", out_caps, NULL);
263 
264   g_object_set (ctx->crop, "left", left, "right", right, "top", top,
265       "bottom", bottom, NULL);
266 
267   /* this will fail if videotestsrc doesn't support our format; we need
268    * videotestsrc from -base CVS 0.10.9.1 with RGBA and AYUV support */
269   fail_unless (gst_element_set_state (ctx->pipeline,
270           GST_STATE_PAUSED) != GST_STATE_CHANGE_FAILURE);
271   fail_unless (gst_element_get_state (ctx->pipeline, NULL, NULL,
272           -1) == GST_STATE_CHANGE_SUCCESS);
273 
274   if (func != NULL) {
275     func (ctx->last_buf, ctx->last_caps);
276   }
277 
278   gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
279 }
280 
281 static void
check_1x1_buffer(GstBuffer * buf,GstCaps * caps)282 check_1x1_buffer (GstBuffer * buf, GstCaps * caps)
283 {
284   GstVideoInfo info;
285   GstVideoFrame frame;
286   /* the exact values we check for come from videotestsrc */
287   static const guint yuv_values[] = { 81, 90, 240, 255 };
288   static const guint rgb_values[] = { 0xff, 0, 0, 255 };
289   static const guint gray8_values[] = { 0x51 };
290   static const guint gray16_values[] = { 0x5151 };
291   const guint *values;
292   guint i;
293   const GstVideoFormatInfo *finfo;
294 
295   fail_unless (buf != NULL);
296   fail_unless (caps != NULL);
297 
298   fail_unless (gst_video_info_from_caps (&info, caps));
299   fail_unless (gst_video_frame_map (&frame, &info, buf, GST_MAP_READ));
300 
301   finfo = info.finfo;
302 
303   if (GST_VIDEO_INFO_IS_YUV (&info))
304     values = yuv_values;
305   else if (GST_VIDEO_INFO_IS_GRAY (&info))
306     if (GST_VIDEO_FORMAT_INFO_BITS (finfo) == 8)
307       values = gray8_values;
308     else
309       values = gray16_values;
310   else
311     values = rgb_values;
312 
313   GST_MEMDUMP ("buffer", GST_VIDEO_FRAME_PLANE_DATA (&frame, 0), 8);
314 
315   for (i = 0; i < GST_VIDEO_FRAME_N_COMPONENTS (&frame); i++) {
316     guint8 *data = GST_VIDEO_FRAME_COMP_DATA (&frame, i);
317 
318     GST_DEBUG ("W: %d", GST_VIDEO_FORMAT_INFO_W_SUB (finfo, i));
319     GST_DEBUG ("H: %d", GST_VIDEO_FORMAT_INFO_H_SUB (finfo, i));
320 
321     if (GST_VIDEO_FORMAT_INFO_W_SUB (finfo,
322             i) >= GST_VIDEO_FRAME_WIDTH (&frame))
323       continue;
324     if (GST_VIDEO_FORMAT_INFO_H_SUB (finfo,
325             i) >= GST_VIDEO_FRAME_HEIGHT (&frame))
326       continue;
327 
328     if (GST_VIDEO_FORMAT_INFO_BITS (finfo) == 8) {
329       fail_unless_equals_int (data[0], values[i]);
330     } else if (GST_VIDEO_FORMAT_INFO_BITS (finfo) == 16) {
331       guint16 pixels, val;
332       gint depth;
333 
334       if (GST_VIDEO_FORMAT_INFO_IS_LE (finfo))
335         pixels = GST_READ_UINT16_LE (data);
336       else
337         pixels = GST_READ_UINT16_BE (data);
338 
339       depth = GST_VIDEO_FORMAT_INFO_DEPTH (finfo, i);
340       val = pixels >> GST_VIDEO_FORMAT_INFO_SHIFT (finfo, i);
341       val = val & ((1 << depth) - 1);
342 
343       GST_DEBUG ("val %08x %d : %d", pixels, i, val);
344       if (depth <= 8) {
345         fail_unless_equals_int (val, values[i] >> (8 - depth));
346       } else {
347         fail_unless_equals_int (val, values[i] >> (16 - depth));
348       }
349     } else {
350     }
351   }
352 
353   gst_video_frame_unmap (&frame);
354 
355   /*
356      fail_unless_equals_int ((pixel & rmask) >> rshift, 0xff);
357      fail_unless_equals_int ((pixel & gmask) >> gshift, 0x00);
358      fail_unless_equals_int ((pixel & bmask) >> bshift, 0x00);
359    */
360 }
361 
GST_START_TEST(test_crop_to_1x1)362 GST_START_TEST (test_crop_to_1x1)
363 {
364   GstVideoCropTestContext ctx;
365   GList *caps_list, *node;
366 
367   videocrop_test_cropping_init_context (&ctx);
368 
369   caps_list = video_crop_get_test_caps (ctx.crop);
370 
371   for (node = caps_list; node != NULL; node = node->next) {
372     GstStructure *s;
373     GstCaps *caps;
374 
375     caps = gst_caps_copy (GST_CAPS (node->data));
376     s = gst_caps_get_structure (caps, 0);
377     fail_unless (s != NULL);
378 
379     GST_INFO ("testing format: %" GST_PTR_FORMAT, caps);
380 
381     gst_structure_set (s, "width", G_TYPE_INT, 160,
382         "height", G_TYPE_INT, 160, NULL);
383 
384     videocrop_test_cropping (&ctx, caps, NULL, 159, 0, 159, 0,
385         check_1x1_buffer);
386     /* commented out because they don't really add anything useful check-wise:
387        videocrop_test_cropping (&ctx, caps, NULL, 0, 159, 0, 159, check_1x1_buffer);
388        videocrop_test_cropping (&ctx, caps, NULL, 159, 0, 0, 159, check_1x1_buffer);
389        videocrop_test_cropping (&ctx, caps, NULL, 0, 159, 159, 0, check_1x1_buffer);
390      */
391     gst_caps_unref (caps);
392   }
393   g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
394   g_list_free (caps_list);
395 
396   videocrop_test_cropping_deinit_context (&ctx);
397 }
398 
399 GST_END_TEST;
400 
GST_START_TEST(test_cropping)401 GST_START_TEST (test_cropping)
402 {
403   GstVideoCropTestContext ctx;
404   struct
405   {
406     gint width, height;
407   } sizes_to_try[] = {
408     {
409     160, 160}, {
410     161, 160}, {
411     160, 161}, {
412     161, 161}, {
413     159, 160}, {
414     160, 159}, {
415     159, 159}, {
416     159, 161}
417   };
418   GList *caps_list, *node;
419   gint i;
420 
421   videocrop_test_cropping_init_context (&ctx);
422 
423   caps_list = video_crop_get_test_caps (ctx.crop);
424   node = g_list_nth (caps_list, __i__);
425 
426   if (node != NULL) {
427     GstStructure *s;
428     GstCaps *caps;
429 
430     caps = gst_caps_copy (GST_CAPS (node->data));
431     s = gst_caps_get_structure (caps, 0);
432     fail_unless (s != NULL);
433 
434     GST_INFO ("testing format: %" GST_PTR_FORMAT, caps);
435 
436     for (i = 0; i < G_N_ELEMENTS (sizes_to_try); ++i) {
437       GstCaps *in_caps, *out_caps;
438 
439       GST_INFO (" - %d x %d", sizes_to_try[i].width, sizes_to_try[i].height);
440 
441       gst_structure_set (s, "width", G_TYPE_INT, sizes_to_try[i].width,
442           "height", G_TYPE_INT, sizes_to_try[i].height, NULL);
443       in_caps = gst_caps_copy (caps);
444 
445       gst_structure_set (s, "width", G_TYPE_INT, 1, "height", G_TYPE_INT, 1,
446           NULL);
447       out_caps = gst_caps_copy (caps);
448 
449       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 0, 0, 0, NULL);
450       videocrop_test_cropping (&ctx, in_caps, NULL, 1, 0, 0, 0, NULL);
451       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 1, 0, 0, NULL);
452       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 0, 1, 0, NULL);
453       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 0, 0, 1, NULL);
454       videocrop_test_cropping (&ctx, in_caps, NULL, 63, 0, 0, 0, NULL);
455       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 63, 0, 0, NULL);
456       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 0, 63, 0, NULL);
457       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 0, 0, 63, NULL);
458       videocrop_test_cropping (&ctx, in_caps, NULL, 63, 0, 0, 1, NULL);
459       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 63, 1, 0, NULL);
460       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 1, 63, 0, NULL);
461       videocrop_test_cropping (&ctx, in_caps, NULL, 1, 0, 0, 63, NULL);
462       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 0, 0, 0, NULL);
463       videocrop_test_cropping (&ctx, in_caps, NULL, 32, 0, 0, 128, NULL);
464       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 32, 128, 0, NULL);
465       videocrop_test_cropping (&ctx, in_caps, NULL, 0, 128, 32, 0, NULL);
466       videocrop_test_cropping (&ctx, in_caps, NULL, 128, 0, 0, 32, NULL);
467       videocrop_test_cropping (&ctx, in_caps, NULL, 1, 1, 1, 1, NULL);
468       videocrop_test_cropping (&ctx, in_caps, NULL, 63, 63, 63, 63, NULL);
469       videocrop_test_cropping (&ctx, in_caps, NULL, 64, 64, 64, 64, NULL);
470 
471       /* Dynamic cropping */
472       videocrop_test_cropping (&ctx, in_caps, out_caps, -1, -1, -1, -1, NULL);
473       videocrop_test_cropping (&ctx, in_caps, out_caps, 0, -1, -1, -1, NULL);
474       videocrop_test_cropping (&ctx, in_caps, out_caps, -1, 0, -1, -1, NULL);
475       videocrop_test_cropping (&ctx, in_caps, out_caps, -1, -1, 0, -1, NULL);
476       videocrop_test_cropping (&ctx, in_caps, out_caps, -1, -1, -1, 0, NULL);
477       videocrop_test_cropping (&ctx, in_caps, out_caps, 10, -1, 10, -1, NULL);
478       videocrop_test_cropping (&ctx, in_caps, out_caps, -1, 10, -1, 10, NULL);
479       videocrop_test_cropping (&ctx, in_caps, out_caps,
480           sizes_to_try[i].width - 1, -1, -1, -1, NULL);
481       videocrop_test_cropping (&ctx, in_caps, out_caps, -1,
482           sizes_to_try[i].width - 1, -1, -1, NULL);
483       videocrop_test_cropping (&ctx, in_caps, out_caps, -1, -1,
484           sizes_to_try[i].height - 1, -1, NULL);
485       videocrop_test_cropping (&ctx, in_caps, out_caps, -1, -1, -1,
486           sizes_to_try[i].height - 1, NULL);
487 
488       gst_caps_unref (in_caps);
489       gst_caps_unref (out_caps);
490     }
491 
492     gst_caps_unref (caps);
493   } else {
494     GST_INFO ("no caps #%d", __i__);
495   }
496   g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
497   g_list_free (caps_list);
498 
499   videocrop_test_cropping_deinit_context (&ctx);
500 }
501 
502 GST_END_TEST;
503 
504 
505 static GstPadProbeReturn
buffer_probe_cb(GstPad * pad,GstPadProbeInfo * info,gpointer data)506 buffer_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer data)
507 {
508   GstBuffer **p_buf = data;
509   GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
510 
511   gst_buffer_replace (p_buf, buf);
512 
513   return GST_PAD_PROBE_OK;      /* keep data */
514 }
515 
GST_START_TEST(test_passthrough)516 GST_START_TEST (test_passthrough)
517 {
518   GstStateChangeReturn state_ret;
519   GstVideoCropTestContext ctx;
520   GstPad *srcpad;
521   GstBuffer *gen_buf = NULL;    /* buffer generated by videotestsrc */
522 
523   videocrop_test_cropping_init_context (&ctx);
524 
525   g_object_set (ctx.src, "num-buffers", 1, NULL);
526 
527   srcpad = gst_element_get_static_pad (ctx.src, "src");
528   fail_unless (srcpad != NULL);
529   gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BUFFER, buffer_probe_cb,
530       &gen_buf, NULL);
531   gst_object_unref (srcpad);
532 
533   g_object_set (ctx.crop, "left", 0, "right", 0, "top", 0, "bottom", 0, NULL);
534 
535   state_ret = gst_element_set_state (ctx.pipeline, GST_STATE_PAUSED);
536   fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
537       "couldn't set pipeline to PAUSED state");
538 
539   state_ret = gst_element_get_state (ctx.pipeline, NULL, NULL, -1);
540   fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
541       "pipeline failed to go to PAUSED state");
542 
543   fail_unless (gen_buf != NULL);
544   fail_unless (ctx.last_buf != NULL);
545 
546   /* pass through should do nothing */
547   fail_unless (gen_buf == ctx.last_buf);
548 
549   videocrop_test_cropping_deinit_context (&ctx);
550 
551   fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT_VALUE (gen_buf), 1);
552   gst_buffer_unref (gen_buf);
553 }
554 
555 GST_END_TEST;
556 
557 static gint
notgst_value_list_get_nth_int(const GValue * list_val,guint n)558 notgst_value_list_get_nth_int (const GValue * list_val, guint n)
559 {
560   const GValue *v;
561 
562   fail_unless (GST_VALUE_HOLDS_LIST (list_val));
563   fail_unless (n < gst_value_list_get_size (list_val));
564 
565   v = gst_value_list_get_value (list_val, n);
566   fail_unless (G_VALUE_HOLDS_INT (v));
567   return g_value_get_int (v);
568 }
569 
GST_START_TEST(test_caps_transform)570 GST_START_TEST (test_caps_transform)
571 {
572   GstVideoCropTestContext ctx;
573   GstBaseTransformClass *klass;
574   GstBaseTransform *crop;
575   const GValue *w_val;
576   const GValue *h_val;
577   GstCaps *caps, *adj_caps;
578 
579   videocrop_test_cropping_init_context (&ctx);
580 
581   crop = GST_BASE_TRANSFORM (ctx.crop);
582   klass = GST_BASE_TRANSFORM_GET_CLASS (ctx.crop);
583   fail_unless (klass != NULL);
584 
585   caps = gst_caps_new_simple ("video/x-raw",
586       "format", G_TYPE_STRING, "I420",
587       "framerate", GST_TYPE_FRACTION, 1, 1,
588       "width", G_TYPE_INT, 200, "height", G_TYPE_INT, 100, NULL);
589 
590   /* by default, it should be no cropping and hence passthrough */
591   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
592   fail_unless (adj_caps != NULL);
593   fail_unless (gst_caps_is_equal (adj_caps, caps));
594   gst_caps_unref (adj_caps);
595 
596   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
597   fail_unless (adj_caps != NULL);
598   fail_unless (gst_caps_is_equal (adj_caps, caps));
599   gst_caps_unref (adj_caps);
600 
601   /* make sure that's still true after changing properties back and forth */
602   g_object_set (ctx.crop, "left", 1, "right", 3, "top", 5, "bottom", 7, NULL);
603   g_object_set (ctx.crop, "left", 0, "right", 0, "top", 0, "bottom", 0, NULL);
604 
605   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
606   fail_unless (adj_caps != NULL);
607   fail_unless (gst_caps_is_equal (adj_caps, caps));
608   gst_caps_unref (adj_caps);
609 
610   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
611   fail_unless (adj_caps != NULL);
612   fail_unless (gst_caps_is_equal (adj_caps, caps));
613   gst_caps_unref (adj_caps);
614 
615   /* now check adjustments made ... */
616   g_object_set (ctx.crop, "left", 1, "right", 3, "top", 5, "bottom", 7, NULL);
617 
618   /* ========= (1) fixed value ============================================= */
619 
620   /* sink => source, source must be bigger if we crop stuff off */
621   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
622   fail_unless (adj_caps != NULL);
623   fail_unless (gst_caps_get_size (adj_caps) == 1);
624   w_val =
625       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
626   fail_unless (w_val != NULL);
627   fail_unless (G_VALUE_HOLDS_INT (w_val));
628   fail_unless_equals_int (g_value_get_int (w_val), 200 + (1 + 3));
629   h_val =
630       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
631   fail_unless (h_val != NULL);
632   fail_unless (G_VALUE_HOLDS_INT (h_val));
633   fail_unless_equals_int (g_value_get_int (h_val), 100 + (5 + 7));
634   gst_caps_unref (adj_caps);
635 
636   /* source => sink becomes smaller */
637   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
638   fail_unless (adj_caps != NULL);
639   fail_unless (gst_caps_get_size (adj_caps) == 1);
640   w_val =
641       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
642   fail_unless (w_val != NULL);
643   fail_unless (G_VALUE_HOLDS_INT (w_val));
644   fail_unless_equals_int (g_value_get_int (w_val), 200 - (1 + 3));
645   h_val =
646       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
647   fail_unless (h_val != NULL);
648   fail_unless (G_VALUE_HOLDS_INT (h_val));
649   fail_unless_equals_int (g_value_get_int (h_val), 100 - (5 + 7));
650   gst_caps_unref (adj_caps);
651 
652   /* ========= (2) range (simple adjustment) =============================== */
653 
654   gst_structure_set (gst_caps_get_structure (caps, 0),
655       "width", GST_TYPE_INT_RANGE, 1000, 2000,
656       "height", GST_TYPE_INT_RANGE, 3000, 4000, NULL);
657 
658   /* sink => source, source must be bigger if we crop stuff off */
659   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
660   fail_unless (adj_caps != NULL);
661   fail_unless (gst_caps_get_size (adj_caps) == 1);
662   w_val =
663       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
664   fail_unless (w_val != NULL);
665   fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
666   fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1000 + (1 + 3));
667   fail_unless_equals_int (gst_value_get_int_range_max (w_val), 2000 + (1 + 3));
668   h_val =
669       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
670   fail_unless (h_val != NULL);
671   fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
672   fail_unless_equals_int (gst_value_get_int_range_min (h_val), 3000 + (5 + 7));
673   fail_unless_equals_int (gst_value_get_int_range_max (h_val), 4000 + (5 + 7));
674   gst_caps_unref (adj_caps);
675 
676   /* source => sink becomes smaller */
677   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
678   fail_unless (adj_caps != NULL);
679   fail_unless (gst_caps_get_size (adj_caps) == 1);
680   w_val =
681       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
682   fail_unless (w_val != NULL);
683   fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
684   fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1000 - (1 + 3));
685   fail_unless_equals_int (gst_value_get_int_range_max (w_val), 2000 - (1 + 3));
686   h_val =
687       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
688   fail_unless (h_val != NULL);
689   fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
690   fail_unless_equals_int (gst_value_get_int_range_min (h_val), 3000 - (5 + 7));
691   fail_unless_equals_int (gst_value_get_int_range_max (h_val), 4000 - (5 + 7));
692   gst_caps_unref (adj_caps);
693 
694   /* ========= (3) range (adjustment at boundary) ========================== */
695 
696   gst_structure_set (gst_caps_get_structure (caps, 0),
697       "width", GST_TYPE_INT_RANGE, 2, G_MAXINT,
698       "height", GST_TYPE_INT_RANGE, 2, G_MAXINT, NULL);
699 
700   /* sink => source, source must be bigger if we crop stuff off */
701   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
702   fail_unless (adj_caps != NULL);
703   fail_unless (gst_caps_get_size (adj_caps) == 1);
704   w_val =
705       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
706   fail_unless (w_val != NULL);
707   fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
708   fail_unless_equals_int (gst_value_get_int_range_min (w_val), 2 + (1 + 3));
709   fail_unless_equals_int (gst_value_get_int_range_max (w_val), G_MAXINT);
710   h_val =
711       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
712   fail_unless (h_val != NULL);
713   fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
714   fail_unless_equals_int (gst_value_get_int_range_min (h_val), 2 + (5 + 7));
715   fail_unless_equals_int (gst_value_get_int_range_max (h_val), G_MAXINT);
716   gst_caps_unref (adj_caps);
717 
718   /* source => sink becomes smaller */
719   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
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_INT_RANGE (w_val));
726   fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1);
727   fail_unless_equals_int (gst_value_get_int_range_max (w_val),
728       G_MAXINT - (1 + 3));
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_INT_RANGE (h_val));
733   fail_unless_equals_int (gst_value_get_int_range_min (h_val), 1);
734   fail_unless_equals_int (gst_value_get_int_range_max (h_val),
735       G_MAXINT - (5 + 7));
736   gst_caps_unref (adj_caps);
737 
738   /* ========= (4) list of values ========================================== */
739 
740   {
741     GValue list = { 0, };
742     GValue ival = { 0, };
743 
744     g_value_init (&ival, G_TYPE_INT);
745     g_value_init (&list, GST_TYPE_LIST);
746     g_value_set_int (&ival, 2);
747     gst_value_list_append_value (&list, &ival);
748     g_value_set_int (&ival, G_MAXINT);
749     gst_value_list_append_value (&list, &ival);
750     gst_structure_set_value (gst_caps_get_structure (caps, 0), "width", &list);
751     g_value_unset (&list);
752     g_value_unset (&ival);
753 
754     g_value_init (&ival, G_TYPE_INT);
755     g_value_init (&list, GST_TYPE_LIST);
756     g_value_set_int (&ival, 5);
757     gst_value_list_append_value (&list, &ival);
758     g_value_set_int (&ival, 1000);
759     gst_value_list_append_value (&list, &ival);
760     gst_structure_set_value (gst_caps_get_structure (caps, 0), "height", &list);
761     g_value_unset (&list);
762     g_value_unset (&ival);
763   }
764 
765   /* sink => source, source must be bigger if we crop stuff off */
766   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
767   fail_unless (adj_caps != NULL);
768   fail_unless (gst_caps_get_size (adj_caps) == 1);
769   w_val =
770       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
771   fail_unless (w_val != NULL);
772   fail_unless (GST_VALUE_HOLDS_LIST (w_val));
773   fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 0),
774       2 + (1 + 3));
775   fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 1), G_MAXINT);
776   h_val =
777       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
778   fail_unless (h_val != NULL);
779   fail_unless (GST_VALUE_HOLDS_LIST (h_val));
780   fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 0),
781       5 + (5 + 7));
782   fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 1),
783       1000 + (5 + 7));
784   gst_caps_unref (adj_caps);
785 
786   /* source => sink becomes smaller */
787   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
788   fail_unless (adj_caps != NULL);
789   fail_unless (gst_caps_get_size (adj_caps) == 1);
790   w_val =
791       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
792   fail_unless (w_val != NULL);
793   fail_unless (GST_VALUE_HOLDS_LIST (w_val));
794   fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 0), 1);
795   fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 1),
796       G_MAXINT - (1 + 3));
797   h_val =
798       gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
799   fail_unless (h_val != NULL);
800   fail_unless (GST_VALUE_HOLDS_LIST (h_val));
801   fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 0), 1);
802   fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 1),
803       1000 - (5 + 7));
804   gst_caps_unref (adj_caps);
805 
806   gst_caps_unref (caps);
807   videocrop_test_cropping_deinit_context (&ctx);
808 }
809 
810 GST_END_TEST;
811 
812 static GstCaps *
get_featured_caps(void)813 get_featured_caps (void)
814 {
815   GstCaps *caps;
816   GstCapsFeatures *features;
817 
818   features = gst_caps_features_new ("memory:DMABuf", NULL);
819   caps = gst_caps_new_simple ("video/x-raw",
820       "format", G_TYPE_STRING, "NV12",
821       "framerate", GST_TYPE_FRACTION, 1, 1,
822       "width", G_TYPE_INT, 200, "height", G_TYPE_INT, 100, NULL);
823   gst_caps_set_features_simple (caps, features);
824 
825   return caps;
826 }
827 
GST_START_TEST(test_caps_transform_featured)828 GST_START_TEST (test_caps_transform_featured)
829 {
830   GstVideoCropTestContext ctx;
831   GstBaseTransformClass *klass;
832   GstBaseTransform *crop;
833   GstCaps *caps, *adj_caps;
834 
835   videocrop_test_cropping_init_context (&ctx);
836 
837   caps = get_featured_caps ();
838 
839   crop = GST_BASE_TRANSFORM (ctx.crop);
840   klass = GST_BASE_TRANSFORM_GET_CLASS (ctx.crop);
841   fail_unless (klass != NULL);
842 
843   /* by default, it should be no cropping and hence passthrough */
844   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
845   fail_unless (adj_caps != NULL);
846 
847   fail_unless (gst_caps_is_equal (adj_caps, caps));
848   gst_caps_unref (adj_caps);
849 
850   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
851   fail_unless (adj_caps != NULL);
852   fail_unless (gst_caps_is_equal (adj_caps, caps));
853   gst_caps_unref (adj_caps);
854 
855   /* make sure that's still true after changing properties back and forth */
856   g_object_set (ctx.crop, "left", 1, "right", 3, "top", 5, "bottom", 7, NULL);
857   g_object_set (ctx.crop, "left", 0, "right", 0, "top", 0, "bottom", 0, NULL);
858 
859   adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps, NULL);
860   fail_unless (adj_caps != NULL);
861   fail_unless (gst_caps_is_equal (adj_caps, caps));
862   gst_caps_unref (adj_caps);
863 
864   adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps, NULL);
865   fail_unless (adj_caps != NULL);
866   fail_unless (gst_caps_is_equal (adj_caps, caps));
867   gst_caps_unref (adj_caps);
868 
869   gst_caps_unref (caps);
870   videocrop_test_cropping_deinit_context (&ctx);
871 }
872 
873 GST_END_TEST;
874 
GST_START_TEST(test_passthrough_featured)875 GST_START_TEST (test_passthrough_featured)
876 {
877   GstStateChangeReturn state_ret;
878   GstVideoCropTestContext ctx;
879   GstCaps *caps;
880 
881   videocrop_test_cropping_init_context_full (&ctx, TRUE);
882 
883   g_object_set (ctx.src, "num-buffers", 1, NULL);
884 
885   caps = get_featured_caps ();
886   g_object_set (ctx.filter, "caps", caps, NULL);
887   gst_caps_unref (caps);
888 
889   g_object_set (ctx.crop, "left", 50, "top", 10, NULL);
890 
891   state_ret = gst_element_set_state (ctx.pipeline, GST_STATE_PAUSED);
892   fail_unless (state_ret == GST_STATE_CHANGE_ASYNC,
893       "pipeline should fail on negotiation");
894 
895   videocrop_test_cropping_deinit_context (&ctx);
896 }
897 
898 GST_END_TEST;
899 
900 static Suite *
videocrop_suite(void)901 videocrop_suite (void)
902 {
903   Suite *s = suite_create ("videocrop");
904   TCase *tc_chain = tcase_create ("general");
905 
906 #ifdef HAVE_VALGRIND
907   if (RUNNING_ON_VALGRIND) {
908     /* our tests take quite a long time, so increase
909      * timeout (~25 minutes on my 1.6GHz AMD K7) */
910     tcase_set_timeout (tc_chain, 30 * 60);
911   } else
912 #endif
913   {
914     /* increase timeout, these tests take a long time (60 secs here) */
915     tcase_set_timeout (tc_chain, 2 * 60);
916   }
917 
918   suite_add_tcase (s, tc_chain);
919   tcase_add_test (tc_chain, test_crop_to_1x1);
920   tcase_add_test (tc_chain, test_caps_transform);
921   tcase_add_test (tc_chain, test_caps_transform_featured);
922   tcase_add_test (tc_chain, test_passthrough);
923   tcase_add_test (tc_chain, test_passthrough_featured);
924   tcase_add_test (tc_chain, test_unit_sizes);
925   tcase_add_loop_test (tc_chain, test_cropping, 0, 25);
926 
927   return s;
928 }
929 
930 GST_CHECK_MAIN (videocrop);
931