Supporters update.
[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<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                 /* See comment below about --cpl */
205                 argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string, string_to_string);
206                 argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir, string_to_path);
207                 argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir, string_to_path);
208                 argument_option(i, argc, argv, "",   "--j2k-bandwidth",    &claimed, &error, &j2k_bandwidth_int);
209
210                 std::function<optional<dcp::Channel> (string)> convert_channel = [](string channel) -> optional<dcp::Channel>{
211                         if (channel == "L") {
212                                 return dcp::Channel::LEFT;
213                         } else if (channel == "R") {
214                                 return dcp::Channel::RIGHT;
215                         } else if (channel == "C") {
216                                 return dcp::Channel::CENTRE;
217                         } else if (channel == "Lfe") {
218                                 return dcp::Channel::LFE;
219                         } else if (channel == "Ls") {
220                                 return dcp::Channel::LS;
221                         } else if (channel == "Rs") {
222                                 return dcp::Channel::RS;
223                         } else if (channel == "BsL") {
224                                 return dcp::Channel::BSL;
225                         } else if (channel == "BsR") {
226                                 return dcp::Channel::BSR;
227                         } else if (channel == "HI") {
228                                 return dcp::Channel::HI;
229                         } else if (channel == "VI") {
230                                 return dcp::Channel::VI;
231                         } else {
232                                 return {};
233                         }
234                 };
235
236                 argument_option(i, argc, argv, "", "--channel", &claimed, &error, &channel, convert_channel);
237                 argument_option(i, argc, argv, "", "--gain", &claimed, &error, &gain);
238                 argument_option(i, argc, argv, "", "--kdm", &claimed, &error, &kdm, string_to_path);
239                 /* It shouldn't be necessary to use this string_to_string here, but using the other argument_option()
240                  * causes an odd compile error on Ubuntu 18.04.
241                  */
242                 argument_option(i, argc, argv, "", "--cpl", &claimed, &error, &cpl, string_to_string);
243
244                 if (!claimed) {
245                         if (a.length() > 2 && a.substr(0, 2) == "--") {
246                                 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
247                                 return;
248                         } else {
249                                 Content c;
250                                 c.path = a;
251                                 c.frame_type = next_frame_type;
252                                 c.channel = channel;
253                                 c.gain = gain;
254                                 c.kdm = kdm;
255                                 c.cpl = cpl;
256                                 content.push_back (c);
257                                 next_frame_type = VideoFrameType::TWO_D;
258                                 channel = {};
259                                 gain = {};
260                         }
261                 }
262
263                 ++i;
264         }
265
266         if (!template_name_string.empty()) {
267                 _template_name = template_name_string;
268         }
269
270         if (dcp_frame_rate_int) {
271                 dcp_frame_rate = dcp_frame_rate_int;
272         }
273
274         if (j2k_bandwidth_int) {
275                 _j2k_bandwidth = j2k_bandwidth_int * 1000000;
276         }
277
278         if (dcp_content_type_string) {
279                 _dcp_content_type = DCPContentType::from_isdcf_name(*dcp_content_type_string);
280                 if (!_dcp_content_type) {
281                         error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], *dcp_content_type_string);
282                         return;
283                 }
284         }
285
286         if (!container_ratio_string.empty()) {
287                 _container_ratio = Ratio::from_id (container_ratio_string);
288                 if (!_container_ratio) {
289                         error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
290                         return;
291                 }
292         }
293
294         if (standard_string) {
295                 if (*standard_string == "interop") {
296                         _standard = dcp::Standard::INTEROP;
297                 } else if (*standard_string == "SMPTE") {
298                         _standard = dcp::Standard::SMPTE;
299                 } else {
300                         error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
301                         return;
302                 }
303         }
304
305         if (_twod && _threed) {
306                 error = String::compose("%1: specify one of --twod or --threed, not both", argv[0]);
307         }
308
309         if (_no_encrypt && _encrypt) {
310                 error = String::compose("%1: specify one of --no-encrypt or --encrypt, not both", argv[0]);
311         }
312
313         if (content.empty()) {
314                 error = String::compose("%1: no content specified", argv[0]);
315                 return;
316         }
317
318         if (_name.empty()) {
319                 _name = content[0].path.filename().string();
320         }
321
322         if (_j2k_bandwidth && (*_j2k_bandwidth < 10000000 || *_j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
323                 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
324                 return;
325         }
326 }
327
328
329 shared_ptr<Film>
330 CreateCLI::make_film() const
331 {
332         auto film = std::make_shared<Film>(output_dir);
333         dcpomatic_log = film->log();
334         dcpomatic_log->set_types(Config::instance()->log_types());
335         if (_template_name) {
336                 film->use_template(_template_name.get());
337         } else {
338                 /* No template: apply our own CLI tool defaults to override the ones in Config.
339                  * Maybe one day there will be no defaults in Config any more (as they'll be in
340                  * a default template) and we can decide whether to use the default template
341                  * or not.
342                  */
343                 film->set_interop(false);
344                 film->set_dcp_content_type(DCPContentType::from_isdcf_name("TST"));
345         }
346         film->set_name(_name);
347
348         if (_container_ratio) {
349                 film->set_container(_container_ratio);
350         }
351         if (_dcp_content_type) {
352                 film->set_dcp_content_type(_dcp_content_type);
353         }
354         if (_standard) {
355                 film->set_interop(*_standard == dcp::Standard::INTEROP);
356         }
357         film->set_use_isdcf_name(!_no_use_isdcf_name);
358         if (_no_encrypt) {
359                 film->set_encrypted(false);
360         } else if (_encrypt) {
361                 film->set_encrypted(true);
362         }
363         if (_twod) {
364                 film->set_three_d(false);
365         } else if (_threed) {
366                 film->set_three_d(true);
367         }
368         if (_twok) {
369                 film->set_resolution(Resolution::TWO_K);
370         }
371         if (_fourk) {
372                 film->set_resolution(Resolution::FOUR_K);
373         }
374         if (_j2k_bandwidth) {
375                 film->set_j2k_bandwidth(*_j2k_bandwidth);
376         }
377
378         int channels = 6;
379         for (auto cli_content: content) {
380                 if (cli_content.channel) {
381                         channels = std::max(channels, static_cast<int>(*cli_content.channel) + 1);
382                 }
383         }
384         if (channels % 2) {
385                 ++channels;
386         }
387
388         film->set_audio_channels(channels);
389
390         return film;
391 }
392