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