Merge master.
[dcpomatic.git] / src / lib / film.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdexcept>
21 #include <iostream>
22 #include <algorithm>
23 #include <fstream>
24 #include <cstdlib>
25 #include <sstream>
26 #include <iomanip>
27 #include <unistd.h>
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/date_time.hpp>
32 #include <libxml++/libxml++.h>
33 #include <libcxml/cxml.h>
34 #include "film.h"
35 #include "job.h"
36 #include "filter.h"
37 #include "util.h"
38 #include "job_manager.h"
39 #include "ab_transcode_job.h"
40 #include "transcode_job.h"
41 #include "scp_dcp_job.h"
42 #include "log.h"
43 #include "exceptions.h"
44 #include "examine_content_job.h"
45 #include "scaler.h"
46 #include "config.h"
47 #include "version.h"
48 #include "ui_signaller.h"
49 #include "analyse_audio_job.h"
50 #include "playlist.h"
51 #include "player.h"
52 #include "ffmpeg_content.h"
53 #include "imagemagick_content.h"
54 #include "sndfile_content.h"
55 #include "dcp_content_type.h"
56 #include "ratio.h"
57
58 #include "i18n.h"
59
60 using std::string;
61 using std::stringstream;
62 using std::multimap;
63 using std::pair;
64 using std::map;
65 using std::vector;
66 using std::ifstream;
67 using std::ofstream;
68 using std::setfill;
69 using std::min;
70 using std::make_pair;
71 using std::endl;
72 using std::cout;
73 using std::list;
74 using boost::shared_ptr;
75 using boost::lexical_cast;
76 using boost::dynamic_pointer_cast;
77 using boost::to_upper_copy;
78 using boost::ends_with;
79 using boost::starts_with;
80 using boost::optional;
81 using libdcp::Size;
82
83 int const Film::state_version = 4;
84
85 /** Construct a Film object in a given directory.
86  *
87  *  @param d Film directory.
88  */
89
90 Film::Film (string d)
91         : _playlist (new Playlist)
92         , _use_dci_name (true)
93         , _dcp_content_type (Config::instance()->default_dcp_content_type ())
94         , _container (Config::instance()->default_container ())
95         , _scaler (Scaler::from_id ("bicubic"))
96         , _ab (false)
97         , _with_subtitles (false)
98         , _subtitle_offset (0)
99         , _subtitle_scale (1)
100         , _colour_lut (0)
101         , _j2k_bandwidth (200000000)
102         , _dci_metadata (Config::instance()->default_dci_metadata ())
103         , _dcp_video_frame_rate (0)
104         , _dcp_audio_channels (MAX_AUDIO_CHANNELS)
105         , _dirty (false)
106 {
107         set_dci_date_today ();
108
109         _playlist->Changed.connect (bind (&Film::playlist_changed, this));
110         _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
111         
112         /* Make state.directory a complete path without ..s (where possible)
113            (Code swiped from Adam Bowen on stackoverflow)
114         */
115         
116         boost::filesystem::path p (boost::filesystem::system_complete (d));
117         boost::filesystem::path result;
118         for (boost::filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
119                 if (*i == "..") {
120                         if (boost::filesystem::is_symlink (result) || result.filename() == "..") {
121                                 result /= *i;
122                         } else {
123                                 result = result.parent_path ();
124                         }
125                 } else if (*i != ".") {
126                         result /= *i;
127                 }
128         }
129
130         set_directory (result.string ());
131         _log.reset (new FileLog (file ("log")));
132 }
133
134 Film::Film (Film const & o)
135         : boost::enable_shared_from_this<Film> (o)
136         /* note: the copied film shares the original's log */
137         , _log               (o._log)
138         , _playlist          (new Playlist (o._playlist))
139         , _directory         (o._directory)
140         , _name              (o._name)
141         , _use_dci_name      (o._use_dci_name)
142         , _dcp_content_type  (o._dcp_content_type)
143         , _container         (o._container)
144         , _scaler            (o._scaler)
145         , _ab                (o._ab)
146         , _with_subtitles    (o._with_subtitles)
147         , _subtitle_offset   (o._subtitle_offset)
148         , _subtitle_scale    (o._subtitle_scale)
149         , _colour_lut        (o._colour_lut)
150         , _j2k_bandwidth     (o._j2k_bandwidth)
151         , _dci_metadata      (o._dci_metadata)
152         , _dcp_video_frame_rate (o._dcp_video_frame_rate)
153         , _dci_date          (o._dci_date)
154         , _dirty             (o._dirty)
155 {
156         _playlist->ContentChanged.connect (bind (&Film::playlist_content_changed, this, _1, _2));
157 }
158
159 string
160 Film::video_state_identifier () const
161 {
162         assert (container ());
163         LocaleGuard lg;
164
165         stringstream s;
166         s << container()->id()
167           << "_" << _playlist->video_digest()
168           << "_" << _dcp_video_frame_rate
169           << "_" << scaler()->id()
170           << "_" << j2k_bandwidth()
171           << "_" << lexical_cast<int> (colour_lut());
172
173         if (trim_type() == ENCODE) {
174                 s << "_" << trim_start() << "_" << trim_end();
175         }
176
177         if (ab()) {
178                 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
179                 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
180         }
181
182         return s.str ();
183 }
184           
185 /** @return The path to the directory to write video frame info files to */
186 string
187 Film::info_dir () const
188 {
189         boost::filesystem::path p;
190         p /= "info";
191         p /= video_state_identifier ();
192         return dir (p.string());
193 }
194
195 string
196 Film::internal_video_mxf_dir () const
197 {
198         boost::filesystem::path p;
199         return dir ("video");
200 }
201
202 string
203 Film::internal_video_mxf_filename () const
204 {
205         return video_state_identifier() + ".mxf";
206 }
207
208 string
209 Film::dcp_video_mxf_filename () const
210 {
211         return filename_safe_name() + "_video.mxf";
212 }
213
214 string
215 Film::dcp_audio_mxf_filename () const
216 {
217         return filename_safe_name() + "_audio.mxf";
218 }
219
220 string
221 Film::filename_safe_name () const
222 {
223         string const n = name ();
224         string o;
225         for (size_t i = 0; i < n.length(); ++i) {
226                 if (isalnum (n[i])) {
227                         o += n[i];
228                 } else {
229                         o += "_";
230                 }
231         }
232
233         return o;
234 }
235
236 string
237 Film::audio_analysis_path () const
238 {
239         boost::filesystem::path p;
240         p /= "analysis";
241         p /= _playlist->audio_digest();
242         return file (p.string ());
243 }
244
245 /** Add suitable Jobs to the JobManager to create a DCP for this Film */
246 void
247 Film::make_dcp ()
248 {
249         set_dci_date_today ();
250         
251         if (dcp_name().find ("/") != string::npos) {
252                 throw BadSettingError (_("name"), _("cannot contain slashes"));
253         }
254         
255         log()->log (String::compose ("DCP-o-matic %1 git %2 using %3", dcpomatic_version, dcpomatic_git_commit, dependency_version_summary()));
256
257         {
258                 char buffer[128];
259                 gethostname (buffer, sizeof (buffer));
260                 log()->log (String::compose ("Starting to make DCP on %1", buffer));
261         }
262         
263 //      log()->log (String::compose ("Content is %1; type %2", content_path(), (content_type() == STILL ? _("still") : _("video"))));
264 //      if (length()) {
265 //              log()->log (String::compose ("Content length %1", length().get()));
266 //      }
267 //      log()->log (String::compose ("Content digest %1", content_digest()));
268 //      log()->log (String::compose ("Content at %1 fps, DCP at %2 fps", source_frame_rate(), dcp_frame_rate()));
269         log()->log (String::compose ("%1 threads", Config::instance()->num_local_encoding_threads()));
270         log()->log (String::compose ("J2K bandwidth %1", j2k_bandwidth()));
271 #ifdef DCPOMATIC_DEBUG
272         log()->log ("DCP-o-matic built in debug mode.");
273 #else
274         log()->log ("DCP-o-matic built in optimised mode.");
275 #endif
276 #ifdef LIBDCP_DEBUG
277         log()->log ("libdcp built in debug mode.");
278 #else
279         log()->log ("libdcp built in optimised mode.");
280 #endif
281         pair<string, int> const c = cpu_info ();
282         log()->log (String::compose ("CPU: %1, %2 processors", c.first, c.second));
283         
284         if (container() == 0) {
285                 throw MissingSettingError (_("container"));
286         }
287
288         if (_playlist->content().empty ()) {
289                 throw StringError (_("You must add some content to the DCP before creating it"));
290         }
291
292         if (dcp_content_type() == 0) {
293                 throw MissingSettingError (_("content type"));
294         }
295
296         if (name().empty()) {
297                 throw MissingSettingError (_("name"));
298         }
299
300         shared_ptr<Job> r;
301
302         if (ab()) {
303                 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (shared_from_this())));
304         } else {
305                 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (shared_from_this())));
306         }
307 }
308
309 /** Start a job to analyse the audio in our Playlist */
310 void
311 Film::analyse_audio ()
312 {
313         if (_analyse_audio_job) {
314                 return;
315         }
316
317         _analyse_audio_job.reset (new AnalyseAudioJob (shared_from_this()));
318         _analyse_audio_job->Finished.connect (bind (&Film::analyse_audio_finished, this));
319         JobManager::instance()->add (_analyse_audio_job);
320 }
321
322 void
323 Film::analyse_audio_finished ()
324 {
325         ensure_ui_thread ();
326
327         if (_analyse_audio_job->finished_ok ()) {
328                 AudioAnalysisSucceeded ();
329         }
330         
331         _analyse_audio_job.reset ();
332 }
333
334 /** Start a job to send our DCP to the configured TMS */
335 void
336 Film::send_dcp_to_tms ()
337 {
338         shared_ptr<Job> j (new SCPDCPJob (shared_from_this()));
339         JobManager::instance()->add (j);
340 }
341
342 /** Count the number of frames that have been encoded for this film.
343  *  @return frame count.
344  */
345 int
346 Film::encoded_frames () const
347 {
348         if (container() == 0) {
349                 return 0;
350         }
351
352         int N = 0;
353         for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (info_dir ()); i != boost::filesystem::directory_iterator(); ++i) {
354                 ++N;
355                 boost::this_thread::interruption_point ();
356         }
357
358         return N;
359 }
360
361 /** Write state to our `metadata' file */
362 void
363 Film::write_metadata () const
364 {
365         if (!boost::filesystem::exists (directory())) {
366                 boost::filesystem::create_directory (directory());
367         }
368         
369         boost::mutex::scoped_lock lm (_state_mutex);
370         LocaleGuard lg;
371
372         boost::filesystem::create_directories (directory());
373
374         xmlpp::Document doc;
375         xmlpp::Element* root = doc.create_root_node ("Metadata");
376
377         root->add_child("Version")->add_child_text (lexical_cast<string> (state_version));
378         root->add_child("Name")->add_child_text (_name);
379         root->add_child("UseDCIName")->add_child_text (_use_dci_name ? "1" : "0");
380
381         if (_dcp_content_type) {
382                 root->add_child("DCPContentType")->add_child_text (_dcp_content_type->dci_name ());
383         }
384
385         if (_container) {
386                 root->add_child("Container")->add_child_text (_container->id ());
387         }
388
389         root->add_child("Scaler")->add_child_text (_scaler->id ());
390         root->add_child("AB")->add_child_text (_ab ? "1" : "0");
391         root->add_child("WithSubtitles")->add_child_text (_with_subtitles ? "1" : "0");
392         root->add_child("SubtitleOffset")->add_child_text (lexical_cast<string> (_subtitle_offset));
393         root->add_child("SubtitleScale")->add_child_text (lexical_cast<string> (_subtitle_scale));
394         root->add_child("ColourLUT")->add_child_text (lexical_cast<string> (_colour_lut));
395         root->add_child("J2KBandwidth")->add_child_text (lexical_cast<string> (_j2k_bandwidth));
396         _dci_metadata.as_xml (root->add_child ("DCIMetadata"));
397         root->add_child("DCPVideoFrameRate")->add_child_text (lexical_cast<string> (_dcp_video_frame_rate));
398         root->add_child("DCIDate")->add_child_text (boost::gregorian::to_iso_string (_dci_date));
399         root->add_child("DCPAudioChannels")->add_child_text (lexical_cast<string> (_dcp_audio_channels));
400         _playlist->as_xml (root->add_child ("Playlist"));
401
402         doc.write_to_file_formatted (file ("metadata.xml"));
403         
404         _dirty = false;
405 }
406
407 /** Read state from our metadata file */
408 void
409 Film::read_metadata ()
410 {
411         boost::mutex::scoped_lock lm (_state_mutex);
412         LocaleGuard lg;
413
414         if (boost::filesystem::exists (file ("metadata")) && !boost::filesystem::exists (file ("metadata.xml"))) {
415                 throw StringError (_("This film was created with an older version of DCP-o-matic, and unfortunately it cannot be loaded into this version.  You will need to create a new Film, re-add your content and set it up again.  Sorry!"));
416         }
417
418         cxml::File f (file ("metadata.xml"), "Metadata");
419         
420         _name = f.string_child ("Name");
421         _use_dci_name = f.bool_child ("UseDCIName");
422
423         {
424                 optional<string> c = f.optional_string_child ("DCPContentType");
425                 if (c) {
426                         _dcp_content_type = DCPContentType::from_dci_name (c.get ());
427                 }
428         }
429
430         {
431                 optional<string> c = f.optional_string_child ("Container");
432                 if (c) {
433                         _container = Ratio::from_id (c.get ());
434                 }
435         }
436
437         _scaler = Scaler::from_id (f.string_child ("Scaler"));
438         _ab = f.bool_child ("AB");
439         _with_subtitles = f.bool_child ("WithSubtitles");
440         _subtitle_offset = f.number_child<float> ("SubtitleOffset");
441         _subtitle_scale = f.number_child<float> ("SubtitleScale");
442         _colour_lut = f.number_child<int> ("ColourLUT");
443         _j2k_bandwidth = f.number_child<int> ("J2KBandwidth");
444         _dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
445         _dcp_video_frame_rate = f.number_child<int> ("DCPVideoFrameRate");
446         _dci_date = boost::gregorian::from_undelimited_string (f.string_child ("DCIDate"));
447         _dcp_audio_channels = f.number_child<int> ("DCPAudioChannels");
448
449         _playlist->set_from_xml (shared_from_this(), f.node_child ("Playlist"));
450
451         _dirty = false;
452 }
453
454 /** Given a directory name, return its full path within the Film's directory.
455  *  The directory (and its parents) will be created if they do not exist.
456  */
457 string
458 Film::dir (string d) const
459 {
460         boost::mutex::scoped_lock lm (_directory_mutex);
461         
462         boost::filesystem::path p;
463         p /= _directory;
464         p /= d;
465         
466         boost::filesystem::create_directories (p);
467         
468         return p.string ();
469 }
470
471 /** Given a file or directory name, return its full path within the Film's directory.
472  *  _directory_mutex must not be locked on entry.
473  *  Any required parent directories will be created.
474  */
475 string
476 Film::file (string f) const
477 {
478         boost::mutex::scoped_lock lm (_directory_mutex);
479
480         boost::filesystem::path p;
481         p /= _directory;
482         p /= f;
483
484         boost::filesystem::create_directories (p.parent_path ());
485         
486         return p.string ();
487 }
488
489 /** @return a DCI-compliant name for a DCP of this film */
490 string
491 Film::dci_name (bool if_created_now) const
492 {
493         stringstream d;
494
495         string fixed_name = to_upper_copy (name());
496         for (size_t i = 0; i < fixed_name.length(); ++i) {
497                 if (fixed_name[i] == ' ') {
498                         fixed_name[i] = '-';
499                 }
500         }
501
502         /* Spec is that the name part should be maximum 14 characters, as I understand it */
503         if (fixed_name.length() > 14) {
504                 fixed_name = fixed_name.substr (0, 14);
505         }
506
507         d << fixed_name;
508
509         if (dcp_content_type()) {
510                 d << "_" << dcp_content_type()->dci_name();
511         }
512
513         if (container()) {
514                 d << "_" << container()->dci_name();
515         }
516
517         DCIMetadata const dm = dci_metadata ();
518
519         if (!dm.audio_language.empty ()) {
520                 d << "_" << dm.audio_language;
521                 if (!dm.subtitle_language.empty()) {
522                         d << "-" << dm.subtitle_language;
523                 } else {
524                         d << "-XX";
525                 }
526         }
527
528         if (!dm.territory.empty ()) {
529                 d << "_" << dm.territory;
530                 if (!dm.rating.empty ()) {
531                         d << "-" << dm.rating;
532                 }
533         }
534
535         d << "_51_2K";
536
537         if (!dm.studio.empty ()) {
538                 d << "_" << dm.studio;
539         }
540
541         if (if_created_now) {
542                 d << "_" << boost::gregorian::to_iso_string (boost::gregorian::day_clock::local_day ());
543         } else {
544                 d << "_" << boost::gregorian::to_iso_string (_dci_date);
545         }
546
547         if (!dm.facility.empty ()) {
548                 d << "_" << dm.facility;
549         }
550
551         if (!dm.package_type.empty ()) {
552                 d << "_" << dm.package_type;
553         }
554
555         return d.str ();
556 }
557
558 /** @return name to give the DCP */
559 string
560 Film::dcp_name (bool if_created_now) const
561 {
562         if (use_dci_name()) {
563                 return dci_name (if_created_now);
564         }
565
566         return name();
567 }
568
569
570 void
571 Film::set_directory (string d)
572 {
573         boost::mutex::scoped_lock lm (_state_mutex);
574         _directory = d;
575         _dirty = true;
576 }
577
578 void
579 Film::set_name (string n)
580 {
581         {
582                 boost::mutex::scoped_lock lm (_state_mutex);
583                 _name = n;
584         }
585         signal_changed (NAME);
586 }
587
588 void
589 Film::set_use_dci_name (bool u)
590 {
591         {
592                 boost::mutex::scoped_lock lm (_state_mutex);
593                 _use_dci_name = u;
594         }
595         signal_changed (USE_DCI_NAME);
596 }
597
598 void
599 Film::set_dcp_content_type (DCPContentType const * t)
600 {
601         {
602                 boost::mutex::scoped_lock lm (_state_mutex);
603                 _dcp_content_type = t;
604         }
605         signal_changed (DCP_CONTENT_TYPE);
606 }
607
608 void
609 Film::set_container (Ratio const * c)
610 {
611         {
612                 boost::mutex::scoped_lock lm (_state_mutex);
613                 _container = c;
614         }
615         signal_changed (CONTAINER);
616 }
617
618 void
619 Film::set_scaler (Scaler const * s)
620 {
621         {
622                 boost::mutex::scoped_lock lm (_state_mutex);
623                 _scaler = s;
624         }
625         signal_changed (SCALER);
626 }
627
628 void
629 Film::set_ab (bool a)
630 {
631         {
632                 boost::mutex::scoped_lock lm (_state_mutex);
633                 _ab = a;
634         }
635         signal_changed (AB);
636 }
637
638 void
639 Film::set_with_subtitles (bool w)
640 {
641         {
642                 boost::mutex::scoped_lock lm (_state_mutex);
643                 _with_subtitles = w;
644         }
645         signal_changed (WITH_SUBTITLES);
646 }
647
648 void
649 Film::set_subtitle_offset (int o)
650 {
651         {
652                 boost::mutex::scoped_lock lm (_state_mutex);
653                 _subtitle_offset = o;
654         }
655         signal_changed (SUBTITLE_OFFSET);
656 }
657
658 void
659 Film::set_subtitle_scale (float s)
660 {
661         {
662                 boost::mutex::scoped_lock lm (_state_mutex);
663                 _subtitle_scale = s;
664         }
665         signal_changed (SUBTITLE_SCALE);
666 }
667
668 void
669 Film::set_colour_lut (int i)
670 {
671         {
672                 boost::mutex::scoped_lock lm (_state_mutex);
673                 _colour_lut = i;
674         }
675         signal_changed (COLOUR_LUT);
676 }
677
678 void
679 Film::set_j2k_bandwidth (int b)
680 {
681         {
682                 boost::mutex::scoped_lock lm (_state_mutex);
683                 _j2k_bandwidth = b;
684         }
685         signal_changed (J2K_BANDWIDTH);
686 }
687
688 void
689 Film::set_dci_metadata (DCIMetadata m)
690 {
691         {
692                 boost::mutex::scoped_lock lm (_state_mutex);
693                 _dci_metadata = m;
694         }
695         signal_changed (DCI_METADATA);
696 }
697
698
699 void
700 Film::set_dcp_video_frame_rate (int f)
701 {
702         {
703                 boost::mutex::scoped_lock lm (_state_mutex);
704                 _dcp_video_frame_rate = f;
705         }
706         signal_changed (DCP_VIDEO_FRAME_RATE);
707 }
708
709 void
710 Film::signal_changed (Property p)
711 {
712         {
713                 boost::mutex::scoped_lock lm (_state_mutex);
714                 _dirty = true;
715         }
716
717         switch (p) {
718         case Film::CONTENT:
719                 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
720                 break;
721         default:
722                 break;
723         }
724
725         if (ui_signaller) {
726                 ui_signaller->emit (boost::bind (boost::ref (Changed), p));
727         }
728 }
729
730 void
731 Film::set_dci_date_today ()
732 {
733         _dci_date = boost::gregorian::day_clock::local_day ();
734 }
735
736 string
737 Film::info_path (int f) const
738 {
739         boost::filesystem::path p;
740         p /= info_dir ();
741
742         stringstream s;
743         s.width (8);
744         s << setfill('0') << f << ".md5";
745
746         p /= s.str();
747
748         /* info_dir() will already have added any initial bit of the path,
749            so don't call file() on this.
750         */
751         return p.string ();
752 }
753
754 string
755 Film::j2c_path (int f, bool t) const
756 {
757         boost::filesystem::path p;
758         p /= "j2c";
759         p /= video_state_identifier ();
760
761         stringstream s;
762         s.width (8);
763         s << setfill('0') << f << ".j2c";
764
765         if (t) {
766                 s << ".tmp";
767         }
768
769         p /= s.str();
770         return file (p.string ());
771 }
772
773 /** Make an educated guess as to whether we have a complete DCP
774  *  or not.
775  *  @return true if we do.
776  */
777
778 bool
779 Film::have_dcp () const
780 {
781         try {
782                 libdcp::DCP dcp (dir (dcp_name()));
783                 dcp.read ();
784         } catch (...) {
785                 return false;
786         }
787
788         return true;
789 }
790
791 shared_ptr<Player>
792 Film::player () const
793 {
794         return shared_ptr<Player> (new Player (shared_from_this (), _playlist));
795 }
796
797 shared_ptr<Playlist>
798 Film::playlist () const
799 {
800         boost::mutex::scoped_lock lm (_state_mutex);
801         return _playlist;
802 }
803
804 Playlist::ContentList
805 Film::content () const
806 {
807         return _playlist->content ();
808 }
809
810 void
811 Film::examine_and_add_content (shared_ptr<Content> c)
812 {
813         shared_ptr<Job> j (new ExamineContentJob (shared_from_this(), c));
814         JobManager::instance()->add (j);
815 }
816
817 void
818 Film::add_content (shared_ptr<Content> c)
819 {
820         /* Add video content after any existing content */
821         if (dynamic_pointer_cast<VideoContent> (c)) {
822                 c->set_start (_playlist->video_end ());
823         }
824
825         _playlist->add (c);
826 }
827
828 void
829 Film::remove_content (shared_ptr<Content> c)
830 {
831         _playlist->remove (c);
832 }
833
834 Time
835 Film::length () const
836 {
837         return _playlist->length ();
838 }
839
840 bool
841 Film::has_subtitles () const
842 {
843         return _playlist->has_subtitles ();
844 }
845
846 OutputVideoFrame
847 Film::best_dcp_video_frame_rate () const
848 {
849         return _playlist->best_dcp_frame_rate ();
850 }
851
852 void
853 Film::playlist_content_changed (boost::weak_ptr<Content> c, int p)
854 {
855         if (p == VideoContentProperty::VIDEO_FRAME_RATE) {
856                 set_dcp_video_frame_rate (_playlist->best_dcp_frame_rate ());
857         } 
858
859         if (ui_signaller) {
860                 ui_signaller->emit (boost::bind (boost::ref (ContentChanged), c, p));
861         }
862 }
863
864 void
865 Film::playlist_changed ()
866 {
867         signal_changed (CONTENT);
868 }       
869
870 int
871 Film::loop () const
872 {
873         return _playlist->loop ();
874 }
875
876 void
877 Film::set_loop (int c)
878 {
879         _playlist->set_loop (c);
880 }
881
882 OutputAudioFrame
883 Film::time_to_audio_frames (Time t) const
884 {
885         return t * dcp_audio_frame_rate () / TIME_HZ;
886 }
887
888 OutputVideoFrame
889 Film::time_to_video_frames (Time t) const
890 {
891         return t * dcp_video_frame_rate () / TIME_HZ;
892 }
893
894 Time
895 Film::audio_frames_to_time (OutputAudioFrame f) const
896 {
897         return f * TIME_HZ / dcp_audio_frame_rate ();
898 }
899
900 Time
901 Film::video_frames_to_time (OutputVideoFrame f) const
902 {
903         return f * TIME_HZ / dcp_video_frame_rate ();
904 }
905
906 OutputAudioFrame
907 Film::dcp_audio_frame_rate () const
908 {
909         /* XXX */
910         return 48000;
911 }
912
913 void
914 Film::set_sequence_video (bool s)
915 {
916         _playlist->set_sequence_video (s);
917 }
918
919 libdcp::Size
920 Film::full_frame () const
921 {
922         return libdcp::Size (2048, 1080);
923 }