f2c7a654aa348ff1ff9b1fea7bda10523141353e
[dcpomatic.git] / src / lib / film.cc
1 /*
2     Copyright (C) 2012 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 <boost/date_time.hpp>
32 #include <libxml++/libxml++.h>
33 #include <libcxml/cxml.h>
34 #include "film.h"
35 #include "format.h"
36 #include "job.h"
37 #include "filter.h"
38 #include "transcoder.h"
39 #include "util.h"
40 #include "job_manager.h"
41 #include "ab_transcode_job.h"
42 #include "transcode_job.h"
43 #include "scp_dcp_job.h"
44 #include "log.h"
45 #include "exceptions.h"
46 #include "examine_content_job.h"
47 #include "scaler.h"
48 #include "config.h"
49 #include "version.h"
50 #include "ui_signaller.h"
51 #include "video_decoder.h"
52 #include "audio_decoder.h"
53 #include "sndfile_decoder.h"
54 #include "analyse_audio_job.h"
55 #include "playlist.h"
56 #include "ffmpeg_content.h"
57 #include "imagemagick_content.h"
58 #include "sndfile_content.h"
59
60 #include "i18n.h"
61
62 using std::string;
63 using std::stringstream;
64 using std::multimap;
65 using std::pair;
66 using std::map;
67 using std::vector;
68 using std::ifstream;
69 using std::ofstream;
70 using std::setfill;
71 using std::min;
72 using std::make_pair;
73 using std::endl;
74 using std::list;
75 using boost::shared_ptr;
76 using boost::lexical_cast;
77 using boost::to_upper_copy;
78 using boost::ends_with;
79 using boost::starts_with;
80 using boost::optional;
81 using libdcp::Size;
82
83 int const Film::state_version = 4;
84
85 /** Construct a Film object in a given directory, reading any metadata
86  *  file that exists in that directory.  An exception will be thrown if
87  *  must_exist is true and the specified directory does not exist.
88  *
89  *  @param d Film directory.
90  *  @param must_exist true to throw an exception if does not exist.
91  */
92
93 Film::Film (string d, bool must_exist)
94         : _use_dci_name (true)
95         , _trust_content_headers (true)
96         , _dcp_content_type (0)
97         , _format (0)
98         , _scaler (Scaler::from_id ("bicubic"))
99         , _trim_start (0)
100         , _trim_end (0)
101         , _ab (false)
102         , _audio_gain (0)
103         , _audio_delay (0)
104         , _with_subtitles (false)
105         , _subtitle_offset (0)
106         , _subtitle_scale (1)
107         , _colour_lut (0)
108         , _j2k_bandwidth (200000000)
109         , _dci_metadata (Config::instance()->default_dci_metadata ())
110         , _dcp_frame_rate (0)
111         , _dirty (false)
112 {
113         set_dci_date_today ();
114         
115         /* Make state.directory a complete path without ..s (where possible)
116            (Code swiped from Adam Bowen on stackoverflow)
117         */
118         
119         boost::filesystem::path p (boost::filesystem::system_complete (d));
120         boost::filesystem::path result;
121         for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
122                 if (*i == "..") {
123                         if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
124                                 result /= *i;
125                         } else {
126                                 result = result.parent_path ();
127                         }
128                 } else if (*i != ".") {
129                         result /= *i;
130                 }
131         }
132
133         set_directory (result.string ());
134         
135         if (!boost::filesystem::exists (directory())) {
136                 if (must_exist) {
137                         throw OpenFileError (directory());
138                 } else {
139                         boost::filesystem::create_directory (directory());
140                 }
141         }
142
143         if (must_exist) {
144                 read_metadata ();
145         }
146
147         _log.reset (new FileLog (file ("log")));
148 }
149
150 Film::Film (Film const & o)
151         : boost::enable_shared_from_this<Film> (o)
152         /* note: the copied film shares the original's log */
153         , _log               (o._log)
154         , _directory         (o._directory)
155         , _name              (o._name)
156         , _use_dci_name      (o._use_dci_name)
157         , _trust_content_headers (o._trust_content_headers)
158         , _dcp_content_type  (o._dcp_content_type)
159         , _format            (o._format)
160         , _crop              (o._crop)
161         , _filters           (o._filters)
162         , _scaler            (o._scaler)
163         , _trim_start        (o._trim_start)
164         , _trim_end          (o._trim_end)
165         , _ab                (o._ab)
166         , _audio_gain        (o._audio_gain)
167         , _audio_delay       (o._audio_delay)
168         , _with_subtitles    (o._with_subtitles)
169         , _subtitle_offset   (o._subtitle_offset)
170         , _subtitle_scale    (o._subtitle_scale)
171         , _colour_lut        (o._colour_lut)
172         , _j2k_bandwidth     (o._j2k_bandwidth)
173         , _dci_metadata      (o._dci_metadata)
174         , _dci_date          (o._dci_date)
175         , _dcp_frame_rate    (o._dcp_frame_rate)
176         , _dirty             (o._dirty)
177 {
178         
179 }
180
181 Film::~Film ()
182 {
183
184 }
185
186 string
187 Film::video_state_identifier () const
188 {
189         assert (format ());
190
191         return "XXX";
192
193 #if 0   
194
195         pair<string, string> f = Filter::ffmpeg_strings (filters());
196
197         stringstream s;
198         s << format()->id()
199           << "_" << content_digest()
200           << "_" << crop().left << "_" << crop().right << "_" << crop().top << "_" << crop().bottom
201           << "_" << _dcp_frame_rate
202           << "_" << f.first << "_" << f.second
203           << "_" << scaler()->id()
204           << "_" << j2k_bandwidth()
205           << "_" << boost::lexical_cast<int> (colour_lut());
206
207         if (ab()) {
208                 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
209                 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
210         }
211
212         return s.str ();
213 #endif  
214 }
215           
216 /** @return The path to the directory to write video frame info files to */
217 string
218 Film::info_dir () const
219 {
220         boost::filesystem::path p;
221         p /= "info";
222         p /= video_state_identifier ();
223         return dir (p.string());
224 }
225
226 string
227 Film::video_mxf_dir () const
228 {
229         boost::filesystem::path p;
230         return dir ("video");
231 }
232
233 string
234 Film::video_mxf_filename () const
235 {
236         return video_state_identifier() + ".mxf";
237 }
238
239 string
240 Film::audio_analysis_path () const
241 {
242         boost::filesystem::path p;
243         p /= "analysis";
244         p /= "XXX";//content_digest();
245         return file (p.string ());
246 }
247
248 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
249 void
250 Film::make_dcp ()
251 {
252         set_dci_date_today ();
253         
254         if (dcp_name().find ("/") != string::npos) {
255                 throw BadSettingError (_("name"), _("cannot contain slashes"));
256         }
257         
258         log()->log (String::compose ("DVD-o-matic %1 git %2 using %3", dvdomatic_version, dvdomatic_git_commit, dependency_version_summary()));
259
260         {
261                 char buffer[128];
262                 gethostname (buffer, sizeof (buffer));
263                 log()->log (String::compose ("Starting to make DCP on %1", buffer));
264         }
265         
266 //      log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
267 //      if (length()) {
268 //              log()->log (String::compose ("Content length %1", length().get()));
269 //      }
270 //      log()->log (String::compose ("Content digest %1", content_digest()));
271 //      log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
272         log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
273         log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
274 #ifdef DVDOMATIC_DEBUG
275         log()->log ("DVD-o-matic built in debug mode.");
276 #else
277         log()->log ("DVD-o-matic built in optimised mode.");
278 #endif
279 #ifdef LIBDCP_DEBUG
280         log()->log ("libdcp built in debug mode.");
281 #else
282         log()->log ("libdcp built in optimised mode.");
283 #endif
284         pair<string, int> const c = cpu_info ();
285         log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
286         
287         if (format() == 0) {
288                 throw MissingSettingError (_("format"));
289         }
290
291         if (content().empty ()) {
292                 throw MissingSettingError (_("content"));
293         }
294
295         if (dcp_content_type() == 0) {
296                 throw MissingSettingError (_("content type"));
297         }
298
299         if (name().empty()) {
300                 throw MissingSettingError (_("name"));
301         }
302
303         shared_ptr<Job> r;
304
305         if (ab()) {
306                 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
307         } else {
308                 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
309         }
310 }
311
312 /** Start a job to analyse the audio of our content file */
313 void
314 Film::analyse_audio ()
315 {
316         if (_analyse_audio_job) {
317                 return;
318         }
319
320         _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
321         _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
322         JobManager::instance()->add (_analyse_audio_job);
323 }
324
325 /** Start a job to examine a piece of content */
326 void
327 Film::examine_content (shared_ptr<Content> c)
328 {
329         shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c, trust_content_headers ()));
330         j->Finished.connect (bind (&Film::examine_content_finished, this));
331         JobManager::instance()->add (j);
332 }
333
334 void
335 Film::analyse_audio_finished ()
336 {
337         ensure_ui_thread ();
338
339         if (_analyse_audio_job->finished_ok ()) {
340                 AudioAnalysisSucceeded ();
341         }
342         
343         _analyse_audio_job.reset ();
344 }
345
346 void
347 Film::examine_content_finished ()
348 {
349         /* XXX */
350 }
351
352 /** Start a job to send our DCP to the configured TMS */
353 void
354 Film::send_dcp_to_tms ()
355 {
356         shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
357         JobManager::instance()->add (j);
358 }
359
360 /** Count the number of frames that have been encoded for this film.
361  *  @return frame count.
362  */
363 int
364 Film::encoded_frames () const
365 {
366         if (format() == 0) {
367                 return 0;
368         }
369
370         int N = 0;
371         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
372                 ++N;
373                 boost::this_thread::interruption_point ();
374         }
375
376         return N;
377 }
378
379 /** Write state to our `metadata' file */
380 void
381 Film::write_metadata () const
382 {
383         ContentList the_content = content ();
384         
385         boost::mutex::scoped_lock lm (_state_mutex);
386
387         boost::filesystem::create_directories (directory());
388
389         xmlpp::Document doc;
390         xmlpp::Element* root = doc.create_root_node ("Metadata");
391
392         root->add_child("Version")->add_child_text (boost::lexical_cast<string> (state_version));
393         root->add_child("Name")->add_child_text (_name);
394         root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
395         root->add_child("TrustContentHeaders")->add_child_text (_trust_content_headers ? "1" : "0");
396         if (_dcp_content_type) {
397                 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
398         }
399         if (_format) {
400                 root->add_child("Format")->add_child_text (_format->id ());
401         }
402         root->add_child("LeftCrop")->add_child_text (boost::lexical_cast<string> (_crop.left));
403         root->add_child("RightCrop")->add_child_text (boost::lexical_cast<string> (_crop.right));
404         root->add_child("TopCrop")->add_child_text (boost::lexical_cast<string> (_crop.top));
405         root->add_child("BottomCrop")->add_child_text (boost::lexical_cast<string> (_crop.bottom));
406
407         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
408                 root->add_child("Filter")->add_child_text ((*i)->id ());
409         }
410         
411         root->add_child("Scaler")->add_child_text (_scaler->id ());
412         root->add_child("TrimStart")->add_child_text (boost::lexical_cast<string> (_trim_start));
413         root->add_child("TrimEnd")->add_child_text (boost::lexical_cast<string> (_trim_end));
414         root->add_child("AB")->add_child_text (_ab ? "1" : "0");
415         root->add_child("AudioGain")->add_child_text (boost::lexical_cast<string> (_audio_gain));
416         root->add_child("AudioDelay")->add_child_text (boost::lexical_cast<string> (_audio_delay));
417         root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
418         root->add_child("SubtitleOffset")->add_child_text (boost::lexical_cast<string> (_subtitle_offset));
419         root->add_child("SubtitleScale")->add_child_text (boost::lexical_cast<string> (_subtitle_scale));
420         root->add_child("ColourLUT")->add_child_text (boost::lexical_cast<string> (_colour_lut));
421         root->add_child("J2KBandwidth")->add_child_text (boost::lexical_cast<string> (_j2k_bandwidth));
422         _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
423         root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
424         root->add_child("DCPFrameRate")->add_child_text (boost::lexical_cast<string> (_dcp_frame_rate));
425
426         for (ContentList::iterator i = the_content.begin(); i != the_content.end(); ++i) {
427                 (*i)->as_xml (root->add_child ("Content"));
428         }
429
430         doc.write_to_file_formatted (file ("metadata.xml"));
431         
432         _dirty = false;
433 }
434
435 /** Read state from our metadata file */
436 void
437 Film::read_metadata ()
438 {
439         boost::mutex::scoped_lock lm (_state_mutex);
440
441         if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
442                 throw StringError (_("This film was created with an older version of DVD-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!"));
443         }
444
445         cxml::File f (file ("metadata.xml"), "Metadata");
446         
447         _name = f.string_child ("Name");
448         _use_dci_name = f.bool_child ("UseDCIName");
449         _trust_content_headers = f.bool_child ("TrustContentHeaders");
450
451         {
452                 optional<string> c = f.optional_string_child ("DCPContentType");
453                 if (c) {
454                         _dcp_content_type = DCPContentType::from_dci_name (c.get ());
455                 }
456         }
457
458         {
459                 optional<string> c = f.optional_string_child ("Format");
460                 if (c) {
461                         _format = Format::from_id (c.get ());
462                 }
463         }
464
465         _crop.left = f.number_child<int> ("LeftCrop");
466         _crop.right = f.number_child<int> ("RightCrop");
467         _crop.top = f.number_child<int> ("TopCrop");
468         _crop.bottom = f.number_child<int> ("BottomCrop");
469
470         {
471                 list<shared_ptr<cxml::Node> > c = f.node_children ("Filter");
472                 for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
473                         _filters.push_back (Filter::from_id ((*i)->content ()));
474                 }
475         }
476
477         _scaler = Scaler::from_id (f.string_child ("Scaler"));
478         _trim_start = f.number_child<int> ("TrimStart");
479         _trim_end = f.number_child<int> ("TrimEnd");
480         _ab = f.bool_child ("AB");
481         _audio_gain = f.number_child<float> ("AudioGain");
482         _audio_delay = f.number_child<int> ("AudioDelay");
483         _with_subtitles = f.bool_child ("WithSubtitles");
484         _subtitle_offset = f.number_child<float> ("SubtitleOffset");
485         _subtitle_scale = f.number_child<float> ("SubtitleScale");
486         _colour_lut = f.number_child<int> ("ColourLUT");
487         _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
488         _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
489         _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
490         _dcp_frame_rate = f.number_child<int> ("DCPFrameRate");
491
492         list<shared_ptr<cxml::Node> > c = f.node_children ("Content");
493         for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
494
495                 string const type = (*i)->string_child ("Type");
496                 
497                 if (type == "FFmpeg") {
498                         _content.push_back (shared_ptr<Content> (new FFmpegContent (*i)));
499                 } else if (type == "ImageMagick") {
500                         _content.push_back (shared_ptr<Content> (new ImageMagickContent (*i)));
501                 } else if (type == "Sndfile") {
502                         _content.push_back (shared_ptr<Content> (new SndfileContent (*i)));
503                 }
504         }
505
506         _dirty = false;
507 }
508
509 libdcp::Size
510 Film::cropped_size (libdcp::Size s) const
511 {
512         boost::mutex::scoped_lock lm (_state_mutex);
513         s.width -= _crop.left + _crop.right;
514         s.height -= _crop.top + _crop.bottom;
515         return s;
516 }
517
518 /** Given a directory name, return its full path within the Film's directory.
519  *  The directory (and its parents) will be created if they do not exist.
520  */
521 string
522 Film::dir (string d) const
523 {
524         boost::mutex::scoped_lock lm (_directory_mutex);
525         
526         boost::filesystem::path p;
527         p /= _directory;
528         p /= d;
529         
530         boost::filesystem::create_directories (p);
531         
532         return p.string ();
533 }
534
535 /** Given a file or directory name, return its full path within the Film's directory.
536  *  _directory_mutex must not be locked on entry.
537  *  Any required parent directories will be created.
538  */
539 string
540 Film::file (string f) const
541 {
542         boost::mutex::scoped_lock lm (_directory_mutex);
543
544         boost::filesystem::path p;
545         p /= _directory;
546         p /= f;
547
548         boost::filesystem::create_directories (p.parent_path ());
549         
550         return p.string ();
551 }
552
553 /** @return The sampling rate that we will resample the audio to */
554 int
555 Film::target_audio_sample_rate () const
556 {
557         /* XXX: how often is this method called? */
558         
559         boost::shared_ptr<Playlist> p = playlist ();
560         if (p->has_audio ()) {
561                 return 0;
562         }
563         
564         /* Resample to a DCI-approved sample rate */
565         double t = dcp_audio_sample_rate (p->audio_frame_rate());
566
567         FrameRateConversion frc (p->video_frame_rate(), dcp_frame_rate());
568
569         /* Compensate if the DCP is being run at a different frame rate
570            to the source; that is, if the video is run such that it will
571            look different in the DCP compared to the source (slower or faster).
572            skip/repeat doesn't come into effect here.
573         */
574
575         if (frc.change_speed) {
576                 t *= p->video_frame_rate() * frc.factor() / dcp_frame_rate();
577         }
578
579         return rint (t);
580 }
581
582 /** @return a DCI-compliant name for a DCP of this film */
583 string
584 Film::dci_name (bool if_created_now) const
585 {
586         stringstream d;
587
588         string fixed_name = to_upper_copy (name());
589         for (size_t i = 0; i < fixed_name.length(); ++i) {
590                 if (fixed_name[i] == ' ') {
591                         fixed_name[i] = '-';
592                 }
593         }
594
595         /* Spec is that the name part should be maximum 14 characters, as I understand it */
596         if (fixed_name.length() > 14) {
597                 fixed_name = fixed_name.substr (0, 14);
598         }
599
600         d << fixed_name;
601
602         if (dcp_content_type()) {
603                 d << "_" << dcp_content_type()->dci_name();
604         }
605
606         if (format()) {
607                 d << "_" << format()->dci_name();
608         }
609
610         DCIMetadata const dm = dci_metadata ();
611
612         if (!dm.audio_language.empty ()) {
613                 d << "_" << dm.audio_language;
614                 if (!dm.subtitle_language.empty()) {
615                         d << "-" << dm.subtitle_language;
616                 } else {
617                         d << "-XX";
618                 }
619         }
620
621         if (!dm.territory.empty ()) {
622                 d << "_" << dm.territory;
623                 if (!dm.rating.empty ()) {
624                         d << "-" << dm.rating;
625                 }
626         }
627
628         /* XXX */
629         switch (2) {
630         case 1:
631                 d << "_10";
632                 break;
633         case 2:
634                 d << "_20";
635                 break;
636         case 6:
637                 d << "_51";
638                 break;
639         case 8:
640                 d << "_71";
641                 break;
642         }
643
644         d << "_2K";
645
646         if (!dm.studio.empty ()) {
647                 d << "_" << dm.studio;
648         }
649
650         if (if_created_now) {
651                 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
652         } else {
653                 d << "_" << boost::gregorian::to_iso_string (_dci_date);
654         }
655
656         if (!dm.facility.empty ()) {
657                 d << "_" << dm.facility;
658         }
659
660         if (!dm.package_type.empty ()) {
661                 d << "_" << dm.package_type;
662         }
663
664         return d.str ();
665 }
666
667 /** @return name to give the DCP */
668 string
669 Film::dcp_name (bool if_created_now) const
670 {
671         if (use_dci_name()) {
672                 return dci_name (if_created_now);
673         }
674
675         return name();
676 }
677
678
679 void
680 Film::set_directory (string d)
681 {
682         boost::mutex::scoped_lock lm (_state_mutex);
683         _directory = d;
684         _dirty = true;
685 }
686
687 void
688 Film::set_name (string n)
689 {
690         {
691                 boost::mutex::scoped_lock lm (_state_mutex);
692                 _name = n;
693         }
694         signal_changed (NAME);
695 }
696
697 void
698 Film::set_use_dci_name (bool u)
699 {
700         {
701                 boost::mutex::scoped_lock lm (_state_mutex);
702                 _use_dci_name = u;
703         }
704         signal_changed (USE_DCI_NAME);
705 }
706
707 void
708 Film::set_trust_content_headers (bool t)
709 {
710         {
711                 boost::mutex::scoped_lock lm (_state_mutex);
712                 _trust_content_headers = t;
713         }
714         
715         signal_changed (TRUST_CONTENT_HEADERS);
716
717         if (!_trust_content_headers && !content().empty()) {
718                 /* We just said that we don't trust the content's header */
719                 /* XXX */
720 //              examine_content ();
721         }
722 }
723                
724 void
725 Film::set_dcp_content_type (DCPContentType const * t)
726 {
727         {
728                 boost::mutex::scoped_lock lm (_state_mutex);
729                 _dcp_content_type = t;
730         }
731         signal_changed (DCP_CONTENT_TYPE);
732 }
733
734 void
735 Film::set_format (Format const * f)
736 {
737         {
738                 boost::mutex::scoped_lock lm (_state_mutex);
739                 _format = f;
740         }
741         signal_changed (FORMAT);
742 }
743
744 void
745 Film::set_crop (Crop c)
746 {
747         {
748                 boost::mutex::scoped_lock lm (_state_mutex);
749                 _crop = c;
750         }
751         signal_changed (CROP);
752 }
753
754 void
755 Film::set_left_crop (int c)
756 {
757         {
758                 boost::mutex::scoped_lock lm (_state_mutex);
759                 
760                 if (_crop.left == c) {
761                         return;
762                 }
763                 
764                 _crop.left = c;
765         }
766         signal_changed (CROP);
767 }
768
769 void
770 Film::set_right_crop (int c)
771 {
772         {
773                 boost::mutex::scoped_lock lm (_state_mutex);
774                 if (_crop.right == c) {
775                         return;
776                 }
777                 
778                 _crop.right = c;
779         }
780         signal_changed (CROP);
781 }
782
783 void
784 Film::set_top_crop (int c)
785 {
786         {
787                 boost::mutex::scoped_lock lm (_state_mutex);
788                 if (_crop.top == c) {
789                         return;
790                 }
791                 
792                 _crop.top = c;
793         }
794         signal_changed (CROP);
795 }
796
797 void
798 Film::set_bottom_crop (int c)
799 {
800         {
801                 boost::mutex::scoped_lock lm (_state_mutex);
802                 if (_crop.bottom == c) {
803                         return;
804                 }
805                 
806                 _crop.bottom = c;
807         }
808         signal_changed (CROP);
809 }
810
811 void
812 Film::set_filters (vector<Filter const *> f)
813 {
814         {
815                 boost::mutex::scoped_lock lm (_state_mutex);
816                 _filters = f;
817         }
818         signal_changed (FILTERS);
819 }
820
821 void
822 Film::set_scaler (Scaler const * s)
823 {
824         {
825                 boost::mutex::scoped_lock lm (_state_mutex);
826                 _scaler = s;
827         }
828         signal_changed (SCALER);
829 }
830
831 void
832 Film::set_trim_start (int t)
833 {
834         {
835                 boost::mutex::scoped_lock lm (_state_mutex);
836                 _trim_start = t;
837         }
838         signal_changed (TRIM_START);
839 }
840
841 void
842 Film::set_trim_end (int t)
843 {
844         {
845                 boost::mutex::scoped_lock lm (_state_mutex);
846                 _trim_end = t;
847         }
848         signal_changed (TRIM_END);
849 }
850
851 void
852 Film::set_ab (bool a)
853 {
854         {
855                 boost::mutex::scoped_lock lm (_state_mutex);
856                 _ab = a;
857         }
858         signal_changed (AB);
859 }
860
861 void
862 Film::set_audio_gain (float g)
863 {
864         {
865                 boost::mutex::scoped_lock lm (_state_mutex);
866                 _audio_gain = g;
867         }
868         signal_changed (AUDIO_GAIN);
869 }
870
871 void
872 Film::set_audio_delay (int d)
873 {
874         {
875                 boost::mutex::scoped_lock lm (_state_mutex);
876                 _audio_delay = d;
877         }
878         signal_changed (AUDIO_DELAY);
879 }
880
881 void
882 Film::set_with_subtitles (bool w)
883 {
884         {
885                 boost::mutex::scoped_lock lm (_state_mutex);
886                 _with_subtitles = w;
887         }
888         signal_changed (WITH_SUBTITLES);
889 }
890
891 void
892 Film::set_subtitle_offset (int o)
893 {
894         {
895                 boost::mutex::scoped_lock lm (_state_mutex);
896                 _subtitle_offset = o;
897         }
898         signal_changed (SUBTITLE_OFFSET);
899 }
900
901 void
902 Film::set_subtitle_scale (float s)
903 {
904         {
905                 boost::mutex::scoped_lock lm (_state_mutex);
906                 _subtitle_scale = s;
907         }
908         signal_changed (SUBTITLE_SCALE);
909 }
910
911 void
912 Film::set_colour_lut (int i)
913 {
914         {
915                 boost::mutex::scoped_lock lm (_state_mutex);
916                 _colour_lut = i;
917         }
918         signal_changed (COLOUR_LUT);
919 }
920
921 void
922 Film::set_j2k_bandwidth (int b)
923 {
924         {
925                 boost::mutex::scoped_lock lm (_state_mutex);
926                 _j2k_bandwidth = b;
927         }
928         signal_changed (J2K_BANDWIDTH);
929 }
930
931 void
932 Film::set_dci_metadata (DCIMetadata m)
933 {
934         {
935                 boost::mutex::scoped_lock lm (_state_mutex);
936                 _dci_metadata = m;
937         }
938         signal_changed (DCI_METADATA);
939 }
940
941
942 void
943 Film::set_dcp_frame_rate (int f)
944 {
945         {
946                 boost::mutex::scoped_lock lm (_state_mutex);
947                 _dcp_frame_rate = f;
948         }
949         signal_changed (DCP_FRAME_RATE);
950 }
951
952 void
953 Film::signal_changed (Property p)
954 {
955         {
956                 boost::mutex::scoped_lock lm (_state_mutex);
957                 _dirty = true;
958         }
959
960         if (ui_signaller) {
961                 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
962         }
963 }
964
965 void
966 Film::set_dci_date_today ()
967 {
968         _dci_date = boost::gregorian::day_clock::local_day ();
969 }
970
971 string
972 Film::info_path (int f) const
973 {
974         boost::filesystem::path p;
975         p /= info_dir ();
976
977         stringstream s;
978         s.width (8);
979         s << setfill('0') << f << ".md5";
980
981         p /= s.str();
982
983         /* info_dir() will already have added any initial bit of the path,
984            so don't call file() on this.
985         */
986         return p.string ();
987 }
988
989 string
990 Film::j2c_path (int f, bool t) const
991 {
992         boost::filesystem::path p;
993         p /= "j2c";
994         p /= video_state_identifier ();
995
996         stringstream s;
997         s.width (8);
998         s << setfill('0') << f << ".j2c";
999
1000         if (t) {
1001                 s << ".tmp";
1002         }
1003
1004         p /= s.str();
1005         return file (p.string ());
1006 }
1007
1008 /** Make an educated guess as to whether we have a complete DCP
1009  *  or not.
1010  *  @return true if we do.
1011  */
1012
1013 bool
1014 Film::have_dcp () const
1015 {
1016         try {
1017                 libdcp::DCP dcp (dir (dcp_name()));
1018                 dcp.read ();
1019         } catch (...) {
1020                 return false;
1021         }
1022
1023         return true;
1024 }
1025
1026 shared_ptr<Playlist>
1027 Film::playlist () const
1028 {
1029         boost::mutex::scoped_lock lm (_state_mutex);
1030         return shared_ptr<Playlist> (new Playlist (shared_from_this (), _content));
1031 }
1032
1033 void
1034 Film::add_content (shared_ptr<Content> c)
1035 {
1036         {
1037                 boost::mutex::scoped_lock lm (_state_mutex);
1038                 _content.push_back (c);
1039         }
1040
1041         signal_changed (CONTENT);
1042
1043         examine_content (c);
1044 }