#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::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)
+ : _loop (1)
+ , _sequence_video (true)
+ , _sequencing_video (false)
{
}
Playlist::Playlist (shared_ptr<const Playlist> other)
- : _audio_from (other->_audio_from)
- , _loop (other->_loop)
+ : _loop (other->_loop)
{
for (ContentList::const_iterator i = other->_content.begin(); i != other->_content.end(); ++i) {
_content.push_back ((*i)->clone ());
}
-
- setup ();
}
-void
-Playlist::setup ()
+Playlist::~Playlist ()
{
- _audio_from = AUDIO_FFMPEG;
-
- _video.clear ();
- _audio.clear ();
-
- for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
- i->disconnect ();
- }
-
- _content_connections.clear ();
-
- for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
-
- /* Video is video */
- shared_ptr<VideoContent> vc = dynamic_pointer_cast<VideoContent> (*i);
- if (vc) {
- _video.push_back (vc);
- }
-
- /* FFmpegContent is audio if we are doing AUDIO_FFMPEG */
- shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
- if (fc && _audio_from == AUDIO_FFMPEG) {
- _audio.push_back (fc);
- }
-
- /* SndfileContent trumps FFmpegContent for audio */
- shared_ptr<SndfileContent> sc = dynamic_pointer_cast<SndfileContent> (*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);
- }
-
- _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
- }
-}
-
-/** @return Length of our audio */
-ContentAudioFrame
-Playlist::audio_length () const
-{
- ContentAudioFrame len = 0;
-
- switch (_audio_from) {
- case AUDIO_FFMPEG:
- /* FFmpeg content is sequential */
- for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
- len += (*i)->audio_length ();
- }
- break;
- case AUDIO_SNDFILE:
- /* Sndfile content is simultaneous */
- for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
- len = max (len, (*i)->audio_length ());
- }
- break;
- }
-
- return len * _loop;
-}
-
-/** @return number of audio channels */
-int
-Playlist::audio_channels () const
-{
- int channels = 0;
-
- switch (_audio_from) {
- case AUDIO_FFMPEG:
- /* FFmpeg audio is sequential, so use the maximum channel count */
- for (list<shared_ptr<const AudioContent> >::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<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
- channels += (*i)->audio_channels ();
- }
- break;
- }
-
- return channels;
-}
-
-int
-Playlist::audio_frame_rate () const
-{
- if (_audio.empty ()) {
- return 0;
- }
-
- /* XXX: assuming that all content has the same rate */
- return _audio.front()->audio_frame_rate ();
-}
-
-float
-Playlist::video_frame_rate () const
-{
- if (_video.empty ()) {
- return 0;
- }
-
- /* XXX: assuming all the same */
- return _video.front()->video_frame_rate ();
-}
-
-libdcp::Size
-Playlist::video_size () const
-{
- if (_video.empty ()) {
- return libdcp::Size ();
- }
-
- /* XXX: assuming all the same */
- return _video.front()->video_size ();
-}
-
-ContentVideoFrame
-Playlist::video_length () const
-{
- ContentVideoFrame len = 0;
- for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
- len += (*i)->video_length ();
- }
-
- return len * _loop;
-}
-
-bool
-Playlist::has_audio () const
-{
- return !_audio.empty ();
+ _content.clear ();
+ reconnect ();
}
void
Playlist::content_changed (weak_ptr<Content> c, int p)
{
- ContentChanged (c, p);
-}
-
-AudioMapping
-Playlist::default_audio_mapping () const
-{
- AudioMapping m;
- if (_audio.empty ()) {
- return m;
- }
-
- 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);
+ 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;
}
- }
- break;
- }
- case AUDIO_SNDFILE:
- {
- int n = 0;
- for (list<shared_ptr<const AudioContent> >::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;
- }
+ (*i)->set_start (last);
+ last = (*i)->end ();
}
- break;
- }
- }
- return m;
-}
-
-string
-Playlist::audio_digest () const
-{
- string t;
-
- for (list<shared_ptr<const AudioContent> >::const_iterator i = _audio.begin(); i != _audio.end(); ++i) {
- t += (*i)->digest ();
-
- shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
- if (fc) {
- t += lexical_cast<string> (fc->audio_stream()->id);
- }
+ _sequencing_video = false;
}
-
- t += lexical_cast<string> (_loop);
-
- return md5_digest (t.c_str(), t.length());
+
+ ContentChanged (c, p);
}
string
{
string t;
- for (list<shared_ptr<const VideoContent> >::const_iterator i = _video.begin(); i != _video.end(); ++i) {
+ 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()) {
return md5_digest (t.c_str(), t.length());
}
-ContentVideoFrame
-Playlist::content_length () const
-{
- float const vfr = video_frame_rate() > 0 ? video_frame_rate() : 24;
- int const afr = audio_frame_rate() > 0 ? audio_frame_rate() : 48000;
-
- return max (
- video_length(),
- ContentVideoFrame (audio_length() * vfr / afr)
- );
-}
-
+/** @param node <Playlist> node */
void
-Playlist::set_from_xml (shared_ptr<const cxml::Node> node)
+Playlist::set_from_xml (shared_ptr<const Film> film, shared_ptr<const cxml::Node> node)
{
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> c;
-
+
+ boost::shared_ptr<Content> content;
+
if (type == "FFmpeg") {
- c.reset (new FFmpegContent (*i));
+ content.reset (new FFmpegContent (film, *i));
} else if (type == "ImageMagick") {
- c.reset (new ImageMagickContent (*i));
+ content.reset (new ImageMagickContent (film, *i));
} else if (type == "Sndfile") {
- c.reset (new SndfileContent (*i));
+ content.reset (new SndfileContent (film, *i));
}
- _content.push_back (c);
+ _content.push_back (content);
}
+ reconnect ();
_loop = node->number_child<int> ("Loop");
-
- setup ();
+ _sequence_video = node->bool_child ("SequenceVideo");
}
+/** @param node <Playlist> node */
void
Playlist::as_xml (xmlpp::Node* node)
{
}
node->add_child("Loop")->add_child_text(lexical_cast<string> (_loop));
+ node->add_child("SequenceVideo")->add_child_text(_sequence_video ? "1" : "0");
}
void
Playlist::add (shared_ptr<Content> c)
{
_content.push_back (c);
- setup ();
+ reconnect ();
Changed ();
}
void
Playlist::remove (shared_ptr<Content> c)
{
- ContentList::iterator i = find (_content.begin(), _content.end(), c);
+ ContentList::iterator i = _content.begin ();
+ while (i != _content.end() && *i != c) {
+ ++i;
+ }
+
if (i != _content.end ()) {
_content.erase (i);
+ Changed ();
}
+}
- setup ();
+void
+Playlist::set_loop (int l)
+{
+ _loop = l;
Changed ();
}
-void
-Playlist::move_earlier (shared_ptr<Content> c)
+bool
+Playlist::has_subtitles () const
{
- ContentList::iterator i = find (_content.begin(), _content.end(), c);
- if (i == _content.begin () || i == _content.end()) {
- return;
+ 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;
+ }
}
- ContentList::iterator j = i;
- --j;
+ return false;
+}
- swap (*i, *j);
+class FrameRateCandidate
+{
+public:
+ FrameRateCandidate (float source_, int dcp_)
+ : source (source_)
+ , dcp (dcp_)
+ {}
- setup ();
- Changed ();
-}
+ float source;
+ int dcp;
+};
-void
-Playlist::move_later (shared_ptr<Content> c)
+int
+Playlist::best_dcp_frame_rate () const
{
- ContentList::iterator i = find (_content.begin(), _content.end(), c);
- if (i == _content.end()) {
- return;
+ 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));
}
- ContentList::iterator j = i;
- ++j;
- if (j == _content.end ()) {
- return;
+ /* 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));
}
- swap (*i, *j);
+ /* 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;
+ }
- setup ();
- Changed ();
+ this_error += fabs (i->source - vc->video_frame_rate ());
+ }
+
+ if (this_error < error) {
+ error = this_error;
+ best = *i;
+ }
+
+ ++i;
+ }
+
+ if (!best) {
+ return 24;
+ }
+
+ return best->dcp;
}
-void
-Playlist::set_loop (int l)
+Time
+Playlist::length () const
{
- _loop = l;
- Changed ();
+ Time len = 0;
+ for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
+ len = max (len, (*i)->end ());
+ }
+
+ return len;
}
+
+void
+Playlist::reconnect ()
+{
+ for (list<boost::signals2::connection>::iterator i = _content_connections.begin(); i != _content_connections.end(); ++i) {
+ i->disconnect ();
+ }
+
+ _content_connections.clear ();
-shared_ptr<FFmpegContent>
-Playlist::ffmpeg () const
+ for (ContentList::iterator i = _content.begin(); i != _content.end(); ++i) {
+ _content_connections.push_back ((*i)->Changed.connect (bind (&Playlist::content_changed, this, _1, _2)));
+ }
+}
+
+Time
+Playlist::video_end () const
{
+ Time end = 0;
for (ContentList::const_iterator i = _content.begin(); i != _content.end(); ++i) {
- shared_ptr<FFmpegContent> fc = dynamic_pointer_cast<FFmpegContent> (*i);
- if (fc) {
- return fc;
+ if (dynamic_pointer_cast<const VideoContent> (*i)) {
+ end = max (end, (*i)->end ());
}
}
- return shared_ptr<FFmpegContent> ();
+ return end;
+}
+
+void
+Playlist::set_sequence_video (bool s)
+{
+ _sequence_video = s;
}
bool
-Playlist::has_subtitles () const
+ContentSorter::operator() (shared_ptr<Content> a, shared_ptr<Content> b)
{
- shared_ptr<FFmpegContent> fc = ffmpeg ();
- if (!fc) {
- return false;
- }
-
- return !fc->subtitle_streams().empty();
+ return a->start() < b->start();
}