Sort notes when checking verifications.
[libdcp.git] / src / verify.cc
index e57439957aa8213cf63a0db1c2cc8738d90e6aef..623794cc9c276684bc00c5f9a97562726d4d0841 100644 (file)
 #include "raw_convert.h"
 #include "reel.h"
 #include "reel_closed_caption_asset.h"
+#include "reel_interop_subtitle_asset.h"
 #include "reel_markers_asset.h"
 #include "reel_picture_asset.h"
 #include "reel_sound_asset.h"
+#include "reel_smpte_subtitle_asset.h"
 #include "reel_subtitle_asset.h"
 #include "smpte_subtitle_asset.h"
 #include "stereo_picture_asset.h"
@@ -154,22 +156,22 @@ private:
 class DCPErrorHandler : public ErrorHandler
 {
 public:
-       void warning(const SAXParseException& e)
+       void warning(const SAXParseException& e) override
        {
                maybe_add (XMLValidationError(e));
        }
 
-       void error(const SAXParseException& e)
+       void error(const SAXParseException& e) override
        {
                maybe_add (XMLValidationError(e));
        }
 
-       void fatalError(const SAXParseException& e)
+       void fatalError(const SAXParseException& e) override
        {
                maybe_add (XMLValidationError(e));
        }
 
-       void resetErrors() {
+       void resetErrors() override {
                _errors.clear ();
        }
 
@@ -244,7 +246,7 @@ public:
                add("http://www.smpte-ra.org/schemas/429-10/2008/Main-Stereo-Picture-CPL", "SMPTE-429-10-2008.xsd");
        }
 
-       InputSource* resolveEntity(XMLCh const *, XMLCh const * system_id)
+       InputSource* resolveEntity(XMLCh const *, XMLCh const * system_id) override
        {
                if (!system_id) {
                        return 0;
@@ -637,6 +639,7 @@ struct State
 void
 verify_smpte_timed_text_asset (
        shared_ptr<const SMPTESubtitleAsset> asset,
+       optional<int64_t> reel_asset_duration,
        vector<VerificationNote>& notes
        )
 {
@@ -670,6 +673,16 @@ verify_smpte_timed_text_asset (
        } else if (asset->start_time() != Time()) {
                notes.push_back ({ VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_SUBTITLE_START_TIME, asset->file().get() });
        }
+
+       if (reel_asset_duration && *reel_asset_duration != asset->intrinsic_duration()) {
+               notes.push_back (
+                       {
+                               VerificationNote::Type::BV21_ERROR,
+                               VerificationNote::Code::MISMATCHED_TIMED_TEXT_DURATION,
+                               String::compose("%1 %2", *reel_asset_duration, asset->intrinsic_duration()),
+                               asset->file().get()
+                       });
+       }
 }
 
 
@@ -704,6 +717,7 @@ verify_smpte_subtitle_asset (
 static void
 verify_subtitle_asset (
        shared_ptr<const SubtitleAsset> asset,
+       optional<int64_t> reel_asset_duration,
        function<void (string, optional<boost::filesystem::path>)> stage,
        boost::filesystem::path xsd_dtd_directory,
        vector<VerificationNote>& notes,
@@ -718,7 +732,7 @@ verify_subtitle_asset (
 
        auto smpte = dynamic_pointer_cast<const SMPTESubtitleAsset>(asset);
        if (smpte) {
-               verify_smpte_timed_text_asset (smpte, notes);
+               verify_smpte_timed_text_asset (smpte, reel_asset_duration, notes);
                verify_smpte_subtitle_asset (smpte, notes, state);
        }
 }
@@ -728,6 +742,7 @@ verify_subtitle_asset (
 static void
 verify_closed_caption_asset (
        shared_ptr<const SubtitleAsset> asset,
+       optional<int64_t> reel_asset_duration,
        function<void (string, optional<boost::filesystem::path>)> stage,
        boost::filesystem::path xsd_dtd_directory,
        vector<VerificationNote>& notes
@@ -741,7 +756,7 @@ verify_closed_caption_asset (
 
        auto smpte = dynamic_pointer_cast<const SMPTESubtitleAsset>(asset);
        if (smpte) {
-               verify_smpte_timed_text_asset (smpte, notes);
+               verify_smpte_timed_text_asset (smpte, reel_asset_duration, notes);
        }
 
        if (asset->raw_xml().size() > 256 * 1024) {
@@ -978,7 +993,13 @@ verify_text_timing (vector<shared_ptr<Reel>> reels, vector<VerificationNote>& no
                                return static_cast<bool>(reel->main_subtitle());
                        },
                        [](shared_ptr<Reel> reel) {
-                               return reel->main_subtitle()->asset()->raw_xml();
+                               auto interop = dynamic_pointer_cast<ReelInteropSubtitleAsset>(reel->main_subtitle());
+                               if (interop) {
+                                       return interop->asset()->raw_xml();
+                               }
+                               auto smpte = dynamic_pointer_cast<ReelSMPTESubtitleAsset>(reel->main_subtitle());
+                               DCP_ASSERT (smpte);
+                               return smpte->asset()->raw_xml();
                        },
                        [](shared_ptr<Reel> reel) {
                                return reel->main_subtitle()->actual_duration();
@@ -1067,14 +1088,9 @@ pkl_has_encrypted_assets (shared_ptr<DCP> dcp, shared_ptr<PKL> pkl)
        for (auto i: dcp->cpls()) {
                for (auto j: i->reel_file_assets()) {
                        if (j->asset_ref().resolved()) {
-                               /* It's a bit surprising / broken but Interop subtitle assets are represented
-                                * in reels by ReelSubtitleAsset which inherits ReelFileAsset, so it's possible for
-                                * ReelFileAssets to have assets which are not MXFs.
-                                */
-                               if (auto asset = dynamic_pointer_cast<MXF>(j->asset_ref().asset())) {
-                                       if (asset->encrypted()) {
-                                               encrypted.push_back(j->asset_ref().id());
-                                       }
+                               auto mxf = dynamic_pointer_cast<MXF>(j->asset_ref().asset());
+                               if (mxf && mxf->encrypted()) {
+                                       encrypted.push_back(j->asset_ref().id());
                                }
                        }
                }
@@ -1115,7 +1131,7 @@ dcp::verify (
                stage ("Checking DCP", dcp->directory());
                bool carry_on = true;
                try {
-                       dcp->read (&notes);
+                       dcp->read (&notes, true);
                } catch (MissingAssetmapError& e) {
                        notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
                        carry_on = false;
@@ -1221,7 +1237,7 @@ dcp::verify (
                                                notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::INVALID_INTRINSIC_DURATION, i->id()});
                                        }
                                        auto file_asset = dynamic_pointer_cast<ReelFileAsset>(i);
-                                       if (file_asset && !file_asset->hash()) {
+                                       if (i->encryptable() && !file_asset->hash()) {
                                                notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_HASH, i->id()});
                                        }
                                }
@@ -1268,7 +1284,7 @@ dcp::verify (
                                if (reel->main_subtitle()) {
                                        verify_main_subtitle_reel (reel->main_subtitle(), notes);
                                        if (reel->main_subtitle()->asset_ref().resolved()) {
-                                               verify_subtitle_asset (reel->main_subtitle()->asset(), stage, *xsd_dtd_directory, notes, state);
+                                               verify_subtitle_asset (reel->main_subtitle()->asset(), reel->main_subtitle()->duration(), stage, *xsd_dtd_directory, notes, state);
                                        }
                                        have_main_subtitle = true;
                                } else {
@@ -1278,7 +1294,7 @@ dcp::verify (
                                for (auto i: reel->closed_captions()) {
                                        verify_closed_caption_reel (i, notes);
                                        if (i->asset_ref().resolved()) {
-                                               verify_closed_caption_asset (i->asset(), stage, *xsd_dtd_directory, notes);
+                                               verify_closed_caption_asset (i->asset(), i->duration(), stage, *xsd_dtd_directory, notes);
                                        }
                                }
 
@@ -1456,6 +1472,8 @@ dcp::note_to_string (VerificationNote note)
                return String::compose("The instantaneous bit rate of the picture asset %1 is close to the limit of 250Mbit/s in at least one place.", note.file()->filename());
        case VerificationNote::Code::EXTERNAL_ASSET:
                return String::compose("The asset %1 that this DCP refers to is not included in the DCP.  It may be a VF.", note.note().get());
+       case VerificationNote::Code::THREED_ASSET_MARKED_AS_TWOD:
+               return String::compose("The asset %1 is 3D but its MXF is marked as 2D.", note.file()->filename());
        case VerificationNote::Code::INVALID_STANDARD:
                return "This DCP does not use the SMPTE standard.";
        case VerificationNote::Code::INVALID_LANGUAGE:
@@ -1580,6 +1598,13 @@ dcp::note_to_string (VerificationNote note)
                return "The Resource ID in a timed text MXF did not match the ID of the contained XML.";
        case VerificationNote::Code::INCORRECT_TIMED_TEXT_ASSET_ID:
                return "The Asset ID in a timed text MXF is the same as the Resource ID or that of the contained XML.";
+       case VerificationNote::Code::MISMATCHED_TIMED_TEXT_DURATION:
+       {
+               vector<string> parts;
+               boost::split (parts, note.note().get(), boost::is_any_of(" "));
+               DCP_ASSERT (parts.size() == 2);
+               return String::compose("The reel duration of some timed text (%1) is not the same as the ContainerDuration of its MXF (%2).", parts[0], parts[1]);
+       }
        }
 
        return "";
@@ -1593,6 +1618,29 @@ dcp::operator== (dcp::VerificationNote const& a, dcp::VerificationNote const& b)
 }
 
 
+bool
+dcp::operator< (dcp::VerificationNote const& a, dcp::VerificationNote const& b)
+{
+       if (a.type() != b.type()) {
+               return a.type() < b.type();
+       }
+
+       if (a.code() != b.code()) {
+               return a.code() < b.code();
+       }
+
+       if (a.note() != b.note()) {
+               return a.note().get_value_or("") < b.note().get_value_or("");
+       }
+
+       if (a.file() != b.file()) {
+               return a.file().get_value_or("") < b.file().get_value_or("");
+       }
+
+       return a.line().get_value_or(0) < b.line().get_value_or(0);
+}
+
+
 std::ostream&
 dcp::operator<< (std::ostream& s, dcp::VerificationNote const& note)
 {