Merge master.
[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 post-processing filters.
22  */
23
24 #include "filter.h"
25 extern "C" {
26 #include <libavfilter/avfilter.h>
27 #include <libpostproc/postprocess.h>
28 }
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 v String for a FFmpeg video filter descriptor, or "".
40  *  @param p String for a FFmpeg post-processing descriptor, or "".
41  */
42 Filter::Filter (string i, string n, string c, string v, string p)
43         : _id (i)
44         , _name (n)
45         , _category (c)
46         , _vf (v)
47         , _pp (p)
48 {
49
50 }
51
52 /** @return All available filters */
53 vector<Filter const *>
54 Filter::all ()
55 {
56         return _filters;
57 }
58
59
60 /** Set up the static _filters vector; must be called before from_*
61  *  methods are used.
62  */
63 void
64 Filter::setup_filters ()
65 {
66         /* Note: "none" is a magic id name, so don't use it here */
67            
68         maybe_add (N_("pphb"),      _("Horizontal deblocking filter"),                _("De-blocking"),     N_(""),          N_("hb"));
69         maybe_add (N_("ppvb"),      _("Vertical deblocking filter"),                  _("De-blocking"),     N_(""),          N_("vb"));
70         maybe_add (N_("ppha"),      _("Horizontal deblocking filter A"),              _("De-blocking"),     N_(""),          N_("ha"));
71         maybe_add (N_("ppva"),      _("Vertical deblocking filter A"),                _("De-blocking"),     N_(""),          N_("va"));
72         maybe_add (N_("pph1"),      _("Experimental horizontal deblocking filter 1"), _("De-blocking"),     N_(""),          N_("h1"));
73         maybe_add (N_("pphv"),      _("Experimental vertical deblocking filter 1"),   _("De-blocking"),     N_(""),          N_("v1"));
74         maybe_add (N_("ppdr"),      _("Deringing filter"),                            _("Misc"),            N_(""),          N_("dr"));
75         maybe_add (N_("pplb"),      _("Linear blend deinterlacer"),                   _("De-interlacing"),  N_(""),          N_("lb"));
76         maybe_add (N_("ppli"),      _("Linear interpolating deinterlacer"),           _("De-interlacing"),  N_(""),          N_("li"));
77         maybe_add (N_("ppci"),      _("Cubic interpolating deinterlacer"),            _("De-interlacing"),  N_(""),          N_("ci"));
78         maybe_add (N_("ppmd"),      _("Median deinterlacer"),                         _("De-interlacing"),  N_(""),          N_("md"));
79         maybe_add (N_("ppfd"),      _("FFMPEG deinterlacer"),                         _("De-interlacing"),  N_(""),          N_("fd"));
80         maybe_add (N_("ppl5"),      _("FIR low-pass deinterlacer"),                   _("De-interlacing"),  N_(""),          N_("l5"));
81         maybe_add (N_("mcdeint"),   _("Motion compensating deinterlacer"),            _("De-interlacing"),  N_("mcdeint"),   N_(""));
82         maybe_add (N_("kerndeint"), _("Kernel deinterlacer"),                         _("De-interlacing"),  N_("kerndeint"), N_(""));
83         maybe_add (N_("yadif"),     _("Yet Another Deinterlacing Filter"),            _("De-interlacing"),  N_("yadif"),     N_(""));
84         maybe_add (N_("pptn"),      _("Temporal noise reducer"),                      _("Noise reduction"), N_(""),          N_("tn"));
85         maybe_add (N_("ppfq"),      _("Force quantizer"),                             _("Misc"),            N_(""),          N_("fq"));
86         maybe_add (N_("gradfun"),   _("Gradient debander"),                           _("Misc"),            N_("gradfun"),   N_(""));
87         maybe_add (N_("unsharp"),   _("Unsharp mask and Gaussian blur"),              _("Misc"),            N_("unsharp"),   N_(""));
88         maybe_add (N_("denoise3d"), _("3D denoiser"),                                 _("Noise reduction"), N_("denoise3d"), N_(""));
89         maybe_add (N_("hqdn3d"),    _("High quality 3D denoiser"),                    _("Noise reduction"), N_("hqdn3d"),    N_(""));
90         maybe_add (N_("telecine"),  _("Telecine filter"),                             _("Misc"),            N_("telecine"),  N_(""));
91         maybe_add (N_("ow"),        _("Overcomplete wavelet denoiser"),               _("Noise reduction"), N_("mp=ow"),     N_(""));
92 }
93
94 void
95 Filter::maybe_add (string i, string n, string c, string v, string p)
96 {
97         if (!v.empty ()) {
98                 if (avfilter_get_by_name (i.c_str())) {
99                         _filters.push_back (new Filter (i, n, c, v, p));
100                 }
101         } else if (!p.empty ()) {
102                 pp_mode* m = pp_get_mode_by_name_and_quality (p.c_str(), PP_QUALITY_MAX);
103                 if (m) {
104                         _filters.push_back (new Filter (i, n, c, v, p));
105                         pp_free_mode (m);
106                 }
107         }
108 }
109
110 /** @param filters Set of filters.
111  *  @return A pair; .first is a string to pass to FFmpeg for the video filters,
112  *  .second is a string to pass for the post-processors.
113  */
114 pair<string, string>
115 Filter::ffmpeg_strings (vector<Filter const *> const & filters)
116 {
117         string vf;
118         string pp;
119
120         for (vector<Filter const *>::const_iterator i = filters.begin(); i != filters.end(); ++i) {
121                 if (!(*i)->vf().empty ()) {
122                         if (!vf.empty ()) {
123                                 vf += N_(",");
124                         }
125                         vf += (*i)->vf ();
126                 }
127                 
128                 if (!(*i)->pp().empty ()) {
129                         if (!pp.empty()) {
130                                 pp += N_(",");
131                         }
132                         pp += (*i)->pp ();
133                 }
134         }
135
136         return make_pair (vf, pp);
137 }
138
139 /** @param d Our id.
140  *  @return Corresponding Filter, or 0.
141  */
142 Filter const *
143 Filter::from_id (string d)
144 {
145         vector<Filter const *>::iterator i = _filters.begin ();
146         while (i != _filters.end() && (*i)->id() != d) {
147                 ++i;
148         }
149
150         if (i == _filters.end ()) {
151                 return 0;
152         }
153
154         return *i;
155 }
156