Supporters update.
[dcpomatic.git] / src / lib / filter.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic 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
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file src/filter.cc
23  *  @brief A class to describe one of FFmpeg's video or audio filters.
24  */
25
26
27 #include "filter.h"
28 #include <dcp/warnings.h>
29 LIBDCP_DISABLE_WARNINGS
30 extern "C" {
31 #include <libavfilter/avfilter.h>
32 }
33 LIBDCP_ENABLE_WARNINGS
34
35 #include "i18n.h"
36
37
38 using namespace std;
39
40
41 vector<Filter> Filter::_filters;
42
43
44 /** @param i Our id.
45  *  @param n User-visible name.
46  *  @param c User-visible category.
47  *  @param f String for a FFmpeg filter descriptor.
48  */
49 Filter::Filter (string i, string n, string c, string f)
50         : _id (i)
51         , _name (n)
52         , _category (c)
53         , _ffmpeg (f)
54 {
55
56 }
57
58
59 /** @return All available filters */
60 vector<Filter const *>
61 Filter::all ()
62 {
63         vector<Filter const *> raw;
64         for (auto& filter: _filters) {
65                 raw.push_back (&filter);
66         }
67         return raw;
68 }
69
70
71 /** Set up the static _filters vector; must be called before from_*
72  *  methods are used.
73  */
74 void
75 Filter::setup_filters ()
76 {
77         /* Note: "none" is a magic id name, so don't use it here */
78
79         maybe_add (N_("vflip"),       _("Vertical flip"),                    _("Orientation"),     N_("vflip"));
80         maybe_add (N_("hflip"),       _("Horizontal flip"),                  _("Orientation"),     N_("hflip"));
81         maybe_add (N_("90clock"),     _("Rotate 90 degrees clockwise"),      _("Orientation"),     N_("transpose=dir=clock"));
82         maybe_add (N_("90anticlock"), _("Rotate 90 degrees anti-clockwise"), _("Orientation"),     N_("transpose=dir=cclock"));
83         maybe_add (N_("mcdeint"),     _("Motion compensating deinterlacer"), _("De-interlacing"),  N_("mcdeint"));
84         maybe_add (N_("kerndeint"),   _("Kernel deinterlacer"),              _("De-interlacing"),  N_("kerndeint"));
85         maybe_add (N_("yadif"),       _("Yet Another Deinterlacing Filter"), _("De-interlacing"),  N_("yadif"));
86         maybe_add (N_("bwdif"),       _("Bob Weaver Deinterlacing Filter"),  _("De-interlacing"),  N_("bwdif"));
87         maybe_add (N_("weave"),       _("Weave filter"),                     _("De-interlacing"),  N_("weave"));
88         maybe_add (N_("gradfun"),     _("Gradient debander"),                _("Misc"),            N_("gradfun"));
89         maybe_add (N_("unsharp"),     _("Unsharp mask and Gaussian blur"),   _("Misc"),            N_("unsharp"));
90         maybe_add (N_("denoise3d"),   _("3D denoiser"),                      _("Noise reduction"), N_("denoise3d"));
91         maybe_add (N_("hqdn3d"),      _("High quality 3D denoiser"),         _("Noise reduction"), N_("hqdn3d"));
92         maybe_add (N_("telecine"),    _("Telecine filter"),                  _("Misc"),            N_("telecine"));
93         maybe_add (N_("ow"),          _("Overcomplete wavelet denoiser"),    _("Noise reduction"), N_("mp=ow"));
94 }
95
96
97 void
98 Filter::maybe_add (string i, string n, string c, string f)
99 {
100         string check_name = f;
101         size_t end = check_name.find("=");
102         if (end != string::npos) {
103                 check_name = check_name.substr (0, end);
104         }
105
106         if (avfilter_get_by_name(check_name.c_str())) {
107                 _filters.push_back (Filter(i, n, c, f));
108         }
109 }
110
111
112 /** @param filters Set of filters.
113  *  @return String to pass to FFmpeg for the video filters.
114  */
115 string
116 Filter::ffmpeg_string (vector<Filter const *> const & filters)
117 {
118         string ff;
119
120         for (auto const i: filters) {
121                 if (!ff.empty ()) {
122                         ff += N_(",");
123                 }
124                 ff += i->ffmpeg ();
125         }
126
127         return ff;
128 }
129
130
131 /** @param d Our id.
132  *  @return Corresponding Filter, or 0.
133  */
134 Filter const *
135 Filter::from_id (string d)
136 {
137         auto i = _filters.begin ();
138         while (i != _filters.end() && i->id() != d) {
139                 ++i;
140         }
141
142         if (i == _filters.end ()) {
143                 return nullptr;
144         }
145
146         return &(*i);
147 }