X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2Fplaylist.cc;h=995067b66ee7bf7133f6d0b025c70f381869dd1e;hb=09a9ac376db005a40a351736bcff4077f098825d;hp=3c69ae15f4599b2ee08c2c87f59ade6ba6138f37;hpb=28dbf4fd074d2046a3c8ddebac9a537a80fd457a;p=dcpomatic.git diff --git a/src/lib/playlist.cc b/src/lib/playlist.cc index 3c69ae15f..995067b66 100644 --- a/src/lib/playlist.cc +++ b/src/lib/playlist.cc @@ -17,6 +17,7 @@ */ +#include #include #include #include "playlist.h" @@ -26,7 +27,12 @@ #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,257 +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 other) + : _loop (other->_loop) { - _audio_from = AUDIO_FFMPEG; - - _video.clear (); - _audio.clear (); - - for (list::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) { - i->disconnect (); + for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) { + _content.push_back ((*i)->clone ()); } - - _content_connections.clear (); - - for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) { - - /* Video is video */ - shared_ptr vc = dynamic_pointer_cast (*i); - if (vc) { - _video.push_back (vc); - } +} - /* FFmpegContent is audio if we are doing AUDIO_FFMPEG */ - shared_ptr fc = dynamic_pointer_cast (*i); - if (fc && _audio_from == AUDIO_FFMPEG) { - _audio.push_back (fc); - } +Playlist::~Playlist () +{ + _content.clear (); + reconnect (); +} - /* SndfileContent trumps FFmpegContent for audio */ - shared_ptr sc = dynamic_pointer_cast (*i); - if (sc) { - if (_audio_from == AUDIO_FFMPEG) { - /* This is our fist SndfileContent; clear any FFmpegContent and - say that we are using Sndfile. - */ - _audio.clear (); - _audio_from = AUDIO_SNDFILE; +void +Playlist::content_changed (weak_ptr c, int p) +{ + 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 (*i)) { + continue; } - - _audio.push_back (sc); + + (*i)->set_start (last); + last = (*i)->end (); } - _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2))); + _sequencing_video = false; } - - Changed (); + + ContentChanged (c, p); } -/** @return Length of our audio */ -ContentAudioFrame -Playlist::audio_length () const +string +Playlist::video_digest () const { - ContentAudioFrame len = 0; - - switch (_audio_from) { - case AUDIO_FFMPEG: - /* FFmpeg content is sequential */ - for (list >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) { - len += (*i)->audio_length (); + string t; + + for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) { + if (!dynamic_pointer_cast (*i)) { + continue; } - break; - case AUDIO_SNDFILE: - /* Sndfile content is simultaneous */ - for (list >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) { - len = max (len, (*i)->audio_length ()); + + t += (*i)->digest (); + shared_ptr fc = dynamic_pointer_cast (*i); + if (fc && fc->subtitle_stream()) { + t += fc->subtitle_stream()->id; } - break; } - return len; + t += lexical_cast (_loop); + + return md5_digest (t.c_str(), t.length()); } -/** @return number of audio channels */ -int -Playlist::audio_channels () const +/** @param node node */ +void +Playlist::set_from_xml (shared_ptr film, shared_ptr node) { - int channels = 0; - - switch (_audio_from) { - case AUDIO_FFMPEG: - /* FFmpeg audio is sequential, so use the maximum channel count */ - for (list >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) { - channels = max (channels, (*i)->audio_channels ()); - } - break; - case AUDIO_SNDFILE: - /* Sndfile audio is simultaneous, so it's the sum of the channel counts */ - for (list >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) { - channels += (*i)->audio_channels (); + list > c = node->node_children ("Content"); + for (list >::iterator i = c.begin(); i != c.end(); ++i) { + string const type = (*i)->string_child ("Type"); + + boost::shared_ptr 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; + + _content.push_back (content); } - return channels; + reconnect (); + _loop = node->number_child ("Loop"); + _sequence_video = node->bool_child ("SequenceVideo"); } -int -Playlist::audio_frame_rate () const +/** @param node node */ +void +Playlist::as_xml (xmlpp::Node* node) { - if (_audio.empty ()) { - return 0; + for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) { + (*i)->as_xml (node->add_child ("Content")); } - /* XXX: assuming that all content has the same rate */ - return _audio.front()->audio_frame_rate (); + node->add_child("Loop")->add_child_text(lexical_cast (_loop)); + node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0"); } -float -Playlist::video_frame_rate () const +void +Playlist::add (shared_ptr c) { - if (_video.empty ()) { - return 0; - } - - /* XXX: assuming all the same */ - return _video.front()->video_frame_rate (); + _content.push_back (c); + reconnect (); + Changed (); } -libdcp::Size -Playlist::video_size () const +void +Playlist::remove (shared_ptr c) { - if (_video.empty ()) { - return libdcp::Size (); + ContentList::iterator i = _content.begin (); + while (i != _content.end() && *i != c) { + ++i; + } + + if (i != _content.end ()) { + _content.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 >::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 { - return !_audio.empty (); + for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) { + shared_ptr fc = dynamic_pointer_cast (*i); + if (fc && !fc->subtitle_streams().empty()) { + return true; + } + } + + return false; } -void -Playlist::content_changed (weak_ptr c, int p) +class FrameRateCandidate { - ContentChanged (c, p); -} +public: + FrameRateCandidate (float source_, int dcp_) + : source (source_) + , dcp (dcp_) + {} -AudioMapping -Playlist::default_audio_mapping () const + float source; + int dcp; +}; + +int +Playlist::best_dcp_frame_rate () const { - AudioMapping m; - if (_audio.empty ()) { - return m; + list 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 candidates; + + /* Start with the ones without skip / repeat so they will get matched in preference to skipped/repeated ones */ + for (list::const_iterator i = allowed_dcp_frame_rates.begin(); i != allowed_dcp_frame_rates.end(); ++i) { + candidates.push_back (FrameRateCandidate (*i, *i)); } - switch (_audio_from) { - case AUDIO_FFMPEG: - { - /* XXX: assumes all the same */ - if (_audio.front()->audio_channels() == 1) { - /* Map mono sources to centre */ - m.add (AudioMapping::Channel (_audio.front(), 0), libdcp::CENTRE); - } else { - int const N = min (_audio.front()->audio_channels (), MAX_AUDIO_CHANNELS); - /* Otherwise just start with a 1:1 mapping */ - for (int i = 0; i < N; ++i) { - m.add (AudioMapping::Channel (_audio.front(), i), (libdcp::Channel) i); - } - } - break; + /* Then the skip/repeat ones */ + for (list::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)); } - case AUDIO_SNDFILE: - { - int n = 0; - for (list >::const_iterator i = _audio.begin(); i != _audio.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; + /* Pick the best one, bailing early if we hit an exact match */ + float error = std::numeric_limits::max (); + optional best; + list::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 vc = dynamic_pointer_cast (*j); + if (!vc) { + continue; } + + this_error += fabs (i->source - vc->video_frame_rate ()); } - break; - } + + if (this_error < error) { + error = this_error; + best = *i; + } + + ++i; } - return m; + if (!best) { + return 24; + } + + return best->dcp; } -string -Playlist::audio_digest () const +Time +Playlist::length () const { - string t; - - for (list >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) { - t += (*i)->digest (); + Time len = 0; + for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) { + len = max (len, (*i)->end ()); + } - shared_ptr fc = dynamic_pointer_cast (*i); - if (fc) { - t += lexical_cast (fc->audio_stream()->id); - } + return len; +} + +void +Playlist::reconnect () +{ + for (list::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 >::const_iterator i = _video.begin(); i != _video.end(); ++i) { - t += (*i)->digest (); - shared_ptr fc = dynamic_pointer_cast (*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 (*i)) { + end = max (end, (*i)->end ()); } } - return md5_digest (t.c_str(), t.length()); + return end; } -ContentVideoFrame -Playlist::content_length () const +void +Playlist::set_sequence_video (bool s) { - float const vfr = video_frame_rate() > 0 ? video_frame_rate() : 24; - int const afr = audio_frame_rate() > 0 ? audio_frame_rate() : 48000; + _sequence_video = s; +} - return max ( - video_length(), - ContentVideoFrame (audio_length() * vfr / afr) - ); +bool +ContentSorter::operator() (shared_ptr a, shared_ptr b) +{ + return a->start() < b->start(); }