Remove non-existant option from create help.
[dcpomatic.git] / src / lib / create_cli.cc
1 /*
2     Copyright (C) 2019-2021 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
61
62 template <class T>
63 void
64 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
65 {
66         string const a = argv[n];
67         if (a != short_name && a != long_name) {
68                 return;
69         }
70
71         if ((n + 1) >= argc) {
72                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
73                 return;
74         }
75
76         *out = dcp::raw_convert<T>(string(argv[++n]));
77         *claimed = true;
78 }
79
80
81 template <class T>
82 void
83 argument_option (
84         int& n,
85         int argc,
86         char* argv[],
87         string short_name,
88         string long_name,
89         bool* claimed,
90         optional<string>* error,
91         boost::optional<T>* out,
92         std::function<boost::optional<T> (string s)> convert = [](string s) { return dcp::raw_convert<T>(s); }
93         )
94 {
95         string const a = argv[n];
96         if (a != short_name && a != long_name) {
97                 return;
98         }
99
100         if ((n + 1) >= argc) {
101                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
102                 return;
103         }
104
105         auto const arg = argv[++n];
106         auto const value = convert(arg);
107         if (!value) {
108                 *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
109                 *claimed = true;
110                 return;
111         }
112
113         *out = value;
114         *claimed = true;
115 }
116
117
118 CreateCLI::CreateCLI (int argc, char* argv[])
119         : version (false)
120         , encrypt (false)
121         , threed (false)
122         , dcp_content_type (nullptr)
123         , container_ratio (nullptr)
124         , still_length (10)
125         , standard (dcp::Standard::SMPTE)
126         , no_use_isdcf_name (false)
127         , twok (false)
128         , fourk (false)
129 {
130         string dcp_content_type_string = "TST";
131         string container_ratio_string;
132         string standard_string = "SMPTE";
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
140         int i = 1;
141         while (i < argc) {
142                 string const a = argv[i];
143                 bool claimed = false;
144
145                 if (a == "-v" || a == "--version") {
146                         version = true;
147                         return;
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]);
152                         return;
153                 }
154
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;
163                         claimed = true;
164                 } else if (a == "--right-eye") {
165                         next_frame_type = VideoFrameType::THREE_D_RIGHT;
166                         claimed = true;
167                 } else if (a == "--twok") {
168                         twok = true;
169                         claimed = true;
170                 } else if (a == "--fourk") {
171                         fourk = true;
172                         claimed = true;
173                 }
174
175                 std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
176                         return boost::filesystem::path(s);
177                 };
178
179                 argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &name);
180                 argument_option(i, argc, argv, "-t", "--template",         &claimed, &error, &template_name_string);
181                 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
182                 argument_option(i, argc, argv, "-f", "--dcp-frame-rate",   &claimed, &error, &dcp_frame_rate_int);
183                 argument_option(i, argc, argv, "",   "--container-ratio",  &claimed, &error, &container_ratio_string);
184                 argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length);
185                 argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string);
186                 argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir, string_to_path);
187                 argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir, string_to_path);
188                 argument_option(i, argc, argv, "",   "--j2k-bandwidth",    &claimed, &error, &j2k_bandwidth_int);
189
190                 std::function<optional<dcp::Channel> (string)> convert_channel = [](string channel) -> optional<dcp::Channel>{
191                         if (channel == "L") {
192                                 return dcp::Channel::LEFT;
193                         } else if (channel == "R") {
194                                 return dcp::Channel::RIGHT;
195                         } else if (channel == "C") {
196                                 return dcp::Channel::CENTRE;
197                         } else if (channel == "Lfe") {
198                                 return dcp::Channel::LFE;
199                         } else if (channel == "Ls") {
200                                 return dcp::Channel::LS;
201                         } else if (channel == "Rs") {
202                                 return dcp::Channel::RS;
203                         } else {
204                                 return {};
205                         }
206                 };
207
208                 argument_option(i, argc, argv, "", "--channel", &claimed, &error, &channel, convert_channel);
209                 argument_option(i, argc, argv, "", "--gain", &claimed, &error, &gain);
210
211                 if (!claimed) {
212                         if (a.length() > 2 && a.substr(0, 2) == "--") {
213                                 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
214                                 return;
215                         } else {
216                                 Content c;
217                                 c.path = a;
218                                 c.frame_type = next_frame_type;
219                                 c.channel = channel;
220                                 c.gain = gain;
221                                 content.push_back (c);
222                                 next_frame_type = VideoFrameType::TWO_D;
223                                 channel = {};
224                                 gain = {};
225                         }
226                 }
227
228                 ++i;
229         }
230
231         if (!template_name_string.empty()) {
232                 template_name = template_name_string;
233         }
234
235         if (dcp_frame_rate_int) {
236                 dcp_frame_rate = dcp_frame_rate_int;
237         }
238
239         if (j2k_bandwidth_int) {
240                 j2k_bandwidth = j2k_bandwidth_int * 1000000;
241         }
242
243         dcp_content_type = DCPContentType::from_isdcf_name(dcp_content_type_string);
244         if (!dcp_content_type) {
245                 error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], dcp_content_type_string);
246                 return;
247         }
248
249         if (!container_ratio_string.empty()) {
250                 container_ratio = Ratio::from_id (container_ratio_string);
251                 if (!container_ratio) {
252                         error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
253                         return;
254                 }
255         }
256
257         if (standard_string != "SMPTE" && standard_string != "interop") {
258                 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
259                 return;
260         }
261
262         if (standard_string == "interop") {
263                 standard = dcp::Standard::INTEROP;
264         }
265
266         if (content.empty()) {
267                 error = String::compose("%1: no content specified", argv[0]);
268                 return;
269         }
270
271         if (name.empty()) {
272                 name = content[0].path.leaf().string();
273         }
274
275         if (j2k_bandwidth && (*j2k_bandwidth < 10000000 || *j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
276                 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
277                 return;
278         }
279 }