videoscale.c 29.4 KB
Newer Older
1
2
3
4
/* GStreamer
 *
 * unit test for videoscale
 *
5
 * Copyright (C) <2009,2010> Sebastian Dröge <sebastian.droege@collabora.co.uk>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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.
 */

23
24
25
#include <gst/video/video.h>
#include <gst/base/gstbasesink.h>

26
27
28
#include <gst/check/gstcheck.h>
#include <string.h>

29
30
31
/* kids, don't do this at home, skipping checks is *BAD* */
#define LINK_CHECK_FLAGS GST_PAD_LINK_CHECK_NOTHING

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
static GstCaps **
videoscale_get_allowed_caps (void)
{
  GstElement *scale = gst_element_factory_make ("videoscale", "scale");
  GstPadTemplate *templ;
  GstCaps *caps, **ret;
  GstStructure *s;
  gint i, n;

  templ =
      gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (scale),
      "sink");
  fail_unless (templ != NULL);

  caps = gst_pad_template_get_caps (templ);

  n = gst_caps_get_size (caps);
  ret = g_new0 (GstCaps *, n + 1);

  for (i = 0; i < n; i++) {
    s = gst_caps_get_structure (caps, i);
    ret[i] = gst_caps_new_empty ();
    gst_caps_append_structure (ret[i], gst_structure_copy (s));
  }

57
  gst_caps_unref (caps);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  gst_object_unref (scale);

  return ret;
}

static void
on_sink_handoff (GstElement * element, GstBuffer * buffer, GstPad * pad,
    gpointer user_data)
{
  guint *n_buffers = user_data;

  *n_buffers = *n_buffers + 1;
}

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
static gboolean
caps_are_64bpp (const GstCaps * caps)
{
  GstVideoFormat fmt;
  GstCaps *fmt_caps;

  /* need fixed caps for _parse_caps */
  fmt_caps = gst_caps_copy (caps);
  gst_structure_remove_field (gst_caps_get_structure (fmt_caps, 0), "width");
  gst_structure_remove_field (gst_caps_get_structure (fmt_caps, 0), "height");
  gst_structure_remove_field (gst_caps_get_structure (fmt_caps, 0),
      "framerate");

  fail_unless (gst_video_format_parse_caps (fmt_caps, &fmt, NULL, NULL));
  gst_caps_unref (fmt_caps);
  return (fmt == GST_VIDEO_FORMAT_ARGB64 || fmt == GST_VIDEO_FORMAT_AYUV64);
}

90
91
92
93
94
95
96
static void
run_test (const GstCaps * caps, gint src_width, gint src_height,
    gint dest_width, gint dest_height, gint method,
    GCallback src_handoff, gpointer src_handoff_user_data,
    GCallback sink_handoff, gpointer sink_handoff_user_data)
{
  GstElement *pipeline;
97
  GstElement *src, *videoconvert, *capsfilter1, *identity, *scale,
98
      *capsfilter2, *sink;
99
  GstMessage *msg;
100
101
102
103
  GstBus *bus;
  GstCaps *copy;
  guint n_buffers = 0;

104
  /* skip formats that videoconvert can't handle */
105
106
107
  if (caps_are_64bpp (caps))
    return;

108
109
110
111
112
  pipeline = gst_element_factory_make ("pipeline", "pipeline");
  fail_unless (pipeline != NULL);

  src = gst_element_factory_make ("videotestsrc", "src");
  fail_unless (src != NULL);
113
  g_object_set (G_OBJECT (src), "num-buffers", 1, NULL);
114

115
116
  videoconvert = gst_element_factory_make ("videoconvert", "csp");
  fail_unless (videoconvert != NULL);
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
  capsfilter1 = gst_element_factory_make ("capsfilter", "filter1");
  fail_unless (capsfilter1 != NULL);
  copy = gst_caps_copy (caps);
  gst_caps_set_simple (copy, "width", G_TYPE_INT, src_width, "height",
      G_TYPE_INT, src_height, "framerate", GST_TYPE_FRACTION, 30, 1, NULL);
  g_object_set (G_OBJECT (capsfilter1), "caps", copy, NULL);
  gst_caps_unref (copy);

  identity = gst_element_factory_make ("identity", "identity");
  fail_unless (identity != NULL);
  if (src_handoff) {
    g_object_set (G_OBJECT (identity), "signal-handoffs", TRUE, NULL);
    g_signal_connect (identity, "handoff", G_CALLBACK (src_handoff),
        src_handoff_user_data);
  }

  scale = gst_element_factory_make ("videoscale", "scale");
  fail_unless (scale != NULL);
  g_object_set (G_OBJECT (scale), "method", method, NULL);

  capsfilter2 = gst_element_factory_make ("capsfilter", "filter2");
  fail_unless (capsfilter2 != NULL);
  copy = gst_caps_copy (caps);
  gst_caps_set_simple (copy, "width", G_TYPE_INT, dest_width, "height",
      G_TYPE_INT, dest_height, NULL);
  g_object_set (G_OBJECT (capsfilter2), "caps", copy, NULL);
  gst_caps_unref (copy);

  sink = gst_element_factory_make ("fakesink", "sink");
  fail_unless (sink != NULL);
  g_object_set (G_OBJECT (sink), "signal-handoffs", TRUE, "async", FALSE, NULL);
  g_signal_connect (sink, "handoff", G_CALLBACK (on_sink_handoff), &n_buffers);
  if (sink_handoff) {
    g_signal_connect (sink, "handoff", G_CALLBACK (sink_handoff),
        sink_handoff_user_data);
  }

155
  gst_bin_add_many (GST_BIN (pipeline), src, videoconvert, capsfilter1,
156
      identity, scale, capsfilter2, sink, NULL);
157

158
  fail_unless (gst_element_link_pads_full (src, "src", videoconvert, "sink",
159
          LINK_CHECK_FLAGS));
160
  fail_unless (gst_element_link_pads_full (videoconvert, "src", capsfilter1,
161
162
163
164
165
166
167
168
169
          "sink", LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (capsfilter1, "src", identity, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (identity, "src", scale, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (scale, "src", capsfilter2, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (capsfilter2, "src", sink, "sink",
          LINK_CHECK_FLAGS));
170
171
172
173
174
175
176

  bus = gst_element_get_bus (pipeline);
  fail_unless (bus != NULL);

  fail_unless (gst_element_set_state (pipeline,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);

177
178
179
180
  msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
      GST_MESSAGE_EOS | GST_MESSAGE_ERROR | GST_MESSAGE_WARNING);

  fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_EOS);
181
182
183
184

  fail_unless (gst_element_set_state (pipeline,
          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);

185
  fail_unless (n_buffers == 1);
186
187

  gst_object_unref (pipeline);
188
189
  gst_message_unref (msg);
  gst_object_unref (bus);
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
}

static void
on_sink_handoff_passthrough (GstElement * element, GstBuffer * buffer,
    GstPad * pad, gpointer user_data)
{
  GList **list = user_data;

  *list = g_list_prepend (*list, gst_buffer_ref (buffer));
}

static void
on_src_handoff_passthrough (GstElement * element, GstBuffer * buffer,
    gpointer user_data)
{
  GList **list = user_data;

  *list = g_list_prepend (*list, gst_buffer_ref (buffer));
}

GST_START_TEST (test_passthrough)
{
  GList *l1, *l2, *src_buffers = NULL, *sink_buffers = NULL;
  GstCaps **allowed_caps = NULL, **p;
  gint method;
  static const gint src_width = 640, src_height = 480;
  static const gint dest_width = 640, dest_height = 480;

  p = allowed_caps = videoscale_get_allowed_caps ();

  while (*p) {
    GstCaps *caps = *p;

    for (method = 0; method < 3; method++) {
224
      /* skip formats that videoconvert can't handle */
225
226
227
      if (caps_are_64bpp (caps))
        continue;

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
      GST_DEBUG ("Running test for caps '%" GST_PTR_FORMAT "'"
          " from %dx%u to %dx%d with method %d", caps, src_width, src_height,
          dest_width, dest_height, method);
      run_test (caps, src_width, src_height,
          dest_width, dest_height, method,
          G_CALLBACK (on_src_handoff_passthrough), &src_buffers,
          G_CALLBACK (on_sink_handoff_passthrough), &sink_buffers);

      fail_unless (src_buffers && sink_buffers);
      fail_unless_equals_int (g_list_length (src_buffers),
          g_list_length (sink_buffers));

      for (l1 = src_buffers, l2 = sink_buffers; l1 && l2;
          l1 = l1->next, l2 = l2->next) {
        GstBuffer *a = l1->data;
        GstBuffer *b = l2->data;
244
245
246
247
248
249
250
251
252
        gsize sa, sb;
        guint8 *pa, *pb;

        pa = gst_buffer_map (a, &sa, NULL, GST_MAP_READ);
        pb = gst_buffer_map (b, &sb, NULL, GST_MAP_READ);
        fail_unless_equals_int (sa, sb);
        fail_unless (pa == pb);
        gst_buffer_unmap (b, pb, sb);
        gst_buffer_unmap (a, pa, sa);
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

        gst_buffer_unref (a);
        gst_buffer_unref (b);
      }
      g_list_free (src_buffers);
      src_buffers = NULL;
      g_list_free (sink_buffers);
      sink_buffers = NULL;
    }

    gst_caps_unref (caps);
    p++;
  }
  g_free (allowed_caps);
}

GST_END_TEST;

#define CREATE_TEST(name,method,src_width,src_height,dest_width,dest_height) \
GST_START_TEST (name) \
{ \
  GstCaps **allowed_caps = NULL, **p; \
  \
  p = allowed_caps = videoscale_get_allowed_caps (); \
  \
  while (*p) { \
    GstCaps *caps = *p; \
    \
    GST_DEBUG ("Running test for caps '%" GST_PTR_FORMAT "'" \
        " from %dx%u to %dx%d with method %d", caps, src_width, src_height, \
        dest_width, dest_height, method); \
    run_test (caps, src_width, src_height, \
        dest_width, dest_height, method, \
        NULL, NULL, NULL, NULL); \
    gst_caps_unref (caps); \
    p++; \
  } \
  g_free (allowed_caps); \
} \
\
GST_END_TEST;

CREATE_TEST (test_downscale_640x480_320x240_method_0, 0, 640, 480, 320, 240);
CREATE_TEST (test_downscale_640x480_320x240_method_1, 1, 640, 480, 320, 240);
CREATE_TEST (test_downscale_640x480_320x240_method_2, 2, 640, 480, 320, 240);
CREATE_TEST (test_upscale_320x240_640x480_method_0, 0, 320, 240, 640, 480);
CREATE_TEST (test_upscale_320x240_640x480_method_1, 1, 320, 240, 640, 480);
CREATE_TEST (test_upscale_320x240_640x480_method_2, 2, 320, 240, 640, 480);
CREATE_TEST (test_downscale_640x480_1x1_method_0, 0, 640, 480, 1, 1);
CREATE_TEST (test_downscale_640x480_1x1_method_1, 1, 640, 480, 1, 1);
CREATE_TEST (test_downscale_640x480_1x1_method_2, 2, 640, 480, 1, 1);
CREATE_TEST (test_upscale_1x1_640x480_method_0, 0, 1, 1, 640, 480);
CREATE_TEST (test_upscale_1x1_640x480_method_1, 1, 1, 1, 640, 480);
CREATE_TEST (test_upscale_1x1_640x480_method_2, 2, 1, 1, 640, 480);
CREATE_TEST (test_downscale_641x481_111x30_method_0, 0, 641, 481, 111, 30);
CREATE_TEST (test_downscale_641x481_111x30_method_1, 1, 641, 481, 111, 30);
CREATE_TEST (test_downscale_641x481_111x30_method_2, 2, 641, 481, 111, 30);
CREATE_TEST (test_upscale_111x30_641x481_method_0, 0, 111, 30, 641, 481);
CREATE_TEST (test_upscale_111x30_641x481_method_1, 1, 111, 30, 641, 481);
CREATE_TEST (test_upscale_111x30_641x481_method_2, 2, 111, 30, 641, 481);
CREATE_TEST (test_downscale_641x481_30x111_method_0, 0, 641, 481, 30, 111);
CREATE_TEST (test_downscale_641x481_30x111_method_1, 1, 641, 481, 30, 111);
CREATE_TEST (test_downscale_641x481_30x111_method_2, 2, 641, 481, 30, 111);
CREATE_TEST (test_upscale_30x111_641x481_method_0, 0, 30, 111, 641, 481);
CREATE_TEST (test_upscale_30x111_641x481_method_1, 1, 30, 111, 641, 481);
CREATE_TEST (test_upscale_30x111_641x481_method_2, 2, 30, 111, 641, 481);
319
320
321
322
323
324
325
326
327
328
329
330
CREATE_TEST (test_downscale_640x480_320x1_method_0, 0, 640, 480, 320, 1);
CREATE_TEST (test_downscale_640x480_320x1_method_1, 1, 640, 480, 320, 1);
CREATE_TEST (test_downscale_640x480_320x1_method_2, 2, 640, 480, 320, 1);
CREATE_TEST (test_upscale_320x1_640x480_method_0, 0, 320, 1, 640, 480);
CREATE_TEST (test_upscale_320x1_640x480_method_1, 1, 320, 1, 640, 480);
CREATE_TEST (test_upscale_320x1_640x480_method_2, 2, 320, 1, 640, 480);
CREATE_TEST (test_downscale_640x480_1x240_method_0, 0, 640, 480, 1, 240);
CREATE_TEST (test_downscale_640x480_1x240_method_1, 1, 640, 480, 1, 240);
CREATE_TEST (test_downscale_640x480_1x240_method_2, 2, 640, 480, 1, 240);
CREATE_TEST (test_upscale_1x240_640x480_method_0, 0, 1, 240, 640, 480);
CREATE_TEST (test_upscale_1x240_640x480_method_1, 1, 1, 240, 640, 480);
CREATE_TEST (test_upscale_1x240_640x480_method_2, 2, 1, 240, 640, 480);
331

332
333
334
335
336
337
338
339
340
341
342
343
typedef struct
{
  gint width, height;
  gint par_n, par_d;
  gboolean ok;
  GMainLoop *loop;
} TestNegotiationData;

static void
_test_negotiation_message (GstBus * bus, GstMessage * message,
    TestNegotiationData * data)
{
344
345
346
  GError *err = NULL;
  gchar *debug;

347
348
  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:
349
350
351
352
353
354
      gst_message_parse_error (message, &err, &debug);
      gst_object_default_error (GST_MESSAGE_SRC (message), err, debug);
      g_error_free (err);
      g_free (debug);
      g_assert_not_reached ();
      break;
355
    case GST_MESSAGE_WARNING:
356
357
358
359
      gst_message_parse_warning (message, &err, &debug);
      gst_object_default_error (GST_MESSAGE_SRC (message), err, debug);
      g_error_free (err);
      g_free (debug);
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
415
416
      g_assert_not_reached ();
      break;
    case GST_MESSAGE_EOS:
      g_main_loop_quit (data->loop);
      break;
    default:
      break;
  }
}

static void
_test_negotiation_notify_caps (GObject * src, GParamSpec * pspec,
    TestNegotiationData * data)
{
  GstCaps *caps;
  GstStructure *s;
  gint width, height;
  gint par_n = 0, par_d = 0;

  g_object_get (src, "caps", &caps, NULL);
  if (caps == NULL)
    return;

  s = gst_caps_get_structure (caps, 0);

  fail_unless (gst_structure_get_int (s, "width", &width));
  fail_unless (gst_structure_get_int (s, "height", &height));
  fail_unless (gst_structure_get_fraction (s, "pixel-aspect-ratio", &par_n,
          &par_d) || (data->par_n == 1 && data->par_d == 1));

  gst_caps_unref (caps);

  fail_unless_equals_int (width, data->width);
  fail_unless_equals_int (height, data->height);
  if (par_n != 0 || par_d != 0) {
    fail_unless_equals_int (par_n, data->par_n);
    fail_unless_equals_int (par_d, data->par_d);
  }

  data->ok = (width == data->width) && (height == data->height)
      && (par_n == data->par_n) && (par_d == data->par_d);

  g_main_loop_quit (data->loop);
}

static void
_test_negotiation (const gchar * src_templ, const gchar * sink_templ,
    gint width, gint height, gint par_n, gint par_d)
{
  GstElement *pipeline;
  GstElement *src, *capsfilter1, *scale, *capsfilter2, *sink;
  GstBus *bus;
  GMainLoop *loop;
  GstCaps *caps;
  TestNegotiationData data = { 0, 0, 0, 0, FALSE, NULL };
  GstPad *pad;

417
418
419
  GST_DEBUG ("Running test for src templ caps '%s' and sink templ caps '%s'",
      src_templ, sink_templ);

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
  pipeline = gst_element_factory_make ("pipeline", "pipeline");
  fail_unless (pipeline != NULL);

  src = gst_element_factory_make ("videotestsrc", "src");
  fail_unless (src != NULL);
  g_object_set (G_OBJECT (src), "num-buffers", 1, NULL);

  capsfilter1 = gst_element_factory_make ("capsfilter", "filter1");
  fail_unless (capsfilter1 != NULL);
  caps = gst_caps_from_string (src_templ);
  fail_unless (caps != NULL);
  g_object_set (G_OBJECT (capsfilter1), "caps", caps, NULL);
  gst_caps_unref (caps);

  scale = gst_element_factory_make ("videoscale", "scale");
  fail_unless (scale != NULL);

  capsfilter2 = gst_element_factory_make ("capsfilter", "filter2");
  fail_unless (capsfilter2 != NULL);
  caps = gst_caps_from_string (sink_templ);
  fail_unless (caps != NULL);
  g_object_set (G_OBJECT (capsfilter2), "caps", caps, NULL);
  gst_caps_unref (caps);

  pad = gst_element_get_static_pad (capsfilter2, "sink");
  fail_unless (pad != NULL);
  g_signal_connect (pad, "notify::caps",
      G_CALLBACK (_test_negotiation_notify_caps), &data);
  gst_object_unref (pad);

  sink = gst_element_factory_make ("fakesink", "sink");
  fail_unless (sink != NULL);
  g_object_set (sink, "async", FALSE, NULL);

  gst_bin_add_many (GST_BIN (pipeline), src, capsfilter1, scale, capsfilter2,
      sink, NULL);
456
457
458
459
460
461
462
463
464

  fail_unless (gst_element_link_pads_full (src, "src", capsfilter1, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (capsfilter1, "src", scale, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (scale, "src", capsfilter2, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (capsfilter2, "src", sink, "sink",
          LINK_CHECK_FLAGS));
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

  loop = g_main_loop_new (NULL, FALSE);

  bus = gst_element_get_bus (pipeline);
  fail_unless (bus != NULL);
  gst_bus_add_signal_watch (bus);

  data.loop = loop;
  data.width = width;
  data.height = height;
  data.par_n = par_n;
  data.par_d = par_d;
  data.ok = FALSE;

  g_signal_connect (bus, "message", G_CALLBACK (_test_negotiation_message),
      &data);

  gst_object_unref (bus);

  fail_unless (gst_element_set_state (pipeline,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);

  g_main_loop_run (loop);

  fail_unless (data.ok == TRUE);

  fail_unless (gst_element_set_state (pipeline,
          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);

  gst_object_unref (pipeline);
  g_main_loop_unref (loop);
}

GST_START_TEST (test_negotiation)
{
  _test_negotiation
Wim Taymans's avatar
Wim Taymans committed
501
502
      ("video/x-raw,format=(string)AYUV,width=720,height=576,pixel-aspect-ratio=16/15",
      "video/x-raw,format=(string)AYUV,width=768,height=576", 768, 576, 1, 1);
503
504

  _test_negotiation
Wim Taymans's avatar
Wim Taymans committed
505
506
      ("video/x-raw,format=(string)AYUV,width=320,height=240",
      "video/x-raw,format=(string)AYUV,width=640,height=320", 640, 320, 2, 3);
507
508

  _test_negotiation
Wim Taymans's avatar
Wim Taymans committed
509
510
      ("video/x-raw,format=(string)AYUV,width=320,height=240",
      "video/x-raw,format=(string)AYUV,width=640,height=320,pixel-aspect-ratio=[0/1, 1/1]",
511
512
513
      640, 320, 2, 3);

  _test_negotiation
Wim Taymans's avatar
Wim Taymans committed
514
515
      ("video/x-raw,format=(string)AYUV,width=1920,height=2560,pixel-aspect-ratio=1/1",
      "video/x-raw,format=(string)AYUV,width=[1, 2048],height=[1, 2048],pixel-aspect-ratio=1/1",
516
517
518
      1536, 2048, 1, 1);

  _test_negotiation
Wim Taymans's avatar
Wim Taymans committed
519
520
      ("video/x-raw,format=(string)AYUV,width=1920,height=2560,pixel-aspect-ratio=1/1",
      "video/x-raw,format=(string)AYUV,width=[1, 2048],height=[1, 2048]",
521
522
523
      1920, 2048, 4, 5);

  _test_negotiation
Wim Taymans's avatar
Wim Taymans committed
524
525
      ("video/x-raw,format=(string)AYUV,width=1920,height=2560",
      "video/x-raw,format=(string)AYUV,width=[1, 2048],height=[1, 2048]",
526
527
528
      1920, 2048, 4, 5);

  _test_negotiation
Wim Taymans's avatar
Wim Taymans committed
529
530
      ("video/x-raw,format=(string)AYUV,width=1920,height=2560",
      "video/x-raw,format=(string)AYUV,width=1200,height=[1, 2048],pixel-aspect-ratio=1/1",
531
532
533
534
      1200, 1600, 1, 1);

  /* Doesn't keep DAR but must be possible! */
  _test_negotiation
Wim Taymans's avatar
Wim Taymans committed
535
536
      ("video/x-raw,format=(string)AYUV,width=320,height=240,pixel-aspect-ratio=1/1",
      "video/x-raw,format=(string)AYUV,width=200,height=200,pixel-aspect-ratio=1/2",
537
538
539
540
541
      200, 200, 1, 2);
}

GST_END_TEST;

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
#define GST_TYPE_TEST_REVERSE_NEGOTIATION_SINK \
  (gst_test_reverse_negotiation_sink_get_type())
#define GST_TEST_REVERSE_NEGOTIATION_SINK(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_TEST_REVERSE_NEGOTIATION_SINK,GstTestReverseNegotiationSink))
#define GST_TEST_REVERSE_NEGOTIATION_SINK_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_TEST_REVERSE_NEGOTIATION_SINK,GstTestReverseNegotiationSinkClass))
#define GST_IS_TEST_REVERSE_NEGOTIATION_SINK(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_TEST_REVERSE_NEGOTIATION_SINK))
#define GST_IS_TEST_REVERSE_NEGOTIATION_SINK_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_TEST_REVERSE_NEGOTIATION_SINK))
#define GST_TEST_REVERSE_NEGOTIATION_SINK_CAST(obj) ((GstTestReverseNegotiationSink *)obj)

typedef struct _GstTestReverseNegotiationSink GstTestReverseNegotiationSink;
typedef struct _GstTestReverseNegotiationSinkClass
    GstTestReverseNegotiationSinkClass;
struct _GstTestReverseNegotiationSink
{
  GstBaseSink element;
  gint nbuffers;
};

struct _GstTestReverseNegotiationSinkClass
{
  GstBaseSinkClass parent_class;
};

GType gst_test_reverse_negotiation_sink_get_type (void);

570
571
G_DEFINE_TYPE (GstTestReverseNegotiationSink,
    gst_test_reverse_negotiation_sink, GST_TYPE_BASE_SINK);
572
573
574
575
576
577

static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB));

Wim Taymans's avatar
Wim Taymans committed
578
#if 0
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
static GstFlowReturn
gst_test_reverse_negotiation_sink_buffer_alloc (GstBaseSink * bsink,
    guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf)
{
  GstTestReverseNegotiationSink *sink =
      GST_TEST_REVERSE_NEGOTIATION_SINK (bsink);
  GstVideoFormat fmt;
  gint width, height;

  fail_unless (gst_video_format_parse_caps (caps, &fmt, &width, &height));

  if (sink->nbuffers < 2) {
    *buf =
        gst_buffer_new_and_alloc (gst_video_format_get_size (fmt, width,
            height));
    gst_buffer_set_caps (*buf, caps);
  } else {
    gint fps_n, fps_d;

    fail_unless (gst_video_parse_caps_framerate (caps, &fps_n, &fps_d));

    width = 512;
    height = 128;
    *buf =
        gst_buffer_new_and_alloc (gst_video_format_get_size (fmt, width,
            height));
    caps = gst_video_format_new_caps (fmt, width, height, fps_n, fps_d, 1, 1);
    gst_buffer_set_caps (*buf, caps);
    gst_caps_unref (caps);
  }

  return GST_FLOW_OK;
}
Wim Taymans's avatar
Wim Taymans committed
612
#endif
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629

static GstFlowReturn
gst_test_reverse_negotiation_sink_render (GstBaseSink * bsink,
    GstBuffer * buffer)
{
  GstTestReverseNegotiationSink *sink =
      GST_TEST_REVERSE_NEGOTIATION_SINK (bsink);
  GstCaps *caps = gst_buffer_get_caps (buffer);
  GstVideoFormat fmt;
  gint width, height;

  fail_unless (caps != NULL);
  fail_unless (gst_video_format_parse_caps (caps, &fmt, &width, &height));

  sink->nbuffers++;

  /* The third buffer is still in the old size
630
   * because the videoconverts can't convert
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
   * the frame sizes
   */
  if (sink->nbuffers > 3) {
    fail_unless_equals_int (width, 512);
    fail_unless_equals_int (height, 128);
  }

  gst_caps_unref (caps);

  return GST_FLOW_OK;
}

static void
gst_test_reverse_negotiation_sink_class_init (GstTestReverseNegotiationSinkClass
    * klass)
{
Wim Taymans's avatar
Wim Taymans committed
647
  GstElementClass *gstelement_class;
648
649
  GstBaseSinkClass *gstbase_sink_class;

Wim Taymans's avatar
Wim Taymans committed
650
  gstelement_class = GST_ELEMENT_CLASS (klass);
651
652
  gstbase_sink_class = GST_BASE_SINK_CLASS (klass);

653
654
655
656
657
658
659
  gst_element_class_set_details_simple (gstelement_class,
      "Test Reverse Negotiation Sink",
      "Sink",
      "Some test sink", "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&sinktemplate));

Wim Taymans's avatar
Wim Taymans committed
660
#if 0
661
662
  gstbase_sink_class->buffer_alloc =
      GST_DEBUG_FUNCPTR (gst_test_reverse_negotiation_sink_buffer_alloc);
Wim Taymans's avatar
Wim Taymans committed
663
#endif
664
665
666
667
668
  gstbase_sink_class->render =
      GST_DEBUG_FUNCPTR (gst_test_reverse_negotiation_sink_render);
}

static void
669
gst_test_reverse_negotiation_sink_init (GstTestReverseNegotiationSink * sink)
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
{
  sink->nbuffers = 0;
}

static void
_test_reverse_negotiation_message (GstBus * bus, GstMessage * message,
    GMainLoop * loop)
{
  GError *err = NULL;
  gchar *debug;

  switch (GST_MESSAGE_TYPE (message)) {
    case GST_MESSAGE_ERROR:
      gst_message_parse_error (message, &err, &debug);
      gst_object_default_error (GST_MESSAGE_SRC (message), err, debug);
      g_error_free (err);
      g_free (debug);
      g_assert_not_reached ();
      break;
    case GST_MESSAGE_WARNING:
      gst_message_parse_warning (message, &err, &debug);
      gst_object_default_error (GST_MESSAGE_SRC (message), err, debug);
      g_error_free (err);
      g_free (debug);
      g_assert_not_reached ();
      break;
    case GST_MESSAGE_EOS:
      g_main_loop_quit (loop);
      break;
    default:
      break;
  }
}

GST_START_TEST (test_reverse_negotiation)
{
  GstElement *pipeline;
  GstElement *src, *csp1, *scale, *csp2, *sink;
  GstBus *bus;
  GMainLoop *loop;

  pipeline = gst_element_factory_make ("pipeline", "pipeline");
  fail_unless (pipeline != NULL);

  src = gst_element_factory_make ("videotestsrc", "src");
  fail_unless (src != NULL);
  g_object_set (G_OBJECT (src), "num-buffers", 8, NULL);

718
  csp1 = gst_element_factory_make ("videoconvert", "csp1");
719
720
721
722
723
  fail_unless (csp1 != NULL);

  scale = gst_element_factory_make ("videoscale", "scale");
  fail_unless (scale != NULL);

724
  csp2 = gst_element_factory_make ("videoconvert", "csp2");
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
  fail_unless (csp2 != NULL);

  sink = g_object_new (GST_TYPE_TEST_REVERSE_NEGOTIATION_SINK, NULL);
  fail_unless (sink != NULL);
  g_object_set (sink, "async", FALSE, NULL);

  gst_bin_add_many (GST_BIN (pipeline), src, csp1, scale, csp2, sink, NULL);

  fail_unless (gst_element_link_pads_full (src, "src", csp1, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (csp1, "src", scale, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (scale, "src", csp2, "sink",
          LINK_CHECK_FLAGS));
  fail_unless (gst_element_link_pads_full (csp2, "src", sink, "sink",
          LINK_CHECK_FLAGS));

  loop = g_main_loop_new (NULL, FALSE);

  bus = gst_element_get_bus (pipeline);
  fail_unless (bus != NULL);
  gst_bus_add_signal_watch (bus);

  g_signal_connect (bus, "message",
      G_CALLBACK (_test_reverse_negotiation_message), loop);

  gst_object_unref (bus);

  fail_unless (gst_element_set_state (pipeline,
          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);

  g_main_loop_run (loop);

  fail_unless (gst_element_set_state (pipeline,
          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);

  gst_object_unref (pipeline);
  g_main_loop_unref (loop);
}

GST_END_TEST;

767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
GST_START_TEST (test_basetransform_negotiation)
{
  GstElement *pipeline, *src, *sink, *scale, *capsfilter1, *capsfilter2;
  GstMessage *msg;
  GstCaps *caps;

  pipeline = gst_pipeline_new (NULL);
  src = gst_element_factory_make ("videotestsrc", NULL);
  capsfilter1 = gst_element_factory_make ("capsfilter", NULL);
  scale = gst_element_factory_make ("videoscale", NULL);
  capsfilter2 = gst_element_factory_make ("capsfilter", NULL);
  sink = gst_element_factory_make ("fakesink", NULL);
  fail_unless (pipeline && src && capsfilter1 && scale && capsfilter2 && sink);

  g_object_set (src, "num-buffers", 3, NULL);

Wim Taymans's avatar
Wim Taymans committed
783
  caps = gst_caps_new_simple ("video/x-raw", "format", GST_TYPE_FOURCC,
784
785
786
787
788
789
790
      GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'), "width", G_TYPE_INT, 352,
      "height", G_TYPE_INT, 288, "framerate", GST_TYPE_FRACTION, 30, 1,
      "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, NULL);
  g_object_set (capsfilter1, "caps", caps, NULL);
  gst_caps_unref (caps);

  /* same caps, just different pixel-aspect-ratio */
Wim Taymans's avatar
Wim Taymans committed
791
  caps = gst_caps_new_simple ("video/x-raw", "format", GST_TYPE_FOURCC,
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
      GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'), "width", G_TYPE_INT, 352,
      "height", G_TYPE_INT, 288, "framerate", GST_TYPE_FRACTION, 30, 1,
      "pixel-aspect-ratio", GST_TYPE_FRACTION, 12, 11, NULL);
  g_object_set (capsfilter2, "caps", caps, NULL);
  gst_caps_unref (caps);

  gst_bin_add_many (GST_BIN (pipeline), src, capsfilter1, scale, capsfilter2,
      sink, NULL);
  fail_unless (gst_element_link_many (src, capsfilter1, scale, capsfilter2,
          sink, NULL));

  fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
      GST_STATE_CHANGE_ASYNC);

  msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline), -1,
      GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
  fail_unless_equals_int (GST_MESSAGE_TYPE (msg), GST_MESSAGE_EOS);
  gst_message_unref (msg);

  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
}

GST_END_TEST;

817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
static Suite *
videoscale_suite (void)
{
  Suite *s = suite_create ("videoscale");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_set_timeout (tc_chain, 180);
  tcase_add_test (tc_chain, test_passthrough);
  tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_0);
  tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_1);
  tcase_add_test (tc_chain, test_downscale_640x480_320x240_method_2);
  tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_0);
  tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_1);
  tcase_add_test (tc_chain, test_upscale_320x240_640x480_method_2);
  tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_0);
  tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_1);
  tcase_add_test (tc_chain, test_downscale_640x480_1x1_method_2);
  tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_0);
  tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_1);
  tcase_add_test (tc_chain, test_upscale_1x1_640x480_method_2);
  tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_0);
  tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_1);
  tcase_add_test (tc_chain, test_downscale_641x481_111x30_method_2);
  tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_0);
  tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_1);
  tcase_add_test (tc_chain, test_upscale_111x30_641x481_method_2);
  tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_0);
  tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_1);
  tcase_add_test (tc_chain, test_downscale_641x481_30x111_method_2);
  tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_0);
  tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_1);
  tcase_add_test (tc_chain, test_upscale_30x111_641x481_method_2);
850
851
852
853
854
855
856
857
858
859
860
861
  tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_0);
  tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_1);
  tcase_add_test (tc_chain, test_downscale_640x480_320x1_method_2);
  tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_0);
  tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_1);
  tcase_add_test (tc_chain, test_upscale_320x1_640x480_method_2);
  tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_0);
  tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_1);
  tcase_add_test (tc_chain, test_downscale_640x480_1x240_method_2);
  tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_0);
  tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_1);
  tcase_add_test (tc_chain, test_upscale_1x240_640x480_method_2);
862
  tcase_add_test (tc_chain, test_negotiation);
863
  tcase_add_test (tc_chain, test_reverse_negotiation);
864
  tcase_add_test (tc_chain, test_basetransform_negotiation);
865

866
  GST_ERROR ("FIXME: test 64-bpp formats as well");
867
868
869
870
  return s;
}

GST_CHECK_MAIN (videoscale);