Try to add basic decryption support to dcpdiff.
[libdcp.git] / tools / dcpdiff.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
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/filesystem.hpp>
26 #include <iostream>
27
28 using namespace std;
29 using namespace boost;
30 using namespace dcp;
31
32 static bool verbose = false;
33
34 static void
35 help (string n)
36 {
37         cerr << "Syntax: " << n << " [OPTION] <DCP> <DCP>\n"
38              << "  -V, --version                show libdcp version\n"
39              << "  -h, --help                   show this help\n"
40              << "  -v, --verbose                be verbose\n"
41              << "  -n, --mxf-filenames          allow differing MXF filenames\n"
42              << "      --cpl-annotation-texts   allow differing CPL annotation texts\n"
43              << "  -m, --mean-pixel             maximum allowed mean pixel error (default 5)\n"
44              << "  -s, --std-dev-pixel          maximum allowed standard deviation of pixel error (default 5)\n"
45              << "      --key                    hexadecimal key to use to decrypt MXFs\n"
46              << "  -k, --keep-going             carry on in the event of errors, if possible\n"
47              << "      --ignore-missing-assets  ignore missing asset files\n"
48              << "\n"
49              << "The <DCP>s are the DCP directories to compare.\n"
50              << "Comparison is of metadata and content, ignoring timestamps\n"
51              << "and differing UUIDs.\n";
52 }
53
54 void
55 note (NoteType t, string n)
56 {
57         if (t == DCP_ERROR || verbose) {
58                 cout << " " << n << "\n";
59         }
60 }
61
62 DCP *
63 load_dcp (boost::filesystem::path path, bool keep_going, bool ignore_missing_assets, optional<string> key)
64 {
65         DCP* dcp = 0;
66         try {
67                 dcp = new DCP (path);
68                 DCP::ReadErrors errors;
69                 dcp->read (keep_going, &errors);
70                 filter_errors (errors, ignore_missing_assets);
71                 for (DCP::ReadErrors::const_iterator i = errors.begin(); i != errors.end(); ++i) {
72                         cerr << (*i)->what() << "\n";
73                 }
74
75                 if (key) {
76                         list<shared_ptr<Asset> > assets = dcp->assets ();
77                         for (list<shared_ptr<Asset> >::const_iterator i = assets.begin(); i != assets.end(); ++i) {
78                                 shared_ptr<MXF> mxf = dynamic_pointer_cast<MXF> (*i);
79                                 if (mxf) {
80                                         mxf->set_key (Key (key.get ()));
81                                 }
82                         }
83                 }
84                 
85         } catch (FileError& e) {
86                 cerr << "Could not read DCP " << path.string() << "; " << e.what() << " " << e.filename() << "\n";
87                 exit (EXIT_FAILURE);
88         }
89
90         return dcp;
91 }
92
93 int
94 main (int argc, char* argv[])
95 {
96         EqualityOptions options;
97         options.max_mean_pixel_error = 5;
98         options.max_std_dev_pixel_error = 5;
99         options.reel_hashes_can_differ = true;
100         bool keep_going = false;
101         bool ignore_missing_assets = false;
102         optional<string> key;
103         
104         int option_index = 0;
105         while (1) {
106                 static struct option long_options[] = {
107                         { "version", no_argument, 0, 'V'},
108                         { "help", no_argument, 0, 'h'},
109                         { "verbose", no_argument, 0, 'v'},
110                         { "mxf-filenames", no_argument, 0, 'n'},
111                         { "mean-pixel", required_argument, 0, 'm'},
112                         { "std-dev-pixel", required_argument, 0, 's'},
113                         { "keep-going", no_argument, 0, 'k'},
114                         /* From here we're using random capital letters for the short option */
115                         { "ignore-missing-assets", no_argument, 0, 'A'},
116                         { "cpl-annotation-texts", no_argument, 0, 'C'},
117                         { "key", no_argument, 0, 'D'},
118                         { 0, 0, 0, 0 }
119                 };
120
121                 int c = getopt_long (argc, argv, "Vhvnm:s:kACD", long_options, &option_index);
122
123                 if (c == -1) {
124                         break;
125                 }
126
127                 switch (c) {
128                 case 'V':
129                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
130                         exit (EXIT_SUCCESS);
131                 case 'h':
132                         help (argv[0]);
133                         exit (EXIT_SUCCESS);
134                 case 'v':
135                         verbose = true;
136                         break;
137                 case 'n':
138                         options.mxf_filenames_can_differ = true;
139                         break;
140                 case 'm':
141                         options.max_mean_pixel_error = atof (optarg);
142                         break;
143                 case 's':
144                         options.max_std_dev_pixel_error = atof (optarg);
145                         break;
146                 case 'k':
147                         keep_going = true;
148                         break;
149                 case 'A':
150                         ignore_missing_assets = true;
151                         break;
152                 case 'B':
153                 case 'C':
154                         options.cpl_annotation_texts_can_differ = true;
155                         break;
156                 case 'D':
157                         key = optarg;
158                         break;
159                 }
160         }
161
162         if (argc <= optind || argc > (optind + 2)) {
163                 help (argv[0]);
164                 exit (EXIT_FAILURE);
165         }
166
167         if (!filesystem::exists (argv[optind])) {
168                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
169                 exit (EXIT_FAILURE);
170         }
171
172         if (!filesystem::exists (argv[optind + 1])) {
173                 cerr << argv[0] << ": DCP " << argv[optind + 1] << " not found.\n";
174                 exit (EXIT_FAILURE);
175         }
176
177         DCP* a = load_dcp (argv[optind], keep_going, ignore_missing_assets, key);
178         DCP* b = load_dcp (argv[optind + 1], keep_going, ignore_missing_assets, key);
179
180         /* I think this is just below the LSB at 16-bits (ie the 8th most significant bit at 24-bit) */
181         options.max_audio_sample_error = 255;
182
183         bool const equals = a->equals (*b, options, boost::bind (note, _1, _2));
184
185         exit (equals ? EXIT_SUCCESS : EXIT_FAILURE);
186 }