summaryrefslogtreecommitdiff
path: root/tools/dcpverify.cc
blob: 8966c3ca84e502f3f7752e9f65d4edabbe3a64fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
    Copyright (C) 2020 Carl Hetherington <cth@carlh.net>

    This file is part of libdcp.

    libdcp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libdcp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations
    including the two.

    You must obey the GNU General Public License in all respects
    for all of the code used other than OpenSSL.  If you modify
    file(s) with this exception, you may extend this exception to your
    version of the file(s), but you are not obligated to do so.  If you
    do not wish to do so, delete this exception statement from your
    version.  If you delete this exception statement from all source
    files in the program, then also delete it here.
*/


#include "common.h"
#include "compose.hpp"
#include "filesystem.h"
#include "html_formatter.h"
#ifdef LIBDCP_HAVE_HARU
#include "pdf_formatter.h"
#endif
#include "raw_convert.h"
#include "text_formatter.h"
#include "verify.h"
#include "verify_report.h"
#include "version.h"
#include <boost/bind/bind.hpp>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#include <getopt.h>
#include <iostream>
#include <cstdlib>


using std::cerr;
using std::cout;
using std::list;
using std::string;
using std::vector;
using boost::bind;
using boost::optional;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif


static void
help (string n)
{
	cerr << "Syntax: " << n << " [OPTION] <DCP>\n"
	     << "  -V, --version                                show libdcp version\n"
	     << "  -h, --help                                   show this help\n"
	     << "  --ignore-missing-assets                      don't give errors about missing assets\n"
	     << "  --ignore-bv21-smpte                          don't give the SMPTE Bv2.1 error about a DCP not being SMPTE\n"
	     << "  --no-asset-hash-check                        don't check asset hashes\n"
	     << "  --asset-hash-check-maximum-size <size-in-MB> only check hashes for assets smaller than this size (in MB)\n"
	     << "  --no-picture-details-check                   don't check details of picture assets (J2K bitstream etc.)\n"
	     << "  -o <filename>                                write report to filename "
#ifdef LIBDCP_HAVE_HARU
	" (.txt, .htm, .html or .pdf)\n"
#else
	" (.txt, .htm or .html)\n"
#endif
	     << "  -q, --quiet                                  don't report progress\n";
}


int
main (int argc, char* argv[])
{
	dcp::init ();

	bool ignore_missing_assets = false;
	bool ignore_bv21_smpte = false;
	bool quiet = false;
	boost::optional<boost::filesystem::path> report_filename;

	dcp::VerificationOptions verification_options;

	int option_index = 0;
	while (true) {
		static struct option long_options[] = {
			{ "version", no_argument, 0, 'V' },
			{ "help", no_argument, 0, 'h' },
			{ "ignore-missing-assets", no_argument, 0, 'A' },
			{ "ignore-bv21-smpte", no_argument, 0, 'B' },
			{ "no-asset-hash-check", no_argument, 0, 'C' },
			{ "no-picture-details-check", no_argument, 0, 'E' },
			{ "asset-hash-check-maximum-size", required_argument, 0, 'D' },
			{ "quiet", no_argument, 0, 'q' },
			{ 0, 0, 0, 0 }
		};

		int c = getopt_long (argc, argv, "VhABCD:Eqo:", long_options, &option_index);

		if (c == -1) {
			break;
		} else if (c == '?' || c == ':') {
			exit(EXIT_FAILURE);
		}

		switch (c) {
		case 'V':
			cout << "dcpverify version " << dcp::version << "\n";
			exit (EXIT_SUCCESS);
		case 'h':
			help(boost::filesystem::path(argv[0]).filename().string());
			exit (EXIT_SUCCESS);
		case 'A':
			ignore_missing_assets = true;
			break;
		case 'B':
			ignore_bv21_smpte = true;
			break;
		case 'C':
			verification_options.check_asset_hashes = false;
			break;
		case 'D':
			verification_options.maximum_asset_size_for_hash_check = dcp::raw_convert<int>(optarg) * 1000000LL;
			break;
		case 'E':
			verification_options.check_picture_details = false;
			break;
		case 'q':
			quiet = true;
			break;
		case 'o':
			report_filename = optarg;
			break;
		}
	}

	if (argc <= optind) {
		help (argv[0]);
		exit (EXIT_FAILURE);
	}

	if (!dcp::filesystem::exists(argv[optind])) {
		cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
		exit (EXIT_FAILURE);
	}

	auto stage = [quiet](string s, optional<boost::filesystem::path> path) {
		if (quiet) {
			return;
		}

		if (path) {
			cout << s << ": " << path->string() << "\n";
		} else {
			cout << s << "\n";
		}
	};

	auto progress = [quiet](float amount) {
		if (quiet) {
			return;
		}
		int const width = 60;
		int const index = std::rint(amount * width);
		cout << "[";
		for (int i = 0; i < width; ++i) {
			if (i < index) {
				std::cout << "=";
			} else if (i == index) {
				std::cout << ">";
			} else {
				std::cout << " ";
			}
		}
		cout << "] " << std::rint(amount * 100) << "%\r";
		cout.flush();
	};

	vector<boost::filesystem::path> directories;
	directories.push_back (argv[optind]);
	auto result = dcp::verify(directories, {}, stage, progress, verification_options);
	result.notes = dcp::filter_notes(result.notes, ignore_missing_assets, ignore_bv21_smpte);

	if (!quiet) {
		cout << "\n";
	}

	bool failed = false;
	bool bv21_failed = false;
	bool warned = false;
	for (auto i: result.notes) {
		switch (i.type()) {
		case dcp::VerificationNote::Type::OK:
			break;
		case dcp::VerificationNote::Type::ERROR:
			cout << "Error: " << note_to_string(i) << "\n";
			failed = true;
			break;
		case dcp::VerificationNote::Type::BV21_ERROR:
			cout << "Bv2.1 error: " << note_to_string(i) << "\n";
			bv21_failed = true;
			break;
		case dcp::VerificationNote::Type::WARNING:
			cout << "Warning: " << note_to_string(i) << "\n";
			warned = true;
			break;
		}
	}

	if (!failed && !quiet) {
		if (bv21_failed && warned) {
			cout << "\nDCP verified OK (but with Bv2.1 errors and warnings).\n";
		} else if (bv21_failed) {
			cout << "\nDCP verified OK (but with Bv2.1 errors).\n";
		} else if (warned) {
			cout << "\nDCP verified OK (but with warnings).\n";
		} else {
			cout << "DCP verified OK.\n";
		}
	}

	if (report_filename) {
		if (report_filename->extension() == ".htm" || report_filename->extension() == ".html") {
			dcp::HTMLFormatter formatter(*report_filename);
			dcp::verify_report({ result }, formatter);
#ifdef LIBDCP_HAVE_HARU
		} else if (report_filename->extension() == ".pdf") {
			dcp::PDFFormatter formatter(*report_filename);
			dcp::verify_report({ result }, formatter);
#endif
		} else {
			dcp::TextFormatter formatter(*report_filename);
			dcp::verify_report({ result }, formatter);
		}
	}

	exit (failed ? EXIT_FAILURE : EXIT_SUCCESS);
}