Merge branch 'move_subdir_editing-services' into tizen_gst_1.19.2_mono
[platform/upstream/gstreamer.git] / subprojects / gstreamer-vaapi / gst / vaapi / gstvaapipostprocutil.c
1 /*
2  *  gstvaapipostprocutil.h - VA-API video post processing utilities
3  *
4  *  Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
5  *  Copyright (C) 2005-2012 David Schleef <ds@schleef.org>
6  *  Copyright (C) 2016 Intel Corporation
7  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
8  *    Author: Victor Jaquez <victorx.jaquez@intel.com>
9  *
10  *  This program is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public License
12  *  as published by the Free Software Foundation; either version 2.1
13  *  of the License, or (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this program; if not, write to the Free
22  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  *  Boston, MA 02110-1301 USA
24  */
25
26 #include <gst/vaapi/gstvaapifilter.h>
27
28 #include "gstvaapipostprocutil.h"
29 #include "gstvaapipluginutil.h"
30
31 #define GST_CAT_DEFAULT (GST_VAAPI_PLUGIN_BASE (postproc)->debug_category)
32
33 /* if format property is set */
34 static void
35 _transform_format (GstVaapiPostproc * postproc, GstCapsFeatures * features,
36     GstStructure * structure)
37 {
38   GValue value = G_VALUE_INIT;
39
40   if (postproc->format == DEFAULT_FORMAT)
41     return;
42
43   if (!gst_caps_features_is_equal (features,
44           GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY)
45       && !gst_caps_features_contains (features,
46           GST_CAPS_FEATURE_MEMORY_VAAPI_SURFACE))
47     return;
48
49   if (!gst_vaapi_value_set_format (&value, postproc->format))
50     return;
51
52   gst_structure_set_value (structure, "format", &value);
53   g_value_unset (&value);
54 }
55
56 static void
57 _set_int (GValue * value, gint val)
58 {
59   g_value_init (value, G_TYPE_INT);
60   g_value_set_int (value, val);
61 }
62
63 static void
64 _set_int_range (GValue * value)
65 {
66   g_value_init (value, GST_TYPE_INT_RANGE);
67   gst_value_set_int_range (value, 1, G_MAXINT);
68 }
69
70 static void
71 _transform_frame_size (GstVaapiPostproc * postproc, GstStructure * structure)
72 {
73   GValue width = G_VALUE_INIT;
74   GValue height = G_VALUE_INIT;
75
76   if (postproc->width && postproc->height) {
77     _set_int (&width, postproc->width);
78     _set_int (&height, postproc->height);
79   } else if (postproc->width) {
80     _set_int (&width, postproc->width);
81     _set_int_range (&height);
82   } else if (postproc->height) {
83     _set_int_range (&width);
84     _set_int (&height, postproc->height);
85   } else {
86     _set_int_range (&width);
87     _set_int_range (&height);
88   }
89
90   gst_structure_set_value (structure, "width", &width);
91   gst_structure_set_value (structure, "height", &height);
92 }
93
94 /**
95  * gst_vaapipostproc_transform_srccaps:
96  * @postproc: a #GstVaapiPostproc instance
97  *
98  * Early apply transformation of the src pad caps according to the set
99  * properties.
100  *
101  * Returns: A new allocated #GstCaps
102  **/
103 GstCaps *
104 gst_vaapipostproc_transform_srccaps (GstVaapiPostproc * postproc)
105 {
106   GstCaps *out_caps;
107   GstStructure *structure;
108   GstCapsFeatures *features;
109   gint i, n;
110
111   out_caps = gst_caps_new_empty ();
112   n = gst_caps_get_size (postproc->allowed_srcpad_caps);
113
114   for (i = 0; i < n; i++) {
115     structure = gst_caps_get_structure (postproc->allowed_srcpad_caps, i);
116     features = gst_caps_get_features (postproc->allowed_srcpad_caps, i);
117
118     /* make copy */
119     structure = gst_structure_copy (structure);
120
121     if (postproc->keep_aspect)
122       gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1,
123           1, NULL);
124
125     _transform_format (postproc, features, structure);
126     _transform_frame_size (postproc, structure);
127
128     gst_caps_append_structure_full (out_caps, structure,
129         gst_caps_features_copy (features));
130   }
131
132   return out_caps;
133 }
134
135 gboolean
136 is_deinterlace_enabled (GstVaapiPostproc * postproc, GstVideoInfo * vip)
137 {
138   gboolean deinterlace;
139
140   switch (postproc->deinterlace_mode) {
141     case GST_VAAPI_DEINTERLACE_MODE_AUTO:
142       deinterlace = GST_VIDEO_INFO_IS_INTERLACED (vip);
143       break;
144     case GST_VAAPI_DEINTERLACE_MODE_INTERLACED:
145       deinterlace = TRUE;
146       break;
147     default:
148       deinterlace = FALSE;
149       break;
150   }
151   return deinterlace;
152 }
153
154 static gboolean
155 _fixate_frame_size (GstVaapiPostproc * postproc, GstVideoInfo * vinfo,
156     GstStructure * outs)
157 {
158   const GValue *to_par;
159   GValue tpar = G_VALUE_INIT;
160   gboolean ret;
161
162   ret = TRUE;
163   to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
164   if (!to_par) {
165     g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
166     gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
167     to_par = &tpar;
168   }
169
170   /* we have both PAR but they might not be fixated */
171   {
172     gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
173     gint w = 0, h = 0;
174     gint from_dar_n, from_dar_d;
175     gint num, den;
176
177     from_par_n = GST_VIDEO_INFO_PAR_N (vinfo);
178     from_par_d = GST_VIDEO_INFO_PAR_D (vinfo);
179     from_w = GST_VIDEO_INFO_WIDTH (vinfo);
180     from_h = GST_VIDEO_INFO_HEIGHT (vinfo);
181
182     if (postproc->has_vpp) {
183       /* adjust for crop settings */
184       from_w -= postproc->crop_left + postproc->crop_right;
185       from_h -= postproc->crop_top + postproc->crop_bottom;
186
187       /* compensate for rotation if needed */
188       switch (gst_vaapi_filter_get_video_direction (postproc->filter)) {
189         case GST_VIDEO_ORIENTATION_90R:
190         case GST_VIDEO_ORIENTATION_90L:
191         case GST_VIDEO_ORIENTATION_UL_LR:
192         case GST_VIDEO_ORIENTATION_UR_LL:
193           G_PRIMITIVE_SWAP (gint, from_w, from_h);
194           G_PRIMITIVE_SWAP (gint, from_par_n, from_par_d);
195         default:
196           break;
197       }
198     }
199
200     gst_structure_get_int (outs, "width", &w);
201     gst_structure_get_int (outs, "height", &h);
202
203     /* if both width and height are already fixed, we can't do anything
204      * about it anymore */
205     if (w && h) {
206       guint n, d;
207
208       GST_DEBUG_OBJECT (postproc,
209           "dimensions already set to %dx%d, not fixating", w, h);
210
211       if (!gst_value_is_fixed (to_par)) {
212         if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
213                 from_par_n, from_par_d, w, h)) {
214           GST_DEBUG_OBJECT (postproc, "fixating to_par to %dx%d", n, d);
215           if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
216             gst_structure_fixate_field_nearest_fraction (outs,
217                 "pixel-aspect-ratio", n, d);
218           else if (n != d)
219             gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
220                 n, d, NULL);
221         }
222       }
223
224       goto done;
225     }
226
227     /* Calculate input DAR */
228     if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
229             &from_dar_n, &from_dar_d))
230       goto overflow_error;
231
232     GST_DEBUG_OBJECT (postproc, "Input DAR is %d/%d", from_dar_n, from_dar_d);
233
234     /* If either width or height are fixed there's not much we
235      * can do either except choosing a height or width and PAR
236      * that matches the DAR as good as possible
237      */
238     if (h) {
239       GstStructure *tmp;
240       gint set_w, set_par_n, set_par_d;
241
242       GST_DEBUG_OBJECT (postproc, "height is fixed (%d)", h);
243
244       /* If the PAR is fixed too, there's not much to do
245        * except choosing the width that is nearest to the
246        * width with the same DAR */
247       if (gst_value_is_fixed (to_par)) {
248         to_par_n = gst_value_get_fraction_numerator (to_par);
249         to_par_d = gst_value_get_fraction_denominator (to_par);
250
251         GST_DEBUG_OBJECT (postproc, "PAR is fixed %d/%d", to_par_n, to_par_d);
252
253         if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
254                 to_par_n, &num, &den))
255           goto overflow_error;
256
257         w = (guint) gst_util_uint64_scale_int (h, num, den);
258         gst_structure_fixate_field_nearest_int (outs, "width", w);
259
260         goto done;
261       }
262
263       /* The PAR is not fixed and it's quite likely that we can set
264        * an arbitrary PAR. */
265
266       /* Check if we can keep the input width */
267       tmp = gst_structure_copy (outs);
268       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
269       gst_structure_get_int (tmp, "width", &set_w);
270
271       /* Might have failed but try to keep the DAR nonetheless by
272        * adjusting the PAR */
273       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
274               &to_par_n, &to_par_d)) {
275         gst_structure_free (tmp);
276         goto overflow_error;
277       }
278
279       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
280         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
281       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
282           to_par_n, to_par_d);
283       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
284           &set_par_d);
285       gst_structure_free (tmp);
286
287       /* Check if the adjusted PAR is accepted */
288       if (set_par_n == to_par_n && set_par_d == to_par_d) {
289         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
290             set_par_n != set_par_d)
291           gst_structure_set (outs, "width", G_TYPE_INT, set_w,
292               "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
293               NULL);
294         goto done;
295       }
296
297       /* Otherwise scale the width to the new PAR and check if the
298        * adjusted with is accepted. If all that fails we can't keep
299        * the DAR */
300       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
301               set_par_n, &num, &den))
302         goto overflow_error;
303
304       w = (guint) gst_util_uint64_scale_int (h, num, den);
305       gst_structure_fixate_field_nearest_int (outs, "width", w);
306       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
307           set_par_n != set_par_d)
308         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
309             set_par_n, set_par_d, NULL);
310
311       goto done;
312     } else if (w) {
313       GstStructure *tmp;
314       gint set_h, set_par_n, set_par_d;
315
316       GST_DEBUG_OBJECT (postproc, "width is fixed (%d)", w);
317
318       /* If the PAR is fixed too, there's not much to do
319        * except choosing the height that is nearest to the
320        * height with the same DAR */
321       if (gst_value_is_fixed (to_par)) {
322         to_par_n = gst_value_get_fraction_numerator (to_par);
323         to_par_d = gst_value_get_fraction_denominator (to_par);
324
325         GST_DEBUG_OBJECT (postproc, "PAR is fixed %d/%d", to_par_n, to_par_d);
326
327         if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
328                 to_par_n, &num, &den))
329           goto overflow_error;
330
331         h = (guint) gst_util_uint64_scale_int (w, den, num);
332         gst_structure_fixate_field_nearest_int (outs, "height", h);
333
334         goto done;
335       }
336
337       /* The PAR is not fixed and it's quite likely that we can set
338        * an arbitrary PAR. */
339
340       /* Check if we can keep the input height */
341       tmp = gst_structure_copy (outs);
342       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
343       gst_structure_get_int (tmp, "height", &set_h);
344
345       /* Might have failed but try to keep the DAR nonetheless by
346        * adjusting the PAR */
347       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
348               &to_par_n, &to_par_d)) {
349         gst_structure_free (tmp);
350         goto overflow_error;
351       }
352
353       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
354         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
355       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
356           to_par_n, to_par_d);
357       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
358           &set_par_d);
359       gst_structure_free (tmp);
360
361       /* Check if the adjusted PAR is accepted */
362       if (set_par_n == to_par_n && set_par_d == to_par_d) {
363         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
364             set_par_n != set_par_d)
365           gst_structure_set (outs, "height", G_TYPE_INT, set_h,
366               "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
367               NULL);
368         goto done;
369       }
370
371       /* Otherwise scale the height to the new PAR and check if the
372        * adjusted with is accepted. If all that fails we can't keep
373        * the DAR */
374       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
375               set_par_n, &num, &den))
376         goto overflow_error;
377
378       h = (guint) gst_util_uint64_scale_int (w, den, num);
379       gst_structure_fixate_field_nearest_int (outs, "height", h);
380       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
381           set_par_n != set_par_d)
382         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
383             set_par_n, set_par_d, NULL);
384
385       goto done;
386     } else if (gst_value_is_fixed (to_par)) {
387       GstStructure *tmp;
388       gint set_h, set_w, f_h, f_w;
389
390       to_par_n = gst_value_get_fraction_numerator (to_par);
391       to_par_d = gst_value_get_fraction_denominator (to_par);
392
393       /* Calculate scale factor for the PAR change */
394       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
395               to_par_d, &num, &den))
396         goto overflow_error;
397
398       /* Try to keep the input height (because of interlacing) */
399       tmp = gst_structure_copy (outs);
400       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
401       gst_structure_get_int (tmp, "height", &set_h);
402
403       /* This might have failed but try to scale the width
404        * to keep the DAR nonetheless */
405       w = (guint) gst_util_uint64_scale_int (set_h, num, den);
406       gst_structure_fixate_field_nearest_int (tmp, "width", w);
407       gst_structure_get_int (tmp, "width", &set_w);
408       gst_structure_free (tmp);
409
410       /* We kept the DAR and the height is nearest to the original height */
411       if (set_w == w) {
412         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
413             G_TYPE_INT, set_h, NULL);
414         goto done;
415       }
416
417       f_h = set_h;
418       f_w = set_w;
419
420       /* If the former failed, try to keep the input width at least */
421       tmp = gst_structure_copy (outs);
422       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
423       gst_structure_get_int (tmp, "width", &set_w);
424
425       /* This might have failed but try to scale the width
426        * to keep the DAR nonetheless */
427       h = (guint) gst_util_uint64_scale_int (set_w, den, num);
428       gst_structure_fixate_field_nearest_int (tmp, "height", h);
429       gst_structure_get_int (tmp, "height", &set_h);
430       gst_structure_free (tmp);
431
432       /* We kept the DAR and the width is nearest to the original width */
433       if (set_h == h) {
434         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
435             G_TYPE_INT, set_h, NULL);
436         goto done;
437       }
438
439       /* If all this failed, keep the height that was nearest to the orignal
440        * height and the nearest possible width. This changes the DAR but
441        * there's not much else to do here.
442        */
443       gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
444           f_h, NULL);
445       goto done;
446     } else {
447       GstStructure *tmp;
448       gint set_h, set_w, set_par_n, set_par_d, tmp2;
449
450       /* width, height and PAR are not fixed but passthrough is not possible */
451
452       /* First try to keep the height and width as good as possible
453        * and scale PAR */
454       tmp = gst_structure_copy (outs);
455       gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
456       gst_structure_get_int (tmp, "height", &set_h);
457       gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
458       gst_structure_get_int (tmp, "width", &set_w);
459
460       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
461               &to_par_n, &to_par_d)) {
462         gst_structure_free (tmp);
463         goto overflow_error;
464       }
465
466       if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
467         gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
468       gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
469           to_par_n, to_par_d);
470       gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
471           &set_par_d);
472       gst_structure_free (tmp);
473
474       if (set_par_n == to_par_n && set_par_d == to_par_d) {
475         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
476             G_TYPE_INT, set_h, NULL);
477
478         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
479             set_par_n != set_par_d)
480           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
481               set_par_n, set_par_d, NULL);
482         goto done;
483       }
484
485       /* Otherwise try to scale width to keep the DAR with the set
486        * PAR and height */
487       if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
488               set_par_n, &num, &den))
489         goto overflow_error;
490
491       w = (guint) gst_util_uint64_scale_int (set_h, num, den);
492       tmp = gst_structure_copy (outs);
493       gst_structure_fixate_field_nearest_int (tmp, "width", w);
494       gst_structure_get_int (tmp, "width", &tmp2);
495       gst_structure_free (tmp);
496
497       if (tmp2 == w) {
498         gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
499             G_TYPE_INT, set_h, NULL);
500         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
501             set_par_n != set_par_d)
502           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
503               set_par_n, set_par_d, NULL);
504         goto done;
505       }
506
507       /* ... or try the same with the height */
508       h = (guint) gst_util_uint64_scale_int (set_w, den, num);
509       tmp = gst_structure_copy (outs);
510       gst_structure_fixate_field_nearest_int (tmp, "height", h);
511       gst_structure_get_int (tmp, "height", &tmp2);
512       gst_structure_free (tmp);
513
514       if (tmp2 == h) {
515         gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
516             G_TYPE_INT, tmp2, NULL);
517         if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
518             set_par_n != set_par_d)
519           gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
520               set_par_n, set_par_d, NULL);
521         goto done;
522       }
523
524       /* If all fails we can't keep the DAR and take the nearest values
525        * for everything from the first try */
526       gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
527           G_TYPE_INT, set_h, NULL);
528       if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
529           set_par_n != set_par_d)
530         gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
531             set_par_n, set_par_d, NULL);
532     }
533   }
534
535 done:
536   if (to_par == &tpar)
537     g_value_unset (&tpar);
538
539   return ret;
540
541   /* ERRORS */
542 overflow_error:
543   {
544     ret = FALSE;
545     GST_ELEMENT_ERROR (postproc, CORE, NEGOTIATION, (NULL),
546         ("Error calculating the output scaled size - integer overflow"));
547     goto done;
548   }
549 }
550
551 static gboolean
552 _fixate_frame_rate (GstVaapiPostproc * postproc, GstVideoInfo * vinfo,
553     GstStructure * outs)
554 {
555   gint fps_n, fps_d;
556
557   fps_n = GST_VIDEO_INFO_FPS_N (vinfo);
558   fps_d = GST_VIDEO_INFO_FPS_D (vinfo);
559   if (is_deinterlace_enabled (postproc, vinfo)) {
560     if (!gst_util_fraction_multiply (fps_n, fps_d, 2, 1, &fps_n, &fps_d))
561       goto overflow_error;
562   }
563   gst_structure_set (outs, "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
564   return TRUE;
565
566   /* ERRORS */
567 overflow_error:
568   {
569     GST_ELEMENT_ERROR (postproc, CORE, NEGOTIATION, (NULL),
570         ("Error calculating the output framerate - integer overflow"));
571     return FALSE;
572   }
573 }
574
575 static gboolean
576 _set_multiview_mode (GstVaapiPostproc * postproc, GstVideoInfo * vinfo,
577     GstStructure * outs)
578 {
579   const gchar *caps_str;
580
581   caps_str =
582       gst_video_multiview_mode_to_caps_string (GST_VIDEO_INFO_MULTIVIEW_MODE
583       (vinfo));
584   if (!caps_str)
585     return TRUE;
586
587   gst_structure_set (outs, "multiview-mode", G_TYPE_STRING, caps_str,
588       "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET,
589       GST_VIDEO_INFO_MULTIVIEW_FLAGS (vinfo), GST_FLAG_SET_MASK_EXACT, NULL);
590
591   if (GST_VIDEO_INFO_VIEWS (vinfo) > 1) {
592     gst_structure_set (outs, "views", G_TYPE_INT, GST_VIDEO_INFO_VIEWS (vinfo),
593         NULL);
594   }
595
596   return TRUE;
597 }
598
599 static gboolean
600 _set_colorimetry (GstVaapiPostproc * postproc, GstVideoInfo * sinkinfo,
601     GstVideoFormat format, GstStructure * outs)
602 {
603   GstVideoInfo vinfo;
604   GstVideoColorimetry colorimetry;
605   gchar *color;
606   gint width, height;
607
608   if (!gst_structure_get_int (outs, "width", &width)
609       || !gst_structure_get_int (outs, "height", &height))
610     return FALSE;
611
612   /* Use the sink resolution and the src format to correctly determine the
613    * default src colorimetry. */
614   gst_video_info_set_format (&vinfo, format, GST_VIDEO_INFO_WIDTH (sinkinfo),
615       GST_VIDEO_INFO_HEIGHT (sinkinfo));
616
617   if (GST_VIDEO_INFO_CHROMA_SITE (&vinfo) != GST_VIDEO_CHROMA_SITE_UNKNOWN) {
618     gst_structure_set (outs, "chroma-site", G_TYPE_STRING,
619         gst_video_chroma_to_string (GST_VIDEO_INFO_CHROMA_SITE (&vinfo)), NULL);
620   }
621
622   /* if outs structure already specifies colorimetry, use it */
623   if (gst_structure_has_field (outs, "colorimetry"))
624     return TRUE;
625
626   /* make sure we set the RGB matrix for RGB formats */
627   colorimetry = GST_VIDEO_INFO_COLORIMETRY (&vinfo);
628   if (GST_VIDEO_FORMAT_INFO_IS_RGB (vinfo.finfo) &&
629       colorimetry.matrix != GST_VIDEO_COLOR_MATRIX_RGB) {
630     GST_WARNING ("invalid matrix %d for RGB format, using RGB",
631         colorimetry.matrix);
632     colorimetry.matrix = GST_VIDEO_COLOR_MATRIX_RGB;
633   }
634
635   if ((color = gst_video_colorimetry_to_string (&colorimetry))) {
636     gst_structure_set (outs, "colorimetry", G_TYPE_STRING, color, NULL);
637     g_free (color);
638   }
639
640   return TRUE;
641 }
642
643 static gboolean
644 _set_interlace_mode (GstVaapiPostproc * postproc, GstVideoInfo * vinfo,
645     GstStructure * outs)
646 {
647   const gchar *interlace_mode = NULL;
648
649   if (is_deinterlace_enabled (postproc, vinfo)) {
650     interlace_mode = "progressive";
651   } else {
652     interlace_mode =
653         gst_video_interlace_mode_to_string (GST_VIDEO_INFO_INTERLACE_MODE
654         (vinfo));
655   }
656
657   if (!interlace_mode)
658     return FALSE;
659
660   gst_structure_set (outs, "interlace-mode", G_TYPE_STRING, interlace_mode,
661       NULL);
662   return TRUE;
663 }
664
665 static gboolean
666 _set_preferred_format (GstStructure * outs, GstVideoFormat format)
667 {
668   GValue value = G_VALUE_INIT;
669
670   if (format == GST_VIDEO_FORMAT_UNKNOWN || format == GST_VIDEO_FORMAT_ENCODED)
671     return FALSE;
672
673   if (!gst_vaapi_value_set_format (&value, format))
674     return FALSE;
675   gst_structure_set_value (outs, "format", &value);
676   g_value_unset (&value);
677   return TRUE;
678 }
679
680 static GstCaps *
681 _get_preferred_caps (GstVaapiPostproc * postproc, GstVideoInfo * vinfo,
682     GstCaps * srccaps)
683 {
684   GstPad *srcpad;
685   GstVideoFormat format;
686   GstVaapiCapsFeature f;
687   const gchar *feature;
688   GstStructure *structure;
689   GstCapsFeatures *features;
690   GstCaps *outcaps;
691   gint i, n;
692
693   format = GST_VIDEO_FORMAT_UNKNOWN;
694   srcpad = GST_BASE_TRANSFORM_SRC_PAD (postproc);
695   f = gst_vaapi_find_preferred_caps_feature (srcpad, srccaps, &format);
696   if (f == GST_VAAPI_CAPS_FEATURE_NOT_NEGOTIATED)
697     return NULL;
698
699   feature = gst_vaapi_caps_feature_to_string (f);
700   if (!feature)
701     feature = GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY;
702
703   n = gst_caps_get_size (srccaps);
704   for (i = 0; i < n; i++) {
705     structure = gst_caps_get_structure (srccaps, i);
706     features = gst_caps_get_features (srccaps, i);
707
708     if (!gst_caps_features_is_any (features)
709         && gst_caps_features_contains (features, feature))
710       break;
711   }
712
713   if (i >= n)
714     goto invalid_caps;
715
716   /* make copy */
717   structure = gst_structure_copy (structure);
718
719   if (!_set_preferred_format (structure, format))
720     goto fixate_failed;
721   if (!_fixate_frame_size (postproc, vinfo, structure))
722     goto fixate_failed;
723   if (!_fixate_frame_rate (postproc, vinfo, structure))
724     goto fixate_failed;
725   _set_multiview_mode (postproc, vinfo, structure);
726
727   if (!_set_colorimetry (postproc, vinfo, format, structure))
728     goto fixate_failed;
729
730   if (!_set_interlace_mode (postproc, vinfo, structure))
731     goto interlace_mode_failed;
732
733   outcaps = gst_caps_new_empty ();
734   gst_caps_append_structure_full (outcaps, structure,
735       gst_caps_features_copy (features));
736
737   /* we don't need to do format conversion if GL_TEXTURE_UPLOAD_META
738    * is negotiated */
739   if (f == GST_VAAPI_CAPS_FEATURE_GL_TEXTURE_UPLOAD_META) {
740     postproc->format = DEFAULT_FORMAT;
741   } else if (postproc->format != format) {
742     postproc->format = format;
743   }
744
745   return gst_caps_fixate (outcaps);
746
747   /* ERRORS */
748 fixate_failed:
749   {
750     GST_WARNING_OBJECT (postproc, "Could not fixate src caps");
751     gst_structure_free (structure);
752     return NULL;
753   }
754 invalid_caps:
755   {
756     GST_WARNING_OBJECT (postproc, "No valid src caps found");
757     return NULL;
758   }
759 interlace_mode_failed:
760   {
761     GST_WARNING_OBJECT (postproc, "Invalid sink caps interlace mode");
762     return NULL;
763   }
764 }
765
766 /**
767  * gst_vaapipostproc_fixate_srccaps:
768  * @postproc: a #GstVaapiPostproc instance
769  * @sinkcaps: fixed #GstCaps from sink pad
770  * @srccaps: #GstCaps from src pad to fixate
771  *
772  * Given @srccaps and @sinkcaps returns a new allocated #GstCaps with
773  * the fixated caps for the src pad.
774  *
775  * Returns: A new allocated #GstCaps
776  **/
777 GstCaps *
778 gst_vaapipostproc_fixate_srccaps (GstVaapiPostproc * postproc,
779     GstCaps * sinkcaps, GstCaps * srccaps)
780 {
781   GstVideoInfo vi;
782
783   if (!gst_video_info_from_caps (&vi, sinkcaps))
784     return NULL;
785   return _get_preferred_caps (postproc, &vi, srccaps);
786 }