Apply another fix for a weird compilation error on old compilers.
[dcpomatic.git] / src / lib / create_cli.cc
1 /*
2     Copyright (C) 2019-2022 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 #include "compose.hpp"
23 #include "config.h"
24 #include "create_cli.h"
25 #include "dcp_content_type.h"
26 #include "dcpomatic_log.h"
27 #include "film.h"
28 #include "ratio.h"
29 #include <dcp/raw_convert.h>
30 #include <iostream>
31 #include <string>
32
33
34 using std::cout;
35 using std::make_shared;
36 using std::shared_ptr;
37 using std::string;
38 using std::vector;
39 using boost::optional;
40
41
42 string CreateCLI::_help =
43         "\nSyntax: %1 [OPTION] <CONTENT> [OPTION] [<CONTENT> ...]\n"
44         "  -v, --version                 show DCP-o-matic version\n"
45         "  -h, --help                    show this help\n"
46         "  -n, --name <name>             film name\n"
47         "  -t, --template <name>         template name\n"
48         "      --no-encrypt              make an unencrypted DCP\n"
49         "  -e, --encrypt                 make an encrypted DCP\n"
50         "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
51         "  -f, --dcp-frame-rate <rate>   set DCP video frame rate (otherwise guessed from content)\n"
52         "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
53         "  -s, --still-length <n>        number of seconds that still content should last\n"
54         "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
55         "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
56         "      --config <dir>            directory containing config.xml and cinemas.xml\n"
57         "      --twok                    make a 2K DCP instead of choosing a resolution based on the content\n"
58         "      --fourk                   make a 4K DCP instead of choosing a resolution based on the content\n"
59         "  -o, --output <dir>            output directory\n"
60         "      --twod                    make a 2D DCP\n"
61         "      --threed                  make a 3D DCP\n"
62         "      --j2k-bandwidth <Mbit/s>  J2K bandwidth in Mbit/s\n"
63         "      --left-eye                next piece of content is for the left eye\n"
64         "      --right-eye               next piece of content is for the right eye\n"
65         "      --channel <channel>       next piece of content should be mapped to audio channel L, R, C, Lfe, Ls, Rs, BsL, BsR, HI, VI\n"
66         "      --gain                    next piece of content should have the given audio gain (in dB)\n"
67         "      --cpl <id>                CPL ID to use from the next piece of content (which is a DCP)\n"
68         "      --kdm <file>              KDM for next piece of content\n";
69
70
71 template <class T>
72 void
73 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
74 {
75         string const a = argv[n];
76         if (a != short_name && a != long_name) {
77                 return;
78         }
79
80         if ((n + 1) >= argc) {
81                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
82                 return;
83         }
84
85         *out = dcp::raw_convert<T>(string(argv[++n]));
86         *claimed = true;
87 }
88
89
90 template <class T>
91 void
92 argument_option (
93         int& n,
94         int argc,
95         char* argv[],
96         string short_name,
97         string long_name,
98         bool* claimed,
99         optional<string>* error,
100         boost::optional<T>* out,
101         std::function<boost::optional<T> (string s)> convert = [](string s) { return dcp::raw_convert<T>(s); }
102         )
103 {
104         string const a = argv[n];
105         if (a != short_name && a != long_name) {
106                 return;
107         }
108
109         if ((n + 1) >= argc) {
110                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
111                 return;
112         }
113
114         auto const arg = argv[++n];
115         auto const value = convert(arg);
116         if (!value) {
117                 *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
118                 *claimed = true;
119                 return;
120         }
121
122         *out = value;
123         *claimed = true;
124 }
125
126
127 CreateCLI::CreateCLI (int argc, char* argv[])
128         : version (false)
129 {
130         optional<string> dcp_content_type_string;
131         string container_ratio_string;
132         optional<string> standard_string;
133         int dcp_frame_rate_int = 0;
134         string template_name_string;
135         int j2k_bandwidth_int = 0;
136         auto next_frame_type = VideoFrameType::TWO_D;
137         optional<dcp::Channel> channel;
138         optional<float> gain;
139         optional<boost::filesystem::path> kdm;
140         optional<std::string> cpl;
141
142         int i = 1;
143         while (i < argc) {
144                 string const a = argv[i];
145                 bool claimed = false;
146
147                 if (a == "-v" || a == "--version") {
148                         version = true;
149                         return;
150                 } else if (a == "-h" || a == "--help") {
151                         error = "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
152                                 "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
153                                 "will be written to stdout.\n" + String::compose(_help, argv[0]);
154                         return;
155                 }
156
157                 if (a == "--no-encrypt") {
158                         _no_encrypt = claimed = true;
159                 } else if (a == "-e" || a == "--encrypt") {
160                         _encrypt = claimed = true;
161                 } else if (a == "--no-use-isdcf-name") {
162                         _no_use_isdcf_name = claimed = true;
163                 } else if (a == "--twod") {
164                         _twod = claimed = true;
165                 } else if (a == "--threed") {
166                         _threed = claimed = true;
167                 } else if (a == "--left-eye") {
168                         next_frame_type = VideoFrameType::THREE_D_LEFT;
169                         claimed = true;
170                 } else if (a == "--right-eye") {
171                         next_frame_type = VideoFrameType::THREE_D_RIGHT;
172                         claimed = true;
173                 } else if (a == "--twok") {
174                         _twok = true;
175                         claimed = true;
176                 } else if (a == "--fourk") {
177                         _fourk = true;
178                         claimed = true;
179                 }
180
181                 std::function<optional<string> (string s)> string_to_string = [](string s) {
182                         return s;
183                 };
184
185                 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
186                         return boost::filesystem::path(s);
187                 };
188
189                 std::function<optional<int> (string s)> string_to_nonzero_int = [](string s) {
190                         auto const value = dcp::raw_convert<int>(s);
191                         if (value == 0) {
192                                 return boost::optional<int>();
193                         }
194                         return boost::optional<int>(value);
195                 };
196
197                 argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &_name);
198                 argument_option(i, argc, argv, "-t", "--template",         &claimed, &error, &template_name_string);
199                 /* See comment below about --cpl */
200                 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string, string_to_string);
201                 argument_option(i, argc, argv, "-f", "--dcp-frame-rate",   &claimed, &error, &dcp_frame_rate_int);
202                 argument_option(i, argc, argv, "",   "--container-ratio",  &claimed, &error, &container_ratio_string);
203                 argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length, string_to_nonzero_int);
204                 argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string);
205                 argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir, string_to_path);
206                 argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir, string_to_path);
207                 argument_option(i, argc, argv, "",   "--j2k-bandwidth",    &claimed, &error, &j2k_bandwidth_int);
208
209                 std::function<optional<dcp::Channel> (string)> convert_channel = [](string channel) -> optional<dcp::Channel>{
210                         if (channel == "L") {
211                                 return dcp::Channel::LEFT;
212                         } else if (channel == "R") {
213                                 return dcp::Channel::RIGHT;
214                         } else if (channel == "C") {
215                                 return dcp::Channel::CENTRE;
216                         } else if (channel == "Lfe") {
217                                 return dcp::Channel::LFE;
218                         } else if (channel == "Ls") {
219                                 return dcp::Channel::LS;
220                         } else if (channel == "Rs") {
221                                 return dcp::Channel::RS;
222                         } else if (channel == "BsL") {
223                                 return dcp::Channel::BSL;
224                         } else if (channel == "BsR") {
225                                 return dcp::Channel::BSR;
226                         } else if (channel == "HI") {
227                                 return dcp::Channel::HI;
228                         } else if (channel == "VI") {
229                                 return dcp::Channel::VI;
230                         } else {
231                                 return {};
232                         }
233                 };
234
235                 argument_option(i, argc, argv, "", "--channel", &claimed, &error, &channel, convert_channel);
236                 argument_option(i, argc, argv, "", "--gain", &claimed, &error, &gain);
237                 argument_option(i, argc, argv, "", "--kdm", &claimed, &error, &kdm, string_to_path);
238                 /* It shouldn't be necessary to use this string_to_string here, but using the other argument_option()
239                  * causes an odd compile error on Ubuntu 18.04.
240                  */
241                 argument_option(i, argc, argv, "", "--cpl", &claimed, &error, &cpl, string_to_string);
242
243                 if (!claimed) {
244                         if (a.length() > 2 && a.substr(0, 2) == "--") {
245                                 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
246                                 return;
247                         } else {
248                                 Content c;
249                                 c.path = a;
250                                 c.frame_type = next_frame_type;
251                                 c.channel = channel;
252                                 c.gain = gain;
253                                 c.kdm = kdm;
254                                 c.cpl = cpl;
255                                 content.push_back (c);
256                                 next_frame_type = VideoFrameType::TWO_D;
257                                 channel = {};
258                                 gain = {};
259                         }
260                 }
261
262                 ++i;
263         }
264
265         if (!template_name_string.empty()) {
266                 _template_name = template_name_string;
267         }
268
269         if (dcp_frame_rate_int) {
270                 dcp_frame_rate = dcp_frame_rate_int;
271         }
272
273         if (j2k_bandwidth_int) {
274                 _j2k_bandwidth = j2k_bandwidth_int * 1000000;
275         }
276
277         if (dcp_content_type_string) {
278                 _dcp_content_type = DCPContentType::from_isdcf_name(*dcp_content_type_string);
279                 if (!_dcp_content_type) {
280                         error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], *dcp_content_type_string);
281                         return;
282                 }
283         }
284
285         if (!container_ratio_string.empty()) {
286                 _container_ratio = Ratio::from_id (container_ratio_string);
287                 if (!_container_ratio) {
288                         error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
289                         return;
290                 }
291         }
292
293         if (standard_string) {
294                 if (*standard_string == "interop") {
295                         _standard = dcp::Standard::INTEROP;
296                 } else if (*standard_string == "SMPTE") {
297                         _standard = dcp::Standard::SMPTE;
298                 } else {
299                         error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
300                         return;
301                 }
302         }
303
304         if (_twod && _threed) {
305                 error = String::compose("%1: specify one of --twod or --threed, not both", argv[0]);
306         }
307
308         if (_no_encrypt && _encrypt) {
309                 error = String::compose("%1: specify one of --no-encrypt or --encrypt, not both", argv[0]);
310         }
311
312         if (content.empty()) {
313                 error = String::compose("%1: no content specified", argv[0]);
314                 return;
315         }
316
317         if (_name.empty()) {
318                 _name = content[0].path.leaf().string();
319         }
320
321         if (_j2k_bandwidth && (*_j2k_bandwidth < 10000000 || *_j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
322                 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
323                 return;
324         }
325 }
326
327
328 shared_ptr<Film>
329 CreateCLI::make_film() const
330 {
331         auto film = std::make_shared<Film>(output_dir);
332         dcpomatic_log = film->log();
333         dcpomatic_log->set_types(Config::instance()->log_types());
334         if (_template_name) {
335                 film->use_template(_template_name.get());
336         } else {
337                 /* No template: apply our own CLI tool defaults to override the ones in Config.
338                  * Maybe one day there will be no defaults in Config any more (as they'll be in
339                  * a default template) and we can decide whether to use the default template
340                  * or not.
341                  */
342                 film->set_interop(false);
343                 film->set_dcp_content_type(DCPContentType::from_isdcf_name("TST"));
344         }
345         film->set_name(_name);
346
347         if (_container_ratio) {
348                 film->set_container(_container_ratio);
349         }
350         if (_dcp_content_type) {
351                 film->set_dcp_content_type(_dcp_content_type);
352         }
353         if (_standard) {
354                 film->set_interop(*_standard == dcp::Standard::INTEROP);
355         }
356         film->set_use_isdcf_name(!_no_use_isdcf_name);
357         if (_no_encrypt) {
358                 film->set_encrypted(false);
359         } else if (_encrypt) {
360                 film->set_encrypted(true);
361         }
362         if (_twod) {
363                 film->set_three_d(false);
364         } else if (_threed) {
365                 film->set_three_d(true);
366         }
367         if (_twok) {
368                 film->set_resolution(Resolution::TWO_K);
369         }
370         if (_fourk) {
371                 film->set_resolution(Resolution::FOUR_K);
372         }
373         if (_j2k_bandwidth) {
374                 film->set_j2k_bandwidth(*_j2k_bandwidth);
375         }
376
377         int channels = 6;
378         for (auto cli_content: content) {
379                 if (cli_content.channel) {
380                         channels = std::max(channels, static_cast<int>(*cli_content.channel) + 1);
381                 }
382         }
383         if (channels % 1) {
384                 ++channels;
385         }
386
387         film->set_audio_channels(channels);
388
389         return film;
390 }
391