WIP: stop using video directory and hard-linking (#2756).
[dcpomatic.git] / src / lib / film.cc
index d747efb0e1706de0122f50fe42fa92f3828be7cc..12902d14d5d9f8a8ccd34680057a1e11210fb57e 100644 (file)
@@ -36,7 +36,7 @@
 #include "cross.h"
 #include "dcp_content.h"
 #include "dcp_content_type.h"
-#include "dcp_encoder.h"
+#include "dcp_film_encoder.h"
 #include "dcpomatic_log.h"
 #include "digester.h"
 #include "environment_info.h"
@@ -57,6 +57,7 @@
 #include "text_content.h"
 #include "transcode_job.h"
 #include "upload_job.h"
+#include "variant.h"
 #include "video_content.h"
 #include "version.h"
 #include <libcxml/cxml.h>
@@ -115,6 +116,7 @@ using namespace dcpomatic;
 
 static constexpr char metadata_file[] = "metadata.xml";
 static constexpr char ui_state_file[] = "ui.xml";
+static constexpr char assets_file[] = "assets.xml";
 
 
 /* 5 -> 6
@@ -162,12 +164,12 @@ Film::Film (optional<boost::filesystem::path> dir)
        , _resolution (Resolution::TWO_K)
        , _encrypted (false)
        , _context_id (dcp::make_uuid ())
-       , _j2k_bandwidth (Config::instance()->default_j2k_bandwidth ())
        , _video_frame_rate (24)
        , _audio_channels (Config::instance()->default_dcp_audio_channels ())
        , _three_d (false)
        , _sequence (true)
        , _interop (Config::instance()->default_interop ())
+       , _video_encoding(VideoEncoding::JPEG2000)
        , _limit_to_smpte_bv20(false)
        , _audio_processor (0)
        , _reel_type (ReelType::SINGLE)
@@ -201,6 +203,10 @@ Film::Film (optional<boost::filesystem::path> dir)
                _studio = metadata["studio"];
        }
 
+       for (auto encoding: {VideoEncoding::JPEG2000, VideoEncoding::MPEG2}) {
+               _video_bit_rate[encoding] = Config::instance()->default_video_bit_rate(encoding);
+       }
+
        _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));
@@ -239,7 +245,7 @@ Film::video_identifier () const
                + "_" + resolution_to_string (_resolution)
                + "_" + _playlist->video_identifier()
                + "_" + raw_convert<string>(_video_frame_rate)
-               + "_" + raw_convert<string>(j2k_bandwidth());
+               + "_" + raw_convert<string>(video_bit_rate(video_encoding()));
 
        if (encrypted ()) {
                /* This is insecure but hey, the key is in plaintext in metadata.xml */
@@ -250,6 +256,9 @@ Film::video_identifier () const
 
        if (_interop) {
                s += "_I";
+               if (_video_encoding == VideoEncoding::MPEG2) {
+                       s += "_M";
+               }
        } else {
                s += "_S";
                if (_limit_to_smpte_bv20) {
@@ -280,17 +289,6 @@ Film::info_file (DCPTimePeriod period) const
        return file (p);
 }
 
-boost::filesystem::path
-Film::internal_video_asset_dir () const
-{
-       return dir ("video");
-}
-
-boost::filesystem::path
-Film::internal_video_asset_filename (DCPTimePeriod p) const
-{
-       return video_identifier() + "_" + raw_convert<string> (p.from.get()) + "_" + raw_convert<string> (p.to.get()) + ".mxf";
-}
 
 boost::filesystem::path
 Film::audio_analysis_path (shared_ptr<const Playlist> playlist) const
@@ -334,6 +332,13 @@ Film::audio_analysis_path (shared_ptr<const Playlist> playlist) const
 }
 
 
+boost::filesystem::path
+Film::assets_path() const
+{
+       return dir("assets");
+}
+
+
 boost::filesystem::path
 Film::subtitle_analysis_path (shared_ptr<const Content> content) const
 {
@@ -380,88 +385,93 @@ Film::metadata (bool with_content_paths) const
        auto doc = make_shared<xmlpp::Document>();
        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");
+       cxml::add_text_child(root, "Version", raw_convert<string>(current_state_version));
+       auto last_write = cxml::add_child(root, "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");
+       cxml::add_text_child(root, "Name", _name);
+       cxml::add_text_child(root, "UseISDCFName", _use_isdcf_name ? "1" : "0");
 
        if (_dcp_content_type) {
-               root->add_child("DCPContentType")->add_child_text (_dcp_content_type->isdcf_name ());
+               cxml::add_text_child(root, "DCPContentType", _dcp_content_type->isdcf_name());
        }
 
        if (_container) {
-               root->add_child("Container")->add_child_text (_container->id ());
-       }
-
-       root->add_child("Resolution")->add_child_text (resolution_to_string (_resolution));
-       root->add_child("J2KBandwidth")->add_child_text (raw_convert<string> (_j2k_bandwidth));
-       root->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
-       root->add_child("AudioFrameRate")->add_child_text(raw_convert<string>(_audio_frame_rate));
-       root->add_child("ISDCFDate")->add_child_text (boost::gregorian::to_iso_string (_isdcf_date));
-       root->add_child("AudioChannels")->add_child_text (raw_convert<string> (_audio_channels));
-       root->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
-       root->add_child("Sequence")->add_child_text (_sequence ? "1" : "0");
-       root->add_child("Interop")->add_child_text (_interop ? "1" : "0");
-       root->add_child("LimitToSMPTEBv20")->add_child_text(_limit_to_smpte_bv20 ? "1" : "0");
-       root->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
-       root->add_child("Key")->add_child_text (_key.hex ());
-       root->add_child("ContextID")->add_child_text (_context_id);
+               cxml::add_text_child(root, "Container", _container->id());
+       }
+
+       cxml::add_text_child(root, "Resolution", resolution_to_string(_resolution));
+       cxml::add_text_child(root, "J2KVideoBitRate", raw_convert<string>(_video_bit_rate[VideoEncoding::JPEG2000]));
+       cxml::add_text_child(root, "MPEG2VideoBitRate", raw_convert<string>(_video_bit_rate[VideoEncoding::MPEG2]));
+       cxml::add_text_child(root, "VideoFrameRate", raw_convert<string>(_video_frame_rate));
+       cxml::add_text_child(root, "AudioFrameRate", raw_convert<string>(_audio_frame_rate));
+       cxml::add_text_child(root, "ISDCFDate", boost::gregorian::to_iso_string(_isdcf_date));
+       cxml::add_text_child(root, "AudioChannels", raw_convert<string>(_audio_channels));
+       cxml::add_text_child(root, "ThreeD", _three_d ? "1" : "0");
+       cxml::add_text_child(root, "Sequence", _sequence ? "1" : "0");
+       cxml::add_text_child(root, "Interop", _interop ? "1" : "0");
+       cxml::add_text_child(root, "VideoEncoding", video_encoding_to_string(_video_encoding));
+       cxml::add_text_child(root, "LimitToSMPTEBv20", _limit_to_smpte_bv20 ? "1" : "0");
+       cxml::add_text_child(root, "Encrypted", _encrypted ? "1" : "0");
+       cxml::add_text_child(root, "Key", _key.hex ());
+       cxml::add_text_child(root, "ContextID", _context_id);
        if (_audio_processor) {
-               root->add_child("AudioProcessor")->add_child_text (_audio_processor->id ());
+               cxml::add_text_child(root, "AudioProcessor", _audio_processor->id());
+       }
+       cxml::add_text_child(root, "ReelType", raw_convert<string>(static_cast<int> (_reel_type)));
+       cxml::add_text_child(root, "ReelLength", raw_convert<string>(_reel_length));
+       for (auto boundary: _custom_reel_boundaries) {
+               cxml::add_text_child(root, "CustomReelBoundary", raw_convert<string>(boundary.get()));
        }
-       root->add_child("ReelType")->add_child_text (raw_convert<string> (static_cast<int> (_reel_type)));
-       root->add_child("ReelLength")->add_child_text (raw_convert<string> (_reel_length));
-       root->add_child("ReencodeJ2K")->add_child_text (_reencode_j2k ? "1" : "0");
-       root->add_child("UserExplicitVideoFrameRate")->add_child_text(_user_explicit_video_frame_rate ? "1" : "0");
+       cxml::add_text_child(root, "ReencodeJ2K", _reencode_j2k ? "1" : "0");
+       cxml::add_text_child(root, "UserExplicitVideoFrameRate", _user_explicit_video_frame_rate ? "1" : "0");
        for (auto const& marker: _markers) {
-               auto m = root->add_child("Marker");
+               auto m = cxml::add_child(root, "Marker");
                m->set_attribute("type", dcp::marker_to_string(marker.first));
                m->add_child_text(raw_convert<string>(marker.second.get()));
        }
        for (auto i: _ratings) {
-               i.as_xml (root->add_child("Rating"));
+               i.as_xml(cxml::add_child(root, "Rating"));
        }
        for (auto i: _content_versions) {
-               root->add_child("ContentVersion")->add_child_text(i);
+               cxml::add_text_child(root, "ContentVersion", 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));
+       cxml::add_text_child(root, "NameLanguage", _name_language.to_string());
+       cxml::add_text_child(root, "TerritoryType", territory_type_to_string(_territory_type));
        if (_release_territory) {
-               root->add_child("ReleaseTerritory")->add_child_text(_release_territory->subtag());
+               cxml::add_text_child(root, "ReleaseTerritory", _release_territory->subtag());
        }
        if (_sign_language_video_language) {
-               root->add_child("SignLanguageVideoLanguage")->add_child_text(_sign_language_video_language->to_string());
+               cxml::add_text_child(root, "SignLanguageVideoLanguage", _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));
+       cxml::add_text_child(root, "VersionNumber", raw_convert<string>(_version_number));
+       cxml::add_text_child(root, "Status", dcp::status_to_string(_status));
        if (_chain) {
-               root->add_child("Chain")->add_child_text(*_chain);
+               cxml::add_text_child(root, "Chain", *_chain);
        }
        if (_distributor) {
-               root->add_child("Distributor")->add_child_text(*_distributor);
+               cxml::add_text_child(root, "Distributor", *_distributor);
        }
        if (_facility) {
-               root->add_child("Facility")->add_child_text(*_facility);
+               cxml::add_text_child(root, "Facility", *_facility);
        }
        if (_studio) {
-               root->add_child("Studio")->add_child_text(*_studio);
+               cxml::add_text_child(root, "Studio", *_studio);
        }
-       root->add_child("TempVersion")->add_child_text(_temp_version ? "1" : "0");
-       root->add_child("PreRelease")->add_child_text(_pre_release ? "1" : "0");
-       root->add_child("RedBand")->add_child_text(_red_band ? "1" : "0");
-       root->add_child("TwoDVersionOfThreeD")->add_child_text(_two_d_version_of_three_d ? "1" : "0");
+       cxml::add_text_child(root, "TempVersion", _temp_version ? "1" : "0");
+       cxml::add_text_child(root, "PreRelease", _pre_release ? "1" : "0");
+       cxml::add_text_child(root, "RedBand", _red_band ? "1" : "0");
+       cxml::add_text_child(root, "TwoDVersionOfThreeD", _two_d_version_of_three_d ? "1" : "0");
        if (_luminance) {
-               root->add_child("LuminanceValue")->add_child_text(raw_convert<string>(_luminance->value()));
-               root->add_child("LuminanceUnit")->add_child_text(dcp::Luminance::unit_to_string(_luminance->unit()));
+               cxml::add_text_child(root, "LuminanceValue", raw_convert<string>(_luminance->value()));
+               cxml::add_text_child(root, "LuminanceUnit", dcp::Luminance::unit_to_string(_luminance->unit()));
        }
-       root->add_child("UserExplicitContainer")->add_child_text(_user_explicit_container ? "1" : "0");
-       root->add_child("UserExplicitResolution")->add_child_text(_user_explicit_resolution ? "1" : "0");
+       cxml::add_text_child(root, "UserExplicitContainer", _user_explicit_container ? "1" : "0");
+       cxml::add_text_child(root, "UserExplicitResolution", _user_explicit_resolution ? "1" : "0");
        if (_audio_language) {
-               root->add_child("AudioLanguage")->add_child_text(_audio_language->to_string());
+               cxml::add_text_child(root, "AudioLanguage", _audio_language->to_string());
        }
-       _playlist->as_xml (root->add_child ("Playlist"), with_content_paths);
+       _playlist->as_xml(cxml::add_child(root, "Playlist"), with_content_paths);
 
        return doc;
 }
@@ -504,7 +514,13 @@ Film::read_metadata (optional<boost::filesystem::path> path)
 {
        if (!path) {
                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!"));
+                       throw runtime_error(
+                               variant::insert_dcpomatic(
+                                       _("This film was created with an older version of %1, 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);
@@ -519,7 +535,7 @@ Film::read_metadata (optional<boost::filesystem::path> path)
 
        _state_version = f.number_child<int> ("Version");
        if (_state_version > current_state_version) {
-               throw runtime_error (_("This film was created with a newer version of DCP-o-matic, and it cannot be loaded into this version.  Sorry!"));
+               throw runtime_error(variant::insert_dcpomatic(_("This film was created with a newer version of %1, and it cannot be loaded into this version.  Sorry!")));
        } 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);
@@ -559,7 +575,12 @@ Film::read_metadata (optional<boost::filesystem::path> path)
        }
 
        _resolution = string_to_resolution (f.string_child ("Resolution"));
-       _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
+       if (auto j2k = f.optional_number_child<int>("J2KBandwidth")) {
+               _video_bit_rate[VideoEncoding::JPEG2000] = *j2k;
+       } else {
+               _video_bit_rate[VideoEncoding::JPEG2000] = f.number_child<int64_t>("J2KVideoBitRate");
+       }
+       _video_bit_rate[VideoEncoding::MPEG2] = f.optional_number_child<int64_t>("MPEG2VideoBitRate").get_value_or(Config::instance()->default_video_bit_rate(VideoEncoding::MPEG2));
        _video_frame_rate = f.number_child<int> ("VideoFrameRate");
        _audio_frame_rate = f.optional_number_child<int>("AudioFrameRate").get_value_or(48000);
        _encrypted = f.bool_child ("Encrypted");
@@ -581,6 +602,9 @@ Film::read_metadata (optional<boost::filesystem::path> path)
 
        _three_d = f.bool_child ("ThreeD");
        _interop = f.bool_child ("Interop");
+       if (auto encoding = f.optional_string_child("VideoEncoding")) {
+               _video_encoding = video_encoding_from_string(*encoding);
+       }
        _limit_to_smpte_bv20 = f.optional_bool_child("LimitToSMPTEBv20").get_value_or(false);
        _key = dcp::Key (f.string_child ("Key"));
        _context_id = f.optional_string_child("ContextID").get_value_or (dcp::make_uuid ());
@@ -600,6 +624,9 @@ Film::read_metadata (optional<boost::filesystem::path> path)
 
        _reel_type = static_cast<ReelType> (f.optional_number_child<int>("ReelType").get_value_or (static_cast<int>(ReelType::SINGLE)));
        _reel_length = f.optional_number_child<int64_t>("ReelLength").get_value_or (2000000000);
+       for (auto boundary: f.node_children("CustomReelBoundary")) {
+               _custom_reel_boundaries.push_back(DCPTime(raw_convert<int64_t>(boundary->content())));
+       }
        _reencode_j2k = f.optional_bool_child("ReencodeJ2K").get_value_or(false);
        _user_explicit_video_frame_rate = f.optional_bool_child("UserExplicitVideoFrameRate").get_value_or(false);
 
@@ -1156,10 +1183,10 @@ Film::set_resolution (Resolution r, bool explicit_user)
 
 
 void
-Film::set_j2k_bandwidth (int b)
+Film::set_video_bit_rate(VideoEncoding encoding, int64_t bit_rate)
 {
-       FilmChangeSignaller ch(this, FilmProperty::J2K_BANDWIDTH);
-       _j2k_bandwidth = b;
+       FilmChangeSignaller ch(this, FilmProperty::VIDEO_BIT_RATE);
+       _video_bit_rate[encoding] = bit_rate;
 }
 
 /** @param f New frame rate.
@@ -1202,6 +1229,15 @@ Film::set_interop (bool i)
 }
 
 
+void
+Film::set_video_encoding(VideoEncoding encoding)
+{
+       FilmChangeSignaller ch(this, FilmProperty::VIDEO_ENCODING);
+       _video_encoding = encoding;
+       check_settings_consistency();
+}
+
+
 void
 Film::set_limit_to_smpte_bv20(bool limit)
 {
@@ -1233,6 +1269,16 @@ Film::set_reel_length (int64_t r)
        _reel_length = r;
 }
 
+
+void
+Film::set_custom_reel_boundaries(vector<DCPTime> boundaries)
+{
+       FilmChangeSignaller ch(this, FilmProperty::CUSTOM_REEL_BOUNDARIES);
+       std::sort(boundaries.begin(), boundaries.end());
+       _custom_reel_boundaries = std::move(boundaries);
+}
+
+
 void
 Film::set_reencode_j2k (bool r)
 {
@@ -1566,7 +1612,7 @@ Film::check_settings_consistency ()
                        } else if (!atmos_rate && rate != video_frame_rate()) {
                                atmos_rate = rate;
                                set_video_frame_rate (rate, false);
-                               Message (_("DCP-o-matic had to change your settings so that the film's frame rate is the same as that of your Atmos content."));
+                               Message(variant::insert_dcpomatic(_("%1 had to change your settings so that the film's frame rate is the same as that of your Atmos content.")));
                        }
                }
        }
@@ -1598,7 +1644,40 @@ Film::check_settings_consistency ()
        }
 
        if (change_made) {
-               Message (_("DCP-o-matic had to change your settings for referring to DCPs as OV.  Please review those settings to make sure they are what you want."));
+               Message(variant::insert_dcpomatic(_("%1 had to change your settings for referring to DCPs as OV.  Please review those settings to make sure they are what you want.")));
+       }
+
+       if (reel_type() == ReelType::CUSTOM) {
+               auto boundaries = custom_reel_boundaries();
+               auto too_late = std::find_if(boundaries.begin(), boundaries.end(), [this](dcpomatic::DCPTime const& time) {
+                       return time >= length();
+               });
+
+               if (too_late != boundaries.end()) {
+                       if (std::distance(too_late, boundaries.end()) > 1) {
+                               Message(variant::insert_dcpomatic(_("%1 had to remove some of your custom reel boundaries as they no longer lie within the film.")));
+                       } else {
+                               Message(variant::insert_dcpomatic(_("%1 had to remove one of your custom reel boundaries as it no longer lies within the film.")));
+                       }
+                       boundaries.erase(too_late, boundaries.end());
+                       set_custom_reel_boundaries(boundaries);
+               }
+       }
+
+       auto const hd = Ratio::from_id("178");
+
+       if (video_encoding() == VideoEncoding::MPEG2) {
+               if (container() != hd) {
+                       set_container(hd);
+                       Message(_("DCP-o-matic had to set your container to 1920x1080 as it's the only one that can be used with MPEG2 encoding."));
+               }
+               if (three_d()) {
+                       set_three_d(false);
+                       Message(_("DCP-o-matic had to set your film to 2D as 3D is not yet supported with MPEG2 encoding."));
+               }
+       } else if (container() == hd && !Config::instance()->allow_any_container()) {
+               set_container(Ratio::from_id("185"));
+               Message(_("DCP-o-matic set your container to DCI Flat as it was previously 1920x1080 and that is not a standard ratio with JPEG2000 encoding."));
        }
 }
 
@@ -1725,7 +1804,7 @@ Film::make_kdm(boost::filesystem::path cpl_file, dcp::LocalTime from, dcp::Local
 uint64_t
 Film::required_disk_space () const
 {
-       return _playlist->required_disk_space (shared_from_this(), j2k_bandwidth(), audio_channels(), audio_frame_rate());
+       return _playlist->required_disk_space (shared_from_this(), video_bit_rate(video_encoding()), audio_channels(), audio_frame_rate());
 }
 
 /** This method checks the disk that the Film is on and tries to decide whether or not
@@ -1736,30 +1815,11 @@ Film::required_disk_space () const
  *  Note: the decision made by this method isn't, of course, 100% reliable.
  */
 bool
-Film::should_be_enough_disk_space (double& required, double& available, bool& can_hard_link) const
-{
-       /* Create a test file and see if we can hard-link it */
-       boost::filesystem::path test = internal_video_asset_dir() / "test";
-       boost::filesystem::path test2 = internal_video_asset_dir() / "test2";
-       can_hard_link = true;
-       dcp::File f(test, "w");
-       if (f) {
-               f.close();
-               boost::system::error_code ec;
-               dcp::filesystem::create_hard_link(test, test2, ec);
-               if (ec) {
-                       can_hard_link = false;
-               }
-               dcp::filesystem::remove(test);
-               dcp::filesystem::remove(test2);
-       }
-
-       auto s = dcp::filesystem::space(internal_video_asset_dir());
-       required = double (required_disk_space ()) / 1073741824.0f;
-       if (!can_hard_link) {
-               required *= 2;
-       }
-       available = double (s.available) / 1073741824.0f;
+Film::should_be_enough_disk_space(double& required, double& available) const
+{
+       DCPOMATIC_ASSERT(directory());
+       required = required_disk_space() / 1073741824.0f;
+       available = dcp::filesystem::space(*directory()).available / 1073741824.0f;
        return (available - required) > 1;
 }
 
@@ -1804,15 +1864,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:
        {
@@ -1837,7 +1898,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 */
@@ -1845,8 +1906,8 @@ Film::reels () const
                        }
                }
 
-               if (!p.empty()) {
-                       p.back().to = split_points.back();
+               if (!periods.empty()) {
+                       periods.back().to = split_points.back();
                }
                break;
        }
@@ -1856,19 +1917,32 @@ Film::reels () const
                /* Integer-divide reel length by the size of one frame to give the number of frames per reel,
                 * making sure we don't go less than 1s long.
                 */
-               Frame const reel_in_frames = max(_reel_length / ((j2k_bandwidth() / video_frame_rate()) / 8), static_cast<Frame>(video_frame_rate()));
+               Frame const reel_in_frames = max(_reel_length / ((video_bit_rate(video_encoding()) / 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;
        }
+       case ReelType::CUSTOM:
+       {
+               DCPTimePeriod current;
+               for (auto boundary: _custom_reel_boundaries) {
+                       current.to = boundary;
+                       periods.push_back(current);
+                       current.from = boundary;
+               }
+               current.to = len;
+               periods.push_back(current);
+               break;
+       }
        }
 
-       return p;
+       return periods;
 }
 
+
 /** @param period A period within the DCP
  *  @return Name of the content which most contributes to the given period.
  */
@@ -1887,7 +1961,9 @@ Film::use_template (string name)
        _dcp_content_type = _template_film->_dcp_content_type;
        _container = _template_film->_container;
        _resolution = _template_film->_resolution;
-       _j2k_bandwidth = _template_film->_j2k_bandwidth;
+       for (auto encoding: { VideoEncoding::JPEG2000, VideoEncoding::MPEG2 }) {
+               _video_bit_rate[encoding] = _template_film->_video_bit_rate[encoding];
+       }
        _video_frame_rate = _template_film->_video_frame_rate;
        _encrypted = _template_film->_encrypted;
        _audio_channels = _template_film->_audio_channels;
@@ -2263,7 +2339,7 @@ Film::write_ui_state() const
        auto root = doc->create_root_node("UI");
 
        for (auto state: _ui_state) {
-               root->add_child(state.first)->add_child_text(state.second);
+               cxml::add_text_child(root, state.first, state.second);
        }
 
        try {
@@ -2285,3 +2361,45 @@ Film::read_ui_state()
                }
        } catch (...) {}
 }
+
+
+vector<RememberedAsset>
+Film::read_remembered_assets() const
+{
+       vector<RememberedAsset> assets;
+
+       try {
+               cxml::Document xml("Assets");
+               xml.read_file(dcp::filesystem::fix_long_path(file(assets_file)));
+               for (auto node: xml.node_children("Asset")) {
+                       assets.push_back(RememberedAsset(node));
+               }
+       } catch (std::exception& e) {
+               LOG_ERROR("Could not read assets file %1 (%2)", file(assets_file), e.what());
+       } catch (...) {
+               LOG_ERROR("Could not read assets file %1", file(assets_file));
+       }
+
+       return assets;
+}
+
+
+void
+Film::write_remembered_assets(vector<RememberedAsset> const& assets) const
+{
+       auto doc = make_shared<xmlpp::Document>();
+       auto root = doc->create_root_node("Assets");
+
+       for (auto asset: assets) {
+               asset.as_xml(root->add_child("Asset"));
+       }
+
+       try {
+               doc->write_to_file_formatted(dcp::filesystem::fix_long_path(file(assets_file)).string());
+       } catch (std::exception& e) {
+               LOG_ERROR("Could not write assets file %1 (%2)", file(assets_file), e.what());
+       } catch (...) {
+               LOG_ERROR("Could not write assets file %1", file(assets_file));
+       }
+}
+