Merge and add option to ignore differing MXF names.
[libdcp.git] / tools / dcpdiff.cc
1 #include <iostream>
2 #include <boost/filesystem.hpp>
3 #include <getopt.h>
4 #include "dcp.h"
5 #include "exceptions.h"
6
7 using namespace std;
8 using namespace boost;
9 using namespace libdcp;
10
11 static bool verbose = false;
12
13 static void
14 help (string n)
15 {
16         cerr << "Syntax: " << n << " [OPTION] <DCP> <DCP>\n"
17              << "  -V, --version      show libdcp version\n"
18              << "  -h, --help         show this help\n"
19              << "  -v, --verbose      be verbose\n"
20              << "  -n, --names        allow differing MXF names\n"
21              << "\n"
22              << "The <DCP>s are the DCP directories to compare.\n"
23              << "Comparison is of metadata and content, ignoring timestamps\n"
24              << "and differing UUIDs.\n";
25 }
26
27 void
28 note (NoteType t, string n)
29 {
30         if (t == ERROR || (t == PROGRESS && verbose)) {
31                 cout << " " << n << "\n";
32         }
33 }
34
35 int
36 main (int argc, char* argv[])
37 {
38         EqualityOptions options;
39         
40         int option_index = 0;
41         while (1) {
42                 static struct option long_options[] = {
43                         { "version", no_argument, 0, 'V'},
44                         { "help", no_argument, 0, 'h'},
45                         { "verbose", no_argument, 0, 'v'},
46                         { "names", no_argument, 0, 'n'},
47                         { 0, 0, 0, 0 }
48                 };
49
50                 int c = getopt_long (argc, argv, "Vhvn", long_options, &option_index);
51
52                 if (c == -1) {
53                         break;
54                 }
55
56                 switch (c) {
57                 case 'V':
58                         cout << "dcpdiff version " << LIBDCP_VERSION << "\n";
59                         exit (EXIT_SUCCESS);
60                 case 'h':
61                         help (argv[0]);
62                         exit (EXIT_SUCCESS);
63                 case 'v':
64                         verbose = true;
65                         break;
66                 case 'n':
67                         options.mxf_names_can_differ = true;
68                         break;
69                 }
70         }
71
72         if (argc <= optind || argc > (optind + 2)) {
73                 help (argv[0]);
74                 exit (EXIT_FAILURE);
75         }
76
77         if (!filesystem::exists (argv[optind])) {
78                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
79                 exit (EXIT_FAILURE);
80         }
81
82         if (!filesystem::exists (argv[optind + 1])) {
83                 cerr << argv[0] << ": DCP " << argv[optind + 1] << " not found.\n";
84                 exit (EXIT_FAILURE);
85         }
86
87         DCP* a = 0;
88         try {
89                 a = new DCP (argv[optind]);
90                 a->read ();
91         } catch (FileError& e) {
92                 cerr << "Could not read DCP " << argv[optind] << "; " << e.what() << " " << e.filename() << "\n";
93                 exit (EXIT_FAILURE);
94         }
95
96         DCP* b = 0;
97         try {
98                 b = new DCP (argv[optind + 1]);
99                 b->read ();
100         } catch (FileError& e) {
101                 cerr << "Could not read DCP " << argv[optind + 1] << "; " << e.what() << " " << e.filename() << "\n";
102                 exit (EXIT_FAILURE);
103         }
104
105         options.max_mean_pixel_error = 5;
106         options.max_std_dev_pixel_error = 5;
107         /* I think this is just below the LSB at 16-bits (ie the 8th most significant bit at 24-bit) */
108         options.max_audio_sample_error = 255;
109
110         bool const equals = a->equals (*b, options, boost::bind (note, _1, _2));
111
112         if (equals) {
113                 exit (EXIT_SUCCESS);
114         }
115
116         exit (EXIT_FAILURE);
117 }