video.c 56.2 KB
Newer Older
1
/* GStreamer unit test for video
2
 *
3
 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
4
 * Copyright (C) <2006> Jan Schmidt <thaytan@mad.scientist.com>
5
 * Copyright (C) <2008,2011> Tim-Philipp Müller <tim centricular net>
6
 * Copyright (C) <2012> Collabora Ltd. <tim.muller@collabora.co.uk>
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
20
21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
22
23
24
25
26
27
 */

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

28
29
30
31
#ifdef HAVE_VALGRIND
# include <valgrind/valgrind.h>
#endif

32
33
34
35
36
#include <unistd.h>

#include <gst/check/gstcheck.h>

#include <gst/video/video.h>
37
#include <gst/video/gstvideometa.h>
38
#include <gst/video/video-overlay-composition.h>
39
40
#include <string.h>

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* These are from the current/old videotestsrc; we check our new public API
 * in libgstvideo against the old one to make sure the sizes and offsets
 * end up the same */

typedef struct paintinfo_struct paintinfo;
struct paintinfo_struct
{
  unsigned char *dest;          /* pointer to first byte of video data */
  unsigned char *yp, *up, *vp;  /* pointers to first byte of each component
                                 * for both packed/planar YUV and RGB */
  unsigned char *ap;            /* pointer to first byte of alpha component */
  unsigned char *endptr;        /* pointer to byte beyond last video data */
  int ystride;
  int ustride;
  int vstride;
  int width;
  int height;
};

struct fourcc_list_struct
{
62
63
  const char *fourcc;
  const char *name;
64
65
66
67
68
69
70
71
72
73
74
75
  int bitspp;
  void (*paint_setup) (paintinfo * p, unsigned char *dest);
};

static void paint_setup_I420 (paintinfo * p, unsigned char *dest);
static void paint_setup_YV12 (paintinfo * p, unsigned char *dest);
static void paint_setup_YUY2 (paintinfo * p, unsigned char *dest);
static void paint_setup_UYVY (paintinfo * p, unsigned char *dest);
static void paint_setup_YVYU (paintinfo * p, unsigned char *dest);
static void paint_setup_IYU2 (paintinfo * p, unsigned char *dest);
static void paint_setup_Y41B (paintinfo * p, unsigned char *dest);
static void paint_setup_Y42B (paintinfo * p, unsigned char *dest);
Wim Taymans's avatar
Wim Taymans committed
76
static void paint_setup_GRAY8 (paintinfo * p, unsigned char *dest);
77
78
79
80
81
82
83
84
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
static void paint_setup_AYUV (paintinfo * p, unsigned char *dest);

#if 0
static void paint_setup_IMC1 (paintinfo * p, unsigned char *dest);
static void paint_setup_IMC2 (paintinfo * p, unsigned char *dest);
static void paint_setup_IMC3 (paintinfo * p, unsigned char *dest);
static void paint_setup_IMC4 (paintinfo * p, unsigned char *dest);
#endif
static void paint_setup_YUV9 (paintinfo * p, unsigned char *dest);
static void paint_setup_YVU9 (paintinfo * p, unsigned char *dest);

int fourcc_get_size (struct fourcc_list_struct *fourcc, int w, int h);

struct fourcc_list_struct fourcc_list[] = {
/* packed */
  {"YUY2", "YUY2", 16, paint_setup_YUY2},
  {"UYVY", "UYVY", 16, paint_setup_UYVY},
  {"Y422", "Y422", 16, paint_setup_UYVY},
  {"UYNV", "UYNV", 16, paint_setup_UYVY},       /* FIXME: UYNV? */
  {"YVYU", "YVYU", 16, paint_setup_YVYU},
  {"AYUV", "AYUV", 32, paint_setup_AYUV},

  /* interlaced */
  /*{   "IUYV", "IUY2", 16, paint_setup_YVYU }, */

  /* inverted */
  /*{   "cyuv", "cyuv", 16, paint_setup_YVYU }, */

  /*{   "Y41P", "Y41P", 12, paint_setup_YVYU }, */

  /* interlaced */
  /*{   "IY41", "IY41", 12, paint_setup_YVYU }, */

  /*{   "Y211", "Y211", 8, paint_setup_YVYU }, */

  /*{   "Y41T", "Y41T", 12, paint_setup_YVYU }, */
  /*{   "Y42P", "Y42P", 16, paint_setup_YVYU }, */
  /*{   "CLJR", "CLJR", 8, paint_setup_YVYU }, */
  /*{   "IYU1", "IYU1", 12, paint_setup_YVYU }, */
  {"IYU2", "IYU2", 24, paint_setup_IYU2},

/* planar */
  /* YVU9 */
  {"YVU9", "YVU9", 9, paint_setup_YVU9},
  /* YUV9 */
  {"YUV9", "YUV9", 9, paint_setup_YUV9},
  /* IF09 */
  /* YV12 */
  {"YV12", "YV12", 12, paint_setup_YV12},
  /* I420 */
  {"I420", "I420", 12, paint_setup_I420},
  /* NV12 */
  /* NV21 */
#if 0
  /* IMC1 */
  {"IMC1", "IMC1", 16, paint_setup_IMC1},
  /* IMC2 */
  {"IMC2", "IMC2", 12, paint_setup_IMC2},
  /* IMC3 */
  {"IMC3", "IMC3", 16, paint_setup_IMC3},
  /* IMC4 */
  {"IMC4", "IMC4", 12, paint_setup_IMC4},
#endif
  /* CLPL */
  /* Y41B */
  {"Y41B", "Y41B", 12, paint_setup_Y41B},
  /* Y42B */
  {"Y42B", "Y42B", 16, paint_setup_Y42B},
Wim Taymans's avatar
Wim Taymans committed
145
146
  /* GRAY8 grayscale */
  {"GRAY8", "GRAY8", 8, paint_setup_GRAY8}
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
};

/* returns the size in bytes for one video frame of the given dimensions
 * given the fourcc */
int
fourcc_get_size (struct fourcc_list_struct *fourcc, int w, int h)
{
  paintinfo pi = { NULL, };
  paintinfo *p = &pi;

  p->width = w;
  p->height = h;

  fourcc->paint_setup (p, NULL);

  return (unsigned long) p->endptr;
}

static void
paint_setup_I420 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->ystride = GST_ROUND_UP_4 (p->width);
  p->up = p->yp + p->ystride * GST_ROUND_UP_2 (p->height);
  p->ustride = GST_ROUND_UP_8 (p->width) / 2;
  p->vp = p->up + p->ustride * GST_ROUND_UP_2 (p->height) / 2;
  p->vstride = GST_ROUND_UP_8 (p->ystride) / 2;
  p->endptr = p->vp + p->vstride * GST_ROUND_UP_2 (p->height) / 2;
}

static void
paint_setup_YV12 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->ystride = GST_ROUND_UP_4 (p->width);
  p->vp = p->yp + p->ystride * GST_ROUND_UP_2 (p->height);
  p->vstride = GST_ROUND_UP_8 (p->ystride) / 2;
  p->up = p->vp + p->vstride * GST_ROUND_UP_2 (p->height) / 2;
  p->ustride = GST_ROUND_UP_8 (p->ystride) / 2;
  p->endptr = p->up + p->ustride * GST_ROUND_UP_2 (p->height) / 2;
}

static void
paint_setup_AYUV (paintinfo * p, unsigned char *dest)
{
  p->ap = dest;
  p->yp = dest + 1;
  p->up = dest + 2;
  p->vp = dest + 3;
  p->ystride = p->width * 4;
  p->endptr = dest + p->ystride * p->height;
}

static void
paint_setup_YUY2 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->up = dest + 1;
  p->vp = dest + 3;
  p->ystride = GST_ROUND_UP_2 (p->width) * 2;
  p->endptr = dest + p->ystride * p->height;
}

static void
paint_setup_UYVY (paintinfo * p, unsigned char *dest)
{
  p->yp = dest + 1;
  p->up = dest;
  p->vp = dest + 2;
  p->ystride = GST_ROUND_UP_2 (p->width) * 2;
  p->endptr = dest + p->ystride * p->height;
}

static void
paint_setup_YVYU (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->up = dest + 3;
  p->vp = dest + 1;
  p->ystride = GST_ROUND_UP_2 (p->width) * 2;
  p->endptr = dest + p->ystride * p->height;
}

static void
paint_setup_IYU2 (paintinfo * p, unsigned char *dest)
{
  /* untested */
  p->yp = dest + 1;
  p->up = dest + 0;
  p->vp = dest + 2;
  p->ystride = GST_ROUND_UP_4 (p->width * 3);
  p->endptr = dest + p->ystride * p->height;
}

static void
paint_setup_Y41B (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->ystride = GST_ROUND_UP_4 (p->width);
  p->up = p->yp + p->ystride * p->height;
247
  p->ustride = GST_ROUND_UP_16 (p->width) / 4;
248
  p->vp = p->up + p->ustride * p->height;
249
  p->vstride = GST_ROUND_UP_16 (p->width) / 4;
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  p->endptr = p->vp + p->vstride * p->height;
}

static void
paint_setup_Y42B (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->ystride = GST_ROUND_UP_4 (p->width);
  p->up = p->yp + p->ystride * p->height;
  p->ustride = GST_ROUND_UP_8 (p->width) / 2;
  p->vp = p->up + p->ustride * p->height;
  p->vstride = GST_ROUND_UP_8 (p->width) / 2;
  p->endptr = p->vp + p->vstride * p->height;
}

static void
Wim Taymans's avatar
Wim Taymans committed
266
paint_setup_GRAY8 (paintinfo * p, unsigned char *dest)
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
{
  /* untested */
  p->yp = dest;
  p->ystride = GST_ROUND_UP_4 (p->width);
  p->endptr = dest + p->ystride * p->height;
}

#if 0
static void
paint_setup_IMC1 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->up = dest + p->width * p->height;
  p->vp = dest + p->width * p->height + p->width * p->height / 2;
}

static void
paint_setup_IMC2 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->vp = dest + p->width * p->height;
  p->up = dest + p->width * p->height + p->width / 2;
}

static void
paint_setup_IMC3 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->up = dest + p->width * p->height + p->width * p->height / 2;
  p->vp = dest + p->width * p->height;
}

static void
paint_setup_IMC4 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->vp = dest + p->width * p->height + p->width / 2;
  p->up = dest + p->width * p->height;
}
#endif

static void
paint_setup_YVU9 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->ystride = GST_ROUND_UP_4 (p->width);
David Schleef's avatar
David Schleef committed
313
  p->vp = p->yp + p->ystride * p->height;
314
  p->vstride = GST_ROUND_UP_4 (p->ystride / 4);
David Schleef's avatar
David Schleef committed
315
  p->up = p->vp + p->vstride * (GST_ROUND_UP_4 (p->height) / 4);
316
  p->ustride = GST_ROUND_UP_4 (p->ystride / 4);
David Schleef's avatar
David Schleef committed
317
  p->endptr = p->up + p->ustride * (GST_ROUND_UP_4 (p->height) / 4);
318
319
320
321
322
323
324
}

static void
paint_setup_YUV9 (paintinfo * p, unsigned char *dest)
{
  p->yp = dest;
  p->ystride = GST_ROUND_UP_4 (p->width);
David Schleef's avatar
David Schleef committed
325
  p->up = p->yp + p->ystride * p->height;
326
  p->ustride = GST_ROUND_UP_4 (p->ystride / 4);
David Schleef's avatar
David Schleef committed
327
  p->vp = p->up + p->ustride * (GST_ROUND_UP_4 (p->height) / 4);
328
  p->vstride = GST_ROUND_UP_4 (p->ystride / 4);
David Schleef's avatar
David Schleef committed
329
  p->endptr = p->vp + p->vstride * (GST_ROUND_UP_4 (p->height) / 4);
330
331
332
333
334
335
336
337
338
339
340
}

#define gst_video_format_is_packed video_format_is_packed
static gboolean
video_format_is_packed (GstVideoFormat fmt)
{
  switch (fmt) {
    case GST_VIDEO_FORMAT_I420:
    case GST_VIDEO_FORMAT_YV12:
    case GST_VIDEO_FORMAT_Y41B:
    case GST_VIDEO_FORMAT_Y42B:
Wim Taymans's avatar
Wim Taymans committed
341
    case GST_VIDEO_FORMAT_GRAY8:
342
343
    case GST_VIDEO_FORMAT_YUV9:
    case GST_VIDEO_FORMAT_YVU9:
344
      return FALSE;
345
    case GST_VIDEO_FORMAT_IYU1:
346
    case GST_VIDEO_FORMAT_YUY2:
347
    case GST_VIDEO_FORMAT_YVYU:
348
349
350
351
352
353
354
355
356
357
358
359
    case GST_VIDEO_FORMAT_UYVY:
    case GST_VIDEO_FORMAT_AYUV:
    case GST_VIDEO_FORMAT_RGBx:
    case GST_VIDEO_FORMAT_BGRx:
    case GST_VIDEO_FORMAT_xRGB:
    case GST_VIDEO_FORMAT_xBGR:
    case GST_VIDEO_FORMAT_RGBA:
    case GST_VIDEO_FORMAT_BGRA:
    case GST_VIDEO_FORMAT_ARGB:
    case GST_VIDEO_FORMAT_ABGR:
    case GST_VIDEO_FORMAT_RGB:
    case GST_VIDEO_FORMAT_BGR:
360
    case GST_VIDEO_FORMAT_RGB8P:
361
362
363
364
365
366
367
      return TRUE;
    default:
      g_return_val_if_reached (FALSE);
  }
  return FALSE;
}

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
GST_START_TEST (test_video_formats_all)
{
  GstStructure *s;
  const GValue *val, *list_val;
  GstCaps *caps;
  guint num, n, num_formats;

  num_formats = 100;
  fail_unless (gst_video_format_to_string (num_formats) == NULL);
  while (gst_video_format_to_string (num_formats) == NULL)
    --num_formats;
  GST_INFO ("number of known video formats: %d", num_formats);

  caps = gst_caps_from_string ("video/x-raw, format=" GST_VIDEO_FORMATS_ALL);
  s = gst_caps_get_structure (caps, 0);
  val = gst_structure_get_value (s, "format");
  fail_unless (val != NULL);
  fail_unless (GST_VALUE_HOLDS_LIST (val));
  num = gst_value_list_get_size (val);
  fail_unless (num > 0);
  for (n = 0; n < num; ++n) {
    const gchar *fmt_str;

    list_val = gst_value_list_get_value (val, n);
    fail_unless (G_VALUE_HOLDS_STRING (list_val));
    fmt_str = g_value_get_string (list_val);
    GST_INFO ("format: %s", fmt_str);
    fail_if (gst_video_format_from_string (fmt_str) ==
        GST_VIDEO_FORMAT_UNKNOWN);
  }
398
399
  /* Take into account GST_VIDEO_FORMAT_ENCODED */
  fail_unless_equals_int (num, num_formats - 1);
400
401
402
403
404
405

  gst_caps_unref (caps);
}

GST_END_TEST;

406
407
408
409
410
411
412
413
414
415
416
417
418
419
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#define WIDTH 77
#define HEIGHT 20

GST_START_TEST (test_video_formats_pack_unpack)
{
  guint n, num_formats;

  num_formats = 100;
  fail_unless (gst_video_format_to_string (num_formats) == NULL);
  while (gst_video_format_to_string (num_formats) == NULL)
    --num_formats;
  GST_INFO ("number of known video formats: %d", num_formats);

  for (n = GST_VIDEO_FORMAT_ENCODED + 1; n < num_formats; ++n) {
    const GstVideoFormatInfo *vfinfo, *unpackinfo;
    GstVideoFormat fmt = n;
    GstVideoInfo vinfo;
    gpointer data[GST_VIDEO_MAX_PLANES];
    gint stride[GST_VIDEO_MAX_PLANES];
    guint8 *vdata, *unpack_data;
    gsize vsize, unpack_size;
    guint p;

    GST_INFO ("testing %s", gst_video_format_to_string (fmt));

    vfinfo = gst_video_format_get_info (fmt);
    fail_unless (vfinfo != NULL);

    unpackinfo = gst_video_format_get_info (vfinfo->unpack_format);
    fail_unless (unpackinfo != NULL);

    gst_video_info_init (&vinfo);
    gst_video_info_set_format (&vinfo, fmt, WIDTH, HEIGHT);
    vsize = GST_VIDEO_INFO_SIZE (&vinfo);
    vdata = g_malloc (vsize);
    memset (vdata, 0x99, vsize);

    g_assert (vfinfo->pack_lines == 1);

    unpack_size =
        GST_VIDEO_FORMAT_INFO_BITS (unpackinfo) *
        GST_VIDEO_FORMAT_INFO_N_COMPONENTS (unpackinfo) *
        GST_ROUND_UP_16 (WIDTH);
    unpack_data = g_malloc (unpack_size);

    for (p = 0; p < GST_VIDEO_INFO_N_PLANES (&vinfo); ++p) {
      data[p] = vdata + GST_VIDEO_INFO_PLANE_OFFSET (&vinfo, p);
      stride[p] = GST_VIDEO_INFO_PLANE_STRIDE (&vinfo, p);
    }

    /* now unpack */
    vfinfo->unpack_func (vfinfo, GST_VIDEO_PACK_FLAG_NONE, unpack_data, data,
        stride, 0, 0, WIDTH);

    /* and pack */
    vfinfo->pack_func (vfinfo, GST_VIDEO_PACK_FLAG_NONE, unpack_data,
        unpack_size, data, stride, GST_VIDEO_CHROMA_SITE_UNKNOWN, 0, WIDTH);

    /* now unpack */
    vfinfo->unpack_func (vfinfo, GST_VIDEO_PACK_FLAG_NONE, unpack_data, data,
        stride, 0, HEIGHT - 1, WIDTH);

    /* and pack */
    vfinfo->pack_func (vfinfo, GST_VIDEO_PACK_FLAG_NONE, unpack_data,
        unpack_size, data, stride, GST_VIDEO_CHROMA_SITE_UNKNOWN, HEIGHT - 1,
        WIDTH);

    g_free (unpack_data);
    g_free (vdata);
  }
}

GST_END_TEST;

480
481
482
483
484
GST_START_TEST (test_video_formats)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (fourcc_list); ++i) {
485
    const GstVideoFormatInfo *vf_info;
486
487
488
489
490
491
492
493
494
    GstVideoFormat fmt;
    const gchar *s;
    guint32 fourcc;
    guint w, h;

    s = fourcc_list[i].fourcc;
    fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
    fmt = gst_video_format_from_fourcc (fourcc);

495
496
    if (fmt == GST_VIDEO_FORMAT_UNKNOWN) {
      GST_DEBUG ("Unknown format %s, skipping tests", fourcc_list[i].fourcc);
497
      continue;
498
    }
499

500
501
502
503
504
    vf_info = gst_video_format_get_info (fmt);
    fail_unless (vf_info != NULL);

    fail_unless_equals_int (GST_VIDEO_FORMAT_INFO_FORMAT (vf_info), fmt);

505
506
507
    GST_INFO ("Fourcc %s, packed=%d", fourcc_list[i].fourcc,
        gst_video_format_is_packed (fmt));

508
    fail_unless (GST_VIDEO_FORMAT_INFO_IS_YUV (vf_info));
509
510
511
512
513
514

    /* use any non-NULL pointer so we can compare against NULL */
    {
      paintinfo paintinfo = { 0, };
      fourcc_list[i].paint_setup (&paintinfo, (unsigned char *) s);
      if (paintinfo.ap != NULL) {
515
        fail_unless (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vf_info));
516
      } else {
517
        fail_if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vf_info));
518
519
520
521
522
      }
    }

    for (w = 1; w <= 65; ++w) {
      for (h = 1; h <= 65; ++h) {
523
        GstVideoInfo vinfo;
524
        paintinfo paintinfo = { 0, };
525
        guint off0, off1, off2, off3;
Wim Taymans's avatar
Wim Taymans committed
526
        guint cs0, cs1, cs2, cs3;
527
        guint size;
528
529
530

        GST_LOG ("%s, %dx%d", fourcc_list[i].fourcc, w, h);

531
532
533
        gst_video_info_init (&vinfo);
        gst_video_info_set_format (&vinfo, fmt, w, h);

534
535
536
        paintinfo.width = w;
        paintinfo.height = h;
        fourcc_list[i].paint_setup (&paintinfo, NULL);
537
        fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 0),
538
            paintinfo.ystride);
539
        if (!gst_video_format_is_packed (fmt)
540
            && GST_VIDEO_INFO_N_PLANES (&vinfo) > 2) {
541
          /* planar */
542
          fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 1),
543
              paintinfo.ustride);
544
          fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 2),
545
546
547
              paintinfo.vstride);
          /* check component_width * height against offsets/size somehow? */
        }
548

549
550
551
552
        size = GST_VIDEO_INFO_SIZE (&vinfo);
        off0 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 0);
        off1 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 1);
        off2 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 2);
553

554
555
556
557
        GST_INFO ("size %d <> %d", size, (int) ((guintptr) paintinfo.endptr));
        GST_INFO ("off0 %d <> %d", off0, (int) ((guintptr) paintinfo.yp));
        GST_INFO ("off1 %d <> %d", off1, (int) ((guintptr) paintinfo.up));
        GST_INFO ("off2 %d <> %d", off2, (int) ((guintptr) paintinfo.vp));
Wim Taymans's avatar
Wim Taymans committed
558

559
560
561
        fail_unless_equals_int (size, (unsigned long) paintinfo.endptr);
        fail_unless_equals_int (off0, (unsigned long) paintinfo.yp);
        fail_unless_equals_int (off1, (unsigned long) paintinfo.up);
562
563
564
        fail_unless_equals_int (off2, (unsigned long) paintinfo.vp);

        /* should be 0 if there's no alpha component */
565
        off3 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 3);
566
567
        fail_unless_equals_int (off3, (unsigned long) paintinfo.ap);

Wim Taymans's avatar
Wim Taymans committed
568
569
570
571
572
573
574
575
576
        cs0 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 0) *
            GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 0);
        cs1 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 1) *
            GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 1);
        cs2 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 2) *
            GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 2);

        /* GST_LOG ("cs0=%d,cs1=%d,cs2=%d,off0=%d,off1=%d,off2=%d,size=%d",
           cs0, cs1, cs2, off0, off1, off2, size); */
577

Wim Taymans's avatar
Wim Taymans committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
        if (!gst_video_format_is_packed (fmt))
          fail_unless (cs0 <= off1);

        if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vinfo.finfo)) {
          cs3 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 3) *
              GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 2);
          fail_unless (cs3 < size);
          /* U/V/alpha shouldn't take up more space than the Y component */
          fail_if (cs1 > cs0, "cs1 (%d) should be <= cs0 (%d)", cs1, cs0);
          fail_if (cs2 > cs0, "cs2 (%d) should be <= cs0 (%d)", cs2, cs0);
          fail_if (cs3 > cs0, "cs3 (%d) should be <= cs0 (%d)", cs3, cs0);

          /* all components together shouldn't take up more space than size */
          fail_unless (cs0 + cs1 + cs2 + cs3 <= size);
        } else {
          /* U/V shouldn't take up more space than the Y component */
          fail_if (cs1 > cs0, "cs1 (%d) should be <= cs0 (%d)", cs1, cs0);
          fail_if (cs2 > cs0, "cs2 (%d) should be <= cs0 (%d)", cs2, cs0);

          /* all components together shouldn't take up more space than size */
          fail_unless (cs0 + cs1 + cs2 <= size,
              "cs0 (%d) + cs1 (%d) + cs2 (%d) should be <= size (%d)",
              cs0, cs1, cs2, size);
601
        }
602
603
604
605
606
607
608
      }
    }
  }
}

GST_END_TEST;

609
610
GST_START_TEST (test_video_formats_rgb)
{
611
  GstVideoInfo vinfo;
612
  gint width, height, framerate_n, framerate_d, par_n, par_d;
613
  GstCaps *caps;
614
615
  GstStructure *structure;

616
  gst_video_info_init (&vinfo);
617
618
619
620
621
622
  gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_RGB, 800, 600);
  vinfo.par_n = 1;
  vinfo.par_d = 1;
  vinfo.fps_n = 0;
  vinfo.fps_d = 1;
  caps = gst_video_info_to_caps (&vinfo);
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
  structure = gst_caps_get_structure (caps, 0);

  fail_unless (gst_structure_get_int (structure, "width", &width));
  fail_unless (gst_structure_get_int (structure, "height", &height));
  fail_unless (gst_structure_get_fraction (structure, "framerate", &framerate_n,
          &framerate_d));
  fail_unless (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
          &par_n, &par_d));

  fail_unless (width == 800);
  fail_unless (height == 600);
  fail_unless (framerate_n == 0);
  fail_unless (framerate_d == 1);
  fail_unless (par_n == 1);
  fail_unless (par_d == 1);

  gst_caps_unref (caps);
}

GST_END_TEST;

644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
GST_START_TEST (test_dar_calc)
{
  guint display_ratio_n, display_ratio_d;

  /* Ensure that various Display Ratio calculations are correctly done */
  /* video 768x576, par 16/15, display par 16/15 = 4/3 */
  fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
          &display_ratio_d, 768, 576, 16, 15, 16, 15));
  fail_unless (display_ratio_n == 4 && display_ratio_d == 3);

  /* video 720x480, par 32/27, display par 1/1 = 16/9 */
  fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
          &display_ratio_d, 720, 480, 32, 27, 1, 1));
  fail_unless (display_ratio_n == 16 && display_ratio_d == 9);

  /* video 360x288, par 533333/500000, display par 16/15 = 
   * dar 1599999/1600000 */
  fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
          &display_ratio_d, 360, 288, 533333, 500000, 16, 15));
  fail_unless (display_ratio_n == 1599999 && display_ratio_d == 1280000);
}

GST_END_TEST;

668
669
670
671
672
673
674
675
676
GST_START_TEST (test_parse_caps_rgb)
{
  struct
  {
    const gchar *tmpl_caps_string;
    GstVideoFormat fmt;
  } formats[] = {
    /* 24 bit */
    {
677
678
    GST_VIDEO_CAPS_MAKE ("RGB"), GST_VIDEO_FORMAT_RGB}, {
    GST_VIDEO_CAPS_MAKE ("BGR"), GST_VIDEO_FORMAT_BGR},
679
680
        /* 32 bit (no alpha) */
    {
681
682
683
684
    GST_VIDEO_CAPS_MAKE ("RGBx"), GST_VIDEO_FORMAT_RGBx}, {
    GST_VIDEO_CAPS_MAKE ("xRGB"), GST_VIDEO_FORMAT_xRGB}, {
    GST_VIDEO_CAPS_MAKE ("BGRx"), GST_VIDEO_FORMAT_BGRx}, {
    GST_VIDEO_CAPS_MAKE ("xBGR"), GST_VIDEO_FORMAT_xBGR},
685
686
        /* 32 bit (with alpha) */
    {
687
688
689
    GST_VIDEO_CAPS_MAKE ("RGBA"), GST_VIDEO_FORMAT_RGBA}, {
    GST_VIDEO_CAPS_MAKE ("ARGB"), GST_VIDEO_FORMAT_ARGB}, {
    GST_VIDEO_CAPS_MAKE ("BGRA"), GST_VIDEO_FORMAT_BGRA}, {
Wim Taymans's avatar
Wim Taymans committed
690
    GST_VIDEO_CAPS_MAKE ("ABGR"), GST_VIDEO_FORMAT_ABGR},
691
692
        /* 16 bit */
    {
Wim Taymans's avatar
Wim Taymans committed
693
694
695
696
    GST_VIDEO_CAPS_MAKE ("RGB16"), GST_VIDEO_FORMAT_RGB16}, {
    GST_VIDEO_CAPS_MAKE ("BGR16"), GST_VIDEO_FORMAT_BGR16}, {
    GST_VIDEO_CAPS_MAKE ("RGB15"), GST_VIDEO_FORMAT_RGB15}, {
    GST_VIDEO_CAPS_MAKE ("BGR15"), GST_VIDEO_FORMAT_BGR15}
697
698
699
700
  };
  gint i;

  for (i = 0; i < G_N_ELEMENTS (formats); ++i) {
701
    GstVideoInfo vinfo;
702
703
704
    GstCaps *caps, *caps2;

    caps = gst_caps_from_string (formats[i].tmpl_caps_string);
705
706
    gst_caps_set_simple (caps, "width", G_TYPE_INT, 2 * (i + 1), "height",
        G_TYPE_INT, i + 1, "framerate", GST_TYPE_FRACTION, 15, 1,
707
        "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
708
709
        "interlace-mode", G_TYPE_STRING, "progressive",
        "colorimetry", G_TYPE_STRING, "1:1:0:0", NULL);
710
711
712
713
    g_assert (gst_caps_is_fixed (caps));

    GST_DEBUG ("testing caps: %" GST_PTR_FORMAT, caps);

714
715
716
717
718
    gst_video_info_init (&vinfo);
    fail_unless (gst_video_info_from_caps (&vinfo, caps));
    fail_unless_equals_int (GST_VIDEO_INFO_FORMAT (&vinfo), formats[i].fmt);
    fail_unless_equals_int (GST_VIDEO_INFO_WIDTH (&vinfo), 2 * (i + 1));
    fail_unless_equals_int (GST_VIDEO_INFO_HEIGHT (&vinfo), i + 1);
719
720

    /* make sure they're serialised back correctly */
721
    caps2 = gst_video_info_to_caps (&vinfo);
722
    fail_unless (caps != NULL);
723
724
725
    fail_unless (gst_caps_is_equal (caps, caps2),
        "caps [%" GST_PTR_FORMAT "] not equal to caps2 [%" GST_PTR_FORMAT "]",
        caps, caps2);
726
727
728
729
730
731
732
733

    gst_caps_unref (caps);
    gst_caps_unref (caps2);
  }
}

GST_END_TEST;

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
GST_START_TEST (test_events)
{
  GstEvent *e;
  gboolean in_still;

  e = gst_video_event_new_still_frame (TRUE);
  fail_if (e == NULL, "Failed to create still frame event");
  fail_unless (gst_video_event_parse_still_frame (e, &in_still),
      "Failed to parse still frame event");
  fail_unless (gst_video_event_parse_still_frame (e, NULL),
      "Failed to parse still frame event w/ in_still == NULL");
  fail_unless (in_still == TRUE);
  gst_event_unref (e);

  e = gst_video_event_new_still_frame (FALSE);
  fail_if (e == NULL, "Failed to create still frame event");
  fail_unless (gst_video_event_parse_still_frame (e, &in_still),
      "Failed to parse still frame event");
  fail_unless (gst_video_event_parse_still_frame (e, NULL),
      "Failed to parse still frame event w/ in_still == NULL");
  fail_unless (in_still == FALSE);
  gst_event_unref (e);
}

GST_END_TEST;

760
761
GST_START_TEST (test_convert_frame)
{
762
  GstVideoInfo vinfo;
763
  GstCaps *from_caps, *to_caps;
764
765
  GstBuffer *from_buffer;
  GstSample *from_sample, *to_sample;
766
767
  GError *error = NULL;
  gint i;
Wim Taymans's avatar
Wim Taymans committed
768
  GstMapInfo map;
769

770
771
  gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);

772
773
  from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);

Wim Taymans's avatar
Wim Taymans committed
774
  gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
775
  for (i = 0; i < 640 * 480; i++) {
Wim Taymans's avatar
Wim Taymans committed
776
777
778
779
    map.data[4 * i + 0] = 0;    /* x */
    map.data[4 * i + 1] = 255;  /* R */
    map.data[4 * i + 2] = 0;    /* G */
    map.data[4 * i + 3] = 0;    /* B */
780
  }
Wim Taymans's avatar
Wim Taymans committed
781
  gst_buffer_unmap (from_buffer, &map);
Wim Taymans's avatar
Wim Taymans committed
782

783
784
785
786
787
788
789
  gst_video_info_init (&vinfo);
  gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640, 480);
  vinfo.fps_n = 25;
  vinfo.fps_d = 1;
  vinfo.par_n = 1;
  vinfo.par_d = 1;
  from_caps = gst_video_info_to_caps (&vinfo);
790

791
792
  from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);

793
794
795
796
  to_caps =
      gst_caps_from_string
      ("something/that, does=(string)not, exist=(boolean)FALSE");

797
798
  to_sample =
      gst_video_convert_sample (from_sample, to_caps,
799
      GST_CLOCK_TIME_NONE, &error);
800
  fail_if (to_sample != NULL);
801
802
803
804
805
  fail_unless (error != NULL);
  g_error_free (error);
  error = NULL;

  gst_caps_unref (to_caps);
806
807
808
809
810
811
812
  gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420, 240, 320);
  vinfo.fps_n = 25;
  vinfo.fps_d = 1;
  vinfo.par_n = 1;
  vinfo.par_d = 2;
  to_caps = gst_video_info_to_caps (&vinfo);

813
814
  to_sample =
      gst_video_convert_sample (from_sample, to_caps,
815
      GST_CLOCK_TIME_NONE, &error);
816
  fail_unless (to_sample != NULL);
817
818
819
820
  fail_unless (error == NULL);

  gst_buffer_unref (from_buffer);
  gst_caps_unref (from_caps);
821
822
  gst_sample_unref (from_sample);
  gst_sample_unref (to_sample);
823
824
825
826
827
  gst_caps_unref (to_caps);
}

GST_END_TEST;

828
829
830
typedef struct
{
  GMainLoop *loop;
831
  GstSample *sample;
832
833
834
835
  GError *error;
} ConvertFrameContext;

static void
836
convert_sample_async_callback (GstSample * sample, GError * err,
837
838
    ConvertFrameContext * cf_data)
{
839
  cf_data->sample = sample;
840
841
842
843
844
845
846
  cf_data->error = err;

  g_main_loop_quit (cf_data->loop);
}

GST_START_TEST (test_convert_frame_async)
{
847
  GstVideoInfo vinfo;
848
849
  GstCaps *from_caps, *to_caps;
  GstBuffer *from_buffer;
850
  GstSample *from_sample;
851
  gint i;
Wim Taymans's avatar
Wim Taymans committed
852
  GstMapInfo map;
853
854
855
  GMainLoop *loop;
  ConvertFrameContext cf_data = { NULL, NULL, NULL };

856
857
  gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);

858
859
  from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);

Wim Taymans's avatar
Wim Taymans committed
860
  gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
861
  for (i = 0; i < 640 * 480; i++) {
Wim Taymans's avatar
Wim Taymans committed
862
863
864
865
    map.data[4 * i + 0] = 0;    /* x */
    map.data[4 * i + 1] = 255;  /* R */
    map.data[4 * i + 2] = 0;    /* G */
    map.data[4 * i + 3] = 0;    /* B */
866
  }
Wim Taymans's avatar
Wim Taymans committed
867
  gst_buffer_unmap (from_buffer, &map);
Wim Taymans's avatar
Wim Taymans committed
868

869
870
871
872
873
874
875
  gst_video_info_init (&vinfo);
  gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640, 470);
  vinfo.par_n = 1;
  vinfo.par_d = 1;
  vinfo.fps_n = 25;
  vinfo.fps_d = 1;
  from_caps = gst_video_info_to_caps (&vinfo);
876
877
878
879
880
881
882

  to_caps =
      gst_caps_from_string
      ("something/that, does=(string)not, exist=(boolean)FALSE");

  loop = cf_data.loop = g_main_loop_new (NULL, FALSE);

883
884
885
886
887
  from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);
  gst_buffer_unref (from_buffer);
  gst_caps_unref (from_caps);

  gst_video_convert_sample_async (from_sample, to_caps,
888
      GST_CLOCK_TIME_NONE,
889
      (GstVideoConvertSampleCallback) convert_sample_async_callback, &cf_data,
890
      NULL);
891
892
893

  g_main_loop_run (loop);

894
  fail_if (cf_data.sample != NULL);
895
896
897
898
899
  fail_unless (cf_data.error != NULL);
  g_error_free (cf_data.error);
  cf_data.error = NULL;

  gst_caps_unref (to_caps);
900
901
902
903
904
905
906
  gst_video_info_init (&vinfo);
  gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420, 240, 320);
  vinfo.par_n = 1;
  vinfo.par_d = 2;
  vinfo.fps_n = 25;
  vinfo.fps_d = 1;
  to_caps = gst_video_info_to_caps (&vinfo);
907
  gst_video_convert_sample_async (from_sample, to_caps,
908
      GST_CLOCK_TIME_NONE,
909
      (GstVideoConvertSampleCallback) convert_sample_async_callback, &cf_data,
910
      NULL);
911
  g_main_loop_run (loop);
912
  fail_unless (cf_data.sample != NULL);
913
914
  fail_unless (cf_data.error == NULL);

915
  gst_sample_unref (cf_data.sample);
916
  gst_caps_unref (to_caps);
917
  gst_sample_unref (from_sample);
918
919
920
921
922
923

  g_main_loop_unref (loop);
}

GST_END_TEST;

924
925
GST_START_TEST (test_video_size_from_caps)
{
926
927
  GstVideoInfo vinfo;
  GstCaps *caps;
Wim Taymans's avatar
Wim Taymans committed
928

929
  caps = gst_caps_new_simple ("video/x-raw",
Wim Taymans's avatar
Wim Taymans committed
930
      "format", G_TYPE_STRING, "YV12",
931
      "width", G_TYPE_INT, 640,
932
      "height", G_TYPE_INT, 480, "framerate", GST_TYPE_FRACTION, 25, 1, NULL);
933

934
935
936
  gst_video_info_init (&vinfo);
  fail_unless (gst_video_info_from_caps (&vinfo, caps));
  fail_unless (GST_VIDEO_INFO_SIZE (&vinfo) == (640 * 480 * 12 / 8));
937
938
939
940
941
942

  gst_caps_unref (caps);
}

GST_END_TEST;

943
944
945
946
GST_START_TEST (test_overlay_composition)
{
  GstVideoOverlayComposition *comp1, *comp2;
  GstVideoOverlayRectangle *rect1, *rect2;
947
  GstVideoOverlayCompositionMeta *ometa;
948
  GstBuffer *pix1, *pix2, *buf;
949
  GstVideoMeta *vmeta;
950
  guint seq1, seq2;
951
952
  guint w, h, stride;
  gint x, y;
953
  guint8 val;
954
955

  pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
956
  gst_buffer_memset (pix1, 0, 0, gst_buffer_get_size (pix1));
957

958
959
  gst_buffer_add_video_meta (pix1, GST_VIDEO_FRAME_FLAG_NONE,
      GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, 200, 50);
960
  rect1 = gst_video_overlay_rectangle_new_raw (pix1,
961
962
963
964
965
966
967
968
969
970
      600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);

  gst_buffer_unref (pix1);
  pix1 = NULL;

  comp1 = gst_video_overlay_composition_new (rect1);
  fail_unless (gst_video_overlay_composition_n_rectangles (comp1) == 1);
  fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 0) == rect1);
  fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 1) == NULL);

971
972
973
974
975
  /* rectangle was created first, sequence number should be smaller */
  seq1 = gst_video_overlay_rectangle_get_seqnum (rect1);
  seq2 = gst_video_overlay_composition_get_seqnum (comp1);
  fail_unless (seq1 < seq2);

976
977
978
979
980
981
982
983
984
985
986
987
988
  /* composition took own ref, so refcount is 2 now, so this should fail */
  ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 50,
          600, 300, 50));

  /* drop our ref, so refcount is 1 (we know it will continue to be valid) */
  gst_video_overlay_rectangle_unref (rect1);
  gst_video_overlay_rectangle_set_render_rectangle (rect1, 50, 600, 300, 50);

  comp2 = gst_video_overlay_composition_new (rect1);
  fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1);
  fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect1);
  fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL);

989
990
991
  fail_unless (seq1 < gst_video_overlay_composition_get_seqnum (comp2));
  fail_unless (seq2 < gst_video_overlay_composition_get_seqnum (comp2));

992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
  /* now refcount is 2 again because comp2 has also taken a ref, so must fail */
  ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 0,
          0, 1, 1));

  /* this should make a copy of the rectangles so drop the original
   * second ref on rect1 */
  comp2 = gst_video_overlay_composition_make_writable (comp2);
  gst_video_overlay_rectangle_set_render_rectangle (rect1, 51, 601, 301, 51);

  rect2 = gst_video_overlay_composition_get_rectangle (comp2, 0);
  fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1);
  fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect2);
  fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL);
  fail_unless (rect1 != rect2);

  gst_video_overlay_composition_add_rectangle (comp1, rect2);
  gst_video_overlay_composition_ref (comp1);
  ASSERT_CRITICAL (gst_video_overlay_composition_add_rectangle (comp1, rect2));
  gst_video_overlay_composition_unref (comp1);

  /* make sure the copy really worked */
  gst_video_overlay_rectangle_get_render_rectangle (rect1, &x, &y, &w, &h);
  fail_unless_equals_int (x, 51);
  fail_unless_equals_int (y, 601);
  fail_unless_equals_int (w, 301);
  fail_unless_equals_int (h, 51);

  /* get scaled pixbuf and touch last byte */
1020
  pix1 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1021
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1022
  stride = 4 * w;
1023
1024
  fail_unless (gst_buffer_get_size (pix1) > ((h - 1) * stride + (w * 4) - 1),
      "size %u vs. last pixel offset %u", gst_buffer_get_size (pix1),
1025
      ((h - 1) * stride + (w * 4) - 1));
1026
1027
  gst_buffer_extract (pix1, ((h - 1) * stride + (w * 4) - 1), &val, 1);
  fail_unless_equals_int (val, 0);
1028
1029
1030
1031
1032
1033
1034
1035

  gst_video_overlay_rectangle_get_render_rectangle (rect2, &x, &y, &w, &h);
  fail_unless_equals_int (x, 50);
  fail_unless_equals_int (y, 600);
  fail_unless_equals_int (w, 300);
  fail_unless_equals_int (h, 50);

  /* get scaled pixbuf and touch last byte */
1036
  pix2 = gst_video_overlay_rectangle_get_pixels_raw (rect2,
1037
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1038
  stride = 4 * w;
1039
1040
  fail_unless (gst_buffer_get_size (pix2) > ((h - 1) * stride + (w * 4) - 1),
      "size %u vs. last pixel offset %u", gst_buffer_get_size (pix1),
1041
      ((h - 1) * stride + (w * 4) - 1));
1042
1043
  gst_buffer_extract (pix2, ((h - 1) * stride + (w * 4) - 1), &val, 1);
  fail_unless_equals_int (val, 0);
1044
1045

  /* get scaled pixbuf again, should be the same buffer as before (caching) */
1046
  pix1 = gst_video_overlay_rectangle_get_pixels_raw (rect2,
1047
1048
1049
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_unless (pix1 == pix2);

1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
  /* get in different format */
  pix1 = gst_video_overlay_rectangle_get_pixels_ayuv (rect2,
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_unless (pix1 != pix2);
  /* get it again, should be same (caching) */
  pix2 = gst_video_overlay_rectangle_get_pixels_ayuv (rect2,
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_unless (pix1 == pix2);
  /* get unscaled, should be different */
  pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_ayuv (rect2,
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_unless (pix1 != pix2);
  /* but should be cached */
  pix1 = gst_video_overlay_rectangle_get_pixels_unscaled_ayuv (rect2,
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_unless (pix1 == pix2);

  vmeta = gst_buffer_get_video_meta (pix1);
  fail_unless (vmeta != NULL);
  w = vmeta->width;
  h = vmeta->height;
  fail_unless_equals_int (w, 200);
  fail_unless_equals_int (h, 50);
  fail_unless_equals_int (vmeta->format,
      GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV);
  fail_unless (gst_buffer_get_size (pix1) == w * h * 4);
  gst_buffer_extract (pix1, 0, &seq1, 4);
  fail_unless (seq1 != 0);

1079
  /* now compare the original unscaled ones */
1080
  pix1 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1081
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1082
  pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect2,
1083
1084
1085
1086
1087
1088
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);

  vmeta = gst_buffer_get_video_meta (pix2);
  fail_unless (vmeta != NULL);
  w = vmeta->width;
  h = vmeta->height;
1089
1090
1091
1092
1093

  /* the original pixel buffers should be identical */
  fail_unless (pix1 == pix2);
  fail_unless_equals_int (w, 200);
  fail_unless_equals_int (h, 50);
1094
  stride = 4 * w;
1095
1096

  /* touch last byte */
1097
1098
  fail_unless (gst_buffer_get_size (pix1) > ((h - 1) * stride + (w * 4) - 1),
      "size %u vs. last pixel offset %u", gst_buffer_get_size (pix1),
1099
      ((h - 1) * stride + (w * 4) - 1));
1100
1101
  gst_buffer_extract (pix1, ((h - 1) * stride + (w * 4) - 1), &val, 1);
  fail_unless_equals_int (val, 0);
1102
1103
1104

  /* test attaching and retrieving of compositions to/from buffers */
  buf = gst_buffer_new ();
1105
  fail_unless (gst_buffer_get_video_overlay_composition_meta (buf) == NULL);
1106
1107

  gst_buffer_ref (buf);
1108
1109
1110
1111
  /* buffer now has refcount of 2, so its metadata is not writable.
   * only check this if we are not running in valgrind, as it leaks */
#ifdef HAVE_VALGRIND
  if (!RUNNING_ON_VALGRIND) {
1112
1113
    ASSERT_CRITICAL (gst_buffer_add_video_overlay_composition_meta (buf,
            comp1));
1114
1115
  }
#endif
1116
  gst_buffer_unref (buf);
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
  gst_buffer_add_video_overlay_composition_meta (buf, comp1);
  ometa = gst_buffer_get_video_overlay_composition_meta (buf);
  fail_unless (ometa != NULL);
  fail_unless (ometa->overlay == comp1);
  fail_unless (gst_buffer_remove_video_overlay_composition_meta (buf, ometa));
  gst_buffer_add_video_overlay_composition_meta (buf, comp2);
  ometa = gst_buffer_get_video_overlay_composition_meta (buf);
  fail_unless (ometa->overlay == comp2);
  fail_unless (gst_buffer_remove_video_overlay_composition_meta (buf, ometa));
  fail_unless (gst_buffer_get_video_overlay_composition_meta (buf) == NULL);
1127
1128

  /* make sure the buffer cleans up its composition ref when unreffed */
1129
  gst_buffer_add_video_overlay_composition_meta (buf, comp2);
1130
1131
1132
1133
1134
1135
1136
1137
  gst_buffer_unref (buf);

  gst_video_overlay_composition_unref (comp2);
  gst_video_overlay_composition_unref (comp1);
}

GST_END_TEST;

1138
1139
1140
GST_START_TEST (test_overlay_composition_premultiplied_alpha)
{
  GstVideoOverlayRectangle *rect1;
1141
  GstVideoMeta *vmeta;
1142
1143
1144
  GstBuffer *pix1, *pix2, *pix3, *pix4, *pix5;
  GstBuffer *pix6, *pix7, *pix8, *pix9, *pix10;
  guint8 *data5, *data7;
1145
  guint w, h, w2, h2;
1146
  GstMapInfo map;
1147
1148

  pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
1149
  gst_buffer_memset (pix1, 0, 0x80, gst_buffer_get_size (pix1));
1150

1151
1152
  gst_buffer_add_video_meta (pix1, GST_VIDEO_FRAME_FLAG_NONE,
      GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, 200, 50);
1153
  rect1 = gst_video_overlay_rectangle_new_raw (pix1,
1154
1155
1156
1157
      600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  gst_buffer_unref (pix1);

  /* same flags, unscaled, should be the same buffer */
1158
  pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1159
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1160
1161
1162
  fail_unless (pix1 == pix2);

  /* same flags, but scaled */
1163
  pix3 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1164
1165
1166
1167
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_if (pix3 == pix1 || pix3 == pix2);

  /* same again, should hopefully get the same (cached) buffer as before */
1168
  pix4 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1169
1170
1171
1172
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_unless (pix4 == pix3);

  /* just to update the vars */
1173
  pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1174
1175
1176
1177
1178
1179
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);

  vmeta = gst_buffer_get_video_meta (pix2);
  fail_unless (vmeta != NULL);
  w = vmeta->width;
  h = vmeta->height;
1180
1181

  /* now, let's try to get premultiplied alpha from the unpremultiplied input */
1182
  pix5 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1183
      GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1184
  fail_if (pix5 == pix1 || pix5 == pix2 || pix5 == pix3);
1185
1186
1187
1188
  vmeta = gst_buffer_get_video_meta (pix5);
  fail_unless (vmeta != NULL);
  w2 = vmeta->width;
  h2 = vmeta->height;
1189
1190
  fail_unless_equals_int (w, w2);
  fail_unless_equals_int (h, h2);
1191
1192
1193
1194
  fail_unless_equals_int (gst_buffer_get_size (pix2),
      gst_buffer_get_size (pix5));
  gst_buffer_map (pix5, &map, GST_MAP_READ);
  fail_if (gst_buffer_memcmp (pix2, 0, map.data, map.size) == 0);
1195
  /* make sure it actually did what we expected it to do (input=0x80808080) */
1196
  data5 = map.data;
1197
1198
1199
1200
1201
1202
1203
1204
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
  /* B - G - R - A */
  fail_unless_equals_int (data5[0], 0x40);
  fail_unless_equals_int (data5[1], 0x40);
  fail_unless_equals_int (data5[2], 0x40);
  fail_unless_equals_int (data5[3], 0x80);
#else
  /* A - R - G - B */
1205
  fail_unless_equals_int (data5[0], 0x80);
1206
1207
  fail_unless_equals_int (data5[1], 0x40);
  fail_unless_equals_int (data5[2], 0x40);
1208
  fail_unless_equals_int (data5[3], 0x40);
1209
#endif
1210
  gst_buffer_unmap (pix5, &map);
1211
1212
1213

  /* same again, now we should be getting back the same buffer as before,
   * as it should have been cached */
1214
  pix6 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1215
      GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1216
1217
1218
  fail_unless (pix6 == pix5);

  /* just to update the stride var */
1219
  pix3 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1220
1221
1222
1223
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_unless (pix3 == pix4);

  /* now try to get scaled premultiplied alpha from unpremultiplied input */
1224
  pix7 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1225
1226
1227
      GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
  fail_if (pix7 == pix1 || pix7 == pix2 || pix7 == pix3 || pix7 == pix5);

1228
1229
  gst_buffer_map (pix7, &map, GST_MAP_READ);
  data7 = map.data;
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
  /* make sure it actually did what we expected it to do (input=0x80808080)
   * hoping that the scaling didn't mess up our values */
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
  /* B - G - R - A */
  fail_unless_equals_int (data7[0], 0x40);
  fail_unless_equals_int (data7[1], 0x40);
  fail_unless_equals_int (data7[2], 0x40);
  fail_unless_equals_int (data7[3], 0x80);
#else
  /* A - R - G - B */
1240
  fail_unless_equals_int (data7[0], 0x80);
1241
1242
  fail_unless_equals_int (data7[1], 0x40);
  fail_unless_equals_int (data7[2], 0x40);
1243
  fail_unless_equals_int (data7[3], 0x40);
1244
#endif
1245
  gst_buffer_unmap (pix7, &map);
1246
1247

  /* and the same again, it should be cached now */
1248
  pix8 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1249
1250
1251
1252
      GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
  fail_unless (pix8 == pix7);

  /* make sure other cached stuff is still there */
1253
  pix9 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1254
1255
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_unless (pix9 == pix3);
1256
  pix10 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1257
      GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1258
1259
1260
1261
1262
  fail_unless (pix10 == pix5);

  gst_video_overlay_rectangle_unref (rect1);
}

1263
1264
GST_END_TEST;

1265
1266
1267
1268
GST_START_TEST (test_overlay_composition_global_alpha)
{
  GstVideoOverlayRectangle *rect1;
  GstBuffer *pix1, *pix2, *pix3, *pix4, *pix5;
1269
  GstVideoMeta *vmeta;
1270
  guint8 *data2, *data4, *data5;
1271
  guint w, h, w4, h4;
1272
1273
1274
  guint seq1, seq2;
  gfloat ga1, ga2;
  GstVideoOverlayFormatFlags flags1;
1275
  GstMapInfo map;
1276
1277

  pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
1278
  gst_buffer_memset (pix1, 0, 0x80, gst_buffer_get_size (pix1));
1279

1280
1281
  gst_buffer_add_video_meta (pix1, GST_VIDEO_FRAME_FLAG_NONE,
      GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, 200, 50);
1282
  rect1 = gst_video_overlay_rectangle_new_raw (pix1,
1283
1284
1285
1286
      600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  gst_buffer_unref (pix1);

  /* same flags, unscaled, should be the same buffer */
1287
  pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1288
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1289
1290
  fail_unless (pix1 == pix2);

1291
1292
1293
1294
1295
  vmeta = gst_buffer_get_video_meta (pix2);
  fail_unless (vmeta != NULL);
  w = vmeta->width;
  h = vmeta->height;

1296
  /* same flags, but scaled */
1297
  pix3 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1298
1299
1300
1301
      GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
  fail_if (pix3 == pix1 || pix3 == pix2);

  /* get unscaled premultiplied data, new cached rectangle should be created */
1302
  pix4 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1303 <