+/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
+
/*
Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
#include "sndfile_content.h"
#include "playlist.h"
#include "job.h"
+#include "image.h"
+#include "null_content.h"
+#include "black_decoder.h"
+#include "silence_decoder.h"
using std::list;
using std::cout;
+using std::min;
+using std::max;
+using std::vector;
using boost::shared_ptr;
using boost::weak_ptr;
using boost::dynamic_pointer_cast;
+struct Piece
+{
+ Piece (shared_ptr<Content> c, shared_ptr<Decoder> d)
+ : content (c)
+ , decoder (d)
+ {}
+
+ shared_ptr<Content> content;
+ shared_ptr<Decoder> decoder;
+};
+
Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
: _film (f)
, _playlist (p)
, _video (true)
, _audio (true)
, _subtitles (true)
- , _have_valid_decoders (false)
- , _video_sync (true)
+ , _have_valid_pieces (false)
+ , _position (0)
+ , _audio_buffers (f->dcp_audio_channels(), 0)
+ , _next_audio (0)
{
_playlist->Changed.connect (bind (&Player::playlist_changed, this));
_playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
bool
Player::pass ()
{
- if (!_have_valid_decoders) {
- setup_decoders ();
- _have_valid_decoders = true;
+ if (!_have_valid_pieces) {
+ setup_pieces ();
+ _have_valid_pieces = true;
}
-
- bool done = true;
-
- if (_video_decoder != _video_decoders.end ()) {
- if ((*_video_decoder)->pass ()) {
- _video_decoder++;
+
+ /* Here we are just finding the active decoder with the earliest last emission time, then
+ calling pass on it.
+ */
+
+ Time earliest_t = TIME_MAX;
+ shared_ptr<Piece> earliest;
+
+ for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+ cout << "check " << (*i)->content->file() << " start=" << (*i)->content->start() << ", next=" << (*i)->decoder->next() << ", end=" << (*i)->content->end() << "\n";
+ if (((*i)->decoder->next() + (*i)->content->start()) >= (*i)->content->end()) {
+ continue;
+ }
+
+ if (!_audio && dynamic_pointer_cast<SndfileContent> ((*i)->content)) {
+ continue;
}
- if (_video_decoder != _video_decoders.end ()) {
- done = false;
+ Time const t = (*i)->content->start() + (*i)->decoder->next();
+ if (t < earliest_t) {
+ cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n";
+ earliest_t = t;
+ earliest = *i;
}
}
- if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
- for (list<shared_ptr<SndfileDecoder> >::iterator i = _sndfile_decoders.begin(); i != _sndfile_decoders.end(); ++i) {
- if (!(*i)->pass ()) {
- done = false;
- }
- }
+ if (!earliest) {
+ return true;
+ }
- Audio (_sndfile_buffers);
- _sndfile_buffers.reset ();
+ cout << "PASS:\n";
+ cout << "\tpass " << earliest->content->file() << " ";
+ if (dynamic_pointer_cast<FFmpegContent> (earliest->content)) {
+ cout << " FFmpeg.\n";
+ } else if (dynamic_pointer_cast<ImageMagickContent> (earliest->content)) {
+ cout << " ImageMagickContent.\n";
+ } else if (dynamic_pointer_cast<SndfileContent> (earliest->content)) {
+ cout << " SndfileContent.\n";
+ } else if (dynamic_pointer_cast<BlackDecoder> (earliest->decoder)) {
+ cout << " Black.\n";
+ } else if (dynamic_pointer_cast<SilenceDecoder> (earliest->decoder)) {
+ cout << " Silence.\n";
}
+
+ earliest->decoder->pass ();
+ _position = earliest->content->start() + earliest->decoder->next ();
+ cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n";
- return done;
+ return false;
}
void
-Player::set_progress (shared_ptr<Job> job)
+Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, shared_ptr<Subtitle> sub, Time time)
{
- /* Assume progress can be divined from how far through the video we are */
-
- if (_video_decoder == _video_decoders.end() || !_playlist->video_length()) {
+ cout << "[V]\n";
+
+ shared_ptr<Content> content = weak_content.lock ();
+ if (!content) {
return;
}
- ContentVideoFrame p = 0;
- list<shared_ptr<VideoDecoder> >::iterator i = _video_decoders.begin ();
- while (i != _video_decoders.end() && i != _video_decoder) {
- p += (*i)->video_length ();
- }
-
- job->set_progress (float ((*_video_decoder)->video_frame ()) / _playlist->video_length ());
+ time += content->start ();
+
+ Video (image, same, sub, time);
}
void
-Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
+Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
{
- Video (i, same, s);
+ shared_ptr<Content> content = weak_content.lock ();
+ if (!content) {
+ return;
+ }
+
+ /* The time of this audio may indicate that some of our buffered audio is not going to
+ be added to any more, so it can be emitted.
+ */
+
+ time += content->start ();
+
+ if (time > _next_audio) {
+ /* We can emit some audio from our buffers */
+ assert (_film->time_to_audio_frames (time - _next_audio) <= _audio_buffers.frames());
+ OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio);
+ shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
+ emit->copy_from (&_audio_buffers, N, 0, 0);
+ Audio (emit, _next_audio);
+ _next_audio += _film->audio_frames_to_time (N);
+
+ /* And remove it from our buffers */
+ if (_audio_buffers.frames() > N) {
+ _audio_buffers.move (N, 0, _audio_buffers.frames() - N);
+ }
+ _audio_buffers.set_frames (_audio_buffers.frames() - N);
+ }
+
+ /* Now accumulate the new audio into our buffers */
+ _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames());
+ _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ());
}
+/** @return true on error */
void
-Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<AudioBuffers> b)
+Player::seek (Time t)
{
- if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
- AudioMapping mapping = _film->audio_mapping ();
- if (!_sndfile_buffers) {
- _sndfile_buffers.reset (new AudioBuffers (mapping.dcp_channels(), b->frames ()));
- _sndfile_buffers->make_silent ();
- }
+ if (!_have_valid_pieces) {
+ setup_pieces ();
+ _have_valid_pieces = true;
+ }
- for (int i = 0; i < b->channels(); ++i) {
- list<libdcp::Channel> dcp = mapping.content_to_dcp (AudioMapping::Channel (c, i));
- for (list<libdcp::Channel>::iterator j = dcp.begin(); j != dcp.end(); ++j) {
- _sndfile_buffers->accumulate (b, i, static_cast<int> (*j));
- }
- }
+ if (_pieces.empty ()) {
+ return;
+ }
- } else {
- Audio (b);
+ cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
+
+ for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+ Time s = t - (*i)->content->start ();
+ s = max (static_cast<Time> (0), s);
+ s = min ((*i)->content->length(), s);
+ cout << "seek [" << (*i)->content->file() << "," << (*i)->content->start() << "," << (*i)->content->end() << "] to " << s << "\n";
+ (*i)->decoder->seek (s);
}
+
+ /* XXX: don't seek audio because we don't need to... */
}
-/** @return true on error */
-bool
-Player::seek (double t)
+
+void
+Player::seek_back ()
{
- if (!_have_valid_decoders) {
- setup_decoders ();
- _have_valid_decoders = true;
- }
- /* Find the decoder that contains this position */
- _video_decoder = _video_decoders.begin ();
- while (_video_decoder != _video_decoders.end ()) {
- double const this_length = double ((*_video_decoder)->video_length()) / _film->video_frame_rate ();
- if (t < this_length) {
- break;
- }
- t -= this_length;
- ++_video_decoder;
- }
-
- if (_video_decoder != _video_decoders.end()) {
- (*_video_decoder)->seek (t);
- } else {
- return true;
- }
+}
- /* XXX: don't seek audio because we don't need to... */
+void
+Player::seek_forward ()
+{
- return false;
}
+struct ContentSorter
+{
+ bool operator() (shared_ptr<Content> a, shared_ptr<Content> b)
+ {
+ return a->start() < b->start();
+ }
+};
+
void
-Player::setup_decoders ()
+Player::setup_pieces ()
{
- _video_decoders.clear ();
- _video_decoder = _video_decoders.end ();
- _sndfile_decoders.clear ();
+// cout << "----- Player SETUP PIECES.\n";
+
+ list<shared_ptr<Piece> > old_pieces = _pieces;
+
+ _pieces.clear ();
+
+ Playlist::ContentList content = _playlist->content ();
+ sort (content.begin(), content.end(), ContentSorter ());
- if (_video) {
- list<shared_ptr<const VideoContent> > vc = _playlist->video ();
- for (list<shared_ptr<const VideoContent> >::iterator i = vc.begin(); i != vc.end(); ++i) {
+ for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
+
+ shared_ptr<Decoder> decoder;
+
+ /* XXX: into content? */
- shared_ptr<VideoDecoder> d;
+ shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
+ if (fc) {
+ shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio, _subtitles));
- /* XXX: into content? */
+ fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
+ fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
+
+ decoder = fd;
+// cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
+ }
+
+ shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
+ if (ic) {
+ shared_ptr<ImageMagickDecoder> id;
- shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
- if (fc) {
- shared_ptr<FFmpegDecoder> fd (
- new FFmpegDecoder (
- _film, fc, _video,
- _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
- _subtitles,
- _video_sync
- )
- );
-
- if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
- fd->Audio.connect (bind (&Player::process_audio, this, fc, _1));
+ /* See if we can re-use an old ImageMagickDecoder */
+ for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
+ shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
+ if (imd && imd->content() == ic) {
+ id = imd;
}
-
- d = fd;
}
- shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
- if (ic) {
- d.reset (new ImageMagickDecoder (_film, ic));
+ if (!id) {
+ id.reset (new ImageMagickDecoder (_film, ic));
+ id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
}
- d->connect_video (shared_from_this ());
- _video_decoders.push_back (d);
+ decoder = id;
+// cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
}
- _video_decoder = _video_decoders.begin ();
- }
+ shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
+ if (sc) {
+ shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
+ sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
- 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->Audio.connect (bind (&Player::process_audio, this, *i, _1));
+ decoder = sd;
+// cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
}
- }
-}
-
-void
-Player::disable_video_sync ()
-{
- _video_sync = false;
-}
-double
-Player::last_video_time () const
-{
- double t = 0;
- for (list<shared_ptr<VideoDecoder> >::const_iterator i = _video_decoders.begin(); i != _video_decoder; ++i) {
- t += (*i)->video_length() / (*i)->video_frame_rate ();
+ _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
}
- return t + (*_video_decoder)->last_content_time ();
+ /* Fill in visual gaps with black and audio gaps with silence */
+
+ Time video_pos = 0;
+ Time audio_pos = 0;
+ list<shared_ptr<Piece> > pieces_copy = _pieces;
+ for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
+ if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
+ Time const diff = (*i)->content->start() - video_pos;
+ if (diff > 0) {
+ shared_ptr<NullContent> nc (new NullContent (_film, video_pos, diff));
+ shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
+ bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3, _4));
+ _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
+// cout << "\tblack @ " << video_pos << " -- " << (video_pos + diff) << "\n";
+ }
+
+ video_pos = (*i)->content->end();
+ } else {
+ Time const diff = (*i)->content->start() - audio_pos;
+ if (diff > 0) {
+ shared_ptr<NullContent> nc (new NullContent (_film, audio_pos, diff));
+ shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
+ sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
+ _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
+// cout << "\tsilence @ " << audio_pos << " -- " << (audio_pos + diff) << "\n";
+ }
+ audio_pos = (*i)->content->end();
+ }
+ }
}
void
return;
}
- if (p == VideoContentProperty::VIDEO_LENGTH) {
- if (dynamic_pointer_cast<FFmpegContent> (c)) {
- /* FFmpeg content length changes are serious; we need new decoders */
- _have_valid_decoders = false;
- }
+ if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
+ _have_valid_pieces = false;
}
}
void
Player::playlist_changed ()
{
- _have_valid_decoders = false;
+ _have_valid_pieces = false;
}