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 "ab_transcode_job.h"
40 #include "transcode_job.h"
41 #include "scp_dcp_job.h"
43 #include "exceptions.h"
44 #include "examine_content_job.h"
48 #include "ui_signaller.h"
49 #include "analyse_audio_job.h"
52 #include "ffmpeg_content.h"
53 #include "imagemagick_content.h"
54 #include "sndfile_content.h"
55 #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"))
97 , _with_subtitles (false)
98 , _subtitle_offset (0)
101 , _j2k_bandwidth (200000000)
102 , _dci_metadata (Config::instance()->default_dci_metadata ())
103 , _dcp_video_frame_rate (0)
104 , _dcp_audio_channels (MAX_AUDIO_CHANNELS)
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 , _scaler (o._scaler)
146 , _with_subtitles (o._with_subtitles)
147 , _subtitle_offset (o._subtitle_offset)
148 , _subtitle_scale (o._subtitle_scale)
149 , _colour_lut (o._colour_lut)
150 , _j2k_bandwidth (o._j2k_bandwidth)
151 , _dci_metadata (o._dci_metadata)
152 , _dcp_video_frame_rate (o._dcp_video_frame_rate)
153 , _dci_date (o._dci_date)
156 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
160 Film::video_state_identifier () const
162 assert (container ());
166 s << container()->id()
167 << "_" << _playlist->video_digest()
168 << "_" << _dcp_video_frame_rate
169 << "_" << scaler()->id()
170 << "_" << j2k_bandwidth()
171 << "_" << lexical_cast<int> (colour_lut());
173 if (trim_type() == ENCODE) {
174 s << "_" << trim_start() << "_" << trim_end();
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);
323 Film::analyse_audio_finished ()
327 if (_analyse_audio_job->finished_ok ()) {
328 AudioAnalysisSucceeded ();
331 _analyse_audio_job.reset ();
334 /** Start a job to send our DCP to the configured TMS */
336 Film::send_dcp_to_tms ()
338 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
339 JobManager::instance()->add (j);
342 /** Count the number of frames that have been encoded for this film.
343 * @return frame count.
346 Film::encoded_frames () const
348 if (container() == 0) {
353 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
355 boost::this_thread::interruption_point ();
361 /** Write state to our `metadata' file */
363 Film::write_metadata () const
365 if (!boost::filesystem::exists (directory())) {
366 boost::filesystem::create_directory (directory());
369 boost::mutex::scoped_lock lm (_state_mutex);
372 boost::filesystem::create_directories (directory());
375 xmlpp::Element* root = doc.create_root_node ("Metadata");
377 root->add_child("Version")->add_child_text (lexical_cast<string> (state_version));
378 root->add_child("Name")->add_child_text (_name);
379 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
381 if (_dcp_content_type) {
382 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
386 root->add_child("Container")->add_child_text (_container->id ());
389 root->add_child("Scaler")->add_child_text (_scaler->id ());
390 root->add_child("AB")->add_child_text (_ab ? "1" : "0");
391 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
392 root->add_child("SubtitleOffset")->add_child_text (lexical_cast<string> (_subtitle_offset));
393 root->add_child("SubtitleScale")->add_child_text (lexical_cast<string> (_subtitle_scale));
394 root->add_child("ColourLUT")->add_child_text (lexical_cast<string> (_colour_lut));
395 root->add_child("J2KBandwidth")->add_child_text (lexical_cast<string> (_j2k_bandwidth));
396 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
397 root->add_child("DCPVideoFrameRate")->add_child_text (lexical_cast<string> (_dcp_video_frame_rate));
398 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
399 root->add_child("DCPAudioChannels")->add_child_text (lexical_cast<string> (_dcp_audio_channels));
400 _playlist->as_xml (root->add_child ("Playlist"));
402 doc.write_to_file_formatted (file ("metadata.xml"));
407 /** Read state from our metadata file */
409 Film::read_metadata ()
411 boost::mutex::scoped_lock lm (_state_mutex);
414 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
415 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!"));
418 cxml::File f (file ("metadata.xml"), "Metadata");
420 _name = f.string_child ("Name");
421 _use_dci_name = f.bool_child ("UseDCIName");
424 optional<string> c = f.optional_string_child ("DCPContentType");
426 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
431 optional<string> c = f.optional_string_child ("Container");
433 _container = Ratio::from_id (c.get ());
437 _scaler = Scaler::from_id (f.string_child ("Scaler"));
438 _ab = f.bool_child ("AB");
439 _with_subtitles = f.bool_child ("WithSubtitles");
440 _subtitle_offset = f.number_child<float> ("SubtitleOffset");
441 _subtitle_scale = f.number_child<float> ("SubtitleScale");
442 _colour_lut = f.number_child<int> ("ColourLUT");
443 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
444 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
445 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
446 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
447 _dcp_audio_channels = f.number_child<int> ("DCPAudioChannels");
449 _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
454 /** Given a directory name, return its full path within the Film's directory.
455 * The directory (and its parents) will be created if they do not exist.
458 Film::dir (string d) const
460 boost::mutex::scoped_lock lm (_directory_mutex);
462 boost::filesystem::path p;
466 boost::filesystem::create_directories (p);
471 /** Given a file or directory name, return its full path within the Film's directory.
472 * _directory_mutex must not be locked on entry.
473 * Any required parent directories will be created.
476 Film::file (string f) const
478 boost::mutex::scoped_lock lm (_directory_mutex);
480 boost::filesystem::path p;
484 boost::filesystem::create_directories (p.parent_path ());
489 /** @return a DCI-compliant name for a DCP of this film */
491 Film::dci_name (bool if_created_now) const
495 string fixed_name = to_upper_copy (name());
496 for (size_t i = 0; i < fixed_name.length(); ++i) {
497 if (fixed_name[i] == ' ') {
502 /* Spec is that the name part should be maximum 14 characters, as I understand it */
503 if (fixed_name.length() > 14) {
504 fixed_name = fixed_name.substr (0, 14);
509 if (dcp_content_type()) {
510 d << "_" << dcp_content_type()->dci_name();
514 d << "_" << container()->dci_name();
517 DCIMetadata const dm = dci_metadata ();
519 if (!dm.audio_language.empty ()) {
520 d << "_" << dm.audio_language;
521 if (!dm.subtitle_language.empty()) {
522 d << "-" << dm.subtitle_language;
528 if (!dm.territory.empty ()) {
529 d << "_" << dm.territory;
530 if (!dm.rating.empty ()) {
531 d << "-" << dm.rating;
537 if (!dm.studio.empty ()) {
538 d << "_" << dm.studio;
541 if (if_created_now) {
542 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
544 d << "_" << boost::gregorian::to_iso_string (_dci_date);
547 if (!dm.facility.empty ()) {
548 d << "_" << dm.facility;
551 if (!dm.package_type.empty ()) {
552 d << "_" << dm.package_type;
558 /** @return name to give the DCP */
560 Film::dcp_name (bool if_created_now) const
562 if (use_dci_name()) {
563 return dci_name (if_created_now);
571 Film::set_directory (string d)
573 boost::mutex::scoped_lock lm (_state_mutex);
579 Film::set_name (string n)
582 boost::mutex::scoped_lock lm (_state_mutex);
585 signal_changed (NAME);
589 Film::set_use_dci_name (bool u)
592 boost::mutex::scoped_lock lm (_state_mutex);
595 signal_changed (USE_DCI_NAME);
599 Film::set_dcp_content_type (DCPContentType const * t)
602 boost::mutex::scoped_lock lm (_state_mutex);
603 _dcp_content_type = t;
605 signal_changed (DCP_CONTENT_TYPE);
609 Film::set_container (Ratio const * c)
612 boost::mutex::scoped_lock lm (_state_mutex);
615 signal_changed (CONTAINER);
619 Film::set_scaler (Scaler const * s)
622 boost::mutex::scoped_lock lm (_state_mutex);
625 signal_changed (SCALER);
629 Film::set_ab (bool a)
632 boost::mutex::scoped_lock lm (_state_mutex);
639 Film::set_with_subtitles (bool w)
642 boost::mutex::scoped_lock lm (_state_mutex);
645 signal_changed (WITH_SUBTITLES);
649 Film::set_subtitle_offset (int o)
652 boost::mutex::scoped_lock lm (_state_mutex);
653 _subtitle_offset = o;
655 signal_changed (SUBTITLE_OFFSET);
659 Film::set_subtitle_scale (float s)
662 boost::mutex::scoped_lock lm (_state_mutex);
665 signal_changed (SUBTITLE_SCALE);
669 Film::set_colour_lut (int i)
672 boost::mutex::scoped_lock lm (_state_mutex);
675 signal_changed (COLOUR_LUT);
679 Film::set_j2k_bandwidth (int b)
682 boost::mutex::scoped_lock lm (_state_mutex);
685 signal_changed (J2K_BANDWIDTH);
689 Film::set_dci_metadata (DCIMetadata m)
692 boost::mutex::scoped_lock lm (_state_mutex);
695 signal_changed (DCI_METADATA);
700 Film::set_dcp_video_frame_rate (int f)
703 boost::mutex::scoped_lock lm (_state_mutex);
704 _dcp_video_frame_rate = f;
706 signal_changed (DCP_VIDEO_FRAME_RATE);
710 Film::signal_changed (Property p)
713 boost::mutex::scoped_lock lm (_state_mutex);
719 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
726 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
731 Film::set_dci_date_today ()
733 _dci_date = boost::gregorian::day_clock::local_day ();
737 Film::info_path (int f) const
739 boost::filesystem::path p;
744 s << setfill('0') << f << ".md5";
748 /* info_dir() will already have added any initial bit of the path,
749 so don't call file() on this.
755 Film::j2c_path (int f, bool t) const
757 boost::filesystem::path p;
759 p /= video_state_identifier ();
763 s << setfill('0') << f << ".j2c";
770 return file (p.string ());
773 /** Make an educated guess as to whether we have a complete DCP
775 * @return true if we do.
779 Film::have_dcp () const
782 libdcp::DCP dcp (dir (dcp_name()));
792 Film::player () const
794 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
798 Film::playlist () const
800 boost::mutex::scoped_lock lm (_state_mutex);
804 Playlist::ContentList
805 Film::content () const
807 return _playlist->content ();
811 Film::examine_and_add_content (shared_ptr<Content> c)
813 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
814 JobManager::instance()->add (j);
818 Film::add_content (shared_ptr<Content> c)
820 /* Add video content after any existing content */
821 if (dynamic_pointer_cast<VideoContent> (c)) {
822 c->set_start (_playlist->video_end ());
829 Film::remove_content (shared_ptr<Content> c)
831 _playlist->remove (c);
835 Film::length () const
837 return _playlist->length ();
841 Film::has_subtitles () const
843 return _playlist->has_subtitles ();
847 Film::best_dcp_video_frame_rate () const
849 return _playlist->best_dcp_frame_rate ();
853 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
855 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
856 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
860 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
865 Film::playlist_changed ()
867 signal_changed (CONTENT);
873 return _playlist->loop ();
877 Film::set_loop (int c)
879 _playlist->set_loop (c);
883 Film::time_to_audio_frames (Time t) const
885 return t * dcp_audio_frame_rate () / TIME_HZ;
889 Film::time_to_video_frames (Time t) const
891 return t * dcp_video_frame_rate () / TIME_HZ;
895 Film::audio_frames_to_time (OutputAudioFrame f) const
897 return f * TIME_HZ / dcp_audio_frame_rate ();
901 Film::video_frames_to_time (OutputVideoFrame f) const
903 return f * TIME_HZ / dcp_video_frame_rate ();
907 Film::dcp_audio_frame_rate () const
914 Film::set_sequence_video (bool s)
916 _playlist->set_sequence_video (s);
920 Film::full_frame () const
922 return libdcp::Size (2048, 1080);