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