Add check for Interop font assets being present (in the ASSETMAP and on disk).
[libdcp.git] / src / verify.h
1 /*
2     Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/verify.h
36  *  @brief dcp::verify() method and associated code
37  */
38
39
40 #ifndef LIBDCP_VERIFY_H
41 #define LIBDCP_VERIFY_H
42
43
44 #include <boost/filesystem.hpp>
45 #include <boost/function.hpp>
46 #include <boost/optional.hpp>
47 #include <string>
48 #include <vector>
49
50
51 /* Something in windows.h defines this */
52 #undef ERROR
53
54
55 namespace dcp {
56
57
58 class VerificationNote
59 {
60 public:
61         enum class Type {
62                 ERROR,
63                 BV21_ERROR, ///< may not always be considered an error, but violates a "shall" requirement of Bv2.1
64                 WARNING
65         };
66
67         /** Codes for errors or warnings from verifying DCPs.
68          *
69          *  The names should (in general) answer the question "what is wrong?" with an answer that begins "There is a ..."
70          *  e.g. "There is a INCORRECT_CPL_HASH"
71          *       "There is a MISSING_ASSET"
72          *
73          *  In general the pattern should be <negative-adjective> <noun>.
74          *  Some <negative-adjective>s are:
75          *
76          *  - INCORRECT: something, which could have any value, is wrong.
77          *  - INVALID: something, which should only be one of a set of values, is not in the set, or some preformatted
78          *             quantity (e.g. XML) is in the wrong format.
79          *  - MISMATCHED: two things, which should be the same, are not.
80          *  - EMPTY: something, which should have a value, has no value.
81          *  - MISSING: something, which should be present, is not.
82          *  - UNEXPECTED: something, which is present, should not be.
83          *  - FAILED: some part of the verification failed in some serious way.
84          *
85          *  Comments should clarify meaning and also say which of the optional fields (e.g. file)
86          *  are filled in when this code is used.
87          */
88
89         // If you change the next line, also look in doc/manual/verifier.py in DCP-o-matic
90         // as it looks for it when compiling the manual.  Also, in this enum:
91         //   [...]  will be taken as a reference to a section of Bv2.1
92         //   _foo_  means foo should be written as a piece of code
93         enum class Code {
94                 /** A general error when reading the DCP
95                  *  note contains (probably technical) details
96                  */
97                 FAILED_READ,
98                 /** The hash of the CPL in the PKL does not agree with the CPL file
99                  *  note contains CPL ID
100                  *  file contains CPL filename
101                  */
102                 MISMATCHED_CPL_HASHES,
103                 /** The frame rate given in a reel for the main picture is not 24, 25, 30, 48, 50 or 60
104                  *  note contains the invalid frame rate as "<numerator>/<denominator>"
105                  */
106                 INVALID_PICTURE_FRAME_RATE,
107                 /** The hash of a main picture asset does not agree with the PKL file
108                  *  file contains the picture asset filename
109                  */
110                 INCORRECT_PICTURE_HASH,
111                 /** The hash of a main picture is different in the CPL and PKL
112                  *  file contains the picture asset filename
113                  */
114                 MISMATCHED_PICTURE_HASHES,
115                 /** The hash of a main sound asset does not agree with the PKL file
116                  *  file contains the sound asset filename
117                  */
118                 INCORRECT_SOUND_HASH,
119                 /** The hash of a main sound is different in the CPL and PKL
120                  *  file contains the sound asset filename
121                  */
122                 MISMATCHED_SOUND_HASHES,
123                 /** An assetmap's _<Path>_ entry is empty */
124                 EMPTY_ASSET_PATH,
125                 /** A file mentioned in an asset map cannot be found
126                  *  file contains the filename that is missing
127                  */
128                 MISSING_ASSET,
129                 /** The DCP contains both SMPTE and Interop-standard components */
130                 MISMATCHED_STANDARD,
131                 /** Some XML fails to validate against its XSD/DTD
132                  *  note contains the (probably technical) details
133                  *  file contains the invalid filename
134                  *  line contains the line number
135                  */
136                 INVALID_XML,
137                 /** No _ASSETMAP_ or _ASSETMAP.xml_ was found */
138                 MISSING_ASSETMAP,
139                 /** An asset's _IntrinsicDuration_ is less than 1 second
140                  *  note contains asset ID
141                  */
142                 INVALID_INTRINSIC_DURATION,
143                 /** An asset's _Duration_ is less than 1 second
144                  *  note contains asset ID
145                  */
146                 INVALID_DURATION,
147                 /** The JPEG2000 data in at least one picture frame is larger than the equivalent of 250Mbit/s
148                  *  file contains the picture asset filename
149                  */
150                 INVALID_PICTURE_FRAME_SIZE_IN_BYTES,
151                 /** The JPEG2000 data in at least one picture frame is larger than the equivalent of 230Mbit/s
152                  *  file contains the picture asset filename
153                  */
154                 NEARLY_INVALID_PICTURE_FRAME_SIZE_IN_BYTES,
155                 /** An asset that the CPL requires is not in this DCP; the DCP may be a VF
156                  *  note contains the asset ID
157                  */
158                 EXTERNAL_ASSET,
159                 /** A stereoscopic asset has an MXF which is marked as being monoscopic
160                  *  file contains the asset filename
161                  */
162                 THREED_ASSET_MARKED_AS_TWOD,
163                 /** DCP is Interop, not SMPTE [Bv2.1_6.1] */
164                 INVALID_STANDARD,
165                 /** A language or territory does not conform to RFC 5646 [Bv2.1_6.2.1]
166                  *  note contains the invalid language
167                  */
168                 INVALID_LANGUAGE,
169                 /** A picture asset does not have one of the required Bv2.1 sizes (in pixels) [Bv2.1_7.1]
170                  *  note contains the incorrect size as "<width>x<height>"
171                  *  file contains the asset filename
172                  */
173                 INVALID_PICTURE_SIZE_IN_PIXELS,
174                 /** A picture asset is 2K but is not at 24, 25 or 48 fps as required by Bv2.1 [Bv2.1_7.1]
175                  *  note contains the invalid frame rate as "<numerator>/<denominator>"
176                  *  file contains the asset filename
177                  */
178                 INVALID_PICTURE_FRAME_RATE_FOR_2K,
179                 /** A picture asset is 4K but is not at 24fps as required by Bv2.1 [Bv2.1_7.1]
180                  *  note contains the invalid frame rate as "<numerator>/<denominator>"
181                  *  file contains the asset filename
182                  */
183                 INVALID_PICTURE_FRAME_RATE_FOR_4K,
184                 /** A picture asset is 4K but is 3D which is not allowed by Bv2.1 [Bv2.1_7.1]
185                  *  note contains the invalid frame rate as "<numerator>/<denominator>"
186                  *  file contains the asset filename
187                  */
188                 INVALID_PICTURE_ASSET_RESOLUTION_FOR_3D,
189                 /** A closed caption's XML file is larger than 256KB [Bv2.1_7.2.1]
190                  *  note contains the invalid size in bytes
191                  *  file contains the asset filename
192                  */
193                 INVALID_CLOSED_CAPTION_XML_SIZE_IN_BYTES,
194                 /** Any timed text asset's total files is larger than 115MB [Bv2.1_7.2.1]
195                  *  note contains the invalid size in bytes
196                  *  file contains the asset filename
197                  */
198                 INVALID_TIMED_TEXT_SIZE_IN_BYTES,
199                 /** The total size of all a timed text asset's fonts is larger than 10MB [Bv2.1_7.2.1]
200                  *  note contains the invalid size in bytes
201                  *  file contains the asset filename
202                  */
203                 INVALID_TIMED_TEXT_FONT_SIZE_IN_BYTES,
204                 /** Some SMPTE subtitle XML has no _<Language>_ tag [Bv2.1_7.2.2]
205                  *  file contains the asset filename
206                  */
207                 MISSING_SUBTITLE_LANGUAGE,
208                 /** Not all subtitle assets specify the same _<Language>_ tag [Bv2.1_7.2.2] */
209                 MISMATCHED_SUBTITLE_LANGUAGES,
210                 /** Some SMPTE subtitle XML has no _<StartTime>_ tag [Bv2.1_7.2.3]
211                  *  file contains the asset filename
212                  */
213                 MISSING_SUBTITLE_START_TIME,
214                 /** Some SMPTE subtitle XML has a non-zero _<StartTime>_ tag [Bv2.1_7.2.3]
215                  *  file contains the asset filename
216                  */
217                 INVALID_SUBTITLE_START_TIME,
218                 /** The first subtitle or closed caption happens before 4s into the first reel [Bv2.1_7.2.4] */
219                 INVALID_SUBTITLE_FIRST_TEXT_TIME,
220                 /** At least one subtitle is less than the minimum of 15 frames suggested by [Bv2.1_7.2.5] */
221                 INVALID_SUBTITLE_DURATION,
222                 /** At least one pair of subtitles are separated by less than the the minimum of 2 frames suggested by [Bv2.1_7.2.5] */
223                 INVALID_SUBTITLE_SPACING,
224                 /** A subtitle lasts for longer than the reel which contains it */
225                 SUBTITLE_OVERLAPS_REEL_BOUNDARY,
226                 /** There are more than 3 subtitle lines in at least one place [Bv2.1_7.2.7] */
227                 INVALID_SUBTITLE_LINE_COUNT,
228                 /** There are more than 52 characters in at least one subtitle line [Bv2.1_7.2.7] */
229                 NEARLY_INVALID_SUBTITLE_LINE_LENGTH,
230                 /** There are more than 79 characters in at least one subtitle line [Bv2.1_7.2.7] */
231                 INVALID_SUBTITLE_LINE_LENGTH,
232                 /** There are more than 3 closed caption lines in at least one place [Bv2.1_7.2.6] */
233                 INVALID_CLOSED_CAPTION_LINE_COUNT,
234                 /** There are more than 32 characters in at least one closed caption line [Bv2.1_7.2.6] */
235                 INVALID_CLOSED_CAPTION_LINE_LENGTH,
236                 /** The audio sampling rate must be 48kHz [Bv2.1_7.3]
237                  *  note contains the invalid frame rate
238                  *  file contains the asset filename
239                  */
240                 INVALID_SOUND_FRAME_RATE,
241                 /** The CPL has no _<AnnotationText>_ tag [Bv2.1_8.1]
242                  *  note contains the CPL ID
243                  *  file contains the CPL filename
244                  */
245                 MISSING_CPL_ANNOTATION_TEXT,
246                 /** The _<AnnotationText>_ is not the same as the _<ContentTitleText>_ [Bv2.1_8.1]
247                  *  note contains the CPL ID
248                  *  file contains the CPL filename
249                  */
250                 MISMATCHED_CPL_ANNOTATION_TEXT,
251                 /** At least one asset in a reel does not have the same duration as the others */
252                 MISMATCHED_ASSET_DURATION,
253                 /** If one reel has a _MainSubtitle_, all must have them */
254                 MISSING_MAIN_SUBTITLE_FROM_SOME_REELS,
255                 /** If one reel has at least one _ClosedCaption_, all reels must have the same number of _ClosedCaptions_ */
256                 MISMATCHED_CLOSED_CAPTION_ASSET_COUNTS,
257                 /** MainSubtitle in reels must have _<EntryPoint>_ [Bv2.1_8.3.2]
258                  *  note contains the asset ID
259                  */
260                 MISSING_SUBTITLE_ENTRY_POINT,
261                 /** MainSubtitle _<EntryPoint>_ must be zero [Bv2.1_8.3.2]
262                  *  note contains the asset ID
263                  */
264                 INCORRECT_SUBTITLE_ENTRY_POINT,
265                 /** Closed caption in reels must have _<EntryPoint>_ [Bv2.1_8.3.2]
266                  *  note contains the asset ID
267                  */
268                 MISSING_CLOSED_CAPTION_ENTRY_POINT,
269                 /** Closed caption _MainSubtitle_ _<EntryPoint>_ must be zero [Bv2.1_8.3.2]
270                  *  note contains the asset ID
271                  */
272                 INCORRECT_CLOSED_CAPTION_ENTRY_POINT,
273                 /** _<Hash>_ must be present for assets in CPLs
274                  * note contains the asset ID
275                  */
276                 MISSING_HASH,
277                 /** If _ContentKind_ is Feature there must be a FFEC marker */
278                 MISSING_FFEC_IN_FEATURE,
279                 /** If _ContentKind_ is Feature there must be a FFMC marker */
280                 MISSING_FFMC_IN_FEATURE,
281                 /** There should be a FFOC marker */
282                 MISSING_FFOC,
283                 /** There should be a LFOC marker */
284                 MISSING_LFOC,
285                 /** The FFOC marker should be 1
286                  *  note contains the incorrect value.
287                  */
288                 INCORRECT_FFOC,
289                 /** The LFOC marker should be the last frame in the reel
290                  *  note contains the incorrect value
291                  */
292                 INCORRECT_LFOC,
293                 /** There must be a _<CompositionMetadataAsset>_
294                  *  note contains the CPL ID
295                  *  file contains the CPL filename
296                  */
297                 MISSING_CPL_METADATA,
298                 /** CPL metadata should contain _<VersionNumber>_ of 1, at least
299                  *  note contains the CPL ID
300                  *  file contains the CPL filename
301                  */
302                 MISSING_CPL_METADATA_VERSION_NUMBER,
303                 /** There must be an _<ExtensionMetadata>_ in _<CompositionMetadataAsset>_ [Bv2.1_8.6.3]
304                  *  note contains the CPL ID
305                  *  file contains the CPL filename
306                  */
307                 MISSING_EXTENSION_METADATA,
308                 /** _<ExtensionMetadata>_ does not have the correct form [Bv2.1_8.6.3]
309                  *  note contains details of what's wrong
310                  *  file contains the CPL filename
311                  */
312                 INVALID_EXTENSION_METADATA,
313                 /** A CPL containing encrypted content is not signed [Bv2.1_8.7]
314                  *  note contains the CPL ID
315                  *  file contains the CPL filename
316                  */
317                 UNSIGNED_CPL_WITH_ENCRYPTED_CONTENT,
318                 /** A PKL containing encrypted content is not signed [Bv2.1_8.7]
319                  *  note contains the PKL ID
320                  *  file contains the PKL filename
321                  */
322                 UNSIGNED_PKL_WITH_ENCRYPTED_CONTENT,
323                 /** If a PKL has one CPL its _<ContentTitleText>_ must be the same as the PKL's _<AnnotationText>_
324                  *  note contains the PKL ID
325                  *  file contains the PKL filename
326                  */
327                 MISMATCHED_PKL_ANNOTATION_TEXT_WITH_CPL,
328                 /** Some, but not all content, is encrypted */
329                 PARTIALLY_ENCRYPTED,
330                 /** General error during JPEG2000 codestream verification
331                  *  note contains details
332                  */
333                 INVALID_JPEG2000_CODESTREAM,
334                 /** Invalid number of guard bits in a 2K JPEG2000 stream (should be 1) [Bv2.1_10.2.1]
335                  *  note contains the number of guard bits
336                  */
337                 INVALID_JPEG2000_GUARD_BITS_FOR_2K,
338                 /** Invalid number of guard bits in a 4K JPEG2000 stream (should be 2) [Bv2.1_10.2.1]
339                  *  note contains the number of guard bits
340                  */
341                 INVALID_JPEG2000_GUARD_BITS_FOR_4K,
342                 /** JPEG2000 tile size is not the same as the image size [Bv2.1_10.2.1] */
343                 INVALID_JPEG2000_TILE_SIZE,
344                 /** JPEG2000 code block width is not 32 [Bv2.1_10.2.1]
345                  *  note contains the code block width
346                  */
347                 INVALID_JPEG2000_CODE_BLOCK_WIDTH,
348                 /** JPEG2000 code block height is not 32 [Bv2.1_10.2.1]
349                  *  note contains the code block height
350                  */
351                 INVALID_JPEG2000_CODE_BLOCK_HEIGHT,
352                 /** There must be no POC markers in a 2K codestream [Bv2.1_10.2.1]
353                  *  note contains the number of POC markers found
354                  */
355                 INCORRECT_JPEG2000_POC_MARKER_COUNT_FOR_2K,
356                 /** There must be exactly one POC marker in a 4K codestream [Bv2.1_10.2.1]
357                  *  note contains the number of POC markers found
358                  */
359                 INCORRECT_JPEG2000_POC_MARKER_COUNT_FOR_4K,
360                 /** A POC marker has incorrect content [Bv2.1_10.2.1]
361                  *  note contains details
362                  */
363                 INCORRECT_JPEG2000_POC_MARKER,
364                 /** A POC marker was found outside the main head [Bv2.1_10.2.1] */
365                 INVALID_JPEG2000_POC_MARKER_LOCATION,
366                 /** Invalid number of tile parts for 2K JPEG2000 stream (should be 3) [Bv2.1_10.2.1]
367                  *  note contains the number of tile parts
368                  */
369                 INVALID_JPEG2000_TILE_PARTS_FOR_2K,
370                 /** Invalid number of tile parts for 4K JPEG2000 stream (should be 6) [Bv2.1_10.2.1]
371                  *  note contains the number of tile parts
372                  */
373                 INVALID_JPEG2000_TILE_PARTS_FOR_4K,
374                 /** No TLM marker was found [Bv2.1_10.2.1] */
375                 MISSING_JPEG200_TLM_MARKER,
376                 /** The MXF _ResourceID_ of a timed text resource was not the same as that of the contained XML essence [Bv2.1_10.4.3] */
377                 MISMATCHED_TIMED_TEXT_RESOURCE_ID,
378                 /** The AssetID of a timed text MXF is the same as its _ResourceID_ or that of the contained XML essence [Bv2.1_10.4.2] */
379                 INCORRECT_TIMED_TEXT_ASSET_ID,
380                 /** The ContainerDuration of a timed text MXF is not the same as the _Duration_ in its reel [Bv2.1_10.4.3]
381                  *  note contains the reel duration, followed by a space, followed by the MXF duration
382                  *  file contains the asset filename
383                  */
384                 MISMATCHED_TIMED_TEXT_DURATION,
385                 /** Something could not be verified because content is encrypted and no key is available */
386                 MISSED_CHECK_OF_ENCRYPTED,
387                 /** Some timed-text XML has an empty <_Text_> node */
388                 EMPTY_TEXT,
389                 /** Some closed captions do not have the same vertical alignment within a <_Subtitle_> node */
390                 MISMATCHED_CLOSED_CAPTION_VALIGN,
391                 /** Some closed captions are not listed in the XML in the order of their vertical position */
392                 INCORRECT_CLOSED_CAPTION_ORDERING,
393                 /** Some <MainMarkers> asset has an <EntryPoint> that should not be there */
394                 UNEXPECTED_ENTRY_POINT,
395                 /** Some <MainMarkers> asset has an <Duration> that should not be there */
396                 UNEXPECTED_DURATION,
397                 /** A <ContentKind> has been specified with either no scope or the SMPTE 429-7 scope, but which is not one of those allowed */
398                 INVALID_CONTENT_KIND,
399                 /** Either the width or height of a <MainPictureActiveArea> in a CPL is either not an even number, or bigger than the corresponding asset dimension.
400                  *  note contains details of what is wrong
401                  *  file contains the CPL filename
402                  */
403                 INVALID_MAIN_PICTURE_ACTIVE_AREA,
404                 /** A PKL has more than one asset with the same ID
405                  *  note contains the PKL ID
406                  *  file contains the PKL filename
407                  */
408                 DUPLICATE_ASSET_ID_IN_PKL,
409                 /** An ASSETMAP has more than one asset with the same ID
410                  *  note contains the ASSETMAP ID
411                  *  file contains the ASSETMAP filename
412                  */
413                 DUPLICATE_ASSET_ID_IN_ASSETMAP,
414                 /** An Interop subtitle asset has no subtitles.
415                  *  note contains the asset ID
416                  *  file contains the asset filename
417                  */
418                 MISSING_SUBTITLE,
419                 /** A SMPTE subtitle asset as an <IssueDate> which is not of the form yyyy-mm-ddThh:mm:ss
420                  *  I can find no reference in a standard to this being required, but the Deluxe delivery
421                  *  specifications require it and their QC will fail DCPs that don't have it.
422                  *  note contains the incorrect <IssueDate>
423                  */
424                 INVALID_SUBTITLE_ISSUE_DATE,
425                 /** The sound assets in the CPL do not have the same audio channel count.
426                  *  file contains the filename of the first asset to differ
427                  */
428                 MISMATCHED_SOUND_CHANNEL_COUNTS,
429                 /** The CPL contains a MainSoundConfiguration tag which does not describe the number of
430                  *  channels in the audio assets.
431                  *  note contains details of what is wrong
432                  *  file contains the CPL filename
433                  */
434                 INVALID_MAIN_SOUND_CONFIGURATION,
435                 /** An interop subtitle file has a <LoadFont> node which refers to a font file that is not found.
436                  *  note contains the <LoadFont> ID
437                  */
438                 MISSING_FONT
439         };
440
441         VerificationNote (Type type, Code code)
442                 : _type (type)
443                 , _code (code)
444         {}
445
446         VerificationNote (Type type, Code code, std::string note)
447                 : _type (type)
448                 , _code (code)
449                 , _note (note)
450         {}
451
452         VerificationNote (Type type, Code code, boost::filesystem::path file)
453                 : _type (type)
454                 , _code (code)
455                 , _file (file)
456         {}
457
458         VerificationNote (Type type, Code code, std::string note, boost::filesystem::path file)
459                 : _type (type)
460                 , _code (code)
461                 , _note (note)
462                 , _file (file)
463         {}
464
465         VerificationNote (Type type, Code code, std::string note, boost::filesystem::path file, uint64_t line)
466                 : _type (type)
467                 , _code (code)
468                 , _note (note)
469                 , _file (file)
470                 , _line (line)
471         {}
472
473         Type type () const {
474                 return _type;
475         }
476
477         Code code () const {
478                 return _code;
479         }
480
481         boost::optional<std::string> note () const {
482                 return _note;
483         }
484
485         boost::optional<boost::filesystem::path> file () const {
486                 return _file;
487         }
488
489         boost::optional<uint64_t> line () const {
490                 return _line;
491         }
492
493 private:
494         Type _type;
495         Code _code;
496         /** Further information about the error, if applicable */
497         boost::optional<std::string> _note;
498         /** Path of file containing the error, if applicable */
499         boost::optional<boost::filesystem::path> _file;
500         /** Error line number within _file, if applicable */
501         boost::optional<uint64_t> _line;
502 };
503
504
505 struct VerificationOptions
506 {
507         ///< If set, any assets larger than this number of bytes will not have their hashes checked
508         boost::optional<boost::uintmax_t> maximum_asset_size_for_hash_check;
509         ///< true to check asset hashes (except those which match maximum_asset_size_for_hash_check)
510         ///< false to check no asset hashes.
511         bool check_asset_hashes = true;
512 };
513
514
515 std::vector<VerificationNote> verify (
516         std::vector<boost::filesystem::path> directories,
517         boost::function<void (std::string, boost::optional<boost::filesystem::path>)> stage,
518         boost::function<void (float)> progress,
519         VerificationOptions options = {},
520         boost::optional<boost::filesystem::path> xsd_dtd_directory = boost::optional<boost::filesystem::path>()
521         );
522
523 std::string note_to_string (dcp::VerificationNote note);
524
525 bool operator== (dcp::VerificationNote const& a, dcp::VerificationNote const& b);
526 bool operator< (dcp::VerificationNote const& a, dcp::VerificationNote const& b);
527
528 std::ostream& operator<<(std::ostream& s, dcp::VerificationNote const& note);
529
530
531 }
532
533
534 #endif