X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fplayer.cc;h=ff13f95dbd2197be7be681ff520b29ab1ef4422b;hb=996b0c06e23bcb6b300d7b8799df94993692e07d;hp=756c3b85493bf940b8c15f136b7194907fb965d5;hpb=d62877ae6c4e316e43f4052e4b9ba673610012cf;p=dcpomatic.git diff --git a/src/lib/player.cc b/src/lib/player.cc index 756c3b854..ff13f95db 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -1,3 +1,5 @@ +/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */ + /* Copyright (C) 2013 Carl Hetherington @@ -20,27 +22,48 @@ #include "player.h" #include "film.h" #include "ffmpeg_decoder.h" +#include "ffmpeg_content.h" #include "imagemagick_decoder.h" +#include "imagemagick_content.h" #include "sndfile_decoder.h" #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 c, shared_ptr d) + : content (c) + , decoder (d) + {} + + shared_ptr content; + shared_ptr decoder; +}; + Player::Player (shared_ptr f, shared_ptr p) : _film (f) , _playlist (p) , _video (true) , _audio (true) , _subtitles (true) - , _have_valid_decoders (false) - , _ffmpeg_decoder_done (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)); @@ -67,253 +90,249 @@ Player::disable_subtitles () bool Player::pass () { - if (!_have_valid_decoders) { - setup_decoders (); - _have_valid_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 (!_have_valid_pieces) { + setup_pieces (); + _have_valid_pieces = true; } - if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) { - if (_imagemagick_decoder != _imagemagick_decoders.end ()) { - if ((*_imagemagick_decoder)->pass ()) { - _imagemagick_decoder++; - } + /* Here we are just finding the active decoder with the earliest last emission time, then + calling pass on it. + */ - if (_imagemagick_decoder != _imagemagick_decoders.end ()) { - done = false; - } + Time earliest_t = TIME_MAX; + shared_ptr earliest; + + for (list >::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 (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) { - for (list >::iterator i = _sndfile_decoders.begin(); i != _sndfile_decoders.end(); ++i) { - if (!(*i)->pass ()) { - done = false; - } + if (!_audio && dynamic_pointer_cast ((*i)->content)) { + continue; + } + + 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; } + } - Audio (_sndfile_buffers); - _sndfile_buffers.reset (); + if (!earliest) { + return true; } - return done; + cout << "PASS:\n"; + cout << "\tpass " << earliest->content->file() << " "; + if (dynamic_pointer_cast (earliest->content)) { + cout << " FFmpeg.\n"; + } else if (dynamic_pointer_cast (earliest->content)) { + cout << " ImageMagickContent.\n"; + } else if (dynamic_pointer_cast (earliest->content)) { + cout << " SndfileContent.\n"; + } else if (dynamic_pointer_cast (earliest->decoder)) { + cout << " Black.\n"; + } else if (dynamic_pointer_cast (earliest->decoder)) { + cout << " Silence.\n"; + } + + earliest->decoder->pass (); + _position = earliest->content->start() + earliest->decoder->next (); + cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n"; + + return false; } void -Player::set_progress (shared_ptr job) +Player::process_video (weak_ptr weak_content, shared_ptr image, bool same, shared_ptr sub, Time time) { - /* 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 (list >::iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoders.end(); ++i) { - if (_imagemagick_decoder == i) { - job->set_progress (float (n) / _imagemagick_decoders.size ()); - } - ++n; - } - break; - } + cout << "[V]\n"; + + shared_ptr content = weak_content.lock (); + if (!content) { + return; } + + time += content->start (); + + Video (image, same, sub, time); } void -Player::process_video (shared_ptr i, bool same, shared_ptr s) +Player::process_audio (weak_ptr weak_content, shared_ptr audio, Time time) { - Video (i, same, s); + shared_ptr 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 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 c, shared_ptr 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 (); - } - - for (int i = 0; i < b->channels(); ++i) { - list dcp = mapping.content_to_dcp (AudioMapping::Channel (c, i)); - for (list::iterator j = dcp.begin(); j != dcp.end(); ++j) { - _sndfile_buffers->accumulate (b, i, static_cast (*j)); - } - } - - } else { - Audio (b); + if (!_have_valid_pieces) { + setup_pieces (); + _have_valid_pieces = true; } -} -/** @return true on error */ -bool -Player::seek (double t) -{ - if (!_have_valid_decoders) { - setup_decoders (); - _have_valid_decoders = true; + if (_pieces.empty ()) { + return; } - - 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; + cout << "seek to " << t << " " << (t / TIME_HZ) << "\n"; + + for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { + Time s = t - (*i)->content->start (); + s = max (static_cast