1 /* Image Quality Assessment plugin
2 * Copyright (C) 2015 Mathieu Duponchelle <mathieu.duponchelle@collabora.co.uk>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 * @short_description: Image Quality Assessment plugin.
24 * IQA will perform full reference image quality assessment, with the
25 * first added pad being the reference.
27 * It will perform comparisons on video streams with the same geometry.
29 * The image output will be the heat map of differences, between
30 * the two pads with the highest measured difference.
32 * For each reference frame, IQA will post a message containing
33 * a structure named IQA.
35 * The only metric supported for now is "dssim", which will be available
36 * if https://github.com/pornel/dssim was installed on the system
37 * at the time that plugin was compiled.
39 * For each metric activated, this structure will contain another
40 * structure, named after the metric.
42 * The message will also contain a "time" field.
44 * For example, if do-dssim is set to true, and there are
45 * two compared streams, the emitted structure will look like this:
47 * IQA, dssim=(structure)"dssim\,\ sink_1\=\(double\)0.053621271267184856\,\
48 * sink_2\=\(double\)0.0082939683976297474\;",
52 * <title>Example launch line</title>
54 * gst-launch-1.0 -m uridecodebin uri=file:///test/file/1 ! iqa name=iqa do-dssim=true \
55 * ! videoconvert ! autovideosink uridecodebin uri=file:///test/file/2 ! iqa.
56 * ]| This pipeline will output messages to the console for each set of compared frames.
70 GST_DEBUG_CATEGORY_STATIC (gst_iqa_debug);
71 #define GST_CAT_DEFAULT gst_iqa_debug
73 #define SINK_FORMATS " { AYUV, BGRA, ARGB, RGBA, ABGR, Y444, Y42B, YUY2, UYVY, "\
74 " YVYU, I420, YV12, NV12, NV21, Y41B, RGB, BGR, xRGB, xBGR, "\
77 #define SRC_FORMAT " { RGBA } "
79 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
82 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (SRC_FORMAT))
92 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
95 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (SINK_FORMATS))
101 #define gst_iqa_parent_class parent_class
102 G_DEFINE_TYPE (GstIqa, gst_iqa, GST_TYPE_VIDEO_AGGREGATOR);
106 inline static unsigned char
111 if (in >= 255.f / 256.f)
117 do_dssim (GstIqa * self, GstVideoFrame * ref, GstVideoFrame * cmp,
118 GstBuffer * outbuf, GstStructure * msg_structure, gchar * padname)
120 dssim_attr *attr = dssim_create_attr ();
122 unsigned char **ptrs, **ptrs2;
126 dssim_image *ref_image;
127 dssim_image *cmp_image;
129 dssim_ssim_map map_meta;
133 GstStructure *dssim_structure;
135 gst_structure_get (msg_structure, "dssim", GST_TYPE_STRUCTURE,
136 &dssim_structure, NULL);
138 dssim_set_save_ssim_maps (attr, 1, 1);
139 if (ref->info.width != cmp->info.width ||
140 ref->info.height != cmp->info.height) {
141 GST_WARNING_OBJECT (self,
142 "Cannot compare two images with a different geometry yet");
146 gst_buffer_map (ref->buffer, &ref_info, GST_MAP_READ);
147 gst_buffer_map (cmp->buffer, &cmp_info, GST_MAP_READ);
148 gst_buffer_map (outbuf, &out_info, GST_MAP_WRITE);
149 out = (dssim_rgba *) out_info.data;
151 ptrs = g_malloc (sizeof (char **) * ref->info.height);
152 ptrs2 = g_malloc (sizeof (char **) * cmp->info.height);
154 for (y = 0; y < ref->info.height; y++) {
155 ptrs[y] = ref_info.data + (ref->info.width * 4 * y);
159 dssim_create_image (attr, ptrs, DSSIM_RGBA, ref->info.width,
160 ref->info.height, 0.45455);
162 ptrs2 = g_malloc (sizeof (char **) * cmp->info.height);
164 for (y = 0; y < cmp->info.height; y++) {
165 ptrs2[y] = cmp_info.data + (cmp->info.width * 4 * y);
169 dssim_create_image (attr, ptrs2, DSSIM_RGBA, cmp->info.width,
170 cmp->info.height, 0.45455);
171 dssim = dssim_compare (attr, ref_image, cmp_image);
173 map_meta = dssim_pop_ssim_map (attr, 0, 0);
175 if (dssim > self->max_dssim) {
178 for (i = 0; i < map_meta.width * map_meta.height; i++) {
179 const float max = 1.0 - map[i];
180 const float maxsq = max * max;
181 out[i] = (dssim_rgba) {
182 .r = to_byte (max * 3.0),.g = to_byte (maxsq * 6.0),.b =
183 to_byte (max / ((1.0 - map_meta.dssim) * 4.0)),.a = 255,};
185 self->max_dssim = dssim;
188 gst_structure_set (dssim_structure, padname, G_TYPE_DOUBLE, dssim, NULL);
189 gst_structure_set (msg_structure, "dssim", GST_TYPE_STRUCTURE,
190 dssim_structure, NULL);
191 gst_structure_free (dssim_structure);
195 gst_buffer_unmap (ref->buffer, &ref_info);
196 gst_buffer_unmap (cmp->buffer, &cmp_info);
197 gst_buffer_unmap (outbuf, &out_info);
198 dssim_dealloc_image (ref_image);
199 dssim_dealloc_image (cmp_image);
200 dssim_dealloc_attr (attr);
204 do_dssim (GstIqa * self, GstVideoFrame * ref, GstVideoFrame * cmp,
205 GstBuffer * outbuf, GstStructure * msg_structure, gchar * padname)
211 compare_frames (GstIqa * self, GstVideoFrame * ref, GstVideoFrame * cmp,
212 GstBuffer * outbuf, GstStructure * msg_structure, gchar * padname)
215 do_dssim (self, ref, cmp, outbuf, msg_structure, padname);
219 gst_iqa_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
222 GstVideoFrame *ref_frame = NULL;
223 GstIqa *self = GST_IQA (vagg);
224 GstStructure *msg_structure = gst_structure_new_empty ("IQA");
225 GstMessage *m = gst_message_new_element (GST_OBJECT (self), msg_structure);
226 GstAggregator *agg = GST_AGGREGATOR (vagg);
228 if (self->do_dssim) {
229 gst_structure_set (msg_structure, "dssim", GST_TYPE_STRUCTURE,
230 gst_structure_new_empty ("dssim"), NULL);
231 self->max_dssim = 0.0;
234 GST_OBJECT_LOCK (vagg);
235 for (l = GST_ELEMENT (vagg)->sinkpads; l; l = l->next) {
236 GstVideoAggregatorPad *pad = l->data;
238 if (pad->aggregated_frame != NULL) {
240 ref_frame = pad->aggregated_frame;
242 gchar *padname = gst_pad_get_name (pad);
243 GstVideoFrame *cmp_frame = pad->aggregated_frame;
245 compare_frames (self, ref_frame, cmp_frame, outbuf, msg_structure,
252 GST_OBJECT_UNLOCK (vagg);
254 /* We only post the message here, because we can't post it while the object
257 gst_structure_set (msg_structure, "time", GST_TYPE_CLOCK_TIME,
258 agg->segment.position, NULL);
259 gst_element_post_message (GST_ELEMENT (self), m);
264 _set_property (GObject * object, guint prop_id, const GValue * value,
267 GstIqa *self = GST_IQA (object);
271 self->do_dssim = g_value_get_boolean (value);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 _get_property (GObject * object,
281 guint prop_id, GValue * value, GParamSpec * pspec)
283 GstIqa *self = GST_IQA (object);
287 g_value_set_boolean (value, self->do_dssim);
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 /* GObject boilerplate */
297 gst_iqa_class_init (GstIqaClass * klass)
299 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
300 GstElementClass *gstelement_class = (GstElementClass *) klass;
301 GstVideoAggregatorClass *videoaggregator_class =
302 (GstVideoAggregatorClass *) klass;
304 videoaggregator_class->aggregate_frames = gst_iqa_aggregate_frames;
306 gst_element_class_add_pad_template (gstelement_class,
307 gst_static_pad_template_get (&src_factory));
308 gst_element_class_add_pad_template (gstelement_class,
309 gst_static_pad_template_get (&sink_factory));
311 gobject_class->set_property = _set_property;
312 gobject_class->get_property = _get_property;
315 g_object_class_install_property (gobject_class, PROP_DO_SSIM,
316 g_param_spec_boolean ("do-dssim", "do-dssim",
317 "Run structural similarity checks", FALSE, G_PARAM_READWRITE));
320 gst_element_class_set_static_metadata (gstelement_class, "Iqa",
321 "Filter/Analyzer/Video",
322 "Provides various Image Quality Assessment metrics",
323 "Mathieu Duponchelle <mathieu.duponchelle@collabora.co.uk>");
327 gst_iqa_init (GstIqa * self)
332 plugin_init (GstPlugin * plugin)
334 GST_DEBUG_CATEGORY_INIT (gst_iqa_debug, "iqa", 0, "iqa");
336 return gst_element_register (plugin, "iqa", GST_RANK_PRIMARY, GST_TYPE_IQA);
339 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
342 "Iqa", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,