2 Copyright (C) 2012 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>
38 #include "job_manager.h"
39 #include "transcode_job.h"
40 #include "scp_dcp_job.h"
42 #include "exceptions.h"
43 #include "examine_content_job.h"
47 #include "ui_signaller.h"
48 #include "analyse_audio_job.h"
51 #include "ffmpeg_content.h"
52 #include "imagemagick_content.h"
53 #include "sndfile_content.h"
54 #include "dcp_content_type.h"
61 using std::stringstream;
74 using boost::shared_ptr;
75 using boost::lexical_cast;
76 using boost::dynamic_pointer_cast;
77 using boost::to_upper_copy;
78 using boost::ends_with;
79 using boost::starts_with;
80 using boost::optional;
83 int const Film::state_version = 4;
85 /** Construct a Film object in a given directory.
87 * @param d Film directory.
91 : _playlist (new Playlist)
92 , _use_dci_name (true)
93 , _dcp_content_type (Config::instance()->default_dcp_content_type ())
94 , _container (Config::instance()->default_container ())
95 , _scaler (Scaler::from_id ("bicubic"))
96 , _with_subtitles (false)
97 , _subtitle_offset (0)
100 , _j2k_bandwidth (200000000)
101 , _dci_metadata (Config::instance()->default_dci_metadata ())
102 , _dcp_video_frame_rate (24)
103 , _dcp_audio_channels (MAX_AUDIO_CHANNELS)
106 set_dci_date_today ();
108 _playlist->Changed.connect (bind (&Film::playlist_changed, this));
109 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
111 /* Make state.directory a complete path without ..s (where possible)
112 (Code swiped from Adam Bowen on stackoverflow)
115 boost::filesystem::path p (boost::filesystem::system_complete (d));
116 boost::filesystem::path result;
117 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
119 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
122 result = result.parent_path ();
124 } else if (*i != ".") {
129 set_directory (result.string ());
130 _log.reset (new FileLog (file ("log")));
133 Film::Film (Film const & o)
134 : boost::enable_shared_from_this<Film> (o)
135 /* note: the copied film shares the original's log */
137 , _playlist (new Playlist (o._playlist))
138 , _directory (o._directory)
140 , _use_dci_name (o._use_dci_name)
141 , _dcp_content_type (o._dcp_content_type)
142 , _container (o._container)
143 , _scaler (o._scaler)
144 , _with_subtitles (o._with_subtitles)
145 , _subtitle_offset (o._subtitle_offset)
146 , _subtitle_scale (o._subtitle_scale)
147 , _colour_lut (o._colour_lut)
148 , _j2k_bandwidth (o._j2k_bandwidth)
149 , _dci_metadata (o._dci_metadata)
150 , _dcp_video_frame_rate (o._dcp_video_frame_rate)
151 , _dci_date (o._dci_date)
154 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
158 Film::video_state_identifier () const
160 assert (container ());
164 s << container()->id()
165 << "_" << _playlist->video_digest()
166 << "_" << _dcp_video_frame_rate
167 << "_" << scaler()->id()
168 << "_" << j2k_bandwidth()
169 << "_" << lexical_cast<int> (colour_lut());
174 /** @return The path to the directory to write video frame info files to */
176 Film::info_dir () const
178 boost::filesystem::path p;
180 p /= video_state_identifier ();
181 return dir (p.string());
185 Film::internal_video_mxf_dir () const
187 boost::filesystem::path p;
188 return dir ("video");
192 Film::internal_video_mxf_filename () const
194 return video_state_identifier() + ".mxf";
198 Film::dcp_video_mxf_filename () const
200 return filename_safe_name() + "_video.mxf";
204 Film::dcp_audio_mxf_filename () const
206 return filename_safe_name() + "_audio.mxf";
210 Film::filename_safe_name () const
212 string const n = name ();
214 for (size_t i = 0; i < n.length(); ++i) {
215 if (isalnum (n[i])) {
226 Film::audio_analysis_path () const
228 boost::filesystem::path p;
230 p /= _playlist->audio_digest();
231 return file (p.string ());
234 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
238 set_dci_date_today ();
240 if (dcp_name().find ("/") != string::npos) {
241 throw BadSettingError (_("name"), _("cannot contain slashes"));
244 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
248 gethostname (buffer, sizeof (buffer));
249 log()->log (String::compose ("Starting to make DCP on %1", buffer));
252 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
254 // log()->log (String::compose ("Content length %1", length().get()));
256 // log()->log (String::compose ("Content digest %1", content_digest()));
257 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
258 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
259 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
260 #ifdef DCPOMATIC_DEBUG
261 log()->log ("DCP-o-matic built in debug mode.");
263 log()->log ("DCP-o-matic built in optimised mode.");
266 log()->log ("libdcp built in debug mode.");
268 log()->log ("libdcp built in optimised mode.");
270 pair<string, int> const c = cpu_info ();
271 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
273 if (container() == 0) {
274 throw MissingSettingError (_("container"));
277 if (_playlist->content().empty ()) {
278 throw StringError (_("You must add some content to the DCP before creating it"));
281 if (dcp_content_type() == 0) {
282 throw MissingSettingError (_("content type"));
285 if (name().empty()) {
286 throw MissingSettingError (_("name"));
289 JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
292 /** Start a job to analyse the audio in our Playlist */
294 Film::analyse_audio ()
296 if (_analyse_audio_job) {
300 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
301 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
302 JobManager::instance()->add (_analyse_audio_job);
306 Film::analyse_audio_finished ()
310 if (_analyse_audio_job->finished_ok ()) {
311 AudioAnalysisSucceeded ();
314 _analyse_audio_job.reset ();
317 /** Start a job to send our DCP to the configured TMS */
319 Film::send_dcp_to_tms ()
321 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
322 JobManager::instance()->add (j);
325 /** Count the number of frames that have been encoded for this film.
326 * @return frame count.
329 Film::encoded_frames () const
331 if (container() == 0) {
336 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
338 boost::this_thread::interruption_point ();
344 /** Write state to our `metadata' file */
346 Film::write_metadata () const
348 if (!boost::filesystem::exists (directory())) {
349 boost::filesystem::create_directory (directory());
352 boost::mutex::scoped_lock lm (_state_mutex);
355 boost::filesystem::create_directories (directory());
358 xmlpp::Element* root = doc.create_root_node ("Metadata");
360 root->add_child("Version")->add_child_text (lexical_cast<string> (state_version));
361 root->add_child("Name")->add_child_text (_name);
362 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
364 if (_dcp_content_type) {
365 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
369 root->add_child("Container")->add_child_text (_container->id ());
372 root->add_child("Scaler")->add_child_text (_scaler->id ());
373 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
374 root->add_child("SubtitleOffset")->add_child_text (lexical_cast<string> (_subtitle_offset));
375 root->add_child("SubtitleScale")->add_child_text (lexical_cast<string> (_subtitle_scale));
376 root->add_child("ColourLUT")->add_child_text (lexical_cast<string> (_colour_lut));
377 root->add_child("J2KBandwidth")->add_child_text (lexical_cast<string> (_j2k_bandwidth));
378 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
379 root->add_child("DCPVideoFrameRate")->add_child_text (lexical_cast<string> (_dcp_video_frame_rate));
380 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
381 root->add_child("DCPAudioChannels")->add_child_text (lexical_cast<string> (_dcp_audio_channels));
382 _playlist->as_xml (root->add_child ("Playlist"));
384 doc.write_to_file_formatted (file ("metadata.xml"));
389 /** Read state from our metadata file */
391 Film::read_metadata ()
393 boost::mutex::scoped_lock lm (_state_mutex);
396 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
397 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!"));
400 cxml::File f (file ("metadata.xml"), "Metadata");
402 _name = f.string_child ("Name");
403 _use_dci_name = f.bool_child ("UseDCIName");
406 optional<string> c = f.optional_string_child ("DCPContentType");
408 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
413 optional<string> c = f.optional_string_child ("Container");
415 _container = Ratio::from_id (c.get ());
419 _scaler = Scaler::from_id (f.string_child ("Scaler"));
420 _with_subtitles = f.bool_child ("WithSubtitles");
421 _subtitle_offset = f.number_child<float> ("SubtitleOffset");
422 _subtitle_scale = f.number_child<float> ("SubtitleScale");
423 _colour_lut = f.number_child<int> ("ColourLUT");
424 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
425 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
426 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
427 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
428 _dcp_audio_channels = f.number_child<int> ("DCPAudioChannels");
430 _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
435 /** Given a directory name, return its full path within the Film's directory.
436 * The directory (and its parents) will be created if they do not exist.
439 Film::dir (string d) const
441 boost::mutex::scoped_lock lm (_directory_mutex);
443 boost::filesystem::path p;
447 boost::filesystem::create_directories (p);
452 /** Given a file or directory name, return its full path within the Film's directory.
453 * _directory_mutex must not be locked on entry.
454 * Any required parent directories will be created.
457 Film::file (string f) const
459 boost::mutex::scoped_lock lm (_directory_mutex);
461 boost::filesystem::path p;
465 boost::filesystem::create_directories (p.parent_path ());
470 /** @return a DCI-compliant name for a DCP of this film */
472 Film::dci_name (bool if_created_now) const
476 string fixed_name = to_upper_copy (name());
477 for (size_t i = 0; i < fixed_name.length(); ++i) {
478 if (fixed_name[i] == ' ') {
483 /* Spec is that the name part should be maximum 14 characters, as I understand it */
484 if (fixed_name.length() > 14) {
485 fixed_name = fixed_name.substr (0, 14);
490 if (dcp_content_type()) {
491 d << "_" << dcp_content_type()->dci_name();
495 d << "_" << container()->dci_name();
498 DCIMetadata const dm = dci_metadata ();
500 if (!dm.audio_language.empty ()) {
501 d << "_" << dm.audio_language;
502 if (!dm.subtitle_language.empty()) {
503 d << "-" << dm.subtitle_language;
509 if (!dm.territory.empty ()) {
510 d << "_" << dm.territory;
511 if (!dm.rating.empty ()) {
512 d << "-" << dm.rating;
518 if (!dm.studio.empty ()) {
519 d << "_" << dm.studio;
522 if (if_created_now) {
523 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
525 d << "_" << boost::gregorian::to_iso_string (_dci_date);
528 if (!dm.facility.empty ()) {
529 d << "_" << dm.facility;
532 if (!dm.package_type.empty ()) {
533 d << "_" << dm.package_type;
539 /** @return name to give the DCP */
541 Film::dcp_name (bool if_created_now) const
543 if (use_dci_name()) {
544 return dci_name (if_created_now);
552 Film::set_directory (string d)
554 boost::mutex::scoped_lock lm (_state_mutex);
560 Film::set_name (string n)
563 boost::mutex::scoped_lock lm (_state_mutex);
566 signal_changed (NAME);
570 Film::set_use_dci_name (bool u)
573 boost::mutex::scoped_lock lm (_state_mutex);
576 signal_changed (USE_DCI_NAME);
580 Film::set_dcp_content_type (DCPContentType const * t)
583 boost::mutex::scoped_lock lm (_state_mutex);
584 _dcp_content_type = t;
586 signal_changed (DCP_CONTENT_TYPE);
590 Film::set_container (Ratio const * c)
593 boost::mutex::scoped_lock lm (_state_mutex);
596 signal_changed (CONTAINER);
600 Film::set_scaler (Scaler const * s)
603 boost::mutex::scoped_lock lm (_state_mutex);
606 signal_changed (SCALER);
610 Film::set_with_subtitles (bool w)
613 boost::mutex::scoped_lock lm (_state_mutex);
616 signal_changed (WITH_SUBTITLES);
620 Film::set_subtitle_offset (int o)
623 boost::mutex::scoped_lock lm (_state_mutex);
624 _subtitle_offset = o;
626 signal_changed (SUBTITLE_OFFSET);
630 Film::set_subtitle_scale (float s)
633 boost::mutex::scoped_lock lm (_state_mutex);
636 signal_changed (SUBTITLE_SCALE);
640 Film::set_colour_lut (int i)
643 boost::mutex::scoped_lock lm (_state_mutex);
646 signal_changed (COLOUR_LUT);
650 Film::set_j2k_bandwidth (int b)
653 boost::mutex::scoped_lock lm (_state_mutex);
656 signal_changed (J2K_BANDWIDTH);
660 Film::set_dci_metadata (DCIMetadata m)
663 boost::mutex::scoped_lock lm (_state_mutex);
666 signal_changed (DCI_METADATA);
671 Film::set_dcp_video_frame_rate (int f)
674 boost::mutex::scoped_lock lm (_state_mutex);
675 _dcp_video_frame_rate = f;
677 signal_changed (DCP_VIDEO_FRAME_RATE);
681 Film::signal_changed (Property p)
684 boost::mutex::scoped_lock lm (_state_mutex);
690 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
697 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
702 Film::set_dci_date_today ()
704 _dci_date = boost::gregorian::day_clock::local_day ();
708 Film::info_path (int f) const
710 boost::filesystem::path p;
715 s << setfill('0') << f << ".md5";
719 /* info_dir() will already have added any initial bit of the path,
720 so don't call file() on this.
726 Film::j2c_path (int f, bool t) const
728 boost::filesystem::path p;
730 p /= video_state_identifier ();
734 s << setfill('0') << f << ".j2c";
741 return file (p.string ());
744 /** Make an educated guess as to whether we have a complete DCP
746 * @return true if we do.
750 Film::have_dcp () const
753 libdcp::DCP dcp (dir (dcp_name()));
763 Film::player () const
765 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
769 Film::playlist () const
771 boost::mutex::scoped_lock lm (_state_mutex);
775 Playlist::ContentList
776 Film::content () const
778 return _playlist->content ();
782 Film::examine_and_add_content (shared_ptr<Content> c)
784 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
785 JobManager::instance()->add (j);
789 Film::add_content (shared_ptr<Content> c)
791 /* Add video content after any existing content */
792 if (dynamic_pointer_cast<VideoContent> (c)) {
793 c->set_start (_playlist->video_end ());
800 Film::remove_content (shared_ptr<Content> c)
802 _playlist->remove (c);
806 Film::length () const
808 return _playlist->length ();
812 Film::has_subtitles () const
814 return _playlist->has_subtitles ();
818 Film::best_dcp_video_frame_rate () const
820 return _playlist->best_dcp_frame_rate ();
824 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
826 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
827 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
831 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
836 Film::playlist_changed ()
838 signal_changed (CONTENT);
844 return _playlist->loop ();
848 Film::set_loop (int c)
850 _playlist->set_loop (c);
854 Film::time_to_audio_frames (Time t) const
856 return t * dcp_audio_frame_rate () / TIME_HZ;
860 Film::time_to_video_frames (Time t) const
862 return t * dcp_video_frame_rate () / TIME_HZ;
866 Film::audio_frames_to_time (OutputAudioFrame f) const
868 return f * TIME_HZ / dcp_audio_frame_rate ();
872 Film::video_frames_to_time (OutputVideoFrame f) const
874 return f * TIME_HZ / dcp_video_frame_rate ();
878 Film::dcp_audio_frame_rate () const
885 Film::set_sequence_video (bool s)
887 _playlist->set_sequence_video (s);
891 Film::full_frame () const
893 return libdcp::Size (2048, 1080);