Allow specification of the CPL ID to use in a DCP with _create (#2302).
[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 "ratio.h"
27 #include <dcp/raw_convert.h>
28 #include <iostream>
29 #include <string>
30
31
32 using std::string;
33 using std::cout;
34 using boost::optional;
35
36
37 string CreateCLI::_help =
38         "\nSyntax: %1 [OPTION] <CONTENT> [OPTION] [<CONTENT> ...]\n"
39         "  -v, --version                 show DCP-o-matic version\n"
40         "  -h, --help                    show this help\n"
41         "  -n, --name <name>             film name\n"
42         "  -t, --template <name>         template name\n"
43         "  -e, --encrypt                 make an encrypted DCP\n"
44         "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
45         "  -f, --dcp-frame-rate <rate>   set DCP video frame rate (otherwise guessed from content)\n"
46         "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
47         "  -s, --still-length <n>        number of seconds that still content should last\n"
48         "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
49         "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
50         "      --config <dir>            directory containing config.xml and cinemas.xml\n"
51         "      --twok                    make a 2K DCP instead of choosing a resolution based on the content\n"
52         "      --fourk                   make a 4K DCP instead of choosing a resolution based on the content\n"
53         "  -o, --output <dir>            output directory\n"
54         "      --threed                  make a 3D DCP\n"
55         "      --j2k-bandwidth <Mbit/s>  J2K bandwidth in Mbit/s\n"
56         "      --left-eye                next piece of content is for the left eye\n"
57         "      --right-eye               next piece of content is for the right eye\n"
58         "      --channel <channel>       next piece of content should be mapped to audio channel L, R, C, Lfe, Ls or Rs\n"
59         "      --gain                    next piece of content should have the given audio gain (in dB)\n"
60         "      --cpl <id>                CPL ID to use from the next piece of content (which is a DCP)\n"
61         "      --kdm <file>              KDM for next piece of content\n";
62
63
64 template <class T>
65 void
66 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
67 {
68         string const a = argv[n];
69         if (a != short_name && a != long_name) {
70                 return;
71         }
72
73         if ((n + 1) >= argc) {
74                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
75                 return;
76         }
77
78         *out = dcp::raw_convert<T>(string(argv[++n]));
79         *claimed = true;
80 }
81
82
83 template <class T>
84 void
85 argument_option (
86         int& n,
87         int argc,
88         char* argv[],
89         string short_name,
90         string long_name,
91         bool* claimed,
92         optional<string>* error,
93         boost::optional<T>* out,
94         std::function<boost::optional<T> (string s)> convert = [](string s) { return dcp::raw_convert<T>(s); }
95         )
96 {
97         string const a = argv[n];
98         if (a != short_name && a != long_name) {
99                 return;
100         }
101
102         if ((n + 1) >= argc) {
103                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
104                 return;
105         }
106
107         auto const arg = argv[++n];
108         auto const value = convert(arg);
109         if (!value) {
110                 *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
111                 *claimed = true;
112                 return;
113         }
114
115         *out = value;
116         *claimed = true;
117 }
118
119
120 CreateCLI::CreateCLI (int argc, char* argv[])
121         : version (false)
122         , encrypt (false)
123         , threed (false)
124         , dcp_content_type (nullptr)
125         , container_ratio (nullptr)
126         , still_length (10)
127         , standard (dcp::Standard::SMPTE)
128         , no_use_isdcf_name (false)
129         , twok (false)
130         , fourk (false)
131 {
132         string dcp_content_type_string = "TST";
133         string container_ratio_string;
134         string standard_string = "SMPTE";
135         int dcp_frame_rate_int = 0;
136         string template_name_string;
137         int j2k_bandwidth_int = 0;
138         auto next_frame_type = VideoFrameType::TWO_D;
139         optional<dcp::Channel> channel;
140         optional<float> gain;
141         optional<boost::filesystem::path> kdm;
142         optional<std::string> cpl;
143
144         int i = 1;
145         while (i < argc) {
146                 string const a = argv[i];
147                 bool claimed = false;
148
149                 if (a == "-v" || a == "--version") {
150                         version = true;
151                         return;
152                 } else if (a == "-h" || a == "--help") {
153                         error = "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
154                                 "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
155                                 "will be written to stdout.\n" + String::compose(_help, argv[0]);
156                         return;
157                 }
158
159                 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 == "--threed") {
164                         threed = claimed = true;
165                 } else if (a == "--left-eye") {
166                         next_frame_type = VideoFrameType::THREE_D_LEFT;
167                         claimed = true;
168                 } else if (a == "--right-eye") {
169                         next_frame_type = VideoFrameType::THREE_D_RIGHT;
170                         claimed = true;
171                 } else if (a == "--twok") {
172                         twok = true;
173                         claimed = true;
174                 } else if (a == "--fourk") {
175                         fourk = true;
176                         claimed = true;
177                 }
178
179                 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
180                         return boost::filesystem::path(s);
181                 };
182
183                 argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &name);
184                 argument_option(i, argc, argv, "-t", "--template",         &claimed, &error, &template_name_string);
185                 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
186                 argument_option(i, argc, argv, "-f", "--dcp-frame-rate",   &claimed, &error, &dcp_frame_rate_int);
187                 argument_option(i, argc, argv, "",   "--container-ratio",  &claimed, &error, &container_ratio_string);
188                 argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length);
189                 argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string);
190                 argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir, string_to_path);
191                 argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir, string_to_path);
192                 argument_option(i, argc, argv, "",   "--j2k-bandwidth",    &claimed, &error, &j2k_bandwidth_int);
193
194                 std::function<optional<dcp::Channel> (string)> convert_channel = [](string channel) -> optional<dcp::Channel>{
195                         if (channel == "L") {
196                                 return dcp::Channel::LEFT;
197                         } else if (channel == "R") {
198                                 return dcp::Channel::RIGHT;
199                         } else if (channel == "C") {
200                                 return dcp::Channel::CENTRE;
201                         } else if (channel == "Lfe") {
202                                 return dcp::Channel::LFE;
203                         } else if (channel == "Ls") {
204                                 return dcp::Channel::LS;
205                         } else if (channel == "Rs") {
206                                 return dcp::Channel::RS;
207                         } else {
208                                 return {};
209                         }
210                 };
211
212                 argument_option(i, argc, argv, "", "--channel", &claimed, &error, &channel, convert_channel);
213                 argument_option(i, argc, argv, "", "--gain", &claimed, &error, &gain);
214                 argument_option(i, argc, argv, "", "--kdm", &claimed, &error, &kdm, string_to_path);
215                 argument_option(i, argc, argv, "", "--cpl", &claimed, &error, &cpl);
216
217                 if (!claimed) {
218                         if (a.length() > 2 && a.substr(0, 2) == "--") {
219                                 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
220                                 return;
221                         } else {
222                                 Content c;
223                                 c.path = a;
224                                 c.frame_type = next_frame_type;
225                                 c.channel = channel;
226                                 c.gain = gain;
227                                 c.kdm = kdm;
228                                 c.cpl = cpl;
229                                 content.push_back (c);
230                                 next_frame_type = VideoFrameType::TWO_D;
231                                 channel = {};
232                                 gain = {};
233                         }
234                 }
235
236                 ++i;
237         }
238
239         if (!template_name_string.empty()) {
240                 template_name = template_name_string;
241         }
242
243         if (dcp_frame_rate_int) {
244                 dcp_frame_rate = dcp_frame_rate_int;
245         }
246
247         if (j2k_bandwidth_int) {
248                 j2k_bandwidth = j2k_bandwidth_int * 1000000;
249         }
250
251         dcp_content_type = DCPContentType::from_isdcf_name(dcp_content_type_string);
252         if (!dcp_content_type) {
253                 error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], dcp_content_type_string);
254                 return;
255         }
256
257         if (!container_ratio_string.empty()) {
258                 container_ratio = Ratio::from_id (container_ratio_string);
259                 if (!container_ratio) {
260                         error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
261                         return;
262                 }
263         }
264
265         if (standard_string != "SMPTE" && standard_string != "interop") {
266                 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
267                 return;
268         }
269
270         if (standard_string == "interop") {
271                 standard = dcp::Standard::INTEROP;
272         }
273
274         if (content.empty()) {
275                 error = String::compose("%1: no content specified", argv[0]);
276                 return;
277         }
278
279         if (name.empty()) {
280                 name = content[0].path.leaf().string();
281         }
282
283         if (j2k_bandwidth && (*j2k_bandwidth < 10000000 || *j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
284                 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
285                 return;
286         }
287 }