Cleanup: use some better variable names.
[dcpomatic.git] / src / tools / dcpomatic_create.cc
1 /*
2     Copyright (C) 2013-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 "lib/audio_content.h"
22 #include "lib/config.h"
23 #include "lib/content_factory.h"
24 #include "lib/create_cli.h"
25 #include "lib/cross.h"
26 #include "lib/dcp_content.h"
27 #include "lib/dcp_content_type.h"
28 #include "lib/dcpomatic_log.h"
29 #include "lib/film.h"
30 #include "lib/image_content.h"
31 #include "lib/job.h"
32 #include "lib/job_manager.h"
33 #include "lib/ratio.h"
34 #include "lib/signal_manager.h"
35 #include "lib/util.h"
36 #include "lib/version.h"
37 #include "lib/version.h"
38 #include "lib/video_content.h"
39 #include <dcp/exceptions.h>
40 #include <libxml++/libxml++.h>
41 #include <boost/filesystem.hpp>
42 #include <getopt.h>
43 #include <cstdlib>
44 #include <iostream>
45 #include <stdexcept>
46 #include <string>
47
48 using std::cerr;
49 using std::cout;
50 using std::dynamic_pointer_cast;
51 using std::exception;
52 using std::list;
53 using std::make_shared;
54 using std::shared_ptr;
55 using std::string;
56 using boost::optional;
57
58 class SimpleSignalManager : public SignalManager
59 {
60 public:
61         /* Do nothing in this method so that UI events happen in our thread
62            when we call SignalManager::ui_idle().
63         */
64         void wake_ui () {}
65 };
66
67 int
68 main (int argc, char* argv[])
69 {
70         dcpomatic_setup_path_encoding ();
71         dcpomatic_setup ();
72
73         CreateCLI cc (argc, argv);
74         if (cc.error) {
75                 cerr << *cc.error << "\n";
76                 exit (1);
77         }
78
79         if (cc.version) {
80                 cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
81                 exit (EXIT_SUCCESS);
82         }
83
84         if (cc.config_dir) {
85                 State::override_path = *cc.config_dir;
86         }
87
88         signal_manager = new SimpleSignalManager ();
89         auto jm = JobManager::instance ();
90
91         try {
92                 auto film = std::make_shared<Film>(cc.output_dir);
93                 dcpomatic_log = film->log ();
94                 dcpomatic_log->set_types (Config::instance()->log_types());
95                 if (cc.template_name) {
96                         film->use_template (cc.template_name.get());
97                 }
98                 film->set_name (cc.name);
99
100                 if (cc.container_ratio) {
101                         film->set_container (cc.container_ratio);
102                 }
103                 film->set_dcp_content_type (cc.dcp_content_type);
104                 film->set_interop (cc.standard == dcp::Standard::INTEROP);
105                 film->set_use_isdcf_name (!cc.no_use_isdcf_name);
106                 film->set_encrypted (cc.encrypt);
107                 film->set_three_d (cc.threed);
108                 if (cc.twok) {
109                         film->set_resolution (Resolution::TWO_K);
110                 }
111                 if (cc.fourk) {
112                         film->set_resolution (Resolution::FOUR_K);
113                 }
114                 if (cc.j2k_bandwidth) {
115                         film->set_j2k_bandwidth (*cc.j2k_bandwidth);
116                 }
117
118                 for (auto cli_content: cc.content) {
119                         auto const can = boost::filesystem::canonical (cli_content.path);
120                         list<shared_ptr<Content>> film_content_list;
121
122                         if (boost::filesystem::exists (can / "ASSETMAP") || (boost::filesystem::exists (can / "ASSETMAP.xml"))) {
123                                 film_content_list.push_back (make_shared<DCPContent>(can));
124                         } else {
125                                 /* I guess it's not a DCP */
126                                 film_content_list = content_factory (can);
127                         }
128
129                         for (auto film_content: film_content_list) {
130                                 film->examine_and_add_content (film_content);
131                         }
132
133                         while (jm->work_to_do ()) {
134                                 dcpomatic_sleep_seconds (1);
135                         }
136
137                         while (signal_manager->ui_idle() > 0) {}
138
139                         for (auto film_content: film_content_list) {
140                                 if (film_content->video) {
141                                         film_content->video->set_frame_type (cli_content.frame_type);
142                                 }
143                                 if (film_content->audio && cli_content.channel) {
144                                         for (auto stream: film_content->audio->streams()) {
145                                                 AudioMapping mapping(stream->channels(), film->audio_channels());
146                                                 for (int channel = 0; channel < stream->channels(); ++channel) {
147                                                         mapping.set(channel, *cli_content.channel, 1.0f);
148                                                 }
149                                                 stream->set_mapping (mapping);
150                                         }
151                                 }
152                                 if (film_content->audio && cli_content.gain) {
153                                         film_content->audio->set_gain (*cli_content.gain);
154                                 }
155                         }
156                 }
157
158                 if (cc.dcp_frame_rate) {
159                         film->set_video_frame_rate (*cc.dcp_frame_rate);
160                 }
161
162                 for (auto i: film->content()) {
163                         auto ic = dynamic_pointer_cast<ImageContent> (i);
164                         if (ic && ic->still()) {
165                                 ic->video->set_length (cc.still_length * 24);
166                         }
167                 }
168
169                 if (jm->errors ()) {
170                         for (auto i: jm->get()) {
171                                 if (i->finished_in_error()) {
172                                         cerr << i->error_summary() << "\n"
173                                              << i->error_details() << "\n";
174                                 }
175                         }
176                         exit (EXIT_FAILURE);
177                 }
178
179                 if (cc.output_dir) {
180                         film->write_metadata ();
181                 } else {
182                         film->metadata()->write_to_stream_formatted(cout, "UTF-8");
183                 }
184         } catch (exception& e) {
185                 cerr << argv[0] << ": " << e.what() << "\n";
186                 exit (EXIT_FAILURE);
187         }
188
189         return 0;
190 }