video-chroma: Add support for any combination of chroma-site flags
[platform/upstream/gstreamer.git] / tests / check / libs / video.c
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  * 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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #ifdef HAVE_VALGRIND
29 # include <valgrind/valgrind.h>
30 #endif
31
32 #include <gst/check/gstcheck.h>
33
34 #include <gst/video/video.h>
35 #include <gst/video/gstvideometa.h>
36 #include <gst/video/video-overlay-composition.h>
37 #include <string.h>
38
39 /* These are from the current/old videotestsrc; we check our new public API
40  * in libgstvideo against the old one to make sure the sizes and offsets
41  * end up the same */
42
43 typedef struct paintinfo_struct paintinfo;
44 struct paintinfo_struct
45 {
46   unsigned char *dest;          /* pointer to first byte of video data */
47   unsigned char *yp, *up, *vp;  /* pointers to first byte of each component
48                                  * for both packed/planar YUV and RGB */
49   unsigned char *ap;            /* pointer to first byte of alpha component */
50   unsigned char *endptr;        /* pointer to byte beyond last video data */
51   int ystride;
52   int ustride;
53   int vstride;
54   int width;
55   int height;
56 };
57
58 struct fourcc_list_struct
59 {
60   const char *fourcc;
61   const char *name;
62   int bitspp;
63   void (*paint_setup) (paintinfo * p, unsigned char *dest);
64 };
65
66 static void paint_setup_I420 (paintinfo * p, unsigned char *dest);
67 static void paint_setup_YV12 (paintinfo * p, unsigned char *dest);
68 static void paint_setup_YUY2 (paintinfo * p, unsigned char *dest);
69 static void paint_setup_UYVY (paintinfo * p, unsigned char *dest);
70 static void paint_setup_YVYU (paintinfo * p, unsigned char *dest);
71 static void paint_setup_IYU2 (paintinfo * p, unsigned char *dest);
72 static void paint_setup_Y41B (paintinfo * p, unsigned char *dest);
73 static void paint_setup_Y42B (paintinfo * p, unsigned char *dest);
74 static void paint_setup_GRAY8 (paintinfo * p, unsigned char *dest);
75 static void paint_setup_AYUV (paintinfo * p, unsigned char *dest);
76
77 #if 0
78 static void paint_setup_IMC1 (paintinfo * p, unsigned char *dest);
79 static void paint_setup_IMC2 (paintinfo * p, unsigned char *dest);
80 static void paint_setup_IMC3 (paintinfo * p, unsigned char *dest);
81 static void paint_setup_IMC4 (paintinfo * p, unsigned char *dest);
82 #endif
83 static void paint_setup_YUV9 (paintinfo * p, unsigned char *dest);
84 static void paint_setup_YVU9 (paintinfo * p, unsigned char *dest);
85
86 int fourcc_get_size (struct fourcc_list_struct *fourcc, int w, int h);
87
88 struct fourcc_list_struct fourcc_list[] = {
89 /* packed */
90   {"YUY2", "YUY2", 16, paint_setup_YUY2},
91   {"UYVY", "UYVY", 16, paint_setup_UYVY},
92   {"Y422", "Y422", 16, paint_setup_UYVY},
93   {"UYNV", "UYNV", 16, paint_setup_UYVY},       /* FIXME: UYNV? */
94   {"YVYU", "YVYU", 16, paint_setup_YVYU},
95   {"AYUV", "AYUV", 32, paint_setup_AYUV},
96
97   /* interlaced */
98   /*{   "IUYV", "IUY2", 16, paint_setup_YVYU }, */
99
100   /* inverted */
101   /*{   "cyuv", "cyuv", 16, paint_setup_YVYU }, */
102
103   /*{   "Y41P", "Y41P", 12, paint_setup_YVYU }, */
104
105   /* interlaced */
106   /*{   "IY41", "IY41", 12, paint_setup_YVYU }, */
107
108   /*{   "Y211", "Y211", 8, paint_setup_YVYU }, */
109
110   /*{   "Y41T", "Y41T", 12, paint_setup_YVYU }, */
111   /*{   "Y42P", "Y42P", 16, paint_setup_YVYU }, */
112   /*{   "CLJR", "CLJR", 8, paint_setup_YVYU }, */
113   /*{   "IYU1", "IYU1", 12, paint_setup_YVYU }, */
114   {"IYU2", "IYU2", 24, paint_setup_IYU2},
115
116 /* planar */
117   /* YVU9 */
118   {"YVU9", "YVU9", 9, paint_setup_YVU9},
119   /* YUV9 */
120   {"YUV9", "YUV9", 9, paint_setup_YUV9},
121   /* IF09 */
122   /* YV12 */
123   {"YV12", "YV12", 12, paint_setup_YV12},
124   /* I420 */
125   {"I420", "I420", 12, paint_setup_I420},
126   /* NV12 */
127   /* NV21 */
128 #if 0
129   /* IMC1 */
130   {"IMC1", "IMC1", 16, paint_setup_IMC1},
131   /* IMC2 */
132   {"IMC2", "IMC2", 12, paint_setup_IMC2},
133   /* IMC3 */
134   {"IMC3", "IMC3", 16, paint_setup_IMC3},
135   /* IMC4 */
136   {"IMC4", "IMC4", 12, paint_setup_IMC4},
137 #endif
138   /* CLPL */
139   /* Y41B */
140   {"Y41B", "Y41B", 12, paint_setup_Y41B},
141   /* Y42B */
142   {"Y42B", "Y42B", 16, paint_setup_Y42B},
143   /* GRAY8 grayscale */
144   {"GRAY8", "GRAY8", 8, paint_setup_GRAY8}
145 };
146
147 /* returns the size in bytes for one video frame of the given dimensions
148  * given the fourcc */
149 int
150 fourcc_get_size (struct fourcc_list_struct *fourcc, int w, int h)
151 {
152   paintinfo pi = { NULL, };
153   paintinfo *p = &pi;
154
155   p->width = w;
156   p->height = h;
157
158   fourcc->paint_setup (p, NULL);
159
160   return GPOINTER_TO_INT (p->endptr);
161 }
162
163 static void
164 paint_setup_I420 (paintinfo * p, unsigned char *dest)
165 {
166   p->yp = dest;
167   p->ystride = GST_ROUND_UP_4 (p->width);
168   p->up = p->yp + p->ystride * GST_ROUND_UP_2 (p->height);
169   p->ustride = GST_ROUND_UP_8 (p->width) / 2;
170   p->vp = p->up + p->ustride * GST_ROUND_UP_2 (p->height) / 2;
171   p->vstride = GST_ROUND_UP_8 (p->ystride) / 2;
172   p->endptr = p->vp + p->vstride * GST_ROUND_UP_2 (p->height) / 2;
173 }
174
175 static void
176 paint_setup_YV12 (paintinfo * p, unsigned char *dest)
177 {
178   p->yp = dest;
179   p->ystride = GST_ROUND_UP_4 (p->width);
180   p->vp = p->yp + p->ystride * GST_ROUND_UP_2 (p->height);
181   p->vstride = GST_ROUND_UP_8 (p->ystride) / 2;
182   p->up = p->vp + p->vstride * GST_ROUND_UP_2 (p->height) / 2;
183   p->ustride = GST_ROUND_UP_8 (p->ystride) / 2;
184   p->endptr = p->up + p->ustride * GST_ROUND_UP_2 (p->height) / 2;
185 }
186
187 static void
188 paint_setup_AYUV (paintinfo * p, unsigned char *dest)
189 {
190   p->ap = dest;
191   p->yp = dest + 1;
192   p->up = dest + 2;
193   p->vp = dest + 3;
194   p->ystride = p->width * 4;
195   p->endptr = dest + p->ystride * p->height;
196 }
197
198 static void
199 paint_setup_YUY2 (paintinfo * p, unsigned char *dest)
200 {
201   p->yp = dest;
202   p->up = dest + 1;
203   p->vp = dest + 3;
204   p->ystride = GST_ROUND_UP_2 (p->width) * 2;
205   p->endptr = dest + p->ystride * p->height;
206 }
207
208 static void
209 paint_setup_UYVY (paintinfo * p, unsigned char *dest)
210 {
211   p->yp = dest + 1;
212   p->up = dest;
213   p->vp = dest + 2;
214   p->ystride = GST_ROUND_UP_2 (p->width) * 2;
215   p->endptr = dest + p->ystride * p->height;
216 }
217
218 static void
219 paint_setup_YVYU (paintinfo * p, unsigned char *dest)
220 {
221   p->yp = dest;
222   p->up = dest + 3;
223   p->vp = dest + 1;
224   p->ystride = GST_ROUND_UP_2 (p->width) * 2;
225   p->endptr = dest + p->ystride * p->height;
226 }
227
228 static void
229 paint_setup_IYU2 (paintinfo * p, unsigned char *dest)
230 {
231   /* untested */
232   p->yp = dest + 1;
233   p->up = dest + 0;
234   p->vp = dest + 2;
235   p->ystride = GST_ROUND_UP_4 (p->width * 3);
236   p->endptr = dest + p->ystride * p->height;
237 }
238
239 static void
240 paint_setup_Y41B (paintinfo * p, unsigned char *dest)
241 {
242   p->yp = dest;
243   p->ystride = GST_ROUND_UP_4 (p->width);
244   p->up = p->yp + p->ystride * p->height;
245   p->ustride = GST_ROUND_UP_16 (p->width) / 4;
246   p->vp = p->up + p->ustride * p->height;
247   p->vstride = GST_ROUND_UP_16 (p->width) / 4;
248   p->endptr = p->vp + p->vstride * p->height;
249 }
250
251 static void
252 paint_setup_Y42B (paintinfo * p, unsigned char *dest)
253 {
254   p->yp = dest;
255   p->ystride = GST_ROUND_UP_4 (p->width);
256   p->up = p->yp + p->ystride * p->height;
257   p->ustride = GST_ROUND_UP_8 (p->width) / 2;
258   p->vp = p->up + p->ustride * p->height;
259   p->vstride = GST_ROUND_UP_8 (p->width) / 2;
260   p->endptr = p->vp + p->vstride * p->height;
261 }
262
263 static void
264 paint_setup_GRAY8 (paintinfo * p, unsigned char *dest)
265 {
266   /* untested */
267   p->yp = dest;
268   p->ystride = GST_ROUND_UP_4 (p->width);
269   p->endptr = dest + p->ystride * p->height;
270 }
271
272 #if 0
273 static void
274 paint_setup_IMC1 (paintinfo * p, unsigned char *dest)
275 {
276   p->yp = dest;
277   p->up = dest + p->width * p->height;
278   p->vp = dest + p->width * p->height + p->width * p->height / 2;
279 }
280
281 static void
282 paint_setup_IMC2 (paintinfo * p, unsigned char *dest)
283 {
284   p->yp = dest;
285   p->vp = dest + p->width * p->height;
286   p->up = dest + p->width * p->height + p->width / 2;
287 }
288
289 static void
290 paint_setup_IMC3 (paintinfo * p, unsigned char *dest)
291 {
292   p->yp = dest;
293   p->up = dest + p->width * p->height + p->width * p->height / 2;
294   p->vp = dest + p->width * p->height;
295 }
296
297 static void
298 paint_setup_IMC4 (paintinfo * p, unsigned char *dest)
299 {
300   p->yp = dest;
301   p->vp = dest + p->width * p->height + p->width / 2;
302   p->up = dest + p->width * p->height;
303 }
304 #endif
305
306 static void
307 paint_setup_YVU9 (paintinfo * p, unsigned char *dest)
308 {
309   p->yp = dest;
310   p->ystride = GST_ROUND_UP_4 (p->width);
311   p->vp = p->yp + p->ystride * p->height;
312   p->vstride = GST_ROUND_UP_4 (p->ystride / 4);
313   p->up = p->vp + p->vstride * (GST_ROUND_UP_4 (p->height) / 4);
314   p->ustride = GST_ROUND_UP_4 (p->ystride / 4);
315   p->endptr = p->up + p->ustride * (GST_ROUND_UP_4 (p->height) / 4);
316 }
317
318 static void
319 paint_setup_YUV9 (paintinfo * p, unsigned char *dest)
320 {
321   p->yp = dest;
322   p->ystride = GST_ROUND_UP_4 (p->width);
323   p->up = p->yp + p->ystride * p->height;
324   p->ustride = GST_ROUND_UP_4 (p->ystride / 4);
325   p->vp = p->up + p->ustride * (GST_ROUND_UP_4 (p->height) / 4);
326   p->vstride = GST_ROUND_UP_4 (p->ystride / 4);
327   p->endptr = p->vp + p->vstride * (GST_ROUND_UP_4 (p->height) / 4);
328 }
329
330 #define gst_video_format_is_packed video_format_is_packed
331 static gboolean
332 video_format_is_packed (GstVideoFormat fmt)
333 {
334   switch (fmt) {
335     case GST_VIDEO_FORMAT_I420:
336     case GST_VIDEO_FORMAT_YV12:
337     case GST_VIDEO_FORMAT_Y41B:
338     case GST_VIDEO_FORMAT_Y42B:
339     case GST_VIDEO_FORMAT_GRAY8:
340     case GST_VIDEO_FORMAT_YUV9:
341     case GST_VIDEO_FORMAT_YVU9:
342       return FALSE;
343     case GST_VIDEO_FORMAT_IYU1:
344     case GST_VIDEO_FORMAT_IYU2:
345     case GST_VIDEO_FORMAT_YUY2:
346     case GST_VIDEO_FORMAT_YVYU:
347     case GST_VIDEO_FORMAT_UYVY:
348     case GST_VIDEO_FORMAT_VYUY:
349     case GST_VIDEO_FORMAT_AYUV:
350     case GST_VIDEO_FORMAT_RGBx:
351     case GST_VIDEO_FORMAT_BGRx:
352     case GST_VIDEO_FORMAT_xRGB:
353     case GST_VIDEO_FORMAT_xBGR:
354     case GST_VIDEO_FORMAT_RGBA:
355     case GST_VIDEO_FORMAT_BGRA:
356     case GST_VIDEO_FORMAT_ARGB:
357     case GST_VIDEO_FORMAT_ABGR:
358     case GST_VIDEO_FORMAT_RGB:
359     case GST_VIDEO_FORMAT_BGR:
360     case GST_VIDEO_FORMAT_RGB8P:
361       return TRUE;
362     default:
363       g_return_val_if_reached (FALSE);
364   }
365   return FALSE;
366 }
367
368 static gint
369 get_num_formats (void)
370 {
371   gint num_formats = 100;
372   fail_unless (gst_video_format_to_string (num_formats) == NULL);
373   while (gst_video_format_to_string (num_formats) == NULL)
374     --num_formats;
375   GST_INFO ("number of known video formats: %d", num_formats);
376   return num_formats + 1;
377 }
378
379 GST_START_TEST (test_video_formats_all)
380 {
381   GstStructure *s;
382   const GValue *val, *list_val;
383   GstCaps *caps;
384   guint num, n, num_formats;
385
386   num_formats = get_num_formats ();
387
388   caps = gst_caps_from_string ("video/x-raw, format=" GST_VIDEO_FORMATS_ALL);
389   s = gst_caps_get_structure (caps, 0);
390   val = gst_structure_get_value (s, "format");
391   fail_unless (val != NULL);
392   fail_unless (GST_VALUE_HOLDS_LIST (val));
393   num = gst_value_list_get_size (val);
394   fail_unless (num > 0);
395   for (n = 0; n < num; ++n) {
396     const gchar *fmt_str;
397
398     list_val = gst_value_list_get_value (val, n);
399     fail_unless (G_VALUE_HOLDS_STRING (list_val));
400     fmt_str = g_value_get_string (list_val);
401     GST_INFO ("format: %s", fmt_str);
402     fail_if (gst_video_format_from_string (fmt_str) ==
403         GST_VIDEO_FORMAT_UNKNOWN);
404   }
405   /* Take into account GST_VIDEO_FORMAT_ENCODED and UNKNOWN */
406   fail_unless_equals_int (num, num_formats - 2);
407
408   gst_caps_unref (caps);
409 }
410
411 GST_END_TEST;
412
413 #define WIDTH 77
414 #define HEIGHT 20
415 GST_START_TEST (test_video_formats_pack_unpack)
416 {
417   guint n, num_formats;
418
419   num_formats = get_num_formats ();
420
421   for (n = GST_VIDEO_FORMAT_ENCODED + 1; n < num_formats; ++n) {
422     const GstVideoFormatInfo *vfinfo, *unpackinfo;
423     GstVideoFormat fmt = n;
424     GstVideoInfo vinfo;
425     gpointer data[GST_VIDEO_MAX_PLANES];
426     gint stride[GST_VIDEO_MAX_PLANES];
427     guint8 *vdata, *unpack_data;
428     gsize vsize, unpack_size;
429     guint p;
430
431     GST_INFO ("testing %s", gst_video_format_to_string (fmt));
432
433     vfinfo = gst_video_format_get_info (fmt);
434     fail_unless (vfinfo != NULL);
435
436     unpackinfo = gst_video_format_get_info (vfinfo->unpack_format);
437     fail_unless (unpackinfo != NULL);
438
439     gst_video_info_init (&vinfo);
440     fail_unless (gst_video_info_set_format (&vinfo, fmt, WIDTH, HEIGHT));
441     vsize = GST_VIDEO_INFO_SIZE (&vinfo);
442     vdata = g_malloc (vsize);
443     memset (vdata, 0x99, vsize);
444
445     g_assert (vfinfo->pack_lines == 1);
446
447     unpack_size =
448         GST_VIDEO_FORMAT_INFO_BITS (unpackinfo) *
449         GST_VIDEO_FORMAT_INFO_N_COMPONENTS (unpackinfo) *
450         GST_ROUND_UP_16 (WIDTH);
451     unpack_data = g_malloc (unpack_size);
452
453     for (p = 0; p < GST_VIDEO_INFO_N_PLANES (&vinfo); ++p) {
454       data[p] = vdata + GST_VIDEO_INFO_PLANE_OFFSET (&vinfo, p);
455       stride[p] = GST_VIDEO_INFO_PLANE_STRIDE (&vinfo, p);
456     }
457
458     /* now unpack */
459     vfinfo->unpack_func (vfinfo, GST_VIDEO_PACK_FLAG_NONE, unpack_data, data,
460         stride, 0, 0, WIDTH);
461
462     /* and pack */
463     vfinfo->pack_func (vfinfo, GST_VIDEO_PACK_FLAG_NONE, unpack_data,
464         unpack_size, data, stride, GST_VIDEO_CHROMA_SITE_UNKNOWN, 0, WIDTH);
465
466     /* now unpack */
467     vfinfo->unpack_func (vfinfo, GST_VIDEO_PACK_FLAG_NONE, unpack_data, data,
468         stride, 0, HEIGHT - 1, WIDTH);
469
470     /* and pack */
471     vfinfo->pack_func (vfinfo, GST_VIDEO_PACK_FLAG_NONE, unpack_data,
472         unpack_size, data, stride, GST_VIDEO_CHROMA_SITE_UNKNOWN, HEIGHT - 1,
473         WIDTH);
474
475     g_free (unpack_data);
476     g_free (vdata);
477   }
478 }
479
480 GST_END_TEST;
481 #undef WIDTH
482 #undef HEIGHT
483
484 GST_START_TEST (test_video_formats)
485 {
486   guint i;
487
488   for (i = 0; i < G_N_ELEMENTS (fourcc_list); ++i) {
489     const GstVideoFormatInfo *vf_info;
490     GstVideoFormat fmt;
491     const gchar *s;
492     guint32 fourcc;
493     guint w, h;
494
495     s = fourcc_list[i].fourcc;
496     fourcc = GST_MAKE_FOURCC (s[0], s[1], s[2], s[3]);
497     fmt = gst_video_format_from_fourcc (fourcc);
498
499     if (fmt == GST_VIDEO_FORMAT_UNKNOWN) {
500       GST_DEBUG ("Unknown format %s, skipping tests", fourcc_list[i].fourcc);
501       continue;
502     }
503
504     vf_info = gst_video_format_get_info (fmt);
505     fail_unless (vf_info != NULL);
506
507     fail_unless_equals_int (GST_VIDEO_FORMAT_INFO_FORMAT (vf_info), fmt);
508
509     GST_INFO ("Fourcc %s, packed=%d", fourcc_list[i].fourcc,
510         gst_video_format_is_packed (fmt));
511
512     fail_unless (GST_VIDEO_FORMAT_INFO_IS_YUV (vf_info));
513
514     /* use any non-NULL pointer so we can compare against NULL */
515     {
516       paintinfo paintinfo = { 0, };
517       fourcc_list[i].paint_setup (&paintinfo, (unsigned char *) s);
518       if (paintinfo.ap != NULL) {
519         fail_unless (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vf_info));
520       } else {
521         fail_if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vf_info));
522       }
523     }
524
525     for (w = 1; w <= 65; ++w) {
526       for (h = 1; h <= 65; ++h) {
527         GstVideoInfo vinfo;
528         paintinfo paintinfo = { 0, };
529         guint off0, off1, off2, off3;
530         guint cs0, cs1, cs2, cs3;
531         guint size;
532
533         GST_LOG ("%s, %dx%d", fourcc_list[i].fourcc, w, h);
534
535         gst_video_info_init (&vinfo);
536         fail_unless (gst_video_info_set_format (&vinfo, fmt, w, h));
537
538         paintinfo.width = w;
539         paintinfo.height = h;
540         fourcc_list[i].paint_setup (&paintinfo, NULL);
541         fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 0),
542             paintinfo.ystride);
543         if (!gst_video_format_is_packed (fmt)
544             && GST_VIDEO_INFO_N_PLANES (&vinfo) <= 2) {
545           /* planar */
546           fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 1),
547               paintinfo.ustride);
548           fail_unless_equals_int (GST_VIDEO_INFO_COMP_STRIDE (&vinfo, 2),
549               paintinfo.vstride);
550           /* check component_width * height against offsets/size somehow? */
551         }
552
553         size = GST_VIDEO_INFO_SIZE (&vinfo);
554         off0 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 0);
555         off1 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 1);
556         off2 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 2);
557
558         GST_TRACE ("size %d <> %d", size, GPOINTER_TO_INT (paintinfo.endptr));
559         GST_TRACE ("off0 %d <> %d", off0, GPOINTER_TO_INT (paintinfo.yp));
560         GST_TRACE ("off1 %d <> %d", off1, GPOINTER_TO_INT (paintinfo.up));
561         GST_TRACE ("off2 %d <> %d", off2, GPOINTER_TO_INT (paintinfo.vp));
562
563         fail_unless_equals_int (size, GPOINTER_TO_INT (paintinfo.endptr));
564         fail_unless_equals_int (off0, GPOINTER_TO_INT (paintinfo.yp));
565         fail_unless_equals_int (off1, GPOINTER_TO_INT (paintinfo.up));
566         fail_unless_equals_int (off2, GPOINTER_TO_INT (paintinfo.vp));
567
568         /* should be 0 if there's no alpha component */
569         off3 = GST_VIDEO_INFO_COMP_OFFSET (&vinfo, 3);
570         fail_unless_equals_int (off3, GPOINTER_TO_INT (paintinfo.ap));
571
572         cs0 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 0) *
573             GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 0);
574         cs1 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 1) *
575             GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 1);
576         cs2 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 2) *
577             GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 2);
578
579         /* GST_LOG ("cs0=%d,cs1=%d,cs2=%d,off0=%d,off1=%d,off2=%d,size=%d",
580            cs0, cs1, cs2, off0, off1, off2, size); */
581
582         if (!gst_video_format_is_packed (fmt))
583           fail_unless (cs0 <= off1);
584
585         if (GST_VIDEO_FORMAT_INFO_HAS_ALPHA (vinfo.finfo)) {
586           cs3 = GST_VIDEO_INFO_COMP_WIDTH (&vinfo, 3) *
587               GST_VIDEO_INFO_COMP_HEIGHT (&vinfo, 2);
588           fail_unless (cs3 < size);
589           /* U/V/alpha shouldn't take up more space than the Y component */
590           fail_if (cs1 > cs0, "cs1 (%d) should be <= cs0 (%d)", cs1, cs0);
591           fail_if (cs2 > cs0, "cs2 (%d) should be <= cs0 (%d)", cs2, cs0);
592           fail_if (cs3 > cs0, "cs3 (%d) should be <= cs0 (%d)", cs3, cs0);
593
594           /* all components together shouldn't take up more space than size */
595           fail_unless (cs0 + cs1 + cs2 + cs3 <= size);
596         } else {
597           /* U/V shouldn't take up more space than the Y component */
598           fail_if (cs1 > cs0, "cs1 (%d) should be <= cs0 (%d)", cs1, cs0);
599           fail_if (cs2 > cs0, "cs2 (%d) should be <= cs0 (%d)", cs2, cs0);
600
601           /* all components together shouldn't take up more space than size */
602           fail_unless (cs0 + cs1 + cs2 <= size,
603               "cs0 (%d) + cs1 (%d) + cs2 (%d) should be <= size (%d)",
604               cs0, cs1, cs2, size);
605         }
606       }
607     }
608   }
609 }
610
611 GST_END_TEST;
612
613 GST_START_TEST (test_video_formats_overflow)
614 {
615   GstVideoInfo vinfo;
616
617   gst_video_info_init (&vinfo);
618
619   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB, 32768,
620           32767));
621   /* fails due to simplification: we forbid some things that would in theory be fine.
622    * We assume a 128 byte alignment for the width currently
623    * fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB, 32767, 32768));
624    */
625   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB, 32768,
626           32768));
627
628   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB,
629           G_MAXINT / 2, G_MAXINT));
630   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB, G_MAXINT,
631           G_MAXINT / 2));
632   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB,
633           G_MAXINT / 2, G_MAXINT / 2));
634   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB, G_MAXINT,
635           G_MAXINT));
636   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB,
637           G_MAXUINT / 2, G_MAXUINT));
638   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB, G_MAXUINT,
639           G_MAXUINT / 2));
640   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB,
641           G_MAXUINT / 2, G_MAXUINT / 2));
642   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB, G_MAXUINT,
643           G_MAXUINT));
644
645   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB,
646           1073741824 - 128, 1));
647   fail_if (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_ARGB, 1073741824,
648           1));
649
650 }
651
652 GST_END_TEST;
653
654 GST_START_TEST (test_video_formats_rgb)
655 {
656   GstVideoInfo vinfo;
657   gint width, height, framerate_n, framerate_d, par_n, par_d;
658   GstCaps *caps;
659   GstStructure *structure;
660
661   gst_video_info_init (&vinfo);
662   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_RGB, 800,
663           600));
664   vinfo.par_n = 1;
665   vinfo.par_d = 1;
666   vinfo.fps_n = 0;
667   vinfo.fps_d = 1;
668   caps = gst_video_info_to_caps (&vinfo);
669   structure = gst_caps_get_structure (caps, 0);
670
671   fail_unless (gst_structure_get_int (structure, "width", &width));
672   fail_unless (gst_structure_get_int (structure, "height", &height));
673   fail_unless (gst_structure_get_fraction (structure, "framerate", &framerate_n,
674           &framerate_d));
675   fail_unless (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
676           &par_n, &par_d));
677
678   fail_unless (width == 800);
679   fail_unless (height == 600);
680   fail_unless (framerate_n == 0);
681   fail_unless (framerate_d == 1);
682   fail_unless (par_n == 1);
683   fail_unless (par_d == 1);
684
685   gst_caps_unref (caps);
686 }
687
688 GST_END_TEST;
689
690
691 GST_START_TEST (test_video_formats_rgba_large_dimension)
692 {
693   GstVideoInfo vinfo;
694   gint width, height, framerate_n, framerate_d, par_n, par_d;
695   GstCaps *caps;
696   GstStructure *structure;
697
698   gst_video_info_init (&vinfo);
699   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_RGBA, 29700,
700           21000));
701   vinfo.par_n = 1;
702   vinfo.par_d = 1;
703   vinfo.fps_n = 0;
704   vinfo.fps_d = 1;
705   caps = gst_video_info_to_caps (&vinfo);
706   structure = gst_caps_get_structure (caps, 0);
707
708   fail_unless (gst_structure_get_int (structure, "width", &width));
709   fail_unless (gst_structure_get_int (structure, "height", &height));
710   fail_unless (gst_structure_get_fraction (structure, "framerate", &framerate_n,
711           &framerate_d));
712   fail_unless (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
713           &par_n, &par_d));
714
715   fail_unless (width == 29700);
716   fail_unless (height == 21000);
717   fail_unless (framerate_n == 0);
718   fail_unless (framerate_d == 1);
719   fail_unless (par_n == 1);
720   fail_unless (par_d == 1);
721   fail_unless (vinfo.size == (gsize) 29700 * 21000 * 4);
722
723   gst_caps_unref (caps);
724 }
725
726 GST_END_TEST;
727
728 GST_START_TEST (test_guess_framerate)
729 {
730   /* Check some obvious exact framerates */
731   gint fps_n, fps_d;
732   fail_unless (gst_video_guess_framerate (GST_SECOND / 24, &fps_n, &fps_d));
733   fail_unless (fps_n == 24 && fps_d == 1);
734
735   fail_unless (gst_video_guess_framerate (GST_SECOND / 30, &fps_n, &fps_d));
736   fail_unless (fps_n == 30 && fps_d == 1);
737
738   fail_unless (gst_video_guess_framerate (GST_SECOND / 25, &fps_n, &fps_d));
739   fail_unless (fps_n == 25 && fps_d == 1);
740
741   /* Some NTSC rates: */
742   fail_unless (gst_video_guess_framerate (GST_SECOND * 1001 / 30000, &fps_n,
743           &fps_d));
744   fail_unless (fps_n == 30000 && fps_d == 1001);
745
746   fail_unless (gst_video_guess_framerate (GST_SECOND * 1001 / 24000, &fps_n,
747           &fps_d));
748   fail_unless (fps_n == 24000 && fps_d == 1001);
749
750   fail_unless (gst_video_guess_framerate (GST_SECOND * 1001 / 60000, &fps_n,
751           &fps_d));
752   fail_unless (fps_n == 60000 && fps_d == 1001);
753
754   /* Check some high FPS, low durations */
755   fail_unless (gst_video_guess_framerate (GST_SECOND / 9000, &fps_n, &fps_d));
756   fail_unless (fps_n == 9000 && fps_d == 1);
757   fail_unless (gst_video_guess_framerate (GST_SECOND / 10000, &fps_n, &fps_d));
758   fail_unless (fps_n == 10000 && fps_d == 1);
759   fail_unless (gst_video_guess_framerate (GST_SECOND / 11000, &fps_n, &fps_d));
760   fail_unless (fps_n == 11000 && fps_d == 1);
761   fail_unless (gst_video_guess_framerate (GST_SECOND / 20000, &fps_n, &fps_d));
762   fail_unless (fps_n == 20000 && fps_d == 1);
763   fail_unless (gst_video_guess_framerate (GST_SECOND / 100000, &fps_n, &fps_d));
764   fail_unless (fps_n == 100000 && fps_d == 1);
765 }
766
767 GST_END_TEST;
768
769 GST_START_TEST (test_dar_calc)
770 {
771   guint display_ratio_n, display_ratio_d;
772
773   /* Ensure that various Display Ratio calculations are correctly done */
774   /* video 768x576, par 16/15, display par 16/15 = 4/3 */
775   fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
776           &display_ratio_d, 768, 576, 16, 15, 16, 15));
777   fail_unless (display_ratio_n == 4 && display_ratio_d == 3);
778
779   /* video 720x480, par 32/27, display par 1/1 = 16/9 */
780   fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
781           &display_ratio_d, 720, 480, 32, 27, 1, 1));
782   fail_unless (display_ratio_n == 16 && display_ratio_d == 9);
783
784   /* video 360x288, par 533333/500000, display par 16/15 =
785    * dar 1599999/1600000 */
786   fail_unless (gst_video_calculate_display_ratio (&display_ratio_n,
787           &display_ratio_d, 360, 288, 533333, 500000, 16, 15));
788   fail_unless (display_ratio_n == 1599999 && display_ratio_d == 1280000);
789 }
790
791 GST_END_TEST;
792
793 GST_START_TEST (test_parse_caps_rgb)
794 {
795   struct
796   {
797     const gchar *tmpl_caps_string;
798     GstVideoFormat fmt;
799   } formats[] = {
800     /* 24 bit */
801     {
802     GST_VIDEO_CAPS_MAKE ("RGB"), GST_VIDEO_FORMAT_RGB}, {
803     GST_VIDEO_CAPS_MAKE ("BGR"), GST_VIDEO_FORMAT_BGR},
804         /* 32 bit (no alpha) */
805     {
806     GST_VIDEO_CAPS_MAKE ("RGBx"), GST_VIDEO_FORMAT_RGBx}, {
807     GST_VIDEO_CAPS_MAKE ("xRGB"), GST_VIDEO_FORMAT_xRGB}, {
808     GST_VIDEO_CAPS_MAKE ("BGRx"), GST_VIDEO_FORMAT_BGRx}, {
809     GST_VIDEO_CAPS_MAKE ("xBGR"), GST_VIDEO_FORMAT_xBGR},
810         /* 32 bit (with alpha) */
811     {
812     GST_VIDEO_CAPS_MAKE ("RGBA"), GST_VIDEO_FORMAT_RGBA}, {
813     GST_VIDEO_CAPS_MAKE ("ARGB"), GST_VIDEO_FORMAT_ARGB}, {
814     GST_VIDEO_CAPS_MAKE ("BGRA"), GST_VIDEO_FORMAT_BGRA}, {
815     GST_VIDEO_CAPS_MAKE ("ABGR"), GST_VIDEO_FORMAT_ABGR},
816         /* 16 bit */
817     {
818     GST_VIDEO_CAPS_MAKE ("RGB16"), GST_VIDEO_FORMAT_RGB16}, {
819     GST_VIDEO_CAPS_MAKE ("BGR16"), GST_VIDEO_FORMAT_BGR16}, {
820     GST_VIDEO_CAPS_MAKE ("RGB15"), GST_VIDEO_FORMAT_RGB15}, {
821     GST_VIDEO_CAPS_MAKE ("BGR15"), GST_VIDEO_FORMAT_BGR15}
822   };
823   gint i;
824
825   for (i = 0; i < G_N_ELEMENTS (formats); ++i) {
826     GstVideoInfo vinfo;
827     GstCaps *caps, *caps2;
828
829     caps = gst_caps_from_string (formats[i].tmpl_caps_string);
830     fail_unless (caps != NULL);
831     gst_caps_set_simple (caps, "width", G_TYPE_INT, 2 * (i + 1), "height",
832         G_TYPE_INT, i + 1, "framerate", GST_TYPE_FRACTION, 15, 1,
833         "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
834         "interlace-mode", G_TYPE_STRING, "progressive",
835         "colorimetry", G_TYPE_STRING, "1:1:0:0",
836         "multiview-mode", G_TYPE_STRING, "mono",
837         "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET, 0,
838         GST_FLAG_SET_MASK_EXACT, NULL);
839     g_assert (gst_caps_is_fixed (caps));
840
841     GST_DEBUG ("testing caps: %" GST_PTR_FORMAT, caps);
842
843     gst_video_info_init (&vinfo);
844     fail_unless (gst_video_info_from_caps (&vinfo, caps));
845     fail_unless_equals_int (GST_VIDEO_INFO_FORMAT (&vinfo), formats[i].fmt);
846     fail_unless_equals_int (GST_VIDEO_INFO_WIDTH (&vinfo), 2 * (i + 1));
847     fail_unless_equals_int (GST_VIDEO_INFO_HEIGHT (&vinfo), i + 1);
848
849     /* make sure they're serialised back correctly */
850     caps2 = gst_video_info_to_caps (&vinfo);
851     fail_unless (caps2 != NULL);
852     if (!gst_caps_is_equal (caps, caps2)) {
853       gchar *caps1s = gst_caps_to_string (caps);
854       gchar *caps2s = gst_caps_to_string (caps2);
855       fail ("caps [%s] not equal to caps2 [%s]", caps1s, caps2s);
856       g_free (caps1s);
857       g_free (caps2s);
858     }
859
860     gst_caps_unref (caps);
861     gst_caps_unref (caps2);
862   }
863 }
864
865 GST_END_TEST;
866
867 GST_START_TEST (test_parse_caps_multiview)
868 {
869   gint i, j;
870   GstVideoMultiviewMode modes[] = {
871     GST_VIDEO_MULTIVIEW_MODE_MONO,
872     GST_VIDEO_MULTIVIEW_MODE_LEFT,
873     GST_VIDEO_MULTIVIEW_MODE_RIGHT,
874     GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE,
875     GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX,
876     GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED,
877     GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED,
878     GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM,
879     GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD,
880     GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME,
881     GST_VIDEO_MULTIVIEW_MODE_MULTIVIEW_FRAME_BY_FRAME,
882     GST_VIDEO_MULTIVIEW_MODE_SEPARATED,
883   };
884   GstVideoMultiviewFlags flags[] = {
885     GST_VIDEO_MULTIVIEW_FLAGS_NONE,
886     GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST,
887     GST_VIDEO_MULTIVIEW_FLAGS_LEFT_FLIPPED,
888     GST_VIDEO_MULTIVIEW_FLAGS_LEFT_FLOPPED,
889     GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_FLIPPED,
890     GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_FLOPPED,
891     GST_VIDEO_MULTIVIEW_FLAGS_MIXED_MONO,
892     GST_VIDEO_MULTIVIEW_FLAGS_MIXED_MONO |
893         GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST,
894     GST_VIDEO_MULTIVIEW_FLAGS_MIXED_MONO |
895         GST_VIDEO_MULTIVIEW_FLAGS_LEFT_FLIPPED
896   };
897
898   for (i = 0; i < G_N_ELEMENTS (modes); i++) {
899     for (j = 0; j < G_N_ELEMENTS (flags); j++) {
900       GstVideoInfo vinfo;
901       GstCaps *caps;
902
903       gst_video_info_init (&vinfo);
904       fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420,
905               320, 240));
906
907       GST_VIDEO_INFO_MULTIVIEW_MODE (&vinfo) = modes[i];
908       GST_VIDEO_INFO_MULTIVIEW_FLAGS (&vinfo) = flags[j];
909
910       caps = gst_video_info_to_caps (&vinfo);
911       fail_if (caps == NULL);
912       GST_LOG ("mview mode %d flags %x -> caps %" GST_PTR_FORMAT,
913           modes[i], flags[j], caps);
914
915       fail_unless (gst_video_info_from_caps (&vinfo, caps));
916
917       GST_LOG ("mview mode %d flags %x -> info mode %d flags %x",
918           modes[i], flags[j], GST_VIDEO_INFO_MULTIVIEW_MODE (&vinfo),
919           GST_VIDEO_INFO_MULTIVIEW_FLAGS (&vinfo));
920
921       fail_unless (GST_VIDEO_INFO_MULTIVIEW_MODE (&vinfo) == modes[i],
922           "Expected multiview mode %d got mode %d", modes[i],
923           GST_VIDEO_INFO_MULTIVIEW_MODE (&vinfo));
924       fail_unless (GST_VIDEO_INFO_MULTIVIEW_FLAGS (&vinfo) == flags[j],
925           "Expected multiview flags 0x%x got 0x%x", flags[j],
926           GST_VIDEO_INFO_MULTIVIEW_FLAGS (&vinfo));
927
928       gst_caps_unref (caps);
929     }
930   }
931 }
932
933 GST_END_TEST;
934
935 typedef struct
936 {
937   const gchar *string_from;
938   const gchar *string_to;
939   const gchar *name;
940   GstVideoColorimetry color;
941 } ColorimetryTest;
942
943 #define MAKE_COLORIMETRY_TEST(s1,s2,n,r,m,t,p) { s1, s2, n,         \
944     { GST_VIDEO_COLOR_RANGE ##r, GST_VIDEO_COLOR_MATRIX_ ##m,       \
945     GST_VIDEO_TRANSFER_ ##t, GST_VIDEO_COLOR_PRIMARIES_ ##p } }
946
947 GST_START_TEST (test_parse_colorimetry)
948 {
949   ColorimetryTest tests[] = {
950     MAKE_COLORIMETRY_TEST ("bt601", "bt601", "bt601",
951         _16_235, BT601, BT601, SMPTE170M),
952     MAKE_COLORIMETRY_TEST ("2:4:5:4", "2:4:5:4", NULL,
953         _16_235, BT601, BT709, SMPTE170M),
954     MAKE_COLORIMETRY_TEST ("bt709", "bt709", "bt709",
955         _16_235, BT709, BT709, BT709),
956     MAKE_COLORIMETRY_TEST ("smpte240m", "smpte240m", "smpte240m",
957         _16_235, SMPTE240M, SMPTE240M, SMPTE240M),
958     MAKE_COLORIMETRY_TEST ("sRGB", "sRGB", "sRGB",
959         _0_255, RGB, SRGB, BT709),
960     MAKE_COLORIMETRY_TEST ("bt2020", "bt2020", "bt2020",
961         _16_235, BT2020, BT2020_12, BT2020),
962     MAKE_COLORIMETRY_TEST ("1:4:0:0", "1:4:0:0", NULL,
963         _0_255, BT601, UNKNOWN, UNKNOWN),
964   };
965   gint i;
966
967   for (i = 0; i < G_N_ELEMENTS (tests); i++) {
968     const ColorimetryTest *test = &tests[i];
969     GstVideoColorimetry color;
970     gchar *string;
971
972     fail_unless (gst_video_colorimetry_from_string (&color, test->string_from));
973     fail_unless_equals_int (color.range, test->color.range);
974     fail_unless_equals_int (color.matrix, test->color.matrix);
975     fail_unless_equals_int (color.transfer, test->color.transfer);
976     fail_unless_equals_int (color.primaries, test->color.primaries);
977
978     string = gst_video_colorimetry_to_string (&color);
979     fail_unless_equals_string (string, test->string_to);
980     g_free (string);
981
982     fail_unless (gst_video_colorimetry_is_equal (&color, &test->color));
983
984     if (test->name)
985       fail_unless (gst_video_colorimetry_matches (&color, test->name));
986   }
987 }
988
989 GST_END_TEST;
990
991 GST_START_TEST (test_events)
992 {
993   GstEvent *e;
994   gboolean in_still;
995
996   e = gst_video_event_new_still_frame (TRUE);
997   fail_if (e == NULL, "Failed to create still frame event");
998   fail_unless (gst_video_event_parse_still_frame (e, &in_still),
999       "Failed to parse still frame event");
1000   fail_unless (gst_video_event_parse_still_frame (e, NULL),
1001       "Failed to parse still frame event w/ in_still == NULL");
1002   fail_unless (in_still == TRUE);
1003   gst_event_unref (e);
1004
1005   e = gst_video_event_new_still_frame (FALSE);
1006   fail_if (e == NULL, "Failed to create still frame event");
1007   fail_unless (gst_video_event_parse_still_frame (e, &in_still),
1008       "Failed to parse still frame event");
1009   fail_unless (gst_video_event_parse_still_frame (e, NULL),
1010       "Failed to parse still frame event w/ in_still == NULL");
1011   fail_unless (in_still == FALSE);
1012   gst_event_unref (e);
1013 }
1014
1015 GST_END_TEST;
1016
1017 GST_START_TEST (test_convert_frame)
1018 {
1019   GstVideoInfo vinfo;
1020   GstCaps *from_caps, *to_caps;
1021   GstBuffer *from_buffer;
1022   GstSample *from_sample, *to_sample;
1023   GError *error = NULL;
1024   gint i;
1025   GstMapInfo map;
1026
1027   gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
1028
1029   from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);
1030
1031   gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
1032   for (i = 0; i < 640 * 480; i++) {
1033     map.data[4 * i + 0] = 0;    /* x */
1034     map.data[4 * i + 1] = 255;  /* R */
1035     map.data[4 * i + 2] = 0;    /* G */
1036     map.data[4 * i + 3] = 0;    /* B */
1037   }
1038   gst_buffer_unmap (from_buffer, &map);
1039
1040   gst_video_info_init (&vinfo);
1041   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640,
1042           480));
1043   vinfo.fps_n = 25;
1044   vinfo.fps_d = 1;
1045   vinfo.par_n = 1;
1046   vinfo.par_d = 1;
1047   from_caps = gst_video_info_to_caps (&vinfo);
1048
1049   from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);
1050
1051   to_caps =
1052       gst_caps_from_string
1053       ("something/that, does=(string)not, exist=(boolean)FALSE");
1054
1055   to_sample =
1056       gst_video_convert_sample (from_sample, to_caps,
1057       GST_CLOCK_TIME_NONE, &error);
1058   fail_if (to_sample != NULL);
1059   fail_unless (error != NULL);
1060   g_error_free (error);
1061   error = NULL;
1062
1063   gst_caps_unref (to_caps);
1064   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420, 240,
1065           320));
1066   vinfo.fps_n = 25;
1067   vinfo.fps_d = 1;
1068   vinfo.par_n = 1;
1069   vinfo.par_d = 2;
1070   to_caps = gst_video_info_to_caps (&vinfo);
1071
1072   to_sample =
1073       gst_video_convert_sample (from_sample, to_caps,
1074       GST_CLOCK_TIME_NONE, &error);
1075   fail_unless (to_sample != NULL);
1076   fail_unless (error == NULL);
1077
1078   gst_buffer_unref (from_buffer);
1079   gst_caps_unref (from_caps);
1080   gst_sample_unref (from_sample);
1081   gst_sample_unref (to_sample);
1082   gst_caps_unref (to_caps);
1083 }
1084
1085 GST_END_TEST;
1086
1087 typedef struct
1088 {
1089   GMainLoop *loop;
1090   GstSample *sample;
1091   GError *error;
1092 } ConvertFrameContext;
1093
1094 static void
1095 convert_sample_async_callback (GstSample * sample, GError * err,
1096     ConvertFrameContext * cf_data)
1097 {
1098   cf_data->sample = sample;
1099   cf_data->error = err;
1100
1101   g_main_loop_quit (cf_data->loop);
1102 }
1103
1104 GST_START_TEST (test_convert_frame_async)
1105 {
1106   GstVideoInfo vinfo;
1107   GstCaps *from_caps, *to_caps;
1108   GstBuffer *from_buffer;
1109   GstSample *from_sample;
1110   gint i;
1111   GstMapInfo map;
1112   GMainLoop *loop;
1113   ConvertFrameContext cf_data = { NULL, NULL, NULL };
1114
1115   gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
1116
1117   from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);
1118
1119   gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
1120   for (i = 0; i < 640 * 480; i++) {
1121     map.data[4 * i + 0] = 0;    /* x */
1122     map.data[4 * i + 1] = 255;  /* R */
1123     map.data[4 * i + 2] = 0;    /* G */
1124     map.data[4 * i + 3] = 0;    /* B */
1125   }
1126   gst_buffer_unmap (from_buffer, &map);
1127
1128   loop = cf_data.loop = g_main_loop_new (NULL, FALSE);
1129
1130   gst_video_info_init (&vinfo);
1131   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640,
1132           470));
1133   vinfo.par_n = 1;
1134   vinfo.par_d = 1;
1135   vinfo.fps_n = 25;
1136   vinfo.fps_d = 1;
1137   from_caps = gst_video_info_to_caps (&vinfo);
1138
1139   from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);
1140   gst_buffer_unref (from_buffer);
1141   gst_caps_unref (from_caps);
1142
1143   gst_video_info_init (&vinfo);
1144   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_I420, 240,
1145           320));
1146   vinfo.par_n = 1;
1147   vinfo.par_d = 2;
1148   vinfo.fps_n = 25;
1149   vinfo.fps_d = 1;
1150   to_caps = gst_video_info_to_caps (&vinfo);
1151   gst_video_convert_sample_async (from_sample, to_caps,
1152       GST_CLOCK_TIME_NONE,
1153       (GstVideoConvertSampleCallback) convert_sample_async_callback, &cf_data,
1154       NULL);
1155   g_main_loop_run (loop);
1156   fail_unless (cf_data.sample != NULL);
1157   fail_unless (cf_data.error == NULL);
1158
1159   gst_sample_unref (cf_data.sample);
1160   gst_caps_unref (to_caps);
1161   gst_sample_unref (from_sample);
1162
1163   g_main_loop_unref (loop);
1164 }
1165
1166 GST_END_TEST;
1167
1168 GST_START_TEST (test_convert_frame_async_error)
1169 {
1170   GstVideoInfo vinfo;
1171   GstCaps *from_caps, *to_caps;
1172   GstBuffer *from_buffer;
1173   GstSample *from_sample;
1174   gint i;
1175   GstMapInfo map;
1176   GMainLoop *loop;
1177   ConvertFrameContext cf_data = { NULL, NULL, NULL };
1178
1179   gst_debug_set_threshold_for_name ("default", GST_LEVEL_NONE);
1180
1181   from_buffer = gst_buffer_new_and_alloc (640 * 480 * 4);
1182
1183   gst_buffer_map (from_buffer, &map, GST_MAP_WRITE);
1184   for (i = 0; i < 640 * 480; i++) {
1185     map.data[4 * i + 0] = 0;    /* x */
1186     map.data[4 * i + 1] = 255;  /* R */
1187     map.data[4 * i + 2] = 0;    /* G */
1188     map.data[4 * i + 3] = 0;    /* B */
1189   }
1190   gst_buffer_unmap (from_buffer, &map);
1191
1192   gst_video_info_init (&vinfo);
1193   fail_unless (gst_video_info_set_format (&vinfo, GST_VIDEO_FORMAT_xRGB, 640,
1194           470));
1195   vinfo.par_n = 1;
1196   vinfo.par_d = 1;
1197   vinfo.fps_n = 25;
1198   vinfo.fps_d = 1;
1199   from_caps = gst_video_info_to_caps (&vinfo);
1200
1201   to_caps =
1202       gst_caps_from_string
1203       ("something/that, does=(string)not, exist=(boolean)FALSE");
1204
1205   loop = cf_data.loop = g_main_loop_new (NULL, FALSE);
1206
1207   from_sample = gst_sample_new (from_buffer, from_caps, NULL, NULL);
1208   gst_buffer_unref (from_buffer);
1209   gst_caps_unref (from_caps);
1210
1211   gst_video_convert_sample_async (from_sample, to_caps,
1212       GST_CLOCK_TIME_NONE,
1213       (GstVideoConvertSampleCallback) convert_sample_async_callback, &cf_data,
1214       NULL);
1215
1216   g_main_loop_run (loop);
1217
1218   fail_if (cf_data.sample != NULL);
1219   fail_unless (cf_data.error != NULL);
1220   g_error_free (cf_data.error);
1221   cf_data.error = NULL;
1222
1223   gst_caps_unref (to_caps);
1224   gst_sample_unref (from_sample);
1225
1226   g_main_loop_unref (loop);
1227 }
1228
1229 GST_END_TEST;
1230
1231 GST_START_TEST (test_video_size_from_caps)
1232 {
1233   GstVideoInfo vinfo;
1234   GstCaps *caps;
1235
1236   caps = gst_caps_new_simple ("video/x-raw",
1237       "format", G_TYPE_STRING, "YV12",
1238       "width", G_TYPE_INT, 640,
1239       "height", G_TYPE_INT, 480, "framerate", GST_TYPE_FRACTION, 25, 1, NULL);
1240
1241   gst_video_info_init (&vinfo);
1242   fail_unless (gst_video_info_from_caps (&vinfo, caps));
1243   fail_unless (GST_VIDEO_INFO_SIZE (&vinfo) == (640 * 480 * 12 / 8));
1244
1245   gst_caps_unref (caps);
1246 }
1247
1248 GST_END_TEST;
1249
1250 GST_START_TEST (test_interlace_mode)
1251 {
1252   GstVideoInfo vinfo;
1253   GstCaps *caps;
1254   GstStructure *structure;
1255   GstCapsFeatures *features;
1256   const char *mode_str, *order_str;
1257   int mode;
1258   GstVideoFieldOrder order;
1259
1260   gst_video_info_init (&vinfo);
1261
1262   /* Progressive */
1263   fail_unless (gst_video_info_set_interlaced_format (&vinfo,
1264           GST_VIDEO_FORMAT_YV12, GST_VIDEO_INTERLACE_MODE_PROGRESSIVE, 320,
1265           240));
1266   fail_unless (GST_VIDEO_INFO_SIZE (&vinfo) == 115200);
1267
1268   caps = gst_video_info_to_caps (&vinfo);
1269   fail_unless (caps != NULL);
1270   structure = gst_caps_get_structure (caps, 0);
1271   fail_unless (structure != NULL);
1272   mode_str = gst_structure_get_string (structure, "interlace-mode");
1273   mode = gst_video_interlace_mode_from_string (mode_str);
1274   fail_unless (mode == GST_VIDEO_INTERLACE_MODE_PROGRESSIVE);
1275
1276   /* Converting back to video info */
1277   fail_unless (gst_video_info_from_caps (&vinfo, caps));
1278   fail_unless (GST_VIDEO_INFO_INTERLACE_MODE (&vinfo) ==
1279       GST_VIDEO_INTERLACE_MODE_PROGRESSIVE);
1280
1281   gst_caps_unref (caps);
1282
1283   /* Interlaced with alternate frame on buffers */
1284   fail_unless (gst_video_info_set_interlaced_format (&vinfo,
1285           GST_VIDEO_FORMAT_YV12, GST_VIDEO_INTERLACE_MODE_ALTERNATE, 320, 240));
1286   fail_unless (GST_VIDEO_INFO_SIZE (&vinfo) == 57600);
1287   GST_VIDEO_INFO_FIELD_ORDER (&vinfo) = GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST;
1288
1289   caps = gst_video_info_to_caps (&vinfo);
1290   fail_unless (caps != NULL);
1291   structure = gst_caps_get_structure (caps, 0);
1292   fail_unless (structure != NULL);
1293   mode_str = gst_structure_get_string (structure, "interlace-mode");
1294   mode = gst_video_interlace_mode_from_string (mode_str);
1295   fail_unless (mode == GST_VIDEO_INTERLACE_MODE_ALTERNATE);
1296   order_str = gst_structure_get_string (structure, "field-order");
1297   order = gst_video_field_order_from_string (order_str);
1298   fail_unless (order == GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST);
1299   /* 'alternate' mode must always be accompanied by interlaced caps feature. */
1300   features = gst_caps_get_features (caps, 0);
1301   fail_unless (gst_caps_features_contains (features,
1302           GST_CAPS_FEATURE_FORMAT_INTERLACED));
1303
1304   /* Converting back to video info */
1305   fail_unless (gst_video_info_from_caps (&vinfo, caps));
1306   fail_unless (GST_VIDEO_INFO_INTERLACE_MODE (&vinfo) ==
1307       GST_VIDEO_INTERLACE_MODE_ALTERNATE);
1308   fail_unless (GST_VIDEO_INFO_FIELD_ORDER (&vinfo) ==
1309       GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST);
1310
1311   gst_caps_unref (caps);
1312
1313   /* gst_video_info_from_caps() fails if an alternate stream doesn't contain
1314    * the caps feature. */
1315   caps =
1316       gst_caps_from_string
1317       ("video/x-raw, format=NV12, width=320, height=240, interlace-mode=alternate");
1318   fail_unless (caps);
1319
1320   fail_if (gst_video_info_from_caps (&vinfo, caps));
1321   gst_caps_unref (caps);
1322
1323   /* ... but it's ok for encoded video */
1324   caps =
1325       gst_caps_from_string
1326       ("video/x-h265, width=320, height=240, interlace-mode=alternate");
1327   fail_unless (caps);
1328
1329   fail_unless (gst_video_info_from_caps (&vinfo, caps));
1330   gst_caps_unref (caps);
1331 }
1332
1333 GST_END_TEST;
1334
1335 GST_START_TEST (test_overlay_composition)
1336 {
1337   GstVideoOverlayComposition *comp1, *comp2;
1338   GstVideoOverlayRectangle *rect1, *rect2;
1339   GstVideoOverlayCompositionMeta *ometa;
1340   GstBuffer *pix1, *pix2, *buf;
1341   GstVideoMeta *vmeta;
1342   guint seq1, seq2;
1343   guint w, h, stride;
1344   gint x, y;
1345   guint8 val;
1346
1347   pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
1348   gst_buffer_memset (pix1, 0, 0, gst_buffer_get_size (pix1));
1349
1350   gst_buffer_add_video_meta (pix1, GST_VIDEO_FRAME_FLAG_NONE,
1351       GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, 200, 50);
1352   rect1 = gst_video_overlay_rectangle_new_raw (pix1,
1353       600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1354
1355   gst_buffer_unref (pix1);
1356   pix1 = NULL;
1357
1358   comp1 = gst_video_overlay_composition_new (rect1);
1359   fail_unless (gst_video_overlay_composition_n_rectangles (comp1) == 1);
1360   fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 0) == rect1);
1361   fail_unless (gst_video_overlay_composition_get_rectangle (comp1, 1) == NULL);
1362
1363   /* rectangle was created first, sequence number should be smaller */
1364   seq1 = gst_video_overlay_rectangle_get_seqnum (rect1);
1365   seq2 = gst_video_overlay_composition_get_seqnum (comp1);
1366   fail_unless (seq1 < seq2);
1367
1368   /* composition took own ref, so refcount is 2 now, so this should fail */
1369   ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 50,
1370           600, 300, 50));
1371
1372   /* drop our ref, so refcount is 1 (we know it will continue to be valid) */
1373   gst_video_overlay_rectangle_unref (rect1);
1374   gst_video_overlay_rectangle_set_render_rectangle (rect1, 50, 600, 300, 50);
1375
1376   comp2 = gst_video_overlay_composition_new (rect1);
1377   fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1);
1378   fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect1);
1379   fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL);
1380
1381   fail_unless (seq1 < gst_video_overlay_composition_get_seqnum (comp2));
1382   fail_unless (seq2 < gst_video_overlay_composition_get_seqnum (comp2));
1383
1384   /* now refcount is 2 again because comp2 has also taken a ref, so must fail */
1385   ASSERT_CRITICAL (gst_video_overlay_rectangle_set_render_rectangle (rect1, 0,
1386           0, 1, 1));
1387
1388   /* this should make a copy of the rectangles so drop the original
1389    * second ref on rect1 */
1390   comp2 = gst_video_overlay_composition_make_writable (comp2);
1391   gst_video_overlay_rectangle_set_render_rectangle (rect1, 51, 601, 301, 51);
1392
1393   rect2 = gst_video_overlay_composition_get_rectangle (comp2, 0);
1394   fail_unless (gst_video_overlay_composition_n_rectangles (comp2) == 1);
1395   fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 0) == rect2);
1396   fail_unless (gst_video_overlay_composition_get_rectangle (comp2, 1) == NULL);
1397   fail_unless (rect1 != rect2);
1398
1399   gst_video_overlay_composition_add_rectangle (comp1, rect2);
1400   gst_video_overlay_composition_ref (comp1);
1401   ASSERT_CRITICAL (gst_video_overlay_composition_add_rectangle (comp1, rect2));
1402   gst_video_overlay_composition_unref (comp1);
1403
1404   /* make sure the copy really worked */
1405   gst_video_overlay_rectangle_get_render_rectangle (rect1, &x, &y, &w, &h);
1406   fail_unless_equals_int (x, 51);
1407   fail_unless_equals_int (y, 601);
1408   fail_unless_equals_int (w, 301);
1409   fail_unless_equals_int (h, 51);
1410
1411   /* get scaled pixbuf and touch last byte */
1412   pix1 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1413       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1414   stride = 4 * w;
1415   fail_unless (gst_buffer_get_size (pix1) > ((h - 1) * stride + (w * 4) - 1),
1416       "size %u vs. last pixel offset %u", gst_buffer_get_size (pix1),
1417       ((h - 1) * stride + (w * 4) - 1));
1418   gst_buffer_extract (pix1, ((h - 1) * stride + (w * 4) - 1), &val, 1);
1419   fail_unless_equals_int (val, 0);
1420
1421   gst_video_overlay_rectangle_get_render_rectangle (rect2, &x, &y, &w, &h);
1422   fail_unless_equals_int (x, 50);
1423   fail_unless_equals_int (y, 600);
1424   fail_unless_equals_int (w, 300);
1425   fail_unless_equals_int (h, 50);
1426
1427   /* get scaled pixbuf and touch last byte */
1428   pix2 = gst_video_overlay_rectangle_get_pixels_raw (rect2,
1429       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1430   stride = 4 * w;
1431   fail_unless (gst_buffer_get_size (pix2) > ((h - 1) * stride + (w * 4) - 1),
1432       "size %u vs. last pixel offset %u", gst_buffer_get_size (pix1),
1433       ((h - 1) * stride + (w * 4) - 1));
1434   gst_buffer_extract (pix2, ((h - 1) * stride + (w * 4) - 1), &val, 1);
1435   fail_unless_equals_int (val, 0);
1436
1437   /* get scaled pixbuf again, should be the same buffer as before (caching) */
1438   pix1 = gst_video_overlay_rectangle_get_pixels_raw (rect2,
1439       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1440   fail_unless (pix1 == pix2);
1441
1442   /* get in different format */
1443   pix1 = gst_video_overlay_rectangle_get_pixels_ayuv (rect2,
1444       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1445   fail_unless (pix1 != pix2);
1446   /* get it again, should be same (caching) */
1447   pix2 = gst_video_overlay_rectangle_get_pixels_ayuv (rect2,
1448       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1449   fail_unless (pix1 == pix2);
1450   /* get unscaled, should be different */
1451   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_ayuv (rect2,
1452       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1453   fail_unless (pix1 != pix2);
1454   /* but should be cached */
1455   pix1 = gst_video_overlay_rectangle_get_pixels_unscaled_ayuv (rect2,
1456       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1457   fail_unless (pix1 == pix2);
1458
1459   vmeta = gst_buffer_get_video_meta (pix1);
1460   fail_unless (vmeta != NULL);
1461   w = vmeta->width;
1462   h = vmeta->height;
1463   fail_unless_equals_int (w, 200);
1464   fail_unless_equals_int (h, 50);
1465   fail_unless_equals_int (vmeta->format,
1466       GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV);
1467   fail_unless (gst_buffer_get_size (pix1) == w * h * 4);
1468   gst_buffer_extract (pix1, 0, &seq1, 4);
1469   fail_unless (seq1 != 0);
1470
1471   /* now compare the original unscaled ones */
1472   pix1 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1473       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1474   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect2,
1475       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1476
1477   vmeta = gst_buffer_get_video_meta (pix2);
1478   fail_unless (vmeta != NULL);
1479   w = vmeta->width;
1480   h = vmeta->height;
1481
1482   /* the original pixel buffers should be identical */
1483   fail_unless (pix1 == pix2);
1484   fail_unless_equals_int (w, 200);
1485   fail_unless_equals_int (h, 50);
1486   stride = 4 * w;
1487
1488   /* touch last byte */
1489   fail_unless (gst_buffer_get_size (pix1) > ((h - 1) * stride + (w * 4) - 1),
1490       "size %u vs. last pixel offset %u", gst_buffer_get_size (pix1),
1491       ((h - 1) * stride + (w * 4) - 1));
1492   gst_buffer_extract (pix1, ((h - 1) * stride + (w * 4) - 1), &val, 1);
1493   fail_unless_equals_int (val, 0);
1494
1495   /* test attaching and retrieving of compositions to/from buffers */
1496   buf = gst_buffer_new ();
1497   fail_unless (gst_buffer_get_video_overlay_composition_meta (buf) == NULL);
1498
1499   gst_buffer_ref (buf);
1500   /* buffer now has refcount of 2, so its metadata is not writable.
1501    * only check this if we are not running in valgrind, as it leaks */
1502 #ifdef HAVE_VALGRIND
1503   if (!RUNNING_ON_VALGRIND) {
1504     ASSERT_CRITICAL (gst_buffer_add_video_overlay_composition_meta (buf,
1505             comp1));
1506   }
1507 #endif
1508   gst_buffer_unref (buf);
1509   gst_buffer_add_video_overlay_composition_meta (buf, comp1);
1510   ometa = gst_buffer_get_video_overlay_composition_meta (buf);
1511   fail_unless (ometa != NULL);
1512   fail_unless (ometa->overlay == comp1);
1513   fail_unless (gst_buffer_remove_video_overlay_composition_meta (buf, ometa));
1514   gst_buffer_add_video_overlay_composition_meta (buf, comp2);
1515   ometa = gst_buffer_get_video_overlay_composition_meta (buf);
1516   fail_unless (ometa->overlay == comp2);
1517   fail_unless (gst_buffer_remove_video_overlay_composition_meta (buf, ometa));
1518   fail_unless (gst_buffer_get_video_overlay_composition_meta (buf) == NULL);
1519
1520   /* make sure the buffer cleans up its composition ref when unreffed */
1521   gst_buffer_add_video_overlay_composition_meta (buf, comp2);
1522   gst_buffer_unref (buf);
1523
1524   gst_video_overlay_composition_unref (comp2);
1525   gst_video_overlay_composition_unref (comp1);
1526 }
1527
1528 GST_END_TEST;
1529
1530 GST_START_TEST (test_overlay_composition_premultiplied_alpha)
1531 {
1532   GstVideoOverlayRectangle *rect1;
1533   GstVideoMeta *vmeta;
1534   GstBuffer *pix1, *pix2, *pix3, *pix4, *pix5;
1535   GstBuffer *pix6, *pix7, *pix8, *pix9, *pix10;
1536   guint8 *data5, *data7;
1537   guint w, h, w2, h2;
1538   GstMapInfo map;
1539
1540   pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
1541   gst_buffer_memset (pix1, 0, 0x80, gst_buffer_get_size (pix1));
1542
1543   gst_buffer_add_video_meta (pix1, GST_VIDEO_FRAME_FLAG_NONE,
1544       GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, 200, 50);
1545   rect1 = gst_video_overlay_rectangle_new_raw (pix1,
1546       600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1547   gst_buffer_unref (pix1);
1548
1549   /* same flags, unscaled, should be the same buffer */
1550   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1551       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1552   fail_unless (pix1 == pix2);
1553
1554   /* same flags, but scaled */
1555   pix3 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1556       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1557   fail_if (pix3 == pix1 || pix3 == pix2);
1558
1559   /* same again, should hopefully get the same (cached) buffer as before */
1560   pix4 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1561       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1562   fail_unless (pix4 == pix3);
1563
1564   /* just to update the vars */
1565   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1566       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1567
1568   vmeta = gst_buffer_get_video_meta (pix2);
1569   fail_unless (vmeta != NULL);
1570   w = vmeta->width;
1571   h = vmeta->height;
1572
1573   /* now, let's try to get premultiplied alpha from the unpremultiplied input */
1574   pix5 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1575       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1576   fail_if (pix5 == pix1 || pix5 == pix2 || pix5 == pix3);
1577   vmeta = gst_buffer_get_video_meta (pix5);
1578   fail_unless (vmeta != NULL);
1579   w2 = vmeta->width;
1580   h2 = vmeta->height;
1581   fail_unless_equals_int (w, w2);
1582   fail_unless_equals_int (h, h2);
1583   fail_unless_equals_int (gst_buffer_get_size (pix2),
1584       gst_buffer_get_size (pix5));
1585   gst_buffer_map (pix5, &map, GST_MAP_READ);
1586   fail_if (gst_buffer_memcmp (pix2, 0, map.data, map.size) == 0);
1587   /* make sure it actually did what we expected it to do (input=0x80808080) */
1588   data5 = map.data;
1589 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1590   /* B - G - R - A */
1591   fail_unless_equals_int (data5[0], 0x40);
1592   fail_unless_equals_int (data5[1], 0x40);
1593   fail_unless_equals_int (data5[2], 0x40);
1594   fail_unless_equals_int (data5[3], 0x80);
1595 #else
1596   /* A - R - G - B */
1597   fail_unless_equals_int (data5[0], 0x80);
1598   fail_unless_equals_int (data5[1], 0x40);
1599   fail_unless_equals_int (data5[2], 0x40);
1600   fail_unless_equals_int (data5[3], 0x40);
1601 #endif
1602   gst_buffer_unmap (pix5, &map);
1603
1604   /* same again, now we should be getting back the same buffer as before,
1605    * as it should have been cached */
1606   pix6 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1607       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1608   fail_unless (pix6 == pix5);
1609
1610   /* just to update the stride var */
1611   pix3 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1612       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1613   fail_unless (pix3 == pix4);
1614
1615   /* now try to get scaled premultiplied alpha from unpremultiplied input */
1616   pix7 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1617       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1618   fail_if (pix7 == pix1 || pix7 == pix2 || pix7 == pix3 || pix7 == pix5);
1619
1620   gst_buffer_map (pix7, &map, GST_MAP_READ);
1621   data7 = map.data;
1622   /* make sure it actually did what we expected it to do (input=0x80808080)
1623    * hoping that the scaling didn't mess up our values */
1624 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1625   /* B - G - R - A */
1626   fail_unless_equals_int (data7[0], 0x40);
1627   fail_unless_equals_int (data7[1], 0x40);
1628   fail_unless_equals_int (data7[2], 0x40);
1629   fail_unless_equals_int (data7[3], 0x80);
1630 #else
1631   /* A - R - G - B */
1632   fail_unless_equals_int (data7[0], 0x80);
1633   fail_unless_equals_int (data7[1], 0x40);
1634   fail_unless_equals_int (data7[2], 0x40);
1635   fail_unless_equals_int (data7[3], 0x40);
1636 #endif
1637   gst_buffer_unmap (pix7, &map);
1638
1639   /* and the same again, it should be cached now */
1640   pix8 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1641       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1642   fail_unless (pix8 == pix7);
1643
1644   /* make sure other cached stuff is still there */
1645   pix9 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1646       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1647   fail_unless (pix9 == pix3);
1648   pix10 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1649       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1650   fail_unless (pix10 == pix5);
1651
1652   gst_video_overlay_rectangle_unref (rect1);
1653 }
1654
1655 GST_END_TEST;
1656
1657 GST_START_TEST (test_overlay_composition_global_alpha)
1658 {
1659   GstVideoOverlayRectangle *rect1;
1660   GstBuffer *pix1, *pix2, *pix3, *pix4, *pix5;
1661   GstVideoMeta *vmeta;
1662   guint8 *data2, *data4, *data5;
1663   guint w, h, w4, h4;
1664   guint seq1, seq2;
1665   gfloat ga1, ga2;
1666   GstVideoOverlayFormatFlags flags1;
1667   GstMapInfo map;
1668
1669   pix1 = gst_buffer_new_and_alloc (200 * sizeof (guint32) * 50);
1670   gst_buffer_memset (pix1, 0, 0x80, gst_buffer_get_size (pix1));
1671
1672   gst_buffer_add_video_meta (pix1, GST_VIDEO_FRAME_FLAG_NONE,
1673       GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, 200, 50);
1674   rect1 = gst_video_overlay_rectangle_new_raw (pix1,
1675       600, 50, 300, 50, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1676   gst_buffer_unref (pix1);
1677
1678   /* same flags, unscaled, should be the same buffer */
1679   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1680       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1681   fail_unless (pix1 == pix2);
1682
1683   vmeta = gst_buffer_get_video_meta (pix2);
1684   fail_unless (vmeta != NULL);
1685   w = vmeta->width;
1686   h = vmeta->height;
1687
1688   /* same flags, but scaled */
1689   pix3 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1690       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1691   fail_if (pix3 == pix1 || pix3 == pix2);
1692
1693   /* get unscaled premultiplied data, new cached rectangle should be created */
1694   pix4 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1695       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1696   fail_if (pix4 == pix2 || pix4 == pix3);
1697   vmeta = gst_buffer_get_video_meta (pix4);
1698   fail_unless (vmeta != NULL);
1699   w4 = vmeta->width;
1700   h4 = vmeta->height;
1701   fail_unless_equals_int (w, w4);
1702   fail_unless_equals_int (h, h4);
1703   fail_unless_equals_int (gst_buffer_get_size (pix2),
1704       gst_buffer_get_size (pix4));
1705   gst_buffer_map (pix4, &map, GST_MAP_READ);
1706   fail_if (gst_buffer_memcmp (pix1, 0, map.data, map.size) == 0);
1707   /* make sure it actually did what we expected it to do (input=0x80808080) */
1708   data4 = map.data;
1709 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1710   /* B - G - R - A */
1711   fail_unless_equals_int (data4[0], 0x40);
1712   fail_unless_equals_int (data4[1], 0x40);
1713   fail_unless_equals_int (data4[2], 0x40);
1714   fail_unless_equals_int (data4[3], 0x80);
1715 #else
1716   /* A - R - G - B */
1717   fail_unless_equals_int (data4[0], 0x80);
1718   fail_unless_equals_int (data4[1], 0x40);
1719   fail_unless_equals_int (data4[2], 0x40);
1720   fail_unless_equals_int (data4[3], 0x40);
1721 #endif
1722   gst_buffer_unmap (pix4, &map);
1723
1724   /* now premultiplied and scaled, again a new cached rectangle should be cached */
1725   pix5 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1726       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1727   fail_if (pix5 == pix2 || pix5 == pix3 || pix5 == pix4);
1728   /* stride and size should be equal to the first scaled rect */
1729   fail_unless_equals_int (gst_buffer_get_size (pix5),
1730       gst_buffer_get_size (pix3));
1731   /* data should be different (premutliplied) though */
1732   gst_buffer_map (pix5, &map, GST_MAP_READ);
1733   fail_if (gst_buffer_memcmp (pix3, 0, map.data, map.size) == 0);
1734   /* make sure it actually did what we expected it to do (input=0x80808080) */
1735   data5 = map.data;
1736 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1737   /* B - G - R - A */
1738   fail_unless_equals_int (data5[0], 0x40);
1739   fail_unless_equals_int (data5[1], 0x40);
1740   fail_unless_equals_int (data5[2], 0x40);
1741   fail_unless_equals_int (data5[3], 0x80);
1742 #else
1743   /* A - R - G - B */
1744   fail_unless_equals_int (data5[0], 0x80);
1745   fail_unless_equals_int (data5[1], 0x40);
1746   fail_unless_equals_int (data5[2], 0x40);
1747   fail_unless_equals_int (data5[3], 0x40);
1748 #endif
1749   gst_buffer_unmap (pix5, &map);
1750
1751   /* global_alpha should initially be 1.0 */
1752   ga1 = gst_video_overlay_rectangle_get_global_alpha (rect1);
1753   fail_unless_equals_float (ga1, 1.0);
1754
1755   /* now set global_alpha */
1756   seq1 = gst_video_overlay_rectangle_get_seqnum (rect1);
1757   gst_video_overlay_rectangle_set_global_alpha (rect1, 0.5);
1758   ga2 = gst_video_overlay_rectangle_get_global_alpha (rect1);
1759   fail_unless_equals_float (ga2, 0.5);
1760
1761   /* seqnum should have changed */
1762   seq2 = gst_video_overlay_rectangle_get_seqnum (rect1);
1763   fail_unless (seq1 < seq2);
1764
1765   /* internal flags should have been set */
1766   flags1 = gst_video_overlay_rectangle_get_flags (rect1);
1767   fail_unless_equals_int (flags1, GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1768
1769   /* request unscaled pixel-data, global-alpha not applied */
1770   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1771       GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1772   /* this should just return the same buffer */
1773   fail_unless (pix2 == pix1);
1774   /* make sure we got the initial data (input=0x80808080) */
1775   gst_buffer_map (pix2, &map, GST_MAP_READ);
1776   data2 = map.data;
1777 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1778   /* B - G - R - A */
1779   fail_unless_equals_int (data2[0], 0x80);
1780   fail_unless_equals_int (data2[1], 0x80);
1781   fail_unless_equals_int (data2[2], 0x80);
1782   fail_unless_equals_int (data2[3], 0x80);
1783 #else
1784   /* A - R - G - B */
1785   fail_unless_equals_int (data2[0], 0x80);
1786   fail_unless_equals_int (data2[1], 0x80);
1787   fail_unless_equals_int (data2[2], 0x80);
1788   fail_unless_equals_int (data2[3], 0x80);
1789 #endif
1790   gst_buffer_unmap (pix2, &map);
1791
1792   /* unscaled pixel-data, global-alpha applied */
1793   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1794       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1795   /* this should be the same buffer with on-the-fly modified alpha-channel */
1796   fail_unless (pix2 == pix1);
1797   gst_buffer_map (pix2, &map, GST_MAP_READ);
1798   data2 = map.data;
1799   /* make sure we got the initial data with adjusted alpha-channel */
1800 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1801   /* B - G - R - A */
1802   fail_unless_equals_int (data2[0], 0x80);
1803   fail_unless_equals_int (data2[1], 0x80);
1804   fail_unless_equals_int (data2[2], 0x80);
1805   fail_unless_equals_int (data2[3], 0x40);
1806 #else
1807   /* A - R - G - B */
1808   fail_unless_equals_int (data2[0], 0x40);
1809   fail_unless_equals_int (data2[1], 0x80);
1810   fail_unless_equals_int (data2[2], 0x80);
1811   fail_unless_equals_int (data2[3], 0x80);
1812 #endif
1813   gst_buffer_unmap (pix2, &map);
1814
1815   /* adjust global_alpha once more */
1816   gst_video_overlay_rectangle_set_global_alpha (rect1, 0.25);
1817   ga2 = gst_video_overlay_rectangle_get_global_alpha (rect1);
1818   fail_unless_equals_float (ga2, 0.25);
1819   /* and again request unscaled pixel-data, global-alpha applied */
1820   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1821       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1822   fail_unless (pix2 == pix1);
1823   /* make sure we got the initial data with adjusted alpha-channel */
1824   gst_buffer_map (pix2, &map, GST_MAP_READ);
1825   data2 = map.data;
1826 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1827   /* B - G - R - A */
1828   fail_unless_equals_int (data2[0], 0x80);
1829   fail_unless_equals_int (data2[1], 0x80);
1830   fail_unless_equals_int (data2[2], 0x80);
1831   fail_unless_equals_int (data2[3], 0x20);
1832 #else
1833   /* A - R - G - B */
1834   fail_unless_equals_int (data2[0], 0x20);
1835   fail_unless_equals_int (data2[1], 0x80);
1836   fail_unless_equals_int (data2[2], 0x80);
1837   fail_unless_equals_int (data2[3], 0x80);
1838 #endif
1839   gst_buffer_unmap (pix2, &map);
1840
1841   /* again: unscaled pixel-data, global-alpha not applied,
1842    * this should revert alpha-channel to initial values */
1843   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1844       GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1845   fail_unless (pix2 == pix1);
1846   /* make sure we got the initial data (input=0x80808080) */
1847   gst_buffer_map (pix2, &map, GST_MAP_READ);
1848   data2 = map.data;
1849 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1850   /* B - G - R - A */
1851   fail_unless_equals_int (data2[0], 0x80);
1852   fail_unless_equals_int (data2[1], 0x80);
1853   fail_unless_equals_int (data2[2], 0x80);
1854   fail_unless_equals_int (data2[3], 0x80);
1855 #else
1856   /* A - R - G - B */
1857   fail_unless_equals_int (data2[0], 0x80);
1858   fail_unless_equals_int (data2[1], 0x80);
1859   fail_unless_equals_int (data2[2], 0x80);
1860   fail_unless_equals_int (data2[3], 0x80);
1861 #endif
1862   gst_buffer_unmap (pix2, &map);
1863
1864   /* now scaled, global-alpha not applied */
1865   pix2 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1866       GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1867   /* this should just return the rect/buffer, that was cached for these
1868    * scaling dimensions */
1869   fail_unless (pix2 == pix3);
1870   /* make sure we got the initial data (input=0x80808080) */
1871   gst_buffer_map (pix2, &map, GST_MAP_READ);
1872   data2 = map.data;
1873 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1874   /* B - G - R - A */
1875   fail_unless_equals_int (data2[0], 0x80);
1876   fail_unless_equals_int (data2[1], 0x80);
1877   fail_unless_equals_int (data2[2], 0x80);
1878   fail_unless_equals_int (data2[3], 0x80);
1879 #else
1880   /* A - R - G - B */
1881   fail_unless_equals_int (data2[0], 0x80);
1882   fail_unless_equals_int (data2[1], 0x80);
1883   fail_unless_equals_int (data2[2], 0x80);
1884   fail_unless_equals_int (data2[3], 0x80);
1885 #endif
1886   gst_buffer_unmap (pix2, &map);
1887
1888   /* scaled, global-alpha (0.25) applied */
1889   pix2 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1890       GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
1891   /* this should just return the rect/buffer, that was cached for these
1892    * scaling dimensions with modified alpha channel */
1893   fail_unless (pix2 == pix3);
1894   /* make sure we got the data we expect for global-alpha=0.25 */
1895   gst_buffer_map (pix2, &map, GST_MAP_READ);
1896   data2 = map.data;
1897 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1898   /* B - G - R - A */
1899   fail_unless_equals_int (data2[0], 0x80);
1900   fail_unless_equals_int (data2[1], 0x80);
1901   fail_unless_equals_int (data2[2], 0x80);
1902   fail_unless_equals_int (data2[3], 0x20);
1903 #else
1904   /* A - R - G - B */
1905   fail_unless_equals_int (data2[0], 0x20);
1906   fail_unless_equals_int (data2[1], 0x80);
1907   fail_unless_equals_int (data2[2], 0x80);
1908   fail_unless_equals_int (data2[3], 0x80);
1909 #endif
1910   gst_buffer_unmap (pix2, &map);
1911
1912   /* now unscaled premultiplied data, global-alpha not applied,
1913    * is this really a valid use case?*/
1914   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1915       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA |
1916       GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
1917   /* this should just return the rect/buffer, that was cached for the
1918    * premultiplied data */
1919   fail_unless (pix2 == pix4);
1920   /* make sure we got what we expected */
1921   gst_buffer_map (pix2, &map, GST_MAP_READ);
1922   data2 = map.data;
1923 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1924   /* B - G - R - A */
1925   fail_unless_equals_int (data2[0], 0x40);
1926   fail_unless_equals_int (data2[1], 0x40);
1927   fail_unless_equals_int (data2[2], 0x40);
1928   fail_unless_equals_int (data2[3], 0x80);
1929 #else
1930   /* A - R - G - B */
1931   fail_unless_equals_int (data2[0], 0x80);
1932   fail_unless_equals_int (data2[1], 0x40);
1933   fail_unless_equals_int (data2[2], 0x40);
1934   fail_unless_equals_int (data2[3], 0x40);
1935 #endif
1936   gst_buffer_unmap (pix2, &map);
1937
1938   /* unscaled premultiplied data, global-alpha (0.25) applied */
1939   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1940       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1941   /* this should just return the rect/buffer, that was cached for the
1942    * premultiplied data */
1943   fail_unless (pix2 == pix4);
1944   /* make sure we got what we expected:
1945    * (0x40 / (0x80/0xFF) * (0x20/0xFF) = 0x10
1946    * NOTE: unless we are using round() for the premultiplied case
1947    * in gst_video_overlay_rectangle_apply_global_alpha() we get rounding
1948    * error, i.e. 0x0F here */
1949   gst_buffer_map (pix2, &map, GST_MAP_READ);
1950   data2 = map.data;
1951 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1952   /* B - G - R - A */
1953   fail_unless_equals_int (data2[0], 0x0F);
1954   fail_unless_equals_int (data2[1], 0x0F);
1955   fail_unless_equals_int (data2[2], 0x0F);
1956   fail_unless_equals_int (data2[3], 0x20);
1957 #else
1958   /* A - R - G - B */
1959   fail_unless_equals_int (data2[0], 0x20);
1960   fail_unless_equals_int (data2[1], 0x0F);
1961   fail_unless_equals_int (data2[2], 0x0F);
1962   fail_unless_equals_int (data2[3], 0x0F);
1963 #endif
1964   gst_buffer_unmap (pix2, &map);
1965
1966   /* set global_alpha once more */
1967   gst_video_overlay_rectangle_set_global_alpha (rect1, 0.75);
1968   /* and verify that also premultiplied data is adjusted
1969    * correspondingly (though with increasing rounding errors) */
1970   pix2 = gst_video_overlay_rectangle_get_pixels_unscaled_raw (rect1,
1971       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
1972   /* this should just return the rect/buffer, that was cached for the
1973    * premultiplied data */
1974   fail_unless (pix2 == pix4);
1975   /* make sure we got what we expected:
1976    * (0x0F / (0x20/0xFF) * (0x60/0xFF) = 0x2D
1977    * NOTE: using floats everywhere we would get 0x30
1978    * here we will actually end up with 0x2C */
1979   gst_buffer_map (pix2, &map, GST_MAP_READ);
1980   data2 = map.data;
1981 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1982   /* B - G - R - A */
1983   fail_unless_equals_int (data2[0], 0x2C);
1984   fail_unless_equals_int (data2[1], 0x2C);
1985   fail_unless_equals_int (data2[2], 0x2C);
1986   fail_unless_equals_int (data2[3], 0x60);
1987 #else
1988   /* A - R - G - B */
1989   fail_unless_equals_int (data2[0], 0x60);
1990   fail_unless_equals_int (data2[1], 0x2C);
1991   fail_unless_equals_int (data2[2], 0x2C);
1992   fail_unless_equals_int (data2[3], 0x2C);
1993 #endif
1994   gst_buffer_unmap (pix2, &map);
1995
1996   /* now scaled and premultiplied data, global-alpha not applied,
1997    * is this really a valid use case?*/
1998   pix2 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
1999       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA |
2000       GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA);
2001   /* this should just return the rect/buffer, that was cached for the
2002    * first premultiplied+scaled rect*/
2003   fail_unless (pix2 == pix5);
2004   /* make sure we got what we expected */
2005   gst_buffer_map (pix2, &map, GST_MAP_READ);
2006   data2 = map.data;
2007 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
2008   /* B - G - R - A */
2009   fail_unless_equals_int (data2[0], 0x40);
2010   fail_unless_equals_int (data2[1], 0x40);
2011   fail_unless_equals_int (data2[2], 0x40);
2012   fail_unless_equals_int (data2[3], 0x80);
2013 #else
2014   /* A - R - G - B */
2015   fail_unless_equals_int (data2[0], 0x80);
2016   fail_unless_equals_int (data2[1], 0x40);
2017   fail_unless_equals_int (data2[2], 0x40);
2018   fail_unless_equals_int (data2[3], 0x40);
2019 #endif
2020   gst_buffer_unmap (pix2, &map);
2021
2022   /* scaled and premultiplied data, global-alpha applied */
2023   pix2 = gst_video_overlay_rectangle_get_pixels_raw (rect1,
2024       GST_VIDEO_OVERLAY_FORMAT_FLAG_PREMULTIPLIED_ALPHA);
2025   /* this should just return the rect/buffer, that was cached for the
2026    * first premultiplied+scaled rect*/
2027   fail_unless (pix2 == pix5);
2028   /* make sure we got what we expected; see above note about rounding errors! */
2029   gst_buffer_map (pix2, &map, GST_MAP_READ);
2030   data2 = map.data;
2031 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
2032   /* B - G - R - A */
2033   fail_unless_equals_int (data2[0], 0x2F);
2034   fail_unless_equals_int (data2[1], 0x2F);
2035   fail_unless_equals_int (data2[2], 0x2F);
2036   fail_unless_equals_int (data2[3], 0x60);
2037 #else
2038   /* A - R - G - B */
2039   fail_unless_equals_int (data2[0], 0x60);
2040   fail_unless_equals_int (data2[1], 0x2F);
2041   fail_unless_equals_int (data2[2], 0x2F);
2042   fail_unless_equals_int (data2[3], 0x2F);
2043 #endif
2044   gst_buffer_unmap (pix2, &map);
2045
2046   gst_video_overlay_rectangle_unref (rect1);
2047 }
2048
2049 GST_END_TEST;
2050
2051 static guint8 *
2052 make_pixels (gint depth, gint width, gint height)
2053 {
2054   guint32 color = 0xff000000;
2055   gint i, j;
2056
2057   if (depth == 8) {
2058     guint8 *pixels = g_malloc (width * height * 4);
2059     for (i = 0; i < height; i++) {
2060       for (j = 0; j < width; j++) {
2061         pixels[(i * width + j) * 4 + 0] = ((color >> 24) & 0xff);
2062         pixels[(i * width + j) * 4 + 1] = ((color >> 16) & 0xff);
2063         pixels[(i * width + j) * 4 + 2] = ((color >> 8) & 0xff);
2064         pixels[(i * width + j) * 4 + 3] = (color & 0xff);
2065         color++;
2066       }
2067     }
2068     return pixels;
2069   } else {
2070 #define TO16(a) (((a)<<8)|(a))
2071     guint16 *pixels = g_malloc (width * height * 8);
2072     for (i = 0; i < height; i++) {
2073       for (j = 0; j < width; j++) {
2074         pixels[(i * width + j) * 4 + 0] = TO16 ((color >> 24) & 0xff);
2075         pixels[(i * width + j) * 4 + 1] = TO16 ((color >> 16) & 0xff);
2076         pixels[(i * width + j) * 4 + 2] = TO16 ((color >> 8) & 0xff);
2077         pixels[(i * width + j) * 4 + 3] = TO16 (color & 0xff);
2078         color++;
2079       }
2080     }
2081 #undef TO16
2082     return (guint8 *) pixels;
2083   }
2084 }
2085
2086 #define HS(x,o) ((x)&hs[o])
2087 #define WS(x,o) ((x)&ws[o])
2088 #define IN(i,j,o) (in[(HS(i, o)*width + WS(j,o))*4+(o)] & mask[o])
2089 #define OUT(i,j,o) (out[((i)*width + (j))*4+o] & mask[o])
2090 static gint
2091 compare_frame (const GstVideoFormatInfo * finfo, gint depth, guint8 * outpixels,
2092     guint8 * pixels, gint width, gint height)
2093 {
2094   gint diff, i, j, k;
2095   guint ws[4], hs[4], mask[4];
2096
2097   for (k = 0; k < 4; k++) {
2098     hs[k] = G_MAXUINT << finfo->h_sub[(3 + k) % 4];
2099     ws[k] = G_MAXUINT << finfo->w_sub[(3 + k) % 4];
2100     mask[k] = G_MAXUINT << (depth - finfo->depth[(3 + k) % 4]);
2101   }
2102   diff = 0;
2103   if (depth == 8) {
2104     guint8 *in = pixels;
2105     guint8 *out = outpixels;
2106
2107     for (i = 0; i < height; i++) {
2108       for (j = 0; j < width; j++) {
2109         for (k = 0; k < 4; k++) {
2110           diff += IN (i, j, k) != OUT (i, j, k);
2111         }
2112       }
2113     }
2114   } else {
2115     guint16 *in = (guint16 *) pixels;
2116     guint16 *out = (guint16 *) outpixels;
2117
2118     for (i = 0; i < height; i++) {
2119       for (j = 0; j < width; j++) {
2120         for (k = 0; k < 4; k++) {
2121           diff += IN (i, j, k) != OUT (i, j, k);
2122         }
2123       }
2124     }
2125   }
2126   return diff;
2127 }
2128
2129 #undef WS
2130 #undef HS
2131 #undef IN
2132 #undef OUT
2133
2134 typedef struct
2135 {
2136   GstVideoFormat infmt;
2137   GstVideoFormat outfmt;
2138   gint method;
2139   gdouble convert_sec;
2140 } ConvertResult;
2141
2142 #define SIGN(a,b) ((a) < (b) ? -1 : (a) > (b) ? 1 : 0)
2143
2144 static gint
2145 compare_result (gconstpointer a, gconstpointer b)
2146 {
2147   const ConvertResult *ap = a;
2148   const ConvertResult *bp = b;
2149
2150   return SIGN (ap->convert_sec, bp->convert_sec);
2151 }
2152
2153 #define UNPACK_FRAME(frame,dest,line,x,width)            \
2154   (frame)->info.finfo->unpack_func ((frame)->info.finfo, \
2155       (GST_VIDEO_FRAME_IS_INTERLACED (frame) ?           \
2156         GST_VIDEO_PACK_FLAG_INTERLACED :                 \
2157         GST_VIDEO_PACK_FLAG_NONE),                       \
2158       dest, (frame)->data, (frame)->info.stride, x,      \
2159       line, width)
2160 #define PACK_FRAME(frame,src,line,width)               \
2161   (frame)->info.finfo->pack_func ((frame)->info.finfo, \
2162       (GST_VIDEO_FRAME_IS_INTERLACED (frame) ?         \
2163         GST_VIDEO_PACK_FLAG_INTERLACED :               \
2164         GST_VIDEO_PACK_FLAG_NONE),                     \
2165       src, 0, (frame)->data, (frame)->info.stride,     \
2166       (frame)->info.chroma_site, line, width);
2167
2168 GST_START_TEST (test_video_pack_unpack2)
2169 {
2170   GstVideoFormat format;
2171   GTimer *timer;
2172   gint num_formats, i;
2173   GArray *packarray, *unpackarray;
2174
2175 #define WIDTH 320
2176 #define HEIGHT 240
2177 /* set to something larger to do benchmarks */
2178 #define TIME 0.01
2179
2180   timer = g_timer_new ();
2181   packarray = g_array_new (FALSE, FALSE, sizeof (ConvertResult));
2182   unpackarray = g_array_new (FALSE, FALSE, sizeof (ConvertResult));
2183
2184   num_formats = get_num_formats ();
2185
2186   GST_DEBUG ("pack/sec\t unpack/sec \tpack GB/sec\tunpack GB/sec\tformat");
2187
2188   for (format = GST_VIDEO_FORMAT_I420; format < num_formats; format++) {
2189     GstVideoInfo info;
2190     const GstVideoFormatInfo *finfo, *fuinfo;
2191     GstBuffer *buffer;
2192     GstVideoFrame frame;
2193     gint k, stride, count, diff, depth;
2194     guint8 *pixels, *outpixels;
2195     gdouble elapsed;
2196     gdouble unpack_sec, pack_sec;
2197     ConvertResult res;
2198
2199     finfo = gst_video_format_get_info (format);
2200     fail_unless (finfo != NULL);
2201
2202     if (GST_VIDEO_FORMAT_INFO_HAS_PALETTE (finfo))
2203       continue;
2204
2205     fuinfo = gst_video_format_get_info (finfo->unpack_format);
2206     fail_unless (fuinfo != NULL);
2207
2208     depth = GST_VIDEO_FORMAT_INFO_BITS (fuinfo);
2209     fail_unless (depth == 8 || depth == 16);
2210
2211     pixels = make_pixels (depth, WIDTH, HEIGHT);
2212     stride = WIDTH * (depth >> 1);
2213
2214     fail_unless (gst_video_info_set_format (&info, format, WIDTH, HEIGHT));
2215     buffer = gst_buffer_new_and_alloc (info.size);
2216     gst_video_frame_map (&frame, &info, buffer, GST_MAP_READWRITE);
2217
2218     /* pack the frame into the target format */
2219     /* warmup */
2220     PACK_FRAME (&frame, pixels, 0, WIDTH);
2221
2222     count = 0;
2223     g_timer_start (timer);
2224     while (TRUE) {
2225       for (k = 0; k < HEIGHT; k += finfo->pack_lines) {
2226         PACK_FRAME (&frame, pixels + k * stride, k, WIDTH);
2227       }
2228       count++;
2229       elapsed = g_timer_elapsed (timer, NULL);
2230       if (elapsed >= TIME)
2231         break;
2232     }
2233     unpack_sec = count / elapsed;
2234
2235     res.infmt = format;
2236     res.outfmt = finfo->unpack_format;
2237     res.convert_sec = unpack_sec;
2238     g_array_append_val (unpackarray, res);
2239
2240     outpixels = g_malloc0 (HEIGHT * stride);
2241
2242     /* unpack the frame */
2243     /* warmup */
2244     UNPACK_FRAME (&frame, outpixels, 0, 0, WIDTH);
2245
2246     count = 0;
2247     g_timer_start (timer);
2248     while (TRUE) {
2249       for (k = 0; k < HEIGHT; k += finfo->pack_lines) {
2250         UNPACK_FRAME (&frame, outpixels + k * stride, k, 0, WIDTH);
2251       }
2252       count++;
2253       elapsed = g_timer_elapsed (timer, NULL);
2254       if (elapsed >= TIME)
2255         break;
2256     }
2257     pack_sec = count / elapsed;
2258
2259     res.outfmt = format;
2260     res.infmt = finfo->unpack_format;
2261     res.convert_sec = pack_sec;
2262     g_array_append_val (packarray, res);
2263
2264     /* compare the frame */
2265     diff = compare_frame (finfo, depth, outpixels, pixels, WIDTH, HEIGHT);
2266
2267     GST_DEBUG ("%f \t %f \t %f \t %f \t %s %d/%f", pack_sec, unpack_sec,
2268         info.size * pack_sec, info.size * unpack_sec, finfo->name, count,
2269         elapsed);
2270
2271     if (diff != 0) {
2272       gst_util_dump_mem (outpixels, 128);
2273       gst_util_dump_mem (pixels, 128);
2274       fail_if (diff != 0);
2275     }
2276     gst_video_frame_unmap (&frame);
2277     gst_buffer_unref (buffer);
2278     g_free (pixels);
2279     g_free (outpixels);
2280   }
2281
2282   g_array_sort (packarray, compare_result);
2283   for (i = 0; i < packarray->len; i++) {
2284     ConvertResult *res = &g_array_index (packarray, ConvertResult, i);
2285
2286     GST_DEBUG ("%f pack/sec %s->%s", res->convert_sec,
2287         gst_video_format_to_string (res->infmt),
2288         gst_video_format_to_string (res->outfmt));
2289   }
2290
2291   g_array_sort (unpackarray, compare_result);
2292   for (i = 0; i < unpackarray->len; i++) {
2293     ConvertResult *res = &g_array_index (unpackarray, ConvertResult, i);
2294
2295     GST_DEBUG ("%f unpack/sec %s->%s", res->convert_sec,
2296         gst_video_format_to_string (res->infmt),
2297         gst_video_format_to_string (res->outfmt));
2298   }
2299
2300   g_timer_destroy (timer);
2301   g_array_free (packarray, TRUE);
2302   g_array_free (unpackarray, TRUE);
2303 }
2304
2305 GST_END_TEST;
2306 #undef WIDTH
2307 #undef HEIGHT
2308 #undef TIME
2309
2310 #define WIDTH 320
2311 #define HEIGHT 240
2312 #define TIME 0.1
2313 #define GET_LINE(l) (pixels + CLAMP (l, 0, HEIGHT-1) * WIDTH * 4)
2314 GST_START_TEST (test_video_chroma)
2315 {
2316   guint8 *pixels;
2317   guint n_lines;
2318   gint i, j, k, offset, count;
2319   gpointer lines[10];
2320   GTimer *timer;
2321   gdouble elapsed, subsample_sec;
2322   GstVideoChromaSite sites[] = {
2323     GST_VIDEO_CHROMA_SITE_NONE,
2324     GST_VIDEO_CHROMA_SITE_H_COSITED,
2325   };
2326
2327   timer = g_timer_new ();
2328   pixels = make_pixels (8, WIDTH, HEIGHT);
2329
2330   for (k = 0; k < G_N_ELEMENTS (sites); k++) {
2331     GstVideoChromaResample *resample;
2332
2333     resample = gst_video_chroma_resample_new (GST_VIDEO_CHROMA_METHOD_LINEAR,
2334         sites[k], GST_VIDEO_CHROMA_FLAG_NONE, GST_VIDEO_FORMAT_AYUV, -1, -1);
2335
2336     gst_video_chroma_resample_get_info (resample, &n_lines, &offset);
2337     fail_unless (n_lines < 10);
2338
2339     /* warmup */
2340     for (j = 0; j < n_lines; j++)
2341       lines[j] = GET_LINE (offset + j);
2342     gst_video_chroma_resample (resample, lines, WIDTH);
2343
2344     count = 0;
2345     g_timer_start (timer);
2346     while (TRUE) {
2347       for (i = 0; i < HEIGHT; i += n_lines) {
2348         for (j = 0; j < n_lines; j++)
2349           lines[j] = GET_LINE (i + offset + j);
2350
2351         gst_video_chroma_resample (resample, lines, WIDTH);
2352       }
2353       count++;
2354       elapsed = g_timer_elapsed (timer, NULL);
2355       if (elapsed >= TIME)
2356         break;
2357     }
2358     subsample_sec = count / elapsed;
2359     GST_DEBUG ("%f downsamples/sec  %d/%f", subsample_sec, count, elapsed);
2360     gst_video_chroma_resample_free (resample);
2361
2362     resample = gst_video_chroma_resample_new (GST_VIDEO_CHROMA_METHOD_LINEAR,
2363         sites[k], GST_VIDEO_CHROMA_FLAG_NONE, GST_VIDEO_FORMAT_AYUV, 1, 1);
2364
2365     gst_video_chroma_resample_get_info (resample, &n_lines, &offset);
2366     fail_unless (n_lines < 10);
2367
2368     /* warmup */
2369     for (j = 0; j < n_lines; j++)
2370       lines[j] = GET_LINE (offset + j);
2371     gst_video_chroma_resample (resample, lines, WIDTH);
2372
2373     count = 0;
2374     g_timer_start (timer);
2375     while (TRUE) {
2376       for (i = 0; i < HEIGHT; i += n_lines) {
2377         for (j = 0; j < n_lines; j++)
2378           lines[j] = GET_LINE (i + offset + j);
2379
2380         gst_video_chroma_resample (resample, lines, WIDTH);
2381       }
2382       count++;
2383       elapsed = g_timer_elapsed (timer, NULL);
2384       if (elapsed >= TIME)
2385         break;
2386     }
2387     subsample_sec = count / elapsed;
2388     GST_DEBUG ("%f upsamples/sec  %d/%f", subsample_sec, count, elapsed);
2389     gst_video_chroma_resample_free (resample);
2390   }
2391
2392   g_free (pixels);
2393   g_timer_destroy (timer);
2394 }
2395
2396 GST_END_TEST;
2397 #undef WIDTH
2398 #undef HEIGHT
2399 #undef TIME
2400
2401 typedef struct
2402 {
2403   const gchar *name;
2404   GstVideoChromaSite site;
2405 } ChromaSiteElem;
2406
2407 GST_START_TEST (test_video_chroma_site)
2408 {
2409   ChromaSiteElem valid_sites[] = {
2410     /* pre-defined flags */
2411     {"jpeg", GST_VIDEO_CHROMA_SITE_JPEG},
2412     {"mpeg2", GST_VIDEO_CHROMA_SITE_MPEG2},
2413     {"dv", GST_VIDEO_CHROMA_SITE_DV},
2414     {"alt-line", GST_VIDEO_CHROMA_SITE_ALT_LINE},
2415     {"cosited", GST_VIDEO_CHROMA_SITE_COSITED},
2416     /* new values */
2417     {"v-cosited", GST_VIDEO_CHROMA_SITE_V_COSITED},
2418     {"v-cosited+alt-line",
2419         GST_VIDEO_CHROMA_SITE_V_COSITED | GST_VIDEO_CHROMA_SITE_ALT_LINE},
2420   };
2421   ChromaSiteElem unknown_sites[] = {
2422     {NULL, GST_VIDEO_CHROMA_SITE_UNKNOWN},
2423     /* Any combination with GST_VIDEO_CHROMA_SITE_NONE doesn' make sense */
2424     {NULL, GST_VIDEO_CHROMA_SITE_NONE | GST_VIDEO_CHROMA_SITE_H_COSITED},
2425   };
2426   gint i;
2427
2428   for (i = 0; i < G_N_ELEMENTS (valid_sites); i++) {
2429     gchar *site = gst_video_chroma_site_to_string (valid_sites[i].site);
2430
2431     fail_unless (site != NULL);
2432     fail_unless (g_strcmp0 (site, valid_sites[i].name) == 0);
2433     fail_unless (gst_video_chroma_site_from_string (site) ==
2434         valid_sites[i].site);
2435     g_free (site);
2436   }
2437
2438   for (i = 0; i < G_N_ELEMENTS (unknown_sites); i++) {
2439     gchar *site = gst_video_chroma_site_to_string (unknown_sites[i].site);
2440     fail_unless (site == NULL);
2441   }
2442
2443   /* totally wrong string */
2444   fail_unless (gst_video_chroma_site_from_string ("foo/bar") ==
2445       GST_VIDEO_CHROMA_SITE_UNKNOWN);
2446
2447   /* valid ones */
2448   fail_unless (gst_video_chroma_site_from_string ("jpeg") ==
2449       GST_VIDEO_CHROMA_SITE_NONE);
2450   fail_unless (gst_video_chroma_site_from_string ("none") ==
2451       GST_VIDEO_CHROMA_SITE_NONE);
2452
2453   fail_unless (gst_video_chroma_site_from_string ("mpeg2") ==
2454       GST_VIDEO_CHROMA_SITE_H_COSITED);
2455   fail_unless (gst_video_chroma_site_from_string ("h-cosited") ==
2456       GST_VIDEO_CHROMA_SITE_H_COSITED);
2457
2458   /* Equal to "cosited" */
2459   fail_unless (gst_video_chroma_site_from_string ("v-cosited+h-cosited") ==
2460       GST_VIDEO_CHROMA_SITE_COSITED);
2461
2462   fail_unless (gst_video_chroma_site_from_string ("v-cosited") ==
2463       GST_VIDEO_CHROMA_SITE_V_COSITED);
2464
2465   /* none + something doesn't make sense */
2466   fail_unless (gst_video_chroma_site_from_string ("none+v-cosited") ==
2467       GST_VIDEO_CHROMA_SITE_UNKNOWN);
2468
2469   /* mix of valid and invalid strings */
2470   fail_unless (gst_video_chroma_site_from_string ("mpeg2+foo/bar") ==
2471       GST_VIDEO_CHROMA_SITE_UNKNOWN);
2472 }
2473
2474 GST_END_TEST;
2475
2476 GST_START_TEST (test_video_scaler)
2477 {
2478   GstVideoScaler *scale;
2479
2480   scale = gst_video_scaler_new (GST_VIDEO_RESAMPLER_METHOD_LINEAR,
2481       GST_VIDEO_SCALER_FLAG_NONE, 2, 10, 5, NULL);
2482   gst_video_scaler_free (scale);
2483
2484   scale = gst_video_scaler_new (GST_VIDEO_RESAMPLER_METHOD_LINEAR,
2485       GST_VIDEO_SCALER_FLAG_NONE, 2, 15, 5, NULL);
2486   gst_video_scaler_free (scale);
2487 }
2488
2489 GST_END_TEST;
2490
2491 typedef enum
2492 {
2493   RGB,
2494   YUV,
2495   OTHER
2496 } ColorType;
2497
2498 #define WIDTH 320
2499 #define HEIGHT 240
2500
2501 static gboolean
2502 check_video_format_is_type (GstVideoFormat fmt, ColorType fmt_type)
2503 {
2504   const GstVideoFormatInfo *info = gst_video_format_get_info (fmt);
2505   gboolean is_rgb = GST_VIDEO_FORMAT_INFO_IS_RGB (info);
2506   gboolean is_yuv = GST_VIDEO_FORMAT_INFO_IS_YUV (info);
2507
2508   switch (fmt_type) {
2509     case RGB:
2510       return is_rgb;
2511     case YUV:
2512       return is_yuv;
2513     case OTHER:
2514       break;
2515   }
2516   return !is_rgb && !is_yuv;
2517 }
2518
2519 static void
2520 run_video_color_convert (ColorType in_type, ColorType out_type)
2521 {
2522   GstVideoFormat infmt, outfmt;
2523   gint num_formats;
2524
2525   num_formats = get_num_formats ();
2526
2527   for (infmt = GST_VIDEO_FORMAT_I420; infmt < num_formats; infmt++) {
2528     GstVideoInfo ininfo;
2529     GstVideoFrame inframe;
2530     GstBuffer *inbuffer;
2531
2532     if (!check_video_format_is_type (infmt, in_type))
2533       continue;
2534
2535     fail_unless (gst_video_info_set_format (&ininfo, infmt, WIDTH, HEIGHT));
2536     inbuffer = gst_buffer_new_and_alloc (ininfo.size);
2537     gst_buffer_memset (inbuffer, 0, 0, -1);
2538     gst_video_frame_map (&inframe, &ininfo, inbuffer, GST_MAP_READ);
2539
2540     for (outfmt = GST_VIDEO_FORMAT_I420; outfmt < num_formats; outfmt++) {
2541       GstVideoInfo outinfo;
2542       GstVideoFrame outframe;
2543       GstBuffer *outbuffer;
2544       GstVideoConverter *convert;
2545
2546       if (!check_video_format_is_type (outfmt, out_type))
2547         continue;
2548
2549       GST_LOG ("%s -> %s @ %ux%u", gst_video_format_to_string (infmt),
2550           gst_video_format_to_string (outfmt), WIDTH, HEIGHT);
2551
2552       fail_unless (gst_video_info_set_format (&outinfo, outfmt, WIDTH, HEIGHT));
2553       outbuffer = gst_buffer_new_and_alloc (outinfo.size);
2554       gst_video_frame_map (&outframe, &outinfo, outbuffer, GST_MAP_WRITE);
2555
2556       convert = gst_video_converter_new (&ininfo, &outinfo, NULL);
2557
2558       gst_video_converter_frame (convert, &inframe, &outframe);
2559
2560       gst_video_converter_free (convert);
2561
2562       gst_video_frame_unmap (&outframe);
2563       gst_buffer_unref (outbuffer);
2564     }
2565     gst_video_frame_unmap (&inframe);
2566     gst_buffer_unref (inbuffer);
2567   }
2568 }
2569
2570 GST_START_TEST (test_video_color_convert_rgb_rgb)
2571 {
2572   run_video_color_convert (RGB, RGB);
2573 }
2574
2575 GST_END_TEST;
2576
2577 GST_START_TEST (test_video_color_convert_rgb_yuv)
2578 {
2579   run_video_color_convert (RGB, YUV);
2580 }
2581
2582 GST_END_TEST;
2583
2584 GST_START_TEST (test_video_color_convert_yuv_yuv)
2585 {
2586   run_video_color_convert (YUV, YUV);
2587 }
2588
2589 GST_END_TEST;
2590
2591 GST_START_TEST (test_video_color_convert_yuv_rgb)
2592 {
2593   run_video_color_convert (YUV, RGB);
2594 }
2595
2596 GST_END_TEST;
2597
2598 GST_START_TEST (test_video_color_convert_other)
2599 {
2600   run_video_color_convert (OTHER, RGB);
2601   run_video_color_convert (RGB, OTHER);
2602   run_video_color_convert (OTHER, YUV);
2603   run_video_color_convert (YUV, OTHER);
2604   run_video_color_convert (OTHER, OTHER);
2605 }
2606
2607 GST_END_TEST;
2608 #undef WIDTH
2609 #undef HEIGHT
2610
2611 #define WIDTH_IN 320
2612 #define HEIGHT_IN 240
2613 #define WIDTH_OUT 400
2614 #define HEIGHT_OUT 300
2615 #define TIME 0.01
2616
2617 GST_START_TEST (test_video_size_convert)
2618 {
2619   GstVideoFormat infmt, outfmt;
2620   GTimer *timer;
2621   gint num_formats, i;
2622   GArray *array;
2623
2624   array = g_array_new (FALSE, FALSE, sizeof (ConvertResult));
2625
2626   timer = g_timer_new ();
2627
2628   num_formats = get_num_formats ();
2629
2630   for (infmt = GST_VIDEO_FORMAT_I420; infmt < num_formats; infmt++) {
2631     GstVideoInfo ininfo, outinfo;
2632     GstVideoFrame inframe, outframe;
2633     GstBuffer *inbuffer, *outbuffer;
2634     GstVideoConverter *convert;
2635     gdouble elapsed;
2636     gint count, method;
2637     ConvertResult res;
2638
2639     fail_unless (gst_video_info_set_format (&ininfo, infmt, WIDTH_IN,
2640             HEIGHT_IN));
2641     inbuffer = gst_buffer_new_and_alloc (ininfo.size);
2642     gst_buffer_memset (inbuffer, 0, 0, -1);
2643     gst_video_frame_map (&inframe, &ininfo, inbuffer, GST_MAP_READ);
2644
2645     outfmt = infmt;
2646     fail_unless (gst_video_info_set_format (&outinfo, outfmt, WIDTH_OUT,
2647             HEIGHT_OUT));
2648     outbuffer = gst_buffer_new_and_alloc (outinfo.size);
2649     gst_video_frame_map (&outframe, &outinfo, outbuffer, GST_MAP_WRITE);
2650
2651     for (method = 0; method < 4; method++) {
2652       convert = gst_video_converter_new (&ininfo, &outinfo,
2653           gst_structure_new ("options",
2654               GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
2655               GST_TYPE_VIDEO_RESAMPLER_METHOD, method, NULL));
2656
2657       /* warmup */
2658       gst_video_converter_frame (convert, &inframe, &outframe);
2659
2660       count = 0;
2661       g_timer_start (timer);
2662       while (TRUE) {
2663         gst_video_converter_frame (convert, &inframe, &outframe);
2664
2665         count++;
2666         elapsed = g_timer_elapsed (timer, NULL);
2667         if (elapsed >= TIME)
2668           break;
2669       }
2670
2671       res.infmt = infmt;
2672       res.outfmt = outfmt;
2673       res.method = method;
2674       res.convert_sec = count / elapsed;
2675
2676       GST_DEBUG ("%f resize/sec %s->%s, %d, %d/%f", res.convert_sec,
2677           gst_video_format_to_string (infmt),
2678           gst_video_format_to_string (outfmt), method, count, elapsed);
2679
2680       g_array_append_val (array, res);
2681
2682       gst_video_converter_free (convert);
2683     }
2684     gst_video_frame_unmap (&outframe);
2685     gst_buffer_unref (outbuffer);
2686     gst_video_frame_unmap (&inframe);
2687     gst_buffer_unref (inbuffer);
2688   }
2689
2690   g_array_sort (array, compare_result);
2691
2692   for (i = 0; i < array->len; i++) {
2693     ConvertResult *res = &g_array_index (array, ConvertResult, i);
2694
2695     GST_DEBUG ("%f method %d, resize/sec %s->%s", res->convert_sec, res->method,
2696         gst_video_format_to_string (res->infmt),
2697         gst_video_format_to_string (res->outfmt));
2698   }
2699
2700   g_array_free (array, TRUE);
2701
2702   g_timer_destroy (timer);
2703 }
2704
2705 GST_END_TEST;
2706 #undef WIDTH
2707 #undef HEIGHT
2708
2709 GST_START_TEST (test_video_convert)
2710 {
2711   GstVideoInfo ininfo, outinfo;
2712   GstVideoFrame inframe, outframe;
2713   GstBuffer *inbuffer, *outbuffer;
2714   GstVideoConverter *convert;
2715
2716   fail_unless (gst_video_info_set_format (&ininfo, GST_VIDEO_FORMAT_ARGB, 320,
2717           240));
2718   inbuffer = gst_buffer_new_and_alloc (ininfo.size);
2719   gst_buffer_memset (inbuffer, 0, 0, -1);
2720   gst_video_frame_map (&inframe, &ininfo, inbuffer, GST_MAP_READ);
2721
2722   fail_unless (gst_video_info_set_format (&outinfo, GST_VIDEO_FORMAT_BGRx, 400,
2723           300));
2724   outbuffer = gst_buffer_new_and_alloc (outinfo.size);
2725   gst_video_frame_map (&outframe, &outinfo, outbuffer, GST_MAP_WRITE);
2726
2727   /* see that we don't reuse the source line directly because we need
2728    * to add borders to it */
2729   convert = gst_video_converter_new (&ininfo, &outinfo,
2730       gst_structure_new ("options",
2731           GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
2732           GST_TYPE_VIDEO_RESAMPLER_METHOD, 3,
2733           GST_VIDEO_CONVERTER_OPT_SRC_X, G_TYPE_INT, 10,
2734           GST_VIDEO_CONVERTER_OPT_SRC_Y, G_TYPE_INT, 0,
2735           GST_VIDEO_CONVERTER_OPT_SRC_WIDTH, G_TYPE_INT, 300,
2736           GST_VIDEO_CONVERTER_OPT_SRC_HEIGHT, G_TYPE_INT, 220,
2737           GST_VIDEO_CONVERTER_OPT_DEST_X, G_TYPE_INT, 80,
2738           GST_VIDEO_CONVERTER_OPT_DEST_Y, G_TYPE_INT, 60,
2739           GST_VIDEO_CONVERTER_OPT_DEST_WIDTH, G_TYPE_INT, 300,
2740           GST_VIDEO_CONVERTER_OPT_DEST_HEIGHT, G_TYPE_INT, 220, NULL));
2741
2742   gst_video_converter_frame (convert, &inframe, &outframe);
2743   gst_video_converter_free (convert);
2744
2745   /* see that we reuse the source line directly because we need to scale
2746    * it first */
2747   convert = gst_video_converter_new (&ininfo, &outinfo,
2748       gst_structure_new ("options",
2749           GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
2750           GST_TYPE_VIDEO_RESAMPLER_METHOD, 3,
2751           GST_VIDEO_CONVERTER_OPT_SRC_X, G_TYPE_INT, 10,
2752           GST_VIDEO_CONVERTER_OPT_SRC_Y, G_TYPE_INT, 0,
2753           GST_VIDEO_CONVERTER_OPT_SRC_WIDTH, G_TYPE_INT, 300,
2754           GST_VIDEO_CONVERTER_OPT_SRC_HEIGHT, G_TYPE_INT, 220,
2755           GST_VIDEO_CONVERTER_OPT_DEST_X, G_TYPE_INT, 80,
2756           GST_VIDEO_CONVERTER_OPT_DEST_Y, G_TYPE_INT, 60,
2757           GST_VIDEO_CONVERTER_OPT_DEST_WIDTH, G_TYPE_INT, 310,
2758           GST_VIDEO_CONVERTER_OPT_DEST_HEIGHT, G_TYPE_INT, 230, NULL));
2759
2760   gst_video_converter_frame (convert, &inframe, &outframe);
2761
2762   /* Check that video convert doesn't crash if we give it frames with different info
2763    * than we configured it with by swapping width/height */
2764   gst_video_frame_unmap (&inframe);
2765   fail_unless (gst_video_info_set_format (&ininfo, GST_VIDEO_FORMAT_ARGB, 240,
2766           320));
2767   gst_video_frame_map (&inframe, &ininfo, inbuffer, GST_MAP_READ);
2768   ASSERT_CRITICAL (gst_video_converter_frame (convert, &inframe, &outframe));
2769   gst_video_converter_free (convert);
2770
2771   /* Make sure we can crop the entire frame away without dying */
2772   convert = gst_video_converter_new (&ininfo, &outinfo,
2773       gst_structure_new ("options",
2774           GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD,
2775           GST_TYPE_VIDEO_RESAMPLER_METHOD, 3,
2776           GST_VIDEO_CONVERTER_OPT_SRC_X, G_TYPE_INT, -500,
2777           GST_VIDEO_CONVERTER_OPT_SRC_Y, G_TYPE_INT, -500,
2778           GST_VIDEO_CONVERTER_OPT_SRC_WIDTH, G_TYPE_INT, 300,
2779           GST_VIDEO_CONVERTER_OPT_SRC_HEIGHT, G_TYPE_INT, 220,
2780           GST_VIDEO_CONVERTER_OPT_DEST_X, G_TYPE_INT, 800,
2781           GST_VIDEO_CONVERTER_OPT_DEST_Y, G_TYPE_INT, 600,
2782           GST_VIDEO_CONVERTER_OPT_DEST_WIDTH, G_TYPE_INT, 310,
2783           GST_VIDEO_CONVERTER_OPT_DEST_HEIGHT, G_TYPE_INT, 230, NULL));
2784
2785   gst_video_converter_frame (convert, &inframe, &outframe);
2786   gst_video_converter_free (convert);
2787
2788   gst_video_frame_unmap (&outframe);
2789   gst_buffer_unref (outbuffer);
2790   gst_video_frame_unmap (&inframe);
2791   gst_buffer_unref (inbuffer);
2792
2793 }
2794
2795 GST_END_TEST;
2796
2797 GST_START_TEST (test_video_convert_multithreading)
2798 {
2799   GstVideoInfo ininfo, outinfo;
2800   GstVideoFrame inframe, outframe, refframe;
2801   GstBuffer *inbuffer, *outbuffer, *refbuffer;
2802   GstVideoConverter *convert;
2803   GstMapInfo info;
2804   GstTaskPool *pool;
2805
2806   /* Large enough input resolution for video-converter to actually use
2807    * 4 threads if required */
2808   fail_unless (gst_video_info_set_format (&ininfo, GST_VIDEO_FORMAT_ARGB, 1280,
2809           720));
2810   inbuffer = gst_buffer_new_and_alloc (ininfo.size);
2811   gst_buffer_memset (inbuffer, 0, 0, -1);
2812   gst_video_frame_map (&inframe, &ininfo, inbuffer, GST_MAP_READ);
2813
2814   fail_unless (gst_video_info_set_format (&outinfo, GST_VIDEO_FORMAT_BGRx, 400,
2815           300));
2816   outbuffer = gst_buffer_new_and_alloc (outinfo.size);
2817   refbuffer = gst_buffer_new_and_alloc (outinfo.size);
2818
2819   gst_video_frame_map (&outframe, &outinfo, outbuffer, GST_MAP_WRITE);
2820   gst_video_frame_map (&refframe, &outinfo, refbuffer, GST_MAP_WRITE);
2821
2822   /* Single threaded-conversion */
2823   convert = gst_video_converter_new (&ininfo, &outinfo,
2824       gst_structure_new_empty ("options"));
2825   gst_video_converter_frame (convert, &inframe, &refframe);
2826   gst_video_converter_free (convert);
2827
2828   /* Multithreaded conversion, converter creates pool */
2829   convert = gst_video_converter_new (&ininfo, &outinfo,
2830       gst_structure_new ("options",
2831           GST_VIDEO_CONVERTER_OPT_THREADS, G_TYPE_UINT, 4, NULL)
2832       );
2833   gst_video_converter_frame (convert, &inframe, &outframe);
2834   gst_video_converter_free (convert);
2835
2836   gst_video_frame_unmap (&outframe);
2837   gst_video_frame_unmap (&refframe);
2838
2839   gst_buffer_map (outbuffer, &info, GST_MAP_READ);
2840   fail_unless (gst_buffer_memcmp (refbuffer, 0, info.data, info.size) == 0);
2841   gst_buffer_unmap (outbuffer, &info);
2842
2843   gst_video_frame_map (&outframe, &outinfo, outbuffer, GST_MAP_WRITE);
2844   gst_video_frame_map (&refframe, &outinfo, refbuffer, GST_MAP_WRITE);
2845
2846   /* Multi-threaded conversion, user-provided pool */
2847   pool = gst_shared_task_pool_new ();
2848   gst_shared_task_pool_set_max_threads (GST_SHARED_TASK_POOL (pool), 4);
2849   gst_task_pool_prepare (pool, NULL);
2850   convert = gst_video_converter_new_with_pool (&ininfo, &outinfo,
2851       gst_structure_new ("options",
2852           GST_VIDEO_CONVERTER_OPT_THREADS, G_TYPE_UINT, 4, NULL), pool);
2853   gst_video_converter_frame (convert, &inframe, &outframe);
2854   gst_video_converter_free (convert);
2855   gst_task_pool_cleanup (pool);
2856   gst_object_unref (pool);
2857
2858   gst_video_frame_unmap (&outframe);
2859   gst_video_frame_unmap (&refframe);
2860
2861   gst_buffer_map (outbuffer, &info, GST_MAP_READ);
2862   fail_unless (gst_buffer_memcmp (refbuffer, 0, info.data, info.size) == 0);
2863   gst_buffer_unmap (outbuffer, &info);
2864
2865
2866   gst_buffer_unref (refbuffer);
2867   gst_buffer_unref (outbuffer);
2868   gst_video_frame_unmap (&inframe);
2869   gst_buffer_unref (inbuffer);
2870
2871 }
2872
2873 GST_END_TEST;
2874
2875 GST_START_TEST (test_video_transfer)
2876 {
2877   gint i, j;
2878
2879   for (j = GST_VIDEO_TRANSFER_GAMMA10; j <= GST_VIDEO_TRANSFER_ARIB_STD_B67;
2880       j++) {
2881     for (i = 0; i < 256; i++) {
2882       gdouble val1, val2;
2883
2884       val1 = gst_video_transfer_function_encode (j, i / 255.0);
2885       fail_if (val1 < 0.0 || val1 > 1.0);
2886
2887       val2 = gst_video_transfer_function_decode (j, val1);
2888       fail_if (val2 < 0.0 || val2 > 1.0);
2889
2890       GST_DEBUG ("%d: %d %f->%f->%f %d", j, i, i / 255.0, val1, val2,
2891           (int) lrint (val2 * 255.0));
2892       if (val1 == 0.0)
2893         fail_if (val2 != 0.0);
2894       else
2895         fail_if (lrint (val2 * 255.0) != i);
2896     }
2897   }
2898 }
2899
2900 GST_END_TEST;
2901
2902 GST_START_TEST (test_video_center_rect)
2903 {
2904   GstVideoRectangle src, dest, result, expected;
2905
2906 #define NEW_RECT(x,y,w,h) ((GstVideoRectangle) {x,y,w,h})
2907 #define CHECK_RECT(res, exp)                    \
2908   fail_unless_equals_int(exp.x, res.x);\
2909   fail_unless_equals_int(exp.y, res.y);\
2910   fail_unless_equals_int(exp.w, res.w);\
2911   fail_unless_equals_int(exp.h, res.h);
2912
2913   /* 1:1 Aspect Ratio */
2914   src = NEW_RECT (0, 0, 100, 100);
2915   dest = NEW_RECT (0, 0, 100, 100);
2916   expected = NEW_RECT (0, 0, 100, 100);
2917   gst_video_sink_center_rect (src, dest, &result, TRUE);
2918   CHECK_RECT (result, expected);
2919
2920   src = NEW_RECT (0, 0, 100, 100);
2921   dest = NEW_RECT (0, 0, 50, 50);
2922   expected = NEW_RECT (0, 0, 50, 50);
2923   gst_video_sink_center_rect (src, dest, &result, TRUE);
2924   CHECK_RECT (result, expected);
2925
2926   src = NEW_RECT (0, 0, 100, 100);
2927   dest = NEW_RECT (50, 50, 100, 100);
2928   expected = NEW_RECT (50, 50, 100, 100);
2929   gst_video_sink_center_rect (src, dest, &result, TRUE);
2930   CHECK_RECT (result, expected);
2931
2932   /* Aspect ratio scaling (tall) */
2933   src = NEW_RECT (0, 0, 50, 100);
2934   dest = NEW_RECT (0, 0, 50, 50);
2935   expected = NEW_RECT (12, 0, 25, 50);
2936   gst_video_sink_center_rect (src, dest, &result, TRUE);
2937   CHECK_RECT (result, expected);
2938
2939   src = NEW_RECT (0, 0, 50, 100);
2940   dest = NEW_RECT (50, 50, 50, 50);
2941   expected = NEW_RECT (62, 50, 25, 50);
2942   gst_video_sink_center_rect (src, dest, &result, TRUE);
2943   CHECK_RECT (result, expected);
2944
2945   /* Aspect ratio scaling (wide) */
2946   src = NEW_RECT (0, 0, 100, 50);
2947   dest = NEW_RECT (0, 0, 50, 50);
2948   expected = NEW_RECT (0, 12, 50, 25);
2949   gst_video_sink_center_rect (src, dest, &result, TRUE);
2950   CHECK_RECT (result, expected);
2951
2952   src = NEW_RECT (0, 0, 100, 50);
2953   dest = NEW_RECT (50, 50, 50, 50);
2954   expected = NEW_RECT (50, 62, 50, 25);
2955   gst_video_sink_center_rect (src, dest, &result, TRUE);
2956   CHECK_RECT (result, expected);
2957 }
2958
2959 GST_END_TEST;
2960
2961 void test_overlay_blend_rect (gint x, gint y, gint width, gint height,
2962     GstVideoFrame * video_frame);
2963 void test_overlay_blend_rect_verify (gint x, gint y, gint width,
2964     gint height, GstVideoFrame * video_frame);
2965 #define VIDEO_WIDTH 320
2966 #define VIDEO_HEIGHT 240
2967
2968 void
2969 test_overlay_blend_rect_verify (gint x, gint y, gint width, gint height,
2970     GstVideoFrame * video_frame)
2971 {
2972   guint8 *data;
2973   gint i = 0, prev_i = 0;
2974   gint size = 0;
2975   gint temp_width = 0, temp_height = 0;
2976
2977   data = GST_VIDEO_FRAME_PLANE_DATA (video_frame, 0);
2978   size = GST_VIDEO_FRAME_SIZE (video_frame);
2979
2980   if (x + width < 0 || y + height < 0 || x >= VIDEO_WIDTH || y >= VIDEO_HEIGHT)
2981     return;
2982   if (x <= 0)
2983     temp_width = width + x;
2984   else if (x > 0 && (x + width) <= VIDEO_WIDTH)
2985     temp_width = width;
2986   else
2987     temp_width = VIDEO_WIDTH - x;
2988   if (y <= 0)
2989     temp_height = height + y;
2990   else if (y > 0 && (y + height) <= VIDEO_HEIGHT)
2991     temp_height = height;
2992   else
2993     temp_height = VIDEO_HEIGHT - y;
2994
2995   if (x <= 0 && y <= 0)
2996     i = 0;
2997   else
2998     i = (((x <= 0) ? 0 : x) + (((y <= 0) ? 0 : y) * VIDEO_WIDTH)) * 4;
2999   prev_i = i;
3000
3001   for (; i < size - 4; i += 4) {
3002 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
3003     /* B - G - R - A */
3004     fail_unless_equals_int (data[i], 0x80);
3005     fail_unless_equals_int (data[i + 1], 0x80);
3006     fail_unless_equals_int (data[i + 2], 0x80);
3007     fail_unless_equals_int (data[i + 3], 0x80);
3008 #else
3009     /* A - R - G - B */
3010     fail_unless_equals_int (data[i], 0x80);
3011     fail_unless_equals_int (data[i + 1], 0x80);
3012     fail_unless_equals_int (data[i + 2], 0x80);
3013     fail_unless_equals_int (data[i + 3], 0x80);
3014 #endif
3015     if ((i + 4) == (4 * (((((y > 0) ? (y + temp_height) : temp_height) -
3016                         1) * VIDEO_WIDTH) + ((x >
3017                         0) ? (x + temp_width) : temp_width))))
3018       break;
3019     if ((i + 4 - prev_i) == ((temp_width) * 4)) {
3020       i += ((VIDEO_WIDTH - (temp_width)) * 4);
3021       prev_i = i + 4;
3022     }
3023
3024   }
3025 }
3026
3027 void
3028 test_overlay_blend_rect (gint x, gint y, gint width, gint height,
3029     GstVideoFrame * video_frame)
3030 {
3031   GstVideoOverlayComposition *comp1;
3032   GstVideoOverlayRectangle *rect1;
3033   GstBuffer *pix, *pix1;
3034   GstVideoInfo vinfo;
3035
3036   memset (video_frame, 0, sizeof (GstVideoFrame));
3037   pix =
3038       gst_buffer_new_and_alloc (VIDEO_WIDTH * VIDEO_HEIGHT * sizeof (guint32));
3039   gst_buffer_memset (pix, 0, 0, gst_buffer_get_size (pix));
3040   gst_video_info_init (&vinfo);
3041   fail_unless (gst_video_info_set_format (&vinfo,
3042           GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, VIDEO_WIDTH, VIDEO_HEIGHT));
3043   gst_video_frame_map (video_frame, &vinfo, pix, GST_MAP_READWRITE);
3044   gst_buffer_unref (pix);
3045   pix = NULL;
3046
3047   pix1 = gst_buffer_new_and_alloc (width * height * sizeof (guint32));
3048   gst_buffer_memset (pix1, 0, 0x80, gst_buffer_get_size (pix1));
3049   gst_buffer_add_video_meta (pix1, GST_VIDEO_FRAME_FLAG_NONE,
3050       GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, width, height);
3051   rect1 = gst_video_overlay_rectangle_new_raw (pix1,
3052       x, y, width, height, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
3053   gst_buffer_unref (pix1);
3054   pix1 = NULL;
3055
3056   comp1 = gst_video_overlay_composition_new (rect1);
3057   fail_unless (gst_video_overlay_composition_blend (comp1, video_frame));
3058   gst_video_overlay_composition_unref (comp1);
3059   gst_video_overlay_rectangle_unref (rect1);
3060
3061   test_overlay_blend_rect_verify (x, y, width, height, video_frame);
3062   gst_video_frame_unmap (video_frame);
3063 }
3064
3065 GST_START_TEST (test_overlay_blend)
3066 {
3067   GstVideoFrame video_frame;
3068
3069   /* Overlay width & height smaller than video width & height */
3070   /* Overlay rendered completely left of video surface
3071    * x + overlay_width <= 0 */
3072   test_overlay_blend_rect (-60, 50, 50, 50, &video_frame);
3073   /* Overlay rendered completely right of video surface
3074    * x >= video_width */
3075   test_overlay_blend_rect (330, 50, 50, 50, &video_frame);
3076   /* Overlay rendered completely top of video surface
3077    * y + overlay_height <= 0 */
3078   test_overlay_blend_rect (50, -60, 50, 50, &video_frame);
3079   /* Overlay rendered completely bottom of video surface
3080    * y >= video_height */
3081   test_overlay_blend_rect (50, 250, 50, 50, &video_frame);
3082   /* Overlay rendered partially left of video surface
3083    * x < 0 && -x < overlay_width */
3084   test_overlay_blend_rect (-40, 50, 50, 50, &video_frame);
3085   /* Overlay rendered partially right of video surface
3086    * x < video_width && (overlay_width + x) > video_width */
3087   test_overlay_blend_rect (300, 50, 50, 50, &video_frame);
3088   /* Overlay rendered partially top of video surface
3089    * y < 0 && -y < overlay_height */
3090   test_overlay_blend_rect (50, -40, 50, 50, &video_frame);
3091   /* Overlay rendered partially bottom of video surface
3092    * y < video_height && (overlay_height + y) > video_height */
3093   test_overlay_blend_rect (50, 220, 50, 50, &video_frame);
3094
3095   /* Overlay width & height bigger than video width & height */
3096   /* Overlay rendered completely left of video surface
3097    * x + overlay_width <= 0 */
3098   test_overlay_blend_rect (-360, 50, 350, 250, &video_frame);
3099   /* Overlay rendered completely right of video surface
3100    * x >= video_width */
3101   test_overlay_blend_rect (330, 50, 350, 250, &video_frame);
3102   /* Overlay rendered completely top of video surface
3103    * y + overlay_height <= 0 */
3104   test_overlay_blend_rect (50, -260, 350, 250, &video_frame);
3105   /* Overlay rendered completely bottom of video surface
3106    * y >= video_height */
3107   test_overlay_blend_rect (50, 250, 350, 250, &video_frame);
3108   /* Overlay rendered partially left of video surface
3109    * x < 0 && -x < overlay_width */
3110   test_overlay_blend_rect (-40, 50, 350, 250, &video_frame);
3111   /* Overlay rendered partially right of video surface
3112    * x < video_width && (overlay_width + x) > video_width */
3113   test_overlay_blend_rect (300, 50, 350, 250, &video_frame);
3114   /* Overlay rendered partially top of video surface
3115    * y < 0 && -y < overlay_height */
3116   test_overlay_blend_rect (50, -40, 350, 250, &video_frame);
3117   /* Overlay rendered partially bottom of video surface
3118    * y < video_height && (overlay_height + y) > video_height */
3119   test_overlay_blend_rect (50, 220, 350, 250, &video_frame);
3120 }
3121
3122 GST_END_TEST;
3123
3124 GST_START_TEST (test_overlay_composition_over_transparency)
3125 {
3126   GstVideoOverlayComposition *comp1;
3127   GstVideoOverlayRectangle *rect1;
3128   GstBuffer *pix1, *pix2;
3129   GstVideoInfo vinfo;
3130   guint8 *data;
3131
3132   GstVideoFrame video_frame;
3133   guint fwidth = 200, height = 50, swidth = 100;
3134
3135   memset (&video_frame, 0, sizeof (GstVideoFrame));
3136
3137   pix1 = gst_buffer_new_and_alloc (fwidth * sizeof (guint32) * height);
3138   gst_buffer_memset (pix1, 0, 0x00, gst_buffer_get_size (pix1));
3139   gst_video_info_init (&vinfo);
3140   fail_unless (gst_video_info_set_format (&vinfo,
3141           GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, fwidth, height));
3142   gst_video_frame_map (&video_frame, &vinfo, pix1, GST_MAP_READWRITE);
3143   gst_buffer_unref (pix1);
3144
3145   pix2 = gst_buffer_new_and_alloc (swidth * sizeof (guint32) * height);
3146   gst_buffer_memset (pix2, 0, 0xFF, gst_buffer_get_size (pix2));
3147   gst_buffer_add_video_meta (pix2, GST_VIDEO_FRAME_FLAG_NONE,
3148       GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, swidth, height);
3149   rect1 = gst_video_overlay_rectangle_new_raw (pix2, swidth, 0,
3150       swidth, height, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
3151
3152   comp1 = gst_video_overlay_composition_new (rect1);
3153   fail_unless (gst_video_overlay_composition_blend (comp1, &video_frame));
3154   gst_video_overlay_composition_unref (comp1);
3155   gst_video_overlay_rectangle_unref (rect1);
3156   gst_buffer_unref (pix2);
3157
3158   data = GST_VIDEO_FRAME_PLANE_DATA (&video_frame, 0);
3159
3160   fail_unless_equals_int (data[0], 0x00);
3161   fail_unless_equals_int (data[1], 0x00);
3162   fail_unless_equals_int (data[2], 0x00);
3163   fail_unless_equals_int (data[3], 0x00);
3164
3165   data += swidth * sizeof (guint32);
3166
3167   fail_unless_equals_int (data[0], 0xFF);
3168   fail_unless_equals_int (data[1], 0xFF);
3169   fail_unless_equals_int (data[2], 0xFF);
3170   fail_unless_equals_int (data[3], 0xFF);
3171
3172   gst_video_frame_unmap (&video_frame);
3173 }
3174
3175 GST_END_TEST;
3176
3177 GST_START_TEST (test_video_format_enum_stability)
3178 {
3179   /* When adding new formats, adding a format in the middle of the enum will
3180    * break the API. This check picks the last known format and checks that
3181    * it's value isn't changing. This test should ideall be updated when a new
3182    * format is added, though will stay valid. */
3183   fail_unless_equals_int (GST_VIDEO_FORMAT_Y210, 82);
3184 }
3185
3186 GST_END_TEST;
3187
3188 GST_START_TEST (test_video_formats_pstrides)
3189 {
3190   GstVideoFormat fmt = GST_VIDEO_FORMAT_I420;
3191
3192
3193   while ((gst_video_format_to_string (fmt) != NULL)) {
3194     const GstVideoFormatInfo *vf_info = gst_video_format_get_info (fmt);
3195     guint n_comps = GST_VIDEO_FORMAT_INFO_N_COMPONENTS (vf_info);
3196
3197     GST_LOG ("format: %s (%d), n_comps = %u", vf_info->name, fmt, n_comps);
3198
3199     if (fmt == GST_VIDEO_FORMAT_v210
3200         || fmt == GST_VIDEO_FORMAT_UYVP
3201         || fmt == GST_VIDEO_FORMAT_IYU1
3202         || fmt == GST_VIDEO_FORMAT_GRAY10_LE32
3203         || fmt == GST_VIDEO_FORMAT_NV12_64Z32
3204         || fmt == GST_VIDEO_FORMAT_NV12_4L4
3205         || fmt == GST_VIDEO_FORMAT_NV12_32L32
3206         || fmt == GST_VIDEO_FORMAT_NV12_10LE32
3207         || fmt == GST_VIDEO_FORMAT_NV16_10LE32
3208         || fmt == GST_VIDEO_FORMAT_NV12_10LE40
3209         || fmt == GST_VIDEO_FORMAT_Y410) {
3210       fmt++;
3211       continue;
3212     }
3213
3214     switch (n_comps) {
3215       case 4:
3216         fail_unless (GST_VIDEO_FORMAT_INFO_PSTRIDE (vf_info, 3) > 0);
3217         /* fall through */
3218       case 3:
3219         fail_unless (GST_VIDEO_FORMAT_INFO_PSTRIDE (vf_info, 2) > 0);
3220         /* fall through */
3221       case 2:
3222         fail_unless (GST_VIDEO_FORMAT_INFO_PSTRIDE (vf_info, 1) > 0);
3223         /* fall through */
3224       case 1:
3225         fail_unless (GST_VIDEO_FORMAT_INFO_PSTRIDE (vf_info, 0) > 0);
3226         break;
3227     }
3228
3229     fmt++;
3230   }
3231 }
3232
3233 GST_END_TEST;
3234
3235 GST_START_TEST (test_hdr)
3236 {
3237   GstCaps *caps;
3238   GstCaps *other_caps;
3239   GstVideoMasteringDisplayInfo minfo;
3240   GstVideoMasteringDisplayInfo other_minfo;
3241   GstVideoMasteringDisplayInfo minfo_from_caps;
3242   GstVideoContentLightLevel level;
3243   GstVideoContentLightLevel other_level;
3244   GstVideoContentLightLevel level_from_caps;
3245   GstStructure *s = NULL;
3246   gchar *minfo_str;
3247   gchar *level_str = NULL;
3248   gint i;
3249   guint val;
3250
3251
3252   gst_video_mastering_display_info_init (&minfo);
3253   gst_video_mastering_display_info_init (&other_minfo);
3254
3255   /* Test GstVideoMasteringDisplayInfo, initialize with random values
3256    * just for comparison */
3257   val = 1;
3258   for (i = 0; i < G_N_ELEMENTS (minfo.display_primaries); i++) {
3259     minfo.display_primaries[i].x = val++;
3260     minfo.display_primaries[i].y = val++;
3261   }
3262   minfo.white_point.x = val++;
3263   minfo.white_point.y = val++;
3264   minfo.max_display_mastering_luminance = val++;
3265   minfo.min_display_mastering_luminance = val++;
3266
3267   caps = gst_caps_new_empty_simple ("video/x-raw");
3268   minfo_str = gst_video_mastering_display_info_to_string (&minfo);
3269   fail_unless (minfo_str != NULL, "cannot convert info to string");
3270   GST_DEBUG ("converted mastering info string %s", minfo_str);
3271
3272   gst_caps_set_simple (caps, "mastering-display-info",
3273       G_TYPE_STRING, minfo_str, NULL);
3274   g_free (minfo_str);
3275   minfo_str = NULL;
3276
3277   /* manually parsing mastering info from string */
3278   s = gst_caps_get_structure (caps, 0);
3279   minfo_str = (gchar *) gst_structure_get_string (s, "mastering-display-info");
3280   fail_unless (minfo_str != NULL);
3281   fail_unless (gst_video_mastering_display_info_from_string
3282       (&other_minfo, minfo_str), "cannot get mastering info from string");
3283   GST_DEBUG ("extracted info string %s", minfo_str);
3284
3285   fail_unless (gst_video_mastering_display_info_is_equal (&minfo,
3286           &other_minfo), "Extracted mastering info is not equal to original");
3287
3288   /* simplified version for caps use case */
3289   fail_unless (gst_video_mastering_display_info_from_caps (&minfo_from_caps,
3290           caps), "cannot parse mastering info from caps");
3291   fail_unless (gst_video_mastering_display_info_is_equal (&minfo,
3292           &minfo_from_caps),
3293       "Extracted mastering info is not equal to original");
3294
3295   /* check _add_to_caps () and manually created one */
3296   other_caps = gst_caps_new_empty_simple ("video/x-raw");
3297   fail_unless (gst_video_mastering_display_info_add_to_caps (&other_minfo,
3298           other_caps));
3299   fail_unless (gst_caps_is_equal (caps, other_caps));
3300
3301   gst_caps_unref (caps);
3302   gst_caps_unref (other_caps);
3303
3304   /* Test GstVideoContentLightLevel */
3305   gst_video_content_light_level_init (&level);
3306   gst_video_content_light_level_init (&other_level);
3307
3308   level.max_content_light_level = 1000;
3309   level.max_frame_average_light_level = 300;
3310
3311   caps = gst_caps_new_empty_simple ("video/x-raw");
3312   level_str = gst_video_content_light_level_to_string (&level);
3313   fail_unless (level_str != NULL);
3314
3315   gst_caps_set_simple (caps, "content-light-level",
3316       G_TYPE_STRING, level_str, NULL);
3317   g_free (level_str);
3318
3319   /* manually parsing CLL info from string */
3320   s = gst_caps_get_structure (caps, 0);
3321   fail_unless (gst_structure_get (s, "content-light-level",
3322           G_TYPE_STRING, &level_str, NULL), "Failed to get level from caps");
3323   fail_unless (gst_video_content_light_level_from_string (&other_level,
3324           level_str));
3325   g_free (level_str);
3326
3327   fail_unless_equals_int (level.max_content_light_level,
3328       other_level.max_content_light_level);
3329   fail_unless_equals_int (level.max_frame_average_light_level,
3330       other_level.max_frame_average_light_level);
3331
3332   /* simplified version for caps use case */
3333   fail_unless (gst_video_content_light_level_from_caps (&level_from_caps,
3334           caps));
3335   fail_unless_equals_int (level.max_content_light_level,
3336       level_from_caps.max_content_light_level);
3337   fail_unless_equals_int (level.max_frame_average_light_level,
3338       level_from_caps.max_frame_average_light_level);
3339
3340   /* check _add_to_caps () and manually created one */
3341   other_caps = gst_caps_new_empty_simple ("video/x-raw");
3342   fail_unless (gst_video_content_light_level_add_to_caps (&other_level,
3343           other_caps));
3344   fail_unless (gst_caps_is_equal (caps, other_caps));
3345
3346   gst_caps_unref (caps);
3347   gst_caps_unref (other_caps);
3348 }
3349
3350 GST_END_TEST;
3351
3352 GST_START_TEST (test_video_color_from_to_iso)
3353 {
3354   gint i;
3355
3356 #define ISO_IEC_UNSPECIFIED_COLOR_VALUE 2
3357
3358   for (i = 0; i <= GST_VIDEO_COLOR_MATRIX_BT2020; i++) {
3359     guint matrix_val = gst_video_color_matrix_to_iso (i);
3360     fail_unless_equals_int (gst_video_color_matrix_from_iso (matrix_val), i);
3361   }
3362
3363   for (i = 0; i <= GST_VIDEO_TRANSFER_ARIB_STD_B67; i++) {
3364     guint transfer_val = gst_video_transfer_function_to_iso (i);
3365
3366     /* don't know how to map below values to spec. */
3367     if (i == GST_VIDEO_TRANSFER_GAMMA18 || i == GST_VIDEO_TRANSFER_GAMMA20
3368         || i == GST_VIDEO_TRANSFER_ADOBERGB) {
3369       fail_unless_equals_int (transfer_val, ISO_IEC_UNSPECIFIED_COLOR_VALUE);
3370       continue;
3371     }
3372
3373     fail_unless_equals_int (gst_video_transfer_function_from_iso (transfer_val),
3374         i);
3375   }
3376
3377   for (i = 0; i <= GST_VIDEO_COLOR_PRIMARIES_EBU3213; i++) {
3378     guint primaries_val = gst_video_color_primaries_to_iso (i);
3379
3380     /* don't know how to map below value to spec. */
3381     if (i == GST_VIDEO_COLOR_PRIMARIES_ADOBERGB) {
3382       fail_unless_equals_int (primaries_val, ISO_IEC_UNSPECIFIED_COLOR_VALUE);
3383       continue;
3384     }
3385
3386     fail_unless_equals_int (gst_video_color_primaries_from_iso (primaries_val),
3387         i);
3388   }
3389 #undef ISO_IEC_UNSPECIFIED_COLOR_VALUE
3390 }
3391
3392 GST_END_TEST;
3393
3394 GST_START_TEST (test_video_format_info_plane_to_components)
3395 {
3396   const GstVideoFormatInfo *info;
3397   gint comps[GST_VIDEO_MAX_COMPONENTS];
3398
3399   /* RGB: 1 plane, 3 components */
3400   info = gst_video_format_get_info (GST_VIDEO_FORMAT_RGB);
3401
3402   gst_video_format_info_component (info, 0, comps);
3403   g_assert_cmpint (comps[0], ==, 0);
3404   g_assert_cmpint (comps[1], ==, 1);
3405   g_assert_cmpint (comps[2], ==, 2);
3406   g_assert_cmpint (comps[3], ==, -1);
3407
3408   gst_video_format_info_component (info, 1, comps);
3409   g_assert_cmpint (comps[0], ==, -1);
3410   g_assert_cmpint (comps[1], ==, -1);
3411   g_assert_cmpint (comps[2], ==, -1);
3412   g_assert_cmpint (comps[3], ==, -1);
3413
3414   gst_video_format_info_component (info, 2, comps);
3415   g_assert_cmpint (comps[0], ==, -1);
3416   g_assert_cmpint (comps[1], ==, -1);
3417   g_assert_cmpint (comps[2], ==, -1);
3418   g_assert_cmpint (comps[3], ==, -1);
3419
3420   gst_video_format_info_component (info, 3, comps);
3421   g_assert_cmpint (comps[0], ==, -1);
3422   g_assert_cmpint (comps[1], ==, -1);
3423   g_assert_cmpint (comps[2], ==, -1);
3424   g_assert_cmpint (comps[3], ==, -1);
3425
3426   /* I420: 3 planes, 3 components */
3427   info = gst_video_format_get_info (GST_VIDEO_FORMAT_I420);
3428
3429   gst_video_format_info_component (info, 0, comps);
3430   g_assert_cmpint (comps[0], ==, 0);
3431   g_assert_cmpint (comps[1], ==, -1);
3432   g_assert_cmpint (comps[2], ==, -1);
3433   g_assert_cmpint (comps[3], ==, -1);
3434
3435   gst_video_format_info_component (info, 1, comps);
3436   g_assert_cmpint (comps[0], ==, 1);
3437   g_assert_cmpint (comps[1], ==, -1);
3438   g_assert_cmpint (comps[2], ==, -1);
3439   g_assert_cmpint (comps[3], ==, -1);
3440
3441   gst_video_format_info_component (info, 2, comps);
3442   g_assert_cmpint (comps[0], ==, 2);
3443   g_assert_cmpint (comps[1], ==, -1);
3444   g_assert_cmpint (comps[2], ==, -1);
3445   g_assert_cmpint (comps[3], ==, -1);
3446
3447   gst_video_format_info_component (info, 3, comps);
3448   g_assert_cmpint (comps[0], ==, -1);
3449   g_assert_cmpint (comps[1], ==, -1);
3450   g_assert_cmpint (comps[2], ==, -1);
3451   g_assert_cmpint (comps[3], ==, -1);
3452
3453   /* NV12: 2 planes, 3 components */
3454   info = gst_video_format_get_info (GST_VIDEO_FORMAT_NV12);
3455
3456   gst_video_format_info_component (info, 0, comps);
3457   g_assert_cmpint (comps[0], ==, 0);
3458   g_assert_cmpint (comps[1], ==, -1);
3459   g_assert_cmpint (comps[2], ==, -1);
3460   g_assert_cmpint (comps[3], ==, -1);
3461
3462   gst_video_format_info_component (info, 1, comps);
3463   g_assert_cmpint (comps[0], ==, 1);
3464   g_assert_cmpint (comps[1], ==, 2);
3465   g_assert_cmpint (comps[2], ==, -1);
3466   g_assert_cmpint (comps[3], ==, -1);
3467
3468   gst_video_format_info_component (info, 2, comps);
3469   g_assert_cmpint (comps[0], ==, -1);
3470   g_assert_cmpint (comps[1], ==, -1);
3471   g_assert_cmpint (comps[2], ==, -1);
3472   g_assert_cmpint (comps[3], ==, -1);
3473
3474   gst_video_format_info_component (info, 3, comps);
3475   g_assert_cmpint (comps[0], ==, -1);
3476   g_assert_cmpint (comps[1], ==, -1);
3477   g_assert_cmpint (comps[2], ==, -1);
3478   g_assert_cmpint (comps[3], ==, -1);
3479 }
3480
3481 GST_END_TEST;
3482
3483 GST_START_TEST (test_video_info_align)
3484 {
3485   GstVideoInfo info;
3486   GstVideoAlignment align;
3487   gsize plane_size[GST_VIDEO_MAX_PLANES];
3488
3489   /* NV12 */
3490   gst_video_info_init (&info);
3491   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_NV12, 1920, 1080);
3492
3493   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3494   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 1080);
3495   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 1080 * 1.5);
3496
3497   gst_video_alignment_reset (&align);
3498   /* Align with no padding to retrieve the plane heights */
3499   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3500
3501   g_assert_cmpuint (plane_size[0], ==, 1920 * 1080);
3502   g_assert_cmpuint (plane_size[1], ==, 1920 * 1080 / 2);
3503   g_assert_cmpuint (plane_size[2], ==, 0);
3504   g_assert_cmpuint (plane_size[3], ==, 0);
3505
3506   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3507   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 1920);
3508   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3509   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3510   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3511       1080);
3512   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3513       540);
3514   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3515   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3516
3517   gst_video_alignment_reset (&align);
3518   align.padding_bottom = 8;
3519   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3520
3521   g_assert_cmpuint (plane_size[0], ==, 1920 * 1088);
3522   g_assert_cmpuint (plane_size[1], ==, 1920 * 1088 / 2);
3523   g_assert_cmpuint (plane_size[2], ==, 0);
3524   g_assert_cmpuint (plane_size[3], ==, 0);
3525
3526   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3527   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 1080);
3528   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 1088 * 1.5);
3529
3530   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3531   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 1920);
3532   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3533   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3534   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3535       1088);
3536   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3537       544);
3538   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3539   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3540
3541   /* NV16 */
3542   gst_video_info_init (&info);
3543   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_NV16, 1920, 1080);
3544
3545   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3546   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 1080);
3547   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 1080 * 2);
3548
3549   gst_video_alignment_reset (&align);
3550   /* Align with no padding to retrieve the plane heights */
3551   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3552
3553   g_assert_cmpuint (plane_size[0], ==, 1920 * 1080);
3554   g_assert_cmpuint (plane_size[1], ==, 1920 * 1080);
3555   g_assert_cmpuint (plane_size[2], ==, 0);
3556   g_assert_cmpuint (plane_size[3], ==, 0);
3557
3558   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3559   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 1920);
3560   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3561   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3562   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3563       1080);
3564   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3565       1080);
3566   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3567   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3568
3569   gst_video_alignment_reset (&align);
3570   align.padding_bottom = 8;
3571   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3572
3573   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3574   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 1080);
3575   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 1088 * 2);
3576
3577   g_assert_cmpuint (plane_size[0], ==, 1920 * 1088);
3578   g_assert_cmpuint (plane_size[1], ==, 1920 * 1088);
3579   g_assert_cmpuint (plane_size[2], ==, 0);
3580   g_assert_cmpuint (plane_size[3], ==, 0);
3581
3582   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3583   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 1920);
3584   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3585   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3586   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3587       1088);
3588   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3589       1088);
3590   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3591   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3592
3593   /* RGB */
3594   gst_video_info_init (&info);
3595   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_RGB, 1920, 1080);
3596
3597   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3598   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 1080);
3599   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 1080 * 3);
3600
3601   gst_video_alignment_reset (&align);
3602   /* Align with no padding to retrieve the plane heights */
3603   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3604
3605   g_assert_cmpuint (plane_size[0], ==, 1920 * 1080 * 3);
3606   g_assert_cmpuint (plane_size[1], ==, 0);
3607   g_assert_cmpuint (plane_size[2], ==, 0);
3608   g_assert_cmpuint (plane_size[3], ==, 0);
3609
3610   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 5760);
3611   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 0);
3612   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3613   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3614   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3615       1080);
3616   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==, 0);
3617   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3618   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3619
3620   gst_video_alignment_reset (&align);
3621   align.padding_bottom = 8;
3622   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3623
3624   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3625   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 1080);
3626   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 1088 * 3);
3627
3628   g_assert_cmpuint (plane_size[0], ==, 1920 * 1088 * 3);
3629   g_assert_cmpuint (plane_size[1], ==, 0);
3630   g_assert_cmpuint (plane_size[2], ==, 0);
3631   g_assert_cmpuint (plane_size[3], ==, 0);
3632
3633   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 5760);
3634   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 0);
3635   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3636   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3637   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3638       1088);
3639   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==, 0);
3640   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3641   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3642
3643   /* I420 */
3644   gst_video_info_init (&info);
3645   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_I420, 1920, 1080);
3646
3647   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3648   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 1080);
3649   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 1080 * 1.5);
3650
3651   gst_video_alignment_reset (&align);
3652   /* Align with no padding to retrieve the plane heights */
3653   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3654
3655   g_assert_cmpuint (plane_size[0], ==, 1920 * 1080);
3656   g_assert_cmpuint (plane_size[1], ==, 1920 * 1080 / 4);
3657   g_assert_cmpuint (plane_size[2], ==, 1920 * 1080 / 4);
3658   g_assert_cmpuint (plane_size[3], ==, 0);
3659
3660   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3661   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 960);
3662   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 960);
3663   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3664   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3665       1080);
3666   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3667       540);
3668   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==,
3669       540);
3670   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3671
3672   gst_video_alignment_reset (&align);
3673   align.padding_bottom = 8;
3674   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3675
3676   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3677   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 1080);
3678   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 1088 * 1.5);
3679
3680   g_assert_cmpuint (plane_size[0], ==, 1920 * 1088);
3681   g_assert_cmpuint (plane_size[1], ==, 1920 * 1088 / 4);
3682   g_assert_cmpuint (plane_size[2], ==, 1920 * 1088 / 4);
3683   g_assert_cmpuint (plane_size[3], ==, 0);
3684
3685   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3686   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 960);
3687   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 960);
3688   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3689   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3690       1088);
3691   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3692       544);
3693   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==,
3694       544);
3695   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3696
3697   /* NV16 alternate */
3698   gst_video_info_init (&info);
3699   gst_video_info_set_interlaced_format (&info, GST_VIDEO_FORMAT_NV16,
3700       GST_VIDEO_INTERLACE_MODE_ALTERNATE, 1920, 1080);
3701
3702   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3703   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 540);
3704   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 540 * 2);
3705
3706   gst_video_alignment_reset (&align);
3707   /* Align with no padding to retrieve the plane heights */
3708   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3709
3710   g_assert_cmpuint (plane_size[0], ==, 1920 * 540);
3711   g_assert_cmpuint (plane_size[1], ==, 1920 * 540);
3712   g_assert_cmpuint (plane_size[2], ==, 0);
3713   g_assert_cmpuint (plane_size[3], ==, 0);
3714
3715   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3716   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 1920);
3717   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3718   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3719   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3720       540);
3721   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3722       540);
3723   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3724   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3725
3726   gst_video_alignment_reset (&align);
3727   align.padding_bottom = 8;
3728   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3729
3730   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1080);
3731   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 540);
3732   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 544 * 2);
3733
3734   g_assert_cmpuint (plane_size[0], ==, 1920 * 544);
3735   g_assert_cmpuint (plane_size[1], ==, 1920 * 544);
3736   g_assert_cmpuint (plane_size[2], ==, 0);
3737   g_assert_cmpuint (plane_size[3], ==, 0);
3738
3739   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3740   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 1920);
3741   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3742   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3743   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3744       544);
3745   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3746       544);
3747   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3748   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3749
3750   /* NV16 alternate with an odd height */
3751   gst_video_info_init (&info);
3752   gst_video_info_set_interlaced_format (&info, GST_VIDEO_FORMAT_NV16,
3753       GST_VIDEO_INTERLACE_MODE_ALTERNATE, 1920, 1081);
3754
3755   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1081);
3756   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 541);
3757   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 541 * 2);
3758
3759   gst_video_alignment_reset (&align);
3760   /* Align with no padding to retrieve the plane heights */
3761   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3762
3763   g_assert_cmpuint (plane_size[0], ==, 1920 * 541);
3764   g_assert_cmpuint (plane_size[1], ==, 1920 * 541);
3765   g_assert_cmpuint (plane_size[2], ==, 0);
3766   g_assert_cmpuint (plane_size[3], ==, 0);
3767
3768   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3769   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 1920);
3770   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3771   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3772   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3773       541);
3774   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3775       541);
3776   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3777   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3778
3779   gst_video_alignment_reset (&align);
3780   align.padding_bottom = 2;
3781   g_assert (gst_video_info_align_full (&info, &align, plane_size));
3782
3783   g_assert_cmpuint (GST_VIDEO_INFO_HEIGHT (&info), ==, 1081);
3784   g_assert_cmpuint (GST_VIDEO_INFO_FIELD_HEIGHT (&info), ==, 541);
3785   g_assert_cmpuint (GST_VIDEO_INFO_SIZE (&info), ==, 1920 * 542 * 2);
3786
3787   g_assert_cmpuint (plane_size[0], ==, 1920 * 542);
3788   g_assert_cmpuint (plane_size[1], ==, 1920 * 542);
3789   g_assert_cmpuint (plane_size[2], ==, 0);
3790   g_assert_cmpuint (plane_size[3], ==, 0);
3791
3792   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 0), ==, 1920);
3793   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 1), ==, 1920);
3794   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 2), ==, 0);
3795   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_STRIDE (&info, 3), ==, 0);
3796   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 0, plane_size), ==,
3797       542);
3798   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 1, plane_size), ==,
3799       542);
3800   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 2, plane_size), ==, 0);
3801   g_assert_cmpuint (GST_VIDEO_INFO_PLANE_HEIGHT (&info, 3, plane_size), ==, 0);
3802 }
3803
3804 GST_END_TEST;
3805
3806 GST_START_TEST (test_video_meta_align)
3807 {
3808   GstBuffer *buf;
3809   GstVideoInfo info;
3810   GstVideoMeta *meta;
3811   gsize plane_size[GST_VIDEO_MAX_PLANES];
3812   guint plane_height[GST_VIDEO_MAX_PLANES];
3813   GstVideoAlignment alig;
3814
3815   buf = gst_buffer_new ();
3816
3817   /* NV12 no alignment */
3818   gst_video_info_init (&info);
3819   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_NV12, 1920, 1080);
3820
3821   meta = gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
3822       GST_VIDEO_INFO_FORMAT (&info), GST_VIDEO_INFO_WIDTH (&info),
3823       GST_VIDEO_INFO_HEIGHT (&info), GST_VIDEO_INFO_N_PLANES (&info),
3824       info.offset, info.stride);
3825
3826   g_assert_cmpuint (meta->alignment.padding_top, ==, 0);
3827   g_assert_cmpuint (meta->alignment.padding_bottom, ==, 0);
3828   g_assert_cmpuint (meta->alignment.padding_left, ==, 0);
3829   g_assert_cmpuint (meta->alignment.padding_right, ==, 0);
3830
3831   g_assert (gst_video_meta_get_plane_size (meta, plane_size));
3832   g_assert_cmpuint (plane_size[0], ==, 1920 * 1080);
3833   g_assert_cmpuint (plane_size[1], ==, 1920 * 1080 * 0.5);
3834   g_assert_cmpuint (plane_size[2], ==, 0);
3835   g_assert_cmpuint (plane_size[3], ==, 0);
3836
3837   g_assert (gst_video_meta_get_plane_height (meta, plane_height));
3838   g_assert_cmpuint (plane_height[0], ==, 1080);
3839   g_assert_cmpuint (plane_height[1], ==, 540);
3840   g_assert_cmpuint (plane_height[2], ==, 0);
3841   g_assert_cmpuint (plane_height[3], ==, 0);
3842
3843   /* horizontal alignment */
3844   gst_video_info_init (&info);
3845   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_NV12, 1920, 1080);
3846
3847   gst_video_alignment_reset (&alig);
3848   alig.padding_left = 2;
3849   alig.padding_right = 6;
3850
3851   g_assert (gst_video_info_align (&info, &alig));
3852
3853   meta = gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
3854       GST_VIDEO_INFO_FORMAT (&info), GST_VIDEO_INFO_WIDTH (&info),
3855       GST_VIDEO_INFO_HEIGHT (&info), GST_VIDEO_INFO_N_PLANES (&info),
3856       info.offset, info.stride);
3857   g_assert (gst_video_meta_set_alignment (meta, alig));
3858
3859   g_assert_cmpuint (meta->alignment.padding_top, ==, 0);
3860   g_assert_cmpuint (meta->alignment.padding_bottom, ==, 0);
3861   g_assert_cmpuint (meta->alignment.padding_left, ==, 2);
3862   g_assert_cmpuint (meta->alignment.padding_right, ==, 6);
3863
3864   g_assert (gst_video_meta_get_plane_size (meta, plane_size));
3865   g_assert_cmpuint (plane_size[0], ==, 1928 * 1080);
3866   g_assert_cmpuint (plane_size[1], ==, 1928 * 1080 * 0.5);
3867   g_assert_cmpuint (plane_size[2], ==, 0);
3868   g_assert_cmpuint (plane_size[3], ==, 0);
3869
3870   g_assert (gst_video_meta_get_plane_height (meta, plane_height));
3871   g_assert_cmpuint (plane_height[0], ==, 1080);
3872   g_assert_cmpuint (plane_height[1], ==, 540);
3873   g_assert_cmpuint (plane_height[2], ==, 0);
3874   g_assert_cmpuint (plane_height[3], ==, 0);
3875
3876   /* vertical alignment */
3877   gst_video_info_init (&info);
3878   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_NV12, 1920, 1080);
3879
3880   gst_video_alignment_reset (&alig);
3881   alig.padding_top = 2;
3882   alig.padding_bottom = 6;
3883
3884   g_assert (gst_video_info_align (&info, &alig));
3885
3886   meta = gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
3887       GST_VIDEO_INFO_FORMAT (&info), GST_VIDEO_INFO_WIDTH (&info),
3888       GST_VIDEO_INFO_HEIGHT (&info), GST_VIDEO_INFO_N_PLANES (&info),
3889       info.offset, info.stride);
3890   g_assert (gst_video_meta_set_alignment (meta, alig));
3891
3892   g_assert_cmpuint (meta->alignment.padding_top, ==, 2);
3893   g_assert_cmpuint (meta->alignment.padding_bottom, ==, 6);
3894   g_assert_cmpuint (meta->alignment.padding_left, ==, 0);
3895   g_assert_cmpuint (meta->alignment.padding_right, ==, 0);
3896
3897   g_assert (gst_video_meta_get_plane_size (meta, plane_size));
3898   g_assert_cmpuint (plane_size[0], ==, 1920 * 1088);
3899   g_assert_cmpuint (plane_size[1], ==, 1920 * 1088 * 0.5);
3900   g_assert_cmpuint (plane_size[2], ==, 0);
3901   g_assert_cmpuint (plane_size[3], ==, 0);
3902
3903   g_assert (gst_video_meta_get_plane_height (meta, plane_height));
3904   g_assert_cmpuint (plane_height[0], ==, 1088);
3905   g_assert_cmpuint (plane_height[1], ==, 544);
3906   g_assert_cmpuint (plane_height[2], ==, 0);
3907   g_assert_cmpuint (plane_height[3], ==, 0);
3908
3909   /* incompatible alignment */
3910   gst_video_info_init (&info);
3911   gst_video_info_set_format (&info, GST_VIDEO_FORMAT_NV12, 1920, 1080);
3912
3913   gst_video_alignment_reset (&alig);
3914   alig.padding_right = 2;
3915
3916   meta = gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
3917       GST_VIDEO_INFO_FORMAT (&info), GST_VIDEO_INFO_WIDTH (&info),
3918       GST_VIDEO_INFO_HEIGHT (&info), GST_VIDEO_INFO_N_PLANES (&info),
3919       info.offset, info.stride);
3920   g_assert (!gst_video_meta_set_alignment (meta, alig));
3921
3922   gst_buffer_unref (buf);
3923 }
3924
3925 GST_END_TEST;
3926
3927 GST_START_TEST (test_video_flags)
3928 {
3929   GstBuffer *buf;
3930   GstVideoInfo info;
3931   GstVideoFrame frame;
3932
3933   gst_video_info_init (&info);
3934   fail_unless (gst_video_info_set_interlaced_format (&info,
3935           GST_VIDEO_FORMAT_RGB, GST_VIDEO_INTERLACE_MODE_ALTERNATE, 4, 4));
3936
3937   buf = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (&info));
3938   fail_unless (!GST_VIDEO_BUFFER_IS_TOP_FIELD (buf));
3939   fail_unless (!GST_VIDEO_BUFFER_IS_BOTTOM_FIELD (buf));
3940   fail_unless (gst_video_frame_map (&frame, &info, buf, GST_MAP_READ));
3941   fail_unless (!GST_VIDEO_FRAME_IS_TOP_FIELD (&frame));
3942   fail_unless (!GST_VIDEO_FRAME_IS_BOTTOM_FIELD (&frame));
3943   gst_video_frame_unmap (&frame);
3944   gst_buffer_unref (buf);
3945
3946   buf = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (&info));
3947   GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_TOP_FIELD);
3948   fail_unless (GST_VIDEO_BUFFER_IS_TOP_FIELD (buf));
3949   fail_unless (!GST_VIDEO_BUFFER_IS_BOTTOM_FIELD (buf));
3950   fail_unless (gst_video_frame_map (&frame, &info, buf, GST_MAP_READ));
3951   fail_unless (GST_VIDEO_FRAME_IS_TOP_FIELD (&frame));
3952   fail_unless (!GST_VIDEO_FRAME_IS_BOTTOM_FIELD (&frame));
3953   gst_video_frame_unmap (&frame);
3954   gst_buffer_unref (buf);
3955
3956   buf = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (&info));
3957   GST_BUFFER_FLAG_SET (buf, GST_VIDEO_BUFFER_FLAG_BOTTOM_FIELD);
3958   fail_unless (!GST_VIDEO_BUFFER_IS_TOP_FIELD (buf));
3959   fail_unless (GST_VIDEO_BUFFER_IS_BOTTOM_FIELD (buf));
3960   fail_unless (gst_video_frame_map (&frame, &info, buf, GST_MAP_READ));
3961   fail_unless (!GST_VIDEO_FRAME_IS_TOP_FIELD (&frame));
3962   fail_unless (GST_VIDEO_FRAME_IS_BOTTOM_FIELD (&frame));
3963   gst_video_frame_unmap (&frame);
3964   gst_buffer_unref (buf);
3965 }
3966
3967 GST_END_TEST;
3968
3969 GST_START_TEST (test_video_make_raw_caps)
3970 {
3971   GstCaps *caps, *expected;
3972   GstVideoFormat f1[] = { GST_VIDEO_FORMAT_NV12 };
3973   GstVideoFormat f2[] = { GST_VIDEO_FORMAT_NV12, GST_VIDEO_FORMAT_NV16 };
3974
3975   caps = gst_video_make_raw_caps (f1, G_N_ELEMENTS (f1));
3976   expected = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("NV12"));
3977   fail_unless (gst_caps_is_equal (caps, expected));
3978   gst_caps_unref (caps);
3979   gst_caps_unref (expected);
3980
3981   caps = gst_video_make_raw_caps (f2, G_N_ELEMENTS (f2));
3982   expected = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("{ NV12, NV16 }"));
3983   fail_unless (gst_caps_is_equal (caps, expected));
3984   gst_caps_unref (caps);
3985   gst_caps_unref (expected);
3986
3987   caps = gst_video_make_raw_caps (NULL, 0);
3988   expected = gst_caps_from_string (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL));
3989   fail_unless (gst_caps_is_equal (caps, expected));
3990   gst_caps_unref (caps);
3991   gst_caps_unref (expected);
3992
3993   caps =
3994       gst_video_make_raw_caps_with_features (NULL, 0,
3995       gst_caps_features_new_any ());
3996   expected =
3997       gst_caps_from_string (GST_VIDEO_CAPS_MAKE_WITH_FEATURES ("ANY",
3998           GST_VIDEO_FORMATS_ALL));
3999   fail_unless (gst_caps_is_equal (caps, expected));
4000   gst_caps_unref (caps);
4001   gst_caps_unref (expected);
4002 }
4003
4004 GST_END_TEST;
4005
4006 static Suite *
4007 video_suite (void)
4008 {
4009   Suite *s = suite_create ("video support library");
4010   TCase *tc_chain = tcase_create ("general");
4011
4012   tcase_set_timeout (tc_chain, 60 * 60);
4013
4014   suite_add_tcase (s, tc_chain);
4015   tcase_add_test (tc_chain, test_video_formats);
4016   tcase_add_test (tc_chain, test_video_formats_overflow);
4017   tcase_add_test (tc_chain, test_video_formats_rgb);
4018   tcase_add_test (tc_chain, test_video_formats_rgba_large_dimension);
4019   tcase_add_test (tc_chain, test_video_formats_all);
4020   tcase_add_test (tc_chain, test_video_formats_pack_unpack);
4021   tcase_add_test (tc_chain, test_guess_framerate);
4022   tcase_add_test (tc_chain, test_dar_calc);
4023   tcase_add_test (tc_chain, test_parse_caps_rgb);
4024   tcase_add_test (tc_chain, test_parse_caps_multiview);
4025   tcase_add_test (tc_chain, test_parse_colorimetry);
4026   tcase_add_test (tc_chain, test_events);
4027   tcase_add_test (tc_chain, test_convert_frame);
4028   tcase_add_test (tc_chain, test_convert_frame_async);
4029   tcase_add_test (tc_chain, test_convert_frame_async_error);
4030   tcase_add_test (tc_chain, test_video_size_from_caps);
4031   tcase_add_test (tc_chain, test_interlace_mode);
4032   tcase_add_test (tc_chain, test_overlay_composition);
4033   tcase_add_test (tc_chain, test_overlay_composition_premultiplied_alpha);
4034   tcase_add_test (tc_chain, test_overlay_composition_global_alpha);
4035   tcase_add_test (tc_chain, test_video_pack_unpack2);
4036   tcase_add_test (tc_chain, test_video_chroma);
4037   tcase_add_test (tc_chain, test_video_chroma_site);
4038   tcase_add_test (tc_chain, test_video_scaler);
4039   tcase_add_test (tc_chain, test_video_color_convert_rgb_rgb);
4040   tcase_add_test (tc_chain, test_video_color_convert_rgb_yuv);
4041   tcase_add_test (tc_chain, test_video_color_convert_yuv_yuv);
4042   tcase_add_test (tc_chain, test_video_color_convert_yuv_rgb);
4043   tcase_add_test (tc_chain, test_video_color_convert_other);
4044   tcase_add_test (tc_chain, test_video_size_convert);
4045   tcase_add_test (tc_chain, test_video_convert);
4046   tcase_add_test (tc_chain, test_video_convert_multithreading);
4047   tcase_add_test (tc_chain, test_video_transfer);
4048   tcase_add_test (tc_chain, test_overlay_blend);
4049   tcase_add_test (tc_chain, test_video_center_rect);
4050   tcase_add_test (tc_chain, test_overlay_composition_over_transparency);
4051   tcase_add_test (tc_chain, test_video_format_enum_stability);
4052   tcase_add_test (tc_chain, test_video_formats_pstrides);
4053   tcase_add_test (tc_chain, test_hdr);
4054   tcase_add_test (tc_chain, test_video_color_from_to_iso);
4055   tcase_add_test (tc_chain, test_video_format_info_plane_to_components);
4056   tcase_add_test (tc_chain, test_video_info_align);
4057   tcase_add_test (tc_chain, test_video_meta_align);
4058   tcase_add_test (tc_chain, test_video_flags);
4059   tcase_add_test (tc_chain, test_video_make_raw_caps);
4060
4061   return s;
4062 }
4063
4064 GST_CHECK_MAIN (video);