X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fplaylist.cc;h=4e46fdbdffb885778b99e373cd5d38a679c81e73;hb=c2a97b6f83a32483817d02194a1e7c8d6828b0bb;hp=216244bd0ef309fd470de5c25744136b06b2ecd3;hpb=d487ee606113a2b4bbab1810758e9777792f1cac;p=dcpomatic.git diff --git a/src/lib/playlist.cc b/src/lib/playlist.cc index 216244bd0..4e46fdbdf 100644 --- a/src/lib/playlist.cc +++ b/src/lib/playlist.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2015 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,16 +19,20 @@ #include #include -#include #include "playlist.h" #include "sndfile_content.h" #include "sndfile_decoder.h" #include "video_content.h" #include "ffmpeg_decoder.h" #include "ffmpeg_content.h" -#include "imagemagick_decoder.h" -#include "imagemagick_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; @@ -36,379 +40,376 @@ using std::vector; using std::min; using std::max; using std::string; +using std::pair; +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) { } -Playlist::Playlist (shared_ptr other) - : _audio_from (other->_audio_from) - , _loop (other->_loop) +Playlist::~Playlist () { - for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) { - _content.push_back ((*i)->clone ()); - } - - setup (); + _content.clear (); + reconnect (); } void -Playlist::setup () +Playlist::content_changed (weak_ptr content, int property, bool frequent) { - _audio_from = AUDIO_FFMPEG; + /* Don't respond to position changes here, as: + - sequencing after earlier/later changes is handled by move_earlier/move_later + - any other position changes will be timeline drags which should not result in content + being sequenced. + */ + + if (property == ContentProperty::LENGTH || property == VideoContentProperty::VIDEO_FRAME_TYPE) { + maybe_sequence_video (); + } - _video.clear (); - _audio.clear (); + ContentChanged (content, property, frequent); +} - for (list::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) { - i->disconnect (); +void +Playlist::maybe_sequence_video () +{ + if (!_sequence_video || _sequencing_video) { + return; } - - _content_connections.clear (); - for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) { + _sequencing_video = true; - /* Video is video */ + DCPTime next_left; + DCPTime next_right; + for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) { 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); + if (!vc) { + continue; } - /* 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; - } - - _audio.push_back (sc); + 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 (); } - - _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2))); } - Changed (); + /* This won't change order, so it does not need a sort */ + + _sequencing_video = false; } -/** @return Length of our audio */ -ContentAudioFrame -Playlist::audio_length () const +string +Playlist::video_identifier () const { - ContentAudioFrame len = 0; + string t; - 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 (); - } - 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 ()); + for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) { + shared_ptr vc = dynamic_pointer_cast (*i); + shared_ptr sc = dynamic_pointer_cast (*i); + if (vc) { + t += vc->identifier (); + } else if (sc && sc->burn_subtitles ()) { + t += sc->identifier (); } - break; } - return len * _loop; + MD5Digester digester; + digester.add (t.c_str(), t.length()); + return digester.get (); } -/** @return number of audio channels */ -int -Playlist::audio_channels () const +/** @param node node */ +void +Playlist::set_from_xml (shared_ptr film, cxml::ConstNodePtr node, int version, list& notes) { - 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 (); - } - break; + list c = node->node_children ("Content"); + for (list::iterator i = c.begin(); i != c.end(); ++i) { + _content.push_back (content_factory (film, *i, version, notes)); } - return channels; + sort (_content.begin(), _content.end(), ContentSorter ()); + + reconnect (); } -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 (); } -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); + sort (_content.begin(), _content.end(), ContentSorter ()); + 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 (); + /* This won't change order, so it does not need a sort */ } -ContentVideoFrame -Playlist::video_length () const +void +Playlist::remove (ContentList c) { - ContentVideoFrame len = 0; - for (list >::const_iterator i = _video.begin(); i != _video.end(); ++i) { - len += (*i)->video_length (); + 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 len * _loop; -} -bool -Playlist::has_audio () const -{ - return !_audio.empty (); + /* This won't change order, so it does not need a sort */ + + Changed (); } -void -Playlist::content_changed (weak_ptr c, int p) +class FrameRateCandidate { - ContentChanged (c, p); -} +public: + FrameRateCandidate (float source_, int dcp_) + : source (source_) + , dcp (dcp_) + {} + + float source; + int dcp; +}; -AudioMapping -Playlist::default_audio_mapping () const +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 */ + 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; } + + /* Best error for this content; we could use the content as-is or double its rate */ + float best_error = min ( + float (fabs (i->source - vc->video_frame_rate ())), + float (fabs (i->source - vc->video_frame_rate () * 2)) + ); + + /* Use the largest difference between DCP and source as the "error" */ + this_error = max (this_error, best_error); + } + + if (this_error < error) { + error = this_error; + best = *i; } - break; + + ++i; } + + if (!best) { + return 24; } - return m; + return best->dcp; } -string -Playlist::audio_digest () const +DCPTime +Playlist::length () const { - string t; - - for (list >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) { - t += (*i)->digest (); - - shared_ptr fc = dynamic_pointer_cast (*i); - if (fc) { - t += lexical_cast (fc->audio_stream()->id); - } + DCPTime len; + for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) { + len = max (len, (*i)->end()); } - t += lexical_cast (_loop); - - return md5_digest (t.c_str(), t.length()); + return len; } -string -Playlist::video_digest () const +void +Playlist::reconnect () { - 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; - } + for (list::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) { + i->disconnect (); } - t += lexical_cast (_loop); + _content_connections.clear (); - return md5_digest (t.c_str(), t.length()); + 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))); + } } -ContentVideoFrame -Playlist::content_length () const +DCPTime +Playlist::video_end () const { - float const vfr = video_frame_rate() > 0 ? video_frame_rate() : 24; - int const afr = audio_frame_rate() > 0 ? audio_frame_rate() : 48000; + DCPTime end; + for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) { + if (dynamic_pointer_cast (*i)) { + end = max (end, (*i)->end ()); + } + } - return max ( - video_length(), - ContentVideoFrame (audio_length() * vfr / afr) - ); + return end; } -void -Playlist::set_from_xml (shared_ptr node) +FrameRateChange +Playlist::active_frame_rate_change (DCPTime t, int dcp_video_frame_rate) const { - 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 c; - - if (type == "FFmpeg") { - c.reset (new FFmpegContent (*i)); - } else if (type == "ImageMagick") { - c.reset (new ImageMagickContent (*i)); - } else if (type == "Sndfile") { - c.reset (new SndfileContent (*i)); + for (ContentList::const_reverse_iterator i = _content.rbegin(); i != _content.rend(); ++i) { + shared_ptr vc = dynamic_pointer_cast (*i); + if (!vc) { + continue; } - _content.push_back (c); + if (vc->position() <= t) { + /* This is the first piece of content (going backwards...) that starts before t, + so it's the active one. + */ + return FrameRateChange (vc->video_frame_rate(), dcp_video_frame_rate); + } } - _loop = node->number_child ("Loop"); + return FrameRateChange (dcp_video_frame_rate, dcp_video_frame_rate); } void -Playlist::as_xml (xmlpp::Node* node) +Playlist::set_sequence_video (bool s) { - for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) { - (*i)->as_xml (node->add_child ("Content")); - } + _sequence_video = s; +} - node->add_child("Loop")->add_child_text(lexical_cast (_loop)); +bool +ContentSorter::operator() (shared_ptr a, shared_ptr b) +{ + return a->position() < b->position(); } -void -Playlist::add (shared_ptr c) +/** @return content in an undefined order */ +ContentList +Playlist::content () const { - _content.push_back (c); - setup (); + return _content; } void -Playlist::remove (shared_ptr c) +Playlist::repeat (ContentList c, int n) { - ContentList::iterator i = find (_content.begin(), _content.end(), c); - if (i != _content.end ()) { - _content.erase (i); + pair 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 ()); + } + + DCPTime pos = range.second; + for (int i = 0; i < n; ++i) { + for (ContentList::iterator i = c.begin(); i != c.end(); ++i) { + shared_ptr copy = (*i)->clone (); + copy->set_position (pos + copy->position() - range.first); + _content.push_back (copy); + } + pos += range.second - range.first; } - setup (); + sort (_content.begin(), _content.end(), ContentSorter ()); + + reconnect (); + Changed (); } void Playlist::move_earlier (shared_ptr c) { - ContentList::iterator i = find (_content.begin(), _content.end(), c); - if (i == _content.begin () || i == _content.end()) { - return; + sort (_content.begin(), _content.end(), ContentSorter ()); + + ContentList::iterator previous = _content.end (); + ContentList::iterator i = _content.begin(); + while (i != _content.end() && *i != c) { + previous = i; + ++i; } - ContentList::iterator j = i; - --j; + DCPOMATIC_ASSERT (i != _content.end ()); + if (previous == _content.end ()) { + return; + } - swap (*i, *j); - setup (); + DCPTime const p = (*previous)->position (); + (*previous)->set_position (p + c->length_after_trim ()); + c->set_position (p); + sort (_content.begin(), _content.end(), ContentSorter ()); } void Playlist::move_later (shared_ptr c) { - ContentList::iterator i = find (_content.begin(), _content.end(), c); - if (i == _content.end()) { - return; - } + sort (_content.begin(), _content.end(), ContentSorter ()); - ContentList::iterator j = i; - ++j; - if (j == _content.end ()) { - return; + ContentList::iterator i = _content.begin(); + while (i != _content.end() && *i != c) { + ++i; } - swap (*i, *j); + DCPOMATIC_ASSERT (i != _content.end ()); - setup (); -} + ContentList::iterator next = i; + ++next; -void -Playlist::set_loop (int l) -{ - _loop = l; - Changed (); -} - -shared_ptr -Playlist::ffmpeg () const -{ - for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) { - shared_ptr fc = dynamic_pointer_cast (*i); - if (fc) { - return fc; - } + if (next == _content.end ()) { + return; } - return shared_ptr (); + (*next)->set_position (c->position ()); + c->set_position (c->position() + (*next)->length_after_trim ()); + sort (_content.begin(), _content.end(), ContentSorter ()); }