Add wrappers around boost::filesystem methods that handle the
[libdcp.git] / tools / dcpdiff.cc
1 /*
2     Copyright (C) 2012-2014 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 "common.h"
36 #include "dcp.h"
37 #include "equality_options.h"
38 #include "exceptions.h"
39 #include "filesystem.h"
40 #include "mxf.h"
41 #include <getopt.h>
42 #include <boost/optional.hpp>
43 #include <memory>
44 #include <boost/filesystem.hpp>
45 #include <iostream>
46 #include <list>
47
48
49 using std::list;
50 using std::cerr;
51 using std::cout;
52 using std::string;
53 using std::shared_ptr;
54 using std::vector;
55 using boost::optional;
56 using std::dynamic_pointer_cast;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
59 #endif
60 using namespace dcp;
61
62 static bool verbose = false;
63
64 static void
65 help (string n)
66 {
67         cerr << "Syntax: " << n << " [OPTION] <DCP> <DCP>\n"
68              << "  -V, --version                     show libdcp version\n"
69              << "  -h, --help                        show this help\n"
70              << "  -v, --verbose                     be verbose\n"
71              << "      --cpl-annotation-texts        allow differing CPL annotation texts\n"
72              << "      --reel-annotation-texts       allow differing reel annotation texts\n"
73              << "  -a, --annotation-texts            allow different CPL and reel annotation texts\n"
74              << "  -d, --issue-dates                 allow different issue dates\n"
75              << "  -m, --mean-pixel                  maximum allowed mean pixel error (default 5)\n"
76              << "  -s, --std-dev-pixel               maximum allowed standard deviation of pixel error (default 5)\n"
77              << "      --key                         hexadecimal key to use to decrypt MXFs\n"
78              << "      --ignore-missing-assets       ignore missing asset files\n"
79              << "      --export-differing-subtitles  export the first pair of differing image subtitles to the current working directory\n"
80              << "\n"
81              << "The <DCP>s are the DCP directories to compare.\n"
82              << "Comparison is of metadata and content, ignoring timestamps\n"
83              << "and differing UUIDs.\n";
84 }
85
86 void
87 note (NoteType t, string n)
88 {
89         if (t == NoteType::ERROR || verbose) {
90                 cout << " " << n << "\n";
91                 cout.flush ();
92         }
93 }
94
95 static
96 DCP *
97 load_dcp (boost::filesystem::path path, bool ignore_missing_assets, optional<string> key)
98 {
99         DCP* dcp = 0;
100         try {
101                 dcp = new DCP (path);
102                 vector<dcp::VerificationNote> notes;
103                 dcp->read (&notes);
104                 filter_notes (notes, ignore_missing_assets);
105                 for (auto i: notes) {
106                         cerr << dcp::note_to_string(i) << "\n";
107                 }
108
109                 if (key) {
110                         auto assets = dcp->assets ();
111                         for (auto i: assets) {
112                                 auto mxf = dynamic_pointer_cast<MXF>(i);
113                                 if (mxf) {
114                                         mxf->set_key (Key (key.get ()));
115                                 }
116                         }
117                 }
118
119         } catch (FileError& e) {
120                 cerr << "Could not read DCP " << path.string() << "; " << e.what() << " " << e.filename() << "\n";
121                 exit (EXIT_FAILURE);
122         }
123
124         return dcp;
125 }
126
127 int
128 main (int argc, char* argv[])
129 {
130         dcp::init ();
131
132         EqualityOptions options;
133         options.max_mean_pixel_error = 5;
134         options.max_std_dev_pixel_error = 5;
135         options.reel_hashes_can_differ = true;
136         options.reel_annotation_texts_can_differ = false;
137         bool ignore_missing_assets = false;
138         optional<string> key;
139
140         int option_index = 0;
141         while (1) {
142                 static struct option long_options[] = {
143                         { "version", no_argument, 0, 'V'},
144                         { "help", no_argument, 0, 'h'},
145                         { "verbose", no_argument, 0, 'v'},
146                         { "mean-pixel", required_argument, 0, 'm'},
147                         { "std-dev-pixel", required_argument, 0, 's'},
148                         { "annotation-texts", no_argument, 0, 'a'},
149                         { "issue-dates", no_argument, 0, 'd'},
150                         /* From here we're using random capital letters for the short option */
151                         { "ignore-missing-assets", no_argument, 0, 'A'},
152                         { "cpl-annotation-texts", no_argument, 0, 'C'},
153                         { "key", required_argument, 0, 'D'},
154                         { "reel-annotation-texts", no_argument, 0, 'E'},
155                         { "export-differing-subtitles", no_argument, 0, 'F' },
156                         { 0, 0, 0, 0 }
157                 };
158
159                 int c = getopt_long (argc, argv, "Vhvm:s:adACD:EF", long_options, &option_index);
160
161                 if (c == -1) {
162                         break;
163                 }
164
165                 switch (c) {
166                 case 'V':
167                         cout << "dcpdiff version " << dcp::version << "\n";
168                         exit (EXIT_SUCCESS);
169                 case 'h':
170                         help (argv[0]);
171                         exit (EXIT_SUCCESS);
172                 case 'v':
173                         verbose = true;
174                         break;
175                 case 'm':
176                         options.max_mean_pixel_error = atof (optarg);
177                         break;
178                 case 's':
179                         options.max_std_dev_pixel_error = atof (optarg);
180                         break;
181                 case 'a':
182                         options.cpl_annotation_texts_can_differ = options.reel_annotation_texts_can_differ = true;
183                         break;
184                 case 'd':
185                         options.issue_dates_can_differ = true;
186                         break;
187                 case 'A':
188                         ignore_missing_assets = true;
189                         break;
190                 case 'B':
191                 case 'C':
192                         options.cpl_annotation_texts_can_differ = true;
193                         break;
194                 case 'D':
195                         key = string (optarg);
196                         break;
197                 case 'E':
198                         options.reel_annotation_texts_can_differ = true;
199                         break;
200                 case 'F':
201                         options.export_differing_subtitles = true;
202                         break;
203                 }
204         }
205
206         if (argc <= (optind + 1) || argc > (optind + 2)) {
207                 help (argv[0]);
208                 exit (EXIT_FAILURE);
209         }
210
211         if (!filesystem::exists(argv[optind])) {
212                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
213                 exit (EXIT_FAILURE);
214         }
215
216         if (!filesystem::exists(argv[optind + 1])) {
217                 cerr << argv[0] << ": DCP " << argv[optind + 1] << " not found.\n";
218                 exit (EXIT_FAILURE);
219         }
220
221         DCP* a = load_dcp (argv[optind], ignore_missing_assets, key);
222         DCP* b = load_dcp (argv[optind + 1], ignore_missing_assets, key);
223
224         /* I think this is just below the LSB at 16-bits (ie the 8th most significant bit at 24-bit) */
225         options.max_audio_sample_error = 255;
226
227         bool const equals = a->equals (*b, options, boost::bind (note, _1, _2));
228
229         exit (equals ? EXIT_SUCCESS : EXIT_FAILURE);
230 }