1 /* Image Quality Assessment plugin
2 * Copyright (C) 2015 Mathieu Duponchelle <mathieu.duponchelle@collabora.co.uk>
3 *
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.
8 *
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.
13 *
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.
18 */
19
20 /**
21 * SECTION:element-iqa
22 * @title: iqa
23 * @short_description: Image Quality Assessment plugin.
24 *
25 * IQA will perform full reference image quality assessment, with the
26 * first added pad being the reference.
27 *
28 * It will perform comparisons on video streams with the same geometry.
29 *
30 * The image output will be the heat map of differences, between
31 * the two pads with the highest measured difference.
32 *
33 * For each reference frame, IQA will post a message containing
34 * a structure named IQA.
35 *
36 * The only metric supported for now is "dssim", which will be available
37 * if https://github.com/pornel/dssim was installed on the system
38 * at the time that plugin was compiled.
39 *
40 * For each metric activated, this structure will contain another
41 * structure, named after the metric.
42 *
43 * The message will also contain a "time" field.
44 *
45 * For example, if do-dssim is set to true, and there are
46 * two compared streams, the emitted structure will look like this:
47 *
48 * IQA, dssim=(structure)"dssim\,\ sink_1\=\(double\)0.053621271267184856\,\
49 * sink_2\=\(double\)0.0082939683976297474\;",
50 * time=(guint64)0;
51 *
52 * ## Example launch line
53 * |[
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.
57 *
58 */
59
60 #ifdef HAVE_CONFIG_H
61 #include "config.h"
62 #endif
63
64 #include "iqa.h"
65
66 #ifdef HAVE_DSSIM
67 #include "dssim.h"
68 #endif
69
70 GST_DEBUG_CATEGORY_STATIC (gst_iqa_debug);
71 #define GST_CAT_DEFAULT gst_iqa_debug
72
73 #define SINK_FORMATS " { AYUV, BGRA, ARGB, RGBA, ABGR, Y444, Y42B, YUY2, UYVY, "\
74 " YVYU, I420, YV12, NV12, NV21, Y41B, RGB, BGR, xRGB, xBGR, "\
75 " RGBx, BGRx } "
76
77 #define SRC_FORMAT " { RGBA } "
78 #define DEFAULT_DSSIM_ERROR_THRESHOLD -1.0
79
80 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
81 GST_PAD_SRC,
82 GST_PAD_ALWAYS,
83 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (SRC_FORMAT))
84 );
85
86 enum
87 {
88 PROP_0,
89 PROP_DO_SSIM,
90 PROP_SSIM_ERROR_THRESHOLD,
91 PROP_MODE,
92 PROP_LAST,
93 };
94
95 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
96 GST_PAD_SINK,
97 GST_PAD_REQUEST,
98 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (SINK_FORMATS))
99 );
100
101 /* Child proxy implementation */
102 static GObject *
gst_iqa_child_proxy_get_child_by_index(GstChildProxy * child_proxy,guint index)103 gst_iqa_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
104 guint index)
105 {
106 GstIqa *iqa = GST_IQA (child_proxy);
107 GObject *obj = NULL;
108
109 GST_OBJECT_LOCK (iqa);
110 obj = g_list_nth_data (GST_ELEMENT_CAST (iqa)->sinkpads, index);
111 if (obj)
112 gst_object_ref (obj);
113 GST_OBJECT_UNLOCK (iqa);
114
115 return obj;
116 }
117
118 static guint
gst_iqa_child_proxy_get_children_count(GstChildProxy * child_proxy)119 gst_iqa_child_proxy_get_children_count (GstChildProxy * child_proxy)
120 {
121 guint count = 0;
122 GstIqa *iqa = GST_IQA (child_proxy);
123
124 GST_OBJECT_LOCK (iqa);
125 count = GST_ELEMENT_CAST (iqa)->numsinkpads;
126 GST_OBJECT_UNLOCK (iqa);
127 GST_INFO_OBJECT (iqa, "Children Count: %d", count);
128
129 return count;
130 }
131
132 static void
gst_iqa_child_proxy_init(gpointer g_iface,gpointer iface_data)133 gst_iqa_child_proxy_init (gpointer g_iface, gpointer iface_data)
134 {
135 GstChildProxyInterface *iface = g_iface;
136
137 iface->get_child_by_index = gst_iqa_child_proxy_get_child_by_index;
138 iface->get_children_count = gst_iqa_child_proxy_get_children_count;
139 }
140
141 /**
142 * GstIqaMode:
143 * @GST_IQA_MODE_STRICT: Strict checks of the frames is enabled, this for
144 * example implies that an error will be posted in case all the streams don't
145 * have the exact same number of frames.
146 *
147 * Since: 1.18
148 */
149 typedef enum
150 {
151 GST_IQA_MODE_STRICT = (1 << 1),
152 } GstIqaMode;
153
154 #define GST_TYPE_IQA_MODE (gst_iqa_mode_flags_get_type())
155 static GType
gst_iqa_mode_flags_get_type(void)156 gst_iqa_mode_flags_get_type (void)
157 {
158 static const GFlagsValue values[] = {
159 {GST_IQA_MODE_STRICT, "Strict comparison of frames.", "strict"},
160 {0, NULL, NULL}
161 };
162 static GType id = 0;
163
164 if (g_once_init_enter ((gsize *) & id)) {
165 GType _id;
166
167 _id = g_flags_register_static ("GstIqaMode", values);
168
169 g_once_init_leave ((gsize *) & id, _id);
170 }
171
172 return id;
173 }
174
175 /* GstIqa */
176 #define gst_iqa_parent_class parent_class
177 G_DEFINE_TYPE_WITH_CODE (GstIqa, gst_iqa, GST_TYPE_VIDEO_AGGREGATOR,
178 G_IMPLEMENT_INTERFACE (GST_TYPE_CHILD_PROXY, gst_iqa_child_proxy_init);
179 GST_DEBUG_CATEGORY_INIT (gst_iqa_debug, "iqa", 0, "iqa");
180 );
181 GST_ELEMENT_REGISTER_DEFINE (iqa, "iqa", GST_RANK_PRIMARY, GST_TYPE_IQA);
182
183 #ifdef HAVE_DSSIM
184 inline static unsigned char
to_byte(float in)185 to_byte (float in)
186 {
187 if (in <= 0)
188 return 0;
189 if (in >= 255.f / 256.f)
190 return 255;
191 return in * 256.f;
192 }
193
194 static gboolean
do_dssim(GstIqa * self,GstVideoFrame * ref,GstVideoFrame * cmp,GstBuffer * outbuf,GstStructure * msg_structure,gchar * padname)195 do_dssim (GstIqa * self, GstVideoFrame * ref, GstVideoFrame * cmp,
196 GstBuffer * outbuf, GstStructure * msg_structure, gchar * padname)
197 {
198 dssim_attr *attr;
199 gint y;
200 unsigned char **ptrs, **ptrs2;
201 GstMapInfo ref_info;
202 GstMapInfo cmp_info;
203 GstMapInfo out_info;
204 dssim_image *ref_image;
205 dssim_image *cmp_image;
206 double dssim;
207 dssim_ssim_map map_meta;
208 float *map;
209 gint i;
210 dssim_rgba *out;
211 GstStructure *dssim_structure;
212 gboolean ret = TRUE;
213
214 if (ref->info.width != cmp->info.width ||
215 ref->info.height != cmp->info.height) {
216 GST_OBJECT_UNLOCK (self);
217
218 GST_ELEMENT_ERROR (self, STREAM, FAILED,
219 ("Video streams do not have the same sizes (add videoscale"
220 " and force the sizes to be equal on all sink pads.)"),
221 ("Reference width %d - compared width: %d. "
222 "Reference height %d - compared height: %d",
223 ref->info.width, cmp->info.width, ref->info.height,
224 cmp->info.height));
225
226 GST_OBJECT_LOCK (self);
227 return FALSE;
228 }
229
230 gst_structure_get (msg_structure, "dssim", GST_TYPE_STRUCTURE,
231 &dssim_structure, NULL);
232
233 attr = dssim_create_attr ();
234 dssim_set_save_ssim_maps (attr, 1, 1);
235
236 gst_buffer_map (ref->buffer, &ref_info, GST_MAP_READ);
237 gst_buffer_map (cmp->buffer, &cmp_info, GST_MAP_READ);
238 gst_buffer_map (outbuf, &out_info, GST_MAP_WRITE);
239 out = (dssim_rgba *) out_info.data;
240
241 ptrs = g_malloc (sizeof (char **) * ref->info.height);
242
243 for (y = 0; y < ref->info.height; y++) {
244 ptrs[y] = ref_info.data + (ref->info.width * 4 * y);
245 }
246
247 ref_image =
248 dssim_create_image (attr, ptrs, DSSIM_RGBA, ref->info.width,
249 ref->info.height, 0.45455);
250
251 ptrs2 = g_malloc (sizeof (char **) * cmp->info.height);
252
253 for (y = 0; y < cmp->info.height; y++) {
254 ptrs2[y] = cmp_info.data + (cmp->info.width * 4 * y);
255 }
256
257 cmp_image =
258 dssim_create_image (attr, ptrs2, DSSIM_RGBA, cmp->info.width,
259 cmp->info.height, 0.45455);
260 dssim = dssim_compare (attr, ref_image, cmp_image);
261
262 map_meta = dssim_pop_ssim_map (attr, 0, 0);
263
264 /* Comparing floats... should not be a big deal anyway */
265 if (self->ssim_threshold > 0 && dssim > self->ssim_threshold) {
266 /* We do not really care about our state... we are going to error ou
267 * anyway! */
268 GST_OBJECT_UNLOCK (self);
269
270 GST_ELEMENT_ERROR (self, STREAM, FAILED,
271 ("Dssim check failed on %s at %"
272 GST_TIME_FORMAT " with dssim %f > %f",
273 padname,
274 GST_TIME_ARGS (GST_AGGREGATOR_PAD (GST_AGGREGATOR (self)->
275 srcpad)->segment.position), dssim, self->ssim_threshold),
276 (NULL));
277
278 GST_OBJECT_LOCK (self);
279
280 ret = FALSE;
281 goto cleanup_return;
282 }
283
284 if (dssim > self->max_dssim) {
285 map = map_meta.data;
286
287 for (i = 0; i < map_meta.width * map_meta.height; i++) {
288 const float max = 1.0 - map[i];
289 const float maxsq = max * max;
290 out[i] = (dssim_rgba) {
291 .r = to_byte (max * 3.0),.g = to_byte (maxsq * 6.0),.b =
292 to_byte (max / ((1.0 - map_meta.dssim) * 4.0)),.a = 255,};
293 }
294 self->max_dssim = dssim;
295 }
296
297 gst_structure_set (dssim_structure, padname, G_TYPE_DOUBLE, dssim, NULL);
298 gst_structure_set (msg_structure, "dssim", GST_TYPE_STRUCTURE,
299 dssim_structure, NULL);
300
301 ret = TRUE;
302
303 cleanup_return:
304
305 gst_structure_free (dssim_structure);
306
307 free (map_meta.data);
308 g_free (ptrs);
309 g_free (ptrs2);
310 gst_buffer_unmap (ref->buffer, &ref_info);
311 gst_buffer_unmap (cmp->buffer, &cmp_info);
312 gst_buffer_unmap (outbuf, &out_info);
313 dssim_dealloc_image (ref_image);
314 dssim_dealloc_image (cmp_image);
315 dssim_dealloc_attr (attr);
316
317 return ret;
318 }
319 #endif
320
321 static gboolean
compare_frames(GstIqa * self,GstVideoFrame * ref,GstVideoFrame * cmp,GstBuffer * outbuf,GstStructure * msg_structure,gchar * padname)322 compare_frames (GstIqa * self, GstVideoFrame * ref, GstVideoFrame * cmp,
323 GstBuffer * outbuf, GstStructure * msg_structure, gchar * padname)
324 {
325 #ifdef HAVE_DSSIM
326 if (self->do_dssim) {
327 if (!do_dssim (self, ref, cmp, outbuf, msg_structure, padname))
328 return FALSE;
329 }
330 #endif
331
332 return TRUE;
333 }
334
335 static GstFlowReturn
gst_iqa_aggregate_frames(GstVideoAggregator * vagg,GstBuffer * outbuf)336 gst_iqa_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
337 {
338 GList *l;
339 GstVideoFrame *ref_frame = NULL;
340 GstIqa *self = GST_IQA (vagg);
341 GstStructure *msg_structure = gst_structure_new_empty ("IQA");
342 GstMessage *m = gst_message_new_element (GST_OBJECT (self), msg_structure);
343 GstAggregator *agg = GST_AGGREGATOR (vagg);
344
345 if (self->do_dssim) {
346 gst_structure_set (msg_structure, "dssim", GST_TYPE_STRUCTURE,
347 gst_structure_new_empty ("dssim"), NULL);
348 self->max_dssim = 0.0;
349 }
350
351 GST_OBJECT_LOCK (vagg);
352 for (l = GST_ELEMENT (vagg)->sinkpads; l; l = l->next) {
353 GstVideoAggregatorPad *pad = l->data;
354 GstVideoFrame *prepared_frame =
355 gst_video_aggregator_pad_get_prepared_frame (pad);
356
357 if (prepared_frame != NULL) {
358 if (!ref_frame) {
359 ref_frame = prepared_frame;
360 } else {
361 gboolean res;
362 gchar *padname = gst_pad_get_name (pad);
363 GstVideoFrame *cmp_frame = prepared_frame;
364
365 res = compare_frames (self, ref_frame, cmp_frame, outbuf, msg_structure,
366 padname);
367 g_free (padname);
368
369 if (!res)
370 goto failed;
371 }
372 } else if ((self->mode & GST_IQA_MODE_STRICT) && ref_frame) {
373 GST_OBJECT_UNLOCK (vagg);
374
375 GST_ELEMENT_ERROR (self, STREAM, FAILED,
376 ("All sources are supposed to have the same number of buffers"
377 " but got no buffer matching %" GST_PTR_FORMAT " on pad: %"
378 GST_PTR_FORMAT, outbuf, pad), (NULL));
379
380 GST_OBJECT_LOCK (vagg);
381 break;
382 }
383 }
384
385 GST_OBJECT_UNLOCK (vagg);
386
387 /* We only post the message here, because we can't post it while the object
388 * is locked.
389 */
390 gst_structure_set (msg_structure, "time", GST_TYPE_CLOCK_TIME,
391 GST_AGGREGATOR_PAD (agg->srcpad)->segment.position, NULL);
392 gst_element_post_message (GST_ELEMENT (self), m);
393 return GST_FLOW_OK;
394
395 failed:
396 GST_OBJECT_UNLOCK (vagg);
397
398 return GST_FLOW_ERROR;
399 }
400
401 static void
_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)402 _set_property (GObject * object, guint prop_id, const GValue * value,
403 GParamSpec * pspec)
404 {
405 GstIqa *self = GST_IQA (object);
406
407 switch (prop_id) {
408 case PROP_DO_SSIM:
409 GST_OBJECT_LOCK (self);
410 self->do_dssim = g_value_get_boolean (value);
411 GST_OBJECT_UNLOCK (self);
412 break;
413 case PROP_SSIM_ERROR_THRESHOLD:
414 GST_OBJECT_LOCK (self);
415 self->ssim_threshold = g_value_get_double (value);
416 GST_OBJECT_UNLOCK (self);
417 break;
418 case PROP_MODE:
419 GST_OBJECT_LOCK (self);
420 self->mode = g_value_get_flags (value);
421 GST_OBJECT_UNLOCK (self);
422 break;
423 default:
424 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
425 break;
426 }
427 }
428
429 static void
_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)430 _get_property (GObject * object,
431 guint prop_id, GValue * value, GParamSpec * pspec)
432 {
433 GstIqa *self = GST_IQA (object);
434
435 switch (prop_id) {
436 case PROP_DO_SSIM:
437 GST_OBJECT_LOCK (self);
438 g_value_set_boolean (value, self->do_dssim);
439 GST_OBJECT_UNLOCK (self);
440 break;
441 case PROP_SSIM_ERROR_THRESHOLD:
442 GST_OBJECT_LOCK (self);
443 g_value_set_double (value, self->ssim_threshold);
444 GST_OBJECT_UNLOCK (self);
445 break;
446 case PROP_MODE:
447 GST_OBJECT_LOCK (self);
448 g_value_set_flags (value, self->mode);
449 GST_OBJECT_UNLOCK (self);
450 break;
451 default:
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453 break;
454 }
455 }
456
457 /* GObject boilerplate */
458 static void
gst_iqa_class_init(GstIqaClass * klass)459 gst_iqa_class_init (GstIqaClass * klass)
460 {
461 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
462 GstElementClass *gstelement_class = (GstElementClass *) klass;
463 GstVideoAggregatorClass *videoaggregator_class =
464 (GstVideoAggregatorClass *) klass;
465
466 videoaggregator_class->aggregate_frames = gst_iqa_aggregate_frames;
467
468 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
469 &src_factory, GST_TYPE_AGGREGATOR_PAD);
470 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
471 &sink_factory, GST_TYPE_VIDEO_AGGREGATOR_CONVERT_PAD);
472
473 gobject_class->set_property = _set_property;
474 gobject_class->get_property = _get_property;
475
476 #ifdef HAVE_DSSIM
477 g_object_class_install_property (gobject_class, PROP_DO_SSIM,
478 g_param_spec_boolean ("do-dssim", "do-dssim",
479 "Run structural similarity checks", FALSE, G_PARAM_READWRITE));
480
481 g_object_class_install_property (gobject_class, PROP_SSIM_ERROR_THRESHOLD,
482 g_param_spec_double ("dssim-error-threshold", "dssim error threshold",
483 "dssim value over which the element will post an error message on the bus."
484 " A value < 0.0 means 'disabled'.",
485 -1.0, G_MAXDOUBLE, DEFAULT_DSSIM_ERROR_THRESHOLD, G_PARAM_READWRITE));
486 #endif
487
488 /**
489 * iqa:mode:
490 *
491 * Controls the frame comparison mode.
492 *
493 * Since: 1.18
494 */
495 g_object_class_install_property (gobject_class, PROP_MODE,
496 g_param_spec_flags ("mode", "IQA mode",
497 "Controls the frame comparison mode.", GST_TYPE_IQA_MODE,
498 0, G_PARAM_READWRITE));
499
500 gst_type_mark_as_plugin_api (GST_TYPE_IQA_MODE, 0);
501
502 gst_element_class_set_static_metadata (gstelement_class, "Iqa",
503 "Filter/Analyzer/Video",
504 "Provides various Image Quality Assessment metrics",
505 "Mathieu Duponchelle <mathieu.duponchelle@collabora.co.uk>");
506 }
507
508 static void
gst_iqa_init(GstIqa * self)509 gst_iqa_init (GstIqa * self)
510 {
511 }
512
513 static gboolean
plugin_init(GstPlugin * plugin)514 plugin_init (GstPlugin * plugin)
515 {
516 return GST_ELEMENT_REGISTER (iqa, plugin);
517 }
518
519 // FIXME: effective iqa plugin license should be AGPL3+ !
520 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
521 GST_VERSION_MINOR,
522 iqa,
523 "Iqa", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
524 GST_PACKAGE_ORIGIN)
525