Fix the build for older macOS.
[dcpomatic.git] / src / lib / film.cc
index c57fc165cf9a1b72db957cccd2ac8ef4dff8092f..0d277b82a0a8e92c1825eedc86dc6b08c7b3085c 100644 (file)
 
 */
 
+
 /** @file  src/film.cc
  *  @brief A representation of some audio, video and subtitle content, and details of
  *  how they should be presented in a DCP.
  */
 
+
 #include "atmos_content.h"
+#include "audio_content.h"
+#include "audio_processor.h"
+#include "change_signaller.h"
+#include "check_content_change_job.h"
+#include "cinema.h"
+#include "compose.hpp"
+#include "config.h"
+#include "cross.h"
+#include "dcp_content.h"
+#include "dcp_content_type.h"
+#include "dcp_encoder.h"
+#include "dcpomatic_log.h"
+#include "digester.h"
+#include "environment_info.h"
+#include "examine_content_job.h"
+#include "exceptions.h"
+#include "ffmpeg_content.h"
+#include "ffmpeg_subtitle_stream.h"
+#include "file_log.h"
 #include "film.h"
+#include "font.h"
 #include "job.h"
-#include "util.h"
 #include "job_manager.h"
-#include "dcp_encoder.h"
-#include "transcode_job.h"
-#include "upload_job.h"
+#include "kdm_with_metadata.h"
 #include "null_log.h"
-#include "file_log.h"
-#include "dcpomatic_log.h"
-#include "exceptions.h"
-#include "examine_content_job.h"
-#include "config.h"
 #include "playlist.h"
-#include "dcp_content_type.h"
 #include "ratio.h"
-#include "cross.h"
-#include "environment_info.h"
-#include "audio_processor.h"
-#include "digester.h"
-#include "compose.hpp"
 #include "screen.h"
-#include "audio_content.h"
-#include "video_content.h"
 #include "text_content.h"
-#include "ffmpeg_content.h"
-#include "dcp_content.h"
-#include "kdm_with_metadata.h"
-#include "cinema.h"
-#include "change_signaller.h"
-#include "check_content_change_job.h"
-#include "ffmpeg_subtitle_stream.h"
-#include "font.h"
+#include "transcode_job.h"
+#include "upload_job.h"
+#include "util.h"
+#include "video_content.h"
+#include "version.h"
 #include <libcxml/cxml.h>
-#include <dcp/cpl.h>
 #include <dcp/certificate_chain.h>
-#include <dcp/util.h>
-#include <dcp/local_time.h>
+#include <dcp/cpl.h>
 #include <dcp/decrypted_kdm.h>
+#include <dcp/local_time.h>
 #include <dcp/raw_convert.h>
-#include <dcp/reel_file_asset.h>
 #include <dcp/reel_asset.h>
+#include <dcp/reel_file_asset.h>
+#include <dcp/util.h>
 #include <libxml++/libxml++.h>
-#include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
+#include <boost/filesystem.hpp>
 #include <boost/regex.hpp>
 #include <unistd.h>
 #include <stdexcept>
 
 #include "i18n.h"
 
-using std::string;
-using std::pair;
-using std::vector;
-using std::setfill;
-using std::min;
-using std::max;
-using std::make_pair;
-using std::cout;
-using std::list;
-using std::set;
-using std::runtime_error;
-using std::copy;
+
 using std::back_inserter;
-using std::map;
+using std::copy;
+using std::cout;
+using std::dynamic_pointer_cast;
 using std::exception;
 using std::find;
+using std::list;
+using std::make_pair;
+using std::make_shared;
+using std::map;
+using std::max;
+using std::min;
+using std::pair;
+using std::runtime_error;
+using std::set;
+using std::setfill;
 using std::shared_ptr;
+using std::string;
+using std::vector;
 using std::weak_ptr;
-using std::make_shared;
-using std::dynamic_pointer_cast;
 using boost::optional;
 using boost::is_any_of;
 #if BOOST_VERSION >= 106100
@@ -108,7 +112,9 @@ using namespace boost::placeholders;
 using dcp::raw_convert;
 using namespace dcpomatic;
 
-string const Film::metadata_file = "metadata.xml";
+
+static constexpr char metadata_file[] = "metadata.xml";
+
 
 /* 5 -> 6
  * AudioMapping XML changed.
@@ -141,6 +147,7 @@ string const Film::metadata_file = "metadata.xml";
  */
 int const Film::current_state_version = 38;
 
+
 /** Construct a Film object in a given directory.
  *
  *  @param dir Film directory.
@@ -176,6 +183,20 @@ Film::Film (optional<boost::filesystem::path> dir)
 {
        set_isdcf_date_today ();
 
+       auto metadata = Config::instance()->default_metadata();
+       if (metadata.find("chain") != metadata.end()) {
+               _chain = metadata["chain"];
+       }
+       if (metadata.find("distributor") != metadata.end()) {
+               _distributor = metadata["distributor"];
+       }
+       if (metadata.find("facility") != metadata.end()) {
+               _facility = metadata["facility"];
+       }
+       if (metadata.find("studio") != metadata.end()) {
+               _studio = metadata["studio"];
+       }
+
        _playlist_change_connection = _playlist->Change.connect (bind (&Film::playlist_change, this, _1));
        _playlist_order_changed_connection = _playlist->OrderChange.connect (bind (&Film::playlist_order_changed, this));
        _playlist_content_change_connection = _playlist->ContentChange.connect (bind (&Film::playlist_content_change, this, _1, _2, _3, _4));
@@ -253,6 +274,10 @@ Film::video_identifier () const
                s += "_3D";
        }
 
+       if (_reencode_j2k) {
+               s += "_R";
+       }
+
        return s;
 }
 
@@ -284,13 +309,13 @@ Film::audio_analysis_path (shared_ptr<const Playlist> playlist) const
        auto p = dir ("analysis");
 
        Digester digester;
-       for (auto i: playlist->content ()) {
+       for (auto i: playlist->content()) {
                if (!i->audio) {
                        continue;
                }
 
-               digester.add (i->digest ());
-               digester.add (i->audio->mapping().digest ());
+               digester.add (i->digest());
+               digester.add (i->audio->mapping().digest());
                if (playlist->content().size() != 1) {
                        /* Analyses should be considered equal regardless of gain
                           if they were made from just one piece of content.  This
@@ -298,7 +323,14 @@ Film::audio_analysis_path (shared_ptr<const Playlist> playlist) const
                           analysis at the plotting stage rather than having to
                           recompute it.
                        */
-                       digester.add (i->audio->gain ());
+                       digester.add (i->audio->gain());
+
+                       /* Likewise we only care about position if we're looking at a
+                        * whole-project view.
+                        */
+                       digester.add (i->position().get());
+                       digester.add (i->trim_start().get());
+                       digester.add (i->trim_end().get());
                }
        }
 
@@ -430,6 +462,9 @@ Film::metadata (bool with_content_paths) const
        auto root = doc->create_root_node ("Metadata");
 
        root->add_child("Version")->add_child_text (raw_convert<string> (current_state_version));
+       auto last_write = root->add_child("LastWrittenBy");
+       last_write->add_child_text (dcpomatic_version);
+       last_write->set_attribute("git", dcpomatic_git_commit);
        root->add_child("Name")->add_child_text (_name);
        root->add_child("UseISDCFName")->add_child_text (_use_isdcf_name ? "1" : "0");
 
@@ -474,6 +509,9 @@ Film::metadata (bool with_content_paths) const
        if (_release_territory) {
                root->add_child("ReleaseTerritory")->add_child_text(_release_territory->subtag());
        }
+       if (_sign_language_video_language) {
+               root->add_child("SignLanguageVideoLanguage")->add_child_text(_sign_language_video_language->to_string());
+       }
        root->add_child("VersionNumber")->add_child_text(raw_convert<string>(_version_number));
        root->add_child("Status")->add_child_text(dcp::status_to_string(_status));
        if (_chain) {
@@ -498,6 +536,9 @@ Film::metadata (bool with_content_paths) const
        }
        root->add_child("UserExplicitContainer")->add_child_text(_user_explicit_container ? "1" : "0");
        root->add_child("UserExplicitResolution")->add_child_text(_user_explicit_resolution ? "1" : "0");
+       if (_audio_language) {
+               root->add_child("AudioLanguage")->add_child_text(_audio_language->to_string());
+       }
        _playlist->as_xml (root->add_child ("Playlist"), with_content_paths);
 
        return doc;
@@ -652,6 +693,11 @@ Film::read_metadata (optional<boost::filesystem::path> path)
                _release_territory = dcp::LanguageTag::RegionSubtag (*release_territory);
        }
 
+       auto sign_language_video_language = f.optional_string_child("SignLanguageVideoLanguage");
+       if (sign_language_video_language) {
+               _sign_language_video_language = dcp::LanguageTag(*sign_language_video_language);
+       }
+
        _version_number = f.optional_number_child<int>("VersionNumber").get_value_or(0);
 
        auto status = f.optional_string_child("Status");
@@ -678,6 +724,46 @@ Film::read_metadata (optional<boost::filesystem::path> path)
        _user_explicit_container = f.optional_bool_child("UserExplicitContainer").get_value_or(true);
        _user_explicit_resolution = f.optional_bool_child("UserExplicitResolution").get_value_or(true);
 
+       auto audio_language = f.optional_string_child("AudioLanguage");
+       if (audio_language) {
+               _audio_language = dcp::LanguageTag(*audio_language);
+       }
+
+       /* Read the old ISDCFMetadata tag from 2.14.x metadata */
+       auto isdcf = f.optional_node_child("ISDCFMetadata");
+       if (isdcf) {
+               if (auto territory = isdcf->optional_string_child("Territory")) {
+                       try {
+                               _release_territory = dcp::LanguageTag::RegionSubtag(*territory);
+                       } catch (...) {
+                               /* Invalid region subtag; just ignore it */
+                       }
+               }
+               if (auto audio_language = isdcf->optional_string_child("AudioLanguage")) {
+                       try {
+                               _audio_language = dcp::LanguageTag(*audio_language);
+                       } catch (...) {
+                               /* Invalid language tag; just ignore it */
+                       }
+               }
+               if (auto content_version = isdcf->optional_string_child("ContentVersion")) {
+                       _content_versions.push_back (*content_version);
+               }
+               if (auto rating = isdcf->optional_string_child("Rating")) {
+                       _ratings.push_back (dcp::Rating("", *rating));
+               }
+               if (auto mastered_luminance = isdcf->optional_number_child<float>("MasteredLuminance")) {
+                       _luminance = dcp::Luminance(*mastered_luminance, dcp::Luminance::Unit::FOOT_LAMBERT);
+               }
+               _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");
+               _chain = isdcf->optional_string_child("Chain");
+       }
+
        list<string> notes;
        _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"), _state_version, notes);
 
@@ -753,23 +839,6 @@ Film::mapped_audio_channels () const
 }
 
 
-vector<dcp::LanguageTag>
-Film::audio_languages () const
-{
-       vector<dcp::LanguageTag> result;
-       for (auto i: content()) {
-               if (i->audio && !i->audio->mapping().mapped_output_channels().empty() && i->audio->language()) {
-                       result.push_back (i->audio->language().get());
-               }
-       }
-
-       std::sort (result.begin(), result.end());
-       auto last = std::unique (result.begin(), result.end());
-       result.erase (last, result.end());
-       return result;
-}
-
-
 pair<optional<dcp::LanguageTag>, vector<dcp::LanguageTag>>
 Film::subtitle_languages () const
 {
@@ -904,23 +973,18 @@ Film::isdcf_name (bool if_created_now) const
 
        /* Interior aspect ratio.  The standard says we don't do this for trailers, for some strange reason */
        if (dcp_content_type() && dcp_content_type()->libdcp_kind() != dcp::ContentKind::TRAILER) {
-               Ratio const* content_ratio = nullptr;
-               for (auto i: content ()) {
-                       if (i->video) {
-                               /* Here's the first piece of video content */
-                               content_ratio = Ratio::nearest_from_ratio(i->video->scaled_size(frame_size()).ratio());
-                               break;
+               auto cl = content();
+               auto first_video = std::find_if(cl.begin(), cl.end(), [](shared_ptr<Content> c) { return static_cast<bool>(c->video); });
+               if (first_video != cl.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) {
+                               d += "-" + dcp::raw_convert<string>(first_ratio);
                        }
                }
-
-               if (content_ratio && content_ratio != container()) {
-                       /* This needs to be the numeric version of the ratio, and ::id() is close enough */
-                       d += "-" + content_ratio->id();
-               }
        }
 
-       auto audio_langs = audio_languages();
-       auto audio_language = (audio_langs.empty() || !audio_langs.front().language()) ? "XX" : audio_langs.front().language()->subtag();
+       auto audio_language = (_audio_language && _audio_language->language()) ? _audio_language->language()->subtag() : "XX";
 
        d += "_" + to_upper (audio_language);
 
@@ -1857,7 +1921,7 @@ void
 Film::use_template (string name)
 {
        _template_film.reset (new Film (optional<boost::filesystem::path>()));
-       _template_film->read_metadata (Config::instance()->template_path (name));
+       _template_film->read_metadata (Config::instance()->template_read_path(name));
        _use_isdcf_name = _template_film->_use_isdcf_name;
        _dcp_content_type = _template_film->_dcp_content_type;
        _container = _template_film->_container;
@@ -2144,3 +2208,26 @@ Film::set_two_d_version_of_three_d (bool t)
        _two_d_version_of_three_d = t;
 }
 
+
+void
+Film::set_audio_language (optional<dcp::LanguageTag> language)
+{
+       FilmChangeSignaller ch (this, Property::AUDIO_LANGUAGE);
+       _audio_language = language;
+}
+
+
+bool
+Film::has_sign_language_video_channel () const
+{
+       return _audio_channels >= static_cast<int>(dcp::Channel::SIGN_LANGUAGE);
+}
+
+
+void
+Film::set_sign_language_video_language (optional<dcp::LanguageTag> lang)
+{
+       FilmChangeSignaller ch (this, Property::SIGN_LANGUAGE_VIDEO_LANGUAGE);
+       _sign_language_video_language = lang;
+}
+