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.
88 * @param d Film directory.
92 : _playlist (new Playlist)
93 , _use_dci_name (true)
94 , _dcp_content_type (Config::instance()->default_dcp_content_type ())
95 , _container (Config::instance()->default_container ())
96 , _scaler (Scaler::from_id ("bicubic"))
98 , _with_subtitles (false)
99 , _subtitle_offset (0)
100 , _subtitle_scale (1)
102 , _j2k_bandwidth (200000000)
103 , _dci_metadata (Config::instance()->default_dci_metadata ())
104 , _dcp_video_frame_rate (0)
105 , _dcp_audio_channels (MAX_AUDIO_CHANNELS)
108 set_dci_date_today ();
110 _playlist->Changed.connect (bind (&Film::playlist_changed, this));
111 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
113 /* Make state.directory a complete path without ..s (where possible)
114 (Code swiped from Adam Bowen on stackoverflow)
117 boost::filesystem::path p (boost::filesystem::system_complete (d));
118 boost::filesystem::path result;
119 for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
121 if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
124 result = result.parent_path ();
126 } else if (*i != ".") {
131 set_directory (result.string ());
132 _log.reset (new FileLog (file ("log")));
135 Film::Film (Film const & o)
136 : boost::enable_shared_from_this<Film> (o)
137 /* note: the copied film shares the original's log */
139 , _playlist (new Playlist (o._playlist))
140 , _directory (o._directory)
142 , _use_dci_name (o._use_dci_name)
143 , _dcp_content_type (o._dcp_content_type)
144 , _container (o._container)
145 , _scaler (o._scaler)
147 , _with_subtitles (o._with_subtitles)
148 , _subtitle_offset (o._subtitle_offset)
149 , _subtitle_scale (o._subtitle_scale)
150 , _colour_lut (o._colour_lut)
151 , _j2k_bandwidth (o._j2k_bandwidth)
152 , _dci_metadata (o._dci_metadata)
153 , _dcp_video_frame_rate (o._dcp_video_frame_rate)
154 , _dci_date (o._dci_date)
157 _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
161 Film::video_state_identifier () const
163 assert (container ());
167 s << container()->id()
168 << "_" << _playlist->video_digest()
169 << "_" << _dcp_video_frame_rate
170 << "_" << scaler()->id()
171 << "_" << j2k_bandwidth()
172 << "_" << lexical_cast<int> (colour_lut());
175 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
176 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
182 /** @return The path to the directory to write video frame info files to */
184 Film::info_dir () const
186 boost::filesystem::path p;
188 p /= video_state_identifier ();
189 return dir (p.string());
193 Film::internal_video_mxf_dir () const
195 boost::filesystem::path p;
196 return dir ("video");
200 Film::internal_video_mxf_filename () const
202 return video_state_identifier() + ".mxf";
206 Film::dcp_video_mxf_filename () const
208 return filename_safe_name() + "_video.mxf";
212 Film::dcp_audio_mxf_filename () const
214 return filename_safe_name() + "_audio.mxf";
218 Film::filename_safe_name () const
220 string const n = name ();
222 for (size_t i = 0; i < n.length(); ++i) {
223 if (isalnum (n[i])) {
234 Film::audio_analysis_path () const
236 boost::filesystem::path p;
238 p /= _playlist->audio_digest();
239 return file (p.string ());
242 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
246 set_dci_date_today ();
248 if (dcp_name().find ("/") != string::npos) {
249 throw BadSettingError (_("name"), _("cannot contain slashes"));
252 log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
256 gethostname (buffer, sizeof (buffer));
257 log()->log (String::compose ("Starting to make DCP on %1", buffer));
260 // log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
262 // log()->log (String::compose ("Content length %1", length().get()));
264 // log()->log (String::compose ("Content digest %1", content_digest()));
265 // log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
266 log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
267 log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
268 #ifdef DCPOMATIC_DEBUG
269 log()->log ("DCP-o-matic built in debug mode.");
271 log()->log ("DCP-o-matic built in optimised mode.");
274 log()->log ("libdcp built in debug mode.");
276 log()->log ("libdcp built in optimised mode.");
278 pair<string, int> const c = cpu_info ();
279 log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
281 if (container() == 0) {
282 throw MissingSettingError (_("container"));
285 if (_playlist->content().empty ()) {
286 throw StringError (_("You must add some content to the DCP before creating it"));
289 if (dcp_content_type() == 0) {
290 throw MissingSettingError (_("content type"));
293 if (name().empty()) {
294 throw MissingSettingError (_("name"));
300 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
302 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
306 /** Start a job to analyse the audio in our Playlist */
308 Film::analyse_audio ()
310 if (_analyse_audio_job) {
314 _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
315 _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
316 JobManager::instance()->add (_analyse_audio_job);
319 /** Start a job to examine a piece of content */
321 Film::examine_content (shared_ptr<Content> c)
323 shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
324 JobManager::instance()->add (j);
328 Film::analyse_audio_finished ()
332 if (_analyse_audio_job->finished_ok ()) {
333 AudioAnalysisSucceeded ();
336 _analyse_audio_job.reset ();
339 /** Start a job to send our DCP to the configured TMS */
341 Film::send_dcp_to_tms ()
343 shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
344 JobManager::instance()->add (j);
347 /** Count the number of frames that have been encoded for this film.
348 * @return frame count.
351 Film::encoded_frames () const
353 if (container() == 0) {
358 for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
360 boost::this_thread::interruption_point ();
366 /** Write state to our `metadata' file */
368 Film::write_metadata () const
370 if (!boost::filesystem::exists (directory())) {
371 boost::filesystem::create_directory (directory());
374 boost::mutex::scoped_lock lm (_state_mutex);
377 boost::filesystem::create_directories (directory());
380 xmlpp::Element* root = doc.create_root_node ("Metadata");
382 root->add_child("Version")->add_child_text (lexical_cast<string> (state_version));
383 root->add_child("Name")->add_child_text (_name);
384 root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
386 if (_dcp_content_type) {
387 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
391 root->add_child("Container")->add_child_text (_container->id ());
394 root->add_child("Scaler")->add_child_text (_scaler->id ());
395 root->add_child("AB")->add_child_text (_ab ? "1" : "0");
396 root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
397 root->add_child("SubtitleOffset")->add_child_text (lexical_cast<string> (_subtitle_offset));
398 root->add_child("SubtitleScale")->add_child_text (lexical_cast<string> (_subtitle_scale));
399 root->add_child("ColourLUT")->add_child_text (lexical_cast<string> (_colour_lut));
400 root->add_child("J2KBandwidth")->add_child_text (lexical_cast<string> (_j2k_bandwidth));
401 _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
402 root->add_child("DCPVideoFrameRate")->add_child_text (lexical_cast<string> (_dcp_video_frame_rate));
403 root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
404 root->add_child("DCPAudioChannels")->add_child_text (lexical_cast<string> (_dcp_audio_channels));
405 _playlist->as_xml (root->add_child ("Playlist"));
407 doc.write_to_file_formatted (file ("metadata.xml"));
412 /** Read state from our metadata file */
414 Film::read_metadata ()
416 boost::mutex::scoped_lock lm (_state_mutex);
419 if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
420 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!"));
423 cxml::File f (file ("metadata.xml"), "Metadata");
425 _name = f.string_child ("Name");
426 _use_dci_name = f.bool_child ("UseDCIName");
429 optional<string> c = f.optional_string_child ("DCPContentType");
431 _dcp_content_type = DCPContentType::from_dci_name (c.get ());
436 optional<string> c = f.optional_string_child ("Container");
438 _container = Container::from_id (c.get ());
442 _scaler = Scaler::from_id (f.string_child ("Scaler"));
443 _ab = f.bool_child ("AB");
444 _with_subtitles = f.bool_child ("WithSubtitles");
445 _subtitle_offset = f.number_child<float> ("SubtitleOffset");
446 _subtitle_scale = f.number_child<float> ("SubtitleScale");
447 _colour_lut = f.number_child<int> ("ColourLUT");
448 _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
449 _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
450 _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
451 _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
452 _dcp_audio_channels = f.number_child<int> ("DCPAudioChannels");
454 _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
459 /** Given a directory name, return its full path within the Film's directory.
460 * The directory (and its parents) will be created if they do not exist.
463 Film::dir (string d) const
465 boost::mutex::scoped_lock lm (_directory_mutex);
467 boost::filesystem::path p;
471 boost::filesystem::create_directories (p);
476 /** Given a file or directory name, return its full path within the Film's directory.
477 * _directory_mutex must not be locked on entry.
478 * Any required parent directories will be created.
481 Film::file (string f) const
483 boost::mutex::scoped_lock lm (_directory_mutex);
485 boost::filesystem::path p;
489 boost::filesystem::create_directories (p.parent_path ());
494 /** @return a DCI-compliant name for a DCP of this film */
496 Film::dci_name (bool if_created_now) const
500 string fixed_name = to_upper_copy (name());
501 for (size_t i = 0; i < fixed_name.length(); ++i) {
502 if (fixed_name[i] == ' ') {
507 /* Spec is that the name part should be maximum 14 characters, as I understand it */
508 if (fixed_name.length() > 14) {
509 fixed_name = fixed_name.substr (0, 14);
514 if (dcp_content_type()) {
515 d << "_" << dcp_content_type()->dci_name();
519 d << "_" << container()->dci_name();
522 DCIMetadata const dm = dci_metadata ();
524 if (!dm.audio_language.empty ()) {
525 d << "_" << dm.audio_language;
526 if (!dm.subtitle_language.empty()) {
527 d << "-" << dm.subtitle_language;
533 if (!dm.territory.empty ()) {
534 d << "_" << dm.territory;
535 if (!dm.rating.empty ()) {
536 d << "-" << dm.rating;
542 if (!dm.studio.empty ()) {
543 d << "_" << dm.studio;
546 if (if_created_now) {
547 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
549 d << "_" << boost::gregorian::to_iso_string (_dci_date);
552 if (!dm.facility.empty ()) {
553 d << "_" << dm.facility;
556 if (!dm.package_type.empty ()) {
557 d << "_" << dm.package_type;
563 /** @return name to give the DCP */
565 Film::dcp_name (bool if_created_now) const
567 if (use_dci_name()) {
568 return dci_name (if_created_now);
576 Film::set_directory (string d)
578 boost::mutex::scoped_lock lm (_state_mutex);
584 Film::set_name (string n)
587 boost::mutex::scoped_lock lm (_state_mutex);
590 signal_changed (NAME);
594 Film::set_use_dci_name (bool u)
597 boost::mutex::scoped_lock lm (_state_mutex);
600 signal_changed (USE_DCI_NAME);
604 Film::set_dcp_content_type (DCPContentType const * t)
607 boost::mutex::scoped_lock lm (_state_mutex);
608 _dcp_content_type = t;
610 signal_changed (DCP_CONTENT_TYPE);
614 Film::set_container (Container const * c)
617 boost::mutex::scoped_lock lm (_state_mutex);
620 signal_changed (CONTAINER);
624 Film::set_scaler (Scaler const * s)
627 boost::mutex::scoped_lock lm (_state_mutex);
630 signal_changed (SCALER);
634 Film::set_ab (bool a)
637 boost::mutex::scoped_lock lm (_state_mutex);
644 Film::set_with_subtitles (bool w)
647 boost::mutex::scoped_lock lm (_state_mutex);
650 signal_changed (WITH_SUBTITLES);
654 Film::set_subtitle_offset (int o)
657 boost::mutex::scoped_lock lm (_state_mutex);
658 _subtitle_offset = o;
660 signal_changed (SUBTITLE_OFFSET);
664 Film::set_subtitle_scale (float s)
667 boost::mutex::scoped_lock lm (_state_mutex);
670 signal_changed (SUBTITLE_SCALE);
674 Film::set_colour_lut (int i)
677 boost::mutex::scoped_lock lm (_state_mutex);
680 signal_changed (COLOUR_LUT);
684 Film::set_j2k_bandwidth (int b)
687 boost::mutex::scoped_lock lm (_state_mutex);
690 signal_changed (J2K_BANDWIDTH);
694 Film::set_dci_metadata (DCIMetadata m)
697 boost::mutex::scoped_lock lm (_state_mutex);
700 signal_changed (DCI_METADATA);
705 Film::set_dcp_video_frame_rate (int f)
708 boost::mutex::scoped_lock lm (_state_mutex);
709 _dcp_video_frame_rate = f;
711 signal_changed (DCP_VIDEO_FRAME_RATE);
715 Film::signal_changed (Property p)
718 boost::mutex::scoped_lock lm (_state_mutex);
724 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
731 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
736 Film::set_dci_date_today ()
738 _dci_date = boost::gregorian::day_clock::local_day ();
742 Film::info_path (int f) const
744 boost::filesystem::path p;
749 s << setfill('0') << f << ".md5";
753 /* info_dir() will already have added any initial bit of the path,
754 so don't call file() on this.
760 Film::j2c_path (int f, bool t) const
762 boost::filesystem::path p;
764 p /= video_state_identifier ();
768 s << setfill('0') << f << ".j2c";
775 return file (p.string ());
778 /** Make an educated guess as to whether we have a complete DCP
780 * @return true if we do.
784 Film::have_dcp () const
787 libdcp::DCP dcp (dir (dcp_name()));
797 Film::player () const
799 return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
803 Film::playlist () const
805 boost::mutex::scoped_lock lm (_state_mutex);
809 Playlist::ContentList
810 Film::content () const
812 return _playlist->content ();
816 Film::add_content (shared_ptr<Content> c)
823 Film::remove_content (shared_ptr<Content> c)
825 _playlist->remove (c);
829 Film::length () const
831 return _playlist->length ();
835 Film::has_subtitles () const
837 return _playlist->has_subtitles ();
841 Film::best_dcp_video_frame_rate () const
843 return _playlist->best_dcp_frame_rate ();
847 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
849 if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
850 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
854 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
859 Film::playlist_changed ()
861 signal_changed (CONTENT);
867 return _playlist->loop ();
871 Film::set_loop (int c)
873 _playlist->set_loop (c);
877 Film::time_to_audio_frames (Time t) const
879 return t * dcp_audio_frame_rate () / TIME_HZ;
883 Film::time_to_video_frames (Time t) const
885 return t * dcp_video_frame_rate () / TIME_HZ;
889 Film::audio_frames_to_time (OutputAudioFrame f) const
891 return f * TIME_HZ / dcp_audio_frame_rate ();
895 Film::video_frames_to_time (OutputVideoFrame f) const
897 return f * TIME_HZ / dcp_video_frame_rate ();
901 Film::dcp_audio_frame_rate () const