+ boost::mutex::scoped_lock lm (_state_mutex);
+
+ _external_audio.clear ();
+ _content_audio_streams.clear ();
+ _subtitle_streams.clear ();
+
+ boost::optional<int> version;
+
+ /* Backward compatibility things */
+ boost::optional<int> audio_sample_rate;
+ boost::optional<int> audio_stream_index;
+ boost::optional<int> subtitle_stream_index;
+
+ ifstream f (file ("metadata").c_str());
+ multimap<string, string> kv = read_key_value (f);
+
+ /* We need version before anything else */
+ multimap<string, string>::iterator v = kv.find ("version");
+ if (v != kv.end ()) {
+ version = atoi (v->second.c_str());
+ }
+
+ for (multimap<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
+ string const k = i->first;
+ string const v = i->second;
+
+ if (k == "audio_sample_rate") {
+ audio_sample_rate = atoi (v.c_str());
+ }
+
+ /* User-specified stuff */
+ if (k == "name") {
+ _name = v;
+ } else if (k == "use_dci_name") {
+ _use_dci_name = (v == "1");
+ } else if (k == "content") {
+ _content = v;
+ } else if (k == "dcp_content_type") {
+ _dcp_content_type = DCPContentType::from_pretty_name (v);
+ } else if (k == "format") {
+ _format = Format::from_metadata (v);
+ } else if (k == "left_crop") {
+ _crop.left = atoi (v.c_str ());
+ } else if (k == "right_crop") {
+ _crop.right = atoi (v.c_str ());
+ } else if (k == "top_crop") {
+ _crop.top = atoi (v.c_str ());
+ } else if (k == "bottom_crop") {
+ _crop.bottom = atoi (v.c_str ());
+ } else if (k == "filter") {
+ _filters.push_back (Filter::from_id (v));
+ } else if (k == "scaler") {
+ _scaler = Scaler::from_id (v);
+ } else if (k == "dcp_trim_start") {
+ _dcp_trim_start = atoi (v.c_str ());
+ } else if (k == "dcp_trim_end") {
+ _dcp_trim_end = atoi (v.c_str ());
+ } else if (k == "dcp_ab") {
+ _dcp_ab = (v == "1");
+ } else if (k == "selected_content_audio_stream" || (!version && k == "selected_audio_stream")) {
+ if (!version) {
+ audio_stream_index = atoi (v.c_str ());
+ } else {
+ _content_audio_stream = audio_stream_factory (v, version);
+ }
+ } else if (k == "external_audio") {
+ _external_audio.push_back (v);
+ } else if (k == "use_content_audio") {
+ _use_content_audio = (v == "1");
+ } else if (k == "audio_gain") {
+ _audio_gain = atof (v.c_str ());
+ } else if (k == "audio_delay") {
+ _audio_delay = atoi (v.c_str ());
+ } else if (k == "still_duration") {
+ _still_duration = atoi (v.c_str ());
+ } else if (k == "selected_subtitle_stream") {
+ if (!version) {
+ subtitle_stream_index = atoi (v.c_str ());
+ } else {
+ _subtitle_stream = subtitle_stream_factory (v, version);
+ }
+ } else if (k == "with_subtitles") {
+ _with_subtitles = (v == "1");
+ } else if (k == "subtitle_offset") {
+ _subtitle_offset = atoi (v.c_str ());
+ } else if (k == "subtitle_scale") {
+ _subtitle_scale = atof (v.c_str ());
+ } else if (k == "audio_language") {
+ _audio_language = v;
+ } else if (k == "subtitle_language") {
+ _subtitle_language = v;
+ } else if (k == "territory") {
+ _territory = v;
+ } else if (k == "rating") {
+ _rating = v;
+ } else if (k == "studio") {
+ _studio = v;
+ } else if (k == "facility") {
+ _facility = v;
+ } else if (k == "package_type") {
+ _package_type = v;
+ }
+
+ /* Cached stuff */
+ if (k == "width") {
+ _size.width = atoi (v.c_str ());
+ } else if (k == "height") {
+ _size.height = atoi (v.c_str ());
+ } else if (k == "length") {
+ int const vv = atoi (v.c_str ());
+ if (vv) {
+ _length = vv;
+ }
+ } else if (k == "content_digest") {
+ _content_digest = v;
+ } else if (k == "content_audio_stream" || (!version && k == "audio_stream")) {
+ _content_audio_streams.push_back (audio_stream_factory (v, version));
+ } else if (k == "external_audio_stream") {
+ _external_audio_stream = audio_stream_factory (v, version);
+ } else if (k == "subtitle_stream") {
+ _subtitle_streams.push_back (subtitle_stream_factory (v, version));
+ } else if (k == "frames_per_second") {
+ _frames_per_second = atof (v.c_str ());
+ }
+ }
+
+ if (!version) {
+ if (audio_sample_rate) {
+ /* version < 1 didn't specify sample rate in the audio streams, so fill it in here */
+ for (vector<shared_ptr<AudioStream> >::iterator i = _content_audio_streams.begin(); i != _content_audio_streams.end(); ++i) {
+ (*i)->set_sample_rate (audio_sample_rate.get());
+ }
+ }
+
+ /* also the selected stream was specified as an index */
+ if (audio_stream_index && audio_stream_index.get() >= 0 && audio_stream_index.get() < (int) _content_audio_streams.size()) {
+ _content_audio_stream = _content_audio_streams[audio_stream_index.get()];
+ }
+
+ /* similarly the subtitle */
+ if (subtitle_stream_index && subtitle_stream_index.get() >= 0 && subtitle_stream_index.get() < (int) _subtitle_streams.size()) {
+ _subtitle_stream = _subtitle_streams[subtitle_stream_index.get()];
+ }
+ }
+
+ _dirty = false;
+}
+
+Size
+Film::cropped_size (Size s) const
+{
+ boost::mutex::scoped_lock lm (_state_mutex);
+ s.width -= _crop.left + _crop.right;
+ s.height -= _crop.top + _crop.bottom;
+ return s;
+}
+
+/** Given a directory name, return its full path within the Film's directory.
+ * The directory (and its parents) will be created if they do not exist.
+ */
+string
+Film::dir (string d) const
+{
+ boost::mutex::scoped_lock lm (_directory_mutex);
+ boost::filesystem::path p;
+ p /= _directory;
+ p /= d;
+ boost::filesystem::create_directories (p);
+ return p.string ();
+}
+
+/** Given a file or directory name, return its full path within the Film's directory.
+ * _directory_mutex must not be locked on entry.
+ */
+string
+Film::file (string f) const
+{
+ boost::mutex::scoped_lock lm (_directory_mutex);
+ boost::filesystem::path p;
+ p /= _directory;
+ p /= f;
+ return p.string ();
+}
+
+/** @return full path of the content (actual video) file
+ * of the Film.
+ */
+string
+Film::content_path () const
+{
+ boost::mutex::scoped_lock lm (_state_mutex);
+ if (boost::filesystem::path(_content).has_root_directory ()) {
+ return _content;
+ }
+
+ return file (_content);
+}
+
+ContentType
+Film::content_type () const
+{
+ if (boost::filesystem::is_directory (_content)) {
+ /* Directory of images, we assume */
+ return VIDEO;
+ }
+
+ if (still_image_file (_content)) {
+ return STILL;
+ }
+
+ return VIDEO;
+}
+
+/** @return The sampling rate that we will resample the audio to */
+int
+Film::target_audio_sample_rate () const
+{
+ if (!audio_stream()) {
+ return 0;
+ }
+
+ /* Resample to a DCI-approved sample rate */
+ double t = dcp_audio_sample_rate (audio_stream()->sample_rate());
+
+ DCPFrameRate dfr = dcp_frame_rate (frames_per_second ());
+
+ /* Compensate for the fact that video will be rounded to the
+ nearest integer number of frames per second.
+ */
+ if (dfr.run_fast) {
+ t *= _frames_per_second * dfr.skip / dfr.frames_per_second;
+ }
+
+ return rint (t);
+}
+
+boost::optional<SourceFrame>
+Film::dcp_length () const
+{
+ if (!length()) {
+ return boost::optional<SourceFrame> ();
+ }
+
+ return length().get() - dcp_trim_start() - dcp_trim_end();
+}
+
+/** @return a DCI-compliant name for a DCP of this film */
+string
+Film::dci_name () const
+{
+ stringstream d;
+
+ string fixed_name = to_upper_copy (name());
+ for (size_t i = 0; i < fixed_name.length(); ++i) {
+ if (fixed_name[i] == ' ') {
+ fixed_name[i] = '-';
+ }
+ }
+
+ /* Spec is that the name part should be maximum 14 characters, as I understand it */
+ if (fixed_name.length() > 14) {
+ fixed_name = fixed_name.substr (0, 14);
+ }
+
+ d << fixed_name << "_";
+
+ if (dcp_content_type()) {
+ d << dcp_content_type()->dci_name() << "_";
+ }
+
+ if (format()) {
+ d << format()->dci_name() << "_";
+ }
+
+ if (!audio_language().empty ()) {
+ d << audio_language();
+ if (!subtitle_language().empty() && with_subtitles()) {
+ d << "-" << subtitle_language();
+ } else {
+ d << "-XX";
+ }
+
+ d << "_";
+ }
+
+ if (!territory().empty ()) {
+ d << territory();
+ if (!rating().empty ()) {
+ d << "-" << rating();
+ }
+ d << "_";
+ }
+
+ switch (audio_channels()) {
+ case 1:
+ d << "10_";
+ break;
+ case 2:
+ d << "20_";
+ break;
+ case 6:
+ d << "51_";
+ break;
+ case 8:
+ d << "71_";
+ break;
+ }
+
+ d << "2K_";
+
+ if (!studio().empty ()) {
+ d << studio() << "_";
+ }
+
+ d << boost::gregorian::to_iso_string (_dci_date) << "_";
+
+ if (!facility().empty ()) {
+ d << facility() << "_";
+ }
+
+ if (!package_type().empty ()) {
+ d << package_type();
+ }
+
+ return d.str ();