Add support for vflip/hflip filters.
[dcpomatic.git] / src / lib / filter.cc
1 /*
2     Copyright (C) 2012 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 /** @file src/filter.cc
22  *  @brief A class to describe one of FFmpeg's video or audio filters.
23  */
24
25 #include "filter.h"
26 extern "C" {
27 #include <libavfilter/avfilter.h>
28 }
29 #include <boost/foreach.hpp>
30
31 #include "i18n.h"
32
33 using namespace std;
34
35 vector<Filter const *> Filter::_filters;
36
37 /** @param i Our id.
38  *  @param n User-visible name.
39  *  @param c User-visible category.
40  *  @param f String for a FFmpeg filter descriptor.
41  */
42 Filter::Filter (string i, string n, string c, string f)
43         : _id (i)
44         , _name (n)
45         , _category (c)
46         , _ffmpeg (f)
47 {
48
49 }
50
51 /** @return All available filters */
52 vector<Filter const *>
53 Filter::all ()
54 {
55         return _filters;
56 }
57
58 /** Set up the static _filters vector; must be called before from_*
59  *  methods are used.
60  */
61 void
62 Filter::setup_filters ()
63 {
64         /* Note: "none" is a magic id name, so don't use it here */
65
66         maybe_add (N_("vflip"),     _("Vertical flip"),                    _("Orientation"),     N_("vflip"));
67         maybe_add (N_("hflip"),     _("Horizontal flip"),                  _("Orientation"),     N_("hflip"));
68         maybe_add (N_("mcdeint"),   _("Motion compensating deinterlacer"), _("De-interlacing"),  N_("mcdeint"));
69         maybe_add (N_("kerndeint"), _("Kernel deinterlacer"),              _("De-interlacing"),  N_("kerndeint"));
70         maybe_add (N_("yadif"),     _("Yet Another Deinterlacing Filter"), _("De-interlacing"),  N_("yadif"));
71         maybe_add (N_("gradfun"),   _("Gradient debander"),                _("Misc"),            N_("gradfun"));
72         maybe_add (N_("unsharp"),   _("Unsharp mask and Gaussian blur"),   _("Misc"),            N_("unsharp"));
73         maybe_add (N_("denoise3d"), _("3D denoiser"),                      _("Noise reduction"), N_("denoise3d"));
74         maybe_add (N_("hqdn3d"),    _("High quality 3D denoiser"),         _("Noise reduction"), N_("hqdn3d"));
75         maybe_add (N_("telecine"),  _("Telecine filter"),                  _("Misc"),            N_("telecine"));
76         maybe_add (N_("ow"),        _("Overcomplete wavelet denoiser"),    _("Noise reduction"), N_("mp=ow"));
77 }
78
79 void
80 Filter::maybe_add (string i, string n, string c, string f)
81 {
82         if (avfilter_get_by_name (i.c_str())) {
83                 _filters.push_back (new Filter (i, n, c, f));
84         }
85 }
86
87 /** @param filters Set of filters.
88  *  @return String to pass to FFmpeg for the video filters.
89  */
90 string
91 Filter::ffmpeg_string (vector<Filter const *> const & filters)
92 {
93         string ff;
94
95         BOOST_FOREACH (Filter const * i, filters) {
96                 if (!ff.empty ()) {
97                         ff += N_(",");
98                 }
99                 ff += i->ffmpeg ();
100         }
101
102         return ff;
103 }
104
105 /** @param d Our id.
106  *  @return Corresponding Filter, or 0.
107  */
108 Filter const *
109 Filter::from_id (string d)
110 {
111         vector<Filter const *>::iterator i = _filters.begin ();
112         while (i != _filters.end() && (*i)->id() != d) {
113                 ++i;
114         }
115
116         if (i == _filters.end ()) {
117                 return 0;
118         }
119
120         return *i;
121 }