2 * gstmsdkvpputil.c - MediaSDK video post processing utilities
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 * Copyright (C) 2018 Intel Corporation
8 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
9 * Author: Victor Jaquez <victorx.jaquez@intel.com>
10 * Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, write to the Free
24 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301 USA
28 #include "gstmsdkvpputil.h"
29 #include "msdk-enums.h"
32 gst_msdkvpp_is_deinterlace_enabled (GstMsdkVPP * msdkvpp, GstVideoInfo * vip)
36 switch (msdkvpp->deinterlace_mode) {
37 case GST_MSDKVPP_DEINTERLACE_MODE_AUTO:
38 deinterlace = GST_VIDEO_INFO_IS_INTERLACED (vip);
40 case GST_MSDKVPP_DEINTERLACE_MODE_INTERLACED:
51 fixate_output_frame_size (GstMsdkVPP * thiz, GstVideoInfo * vinfo,
55 GValue tpar = G_VALUE_INIT;
59 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
61 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
62 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
66 /* we have both PAR but they might not be fixated */
68 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
70 gint from_dar_n, from_dar_d;
73 from_par_n = GST_VIDEO_INFO_PAR_N (vinfo);
74 from_par_d = GST_VIDEO_INFO_PAR_D (vinfo);
75 from_w = GST_VIDEO_INFO_WIDTH (vinfo);
76 from_h = GST_VIDEO_INFO_HEIGHT (vinfo);
78 gst_structure_get_int (outs, "width", &w);
79 gst_structure_get_int (outs, "height", &h);
81 /* if both width and height are already fixed, we can't do anything
86 GST_DEBUG_OBJECT (thiz,
87 "dimensions already set to %dx%d, not fixating", w, h);
89 if (!gst_value_is_fixed (to_par)) {
90 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
91 from_par_n, from_par_d, w, h)) {
92 GST_DEBUG_OBJECT (thiz, "fixating to_par to %dx%d", n, d);
93 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
94 gst_structure_fixate_field_nearest_fraction (outs,
95 "pixel-aspect-ratio", n, d);
97 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
105 /* Calculate input DAR */
106 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
107 &from_dar_n, &from_dar_d))
110 GST_DEBUG_OBJECT (thiz, "Input DAR is %d/%d", from_dar_n, from_dar_d);
112 /* If either width or height are fixed there's not much we
113 * can do either except choosing a height or width and PAR
114 * that matches the DAR as good as possible
118 gint set_w, set_par_n, set_par_d;
120 GST_DEBUG_OBJECT (thiz, "height is fixed (%d)", h);
122 /* If the PAR is fixed too, there's not much to do
123 * except choosing the width that is nearest to the
124 * width with the same DAR */
125 if (gst_value_is_fixed (to_par)) {
126 to_par_n = gst_value_get_fraction_numerator (to_par);
127 to_par_d = gst_value_get_fraction_denominator (to_par);
129 GST_DEBUG_OBJECT (thiz, "PAR is fixed %d/%d", to_par_n, to_par_d);
131 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
132 to_par_n, &num, &den))
135 w = (guint) gst_util_uint64_scale_int (h, num, den);
136 gst_structure_fixate_field_nearest_int (outs, "width", w);
141 /* The PAR is not fixed and it's quite likely that we can set
142 * an arbitrary PAR. */
144 /* Check if we can keep the input width */
145 tmp = gst_structure_copy (outs);
146 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
147 gst_structure_get_int (tmp, "width", &set_w);
149 /* Might have failed but try to keep the DAR nonetheless by
150 * adjusting the PAR */
151 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
152 &to_par_n, &to_par_d)) {
153 gst_structure_free (tmp);
157 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
158 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
159 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
161 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
163 gst_structure_free (tmp);
165 /* Check if the adjusted PAR is accepted */
166 if (set_par_n == to_par_n && set_par_d == to_par_d) {
167 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
168 set_par_n != set_par_d)
169 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
170 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
175 /* Otherwise scale the width to the new PAR and check if the
176 * adjusted with is accepted. If all that fails we can't keep
178 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
179 set_par_n, &num, &den))
182 w = (guint) gst_util_uint64_scale_int (h, num, den);
183 gst_structure_fixate_field_nearest_int (outs, "width", w);
184 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
185 set_par_n != set_par_d)
186 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
187 set_par_n, set_par_d, NULL);
192 gint set_h, set_par_n, set_par_d;
194 GST_DEBUG_OBJECT (thiz, "width is fixed (%d)", w);
196 /* If the PAR is fixed too, there's not much to do
197 * except choosing the height that is nearest to the
198 * height with the same DAR */
199 if (gst_value_is_fixed (to_par)) {
200 to_par_n = gst_value_get_fraction_numerator (to_par);
201 to_par_d = gst_value_get_fraction_denominator (to_par);
203 GST_DEBUG_OBJECT (thiz, "PAR is fixed %d/%d", to_par_n, to_par_d);
205 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
206 to_par_n, &num, &den))
209 h = (guint) gst_util_uint64_scale_int (w, den, num);
210 gst_structure_fixate_field_nearest_int (outs, "height", h);
215 /* The PAR is not fixed and it's quite likely that we can set
216 * an arbitrary PAR. */
218 /* Check if we can keep the input height */
219 tmp = gst_structure_copy (outs);
220 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
221 gst_structure_get_int (tmp, "height", &set_h);
223 /* Might have failed but try to keep the DAR nonetheless by
224 * adjusting the PAR */
225 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
226 &to_par_n, &to_par_d)) {
227 gst_structure_free (tmp);
231 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
232 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
233 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
235 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
237 gst_structure_free (tmp);
239 /* Check if the adjusted PAR is accepted */
240 if (set_par_n == to_par_n && set_par_d == to_par_d) {
241 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
242 set_par_n != set_par_d)
243 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
244 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
249 /* Otherwise scale the height to the new PAR and check if the
250 * adjusted with is accepted. If all that fails we can't keep
252 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
253 set_par_n, &num, &den))
256 h = (guint) gst_util_uint64_scale_int (w, den, num);
257 gst_structure_fixate_field_nearest_int (outs, "height", h);
258 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
259 set_par_n != set_par_d)
260 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
261 set_par_n, set_par_d, NULL);
264 } else if (gst_value_is_fixed (to_par)) {
266 gint set_h, set_w, f_h, f_w;
268 to_par_n = gst_value_get_fraction_numerator (to_par);
269 to_par_d = gst_value_get_fraction_denominator (to_par);
271 /* Calculate scale factor for the PAR change */
272 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_n,
273 to_par_d, &num, &den))
276 /* Try to keep the input height (because of interlacing) */
277 tmp = gst_structure_copy (outs);
278 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
279 gst_structure_get_int (tmp, "height", &set_h);
281 /* This might have failed but try to scale the width
282 * to keep the DAR nonetheless */
283 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
284 gst_structure_fixate_field_nearest_int (tmp, "width", w);
285 gst_structure_get_int (tmp, "width", &set_w);
286 gst_structure_free (tmp);
288 /* We kept the DAR and the height is nearest to the original height */
290 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
291 G_TYPE_INT, set_h, NULL);
298 /* If the former failed, try to keep the input width at least */
299 tmp = gst_structure_copy (outs);
300 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
301 gst_structure_get_int (tmp, "width", &set_w);
303 /* This might have failed but try to scale the width
304 * to keep the DAR nonetheless */
305 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
306 gst_structure_fixate_field_nearest_int (tmp, "height", h);
307 gst_structure_get_int (tmp, "height", &set_h);
308 gst_structure_free (tmp);
310 /* We kept the DAR and the width is nearest to the original width */
312 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
313 G_TYPE_INT, set_h, NULL);
317 /* If all this failed, keep the height that was nearest to the orignal
318 * height and the nearest possible width. This changes the DAR but
319 * there's not much else to do here.
321 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
326 gint set_h, set_w, set_par_n, set_par_d, tmp2;
328 /* width, height and PAR are not fixed but passthrough is not possible */
330 /* First try to keep the height and width as good as possible
332 tmp = gst_structure_copy (outs);
333 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
334 gst_structure_get_int (tmp, "height", &set_h);
335 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
336 gst_structure_get_int (tmp, "width", &set_w);
338 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
339 &to_par_n, &to_par_d)) {
340 gst_structure_free (tmp);
344 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
345 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
346 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
348 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
350 gst_structure_free (tmp);
352 if (set_par_n == to_par_n && set_par_d == to_par_d) {
353 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
354 G_TYPE_INT, set_h, NULL);
356 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
357 set_par_n != set_par_d)
358 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
359 set_par_n, set_par_d, NULL);
363 /* Otherwise try to scale width to keep the DAR with the set
365 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
366 set_par_n, &num, &den))
369 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
370 tmp = gst_structure_copy (outs);
371 gst_structure_fixate_field_nearest_int (tmp, "width", w);
372 gst_structure_get_int (tmp, "width", &tmp2);
373 gst_structure_free (tmp);
376 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
377 G_TYPE_INT, set_h, NULL);
378 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
379 set_par_n != set_par_d)
380 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
381 set_par_n, set_par_d, NULL);
385 /* ... or try the same with the height */
386 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
387 tmp = gst_structure_copy (outs);
388 gst_structure_fixate_field_nearest_int (tmp, "height", h);
389 gst_structure_get_int (tmp, "height", &tmp2);
390 gst_structure_free (tmp);
393 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
394 G_TYPE_INT, tmp2, NULL);
395 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
396 set_par_n != set_par_d)
397 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
398 set_par_n, set_par_d, NULL);
402 /* If all fails we can't keep the DAR and take the nearest values
403 * for everything from the first try */
404 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
405 G_TYPE_INT, set_h, NULL);
406 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
407 set_par_n != set_par_d)
408 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
409 set_par_n, set_par_d, NULL);
415 g_value_unset (&tpar);
423 GST_ELEMENT_ERROR (thiz, CORE, NEGOTIATION, (NULL),
424 ("Error calculating the output scaled size - integer overflow"));
430 fixate_frame_rate (GstMsdkVPP * thiz, GstVideoInfo * vinfo, GstStructure * outs)
434 fps_n = GST_VIDEO_INFO_FPS_N (vinfo);
435 fps_d = GST_VIDEO_INFO_FPS_D (vinfo);
436 if (gst_msdkvpp_is_deinterlace_enabled (thiz, vinfo)) {
437 /* Fixme: set double framerate?:
438 * msdk is not outputting double framerate for bob or adv deinterlace */
439 if (!gst_util_fraction_multiply (fps_n, fps_d, 1, 1, &fps_n, &fps_d))
442 gst_structure_set (outs, "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
448 GST_ELEMENT_ERROR (thiz, CORE, NEGOTIATION, (NULL),
449 ("Error calculating the output framerate - integer overflow"));
455 set_multiview_mode (GstMsdkVPP * thiz, GstVideoInfo * vinfo,
458 const gchar *caps_str;
461 gst_video_multiview_mode_to_caps_string (GST_VIDEO_INFO_MULTIVIEW_MODE
466 gst_structure_set (outs, "multiview-mode", G_TYPE_STRING, caps_str,
467 "multiview-flags", GST_TYPE_VIDEO_MULTIVIEW_FLAGSET,
468 GST_VIDEO_INFO_MULTIVIEW_FLAGS (vinfo), GST_FLAG_SET_MASK_EXACT, NULL);
470 if (GST_VIDEO_INFO_VIEWS (vinfo) > 1) {
471 gst_structure_set (outs, "views", G_TYPE_INT, GST_VIDEO_INFO_VIEWS (vinfo),
478 set_interlace_mode (GstMsdkVPP * thiz, GstVideoInfo * vinfo,
481 const gchar *interlace_mode = NULL;
483 if (gst_msdkvpp_is_deinterlace_enabled (thiz, vinfo)) {
484 interlace_mode = "progressive";
487 gst_video_interlace_mode_to_string (GST_VIDEO_INFO_INTERLACE_MODE
494 gst_structure_set (outs, "interlace-mode", G_TYPE_STRING, interlace_mode,
500 _get_preferred_src_caps (GstMsdkVPP * thiz, GstVideoInfo * vinfo,
503 GstStructure *structure;
506 structure = gst_caps_get_structure (srccaps, 0);
509 structure = gst_structure_copy (structure);
511 if (thiz->keep_aspect)
512 gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1,
515 /* Fixate the format */
516 if (!gst_structure_fixate_field (structure, "format"))
519 /* Fixate the frame size */
520 if (!fixate_output_frame_size (thiz, vinfo, structure))
523 /* Fixate the framerate */
524 if (!fixate_frame_rate (thiz, vinfo, structure))
527 /* set multiview mode based on input caps */
528 if (!set_multiview_mode (thiz, vinfo, structure))
531 /*Fixme: Set colorimetry */
533 /* set interlace mode */
534 if (!set_interlace_mode (thiz, vinfo, structure))
535 goto interlace_mode_failed;
537 outcaps = gst_caps_new_empty ();
538 gst_caps_append_structure (outcaps, structure);
545 GST_WARNING_OBJECT (thiz, "Could not fixate src caps");
546 gst_structure_free (structure);
549 interlace_mode_failed:
551 GST_WARNING_OBJECT (thiz, "Invalid sink caps interlace mode");
557 * gst_msdkvpp_fixate_srccaps:
558 * @vpp: a #GstMsdkVPP instance
559 * @sinkcaps: fixed #GstCaps from sink pad
560 * @srccaps: #GstCaps from src pad to fixate
562 * Given @srccaps and @sinkcaps returns a new allocated #GstCaps with
563 * the fixated caps for the src pad.
565 * Returns: A new allocated #GstCaps
568 gst_msdkvpp_fixate_srccaps (GstMsdkVPP * msdkvpp,
569 GstCaps * sinkcaps, GstCaps * srccaps)
572 if (!gst_video_info_from_caps (&vi, sinkcaps))
574 return _get_preferred_src_caps (msdkvpp, &vi, srccaps);