Cleanup: handle Filter objects by value rather than by reference.
[dcpomatic.git] / src / lib / filter.cc
index fbb532b321123121355cc13be9d27131fe9e39d1..7db329b00fca5c46b13c867ae17caf3337ccea59 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
+
 /** @file src/filter.cc
  *  @brief A class to describe one of FFmpeg's video or audio filters.
  */
 
+
+#include "dcpomatic_assert.h"
 #include "filter.h"
+#include <dcp/warnings.h>
+LIBDCP_DISABLE_WARNINGS
 extern "C" {
 #include <libavfilter/avfilter.h>
 }
-#include <boost/foreach.hpp>
-#include <iostream>
+LIBDCP_ENABLE_WARNINGS
+#include <algorithm>
 
 #include "i18n.h"
 
+
 using namespace std;
+using boost::optional;
+
+
+vector<Filter> Filter::_filters;
 
-vector<Filter const *> Filter::_filters;
 
 /** @param i Our id.
  *  @param n User-visible name.
@@ -49,13 +58,15 @@ Filter::Filter (string i, string n, string c, string f)
 
 }
 
+
 /** @return All available filters */
-vector<Filter const *>
+vector<Filter>
 Filter::all ()
 {
        return _filters;
 }
 
+
 /** Set up the static _filters vector; must be called before from_*
  *  methods are used.
  */
@@ -71,6 +82,7 @@ Filter::setup_filters ()
        maybe_add (N_("mcdeint"),     _("Motion compensating deinterlacer"), _("De-interlacing"),  N_("mcdeint"));
        maybe_add (N_("kerndeint"),   _("Kernel deinterlacer"),              _("De-interlacing"),  N_("kerndeint"));
        maybe_add (N_("yadif"),       _("Yet Another Deinterlacing Filter"), _("De-interlacing"),  N_("yadif"));
+       maybe_add (N_("bwdif"),       _("Bob Weaver Deinterlacing Filter"),  _("De-interlacing"),  N_("bwdif"));
        maybe_add (N_("weave"),       _("Weave filter"),                     _("De-interlacing"),  N_("weave"));
        maybe_add (N_("gradfun"),     _("Gradient debander"),                _("Misc"),            N_("gradfun"));
        maybe_add (N_("unsharp"),     _("Unsharp mask and Gaussian blur"),   _("Misc"),            N_("unsharp"));
@@ -80,6 +92,7 @@ Filter::setup_filters ()
        maybe_add (N_("ow"),          _("Overcomplete wavelet denoiser"),    _("Noise reduction"), N_("mp=ow"));
 }
 
+
 void
 Filter::maybe_add (string i, string n, string c, string f)
 {
@@ -89,43 +102,73 @@ Filter::maybe_add (string i, string n, string c, string f)
                check_name = check_name.substr (0, end);
        }
 
-       if (avfilter_get_by_name (check_name.c_str())) {
-               _filters.push_back (new Filter (i, n, c, f));
+       if (avfilter_get_by_name(check_name.c_str())) {
+               _filters.push_back (Filter(i, n, c, f));
        }
 }
 
+
 /** @param filters Set of filters.
  *  @return String to pass to FFmpeg for the video filters.
  */
 string
-Filter::ffmpeg_string (vector<Filter const *> const & filters)
+Filter::ffmpeg_string(vector<Filter> const& filters)
 {
        string ff;
 
-       BOOST_FOREACH (Filter const * i, filters) {
+       for (auto const& i: filters) {
                if (!ff.empty ()) {
                        ff += N_(",");
                }
-               ff += i->ffmpeg ();
+               ff += i.ffmpeg();
        }
 
        return ff;
 }
 
+
 /** @param d Our id.
- *  @return Corresponding Filter, or 0.
+ *  @return Corresponding Filter, if found.
  */
-Filter const *
-Filter::from_id (string d)
+optional<Filter>
+Filter::from_id(string id)
 {
-       vector<Filter const *>::iterator i = _filters.begin ();
-       while (i != _filters.end() && (*i)->id() != d) {
-               ++i;
+       auto iter = std::find_if(_filters.begin(), _filters.end(), [id](Filter const& filter) { return filter.id() == id; });
+       if (iter == _filters.end()) {
+               return {};
+       }
+       return *iter;
+}
+
+
+bool
+operator==(Filter const& a, Filter const& b)
+{
+       return a.id() == b.id() && a.name() == b.name() && a.category() == b.category() && a.ffmpeg() == b.ffmpeg();
+}
+
+
+bool
+operator!=(Filter const& a, Filter const& b)
+{
+       return a.id() != b.id() || a.name() != b.name() || a.category() != b.category() || a.ffmpeg() != b.ffmpeg();
+}
+
+
+bool
+operator<(Filter const& a, Filter const& b)
+{
+       if (a.id() != b.id()) {
+               return a.id() < b.id();
+       }
+
+       if (a.name() != b.name()) {
+               return a.name() < b.name();
        }
 
-       if (i == _filters.end ()) {
-               return 0;
+       if (a.category() != b.category()) {
+               return a.category() < b.category();
        }
 
-       return *i;
+       return a.ffmpeg() < b.ffmpeg();
 }