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());
174 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
175 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
181 /** @return The path to the directory to write video frame info files to */
183 Film::info_dir () const
185 boost::filesystem::path p;
187 p /= video_state_identifier ();
188 return dir (p.string());
192 Film::internal_video_mxf_dir () const
194 boost::filesystem::path p;
195 return dir ("video");
199 Film::internal_video_mxf_filename () const
201 return video_state_identifier() + ".mxf";
205 Film::dcp_video_mxf_filename () const
207 return filename_safe_name() + "_video.mxf";
211 Film::dcp_audio_mxf_filename () const
213 return filename_safe_name() + "_audio.mxf";
217 Film::filename_safe_name () const
219 string const n = name ();
221 for (size_t i = 0; i < n.length(); ++i) {
222 if (isalnum (n[i])) {
233 Film::audio_analysis_path () const
235 boost::filesystem::path p;
237 p /= _playlist->audio_digest();
238 return file (p.string ());
241 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
245 set_dci_date_today ();
247 if (dcp_name().find ("/") != string::npos) {
248 throw BadSettingError (_("name"), _("cannot contain slashes"));
251 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
255 gethostname (buffer, sizeof (buffer));
256 log()->log (String::compose ("Starting to make DCP on %1", buffer));
259 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
261 // log()->log (String::compose ("Content length %1", length().get()));
263 // log()->log (String::compose ("Content digest %1", content_digest()));
264 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
265 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
266 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
267 #ifdef DCPOMATIC_DEBUG
268 log()->log ("DCP-o-matic built in debug mode.");
270 log()->log ("DCP-o-matic built in optimised mode.");
273 log()->log ("libdcp built in debug mode.");
275 log()->log ("libdcp built in optimised mode.");
277 pair<string, int> const c = cpu_info ();
278 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
280 if (container() == 0) {
281 throw MissingSettingError (_("container"));
284 if (_playlist->content().empty ()) {
285 throw StringError (_("You must add some content to the DCP before creating it"));
288 if (dcp_content_type() == 0) {
289 throw MissingSettingError (_("content type"));
292 if (name().empty()) {
293 throw MissingSettingError (_("name"));
299 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
301 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
305 /** Start a job to analyse the audio in our Playlist */
307 Film::analyse_audio ()
309 if (_analyse_audio_job) {
313 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
314 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
315 JobManager::instance()->add (_analyse_audio_job);
319 Film::analyse_audio_finished ()
323 if (_analyse_audio_job->finished_ok ()) {
324 AudioAnalysisSucceeded ();
327 _analyse_audio_job.reset ();
330 /** Start a job to send our DCP to the configured TMS */
332 Film::send_dcp_to_tms ()
334 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
335 JobManager::instance()->add (j);
338 /** Count the number of frames that have been encoded for this film.
339 * @return frame count.
342 Film::encoded_frames () const
344 if (container() == 0) {
349 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
351 boost::this_thread::interruption_point ();
357 /** Write state to our `metadata' file */
359 Film::write_metadata () const
361 if (!boost::filesystem::exists (directory())) {
362 boost::filesystem::create_directory (directory());
365 boost::mutex::scoped_lock lm (_state_mutex);
368 boost::filesystem::create_directories (directory());
371 xmlpp::Element* root = doc.create_root_node ("Metadata");
373 root->add_child("Version")->add_child_text (lexical_cast<string> (state_version));
374 root->add_child("Name")->add_child_text (_name);
375 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
377 if (_dcp_content_type) {
378 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
382 root->add_child("Container")->add_child_text (_container->id ());
385 root->add_child("Scaler")->add_child_text (_scaler->id ());
386 root->add_child("AB")->add_child_text (_ab ? "1" : "0");
387 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
388 root->add_child("SubtitleOffset")->add_child_text (lexical_cast<string> (_subtitle_offset));
389 root->add_child("SubtitleScale")->add_child_text (lexical_cast<string> (_subtitle_scale));
390 root->add_child("ColourLUT")->add_child_text (lexical_cast<string> (_colour_lut));
391 root->add_child("J2KBandwidth")->add_child_text (lexical_cast<string> (_j2k_bandwidth));
392 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
393 root->add_child("DCPVideoFrameRate")->add_child_text (lexical_cast<string> (_dcp_video_frame_rate));
394 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
395 root->add_child("DCPAudioChannels")->add_child_text (lexical_cast<string> (_dcp_audio_channels));
396 _playlist->as_xml (root->add_child ("Playlist"));
398 doc.write_to_file_formatted (file ("metadata.xml"));
403 /** Read state from our metadata file */
405 Film::read_metadata ()
407 boost::mutex::scoped_lock lm (_state_mutex);
410 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
411 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!"));
414 cxml::File f (file ("metadata.xml"), "Metadata");
416 _name = f.string_child ("Name");
417 _use_dci_name = f.bool_child ("UseDCIName");
420 optional<string> c = f.optional_string_child ("DCPContentType");
422 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
427 optional<string> c = f.optional_string_child ("Container");
429 _container = Ratio::from_id (c.get ());
433 _scaler = Scaler::from_id (f.string_child ("Scaler"));
434 _ab = f.bool_child ("AB");
435 _with_subtitles = f.bool_child ("WithSubtitles");
436 _subtitle_offset = f.number_child<float> ("SubtitleOffset");
437 _subtitle_scale = f.number_child<float> ("SubtitleScale");
438 _colour_lut = f.number_child<int> ("ColourLUT");
439 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
440 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
441 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
442 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
443 _dcp_audio_channels = f.number_child<int> ("DCPAudioChannels");
445 _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
450 /** Given a directory name, return its full path within the Film's directory.
451 * The directory (and its parents) will be created if they do not exist.
454 Film::dir (string d) const
456 boost::mutex::scoped_lock lm (_directory_mutex);
458 boost::filesystem::path p;
462 boost::filesystem::create_directories (p);
467 /** Given a file or directory name, return its full path within the Film's directory.
468 * _directory_mutex must not be locked on entry.
469 * Any required parent directories will be created.
472 Film::file (string f) const
474 boost::mutex::scoped_lock lm (_directory_mutex);
476 boost::filesystem::path p;
480 boost::filesystem::create_directories (p.parent_path ());
485 /** @return a DCI-compliant name for a DCP of this film */
487 Film::dci_name (bool if_created_now) const
491 string fixed_name = to_upper_copy (name());
492 for (size_t i = 0; i < fixed_name.length(); ++i) {
493 if (fixed_name[i] == ' ') {
498 /* Spec is that the name part should be maximum 14 characters, as I understand it */
499 if (fixed_name.length() > 14) {
500 fixed_name = fixed_name.substr (0, 14);
505 if (dcp_content_type()) {
506 d << "_" << dcp_content_type()->dci_name();
510 d << "_" << container()->dci_name();
513 DCIMetadata const dm = dci_metadata ();
515 if (!dm.audio_language.empty ()) {
516 d << "_" << dm.audio_language;
517 if (!dm.subtitle_language.empty()) {
518 d << "-" << dm.subtitle_language;
524 if (!dm.territory.empty ()) {
525 d << "_" << dm.territory;
526 if (!dm.rating.empty ()) {
527 d << "-" << dm.rating;
533 if (!dm.studio.empty ()) {
534 d << "_" << dm.studio;
537 if (if_created_now) {
538 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
540 d << "_" << boost::gregorian::to_iso_string (_dci_date);
543 if (!dm.facility.empty ()) {
544 d << "_" << dm.facility;
547 if (!dm.package_type.empty ()) {
548 d << "_" << dm.package_type;
554 /** @return name to give the DCP */
556 Film::dcp_name (bool if_created_now) const
558 if (use_dci_name()) {
559 return dci_name (if_created_now);
567 Film::set_directory (string d)
569 boost::mutex::scoped_lock lm (_state_mutex);
575 Film::set_name (string n)
578 boost::mutex::scoped_lock lm (_state_mutex);
581 signal_changed (NAME);
585 Film::set_use_dci_name (bool u)
588 boost::mutex::scoped_lock lm (_state_mutex);
591 signal_changed (USE_DCI_NAME);
595 Film::set_dcp_content_type (DCPContentType const * t)
598 boost::mutex::scoped_lock lm (_state_mutex);
599 _dcp_content_type = t;
601 signal_changed (DCP_CONTENT_TYPE);
605 Film::set_container (Ratio const * c)
608 boost::mutex::scoped_lock lm (_state_mutex);
611 signal_changed (CONTAINER);
615 Film::set_scaler (Scaler const * s)
618 boost::mutex::scoped_lock lm (_state_mutex);
621 signal_changed (SCALER);
625 Film::set_ab (bool a)
628 boost::mutex::scoped_lock lm (_state_mutex);
635 Film::set_with_subtitles (bool w)
638 boost::mutex::scoped_lock lm (_state_mutex);
641 signal_changed (WITH_SUBTITLES);
645 Film::set_subtitle_offset (int o)
648 boost::mutex::scoped_lock lm (_state_mutex);
649 _subtitle_offset = o;
651 signal_changed (SUBTITLE_OFFSET);
655 Film::set_subtitle_scale (float s)
658 boost::mutex::scoped_lock lm (_state_mutex);
661 signal_changed (SUBTITLE_SCALE);
665 Film::set_colour_lut (int i)
668 boost::mutex::scoped_lock lm (_state_mutex);
671 signal_changed (COLOUR_LUT);
675 Film::set_j2k_bandwidth (int b)
678 boost::mutex::scoped_lock lm (_state_mutex);
681 signal_changed (J2K_BANDWIDTH);
685 Film::set_dci_metadata (DCIMetadata m)
688 boost::mutex::scoped_lock lm (_state_mutex);
691 signal_changed (DCI_METADATA);
696 Film::set_dcp_video_frame_rate (int f)
699 boost::mutex::scoped_lock lm (_state_mutex);
700 _dcp_video_frame_rate = f;
702 signal_changed (DCP_VIDEO_FRAME_RATE);
706 Film::signal_changed (Property p)
709 boost::mutex::scoped_lock lm (_state_mutex);
715 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
722 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
727 Film::set_dci_date_today ()
729 _dci_date = boost::gregorian::day_clock::local_day ();
733 Film::info_path (int f) const
735 boost::filesystem::path p;
740 s << setfill('0') << f << ".md5";
744 /* info_dir() will already have added any initial bit of the path,
745 so don't call file() on this.
751 Film::j2c_path (int f, bool t) const
753 boost::filesystem::path p;
755 p /= video_state_identifier ();
759 s << setfill('0') << f << ".j2c";
766 return file (p.string ());
769 /** Make an educated guess as to whether we have a complete DCP
771 * @return true if we do.
775 Film::have_dcp () const
778 libdcp::DCP dcp (dir (dcp_name()));
788 Film::player () const
790 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
794 Film::playlist () const
796 boost::mutex::scoped_lock lm (_state_mutex);
800 Playlist::ContentList
801 Film::content () const
803 return _playlist->content ();
807 Film::examine_and_add_content (shared_ptr<Content> c)
809 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
810 JobManager::instance()->add (j);
814 Film::add_content (shared_ptr<Content> c)
816 /* Add video content after any existing content */
817 if (dynamic_pointer_cast<VideoContent> (c)) {
818 c->set_start (_playlist->video_end ());
825 Film::remove_content (shared_ptr<Content> c)
827 _playlist->remove (c);
831 Film::length () const
833 return _playlist->length ();
837 Film::has_subtitles () const
839 return _playlist->has_subtitles ();
843 Film::best_dcp_video_frame_rate () const
845 return _playlist->best_dcp_frame_rate ();
849 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
851 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
852 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
856 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
861 Film::playlist_changed ()
863 signal_changed (CONTENT);
869 return _playlist->loop ();
873 Film::set_loop (int c)
875 _playlist->set_loop (c);
879 Film::time_to_audio_frames (Time t) const
881 return t * dcp_audio_frame_rate () / TIME_HZ;
885 Film::time_to_video_frames (Time t) const
887 return t * dcp_video_frame_rate () / TIME_HZ;
891 Film::audio_frames_to_time (OutputAudioFrame f) const
893 return f * TIME_HZ / dcp_audio_frame_rate ();
897 Film::video_frames_to_time (OutputVideoFrame f) const
899 return f * TIME_HZ / dcp_video_frame_rate ();
903 Film::dcp_audio_frame_rate () const
910 Film::set_sequence_video (bool s)
912 _playlist->set_sequence_video (s);
916 Film::full_frame () const
918 return libdcp::Size (2048, 1080);