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