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 , _minimum_audio_channels (0)
112 , _source_frame_rate (0)
115 set_dci_date_today ();
117 /* Make state.directory a complete path without ..s (where possible)
118 (Code swiped from Adam Bowen on stackoverflow)
121 boost::filesystem::path p (boost::filesystem::system_complete (d));
122 boost::filesystem::path result;
123 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
125 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
128 result = result.parent_path ();
130 } else if (*i != ".") {
135 set_directory (result.string ());
137 if (!boost::filesystem::exists (directory())) {
139 throw OpenFileError (directory());
141 boost::filesystem::create_directory (directory());
145 _sndfile_stream = SndfileStream::create ();
147 _log.reset (new FileLog (file ("log")));
156 Film::Film (Film const & o)
157 : boost::enable_shared_from_this<Film> (o)
158 /* note: the copied film shares the original's log */
160 , _directory (o._directory)
162 , _use_dci_name (o._use_dci_name)
163 , _content (o._content)
164 , _trust_content_header (o._trust_content_header)
165 , _dcp_content_type (o._dcp_content_type)
166 , _format (o._format)
168 , _filters (o._filters)
169 , _scaler (o._scaler)
170 , _trim_start (o._trim_start)
171 , _trim_end (o._trim_end)
172 , _trim_type (o._trim_type)
173 , _dcp_ab (o._dcp_ab)
174 , _content_audio_stream (o._content_audio_stream)
175 , _external_audio (o._external_audio)
176 , _use_content_audio (o._use_content_audio)
177 , _audio_gain (o._audio_gain)
178 , _audio_delay (o._audio_delay)
179 , _still_duration (o._still_duration)
180 , _subtitle_stream (o._subtitle_stream)
181 , _with_subtitles (o._with_subtitles)
182 , _subtitle_offset (o._subtitle_offset)
183 , _subtitle_scale (o._subtitle_scale)
184 , _colour_lut (o._colour_lut)
185 , _j2k_bandwidth (o._j2k_bandwidth)
186 , _dci_metadata (o._dci_metadata)
187 , _dci_date (o._dci_date)
188 , _dcp_frame_rate (o._dcp_frame_rate)
189 , _minimum_audio_channels (o._minimum_audio_channels)
191 , _length (o._length)
192 , _content_digest (o._content_digest)
193 , _content_audio_streams (o._content_audio_streams)
194 , _sndfile_stream (o._sndfile_stream)
195 , _subtitle_streams (o._subtitle_streams)
196 , _source_frame_rate (o._source_frame_rate)
208 Film::video_state_identifier () const
213 pair<string, string> f = Filter::ffmpeg_strings (filters());
217 << "_" << content_digest()
218 << "_" << crop().left << "_" << crop().right << "_" << crop().top << "_" << crop().bottom
219 << "_" << _dcp_frame_rate
220 << "_" << f.first << "_" << f.second
221 << "_" << scaler()->id()
222 << "_" << j2k_bandwidth()
223 << "_" << boost::lexical_cast<int> (colour_lut());
225 if (trim_type() == ENCODE) {
226 s << "_" << trim_start() << "_" << trim_end();
230 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
231 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
237 /** @return The path to the directory to write video frame info files to */
239 Film::info_dir () const
241 boost::filesystem::path p;
243 p /= video_state_identifier ();
244 return dir (p.string());
248 Film::internal_video_mxf_dir () const
250 return dir ("video");
254 Film::internal_video_mxf_filename () const
256 return video_state_identifier() + ".mxf";
260 Film::dcp_video_mxf_filename () const
262 return filename_safe_name() + "_video.mxf";
266 Film::dcp_audio_mxf_filename () const
268 return filename_safe_name() + "_audio.mxf";
272 Film::filename_safe_name () const
274 string const n = name ();
276 for (size_t i = 0; i < n.length(); ++i) {
277 if (isalnum (n[i])) {
288 Film::audio_analysis_path () const
290 boost::filesystem::path p;
292 p /= content_digest();
293 return file (p.string ());
296 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
300 set_dci_date_today ();
302 if (dcp_name().find ("/") != string::npos) {
303 throw BadSettingError (_("name"), _("cannot contain slashes"));
306 log()->log (String::compose ("DVD-o-matic %1 git %2 using %3", dvdomatic_version, dvdomatic_git_commit, dependency_version_summary()));
310 gethostname (buffer, sizeof (buffer));
311 log()->log (String::compose ("Starting to make DCP on %1", buffer));
314 log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
316 log()->log (String::compose ("Content length %1", length().get()));
318 log()->log (String::compose ("Content digest %1", content_digest()));
319 log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
320 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
321 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
322 if (use_content_audio()) {
323 log()->log ("Using content's audio");
325 log()->log (String::compose ("Using external audio (%1 files)", external_audio().size()));
327 #ifdef DVDOMATIC_DEBUG
328 log()->log ("DVD-o-matic built in debug mode.");
330 log()->log ("DVD-o-matic built in optimised mode.");
333 log()->log ("libdcp built in debug mode.");
335 log()->log ("libdcp built in optimised mode.");
337 pair<string, int> const c = cpu_info ();
338 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
339 list<pair<string, string> > const m = mount_info ();
340 for (list<pair<string, string> >::const_iterator i = m.begin(); i != m.end(); ++i) {
341 log()->log (String::compose ("Mount: %1 %2", i->first, i->second));
345 throw MissingSettingError (_("format"));
348 if (content().empty ()) {
349 throw MissingSettingError (_("content"));
352 if (dcp_content_type() == 0) {
353 throw MissingSettingError (_("content type"));
356 if (name().empty()) {
357 throw MissingSettingError (_("name"));
361 od.decode_subtitles = with_subtitles ();
366 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this(), od)));
368 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this(), od)));
372 /** Start a job to analyse the audio of our content file */
374 Film::analyse_audio ()
376 if (_analyse_audio_job) {
380 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
381 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
382 JobManager::instance()->add (_analyse_audio_job);
385 /** Start a job to examine our content file */
387 Film::examine_content ()
389 if (_examine_content_job) {
393 _examine_content_job.reset (new ExamineContentJob (shared_from_this()));
394 _examine_content_job->Finished.connect (bind (&Film::examine_content_finished, this));
395 JobManager::instance()->add (_examine_content_job);
399 Film::analyse_audio_finished ()
403 if (_analyse_audio_job->finished_ok ()) {
404 AudioAnalysisSucceeded ();
407 _analyse_audio_job.reset ();
411 Film::examine_content_finished ()
413 _examine_content_job.reset ();
416 /** Start a job to send our DCP to the configured TMS */
418 Film::send_dcp_to_tms ()
420 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
421 JobManager::instance()->add (j);
424 /** Count the number of frames that have been encoded for this film.
425 * @return frame count.
428 Film::encoded_frames () const
435 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
437 boost::this_thread::interruption_point ();
443 /** Write state to our `metadata' file */
445 Film::write_metadata () const
447 boost::mutex::scoped_lock lm (_state_mutex);
450 boost::filesystem::create_directories (directory());
452 string const m = file ("metadata");
453 ofstream f (m.c_str ());
455 throw CreateFileError (m);
458 f << "version " << state_version << endl;
461 f << "name " << _name << endl;
462 f << "use_dci_name " << _use_dci_name << endl;
463 f << "content " << _content << endl;
464 f << "trust_content_header " << (_trust_content_header ? "1" : "0") << endl;
465 if (_dcp_content_type) {
466 f << "dcp_content_type " << _dcp_content_type->dci_name () << endl;
469 f << "format " << _format->as_metadata () << endl;
471 f << "left_crop " << _crop.left << endl;
472 f << "right_crop " << _crop.right << endl;
473 f << "top_crop " << _crop.top << endl;
474 f << "bottom_crop " << _crop.bottom << endl;
475 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
476 f << "filter " << (*i)->id () << endl;
478 f << "scaler " << _scaler->id () << endl;
479 f << "trim_start " << _trim_start << endl;
480 f << "trim_end " << _trim_end << endl;
481 switch (_trim_type) {
483 f << "trim_type cpl\n";
486 f << "trim_type encode\n";
489 f << "dcp_ab " << (_dcp_ab ? "1" : "0") << endl;
490 if (_content_audio_stream) {
491 f << "selected_content_audio_stream " << _content_audio_stream->to_string() << endl;
493 for (vector<string>::const_iterator i = _external_audio.begin(); i != _external_audio.end(); ++i) {
494 f << "external_audio " << *i << endl;
496 f << "use_content_audio " << (_use_content_audio ? "1" : "0") << endl;
497 f << "audio_gain " << _audio_gain << endl;
498 f << "audio_delay " << _audio_delay << endl;
499 f << "still_duration " << _still_duration << endl;
500 if (_subtitle_stream) {
501 f << "selected_subtitle_stream " << _subtitle_stream->to_string() << endl;
503 f << "with_subtitles " << _with_subtitles << endl;
504 f << "subtitle_offset " << _subtitle_offset << endl;
505 f << "subtitle_scale " << _subtitle_scale << endl;
506 f << "colour_lut " << _colour_lut << endl;
507 f << "j2k_bandwidth " << _j2k_bandwidth << endl;
508 _dci_metadata.write (f);
509 f << "dci_date " << boost::gregorian::to_iso_string (_dci_date) << endl;
510 f << "dcp_frame_rate " << _dcp_frame_rate << endl;
511 f << "minimum_audio_channels " << _minimum_audio_channels << endl;
512 f << "width " << _size.width << endl;
513 f << "height " << _size.height << endl;
514 f << "length " << _length.get_value_or(0) << endl;
515 f << "content_digest " << _content_digest << endl;
517 for (vector<shared_ptr<AudioStream> >::const_iterator i = _content_audio_streams.begin(); i != _content_audio_streams.end(); ++i) {
518 f << "content_audio_stream " << (*i)->to_string () << endl;
521 f << "external_audio_stream " << _sndfile_stream->to_string() << endl;
523 for (vector<shared_ptr<SubtitleStream> >::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
524 f << "subtitle_stream " << (*i)->to_string () << endl;
527 f << "source_frame_rate " << _source_frame_rate << endl;
532 /** Read state from our metadata file */
534 Film::read_metadata ()
536 boost::mutex::scoped_lock lm (_state_mutex);
539 _external_audio.clear ();
540 _content_audio_streams.clear ();
541 _subtitle_streams.clear ();
543 boost::optional<int> version;
545 /* Backward compatibility things */
546 boost::optional<int> audio_sample_rate;
547 boost::optional<int> audio_stream_index;
548 boost::optional<int> subtitle_stream_index;
550 ifstream f (file ("metadata").c_str());
552 throw OpenFileError (file ("metadata"));
555 multimap<string, string> kv = read_key_value (f);
557 /* We need version before anything else */
558 multimap<string, string>::iterator v = kv.find ("version");
559 if (v != kv.end ()) {
560 version = atoi (v->second.c_str());
563 for (multimap<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
564 string const k = i->first;
565 string const v = i->second;
567 if (k == "audio_sample_rate") {
568 audio_sample_rate = atoi (v.c_str());
571 /* User-specified stuff */
574 } else if (k == "use_dci_name") {
575 _use_dci_name = (v == "1");
576 } else if (k == "content") {
578 } else if (k == "trust_content_header") {
579 _trust_content_header = (v == "1");
580 } else if (k == "dcp_content_type") {
582 _dcp_content_type = DCPContentType::from_pretty_name (v);
584 _dcp_content_type = DCPContentType::from_dci_name (v);
586 } else if (k == "format") {
587 _format = Format::from_metadata (v);
588 } else if (k == "left_crop") {
589 _crop.left = atoi (v.c_str ());
590 } else if (k == "right_crop") {
591 _crop.right = atoi (v.c_str ());
592 } else if (k == "top_crop") {
593 _crop.top = atoi (v.c_str ());
594 } else if (k == "bottom_crop") {
595 _crop.bottom = atoi (v.c_str ());
596 } else if (k == "filter") {
597 _filters.push_back (Filter::from_id (v));
598 } else if (k == "scaler") {
599 _scaler = Scaler::from_id (v);
600 } else if ( ((!version || version < 2) && k == "dcp_trim_start") || k == "trim_start") {
601 _trim_start = atoi (v.c_str ());
602 } else if ( ((!version || version < 2) && k == "dcp_trim_end") || k == "trim_end") {
603 _trim_end = atoi (v.c_str ());
604 } else if (k == "trim_type") {
607 } else if (v == "encode") {
610 } else if (k == "dcp_ab") {
611 _dcp_ab = (v == "1");
612 } else if (k == "selected_content_audio_stream" || (!version && k == "selected_audio_stream")) {
614 audio_stream_index = atoi (v.c_str ());
616 _content_audio_stream = audio_stream_factory (v, version);
618 } else if (k == "external_audio") {
619 _external_audio.push_back (v);
620 } else if (k == "use_content_audio") {
621 _use_content_audio = (v == "1");
622 } else if (k == "audio_gain") {
623 _audio_gain = atof (v.c_str ());
624 } else if (k == "audio_delay") {
625 _audio_delay = atoi (v.c_str ());
626 } else if (k == "still_duration") {
627 _still_duration = atoi (v.c_str ());
628 } else if (k == "selected_subtitle_stream") {
630 subtitle_stream_index = atoi (v.c_str ());
632 _subtitle_stream = subtitle_stream_factory (v, version);
634 } else if (k == "with_subtitles") {
635 _with_subtitles = (v == "1");
636 } else if (k == "subtitle_offset") {
637 _subtitle_offset = atoi (v.c_str ());
638 } else if (k == "subtitle_scale") {
639 _subtitle_scale = atof (v.c_str ());
640 } else if (k == "colour_lut") {
641 _colour_lut = atoi (v.c_str ());
642 } else if (k == "j2k_bandwidth") {
643 _j2k_bandwidth = atoi (v.c_str ());
644 } else if (k == "dci_date") {
645 _dci_date = boost::gregorian::from_undelimited_string (v);
646 } else if (k == "dcp_frame_rate") {
647 _dcp_frame_rate = atoi (v.c_str ());
648 } else if (k == "minimum_audio_channels") {
649 _minimum_audio_channels = atoi (v.c_str ());
652 _dci_metadata.read (k, v);
656 _size.width = atoi (v.c_str ());
657 } else if (k == "height") {
658 _size.height = atoi (v.c_str ());
659 } else if (k == "length") {
660 int const vv = atoi (v.c_str ());
664 } else if (k == "content_digest") {
666 } else if (k == "content_audio_stream" || (!version && k == "audio_stream")) {
667 _content_audio_streams.push_back (audio_stream_factory (v, version));
668 } else if (k == "external_audio_stream") {
669 _sndfile_stream = audio_stream_factory (v, version);
670 } else if (k == "subtitle_stream") {
671 _subtitle_streams.push_back (subtitle_stream_factory (v, version));
672 } else if (k == "source_frame_rate") {
673 _source_frame_rate = atof (v.c_str ());
674 } else if (version < 4 && k == "frames_per_second") {
675 _source_frame_rate = atof (v.c_str ());
676 /* Fill in what would have been used for DCP frame rate by the older version */
677 _dcp_frame_rate = best_dcp_frame_rate (_source_frame_rate);
682 if (audio_sample_rate) {
683 /* version < 1 didn't specify sample rate in the audio streams, so fill it in here */
684 for (vector<shared_ptr<AudioStream> >::iterator i = _content_audio_streams.begin(); i != _content_audio_streams.end(); ++i) {
685 (*i)->set_sample_rate (audio_sample_rate.get());
689 /* also the selected stream was specified as an index */
690 if (audio_stream_index && audio_stream_index.get() >= 0 && audio_stream_index.get() < (int) _content_audio_streams.size()) {
691 _content_audio_stream = _content_audio_streams[audio_stream_index.get()];
694 /* similarly the subtitle */
695 if (subtitle_stream_index && subtitle_stream_index.get() >= 0 && subtitle_stream_index.get() < (int) _subtitle_streams.size()) {
696 _subtitle_stream = _subtitle_streams[subtitle_stream_index.get()];
704 Film::cropped_size (libdcp::Size s) const
706 boost::mutex::scoped_lock lm (_state_mutex);
707 s.width -= _crop.left + _crop.right;
708 s.height -= _crop.top + _crop.bottom;
712 /** Given a directory name, return its full path within the Film's directory.
713 * The directory (and its parents) will be created if they do not exist.
716 Film::dir (string d) const
718 boost::mutex::scoped_lock lm (_directory_mutex);
720 boost::filesystem::path p;
724 boost::filesystem::create_directories (p);
729 /** Given a file or directory name, return its full path within the Film's directory.
730 * _directory_mutex must not be locked on entry.
731 * Any required parent directories will be created.
734 Film::file (string f) const
736 boost::mutex::scoped_lock lm (_directory_mutex);
738 boost::filesystem::path p;
742 boost::filesystem::create_directories (p.parent_path ());
747 /** @return full path of the content (actual video) file
751 Film::content_path () const
753 boost::mutex::scoped_lock lm (_state_mutex);
754 if (boost::filesystem::path(_content).has_root_directory ()) {
758 return file (_content);
762 Film::content_type () const
764 if (boost::filesystem::is_directory (_content)) {
765 /* Directory of images, we assume */
769 if (still_image_file (_content)) {
776 /** @return The sampling rate that we will resample the audio to */
778 Film::target_audio_sample_rate () const
780 if (!audio_stream()) {
784 /* Resample to a DCI-approved sample rate */
785 double t = dcp_audio_sample_rate (audio_stream()->sample_rate());
787 FrameRateConversion frc (source_frame_rate(), dcp_frame_rate());
789 /* Compensate if the DCP is being run at a different frame rate
790 to the source; that is, if the video is run such that it will
791 look different in the DCP compared to the source (slower or faster).
792 skip/repeat doesn't come into effect here.
795 if (frc.change_speed) {
796 t *= source_frame_rate() * frc.factor() / dcp_frame_rate();
803 Film::still_duration_in_frames () const
805 return still_duration() * source_frame_rate();
808 /** @return a DCI-compliant name for a DCP of this film */
810 Film::dci_name (bool if_created_now) const
814 string fixed_name = to_upper_copy (name());
815 for (size_t i = 0; i < fixed_name.length(); ++i) {
816 if (fixed_name[i] == ' ') {
821 /* Spec is that the name part should be maximum 14 characters, as I understand it */
822 if (fixed_name.length() > 14) {
823 fixed_name = fixed_name.substr (0, 14);
828 if (dcp_content_type()) {
829 d << "_" << dcp_content_type()->dci_name();
833 d << "_" << format()->dci_name();
836 DCIMetadata const dm = dci_metadata ();
838 if (!dm.audio_language.empty ()) {
839 d << "_" << dm.audio_language;
840 if (!dm.subtitle_language.empty()) {
841 d << "-" << dm.subtitle_language;
847 if (!dm.territory.empty ()) {
848 d << "_" << dm.territory;
849 if (!dm.rating.empty ()) {
850 d << "-" << dm.rating;
854 switch (audio_channels()) {
871 if (!dm.studio.empty ()) {
872 d << "_" << dm.studio;
875 if (if_created_now) {
876 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
878 d << "_" << boost::gregorian::to_iso_string (_dci_date);
881 if (!dm.facility.empty ()) {
882 d << "_" << dm.facility;
885 if (!dm.package_type.empty ()) {
886 d << "_" << dm.package_type;
892 /** @return name to give the DCP */
894 Film::dcp_name (bool if_created_now) const
896 if (use_dci_name()) {
897 return dci_name (if_created_now);
905 Film::set_directory (string d)
907 boost::mutex::scoped_lock lm (_state_mutex);
913 Film::set_name (string n)
916 boost::mutex::scoped_lock lm (_state_mutex);
919 signal_changed (NAME);
923 Film::set_use_dci_name (bool u)
926 boost::mutex::scoped_lock lm (_state_mutex);
929 signal_changed (USE_DCI_NAME);
933 Film::set_content (string c)
935 string check = directory ();
937 boost::filesystem::path slash ("/");
938 string platform_slash = slash.make_preferred().string ();
940 if (!ends_with (check, platform_slash)) {
941 check += platform_slash;
944 if (boost::filesystem::path(c).has_root_directory () && starts_with (c, check)) {
945 c = c.substr (_directory.length() + 1);
951 boost::mutex::scoped_lock lm (_state_mutex);
956 old_content = _content;
960 /* Do this before we start using FFmpeg ourselves */
961 run_ffprobe (c, file ("ffprobe.log"), _log);
963 /* Reset streams here in case the new content doesn't have one or the other */
964 _content_audio_stream = shared_ptr<AudioStream> ();
965 _subtitle_stream = shared_ptr<SubtitleStream> ();
967 /* Start off using content audio */
968 set_use_content_audio (true);
970 /* Create a temporary decoder so that we can get information
975 Decoders d = decoder_factory (shared_from_this(), DecodeOptions());
977 set_size (d.video->native_size ());
978 set_source_frame_rate (d.video->frames_per_second ());
979 set_dcp_frame_rate (best_dcp_frame_rate (source_frame_rate ()));
980 set_subtitle_streams (d.video->subtitle_streams ());
982 set_content_audio_streams (d.audio->audio_streams ());
986 boost::mutex::scoped_lock lm (_state_mutex);
990 signal_changed (CONTENT);
992 /* Start off with the first audio and subtitle streams */
993 if (d.audio && !d.audio->audio_streams().empty()) {
994 set_content_audio_stream (d.audio->audio_streams().front());
997 if (!d.video->subtitle_streams().empty()) {
998 set_subtitle_stream (d.video->subtitle_streams().front());
1005 boost::mutex::scoped_lock lm (_state_mutex);
1006 _content = old_content;
1011 /* Default format */
1012 set_format (Config::instance()->default_format ());
1014 /* Still image DCPs must use external audio */
1015 if (content_type() == STILL) {
1016 set_use_content_audio (false);
1021 Film::set_trust_content_header (bool t)
1024 boost::mutex::scoped_lock lm (_state_mutex);
1025 _trust_content_header = t;
1028 signal_changed (TRUST_CONTENT_HEADER);
1030 if (!_trust_content_header && !content().empty()) {
1031 /* We just said that we don't trust the content's header */
1037 Film::set_dcp_content_type (DCPContentType const * t)
1040 boost::mutex::scoped_lock lm (_state_mutex);
1041 _dcp_content_type = t;
1043 signal_changed (DCP_CONTENT_TYPE);
1047 Film::set_format (Format const * f)
1050 boost::mutex::scoped_lock lm (_state_mutex);
1053 signal_changed (FORMAT);
1057 Film::set_crop (Crop c)
1060 boost::mutex::scoped_lock lm (_state_mutex);
1063 signal_changed (CROP);
1067 Film::set_left_crop (int c)
1070 boost::mutex::scoped_lock lm (_state_mutex);
1072 if (_crop.left == c) {
1078 signal_changed (CROP);
1082 Film::set_right_crop (int c)
1085 boost::mutex::scoped_lock lm (_state_mutex);
1086 if (_crop.right == c) {
1092 signal_changed (CROP);
1096 Film::set_top_crop (int c)
1099 boost::mutex::scoped_lock lm (_state_mutex);
1100 if (_crop.top == c) {
1106 signal_changed (CROP);
1110 Film::set_bottom_crop (int c)
1113 boost::mutex::scoped_lock lm (_state_mutex);
1114 if (_crop.bottom == c) {
1120 signal_changed (CROP);
1124 Film::set_filters (vector<Filter const *> f)
1127 boost::mutex::scoped_lock lm (_state_mutex);
1130 signal_changed (FILTERS);
1134 Film::set_scaler (Scaler const * s)
1137 boost::mutex::scoped_lock lm (_state_mutex);
1140 signal_changed (SCALER);
1144 Film::set_trim_start (int t)
1147 boost::mutex::scoped_lock lm (_state_mutex);
1150 signal_changed (TRIM_START);
1154 Film::set_trim_end (int t)
1157 boost::mutex::scoped_lock lm (_state_mutex);
1160 signal_changed (TRIM_END);
1164 Film::set_trim_type (TrimType t)
1167 boost::mutex::scoped_lock lm (_state_mutex);
1170 signal_changed (TRIM_TYPE);
1174 Film::set_dcp_ab (bool a)
1177 boost::mutex::scoped_lock lm (_state_mutex);
1180 signal_changed (DCP_AB);
1184 Film::set_content_audio_stream (shared_ptr<AudioStream> s)
1187 boost::mutex::scoped_lock lm (_state_mutex);
1188 _content_audio_stream = s;
1190 signal_changed (CONTENT_AUDIO_STREAM);
1194 Film::set_external_audio (vector<string> a)
1197 boost::mutex::scoped_lock lm (_state_mutex);
1198 _external_audio = a;
1201 shared_ptr<SndfileDecoder> decoder (new SndfileDecoder (shared_from_this(), DecodeOptions()));
1202 if (decoder->audio_stream()) {
1203 _sndfile_stream = decoder->audio_stream ();
1206 signal_changed (EXTERNAL_AUDIO);
1210 Film::set_use_content_audio (bool e)
1213 boost::mutex::scoped_lock lm (_state_mutex);
1214 _use_content_audio = e;
1217 signal_changed (USE_CONTENT_AUDIO);
1221 Film::set_audio_gain (float g)
1224 boost::mutex::scoped_lock lm (_state_mutex);
1227 signal_changed (AUDIO_GAIN);
1231 Film::set_audio_delay (int d)
1234 boost::mutex::scoped_lock lm (_state_mutex);
1237 signal_changed (AUDIO_DELAY);
1241 Film::set_still_duration (int d)
1244 boost::mutex::scoped_lock lm (_state_mutex);
1245 _still_duration = d;
1247 signal_changed (STILL_DURATION);
1251 Film::set_subtitle_stream (shared_ptr<SubtitleStream> s)
1254 boost::mutex::scoped_lock lm (_state_mutex);
1255 _subtitle_stream = s;
1257 signal_changed (SUBTITLE_STREAM);
1261 Film::set_with_subtitles (bool w)
1264 boost::mutex::scoped_lock lm (_state_mutex);
1265 _with_subtitles = w;
1267 signal_changed (WITH_SUBTITLES);
1271 Film::set_subtitle_offset (int o)
1274 boost::mutex::scoped_lock lm (_state_mutex);
1275 _subtitle_offset = o;
1277 signal_changed (SUBTITLE_OFFSET);
1281 Film::set_subtitle_scale (float s)
1284 boost::mutex::scoped_lock lm (_state_mutex);
1285 _subtitle_scale = s;
1287 signal_changed (SUBTITLE_SCALE);
1291 Film::set_colour_lut (int i)
1294 boost::mutex::scoped_lock lm (_state_mutex);
1297 signal_changed (COLOUR_LUT);
1301 Film::set_j2k_bandwidth (int b)
1304 boost::mutex::scoped_lock lm (_state_mutex);
1307 signal_changed (J2K_BANDWIDTH);
1311 Film::set_dci_metadata (DCIMetadata m)
1314 boost::mutex::scoped_lock lm (_state_mutex);
1317 signal_changed (DCI_METADATA);
1322 Film::set_dcp_frame_rate (int f)
1325 boost::mutex::scoped_lock lm (_state_mutex);
1326 _dcp_frame_rate = f;
1328 signal_changed (DCP_FRAME_RATE);
1332 Film::set_minimum_audio_channels (int c)
1335 boost::mutex::scoped_lock lm (_state_mutex);
1336 _minimum_audio_channels = c;
1338 signal_changed (MINIMUM_AUDIO_CHANNELS);
1342 Film::set_size (libdcp::Size s)
1345 boost::mutex::scoped_lock lm (_state_mutex);
1348 signal_changed (SIZE);
1352 Film::set_length (SourceFrame l)
1355 boost::mutex::scoped_lock lm (_state_mutex);
1358 signal_changed (LENGTH);
1362 Film::unset_length ()
1365 boost::mutex::scoped_lock lm (_state_mutex);
1366 _length = boost::none;
1368 signal_changed (LENGTH);
1372 Film::set_content_digest (string d)
1375 boost::mutex::scoped_lock lm (_state_mutex);
1376 _content_digest = d;
1382 Film::set_content_audio_streams (vector<shared_ptr<AudioStream> > s)
1385 boost::mutex::scoped_lock lm (_state_mutex);
1386 _content_audio_streams = s;
1388 signal_changed (CONTENT_AUDIO_STREAMS);
1392 Film::set_subtitle_streams (vector<shared_ptr<SubtitleStream> > s)
1395 boost::mutex::scoped_lock lm (_state_mutex);
1396 _subtitle_streams = s;
1398 signal_changed (SUBTITLE_STREAMS);
1402 Film::set_source_frame_rate (float f)
1405 boost::mutex::scoped_lock lm (_state_mutex);
1406 _source_frame_rate = f;
1408 signal_changed (SOURCE_FRAME_RATE);
1412 Film::signal_changed (Property p)
1415 boost::mutex::scoped_lock lm (_state_mutex);
1420 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
1425 Film::audio_channels () const
1427 shared_ptr<AudioStream> s = audio_stream ();
1432 return s->channels ();
1436 Film::set_dci_date_today ()
1438 _dci_date = boost::gregorian::day_clock::local_day ();
1441 boost::shared_ptr<AudioStream>
1442 Film::audio_stream () const
1444 if (use_content_audio()) {
1445 return _content_audio_stream;
1448 return _sndfile_stream;
1452 Film::info_path (int f) const
1454 boost::filesystem::path p;
1459 s << setfill('0') << f << ".md5";
1463 /* info_dir() will already have added any initial bit of the path,
1464 so don't call file() on this.
1470 Film::j2c_path (int f, bool t) const
1472 boost::filesystem::path p;
1474 p /= video_state_identifier ();
1478 s << setfill('0') << f << ".j2c";
1485 return file (p.string ());
1488 /** Make an educated guess as to whether we have a complete DCP
1490 * @return true if we do.
1494 Film::have_dcp () const
1497 libdcp::DCP dcp (dir (dcp_name()));
1507 Film::has_audio () const
1509 if (use_content_audio()) {
1510 return audio_stream();
1513 vector<string> const e = external_audio ();
1514 for (vector<string>::const_iterator i = e.begin(); i != e.end(); ++i) {