Various bits mostly related to colour conversions.
[dcpomatic.git] / src / lib / config.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 #include <sstream>
21 #include <cstdlib>
22 #include <fstream>
23 #include <glib.h>
24 #include <boost/filesystem.hpp>
25 #include <libdcp/colour_matrix.h>
26 #include <libcxml/cxml.h>
27 #include "config.h"
28 #include "server.h"
29 #include "scaler.h"
30 #include "filter.h"
31 #include "ratio.h"
32 #include "dcp_content_type.h"
33 #include "sound_processor.h"
34 #include "colour_conversion.h"
35
36 #include "i18n.h"
37
38 using std::vector;
39 using std::ifstream;
40 using std::string;
41 using std::ofstream;
42 using std::list;
43 using std::max;
44 using boost::shared_ptr;
45 using boost::lexical_cast;
46 using boost::optional;
47
48 Config* Config::_instance = 0;
49
50 /** Construct default configuration */
51 Config::Config ()
52         : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
53         , _server_port (6192)
54         , _tms_path (N_("."))
55         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
56         , _default_still_length (10)
57         , _default_container (Ratio::from_id ("185"))
58         , _default_dcp_content_type (DCPContentType::from_dci_name ("TST"))
59         , _default_j2k_bandwidth (200000000)
60 {
61         _allowed_dcp_frame_rates.push_back (24);
62         _allowed_dcp_frame_rates.push_back (25);
63         _allowed_dcp_frame_rates.push_back (30);
64         _allowed_dcp_frame_rates.push_back (48);
65         _allowed_dcp_frame_rates.push_back (50);
66         _allowed_dcp_frame_rates.push_back (60);
67
68         _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::xyz_to_rgb, 2.6));
69         _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, libdcp::colour_matrix::xyz_to_rgb, 2.6));
70 }
71
72 void
73 Config::read ()
74 {
75         if (!boost::filesystem::exists (file (false))) {
76                 read_old_metadata ();
77                 return;
78         }
79
80         cxml::File f (file (false), "Config");
81         optional<string> c;
82
83         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
84         _default_directory = f.string_child ("DefaultDirectory");
85         _server_port = f.number_child<int> ("ServerPort");
86         
87         list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
88         for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
89                 _servers.push_back (ServerDescription (*i));
90         }
91
92         _tms_ip = f.string_child ("TMSIP");
93         _tms_path = f.string_child ("TMSPath");
94         _tms_user = f.string_child ("TMSUser");
95         _tms_password = f.string_child ("TMSPassword");
96
97         c = f.optional_string_child ("SoundProcessor");
98         if (c) {
99                 _sound_processor = SoundProcessor::from_id (c.get ());
100         }
101
102         _language = f.optional_string_child ("Language");
103
104         c = f.optional_string_child ("DefaultContainer");
105         if (c) {
106                 _default_container = Ratio::from_id (c.get ());
107         }
108
109         c = f.optional_string_child ("DefaultDCPContentType");
110         if (c) {
111                 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
112         }
113
114         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
115         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
116
117         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
118         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
119         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
120
121         list<shared_ptr<cxml::Node> > cc = f.node_children ("ColourConversion");
122
123         if (!cc.empty ()) {
124                 _colour_conversions.clear ();
125         }
126         
127         for (list<shared_ptr<cxml::Node> >::iterator i = cc.begin(); i != cc.end(); ++i) {
128                 _colour_conversions.push_back (PresetColourConversion (*i));
129         }
130 }
131
132 void
133 Config::read_old_metadata ()
134 {
135         ifstream f (file(true).c_str ());
136         string line;
137         while (getline (f, line)) {
138                 if (line.empty ()) {
139                         continue;
140                 }
141
142                 if (line[0] == '#') {
143                         continue;
144                 }
145
146                 size_t const s = line.find (' ');
147                 if (s == string::npos) {
148                         continue;
149                 }
150                 
151                 string const k = line.substr (0, s);
152                 string const v = line.substr (s + 1);
153
154                 if (k == N_("num_local_encoding_threads")) {
155                         _num_local_encoding_threads = atoi (v.c_str ());
156                 } else if (k == N_("default_directory")) {
157                         _default_directory = v;
158                 } else if (k == N_("server_port")) {
159                         _server_port = atoi (v.c_str ());
160                 } else if (k == N_("server")) {
161                         optional<ServerDescription> server = ServerDescription::create_from_metadata (v);
162                         if (server) {
163                                 _servers.push_back (server.get ());
164                         }
165                 } else if (k == N_("tms_ip")) {
166                         _tms_ip = v;
167                 } else if (k == N_("tms_path")) {
168                         _tms_path = v;
169                 } else if (k == N_("tms_user")) {
170                         _tms_user = v;
171                 } else if (k == N_("tms_password")) {
172                         _tms_password = v;
173                 } else if (k == N_("sound_processor")) {
174                         _sound_processor = SoundProcessor::from_id (v);
175                 } else if (k == "language") {
176                         _language = v;
177                 } else if (k == "default_container") {
178                         _default_container = Ratio::from_id (v);
179                 } else if (k == "default_dcp_content_type") {
180                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
181                 } else if (k == "dcp_metadata_issuer") {
182                         _dcp_metadata.issuer = v;
183                 } else if (k == "dcp_metadata_creator") {
184                         _dcp_metadata.creator = v;
185                 } else if (k == "dcp_metadata_issue_date") {
186                         _dcp_metadata.issue_date = v;
187                 }
188
189                 _default_dci_metadata.read_old_metadata (k, v);
190         }
191 }
192
193 /** @return Filename to write configuration to */
194 string
195 Config::file (bool old) const
196 {
197         boost::filesystem::path p;
198         p /= g_get_user_config_dir ();
199         boost::system::error_code ec;
200         boost::filesystem::create_directory (p, ec);
201         if (old) {
202                 p /= ".dvdomatic";
203         } else {
204                 p /= "dcpomatic.xml";
205         }
206         return p.string ();
207 }
208
209 /** @return Singleton instance */
210 Config *
211 Config::instance ()
212 {
213         if (_instance == 0) {
214                 _instance = new Config;
215                 try {
216                         _instance->read ();
217                 } catch (...) {
218                         /* configuration load failed; never mind, just
219                            stick with the default.
220                         */
221                 }
222         }
223
224         return _instance;
225 }
226
227 /** Write our configuration to disk */
228 void
229 Config::write () const
230 {
231         xmlpp::Document doc;
232         xmlpp::Element* root = doc.create_root_node ("Config");
233
234         root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
235         root->add_child("DefaultDirectory")->add_child_text (_default_directory);
236         root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
237         
238         for (vector<ServerDescription>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
239                 i->as_xml (root->add_child ("Server"));
240         }
241
242         root->add_child("TMSIP")->add_child_text (_tms_ip);
243         root->add_child("TMSPath")->add_child_text (_tms_path);
244         root->add_child("TMSUser")->add_child_text (_tms_user);
245         root->add_child("TMSPassword")->add_child_text (_tms_password);
246         if (_sound_processor) {
247                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
248         }
249         if (_language) {
250                 root->add_child("Language")->add_child_text (_language.get());
251         }
252         if (_default_container) {
253                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
254         }
255         if (_default_dcp_content_type) {
256                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
257         }
258         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
259         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
260
261         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
262
263         root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
264         root->add_child("DefaultJ2KBandwidth")->add_child_text (lexical_cast<string> (_default_j2k_bandwidth));
265
266         for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
267                 i->as_xml (root->add_child ("ColourConversion"));
268         }
269
270         doc.write_to_file_formatted (file (false));
271 }
272
273 string
274 Config::default_directory_or (string a) const
275 {
276         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
277                 return a;
278         }
279
280         return _default_directory;
281 }
282
283 void
284 Config::drop ()
285 {
286         delete _instance;
287         _instance = 0;
288 }