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