videocrop.c 26.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* GStreamer unit test for the videocrop element
 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_VALGRIND
# include <valgrind/valgrind.h>
#endif

#include <unistd.h>

#include <gst/check/gstcheck.h>
#include <gst/base/gstbasetransform.h>

/* return a list of caps where we only need to set
 * width and height to get fixed caps */
static GList *
video_crop_get_test_caps (GstElement * videocrop)
{
  const GstCaps *allowed_caps;
  GstPad *srcpad;
  GList *list = NULL;
  guint i;

  srcpad = gst_element_get_pad (videocrop, "src");
  fail_unless (srcpad != NULL);
  allowed_caps = gst_pad_get_pad_template_caps (srcpad);
  fail_unless (allowed_caps != NULL);

  for (i = 0; i < gst_caps_get_size (allowed_caps); ++i) {
    GstStructure *new_structure;
    GstCaps *single_caps;

    single_caps = gst_caps_new_empty ();
    new_structure =
        gst_structure_copy (gst_caps_get_structure (allowed_caps, i));
    gst_structure_set (new_structure, "framerate", GST_TYPE_FRACTION,
        1, 1, NULL);
    gst_structure_remove_field (new_structure, "width");
    gst_structure_remove_field (new_structure, "height");
    gst_caps_append_structure (single_caps, new_structure);

    /* should be fixed without width/height */
    fail_unless (gst_caps_is_fixed (single_caps));

    list = g_list_prepend (list, single_caps);
  }

  gst_object_unref (srcpad);

  return list;
}

GST_START_TEST (test_unit_sizes)
{
  GstBaseTransformClass *csp_klass, *vcrop_klass;
  GstElement *videocrop, *csp;
  GList *caps_list, *l;
  gint i;

  videocrop = gst_element_factory_make ("videocrop", "videocrop");
80
  fail_unless (videocrop != NULL, "Failed to create videocrop element");
81
82
83
  vcrop_klass = GST_BASE_TRANSFORM_GET_CLASS (videocrop);

  csp = gst_element_factory_make ("ffmpegcolorspace", "csp");
84
  fail_unless (csp != NULL, "Failed to create ffmpegcolorspace element");
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  csp_klass = GST_BASE_TRANSFORM_GET_CLASS (csp);

  caps_list = video_crop_get_test_caps (videocrop);

  for (l = caps_list; l != NULL; l = l->next) {
    const struct
    {
      gint width, height;
    } sizes_to_try[] = {
      {
      160, 120}, {
      161, 120}, {
      160, 121}, {
      161, 121}, {
      159, 120}, {
      160, 119}, {
      159, 119}, {
      159, 121}
    };
    GstStructure *s;
    GstCaps *caps;
    gint i;

    caps = gst_caps_copy (GST_CAPS (l->data));
    s = gst_caps_get_structure (caps, 0);
    fail_unless (s != NULL);

    for (i = 0; i < G_N_ELEMENTS (sizes_to_try); ++i) {
      gchar *caps_str;
      guint32 format = 0;
      guint csp_size = 0;
      guint vc_size = 0;

      gst_structure_set (s, "width", G_TYPE_INT, sizes_to_try[i].width,
          "height", G_TYPE_INT, sizes_to_try[i].height, NULL);

      caps_str = gst_caps_to_string (caps);
      GST_INFO ("Testing unit size for %s", caps_str);

      /* skip if ffmpegcolorspace doesn't support these caps
       * (only works with gst-plugins-base 0.10.9.1 or later) */
      if (!csp_klass->get_unit_size ((GstBaseTransform *) csp, caps, &csp_size)) {
        GST_INFO ("ffmpegcolorspace does not support format %s", caps_str);
        g_free (caps_str);
        continue;
      }

      fail_unless (vcrop_klass->get_unit_size ((GstBaseTransform *) videocrop,
              caps, &vc_size));

      fail_unless (vc_size == csp_size,
          "videocrop and ffmpegcolorspace return different unit sizes for "
          "caps %s: vc_size=%d, csp_size=%d", caps_str, vc_size, csp_size);

      g_free (caps_str);
    }

    gst_caps_unref (caps);
  }

  g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
  g_list_free (caps_list);

  gst_object_unref (csp);
  gst_object_unref (videocrop);
}

GST_END_TEST;

typedef struct
{
  GstElement *pipeline;
  GstElement *src;
  GstElement *filter;
  GstElement *crop;
  GstElement *sink;
161
  GstBuffer *last_buf;
162
163
} GstVideoCropTestContext;

164
165
166
167
168
169
170
static void
handoff_cb (GstElement * sink, GstBuffer * buf, GstPad * pad,
    GstBuffer ** p_buf)
{
  gst_buffer_replace (p_buf, buf);
}

171
172
173
174
175
176
177
178
static void
videocrop_test_cropping_init_context (GstVideoCropTestContext * ctx)
{
  fail_unless (ctx != NULL);

  ctx->pipeline = gst_pipeline_new ("pipeline");
  fail_unless (ctx->pipeline != NULL);
  ctx->src = gst_element_factory_make ("videotestsrc", "src");
179
  fail_unless (ctx->src != NULL, "Failed to create videotestsrc element");
180
  ctx->filter = gst_element_factory_make ("capsfilter", "filter");
181
  fail_unless (ctx->filter != NULL, "Failed to create capsfilter element");
182
  ctx->crop = gst_element_factory_make ("videocrop", "crop");
183
  fail_unless (ctx->crop != NULL, "Failed to create videocrop element");
184
  ctx->sink = gst_element_factory_make ("fakesink", "sink");
185
  fail_unless (ctx->sink != NULL, "Failed to create fakesink element");
186
187
188
189
190

  gst_bin_add_many (GST_BIN (ctx->pipeline), ctx->src, ctx->filter,
      ctx->crop, ctx->sink, NULL);
  gst_element_link_many (ctx->src, ctx->filter, ctx->crop, ctx->sink, NULL);

191
192
193
194
195
196
197
198
199
  /* set pattern to 'red' - for our purposes it doesn't matter anyway */
  g_object_set (ctx->src, "pattern", 4, NULL);

  g_object_set (ctx->sink, "signal-handoffs", TRUE, NULL);
  g_signal_connect (ctx->sink, "preroll-handoff", G_CALLBACK (handoff_cb),
      &ctx->last_buf);

  ctx->last_buf = NULL;

200
201
202
203
204
205
206
207
208
209
  GST_LOG ("context inited");
}

static void
videocrop_test_cropping_deinit_context (GstVideoCropTestContext * ctx)
{
  GST_LOG ("deiniting context");

  gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
  gst_object_unref (ctx->pipeline);
210
  gst_buffer_replace (&ctx->last_buf, NULL);
211
212
  memset (ctx, 0x00, sizeof (GstVideoCropTestContext));
}
213
typedef void (*GstVideoCropTestBufferFunc) (GstBuffer * buffer);
214
215
216

static void
videocrop_test_cropping (GstVideoCropTestContext * ctx, GstCaps * in_caps,
217
218
    gint left, gint right, gint top, gint bottom,
    GstVideoCropTestBufferFunc func)
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
{
  GST_LOG ("lrtb = %03u %03u %03u %03u, caps = %" GST_PTR_FORMAT, left, right,
      top, bottom, in_caps);

  g_object_set (ctx->filter, "caps", in_caps, NULL);

  g_object_set (ctx->crop, "left", left, "right", right, "top", top,
      "bottom", bottom, NULL);

  /* this will fail if videotestsrc doesn't support our format; we need
   * videotestsrc from -base CVS 0.10.9.1 with RGBA and AYUV support */
  fail_unless (gst_element_set_state (ctx->pipeline,
          GST_STATE_PAUSED) != GST_STATE_CHANGE_FAILURE);
  fail_unless (gst_element_get_state (ctx->pipeline, NULL, NULL,
          -1) == GST_STATE_CHANGE_SUCCESS);

235
236
237
238
  if (func != NULL) {
    func (ctx->last_buf);
  }

239
240
241
  gst_element_set_state (ctx->pipeline, GST_STATE_NULL);
}

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
static void
check_1x1_buffer (GstBuffer * buf)
{
  GstStructure *s;

  fail_unless (buf != NULL);
  fail_unless (GST_BUFFER_CAPS (buf) != NULL);

  s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
  if (gst_structure_has_name (s, "video/x-raw-yuv")) {
    guint32 format = 0;

    fail_unless (gst_structure_get_fourcc (s, "format", &format));

    /* the exact values we check for come from videotestsrc */
    switch (format) {
      case GST_MAKE_FOURCC ('I', '4', '2', '0'):
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[0], 76);
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[8], 85);
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[12], 255);
        break;
      case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[0], 76);
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[8], 255);
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[12], 85);
        break;
      case GST_MAKE_FOURCC ('Y', '8', '0', '0'):
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[0], 76);
        /* no chroma planes */
        break;
      case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[1], 76);
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[2], 85);
        fail_unless_equals_int (GST_BUFFER_DATA (buf)[3], 255);
        /* no chroma planes */
        break;
      default:
        GST_LOG ("not checking %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (format));
        break;
    }
  } else if (gst_structure_has_name (s, "video/x-raw-rgb")) {
    guint32 pixel;
    gint rmask = 0, bmask = 0, gmask = 0, endianness = 0, bpp = 0;
    gint rshift, gshift, bshift;

    fail_unless (gst_structure_get_int (s, "red_mask", &rmask));
    fail_unless (gst_structure_get_int (s, "blue_mask", &bmask));
    fail_unless (gst_structure_get_int (s, "green_mask", &gmask));
    fail_unless (gst_structure_get_int (s, "bpp", &bpp));
    fail_unless (gst_structure_get_int (s, "endianness", &endianness));

    fail_unless (rmask != 0);
    fail_unless (gmask != 0);
    fail_unless (bmask != 0);
    fail_unless (bpp != 0);
    fail_unless (endianness != 0);

    rshift = g_bit_nth_lsf (rmask, -1);
    gshift = g_bit_nth_lsf (gmask, -1);
    bshift = g_bit_nth_lsf (bmask, -1);

    switch (bpp) {
      case 32:{
        if (endianness == G_LITTLE_ENDIAN)
          pixel = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
        else
          pixel = GST_READ_UINT32_BE (GST_BUFFER_DATA (buf));
        break;
      }
      case 24:{
        if (endianness == G_BIG_ENDIAN) {
          pixel = (GST_READ_UINT8 (GST_BUFFER_DATA (buf)) << 16) |
              (GST_READ_UINT8 (GST_BUFFER_DATA (buf) + 1) << 8) |
              (GST_READ_UINT8 (GST_BUFFER_DATA (buf) + 2) << 0);
        } else {
          pixel = (GST_READ_UINT8 (GST_BUFFER_DATA (buf) + 2) << 16) |
              (GST_READ_UINT8 (GST_BUFFER_DATA (buf) + 1) << 8) |
              (GST_READ_UINT8 (GST_BUFFER_DATA (buf) + 0) << 0);
        }
        break;
      }
      default:{
        GST_LOG ("not checking RGB-format buffer with %ubpp", bpp);
        return;
      }
    }

    fail_unless_equals_int ((pixel & rmask) >> rshift, 0xff);
    fail_unless_equals_int ((pixel & gmask) >> gshift, 0x00);
    fail_unless_equals_int ((pixel & bmask) >> bshift, 0x00);
  }
}

GST_START_TEST (test_crop_to_1x1)
{
  GstVideoCropTestContext ctx;
  GList *caps_list, *node;

  videocrop_test_cropping_init_context (&ctx);

  caps_list = video_crop_get_test_caps (ctx.crop);

  for (node = caps_list; node != NULL; node = node->next) {
    GstStructure *s;
    GstCaps *caps;

    caps = gst_caps_copy (GST_CAPS (node->data));
    s = gst_caps_get_structure (caps, 0);
    fail_unless (s != NULL);

    GST_INFO ("testing format: %" GST_PTR_FORMAT, caps);

    gst_structure_set (s, "width", G_TYPE_INT, 160,
        "height", G_TYPE_INT, 160, NULL);

    videocrop_test_cropping (&ctx, caps, 159, 0, 159, 0, check_1x1_buffer);
    /* commented out because they don't really add anything useful check-wise:
       videocrop_test_cropping (&ctx, caps, 0, 159, 0, 159, check_1x1_buffer);
       videocrop_test_cropping (&ctx, caps, 159, 0, 0, 159, check_1x1_buffer);
       videocrop_test_cropping (&ctx, caps, 0, 159, 159, 0, check_1x1_buffer);
     */
    gst_caps_unref (caps);
  }
  g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
  g_list_free (caps_list);

  videocrop_test_cropping_deinit_context (&ctx);
}

GST_END_TEST;



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
GST_START_TEST (test_cropping)
{
  GstVideoCropTestContext ctx;
  struct
  {
    gint width, height;
  } sizes_to_try[] = {
    {
    160, 160}, {
    161, 160}, {
    160, 161}, {
    161, 161}, {
    159, 160}, {
    160, 159}, {
    159, 159}, {
    159, 161}
  };
  GList *caps_list, *node;
  gint i;

  videocrop_test_cropping_init_context (&ctx);

  caps_list = video_crop_get_test_caps (ctx.crop);

  for (node = caps_list; node != NULL; node = node->next) {
    GstStructure *s;
    GstCaps *caps;

    caps = gst_caps_copy (GST_CAPS (node->data));
    s = gst_caps_get_structure (caps, 0);
    fail_unless (s != NULL);

    GST_INFO ("testing format: %" GST_PTR_FORMAT, caps);

    for (i = 0; i < G_N_ELEMENTS (sizes_to_try); ++i) {
      gst_structure_set (s, "width", G_TYPE_INT, sizes_to_try[i].width,
          "height", G_TYPE_INT, sizes_to_try[i].height, NULL);

      GST_INFO (" - %d x %d", sizes_to_try[i].width, sizes_to_try[i].height);

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
      videocrop_test_cropping (&ctx, caps, 0, 0, 0, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 1, 0, 0, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 1, 0, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 0, 1, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 0, 0, 1, NULL);
      videocrop_test_cropping (&ctx, caps, 63, 0, 0, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 63, 0, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 0, 63, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 0, 0, 63, NULL);
      videocrop_test_cropping (&ctx, caps, 63, 0, 0, 1, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 63, 1, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 1, 63, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 1, 0, 0, 63, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 0, 0, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 32, 0, 0, 128, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 32, 128, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 0, 128, 32, 0, NULL);
      videocrop_test_cropping (&ctx, caps, 128, 0, 0, 32, NULL);
      videocrop_test_cropping (&ctx, caps, 1, 1, 1, 1, NULL);
      videocrop_test_cropping (&ctx, caps, 63, 63, 63, 63, NULL);
      videocrop_test_cropping (&ctx, caps, 64, 64, 64, 64, NULL);
436
    }
437
438

    gst_caps_unref (caps);
439
  }
440
441
  g_list_foreach (caps_list, (GFunc) gst_caps_unref, NULL);
  g_list_free (caps_list);
442
443
444
445
446
447

  videocrop_test_cropping_deinit_context (&ctx);
}

GST_END_TEST;

448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482

static gboolean
buffer_probe_cb (GstPad * pad, GstBuffer * buf, GstBuffer ** p_buf)
{
  gst_buffer_replace (p_buf, buf);
  return TRUE;                  /* keep data */
}

GST_START_TEST (test_passthrough)
{
  GstStateChangeReturn state_ret;
  GstVideoCropTestContext ctx;
  GstPad *srcpad;
  GstBuffer *gen_buf = NULL;    /* buffer generated by videotestsrc */

  videocrop_test_cropping_init_context (&ctx);

  g_object_set (ctx.src, "num-buffers", 1, NULL);

  srcpad = gst_element_get_pad (ctx.src, "src");
  fail_unless (srcpad != NULL);
  gst_pad_add_buffer_probe (srcpad, G_CALLBACK (buffer_probe_cb), &gen_buf);
  gst_object_unref (srcpad);

  g_object_set (ctx.crop, "left", 0, "right", 0, "top", 0, "bottom", 0, NULL);

  state_ret = gst_element_set_state (ctx.pipeline, GST_STATE_PAUSED);
  fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
      "couldn't set pipeline to PAUSED state");

  state_ret = gst_element_get_state (ctx.pipeline, NULL, NULL, -1);
  fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
      "pipeline failed to go to PAUSED state");

  fail_unless (gen_buf != NULL);
483
  fail_unless (ctx.last_buf != NULL);
484
485

  /* pass through should do nothing */
486
  fail_unless (gen_buf == ctx.last_buf);
487
488
489

  videocrop_test_cropping_deinit_context (&ctx);

490
  fail_unless_equals_int (GST_MINI_OBJECT_REFCOUNT_VALUE (gen_buf), 1);
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
  gst_buffer_unref (gen_buf);
}

GST_END_TEST;

static gint
notgst_value_list_get_nth_int (const GValue * list_val, guint n)
{
  const GValue *v;

  fail_unless (GST_VALUE_HOLDS_LIST (list_val));
  fail_unless (n < gst_value_list_get_size (list_val));

  v = gst_value_list_get_value (list_val, n);
  fail_unless (G_VALUE_HOLDS_INT (v));
  return g_value_get_int (v);
}

GST_START_TEST (test_caps_transform)
{
  GstVideoCropTestContext ctx;
  GstBaseTransformClass *klass;
  GstBaseTransform *crop;
  const GValue *w_val;
  const GValue *h_val;
  GstCaps *caps, *adj_caps;

  videocrop_test_cropping_init_context (&ctx);

  crop = GST_BASE_TRANSFORM (ctx.crop);
  klass = GST_BASE_TRANSFORM_GET_CLASS (ctx.crop);
  fail_unless (klass != NULL);

  caps = gst_caps_new_simple ("video/x-raw-yuv",
      "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('I', '4', '2', '0'),
      "framerate", GST_TYPE_FRACTION, 1, 1,
      "width", G_TYPE_INT, 200, "height", G_TYPE_INT, 100, NULL);

  /* by default, it should be no cropping and hence passthrough */
  adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_is_equal (adj_caps, caps));
  gst_caps_unref (adj_caps);

  adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_is_equal (adj_caps, caps));
  gst_caps_unref (adj_caps);

  /* make sure that's still true after changing properties back and forth */
  g_object_set (ctx.crop, "left", 1, "right", 3, "top", 5, "bottom", 7, NULL);
  g_object_set (ctx.crop, "left", 0, "right", 0, "top", 0, "bottom", 0, NULL);

  adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_is_equal (adj_caps, caps));
  gst_caps_unref (adj_caps);

  adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_is_equal (adj_caps, caps));
  gst_caps_unref (adj_caps);

  /* now check adjustments made ... */
  g_object_set (ctx.crop, "left", 1, "right", 3, "top", 5, "bottom", 7, NULL);

  /* ========= (1) fixed value ============================================= */

  /* sink => source, source must be bigger if we crop stuff off */
  adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_get_size (adj_caps) == 1);
  w_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
  fail_unless (w_val != NULL);
  fail_unless (G_VALUE_HOLDS_INT (w_val));
  fail_unless_equals_int (g_value_get_int (w_val), 200 + (1 + 3));
  h_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
  fail_unless (h_val != NULL);
  fail_unless (G_VALUE_HOLDS_INT (h_val));
  fail_unless_equals_int (g_value_get_int (h_val), 100 + (5 + 7));
  gst_caps_unref (adj_caps);

  /* source => sink becomes smaller */
  adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_get_size (adj_caps) == 1);
  w_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
  fail_unless (w_val != NULL);
  fail_unless (G_VALUE_HOLDS_INT (w_val));
  fail_unless_equals_int (g_value_get_int (w_val), 200 - (1 + 3));
  h_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
  fail_unless (h_val != NULL);
  fail_unless (G_VALUE_HOLDS_INT (h_val));
  fail_unless_equals_int (g_value_get_int (h_val), 100 - (5 + 7));
  gst_caps_unref (adj_caps);

  /* ========= (2) range (simple adjustment) =============================== */

  gst_structure_set (gst_caps_get_structure (caps, 0),
      "width", GST_TYPE_INT_RANGE, 1000, 2000,
      "height", GST_TYPE_INT_RANGE, 3000, 4000, NULL);

  /* sink => source, source must be bigger if we crop stuff off */
  adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_get_size (adj_caps) == 1);
  w_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
  fail_unless (w_val != NULL);
  fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
  fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1000 + (1 + 3));
  fail_unless_equals_int (gst_value_get_int_range_max (w_val), 2000 + (1 + 3));
  h_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
  fail_unless (h_val != NULL);
  fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
  fail_unless_equals_int (gst_value_get_int_range_min (h_val), 3000 + (5 + 7));
  fail_unless_equals_int (gst_value_get_int_range_max (h_val), 4000 + (5 + 7));
  gst_caps_unref (adj_caps);

  /* source => sink becomes smaller */
  adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_get_size (adj_caps) == 1);
  w_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
  fail_unless (w_val != NULL);
  fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
  fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1000 - (1 + 3));
  fail_unless_equals_int (gst_value_get_int_range_max (w_val), 2000 - (1 + 3));
  h_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
  fail_unless (h_val != NULL);
  fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
  fail_unless_equals_int (gst_value_get_int_range_min (h_val), 3000 - (5 + 7));
  fail_unless_equals_int (gst_value_get_int_range_max (h_val), 4000 - (5 + 7));
  gst_caps_unref (adj_caps);

  /* ========= (3) range (adjustment at boundary) ========================== */

  gst_structure_set (gst_caps_get_structure (caps, 0),
      "width", GST_TYPE_INT_RANGE, 2, G_MAXINT,
      "height", GST_TYPE_INT_RANGE, 2, G_MAXINT, NULL);

  /* sink => source, source must be bigger if we crop stuff off */
  adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_get_size (adj_caps) == 1);
  w_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
  fail_unless (w_val != NULL);
  fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
  fail_unless_equals_int (gst_value_get_int_range_min (w_val), 2 + (1 + 3));
  fail_unless_equals_int (gst_value_get_int_range_max (w_val), G_MAXINT);
  h_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
  fail_unless (h_val != NULL);
  fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
  fail_unless_equals_int (gst_value_get_int_range_min (h_val), 2 + (5 + 7));
  fail_unless_equals_int (gst_value_get_int_range_max (h_val), G_MAXINT);
  gst_caps_unref (adj_caps);

  /* source => sink becomes smaller */
  adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_get_size (adj_caps) == 1);
  w_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
  fail_unless (w_val != NULL);
  fail_unless (GST_VALUE_HOLDS_INT_RANGE (w_val));
  fail_unless_equals_int (gst_value_get_int_range_min (w_val), 1);
  fail_unless_equals_int (gst_value_get_int_range_max (w_val),
      G_MAXINT - (1 + 3));
  h_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
  fail_unless (h_val != NULL);
  fail_unless (GST_VALUE_HOLDS_INT_RANGE (h_val));
  fail_unless_equals_int (gst_value_get_int_range_min (h_val), 1);
  fail_unless_equals_int (gst_value_get_int_range_max (h_val),
      G_MAXINT - (5 + 7));
  gst_caps_unref (adj_caps);

  /* ========= (4) list of values ========================================== */

  {
    GValue list = { 0, };
    GValue ival = { 0, };

    g_value_init (&ival, G_TYPE_INT);
    g_value_init (&list, GST_TYPE_LIST);
    g_value_set_int (&ival, 2);
    gst_value_list_append_value (&list, &ival);
    g_value_set_int (&ival, G_MAXINT);
    gst_value_list_append_value (&list, &ival);
    gst_structure_set_value (gst_caps_get_structure (caps, 0), "width", &list);
    g_value_unset (&list);
    g_value_unset (&ival);

    g_value_init (&ival, G_TYPE_INT);
    g_value_init (&list, GST_TYPE_LIST);
    g_value_set_int (&ival, 5);
    gst_value_list_append_value (&list, &ival);
    g_value_set_int (&ival, 1000);
    gst_value_list_append_value (&list, &ival);
    gst_structure_set_value (gst_caps_get_structure (caps, 0), "height", &list);
    g_value_unset (&list);
    g_value_unset (&ival);
  }

  /* sink => source, source must be bigger if we crop stuff off */
  adj_caps = klass->transform_caps (crop, GST_PAD_SRC, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_get_size (adj_caps) == 1);
  w_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
  fail_unless (w_val != NULL);
  fail_unless (GST_VALUE_HOLDS_LIST (w_val));
  fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 0),
      2 + (1 + 3));
  fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 1), G_MAXINT);
  h_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
  fail_unless (h_val != NULL);
  fail_unless (GST_VALUE_HOLDS_LIST (h_val));
  fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 0),
      5 + (5 + 7));
  fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 1),
      1000 + (5 + 7));
  gst_caps_unref (adj_caps);

  /* source => sink becomes smaller */
  adj_caps = klass->transform_caps (crop, GST_PAD_SINK, caps);
  fail_unless (adj_caps != NULL);
  fail_unless (gst_caps_get_size (adj_caps) == 1);
  w_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "width");
  fail_unless (w_val != NULL);
  fail_unless (GST_VALUE_HOLDS_LIST (w_val));
  fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 0), 1);
  fail_unless_equals_int (notgst_value_list_get_nth_int (w_val, 1),
      G_MAXINT - (1 + 3));
  h_val =
      gst_structure_get_value (gst_caps_get_structure (adj_caps, 0), "height");
  fail_unless (h_val != NULL);
  fail_unless (GST_VALUE_HOLDS_LIST (h_val));
  fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 0), 1);
  fail_unless_equals_int (notgst_value_list_get_nth_int (h_val, 1),
      1000 - (5 + 7));
  gst_caps_unref (adj_caps);

  gst_caps_unref (caps);
  videocrop_test_cropping_deinit_context (&ctx);
}

GST_END_TEST;

751
752
753
754
755
756
757
758
759
static Suite *
videocrop_suite (void)
{
  Suite *s = suite_create ("videocrop");
  TCase *tc_chain = tcase_create ("general");

#ifdef HAVE_VALGRIND
  if (RUNNING_ON_VALGRIND) {
    /* our tests take quite a long time, so increase
760
761
     * timeout (~25 minutes on my 1.6GHz AMD K7) */
    tcase_set_timeout (tc_chain, 30 * 60);
762
763
764
765
766
767
768
769
  } else
#endif
  {
    /* increase timeout, these tests take a long time (60 secs here) */
    tcase_set_timeout (tc_chain, 2 * 60);
  }

  suite_add_tcase (s, tc_chain);
770
  tcase_add_test (tc_chain, test_crop_to_1x1);
771
772
  tcase_add_test (tc_chain, test_caps_transform);
  tcase_add_test (tc_chain, test_passthrough);
773
774
775
776
777
778
  tcase_add_test (tc_chain, test_unit_sizes);
  tcase_add_test (tc_chain, test_cropping);

  return s;
}

779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
int
main (int argc, char **argv)
{
  int nf;

  Suite *s = videocrop_suite ();
  SRunner *sr = srunner_create (s);

#ifdef HAVE_VALGRIND
  if (RUNNING_ON_VALGRIND) {
    /* otherwise valgrind errors out when liboil probes CPU extensions
     * in oil_init() during which it causes SIGILLs etc. to be fired */
    g_setenv ("OIL_CPU_FLAGS", "0", 0);
  }
#endif

  gst_check_init (&argc, &argv);

  srunner_run_all (sr, CK_NORMAL);
  nf = srunner_ntests_failed (sr);
  srunner_free (sr);

  return nf;
}