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