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.
88 * @param d Film directory.
92 : _playlist (new Playlist)
93 , _use_dci_name (true)
94 , _dcp_content_type (Config::instance()->default_dcp_content_type ())
95 , _container (Config::instance()->default_container ())
96 , _scaler (Scaler::from_id ("bicubic"))
98 , _with_subtitles (false)
99 , _subtitle_offset (0)
100 , _subtitle_scale (1)
102 , _j2k_bandwidth (200000000)
103 , _dci_metadata (Config::instance()->default_dci_metadata ())
104 , _dcp_video_frame_rate (0)
107 set_dci_date_today ();
109 _playlist->Changed.connect (bind (&Film::playlist_changed, this));
110 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
112 /* Make state.directory a complete path without ..s (where possible)
113 (Code swiped from Adam Bowen on stackoverflow)
116 boost::filesystem::path p (boost::filesystem::system_complete (d));
117 boost::filesystem::path result;
118 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
120 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
123 result = result.parent_path ();
125 } else if (*i != ".") {
130 set_directory (result.string ());
131 _log.reset (new FileLog (file ("log")));
134 Film::Film (Film const & o)
135 : boost::enable_shared_from_this<Film> (o)
136 /* note: the copied film shares the original's log */
138 , _playlist (new Playlist (o._playlist))
139 , _directory (o._directory)
141 , _use_dci_name (o._use_dci_name)
142 , _dcp_content_type (o._dcp_content_type)
143 , _container (o._container)
144 , _filters (o._filters)
145 , _scaler (o._scaler)
147 , _with_subtitles (o._with_subtitles)
148 , _subtitle_offset (o._subtitle_offset)
149 , _subtitle_scale (o._subtitle_scale)
150 , _colour_lut (o._colour_lut)
151 , _j2k_bandwidth (o._j2k_bandwidth)
152 , _dci_metadata (o._dci_metadata)
153 , _dcp_video_frame_rate (o._dcp_video_frame_rate)
154 , _dci_date (o._dci_date)
157 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
161 Film::video_state_identifier () const
163 assert (container ());
166 pair<string, string> f = Filter::ffmpeg_strings (filters());
169 s << container()->id()
170 << "_" << _playlist->video_digest()
171 << "_" << _dcp_video_frame_rate
172 << "_" << f.first << "_" << f.second
173 << "_" << scaler()->id()
174 << "_" << j2k_bandwidth()
175 << "_" << boost::lexical_cast<int> (colour_lut());
178 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
179 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
185 /** @return The path to the directory to write video frame info files to */
187 Film::info_dir () const
189 boost::filesystem::path p;
191 p /= video_state_identifier ();
192 return dir (p.string());
196 Film::internal_video_mxf_dir () const
198 boost::filesystem::path p;
199 return dir ("video");
203 Film::internal_video_mxf_filename () const
205 return video_state_identifier() + ".mxf";
209 Film::dcp_video_mxf_filename () const
211 return filename_safe_name() + "_video.mxf";
215 Film::dcp_audio_mxf_filename () const
217 return filename_safe_name() + "_audio.mxf";
221 Film::filename_safe_name () const
223 string const n = name ();
225 for (size_t i = 0; i < n.length(); ++i) {
226 if (isalnum (n[i])) {
237 Film::audio_analysis_path () const
239 boost::filesystem::path p;
241 p /= _playlist->audio_digest();
242 return file (p.string ());
245 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
249 set_dci_date_today ();
251 if (dcp_name().find ("/") != string::npos) {
252 throw BadSettingError (_("name"), _("cannot contain slashes"));
255 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
259 gethostname (buffer, sizeof (buffer));
260 log()->log (String::compose ("Starting to make DCP on %1", buffer));
263 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
265 // log()->log (String::compose ("Content length %1", length().get()));
267 // log()->log (String::compose ("Content digest %1", content_digest()));
268 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
269 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
270 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
271 #ifdef DCPOMATIC_DEBUG
272 log()->log ("DCP-o-matic built in debug mode.");
274 log()->log ("DCP-o-matic built in optimised mode.");
277 log()->log ("libdcp built in debug mode.");
279 log()->log ("libdcp built in optimised mode.");
281 pair<string, int> const c = cpu_info ();
282 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
284 if (container() == 0) {
285 throw MissingSettingError (_("container"));
288 if (_playlist->content().empty ()) {
289 throw StringError (_("You must add some content to the DCP before creating it"));
292 if (dcp_content_type() == 0) {
293 throw MissingSettingError (_("content type"));
296 if (name().empty()) {
297 throw MissingSettingError (_("name"));
303 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
305 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
309 /** Start a job to analyse the audio in our Playlist */
311 Film::analyse_audio ()
313 if (_analyse_audio_job) {
317 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
318 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
319 JobManager::instance()->add (_analyse_audio_job);
322 /** Start a job to examine a piece of content */
324 Film::examine_content (shared_ptr<Content> c)
326 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
327 JobManager::instance()->add (j);
331 Film::analyse_audio_finished ()
335 if (_analyse_audio_job->finished_ok ()) {
336 AudioAnalysisSucceeded ();
339 _analyse_audio_job.reset ();
342 /** Start a job to send our DCP to the configured TMS */
344 Film::send_dcp_to_tms ()
346 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
347 JobManager::instance()->add (j);
350 /** Count the number of frames that have been encoded for this film.
351 * @return frame count.
354 Film::encoded_frames () const
356 if (container() == 0) {
361 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
363 boost::this_thread::interruption_point ();
369 /** Write state to our `metadata' file */
371 Film::write_metadata () const
373 if (!boost::filesystem::exists (directory())) {
374 boost::filesystem::create_directory (directory());
377 boost::mutex::scoped_lock lm (_state_mutex);
380 boost::filesystem::create_directories (directory());
383 xmlpp::Element* root = doc.create_root_node ("Metadata");
385 root->add_child("Version")->add_child_text (boost::lexical_cast<string> (state_version));
386 root->add_child("Name")->add_child_text (_name);
387 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
389 if (_dcp_content_type) {
390 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
394 root->add_child("Container")->add_child_text (_container->id ());
397 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
398 root->add_child("Filter")->add_child_text ((*i)->id ());
401 root->add_child("Scaler")->add_child_text (_scaler->id ());
402 root->add_child("AB")->add_child_text (_ab ? "1" : "0");
403 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
404 root->add_child("SubtitleOffset")->add_child_text (boost::lexical_cast<string> (_subtitle_offset));
405 root->add_child("SubtitleScale")->add_child_text (boost::lexical_cast<string> (_subtitle_scale));
406 root->add_child("ColourLUT")->add_child_text (boost::lexical_cast<string> (_colour_lut));
407 root->add_child("J2KBandwidth")->add_child_text (boost::lexical_cast<string> (_j2k_bandwidth));
408 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
409 root->add_child("DCPVideoFrameRate")->add_child_text (boost::lexical_cast<string> (_dcp_video_frame_rate));
410 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
411 _playlist->as_xml (root->add_child ("Playlist"));
413 doc.write_to_file_formatted (file ("metadata.xml"));
418 /** Read state from our metadata file */
420 Film::read_metadata ()
422 boost::mutex::scoped_lock lm (_state_mutex);
425 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
426 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!"));
429 cxml::File f (file ("metadata.xml"), "Metadata");
431 _name = f.string_child ("Name");
432 _use_dci_name = f.bool_child ("UseDCIName");
435 optional<string> c = f.optional_string_child ("DCPContentType");
437 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
442 optional<string> c = f.optional_string_child ("Container");
444 _container = Container::from_id (c.get ());
449 list<shared_ptr<cxml::Node> > c = f.node_children ("Filter");
450 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
451 _filters.push_back (Filter::from_id ((*i)->content ()));
455 _scaler = Scaler::from_id (f.string_child ("Scaler"));
456 _ab = f.bool_child ("AB");
457 _with_subtitles = f.bool_child ("WithSubtitles");
458 _subtitle_offset = f.number_child<float> ("SubtitleOffset");
459 _subtitle_scale = f.number_child<float> ("SubtitleScale");
460 _colour_lut = f.number_child<int> ("ColourLUT");
461 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
462 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
463 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
464 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
466 _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
471 /** Given a directory name, return its full path within the Film's directory.
472 * The directory (and its parents) will be created if they do not exist.
475 Film::dir (string d) const
477 boost::mutex::scoped_lock lm (_directory_mutex);
479 boost::filesystem::path p;
483 boost::filesystem::create_directories (p);
488 /** Given a file or directory name, return its full path within the Film's directory.
489 * _directory_mutex must not be locked on entry.
490 * Any required parent directories will be created.
493 Film::file (string f) const
495 boost::mutex::scoped_lock lm (_directory_mutex);
497 boost::filesystem::path p;
501 boost::filesystem::create_directories (p.parent_path ());
506 /** @return a DCI-compliant name for a DCP of this film */
508 Film::dci_name (bool if_created_now) const
512 string fixed_name = to_upper_copy (name());
513 for (size_t i = 0; i < fixed_name.length(); ++i) {
514 if (fixed_name[i] == ' ') {
519 /* Spec is that the name part should be maximum 14 characters, as I understand it */
520 if (fixed_name.length() > 14) {
521 fixed_name = fixed_name.substr (0, 14);
526 if (dcp_content_type()) {
527 d << "_" << dcp_content_type()->dci_name();
531 d << "_" << container()->dci_name();
534 DCIMetadata const dm = dci_metadata ();
536 if (!dm.audio_language.empty ()) {
537 d << "_" << dm.audio_language;
538 if (!dm.subtitle_language.empty()) {
539 d << "-" << dm.subtitle_language;
545 if (!dm.territory.empty ()) {
546 d << "_" << dm.territory;
547 if (!dm.rating.empty ()) {
548 d << "-" << dm.rating;
554 if (!dm.studio.empty ()) {
555 d << "_" << dm.studio;
558 if (if_created_now) {
559 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
561 d << "_" << boost::gregorian::to_iso_string (_dci_date);
564 if (!dm.facility.empty ()) {
565 d << "_" << dm.facility;
568 if (!dm.package_type.empty ()) {
569 d << "_" << dm.package_type;
575 /** @return name to give the DCP */
577 Film::dcp_name (bool if_created_now) const
579 if (use_dci_name()) {
580 return dci_name (if_created_now);
588 Film::set_directory (string d)
590 boost::mutex::scoped_lock lm (_state_mutex);
596 Film::set_name (string n)
599 boost::mutex::scoped_lock lm (_state_mutex);
602 signal_changed (NAME);
606 Film::set_use_dci_name (bool u)
609 boost::mutex::scoped_lock lm (_state_mutex);
612 signal_changed (USE_DCI_NAME);
616 Film::set_dcp_content_type (DCPContentType const * t)
619 boost::mutex::scoped_lock lm (_state_mutex);
620 _dcp_content_type = t;
622 signal_changed (DCP_CONTENT_TYPE);
626 Film::set_container (Container const * c)
629 boost::mutex::scoped_lock lm (_state_mutex);
632 signal_changed (CONTAINER);
636 Film::set_filters (vector<Filter const *> f)
639 boost::mutex::scoped_lock lm (_state_mutex);
642 signal_changed (FILTERS);
646 Film::set_scaler (Scaler const * s)
649 boost::mutex::scoped_lock lm (_state_mutex);
652 signal_changed (SCALER);
656 Film::set_ab (bool a)
659 boost::mutex::scoped_lock lm (_state_mutex);
666 Film::set_with_subtitles (bool w)
669 boost::mutex::scoped_lock lm (_state_mutex);
672 signal_changed (WITH_SUBTITLES);
676 Film::set_subtitle_offset (int o)
679 boost::mutex::scoped_lock lm (_state_mutex);
680 _subtitle_offset = o;
682 signal_changed (SUBTITLE_OFFSET);
686 Film::set_subtitle_scale (float s)
689 boost::mutex::scoped_lock lm (_state_mutex);
692 signal_changed (SUBTITLE_SCALE);
696 Film::set_colour_lut (int i)
699 boost::mutex::scoped_lock lm (_state_mutex);
702 signal_changed (COLOUR_LUT);
706 Film::set_j2k_bandwidth (int b)
709 boost::mutex::scoped_lock lm (_state_mutex);
712 signal_changed (J2K_BANDWIDTH);
716 Film::set_dci_metadata (DCIMetadata m)
719 boost::mutex::scoped_lock lm (_state_mutex);
722 signal_changed (DCI_METADATA);
727 Film::set_dcp_video_frame_rate (int f)
730 boost::mutex::scoped_lock lm (_state_mutex);
731 _dcp_video_frame_rate = f;
733 signal_changed (DCP_VIDEO_FRAME_RATE);
737 Film::signal_changed (Property p)
740 boost::mutex::scoped_lock lm (_state_mutex);
746 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
753 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
758 Film::set_dci_date_today ()
760 _dci_date = boost::gregorian::day_clock::local_day ();
764 Film::info_path (int f) const
766 boost::filesystem::path p;
771 s << setfill('0') << f << ".md5";
775 /* info_dir() will already have added any initial bit of the path,
776 so don't call file() on this.
782 Film::j2c_path (int f, bool t) const
784 boost::filesystem::path p;
786 p /= video_state_identifier ();
790 s << setfill('0') << f << ".j2c";
797 return file (p.string ());
800 /** Make an educated guess as to whether we have a complete DCP
802 * @return true if we do.
806 Film::have_dcp () const
809 libdcp::DCP dcp (dir (dcp_name()));
819 Film::player () const
821 boost::mutex::scoped_lock lm (_state_mutex);
822 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
826 Film::playlist () const
828 boost::mutex::scoped_lock lm (_state_mutex);
832 Playlist::ContentList
833 Film::content () const
835 return _playlist->content ();
839 Film::add_content (shared_ptr<Content> c)
846 Film::remove_content (shared_ptr<Content> c)
848 _playlist->remove (c);
852 Film::length () const
854 return _playlist->length ();
858 Film::has_subtitles () const
860 return _playlist->has_subtitles ();
864 Film::best_dcp_video_frame_rate () const
866 return _playlist->best_dcp_frame_rate ();
870 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
872 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
873 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
877 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
882 Film::playlist_changed ()
884 signal_changed (CONTENT);
890 return _playlist->loop ();
894 Film::set_loop (int c)
896 _playlist->set_loop (c);
900 Film::time_to_audio_frames (Time t) const
902 return t * dcp_audio_frame_rate () / TIME_HZ;
906 Film::time_to_video_frames (Time t) const
908 return t * dcp_video_frame_rate () / TIME_HZ;
912 Film::audio_frames_to_time (OutputAudioFrame f) const
914 return f * TIME_HZ / dcp_audio_frame_rate ();
918 Film::video_frames_to_time (OutputVideoFrame f) const
920 return f * TIME_HZ / dcp_video_frame_rate ();
924 Film::dcp_audio_frame_rate () const