1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
4 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <boost/filesystem.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/lexical_cast.hpp>
33 #include <boost/date_time.hpp>
34 #include <libxml++/libxml++.h>
35 #include <libcxml/cxml.h>
41 #include "job_manager.h"
42 #include "ab_transcode_job.h"
43 #include "transcode_job.h"
44 #include "scp_dcp_job.h"
46 #include "exceptions.h"
47 #include "examine_content_job.h"
51 #include "ui_signaller.h"
52 #include "analyse_audio_job.h"
55 #include "ffmpeg_content.h"
56 #include "imagemagick_content.h"
57 #include "sndfile_content.h"
58 #include "dcp_content_type.h"
63 using std::stringstream;
76 using boost::shared_ptr;
77 using boost::lexical_cast;
78 using boost::to_upper_copy;
79 using boost::ends_with;
80 using boost::starts_with;
81 using boost::optional;
84 int const Film::state_version = 4;
86 /** Construct a Film object in a given directory, reading any metadata
87 * file that exists in that directory. An exception will be thrown if
88 * must_exist is true and the specified directory does not exist.
90 * @param d Film directory.
91 * @param must_exist true to throw an exception if does not exist.
94 Film::Film (string d, bool must_exist)
95 : _playlist (new Playlist)
96 , _use_dci_name (true)
97 , _dcp_content_type (Config::instance()->default_dcp_content_type ())
98 , _format (Config::instance()->default_format ())
99 , _scaler (Scaler::from_id ("bicubic"))
103 , _with_subtitles (false)
104 , _subtitle_offset (0)
105 , _subtitle_scale (1)
107 , _j2k_bandwidth (200000000)
108 , _dci_metadata (Config::instance()->default_dci_metadata ())
109 , _dcp_video_frame_rate (0)
112 set_dci_date_today ();
114 _playlist->Changed.connect (bind (&Film::playlist_changed, this));
115 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
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());
151 _log.reset (new FileLog (file ("log")));
154 Film::Film (Film const & o)
155 : boost::enable_shared_from_this<Film> (o)
156 /* note: the copied film shares the original's log */
158 , _playlist (new Playlist (o._playlist))
159 , _directory (o._directory)
161 , _use_dci_name (o._use_dci_name)
162 , _dcp_content_type (o._dcp_content_type)
163 , _format (o._format)
165 , _filters (o._filters)
166 , _scaler (o._scaler)
168 , _audio_gain (o._audio_gain)
169 , _audio_delay (o._audio_delay)
170 , _with_subtitles (o._with_subtitles)
171 , _subtitle_offset (o._subtitle_offset)
172 , _subtitle_scale (o._subtitle_scale)
173 , _colour_lut (o._colour_lut)
174 , _j2k_bandwidth (o._j2k_bandwidth)
175 , _dci_metadata (o._dci_metadata)
176 , _dcp_video_frame_rate (o._dcp_video_frame_rate)
177 , _dci_date (o._dci_date)
180 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
184 Film::video_state_identifier () const
189 pair<string, string> f = Filter::ffmpeg_strings (filters());
193 << "_" << _playlist->video_digest()
194 << "_" << crop().left << "_" << crop().right << "_" << crop().top << "_" << crop().bottom
195 << "_" << _dcp_video_frame_rate
196 << "_" << f.first << "_" << f.second
197 << "_" << scaler()->id()
198 << "_" << j2k_bandwidth()
199 << "_" << boost::lexical_cast<int> (colour_lut());
202 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
203 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
209 /** @return The path to the directory to write video frame info files to */
211 Film::info_dir () const
213 boost::filesystem::path p;
215 p /= video_state_identifier ();
216 return dir (p.string());
220 Film::internal_video_mxf_dir () const
222 boost::filesystem::path p;
223 return dir ("video");
227 Film::internal_video_mxf_filename () const
229 return video_state_identifier() + ".mxf";
233 Film::dcp_video_mxf_filename () const
235 return filename_safe_name() + "_video.mxf";
239 Film::dcp_audio_mxf_filename () const
241 return filename_safe_name() + "_audio.mxf";
245 Film::filename_safe_name () const
247 string const n = name ();
249 for (size_t i = 0; i < n.length(); ++i) {
250 if (isalnum (n[i])) {
261 Film::audio_analysis_path () const
263 boost::filesystem::path p;
265 p /= _playlist->audio_digest();
266 return file (p.string ());
269 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
273 set_dci_date_today ();
275 if (dcp_name().find ("/") != string::npos) {
276 throw BadSettingError (_("name"), _("cannot contain slashes"));
279 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
283 gethostname (buffer, sizeof (buffer));
284 log()->log (String::compose ("Starting to make DCP on %1", buffer));
287 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
289 // log()->log (String::compose ("Content length %1", length().get()));
291 // log()->log (String::compose ("Content digest %1", content_digest()));
292 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
293 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
294 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
295 #ifdef DCPOMATIC_DEBUG
296 log()->log ("DCP-o-matic built in debug mode.");
298 log()->log ("DCP-o-matic built in optimised mode.");
301 log()->log ("libdcp built in debug mode.");
303 log()->log ("libdcp built in optimised mode.");
305 pair<string, int> const c = cpu_info ();
306 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
309 throw MissingSettingError (_("format"));
312 if (_playlist->content().empty ()) {
313 throw StringError (_("You must add some content to the DCP before creating it"));
316 if (dcp_content_type() == 0) {
317 throw MissingSettingError (_("content type"));
320 if (name().empty()) {
321 throw MissingSettingError (_("name"));
327 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
329 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
333 /** Start a job to analyse the audio in our Playlist */
335 Film::analyse_audio ()
337 if (_analyse_audio_job) {
341 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
342 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
343 JobManager::instance()->add (_analyse_audio_job);
346 /** Start a job to examine a piece of content */
348 Film::examine_content (shared_ptr<Content> c)
350 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
351 JobManager::instance()->add (j);
355 Film::analyse_audio_finished ()
359 if (_analyse_audio_job->finished_ok ()) {
360 AudioAnalysisSucceeded ();
363 _analyse_audio_job.reset ();
366 /** Start a job to send our DCP to the configured TMS */
368 Film::send_dcp_to_tms ()
370 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
371 JobManager::instance()->add (j);
374 /** Count the number of frames that have been encoded for this film.
375 * @return frame count.
378 Film::encoded_frames () const
385 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
387 boost::this_thread::interruption_point ();
393 /** Write state to our `metadata' file */
395 Film::write_metadata () const
397 boost::mutex::scoped_lock lm (_state_mutex);
400 boost::filesystem::create_directories (directory());
403 xmlpp::Element* root = doc.create_root_node ("Metadata");
405 root->add_child("Version")->add_child_text (boost::lexical_cast<string> (state_version));
406 root->add_child("Name")->add_child_text (_name);
407 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
409 if (_dcp_content_type) {
410 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
414 root->add_child("Format")->add_child_text (_format->id ());
417 root->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
418 root->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
419 root->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
420 root->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
422 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
423 root->add_child("Filter")->add_child_text ((*i)->id ());
426 root->add_child("Scaler")->add_child_text (_scaler->id ());
427 root->add_child("AB")->add_child_text (_ab ? "1" : "0");
428 root->add_child("AudioGain")->add_child_text (boost::lexical_cast<string> (_audio_gain));
429 root->add_child("AudioDelay")->add_child_text (boost::lexical_cast<string> (_audio_delay));
430 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
431 root->add_child("SubtitleOffset")->add_child_text (boost::lexical_cast<string> (_subtitle_offset));
432 root->add_child("SubtitleScale")->add_child_text (boost::lexical_cast<string> (_subtitle_scale));
433 root->add_child("ColourLUT")->add_child_text (boost::lexical_cast<string> (_colour_lut));
434 root->add_child("J2KBandwidth")->add_child_text (boost::lexical_cast<string> (_j2k_bandwidth));
435 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
436 root->add_child("DCPVideoFrameRate")->add_child_text (boost::lexical_cast<string> (_dcp_video_frame_rate));
437 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
438 _playlist->as_xml (root->add_child ("Playlist"));
440 doc.write_to_file_formatted (file ("metadata.xml"));
445 /** Read state from our metadata file */
447 Film::read_metadata ()
449 boost::mutex::scoped_lock lm (_state_mutex);
452 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
453 throw StringError (_("This film was created with an older version of DCP-o-matic, and unfortunately it cannot be loaded into this version. You will need to create a new Film, re-add your content and set it up again. Sorry!"));
456 cxml::File f (file ("metadata.xml"), "Metadata");
458 _name = f.string_child ("Name");
459 _use_dci_name = f.bool_child ("UseDCIName");
462 optional<string> c = f.optional_string_child ("DCPContentType");
464 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
469 optional<string> c = f.optional_string_child ("Format");
471 _format = Format::from_id (c.get ());
475 _crop.left = f.number_child<int> ("LeftCrop");
476 _crop.right = f.number_child<int> ("RightCrop");
477 _crop.top = f.number_child<int> ("TopCrop");
478 _crop.bottom = f.number_child<int> ("BottomCrop");
481 list<shared_ptr<cxml::Node> > c = f.node_children ("Filter");
482 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
483 _filters.push_back (Filter::from_id ((*i)->content ()));
487 _scaler = Scaler::from_id (f.string_child ("Scaler"));
488 _ab = f.bool_child ("AB");
489 _audio_gain = f.number_child<float> ("AudioGain");
490 _audio_delay = f.number_child<int> ("AudioDelay");
491 _with_subtitles = f.bool_child ("WithSubtitles");
492 _subtitle_offset = f.number_child<float> ("SubtitleOffset");
493 _subtitle_scale = f.number_child<float> ("SubtitleScale");
494 _colour_lut = f.number_child<int> ("ColourLUT");
495 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
496 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
497 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
498 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
500 _playlist->set_from_xml (f.node_child ("Playlist"));
506 Film::cropped_size (libdcp::Size s) const
508 boost::mutex::scoped_lock lm (_state_mutex);
509 s.width -= _crop.left + _crop.right;
510 s.height -= _crop.top + _crop.bottom;
514 /** Given a directory name, return its full path within the Film's directory.
515 * The directory (and its parents) will be created if they do not exist.
518 Film::dir (string d) const
520 boost::mutex::scoped_lock lm (_directory_mutex);
522 boost::filesystem::path p;
526 boost::filesystem::create_directories (p);
531 /** Given a file or directory name, return its full path within the Film's directory.
532 * _directory_mutex must not be locked on entry.
533 * Any required parent directories will be created.
536 Film::file (string f) const
538 boost::mutex::scoped_lock lm (_directory_mutex);
540 boost::filesystem::path p;
544 boost::filesystem::create_directories (p.parent_path ());
549 /** @return a DCI-compliant name for a DCP of this film */
551 Film::dci_name (bool if_created_now) const
555 string fixed_name = to_upper_copy (name());
556 for (size_t i = 0; i < fixed_name.length(); ++i) {
557 if (fixed_name[i] == ' ') {
562 /* Spec is that the name part should be maximum 14 characters, as I understand it */
563 if (fixed_name.length() > 14) {
564 fixed_name = fixed_name.substr (0, 14);
569 if (dcp_content_type()) {
570 d << "_" << dcp_content_type()->dci_name();
574 d << "_" << format()->dci_name();
577 DCIMetadata const dm = dci_metadata ();
579 if (!dm.audio_language.empty ()) {
580 d << "_" << dm.audio_language;
581 if (!dm.subtitle_language.empty()) {
582 d << "-" << dm.subtitle_language;
588 if (!dm.territory.empty ()) {
589 d << "_" << dm.territory;
590 if (!dm.rating.empty ()) {
591 d << "-" << dm.rating;
597 if (!dm.studio.empty ()) {
598 d << "_" << dm.studio;
601 if (if_created_now) {
602 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
604 d << "_" << boost::gregorian::to_iso_string (_dci_date);
607 if (!dm.facility.empty ()) {
608 d << "_" << dm.facility;
611 if (!dm.package_type.empty ()) {
612 d << "_" << dm.package_type;
618 /** @return name to give the DCP */
620 Film::dcp_name (bool if_created_now) const
622 if (use_dci_name()) {
623 return dci_name (if_created_now);
631 Film::set_directory (string d)
633 boost::mutex::scoped_lock lm (_state_mutex);
639 Film::set_name (string n)
642 boost::mutex::scoped_lock lm (_state_mutex);
645 signal_changed (NAME);
649 Film::set_use_dci_name (bool u)
652 boost::mutex::scoped_lock lm (_state_mutex);
655 signal_changed (USE_DCI_NAME);
659 Film::set_dcp_content_type (DCPContentType const * t)
662 boost::mutex::scoped_lock lm (_state_mutex);
663 _dcp_content_type = t;
665 signal_changed (DCP_CONTENT_TYPE);
669 Film::set_format (Format const * f)
672 boost::mutex::scoped_lock lm (_state_mutex);
675 signal_changed (FORMAT);
679 Film::set_crop (Crop c)
682 boost::mutex::scoped_lock lm (_state_mutex);
685 signal_changed (CROP);
689 Film::set_left_crop (int c)
692 boost::mutex::scoped_lock lm (_state_mutex);
694 if (_crop.left == c) {
700 signal_changed (CROP);
704 Film::set_right_crop (int c)
707 boost::mutex::scoped_lock lm (_state_mutex);
708 if (_crop.right == c) {
714 signal_changed (CROP);
718 Film::set_top_crop (int c)
721 boost::mutex::scoped_lock lm (_state_mutex);
722 if (_crop.top == c) {
728 signal_changed (CROP);
732 Film::set_bottom_crop (int c)
735 boost::mutex::scoped_lock lm (_state_mutex);
736 if (_crop.bottom == c) {
742 signal_changed (CROP);
746 Film::set_filters (vector<Filter const *> f)
749 boost::mutex::scoped_lock lm (_state_mutex);
752 signal_changed (FILTERS);
756 Film::set_scaler (Scaler const * s)
759 boost::mutex::scoped_lock lm (_state_mutex);
762 signal_changed (SCALER);
766 Film::set_ab (bool a)
769 boost::mutex::scoped_lock lm (_state_mutex);
776 Film::set_audio_gain (float g)
779 boost::mutex::scoped_lock lm (_state_mutex);
782 signal_changed (AUDIO_GAIN);
786 Film::set_audio_delay (int d)
789 boost::mutex::scoped_lock lm (_state_mutex);
792 signal_changed (AUDIO_DELAY);
796 Film::set_with_subtitles (bool w)
799 boost::mutex::scoped_lock lm (_state_mutex);
802 signal_changed (WITH_SUBTITLES);
806 Film::set_subtitle_offset (int o)
809 boost::mutex::scoped_lock lm (_state_mutex);
810 _subtitle_offset = o;
812 signal_changed (SUBTITLE_OFFSET);
816 Film::set_subtitle_scale (float s)
819 boost::mutex::scoped_lock lm (_state_mutex);
822 signal_changed (SUBTITLE_SCALE);
826 Film::set_colour_lut (int i)
829 boost::mutex::scoped_lock lm (_state_mutex);
832 signal_changed (COLOUR_LUT);
836 Film::set_j2k_bandwidth (int b)
839 boost::mutex::scoped_lock lm (_state_mutex);
842 signal_changed (J2K_BANDWIDTH);
846 Film::set_dci_metadata (DCIMetadata m)
849 boost::mutex::scoped_lock lm (_state_mutex);
852 signal_changed (DCI_METADATA);
857 Film::set_dcp_video_frame_rate (int f)
860 boost::mutex::scoped_lock lm (_state_mutex);
861 _dcp_video_frame_rate = f;
863 signal_changed (DCP_VIDEO_FRAME_RATE);
867 Film::signal_changed (Property p)
870 boost::mutex::scoped_lock lm (_state_mutex);
876 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
883 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
888 Film::set_dci_date_today ()
890 _dci_date = boost::gregorian::day_clock::local_day ();
894 Film::info_path (int f) const
896 boost::filesystem::path p;
901 s << setfill('0') << f << ".md5";
905 /* info_dir() will already have added any initial bit of the path,
906 so don't call file() on this.
912 Film::j2c_path (int f, bool t) const
914 boost::filesystem::path p;
916 p /= video_state_identifier ();
920 s << setfill('0') << f << ".j2c";
927 return file (p.string ());
930 /** Make an educated guess as to whether we have a complete DCP
932 * @return true if we do.
936 Film::have_dcp () const
939 libdcp::DCP dcp (dir (dcp_name()));
949 Film::player () const
951 boost::mutex::scoped_lock lm (_state_mutex);
952 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
956 Film::playlist () const
958 boost::mutex::scoped_lock lm (_state_mutex);
962 Playlist::ContentList
963 Film::content () const
965 return _playlist->content ();
969 Film::add_content (shared_ptr<Content> c)
976 Film::remove_content (shared_ptr<Content> c)
978 _playlist->remove (c);
982 Film::length () const
984 return _playlist->length (shared_from_this ());
988 Film::has_subtitles () const
990 return _playlist->has_subtitles ();
994 Film::best_dcp_video_frame_rate () const
996 return _playlist->best_dcp_frame_rate ();
1000 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
1002 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
1003 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
1007 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
1012 Film::playlist_changed ()
1014 signal_changed (CONTENT);
1020 return _playlist->loop ();
1024 Film::set_loop (int c)
1026 _playlist->set_loop (c);
1030 Film::time_to_audio_frames (Time t) const
1032 return t * dcp_audio_frame_rate () / TIME_HZ;
1036 Film::time_to_video_frames (Time t) const
1038 return t * dcp_video_frame_rate () / TIME_HZ;
1042 Film::audio_frames_to_time (OutputAudioFrame f) const
1044 return f * TIME_HZ / dcp_audio_frame_rate ();
1048 Film::video_frames_to_time (OutputVideoFrame f) const
1050 return f * TIME_HZ / dcp_video_frame_rate ();
1054 Film::dcp_audio_frame_rate () const