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