1 /* GStreamer unit test for video
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>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 # include <valgrind/valgrind.h>
34 #include <gst/check/gstcheck.h>
36 #include <gst/video/video.h>
38 #include <gst/video/video-overlay-composition.h>
42 /* These are from the current/old videotestsrc; we check our new public API
43 * in libgstvideo against the old one to make sure the sizes and offsets
46 typedef struct paintinfo_struct paintinfo;
47 struct paintinfo_struct
49 unsigned char *dest; /* pointer to first byte of video data */
50 unsigned char *yp, *up, *vp; /* pointers to first byte of each component
51 * for both packed/planar YUV and RGB */
52 unsigned char *ap; /* pointer to first byte of alpha component */
53 unsigned char *endptr; /* pointer to byte beyond last video data */
61 struct fourcc_list_struct
66 void (*paint_setup) (paintinfo * p, unsigned char *dest);
69 static void paint_setup_I420 (paintinfo * p, unsigned char *dest);
70 static void paint_setup_YV12 (paintinfo * p, unsigned char *dest);
71 static void paint_setup_YUY2 (paintinfo * p, unsigned char *dest);
72 static void paint_setup_UYVY (paintinfo * p, unsigned char *dest);
73 static void paint_setup_YVYU (paintinfo * p, unsigned char *dest);
74 static void paint_setup_IYU2 (paintinfo * p, unsigned char *dest);
75 static void paint_setup_Y41B (paintinfo * p, unsigned char *dest);
76 static void paint_setup_Y42B (paintinfo * p, unsigned char *dest);
77 static void paint_setup_GRAY8 (paintinfo * p, unsigned char *dest);
78 static void paint_setup_AYUV (paintinfo * p, unsigned char *dest);
81 static void paint_setup_IMC1 (paintinfo * p, unsigned char *dest);
82 static void paint_setup_IMC2 (paintinfo * p, unsigned char *dest);
83 static void paint_setup_IMC3 (paintinfo * p, unsigned char *dest);
84 static void paint_setup_IMC4 (paintinfo * p, unsigned char *dest);
86 static void paint_setup_YUV9 (paintinfo * p, unsigned char *dest);
87 static void paint_setup_YVU9 (paintinfo * p, unsigned char *dest);
89 int fourcc_get_size (struct fourcc_list_struct *fourcc, int w, int h);
91 struct fourcc_list_struct fourcc_list[] = {
93 {"YUY2", "YUY2", 16, paint_setup_YUY2},
94 {"UYVY", "UYVY", 16, paint_setup_UYVY},
95 {"Y422", "Y422", 16, paint_setup_UYVY},
96 {"UYNV", "UYNV", 16, paint_setup_UYVY}, /* FIXME: UYNV? */
97 {"YVYU", "YVYU", 16, paint_setup_YVYU},
98 {"AYUV", "AYUV", 32, paint_setup_AYUV},
101 /*{ "IUYV", "IUY2", 16, paint_setup_YVYU }, */
104 /*{ "cyuv", "cyuv", 16, paint_setup_YVYU }, */
106 /*{ "Y41P", "Y41P", 12, paint_setup_YVYU }, */
109 /*{ "IY41", "IY41", 12, paint_setup_YVYU }, */
111 /*{ "Y211", "Y211", 8, paint_setup_YVYU }, */
113 /*{ "Y41T", "Y41T", 12, paint_setup_YVYU }, */
114 /*{ "Y42P", "Y42P", 16, paint_setup_YVYU }, */
115 /*{ "CLJR", "CLJR", 8, paint_setup_YVYU }, */
116 /*{ "IYU1", "IYU1", 12, paint_setup_YVYU }, */
117 {"IYU2", "IYU2", 24, paint_setup_IYU2},
121 {"YVU9", "YVU9", 9, paint_setup_YVU9},
123 {"YUV9", "YUV9", 9, paint_setup_YUV9},
126 {"YV12", "YV12", 12, paint_setup_YV12},
128 {"I420", "I420", 12, paint_setup_I420},
133 {"IMC1", "IMC1", 16, paint_setup_IMC1},
135 {"IMC2", "IMC2", 12, paint_setup_IMC2},
137 {"IMC3", "IMC3", 16, paint_setup_IMC3},
139 {"IMC4", "IMC4", 12, paint_setup_IMC4},
143 {"Y41B", "Y41B", 12, paint_setup_Y41B},
145 {"Y42B", "Y42B", 16, paint_setup_Y42B},
146 /* GRAY8 grayscale */
147 {"GRAY8", "GRAY8", 8, paint_setup_GRAY8}
150 /* returns the size in bytes for one video frame of the given dimensions
151 * given the fourcc */
153 fourcc_get_size (struct fourcc_list_struct *fourcc, int w, int h)
155 paintinfo pi = { NULL, };
161 fourcc->paint_setup (p, NULL);
163 return (unsigned long) p->endptr;
167 paint_setup_I420 (paintinfo * p, unsigned char *dest)
170 p->ystride = GST_ROUND_UP_4 (p->width);
171 p->up = p->yp + p->ystride * GST_ROUND_UP_2 (p->height);
172 p->ustride = GST_ROUND_UP_8 (p->width) / 2;
173 p->vp = p->up + p->ustride * GST_ROUND_UP_2 (p->height) / 2;
174 p->vstride = GST_ROUND_UP_8 (p->ystride) / 2;
175 p->endptr = p->vp + p->vstride * GST_ROUND_UP_2 (p->height) / 2;
179 paint_setup_YV12 (paintinfo * p, unsigned char *dest)
182 p->ystride = GST_ROUND_UP_4 (p->width);
183 p->vp = p->yp + p->ystride * GST_ROUND_UP_2 (p->height);
184 p->vstride = GST_ROUND_UP_8 (p->ystride) / 2;
185 p->up = p->vp + p->vstride * GST_ROUND_UP_2 (p->height) / 2;
186 p->ustride = GST_ROUND_UP_8 (p->ystride) / 2;
187 p->endptr = p->up + p->ustride * GST_ROUND_UP_2 (p->height) / 2;
191 paint_setup_AYUV (paintinfo * p, unsigned char *dest)
197 p->ystride = p->width * 4;
198 p->endptr = dest + p->ystride * p->height;
202 paint_setup_YUY2 (paintinfo * p, unsigned char *dest)
207 p->ystride = GST_ROUND_UP_2 (p->width) * 2;
208 p->endptr = dest + p->ystride * p->height;
212 paint_setup_UYVY (paintinfo * p, unsigned char *dest)
217 p->ystride = GST_ROUND_UP_2 (p->width) * 2;
218 p->endptr = dest + p->ystride * p->height;
222 paint_setup_YVYU (paintinfo * p, unsigned char *dest)
227 p->ystride = GST_ROUND_UP_2 (p->width) * 2;
228 p->endptr = dest + p->ystride * p->height;
232 paint_setup_IYU2 (paintinfo * p, unsigned char *dest)
238 p->ystride = GST_ROUND_UP_4 (p->width * 3);
239 p->endptr = dest + p->ystride * p->height;
243 paint_setup_Y41B (paintinfo * p, unsigned char *dest)
246 p->ystride = GST_ROUND_UP_4 (p->width);
247 p->up = p->yp + p->ystride * p->height;
248 p->ustride = GST_ROUND_UP_16 (p->width) / 4;
249 p->vp = p->up + p->ustride * p->height;
250 p->vstride = GST_ROUND_UP_16 (p->width) / 4;
251 p->endptr = p->vp + p->vstride * p->height;
255 paint_setup_Y42B (paintinfo * p, unsigned char *dest)
258 p->ystride = GST_ROUND_UP_4 (p->width);
259 p->up = p->yp + p->ystride * p->height;
260 p->ustride = GST_ROUND_UP_8 (p->width) / 2;
261 p->vp = p->up + p->ustride * p->height;
262 p->vstride = GST_ROUND_UP_8 (p->width) / 2;
263 p->endptr = p->vp + p->vstride * p->height;
267 paint_setup_GRAY8 (paintinfo * p, unsigned char *dest)
271 p->ystride = GST_ROUND_UP_4 (p->width);
272 p->endptr = dest + p->ystride * p->height;
277 paint_setup_IMC1 (paintinfo * p, unsigned char *dest)
280 p->up = dest + p->width * p->height;
281 p->vp = dest + p->width * p->height + p->width * p->height / 2;
285 paint_setup_IMC2 (paintinfo * p, unsigned char *dest)
288 p->vp = dest + p->width * p->height;
289 p->up = dest + p->width * p->height + p->width / 2;
293 paint_setup_IMC3 (paintinfo * p, unsigned char *dest)
296 p->up = dest + p->width * p->height + p->width * p->height / 2;
297 p->vp = dest + p->width * p->height;
301 paint_setup_IMC4 (paintinfo * p, unsigned char *dest)
304 p->vp = dest + p->width * p->height + p->width / 2;
305 p->up = dest + p->width * p->height;
310 paint_setup_YVU9 (paintinfo * p, unsigned char *dest)
313 p->ystride = GST_ROUND_UP_4 (p->width);
314 p->vp = p->yp + p->ystride * p->height;
315 p->vstride = GST_ROUND_UP_4 (p->ystride / 4);
316 p->up = p->vp + p->vstride * (GST_ROUND_UP_4 (p->height) / 4);
317 p->ustride = GST_ROUND_UP_4 (p->ystride / 4);
318 p->endptr = p->up + p->ustride * (GST_ROUND_UP_4 (p->height) / 4);
322 paint_setup_YUV9 (paintinfo * p, unsigned char *dest)
325 p->ystride = GST_ROUND_UP_4 (p->width);
326 p->up = p->yp + p->ystride * p->height;
327 p->ustride = GST_ROUND_UP_4 (p->ystride / 4);
328 p->vp = p->up + p->ustride * (GST_ROUND_UP_4 (p->height) / 4);
329 p->vstride = GST_ROUND_UP_4 (p->ystride / 4);
330 p->endptr = p->vp + p->vstride * (GST_ROUND_UP_4 (p->height) / 4);
333 #define gst_video_format_is_packed video_format_is_packed
335 video_format_is_packed (GstVideoFormat fmt)
338 case GST_VIDEO_FORMAT_I420:
339 case GST_VIDEO_FORMAT_YV12:
340 case GST_VIDEO_FORMAT_Y41B:
341 case GST_VIDEO_FORMAT_Y42B:
342 case GST_VIDEO_FORMAT_GRAY8:
343 case GST_VIDEO_FORMAT_YUV9:
344 case GST_VIDEO_FORMAT_YVU9:
346 case GST_VIDEO_FORMAT_IYU1:
347 case GST_VIDEO_FORMAT_YUY2:
348 case GST_VIDEO_FORMAT_YVYU:
349 case GST_VIDEO_FORMAT_UYVY:
350 case GST_VIDEO_FORMAT_AYUV:
351 case GST_VIDEO_FORMAT_RGBx:
352 case GST_VIDEO_FORMAT_BGRx:
353 case GST_VIDEO_FORMAT_xRGB:
354 case GST_VIDEO_FORMAT_xBGR:
355 case GST_VIDEO_FORMAT_RGBA:
356 case GST_VIDEO_FORMAT_BGRA:
357 case GST_VIDEO_FORMAT_ARGB:
358 case GST_VIDEO_FORMAT_ABGR:
359 case GST_VIDEO_FORMAT_RGB:
360 case GST_VIDEO_FORMAT_BGR:
361 case GST_VIDEO_FORMAT_RGB8_PALETTED:
364 g_return_val_if_reached (FALSE);
369 GST_START_TEST (test_video_formats_all)
372 const GValue *val, *list_val;
374 guint num, n, num_formats;
377 fail_unless (gst_video_format_to_string (num_formats) == NULL);
378 while (gst_video_format_to_string (num_formats) == NULL)
380 GST_INFO ("number of known video formats: %d", num_formats);
382 caps = gst_caps_from_string ("video/x-raw, format=" GST_VIDEO_FORMATS_ALL);
383 s = gst_caps_get_structure (caps, 0);
384 val = gst_structure_get_value (s, "format");
385 fail_unless (val != NULL);
386 fail_unless (GST_VALUE_HOLDS_LIST (val));
387 num = gst_value_list_get_size (val);
388 fail_unless (num > 0);
389 for (n = 0; n < num; ++n) {
390 const gchar *fmt_str;
392 list_val = gst_value_list_get_value (val, n);
393 fail_unless (G_VALUE_HOLDS_STRING (list_val));
394 fmt_str = g_value_get_string (list_val);
395 GST_INFO ("format: %s", fmt_str);
396 fail_if (gst_video_format_from_string (fmt_str) ==
397 GST_VIDEO_FORMAT_UNKNOWN);
399 fail_unless_equals_int (num, num_formats);
401 gst_caps_unref (caps);
406 GST_START_TEST (test_video_formats)
410 for (i = 0; i < G_N_ELEMENTS (fourcc_list); ++i) {
411 const GstVideoFormatInfo *vf_info;
417 s = fourcc_list[i].fourcc;
418 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
419 fmt = gst_video_format_from_fourcc (fourcc);
421 if (fmt == GST_VIDEO_FORMAT_UNKNOWN) {
422 GST_DEBUG ("Unknown format %s, skipping tests", fourcc_list[i].fourcc);
426 vf_info = gst_video_format_get_info (fmt);
427 fail_unless (vf_info != NULL);
429 fail_unless_equals_int (GST_VIDEO_FORMAT_INFO_FORMAT (vf_info), fmt);
431 GST_INFO ("Fourcc %s, packed=%d", fourcc_list[i].fourcc,
432 gst_video_format_is_packed (fmt));
434 fail_unless (GST_VIDEO_FORMAT_INFO_IS_YUV (vf_info));
436 /* use any non-NULL pointer so we can compare against NULL */
438 paintinfo paintinfo = { 0, };
439 fourcc_list[i].paint_setup (&paintinfo, (unsigned char *) s);
440 if (paintinfo.ap != NULL) {
441 fail_unless (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vf_info));
443 fail_if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vf_info));
447 for (w = 1; w <= 65; ++w) {
448 for (h = 1; h <= 65; ++h) {
450 paintinfo paintinfo = { 0, };
451 guint off0, off1, off2, off3;
452 guint cs0, cs1, cs2, cs3;
455 GST_LOG ("%s, %dx%d", fourcc_list[i].fourcc, w, h);
457 gst_video_info_init (&vinfo);
458 gst_video_info_set_format (&vinfo, fmt, w, h);
461 paintinfo.height = h;
462 fourcc_list[i].paint_setup (&paintinfo, NULL);
463 fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 0),
465 if (!gst_video_format_is_packed (fmt)
466 && !GST_VIDEO_INFO_N_PLANES (&vinfo) > 2) {
468 fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 1),
470 fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 2),
472 /* check component_width * height against offsets/size somehow? */
475 size = GST_VIDEO_INFO_SIZE (&vinfo);
476 off0 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 0);
477 off1 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 1);
478 off2 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 2);
480 GST_INFO ("size %d <> %d", size, paintinfo.endptr);
481 GST_INFO ("off0 %d <> %d", off0, paintinfo.yp);
482 GST_INFO ("off1 %d <> %d", off1, paintinfo.up);
483 GST_INFO ("off2 %d <> %d", off2, paintinfo.vp);
485 fail_unless_equals_int (size, (unsigned long) paintinfo.endptr);
486 fail_unless_equals_int (off0, (unsigned long) paintinfo.yp);
487 fail_unless_equals_int (off1, (unsigned long) paintinfo.up);
488 fail_unless_equals_int (off2, (unsigned long) paintinfo.vp);
490 /* should be 0 if there's no alpha component */
491 off3 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 3);
492 fail_unless_equals_int (off3, (unsigned long) paintinfo.ap);
494 cs0 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 0) *
495 GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 0);
496 cs1 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 1) *
497 GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 1);
498 cs2 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 2) *
499 GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 2);
501 /* GST_LOG ("cs0=%d,cs1=%d,cs2=%d,off0=%d,off1=%d,off2=%d,size=%d",
502 cs0, cs1, cs2, off0, off1, off2, size); */
504 if (!gst_video_format_is_packed (fmt))
505 fail_unless (cs0 <= off1);
507 if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vinfo.finfo)) {
508 cs3 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 3) *
509 GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 2);
510 fail_unless (cs3 < size);
511 /* U/V/alpha shouldn't take up more space than the Y component */
512 fail_if (cs1 > cs0, "cs1 (%d) should be <= cs0 (%d)", cs1, cs0);
513 fail_if (cs2 > cs0, "cs2 (%d) should be <= cs0 (%d)", cs2, cs0);
514 fail_if (cs3 > cs0, "cs3 (%d) should be <= cs0 (%d)", cs3, cs0);
516 /* all components together shouldn't take up more space than size */
517 fail_unless (cs0 + cs1 + cs2 + cs3 <= size);
519 /* U/V shouldn't take up more space than the Y component */
520 fail_if (cs1 > cs0, "cs1 (%d) should be <= cs0 (%d)", cs1, cs0);
521 fail_if (cs2 > cs0, "cs2 (%d) should be <= cs0 (%d)", cs2, cs0);
523 /* all components together shouldn't take up more space than size */
524 fail_unless (cs0 + cs1 + cs2 <= size,
525 "cs0 (%d) + cs1 (%d) + cs2 (%d) should be <= size (%d)",
526 cs0, cs1, cs2, size);
535 GST_START_TEST (test_video_formats_rgb)
538 gint width, height, framerate_n, framerate_d, par_n, par_d;
540 GstStructure *structure;
542 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_RGB, 800, 600);
547 caps = gst_video_info_to_caps (&vinfo);
548 structure = gst_caps_get_structure (caps, 0);
550 fail_unless (gst_structure_get_int (structure, "width", &width));
551 fail_unless (gst_structure_get_int (structure, "height", &height));
552 fail_unless (gst_structure_get_fraction (structure, "framerate", &framerate_n,
554 fail_unless (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
557 fail_unless (width == 800);
558 fail_unless (height == 600);
559 fail_unless (framerate_n == 0);
560 fail_unless (framerate_d == 1);
561 fail_unless (par_n == 1);
562 fail_unless (par_d == 1);
564 gst_caps_unref (caps);
569 GST_START_TEST (test_dar_calc)
571 guint display_ratio_n, display_ratio_d;
573 /* Ensure that various Display Ratio calculations are correctly done */
574 /* video 768x576, par 16/15, display par 16/15 = 4/3 */
575 fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
576 &display_ratio_d, 768, 576, 16, 15, 16, 15));
577 fail_unless (display_ratio_n == 4 && display_ratio_d == 3);
579 /* video 720x480, par 32/27, display par 1/1 = 16/9 */
580 fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
581 &display_ratio_d, 720, 480, 32, 27, 1, 1));
582 fail_unless (display_ratio_n == 16 && display_ratio_d == 9);
584 /* video 360x288, par 533333/500000, display par 16/15 =
585 * dar 1599999/1600000 */
586 fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
587 &display_ratio_d, 360, 288, 533333, 500000, 16, 15));
588 fail_unless (display_ratio_n == 1599999 && display_ratio_d == 1280000);
593 GST_START_TEST (test_parse_caps_rgb)
597 const gchar *tmpl_caps_string;
602 GST_VIDEO_CAPS_MAKE ("RGB"), GST_VIDEO_FORMAT_RGB}, {
603 GST_VIDEO_CAPS_MAKE ("BGR"), GST_VIDEO_FORMAT_BGR},
604 /* 32 bit (no alpha) */
606 GST_VIDEO_CAPS_MAKE ("RGBx"), GST_VIDEO_FORMAT_RGBx}, {
607 GST_VIDEO_CAPS_MAKE ("xRGB"), GST_VIDEO_FORMAT_xRGB}, {
608 GST_VIDEO_CAPS_MAKE ("BGRx"), GST_VIDEO_FORMAT_BGRx}, {
609 GST_VIDEO_CAPS_MAKE ("xBGR"), GST_VIDEO_FORMAT_xBGR},
610 /* 32 bit (with alpha) */
612 GST_VIDEO_CAPS_MAKE ("RGBA"), GST_VIDEO_FORMAT_RGBA}, {
613 GST_VIDEO_CAPS_MAKE ("ARGB"), GST_VIDEO_FORMAT_ARGB}, {
614 GST_VIDEO_CAPS_MAKE ("BGRA"), GST_VIDEO_FORMAT_BGRA}, {
615 GST_VIDEO_CAPS_MAKE ("ABGR"), GST_VIDEO_FORMAT_ABGR},
618 GST_VIDEO_CAPS_MAKE ("RGB16"), GST_VIDEO_FORMAT_RGB16}, {
619 GST_VIDEO_CAPS_MAKE ("BGR16"), GST_VIDEO_FORMAT_BGR16}, {
620 GST_VIDEO_CAPS_MAKE ("RGB15"), GST_VIDEO_FORMAT_RGB15}, {
621 GST_VIDEO_CAPS_MAKE ("BGR15"), GST_VIDEO_FORMAT_BGR15}
625 for (i = 0; i < G_N_ELEMENTS (formats); ++i) {
627 GstCaps *caps, *caps2;
629 caps = gst_caps_from_string (formats[i].tmpl_caps_string);
630 gst_caps_set_simple (caps, "width", G_TYPE_INT, 2 * (i + 1), "height",
631 G_TYPE_INT, i + 1, "framerate", GST_TYPE_FRACTION, 15, 1,
632 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
633 "interlace-mode", G_TYPE_STRING, "progressive", NULL);
634 g_assert (gst_caps_is_fixed (caps));
636 GST_DEBUG ("testing caps: %" GST_PTR_FORMAT, caps);
638 gst_video_info_init (&vinfo);
639 fail_unless (gst_video_info_from_caps (&vinfo, caps));
640 fail_unless_equals_int (GST_VIDEO_INFO_FORMAT (&vinfo), formats[i].fmt);
641 fail_unless_equals_int (GST_VIDEO_INFO_WIDTH (&vinfo), 2 * (i + 1));
642 fail_unless_equals_int (GST_VIDEO_INFO_HEIGHT (&vinfo), i + 1);
644 /* make sure they're serialised back correctly */
645 caps2 = gst_video_info_to_caps (&vinfo);
646 fail_unless (caps != NULL);
647 fail_unless (gst_caps_is_equal (caps, caps2),
648 "caps [%" GST_PTR_FORMAT "] not equal to caps2 [%" GST_PTR_FORMAT "]",
651 gst_caps_unref (caps);
652 gst_caps_unref (caps2);
658 GST_START_TEST (test_events)
663 e = gst_video_event_new_still_frame (TRUE);
664 fail_if (e == NULL, "Failed to create still frame event");
665 fail_unless (gst_video_event_parse_still_frame (e, &in_still),
666 "Failed to parse still frame event");
667 fail_unless (gst_video_event_parse_still_frame (e, NULL),
668 "Failed to parse still frame event w/ in_still == NULL");
669 fail_unless (in_still == TRUE);
672 e = gst_video_event_new_still_frame (FALSE);
673 fail_if (e == NULL, "Failed to create still frame event");
674 fail_unless (gst_video_event_parse_still_frame (e, &in_still),
675 "Failed to parse still frame event");
676 fail_unless (gst_video_event_parse_still_frame (e, NULL),
677 "Failed to parse still frame event w/ in_still == NULL");
678 fail_unless (in_still == FALSE);
684 GST_START_TEST (test_convert_frame)
687 GstCaps *from_caps, *to_caps;
688 GstBuffer *from_buffer;
689 GstSample *from_sample, *to_sample;
690 GError *error = NULL;
694 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
696 from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);
698 gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
699 for (i = 0; i < 640 * 480; i++) {
700 map.data[4 * i + 0] = 0; /* x */
701 map.data[4 * i + 1] = 255; /* R */
702 map.data[4 * i + 2] = 0; /* G */
703 map.data[4 * i + 3] = 0; /* B */
705 gst_buffer_unmap (from_buffer, &map);
707 gst_video_info_init (&vinfo);
708 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640, 480);
713 from_caps = gst_video_info_to_caps (&vinfo);
715 from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);
719 ("something/that, does=(string)not, exist=(boolean)FALSE");
722 gst_video_convert_sample (from_sample, to_caps,
723 GST_CLOCK_TIME_NONE, &error);
724 fail_if (to_sample != NULL);
725 fail_unless (error != NULL);
726 g_error_free (error);
729 gst_caps_unref (to_caps);
730 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420, 240, 320);
735 to_caps = gst_video_info_to_caps (&vinfo);
738 gst_video_convert_sample (from_sample, to_caps,
739 GST_CLOCK_TIME_NONE, &error);
740 fail_unless (to_sample != NULL);
741 fail_unless (error == NULL);
743 gst_buffer_unref (from_buffer);
744 gst_caps_unref (from_caps);
745 gst_sample_unref (from_sample);
746 gst_sample_unref (to_sample);
747 gst_caps_unref (to_caps);
757 } ConvertFrameContext;
760 convert_sample_async_callback (GstSample * sample, GError * err,
761 ConvertFrameContext * cf_data)
763 cf_data->sample = sample;
764 cf_data->error = err;
766 g_main_loop_quit (cf_data->loop);
769 GST_START_TEST (test_convert_frame_async)
772 GstCaps *from_caps, *to_caps;
773 GstBuffer *from_buffer;
774 GstSample *from_sample;
778 ConvertFrameContext cf_data = { NULL, NULL, NULL };
780 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
782 from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);
784 gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
785 for (i = 0; i < 640 * 480; i++) {
786 map.data[4 * i + 0] = 0; /* x */
787 map.data[4 * i + 1] = 255; /* R */
788 map.data[4 * i + 2] = 0; /* G */
789 map.data[4 * i + 3] = 0; /* B */
791 gst_buffer_unmap (from_buffer, &map);
793 gst_video_info_init (&vinfo);
794 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640, 470);
799 from_caps = gst_video_info_to_caps (&vinfo);
803 ("something/that, does=(string)not, exist=(boolean)FALSE");
805 loop = cf_data.loop = g_main_loop_new (NULL, FALSE);
807 from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);
808 gst_buffer_unref (from_buffer);
809 gst_caps_unref (from_caps);
811 gst_video_convert_sample_async (from_sample, to_caps,
813 (GstVideoConvertSampleCallback) convert_sample_async_callback, &cf_data,
816 g_main_loop_run (loop);
818 fail_if (cf_data.sample != NULL);
819 fail_unless (cf_data.error != NULL);
820 g_error_free (cf_data.error);
821 cf_data.error = NULL;
823 gst_caps_unref (to_caps);
824 gst_video_info_init (&vinfo);
825 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420, 240, 320);
830 to_caps = gst_video_info_to_caps (&vinfo);
831 gst_video_convert_sample_async (from_sample, to_caps,
833 (GstVideoConvertSampleCallback) convert_sample_async_callback, &cf_data,
835 g_main_loop_run (loop);
836 fail_unless (cf_data.sample != NULL);
837 fail_unless (cf_data.error == NULL);
839 gst_sample_unref (cf_data.sample);
840 gst_caps_unref (to_caps);
842 g_main_loop_unref (loop);
847 GST_START_TEST (test_video_size_from_caps)
852 caps = gst_caps_new_simple ("video/x-raw",
853 "format", G_TYPE_STRING, "YV12",
854 "width", G_TYPE_INT, 640,
855 "height", G_TYPE_INT, 480, "framerate", GST_TYPE_FRACTION, 25, 1, NULL);
857 gst_video_info_init (&vinfo);
858 fail_unless (gst_video_info_from_caps (&vinfo, caps));
859 fail_unless (GST_VIDEO_INFO_SIZE (&vinfo) == (640 * 480 * 12 / 8));
861 gst_caps_unref (caps);
867 /* FIXME 0.11: port overlay composition to buffer meta */
868 GST_START_TEST (test_overlay_composition)
870 GstVideoOverlayComposition *comp1, *comp2;
871 GstVideoOverlayRectangle *rect1, *rect2;
872 GstBuffer *pix1, *pix2, *buf;
877 pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
878 memset (GST_BUFFER_DATA (pix1), 0, GST_BUFFER_SIZE (pix1));
880 rect1 = gst_video_overlay_rectangle_new_argb (pix1, 200, 50, 200 * 4,
881 600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
883 gst_buffer_unref (pix1);
886 comp1 = gst_video_overlay_composition_new (rect1);
887 fail_unless (gst_video_overlay_composition_n_rectangles (comp1) == 1);
888 fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 0) == rect1);
889 fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 1) == NULL);
891 /* rectangle was created first, sequence number should be smaller */
892 seq1 = gst_video_overlay_rectangle_get_seqnum (rect1);
893 seq2 = gst_video_overlay_composition_get_seqnum (comp1);
894 fail_unless (seq1 < seq2);
896 /* composition took own ref, so refcount is 2 now, so this should fail */
897 ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 50,
900 /* drop our ref, so refcount is 1 (we know it will continue to be valid) */
901 gst_video_overlay_rectangle_unref (rect1);
902 gst_video_overlay_rectangle_set_render_rectangle (rect1, 50, 600, 300, 50);
904 comp2 = gst_video_overlay_composition_new (rect1);
905 fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1);
906 fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect1);
907 fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL);
909 fail_unless (seq1 < gst_video_overlay_composition_get_seqnum (comp2));
910 fail_unless (seq2 < gst_video_overlay_composition_get_seqnum (comp2));
912 /* now refcount is 2 again because comp2 has also taken a ref, so must fail */
913 ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 0,
916 /* this should make a copy of the rectangles so drop the original
917 * second ref on rect1 */
918 comp2 = gst_video_overlay_composition_make_writable (comp2);
919 gst_video_overlay_rectangle_set_render_rectangle (rect1, 51, 601, 301, 51);
921 rect2 = gst_video_overlay_composition_get_rectangle (comp2, 0);
922 fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1);
923 fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect2);
924 fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL);
925 fail_unless (rect1 != rect2);
927 gst_video_overlay_composition_add_rectangle (comp1, rect2);
928 gst_video_overlay_composition_ref (comp1);
929 ASSERT_CRITICAL (gst_video_overlay_composition_add_rectangle (comp1, rect2));
930 gst_video_overlay_composition_unref (comp1);
932 /* make sure the copy really worked */
933 gst_video_overlay_rectangle_get_render_rectangle (rect1, &x, &y, &w, &h);
934 fail_unless_equals_int (x, 51);
935 fail_unless_equals_int (y, 601);
936 fail_unless_equals_int (w, 301);
937 fail_unless_equals_int (h, 51);
939 /* get scaled pixbuf and touch last byte */
940 pix1 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
941 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
942 fail_unless (GST_BUFFER_SIZE (pix1) > ((h - 1) * stride + (w * 4) - 1),
943 "size %u vs. last pixel offset %u", GST_BUFFER_SIZE (pix1),
944 ((h - 1) * stride + (w * 4) - 1));
945 fail_unless_equals_int (*(GST_BUFFER_DATA (pix1) + ((h - 1) * stride +
948 gst_video_overlay_rectangle_get_render_rectangle (rect2, &x, &y, &w, &h);
949 fail_unless_equals_int (x, 50);
950 fail_unless_equals_int (y, 600);
951 fail_unless_equals_int (w, 300);
952 fail_unless_equals_int (h, 50);
954 /* get scaled pixbuf and touch last byte */
955 pix2 = gst_video_overlay_rectangle_get_pixels_argb (rect2, &stride,
956 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
957 fail_unless (GST_BUFFER_SIZE (pix2) > ((h - 1) * stride + (w * 4) - 1),
958 "size %u vs. last pixel offset %u", GST_BUFFER_SIZE (pix1),
959 ((h - 1) * stride + (w * 4) - 1));
960 fail_unless_equals_int (*(GST_BUFFER_DATA (pix2) + ((h - 1) * stride +
963 /* get scaled pixbuf again, should be the same buffer as before (caching) */
964 pix1 = gst_video_overlay_rectangle_get_pixels_argb (rect2, &stride,
965 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
966 fail_unless (pix1 == pix2);
968 /* now compare the original unscaled ones */
969 pix1 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
970 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
971 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect2, &w, &h,
972 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
974 /* the original pixel buffers should be identical */
975 fail_unless (pix1 == pix2);
976 fail_unless_equals_int (w, 200);
977 fail_unless_equals_int (h, 50);
979 /* touch last byte */
980 fail_unless (GST_BUFFER_SIZE (pix1) > ((h - 1) * stride + (w * 4) - 1),
981 "size %u vs. last pixel offset %u", GST_BUFFER_SIZE (pix1),
982 ((h - 1) * stride + (w * 4) - 1));
983 fail_unless_equals_int (*(GST_BUFFER_DATA (pix1) + ((h - 1) * stride +
986 /* test attaching and retrieving of compositions to/from buffers */
987 buf = gst_buffer_new ();
988 fail_unless (gst_video_buffer_get_overlay_composition (buf) == NULL);
990 gst_buffer_ref (buf);
991 /* buffer now has refcount of 2, so its metadata is not writable.
992 * only check this if we are not running in valgrind, as it leaks */
994 if (!RUNNING_ON_VALGRIND) {
995 ASSERT_CRITICAL (gst_video_buffer_set_overlay_composition (buf, comp1));
998 gst_buffer_unref (buf);
999 gst_video_buffer_set_overlay_composition (buf, comp1);
1000 fail_unless (gst_video_buffer_get_overlay_composition (buf) == comp1);
1001 gst_video_buffer_set_overlay_composition (buf, comp2);
1002 fail_unless (gst_video_buffer_get_overlay_composition (buf) == comp2);
1003 gst_video_buffer_set_overlay_composition (buf, NULL);
1004 fail_unless (gst_video_buffer_get_overlay_composition (buf) == NULL);
1006 /* make sure the buffer cleans up its composition ref when unreffed */
1007 gst_video_buffer_set_overlay_composition (buf, comp2);
1008 gst_buffer_unref (buf);
1010 gst_video_overlay_composition_unref (comp2);
1011 gst_video_overlay_composition_unref (comp1);
1016 GST_START_TEST (test_overlay_composition_premultiplied_alpha)
1018 GstVideoOverlayRectangle *rect1;
1019 GstBuffer *pix1, *pix2, *pix3, *pix4, *pix5;
1020 GstBuffer *pix6, *pix7, *pix8, *pix9, *pix10;
1021 guint8 *data5, *data7;
1022 guint w, h, stride, w2, h2, stride2;
1024 pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
1025 memset (GST_BUFFER_DATA (pix1), 0x80, GST_BUFFER_SIZE (pix1));
1027 rect1 = gst_video_overlay_rectangle_new_argb (pix1, 200, 50, 200 * 4,
1028 600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1029 gst_buffer_unref (pix1);
1031 /* same flags, unscaled, should be the same buffer */
1032 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1033 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1034 fail_unless (pix1 == pix2);
1036 /* same flags, but scaled */
1037 pix3 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1038 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1039 fail_if (pix3 == pix1 || pix3 == pix2);
1041 /* same again, should hopefully get the same (cached) buffer as before */
1042 pix4 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1043 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1044 fail_unless (pix4 == pix3);
1046 /* just to update the vars */
1047 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1048 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1050 /* now, let's try to get premultiplied alpha from the unpremultiplied input */
1051 pix5 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w2, &h2,
1052 &stride2, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1053 fail_if (pix5 == pix1 || pix5 == pix2 || pix5 == pix3);
1054 fail_unless_equals_int (stride, stride2);
1055 fail_unless_equals_int (w, w2);
1056 fail_unless_equals_int (h, h2);
1057 fail_unless_equals_int (GST_BUFFER_SIZE (pix2), GST_BUFFER_SIZE (pix5));
1058 data5 = GST_BUFFER_DATA (pix5);
1059 fail_if (memcmp (data5, GST_BUFFER_DATA (pix2), GST_BUFFER_SIZE (pix5)) == 0);
1061 /* make sure it actually did what we expected it to do (input=0x80808080) */
1062 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1064 fail_unless_equals_int (data5[0], 0x40);
1065 fail_unless_equals_int (data5[1], 0x40);
1066 fail_unless_equals_int (data5[2], 0x40);
1067 fail_unless_equals_int (data5[3], 0x80);
1070 fail_unless_equals_int (data5[0], 0x40);
1071 fail_unless_equals_int (data5[1], 0x40);
1072 fail_unless_equals_int (data5[2], 0x40);
1073 fail_unless_equals_int (data5[3], 0x80);
1076 /* same again, now we should be getting back the same buffer as before,
1077 * as it should have been cached */
1078 pix6 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w2, &h2,
1079 &stride2, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1080 fail_unless (pix6 == pix5);
1082 /* just to update the stride var */
1083 pix3 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1084 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1085 fail_unless (pix3 == pix4);
1087 /* now try to get scaled premultiplied alpha from unpremultiplied input */
1088 pix7 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride2,
1089 GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1090 fail_if (pix7 == pix1 || pix7 == pix2 || pix7 == pix3 || pix7 == pix5);
1091 fail_unless_equals_int (stride, stride2);
1093 data7 = GST_BUFFER_DATA (pix7);
1094 /* make sure it actually did what we expected it to do (input=0x80808080)
1095 * hoping that the scaling didn't mess up our values */
1096 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1098 fail_unless_equals_int (data7[0], 0x40);
1099 fail_unless_equals_int (data7[1], 0x40);
1100 fail_unless_equals_int (data7[2], 0x40);
1101 fail_unless_equals_int (data7[3], 0x80);
1104 fail_unless_equals_int (data7[0], 0x40);
1105 fail_unless_equals_int (data7[1], 0x40);
1106 fail_unless_equals_int (data7[2], 0x40);
1107 fail_unless_equals_int (data7[3], 0x80);
1110 /* and the same again, it should be cached now */
1111 pix8 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride2,
1112 GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1113 fail_unless (pix8 == pix7);
1115 /* make sure other cached stuff is still there */
1116 pix9 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1117 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1118 fail_unless (pix9 == pix3);
1119 pix10 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w2, &h2,
1120 &stride2, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1121 fail_unless (pix10 == pix5);
1123 gst_video_overlay_rectangle_unref (rect1);
1130 GST_START_TEST (test_overlay_composition_global_alpha)
1132 GstVideoOverlayRectangle *rect1;
1133 GstBuffer *pix1, *pix2, *pix3, *pix4, *pix5;
1134 guint8 *data2, *data4, *data5;
1135 guint w, h, stride, stride3, w4, h4, stride4, stride5;
1138 GstVideoOverlayFormatFlags flags1;
1140 pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
1141 memset (GST_BUFFER_DATA (pix1), 0x80, GST_BUFFER_SIZE (pix1));
1143 rect1 = gst_video_overlay_rectangle_new_argb (pix1, 200, 50, 200 * 4,
1144 600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1145 gst_buffer_unref (pix1);
1147 /* same flags, unscaled, should be the same buffer */
1148 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1149 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1150 fail_unless (pix1 == pix2);
1152 /* same flags, but scaled */
1153 pix3 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride3,
1154 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1155 fail_if (pix3 == pix1 || pix3 == pix2);
1157 /* get unscaled premultiplied data, new cached rectangle should be created */
1158 pix4 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w4, &h4,
1159 &stride4, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1160 fail_if (pix4 == pix2 || pix4 == pix3);
1161 fail_unless_equals_int (stride, stride4);
1162 fail_unless_equals_int (w, w4);
1163 fail_unless_equals_int (h, h4);
1164 fail_unless_equals_int (GST_BUFFER_SIZE (pix2), GST_BUFFER_SIZE (pix4));
1165 data4 = GST_BUFFER_DATA (pix4);
1166 fail_if (memcmp (data4, GST_BUFFER_DATA (pix1), GST_BUFFER_SIZE (pix1)) == 0);
1167 /* make sure it actually did what we expected it to do (input=0x80808080) */
1168 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1170 fail_unless_equals_int (data4[0], 0x40);
1171 fail_unless_equals_int (data4[1], 0x40);
1172 fail_unless_equals_int (data4[2], 0x40);
1173 fail_unless_equals_int (data4[3], 0x80);
1176 fail_unless_equals_int (data4[0], 0x80);
1177 fail_unless_equals_int (data4[1], 0x40);
1178 fail_unless_equals_int (data4[2], 0x40);
1179 fail_unless_equals_int (data4[3], 0x40);
1182 /* now premultiplied and scaled, again a new cached rectangle should be cached */
1183 pix5 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride5,
1184 GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1185 fail_if (pix5 == pix2 || pix5 == pix3 || pix5 == pix4);
1186 /* stride and size should be equal to the first scaled rect */
1187 fail_unless_equals_int (stride5, stride3);
1188 fail_unless_equals_int (GST_BUFFER_SIZE (pix3), GST_BUFFER_SIZE (pix3));
1189 data5 = GST_BUFFER_DATA (pix5);
1190 /* data should be different (premutliplied) though */
1191 fail_if (memcmp (data5, GST_BUFFER_DATA (pix3), GST_BUFFER_SIZE (pix3)) == 0);
1192 /* make sure it actually did what we expected it to do (input=0x80808080) */
1193 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1195 fail_unless_equals_int (data5[0], 0x40);
1196 fail_unless_equals_int (data5[1], 0x40);
1197 fail_unless_equals_int (data5[2], 0x40);
1198 fail_unless_equals_int (data5[3], 0x80);
1201 fail_unless_equals_int (data5[0], 0x80);
1202 fail_unless_equals_int (data5[1], 0x40);
1203 fail_unless_equals_int (data5[2], 0x40);
1204 fail_unless_equals_int (data5[3], 0x40);
1207 /* global_alpha should initially be 1.0 */
1208 ga1 = gst_video_overlay_rectangle_get_global_alpha (rect1);
1209 fail_unless_equals_float (ga1, 1.0);
1211 /* now set global_alpha */
1212 seq1 = gst_video_overlay_rectangle_get_seqnum (rect1);
1213 gst_video_overlay_rectangle_set_global_alpha (rect1, 0.5);
1214 ga2 = gst_video_overlay_rectangle_get_global_alpha (rect1);
1215 fail_unless_equals_float (ga2, 0.5);
1217 /* seqnum should have changed */
1218 seq2 = gst_video_overlay_rectangle_get_seqnum (rect1);
1219 fail_unless (seq1 < seq2);
1221 /* internal flags should have been set */
1222 flags1 = gst_video_overlay_rectangle_get_flags (rect1);
1223 fail_unless_equals_int (flags1, GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1225 /* request unscaled pixel-data, global-alpha not applied */
1226 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1227 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1228 /* this should just return the same buffer */
1229 fail_unless (pix2 == pix1);
1230 data2 = GST_BUFFER_DATA (pix2);
1231 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix1),
1232 GST_BUFFER_SIZE (pix1)) == 0);
1233 /* make sure we got the initial data (input=0x80808080) */
1234 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1236 fail_unless_equals_int (data2[0], 0x80);
1237 fail_unless_equals_int (data2[1], 0x80);
1238 fail_unless_equals_int (data2[2], 0x80);
1239 fail_unless_equals_int (data2[3], 0x80);
1242 fail_unless_equals_int (data2[0], 0x80);
1243 fail_unless_equals_int (data2[1], 0x80);
1244 fail_unless_equals_int (data2[2], 0x80);
1245 fail_unless_equals_int (data2[3], 0x80);
1248 /* unscaled pixel-data, global-alpha applied */
1249 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1250 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1251 /* this should be the same buffer with on-the-fly modified alpha-channel */
1252 fail_unless (pix2 == pix1);
1253 data2 = GST_BUFFER_DATA (pix2);
1254 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix1),
1255 GST_BUFFER_SIZE (pix1)) == 0);
1256 /* make sure we got the initial data with adjusted alpha-channel */
1257 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1259 fail_unless_equals_int (data2[0], 0x80);
1260 fail_unless_equals_int (data2[1], 0x80);
1261 fail_unless_equals_int (data2[2], 0x80);
1262 fail_unless_equals_int (data2[3], 0x40);
1265 fail_unless_equals_int (data2[0], 0x40);
1266 fail_unless_equals_int (data2[1], 0x80);
1267 fail_unless_equals_int (data2[2], 0x80);
1268 fail_unless_equals_int (data2[3], 0x80);
1271 /* adjust global_alpha once more */
1272 gst_video_overlay_rectangle_set_global_alpha (rect1, 0.25);
1273 ga2 = gst_video_overlay_rectangle_get_global_alpha (rect1);
1274 fail_unless_equals_float (ga2, 0.25);
1275 /* and again request unscaled pixel-data, global-alpha applied */
1276 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1277 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1278 fail_unless (pix2 == pix1);
1279 data2 = GST_BUFFER_DATA (pix2);
1280 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix1),
1281 GST_BUFFER_SIZE (pix1)) == 0);
1282 /* make sure we got the initial data with adjusted alpha-channel */
1283 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1285 fail_unless_equals_int (data2[0], 0x80);
1286 fail_unless_equals_int (data2[1], 0x80);
1287 fail_unless_equals_int (data2[2], 0x80);
1288 fail_unless_equals_int (data2[3], 0x20);
1291 fail_unless_equals_int (data2[0], 0x20);
1292 fail_unless_equals_int (data2[1], 0x80);
1293 fail_unless_equals_int (data2[2], 0x80);
1294 fail_unless_equals_int (data2[3], 0x80);
1297 /* again: unscaled pixel-data, global-alpha not applied,
1298 * this should revert alpha-channel to initial values */
1299 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1300 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1301 fail_unless (pix2 == pix1);
1302 data2 = GST_BUFFER_DATA (pix2);
1303 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix1),
1304 GST_BUFFER_SIZE (pix1)) == 0);
1305 /* make sure we got the initial data (input=0x80808080) */
1306 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1308 fail_unless_equals_int (data2[0], 0x80);
1309 fail_unless_equals_int (data2[1], 0x80);
1310 fail_unless_equals_int (data2[2], 0x80);
1311 fail_unless_equals_int (data2[3], 0x80);
1314 fail_unless_equals_int (data2[0], 0x80);
1315 fail_unless_equals_int (data2[1], 0x80);
1316 fail_unless_equals_int (data2[2], 0x80);
1317 fail_unless_equals_int (data2[3], 0x80);
1320 /* now scaled, global-alpha not applied */
1321 pix2 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1322 GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1323 /* this should just return the rect/buffer, that was cached for these
1324 * scaling dimensions */
1325 fail_unless (pix2 == pix3);
1326 data2 = GST_BUFFER_DATA (pix2);
1327 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix3),
1328 GST_BUFFER_SIZE (pix3)) == 0);
1329 /* make sure we got the initial data (input=0x80808080) */
1330 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1332 fail_unless_equals_int (data2[0], 0x80);
1333 fail_unless_equals_int (data2[1], 0x80);
1334 fail_unless_equals_int (data2[2], 0x80);
1335 fail_unless_equals_int (data2[3], 0x80);
1338 fail_unless_equals_int (data2[0], 0x80);
1339 fail_unless_equals_int (data2[1], 0x80);
1340 fail_unless_equals_int (data2[2], 0x80);
1341 fail_unless_equals_int (data2[3], 0x80);
1344 /* scaled, global-alpha (0.25) applied */
1345 pix2 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1346 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1347 /* this should just return the rect/buffer, that was cached for these
1348 * scaling dimensions with modified alpha channel */
1349 fail_unless (pix2 == pix3);
1350 data2 = GST_BUFFER_DATA (pix2);
1351 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix3),
1352 GST_BUFFER_SIZE (pix3)) == 0);
1353 /* make sure we got the data we expect for global-alpha=0.25 */
1354 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1356 fail_unless_equals_int (data2[0], 0x80);
1357 fail_unless_equals_int (data2[1], 0x80);
1358 fail_unless_equals_int (data2[2], 0x80);
1359 fail_unless_equals_int (data2[3], 0x20);
1362 fail_unless_equals_int (data2[0], 0x20);
1363 fail_unless_equals_int (data2[1], 0x80);
1364 fail_unless_equals_int (data2[2], 0x80);
1365 fail_unless_equals_int (data2[3], 0x80);
1368 /* now unscaled premultiplied data, global-alpha not applied,
1369 * is this really a valid use case?*/
1370 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1371 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA |
1372 GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1373 /* this should just return the rect/buffer, that was cached for the
1374 * premultiplied data */
1375 fail_unless (pix2 == pix4);
1376 data2 = GST_BUFFER_DATA (pix2);
1377 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix4),
1378 GST_BUFFER_SIZE (pix4)) == 0);
1379 /* make sure we got what we expected */
1380 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1382 fail_unless_equals_int (data2[0], 0x40);
1383 fail_unless_equals_int (data2[1], 0x40);
1384 fail_unless_equals_int (data2[2], 0x40);
1385 fail_unless_equals_int (data2[3], 0x80);
1388 fail_unless_equals_int (data2[0], 0x80);
1389 fail_unless_equals_int (data2[1], 0x40);
1390 fail_unless_equals_int (data2[2], 0x40);
1391 fail_unless_equals_int (data2[3], 0x40);
1394 /* unscaled premultiplied data, global-alpha (0.25) applied */
1395 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1396 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1397 /* this should just return the rect/buffer, that was cached for the
1398 * premultiplied data */
1399 fail_unless (pix2 == pix4);
1400 data2 = GST_BUFFER_DATA (pix2);
1401 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix4),
1402 GST_BUFFER_SIZE (pix4)) == 0);
1403 /* make sure we got what we expected:
1404 * (0x40 / (0x80/0xFF) * (0x20/0xFF) = 0x10
1405 * NOTE: unless we are using round() for the premultiplied case
1406 * in gst_video_overlay_rectangle_apply_global_alpha() we get rounding
1407 * error, i.e. 0x0F here */
1408 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1410 fail_unless_equals_int (data2[0], 0x0F);
1411 fail_unless_equals_int (data2[1], 0x0F);
1412 fail_unless_equals_int (data2[2], 0x0F);
1413 fail_unless_equals_int (data2[3], 0x20);
1416 fail_unless_equals_int (data2[0], 0x20);
1417 fail_unless_equals_int (data2[1], 0x0F);
1418 fail_unless_equals_int (data2[2], 0x0F);
1419 fail_unless_equals_int (data2[3], 0x0F);
1422 /* set global_alpha once more */
1423 gst_video_overlay_rectangle_set_global_alpha (rect1, 0.75);
1424 /* and verify that also premultiplied data is adjusted
1425 * correspondingly (though with increasing rounding errors) */
1426 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1427 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1428 /* this should just return the rect/buffer, that was cached for the
1429 * premultiplied data */
1430 fail_unless (pix2 == pix4);
1431 data2 = GST_BUFFER_DATA (pix2);
1432 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix4),
1433 GST_BUFFER_SIZE (pix4)) == 0);
1434 /* make sure we got what we expected:
1435 * (0x0F / (0x20/0xFF) * (0x60/0xFF) = 0x2D
1436 * NOTE: using floats everywhere we would get 0x30
1437 * here we will actually end up with 0x2C */
1438 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1440 fail_unless_equals_int (data2[0], 0x2C);
1441 fail_unless_equals_int (data2[1], 0x2C);
1442 fail_unless_equals_int (data2[2], 0x2C);
1443 fail_unless_equals_int (data2[3], 0x60);
1446 fail_unless_equals_int (data2[0], 0x60);
1447 fail_unless_equals_int (data2[1], 0x2C);
1448 fail_unless_equals_int (data2[2], 0x2C);
1449 fail_unless_equals_int (data2[3], 0x2C);
1452 /* now scaled and premultiplied data, global-alpha not applied,
1453 * is this really a valid use case?*/
1454 pix2 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1455 GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA |
1456 GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1457 /* this should just return the rect/buffer, that was cached for the
1458 * first premultiplied+scaled rect*/
1459 fail_unless (pix2 == pix5);
1460 fail_unless (stride == stride5);
1461 data2 = GST_BUFFER_DATA (pix2);
1462 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix5),
1463 GST_BUFFER_SIZE (pix5)) == 0);
1464 /* make sure we got what we expected */
1465 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1467 fail_unless_equals_int (data2[0], 0x40);
1468 fail_unless_equals_int (data2[1], 0x40);
1469 fail_unless_equals_int (data2[2], 0x40);
1470 fail_unless_equals_int (data2[3], 0x80);
1473 fail_unless_equals_int (data2[0], 0x80);
1474 fail_unless_equals_int (data2[1], 0x40);
1475 fail_unless_equals_int (data2[2], 0x40);
1476 fail_unless_equals_int (data2[3], 0x40);
1479 /* scaled and premultiplied data, global-alpha applied */
1480 pix2 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1481 GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1482 /* this should just return the rect/buffer, that was cached for the
1483 * first premultiplied+scaled rect*/
1484 fail_unless (pix2 == pix5);
1485 fail_unless (stride == stride5);
1486 data2 = GST_BUFFER_DATA (pix2);
1487 fail_unless (memcmp (data2, GST_BUFFER_DATA (pix5),
1488 GST_BUFFER_SIZE (pix5)) == 0);
1489 /* make sure we got what we expected; see above note about rounding errors! */
1490 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1492 fail_unless_equals_int (data2[0], 0x2F);
1493 fail_unless_equals_int (data2[1], 0x2F);
1494 fail_unless_equals_int (data2[2], 0x2F);
1495 fail_unless_equals_int (data2[3], 0x60);
1498 fail_unless_equals_int (data2[0], 0x60);
1499 fail_unless_equals_int (data2[1], 0x2F);
1500 fail_unless_equals_int (data2[2], 0x2F);
1501 fail_unless_equals_int (data2[3], 0x2F);
1504 gst_video_overlay_rectangle_unref (rect1);
1512 Suite *s = suite_create ("video support library");
1513 TCase *tc_chain = tcase_create ("general");
1515 suite_add_tcase (s, tc_chain);
1516 tcase_add_test (tc_chain, test_video_formats);
1517 tcase_add_test (tc_chain, test_video_formats_rgb);
1518 tcase_add_test (tc_chain, test_video_formats_all);
1519 tcase_add_test (tc_chain, test_dar_calc);
1520 tcase_add_test (tc_chain, test_parse_caps_rgb);
1521 tcase_add_test (tc_chain, test_events);
1522 tcase_add_test (tc_chain, test_convert_frame);
1523 tcase_add_test (tc_chain, test_convert_frame_async);
1524 tcase_add_test (tc_chain, test_video_size_from_caps);
1526 /* FIXME 0.11: port overlay compositions */
1527 tcase_add_test (tc_chain, test_overlay_composition);
1528 tcase_add_test (tc_chain, test_overlay_composition_premultiplied_alpha);
1529 tcase_add_test (tc_chain, test_overlay_composition_global_alpha);
1535 GST_CHECK_MAIN (video);