2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/date_time.hpp>
36 #include "transcoder.h"
38 #include "job_manager.h"
39 #include "ab_transcode_job.h"
40 #include "transcode_job.h"
41 #include "scp_dcp_job.h"
44 #include "exceptions.h"
45 #include "examine_content_job.h"
47 #include "decoder_factory.h"
50 #include "ui_signaller.h"
51 #include "video_decoder.h"
52 #include "audio_decoder.h"
53 #include "sndfile_decoder.h"
54 #include "analyse_audio_job.h"
60 using std::stringstream;
72 using boost::shared_ptr;
73 using boost::lexical_cast;
74 using boost::to_upper_copy;
75 using boost::ends_with;
76 using boost::starts_with;
77 using boost::optional;
80 int const Film::state_version = 4;
82 /** Construct a Film object in a given directory, reading any metadata
83 * file that exists in that directory. An exception will be thrown if
84 * must_exist is true and the specified directory does not exist.
86 * @param d Film directory.
87 * @param must_exist true to throw an exception if does not exist.
90 Film::Film (string d, bool must_exist)
91 : _use_dci_name (true)
92 , _trust_content_header (true)
93 , _dcp_content_type (Config::instance()->default_dcp_content_type ())
94 , _format (Config::instance()->default_format ())
95 , _scaler (Scaler::from_id ("bicubic"))
100 , _use_content_audio (true)
103 , _still_duration (10)
104 , _with_subtitles (false)
105 , _subtitle_offset (0)
106 , _subtitle_scale (1)
108 , _j2k_bandwidth (200000000)
109 , _dci_metadata (Config::instance()->default_dci_metadata ())
110 , _dcp_frame_rate (0)
111 , _source_frame_rate (0)
114 set_dci_date_today ();
116 /* Make state.directory a complete path without ..s (where possible)
117 (Code swiped from Adam Bowen on stackoverflow)
120 boost::filesystem::path p (boost::filesystem::system_complete (d));
121 boost::filesystem::path result;
122 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
124 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
127 result = result.parent_path ();
129 } else if (*i != ".") {
134 set_directory (result.string ());
136 if (!boost::filesystem::exists (directory())) {
138 throw OpenFileError (directory());
140 boost::filesystem::create_directory (directory());
144 _sndfile_stream = SndfileStream::create ();
152 _log.reset (new FileLog (file ("log")));
155 Film::Film (Film const & o)
156 : boost::enable_shared_from_this<Film> (o)
157 /* note: the copied film shares the original's log */
159 , _directory (o._directory)
161 , _use_dci_name (o._use_dci_name)
162 , _content (o._content)
163 , _trust_content_header (o._trust_content_header)
164 , _dcp_content_type (o._dcp_content_type)
165 , _format (o._format)
167 , _filters (o._filters)
168 , _scaler (o._scaler)
169 , _trim_start (o._trim_start)
170 , _trim_end (o._trim_end)
171 , _trim_type (o._trim_type)
172 , _dcp_ab (o._dcp_ab)
173 , _content_audio_stream (o._content_audio_stream)
174 , _external_audio (o._external_audio)
175 , _use_content_audio (o._use_content_audio)
176 , _audio_gain (o._audio_gain)
177 , _audio_delay (o._audio_delay)
178 , _still_duration (o._still_duration)
179 , _subtitle_stream (o._subtitle_stream)
180 , _with_subtitles (o._with_subtitles)
181 , _subtitle_offset (o._subtitle_offset)
182 , _subtitle_scale (o._subtitle_scale)
183 , _colour_lut (o._colour_lut)
184 , _j2k_bandwidth (o._j2k_bandwidth)
185 , _dci_metadata (o._dci_metadata)
186 , _dci_date (o._dci_date)
187 , _dcp_frame_rate (o._dcp_frame_rate)
189 , _length (o._length)
190 , _content_digest (o._content_digest)
191 , _content_audio_streams (o._content_audio_streams)
192 , _sndfile_stream (o._sndfile_stream)
193 , _subtitle_streams (o._subtitle_streams)
194 , _source_frame_rate (o._source_frame_rate)
206 Film::video_state_identifier () const
211 pair<string, string> f = Filter::ffmpeg_strings (filters());
215 << "_" << content_digest()
216 << "_" << crop().left << "_" << crop().right << "_" << crop().top << "_" << crop().bottom
217 << "_" << _dcp_frame_rate
218 << "_" << f.first << "_" << f.second
219 << "_" << scaler()->id()
220 << "_" << j2k_bandwidth()
221 << "_" << boost::lexical_cast<int> (colour_lut());
223 if (trim_type() == ENCODE) {
224 s << "_" << trim_start() << "_" << trim_end();
228 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
229 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
235 /** @return The path to the directory to write video frame info files to */
237 Film::info_dir () const
239 boost::filesystem::path p;
241 p /= video_state_identifier ();
242 return dir (p.string());
246 Film::internal_video_mxf_dir () const
248 boost::filesystem::path p;
249 return dir ("video");
253 Film::internal_video_mxf_filename () const
255 return video_state_identifier() + ".mxf";
259 Film::dcp_video_mxf_filename () const
261 return filename_safe_name() + "_video.mxf";
265 Film::dcp_audio_mxf_filename () const
267 return filename_safe_name() + "_audio.mxf";
271 Film::filename_safe_name () const
273 string const n = name ();
275 for (size_t i = 0; i < n.length(); ++i) {
276 if (isalnum (n[i])) {
287 Film::audio_analysis_path () const
289 boost::filesystem::path p;
291 p /= content_digest();
292 return file (p.string ());
295 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
299 set_dci_date_today ();
301 if (dcp_name().find ("/") != string::npos) {
302 throw BadSettingError (_("name"), _("cannot contain slashes"));
305 log()->log (String::compose ("DVD-o-matic %1 git %2 using %3", dvdomatic_version, dvdomatic_git_commit, dependency_version_summary()));
309 gethostname (buffer, sizeof (buffer));
310 log()->log (String::compose ("Starting to make DCP on %1", buffer));
313 log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
315 log()->log (String::compose ("Content length %1", length().get()));
317 log()->log (String::compose ("Content digest %1", content_digest()));
318 log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
319 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
320 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
321 #ifdef DVDOMATIC_DEBUG
322 log()->log ("DVD-o-matic built in debug mode.");
324 log()->log ("DVD-o-matic built in optimised mode.");
327 log()->log ("libdcp built in debug mode.");
329 log()->log ("libdcp built in optimised mode.");
331 pair<string, int> const c = cpu_info ();
332 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
333 list<pair<string, string> > const m = mount_info ();
334 for (list<pair<string, string> >::const_iterator i = m.begin(); i != m.end(); ++i) {
335 log()->log (String::compose ("Mount: %1 %2", i->first, i->second));
339 throw MissingSettingError (_("format"));
342 if (content().empty ()) {
343 throw MissingSettingError (_("content"));
346 if (dcp_content_type() == 0) {
347 throw MissingSettingError (_("content type"));
350 if (name().empty()) {
351 throw MissingSettingError (_("name"));
355 od.decode_subtitles = with_subtitles ();
360 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this(), od)));
362 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this(), od)));
366 /** Start a job to analyse the audio of our content file */
368 Film::analyse_audio ()
370 if (_analyse_audio_job) {
374 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
375 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
376 JobManager::instance()->add (_analyse_audio_job);
379 /** Start a job to examine our content file */
381 Film::examine_content ()
383 if (_examine_content_job) {
387 _examine_content_job.reset (new ExamineContentJob (shared_from_this()));
388 _examine_content_job->Finished.connect (bind (&Film::examine_content_finished, this));
389 JobManager::instance()->add (_examine_content_job);
393 Film::analyse_audio_finished ()
397 if (_analyse_audio_job->finished_ok ()) {
398 AudioAnalysisSucceeded ();
401 _analyse_audio_job.reset ();
405 Film::examine_content_finished ()
407 _examine_content_job.reset ();
410 /** Start a job to send our DCP to the configured TMS */
412 Film::send_dcp_to_tms ()
414 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
415 JobManager::instance()->add (j);
418 /** Count the number of frames that have been encoded for this film.
419 * @return frame count.
422 Film::encoded_frames () const
429 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
431 boost::this_thread::interruption_point ();
437 /** Write state to our `metadata' file */
439 Film::write_metadata () const
441 boost::mutex::scoped_lock lm (_state_mutex);
444 boost::filesystem::create_directories (directory());
446 string const m = file ("metadata");
447 ofstream f (m.c_str ());
449 throw CreateFileError (m);
452 f << "version " << state_version << endl;
455 f << "name " << _name << endl;
456 f << "use_dci_name " << _use_dci_name << endl;
457 f << "content " << _content << endl;
458 f << "trust_content_header " << (_trust_content_header ? "1" : "0") << endl;
459 if (_dcp_content_type) {
460 f << "dcp_content_type " << _dcp_content_type->dci_name () << endl;
463 f << "format " << _format->as_metadata () << endl;
465 f << "left_crop " << _crop.left << endl;
466 f << "right_crop " << _crop.right << endl;
467 f << "top_crop " << _crop.top << endl;
468 f << "bottom_crop " << _crop.bottom << endl;
469 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
470 f << "filter " << (*i)->id () << endl;
472 f << "scaler " << _scaler->id () << endl;
473 f << "trim_start " << _trim_start << endl;
474 f << "trim_end " << _trim_end << endl;
475 switch (_trim_type) {
477 f << "trim_type cpl\n";
480 f << "trim_type encode\n";
483 f << "dcp_ab " << (_dcp_ab ? "1" : "0") << endl;
484 if (_content_audio_stream) {
485 f << "selected_content_audio_stream " << _content_audio_stream->to_string() << endl;
487 for (vector<string>::const_iterator i = _external_audio.begin(); i != _external_audio.end(); ++i) {
488 f << "external_audio " << *i << endl;
490 f << "use_content_audio " << (_use_content_audio ? "1" : "0") << endl;
491 f << "audio_gain " << _audio_gain << endl;
492 f << "audio_delay " << _audio_delay << endl;
493 f << "still_duration " << _still_duration << endl;
494 if (_subtitle_stream) {
495 f << "selected_subtitle_stream " << _subtitle_stream->to_string() << endl;
497 f << "with_subtitles " << _with_subtitles << endl;
498 f << "subtitle_offset " << _subtitle_offset << endl;
499 f << "subtitle_scale " << _subtitle_scale << endl;
500 f << "colour_lut " << _colour_lut << endl;
501 f << "j2k_bandwidth " << _j2k_bandwidth << endl;
502 _dci_metadata.write (f);
503 f << "dci_date " << boost::gregorian::to_iso_string (_dci_date) << endl;
504 f << "dcp_frame_rate " << _dcp_frame_rate << endl;
505 f << "width " << _size.width << endl;
506 f << "height " << _size.height << endl;
507 f << "length " << _length.get_value_or(0) << endl;
508 f << "content_digest " << _content_digest << endl;
510 for (vector<shared_ptr<AudioStream> >::const_iterator i = _content_audio_streams.begin(); i != _content_audio_streams.end(); ++i) {
511 f << "content_audio_stream " << (*i)->to_string () << endl;
514 f << "external_audio_stream " << _sndfile_stream->to_string() << endl;
516 for (vector<shared_ptr<SubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
517 f << "subtitle_stream " << (*i)->to_string () << endl;
520 f << "source_frame_rate " << _source_frame_rate << endl;
525 /** Read state from our metadata file */
527 Film::read_metadata ()
529 boost::mutex::scoped_lock lm (_state_mutex);
532 _external_audio.clear ();
533 _content_audio_streams.clear ();
534 _subtitle_streams.clear ();
536 boost::optional<int> version;
538 /* Backward compatibility things */
539 boost::optional<int> audio_sample_rate;
540 boost::optional<int> audio_stream_index;
541 boost::optional<int> subtitle_stream_index;
543 ifstream f (file ("metadata").c_str());
545 throw OpenFileError (file ("metadata"));
548 multimap<string, string> kv = read_key_value (f);
550 /* We need version before anything else */
551 multimap<string, string>::iterator v = kv.find ("version");
552 if (v != kv.end ()) {
553 version = atoi (v->second.c_str());
556 for (multimap<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
557 string const k = i->first;
558 string const v = i->second;
560 if (k == "audio_sample_rate") {
561 audio_sample_rate = atoi (v.c_str());
564 /* User-specified stuff */
567 } else if (k == "use_dci_name") {
568 _use_dci_name = (v == "1");
569 } else if (k == "content") {
571 } else if (k == "trust_content_header") {
572 _trust_content_header = (v == "1");
573 } else if (k == "dcp_content_type") {
575 _dcp_content_type = DCPContentType::from_pretty_name (v);
577 _dcp_content_type = DCPContentType::from_dci_name (v);
579 } else if (k == "format") {
580 _format = Format::from_metadata (v);
581 } else if (k == "left_crop") {
582 _crop.left = atoi (v.c_str ());
583 } else if (k == "right_crop") {
584 _crop.right = atoi (v.c_str ());
585 } else if (k == "top_crop") {
586 _crop.top = atoi (v.c_str ());
587 } else if (k == "bottom_crop") {
588 _crop.bottom = atoi (v.c_str ());
589 } else if (k == "filter") {
590 _filters.push_back (Filter::from_id (v));
591 } else if (k == "scaler") {
592 _scaler = Scaler::from_id (v);
593 } else if ( ((!version || version < 2) && k == "dcp_trim_start") || k == "trim_start") {
594 _trim_start = atoi (v.c_str ());
595 } else if ( ((!version || version < 2) && k == "dcp_trim_end") || k == "trim_end") {
596 _trim_end = atoi (v.c_str ());
597 } else if (k == "trim_type") {
600 } else if (v == "encode") {
603 } else if (k == "dcp_ab") {
604 _dcp_ab = (v == "1");
605 } else if (k == "selected_content_audio_stream" || (!version && k == "selected_audio_stream")) {
607 audio_stream_index = atoi (v.c_str ());
609 _content_audio_stream = audio_stream_factory (v, version);
611 } else if (k == "external_audio") {
612 _external_audio.push_back (v);
613 } else if (k == "use_content_audio") {
614 _use_content_audio = (v == "1");
615 } else if (k == "audio_gain") {
616 _audio_gain = atof (v.c_str ());
617 } else if (k == "audio_delay") {
618 _audio_delay = atoi (v.c_str ());
619 } else if (k == "still_duration") {
620 _still_duration = atoi (v.c_str ());
621 } else if (k == "selected_subtitle_stream") {
623 subtitle_stream_index = atoi (v.c_str ());
625 _subtitle_stream = subtitle_stream_factory (v, version);
627 } else if (k == "with_subtitles") {
628 _with_subtitles = (v == "1");
629 } else if (k == "subtitle_offset") {
630 _subtitle_offset = atoi (v.c_str ());
631 } else if (k == "subtitle_scale") {
632 _subtitle_scale = atof (v.c_str ());
633 } else if (k == "colour_lut") {
634 _colour_lut = atoi (v.c_str ());
635 } else if (k == "j2k_bandwidth") {
636 _j2k_bandwidth = atoi (v.c_str ());
637 } else if (k == "dci_date") {
638 _dci_date = boost::gregorian::from_undelimited_string (v);
639 } else if (k == "dcp_frame_rate") {
640 _dcp_frame_rate = atoi (v.c_str ());
643 _dci_metadata.read (k, v);
647 _size.width = atoi (v.c_str ());
648 } else if (k == "height") {
649 _size.height = atoi (v.c_str ());
650 } else if (k == "length") {
651 int const vv = atoi (v.c_str ());
655 } else if (k == "content_digest") {
657 } else if (k == "content_audio_stream" || (!version && k == "audio_stream")) {
658 _content_audio_streams.push_back (audio_stream_factory (v, version));
659 } else if (k == "external_audio_stream") {
660 _sndfile_stream = audio_stream_factory (v, version);
661 } else if (k == "subtitle_stream") {
662 _subtitle_streams.push_back (subtitle_stream_factory (v, version));
663 } else if (k == "source_frame_rate") {
664 _source_frame_rate = atof (v.c_str ());
665 } else if (version < 4 && k == "frames_per_second") {
666 _source_frame_rate = atof (v.c_str ());
667 /* Fill in what would have been used for DCP frame rate by the older version */
668 _dcp_frame_rate = best_dcp_frame_rate (_source_frame_rate);
673 if (audio_sample_rate) {
674 /* version < 1 didn't specify sample rate in the audio streams, so fill it in here */
675 for (vector<shared_ptr<AudioStream> >::iterator i = _content_audio_streams.begin(); i != _content_audio_streams.end(); ++i) {
676 (*i)->set_sample_rate (audio_sample_rate.get());
680 /* also the selected stream was specified as an index */
681 if (audio_stream_index && audio_stream_index.get() >= 0 && audio_stream_index.get() < (int) _content_audio_streams.size()) {
682 _content_audio_stream = _content_audio_streams[audio_stream_index.get()];
685 /* similarly the subtitle */
686 if (subtitle_stream_index && subtitle_stream_index.get() >= 0 && subtitle_stream_index.get() < (int) _subtitle_streams.size()) {
687 _subtitle_stream = _subtitle_streams[subtitle_stream_index.get()];
695 Film::cropped_size (libdcp::Size s) const
697 boost::mutex::scoped_lock lm (_state_mutex);
698 s.width -= _crop.left + _crop.right;
699 s.height -= _crop.top + _crop.bottom;
703 /** Given a directory name, return its full path within the Film's directory.
704 * The directory (and its parents) will be created if they do not exist.
707 Film::dir (string d) const
709 boost::mutex::scoped_lock lm (_directory_mutex);
711 boost::filesystem::path p;
715 boost::filesystem::create_directories (p);
720 /** Given a file or directory name, return its full path within the Film's directory.
721 * _directory_mutex must not be locked on entry.
722 * Any required parent directories will be created.
725 Film::file (string f) const
727 boost::mutex::scoped_lock lm (_directory_mutex);
729 boost::filesystem::path p;
733 boost::filesystem::create_directories (p.parent_path ());
738 /** @return full path of the content (actual video) file
742 Film::content_path () const
744 boost::mutex::scoped_lock lm (_state_mutex);
745 if (boost::filesystem::path(_content).has_root_directory ()) {
749 return file (_content);
753 Film::content_type () const
755 if (boost::filesystem::is_directory (_content)) {
756 /* Directory of images, we assume */
760 if (still_image_file (_content)) {
767 /** @return The sampling rate that we will resample the audio to */
769 Film::target_audio_sample_rate () const
771 if (!audio_stream()) {
775 /* Resample to a DCI-approved sample rate */
776 double t = dcp_audio_sample_rate (audio_stream()->sample_rate());
778 FrameRateConversion frc (source_frame_rate(), dcp_frame_rate());
780 /* Compensate if the DCP is being run at a different frame rate
781 to the source; that is, if the video is run such that it will
782 look different in the DCP compared to the source (slower or faster).
783 skip/repeat doesn't come into effect here.
786 if (frc.change_speed) {
787 t *= source_frame_rate() * frc.factor() / dcp_frame_rate();
794 Film::still_duration_in_frames () const
796 return still_duration() * source_frame_rate();
799 /** @return a DCI-compliant name for a DCP of this film */
801 Film::dci_name (bool if_created_now) const
805 string fixed_name = to_upper_copy (name());
806 for (size_t i = 0; i < fixed_name.length(); ++i) {
807 if (fixed_name[i] == ' ') {
812 /* Spec is that the name part should be maximum 14 characters, as I understand it */
813 if (fixed_name.length() > 14) {
814 fixed_name = fixed_name.substr (0, 14);
819 if (dcp_content_type()) {
820 d << "_" << dcp_content_type()->dci_name();
824 d << "_" << format()->dci_name();
827 DCIMetadata const dm = dci_metadata ();
829 if (!dm.audio_language.empty ()) {
830 d << "_" << dm.audio_language;
831 if (!dm.subtitle_language.empty()) {
832 d << "-" << dm.subtitle_language;
838 if (!dm.territory.empty ()) {
839 d << "_" << dm.territory;
840 if (!dm.rating.empty ()) {
841 d << "-" << dm.rating;
845 switch (audio_channels()) {
862 if (!dm.studio.empty ()) {
863 d << "_" << dm.studio;
866 if (if_created_now) {
867 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
869 d << "_" << boost::gregorian::to_iso_string (_dci_date);
872 if (!dm.facility.empty ()) {
873 d << "_" << dm.facility;
876 if (!dm.package_type.empty ()) {
877 d << "_" << dm.package_type;
883 /** @return name to give the DCP */
885 Film::dcp_name (bool if_created_now) const
887 if (use_dci_name()) {
888 return dci_name (if_created_now);
896 Film::set_directory (string d)
898 boost::mutex::scoped_lock lm (_state_mutex);
904 Film::set_name (string n)
907 boost::mutex::scoped_lock lm (_state_mutex);
910 signal_changed (NAME);
914 Film::set_use_dci_name (bool u)
917 boost::mutex::scoped_lock lm (_state_mutex);
920 signal_changed (USE_DCI_NAME);
924 Film::set_content (string c)
926 string check = directory ();
928 boost::filesystem::path slash ("/");
929 string platform_slash = slash.make_preferred().string ();
931 if (!ends_with (check, platform_slash)) {
932 check += platform_slash;
935 if (boost::filesystem::path(c).has_root_directory () && starts_with (c, check)) {
936 c = c.substr (_directory.length() + 1);
942 boost::mutex::scoped_lock lm (_state_mutex);
947 old_content = _content;
951 /* Do this before we start using FFmpeg ourselves */
952 run_ffprobe (c, file ("ffprobe.log"), _log);
954 /* Reset streams here in case the new content doesn't have one or the other */
955 _content_audio_stream = shared_ptr<AudioStream> ();
956 _subtitle_stream = shared_ptr<SubtitleStream> ();
958 /* Start off using content audio */
959 set_use_content_audio (true);
961 /* Create a temporary decoder so that we can get information
966 Decoders d = decoder_factory (shared_from_this(), DecodeOptions());
968 set_size (d.video->native_size ());
969 set_source_frame_rate (d.video->frames_per_second ());
970 set_dcp_frame_rate (best_dcp_frame_rate (source_frame_rate ()));
971 set_subtitle_streams (d.video->subtitle_streams ());
973 set_content_audio_streams (d.audio->audio_streams ());
977 boost::mutex::scoped_lock lm (_state_mutex);
981 signal_changed (CONTENT);
983 /* Start off with the first audio and subtitle streams */
984 if (d.audio && !d.audio->audio_streams().empty()) {
985 set_content_audio_stream (d.audio->audio_streams().front());
988 if (!d.video->subtitle_streams().empty()) {
989 set_subtitle_stream (d.video->subtitle_streams().front());
996 boost::mutex::scoped_lock lm (_state_mutex);
997 _content = old_content;
1002 /* Default format */
1003 set_format (Config::instance()->default_format ());
1005 /* Still image DCPs must use external audio */
1006 if (content_type() == STILL) {
1007 set_use_content_audio (false);
1012 Film::set_trust_content_header (bool t)
1015 boost::mutex::scoped_lock lm (_state_mutex);
1016 _trust_content_header = t;
1019 signal_changed (TRUST_CONTENT_HEADER);
1021 if (!_trust_content_header && !content().empty()) {
1022 /* We just said that we don't trust the content's header */
1028 Film::set_dcp_content_type (DCPContentType const * t)
1031 boost::mutex::scoped_lock lm (_state_mutex);
1032 _dcp_content_type = t;
1034 signal_changed (DCP_CONTENT_TYPE);
1038 Film::set_format (Format const * f)
1041 boost::mutex::scoped_lock lm (_state_mutex);
1044 signal_changed (FORMAT);
1048 Film::set_crop (Crop c)
1051 boost::mutex::scoped_lock lm (_state_mutex);
1054 signal_changed (CROP);
1058 Film::set_left_crop (int c)
1061 boost::mutex::scoped_lock lm (_state_mutex);
1063 if (_crop.left == c) {
1069 signal_changed (CROP);
1073 Film::set_right_crop (int c)
1076 boost::mutex::scoped_lock lm (_state_mutex);
1077 if (_crop.right == c) {
1083 signal_changed (CROP);
1087 Film::set_top_crop (int c)
1090 boost::mutex::scoped_lock lm (_state_mutex);
1091 if (_crop.top == c) {
1097 signal_changed (CROP);
1101 Film::set_bottom_crop (int c)
1104 boost::mutex::scoped_lock lm (_state_mutex);
1105 if (_crop.bottom == c) {
1111 signal_changed (CROP);
1115 Film::set_filters (vector<Filter const *> f)
1118 boost::mutex::scoped_lock lm (_state_mutex);
1121 signal_changed (FILTERS);
1125 Film::set_scaler (Scaler const * s)
1128 boost::mutex::scoped_lock lm (_state_mutex);
1131 signal_changed (SCALER);
1135 Film::set_trim_start (int t)
1138 boost::mutex::scoped_lock lm (_state_mutex);
1141 signal_changed (TRIM_START);
1145 Film::set_trim_end (int t)
1148 boost::mutex::scoped_lock lm (_state_mutex);
1151 signal_changed (TRIM_END);
1155 Film::set_trim_type (TrimType t)
1158 boost::mutex::scoped_lock lm (_state_mutex);
1161 signal_changed (TRIM_TYPE);
1165 Film::set_dcp_ab (bool a)
1168 boost::mutex::scoped_lock lm (_state_mutex);
1171 signal_changed (DCP_AB);
1175 Film::set_content_audio_stream (shared_ptr<AudioStream> s)
1178 boost::mutex::scoped_lock lm (_state_mutex);
1179 _content_audio_stream = s;
1181 signal_changed (CONTENT_AUDIO_STREAM);
1185 Film::set_external_audio (vector<string> a)
1188 boost::mutex::scoped_lock lm (_state_mutex);
1189 _external_audio = a;
1192 shared_ptr<SndfileDecoder> decoder (new SndfileDecoder (shared_from_this(), DecodeOptions()));
1193 if (decoder->audio_stream()) {
1194 _sndfile_stream = decoder->audio_stream ();
1197 signal_changed (EXTERNAL_AUDIO);
1201 Film::set_use_content_audio (bool e)
1204 boost::mutex::scoped_lock lm (_state_mutex);
1205 _use_content_audio = e;
1208 signal_changed (USE_CONTENT_AUDIO);
1212 Film::set_audio_gain (float g)
1215 boost::mutex::scoped_lock lm (_state_mutex);
1218 signal_changed (AUDIO_GAIN);
1222 Film::set_audio_delay (int d)
1225 boost::mutex::scoped_lock lm (_state_mutex);
1228 signal_changed (AUDIO_DELAY);
1232 Film::set_still_duration (int d)
1235 boost::mutex::scoped_lock lm (_state_mutex);
1236 _still_duration = d;
1238 signal_changed (STILL_DURATION);
1242 Film::set_subtitle_stream (shared_ptr<SubtitleStream> s)
1245 boost::mutex::scoped_lock lm (_state_mutex);
1246 _subtitle_stream = s;
1248 signal_changed (SUBTITLE_STREAM);
1252 Film::set_with_subtitles (bool w)
1255 boost::mutex::scoped_lock lm (_state_mutex);
1256 _with_subtitles = w;
1258 signal_changed (WITH_SUBTITLES);
1262 Film::set_subtitle_offset (int o)
1265 boost::mutex::scoped_lock lm (_state_mutex);
1266 _subtitle_offset = o;
1268 signal_changed (SUBTITLE_OFFSET);
1272 Film::set_subtitle_scale (float s)
1275 boost::mutex::scoped_lock lm (_state_mutex);
1276 _subtitle_scale = s;
1278 signal_changed (SUBTITLE_SCALE);
1282 Film::set_colour_lut (int i)
1285 boost::mutex::scoped_lock lm (_state_mutex);
1288 signal_changed (COLOUR_LUT);
1292 Film::set_j2k_bandwidth (int b)
1295 boost::mutex::scoped_lock lm (_state_mutex);
1298 signal_changed (J2K_BANDWIDTH);
1302 Film::set_dci_metadata (DCIMetadata m)
1305 boost::mutex::scoped_lock lm (_state_mutex);
1308 signal_changed (DCI_METADATA);
1313 Film::set_dcp_frame_rate (int f)
1316 boost::mutex::scoped_lock lm (_state_mutex);
1317 _dcp_frame_rate = f;
1319 signal_changed (DCP_FRAME_RATE);
1323 Film::set_size (libdcp::Size s)
1326 boost::mutex::scoped_lock lm (_state_mutex);
1329 signal_changed (SIZE);
1333 Film::set_length (SourceFrame l)
1336 boost::mutex::scoped_lock lm (_state_mutex);
1339 signal_changed (LENGTH);
1343 Film::unset_length ()
1346 boost::mutex::scoped_lock lm (_state_mutex);
1347 _length = boost::none;
1349 signal_changed (LENGTH);
1353 Film::set_content_digest (string d)
1356 boost::mutex::scoped_lock lm (_state_mutex);
1357 _content_digest = d;
1363 Film::set_content_audio_streams (vector<shared_ptr<AudioStream> > s)
1366 boost::mutex::scoped_lock lm (_state_mutex);
1367 _content_audio_streams = s;
1369 signal_changed (CONTENT_AUDIO_STREAMS);
1373 Film::set_subtitle_streams (vector<shared_ptr<SubtitleStream> > s)
1376 boost::mutex::scoped_lock lm (_state_mutex);
1377 _subtitle_streams = s;
1379 signal_changed (SUBTITLE_STREAMS);
1383 Film::set_source_frame_rate (float f)
1386 boost::mutex::scoped_lock lm (_state_mutex);
1387 _source_frame_rate = f;
1389 signal_changed (SOURCE_FRAME_RATE);
1393 Film::signal_changed (Property p)
1396 boost::mutex::scoped_lock lm (_state_mutex);
1401 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
1406 Film::audio_channels () const
1408 shared_ptr<AudioStream> s = audio_stream ();
1413 return s->channels ();
1417 Film::set_dci_date_today ()
1419 _dci_date = boost::gregorian::day_clock::local_day ();
1422 boost::shared_ptr<AudioStream>
1423 Film::audio_stream () const
1425 if (use_content_audio()) {
1426 return _content_audio_stream;
1429 return _sndfile_stream;
1433 Film::info_path (int f) const
1435 boost::filesystem::path p;
1440 s << setfill('0') << f << ".md5";
1444 /* info_dir() will already have added any initial bit of the path,
1445 so don't call file() on this.
1451 Film::j2c_path (int f, bool t) const
1453 boost::filesystem::path p;
1455 p /= video_state_identifier ();
1459 s << setfill('0') << f << ".j2c";
1466 return file (p.string ());
1469 /** Make an educated guess as to whether we have a complete DCP
1471 * @return true if we do.
1475 Film::have_dcp () const
1478 libdcp::DCP dcp (dir (dcp_name()));
1488 Film::has_audio () const
1490 if (use_content_audio()) {
1491 return audio_stream();
1494 vector<string> const e = external_audio ();
1495 for (vector<string>::const_iterator i = e.begin(); i != e.end(); ++i) {