2 Copyright (C) 2019-2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "compose.hpp"
24 #include "create_cli.h"
25 #include "dcp_content_type.h"
26 #include "dcpomatic_log.h"
29 #include <dcp/raw_convert.h>
35 using std::make_shared;
36 using std::shared_ptr;
39 using boost::optional;
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";
73 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
75 string const a = argv[n];
76 if (a != short_name && a != long_name) {
80 if ((n + 1) >= argc) {
81 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
85 *out = dcp::raw_convert<T>(string(argv[++n]));
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); }
104 string const a = argv[n];
105 if (a != short_name && a != long_name) {
109 if ((n + 1) >= argc) {
110 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
114 auto const arg = argv[++n];
115 auto const value = convert(arg);
117 *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
127 CreateCLI::CreateCLI (int argc, char* argv[])
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;
144 string const a = argv[i];
145 bool claimed = false;
147 if (a == "-v" || a == "--version") {
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]);
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;
170 } else if (a == "--right-eye") {
171 next_frame_type = VideoFrameType::THREE_D_RIGHT;
173 } else if (a == "--twok") {
176 } else if (a == "--fourk") {
181 std::function<optional<string> (string s)> string_to_string = [](string s) {
185 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
186 return boost::filesystem::path(s);
189 std::function<optional<int> (string s)> string_to_nonzero_int = [](string s) {
190 auto const value = dcp::raw_convert<int>(s);
192 return boost::optional<int>();
194 return boost::optional<int>(value);
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);
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;
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.
242 argument_option(i, argc, argv, "", "--cpl", &claimed, &error, &cpl, string_to_string);
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]);
251 c.frame_type = next_frame_type;
256 content.push_back (c);
257 next_frame_type = VideoFrameType::TWO_D;
266 if (!template_name_string.empty()) {
267 _template_name = template_name_string;
270 if (dcp_frame_rate_int) {
271 dcp_frame_rate = dcp_frame_rate_int;
274 if (j2k_bandwidth_int) {
275 _j2k_bandwidth = j2k_bandwidth_int * 1000000;
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);
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);
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;
300 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
305 if (_twod && _threed) {
306 error = String::compose("%1: specify one of --twod or --threed, not both", argv[0]);
309 if (_no_encrypt && _encrypt) {
310 error = String::compose("%1: specify one of --no-encrypt or --encrypt, not both", argv[0]);
313 if (content.empty()) {
314 error = String::compose("%1: no content specified", argv[0]);
319 _name = content[0].path.filename().string();
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));
330 CreateCLI::make_film() const
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());
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
343 film->set_interop(false);
344 film->set_dcp_content_type(DCPContentType::from_isdcf_name("TST"));
346 film->set_name(_name);
348 if (_container_ratio) {
349 film->set_container(_container_ratio);
351 if (_dcp_content_type) {
352 film->set_dcp_content_type(_dcp_content_type);
355 film->set_interop(*_standard == dcp::Standard::INTEROP);
357 film->set_use_isdcf_name(!_no_use_isdcf_name);
359 film->set_encrypted(false);
360 } else if (_encrypt) {
361 film->set_encrypted(true);
364 film->set_three_d(false);
365 } else if (_threed) {
366 film->set_three_d(true);
369 film->set_resolution(Resolution::TWO_K);
372 film->set_resolution(Resolution::FOUR_K);
374 if (_j2k_bandwidth) {
375 film->set_j2k_bandwidth(*_j2k_bandwidth);
379 for (auto cli_content: content) {
380 if (cli_content.channel) {
381 channels = std::max(channels, static_cast<int>(*cli_content.channel) + 1);
388 film->set_audio_channels(channels);