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