*/
+#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 (shared_ptr<const Film> f, list<shared_ptr<Content> > c)
- : _video_from (VIDEO_NONE)
- , _audio_from (AUDIO_NONE)
- , _ffmpeg_decoder_done (false)
+Playlist::Playlist ()
+ : _loop (1)
+ , _sequence_video (true)
+ , _sequencing_video (false)
{
- for (list<shared_ptr<Content> >::const_iterator i = c.begin(); i != c.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;
- }
- }
- if (_video_from == VIDEO_FFMPEG || _audio_from == AUDIO_FFMPEG) {
- DecodeOptions o;
- /* XXX: decodeoptions */
- _ffmpeg_decoder.reset (new FFmpegDecoder (f, _ffmpeg, o));
- }
-
- if (_video_from == VIDEO_FFMPEG) {
- _ffmpeg_decoder->connect_video (shared_from_this ());
- }
+}
- if (_audio_from == AUDIO_FFMPEG) {
- _ffmpeg_decoder->connect_audio (shared_from_this ());
+Playlist::Playlist (shared_ptr<const Playlist> other)
+ : _loop (other->_loop)
+{
+ for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
+ _content.push_back ((*i)->clone ());
}
+}
- if (_video_from == VIDEO_IMAGEMAGICK) {
- for (list<shared_ptr<ImageMagickContent> >::iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
- DecodeOptions o;
- /* XXX: decodeoptions */
- shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (f, *i, o));
- _imagemagick_decoders.push_back (d);
- d->connect_video (shared_from_this ());
- }
+Playlist::~Playlist ()
+{
+ _content.clear ();
+ reconnect ();
+}
- _imagemagick_decoder = _imagemagick_decoders.begin ();
- }
+void
+Playlist::content_changed (weak_ptr<Content> c, int p)
+{
+ if (p == ContentProperty::LENGTH && _sequence_video && !_sequencing_video) {
+ cout << "sequencing.\n";
+ _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;
+ }
- if (_audio_from == AUDIO_SNDFILE) {
- for (list<shared_ptr<SndfileContent> >::iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
- DecodeOptions o;
- /* XXX: decodeoptions */
- shared_ptr<SndfileDecoder> d (new SndfileDecoder (f, *i, o));
- _sndfile_decoders.push_back (d);
- d->connect_audio (shared_from_this ());
+ (*i)->set_start (last);
+ cout << (*i)->file() << " -> " << last << "\n";
+ last = (*i)->end ();
}
+
+ _sequencing_video = false;
}
+
+ ContentChanged (c, p);
}
-ContentAudioFrame
-Playlist::audio_length () const
+string
+Playlist::audio_digest () 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<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) {
+ if (!dynamic_pointer_cast<const AudioContent> (*i)) {
+ continue;
+ }
+
+ t += (*i)->digest ();
+
+ shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
+ if (fc) {
+ t += lexical_cast<string> (fc->audio_stream()->id);
}
- return l;
- }
}
- return 0;
+ t += lexical_cast<string> (_loop);
+
+ return md5_digest (t.c_str(), t.length());
}
-int
-Playlist::audio_channels () const
+string
+Playlist::video_digest () const
{
- 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<SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
- c += (*i)->audio_channels ();
+ 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 c;
- }
}
- return 0;
+ 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)
{
- 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 ();
+ 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));
+ }
+
+ _content.push_back (content);
}
- return 0;
+ reconnect ();
+ _loop = node->number_child<int> ("Loop");
+ _sequence_video = node->bool_child ("SequenceVideo");
}
-int64_t
-Playlist::audio_channel_layout () 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_channel_layout ();
- case AUDIO_SNDFILE:
- /* XXX */
- return 0;
+ for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+ (*i)->as_xml (node->add_child ("Content"));
}
- return 0;
+ node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
+ node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
}
-float
-Playlist::video_frame_rate () const
+void
+Playlist::add (shared_ptr<Content> c)
{
- switch (_video_from) {
- case VIDEO_NONE:
- return 0;
- case VIDEO_FFMPEG:
- return _ffmpeg->video_frame_rate ();
- case VIDEO_IMAGEMAGICK:
- return 24;
- }
-
- return 0;
+ _content.push_back (c);
+ reconnect ();
+ Changed ();
}
-libdcp::Size
-Playlist::video_size () const
+void
+Playlist::remove (shared_ptr<Content> c)
{
- 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 ();
+ ContentList::iterator i = _content.begin ();
+ while (i != _content.end() && *i != c) {
+ ++i;
+ }
+
+ if (i != _content.end ()) {
+ _content.erase (i);
+ Changed ();
}
-
- return libdcp::Size ();
}
-bool
-Playlist::has_audio () const
-{
- return _audio_from != AUDIO_NONE;
-}
-
void
-Playlist::disable_video ()
+Playlist::set_loop (int l)
{
- _video_from = VIDEO_NONE;
+ _loop = l;
+ Changed ();
}
bool
-Playlist::pass ()
+Playlist::has_subtitles () const
{
- bool done = true;
-
- if (_video_from == VIDEO_FFMPEG || _audio_from == AUDIO_FFMPEG) {
- if (!_ffmpeg_decoder_done) {
- if (_ffmpeg_decoder->pass ()) {
- _ffmpeg_decoder_done = true;
- } else {
- 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;
}
}
- if (_video_from == VIDEO_IMAGEMAGICK) {
- if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- if ((*_imagemagick_decoder)->pass ()) {
- _imagemagick_decoder++;
- }
+ return false;
+}
+
+class FrameRateCandidate
+{
+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));
+ }
- if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- done = false;
+ /* 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 ());
}
+
+ if (this_error < error) {
+ error = this_error;
+ best = *i;
+ }
+
+ ++i;
}
- /* XXX: sndfile */
+ if (!best) {
+ return 24;
+ }
+
+ return best->dcp;
+}
+
+Time
+Playlist::length () const
+{
+ Time len = 0;
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ len = max (len, (*i)->end ());
+ }
- return done;
+ return len;
}
void
-Playlist::set_progress (shared_ptr<Job> job)
+Playlist::reconnect ()
{
- /* XXX */
+ 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)));
+ }
}
-void
-Playlist::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
+Time
+Playlist::video_end () const
{
- Video (i, same, s);
+ 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 end;
}
void
-Playlist::process_audio (shared_ptr<AudioBuffers> b)
+Playlist::set_sequence_video (bool s)
{
- Audio (b);
+ _sequence_video = s;
}
+bool
+ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
+{
+ return a->start() < b->start();
+}