Cleanup: extract verify_assetmap().
[libdcp.git] / src / verify.cc
index e698920c11da3f8f1f49c3e43df16190c1921ba2..b554612b1480641f2bb1cce07df9f1889b4f4e93 100644 (file)
 #include <boost/algorithm/string.hpp>
 #include <iostream>
 #include <map>
+#include <set>
 #include <vector>
 
 
-using std::list;
-using std::vector;
-using std::string;
 using std::cout;
+using std::dynamic_pointer_cast;
+using std::list;
+using std::make_shared;
 using std::map;
 using std::max;
+using std::set;
 using std::shared_ptr;
-using std::make_shared;
+using std::string;
+using std::vector;
 using boost::optional;
 using boost::function;
-using std::dynamic_pointer_cast;
 
 
 using namespace dcp;
@@ -454,7 +456,7 @@ verify_picture_asset (shared_ptr<const ReelFileAsset> reel_file_asset, boost::fi
                for (int64_t i = 0; i < duration; ++i) {
                        auto frame = reader->get_frame (i);
                        biggest_frame = max(biggest_frame, max(frame->left()->size(), frame->right()->size()));
-                       if (!stereo_asset->encrypted() || mono_asset->key()) {
+                       if (!stereo_asset->encrypted() || stereo_asset->key()) {
                                vector<VerificationNote> j2k_notes;
                                verify_j2k (frame->left(), j2k_notes);
                                verify_j2k (frame->right(), j2k_notes);
@@ -1174,7 +1176,7 @@ verify_text_details (vector<shared_ptr<Reel>> reels, vector<VerificationNote>& n
 
 
 void
-verify_extension_metadata (shared_ptr<CPL> cpl, vector<VerificationNote>& notes)
+verify_extension_metadata(shared_ptr<const CPL> cpl, vector<VerificationNote>& notes)
 {
        DCP_ASSERT (cpl->file());
        cxml::Document doc ("CompositionPlaylist");
@@ -1232,7 +1234,7 @@ verify_extension_metadata (shared_ptr<CPL> cpl, vector<VerificationNote>& notes)
 
 
 bool
-pkl_has_encrypted_assets (shared_ptr<DCP> dcp, shared_ptr<PKL> pkl)
+pkl_has_encrypted_assets(shared_ptr<const DCP> dcp, shared_ptr<const PKL> pkl)
 {
        vector<string> encrypted;
        for (auto i: dcp->cpls()) {
@@ -1256,318 +1258,465 @@ pkl_has_encrypted_assets (shared_ptr<DCP> dcp, shared_ptr<PKL> pkl)
 }
 
 
-vector<VerificationNote>
-dcp::verify (
-       vector<boost::filesystem::path> directories,
+static
+void
+verify_reel(
+       shared_ptr<const DCP> dcp,
+       shared_ptr<const CPL> cpl,
+       shared_ptr<const Reel> reel,
+       optional<dcp::Size> main_picture_active_area,
        function<void (string, optional<boost::filesystem::path>)> stage,
+       boost::filesystem::path xsd_dtd_directory,
        function<void (float)> progress,
-       optional<boost::filesystem::path> xsd_dtd_directory
+       vector<VerificationNote>& notes,
+       State& state,
+       bool* have_main_subtitle,
+       bool* have_no_main_subtitle,
+       size_t* most_closed_captions,
+       size_t* fewest_closed_captions,
+       map<Marker, Time>* markers_seen
        )
 {
-       if (!xsd_dtd_directory) {
-               xsd_dtd_directory = resources_directory() / "xsd";
+       for (auto i: reel->assets()) {
+               if (i->duration() && (i->duration().get() * i->edit_rate().denominator / i->edit_rate().numerator) < 1) {
+                       notes.push_back({VerificationNote::Type::ERROR, VerificationNote::Code::INVALID_DURATION, i->id()});
+               }
+               if ((i->intrinsic_duration() * i->edit_rate().denominator / i->edit_rate().numerator) < 1) {
+                       notes.push_back({VerificationNote::Type::ERROR, VerificationNote::Code::INVALID_INTRINSIC_DURATION, i->id()});
+               }
+               auto file_asset = dynamic_pointer_cast<ReelFileAsset>(i);
+               if (i->encryptable() && !file_asset->hash()) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_HASH, i->id()});
+               }
        }
-       *xsd_dtd_directory = boost::filesystem::canonical (*xsd_dtd_directory);
 
-       vector<VerificationNote> notes;
-       State state{};
+       if (dcp->standard() == Standard::SMPTE) {
+               boost::optional<int64_t> duration;
+               for (auto i: reel->assets()) {
+                       if (!duration) {
+                               duration = i->actual_duration();
+                       } else if (*duration != i->actual_duration()) {
+                               notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISMATCHED_ASSET_DURATION});
+                               break;
+                       }
+               }
+       }
 
-       vector<shared_ptr<DCP>> dcps;
-       for (auto i: directories) {
-               dcps.push_back (make_shared<DCP>(i));
+       if (reel->main_picture()) {
+               /* Check reel stuff */
+               auto const frame_rate = reel->main_picture()->frame_rate();
+               if (frame_rate.denominator != 1 ||
+                   (frame_rate.numerator != 24 &&
+                    frame_rate.numerator != 25 &&
+                    frame_rate.numerator != 30 &&
+                    frame_rate.numerator != 48 &&
+                    frame_rate.numerator != 50 &&
+                    frame_rate.numerator != 60 &&
+                    frame_rate.numerator != 96)) {
+                       notes.push_back({
+                               VerificationNote::Type::ERROR,
+                               VerificationNote::Code::INVALID_PICTURE_FRAME_RATE,
+                               String::compose("%1/%2", frame_rate.numerator, frame_rate.denominator)
+                       });
+               }
+               /* Check asset */
+               if (reel->main_picture()->asset_ref().resolved()) {
+                       verify_main_picture_asset(dcp, reel->main_picture(), stage, progress, notes);
+                       auto const asset_size = reel->main_picture()->asset()->size();
+                       if (main_picture_active_area) {
+                               if (main_picture_active_area->width > asset_size.width) {
+                                       notes.push_back({
+                                                       VerificationNote::Type::ERROR,
+                                                       VerificationNote::Code::INVALID_MAIN_PICTURE_ACTIVE_AREA,
+                                                       String::compose("width %1 is bigger than the asset width %2", main_picture_active_area->width, asset_size.width),
+                                                       cpl->file().get()
+                                                       });
+                               }
+                               if (main_picture_active_area->height > asset_size.height) {
+                                       notes.push_back({
+                                                       VerificationNote::Type::ERROR,
+                                                       VerificationNote::Code::INVALID_MAIN_PICTURE_ACTIVE_AREA,
+                                                       String::compose("height %1 is bigger than the asset height %2", main_picture_active_area->height, asset_size.height),
+                                                       cpl->file().get()
+                                                       });
+                               }
+                       }
+               }
        }
 
-       for (auto dcp: dcps) {
-               stage ("Checking DCP", dcp->directory());
-               bool carry_on = true;
-               try {
-                       dcp->read (&notes, true);
-               } catch (MissingAssetmapError& e) {
-                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
-                       carry_on = false;
-               } catch (ReadError& e) {
-                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
-               } catch (XMLError& e) {
-                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
-               } catch (MXFFileError& e) {
-                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
-               } catch (cxml::Error& e) {
-                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
+       if (reel->main_sound() && reel->main_sound()->asset_ref().resolved()) {
+               verify_main_sound_asset(dcp, reel->main_sound(), stage, progress, notes);
+       }
+
+       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(), reel->main_subtitle()->duration(), stage, xsd_dtd_directory, notes, state);
                }
+               *have_main_subtitle = true;
+       } else {
+               *have_no_main_subtitle = true;
+       }
 
-               if (!carry_on) {
-                       continue;
+       for (auto i: reel->closed_captions()) {
+               verify_closed_caption_reel(i, notes);
+               if (i->asset_ref().resolved()) {
+                       verify_closed_caption_asset(i->asset(), i->duration(), stage, xsd_dtd_directory, notes);
                }
+       }
 
-               if (dcp->standard() != Standard::SMPTE) {
-                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_STANDARD});
+       if (reel->main_markers()) {
+               for (auto const& i: reel->main_markers()->get()) {
+                       markers_seen->insert(i);
+               }
+               if (reel->main_markers()->entry_point()) {
+                       notes.push_back({VerificationNote::Type::ERROR, VerificationNote::Code::UNEXPECTED_ENTRY_POINT});
                }
+               if (reel->main_markers()->duration()) {
+                       notes.push_back({VerificationNote::Type::ERROR, VerificationNote::Code::UNEXPECTED_DURATION});
+               }
+       }
 
-               for (auto cpl: dcp->cpls()) {
-                       stage ("Checking CPL", cpl->file());
-                       validate_xml (cpl->file().get(), *xsd_dtd_directory, notes);
+       *fewest_closed_captions = std::min(*fewest_closed_captions, reel->closed_captions().size());
+       *most_closed_captions = std::max(*most_closed_captions, reel->closed_captions().size());
 
-                       if (cpl->any_encrypted() && !cpl->all_encrypted()) {
-                               notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::PARTIALLY_ENCRYPTED});
-                       }
+}
 
-                       for (auto const& i: cpl->additional_subtitle_languages()) {
-                               verify_language_tag (i, notes);
-                       }
 
-                       if (cpl->release_territory()) {
-                               if (!cpl->release_territory_scope() || cpl->release_territory_scope().get() != "http://www.smpte-ra.org/schemas/429-16/2014/CPL-Metadata#scope/release-territory/UNM49") {
-                                       auto terr = cpl->release_territory().get();
-                                       /* Must be a valid region tag, or "001" */
-                                       try {
-                                               LanguageTag::RegionSubtag test (terr);
-                                       } catch (...) {
-                                               if (terr != "001") {
-                                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_LANGUAGE, terr});
-                                               }
-                                       }
-                               }
-                       }
+static
+void
+verify_cpl(
+       shared_ptr<const DCP> dcp,
+       shared_ptr<const CPL> cpl,
+       function<void (string, optional<boost::filesystem::path>)> stage,
+       boost::filesystem::path xsd_dtd_directory,
+       function<void (float)> progress,
+       vector<VerificationNote>& notes,
+       State& state
+       )
+{
+       stage("Checking CPL", cpl->file());
+       validate_xml(cpl->file().get(), xsd_dtd_directory, notes);
+
+       if (cpl->any_encrypted() && !cpl->all_encrypted()) {
+               notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::PARTIALLY_ENCRYPTED});
+       }
+
+       for (auto const& i: cpl->additional_subtitle_languages()) {
+               verify_language_tag(i, notes);
+       }
+
+       if (!cpl->content_kind().scope() || *cpl->content_kind().scope() == "http://www.smpte-ra.org/schemas/429-7/2006/CPL#standard-content") {
+               /* This is a content kind from http://www.smpte-ra.org/schemas/429-7/2006/CPL#standard-content; make sure it's one
+                * of the approved ones.
+                */
+               auto all = ContentKind::all();
+               auto name = cpl->content_kind().name();
+               transform(name.begin(), name.end(), name.begin(), ::tolower);
+               auto iter = std::find_if(all.begin(), all.end(), [name](ContentKind const& k) { return !k.scope() && k.name() == name; });
+               if (iter == all.end()) {
+                       notes.push_back({VerificationNote::Type::ERROR, VerificationNote::Code::INVALID_CONTENT_KIND, cpl->content_kind().name()});
+               }
+       }
 
-                       if (dcp->standard() == Standard::SMPTE) {
-                               if (!cpl->annotation_text()) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_CPL_ANNOTATION_TEXT, cpl->id(), cpl->file().get()});
-                               } else if (cpl->annotation_text().get() != cpl->content_title_text()) {
-                                       notes.push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::MISMATCHED_CPL_ANNOTATION_TEXT, cpl->id(), cpl->file().get()});
+       if (cpl->release_territory()) {
+               if (!cpl->release_territory_scope() || cpl->release_territory_scope().get() != "http://www.smpte-ra.org/schemas/429-16/2014/CPL-Metadata#scope/release-territory/UNM49") {
+                       auto terr = cpl->release_territory().get();
+                       /* Must be a valid region tag, or "001" */
+                       try {
+                               LanguageTag::RegionSubtag test(terr);
+                       } catch (...) {
+                               if (terr != "001") {
+                                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_LANGUAGE, terr});
                                }
                        }
+               }
+       }
 
-                       for (auto i: dcp->pkls()) {
-                               /* Check that the CPL's hash corresponds to the PKL */
-                               optional<string> h = i->hash(cpl->id());
-                               if (h && make_digest(ArrayData(*cpl->file())) != *h) {
-                                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::MISMATCHED_CPL_HASHES, cpl->id(), cpl->file().get()});
-                               }
+       if (dcp->standard() == Standard::SMPTE) {
+               if (!cpl->annotation_text()) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_CPL_ANNOTATION_TEXT, cpl->id(), cpl->file().get()});
+               } else if (cpl->annotation_text().get() != cpl->content_title_text()) {
+                       notes.push_back({VerificationNote::Type::WARNING, VerificationNote::Code::MISMATCHED_CPL_ANNOTATION_TEXT, cpl->id(), cpl->file().get()});
+               }
+       }
 
-                               /* Check that any PKL with a single CPL has its AnnotationText the same as the CPL's ContentTitleText */
-                               optional<string> required_annotation_text;
-                               for (auto j: i->asset_list()) {
-                                       /* See if this is a CPL */
-                                       for (auto k: dcp->cpls()) {
-                                               if (j->id() == k->id()) {
-                                                       if (!required_annotation_text) {
-                                                               /* First CPL we have found; this is the required AnnotationText unless we find another */
-                                                               required_annotation_text = cpl->content_title_text();
-                                                       } else {
-                                                               /* There's more than one CPL so we don't care what the PKL's AnnotationText is */
-                                                               required_annotation_text = boost::none;
-                                                       }
-                                               }
+       for (auto i: dcp->pkls()) {
+               /* Check that the CPL's hash corresponds to the PKL */
+               optional<string> h = i->hash(cpl->id());
+               if (h && make_digest(ArrayData(*cpl->file())) != *h) {
+                       notes.push_back({VerificationNote::Type::ERROR, VerificationNote::Code::MISMATCHED_CPL_HASHES, cpl->id(), cpl->file().get()});
+               }
+
+               /* Check that any PKL with a single CPL has its AnnotationText the same as the CPL's ContentTitleText */
+               optional<string> required_annotation_text;
+               for (auto j: i->asset_list()) {
+                       /* See if this is a CPL */
+                       for (auto k: dcp->cpls()) {
+                               if (j->id() == k->id()) {
+                                       if (!required_annotation_text) {
+                                               /* First CPL we have found; this is the required AnnotationText unless we find another */
+                                               required_annotation_text = cpl->content_title_text();
+                                       } else {
+                                               /* There's more than one CPL so we don't care what the PKL's AnnotationText is */
+                                               required_annotation_text = boost::none;
                                        }
                                }
+                       }
+               }
 
-                               if (required_annotation_text && i->annotation_text() != required_annotation_text) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISMATCHED_PKL_ANNOTATION_TEXT_WITH_CPL, i->id(), i->file().get()});
-                               }
+               if (required_annotation_text && i->annotation_text() != required_annotation_text) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISMATCHED_PKL_ANNOTATION_TEXT_WITH_CPL, i->id(), i->file().get()});
+               }
+       }
+
+       /* set to true if any reel has a MainSubtitle */
+       auto have_main_subtitle = false;
+       /* set to true if any reel has no MainSubtitle */
+       auto have_no_main_subtitle = false;
+       /* fewest number of closed caption assets seen in a reel */
+       size_t fewest_closed_captions = SIZE_MAX;
+       /* most number of closed caption assets seen in a reel */
+       size_t most_closed_captions = 0;
+       map<Marker, Time> markers_seen;
+
+       auto const main_picture_active_area = cpl->main_picture_active_area();
+       if (main_picture_active_area && (main_picture_active_area->width % 2)) {
+               notes.push_back({
+                               VerificationNote::Type::ERROR,
+                               VerificationNote::Code::INVALID_MAIN_PICTURE_ACTIVE_AREA,
+                               String::compose("width %1 is not a multiple of 2", main_picture_active_area->width),
+                               cpl->file().get()
+                       });
+       }
+       if (main_picture_active_area && (main_picture_active_area->height % 2)) {
+               notes.push_back({
+                               VerificationNote::Type::ERROR,
+                               VerificationNote::Code::INVALID_MAIN_PICTURE_ACTIVE_AREA,
+                               String::compose("height %1 is not a multiple of 2", main_picture_active_area->height),
+                               cpl->file().get()
+                       });
+       }
+
+       for (auto reel: cpl->reels()) {
+               stage("Checking reel", optional<boost::filesystem::path>());
+               verify_reel(
+                       dcp,
+                       cpl,
+                       reel,
+                       main_picture_active_area,
+                       stage,
+                       xsd_dtd_directory,
+                       progress,
+                       notes,
+                       state,
+                       &have_main_subtitle,
+                       &have_no_main_subtitle,
+                       &most_closed_captions,
+                       &fewest_closed_captions,
+                       &markers_seen
+                       );
+       }
+
+       verify_text_details(cpl->reels(), notes);
+
+       if (dcp->standard() == Standard::SMPTE) {
+
+               if (have_main_subtitle && have_no_main_subtitle) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_MAIN_SUBTITLE_FROM_SOME_REELS});
+               }
+
+               if (fewest_closed_captions != most_closed_captions) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISMATCHED_CLOSED_CAPTION_ASSET_COUNTS});
+               }
+
+               if (cpl->content_kind() == ContentKind::FEATURE) {
+                       if (markers_seen.find(Marker::FFEC) == markers_seen.end()) {
+                               notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_FFEC_IN_FEATURE});
+                       }
+                       if (markers_seen.find(Marker::FFMC) == markers_seen.end()) {
+                               notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_FFMC_IN_FEATURE});
                        }
+               }
 
-                       /* set to true if any reel has a MainSubtitle */
-                       auto have_main_subtitle = false;
-                       /* set to true if any reel has no MainSubtitle */
-                       auto have_no_main_subtitle = false;
-                       /* fewest number of closed caption assets seen in a reel */
-                       size_t fewest_closed_captions = SIZE_MAX;
-                       /* most number of closed caption assets seen in a reel */
-                       size_t most_closed_captions = 0;
-                       map<Marker, Time> markers_seen;
-
-                       for (auto reel: cpl->reels()) {
-                               stage ("Checking reel", optional<boost::filesystem::path>());
-
-                               for (auto i: reel->assets()) {
-                                       if (i->duration() && (i->duration().get() * i->edit_rate().denominator / i->edit_rate().numerator) < 1) {
-                                               notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::INVALID_DURATION, i->id()});
-                                       }
-                                       if ((i->intrinsic_duration() * i->edit_rate().denominator / i->edit_rate().numerator) < 1) {
-                                               notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::INVALID_INTRINSIC_DURATION, i->id()});
-                                       }
-                                       auto file_asset = dynamic_pointer_cast<ReelFileAsset>(i);
-                                       if (i->encryptable() && !file_asset->hash()) {
-                                               notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_HASH, i->id()});
-                                       }
-                               }
+               auto ffoc = markers_seen.find(Marker::FFOC);
+               if (ffoc == markers_seen.end()) {
+                       notes.push_back({VerificationNote::Type::WARNING, VerificationNote::Code::MISSING_FFOC});
+               } else if (ffoc->second.e != 1) {
+                       notes.push_back({VerificationNote::Type::WARNING, VerificationNote::Code::INCORRECT_FFOC, raw_convert<string>(ffoc->second.e)});
+               }
 
-                               if (dcp->standard() == Standard::SMPTE) {
-                                       boost::optional<int64_t> duration;
-                                       for (auto i: reel->assets()) {
-                                               if (!duration) {
-                                                       duration = i->actual_duration();
-                                               } else if (*duration != i->actual_duration()) {
-                                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISMATCHED_ASSET_DURATION});
-                                                       break;
-                                               }
-                                       }
-                               }
+               auto lfoc = markers_seen.find(Marker::LFOC);
+               if (lfoc == markers_seen.end()) {
+                       notes.push_back({VerificationNote::Type::WARNING, VerificationNote::Code::MISSING_LFOC});
+               } else {
+                       auto lfoc_time = lfoc->second.as_editable_units_ceil(lfoc->second.tcr);
+                       if (lfoc_time != (cpl->reels().back()->duration() - 1)) {
+                               notes.push_back({VerificationNote::Type::WARNING, VerificationNote::Code::INCORRECT_LFOC, raw_convert<string>(lfoc_time)});
+                       }
+               }
 
-                               if (reel->main_picture()) {
-                                       /* Check reel stuff */
-                                       auto const frame_rate = reel->main_picture()->frame_rate();
-                                       if (frame_rate.denominator != 1 ||
-                                           (frame_rate.numerator != 24 &&
-                                            frame_rate.numerator != 25 &&
-                                            frame_rate.numerator != 30 &&
-                                            frame_rate.numerator != 48 &&
-                                            frame_rate.numerator != 50 &&
-                                            frame_rate.numerator != 60 &&
-                                            frame_rate.numerator != 96)) {
-                                               notes.push_back ({
-                                                       VerificationNote::Type::ERROR,
-                                                       VerificationNote::Code::INVALID_PICTURE_FRAME_RATE,
-                                                       String::compose("%1/%2", frame_rate.numerator, frame_rate.denominator)
-                                               });
-                                       }
-                                       /* Check asset */
-                                       if (reel->main_picture()->asset_ref().resolved()) {
-                                               verify_main_picture_asset (dcp, reel->main_picture(), stage, progress, notes);
-                                       }
-                               }
+               LinesCharactersResult result;
+               for (auto reel: cpl->reels()) {
+                       if (reel->main_subtitle() && reel->main_subtitle()->asset()) {
+                               verify_text_lines_and_characters(reel->main_subtitle()->asset(), 52, 79, &result);
+                       }
+               }
 
-                               if (reel->main_sound() && reel->main_sound()->asset_ref().resolved()) {
-                                       verify_main_sound_asset (dcp, reel->main_sound(), stage, progress, notes);
-                               }
+               if (result.line_count_exceeded) {
+                       notes.push_back({VerificationNote::Type::WARNING, VerificationNote::Code::INVALID_SUBTITLE_LINE_COUNT});
+               }
+               if (result.error_length_exceeded) {
+                       notes.push_back({VerificationNote::Type::WARNING, VerificationNote::Code::INVALID_SUBTITLE_LINE_LENGTH});
+               } else if (result.warning_length_exceeded) {
+                       notes.push_back({VerificationNote::Type::WARNING, VerificationNote::Code::NEARLY_INVALID_SUBTITLE_LINE_LENGTH});
+               }
 
-                               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(), reel->main_subtitle()->duration(), stage, *xsd_dtd_directory, notes, state);
-                                       }
-                                       have_main_subtitle = true;
-                               } else {
-                                       have_no_main_subtitle = true;
+               result = LinesCharactersResult();
+               for (auto reel: cpl->reels()) {
+                       for (auto i: reel->closed_captions()) {
+                               if (i->asset()) {
+                                       verify_text_lines_and_characters(i->asset(), 32, 32, &result);
                                }
+                       }
+               }
 
-                               for (auto i: reel->closed_captions()) {
-                                       verify_closed_caption_reel (i, notes);
-                                       if (i->asset_ref().resolved()) {
-                                               verify_closed_caption_asset (i->asset(), i->duration(), stage, *xsd_dtd_directory, notes);
-                                       }
-                               }
+               if (result.line_count_exceeded) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_CLOSED_CAPTION_LINE_COUNT});
+               }
+               if (result.error_length_exceeded) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_CLOSED_CAPTION_LINE_LENGTH});
+               }
 
-                               if (reel->main_markers()) {
-                                       for (auto const& i: reel->main_markers()->get()) {
-                                               markers_seen.insert (i);
-                                       }
-                                       if (reel->main_markers()->entry_point()) {
-                                               notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::UNEXPECTED_ENTRY_POINT});
-                                       }
-                                       if (reel->main_markers()->duration()) {
-                                               notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::UNEXPECTED_DURATION});
-                                       }
-                               }
+               if (!cpl->read_composition_metadata()) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_CPL_METADATA, cpl->id(), cpl->file().get()});
+               } else if (!cpl->version_number()) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_CPL_METADATA_VERSION_NUMBER, cpl->id(), cpl->file().get()});
+               }
+
+               verify_extension_metadata(cpl, notes);
 
-                               fewest_closed_captions = std::min (fewest_closed_captions, reel->closed_captions().size());
-                               most_closed_captions = std::max (most_closed_captions, reel->closed_captions().size());
+               if (cpl->any_encrypted()) {
+                       cxml::Document doc("CompositionPlaylist");
+                       DCP_ASSERT(cpl->file());
+                       doc.read_file(cpl->file().get());
+                       if (!doc.optional_node_child("Signature")) {
+                               notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::UNSIGNED_CPL_WITH_ENCRYPTED_CONTENT, cpl->id(), cpl->file().get()});
                        }
+               }
+       }
+}
 
-                       verify_text_details (cpl->reels(), notes);
 
-                       if (dcp->standard() == Standard::SMPTE) {
+static
+void
+verify_pkl(
+       shared_ptr<const DCP> dcp,
+       shared_ptr<const PKL> pkl,
+       boost::filesystem::path xsd_dtd_directory,
+       vector<VerificationNote>& notes
+       )
+{
+       validate_xml(pkl->file().get(), xsd_dtd_directory, notes);
 
-                               if (have_main_subtitle && have_no_main_subtitle) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_MAIN_SUBTITLE_FROM_SOME_REELS});
-                               }
+       if (pkl_has_encrypted_assets(dcp, pkl)) {
+               cxml::Document doc("PackingList");
+               doc.read_file(pkl->file().get());
+               if (!doc.optional_node_child("Signature")) {
+                       notes.push_back({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::UNSIGNED_PKL_WITH_ENCRYPTED_CONTENT, pkl->id(), pkl->file().get()});
+               }
+       }
 
-                               if (fewest_closed_captions != most_closed_captions) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISMATCHED_CLOSED_CAPTION_ASSET_COUNTS});
-                               }
+       set<string> uuid_set;
+       for (auto asset: pkl->asset_list()) {
+               if (!uuid_set.insert(asset->id()).second) {
+                       notes.push_back({VerificationNote::Type::ERROR, VerificationNote::Code::DUPLICATE_ASSET_ID_IN_PKL, pkl->id(), pkl->file().get()});
+                       break;
+               }
+       }
+}
 
-                               if (cpl->content_kind() == ContentKind::FEATURE) {
-                                       if (markers_seen.find(Marker::FFEC) == markers_seen.end()) {
-                                               notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_FFEC_IN_FEATURE});
-                                       }
-                                       if (markers_seen.find(Marker::FFMC) == markers_seen.end()) {
-                                               notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_FFMC_IN_FEATURE});
-                                       }
-                               }
 
-                               auto ffoc = markers_seen.find(Marker::FFOC);
-                               if (ffoc == markers_seen.end()) {
-                                       notes.push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::MISSING_FFOC});
-                               } else if (ffoc->second.e != 1) {
-                                       notes.push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::INCORRECT_FFOC, raw_convert<string>(ffoc->second.e)});
-                               }
 
-                               auto lfoc = markers_seen.find(Marker::LFOC);
-                               if (lfoc == markers_seen.end()) {
-                                       notes.push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::MISSING_LFOC});
-                               } else {
-                                       auto lfoc_time = lfoc->second.as_editable_units_ceil(lfoc->second.tcr);
-                                       if (lfoc_time != (cpl->reels().back()->duration() - 1)) {
-                                               notes.push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::INCORRECT_LFOC, raw_convert<string>(lfoc_time)});
-                                       }
-                               }
+static
+void
+verify_assetmap(
+       shared_ptr<const DCP> dcp,
+       boost::filesystem::path xsd_dtd_directory,
+       vector<VerificationNote>& notes
+       )
+{
+       validate_xml(dcp->asset_map_path().get(), xsd_dtd_directory, notes);
+}
 
-                               LinesCharactersResult result;
-                               for (auto reel: cpl->reels()) {
-                                       if (reel->main_subtitle() && reel->main_subtitle()->asset()) {
-                                               verify_text_lines_and_characters (reel->main_subtitle()->asset(), 52, 79, &result);
-                                       }
-                               }
 
-                               if (result.line_count_exceeded) {
-                                       notes.push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::INVALID_SUBTITLE_LINE_COUNT});
-                               }
-                               if (result.error_length_exceeded) {
-                                       notes.push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::INVALID_SUBTITLE_LINE_LENGTH});
-                               } else if (result.warning_length_exceeded) {
-                                       notes.push_back ({VerificationNote::Type::WARNING, VerificationNote::Code::NEARLY_INVALID_SUBTITLE_LINE_LENGTH});
-                               }
+vector<VerificationNote>
+dcp::verify (
+       vector<boost::filesystem::path> directories,
+       function<void (string, optional<boost::filesystem::path>)> stage,
+       function<void (float)> progress,
+       optional<boost::filesystem::path> xsd_dtd_directory
+       )
+{
+       if (!xsd_dtd_directory) {
+               xsd_dtd_directory = resources_directory() / "xsd";
+       }
+       *xsd_dtd_directory = boost::filesystem::canonical (*xsd_dtd_directory);
 
-                               result = LinesCharactersResult();
-                               for (auto reel: cpl->reels()) {
-                                       for (auto i: reel->closed_captions()) {
-                                               if (i->asset()) {
-                                                       verify_text_lines_and_characters (i->asset(), 32, 32, &result);
-                                               }
-                                       }
-                               }
+       vector<VerificationNote> notes;
+       State state{};
 
-                               if (result.line_count_exceeded) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_CLOSED_CAPTION_LINE_COUNT});
-                               }
-                               if (result.error_length_exceeded) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_CLOSED_CAPTION_LINE_LENGTH});
-                               }
+       vector<shared_ptr<DCP>> dcps;
+       for (auto i: directories) {
+               dcps.push_back (make_shared<DCP>(i));
+       }
 
-                               if (!cpl->read_composition_metadata()) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_CPL_METADATA, cpl->id(), cpl->file().get()});
-                               } else if (!cpl->version_number()) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::MISSING_CPL_METADATA_VERSION_NUMBER, cpl->id(), cpl->file().get()});
-                               }
+       for (auto dcp: dcps) {
+               stage ("Checking DCP", dcp->directory());
+               bool carry_on = true;
+               try {
+                       dcp->read (&notes, true);
+               } catch (MissingAssetmapError& e) {
+                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
+                       carry_on = false;
+               } catch (ReadError& e) {
+                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
+               } catch (XMLError& e) {
+                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
+               } catch (MXFFileError& e) {
+                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
+               } catch (cxml::Error& e) {
+                       notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::FAILED_READ, string(e.what())});
+               }
 
-                               verify_extension_metadata (cpl, notes);
+               if (!carry_on) {
+                       continue;
+               }
 
-                               if (cpl->any_encrypted()) {
-                                       cxml::Document doc ("CompositionPlaylist");
-                                       DCP_ASSERT (cpl->file());
-                                       doc.read_file (cpl->file().get());
-                                       if (!doc.optional_node_child("Signature")) {
-                                               notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::UNSIGNED_CPL_WITH_ENCRYPTED_CONTENT, cpl->id(), cpl->file().get()});
-                                       }
-                               }
-                       }
+               if (dcp->standard() != Standard::SMPTE) {
+                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::INVALID_STANDARD});
+               }
+
+               for (auto cpl: dcp->cpls()) {
+                       verify_cpl(
+                               dcp,
+                               cpl,
+                               stage,
+                               *xsd_dtd_directory,
+                               progress,
+                               notes,
+                               state
+                               );
                }
 
                for (auto pkl: dcp->pkls()) {
-                       stage ("Checking PKL", pkl->file());
-                       validate_xml (pkl->file().get(), *xsd_dtd_directory, notes);
-                       if (pkl_has_encrypted_assets(dcp, pkl)) {
-                               cxml::Document doc ("PackingList");
-                               doc.read_file (pkl->file().get());
-                               if (!doc.optional_node_child("Signature")) {
-                                       notes.push_back ({VerificationNote::Type::BV21_ERROR, VerificationNote::Code::UNSIGNED_PKL_WITH_ENCRYPTED_CONTENT, pkl->id(), pkl->file().get()});
-                               }
-                       }
+                       stage("Checking PKL", pkl->file());
+                       verify_pkl(dcp, pkl, *xsd_dtd_directory, notes);
                }
 
                if (dcp->asset_map_path()) {
                        stage ("Checking ASSETMAP", dcp->asset_map_path().get());
-                       validate_xml (dcp->asset_map_path().get(), *xsd_dtd_directory, notes);
+                       verify_assetmap(dcp, *xsd_dtd_directory, notes);
                } else {
                        notes.push_back ({VerificationNote::Type::ERROR, VerificationNote::Code::MISSING_ASSETMAP});
                }
@@ -1676,11 +1825,11 @@ dcp::note_to_string (VerificationNote note)
        case VerificationNote::Code::MISSING_CPL_ANNOTATION_TEXT:
                return String::compose("The CPL %1 has no <AnnotationText> tag.", note.note().get());
        case VerificationNote::Code::MISMATCHED_CPL_ANNOTATION_TEXT:
-               return String::compose("The CPL %1 has an <AnnotationText> which differs from its <ContentTitleText>", note.note().get());
+               return String::compose("The CPL %1 has an <AnnotationText> which differs from its <ContentTitleText>.", note.note().get());
        case VerificationNote::Code::MISMATCHED_ASSET_DURATION:
                return "All assets in a reel do not have the same duration.";
        case VerificationNote::Code::MISSING_MAIN_SUBTITLE_FROM_SOME_REELS:
-               return "At least one reel contains a subtitle asset, but some reel(s) do not";
+               return "At least one reel contains a subtitle asset, but some reel(s) do not.";
        case VerificationNote::Code::MISMATCHED_CLOSED_CAPTION_ASSET_COUNTS:
                return "At least one reel has closed captions, but reels have different numbers of closed caption assets.";
        case VerificationNote::Code::MISSING_SUBTITLE_ENTRY_POINT:
@@ -1694,13 +1843,13 @@ dcp::note_to_string (VerificationNote note)
        case VerificationNote::Code::MISSING_HASH:
                return String::compose("The asset %1 has no <Hash> tag in the CPL.", note.note().get());
        case VerificationNote::Code::MISSING_FFEC_IN_FEATURE:
-               return "The DCP is marked as a Feature but there is no FFEC (first frame of end credits) marker";
+               return "The DCP is marked as a Feature but there is no FFEC (first frame of end credits) marker.";
        case VerificationNote::Code::MISSING_FFMC_IN_FEATURE:
-               return "The DCP is marked as a Feature but there is no FFMC (first frame of moving credits) marker";
+               return "The DCP is marked as a Feature but there is no FFMC (first frame of moving credits) marker.";
        case VerificationNote::Code::MISSING_FFOC:
-               return "There should be a FFOC (first frame of content) marker";
+               return "There should be a FFOC (first frame of content) marker.";
        case VerificationNote::Code::MISSING_LFOC:
-               return "There should be a LFOC (last frame of content) marker";
+               return "There should be a LFOC (last frame of content) marker.";
        case VerificationNote::Code::INCORRECT_FFOC:
                return String::compose("The FFOC marker is %1 instead of 1", note.note().get());
        case VerificationNote::Code::INCORRECT_LFOC:
@@ -1722,7 +1871,7 @@ dcp::note_to_string (VerificationNote note)
        case VerificationNote::Code::PARTIALLY_ENCRYPTED:
                return "Some assets are encrypted but some are not.";
        case VerificationNote::Code::INVALID_JPEG2000_CODESTREAM:
-               return String::compose("The JPEG2000 codestream for at least one frame is invalid (%1)", note.note().get());
+               return String::compose("The JPEG2000 codestream for at least one frame is invalid (%1).", note.note().get());
        case VerificationNote::Code::INVALID_JPEG2000_GUARD_BITS_FOR_2K:
                return String::compose("The JPEG2000 codestream uses %1 guard bits in a 2K image instead of 1.", note.note().get());
        case VerificationNote::Code::INVALID_JPEG2000_GUARD_BITS_FOR_4K:
@@ -1738,9 +1887,9 @@ dcp::note_to_string (VerificationNote note)
        case VerificationNote::Code::INCORRECT_JPEG2000_POC_MARKER_COUNT_FOR_4K:
                return String::compose("%1 POC markers found in 4K JPEG2000 codestream instead of 1.", note.note().get());
        case VerificationNote::Code::INCORRECT_JPEG2000_POC_MARKER:
-               return String::compose("Incorrect POC marker content found (%1)", note.note().get());
+               return String::compose("Incorrect POC marker content found (%1).", note.note().get());
        case VerificationNote::Code::INVALID_JPEG2000_POC_MARKER_LOCATION:
-               return "POC marker found outside main header";
+               return "POC marker found outside main header.";
        case VerificationNote::Code::INVALID_JPEG2000_TILE_PARTS_FOR_2K:
                return String::compose("The JPEG2000 codestream has %1 tile parts in a 2K image instead of 3.", note.note().get());
        case VerificationNote::Code::INVALID_JPEG2000_TILE_PARTS_FOR_4K:
@@ -1770,6 +1919,12 @@ dcp::note_to_string (VerificationNote note)
                return "There is an <EntryPoint> node inside a <MainMarkers>.";
        case VerificationNote::Code::UNEXPECTED_DURATION:
                return "There is an <Duration> node inside a <MainMarkers>.";
+       case VerificationNote::Code::INVALID_CONTENT_KIND:
+               return String::compose("<ContentKind> has an invalid value %1.", note.note().get());
+       case VerificationNote::Code::INVALID_MAIN_PICTURE_ACTIVE_AREA:
+               return String::compose("<MainPictureActiveaArea> has an invalid value: %1", note.note().get());
+       case VerificationNote::Code::DUPLICATE_ASSET_ID_IN_PKL:
+               return String::compose("The PKL %1 has more than one asset with the same ID", note.note().get());
        }
 
        return "";