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"))
101 , _with_subtitles (false)
102 , _subtitle_offset (0)
103 , _subtitle_scale (1)
105 , _j2k_bandwidth (200000000)
106 , _dci_metadata (Config::instance()->default_dci_metadata ())
107 , _dcp_video_frame_rate (0)
110 set_dci_date_today ();
112 _playlist->Changed.connect (bind (&Film::playlist_changed, this));
113 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
115 /* Make state.directory a complete path without ..s (where possible)
116 (Code swiped from Adam Bowen on stackoverflow)
119 boost::filesystem::path p (boost::filesystem::system_complete (d));
120 boost::filesystem::path result;
121 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
123 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
126 result = result.parent_path ();
128 } else if (*i != ".") {
133 set_directory (result.string ());
135 if (!boost::filesystem::exists (directory())) {
137 throw OpenFileError (directory());
139 boost::filesystem::create_directory (directory());
149 _log.reset (new FileLog (file ("log")));
152 Film::Film (Film const & o)
153 : boost::enable_shared_from_this<Film> (o)
154 /* note: the copied film shares the original's log */
156 , _playlist (new Playlist (o._playlist))
157 , _directory (o._directory)
159 , _use_dci_name (o._use_dci_name)
160 , _dcp_content_type (o._dcp_content_type)
161 , _container (o._container)
162 , _filters (o._filters)
163 , _scaler (o._scaler)
165 , _with_subtitles (o._with_subtitles)
166 , _subtitle_offset (o._subtitle_offset)
167 , _subtitle_scale (o._subtitle_scale)
168 , _colour_lut (o._colour_lut)
169 , _j2k_bandwidth (o._j2k_bandwidth)
170 , _dci_metadata (o._dci_metadata)
171 , _dcp_video_frame_rate (o._dcp_video_frame_rate)
172 , _dci_date (o._dci_date)
175 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
179 Film::video_state_identifier () const
181 assert (container ());
184 pair<string, string> f = Filter::ffmpeg_strings (filters());
187 s << container()->id()
188 << "_" << _playlist->video_digest()
189 << "_" << _dcp_video_frame_rate
190 << "_" << f.first << "_" << f.second
191 << "_" << scaler()->id()
192 << "_" << j2k_bandwidth()
193 << "_" << boost::lexical_cast<int> (colour_lut());
196 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
197 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
203 /** @return The path to the directory to write video frame info files to */
205 Film::info_dir () const
207 boost::filesystem::path p;
209 p /= video_state_identifier ();
210 return dir (p.string());
214 Film::internal_video_mxf_dir () const
216 boost::filesystem::path p;
217 return dir ("video");
221 Film::internal_video_mxf_filename () const
223 return video_state_identifier() + ".mxf";
227 Film::dcp_video_mxf_filename () const
229 return filename_safe_name() + "_video.mxf";
233 Film::dcp_audio_mxf_filename () const
235 return filename_safe_name() + "_audio.mxf";
239 Film::filename_safe_name () const
241 string const n = name ();
243 for (size_t i = 0; i < n.length(); ++i) {
244 if (isalnum (n[i])) {
255 Film::audio_analysis_path () const
257 boost::filesystem::path p;
259 p /= _playlist->audio_digest();
260 return file (p.string ());
263 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
267 set_dci_date_today ();
269 if (dcp_name().find ("/") != string::npos) {
270 throw BadSettingError (_("name"), _("cannot contain slashes"));
273 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
277 gethostname (buffer, sizeof (buffer));
278 log()->log (String::compose ("Starting to make DCP on %1", buffer));
281 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
283 // log()->log (String::compose ("Content length %1", length().get()));
285 // log()->log (String::compose ("Content digest %1", content_digest()));
286 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
287 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
288 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
289 #ifdef DCPOMATIC_DEBUG
290 log()->log ("DCP-o-matic built in debug mode.");
292 log()->log ("DCP-o-matic built in optimised mode.");
295 log()->log ("libdcp built in debug mode.");
297 log()->log ("libdcp built in optimised mode.");
299 pair<string, int> const c = cpu_info ();
300 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
302 if (container() == 0) {
303 throw MissingSettingError (_("container"));
306 if (_playlist->content().empty ()) {
307 throw StringError (_("You must add some content to the DCP before creating it"));
310 if (dcp_content_type() == 0) {
311 throw MissingSettingError (_("content type"));
314 if (name().empty()) {
315 throw MissingSettingError (_("name"));
321 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
323 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
327 /** Start a job to analyse the audio in our Playlist */
329 Film::analyse_audio ()
331 if (_analyse_audio_job) {
335 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
336 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
337 JobManager::instance()->add (_analyse_audio_job);
340 /** Start a job to examine a piece of content */
342 Film::examine_content (shared_ptr<Content> c)
344 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
345 JobManager::instance()->add (j);
349 Film::analyse_audio_finished ()
353 if (_analyse_audio_job->finished_ok ()) {
354 AudioAnalysisSucceeded ();
357 _analyse_audio_job.reset ();
360 /** Start a job to send our DCP to the configured TMS */
362 Film::send_dcp_to_tms ()
364 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
365 JobManager::instance()->add (j);
368 /** Count the number of frames that have been encoded for this film.
369 * @return frame count.
372 Film::encoded_frames () const
374 if (container() == 0) {
379 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
381 boost::this_thread::interruption_point ();
387 /** Write state to our `metadata' file */
389 Film::write_metadata () const
391 boost::mutex::scoped_lock lm (_state_mutex);
394 boost::filesystem::create_directories (directory());
397 xmlpp::Element* root = doc.create_root_node ("Metadata");
399 root->add_child("Version")->add_child_text (boost::lexical_cast<string> (state_version));
400 root->add_child("Name")->add_child_text (_name);
401 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
403 if (_dcp_content_type) {
404 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
408 root->add_child("Container")->add_child_text (_container->id ());
411 for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
412 root->add_child("Filter")->add_child_text ((*i)->id ());
415 root->add_child("Scaler")->add_child_text (_scaler->id ());
416 root->add_child("AB")->add_child_text (_ab ? "1" : "0");
417 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
418 root->add_child("SubtitleOffset")->add_child_text (boost::lexical_cast<string> (_subtitle_offset));
419 root->add_child("SubtitleScale")->add_child_text (boost::lexical_cast<string> (_subtitle_scale));
420 root->add_child("ColourLUT")->add_child_text (boost::lexical_cast<string> (_colour_lut));
421 root->add_child("J2KBandwidth")->add_child_text (boost::lexical_cast<string> (_j2k_bandwidth));
422 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
423 root->add_child("DCPVideoFrameRate")->add_child_text (boost::lexical_cast<string> (_dcp_video_frame_rate));
424 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
425 _playlist->as_xml (root->add_child ("Playlist"));
427 doc.write_to_file_formatted (file ("metadata.xml"));
432 /** Read state from our metadata file */
434 Film::read_metadata ()
436 boost::mutex::scoped_lock lm (_state_mutex);
439 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
440 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!"));
443 cxml::File f (file ("metadata.xml"), "Metadata");
445 _name = f.string_child ("Name");
446 _use_dci_name = f.bool_child ("UseDCIName");
449 optional<string> c = f.optional_string_child ("DCPContentType");
451 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
456 optional<string> c = f.optional_string_child ("Container");
458 _container = Container::from_id (c.get ());
463 list<shared_ptr<cxml::Node> > c = f.node_children ("Filter");
464 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
465 _filters.push_back (Filter::from_id ((*i)->content ()));
469 _scaler = Scaler::from_id (f.string_child ("Scaler"));
470 _ab = f.bool_child ("AB");
471 _with_subtitles = f.bool_child ("WithSubtitles");
472 _subtitle_offset = f.number_child<float> ("SubtitleOffset");
473 _subtitle_scale = f.number_child<float> ("SubtitleScale");
474 _colour_lut = f.number_child<int> ("ColourLUT");
475 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
476 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
477 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
478 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
480 _playlist->set_from_xml (f.node_child ("Playlist"));
485 /** Given a directory name, return its full path within the Film's directory.
486 * The directory (and its parents) will be created if they do not exist.
489 Film::dir (string d) const
491 boost::mutex::scoped_lock lm (_directory_mutex);
493 boost::filesystem::path p;
497 boost::filesystem::create_directories (p);
502 /** Given a file or directory name, return its full path within the Film's directory.
503 * _directory_mutex must not be locked on entry.
504 * Any required parent directories will be created.
507 Film::file (string f) const
509 boost::mutex::scoped_lock lm (_directory_mutex);
511 boost::filesystem::path p;
515 boost::filesystem::create_directories (p.parent_path ());
520 /** @return a DCI-compliant name for a DCP of this film */
522 Film::dci_name (bool if_created_now) const
526 string fixed_name = to_upper_copy (name());
527 for (size_t i = 0; i < fixed_name.length(); ++i) {
528 if (fixed_name[i] == ' ') {
533 /* Spec is that the name part should be maximum 14 characters, as I understand it */
534 if (fixed_name.length() > 14) {
535 fixed_name = fixed_name.substr (0, 14);
540 if (dcp_content_type()) {
541 d << "_" << dcp_content_type()->dci_name();
545 d << "_" << container()->dci_name();
548 DCIMetadata const dm = dci_metadata ();
550 if (!dm.audio_language.empty ()) {
551 d << "_" << dm.audio_language;
552 if (!dm.subtitle_language.empty()) {
553 d << "-" << dm.subtitle_language;
559 if (!dm.territory.empty ()) {
560 d << "_" << dm.territory;
561 if (!dm.rating.empty ()) {
562 d << "-" << dm.rating;
568 if (!dm.studio.empty ()) {
569 d << "_" << dm.studio;
572 if (if_created_now) {
573 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
575 d << "_" << boost::gregorian::to_iso_string (_dci_date);
578 if (!dm.facility.empty ()) {
579 d << "_" << dm.facility;
582 if (!dm.package_type.empty ()) {
583 d << "_" << dm.package_type;
589 /** @return name to give the DCP */
591 Film::dcp_name (bool if_created_now) const
593 if (use_dci_name()) {
594 return dci_name (if_created_now);
602 Film::set_directory (string d)
604 boost::mutex::scoped_lock lm (_state_mutex);
610 Film::set_name (string n)
613 boost::mutex::scoped_lock lm (_state_mutex);
616 signal_changed (NAME);
620 Film::set_use_dci_name (bool u)
623 boost::mutex::scoped_lock lm (_state_mutex);
626 signal_changed (USE_DCI_NAME);
630 Film::set_dcp_content_type (DCPContentType const * t)
633 boost::mutex::scoped_lock lm (_state_mutex);
634 _dcp_content_type = t;
636 signal_changed (DCP_CONTENT_TYPE);
640 Film::set_container (Container const * c)
643 boost::mutex::scoped_lock lm (_state_mutex);
646 signal_changed (CONTAINER);
650 Film::set_filters (vector<Filter const *> f)
653 boost::mutex::scoped_lock lm (_state_mutex);
656 signal_changed (FILTERS);
660 Film::set_scaler (Scaler const * s)
663 boost::mutex::scoped_lock lm (_state_mutex);
666 signal_changed (SCALER);
670 Film::set_ab (bool a)
673 boost::mutex::scoped_lock lm (_state_mutex);
680 Film::set_with_subtitles (bool w)
683 boost::mutex::scoped_lock lm (_state_mutex);
686 signal_changed (WITH_SUBTITLES);
690 Film::set_subtitle_offset (int o)
693 boost::mutex::scoped_lock lm (_state_mutex);
694 _subtitle_offset = o;
696 signal_changed (SUBTITLE_OFFSET);
700 Film::set_subtitle_scale (float s)
703 boost::mutex::scoped_lock lm (_state_mutex);
706 signal_changed (SUBTITLE_SCALE);
710 Film::set_colour_lut (int i)
713 boost::mutex::scoped_lock lm (_state_mutex);
716 signal_changed (COLOUR_LUT);
720 Film::set_j2k_bandwidth (int b)
723 boost::mutex::scoped_lock lm (_state_mutex);
726 signal_changed (J2K_BANDWIDTH);
730 Film::set_dci_metadata (DCIMetadata m)
733 boost::mutex::scoped_lock lm (_state_mutex);
736 signal_changed (DCI_METADATA);
741 Film::set_dcp_video_frame_rate (int f)
744 boost::mutex::scoped_lock lm (_state_mutex);
745 _dcp_video_frame_rate = f;
747 signal_changed (DCP_VIDEO_FRAME_RATE);
751 Film::signal_changed (Property p)
754 boost::mutex::scoped_lock lm (_state_mutex);
760 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
767 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
772 Film::set_dci_date_today ()
774 _dci_date = boost::gregorian::day_clock::local_day ();
778 Film::info_path (int f) const
780 boost::filesystem::path p;
785 s << setfill('0') << f << ".md5";
789 /* info_dir() will already have added any initial bit of the path,
790 so don't call file() on this.
796 Film::j2c_path (int f, bool t) const
798 boost::filesystem::path p;
800 p /= video_state_identifier ();
804 s << setfill('0') << f << ".j2c";
811 return file (p.string ());
814 /** Make an educated guess as to whether we have a complete DCP
816 * @return true if we do.
820 Film::have_dcp () const
823 libdcp::DCP dcp (dir (dcp_name()));
833 Film::player () const
835 boost::mutex::scoped_lock lm (_state_mutex);
836 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
840 Film::playlist () const
842 boost::mutex::scoped_lock lm (_state_mutex);
846 Playlist::ContentList
847 Film::content () const
849 return _playlist->content ();
853 Film::add_content (shared_ptr<Content> c)
860 Film::remove_content (shared_ptr<Content> c)
862 _playlist->remove (c);
866 Film::length () const
868 return _playlist->length (shared_from_this ());
872 Film::has_subtitles () const
874 return _playlist->has_subtitles ();
878 Film::best_dcp_video_frame_rate () const
880 return _playlist->best_dcp_frame_rate ();
884 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
886 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
887 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
891 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
896 Film::playlist_changed ()
898 signal_changed (CONTENT);
904 return _playlist->loop ();
908 Film::set_loop (int c)
910 _playlist->set_loop (c);
914 Film::time_to_audio_frames (Time t) const
916 return t * dcp_audio_frame_rate () / TIME_HZ;
920 Film::time_to_video_frames (Time t) const
922 return t * dcp_video_frame_rate () / TIME_HZ;
926 Film::audio_frames_to_time (OutputAudioFrame f) const
928 return f * TIME_HZ / dcp_audio_frame_rate ();
932 Film::video_frames_to_time (OutputVideoFrame f) const
934 return f * TIME_HZ / dcp_video_frame_rate ();
938 Film::dcp_audio_frame_rate () const