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