Various fixes to make audio analysis sort-of work.
[dcpomatic.git] / src / lib / playlist.cc
index 7fe4fb2a5496c00cb83562eb606f426461a0b011..9cc1789d135d5c38a66e416b4c90888421d7142e 100644 (file)
@@ -17,6 +17,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,264 +40,269 @@ 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)
+       , _sequence_video (true)
+       , _sequencing_video (false)
 {
 
 }
 
-void
-Playlist::setup (ContentList content)
+Playlist::Playlist (shared_ptr<const Playlist> other)
+       : _loop (other->_loop)
 {
-       _audio_from = AUDIO_FFMPEG;
-
-       _video.clear ();
-       _sndfile.clear ();
-
-       for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
-               i->disconnect ();
-       }
-       
-       _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);
-               }
-               
-               shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*i);
-               if (sc) {
-                       _sndfile.push_back (sc);
-                       _audio_from = AUDIO_SNDFILE;
-               }
-
-               _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
+       for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
+               _content.push_back ((*i)->clone ());
        }
+}
 
-       Changed ();
+Playlist::~Playlist ()
+{
+       _content.clear ();
+       reconnect ();
 }
 
-ContentAudioFrame
-Playlist::audio_length () const
+void
+Playlist::content_changed (weak_ptr<Content> c, int p)
 {
-       ContentAudioFrame len = 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) {
-                               len += fc->audio_length ();
+       if (p == ContentProperty::LENGTH && _sequence_video && !_sequencing_video) {
+               _sequencing_video = true;
+
+               ContentList cl = _content;
+               sort (cl.begin(), cl.end(), ContentSorter ());
+               Time last = 0;
+               for (ContentList::iterator i = cl.begin(); i != cl.end(); ++i) {
+                       if (!dynamic_pointer_cast<VideoContent> (*i)) {
+                               continue;
                        }
+
+                       (*i)->set_start (last);
+                       last = (*i)->end ();
                }
-               break;
-       case AUDIO_SNDFILE:
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       len += (*i)->audio_length ();
-               }
-               break;
-       }
 
-       return len;
+               _sequencing_video = false;
+       }
+       
+       ContentChanged (c, p);
 }
 
-int
-Playlist::audio_channels () const
+string
+Playlist::video_digest () const
 {
-       int channels = 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) {
-                               channels = max (channels, fc->audio_channels ());
-                       }
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               if (!dynamic_pointer_cast<const VideoContent> (*i)) {
+                       continue;
                }
-               break;
-       case AUDIO_SNDFILE:
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       channels += (*i)->audio_channels ();
+               
+               t += (*i)->digest ();
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
+               if (fc && fc->subtitle_stream()) {
+                       t += fc->subtitle_stream()->id;
                }
-               break;
        }
 
-       return channels;
+       t += lexical_cast<string> (_loop);
+
+       return md5_digest (t.c_str(), t.length());
 }
 
-int
-Playlist::audio_frame_rate () const
+/** @param node <Playlist> node */
+void
+Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::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 ();
+       list<shared_ptr<cxml::Node> > c = node->node_children ("Content");
+       for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
+               string const type = (*i)->string_child ("Type");
+
+               boost::shared_ptr<Content> content;
+
+               if (type == "FFmpeg") {
+                       content.reset (new FFmpegContent (film, *i));
+               } else if (type == "ImageMagick") {
+                       content.reset (new ImageMagickContent (film, *i));
+               } else if (type == "Sndfile") {
+                       content.reset (new SndfileContent (film, *i));
                }
-               break;
-       }
-       case AUDIO_SNDFILE:
-               return _sndfile.front()->audio_frame_rate ();
+
+               _content.push_back (content);
        }
 
-       return 0;
+       reconnect ();
+       _loop = node->number_child<int> ("Loop");
+       _sequence_video = node->bool_child ("SequenceVideo");
 }
 
-float
-Playlist::video_frame_rate () const
+/** @param node <Playlist> node */
+void
+Playlist::as_xml (xmlpp::Node* node)
 {
-       if (_video.empty ()) {
-               return 0;
+       for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+               (*i)->as_xml (node->add_child ("Content"));
        }
-       
-       /* XXX: assuming all the same */
-       return _video.front()->video_frame_rate ();
+
+       node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
+       node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
 }
 
-libdcp::Size
-Playlist::video_size () const
+void
+Playlist::add (shared_ptr<Content> c)
 {
-       if (_video.empty ()) {
-               return libdcp::Size ();
-       }
-
-       /* XXX: assuming all the same */
-       return _video.front()->video_size ();
+       _content.push_back (c);
+       reconnect ();
+       Changed ();
 }
 
-ContentVideoFrame
-Playlist::video_length () const
+void
+Playlist::remove (shared_ptr<Content> c)
 {
-       ContentVideoFrame len = 0;
-       for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
-               len += (*i)->video_length ();
+       ContentList::iterator i = _content.begin ();
+       while (i != _content.end() && *i != c) {
+               ++i;
        }
        
-       return len;
-}
-
-bool
-Playlist::has_audio () const
-{
-       /* XXX */
-       return true;
+       if (i != _content.end ()) {
+               _content.erase (i);
+               Changed ();
+       }
 }
 
 void
-Playlist::content_changed (weak_ptr<Content> c, int p)
+Playlist::set_loop (int l)
 {
-       ContentChanged (c, p);
+       _loop = l;
+       Changed ();
 }
 
-shared_ptr<const FFmpegContent>
-Playlist::first_ffmpeg () const
+bool
+Playlist::has_subtitles () const
 {
-       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;
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
+               if (fc && !fc->subtitle_streams().empty()) {
+                       return true;
                }
        }
 
-       return shared_ptr<const FFmpegContent> ();
+       return false;
 }
-       
 
-AudioMapping
-Playlist::default_audio_mapping () const
+class FrameRateCandidate
 {
-       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;
+public:
+       FrameRateCandidate (float source_, int dcp_)
+               : source (source_)
+               , dcp (dcp_)
+       {}
+
+       float source;
+       int dcp;
+};
+
+int
+Playlist::best_dcp_frame_rate () const
+{
+       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));
        }
 
-       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;
+       /* 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 (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
+                       shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
+                       if (!vc) {
+                               continue;
                        }
+
+                       this_error += fabs (i->source - vc->video_frame_rate ());
                }
-               break;
+
+               if (this_error < error) {
+                       error = this_error;
+                       best = *i;
+               }
+
+               ++i;
        }
+
+       if (!best) {
+               return 24;
        }
+       
+       return best->dcp;
+}
 
-       return m;
+Time
+Playlist::length () const
+{
+       Time len = 0;
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               len = max (len, (*i)->end ());
+       }
+
+       return len;
 }
 
-string
-Playlist::audio_digest () const
+void
+Playlist::reconnect ()
 {
-       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);
-                       }
-               }
-               break;
-       case AUDIO_SNDFILE:
-               for (list<shared_ptr<const SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
-                       t += (*i)->digest ();
-               }
-               break;
+       for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
+               i->disconnect ();
        }
 
-       return md5_digest (t.c_str(), t.length());
+       _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)));
+       }
 }
 
-string
-Playlist::video_digest () const
+Time
+Playlist::video_end () 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 end = 0;
+       for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+               if (dynamic_pointer_cast<const VideoContent> (*i)) {
+                       end = max (end, (*i)->end ());
                }
        }
 
-       return md5_digest (t.c_str(), t.length());
+       return end;
+}
+
+void
+Playlist::set_sequence_video (bool s)
+{
+       _sequence_video = s;
+}
+
+bool
+ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
+{
+       return a->start() < b->start();
 }