d0e8c8fa4e3e825db89604c32489cb00718e76cb
[dcpomatic.git] / src / lib / filter.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program 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
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/filter.cc
21  *  @brief A class to describe one of FFmpeg's video or audio filters.
22  */
23
24 #include "filter.h"
25 extern "C" {
26 #include <libavfilter/avfilter.h>
27 }
28 #include <boost/foreach.hpp>
29
30 #include "i18n.h"
31
32 using namespace std;
33
34 vector<Filter const *> Filter::_filters;
35
36 /** @param i Our id.
37  *  @param n User-visible name.
38  *  @param c User-visible category.
39  *  @param f String for a FFmpeg filter descriptor.
40  */
41 Filter::Filter (string i, string n, string c, string f)
42         : _id (i)
43         , _name (n)
44         , _category (c)
45         , _ffmpeg (f)
46 {
47
48 }
49
50 /** @return All available filters */
51 vector<Filter const *>
52 Filter::all ()
53 {
54         return _filters;
55 }
56
57 /** Set up the static _filters vector; must be called before from_*
58  *  methods are used.
59  */
60 void
61 Filter::setup_filters ()
62 {
63         /* Note: "none" is a magic id name, so don't use it here */
64
65         maybe_add (N_("mcdeint"),   _("Motion compensating deinterlacer"), _("De-interlacing"),  N_("mcdeint"));
66         maybe_add (N_("kerndeint"), _("Kernel deinterlacer"),              _("De-interlacing"),  N_("kerndeint"));
67         maybe_add (N_("yadif"),     _("Yet Another Deinterlacing Filter"), _("De-interlacing"),  N_("yadif"));
68         maybe_add (N_("gradfun"),   _("Gradient debander"),                _("Misc"),            N_("gradfun"));
69         maybe_add (N_("unsharp"),   _("Unsharp mask and Gaussian blur"),   _("Misc"),            N_("unsharp"));
70         maybe_add (N_("denoise3d"), _("3D denoiser"),                      _("Noise reduction"), N_("denoise3d"));
71         maybe_add (N_("hqdn3d"),    _("High quality 3D denoiser"),         _("Noise reduction"), N_("hqdn3d"));
72         maybe_add (N_("telecine"),  _("Telecine filter"),                  _("Misc"),            N_("telecine"));
73         maybe_add (N_("ow"),        _("Overcomplete wavelet denoiser"),    _("Noise reduction"), N_("mp=ow"));
74 }
75
76 void
77 Filter::maybe_add (string i, string n, string c, string f)
78 {
79         if (avfilter_get_by_name (i.c_str())) {
80                 _filters.push_back (new Filter (i, n, c, f));
81         }
82 }
83
84 /** @param filters Set of filters.
85  *  @return String to pass to FFmpeg for the video filters.
86  */
87 string
88 Filter::ffmpeg_string (vector<Filter const *> const & filters)
89 {
90         string ff;
91
92         BOOST_FOREACH (Filter const * i, filters) {
93                 if (!ff.empty ()) {
94                         ff += N_(",");
95                 }
96                 ff += i->ffmpeg ();
97         }
98
99         return ff;
100 }
101
102 /** @param d Our id.
103  *  @return Corresponding Filter, or 0.
104  */
105 Filter const *
106 Filter::from_id (string d)
107 {
108         vector<Filter const *>::iterator i = _filters.begin ();
109         while (i != _filters.end() && (*i)->id() != d) {
110                 ++i;
111         }
112
113         if (i == _filters.end ()) {
114                 return 0;
115         }
116
117         return *i;
118 }