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