2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
5 * Copyright (C) 2010 David Schleef <ds@schleef.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:element-videoconvert
25 * @title: videoconvert
27 * Convert video frames between a great variety of video formats.
29 * ## Example launch line
31 * gst-launch-1.0 -v videotestsrc ! video/x-raw,format=YUY2 ! videoconvert ! autovideosink
33 * This will output a test video (generated in YUY2 format) in a video
34 * window. If the video sink selected does not support YUY2 videoconvert will
35 * automatically convert the video to a format understood by the video sink.
43 #include "gstvideoconvert.h"
45 #include <gst/video/video.h>
46 #include <gst/video/gstvideometa.h>
47 #include <gst/video/gstvideopool.h>
51 #include "gsttbmbufferpool.h"
56 GST_DEBUG_CATEGORY (videoconvert_debug);
57 #define GST_CAT_DEFAULT videoconvert_debug
58 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
60 GType gst_video_convert_get_type (void);
62 static GQuark _colorspace_quark;
64 #define gst_video_convert_parent_class parent_class
65 G_DEFINE_TYPE (GstVideoConvert, gst_video_convert, GST_TYPE_VIDEO_FILTER);
67 #define DEFAULT_PROP_DITHER GST_VIDEO_DITHER_BAYER
68 #define DEFAULT_PROP_DITHER_QUANTIZATION 1
69 #define DEFAULT_PROP_CHROMA_RESAMPLER GST_VIDEO_RESAMPLER_METHOD_LINEAR
70 #define DEFAULT_PROP_ALPHA_MODE GST_VIDEO_ALPHA_MODE_COPY
71 #define DEFAULT_PROP_ALPHA_VALUE 1.0
72 #define DEFAULT_PROP_CHROMA_MODE GST_VIDEO_CHROMA_MODE_FULL
73 #define DEFAULT_PROP_MATRIX_MODE GST_VIDEO_MATRIX_MODE_FULL
74 #define DEFAULT_PROP_GAMMA_MODE GST_VIDEO_GAMMA_MODE_NONE
75 #define DEFAULT_PROP_PRIMARIES_MODE GST_VIDEO_PRIMARIES_MODE_NONE
76 #define DEFAULT_PROP_N_THREADS 1
82 PROP_DITHER_QUANTIZATION,
83 PROP_CHROMA_RESAMPLER,
93 #define CSP_VIDEO_CAPS GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) ";" \
94 GST_VIDEO_CAPS_MAKE_WITH_FEATURES ("ANY", GST_VIDEO_FORMATS_ALL) ";" \
95 GST_VIDEO_CAPS_MAKE("{ SUYV , SYVY , S420 , ITLV }") ";" \
96 GST_VIDEO_CAPS_MAKE_WITH_FEATURES ("ANY", "{ SUYV , SYVY , S420 , ITLV }")
98 #define CSP_VIDEO_SRC_CAPS GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL) ";" \
99 GST_VIDEO_CAPS_MAKE_WITH_FEATURES ("ANY", GST_VIDEO_FORMATS_ALL) ";" \
100 GST_VIDEO_CAPS_MAKE("{ SUYV , SYVY , S420 , ITLV , SN12 }") ";" \
101 GST_VIDEO_CAPS_MAKE_WITH_FEATURES ("ANY", "{ SUYV , SYVY , S420 , ITLV , SN12 }")
103 static GstStaticPadTemplate gst_video_convert_src_template =
104 GST_STATIC_PAD_TEMPLATE ("src",
107 GST_STATIC_CAPS (CSP_VIDEO_SRC_CAPS)
110 static GstStaticPadTemplate gst_video_convert_sink_template =
111 GST_STATIC_PAD_TEMPLATE ("sink",
114 GST_STATIC_CAPS (CSP_VIDEO_CAPS)
117 static void gst_video_convert_set_property (GObject * object,
118 guint property_id, const GValue * value, GParamSpec * pspec);
119 static void gst_video_convert_get_property (GObject * object,
120 guint property_id, GValue * value, GParamSpec * pspec);
122 static gboolean gst_video_convert_set_info (GstVideoFilter * filter,
123 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
124 GstVideoInfo * out_info);
125 static GstFlowReturn gst_video_convert_transform_frame (GstVideoFilter * filter,
126 GstVideoFrame * in_frame, GstVideoFrame * out_frame);
128 #ifdef USE_TBM_BUFFER
129 static gboolean gst_video_convert_decide_allocation (GstBaseTransform * bsrc,
131 static GstFlowReturn gst_video_convert_prepare_output_buffer (GstBaseTransform * trans,
132 GstBuffer *input, GstBuffer **outbuf);
135 /* copies the given caps */
137 gst_video_convert_caps_remove_format_info (GstCaps * caps)
144 res = gst_caps_new_empty ();
146 n = gst_caps_get_size (caps);
147 for (i = 0; i < n; i++) {
148 st = gst_caps_get_structure (caps, i);
149 f = gst_caps_get_features (caps, i);
151 /* If this is already expressed by the existing caps
152 * skip this structure */
153 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
156 st = gst_structure_copy (st);
157 /* Only remove format info for the cases when we can actually convert */
158 if (!gst_caps_features_is_any (f)
159 && gst_caps_features_is_equal (f,
160 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
161 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
164 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
171 * This is an incomplete matrix of in formats and a score for the prefered output
174 * out: RGB24 RGB16 ARGB AYUV YUV444 YUV422 YUV420 YUV411 YUV410 PAL GRAY
176 * RGB24 0 2 1 2 2 3 4 5 6 7 8
177 * RGB16 1 0 1 2 2 3 4 5 6 7 8
178 * ARGB 2 3 0 1 4 5 6 7 8 9 10
179 * AYUV 3 4 1 0 2 5 6 7 8 9 10
180 * YUV444 2 4 3 1 0 5 6 7 8 9 10
181 * YUV422 3 5 4 2 1 0 6 7 8 9 10
182 * YUV420 4 6 5 3 2 1 0 7 8 9 10
183 * YUV411 4 6 5 3 2 1 7 0 8 9 10
184 * YUV410 6 8 7 5 4 3 2 1 0 9 10
185 * PAL 1 3 2 6 4 6 7 8 9 0 10
186 * GRAY 1 4 3 2 1 5 6 7 8 9 0
188 * PAL or GRAY are never prefered, if we can we would convert to PAL instead
190 * less subsampling is prefered and if any, preferably horizontal
191 * We would like to keep the alpha, even if we would need to to colorspace conversion
194 #define SCORE_FORMAT_CHANGE 1
195 #define SCORE_DEPTH_CHANGE 1
196 #define SCORE_ALPHA_CHANGE 1
197 #define SCORE_CHROMA_W_CHANGE 1
198 #define SCORE_CHROMA_H_CHANGE 1
199 #define SCORE_PALETTE_CHANGE 1
201 #define SCORE_COLORSPACE_LOSS 2 /* RGB <-> YUV */
202 #define SCORE_DEPTH_LOSS 4 /* change bit depth */
203 #define SCORE_ALPHA_LOSS 8 /* lose the alpha channel */
204 #define SCORE_CHROMA_W_LOSS 16 /* vertical subsample */
205 #define SCORE_CHROMA_H_LOSS 32 /* horizontal subsample */
206 #define SCORE_PALETTE_LOSS 64 /* convert to palette format */
207 #define SCORE_COLOR_LOSS 128 /* convert to GRAY */
209 #define COLORSPACE_MASK (GST_VIDEO_FORMAT_FLAG_YUV | \
210 GST_VIDEO_FORMAT_FLAG_RGB | GST_VIDEO_FORMAT_FLAG_GRAY)
211 #define ALPHA_MASK (GST_VIDEO_FORMAT_FLAG_ALPHA)
212 #define PALETTE_MASK (GST_VIDEO_FORMAT_FLAG_PALETTE)
214 /* calculate how much loss a conversion would be */
216 score_value (GstBaseTransform * base, const GstVideoFormatInfo * in_info,
217 const GValue * val, gint * min_loss, const GstVideoFormatInfo ** out_info)
220 const GstVideoFormatInfo *t_info;
221 GstVideoFormatFlags in_flags, t_flags;
224 fname = g_value_get_string (val);
225 t_info = gst_video_format_get_info (gst_video_format_from_string (fname));
229 /* accept input format immediately without loss */
230 if (in_info == t_info) {
236 loss = SCORE_FORMAT_CHANGE;
238 in_flags = GST_VIDEO_FORMAT_INFO_FLAGS (in_info);
239 in_flags &= ~GST_VIDEO_FORMAT_FLAG_LE;
240 in_flags &= ~GST_VIDEO_FORMAT_FLAG_COMPLEX;
241 in_flags &= ~GST_VIDEO_FORMAT_FLAG_UNPACK;
243 t_flags = GST_VIDEO_FORMAT_INFO_FLAGS (t_info);
244 t_flags &= ~GST_VIDEO_FORMAT_FLAG_LE;
245 t_flags &= ~GST_VIDEO_FORMAT_FLAG_COMPLEX;
246 t_flags &= ~GST_VIDEO_FORMAT_FLAG_UNPACK;
248 if ((t_flags & PALETTE_MASK) != (in_flags & PALETTE_MASK)) {
249 loss += SCORE_PALETTE_CHANGE;
250 if (t_flags & PALETTE_MASK)
251 loss += SCORE_PALETTE_LOSS;
254 if ((t_flags & COLORSPACE_MASK) != (in_flags & COLORSPACE_MASK)) {
255 loss += SCORE_COLORSPACE_LOSS;
256 if (t_flags & GST_VIDEO_FORMAT_FLAG_GRAY)
257 loss += SCORE_COLOR_LOSS;
260 if ((t_flags & ALPHA_MASK) != (in_flags & ALPHA_MASK)) {
261 loss += SCORE_ALPHA_CHANGE;
262 if (in_flags & ALPHA_MASK)
263 loss += SCORE_ALPHA_LOSS;
266 if ((in_info->h_sub[1]) != (t_info->h_sub[1])) {
267 loss += SCORE_CHROMA_H_CHANGE;
268 if ((in_info->h_sub[1]) < (t_info->h_sub[1]))
269 loss += SCORE_CHROMA_H_LOSS;
271 if ((in_info->w_sub[1]) != (t_info->w_sub[1])) {
272 loss += SCORE_CHROMA_W_CHANGE;
273 if ((in_info->w_sub[1]) < (t_info->w_sub[1]))
274 loss += SCORE_CHROMA_W_LOSS;
277 if ((in_info->bits) != (t_info->bits)) {
278 loss += SCORE_DEPTH_CHANGE;
279 if ((in_info->bits) > (t_info->bits))
280 loss += SCORE_DEPTH_LOSS;
283 GST_DEBUG_OBJECT (base, "score %s -> %s = %d",
284 GST_VIDEO_FORMAT_INFO_NAME (in_info),
285 GST_VIDEO_FORMAT_INFO_NAME (t_info), loss);
287 if (loss < *min_loss) {
288 GST_DEBUG_OBJECT (base, "found new best %d", loss);
295 gst_video_convert_fixate_format (GstBaseTransform * base, GstCaps * caps,
298 GstStructure *ins, *outs;
299 const gchar *in_format;
300 const GstVideoFormatInfo *in_info, *out_info = NULL;
301 gint min_loss = G_MAXINT;
304 ins = gst_caps_get_structure (caps, 0);
305 in_format = gst_structure_get_string (ins, "format");
309 GST_DEBUG_OBJECT (base, "source format %s", in_format);
312 gst_video_format_get_info (gst_video_format_from_string (in_format));
316 outs = gst_caps_get_structure (result, 0);
318 capslen = gst_caps_get_size (result);
319 GST_DEBUG_OBJECT (base, "iterate %d structures", capslen);
320 for (i = 0; i < capslen; i++) {
322 const GValue *format;
324 tests = gst_caps_get_structure (result, i);
325 format = gst_structure_get_value (tests, "format");
326 /* should not happen */
330 if (GST_VALUE_HOLDS_LIST (format)) {
333 len = gst_value_list_get_size (format);
334 GST_DEBUG_OBJECT (base, "have %d formats", len);
335 for (j = 0; j < len; j++) {
338 val = gst_value_list_get_value (format, j);
339 if (G_VALUE_HOLDS_STRING (val)) {
340 score_value (base, in_info, val, &min_loss, &out_info);
345 } else if (G_VALUE_HOLDS_STRING (format)) {
346 score_value (base, in_info, format, &min_loss, &out_info);
350 gst_structure_set (outs, "format", G_TYPE_STRING,
351 GST_VIDEO_FORMAT_INFO_NAME (out_info), NULL);
356 gst_video_convert_fixate_caps (GstBaseTransform * trans,
357 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
361 GST_DEBUG_OBJECT (trans, "trying to fixate othercaps %" GST_PTR_FORMAT
362 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
364 result = gst_caps_intersect (othercaps, caps);
365 if (gst_caps_is_empty (result)) {
366 gst_caps_unref (result);
369 gst_caps_unref (othercaps);
372 GST_DEBUG_OBJECT (trans, "now fixating %" GST_PTR_FORMAT, result);
374 result = gst_caps_make_writable (result);
375 gst_video_convert_fixate_format (trans, caps, result);
377 /* fixate remaining fields */
378 result = gst_caps_fixate (result);
380 if (direction == GST_PAD_SINK) {
381 if (gst_caps_is_subset (caps, result)) {
382 gst_caps_replace (&result, caps);
390 gst_video_convert_filter_meta (GstBaseTransform * trans, GstQuery * query,
391 GType api, const GstStructure * params)
393 /* propose all metadata upstream */
397 /* The caps can be transformed into any other caps with format info removed.
398 * However, we should prefer passthrough, so if passthrough is possible,
399 * put it first in the list. */
401 gst_video_convert_transform_caps (GstBaseTransform * btrans,
402 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
407 /* Get all possible caps that we can transform to */
408 tmp = gst_video_convert_caps_remove_format_info (caps);
411 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
412 gst_caps_unref (tmp);
418 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
419 GST_PTR_FORMAT, caps, result);
425 gst_video_convert_transform_meta (GstBaseTransform * trans, GstBuffer * outbuf,
426 GstMeta * meta, GstBuffer * inbuf)
428 const GstMetaInfo *info = meta->info;
431 if (gst_meta_api_type_has_tag (info->api, _colorspace_quark)) {
432 /* don't copy colorspace specific metadata, FIXME, we need a MetaTransform
433 * for the colorspace metadata. */
436 /* copy other metadata */
443 gst_video_convert_set_info (GstVideoFilter * filter,
444 GstCaps * incaps, GstVideoInfo * in_info, GstCaps * outcaps,
445 GstVideoInfo * out_info)
447 GstVideoConvert *space;
449 space = GST_VIDEO_CONVERT_CAST (filter);
451 if (space->convert) {
452 gst_video_converter_free (space->convert);
453 space->convert = NULL;
456 /* these must match */
457 if (in_info->width != out_info->width || in_info->height != out_info->height
458 || in_info->fps_n != out_info->fps_n || in_info->fps_d != out_info->fps_d)
459 goto format_mismatch;
461 /* if present, these must match too */
462 if (in_info->par_n != out_info->par_n || in_info->par_d != out_info->par_d)
463 goto format_mismatch;
465 /* if present, these must match too */
466 if (in_info->interlace_mode != out_info->interlace_mode)
467 goto format_mismatch;
470 space->convert = gst_video_converter_new (in_info, out_info,
471 gst_structure_new ("GstVideoConvertConfig",
472 GST_VIDEO_CONVERTER_OPT_DITHER_METHOD, GST_TYPE_VIDEO_DITHER_METHOD,
474 GST_VIDEO_CONVERTER_OPT_DITHER_QUANTIZATION, G_TYPE_UINT,
475 space->dither_quantization,
476 GST_VIDEO_CONVERTER_OPT_CHROMA_RESAMPLER_METHOD,
477 GST_TYPE_VIDEO_RESAMPLER_METHOD, space->chroma_resampler,
478 GST_VIDEO_CONVERTER_OPT_ALPHA_MODE,
479 GST_TYPE_VIDEO_ALPHA_MODE, space->alpha_mode,
480 GST_VIDEO_CONVERTER_OPT_ALPHA_VALUE,
481 G_TYPE_DOUBLE, space->alpha_value,
482 GST_VIDEO_CONVERTER_OPT_CHROMA_MODE,
483 GST_TYPE_VIDEO_CHROMA_MODE, space->chroma_mode,
484 GST_VIDEO_CONVERTER_OPT_MATRIX_MODE,
485 GST_TYPE_VIDEO_MATRIX_MODE, space->matrix_mode,
486 GST_VIDEO_CONVERTER_OPT_GAMMA_MODE,
487 GST_TYPE_VIDEO_GAMMA_MODE, space->gamma_mode,
488 GST_VIDEO_CONVERTER_OPT_PRIMARIES_MODE,
489 GST_TYPE_VIDEO_PRIMARIES_MODE, space->primaries_mode,
490 GST_VIDEO_CONVERTER_OPT_THREADS, G_TYPE_UINT,
491 space->n_threads, NULL));
492 if (space->convert == NULL)
495 GST_DEBUG ("reconfigured %d %d", GST_VIDEO_INFO_FORMAT (in_info),
496 GST_VIDEO_INFO_FORMAT (out_info));
503 GST_ERROR_OBJECT (space, "input and output formats do not match");
508 GST_ERROR_OBJECT (space, "could not create converter");
514 gst_video_convert_finalize (GObject * obj)
516 GstVideoConvert *space = GST_VIDEO_CONVERT (obj);
518 if (space->convert) {
519 gst_video_converter_free (space->convert);
521 #ifdef USE_TBM_BUFFER
522 if(space->tbm_buffer_pool) {
523 gst_buffer_pool_set_active (space->tbm_buffer_pool, FALSE);
524 gst_object_unref (space->tbm_buffer_pool);
525 space->tbm_buffer_pool = NULL;
528 G_OBJECT_CLASS (parent_class)->finalize (obj);
532 gst_video_convert_class_init (GstVideoConvertClass * klass)
534 GObjectClass *gobject_class = (GObjectClass *) klass;
535 GstElementClass *gstelement_class = (GstElementClass *) klass;
536 GstBaseTransformClass *gstbasetransform_class =
537 (GstBaseTransformClass *) klass;
538 GstVideoFilterClass *gstvideofilter_class = (GstVideoFilterClass *) klass;
540 gobject_class->set_property = gst_video_convert_set_property;
541 gobject_class->get_property = gst_video_convert_get_property;
542 gobject_class->finalize = gst_video_convert_finalize;
544 gst_element_class_add_static_pad_template (gstelement_class,
545 &gst_video_convert_src_template);
546 gst_element_class_add_static_pad_template (gstelement_class,
547 &gst_video_convert_sink_template);
549 gst_element_class_set_static_metadata (gstelement_class,
550 "Colorspace converter", "Filter/Converter/Video",
551 "Converts video from one colorspace to another",
552 "GStreamer maintainers <gstreamer-devel@lists.freedesktop.org>");
554 gstbasetransform_class->transform_caps =
555 GST_DEBUG_FUNCPTR (gst_video_convert_transform_caps);
556 gstbasetransform_class->fixate_caps =
557 GST_DEBUG_FUNCPTR (gst_video_convert_fixate_caps);
558 gstbasetransform_class->filter_meta =
559 GST_DEBUG_FUNCPTR (gst_video_convert_filter_meta);
560 gstbasetransform_class->transform_meta =
561 GST_DEBUG_FUNCPTR (gst_video_convert_transform_meta);
563 gstbasetransform_class->passthrough_on_same_caps = TRUE;
565 gstvideofilter_class->set_info =
566 GST_DEBUG_FUNCPTR (gst_video_convert_set_info);
567 gstvideofilter_class->transform_frame =
568 GST_DEBUG_FUNCPTR (gst_video_convert_transform_frame);
570 #ifdef USE_TBM_BUFFER
571 gstbasetransform_class->decide_allocation = gst_video_convert_decide_allocation;
572 gstbasetransform_class->prepare_output_buffer = gst_video_convert_prepare_output_buffer;
575 g_object_class_install_property (gobject_class, PROP_DITHER,
576 g_param_spec_enum ("dither", "Dither", "Apply dithering while converting",
577 gst_video_dither_method_get_type (), DEFAULT_PROP_DITHER,
578 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579 g_object_class_install_property (gobject_class, PROP_DITHER_QUANTIZATION,
580 g_param_spec_uint ("dither-quantization", "Dither Quantize",
581 "Quantizer to use", 0, G_MAXUINT, DEFAULT_PROP_DITHER_QUANTIZATION,
582 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 g_object_class_install_property (gobject_class, PROP_CHROMA_RESAMPLER,
584 g_param_spec_enum ("chroma-resampler", "Chroma resampler",
585 "Chroma resampler method", gst_video_resampler_method_get_type (),
586 DEFAULT_PROP_CHROMA_RESAMPLER,
587 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
588 g_object_class_install_property (gobject_class, PROP_ALPHA_MODE,
589 g_param_spec_enum ("alpha-mode", "Alpha Mode",
590 "Alpha Mode to use", gst_video_alpha_mode_get_type (),
591 DEFAULT_PROP_ALPHA_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
592 g_object_class_install_property (gobject_class, PROP_ALPHA_VALUE,
593 g_param_spec_double ("alpha-value", "Alpha Value",
594 "Alpha Value to use", 0.0, 1.0,
595 DEFAULT_PROP_ALPHA_VALUE,
596 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
597 g_object_class_install_property (gobject_class, PROP_CHROMA_MODE,
598 g_param_spec_enum ("chroma-mode", "Chroma Mode", "Chroma Resampling Mode",
599 gst_video_chroma_mode_get_type (), DEFAULT_PROP_CHROMA_MODE,
600 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
601 g_object_class_install_property (gobject_class, PROP_MATRIX_MODE,
602 g_param_spec_enum ("matrix-mode", "Matrix Mode", "Matrix Conversion Mode",
603 gst_video_matrix_mode_get_type (), DEFAULT_PROP_MATRIX_MODE,
604 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
605 g_object_class_install_property (gobject_class, PROP_GAMMA_MODE,
606 g_param_spec_enum ("gamma-mode", "Gamma Mode", "Gamma Conversion Mode",
607 gst_video_gamma_mode_get_type (), DEFAULT_PROP_GAMMA_MODE,
608 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
609 g_object_class_install_property (gobject_class, PROP_PRIMARIES_MODE,
610 g_param_spec_enum ("primaries-mode", "Primaries Mode",
611 "Primaries Conversion Mode", gst_video_primaries_mode_get_type (),
612 DEFAULT_PROP_PRIMARIES_MODE,
613 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
614 g_object_class_install_property (gobject_class, PROP_N_THREADS,
615 g_param_spec_uint ("n-threads", "Threads",
616 "Maximum number of threads to use", 0, G_MAXUINT,
617 DEFAULT_PROP_N_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
621 gst_video_convert_init (GstVideoConvert * space)
623 #ifdef USE_TBM_BUFFER
624 space->tbm_buffer_pool = NULL;
626 space->dither = DEFAULT_PROP_DITHER;
627 space->dither_quantization = DEFAULT_PROP_DITHER_QUANTIZATION;
628 space->chroma_resampler = DEFAULT_PROP_CHROMA_RESAMPLER;
629 space->alpha_mode = DEFAULT_PROP_ALPHA_MODE;
630 space->alpha_value = DEFAULT_PROP_ALPHA_VALUE;
631 space->chroma_mode = DEFAULT_PROP_CHROMA_MODE;
632 space->matrix_mode = DEFAULT_PROP_MATRIX_MODE;
633 space->gamma_mode = DEFAULT_PROP_GAMMA_MODE;
634 space->primaries_mode = DEFAULT_PROP_PRIMARIES_MODE;
635 space->n_threads = DEFAULT_PROP_N_THREADS;
639 gst_video_convert_set_property (GObject * object, guint property_id,
640 const GValue * value, GParamSpec * pspec)
642 GstVideoConvert *csp;
644 csp = GST_VIDEO_CONVERT (object);
646 switch (property_id) {
648 csp->dither = g_value_get_enum (value);
650 case PROP_CHROMA_RESAMPLER:
651 csp->chroma_resampler = g_value_get_enum (value);
653 case PROP_ALPHA_MODE:
654 csp->alpha_mode = g_value_get_enum (value);
656 case PROP_ALPHA_VALUE:
657 csp->alpha_value = g_value_get_double (value);
659 case PROP_CHROMA_MODE:
660 csp->chroma_mode = g_value_get_enum (value);
662 case PROP_MATRIX_MODE:
663 csp->matrix_mode = g_value_get_enum (value);
665 case PROP_GAMMA_MODE:
666 csp->gamma_mode = g_value_get_enum (value);
668 case PROP_PRIMARIES_MODE:
669 csp->primaries_mode = g_value_get_enum (value);
671 case PROP_DITHER_QUANTIZATION:
672 csp->dither_quantization = g_value_get_uint (value);
675 csp->n_threads = g_value_get_uint (value);
678 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
684 gst_video_convert_get_property (GObject * object, guint property_id,
685 GValue * value, GParamSpec * pspec)
687 GstVideoConvert *csp;
689 csp = GST_VIDEO_CONVERT (object);
691 switch (property_id) {
693 g_value_set_enum (value, csp->dither);
695 case PROP_CHROMA_RESAMPLER:
696 g_value_set_enum (value, csp->chroma_resampler);
698 case PROP_ALPHA_MODE:
699 g_value_set_enum (value, csp->alpha_mode);
701 case PROP_ALPHA_VALUE:
702 g_value_set_double (value, csp->alpha_value);
704 case PROP_CHROMA_MODE:
705 g_value_set_enum (value, csp->chroma_mode);
707 case PROP_MATRIX_MODE:
708 g_value_set_enum (value, csp->matrix_mode);
710 case PROP_GAMMA_MODE:
711 g_value_set_enum (value, csp->gamma_mode);
713 case PROP_PRIMARIES_MODE:
714 g_value_set_enum (value, csp->primaries_mode);
716 case PROP_DITHER_QUANTIZATION:
717 g_value_set_uint (value, csp->dither_quantization);
720 g_value_set_uint (value, csp->n_threads);
723 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
729 gst_video_convert_transform_frame (GstVideoFilter * filter,
730 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
732 GstVideoConvert *space;
734 space = GST_VIDEO_CONVERT_CAST (filter);
736 GST_CAT_DEBUG_OBJECT (CAT_PERFORMANCE, filter,
737 "doing colorspace conversion from %s -> to %s",
738 GST_VIDEO_INFO_NAME (&filter->in_info),
739 GST_VIDEO_INFO_NAME (&filter->out_info));
740 gst_video_converter_frame (space->convert, in_frame, out_frame);
745 #ifdef USE_TBM_BUFFER
748 gst_video_convert_decide_allocation (GstBaseTransform * trans,
752 GstVideoConvert *vc = NULL;
753 GstVideoFilter *filter = GST_VIDEO_FILTER_CAST (trans);
754 vc = GST_VIDEO_CONVERT_CAST(trans);
756 if(filter->out_info.finfo->format == GST_VIDEO_FORMAT_SN12 ) {
761 GstStructure *config;
762 GstCaps *caps = NULL;
764 if (gst_query_get_n_allocation_pools (query) > 0) {
765 gst_query_parse_nth_allocation_pool (query, 0, &vc->tbm_buffer_pool, &size, &min, &max);
768 size = MAX (size, sizeof(MMVideoBuffer));
771 vc->tbm_buffer_pool = NULL;
775 size = sizeof(MMVideoBuffer);
776 gst_query_parse_allocation (query, &caps, NULL);
779 vc->tbm_buffer_pool = gst_mm_buffer_pool_new(GST_ELEMENT_CAST(trans));
780 GST_DEBUG("[%s]CREATING VIDEO_BUFFER_POOL",__FUNCTION__);
784 config = gst_buffer_pool_get_config (vc->tbm_buffer_pool);
789 gst_buffer_pool_config_set_params (config, caps, size, min, max);
791 if (gst_query_find_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL)) {
792 gst_buffer_pool_config_add_option (config,
793 GST_BUFFER_POOL_OPTION_VIDEO_META);
796 if (!gst_buffer_pool_set_config (vc->tbm_buffer_pool, config)) {
797 GST_INFO ("Failed to set config on internal pool");
798 gst_object_unref (vc->tbm_buffer_pool);
799 vc->tbm_buffer_pool = NULL;
803 gst_query_add_allocation_pool (query, vc->tbm_buffer_pool, size, min, max);
805 if (!gst_buffer_pool_set_active(vc->tbm_buffer_pool,TRUE)) {
806 gst_object_unref (vc->tbm_buffer_pool);
807 vc->tbm_buffer_pool = NULL;
808 GST_INFO ("Failed to activate internal pool");
811 GST_DEBUG("[%s]BUFFER_POOL max:[%d], min:[%d]",__FUNCTION__, max, min);
813 GST_DEBUG("Not using our internal pool and copying buffers for downstream");
817 return GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans, query);
821 gst_video_convert_prepare_output_buffer (GstBaseTransform * trans,
822 GstBuffer *input, GstBuffer **outbuf)
824 GstVideoConvert *vc = NULL;
825 GstVideoFilter *filter = GST_VIDEO_FILTER_CAST (trans);
827 vc = GST_VIDEO_CONVERT_CAST(trans);
829 if(filter->out_info.finfo->format == GST_VIDEO_FORMAT_SN12 ) {
830 if(gst_buffer_pool_acquire_buffer(vc->tbm_buffer_pool,outbuf,0) != GST_FLOW_OK) {
831 GST_ERROR("[%s] memory prepare failed.",__FUNCTION__);
832 return GST_FLOW_ERROR;
836 return GST_BASE_TRANSFORM_CLASS (parent_class)->prepare_output_buffer(trans, input, outbuf);
841 plugin_init (GstPlugin * plugin)
843 GST_DEBUG_CATEGORY_INIT (videoconvert_debug, "videoconvert", 0,
844 "Colorspace Converter");
846 GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
848 _colorspace_quark = g_quark_from_static_string ("colorspace");
850 return gst_element_register (plugin, "videoconvert",
851 GST_RANK_NONE, GST_TYPE_VIDEO_CONVERT);
854 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
856 videoconvert, "Colorspace conversion", plugin_init, VERSION, GST_LICENSE,
857 GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)