+
+list<DCPTimePeriod>
+Film::reels () const
+{
+ list<DCPTimePeriod> p;
+ DCPTime const len = length();
+
+ switch (reel_type ()) {
+ case REELTYPE_SINGLE:
+ p.push_back (DCPTimePeriod (DCPTime (), len));
+ break;
+ case REELTYPE_BY_VIDEO_CONTENT:
+ {
+ /* Collect all reel boundaries */
+ list<DCPTime> split_points;
+ split_points.push_back (DCPTime());
+ split_points.push_back (len);
+ BOOST_FOREACH (shared_ptr<Content> c, content()) {
+ if (c->video) {
+ BOOST_FOREACH (DCPTime t, c->reel_split_points(shared_from_this())) {
+ split_points.push_back (t);
+ }
+ split_points.push_back (c->end(shared_from_this()));
+ }
+ }
+
+ split_points.sort ();
+ split_points.unique ();
+
+ /* Make them into periods, coalescing any that are less than 1 second long */
+ optional<DCPTime> last;
+ BOOST_FOREACH (DCPTime 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));
+ last = t;
+ } else if (!last) {
+ /* That was the first time, so start a new period */
+ last = t;
+ }
+ }
+ break;
+ }
+ case REELTYPE_BY_LENGTH:
+ {
+ DCPTime current;
+ /* 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()));
+ while (current < len) {
+ DCPTime end = min (len, current + DCPTime::from_frames (reel_in_frames, video_frame_rate ()));
+ p.push_back (DCPTimePeriod (current, end));
+ current = end;
+ }
+ break;
+ }
+ }
+
+ return p;
+}
+
+/** @param period A period within the DCP
+ * @return Name of the content which most contributes to the given period.
+ */
+string
+Film::content_summary (DCPTimePeriod period) const
+{
+ return _playlist->content_summary (shared_from_this(), period);
+}
+
+void
+Film::use_template (string name)
+{
+ _template_film.reset (new Film (optional<boost::filesystem::path>()));
+ _template_film->read_metadata (Config::instance()->template_path (name));
+ _use_isdcf_name = _template_film->_use_isdcf_name;
+ _dcp_content_type = _template_film->_dcp_content_type;
+ _container = _template_film->_container;
+ _resolution = _template_film->_resolution;
+ _j2k_bandwidth = _template_film->_j2k_bandwidth;
+ _video_frame_rate = _template_film->_video_frame_rate;
+ _encrypted = _template_film->_encrypted;
+ _audio_channels = _template_film->_audio_channels;
+ _sequence = _template_film->_sequence;
+ _three_d = _template_film->_three_d;
+ _interop = _template_film->_interop;
+ _audio_processor = _template_film->_audio_processor;
+ _reel_type = _template_film->_reel_type;
+ _reel_length = _template_film->_reel_length;
+ _isdcf_metadata = _template_film->_isdcf_metadata;
+}
+
+pair<double, double>
+Film::speed_up_range (int dcp_frame_rate) const
+{
+ return _playlist->speed_up_range (dcp_frame_rate);
+}
+
+void
+Film::copy_from (shared_ptr<const Film> film)
+{
+ read_metadata (film->file (metadata_file));
+}
+
+bool
+Film::references_dcp_video () const
+{
+ BOOST_FOREACH (shared_ptr<Content> i, _playlist->content()) {
+ shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent>(i);
+ if (d && d->reference_video()) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool
+Film::references_dcp_audio () const
+{
+ BOOST_FOREACH (shared_ptr<Content> i, _playlist->content()) {
+ shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent>(i);
+ if (d && d->reference_audio()) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+bool
+Film::contains_atmos_content () const
+{
+ BOOST_FOREACH (shared_ptr<Content> i, _playlist->content()) {
+ if (i->atmos) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+list<DCPTextTrack>
+Film::closed_caption_tracks () const
+{
+ list<DCPTextTrack> tt;
+ BOOST_FOREACH (shared_ptr<Content> i, content()) {
+ BOOST_FOREACH (shared_ptr<TextContent> j, i->text) {
+ /* XXX: Empty DCPTextTrack ends up being a magic value here - the "unknown" or "not specified" track */
+ DCPTextTrack dtt = j->dcp_track().get_value_or(DCPTextTrack());
+ if (j->type() == TEXT_CLOSED_CAPTION && find(tt.begin(), tt.end(), dtt) == tt.end()) {
+ tt.push_back (dtt);
+ }
+ }
+ }
+
+ return tt;
+}
+
+void
+Film::set_marker (dcp::Marker type, DCPTime time)
+{
+ ChangeSignaller<Film> ch (this, MARKERS);
+ _markers[type] = time;
+}
+
+void
+Film::unset_marker (dcp::Marker type)
+{
+ ChangeSignaller<Film> ch (this, MARKERS);
+ _markers.erase (type);
+}
+
+
+void
+Film::clear_markers ()
+{
+ ChangeSignaller<Film> ch (this, MARKERS);
+ _markers.clear ();
+}
+
+
+void
+Film::set_ratings (vector<dcp::Rating> r)
+{
+ ChangeSignaller<Film> ch (this, RATINGS);
+ _ratings = r;
+}
+
+void
+Film::set_content_versions (vector<string> v)
+{
+ ChangeSignaller<Film> ch (this, CONTENT_VERSIONS);
+ _content_versions = v;
+}
+
+
+void
+Film::set_name_language (dcp::LanguageTag lang)
+{
+ ChangeSignaller<Film> ch (this, NAME_LANGUAGE);
+ _name_language = lang;
+}
+
+
+void
+Film::set_audio_language (dcp::LanguageTag lang)
+{
+ ChangeSignaller<Film> ch (this, AUDIO_LANGUAGE);
+ _audio_language = lang;
+}
+
+
+void
+Film::set_release_territory (dcp::LanguageTag::RegionSubtag region)
+{
+ ChangeSignaller<Film> ch (this, RELEASE_TERRITORY);
+ _release_territory = region;
+}
+
+
+void
+Film::set_status (dcp::Status s)
+{
+ ChangeSignaller<Film> ch (this, STATUS);
+ _status = s;
+}
+
+
+void
+Film::set_version_number (int v)
+{
+ ChangeSignaller<Film> ch (this, VERSION_NUMBER);
+ _version_number = v;
+}
+
+
+void
+Film::set_chain (string c)
+{
+ ChangeSignaller<Film> ch (this, CHAIN);
+ _chain = c;
+}
+
+
+void
+Film::set_distributor (string d)
+{
+ ChangeSignaller<Film> ch (this, DISTRIBUTOR);
+ _distributor = d;
+}
+
+
+void
+Film::set_luminance (dcp::Luminance l)
+{
+ ChangeSignaller<Film> ch (this, LUMINANCE);
+ _luminance = l;
+}
+
+
+void
+Film::set_facility (string f)
+{
+ ChangeSignaller<Film> ch (this, FACILITY);
+ _facility = f;
+}
+
+
+optional<DCPTime>
+Film::marker (dcp::Marker type) const
+{
+ map<dcp::Marker, DCPTime>::const_iterator i = _markers.find (type);
+ if (i == _markers.end()) {
+ return optional<DCPTime>();
+ }
+ return i->second;
+}
+
+shared_ptr<InfoFileHandle>
+Film::info_file_handle (DCPTimePeriod period, bool read) const
+{
+ return shared_ptr<InfoFileHandle> (new InfoFileHandle(_info_file_mutex, info_file(period), read));
+}
+
+InfoFileHandle::InfoFileHandle (boost::mutex& mutex, boost::filesystem::path file, bool read)
+ : _lock (mutex)
+ , _file (file)
+{
+ if (read) {
+ _handle = fopen_boost (file, "rb");
+ if (!_handle) {
+ throw OpenFileError (file, errno, OpenFileError::READ);
+ }
+ } else {
+ bool const exists = boost::filesystem::exists (file);
+ if (exists) {
+ _handle = fopen_boost (file, "r+b");
+ } else {
+ _handle = fopen_boost (file, "wb");
+ }
+
+ if (!_handle) {
+ throw OpenFileError (file, errno, exists ? OpenFileError::READ_WRITE : OpenFileError::WRITE);
+ }
+ }
+}
+
+InfoFileHandle::~InfoFileHandle ()
+{
+ fclose (_handle);
+}