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.
30 #include <gst/check/gstcheck.h>
32 #include <gst/video/video.h>
34 #include <gst/video/video-overlay-composition.h>
38 /* These are from the current/old videotestsrc; we check our new public API
39 * in libgstvideo against the old one to make sure the sizes and offsets
42 typedef struct paintinfo_struct paintinfo;
43 struct paintinfo_struct
45 unsigned char *dest; /* pointer to first byte of video data */
46 unsigned char *yp, *up, *vp; /* pointers to first byte of each component
47 * for both packed/planar YUV and RGB */
48 unsigned char *ap; /* pointer to first byte of alpha component */
49 unsigned char *endptr; /* pointer to byte beyond last video data */
57 struct fourcc_list_struct
62 void (*paint_setup) (paintinfo * p, unsigned char *dest);
65 static void paint_setup_I420 (paintinfo * p, unsigned char *dest);
66 static void paint_setup_YV12 (paintinfo * p, unsigned char *dest);
67 static void paint_setup_YUY2 (paintinfo * p, unsigned char *dest);
68 static void paint_setup_UYVY (paintinfo * p, unsigned char *dest);
69 static void paint_setup_YVYU (paintinfo * p, unsigned char *dest);
70 static void paint_setup_IYU2 (paintinfo * p, unsigned char *dest);
71 static void paint_setup_Y41B (paintinfo * p, unsigned char *dest);
72 static void paint_setup_Y42B (paintinfo * p, unsigned char *dest);
73 static void paint_setup_GRAY8 (paintinfo * p, unsigned char *dest);
74 static void paint_setup_AYUV (paintinfo * p, unsigned char *dest);
77 static void paint_setup_IMC1 (paintinfo * p, unsigned char *dest);
78 static void paint_setup_IMC2 (paintinfo * p, unsigned char *dest);
79 static void paint_setup_IMC3 (paintinfo * p, unsigned char *dest);
80 static void paint_setup_IMC4 (paintinfo * p, unsigned char *dest);
82 static void paint_setup_YUV9 (paintinfo * p, unsigned char *dest);
83 static void paint_setup_YVU9 (paintinfo * p, unsigned char *dest);
85 int fourcc_get_size (struct fourcc_list_struct *fourcc, int w, int h);
87 struct fourcc_list_struct fourcc_list[] = {
89 {"YUY2", "YUY2", 16, paint_setup_YUY2},
90 {"UYVY", "UYVY", 16, paint_setup_UYVY},
91 {"Y422", "Y422", 16, paint_setup_UYVY},
92 {"UYNV", "UYNV", 16, paint_setup_UYVY}, /* FIXME: UYNV? */
93 {"YVYU", "YVYU", 16, paint_setup_YVYU},
94 {"AYUV", "AYUV", 32, paint_setup_AYUV},
97 /*{ "IUYV", "IUY2", 16, paint_setup_YVYU }, */
100 /*{ "cyuv", "cyuv", 16, paint_setup_YVYU }, */
102 /*{ "Y41P", "Y41P", 12, paint_setup_YVYU }, */
105 /*{ "IY41", "IY41", 12, paint_setup_YVYU }, */
107 /*{ "Y211", "Y211", 8, paint_setup_YVYU }, */
109 /*{ "Y41T", "Y41T", 12, paint_setup_YVYU }, */
110 /*{ "Y42P", "Y42P", 16, paint_setup_YVYU }, */
111 /*{ "CLJR", "CLJR", 8, paint_setup_YVYU }, */
112 /*{ "IYU1", "IYU1", 12, paint_setup_YVYU }, */
113 {"IYU2", "IYU2", 24, paint_setup_IYU2},
117 {"YVU9", "YVU9", 9, paint_setup_YVU9},
119 {"YUV9", "YUV9", 9, paint_setup_YUV9},
122 {"YV12", "YV12", 12, paint_setup_YV12},
124 {"I420", "I420", 12, paint_setup_I420},
129 {"IMC1", "IMC1", 16, paint_setup_IMC1},
131 {"IMC2", "IMC2", 12, paint_setup_IMC2},
133 {"IMC3", "IMC3", 16, paint_setup_IMC3},
135 {"IMC4", "IMC4", 12, paint_setup_IMC4},
139 {"Y41B", "Y41B", 12, paint_setup_Y41B},
141 {"Y42B", "Y42B", 16, paint_setup_Y42B},
142 /* GRAY8 grayscale */
143 {"GRAY8", "GRAY8", 8, paint_setup_GRAY8}
146 /* returns the size in bytes for one video frame of the given dimensions
147 * given the fourcc */
149 fourcc_get_size (struct fourcc_list_struct *fourcc, int w, int h)
151 paintinfo pi = { NULL, };
157 fourcc->paint_setup (p, NULL);
159 return (unsigned long) p->endptr;
163 paint_setup_I420 (paintinfo * p, unsigned char *dest)
166 p->ystride = GST_ROUND_UP_4 (p->width);
167 p->up = p->yp + p->ystride * GST_ROUND_UP_2 (p->height);
168 p->ustride = GST_ROUND_UP_8 (p->width) / 2;
169 p->vp = p->up + p->ustride * GST_ROUND_UP_2 (p->height) / 2;
170 p->vstride = GST_ROUND_UP_8 (p->ystride) / 2;
171 p->endptr = p->vp + p->vstride * GST_ROUND_UP_2 (p->height) / 2;
175 paint_setup_YV12 (paintinfo * p, unsigned char *dest)
178 p->ystride = GST_ROUND_UP_4 (p->width);
179 p->vp = p->yp + p->ystride * GST_ROUND_UP_2 (p->height);
180 p->vstride = GST_ROUND_UP_8 (p->ystride) / 2;
181 p->up = p->vp + p->vstride * GST_ROUND_UP_2 (p->height) / 2;
182 p->ustride = GST_ROUND_UP_8 (p->ystride) / 2;
183 p->endptr = p->up + p->ustride * GST_ROUND_UP_2 (p->height) / 2;
187 paint_setup_AYUV (paintinfo * p, unsigned char *dest)
193 p->ystride = p->width * 4;
194 p->endptr = dest + p->ystride * p->height;
198 paint_setup_YUY2 (paintinfo * p, unsigned char *dest)
203 p->ystride = GST_ROUND_UP_2 (p->width) * 2;
204 p->endptr = dest + p->ystride * p->height;
208 paint_setup_UYVY (paintinfo * p, unsigned char *dest)
213 p->ystride = GST_ROUND_UP_2 (p->width) * 2;
214 p->endptr = dest + p->ystride * p->height;
218 paint_setup_YVYU (paintinfo * p, unsigned char *dest)
223 p->ystride = GST_ROUND_UP_2 (p->width) * 2;
224 p->endptr = dest + p->ystride * p->height;
228 paint_setup_IYU2 (paintinfo * p, unsigned char *dest)
234 p->ystride = GST_ROUND_UP_4 (p->width * 3);
235 p->endptr = dest + p->ystride * p->height;
239 paint_setup_Y41B (paintinfo * p, unsigned char *dest)
242 p->ystride = GST_ROUND_UP_4 (p->width);
243 p->up = p->yp + p->ystride * p->height;
244 p->ustride = GST_ROUND_UP_16 (p->width) / 4;
245 p->vp = p->up + p->ustride * p->height;
246 p->vstride = GST_ROUND_UP_16 (p->width) / 4;
247 p->endptr = p->vp + p->vstride * p->height;
251 paint_setup_Y42B (paintinfo * p, unsigned char *dest)
254 p->ystride = GST_ROUND_UP_4 (p->width);
255 p->up = p->yp + p->ystride * p->height;
256 p->ustride = GST_ROUND_UP_8 (p->width) / 2;
257 p->vp = p->up + p->ustride * p->height;
258 p->vstride = GST_ROUND_UP_8 (p->width) / 2;
259 p->endptr = p->vp + p->vstride * p->height;
263 paint_setup_GRAY8 (paintinfo * p, unsigned char *dest)
267 p->ystride = GST_ROUND_UP_4 (p->width);
268 p->endptr = dest + p->ystride * p->height;
273 paint_setup_IMC1 (paintinfo * p, unsigned char *dest)
276 p->up = dest + p->width * p->height;
277 p->vp = dest + p->width * p->height + p->width * p->height / 2;
281 paint_setup_IMC2 (paintinfo * p, unsigned char *dest)
284 p->vp = dest + p->width * p->height;
285 p->up = dest + p->width * p->height + p->width / 2;
289 paint_setup_IMC3 (paintinfo * p, unsigned char *dest)
292 p->up = dest + p->width * p->height + p->width * p->height / 2;
293 p->vp = dest + p->width * p->height;
297 paint_setup_IMC4 (paintinfo * p, unsigned char *dest)
300 p->vp = dest + p->width * p->height + p->width / 2;
301 p->up = dest + p->width * p->height;
306 paint_setup_YVU9 (paintinfo * p, unsigned char *dest)
309 p->ystride = GST_ROUND_UP_4 (p->width);
310 p->vp = p->yp + p->ystride * p->height;
311 p->vstride = GST_ROUND_UP_4 (p->ystride / 4);
312 p->up = p->vp + p->vstride * (GST_ROUND_UP_4 (p->height) / 4);
313 p->ustride = GST_ROUND_UP_4 (p->ystride / 4);
314 p->endptr = p->up + p->ustride * (GST_ROUND_UP_4 (p->height) / 4);
318 paint_setup_YUV9 (paintinfo * p, unsigned char *dest)
321 p->ystride = GST_ROUND_UP_4 (p->width);
322 p->up = p->yp + p->ystride * p->height;
323 p->ustride = GST_ROUND_UP_4 (p->ystride / 4);
324 p->vp = p->up + p->ustride * (GST_ROUND_UP_4 (p->height) / 4);
325 p->vstride = GST_ROUND_UP_4 (p->ystride / 4);
326 p->endptr = p->vp + p->vstride * (GST_ROUND_UP_4 (p->height) / 4);
329 #define gst_video_format_is_packed video_format_is_packed
331 video_format_is_packed (GstVideoFormat fmt)
334 case GST_VIDEO_FORMAT_I420:
335 case GST_VIDEO_FORMAT_YV12:
336 case GST_VIDEO_FORMAT_Y41B:
337 case GST_VIDEO_FORMAT_Y42B:
338 case GST_VIDEO_FORMAT_GRAY8:
339 case GST_VIDEO_FORMAT_YUV9:
340 case GST_VIDEO_FORMAT_YVU9:
342 case GST_VIDEO_FORMAT_IYU1:
343 case GST_VIDEO_FORMAT_YUY2:
344 case GST_VIDEO_FORMAT_YVYU:
345 case GST_VIDEO_FORMAT_UYVY:
346 case GST_VIDEO_FORMAT_AYUV:
347 case GST_VIDEO_FORMAT_RGBx:
348 case GST_VIDEO_FORMAT_BGRx:
349 case GST_VIDEO_FORMAT_xRGB:
350 case GST_VIDEO_FORMAT_xBGR:
351 case GST_VIDEO_FORMAT_RGBA:
352 case GST_VIDEO_FORMAT_BGRA:
353 case GST_VIDEO_FORMAT_ARGB:
354 case GST_VIDEO_FORMAT_ABGR:
355 case GST_VIDEO_FORMAT_RGB:
356 case GST_VIDEO_FORMAT_BGR:
357 case GST_VIDEO_FORMAT_RGB8_PALETTED:
360 g_return_val_if_reached (FALSE);
365 GST_START_TEST (test_video_formats_all)
368 const GValue *val, *list_val;
370 guint num, n, num_formats;
373 fail_unless (gst_video_format_to_string (num_formats) == NULL);
374 while (gst_video_format_to_string (num_formats) == NULL)
376 GST_INFO ("number of known video formats: %d", num_formats);
378 caps = gst_caps_from_string ("video/x-raw, format=" GST_VIDEO_FORMATS_ALL);
379 s = gst_caps_get_structure (caps, 0);
380 val = gst_structure_get_value (s, "format");
381 fail_unless (val != NULL);
382 fail_unless (GST_VALUE_HOLDS_LIST (val));
383 num = gst_value_list_get_size (val);
384 fail_unless (num > 0);
385 for (n = 0; n < num; ++n) {
386 const gchar *fmt_str;
388 list_val = gst_value_list_get_value (val, n);
389 fail_unless (G_VALUE_HOLDS_STRING (list_val));
390 fmt_str = g_value_get_string (list_val);
391 GST_INFO ("format: %s", fmt_str);
392 fail_if (gst_video_format_from_string (fmt_str) ==
393 GST_VIDEO_FORMAT_UNKNOWN);
395 fail_unless_equals_int (num, num_formats);
397 gst_caps_unref (caps);
402 GST_START_TEST (test_video_formats)
406 for (i = 0; i < G_N_ELEMENTS (fourcc_list); ++i) {
407 const GstVideoFormatInfo *vf_info;
413 s = fourcc_list[i].fourcc;
414 fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
415 fmt = gst_video_format_from_fourcc (fourcc);
417 if (fmt == GST_VIDEO_FORMAT_UNKNOWN) {
418 GST_DEBUG ("Unknown format %s, skipping tests", fourcc_list[i].fourcc);
422 vf_info = gst_video_format_get_info (fmt);
423 fail_unless (vf_info != NULL);
425 fail_unless_equals_int (GST_VIDEO_FORMAT_INFO_FORMAT (vf_info), fmt);
427 GST_INFO ("Fourcc %s, packed=%d", fourcc_list[i].fourcc,
428 gst_video_format_is_packed (fmt));
430 fail_unless (GST_VIDEO_FORMAT_INFO_IS_YUV (vf_info));
432 /* use any non-NULL pointer so we can compare against NULL */
434 paintinfo paintinfo = { 0, };
435 fourcc_list[i].paint_setup (&paintinfo, (unsigned char *) s);
436 if (paintinfo.ap != NULL) {
437 fail_unless (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vf_info));
439 fail_if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vf_info));
443 for (w = 1; w <= 65; ++w) {
444 for (h = 1; h <= 65; ++h) {
446 paintinfo paintinfo = { 0, };
447 guint off0, off1, off2, off3;
448 guint cs0, cs1, cs2, cs3;
451 GST_LOG ("%s, %dx%d", fourcc_list[i].fourcc, w, h);
453 gst_video_info_init (&vinfo);
454 gst_video_info_set_format (&vinfo, fmt, w, h);
457 paintinfo.height = h;
458 fourcc_list[i].paint_setup (&paintinfo, NULL);
459 fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 0),
461 if (!gst_video_format_is_packed (fmt)
462 && !GST_VIDEO_INFO_N_PLANES (&vinfo) > 2) {
464 fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 1),
466 fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 2),
468 /* check component_width * height against offsets/size somehow? */
471 size = GST_VIDEO_INFO_SIZE (&vinfo);
472 off0 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 0);
473 off1 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 1);
474 off2 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 2);
476 GST_INFO ("size %d <> %d", size, paintinfo.endptr);
477 GST_INFO ("off0 %d <> %d", off0, paintinfo.yp);
478 GST_INFO ("off1 %d <> %d", off1, paintinfo.up);
479 GST_INFO ("off2 %d <> %d", off2, paintinfo.vp);
481 fail_unless_equals_int (size, (unsigned long) paintinfo.endptr);
482 fail_unless_equals_int (off0, (unsigned long) paintinfo.yp);
483 fail_unless_equals_int (off1, (unsigned long) paintinfo.up);
484 fail_unless_equals_int (off2, (unsigned long) paintinfo.vp);
486 /* should be 0 if there's no alpha component */
487 off3 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 3);
488 fail_unless_equals_int (off3, (unsigned long) paintinfo.ap);
490 cs0 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 0) *
491 GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 0);
492 cs1 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 1) *
493 GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 1);
494 cs2 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 2) *
495 GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 2);
497 /* GST_LOG ("cs0=%d,cs1=%d,cs2=%d,off0=%d,off1=%d,off2=%d,size=%d",
498 cs0, cs1, cs2, off0, off1, off2, size); */
500 if (!gst_video_format_is_packed (fmt))
501 fail_unless (cs0 <= off1);
503 if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vinfo.finfo)) {
504 cs3 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 3) *
505 GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 2);
506 fail_unless (cs3 < size);
507 /* U/V/alpha shouldn't take up more space than the Y component */
508 fail_if (cs1 > cs0, "cs1 (%d) should be <= cs0 (%d)", cs1, cs0);
509 fail_if (cs2 > cs0, "cs2 (%d) should be <= cs0 (%d)", cs2, cs0);
510 fail_if (cs3 > cs0, "cs3 (%d) should be <= cs0 (%d)", cs3, cs0);
512 /* all components together shouldn't take up more space than size */
513 fail_unless (cs0 + cs1 + cs2 + cs3 <= size);
515 /* U/V shouldn't take up more space than the Y component */
516 fail_if (cs1 > cs0, "cs1 (%d) should be <= cs0 (%d)", cs1, cs0);
517 fail_if (cs2 > cs0, "cs2 (%d) should be <= cs0 (%d)", cs2, cs0);
519 /* all components together shouldn't take up more space than size */
520 fail_unless (cs0 + cs1 + cs2 <= size,
521 "cs0 (%d) + cs1 (%d) + cs2 (%d) should be <= size (%d)",
522 cs0, cs1, cs2, size);
531 GST_START_TEST (test_video_formats_rgb)
534 gint width, height, framerate_n, framerate_d, par_n, par_d;
536 GstStructure *structure;
538 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_RGB, 800, 600);
543 caps = gst_video_info_to_caps (&vinfo);
544 structure = gst_caps_get_structure (caps, 0);
546 fail_unless (gst_structure_get_int (structure, "width", &width));
547 fail_unless (gst_structure_get_int (structure, "height", &height));
548 fail_unless (gst_structure_get_fraction (structure, "framerate", &framerate_n,
550 fail_unless (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
553 fail_unless (width == 800);
554 fail_unless (height == 600);
555 fail_unless (framerate_n == 0);
556 fail_unless (framerate_d == 1);
557 fail_unless (par_n == 1);
558 fail_unless (par_d == 1);
560 gst_caps_unref (caps);
565 GST_START_TEST (test_dar_calc)
567 guint display_ratio_n, display_ratio_d;
569 /* Ensure that various Display Ratio calculations are correctly done */
570 /* video 768x576, par 16/15, display par 16/15 = 4/3 */
571 fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
572 &display_ratio_d, 768, 576, 16, 15, 16, 15));
573 fail_unless (display_ratio_n == 4 && display_ratio_d == 3);
575 /* video 720x480, par 32/27, display par 1/1 = 16/9 */
576 fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
577 &display_ratio_d, 720, 480, 32, 27, 1, 1));
578 fail_unless (display_ratio_n == 16 && display_ratio_d == 9);
580 /* video 360x288, par 533333/500000, display par 16/15 =
581 * dar 1599999/1600000 */
582 fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
583 &display_ratio_d, 360, 288, 533333, 500000, 16, 15));
584 fail_unless (display_ratio_n == 1599999 && display_ratio_d == 1280000);
589 GST_START_TEST (test_parse_caps_rgb)
593 const gchar *tmpl_caps_string;
598 GST_VIDEO_CAPS_MAKE ("RGB"), GST_VIDEO_FORMAT_RGB}, {
599 GST_VIDEO_CAPS_MAKE ("BGR"), GST_VIDEO_FORMAT_BGR},
600 /* 32 bit (no alpha) */
602 GST_VIDEO_CAPS_MAKE ("RGBx"), GST_VIDEO_FORMAT_RGBx}, {
603 GST_VIDEO_CAPS_MAKE ("xRGB"), GST_VIDEO_FORMAT_xRGB}, {
604 GST_VIDEO_CAPS_MAKE ("BGRx"), GST_VIDEO_FORMAT_BGRx}, {
605 GST_VIDEO_CAPS_MAKE ("xBGR"), GST_VIDEO_FORMAT_xBGR},
606 /* 32 bit (with alpha) */
608 GST_VIDEO_CAPS_MAKE ("RGBA"), GST_VIDEO_FORMAT_RGBA}, {
609 GST_VIDEO_CAPS_MAKE ("ARGB"), GST_VIDEO_FORMAT_ARGB}, {
610 GST_VIDEO_CAPS_MAKE ("BGRA"), GST_VIDEO_FORMAT_BGRA}, {
611 GST_VIDEO_CAPS_MAKE ("ABGR"), GST_VIDEO_FORMAT_ABGR},
614 GST_VIDEO_CAPS_MAKE ("RGB16"), GST_VIDEO_FORMAT_RGB16}, {
615 GST_VIDEO_CAPS_MAKE ("BGR16"), GST_VIDEO_FORMAT_BGR16}, {
616 GST_VIDEO_CAPS_MAKE ("RGB15"), GST_VIDEO_FORMAT_RGB15}, {
617 GST_VIDEO_CAPS_MAKE ("BGR15"), GST_VIDEO_FORMAT_BGR15}
621 for (i = 0; i < G_N_ELEMENTS (formats); ++i) {
623 GstCaps *caps, *caps2;
625 caps = gst_caps_from_string (formats[i].tmpl_caps_string);
626 gst_caps_set_simple (caps, "width", G_TYPE_INT, 2 * (i + 1), "height",
627 G_TYPE_INT, i + 1, "framerate", GST_TYPE_FRACTION, 15, 1,
628 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
629 "interlace-mode", G_TYPE_STRING, "progressive", NULL);
630 g_assert (gst_caps_is_fixed (caps));
632 GST_DEBUG ("testing caps: %" GST_PTR_FORMAT, caps);
634 gst_video_info_init (&vinfo);
635 fail_unless (gst_video_info_from_caps (&vinfo, caps));
636 fail_unless_equals_int (GST_VIDEO_INFO_FORMAT (&vinfo), formats[i].fmt);
637 fail_unless_equals_int (GST_VIDEO_INFO_WIDTH (&vinfo), 2 * (i + 1));
638 fail_unless_equals_int (GST_VIDEO_INFO_HEIGHT (&vinfo), i + 1);
640 /* make sure they're serialised back correctly */
641 caps2 = gst_video_info_to_caps (&vinfo);
642 fail_unless (caps != NULL);
643 fail_unless (gst_caps_is_equal (caps, caps2),
644 "caps [%" GST_PTR_FORMAT "] not equal to caps2 [%" GST_PTR_FORMAT "]",
647 gst_caps_unref (caps);
648 gst_caps_unref (caps2);
654 GST_START_TEST (test_events)
659 e = gst_video_event_new_still_frame (TRUE);
660 fail_if (e == NULL, "Failed to create still frame event");
661 fail_unless (gst_video_event_parse_still_frame (e, &in_still),
662 "Failed to parse still frame event");
663 fail_unless (gst_video_event_parse_still_frame (e, NULL),
664 "Failed to parse still frame event w/ in_still == NULL");
665 fail_unless (in_still == TRUE);
668 e = gst_video_event_new_still_frame (FALSE);
669 fail_if (e == NULL, "Failed to create still frame event");
670 fail_unless (gst_video_event_parse_still_frame (e, &in_still),
671 "Failed to parse still frame event");
672 fail_unless (gst_video_event_parse_still_frame (e, NULL),
673 "Failed to parse still frame event w/ in_still == NULL");
674 fail_unless (in_still == FALSE);
680 GST_START_TEST (test_convert_frame)
683 GstCaps *from_caps, *to_caps;
684 GstBuffer *from_buffer;
685 GstSample *from_sample, *to_sample;
686 GError *error = NULL;
690 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
692 from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);
694 gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
695 for (i = 0; i < 640 * 480; i++) {
696 map.data[4 * i + 0] = 0; /* x */
697 map.data[4 * i + 1] = 255; /* R */
698 map.data[4 * i + 2] = 0; /* G */
699 map.data[4 * i + 3] = 0; /* B */
701 gst_buffer_unmap (from_buffer, &map);
703 gst_video_info_init (&vinfo);
704 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640, 480);
709 from_caps = gst_video_info_to_caps (&vinfo);
711 from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);
715 ("something/that, does=(string)not, exist=(boolean)FALSE");
718 gst_video_convert_sample (from_sample, to_caps,
719 GST_CLOCK_TIME_NONE, &error);
720 fail_if (to_sample != NULL);
721 fail_unless (error != NULL);
722 g_error_free (error);
725 gst_caps_unref (to_caps);
726 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420, 240, 320);
731 to_caps = gst_video_info_to_caps (&vinfo);
734 gst_video_convert_sample (from_sample, to_caps,
735 GST_CLOCK_TIME_NONE, &error);
736 fail_unless (to_sample != NULL);
737 fail_unless (error == NULL);
739 gst_buffer_unref (from_buffer);
740 gst_caps_unref (from_caps);
741 gst_sample_unref (from_sample);
742 gst_sample_unref (to_sample);
743 gst_caps_unref (to_caps);
753 } ConvertFrameContext;
756 convert_sample_async_callback (GstSample * sample, GError * err,
757 ConvertFrameContext * cf_data)
759 cf_data->sample = sample;
760 cf_data->error = err;
762 g_main_loop_quit (cf_data->loop);
765 GST_START_TEST (test_convert_frame_async)
768 GstCaps *from_caps, *to_caps;
769 GstBuffer *from_buffer;
770 GstSample *from_sample;
774 ConvertFrameContext cf_data = { NULL, NULL, NULL };
776 gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
778 from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);
780 gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
781 for (i = 0; i < 640 * 480; i++) {
782 map.data[4 * i + 0] = 0; /* x */
783 map.data[4 * i + 1] = 255; /* R */
784 map.data[4 * i + 2] = 0; /* G */
785 map.data[4 * i + 3] = 0; /* B */
787 gst_buffer_unmap (from_buffer, &map);
789 gst_video_info_init (&vinfo);
790 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640, 470);
795 from_caps = gst_video_info_to_caps (&vinfo);
799 ("something/that, does=(string)not, exist=(boolean)FALSE");
801 loop = cf_data.loop = g_main_loop_new (NULL, FALSE);
803 from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);
804 gst_buffer_unref (from_buffer);
805 gst_caps_unref (from_caps);
807 gst_video_convert_sample_async (from_sample, to_caps,
809 (GstVideoConvertSampleCallback) convert_sample_async_callback, &cf_data,
812 g_main_loop_run (loop);
814 fail_if (cf_data.sample != NULL);
815 fail_unless (cf_data.error != NULL);
816 g_error_free (cf_data.error);
817 cf_data.error = NULL;
819 gst_caps_unref (to_caps);
820 gst_video_info_init (&vinfo);
821 gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420, 240, 320);
826 to_caps = gst_video_info_to_caps (&vinfo);
827 gst_video_convert_sample_async (from_sample, to_caps,
829 (GstVideoConvertSampleCallback) convert_sample_async_callback, &cf_data,
831 g_main_loop_run (loop);
832 fail_unless (cf_data.sample != NULL);
833 fail_unless (cf_data.error == NULL);
835 gst_sample_unref (cf_data.sample);
836 gst_caps_unref (to_caps);
838 g_main_loop_unref (loop);
843 GST_START_TEST (test_video_size_from_caps)
848 caps = gst_caps_new_simple ("video/x-raw",
849 "format", G_TYPE_STRING, "YV12",
850 "width", G_TYPE_INT, 640,
851 "height", G_TYPE_INT, 480, "framerate", GST_TYPE_FRACTION, 25, 1, NULL);
853 gst_video_info_init (&vinfo);
854 fail_unless (gst_video_info_from_caps (&vinfo, caps));
855 fail_unless (GST_VIDEO_INFO_SIZE (&vinfo) == (640 * 480 * 12 / 8));
857 gst_caps_unref (caps);
863 /* FIXME 0.11: port overlay composition to buffer meta */
864 GST_START_TEST (test_overlay_composition)
866 GstVideoOverlayComposition *comp1, *comp2;
867 GstVideoOverlayRectangle *rect1, *rect2;
868 GstBuffer *pix1, *pix2, *buf;
873 pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
874 memset (GST_BUFFER_DATA (pix1), 0, GST_BUFFER_SIZE (pix1));
876 rect1 = gst_video_overlay_rectangle_new_argb (pix1, 200, 50, 200 * 4,
877 600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
879 gst_buffer_unref (pix1);
882 comp1 = gst_video_overlay_composition_new (rect1);
883 fail_unless (gst_video_overlay_composition_n_rectangles (comp1) == 1);
884 fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 0) == rect1);
885 fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 1) == NULL);
887 /* rectangle was created first, sequence number should be smaller */
888 seq1 = gst_video_overlay_rectangle_get_seqnum (rect1);
889 seq2 = gst_video_overlay_composition_get_seqnum (comp1);
890 fail_unless (seq1 < seq2);
892 /* composition took own ref, so refcount is 2 now, so this should fail */
893 ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 50,
896 /* drop our ref, so refcount is 1 (we know it will continue to be valid) */
897 gst_video_overlay_rectangle_unref (rect1);
898 gst_video_overlay_rectangle_set_render_rectangle (rect1, 50, 600, 300, 50);
900 comp2 = gst_video_overlay_composition_new (rect1);
901 fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1);
902 fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect1);
903 fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL);
905 fail_unless (seq1 < gst_video_overlay_composition_get_seqnum (comp2));
906 fail_unless (seq2 < gst_video_overlay_composition_get_seqnum (comp2));
908 /* now refcount is 2 again because comp2 has also taken a ref, so must fail */
909 ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 0,
912 /* this should make a copy of the rectangles so drop the original
913 * second ref on rect1 */
914 comp2 = gst_video_overlay_composition_make_writable (comp2);
915 gst_video_overlay_rectangle_set_render_rectangle (rect1, 51, 601, 301, 51);
917 rect2 = gst_video_overlay_composition_get_rectangle (comp2, 0);
918 fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1);
919 fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect2);
920 fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL);
921 fail_unless (rect1 != rect2);
923 gst_video_overlay_composition_add_rectangle (comp1, rect2);
924 gst_video_overlay_composition_ref (comp1);
925 ASSERT_CRITICAL (gst_video_overlay_composition_add_rectangle (comp1, rect2));
926 gst_video_overlay_composition_unref (comp1);
928 /* make sure the copy really worked */
929 gst_video_overlay_rectangle_get_render_rectangle (rect1, &x, &y, &w, &h);
930 fail_unless_equals_int (x, 51);
931 fail_unless_equals_int (y, 601);
932 fail_unless_equals_int (w, 301);
933 fail_unless_equals_int (h, 51);
935 /* get scaled pixbuf and touch last byte */
936 pix1 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
937 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
938 fail_unless (GST_BUFFER_SIZE (pix1) > ((h - 1) * stride + (w * 4) - 1),
939 "size %u vs. last pixel offset %u", GST_BUFFER_SIZE (pix1),
940 ((h - 1) * stride + (w * 4) - 1));
941 fail_unless_equals_int (*(GST_BUFFER_DATA (pix1) + ((h - 1) * stride +
944 gst_video_overlay_rectangle_get_render_rectangle (rect2, &x, &y, &w, &h);
945 fail_unless_equals_int (x, 50);
946 fail_unless_equals_int (y, 600);
947 fail_unless_equals_int (w, 300);
948 fail_unless_equals_int (h, 50);
950 /* get scaled pixbuf and touch last byte */
951 pix2 = gst_video_overlay_rectangle_get_pixels_argb (rect2, &stride,
952 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
953 fail_unless (GST_BUFFER_SIZE (pix2) > ((h - 1) * stride + (w * 4) - 1),
954 "size %u vs. last pixel offset %u", GST_BUFFER_SIZE (pix1),
955 ((h - 1) * stride + (w * 4) - 1));
956 fail_unless_equals_int (*(GST_BUFFER_DATA (pix2) + ((h - 1) * stride +
959 /* get scaled pixbuf again, should be the same buffer as before (caching) */
960 pix1 = gst_video_overlay_rectangle_get_pixels_argb (rect2, &stride,
961 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
962 fail_unless (pix1 == pix2);
964 /* now compare the original unscaled ones */
965 pix1 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
966 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
967 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect2, &w, &h,
968 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
970 /* the original pixel buffers should be identical */
971 fail_unless (pix1 == pix2);
972 fail_unless_equals_int (w, 200);
973 fail_unless_equals_int (h, 50);
975 /* touch last byte */
976 fail_unless (GST_BUFFER_SIZE (pix1) > ((h - 1) * stride + (w * 4) - 1),
977 "size %u vs. last pixel offset %u", GST_BUFFER_SIZE (pix1),
978 ((h - 1) * stride + (w * 4) - 1));
979 fail_unless_equals_int (*(GST_BUFFER_DATA (pix1) + ((h - 1) * stride +
982 /* test attaching and retrieving of compositions to/from buffers */
983 buf = gst_buffer_new ();
984 fail_unless (gst_video_buffer_get_overlay_composition (buf) == NULL);
986 gst_buffer_ref (buf);
987 /* buffer now has refcount of 2, so its metadata is not writable */
988 ASSERT_CRITICAL (gst_video_buffer_set_overlay_composition (buf, comp1));
989 gst_buffer_unref (buf);
990 gst_video_buffer_set_overlay_composition (buf, comp1);
991 fail_unless (gst_video_buffer_get_overlay_composition (buf) == comp1);
992 gst_video_buffer_set_overlay_composition (buf, comp2);
993 fail_unless (gst_video_buffer_get_overlay_composition (buf) == comp2);
994 gst_video_buffer_set_overlay_composition (buf, NULL);
995 fail_unless (gst_video_buffer_get_overlay_composition (buf) == NULL);
997 /* make sure the buffer cleans up its composition ref when unreffed */
998 gst_video_buffer_set_overlay_composition (buf, comp2);
999 gst_buffer_unref (buf);
1001 gst_video_overlay_composition_unref (comp2);
1002 gst_video_overlay_composition_unref (comp1);
1007 GST_START_TEST (test_overlay_composition_premultiplied_alpha)
1009 GstVideoOverlayRectangle *rect1;
1010 GstBuffer *pix1, *pix2, *pix3, *pix4, *pix5;
1011 GstBuffer *pix6, *pix7, *pix8, *pix9, *pix10;
1012 guint8 *data5, *data7;
1013 guint w, h, stride, w2, h2, stride2;
1015 pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
1016 memset (GST_BUFFER_DATA (pix1), 0x80, GST_BUFFER_SIZE (pix1));
1018 rect1 = gst_video_overlay_rectangle_new_argb (pix1, 200, 50, 200 * 4,
1019 600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1020 gst_buffer_unref (pix1);
1022 /* same flags, unscaled, should be the same buffer */
1023 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1024 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1025 fail_unless (pix1 == pix2);
1027 /* same flags, but scaled */
1028 pix3 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1029 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1030 fail_if (pix3 == pix1 || pix3 == pix2);
1032 /* same again, should hopefully get the same (cached) buffer as before */
1033 pix4 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1034 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1035 fail_unless (pix4 == pix3);
1037 /* just to update the vars */
1038 pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w, &h,
1039 &stride, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1041 /* now, let's try to get premultiplied alpha from the unpremultiplied input */
1042 pix5 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w2, &h2,
1043 &stride2, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1044 fail_if (pix5 == pix1 || pix5 == pix2 || pix5 == pix3);
1045 fail_unless_equals_int (stride, stride2);
1046 fail_unless_equals_int (w, w2);
1047 fail_unless_equals_int (h, h2);
1048 fail_unless_equals_int (GST_BUFFER_SIZE (pix2), GST_BUFFER_SIZE (pix5));
1049 data5 = GST_BUFFER_DATA (pix5);
1050 fail_if (memcmp (data5, GST_BUFFER_DATA (pix2), GST_BUFFER_SIZE (pix5)) == 0);
1052 /* make sure it actually did what we expected it to do (input=0x80808080) */
1053 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1055 fail_unless_equals_int (data5[0], 0x40);
1056 fail_unless_equals_int (data5[1], 0x40);
1057 fail_unless_equals_int (data5[2], 0x40);
1058 fail_unless_equals_int (data5[3], 0x80);
1061 fail_unless_equals_int (data5[0], 0x40);
1062 fail_unless_equals_int (data5[1], 0x40);
1063 fail_unless_equals_int (data5[2], 0x40);
1064 fail_unless_equals_int (data5[3], 0x80);
1067 /* same again, now we should be getting back the same buffer as before,
1068 * as it should have been cached */
1069 pix6 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w2, &h2,
1070 &stride2, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1071 fail_unless (pix6 == pix5);
1073 /* just to update the stride var */
1074 pix3 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1075 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1076 fail_unless (pix3 == pix4);
1078 /* now try to get scaled premultiplied alpha from unpremultiplied input */
1079 pix7 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride2,
1080 GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1081 fail_if (pix7 == pix1 || pix7 == pix2 || pix7 == pix3 || pix7 == pix5);
1082 fail_unless_equals_int (stride, stride2);
1084 data7 = GST_BUFFER_DATA (pix7);
1085 /* make sure it actually did what we expected it to do (input=0x80808080)
1086 * hoping that the scaling didn't mess up our values */
1087 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1089 fail_unless_equals_int (data7[0], 0x40);
1090 fail_unless_equals_int (data7[1], 0x40);
1091 fail_unless_equals_int (data7[2], 0x40);
1092 fail_unless_equals_int (data7[3], 0x80);
1095 fail_unless_equals_int (data7[0], 0x40);
1096 fail_unless_equals_int (data7[1], 0x40);
1097 fail_unless_equals_int (data7[2], 0x40);
1098 fail_unless_equals_int (data7[3], 0x80);
1101 /* and the same again, it should be cached now */
1102 pix8 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride2,
1103 GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1104 fail_unless (pix8 == pix7);
1106 /* make sure other cached stuff is still there */
1107 pix9 = gst_video_overlay_rectangle_get_pixels_argb (rect1, &stride,
1108 GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1109 fail_unless (pix9 == pix3);
1110 pix10 = gst_video_overlay_rectangle_get_pixels_unscaled_argb (rect1, &w2, &h2,
1111 &stride2, GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1112 fail_unless (pix10 == pix5);
1114 gst_video_overlay_rectangle_unref (rect1);
1123 Suite *s = suite_create ("video support library");
1124 TCase *tc_chain = tcase_create ("general");
1126 suite_add_tcase (s, tc_chain);
1127 tcase_add_test (tc_chain, test_video_formats);
1128 tcase_add_test (tc_chain, test_video_formats_rgb);
1129 tcase_add_test (tc_chain, test_video_formats_all);
1130 tcase_add_test (tc_chain, test_dar_calc);
1131 tcase_add_test (tc_chain, test_parse_caps_rgb);
1132 tcase_add_test (tc_chain, test_events);
1133 tcase_add_test (tc_chain, test_convert_frame);
1134 tcase_add_test (tc_chain, test_convert_frame_async);
1135 tcase_add_test (tc_chain, test_video_size_from_caps);
1137 /* FIXME 0.11: port overlay compositions */
1138 tcase_add_test (tc_chain, test_overlay_composition);
1139 tcase_add_test (tc_chain, test_overlay_composition_premultiplied_alpha);
1145 GST_CHECK_MAIN (video);