Some tidying up. Do encode progress in the writer to improve progress bar movement...
[dcpomatic.git] / src / lib / player.cc
index a84117a4e4ce0cb14821bcfea050d3818c4e242b..95036cfe0ae18f1ca24ffebea6a8f2a899eb61c6 100644 (file)
@@ -75,10 +75,10 @@ Player::pass ()
        
        bool done = true;
        
-       if (_video_decoder < _video_decoders.size ()) {
+       if (_video && _video_decoder < _video_decoders.size ()) {
 
                /* Run video decoder; this may also produce audio */
-               
+
                if (_video_decoders[_video_decoder]->pass ()) {
                        _video_decoder++;
                }
@@ -87,7 +87,9 @@ Player::pass ()
                        done = false;
                }
                
-       } else if (!_video && _playlist->audio_from() == Playlist::AUDIO_FFMPEG && _sequential_audio_decoder < _audio_decoders.size ()) {
+       }
+
+       if (!_video && _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG && _sequential_audio_decoder < _audio_decoders.size ()) {
 
                /* We're not producing video, so we may need to run FFmpeg content to get the audio */
                
@@ -99,8 +101,10 @@ Player::pass ()
                        done = false;
                }
                
-       } else if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
+       }
 
+       if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
+               
                /* We're getting audio from SndfileContent */
                
                for (vector<shared_ptr<AudioDecoder> >::iterator i = _audio_decoders.begin(); i != _audio_decoders.end(); ++i) {
@@ -118,25 +122,13 @@ Player::pass ()
 }
 
 void
-Player::set_progress (shared_ptr<Job> job)
-{
-       /* Assume progress can be divined from how far through the video we are */
-
-       if (_video_decoder >= _video_decoders.size() || !_playlist->video_length()) {
-               return;
-       }
-
-       job->set_progress ((_video_start[_video_decoder] + _video_decoders[_video_decoder]->video_frame()) / _playlist->video_length ());
-}
-
-void
-Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s, double t)
+Player::process_video (shared_ptr<const Image> i, bool same, shared_ptr<Subtitle> s, double t)
 {
        Video (i, same, s, _video_start[_video_decoder] + t);
 }
 
 void
-Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<AudioBuffers> b, double t)
+Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<const AudioBuffers> b, double t)
 {
        AudioMapping mapping = _film->audio_mapping ();
        if (!_audio_buffers) {
@@ -172,19 +164,21 @@ Player::seek (double t)
                _have_valid_decoders = true;
        }
 
+       if (_video_decoders.empty ()) {
+               return true;
+       }
+
        /* Find the decoder that contains this position */
        _video_decoder = 0;
-       while (_video_decoder < _video_decoders.size ()) {
-               if (t < _video_start[_video_decoder]) {
-                       assert (_video_decoder);
+       while (1) {
+               ++_video_decoder;
+               if (_video_decoder >= _video_decoders.size () || t < _video_start[_video_decoder]) {
                        --_video_decoder;
+                       t -= _video_start[_video_decoder];
                        break;
                }
-
-               t -= _video_start[_video_decoder];
-               ++_video_decoder;
        }
-       
+
        if (_video_decoder < _video_decoders.size()) {
                _video_decoders[_video_decoder]->seek (t);
        } else {
@@ -213,6 +207,8 @@ Player::seek_forward ()
 void
 Player::setup_decoders ()
 {
+       vector<shared_ptr<VideoDecoder> > old_video_decoders = _video_decoders;
+
        _video_decoders.clear ();
        _video_decoder = 0;
        _audio_decoders.clear ();
@@ -223,67 +219,82 @@ Player::setup_decoders ()
 
        double video_so_far = 0;
        double audio_so_far = 0;
-       
-       list<shared_ptr<const VideoContent> > vc = _playlist->video ();
-       for (list<shared_ptr<const VideoContent> >::iterator i = vc.begin(); i != vc.end(); ++i) {
-               
-               shared_ptr<const VideoContent> video_content;
-               shared_ptr<const AudioContent> audio_content;
-               shared_ptr<VideoDecoder> video_decoder;
-               shared_ptr<AudioDecoder> audio_decoder;
-               
-               /* XXX: into content? */
-               
-               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
-                                       )
-                               );
+
+       for (int l = 0; l < _playlist->loop(); ++l) {
+               list<shared_ptr<const VideoContent> > vc = _playlist->video ();
+               for (list<shared_ptr<const VideoContent> >::iterator i = vc.begin(); i != vc.end(); ++i) {
                        
-                       video_content = fc;
-                       audio_content = fc;
-                       video_decoder = fd;
-                       audio_decoder = fd;
-               }
-               
-               shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
-               if (ic) {
-                       video_content = ic;
-                       video_decoder.reset (new ImageMagickDecoder (_film, ic));
+                       shared_ptr<const VideoContent> video_content;
+                       shared_ptr<const AudioContent> audio_content;
+                       shared_ptr<VideoDecoder> video_decoder;
+                       shared_ptr<AudioDecoder> audio_decoder;
+                       
+                       /* XXX: into content? */
+                       
+                       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_content = fc;
+                               audio_content = fc;
+                               video_decoder = fd;
+                               audio_decoder = fd;
+
+                               video_decoder->connect_video (shared_from_this ());
+                       }
+                       
+                       shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
+                       if (ic) {
+                               video_content = ic;
+
+                               /* See if we can re-use an old ImageMagickDecoder */
+                               for (vector<shared_ptr<VideoDecoder> >::const_iterator i = old_video_decoders.begin(); i != old_video_decoders.end(); ++i) {
+                                       shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> (*i);
+                                       if (imd && imd->content() == ic) {
+                                               video_decoder = *i;
+                                       }
+                               }
+
+                               if (!video_decoder) {
+                                       video_decoder.reset (new ImageMagickDecoder (_film, ic));
+                                       video_decoder->connect_video (shared_from_this ());
+                               }
+                       }
+                       
+                       _video_decoders.push_back (video_decoder);
+                       _video_start.push_back (video_so_far);
+                       video_so_far += video_content->video_length() / video_content->video_frame_rate();
+                       
+                       if (audio_decoder && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
+                               audio_decoder->Audio.connect (bind (&Player::process_audio, this, audio_content, _1, _2));
+                               _audio_decoders.push_back (audio_decoder);
+                               _audio_start.push_back (audio_so_far);
+                               audio_so_far += double(audio_content->audio_length()) / audio_content->audio_frame_rate();
+                       }
                }
                
-               video_decoder->connect_video (shared_from_this ());
-               _video_decoders.push_back (video_decoder);
-               _video_start.push_back (video_so_far);
-               video_so_far += video_content->video_length() / video_content->video_frame_rate();
-
-               if (audio_decoder && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
-                       audio_decoder->Audio.connect (bind (&Player::process_audio, this, audio_content, _1, _2));
-                       _audio_decoders.push_back (audio_decoder);
-                       _audio_start.push_back (audio_so_far);
-                       audio_so_far += double(audio_content->audio_length()) / audio_content->audio_frame_rate();
-               }
-       }
-       
-       _video_decoder = 0;
-       _sequential_audio_decoder = 0;
-
-       if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
+               _video_decoder = 0;
+               _sequential_audio_decoder = 0;
                
-               list<shared_ptr<const AudioContent> > ac = _playlist->audio ();
-               for (list<shared_ptr<const AudioContent> >::iterator i = ac.begin(); i != ac.end(); ++i) {
-                       
-                       shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
-                       assert (sc);
+               if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
                        
-                       shared_ptr<AudioDecoder> d (new SndfileDecoder (_film, sc));
-                       d->Audio.connect (bind (&Player::process_audio, this, sc, _1, _2));
-                       _audio_decoders.push_back (d);
-                       _audio_start.push_back (audio_so_far);
+                       list<shared_ptr<const AudioContent> > ac = _playlist->audio ();
+                       for (list<shared_ptr<const AudioContent> >::iterator i = ac.begin(); i != ac.end(); ++i) {
+                               
+                               shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
+                               assert (sc);
+                               
+                               shared_ptr<AudioDecoder> d (new SndfileDecoder (_film, sc));
+                               d->Audio.connect (bind (&Player::process_audio, this, sc, _1, _2));
+                               _audio_decoders.push_back (d);
+                               _audio_start.push_back (audio_so_far);
+                       }
                }
        }
 }
@@ -291,6 +302,10 @@ Player::setup_decoders ()
 double
 Player::last_video_time () const
 {
+       if (_video_decoder >= _video_decoders.size ()) {
+               return 0;
+       }
+       
        return _video_start[_video_decoder] + _video_decoders[_video_decoder]->last_content_time ();
 }
 
@@ -303,10 +318,7 @@ Player::content_changed (weak_ptr<Content> w, int p)
        }
 
        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;
-               }
+               _have_valid_decoders = false;
        }
 }