Various time-related fixes; fix daft hang on decodes.
[dcpomatic.git] / src / lib / playlist.cc
index f04bbe0cb13b608d1a07d3d297ec1cbc90987849..912d90f0e5f482d19404101e282e713a1268b48e 100644 (file)
@@ -1,3 +1,5 @@
+/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
+
 /*
     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
 
@@ -17,6 +19,7 @@
 
 */
 
+#include <libcxml/cxml.h>
 #include <boost/shared_ptr.hpp>
 #include <boost/lexical_cast.hpp>
 #include "playlist.h"
 #include "ffmpeg_decoder.h"
 #include "ffmpeg_content.h"
 #include "imagemagick_decoder.h"
+#include "imagemagick_content.h"
 #include "job.h"
+#include "config.h"
+#include "util.h"
+
+#include "i18n.h"
 
 using std::list;
 using std::cout;
@@ -34,274 +42,242 @@ using std::vector;
 using std::min;
 using std::max;
 using std::string;
+using std::stringstream;
+using boost::optional;
 using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
 using boost::lexical_cast;
 
 Playlist::Playlist ()
-       : _audio_from (AUDIO_FFMPEG)
+       : _loop (1)
 {
 
 }
 
-void
-Playlist::setup (ContentList content)
+Playlist::Playlist (shared_ptr<const Playlist> other)
+       : _loop (other->_loop)
 {
-       _audio_from = AUDIO_FFMPEG;
+       for (RegionList::const_iterator i = other->_regions.begin(); i != other->_regions.end(); ++i) {
+               _regions.push_back (Region (i->content->clone(), i->time, this));
+       }
+}
 
-       _video.clear ();
-       _sndfile.clear ();
+void
+Playlist::content_changed (weak_ptr<Content> c, int p)
+{
+       ContentChanged (c, p);
+}
 
-       for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
-               i->disconnect ();
-       }
+string
+Playlist::audio_digest () const
+{
+       string t;
        
-       _content_connections.clear ();
-
-       for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
-               shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
-               if (vc) {
-                       _video.push_back (vc);
+       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+               if (!dynamic_pointer_cast<const AudioContent> (i->content)) {
+                       continue;
                }
                
-               shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
-               if (sc) {
-                       _sndfile.push_back (sc);
-                       _audio_from = AUDIO_SNDFILE;
-               }
+               t += i->content->digest ();
 
-               _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
+               if (fc) {
+                       t += lexical_cast<string> (fc->audio_stream()->id);
+               }
        }
 
-       Changed ();
+       t += lexical_cast<string> (_loop);
+
+       return md5_digest (t.c_str(), t.length());
 }
 
-ContentAudioFrame
-Playlist::audio_length () const
+string
+Playlist::video_digest () const
 {
-       ContentAudioFrame len = 0;
+       string t;
        
-       switch (_audio_from) {
-       case AUDIO_FFMPEG:
-               for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
-                       shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
-                       if (fc) {
-                               len += fc->audio_length ();
-                       }
+       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+               if (!dynamic_pointer_cast<const VideoContent> (i->content)) {
+                       continue;
                }
-               break;
-       case AUDIO_SNDFILE:
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       len += (*i)->audio_length ();
+               
+               t += i->content->digest ();
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
+               if (fc && fc->subtitle_stream()) {
+                       t += fc->subtitle_stream()->id;
                }
-               break;
        }
 
-       return len;
+       t += lexical_cast<string> (_loop);
+
+       return md5_digest (t.c_str(), t.length());
 }
 
-int
-Playlist::audio_channels () const
+void
+Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
 {
-       int channels = 0;
-       
-       switch (_audio_from) {
-       case AUDIO_FFMPEG:
-               for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
-                       shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
-                       if (fc) {
-                               channels = max (channels, fc->audio_channels ());
-                       }
-               }
-               break;
-       case AUDIO_SNDFILE:
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       channels += (*i)->audio_channels ();
-               }
-               break;
+       list<shared_ptr<cxml::Node> > c = node->node_children ("Region");
+       for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
+               _regions.push_back (Region (*i, this));
        }
 
-       return channels;
+       _loop = node->number_child<int> ("Loop");
 }
 
-int
-Playlist::audio_frame_rate () const
+void
+Playlist::as_xml (xmlpp::Node* node)
 {
-       /* XXX: assuming that all content has the same rate */
-       
-       switch (_audio_from) {
-       case AUDIO_FFMPEG:
-       {
-               shared_ptr<const FFmpegContent> fc = first_ffmpeg ();
-               if (fc) {
-                       return fc->audio_frame_rate ();
-               }
-               break;
-       }
-       case AUDIO_SNDFILE:
-               return _sndfile.front()->audio_frame_rate ();
+       for (RegionList::iterator i = _regions.begin(); i != _regions.end(); ++i) {
+               i->as_xml (node->add_child ("Region"));
        }
 
-       return 0;
+       node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
 }
 
-float
-Playlist::video_frame_rate () const
+void
+Playlist::add (shared_ptr<Content> c)
 {
-       if (_video.empty ()) {
-               return 0;
-       }
-       
-       /* XXX: assuming all the same */
-       return _video.front()->video_frame_rate ();
+       _regions.push_back (Region (c, 0, this));
+       Changed ();
 }
 
-libdcp::Size
-Playlist::video_size () const
+void
+Playlist::remove (shared_ptr<Content> c)
 {
-       if (_video.empty ()) {
-               return libdcp::Size ();
+       RegionList::iterator i = _regions.begin ();
+       while (i != _regions.end() && i->content != c) {
+               ++i;
+       }
+       
+       if (i != _regions.end ()) {
+               _regions.erase (i);
+               Changed ();
        }
-
-       /* XXX: assuming all the same */
-       return _video.front()->video_size ();
 }
 
-ContentVideoFrame
-Playlist::video_length () const
+void
+Playlist::set_loop (int l)
 {
-       ContentVideoFrame len = 0;
-       for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
-               len += (*i)->video_length ();
-       }
-       
-       return len;
+       _loop = l;
+       Changed ();
 }
 
 bool
-Playlist::has_audio () const
+Playlist::has_subtitles () const
 {
-       if (!_sndfile.empty ()) {
-               return true;
-       }
-
-       for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
-               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
-               if (fc && fc->audio_stream ()) {
+       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i->content);
+               if (fc && !fc->subtitle_streams().empty()) {
                        return true;
                }
        }
-       
+
        return false;
 }
 
-void
-Playlist::content_changed (weak_ptr<Content> c, int p)
+Playlist::Region::Region (shared_ptr<Content> c, Time t, Playlist* p)
+       : content (c)
+       , time (t)
 {
-       ContentChanged (c, p);
+       connection = c->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
 }
 
-shared_ptr<const FFmpegContent>
-Playlist::first_ffmpeg () const
+Playlist::Region::Region (shared_ptr<const cxml::Node> node, Playlist* p)
 {
-       for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
-               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
-               if (fc) {
-                       return fc;
-               }
+       shared_ptr<const cxml::Node> content_node = node->node_child ("Content");
+       string const type = content_node->string_child ("Type");
+
+       if (type == "FFmpeg") {
+               content.reset (new FFmpegContent (content_node));
+       } else if (type == "ImageMagick") {
+               content.reset (new ImageMagickContent (content_node));
+       } else if (type == "Sndfile") {
+               content.reset (new SndfileContent (content_node));
        }
 
-       return shared_ptr<const FFmpegContent> ();
+       time = node->number_child<Time> ("Time");
+       connection = content->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
 }
-       
 
-AudioMapping
-Playlist::default_audio_mapping () const
+void
+Playlist::Region::as_xml (xmlpp::Node* node) const
 {
-       AudioMapping m;
-
-       switch (_audio_from) {
-       case AUDIO_FFMPEG:
-       {
-               shared_ptr<const FFmpegContent> fc = first_ffmpeg ();
-               if (!fc) {
-                       break;
-               }
-               
-               /* XXX: assumes all the same */
-               if (fc->audio_channels() == 1) {
-                       /* Map mono sources to centre */
-                       m.add (AudioMapping::Channel (fc, 0), libdcp::CENTRE);
-               } else {
-                       int const N = min (fc->audio_channels (), MAX_AUDIO_CHANNELS);
-                       /* Otherwise just start with a 1:1 mapping */
-                       for (int i = 0; i < N; ++i) {
-                               m.add (AudioMapping::Channel (fc, i), (libdcp::Channel) i);
-                       }
-               }
-               break;
-       }
+       xmlpp::Node* sub = node->add_child ("Content");
+       content->as_xml (sub);
+       node->add_child ("Time")->add_child_text (lexical_cast<string> (time));
+}
 
-       case AUDIO_SNDFILE:
-       {
-               int n = 0;
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       for (int j = 0; j < (*i)->audio_channels(); ++j) {
-                               m.add (AudioMapping::Channel (*i, j), (libdcp::Channel) n);
-                               ++n;
-                               if (n >= MAX_AUDIO_CHANNELS) {
-                                       break;
-                               }
-                       }
-                       if (n >= MAX_AUDIO_CHANNELS) {
-                               break;
-                       }
-               }
-               break;
-       }
-       }
+class FrameRateCandidate
+{
+public:
+       FrameRateCandidate (float source_, int dcp_)
+               : source (source_)
+               , dcp (dcp_)
+       {}
 
-       return m;
-}
+       float source;
+       int dcp;
+};
 
-string
-Playlist::audio_digest () const
+int
+Playlist::best_dcp_frame_rate () const
 {
-       string t;
-       
-       switch (_audio_from) {
-       case AUDIO_FFMPEG:
-               for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
-                       shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
-                       if (fc) {
-                               t += (*i)->digest ();
-                               t += lexical_cast<string> (fc->audio_stream()->id);
+       list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
+
+       /* Work out what rates we could manage, including those achieved by using skip / repeat. */
+       list<FrameRateCandidate> candidates;
+
+       /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */
+       for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
+               candidates.push_back (FrameRateCandidate (*i, *i));
+       }
+
+       /* Then the skip/repeat ones */
+       for (list<int>::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) {
+               candidates.push_back (FrameRateCandidate (float (*i) / 2, *i));
+               candidates.push_back (FrameRateCandidate (float (*i) * 2, *i));
+       }
+
+       /* Pick the best one, bailing early if we hit an exact match */
+       float error = std::numeric_limits<float>::max ();
+       optional<FrameRateCandidate> best;
+       list<FrameRateCandidate>::iterator i = candidates.begin();
+       while (i != candidates.end()) {
+
+               float this_error = std::numeric_limits<float>::max ();
+               for (RegionList::const_iterator j = _regions.begin(); j != _regions.end(); ++j) {
+                       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j->content);
+                       if (!vc) {
+                               continue;
                        }
+
+                       this_error += fabs (i->source - vc->video_frame_rate ());
                }
-               break;
-       case AUDIO_SNDFILE:
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       t += (*i)->digest ();
+
+               if (this_error < error) {
+                       error = this_error;
+                       best = *i;
                }
-               break;
+
+               ++i;
        }
 
-       return md5_digest (t.c_str(), t.length());
+       if (!best) {
+               return 24;
+       }
+       
+       return best->dcp;
 }
 
-string
-Playlist::video_digest () const
+Time
+Playlist::length (shared_ptr<const Film> film) const
 {
-       string t;
-       
-       for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
-               t += (*i)->digest ();
-               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
-               if (fc && fc->subtitle_stream()) {
-                       t += fc->subtitle_stream()->id;
-               }
+       Time len = 0;
+       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+               Time const t = i->time + i->content->length (film);
+               len = max (len, t);
        }
 
-       return md5_digest (t.c_str(), t.length());
+       return len;
 }