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 ();
146 _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 if (use_content_audio()) {
322 log()->log ("Using content's audio");
324 log()->log (String::compose ("Using external audio (%1 files)", external_audio().size()));
326 #ifdef DVDOMATIC_DEBUG
327 log()->log ("DVD-o-matic built in debug mode.");
329 log()->log ("DVD-o-matic built in optimised mode.");
332 log()->log ("libdcp built in debug mode.");
334 log()->log ("libdcp built in optimised mode.");
336 pair<string, int> const c = cpu_info ();
337 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
338 list<pair<string, string> > const m = mount_info ();
339 for (list<pair<string, string> >::const_iterator i = m.begin(); i != m.end(); ++i) {
340 log()->log (String::compose ("Mount: %1 %2", i->first, i->second));
344 throw MissingSettingError (_("format"));
347 if (content().empty ()) {
348 throw MissingSettingError (_("content"));
351 if (dcp_content_type() == 0) {
352 throw MissingSettingError (_("content type"));
355 if (name().empty()) {
356 throw MissingSettingError (_("name"));
360 od.decode_subtitles = with_subtitles ();
365 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this(), od)));
367 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this(), od)));
371 /** Start a job to analyse the audio of our content file */
373 Film::analyse_audio ()
375 if (_analyse_audio_job) {
379 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
380 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
381 JobManager::instance()->add (_analyse_audio_job);
384 /** Start a job to examine our content file */
386 Film::examine_content ()
388 if (_examine_content_job) {
392 _examine_content_job.reset (new ExamineContentJob (shared_from_this()));
393 _examine_content_job->Finished.connect (bind (&Film::examine_content_finished, this));
394 JobManager::instance()->add (_examine_content_job);
398 Film::analyse_audio_finished ()
402 if (_analyse_audio_job->finished_ok ()) {
403 AudioAnalysisSucceeded ();
406 _analyse_audio_job.reset ();
410 Film::examine_content_finished ()
412 _examine_content_job.reset ();
415 /** Start a job to send our DCP to the configured TMS */
417 Film::send_dcp_to_tms ()
419 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
420 JobManager::instance()->add (j);
423 /** Count the number of frames that have been encoded for this film.
424 * @return frame count.
427 Film::encoded_frames () const
434 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
436 boost::this_thread::interruption_point ();
442 /** Write state to our `metadata' file */
444 Film::write_metadata () const
446 boost::mutex::scoped_lock lm (_state_mutex);
449 boost::filesystem::create_directories (directory());
451 string const m = file ("metadata");
452 ofstream f (m.c_str ());
454 throw CreateFileError (m);
457 f << "version " << state_version << endl;
460 f << "name " << _name << endl;
461 f << "use_dci_name " << _use_dci_name << endl;
462 f << "content " << _content << endl;
463 f << "trust_content_header " << (_trust_content_header ? "1" : "0") << endl;
464 if (_dcp_content_type) {
465 f << "dcp_content_type " << _dcp_content_type->dci_name () << endl;
468 f << "format " << _format->as_metadata () << endl;
470 f << "left_crop " << _crop.left << endl;
471 f << "right_crop " << _crop.right << endl;
472 f << "top_crop " << _crop.top << endl;
473 f << "bottom_crop " << _crop.bottom << endl;
474 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
475 f << "filter " << (*i)->id () << endl;
477 f << "scaler " << _scaler->id () << endl;
478 f << "trim_start " << _trim_start << endl;
479 f << "trim_end " << _trim_end << endl;
480 switch (_trim_type) {
482 f << "trim_type cpl\n";
485 f << "trim_type encode\n";
488 f << "dcp_ab " << (_dcp_ab ? "1" : "0") << endl;
489 if (_content_audio_stream) {
490 f << "selected_content_audio_stream " << _content_audio_stream->to_string() << endl;
492 for (vector<string>::const_iterator i = _external_audio.begin(); i != _external_audio.end(); ++i) {
493 f << "external_audio " << *i << endl;
495 f << "use_content_audio " << (_use_content_audio ? "1" : "0") << endl;
496 f << "audio_gain " << _audio_gain << endl;
497 f << "audio_delay " << _audio_delay << endl;
498 f << "still_duration " << _still_duration << endl;
499 if (_subtitle_stream) {
500 f << "selected_subtitle_stream " << _subtitle_stream->to_string() << endl;
502 f << "with_subtitles " << _with_subtitles << endl;
503 f << "subtitle_offset " << _subtitle_offset << endl;
504 f << "subtitle_scale " << _subtitle_scale << endl;
505 f << "colour_lut " << _colour_lut << endl;
506 f << "j2k_bandwidth " << _j2k_bandwidth << endl;
507 _dci_metadata.write (f);
508 f << "dci_date " << boost::gregorian::to_iso_string (_dci_date) << endl;
509 f << "dcp_frame_rate " << _dcp_frame_rate << endl;
510 f << "width " << _size.width << endl;
511 f << "height " << _size.height << endl;
512 f << "length " << _length.get_value_or(0) << endl;
513 f << "content_digest " << _content_digest << endl;
515 for (vector<shared_ptr<AudioStream> >::const_iterator i = _content_audio_streams.begin(); i != _content_audio_streams.end(); ++i) {
516 f << "content_audio_stream " << (*i)->to_string () << endl;
519 f << "external_audio_stream " << _sndfile_stream->to_string() << endl;
521 for (vector<shared_ptr<SubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
522 f << "subtitle_stream " << (*i)->to_string () << endl;
525 f << "source_frame_rate " << _source_frame_rate << endl;
530 /** Read state from our metadata file */
532 Film::read_metadata ()
534 boost::mutex::scoped_lock lm (_state_mutex);
537 _external_audio.clear ();
538 _content_audio_streams.clear ();
539 _subtitle_streams.clear ();
541 boost::optional<int> version;
543 /* Backward compatibility things */
544 boost::optional<int> audio_sample_rate;
545 boost::optional<int> audio_stream_index;
546 boost::optional<int> subtitle_stream_index;
548 ifstream f (file ("metadata").c_str());
550 throw OpenFileError (file ("metadata"));
553 multimap<string, string> kv = read_key_value (f);
555 /* We need version before anything else */
556 multimap<string, string>::iterator v = kv.find ("version");
557 if (v != kv.end ()) {
558 version = atoi (v->second.c_str());
561 for (multimap<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
562 string const k = i->first;
563 string const v = i->second;
565 if (k == "audio_sample_rate") {
566 audio_sample_rate = atoi (v.c_str());
569 /* User-specified stuff */
572 } else if (k == "use_dci_name") {
573 _use_dci_name = (v == "1");
574 } else if (k == "content") {
576 } else if (k == "trust_content_header") {
577 _trust_content_header = (v == "1");
578 } else if (k == "dcp_content_type") {
580 _dcp_content_type = DCPContentType::from_pretty_name (v);
582 _dcp_content_type = DCPContentType::from_dci_name (v);
584 } else if (k == "format") {
585 _format = Format::from_metadata (v);
586 } else if (k == "left_crop") {
587 _crop.left = atoi (v.c_str ());
588 } else if (k == "right_crop") {
589 _crop.right = atoi (v.c_str ());
590 } else if (k == "top_crop") {
591 _crop.top = atoi (v.c_str ());
592 } else if (k == "bottom_crop") {
593 _crop.bottom = atoi (v.c_str ());
594 } else if (k == "filter") {
595 _filters.push_back (Filter::from_id (v));
596 } else if (k == "scaler") {
597 _scaler = Scaler::from_id (v);
598 } else if ( ((!version || version < 2) && k == "dcp_trim_start") || k == "trim_start") {
599 _trim_start = atoi (v.c_str ());
600 } else if ( ((!version || version < 2) && k == "dcp_trim_end") || k == "trim_end") {
601 _trim_end = atoi (v.c_str ());
602 } else if (k == "trim_type") {
605 } else if (v == "encode") {
608 } else if (k == "dcp_ab") {
609 _dcp_ab = (v == "1");
610 } else if (k == "selected_content_audio_stream" || (!version && k == "selected_audio_stream")) {
612 audio_stream_index = atoi (v.c_str ());
614 _content_audio_stream = audio_stream_factory (v, version);
616 } else if (k == "external_audio") {
617 _external_audio.push_back (v);
618 } else if (k == "use_content_audio") {
619 _use_content_audio = (v == "1");
620 } else if (k == "audio_gain") {
621 _audio_gain = atof (v.c_str ());
622 } else if (k == "audio_delay") {
623 _audio_delay = atoi (v.c_str ());
624 } else if (k == "still_duration") {
625 _still_duration = atoi (v.c_str ());
626 } else if (k == "selected_subtitle_stream") {
628 subtitle_stream_index = atoi (v.c_str ());
630 _subtitle_stream = subtitle_stream_factory (v, version);
632 } else if (k == "with_subtitles") {
633 _with_subtitles = (v == "1");
634 } else if (k == "subtitle_offset") {
635 _subtitle_offset = atoi (v.c_str ());
636 } else if (k == "subtitle_scale") {
637 _subtitle_scale = atof (v.c_str ());
638 } else if (k == "colour_lut") {
639 _colour_lut = atoi (v.c_str ());
640 } else if (k == "j2k_bandwidth") {
641 _j2k_bandwidth = atoi (v.c_str ());
642 } else if (k == "dci_date") {
643 _dci_date = boost::gregorian::from_undelimited_string (v);
644 } else if (k == "dcp_frame_rate") {
645 _dcp_frame_rate = atoi (v.c_str ());
648 _dci_metadata.read (k, v);
652 _size.width = atoi (v.c_str ());
653 } else if (k == "height") {
654 _size.height = atoi (v.c_str ());
655 } else if (k == "length") {
656 int const vv = atoi (v.c_str ());
660 } else if (k == "content_digest") {
662 } else if (k == "content_audio_stream" || (!version && k == "audio_stream")) {
663 _content_audio_streams.push_back (audio_stream_factory (v, version));
664 } else if (k == "external_audio_stream") {
665 _sndfile_stream = audio_stream_factory (v, version);
666 } else if (k == "subtitle_stream") {
667 _subtitle_streams.push_back (subtitle_stream_factory (v, version));
668 } else if (k == "source_frame_rate") {
669 _source_frame_rate = atof (v.c_str ());
670 } else if (version < 4 && k == "frames_per_second") {
671 _source_frame_rate = atof (v.c_str ());
672 /* Fill in what would have been used for DCP frame rate by the older version */
673 _dcp_frame_rate = best_dcp_frame_rate (_source_frame_rate);
678 if (audio_sample_rate) {
679 /* version < 1 didn't specify sample rate in the audio streams, so fill it in here */
680 for (vector<shared_ptr<AudioStream> >::iterator i = _content_audio_streams.begin(); i != _content_audio_streams.end(); ++i) {
681 (*i)->set_sample_rate (audio_sample_rate.get());
685 /* also the selected stream was specified as an index */
686 if (audio_stream_index && audio_stream_index.get() >= 0 && audio_stream_index.get() < (int) _content_audio_streams.size()) {
687 _content_audio_stream = _content_audio_streams[audio_stream_index.get()];
690 /* similarly the subtitle */
691 if (subtitle_stream_index && subtitle_stream_index.get() >= 0 && subtitle_stream_index.get() < (int) _subtitle_streams.size()) {
692 _subtitle_stream = _subtitle_streams[subtitle_stream_index.get()];
698 _log->log (String::compose ("Loaded film with use_content_audio = %1", use_content_audio ()));
702 Film::cropped_size (libdcp::Size s) const
704 boost::mutex::scoped_lock lm (_state_mutex);
705 s.width -= _crop.left + _crop.right;
706 s.height -= _crop.top + _crop.bottom;
710 /** Given a directory name, return its full path within the Film's directory.
711 * The directory (and its parents) will be created if they do not exist.
714 Film::dir (string d) const
716 boost::mutex::scoped_lock lm (_directory_mutex);
718 boost::filesystem::path p;
722 boost::filesystem::create_directories (p);
727 /** Given a file or directory name, return its full path within the Film's directory.
728 * _directory_mutex must not be locked on entry.
729 * Any required parent directories will be created.
732 Film::file (string f) const
734 boost::mutex::scoped_lock lm (_directory_mutex);
736 boost::filesystem::path p;
740 boost::filesystem::create_directories (p.parent_path ());
745 /** @return full path of the content (actual video) file
749 Film::content_path () const
751 boost::mutex::scoped_lock lm (_state_mutex);
752 if (boost::filesystem::path(_content).has_root_directory ()) {
756 return file (_content);
760 Film::content_type () const
762 if (boost::filesystem::is_directory (_content)) {
763 /* Directory of images, we assume */
767 if (still_image_file (_content)) {
774 /** @return The sampling rate that we will resample the audio to */
776 Film::target_audio_sample_rate () const
778 if (!audio_stream()) {
782 /* Resample to a DCI-approved sample rate */
783 double t = dcp_audio_sample_rate (audio_stream()->sample_rate());
785 FrameRateConversion frc (source_frame_rate(), dcp_frame_rate());
787 /* Compensate if the DCP is being run at a different frame rate
788 to the source; that is, if the video is run such that it will
789 look different in the DCP compared to the source (slower or faster).
790 skip/repeat doesn't come into effect here.
793 if (frc.change_speed) {
794 t *= source_frame_rate() * frc.factor() / dcp_frame_rate();
801 Film::still_duration_in_frames () const
803 return still_duration() * source_frame_rate();
806 /** @return a DCI-compliant name for a DCP of this film */
808 Film::dci_name (bool if_created_now) const
812 string fixed_name = to_upper_copy (name());
813 for (size_t i = 0; i < fixed_name.length(); ++i) {
814 if (fixed_name[i] == ' ') {
819 /* Spec is that the name part should be maximum 14 characters, as I understand it */
820 if (fixed_name.length() > 14) {
821 fixed_name = fixed_name.substr (0, 14);
826 if (dcp_content_type()) {
827 d << "_" << dcp_content_type()->dci_name();
831 d << "_" << format()->dci_name();
834 DCIMetadata const dm = dci_metadata ();
836 if (!dm.audio_language.empty ()) {
837 d << "_" << dm.audio_language;
838 if (!dm.subtitle_language.empty()) {
839 d << "-" << dm.subtitle_language;
845 if (!dm.territory.empty ()) {
846 d << "_" << dm.territory;
847 if (!dm.rating.empty ()) {
848 d << "-" << dm.rating;
852 switch (audio_channels()) {
869 if (!dm.studio.empty ()) {
870 d << "_" << dm.studio;
873 if (if_created_now) {
874 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
876 d << "_" << boost::gregorian::to_iso_string (_dci_date);
879 if (!dm.facility.empty ()) {
880 d << "_" << dm.facility;
883 if (!dm.package_type.empty ()) {
884 d << "_" << dm.package_type;
890 /** @return name to give the DCP */
892 Film::dcp_name (bool if_created_now) const
894 if (use_dci_name()) {
895 return dci_name (if_created_now);
903 Film::set_directory (string d)
905 boost::mutex::scoped_lock lm (_state_mutex);
911 Film::set_name (string n)
914 boost::mutex::scoped_lock lm (_state_mutex);
917 signal_changed (NAME);
921 Film::set_use_dci_name (bool u)
924 boost::mutex::scoped_lock lm (_state_mutex);
927 signal_changed (USE_DCI_NAME);
931 Film::set_content (string c)
933 string check = directory ();
935 boost::filesystem::path slash ("/");
936 string platform_slash = slash.make_preferred().string ();
938 if (!ends_with (check, platform_slash)) {
939 check += platform_slash;
942 if (boost::filesystem::path(c).has_root_directory () && starts_with (c, check)) {
943 c = c.substr (_directory.length() + 1);
949 boost::mutex::scoped_lock lm (_state_mutex);
954 old_content = _content;
958 /* Do this before we start using FFmpeg ourselves */
959 run_ffprobe (c, file ("ffprobe.log"), _log);
961 /* Reset streams here in case the new content doesn't have one or the other */
962 _content_audio_stream = shared_ptr<AudioStream> ();
963 _subtitle_stream = shared_ptr<SubtitleStream> ();
965 /* Start off using content audio */
966 set_use_content_audio (true);
968 /* Create a temporary decoder so that we can get information
973 Decoders d = decoder_factory (shared_from_this(), DecodeOptions());
975 set_size (d.video->native_size ());
976 set_source_frame_rate (d.video->frames_per_second ());
977 set_dcp_frame_rate (best_dcp_frame_rate (source_frame_rate ()));
978 set_subtitle_streams (d.video->subtitle_streams ());
980 set_content_audio_streams (d.audio->audio_streams ());
984 boost::mutex::scoped_lock lm (_state_mutex);
988 signal_changed (CONTENT);
990 /* Start off with the first audio and subtitle streams */
991 if (d.audio && !d.audio->audio_streams().empty()) {
992 set_content_audio_stream (d.audio->audio_streams().front());
995 if (!d.video->subtitle_streams().empty()) {
996 set_subtitle_stream (d.video->subtitle_streams().front());
1003 boost::mutex::scoped_lock lm (_state_mutex);
1004 _content = old_content;
1009 /* Default format */
1010 set_format (Config::instance()->default_format ());
1012 /* Still image DCPs must use external audio */
1013 if (content_type() == STILL) {
1014 set_use_content_audio (false);
1019 Film::set_trust_content_header (bool t)
1022 boost::mutex::scoped_lock lm (_state_mutex);
1023 _trust_content_header = t;
1026 signal_changed (TRUST_CONTENT_HEADER);
1028 if (!_trust_content_header && !content().empty()) {
1029 /* We just said that we don't trust the content's header */
1035 Film::set_dcp_content_type (DCPContentType const * t)
1038 boost::mutex::scoped_lock lm (_state_mutex);
1039 _dcp_content_type = t;
1041 signal_changed (DCP_CONTENT_TYPE);
1045 Film::set_format (Format const * f)
1048 boost::mutex::scoped_lock lm (_state_mutex);
1051 signal_changed (FORMAT);
1055 Film::set_crop (Crop c)
1058 boost::mutex::scoped_lock lm (_state_mutex);
1061 signal_changed (CROP);
1065 Film::set_left_crop (int c)
1068 boost::mutex::scoped_lock lm (_state_mutex);
1070 if (_crop.left == c) {
1076 signal_changed (CROP);
1080 Film::set_right_crop (int c)
1083 boost::mutex::scoped_lock lm (_state_mutex);
1084 if (_crop.right == c) {
1090 signal_changed (CROP);
1094 Film::set_top_crop (int c)
1097 boost::mutex::scoped_lock lm (_state_mutex);
1098 if (_crop.top == c) {
1104 signal_changed (CROP);
1108 Film::set_bottom_crop (int c)
1111 boost::mutex::scoped_lock lm (_state_mutex);
1112 if (_crop.bottom == c) {
1118 signal_changed (CROP);
1122 Film::set_filters (vector<Filter const *> f)
1125 boost::mutex::scoped_lock lm (_state_mutex);
1128 signal_changed (FILTERS);
1132 Film::set_scaler (Scaler const * s)
1135 boost::mutex::scoped_lock lm (_state_mutex);
1138 signal_changed (SCALER);
1142 Film::set_trim_start (int t)
1145 boost::mutex::scoped_lock lm (_state_mutex);
1148 signal_changed (TRIM_START);
1152 Film::set_trim_end (int t)
1155 boost::mutex::scoped_lock lm (_state_mutex);
1158 signal_changed (TRIM_END);
1162 Film::set_trim_type (TrimType t)
1165 boost::mutex::scoped_lock lm (_state_mutex);
1168 signal_changed (TRIM_TYPE);
1172 Film::set_dcp_ab (bool a)
1175 boost::mutex::scoped_lock lm (_state_mutex);
1178 signal_changed (DCP_AB);
1182 Film::set_content_audio_stream (shared_ptr<AudioStream> s)
1185 boost::mutex::scoped_lock lm (_state_mutex);
1186 _content_audio_stream = s;
1188 signal_changed (CONTENT_AUDIO_STREAM);
1192 Film::set_external_audio (vector<string> a)
1195 boost::mutex::scoped_lock lm (_state_mutex);
1196 _external_audio = a;
1199 shared_ptr<SndfileDecoder> decoder (new SndfileDecoder (shared_from_this(), DecodeOptions()));
1200 if (decoder->audio_stream()) {
1201 _sndfile_stream = decoder->audio_stream ();
1204 signal_changed (EXTERNAL_AUDIO);
1208 Film::set_use_content_audio (bool e)
1211 boost::mutex::scoped_lock lm (_state_mutex);
1212 _use_content_audio = e;
1215 signal_changed (USE_CONTENT_AUDIO);
1219 Film::set_audio_gain (float g)
1222 boost::mutex::scoped_lock lm (_state_mutex);
1225 signal_changed (AUDIO_GAIN);
1229 Film::set_audio_delay (int d)
1232 boost::mutex::scoped_lock lm (_state_mutex);
1235 signal_changed (AUDIO_DELAY);
1239 Film::set_still_duration (int d)
1242 boost::mutex::scoped_lock lm (_state_mutex);
1243 _still_duration = d;
1245 signal_changed (STILL_DURATION);
1249 Film::set_subtitle_stream (shared_ptr<SubtitleStream> s)
1252 boost::mutex::scoped_lock lm (_state_mutex);
1253 _subtitle_stream = s;
1255 signal_changed (SUBTITLE_STREAM);
1259 Film::set_with_subtitles (bool w)
1262 boost::mutex::scoped_lock lm (_state_mutex);
1263 _with_subtitles = w;
1265 signal_changed (WITH_SUBTITLES);
1269 Film::set_subtitle_offset (int o)
1272 boost::mutex::scoped_lock lm (_state_mutex);
1273 _subtitle_offset = o;
1275 signal_changed (SUBTITLE_OFFSET);
1279 Film::set_subtitle_scale (float s)
1282 boost::mutex::scoped_lock lm (_state_mutex);
1283 _subtitle_scale = s;
1285 signal_changed (SUBTITLE_SCALE);
1289 Film::set_colour_lut (int i)
1292 boost::mutex::scoped_lock lm (_state_mutex);
1295 signal_changed (COLOUR_LUT);
1299 Film::set_j2k_bandwidth (int b)
1302 boost::mutex::scoped_lock lm (_state_mutex);
1305 signal_changed (J2K_BANDWIDTH);
1309 Film::set_dci_metadata (DCIMetadata m)
1312 boost::mutex::scoped_lock lm (_state_mutex);
1315 signal_changed (DCI_METADATA);
1320 Film::set_dcp_frame_rate (int f)
1323 boost::mutex::scoped_lock lm (_state_mutex);
1324 _dcp_frame_rate = f;
1326 signal_changed (DCP_FRAME_RATE);
1330 Film::set_size (libdcp::Size s)
1333 boost::mutex::scoped_lock lm (_state_mutex);
1336 signal_changed (SIZE);
1340 Film::set_length (SourceFrame l)
1343 boost::mutex::scoped_lock lm (_state_mutex);
1346 signal_changed (LENGTH);
1350 Film::unset_length ()
1353 boost::mutex::scoped_lock lm (_state_mutex);
1354 _length = boost::none;
1356 signal_changed (LENGTH);
1360 Film::set_content_digest (string d)
1363 boost::mutex::scoped_lock lm (_state_mutex);
1364 _content_digest = d;
1370 Film::set_content_audio_streams (vector<shared_ptr<AudioStream> > s)
1373 boost::mutex::scoped_lock lm (_state_mutex);
1374 _content_audio_streams = s;
1376 signal_changed (CONTENT_AUDIO_STREAMS);
1380 Film::set_subtitle_streams (vector<shared_ptr<SubtitleStream> > s)
1383 boost::mutex::scoped_lock lm (_state_mutex);
1384 _subtitle_streams = s;
1386 signal_changed (SUBTITLE_STREAMS);
1390 Film::set_source_frame_rate (float f)
1393 boost::mutex::scoped_lock lm (_state_mutex);
1394 _source_frame_rate = f;
1396 signal_changed (SOURCE_FRAME_RATE);
1400 Film::signal_changed (Property p)
1403 boost::mutex::scoped_lock lm (_state_mutex);
1408 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
1413 Film::audio_channels () const
1415 shared_ptr<AudioStream> s = audio_stream ();
1420 return s->channels ();
1424 Film::set_dci_date_today ()
1426 _dci_date = boost::gregorian::day_clock::local_day ();
1429 boost::shared_ptr<AudioStream>
1430 Film::audio_stream () const
1432 if (use_content_audio()) {
1433 return _content_audio_stream;
1436 return _sndfile_stream;
1440 Film::info_path (int f) const
1442 boost::filesystem::path p;
1447 s << setfill('0') << f << ".md5";
1451 /* info_dir() will already have added any initial bit of the path,
1452 so don't call file() on this.
1458 Film::j2c_path (int f, bool t) const
1460 boost::filesystem::path p;
1462 p /= video_state_identifier ();
1466 s << setfill('0') << f << ".j2c";
1473 return file (p.string ());
1476 /** Make an educated guess as to whether we have a complete DCP
1478 * @return true if we do.
1482 Film::have_dcp () const
1485 libdcp::DCP dcp (dir (dcp_name()));
1495 Film::has_audio () const
1497 if (use_content_audio()) {
1498 return audio_stream();
1501 vector<string> const e = external_audio ();
1502 for (vector<string>::const_iterator i = e.begin(); i != e.end(); ++i) {