Work around symbol clash with ERROR on Win32/mingw.
[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, --names                  allow differing MXF names\n"
41              << "      --cpl-names              allow differing CPL annotation texts (backwards compatible)\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              << "  -k, --keep-going             carry on in the event of errors, if possible\n"
46              << "      --ignore-missing-assets  ignore missing asset files\n"
47              << "\n"
48              << "The <DCP>s are the DCP directories to compare.\n"
49              << "Comparison is of metadata and content, ignoring timestamps\n"
50              << "and differing UUIDs.\n";
51 }
52
53 void
54 note (NoteType t, string n)
55 {
56         if (t == DCP_ERROR || verbose) {
57                 cout << " " << n << "\n";
58         }
59 }
60
61 DCP *
62 load_dcp (boost::filesystem::path path, bool keep_going, bool ignore_missing_assets)
63 {
64         DCP* dcp = 0;
65         try {
66                 dcp = new DCP (path);
67                 DCP::ReadErrors errors;
68                 dcp->read (keep_going, &errors);
69                 filter_errors (errors, ignore_missing_assets);
70                 for (DCP::ReadErrors::const_iterator i = errors.begin(); i != errors.end(); ++i) {
71                         cerr << (*i)->what() << "\n";
72                 }
73         } catch (FileError& e) {
74                 cerr << "Could not read DCP " << path.string() << "; " << e.what() << " " << e.filename() << "\n";
75                 exit (EXIT_FAILURE);
76         }
77
78         return dcp;
79 }
80
81 int
82 main (int argc, char* argv[])
83 {
84         EqualityOptions options;
85         options.max_mean_pixel_error = 5;
86         options.max_std_dev_pixel_error = 5;
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                         { "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-names", no_argument, 0, 'B'},
103                         { "cpl-annotation-texts", no_argument, 0, 'C'},
104                         { 0, 0, 0, 0 }
105                 };
106
107                 int c = getopt_long (argc, argv, "Vhvnm:s:kAB", long_options, &option_index);
108
109                 if (c == -1) {
110                         break;
111                 }
112
113                 switch (c) {
114                 case 'V':
115                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
116                         exit (EXIT_SUCCESS);
117                 case 'h':
118                         help (argv[0]);
119                         exit (EXIT_SUCCESS);
120                 case 'v':
121                         verbose = true;
122                         break;
123                 case 'n':
124                         options.mxf_names_can_differ = true;
125                         break;
126                 case 'm':
127                         options.max_mean_pixel_error = atof (optarg);
128                         break;
129                 case 's':
130                         options.max_std_dev_pixel_error = atof (optarg);
131                         break;
132                 case 'k':
133                         keep_going = true;
134                         break;
135                 case 'A':
136                         ignore_missing_assets = true;
137                         break;
138                 case 'B':
139                 case 'C':
140                         options.cpl_annotation_texts_can_differ = true;
141                         break;
142                 }
143         }
144
145         if (argc <= optind || argc > (optind + 2)) {
146                 help (argv[0]);
147                 exit (EXIT_FAILURE);
148         }
149
150         if (!filesystem::exists (argv[optind])) {
151                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
152                 exit (EXIT_FAILURE);
153         }
154
155         if (!filesystem::exists (argv[optind + 1])) {
156                 cerr << argv[0] << ": DCP " << argv[optind + 1] << " not found.\n";
157                 exit (EXIT_FAILURE);
158         }
159
160         DCP* a = load_dcp (argv[optind], keep_going, ignore_missing_assets);
161         DCP* b = load_dcp (argv[optind + 1], keep_going, ignore_missing_assets);
162
163         /* I think this is just below the LSB at 16-bits (ie the 8th most significant bit at 24-bit) */
164         options.max_audio_sample_error = 255;
165
166         bool const equals = a->equals (*b, options, boost::bind (note, _1, _2));
167
168         if (equals) {
169                 exit (EXIT_SUCCESS);
170         }
171
172         exit (EXIT_FAILURE);
173 }