2 Copyright (C) 2012-2013 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>
32 #include <libxml++/libxml++.h>
33 #include <libcxml/cxml.h>
37 #include "job_manager.h"
38 #include "transcode_job.h"
39 #include "scp_dcp_job.h"
41 #include "exceptions.h"
42 #include "examine_content_job.h"
46 #include "ui_signaller.h"
49 #include "dcp_content_type.h"
56 using std::stringstream;
69 using boost::shared_ptr;
70 using boost::weak_ptr;
71 using boost::lexical_cast;
72 using boost::dynamic_pointer_cast;
73 using boost::to_upper_copy;
74 using boost::ends_with;
75 using boost::starts_with;
76 using boost::optional;
79 int const Film::state_version = 4;
81 /** Construct a Film object in a given directory.
83 * @param dir Film directory.
86 Film::Film (boost::filesystem::path dir)
87 : _playlist (new Playlist)
88 , _use_dci_name (true)
89 , _dcp_content_type (Config::instance()->default_dcp_content_type ())
90 , _container (Config::instance()->default_container ())
91 , _resolution (RESOLUTION_2K)
92 , _scaler (Scaler::from_id ("bicubic"))
93 , _with_subtitles (false)
94 , _j2k_bandwidth (Config::instance()->default_j2k_bandwidth ())
95 , _dci_metadata (Config::instance()->default_dci_metadata ())
96 , _video_frame_rate (24)
97 , _audio_channels (MAX_AUDIO_CHANNELS)
99 , _sequence_video (true)
103 set_dci_date_today ();
105 _playlist->Changed.connect (bind (&Film::playlist_changed, this));
106 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
108 /* Make state.directory a complete path without ..s (where possible)
109 (Code swiped from Adam Bowen on stackoverflow)
112 boost::filesystem::path p (boost::filesystem::system_complete (dir));
113 boost::filesystem::path result;
114 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
116 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
119 result = result.parent_path ();
121 } else if (*i != ".") {
126 set_directory (result.string ());
127 _log.reset (new FileLog (file ("log")));
129 _playlist->set_sequence_video (_sequence_video);
133 Film::video_identifier () const
135 assert (container ());
139 s << container()->id()
140 << "_" << resolution_to_string (_resolution)
141 << "_" << _playlist->video_identifier()
142 << "_" << _video_frame_rate
143 << "_" << scaler()->id()
144 << "_" << j2k_bandwidth();
159 /** @return The path to the directory to write video frame info files to */
161 Film::info_dir () const
163 boost::filesystem::path p;
165 p /= video_identifier ();
166 return dir (p.string());
170 Film::internal_video_mxf_dir () const
172 return dir ("video");
176 Film::internal_video_mxf_filename () const
178 return video_identifier() + ".mxf";
182 Film::video_mxf_filename () const
184 return filename_safe_name() + "_video.mxf";
188 Film::audio_mxf_filename () const
190 return filename_safe_name() + "_audio.mxf";
194 Film::filename_safe_name () const
196 string const n = name ();
198 for (size_t i = 0; i < n.length(); ++i) {
199 if (isalnum (n[i])) {
209 boost::filesystem::path
210 Film::audio_analysis_path (shared_ptr<const AudioContent> c) const
212 boost::filesystem::path p = dir ("analysis");
217 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
221 set_dci_date_today ();
223 if (dcp_name().find ("/") != string::npos) {
224 throw BadSettingError (_("name"), _("cannot contain slashes"));
227 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
231 gethostname (buffer, sizeof (buffer));
232 log()->log (String::compose ("Starting to make DCP on %1", buffer));
235 ContentList cl = content ();
236 for (ContentList::const_iterator i = cl.begin(); i != cl.end(); ++i) {
237 log()->log (String::compose ("Content: %1", (*i)->technical_summary()));
239 log()->log (String::compose ("DCP video rate %1 fps", video_frame_rate()));
240 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
241 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
242 #ifdef DCPOMATIC_DEBUG
243 log()->log ("DCP-o-matic built in debug mode.");
245 log()->log ("DCP-o-matic built in optimised mode.");
248 log()->log ("libdcp built in debug mode.");
250 log()->log ("libdcp built in optimised mode.");
252 log()->log (String::compose ("CPU: %1, %2 processors", cpu_info(), boost::thread::hardware_concurrency ()));
253 list<pair<string, string> > const m = mount_info ();
254 for (list<pair<string, string> >::const_iterator i = m.begin(); i != m.end(); ++i) {
255 log()->log (String::compose ("Mount: %1 %2", i->first, i->second));
258 if (container() == 0) {
259 throw MissingSettingError (_("container"));
262 if (content().empty()) {
263 throw StringError (_("You must add some content to the DCP before creating it"));
266 if (dcp_content_type() == 0) {
267 throw MissingSettingError (_("content type"));
270 if (name().empty()) {
271 throw MissingSettingError (_("name"));
274 JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
277 /** Start a job to send our DCP to the configured TMS */
279 Film::send_dcp_to_tms ()
281 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
282 JobManager::instance()->add (j);
285 /** Count the number of frames that have been encoded for this film.
286 * @return frame count.
289 Film::encoded_frames () const
291 if (container() == 0) {
296 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
298 boost::this_thread::interruption_point ();
304 /** Write state to our `metadata' file */
306 Film::write_metadata () const
308 if (!boost::filesystem::exists (directory())) {
309 boost::filesystem::create_directory (directory());
314 boost::filesystem::create_directories (directory());
317 xmlpp::Element* root = doc.create_root_node ("Metadata");
319 root->add_child("Version")->add_child_text (lexical_cast<string> (state_version));
320 root->add_child("Name")->add_child_text (_name);
321 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
323 if (_dcp_content_type) {
324 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
328 root->add_child("Container")->add_child_text (_container->id ());
331 root->add_child("Resolution")->add_child_text (resolution_to_string (_resolution));
332 root->add_child("Scaler")->add_child_text (_scaler->id ());
333 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
334 root->add_child("J2KBandwidth")->add_child_text (lexical_cast<string> (_j2k_bandwidth));
335 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
336 root->add_child("VideoFrameRate")->add_child_text (lexical_cast<string> (_video_frame_rate));
337 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
338 root->add_child("AudioChannels")->add_child_text (lexical_cast<string> (_audio_channels));
339 root->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
340 root->add_child("SequenceVideo")->add_child_text (_sequence_video ? "1" : "0");
341 root->add_child("Interop")->add_child_text (_interop ? "1" : "0");
342 _playlist->as_xml (root->add_child ("Playlist"));
344 doc.write_to_file_formatted (file ("metadata.xml"));
349 /** Read state from our metadata file */
351 Film::read_metadata ()
355 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
356 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!"));
359 cxml::Document f ("Metadata");
360 f.read_file (file ("metadata.xml"));
362 _name = f.string_child ("Name");
363 _use_dci_name = f.bool_child ("UseDCIName");
366 optional<string> c = f.optional_string_child ("DCPContentType");
368 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
373 optional<string> c = f.optional_string_child ("Container");
375 _container = Ratio::from_id (c.get ());
379 _resolution = string_to_resolution (f.string_child ("Resolution"));
380 _scaler = Scaler::from_id (f.string_child ("Scaler"));
381 _with_subtitles = f.bool_child ("WithSubtitles");
382 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
383 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
384 _video_frame_rate = f.number_child<int> ("VideoFrameRate");
385 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
386 _audio_channels = f.number_child<int> ("AudioChannels");
387 _sequence_video = f.bool_child ("SequenceVideo");
388 _three_d = f.bool_child ("ThreeD");
389 _interop = f.bool_child ("Interop");
391 _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
396 /** Given a directory name, return its full path within the Film's directory.
397 * The directory (and its parents) will be created if they do not exist.
400 Film::dir (string d) const
402 boost::filesystem::path p;
406 boost::filesystem::create_directories (p);
411 /** Given a file or directory name, return its full path within the Film's directory.
412 * Any required parent directories will be created.
415 Film::file (string f) const
417 boost::filesystem::path p;
421 boost::filesystem::create_directories (p.parent_path ());
426 /** @return a DCI-compliant name for a DCP of this film */
428 Film::dci_name (bool if_created_now) const
432 string fixed_name = to_upper_copy (name());
433 for (size_t i = 0; i < fixed_name.length(); ++i) {
434 if (fixed_name[i] == ' ') {
439 /* Spec is that the name part should be maximum 14 characters, as I understand it */
440 if (fixed_name.length() > 14) {
441 fixed_name = fixed_name.substr (0, 14);
446 if (dcp_content_type()) {
447 d << "_" << dcp_content_type()->dci_name();
448 d << "-" << dci_metadata().content_version;
455 if (video_frame_rate() != 24) {
456 d << "-" << video_frame_rate();
460 d << "_" << container()->dci_name();
463 DCIMetadata const dm = dci_metadata ();
465 if (!dm.audio_language.empty ()) {
466 d << "_" << dm.audio_language;
467 if (!dm.subtitle_language.empty() && with_subtitles()) {
468 d << "-" << dm.subtitle_language;
474 if (!dm.territory.empty ()) {
475 d << "_" << dm.territory;
476 if (!dm.rating.empty ()) {
477 d << "-" << dm.rating;
481 switch (audio_channels ()) {
502 d << "_" << resolution_to_string (_resolution);
504 if (!dm.studio.empty ()) {
505 d << "_" << dm.studio;
508 if (if_created_now) {
509 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
511 d << "_" << boost::gregorian::to_iso_string (_dci_date);
514 if (!dm.facility.empty ()) {
515 d << "_" << dm.facility;
518 if (!dm.package_type.empty ()) {
519 d << "_" << dm.package_type;
525 /** @return name to give the DCP */
527 Film::dcp_name (bool if_created_now) const
529 if (use_dci_name()) {
530 return dci_name (if_created_now);
538 Film::set_directory (string d)
545 Film::set_name (string n)
548 signal_changed (NAME);
552 Film::set_use_dci_name (bool u)
555 signal_changed (USE_DCI_NAME);
559 Film::set_dcp_content_type (DCPContentType const * t)
561 _dcp_content_type = t;
562 signal_changed (DCP_CONTENT_TYPE);
566 Film::set_container (Ratio const * c)
569 signal_changed (CONTAINER);
573 Film::set_resolution (Resolution r)
576 signal_changed (RESOLUTION);
580 Film::set_scaler (Scaler const * s)
583 signal_changed (SCALER);
587 Film::set_with_subtitles (bool w)
590 signal_changed (WITH_SUBTITLES);
594 Film::set_j2k_bandwidth (int b)
597 signal_changed (J2K_BANDWIDTH);
601 Film::set_dci_metadata (DCIMetadata m)
604 signal_changed (DCI_METADATA);
608 Film::set_video_frame_rate (int f)
610 _video_frame_rate = f;
611 signal_changed (VIDEO_FRAME_RATE);
615 Film::set_audio_channels (int c)
618 signal_changed (AUDIO_CHANNELS);
622 Film::set_three_d (bool t)
625 signal_changed (THREE_D);
629 Film::set_interop (bool i)
632 signal_changed (INTEROP);
636 Film::signal_changed (Property p)
642 set_video_frame_rate (_playlist->best_dcp_frame_rate ());
644 case Film::VIDEO_FRAME_RATE:
645 case Film::SEQUENCE_VIDEO:
646 _playlist->maybe_sequence_video ();
653 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
658 Film::set_dci_date_today ()
660 _dci_date = boost::gregorian::day_clock::local_day ();
664 Film::info_path (int f, Eyes e) const
666 boost::filesystem::path p;
671 s << setfill('0') << f;
673 if (e == EYES_LEFT) {
675 } else if (e == EYES_RIGHT) {
683 /* info_dir() will already have added any initial bit of the path,
684 so don't call file() on this.
690 Film::j2c_path (int f, Eyes e, bool t) const
692 boost::filesystem::path p;
694 p /= video_identifier ();
698 s << setfill('0') << f;
700 if (e == EYES_LEFT) {
702 } else if (e == EYES_RIGHT) {
713 return file (p.string ());
716 /** Make an educated guess as to whether we have a complete DCP
718 * @return true if we do.
722 Film::have_dcp () const
725 libdcp::DCP dcp (dir (dcp_name()));
735 Film::make_player () const
737 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
741 Film::playlist () const
747 Film::content () const
749 return _playlist->content ();
753 Film::examine_and_add_content (shared_ptr<Content> c)
755 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
756 j->Finished.connect (bind (&Film::maybe_add_content, this, boost::weak_ptr<Job> (j), boost::weak_ptr<Content> (c)));
757 JobManager::instance()->add (j);
761 Film::maybe_add_content (weak_ptr<Job> j, weak_ptr<Content> c)
763 shared_ptr<Job> job = j.lock ();
764 if (!job || !job->finished_ok ()) {
768 shared_ptr<Content> content = c.lock ();
770 add_content (content);
775 Film::add_content (shared_ptr<Content> c)
777 /* Add video content after any existing content */
778 if (dynamic_pointer_cast<VideoContent> (c)) {
779 c->set_position (_playlist->video_end ());
786 Film::remove_content (shared_ptr<Content> c)
788 _playlist->remove (c);
792 Film::length () const
794 return _playlist->length ();
798 Film::has_subtitles () const
800 return _playlist->has_subtitles ();
804 Film::best_video_frame_rate () const
806 return _playlist->best_dcp_frame_rate ();
810 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
812 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
813 set_video_frame_rate (_playlist->best_dcp_frame_rate ());
817 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
822 Film::playlist_changed ()
824 signal_changed (CONTENT);
828 Film::time_to_audio_frames (Time t) const
830 return t * audio_frame_rate () / TIME_HZ;
834 Film::time_to_video_frames (Time t) const
836 return t * video_frame_rate () / TIME_HZ;
840 Film::audio_frames_to_time (OutputAudioFrame f) const
842 return f * TIME_HZ / audio_frame_rate ();
846 Film::video_frames_to_time (OutputVideoFrame f) const
848 return f * TIME_HZ / video_frame_rate ();
852 Film::audio_frame_rate () const
859 Film::set_sequence_video (bool s)
862 _playlist->set_sequence_video (s);
863 signal_changed (SEQUENCE_VIDEO);
867 Film::full_frame () const
869 switch (_resolution) {
871 return libdcp::Size (2048, 1080);
873 return libdcp::Size (4096, 2160);
877 return libdcp::Size ();