Cleanup: extract some stuff to a method in CreateCLI.
[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         "  -e, --encrypt                 make an encrypted DCP\n"
49         "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
50         "  -f, --dcp-frame-rate <rate>   set DCP video frame rate (otherwise guessed from content)\n"
51         "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
52         "  -s, --still-length <n>        number of seconds that still content should last\n"
53         "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
54         "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
55         "      --config <dir>            directory containing config.xml and cinemas.xml\n"
56         "      --twok                    make a 2K DCP instead of choosing a resolution based on the content\n"
57         "      --fourk                   make a 4K DCP instead of choosing a resolution based on the content\n"
58         "  -o, --output <dir>            output directory\n"
59         "      --threed                  make a 3D DCP\n"
60         "      --j2k-bandwidth <Mbit/s>  J2K bandwidth in Mbit/s\n"
61         "      --left-eye                next piece of content is for the left eye\n"
62         "      --right-eye               next piece of content is for the right eye\n"
63         "      --channel <channel>       next piece of content should be mapped to audio channel L, R, C, Lfe, Ls, Rs, BsL, BsR, HI, VI\n"
64         "      --gain                    next piece of content should have the given audio gain (in dB)\n"
65         "      --cpl <id>                CPL ID to use from the next piece of content (which is a DCP)\n"
66         "      --kdm <file>              KDM for next piece of content\n";
67
68
69 template <class T>
70 void
71 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
72 {
73         string const a = argv[n];
74         if (a != short_name && a != long_name) {
75                 return;
76         }
77
78         if ((n + 1) >= argc) {
79                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
80                 return;
81         }
82
83         *out = dcp::raw_convert<T>(string(argv[++n]));
84         *claimed = true;
85 }
86
87
88 template <class T>
89 void
90 argument_option (
91         int& n,
92         int argc,
93         char* argv[],
94         string short_name,
95         string long_name,
96         bool* claimed,
97         optional<string>* error,
98         boost::optional<T>* out,
99         std::function<boost::optional<T> (string s)> convert = [](string s) { return dcp::raw_convert<T>(s); }
100         )
101 {
102         string const a = argv[n];
103         if (a != short_name && a != long_name) {
104                 return;
105         }
106
107         if ((n + 1) >= argc) {
108                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
109                 return;
110         }
111
112         auto const arg = argv[++n];
113         auto const value = convert(arg);
114         if (!value) {
115                 *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
116                 *claimed = true;
117                 return;
118         }
119
120         *out = value;
121         *claimed = true;
122 }
123
124
125 CreateCLI::CreateCLI (int argc, char* argv[])
126         : version (false)
127         , encrypt (false)
128         , threed (false)
129         , dcp_content_type (nullptr)
130         , container_ratio (nullptr)
131         , standard (dcp::Standard::SMPTE)
132         , no_use_isdcf_name (false)
133         , twok (false)
134         , fourk (false)
135 {
136         string dcp_content_type_string = "TST";
137         string container_ratio_string;
138         string standard_string = "SMPTE";
139         int dcp_frame_rate_int = 0;
140         string template_name_string;
141         int j2k_bandwidth_int = 0;
142         auto next_frame_type = VideoFrameType::TWO_D;
143         optional<dcp::Channel> channel;
144         optional<float> gain;
145         optional<boost::filesystem::path> kdm;
146         optional<std::string> cpl;
147
148         int i = 1;
149         while (i < argc) {
150                 string const a = argv[i];
151                 bool claimed = false;
152
153                 if (a == "-v" || a == "--version") {
154                         version = true;
155                         return;
156                 } else if (a == "-h" || a == "--help") {
157                         error = "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
158                                 "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
159                                 "will be written to stdout.\n" + String::compose(_help, argv[0]);
160                         return;
161                 }
162
163                 if (a == "-e" || a == "--encrypt") {
164                         encrypt = claimed = true;
165                 } else if (a == "--no-use-isdcf-name") {
166                         no_use_isdcf_name = claimed = true;
167                 } else if (a == "--threed") {
168                         threed = claimed = true;
169                 } else if (a == "--left-eye") {
170                         next_frame_type = VideoFrameType::THREE_D_LEFT;
171                         claimed = true;
172                 } else if (a == "--right-eye") {
173                         next_frame_type = VideoFrameType::THREE_D_RIGHT;
174                         claimed = true;
175                 } else if (a == "--twok") {
176                         twok = true;
177                         claimed = true;
178                 } else if (a == "--fourk") {
179                         fourk = true;
180                         claimed = true;
181                 }
182
183                 std::function<optional<string> (string s)> string_to_string = [](string s) {
184                         return s;
185                 };
186
187                 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
188                         return boost::filesystem::path(s);
189                 };
190
191                 std::function<optional<int> (string s)> string_to_nonzero_int = [](string s) {
192                         auto const value = dcp::raw_convert<int>(s);
193                         if (value == 0) {
194                                 return boost::optional<int>();
195                         }
196                         return boost::optional<int>(value);
197                 };
198
199                 argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &name);
200                 argument_option(i, argc, argv, "-t", "--template",         &claimed, &error, &template_name_string);
201                 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
202                 argument_option(i, argc, argv, "-f", "--dcp-frame-rate",   &claimed, &error, &dcp_frame_rate_int);
203                 argument_option(i, argc, argv, "",   "--container-ratio",  &claimed, &error, &container_ratio_string);
204                 argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length, string_to_nonzero_int);
205                 argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_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         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         if (!container_ratio_string.empty()) {
285                 container_ratio = Ratio::from_id (container_ratio_string);
286                 if (!container_ratio) {
287                         error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
288                         return;
289                 }
290         }
291
292         if (standard_string != "SMPTE" && standard_string != "interop") {
293                 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
294                 return;
295         }
296
297         if (standard_string == "interop") {
298                 standard = dcp::Standard::INTEROP;
299         }
300
301         if (content.empty()) {
302                 error = String::compose("%1: no content specified", argv[0]);
303                 return;
304         }
305
306         if (name.empty()) {
307                 name = content[0].path.leaf().string();
308         }
309
310         if (j2k_bandwidth && (*j2k_bandwidth < 10000000 || *j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
311                 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
312                 return;
313         }
314 }
315
316
317 shared_ptr<Film>
318 CreateCLI::make_film() const
319 {
320         auto film = std::make_shared<Film>(output_dir);
321         dcpomatic_log = film->log();
322         dcpomatic_log->set_types(Config::instance()->log_types());
323         if (template_name) {
324                 film->use_template(template_name.get());
325         }
326         film->set_name(name);
327
328         if (container_ratio) {
329                 film->set_container(container_ratio);
330         }
331         film->set_dcp_content_type(dcp_content_type);
332         film->set_interop(standard == dcp::Standard::INTEROP);
333         film->set_use_isdcf_name(!no_use_isdcf_name);
334         film->set_encrypted(encrypt);
335         film->set_three_d(threed);
336         if (twok) {
337                 film->set_resolution(Resolution::TWO_K);
338         }
339         if (fourk) {
340                 film->set_resolution(Resolution::FOUR_K);
341         }
342         if (j2k_bandwidth) {
343                 film->set_j2k_bandwidth(*j2k_bandwidth);
344         }
345
346         int channels = 6;
347         for (auto cli_content: content) {
348                 if (cli_content.channel) {
349                         channels = std::max(channels, static_cast<int>(*cli_content.channel) + 1);
350                 }
351         }
352         if (channels % 1) {
353                 ++channels;
354         }
355
356         film->set_audio_channels(channels);
357
358         return film;
359 }
360