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 " -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";
71 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
73 string const a = argv[n];
74 if (a != short_name && a != long_name) {
78 if ((n + 1) >= argc) {
79 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
83 *out = dcp::raw_convert<T>(string(argv[++n]));
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); }
102 string const a = argv[n];
103 if (a != short_name && a != long_name) {
107 if ((n + 1) >= argc) {
108 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
112 auto const arg = argv[++n];
113 auto const value = convert(arg);
115 *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
125 CreateCLI::CreateCLI (int argc, char* argv[])
128 string dcp_content_type_string = "TST";
129 string container_ratio_string;
130 string standard_string = "SMPTE";
131 int dcp_frame_rate_int = 0;
132 string template_name_string;
133 int j2k_bandwidth_int = 0;
134 auto next_frame_type = VideoFrameType::TWO_D;
135 optional<dcp::Channel> channel;
136 optional<float> gain;
137 optional<boost::filesystem::path> kdm;
138 optional<std::string> cpl;
142 string const a = argv[i];
143 bool claimed = false;
145 if (a == "-v" || a == "--version") {
148 } else if (a == "-h" || a == "--help") {
149 error = "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
150 "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
151 "will be written to stdout.\n" + String::compose(_help, argv[0]);
155 if (a == "-e" || a == "--encrypt") {
156 _encrypt = claimed = true;
157 } else if (a == "--no-use-isdcf-name") {
158 _no_use_isdcf_name = claimed = true;
159 } else if (a == "--threed") {
160 _threed = claimed = true;
161 } else if (a == "--left-eye") {
162 next_frame_type = VideoFrameType::THREE_D_LEFT;
164 } else if (a == "--right-eye") {
165 next_frame_type = VideoFrameType::THREE_D_RIGHT;
167 } else if (a == "--twok") {
170 } else if (a == "--fourk") {
175 std::function<optional<string> (string s)> string_to_string = [](string s) {
179 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
180 return boost::filesystem::path(s);
183 std::function<optional<int> (string s)> string_to_nonzero_int = [](string s) {
184 auto const value = dcp::raw_convert<int>(s);
186 return boost::optional<int>();
188 return boost::optional<int>(value);
191 argument_option(i, argc, argv, "-n", "--name", &claimed, &error, &_name);
192 argument_option(i, argc, argv, "-t", "--template", &claimed, &error, &template_name_string);
193 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
194 argument_option(i, argc, argv, "-f", "--dcp-frame-rate", &claimed, &error, &dcp_frame_rate_int);
195 argument_option(i, argc, argv, "", "--container-ratio", &claimed, &error, &container_ratio_string);
196 argument_option(i, argc, argv, "-s", "--still-length", &claimed, &error, &still_length, string_to_nonzero_int);
197 argument_option(i, argc, argv, "", "--standard", &claimed, &error, &standard_string);
198 argument_option(i, argc, argv, "", "--config", &claimed, &error, &config_dir, string_to_path);
199 argument_option(i, argc, argv, "-o", "--output", &claimed, &error, &output_dir, string_to_path);
200 argument_option(i, argc, argv, "", "--j2k-bandwidth", &claimed, &error, &j2k_bandwidth_int);
202 std::function<optional<dcp::Channel> (string)> convert_channel = [](string channel) -> optional<dcp::Channel>{
203 if (channel == "L") {
204 return dcp::Channel::LEFT;
205 } else if (channel == "R") {
206 return dcp::Channel::RIGHT;
207 } else if (channel == "C") {
208 return dcp::Channel::CENTRE;
209 } else if (channel == "Lfe") {
210 return dcp::Channel::LFE;
211 } else if (channel == "Ls") {
212 return dcp::Channel::LS;
213 } else if (channel == "Rs") {
214 return dcp::Channel::RS;
215 } else if (channel == "BsL") {
216 return dcp::Channel::BSL;
217 } else if (channel == "BsR") {
218 return dcp::Channel::BSR;
219 } else if (channel == "HI") {
220 return dcp::Channel::HI;
221 } else if (channel == "VI") {
222 return dcp::Channel::VI;
228 argument_option(i, argc, argv, "", "--channel", &claimed, &error, &channel, convert_channel);
229 argument_option(i, argc, argv, "", "--gain", &claimed, &error, &gain);
230 argument_option(i, argc, argv, "", "--kdm", &claimed, &error, &kdm, string_to_path);
231 /* It shouldn't be necessary to use this string_to_string here, but using the other argument_option()
232 * causes an odd compile error on Ubuntu 18.04.
234 argument_option(i, argc, argv, "", "--cpl", &claimed, &error, &cpl, string_to_string);
237 if (a.length() > 2 && a.substr(0, 2) == "--") {
238 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
243 c.frame_type = next_frame_type;
248 content.push_back (c);
249 next_frame_type = VideoFrameType::TWO_D;
258 if (!template_name_string.empty()) {
259 _template_name = template_name_string;
262 if (dcp_frame_rate_int) {
263 dcp_frame_rate = dcp_frame_rate_int;
266 if (j2k_bandwidth_int) {
267 _j2k_bandwidth = j2k_bandwidth_int * 1000000;
270 _dcp_content_type = DCPContentType::from_isdcf_name(dcp_content_type_string);
271 if (!_dcp_content_type) {
272 error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], dcp_content_type_string);
276 if (!container_ratio_string.empty()) {
277 _container_ratio = Ratio::from_id (container_ratio_string);
278 if (!_container_ratio) {
279 error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
284 if (standard_string != "SMPTE" && standard_string != "interop") {
285 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
289 if (standard_string == "interop") {
290 _standard = dcp::Standard::INTEROP;
293 if (content.empty()) {
294 error = String::compose("%1: no content specified", argv[0]);
299 _name = content[0].path.leaf().string();
302 if (_j2k_bandwidth && (*_j2k_bandwidth < 10000000 || *_j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
303 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
310 CreateCLI::make_film() const
312 auto film = std::make_shared<Film>(output_dir);
313 dcpomatic_log = film->log();
314 dcpomatic_log->set_types(Config::instance()->log_types());
315 if (_template_name) {
316 film->use_template(_template_name.get());
318 film->set_name(_name);
320 if (_container_ratio) {
321 film->set_container(_container_ratio);
323 film->set_dcp_content_type(_dcp_content_type);
324 film->set_interop(_standard == dcp::Standard::INTEROP);
325 film->set_use_isdcf_name(!_no_use_isdcf_name);
326 film->set_encrypted(_encrypt);
327 film->set_three_d(_threed);
329 film->set_resolution(Resolution::TWO_K);
332 film->set_resolution(Resolution::FOUR_K);
334 if (_j2k_bandwidth) {
335 film->set_j2k_bandwidth(*_j2k_bandwidth);
339 for (auto cli_content: content) {
340 if (cli_content.channel) {
341 channels = std::max(channels, static_cast<int>(*cli_content.channel) + 1);
348 film->set_audio_channels(channels);