Merge master.
[dcpomatic.git] / src / lib / film.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <stdexcept>
21 #include <iostream>
22 #include <algorithm>
23 #include <fstream>
24 #include <cstdlib>
25 #include <sstream>
26 #include <iomanip>
27 #include <unistd.h>
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <libxml++/libxml++.h>
32 #include <libcxml/cxml.h>
33 #include <dcp/signer_chain.h>
34 #include <dcp/cpl.h>
35 #include <dcp/signer.h>
36 #include <dcp/util.h>
37 #include <dcp/local_time.h>
38 #include <dcp/raw_convert.h>
39 #include "film.h"
40 #include "job.h"
41 #include "util.h"
42 #include "job_manager.h"
43 #include "transcode_job.h"
44 #include "scp_dcp_job.h"
45 #include "log.h"
46 #include "exceptions.h"
47 #include "examine_content_job.h"
48 #include "scaler.h"
49 #include "config.h"
50 #include "version.h"
51 #include "ui_signaller.h"
52 #include "playlist.h"
53 #include "player.h"
54 #include "dcp_content_type.h"
55 #include "ratio.h"
56 #include "cross.h"
57 #include "cinema.h"
58
59 #include "i18n.h"
60
61 using std::string;
62 using std::stringstream;
63 using std::multimap;
64 using std::pair;
65 using std::map;
66 using std::vector;
67 using std::setfill;
68 using std::min;
69 using std::make_pair;
70 using std::endl;
71 using std::cout;
72 using std::list;
73 using boost::shared_ptr;
74 using boost::weak_ptr;
75 using boost::dynamic_pointer_cast;
76 using boost::to_upper_copy;
77 using boost::ends_with;
78 using boost::starts_with;
79 using boost::optional;
80 using dcp::Size;
81 using dcp::Signer;
82 using dcp::raw_convert;
83
84 #define LOG_GENERAL(...) log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
85 #define LOG_GENERAL_NC(...) log()->log (__VA_ARGS__, Log::TYPE_GENERAL);
86
87 /* 5 -> 6
88  * AudioMapping XML changed.
89  * 6 -> 7
90  * Subtitle offset changed to subtitle y offset, and subtitle x offset added.
91  * 7 -> 8
92  * Use <Scale> tag in <VideoContent> rather than <Ratio>.
93  *
94  * Bumped to 32 for 2.0 branch; some times are expressed in Times rather
95  * than frames now.
96  */
97 int const Film::current_state_version = 32;
98
99 /** Construct a Film object in a given directory.
100  *
101  *  @param dir Film directory.
102  */
103
104 Film::Film (boost::filesystem::path dir, bool log)
105         : _playlist (new Playlist)
106         , _use_dci_name (true)
107         , _dcp_content_type (Config::instance()->default_dcp_content_type ())
108         , _container (Config::instance()->default_container ())
109         , _resolution (RESOLUTION_2K)
110         , _scaler (Scaler::from_id ("bicubic"))
111         , _with_subtitles (false)
112         , _signed (true)
113         , _encrypted (false)
114         , _j2k_bandwidth (Config::instance()->default_j2k_bandwidth ())
115         , _dci_metadata (Config::instance()->default_dci_metadata ())
116         , _video_frame_rate (24)
117         , _audio_channels (6)
118         , _three_d (false)
119         , _sequence_video (true)
120         , _interop (false)
121         , _state_version (current_state_version)
122         , _dirty (false)
123 {
124         set_dci_date_today ();
125
126         _playlist->Changed.connect (bind (&Film::playlist_changed, this));
127         _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
128         
129         /* Make state.directory a complete path without ..s (where possible)
130            (Code swiped from Adam Bowen on stackoverflow)
131         */
132         
133         boost::filesystem::path p (boost::filesystem::system_complete (dir));
134         boost::filesystem::path result;
135         for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
136                 if (*i == "..") {
137                         if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
138                                 result /= *i;
139                         } else {
140                                 result = result.parent_path ();
141                         }
142                 } else if (*i != ".") {
143                         result /= *i;
144                 }
145         }
146
147         set_directory (result);
148         if (log) {
149                 _log.reset (new FileLog (file ("log")));
150         } else {
151                 _log.reset (new NullLog);
152         }
153
154         _playlist->set_sequence_video (_sequence_video);
155 }
156
157 string
158 Film::video_identifier () const
159 {
160         assert (container ());
161
162         stringstream s;
163         s.imbue (std::locale::classic ());
164         
165         s << container()->id()
166           << "_" << resolution_to_string (_resolution)
167           << "_" << _playlist->video_identifier()
168           << "_" << _video_frame_rate
169           << "_" << scaler()->id()
170           << "_" << j2k_bandwidth();
171
172         if (encrypted ()) {
173                 s << "_E";
174         } else {
175                 s << "_P";
176         }
177
178         if (_interop) {
179                 s << "_I";
180         } else {
181                 s << "_S";
182         }
183
184         if (_three_d) {
185                 s << "_3D";
186         }
187
188         return s.str ();
189 }
190           
191 /** @return The path to the directory to write video frame info files to */
192 boost::filesystem::path
193 Film::info_dir () const
194 {
195         boost::filesystem::path p;
196         p /= "info";
197         p /= video_identifier ();
198         return dir (p);
199 }
200
201 boost::filesystem::path
202 Film::internal_video_mxf_dir () const
203 {
204         return dir ("video");
205 }
206
207 boost::filesystem::path
208 Film::internal_video_mxf_filename () const
209 {
210         return video_identifier() + ".mxf";
211 }
212
213 boost::filesystem::path
214 Film::video_mxf_filename () const
215 {
216         return filename_safe_name() + "_video.mxf";
217 }
218
219 boost::filesystem::path
220 Film::audio_mxf_filename () const
221 {
222         return filename_safe_name() + "_audio.mxf";
223 }
224
225 string
226 Film::filename_safe_name () const
227 {
228         string const n = name ();
229         string o;
230         for (size_t i = 0; i < n.length(); ++i) {
231                 if (isalnum (n[i])) {
232                         o += n[i];
233                 } else {
234                         o += "_";
235                 }
236         }
237
238         return o;
239 }
240
241 boost::filesystem::path
242 Film::audio_analysis_dir () const
243 {
244         return dir ("analysis");
245 }
246
247 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
248 void
249 Film::make_dcp ()
250 {
251         set_dci_date_today ();
252         
253         if (dcp_name().find ("/") != string::npos) {
254                 throw BadSettingError (_("name"), _("cannot contain slashes"));
255         }
256
257         /* It seems to make sense to auto-save metadata here, since the make DCP may last
258            a long time, and crashes/power failures are moderately likely.
259          */
260         write_metadata ();
261
262         LOG_GENERAL ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary());
263
264         {
265                 char buffer[128];
266                 gethostname (buffer, sizeof (buffer));
267                 LOG_GENERAL ("Starting to make DCP on %1", buffer);
268         }
269
270         ContentList cl = content ();
271         for (ContentList::const_iterator i = cl.begin(); i != cl.end(); ++i) {
272                 LOG_GENERAL ("Content: %1", (*i)->technical_summary());
273         }
274         LOG_GENERAL ("DCP video rate %1 fps", video_frame_rate());
275         LOG_GENERAL ("%1 threads", Config::instance()->num_local_encoding_threads());
276         LOG_GENERAL ("J2K bandwidth %1", j2k_bandwidth());
277 #ifdef DCPOMATIC_DEBUG
278         LOG_GENERAL_NC ("DCP-o-matic built in debug mode.");
279 #else
280         LOG_GENERAL_NC ("DCP-o-matic built in optimised mode.");
281 #endif
282 #ifdef LIBDCP_DEBUG
283         LOG_GENERAL_NC ("libdcp built in debug mode.");
284 #else
285         LOG_GENERAL_NC ("libdcp built in optimised mode.");
286 #endif
287
288 #ifdef DCPOMATIC_WINDOWS
289         OSVERSIONINFO info;
290         info.dwOSVersionInfoSize = sizeof (info);
291         GetVersionEx (&info);
292         LOG_GENERAL ("Windows version %1.%2.%3 SP %4", info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber, info.szCSDVersion);
293 #endif  
294         
295         LOG_GENERAL ("CPU: %1, %2 processors", cpu_info(), boost::thread::hardware_concurrency ());
296         list<pair<string, string> > const m = mount_info ();
297         for (list<pair<string, string> >::const_iterator i = m.begin(); i != m.end(); ++i) {
298                 LOG_GENERAL ("Mount: %1 %2", i->first, i->second);
299         }
300         
301         if (container() == 0) {
302                 throw MissingSettingError (_("container"));
303         }
304
305         if (content().empty()) {
306                 throw StringError (_("You must add some content to the DCP before creating it"));
307         }
308
309         if (dcp_content_type() == 0) {
310                 throw MissingSettingError (_("content type"));
311         }
312
313         if (name().empty()) {
314                 throw MissingSettingError (_("name"));
315         }
316
317         JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
318 }
319
320 /** Start a job to send our DCP to the configured TMS */
321 void
322 Film::send_dcp_to_tms ()
323 {
324         shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
325         JobManager::instance()->add (j);
326 }
327
328 /** Count the number of frames that have been encoded for this film.
329  *  @return frame count.
330  */
331 int
332 Film::encoded_frames () const
333 {
334         if (container() == 0) {
335                 return 0;
336         }
337
338         int N = 0;
339         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
340                 ++N;
341                 boost::this_thread::interruption_point ();
342         }
343
344         return N;
345 }
346
347 shared_ptr<xmlpp::Document>
348 Film::metadata () const
349 {
350         shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
351         xmlpp::Element* root = doc->create_root_node ("Metadata");
352
353         root->add_child("Version")->add_child_text (raw_convert<string> (current_state_version));
354         root->add_child("Name")->add_child_text (_name);
355         root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
356
357         if (_dcp_content_type) {
358                 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
359         }
360
361         if (_container) {
362                 root->add_child("Container")->add_child_text (_container->id ());
363         }
364
365         root->add_child("Resolution")->add_child_text (resolution_to_string (_resolution));
366         root->add_child("Scaler")->add_child_text (_scaler->id ());
367         root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
368         root->add_child("J2KBandwidth")->add_child_text (raw_convert<string> (_j2k_bandwidth));
369         _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
370         root->add_child("VideoFrameRate")->add_child_text (raw_convert<string> (_video_frame_rate));
371         root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
372         root->add_child("AudioChannels")->add_child_text (raw_convert<string> (_audio_channels));
373         root->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
374         root->add_child("SequenceVideo")->add_child_text (_sequence_video ? "1" : "0");
375         root->add_child("Interop")->add_child_text (_interop ? "1" : "0");
376         root->add_child("Signed")->add_child_text (_signed ? "1" : "0");
377         root->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
378         root->add_child("Key")->add_child_text (_key.hex ());
379         _playlist->as_xml (root->add_child ("Playlist"));
380
381         return doc;
382 }
383
384 /** Write state to our `metadata' file */
385 void
386 Film::write_metadata () const
387 {
388         boost::filesystem::create_directories (directory ());
389         shared_ptr<xmlpp::Document> doc = metadata ();
390         doc->write_to_file_formatted (file("metadata.xml").string ());
391         _dirty = false;
392 }
393
394 /** Read state from our metadata file.
395  *  @return Notes about things that the user should know about, or empty.
396  */
397 list<string>
398 Film::read_metadata ()
399 {
400         if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
401                 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!"));
402         }
403
404         cxml::Document f ("Metadata");
405         f.read_file (file ("metadata.xml"));
406
407         _state_version = f.number_child<int> ("Version");
408         if (_state_version > current_state_version) {
409                 throw StringError (_("This film was created with a newer version of DCP-o-matic, and it cannot be loaded into this version.  Sorry!"));
410         }
411         
412         _name = f.string_child ("Name");
413         _use_dci_name = f.bool_child ("UseDCIName");
414
415         {
416                 optional<string> c = f.optional_string_child ("DCPContentType");
417                 if (c) {
418                         _dcp_content_type = DCPContentType::from_dci_name (c.get ());
419                 }
420         }
421
422         {
423                 optional<string> c = f.optional_string_child ("Container");
424                 if (c) {
425                         _container = Ratio::from_id (c.get ());
426                 }
427         }
428
429         _resolution = string_to_resolution (f.string_child ("Resolution"));
430         _scaler = Scaler::from_id (f.string_child ("Scaler"));
431         _with_subtitles = f.bool_child ("WithSubtitles");
432         _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
433         _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
434         _video_frame_rate = f.number_child<int> ("VideoFrameRate");
435         _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
436         _signed = f.optional_bool_child("Signed").get_value_or (true);
437         _encrypted = f.bool_child ("Encrypted");
438         _audio_channels = f.number_child<int> ("AudioChannels");
439         _sequence_video = f.bool_child ("SequenceVideo");
440         _three_d = f.bool_child ("ThreeD");
441         _interop = f.bool_child ("Interop");
442         _key = dcp::Key (f.string_child ("Key"));
443
444         list<string> notes;
445         /* This method is the only one that can return notes (so far) */
446         _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"), _state_version, notes);
447
448         _dirty = false;
449         return notes;
450 }
451
452 /** Given a directory name, return its full path within the Film's directory.
453  *  The directory (and its parents) will be created if they do not exist.
454  */
455 boost::filesystem::path
456 Film::dir (boost::filesystem::path d) const
457 {
458         boost::filesystem::path p;
459         p /= _directory;
460         p /= d;
461         
462         boost::filesystem::create_directories (p);
463         
464         return p;
465 }
466
467 /** Given a file or directory name, return its full path within the Film's directory.
468  *  Any required parent directories will be created.
469  */
470 boost::filesystem::path
471 Film::file (boost::filesystem::path f) const
472 {
473         boost::filesystem::path p;
474         p /= _directory;
475         p /= f;
476
477         boost::filesystem::create_directories (p.parent_path ());
478         
479         return p;
480 }
481
482 /** @return a DCI-compliant name for a DCP of this film */
483 string
484 Film::dci_name (bool if_created_now) const
485 {
486         stringstream d;
487
488         string fixed_name = to_upper_copy (name());
489         for (size_t i = 0; i < fixed_name.length(); ++i) {
490                 if (fixed_name[i] == ' ') {
491                         fixed_name[i] = '-';
492                 }
493         }
494
495         /* Spec is that the name part should be maximum 14 characters, as I understand it */
496         if (fixed_name.length() > 14) {
497                 fixed_name = fixed_name.substr (0, 14);
498         }
499
500         d << fixed_name;
501
502         if (dcp_content_type()) {
503                 d << "_" << dcp_content_type()->dci_name();
504                 d << "-" << dci_metadata().content_version;
505         }
506
507         if (three_d ()) {
508                 d << "-3D";
509         }
510
511         if (video_frame_rate() != 24) {
512                 d << "-" << video_frame_rate();
513         }
514
515         if (container()) {
516                 d << "_" << container()->dci_name();
517         }
518
519         DCIMetadata const dm = dci_metadata ();
520
521         if (!dm.audio_language.empty ()) {
522                 d << "_" << dm.audio_language;
523                 if (!dm.subtitle_language.empty()) {
524                         d << "-" << dm.subtitle_language;
525                 } else {
526                         d << "-XX";
527                 }
528         }
529
530         if (!dm.territory.empty ()) {
531                 d << "_" << dm.territory;
532                 if (!dm.rating.empty ()) {
533                         d << "-" << dm.rating;
534                 }
535         }
536
537         switch (audio_channels ()) {
538         case 1:
539                 d << "_10";
540                 break;
541         case 2:
542                 d << "_20";
543                 break;
544         case 3:
545                 d << "_30";
546                 break;
547         case 4:
548                 d << "_40";
549                 break;
550         case 5:
551                 d << "_50";
552                 break;
553         case 6:
554                 d << "_51";
555                 break;
556         }
557
558         d << "_" << resolution_to_string (_resolution);
559
560         if (!dm.studio.empty ()) {
561                 d << "_" << dm.studio;
562         }
563
564         if (if_created_now) {
565                 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
566         } else {
567                 d << "_" << boost::gregorian::to_iso_string (_dci_date);
568         }
569
570         if (!dm.facility.empty ()) {
571                 d << "_" << dm.facility;
572         }
573
574         if (!dm.package_type.empty ()) {
575                 d << "_" << dm.package_type;
576         }
577
578         return d.str ();
579 }
580
581 /** @return name to give the DCP */
582 string
583 Film::dcp_name (bool if_created_now) const
584 {
585         if (use_dci_name()) {
586                 return dci_name (if_created_now);
587         }
588
589         return name();
590 }
591
592
593 void
594 Film::set_directory (boost::filesystem::path d)
595 {
596         _directory = d;
597         _dirty = true;
598 }
599
600 void
601 Film::set_name (string n)
602 {
603         _name = n;
604         signal_changed (NAME);
605 }
606
607 void
608 Film::set_use_dci_name (bool u)
609 {
610         _use_dci_name = u;
611         signal_changed (USE_DCI_NAME);
612 }
613
614 void
615 Film::set_dcp_content_type (DCPContentType const * t)
616 {
617         _dcp_content_type = t;
618         signal_changed (DCP_CONTENT_TYPE);
619 }
620
621 void
622 Film::set_container (Ratio const * c)
623 {
624         _container = c;
625         signal_changed (CONTAINER);
626 }
627
628 void
629 Film::set_resolution (Resolution r)
630 {
631         _resolution = r;
632         signal_changed (RESOLUTION);
633 }
634
635 void
636 Film::set_scaler (Scaler const * s)
637 {
638         _scaler = s;
639         signal_changed (SCALER);
640 }
641
642 void
643 Film::set_with_subtitles (bool w)
644 {
645         _with_subtitles = w;
646         signal_changed (WITH_SUBTITLES);
647 }
648
649 void
650 Film::set_j2k_bandwidth (int b)
651 {
652         _j2k_bandwidth = b;
653         signal_changed (J2K_BANDWIDTH);
654 }
655
656 void
657 Film::set_dci_metadata (DCIMetadata m)
658 {
659         _dci_metadata = m;
660         signal_changed (DCI_METADATA);
661 }
662
663 void
664 Film::set_video_frame_rate (int f)
665 {
666         _video_frame_rate = f;
667         signal_changed (VIDEO_FRAME_RATE);
668 }
669
670 void
671 Film::set_audio_channels (int c)
672 {
673         _audio_channels = c;
674         signal_changed (AUDIO_CHANNELS);
675 }
676
677 void
678 Film::set_three_d (bool t)
679 {
680         _three_d = t;
681         signal_changed (THREE_D);
682 }
683
684 void
685 Film::set_interop (bool i)
686 {
687         _interop = i;
688         signal_changed (INTEROP);
689 }
690
691 void
692 Film::signal_changed (Property p)
693 {
694         _dirty = true;
695
696         switch (p) {
697         case Film::CONTENT:
698                 set_video_frame_rate (_playlist->best_dcp_frame_rate ());
699                 break;
700         case Film::VIDEO_FRAME_RATE:
701         case Film::SEQUENCE_VIDEO:
702                 _playlist->maybe_sequence_video ();
703                 break;
704         default:
705                 break;
706         }
707
708         if (ui_signaller) {
709                 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
710         }
711 }
712
713 void
714 Film::set_dci_date_today ()
715 {
716         _dci_date = boost::gregorian::day_clock::local_day ();
717 }
718
719 boost::filesystem::path
720 Film::info_path (int f, Eyes e) const
721 {
722         boost::filesystem::path p;
723         p /= info_dir ();
724
725         stringstream s;
726         s.width (8);
727         s << setfill('0') << f;
728
729         if (e == EYES_LEFT) {
730                 s << ".L";
731         } else if (e == EYES_RIGHT) {
732                 s << ".R";
733         }
734
735         s << ".md5";
736         
737         p /= s.str();
738
739         /* info_dir() will already have added any initial bit of the path,
740            so don't call file() on this.
741         */
742         return p;
743 }
744
745 boost::filesystem::path
746 Film::j2c_path (int f, Eyes e, bool t) const
747 {
748         boost::filesystem::path p;
749         p /= "j2c";
750         p /= video_identifier ();
751
752         stringstream s;
753         s.width (8);
754         s << setfill('0') << f;
755
756         if (e == EYES_LEFT) {
757                 s << ".L";
758         } else if (e == EYES_RIGHT) {
759                 s << ".R";
760         }
761         
762         s << ".j2c";
763
764         if (t) {
765                 s << ".tmp";
766         }
767
768         p /= s.str();
769         return file (p);
770 }
771
772 /** Find all the DCPs in our directory that can be libdcp::DCP::read() and return details of their CPLs */
773 vector<CPLSummary>
774 Film::cpls () const
775 {
776         vector<CPLSummary> out;
777         
778         boost::filesystem::path const dir = directory ();
779         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator(dir); i != boost::filesystem::directory_iterator(); ++i) {
780                 if (
781                         boost::filesystem::is_directory (*i) &&
782                         i->path().leaf() != "j2c" && i->path().leaf() != "video" && i->path().leaf() != "info" && i->path().leaf() != "analysis"
783                         ) {
784
785                         try {
786                                 dcp::DCP dcp (*i);
787                                 dcp.read ();
788                                 out.push_back (
789                                         CPLSummary (
790                                                 i->path().leaf().string(),
791                                                 dcp.cpls().front()->id(),
792                                                 dcp.cpls().front()->annotation_text(),
793                                                 dcp.cpls().front()->file()
794                                                 )
795                                         );
796                         } catch (...) {
797
798                         }
799                 }
800         }
801         
802         return out;
803 }
804
805 shared_ptr<Player>
806 Film::make_player () const
807 {
808         return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
809 }
810
811 void
812 Film::set_signed (bool s)
813 {
814         _signed = s;
815         signal_changed (SIGNED);
816 }
817
818 void
819 Film::set_encrypted (bool e)
820 {
821         _encrypted = e;
822         signal_changed (ENCRYPTED);
823 }
824
825 shared_ptr<Playlist>
826 Film::playlist () const
827 {
828         return _playlist;
829 }
830
831 ContentList
832 Film::content () const
833 {
834         return _playlist->content ();
835 }
836
837 void
838 Film::examine_and_add_content (shared_ptr<Content> c)
839 {
840         if (dynamic_pointer_cast<FFmpegContent> (c)) {
841                 run_ffprobe (c->path(0), file ("ffprobe.log"), _log);
842         }
843                         
844         shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
845         j->Finished.connect (bind (&Film::maybe_add_content, this, boost::weak_ptr<Job> (j), boost::weak_ptr<Content> (c)));
846         JobManager::instance()->add (j);
847 }
848
849 void
850 Film::maybe_add_content (weak_ptr<Job> j, weak_ptr<Content> c)
851 {
852         shared_ptr<Job> job = j.lock ();
853         if (!job || !job->finished_ok ()) {
854                 return;
855         }
856         
857         shared_ptr<Content> content = c.lock ();
858         if (content) {
859                 add_content (content);
860         }
861 }
862
863 void
864 Film::add_content (shared_ptr<Content> c)
865 {
866         /* Add video content after any existing content */
867         if (dynamic_pointer_cast<VideoContent> (c)) {
868                 c->set_position (_playlist->video_end ());
869         }
870
871         _playlist->add (c);
872 }
873
874 void
875 Film::remove_content (shared_ptr<Content> c)
876 {
877         _playlist->remove (c);
878 }
879
880 void
881 Film::move_content_earlier (shared_ptr<Content> c)
882 {
883         _playlist->move_earlier (c);
884 }
885
886 void
887 Film::move_content_later (shared_ptr<Content> c)
888 {
889         _playlist->move_later (c);
890 }
891
892 DCPTime
893 Film::length () const
894 {
895         return _playlist->length ();
896 }
897
898 bool
899 Film::has_subtitles () const
900 {
901         return _playlist->has_subtitles ();
902 }
903
904 int
905 Film::best_video_frame_rate () const
906 {
907         return _playlist->best_dcp_frame_rate ();
908 }
909
910 FrameRateChange
911 Film::active_frame_rate_change (DCPTime t) const
912 {
913         return _playlist->active_frame_rate_change (t, video_frame_rate ());
914 }
915
916 void
917 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
918 {
919         if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
920                 set_video_frame_rate (_playlist->best_dcp_frame_rate ());
921         } 
922
923         if (ui_signaller) {
924                 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
925         }
926 }
927
928 void
929 Film::playlist_changed ()
930 {
931         signal_changed (CONTENT);
932 }       
933
934 int
935 Film::audio_frame_rate () const
936 {
937         /* XXX */
938         return 48000;
939 }
940
941 void
942 Film::set_sequence_video (bool s)
943 {
944         _sequence_video = s;
945         _playlist->set_sequence_video (s);
946         signal_changed (SEQUENCE_VIDEO);
947 }
948
949 /** @return Size of the largest possible image in whatever resolution we are using */
950 dcp::Size
951 Film::full_frame () const
952 {
953         switch (_resolution) {
954         case RESOLUTION_2K:
955                 return dcp::Size (2048, 1080);
956         case RESOLUTION_4K:
957                 return dcp::Size (4096, 2160);
958         }
959
960         assert (false);
961         return dcp::Size ();
962 }
963
964 /** @return Size of the frame */
965 dcp::Size
966 Film::frame_size () const
967 {
968         return fit_ratio_within (container()->ratio(), full_frame ());
969 }
970
971 dcp::EncryptedKDM
972 Film::make_kdm (
973         shared_ptr<dcp::Certificate> target,
974         boost::filesystem::path cpl_file,
975         dcp::LocalTime from,
976         dcp::LocalTime until
977         ) const
978 {
979         shared_ptr<const dcp::CPL> cpl (new dcp::CPL (cpl_file));
980         return dcp::DecryptedKDM (
981                 cpl, from, until, "DCP-o-matic", cpl->content_title_text(), dcp::LocalTime().as_string()
982                 ).encrypt (make_signer(), target);
983 }
984
985 list<dcp::EncryptedKDM>
986 Film::make_kdms (
987         list<shared_ptr<Screen> > screens,
988         boost::filesystem::path dcp,
989         dcp::LocalTime from,
990         dcp::LocalTime until
991         ) const
992 {
993         list<dcp::EncryptedKDM> kdms;
994
995         for (list<shared_ptr<Screen> >::iterator i = screens.begin(); i != screens.end(); ++i) {
996                 kdms.push_back (make_kdm ((*i)->certificate, dcp, from, until));
997         }
998
999         return kdms;
1000 }
1001
1002 /** @return The approximate disk space required to encode a DCP of this film with the
1003  *  current settings, in bytes.
1004  */
1005 uint64_t
1006 Film::required_disk_space () const
1007 {
1008         return uint64_t (j2k_bandwidth() / 8) * length().seconds();
1009 }
1010
1011 /** This method checks the disk that the Film is on and tries to decide whether or not
1012  *  there will be enough space to make a DCP for it.  If so, true is returned; if not,
1013  *  false is returned and required and availabe are filled in with the amount of disk space
1014  *  required and available respectively (in Gb).
1015  *
1016  *  Note: the decision made by this method isn't, of course, 100% reliable.
1017  */
1018 bool
1019 Film::should_be_enough_disk_space (double& required, double& available) const
1020 {
1021         boost::filesystem::space_info s = boost::filesystem::space (internal_video_mxf_dir ());
1022         required = double (required_disk_space ()) / 1073741824.0f;
1023         available = double (s.available) / 1073741824.0f;
1024         return (available - required) > 1;
1025 }