Supporters update.
[dcpomatic.git] / src / lib / film.cc
index 803dbb73204ad7a3b223ca183273d33482066d30..d9ab6e2a3f8a2e4df368a6eece61ae2b37187b44 100644 (file)
@@ -63,6 +63,7 @@
 #include <dcp/certificate_chain.h>
 #include <dcp/cpl.h>
 #include <dcp/decrypted_kdm.h>
+#include <dcp/filesystem.h>
 #include <dcp/local_time.h>
 #include <dcp/raw_convert.h>
 #include <dcp/reel_asset.h>
@@ -205,27 +206,7 @@ Film::Film (optional<boost::filesystem::path> dir)
        _playlist_length_change_connection = _playlist->LengthChange.connect (bind(&Film::playlist_length_change, this));
 
        if (dir) {
-               /* Make state.directory a complete path without ..s (where possible)
-                  (Code swiped from Adam Bowen on stackoverflow)
-                  XXX: couldn't/shouldn't this just be boost::filesystem::canonical?
-               */
-
-               boost::filesystem::path p (boost::filesystem::system_complete (dir.get()));
-               boost::filesystem::path result;
-               for (auto i: p) {
-                       if (i == "..") {
-                               boost::system::error_code ec;
-                               if (boost::filesystem::is_symlink(result, ec) || result.filename() == "..") {
-                                       result /= i;
-                               } else {
-                                       result = result.parent_path ();
-                               }
-                       } else if (i != ".") {
-                               result /= i;
-                       }
-               }
-
-               set_directory (result.make_preferred ());
+               set_directory(dcp::filesystem::weakly_canonical(*dir));
        }
 
        if (_directory) {
@@ -445,6 +426,7 @@ Film::metadata (bool with_content_paths) const
                root->add_child("ContentVersion")->add_child_text(i);
        }
        root->add_child("NameLanguage")->add_child_text(_name_language.to_string());
+       root->add_child("TerritoryType")->add_child_text(territory_type_to_string(_territory_type));
        if (_release_territory) {
                root->add_child("ReleaseTerritory")->add_child_text(_release_territory->subtag());
        }
@@ -494,7 +476,7 @@ void
 Film::write_metadata ()
 {
        DCPOMATIC_ASSERT (directory());
-       boost::filesystem::create_directories (directory().get());
+       dcp::filesystem::create_directories(directory().get());
        auto const filename = file(metadata_file);
        try {
                metadata()->write_to_file_formatted(filename.string());
@@ -508,7 +490,7 @@ Film::write_metadata ()
 void
 Film::write_template (boost::filesystem::path path) const
 {
-       boost::filesystem::create_directories (path.parent_path());
+       dcp::filesystem::create_directories(path.parent_path());
        shared_ptr<xmlpp::Document> doc = metadata (false);
        metadata(false)->write_to_file_formatted(path.string());
 }
@@ -520,19 +502,19 @@ list<string>
 Film::read_metadata (optional<boost::filesystem::path> path)
 {
        if (!path) {
-               if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file (metadata_file))) {
+               if (dcp::filesystem::exists(file("metadata")) && !dcp::filesystem::exists(file(metadata_file))) {
                        throw runtime_error (_("This film was created with an older version of DCP-o-matic, and unfortunately it cannot be loaded into this version.  You will need to create a new Film, re-add your content and set it up again.  Sorry!"));
                }
 
                path = file (metadata_file);
        }
 
-       if (!boost::filesystem::exists(*path)) {
+       if (!dcp::filesystem::exists(*path)) {
                throw FileNotFoundError(*path);
        }
 
        cxml::Document f ("Metadata");
-       f.read_file (path.get ());
+       f.read_file(dcp::filesystem::fix_long_path(path.get()));
 
        _state_version = f.number_child<int> ("Version");
        if (_state_version > current_state_version) {
@@ -540,9 +522,9 @@ Film::read_metadata (optional<boost::filesystem::path> path)
        } else if (_state_version < current_state_version) {
                /* This is an older version; save a copy (if we haven't already) */
                auto const older = path->parent_path() / String::compose("metadata.%1.xml", _state_version);
-               if (!boost::filesystem::is_regular_file(older)) {
+               if (!dcp::filesystem::is_regular_file(older)) {
                        try {
-                               boost::filesystem::copy_file(*path, older);
+                               dcp::filesystem::copy_file(*path, older);
                        } catch (...) {
                                /* Never mind; at least we tried */
                        }
@@ -636,6 +618,10 @@ Film::read_metadata (optional<boost::filesystem::path> path)
        if (name_language) {
                _name_language = dcp::LanguageTag (*name_language);
        }
+       auto territory_type = f.optional_string_child("TerritoryType");
+       if (territory_type) {
+               _territory_type = string_to_territory_type(*territory_type);
+       }
        auto release_territory = f.optional_string_child("ReleaseTerritory");
        if (release_territory) {
                _release_territory = dcp::LanguageTag::RegionSubtag (*release_territory);
@@ -707,10 +693,10 @@ Film::read_metadata (optional<boost::filesystem::path> path)
                }
                _studio = isdcf->optional_string_child("Studio");
                _facility = isdcf->optional_string_child("Facility");
-               _temp_version = isdcf->optional_bool_child("TempVersion").get_value_or("false");
-               _pre_release = isdcf->optional_bool_child("PreRelease").get_value_or("false");
-               _red_band = isdcf->optional_bool_child("RedBand").get_value_or("false");
-               _two_d_version_of_three_d = isdcf->optional_bool_child("TwoDVersionOfThreeD").get_value_or("false");
+               _temp_version = isdcf->optional_bool_child("TempVersion").get_value_or(false);
+               _pre_release = isdcf->optional_bool_child("PreRelease").get_value_or(false);
+               _red_band = isdcf->optional_bool_child("RedBand").get_value_or(false);
+               _two_d_version_of_three_d = isdcf->optional_bool_child("TwoDVersionOfThreeD").get_value_or(false);
                _chain = isdcf->optional_string_child("Chain");
        }
 
@@ -740,7 +726,7 @@ Film::dir (boost::filesystem::path d, bool create) const
        p /= d;
 
        if (create) {
-               boost::filesystem::create_directories (p);
+               dcp::filesystem::create_directories(p);
        }
 
        return p;
@@ -758,7 +744,7 @@ Film::file (boost::filesystem::path f) const
        p /= _directory.get();
        p /= f;
 
-       boost::filesystem::create_directories (p.parent_path ());
+       dcp::filesystem::create_directories(p.parent_path());
 
        return p;
 }
@@ -790,16 +776,27 @@ Film::mapped_audio_channels () const
 
 
 pair<optional<dcp::LanguageTag>, vector<dcp::LanguageTag>>
-Film::subtitle_languages () const
+Film::subtitle_languages(bool* burnt_in) const
 {
+       if (burnt_in) {
+               *burnt_in = true;
+       }
+
        pair<optional<dcp::LanguageTag>, vector<dcp::LanguageTag>> result;
        for (auto i: content()) {
-               for (auto text: i->text) {
-                       if (text->use() && text->type() == TextType::OPEN_SUBTITLE && text->language()) {
-                               if (text->language_is_additional()) {
-                                       result.second.push_back(text->language().get());
-                               } else {
-                                       result.first = text->language().get();
+               auto dcp = dynamic_pointer_cast<DCPContent>(i);
+               for (auto const& text: i->text) {
+                       auto const use = text->use() || (dcp && dcp->reference_text(TextType::OPEN_SUBTITLE));
+                       if (use && text->type() == TextType::OPEN_SUBTITLE) {
+                               if (!text->burn() && burnt_in) {
+                                       *burnt_in = false;
+                               }
+                               if (text->language()) {
+                                       if (text->language_is_additional()) {
+                                               result.second.push_back(text->language().get());
+                                       } else {
+                                               result.first = text->language().get();
+                                       }
                                }
                        }
                }
@@ -946,10 +943,12 @@ Film::isdcf_name (bool if_created_now) const
        if (dcp_content_type() && dcp_content_type()->libdcp_kind() != dcp::ContentKind::TRAILER) {
                auto first_video = std::find_if(content_list.begin(), content_list.end(), [](shared_ptr<Content> c) { return static_cast<bool>(c->video); });
                if (first_video != content_list.end()) {
-                       auto first_ratio = lrintf((*first_video)->video->scaled_size(frame_size()).ratio() * 100);
-                       auto container_ratio = lrintf(container()->ratio() * 100);
-                       if (first_ratio != container_ratio) {
-                               isdcf_name += "-" + dcp::raw_convert<string>(first_ratio);
+                       if (auto scaled_size = (*first_video)->video->scaled_size(frame_size())) {
+                               auto first_ratio = lrintf(scaled_size->ratio() * 100);
+                               auto container_ratio = lrintf(container()->ratio() * 100);
+                               if (first_ratio != container_ratio) {
+                                       isdcf_name += "-" + dcp::raw_convert<string>(first_ratio);
+                               }
                        }
                }
        }
@@ -969,16 +968,8 @@ Film::isdcf_name (bool if_created_now) const
 
        isdcf_name += "_" + to_upper (audio_language);
 
-       auto burnt_in = true;
-       for (auto i: content_list) {
-               for (auto text: i->text) {
-                       if (text->type() == TextType::OPEN_SUBTITLE && text->use() && !text->burn()) {
-                               burnt_in = false;
-                       }
-               }
-       }
-
-       auto sub_langs = subtitle_languages();
+       bool burnt_in;
+       auto sub_langs = subtitle_languages(&burnt_in);
        auto ccap_langs = closed_caption_languages();
        if (sub_langs.first && sub_langs.first->language()) {
                auto lang = entry_for_language(*sub_langs.first);
@@ -996,7 +987,11 @@ Film::isdcf_name (bool if_created_now) const
                isdcf_name += "-XX";
        }
 
-       if (_release_territory) {
+       if (_territory_type == TerritoryType::INTERNATIONAL_TEXTED) {
+               isdcf_name += "_INT-TD";
+       } else if (_territory_type == TerritoryType::INTERNATIONAL_TEXTLESS) {
+               isdcf_name += "_INT-TL";
+       } else if (_release_territory) {
                auto territory = _release_territory->subtag();
                isdcf_name += "_" + to_upper (territory);
                if (!_ratings.empty()) {
@@ -1319,10 +1314,10 @@ Film::cpls () const
        vector<CPLSummary> out;
 
        auto const dir = directory().get();
-       for (auto const& item: boost::filesystem::directory_iterator(dir)) {
+       for (auto const& item: dcp::filesystem::directory_iterator(dir)) {
                if (
-                       boost::filesystem::is_directory(item) &&
-                       item.path().leaf() != "j2c" && item.path().leaf() != "video" && item.path().leaf() != "info" && item.path().leaf() != "analysis"
+                       dcp::filesystem::is_directory(item) &&
+                       item.path().filename() != "j2c" && item.path().filename() != "video" && item.path().filename() != "info" && item.path().filename() != "analysis"
                        ) {
 
                        try {
@@ -1419,7 +1414,9 @@ Film::add_content (shared_ptr<Content> c)
 
        maybe_set_container_and_resolution ();
        if (c->atmos) {
-               set_audio_channels (14);
+               if (_audio_channels < 14) {
+                       set_audio_channels(14);
+               }
                set_interop (false);
        }
 }
@@ -1440,13 +1437,13 @@ Film::maybe_set_container_and_resolution ()
                }
        }
 
-       if (video) {
+       if (video && video->size()) {
                /* This is the only piece of video content in this Film.  Use it to make a guess for
                 * DCP container size and resolution, unless the user has already explicitly set these
                 * things.
                 */
                if (!_user_explicit_container) {
-                       if (video->size().ratio() > 2.3) {
+                       if (video->size()->ratio() > 2.3) {
                                set_container (Ratio::from_id("239"), false);
                        } else {
                                set_container (Ratio::from_id("185"), false);
@@ -1454,7 +1451,7 @@ Film::maybe_set_container_and_resolution ()
                }
 
                if (!_user_explicit_resolution) {
-                       if (video->size_after_crop().width > 2048 || video->size_after_crop().height > 1080) {
+                       if (video->size_after_crop()->width > 2048 || video->size_after_crop()->height > 1080) {
                                set_resolution (Resolution::FOUR_K, false);
                        } else {
                                set_resolution (Resolution::TWO_K, false);
@@ -1652,9 +1649,10 @@ Film::active_area () const
 
        for (auto i: content()) {
                if (i->video) {
-                       dcp::Size s = i->video->scaled_size (frame);
-                       active.width = max(active.width, s.width);
-                       active.height = max(active.height, s.height);
+                       if (auto s = i->video->scaled_size(frame)) {
+                               active.width = max(active.width, s->width);
+                               active.height = max(active.height, s->height);
+                       }
                }
        }
 
@@ -1743,15 +1741,15 @@ Film::should_be_enough_disk_space (double& required, double& available, bool& ca
        if (f) {
                f.close();
                boost::system::error_code ec;
-               boost::filesystem::create_hard_link (test, test2, ec);
+               dcp::filesystem::create_hard_link(test, test2, ec);
                if (ec) {
                        can_hard_link = false;
                }
-               boost::filesystem::remove (test);
-               boost::filesystem::remove (test2);
+               dcp::filesystem::remove(test);
+               dcp::filesystem::remove(test2);
        }
 
-       auto s = boost::filesystem::space (internal_video_asset_dir ());
+       auto s = dcp::filesystem::space(internal_video_asset_dir());
        required = double (required_disk_space ()) / 1073741824.0f;
        if (!can_hard_link) {
                required *= 2;
@@ -2091,10 +2089,10 @@ Film::info_file_handle (DCPTimePeriod period, bool read) const
 
 InfoFileHandle::InfoFileHandle (boost::mutex& mutex, boost::filesystem::path path, bool read)
        : _lock (mutex)
-       , _file (path, read ? "rb" : (boost::filesystem::exists(path) ? "r+b" : "wb"))
+       , _file(path, read ? "rb" : (dcp::filesystem::exists(path) ? "r+b" : "wb"))
 {
        if (!_file) {
-               throw OpenFileError (path, errno, read ? OpenFileError::READ : (boost::filesystem::exists(path) ? OpenFileError::READ_WRITE : OpenFileError::WRITE));
+               throw OpenFileError(path, errno, read ? OpenFileError::READ : (dcp::filesystem::exists(path) ? OpenFileError::READ_WRITE : OpenFileError::WRITE));
        }
 }
 
@@ -2224,3 +2222,11 @@ Film::last_written_by_earlier_than(int major, int minor, int micro) const
        return our_micro < micro;
 }
 
+
+void
+Film::set_territory_type(TerritoryType type)
+{
+       FilmChangeSignaller ch(this, FilmProperty::TERRITORY_TYPE);
+       _territory_type = type;
+}
+