cuda: Add convertscale element
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / nvcodec / gstcudaconvert.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2005-2012 David Schleef <ds@schleef.org>
4  * Copyright (C) <2019> Seungha Yang <seungha.yang@navercorp.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:element-cudaconvert
24  * @title: cudaconvert
25  *
26  * Convert video frames between supported video formats.
27  *
28  * ## Example launch line
29  * |[
30  * gst-launch-1.0 -v videotestsrc ! video/x-raw,format=Y444_16LE ! cudaupload ! cudaconvert ! cudadownload ! autovideosink
31  * ]|
32  *  This will output a test video (generated in Y444_16LE format) in a video
33  * window. If the video sink selected does not support Y444_16LE
34  * cudaconvert will automatically convert the video to a format understood
35  * by the video sink.
36  *
37  * Since: 1.20
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #  include <config.h>
42 #endif
43
44 #include <gst/cuda/gstcudautils.h>
45
46 #include "gstcudaconvert.h"
47
48 GST_DEBUG_CATEGORY_STATIC (gst_cuda_convert_debug);
49 #define GST_CAT_DEFAULT gst_cuda_convert_debug
50
51 #define gst_cuda_convert_parent_class parent_class
52 G_DEFINE_TYPE (GstCudaConvert, gst_cuda_convert, GST_TYPE_CUDA_BASE_FILTER);
53
54 static GstCaps *gst_cuda_convert_transform_caps (GstBaseTransform * trans,
55     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
56 static GstCaps *gst_cuda_convert_fixate_caps (GstBaseTransform * base,
57     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
58 static gboolean gst_cuda_convert_filter_meta (GstBaseTransform * trans,
59     GstQuery * query, GType api, const GstStructure * params);
60 static gboolean
61 gst_cuda_convert_set_info (GstCudaBaseTransform * btrans, GstCaps * incaps,
62     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info);
63
64 /* copies the given caps */
65 static GstCaps *
66 gst_cuda_convert_caps_remove_format_info (GstCaps * caps)
67 {
68   GstStructure *st;
69   GstCapsFeatures *f;
70   gint i, n;
71   GstCaps *res;
72   GstCapsFeatures *feature =
73       gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY);
74
75   res = gst_caps_new_empty ();
76
77   n = gst_caps_get_size (caps);
78   for (i = 0; i < n; i++) {
79     st = gst_caps_get_structure (caps, i);
80     f = gst_caps_get_features (caps, i);
81
82     /* If this is already expressed by the existing caps
83      * skip this structure */
84     if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
85       continue;
86
87     st = gst_structure_copy (st);
88     /* Only remove format info for the cases when we can actually convert */
89     if (!gst_caps_features_is_any (f)
90         && gst_caps_features_is_equal (f, feature))
91       gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
92           NULL);
93
94     gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
95   }
96   gst_caps_features_free (feature);
97
98   return res;
99 }
100
101 /*
102  * This is an incomplete matrix of in formats and a score for the prefered output
103  * format.
104  *
105  *         out: RGB24   RGB16  ARGB  AYUV  YUV444  YUV422 YUV420 YUV411 YUV410  PAL  GRAY
106  *  in
107  * RGB24          0      2       1     2     2       3      4      5      6      7    8
108  * RGB16          1      0       1     2     2       3      4      5      6      7    8
109  * ARGB           2      3       0     1     4       5      6      7      8      9    10
110  * AYUV           3      4       1     0     2       5      6      7      8      9    10
111  * YUV444         2      4       3     1     0       5      6      7      8      9    10
112  * YUV422         3      5       4     2     1       0      6      7      8      9    10
113  * YUV420         4      6       5     3     2       1      0      7      8      9    10
114  * YUV411         4      6       5     3     2       1      7      0      8      9    10
115  * YUV410         6      8       7     5     4       3      2      1      0      9    10
116  * PAL            1      3       2     6     4       6      7      8      9      0    10
117  * GRAY           1      4       3     2     1       5      6      7      8      9    0
118  *
119  * PAL or GRAY are never preferred, if we can we would convert to PAL instead
120  * of GRAY, though
121  * less subsampling is preferred and if any, preferably horizontal
122  * We would like to keep the alpha, even if we would need to to colorspace conversion
123  * or lose depth.
124  */
125 #define SCORE_FORMAT_CHANGE       1
126 #define SCORE_DEPTH_CHANGE        1
127 #define SCORE_ALPHA_CHANGE        1
128 #define SCORE_CHROMA_W_CHANGE     1
129 #define SCORE_CHROMA_H_CHANGE     1
130 #define SCORE_PALETTE_CHANGE      1
131
132 #define SCORE_COLORSPACE_LOSS     2     /* RGB <-> YUV */
133 #define SCORE_DEPTH_LOSS          4     /* change bit depth */
134 #define SCORE_ALPHA_LOSS          8     /* lose the alpha channel */
135 #define SCORE_CHROMA_W_LOSS      16     /* vertical subsample */
136 #define SCORE_CHROMA_H_LOSS      32     /* horizontal subsample */
137 #define SCORE_PALETTE_LOSS       64     /* convert to palette format */
138 #define SCORE_COLOR_LOSS        128     /* convert to GRAY */
139
140 #define COLORSPACE_MASK (GST_VIDEO_FORMAT_FLAG_YUV | \
141                          GST_VIDEO_FORMAT_FLAG_RGB | GST_VIDEO_FORMAT_FLAG_GRAY)
142 #define ALPHA_MASK      (GST_VIDEO_FORMAT_FLAG_ALPHA)
143 #define PALETTE_MASK    (GST_VIDEO_FORMAT_FLAG_PALETTE)
144
145 /* calculate how much loss a conversion would be */
146 static void
147 score_value (GstBaseTransform * base, const GstVideoFormatInfo * in_info,
148     const GValue * val, gint * min_loss, const GstVideoFormatInfo ** out_info)
149 {
150   const gchar *fname;
151   const GstVideoFormatInfo *t_info;
152   GstVideoFormatFlags in_flags, t_flags;
153   gint loss;
154
155   fname = g_value_get_string (val);
156   t_info = gst_video_format_get_info (gst_video_format_from_string (fname));
157   if (!t_info)
158     return;
159
160   /* accept input format immediately without loss */
161   if (in_info == t_info) {
162     *min_loss = 0;
163     *out_info = t_info;
164     return;
165   }
166
167   loss = SCORE_FORMAT_CHANGE;
168
169   in_flags = GST_VIDEO_FORMAT_INFO_FLAGS (in_info);
170   in_flags &= ~GST_VIDEO_FORMAT_FLAG_LE;
171   in_flags &= ~GST_VIDEO_FORMAT_FLAG_COMPLEX;
172   in_flags &= ~GST_VIDEO_FORMAT_FLAG_UNPACK;
173
174   t_flags = GST_VIDEO_FORMAT_INFO_FLAGS (t_info);
175   t_flags &= ~GST_VIDEO_FORMAT_FLAG_LE;
176   t_flags &= ~GST_VIDEO_FORMAT_FLAG_COMPLEX;
177   t_flags &= ~GST_VIDEO_FORMAT_FLAG_UNPACK;
178
179   if ((t_flags & PALETTE_MASK) != (in_flags & PALETTE_MASK)) {
180     loss += SCORE_PALETTE_CHANGE;
181     if (t_flags & PALETTE_MASK)
182       loss += SCORE_PALETTE_LOSS;
183   }
184
185   if ((t_flags & COLORSPACE_MASK) != (in_flags & COLORSPACE_MASK)) {
186     loss += SCORE_COLORSPACE_LOSS;
187     if (t_flags & GST_VIDEO_FORMAT_FLAG_GRAY)
188       loss += SCORE_COLOR_LOSS;
189   }
190
191   if ((t_flags & ALPHA_MASK) != (in_flags & ALPHA_MASK)) {
192     loss += SCORE_ALPHA_CHANGE;
193     if (in_flags & ALPHA_MASK)
194       loss += SCORE_ALPHA_LOSS;
195   }
196
197   if ((in_info->h_sub[1]) != (t_info->h_sub[1])) {
198     loss += SCORE_CHROMA_H_CHANGE;
199     if ((in_info->h_sub[1]) < (t_info->h_sub[1]))
200       loss += SCORE_CHROMA_H_LOSS;
201   }
202   if ((in_info->w_sub[1]) != (t_info->w_sub[1])) {
203     loss += SCORE_CHROMA_W_CHANGE;
204     if ((in_info->w_sub[1]) < (t_info->w_sub[1]))
205       loss += SCORE_CHROMA_W_LOSS;
206   }
207
208   if ((in_info->bits) != (t_info->bits)) {
209     loss += SCORE_DEPTH_CHANGE;
210     if ((in_info->bits) > (t_info->bits))
211       loss += SCORE_DEPTH_LOSS;
212   }
213
214   GST_DEBUG_OBJECT (base, "score %s -> %s = %d",
215       GST_VIDEO_FORMAT_INFO_NAME (in_info),
216       GST_VIDEO_FORMAT_INFO_NAME (t_info), loss);
217
218   if (loss < *min_loss) {
219     GST_DEBUG_OBJECT (base, "found new best %d", loss);
220     *out_info = t_info;
221     *min_loss = loss;
222   }
223 }
224
225 static void
226 gst_cuda_convert_class_init (GstCudaConvertClass * klass)
227 {
228   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
229   GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
230   GstCudaBaseTransformClass *btrans_class =
231       GST_CUDA_BASE_TRANSFORM_CLASS (klass);
232
233   gst_element_class_set_static_metadata (element_class,
234       "CUDA Colorspace converter",
235       "Filter/Converter/Video/Hardware",
236       "Converts video from one colorspace to another using CUDA",
237       "Seungha Yang <seungha.yang@navercorp.com>");
238
239   trans_class->passthrough_on_same_caps = TRUE;
240
241   trans_class->transform_caps =
242       GST_DEBUG_FUNCPTR (gst_cuda_convert_transform_caps);
243   trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_cuda_convert_fixate_caps);
244   trans_class->filter_meta = GST_DEBUG_FUNCPTR (gst_cuda_convert_filter_meta);
245
246   btrans_class->set_info = GST_DEBUG_FUNCPTR (gst_cuda_convert_set_info);
247
248   GST_DEBUG_CATEGORY_INIT (gst_cuda_convert_debug,
249       "cudaconvert", 0, "Video ColorSpace convert using CUDA");
250 }
251
252 static void
253 gst_cuda_convert_init (GstCudaConvert * convert)
254 {
255 }
256
257 static GstCaps *
258 gst_cuda_convert_transform_caps (GstBaseTransform * trans,
259     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
260 {
261   GstCaps *tmp, *tmp2;
262   GstCaps *result;
263
264   /* Get all possible caps that we can transform to */
265   tmp = gst_cuda_convert_caps_remove_format_info (caps);
266
267   if (filter) {
268     tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
269     gst_caps_unref (tmp);
270     tmp = tmp2;
271   }
272
273   result = tmp;
274
275   GST_DEBUG_OBJECT (trans, "transformed %" GST_PTR_FORMAT " into %"
276       GST_PTR_FORMAT, caps, result);
277
278   return result;
279 }
280
281 /* fork of gstvideoconvert */
282 static void
283 gst_cuda_convert_fixate_format (GstBaseTransform * base, GstCaps * caps,
284     GstCaps * result)
285 {
286   GstStructure *ins, *outs;
287   const gchar *in_format;
288   const GstVideoFormatInfo *in_info, *out_info = NULL;
289   gint min_loss = G_MAXINT;
290   guint i, capslen;
291
292   ins = gst_caps_get_structure (caps, 0);
293   in_format = gst_structure_get_string (ins, "format");
294   if (!in_format)
295     return;
296
297   GST_DEBUG_OBJECT (base, "source format %s", in_format);
298
299   in_info =
300       gst_video_format_get_info (gst_video_format_from_string (in_format));
301   if (!in_info)
302     return;
303
304   outs = gst_caps_get_structure (result, 0);
305
306   capslen = gst_caps_get_size (result);
307   GST_DEBUG_OBJECT (base, "iterate %d structures", capslen);
308   for (i = 0; i < capslen; i++) {
309     GstStructure *tests;
310     const GValue *format;
311
312     tests = gst_caps_get_structure (result, i);
313     format = gst_structure_get_value (tests, "format");
314     /* should not happen */
315     if (format == NULL)
316       continue;
317
318     if (GST_VALUE_HOLDS_LIST (format)) {
319       gint j, len;
320
321       len = gst_value_list_get_size (format);
322       GST_DEBUG_OBJECT (base, "have %d formats", len);
323       for (j = 0; j < len; j++) {
324         const GValue *val;
325
326         val = gst_value_list_get_value (format, j);
327         if (G_VALUE_HOLDS_STRING (val)) {
328           score_value (base, in_info, val, &min_loss, &out_info);
329           if (min_loss == 0)
330             break;
331         }
332       }
333     } else if (G_VALUE_HOLDS_STRING (format)) {
334       score_value (base, in_info, format, &min_loss, &out_info);
335     }
336   }
337   if (out_info)
338     gst_structure_set (outs, "format", G_TYPE_STRING,
339         GST_VIDEO_FORMAT_INFO_NAME (out_info), NULL);
340 }
341
342 static GstCaps *
343 gst_cuda_convert_fixate_caps (GstBaseTransform * trans,
344     GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
345 {
346   GstCaps *result;
347
348   GST_DEBUG_OBJECT (trans, "trying to fixate othercaps %" GST_PTR_FORMAT
349       " based on caps %" GST_PTR_FORMAT, othercaps, caps);
350
351   result = gst_caps_intersect (othercaps, caps);
352   if (gst_caps_is_empty (result)) {
353     gst_caps_unref (result);
354     result = othercaps;
355   } else {
356     gst_caps_unref (othercaps);
357   }
358
359   GST_DEBUG_OBJECT (trans, "now fixating %" GST_PTR_FORMAT, result);
360
361   result = gst_caps_make_writable (result);
362   gst_cuda_convert_fixate_format (trans, caps, result);
363
364   /* fixate remaining fields */
365   result = gst_caps_fixate (result);
366
367   if (direction == GST_PAD_SINK) {
368     if (gst_caps_is_subset (caps, result)) {
369       gst_caps_replace (&result, caps);
370     }
371   }
372
373   return result;
374 }
375
376 static gboolean
377 gst_cuda_convert_filter_meta (GstBaseTransform * trans, GstQuery * query,
378     GType api, const GstStructure * params)
379 {
380   /* This element cannot passthrough the crop meta, because it would convert the
381    * wrong sub-region of the image, and worst, our output image may not be large
382    * enough for the crop to be applied later */
383   if (api == GST_VIDEO_CROP_META_API_TYPE)
384     return FALSE;
385
386   /* propose all other metadata upstream */
387   return TRUE;
388 }
389
390 #define CHECK_INFO_FIELDS_MATCHES(field) { \
391   if (in_info->field != in_info->field) { \
392     GST_ERROR_OBJECT (btrans, "%s do not match %d != %d", G_STRINGIFY(field), \
393       in_info->field, out_info->field); \
394     return FALSE;\
395   } \
396 }
397
398 static gboolean
399 gst_cuda_convert_set_info (GstCudaBaseTransform * btrans, GstCaps * incaps,
400     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
401 {
402   CHECK_INFO_FIELDS_MATCHES (width);
403   CHECK_INFO_FIELDS_MATCHES (height);
404   CHECK_INFO_FIELDS_MATCHES (fps_n);
405   CHECK_INFO_FIELDS_MATCHES (fps_d);
406   CHECK_INFO_FIELDS_MATCHES (par_n);
407
408   /* if present, these must match too */
409   CHECK_INFO_FIELDS_MATCHES (par_d);
410   CHECK_INFO_FIELDS_MATCHES (interlace_mode);
411
412
413   return GST_CUDA_BASE_TRANSFORM_CLASS (parent_class)->set_info (btrans, incaps,
414       in_info, outcaps, out_info);
415 }
416
417 #undef CHECK_INFO_FIELDS_MATCHES