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