Untested trim of start and end.
[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 "film.h"
33 #include "format.h"
34 #include "imagemagick_encoder.h"
35 #include "job.h"
36 #include "filter.h"
37 #include "transcoder.h"
38 #include "util.h"
39 #include "job_manager.h"
40 #include "ab_transcode_job.h"
41 #include "transcode_job.h"
42 #include "scp_dcp_job.h"
43 #include "copy_from_dvd_job.h"
44 #include "make_dcp_job.h"
45 #include "log.h"
46 #include "options.h"
47 #include "exceptions.h"
48 #include "examine_content_job.h"
49 #include "scaler.h"
50 #include "decoder_factory.h"
51 #include "config.h"
52 #include "check_hashes_job.h"
53 #include "version.h"
54 #include "ui_signaller.h"
55
56 using std::string;
57 using std::stringstream;
58 using std::multimap;
59 using std::pair;
60 using std::map;
61 using std::vector;
62 using std::ifstream;
63 using std::ofstream;
64 using std::setfill;
65 using std::min;
66 using boost::shared_ptr;
67 using boost::lexical_cast;
68 using boost::to_upper_copy;
69 using boost::ends_with;
70 using boost::starts_with;
71
72 /** Construct a Film object in a given directory, reading any metadata
73  *  file that exists in that directory.  An exception will be thrown if
74  *  must_exist is true and the specified directory does not exist.
75  *
76  *  @param d Film directory.
77  *  @param must_exist true to throw an exception if does not exist.
78  */
79
80 Film::Film (string d, bool must_exist)
81         : _use_dci_name (false)
82         , _dcp_content_type (0)
83         , _format (0)
84         , _scaler (Scaler::from_id ("bicubic"))
85         , _dcp_trim_start (0)
86         , _dcp_trim_end (0)
87         , _dcp_ab (false)
88         , _audio_stream (-1)
89         , _audio_gain (0)
90         , _audio_delay (0)
91         , _still_duration (10)
92         , _subtitle_stream (-1)
93         , _with_subtitles (false)
94         , _subtitle_offset (0)
95         , _subtitle_scale (1)
96         , _audio_sample_rate (0)
97         , _has_subtitles (false)
98         , _frames_per_second (0)
99         , _dirty (false)
100 {
101         /* Make state.directory a complete path without ..s (where possible)
102            (Code swiped from Adam Bowen on stackoverflow)
103         */
104         
105         boost::filesystem::path p (boost::filesystem::system_complete (d));
106         boost::filesystem::path result;
107         for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
108                 if (*i == "..") {
109                         if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
110                                 result /= *i;
111                         } else {
112                                 result = result.parent_path ();
113                         }
114                 } else if (*i != ".") {
115                         result /= *i;
116                 }
117         }
118
119         set_directory (result.string ());
120         
121         if (!boost::filesystem::exists (directory())) {
122                 if (must_exist) {
123                         throw OpenFileError (directory());
124                 } else {
125                         boost::filesystem::create_directory (directory());
126                 }
127         }
128
129         read_metadata ();
130
131         _log = new FileLog (file ("log"));
132         set_dci_date_today ();
133 }
134
135 Film::Film (Film const & o)
136         : _log (0)
137         , _directory         (o._directory)
138         , _name              (o._name)
139         , _use_dci_name      (o._use_dci_name)
140         , _content           (o._content)
141         , _dcp_content_type  (o._dcp_content_type)
142         , _format            (o._format)
143         , _crop              (o._crop)
144         , _filters           (o._filters)
145         , _scaler            (o._scaler)
146         , _dcp_trim_start    (o._dcp_trim_start)
147         , _dcp_trim_end      (o._dcp_trim_end)
148         , _dcp_ab            (o._dcp_ab)
149         , _audio_stream      (o._audio_stream)
150         , _audio_gain        (o._audio_gain)
151         , _audio_delay       (o._audio_delay)
152         , _still_duration    (o._still_duration)
153         , _subtitle_stream   (o._subtitle_stream)
154         , _with_subtitles    (o._with_subtitles)
155         , _subtitle_offset   (o._subtitle_offset)
156         , _subtitle_scale    (o._subtitle_scale)
157         , _audio_language    (o._audio_language)
158         , _subtitle_language (o._subtitle_language)
159         , _territory         (o._territory)
160         , _rating            (o._rating)
161         , _studio            (o._studio)
162         , _facility          (o._facility)
163         , _package_type      (o._package_type)
164         , _thumbs            (o._thumbs)
165         , _size              (o._size)
166         , _length            (o._length)
167         , _audio_sample_rate (o._audio_sample_rate)
168         , _content_digest    (o._content_digest)
169         , _has_subtitles     (o._has_subtitles)
170         , _audio_streams     (o._audio_streams)
171         , _subtitle_streams  (o._subtitle_streams)
172         , _frames_per_second (o._frames_per_second)
173         , _dirty             (o._dirty)
174 {
175
176 }
177
178 Film::~Film ()
179 {
180         delete _log;
181 }
182           
183 /** @return The path to the directory to write JPEG2000 files to */
184 string
185 Film::j2k_dir () const
186 {
187         assert (format());
188
189         boost::filesystem::path p;
190
191         /* Start with j2c */
192         p /= "j2c";
193
194         pair<string, string> f = Filter::ffmpeg_strings (filters());
195
196         /* Write stuff to specify the filter / post-processing settings that are in use,
197            so that we don't get confused about J2K files generated using different
198            settings.
199         */
200         stringstream s;
201         s << format()->id()
202           << "_" << content_digest()
203           << "_" << crop().left << "_" << crop().right << "_" << crop().top << "_" << crop().bottom
204           << "_" << f.first << "_" << f.second
205           << "_" << scaler()->id();
206
207         p /= s.str ();
208
209         /* Similarly for the A/B case */
210         if (dcp_ab()) {
211                 stringstream s;
212                 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
213                 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
214                 p /= s.str ();
215         }
216         
217         return dir (p.string());
218 }
219
220 /** Add suitable Jobs to the JobManager to create a DCP for this Film.
221  *  @param true to transcode, false to use the WAV and J2K files that are already there.
222  */
223 void
224 Film::make_dcp (bool transcode)
225 {
226         set_dci_date_today ();
227         
228         if (dcp_name().find ("/") != string::npos) {
229                 throw BadSettingError ("name", "cannot contain slashes");
230         }
231         
232         log()->log (String::compose ("DVD-o-matic %1 git %2 using %3", dvdomatic_version, dvdomatic_git_commit, dependency_version_summary()));
233
234         {
235                 char buffer[128];
236                 gethostname (buffer, sizeof (buffer));
237                 log()->log (String::compose ("Starting to make DCP on %1", buffer));
238         }
239                 
240         if (format() == 0) {
241                 throw MissingSettingError ("format");
242         }
243
244         if (content().empty ()) {
245                 throw MissingSettingError ("content");
246         }
247
248         if (dcp_content_type() == 0) {
249                 throw MissingSettingError ("content type");
250         }
251
252         if (name().empty()) {
253                 throw MissingSettingError ("name");
254         }
255
256         shared_ptr<Options> o (new Options (j2k_dir(), ".j2c", dir ("wavs")));
257         o->out_size = format()->dcp_size ();
258         o->padding = format()->dcp_padding (shared_from_this ());
259         o->ratio = format()->ratio_as_float (shared_from_this ());
260         o->decode_subtitles = with_subtitles ();
261
262         shared_ptr<Job> r;
263
264         if (transcode) {
265                 if (dcp_ab()) {
266                         r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this(), o, shared_ptr<Job> ())));
267                 } else {
268                         r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this(), o, shared_ptr<Job> ())));
269                 }
270         }
271
272         r = JobManager::instance()->add (shared_ptr<Job> (new CheckHashesJob (shared_from_this(), o, r)));
273         JobManager::instance()->add (shared_ptr<Job> (new MakeDCPJob (shared_from_this(), o, r)));
274 }
275
276 /** Start a job to examine our content file */
277 void
278 Film::examine_content ()
279 {
280         if (_examine_content_job) {
281                 return;
282         }
283
284         set_thumbs (vector<int> ());
285         boost::filesystem::remove_all (dir ("thumbs"));
286
287         /* This call will recreate the directory */
288         dir ("thumbs");
289         
290         _examine_content_job.reset (new ExamineContentJob (shared_from_this(), shared_ptr<Job> ()));
291         _examine_content_job->Finished.connect (bind (&Film::examine_content_finished, this));
292         JobManager::instance()->add (_examine_content_job);
293 }
294
295 void
296 Film::examine_content_finished ()
297 {
298         _examine_content_job.reset ();
299 }
300
301 /** @return full paths to any audio files that this Film has */
302 vector<string>
303 Film::audio_files () const
304 {
305         vector<string> f;
306         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (dir("wavs")); i != boost::filesystem::directory_iterator(); ++i) {
307                 f.push_back (i->path().string ());
308         }
309
310         return f;
311 }
312
313 /** Start a job to send our DCP to the configured TMS */
314 void
315 Film::send_dcp_to_tms ()
316 {
317         shared_ptr<Job> j (new SCPDCPJob (shared_from_this(), shared_ptr<Job> ()));
318         JobManager::instance()->add (j);
319 }
320
321 void
322 Film::copy_from_dvd ()
323 {
324         shared_ptr<Job> j (new CopyFromDVDJob (shared_from_this(), shared_ptr<Job> ()));
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 (format() == 0) {
335                 return 0;
336         }
337
338         int N = 0;
339         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (j2k_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
340                 ++N;
341                 boost::this_thread::interruption_point ();
342         }
343
344         return N;
345 }
346
347 /** Return the filename of a subtitle image if one exists for a given thumb index.
348  *  @param Thumbnail index.
349  *  @return Position of the image within the source frame, and the image filename, if one exists.
350  *  Otherwise the filename will be empty.
351  */
352 pair<Position, string>
353 Film::thumb_subtitle (int n) const
354 {
355         string sub_file = thumb_base(n) + ".sub";
356         if (!boost::filesystem::exists (sub_file)) {
357                 return pair<Position, string> ();
358         }
359
360         pair<Position, string> sub;
361         
362         ifstream f (sub_file.c_str ());
363         multimap<string, string> kv = read_key_value (f);
364         for (map<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
365                 if (i->first == "x") {
366                         sub.first.x = lexical_cast<int> (i->second);
367                 } else if (i->first == "y") {
368                         sub.first.y = lexical_cast<int> (i->second);
369                         sub.second = String::compose ("%1.sub.png", thumb_base(n));
370                 }
371         }
372         
373         return sub;
374 }
375
376 /** Write state to our `metadata' file */
377 void
378 Film::write_metadata () const
379 {
380         boost::mutex::scoped_lock lm (_state_mutex);
381
382         boost::filesystem::create_directories (directory());
383
384         string const m = file ("metadata");
385         ofstream f (m.c_str ());
386         if (!f.good ()) {
387                 throw CreateFileError (m);
388         }
389
390         /* User stuff */
391         f << "name " << _name << "\n";
392         f << "use_dci_name " << _use_dci_name << "\n";
393         f << "content " << _content << "\n";
394         if (_dcp_content_type) {
395                 f << "dcp_content_type " << _dcp_content_type->pretty_name () << "\n";
396         }
397         if (_format) {
398                 f << "format " << _format->as_metadata () << "\n";
399         }
400         f << "left_crop " << _crop.left << "\n";
401         f << "right_crop " << _crop.right << "\n";
402         f << "top_crop " << _crop.top << "\n";
403         f << "bottom_crop " << _crop.bottom << "\n";
404         for (vector<Filter const *>::const_iterator i = _filters.begin(); i != _filters.end(); ++i) {
405                 f << "filter " << (*i)->id () << "\n";
406         }
407         f << "scaler " << _scaler->id () << "\n";
408         f << "dcp_trim_start " << _dcp_trim_start << "\n";
409         f << "dcp_trim_end " << _dcp_trim_end << "\n";
410         f << "dcp_ab " << (_dcp_ab ? "1" : "0") << "\n";
411         f << "selected_audio_stream " << _audio_stream << "\n";
412         f << "audio_gain " << _audio_gain << "\n";
413         f << "audio_delay " << _audio_delay << "\n";
414         f << "still_duration " << _still_duration << "\n";
415         f << "selected_subtitle_stream " << _subtitle_stream << "\n";
416         f << "with_subtitles " << _with_subtitles << "\n";
417         f << "subtitle_offset " << _subtitle_offset << "\n";
418         f << "subtitle_scale " << _subtitle_scale << "\n";
419         f << "audio_language " << _audio_language << "\n";
420         f << "subtitle_language " << _subtitle_language << "\n";
421         f << "territory " << _territory << "\n";
422         f << "rating " << _rating << "\n";
423         f << "studio " << _studio << "\n";
424         f << "facility " << _facility << "\n";
425         f << "package_type " << _package_type << "\n";
426
427         /* Cached stuff; this is information about our content; we could
428            look it up each time, but that's slow.
429         */
430         for (vector<int>::const_iterator i = _thumbs.begin(); i != _thumbs.end(); ++i) {
431                 f << "thumb " << *i << "\n";
432         }
433         f << "width " << _size.width << "\n";
434         f << "height " << _size.height << "\n";
435         f << "length " << _length.get_value_or(0) << "\n";
436         f << "audio_sample_rate " << _audio_sample_rate << "\n";
437         f << "content_digest " << _content_digest << "\n";
438         f << "has_subtitles " << _has_subtitles << "\n";
439
440         for (vector<AudioStream>::const_iterator i = _audio_streams.begin(); i != _audio_streams.end(); ++i) {
441                 f << "audio_stream " << i->to_string () << "\n";
442         }
443
444         for (vector<SubtitleStream>::const_iterator i = _subtitle_streams.begin(); i != _subtitle_streams.end(); ++i) {
445                 f << "subtitle_stream " << i->to_string () << "\n";
446         }
447
448         f << "frames_per_second " << _frames_per_second << "\n";
449         
450         _dirty = false;
451 }
452
453 /** Read state from our metadata file */
454 void
455 Film::read_metadata ()
456 {
457         boost::mutex::scoped_lock lm (_state_mutex);
458         
459         ifstream f (file ("metadata").c_str());
460         multimap<string, string> kv = read_key_value (f);
461         for (multimap<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
462                 string const k = i->first;
463                 string const v = i->second;
464
465                 /* User-specified stuff */
466                 if (k == "name") {
467                         _name = v;
468                 } else if (k == "use_dci_name") {
469                         _use_dci_name = (v == "1");
470                 } else if (k == "content") {
471                         _content = v;
472                 } else if (k == "dcp_content_type") {
473                         _dcp_content_type = DCPContentType::from_pretty_name (v);
474                 } else if (k == "format") {
475                         _format = Format::from_metadata (v);
476                 } else if (k == "left_crop") {
477                         _crop.left = atoi (v.c_str ());
478                 } else if (k == "right_crop") {
479                         _crop.right = atoi (v.c_str ());
480                 } else if (k == "top_crop") {
481                         _crop.top = atoi (v.c_str ());
482                 } else if (k == "bottom_crop") {
483                         _crop.bottom = atoi (v.c_str ());
484                 } else if (k == "filter") {
485                         _filters.push_back (Filter::from_id (v));
486                 } else if (k == "scaler") {
487                         _scaler = Scaler::from_id (v);
488                 } else if (k == "dcp_trim_start") {
489                         _dcp_trim_start = atoi (v.c_str ());
490                 } else if (k == "dcp_trim_end") {
491                         _dcp_trim_end = atoi (v.c_str ());
492                 } else if (k == "dcp_ab") {
493                         _dcp_ab = (v == "1");
494                 } else if (k == "selected_audio_stream") {
495                         _audio_stream = atoi (v.c_str ());
496                 } else if (k == "audio_gain") {
497                         _audio_gain = atof (v.c_str ());
498                 } else if (k == "audio_delay") {
499                         _audio_delay = atoi (v.c_str ());
500                 } else if (k == "still_duration") {
501                         _still_duration = atoi (v.c_str ());
502                 } else if (k == "selected_subtitle_stream") {
503                         _subtitle_stream = atoi (v.c_str ());
504                 } else if (k == "with_subtitles") {
505                         _with_subtitles = (v == "1");
506                 } else if (k == "subtitle_offset") {
507                         _subtitle_offset = atoi (v.c_str ());
508                 } else if (k == "subtitle_scale") {
509                         _subtitle_scale = atof (v.c_str ());
510                 } else if (k == "audio_language") {
511                         _audio_language = v;
512                 } else if (k == "subtitle_language") {
513                         _subtitle_language = v;
514                 } else if (k == "territory") {
515                         _territory = v;
516                 } else if (k == "rating") {
517                         _rating = v;
518                 } else if (k == "studio") {
519                         _studio = v;
520                 } else if (k == "facility") {
521                         _facility = v;
522                 } else if (k == "package_type") {
523                         _package_type = v;
524                 }
525                 
526                 /* Cached stuff */
527                 if (k == "thumb") {
528                         int const n = atoi (v.c_str ());
529                         /* Only add it to the list if it still exists */
530                         if (boost::filesystem::exists (thumb_file_for_frame (n))) {
531                                 _thumbs.push_back (n);
532                         }
533                 } else if (k == "width") {
534                         _size.width = atoi (v.c_str ());
535                 } else if (k == "height") {
536                         _size.height = atoi (v.c_str ());
537                 } else if (k == "length") {
538                         int const vv = atoi (v.c_str ());
539                         if (vv) {
540                                 _length = vv;
541                         }
542                 } else if (k == "audio_sample_rate") {
543                         _audio_sample_rate = atoi (v.c_str ());
544                 } else if (k == "content_digest") {
545                         _content_digest = v;
546                 } else if (k == "has_subtitles") {
547                         _has_subtitles = (v == "1");
548                 } else if (k == "audio_stream") {
549                         _audio_streams.push_back (AudioStream (v));
550                 } else if (k == "subtitle_stream") {
551                         _subtitle_streams.push_back (SubtitleStream (v));
552                 } else if (k == "frames_per_second") {
553                         _frames_per_second = atof (v.c_str ());
554                 }
555         }
556                 
557         _dirty = false;
558 }
559
560 /** @param n A thumb index.
561  *  @return The path to the thumb's image file.
562  */
563 string
564 Film::thumb_file (int n) const
565 {
566         return thumb_file_for_frame (thumb_frame (n));
567 }
568
569 /** @param n A frame index within the Film.
570  *  @return The path to the thumb's image file for this frame;
571  *  we assume that it exists.
572  */
573 string
574 Film::thumb_file_for_frame (int n) const
575 {
576         return thumb_base_for_frame(n) + ".png";
577 }
578
579 /** Must not be called with the _state_mutex locked */
580 string
581 Film::thumb_base (int n) const
582 {
583         return thumb_base_for_frame (thumb_frame (n));
584 }
585
586 string
587 Film::thumb_base_for_frame (int n) const
588 {
589         stringstream s;
590         s.width (8);
591         s << setfill('0') << n;
592         
593         boost::filesystem::path p;
594         p /= dir ("thumbs");
595         p /= s.str ();
596                 
597         return p.string ();
598 }
599
600 /** @param n A thumb index.
601  *  @return The frame within the Film that it is for.
602  *
603  *  Must not be called with the _state_mutex locked.
604  */
605 int
606 Film::thumb_frame (int n) const
607 {
608         boost::mutex::scoped_lock lm (_state_mutex);
609         assert (n < int (_thumbs.size ()));
610         return _thumbs[n];
611 }
612
613 Size
614 Film::cropped_size (Size s) const
615 {
616         boost::mutex::scoped_lock lm (_state_mutex);
617         s.width -= _crop.left + _crop.right;
618         s.height -= _crop.top + _crop.bottom;
619         return s;
620 }
621
622 /** Given a directory name, return its full path within the Film's directory.
623  *  The directory (and its parents) will be created if they do not exist.
624  */
625 string
626 Film::dir (string d) const
627 {
628         boost::mutex::scoped_lock lm (_directory_mutex);
629         boost::filesystem::path p;
630         p /= _directory;
631         p /= d;
632         boost::filesystem::create_directories (p);
633         return p.string ();
634 }
635
636 /** Given a file or directory name, return its full path within the Film's directory.
637  *  _directory_mutex must not be locked on entry.
638  */
639 string
640 Film::file (string f) const
641 {
642         boost::mutex::scoped_lock lm (_directory_mutex);
643         boost::filesystem::path p;
644         p /= _directory;
645         p /= f;
646         return p.string ();
647 }
648
649 /** @return full path of the content (actual video) file
650  *  of the Film.
651  */
652 string
653 Film::content_path () const
654 {
655         boost::mutex::scoped_lock lm (_state_mutex);
656         if (boost::filesystem::path(_content).has_root_directory ()) {
657                 return _content;
658         }
659
660         return file (_content);
661 }
662
663 ContentType
664 Film::content_type () const
665 {
666 #if BOOST_FILESYSTEM_VERSION == 3
667         string ext = boost::filesystem::path(_content).extension().string();
668 #else
669         string ext = boost::filesystem::path(_content).extension();
670 #endif
671
672         transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
673         
674         if (ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
675                 return STILL;
676         }
677
678         return VIDEO;
679 }
680
681 /** @return The sampling rate that we will resample the audio to */
682 int
683 Film::target_audio_sample_rate () const
684 {
685         /* Resample to a DCI-approved sample rate */
686         double t = dcp_audio_sample_rate (audio_sample_rate());
687
688         /* Compensate for the fact that video will be rounded to the
689            nearest integer number of frames per second.
690         */
691         if (rint (frames_per_second()) != frames_per_second()) {
692                 t *= _frames_per_second / rint (frames_per_second());
693         }
694
695         return rint (t);
696 }
697
698 boost::optional<int>
699 Film::dcp_length () const
700 {
701         if (!length()) {
702                 return boost::optional<int> ();
703         }
704
705         return length().get() - dcp_trim_start() - dcp_trim_end();
706 }
707
708 /** @return a DCI-compliant name for a DCP of this film */
709 string
710 Film::dci_name () const
711 {
712         boost::mutex::scoped_lock lm (_state_mutex);
713         
714         stringstream d;
715
716         string fixed_name = to_upper_copy (_name);
717         for (size_t i = 0; i < fixed_name.length(); ++i) {
718                 if (fixed_name[i] == ' ') {
719                         fixed_name[i] = '-';
720                 }
721         }
722
723         /* Spec is that the name part should be maximum 14 characters, as I understand it */
724         if (fixed_name.length() > 14) {
725                 fixed_name = fixed_name.substr (0, 14);
726         }
727
728         d << fixed_name << "_";
729
730         if (_dcp_content_type) {
731                 d << _dcp_content_type->dci_name() << "_";
732         }
733
734         if (_format) {
735                 d << _format->dci_name() << "_";
736         }
737
738         if (!_audio_language.empty ()) {
739                 d << _audio_language;
740                 if (!_subtitle_language.empty() && _with_subtitles) {
741                         d << "-" << _subtitle_language;
742                 } else {
743                         d << "-XX";
744                 }
745                         
746                 d << "_";
747         }
748
749         if (!_territory.empty ()) {
750                 d << _territory;
751                 if (!_rating.empty ()) {
752                         d << "-" << _rating;
753                 }
754                 d << "_";
755         }
756
757         switch (_audio_streams[_audio_stream].channels()) {
758         case 1:
759                 d << "10_";
760                 break;
761         case 2:
762                 d << "20_";
763                 break;
764         case 6:
765                 d << "51_";
766                 break;
767         case 8:
768                 d << "71_";
769                 break;
770         }
771
772         d << "2K_";
773
774         if (!_studio.empty ()) {
775                 d << _studio << "_";
776         }
777
778         d << boost::gregorian::to_iso_string (_dci_date) << "_";
779
780         if (!_facility.empty ()) {
781                 d << _facility << "_";
782         }
783
784         if (!_package_type.empty ()) {
785                 d << _package_type;
786         }
787
788         return d.str ();
789 }
790
791 /** @return name to give the DCP */
792 string
793 Film::dcp_name () const
794 {
795         if (use_dci_name()) {
796                 return dci_name ();
797         }
798
799         return name();
800 }
801
802
803 void
804 Film::set_directory (string d)
805 {
806         boost::mutex::scoped_lock lm (_state_mutex);
807         _directory = d;
808         _dirty = true;
809 }
810
811 void
812 Film::set_name (string n)
813 {
814         {
815                 boost::mutex::scoped_lock lm (_state_mutex);
816                 _name = n;
817         }
818         signal_changed (NAME);
819 }
820
821 void
822 Film::set_use_dci_name (bool u)
823 {
824         {
825                 boost::mutex::scoped_lock lm (_state_mutex);
826                 _use_dci_name = u;
827         }
828         signal_changed (USE_DCI_NAME);
829 }
830
831 void
832 Film::set_content (string c)
833 {
834         string check = directory ();
835
836 #if BOOST_FILESYSTEM_VERSION == 3
837         boost::filesystem::path slash ("/");
838         string platform_slash = slash.make_preferred().string ();
839 #else
840 #ifdef DVDOMATIC_WINDOWS
841         string platform_slash = "\\";
842 #else
843         string platform_slash = "/";
844 #endif
845 #endif  
846
847         if (!ends_with (check, platform_slash)) {
848                 check += platform_slash;
849         }
850         
851         if (boost::filesystem::path(c).has_root_directory () && starts_with (c, check)) {
852                 c = c.substr (_directory.length() + 1);
853         }
854
855         string old_content;
856         
857         {
858                 boost::mutex::scoped_lock lm (_state_mutex);
859                 if (c == _content) {
860                         return;
861                 }
862
863                 old_content = _content;
864                 _content = c;
865         }
866
867         /* Create a temporary decoder so that we can get information
868            about the content.
869         */
870
871         try {
872                 shared_ptr<Options> o (new Options ("", "", ""));
873                 o->out_size = Size (1024, 1024);
874                 
875                 shared_ptr<Decoder> d = decoder_factory (shared_from_this(), o, 0, 0);
876                 
877                 set_size (d->native_size ());
878                 set_frames_per_second (d->frames_per_second ());
879                 set_audio_sample_rate (d->audio_sample_rate ());
880                 set_has_subtitles (d->has_subtitles ());
881                 set_audio_streams (d->audio_streams ());
882                 set_subtitle_streams (d->subtitle_streams ());
883                 set_audio_stream (audio_streams().empty() ? -1 : 0);
884                 set_subtitle_stream (subtitle_streams().empty() ? -1 : 0);
885                 
886                 {
887                         boost::mutex::scoped_lock lm (_state_mutex);
888                         _content = c;
889                 }
890                 
891                 signal_changed (CONTENT);
892                 
893                 set_content_digest (md5_digest (content_path ()));
894                 
895                 examine_content ();
896
897         } catch (...) {
898
899                 boost::mutex::scoped_lock lm (_state_mutex);
900                 _content = old_content;
901                 throw;
902
903         }
904 }
905                
906 void
907 Film::set_dcp_content_type (DCPContentType const * t)
908 {
909         {
910                 boost::mutex::scoped_lock lm (_state_mutex);
911                 _dcp_content_type = t;
912         }
913         signal_changed (DCP_CONTENT_TYPE);
914 }
915
916 void
917 Film::set_format (Format const * f)
918 {
919         {
920                 boost::mutex::scoped_lock lm (_state_mutex);
921                 _format = f;
922         }
923         signal_changed (FORMAT);
924 }
925
926 void
927 Film::set_crop (Crop c)
928 {
929         {
930                 boost::mutex::scoped_lock lm (_state_mutex);
931                 _crop = c;
932         }
933         signal_changed (CROP);
934 }
935
936 void
937 Film::set_left_crop (int c)
938 {
939         {
940                 boost::mutex::scoped_lock lm (_state_mutex);
941                 
942                 if (_crop.left == c) {
943                         return;
944                 }
945                 
946                 _crop.left = c;
947         }
948         signal_changed (CROP);
949 }
950
951 void
952 Film::set_right_crop (int c)
953 {
954         {
955                 boost::mutex::scoped_lock lm (_state_mutex);
956                 if (_crop.right == c) {
957                         return;
958                 }
959                 
960                 _crop.right = c;
961         }
962         signal_changed (CROP);
963 }
964
965 void
966 Film::set_top_crop (int c)
967 {
968         {
969                 boost::mutex::scoped_lock lm (_state_mutex);
970                 if (_crop.top == c) {
971                         return;
972                 }
973                 
974                 _crop.top = c;
975         }
976         signal_changed (CROP);
977 }
978
979 void
980 Film::set_bottom_crop (int c)
981 {
982         {
983                 boost::mutex::scoped_lock lm (_state_mutex);
984                 if (_crop.bottom == c) {
985                         return;
986                 }
987                 
988                 _crop.bottom = c;
989         }
990         signal_changed (CROP);
991 }
992
993 void
994 Film::set_filters (vector<Filter const *> f)
995 {
996         {
997                 boost::mutex::scoped_lock lm (_state_mutex);
998                 _filters = f;
999         }
1000         signal_changed (FILTERS);
1001 }
1002
1003 void
1004 Film::set_scaler (Scaler const * s)
1005 {
1006         {
1007                 boost::mutex::scoped_lock lm (_state_mutex);
1008                 _scaler = s;
1009         }
1010         signal_changed (SCALER);
1011 }
1012
1013 void
1014 Film::set_dcp_trim_start (int t)
1015 {
1016         {
1017                 boost::mutex::scoped_lock lm (_state_mutex);
1018                 _dcp_trim_start = t;
1019         }
1020         signal_changed (DCP_TRIM_START);
1021 }
1022
1023 void
1024 Film::set_dcp_trim_end (int t)
1025 {
1026         {
1027                 boost::mutex::scoped_lock lm (_state_mutex);
1028                 _dcp_trim_end = t;
1029         }
1030         signal_changed (DCP_TRIM_END);
1031 }
1032
1033 void
1034 Film::set_dcp_ab (bool a)
1035 {
1036         {
1037                 boost::mutex::scoped_lock lm (_state_mutex);
1038                 _dcp_ab = a;
1039         }
1040         signal_changed (DCP_AB);
1041 }
1042
1043 void
1044 Film::set_audio_stream (int s)
1045 {
1046         {
1047                 boost::mutex::scoped_lock lm (_state_mutex);
1048                 _audio_stream = s;
1049         }
1050         signal_changed (AUDIO_STREAM);
1051 }
1052
1053 void
1054 Film::set_audio_gain (float g)
1055 {
1056         {
1057                 boost::mutex::scoped_lock lm (_state_mutex);
1058                 _audio_gain = g;
1059         }
1060         signal_changed (AUDIO_GAIN);
1061 }
1062
1063 void
1064 Film::set_audio_delay (int d)
1065 {
1066         {
1067                 boost::mutex::scoped_lock lm (_state_mutex);
1068                 _audio_delay = d;
1069         }
1070         signal_changed (AUDIO_DELAY);
1071 }
1072
1073 void
1074 Film::set_still_duration (int d)
1075 {
1076         {
1077                 boost::mutex::scoped_lock lm (_state_mutex);
1078                 _still_duration = d;
1079         }
1080         signal_changed (STILL_DURATION);
1081 }
1082
1083 void
1084 Film::set_subtitle_stream (int s)
1085 {
1086         {
1087                 boost::mutex::scoped_lock lm (_state_mutex);
1088                 _subtitle_stream = s;
1089         }
1090         signal_changed (SUBTITLE_STREAM);
1091 }
1092
1093 void
1094 Film::set_with_subtitles (bool w)
1095 {
1096         {
1097                 boost::mutex::scoped_lock lm (_state_mutex);
1098                 _with_subtitles = w;
1099         }
1100         signal_changed (WITH_SUBTITLES);
1101 }
1102
1103 void
1104 Film::set_subtitle_offset (int o)
1105 {
1106         {
1107                 boost::mutex::scoped_lock lm (_state_mutex);
1108                 _subtitle_offset = o;
1109         }
1110         signal_changed (SUBTITLE_OFFSET);
1111 }
1112
1113 void
1114 Film::set_subtitle_scale (float s)
1115 {
1116         {
1117                 boost::mutex::scoped_lock lm (_state_mutex);
1118                 _subtitle_scale = s;
1119         }
1120         signal_changed (SUBTITLE_SCALE);
1121 }
1122
1123 void
1124 Film::set_audio_language (string l)
1125 {
1126         {
1127                 boost::mutex::scoped_lock lm (_state_mutex);
1128                 _audio_language = l;
1129         }
1130         signal_changed (DCI_METADATA);
1131 }
1132
1133 void
1134 Film::set_subtitle_language (string l)
1135 {
1136         {
1137                 boost::mutex::scoped_lock lm (_state_mutex);
1138                 _subtitle_language = l;
1139         }
1140         signal_changed (DCI_METADATA);
1141 }
1142
1143 void
1144 Film::set_territory (string t)
1145 {
1146         {
1147                 boost::mutex::scoped_lock lm (_state_mutex);
1148                 _territory = t;
1149         }
1150         signal_changed (DCI_METADATA);
1151 }
1152
1153 void
1154 Film::set_rating (string r)
1155 {
1156         {
1157                 boost::mutex::scoped_lock lm (_state_mutex);
1158                 _rating = r;
1159         }
1160         signal_changed (DCI_METADATA);
1161 }
1162
1163 void
1164 Film::set_studio (string s)
1165 {
1166         {
1167                 boost::mutex::scoped_lock lm (_state_mutex);
1168                 _studio = s;
1169         }
1170         signal_changed (DCI_METADATA);
1171 }
1172
1173 void
1174 Film::set_facility (string f)
1175 {
1176         {
1177                 boost::mutex::scoped_lock lm (_state_mutex);
1178                 _facility = f;
1179         }
1180         signal_changed (DCI_METADATA);
1181 }
1182
1183 void
1184 Film::set_package_type (string p)
1185 {
1186         {
1187                 boost::mutex::scoped_lock lm (_state_mutex);
1188                 _package_type = p;
1189         }
1190         signal_changed (DCI_METADATA);
1191 }
1192
1193 void
1194 Film::set_thumbs (vector<int> t)
1195 {
1196         {
1197                 boost::mutex::scoped_lock lm (_state_mutex);
1198                 _thumbs = t;
1199         }
1200         signal_changed (THUMBS);
1201 }
1202
1203 void
1204 Film::set_size (Size s)
1205 {
1206         {
1207                 boost::mutex::scoped_lock lm (_state_mutex);
1208                 _size = s;
1209         }
1210         signal_changed (SIZE);
1211 }
1212
1213 void
1214 Film::set_length (int l)
1215 {
1216         {
1217                 boost::mutex::scoped_lock lm (_state_mutex);
1218                 _length = l;
1219         }
1220         signal_changed (LENGTH);
1221 }
1222
1223 void
1224 Film::unset_length ()
1225 {
1226         {
1227                 boost::mutex::scoped_lock lm (_state_mutex);
1228                 _length = boost::none;
1229         }
1230         signal_changed (LENGTH);
1231 }       
1232
1233 void
1234 Film::set_audio_sample_rate (int r)
1235 {
1236         {
1237                 boost::mutex::scoped_lock lm (_state_mutex);
1238                 _audio_sample_rate = r;
1239         }
1240         signal_changed (AUDIO_SAMPLE_RATE);
1241 }
1242
1243 void
1244 Film::set_content_digest (string d)
1245 {
1246         {
1247                 boost::mutex::scoped_lock lm (_state_mutex);
1248                 _content_digest = d;
1249         }
1250         _dirty = true;
1251 }
1252
1253 void
1254 Film::set_has_subtitles (bool s)
1255 {
1256         {
1257                 boost::mutex::scoped_lock lm (_state_mutex);
1258                 _has_subtitles = s;
1259         }
1260         signal_changed (HAS_SUBTITLES);
1261 }
1262
1263 void
1264 Film::set_audio_streams (vector<AudioStream> s)
1265 {
1266         {
1267                 boost::mutex::scoped_lock lm (_state_mutex);
1268                 _audio_streams = s;
1269         }
1270         _dirty = true;
1271 }
1272
1273 void
1274 Film::set_subtitle_streams (vector<SubtitleStream> s)
1275 {
1276         {
1277                 boost::mutex::scoped_lock lm (_state_mutex);
1278                 _subtitle_streams = s;
1279         }
1280         _dirty = true;
1281 }
1282
1283 void
1284 Film::set_frames_per_second (float f)
1285 {
1286         {
1287                 boost::mutex::scoped_lock lm (_state_mutex);
1288                 _frames_per_second = f;
1289         }
1290         signal_changed (FRAMES_PER_SECOND);
1291 }
1292         
1293 void
1294 Film::signal_changed (Property p)
1295 {
1296         {
1297                 boost::mutex::scoped_lock lm (_state_mutex);
1298                 _dirty = true;
1299         }
1300
1301         if (ui_signaller) {
1302                 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
1303         }
1304 }
1305
1306 int
1307 Film::audio_channels () const
1308 {
1309         boost::mutex::scoped_lock lm (_state_mutex);
1310         if (_audio_stream == -1) {
1311                 return 0;
1312         }
1313         
1314         return _audio_streams[_audio_stream].channels ();
1315 }
1316
1317 void
1318 Film::set_dci_date_today ()
1319 {
1320         _dci_date = boost::gregorian::day_clock::local_day ();
1321 }