Use plain git hash for VERSION when there is no exact tag.
[dcpomatic.git] / src / lib / create_cli.cc
index b670282b4717a0bfcbc3988b391a55cd1f5d1bcb..1c2f2c6354fcf3ef4f2edf73096c9add9af39c22 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2019-2022 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
+
+#include "compose.hpp"
+#include "config.h"
 #include "create_cli.h"
 #include "dcp_content_type.h"
+#include "dcpomatic_log.h"
+#include "film.h"
 #include "ratio.h"
-#include "config.h"
-#include "compose.hpp"
 #include <dcp/raw_convert.h>
+#include <iostream>
 #include <string>
 
-#include <iostream>
 
-using std::string;
 using std::cout;
+using std::make_shared;
+using std::shared_ptr;
+using std::string;
+using std::vector;
 using boost::optional;
 
+
 string CreateCLI::_help =
        "\nSyntax: %1 [OPTION] <CONTENT> [OPTION] [<CONTENT> ...]\n"
        "  -v, --version                 show DCP-o-matic version\n"
        "  -h, --help                    show this help\n"
        "  -n, --name <name>             film name\n"
        "  -t, --template <name>         template name\n"
+       "      --no-encrypt              make an unencrypted DCP\n"
        "  -e, --encrypt                 make an encrypted DCP\n"
        "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
        "  -f, --dcp-frame-rate <rate>   set DCP video frame rate (otherwise guessed from content)\n"
@@ -45,14 +53,20 @@ string CreateCLI::_help =
        "  -s, --still-length <n>        number of seconds that still content should last\n"
        "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
        "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
-       "      --no-sign                 do not sign the DCP\n"
        "      --config <dir>            directory containing config.xml and cinemas.xml\n"
-       "      --fourk                   make a 4K DCP rather than a 2K one\n"
+       "      --twok                    make a 2K DCP instead of choosing a resolution based on the content\n"
+       "      --fourk                   make a 4K DCP instead of choosing a resolution based on the content\n"
        "  -o, --output <dir>            output directory\n"
+       "      --twod                    make a 2D DCP\n"
        "      --threed                  make a 3D DCP\n"
        "      --j2k-bandwidth <Mbit/s>  J2K bandwidth in Mbit/s\n"
        "      --left-eye                next piece of content is for the left eye\n"
-       "      --right-eye               next piece of content is for the right eye\n";
+       "      --right-eye               next piece of content is for the right eye\n"
+       "      --channel <channel>       next piece of content should be mapped to audio channel L, R, C, Lfe, Ls, Rs, BsL, BsR, HI, VI\n"
+       "      --gain                    next piece of content should have the given audio gain (in dB)\n"
+       "      --cpl <id>                CPL ID to use from the next piece of content (which is a DCP)\n"
+       "      --kdm <file>              KDM for next piece of content\n";
+
 
 template <class T>
 void
@@ -72,27 +86,58 @@ argument_option (int& n, int argc, char* argv[], string short_name, string long_
        *claimed = true;
 }
 
+
+template <class T>
+void
+argument_option (
+       int& n,
+       int argc,
+       char* argv[],
+       string short_name,
+       string long_name,
+       bool* claimed,
+       optional<string>* error,
+       boost::optional<T>* out,
+       std::function<boost::optional<T> (string s)> convert = [](string s) { return dcp::raw_convert<T>(s); }
+       )
+{
+       string const a = argv[n];
+       if (a != short_name && a != long_name) {
+               return;
+       }
+
+       if ((n + 1) >= argc) {
+               **error = String::compose("%1: option %2 requires an argument", argv[0], long_name);
+               return;
+       }
+
+       auto const arg = argv[++n];
+       auto const value = convert(arg);
+       if (!value) {
+               *error = String::compose("%1: %2 is not valid for %3", argv[0], arg, long_name);
+               *claimed = true;
+               return;
+       }
+
+       *out = value;
+       *claimed = true;
+}
+
+
 CreateCLI::CreateCLI (int argc, char* argv[])
        : version (false)
-       , encrypt (false)
-       , threed (false)
-       , dcp_content_type (0)
-       , container_ratio (0)
-       , still_length (10)
-       , standard (dcp::SMPTE)
-       , no_use_isdcf_name (false)
-       , no_sign (false)
-       , fourk (false)
 {
-       string dcp_content_type_string = "TST";
+       optional<string> dcp_content_type_string;
        string container_ratio_string;
-       string standard_string = "SMPTE";
+       optional<string> standard_string;
        int dcp_frame_rate_int = 0;
        string template_name_string;
-       string config_dir_string;
-       string output_dir_string;
        int j2k_bandwidth_int = 0;
-       VideoFrameType next_frame_type = VIDEO_FRAME_TYPE_2D;
+       auto next_frame_type = VideoFrameType::TWO_D;
+       optional<dcp::Channel> channel;
+       optional<float> gain;
+       optional<boost::filesystem::path> kdm;
+       optional<string> cpl;
 
        int i = 1;
        while (i < argc) {
@@ -109,36 +154,93 @@ CreateCLI::CreateCLI (int argc, char* argv[])
                        return;
                }
 
-               if (a == "-e" || a == "--encrypt") {
-                       encrypt = claimed = true;
+               if (a == "--no-encrypt") {
+                       _no_encrypt = claimed = true;
+               } else if (a == "-e" || a == "--encrypt") {
+                       _encrypt = claimed = true;
                } else if (a == "--no-use-isdcf-name") {
-                       no_use_isdcf_name = claimed = true;
-               } else if (a == "--no-sign") {
-                       no_sign = claimed = true;
+                       _no_use_isdcf_name = claimed = true;
+               } else if (a == "--twod") {
+                       _twod = claimed = true;
                } else if (a == "--threed") {
-                       threed = claimed = true;
+                       _threed = claimed = true;
                } else if (a == "--left-eye") {
-                       next_frame_type = VIDEO_FRAME_TYPE_3D_LEFT;
+                       next_frame_type = VideoFrameType::THREE_D_LEFT;
                        claimed = true;
                } else if (a == "--right-eye") {
-                       next_frame_type = VIDEO_FRAME_TYPE_3D_RIGHT;
+                       next_frame_type = VideoFrameType::THREE_D_RIGHT;
+                       claimed = true;
+               } else if (a == "--twok") {
+                       _twok = true;
                        claimed = true;
                } else if (a == "--fourk") {
-                       fourk = true;
+                       _fourk = true;
                        claimed = true;
                }
 
-               argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &name);
+               std::function<optional<string> (string s)> string_to_string = [](string s) {
+                       return s;
+               };
+
+               std::function<optional<boost::filesystem::path> (string s)> string_to_path = [](string s) {
+                       return boost::filesystem::path(s);
+               };
+
+               std::function<optional<int> (string s)> string_to_nonzero_int = [](string s) {
+                       auto const value = dcp::raw_convert<int>(s);
+                       if (value == 0) {
+                               return boost::optional<int>();
+                       }
+                       return boost::optional<int>(value);
+               };
+
+               argument_option(i, argc, argv, "-n", "--name",             &claimed, &error, &_name);
                argument_option(i, argc, argv, "-t", "--template",         &claimed, &error, &template_name_string);
-               argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string);
+               /* See comment below about --cpl */
+               argument_option(i, argc, argv, "-c", "--dcp-content-type", &claimed, &error, &dcp_content_type_string, string_to_string);
                argument_option(i, argc, argv, "-f", "--dcp-frame-rate",   &claimed, &error, &dcp_frame_rate_int);
                argument_option(i, argc, argv, "",   "--container-ratio",  &claimed, &error, &container_ratio_string);
-               argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length);
-               argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string);
-               argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir_string);
-               argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir_string);
+               argument_option(i, argc, argv, "-s", "--still-length",     &claimed, &error, &still_length, string_to_nonzero_int);
+               /* See comment below about --cpl */
+               argument_option(i, argc, argv, "",   "--standard",         &claimed, &error, &standard_string, string_to_string);
+               argument_option(i, argc, argv, "",   "--config",           &claimed, &error, &config_dir, string_to_path);
+               argument_option(i, argc, argv, "-o", "--output",           &claimed, &error, &output_dir, string_to_path);
                argument_option(i, argc, argv, "",   "--j2k-bandwidth",    &claimed, &error, &j2k_bandwidth_int);
 
+               std::function<optional<dcp::Channel> (string)> convert_channel = [](string channel) -> optional<dcp::Channel>{
+                       if (channel == "L") {
+                               return dcp::Channel::LEFT;
+                       } else if (channel == "R") {
+                               return dcp::Channel::RIGHT;
+                       } else if (channel == "C") {
+                               return dcp::Channel::CENTRE;
+                       } else if (channel == "Lfe") {
+                               return dcp::Channel::LFE;
+                       } else if (channel == "Ls") {
+                               return dcp::Channel::LS;
+                       } else if (channel == "Rs") {
+                               return dcp::Channel::RS;
+                       } else if (channel == "BsL") {
+                               return dcp::Channel::BSL;
+                       } else if (channel == "BsR") {
+                               return dcp::Channel::BSR;
+                       } else if (channel == "HI") {
+                               return dcp::Channel::HI;
+                       } else if (channel == "VI") {
+                               return dcp::Channel::VI;
+                       } else {
+                               return {};
+                       }
+               };
+
+               argument_option(i, argc, argv, "", "--channel", &claimed, &error, &channel, convert_channel);
+               argument_option(i, argc, argv, "", "--gain", &claimed, &error, &gain);
+               argument_option(i, argc, argv, "", "--kdm", &claimed, &error, &kdm, string_to_path);
+               /* It shouldn't be necessary to use this string_to_string here, but using the other argument_option()
+                * causes an odd compile error on Ubuntu 18.04.
+                */
+               argument_option(i, argc, argv, "", "--cpl", &claimed, &error, &cpl, string_to_string);
+
                if (!claimed) {
                        if (a.length() > 2 && a.substr(0, 2) == "--") {
                                error = String::compose("%1: unrecognised option '%2'", argv[0], a) + String::compose(_help, argv[0]);
@@ -147,24 +249,22 @@ CreateCLI::CreateCLI (int argc, char* argv[])
                                Content c;
                                c.path = a;
                                c.frame_type = next_frame_type;
+                               c.channel = channel;
+                               c.gain = gain;
+                               c.kdm = kdm;
+                               c.cpl = cpl;
                                content.push_back (c);
-                               next_frame_type = VIDEO_FRAME_TYPE_2D;
+                               next_frame_type = VideoFrameType::TWO_D;
+                               channel = {};
+                               gain = {};
                        }
                }
 
                ++i;
        }
 
-       if (!config_dir_string.empty()) {
-               config_dir = config_dir_string;
-       }
-
-       if (!output_dir_string.empty()) {
-               output_dir = output_dir_string;
-       }
-
        if (!template_name_string.empty()) {
-               template_name = template_name_string;
+               _template_name = template_name_string;
        }
 
        if (dcp_frame_rate_int) {
@@ -172,26 +272,42 @@ CreateCLI::CreateCLI (int argc, char* argv[])
        }
 
        if (j2k_bandwidth_int) {
-               j2k_bandwidth = j2k_bandwidth_int * 1000000;
+               _j2k_bandwidth = j2k_bandwidth_int * 1000000;
        }
 
-       dcp_content_type = DCPContentType::from_isdcf_name(dcp_content_type_string);
-       if (!dcp_content_type) {
-               error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], dcp_content_type_string);
-               return;
+       if (dcp_content_type_string) {
+               _dcp_content_type = DCPContentType::from_isdcf_name(*dcp_content_type_string);
+               if (!_dcp_content_type) {
+                       error = String::compose("%1: unrecognised DCP content type '%2'", argv[0], *dcp_content_type_string);
+                       return;
+               }
        }
 
        if (!container_ratio_string.empty()) {
-               container_ratio = Ratio::from_id (container_ratio_string);
-               if (!container_ratio) {
+               _container_ratio = Ratio::from_id (container_ratio_string);
+               if (!_container_ratio) {
                        error = String::compose("%1: unrecognised container ratio %2", argv[0], container_ratio_string);
                        return;
                }
        }
 
-       if (standard_string != "SMPTE" && standard_string != "interop") {
-               error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
-               return;
+       if (standard_string) {
+               if (*standard_string == "interop") {
+                       _standard = dcp::Standard::INTEROP;
+               } else if (*standard_string == "SMPTE") {
+                       _standard = dcp::Standard::SMPTE;
+               } else {
+                       error = String::compose("%1: standard must be SMPTE or interop", argv[0]);
+                       return;
+               }
+       }
+
+       if (_twod && _threed) {
+               error = String::compose("%1: specify one of --twod or --threed, not both", argv[0]);
+       }
+
+       if (_no_encrypt && _encrypt) {
+               error = String::compose("%1: specify one of --no-encrypt or --encrypt, not both", argv[0]);
        }
 
        if (content.empty()) {
@@ -199,12 +315,78 @@ CreateCLI::CreateCLI (int argc, char* argv[])
                return;
        }
 
-       if (name.empty()) {
-               name = content[0].path.leaf().string();
+       if (_name.empty()) {
+               _name = content[0].path.filename().string();
        }
 
-       if (j2k_bandwidth && (*j2k_bandwidth < 10000000 || *j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
+       if (_j2k_bandwidth && (*_j2k_bandwidth < 10000000 || *_j2k_bandwidth > Config::instance()->maximum_j2k_bandwidth())) {
                error = String::compose("%1: j2k-bandwidth must be between 10 and %2 Mbit/s", argv[0], (Config::instance()->maximum_j2k_bandwidth() / 1000000));
                return;
        }
 }
+
+
+shared_ptr<Film>
+CreateCLI::make_film() const
+{
+       auto film = std::make_shared<Film>(output_dir);
+       dcpomatic_log = film->log();
+       dcpomatic_log->set_types(Config::instance()->log_types());
+       if (_template_name) {
+               film->use_template(_template_name.get());
+       } else {
+               /* No template: apply our own CLI tool defaults to override the ones in Config.
+                * Maybe one day there will be no defaults in Config any more (as they'll be in
+                * a default template) and we can decide whether to use the default template
+                * or not.
+                */
+               film->set_interop(false);
+               film->set_dcp_content_type(DCPContentType::from_isdcf_name("TST"));
+       }
+       film->set_name(_name);
+
+       if (_container_ratio) {
+               film->set_container(_container_ratio);
+       }
+       if (_dcp_content_type) {
+               film->set_dcp_content_type(_dcp_content_type);
+       }
+       if (_standard) {
+               film->set_interop(*_standard == dcp::Standard::INTEROP);
+       }
+       film->set_use_isdcf_name(!_no_use_isdcf_name);
+       if (_no_encrypt) {
+               film->set_encrypted(false);
+       } else if (_encrypt) {
+               film->set_encrypted(true);
+       }
+       if (_twod) {
+               film->set_three_d(false);
+       } else if (_threed) {
+               film->set_three_d(true);
+       }
+       if (_twok) {
+               film->set_resolution(Resolution::TWO_K);
+       }
+       if (_fourk) {
+               film->set_resolution(Resolution::FOUR_K);
+       }
+       if (_j2k_bandwidth) {
+               film->set_j2k_bandwidth(*_j2k_bandwidth);
+       }
+
+       int channels = 6;
+       for (auto cli_content: content) {
+               if (cli_content.channel) {
+                       channels = std::max(channels, static_cast<int>(*cli_content.channel) + 1);
+               }
+       }
+       if (channels % 2) {
+               ++channels;
+       }
+
+       film->set_audio_channels(channels);
+
+       return film;
+}
+