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