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