+/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
+
/*
Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
*/
+#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)
- : _film (f)
- , _video_from (VIDEO_NONE)
- , _audio_from (AUDIO_NONE)
- , _have_setup_decoders (false)
- , _ffmpeg_decoder_done (false)
- , _video_sync (true)
+Playlist::Playlist ()
+ : _loop (1)
{
- 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;
- }
- }
}
-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<SndfileContent> >::const_iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
- l += (*i)->audio_length ();
- }
- return l;
+ for (RegionList::const_iterator i = other->_regions.begin(); i != other->_regions.end(); ++i) {
+ _regions.push_back (Region (i->content->clone(), i->time, this));
}
- }
-
- return 0;
}
-int
-Playlist::audio_channels () 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_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 ();
- }
- return c;
- }
- }
-
- return 0;
+ ContentChanged (c, p);
}
-int
-Playlist::audio_frame_rate () const
+string
+Playlist::audio_digest () const
{
- 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 ();
- }
-
- return 0;
-}
+ string t;
+
+ for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+ if (!dynamic_pointer_cast<const AudioContent> (i->content)) {
+ continue;
+ }
+
+ t += i->content->digest ();
-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;
+ shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
+ if (fc) {
+ t += lexical_cast<string> (fc->audio_stream()->id);
+ }
}
- return 0;
+ t += lexical_cast<string> (_loop);
+
+ return md5_digest (t.c_str(), t.length());
}
-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 (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+ if (!dynamic_pointer_cast<const VideoContent> (i->content)) {
+ continue;
+ }
+
+ t += i->content->digest ();
+ shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (i->content);
+ if (fc && fc->subtitle_stream()) {
+ t += fc->subtitle_stream()->id;
+ }
}
- return 0;
+ t += lexical_cast<string> (_loop);
+
+ return md5_digest (t.c_str(), t.length());
}
-libdcp::Size
-Playlist::video_size () const
+void
+Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
{
- 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 ();
+ list<shared_ptr<cxml::Node> > c = node->node_children ("Region");
+ for (list<shared_ptr<cxml::Node> >::iterator i = c.begin(); i != c.end(); ++i) {
+ _regions.push_back (Region (*i, this));
}
- return libdcp::Size ();
+ _loop = node->number_child<int> ("Loop");
}
-ContentVideoFrame
-Playlist::video_length () const
+void
+Playlist::as_xml (xmlpp::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<ImageMagickContent> >::const_iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
- l += (*i)->video_length ();
- }
- return l;
- }
+ for (RegionList::iterator i = _regions.begin(); i != _regions.end(); ++i) {
+ i->as_xml (node->add_child ("Region"));
}
- return 0;
+ node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
}
-bool
-Playlist::has_audio () const
-{
- return _audio_from != AUDIO_NONE;
-}
-
void
-Playlist::disable_video ()
+Playlist::add (shared_ptr<Content> c)
{
- _video_from = VIDEO_NONE;
+ _regions.push_back (Region (c, 0, this));
+ Changed ();
}
void
-Playlist::disable_audio ()
+Playlist::remove (shared_ptr<Content> c)
{
- _audio_from = AUDIO_NONE;
+ RegionList::iterator i = _regions.begin ();
+ while (i != _regions.end() && i->content != c) {
+ ++i;
+ }
+
+ if (i != _regions.end ()) {
+ _regions.erase (i);
+ Changed ();
+ }
}
void
-Playlist::disable_subtitles ()
+Playlist::set_loop (int l)
{
- /* XXX */
+ _loop = l;
+ Changed ();
}
bool
-Playlist::pass ()
+Playlist::has_subtitles () const
{
- if (!_have_setup_decoders) {
- setup_decoders ();
- _have_setup_decoders = true;
- }
-
- 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;
- }
- }
- }
-
- if (_video_from == VIDEO_IMAGEMAGICK) {
- if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- if ((*_imagemagick_decoder)->pass ()) {
- _imagemagick_decoder++;
- }
-
- if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
- done = false;
- }
+ for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+ shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (i->content);
+ if (fc && !fc->subtitle_streams().empty()) {
+ return true;
}
}
- /* XXX: sndfile */
-
- return done;
+ return false;
}
-void
-Playlist::set_progress (shared_ptr<Job> job)
+Playlist::Region::Region (shared_ptr<Content> c, Time t, Playlist* p)
+ : content (c)
+ , time (t)
{
- /* XXX */
+ connection = c->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
}
-void
-Playlist::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
+Playlist::Region::Region (shared_ptr<const cxml::Node> node, Playlist* p)
{
- Video (i, same, s);
+ shared_ptr<const cxml::Node> content_node = node->node_child ("Content");
+ string const type = content_node->string_child ("Type");
+
+ if (type == "FFmpeg") {
+ content.reset (new FFmpegContent (content_node));
+ } else if (type == "ImageMagick") {
+ content.reset (new ImageMagickContent (content_node));
+ } else if (type == "Sndfile") {
+ content.reset (new SndfileContent (content_node));
+ }
+
+ time = node->number_child<Time> ("Time");
+ connection = content->Changed.connect (bind (&Playlist::content_changed, p, _1, _2));
}
void
-Playlist::process_audio (shared_ptr<AudioBuffers> b)
+Playlist::Region::as_xml (xmlpp::Node* node) const
{
- Audio (b);
+ xmlpp::Node* sub = node->add_child ("Content");
+ content->as_xml (sub);
+ node->add_child ("Time")->add_child_text (lexical_cast<string> (time));
}
-bool
-Playlist::seek (double t)
+class FrameRateCandidate
{
- bool r = false;
-
- switch (_video_from) {
- case VIDEO_NONE:
- break;
- case VIDEO_FFMPEG:
- if (_ffmpeg_decoder->seek (t)) {
- r = true;
- }
- break;
- case VIDEO_IMAGEMAGICK:
- if ((*_imagemagick_decoder)->seek (t)) {
- r = true;
- }
- break;
- }
+public:
+ FrameRateCandidate (float source_, int dcp_)
+ : source (source_)
+ , dcp (dcp_)
+ {}
- /* XXX: don't seek audio because we don't need to... */
+ float source;
+ int dcp;
+};
- return r;
-}
-
-bool
-Playlist::seek_to_last ()
+int
+Playlist::best_dcp_frame_rate () const
{
- bool r = false;
-
- switch (_video_from) {
- case VIDEO_NONE:
- break;
- case VIDEO_FFMPEG:
- if (_ffmpeg_decoder->seek_to_last ()) {
- r = true;
- }
- break;
- case VIDEO_IMAGEMAGICK:
- if ((*_imagemagick_decoder)->seek_to_last ()) {
- r = true;
- }
- break;
- }
-
- /* XXX: don't seek audio because we don't need to... */
+ list<int> const allowed_dcp_frame_rates = Config::instance()->allowed_dcp_frame_rates ();
- return r;
-}
+ /* Work out what rates we could manage, including those achieved by using skip / repeat. */
+ list<FrameRateCandidate> candidates;
-void
-Playlist::setup_decoders ()
-{
- if (_video_from == VIDEO_FFMPEG || _audio_from == AUDIO_FFMPEG) {
- _ffmpeg_decoder.reset (
- new FFmpegDecoder (
- _film, _ffmpeg, _video_from == VIDEO_FFMPEG, _audio_from == AUDIO_FFMPEG, _film->with_subtitles(), _video_sync
- )
- );
- }
-
- if (_video_from == VIDEO_FFMPEG) {
- _ffmpeg_decoder->connect_video (shared_from_this ());
+ /* 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 (_audio_from == AUDIO_FFMPEG) {
- _ffmpeg_decoder->connect_audio (shared_from_this ());
+ /* 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));
}
- if (_video_from == VIDEO_IMAGEMAGICK) {
- for (list<shared_ptr<ImageMagickContent> >::iterator i = _imagemagick.begin(); i != _imagemagick.end(); ++i) {
- shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
- _imagemagick_decoders.push_back (d);
- d->connect_video (shared_from_this ());
+ /* 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 (RegionList::const_iterator j = _regions.begin(); j != _regions.end(); ++j) {
+ shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (j->content);
+ if (!vc) {
+ continue;
+ }
+
+ this_error += fabs (i->source - vc->video_frame_rate ());
}
- _imagemagick_decoder = _imagemagick_decoders.begin ();
+ if (this_error < error) {
+ error = this_error;
+ best = *i;
+ }
+
+ ++i;
}
- if (_audio_from == AUDIO_SNDFILE) {
- for (list<shared_ptr<SndfileContent> >::iterator i = _sndfile.begin(); i != _sndfile.end(); ++i) {
- shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
- _sndfile_decoders.push_back (d);
- d->connect_audio (shared_from_this ());
- }
+ if (!best) {
+ return 24;
}
+
+ return best->dcp;
}
-void
-Playlist::disable_video_sync ()
+Time
+Playlist::length (shared_ptr<const Film> film) const
{
- _video_sync = false;
+ Time len = 0;
+ for (RegionList::const_iterator i = _regions.begin(); i != _regions.end(); ++i) {
+ Time const t = i->time + i->content->length (film);
+ len = max (len, t);
+ }
+
+ return len;
}