Add an assertion.
[dcpomatic.git] / src / lib / create_cli.cc
1 /*
2     Copyright (C) 2019 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 #include "create_cli.h"
22 #include "dcp_content_type.h"
23 #include "ratio.h"
24 #include "config.h"
25 #include "compose.hpp"
26 #include <dcp/raw_convert.h>
27 #include <string>
28
29 #include <iostream>
30
31 using std::string;
32 using std::cout;
33 using boost::optional;
34
35 string CreateCLI::_help =
36         "\nSyntax: %1 [OPTION] <CONTENT> [OPTION] [<CONTENT> ...]\n"
37         "  -v, --version                 show DCP-o-matic version\n"
38         "  -h, --help                    show this help\n"
39         "  -n, --name <name>             film name\n"
40         "  -t, --template <name>         template name\n"
41         "  -e, --encrypt                 make an encrypted DCP\n"
42         "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
43         "  -f, --dcp-frame-rate <rate>   set DCP video frame rate (otherwise guessed from content)\n"
44         "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
45         "      --content-ratio <ratio>   119, 133, 137, 138, 166, 178, 185 or 239\n"
46         "  -s, --still-length <n>        number of seconds that still content should last\n"
47         "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
48         "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
49         "      --no-sign                 do not sign the DCP\n"
50         "      --config <dir>            directory containing config.xml and cinemas.xml\n"
51         "      --fourk                   make a 4K DCP rather than a 2K one\n"
52         "  -o, --output <dir>            output directory\n"
53         "      --threed                  make a 3D DCP\n"
54         "      --j2k-bandwidth <Mbit/s>  J2K bandwidth in Mbit/s\n"
55         "      --left-eye                next piece of content is for the left eye\n"
56         "      --right-eye               next piece of content is for the right eye\n";
57
58 template <class T>
59 void
60 argument_option (int& n, int argc, char* argv[], string short_name, string long_name, bool* claimed, optional<string>* error, T* out)
61 {
62         string const a = argv[n];
63         if (a != short_name && a != long_name) {
64                 return;
65         }
66
67         if ((n + 1) >= argc) {
68                 **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
69                 return;
70         }
71
72         *out = dcp::raw_convert<T>(string(argv[++n]));
73         *claimed = true;
74 }
75
76 CreateCLI::CreateCLI (int argc, char* argv[])
77         : version (false)
78         , encrypt (false)
79         , threed (false)
80         , dcp_content_type (0)
81         , dcp_frame_rate (24)
82         , container_ratio (0)
83         , content_ratio (0)
84         , still_length (10)
85         , standard (dcp::SMPTE)
86         , no_use_isdcf_name (false)
87         , no_sign (false)
88         , fourk (false)
89 {
90         string dcp_content_type_string = "TST";
91         string content_ratio_string;
92         string container_ratio_string;
93         string standard_string = "SMPTE";
94         int dcp_frame_rate_int = 0;
95         string template_name_string;
96         string config_dir_string;
97         string output_dir_string;
98         int j2k_bandwidth_int = 0;
99         VideoFrameType next_frame_type = VIDEO_FRAME_TYPE_2D;
100
101         int i = 1;
102         while (i < argc) {
103                 string const a = argv[i];
104                 bool claimed = false;
105
106                 if (a == "-v" || a == "--version") {
107                         version = true;
108                         return;
109                 } else if (a == "-h" || a == "--help") {
110                         error = "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
111                                 "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
112                                 "will be written to stdout.\n" + String::compose(_help, argv[0]);
113                         return;
114                 }
115
116                 if (a == "-e" || a == "--encrypt") {
117                         encrypt = claimed = true;
118                 } else if (a == "--no-use-isdcf-name") {
119                         no_use_isdcf_name = claimed = true;
120                 } else if (a == "--no-sign") {
121                         no_sign = claimed = true;
122                 } else if (a == "--threed") {
123                         threed = claimed = true;
124                 } else if (a == "--left-eye") {
125                         next_frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
126                         claimed = true;
127                 } else if (a == "--right-eye") {
128                         next_frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
129                         claimed = true;
130                 } else if (a == "--fourk") {
131                         fourk = true;
132                         claimed = true;
133                 }
134
135                 argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &name);
136                 argument_option(i, argc, argv, "-t", "--template",         &claimed, &error, &template_name_string);
137                 argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
138                 argument_option(i, argc, argv, "-f", "--dcp-frame-rate",   &claimed, &error, &dcp_frame_rate_int);
139                 argument_option(i, argc, argv, "",   "--container-ratio",  &claimed, &error, &container_ratio_string);
140                 argument_option(i, argc, argv, "",   "--content-ratio",    &claimed, &error, &content_ratio_string);
141                 argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length);
142                 argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string);
143                 argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir_string);
144                 argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir_string);
145                 argument_option(i, argc, argv, "",   "--j2k-bandwidth",    &claimed, &error, &j2k_bandwidth_int);
146
147                 if (!claimed) {
148                         if (a.length() > 2 && a.substr(0, 2) == "--") {
149                                 error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
150                                 return;
151                         } else {
152                                 Content c;
153                                 c.path = a;
154                                 c.frame_type = next_frame_type;
155                                 content.push_back (c);
156                                 next_frame_type = VIDEO_FRAME_TYPE_2D;
157                         }
158                 }
159
160                 ++i;
161         }
162
163         if (!config_dir_string.empty()) {
164                 config_dir = config_dir_string;
165         }
166
167         if (!output_dir_string.empty()) {
168                 output_dir = output_dir_string;
169         }
170
171         if (!template_name_string.empty()) {
172                 template_name = template_name_string;
173         }
174
175         if (dcp_frame_rate_int) {
176                 dcp_frame_rate = dcp_frame_rate_int;
177         }
178
179         if (j2k_bandwidth_int) {
180                 j2k_bandwidth = j2k_bandwidth_int * 1000000;
181         }
182
183         dcp_content_type = DCPContentType::from_isdcf_name(dcp_content_type_string);
184         if (!dcp_content_type) {
185                 error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], dcp_content_type_string);
186                 return;
187         }
188
189         if (content_ratio_string.empty()) {
190                 error = String::compose("%1: missing required option --content-ratio", argv[0]);
191                 return;
192         }
193
194         content_ratio = Ratio::from_id (content_ratio_string);
195         if (!content_ratio) {
196                 error = String::compose("%1: unrecognised content ratio %2", content_ratio_string);
197                 return;
198         }
199
200         if (container_ratio_string.empty()) {
201                 container_ratio_string = content_ratio_string;
202         }
203
204         container_ratio = Ratio::from_id (container_ratio_string);
205         if (!container_ratio) {
206                 error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
207                 return;
208         }
209
210         if (standard_string != "SMPTE" && standard_string != "interop") {
211                 error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
212                 return;
213         }
214
215         if (content.empty()) {
216                 error = String::compose("%1: no content specified", argv[0]);
217                 return;
218         }
219
220         if (name.empty()) {
221                 name = content[0].path.leaf().string();
222         }
223
224         if (j2k_bandwidth && (*j2k_bandwidth < 10000000 || *j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
225                 error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
226                 return;
227         }
228 }