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