Add --twod option to create CLI and stop 2D from being forced over
[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         "      --twod                    make a 2D DCP\n"
60         "      --threed                  make a 3D DCP\n"
61         "      --j2k-bandwidth <Mbit/s>  J2K bandwidth in Mbit/s\n"
62         "      --left-eye                next piece of content is for the left eye\n"
63         "      --right-eye               next piece of content is for the right eye\n"
64         "      --channel <channel>       next piece of content should be mapped to audio channel L, R, C, Lfe, Ls, Rs, BsL, BsR, HI, VI\n"
65         "      --gain                    next piece of content should have the given audio gain (in dB)\n"
66         "      --cpl <id>                CPL ID to use from the next piece of content (which is a DCP)\n"
67         "      --kdm <file>              KDM for next piece of content\n";
68
69
70 template <class T>
71 void
72 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
73 {
74         string const a = argv[n];
75         if (a != short_name && a != long_name) {
76                 return;
77         }
78
79         if ((n + 1) >= argc) {
80                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
81                 return;
82         }
83
84         *out = dcp::raw_convert<T>(string(argv[++n]));
85         *claimed = true;
86 }
87
88
89 template <class T>
90 void
91 argument_option (
92         int& n,
93         int argc,
94         char* argv[],
95         string short_name,
96         string long_name,
97         bool* claimed,
98         optional<string>* error,
99         boost::optional<T>* out,
100         std::function<boost::optional<T> (string s)> convert = [](string s) { return dcp::raw_convert<T>(s); }
101         )
102 {
103         string const a = argv[n];
104         if (a != short_name && a != long_name) {
105                 return;
106         }
107
108         if ((n + 1) >= argc) {
109                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
110                 return;
111         }
112
113         auto const arg = argv[++n];
114         auto const value = convert(arg);
115         if (!value) {
116                 *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
117                 *claimed = true;
118                 return;
119         }
120
121         *out = value;
122         *claimed = true;
123 }
124
125
126 CreateCLI::CreateCLI (int argc, char* argv[])
127         : version (false)
128 {
129         string dcp_content_type_string = "TST";
130         string container_ratio_string;
131         string standard_string = "SMPTE";
132         int dcp_frame_rate_int = 0;
133         string template_name_string;
134         int j2k_bandwidth_int = 0;
135         auto next_frame_type = VideoFrameType::TWO_D;
136         optional<dcp::Channel> channel;
137         optional<float> gain;
138         optional<boost::filesystem::path> kdm;
139         optional<std::string> cpl;
140
141         int i = 1;
142         while (i < argc) {
143                 string const a = argv[i];
144                 bool claimed = false;
145
146                 if (a == "-v" || a == "--version") {
147                         version = true;
148                         return;
149                 } else if (a == "-h" || a == "--help") {
150                         error = "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
151                                 "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
152                                 "will be written to stdout.\n" + String::compose(_help, argv[0]);
153                         return;
154                 }
155
156                 if (a == "-e" || a == "--encrypt") {
157                         _encrypt = claimed = true;
158                 } else if (a == "--no-use-isdcf-name") {
159                         _no_use_isdcf_name = claimed = true;
160                 } else if (a == "--twod") {
161                         _twod = claimed = true;
162                 } else if (a == "--threed") {
163                         _threed = claimed = true;
164                 } else if (a == "--left-eye") {
165                         next_frame_type = VideoFrameType::THREE_D_LEFT;
166                         claimed = true;
167                 } else if (a == "--right-eye") {
168                         next_frame_type = VideoFrameType::THREE_D_RIGHT;
169                         claimed = true;
170                 } else if (a == "--twok") {
171                         _twok = true;
172                         claimed = true;
173                 } else if (a == "--fourk") {
174                         _fourk = true;
175                         claimed = true;
176                 }
177
178                 std::function<optional<string> (string s)> string_to_string = [](string s) {
179                         return s;
180                 };
181
182                 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
183                         return boost::filesystem::path(s);
184                 };
185
186                 std::function<optional<int> (string s)> string_to_nonzero_int = [](string s) {
187                         auto const value = dcp::raw_convert<int>(s);
188                         if (value == 0) {
189                                 return boost::optional<int>();
190                         }
191                         return boost::optional<int>(value);
192                 };
193
194                 argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &_name);
195                 argument_option(i, argc, argv, "-t", "--template",         &claimed, &error, &template_name_string);
196                 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
197                 argument_option(i, argc, argv, "-f", "--dcp-frame-rate",   &claimed, &error, &dcp_frame_rate_int);
198                 argument_option(i, argc, argv, "",   "--container-ratio",  &claimed, &error, &container_ratio_string);
199                 argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length, string_to_nonzero_int);
200                 argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string);
201                 argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir, string_to_path);
202                 argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir, string_to_path);
203                 argument_option(i, argc, argv, "",   "--j2k-bandwidth",    &claimed, &error, &j2k_bandwidth_int);
204
205                 std::function<optional<dcp::Channel> (string)> convert_channel = [](string channel) -> optional<dcp::Channel>{
206                         if (channel == "L") {
207                                 return dcp::Channel::LEFT;
208                         } else if (channel == "R") {
209                                 return dcp::Channel::RIGHT;
210                         } else if (channel == "C") {
211                                 return dcp::Channel::CENTRE;
212                         } else if (channel == "Lfe") {
213                                 return dcp::Channel::LFE;
214                         } else if (channel == "Ls") {
215                                 return dcp::Channel::LS;
216                         } else if (channel == "Rs") {
217                                 return dcp::Channel::RS;
218                         } else if (channel == "BsL") {
219                                 return dcp::Channel::BSL;
220                         } else if (channel == "BsR") {
221                                 return dcp::Channel::BSR;
222                         } else if (channel == "HI") {
223                                 return dcp::Channel::HI;
224                         } else if (channel == "VI") {
225                                 return dcp::Channel::VI;
226                         } else {
227                                 return {};
228                         }
229                 };
230
231                 argument_option(i, argc, argv, "", "--channel", &claimed, &error, &channel, convert_channel);
232                 argument_option(i, argc, argv, "", "--gain", &claimed, &error, &gain);
233                 argument_option(i, argc, argv, "", "--kdm", &claimed, &error, &kdm, string_to_path);
234                 /* It shouldn't be necessary to use this string_to_string here, but using the other argument_option()
235                  * causes an odd compile error on Ubuntu 18.04.
236                  */
237                 argument_option(i, argc, argv, "", "--cpl", &claimed, &error, &cpl, string_to_string);
238
239                 if (!claimed) {
240                         if (a.length() > 2 && a.substr(0, 2) == "--") {
241                                 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
242                                 return;
243                         } else {
244                                 Content c;
245                                 c.path = a;
246                                 c.frame_type = next_frame_type;
247                                 c.channel = channel;
248                                 c.gain = gain;
249                                 c.kdm = kdm;
250                                 c.cpl = cpl;
251                                 content.push_back (c);
252                                 next_frame_type = VideoFrameType::TWO_D;
253                                 channel = {};
254                                 gain = {};
255                         }
256                 }
257
258                 ++i;
259         }
260
261         if (!template_name_string.empty()) {
262                 _template_name = template_name_string;
263         }
264
265         if (dcp_frame_rate_int) {
266                 dcp_frame_rate = dcp_frame_rate_int;
267         }
268
269         if (j2k_bandwidth_int) {
270                 _j2k_bandwidth = j2k_bandwidth_int * 1000000;
271         }
272
273         _dcp_content_type = DCPContentType::from_isdcf_name(dcp_content_type_string);
274         if (!_dcp_content_type) {
275                 error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], dcp_content_type_string);
276                 return;
277         }
278
279         if (!container_ratio_string.empty()) {
280                 _container_ratio = Ratio::from_id (container_ratio_string);
281                 if (!_container_ratio) {
282                         error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
283                         return;
284                 }
285         }
286
287         if (standard_string != "SMPTE" && standard_string != "interop") {
288                 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
289                 return;
290         }
291
292         if (standard_string == "interop") {
293                 _standard = dcp::Standard::INTEROP;
294         }
295
296         if (_twod && _threed) {
297                 error = String::compose("%1: specify one of --twod or --threed, not both", argv[0]);
298         }
299
300         if (content.empty()) {
301                 error = String::compose("%1: no content specified", argv[0]);
302                 return;
303         }
304
305         if (_name.empty()) {
306                 _name = content[0].path.leaf().string();
307         }
308
309         if (_j2k_bandwidth && (*_j2k_bandwidth < 10000000 || *_j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
310                 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
311                 return;
312         }
313 }
314
315
316 shared_ptr<Film>
317 CreateCLI::make_film() const
318 {
319         auto film = std::make_shared<Film>(output_dir);
320         dcpomatic_log = film->log();
321         dcpomatic_log->set_types(Config::instance()->log_types());
322         if (_template_name) {
323                 film->use_template(_template_name.get());
324         }
325         film->set_name(_name);
326
327         if (_container_ratio) {
328                 film->set_container(_container_ratio);
329         }
330         film->set_dcp_content_type(_dcp_content_type);
331         film->set_interop(_standard == dcp::Standard::INTEROP);
332         film->set_use_isdcf_name(!_no_use_isdcf_name);
333         film->set_encrypted(_encrypt);
334         if (_twod) {
335                 film->set_three_d(false);
336         } else if (_threed) {
337                 film->set_three_d(true);
338         }
339         if (_twok) {
340                 film->set_resolution(Resolution::TWO_K);
341         }
342         if (_fourk) {
343                 film->set_resolution(Resolution::FOUR_K);
344         }
345         if (_j2k_bandwidth) {
346                 film->set_j2k_bandwidth(*_j2k_bandwidth);
347         }
348
349         int channels = 6;
350         for (auto cli_content: content) {
351                 if (cli_content.channel) {
352                         channels = std::max(channels, static_cast<int>(*cli_content.channel) + 1);
353                 }
354         }
355         if (channels % 1) {
356                 ++channels;
357         }
358
359         film->set_audio_channels(channels);
360
361         return film;
362 }
363