Merge master.
[dcpomatic.git] / src / lib / playlist.cc
index 3822420da1a3f4c7de9c46c09004ff8104f82487..16c740943ca89f382f27082ba70a935d987c484b 100644 (file)
 
 */
 
+#include <libcxml/cxml.h>
 #include <boost/shared_ptr.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 "imagemagick_decoder.h"
+#include "ffmpeg_content.h"
+#include "image_decoder.h"
+#include "content_factory.h"
+#include "job.h"
+#include "config.h"
+#include "util.h"
+#include "md5_digester.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 std::pair;
+using boost::optional;
 using boost::shared_ptr;
+using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
 
 Playlist::Playlist ()
-       : _video_from (VIDEO_NONE)
-       , _audio_from (AUDIO_NONE)
+       : _sequence_video (true)
+       , _sequencing_video (false)
 {
 
 }
 
+Playlist::~Playlist ()
+{
+       _content.clear ();
+       reconnect ();
+}
+
 void
-Playlist::setup (ContentList content)
+Playlist::content_changed (weak_ptr<Content> content, int property, bool frequent)
 {
-       _video_from = VIDEO_NONE;
-       _audio_from = AUDIO_NONE;
+       if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) {
+               maybe_sequence_video ();
+       }
+       
+       ContentChanged (content, property, frequent);
+}
 
-       _ffmpeg.reset ();
-       _imagemagick.clear ();
-       _sndfile.clear ();
+void
+Playlist::maybe_sequence_video ()
+{
+       if (!_sequence_video || _sequencing_video) {
+               return;
+       }
        
-       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;
-                       }
+       _sequencing_video = true;
+       
+       ContentList cl = _content;
+       DCPTime next_left;
+       DCPTime next_right;
+       for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+               shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
+               if (!vc) {
+                       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;
-                       }
-               }
-
-               shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
-               if (sc) {
-                       _sndfile.push_back (sc);
-                       _audio_from = AUDIO_SNDFILE;
+               if (vc->video_frame_type() == VIDEO_FRAME_TYPE_3D_RIGHT) {
+                       vc->set_position (next_right);
+                       next_right = vc->end() + DCPTime::delta ();
+               } else {
+                       vc->set_position (next_left);
+                       next_left = vc->end() + DCPTime::delta ();
                }
        }
+
+       /* This won't change order, so it does not need a sort */
+       
+       _sequencing_video = false;
 }
 
-ContentAudioFrame
-Playlist::audio_length () const
+string
+Playlist::video_identifier () 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 (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
+               if (vc) {
+                       t += vc->identifier ();
                }
-               return l;
-       }
        }
 
-       return 0;
+       MD5Digester digester;
+       digester.add (t.c_str(), t.length());
+       return digester.get ();
 }
 
-int
-Playlist::audio_channels () const
+/** @param node <Playlist> node */
+void
+Playlist::set_from_xml (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version, list<string>& notes)
 {
-       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<cxml::NodePtr> c = node->node_children ("Content");
+       for (list<cxml::NodePtr>::iterator i = c.begin(); i != c.end(); ++i) {
+               _content.push_back (content_factory (film, *i, version, notes));
        }
 
-       return 0;
+       sort (_content.begin(), _content.end(), ContentSorter ());
+
+       reconnect ();
 }
 
-int
-Playlist::audio_frame_rate () const
+/** @param node <Playlist> node */
+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 (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+               (*i)->as_xml (node->add_child ("Content"));
        }
+}
 
-       return 0;
+void
+Playlist::add (shared_ptr<Content> c)
+{
+       _content.push_back (c);
+       sort (_content.begin(), _content.end(), ContentSorter ());
+       reconnect ();
+       Changed ();
 }
 
-int64_t
-Playlist::audio_channel_layout () const
+void
+Playlist::remove (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;
+       ContentList::iterator i = _content.begin ();
+       while (i != _content.end() && *i != c) {
+               ++i;
+       }
+       
+       if (i != _content.end ()) {
+               _content.erase (i);
+               Changed ();
        }
 
-       return 0;
+       /* This won't change order, so it does not need a sort */
 }
 
-float
-Playlist::video_frame_rate () const
+void
+Playlist::remove (ContentList c)
 {
-       switch (_video_from) {
-       case VIDEO_NONE:
-               return 0;
-       case VIDEO_FFMPEG:
-               return _ffmpeg->video_frame_rate ();
-       case VIDEO_IMAGEMAGICK:
-               return 24;
+       for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
+               ContentList::iterator j = _content.begin ();
+               while (j != _content.end() && *j != *i) {
+                       ++j;
+               }
+       
+               if (j != _content.end ()) {
+                       _content.erase (j);
+               }
        }
 
-       return 0;
+       /* This won't change order, so it does not need a sort */
+       
+       Changed ();
 }
 
-libdcp::Size
-Playlist::video_size () const
+class FrameRateCandidate
 {
-       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 ();
-       }
+public:
+       FrameRateCandidate (float source_, int dcp_)
+               : source (source_)
+               , dcp (dcp_)
+       {}
 
-       return libdcp::Size ();
-}
+       float source;
+       int dcp;
+};
 
-ContentVideoFrame
-Playlist::video_length () const
+int
+Playlist::best_dcp_frame_rate () 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 ();
-               }
-               return l;
+       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));
        }
 
-       return 0;
-}
+       /* Pick the best one */
+       float error = std::numeric_limits<float>::max ();
+       optional<FrameRateCandidate> best;
+       list<FrameRateCandidate>::iterator i = candidates.begin();
+       while (i != candidates.end()) {
+
+               float this_error = 0;
+               for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
+                       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
+                       if (!vc) {
+                               continue;
+                       }
 
-bool
-Playlist::has_audio () const
-{
-       return _audio_from != AUDIO_NONE;
-}
+                       /* Use the largest difference between DCP and source as the "error" */
+                       this_error = max (this_error, float (fabs (i->source - vc->video_frame_rate ())));
+               }
 
-Player::Player (boost::shared_ptr<const Film> f, boost::shared_ptr<const Playlist> p)
-       : _film (f)
-       , _playlist (p)
-       , _video (true)
-       , _audio (true)
-       , _subtitles (true)
-       , _have_setup_decoders (false)
-       , _ffmpeg_decoder_done (false)
-       , _video_sync (true)
-{
+               if (this_error < error) {
+                       error = this_error;
+                       best = *i;
+               }
 
-}
-               
-void
-Player::disable_video ()
-{
-       _video = false;
+               ++i;
+       }
+
+       if (!best) {
+               return 24;
+       }
+       
+       return best->dcp;
 }
 
-void
-Player::disable_audio ()
+DCPTime
+Playlist::length () const
 {
-       _audio = false;
+       DCPTime len;
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               len = max (len, (*i)->end() + DCPTime::delta ());
+       }
+
+       return len;
 }
 
 void
-Player::disable_subtitles ()
+Playlist::reconnect ()
 {
-       _subtitles = false;
+       for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
+               i->disconnect ();
+       }
+
+       _content_connections.clear ();
+               
+       for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+               _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2, _3)));
+       }
 }
 
-bool
-Player::pass ()
+DCPTime
+Playlist::video_end () const
 {
-       if (!_have_setup_decoders) {
-               setup_decoders ();
-               _have_setup_decoders = true;
-       }
-       
-       bool done = true;
-       
-       if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
-               if (!_ffmpeg_decoder_done) {
-                       if (_ffmpeg_decoder->pass ()) {
-                               _ffmpeg_decoder_done = true;
-                       } else {
-                               done = false;
-                       }
+       DCPTime end;
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               if (dynamic_pointer_cast<const VideoContent> (*i)) {
+                       end = max (end, (*i)->end ());
                }
        }
 
-       if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
-               if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
-                       if ((*_imagemagick_decoder)->pass ()) {
-                               _imagemagick_decoder++;
-                       }
+       return end;
+}
 
-                       if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
-                               done = false;
-                       }
+FrameRateChange
+Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const
+{
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               shared_ptr<const VideoContent> vc = dynamic_pointer_cast<const VideoContent> (*i);
+               if (!vc) {
+                       continue;
                }
-       }
 
-       /* XXX: sndfile */
+               if (vc->position() >= t && t < vc->end()) {
+                       return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate);
+               }
+       }
 
-       return done;
+       return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate);
 }
 
 void
-Player::set_progress (shared_ptr<Job> job)
+Playlist::set_sequence_video (bool s)
 {
-       /* XXX */
+       _sequence_video = s;
 }
 
-void
-Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
+bool
+ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
 {
-       Video (i, same, s);
+       return a->position() < b->position();
 }
 
-void
-Player::process_audio (shared_ptr<AudioBuffers> b)
+/** @return content in an undefined order */
+ContentList
+Playlist::content () const
 {
-       Audio (b);
+       return _content;
 }
 
-/** @return true on error */
-bool
-Player::seek (double t)
+void
+Playlist::repeat (ContentList c, int n)
 {
-       bool r = false;
-
-       switch (_playlist->video_from()) {
-       case Playlist::VIDEO_NONE:
-               break;
-       case Playlist::VIDEO_FFMPEG:
-               if (_ffmpeg_decoder->seek (t)) {
-                       r = true;
-               }
-               break;
-       case Playlist::VIDEO_IMAGEMAGICK:
-               /* Find the decoder that contains this position */
-               _imagemagick_decoder = _imagemagick_decoders.begin ();
-               while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
-                       double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
-                       if (t < this_length) {
-                               break;
-                       }
-                       t -= this_length;
-                       ++_imagemagick_decoder;
-               }
+       pair<DCPTime, DCPTime> range (DCPTime::max (), DCPTime ());
+       for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
+               range.first = min (range.first, (*i)->position ());
+               range.second = max (range.second, (*i)->position ());
+               range.first = min (range.first, (*i)->end ());
+               range.second = max (range.second, (*i)->end ());
+       }
 
-               if (_imagemagick_decoder != _imagemagick_decoders.end()) {
-                       (*_imagemagick_decoder)->seek (t);
-               } else {
-                       r = true;
+       DCPTime pos = range.second;
+       for (int i = 0; i < n; ++i) {
+               for (ContentList::iterator i = c.begin(); i != c.end(); ++i) {
+                       shared_ptr<Content> copy = (*i)->clone ();
+                       copy->set_position (pos + copy->position() - range.first);
+                       _content.push_back (copy);
                }
-               break;
+               pos += range.second - range.first;
        }
 
-       /* XXX: don't seek audio because we don't need to... */
-
-       return r;
+       sort (_content.begin(), _content.end(), ContentSorter ());
+       
+       reconnect ();
+       Changed ();
 }
 
-bool
-Player::seek_to_last ()
+void
+Playlist::move_earlier (shared_ptr<Content> c)
 {
-       bool r = false;
+       sort (_content.begin(), _content.end(), ContentSorter ());
        
-       switch (_playlist->video_from ()) {
-       case Playlist::VIDEO_NONE:
-               break;
-       case Playlist::VIDEO_FFMPEG:
-               if (_ffmpeg_decoder->seek_to_last ()) {
-                       r = true;
-               }
-               break;
-       case Playlist::VIDEO_IMAGEMAGICK:
-               if ((*_imagemagick_decoder)->seek_to_last ()) {
-                       r = true;
-               }
-               break;
+       ContentList::iterator previous = _content.end ();
+       ContentList::iterator i = _content.begin();
+       while (i != _content.end() && *i != c) {
+               previous = i;
+               ++i;
        }
 
-       /* XXX: don't seek audio because we don't need to... */
+       assert (i != _content.end ());
+       if (previous == _content.end ()) {
+               return;
+       }
 
-       return r;
+       
+       DCPTime const p = (*previous)->position ();
+       (*previous)->set_position (p + c->length_after_trim ());
+       c->set_position (p);
+       sort (_content.begin(), _content.end(), ContentSorter ());
 }
 
 void
-Player::setup_decoders ()
+Playlist::move_later (shared_ptr<Content> c)
 {
-       if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
-               _ffmpeg_decoder.reset (
-                       new FFmpegDecoder (
-                               _film,
-                               _playlist->ffmpeg(),
-                               _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
-                               _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
-                               _subtitles && _film->with_subtitles(),
-                               _video_sync
-                               )
-                       );
-       }
+       sort (_content.begin(), _content.end(), ContentSorter ());
        
-       if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
-               _ffmpeg_decoder->connect_video (shared_from_this ());
-       }
-
-       if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
-               _ffmpeg_decoder->connect_audio (shared_from_this ());
-       }
-
-       if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
-               list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
-               for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
-                       shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
-                       _imagemagick_decoders.push_back (d);
-                       d->connect_video (shared_from_this ());
-               }
-
-               _imagemagick_decoder = _imagemagick_decoders.begin ();
+       ContentList::iterator i = _content.begin();
+       while (i != _content.end() && *i != c) {
+               ++i;
        }
 
-       if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
-               list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
-               for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
-                       shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
-                       _sndfile_decoders.push_back (d);
-                       d->connect_audio (shared_from_this ());
-               }
-       }
-}
+       assert (i != _content.end ());
 
-void
-Player::disable_video_sync ()
-{
-       _video_sync = false;
-}
+       ContentList::iterator next = i;
+       ++next;
 
-double
-Player::last_video_time () const
-{
-       switch (_playlist->video_from ()) {
-       case Playlist::VIDEO_NONE:
-               return 0;
-       case Playlist::VIDEO_FFMPEG:
-               return _ffmpeg_decoder->last_source_time ();
-       case Playlist::VIDEO_IMAGEMAGICK:
-               return (*_imagemagick_decoder)->last_source_time ();
+       if (next == _content.end ()) {
+               return;
        }
 
-       return 0;
+       (*next)->set_position (c->position ());
+       c->set_position (c->position() + c->length_after_trim ());
+       sort (_content.begin(), _content.end(), ContentSorter ());
 }