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