Various time-related fixes; fix daft hang on decodes.
[dcpomatic.git] / src / lib / playlist.cc
index 3f7905fa9c11c251bacdeeefb38f565aab699141..912d90f0e5f482d19404101e282e713a1268b48e 100644 (file)
@@ -1,3 +1,5 @@
+/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
+
 /*
     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
 
 
 */
 
+#include <libcxml/cxml.h>
 #include <boost/shared_ptr.hpp>
+#include <boost/lexical_cast.hpp>
 #include "playlist.h"
 #include "sndfile_content.h"
 #include "sndfile_decoder.h"
-#include "ffmpeg_content.h"
+#include "video_content.h"
 #include "ffmpeg_decoder.h"
-#include "imagemagick_content.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;
 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 ()
-       : _video_from (VIDEO_NONE)
-       , _audio_from (AUDIO_NONE)
+       : _loop (1)
 {
 
 }
 
-void
-Playlist::setup (ContentList content)
+Playlist::Playlist (shared_ptr<const Playlist> other)
+       : _loop (other->_loop)
 {
-       _video_from = VIDEO_NONE;
-       _audio_from = AUDIO_NONE;
+       for (RegionList::const_iterator i = other->_regions.begin(); i != other->_regions.end(); ++i) {
+               _regions.push_back (Region (i->content->clone(), i->time, this));
+       }
+}
 
-       _ffmpeg.reset ();
-       _imagemagick.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<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
-               if (fc) {
-                       assert (!_ffmpeg);
-                       _ffmpeg = fc;
-                       _video_from = VIDEO_FFMPEG;
-                       if (_audio_from == AUDIO_NONE) {
-                               _audio_from = AUDIO_FFMPEG;
-                       }
+       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+               if (!dynamic_pointer_cast<const AudioContent> (i->content)) {
+                       continue;
                }
                
-               shared_ptr<ImageMagickContent> ic = dynamic_pointer_cast<ImageMagickContent> (*i);
-               if (ic) {
-                       _imagemagick.push_back (ic);
-                       if (_video_from == VIDEO_NONE) {
-                               _video_from = VIDEO_IMAGEMAGICK;
-                       }
-               }
+               t += i->content->digest ();
 
-               shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
-               if (sc) {
-                       _sndfile.push_back (sc);
-                       _audio_from = AUDIO_SNDFILE;
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
+               if (fc) {
+                       t += lexical_cast<string> (fc->audio_stream()->id);
                }
-
-               _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
        }
 
-       Changed ();
+       t += lexical_cast<string> (_loop);
+
+       return md5_digest (t.c_str(), t.length());
 }
 
-ContentAudioFrame
-Playlist::audio_length () const
+string
+Playlist::video_digest () const
 {
-       switch (_audio_from) {
-       case AUDIO_NONE:
-               return 0;
-       case AUDIO_FFMPEG:
-               return _ffmpeg->audio_length ();
-       case AUDIO_SNDFILE:
-       {
-               ContentAudioFrame l = 0;
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       l += (*i)->audio_length ();
+       string t;
+       
+       for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+               if (!dynamic_pointer_cast<const VideoContent> (i->content)) {
+                       continue;
+               }
+               
+               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;
                }
-               return l;
-       }
        }
 
-       return 0;
+       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)
 {
-       switch (_audio_from) {
-       case AUDIO_NONE:
-               return 0;
-       case AUDIO_FFMPEG:
-               return _ffmpeg->audio_channels ();
-       case AUDIO_SNDFILE:
-       {
-               int c = 0;
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       c += (*i)->audio_channels ();
-               }
-               return c;
-       }
+       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 0;
+       _loop = node->number_child<int> ("Loop");
 }
 
-int
-Playlist::audio_frame_rate () const
+void
+Playlist::as_xml (xmlpp::Node* node)
 {
-       switch (_audio_from) {
-       case AUDIO_NONE:
-               return 0;
-       case AUDIO_FFMPEG:
-               return _ffmpeg->audio_frame_rate ();
-       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));
 }
 
-int64_t
-Playlist::audio_channel_layout () const
+void
+Playlist::add (shared_ptr<Content> c)
 {
-       switch (_audio_from) {
-       case AUDIO_NONE:
-               return 0;
-       case AUDIO_FFMPEG:
-               return _ffmpeg->audio_channel_layout ();
-       case AUDIO_SNDFILE:
-               /* XXX */
-               return 0;
-       }
-
-       return 0;
+       _regions.push_back (Region (c, 0, this));
+       Changed ();
 }
 
-float
-Playlist::video_frame_rate () const
+void
+Playlist::remove (shared_ptr<Content> c)
 {
-       switch (_video_from) {
-       case VIDEO_NONE:
-               return 0;
-       case VIDEO_FFMPEG:
-               return _ffmpeg->video_frame_rate ();
-       case VIDEO_IMAGEMAGICK:
-               return 24;
+       RegionList::iterator i = _regions.begin ();
+       while (i != _regions.end() && i->content != c) {
+               ++i;
+       }
+       
+       if (i != _regions.end ()) {
+               _regions.erase (i);
+               Changed ();
        }
-
-       return 0;
 }
 
-libdcp::Size
-Playlist::video_size () const
+void
+Playlist::set_loop (int l)
 {
-       switch (_video_from) {
-       case VIDEO_NONE:
-               return libdcp::Size ();
-       case VIDEO_FFMPEG:
-               return _ffmpeg->video_size ();
-       case VIDEO_IMAGEMAGICK:
-               /* XXX */
-               return _imagemagick.front()->video_size ();
-       }
-
-       return libdcp::Size ();
+       _loop = l;
+       Changed ();
 }
 
-ContentVideoFrame
-Playlist::video_length () const
+bool
+Playlist::has_subtitles () const
 {
-       switch (_video_from) {
-       case VIDEO_NONE:
-               return 0;
-       case VIDEO_FFMPEG:
-               return _ffmpeg->video_length ();
-       case VIDEO_IMAGEMAGICK:
-       {
-               ContentVideoFrame l = 0;
-               for (list<shared_ptr<const ImageMagickContent> >::const_iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
-                       l += (*i)->video_length ();
+       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 l;
-       }
        }
 
-       return 0;
+       return false;
 }
 
-bool
-Playlist::has_audio () const
+Playlist::Region::Region (shared_ptr<Content> c, Time t, Playlist* p)
+       : content (c)
+       , time (t)
 {
-       return _audio_from != AUDIO_NONE;
+       connection = c->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
+}
+
+Playlist::Region::Region (shared_ptr<const cxml::Node> node, Playlist* p)
+{
+       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));
+       }
+
+       time = node->number_child<Time> ("Time");
+       connection = content->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
 }
 
 void
-Playlist::content_changed (weak_ptr<Content> c, int p)
+Playlist::Region::as_xml (xmlpp::Node* node) const
 {
-       ContentChanged (c, p);
+       xmlpp::Node* sub = node->add_child ("Content");
+       content->as_xml (sub);
+       node->add_child ("Time")->add_child_text (lexical_cast<string> (time));
 }
 
-AudioMapping
-Playlist::default_audio_mapping () const
+class FrameRateCandidate
+{
+public:
+       FrameRateCandidate (float source_, int dcp_)
+               : source (source_)
+               , dcp (dcp_)
+       {}
+
+       float source;
+       int dcp;
+};
+
+int
+Playlist::best_dcp_frame_rate () const
 {
-       AudioMapping m;
-
-       switch (_audio_from) {
-       case AUDIO_NONE:
-               break;
-       case AUDIO_FFMPEG:
-               if (_ffmpeg->audio_channels() == 1) {
-                       /* Map mono sources to centre */
-                       m.add (AudioMapping::Channel (_ffmpeg, 0), libdcp::CENTRE);
-               } else {
-                       int const N = min (_ffmpeg->audio_channels (), MAX_AUDIO_CHANNELS);
-                       /* Otherwise just start with a 1:1 mapping */
-                       for (int i = 0; i < N; ++i) {
-                               m.add (AudioMapping::Channel (_ffmpeg, i), (libdcp::Channel) i);
+       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:
-       {
-               int n = 0;
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       cout << "sndfile " << (*i)->audio_channels() << "\n";
-                       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;
-                       }
+
+               if (this_error < error) {
+                       error = this_error;
+                       best = *i;
                }
-               break;
+
+               ++i;
+       }
+
+       if (!best) {
+               return 24;
        }
+       
+       return best->dcp;
+}
+
+Time
+Playlist::length (shared_ptr<const Film> film) const
+{
+       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 m;
+       return len;
 }