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>
37 #include "container.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 , _container (Config::instance()->default_container ())
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 , _container (o._container)
164 , _filters (o._filters)
165 , _scaler (o._scaler)
167 , _audio_gain (o._audio_gain)
168 , _audio_delay (o._audio_delay)
169 , _with_subtitles (o._with_subtitles)
170 , _subtitle_offset (o._subtitle_offset)
171 , _subtitle_scale (o._subtitle_scale)
172 , _colour_lut (o._colour_lut)
173 , _j2k_bandwidth (o._j2k_bandwidth)
174 , _dci_metadata (o._dci_metadata)
175 , _dcp_video_frame_rate (o._dcp_video_frame_rate)
176 , _dci_date (o._dci_date)
179 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
183 Film::video_state_identifier () const
185 assert (container ());
188 pair<string, string> f = Filter::ffmpeg_strings (filters());
191 s << container()->id()
192 << "_" << _playlist->video_digest()
193 << "_" << _dcp_video_frame_rate
194 << "_" << f.first << "_" << f.second
195 << "_" << scaler()->id()
196 << "_" << j2k_bandwidth()
197 << "_" << boost::lexical_cast<int> (colour_lut());
200 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
201 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
207 /** @return The path to the directory to write video frame info files to */
209 Film::info_dir () const
211 boost::filesystem::path p;
213 p /= video_state_identifier ();
214 return dir (p.string());
218 Film::internal_video_mxf_dir () const
220 boost::filesystem::path p;
221 return dir ("video");
225 Film::internal_video_mxf_filename () const
227 return video_state_identifier() + ".mxf";
231 Film::dcp_video_mxf_filename () const
233 return filename_safe_name() + "_video.mxf";
237 Film::dcp_audio_mxf_filename () const
239 return filename_safe_name() + "_audio.mxf";
243 Film::filename_safe_name () const
245 string const n = name ();
247 for (size_t i = 0; i < n.length(); ++i) {
248 if (isalnum (n[i])) {
259 Film::audio_analysis_path () const
261 boost::filesystem::path p;
263 p /= _playlist->audio_digest();
264 return file (p.string ());
267 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
271 set_dci_date_today ();
273 if (dcp_name().find ("/") != string::npos) {
274 throw BadSettingError (_("name"), _("cannot contain slashes"));
277 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
281 gethostname (buffer, sizeof (buffer));
282 log()->log (String::compose ("Starting to make DCP on %1", buffer));
285 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
287 // log()->log (String::compose ("Content length %1", length().get()));
289 // log()->log (String::compose ("Content digest %1", content_digest()));
290 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
291 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
292 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
293 #ifdef DCPOMATIC_DEBUG
294 log()->log ("DCP-o-matic built in debug mode.");
296 log()->log ("DCP-o-matic built in optimised mode.");
299 log()->log ("libdcp built in debug mode.");
301 log()->log ("libdcp built in optimised mode.");
303 pair<string, int> const c = cpu_info ();
304 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
306 if (container() == 0) {
307 throw MissingSettingError (_("container"));
310 if (_playlist->content().empty ()) {
311 throw StringError (_("You must add some content to the DCP before creating it"));
314 if (dcp_content_type() == 0) {
315 throw MissingSettingError (_("content type"));
318 if (name().empty()) {
319 throw MissingSettingError (_("name"));
325 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
327 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
331 /** Start a job to analyse the audio in our Playlist */
333 Film::analyse_audio ()
335 if (_analyse_audio_job) {
339 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
340 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
341 JobManager::instance()->add (_analyse_audio_job);
344 /** Start a job to examine a piece of content */
346 Film::examine_content (shared_ptr<Content> c)
348 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
349 JobManager::instance()->add (j);
353 Film::analyse_audio_finished ()
357 if (_analyse_audio_job->finished_ok ()) {
358 AudioAnalysisSucceeded ();
361 _analyse_audio_job.reset ();
364 /** Start a job to send our DCP to the configured TMS */
366 Film::send_dcp_to_tms ()
368 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
369 JobManager::instance()->add (j);
372 /** Count the number of frames that have been encoded for this film.
373 * @return frame count.
376 Film::encoded_frames () const
378 if (container() == 0) {
383 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
385 boost::this_thread::interruption_point ();
391 /** Write state to our `metadata' file */
393 Film::write_metadata () const
395 boost::mutex::scoped_lock lm (_state_mutex);
398 boost::filesystem::create_directories (directory());
401 xmlpp::Element* root = doc.create_root_node ("Metadata");
403 root->add_child("Version")->add_child_text (boost::lexical_cast<string> (state_version));
404 root->add_child("Name")->add_child_text (_name);
405 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
407 if (_dcp_content_type) {
408 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
412 root->add_child("Container")->add_child_text (_container->id ());
415 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
416 root->add_child("Filter")->add_child_text ((*i)->id ());
419 root->add_child("Scaler")->add_child_text (_scaler->id ());
420 root->add_child("AB")->add_child_text (_ab ? "1" : "0");
421 root->add_child("AudioGain")->add_child_text (boost::lexical_cast<string> (_audio_gain));
422 root->add_child("AudioDelay")->add_child_text (boost::lexical_cast<string> (_audio_delay));
423 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
424 root->add_child("SubtitleOffset")->add_child_text (boost::lexical_cast<string> (_subtitle_offset));
425 root->add_child("SubtitleScale")->add_child_text (boost::lexical_cast<string> (_subtitle_scale));
426 root->add_child("ColourLUT")->add_child_text (boost::lexical_cast<string> (_colour_lut));
427 root->add_child("J2KBandwidth")->add_child_text (boost::lexical_cast<string> (_j2k_bandwidth));
428 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
429 root->add_child("DCPVideoFrameRate")->add_child_text (boost::lexical_cast<string> (_dcp_video_frame_rate));
430 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
431 _playlist->as_xml (root->add_child ("Playlist"));
433 doc.write_to_file_formatted (file ("metadata.xml"));
438 /** Read state from our metadata file */
440 Film::read_metadata ()
442 boost::mutex::scoped_lock lm (_state_mutex);
445 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
446 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!"));
449 cxml::File f (file ("metadata.xml"), "Metadata");
451 _name = f.string_child ("Name");
452 _use_dci_name = f.bool_child ("UseDCIName");
455 optional<string> c = f.optional_string_child ("DCPContentType");
457 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
462 optional<string> c = f.optional_string_child ("Container");
464 _container = Container::from_id (c.get ());
469 list<shared_ptr<cxml::Node> > c = f.node_children ("Filter");
470 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
471 _filters.push_back (Filter::from_id ((*i)->content ()));
475 _scaler = Scaler::from_id (f.string_child ("Scaler"));
476 _ab = f.bool_child ("AB");
477 _audio_gain = f.number_child<float> ("AudioGain");
478 _audio_delay = f.number_child<int> ("AudioDelay");
479 _with_subtitles = f.bool_child ("WithSubtitles");
480 _subtitle_offset = f.number_child<float> ("SubtitleOffset");
481 _subtitle_scale = f.number_child<float> ("SubtitleScale");
482 _colour_lut = f.number_child<int> ("ColourLUT");
483 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
484 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
485 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
486 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
488 _playlist->set_from_xml (f.node_child ("Playlist"));
493 /** Given a directory name, return its full path within the Film's directory.
494 * The directory (and its parents) will be created if they do not exist.
497 Film::dir (string d) const
499 boost::mutex::scoped_lock lm (_directory_mutex);
501 boost::filesystem::path p;
505 boost::filesystem::create_directories (p);
510 /** Given a file or directory name, return its full path within the Film's directory.
511 * _directory_mutex must not be locked on entry.
512 * Any required parent directories will be created.
515 Film::file (string f) const
517 boost::mutex::scoped_lock lm (_directory_mutex);
519 boost::filesystem::path p;
523 boost::filesystem::create_directories (p.parent_path ());
528 /** @return a DCI-compliant name for a DCP of this film */
530 Film::dci_name (bool if_created_now) const
534 string fixed_name = to_upper_copy (name());
535 for (size_t i = 0; i < fixed_name.length(); ++i) {
536 if (fixed_name[i] == ' ') {
541 /* Spec is that the name part should be maximum 14 characters, as I understand it */
542 if (fixed_name.length() > 14) {
543 fixed_name = fixed_name.substr (0, 14);
548 if (dcp_content_type()) {
549 d << "_" << dcp_content_type()->dci_name();
553 d << "_" << container()->dci_name();
556 DCIMetadata const dm = dci_metadata ();
558 if (!dm.audio_language.empty ()) {
559 d << "_" << dm.audio_language;
560 if (!dm.subtitle_language.empty()) {
561 d << "-" << dm.subtitle_language;
567 if (!dm.territory.empty ()) {
568 d << "_" << dm.territory;
569 if (!dm.rating.empty ()) {
570 d << "-" << dm.rating;
576 if (!dm.studio.empty ()) {
577 d << "_" << dm.studio;
580 if (if_created_now) {
581 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
583 d << "_" << boost::gregorian::to_iso_string (_dci_date);
586 if (!dm.facility.empty ()) {
587 d << "_" << dm.facility;
590 if (!dm.package_type.empty ()) {
591 d << "_" << dm.package_type;
597 /** @return name to give the DCP */
599 Film::dcp_name (bool if_created_now) const
601 if (use_dci_name()) {
602 return dci_name (if_created_now);
610 Film::set_directory (string d)
612 boost::mutex::scoped_lock lm (_state_mutex);
618 Film::set_name (string n)
621 boost::mutex::scoped_lock lm (_state_mutex);
624 signal_changed (NAME);
628 Film::set_use_dci_name (bool u)
631 boost::mutex::scoped_lock lm (_state_mutex);
634 signal_changed (USE_DCI_NAME);
638 Film::set_dcp_content_type (DCPContentType const * t)
641 boost::mutex::scoped_lock lm (_state_mutex);
642 _dcp_content_type = t;
644 signal_changed (DCP_CONTENT_TYPE);
648 Film::set_container (Container const * c)
651 boost::mutex::scoped_lock lm (_state_mutex);
654 signal_changed (CONTAINER);
658 Film::set_filters (vector<Filter const *> f)
661 boost::mutex::scoped_lock lm (_state_mutex);
664 signal_changed (FILTERS);
668 Film::set_scaler (Scaler const * s)
671 boost::mutex::scoped_lock lm (_state_mutex);
674 signal_changed (SCALER);
678 Film::set_ab (bool a)
681 boost::mutex::scoped_lock lm (_state_mutex);
688 Film::set_audio_gain (float g)
691 boost::mutex::scoped_lock lm (_state_mutex);
694 signal_changed (AUDIO_GAIN);
698 Film::set_audio_delay (int d)
701 boost::mutex::scoped_lock lm (_state_mutex);
704 signal_changed (AUDIO_DELAY);
708 Film::set_with_subtitles (bool w)
711 boost::mutex::scoped_lock lm (_state_mutex);
714 signal_changed (WITH_SUBTITLES);
718 Film::set_subtitle_offset (int o)
721 boost::mutex::scoped_lock lm (_state_mutex);
722 _subtitle_offset = o;
724 signal_changed (SUBTITLE_OFFSET);
728 Film::set_subtitle_scale (float s)
731 boost::mutex::scoped_lock lm (_state_mutex);
734 signal_changed (SUBTITLE_SCALE);
738 Film::set_colour_lut (int i)
741 boost::mutex::scoped_lock lm (_state_mutex);
744 signal_changed (COLOUR_LUT);
748 Film::set_j2k_bandwidth (int b)
751 boost::mutex::scoped_lock lm (_state_mutex);
754 signal_changed (J2K_BANDWIDTH);
758 Film::set_dci_metadata (DCIMetadata m)
761 boost::mutex::scoped_lock lm (_state_mutex);
764 signal_changed (DCI_METADATA);
769 Film::set_dcp_video_frame_rate (int f)
772 boost::mutex::scoped_lock lm (_state_mutex);
773 _dcp_video_frame_rate = f;
775 signal_changed (DCP_VIDEO_FRAME_RATE);
779 Film::signal_changed (Property p)
782 boost::mutex::scoped_lock lm (_state_mutex);
788 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
795 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
800 Film::set_dci_date_today ()
802 _dci_date = boost::gregorian::day_clock::local_day ();
806 Film::info_path (int f) const
808 boost::filesystem::path p;
813 s << setfill('0') << f << ".md5";
817 /* info_dir() will already have added any initial bit of the path,
818 so don't call file() on this.
824 Film::j2c_path (int f, bool t) const
826 boost::filesystem::path p;
828 p /= video_state_identifier ();
832 s << setfill('0') << f << ".j2c";
839 return file (p.string ());
842 /** Make an educated guess as to whether we have a complete DCP
844 * @return true if we do.
848 Film::have_dcp () const
851 libdcp::DCP dcp (dir (dcp_name()));
861 Film::player () const
863 boost::mutex::scoped_lock lm (_state_mutex);
864 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
868 Film::playlist () const
870 boost::mutex::scoped_lock lm (_state_mutex);
874 Playlist::ContentList
875 Film::content () const
877 return _playlist->content ();
881 Film::add_content (shared_ptr<Content> c)
888 Film::remove_content (shared_ptr<Content> c)
890 _playlist->remove (c);
894 Film::length () const
896 return _playlist->length (shared_from_this ());
900 Film::has_subtitles () const
902 return _playlist->has_subtitles ();
906 Film::best_dcp_video_frame_rate () const
908 return _playlist->best_dcp_frame_rate ();
912 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
914 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
915 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
919 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
924 Film::playlist_changed ()
926 signal_changed (CONTENT);
932 return _playlist->loop ();
936 Film::set_loop (int c)
938 _playlist->set_loop (c);
942 Film::time_to_audio_frames (Time t) const
944 return t * dcp_audio_frame_rate () / TIME_HZ;
948 Film::time_to_video_frames (Time t) const
950 return t * dcp_video_frame_rate () / TIME_HZ;
954 Film::audio_frames_to_time (OutputAudioFrame f) const
956 return f * TIME_HZ / dcp_audio_frame_rate ();
960 Film::video_frames_to_time (OutputVideoFrame f) const
962 return f * TIME_HZ / dcp_video_frame_rate ();
966 Film::dcp_audio_frame_rate () const