*/
+#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)
-{
-
-}
-
-void
-Playlist::setup (ContentList content)
+ : _loop (1)
+ , _sequence_video (true)
+ , _sequencing_video (false)
{
- _video_from = VIDEO_NONE;
- _audio_from = AUDIO_NONE;
-
- _ffmpeg.reset ();
- _imagemagick.clear ();
- _sndfile.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;
- }
- }
-
- 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;
- }
- }
}
-ContentAudioFrame
-Playlist::audio_length () const
+Playlist::Playlist (shared_ptr<const Playlist> other)
+ : _loop (other->_loop)
{
- 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 ();
- }
- return l;
+ for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
+ _content.push_back ((*i)->clone ());
}
- }
-
- return 0;
}
-int
-Playlist::audio_channels () const
+Playlist::~Playlist ()
{
- 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;
- }
- }
-
- return 0;
+ _content.clear ();
+ reconnect ();
}
-int
-Playlist::audio_frame_rate () const
+void
+Playlist::content_changed (weak_ptr<Content> c, int p)
{
- 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 ();
- }
+ 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;
+ }
- return 0;
-}
+ (*i)->set_start (last);
+ last = (*i)->end ();
+ }
-int64_t
-Playlist::audio_channel_layout () const
-{
- switch (_audio_from) {
- case AUDIO_NONE:
- return 0;
- case AUDIO_FFMPEG:
- return _ffmpeg->audio_channel_layout ();
- case AUDIO_SNDFILE:
- /* XXX */
- return 0;
+ _sequencing_video = false;
}
-
- return 0;
+
+ ContentChanged (c, p);
}
-float
-Playlist::video_frame_rate () const
+string
+Playlist::video_digest () const
{
- switch (_video_from) {
- case VIDEO_NONE:
- return 0;
- case VIDEO_FFMPEG:
- return _ffmpeg->video_frame_rate ();
- case VIDEO_IMAGEMAGICK:
- return 24;
+ string t;
+
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ if (!dynamic_pointer_cast<const VideoContent> (*i)) {
+ continue;
+ }
+
+ t += (*i)->digest ();
+ shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
+ if (fc && fc->subtitle_stream()) {
+ t += fc->subtitle_stream()->id;
+ }
}
- return 0;
-}
-
-libdcp::Size
-Playlist::video_size () const
-{
- 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 ();
- }
+ t += lexical_cast<string> (_loop);
- return libdcp::Size ();
+ return md5_digest (t.c_str(), t.length());
}
-ContentVideoFrame
-Playlist::video_length () const
+/** @param node <Playlist> node */
+void
+Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
{
- 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 ();
+ 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));
}
- return l;
- }
- }
- return 0;
-}
+ _content.push_back (content);
+ }
-bool
-Playlist::has_audio () const
-{
- return _audio_from != AUDIO_NONE;
+ reconnect ();
+ _loop = node->number_child<int> ("Loop");
+ _sequence_video = node->bool_child ("SequenceVideo");
}
-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)
+/** @param node <Playlist> node */
+void
+Playlist::as_xml (xmlpp::Node* node)
{
+ for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+ (*i)->as_xml (node->add_child ("Content"));
+ }
+ node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
+ node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
}
void
-Player::disable_video ()
+Playlist::add (shared_ptr<Content> c)
{
- _video = false;
+ _content.push_back (c);
+ reconnect ();
+ Changed ();
}
void
-Player::disable_audio ()
+Playlist::remove (shared_ptr<Content> c)
{
- _audio = false;
+ ContentList::iterator i = _content.begin ();
+ while (i != _content.end() && *i != c) {
+ ++i;
+ }
+
+ if (i != _content.end ()) {
+ _content.erase (i);
+ Changed ();
+ }
}
void
-Player::disable_subtitles ()
+Playlist::set_loop (int l)
{
- _subtitles = false;
+ _loop = l;
+ Changed ();
}
bool
-Player::pass ()
+Playlist::has_subtitles () 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;
- }
- }
- }
-
- if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
- if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- if ((*_imagemagick_decoder)->pass ()) {
- _imagemagick_decoder++;
- }
-
- if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- done = false;
- }
+ 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;
}
}
- /* XXX: sndfile */
-
- return done;
+ return false;
}
-void
-Player::set_progress (shared_ptr<Job> job)
+class FrameRateCandidate
{
- /* Assume progress can be divined from how far through the video we are */
- switch (_playlist->video_from ()) {
- case Playlist::VIDEO_NONE:
- break;
- case Playlist::VIDEO_FFMPEG:
- if (_playlist->video_length ()) {
- job->set_progress (float(_ffmpeg_decoder->video_frame()) / _playlist->video_length ());
- }
- break;
- case Playlist::VIDEO_IMAGEMAGICK:
- {
- int n = 0;
- for (std::list<boost::shared_ptr<ImageMagickDecoder> >::iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoders.end(); ++i) {
- if (_imagemagick_decoder == i) {
- job->set_progress (float (n) / _imagemagick_decoders.size ());
- }
- ++n;
- }
- break;
- }
- }
-}
+public:
+ FrameRateCandidate (float source_, int dcp_)
+ : source (source_)
+ , dcp (dcp_)
+ {}
-void
-Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
-{
- Video (i, same, s);
-}
+ float source;
+ int dcp;
+};
-void
-Player::process_audio (shared_ptr<AudioBuffers> b)
+int
+Playlist::best_dcp_frame_rate () const
{
- Audio (b);
-}
+ list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
-/** @return true on error */
-bool
-Player::seek (double t)
-{
- if (!_have_setup_decoders) {
- setup_decoders ();
- _have_setup_decoders = true;
+ /* 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));
}
-
- bool r = false;
-
- switch (_playlist->video_from()) {
- case Playlist::VIDEO_NONE:
- break;
- case Playlist::VIDEO_FFMPEG:
- if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
- r = true;
- }
- /* We're seeking, so all `all done' bets are off */
- _ffmpeg_decoder_done = false;
- 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;
- }
- if (_imagemagick_decoder != _imagemagick_decoders.end()) {
- (*_imagemagick_decoder)->seek (t);
- } else {
- r = true;
- }
- 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));
}
- /* XXX: don't seek audio because we don't need to... */
+ /* 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 = 0;
+ for (ContentList::const_iterator j = _content.begin(); j != _content.end(); ++j) {
+ shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*j);
+ if (!vc) {
+ continue;
+ }
- return r;
-}
+ this_error += fabs (i->source - vc->video_frame_rate ());
+ }
-bool
-Player::seek_to_last ()
-{
- if (!_have_setup_decoders) {
- setup_decoders ();
- _have_setup_decoders = true;
+ if (this_error < error) {
+ error = this_error;
+ best = *i;
+ }
+
+ ++i;
}
- bool r = false;
+ if (!best) {
+ return 24;
+ }
- switch (_playlist->video_from ()) {
- case Playlist::VIDEO_NONE:
- break;
- case Playlist::VIDEO_FFMPEG:
- if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
- r = true;
- }
+ return best->dcp;
+}
- /* We're seeking, so all `all done' bets are off */
- _ffmpeg_decoder_done = false;
- break;
- case Playlist::VIDEO_IMAGEMAGICK:
- if ((*_imagemagick_decoder)->seek_to_last ()) {
- r = true;
- }
- break;
+Time
+Playlist::length () const
+{
+ Time len = 0;
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ len = max (len, (*i)->end ());
}
- /* XXX: don't seek audio because we don't need to... */
-
- return r;
+ return len;
}
void
-Player::setup_decoders ()
+Playlist::reconnect ()
{
- 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
- )
- );
- }
-
- if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
- _ffmpeg_decoder->connect_video (shared_from_this ());
+ for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
+ i->disconnect ();
}
- if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
- _ffmpeg_decoder->connect_audio (shared_from_this ());
+ _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)));
}
+}
- 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 ());
+Time
+Playlist::video_end () const
+{
+ 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 ());
}
-
- _imagemagick_decoder = _imagemagick_decoders.begin ();
}
- 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 ());
- }
- }
+ return end;
}
void
-Player::disable_video_sync ()
+Playlist::set_sequence_video (bool s)
{
- _video_sync = false;
+ _sequence_video = s;
}
-double
-Player::last_video_time () const
+bool
+ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
{
- 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 ();
- }
-
- return 0;
+ return a->start() < b->start();
}