X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fplayer.cc;h=70d6fa8773a445977da35edd2d2b286a042221dd;hb=f98c4cdec82fc2fbdcc4ca19748d09b0ea0556b4;hp=68a962cddb56f545d10eb75b0147c224f42da959;hpb=eecad2ada9fe83ab90be735c2aa333dba73434b4;p=dcpomatic.git diff --git a/src/lib/player.cc b/src/lib/player.cc index 68a962cdd..70d6fa877 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -17,34 +17,93 @@ */ +#include #include "player.h" #include "film.h" #include "ffmpeg_decoder.h" #include "ffmpeg_content.h" -#include "imagemagick_decoder.h" -#include "imagemagick_content.h" +#include "still_image_decoder.h" +#include "still_image_content.h" +#include "moving_image_decoder.h" +#include "moving_image_content.h" #include "sndfile_decoder.h" #include "sndfile_content.h" +#include "subtitle_content.h" #include "playlist.h" #include "job.h" +#include "image.h" +#include "ratio.h" +#include "resampler.h" +#include "log.h" +#include "scaler.h" using std::list; using std::cout; +using std::min; +using std::max; using std::vector; +using std::pair; +using std::map; using boost::shared_ptr; using boost::weak_ptr; using boost::dynamic_pointer_cast; +//#define DEBUG_PLAYER 1 + +class Piece +{ +public: + Piece (shared_ptr c) + : content (c) + , video_position (c->position ()) + , audio_position (c->position ()) + {} + + Piece (shared_ptr c, shared_ptr d) + : content (c) + , decoder (d) + , video_position (c->position ()) + , audio_position (c->position ()) + {} + + shared_ptr content; + shared_ptr decoder; + Time video_position; + Time audio_position; +}; + +#ifdef DEBUG_PLAYER +std::ostream& operator<<(std::ostream& s, Piece const & p) +{ + if (dynamic_pointer_cast (p.content)) { + s << "\tffmpeg "; + } else if (dynamic_pointer_cast (p.content)) { + s << "\tstill image"; + } else if (dynamic_pointer_cast (p.content)) { + s << "\tsndfile "; + } + + s << " at " << p.content->position() << " until " << p.content->end(); + + return s; +} +#endif + Player::Player (shared_ptr f, shared_ptr p) : _film (f) , _playlist (p) , _video (true) , _audio (true) - , _subtitles (true) - , _have_valid_decoders (false) + , _have_valid_pieces (false) + , _video_position (0) + , _audio_position (0) + , _audio_merger (f->audio_channels(), bind (&Film::time_to_audio_frames, f.get(), _1), bind (&Film::audio_frames_to_time, f.get(), _1)) + , _last_emit_was_black (false) { - _playlist->Changed.connect (bind (&Player::playlist_changed, this)); - _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2)); + _playlist_changed_connection = _playlist->Changed.connect (bind (&Player::playlist_changed, this)); + _playlist_content_changed_connection = _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2, _3)); + _film_changed_connection = _film->Changed.connect (bind (&Player::film_changed, this, _1)); + set_video_container_size (_film->container()->size (_film->full_frame ())); } void @@ -59,283 +118,537 @@ Player::disable_audio () _audio = false; } -void -Player::disable_subtitles () -{ - _subtitles = false; -} - 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 && _video_decoder < _video_decoders.size ()) { - /* Run video decoder; this may also produce audio */ +#ifdef DEBUG_PLAYER + cout << "= PASS\n"; +#endif + + Time earliest_t = TIME_MAX; + shared_ptr earliest; + enum { + VIDEO, + AUDIO + } type = VIDEO; - if (_video_decoders[_video_decoder]->pass ()) { - _video_decoder++; + for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { + if ((*i)->decoder->done ()) { + continue; } - - if (_video_decoder < _video_decoders.size ()) { - done = false; + + if (_video && dynamic_pointer_cast ((*i)->decoder)) { + if ((*i)->video_position < earliest_t) { + earliest_t = (*i)->video_position; + earliest = *i; + type = VIDEO; + } } - - } - if (!_video && _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG && _sequential_audio_decoder < _audio_decoders.size ()) { + if (_audio && dynamic_pointer_cast ((*i)->decoder)) { + if ((*i)->audio_position < earliest_t) { + earliest_t = (*i)->audio_position; + earliest = *i; + type = AUDIO; + } + } + } - /* We're not producing video, so we may need to run FFmpeg content to get the audio */ + if (!earliest) { +#ifdef DEBUG_PLAYER + cout << "no earliest piece.\n"; +#endif - if (_audio_decoders[_sequential_audio_decoder]->pass ()) { - _sequential_audio_decoder++; + flush (); + return true; + } + + switch (type) { + case VIDEO: + if (earliest_t > _video_position) { +#ifdef DEBUG_PLAYER + cout << "no video here; emitting black frame (earliest=" << earliest_t << ", video_position=" << _video_position << ").\n"; +#endif + emit_black (); + } else { +#ifdef DEBUG_PLAYER + cout << "Pass video " << *earliest << "\n"; +#endif + earliest->decoder->pass (); } - - if (_sequential_audio_decoder < _audio_decoders.size ()) { - done = false; + break; + + case AUDIO: + if (earliest_t > _audio_position) { +#ifdef DEBUG_PLAYER + cout << "no audio here (none until " << earliest_t << "); emitting silence.\n"; +#endif + emit_silence (_film->time_to_audio_frames (earliest_t - _audio_position)); + } else { +#ifdef DEBUG_PLAYER + cout << "Pass audio " << *earliest << "\n"; +#endif + earliest->decoder->pass (); + + if (earliest->decoder->done()) { + shared_ptr ac = dynamic_pointer_cast (earliest->content); + assert (ac); + shared_ptr re = resampler (ac, false); + if (re) { + shared_ptr b = re->flush (); + if (b->frames ()) { + process_audio (earliest, b, ac->audio_length ()); + } + } + } } - + break; } - if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) { - - /* We're getting audio from SndfileContent */ - - for (vector >::iterator i = _audio_decoders.begin(); i != _audio_decoders.end(); ++i) { - if (!(*i)->pass ()) { - done = false; + if (_audio) { + Time audio_done_up_to = TIME_MAX; + for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { + if (dynamic_pointer_cast ((*i)->decoder)) { + audio_done_up_to = min (audio_done_up_to, (*i)->audio_position); } } - Audio (_audio_buffers, _audio_time.get()); - _audio_buffers.reset (); - _audio_time = boost::none; + TimedAudioBuffers