Use an integer LUT for PiecewiseLUT2, hence removing a lrint and a multiply from...
[libdcp.git] / tools / dcprecover.cc
1 /*
2     Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "asset_factory.h"
36 #include "cpl.h"
37 #include "dcp.h"
38 #include "exceptions.h"
39 #include "filesystem.h"
40 #include "reel_asset.h"
41 #include "warnings.h"
42 #include <getopt.h>
43 LIBDCP_DISABLE_WARNINGS
44 #include <libxml++/libxml++.h>
45 LIBDCP_ENABLE_WARNINGS
46 #include <boost/filesystem.hpp>
47 #include <iostream>
48
49
50 using std::cerr;
51 using std::cout;
52 using std::make_shared;
53 using std::shared_ptr;
54 using std::string;
55 using std::vector;
56 using boost::optional;
57
58
59 static void
60 help (string n)
61 {
62         cerr << "Syntax: " << n << " [OPTION] <DCP>]\n"
63              << "  -h, --help         show this help\n"
64              << "  -o, --output       output DCP directory\n";
65 }
66
67
68 void progress (float f)
69 {
70         cout << (f * 100) << "%               \r";
71 }
72
73
74 int
75 main (int argc, char* argv[])
76 {
77         dcp::init ();
78
79         int option_index = 0;
80         optional<boost::filesystem::path> output;
81         while (true) {
82                 struct option long_options[] = {
83                         { "help", no_argument, 0, 'h' },
84                         { "output", required_argument, 0, 'o' },
85                         { 0, 0, 0, 0 }
86                 };
87
88                 int c = getopt_long (argc, argv, "ho:", long_options, &option_index);
89
90                 if (c == -1) {
91                         break;
92                 }
93
94                 switch (c) {
95                 case 'h':
96                         help (argv[0]);
97                         exit (EXIT_SUCCESS);
98                 case 'o':
99                         output = optarg;
100                         break;
101                 }
102         }
103
104         if (optind >= argc) {
105                 help (argv[0]);
106                 exit (EXIT_FAILURE);
107         }
108
109         boost::filesystem::path dcp_dir = argv[optind];
110
111         /* Try to read it and report errors */
112
113         dcp::DCP dcp (dcp_dir);
114         vector<dcp::VerificationNote> notes;
115         try {
116                 dcp.read (&notes, true);
117         } catch (dcp::ReadError& e) {
118                 cout << "Error:" <<  e.what() << "\n";
119         }
120
121         for (auto i: notes) {
122                 cout << "Error: " << dcp::note_to_string(i) << "\n";
123         }
124
125         /* Look for a CPL */
126
127         shared_ptr<dcp::CPL> cpl;
128         for (auto i: dcp::filesystem::directory_iterator(dcp_dir)) {
129                 if (i.path().extension() == ".xml") {
130                         try {
131                                 cpl = make_shared<dcp::CPL>(i.path());
132                         } catch (dcp::ReadError& e) {
133                                 cout << "Error: " << e.what() << "\n";
134                         } catch (xmlpp::parse_error& e) {
135                                 cout << "Error: " << e.what() << "\n";
136                         }
137                 }
138         }
139
140         if (cpl) {
141                 cout << "Got a CPL!\n";
142
143                 if (!output) {
144                         cerr << "No output directory specified.\n";
145                         exit(1);
146                 }
147
148                 /* Read all MXF assets */
149                 vector<shared_ptr<dcp::Asset>> assets;
150                 for (auto i: dcp::filesystem::directory_iterator(dcp_dir)) {
151                         if (i.path().extension() == ".mxf") {
152                                 try {
153                                         auto asset = dcp::asset_factory(i.path(), true);
154                                         asset->set_file (*output / i.path().filename());
155                                         cout << "Hashing " << i.path().filename() << "\n";
156                                         asset->hash (&progress);
157                                         cout << "100%                     \n";
158                                         assets.push_back (asset);
159                                 } catch (dcp::ReadError& e) {
160                                         cout << "Error: " << e.what() << "\n";
161                                 }
162                         }
163                 }
164
165                 dcp::DCP fixed (*output);
166                 fixed.add (cpl);
167                 fixed.resolve_refs (assets);
168                 fixed.write_xml ();
169                 cout << "Fixed XML files written to " << output->string() << "\n";
170         }
171
172         return 0;
173 }