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