Use std::vector and emplace_back(), lengthen variable name.
[dcpomatic.git] / src / lib / film.cc
index 0bba13e2802c369504e973262b7140f0b306c8d4..d94ef0af31061c82e2e8f009deda3df1663e6b60 100644 (file)
@@ -114,6 +114,7 @@ using namespace dcpomatic;
 
 
 static constexpr char metadata_file[] = "metadata.xml";
+static constexpr char ui_state_file[] = "ui.xml";
 
 
 /* 5 -> 6
@@ -416,7 +417,7 @@ Film::metadata (bool with_content_paths) const
        root->add_child("UserExplicitVideoFrameRate")->add_child_text(_user_explicit_video_frame_rate ? "1" : "0");
        for (auto const& marker: _markers) {
                auto m = root->add_child("Marker");
-               m->set_attribute("Type", dcp::marker_to_string(marker.first));
+               m->set_attribute("type", dcp::marker_to_string(marker.first));
                m->add_child_text(raw_convert<string>(marker.second.get()));
        }
        for (auto i: _ratings) {
@@ -603,7 +604,11 @@ Film::read_metadata (optional<boost::filesystem::path> path)
        _user_explicit_video_frame_rate = f.optional_bool_child("UserExplicitVideoFrameRate").get_value_or(false);
 
        for (auto i: f.node_children("Marker")) {
-               _markers[dcp::marker_from_string(i->string_attribute("Type"))] = DCPTime(dcp::raw_convert<DCPTime::Type>(i->content()));
+               auto type = i->optional_string_attribute("Type");
+               if (!type) {
+                       type = i->string_attribute("type");
+               }
+               _markers[dcp::marker_from_string(*type)] = DCPTime(dcp::raw_convert<DCPTime::Type>(i->content()));
        }
 
        for (auto i: f.node_children("Rating")) {
@@ -943,10 +948,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);
+                               }
                        }
                }
        }
@@ -1435,13 +1442,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);
@@ -1449,7 +1456,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);
@@ -1647,9 +1654,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);
+                       }
                }
        }
 
@@ -1796,15 +1804,16 @@ Film::audio_analysis_finished ()
        /* XXX */
 }
 
-list<DCPTimePeriod>
+
+vector<DCPTimePeriod>
 Film::reels () const
 {
-       list<DCPTimePeriod> p;
+       vector<DCPTimePeriod> periods;
        auto const len = length();
 
        switch (reel_type ()) {
        case ReelType::SINGLE:
-               p.push_back (DCPTimePeriod (DCPTime (), len));
+               periods.emplace_back(DCPTime(), len);
                break;
        case ReelType::BY_VIDEO_CONTENT:
        {
@@ -1829,7 +1838,7 @@ Film::reels () const
                for (auto t: split_points) {
                        if (last && (t - *last) >= DCPTime::from_seconds(1)) {
                                /* Period from *last to t is long enough; use it and start a new one */
-                               p.push_back (DCPTimePeriod(*last, t));
+                               periods.emplace_back(*last, t);
                                last = t;
                        } else if (!last) {
                                /* That was the first time, so start a new period */
@@ -1837,8 +1846,8 @@ Film::reels () const
                        }
                }
 
-               if (!p.empty()) {
-                       p.back().to = split_points.back();
+               if (!periods.empty()) {
+                       periods.back().to = split_points.back();
                }
                break;
        }
@@ -1851,16 +1860,17 @@ Film::reels () const
                Frame const reel_in_frames = max(_reel_length / ((j2k_bandwidth() / video_frame_rate()) / 8), static_cast<Frame>(video_frame_rate()));
                while (current < len) {
                        DCPTime end = min (len, current + DCPTime::from_frames (reel_in_frames, video_frame_rate ()));
-                       p.push_back (DCPTimePeriod (current, end));
+                       periods.emplace_back(current, end);
                        current = end;
                }
                break;
        }
        }
 
-       return p;
+       return periods;
 }
 
+
 /** @param period A period within the DCP
  *  @return Name of the content which most contributes to the given period.
  */
@@ -2227,3 +2237,53 @@ Film::set_territory_type(TerritoryType type)
        _territory_type = type;
 }
 
+
+void
+Film::set_ui_state(string key, string value)
+{
+       _ui_state[key] = value;
+       write_ui_state();
+}
+
+
+boost::optional<std::string>
+Film::ui_state(string key) const
+{
+       auto iter = _ui_state.find(key);
+       if (iter == _ui_state.end()) {
+               return {};
+       }
+
+       return iter->second;
+}
+
+
+void
+Film::write_ui_state() const
+{
+       auto doc = make_shared<xmlpp::Document>();
+       auto root = doc->create_root_node("UI");
+
+       for (auto state: _ui_state) {
+               root->add_child(state.first)->add_child_text(state.second);
+       }
+
+       try {
+               doc->write_to_file_formatted(dcp::filesystem::fix_long_path(file(ui_state_file)).string());
+       } catch (...) {}
+}
+
+
+void
+Film::read_ui_state()
+{
+       try {
+               cxml::Document xml("UI");
+               xml.read_file(dcp::filesystem::fix_long_path(file(ui_state_file)));
+               for (auto node: xml.node_children()) {
+                       if (!node->is_text()) {
+                               _ui_state[node->name()] = node->content();
+                       }
+               }
+       } catch (...) {}
+}